a373b09c62b3bd620e2daea2dce128ed26757310
[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 void read_signatured_type_at_offset (struct objfile *objfile,
1211 struct dwarf2_section_info *sect,
1212 unsigned int offset);
1213
1214 static void read_signatured_type (struct objfile *,
1215 struct signatured_type *type_sig);
1216
1217 /* memory allocation interface */
1218
1219 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1220
1221 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1222
1223 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1224
1225 static void initialize_cu_func_list (struct dwarf2_cu *);
1226
1227 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1228 struct dwarf2_cu *);
1229
1230 static void dwarf_decode_macros (struct line_header *, unsigned int,
1231 char *, bfd *, struct dwarf2_cu *);
1232
1233 static int attr_form_is_block (struct attribute *);
1234
1235 static int attr_form_is_section_offset (struct attribute *);
1236
1237 static int attr_form_is_constant (struct attribute *);
1238
1239 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1240 struct dwarf2_loclist_baton *baton,
1241 struct attribute *attr);
1242
1243 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1244 struct symbol *sym,
1245 struct dwarf2_cu *cu);
1246
1247 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1248 struct abbrev_info *abbrev,
1249 struct dwarf2_cu *cu);
1250
1251 static void free_stack_comp_unit (void *);
1252
1253 static hashval_t partial_die_hash (const void *item);
1254
1255 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1256
1257 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1258 (unsigned int offset, struct objfile *objfile);
1259
1260 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1261 (unsigned int offset, struct objfile *objfile);
1262
1263 static void init_one_comp_unit (struct dwarf2_cu *cu,
1264 struct objfile *objfile);
1265
1266 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1267 struct die_info *comp_unit_die);
1268
1269 static void free_one_comp_unit (void *);
1270
1271 static void free_cached_comp_units (void *);
1272
1273 static void age_cached_comp_units (void);
1274
1275 static void free_one_cached_comp_unit (void *);
1276
1277 static struct type *set_die_type (struct die_info *, struct type *,
1278 struct dwarf2_cu *);
1279
1280 static void create_all_comp_units (struct objfile *);
1281
1282 static int create_debug_types_hash_table (struct objfile *objfile);
1283
1284 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1285 struct objfile *);
1286
1287 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1288
1289 static void dwarf2_add_dependence (struct dwarf2_cu *,
1290 struct dwarf2_per_cu_data *);
1291
1292 static void dwarf2_mark (struct dwarf2_cu *);
1293
1294 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1295
1296 static struct type *get_die_type_at_offset (unsigned int,
1297 struct dwarf2_per_cu_data *per_cu);
1298
1299 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1300
1301 static void dwarf2_release_queue (void *dummy);
1302
1303 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1304 struct objfile *objfile);
1305
1306 static void process_queue (struct objfile *objfile);
1307
1308 static void find_file_and_directory (struct die_info *die,
1309 struct dwarf2_cu *cu,
1310 char **name, char **comp_dir);
1311
1312 static char *file_full_name (int file, struct line_header *lh,
1313 const char *comp_dir);
1314
1315 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1316 gdb_byte *info_ptr,
1317 gdb_byte *buffer,
1318 unsigned int buffer_size,
1319 bfd *abfd);
1320
1321 static void init_cu_die_reader (struct die_reader_specs *reader,
1322 struct dwarf2_cu *cu);
1323
1324 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1325
1326 #if WORDS_BIGENDIAN
1327
1328 /* Convert VALUE between big- and little-endian. */
1329 static offset_type
1330 byte_swap (offset_type value)
1331 {
1332 offset_type result;
1333
1334 result = (value & 0xff) << 24;
1335 result |= (value & 0xff00) << 8;
1336 result |= (value & 0xff0000) >> 8;
1337 result |= (value & 0xff000000) >> 24;
1338 return result;
1339 }
1340
1341 #define MAYBE_SWAP(V) byte_swap (V)
1342
1343 #else
1344 #define MAYBE_SWAP(V) (V)
1345 #endif /* WORDS_BIGENDIAN */
1346
1347 /* The suffix for an index file. */
1348 #define INDEX_SUFFIX ".gdb-index"
1349
1350 static const char *dwarf2_physname (char *name, struct die_info *die,
1351 struct dwarf2_cu *cu);
1352
1353 /* Try to locate the sections we need for DWARF 2 debugging
1354 information and return true if we have enough to do something.
1355 NAMES points to the dwarf2 section names, or is NULL if the standard
1356 ELF names are used. */
1357
1358 int
1359 dwarf2_has_info (struct objfile *objfile,
1360 const struct dwarf2_debug_sections *names)
1361 {
1362 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1363 if (!dwarf2_per_objfile)
1364 {
1365 /* Initialize per-objfile state. */
1366 struct dwarf2_per_objfile *data
1367 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1368
1369 memset (data, 0, sizeof (*data));
1370 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1371 dwarf2_per_objfile = data;
1372
1373 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1374 (void *) names);
1375 dwarf2_per_objfile->objfile = objfile;
1376 }
1377 return (dwarf2_per_objfile->info.asection != NULL
1378 && dwarf2_per_objfile->abbrev.asection != NULL);
1379 }
1380
1381 /* When loading sections, we look either for uncompressed section or for
1382 compressed section names. */
1383
1384 static int
1385 section_is_p (const char *section_name,
1386 const struct dwarf2_section_names *names)
1387 {
1388 if (names->normal != NULL
1389 && strcmp (section_name, names->normal) == 0)
1390 return 1;
1391 if (names->compressed != NULL
1392 && strcmp (section_name, names->compressed) == 0)
1393 return 1;
1394 return 0;
1395 }
1396
1397 /* This function is mapped across the sections and remembers the
1398 offset and size of each of the debugging sections we are interested
1399 in. */
1400
1401 static void
1402 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1403 {
1404 const struct dwarf2_debug_sections *names;
1405
1406 if (vnames == NULL)
1407 names = &dwarf2_elf_names;
1408 else
1409 names = (const struct dwarf2_debug_sections *) vnames;
1410
1411 if (section_is_p (sectp->name, &names->info))
1412 {
1413 dwarf2_per_objfile->info.asection = sectp;
1414 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1415 }
1416 else if (section_is_p (sectp->name, &names->abbrev))
1417 {
1418 dwarf2_per_objfile->abbrev.asection = sectp;
1419 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1420 }
1421 else if (section_is_p (sectp->name, &names->line))
1422 {
1423 dwarf2_per_objfile->line.asection = sectp;
1424 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1425 }
1426 else if (section_is_p (sectp->name, &names->loc))
1427 {
1428 dwarf2_per_objfile->loc.asection = sectp;
1429 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1430 }
1431 else if (section_is_p (sectp->name, &names->macinfo))
1432 {
1433 dwarf2_per_objfile->macinfo.asection = sectp;
1434 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1435 }
1436 else if (section_is_p (sectp->name, &names->str))
1437 {
1438 dwarf2_per_objfile->str.asection = sectp;
1439 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1440 }
1441 else if (section_is_p (sectp->name, &names->frame))
1442 {
1443 dwarf2_per_objfile->frame.asection = sectp;
1444 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1445 }
1446 else if (section_is_p (sectp->name, &names->eh_frame))
1447 {
1448 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1449
1450 if (aflag & SEC_HAS_CONTENTS)
1451 {
1452 dwarf2_per_objfile->eh_frame.asection = sectp;
1453 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1454 }
1455 }
1456 else if (section_is_p (sectp->name, &names->ranges))
1457 {
1458 dwarf2_per_objfile->ranges.asection = sectp;
1459 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1460 }
1461 else if (section_is_p (sectp->name, &names->types))
1462 {
1463 struct dwarf2_section_info type_section;
1464
1465 memset (&type_section, 0, sizeof (type_section));
1466 type_section.asection = sectp;
1467 type_section.size = bfd_get_section_size (sectp);
1468
1469 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1470 &type_section);
1471 }
1472 else if (section_is_p (sectp->name, &names->gdb_index))
1473 {
1474 dwarf2_per_objfile->gdb_index.asection = sectp;
1475 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1476 }
1477
1478 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1479 && bfd_section_vma (abfd, sectp) == 0)
1480 dwarf2_per_objfile->has_section_at_zero = 1;
1481 }
1482
1483 /* Decompress a section that was compressed using zlib. Store the
1484 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1485
1486 static void
1487 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1488 gdb_byte **outbuf, bfd_size_type *outsize)
1489 {
1490 bfd *abfd = objfile->obfd;
1491 #ifndef HAVE_ZLIB_H
1492 error (_("Support for zlib-compressed DWARF data (from '%s') "
1493 "is disabled in this copy of GDB"),
1494 bfd_get_filename (abfd));
1495 #else
1496 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1497 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1498 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1499 bfd_size_type uncompressed_size;
1500 gdb_byte *uncompressed_buffer;
1501 z_stream strm;
1502 int rc;
1503 int header_size = 12;
1504
1505 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1506 || bfd_bread (compressed_buffer,
1507 compressed_size, abfd) != compressed_size)
1508 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1509 bfd_get_filename (abfd));
1510
1511 /* Read the zlib header. In this case, it should be "ZLIB" followed
1512 by the uncompressed section size, 8 bytes in big-endian order. */
1513 if (compressed_size < header_size
1514 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1515 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1516 bfd_get_filename (abfd));
1517 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1518 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1519 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1520 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1521 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1522 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1523 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1524 uncompressed_size += compressed_buffer[11];
1525
1526 /* It is possible the section consists of several compressed
1527 buffers concatenated together, so we uncompress in a loop. */
1528 strm.zalloc = NULL;
1529 strm.zfree = NULL;
1530 strm.opaque = NULL;
1531 strm.avail_in = compressed_size - header_size;
1532 strm.next_in = (Bytef*) compressed_buffer + header_size;
1533 strm.avail_out = uncompressed_size;
1534 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1535 uncompressed_size);
1536 rc = inflateInit (&strm);
1537 while (strm.avail_in > 0)
1538 {
1539 if (rc != Z_OK)
1540 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1541 bfd_get_filename (abfd), rc);
1542 strm.next_out = ((Bytef*) uncompressed_buffer
1543 + (uncompressed_size - strm.avail_out));
1544 rc = inflate (&strm, Z_FINISH);
1545 if (rc != Z_STREAM_END)
1546 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1547 bfd_get_filename (abfd), rc);
1548 rc = inflateReset (&strm);
1549 }
1550 rc = inflateEnd (&strm);
1551 if (rc != Z_OK
1552 || strm.avail_out != 0)
1553 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1554 bfd_get_filename (abfd), rc);
1555
1556 do_cleanups (cleanup);
1557 *outbuf = uncompressed_buffer;
1558 *outsize = uncompressed_size;
1559 #endif
1560 }
1561
1562 /* A helper function that decides whether a section is empty. */
1563
1564 static int
1565 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1566 {
1567 return info->asection == NULL || info->size == 0;
1568 }
1569
1570 /* Read the contents of the section SECTP from object file specified by
1571 OBJFILE, store info about the section into INFO.
1572 If the section is compressed, uncompress it before returning. */
1573
1574 static void
1575 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1576 {
1577 bfd *abfd = objfile->obfd;
1578 asection *sectp = info->asection;
1579 gdb_byte *buf, *retbuf;
1580 unsigned char header[4];
1581
1582 if (info->readin)
1583 return;
1584 info->buffer = NULL;
1585 info->map_addr = NULL;
1586 info->readin = 1;
1587
1588 if (dwarf2_section_empty_p (info))
1589 return;
1590
1591 /* Check if the file has a 4-byte header indicating compression. */
1592 if (info->size > sizeof (header)
1593 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1594 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1595 {
1596 /* Upon decompression, update the buffer and its size. */
1597 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1598 {
1599 zlib_decompress_section (objfile, sectp, &info->buffer,
1600 &info->size);
1601 return;
1602 }
1603 }
1604
1605 #ifdef HAVE_MMAP
1606 if (pagesize == 0)
1607 pagesize = getpagesize ();
1608
1609 /* Only try to mmap sections which are large enough: we don't want to
1610 waste space due to fragmentation. Also, only try mmap for sections
1611 without relocations. */
1612
1613 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1614 {
1615 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1616 MAP_PRIVATE, sectp->filepos,
1617 &info->map_addr, &info->map_len);
1618
1619 if ((caddr_t)info->buffer != MAP_FAILED)
1620 {
1621 #if HAVE_POSIX_MADVISE
1622 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1623 #endif
1624 return;
1625 }
1626 }
1627 #endif
1628
1629 /* If we get here, we are a normal, not-compressed section. */
1630 info->buffer = buf
1631 = obstack_alloc (&objfile->objfile_obstack, info->size);
1632
1633 /* When debugging .o files, we may need to apply relocations; see
1634 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1635 We never compress sections in .o files, so we only need to
1636 try this when the section is not compressed. */
1637 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1638 if (retbuf != NULL)
1639 {
1640 info->buffer = retbuf;
1641 return;
1642 }
1643
1644 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1645 || bfd_bread (buf, info->size, abfd) != info->size)
1646 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1647 bfd_get_filename (abfd));
1648 }
1649
1650 /* A helper function that returns the size of a section in a safe way.
1651 If you are positive that the section has been read before using the
1652 size, then it is safe to refer to the dwarf2_section_info object's
1653 "size" field directly. In other cases, you must call this
1654 function, because for compressed sections the size field is not set
1655 correctly until the section has been read. */
1656
1657 static bfd_size_type
1658 dwarf2_section_size (struct objfile *objfile,
1659 struct dwarf2_section_info *info)
1660 {
1661 if (!info->readin)
1662 dwarf2_read_section (objfile, info);
1663 return info->size;
1664 }
1665
1666 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1667 SECTION_NAME. */
1668
1669 void
1670 dwarf2_get_section_info (struct objfile *objfile,
1671 enum dwarf2_section_enum sect,
1672 asection **sectp, gdb_byte **bufp,
1673 bfd_size_type *sizep)
1674 {
1675 struct dwarf2_per_objfile *data
1676 = objfile_data (objfile, dwarf2_objfile_data_key);
1677 struct dwarf2_section_info *info;
1678
1679 /* We may see an objfile without any DWARF, in which case we just
1680 return nothing. */
1681 if (data == NULL)
1682 {
1683 *sectp = NULL;
1684 *bufp = NULL;
1685 *sizep = 0;
1686 return;
1687 }
1688 switch (sect)
1689 {
1690 case DWARF2_DEBUG_FRAME:
1691 info = &data->frame;
1692 break;
1693 case DWARF2_EH_FRAME:
1694 info = &data->eh_frame;
1695 break;
1696 default:
1697 gdb_assert_not_reached ("unexpected section");
1698 }
1699
1700 dwarf2_read_section (objfile, info);
1701
1702 *sectp = info->asection;
1703 *bufp = info->buffer;
1704 *sizep = info->size;
1705 }
1706
1707 \f
1708 /* DWARF quick_symbols_functions support. */
1709
1710 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1711 unique line tables, so we maintain a separate table of all .debug_line
1712 derived entries to support the sharing.
1713 All the quick functions need is the list of file names. We discard the
1714 line_header when we're done and don't need to record it here. */
1715 struct quick_file_names
1716 {
1717 /* The offset in .debug_line of the line table. We hash on this. */
1718 unsigned int offset;
1719
1720 /* The number of entries in file_names, real_names. */
1721 unsigned int num_file_names;
1722
1723 /* The file names from the line table, after being run through
1724 file_full_name. */
1725 const char **file_names;
1726
1727 /* The file names from the line table after being run through
1728 gdb_realpath. These are computed lazily. */
1729 const char **real_names;
1730 };
1731
1732 /* When using the index (and thus not using psymtabs), each CU has an
1733 object of this type. This is used to hold information needed by
1734 the various "quick" methods. */
1735 struct dwarf2_per_cu_quick_data
1736 {
1737 /* The file table. This can be NULL if there was no file table
1738 or it's currently not read in.
1739 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1740 struct quick_file_names *file_names;
1741
1742 /* The corresponding symbol table. This is NULL if symbols for this
1743 CU have not yet been read. */
1744 struct symtab *symtab;
1745
1746 /* A temporary mark bit used when iterating over all CUs in
1747 expand_symtabs_matching. */
1748 unsigned int mark : 1;
1749
1750 /* True if we've tried to read the file table and found there isn't one.
1751 There will be no point in trying to read it again next time. */
1752 unsigned int no_file_data : 1;
1753 };
1754
1755 /* Hash function for a quick_file_names. */
1756
1757 static hashval_t
1758 hash_file_name_entry (const void *e)
1759 {
1760 const struct quick_file_names *file_data = e;
1761
1762 return file_data->offset;
1763 }
1764
1765 /* Equality function for a quick_file_names. */
1766
1767 static int
1768 eq_file_name_entry (const void *a, const void *b)
1769 {
1770 const struct quick_file_names *ea = a;
1771 const struct quick_file_names *eb = b;
1772
1773 return ea->offset == eb->offset;
1774 }
1775
1776 /* Delete function for a quick_file_names. */
1777
1778 static void
1779 delete_file_name_entry (void *e)
1780 {
1781 struct quick_file_names *file_data = e;
1782 int i;
1783
1784 for (i = 0; i < file_data->num_file_names; ++i)
1785 {
1786 xfree ((void*) file_data->file_names[i]);
1787 if (file_data->real_names)
1788 xfree ((void*) file_data->real_names[i]);
1789 }
1790
1791 /* The space for the struct itself lives on objfile_obstack,
1792 so we don't free it here. */
1793 }
1794
1795 /* Create a quick_file_names hash table. */
1796
1797 static htab_t
1798 create_quick_file_names_table (unsigned int nr_initial_entries)
1799 {
1800 return htab_create_alloc (nr_initial_entries,
1801 hash_file_name_entry, eq_file_name_entry,
1802 delete_file_name_entry, xcalloc, xfree);
1803 }
1804
1805 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1806 have to be created afterwards. You should call age_cached_comp_units after
1807 processing PER_CU->CU. dw2_setup must have been already called. */
1808
1809 static void
1810 load_cu (struct dwarf2_per_cu_data *per_cu)
1811 {
1812 if (per_cu->debug_type_section)
1813 read_signatured_type_at_offset (per_cu->objfile,
1814 per_cu->debug_type_section,
1815 per_cu->offset);
1816 else
1817 load_full_comp_unit (per_cu, per_cu->objfile);
1818
1819 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1820
1821 gdb_assert (per_cu->cu != NULL);
1822 }
1823
1824 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1825 this CU came. */
1826
1827 static void
1828 dw2_do_instantiate_symtab (struct objfile *objfile,
1829 struct dwarf2_per_cu_data *per_cu)
1830 {
1831 struct cleanup *back_to;
1832
1833 back_to = make_cleanup (dwarf2_release_queue, NULL);
1834
1835 queue_comp_unit (per_cu, objfile);
1836
1837 load_cu (per_cu);
1838
1839 process_queue (objfile);
1840
1841 /* Age the cache, releasing compilation units that have not
1842 been used recently. */
1843 age_cached_comp_units ();
1844
1845 do_cleanups (back_to);
1846 }
1847
1848 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1849 the objfile from which this CU came. Returns the resulting symbol
1850 table. */
1851
1852 static struct symtab *
1853 dw2_instantiate_symtab (struct objfile *objfile,
1854 struct dwarf2_per_cu_data *per_cu)
1855 {
1856 if (!per_cu->v.quick->symtab)
1857 {
1858 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1859 increment_reading_symtab ();
1860 dw2_do_instantiate_symtab (objfile, per_cu);
1861 do_cleanups (back_to);
1862 }
1863 return per_cu->v.quick->symtab;
1864 }
1865
1866 /* Return the CU given its index. */
1867
1868 static struct dwarf2_per_cu_data *
1869 dw2_get_cu (int index)
1870 {
1871 if (index >= dwarf2_per_objfile->n_comp_units)
1872 {
1873 index -= dwarf2_per_objfile->n_comp_units;
1874 return dwarf2_per_objfile->type_comp_units[index];
1875 }
1876 return dwarf2_per_objfile->all_comp_units[index];
1877 }
1878
1879 /* A helper function that knows how to read a 64-bit value in a way
1880 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1881 otherwise. */
1882
1883 static int
1884 extract_cu_value (const char *bytes, ULONGEST *result)
1885 {
1886 if (sizeof (ULONGEST) < 8)
1887 {
1888 int i;
1889
1890 /* Ignore the upper 4 bytes if they are all zero. */
1891 for (i = 0; i < 4; ++i)
1892 if (bytes[i + 4] != 0)
1893 return 0;
1894
1895 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1896 }
1897 else
1898 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1899 return 1;
1900 }
1901
1902 /* Read the CU list from the mapped index, and use it to create all
1903 the CU objects for this objfile. Return 0 if something went wrong,
1904 1 if everything went ok. */
1905
1906 static int
1907 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1908 offset_type cu_list_elements)
1909 {
1910 offset_type i;
1911
1912 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1913 dwarf2_per_objfile->all_comp_units
1914 = obstack_alloc (&objfile->objfile_obstack,
1915 dwarf2_per_objfile->n_comp_units
1916 * sizeof (struct dwarf2_per_cu_data *));
1917
1918 for (i = 0; i < cu_list_elements; i += 2)
1919 {
1920 struct dwarf2_per_cu_data *the_cu;
1921 ULONGEST offset, length;
1922
1923 if (!extract_cu_value (cu_list, &offset)
1924 || !extract_cu_value (cu_list + 8, &length))
1925 return 0;
1926 cu_list += 2 * 8;
1927
1928 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1929 struct dwarf2_per_cu_data);
1930 the_cu->offset = offset;
1931 the_cu->length = length;
1932 the_cu->objfile = objfile;
1933 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1934 struct dwarf2_per_cu_quick_data);
1935 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1936 }
1937
1938 return 1;
1939 }
1940
1941 /* Create the signatured type hash table from the index. */
1942
1943 static int
1944 create_signatured_type_table_from_index (struct objfile *objfile,
1945 struct dwarf2_section_info *section,
1946 const gdb_byte *bytes,
1947 offset_type elements)
1948 {
1949 offset_type i;
1950 htab_t sig_types_hash;
1951
1952 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1953 dwarf2_per_objfile->type_comp_units
1954 = obstack_alloc (&objfile->objfile_obstack,
1955 dwarf2_per_objfile->n_type_comp_units
1956 * sizeof (struct dwarf2_per_cu_data *));
1957
1958 sig_types_hash = allocate_signatured_type_table (objfile);
1959
1960 for (i = 0; i < elements; i += 3)
1961 {
1962 struct signatured_type *type_sig;
1963 ULONGEST offset, type_offset, signature;
1964 void **slot;
1965
1966 if (!extract_cu_value (bytes, &offset)
1967 || !extract_cu_value (bytes + 8, &type_offset))
1968 return 0;
1969 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1970 bytes += 3 * 8;
1971
1972 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1973 struct signatured_type);
1974 type_sig->signature = signature;
1975 type_sig->type_offset = type_offset;
1976 type_sig->per_cu.debug_type_section = section;
1977 type_sig->per_cu.offset = offset;
1978 type_sig->per_cu.objfile = objfile;
1979 type_sig->per_cu.v.quick
1980 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1981 struct dwarf2_per_cu_quick_data);
1982
1983 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1984 *slot = type_sig;
1985
1986 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1987 }
1988
1989 dwarf2_per_objfile->signatured_types = sig_types_hash;
1990
1991 return 1;
1992 }
1993
1994 /* Read the address map data from the mapped index, and use it to
1995 populate the objfile's psymtabs_addrmap. */
1996
1997 static void
1998 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1999 {
2000 const gdb_byte *iter, *end;
2001 struct obstack temp_obstack;
2002 struct addrmap *mutable_map;
2003 struct cleanup *cleanup;
2004 CORE_ADDR baseaddr;
2005
2006 obstack_init (&temp_obstack);
2007 cleanup = make_cleanup_obstack_free (&temp_obstack);
2008 mutable_map = addrmap_create_mutable (&temp_obstack);
2009
2010 iter = index->address_table;
2011 end = iter + index->address_table_size;
2012
2013 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2014
2015 while (iter < end)
2016 {
2017 ULONGEST hi, lo, cu_index;
2018 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2019 iter += 8;
2020 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2021 iter += 8;
2022 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2023 iter += 4;
2024
2025 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2026 dw2_get_cu (cu_index));
2027 }
2028
2029 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2030 &objfile->objfile_obstack);
2031 do_cleanups (cleanup);
2032 }
2033
2034 /* The hash function for strings in the mapped index. This is the same as
2035 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2036 implementation. This is necessary because the hash function is tied to the
2037 format of the mapped index file. The hash values do not have to match with
2038 SYMBOL_HASH_NEXT.
2039
2040 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2041
2042 static hashval_t
2043 mapped_index_string_hash (int index_version, const void *p)
2044 {
2045 const unsigned char *str = (const unsigned char *) p;
2046 hashval_t r = 0;
2047 unsigned char c;
2048
2049 while ((c = *str++) != 0)
2050 {
2051 if (index_version >= 5)
2052 c = tolower (c);
2053 r = r * 67 + c - 113;
2054 }
2055
2056 return r;
2057 }
2058
2059 /* Find a slot in the mapped index INDEX for the object named NAME.
2060 If NAME is found, set *VEC_OUT to point to the CU vector in the
2061 constant pool and return 1. If NAME cannot be found, return 0. */
2062
2063 static int
2064 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2065 offset_type **vec_out)
2066 {
2067 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2068 offset_type hash;
2069 offset_type slot, step;
2070 int (*cmp) (const char *, const char *);
2071
2072 if (current_language->la_language == language_cplus
2073 || current_language->la_language == language_java
2074 || current_language->la_language == language_fortran)
2075 {
2076 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2077 not contain any. */
2078 const char *paren = strchr (name, '(');
2079
2080 if (paren)
2081 {
2082 char *dup;
2083
2084 dup = xmalloc (paren - name + 1);
2085 memcpy (dup, name, paren - name);
2086 dup[paren - name] = 0;
2087
2088 make_cleanup (xfree, dup);
2089 name = dup;
2090 }
2091 }
2092
2093 /* Index version 4 did not support case insensitive searches. But the
2094 indexes for case insensitive languages are built in lowercase, therefore
2095 simulate our NAME being searched is also lowercased. */
2096 hash = mapped_index_string_hash ((index->version == 4
2097 && case_sensitivity == case_sensitive_off
2098 ? 5 : index->version),
2099 name);
2100
2101 slot = hash & (index->symbol_table_slots - 1);
2102 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2103 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2104
2105 for (;;)
2106 {
2107 /* Convert a slot number to an offset into the table. */
2108 offset_type i = 2 * slot;
2109 const char *str;
2110 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2111 {
2112 do_cleanups (back_to);
2113 return 0;
2114 }
2115
2116 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2117 if (!cmp (name, str))
2118 {
2119 *vec_out = (offset_type *) (index->constant_pool
2120 + MAYBE_SWAP (index->symbol_table[i + 1]));
2121 do_cleanups (back_to);
2122 return 1;
2123 }
2124
2125 slot = (slot + step) & (index->symbol_table_slots - 1);
2126 }
2127 }
2128
2129 /* Read the index file. If everything went ok, initialize the "quick"
2130 elements of all the CUs and return 1. Otherwise, return 0. */
2131
2132 static int
2133 dwarf2_read_index (struct objfile *objfile)
2134 {
2135 char *addr;
2136 struct mapped_index *map;
2137 offset_type *metadata;
2138 const gdb_byte *cu_list;
2139 const gdb_byte *types_list = NULL;
2140 offset_type version, cu_list_elements;
2141 offset_type types_list_elements = 0;
2142 int i;
2143
2144 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2145 return 0;
2146
2147 /* Older elfutils strip versions could keep the section in the main
2148 executable while splitting it for the separate debug info file. */
2149 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2150 & SEC_HAS_CONTENTS) == 0)
2151 return 0;
2152
2153 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2154
2155 addr = dwarf2_per_objfile->gdb_index.buffer;
2156 /* Version check. */
2157 version = MAYBE_SWAP (*(offset_type *) addr);
2158 /* Versions earlier than 3 emitted every copy of a psymbol. This
2159 causes the index to behave very poorly for certain requests. Version 3
2160 contained incomplete addrmap. So, it seems better to just ignore such
2161 indices. Index version 4 uses a different hash function than index
2162 version 5 and later. */
2163 if (version < 4)
2164 return 0;
2165 /* Indexes with higher version than the one supported by GDB may be no
2166 longer backward compatible. */
2167 if (version > 5)
2168 return 0;
2169
2170 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2171 map->version = version;
2172 map->total_size = dwarf2_per_objfile->gdb_index.size;
2173
2174 metadata = (offset_type *) (addr + sizeof (offset_type));
2175
2176 i = 0;
2177 cu_list = addr + MAYBE_SWAP (metadata[i]);
2178 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2179 / 8);
2180 ++i;
2181
2182 types_list = addr + MAYBE_SWAP (metadata[i]);
2183 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2184 - MAYBE_SWAP (metadata[i]))
2185 / 8);
2186 ++i;
2187
2188 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2189 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2190 - MAYBE_SWAP (metadata[i]));
2191 ++i;
2192
2193 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2194 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2195 - MAYBE_SWAP (metadata[i]))
2196 / (2 * sizeof (offset_type)));
2197 ++i;
2198
2199 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2200
2201 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2202 return 0;
2203
2204 if (types_list_elements)
2205 {
2206 struct dwarf2_section_info *section;
2207
2208 /* We can only handle a single .debug_types when we have an
2209 index. */
2210 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2211 return 0;
2212
2213 section = VEC_index (dwarf2_section_info_def,
2214 dwarf2_per_objfile->types, 0);
2215
2216 if (!create_signatured_type_table_from_index (objfile, section,
2217 types_list,
2218 types_list_elements))
2219 return 0;
2220 }
2221
2222 create_addrmap_from_index (objfile, map);
2223
2224 dwarf2_per_objfile->index_table = map;
2225 dwarf2_per_objfile->using_index = 1;
2226 dwarf2_per_objfile->quick_file_names_table =
2227 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2228
2229 return 1;
2230 }
2231
2232 /* A helper for the "quick" functions which sets the global
2233 dwarf2_per_objfile according to OBJFILE. */
2234
2235 static void
2236 dw2_setup (struct objfile *objfile)
2237 {
2238 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2239 gdb_assert (dwarf2_per_objfile);
2240 }
2241
2242 /* A helper for the "quick" functions which attempts to read the line
2243 table for THIS_CU. */
2244
2245 static struct quick_file_names *
2246 dw2_get_file_names (struct objfile *objfile,
2247 struct dwarf2_per_cu_data *this_cu)
2248 {
2249 bfd *abfd = objfile->obfd;
2250 struct line_header *lh;
2251 struct attribute *attr;
2252 struct cleanup *cleanups;
2253 struct die_info *comp_unit_die;
2254 struct dwarf2_section_info* sec;
2255 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2256 int has_children, i;
2257 struct dwarf2_cu cu;
2258 unsigned int bytes_read, buffer_size;
2259 struct die_reader_specs reader_specs;
2260 char *name, *comp_dir;
2261 void **slot;
2262 struct quick_file_names *qfn;
2263 unsigned int line_offset;
2264
2265 if (this_cu->v.quick->file_names != NULL)
2266 return this_cu->v.quick->file_names;
2267 /* If we know there is no line data, no point in looking again. */
2268 if (this_cu->v.quick->no_file_data)
2269 return NULL;
2270
2271 init_one_comp_unit (&cu, objfile);
2272 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2273
2274 if (this_cu->debug_type_section)
2275 sec = this_cu->debug_type_section;
2276 else
2277 sec = &dwarf2_per_objfile->info;
2278 dwarf2_read_section (objfile, sec);
2279 buffer_size = sec->size;
2280 buffer = sec->buffer;
2281 info_ptr = buffer + this_cu->offset;
2282 beg_of_comp_unit = info_ptr;
2283
2284 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2285 buffer, buffer_size,
2286 abfd);
2287
2288 /* Complete the cu_header. */
2289 cu.header.offset = beg_of_comp_unit - buffer;
2290 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2291
2292 this_cu->cu = &cu;
2293 cu.per_cu = this_cu;
2294
2295 dwarf2_read_abbrevs (abfd, &cu);
2296 make_cleanup (dwarf2_free_abbrev_table, &cu);
2297
2298 if (this_cu->debug_type_section)
2299 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2300 init_cu_die_reader (&reader_specs, &cu);
2301 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2302 &has_children);
2303
2304 lh = NULL;
2305 slot = NULL;
2306 line_offset = 0;
2307 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2308 if (attr)
2309 {
2310 struct quick_file_names find_entry;
2311
2312 line_offset = DW_UNSND (attr);
2313
2314 /* We may have already read in this line header (TU line header sharing).
2315 If we have we're done. */
2316 find_entry.offset = line_offset;
2317 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2318 &find_entry, INSERT);
2319 if (*slot != NULL)
2320 {
2321 do_cleanups (cleanups);
2322 this_cu->v.quick->file_names = *slot;
2323 return *slot;
2324 }
2325
2326 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2327 }
2328 if (lh == NULL)
2329 {
2330 do_cleanups (cleanups);
2331 this_cu->v.quick->no_file_data = 1;
2332 return NULL;
2333 }
2334
2335 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2336 qfn->offset = line_offset;
2337 gdb_assert (slot != NULL);
2338 *slot = qfn;
2339
2340 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2341
2342 qfn->num_file_names = lh->num_file_names;
2343 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2344 lh->num_file_names * sizeof (char *));
2345 for (i = 0; i < lh->num_file_names; ++i)
2346 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2347 qfn->real_names = NULL;
2348
2349 free_line_header (lh);
2350 do_cleanups (cleanups);
2351
2352 this_cu->v.quick->file_names = qfn;
2353 return qfn;
2354 }
2355
2356 /* A helper for the "quick" functions which computes and caches the
2357 real path for a given file name from the line table. */
2358
2359 static const char *
2360 dw2_get_real_path (struct objfile *objfile,
2361 struct quick_file_names *qfn, int index)
2362 {
2363 if (qfn->real_names == NULL)
2364 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2365 qfn->num_file_names, sizeof (char *));
2366
2367 if (qfn->real_names[index] == NULL)
2368 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2369
2370 return qfn->real_names[index];
2371 }
2372
2373 static struct symtab *
2374 dw2_find_last_source_symtab (struct objfile *objfile)
2375 {
2376 int index;
2377
2378 dw2_setup (objfile);
2379 index = dwarf2_per_objfile->n_comp_units - 1;
2380 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2381 }
2382
2383 /* Traversal function for dw2_forget_cached_source_info. */
2384
2385 static int
2386 dw2_free_cached_file_names (void **slot, void *info)
2387 {
2388 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2389
2390 if (file_data->real_names)
2391 {
2392 int i;
2393
2394 for (i = 0; i < file_data->num_file_names; ++i)
2395 {
2396 xfree ((void*) file_data->real_names[i]);
2397 file_data->real_names[i] = NULL;
2398 }
2399 }
2400
2401 return 1;
2402 }
2403
2404 static void
2405 dw2_forget_cached_source_info (struct objfile *objfile)
2406 {
2407 dw2_setup (objfile);
2408
2409 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2410 dw2_free_cached_file_names, NULL);
2411 }
2412
2413 static int
2414 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2415 const char *full_path, const char *real_path,
2416 struct symtab **result)
2417 {
2418 int i;
2419 int check_basename = lbasename (name) == name;
2420 struct dwarf2_per_cu_data *base_cu = NULL;
2421
2422 dw2_setup (objfile);
2423
2424 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2425 + dwarf2_per_objfile->n_type_comp_units); ++i)
2426 {
2427 int j;
2428 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2429 struct quick_file_names *file_data;
2430
2431 if (per_cu->v.quick->symtab)
2432 continue;
2433
2434 file_data = dw2_get_file_names (objfile, per_cu);
2435 if (file_data == NULL)
2436 continue;
2437
2438 for (j = 0; j < file_data->num_file_names; ++j)
2439 {
2440 const char *this_name = file_data->file_names[j];
2441
2442 if (FILENAME_CMP (name, this_name) == 0)
2443 {
2444 *result = dw2_instantiate_symtab (objfile, per_cu);
2445 return 1;
2446 }
2447
2448 if (check_basename && ! base_cu
2449 && FILENAME_CMP (lbasename (this_name), name) == 0)
2450 base_cu = per_cu;
2451
2452 if (full_path != NULL)
2453 {
2454 const char *this_real_name = dw2_get_real_path (objfile,
2455 file_data, j);
2456
2457 if (this_real_name != NULL
2458 && FILENAME_CMP (full_path, this_real_name) == 0)
2459 {
2460 *result = dw2_instantiate_symtab (objfile, per_cu);
2461 return 1;
2462 }
2463 }
2464
2465 if (real_path != NULL)
2466 {
2467 const char *this_real_name = dw2_get_real_path (objfile,
2468 file_data, j);
2469
2470 if (this_real_name != NULL
2471 && FILENAME_CMP (real_path, this_real_name) == 0)
2472 {
2473 *result = dw2_instantiate_symtab (objfile, per_cu);
2474 return 1;
2475 }
2476 }
2477 }
2478 }
2479
2480 if (base_cu)
2481 {
2482 *result = dw2_instantiate_symtab (objfile, base_cu);
2483 return 1;
2484 }
2485
2486 return 0;
2487 }
2488
2489 static struct symtab *
2490 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2491 const char *name, domain_enum domain)
2492 {
2493 /* We do all the work in the pre_expand_symtabs_matching hook
2494 instead. */
2495 return NULL;
2496 }
2497
2498 /* A helper function that expands all symtabs that hold an object
2499 named NAME. */
2500
2501 static void
2502 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2503 {
2504 dw2_setup (objfile);
2505
2506 /* index_table is NULL if OBJF_READNOW. */
2507 if (dwarf2_per_objfile->index_table)
2508 {
2509 offset_type *vec;
2510
2511 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2512 name, &vec))
2513 {
2514 offset_type i, len = MAYBE_SWAP (*vec);
2515 for (i = 0; i < len; ++i)
2516 {
2517 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2518 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2519
2520 dw2_instantiate_symtab (objfile, per_cu);
2521 }
2522 }
2523 }
2524 }
2525
2526 static void
2527 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2528 enum block_enum block_kind, const char *name,
2529 domain_enum domain)
2530 {
2531 dw2_do_expand_symtabs_matching (objfile, name);
2532 }
2533
2534 static void
2535 dw2_print_stats (struct objfile *objfile)
2536 {
2537 int i, count;
2538
2539 dw2_setup (objfile);
2540 count = 0;
2541 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2542 + dwarf2_per_objfile->n_type_comp_units); ++i)
2543 {
2544 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2545
2546 if (!per_cu->v.quick->symtab)
2547 ++count;
2548 }
2549 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2550 }
2551
2552 static void
2553 dw2_dump (struct objfile *objfile)
2554 {
2555 /* Nothing worth printing. */
2556 }
2557
2558 static void
2559 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2560 struct section_offsets *delta)
2561 {
2562 /* There's nothing to relocate here. */
2563 }
2564
2565 static void
2566 dw2_expand_symtabs_for_function (struct objfile *objfile,
2567 const char *func_name)
2568 {
2569 dw2_do_expand_symtabs_matching (objfile, func_name);
2570 }
2571
2572 static void
2573 dw2_expand_all_symtabs (struct objfile *objfile)
2574 {
2575 int i;
2576
2577 dw2_setup (objfile);
2578
2579 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2580 + dwarf2_per_objfile->n_type_comp_units); ++i)
2581 {
2582 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2583
2584 dw2_instantiate_symtab (objfile, per_cu);
2585 }
2586 }
2587
2588 static void
2589 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2590 const char *filename)
2591 {
2592 int i;
2593
2594 dw2_setup (objfile);
2595
2596 /* We don't need to consider type units here.
2597 This is only called for examining code, e.g. expand_line_sal.
2598 There can be an order of magnitude (or more) more type units
2599 than comp units, and we avoid them if we can. */
2600
2601 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2602 {
2603 int j;
2604 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2605 struct quick_file_names *file_data;
2606
2607 if (per_cu->v.quick->symtab)
2608 continue;
2609
2610 file_data = dw2_get_file_names (objfile, per_cu);
2611 if (file_data == NULL)
2612 continue;
2613
2614 for (j = 0; j < file_data->num_file_names; ++j)
2615 {
2616 const char *this_name = file_data->file_names[j];
2617 if (FILENAME_CMP (this_name, filename) == 0)
2618 {
2619 dw2_instantiate_symtab (objfile, per_cu);
2620 break;
2621 }
2622 }
2623 }
2624 }
2625
2626 static const char *
2627 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2628 {
2629 struct dwarf2_per_cu_data *per_cu;
2630 offset_type *vec;
2631 struct quick_file_names *file_data;
2632
2633 dw2_setup (objfile);
2634
2635 /* index_table is NULL if OBJF_READNOW. */
2636 if (!dwarf2_per_objfile->index_table)
2637 return NULL;
2638
2639 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2640 name, &vec))
2641 return NULL;
2642
2643 /* Note that this just looks at the very first one named NAME -- but
2644 actually we are looking for a function. find_main_filename
2645 should be rewritten so that it doesn't require a custom hook. It
2646 could just use the ordinary symbol tables. */
2647 /* vec[0] is the length, which must always be >0. */
2648 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2649
2650 file_data = dw2_get_file_names (objfile, per_cu);
2651 if (file_data == NULL)
2652 return NULL;
2653
2654 return file_data->file_names[file_data->num_file_names - 1];
2655 }
2656
2657 static void
2658 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2659 struct objfile *objfile, int global,
2660 int (*callback) (struct block *,
2661 struct symbol *, void *),
2662 void *data, symbol_compare_ftype *match,
2663 symbol_compare_ftype *ordered_compare)
2664 {
2665 /* Currently unimplemented; used for Ada. The function can be called if the
2666 current language is Ada for a non-Ada objfile using GNU index. As Ada
2667 does not look for non-Ada symbols this function should just return. */
2668 }
2669
2670 static void
2671 dw2_expand_symtabs_matching (struct objfile *objfile,
2672 int (*file_matcher) (const char *, void *),
2673 int (*name_matcher) (const char *, void *),
2674 enum search_domain kind,
2675 void *data)
2676 {
2677 int i;
2678 offset_type iter;
2679 struct mapped_index *index;
2680
2681 dw2_setup (objfile);
2682
2683 /* index_table is NULL if OBJF_READNOW. */
2684 if (!dwarf2_per_objfile->index_table)
2685 return;
2686 index = dwarf2_per_objfile->index_table;
2687
2688 if (file_matcher != NULL)
2689 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2690 + dwarf2_per_objfile->n_type_comp_units); ++i)
2691 {
2692 int j;
2693 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2694 struct quick_file_names *file_data;
2695
2696 per_cu->v.quick->mark = 0;
2697 if (per_cu->v.quick->symtab)
2698 continue;
2699
2700 file_data = dw2_get_file_names (objfile, per_cu);
2701 if (file_data == NULL)
2702 continue;
2703
2704 for (j = 0; j < file_data->num_file_names; ++j)
2705 {
2706 if (file_matcher (file_data->file_names[j], data))
2707 {
2708 per_cu->v.quick->mark = 1;
2709 break;
2710 }
2711 }
2712 }
2713
2714 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2715 {
2716 offset_type idx = 2 * iter;
2717 const char *name;
2718 offset_type *vec, vec_len, vec_idx;
2719
2720 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2721 continue;
2722
2723 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2724
2725 if (! (*name_matcher) (name, data))
2726 continue;
2727
2728 /* The name was matched, now expand corresponding CUs that were
2729 marked. */
2730 vec = (offset_type *) (index->constant_pool
2731 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2732 vec_len = MAYBE_SWAP (vec[0]);
2733 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2734 {
2735 struct dwarf2_per_cu_data *per_cu;
2736
2737 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2738 if (file_matcher == NULL || per_cu->v.quick->mark)
2739 dw2_instantiate_symtab (objfile, per_cu);
2740 }
2741 }
2742 }
2743
2744 static struct symtab *
2745 dw2_find_pc_sect_symtab (struct objfile *objfile,
2746 struct minimal_symbol *msymbol,
2747 CORE_ADDR pc,
2748 struct obj_section *section,
2749 int warn_if_readin)
2750 {
2751 struct dwarf2_per_cu_data *data;
2752
2753 dw2_setup (objfile);
2754
2755 if (!objfile->psymtabs_addrmap)
2756 return NULL;
2757
2758 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2759 if (!data)
2760 return NULL;
2761
2762 if (warn_if_readin && data->v.quick->symtab)
2763 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2764 paddress (get_objfile_arch (objfile), pc));
2765
2766 return dw2_instantiate_symtab (objfile, data);
2767 }
2768
2769 static void
2770 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2771 void *data)
2772 {
2773 int i;
2774
2775 dw2_setup (objfile);
2776
2777 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2778 + dwarf2_per_objfile->n_type_comp_units); ++i)
2779 {
2780 int j;
2781 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2782 struct quick_file_names *file_data;
2783
2784 if (per_cu->v.quick->symtab)
2785 continue;
2786
2787 file_data = dw2_get_file_names (objfile, per_cu);
2788 if (file_data == NULL)
2789 continue;
2790
2791 for (j = 0; j < file_data->num_file_names; ++j)
2792 {
2793 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2794 j);
2795 (*fun) (file_data->file_names[j], this_real_name, data);
2796 }
2797 }
2798 }
2799
2800 static int
2801 dw2_has_symbols (struct objfile *objfile)
2802 {
2803 return 1;
2804 }
2805
2806 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2807 {
2808 dw2_has_symbols,
2809 dw2_find_last_source_symtab,
2810 dw2_forget_cached_source_info,
2811 dw2_lookup_symtab,
2812 dw2_lookup_symbol,
2813 dw2_pre_expand_symtabs_matching,
2814 dw2_print_stats,
2815 dw2_dump,
2816 dw2_relocate,
2817 dw2_expand_symtabs_for_function,
2818 dw2_expand_all_symtabs,
2819 dw2_expand_symtabs_with_filename,
2820 dw2_find_symbol_file,
2821 dw2_map_matching_symbols,
2822 dw2_expand_symtabs_matching,
2823 dw2_find_pc_sect_symtab,
2824 dw2_map_symbol_filenames
2825 };
2826
2827 /* Initialize for reading DWARF for this objfile. Return 0 if this
2828 file will use psymtabs, or 1 if using the GNU index. */
2829
2830 int
2831 dwarf2_initialize_objfile (struct objfile *objfile)
2832 {
2833 /* If we're about to read full symbols, don't bother with the
2834 indices. In this case we also don't care if some other debug
2835 format is making psymtabs, because they are all about to be
2836 expanded anyway. */
2837 if ((objfile->flags & OBJF_READNOW))
2838 {
2839 int i;
2840
2841 dwarf2_per_objfile->using_index = 1;
2842 create_all_comp_units (objfile);
2843 create_debug_types_hash_table (objfile);
2844 dwarf2_per_objfile->quick_file_names_table =
2845 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2846
2847 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2848 + dwarf2_per_objfile->n_type_comp_units); ++i)
2849 {
2850 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2851
2852 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2853 struct dwarf2_per_cu_quick_data);
2854 }
2855
2856 /* Return 1 so that gdb sees the "quick" functions. However,
2857 these functions will be no-ops because we will have expanded
2858 all symtabs. */
2859 return 1;
2860 }
2861
2862 if (dwarf2_read_index (objfile))
2863 return 1;
2864
2865 return 0;
2866 }
2867
2868 \f
2869
2870 /* Build a partial symbol table. */
2871
2872 void
2873 dwarf2_build_psymtabs (struct objfile *objfile)
2874 {
2875 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2876 {
2877 init_psymbol_list (objfile, 1024);
2878 }
2879
2880 dwarf2_build_psymtabs_hard (objfile);
2881 }
2882
2883 /* Return TRUE if OFFSET is within CU_HEADER. */
2884
2885 static inline int
2886 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2887 {
2888 unsigned int bottom = cu_header->offset;
2889 unsigned int top = (cu_header->offset
2890 + cu_header->length
2891 + cu_header->initial_length_size);
2892
2893 return (offset >= bottom && offset < top);
2894 }
2895
2896 /* Read in the comp unit header information from the debug_info at info_ptr.
2897 NOTE: This leaves members offset, first_die_offset to be filled in
2898 by the caller. */
2899
2900 static gdb_byte *
2901 read_comp_unit_head (struct comp_unit_head *cu_header,
2902 gdb_byte *info_ptr, bfd *abfd)
2903 {
2904 int signed_addr;
2905 unsigned int bytes_read;
2906
2907 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2908 cu_header->initial_length_size = bytes_read;
2909 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2910 info_ptr += bytes_read;
2911 cu_header->version = read_2_bytes (abfd, info_ptr);
2912 info_ptr += 2;
2913 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2914 &bytes_read);
2915 info_ptr += bytes_read;
2916 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2917 info_ptr += 1;
2918 signed_addr = bfd_get_sign_extend_vma (abfd);
2919 if (signed_addr < 0)
2920 internal_error (__FILE__, __LINE__,
2921 _("read_comp_unit_head: dwarf from non elf file"));
2922 cu_header->signed_addr_p = signed_addr;
2923
2924 return info_ptr;
2925 }
2926
2927 static gdb_byte *
2928 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2929 gdb_byte *buffer, unsigned int buffer_size,
2930 bfd *abfd)
2931 {
2932 gdb_byte *beg_of_comp_unit = info_ptr;
2933
2934 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2935
2936 if (header->version != 2 && header->version != 3 && header->version != 4)
2937 error (_("Dwarf Error: wrong version in compilation unit header "
2938 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2939 bfd_get_filename (abfd));
2940
2941 if (header->abbrev_offset
2942 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2943 &dwarf2_per_objfile->abbrev))
2944 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2945 "(offset 0x%lx + 6) [in module %s]"),
2946 (long) header->abbrev_offset,
2947 (long) (beg_of_comp_unit - buffer),
2948 bfd_get_filename (abfd));
2949
2950 if (beg_of_comp_unit + header->length + header->initial_length_size
2951 > buffer + buffer_size)
2952 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2953 "(offset 0x%lx + 0) [in module %s]"),
2954 (long) header->length,
2955 (long) (beg_of_comp_unit - buffer),
2956 bfd_get_filename (abfd));
2957
2958 return info_ptr;
2959 }
2960
2961 /* Read in the types comp unit header information from .debug_types entry at
2962 types_ptr. The result is a pointer to one past the end of the header. */
2963
2964 static gdb_byte *
2965 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2966 struct dwarf2_section_info *section,
2967 ULONGEST *signature,
2968 gdb_byte *types_ptr, bfd *abfd)
2969 {
2970 gdb_byte *initial_types_ptr = types_ptr;
2971
2972 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
2973 cu_header->offset = types_ptr - section->buffer;
2974
2975 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2976
2977 *signature = read_8_bytes (abfd, types_ptr);
2978 types_ptr += 8;
2979 types_ptr += cu_header->offset_size;
2980 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2981
2982 return types_ptr;
2983 }
2984
2985 /* Allocate a new partial symtab for file named NAME and mark this new
2986 partial symtab as being an include of PST. */
2987
2988 static void
2989 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2990 struct objfile *objfile)
2991 {
2992 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2993
2994 subpst->section_offsets = pst->section_offsets;
2995 subpst->textlow = 0;
2996 subpst->texthigh = 0;
2997
2998 subpst->dependencies = (struct partial_symtab **)
2999 obstack_alloc (&objfile->objfile_obstack,
3000 sizeof (struct partial_symtab *));
3001 subpst->dependencies[0] = pst;
3002 subpst->number_of_dependencies = 1;
3003
3004 subpst->globals_offset = 0;
3005 subpst->n_global_syms = 0;
3006 subpst->statics_offset = 0;
3007 subpst->n_static_syms = 0;
3008 subpst->symtab = NULL;
3009 subpst->read_symtab = pst->read_symtab;
3010 subpst->readin = 0;
3011
3012 /* No private part is necessary for include psymtabs. This property
3013 can be used to differentiate between such include psymtabs and
3014 the regular ones. */
3015 subpst->read_symtab_private = NULL;
3016 }
3017
3018 /* Read the Line Number Program data and extract the list of files
3019 included by the source file represented by PST. Build an include
3020 partial symtab for each of these included files. */
3021
3022 static void
3023 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3024 struct die_info *die,
3025 struct partial_symtab *pst)
3026 {
3027 struct objfile *objfile = cu->objfile;
3028 bfd *abfd = objfile->obfd;
3029 struct line_header *lh = NULL;
3030 struct attribute *attr;
3031
3032 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3033 if (attr)
3034 {
3035 unsigned int line_offset = DW_UNSND (attr);
3036
3037 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3038 }
3039 if (lh == NULL)
3040 return; /* No linetable, so no includes. */
3041
3042 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3043 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3044
3045 free_line_header (lh);
3046 }
3047
3048 static hashval_t
3049 hash_type_signature (const void *item)
3050 {
3051 const struct signatured_type *type_sig = item;
3052
3053 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3054 return type_sig->signature;
3055 }
3056
3057 static int
3058 eq_type_signature (const void *item_lhs, const void *item_rhs)
3059 {
3060 const struct signatured_type *lhs = item_lhs;
3061 const struct signatured_type *rhs = item_rhs;
3062
3063 return lhs->signature == rhs->signature;
3064 }
3065
3066 /* Allocate a hash table for signatured types. */
3067
3068 static htab_t
3069 allocate_signatured_type_table (struct objfile *objfile)
3070 {
3071 return htab_create_alloc_ex (41,
3072 hash_type_signature,
3073 eq_type_signature,
3074 NULL,
3075 &objfile->objfile_obstack,
3076 hashtab_obstack_allocate,
3077 dummy_obstack_deallocate);
3078 }
3079
3080 /* A helper function to add a signatured type CU to a list. */
3081
3082 static int
3083 add_signatured_type_cu_to_list (void **slot, void *datum)
3084 {
3085 struct signatured_type *sigt = *slot;
3086 struct dwarf2_per_cu_data ***datap = datum;
3087
3088 **datap = &sigt->per_cu;
3089 ++*datap;
3090
3091 return 1;
3092 }
3093
3094 /* Create the hash table of all entries in the .debug_types section.
3095 The result is zero if there is an error (e.g. missing .debug_types section),
3096 otherwise non-zero. */
3097
3098 static int
3099 create_debug_types_hash_table (struct objfile *objfile)
3100 {
3101 htab_t types_htab = NULL;
3102 struct dwarf2_per_cu_data **iter;
3103 int ix;
3104 struct dwarf2_section_info *section;
3105
3106 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3107 {
3108 dwarf2_per_objfile->signatured_types = NULL;
3109 return 0;
3110 }
3111
3112 for (ix = 0;
3113 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3114 ix, section);
3115 ++ix)
3116 {
3117 gdb_byte *info_ptr, *end_ptr;
3118
3119 dwarf2_read_section (objfile, section);
3120 info_ptr = section->buffer;
3121
3122 if (info_ptr == NULL)
3123 continue;
3124
3125 if (types_htab == NULL)
3126 types_htab = allocate_signatured_type_table (objfile);
3127
3128 if (dwarf2_die_debug)
3129 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3130
3131 end_ptr = info_ptr + section->size;
3132 while (info_ptr < end_ptr)
3133 {
3134 unsigned int offset;
3135 unsigned int offset_size;
3136 unsigned int type_offset;
3137 unsigned int length, initial_length_size;
3138 unsigned short version;
3139 ULONGEST signature;
3140 struct signatured_type *type_sig;
3141 void **slot;
3142 gdb_byte *ptr = info_ptr;
3143
3144 offset = ptr - section->buffer;
3145
3146 /* We need to read the type's signature in order to build the hash
3147 table, but we don't need to read anything else just yet. */
3148
3149 /* Sanity check to ensure entire cu is present. */
3150 length = read_initial_length (objfile->obfd, ptr,
3151 &initial_length_size);
3152 if (ptr + length + initial_length_size > end_ptr)
3153 {
3154 complaint (&symfile_complaints,
3155 _("debug type entry runs off end "
3156 "of `.debug_types' section, ignored"));
3157 break;
3158 }
3159
3160 offset_size = initial_length_size == 4 ? 4 : 8;
3161 ptr += initial_length_size;
3162 version = bfd_get_16 (objfile->obfd, ptr);
3163 ptr += 2;
3164 ptr += offset_size; /* abbrev offset */
3165 ptr += 1; /* address size */
3166 signature = bfd_get_64 (objfile->obfd, ptr);
3167 ptr += 8;
3168 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3169
3170 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3171 memset (type_sig, 0, sizeof (*type_sig));
3172 type_sig->signature = signature;
3173 type_sig->type_offset = type_offset;
3174 type_sig->per_cu.objfile = objfile;
3175 type_sig->per_cu.debug_type_section = section;
3176 type_sig->per_cu.offset = offset;
3177
3178 slot = htab_find_slot (types_htab, type_sig, INSERT);
3179 gdb_assert (slot != NULL);
3180 if (*slot != NULL)
3181 {
3182 const struct signatured_type *dup_sig = *slot;
3183
3184 complaint (&symfile_complaints,
3185 _("debug type entry at offset 0x%x is duplicate to the "
3186 "entry at offset 0x%x, signature 0x%s"),
3187 offset, dup_sig->per_cu.offset,
3188 phex (signature, sizeof (signature)));
3189 gdb_assert (signature == dup_sig->signature);
3190 }
3191 *slot = type_sig;
3192
3193 if (dwarf2_die_debug)
3194 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3195 offset, phex (signature, sizeof (signature)));
3196
3197 info_ptr = info_ptr + initial_length_size + length;
3198 }
3199 }
3200
3201 dwarf2_per_objfile->signatured_types = types_htab;
3202
3203 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3204 dwarf2_per_objfile->type_comp_units
3205 = obstack_alloc (&objfile->objfile_obstack,
3206 dwarf2_per_objfile->n_type_comp_units
3207 * sizeof (struct dwarf2_per_cu_data *));
3208 iter = &dwarf2_per_objfile->type_comp_units[0];
3209 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3210 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3211 == dwarf2_per_objfile->n_type_comp_units);
3212
3213 return 1;
3214 }
3215
3216 /* Lookup a signature based type.
3217 Returns NULL if SIG is not present in the table. */
3218
3219 static struct signatured_type *
3220 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3221 {
3222 struct signatured_type find_entry, *entry;
3223
3224 if (dwarf2_per_objfile->signatured_types == NULL)
3225 {
3226 complaint (&symfile_complaints,
3227 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3228 return 0;
3229 }
3230
3231 find_entry.signature = sig;
3232 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3233 return entry;
3234 }
3235
3236 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3237
3238 static void
3239 init_cu_die_reader (struct die_reader_specs *reader,
3240 struct dwarf2_cu *cu)
3241 {
3242 reader->abfd = cu->objfile->obfd;
3243 reader->cu = cu;
3244 if (cu->per_cu->debug_type_section)
3245 {
3246 gdb_assert (cu->per_cu->debug_type_section->readin);
3247 reader->buffer = cu->per_cu->debug_type_section->buffer;
3248 }
3249 else
3250 {
3251 gdb_assert (dwarf2_per_objfile->info.readin);
3252 reader->buffer = dwarf2_per_objfile->info.buffer;
3253 }
3254 }
3255
3256 /* Find the base address of the compilation unit for range lists and
3257 location lists. It will normally be specified by DW_AT_low_pc.
3258 In DWARF-3 draft 4, the base address could be overridden by
3259 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3260 compilation units with discontinuous ranges. */
3261
3262 static void
3263 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3264 {
3265 struct attribute *attr;
3266
3267 cu->base_known = 0;
3268 cu->base_address = 0;
3269
3270 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3271 if (attr)
3272 {
3273 cu->base_address = DW_ADDR (attr);
3274 cu->base_known = 1;
3275 }
3276 else
3277 {
3278 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3279 if (attr)
3280 {
3281 cu->base_address = DW_ADDR (attr);
3282 cu->base_known = 1;
3283 }
3284 }
3285 }
3286
3287 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3288 to combine the common parts.
3289 Process a compilation unit for a psymtab.
3290 BUFFER is a pointer to the beginning of the dwarf section buffer,
3291 either .debug_info or debug_types.
3292 INFO_PTR is a pointer to the start of the CU.
3293 Returns a pointer to the next CU. */
3294
3295 static gdb_byte *
3296 process_psymtab_comp_unit (struct objfile *objfile,
3297 struct dwarf2_per_cu_data *this_cu,
3298 gdb_byte *buffer, gdb_byte *info_ptr,
3299 unsigned int buffer_size)
3300 {
3301 bfd *abfd = objfile->obfd;
3302 gdb_byte *beg_of_comp_unit = info_ptr;
3303 struct die_info *comp_unit_die;
3304 struct partial_symtab *pst;
3305 CORE_ADDR baseaddr;
3306 struct cleanup *back_to_inner;
3307 struct dwarf2_cu cu;
3308 int has_children, has_pc_info;
3309 struct attribute *attr;
3310 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3311 struct die_reader_specs reader_specs;
3312 const char *filename;
3313
3314 init_one_comp_unit (&cu, objfile);
3315 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3316
3317 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3318 buffer, buffer_size,
3319 abfd);
3320
3321 /* Complete the cu_header. */
3322 cu.header.offset = beg_of_comp_unit - buffer;
3323 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3324
3325 cu.list_in_scope = &file_symbols;
3326
3327 /* If this compilation unit was already read in, free the
3328 cached copy in order to read it in again. This is
3329 necessary because we skipped some symbols when we first
3330 read in the compilation unit (see load_partial_dies).
3331 This problem could be avoided, but the benefit is
3332 unclear. */
3333 if (this_cu->cu != NULL)
3334 free_one_cached_comp_unit (this_cu->cu);
3335
3336 /* Note that this is a pointer to our stack frame, being
3337 added to a global data structure. It will be cleaned up
3338 in free_stack_comp_unit when we finish with this
3339 compilation unit. */
3340 this_cu->cu = &cu;
3341 cu.per_cu = this_cu;
3342
3343 /* Read the abbrevs for this compilation unit into a table. */
3344 dwarf2_read_abbrevs (abfd, &cu);
3345 make_cleanup (dwarf2_free_abbrev_table, &cu);
3346
3347 /* Read the compilation unit die. */
3348 if (this_cu->debug_type_section)
3349 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3350 init_cu_die_reader (&reader_specs, &cu);
3351 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3352 &has_children);
3353
3354 if (this_cu->debug_type_section)
3355 {
3356 /* LENGTH has not been set yet for type units. */
3357 gdb_assert (this_cu->offset == cu.header.offset);
3358 this_cu->length = cu.header.length + cu.header.initial_length_size;
3359 }
3360 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3361 {
3362 info_ptr = (beg_of_comp_unit + cu.header.length
3363 + cu.header.initial_length_size);
3364 do_cleanups (back_to_inner);
3365 return info_ptr;
3366 }
3367
3368 prepare_one_comp_unit (&cu, comp_unit_die);
3369
3370 /* Allocate a new partial symbol table structure. */
3371 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3372 if (attr == NULL || !DW_STRING (attr))
3373 filename = "";
3374 else
3375 filename = DW_STRING (attr);
3376 pst = start_psymtab_common (objfile, objfile->section_offsets,
3377 filename,
3378 /* TEXTLOW and TEXTHIGH are set below. */
3379 0,
3380 objfile->global_psymbols.next,
3381 objfile->static_psymbols.next);
3382
3383 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3384 if (attr != NULL)
3385 pst->dirname = DW_STRING (attr);
3386
3387 pst->read_symtab_private = this_cu;
3388
3389 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3390
3391 /* Store the function that reads in the rest of the symbol table. */
3392 pst->read_symtab = dwarf2_psymtab_to_symtab;
3393
3394 this_cu->v.psymtab = pst;
3395
3396 dwarf2_find_base_address (comp_unit_die, &cu);
3397
3398 /* Possibly set the default values of LOWPC and HIGHPC from
3399 `DW_AT_ranges'. */
3400 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3401 &best_highpc, &cu, pst);
3402 if (has_pc_info == 1 && best_lowpc < best_highpc)
3403 /* Store the contiguous range if it is not empty; it can be empty for
3404 CUs with no code. */
3405 addrmap_set_empty (objfile->psymtabs_addrmap,
3406 best_lowpc + baseaddr,
3407 best_highpc + baseaddr - 1, pst);
3408
3409 /* Check if comp unit has_children.
3410 If so, read the rest of the partial symbols from this comp unit.
3411 If not, there's no more debug_info for this comp unit. */
3412 if (has_children)
3413 {
3414 struct partial_die_info *first_die;
3415 CORE_ADDR lowpc, highpc;
3416
3417 lowpc = ((CORE_ADDR) -1);
3418 highpc = ((CORE_ADDR) 0);
3419
3420 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3421
3422 scan_partial_symbols (first_die, &lowpc, &highpc,
3423 ! has_pc_info, &cu);
3424
3425 /* If we didn't find a lowpc, set it to highpc to avoid
3426 complaints from `maint check'. */
3427 if (lowpc == ((CORE_ADDR) -1))
3428 lowpc = highpc;
3429
3430 /* If the compilation unit didn't have an explicit address range,
3431 then use the information extracted from its child dies. */
3432 if (! has_pc_info)
3433 {
3434 best_lowpc = lowpc;
3435 best_highpc = highpc;
3436 }
3437 }
3438 pst->textlow = best_lowpc + baseaddr;
3439 pst->texthigh = best_highpc + baseaddr;
3440
3441 pst->n_global_syms = objfile->global_psymbols.next -
3442 (objfile->global_psymbols.list + pst->globals_offset);
3443 pst->n_static_syms = objfile->static_psymbols.next -
3444 (objfile->static_psymbols.list + pst->statics_offset);
3445 sort_pst_symbols (pst);
3446
3447 info_ptr = (beg_of_comp_unit + cu.header.length
3448 + cu.header.initial_length_size);
3449
3450 if (this_cu->debug_type_section)
3451 {
3452 /* It's not clear we want to do anything with stmt lists here.
3453 Waiting to see what gcc ultimately does. */
3454 }
3455 else
3456 {
3457 /* Get the list of files included in the current compilation unit,
3458 and build a psymtab for each of them. */
3459 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3460 }
3461
3462 do_cleanups (back_to_inner);
3463
3464 return info_ptr;
3465 }
3466
3467 /* Traversal function for htab_traverse_noresize.
3468 Process one .debug_types comp-unit. */
3469
3470 static int
3471 process_type_comp_unit (void **slot, void *info)
3472 {
3473 struct signatured_type *entry = (struct signatured_type *) *slot;
3474 struct objfile *objfile = (struct objfile *) info;
3475 struct dwarf2_per_cu_data *this_cu;
3476
3477 this_cu = &entry->per_cu;
3478
3479 gdb_assert (this_cu->debug_type_section->readin);
3480 process_psymtab_comp_unit (objfile, this_cu,
3481 this_cu->debug_type_section->buffer,
3482 (this_cu->debug_type_section->buffer
3483 + this_cu->offset),
3484 this_cu->debug_type_section->size);
3485
3486 return 1;
3487 }
3488
3489 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3490 Build partial symbol tables for the .debug_types comp-units. */
3491
3492 static void
3493 build_type_psymtabs (struct objfile *objfile)
3494 {
3495 if (! create_debug_types_hash_table (objfile))
3496 return;
3497
3498 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3499 process_type_comp_unit, objfile);
3500 }
3501
3502 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3503
3504 static void
3505 psymtabs_addrmap_cleanup (void *o)
3506 {
3507 struct objfile *objfile = o;
3508
3509 objfile->psymtabs_addrmap = NULL;
3510 }
3511
3512 /* Build the partial symbol table by doing a quick pass through the
3513 .debug_info and .debug_abbrev sections. */
3514
3515 static void
3516 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3517 {
3518 gdb_byte *info_ptr;
3519 struct cleanup *back_to, *addrmap_cleanup;
3520 struct obstack temp_obstack;
3521
3522 dwarf2_per_objfile->reading_partial_symbols = 1;
3523
3524 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3525 info_ptr = dwarf2_per_objfile->info.buffer;
3526
3527 /* Any cached compilation units will be linked by the per-objfile
3528 read_in_chain. Make sure to free them when we're done. */
3529 back_to = make_cleanup (free_cached_comp_units, NULL);
3530
3531 build_type_psymtabs (objfile);
3532
3533 create_all_comp_units (objfile);
3534
3535 /* Create a temporary address map on a temporary obstack. We later
3536 copy this to the final obstack. */
3537 obstack_init (&temp_obstack);
3538 make_cleanup_obstack_free (&temp_obstack);
3539 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3540 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3541
3542 /* Since the objects we're extracting from .debug_info vary in
3543 length, only the individual functions to extract them (like
3544 read_comp_unit_head and load_partial_die) can really know whether
3545 the buffer is large enough to hold another complete object.
3546
3547 At the moment, they don't actually check that. If .debug_info
3548 holds just one extra byte after the last compilation unit's dies,
3549 then read_comp_unit_head will happily read off the end of the
3550 buffer. read_partial_die is similarly casual. Those functions
3551 should be fixed.
3552
3553 For this loop condition, simply checking whether there's any data
3554 left at all should be sufficient. */
3555
3556 while (info_ptr < (dwarf2_per_objfile->info.buffer
3557 + dwarf2_per_objfile->info.size))
3558 {
3559 struct dwarf2_per_cu_data *this_cu;
3560
3561 this_cu = dwarf2_find_comp_unit (info_ptr
3562 - dwarf2_per_objfile->info.buffer,
3563 objfile);
3564
3565 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3566 dwarf2_per_objfile->info.buffer,
3567 info_ptr,
3568 dwarf2_per_objfile->info.size);
3569 }
3570
3571 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3572 &objfile->objfile_obstack);
3573 discard_cleanups (addrmap_cleanup);
3574
3575 do_cleanups (back_to);
3576 }
3577
3578 /* Load the partial DIEs for a secondary CU into memory. */
3579
3580 static void
3581 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3582 struct objfile *objfile)
3583 {
3584 bfd *abfd = objfile->obfd;
3585 gdb_byte *info_ptr, *beg_of_comp_unit;
3586 struct die_info *comp_unit_die;
3587 struct dwarf2_cu *cu;
3588 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3589 int has_children;
3590 struct die_reader_specs reader_specs;
3591 int read_cu = 0;
3592
3593 gdb_assert (! this_cu->debug_type_section);
3594
3595 gdb_assert (dwarf2_per_objfile->info.readin);
3596 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3597 beg_of_comp_unit = info_ptr;
3598
3599 if (this_cu->cu == NULL)
3600 {
3601 cu = xmalloc (sizeof (*cu));
3602 init_one_comp_unit (cu, objfile);
3603
3604 read_cu = 1;
3605
3606 /* If an error occurs while loading, release our storage. */
3607 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3608
3609 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3610 dwarf2_per_objfile->info.buffer,
3611 dwarf2_per_objfile->info.size,
3612 abfd);
3613
3614 /* Complete the cu_header. */
3615 cu->header.offset = this_cu->offset;
3616 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3617
3618 /* Link this compilation unit into the compilation unit tree. */
3619 this_cu->cu = cu;
3620 cu->per_cu = this_cu;
3621
3622 /* Link this CU into read_in_chain. */
3623 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3624 dwarf2_per_objfile->read_in_chain = this_cu;
3625 }
3626 else
3627 {
3628 cu = this_cu->cu;
3629 info_ptr += cu->header.first_die_offset;
3630 }
3631
3632 /* Read the abbrevs for this compilation unit into a table. */
3633 gdb_assert (cu->dwarf2_abbrevs == NULL);
3634 dwarf2_read_abbrevs (abfd, cu);
3635 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3636
3637 /* Read the compilation unit die. */
3638 init_cu_die_reader (&reader_specs, cu);
3639 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3640 &has_children);
3641
3642 prepare_one_comp_unit (cu, comp_unit_die);
3643
3644 /* Check if comp unit has_children.
3645 If so, read the rest of the partial symbols from this comp unit.
3646 If not, there's no more debug_info for this comp unit. */
3647 if (has_children)
3648 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3649
3650 do_cleanups (free_abbrevs_cleanup);
3651
3652 if (read_cu)
3653 {
3654 /* We've successfully allocated this compilation unit. Let our
3655 caller clean it up when finished with it. */
3656 discard_cleanups (free_cu_cleanup);
3657 }
3658 }
3659
3660 /* Create a list of all compilation units in OBJFILE. We do this only
3661 if an inter-comp-unit reference is found; presumably if there is one,
3662 there will be many, and one will occur early in the .debug_info section.
3663 So there's no point in building this list incrementally. */
3664
3665 static void
3666 create_all_comp_units (struct objfile *objfile)
3667 {
3668 int n_allocated;
3669 int n_comp_units;
3670 struct dwarf2_per_cu_data **all_comp_units;
3671 gdb_byte *info_ptr;
3672
3673 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3674 info_ptr = dwarf2_per_objfile->info.buffer;
3675
3676 n_comp_units = 0;
3677 n_allocated = 10;
3678 all_comp_units = xmalloc (n_allocated
3679 * sizeof (struct dwarf2_per_cu_data *));
3680
3681 while (info_ptr < dwarf2_per_objfile->info.buffer
3682 + dwarf2_per_objfile->info.size)
3683 {
3684 unsigned int length, initial_length_size;
3685 struct dwarf2_per_cu_data *this_cu;
3686 unsigned int offset;
3687
3688 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3689
3690 /* Read just enough information to find out where the next
3691 compilation unit is. */
3692 length = read_initial_length (objfile->obfd, info_ptr,
3693 &initial_length_size);
3694
3695 /* Save the compilation unit for later lookup. */
3696 this_cu = obstack_alloc (&objfile->objfile_obstack,
3697 sizeof (struct dwarf2_per_cu_data));
3698 memset (this_cu, 0, sizeof (*this_cu));
3699 this_cu->offset = offset;
3700 this_cu->length = length + initial_length_size;
3701 this_cu->objfile = objfile;
3702
3703 if (n_comp_units == n_allocated)
3704 {
3705 n_allocated *= 2;
3706 all_comp_units = xrealloc (all_comp_units,
3707 n_allocated
3708 * sizeof (struct dwarf2_per_cu_data *));
3709 }
3710 all_comp_units[n_comp_units++] = this_cu;
3711
3712 info_ptr = info_ptr + this_cu->length;
3713 }
3714
3715 dwarf2_per_objfile->all_comp_units
3716 = obstack_alloc (&objfile->objfile_obstack,
3717 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3718 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3719 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3720 xfree (all_comp_units);
3721 dwarf2_per_objfile->n_comp_units = n_comp_units;
3722 }
3723
3724 /* Process all loaded DIEs for compilation unit CU, starting at
3725 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3726 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3727 DW_AT_ranges). If NEED_PC is set, then this function will set
3728 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3729 and record the covered ranges in the addrmap. */
3730
3731 static void
3732 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3733 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3734 {
3735 struct partial_die_info *pdi;
3736
3737 /* Now, march along the PDI's, descending into ones which have
3738 interesting children but skipping the children of the other ones,
3739 until we reach the end of the compilation unit. */
3740
3741 pdi = first_die;
3742
3743 while (pdi != NULL)
3744 {
3745 fixup_partial_die (pdi, cu);
3746
3747 /* Anonymous namespaces or modules have no name but have interesting
3748 children, so we need to look at them. Ditto for anonymous
3749 enums. */
3750
3751 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3752 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3753 {
3754 switch (pdi->tag)
3755 {
3756 case DW_TAG_subprogram:
3757 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3758 break;
3759 case DW_TAG_constant:
3760 case DW_TAG_variable:
3761 case DW_TAG_typedef:
3762 case DW_TAG_union_type:
3763 if (!pdi->is_declaration)
3764 {
3765 add_partial_symbol (pdi, cu);
3766 }
3767 break;
3768 case DW_TAG_class_type:
3769 case DW_TAG_interface_type:
3770 case DW_TAG_structure_type:
3771 if (!pdi->is_declaration)
3772 {
3773 add_partial_symbol (pdi, cu);
3774 }
3775 break;
3776 case DW_TAG_enumeration_type:
3777 if (!pdi->is_declaration)
3778 add_partial_enumeration (pdi, cu);
3779 break;
3780 case DW_TAG_base_type:
3781 case DW_TAG_subrange_type:
3782 /* File scope base type definitions are added to the partial
3783 symbol table. */
3784 add_partial_symbol (pdi, cu);
3785 break;
3786 case DW_TAG_namespace:
3787 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3788 break;
3789 case DW_TAG_module:
3790 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3791 break;
3792 default:
3793 break;
3794 }
3795 }
3796
3797 /* If the die has a sibling, skip to the sibling. */
3798
3799 pdi = pdi->die_sibling;
3800 }
3801 }
3802
3803 /* Functions used to compute the fully scoped name of a partial DIE.
3804
3805 Normally, this is simple. For C++, the parent DIE's fully scoped
3806 name is concatenated with "::" and the partial DIE's name. For
3807 Java, the same thing occurs except that "." is used instead of "::".
3808 Enumerators are an exception; they use the scope of their parent
3809 enumeration type, i.e. the name of the enumeration type is not
3810 prepended to the enumerator.
3811
3812 There are two complexities. One is DW_AT_specification; in this
3813 case "parent" means the parent of the target of the specification,
3814 instead of the direct parent of the DIE. The other is compilers
3815 which do not emit DW_TAG_namespace; in this case we try to guess
3816 the fully qualified name of structure types from their members'
3817 linkage names. This must be done using the DIE's children rather
3818 than the children of any DW_AT_specification target. We only need
3819 to do this for structures at the top level, i.e. if the target of
3820 any DW_AT_specification (if any; otherwise the DIE itself) does not
3821 have a parent. */
3822
3823 /* Compute the scope prefix associated with PDI's parent, in
3824 compilation unit CU. The result will be allocated on CU's
3825 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3826 field. NULL is returned if no prefix is necessary. */
3827 static char *
3828 partial_die_parent_scope (struct partial_die_info *pdi,
3829 struct dwarf2_cu *cu)
3830 {
3831 char *grandparent_scope;
3832 struct partial_die_info *parent, *real_pdi;
3833
3834 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3835 then this means the parent of the specification DIE. */
3836
3837 real_pdi = pdi;
3838 while (real_pdi->has_specification)
3839 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3840
3841 parent = real_pdi->die_parent;
3842 if (parent == NULL)
3843 return NULL;
3844
3845 if (parent->scope_set)
3846 return parent->scope;
3847
3848 fixup_partial_die (parent, cu);
3849
3850 grandparent_scope = partial_die_parent_scope (parent, cu);
3851
3852 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3853 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3854 Work around this problem here. */
3855 if (cu->language == language_cplus
3856 && parent->tag == DW_TAG_namespace
3857 && strcmp (parent->name, "::") == 0
3858 && grandparent_scope == NULL)
3859 {
3860 parent->scope = NULL;
3861 parent->scope_set = 1;
3862 return NULL;
3863 }
3864
3865 if (parent->tag == DW_TAG_namespace
3866 || parent->tag == DW_TAG_module
3867 || parent->tag == DW_TAG_structure_type
3868 || parent->tag == DW_TAG_class_type
3869 || parent->tag == DW_TAG_interface_type
3870 || parent->tag == DW_TAG_union_type
3871 || parent->tag == DW_TAG_enumeration_type)
3872 {
3873 if (grandparent_scope == NULL)
3874 parent->scope = parent->name;
3875 else
3876 parent->scope = typename_concat (&cu->comp_unit_obstack,
3877 grandparent_scope,
3878 parent->name, 0, cu);
3879 }
3880 else if (parent->tag == DW_TAG_enumerator)
3881 /* Enumerators should not get the name of the enumeration as a prefix. */
3882 parent->scope = grandparent_scope;
3883 else
3884 {
3885 /* FIXME drow/2004-04-01: What should we be doing with
3886 function-local names? For partial symbols, we should probably be
3887 ignoring them. */
3888 complaint (&symfile_complaints,
3889 _("unhandled containing DIE tag %d for DIE at %d"),
3890 parent->tag, pdi->offset);
3891 parent->scope = grandparent_scope;
3892 }
3893
3894 parent->scope_set = 1;
3895 return parent->scope;
3896 }
3897
3898 /* Return the fully scoped name associated with PDI, from compilation unit
3899 CU. The result will be allocated with malloc. */
3900 static char *
3901 partial_die_full_name (struct partial_die_info *pdi,
3902 struct dwarf2_cu *cu)
3903 {
3904 char *parent_scope;
3905
3906 /* If this is a template instantiation, we can not work out the
3907 template arguments from partial DIEs. So, unfortunately, we have
3908 to go through the full DIEs. At least any work we do building
3909 types here will be reused if full symbols are loaded later. */
3910 if (pdi->has_template_arguments)
3911 {
3912 fixup_partial_die (pdi, cu);
3913
3914 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3915 {
3916 struct die_info *die;
3917 struct attribute attr;
3918 struct dwarf2_cu *ref_cu = cu;
3919
3920 attr.name = 0;
3921 attr.form = DW_FORM_ref_addr;
3922 attr.u.addr = pdi->offset;
3923 die = follow_die_ref (NULL, &attr, &ref_cu);
3924
3925 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3926 }
3927 }
3928
3929 parent_scope = partial_die_parent_scope (pdi, cu);
3930 if (parent_scope == NULL)
3931 return NULL;
3932 else
3933 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3934 }
3935
3936 static void
3937 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3938 {
3939 struct objfile *objfile = cu->objfile;
3940 CORE_ADDR addr = 0;
3941 char *actual_name = NULL;
3942 const struct partial_symbol *psym = NULL;
3943 CORE_ADDR baseaddr;
3944 int built_actual_name = 0;
3945
3946 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3947
3948 actual_name = partial_die_full_name (pdi, cu);
3949 if (actual_name)
3950 built_actual_name = 1;
3951
3952 if (actual_name == NULL)
3953 actual_name = pdi->name;
3954
3955 switch (pdi->tag)
3956 {
3957 case DW_TAG_subprogram:
3958 if (pdi->is_external || cu->language == language_ada)
3959 {
3960 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3961 of the global scope. But in Ada, we want to be able to access
3962 nested procedures globally. So all Ada subprograms are stored
3963 in the global scope. */
3964 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3965 mst_text, objfile); */
3966 add_psymbol_to_list (actual_name, strlen (actual_name),
3967 built_actual_name,
3968 VAR_DOMAIN, LOC_BLOCK,
3969 &objfile->global_psymbols,
3970 0, pdi->lowpc + baseaddr,
3971 cu->language, objfile);
3972 }
3973 else
3974 {
3975 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3976 mst_file_text, objfile); */
3977 add_psymbol_to_list (actual_name, strlen (actual_name),
3978 built_actual_name,
3979 VAR_DOMAIN, LOC_BLOCK,
3980 &objfile->static_psymbols,
3981 0, pdi->lowpc + baseaddr,
3982 cu->language, objfile);
3983 }
3984 break;
3985 case DW_TAG_constant:
3986 {
3987 struct psymbol_allocation_list *list;
3988
3989 if (pdi->is_external)
3990 list = &objfile->global_psymbols;
3991 else
3992 list = &objfile->static_psymbols;
3993 add_psymbol_to_list (actual_name, strlen (actual_name),
3994 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3995 list, 0, 0, cu->language, objfile);
3996 }
3997 break;
3998 case DW_TAG_variable:
3999 if (pdi->locdesc)
4000 addr = decode_locdesc (pdi->locdesc, cu);
4001
4002 if (pdi->locdesc
4003 && addr == 0
4004 && !dwarf2_per_objfile->has_section_at_zero)
4005 {
4006 /* A global or static variable may also have been stripped
4007 out by the linker if unused, in which case its address
4008 will be nullified; do not add such variables into partial
4009 symbol table then. */
4010 }
4011 else if (pdi->is_external)
4012 {
4013 /* Global Variable.
4014 Don't enter into the minimal symbol tables as there is
4015 a minimal symbol table entry from the ELF symbols already.
4016 Enter into partial symbol table if it has a location
4017 descriptor or a type.
4018 If the location descriptor is missing, new_symbol will create
4019 a LOC_UNRESOLVED symbol, the address of the variable will then
4020 be determined from the minimal symbol table whenever the variable
4021 is referenced.
4022 The address for the partial symbol table entry is not
4023 used by GDB, but it comes in handy for debugging partial symbol
4024 table building. */
4025
4026 if (pdi->locdesc || pdi->has_type)
4027 add_psymbol_to_list (actual_name, strlen (actual_name),
4028 built_actual_name,
4029 VAR_DOMAIN, LOC_STATIC,
4030 &objfile->global_psymbols,
4031 0, addr + baseaddr,
4032 cu->language, objfile);
4033 }
4034 else
4035 {
4036 /* Static Variable. Skip symbols without location descriptors. */
4037 if (pdi->locdesc == NULL)
4038 {
4039 if (built_actual_name)
4040 xfree (actual_name);
4041 return;
4042 }
4043 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4044 mst_file_data, objfile); */
4045 add_psymbol_to_list (actual_name, strlen (actual_name),
4046 built_actual_name,
4047 VAR_DOMAIN, LOC_STATIC,
4048 &objfile->static_psymbols,
4049 0, addr + baseaddr,
4050 cu->language, objfile);
4051 }
4052 break;
4053 case DW_TAG_typedef:
4054 case DW_TAG_base_type:
4055 case DW_TAG_subrange_type:
4056 add_psymbol_to_list (actual_name, strlen (actual_name),
4057 built_actual_name,
4058 VAR_DOMAIN, LOC_TYPEDEF,
4059 &objfile->static_psymbols,
4060 0, (CORE_ADDR) 0, cu->language, objfile);
4061 break;
4062 case DW_TAG_namespace:
4063 add_psymbol_to_list (actual_name, strlen (actual_name),
4064 built_actual_name,
4065 VAR_DOMAIN, LOC_TYPEDEF,
4066 &objfile->global_psymbols,
4067 0, (CORE_ADDR) 0, cu->language, objfile);
4068 break;
4069 case DW_TAG_class_type:
4070 case DW_TAG_interface_type:
4071 case DW_TAG_structure_type:
4072 case DW_TAG_union_type:
4073 case DW_TAG_enumeration_type:
4074 /* Skip external references. The DWARF standard says in the section
4075 about "Structure, Union, and Class Type Entries": "An incomplete
4076 structure, union or class type is represented by a structure,
4077 union or class entry that does not have a byte size attribute
4078 and that has a DW_AT_declaration attribute." */
4079 if (!pdi->has_byte_size && pdi->is_declaration)
4080 {
4081 if (built_actual_name)
4082 xfree (actual_name);
4083 return;
4084 }
4085
4086 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4087 static vs. global. */
4088 add_psymbol_to_list (actual_name, strlen (actual_name),
4089 built_actual_name,
4090 STRUCT_DOMAIN, LOC_TYPEDEF,
4091 (cu->language == language_cplus
4092 || cu->language == language_java)
4093 ? &objfile->global_psymbols
4094 : &objfile->static_psymbols,
4095 0, (CORE_ADDR) 0, cu->language, objfile);
4096
4097 break;
4098 case DW_TAG_enumerator:
4099 add_psymbol_to_list (actual_name, strlen (actual_name),
4100 built_actual_name,
4101 VAR_DOMAIN, LOC_CONST,
4102 (cu->language == language_cplus
4103 || cu->language == language_java)
4104 ? &objfile->global_psymbols
4105 : &objfile->static_psymbols,
4106 0, (CORE_ADDR) 0, cu->language, objfile);
4107 break;
4108 default:
4109 break;
4110 }
4111
4112 if (built_actual_name)
4113 xfree (actual_name);
4114 }
4115
4116 /* Read a partial die corresponding to a namespace; also, add a symbol
4117 corresponding to that namespace to the symbol table. NAMESPACE is
4118 the name of the enclosing namespace. */
4119
4120 static void
4121 add_partial_namespace (struct partial_die_info *pdi,
4122 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4123 int need_pc, struct dwarf2_cu *cu)
4124 {
4125 /* Add a symbol for the namespace. */
4126
4127 add_partial_symbol (pdi, cu);
4128
4129 /* Now scan partial symbols in that namespace. */
4130
4131 if (pdi->has_children)
4132 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4133 }
4134
4135 /* Read a partial die corresponding to a Fortran module. */
4136
4137 static void
4138 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4139 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4140 {
4141 /* Now scan partial symbols in that module. */
4142
4143 if (pdi->has_children)
4144 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4145 }
4146
4147 /* Read a partial die corresponding to a subprogram and create a partial
4148 symbol for that subprogram. When the CU language allows it, this
4149 routine also defines a partial symbol for each nested subprogram
4150 that this subprogram contains.
4151
4152 DIE my also be a lexical block, in which case we simply search
4153 recursively for suprograms defined inside that lexical block.
4154 Again, this is only performed when the CU language allows this
4155 type of definitions. */
4156
4157 static void
4158 add_partial_subprogram (struct partial_die_info *pdi,
4159 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4160 int need_pc, struct dwarf2_cu *cu)
4161 {
4162 if (pdi->tag == DW_TAG_subprogram)
4163 {
4164 if (pdi->has_pc_info)
4165 {
4166 if (pdi->lowpc < *lowpc)
4167 *lowpc = pdi->lowpc;
4168 if (pdi->highpc > *highpc)
4169 *highpc = pdi->highpc;
4170 if (need_pc)
4171 {
4172 CORE_ADDR baseaddr;
4173 struct objfile *objfile = cu->objfile;
4174
4175 baseaddr = ANOFFSET (objfile->section_offsets,
4176 SECT_OFF_TEXT (objfile));
4177 addrmap_set_empty (objfile->psymtabs_addrmap,
4178 pdi->lowpc + baseaddr,
4179 pdi->highpc - 1 + baseaddr,
4180 cu->per_cu->v.psymtab);
4181 }
4182 if (!pdi->is_declaration)
4183 /* Ignore subprogram DIEs that do not have a name, they are
4184 illegal. Do not emit a complaint at this point, we will
4185 do so when we convert this psymtab into a symtab. */
4186 if (pdi->name)
4187 add_partial_symbol (pdi, cu);
4188 }
4189 }
4190
4191 if (! pdi->has_children)
4192 return;
4193
4194 if (cu->language == language_ada)
4195 {
4196 pdi = pdi->die_child;
4197 while (pdi != NULL)
4198 {
4199 fixup_partial_die (pdi, cu);
4200 if (pdi->tag == DW_TAG_subprogram
4201 || pdi->tag == DW_TAG_lexical_block)
4202 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4203 pdi = pdi->die_sibling;
4204 }
4205 }
4206 }
4207
4208 /* Read a partial die corresponding to an enumeration type. */
4209
4210 static void
4211 add_partial_enumeration (struct partial_die_info *enum_pdi,
4212 struct dwarf2_cu *cu)
4213 {
4214 struct partial_die_info *pdi;
4215
4216 if (enum_pdi->name != NULL)
4217 add_partial_symbol (enum_pdi, cu);
4218
4219 pdi = enum_pdi->die_child;
4220 while (pdi)
4221 {
4222 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4223 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4224 else
4225 add_partial_symbol (pdi, cu);
4226 pdi = pdi->die_sibling;
4227 }
4228 }
4229
4230 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4231 Return the corresponding abbrev, or NULL if the number is zero (indicating
4232 an empty DIE). In either case *BYTES_READ will be set to the length of
4233 the initial number. */
4234
4235 static struct abbrev_info *
4236 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4237 struct dwarf2_cu *cu)
4238 {
4239 bfd *abfd = cu->objfile->obfd;
4240 unsigned int abbrev_number;
4241 struct abbrev_info *abbrev;
4242
4243 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4244
4245 if (abbrev_number == 0)
4246 return NULL;
4247
4248 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4249 if (!abbrev)
4250 {
4251 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4252 abbrev_number, bfd_get_filename (abfd));
4253 }
4254
4255 return abbrev;
4256 }
4257
4258 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4259 Returns a pointer to the end of a series of DIEs, terminated by an empty
4260 DIE. Any children of the skipped DIEs will also be skipped. */
4261
4262 static gdb_byte *
4263 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4264 {
4265 struct abbrev_info *abbrev;
4266 unsigned int bytes_read;
4267
4268 while (1)
4269 {
4270 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4271 if (abbrev == NULL)
4272 return info_ptr + bytes_read;
4273 else
4274 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4275 }
4276 }
4277
4278 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4279 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4280 abbrev corresponding to that skipped uleb128 should be passed in
4281 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4282 children. */
4283
4284 static gdb_byte *
4285 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4286 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4287 {
4288 unsigned int bytes_read;
4289 struct attribute attr;
4290 bfd *abfd = cu->objfile->obfd;
4291 unsigned int form, i;
4292
4293 for (i = 0; i < abbrev->num_attrs; i++)
4294 {
4295 /* The only abbrev we care about is DW_AT_sibling. */
4296 if (abbrev->attrs[i].name == DW_AT_sibling)
4297 {
4298 read_attribute (&attr, &abbrev->attrs[i],
4299 abfd, info_ptr, cu);
4300 if (attr.form == DW_FORM_ref_addr)
4301 complaint (&symfile_complaints,
4302 _("ignoring absolute DW_AT_sibling"));
4303 else
4304 return buffer + dwarf2_get_ref_die_offset (&attr);
4305 }
4306
4307 /* If it isn't DW_AT_sibling, skip this attribute. */
4308 form = abbrev->attrs[i].form;
4309 skip_attribute:
4310 switch (form)
4311 {
4312 case DW_FORM_ref_addr:
4313 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4314 and later it is offset sized. */
4315 if (cu->header.version == 2)
4316 info_ptr += cu->header.addr_size;
4317 else
4318 info_ptr += cu->header.offset_size;
4319 break;
4320 case DW_FORM_addr:
4321 info_ptr += cu->header.addr_size;
4322 break;
4323 case DW_FORM_data1:
4324 case DW_FORM_ref1:
4325 case DW_FORM_flag:
4326 info_ptr += 1;
4327 break;
4328 case DW_FORM_flag_present:
4329 break;
4330 case DW_FORM_data2:
4331 case DW_FORM_ref2:
4332 info_ptr += 2;
4333 break;
4334 case DW_FORM_data4:
4335 case DW_FORM_ref4:
4336 info_ptr += 4;
4337 break;
4338 case DW_FORM_data8:
4339 case DW_FORM_ref8:
4340 case DW_FORM_ref_sig8:
4341 info_ptr += 8;
4342 break;
4343 case DW_FORM_string:
4344 read_direct_string (abfd, info_ptr, &bytes_read);
4345 info_ptr += bytes_read;
4346 break;
4347 case DW_FORM_sec_offset:
4348 case DW_FORM_strp:
4349 info_ptr += cu->header.offset_size;
4350 break;
4351 case DW_FORM_exprloc:
4352 case DW_FORM_block:
4353 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4354 info_ptr += bytes_read;
4355 break;
4356 case DW_FORM_block1:
4357 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4358 break;
4359 case DW_FORM_block2:
4360 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4361 break;
4362 case DW_FORM_block4:
4363 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4364 break;
4365 case DW_FORM_sdata:
4366 case DW_FORM_udata:
4367 case DW_FORM_ref_udata:
4368 info_ptr = skip_leb128 (abfd, info_ptr);
4369 break;
4370 case DW_FORM_indirect:
4371 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4372 info_ptr += bytes_read;
4373 /* We need to continue parsing from here, so just go back to
4374 the top. */
4375 goto skip_attribute;
4376
4377 default:
4378 error (_("Dwarf Error: Cannot handle %s "
4379 "in DWARF reader [in module %s]"),
4380 dwarf_form_name (form),
4381 bfd_get_filename (abfd));
4382 }
4383 }
4384
4385 if (abbrev->has_children)
4386 return skip_children (buffer, info_ptr, cu);
4387 else
4388 return info_ptr;
4389 }
4390
4391 /* Locate ORIG_PDI's sibling.
4392 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4393 in BUFFER. */
4394
4395 static gdb_byte *
4396 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4397 gdb_byte *buffer, gdb_byte *info_ptr,
4398 bfd *abfd, struct dwarf2_cu *cu)
4399 {
4400 /* Do we know the sibling already? */
4401
4402 if (orig_pdi->sibling)
4403 return orig_pdi->sibling;
4404
4405 /* Are there any children to deal with? */
4406
4407 if (!orig_pdi->has_children)
4408 return info_ptr;
4409
4410 /* Skip the children the long way. */
4411
4412 return skip_children (buffer, info_ptr, cu);
4413 }
4414
4415 /* Expand this partial symbol table into a full symbol table. */
4416
4417 static void
4418 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4419 {
4420 if (pst != NULL)
4421 {
4422 if (pst->readin)
4423 {
4424 warning (_("bug: psymtab for %s is already read in."),
4425 pst->filename);
4426 }
4427 else
4428 {
4429 if (info_verbose)
4430 {
4431 printf_filtered (_("Reading in symbols for %s..."),
4432 pst->filename);
4433 gdb_flush (gdb_stdout);
4434 }
4435
4436 /* Restore our global data. */
4437 dwarf2_per_objfile = objfile_data (pst->objfile,
4438 dwarf2_objfile_data_key);
4439
4440 /* If this psymtab is constructed from a debug-only objfile, the
4441 has_section_at_zero flag will not necessarily be correct. We
4442 can get the correct value for this flag by looking at the data
4443 associated with the (presumably stripped) associated objfile. */
4444 if (pst->objfile->separate_debug_objfile_backlink)
4445 {
4446 struct dwarf2_per_objfile *dpo_backlink
4447 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4448 dwarf2_objfile_data_key);
4449
4450 dwarf2_per_objfile->has_section_at_zero
4451 = dpo_backlink->has_section_at_zero;
4452 }
4453
4454 dwarf2_per_objfile->reading_partial_symbols = 0;
4455
4456 psymtab_to_symtab_1 (pst);
4457
4458 /* Finish up the debug error message. */
4459 if (info_verbose)
4460 printf_filtered (_("done.\n"));
4461 }
4462 }
4463 }
4464
4465 /* Add PER_CU to the queue. */
4466
4467 static void
4468 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4469 {
4470 struct dwarf2_queue_item *item;
4471
4472 per_cu->queued = 1;
4473 item = xmalloc (sizeof (*item));
4474 item->per_cu = per_cu;
4475 item->next = NULL;
4476
4477 if (dwarf2_queue == NULL)
4478 dwarf2_queue = item;
4479 else
4480 dwarf2_queue_tail->next = item;
4481
4482 dwarf2_queue_tail = item;
4483 }
4484
4485 /* Process the queue. */
4486
4487 static void
4488 process_queue (struct objfile *objfile)
4489 {
4490 struct dwarf2_queue_item *item, *next_item;
4491
4492 /* The queue starts out with one item, but following a DIE reference
4493 may load a new CU, adding it to the end of the queue. */
4494 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4495 {
4496 if (dwarf2_per_objfile->using_index
4497 ? !item->per_cu->v.quick->symtab
4498 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4499 process_full_comp_unit (item->per_cu);
4500
4501 item->per_cu->queued = 0;
4502 next_item = item->next;
4503 xfree (item);
4504 }
4505
4506 dwarf2_queue_tail = NULL;
4507 }
4508
4509 /* Free all allocated queue entries. This function only releases anything if
4510 an error was thrown; if the queue was processed then it would have been
4511 freed as we went along. */
4512
4513 static void
4514 dwarf2_release_queue (void *dummy)
4515 {
4516 struct dwarf2_queue_item *item, *last;
4517
4518 item = dwarf2_queue;
4519 while (item)
4520 {
4521 /* Anything still marked queued is likely to be in an
4522 inconsistent state, so discard it. */
4523 if (item->per_cu->queued)
4524 {
4525 if (item->per_cu->cu != NULL)
4526 free_one_cached_comp_unit (item->per_cu->cu);
4527 item->per_cu->queued = 0;
4528 }
4529
4530 last = item;
4531 item = item->next;
4532 xfree (last);
4533 }
4534
4535 dwarf2_queue = dwarf2_queue_tail = NULL;
4536 }
4537
4538 /* Read in full symbols for PST, and anything it depends on. */
4539
4540 static void
4541 psymtab_to_symtab_1 (struct partial_symtab *pst)
4542 {
4543 struct dwarf2_per_cu_data *per_cu;
4544 struct cleanup *back_to;
4545 int i;
4546
4547 for (i = 0; i < pst->number_of_dependencies; i++)
4548 if (!pst->dependencies[i]->readin)
4549 {
4550 /* Inform about additional files that need to be read in. */
4551 if (info_verbose)
4552 {
4553 /* FIXME: i18n: Need to make this a single string. */
4554 fputs_filtered (" ", gdb_stdout);
4555 wrap_here ("");
4556 fputs_filtered ("and ", gdb_stdout);
4557 wrap_here ("");
4558 printf_filtered ("%s...", pst->dependencies[i]->filename);
4559 wrap_here (""); /* Flush output. */
4560 gdb_flush (gdb_stdout);
4561 }
4562 psymtab_to_symtab_1 (pst->dependencies[i]);
4563 }
4564
4565 per_cu = pst->read_symtab_private;
4566
4567 if (per_cu == NULL)
4568 {
4569 /* It's an include file, no symbols to read for it.
4570 Everything is in the parent symtab. */
4571 pst->readin = 1;
4572 return;
4573 }
4574
4575 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4576 }
4577
4578 /* Load the DIEs associated with PER_CU into memory. */
4579
4580 static void
4581 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4582 struct objfile *objfile)
4583 {
4584 bfd *abfd = objfile->obfd;
4585 struct dwarf2_cu *cu;
4586 unsigned int offset;
4587 gdb_byte *info_ptr, *beg_of_comp_unit;
4588 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4589 struct attribute *attr;
4590 int read_cu = 0;
4591
4592 gdb_assert (! per_cu->debug_type_section);
4593
4594 /* Set local variables from the partial symbol table info. */
4595 offset = per_cu->offset;
4596
4597 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4598 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4599 beg_of_comp_unit = info_ptr;
4600
4601 if (per_cu->cu == NULL)
4602 {
4603 cu = xmalloc (sizeof (*cu));
4604 init_one_comp_unit (cu, objfile);
4605
4606 read_cu = 1;
4607
4608 /* If an error occurs while loading, release our storage. */
4609 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4610
4611 /* Read in the comp_unit header. */
4612 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4613
4614 /* Complete the cu_header. */
4615 cu->header.offset = offset;
4616 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4617
4618 /* Read the abbrevs for this compilation unit. */
4619 dwarf2_read_abbrevs (abfd, cu);
4620 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4621
4622 /* Link this compilation unit into the compilation unit tree. */
4623 per_cu->cu = cu;
4624 cu->per_cu = per_cu;
4625
4626 /* Link this CU into read_in_chain. */
4627 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4628 dwarf2_per_objfile->read_in_chain = per_cu;
4629 }
4630 else
4631 {
4632 cu = per_cu->cu;
4633 info_ptr += cu->header.first_die_offset;
4634 }
4635
4636 cu->dies = read_comp_unit (info_ptr, cu);
4637
4638 /* We try not to read any attributes in this function, because not
4639 all objfiles needed for references have been loaded yet, and symbol
4640 table processing isn't initialized. But we have to set the CU language,
4641 or we won't be able to build types correctly. */
4642 prepare_one_comp_unit (cu, cu->dies);
4643
4644 /* Similarly, if we do not read the producer, we can not apply
4645 producer-specific interpretation. */
4646 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4647 if (attr)
4648 cu->producer = DW_STRING (attr);
4649
4650 if (read_cu)
4651 {
4652 do_cleanups (free_abbrevs_cleanup);
4653
4654 /* We've successfully allocated this compilation unit. Let our
4655 caller clean it up when finished with it. */
4656 discard_cleanups (free_cu_cleanup);
4657 }
4658 }
4659
4660 /* Add a DIE to the delayed physname list. */
4661
4662 static void
4663 add_to_method_list (struct type *type, int fnfield_index, int index,
4664 const char *name, struct die_info *die,
4665 struct dwarf2_cu *cu)
4666 {
4667 struct delayed_method_info mi;
4668 mi.type = type;
4669 mi.fnfield_index = fnfield_index;
4670 mi.index = index;
4671 mi.name = name;
4672 mi.die = die;
4673 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4674 }
4675
4676 /* A cleanup for freeing the delayed method list. */
4677
4678 static void
4679 free_delayed_list (void *ptr)
4680 {
4681 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4682 if (cu->method_list != NULL)
4683 {
4684 VEC_free (delayed_method_info, cu->method_list);
4685 cu->method_list = NULL;
4686 }
4687 }
4688
4689 /* Compute the physnames of any methods on the CU's method list.
4690
4691 The computation of method physnames is delayed in order to avoid the
4692 (bad) condition that one of the method's formal parameters is of an as yet
4693 incomplete type. */
4694
4695 static void
4696 compute_delayed_physnames (struct dwarf2_cu *cu)
4697 {
4698 int i;
4699 struct delayed_method_info *mi;
4700 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4701 {
4702 const char *physname;
4703 struct fn_fieldlist *fn_flp
4704 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4705 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4706 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4707 }
4708 }
4709
4710 /* Check for GCC >= 4.x. Return minor version (x) of 4.x in such case. If it
4711 is not GCC or it is GCC older than 4.x return -1. If it is GCC 5.x or
4712 higher return INT_MAX. */
4713
4714 static int
4715 producer_is_gcc_ge_4 (struct dwarf2_cu *cu)
4716 {
4717 const char *cs;
4718 int major, minor;
4719
4720 if (cu->producer == NULL)
4721 {
4722 /* For unknown compilers expect their behavior is not compliant. For GCC
4723 this case can also happen for -gdwarf-4 type units supported since
4724 gcc-4.5. */
4725
4726 return -1;
4727 }
4728
4729 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4730
4731 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4732 {
4733 /* For non-GCC compilers expect their behavior is not compliant. */
4734
4735 return -1;
4736 }
4737 cs = &cu->producer[strlen ("GNU ")];
4738 while (*cs && !isdigit (*cs))
4739 cs++;
4740 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4741 {
4742 /* Not recognized as GCC. */
4743
4744 return -1;
4745 }
4746
4747 if (major < 4)
4748 return -1;
4749 if (major > 4)
4750 return INT_MAX;
4751 return minor;
4752 }
4753
4754 /* Generate full symbol information for PST and CU, whose DIEs have
4755 already been loaded into memory. */
4756
4757 static void
4758 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4759 {
4760 struct dwarf2_cu *cu = per_cu->cu;
4761 struct objfile *objfile = per_cu->objfile;
4762 CORE_ADDR lowpc, highpc;
4763 struct symtab *symtab;
4764 struct cleanup *back_to, *delayed_list_cleanup;
4765 CORE_ADDR baseaddr;
4766
4767 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4768
4769 buildsym_init ();
4770 back_to = make_cleanup (really_free_pendings, NULL);
4771 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4772
4773 cu->list_in_scope = &file_symbols;
4774
4775 /* Do line number decoding in read_file_scope () */
4776 process_die (cu->dies, cu);
4777
4778 /* Now that we have processed all the DIEs in the CU, all the types
4779 should be complete, and it should now be safe to compute all of the
4780 physnames. */
4781 compute_delayed_physnames (cu);
4782 do_cleanups (delayed_list_cleanup);
4783
4784 /* Some compilers don't define a DW_AT_high_pc attribute for the
4785 compilation unit. If the DW_AT_high_pc is missing, synthesize
4786 it, by scanning the DIE's below the compilation unit. */
4787 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4788
4789 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4790
4791 if (symtab != NULL)
4792 {
4793 int gcc_4_minor = producer_is_gcc_ge_4 (cu);
4794
4795 /* Set symtab language to language from DW_AT_language. If the
4796 compilation is from a C file generated by language preprocessors, do
4797 not set the language if it was already deduced by start_subfile. */
4798 if (!(cu->language == language_c && symtab->language != language_c))
4799 symtab->language = cu->language;
4800
4801 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4802 produce DW_AT_location with location lists but it can be possibly
4803 invalid without -fvar-tracking.
4804
4805 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4806 needed, it would be wrong due to missing DW_AT_producer there.
4807
4808 Still one can confuse GDB by using non-standard GCC compilation
4809 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4810 */
4811 if (cu->has_loclist && gcc_4_minor >= 0)
4812 symtab->locations_valid = 1;
4813
4814 if (gcc_4_minor >= 5)
4815 symtab->epilogue_unwind_valid = 1;
4816 }
4817
4818 if (dwarf2_per_objfile->using_index)
4819 per_cu->v.quick->symtab = symtab;
4820 else
4821 {
4822 struct partial_symtab *pst = per_cu->v.psymtab;
4823 pst->symtab = symtab;
4824 pst->readin = 1;
4825 }
4826
4827 do_cleanups (back_to);
4828 }
4829
4830 /* Process a die and its children. */
4831
4832 static void
4833 process_die (struct die_info *die, struct dwarf2_cu *cu)
4834 {
4835 switch (die->tag)
4836 {
4837 case DW_TAG_padding:
4838 break;
4839 case DW_TAG_compile_unit:
4840 read_file_scope (die, cu);
4841 break;
4842 case DW_TAG_type_unit:
4843 read_type_unit_scope (die, cu);
4844 break;
4845 case DW_TAG_subprogram:
4846 case DW_TAG_inlined_subroutine:
4847 read_func_scope (die, cu);
4848 break;
4849 case DW_TAG_lexical_block:
4850 case DW_TAG_try_block:
4851 case DW_TAG_catch_block:
4852 read_lexical_block_scope (die, cu);
4853 break;
4854 case DW_TAG_class_type:
4855 case DW_TAG_interface_type:
4856 case DW_TAG_structure_type:
4857 case DW_TAG_union_type:
4858 process_structure_scope (die, cu);
4859 break;
4860 case DW_TAG_enumeration_type:
4861 process_enumeration_scope (die, cu);
4862 break;
4863
4864 /* These dies have a type, but processing them does not create
4865 a symbol or recurse to process the children. Therefore we can
4866 read them on-demand through read_type_die. */
4867 case DW_TAG_subroutine_type:
4868 case DW_TAG_set_type:
4869 case DW_TAG_array_type:
4870 case DW_TAG_pointer_type:
4871 case DW_TAG_ptr_to_member_type:
4872 case DW_TAG_reference_type:
4873 case DW_TAG_string_type:
4874 break;
4875
4876 case DW_TAG_base_type:
4877 case DW_TAG_subrange_type:
4878 case DW_TAG_typedef:
4879 /* Add a typedef symbol for the type definition, if it has a
4880 DW_AT_name. */
4881 new_symbol (die, read_type_die (die, cu), cu);
4882 break;
4883 case DW_TAG_common_block:
4884 read_common_block (die, cu);
4885 break;
4886 case DW_TAG_common_inclusion:
4887 break;
4888 case DW_TAG_namespace:
4889 processing_has_namespace_info = 1;
4890 read_namespace (die, cu);
4891 break;
4892 case DW_TAG_module:
4893 processing_has_namespace_info = 1;
4894 read_module (die, cu);
4895 break;
4896 case DW_TAG_imported_declaration:
4897 case DW_TAG_imported_module:
4898 processing_has_namespace_info = 1;
4899 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4900 || cu->language != language_fortran))
4901 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4902 dwarf_tag_name (die->tag));
4903 read_import_statement (die, cu);
4904 break;
4905 default:
4906 new_symbol (die, NULL, cu);
4907 break;
4908 }
4909 }
4910
4911 /* A helper function for dwarf2_compute_name which determines whether DIE
4912 needs to have the name of the scope prepended to the name listed in the
4913 die. */
4914
4915 static int
4916 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4917 {
4918 struct attribute *attr;
4919
4920 switch (die->tag)
4921 {
4922 case DW_TAG_namespace:
4923 case DW_TAG_typedef:
4924 case DW_TAG_class_type:
4925 case DW_TAG_interface_type:
4926 case DW_TAG_structure_type:
4927 case DW_TAG_union_type:
4928 case DW_TAG_enumeration_type:
4929 case DW_TAG_enumerator:
4930 case DW_TAG_subprogram:
4931 case DW_TAG_member:
4932 return 1;
4933
4934 case DW_TAG_variable:
4935 case DW_TAG_constant:
4936 /* We only need to prefix "globally" visible variables. These include
4937 any variable marked with DW_AT_external or any variable that
4938 lives in a namespace. [Variables in anonymous namespaces
4939 require prefixing, but they are not DW_AT_external.] */
4940
4941 if (dwarf2_attr (die, DW_AT_specification, cu))
4942 {
4943 struct dwarf2_cu *spec_cu = cu;
4944
4945 return die_needs_namespace (die_specification (die, &spec_cu),
4946 spec_cu);
4947 }
4948
4949 attr = dwarf2_attr (die, DW_AT_external, cu);
4950 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4951 && die->parent->tag != DW_TAG_module)
4952 return 0;
4953 /* A variable in a lexical block of some kind does not need a
4954 namespace, even though in C++ such variables may be external
4955 and have a mangled name. */
4956 if (die->parent->tag == DW_TAG_lexical_block
4957 || die->parent->tag == DW_TAG_try_block
4958 || die->parent->tag == DW_TAG_catch_block
4959 || die->parent->tag == DW_TAG_subprogram)
4960 return 0;
4961 return 1;
4962
4963 default:
4964 return 0;
4965 }
4966 }
4967
4968 /* Retrieve the last character from a mem_file. */
4969
4970 static void
4971 do_ui_file_peek_last (void *object, const char *buffer, long length)
4972 {
4973 char *last_char_p = (char *) object;
4974
4975 if (length > 0)
4976 *last_char_p = buffer[length - 1];
4977 }
4978
4979 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4980 compute the physname for the object, which include a method's
4981 formal parameters (C++/Java) and return type (Java).
4982
4983 For Ada, return the DIE's linkage name rather than the fully qualified
4984 name. PHYSNAME is ignored..
4985
4986 The result is allocated on the objfile_obstack and canonicalized. */
4987
4988 static const char *
4989 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4990 int physname)
4991 {
4992 if (name == NULL)
4993 name = dwarf2_name (die, cu);
4994
4995 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4996 compute it by typename_concat inside GDB. */
4997 if (cu->language == language_ada
4998 || (cu->language == language_fortran && physname))
4999 {
5000 /* For Ada unit, we prefer the linkage name over the name, as
5001 the former contains the exported name, which the user expects
5002 to be able to reference. Ideally, we want the user to be able
5003 to reference this entity using either natural or linkage name,
5004 but we haven't started looking at this enhancement yet. */
5005 struct attribute *attr;
5006
5007 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5008 if (attr == NULL)
5009 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5010 if (attr && DW_STRING (attr))
5011 return DW_STRING (attr);
5012 }
5013
5014 /* These are the only languages we know how to qualify names in. */
5015 if (name != NULL
5016 && (cu->language == language_cplus || cu->language == language_java
5017 || cu->language == language_fortran))
5018 {
5019 if (die_needs_namespace (die, cu))
5020 {
5021 long length;
5022 char *prefix;
5023 struct ui_file *buf;
5024
5025 prefix = determine_prefix (die, cu);
5026 buf = mem_fileopen ();
5027 if (*prefix != '\0')
5028 {
5029 char *prefixed_name = typename_concat (NULL, prefix, name,
5030 physname, cu);
5031
5032 fputs_unfiltered (prefixed_name, buf);
5033 xfree (prefixed_name);
5034 }
5035 else
5036 fputs_unfiltered (name, buf);
5037
5038 /* Template parameters may be specified in the DIE's DW_AT_name, or
5039 as children with DW_TAG_template_type_param or
5040 DW_TAG_value_type_param. If the latter, add them to the name
5041 here. If the name already has template parameters, then
5042 skip this step; some versions of GCC emit both, and
5043 it is more efficient to use the pre-computed name.
5044
5045 Something to keep in mind about this process: it is very
5046 unlikely, or in some cases downright impossible, to produce
5047 something that will match the mangled name of a function.
5048 If the definition of the function has the same debug info,
5049 we should be able to match up with it anyway. But fallbacks
5050 using the minimal symbol, for instance to find a method
5051 implemented in a stripped copy of libstdc++, will not work.
5052 If we do not have debug info for the definition, we will have to
5053 match them up some other way.
5054
5055 When we do name matching there is a related problem with function
5056 templates; two instantiated function templates are allowed to
5057 differ only by their return types, which we do not add here. */
5058
5059 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5060 {
5061 struct attribute *attr;
5062 struct die_info *child;
5063 int first = 1;
5064
5065 die->building_fullname = 1;
5066
5067 for (child = die->child; child != NULL; child = child->sibling)
5068 {
5069 struct type *type;
5070 long value;
5071 gdb_byte *bytes;
5072 struct dwarf2_locexpr_baton *baton;
5073 struct value *v;
5074
5075 if (child->tag != DW_TAG_template_type_param
5076 && child->tag != DW_TAG_template_value_param)
5077 continue;
5078
5079 if (first)
5080 {
5081 fputs_unfiltered ("<", buf);
5082 first = 0;
5083 }
5084 else
5085 fputs_unfiltered (", ", buf);
5086
5087 attr = dwarf2_attr (child, DW_AT_type, cu);
5088 if (attr == NULL)
5089 {
5090 complaint (&symfile_complaints,
5091 _("template parameter missing DW_AT_type"));
5092 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5093 continue;
5094 }
5095 type = die_type (child, cu);
5096
5097 if (child->tag == DW_TAG_template_type_param)
5098 {
5099 c_print_type (type, "", buf, -1, 0);
5100 continue;
5101 }
5102
5103 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5104 if (attr == NULL)
5105 {
5106 complaint (&symfile_complaints,
5107 _("template parameter missing "
5108 "DW_AT_const_value"));
5109 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5110 continue;
5111 }
5112
5113 dwarf2_const_value_attr (attr, type, name,
5114 &cu->comp_unit_obstack, cu,
5115 &value, &bytes, &baton);
5116
5117 if (TYPE_NOSIGN (type))
5118 /* GDB prints characters as NUMBER 'CHAR'. If that's
5119 changed, this can use value_print instead. */
5120 c_printchar (value, type, buf);
5121 else
5122 {
5123 struct value_print_options opts;
5124
5125 if (baton != NULL)
5126 v = dwarf2_evaluate_loc_desc (type, NULL,
5127 baton->data,
5128 baton->size,
5129 baton->per_cu);
5130 else if (bytes != NULL)
5131 {
5132 v = allocate_value (type);
5133 memcpy (value_contents_writeable (v), bytes,
5134 TYPE_LENGTH (type));
5135 }
5136 else
5137 v = value_from_longest (type, value);
5138
5139 /* Specify decimal so that we do not depend on
5140 the radix. */
5141 get_formatted_print_options (&opts, 'd');
5142 opts.raw = 1;
5143 value_print (v, buf, &opts);
5144 release_value (v);
5145 value_free (v);
5146 }
5147 }
5148
5149 die->building_fullname = 0;
5150
5151 if (!first)
5152 {
5153 /* Close the argument list, with a space if necessary
5154 (nested templates). */
5155 char last_char = '\0';
5156 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5157 if (last_char == '>')
5158 fputs_unfiltered (" >", buf);
5159 else
5160 fputs_unfiltered (">", buf);
5161 }
5162 }
5163
5164 /* For Java and C++ methods, append formal parameter type
5165 information, if PHYSNAME. */
5166
5167 if (physname && die->tag == DW_TAG_subprogram
5168 && (cu->language == language_cplus
5169 || cu->language == language_java))
5170 {
5171 struct type *type = read_type_die (die, cu);
5172
5173 c_type_print_args (type, buf, 1, cu->language);
5174
5175 if (cu->language == language_java)
5176 {
5177 /* For java, we must append the return type to method
5178 names. */
5179 if (die->tag == DW_TAG_subprogram)
5180 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5181 0, 0);
5182 }
5183 else if (cu->language == language_cplus)
5184 {
5185 /* Assume that an artificial first parameter is
5186 "this", but do not crash if it is not. RealView
5187 marks unnamed (and thus unused) parameters as
5188 artificial; there is no way to differentiate
5189 the two cases. */
5190 if (TYPE_NFIELDS (type) > 0
5191 && TYPE_FIELD_ARTIFICIAL (type, 0)
5192 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5193 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5194 0))))
5195 fputs_unfiltered (" const", buf);
5196 }
5197 }
5198
5199 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5200 &length);
5201 ui_file_delete (buf);
5202
5203 if (cu->language == language_cplus)
5204 {
5205 char *cname
5206 = dwarf2_canonicalize_name (name, cu,
5207 &cu->objfile->objfile_obstack);
5208
5209 if (cname != NULL)
5210 name = cname;
5211 }
5212 }
5213 }
5214
5215 return name;
5216 }
5217
5218 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5219 If scope qualifiers are appropriate they will be added. The result
5220 will be allocated on the objfile_obstack, or NULL if the DIE does
5221 not have a name. NAME may either be from a previous call to
5222 dwarf2_name or NULL.
5223
5224 The output string will be canonicalized (if C++/Java). */
5225
5226 static const char *
5227 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5228 {
5229 return dwarf2_compute_name (name, die, cu, 0);
5230 }
5231
5232 /* Construct a physname for the given DIE in CU. NAME may either be
5233 from a previous call to dwarf2_name or NULL. The result will be
5234 allocated on the objfile_objstack or NULL if the DIE does not have a
5235 name.
5236
5237 The output string will be canonicalized (if C++/Java). */
5238
5239 static const char *
5240 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5241 {
5242 struct attribute *attr;
5243 const char *retval, *mangled = NULL, *canon = NULL;
5244 struct cleanup *back_to;
5245 int need_copy = 1;
5246
5247 /* In this case dwarf2_compute_name is just a shortcut not building anything
5248 on its own. */
5249 if (!die_needs_namespace (die, cu))
5250 return dwarf2_compute_name (name, die, cu, 1);
5251
5252 back_to = make_cleanup (null_cleanup, NULL);
5253
5254 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5255 if (!attr)
5256 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5257
5258 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5259 has computed. */
5260 if (attr && DW_STRING (attr))
5261 {
5262 char *demangled;
5263
5264 mangled = DW_STRING (attr);
5265
5266 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5267 type. It is easier for GDB users to search for such functions as
5268 `name(params)' than `long name(params)'. In such case the minimal
5269 symbol names do not match the full symbol names but for template
5270 functions there is never a need to look up their definition from their
5271 declaration so the only disadvantage remains the minimal symbol
5272 variant `long name(params)' does not have the proper inferior type.
5273 */
5274
5275 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5276 | (cu->language == language_java
5277 ? DMGL_JAVA | DMGL_RET_POSTFIX
5278 : DMGL_RET_DROP)));
5279 if (demangled)
5280 {
5281 make_cleanup (xfree, demangled);
5282 canon = demangled;
5283 }
5284 else
5285 {
5286 canon = mangled;
5287 need_copy = 0;
5288 }
5289 }
5290
5291 if (canon == NULL || check_physname)
5292 {
5293 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5294
5295 if (canon != NULL && strcmp (physname, canon) != 0)
5296 {
5297 /* It may not mean a bug in GDB. The compiler could also
5298 compute DW_AT_linkage_name incorrectly. But in such case
5299 GDB would need to be bug-to-bug compatible. */
5300
5301 complaint (&symfile_complaints,
5302 _("Computed physname <%s> does not match demangled <%s> "
5303 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5304 physname, canon, mangled, die->offset, cu->objfile->name);
5305
5306 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5307 is available here - over computed PHYSNAME. It is safer
5308 against both buggy GDB and buggy compilers. */
5309
5310 retval = canon;
5311 }
5312 else
5313 {
5314 retval = physname;
5315 need_copy = 0;
5316 }
5317 }
5318 else
5319 retval = canon;
5320
5321 if (need_copy)
5322 retval = obsavestring (retval, strlen (retval),
5323 &cu->objfile->objfile_obstack);
5324
5325 do_cleanups (back_to);
5326 return retval;
5327 }
5328
5329 /* Read the import statement specified by the given die and record it. */
5330
5331 static void
5332 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5333 {
5334 struct attribute *import_attr;
5335 struct die_info *imported_die, *child_die;
5336 struct dwarf2_cu *imported_cu;
5337 const char *imported_name;
5338 const char *imported_name_prefix;
5339 const char *canonical_name;
5340 const char *import_alias;
5341 const char *imported_declaration = NULL;
5342 const char *import_prefix;
5343 VEC (const_char_ptr) *excludes = NULL;
5344 struct cleanup *cleanups;
5345
5346 char *temp;
5347
5348 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5349 if (import_attr == NULL)
5350 {
5351 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5352 dwarf_tag_name (die->tag));
5353 return;
5354 }
5355
5356 imported_cu = cu;
5357 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5358 imported_name = dwarf2_name (imported_die, imported_cu);
5359 if (imported_name == NULL)
5360 {
5361 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5362
5363 The import in the following code:
5364 namespace A
5365 {
5366 typedef int B;
5367 }
5368
5369 int main ()
5370 {
5371 using A::B;
5372 B b;
5373 return b;
5374 }
5375
5376 ...
5377 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5378 <52> DW_AT_decl_file : 1
5379 <53> DW_AT_decl_line : 6
5380 <54> DW_AT_import : <0x75>
5381 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5382 <59> DW_AT_name : B
5383 <5b> DW_AT_decl_file : 1
5384 <5c> DW_AT_decl_line : 2
5385 <5d> DW_AT_type : <0x6e>
5386 ...
5387 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5388 <76> DW_AT_byte_size : 4
5389 <77> DW_AT_encoding : 5 (signed)
5390
5391 imports the wrong die ( 0x75 instead of 0x58 ).
5392 This case will be ignored until the gcc bug is fixed. */
5393 return;
5394 }
5395
5396 /* Figure out the local name after import. */
5397 import_alias = dwarf2_name (die, cu);
5398
5399 /* Figure out where the statement is being imported to. */
5400 import_prefix = determine_prefix (die, cu);
5401
5402 /* Figure out what the scope of the imported die is and prepend it
5403 to the name of the imported die. */
5404 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5405
5406 if (imported_die->tag != DW_TAG_namespace
5407 && imported_die->tag != DW_TAG_module)
5408 {
5409 imported_declaration = imported_name;
5410 canonical_name = imported_name_prefix;
5411 }
5412 else if (strlen (imported_name_prefix) > 0)
5413 {
5414 temp = alloca (strlen (imported_name_prefix)
5415 + 2 + strlen (imported_name) + 1);
5416 strcpy (temp, imported_name_prefix);
5417 strcat (temp, "::");
5418 strcat (temp, imported_name);
5419 canonical_name = temp;
5420 }
5421 else
5422 canonical_name = imported_name;
5423
5424 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5425
5426 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5427 for (child_die = die->child; child_die && child_die->tag;
5428 child_die = sibling_die (child_die))
5429 {
5430 /* DWARF-4: A Fortran use statement with a “rename list” may be
5431 represented by an imported module entry with an import attribute
5432 referring to the module and owned entries corresponding to those
5433 entities that are renamed as part of being imported. */
5434
5435 if (child_die->tag != DW_TAG_imported_declaration)
5436 {
5437 complaint (&symfile_complaints,
5438 _("child DW_TAG_imported_declaration expected "
5439 "- DIE at 0x%x [in module %s]"),
5440 child_die->offset, cu->objfile->name);
5441 continue;
5442 }
5443
5444 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5445 if (import_attr == NULL)
5446 {
5447 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5448 dwarf_tag_name (child_die->tag));
5449 continue;
5450 }
5451
5452 imported_cu = cu;
5453 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5454 &imported_cu);
5455 imported_name = dwarf2_name (imported_die, imported_cu);
5456 if (imported_name == NULL)
5457 {
5458 complaint (&symfile_complaints,
5459 _("child DW_TAG_imported_declaration has unknown "
5460 "imported name - DIE at 0x%x [in module %s]"),
5461 child_die->offset, cu->objfile->name);
5462 continue;
5463 }
5464
5465 VEC_safe_push (const_char_ptr, excludes, imported_name);
5466
5467 process_die (child_die, cu);
5468 }
5469
5470 cp_add_using_directive (import_prefix,
5471 canonical_name,
5472 import_alias,
5473 imported_declaration,
5474 excludes,
5475 &cu->objfile->objfile_obstack);
5476
5477 do_cleanups (cleanups);
5478 }
5479
5480 static void
5481 initialize_cu_func_list (struct dwarf2_cu *cu)
5482 {
5483 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5484 }
5485
5486 /* Cleanup function for read_file_scope. */
5487
5488 static void
5489 free_cu_line_header (void *arg)
5490 {
5491 struct dwarf2_cu *cu = arg;
5492
5493 free_line_header (cu->line_header);
5494 cu->line_header = NULL;
5495 }
5496
5497 static void
5498 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5499 char **name, char **comp_dir)
5500 {
5501 struct attribute *attr;
5502
5503 *name = NULL;
5504 *comp_dir = NULL;
5505
5506 /* Find the filename. Do not use dwarf2_name here, since the filename
5507 is not a source language identifier. */
5508 attr = dwarf2_attr (die, DW_AT_name, cu);
5509 if (attr)
5510 {
5511 *name = DW_STRING (attr);
5512 }
5513
5514 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5515 if (attr)
5516 *comp_dir = DW_STRING (attr);
5517 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5518 {
5519 *comp_dir = ldirname (*name);
5520 if (*comp_dir != NULL)
5521 make_cleanup (xfree, *comp_dir);
5522 }
5523 if (*comp_dir != NULL)
5524 {
5525 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5526 directory, get rid of it. */
5527 char *cp = strchr (*comp_dir, ':');
5528
5529 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5530 *comp_dir = cp + 1;
5531 }
5532
5533 if (*name == NULL)
5534 *name = "<unknown>";
5535 }
5536
5537 /* Handle DW_AT_stmt_list for a compilation unit. */
5538
5539 static void
5540 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5541 const char *comp_dir)
5542 {
5543 struct attribute *attr;
5544 struct objfile *objfile = cu->objfile;
5545 bfd *abfd = objfile->obfd;
5546
5547 /* Decode line number information if present. We do this before
5548 processing child DIEs, so that the line header table is available
5549 for DW_AT_decl_file. */
5550 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5551 if (attr)
5552 {
5553 unsigned int line_offset = DW_UNSND (attr);
5554 struct line_header *line_header
5555 = dwarf_decode_line_header (line_offset, abfd, cu);
5556
5557 if (line_header)
5558 {
5559 cu->line_header = line_header;
5560 make_cleanup (free_cu_line_header, cu);
5561 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5562 }
5563 }
5564 }
5565
5566 /* Process DW_TAG_compile_unit. */
5567
5568 static void
5569 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5570 {
5571 struct objfile *objfile = cu->objfile;
5572 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5573 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5574 CORE_ADDR highpc = ((CORE_ADDR) 0);
5575 struct attribute *attr;
5576 char *name = NULL;
5577 char *comp_dir = NULL;
5578 struct die_info *child_die;
5579 bfd *abfd = objfile->obfd;
5580 CORE_ADDR baseaddr;
5581
5582 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5583
5584 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5585
5586 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5587 from finish_block. */
5588 if (lowpc == ((CORE_ADDR) -1))
5589 lowpc = highpc;
5590 lowpc += baseaddr;
5591 highpc += baseaddr;
5592
5593 find_file_and_directory (die, cu, &name, &comp_dir);
5594
5595 attr = dwarf2_attr (die, DW_AT_language, cu);
5596 if (attr)
5597 {
5598 set_cu_language (DW_UNSND (attr), cu);
5599 }
5600
5601 attr = dwarf2_attr (die, DW_AT_producer, cu);
5602 if (attr)
5603 cu->producer = DW_STRING (attr);
5604
5605 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5606 standardised yet. As a workaround for the language detection we fall
5607 back to the DW_AT_producer string. */
5608 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5609 cu->language = language_opencl;
5610
5611 /* We assume that we're processing GCC output. */
5612 processing_gcc_compilation = 2;
5613
5614 processing_has_namespace_info = 0;
5615
5616 start_symtab (name, comp_dir, lowpc);
5617 record_debugformat ("DWARF 2");
5618 record_producer (cu->producer);
5619
5620 initialize_cu_func_list (cu);
5621
5622 handle_DW_AT_stmt_list (die, cu, comp_dir);
5623
5624 /* Process all dies in compilation unit. */
5625 if (die->child != NULL)
5626 {
5627 child_die = die->child;
5628 while (child_die && child_die->tag)
5629 {
5630 process_die (child_die, cu);
5631 child_die = sibling_die (child_die);
5632 }
5633 }
5634
5635 /* Decode macro information, if present. Dwarf 2 macro information
5636 refers to information in the line number info statement program
5637 header, so we can only read it if we've read the header
5638 successfully. */
5639 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5640 if (attr && cu->line_header)
5641 {
5642 unsigned int macro_offset = DW_UNSND (attr);
5643
5644 dwarf_decode_macros (cu->line_header, macro_offset,
5645 comp_dir, abfd, cu);
5646 }
5647 do_cleanups (back_to);
5648 }
5649
5650 /* Process DW_TAG_type_unit.
5651 For TUs we want to skip the first top level sibling if it's not the
5652 actual type being defined by this TU. In this case the first top
5653 level sibling is there to provide context only. */
5654
5655 static void
5656 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5657 {
5658 struct objfile *objfile = cu->objfile;
5659 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5660 CORE_ADDR lowpc;
5661 struct attribute *attr;
5662 char *name = NULL;
5663 char *comp_dir = NULL;
5664 struct die_info *child_die;
5665 bfd *abfd = objfile->obfd;
5666
5667 /* start_symtab needs a low pc, but we don't really have one.
5668 Do what read_file_scope would do in the absence of such info. */
5669 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5670
5671 /* Find the filename. Do not use dwarf2_name here, since the filename
5672 is not a source language identifier. */
5673 attr = dwarf2_attr (die, DW_AT_name, cu);
5674 if (attr)
5675 name = DW_STRING (attr);
5676
5677 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5678 if (attr)
5679 comp_dir = DW_STRING (attr);
5680 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5681 {
5682 comp_dir = ldirname (name);
5683 if (comp_dir != NULL)
5684 make_cleanup (xfree, comp_dir);
5685 }
5686
5687 if (name == NULL)
5688 name = "<unknown>";
5689
5690 attr = dwarf2_attr (die, DW_AT_language, cu);
5691 if (attr)
5692 set_cu_language (DW_UNSND (attr), cu);
5693
5694 /* This isn't technically needed today. It is done for symmetry
5695 with read_file_scope. */
5696 attr = dwarf2_attr (die, DW_AT_producer, cu);
5697 if (attr)
5698 cu->producer = DW_STRING (attr);
5699
5700 /* We assume that we're processing GCC output. */
5701 processing_gcc_compilation = 2;
5702
5703 processing_has_namespace_info = 0;
5704
5705 start_symtab (name, comp_dir, lowpc);
5706 record_debugformat ("DWARF 2");
5707 record_producer (cu->producer);
5708
5709 handle_DW_AT_stmt_list (die, cu, comp_dir);
5710
5711 /* Process the dies in the type unit. */
5712 if (die->child == NULL)
5713 {
5714 dump_die_for_error (die);
5715 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5716 bfd_get_filename (abfd));
5717 }
5718
5719 child_die = die->child;
5720
5721 while (child_die && child_die->tag)
5722 {
5723 process_die (child_die, cu);
5724
5725 child_die = sibling_die (child_die);
5726 }
5727
5728 do_cleanups (back_to);
5729 }
5730
5731 static void
5732 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5733 struct dwarf2_cu *cu)
5734 {
5735 struct function_range *thisfn;
5736
5737 thisfn = (struct function_range *)
5738 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5739 thisfn->name = name;
5740 thisfn->lowpc = lowpc;
5741 thisfn->highpc = highpc;
5742 thisfn->seen_line = 0;
5743 thisfn->next = NULL;
5744
5745 if (cu->last_fn == NULL)
5746 cu->first_fn = thisfn;
5747 else
5748 cu->last_fn->next = thisfn;
5749
5750 cu->last_fn = thisfn;
5751 }
5752
5753 /* qsort helper for inherit_abstract_dies. */
5754
5755 static int
5756 unsigned_int_compar (const void *ap, const void *bp)
5757 {
5758 unsigned int a = *(unsigned int *) ap;
5759 unsigned int b = *(unsigned int *) bp;
5760
5761 return (a > b) - (b > a);
5762 }
5763
5764 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5765 Inherit only the children of the DW_AT_abstract_origin DIE not being
5766 already referenced by DW_AT_abstract_origin from the children of the
5767 current DIE. */
5768
5769 static void
5770 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5771 {
5772 struct die_info *child_die;
5773 unsigned die_children_count;
5774 /* CU offsets which were referenced by children of the current DIE. */
5775 unsigned *offsets;
5776 unsigned *offsets_end, *offsetp;
5777 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5778 struct die_info *origin_die;
5779 /* Iterator of the ORIGIN_DIE children. */
5780 struct die_info *origin_child_die;
5781 struct cleanup *cleanups;
5782 struct attribute *attr;
5783 struct dwarf2_cu *origin_cu;
5784 struct pending **origin_previous_list_in_scope;
5785
5786 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5787 if (!attr)
5788 return;
5789
5790 /* Note that following die references may follow to a die in a
5791 different cu. */
5792
5793 origin_cu = cu;
5794 origin_die = follow_die_ref (die, attr, &origin_cu);
5795
5796 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5797 symbols in. */
5798 origin_previous_list_in_scope = origin_cu->list_in_scope;
5799 origin_cu->list_in_scope = cu->list_in_scope;
5800
5801 if (die->tag != origin_die->tag
5802 && !(die->tag == DW_TAG_inlined_subroutine
5803 && origin_die->tag == DW_TAG_subprogram))
5804 complaint (&symfile_complaints,
5805 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5806 die->offset, origin_die->offset);
5807
5808 child_die = die->child;
5809 die_children_count = 0;
5810 while (child_die && child_die->tag)
5811 {
5812 child_die = sibling_die (child_die);
5813 die_children_count++;
5814 }
5815 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5816 cleanups = make_cleanup (xfree, offsets);
5817
5818 offsets_end = offsets;
5819 child_die = die->child;
5820 while (child_die && child_die->tag)
5821 {
5822 /* For each CHILD_DIE, find the corresponding child of
5823 ORIGIN_DIE. If there is more than one layer of
5824 DW_AT_abstract_origin, follow them all; there shouldn't be,
5825 but GCC versions at least through 4.4 generate this (GCC PR
5826 40573). */
5827 struct die_info *child_origin_die = child_die;
5828 struct dwarf2_cu *child_origin_cu = cu;
5829
5830 while (1)
5831 {
5832 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5833 child_origin_cu);
5834 if (attr == NULL)
5835 break;
5836 child_origin_die = follow_die_ref (child_origin_die, attr,
5837 &child_origin_cu);
5838 }
5839
5840 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5841 counterpart may exist. */
5842 if (child_origin_die != child_die)
5843 {
5844 if (child_die->tag != child_origin_die->tag
5845 && !(child_die->tag == DW_TAG_inlined_subroutine
5846 && child_origin_die->tag == DW_TAG_subprogram))
5847 complaint (&symfile_complaints,
5848 _("Child DIE 0x%x and its abstract origin 0x%x have "
5849 "different tags"), child_die->offset,
5850 child_origin_die->offset);
5851 if (child_origin_die->parent != origin_die)
5852 complaint (&symfile_complaints,
5853 _("Child DIE 0x%x and its abstract origin 0x%x have "
5854 "different parents"), child_die->offset,
5855 child_origin_die->offset);
5856 else
5857 *offsets_end++ = child_origin_die->offset;
5858 }
5859 child_die = sibling_die (child_die);
5860 }
5861 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5862 unsigned_int_compar);
5863 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5864 if (offsetp[-1] == *offsetp)
5865 complaint (&symfile_complaints,
5866 _("Multiple children of DIE 0x%x refer "
5867 "to DIE 0x%x as their abstract origin"),
5868 die->offset, *offsetp);
5869
5870 offsetp = offsets;
5871 origin_child_die = origin_die->child;
5872 while (origin_child_die && origin_child_die->tag)
5873 {
5874 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5875 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5876 offsetp++;
5877 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5878 {
5879 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5880 process_die (origin_child_die, origin_cu);
5881 }
5882 origin_child_die = sibling_die (origin_child_die);
5883 }
5884 origin_cu->list_in_scope = origin_previous_list_in_scope;
5885
5886 do_cleanups (cleanups);
5887 }
5888
5889 static void
5890 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5891 {
5892 struct objfile *objfile = cu->objfile;
5893 struct context_stack *new;
5894 CORE_ADDR lowpc;
5895 CORE_ADDR highpc;
5896 struct die_info *child_die;
5897 struct attribute *attr, *call_line, *call_file;
5898 char *name;
5899 CORE_ADDR baseaddr;
5900 struct block *block;
5901 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5902 VEC (symbolp) *template_args = NULL;
5903 struct template_symbol *templ_func = NULL;
5904
5905 if (inlined_func)
5906 {
5907 /* If we do not have call site information, we can't show the
5908 caller of this inlined function. That's too confusing, so
5909 only use the scope for local variables. */
5910 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5911 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5912 if (call_line == NULL || call_file == NULL)
5913 {
5914 read_lexical_block_scope (die, cu);
5915 return;
5916 }
5917 }
5918
5919 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5920
5921 name = dwarf2_name (die, cu);
5922
5923 /* Ignore functions with missing or empty names. These are actually
5924 illegal according to the DWARF standard. */
5925 if (name == NULL)
5926 {
5927 complaint (&symfile_complaints,
5928 _("missing name for subprogram DIE at %d"), die->offset);
5929 return;
5930 }
5931
5932 /* Ignore functions with missing or invalid low and high pc attributes. */
5933 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5934 {
5935 attr = dwarf2_attr (die, DW_AT_external, cu);
5936 if (!attr || !DW_UNSND (attr))
5937 complaint (&symfile_complaints,
5938 _("cannot get low and high bounds "
5939 "for subprogram DIE at %d"),
5940 die->offset);
5941 return;
5942 }
5943
5944 lowpc += baseaddr;
5945 highpc += baseaddr;
5946
5947 /* Record the function range for dwarf_decode_lines. */
5948 add_to_cu_func_list (name, lowpc, highpc, cu);
5949
5950 /* If we have any template arguments, then we must allocate a
5951 different sort of symbol. */
5952 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5953 {
5954 if (child_die->tag == DW_TAG_template_type_param
5955 || child_die->tag == DW_TAG_template_value_param)
5956 {
5957 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5958 struct template_symbol);
5959 templ_func->base.is_cplus_template_function = 1;
5960 break;
5961 }
5962 }
5963
5964 new = push_context (0, lowpc);
5965 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5966 (struct symbol *) templ_func);
5967
5968 /* If there is a location expression for DW_AT_frame_base, record
5969 it. */
5970 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5971 if (attr)
5972 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5973 expression is being recorded directly in the function's symbol
5974 and not in a separate frame-base object. I guess this hack is
5975 to avoid adding some sort of frame-base adjunct/annex to the
5976 function's symbol :-(. The problem with doing this is that it
5977 results in a function symbol with a location expression that
5978 has nothing to do with the location of the function, ouch! The
5979 relationship should be: a function's symbol has-a frame base; a
5980 frame-base has-a location expression. */
5981 dwarf2_symbol_mark_computed (attr, new->name, cu);
5982
5983 cu->list_in_scope = &local_symbols;
5984
5985 if (die->child != NULL)
5986 {
5987 child_die = die->child;
5988 while (child_die && child_die->tag)
5989 {
5990 if (child_die->tag == DW_TAG_template_type_param
5991 || child_die->tag == DW_TAG_template_value_param)
5992 {
5993 struct symbol *arg = new_symbol (child_die, NULL, cu);
5994
5995 if (arg != NULL)
5996 VEC_safe_push (symbolp, template_args, arg);
5997 }
5998 else
5999 process_die (child_die, cu);
6000 child_die = sibling_die (child_die);
6001 }
6002 }
6003
6004 inherit_abstract_dies (die, cu);
6005
6006 /* If we have a DW_AT_specification, we might need to import using
6007 directives from the context of the specification DIE. See the
6008 comment in determine_prefix. */
6009 if (cu->language == language_cplus
6010 && dwarf2_attr (die, DW_AT_specification, cu))
6011 {
6012 struct dwarf2_cu *spec_cu = cu;
6013 struct die_info *spec_die = die_specification (die, &spec_cu);
6014
6015 while (spec_die)
6016 {
6017 child_die = spec_die->child;
6018 while (child_die && child_die->tag)
6019 {
6020 if (child_die->tag == DW_TAG_imported_module)
6021 process_die (child_die, spec_cu);
6022 child_die = sibling_die (child_die);
6023 }
6024
6025 /* In some cases, GCC generates specification DIEs that
6026 themselves contain DW_AT_specification attributes. */
6027 spec_die = die_specification (spec_die, &spec_cu);
6028 }
6029 }
6030
6031 new = pop_context ();
6032 /* Make a block for the local symbols within. */
6033 block = finish_block (new->name, &local_symbols, new->old_blocks,
6034 lowpc, highpc, objfile);
6035
6036 /* For C++, set the block's scope. */
6037 if (cu->language == language_cplus || cu->language == language_fortran)
6038 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6039 determine_prefix (die, cu),
6040 processing_has_namespace_info);
6041
6042 /* If we have address ranges, record them. */
6043 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6044
6045 /* Attach template arguments to function. */
6046 if (! VEC_empty (symbolp, template_args))
6047 {
6048 gdb_assert (templ_func != NULL);
6049
6050 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6051 templ_func->template_arguments
6052 = obstack_alloc (&objfile->objfile_obstack,
6053 (templ_func->n_template_arguments
6054 * sizeof (struct symbol *)));
6055 memcpy (templ_func->template_arguments,
6056 VEC_address (symbolp, template_args),
6057 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6058 VEC_free (symbolp, template_args);
6059 }
6060
6061 /* In C++, we can have functions nested inside functions (e.g., when
6062 a function declares a class that has methods). This means that
6063 when we finish processing a function scope, we may need to go
6064 back to building a containing block's symbol lists. */
6065 local_symbols = new->locals;
6066 param_symbols = new->params;
6067 using_directives = new->using_directives;
6068
6069 /* If we've finished processing a top-level function, subsequent
6070 symbols go in the file symbol list. */
6071 if (outermost_context_p ())
6072 cu->list_in_scope = &file_symbols;
6073 }
6074
6075 /* Process all the DIES contained within a lexical block scope. Start
6076 a new scope, process the dies, and then close the scope. */
6077
6078 static void
6079 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6080 {
6081 struct objfile *objfile = cu->objfile;
6082 struct context_stack *new;
6083 CORE_ADDR lowpc, highpc;
6084 struct die_info *child_die;
6085 CORE_ADDR baseaddr;
6086
6087 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6088
6089 /* Ignore blocks with missing or invalid low and high pc attributes. */
6090 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6091 as multiple lexical blocks? Handling children in a sane way would
6092 be nasty. Might be easier to properly extend generic blocks to
6093 describe ranges. */
6094 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6095 return;
6096 lowpc += baseaddr;
6097 highpc += baseaddr;
6098
6099 push_context (0, lowpc);
6100 if (die->child != NULL)
6101 {
6102 child_die = die->child;
6103 while (child_die && child_die->tag)
6104 {
6105 process_die (child_die, cu);
6106 child_die = sibling_die (child_die);
6107 }
6108 }
6109 new = pop_context ();
6110
6111 if (local_symbols != NULL || using_directives != NULL)
6112 {
6113 struct block *block
6114 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6115 highpc, objfile);
6116
6117 /* Note that recording ranges after traversing children, as we
6118 do here, means that recording a parent's ranges entails
6119 walking across all its children's ranges as they appear in
6120 the address map, which is quadratic behavior.
6121
6122 It would be nicer to record the parent's ranges before
6123 traversing its children, simply overriding whatever you find
6124 there. But since we don't even decide whether to create a
6125 block until after we've traversed its children, that's hard
6126 to do. */
6127 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6128 }
6129 local_symbols = new->locals;
6130 using_directives = new->using_directives;
6131 }
6132
6133 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6134 Return 1 if the attributes are present and valid, otherwise, return 0.
6135 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6136
6137 static int
6138 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6139 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6140 struct partial_symtab *ranges_pst)
6141 {
6142 struct objfile *objfile = cu->objfile;
6143 struct comp_unit_head *cu_header = &cu->header;
6144 bfd *obfd = objfile->obfd;
6145 unsigned int addr_size = cu_header->addr_size;
6146 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6147 /* Base address selection entry. */
6148 CORE_ADDR base;
6149 int found_base;
6150 unsigned int dummy;
6151 gdb_byte *buffer;
6152 CORE_ADDR marker;
6153 int low_set;
6154 CORE_ADDR low = 0;
6155 CORE_ADDR high = 0;
6156 CORE_ADDR baseaddr;
6157
6158 found_base = cu->base_known;
6159 base = cu->base_address;
6160
6161 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6162 if (offset >= dwarf2_per_objfile->ranges.size)
6163 {
6164 complaint (&symfile_complaints,
6165 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6166 offset);
6167 return 0;
6168 }
6169 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6170
6171 /* Read in the largest possible address. */
6172 marker = read_address (obfd, buffer, cu, &dummy);
6173 if ((marker & mask) == mask)
6174 {
6175 /* If we found the largest possible address, then
6176 read the base address. */
6177 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6178 buffer += 2 * addr_size;
6179 offset += 2 * addr_size;
6180 found_base = 1;
6181 }
6182
6183 low_set = 0;
6184
6185 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6186
6187 while (1)
6188 {
6189 CORE_ADDR range_beginning, range_end;
6190
6191 range_beginning = read_address (obfd, buffer, cu, &dummy);
6192 buffer += addr_size;
6193 range_end = read_address (obfd, buffer, cu, &dummy);
6194 buffer += addr_size;
6195 offset += 2 * addr_size;
6196
6197 /* An end of list marker is a pair of zero addresses. */
6198 if (range_beginning == 0 && range_end == 0)
6199 /* Found the end of list entry. */
6200 break;
6201
6202 /* Each base address selection entry is a pair of 2 values.
6203 The first is the largest possible address, the second is
6204 the base address. Check for a base address here. */
6205 if ((range_beginning & mask) == mask)
6206 {
6207 /* If we found the largest possible address, then
6208 read the base address. */
6209 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6210 found_base = 1;
6211 continue;
6212 }
6213
6214 if (!found_base)
6215 {
6216 /* We have no valid base address for the ranges
6217 data. */
6218 complaint (&symfile_complaints,
6219 _("Invalid .debug_ranges data (no base address)"));
6220 return 0;
6221 }
6222
6223 if (range_beginning > range_end)
6224 {
6225 /* Inverted range entries are invalid. */
6226 complaint (&symfile_complaints,
6227 _("Invalid .debug_ranges data (inverted range)"));
6228 return 0;
6229 }
6230
6231 /* Empty range entries have no effect. */
6232 if (range_beginning == range_end)
6233 continue;
6234
6235 range_beginning += base;
6236 range_end += base;
6237
6238 if (ranges_pst != NULL)
6239 addrmap_set_empty (objfile->psymtabs_addrmap,
6240 range_beginning + baseaddr,
6241 range_end - 1 + baseaddr,
6242 ranges_pst);
6243
6244 /* FIXME: This is recording everything as a low-high
6245 segment of consecutive addresses. We should have a
6246 data structure for discontiguous block ranges
6247 instead. */
6248 if (! low_set)
6249 {
6250 low = range_beginning;
6251 high = range_end;
6252 low_set = 1;
6253 }
6254 else
6255 {
6256 if (range_beginning < low)
6257 low = range_beginning;
6258 if (range_end > high)
6259 high = range_end;
6260 }
6261 }
6262
6263 if (! low_set)
6264 /* If the first entry is an end-of-list marker, the range
6265 describes an empty scope, i.e. no instructions. */
6266 return 0;
6267
6268 if (low_return)
6269 *low_return = low;
6270 if (high_return)
6271 *high_return = high;
6272 return 1;
6273 }
6274
6275 /* Get low and high pc attributes from a die. Return 1 if the attributes
6276 are present and valid, otherwise, return 0. Return -1 if the range is
6277 discontinuous, i.e. derived from DW_AT_ranges information. */
6278 static int
6279 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6280 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6281 struct partial_symtab *pst)
6282 {
6283 struct attribute *attr;
6284 CORE_ADDR low = 0;
6285 CORE_ADDR high = 0;
6286 int ret = 0;
6287
6288 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6289 if (attr)
6290 {
6291 high = DW_ADDR (attr);
6292 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6293 if (attr)
6294 low = DW_ADDR (attr);
6295 else
6296 /* Found high w/o low attribute. */
6297 return 0;
6298
6299 /* Found consecutive range of addresses. */
6300 ret = 1;
6301 }
6302 else
6303 {
6304 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6305 if (attr != NULL)
6306 {
6307 /* Value of the DW_AT_ranges attribute is the offset in the
6308 .debug_ranges section. */
6309 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6310 return 0;
6311 /* Found discontinuous range of addresses. */
6312 ret = -1;
6313 }
6314 }
6315
6316 /* read_partial_die has also the strict LOW < HIGH requirement. */
6317 if (high <= low)
6318 return 0;
6319
6320 /* When using the GNU linker, .gnu.linkonce. sections are used to
6321 eliminate duplicate copies of functions and vtables and such.
6322 The linker will arbitrarily choose one and discard the others.
6323 The AT_*_pc values for such functions refer to local labels in
6324 these sections. If the section from that file was discarded, the
6325 labels are not in the output, so the relocs get a value of 0.
6326 If this is a discarded function, mark the pc bounds as invalid,
6327 so that GDB will ignore it. */
6328 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6329 return 0;
6330
6331 *lowpc = low;
6332 *highpc = high;
6333 return ret;
6334 }
6335
6336 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6337 its low and high PC addresses. Do nothing if these addresses could not
6338 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6339 and HIGHPC to the high address if greater than HIGHPC. */
6340
6341 static void
6342 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6343 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6344 struct dwarf2_cu *cu)
6345 {
6346 CORE_ADDR low, high;
6347 struct die_info *child = die->child;
6348
6349 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6350 {
6351 *lowpc = min (*lowpc, low);
6352 *highpc = max (*highpc, high);
6353 }
6354
6355 /* If the language does not allow nested subprograms (either inside
6356 subprograms or lexical blocks), we're done. */
6357 if (cu->language != language_ada)
6358 return;
6359
6360 /* Check all the children of the given DIE. If it contains nested
6361 subprograms, then check their pc bounds. Likewise, we need to
6362 check lexical blocks as well, as they may also contain subprogram
6363 definitions. */
6364 while (child && child->tag)
6365 {
6366 if (child->tag == DW_TAG_subprogram
6367 || child->tag == DW_TAG_lexical_block)
6368 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6369 child = sibling_die (child);
6370 }
6371 }
6372
6373 /* Get the low and high pc's represented by the scope DIE, and store
6374 them in *LOWPC and *HIGHPC. If the correct values can't be
6375 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6376
6377 static void
6378 get_scope_pc_bounds (struct die_info *die,
6379 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6380 struct dwarf2_cu *cu)
6381 {
6382 CORE_ADDR best_low = (CORE_ADDR) -1;
6383 CORE_ADDR best_high = (CORE_ADDR) 0;
6384 CORE_ADDR current_low, current_high;
6385
6386 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6387 {
6388 best_low = current_low;
6389 best_high = current_high;
6390 }
6391 else
6392 {
6393 struct die_info *child = die->child;
6394
6395 while (child && child->tag)
6396 {
6397 switch (child->tag) {
6398 case DW_TAG_subprogram:
6399 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6400 break;
6401 case DW_TAG_namespace:
6402 case DW_TAG_module:
6403 /* FIXME: carlton/2004-01-16: Should we do this for
6404 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6405 that current GCC's always emit the DIEs corresponding
6406 to definitions of methods of classes as children of a
6407 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6408 the DIEs giving the declarations, which could be
6409 anywhere). But I don't see any reason why the
6410 standards says that they have to be there. */
6411 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6412
6413 if (current_low != ((CORE_ADDR) -1))
6414 {
6415 best_low = min (best_low, current_low);
6416 best_high = max (best_high, current_high);
6417 }
6418 break;
6419 default:
6420 /* Ignore. */
6421 break;
6422 }
6423
6424 child = sibling_die (child);
6425 }
6426 }
6427
6428 *lowpc = best_low;
6429 *highpc = best_high;
6430 }
6431
6432 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6433 in DIE. */
6434 static void
6435 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6436 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6437 {
6438 struct attribute *attr;
6439
6440 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6441 if (attr)
6442 {
6443 CORE_ADDR high = DW_ADDR (attr);
6444
6445 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6446 if (attr)
6447 {
6448 CORE_ADDR low = DW_ADDR (attr);
6449
6450 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6451 }
6452 }
6453
6454 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6455 if (attr)
6456 {
6457 bfd *obfd = cu->objfile->obfd;
6458
6459 /* The value of the DW_AT_ranges attribute is the offset of the
6460 address range list in the .debug_ranges section. */
6461 unsigned long offset = DW_UNSND (attr);
6462 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6463
6464 /* For some target architectures, but not others, the
6465 read_address function sign-extends the addresses it returns.
6466 To recognize base address selection entries, we need a
6467 mask. */
6468 unsigned int addr_size = cu->header.addr_size;
6469 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6470
6471 /* The base address, to which the next pair is relative. Note
6472 that this 'base' is a DWARF concept: most entries in a range
6473 list are relative, to reduce the number of relocs against the
6474 debugging information. This is separate from this function's
6475 'baseaddr' argument, which GDB uses to relocate debugging
6476 information from a shared library based on the address at
6477 which the library was loaded. */
6478 CORE_ADDR base = cu->base_address;
6479 int base_known = cu->base_known;
6480
6481 gdb_assert (dwarf2_per_objfile->ranges.readin);
6482 if (offset >= dwarf2_per_objfile->ranges.size)
6483 {
6484 complaint (&symfile_complaints,
6485 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6486 offset);
6487 return;
6488 }
6489
6490 for (;;)
6491 {
6492 unsigned int bytes_read;
6493 CORE_ADDR start, end;
6494
6495 start = read_address (obfd, buffer, cu, &bytes_read);
6496 buffer += bytes_read;
6497 end = read_address (obfd, buffer, cu, &bytes_read);
6498 buffer += bytes_read;
6499
6500 /* Did we find the end of the range list? */
6501 if (start == 0 && end == 0)
6502 break;
6503
6504 /* Did we find a base address selection entry? */
6505 else if ((start & base_select_mask) == base_select_mask)
6506 {
6507 base = end;
6508 base_known = 1;
6509 }
6510
6511 /* We found an ordinary address range. */
6512 else
6513 {
6514 if (!base_known)
6515 {
6516 complaint (&symfile_complaints,
6517 _("Invalid .debug_ranges data "
6518 "(no base address)"));
6519 return;
6520 }
6521
6522 if (start > end)
6523 {
6524 /* Inverted range entries are invalid. */
6525 complaint (&symfile_complaints,
6526 _("Invalid .debug_ranges data "
6527 "(inverted range)"));
6528 return;
6529 }
6530
6531 /* Empty range entries have no effect. */
6532 if (start == end)
6533 continue;
6534
6535 record_block_range (block,
6536 baseaddr + base + start,
6537 baseaddr + base + end - 1);
6538 }
6539 }
6540 }
6541 }
6542
6543 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6544 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6545 during 4.6.0 experimental. */
6546
6547 static int
6548 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6549 {
6550 const char *cs;
6551 int major, minor, release;
6552
6553 if (cu->producer == NULL)
6554 {
6555 /* For unknown compilers expect their behavior is DWARF version
6556 compliant.
6557
6558 GCC started to support .debug_types sections by -gdwarf-4 since
6559 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6560 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6561 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6562 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6563
6564 return 0;
6565 }
6566
6567 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6568
6569 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6570 {
6571 /* For non-GCC compilers expect their behavior is DWARF version
6572 compliant. */
6573
6574 return 0;
6575 }
6576 cs = &cu->producer[strlen ("GNU ")];
6577 while (*cs && !isdigit (*cs))
6578 cs++;
6579 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6580 {
6581 /* Not recognized as GCC. */
6582
6583 return 0;
6584 }
6585
6586 return major < 4 || (major == 4 && minor < 6);
6587 }
6588
6589 /* Return the default accessibility type if it is not overriden by
6590 DW_AT_accessibility. */
6591
6592 static enum dwarf_access_attribute
6593 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6594 {
6595 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6596 {
6597 /* The default DWARF 2 accessibility for members is public, the default
6598 accessibility for inheritance is private. */
6599
6600 if (die->tag != DW_TAG_inheritance)
6601 return DW_ACCESS_public;
6602 else
6603 return DW_ACCESS_private;
6604 }
6605 else
6606 {
6607 /* DWARF 3+ defines the default accessibility a different way. The same
6608 rules apply now for DW_TAG_inheritance as for the members and it only
6609 depends on the container kind. */
6610
6611 if (die->parent->tag == DW_TAG_class_type)
6612 return DW_ACCESS_private;
6613 else
6614 return DW_ACCESS_public;
6615 }
6616 }
6617
6618 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6619 offset. If the attribute was not found return 0, otherwise return
6620 1. If it was found but could not properly be handled, set *OFFSET
6621 to 0. */
6622
6623 static int
6624 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6625 LONGEST *offset)
6626 {
6627 struct attribute *attr;
6628
6629 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6630 if (attr != NULL)
6631 {
6632 *offset = 0;
6633
6634 /* Note that we do not check for a section offset first here.
6635 This is because DW_AT_data_member_location is new in DWARF 4,
6636 so if we see it, we can assume that a constant form is really
6637 a constant and not a section offset. */
6638 if (attr_form_is_constant (attr))
6639 *offset = dwarf2_get_attr_constant_value (attr, 0);
6640 else if (attr_form_is_section_offset (attr))
6641 dwarf2_complex_location_expr_complaint ();
6642 else if (attr_form_is_block (attr))
6643 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6644 else
6645 dwarf2_complex_location_expr_complaint ();
6646
6647 return 1;
6648 }
6649
6650 return 0;
6651 }
6652
6653 /* Add an aggregate field to the field list. */
6654
6655 static void
6656 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6657 struct dwarf2_cu *cu)
6658 {
6659 struct objfile *objfile = cu->objfile;
6660 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6661 struct nextfield *new_field;
6662 struct attribute *attr;
6663 struct field *fp;
6664 char *fieldname = "";
6665
6666 /* Allocate a new field list entry and link it in. */
6667 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6668 make_cleanup (xfree, new_field);
6669 memset (new_field, 0, sizeof (struct nextfield));
6670
6671 if (die->tag == DW_TAG_inheritance)
6672 {
6673 new_field->next = fip->baseclasses;
6674 fip->baseclasses = new_field;
6675 }
6676 else
6677 {
6678 new_field->next = fip->fields;
6679 fip->fields = new_field;
6680 }
6681 fip->nfields++;
6682
6683 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6684 if (attr)
6685 new_field->accessibility = DW_UNSND (attr);
6686 else
6687 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6688 if (new_field->accessibility != DW_ACCESS_public)
6689 fip->non_public_fields = 1;
6690
6691 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6692 if (attr)
6693 new_field->virtuality = DW_UNSND (attr);
6694 else
6695 new_field->virtuality = DW_VIRTUALITY_none;
6696
6697 fp = &new_field->field;
6698
6699 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6700 {
6701 LONGEST offset;
6702
6703 /* Data member other than a C++ static data member. */
6704
6705 /* Get type of field. */
6706 fp->type = die_type (die, cu);
6707
6708 SET_FIELD_BITPOS (*fp, 0);
6709
6710 /* Get bit size of field (zero if none). */
6711 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6712 if (attr)
6713 {
6714 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6715 }
6716 else
6717 {
6718 FIELD_BITSIZE (*fp) = 0;
6719 }
6720
6721 /* Get bit offset of field. */
6722 if (handle_data_member_location (die, cu, &offset))
6723 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6724 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6725 if (attr)
6726 {
6727 if (gdbarch_bits_big_endian (gdbarch))
6728 {
6729 /* For big endian bits, the DW_AT_bit_offset gives the
6730 additional bit offset from the MSB of the containing
6731 anonymous object to the MSB of the field. We don't
6732 have to do anything special since we don't need to
6733 know the size of the anonymous object. */
6734 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6735 }
6736 else
6737 {
6738 /* For little endian bits, compute the bit offset to the
6739 MSB of the anonymous object, subtract off the number of
6740 bits from the MSB of the field to the MSB of the
6741 object, and then subtract off the number of bits of
6742 the field itself. The result is the bit offset of
6743 the LSB of the field. */
6744 int anonymous_size;
6745 int bit_offset = DW_UNSND (attr);
6746
6747 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6748 if (attr)
6749 {
6750 /* The size of the anonymous object containing
6751 the bit field is explicit, so use the
6752 indicated size (in bytes). */
6753 anonymous_size = DW_UNSND (attr);
6754 }
6755 else
6756 {
6757 /* The size of the anonymous object containing
6758 the bit field must be inferred from the type
6759 attribute of the data member containing the
6760 bit field. */
6761 anonymous_size = TYPE_LENGTH (fp->type);
6762 }
6763 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6764 - bit_offset - FIELD_BITSIZE (*fp);
6765 }
6766 }
6767
6768 /* Get name of field. */
6769 fieldname = dwarf2_name (die, cu);
6770 if (fieldname == NULL)
6771 fieldname = "";
6772
6773 /* The name is already allocated along with this objfile, so we don't
6774 need to duplicate it for the type. */
6775 fp->name = fieldname;
6776
6777 /* Change accessibility for artificial fields (e.g. virtual table
6778 pointer or virtual base class pointer) to private. */
6779 if (dwarf2_attr (die, DW_AT_artificial, cu))
6780 {
6781 FIELD_ARTIFICIAL (*fp) = 1;
6782 new_field->accessibility = DW_ACCESS_private;
6783 fip->non_public_fields = 1;
6784 }
6785 }
6786 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6787 {
6788 /* C++ static member. */
6789
6790 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6791 is a declaration, but all versions of G++ as of this writing
6792 (so through at least 3.2.1) incorrectly generate
6793 DW_TAG_variable tags. */
6794
6795 const char *physname;
6796
6797 /* Get name of field. */
6798 fieldname = dwarf2_name (die, cu);
6799 if (fieldname == NULL)
6800 return;
6801
6802 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6803 if (attr
6804 /* Only create a symbol if this is an external value.
6805 new_symbol checks this and puts the value in the global symbol
6806 table, which we want. If it is not external, new_symbol
6807 will try to put the value in cu->list_in_scope which is wrong. */
6808 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6809 {
6810 /* A static const member, not much different than an enum as far as
6811 we're concerned, except that we can support more types. */
6812 new_symbol (die, NULL, cu);
6813 }
6814
6815 /* Get physical name. */
6816 physname = dwarf2_physname (fieldname, die, cu);
6817
6818 /* The name is already allocated along with this objfile, so we don't
6819 need to duplicate it for the type. */
6820 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6821 FIELD_TYPE (*fp) = die_type (die, cu);
6822 FIELD_NAME (*fp) = fieldname;
6823 }
6824 else if (die->tag == DW_TAG_inheritance)
6825 {
6826 LONGEST offset;
6827
6828 /* C++ base class field. */
6829 if (handle_data_member_location (die, cu, &offset))
6830 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6831 FIELD_BITSIZE (*fp) = 0;
6832 FIELD_TYPE (*fp) = die_type (die, cu);
6833 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6834 fip->nbaseclasses++;
6835 }
6836 }
6837
6838 /* Add a typedef defined in the scope of the FIP's class. */
6839
6840 static void
6841 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6842 struct dwarf2_cu *cu)
6843 {
6844 struct objfile *objfile = cu->objfile;
6845 struct typedef_field_list *new_field;
6846 struct attribute *attr;
6847 struct typedef_field *fp;
6848 char *fieldname = "";
6849
6850 /* Allocate a new field list entry and link it in. */
6851 new_field = xzalloc (sizeof (*new_field));
6852 make_cleanup (xfree, new_field);
6853
6854 gdb_assert (die->tag == DW_TAG_typedef);
6855
6856 fp = &new_field->field;
6857
6858 /* Get name of field. */
6859 fp->name = dwarf2_name (die, cu);
6860 if (fp->name == NULL)
6861 return;
6862
6863 fp->type = read_type_die (die, cu);
6864
6865 new_field->next = fip->typedef_field_list;
6866 fip->typedef_field_list = new_field;
6867 fip->typedef_field_list_count++;
6868 }
6869
6870 /* Create the vector of fields, and attach it to the type. */
6871
6872 static void
6873 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6874 struct dwarf2_cu *cu)
6875 {
6876 int nfields = fip->nfields;
6877
6878 /* Record the field count, allocate space for the array of fields,
6879 and create blank accessibility bitfields if necessary. */
6880 TYPE_NFIELDS (type) = nfields;
6881 TYPE_FIELDS (type) = (struct field *)
6882 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6883 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6884
6885 if (fip->non_public_fields && cu->language != language_ada)
6886 {
6887 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6888
6889 TYPE_FIELD_PRIVATE_BITS (type) =
6890 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6891 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6892
6893 TYPE_FIELD_PROTECTED_BITS (type) =
6894 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6895 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6896
6897 TYPE_FIELD_IGNORE_BITS (type) =
6898 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6899 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6900 }
6901
6902 /* If the type has baseclasses, allocate and clear a bit vector for
6903 TYPE_FIELD_VIRTUAL_BITS. */
6904 if (fip->nbaseclasses && cu->language != language_ada)
6905 {
6906 int num_bytes = B_BYTES (fip->nbaseclasses);
6907 unsigned char *pointer;
6908
6909 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6910 pointer = TYPE_ALLOC (type, num_bytes);
6911 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6912 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6913 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6914 }
6915
6916 /* Copy the saved-up fields into the field vector. Start from the head of
6917 the list, adding to the tail of the field array, so that they end up in
6918 the same order in the array in which they were added to the list. */
6919 while (nfields-- > 0)
6920 {
6921 struct nextfield *fieldp;
6922
6923 if (fip->fields)
6924 {
6925 fieldp = fip->fields;
6926 fip->fields = fieldp->next;
6927 }
6928 else
6929 {
6930 fieldp = fip->baseclasses;
6931 fip->baseclasses = fieldp->next;
6932 }
6933
6934 TYPE_FIELD (type, nfields) = fieldp->field;
6935 switch (fieldp->accessibility)
6936 {
6937 case DW_ACCESS_private:
6938 if (cu->language != language_ada)
6939 SET_TYPE_FIELD_PRIVATE (type, nfields);
6940 break;
6941
6942 case DW_ACCESS_protected:
6943 if (cu->language != language_ada)
6944 SET_TYPE_FIELD_PROTECTED (type, nfields);
6945 break;
6946
6947 case DW_ACCESS_public:
6948 break;
6949
6950 default:
6951 /* Unknown accessibility. Complain and treat it as public. */
6952 {
6953 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6954 fieldp->accessibility);
6955 }
6956 break;
6957 }
6958 if (nfields < fip->nbaseclasses)
6959 {
6960 switch (fieldp->virtuality)
6961 {
6962 case DW_VIRTUALITY_virtual:
6963 case DW_VIRTUALITY_pure_virtual:
6964 if (cu->language == language_ada)
6965 error (_("unexpected virtuality in component of Ada type"));
6966 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6967 break;
6968 }
6969 }
6970 }
6971 }
6972
6973 /* Add a member function to the proper fieldlist. */
6974
6975 static void
6976 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6977 struct type *type, struct dwarf2_cu *cu)
6978 {
6979 struct objfile *objfile = cu->objfile;
6980 struct attribute *attr;
6981 struct fnfieldlist *flp;
6982 int i;
6983 struct fn_field *fnp;
6984 char *fieldname;
6985 struct nextfnfield *new_fnfield;
6986 struct type *this_type;
6987 enum dwarf_access_attribute accessibility;
6988
6989 if (cu->language == language_ada)
6990 error (_("unexpected member function in Ada type"));
6991
6992 /* Get name of member function. */
6993 fieldname = dwarf2_name (die, cu);
6994 if (fieldname == NULL)
6995 return;
6996
6997 /* Look up member function name in fieldlist. */
6998 for (i = 0; i < fip->nfnfields; i++)
6999 {
7000 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7001 break;
7002 }
7003
7004 /* Create new list element if necessary. */
7005 if (i < fip->nfnfields)
7006 flp = &fip->fnfieldlists[i];
7007 else
7008 {
7009 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7010 {
7011 fip->fnfieldlists = (struct fnfieldlist *)
7012 xrealloc (fip->fnfieldlists,
7013 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7014 * sizeof (struct fnfieldlist));
7015 if (fip->nfnfields == 0)
7016 make_cleanup (free_current_contents, &fip->fnfieldlists);
7017 }
7018 flp = &fip->fnfieldlists[fip->nfnfields];
7019 flp->name = fieldname;
7020 flp->length = 0;
7021 flp->head = NULL;
7022 i = fip->nfnfields++;
7023 }
7024
7025 /* Create a new member function field and chain it to the field list
7026 entry. */
7027 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7028 make_cleanup (xfree, new_fnfield);
7029 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7030 new_fnfield->next = flp->head;
7031 flp->head = new_fnfield;
7032 flp->length++;
7033
7034 /* Fill in the member function field info. */
7035 fnp = &new_fnfield->fnfield;
7036
7037 /* Delay processing of the physname until later. */
7038 if (cu->language == language_cplus || cu->language == language_java)
7039 {
7040 add_to_method_list (type, i, flp->length - 1, fieldname,
7041 die, cu);
7042 }
7043 else
7044 {
7045 const char *physname = dwarf2_physname (fieldname, die, cu);
7046 fnp->physname = physname ? physname : "";
7047 }
7048
7049 fnp->type = alloc_type (objfile);
7050 this_type = read_type_die (die, cu);
7051 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7052 {
7053 int nparams = TYPE_NFIELDS (this_type);
7054
7055 /* TYPE is the domain of this method, and THIS_TYPE is the type
7056 of the method itself (TYPE_CODE_METHOD). */
7057 smash_to_method_type (fnp->type, type,
7058 TYPE_TARGET_TYPE (this_type),
7059 TYPE_FIELDS (this_type),
7060 TYPE_NFIELDS (this_type),
7061 TYPE_VARARGS (this_type));
7062
7063 /* Handle static member functions.
7064 Dwarf2 has no clean way to discern C++ static and non-static
7065 member functions. G++ helps GDB by marking the first
7066 parameter for non-static member functions (which is the this
7067 pointer) as artificial. We obtain this information from
7068 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7069 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7070 fnp->voffset = VOFFSET_STATIC;
7071 }
7072 else
7073 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7074 dwarf2_full_name (fieldname, die, cu));
7075
7076 /* Get fcontext from DW_AT_containing_type if present. */
7077 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7078 fnp->fcontext = die_containing_type (die, cu);
7079
7080 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7081 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7082
7083 /* Get accessibility. */
7084 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7085 if (attr)
7086 accessibility = DW_UNSND (attr);
7087 else
7088 accessibility = dwarf2_default_access_attribute (die, cu);
7089 switch (accessibility)
7090 {
7091 case DW_ACCESS_private:
7092 fnp->is_private = 1;
7093 break;
7094 case DW_ACCESS_protected:
7095 fnp->is_protected = 1;
7096 break;
7097 }
7098
7099 /* Check for artificial methods. */
7100 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7101 if (attr && DW_UNSND (attr) != 0)
7102 fnp->is_artificial = 1;
7103
7104 /* Get index in virtual function table if it is a virtual member
7105 function. For older versions of GCC, this is an offset in the
7106 appropriate virtual table, as specified by DW_AT_containing_type.
7107 For everyone else, it is an expression to be evaluated relative
7108 to the object address. */
7109
7110 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7111 if (attr)
7112 {
7113 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7114 {
7115 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7116 {
7117 /* Old-style GCC. */
7118 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7119 }
7120 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7121 || (DW_BLOCK (attr)->size > 1
7122 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7123 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7124 {
7125 struct dwarf_block blk;
7126 int offset;
7127
7128 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7129 ? 1 : 2);
7130 blk.size = DW_BLOCK (attr)->size - offset;
7131 blk.data = DW_BLOCK (attr)->data + offset;
7132 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7133 if ((fnp->voffset % cu->header.addr_size) != 0)
7134 dwarf2_complex_location_expr_complaint ();
7135 else
7136 fnp->voffset /= cu->header.addr_size;
7137 fnp->voffset += 2;
7138 }
7139 else
7140 dwarf2_complex_location_expr_complaint ();
7141
7142 if (!fnp->fcontext)
7143 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7144 }
7145 else if (attr_form_is_section_offset (attr))
7146 {
7147 dwarf2_complex_location_expr_complaint ();
7148 }
7149 else
7150 {
7151 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7152 fieldname);
7153 }
7154 }
7155 else
7156 {
7157 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7158 if (attr && DW_UNSND (attr))
7159 {
7160 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7161 complaint (&symfile_complaints,
7162 _("Member function \"%s\" (offset %d) is virtual "
7163 "but the vtable offset is not specified"),
7164 fieldname, die->offset);
7165 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7166 TYPE_CPLUS_DYNAMIC (type) = 1;
7167 }
7168 }
7169 }
7170
7171 /* Create the vector of member function fields, and attach it to the type. */
7172
7173 static void
7174 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7175 struct dwarf2_cu *cu)
7176 {
7177 struct fnfieldlist *flp;
7178 int total_length = 0;
7179 int i;
7180
7181 if (cu->language == language_ada)
7182 error (_("unexpected member functions in Ada type"));
7183
7184 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7185 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7186 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7187
7188 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7189 {
7190 struct nextfnfield *nfp = flp->head;
7191 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7192 int k;
7193
7194 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7195 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7196 fn_flp->fn_fields = (struct fn_field *)
7197 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7198 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7199 fn_flp->fn_fields[k] = nfp->fnfield;
7200
7201 total_length += flp->length;
7202 }
7203
7204 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7205 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7206 }
7207
7208 /* Returns non-zero if NAME is the name of a vtable member in CU's
7209 language, zero otherwise. */
7210 static int
7211 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7212 {
7213 static const char vptr[] = "_vptr";
7214 static const char vtable[] = "vtable";
7215
7216 /* Look for the C++ and Java forms of the vtable. */
7217 if ((cu->language == language_java
7218 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7219 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7220 && is_cplus_marker (name[sizeof (vptr) - 1])))
7221 return 1;
7222
7223 return 0;
7224 }
7225
7226 /* GCC outputs unnamed structures that are really pointers to member
7227 functions, with the ABI-specified layout. If TYPE describes
7228 such a structure, smash it into a member function type.
7229
7230 GCC shouldn't do this; it should just output pointer to member DIEs.
7231 This is GCC PR debug/28767. */
7232
7233 static void
7234 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7235 {
7236 struct type *pfn_type, *domain_type, *new_type;
7237
7238 /* Check for a structure with no name and two children. */
7239 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7240 return;
7241
7242 /* Check for __pfn and __delta members. */
7243 if (TYPE_FIELD_NAME (type, 0) == NULL
7244 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7245 || TYPE_FIELD_NAME (type, 1) == NULL
7246 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7247 return;
7248
7249 /* Find the type of the method. */
7250 pfn_type = TYPE_FIELD_TYPE (type, 0);
7251 if (pfn_type == NULL
7252 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7253 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7254 return;
7255
7256 /* Look for the "this" argument. */
7257 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7258 if (TYPE_NFIELDS (pfn_type) == 0
7259 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7260 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7261 return;
7262
7263 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7264 new_type = alloc_type (objfile);
7265 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7266 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7267 TYPE_VARARGS (pfn_type));
7268 smash_to_methodptr_type (type, new_type);
7269 }
7270
7271 /* Called when we find the DIE that starts a structure or union scope
7272 (definition) to create a type for the structure or union. Fill in
7273 the type's name and general properties; the members will not be
7274 processed until process_structure_type.
7275
7276 NOTE: we need to call these functions regardless of whether or not the
7277 DIE has a DW_AT_name attribute, since it might be an anonymous
7278 structure or union. This gets the type entered into our set of
7279 user defined types.
7280
7281 However, if the structure is incomplete (an opaque struct/union)
7282 then suppress creating a symbol table entry for it since gdb only
7283 wants to find the one with the complete definition. Note that if
7284 it is complete, we just call new_symbol, which does it's own
7285 checking about whether the struct/union is anonymous or not (and
7286 suppresses creating a symbol table entry itself). */
7287
7288 static struct type *
7289 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7290 {
7291 struct objfile *objfile = cu->objfile;
7292 struct type *type;
7293 struct attribute *attr;
7294 char *name;
7295
7296 /* If the definition of this type lives in .debug_types, read that type.
7297 Don't follow DW_AT_specification though, that will take us back up
7298 the chain and we want to go down. */
7299 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7300 if (attr)
7301 {
7302 struct dwarf2_cu *type_cu = cu;
7303 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7304
7305 /* We could just recurse on read_structure_type, but we need to call
7306 get_die_type to ensure only one type for this DIE is created.
7307 This is important, for example, because for c++ classes we need
7308 TYPE_NAME set which is only done by new_symbol. Blech. */
7309 type = read_type_die (type_die, type_cu);
7310
7311 /* TYPE_CU may not be the same as CU.
7312 Ensure TYPE is recorded in CU's type_hash table. */
7313 return set_die_type (die, type, cu);
7314 }
7315
7316 type = alloc_type (objfile);
7317 INIT_CPLUS_SPECIFIC (type);
7318
7319 name = dwarf2_name (die, cu);
7320 if (name != NULL)
7321 {
7322 if (cu->language == language_cplus
7323 || cu->language == language_java)
7324 {
7325 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7326
7327 /* dwarf2_full_name might have already finished building the DIE's
7328 type. If so, there is no need to continue. */
7329 if (get_die_type (die, cu) != NULL)
7330 return get_die_type (die, cu);
7331
7332 TYPE_TAG_NAME (type) = full_name;
7333 if (die->tag == DW_TAG_structure_type
7334 || die->tag == DW_TAG_class_type)
7335 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7336 }
7337 else
7338 {
7339 /* The name is already allocated along with this objfile, so
7340 we don't need to duplicate it for the type. */
7341 TYPE_TAG_NAME (type) = (char *) name;
7342 if (die->tag == DW_TAG_class_type)
7343 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7344 }
7345 }
7346
7347 if (die->tag == DW_TAG_structure_type)
7348 {
7349 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7350 }
7351 else if (die->tag == DW_TAG_union_type)
7352 {
7353 TYPE_CODE (type) = TYPE_CODE_UNION;
7354 }
7355 else
7356 {
7357 TYPE_CODE (type) = TYPE_CODE_CLASS;
7358 }
7359
7360 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7361 TYPE_DECLARED_CLASS (type) = 1;
7362
7363 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7364 if (attr)
7365 {
7366 TYPE_LENGTH (type) = DW_UNSND (attr);
7367 }
7368 else
7369 {
7370 TYPE_LENGTH (type) = 0;
7371 }
7372
7373 TYPE_STUB_SUPPORTED (type) = 1;
7374 if (die_is_declaration (die, cu))
7375 TYPE_STUB (type) = 1;
7376 else if (attr == NULL && die->child == NULL
7377 && producer_is_realview (cu->producer))
7378 /* RealView does not output the required DW_AT_declaration
7379 on incomplete types. */
7380 TYPE_STUB (type) = 1;
7381
7382 /* We need to add the type field to the die immediately so we don't
7383 infinitely recurse when dealing with pointers to the structure
7384 type within the structure itself. */
7385 set_die_type (die, type, cu);
7386
7387 /* set_die_type should be already done. */
7388 set_descriptive_type (type, die, cu);
7389
7390 return type;
7391 }
7392
7393 /* Finish creating a structure or union type, including filling in
7394 its members and creating a symbol for it. */
7395
7396 static void
7397 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7398 {
7399 struct objfile *objfile = cu->objfile;
7400 struct die_info *child_die = die->child;
7401 struct type *type;
7402
7403 type = get_die_type (die, cu);
7404 if (type == NULL)
7405 type = read_structure_type (die, cu);
7406
7407 if (die->child != NULL && ! die_is_declaration (die, cu))
7408 {
7409 struct field_info fi;
7410 struct die_info *child_die;
7411 VEC (symbolp) *template_args = NULL;
7412 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7413
7414 memset (&fi, 0, sizeof (struct field_info));
7415
7416 child_die = die->child;
7417
7418 while (child_die && child_die->tag)
7419 {
7420 if (child_die->tag == DW_TAG_member
7421 || child_die->tag == DW_TAG_variable)
7422 {
7423 /* NOTE: carlton/2002-11-05: A C++ static data member
7424 should be a DW_TAG_member that is a declaration, but
7425 all versions of G++ as of this writing (so through at
7426 least 3.2.1) incorrectly generate DW_TAG_variable
7427 tags for them instead. */
7428 dwarf2_add_field (&fi, child_die, cu);
7429 }
7430 else if (child_die->tag == DW_TAG_subprogram)
7431 {
7432 /* C++ member function. */
7433 dwarf2_add_member_fn (&fi, child_die, type, cu);
7434 }
7435 else if (child_die->tag == DW_TAG_inheritance)
7436 {
7437 /* C++ base class field. */
7438 dwarf2_add_field (&fi, child_die, cu);
7439 }
7440 else if (child_die->tag == DW_TAG_typedef)
7441 dwarf2_add_typedef (&fi, child_die, cu);
7442 else if (child_die->tag == DW_TAG_template_type_param
7443 || child_die->tag == DW_TAG_template_value_param)
7444 {
7445 struct symbol *arg = new_symbol (child_die, NULL, cu);
7446
7447 if (arg != NULL)
7448 VEC_safe_push (symbolp, template_args, arg);
7449 }
7450
7451 child_die = sibling_die (child_die);
7452 }
7453
7454 /* Attach template arguments to type. */
7455 if (! VEC_empty (symbolp, template_args))
7456 {
7457 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7458 TYPE_N_TEMPLATE_ARGUMENTS (type)
7459 = VEC_length (symbolp, template_args);
7460 TYPE_TEMPLATE_ARGUMENTS (type)
7461 = obstack_alloc (&objfile->objfile_obstack,
7462 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7463 * sizeof (struct symbol *)));
7464 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7465 VEC_address (symbolp, template_args),
7466 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7467 * sizeof (struct symbol *)));
7468 VEC_free (symbolp, template_args);
7469 }
7470
7471 /* Attach fields and member functions to the type. */
7472 if (fi.nfields)
7473 dwarf2_attach_fields_to_type (&fi, type, cu);
7474 if (fi.nfnfields)
7475 {
7476 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7477
7478 /* Get the type which refers to the base class (possibly this
7479 class itself) which contains the vtable pointer for the current
7480 class from the DW_AT_containing_type attribute. This use of
7481 DW_AT_containing_type is a GNU extension. */
7482
7483 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7484 {
7485 struct type *t = die_containing_type (die, cu);
7486
7487 TYPE_VPTR_BASETYPE (type) = t;
7488 if (type == t)
7489 {
7490 int i;
7491
7492 /* Our own class provides vtbl ptr. */
7493 for (i = TYPE_NFIELDS (t) - 1;
7494 i >= TYPE_N_BASECLASSES (t);
7495 --i)
7496 {
7497 char *fieldname = TYPE_FIELD_NAME (t, i);
7498
7499 if (is_vtable_name (fieldname, cu))
7500 {
7501 TYPE_VPTR_FIELDNO (type) = i;
7502 break;
7503 }
7504 }
7505
7506 /* Complain if virtual function table field not found. */
7507 if (i < TYPE_N_BASECLASSES (t))
7508 complaint (&symfile_complaints,
7509 _("virtual function table pointer "
7510 "not found when defining class '%s'"),
7511 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7512 "");
7513 }
7514 else
7515 {
7516 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7517 }
7518 }
7519 else if (cu->producer
7520 && strncmp (cu->producer,
7521 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7522 {
7523 /* The IBM XLC compiler does not provide direct indication
7524 of the containing type, but the vtable pointer is
7525 always named __vfp. */
7526
7527 int i;
7528
7529 for (i = TYPE_NFIELDS (type) - 1;
7530 i >= TYPE_N_BASECLASSES (type);
7531 --i)
7532 {
7533 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7534 {
7535 TYPE_VPTR_FIELDNO (type) = i;
7536 TYPE_VPTR_BASETYPE (type) = type;
7537 break;
7538 }
7539 }
7540 }
7541 }
7542
7543 /* Copy fi.typedef_field_list linked list elements content into the
7544 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7545 if (fi.typedef_field_list)
7546 {
7547 int i = fi.typedef_field_list_count;
7548
7549 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7550 TYPE_TYPEDEF_FIELD_ARRAY (type)
7551 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7552 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7553
7554 /* Reverse the list order to keep the debug info elements order. */
7555 while (--i >= 0)
7556 {
7557 struct typedef_field *dest, *src;
7558
7559 dest = &TYPE_TYPEDEF_FIELD (type, i);
7560 src = &fi.typedef_field_list->field;
7561 fi.typedef_field_list = fi.typedef_field_list->next;
7562 *dest = *src;
7563 }
7564 }
7565
7566 do_cleanups (back_to);
7567
7568 if (HAVE_CPLUS_STRUCT (type))
7569 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7570 }
7571
7572 quirk_gcc_member_function_pointer (type, cu->objfile);
7573
7574 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7575 snapshots) has been known to create a die giving a declaration
7576 for a class that has, as a child, a die giving a definition for a
7577 nested class. So we have to process our children even if the
7578 current die is a declaration. Normally, of course, a declaration
7579 won't have any children at all. */
7580
7581 while (child_die != NULL && child_die->tag)
7582 {
7583 if (child_die->tag == DW_TAG_member
7584 || child_die->tag == DW_TAG_variable
7585 || child_die->tag == DW_TAG_inheritance
7586 || child_die->tag == DW_TAG_template_value_param
7587 || child_die->tag == DW_TAG_template_type_param)
7588 {
7589 /* Do nothing. */
7590 }
7591 else
7592 process_die (child_die, cu);
7593
7594 child_die = sibling_die (child_die);
7595 }
7596
7597 /* Do not consider external references. According to the DWARF standard,
7598 these DIEs are identified by the fact that they have no byte_size
7599 attribute, and a declaration attribute. */
7600 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7601 || !die_is_declaration (die, cu))
7602 new_symbol (die, type, cu);
7603 }
7604
7605 /* Given a DW_AT_enumeration_type die, set its type. We do not
7606 complete the type's fields yet, or create any symbols. */
7607
7608 static struct type *
7609 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7610 {
7611 struct objfile *objfile = cu->objfile;
7612 struct type *type;
7613 struct attribute *attr;
7614 const char *name;
7615
7616 /* If the definition of this type lives in .debug_types, read that type.
7617 Don't follow DW_AT_specification though, that will take us back up
7618 the chain and we want to go down. */
7619 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7620 if (attr)
7621 {
7622 struct dwarf2_cu *type_cu = cu;
7623 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7624
7625 type = read_type_die (type_die, type_cu);
7626
7627 /* TYPE_CU may not be the same as CU.
7628 Ensure TYPE is recorded in CU's type_hash table. */
7629 return set_die_type (die, type, cu);
7630 }
7631
7632 type = alloc_type (objfile);
7633
7634 TYPE_CODE (type) = TYPE_CODE_ENUM;
7635 name = dwarf2_full_name (NULL, die, cu);
7636 if (name != NULL)
7637 TYPE_TAG_NAME (type) = (char *) name;
7638
7639 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7640 if (attr)
7641 {
7642 TYPE_LENGTH (type) = DW_UNSND (attr);
7643 }
7644 else
7645 {
7646 TYPE_LENGTH (type) = 0;
7647 }
7648
7649 /* The enumeration DIE can be incomplete. In Ada, any type can be
7650 declared as private in the package spec, and then defined only
7651 inside the package body. Such types are known as Taft Amendment
7652 Types. When another package uses such a type, an incomplete DIE
7653 may be generated by the compiler. */
7654 if (die_is_declaration (die, cu))
7655 TYPE_STUB (type) = 1;
7656
7657 return set_die_type (die, type, cu);
7658 }
7659
7660 /* Given a pointer to a die which begins an enumeration, process all
7661 the dies that define the members of the enumeration, and create the
7662 symbol for the enumeration type.
7663
7664 NOTE: We reverse the order of the element list. */
7665
7666 static void
7667 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7668 {
7669 struct type *this_type;
7670
7671 this_type = get_die_type (die, cu);
7672 if (this_type == NULL)
7673 this_type = read_enumeration_type (die, cu);
7674
7675 if (die->child != NULL)
7676 {
7677 struct die_info *child_die;
7678 struct symbol *sym;
7679 struct field *fields = NULL;
7680 int num_fields = 0;
7681 int unsigned_enum = 1;
7682 char *name;
7683
7684 child_die = die->child;
7685 while (child_die && child_die->tag)
7686 {
7687 if (child_die->tag != DW_TAG_enumerator)
7688 {
7689 process_die (child_die, cu);
7690 }
7691 else
7692 {
7693 name = dwarf2_name (child_die, cu);
7694 if (name)
7695 {
7696 sym = new_symbol (child_die, this_type, cu);
7697 if (SYMBOL_VALUE (sym) < 0)
7698 unsigned_enum = 0;
7699
7700 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7701 {
7702 fields = (struct field *)
7703 xrealloc (fields,
7704 (num_fields + DW_FIELD_ALLOC_CHUNK)
7705 * sizeof (struct field));
7706 }
7707
7708 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7709 FIELD_TYPE (fields[num_fields]) = NULL;
7710 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7711 FIELD_BITSIZE (fields[num_fields]) = 0;
7712
7713 num_fields++;
7714 }
7715 }
7716
7717 child_die = sibling_die (child_die);
7718 }
7719
7720 if (num_fields)
7721 {
7722 TYPE_NFIELDS (this_type) = num_fields;
7723 TYPE_FIELDS (this_type) = (struct field *)
7724 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7725 memcpy (TYPE_FIELDS (this_type), fields,
7726 sizeof (struct field) * num_fields);
7727 xfree (fields);
7728 }
7729 if (unsigned_enum)
7730 TYPE_UNSIGNED (this_type) = 1;
7731 }
7732
7733 new_symbol (die, this_type, cu);
7734 }
7735
7736 /* Extract all information from a DW_TAG_array_type DIE and put it in
7737 the DIE's type field. For now, this only handles one dimensional
7738 arrays. */
7739
7740 static struct type *
7741 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7742 {
7743 struct objfile *objfile = cu->objfile;
7744 struct die_info *child_die;
7745 struct type *type;
7746 struct type *element_type, *range_type, *index_type;
7747 struct type **range_types = NULL;
7748 struct attribute *attr;
7749 int ndim = 0;
7750 struct cleanup *back_to;
7751 char *name;
7752
7753 element_type = die_type (die, cu);
7754
7755 /* The die_type call above may have already set the type for this DIE. */
7756 type = get_die_type (die, cu);
7757 if (type)
7758 return type;
7759
7760 /* Irix 6.2 native cc creates array types without children for
7761 arrays with unspecified length. */
7762 if (die->child == NULL)
7763 {
7764 index_type = objfile_type (objfile)->builtin_int;
7765 range_type = create_range_type (NULL, index_type, 0, -1);
7766 type = create_array_type (NULL, element_type, range_type);
7767 return set_die_type (die, type, cu);
7768 }
7769
7770 back_to = make_cleanup (null_cleanup, NULL);
7771 child_die = die->child;
7772 while (child_die && child_die->tag)
7773 {
7774 if (child_die->tag == DW_TAG_subrange_type)
7775 {
7776 struct type *child_type = read_type_die (child_die, cu);
7777
7778 if (child_type != NULL)
7779 {
7780 /* The range type was succesfully read. Save it for the
7781 array type creation. */
7782 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7783 {
7784 range_types = (struct type **)
7785 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7786 * sizeof (struct type *));
7787 if (ndim == 0)
7788 make_cleanup (free_current_contents, &range_types);
7789 }
7790 range_types[ndim++] = child_type;
7791 }
7792 }
7793 child_die = sibling_die (child_die);
7794 }
7795
7796 /* Dwarf2 dimensions are output from left to right, create the
7797 necessary array types in backwards order. */
7798
7799 type = element_type;
7800
7801 if (read_array_order (die, cu) == DW_ORD_col_major)
7802 {
7803 int i = 0;
7804
7805 while (i < ndim)
7806 type = create_array_type (NULL, type, range_types[i++]);
7807 }
7808 else
7809 {
7810 while (ndim-- > 0)
7811 type = create_array_type (NULL, type, range_types[ndim]);
7812 }
7813
7814 /* Understand Dwarf2 support for vector types (like they occur on
7815 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7816 array type. This is not part of the Dwarf2/3 standard yet, but a
7817 custom vendor extension. The main difference between a regular
7818 array and the vector variant is that vectors are passed by value
7819 to functions. */
7820 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7821 if (attr)
7822 make_vector_type (type);
7823
7824 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7825 implementation may choose to implement triple vectors using this
7826 attribute. */
7827 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7828 if (attr)
7829 {
7830 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7831 TYPE_LENGTH (type) = DW_UNSND (attr);
7832 else
7833 complaint (&symfile_complaints,
7834 _("DW_AT_byte_size for array type smaller "
7835 "than the total size of elements"));
7836 }
7837
7838 name = dwarf2_name (die, cu);
7839 if (name)
7840 TYPE_NAME (type) = name;
7841
7842 /* Install the type in the die. */
7843 set_die_type (die, type, cu);
7844
7845 /* set_die_type should be already done. */
7846 set_descriptive_type (type, die, cu);
7847
7848 do_cleanups (back_to);
7849
7850 return type;
7851 }
7852
7853 static enum dwarf_array_dim_ordering
7854 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7855 {
7856 struct attribute *attr;
7857
7858 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7859
7860 if (attr) return DW_SND (attr);
7861
7862 /* GNU F77 is a special case, as at 08/2004 array type info is the
7863 opposite order to the dwarf2 specification, but data is still
7864 laid out as per normal fortran.
7865
7866 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7867 version checking. */
7868
7869 if (cu->language == language_fortran
7870 && cu->producer && strstr (cu->producer, "GNU F77"))
7871 {
7872 return DW_ORD_row_major;
7873 }
7874
7875 switch (cu->language_defn->la_array_ordering)
7876 {
7877 case array_column_major:
7878 return DW_ORD_col_major;
7879 case array_row_major:
7880 default:
7881 return DW_ORD_row_major;
7882 };
7883 }
7884
7885 /* Extract all information from a DW_TAG_set_type DIE and put it in
7886 the DIE's type field. */
7887
7888 static struct type *
7889 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7890 {
7891 struct type *domain_type, *set_type;
7892 struct attribute *attr;
7893
7894 domain_type = die_type (die, cu);
7895
7896 /* The die_type call above may have already set the type for this DIE. */
7897 set_type = get_die_type (die, cu);
7898 if (set_type)
7899 return set_type;
7900
7901 set_type = create_set_type (NULL, domain_type);
7902
7903 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7904 if (attr)
7905 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7906
7907 return set_die_type (die, set_type, cu);
7908 }
7909
7910 /* First cut: install each common block member as a global variable. */
7911
7912 static void
7913 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7914 {
7915 struct die_info *child_die;
7916 struct attribute *attr;
7917 struct symbol *sym;
7918 CORE_ADDR base = (CORE_ADDR) 0;
7919
7920 attr = dwarf2_attr (die, DW_AT_location, cu);
7921 if (attr)
7922 {
7923 /* Support the .debug_loc offsets. */
7924 if (attr_form_is_block (attr))
7925 {
7926 base = decode_locdesc (DW_BLOCK (attr), cu);
7927 }
7928 else if (attr_form_is_section_offset (attr))
7929 {
7930 dwarf2_complex_location_expr_complaint ();
7931 }
7932 else
7933 {
7934 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7935 "common block member");
7936 }
7937 }
7938 if (die->child != NULL)
7939 {
7940 child_die = die->child;
7941 while (child_die && child_die->tag)
7942 {
7943 LONGEST offset;
7944
7945 sym = new_symbol (child_die, NULL, cu);
7946 if (sym != NULL
7947 && handle_data_member_location (child_die, cu, &offset))
7948 {
7949 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
7950 add_symbol_to_list (sym, &global_symbols);
7951 }
7952 child_die = sibling_die (child_die);
7953 }
7954 }
7955 }
7956
7957 /* Create a type for a C++ namespace. */
7958
7959 static struct type *
7960 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7961 {
7962 struct objfile *objfile = cu->objfile;
7963 const char *previous_prefix, *name;
7964 int is_anonymous;
7965 struct type *type;
7966
7967 /* For extensions, reuse the type of the original namespace. */
7968 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7969 {
7970 struct die_info *ext_die;
7971 struct dwarf2_cu *ext_cu = cu;
7972
7973 ext_die = dwarf2_extension (die, &ext_cu);
7974 type = read_type_die (ext_die, ext_cu);
7975
7976 /* EXT_CU may not be the same as CU.
7977 Ensure TYPE is recorded in CU's type_hash table. */
7978 return set_die_type (die, type, cu);
7979 }
7980
7981 name = namespace_name (die, &is_anonymous, cu);
7982
7983 /* Now build the name of the current namespace. */
7984
7985 previous_prefix = determine_prefix (die, cu);
7986 if (previous_prefix[0] != '\0')
7987 name = typename_concat (&objfile->objfile_obstack,
7988 previous_prefix, name, 0, cu);
7989
7990 /* Create the type. */
7991 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7992 objfile);
7993 TYPE_NAME (type) = (char *) name;
7994 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7995
7996 return set_die_type (die, type, cu);
7997 }
7998
7999 /* Read a C++ namespace. */
8000
8001 static void
8002 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8003 {
8004 struct objfile *objfile = cu->objfile;
8005 int is_anonymous;
8006
8007 /* Add a symbol associated to this if we haven't seen the namespace
8008 before. Also, add a using directive if it's an anonymous
8009 namespace. */
8010
8011 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8012 {
8013 struct type *type;
8014
8015 type = read_type_die (die, cu);
8016 new_symbol (die, type, cu);
8017
8018 namespace_name (die, &is_anonymous, cu);
8019 if (is_anonymous)
8020 {
8021 const char *previous_prefix = determine_prefix (die, cu);
8022
8023 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8024 NULL, NULL, &objfile->objfile_obstack);
8025 }
8026 }
8027
8028 if (die->child != NULL)
8029 {
8030 struct die_info *child_die = die->child;
8031
8032 while (child_die && child_die->tag)
8033 {
8034 process_die (child_die, cu);
8035 child_die = sibling_die (child_die);
8036 }
8037 }
8038 }
8039
8040 /* Read a Fortran module as type. This DIE can be only a declaration used for
8041 imported module. Still we need that type as local Fortran "use ... only"
8042 declaration imports depend on the created type in determine_prefix. */
8043
8044 static struct type *
8045 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8046 {
8047 struct objfile *objfile = cu->objfile;
8048 char *module_name;
8049 struct type *type;
8050
8051 module_name = dwarf2_name (die, cu);
8052 if (!module_name)
8053 complaint (&symfile_complaints,
8054 _("DW_TAG_module has no name, offset 0x%x"),
8055 die->offset);
8056 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8057
8058 /* determine_prefix uses TYPE_TAG_NAME. */
8059 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8060
8061 return set_die_type (die, type, cu);
8062 }
8063
8064 /* Read a Fortran module. */
8065
8066 static void
8067 read_module (struct die_info *die, struct dwarf2_cu *cu)
8068 {
8069 struct die_info *child_die = die->child;
8070
8071 while (child_die && child_die->tag)
8072 {
8073 process_die (child_die, cu);
8074 child_die = sibling_die (child_die);
8075 }
8076 }
8077
8078 /* Return the name of the namespace represented by DIE. Set
8079 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8080 namespace. */
8081
8082 static const char *
8083 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8084 {
8085 struct die_info *current_die;
8086 const char *name = NULL;
8087
8088 /* Loop through the extensions until we find a name. */
8089
8090 for (current_die = die;
8091 current_die != NULL;
8092 current_die = dwarf2_extension (die, &cu))
8093 {
8094 name = dwarf2_name (current_die, cu);
8095 if (name != NULL)
8096 break;
8097 }
8098
8099 /* Is it an anonymous namespace? */
8100
8101 *is_anonymous = (name == NULL);
8102 if (*is_anonymous)
8103 name = CP_ANONYMOUS_NAMESPACE_STR;
8104
8105 return name;
8106 }
8107
8108 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8109 the user defined type vector. */
8110
8111 static struct type *
8112 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8113 {
8114 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8115 struct comp_unit_head *cu_header = &cu->header;
8116 struct type *type;
8117 struct attribute *attr_byte_size;
8118 struct attribute *attr_address_class;
8119 int byte_size, addr_class;
8120 struct type *target_type;
8121
8122 target_type = die_type (die, cu);
8123
8124 /* The die_type call above may have already set the type for this DIE. */
8125 type = get_die_type (die, cu);
8126 if (type)
8127 return type;
8128
8129 type = lookup_pointer_type (target_type);
8130
8131 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8132 if (attr_byte_size)
8133 byte_size = DW_UNSND (attr_byte_size);
8134 else
8135 byte_size = cu_header->addr_size;
8136
8137 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8138 if (attr_address_class)
8139 addr_class = DW_UNSND (attr_address_class);
8140 else
8141 addr_class = DW_ADDR_none;
8142
8143 /* If the pointer size or address class is different than the
8144 default, create a type variant marked as such and set the
8145 length accordingly. */
8146 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8147 {
8148 if (gdbarch_address_class_type_flags_p (gdbarch))
8149 {
8150 int type_flags;
8151
8152 type_flags = gdbarch_address_class_type_flags
8153 (gdbarch, byte_size, addr_class);
8154 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8155 == 0);
8156 type = make_type_with_address_space (type, type_flags);
8157 }
8158 else if (TYPE_LENGTH (type) != byte_size)
8159 {
8160 complaint (&symfile_complaints,
8161 _("invalid pointer size %d"), byte_size);
8162 }
8163 else
8164 {
8165 /* Should we also complain about unhandled address classes? */
8166 }
8167 }
8168
8169 TYPE_LENGTH (type) = byte_size;
8170 return set_die_type (die, type, cu);
8171 }
8172
8173 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8174 the user defined type vector. */
8175
8176 static struct type *
8177 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8178 {
8179 struct type *type;
8180 struct type *to_type;
8181 struct type *domain;
8182
8183 to_type = die_type (die, cu);
8184 domain = die_containing_type (die, cu);
8185
8186 /* The calls above may have already set the type for this DIE. */
8187 type = get_die_type (die, cu);
8188 if (type)
8189 return type;
8190
8191 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8192 type = lookup_methodptr_type (to_type);
8193 else
8194 type = lookup_memberptr_type (to_type, domain);
8195
8196 return set_die_type (die, type, cu);
8197 }
8198
8199 /* Extract all information from a DW_TAG_reference_type DIE and add to
8200 the user defined type vector. */
8201
8202 static struct type *
8203 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8204 {
8205 struct comp_unit_head *cu_header = &cu->header;
8206 struct type *type, *target_type;
8207 struct attribute *attr;
8208
8209 target_type = die_type (die, cu);
8210
8211 /* The die_type call above may have already set the type for this DIE. */
8212 type = get_die_type (die, cu);
8213 if (type)
8214 return type;
8215
8216 type = lookup_reference_type (target_type);
8217 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8218 if (attr)
8219 {
8220 TYPE_LENGTH (type) = DW_UNSND (attr);
8221 }
8222 else
8223 {
8224 TYPE_LENGTH (type) = cu_header->addr_size;
8225 }
8226 return set_die_type (die, type, cu);
8227 }
8228
8229 static struct type *
8230 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8231 {
8232 struct type *base_type, *cv_type;
8233
8234 base_type = die_type (die, cu);
8235
8236 /* The die_type call above may have already set the type for this DIE. */
8237 cv_type = get_die_type (die, cu);
8238 if (cv_type)
8239 return cv_type;
8240
8241 /* In case the const qualifier is applied to an array type, the element type
8242 is so qualified, not the array type (section 6.7.3 of C99). */
8243 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8244 {
8245 struct type *el_type, *inner_array;
8246
8247 base_type = copy_type (base_type);
8248 inner_array = base_type;
8249
8250 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8251 {
8252 TYPE_TARGET_TYPE (inner_array) =
8253 copy_type (TYPE_TARGET_TYPE (inner_array));
8254 inner_array = TYPE_TARGET_TYPE (inner_array);
8255 }
8256
8257 el_type = TYPE_TARGET_TYPE (inner_array);
8258 TYPE_TARGET_TYPE (inner_array) =
8259 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8260
8261 return set_die_type (die, base_type, cu);
8262 }
8263
8264 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8265 return set_die_type (die, cv_type, cu);
8266 }
8267
8268 static struct type *
8269 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8270 {
8271 struct type *base_type, *cv_type;
8272
8273 base_type = die_type (die, cu);
8274
8275 /* The die_type call above may have already set the type for this DIE. */
8276 cv_type = get_die_type (die, cu);
8277 if (cv_type)
8278 return cv_type;
8279
8280 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8281 return set_die_type (die, cv_type, cu);
8282 }
8283
8284 /* Extract all information from a DW_TAG_string_type DIE and add to
8285 the user defined type vector. It isn't really a user defined type,
8286 but it behaves like one, with other DIE's using an AT_user_def_type
8287 attribute to reference it. */
8288
8289 static struct type *
8290 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8291 {
8292 struct objfile *objfile = cu->objfile;
8293 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8294 struct type *type, *range_type, *index_type, *char_type;
8295 struct attribute *attr;
8296 unsigned int length;
8297
8298 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8299 if (attr)
8300 {
8301 length = DW_UNSND (attr);
8302 }
8303 else
8304 {
8305 /* Check for the DW_AT_byte_size attribute. */
8306 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8307 if (attr)
8308 {
8309 length = DW_UNSND (attr);
8310 }
8311 else
8312 {
8313 length = 1;
8314 }
8315 }
8316
8317 index_type = objfile_type (objfile)->builtin_int;
8318 range_type = create_range_type (NULL, index_type, 1, length);
8319 char_type = language_string_char_type (cu->language_defn, gdbarch);
8320 type = create_string_type (NULL, char_type, range_type);
8321
8322 return set_die_type (die, type, cu);
8323 }
8324
8325 /* Handle DIES due to C code like:
8326
8327 struct foo
8328 {
8329 int (*funcp)(int a, long l);
8330 int b;
8331 };
8332
8333 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8334
8335 static struct type *
8336 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8337 {
8338 struct type *type; /* Type that this function returns. */
8339 struct type *ftype; /* Function that returns above type. */
8340 struct attribute *attr;
8341
8342 type = die_type (die, cu);
8343
8344 /* The die_type call above may have already set the type for this DIE. */
8345 ftype = get_die_type (die, cu);
8346 if (ftype)
8347 return ftype;
8348
8349 ftype = lookup_function_type (type);
8350
8351 /* All functions in C++, Pascal and Java have prototypes. */
8352 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8353 if ((attr && (DW_UNSND (attr) != 0))
8354 || cu->language == language_cplus
8355 || cu->language == language_java
8356 || cu->language == language_pascal)
8357 TYPE_PROTOTYPED (ftype) = 1;
8358 else if (producer_is_realview (cu->producer))
8359 /* RealView does not emit DW_AT_prototyped. We can not
8360 distinguish prototyped and unprototyped functions; default to
8361 prototyped, since that is more common in modern code (and
8362 RealView warns about unprototyped functions). */
8363 TYPE_PROTOTYPED (ftype) = 1;
8364
8365 /* Store the calling convention in the type if it's available in
8366 the subroutine die. Otherwise set the calling convention to
8367 the default value DW_CC_normal. */
8368 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8369 if (attr)
8370 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8371 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8372 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8373 else
8374 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8375
8376 /* We need to add the subroutine type to the die immediately so
8377 we don't infinitely recurse when dealing with parameters
8378 declared as the same subroutine type. */
8379 set_die_type (die, ftype, cu);
8380
8381 if (die->child != NULL)
8382 {
8383 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8384 struct die_info *child_die;
8385 int nparams, iparams;
8386
8387 /* Count the number of parameters.
8388 FIXME: GDB currently ignores vararg functions, but knows about
8389 vararg member functions. */
8390 nparams = 0;
8391 child_die = die->child;
8392 while (child_die && child_die->tag)
8393 {
8394 if (child_die->tag == DW_TAG_formal_parameter)
8395 nparams++;
8396 else if (child_die->tag == DW_TAG_unspecified_parameters)
8397 TYPE_VARARGS (ftype) = 1;
8398 child_die = sibling_die (child_die);
8399 }
8400
8401 /* Allocate storage for parameters and fill them in. */
8402 TYPE_NFIELDS (ftype) = nparams;
8403 TYPE_FIELDS (ftype) = (struct field *)
8404 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8405
8406 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8407 even if we error out during the parameters reading below. */
8408 for (iparams = 0; iparams < nparams; iparams++)
8409 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8410
8411 iparams = 0;
8412 child_die = die->child;
8413 while (child_die && child_die->tag)
8414 {
8415 if (child_die->tag == DW_TAG_formal_parameter)
8416 {
8417 struct type *arg_type;
8418
8419 /* DWARF version 2 has no clean way to discern C++
8420 static and non-static member functions. G++ helps
8421 GDB by marking the first parameter for non-static
8422 member functions (which is the this pointer) as
8423 artificial. We pass this information to
8424 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8425
8426 DWARF version 3 added DW_AT_object_pointer, which GCC
8427 4.5 does not yet generate. */
8428 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8429 if (attr)
8430 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8431 else
8432 {
8433 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8434
8435 /* GCC/43521: In java, the formal parameter
8436 "this" is sometimes not marked with DW_AT_artificial. */
8437 if (cu->language == language_java)
8438 {
8439 const char *name = dwarf2_name (child_die, cu);
8440
8441 if (name && !strcmp (name, "this"))
8442 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8443 }
8444 }
8445 arg_type = die_type (child_die, cu);
8446
8447 /* RealView does not mark THIS as const, which the testsuite
8448 expects. GCC marks THIS as const in method definitions,
8449 but not in the class specifications (GCC PR 43053). */
8450 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8451 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8452 {
8453 int is_this = 0;
8454 struct dwarf2_cu *arg_cu = cu;
8455 const char *name = dwarf2_name (child_die, cu);
8456
8457 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8458 if (attr)
8459 {
8460 /* If the compiler emits this, use it. */
8461 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8462 is_this = 1;
8463 }
8464 else if (name && strcmp (name, "this") == 0)
8465 /* Function definitions will have the argument names. */
8466 is_this = 1;
8467 else if (name == NULL && iparams == 0)
8468 /* Declarations may not have the names, so like
8469 elsewhere in GDB, assume an artificial first
8470 argument is "this". */
8471 is_this = 1;
8472
8473 if (is_this)
8474 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8475 arg_type, 0);
8476 }
8477
8478 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8479 iparams++;
8480 }
8481 child_die = sibling_die (child_die);
8482 }
8483 }
8484
8485 return ftype;
8486 }
8487
8488 static struct type *
8489 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8490 {
8491 struct objfile *objfile = cu->objfile;
8492 const char *name = NULL;
8493 struct type *this_type;
8494
8495 name = dwarf2_full_name (NULL, die, cu);
8496 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8497 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8498 TYPE_NAME (this_type) = (char *) name;
8499 set_die_type (die, this_type, cu);
8500 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8501 return this_type;
8502 }
8503
8504 /* Find a representation of a given base type and install
8505 it in the TYPE field of the die. */
8506
8507 static struct type *
8508 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8509 {
8510 struct objfile *objfile = cu->objfile;
8511 struct type *type;
8512 struct attribute *attr;
8513 int encoding = 0, size = 0;
8514 char *name;
8515 enum type_code code = TYPE_CODE_INT;
8516 int type_flags = 0;
8517 struct type *target_type = NULL;
8518
8519 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8520 if (attr)
8521 {
8522 encoding = DW_UNSND (attr);
8523 }
8524 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8525 if (attr)
8526 {
8527 size = DW_UNSND (attr);
8528 }
8529 name = dwarf2_name (die, cu);
8530 if (!name)
8531 {
8532 complaint (&symfile_complaints,
8533 _("DW_AT_name missing from DW_TAG_base_type"));
8534 }
8535
8536 switch (encoding)
8537 {
8538 case DW_ATE_address:
8539 /* Turn DW_ATE_address into a void * pointer. */
8540 code = TYPE_CODE_PTR;
8541 type_flags |= TYPE_FLAG_UNSIGNED;
8542 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8543 break;
8544 case DW_ATE_boolean:
8545 code = TYPE_CODE_BOOL;
8546 type_flags |= TYPE_FLAG_UNSIGNED;
8547 break;
8548 case DW_ATE_complex_float:
8549 code = TYPE_CODE_COMPLEX;
8550 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8551 break;
8552 case DW_ATE_decimal_float:
8553 code = TYPE_CODE_DECFLOAT;
8554 break;
8555 case DW_ATE_float:
8556 code = TYPE_CODE_FLT;
8557 break;
8558 case DW_ATE_signed:
8559 break;
8560 case DW_ATE_unsigned:
8561 type_flags |= TYPE_FLAG_UNSIGNED;
8562 if (cu->language == language_fortran
8563 && name
8564 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8565 code = TYPE_CODE_CHAR;
8566 break;
8567 case DW_ATE_signed_char:
8568 if (cu->language == language_ada || cu->language == language_m2
8569 || cu->language == language_pascal
8570 || cu->language == language_fortran)
8571 code = TYPE_CODE_CHAR;
8572 break;
8573 case DW_ATE_unsigned_char:
8574 if (cu->language == language_ada || cu->language == language_m2
8575 || cu->language == language_pascal
8576 || cu->language == language_fortran)
8577 code = TYPE_CODE_CHAR;
8578 type_flags |= TYPE_FLAG_UNSIGNED;
8579 break;
8580 case DW_ATE_UTF:
8581 /* We just treat this as an integer and then recognize the
8582 type by name elsewhere. */
8583 break;
8584
8585 default:
8586 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8587 dwarf_type_encoding_name (encoding));
8588 break;
8589 }
8590
8591 type = init_type (code, size, type_flags, NULL, objfile);
8592 TYPE_NAME (type) = name;
8593 TYPE_TARGET_TYPE (type) = target_type;
8594
8595 if (name && strcmp (name, "char") == 0)
8596 TYPE_NOSIGN (type) = 1;
8597
8598 return set_die_type (die, type, cu);
8599 }
8600
8601 /* Read the given DW_AT_subrange DIE. */
8602
8603 static struct type *
8604 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8605 {
8606 struct type *base_type;
8607 struct type *range_type;
8608 struct attribute *attr;
8609 LONGEST low = 0;
8610 LONGEST high = -1;
8611 char *name;
8612 LONGEST negative_mask;
8613
8614 base_type = die_type (die, cu);
8615 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8616 check_typedef (base_type);
8617
8618 /* The die_type call above may have already set the type for this DIE. */
8619 range_type = get_die_type (die, cu);
8620 if (range_type)
8621 return range_type;
8622
8623 if (cu->language == language_fortran)
8624 {
8625 /* FORTRAN implies a lower bound of 1, if not given. */
8626 low = 1;
8627 }
8628
8629 /* FIXME: For variable sized arrays either of these could be
8630 a variable rather than a constant value. We'll allow it,
8631 but we don't know how to handle it. */
8632 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8633 if (attr)
8634 low = dwarf2_get_attr_constant_value (attr, 0);
8635
8636 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8637 if (attr)
8638 {
8639 if (attr_form_is_block (attr) || is_ref_attr (attr))
8640 {
8641 /* GCC encodes arrays with unspecified or dynamic length
8642 with a DW_FORM_block1 attribute or a reference attribute.
8643 FIXME: GDB does not yet know how to handle dynamic
8644 arrays properly, treat them as arrays with unspecified
8645 length for now.
8646
8647 FIXME: jimb/2003-09-22: GDB does not really know
8648 how to handle arrays of unspecified length
8649 either; we just represent them as zero-length
8650 arrays. Choose an appropriate upper bound given
8651 the lower bound we've computed above. */
8652 high = low - 1;
8653 }
8654 else
8655 high = dwarf2_get_attr_constant_value (attr, 1);
8656 }
8657 else
8658 {
8659 attr = dwarf2_attr (die, DW_AT_count, cu);
8660 if (attr)
8661 {
8662 int count = dwarf2_get_attr_constant_value (attr, 1);
8663 high = low + count - 1;
8664 }
8665 else
8666 {
8667 /* Unspecified array length. */
8668 high = low - 1;
8669 }
8670 }
8671
8672 /* Dwarf-2 specifications explicitly allows to create subrange types
8673 without specifying a base type.
8674 In that case, the base type must be set to the type of
8675 the lower bound, upper bound or count, in that order, if any of these
8676 three attributes references an object that has a type.
8677 If no base type is found, the Dwarf-2 specifications say that
8678 a signed integer type of size equal to the size of an address should
8679 be used.
8680 For the following C code: `extern char gdb_int [];'
8681 GCC produces an empty range DIE.
8682 FIXME: muller/2010-05-28: Possible references to object for low bound,
8683 high bound or count are not yet handled by this code. */
8684 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8685 {
8686 struct objfile *objfile = cu->objfile;
8687 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8688 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8689 struct type *int_type = objfile_type (objfile)->builtin_int;
8690
8691 /* Test "int", "long int", and "long long int" objfile types,
8692 and select the first one having a size above or equal to the
8693 architecture address size. */
8694 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8695 base_type = int_type;
8696 else
8697 {
8698 int_type = objfile_type (objfile)->builtin_long;
8699 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8700 base_type = int_type;
8701 else
8702 {
8703 int_type = objfile_type (objfile)->builtin_long_long;
8704 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8705 base_type = int_type;
8706 }
8707 }
8708 }
8709
8710 negative_mask =
8711 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8712 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8713 low |= negative_mask;
8714 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8715 high |= negative_mask;
8716
8717 range_type = create_range_type (NULL, base_type, low, high);
8718
8719 /* Mark arrays with dynamic length at least as an array of unspecified
8720 length. GDB could check the boundary but before it gets implemented at
8721 least allow accessing the array elements. */
8722 if (attr && attr_form_is_block (attr))
8723 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8724
8725 /* Ada expects an empty array on no boundary attributes. */
8726 if (attr == NULL && cu->language != language_ada)
8727 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8728
8729 name = dwarf2_name (die, cu);
8730 if (name)
8731 TYPE_NAME (range_type) = name;
8732
8733 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8734 if (attr)
8735 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8736
8737 set_die_type (die, range_type, cu);
8738
8739 /* set_die_type should be already done. */
8740 set_descriptive_type (range_type, die, cu);
8741
8742 return range_type;
8743 }
8744
8745 static struct type *
8746 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8747 {
8748 struct type *type;
8749
8750 /* For now, we only support the C meaning of an unspecified type: void. */
8751
8752 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8753 TYPE_NAME (type) = dwarf2_name (die, cu);
8754
8755 return set_die_type (die, type, cu);
8756 }
8757
8758 /* Trivial hash function for die_info: the hash value of a DIE
8759 is its offset in .debug_info for this objfile. */
8760
8761 static hashval_t
8762 die_hash (const void *item)
8763 {
8764 const struct die_info *die = item;
8765
8766 return die->offset;
8767 }
8768
8769 /* Trivial comparison function for die_info structures: two DIEs
8770 are equal if they have the same offset. */
8771
8772 static int
8773 die_eq (const void *item_lhs, const void *item_rhs)
8774 {
8775 const struct die_info *die_lhs = item_lhs;
8776 const struct die_info *die_rhs = item_rhs;
8777
8778 return die_lhs->offset == die_rhs->offset;
8779 }
8780
8781 /* Read a whole compilation unit into a linked list of dies. */
8782
8783 static struct die_info *
8784 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8785 {
8786 struct die_reader_specs reader_specs;
8787 int read_abbrevs = 0;
8788 struct cleanup *back_to = NULL;
8789 struct die_info *die;
8790
8791 if (cu->dwarf2_abbrevs == NULL)
8792 {
8793 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8794 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8795 read_abbrevs = 1;
8796 }
8797
8798 gdb_assert (cu->die_hash == NULL);
8799 cu->die_hash
8800 = htab_create_alloc_ex (cu->header.length / 12,
8801 die_hash,
8802 die_eq,
8803 NULL,
8804 &cu->comp_unit_obstack,
8805 hashtab_obstack_allocate,
8806 dummy_obstack_deallocate);
8807
8808 init_cu_die_reader (&reader_specs, cu);
8809
8810 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8811
8812 if (read_abbrevs)
8813 do_cleanups (back_to);
8814
8815 return die;
8816 }
8817
8818 /* Main entry point for reading a DIE and all children.
8819 Read the DIE and dump it if requested. */
8820
8821 static struct die_info *
8822 read_die_and_children (const struct die_reader_specs *reader,
8823 gdb_byte *info_ptr,
8824 gdb_byte **new_info_ptr,
8825 struct die_info *parent)
8826 {
8827 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8828 new_info_ptr, parent);
8829
8830 if (dwarf2_die_debug)
8831 {
8832 fprintf_unfiltered (gdb_stdlog,
8833 "\nRead die from %s of %s:\n",
8834 (reader->cu->per_cu->debug_type_section
8835 ? ".debug_types"
8836 : ".debug_info"),
8837 reader->abfd->filename);
8838 dump_die (result, dwarf2_die_debug);
8839 }
8840
8841 return result;
8842 }
8843
8844 /* Read a single die and all its descendents. Set the die's sibling
8845 field to NULL; set other fields in the die correctly, and set all
8846 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8847 location of the info_ptr after reading all of those dies. PARENT
8848 is the parent of the die in question. */
8849
8850 static struct die_info *
8851 read_die_and_children_1 (const struct die_reader_specs *reader,
8852 gdb_byte *info_ptr,
8853 gdb_byte **new_info_ptr,
8854 struct die_info *parent)
8855 {
8856 struct die_info *die;
8857 gdb_byte *cur_ptr;
8858 int has_children;
8859
8860 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8861 if (die == NULL)
8862 {
8863 *new_info_ptr = cur_ptr;
8864 return NULL;
8865 }
8866 store_in_ref_table (die, reader->cu);
8867
8868 if (has_children)
8869 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8870 else
8871 {
8872 die->child = NULL;
8873 *new_info_ptr = cur_ptr;
8874 }
8875
8876 die->sibling = NULL;
8877 die->parent = parent;
8878 return die;
8879 }
8880
8881 /* Read a die, all of its descendents, and all of its siblings; set
8882 all of the fields of all of the dies correctly. Arguments are as
8883 in read_die_and_children. */
8884
8885 static struct die_info *
8886 read_die_and_siblings (const struct die_reader_specs *reader,
8887 gdb_byte *info_ptr,
8888 gdb_byte **new_info_ptr,
8889 struct die_info *parent)
8890 {
8891 struct die_info *first_die, *last_sibling;
8892 gdb_byte *cur_ptr;
8893
8894 cur_ptr = info_ptr;
8895 first_die = last_sibling = NULL;
8896
8897 while (1)
8898 {
8899 struct die_info *die
8900 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8901
8902 if (die == NULL)
8903 {
8904 *new_info_ptr = cur_ptr;
8905 return first_die;
8906 }
8907
8908 if (!first_die)
8909 first_die = die;
8910 else
8911 last_sibling->sibling = die;
8912
8913 last_sibling = die;
8914 }
8915 }
8916
8917 /* Read the die from the .debug_info section buffer. Set DIEP to
8918 point to a newly allocated die with its information, except for its
8919 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8920 whether the die has children or not. */
8921
8922 static gdb_byte *
8923 read_full_die (const struct die_reader_specs *reader,
8924 struct die_info **diep, gdb_byte *info_ptr,
8925 int *has_children)
8926 {
8927 unsigned int abbrev_number, bytes_read, i, offset;
8928 struct abbrev_info *abbrev;
8929 struct die_info *die;
8930 struct dwarf2_cu *cu = reader->cu;
8931 bfd *abfd = reader->abfd;
8932
8933 offset = info_ptr - reader->buffer;
8934 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8935 info_ptr += bytes_read;
8936 if (!abbrev_number)
8937 {
8938 *diep = NULL;
8939 *has_children = 0;
8940 return info_ptr;
8941 }
8942
8943 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8944 if (!abbrev)
8945 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8946 abbrev_number,
8947 bfd_get_filename (abfd));
8948
8949 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8950 die->offset = offset;
8951 die->tag = abbrev->tag;
8952 die->abbrev = abbrev_number;
8953
8954 die->num_attrs = abbrev->num_attrs;
8955
8956 for (i = 0; i < abbrev->num_attrs; ++i)
8957 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8958 abfd, info_ptr, cu);
8959
8960 *diep = die;
8961 *has_children = abbrev->has_children;
8962 return info_ptr;
8963 }
8964
8965 /* In DWARF version 2, the description of the debugging information is
8966 stored in a separate .debug_abbrev section. Before we read any
8967 dies from a section we read in all abbreviations and install them
8968 in a hash table. This function also sets flags in CU describing
8969 the data found in the abbrev table. */
8970
8971 static void
8972 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8973 {
8974 struct comp_unit_head *cu_header = &cu->header;
8975 gdb_byte *abbrev_ptr;
8976 struct abbrev_info *cur_abbrev;
8977 unsigned int abbrev_number, bytes_read, abbrev_name;
8978 unsigned int abbrev_form, hash_number;
8979 struct attr_abbrev *cur_attrs;
8980 unsigned int allocated_attrs;
8981
8982 /* Initialize dwarf2 abbrevs. */
8983 obstack_init (&cu->abbrev_obstack);
8984 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8985 (ABBREV_HASH_SIZE
8986 * sizeof (struct abbrev_info *)));
8987 memset (cu->dwarf2_abbrevs, 0,
8988 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8989
8990 dwarf2_read_section (dwarf2_per_objfile->objfile,
8991 &dwarf2_per_objfile->abbrev);
8992 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8993 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8994 abbrev_ptr += bytes_read;
8995
8996 allocated_attrs = ATTR_ALLOC_CHUNK;
8997 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8998
8999 /* Loop until we reach an abbrev number of 0. */
9000 while (abbrev_number)
9001 {
9002 cur_abbrev = dwarf_alloc_abbrev (cu);
9003
9004 /* read in abbrev header */
9005 cur_abbrev->number = abbrev_number;
9006 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9007 abbrev_ptr += bytes_read;
9008 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9009 abbrev_ptr += 1;
9010
9011 if (cur_abbrev->tag == DW_TAG_namespace)
9012 cu->has_namespace_info = 1;
9013
9014 /* now read in declarations */
9015 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9016 abbrev_ptr += bytes_read;
9017 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9018 abbrev_ptr += bytes_read;
9019 while (abbrev_name)
9020 {
9021 if (cur_abbrev->num_attrs == allocated_attrs)
9022 {
9023 allocated_attrs += ATTR_ALLOC_CHUNK;
9024 cur_attrs
9025 = xrealloc (cur_attrs, (allocated_attrs
9026 * sizeof (struct attr_abbrev)));
9027 }
9028
9029 /* Record whether this compilation unit might have
9030 inter-compilation-unit references. If we don't know what form
9031 this attribute will have, then it might potentially be a
9032 DW_FORM_ref_addr, so we conservatively expect inter-CU
9033 references. */
9034
9035 if (abbrev_form == DW_FORM_ref_addr
9036 || abbrev_form == DW_FORM_indirect)
9037 cu->has_form_ref_addr = 1;
9038
9039 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9040 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
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 }
9046
9047 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9048 (cur_abbrev->num_attrs
9049 * sizeof (struct attr_abbrev)));
9050 memcpy (cur_abbrev->attrs, cur_attrs,
9051 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9052
9053 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9054 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9055 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9056
9057 /* Get next abbreviation.
9058 Under Irix6 the abbreviations for a compilation unit are not
9059 always properly terminated with an abbrev number of 0.
9060 Exit loop if we encounter an abbreviation which we have
9061 already read (which means we are about to read the abbreviations
9062 for the next compile unit) or if the end of the abbreviation
9063 table is reached. */
9064 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9065 >= dwarf2_per_objfile->abbrev.size)
9066 break;
9067 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9068 abbrev_ptr += bytes_read;
9069 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9070 break;
9071 }
9072
9073 xfree (cur_attrs);
9074 }
9075
9076 /* Release the memory used by the abbrev table for a compilation unit. */
9077
9078 static void
9079 dwarf2_free_abbrev_table (void *ptr_to_cu)
9080 {
9081 struct dwarf2_cu *cu = ptr_to_cu;
9082
9083 obstack_free (&cu->abbrev_obstack, NULL);
9084 cu->dwarf2_abbrevs = NULL;
9085 }
9086
9087 /* Lookup an abbrev_info structure in the abbrev hash table. */
9088
9089 static struct abbrev_info *
9090 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9091 {
9092 unsigned int hash_number;
9093 struct abbrev_info *abbrev;
9094
9095 hash_number = number % ABBREV_HASH_SIZE;
9096 abbrev = cu->dwarf2_abbrevs[hash_number];
9097
9098 while (abbrev)
9099 {
9100 if (abbrev->number == number)
9101 return abbrev;
9102 else
9103 abbrev = abbrev->next;
9104 }
9105 return NULL;
9106 }
9107
9108 /* Returns nonzero if TAG represents a type that we might generate a partial
9109 symbol for. */
9110
9111 static int
9112 is_type_tag_for_partial (int tag)
9113 {
9114 switch (tag)
9115 {
9116 #if 0
9117 /* Some types that would be reasonable to generate partial symbols for,
9118 that we don't at present. */
9119 case DW_TAG_array_type:
9120 case DW_TAG_file_type:
9121 case DW_TAG_ptr_to_member_type:
9122 case DW_TAG_set_type:
9123 case DW_TAG_string_type:
9124 case DW_TAG_subroutine_type:
9125 #endif
9126 case DW_TAG_base_type:
9127 case DW_TAG_class_type:
9128 case DW_TAG_interface_type:
9129 case DW_TAG_enumeration_type:
9130 case DW_TAG_structure_type:
9131 case DW_TAG_subrange_type:
9132 case DW_TAG_typedef:
9133 case DW_TAG_union_type:
9134 return 1;
9135 default:
9136 return 0;
9137 }
9138 }
9139
9140 /* Load all DIEs that are interesting for partial symbols into memory. */
9141
9142 static struct partial_die_info *
9143 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9144 int building_psymtab, struct dwarf2_cu *cu)
9145 {
9146 struct partial_die_info *part_die;
9147 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9148 struct abbrev_info *abbrev;
9149 unsigned int bytes_read;
9150 unsigned int load_all = 0;
9151
9152 int nesting_level = 1;
9153
9154 parent_die = NULL;
9155 last_die = NULL;
9156
9157 if (cu->per_cu && cu->per_cu->load_all_dies)
9158 load_all = 1;
9159
9160 cu->partial_dies
9161 = htab_create_alloc_ex (cu->header.length / 12,
9162 partial_die_hash,
9163 partial_die_eq,
9164 NULL,
9165 &cu->comp_unit_obstack,
9166 hashtab_obstack_allocate,
9167 dummy_obstack_deallocate);
9168
9169 part_die = obstack_alloc (&cu->comp_unit_obstack,
9170 sizeof (struct partial_die_info));
9171
9172 while (1)
9173 {
9174 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9175
9176 /* A NULL abbrev means the end of a series of children. */
9177 if (abbrev == NULL)
9178 {
9179 if (--nesting_level == 0)
9180 {
9181 /* PART_DIE was probably the last thing allocated on the
9182 comp_unit_obstack, so we could call obstack_free
9183 here. We don't do that because the waste is small,
9184 and will be cleaned up when we're done with this
9185 compilation unit. This way, we're also more robust
9186 against other users of the comp_unit_obstack. */
9187 return first_die;
9188 }
9189 info_ptr += bytes_read;
9190 last_die = parent_die;
9191 parent_die = parent_die->die_parent;
9192 continue;
9193 }
9194
9195 /* Check for template arguments. We never save these; if
9196 they're seen, we just mark the parent, and go on our way. */
9197 if (parent_die != NULL
9198 && cu->language == language_cplus
9199 && (abbrev->tag == DW_TAG_template_type_param
9200 || abbrev->tag == DW_TAG_template_value_param))
9201 {
9202 parent_die->has_template_arguments = 1;
9203
9204 if (!load_all)
9205 {
9206 /* We don't need a partial DIE for the template argument. */
9207 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9208 cu);
9209 continue;
9210 }
9211 }
9212
9213 /* We only recurse into subprograms looking for template arguments.
9214 Skip their other children. */
9215 if (!load_all
9216 && cu->language == language_cplus
9217 && parent_die != NULL
9218 && parent_die->tag == DW_TAG_subprogram)
9219 {
9220 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9221 continue;
9222 }
9223
9224 /* Check whether this DIE is interesting enough to save. Normally
9225 we would not be interested in members here, but there may be
9226 later variables referencing them via DW_AT_specification (for
9227 static members). */
9228 if (!load_all
9229 && !is_type_tag_for_partial (abbrev->tag)
9230 && abbrev->tag != DW_TAG_constant
9231 && abbrev->tag != DW_TAG_enumerator
9232 && abbrev->tag != DW_TAG_subprogram
9233 && abbrev->tag != DW_TAG_lexical_block
9234 && abbrev->tag != DW_TAG_variable
9235 && abbrev->tag != DW_TAG_namespace
9236 && abbrev->tag != DW_TAG_module
9237 && abbrev->tag != DW_TAG_member)
9238 {
9239 /* Otherwise we skip to the next sibling, if any. */
9240 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9241 continue;
9242 }
9243
9244 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9245 buffer, info_ptr, cu);
9246
9247 /* This two-pass algorithm for processing partial symbols has a
9248 high cost in cache pressure. Thus, handle some simple cases
9249 here which cover the majority of C partial symbols. DIEs
9250 which neither have specification tags in them, nor could have
9251 specification tags elsewhere pointing at them, can simply be
9252 processed and discarded.
9253
9254 This segment is also optional; scan_partial_symbols and
9255 add_partial_symbol will handle these DIEs if we chain
9256 them in normally. When compilers which do not emit large
9257 quantities of duplicate debug information are more common,
9258 this code can probably be removed. */
9259
9260 /* Any complete simple types at the top level (pretty much all
9261 of them, for a language without namespaces), can be processed
9262 directly. */
9263 if (parent_die == NULL
9264 && part_die->has_specification == 0
9265 && part_die->is_declaration == 0
9266 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9267 || part_die->tag == DW_TAG_base_type
9268 || part_die->tag == DW_TAG_subrange_type))
9269 {
9270 if (building_psymtab && part_die->name != NULL)
9271 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9272 VAR_DOMAIN, LOC_TYPEDEF,
9273 &cu->objfile->static_psymbols,
9274 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9275 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9276 continue;
9277 }
9278
9279 /* The exception for DW_TAG_typedef with has_children above is
9280 a workaround of GCC PR debug/47510. In the case of this complaint
9281 type_name_no_tag_or_error will error on such types later.
9282
9283 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9284 it could not find the child DIEs referenced later, this is checked
9285 above. In correct DWARF DW_TAG_typedef should have no children. */
9286
9287 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9288 complaint (&symfile_complaints,
9289 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9290 "- DIE at 0x%x [in module %s]"),
9291 part_die->offset, cu->objfile->name);
9292
9293 /* If we're at the second level, and we're an enumerator, and
9294 our parent has no specification (meaning possibly lives in a
9295 namespace elsewhere), then we can add the partial symbol now
9296 instead of queueing it. */
9297 if (part_die->tag == DW_TAG_enumerator
9298 && parent_die != NULL
9299 && parent_die->die_parent == NULL
9300 && parent_die->tag == DW_TAG_enumeration_type
9301 && parent_die->has_specification == 0)
9302 {
9303 if (part_die->name == NULL)
9304 complaint (&symfile_complaints,
9305 _("malformed enumerator DIE ignored"));
9306 else if (building_psymtab)
9307 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9308 VAR_DOMAIN, LOC_CONST,
9309 (cu->language == language_cplus
9310 || cu->language == language_java)
9311 ? &cu->objfile->global_psymbols
9312 : &cu->objfile->static_psymbols,
9313 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9314
9315 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9316 continue;
9317 }
9318
9319 /* We'll save this DIE so link it in. */
9320 part_die->die_parent = parent_die;
9321 part_die->die_sibling = NULL;
9322 part_die->die_child = NULL;
9323
9324 if (last_die && last_die == parent_die)
9325 last_die->die_child = part_die;
9326 else if (last_die)
9327 last_die->die_sibling = part_die;
9328
9329 last_die = part_die;
9330
9331 if (first_die == NULL)
9332 first_die = part_die;
9333
9334 /* Maybe add the DIE to the hash table. Not all DIEs that we
9335 find interesting need to be in the hash table, because we
9336 also have the parent/sibling/child chains; only those that we
9337 might refer to by offset later during partial symbol reading.
9338
9339 For now this means things that might have be the target of a
9340 DW_AT_specification, DW_AT_abstract_origin, or
9341 DW_AT_extension. DW_AT_extension will refer only to
9342 namespaces; DW_AT_abstract_origin refers to functions (and
9343 many things under the function DIE, but we do not recurse
9344 into function DIEs during partial symbol reading) and
9345 possibly variables as well; DW_AT_specification refers to
9346 declarations. Declarations ought to have the DW_AT_declaration
9347 flag. It happens that GCC forgets to put it in sometimes, but
9348 only for functions, not for types.
9349
9350 Adding more things than necessary to the hash table is harmless
9351 except for the performance cost. Adding too few will result in
9352 wasted time in find_partial_die, when we reread the compilation
9353 unit with load_all_dies set. */
9354
9355 if (load_all
9356 || abbrev->tag == DW_TAG_constant
9357 || abbrev->tag == DW_TAG_subprogram
9358 || abbrev->tag == DW_TAG_variable
9359 || abbrev->tag == DW_TAG_namespace
9360 || part_die->is_declaration)
9361 {
9362 void **slot;
9363
9364 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9365 part_die->offset, INSERT);
9366 *slot = part_die;
9367 }
9368
9369 part_die = obstack_alloc (&cu->comp_unit_obstack,
9370 sizeof (struct partial_die_info));
9371
9372 /* For some DIEs we want to follow their children (if any). For C
9373 we have no reason to follow the children of structures; for other
9374 languages we have to, so that we can get at method physnames
9375 to infer fully qualified class names, for DW_AT_specification,
9376 and for C++ template arguments. For C++, we also look one level
9377 inside functions to find template arguments (if the name of the
9378 function does not already contain the template arguments).
9379
9380 For Ada, we need to scan the children of subprograms and lexical
9381 blocks as well because Ada allows the definition of nested
9382 entities that could be interesting for the debugger, such as
9383 nested subprograms for instance. */
9384 if (last_die->has_children
9385 && (load_all
9386 || last_die->tag == DW_TAG_namespace
9387 || last_die->tag == DW_TAG_module
9388 || last_die->tag == DW_TAG_enumeration_type
9389 || (cu->language == language_cplus
9390 && last_die->tag == DW_TAG_subprogram
9391 && (last_die->name == NULL
9392 || strchr (last_die->name, '<') == NULL))
9393 || (cu->language != language_c
9394 && (last_die->tag == DW_TAG_class_type
9395 || last_die->tag == DW_TAG_interface_type
9396 || last_die->tag == DW_TAG_structure_type
9397 || last_die->tag == DW_TAG_union_type))
9398 || (cu->language == language_ada
9399 && (last_die->tag == DW_TAG_subprogram
9400 || last_die->tag == DW_TAG_lexical_block))))
9401 {
9402 nesting_level++;
9403 parent_die = last_die;
9404 continue;
9405 }
9406
9407 /* Otherwise we skip to the next sibling, if any. */
9408 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9409
9410 /* Back to the top, do it again. */
9411 }
9412 }
9413
9414 /* Read a minimal amount of information into the minimal die structure. */
9415
9416 static gdb_byte *
9417 read_partial_die (struct partial_die_info *part_die,
9418 struct abbrev_info *abbrev,
9419 unsigned int abbrev_len, bfd *abfd,
9420 gdb_byte *buffer, gdb_byte *info_ptr,
9421 struct dwarf2_cu *cu)
9422 {
9423 unsigned int i;
9424 struct attribute attr;
9425 int has_low_pc_attr = 0;
9426 int has_high_pc_attr = 0;
9427
9428 memset (part_die, 0, sizeof (struct partial_die_info));
9429
9430 part_die->offset = info_ptr - buffer;
9431
9432 info_ptr += abbrev_len;
9433
9434 if (abbrev == NULL)
9435 return info_ptr;
9436
9437 part_die->tag = abbrev->tag;
9438 part_die->has_children = abbrev->has_children;
9439
9440 for (i = 0; i < abbrev->num_attrs; ++i)
9441 {
9442 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9443
9444 /* Store the data if it is of an attribute we want to keep in a
9445 partial symbol table. */
9446 switch (attr.name)
9447 {
9448 case DW_AT_name:
9449 switch (part_die->tag)
9450 {
9451 case DW_TAG_compile_unit:
9452 case DW_TAG_type_unit:
9453 /* Compilation units have a DW_AT_name that is a filename, not
9454 a source language identifier. */
9455 case DW_TAG_enumeration_type:
9456 case DW_TAG_enumerator:
9457 /* These tags always have simple identifiers already; no need
9458 to canonicalize them. */
9459 part_die->name = DW_STRING (&attr);
9460 break;
9461 default:
9462 part_die->name
9463 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9464 &cu->objfile->objfile_obstack);
9465 break;
9466 }
9467 break;
9468 case DW_AT_linkage_name:
9469 case DW_AT_MIPS_linkage_name:
9470 /* Note that both forms of linkage name might appear. We
9471 assume they will be the same, and we only store the last
9472 one we see. */
9473 if (cu->language == language_ada)
9474 part_die->name = DW_STRING (&attr);
9475 part_die->linkage_name = DW_STRING (&attr);
9476 break;
9477 case DW_AT_low_pc:
9478 has_low_pc_attr = 1;
9479 part_die->lowpc = DW_ADDR (&attr);
9480 break;
9481 case DW_AT_high_pc:
9482 has_high_pc_attr = 1;
9483 part_die->highpc = DW_ADDR (&attr);
9484 break;
9485 case DW_AT_location:
9486 /* Support the .debug_loc offsets. */
9487 if (attr_form_is_block (&attr))
9488 {
9489 part_die->locdesc = DW_BLOCK (&attr);
9490 }
9491 else if (attr_form_is_section_offset (&attr))
9492 {
9493 dwarf2_complex_location_expr_complaint ();
9494 }
9495 else
9496 {
9497 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9498 "partial symbol information");
9499 }
9500 break;
9501 case DW_AT_external:
9502 part_die->is_external = DW_UNSND (&attr);
9503 break;
9504 case DW_AT_declaration:
9505 part_die->is_declaration = DW_UNSND (&attr);
9506 break;
9507 case DW_AT_type:
9508 part_die->has_type = 1;
9509 break;
9510 case DW_AT_abstract_origin:
9511 case DW_AT_specification:
9512 case DW_AT_extension:
9513 part_die->has_specification = 1;
9514 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9515 break;
9516 case DW_AT_sibling:
9517 /* Ignore absolute siblings, they might point outside of
9518 the current compile unit. */
9519 if (attr.form == DW_FORM_ref_addr)
9520 complaint (&symfile_complaints,
9521 _("ignoring absolute DW_AT_sibling"));
9522 else
9523 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9524 break;
9525 case DW_AT_byte_size:
9526 part_die->has_byte_size = 1;
9527 break;
9528 case DW_AT_calling_convention:
9529 /* DWARF doesn't provide a way to identify a program's source-level
9530 entry point. DW_AT_calling_convention attributes are only meant
9531 to describe functions' calling conventions.
9532
9533 However, because it's a necessary piece of information in
9534 Fortran, and because DW_CC_program is the only piece of debugging
9535 information whose definition refers to a 'main program' at all,
9536 several compilers have begun marking Fortran main programs with
9537 DW_CC_program --- even when those functions use the standard
9538 calling conventions.
9539
9540 So until DWARF specifies a way to provide this information and
9541 compilers pick up the new representation, we'll support this
9542 practice. */
9543 if (DW_UNSND (&attr) == DW_CC_program
9544 && cu->language == language_fortran)
9545 {
9546 set_main_name (part_die->name);
9547
9548 /* As this DIE has a static linkage the name would be difficult
9549 to look up later. */
9550 language_of_main = language_fortran;
9551 }
9552 break;
9553 default:
9554 break;
9555 }
9556 }
9557
9558 if (has_low_pc_attr && has_high_pc_attr)
9559 {
9560 /* When using the GNU linker, .gnu.linkonce. sections are used to
9561 eliminate duplicate copies of functions and vtables and such.
9562 The linker will arbitrarily choose one and discard the others.
9563 The AT_*_pc values for such functions refer to local labels in
9564 these sections. If the section from that file was discarded, the
9565 labels are not in the output, so the relocs get a value of 0.
9566 If this is a discarded function, mark the pc bounds as invalid,
9567 so that GDB will ignore it. */
9568 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9569 {
9570 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9571
9572 complaint (&symfile_complaints,
9573 _("DW_AT_low_pc %s is zero "
9574 "for DIE at 0x%x [in module %s]"),
9575 paddress (gdbarch, part_die->lowpc),
9576 part_die->offset, cu->objfile->name);
9577 }
9578 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9579 else if (part_die->lowpc >= part_die->highpc)
9580 {
9581 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9582
9583 complaint (&symfile_complaints,
9584 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9585 "for DIE at 0x%x [in module %s]"),
9586 paddress (gdbarch, part_die->lowpc),
9587 paddress (gdbarch, part_die->highpc),
9588 part_die->offset, cu->objfile->name);
9589 }
9590 else
9591 part_die->has_pc_info = 1;
9592 }
9593
9594 return info_ptr;
9595 }
9596
9597 /* Find a cached partial DIE at OFFSET in CU. */
9598
9599 static struct partial_die_info *
9600 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9601 {
9602 struct partial_die_info *lookup_die = NULL;
9603 struct partial_die_info part_die;
9604
9605 part_die.offset = offset;
9606 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9607
9608 return lookup_die;
9609 }
9610
9611 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9612 except in the case of .debug_types DIEs which do not reference
9613 outside their CU (they do however referencing other types via
9614 DW_FORM_ref_sig8). */
9615
9616 static struct partial_die_info *
9617 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9618 {
9619 struct dwarf2_per_cu_data *per_cu = NULL;
9620 struct partial_die_info *pd = NULL;
9621
9622 if (cu->per_cu->debug_type_section)
9623 {
9624 pd = find_partial_die_in_comp_unit (offset, cu);
9625 if (pd != NULL)
9626 return pd;
9627 goto not_found;
9628 }
9629
9630 if (offset_in_cu_p (&cu->header, offset))
9631 {
9632 pd = find_partial_die_in_comp_unit (offset, cu);
9633 if (pd != NULL)
9634 return pd;
9635 }
9636
9637 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9638
9639 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9640 load_partial_comp_unit (per_cu, cu->objfile);
9641
9642 per_cu->cu->last_used = 0;
9643 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9644
9645 if (pd == NULL && per_cu->load_all_dies == 0)
9646 {
9647 struct cleanup *back_to;
9648 struct partial_die_info comp_unit_die;
9649 struct abbrev_info *abbrev;
9650 unsigned int bytes_read;
9651 char *info_ptr;
9652
9653 per_cu->load_all_dies = 1;
9654
9655 /* Re-read the DIEs. */
9656 back_to = make_cleanup (null_cleanup, 0);
9657 if (per_cu->cu->dwarf2_abbrevs == NULL)
9658 {
9659 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9660 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9661 }
9662 info_ptr = (dwarf2_per_objfile->info.buffer
9663 + per_cu->cu->header.offset
9664 + per_cu->cu->header.first_die_offset);
9665 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9666 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9667 per_cu->cu->objfile->obfd,
9668 dwarf2_per_objfile->info.buffer, info_ptr,
9669 per_cu->cu);
9670 if (comp_unit_die.has_children)
9671 load_partial_dies (per_cu->cu->objfile->obfd,
9672 dwarf2_per_objfile->info.buffer, info_ptr,
9673 0, per_cu->cu);
9674 do_cleanups (back_to);
9675
9676 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9677 }
9678
9679 not_found:
9680
9681 if (pd == NULL)
9682 internal_error (__FILE__, __LINE__,
9683 _("could not find partial DIE 0x%x "
9684 "in cache [from module %s]\n"),
9685 offset, bfd_get_filename (cu->objfile->obfd));
9686 return pd;
9687 }
9688
9689 /* See if we can figure out if the class lives in a namespace. We do
9690 this by looking for a member function; its demangled name will
9691 contain namespace info, if there is any. */
9692
9693 static void
9694 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9695 struct dwarf2_cu *cu)
9696 {
9697 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9698 what template types look like, because the demangler
9699 frequently doesn't give the same name as the debug info. We
9700 could fix this by only using the demangled name to get the
9701 prefix (but see comment in read_structure_type). */
9702
9703 struct partial_die_info *real_pdi;
9704 struct partial_die_info *child_pdi;
9705
9706 /* If this DIE (this DIE's specification, if any) has a parent, then
9707 we should not do this. We'll prepend the parent's fully qualified
9708 name when we create the partial symbol. */
9709
9710 real_pdi = struct_pdi;
9711 while (real_pdi->has_specification)
9712 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9713
9714 if (real_pdi->die_parent != NULL)
9715 return;
9716
9717 for (child_pdi = struct_pdi->die_child;
9718 child_pdi != NULL;
9719 child_pdi = child_pdi->die_sibling)
9720 {
9721 if (child_pdi->tag == DW_TAG_subprogram
9722 && child_pdi->linkage_name != NULL)
9723 {
9724 char *actual_class_name
9725 = language_class_name_from_physname (cu->language_defn,
9726 child_pdi->linkage_name);
9727 if (actual_class_name != NULL)
9728 {
9729 struct_pdi->name
9730 = obsavestring (actual_class_name,
9731 strlen (actual_class_name),
9732 &cu->objfile->objfile_obstack);
9733 xfree (actual_class_name);
9734 }
9735 break;
9736 }
9737 }
9738 }
9739
9740 /* Adjust PART_DIE before generating a symbol for it. This function
9741 may set the is_external flag or change the DIE's name. */
9742
9743 static void
9744 fixup_partial_die (struct partial_die_info *part_die,
9745 struct dwarf2_cu *cu)
9746 {
9747 /* Once we've fixed up a die, there's no point in doing so again.
9748 This also avoids a memory leak if we were to call
9749 guess_partial_die_structure_name multiple times. */
9750 if (part_die->fixup_called)
9751 return;
9752
9753 /* If we found a reference attribute and the DIE has no name, try
9754 to find a name in the referred to DIE. */
9755
9756 if (part_die->name == NULL && part_die->has_specification)
9757 {
9758 struct partial_die_info *spec_die;
9759
9760 spec_die = find_partial_die (part_die->spec_offset, cu);
9761
9762 fixup_partial_die (spec_die, cu);
9763
9764 if (spec_die->name)
9765 {
9766 part_die->name = spec_die->name;
9767
9768 /* Copy DW_AT_external attribute if it is set. */
9769 if (spec_die->is_external)
9770 part_die->is_external = spec_die->is_external;
9771 }
9772 }
9773
9774 /* Set default names for some unnamed DIEs. */
9775
9776 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9777 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
9778
9779 /* If there is no parent die to provide a namespace, and there are
9780 children, see if we can determine the namespace from their linkage
9781 name.
9782 NOTE: We need to do this even if cu->has_namespace_info != 0.
9783 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9784 if (cu->language == language_cplus
9785 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
9786 && part_die->die_parent == NULL
9787 && part_die->has_children
9788 && (part_die->tag == DW_TAG_class_type
9789 || part_die->tag == DW_TAG_structure_type
9790 || part_die->tag == DW_TAG_union_type))
9791 guess_partial_die_structure_name (part_die, cu);
9792
9793 /* GCC might emit a nameless struct or union that has a linkage
9794 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9795 if (part_die->name == NULL
9796 && (part_die->tag == DW_TAG_structure_type
9797 || part_die->tag == DW_TAG_union_type
9798 || part_die->tag == DW_TAG_class_type)
9799 && part_die->linkage_name != NULL)
9800 {
9801 char *demangled;
9802
9803 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9804 if (demangled)
9805 {
9806 part_die->name = obsavestring (demangled, strlen (demangled),
9807 &cu->objfile->objfile_obstack);
9808 xfree (demangled);
9809 }
9810 }
9811
9812 part_die->fixup_called = 1;
9813 }
9814
9815 /* Read an attribute value described by an attribute form. */
9816
9817 static gdb_byte *
9818 read_attribute_value (struct attribute *attr, unsigned form,
9819 bfd *abfd, gdb_byte *info_ptr,
9820 struct dwarf2_cu *cu)
9821 {
9822 struct comp_unit_head *cu_header = &cu->header;
9823 unsigned int bytes_read;
9824 struct dwarf_block *blk;
9825
9826 attr->form = form;
9827 switch (form)
9828 {
9829 case DW_FORM_ref_addr:
9830 if (cu->header.version == 2)
9831 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9832 else
9833 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9834 &cu->header, &bytes_read);
9835 info_ptr += bytes_read;
9836 break;
9837 case DW_FORM_addr:
9838 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9839 info_ptr += bytes_read;
9840 break;
9841 case DW_FORM_block2:
9842 blk = dwarf_alloc_block (cu);
9843 blk->size = read_2_bytes (abfd, info_ptr);
9844 info_ptr += 2;
9845 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9846 info_ptr += blk->size;
9847 DW_BLOCK (attr) = blk;
9848 break;
9849 case DW_FORM_block4:
9850 blk = dwarf_alloc_block (cu);
9851 blk->size = read_4_bytes (abfd, info_ptr);
9852 info_ptr += 4;
9853 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9854 info_ptr += blk->size;
9855 DW_BLOCK (attr) = blk;
9856 break;
9857 case DW_FORM_data2:
9858 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9859 info_ptr += 2;
9860 break;
9861 case DW_FORM_data4:
9862 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9863 info_ptr += 4;
9864 break;
9865 case DW_FORM_data8:
9866 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9867 info_ptr += 8;
9868 break;
9869 case DW_FORM_sec_offset:
9870 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9871 info_ptr += bytes_read;
9872 break;
9873 case DW_FORM_string:
9874 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9875 DW_STRING_IS_CANONICAL (attr) = 0;
9876 info_ptr += bytes_read;
9877 break;
9878 case DW_FORM_strp:
9879 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9880 &bytes_read);
9881 DW_STRING_IS_CANONICAL (attr) = 0;
9882 info_ptr += bytes_read;
9883 break;
9884 case DW_FORM_exprloc:
9885 case DW_FORM_block:
9886 blk = dwarf_alloc_block (cu);
9887 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9888 info_ptr += bytes_read;
9889 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9890 info_ptr += blk->size;
9891 DW_BLOCK (attr) = blk;
9892 break;
9893 case DW_FORM_block1:
9894 blk = dwarf_alloc_block (cu);
9895 blk->size = read_1_byte (abfd, info_ptr);
9896 info_ptr += 1;
9897 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9898 info_ptr += blk->size;
9899 DW_BLOCK (attr) = blk;
9900 break;
9901 case DW_FORM_data1:
9902 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9903 info_ptr += 1;
9904 break;
9905 case DW_FORM_flag:
9906 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9907 info_ptr += 1;
9908 break;
9909 case DW_FORM_flag_present:
9910 DW_UNSND (attr) = 1;
9911 break;
9912 case DW_FORM_sdata:
9913 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9914 info_ptr += bytes_read;
9915 break;
9916 case DW_FORM_udata:
9917 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9918 info_ptr += bytes_read;
9919 break;
9920 case DW_FORM_ref1:
9921 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9922 info_ptr += 1;
9923 break;
9924 case DW_FORM_ref2:
9925 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9926 info_ptr += 2;
9927 break;
9928 case DW_FORM_ref4:
9929 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9930 info_ptr += 4;
9931 break;
9932 case DW_FORM_ref8:
9933 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9934 info_ptr += 8;
9935 break;
9936 case DW_FORM_ref_sig8:
9937 /* Convert the signature to something we can record in DW_UNSND
9938 for later lookup.
9939 NOTE: This is NULL if the type wasn't found. */
9940 DW_SIGNATURED_TYPE (attr) =
9941 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9942 info_ptr += 8;
9943 break;
9944 case DW_FORM_ref_udata:
9945 DW_ADDR (attr) = (cu->header.offset
9946 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9947 info_ptr += bytes_read;
9948 break;
9949 case DW_FORM_indirect:
9950 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9951 info_ptr += bytes_read;
9952 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9953 break;
9954 default:
9955 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9956 dwarf_form_name (form),
9957 bfd_get_filename (abfd));
9958 }
9959
9960 /* We have seen instances where the compiler tried to emit a byte
9961 size attribute of -1 which ended up being encoded as an unsigned
9962 0xffffffff. Although 0xffffffff is technically a valid size value,
9963 an object of this size seems pretty unlikely so we can relatively
9964 safely treat these cases as if the size attribute was invalid and
9965 treat them as zero by default. */
9966 if (attr->name == DW_AT_byte_size
9967 && form == DW_FORM_data4
9968 && DW_UNSND (attr) >= 0xffffffff)
9969 {
9970 complaint
9971 (&symfile_complaints,
9972 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9973 hex_string (DW_UNSND (attr)));
9974 DW_UNSND (attr) = 0;
9975 }
9976
9977 return info_ptr;
9978 }
9979
9980 /* Read an attribute described by an abbreviated attribute. */
9981
9982 static gdb_byte *
9983 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9984 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9985 {
9986 attr->name = abbrev->name;
9987 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9988 }
9989
9990 /* Read dwarf information from a buffer. */
9991
9992 static unsigned int
9993 read_1_byte (bfd *abfd, gdb_byte *buf)
9994 {
9995 return bfd_get_8 (abfd, buf);
9996 }
9997
9998 static int
9999 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10000 {
10001 return bfd_get_signed_8 (abfd, buf);
10002 }
10003
10004 static unsigned int
10005 read_2_bytes (bfd *abfd, gdb_byte *buf)
10006 {
10007 return bfd_get_16 (abfd, buf);
10008 }
10009
10010 static int
10011 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10012 {
10013 return bfd_get_signed_16 (abfd, buf);
10014 }
10015
10016 static unsigned int
10017 read_4_bytes (bfd *abfd, gdb_byte *buf)
10018 {
10019 return bfd_get_32 (abfd, buf);
10020 }
10021
10022 static int
10023 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10024 {
10025 return bfd_get_signed_32 (abfd, buf);
10026 }
10027
10028 static ULONGEST
10029 read_8_bytes (bfd *abfd, gdb_byte *buf)
10030 {
10031 return bfd_get_64 (abfd, buf);
10032 }
10033
10034 static CORE_ADDR
10035 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10036 unsigned int *bytes_read)
10037 {
10038 struct comp_unit_head *cu_header = &cu->header;
10039 CORE_ADDR retval = 0;
10040
10041 if (cu_header->signed_addr_p)
10042 {
10043 switch (cu_header->addr_size)
10044 {
10045 case 2:
10046 retval = bfd_get_signed_16 (abfd, buf);
10047 break;
10048 case 4:
10049 retval = bfd_get_signed_32 (abfd, buf);
10050 break;
10051 case 8:
10052 retval = bfd_get_signed_64 (abfd, buf);
10053 break;
10054 default:
10055 internal_error (__FILE__, __LINE__,
10056 _("read_address: bad switch, signed [in module %s]"),
10057 bfd_get_filename (abfd));
10058 }
10059 }
10060 else
10061 {
10062 switch (cu_header->addr_size)
10063 {
10064 case 2:
10065 retval = bfd_get_16 (abfd, buf);
10066 break;
10067 case 4:
10068 retval = bfd_get_32 (abfd, buf);
10069 break;
10070 case 8:
10071 retval = bfd_get_64 (abfd, buf);
10072 break;
10073 default:
10074 internal_error (__FILE__, __LINE__,
10075 _("read_address: bad switch, "
10076 "unsigned [in module %s]"),
10077 bfd_get_filename (abfd));
10078 }
10079 }
10080
10081 *bytes_read = cu_header->addr_size;
10082 return retval;
10083 }
10084
10085 /* Read the initial length from a section. The (draft) DWARF 3
10086 specification allows the initial length to take up either 4 bytes
10087 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10088 bytes describe the length and all offsets will be 8 bytes in length
10089 instead of 4.
10090
10091 An older, non-standard 64-bit format is also handled by this
10092 function. The older format in question stores the initial length
10093 as an 8-byte quantity without an escape value. Lengths greater
10094 than 2^32 aren't very common which means that the initial 4 bytes
10095 is almost always zero. Since a length value of zero doesn't make
10096 sense for the 32-bit format, this initial zero can be considered to
10097 be an escape value which indicates the presence of the older 64-bit
10098 format. As written, the code can't detect (old format) lengths
10099 greater than 4GB. If it becomes necessary to handle lengths
10100 somewhat larger than 4GB, we could allow other small values (such
10101 as the non-sensical values of 1, 2, and 3) to also be used as
10102 escape values indicating the presence of the old format.
10103
10104 The value returned via bytes_read should be used to increment the
10105 relevant pointer after calling read_initial_length().
10106
10107 [ Note: read_initial_length() and read_offset() are based on the
10108 document entitled "DWARF Debugging Information Format", revision
10109 3, draft 8, dated November 19, 2001. This document was obtained
10110 from:
10111
10112 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10113
10114 This document is only a draft and is subject to change. (So beware.)
10115
10116 Details regarding the older, non-standard 64-bit format were
10117 determined empirically by examining 64-bit ELF files produced by
10118 the SGI toolchain on an IRIX 6.5 machine.
10119
10120 - Kevin, July 16, 2002
10121 ] */
10122
10123 static LONGEST
10124 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10125 {
10126 LONGEST length = bfd_get_32 (abfd, buf);
10127
10128 if (length == 0xffffffff)
10129 {
10130 length = bfd_get_64 (abfd, buf + 4);
10131 *bytes_read = 12;
10132 }
10133 else if (length == 0)
10134 {
10135 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10136 length = bfd_get_64 (abfd, buf);
10137 *bytes_read = 8;
10138 }
10139 else
10140 {
10141 *bytes_read = 4;
10142 }
10143
10144 return length;
10145 }
10146
10147 /* Cover function for read_initial_length.
10148 Returns the length of the object at BUF, and stores the size of the
10149 initial length in *BYTES_READ and stores the size that offsets will be in
10150 *OFFSET_SIZE.
10151 If the initial length size is not equivalent to that specified in
10152 CU_HEADER then issue a complaint.
10153 This is useful when reading non-comp-unit headers. */
10154
10155 static LONGEST
10156 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10157 const struct comp_unit_head *cu_header,
10158 unsigned int *bytes_read,
10159 unsigned int *offset_size)
10160 {
10161 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10162
10163 gdb_assert (cu_header->initial_length_size == 4
10164 || cu_header->initial_length_size == 8
10165 || cu_header->initial_length_size == 12);
10166
10167 if (cu_header->initial_length_size != *bytes_read)
10168 complaint (&symfile_complaints,
10169 _("intermixed 32-bit and 64-bit DWARF sections"));
10170
10171 *offset_size = (*bytes_read == 4) ? 4 : 8;
10172 return length;
10173 }
10174
10175 /* Read an offset from the data stream. The size of the offset is
10176 given by cu_header->offset_size. */
10177
10178 static LONGEST
10179 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10180 unsigned int *bytes_read)
10181 {
10182 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10183
10184 *bytes_read = cu_header->offset_size;
10185 return offset;
10186 }
10187
10188 /* Read an offset from the data stream. */
10189
10190 static LONGEST
10191 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10192 {
10193 LONGEST retval = 0;
10194
10195 switch (offset_size)
10196 {
10197 case 4:
10198 retval = bfd_get_32 (abfd, buf);
10199 break;
10200 case 8:
10201 retval = bfd_get_64 (abfd, buf);
10202 break;
10203 default:
10204 internal_error (__FILE__, __LINE__,
10205 _("read_offset_1: bad switch [in module %s]"),
10206 bfd_get_filename (abfd));
10207 }
10208
10209 return retval;
10210 }
10211
10212 static gdb_byte *
10213 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10214 {
10215 /* If the size of a host char is 8 bits, we can return a pointer
10216 to the buffer, otherwise we have to copy the data to a buffer
10217 allocated on the temporary obstack. */
10218 gdb_assert (HOST_CHAR_BIT == 8);
10219 return buf;
10220 }
10221
10222 static char *
10223 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10224 {
10225 /* If the size of a host char is 8 bits, we can return a pointer
10226 to the string, otherwise we have to copy the string to a buffer
10227 allocated on the temporary obstack. */
10228 gdb_assert (HOST_CHAR_BIT == 8);
10229 if (*buf == '\0')
10230 {
10231 *bytes_read_ptr = 1;
10232 return NULL;
10233 }
10234 *bytes_read_ptr = strlen ((char *) buf) + 1;
10235 return (char *) buf;
10236 }
10237
10238 static char *
10239 read_indirect_string (bfd *abfd, gdb_byte *buf,
10240 const struct comp_unit_head *cu_header,
10241 unsigned int *bytes_read_ptr)
10242 {
10243 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10244
10245 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10246 if (dwarf2_per_objfile->str.buffer == NULL)
10247 {
10248 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10249 bfd_get_filename (abfd));
10250 return NULL;
10251 }
10252 if (str_offset >= dwarf2_per_objfile->str.size)
10253 {
10254 error (_("DW_FORM_strp pointing outside of "
10255 ".debug_str section [in module %s]"),
10256 bfd_get_filename (abfd));
10257 return NULL;
10258 }
10259 gdb_assert (HOST_CHAR_BIT == 8);
10260 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10261 return NULL;
10262 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10263 }
10264
10265 static unsigned long
10266 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10267 {
10268 unsigned long result;
10269 unsigned int num_read;
10270 int i, shift;
10271 unsigned char byte;
10272
10273 result = 0;
10274 shift = 0;
10275 num_read = 0;
10276 i = 0;
10277 while (1)
10278 {
10279 byte = bfd_get_8 (abfd, buf);
10280 buf++;
10281 num_read++;
10282 result |= ((unsigned long)(byte & 127) << shift);
10283 if ((byte & 128) == 0)
10284 {
10285 break;
10286 }
10287 shift += 7;
10288 }
10289 *bytes_read_ptr = num_read;
10290 return result;
10291 }
10292
10293 static long
10294 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10295 {
10296 long result;
10297 int i, shift, num_read;
10298 unsigned char byte;
10299
10300 result = 0;
10301 shift = 0;
10302 num_read = 0;
10303 i = 0;
10304 while (1)
10305 {
10306 byte = bfd_get_8 (abfd, buf);
10307 buf++;
10308 num_read++;
10309 result |= ((long)(byte & 127) << shift);
10310 shift += 7;
10311 if ((byte & 128) == 0)
10312 {
10313 break;
10314 }
10315 }
10316 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10317 result |= -(((long)1) << shift);
10318 *bytes_read_ptr = num_read;
10319 return result;
10320 }
10321
10322 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10323
10324 static gdb_byte *
10325 skip_leb128 (bfd *abfd, gdb_byte *buf)
10326 {
10327 int byte;
10328
10329 while (1)
10330 {
10331 byte = bfd_get_8 (abfd, buf);
10332 buf++;
10333 if ((byte & 128) == 0)
10334 return buf;
10335 }
10336 }
10337
10338 static void
10339 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10340 {
10341 switch (lang)
10342 {
10343 case DW_LANG_C89:
10344 case DW_LANG_C99:
10345 case DW_LANG_C:
10346 cu->language = language_c;
10347 break;
10348 case DW_LANG_C_plus_plus:
10349 cu->language = language_cplus;
10350 break;
10351 case DW_LANG_D:
10352 cu->language = language_d;
10353 break;
10354 case DW_LANG_Fortran77:
10355 case DW_LANG_Fortran90:
10356 case DW_LANG_Fortran95:
10357 cu->language = language_fortran;
10358 break;
10359 case DW_LANG_Mips_Assembler:
10360 cu->language = language_asm;
10361 break;
10362 case DW_LANG_Java:
10363 cu->language = language_java;
10364 break;
10365 case DW_LANG_Ada83:
10366 case DW_LANG_Ada95:
10367 cu->language = language_ada;
10368 break;
10369 case DW_LANG_Modula2:
10370 cu->language = language_m2;
10371 break;
10372 case DW_LANG_Pascal83:
10373 cu->language = language_pascal;
10374 break;
10375 case DW_LANG_ObjC:
10376 cu->language = language_objc;
10377 break;
10378 case DW_LANG_Cobol74:
10379 case DW_LANG_Cobol85:
10380 default:
10381 cu->language = language_minimal;
10382 break;
10383 }
10384 cu->language_defn = language_def (cu->language);
10385 }
10386
10387 /* Return the named attribute or NULL if not there. */
10388
10389 static struct attribute *
10390 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10391 {
10392 unsigned int i;
10393 struct attribute *spec = NULL;
10394
10395 for (i = 0; i < die->num_attrs; ++i)
10396 {
10397 if (die->attrs[i].name == name)
10398 return &die->attrs[i];
10399 if (die->attrs[i].name == DW_AT_specification
10400 || die->attrs[i].name == DW_AT_abstract_origin)
10401 spec = &die->attrs[i];
10402 }
10403
10404 if (spec)
10405 {
10406 die = follow_die_ref (die, spec, &cu);
10407 return dwarf2_attr (die, name, cu);
10408 }
10409
10410 return NULL;
10411 }
10412
10413 /* Return the named attribute or NULL if not there,
10414 but do not follow DW_AT_specification, etc.
10415 This is for use in contexts where we're reading .debug_types dies.
10416 Following DW_AT_specification, DW_AT_abstract_origin will take us
10417 back up the chain, and we want to go down. */
10418
10419 static struct attribute *
10420 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10421 struct dwarf2_cu *cu)
10422 {
10423 unsigned int i;
10424
10425 for (i = 0; i < die->num_attrs; ++i)
10426 if (die->attrs[i].name == name)
10427 return &die->attrs[i];
10428
10429 return NULL;
10430 }
10431
10432 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10433 and holds a non-zero value. This function should only be used for
10434 DW_FORM_flag or DW_FORM_flag_present attributes. */
10435
10436 static int
10437 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10438 {
10439 struct attribute *attr = dwarf2_attr (die, name, cu);
10440
10441 return (attr && DW_UNSND (attr));
10442 }
10443
10444 static int
10445 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10446 {
10447 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10448 which value is non-zero. However, we have to be careful with
10449 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10450 (via dwarf2_flag_true_p) follows this attribute. So we may
10451 end up accidently finding a declaration attribute that belongs
10452 to a different DIE referenced by the specification attribute,
10453 even though the given DIE does not have a declaration attribute. */
10454 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10455 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10456 }
10457
10458 /* Return the die giving the specification for DIE, if there is
10459 one. *SPEC_CU is the CU containing DIE on input, and the CU
10460 containing the return value on output. If there is no
10461 specification, but there is an abstract origin, that is
10462 returned. */
10463
10464 static struct die_info *
10465 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10466 {
10467 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10468 *spec_cu);
10469
10470 if (spec_attr == NULL)
10471 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10472
10473 if (spec_attr == NULL)
10474 return NULL;
10475 else
10476 return follow_die_ref (die, spec_attr, spec_cu);
10477 }
10478
10479 /* Free the line_header structure *LH, and any arrays and strings it
10480 refers to.
10481 NOTE: This is also used as a "cleanup" function. */
10482
10483 static void
10484 free_line_header (struct line_header *lh)
10485 {
10486 if (lh->standard_opcode_lengths)
10487 xfree (lh->standard_opcode_lengths);
10488
10489 /* Remember that all the lh->file_names[i].name pointers are
10490 pointers into debug_line_buffer, and don't need to be freed. */
10491 if (lh->file_names)
10492 xfree (lh->file_names);
10493
10494 /* Similarly for the include directory names. */
10495 if (lh->include_dirs)
10496 xfree (lh->include_dirs);
10497
10498 xfree (lh);
10499 }
10500
10501 /* Add an entry to LH's include directory table. */
10502
10503 static void
10504 add_include_dir (struct line_header *lh, char *include_dir)
10505 {
10506 /* Grow the array if necessary. */
10507 if (lh->include_dirs_size == 0)
10508 {
10509 lh->include_dirs_size = 1; /* for testing */
10510 lh->include_dirs = xmalloc (lh->include_dirs_size
10511 * sizeof (*lh->include_dirs));
10512 }
10513 else if (lh->num_include_dirs >= lh->include_dirs_size)
10514 {
10515 lh->include_dirs_size *= 2;
10516 lh->include_dirs = xrealloc (lh->include_dirs,
10517 (lh->include_dirs_size
10518 * sizeof (*lh->include_dirs)));
10519 }
10520
10521 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10522 }
10523
10524 /* Add an entry to LH's file name table. */
10525
10526 static void
10527 add_file_name (struct line_header *lh,
10528 char *name,
10529 unsigned int dir_index,
10530 unsigned int mod_time,
10531 unsigned int length)
10532 {
10533 struct file_entry *fe;
10534
10535 /* Grow the array if necessary. */
10536 if (lh->file_names_size == 0)
10537 {
10538 lh->file_names_size = 1; /* for testing */
10539 lh->file_names = xmalloc (lh->file_names_size
10540 * sizeof (*lh->file_names));
10541 }
10542 else if (lh->num_file_names >= lh->file_names_size)
10543 {
10544 lh->file_names_size *= 2;
10545 lh->file_names = xrealloc (lh->file_names,
10546 (lh->file_names_size
10547 * sizeof (*lh->file_names)));
10548 }
10549
10550 fe = &lh->file_names[lh->num_file_names++];
10551 fe->name = name;
10552 fe->dir_index = dir_index;
10553 fe->mod_time = mod_time;
10554 fe->length = length;
10555 fe->included_p = 0;
10556 fe->symtab = NULL;
10557 }
10558
10559 /* Read the statement program header starting at OFFSET in
10560 .debug_line, according to the endianness of ABFD. Return a pointer
10561 to a struct line_header, allocated using xmalloc.
10562
10563 NOTE: the strings in the include directory and file name tables of
10564 the returned object point into debug_line_buffer, and must not be
10565 freed. */
10566
10567 static struct line_header *
10568 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10569 struct dwarf2_cu *cu)
10570 {
10571 struct cleanup *back_to;
10572 struct line_header *lh;
10573 gdb_byte *line_ptr;
10574 unsigned int bytes_read, offset_size;
10575 int i;
10576 char *cur_dir, *cur_file;
10577
10578 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10579 if (dwarf2_per_objfile->line.buffer == NULL)
10580 {
10581 complaint (&symfile_complaints, _("missing .debug_line section"));
10582 return 0;
10583 }
10584
10585 /* Make sure that at least there's room for the total_length field.
10586 That could be 12 bytes long, but we're just going to fudge that. */
10587 if (offset + 4 >= dwarf2_per_objfile->line.size)
10588 {
10589 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10590 return 0;
10591 }
10592
10593 lh = xmalloc (sizeof (*lh));
10594 memset (lh, 0, sizeof (*lh));
10595 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10596 (void *) lh);
10597
10598 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10599
10600 /* Read in the header. */
10601 lh->total_length =
10602 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10603 &bytes_read, &offset_size);
10604 line_ptr += bytes_read;
10605 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10606 + dwarf2_per_objfile->line.size))
10607 {
10608 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10609 return 0;
10610 }
10611 lh->statement_program_end = line_ptr + lh->total_length;
10612 lh->version = read_2_bytes (abfd, line_ptr);
10613 line_ptr += 2;
10614 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10615 line_ptr += offset_size;
10616 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10617 line_ptr += 1;
10618 if (lh->version >= 4)
10619 {
10620 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10621 line_ptr += 1;
10622 }
10623 else
10624 lh->maximum_ops_per_instruction = 1;
10625
10626 if (lh->maximum_ops_per_instruction == 0)
10627 {
10628 lh->maximum_ops_per_instruction = 1;
10629 complaint (&symfile_complaints,
10630 _("invalid maximum_ops_per_instruction "
10631 "in `.debug_line' section"));
10632 }
10633
10634 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10635 line_ptr += 1;
10636 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10637 line_ptr += 1;
10638 lh->line_range = read_1_byte (abfd, line_ptr);
10639 line_ptr += 1;
10640 lh->opcode_base = read_1_byte (abfd, line_ptr);
10641 line_ptr += 1;
10642 lh->standard_opcode_lengths
10643 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10644
10645 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10646 for (i = 1; i < lh->opcode_base; ++i)
10647 {
10648 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10649 line_ptr += 1;
10650 }
10651
10652 /* Read directory table. */
10653 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10654 {
10655 line_ptr += bytes_read;
10656 add_include_dir (lh, cur_dir);
10657 }
10658 line_ptr += bytes_read;
10659
10660 /* Read file name table. */
10661 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10662 {
10663 unsigned int dir_index, mod_time, length;
10664
10665 line_ptr += bytes_read;
10666 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10667 line_ptr += bytes_read;
10668 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10669 line_ptr += bytes_read;
10670 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10671 line_ptr += bytes_read;
10672
10673 add_file_name (lh, cur_file, dir_index, mod_time, length);
10674 }
10675 line_ptr += bytes_read;
10676 lh->statement_program_start = line_ptr;
10677
10678 if (line_ptr > (dwarf2_per_objfile->line.buffer
10679 + dwarf2_per_objfile->line.size))
10680 complaint (&symfile_complaints,
10681 _("line number info header doesn't "
10682 "fit in `.debug_line' section"));
10683
10684 discard_cleanups (back_to);
10685 return lh;
10686 }
10687
10688 /* This function exists to work around a bug in certain compilers
10689 (particularly GCC 2.95), in which the first line number marker of a
10690 function does not show up until after the prologue, right before
10691 the second line number marker. This function shifts ADDRESS down
10692 to the beginning of the function if necessary, and is called on
10693 addresses passed to record_line. */
10694
10695 static CORE_ADDR
10696 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10697 {
10698 struct function_range *fn;
10699
10700 /* Find the function_range containing address. */
10701 if (!cu->first_fn)
10702 return address;
10703
10704 if (!cu->cached_fn)
10705 cu->cached_fn = cu->first_fn;
10706
10707 fn = cu->cached_fn;
10708 while (fn)
10709 if (fn->lowpc <= address && fn->highpc > address)
10710 goto found;
10711 else
10712 fn = fn->next;
10713
10714 fn = cu->first_fn;
10715 while (fn && fn != cu->cached_fn)
10716 if (fn->lowpc <= address && fn->highpc > address)
10717 goto found;
10718 else
10719 fn = fn->next;
10720
10721 return address;
10722
10723 found:
10724 if (fn->seen_line)
10725 return address;
10726 if (address != fn->lowpc)
10727 complaint (&symfile_complaints,
10728 _("misplaced first line number at 0x%lx for '%s'"),
10729 (unsigned long) address, fn->name);
10730 fn->seen_line = 1;
10731 return fn->lowpc;
10732 }
10733
10734 /* Subroutine of dwarf_decode_lines to simplify it.
10735 Return the file name of the psymtab for included file FILE_INDEX
10736 in line header LH of PST.
10737 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10738 If space for the result is malloc'd, it will be freed by a cleanup.
10739 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10740
10741 static char *
10742 psymtab_include_file_name (const struct line_header *lh, int file_index,
10743 const struct partial_symtab *pst,
10744 const char *comp_dir)
10745 {
10746 const struct file_entry fe = lh->file_names [file_index];
10747 char *include_name = fe.name;
10748 char *include_name_to_compare = include_name;
10749 char *dir_name = NULL;
10750 const char *pst_filename;
10751 char *copied_name = NULL;
10752 int file_is_pst;
10753
10754 if (fe.dir_index)
10755 dir_name = lh->include_dirs[fe.dir_index - 1];
10756
10757 if (!IS_ABSOLUTE_PATH (include_name)
10758 && (dir_name != NULL || comp_dir != NULL))
10759 {
10760 /* Avoid creating a duplicate psymtab for PST.
10761 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10762 Before we do the comparison, however, we need to account
10763 for DIR_NAME and COMP_DIR.
10764 First prepend dir_name (if non-NULL). If we still don't
10765 have an absolute path prepend comp_dir (if non-NULL).
10766 However, the directory we record in the include-file's
10767 psymtab does not contain COMP_DIR (to match the
10768 corresponding symtab(s)).
10769
10770 Example:
10771
10772 bash$ cd /tmp
10773 bash$ gcc -g ./hello.c
10774 include_name = "hello.c"
10775 dir_name = "."
10776 DW_AT_comp_dir = comp_dir = "/tmp"
10777 DW_AT_name = "./hello.c" */
10778
10779 if (dir_name != NULL)
10780 {
10781 include_name = concat (dir_name, SLASH_STRING,
10782 include_name, (char *)NULL);
10783 include_name_to_compare = include_name;
10784 make_cleanup (xfree, include_name);
10785 }
10786 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10787 {
10788 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10789 include_name, (char *)NULL);
10790 }
10791 }
10792
10793 pst_filename = pst->filename;
10794 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10795 {
10796 copied_name = concat (pst->dirname, SLASH_STRING,
10797 pst_filename, (char *)NULL);
10798 pst_filename = copied_name;
10799 }
10800
10801 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10802
10803 if (include_name_to_compare != include_name)
10804 xfree (include_name_to_compare);
10805 if (copied_name != NULL)
10806 xfree (copied_name);
10807
10808 if (file_is_pst)
10809 return NULL;
10810 return include_name;
10811 }
10812
10813 /* Ignore this record_line request. */
10814
10815 static void
10816 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10817 {
10818 return;
10819 }
10820
10821 /* Decode the Line Number Program (LNP) for the given line_header
10822 structure and CU. The actual information extracted and the type
10823 of structures created from the LNP depends on the value of PST.
10824
10825 1. If PST is NULL, then this procedure uses the data from the program
10826 to create all necessary symbol tables, and their linetables.
10827
10828 2. If PST is not NULL, this procedure reads the program to determine
10829 the list of files included by the unit represented by PST, and
10830 builds all the associated partial symbol tables.
10831
10832 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10833 It is used for relative paths in the line table.
10834 NOTE: When processing partial symtabs (pst != NULL),
10835 comp_dir == pst->dirname.
10836
10837 NOTE: It is important that psymtabs have the same file name (via strcmp)
10838 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10839 symtab we don't use it in the name of the psymtabs we create.
10840 E.g. expand_line_sal requires this when finding psymtabs to expand.
10841 A good testcase for this is mb-inline.exp. */
10842
10843 static void
10844 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10845 struct dwarf2_cu *cu, struct partial_symtab *pst)
10846 {
10847 gdb_byte *line_ptr, *extended_end;
10848 gdb_byte *line_end;
10849 unsigned int bytes_read, extended_len;
10850 unsigned char op_code, extended_op, adj_opcode;
10851 CORE_ADDR baseaddr;
10852 struct objfile *objfile = cu->objfile;
10853 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10854 const int decode_for_pst_p = (pst != NULL);
10855 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10856 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10857 = record_line;
10858
10859 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10860
10861 line_ptr = lh->statement_program_start;
10862 line_end = lh->statement_program_end;
10863
10864 /* Read the statement sequences until there's nothing left. */
10865 while (line_ptr < line_end)
10866 {
10867 /* state machine registers */
10868 CORE_ADDR address = 0;
10869 unsigned int file = 1;
10870 unsigned int line = 1;
10871 unsigned int column = 0;
10872 int is_stmt = lh->default_is_stmt;
10873 int basic_block = 0;
10874 int end_sequence = 0;
10875 CORE_ADDR addr;
10876 unsigned char op_index = 0;
10877
10878 if (!decode_for_pst_p && lh->num_file_names >= file)
10879 {
10880 /* Start a subfile for the current file of the state machine. */
10881 /* lh->include_dirs and lh->file_names are 0-based, but the
10882 directory and file name numbers in the statement program
10883 are 1-based. */
10884 struct file_entry *fe = &lh->file_names[file - 1];
10885 char *dir = NULL;
10886
10887 if (fe->dir_index)
10888 dir = lh->include_dirs[fe->dir_index - 1];
10889
10890 dwarf2_start_subfile (fe->name, dir, comp_dir);
10891 }
10892
10893 /* Decode the table. */
10894 while (!end_sequence)
10895 {
10896 op_code = read_1_byte (abfd, line_ptr);
10897 line_ptr += 1;
10898 if (line_ptr > line_end)
10899 {
10900 dwarf2_debug_line_missing_end_sequence_complaint ();
10901 break;
10902 }
10903
10904 if (op_code >= lh->opcode_base)
10905 {
10906 /* Special operand. */
10907 adj_opcode = op_code - lh->opcode_base;
10908 address += (((op_index + (adj_opcode / lh->line_range))
10909 / lh->maximum_ops_per_instruction)
10910 * lh->minimum_instruction_length);
10911 op_index = ((op_index + (adj_opcode / lh->line_range))
10912 % lh->maximum_ops_per_instruction);
10913 line += lh->line_base + (adj_opcode % lh->line_range);
10914 if (lh->num_file_names < file || file == 0)
10915 dwarf2_debug_line_missing_file_complaint ();
10916 /* For now we ignore lines not starting on an
10917 instruction boundary. */
10918 else if (op_index == 0)
10919 {
10920 lh->file_names[file - 1].included_p = 1;
10921 if (!decode_for_pst_p && is_stmt)
10922 {
10923 if (last_subfile != current_subfile)
10924 {
10925 addr = gdbarch_addr_bits_remove (gdbarch, address);
10926 if (last_subfile)
10927 (*p_record_line) (last_subfile, 0, addr);
10928 last_subfile = current_subfile;
10929 }
10930 /* Append row to matrix using current values. */
10931 addr = check_cu_functions (address, cu);
10932 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10933 (*p_record_line) (current_subfile, line, addr);
10934 }
10935 }
10936 basic_block = 0;
10937 }
10938 else switch (op_code)
10939 {
10940 case DW_LNS_extended_op:
10941 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10942 &bytes_read);
10943 line_ptr += bytes_read;
10944 extended_end = line_ptr + extended_len;
10945 extended_op = read_1_byte (abfd, line_ptr);
10946 line_ptr += 1;
10947 switch (extended_op)
10948 {
10949 case DW_LNE_end_sequence:
10950 p_record_line = record_line;
10951 end_sequence = 1;
10952 break;
10953 case DW_LNE_set_address:
10954 address = read_address (abfd, line_ptr, cu, &bytes_read);
10955
10956 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10957 {
10958 /* This line table is for a function which has been
10959 GCd by the linker. Ignore it. PR gdb/12528 */
10960
10961 long line_offset
10962 = line_ptr - dwarf2_per_objfile->line.buffer;
10963
10964 complaint (&symfile_complaints,
10965 _(".debug_line address at offset 0x%lx is 0 "
10966 "[in module %s]"),
10967 line_offset, cu->objfile->name);
10968 p_record_line = noop_record_line;
10969 }
10970
10971 op_index = 0;
10972 line_ptr += bytes_read;
10973 address += baseaddr;
10974 break;
10975 case DW_LNE_define_file:
10976 {
10977 char *cur_file;
10978 unsigned int dir_index, mod_time, length;
10979
10980 cur_file = read_direct_string (abfd, line_ptr,
10981 &bytes_read);
10982 line_ptr += bytes_read;
10983 dir_index =
10984 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10985 line_ptr += bytes_read;
10986 mod_time =
10987 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10988 line_ptr += bytes_read;
10989 length =
10990 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10991 line_ptr += bytes_read;
10992 add_file_name (lh, cur_file, dir_index, mod_time, length);
10993 }
10994 break;
10995 case DW_LNE_set_discriminator:
10996 /* The discriminator is not interesting to the debugger;
10997 just ignore it. */
10998 line_ptr = extended_end;
10999 break;
11000 default:
11001 complaint (&symfile_complaints,
11002 _("mangled .debug_line section"));
11003 return;
11004 }
11005 /* Make sure that we parsed the extended op correctly. If e.g.
11006 we expected a different address size than the producer used,
11007 we may have read the wrong number of bytes. */
11008 if (line_ptr != extended_end)
11009 {
11010 complaint (&symfile_complaints,
11011 _("mangled .debug_line section"));
11012 return;
11013 }
11014 break;
11015 case DW_LNS_copy:
11016 if (lh->num_file_names < file || file == 0)
11017 dwarf2_debug_line_missing_file_complaint ();
11018 else
11019 {
11020 lh->file_names[file - 1].included_p = 1;
11021 if (!decode_for_pst_p && is_stmt)
11022 {
11023 if (last_subfile != current_subfile)
11024 {
11025 addr = gdbarch_addr_bits_remove (gdbarch, address);
11026 if (last_subfile)
11027 (*p_record_line) (last_subfile, 0, addr);
11028 last_subfile = current_subfile;
11029 }
11030 addr = check_cu_functions (address, cu);
11031 addr = gdbarch_addr_bits_remove (gdbarch, addr);
11032 (*p_record_line) (current_subfile, line, addr);
11033 }
11034 }
11035 basic_block = 0;
11036 break;
11037 case DW_LNS_advance_pc:
11038 {
11039 CORE_ADDR adjust
11040 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11041
11042 address += (((op_index + adjust)
11043 / lh->maximum_ops_per_instruction)
11044 * lh->minimum_instruction_length);
11045 op_index = ((op_index + adjust)
11046 % lh->maximum_ops_per_instruction);
11047 line_ptr += bytes_read;
11048 }
11049 break;
11050 case DW_LNS_advance_line:
11051 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11052 line_ptr += bytes_read;
11053 break;
11054 case DW_LNS_set_file:
11055 {
11056 /* The arrays lh->include_dirs and lh->file_names are
11057 0-based, but the directory and file name numbers in
11058 the statement program are 1-based. */
11059 struct file_entry *fe;
11060 char *dir = NULL;
11061
11062 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11063 line_ptr += bytes_read;
11064 if (lh->num_file_names < file || file == 0)
11065 dwarf2_debug_line_missing_file_complaint ();
11066 else
11067 {
11068 fe = &lh->file_names[file - 1];
11069 if (fe->dir_index)
11070 dir = lh->include_dirs[fe->dir_index - 1];
11071 if (!decode_for_pst_p)
11072 {
11073 last_subfile = current_subfile;
11074 dwarf2_start_subfile (fe->name, dir, comp_dir);
11075 }
11076 }
11077 }
11078 break;
11079 case DW_LNS_set_column:
11080 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11081 line_ptr += bytes_read;
11082 break;
11083 case DW_LNS_negate_stmt:
11084 is_stmt = (!is_stmt);
11085 break;
11086 case DW_LNS_set_basic_block:
11087 basic_block = 1;
11088 break;
11089 /* Add to the address register of the state machine the
11090 address increment value corresponding to special opcode
11091 255. I.e., this value is scaled by the minimum
11092 instruction length since special opcode 255 would have
11093 scaled the increment. */
11094 case DW_LNS_const_add_pc:
11095 {
11096 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11097
11098 address += (((op_index + adjust)
11099 / lh->maximum_ops_per_instruction)
11100 * lh->minimum_instruction_length);
11101 op_index = ((op_index + adjust)
11102 % lh->maximum_ops_per_instruction);
11103 }
11104 break;
11105 case DW_LNS_fixed_advance_pc:
11106 address += read_2_bytes (abfd, line_ptr);
11107 op_index = 0;
11108 line_ptr += 2;
11109 break;
11110 default:
11111 {
11112 /* Unknown standard opcode, ignore it. */
11113 int i;
11114
11115 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11116 {
11117 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11118 line_ptr += bytes_read;
11119 }
11120 }
11121 }
11122 }
11123 if (lh->num_file_names < file || file == 0)
11124 dwarf2_debug_line_missing_file_complaint ();
11125 else
11126 {
11127 lh->file_names[file - 1].included_p = 1;
11128 if (!decode_for_pst_p)
11129 {
11130 addr = gdbarch_addr_bits_remove (gdbarch, address);
11131 (*p_record_line) (current_subfile, 0, addr);
11132 }
11133 }
11134 }
11135
11136 if (decode_for_pst_p)
11137 {
11138 int file_index;
11139
11140 /* Now that we're done scanning the Line Header Program, we can
11141 create the psymtab of each included file. */
11142 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11143 if (lh->file_names[file_index].included_p == 1)
11144 {
11145 char *include_name =
11146 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11147 if (include_name != NULL)
11148 dwarf2_create_include_psymtab (include_name, pst, objfile);
11149 }
11150 }
11151 else
11152 {
11153 /* Make sure a symtab is created for every file, even files
11154 which contain only variables (i.e. no code with associated
11155 line numbers). */
11156
11157 int i;
11158 struct file_entry *fe;
11159
11160 for (i = 0; i < lh->num_file_names; i++)
11161 {
11162 char *dir = NULL;
11163
11164 fe = &lh->file_names[i];
11165 if (fe->dir_index)
11166 dir = lh->include_dirs[fe->dir_index - 1];
11167 dwarf2_start_subfile (fe->name, dir, comp_dir);
11168
11169 /* Skip the main file; we don't need it, and it must be
11170 allocated last, so that it will show up before the
11171 non-primary symtabs in the objfile's symtab list. */
11172 if (current_subfile == first_subfile)
11173 continue;
11174
11175 if (current_subfile->symtab == NULL)
11176 current_subfile->symtab = allocate_symtab (current_subfile->name,
11177 cu->objfile);
11178 fe->symtab = current_subfile->symtab;
11179 }
11180 }
11181 }
11182
11183 /* Start a subfile for DWARF. FILENAME is the name of the file and
11184 DIRNAME the name of the source directory which contains FILENAME
11185 or NULL if not known. COMP_DIR is the compilation directory for the
11186 linetable's compilation unit or NULL if not known.
11187 This routine tries to keep line numbers from identical absolute and
11188 relative file names in a common subfile.
11189
11190 Using the `list' example from the GDB testsuite, which resides in
11191 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11192 of /srcdir/list0.c yields the following debugging information for list0.c:
11193
11194 DW_AT_name: /srcdir/list0.c
11195 DW_AT_comp_dir: /compdir
11196 files.files[0].name: list0.h
11197 files.files[0].dir: /srcdir
11198 files.files[1].name: list0.c
11199 files.files[1].dir: /srcdir
11200
11201 The line number information for list0.c has to end up in a single
11202 subfile, so that `break /srcdir/list0.c:1' works as expected.
11203 start_subfile will ensure that this happens provided that we pass the
11204 concatenation of files.files[1].dir and files.files[1].name as the
11205 subfile's name. */
11206
11207 static void
11208 dwarf2_start_subfile (char *filename, const char *dirname,
11209 const char *comp_dir)
11210 {
11211 char *fullname;
11212
11213 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11214 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11215 second argument to start_subfile. To be consistent, we do the
11216 same here. In order not to lose the line information directory,
11217 we concatenate it to the filename when it makes sense.
11218 Note that the Dwarf3 standard says (speaking of filenames in line
11219 information): ``The directory index is ignored for file names
11220 that represent full path names''. Thus ignoring dirname in the
11221 `else' branch below isn't an issue. */
11222
11223 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11224 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11225 else
11226 fullname = filename;
11227
11228 start_subfile (fullname, comp_dir);
11229
11230 if (fullname != filename)
11231 xfree (fullname);
11232 }
11233
11234 static void
11235 var_decode_location (struct attribute *attr, struct symbol *sym,
11236 struct dwarf2_cu *cu)
11237 {
11238 struct objfile *objfile = cu->objfile;
11239 struct comp_unit_head *cu_header = &cu->header;
11240
11241 /* NOTE drow/2003-01-30: There used to be a comment and some special
11242 code here to turn a symbol with DW_AT_external and a
11243 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11244 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11245 with some versions of binutils) where shared libraries could have
11246 relocations against symbols in their debug information - the
11247 minimal symbol would have the right address, but the debug info
11248 would not. It's no longer necessary, because we will explicitly
11249 apply relocations when we read in the debug information now. */
11250
11251 /* A DW_AT_location attribute with no contents indicates that a
11252 variable has been optimized away. */
11253 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11254 {
11255 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11256 return;
11257 }
11258
11259 /* Handle one degenerate form of location expression specially, to
11260 preserve GDB's previous behavior when section offsets are
11261 specified. If this is just a DW_OP_addr then mark this symbol
11262 as LOC_STATIC. */
11263
11264 if (attr_form_is_block (attr)
11265 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11266 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11267 {
11268 unsigned int dummy;
11269
11270 SYMBOL_VALUE_ADDRESS (sym) =
11271 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11272 SYMBOL_CLASS (sym) = LOC_STATIC;
11273 fixup_symbol_section (sym, objfile);
11274 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11275 SYMBOL_SECTION (sym));
11276 return;
11277 }
11278
11279 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11280 expression evaluator, and use LOC_COMPUTED only when necessary
11281 (i.e. when the value of a register or memory location is
11282 referenced, or a thread-local block, etc.). Then again, it might
11283 not be worthwhile. I'm assuming that it isn't unless performance
11284 or memory numbers show me otherwise. */
11285
11286 dwarf2_symbol_mark_computed (attr, sym, cu);
11287 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11288
11289 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11290 cu->has_loclist = 1;
11291 }
11292
11293 /* Given a pointer to a DWARF information entry, figure out if we need
11294 to make a symbol table entry for it, and if so, create a new entry
11295 and return a pointer to it.
11296 If TYPE is NULL, determine symbol type from the die, otherwise
11297 used the passed type.
11298 If SPACE is not NULL, use it to hold the new symbol. If it is
11299 NULL, allocate a new symbol on the objfile's obstack. */
11300
11301 static struct symbol *
11302 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11303 struct symbol *space)
11304 {
11305 struct objfile *objfile = cu->objfile;
11306 struct symbol *sym = NULL;
11307 char *name;
11308 struct attribute *attr = NULL;
11309 struct attribute *attr2 = NULL;
11310 CORE_ADDR baseaddr;
11311 struct pending **list_to_add = NULL;
11312
11313 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11314
11315 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11316
11317 name = dwarf2_name (die, cu);
11318 if (name)
11319 {
11320 const char *linkagename;
11321 int suppress_add = 0;
11322
11323 if (space)
11324 sym = space;
11325 else
11326 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11327 OBJSTAT (objfile, n_syms++);
11328
11329 /* Cache this symbol's name and the name's demangled form (if any). */
11330 SYMBOL_SET_LANGUAGE (sym, cu->language);
11331 linkagename = dwarf2_physname (name, die, cu);
11332 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11333
11334 /* Fortran does not have mangling standard and the mangling does differ
11335 between gfortran, iFort etc. */
11336 if (cu->language == language_fortran
11337 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11338 symbol_set_demangled_name (&(sym->ginfo),
11339 (char *) dwarf2_full_name (name, die, cu),
11340 NULL);
11341
11342 /* Default assumptions.
11343 Use the passed type or decode it from the die. */
11344 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11345 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11346 if (type != NULL)
11347 SYMBOL_TYPE (sym) = type;
11348 else
11349 SYMBOL_TYPE (sym) = die_type (die, cu);
11350 attr = dwarf2_attr (die,
11351 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11352 cu);
11353 if (attr)
11354 {
11355 SYMBOL_LINE (sym) = DW_UNSND (attr);
11356 }
11357
11358 attr = dwarf2_attr (die,
11359 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11360 cu);
11361 if (attr)
11362 {
11363 int file_index = DW_UNSND (attr);
11364
11365 if (cu->line_header == NULL
11366 || file_index > cu->line_header->num_file_names)
11367 complaint (&symfile_complaints,
11368 _("file index out of range"));
11369 else if (file_index > 0)
11370 {
11371 struct file_entry *fe;
11372
11373 fe = &cu->line_header->file_names[file_index - 1];
11374 SYMBOL_SYMTAB (sym) = fe->symtab;
11375 }
11376 }
11377
11378 switch (die->tag)
11379 {
11380 case DW_TAG_label:
11381 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11382 if (attr)
11383 {
11384 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11385 }
11386 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11387 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11388 SYMBOL_CLASS (sym) = LOC_LABEL;
11389 add_symbol_to_list (sym, cu->list_in_scope);
11390 break;
11391 case DW_TAG_subprogram:
11392 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11393 finish_block. */
11394 SYMBOL_CLASS (sym) = LOC_BLOCK;
11395 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11396 if ((attr2 && (DW_UNSND (attr2) != 0))
11397 || cu->language == language_ada)
11398 {
11399 /* Subprograms marked external are stored as a global symbol.
11400 Ada subprograms, whether marked external or not, are always
11401 stored as a global symbol, because we want to be able to
11402 access them globally. For instance, we want to be able
11403 to break on a nested subprogram without having to
11404 specify the context. */
11405 list_to_add = &global_symbols;
11406 }
11407 else
11408 {
11409 list_to_add = cu->list_in_scope;
11410 }
11411 break;
11412 case DW_TAG_inlined_subroutine:
11413 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11414 finish_block. */
11415 SYMBOL_CLASS (sym) = LOC_BLOCK;
11416 SYMBOL_INLINED (sym) = 1;
11417 /* Do not add the symbol to any lists. It will be found via
11418 BLOCK_FUNCTION from the blockvector. */
11419 break;
11420 case DW_TAG_template_value_param:
11421 suppress_add = 1;
11422 /* Fall through. */
11423 case DW_TAG_constant:
11424 case DW_TAG_variable:
11425 case DW_TAG_member:
11426 /* Compilation with minimal debug info may result in
11427 variables with missing type entries. Change the
11428 misleading `void' type to something sensible. */
11429 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11430 SYMBOL_TYPE (sym)
11431 = objfile_type (objfile)->nodebug_data_symbol;
11432
11433 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11434 /* In the case of DW_TAG_member, we should only be called for
11435 static const members. */
11436 if (die->tag == DW_TAG_member)
11437 {
11438 /* dwarf2_add_field uses die_is_declaration,
11439 so we do the same. */
11440 gdb_assert (die_is_declaration (die, cu));
11441 gdb_assert (attr);
11442 }
11443 if (attr)
11444 {
11445 dwarf2_const_value (attr, sym, cu);
11446 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11447 if (!suppress_add)
11448 {
11449 if (attr2 && (DW_UNSND (attr2) != 0))
11450 list_to_add = &global_symbols;
11451 else
11452 list_to_add = cu->list_in_scope;
11453 }
11454 break;
11455 }
11456 attr = dwarf2_attr (die, DW_AT_location, cu);
11457 if (attr)
11458 {
11459 var_decode_location (attr, sym, cu);
11460 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11461 if (SYMBOL_CLASS (sym) == LOC_STATIC
11462 && SYMBOL_VALUE_ADDRESS (sym) == 0
11463 && !dwarf2_per_objfile->has_section_at_zero)
11464 {
11465 /* When a static variable is eliminated by the linker,
11466 the corresponding debug information is not stripped
11467 out, but the variable address is set to null;
11468 do not add such variables into symbol table. */
11469 }
11470 else if (attr2 && (DW_UNSND (attr2) != 0))
11471 {
11472 /* Workaround gfortran PR debug/40040 - it uses
11473 DW_AT_location for variables in -fPIC libraries which may
11474 get overriden by other libraries/executable and get
11475 a different address. Resolve it by the minimal symbol
11476 which may come from inferior's executable using copy
11477 relocation. Make this workaround only for gfortran as for
11478 other compilers GDB cannot guess the minimal symbol
11479 Fortran mangling kind. */
11480 if (cu->language == language_fortran && die->parent
11481 && die->parent->tag == DW_TAG_module
11482 && cu->producer
11483 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11484 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11485
11486 /* A variable with DW_AT_external is never static,
11487 but it may be block-scoped. */
11488 list_to_add = (cu->list_in_scope == &file_symbols
11489 ? &global_symbols : cu->list_in_scope);
11490 }
11491 else
11492 list_to_add = cu->list_in_scope;
11493 }
11494 else
11495 {
11496 /* We do not know the address of this symbol.
11497 If it is an external symbol and we have type information
11498 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11499 The address of the variable will then be determined from
11500 the minimal symbol table whenever the variable is
11501 referenced. */
11502 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11503 if (attr2 && (DW_UNSND (attr2) != 0)
11504 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11505 {
11506 /* A variable with DW_AT_external is never static, but it
11507 may be block-scoped. */
11508 list_to_add = (cu->list_in_scope == &file_symbols
11509 ? &global_symbols : cu->list_in_scope);
11510
11511 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11512 }
11513 else if (!die_is_declaration (die, cu))
11514 {
11515 /* Use the default LOC_OPTIMIZED_OUT class. */
11516 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11517 if (!suppress_add)
11518 list_to_add = cu->list_in_scope;
11519 }
11520 }
11521 break;
11522 case DW_TAG_formal_parameter:
11523 /* If we are inside a function, mark this as an argument. If
11524 not, we might be looking at an argument to an inlined function
11525 when we do not have enough information to show inlined frames;
11526 pretend it's a local variable in that case so that the user can
11527 still see it. */
11528 if (context_stack_depth > 0
11529 && context_stack[context_stack_depth - 1].name != NULL)
11530 SYMBOL_IS_ARGUMENT (sym) = 1;
11531 attr = dwarf2_attr (die, DW_AT_location, cu);
11532 if (attr)
11533 {
11534 var_decode_location (attr, sym, cu);
11535 }
11536 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11537 if (attr)
11538 {
11539 dwarf2_const_value (attr, sym, cu);
11540 }
11541 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11542 if (attr && DW_UNSND (attr))
11543 {
11544 struct type *ref_type;
11545
11546 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11547 SYMBOL_TYPE (sym) = ref_type;
11548 }
11549
11550 list_to_add = cu->list_in_scope;
11551 break;
11552 case DW_TAG_unspecified_parameters:
11553 /* From varargs functions; gdb doesn't seem to have any
11554 interest in this information, so just ignore it for now.
11555 (FIXME?) */
11556 break;
11557 case DW_TAG_template_type_param:
11558 suppress_add = 1;
11559 /* Fall through. */
11560 case DW_TAG_class_type:
11561 case DW_TAG_interface_type:
11562 case DW_TAG_structure_type:
11563 case DW_TAG_union_type:
11564 case DW_TAG_set_type:
11565 case DW_TAG_enumeration_type:
11566 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11567 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11568
11569 {
11570 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11571 really ever be static objects: otherwise, if you try
11572 to, say, break of a class's method and you're in a file
11573 which doesn't mention that class, it won't work unless
11574 the check for all static symbols in lookup_symbol_aux
11575 saves you. See the OtherFileClass tests in
11576 gdb.c++/namespace.exp. */
11577
11578 if (!suppress_add)
11579 {
11580 list_to_add = (cu->list_in_scope == &file_symbols
11581 && (cu->language == language_cplus
11582 || cu->language == language_java)
11583 ? &global_symbols : cu->list_in_scope);
11584
11585 /* The semantics of C++ state that "struct foo {
11586 ... }" also defines a typedef for "foo". A Java
11587 class declaration also defines a typedef for the
11588 class. */
11589 if (cu->language == language_cplus
11590 || cu->language == language_java
11591 || cu->language == language_ada)
11592 {
11593 /* The symbol's name is already allocated along
11594 with this objfile, so we don't need to
11595 duplicate it for the type. */
11596 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11597 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11598 }
11599 }
11600 }
11601 break;
11602 case DW_TAG_typedef:
11603 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11604 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11605 list_to_add = cu->list_in_scope;
11606 break;
11607 case DW_TAG_base_type:
11608 case DW_TAG_subrange_type:
11609 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11610 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11611 list_to_add = cu->list_in_scope;
11612 break;
11613 case DW_TAG_enumerator:
11614 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11615 if (attr)
11616 {
11617 dwarf2_const_value (attr, sym, cu);
11618 }
11619 {
11620 /* NOTE: carlton/2003-11-10: See comment above in the
11621 DW_TAG_class_type, etc. block. */
11622
11623 list_to_add = (cu->list_in_scope == &file_symbols
11624 && (cu->language == language_cplus
11625 || cu->language == language_java)
11626 ? &global_symbols : cu->list_in_scope);
11627 }
11628 break;
11629 case DW_TAG_namespace:
11630 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11631 list_to_add = &global_symbols;
11632 break;
11633 default:
11634 /* Not a tag we recognize. Hopefully we aren't processing
11635 trash data, but since we must specifically ignore things
11636 we don't recognize, there is nothing else we should do at
11637 this point. */
11638 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11639 dwarf_tag_name (die->tag));
11640 break;
11641 }
11642
11643 if (suppress_add)
11644 {
11645 sym->hash_next = objfile->template_symbols;
11646 objfile->template_symbols = sym;
11647 list_to_add = NULL;
11648 }
11649
11650 if (list_to_add != NULL)
11651 add_symbol_to_list (sym, list_to_add);
11652
11653 /* For the benefit of old versions of GCC, check for anonymous
11654 namespaces based on the demangled name. */
11655 if (!processing_has_namespace_info
11656 && cu->language == language_cplus)
11657 cp_scan_for_anonymous_namespaces (sym);
11658 }
11659 return (sym);
11660 }
11661
11662 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11663
11664 static struct symbol *
11665 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11666 {
11667 return new_symbol_full (die, type, cu, NULL);
11668 }
11669
11670 /* Given an attr with a DW_FORM_dataN value in host byte order,
11671 zero-extend it as appropriate for the symbol's type. The DWARF
11672 standard (v4) is not entirely clear about the meaning of using
11673 DW_FORM_dataN for a constant with a signed type, where the type is
11674 wider than the data. The conclusion of a discussion on the DWARF
11675 list was that this is unspecified. We choose to always zero-extend
11676 because that is the interpretation long in use by GCC. */
11677
11678 static gdb_byte *
11679 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11680 const char *name, struct obstack *obstack,
11681 struct dwarf2_cu *cu, long *value, int bits)
11682 {
11683 struct objfile *objfile = cu->objfile;
11684 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11685 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11686 LONGEST l = DW_UNSND (attr);
11687
11688 if (bits < sizeof (*value) * 8)
11689 {
11690 l &= ((LONGEST) 1 << bits) - 1;
11691 *value = l;
11692 }
11693 else if (bits == sizeof (*value) * 8)
11694 *value = l;
11695 else
11696 {
11697 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11698 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11699 return bytes;
11700 }
11701
11702 return NULL;
11703 }
11704
11705 /* Read a constant value from an attribute. Either set *VALUE, or if
11706 the value does not fit in *VALUE, set *BYTES - either already
11707 allocated on the objfile obstack, or newly allocated on OBSTACK,
11708 or, set *BATON, if we translated the constant to a location
11709 expression. */
11710
11711 static void
11712 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11713 const char *name, struct obstack *obstack,
11714 struct dwarf2_cu *cu,
11715 long *value, gdb_byte **bytes,
11716 struct dwarf2_locexpr_baton **baton)
11717 {
11718 struct objfile *objfile = cu->objfile;
11719 struct comp_unit_head *cu_header = &cu->header;
11720 struct dwarf_block *blk;
11721 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11722 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11723
11724 *value = 0;
11725 *bytes = NULL;
11726 *baton = NULL;
11727
11728 switch (attr->form)
11729 {
11730 case DW_FORM_addr:
11731 {
11732 gdb_byte *data;
11733
11734 if (TYPE_LENGTH (type) != cu_header->addr_size)
11735 dwarf2_const_value_length_mismatch_complaint (name,
11736 cu_header->addr_size,
11737 TYPE_LENGTH (type));
11738 /* Symbols of this form are reasonably rare, so we just
11739 piggyback on the existing location code rather than writing
11740 a new implementation of symbol_computed_ops. */
11741 *baton = obstack_alloc (&objfile->objfile_obstack,
11742 sizeof (struct dwarf2_locexpr_baton));
11743 (*baton)->per_cu = cu->per_cu;
11744 gdb_assert ((*baton)->per_cu);
11745
11746 (*baton)->size = 2 + cu_header->addr_size;
11747 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11748 (*baton)->data = data;
11749
11750 data[0] = DW_OP_addr;
11751 store_unsigned_integer (&data[1], cu_header->addr_size,
11752 byte_order, DW_ADDR (attr));
11753 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11754 }
11755 break;
11756 case DW_FORM_string:
11757 case DW_FORM_strp:
11758 /* DW_STRING is already allocated on the objfile obstack, point
11759 directly to it. */
11760 *bytes = (gdb_byte *) DW_STRING (attr);
11761 break;
11762 case DW_FORM_block1:
11763 case DW_FORM_block2:
11764 case DW_FORM_block4:
11765 case DW_FORM_block:
11766 case DW_FORM_exprloc:
11767 blk = DW_BLOCK (attr);
11768 if (TYPE_LENGTH (type) != blk->size)
11769 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11770 TYPE_LENGTH (type));
11771 *bytes = blk->data;
11772 break;
11773
11774 /* The DW_AT_const_value attributes are supposed to carry the
11775 symbol's value "represented as it would be on the target
11776 architecture." By the time we get here, it's already been
11777 converted to host endianness, so we just need to sign- or
11778 zero-extend it as appropriate. */
11779 case DW_FORM_data1:
11780 *bytes = dwarf2_const_value_data (attr, type, name,
11781 obstack, cu, value, 8);
11782 break;
11783 case DW_FORM_data2:
11784 *bytes = dwarf2_const_value_data (attr, type, name,
11785 obstack, cu, value, 16);
11786 break;
11787 case DW_FORM_data4:
11788 *bytes = dwarf2_const_value_data (attr, type, name,
11789 obstack, cu, value, 32);
11790 break;
11791 case DW_FORM_data8:
11792 *bytes = dwarf2_const_value_data (attr, type, name,
11793 obstack, cu, value, 64);
11794 break;
11795
11796 case DW_FORM_sdata:
11797 *value = DW_SND (attr);
11798 break;
11799
11800 case DW_FORM_udata:
11801 *value = DW_UNSND (attr);
11802 break;
11803
11804 default:
11805 complaint (&symfile_complaints,
11806 _("unsupported const value attribute form: '%s'"),
11807 dwarf_form_name (attr->form));
11808 *value = 0;
11809 break;
11810 }
11811 }
11812
11813
11814 /* Copy constant value from an attribute to a symbol. */
11815
11816 static void
11817 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11818 struct dwarf2_cu *cu)
11819 {
11820 struct objfile *objfile = cu->objfile;
11821 struct comp_unit_head *cu_header = &cu->header;
11822 long value;
11823 gdb_byte *bytes;
11824 struct dwarf2_locexpr_baton *baton;
11825
11826 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11827 SYMBOL_PRINT_NAME (sym),
11828 &objfile->objfile_obstack, cu,
11829 &value, &bytes, &baton);
11830
11831 if (baton != NULL)
11832 {
11833 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11834 SYMBOL_LOCATION_BATON (sym) = baton;
11835 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11836 }
11837 else if (bytes != NULL)
11838 {
11839 SYMBOL_VALUE_BYTES (sym) = bytes;
11840 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11841 }
11842 else
11843 {
11844 SYMBOL_VALUE (sym) = value;
11845 SYMBOL_CLASS (sym) = LOC_CONST;
11846 }
11847 }
11848
11849 /* Return the type of the die in question using its DW_AT_type attribute. */
11850
11851 static struct type *
11852 die_type (struct die_info *die, struct dwarf2_cu *cu)
11853 {
11854 struct attribute *type_attr;
11855
11856 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11857 if (!type_attr)
11858 {
11859 /* A missing DW_AT_type represents a void type. */
11860 return objfile_type (cu->objfile)->builtin_void;
11861 }
11862
11863 return lookup_die_type (die, type_attr, cu);
11864 }
11865
11866 /* True iff CU's producer generates GNAT Ada auxiliary information
11867 that allows to find parallel types through that information instead
11868 of having to do expensive parallel lookups by type name. */
11869
11870 static int
11871 need_gnat_info (struct dwarf2_cu *cu)
11872 {
11873 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11874 of GNAT produces this auxiliary information, without any indication
11875 that it is produced. Part of enhancing the FSF version of GNAT
11876 to produce that information will be to put in place an indicator
11877 that we can use in order to determine whether the descriptive type
11878 info is available or not. One suggestion that has been made is
11879 to use a new attribute, attached to the CU die. For now, assume
11880 that the descriptive type info is not available. */
11881 return 0;
11882 }
11883
11884 /* Return the auxiliary type of the die in question using its
11885 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11886 attribute is not present. */
11887
11888 static struct type *
11889 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11890 {
11891 struct attribute *type_attr;
11892
11893 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11894 if (!type_attr)
11895 return NULL;
11896
11897 return lookup_die_type (die, type_attr, cu);
11898 }
11899
11900 /* If DIE has a descriptive_type attribute, then set the TYPE's
11901 descriptive type accordingly. */
11902
11903 static void
11904 set_descriptive_type (struct type *type, struct die_info *die,
11905 struct dwarf2_cu *cu)
11906 {
11907 struct type *descriptive_type = die_descriptive_type (die, cu);
11908
11909 if (descriptive_type)
11910 {
11911 ALLOCATE_GNAT_AUX_TYPE (type);
11912 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11913 }
11914 }
11915
11916 /* Return the containing type of the die in question using its
11917 DW_AT_containing_type attribute. */
11918
11919 static struct type *
11920 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11921 {
11922 struct attribute *type_attr;
11923
11924 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11925 if (!type_attr)
11926 error (_("Dwarf Error: Problem turning containing type into gdb type "
11927 "[in module %s]"), cu->objfile->name);
11928
11929 return lookup_die_type (die, type_attr, cu);
11930 }
11931
11932 /* Look up the type of DIE in CU using its type attribute ATTR.
11933 If there is no type substitute an error marker. */
11934
11935 static struct type *
11936 lookup_die_type (struct die_info *die, struct attribute *attr,
11937 struct dwarf2_cu *cu)
11938 {
11939 struct type *this_type;
11940
11941 /* First see if we have it cached. */
11942
11943 if (is_ref_attr (attr))
11944 {
11945 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11946
11947 this_type = get_die_type_at_offset (offset, cu->per_cu);
11948 }
11949 else if (attr->form == DW_FORM_ref_sig8)
11950 {
11951 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11952 struct dwarf2_cu *sig_cu;
11953 unsigned int offset;
11954
11955 /* sig_type will be NULL if the signatured type is missing from
11956 the debug info. */
11957 if (sig_type == NULL)
11958 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11959 "at 0x%x [in module %s]"),
11960 die->offset, cu->objfile->name);
11961
11962 gdb_assert (sig_type->per_cu.debug_type_section);
11963 offset = sig_type->per_cu.offset + sig_type->type_offset;
11964 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11965 }
11966 else
11967 {
11968 dump_die_for_error (die);
11969 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11970 dwarf_attr_name (attr->name), cu->objfile->name);
11971 }
11972
11973 /* If not cached we need to read it in. */
11974
11975 if (this_type == NULL)
11976 {
11977 struct die_info *type_die;
11978 struct dwarf2_cu *type_cu = cu;
11979
11980 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11981 /* If the type is cached, we should have found it above. */
11982 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11983 this_type = read_type_die_1 (type_die, type_cu);
11984 }
11985
11986 /* If we still don't have a type use an error marker. */
11987
11988 if (this_type == NULL)
11989 {
11990 char *message, *saved;
11991
11992 /* read_type_die already issued a complaint. */
11993 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11994 cu->objfile->name,
11995 cu->header.offset,
11996 die->offset);
11997 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11998 message, strlen (message));
11999 xfree (message);
12000
12001 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
12002 }
12003
12004 return this_type;
12005 }
12006
12007 /* Return the type in DIE, CU.
12008 Returns NULL for invalid types.
12009
12010 This first does a lookup in the appropriate type_hash table,
12011 and only reads the die in if necessary.
12012
12013 NOTE: This can be called when reading in partial or full symbols. */
12014
12015 static struct type *
12016 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12017 {
12018 struct type *this_type;
12019
12020 this_type = get_die_type (die, cu);
12021 if (this_type)
12022 return this_type;
12023
12024 return read_type_die_1 (die, cu);
12025 }
12026
12027 /* Read the type in DIE, CU.
12028 Returns NULL for invalid types. */
12029
12030 static struct type *
12031 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12032 {
12033 struct type *this_type = NULL;
12034
12035 switch (die->tag)
12036 {
12037 case DW_TAG_class_type:
12038 case DW_TAG_interface_type:
12039 case DW_TAG_structure_type:
12040 case DW_TAG_union_type:
12041 this_type = read_structure_type (die, cu);
12042 break;
12043 case DW_TAG_enumeration_type:
12044 this_type = read_enumeration_type (die, cu);
12045 break;
12046 case DW_TAG_subprogram:
12047 case DW_TAG_subroutine_type:
12048 case DW_TAG_inlined_subroutine:
12049 this_type = read_subroutine_type (die, cu);
12050 break;
12051 case DW_TAG_array_type:
12052 this_type = read_array_type (die, cu);
12053 break;
12054 case DW_TAG_set_type:
12055 this_type = read_set_type (die, cu);
12056 break;
12057 case DW_TAG_pointer_type:
12058 this_type = read_tag_pointer_type (die, cu);
12059 break;
12060 case DW_TAG_ptr_to_member_type:
12061 this_type = read_tag_ptr_to_member_type (die, cu);
12062 break;
12063 case DW_TAG_reference_type:
12064 this_type = read_tag_reference_type (die, cu);
12065 break;
12066 case DW_TAG_const_type:
12067 this_type = read_tag_const_type (die, cu);
12068 break;
12069 case DW_TAG_volatile_type:
12070 this_type = read_tag_volatile_type (die, cu);
12071 break;
12072 case DW_TAG_string_type:
12073 this_type = read_tag_string_type (die, cu);
12074 break;
12075 case DW_TAG_typedef:
12076 this_type = read_typedef (die, cu);
12077 break;
12078 case DW_TAG_subrange_type:
12079 this_type = read_subrange_type (die, cu);
12080 break;
12081 case DW_TAG_base_type:
12082 this_type = read_base_type (die, cu);
12083 break;
12084 case DW_TAG_unspecified_type:
12085 this_type = read_unspecified_type (die, cu);
12086 break;
12087 case DW_TAG_namespace:
12088 this_type = read_namespace_type (die, cu);
12089 break;
12090 case DW_TAG_module:
12091 this_type = read_module_type (die, cu);
12092 break;
12093 default:
12094 complaint (&symfile_complaints,
12095 _("unexpected tag in read_type_die: '%s'"),
12096 dwarf_tag_name (die->tag));
12097 break;
12098 }
12099
12100 return this_type;
12101 }
12102
12103 /* See if we can figure out if the class lives in a namespace. We do
12104 this by looking for a member function; its demangled name will
12105 contain namespace info, if there is any.
12106 Return the computed name or NULL.
12107 Space for the result is allocated on the objfile's obstack.
12108 This is the full-die version of guess_partial_die_structure_name.
12109 In this case we know DIE has no useful parent. */
12110
12111 static char *
12112 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12113 {
12114 struct die_info *spec_die;
12115 struct dwarf2_cu *spec_cu;
12116 struct die_info *child;
12117
12118 spec_cu = cu;
12119 spec_die = die_specification (die, &spec_cu);
12120 if (spec_die != NULL)
12121 {
12122 die = spec_die;
12123 cu = spec_cu;
12124 }
12125
12126 for (child = die->child;
12127 child != NULL;
12128 child = child->sibling)
12129 {
12130 if (child->tag == DW_TAG_subprogram)
12131 {
12132 struct attribute *attr;
12133
12134 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12135 if (attr == NULL)
12136 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12137 if (attr != NULL)
12138 {
12139 char *actual_name
12140 = language_class_name_from_physname (cu->language_defn,
12141 DW_STRING (attr));
12142 char *name = NULL;
12143
12144 if (actual_name != NULL)
12145 {
12146 char *die_name = dwarf2_name (die, cu);
12147
12148 if (die_name != NULL
12149 && strcmp (die_name, actual_name) != 0)
12150 {
12151 /* Strip off the class name from the full name.
12152 We want the prefix. */
12153 int die_name_len = strlen (die_name);
12154 int actual_name_len = strlen (actual_name);
12155
12156 /* Test for '::' as a sanity check. */
12157 if (actual_name_len > die_name_len + 2
12158 && actual_name[actual_name_len
12159 - die_name_len - 1] == ':')
12160 name =
12161 obsavestring (actual_name,
12162 actual_name_len - die_name_len - 2,
12163 &cu->objfile->objfile_obstack);
12164 }
12165 }
12166 xfree (actual_name);
12167 return name;
12168 }
12169 }
12170 }
12171
12172 return NULL;
12173 }
12174
12175 /* Return the name of the namespace/class that DIE is defined within,
12176 or "" if we can't tell. The caller should not xfree the result.
12177
12178 For example, if we're within the method foo() in the following
12179 code:
12180
12181 namespace N {
12182 class C {
12183 void foo () {
12184 }
12185 };
12186 }
12187
12188 then determine_prefix on foo's die will return "N::C". */
12189
12190 static char *
12191 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12192 {
12193 struct die_info *parent, *spec_die;
12194 struct dwarf2_cu *spec_cu;
12195 struct type *parent_type;
12196
12197 if (cu->language != language_cplus && cu->language != language_java
12198 && cu->language != language_fortran)
12199 return "";
12200
12201 /* We have to be careful in the presence of DW_AT_specification.
12202 For example, with GCC 3.4, given the code
12203
12204 namespace N {
12205 void foo() {
12206 // Definition of N::foo.
12207 }
12208 }
12209
12210 then we'll have a tree of DIEs like this:
12211
12212 1: DW_TAG_compile_unit
12213 2: DW_TAG_namespace // N
12214 3: DW_TAG_subprogram // declaration of N::foo
12215 4: DW_TAG_subprogram // definition of N::foo
12216 DW_AT_specification // refers to die #3
12217
12218 Thus, when processing die #4, we have to pretend that we're in
12219 the context of its DW_AT_specification, namely the contex of die
12220 #3. */
12221 spec_cu = cu;
12222 spec_die = die_specification (die, &spec_cu);
12223 if (spec_die == NULL)
12224 parent = die->parent;
12225 else
12226 {
12227 parent = spec_die->parent;
12228 cu = spec_cu;
12229 }
12230
12231 if (parent == NULL)
12232 return "";
12233 else if (parent->building_fullname)
12234 {
12235 const char *name;
12236 const char *parent_name;
12237
12238 /* It has been seen on RealView 2.2 built binaries,
12239 DW_TAG_template_type_param types actually _defined_ as
12240 children of the parent class:
12241
12242 enum E {};
12243 template class <class Enum> Class{};
12244 Class<enum E> class_e;
12245
12246 1: DW_TAG_class_type (Class)
12247 2: DW_TAG_enumeration_type (E)
12248 3: DW_TAG_enumerator (enum1:0)
12249 3: DW_TAG_enumerator (enum2:1)
12250 ...
12251 2: DW_TAG_template_type_param
12252 DW_AT_type DW_FORM_ref_udata (E)
12253
12254 Besides being broken debug info, it can put GDB into an
12255 infinite loop. Consider:
12256
12257 When we're building the full name for Class<E>, we'll start
12258 at Class, and go look over its template type parameters,
12259 finding E. We'll then try to build the full name of E, and
12260 reach here. We're now trying to build the full name of E,
12261 and look over the parent DIE for containing scope. In the
12262 broken case, if we followed the parent DIE of E, we'd again
12263 find Class, and once again go look at its template type
12264 arguments, etc., etc. Simply don't consider such parent die
12265 as source-level parent of this die (it can't be, the language
12266 doesn't allow it), and break the loop here. */
12267 name = dwarf2_name (die, cu);
12268 parent_name = dwarf2_name (parent, cu);
12269 complaint (&symfile_complaints,
12270 _("template param type '%s' defined within parent '%s'"),
12271 name ? name : "<unknown>",
12272 parent_name ? parent_name : "<unknown>");
12273 return "";
12274 }
12275 else
12276 switch (parent->tag)
12277 {
12278 case DW_TAG_namespace:
12279 parent_type = read_type_die (parent, cu);
12280 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12281 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12282 Work around this problem here. */
12283 if (cu->language == language_cplus
12284 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12285 return "";
12286 /* We give a name to even anonymous namespaces. */
12287 return TYPE_TAG_NAME (parent_type);
12288 case DW_TAG_class_type:
12289 case DW_TAG_interface_type:
12290 case DW_TAG_structure_type:
12291 case DW_TAG_union_type:
12292 case DW_TAG_module:
12293 parent_type = read_type_die (parent, cu);
12294 if (TYPE_TAG_NAME (parent_type) != NULL)
12295 return TYPE_TAG_NAME (parent_type);
12296 else
12297 /* An anonymous structure is only allowed non-static data
12298 members; no typedefs, no member functions, et cetera.
12299 So it does not need a prefix. */
12300 return "";
12301 case DW_TAG_compile_unit:
12302 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12303 if (cu->language == language_cplus
12304 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12305 && die->child != NULL
12306 && (die->tag == DW_TAG_class_type
12307 || die->tag == DW_TAG_structure_type
12308 || die->tag == DW_TAG_union_type))
12309 {
12310 char *name = guess_full_die_structure_name (die, cu);
12311 if (name != NULL)
12312 return name;
12313 }
12314 return "";
12315 default:
12316 return determine_prefix (parent, cu);
12317 }
12318 }
12319
12320 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12321 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12322 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12323 an obconcat, otherwise allocate storage for the result. The CU argument is
12324 used to determine the language and hence, the appropriate separator. */
12325
12326 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12327
12328 static char *
12329 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12330 int physname, struct dwarf2_cu *cu)
12331 {
12332 const char *lead = "";
12333 const char *sep;
12334
12335 if (suffix == NULL || suffix[0] == '\0'
12336 || prefix == NULL || prefix[0] == '\0')
12337 sep = "";
12338 else if (cu->language == language_java)
12339 sep = ".";
12340 else if (cu->language == language_fortran && physname)
12341 {
12342 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12343 DW_AT_MIPS_linkage_name is preferred and used instead. */
12344
12345 lead = "__";
12346 sep = "_MOD_";
12347 }
12348 else
12349 sep = "::";
12350
12351 if (prefix == NULL)
12352 prefix = "";
12353 if (suffix == NULL)
12354 suffix = "";
12355
12356 if (obs == NULL)
12357 {
12358 char *retval
12359 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12360
12361 strcpy (retval, lead);
12362 strcat (retval, prefix);
12363 strcat (retval, sep);
12364 strcat (retval, suffix);
12365 return retval;
12366 }
12367 else
12368 {
12369 /* We have an obstack. */
12370 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12371 }
12372 }
12373
12374 /* Return sibling of die, NULL if no sibling. */
12375
12376 static struct die_info *
12377 sibling_die (struct die_info *die)
12378 {
12379 return die->sibling;
12380 }
12381
12382 /* Get name of a die, return NULL if not found. */
12383
12384 static char *
12385 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12386 struct obstack *obstack)
12387 {
12388 if (name && cu->language == language_cplus)
12389 {
12390 char *canon_name = cp_canonicalize_string (name);
12391
12392 if (canon_name != NULL)
12393 {
12394 if (strcmp (canon_name, name) != 0)
12395 name = obsavestring (canon_name, strlen (canon_name),
12396 obstack);
12397 xfree (canon_name);
12398 }
12399 }
12400
12401 return name;
12402 }
12403
12404 /* Get name of a die, return NULL if not found. */
12405
12406 static char *
12407 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12408 {
12409 struct attribute *attr;
12410
12411 attr = dwarf2_attr (die, DW_AT_name, cu);
12412 if ((!attr || !DW_STRING (attr))
12413 && die->tag != DW_TAG_class_type
12414 && die->tag != DW_TAG_interface_type
12415 && die->tag != DW_TAG_structure_type
12416 && die->tag != DW_TAG_union_type)
12417 return NULL;
12418
12419 switch (die->tag)
12420 {
12421 case DW_TAG_compile_unit:
12422 /* Compilation units have a DW_AT_name that is a filename, not
12423 a source language identifier. */
12424 case DW_TAG_enumeration_type:
12425 case DW_TAG_enumerator:
12426 /* These tags always have simple identifiers already; no need
12427 to canonicalize them. */
12428 return DW_STRING (attr);
12429
12430 case DW_TAG_subprogram:
12431 /* Java constructors will all be named "<init>", so return
12432 the class name when we see this special case. */
12433 if (cu->language == language_java
12434 && DW_STRING (attr) != NULL
12435 && strcmp (DW_STRING (attr), "<init>") == 0)
12436 {
12437 struct dwarf2_cu *spec_cu = cu;
12438 struct die_info *spec_die;
12439
12440 /* GCJ will output '<init>' for Java constructor names.
12441 For this special case, return the name of the parent class. */
12442
12443 /* GCJ may output suprogram DIEs with AT_specification set.
12444 If so, use the name of the specified DIE. */
12445 spec_die = die_specification (die, &spec_cu);
12446 if (spec_die != NULL)
12447 return dwarf2_name (spec_die, spec_cu);
12448
12449 do
12450 {
12451 die = die->parent;
12452 if (die->tag == DW_TAG_class_type)
12453 return dwarf2_name (die, cu);
12454 }
12455 while (die->tag != DW_TAG_compile_unit);
12456 }
12457 break;
12458
12459 case DW_TAG_class_type:
12460 case DW_TAG_interface_type:
12461 case DW_TAG_structure_type:
12462 case DW_TAG_union_type:
12463 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12464 structures or unions. These were of the form "._%d" in GCC 4.1,
12465 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12466 and GCC 4.4. We work around this problem by ignoring these. */
12467 if (attr && DW_STRING (attr)
12468 && (strncmp (DW_STRING (attr), "._", 2) == 0
12469 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12470 return NULL;
12471
12472 /* GCC might emit a nameless typedef that has a linkage name. See
12473 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12474 if (!attr || DW_STRING (attr) == NULL)
12475 {
12476 char *demangled = NULL;
12477
12478 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12479 if (attr == NULL)
12480 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12481
12482 if (attr == NULL || DW_STRING (attr) == NULL)
12483 return NULL;
12484
12485 /* Avoid demangling DW_STRING (attr) the second time on a second
12486 call for the same DIE. */
12487 if (!DW_STRING_IS_CANONICAL (attr))
12488 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12489
12490 if (demangled)
12491 {
12492 /* FIXME: we already did this for the partial symbol... */
12493 DW_STRING (attr)
12494 = obsavestring (demangled, strlen (demangled),
12495 &cu->objfile->objfile_obstack);
12496 DW_STRING_IS_CANONICAL (attr) = 1;
12497 xfree (demangled);
12498 }
12499 }
12500 break;
12501
12502 default:
12503 break;
12504 }
12505
12506 if (!DW_STRING_IS_CANONICAL (attr))
12507 {
12508 DW_STRING (attr)
12509 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12510 &cu->objfile->objfile_obstack);
12511 DW_STRING_IS_CANONICAL (attr) = 1;
12512 }
12513 return DW_STRING (attr);
12514 }
12515
12516 /* Return the die that this die in an extension of, or NULL if there
12517 is none. *EXT_CU is the CU containing DIE on input, and the CU
12518 containing the return value on output. */
12519
12520 static struct die_info *
12521 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12522 {
12523 struct attribute *attr;
12524
12525 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12526 if (attr == NULL)
12527 return NULL;
12528
12529 return follow_die_ref (die, attr, ext_cu);
12530 }
12531
12532 /* Convert a DIE tag into its string name. */
12533
12534 static char *
12535 dwarf_tag_name (unsigned tag)
12536 {
12537 switch (tag)
12538 {
12539 case DW_TAG_padding:
12540 return "DW_TAG_padding";
12541 case DW_TAG_array_type:
12542 return "DW_TAG_array_type";
12543 case DW_TAG_class_type:
12544 return "DW_TAG_class_type";
12545 case DW_TAG_entry_point:
12546 return "DW_TAG_entry_point";
12547 case DW_TAG_enumeration_type:
12548 return "DW_TAG_enumeration_type";
12549 case DW_TAG_formal_parameter:
12550 return "DW_TAG_formal_parameter";
12551 case DW_TAG_imported_declaration:
12552 return "DW_TAG_imported_declaration";
12553 case DW_TAG_label:
12554 return "DW_TAG_label";
12555 case DW_TAG_lexical_block:
12556 return "DW_TAG_lexical_block";
12557 case DW_TAG_member:
12558 return "DW_TAG_member";
12559 case DW_TAG_pointer_type:
12560 return "DW_TAG_pointer_type";
12561 case DW_TAG_reference_type:
12562 return "DW_TAG_reference_type";
12563 case DW_TAG_compile_unit:
12564 return "DW_TAG_compile_unit";
12565 case DW_TAG_string_type:
12566 return "DW_TAG_string_type";
12567 case DW_TAG_structure_type:
12568 return "DW_TAG_structure_type";
12569 case DW_TAG_subroutine_type:
12570 return "DW_TAG_subroutine_type";
12571 case DW_TAG_typedef:
12572 return "DW_TAG_typedef";
12573 case DW_TAG_union_type:
12574 return "DW_TAG_union_type";
12575 case DW_TAG_unspecified_parameters:
12576 return "DW_TAG_unspecified_parameters";
12577 case DW_TAG_variant:
12578 return "DW_TAG_variant";
12579 case DW_TAG_common_block:
12580 return "DW_TAG_common_block";
12581 case DW_TAG_common_inclusion:
12582 return "DW_TAG_common_inclusion";
12583 case DW_TAG_inheritance:
12584 return "DW_TAG_inheritance";
12585 case DW_TAG_inlined_subroutine:
12586 return "DW_TAG_inlined_subroutine";
12587 case DW_TAG_module:
12588 return "DW_TAG_module";
12589 case DW_TAG_ptr_to_member_type:
12590 return "DW_TAG_ptr_to_member_type";
12591 case DW_TAG_set_type:
12592 return "DW_TAG_set_type";
12593 case DW_TAG_subrange_type:
12594 return "DW_TAG_subrange_type";
12595 case DW_TAG_with_stmt:
12596 return "DW_TAG_with_stmt";
12597 case DW_TAG_access_declaration:
12598 return "DW_TAG_access_declaration";
12599 case DW_TAG_base_type:
12600 return "DW_TAG_base_type";
12601 case DW_TAG_catch_block:
12602 return "DW_TAG_catch_block";
12603 case DW_TAG_const_type:
12604 return "DW_TAG_const_type";
12605 case DW_TAG_constant:
12606 return "DW_TAG_constant";
12607 case DW_TAG_enumerator:
12608 return "DW_TAG_enumerator";
12609 case DW_TAG_file_type:
12610 return "DW_TAG_file_type";
12611 case DW_TAG_friend:
12612 return "DW_TAG_friend";
12613 case DW_TAG_namelist:
12614 return "DW_TAG_namelist";
12615 case DW_TAG_namelist_item:
12616 return "DW_TAG_namelist_item";
12617 case DW_TAG_packed_type:
12618 return "DW_TAG_packed_type";
12619 case DW_TAG_subprogram:
12620 return "DW_TAG_subprogram";
12621 case DW_TAG_template_type_param:
12622 return "DW_TAG_template_type_param";
12623 case DW_TAG_template_value_param:
12624 return "DW_TAG_template_value_param";
12625 case DW_TAG_thrown_type:
12626 return "DW_TAG_thrown_type";
12627 case DW_TAG_try_block:
12628 return "DW_TAG_try_block";
12629 case DW_TAG_variant_part:
12630 return "DW_TAG_variant_part";
12631 case DW_TAG_variable:
12632 return "DW_TAG_variable";
12633 case DW_TAG_volatile_type:
12634 return "DW_TAG_volatile_type";
12635 case DW_TAG_dwarf_procedure:
12636 return "DW_TAG_dwarf_procedure";
12637 case DW_TAG_restrict_type:
12638 return "DW_TAG_restrict_type";
12639 case DW_TAG_interface_type:
12640 return "DW_TAG_interface_type";
12641 case DW_TAG_namespace:
12642 return "DW_TAG_namespace";
12643 case DW_TAG_imported_module:
12644 return "DW_TAG_imported_module";
12645 case DW_TAG_unspecified_type:
12646 return "DW_TAG_unspecified_type";
12647 case DW_TAG_partial_unit:
12648 return "DW_TAG_partial_unit";
12649 case DW_TAG_imported_unit:
12650 return "DW_TAG_imported_unit";
12651 case DW_TAG_condition:
12652 return "DW_TAG_condition";
12653 case DW_TAG_shared_type:
12654 return "DW_TAG_shared_type";
12655 case DW_TAG_type_unit:
12656 return "DW_TAG_type_unit";
12657 case DW_TAG_MIPS_loop:
12658 return "DW_TAG_MIPS_loop";
12659 case DW_TAG_HP_array_descriptor:
12660 return "DW_TAG_HP_array_descriptor";
12661 case DW_TAG_format_label:
12662 return "DW_TAG_format_label";
12663 case DW_TAG_function_template:
12664 return "DW_TAG_function_template";
12665 case DW_TAG_class_template:
12666 return "DW_TAG_class_template";
12667 case DW_TAG_GNU_BINCL:
12668 return "DW_TAG_GNU_BINCL";
12669 case DW_TAG_GNU_EINCL:
12670 return "DW_TAG_GNU_EINCL";
12671 case DW_TAG_upc_shared_type:
12672 return "DW_TAG_upc_shared_type";
12673 case DW_TAG_upc_strict_type:
12674 return "DW_TAG_upc_strict_type";
12675 case DW_TAG_upc_relaxed_type:
12676 return "DW_TAG_upc_relaxed_type";
12677 case DW_TAG_PGI_kanji_type:
12678 return "DW_TAG_PGI_kanji_type";
12679 case DW_TAG_PGI_interface_block:
12680 return "DW_TAG_PGI_interface_block";
12681 default:
12682 return "DW_TAG_<unknown>";
12683 }
12684 }
12685
12686 /* Convert a DWARF attribute code into its string name. */
12687
12688 static char *
12689 dwarf_attr_name (unsigned attr)
12690 {
12691 switch (attr)
12692 {
12693 case DW_AT_sibling:
12694 return "DW_AT_sibling";
12695 case DW_AT_location:
12696 return "DW_AT_location";
12697 case DW_AT_name:
12698 return "DW_AT_name";
12699 case DW_AT_ordering:
12700 return "DW_AT_ordering";
12701 case DW_AT_subscr_data:
12702 return "DW_AT_subscr_data";
12703 case DW_AT_byte_size:
12704 return "DW_AT_byte_size";
12705 case DW_AT_bit_offset:
12706 return "DW_AT_bit_offset";
12707 case DW_AT_bit_size:
12708 return "DW_AT_bit_size";
12709 case DW_AT_element_list:
12710 return "DW_AT_element_list";
12711 case DW_AT_stmt_list:
12712 return "DW_AT_stmt_list";
12713 case DW_AT_low_pc:
12714 return "DW_AT_low_pc";
12715 case DW_AT_high_pc:
12716 return "DW_AT_high_pc";
12717 case DW_AT_language:
12718 return "DW_AT_language";
12719 case DW_AT_member:
12720 return "DW_AT_member";
12721 case DW_AT_discr:
12722 return "DW_AT_discr";
12723 case DW_AT_discr_value:
12724 return "DW_AT_discr_value";
12725 case DW_AT_visibility:
12726 return "DW_AT_visibility";
12727 case DW_AT_import:
12728 return "DW_AT_import";
12729 case DW_AT_string_length:
12730 return "DW_AT_string_length";
12731 case DW_AT_common_reference:
12732 return "DW_AT_common_reference";
12733 case DW_AT_comp_dir:
12734 return "DW_AT_comp_dir";
12735 case DW_AT_const_value:
12736 return "DW_AT_const_value";
12737 case DW_AT_containing_type:
12738 return "DW_AT_containing_type";
12739 case DW_AT_default_value:
12740 return "DW_AT_default_value";
12741 case DW_AT_inline:
12742 return "DW_AT_inline";
12743 case DW_AT_is_optional:
12744 return "DW_AT_is_optional";
12745 case DW_AT_lower_bound:
12746 return "DW_AT_lower_bound";
12747 case DW_AT_producer:
12748 return "DW_AT_producer";
12749 case DW_AT_prototyped:
12750 return "DW_AT_prototyped";
12751 case DW_AT_return_addr:
12752 return "DW_AT_return_addr";
12753 case DW_AT_start_scope:
12754 return "DW_AT_start_scope";
12755 case DW_AT_bit_stride:
12756 return "DW_AT_bit_stride";
12757 case DW_AT_upper_bound:
12758 return "DW_AT_upper_bound";
12759 case DW_AT_abstract_origin:
12760 return "DW_AT_abstract_origin";
12761 case DW_AT_accessibility:
12762 return "DW_AT_accessibility";
12763 case DW_AT_address_class:
12764 return "DW_AT_address_class";
12765 case DW_AT_artificial:
12766 return "DW_AT_artificial";
12767 case DW_AT_base_types:
12768 return "DW_AT_base_types";
12769 case DW_AT_calling_convention:
12770 return "DW_AT_calling_convention";
12771 case DW_AT_count:
12772 return "DW_AT_count";
12773 case DW_AT_data_member_location:
12774 return "DW_AT_data_member_location";
12775 case DW_AT_decl_column:
12776 return "DW_AT_decl_column";
12777 case DW_AT_decl_file:
12778 return "DW_AT_decl_file";
12779 case DW_AT_decl_line:
12780 return "DW_AT_decl_line";
12781 case DW_AT_declaration:
12782 return "DW_AT_declaration";
12783 case DW_AT_discr_list:
12784 return "DW_AT_discr_list";
12785 case DW_AT_encoding:
12786 return "DW_AT_encoding";
12787 case DW_AT_external:
12788 return "DW_AT_external";
12789 case DW_AT_frame_base:
12790 return "DW_AT_frame_base";
12791 case DW_AT_friend:
12792 return "DW_AT_friend";
12793 case DW_AT_identifier_case:
12794 return "DW_AT_identifier_case";
12795 case DW_AT_macro_info:
12796 return "DW_AT_macro_info";
12797 case DW_AT_namelist_items:
12798 return "DW_AT_namelist_items";
12799 case DW_AT_priority:
12800 return "DW_AT_priority";
12801 case DW_AT_segment:
12802 return "DW_AT_segment";
12803 case DW_AT_specification:
12804 return "DW_AT_specification";
12805 case DW_AT_static_link:
12806 return "DW_AT_static_link";
12807 case DW_AT_type:
12808 return "DW_AT_type";
12809 case DW_AT_use_location:
12810 return "DW_AT_use_location";
12811 case DW_AT_variable_parameter:
12812 return "DW_AT_variable_parameter";
12813 case DW_AT_virtuality:
12814 return "DW_AT_virtuality";
12815 case DW_AT_vtable_elem_location:
12816 return "DW_AT_vtable_elem_location";
12817 /* DWARF 3 values. */
12818 case DW_AT_allocated:
12819 return "DW_AT_allocated";
12820 case DW_AT_associated:
12821 return "DW_AT_associated";
12822 case DW_AT_data_location:
12823 return "DW_AT_data_location";
12824 case DW_AT_byte_stride:
12825 return "DW_AT_byte_stride";
12826 case DW_AT_entry_pc:
12827 return "DW_AT_entry_pc";
12828 case DW_AT_use_UTF8:
12829 return "DW_AT_use_UTF8";
12830 case DW_AT_extension:
12831 return "DW_AT_extension";
12832 case DW_AT_ranges:
12833 return "DW_AT_ranges";
12834 case DW_AT_trampoline:
12835 return "DW_AT_trampoline";
12836 case DW_AT_call_column:
12837 return "DW_AT_call_column";
12838 case DW_AT_call_file:
12839 return "DW_AT_call_file";
12840 case DW_AT_call_line:
12841 return "DW_AT_call_line";
12842 case DW_AT_description:
12843 return "DW_AT_description";
12844 case DW_AT_binary_scale:
12845 return "DW_AT_binary_scale";
12846 case DW_AT_decimal_scale:
12847 return "DW_AT_decimal_scale";
12848 case DW_AT_small:
12849 return "DW_AT_small";
12850 case DW_AT_decimal_sign:
12851 return "DW_AT_decimal_sign";
12852 case DW_AT_digit_count:
12853 return "DW_AT_digit_count";
12854 case DW_AT_picture_string:
12855 return "DW_AT_picture_string";
12856 case DW_AT_mutable:
12857 return "DW_AT_mutable";
12858 case DW_AT_threads_scaled:
12859 return "DW_AT_threads_scaled";
12860 case DW_AT_explicit:
12861 return "DW_AT_explicit";
12862 case DW_AT_object_pointer:
12863 return "DW_AT_object_pointer";
12864 case DW_AT_endianity:
12865 return "DW_AT_endianity";
12866 case DW_AT_elemental:
12867 return "DW_AT_elemental";
12868 case DW_AT_pure:
12869 return "DW_AT_pure";
12870 case DW_AT_recursive:
12871 return "DW_AT_recursive";
12872 /* DWARF 4 values. */
12873 case DW_AT_signature:
12874 return "DW_AT_signature";
12875 case DW_AT_linkage_name:
12876 return "DW_AT_linkage_name";
12877 /* SGI/MIPS extensions. */
12878 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12879 case DW_AT_MIPS_fde:
12880 return "DW_AT_MIPS_fde";
12881 #endif
12882 case DW_AT_MIPS_loop_begin:
12883 return "DW_AT_MIPS_loop_begin";
12884 case DW_AT_MIPS_tail_loop_begin:
12885 return "DW_AT_MIPS_tail_loop_begin";
12886 case DW_AT_MIPS_epilog_begin:
12887 return "DW_AT_MIPS_epilog_begin";
12888 case DW_AT_MIPS_loop_unroll_factor:
12889 return "DW_AT_MIPS_loop_unroll_factor";
12890 case DW_AT_MIPS_software_pipeline_depth:
12891 return "DW_AT_MIPS_software_pipeline_depth";
12892 case DW_AT_MIPS_linkage_name:
12893 return "DW_AT_MIPS_linkage_name";
12894 case DW_AT_MIPS_stride:
12895 return "DW_AT_MIPS_stride";
12896 case DW_AT_MIPS_abstract_name:
12897 return "DW_AT_MIPS_abstract_name";
12898 case DW_AT_MIPS_clone_origin:
12899 return "DW_AT_MIPS_clone_origin";
12900 case DW_AT_MIPS_has_inlines:
12901 return "DW_AT_MIPS_has_inlines";
12902 /* HP extensions. */
12903 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12904 case DW_AT_HP_block_index:
12905 return "DW_AT_HP_block_index";
12906 #endif
12907 case DW_AT_HP_unmodifiable:
12908 return "DW_AT_HP_unmodifiable";
12909 case DW_AT_HP_actuals_stmt_list:
12910 return "DW_AT_HP_actuals_stmt_list";
12911 case DW_AT_HP_proc_per_section:
12912 return "DW_AT_HP_proc_per_section";
12913 case DW_AT_HP_raw_data_ptr:
12914 return "DW_AT_HP_raw_data_ptr";
12915 case DW_AT_HP_pass_by_reference:
12916 return "DW_AT_HP_pass_by_reference";
12917 case DW_AT_HP_opt_level:
12918 return "DW_AT_HP_opt_level";
12919 case DW_AT_HP_prof_version_id:
12920 return "DW_AT_HP_prof_version_id";
12921 case DW_AT_HP_opt_flags:
12922 return "DW_AT_HP_opt_flags";
12923 case DW_AT_HP_cold_region_low_pc:
12924 return "DW_AT_HP_cold_region_low_pc";
12925 case DW_AT_HP_cold_region_high_pc:
12926 return "DW_AT_HP_cold_region_high_pc";
12927 case DW_AT_HP_all_variables_modifiable:
12928 return "DW_AT_HP_all_variables_modifiable";
12929 case DW_AT_HP_linkage_name:
12930 return "DW_AT_HP_linkage_name";
12931 case DW_AT_HP_prof_flags:
12932 return "DW_AT_HP_prof_flags";
12933 /* GNU extensions. */
12934 case DW_AT_sf_names:
12935 return "DW_AT_sf_names";
12936 case DW_AT_src_info:
12937 return "DW_AT_src_info";
12938 case DW_AT_mac_info:
12939 return "DW_AT_mac_info";
12940 case DW_AT_src_coords:
12941 return "DW_AT_src_coords";
12942 case DW_AT_body_begin:
12943 return "DW_AT_body_begin";
12944 case DW_AT_body_end:
12945 return "DW_AT_body_end";
12946 case DW_AT_GNU_vector:
12947 return "DW_AT_GNU_vector";
12948 case DW_AT_GNU_odr_signature:
12949 return "DW_AT_GNU_odr_signature";
12950 /* VMS extensions. */
12951 case DW_AT_VMS_rtnbeg_pd_address:
12952 return "DW_AT_VMS_rtnbeg_pd_address";
12953 /* UPC extension. */
12954 case DW_AT_upc_threads_scaled:
12955 return "DW_AT_upc_threads_scaled";
12956 /* PGI (STMicroelectronics) extensions. */
12957 case DW_AT_PGI_lbase:
12958 return "DW_AT_PGI_lbase";
12959 case DW_AT_PGI_soffset:
12960 return "DW_AT_PGI_soffset";
12961 case DW_AT_PGI_lstride:
12962 return "DW_AT_PGI_lstride";
12963 default:
12964 return "DW_AT_<unknown>";
12965 }
12966 }
12967
12968 /* Convert a DWARF value form code into its string name. */
12969
12970 static char *
12971 dwarf_form_name (unsigned form)
12972 {
12973 switch (form)
12974 {
12975 case DW_FORM_addr:
12976 return "DW_FORM_addr";
12977 case DW_FORM_block2:
12978 return "DW_FORM_block2";
12979 case DW_FORM_block4:
12980 return "DW_FORM_block4";
12981 case DW_FORM_data2:
12982 return "DW_FORM_data2";
12983 case DW_FORM_data4:
12984 return "DW_FORM_data4";
12985 case DW_FORM_data8:
12986 return "DW_FORM_data8";
12987 case DW_FORM_string:
12988 return "DW_FORM_string";
12989 case DW_FORM_block:
12990 return "DW_FORM_block";
12991 case DW_FORM_block1:
12992 return "DW_FORM_block1";
12993 case DW_FORM_data1:
12994 return "DW_FORM_data1";
12995 case DW_FORM_flag:
12996 return "DW_FORM_flag";
12997 case DW_FORM_sdata:
12998 return "DW_FORM_sdata";
12999 case DW_FORM_strp:
13000 return "DW_FORM_strp";
13001 case DW_FORM_udata:
13002 return "DW_FORM_udata";
13003 case DW_FORM_ref_addr:
13004 return "DW_FORM_ref_addr";
13005 case DW_FORM_ref1:
13006 return "DW_FORM_ref1";
13007 case DW_FORM_ref2:
13008 return "DW_FORM_ref2";
13009 case DW_FORM_ref4:
13010 return "DW_FORM_ref4";
13011 case DW_FORM_ref8:
13012 return "DW_FORM_ref8";
13013 case DW_FORM_ref_udata:
13014 return "DW_FORM_ref_udata";
13015 case DW_FORM_indirect:
13016 return "DW_FORM_indirect";
13017 case DW_FORM_sec_offset:
13018 return "DW_FORM_sec_offset";
13019 case DW_FORM_exprloc:
13020 return "DW_FORM_exprloc";
13021 case DW_FORM_flag_present:
13022 return "DW_FORM_flag_present";
13023 case DW_FORM_ref_sig8:
13024 return "DW_FORM_ref_sig8";
13025 default:
13026 return "DW_FORM_<unknown>";
13027 }
13028 }
13029
13030 /* Convert a DWARF stack opcode into its string name. */
13031
13032 const char *
13033 dwarf_stack_op_name (unsigned op)
13034 {
13035 switch (op)
13036 {
13037 case DW_OP_addr:
13038 return "DW_OP_addr";
13039 case DW_OP_deref:
13040 return "DW_OP_deref";
13041 case DW_OP_const1u:
13042 return "DW_OP_const1u";
13043 case DW_OP_const1s:
13044 return "DW_OP_const1s";
13045 case DW_OP_const2u:
13046 return "DW_OP_const2u";
13047 case DW_OP_const2s:
13048 return "DW_OP_const2s";
13049 case DW_OP_const4u:
13050 return "DW_OP_const4u";
13051 case DW_OP_const4s:
13052 return "DW_OP_const4s";
13053 case DW_OP_const8u:
13054 return "DW_OP_const8u";
13055 case DW_OP_const8s:
13056 return "DW_OP_const8s";
13057 case DW_OP_constu:
13058 return "DW_OP_constu";
13059 case DW_OP_consts:
13060 return "DW_OP_consts";
13061 case DW_OP_dup:
13062 return "DW_OP_dup";
13063 case DW_OP_drop:
13064 return "DW_OP_drop";
13065 case DW_OP_over:
13066 return "DW_OP_over";
13067 case DW_OP_pick:
13068 return "DW_OP_pick";
13069 case DW_OP_swap:
13070 return "DW_OP_swap";
13071 case DW_OP_rot:
13072 return "DW_OP_rot";
13073 case DW_OP_xderef:
13074 return "DW_OP_xderef";
13075 case DW_OP_abs:
13076 return "DW_OP_abs";
13077 case DW_OP_and:
13078 return "DW_OP_and";
13079 case DW_OP_div:
13080 return "DW_OP_div";
13081 case DW_OP_minus:
13082 return "DW_OP_minus";
13083 case DW_OP_mod:
13084 return "DW_OP_mod";
13085 case DW_OP_mul:
13086 return "DW_OP_mul";
13087 case DW_OP_neg:
13088 return "DW_OP_neg";
13089 case DW_OP_not:
13090 return "DW_OP_not";
13091 case DW_OP_or:
13092 return "DW_OP_or";
13093 case DW_OP_plus:
13094 return "DW_OP_plus";
13095 case DW_OP_plus_uconst:
13096 return "DW_OP_plus_uconst";
13097 case DW_OP_shl:
13098 return "DW_OP_shl";
13099 case DW_OP_shr:
13100 return "DW_OP_shr";
13101 case DW_OP_shra:
13102 return "DW_OP_shra";
13103 case DW_OP_xor:
13104 return "DW_OP_xor";
13105 case DW_OP_bra:
13106 return "DW_OP_bra";
13107 case DW_OP_eq:
13108 return "DW_OP_eq";
13109 case DW_OP_ge:
13110 return "DW_OP_ge";
13111 case DW_OP_gt:
13112 return "DW_OP_gt";
13113 case DW_OP_le:
13114 return "DW_OP_le";
13115 case DW_OP_lt:
13116 return "DW_OP_lt";
13117 case DW_OP_ne:
13118 return "DW_OP_ne";
13119 case DW_OP_skip:
13120 return "DW_OP_skip";
13121 case DW_OP_lit0:
13122 return "DW_OP_lit0";
13123 case DW_OP_lit1:
13124 return "DW_OP_lit1";
13125 case DW_OP_lit2:
13126 return "DW_OP_lit2";
13127 case DW_OP_lit3:
13128 return "DW_OP_lit3";
13129 case DW_OP_lit4:
13130 return "DW_OP_lit4";
13131 case DW_OP_lit5:
13132 return "DW_OP_lit5";
13133 case DW_OP_lit6:
13134 return "DW_OP_lit6";
13135 case DW_OP_lit7:
13136 return "DW_OP_lit7";
13137 case DW_OP_lit8:
13138 return "DW_OP_lit8";
13139 case DW_OP_lit9:
13140 return "DW_OP_lit9";
13141 case DW_OP_lit10:
13142 return "DW_OP_lit10";
13143 case DW_OP_lit11:
13144 return "DW_OP_lit11";
13145 case DW_OP_lit12:
13146 return "DW_OP_lit12";
13147 case DW_OP_lit13:
13148 return "DW_OP_lit13";
13149 case DW_OP_lit14:
13150 return "DW_OP_lit14";
13151 case DW_OP_lit15:
13152 return "DW_OP_lit15";
13153 case DW_OP_lit16:
13154 return "DW_OP_lit16";
13155 case DW_OP_lit17:
13156 return "DW_OP_lit17";
13157 case DW_OP_lit18:
13158 return "DW_OP_lit18";
13159 case DW_OP_lit19:
13160 return "DW_OP_lit19";
13161 case DW_OP_lit20:
13162 return "DW_OP_lit20";
13163 case DW_OP_lit21:
13164 return "DW_OP_lit21";
13165 case DW_OP_lit22:
13166 return "DW_OP_lit22";
13167 case DW_OP_lit23:
13168 return "DW_OP_lit23";
13169 case DW_OP_lit24:
13170 return "DW_OP_lit24";
13171 case DW_OP_lit25:
13172 return "DW_OP_lit25";
13173 case DW_OP_lit26:
13174 return "DW_OP_lit26";
13175 case DW_OP_lit27:
13176 return "DW_OP_lit27";
13177 case DW_OP_lit28:
13178 return "DW_OP_lit28";
13179 case DW_OP_lit29:
13180 return "DW_OP_lit29";
13181 case DW_OP_lit30:
13182 return "DW_OP_lit30";
13183 case DW_OP_lit31:
13184 return "DW_OP_lit31";
13185 case DW_OP_reg0:
13186 return "DW_OP_reg0";
13187 case DW_OP_reg1:
13188 return "DW_OP_reg1";
13189 case DW_OP_reg2:
13190 return "DW_OP_reg2";
13191 case DW_OP_reg3:
13192 return "DW_OP_reg3";
13193 case DW_OP_reg4:
13194 return "DW_OP_reg4";
13195 case DW_OP_reg5:
13196 return "DW_OP_reg5";
13197 case DW_OP_reg6:
13198 return "DW_OP_reg6";
13199 case DW_OP_reg7:
13200 return "DW_OP_reg7";
13201 case DW_OP_reg8:
13202 return "DW_OP_reg8";
13203 case DW_OP_reg9:
13204 return "DW_OP_reg9";
13205 case DW_OP_reg10:
13206 return "DW_OP_reg10";
13207 case DW_OP_reg11:
13208 return "DW_OP_reg11";
13209 case DW_OP_reg12:
13210 return "DW_OP_reg12";
13211 case DW_OP_reg13:
13212 return "DW_OP_reg13";
13213 case DW_OP_reg14:
13214 return "DW_OP_reg14";
13215 case DW_OP_reg15:
13216 return "DW_OP_reg15";
13217 case DW_OP_reg16:
13218 return "DW_OP_reg16";
13219 case DW_OP_reg17:
13220 return "DW_OP_reg17";
13221 case DW_OP_reg18:
13222 return "DW_OP_reg18";
13223 case DW_OP_reg19:
13224 return "DW_OP_reg19";
13225 case DW_OP_reg20:
13226 return "DW_OP_reg20";
13227 case DW_OP_reg21:
13228 return "DW_OP_reg21";
13229 case DW_OP_reg22:
13230 return "DW_OP_reg22";
13231 case DW_OP_reg23:
13232 return "DW_OP_reg23";
13233 case DW_OP_reg24:
13234 return "DW_OP_reg24";
13235 case DW_OP_reg25:
13236 return "DW_OP_reg25";
13237 case DW_OP_reg26:
13238 return "DW_OP_reg26";
13239 case DW_OP_reg27:
13240 return "DW_OP_reg27";
13241 case DW_OP_reg28:
13242 return "DW_OP_reg28";
13243 case DW_OP_reg29:
13244 return "DW_OP_reg29";
13245 case DW_OP_reg30:
13246 return "DW_OP_reg30";
13247 case DW_OP_reg31:
13248 return "DW_OP_reg31";
13249 case DW_OP_breg0:
13250 return "DW_OP_breg0";
13251 case DW_OP_breg1:
13252 return "DW_OP_breg1";
13253 case DW_OP_breg2:
13254 return "DW_OP_breg2";
13255 case DW_OP_breg3:
13256 return "DW_OP_breg3";
13257 case DW_OP_breg4:
13258 return "DW_OP_breg4";
13259 case DW_OP_breg5:
13260 return "DW_OP_breg5";
13261 case DW_OP_breg6:
13262 return "DW_OP_breg6";
13263 case DW_OP_breg7:
13264 return "DW_OP_breg7";
13265 case DW_OP_breg8:
13266 return "DW_OP_breg8";
13267 case DW_OP_breg9:
13268 return "DW_OP_breg9";
13269 case DW_OP_breg10:
13270 return "DW_OP_breg10";
13271 case DW_OP_breg11:
13272 return "DW_OP_breg11";
13273 case DW_OP_breg12:
13274 return "DW_OP_breg12";
13275 case DW_OP_breg13:
13276 return "DW_OP_breg13";
13277 case DW_OP_breg14:
13278 return "DW_OP_breg14";
13279 case DW_OP_breg15:
13280 return "DW_OP_breg15";
13281 case DW_OP_breg16:
13282 return "DW_OP_breg16";
13283 case DW_OP_breg17:
13284 return "DW_OP_breg17";
13285 case DW_OP_breg18:
13286 return "DW_OP_breg18";
13287 case DW_OP_breg19:
13288 return "DW_OP_breg19";
13289 case DW_OP_breg20:
13290 return "DW_OP_breg20";
13291 case DW_OP_breg21:
13292 return "DW_OP_breg21";
13293 case DW_OP_breg22:
13294 return "DW_OP_breg22";
13295 case DW_OP_breg23:
13296 return "DW_OP_breg23";
13297 case DW_OP_breg24:
13298 return "DW_OP_breg24";
13299 case DW_OP_breg25:
13300 return "DW_OP_breg25";
13301 case DW_OP_breg26:
13302 return "DW_OP_breg26";
13303 case DW_OP_breg27:
13304 return "DW_OP_breg27";
13305 case DW_OP_breg28:
13306 return "DW_OP_breg28";
13307 case DW_OP_breg29:
13308 return "DW_OP_breg29";
13309 case DW_OP_breg30:
13310 return "DW_OP_breg30";
13311 case DW_OP_breg31:
13312 return "DW_OP_breg31";
13313 case DW_OP_regx:
13314 return "DW_OP_regx";
13315 case DW_OP_fbreg:
13316 return "DW_OP_fbreg";
13317 case DW_OP_bregx:
13318 return "DW_OP_bregx";
13319 case DW_OP_piece:
13320 return "DW_OP_piece";
13321 case DW_OP_deref_size:
13322 return "DW_OP_deref_size";
13323 case DW_OP_xderef_size:
13324 return "DW_OP_xderef_size";
13325 case DW_OP_nop:
13326 return "DW_OP_nop";
13327 /* DWARF 3 extensions. */
13328 case DW_OP_push_object_address:
13329 return "DW_OP_push_object_address";
13330 case DW_OP_call2:
13331 return "DW_OP_call2";
13332 case DW_OP_call4:
13333 return "DW_OP_call4";
13334 case DW_OP_call_ref:
13335 return "DW_OP_call_ref";
13336 case DW_OP_form_tls_address:
13337 return "DW_OP_form_tls_address";
13338 case DW_OP_call_frame_cfa:
13339 return "DW_OP_call_frame_cfa";
13340 case DW_OP_bit_piece:
13341 return "DW_OP_bit_piece";
13342 /* DWARF 4 extensions. */
13343 case DW_OP_implicit_value:
13344 return "DW_OP_implicit_value";
13345 case DW_OP_stack_value:
13346 return "DW_OP_stack_value";
13347 /* GNU extensions. */
13348 case DW_OP_GNU_push_tls_address:
13349 return "DW_OP_GNU_push_tls_address";
13350 case DW_OP_GNU_uninit:
13351 return "DW_OP_GNU_uninit";
13352 case DW_OP_GNU_implicit_pointer:
13353 return "DW_OP_GNU_implicit_pointer";
13354 case DW_OP_GNU_entry_value:
13355 return "DW_OP_GNU_entry_value";
13356 case DW_OP_GNU_const_type:
13357 return "DW_OP_GNU_const_type";
13358 case DW_OP_GNU_regval_type:
13359 return "DW_OP_GNU_regval_type";
13360 case DW_OP_GNU_deref_type:
13361 return "DW_OP_GNU_deref_type";
13362 case DW_OP_GNU_convert:
13363 return "DW_OP_GNU_convert";
13364 case DW_OP_GNU_reinterpret:
13365 return "DW_OP_GNU_reinterpret";
13366 default:
13367 return NULL;
13368 }
13369 }
13370
13371 static char *
13372 dwarf_bool_name (unsigned mybool)
13373 {
13374 if (mybool)
13375 return "TRUE";
13376 else
13377 return "FALSE";
13378 }
13379
13380 /* Convert a DWARF type code into its string name. */
13381
13382 static char *
13383 dwarf_type_encoding_name (unsigned enc)
13384 {
13385 switch (enc)
13386 {
13387 case DW_ATE_void:
13388 return "DW_ATE_void";
13389 case DW_ATE_address:
13390 return "DW_ATE_address";
13391 case DW_ATE_boolean:
13392 return "DW_ATE_boolean";
13393 case DW_ATE_complex_float:
13394 return "DW_ATE_complex_float";
13395 case DW_ATE_float:
13396 return "DW_ATE_float";
13397 case DW_ATE_signed:
13398 return "DW_ATE_signed";
13399 case DW_ATE_signed_char:
13400 return "DW_ATE_signed_char";
13401 case DW_ATE_unsigned:
13402 return "DW_ATE_unsigned";
13403 case DW_ATE_unsigned_char:
13404 return "DW_ATE_unsigned_char";
13405 /* DWARF 3. */
13406 case DW_ATE_imaginary_float:
13407 return "DW_ATE_imaginary_float";
13408 case DW_ATE_packed_decimal:
13409 return "DW_ATE_packed_decimal";
13410 case DW_ATE_numeric_string:
13411 return "DW_ATE_numeric_string";
13412 case DW_ATE_edited:
13413 return "DW_ATE_edited";
13414 case DW_ATE_signed_fixed:
13415 return "DW_ATE_signed_fixed";
13416 case DW_ATE_unsigned_fixed:
13417 return "DW_ATE_unsigned_fixed";
13418 case DW_ATE_decimal_float:
13419 return "DW_ATE_decimal_float";
13420 /* DWARF 4. */
13421 case DW_ATE_UTF:
13422 return "DW_ATE_UTF";
13423 /* HP extensions. */
13424 case DW_ATE_HP_float80:
13425 return "DW_ATE_HP_float80";
13426 case DW_ATE_HP_complex_float80:
13427 return "DW_ATE_HP_complex_float80";
13428 case DW_ATE_HP_float128:
13429 return "DW_ATE_HP_float128";
13430 case DW_ATE_HP_complex_float128:
13431 return "DW_ATE_HP_complex_float128";
13432 case DW_ATE_HP_floathpintel:
13433 return "DW_ATE_HP_floathpintel";
13434 case DW_ATE_HP_imaginary_float80:
13435 return "DW_ATE_HP_imaginary_float80";
13436 case DW_ATE_HP_imaginary_float128:
13437 return "DW_ATE_HP_imaginary_float128";
13438 default:
13439 return "DW_ATE_<unknown>";
13440 }
13441 }
13442
13443 /* Convert a DWARF call frame info operation to its string name. */
13444
13445 #if 0
13446 static char *
13447 dwarf_cfi_name (unsigned cfi_opc)
13448 {
13449 switch (cfi_opc)
13450 {
13451 case DW_CFA_advance_loc:
13452 return "DW_CFA_advance_loc";
13453 case DW_CFA_offset:
13454 return "DW_CFA_offset";
13455 case DW_CFA_restore:
13456 return "DW_CFA_restore";
13457 case DW_CFA_nop:
13458 return "DW_CFA_nop";
13459 case DW_CFA_set_loc:
13460 return "DW_CFA_set_loc";
13461 case DW_CFA_advance_loc1:
13462 return "DW_CFA_advance_loc1";
13463 case DW_CFA_advance_loc2:
13464 return "DW_CFA_advance_loc2";
13465 case DW_CFA_advance_loc4:
13466 return "DW_CFA_advance_loc4";
13467 case DW_CFA_offset_extended:
13468 return "DW_CFA_offset_extended";
13469 case DW_CFA_restore_extended:
13470 return "DW_CFA_restore_extended";
13471 case DW_CFA_undefined:
13472 return "DW_CFA_undefined";
13473 case DW_CFA_same_value:
13474 return "DW_CFA_same_value";
13475 case DW_CFA_register:
13476 return "DW_CFA_register";
13477 case DW_CFA_remember_state:
13478 return "DW_CFA_remember_state";
13479 case DW_CFA_restore_state:
13480 return "DW_CFA_restore_state";
13481 case DW_CFA_def_cfa:
13482 return "DW_CFA_def_cfa";
13483 case DW_CFA_def_cfa_register:
13484 return "DW_CFA_def_cfa_register";
13485 case DW_CFA_def_cfa_offset:
13486 return "DW_CFA_def_cfa_offset";
13487 /* DWARF 3. */
13488 case DW_CFA_def_cfa_expression:
13489 return "DW_CFA_def_cfa_expression";
13490 case DW_CFA_expression:
13491 return "DW_CFA_expression";
13492 case DW_CFA_offset_extended_sf:
13493 return "DW_CFA_offset_extended_sf";
13494 case DW_CFA_def_cfa_sf:
13495 return "DW_CFA_def_cfa_sf";
13496 case DW_CFA_def_cfa_offset_sf:
13497 return "DW_CFA_def_cfa_offset_sf";
13498 case DW_CFA_val_offset:
13499 return "DW_CFA_val_offset";
13500 case DW_CFA_val_offset_sf:
13501 return "DW_CFA_val_offset_sf";
13502 case DW_CFA_val_expression:
13503 return "DW_CFA_val_expression";
13504 /* SGI/MIPS specific. */
13505 case DW_CFA_MIPS_advance_loc8:
13506 return "DW_CFA_MIPS_advance_loc8";
13507 /* GNU extensions. */
13508 case DW_CFA_GNU_window_save:
13509 return "DW_CFA_GNU_window_save";
13510 case DW_CFA_GNU_args_size:
13511 return "DW_CFA_GNU_args_size";
13512 case DW_CFA_GNU_negative_offset_extended:
13513 return "DW_CFA_GNU_negative_offset_extended";
13514 default:
13515 return "DW_CFA_<unknown>";
13516 }
13517 }
13518 #endif
13519
13520 static void
13521 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13522 {
13523 unsigned int i;
13524
13525 print_spaces (indent, f);
13526 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13527 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13528
13529 if (die->parent != NULL)
13530 {
13531 print_spaces (indent, f);
13532 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13533 die->parent->offset);
13534 }
13535
13536 print_spaces (indent, f);
13537 fprintf_unfiltered (f, " has children: %s\n",
13538 dwarf_bool_name (die->child != NULL));
13539
13540 print_spaces (indent, f);
13541 fprintf_unfiltered (f, " attributes:\n");
13542
13543 for (i = 0; i < die->num_attrs; ++i)
13544 {
13545 print_spaces (indent, f);
13546 fprintf_unfiltered (f, " %s (%s) ",
13547 dwarf_attr_name (die->attrs[i].name),
13548 dwarf_form_name (die->attrs[i].form));
13549
13550 switch (die->attrs[i].form)
13551 {
13552 case DW_FORM_ref_addr:
13553 case DW_FORM_addr:
13554 fprintf_unfiltered (f, "address: ");
13555 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13556 break;
13557 case DW_FORM_block2:
13558 case DW_FORM_block4:
13559 case DW_FORM_block:
13560 case DW_FORM_block1:
13561 fprintf_unfiltered (f, "block: size %d",
13562 DW_BLOCK (&die->attrs[i])->size);
13563 break;
13564 case DW_FORM_exprloc:
13565 fprintf_unfiltered (f, "expression: size %u",
13566 DW_BLOCK (&die->attrs[i])->size);
13567 break;
13568 case DW_FORM_ref1:
13569 case DW_FORM_ref2:
13570 case DW_FORM_ref4:
13571 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13572 (long) (DW_ADDR (&die->attrs[i])));
13573 break;
13574 case DW_FORM_data1:
13575 case DW_FORM_data2:
13576 case DW_FORM_data4:
13577 case DW_FORM_data8:
13578 case DW_FORM_udata:
13579 case DW_FORM_sdata:
13580 fprintf_unfiltered (f, "constant: %s",
13581 pulongest (DW_UNSND (&die->attrs[i])));
13582 break;
13583 case DW_FORM_sec_offset:
13584 fprintf_unfiltered (f, "section offset: %s",
13585 pulongest (DW_UNSND (&die->attrs[i])));
13586 break;
13587 case DW_FORM_ref_sig8:
13588 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13589 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13590 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13591 else
13592 fprintf_unfiltered (f, "signatured type, offset: unknown");
13593 break;
13594 case DW_FORM_string:
13595 case DW_FORM_strp:
13596 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13597 DW_STRING (&die->attrs[i])
13598 ? DW_STRING (&die->attrs[i]) : "",
13599 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13600 break;
13601 case DW_FORM_flag:
13602 if (DW_UNSND (&die->attrs[i]))
13603 fprintf_unfiltered (f, "flag: TRUE");
13604 else
13605 fprintf_unfiltered (f, "flag: FALSE");
13606 break;
13607 case DW_FORM_flag_present:
13608 fprintf_unfiltered (f, "flag: TRUE");
13609 break;
13610 case DW_FORM_indirect:
13611 /* The reader will have reduced the indirect form to
13612 the "base form" so this form should not occur. */
13613 fprintf_unfiltered (f,
13614 "unexpected attribute form: DW_FORM_indirect");
13615 break;
13616 default:
13617 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13618 die->attrs[i].form);
13619 break;
13620 }
13621 fprintf_unfiltered (f, "\n");
13622 }
13623 }
13624
13625 static void
13626 dump_die_for_error (struct die_info *die)
13627 {
13628 dump_die_shallow (gdb_stderr, 0, die);
13629 }
13630
13631 static void
13632 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13633 {
13634 int indent = level * 4;
13635
13636 gdb_assert (die != NULL);
13637
13638 if (level >= max_level)
13639 return;
13640
13641 dump_die_shallow (f, indent, die);
13642
13643 if (die->child != NULL)
13644 {
13645 print_spaces (indent, f);
13646 fprintf_unfiltered (f, " Children:");
13647 if (level + 1 < max_level)
13648 {
13649 fprintf_unfiltered (f, "\n");
13650 dump_die_1 (f, level + 1, max_level, die->child);
13651 }
13652 else
13653 {
13654 fprintf_unfiltered (f,
13655 " [not printed, max nesting level reached]\n");
13656 }
13657 }
13658
13659 if (die->sibling != NULL && level > 0)
13660 {
13661 dump_die_1 (f, level, max_level, die->sibling);
13662 }
13663 }
13664
13665 /* This is called from the pdie macro in gdbinit.in.
13666 It's not static so gcc will keep a copy callable from gdb. */
13667
13668 void
13669 dump_die (struct die_info *die, int max_level)
13670 {
13671 dump_die_1 (gdb_stdlog, 0, max_level, die);
13672 }
13673
13674 static void
13675 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13676 {
13677 void **slot;
13678
13679 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13680
13681 *slot = die;
13682 }
13683
13684 static int
13685 is_ref_attr (struct attribute *attr)
13686 {
13687 switch (attr->form)
13688 {
13689 case DW_FORM_ref_addr:
13690 case DW_FORM_ref1:
13691 case DW_FORM_ref2:
13692 case DW_FORM_ref4:
13693 case DW_FORM_ref8:
13694 case DW_FORM_ref_udata:
13695 return 1;
13696 default:
13697 return 0;
13698 }
13699 }
13700
13701 static unsigned int
13702 dwarf2_get_ref_die_offset (struct attribute *attr)
13703 {
13704 if (is_ref_attr (attr))
13705 return DW_ADDR (attr);
13706
13707 complaint (&symfile_complaints,
13708 _("unsupported die ref attribute form: '%s'"),
13709 dwarf_form_name (attr->form));
13710 return 0;
13711 }
13712
13713 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13714 * the value held by the attribute is not constant. */
13715
13716 static LONGEST
13717 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13718 {
13719 if (attr->form == DW_FORM_sdata)
13720 return DW_SND (attr);
13721 else if (attr->form == DW_FORM_udata
13722 || attr->form == DW_FORM_data1
13723 || attr->form == DW_FORM_data2
13724 || attr->form == DW_FORM_data4
13725 || attr->form == DW_FORM_data8)
13726 return DW_UNSND (attr);
13727 else
13728 {
13729 complaint (&symfile_complaints,
13730 _("Attribute value is not a constant (%s)"),
13731 dwarf_form_name (attr->form));
13732 return default_value;
13733 }
13734 }
13735
13736 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
13737 unit and add it to our queue.
13738 The result is non-zero if PER_CU was queued, otherwise the result is zero
13739 meaning either PER_CU is already queued or it is already loaded. */
13740
13741 static int
13742 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13743 struct dwarf2_per_cu_data *per_cu)
13744 {
13745 /* We may arrive here during partial symbol reading, if we need full
13746 DIEs to process an unusual case (e.g. template arguments). Do
13747 not queue PER_CU, just tell our caller to load its DIEs. */
13748 if (dwarf2_per_objfile->reading_partial_symbols)
13749 {
13750 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13751 return 1;
13752 return 0;
13753 }
13754
13755 /* Mark the dependence relation so that we don't flush PER_CU
13756 too early. */
13757 dwarf2_add_dependence (this_cu, per_cu);
13758
13759 /* If it's already on the queue, we have nothing to do. */
13760 if (per_cu->queued)
13761 return 0;
13762
13763 /* If the compilation unit is already loaded, just mark it as
13764 used. */
13765 if (per_cu->cu != NULL)
13766 {
13767 per_cu->cu->last_used = 0;
13768 return 0;
13769 }
13770
13771 /* Add it to the queue. */
13772 queue_comp_unit (per_cu, this_cu->objfile);
13773
13774 return 1;
13775 }
13776
13777 /* Follow reference or signature attribute ATTR of SRC_DIE.
13778 On entry *REF_CU is the CU of SRC_DIE.
13779 On exit *REF_CU is the CU of the result. */
13780
13781 static struct die_info *
13782 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13783 struct dwarf2_cu **ref_cu)
13784 {
13785 struct die_info *die;
13786
13787 if (is_ref_attr (attr))
13788 die = follow_die_ref (src_die, attr, ref_cu);
13789 else if (attr->form == DW_FORM_ref_sig8)
13790 die = follow_die_sig (src_die, attr, ref_cu);
13791 else
13792 {
13793 dump_die_for_error (src_die);
13794 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13795 (*ref_cu)->objfile->name);
13796 }
13797
13798 return die;
13799 }
13800
13801 /* Follow reference OFFSET.
13802 On entry *REF_CU is the CU of the source die referencing OFFSET.
13803 On exit *REF_CU is the CU of the result.
13804 Returns NULL if OFFSET is invalid. */
13805
13806 static struct die_info *
13807 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13808 {
13809 struct die_info temp_die;
13810 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13811
13812 gdb_assert (cu->per_cu != NULL);
13813
13814 target_cu = cu;
13815
13816 if (cu->per_cu->debug_type_section)
13817 {
13818 /* .debug_types CUs cannot reference anything outside their CU.
13819 If they need to, they have to reference a signatured type via
13820 DW_FORM_ref_sig8. */
13821 if (! offset_in_cu_p (&cu->header, offset))
13822 return NULL;
13823 }
13824 else if (! offset_in_cu_p (&cu->header, offset))
13825 {
13826 struct dwarf2_per_cu_data *per_cu;
13827
13828 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13829
13830 /* If necessary, add it to the queue and load its DIEs. */
13831 if (maybe_queue_comp_unit (cu, per_cu))
13832 load_full_comp_unit (per_cu, cu->objfile);
13833
13834 target_cu = per_cu->cu;
13835 }
13836 else if (cu->dies == NULL)
13837 {
13838 /* We're loading full DIEs during partial symbol reading. */
13839 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13840 load_full_comp_unit (cu->per_cu, cu->objfile);
13841 }
13842
13843 *ref_cu = target_cu;
13844 temp_die.offset = offset;
13845 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13846 }
13847
13848 /* Follow reference attribute ATTR of SRC_DIE.
13849 On entry *REF_CU is the CU of SRC_DIE.
13850 On exit *REF_CU is the CU of the result. */
13851
13852 static struct die_info *
13853 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13854 struct dwarf2_cu **ref_cu)
13855 {
13856 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13857 struct dwarf2_cu *cu = *ref_cu;
13858 struct die_info *die;
13859
13860 die = follow_die_offset (offset, ref_cu);
13861 if (!die)
13862 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13863 "at 0x%x [in module %s]"),
13864 offset, src_die->offset, cu->objfile->name);
13865
13866 return die;
13867 }
13868
13869 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13870 value is intended for DW_OP_call*. You must call xfree on returned
13871 dwarf2_locexpr_baton->data. */
13872
13873 struct dwarf2_locexpr_baton
13874 dwarf2_fetch_die_location_block (unsigned int offset,
13875 struct dwarf2_per_cu_data *per_cu,
13876 CORE_ADDR (*get_frame_pc) (void *baton),
13877 void *baton)
13878 {
13879 struct dwarf2_cu *cu;
13880 struct die_info *die;
13881 struct attribute *attr;
13882 struct dwarf2_locexpr_baton retval;
13883
13884 dw2_setup (per_cu->objfile);
13885
13886 if (per_cu->cu == NULL)
13887 load_cu (per_cu);
13888 cu = per_cu->cu;
13889
13890 die = follow_die_offset (offset, &cu);
13891 if (!die)
13892 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13893 offset, per_cu->cu->objfile->name);
13894
13895 attr = dwarf2_attr (die, DW_AT_location, cu);
13896 if (!attr)
13897 {
13898 /* DWARF: "If there is no such attribute, then there is no effect.". */
13899
13900 retval.data = NULL;
13901 retval.size = 0;
13902 }
13903 else if (attr_form_is_section_offset (attr))
13904 {
13905 struct dwarf2_loclist_baton loclist_baton;
13906 CORE_ADDR pc = (*get_frame_pc) (baton);
13907 size_t size;
13908
13909 fill_in_loclist_baton (cu, &loclist_baton, attr);
13910
13911 retval.data = dwarf2_find_location_expression (&loclist_baton,
13912 &size, pc);
13913 retval.size = size;
13914 }
13915 else
13916 {
13917 if (!attr_form_is_block (attr))
13918 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13919 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13920 offset, per_cu->cu->objfile->name);
13921
13922 retval.data = DW_BLOCK (attr)->data;
13923 retval.size = DW_BLOCK (attr)->size;
13924 }
13925 retval.per_cu = cu->per_cu;
13926
13927 if (retval.data)
13928 retval.data = xmemdup (retval.data, retval.size, retval.size);
13929
13930 age_cached_comp_units ();
13931
13932 return retval;
13933 }
13934
13935 /* Return the type of the DIE at DIE_OFFSET in the CU named by
13936 PER_CU. */
13937
13938 struct type *
13939 dwarf2_get_die_type (unsigned int die_offset,
13940 struct dwarf2_per_cu_data *per_cu)
13941 {
13942 dw2_setup (per_cu->objfile);
13943 return get_die_type_at_offset (die_offset, per_cu);
13944 }
13945
13946 /* Follow the signature attribute ATTR in SRC_DIE.
13947 On entry *REF_CU is the CU of SRC_DIE.
13948 On exit *REF_CU is the CU of the result. */
13949
13950 static struct die_info *
13951 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13952 struct dwarf2_cu **ref_cu)
13953 {
13954 struct objfile *objfile = (*ref_cu)->objfile;
13955 struct die_info temp_die;
13956 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13957 struct dwarf2_cu *sig_cu;
13958 struct die_info *die;
13959
13960 /* sig_type will be NULL if the signatured type is missing from
13961 the debug info. */
13962 if (sig_type == NULL)
13963 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13964 "at 0x%x [in module %s]"),
13965 src_die->offset, objfile->name);
13966
13967 /* If necessary, add it to the queue and load its DIEs. */
13968
13969 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13970 read_signatured_type (objfile, sig_type);
13971
13972 gdb_assert (sig_type->per_cu.cu != NULL);
13973
13974 sig_cu = sig_type->per_cu.cu;
13975 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13976 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13977 if (die)
13978 {
13979 *ref_cu = sig_cu;
13980 return die;
13981 }
13982
13983 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13984 "from DIE at 0x%x [in module %s]"),
13985 sig_type->type_offset, src_die->offset, objfile->name);
13986 }
13987
13988 /* Given an offset of a signatured type, return its signatured_type. */
13989
13990 static struct signatured_type *
13991 lookup_signatured_type_at_offset (struct objfile *objfile,
13992 struct dwarf2_section_info *section,
13993 unsigned int offset)
13994 {
13995 gdb_byte *info_ptr = section->buffer + offset;
13996 unsigned int length, initial_length_size;
13997 unsigned int sig_offset;
13998 struct signatured_type find_entry, *type_sig;
13999
14000 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14001 sig_offset = (initial_length_size
14002 + 2 /*version*/
14003 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14004 + 1 /*address_size*/);
14005 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14006 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14007
14008 /* This is only used to lookup previously recorded types.
14009 If we didn't find it, it's our bug. */
14010 gdb_assert (type_sig != NULL);
14011 gdb_assert (offset == type_sig->per_cu.offset);
14012
14013 return type_sig;
14014 }
14015
14016 /* Read in signatured type at OFFSET and build its CU and die(s). */
14017
14018 static void
14019 read_signatured_type_at_offset (struct objfile *objfile,
14020 struct dwarf2_section_info *sect,
14021 unsigned int offset)
14022 {
14023 struct signatured_type *type_sig;
14024
14025 dwarf2_read_section (objfile, sect);
14026
14027 /* We have the section offset, but we need the signature to do the
14028 hash table lookup. */
14029 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14030
14031 gdb_assert (type_sig->per_cu.cu == NULL);
14032
14033 read_signatured_type (objfile, type_sig);
14034
14035 gdb_assert (type_sig->per_cu.cu != NULL);
14036 }
14037
14038 /* Read in a signatured type and build its CU and DIEs. */
14039
14040 static void
14041 read_signatured_type (struct objfile *objfile,
14042 struct signatured_type *type_sig)
14043 {
14044 gdb_byte *types_ptr;
14045 struct die_reader_specs reader_specs;
14046 struct dwarf2_cu *cu;
14047 ULONGEST signature;
14048 struct cleanup *back_to, *free_cu_cleanup;
14049 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
14050
14051 dwarf2_read_section (objfile, section);
14052 types_ptr = section->buffer + type_sig->per_cu.offset;
14053
14054 gdb_assert (type_sig->per_cu.cu == NULL);
14055
14056 cu = xmalloc (sizeof (*cu));
14057 init_one_comp_unit (cu, objfile);
14058
14059 type_sig->per_cu.cu = cu;
14060 cu->per_cu = &type_sig->per_cu;
14061
14062 /* If an error occurs while loading, release our storage. */
14063 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14064
14065 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14066 types_ptr, objfile->obfd);
14067 gdb_assert (signature == type_sig->signature);
14068
14069 cu->die_hash
14070 = htab_create_alloc_ex (cu->header.length / 12,
14071 die_hash,
14072 die_eq,
14073 NULL,
14074 &cu->comp_unit_obstack,
14075 hashtab_obstack_allocate,
14076 dummy_obstack_deallocate);
14077
14078 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14079 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14080
14081 init_cu_die_reader (&reader_specs, cu);
14082
14083 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14084 NULL /*parent*/);
14085
14086 /* We try not to read any attributes in this function, because not
14087 all objfiles needed for references have been loaded yet, and symbol
14088 table processing isn't initialized. But we have to set the CU language,
14089 or we won't be able to build types correctly. */
14090 prepare_one_comp_unit (cu, cu->dies);
14091
14092 do_cleanups (back_to);
14093
14094 /* We've successfully allocated this compilation unit. Let our caller
14095 clean it up when finished with it. */
14096 discard_cleanups (free_cu_cleanup);
14097
14098 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14099 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14100 }
14101
14102 /* Decode simple location descriptions.
14103 Given a pointer to a dwarf block that defines a location, compute
14104 the location and return the value.
14105
14106 NOTE drow/2003-11-18: This function is called in two situations
14107 now: for the address of static or global variables (partial symbols
14108 only) and for offsets into structures which are expected to be
14109 (more or less) constant. The partial symbol case should go away,
14110 and only the constant case should remain. That will let this
14111 function complain more accurately. A few special modes are allowed
14112 without complaint for global variables (for instance, global
14113 register values and thread-local values).
14114
14115 A location description containing no operations indicates that the
14116 object is optimized out. The return value is 0 for that case.
14117 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14118 callers will only want a very basic result and this can become a
14119 complaint.
14120
14121 Note that stack[0] is unused except as a default error return. */
14122
14123 static CORE_ADDR
14124 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14125 {
14126 struct objfile *objfile = cu->objfile;
14127 int i;
14128 int size = blk->size;
14129 gdb_byte *data = blk->data;
14130 CORE_ADDR stack[64];
14131 int stacki;
14132 unsigned int bytes_read, unsnd;
14133 gdb_byte op;
14134
14135 i = 0;
14136 stacki = 0;
14137 stack[stacki] = 0;
14138 stack[++stacki] = 0;
14139
14140 while (i < size)
14141 {
14142 op = data[i++];
14143 switch (op)
14144 {
14145 case DW_OP_lit0:
14146 case DW_OP_lit1:
14147 case DW_OP_lit2:
14148 case DW_OP_lit3:
14149 case DW_OP_lit4:
14150 case DW_OP_lit5:
14151 case DW_OP_lit6:
14152 case DW_OP_lit7:
14153 case DW_OP_lit8:
14154 case DW_OP_lit9:
14155 case DW_OP_lit10:
14156 case DW_OP_lit11:
14157 case DW_OP_lit12:
14158 case DW_OP_lit13:
14159 case DW_OP_lit14:
14160 case DW_OP_lit15:
14161 case DW_OP_lit16:
14162 case DW_OP_lit17:
14163 case DW_OP_lit18:
14164 case DW_OP_lit19:
14165 case DW_OP_lit20:
14166 case DW_OP_lit21:
14167 case DW_OP_lit22:
14168 case DW_OP_lit23:
14169 case DW_OP_lit24:
14170 case DW_OP_lit25:
14171 case DW_OP_lit26:
14172 case DW_OP_lit27:
14173 case DW_OP_lit28:
14174 case DW_OP_lit29:
14175 case DW_OP_lit30:
14176 case DW_OP_lit31:
14177 stack[++stacki] = op - DW_OP_lit0;
14178 break;
14179
14180 case DW_OP_reg0:
14181 case DW_OP_reg1:
14182 case DW_OP_reg2:
14183 case DW_OP_reg3:
14184 case DW_OP_reg4:
14185 case DW_OP_reg5:
14186 case DW_OP_reg6:
14187 case DW_OP_reg7:
14188 case DW_OP_reg8:
14189 case DW_OP_reg9:
14190 case DW_OP_reg10:
14191 case DW_OP_reg11:
14192 case DW_OP_reg12:
14193 case DW_OP_reg13:
14194 case DW_OP_reg14:
14195 case DW_OP_reg15:
14196 case DW_OP_reg16:
14197 case DW_OP_reg17:
14198 case DW_OP_reg18:
14199 case DW_OP_reg19:
14200 case DW_OP_reg20:
14201 case DW_OP_reg21:
14202 case DW_OP_reg22:
14203 case DW_OP_reg23:
14204 case DW_OP_reg24:
14205 case DW_OP_reg25:
14206 case DW_OP_reg26:
14207 case DW_OP_reg27:
14208 case DW_OP_reg28:
14209 case DW_OP_reg29:
14210 case DW_OP_reg30:
14211 case DW_OP_reg31:
14212 stack[++stacki] = op - DW_OP_reg0;
14213 if (i < size)
14214 dwarf2_complex_location_expr_complaint ();
14215 break;
14216
14217 case DW_OP_regx:
14218 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14219 i += bytes_read;
14220 stack[++stacki] = unsnd;
14221 if (i < size)
14222 dwarf2_complex_location_expr_complaint ();
14223 break;
14224
14225 case DW_OP_addr:
14226 stack[++stacki] = read_address (objfile->obfd, &data[i],
14227 cu, &bytes_read);
14228 i += bytes_read;
14229 break;
14230
14231 case DW_OP_const1u:
14232 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14233 i += 1;
14234 break;
14235
14236 case DW_OP_const1s:
14237 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14238 i += 1;
14239 break;
14240
14241 case DW_OP_const2u:
14242 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14243 i += 2;
14244 break;
14245
14246 case DW_OP_const2s:
14247 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14248 i += 2;
14249 break;
14250
14251 case DW_OP_const4u:
14252 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14253 i += 4;
14254 break;
14255
14256 case DW_OP_const4s:
14257 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14258 i += 4;
14259 break;
14260
14261 case DW_OP_constu:
14262 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14263 &bytes_read);
14264 i += bytes_read;
14265 break;
14266
14267 case DW_OP_consts:
14268 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14269 i += bytes_read;
14270 break;
14271
14272 case DW_OP_dup:
14273 stack[stacki + 1] = stack[stacki];
14274 stacki++;
14275 break;
14276
14277 case DW_OP_plus:
14278 stack[stacki - 1] += stack[stacki];
14279 stacki--;
14280 break;
14281
14282 case DW_OP_plus_uconst:
14283 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14284 &bytes_read);
14285 i += bytes_read;
14286 break;
14287
14288 case DW_OP_minus:
14289 stack[stacki - 1] -= stack[stacki];
14290 stacki--;
14291 break;
14292
14293 case DW_OP_deref:
14294 /* If we're not the last op, then we definitely can't encode
14295 this using GDB's address_class enum. This is valid for partial
14296 global symbols, although the variable's address will be bogus
14297 in the psymtab. */
14298 if (i < size)
14299 dwarf2_complex_location_expr_complaint ();
14300 break;
14301
14302 case DW_OP_GNU_push_tls_address:
14303 /* The top of the stack has the offset from the beginning
14304 of the thread control block at which the variable is located. */
14305 /* Nothing should follow this operator, so the top of stack would
14306 be returned. */
14307 /* This is valid for partial global symbols, but the variable's
14308 address will be bogus in the psymtab. */
14309 if (i < size)
14310 dwarf2_complex_location_expr_complaint ();
14311 break;
14312
14313 case DW_OP_GNU_uninit:
14314 break;
14315
14316 default:
14317 {
14318 const char *name = dwarf_stack_op_name (op);
14319
14320 if (name)
14321 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14322 name);
14323 else
14324 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14325 op);
14326 }
14327
14328 return (stack[stacki]);
14329 }
14330
14331 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14332 outside of the allocated space. Also enforce minimum>0. */
14333 if (stacki >= ARRAY_SIZE (stack) - 1)
14334 {
14335 complaint (&symfile_complaints,
14336 _("location description stack overflow"));
14337 return 0;
14338 }
14339
14340 if (stacki <= 0)
14341 {
14342 complaint (&symfile_complaints,
14343 _("location description stack underflow"));
14344 return 0;
14345 }
14346 }
14347 return (stack[stacki]);
14348 }
14349
14350 /* memory allocation interface */
14351
14352 static struct dwarf_block *
14353 dwarf_alloc_block (struct dwarf2_cu *cu)
14354 {
14355 struct dwarf_block *blk;
14356
14357 blk = (struct dwarf_block *)
14358 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14359 return (blk);
14360 }
14361
14362 static struct abbrev_info *
14363 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14364 {
14365 struct abbrev_info *abbrev;
14366
14367 abbrev = (struct abbrev_info *)
14368 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14369 memset (abbrev, 0, sizeof (struct abbrev_info));
14370 return (abbrev);
14371 }
14372
14373 static struct die_info *
14374 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14375 {
14376 struct die_info *die;
14377 size_t size = sizeof (struct die_info);
14378
14379 if (num_attrs > 1)
14380 size += (num_attrs - 1) * sizeof (struct attribute);
14381
14382 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14383 memset (die, 0, sizeof (struct die_info));
14384 return (die);
14385 }
14386
14387 \f
14388 /* Macro support. */
14389
14390 /* Return the full name of file number I in *LH's file name table.
14391 Use COMP_DIR as the name of the current directory of the
14392 compilation. The result is allocated using xmalloc; the caller is
14393 responsible for freeing it. */
14394 static char *
14395 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14396 {
14397 /* Is the file number a valid index into the line header's file name
14398 table? Remember that file numbers start with one, not zero. */
14399 if (1 <= file && file <= lh->num_file_names)
14400 {
14401 struct file_entry *fe = &lh->file_names[file - 1];
14402
14403 if (IS_ABSOLUTE_PATH (fe->name))
14404 return xstrdup (fe->name);
14405 else
14406 {
14407 const char *dir;
14408 int dir_len;
14409 char *full_name;
14410
14411 if (fe->dir_index)
14412 dir = lh->include_dirs[fe->dir_index - 1];
14413 else
14414 dir = comp_dir;
14415
14416 if (dir)
14417 {
14418 dir_len = strlen (dir);
14419 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14420 strcpy (full_name, dir);
14421 full_name[dir_len] = '/';
14422 strcpy (full_name + dir_len + 1, fe->name);
14423 return full_name;
14424 }
14425 else
14426 return xstrdup (fe->name);
14427 }
14428 }
14429 else
14430 {
14431 /* The compiler produced a bogus file number. We can at least
14432 record the macro definitions made in the file, even if we
14433 won't be able to find the file by name. */
14434 char fake_name[80];
14435
14436 sprintf (fake_name, "<bad macro file number %d>", file);
14437
14438 complaint (&symfile_complaints,
14439 _("bad file number in macro information (%d)"),
14440 file);
14441
14442 return xstrdup (fake_name);
14443 }
14444 }
14445
14446
14447 static struct macro_source_file *
14448 macro_start_file (int file, int line,
14449 struct macro_source_file *current_file,
14450 const char *comp_dir,
14451 struct line_header *lh, struct objfile *objfile)
14452 {
14453 /* The full name of this source file. */
14454 char *full_name = file_full_name (file, lh, comp_dir);
14455
14456 /* We don't create a macro table for this compilation unit
14457 at all until we actually get a filename. */
14458 if (! pending_macros)
14459 pending_macros = new_macro_table (&objfile->objfile_obstack,
14460 objfile->macro_cache);
14461
14462 if (! current_file)
14463 /* If we have no current file, then this must be the start_file
14464 directive for the compilation unit's main source file. */
14465 current_file = macro_set_main (pending_macros, full_name);
14466 else
14467 current_file = macro_include (current_file, line, full_name);
14468
14469 xfree (full_name);
14470
14471 return current_file;
14472 }
14473
14474
14475 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14476 followed by a null byte. */
14477 static char *
14478 copy_string (const char *buf, int len)
14479 {
14480 char *s = xmalloc (len + 1);
14481
14482 memcpy (s, buf, len);
14483 s[len] = '\0';
14484 return s;
14485 }
14486
14487
14488 static const char *
14489 consume_improper_spaces (const char *p, const char *body)
14490 {
14491 if (*p == ' ')
14492 {
14493 complaint (&symfile_complaints,
14494 _("macro definition contains spaces "
14495 "in formal argument list:\n`%s'"),
14496 body);
14497
14498 while (*p == ' ')
14499 p++;
14500 }
14501
14502 return p;
14503 }
14504
14505
14506 static void
14507 parse_macro_definition (struct macro_source_file *file, int line,
14508 const char *body)
14509 {
14510 const char *p;
14511
14512 /* The body string takes one of two forms. For object-like macro
14513 definitions, it should be:
14514
14515 <macro name> " " <definition>
14516
14517 For function-like macro definitions, it should be:
14518
14519 <macro name> "() " <definition>
14520 or
14521 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14522
14523 Spaces may appear only where explicitly indicated, and in the
14524 <definition>.
14525
14526 The Dwarf 2 spec says that an object-like macro's name is always
14527 followed by a space, but versions of GCC around March 2002 omit
14528 the space when the macro's definition is the empty string.
14529
14530 The Dwarf 2 spec says that there should be no spaces between the
14531 formal arguments in a function-like macro's formal argument list,
14532 but versions of GCC around March 2002 include spaces after the
14533 commas. */
14534
14535
14536 /* Find the extent of the macro name. The macro name is terminated
14537 by either a space or null character (for an object-like macro) or
14538 an opening paren (for a function-like macro). */
14539 for (p = body; *p; p++)
14540 if (*p == ' ' || *p == '(')
14541 break;
14542
14543 if (*p == ' ' || *p == '\0')
14544 {
14545 /* It's an object-like macro. */
14546 int name_len = p - body;
14547 char *name = copy_string (body, name_len);
14548 const char *replacement;
14549
14550 if (*p == ' ')
14551 replacement = body + name_len + 1;
14552 else
14553 {
14554 dwarf2_macro_malformed_definition_complaint (body);
14555 replacement = body + name_len;
14556 }
14557
14558 macro_define_object (file, line, name, replacement);
14559
14560 xfree (name);
14561 }
14562 else if (*p == '(')
14563 {
14564 /* It's a function-like macro. */
14565 char *name = copy_string (body, p - body);
14566 int argc = 0;
14567 int argv_size = 1;
14568 char **argv = xmalloc (argv_size * sizeof (*argv));
14569
14570 p++;
14571
14572 p = consume_improper_spaces (p, body);
14573
14574 /* Parse the formal argument list. */
14575 while (*p && *p != ')')
14576 {
14577 /* Find the extent of the current argument name. */
14578 const char *arg_start = p;
14579
14580 while (*p && *p != ',' && *p != ')' && *p != ' ')
14581 p++;
14582
14583 if (! *p || p == arg_start)
14584 dwarf2_macro_malformed_definition_complaint (body);
14585 else
14586 {
14587 /* Make sure argv has room for the new argument. */
14588 if (argc >= argv_size)
14589 {
14590 argv_size *= 2;
14591 argv = xrealloc (argv, argv_size * sizeof (*argv));
14592 }
14593
14594 argv[argc++] = copy_string (arg_start, p - arg_start);
14595 }
14596
14597 p = consume_improper_spaces (p, body);
14598
14599 /* Consume the comma, if present. */
14600 if (*p == ',')
14601 {
14602 p++;
14603
14604 p = consume_improper_spaces (p, body);
14605 }
14606 }
14607
14608 if (*p == ')')
14609 {
14610 p++;
14611
14612 if (*p == ' ')
14613 /* Perfectly formed definition, no complaints. */
14614 macro_define_function (file, line, name,
14615 argc, (const char **) argv,
14616 p + 1);
14617 else if (*p == '\0')
14618 {
14619 /* Complain, but do define it. */
14620 dwarf2_macro_malformed_definition_complaint (body);
14621 macro_define_function (file, line, name,
14622 argc, (const char **) argv,
14623 p);
14624 }
14625 else
14626 /* Just complain. */
14627 dwarf2_macro_malformed_definition_complaint (body);
14628 }
14629 else
14630 /* Just complain. */
14631 dwarf2_macro_malformed_definition_complaint (body);
14632
14633 xfree (name);
14634 {
14635 int i;
14636
14637 for (i = 0; i < argc; i++)
14638 xfree (argv[i]);
14639 }
14640 xfree (argv);
14641 }
14642 else
14643 dwarf2_macro_malformed_definition_complaint (body);
14644 }
14645
14646
14647 static void
14648 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14649 char *comp_dir, bfd *abfd,
14650 struct dwarf2_cu *cu)
14651 {
14652 gdb_byte *mac_ptr, *mac_end;
14653 struct macro_source_file *current_file = 0;
14654 enum dwarf_macinfo_record_type macinfo_type;
14655 int at_commandline;
14656
14657 dwarf2_read_section (dwarf2_per_objfile->objfile,
14658 &dwarf2_per_objfile->macinfo);
14659 if (dwarf2_per_objfile->macinfo.buffer == NULL)
14660 {
14661 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14662 return;
14663 }
14664
14665 /* First pass: Find the name of the base filename.
14666 This filename is needed in order to process all macros whose definition
14667 (or undefinition) comes from the command line. These macros are defined
14668 before the first DW_MACINFO_start_file entry, and yet still need to be
14669 associated to the base file.
14670
14671 To determine the base file name, we scan the macro definitions until we
14672 reach the first DW_MACINFO_start_file entry. We then initialize
14673 CURRENT_FILE accordingly so that any macro definition found before the
14674 first DW_MACINFO_start_file can still be associated to the base file. */
14675
14676 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14677 mac_end = dwarf2_per_objfile->macinfo.buffer
14678 + dwarf2_per_objfile->macinfo.size;
14679
14680 do
14681 {
14682 /* Do we at least have room for a macinfo type byte? */
14683 if (mac_ptr >= mac_end)
14684 {
14685 /* Complaint is printed during the second pass as GDB will probably
14686 stop the first pass earlier upon finding
14687 DW_MACINFO_start_file. */
14688 break;
14689 }
14690
14691 macinfo_type = read_1_byte (abfd, mac_ptr);
14692 mac_ptr++;
14693
14694 switch (macinfo_type)
14695 {
14696 /* A zero macinfo type indicates the end of the macro
14697 information. */
14698 case 0:
14699 break;
14700
14701 case DW_MACINFO_define:
14702 case DW_MACINFO_undef:
14703 /* Only skip the data by MAC_PTR. */
14704 {
14705 unsigned int bytes_read;
14706
14707 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14708 mac_ptr += bytes_read;
14709 read_direct_string (abfd, mac_ptr, &bytes_read);
14710 mac_ptr += bytes_read;
14711 }
14712 break;
14713
14714 case DW_MACINFO_start_file:
14715 {
14716 unsigned int bytes_read;
14717 int line, file;
14718
14719 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14720 mac_ptr += bytes_read;
14721 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14722 mac_ptr += bytes_read;
14723
14724 current_file = macro_start_file (file, line, current_file,
14725 comp_dir, lh, cu->objfile);
14726 }
14727 break;
14728
14729 case DW_MACINFO_end_file:
14730 /* No data to skip by MAC_PTR. */
14731 break;
14732
14733 case DW_MACINFO_vendor_ext:
14734 /* Only skip the data by MAC_PTR. */
14735 {
14736 unsigned int bytes_read;
14737
14738 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14739 mac_ptr += bytes_read;
14740 read_direct_string (abfd, mac_ptr, &bytes_read);
14741 mac_ptr += bytes_read;
14742 }
14743 break;
14744
14745 default:
14746 break;
14747 }
14748 } while (macinfo_type != 0 && current_file == NULL);
14749
14750 /* Second pass: Process all entries.
14751
14752 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14753 command-line macro definitions/undefinitions. This flag is unset when we
14754 reach the first DW_MACINFO_start_file entry. */
14755
14756 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14757
14758 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14759 GDB is still reading the definitions from command line. First
14760 DW_MACINFO_start_file will need to be ignored as it was already executed
14761 to create CURRENT_FILE for the main source holding also the command line
14762 definitions. On first met DW_MACINFO_start_file this flag is reset to
14763 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14764
14765 at_commandline = 1;
14766
14767 do
14768 {
14769 /* Do we at least have room for a macinfo type byte? */
14770 if (mac_ptr >= mac_end)
14771 {
14772 dwarf2_macros_too_long_complaint ();
14773 break;
14774 }
14775
14776 macinfo_type = read_1_byte (abfd, mac_ptr);
14777 mac_ptr++;
14778
14779 switch (macinfo_type)
14780 {
14781 /* A zero macinfo type indicates the end of the macro
14782 information. */
14783 case 0:
14784 break;
14785
14786 case DW_MACINFO_define:
14787 case DW_MACINFO_undef:
14788 {
14789 unsigned int bytes_read;
14790 int line;
14791 char *body;
14792
14793 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14794 mac_ptr += bytes_read;
14795 body = read_direct_string (abfd, mac_ptr, &bytes_read);
14796 mac_ptr += bytes_read;
14797
14798 if (! current_file)
14799 {
14800 /* DWARF violation as no main source is present. */
14801 complaint (&symfile_complaints,
14802 _("debug info with no main source gives macro %s "
14803 "on line %d: %s"),
14804 macinfo_type == DW_MACINFO_define ?
14805 _("definition") :
14806 macinfo_type == DW_MACINFO_undef ?
14807 _("undefinition") :
14808 _("something-or-other"), line, body);
14809 break;
14810 }
14811 if ((line == 0 && !at_commandline)
14812 || (line != 0 && at_commandline))
14813 complaint (&symfile_complaints,
14814 _("debug info gives %s macro %s with %s line %d: %s"),
14815 at_commandline ? _("command-line") : _("in-file"),
14816 macinfo_type == DW_MACINFO_define ?
14817 _("definition") :
14818 macinfo_type == DW_MACINFO_undef ?
14819 _("undefinition") :
14820 _("something-or-other"),
14821 line == 0 ? _("zero") : _("non-zero"), line, body);
14822
14823 if (macinfo_type == DW_MACINFO_define)
14824 parse_macro_definition (current_file, line, body);
14825 else if (macinfo_type == DW_MACINFO_undef)
14826 macro_undef (current_file, line, body);
14827 }
14828 break;
14829
14830 case DW_MACINFO_start_file:
14831 {
14832 unsigned int bytes_read;
14833 int line, file;
14834
14835 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14836 mac_ptr += bytes_read;
14837 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14838 mac_ptr += bytes_read;
14839
14840 if ((line == 0 && !at_commandline)
14841 || (line != 0 && at_commandline))
14842 complaint (&symfile_complaints,
14843 _("debug info gives source %d included "
14844 "from %s at %s line %d"),
14845 file, at_commandline ? _("command-line") : _("file"),
14846 line == 0 ? _("zero") : _("non-zero"), line);
14847
14848 if (at_commandline)
14849 {
14850 /* This DW_MACINFO_start_file was executed in the pass one. */
14851 at_commandline = 0;
14852 }
14853 else
14854 current_file = macro_start_file (file, line,
14855 current_file, comp_dir,
14856 lh, cu->objfile);
14857 }
14858 break;
14859
14860 case DW_MACINFO_end_file:
14861 if (! current_file)
14862 complaint (&symfile_complaints,
14863 _("macro debug info has an unmatched "
14864 "`close_file' directive"));
14865 else
14866 {
14867 current_file = current_file->included_by;
14868 if (! current_file)
14869 {
14870 enum dwarf_macinfo_record_type next_type;
14871
14872 /* GCC circa March 2002 doesn't produce the zero
14873 type byte marking the end of the compilation
14874 unit. Complain if it's not there, but exit no
14875 matter what. */
14876
14877 /* Do we at least have room for a macinfo type byte? */
14878 if (mac_ptr >= mac_end)
14879 {
14880 dwarf2_macros_too_long_complaint ();
14881 return;
14882 }
14883
14884 /* We don't increment mac_ptr here, so this is just
14885 a look-ahead. */
14886 next_type = read_1_byte (abfd, mac_ptr);
14887 if (next_type != 0)
14888 complaint (&symfile_complaints,
14889 _("no terminating 0-type entry for "
14890 "macros in `.debug_macinfo' section"));
14891
14892 return;
14893 }
14894 }
14895 break;
14896
14897 case DW_MACINFO_vendor_ext:
14898 {
14899 unsigned int bytes_read;
14900 int constant;
14901
14902 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14903 mac_ptr += bytes_read;
14904 read_direct_string (abfd, mac_ptr, &bytes_read);
14905 mac_ptr += bytes_read;
14906
14907 /* We don't recognize any vendor extensions. */
14908 }
14909 break;
14910 }
14911 } while (macinfo_type != 0);
14912 }
14913
14914 /* Check if the attribute's form is a DW_FORM_block*
14915 if so return true else false. */
14916 static int
14917 attr_form_is_block (struct attribute *attr)
14918 {
14919 return (attr == NULL ? 0 :
14920 attr->form == DW_FORM_block1
14921 || attr->form == DW_FORM_block2
14922 || attr->form == DW_FORM_block4
14923 || attr->form == DW_FORM_block
14924 || attr->form == DW_FORM_exprloc);
14925 }
14926
14927 /* Return non-zero if ATTR's value is a section offset --- classes
14928 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14929 You may use DW_UNSND (attr) to retrieve such offsets.
14930
14931 Section 7.5.4, "Attribute Encodings", explains that no attribute
14932 may have a value that belongs to more than one of these classes; it
14933 would be ambiguous if we did, because we use the same forms for all
14934 of them. */
14935 static int
14936 attr_form_is_section_offset (struct attribute *attr)
14937 {
14938 return (attr->form == DW_FORM_data4
14939 || attr->form == DW_FORM_data8
14940 || attr->form == DW_FORM_sec_offset);
14941 }
14942
14943
14944 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14945 zero otherwise. When this function returns true, you can apply
14946 dwarf2_get_attr_constant_value to it.
14947
14948 However, note that for some attributes you must check
14949 attr_form_is_section_offset before using this test. DW_FORM_data4
14950 and DW_FORM_data8 are members of both the constant class, and of
14951 the classes that contain offsets into other debug sections
14952 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14953 that, if an attribute's can be either a constant or one of the
14954 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14955 taken as section offsets, not constants. */
14956 static int
14957 attr_form_is_constant (struct attribute *attr)
14958 {
14959 switch (attr->form)
14960 {
14961 case DW_FORM_sdata:
14962 case DW_FORM_udata:
14963 case DW_FORM_data1:
14964 case DW_FORM_data2:
14965 case DW_FORM_data4:
14966 case DW_FORM_data8:
14967 return 1;
14968 default:
14969 return 0;
14970 }
14971 }
14972
14973 /* A helper function that fills in a dwarf2_loclist_baton. */
14974
14975 static void
14976 fill_in_loclist_baton (struct dwarf2_cu *cu,
14977 struct dwarf2_loclist_baton *baton,
14978 struct attribute *attr)
14979 {
14980 dwarf2_read_section (dwarf2_per_objfile->objfile,
14981 &dwarf2_per_objfile->loc);
14982
14983 baton->per_cu = cu->per_cu;
14984 gdb_assert (baton->per_cu);
14985 /* We don't know how long the location list is, but make sure we
14986 don't run off the edge of the section. */
14987 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14988 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14989 baton->base_address = cu->base_address;
14990 }
14991
14992 static void
14993 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14994 struct dwarf2_cu *cu)
14995 {
14996 if (attr_form_is_section_offset (attr)
14997 /* ".debug_loc" may not exist at all, or the offset may be outside
14998 the section. If so, fall through to the complaint in the
14999 other branch. */
15000 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15001 &dwarf2_per_objfile->loc))
15002 {
15003 struct dwarf2_loclist_baton *baton;
15004
15005 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15006 sizeof (struct dwarf2_loclist_baton));
15007
15008 fill_in_loclist_baton (cu, baton, attr);
15009
15010 if (cu->base_known == 0)
15011 complaint (&symfile_complaints,
15012 _("Location list used without "
15013 "specifying the CU base address."));
15014
15015 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15016 SYMBOL_LOCATION_BATON (sym) = baton;
15017 }
15018 else
15019 {
15020 struct dwarf2_locexpr_baton *baton;
15021
15022 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15023 sizeof (struct dwarf2_locexpr_baton));
15024 baton->per_cu = cu->per_cu;
15025 gdb_assert (baton->per_cu);
15026
15027 if (attr_form_is_block (attr))
15028 {
15029 /* Note that we're just copying the block's data pointer
15030 here, not the actual data. We're still pointing into the
15031 info_buffer for SYM's objfile; right now we never release
15032 that buffer, but when we do clean up properly this may
15033 need to change. */
15034 baton->size = DW_BLOCK (attr)->size;
15035 baton->data = DW_BLOCK (attr)->data;
15036 }
15037 else
15038 {
15039 dwarf2_invalid_attrib_class_complaint ("location description",
15040 SYMBOL_NATURAL_NAME (sym));
15041 baton->size = 0;
15042 baton->data = NULL;
15043 }
15044
15045 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15046 SYMBOL_LOCATION_BATON (sym) = baton;
15047 }
15048 }
15049
15050 /* Return the OBJFILE associated with the compilation unit CU. If CU
15051 came from a separate debuginfo file, then the master objfile is
15052 returned. */
15053
15054 struct objfile *
15055 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15056 {
15057 struct objfile *objfile = per_cu->objfile;
15058
15059 /* Return the master objfile, so that we can report and look up the
15060 correct file containing this variable. */
15061 if (objfile->separate_debug_objfile_backlink)
15062 objfile = objfile->separate_debug_objfile_backlink;
15063
15064 return objfile;
15065 }
15066
15067 /* Return the address size given in the compilation unit header for CU. */
15068
15069 CORE_ADDR
15070 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15071 {
15072 if (per_cu->cu)
15073 return per_cu->cu->header.addr_size;
15074 else
15075 {
15076 /* If the CU is not currently read in, we re-read its header. */
15077 struct objfile *objfile = per_cu->objfile;
15078 struct dwarf2_per_objfile *per_objfile
15079 = objfile_data (objfile, dwarf2_objfile_data_key);
15080 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15081 struct comp_unit_head cu_header;
15082
15083 memset (&cu_header, 0, sizeof cu_header);
15084 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15085 return cu_header.addr_size;
15086 }
15087 }
15088
15089 /* Return the offset size given in the compilation unit header for CU. */
15090
15091 int
15092 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15093 {
15094 if (per_cu->cu)
15095 return per_cu->cu->header.offset_size;
15096 else
15097 {
15098 /* If the CU is not currently read in, we re-read its header. */
15099 struct objfile *objfile = per_cu->objfile;
15100 struct dwarf2_per_objfile *per_objfile
15101 = objfile_data (objfile, dwarf2_objfile_data_key);
15102 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15103 struct comp_unit_head cu_header;
15104
15105 memset (&cu_header, 0, sizeof cu_header);
15106 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15107 return cu_header.offset_size;
15108 }
15109 }
15110
15111 /* Return the text offset of the CU. The returned offset comes from
15112 this CU's objfile. If this objfile came from a separate debuginfo
15113 file, then the offset may be different from the corresponding
15114 offset in the parent objfile. */
15115
15116 CORE_ADDR
15117 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15118 {
15119 struct objfile *objfile = per_cu->objfile;
15120
15121 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15122 }
15123
15124 /* Locate the .debug_info compilation unit from CU's objfile which contains
15125 the DIE at OFFSET. Raises an error on failure. */
15126
15127 static struct dwarf2_per_cu_data *
15128 dwarf2_find_containing_comp_unit (unsigned int offset,
15129 struct objfile *objfile)
15130 {
15131 struct dwarf2_per_cu_data *this_cu;
15132 int low, high;
15133
15134 low = 0;
15135 high = dwarf2_per_objfile->n_comp_units - 1;
15136 while (high > low)
15137 {
15138 int mid = low + (high - low) / 2;
15139
15140 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15141 high = mid;
15142 else
15143 low = mid + 1;
15144 }
15145 gdb_assert (low == high);
15146 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15147 {
15148 if (low == 0)
15149 error (_("Dwarf Error: could not find partial DIE containing "
15150 "offset 0x%lx [in module %s]"),
15151 (long) offset, bfd_get_filename (objfile->obfd));
15152
15153 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15154 return dwarf2_per_objfile->all_comp_units[low-1];
15155 }
15156 else
15157 {
15158 this_cu = dwarf2_per_objfile->all_comp_units[low];
15159 if (low == dwarf2_per_objfile->n_comp_units - 1
15160 && offset >= this_cu->offset + this_cu->length)
15161 error (_("invalid dwarf2 offset %u"), offset);
15162 gdb_assert (offset < this_cu->offset + this_cu->length);
15163 return this_cu;
15164 }
15165 }
15166
15167 /* Locate the compilation unit from OBJFILE which is located at exactly
15168 OFFSET. Raises an error on failure. */
15169
15170 static struct dwarf2_per_cu_data *
15171 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15172 {
15173 struct dwarf2_per_cu_data *this_cu;
15174
15175 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15176 if (this_cu->offset != offset)
15177 error (_("no compilation unit with offset %u."), offset);
15178 return this_cu;
15179 }
15180
15181 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
15182
15183 static void
15184 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15185 {
15186 memset (cu, 0, sizeof (*cu));
15187 cu->objfile = objfile;
15188 obstack_init (&cu->comp_unit_obstack);
15189 }
15190
15191 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15192
15193 static void
15194 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15195 {
15196 struct attribute *attr;
15197
15198 /* Set the language we're debugging. */
15199 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15200 if (attr)
15201 set_cu_language (DW_UNSND (attr), cu);
15202 else
15203 {
15204 cu->language = language_minimal;
15205 cu->language_defn = language_def (cu->language);
15206 }
15207 }
15208
15209 /* Release one cached compilation unit, CU. We unlink it from the tree
15210 of compilation units, but we don't remove it from the read_in_chain;
15211 the caller is responsible for that.
15212 NOTE: DATA is a void * because this function is also used as a
15213 cleanup routine. */
15214
15215 static void
15216 free_one_comp_unit (void *data)
15217 {
15218 struct dwarf2_cu *cu = data;
15219
15220 if (cu->per_cu != NULL)
15221 cu->per_cu->cu = NULL;
15222 cu->per_cu = NULL;
15223
15224 obstack_free (&cu->comp_unit_obstack, NULL);
15225
15226 xfree (cu);
15227 }
15228
15229 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15230 when we're finished with it. We can't free the pointer itself, but be
15231 sure to unlink it from the cache. Also release any associated storage
15232 and perform cache maintenance.
15233
15234 Only used during partial symbol parsing. */
15235
15236 static void
15237 free_stack_comp_unit (void *data)
15238 {
15239 struct dwarf2_cu *cu = data;
15240
15241 obstack_free (&cu->comp_unit_obstack, NULL);
15242 cu->partial_dies = NULL;
15243
15244 if (cu->per_cu != NULL)
15245 {
15246 /* This compilation unit is on the stack in our caller, so we
15247 should not xfree it. Just unlink it. */
15248 cu->per_cu->cu = NULL;
15249 cu->per_cu = NULL;
15250
15251 /* If we had a per-cu pointer, then we may have other compilation
15252 units loaded, so age them now. */
15253 age_cached_comp_units ();
15254 }
15255 }
15256
15257 /* Free all cached compilation units. */
15258
15259 static void
15260 free_cached_comp_units (void *data)
15261 {
15262 struct dwarf2_per_cu_data *per_cu, **last_chain;
15263
15264 per_cu = dwarf2_per_objfile->read_in_chain;
15265 last_chain = &dwarf2_per_objfile->read_in_chain;
15266 while (per_cu != NULL)
15267 {
15268 struct dwarf2_per_cu_data *next_cu;
15269
15270 next_cu = per_cu->cu->read_in_chain;
15271
15272 free_one_comp_unit (per_cu->cu);
15273 *last_chain = next_cu;
15274
15275 per_cu = next_cu;
15276 }
15277 }
15278
15279 /* Increase the age counter on each cached compilation unit, and free
15280 any that are too old. */
15281
15282 static void
15283 age_cached_comp_units (void)
15284 {
15285 struct dwarf2_per_cu_data *per_cu, **last_chain;
15286
15287 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15288 per_cu = dwarf2_per_objfile->read_in_chain;
15289 while (per_cu != NULL)
15290 {
15291 per_cu->cu->last_used ++;
15292 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15293 dwarf2_mark (per_cu->cu);
15294 per_cu = per_cu->cu->read_in_chain;
15295 }
15296
15297 per_cu = dwarf2_per_objfile->read_in_chain;
15298 last_chain = &dwarf2_per_objfile->read_in_chain;
15299 while (per_cu != NULL)
15300 {
15301 struct dwarf2_per_cu_data *next_cu;
15302
15303 next_cu = per_cu->cu->read_in_chain;
15304
15305 if (!per_cu->cu->mark)
15306 {
15307 free_one_comp_unit (per_cu->cu);
15308 *last_chain = next_cu;
15309 }
15310 else
15311 last_chain = &per_cu->cu->read_in_chain;
15312
15313 per_cu = next_cu;
15314 }
15315 }
15316
15317 /* Remove a single compilation unit from the cache. */
15318
15319 static void
15320 free_one_cached_comp_unit (void *target_cu)
15321 {
15322 struct dwarf2_per_cu_data *per_cu, **last_chain;
15323
15324 per_cu = dwarf2_per_objfile->read_in_chain;
15325 last_chain = &dwarf2_per_objfile->read_in_chain;
15326 while (per_cu != NULL)
15327 {
15328 struct dwarf2_per_cu_data *next_cu;
15329
15330 next_cu = per_cu->cu->read_in_chain;
15331
15332 if (per_cu->cu == target_cu)
15333 {
15334 free_one_comp_unit (per_cu->cu);
15335 *last_chain = next_cu;
15336 break;
15337 }
15338 else
15339 last_chain = &per_cu->cu->read_in_chain;
15340
15341 per_cu = next_cu;
15342 }
15343 }
15344
15345 /* Release all extra memory associated with OBJFILE. */
15346
15347 void
15348 dwarf2_free_objfile (struct objfile *objfile)
15349 {
15350 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15351
15352 if (dwarf2_per_objfile == NULL)
15353 return;
15354
15355 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15356 free_cached_comp_units (NULL);
15357
15358 if (dwarf2_per_objfile->quick_file_names_table)
15359 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15360
15361 /* Everything else should be on the objfile obstack. */
15362 }
15363
15364 /* A pair of DIE offset and GDB type pointer. We store these
15365 in a hash table separate from the DIEs, and preserve them
15366 when the DIEs are flushed out of cache. */
15367
15368 struct dwarf2_offset_and_type
15369 {
15370 unsigned int offset;
15371 struct type *type;
15372 };
15373
15374 /* Hash function for a dwarf2_offset_and_type. */
15375
15376 static hashval_t
15377 offset_and_type_hash (const void *item)
15378 {
15379 const struct dwarf2_offset_and_type *ofs = item;
15380
15381 return ofs->offset;
15382 }
15383
15384 /* Equality function for a dwarf2_offset_and_type. */
15385
15386 static int
15387 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15388 {
15389 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15390 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15391
15392 return ofs_lhs->offset == ofs_rhs->offset;
15393 }
15394
15395 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15396 table if necessary. For convenience, return TYPE.
15397
15398 The DIEs reading must have careful ordering to:
15399 * Not cause infite loops trying to read in DIEs as a prerequisite for
15400 reading current DIE.
15401 * Not trying to dereference contents of still incompletely read in types
15402 while reading in other DIEs.
15403 * Enable referencing still incompletely read in types just by a pointer to
15404 the type without accessing its fields.
15405
15406 Therefore caller should follow these rules:
15407 * Try to fetch any prerequisite types we may need to build this DIE type
15408 before building the type and calling set_die_type.
15409 * After building type call set_die_type for current DIE as soon as
15410 possible before fetching more types to complete the current type.
15411 * Make the type as complete as possible before fetching more types. */
15412
15413 static struct type *
15414 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15415 {
15416 struct dwarf2_offset_and_type **slot, ofs;
15417 struct objfile *objfile = cu->objfile;
15418 htab_t *type_hash_ptr;
15419
15420 /* For Ada types, make sure that the gnat-specific data is always
15421 initialized (if not already set). There are a few types where
15422 we should not be doing so, because the type-specific area is
15423 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15424 where the type-specific area is used to store the floatformat).
15425 But this is not a problem, because the gnat-specific information
15426 is actually not needed for these types. */
15427 if (need_gnat_info (cu)
15428 && TYPE_CODE (type) != TYPE_CODE_FUNC
15429 && TYPE_CODE (type) != TYPE_CODE_FLT
15430 && !HAVE_GNAT_AUX_INFO (type))
15431 INIT_GNAT_SPECIFIC (type);
15432
15433 if (cu->per_cu->debug_type_section)
15434 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15435 else
15436 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15437
15438 if (*type_hash_ptr == NULL)
15439 {
15440 *type_hash_ptr
15441 = htab_create_alloc_ex (127,
15442 offset_and_type_hash,
15443 offset_and_type_eq,
15444 NULL,
15445 &objfile->objfile_obstack,
15446 hashtab_obstack_allocate,
15447 dummy_obstack_deallocate);
15448 }
15449
15450 ofs.offset = die->offset;
15451 ofs.type = type;
15452 slot = (struct dwarf2_offset_and_type **)
15453 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15454 if (*slot)
15455 complaint (&symfile_complaints,
15456 _("A problem internal to GDB: DIE 0x%x has type already set"),
15457 die->offset);
15458 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15459 **slot = ofs;
15460 return type;
15461 }
15462
15463 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15464 table, or return NULL if the die does not have a saved type. */
15465
15466 static struct type *
15467 get_die_type_at_offset (unsigned int offset,
15468 struct dwarf2_per_cu_data *per_cu)
15469 {
15470 struct dwarf2_offset_and_type *slot, ofs;
15471 htab_t type_hash;
15472
15473 if (per_cu->debug_type_section)
15474 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15475 else
15476 type_hash = dwarf2_per_objfile->debug_info_type_hash;
15477 if (type_hash == NULL)
15478 return NULL;
15479
15480 ofs.offset = offset;
15481 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15482 if (slot)
15483 return slot->type;
15484 else
15485 return NULL;
15486 }
15487
15488 /* Look up the type for DIE in the appropriate type_hash table,
15489 or return NULL if DIE does not have a saved type. */
15490
15491 static struct type *
15492 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15493 {
15494 return get_die_type_at_offset (die->offset, cu->per_cu);
15495 }
15496
15497 /* Add a dependence relationship from CU to REF_PER_CU. */
15498
15499 static void
15500 dwarf2_add_dependence (struct dwarf2_cu *cu,
15501 struct dwarf2_per_cu_data *ref_per_cu)
15502 {
15503 void **slot;
15504
15505 if (cu->dependencies == NULL)
15506 cu->dependencies
15507 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15508 NULL, &cu->comp_unit_obstack,
15509 hashtab_obstack_allocate,
15510 dummy_obstack_deallocate);
15511
15512 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15513 if (*slot == NULL)
15514 *slot = ref_per_cu;
15515 }
15516
15517 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15518 Set the mark field in every compilation unit in the
15519 cache that we must keep because we are keeping CU. */
15520
15521 static int
15522 dwarf2_mark_helper (void **slot, void *data)
15523 {
15524 struct dwarf2_per_cu_data *per_cu;
15525
15526 per_cu = (struct dwarf2_per_cu_data *) *slot;
15527
15528 /* cu->dependencies references may not yet have been ever read if QUIT aborts
15529 reading of the chain. As such dependencies remain valid it is not much
15530 useful to track and undo them during QUIT cleanups. */
15531 if (per_cu->cu == NULL)
15532 return 1;
15533
15534 if (per_cu->cu->mark)
15535 return 1;
15536 per_cu->cu->mark = 1;
15537
15538 if (per_cu->cu->dependencies != NULL)
15539 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15540
15541 return 1;
15542 }
15543
15544 /* Set the mark field in CU and in every other compilation unit in the
15545 cache that we must keep because we are keeping CU. */
15546
15547 static void
15548 dwarf2_mark (struct dwarf2_cu *cu)
15549 {
15550 if (cu->mark)
15551 return;
15552 cu->mark = 1;
15553 if (cu->dependencies != NULL)
15554 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15555 }
15556
15557 static void
15558 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15559 {
15560 while (per_cu)
15561 {
15562 per_cu->cu->mark = 0;
15563 per_cu = per_cu->cu->read_in_chain;
15564 }
15565 }
15566
15567 /* Trivial hash function for partial_die_info: the hash value of a DIE
15568 is its offset in .debug_info for this objfile. */
15569
15570 static hashval_t
15571 partial_die_hash (const void *item)
15572 {
15573 const struct partial_die_info *part_die = item;
15574
15575 return part_die->offset;
15576 }
15577
15578 /* Trivial comparison function for partial_die_info structures: two DIEs
15579 are equal if they have the same offset. */
15580
15581 static int
15582 partial_die_eq (const void *item_lhs, const void *item_rhs)
15583 {
15584 const struct partial_die_info *part_die_lhs = item_lhs;
15585 const struct partial_die_info *part_die_rhs = item_rhs;
15586
15587 return part_die_lhs->offset == part_die_rhs->offset;
15588 }
15589
15590 static struct cmd_list_element *set_dwarf2_cmdlist;
15591 static struct cmd_list_element *show_dwarf2_cmdlist;
15592
15593 static void
15594 set_dwarf2_cmd (char *args, int from_tty)
15595 {
15596 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15597 }
15598
15599 static void
15600 show_dwarf2_cmd (char *args, int from_tty)
15601 {
15602 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15603 }
15604
15605 /* If section described by INFO was mmapped, munmap it now. */
15606
15607 static void
15608 munmap_section_buffer (struct dwarf2_section_info *info)
15609 {
15610 if (info->map_addr != NULL)
15611 {
15612 #ifdef HAVE_MMAP
15613 int res;
15614
15615 res = munmap (info->map_addr, info->map_len);
15616 gdb_assert (res == 0);
15617 #else
15618 /* Without HAVE_MMAP, we should never be here to begin with. */
15619 gdb_assert_not_reached ("no mmap support");
15620 #endif
15621 }
15622 }
15623
15624 /* munmap debug sections for OBJFILE, if necessary. */
15625
15626 static void
15627 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15628 {
15629 struct dwarf2_per_objfile *data = d;
15630 int ix;
15631 struct dwarf2_section_info *section;
15632
15633 /* This is sorted according to the order they're defined in to make it easier
15634 to keep in sync. */
15635 munmap_section_buffer (&data->info);
15636 munmap_section_buffer (&data->abbrev);
15637 munmap_section_buffer (&data->line);
15638 munmap_section_buffer (&data->loc);
15639 munmap_section_buffer (&data->macinfo);
15640 munmap_section_buffer (&data->str);
15641 munmap_section_buffer (&data->ranges);
15642 munmap_section_buffer (&data->frame);
15643 munmap_section_buffer (&data->eh_frame);
15644 munmap_section_buffer (&data->gdb_index);
15645
15646 for (ix = 0;
15647 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
15648 ++ix)
15649 munmap_section_buffer (section);
15650
15651 VEC_free (dwarf2_section_info_def, data->types);
15652 }
15653
15654 \f
15655 /* The "save gdb-index" command. */
15656
15657 /* The contents of the hash table we create when building the string
15658 table. */
15659 struct strtab_entry
15660 {
15661 offset_type offset;
15662 const char *str;
15663 };
15664
15665 /* Hash function for a strtab_entry.
15666
15667 Function is used only during write_hash_table so no index format backward
15668 compatibility is needed. */
15669
15670 static hashval_t
15671 hash_strtab_entry (const void *e)
15672 {
15673 const struct strtab_entry *entry = e;
15674 return mapped_index_string_hash (INT_MAX, entry->str);
15675 }
15676
15677 /* Equality function for a strtab_entry. */
15678
15679 static int
15680 eq_strtab_entry (const void *a, const void *b)
15681 {
15682 const struct strtab_entry *ea = a;
15683 const struct strtab_entry *eb = b;
15684 return !strcmp (ea->str, eb->str);
15685 }
15686
15687 /* Create a strtab_entry hash table. */
15688
15689 static htab_t
15690 create_strtab (void)
15691 {
15692 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15693 xfree, xcalloc, xfree);
15694 }
15695
15696 /* Add a string to the constant pool. Return the string's offset in
15697 host order. */
15698
15699 static offset_type
15700 add_string (htab_t table, struct obstack *cpool, const char *str)
15701 {
15702 void **slot;
15703 struct strtab_entry entry;
15704 struct strtab_entry *result;
15705
15706 entry.str = str;
15707 slot = htab_find_slot (table, &entry, INSERT);
15708 if (*slot)
15709 result = *slot;
15710 else
15711 {
15712 result = XNEW (struct strtab_entry);
15713 result->offset = obstack_object_size (cpool);
15714 result->str = str;
15715 obstack_grow_str0 (cpool, str);
15716 *slot = result;
15717 }
15718 return result->offset;
15719 }
15720
15721 /* An entry in the symbol table. */
15722 struct symtab_index_entry
15723 {
15724 /* The name of the symbol. */
15725 const char *name;
15726 /* The offset of the name in the constant pool. */
15727 offset_type index_offset;
15728 /* A sorted vector of the indices of all the CUs that hold an object
15729 of this name. */
15730 VEC (offset_type) *cu_indices;
15731 };
15732
15733 /* The symbol table. This is a power-of-2-sized hash table. */
15734 struct mapped_symtab
15735 {
15736 offset_type n_elements;
15737 offset_type size;
15738 struct symtab_index_entry **data;
15739 };
15740
15741 /* Hash function for a symtab_index_entry. */
15742
15743 static hashval_t
15744 hash_symtab_entry (const void *e)
15745 {
15746 const struct symtab_index_entry *entry = e;
15747 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15748 sizeof (offset_type) * VEC_length (offset_type,
15749 entry->cu_indices),
15750 0);
15751 }
15752
15753 /* Equality function for a symtab_index_entry. */
15754
15755 static int
15756 eq_symtab_entry (const void *a, const void *b)
15757 {
15758 const struct symtab_index_entry *ea = a;
15759 const struct symtab_index_entry *eb = b;
15760 int len = VEC_length (offset_type, ea->cu_indices);
15761 if (len != VEC_length (offset_type, eb->cu_indices))
15762 return 0;
15763 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15764 VEC_address (offset_type, eb->cu_indices),
15765 sizeof (offset_type) * len);
15766 }
15767
15768 /* Destroy a symtab_index_entry. */
15769
15770 static void
15771 delete_symtab_entry (void *p)
15772 {
15773 struct symtab_index_entry *entry = p;
15774 VEC_free (offset_type, entry->cu_indices);
15775 xfree (entry);
15776 }
15777
15778 /* Create a hash table holding symtab_index_entry objects. */
15779
15780 static htab_t
15781 create_symbol_hash_table (void)
15782 {
15783 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15784 delete_symtab_entry, xcalloc, xfree);
15785 }
15786
15787 /* Create a new mapped symtab object. */
15788
15789 static struct mapped_symtab *
15790 create_mapped_symtab (void)
15791 {
15792 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15793 symtab->n_elements = 0;
15794 symtab->size = 1024;
15795 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15796 return symtab;
15797 }
15798
15799 /* Destroy a mapped_symtab. */
15800
15801 static void
15802 cleanup_mapped_symtab (void *p)
15803 {
15804 struct mapped_symtab *symtab = p;
15805 /* The contents of the array are freed when the other hash table is
15806 destroyed. */
15807 xfree (symtab->data);
15808 xfree (symtab);
15809 }
15810
15811 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15812 the slot.
15813
15814 Function is used only during write_hash_table so no index format backward
15815 compatibility is needed. */
15816
15817 static struct symtab_index_entry **
15818 find_slot (struct mapped_symtab *symtab, const char *name)
15819 {
15820 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15821
15822 index = hash & (symtab->size - 1);
15823 step = ((hash * 17) & (symtab->size - 1)) | 1;
15824
15825 for (;;)
15826 {
15827 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15828 return &symtab->data[index];
15829 index = (index + step) & (symtab->size - 1);
15830 }
15831 }
15832
15833 /* Expand SYMTAB's hash table. */
15834
15835 static void
15836 hash_expand (struct mapped_symtab *symtab)
15837 {
15838 offset_type old_size = symtab->size;
15839 offset_type i;
15840 struct symtab_index_entry **old_entries = symtab->data;
15841
15842 symtab->size *= 2;
15843 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15844
15845 for (i = 0; i < old_size; ++i)
15846 {
15847 if (old_entries[i])
15848 {
15849 struct symtab_index_entry **slot = find_slot (symtab,
15850 old_entries[i]->name);
15851 *slot = old_entries[i];
15852 }
15853 }
15854
15855 xfree (old_entries);
15856 }
15857
15858 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15859 is the index of the CU in which the symbol appears. */
15860
15861 static void
15862 add_index_entry (struct mapped_symtab *symtab, const char *name,
15863 offset_type cu_index)
15864 {
15865 struct symtab_index_entry **slot;
15866
15867 ++symtab->n_elements;
15868 if (4 * symtab->n_elements / 3 >= symtab->size)
15869 hash_expand (symtab);
15870
15871 slot = find_slot (symtab, name);
15872 if (!*slot)
15873 {
15874 *slot = XNEW (struct symtab_index_entry);
15875 (*slot)->name = name;
15876 (*slot)->cu_indices = NULL;
15877 }
15878 /* Don't push an index twice. Due to how we add entries we only
15879 have to check the last one. */
15880 if (VEC_empty (offset_type, (*slot)->cu_indices)
15881 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
15882 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15883 }
15884
15885 /* Add a vector of indices to the constant pool. */
15886
15887 static offset_type
15888 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15889 struct symtab_index_entry *entry)
15890 {
15891 void **slot;
15892
15893 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15894 if (!*slot)
15895 {
15896 offset_type len = VEC_length (offset_type, entry->cu_indices);
15897 offset_type val = MAYBE_SWAP (len);
15898 offset_type iter;
15899 int i;
15900
15901 *slot = entry;
15902 entry->index_offset = obstack_object_size (cpool);
15903
15904 obstack_grow (cpool, &val, sizeof (val));
15905 for (i = 0;
15906 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15907 ++i)
15908 {
15909 val = MAYBE_SWAP (iter);
15910 obstack_grow (cpool, &val, sizeof (val));
15911 }
15912 }
15913 else
15914 {
15915 struct symtab_index_entry *old_entry = *slot;
15916 entry->index_offset = old_entry->index_offset;
15917 entry = old_entry;
15918 }
15919 return entry->index_offset;
15920 }
15921
15922 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15923 constant pool entries going into the obstack CPOOL. */
15924
15925 static void
15926 write_hash_table (struct mapped_symtab *symtab,
15927 struct obstack *output, struct obstack *cpool)
15928 {
15929 offset_type i;
15930 htab_t symbol_hash_table;
15931 htab_t str_table;
15932
15933 symbol_hash_table = create_symbol_hash_table ();
15934 str_table = create_strtab ();
15935
15936 /* We add all the index vectors to the constant pool first, to
15937 ensure alignment is ok. */
15938 for (i = 0; i < symtab->size; ++i)
15939 {
15940 if (symtab->data[i])
15941 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15942 }
15943
15944 /* Now write out the hash table. */
15945 for (i = 0; i < symtab->size; ++i)
15946 {
15947 offset_type str_off, vec_off;
15948
15949 if (symtab->data[i])
15950 {
15951 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15952 vec_off = symtab->data[i]->index_offset;
15953 }
15954 else
15955 {
15956 /* While 0 is a valid constant pool index, it is not valid
15957 to have 0 for both offsets. */
15958 str_off = 0;
15959 vec_off = 0;
15960 }
15961
15962 str_off = MAYBE_SWAP (str_off);
15963 vec_off = MAYBE_SWAP (vec_off);
15964
15965 obstack_grow (output, &str_off, sizeof (str_off));
15966 obstack_grow (output, &vec_off, sizeof (vec_off));
15967 }
15968
15969 htab_delete (str_table);
15970 htab_delete (symbol_hash_table);
15971 }
15972
15973 /* Struct to map psymtab to CU index in the index file. */
15974 struct psymtab_cu_index_map
15975 {
15976 struct partial_symtab *psymtab;
15977 unsigned int cu_index;
15978 };
15979
15980 static hashval_t
15981 hash_psymtab_cu_index (const void *item)
15982 {
15983 const struct psymtab_cu_index_map *map = item;
15984
15985 return htab_hash_pointer (map->psymtab);
15986 }
15987
15988 static int
15989 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15990 {
15991 const struct psymtab_cu_index_map *lhs = item_lhs;
15992 const struct psymtab_cu_index_map *rhs = item_rhs;
15993
15994 return lhs->psymtab == rhs->psymtab;
15995 }
15996
15997 /* Helper struct for building the address table. */
15998 struct addrmap_index_data
15999 {
16000 struct objfile *objfile;
16001 struct obstack *addr_obstack;
16002 htab_t cu_index_htab;
16003
16004 /* Non-zero if the previous_* fields are valid.
16005 We can't write an entry until we see the next entry (since it is only then
16006 that we know the end of the entry). */
16007 int previous_valid;
16008 /* Index of the CU in the table of all CUs in the index file. */
16009 unsigned int previous_cu_index;
16010 /* Start address of the CU. */
16011 CORE_ADDR previous_cu_start;
16012 };
16013
16014 /* Write an address entry to OBSTACK. */
16015
16016 static void
16017 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16018 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16019 {
16020 offset_type cu_index_to_write;
16021 char addr[8];
16022 CORE_ADDR baseaddr;
16023
16024 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16025
16026 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16027 obstack_grow (obstack, addr, 8);
16028 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16029 obstack_grow (obstack, addr, 8);
16030 cu_index_to_write = MAYBE_SWAP (cu_index);
16031 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16032 }
16033
16034 /* Worker function for traversing an addrmap to build the address table. */
16035
16036 static int
16037 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16038 {
16039 struct addrmap_index_data *data = datap;
16040 struct partial_symtab *pst = obj;
16041 offset_type cu_index;
16042 void **slot;
16043
16044 if (data->previous_valid)
16045 add_address_entry (data->objfile, data->addr_obstack,
16046 data->previous_cu_start, start_addr,
16047 data->previous_cu_index);
16048
16049 data->previous_cu_start = start_addr;
16050 if (pst != NULL)
16051 {
16052 struct psymtab_cu_index_map find_map, *map;
16053 find_map.psymtab = pst;
16054 map = htab_find (data->cu_index_htab, &find_map);
16055 gdb_assert (map != NULL);
16056 data->previous_cu_index = map->cu_index;
16057 data->previous_valid = 1;
16058 }
16059 else
16060 data->previous_valid = 0;
16061
16062 return 0;
16063 }
16064
16065 /* Write OBJFILE's address map to OBSTACK.
16066 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16067 in the index file. */
16068
16069 static void
16070 write_address_map (struct objfile *objfile, struct obstack *obstack,
16071 htab_t cu_index_htab)
16072 {
16073 struct addrmap_index_data addrmap_index_data;
16074
16075 /* When writing the address table, we have to cope with the fact that
16076 the addrmap iterator only provides the start of a region; we have to
16077 wait until the next invocation to get the start of the next region. */
16078
16079 addrmap_index_data.objfile = objfile;
16080 addrmap_index_data.addr_obstack = obstack;
16081 addrmap_index_data.cu_index_htab = cu_index_htab;
16082 addrmap_index_data.previous_valid = 0;
16083
16084 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16085 &addrmap_index_data);
16086
16087 /* It's highly unlikely the last entry (end address = 0xff...ff)
16088 is valid, but we should still handle it.
16089 The end address is recorded as the start of the next region, but that
16090 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16091 anyway. */
16092 if (addrmap_index_data.previous_valid)
16093 add_address_entry (objfile, obstack,
16094 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16095 addrmap_index_data.previous_cu_index);
16096 }
16097
16098 /* Add a list of partial symbols to SYMTAB. */
16099
16100 static void
16101 write_psymbols (struct mapped_symtab *symtab,
16102 htab_t psyms_seen,
16103 struct partial_symbol **psymp,
16104 int count,
16105 offset_type cu_index,
16106 int is_static)
16107 {
16108 for (; count-- > 0; ++psymp)
16109 {
16110 void **slot, *lookup;
16111
16112 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16113 error (_("Ada is not currently supported by the index"));
16114
16115 /* We only want to add a given psymbol once. However, we also
16116 want to account for whether it is global or static. So, we
16117 may add it twice, using slightly different values. */
16118 if (is_static)
16119 {
16120 uintptr_t val = 1 | (uintptr_t) *psymp;
16121
16122 lookup = (void *) val;
16123 }
16124 else
16125 lookup = *psymp;
16126
16127 /* Only add a given psymbol once. */
16128 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16129 if (!*slot)
16130 {
16131 *slot = lookup;
16132 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16133 }
16134 }
16135 }
16136
16137 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16138 exception if there is an error. */
16139
16140 static void
16141 write_obstack (FILE *file, struct obstack *obstack)
16142 {
16143 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16144 file)
16145 != obstack_object_size (obstack))
16146 error (_("couldn't data write to file"));
16147 }
16148
16149 /* Unlink a file if the argument is not NULL. */
16150
16151 static void
16152 unlink_if_set (void *p)
16153 {
16154 char **filename = p;
16155 if (*filename)
16156 unlink (*filename);
16157 }
16158
16159 /* A helper struct used when iterating over debug_types. */
16160 struct signatured_type_index_data
16161 {
16162 struct objfile *objfile;
16163 struct mapped_symtab *symtab;
16164 struct obstack *types_list;
16165 htab_t psyms_seen;
16166 int cu_index;
16167 };
16168
16169 /* A helper function that writes a single signatured_type to an
16170 obstack. */
16171
16172 static int
16173 write_one_signatured_type (void **slot, void *d)
16174 {
16175 struct signatured_type_index_data *info = d;
16176 struct signatured_type *entry = (struct signatured_type *) *slot;
16177 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16178 struct partial_symtab *psymtab = per_cu->v.psymtab;
16179 gdb_byte val[8];
16180
16181 write_psymbols (info->symtab,
16182 info->psyms_seen,
16183 info->objfile->global_psymbols.list
16184 + psymtab->globals_offset,
16185 psymtab->n_global_syms, info->cu_index,
16186 0);
16187 write_psymbols (info->symtab,
16188 info->psyms_seen,
16189 info->objfile->static_psymbols.list
16190 + psymtab->statics_offset,
16191 psymtab->n_static_syms, info->cu_index,
16192 1);
16193
16194 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16195 obstack_grow (info->types_list, val, 8);
16196 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16197 obstack_grow (info->types_list, val, 8);
16198 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16199 obstack_grow (info->types_list, val, 8);
16200
16201 ++info->cu_index;
16202
16203 return 1;
16204 }
16205
16206 /* A cleanup function for an htab_t. */
16207
16208 static void
16209 cleanup_htab (void *arg)
16210 {
16211 htab_delete (arg);
16212 }
16213
16214 /* Create an index file for OBJFILE in the directory DIR. */
16215
16216 static void
16217 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16218 {
16219 struct cleanup *cleanup;
16220 char *filename, *cleanup_filename;
16221 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16222 struct obstack cu_list, types_cu_list;
16223 int i;
16224 FILE *out_file;
16225 struct mapped_symtab *symtab;
16226 offset_type val, size_of_contents, total_len;
16227 struct stat st;
16228 char buf[8];
16229 htab_t psyms_seen;
16230 htab_t cu_index_htab;
16231 struct psymtab_cu_index_map *psymtab_cu_index_map;
16232
16233 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16234 return;
16235
16236 if (dwarf2_per_objfile->using_index)
16237 error (_("Cannot use an index to create the index"));
16238
16239 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16240 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16241
16242 if (stat (objfile->name, &st) < 0)
16243 perror_with_name (objfile->name);
16244
16245 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16246 INDEX_SUFFIX, (char *) NULL);
16247 cleanup = make_cleanup (xfree, filename);
16248
16249 out_file = fopen (filename, "wb");
16250 if (!out_file)
16251 error (_("Can't open `%s' for writing"), filename);
16252
16253 cleanup_filename = filename;
16254 make_cleanup (unlink_if_set, &cleanup_filename);
16255
16256 symtab = create_mapped_symtab ();
16257 make_cleanup (cleanup_mapped_symtab, symtab);
16258
16259 obstack_init (&addr_obstack);
16260 make_cleanup_obstack_free (&addr_obstack);
16261
16262 obstack_init (&cu_list);
16263 make_cleanup_obstack_free (&cu_list);
16264
16265 obstack_init (&types_cu_list);
16266 make_cleanup_obstack_free (&types_cu_list);
16267
16268 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16269 NULL, xcalloc, xfree);
16270 make_cleanup (cleanup_htab, psyms_seen);
16271
16272 /* While we're scanning CU's create a table that maps a psymtab pointer
16273 (which is what addrmap records) to its index (which is what is recorded
16274 in the index file). This will later be needed to write the address
16275 table. */
16276 cu_index_htab = htab_create_alloc (100,
16277 hash_psymtab_cu_index,
16278 eq_psymtab_cu_index,
16279 NULL, xcalloc, xfree);
16280 make_cleanup (cleanup_htab, cu_index_htab);
16281 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16282 xmalloc (sizeof (struct psymtab_cu_index_map)
16283 * dwarf2_per_objfile->n_comp_units);
16284 make_cleanup (xfree, psymtab_cu_index_map);
16285
16286 /* The CU list is already sorted, so we don't need to do additional
16287 work here. Also, the debug_types entries do not appear in
16288 all_comp_units, but only in their own hash table. */
16289 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16290 {
16291 struct dwarf2_per_cu_data *per_cu
16292 = dwarf2_per_objfile->all_comp_units[i];
16293 struct partial_symtab *psymtab = per_cu->v.psymtab;
16294 gdb_byte val[8];
16295 struct psymtab_cu_index_map *map;
16296 void **slot;
16297
16298 write_psymbols (symtab,
16299 psyms_seen,
16300 objfile->global_psymbols.list + psymtab->globals_offset,
16301 psymtab->n_global_syms, i,
16302 0);
16303 write_psymbols (symtab,
16304 psyms_seen,
16305 objfile->static_psymbols.list + psymtab->statics_offset,
16306 psymtab->n_static_syms, i,
16307 1);
16308
16309 map = &psymtab_cu_index_map[i];
16310 map->psymtab = psymtab;
16311 map->cu_index = i;
16312 slot = htab_find_slot (cu_index_htab, map, INSERT);
16313 gdb_assert (slot != NULL);
16314 gdb_assert (*slot == NULL);
16315 *slot = map;
16316
16317 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16318 obstack_grow (&cu_list, val, 8);
16319 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16320 obstack_grow (&cu_list, val, 8);
16321 }
16322
16323 /* Dump the address map. */
16324 write_address_map (objfile, &addr_obstack, cu_index_htab);
16325
16326 /* Write out the .debug_type entries, if any. */
16327 if (dwarf2_per_objfile->signatured_types)
16328 {
16329 struct signatured_type_index_data sig_data;
16330
16331 sig_data.objfile = objfile;
16332 sig_data.symtab = symtab;
16333 sig_data.types_list = &types_cu_list;
16334 sig_data.psyms_seen = psyms_seen;
16335 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16336 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16337 write_one_signatured_type, &sig_data);
16338 }
16339
16340 obstack_init (&constant_pool);
16341 make_cleanup_obstack_free (&constant_pool);
16342 obstack_init (&symtab_obstack);
16343 make_cleanup_obstack_free (&symtab_obstack);
16344 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16345
16346 obstack_init (&contents);
16347 make_cleanup_obstack_free (&contents);
16348 size_of_contents = 6 * sizeof (offset_type);
16349 total_len = size_of_contents;
16350
16351 /* The version number. */
16352 val = MAYBE_SWAP (5);
16353 obstack_grow (&contents, &val, sizeof (val));
16354
16355 /* The offset of the CU list from the start of the file. */
16356 val = MAYBE_SWAP (total_len);
16357 obstack_grow (&contents, &val, sizeof (val));
16358 total_len += obstack_object_size (&cu_list);
16359
16360 /* The offset of the types CU list from the start of the file. */
16361 val = MAYBE_SWAP (total_len);
16362 obstack_grow (&contents, &val, sizeof (val));
16363 total_len += obstack_object_size (&types_cu_list);
16364
16365 /* The offset of the address table from the start of the file. */
16366 val = MAYBE_SWAP (total_len);
16367 obstack_grow (&contents, &val, sizeof (val));
16368 total_len += obstack_object_size (&addr_obstack);
16369
16370 /* The offset of the symbol table from the start of the file. */
16371 val = MAYBE_SWAP (total_len);
16372 obstack_grow (&contents, &val, sizeof (val));
16373 total_len += obstack_object_size (&symtab_obstack);
16374
16375 /* The offset of the constant pool from the start of the file. */
16376 val = MAYBE_SWAP (total_len);
16377 obstack_grow (&contents, &val, sizeof (val));
16378 total_len += obstack_object_size (&constant_pool);
16379
16380 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16381
16382 write_obstack (out_file, &contents);
16383 write_obstack (out_file, &cu_list);
16384 write_obstack (out_file, &types_cu_list);
16385 write_obstack (out_file, &addr_obstack);
16386 write_obstack (out_file, &symtab_obstack);
16387 write_obstack (out_file, &constant_pool);
16388
16389 fclose (out_file);
16390
16391 /* We want to keep the file, so we set cleanup_filename to NULL
16392 here. See unlink_if_set. */
16393 cleanup_filename = NULL;
16394
16395 do_cleanups (cleanup);
16396 }
16397
16398 /* Implementation of the `save gdb-index' command.
16399
16400 Note that the file format used by this command is documented in the
16401 GDB manual. Any changes here must be documented there. */
16402
16403 static void
16404 save_gdb_index_command (char *arg, int from_tty)
16405 {
16406 struct objfile *objfile;
16407
16408 if (!arg || !*arg)
16409 error (_("usage: save gdb-index DIRECTORY"));
16410
16411 ALL_OBJFILES (objfile)
16412 {
16413 struct stat st;
16414
16415 /* If the objfile does not correspond to an actual file, skip it. */
16416 if (stat (objfile->name, &st) < 0)
16417 continue;
16418
16419 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16420 if (dwarf2_per_objfile)
16421 {
16422 volatile struct gdb_exception except;
16423
16424 TRY_CATCH (except, RETURN_MASK_ERROR)
16425 {
16426 write_psymtabs_to_index (objfile, arg);
16427 }
16428 if (except.reason < 0)
16429 exception_fprintf (gdb_stderr, except,
16430 _("Error while writing index for `%s': "),
16431 objfile->name);
16432 }
16433 }
16434 }
16435
16436 \f
16437
16438 int dwarf2_always_disassemble;
16439
16440 static void
16441 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16442 struct cmd_list_element *c, const char *value)
16443 {
16444 fprintf_filtered (file,
16445 _("Whether to always disassemble "
16446 "DWARF expressions is %s.\n"),
16447 value);
16448 }
16449
16450 static void
16451 show_check_physname (struct ui_file *file, int from_tty,
16452 struct cmd_list_element *c, const char *value)
16453 {
16454 fprintf_filtered (file,
16455 _("Whether to check \"physname\" is %s.\n"),
16456 value);
16457 }
16458
16459 void _initialize_dwarf2_read (void);
16460
16461 void
16462 _initialize_dwarf2_read (void)
16463 {
16464 struct cmd_list_element *c;
16465
16466 dwarf2_objfile_data_key
16467 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16468
16469 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16470 Set DWARF 2 specific variables.\n\
16471 Configure DWARF 2 variables such as the cache size"),
16472 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16473 0/*allow-unknown*/, &maintenance_set_cmdlist);
16474
16475 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16476 Show DWARF 2 specific variables\n\
16477 Show DWARF 2 variables such as the cache size"),
16478 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16479 0/*allow-unknown*/, &maintenance_show_cmdlist);
16480
16481 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16482 &dwarf2_max_cache_age, _("\
16483 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16484 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16485 A higher limit means that cached compilation units will be stored\n\
16486 in memory longer, and more total memory will be used. Zero disables\n\
16487 caching, which can slow down startup."),
16488 NULL,
16489 show_dwarf2_max_cache_age,
16490 &set_dwarf2_cmdlist,
16491 &show_dwarf2_cmdlist);
16492
16493 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16494 &dwarf2_always_disassemble, _("\
16495 Set whether `info address' always disassembles DWARF expressions."), _("\
16496 Show whether `info address' always disassembles DWARF expressions."), _("\
16497 When enabled, DWARF expressions are always printed in an assembly-like\n\
16498 syntax. When disabled, expressions will be printed in a more\n\
16499 conversational style, when possible."),
16500 NULL,
16501 show_dwarf2_always_disassemble,
16502 &set_dwarf2_cmdlist,
16503 &show_dwarf2_cmdlist);
16504
16505 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16506 Set debugging of the dwarf2 DIE reader."), _("\
16507 Show debugging of the dwarf2 DIE reader."), _("\
16508 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16509 The value is the maximum depth to print."),
16510 NULL,
16511 NULL,
16512 &setdebuglist, &showdebuglist);
16513
16514 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16515 Set cross-checking of \"physname\" code against demangler."), _("\
16516 Show cross-checking of \"physname\" code against demangler."), _("\
16517 When enabled, GDB's internal \"physname\" code is checked against\n\
16518 the demangler."),
16519 NULL, show_check_physname,
16520 &setdebuglist, &showdebuglist);
16521
16522 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16523 _("\
16524 Save a gdb-index file.\n\
16525 Usage: save gdb-index DIRECTORY"),
16526 &save_cmdlist);
16527 set_cmd_completer (c, filename_completer);
16528 }
This page took 0.376405 seconds and 4 git commands to generate.