* dwarf2read.c (dwarf2_read_index): Don't use index if symbol table
[binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "symtab.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h" /* for DOSish file names */
42 #include "macrotab.h"
43 #include "language.h"
44 #include "complaints.h"
45 #include "bcache.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
49 #include "hashtab.h"
50 #include "command.h"
51 #include "gdbcmd.h"
52 #include "block.h"
53 #include "addrmap.h"
54 #include "typeprint.h"
55 #include "jv-lang.h"
56 #include "psympriv.h"
57 #include "exceptions.h"
58 #include "gdb_stat.h"
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "go-lang.h"
63 #include "valprint.h"
64 #include <ctype.h>
65
66 #include <fcntl.h>
67 #include "gdb_string.h"
68 #include "gdb_assert.h"
69 #include <sys/types.h>
70 #ifdef HAVE_ZLIB_H
71 #include <zlib.h>
72 #endif
73 #ifdef HAVE_MMAP
74 #include <sys/mman.h>
75 #ifndef MAP_FAILED
76 #define MAP_FAILED ((void *) -1)
77 #endif
78 #endif
79
80 typedef struct symbol *symbolp;
81 DEF_VEC_P (symbolp);
82
83 /* When non-zero, dump DIEs after they are read in. */
84 static int dwarf2_die_debug = 0;
85
86 /* When non-zero, cross-check physname against demangler. */
87 static int check_physname = 0;
88
89 /* When non-zero, do not reject deprecated .gdb_index sections. */
90 int use_deprecated_index_sections = 0;
91
92 static int pagesize;
93
94 /* When set, the file that we're processing is known to have debugging
95 info for C++ namespaces. GCC 3.3.x did not produce this information,
96 but later versions do. */
97
98 static int processing_has_namespace_info;
99
100 static const struct objfile_data *dwarf2_objfile_data_key;
101
102 struct dwarf2_section_info
103 {
104 asection *asection;
105 gdb_byte *buffer;
106 bfd_size_type size;
107 /* Not NULL if the section was actually mmapped. */
108 void *map_addr;
109 /* Page aligned size of mmapped area. */
110 bfd_size_type map_len;
111 /* True if we have tried to read this section. */
112 int readin;
113 };
114
115 typedef struct dwarf2_section_info dwarf2_section_info_def;
116 DEF_VEC_O (dwarf2_section_info_def);
117
118 /* All offsets in the index are of this type. It must be
119 architecture-independent. */
120 typedef uint32_t offset_type;
121
122 DEF_VEC_I (offset_type);
123
124 /* A description of the mapped index. The file format is described in
125 a comment by the code that writes the index. */
126 struct mapped_index
127 {
128 /* Index data format version. */
129 int version;
130
131 /* The total length of the buffer. */
132 off_t total_size;
133
134 /* A pointer to the address table data. */
135 const gdb_byte *address_table;
136
137 /* Size of the address table data in bytes. */
138 offset_type address_table_size;
139
140 /* The symbol table, implemented as a hash table. */
141 const offset_type *symbol_table;
142
143 /* Size in slots, each slot is 2 offset_types. */
144 offset_type symbol_table_slots;
145
146 /* A pointer to the constant pool. */
147 const char *constant_pool;
148 };
149
150 /* Collection of data recorded per objfile.
151 This hangs off of dwarf2_objfile_data_key. */
152
153 struct dwarf2_per_objfile
154 {
155 struct dwarf2_section_info info;
156 struct dwarf2_section_info abbrev;
157 struct dwarf2_section_info line;
158 struct dwarf2_section_info loc;
159 struct dwarf2_section_info macinfo;
160 struct dwarf2_section_info macro;
161 struct dwarf2_section_info str;
162 struct dwarf2_section_info ranges;
163 struct dwarf2_section_info frame;
164 struct dwarf2_section_info eh_frame;
165 struct dwarf2_section_info gdb_index;
166
167 VEC (dwarf2_section_info_def) *types;
168
169 /* Back link. */
170 struct objfile *objfile;
171
172 /* Table of all the compilation units. This is used to locate
173 the target compilation unit of a particular reference. */
174 struct dwarf2_per_cu_data **all_comp_units;
175
176 /* The number of compilation units in ALL_COMP_UNITS. */
177 int n_comp_units;
178
179 /* The number of .debug_types-related CUs. */
180 int n_type_units;
181
182 /* The .debug_types-related CUs (TUs). */
183 struct dwarf2_per_cu_data **all_type_units;
184
185 /* A chain of compilation units that are currently read in, so that
186 they can be freed later. */
187 struct dwarf2_per_cu_data *read_in_chain;
188
189 /* A table mapping .debug_types signatures to its signatured_type entry.
190 This is NULL if the .debug_types section hasn't been read in yet. */
191 htab_t signatured_types;
192
193 /* A flag indicating wether this objfile has a section loaded at a
194 VMA of 0. */
195 int has_section_at_zero;
196
197 /* True if we are using the mapped index,
198 or we are faking it for OBJF_READNOW's sake. */
199 unsigned char using_index;
200
201 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
202 struct mapped_index *index_table;
203
204 /* When using index_table, this keeps track of all quick_file_names entries.
205 TUs can share line table entries with CUs or other TUs, and there can be
206 a lot more TUs than unique line tables, so we maintain a separate table
207 of all line table entries to support the sharing. */
208 htab_t quick_file_names_table;
209
210 /* Set during partial symbol reading, to prevent queueing of full
211 symbols. */
212 int reading_partial_symbols;
213
214 /* Table mapping type .debug_info DIE offsets to types.
215 This is NULL if not allocated yet.
216 It (currently) makes sense to allocate debug_types_type_hash lazily.
217 To keep things simple we allocate both lazily. */
218 htab_t debug_info_type_hash;
219
220 /* Table mapping type .debug_types DIE sect_offset to types.
221 This is NULL if not allocated yet. */
222 htab_t debug_types_type_hash;
223 };
224
225 static struct dwarf2_per_objfile *dwarf2_per_objfile;
226
227 /* Default names of the debugging sections. */
228
229 /* Note that if the debugging section has been compressed, it might
230 have a name like .zdebug_info. */
231
232 static const struct dwarf2_debug_sections dwarf2_elf_names =
233 {
234 { ".debug_info", ".zdebug_info" },
235 { ".debug_abbrev", ".zdebug_abbrev" },
236 { ".debug_line", ".zdebug_line" },
237 { ".debug_loc", ".zdebug_loc" },
238 { ".debug_macinfo", ".zdebug_macinfo" },
239 { ".debug_macro", ".zdebug_macro" },
240 { ".debug_str", ".zdebug_str" },
241 { ".debug_ranges", ".zdebug_ranges" },
242 { ".debug_types", ".zdebug_types" },
243 { ".debug_frame", ".zdebug_frame" },
244 { ".eh_frame", NULL },
245 { ".gdb_index", ".zgdb_index" },
246 23
247 };
248
249 /* local data types */
250
251 /* We hold several abbreviation tables in memory at the same time. */
252 #ifndef ABBREV_HASH_SIZE
253 #define ABBREV_HASH_SIZE 121
254 #endif
255
256 /* The data in a compilation unit header, after target2host
257 translation, looks like this. */
258 struct comp_unit_head
259 {
260 unsigned int length;
261 short version;
262 unsigned char addr_size;
263 unsigned char signed_addr_p;
264 sect_offset abbrev_offset;
265
266 /* Size of file offsets; either 4 or 8. */
267 unsigned int offset_size;
268
269 /* Size of the length field; either 4 or 12. */
270 unsigned int initial_length_size;
271
272 /* Offset to the first byte of this compilation unit header in the
273 .debug_info section, for resolving relative reference dies. */
274 sect_offset offset;
275
276 /* Offset to first die in this cu from the start of the cu.
277 This will be the first byte following the compilation unit header. */
278 cu_offset first_die_offset;
279 };
280
281 /* Type used for delaying computation of method physnames.
282 See comments for compute_delayed_physnames. */
283 struct delayed_method_info
284 {
285 /* The type to which the method is attached, i.e., its parent class. */
286 struct type *type;
287
288 /* The index of the method in the type's function fieldlists. */
289 int fnfield_index;
290
291 /* The index of the method in the fieldlist. */
292 int index;
293
294 /* The name of the DIE. */
295 const char *name;
296
297 /* The DIE associated with this method. */
298 struct die_info *die;
299 };
300
301 typedef struct delayed_method_info delayed_method_info;
302 DEF_VEC_O (delayed_method_info);
303
304 /* Internal state when decoding a particular compilation unit. */
305 struct dwarf2_cu
306 {
307 /* The objfile containing this compilation unit. */
308 struct objfile *objfile;
309
310 /* The header of the compilation unit. */
311 struct comp_unit_head header;
312
313 /* Base address of this compilation unit. */
314 CORE_ADDR base_address;
315
316 /* Non-zero if base_address has been set. */
317 int base_known;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
323 const char *producer;
324
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
341
342 /* Hash table holding all the loaded partial DIEs
343 with partial_die->offset.SECT_OFF as hash. */
344 htab_t partial_dies;
345
346 /* Storage for things with the same lifetime as this read-in compilation
347 unit, including partial DIEs. */
348 struct obstack comp_unit_obstack;
349
350 /* When multiple dwarf2_cu structures are living in memory, this field
351 chains them all together, so that they can be released efficiently.
352 We will probably also want a generation counter so that most-recently-used
353 compilation units are cached... */
354 struct dwarf2_per_cu_data *read_in_chain;
355
356 /* Backchain to our per_cu entry if the tree has been built. */
357 struct dwarf2_per_cu_data *per_cu;
358
359 /* How many compilation units ago was this CU last referenced? */
360 int last_used;
361
362 /* A hash table of DIE cu_offset for following references with
363 die_info->offset.sect_off as hash. */
364 htab_t die_hash;
365
366 /* Full DIEs if read in. */
367 struct die_info *dies;
368
369 /* A set of pointers to dwarf2_per_cu_data objects for compilation
370 units referenced by this one. Only set during full symbol processing;
371 partial symbol tables do not have dependencies. */
372 htab_t dependencies;
373
374 /* Header data from the line table, during full symbol processing. */
375 struct line_header *line_header;
376
377 /* A list of methods which need to have physnames computed
378 after all type information has been read. */
379 VEC (delayed_method_info) *method_list;
380
381 /* To be copied to symtab->call_site_htab. */
382 htab_t call_site_htab;
383
384 /* Mark used when releasing cached dies. */
385 unsigned int mark : 1;
386
387 /* This CU references .debug_loc. See the symtab->locations_valid field.
388 This test is imperfect as there may exist optimized debug code not using
389 any location list and still facing inlining issues if handled as
390 unoptimized code. For a future better test see GCC PR other/32998. */
391 unsigned int has_loclist : 1;
392
393 /* These cache the results of producer_is_gxx_lt_4_6.
394 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
395 information is cached because profiling CU expansion showed
396 excessive time spent in producer_is_gxx_lt_4_6. */
397 unsigned int checked_producer : 1;
398 unsigned int producer_is_gxx_lt_4_6 : 1;
399 };
400
401 /* Persistent data held for a compilation unit, even when not
402 processing it. We put a pointer to this structure in the
403 read_symtab_private field of the psymtab. */
404
405 struct dwarf2_per_cu_data
406 {
407 /* The start offset and length of this compilation unit. 2**29-1
408 bytes should suffice to store the length of any compilation unit
409 - if it doesn't, GDB will fall over anyway.
410 NOTE: Unlike comp_unit_head.length, this length includes
411 initial_length_size. */
412 sect_offset offset;
413 unsigned int length : 29;
414
415 /* Flag indicating this compilation unit will be read in before
416 any of the current compilation units are processed. */
417 unsigned int queued : 1;
418
419 /* This flag will be set when reading partial DIEs if we need to load
420 absolutely all DIEs for this compilation unit, instead of just the ones
421 we think are interesting. It gets set if we look for a DIE in the
422 hash table and don't find it. */
423 unsigned int load_all_dies : 1;
424
425 /* Non-null if this CU is from .debug_types; in which case it points
426 to the section. Otherwise it's from .debug_info. */
427 struct dwarf2_section_info *debug_types_section;
428
429 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
430 of the CU cache it gets reset to NULL again. */
431 struct dwarf2_cu *cu;
432
433 /* The corresponding objfile.
434 Normally we can get the objfile from dwarf2_per_objfile.
435 However we can enter this file with just a "per_cu" handle. */
436 struct objfile *objfile;
437
438 /* When using partial symbol tables, the 'psymtab' field is active.
439 Otherwise the 'quick' field is active. */
440 union
441 {
442 /* The partial symbol table associated with this compilation unit,
443 or NULL for partial units (which do not have an associated
444 symtab). */
445 struct partial_symtab *psymtab;
446
447 /* Data needed by the "quick" functions. */
448 struct dwarf2_per_cu_quick_data *quick;
449 } v;
450 };
451
452 /* Entry in the signatured_types hash table. */
453
454 struct signatured_type
455 {
456 ULONGEST signature;
457
458 /* Offset in this TU of the type defined by this TU. */
459 cu_offset type_offset;
460
461 /* The CU(/TU) of this type. */
462 struct dwarf2_per_cu_data per_cu;
463 };
464
465 /* Struct used to pass misc. parameters to read_die_and_children, et
466 al. which are used for both .debug_info and .debug_types dies.
467 All parameters here are unchanging for the life of the call. This
468 struct exists to abstract away the constant parameters of die
469 reading. */
470
471 struct die_reader_specs
472 {
473 /* The bfd of this objfile. */
474 bfd* abfd;
475
476 /* The CU of the DIE we are parsing. */
477 struct dwarf2_cu *cu;
478
479 /* Pointer to start of section buffer.
480 This is either the start of .debug_info or .debug_types. */
481 const gdb_byte *buffer;
482 };
483
484 /* The line number information for a compilation unit (found in the
485 .debug_line section) begins with a "statement program header",
486 which contains the following information. */
487 struct line_header
488 {
489 unsigned int total_length;
490 unsigned short version;
491 unsigned int header_length;
492 unsigned char minimum_instruction_length;
493 unsigned char maximum_ops_per_instruction;
494 unsigned char default_is_stmt;
495 int line_base;
496 unsigned char line_range;
497 unsigned char opcode_base;
498
499 /* standard_opcode_lengths[i] is the number of operands for the
500 standard opcode whose value is i. This means that
501 standard_opcode_lengths[0] is unused, and the last meaningful
502 element is standard_opcode_lengths[opcode_base - 1]. */
503 unsigned char *standard_opcode_lengths;
504
505 /* The include_directories table. NOTE! These strings are not
506 allocated with xmalloc; instead, they are pointers into
507 debug_line_buffer. If you try to free them, `free' will get
508 indigestion. */
509 unsigned int num_include_dirs, include_dirs_size;
510 char **include_dirs;
511
512 /* The file_names table. NOTE! These strings are not allocated
513 with xmalloc; instead, they are pointers into debug_line_buffer.
514 Don't try to free them directly. */
515 unsigned int num_file_names, file_names_size;
516 struct file_entry
517 {
518 char *name;
519 unsigned int dir_index;
520 unsigned int mod_time;
521 unsigned int length;
522 int included_p; /* Non-zero if referenced by the Line Number Program. */
523 struct symtab *symtab; /* The associated symbol table, if any. */
524 } *file_names;
525
526 /* The start and end of the statement program following this
527 header. These point into dwarf2_per_objfile->line_buffer. */
528 gdb_byte *statement_program_start, *statement_program_end;
529 };
530
531 /* When we construct a partial symbol table entry we only
532 need this much information. */
533 struct partial_die_info
534 {
535 /* Offset of this DIE. */
536 sect_offset offset;
537
538 /* DWARF-2 tag for this DIE. */
539 ENUM_BITFIELD(dwarf_tag) tag : 16;
540
541 /* Assorted flags describing the data found in this DIE. */
542 unsigned int has_children : 1;
543 unsigned int is_external : 1;
544 unsigned int is_declaration : 1;
545 unsigned int has_type : 1;
546 unsigned int has_specification : 1;
547 unsigned int has_pc_info : 1;
548 unsigned int may_be_inlined : 1;
549
550 /* Flag set if the SCOPE field of this structure has been
551 computed. */
552 unsigned int scope_set : 1;
553
554 /* Flag set if the DIE has a byte_size attribute. */
555 unsigned int has_byte_size : 1;
556
557 /* Flag set if any of the DIE's children are template arguments. */
558 unsigned int has_template_arguments : 1;
559
560 /* Flag set if fixup_partial_die has been called on this die. */
561 unsigned int fixup_called : 1;
562
563 /* The name of this DIE. Normally the value of DW_AT_name, but
564 sometimes a default name for unnamed DIEs. */
565 char *name;
566
567 /* The linkage name, if present. */
568 const char *linkage_name;
569
570 /* The scope to prepend to our children. This is generally
571 allocated on the comp_unit_obstack, so will disappear
572 when this compilation unit leaves the cache. */
573 char *scope;
574
575 /* The location description associated with this DIE, if any. */
576 struct dwarf_block *locdesc;
577
578 /* If HAS_PC_INFO, the PC range associated with this DIE. */
579 CORE_ADDR lowpc;
580 CORE_ADDR highpc;
581
582 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
583 DW_AT_sibling, if any. */
584 /* NOTE: This member isn't strictly necessary, read_partial_die could
585 return DW_AT_sibling values to its caller load_partial_dies. */
586 gdb_byte *sibling;
587
588 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
589 DW_AT_specification (or DW_AT_abstract_origin or
590 DW_AT_extension). */
591 sect_offset spec_offset;
592
593 /* Pointers to this DIE's parent, first child, and next sibling,
594 if any. */
595 struct partial_die_info *die_parent, *die_child, *die_sibling;
596 };
597
598 /* This data structure holds the information of an abbrev. */
599 struct abbrev_info
600 {
601 unsigned int number; /* number identifying abbrev */
602 enum dwarf_tag tag; /* dwarf tag */
603 unsigned short has_children; /* boolean */
604 unsigned short num_attrs; /* number of attributes */
605 struct attr_abbrev *attrs; /* an array of attribute descriptions */
606 struct abbrev_info *next; /* next in chain */
607 };
608
609 struct attr_abbrev
610 {
611 ENUM_BITFIELD(dwarf_attribute) name : 16;
612 ENUM_BITFIELD(dwarf_form) form : 16;
613 };
614
615 /* Attributes have a name and a value. */
616 struct attribute
617 {
618 ENUM_BITFIELD(dwarf_attribute) name : 16;
619 ENUM_BITFIELD(dwarf_form) form : 15;
620
621 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
622 field should be in u.str (existing only for DW_STRING) but it is kept
623 here for better struct attribute alignment. */
624 unsigned int string_is_canonical : 1;
625
626 union
627 {
628 char *str;
629 struct dwarf_block *blk;
630 ULONGEST unsnd;
631 LONGEST snd;
632 CORE_ADDR addr;
633 struct signatured_type *signatured_type;
634 }
635 u;
636 };
637
638 /* This data structure holds a complete die structure. */
639 struct die_info
640 {
641 /* DWARF-2 tag for this DIE. */
642 ENUM_BITFIELD(dwarf_tag) tag : 16;
643
644 /* Number of attributes */
645 unsigned char num_attrs;
646
647 /* True if we're presently building the full type name for the
648 type derived from this DIE. */
649 unsigned char building_fullname : 1;
650
651 /* Abbrev number */
652 unsigned int abbrev;
653
654 /* Offset in .debug_info or .debug_types section. */
655 sect_offset offset;
656
657 /* The dies in a compilation unit form an n-ary tree. PARENT
658 points to this die's parent; CHILD points to the first child of
659 this node; and all the children of a given node are chained
660 together via their SIBLING fields. */
661 struct die_info *child; /* Its first child, if any. */
662 struct die_info *sibling; /* Its next sibling, if any. */
663 struct die_info *parent; /* Its parent, if any. */
664
665 /* An array of attributes, with NUM_ATTRS elements. There may be
666 zero, but it's not common and zero-sized arrays are not
667 sufficiently portable C. */
668 struct attribute attrs[1];
669 };
670
671 /* Get at parts of an attribute structure. */
672
673 #define DW_STRING(attr) ((attr)->u.str)
674 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
675 #define DW_UNSND(attr) ((attr)->u.unsnd)
676 #define DW_BLOCK(attr) ((attr)->u.blk)
677 #define DW_SND(attr) ((attr)->u.snd)
678 #define DW_ADDR(attr) ((attr)->u.addr)
679 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
680
681 /* Blocks are a bunch of untyped bytes. */
682 struct dwarf_block
683 {
684 unsigned int size;
685
686 /* Valid only if SIZE is not zero. */
687 gdb_byte *data;
688 };
689
690 #ifndef ATTR_ALLOC_CHUNK
691 #define ATTR_ALLOC_CHUNK 4
692 #endif
693
694 /* Allocate fields for structs, unions and enums in this size. */
695 #ifndef DW_FIELD_ALLOC_CHUNK
696 #define DW_FIELD_ALLOC_CHUNK 4
697 #endif
698
699 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
700 but this would require a corresponding change in unpack_field_as_long
701 and friends. */
702 static int bits_per_byte = 8;
703
704 /* The routines that read and process dies for a C struct or C++ class
705 pass lists of data member fields and lists of member function fields
706 in an instance of a field_info structure, as defined below. */
707 struct field_info
708 {
709 /* List of data member and baseclasses fields. */
710 struct nextfield
711 {
712 struct nextfield *next;
713 int accessibility;
714 int virtuality;
715 struct field field;
716 }
717 *fields, *baseclasses;
718
719 /* Number of fields (including baseclasses). */
720 int nfields;
721
722 /* Number of baseclasses. */
723 int nbaseclasses;
724
725 /* Set if the accesibility of one of the fields is not public. */
726 int non_public_fields;
727
728 /* Member function fields array, entries are allocated in the order they
729 are encountered in the object file. */
730 struct nextfnfield
731 {
732 struct nextfnfield *next;
733 struct fn_field fnfield;
734 }
735 *fnfields;
736
737 /* Member function fieldlist array, contains name of possibly overloaded
738 member function, number of overloaded member functions and a pointer
739 to the head of the member function field chain. */
740 struct fnfieldlist
741 {
742 char *name;
743 int length;
744 struct nextfnfield *head;
745 }
746 *fnfieldlists;
747
748 /* Number of entries in the fnfieldlists array. */
749 int nfnfields;
750
751 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
752 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
753 struct typedef_field_list
754 {
755 struct typedef_field field;
756 struct typedef_field_list *next;
757 }
758 *typedef_field_list;
759 unsigned typedef_field_list_count;
760 };
761
762 /* One item on the queue of compilation units to read in full symbols
763 for. */
764 struct dwarf2_queue_item
765 {
766 struct dwarf2_per_cu_data *per_cu;
767 struct dwarf2_queue_item *next;
768 };
769
770 /* The current queue. */
771 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
772
773 /* Loaded secondary compilation units are kept in memory until they
774 have not been referenced for the processing of this many
775 compilation units. Set this to zero to disable caching. Cache
776 sizes of up to at least twenty will improve startup time for
777 typical inter-CU-reference binaries, at an obvious memory cost. */
778 static int dwarf2_max_cache_age = 5;
779 static void
780 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
781 struct cmd_list_element *c, const char *value)
782 {
783 fprintf_filtered (file, _("The upper bound on the age of cached "
784 "dwarf2 compilation units is %s.\n"),
785 value);
786 }
787
788
789 /* Various complaints about symbol reading that don't abort the process. */
790
791 static void
792 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
793 {
794 complaint (&symfile_complaints,
795 _("statement list doesn't fit in .debug_line section"));
796 }
797
798 static void
799 dwarf2_debug_line_missing_file_complaint (void)
800 {
801 complaint (&symfile_complaints,
802 _(".debug_line section has line data without a file"));
803 }
804
805 static void
806 dwarf2_debug_line_missing_end_sequence_complaint (void)
807 {
808 complaint (&symfile_complaints,
809 _(".debug_line section has line "
810 "program sequence without an end"));
811 }
812
813 static void
814 dwarf2_complex_location_expr_complaint (void)
815 {
816 complaint (&symfile_complaints, _("location expression too complex"));
817 }
818
819 static void
820 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
821 int arg3)
822 {
823 complaint (&symfile_complaints,
824 _("const value length mismatch for '%s', got %d, expected %d"),
825 arg1, arg2, arg3);
826 }
827
828 static void
829 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
830 {
831 complaint (&symfile_complaints,
832 _("macro info runs off end of `%s' section"),
833 section->asection->name);
834 }
835
836 static void
837 dwarf2_macro_malformed_definition_complaint (const char *arg1)
838 {
839 complaint (&symfile_complaints,
840 _("macro debug info contains a "
841 "malformed macro definition:\n`%s'"),
842 arg1);
843 }
844
845 static void
846 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
847 {
848 complaint (&symfile_complaints,
849 _("invalid attribute class or form for '%s' in '%s'"),
850 arg1, arg2);
851 }
852
853 /* local function prototypes */
854
855 static void dwarf2_locate_sections (bfd *, asection *, void *);
856
857 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
858 struct objfile *);
859
860 static void dwarf2_find_base_address (struct die_info *die,
861 struct dwarf2_cu *cu);
862
863 static void dwarf2_build_psymtabs_hard (struct objfile *);
864
865 static void scan_partial_symbols (struct partial_die_info *,
866 CORE_ADDR *, CORE_ADDR *,
867 int, struct dwarf2_cu *);
868
869 static void add_partial_symbol (struct partial_die_info *,
870 struct dwarf2_cu *);
871
872 static void add_partial_namespace (struct partial_die_info *pdi,
873 CORE_ADDR *lowpc, CORE_ADDR *highpc,
874 int need_pc, struct dwarf2_cu *cu);
875
876 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
877 CORE_ADDR *highpc, int need_pc,
878 struct dwarf2_cu *cu);
879
880 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
881 struct dwarf2_cu *cu);
882
883 static void add_partial_subprogram (struct partial_die_info *pdi,
884 CORE_ADDR *lowpc, CORE_ADDR *highpc,
885 int need_pc, struct dwarf2_cu *cu);
886
887 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
888 gdb_byte *buffer, gdb_byte *info_ptr,
889 bfd *abfd, struct dwarf2_cu *cu);
890
891 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
892
893 static void psymtab_to_symtab_1 (struct partial_symtab *);
894
895 static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
896
897 static void dwarf2_free_abbrev_table (void *);
898
899 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
900
901 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
902 struct dwarf2_cu *);
903
904 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
905 struct dwarf2_cu *);
906
907 static struct partial_die_info *load_partial_dies (bfd *,
908 gdb_byte *, gdb_byte *,
909 int, struct dwarf2_cu *);
910
911 static gdb_byte *read_partial_die (struct partial_die_info *,
912 struct abbrev_info *abbrev,
913 unsigned int, bfd *,
914 gdb_byte *, gdb_byte *,
915 struct dwarf2_cu *);
916
917 static struct partial_die_info *find_partial_die (sect_offset,
918 struct dwarf2_cu *);
919
920 static void fixup_partial_die (struct partial_die_info *,
921 struct dwarf2_cu *);
922
923 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
924 bfd *, gdb_byte *, struct dwarf2_cu *);
925
926 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
927 bfd *, gdb_byte *, struct dwarf2_cu *);
928
929 static unsigned int read_1_byte (bfd *, gdb_byte *);
930
931 static int read_1_signed_byte (bfd *, gdb_byte *);
932
933 static unsigned int read_2_bytes (bfd *, gdb_byte *);
934
935 static unsigned int read_4_bytes (bfd *, gdb_byte *);
936
937 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
938
939 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
940 unsigned int *);
941
942 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
943
944 static LONGEST read_checked_initial_length_and_offset
945 (bfd *, gdb_byte *, const struct comp_unit_head *,
946 unsigned int *, unsigned int *);
947
948 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
949 unsigned int *);
950
951 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
952
953 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
954
955 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
956
957 static char *read_indirect_string (bfd *, gdb_byte *,
958 const struct comp_unit_head *,
959 unsigned int *);
960
961 static ULONGEST read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
962
963 static LONGEST read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
964
965 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
966
967 static void set_cu_language (unsigned int, struct dwarf2_cu *);
968
969 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
970 struct dwarf2_cu *);
971
972 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
973 unsigned int,
974 struct dwarf2_cu *);
975
976 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
977 struct dwarf2_cu *cu);
978
979 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
980
981 static struct die_info *die_specification (struct die_info *die,
982 struct dwarf2_cu **);
983
984 static void free_line_header (struct line_header *lh);
985
986 static void add_file_name (struct line_header *, char *, unsigned int,
987 unsigned int, unsigned int);
988
989 static struct line_header *(dwarf_decode_line_header
990 (unsigned int offset,
991 bfd *abfd, struct dwarf2_cu *cu));
992
993 static void dwarf_decode_lines (struct line_header *, const char *,
994 struct dwarf2_cu *, struct partial_symtab *,
995 int);
996
997 static void dwarf2_start_subfile (char *, const char *, const char *);
998
999 static struct symbol *new_symbol (struct die_info *, struct type *,
1000 struct dwarf2_cu *);
1001
1002 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1003 struct dwarf2_cu *, struct symbol *);
1004
1005 static void dwarf2_const_value (struct attribute *, struct symbol *,
1006 struct dwarf2_cu *);
1007
1008 static void dwarf2_const_value_attr (struct attribute *attr,
1009 struct type *type,
1010 const char *name,
1011 struct obstack *obstack,
1012 struct dwarf2_cu *cu, LONGEST *value,
1013 gdb_byte **bytes,
1014 struct dwarf2_locexpr_baton **baton);
1015
1016 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1017
1018 static int need_gnat_info (struct dwarf2_cu *);
1019
1020 static struct type *die_descriptive_type (struct die_info *,
1021 struct dwarf2_cu *);
1022
1023 static void set_descriptive_type (struct type *, struct die_info *,
1024 struct dwarf2_cu *);
1025
1026 static struct type *die_containing_type (struct die_info *,
1027 struct dwarf2_cu *);
1028
1029 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1030 struct dwarf2_cu *);
1031
1032 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1033
1034 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1035
1036 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1037
1038 static char *typename_concat (struct obstack *obs, const char *prefix,
1039 const char *suffix, int physname,
1040 struct dwarf2_cu *cu);
1041
1042 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1043
1044 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1045
1046 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1047
1048 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1049
1050 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1051
1052 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1053 struct dwarf2_cu *, struct partial_symtab *);
1054
1055 static int dwarf2_get_pc_bounds (struct die_info *,
1056 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1057 struct partial_symtab *);
1058
1059 static void get_scope_pc_bounds (struct die_info *,
1060 CORE_ADDR *, CORE_ADDR *,
1061 struct dwarf2_cu *);
1062
1063 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1064 CORE_ADDR, struct dwarf2_cu *);
1065
1066 static void dwarf2_add_field (struct field_info *, struct die_info *,
1067 struct dwarf2_cu *);
1068
1069 static void dwarf2_attach_fields_to_type (struct field_info *,
1070 struct type *, struct dwarf2_cu *);
1071
1072 static void dwarf2_add_member_fn (struct field_info *,
1073 struct die_info *, struct type *,
1074 struct dwarf2_cu *);
1075
1076 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1077 struct type *,
1078 struct dwarf2_cu *);
1079
1080 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1081
1082 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1083
1084 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1085
1086 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1087
1088 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1089
1090 static struct type *read_module_type (struct die_info *die,
1091 struct dwarf2_cu *cu);
1092
1093 static const char *namespace_name (struct die_info *die,
1094 int *is_anonymous, struct dwarf2_cu *);
1095
1096 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1097
1098 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1099
1100 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1101 struct dwarf2_cu *);
1102
1103 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1104
1105 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1106 gdb_byte *info_ptr,
1107 gdb_byte **new_info_ptr,
1108 struct die_info *parent);
1109
1110 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1111 gdb_byte *info_ptr,
1112 gdb_byte **new_info_ptr,
1113 struct die_info *parent);
1114
1115 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1116 gdb_byte *info_ptr,
1117 gdb_byte **new_info_ptr,
1118 struct die_info *parent);
1119
1120 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1121 struct die_info **, gdb_byte *,
1122 int *);
1123
1124 static void process_die (struct die_info *, struct dwarf2_cu *);
1125
1126 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1127 struct obstack *);
1128
1129 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1130
1131 static const char *dwarf2_full_name (char *name,
1132 struct die_info *die,
1133 struct dwarf2_cu *cu);
1134
1135 static struct die_info *dwarf2_extension (struct die_info *die,
1136 struct dwarf2_cu **);
1137
1138 static char *dwarf_tag_name (unsigned int);
1139
1140 static char *dwarf_attr_name (unsigned int);
1141
1142 static char *dwarf_form_name (unsigned int);
1143
1144 static char *dwarf_bool_name (unsigned int);
1145
1146 static char *dwarf_type_encoding_name (unsigned int);
1147
1148 #if 0
1149 static char *dwarf_cfi_name (unsigned int);
1150 #endif
1151
1152 static struct die_info *sibling_die (struct die_info *);
1153
1154 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1155
1156 static void dump_die_for_error (struct die_info *);
1157
1158 static void dump_die_1 (struct ui_file *, int level, int max_level,
1159 struct die_info *);
1160
1161 /*static*/ void dump_die (struct die_info *, int max_level);
1162
1163 static void store_in_ref_table (struct die_info *,
1164 struct dwarf2_cu *);
1165
1166 static int is_ref_attr (struct attribute *);
1167
1168 static sect_offset dwarf2_get_ref_die_offset (struct attribute *);
1169
1170 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1171
1172 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1173 struct attribute *,
1174 struct dwarf2_cu **);
1175
1176 static struct die_info *follow_die_ref (struct die_info *,
1177 struct attribute *,
1178 struct dwarf2_cu **);
1179
1180 static struct die_info *follow_die_sig (struct die_info *,
1181 struct attribute *,
1182 struct dwarf2_cu **);
1183
1184 static struct signatured_type *lookup_signatured_type_at_offset
1185 (struct objfile *objfile,
1186 struct dwarf2_section_info *section, sect_offset offset);
1187
1188 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1189
1190 static void read_signatured_type (struct signatured_type *);
1191
1192 /* memory allocation interface */
1193
1194 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1195
1196 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1197
1198 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1199
1200 static void dwarf_decode_macros (struct line_header *, unsigned int,
1201 char *, bfd *, struct dwarf2_cu *,
1202 struct dwarf2_section_info *,
1203 int);
1204
1205 static int attr_form_is_block (struct attribute *);
1206
1207 static int attr_form_is_section_offset (struct attribute *);
1208
1209 static int attr_form_is_constant (struct attribute *);
1210
1211 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1212 struct dwarf2_loclist_baton *baton,
1213 struct attribute *attr);
1214
1215 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1216 struct symbol *sym,
1217 struct dwarf2_cu *cu);
1218
1219 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1220 struct abbrev_info *abbrev,
1221 struct dwarf2_cu *cu);
1222
1223 static void free_stack_comp_unit (void *);
1224
1225 static hashval_t partial_die_hash (const void *item);
1226
1227 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1228
1229 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1230 (sect_offset offset, struct objfile *objfile);
1231
1232 static void init_one_comp_unit (struct dwarf2_cu *cu,
1233 struct dwarf2_per_cu_data *per_cu);
1234
1235 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1236 struct die_info *comp_unit_die);
1237
1238 static void free_heap_comp_unit (void *);
1239
1240 static void free_cached_comp_units (void *);
1241
1242 static void age_cached_comp_units (void);
1243
1244 static void free_one_cached_comp_unit (void *);
1245
1246 static struct type *set_die_type (struct die_info *, struct type *,
1247 struct dwarf2_cu *);
1248
1249 static void create_all_comp_units (struct objfile *);
1250
1251 static int create_all_type_units (struct objfile *);
1252
1253 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1254
1255 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1256
1257 static void dwarf2_add_dependence (struct dwarf2_cu *,
1258 struct dwarf2_per_cu_data *);
1259
1260 static void dwarf2_mark (struct dwarf2_cu *);
1261
1262 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1263
1264 static struct type *get_die_type_at_offset (sect_offset,
1265 struct dwarf2_per_cu_data *per_cu);
1266
1267 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1268
1269 static void dwarf2_release_queue (void *dummy);
1270
1271 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1272
1273 static void process_queue (void);
1274
1275 static void find_file_and_directory (struct die_info *die,
1276 struct dwarf2_cu *cu,
1277 char **name, char **comp_dir);
1278
1279 static char *file_full_name (int file, struct line_header *lh,
1280 const char *comp_dir);
1281
1282 static gdb_byte *read_and_check_comp_unit_head
1283 (struct comp_unit_head *header,
1284 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1285 int is_debug_types_section);
1286
1287 static void init_cu_die_reader (struct die_reader_specs *reader,
1288 struct dwarf2_cu *cu);
1289
1290 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1291
1292 #if WORDS_BIGENDIAN
1293
1294 /* Convert VALUE between big- and little-endian. */
1295 static offset_type
1296 byte_swap (offset_type value)
1297 {
1298 offset_type result;
1299
1300 result = (value & 0xff) << 24;
1301 result |= (value & 0xff00) << 8;
1302 result |= (value & 0xff0000) >> 8;
1303 result |= (value & 0xff000000) >> 24;
1304 return result;
1305 }
1306
1307 #define MAYBE_SWAP(V) byte_swap (V)
1308
1309 #else
1310 #define MAYBE_SWAP(V) (V)
1311 #endif /* WORDS_BIGENDIAN */
1312
1313 /* The suffix for an index file. */
1314 #define INDEX_SUFFIX ".gdb-index"
1315
1316 static const char *dwarf2_physname (char *name, struct die_info *die,
1317 struct dwarf2_cu *cu);
1318
1319 /* Try to locate the sections we need for DWARF 2 debugging
1320 information and return true if we have enough to do something.
1321 NAMES points to the dwarf2 section names, or is NULL if the standard
1322 ELF names are used. */
1323
1324 int
1325 dwarf2_has_info (struct objfile *objfile,
1326 const struct dwarf2_debug_sections *names)
1327 {
1328 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1329 if (!dwarf2_per_objfile)
1330 {
1331 /* Initialize per-objfile state. */
1332 struct dwarf2_per_objfile *data
1333 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1334
1335 memset (data, 0, sizeof (*data));
1336 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1337 dwarf2_per_objfile = data;
1338
1339 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1340 (void *) names);
1341 dwarf2_per_objfile->objfile = objfile;
1342 }
1343 return (dwarf2_per_objfile->info.asection != NULL
1344 && dwarf2_per_objfile->abbrev.asection != NULL);
1345 }
1346
1347 /* When loading sections, we look either for uncompressed section or for
1348 compressed section names. */
1349
1350 static int
1351 section_is_p (const char *section_name,
1352 const struct dwarf2_section_names *names)
1353 {
1354 if (names->normal != NULL
1355 && strcmp (section_name, names->normal) == 0)
1356 return 1;
1357 if (names->compressed != NULL
1358 && strcmp (section_name, names->compressed) == 0)
1359 return 1;
1360 return 0;
1361 }
1362
1363 /* This function is mapped across the sections and remembers the
1364 offset and size of each of the debugging sections we are interested
1365 in. */
1366
1367 static void
1368 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1369 {
1370 const struct dwarf2_debug_sections *names;
1371
1372 if (vnames == NULL)
1373 names = &dwarf2_elf_names;
1374 else
1375 names = (const struct dwarf2_debug_sections *) vnames;
1376
1377 if (section_is_p (sectp->name, &names->info))
1378 {
1379 dwarf2_per_objfile->info.asection = sectp;
1380 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1381 }
1382 else if (section_is_p (sectp->name, &names->abbrev))
1383 {
1384 dwarf2_per_objfile->abbrev.asection = sectp;
1385 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1386 }
1387 else if (section_is_p (sectp->name, &names->line))
1388 {
1389 dwarf2_per_objfile->line.asection = sectp;
1390 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1391 }
1392 else if (section_is_p (sectp->name, &names->loc))
1393 {
1394 dwarf2_per_objfile->loc.asection = sectp;
1395 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1396 }
1397 else if (section_is_p (sectp->name, &names->macinfo))
1398 {
1399 dwarf2_per_objfile->macinfo.asection = sectp;
1400 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1401 }
1402 else if (section_is_p (sectp->name, &names->macro))
1403 {
1404 dwarf2_per_objfile->macro.asection = sectp;
1405 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1406 }
1407 else if (section_is_p (sectp->name, &names->str))
1408 {
1409 dwarf2_per_objfile->str.asection = sectp;
1410 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1411 }
1412 else if (section_is_p (sectp->name, &names->frame))
1413 {
1414 dwarf2_per_objfile->frame.asection = sectp;
1415 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1416 }
1417 else if (section_is_p (sectp->name, &names->eh_frame))
1418 {
1419 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1420
1421 if (aflag & SEC_HAS_CONTENTS)
1422 {
1423 dwarf2_per_objfile->eh_frame.asection = sectp;
1424 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1425 }
1426 }
1427 else if (section_is_p (sectp->name, &names->ranges))
1428 {
1429 dwarf2_per_objfile->ranges.asection = sectp;
1430 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1431 }
1432 else if (section_is_p (sectp->name, &names->types))
1433 {
1434 struct dwarf2_section_info type_section;
1435
1436 memset (&type_section, 0, sizeof (type_section));
1437 type_section.asection = sectp;
1438 type_section.size = bfd_get_section_size (sectp);
1439
1440 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1441 &type_section);
1442 }
1443 else if (section_is_p (sectp->name, &names->gdb_index))
1444 {
1445 dwarf2_per_objfile->gdb_index.asection = sectp;
1446 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1447 }
1448
1449 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1450 && bfd_section_vma (abfd, sectp) == 0)
1451 dwarf2_per_objfile->has_section_at_zero = 1;
1452 }
1453
1454 /* Decompress a section that was compressed using zlib. Store the
1455 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1456
1457 static void
1458 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1459 gdb_byte **outbuf, bfd_size_type *outsize)
1460 {
1461 bfd *abfd = objfile->obfd;
1462 #ifndef HAVE_ZLIB_H
1463 error (_("Support for zlib-compressed DWARF data (from '%s') "
1464 "is disabled in this copy of GDB"),
1465 bfd_get_filename (abfd));
1466 #else
1467 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1468 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1469 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1470 bfd_size_type uncompressed_size;
1471 gdb_byte *uncompressed_buffer;
1472 z_stream strm;
1473 int rc;
1474 int header_size = 12;
1475
1476 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1477 || bfd_bread (compressed_buffer,
1478 compressed_size, abfd) != compressed_size)
1479 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1480 bfd_get_filename (abfd));
1481
1482 /* Read the zlib header. In this case, it should be "ZLIB" followed
1483 by the uncompressed section size, 8 bytes in big-endian order. */
1484 if (compressed_size < header_size
1485 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1486 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1487 bfd_get_filename (abfd));
1488 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1493 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1494 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1495 uncompressed_size += compressed_buffer[11];
1496
1497 /* It is possible the section consists of several compressed
1498 buffers concatenated together, so we uncompress in a loop. */
1499 strm.zalloc = NULL;
1500 strm.zfree = NULL;
1501 strm.opaque = NULL;
1502 strm.avail_in = compressed_size - header_size;
1503 strm.next_in = (Bytef*) compressed_buffer + header_size;
1504 strm.avail_out = uncompressed_size;
1505 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1506 uncompressed_size);
1507 rc = inflateInit (&strm);
1508 while (strm.avail_in > 0)
1509 {
1510 if (rc != Z_OK)
1511 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1512 bfd_get_filename (abfd), rc);
1513 strm.next_out = ((Bytef*) uncompressed_buffer
1514 + (uncompressed_size - strm.avail_out));
1515 rc = inflate (&strm, Z_FINISH);
1516 if (rc != Z_STREAM_END)
1517 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1518 bfd_get_filename (abfd), rc);
1519 rc = inflateReset (&strm);
1520 }
1521 rc = inflateEnd (&strm);
1522 if (rc != Z_OK
1523 || strm.avail_out != 0)
1524 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1525 bfd_get_filename (abfd), rc);
1526
1527 do_cleanups (cleanup);
1528 *outbuf = uncompressed_buffer;
1529 *outsize = uncompressed_size;
1530 #endif
1531 }
1532
1533 /* A helper function that decides whether a section is empty. */
1534
1535 static int
1536 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1537 {
1538 return info->asection == NULL || info->size == 0;
1539 }
1540
1541 /* Read the contents of the section INFO from object file specified by
1542 OBJFILE, store info about the section into INFO.
1543 If the section is compressed, uncompress it before returning. */
1544
1545 static void
1546 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1547 {
1548 bfd *abfd = objfile->obfd;
1549 asection *sectp = info->asection;
1550 gdb_byte *buf, *retbuf;
1551 unsigned char header[4];
1552
1553 if (info->readin)
1554 return;
1555 info->buffer = NULL;
1556 info->map_addr = NULL;
1557 info->readin = 1;
1558
1559 if (dwarf2_section_empty_p (info))
1560 return;
1561
1562 /* Check if the file has a 4-byte header indicating compression. */
1563 if (info->size > sizeof (header)
1564 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1565 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1566 {
1567 /* Upon decompression, update the buffer and its size. */
1568 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1569 {
1570 zlib_decompress_section (objfile, sectp, &info->buffer,
1571 &info->size);
1572 return;
1573 }
1574 }
1575
1576 #ifdef HAVE_MMAP
1577 if (pagesize == 0)
1578 pagesize = getpagesize ();
1579
1580 /* Only try to mmap sections which are large enough: we don't want to
1581 waste space due to fragmentation. Also, only try mmap for sections
1582 without relocations. */
1583
1584 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1585 {
1586 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1587 MAP_PRIVATE, sectp->filepos,
1588 &info->map_addr, &info->map_len);
1589
1590 if ((caddr_t)info->buffer != MAP_FAILED)
1591 {
1592 #if HAVE_POSIX_MADVISE
1593 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1594 #endif
1595 return;
1596 }
1597 }
1598 #endif
1599
1600 /* If we get here, we are a normal, not-compressed section. */
1601 info->buffer = buf
1602 = obstack_alloc (&objfile->objfile_obstack, info->size);
1603
1604 /* When debugging .o files, we may need to apply relocations; see
1605 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1606 We never compress sections in .o files, so we only need to
1607 try this when the section is not compressed. */
1608 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1609 if (retbuf != NULL)
1610 {
1611 info->buffer = retbuf;
1612 return;
1613 }
1614
1615 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1616 || bfd_bread (buf, info->size, abfd) != info->size)
1617 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1618 bfd_get_filename (abfd));
1619 }
1620
1621 /* A helper function that returns the size of a section in a safe way.
1622 If you are positive that the section has been read before using the
1623 size, then it is safe to refer to the dwarf2_section_info object's
1624 "size" field directly. In other cases, you must call this
1625 function, because for compressed sections the size field is not set
1626 correctly until the section has been read. */
1627
1628 static bfd_size_type
1629 dwarf2_section_size (struct objfile *objfile,
1630 struct dwarf2_section_info *info)
1631 {
1632 if (!info->readin)
1633 dwarf2_read_section (objfile, info);
1634 return info->size;
1635 }
1636
1637 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1638 SECTION_NAME. */
1639
1640 void
1641 dwarf2_get_section_info (struct objfile *objfile,
1642 enum dwarf2_section_enum sect,
1643 asection **sectp, gdb_byte **bufp,
1644 bfd_size_type *sizep)
1645 {
1646 struct dwarf2_per_objfile *data
1647 = objfile_data (objfile, dwarf2_objfile_data_key);
1648 struct dwarf2_section_info *info;
1649
1650 /* We may see an objfile without any DWARF, in which case we just
1651 return nothing. */
1652 if (data == NULL)
1653 {
1654 *sectp = NULL;
1655 *bufp = NULL;
1656 *sizep = 0;
1657 return;
1658 }
1659 switch (sect)
1660 {
1661 case DWARF2_DEBUG_FRAME:
1662 info = &data->frame;
1663 break;
1664 case DWARF2_EH_FRAME:
1665 info = &data->eh_frame;
1666 break;
1667 default:
1668 gdb_assert_not_reached ("unexpected section");
1669 }
1670
1671 dwarf2_read_section (objfile, info);
1672
1673 *sectp = info->asection;
1674 *bufp = info->buffer;
1675 *sizep = info->size;
1676 }
1677
1678 \f
1679 /* DWARF quick_symbols_functions support. */
1680
1681 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1682 unique line tables, so we maintain a separate table of all .debug_line
1683 derived entries to support the sharing.
1684 All the quick functions need is the list of file names. We discard the
1685 line_header when we're done and don't need to record it here. */
1686 struct quick_file_names
1687 {
1688 /* The offset in .debug_line of the line table. We hash on this. */
1689 unsigned int offset;
1690
1691 /* The number of entries in file_names, real_names. */
1692 unsigned int num_file_names;
1693
1694 /* The file names from the line table, after being run through
1695 file_full_name. */
1696 const char **file_names;
1697
1698 /* The file names from the line table after being run through
1699 gdb_realpath. These are computed lazily. */
1700 const char **real_names;
1701 };
1702
1703 /* When using the index (and thus not using psymtabs), each CU has an
1704 object of this type. This is used to hold information needed by
1705 the various "quick" methods. */
1706 struct dwarf2_per_cu_quick_data
1707 {
1708 /* The file table. This can be NULL if there was no file table
1709 or it's currently not read in.
1710 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1711 struct quick_file_names *file_names;
1712
1713 /* The corresponding symbol table. This is NULL if symbols for this
1714 CU have not yet been read. */
1715 struct symtab *symtab;
1716
1717 /* A temporary mark bit used when iterating over all CUs in
1718 expand_symtabs_matching. */
1719 unsigned int mark : 1;
1720
1721 /* True if we've tried to read the file table and found there isn't one.
1722 There will be no point in trying to read it again next time. */
1723 unsigned int no_file_data : 1;
1724 };
1725
1726 /* Hash function for a quick_file_names. */
1727
1728 static hashval_t
1729 hash_file_name_entry (const void *e)
1730 {
1731 const struct quick_file_names *file_data = e;
1732
1733 return file_data->offset;
1734 }
1735
1736 /* Equality function for a quick_file_names. */
1737
1738 static int
1739 eq_file_name_entry (const void *a, const void *b)
1740 {
1741 const struct quick_file_names *ea = a;
1742 const struct quick_file_names *eb = b;
1743
1744 return ea->offset == eb->offset;
1745 }
1746
1747 /* Delete function for a quick_file_names. */
1748
1749 static void
1750 delete_file_name_entry (void *e)
1751 {
1752 struct quick_file_names *file_data = e;
1753 int i;
1754
1755 for (i = 0; i < file_data->num_file_names; ++i)
1756 {
1757 xfree ((void*) file_data->file_names[i]);
1758 if (file_data->real_names)
1759 xfree ((void*) file_data->real_names[i]);
1760 }
1761
1762 /* The space for the struct itself lives on objfile_obstack,
1763 so we don't free it here. */
1764 }
1765
1766 /* Create a quick_file_names hash table. */
1767
1768 static htab_t
1769 create_quick_file_names_table (unsigned int nr_initial_entries)
1770 {
1771 return htab_create_alloc (nr_initial_entries,
1772 hash_file_name_entry, eq_file_name_entry,
1773 delete_file_name_entry, xcalloc, xfree);
1774 }
1775
1776 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1777 have to be created afterwards. You should call age_cached_comp_units after
1778 processing PER_CU->CU. dw2_setup must have been already called. */
1779
1780 static void
1781 load_cu (struct dwarf2_per_cu_data *per_cu)
1782 {
1783 if (per_cu->debug_types_section)
1784 load_full_type_unit (per_cu);
1785 else
1786 load_full_comp_unit (per_cu);
1787
1788 gdb_assert (per_cu->cu != NULL);
1789
1790 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1791 }
1792
1793 /* Read in the symbols for PER_CU. */
1794
1795 static void
1796 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1797 {
1798 struct cleanup *back_to;
1799
1800 back_to = make_cleanup (dwarf2_release_queue, NULL);
1801
1802 queue_comp_unit (per_cu);
1803
1804 load_cu (per_cu);
1805
1806 process_queue ();
1807
1808 /* Age the cache, releasing compilation units that have not
1809 been used recently. */
1810 age_cached_comp_units ();
1811
1812 do_cleanups (back_to);
1813 }
1814
1815 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1816 the objfile from which this CU came. Returns the resulting symbol
1817 table. */
1818
1819 static struct symtab *
1820 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1821 {
1822 if (!per_cu->v.quick->symtab)
1823 {
1824 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1825 increment_reading_symtab ();
1826 dw2_do_instantiate_symtab (per_cu);
1827 do_cleanups (back_to);
1828 }
1829 return per_cu->v.quick->symtab;
1830 }
1831
1832 /* Return the CU given its index. */
1833
1834 static struct dwarf2_per_cu_data *
1835 dw2_get_cu (int index)
1836 {
1837 if (index >= dwarf2_per_objfile->n_comp_units)
1838 {
1839 index -= dwarf2_per_objfile->n_comp_units;
1840 return dwarf2_per_objfile->all_type_units[index];
1841 }
1842 return dwarf2_per_objfile->all_comp_units[index];
1843 }
1844
1845 /* A helper function that knows how to read a 64-bit value in a way
1846 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1847 otherwise. */
1848
1849 static int
1850 extract_cu_value (const char *bytes, ULONGEST *result)
1851 {
1852 if (sizeof (ULONGEST) < 8)
1853 {
1854 int i;
1855
1856 /* Ignore the upper 4 bytes if they are all zero. */
1857 for (i = 0; i < 4; ++i)
1858 if (bytes[i + 4] != 0)
1859 return 0;
1860
1861 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1862 }
1863 else
1864 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1865 return 1;
1866 }
1867
1868 /* Read the CU list from the mapped index, and use it to create all
1869 the CU objects for this objfile. Return 0 if something went wrong,
1870 1 if everything went ok. */
1871
1872 static int
1873 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1874 offset_type cu_list_elements)
1875 {
1876 offset_type i;
1877
1878 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1879 dwarf2_per_objfile->all_comp_units
1880 = obstack_alloc (&objfile->objfile_obstack,
1881 dwarf2_per_objfile->n_comp_units
1882 * sizeof (struct dwarf2_per_cu_data *));
1883
1884 for (i = 0; i < cu_list_elements; i += 2)
1885 {
1886 struct dwarf2_per_cu_data *the_cu;
1887 ULONGEST offset, length;
1888
1889 if (!extract_cu_value (cu_list, &offset)
1890 || !extract_cu_value (cu_list + 8, &length))
1891 return 0;
1892 cu_list += 2 * 8;
1893
1894 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1895 struct dwarf2_per_cu_data);
1896 the_cu->offset.sect_off = offset;
1897 the_cu->length = length;
1898 the_cu->objfile = objfile;
1899 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1900 struct dwarf2_per_cu_quick_data);
1901 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1902 }
1903
1904 return 1;
1905 }
1906
1907 /* Create the signatured type hash table from the index. */
1908
1909 static int
1910 create_signatured_type_table_from_index (struct objfile *objfile,
1911 struct dwarf2_section_info *section,
1912 const gdb_byte *bytes,
1913 offset_type elements)
1914 {
1915 offset_type i;
1916 htab_t sig_types_hash;
1917
1918 dwarf2_per_objfile->n_type_units = elements / 3;
1919 dwarf2_per_objfile->all_type_units
1920 = obstack_alloc (&objfile->objfile_obstack,
1921 dwarf2_per_objfile->n_type_units
1922 * sizeof (struct dwarf2_per_cu_data *));
1923
1924 sig_types_hash = allocate_signatured_type_table (objfile);
1925
1926 for (i = 0; i < elements; i += 3)
1927 {
1928 struct signatured_type *sig_type;
1929 ULONGEST offset, type_offset_in_tu, signature;
1930 void **slot;
1931
1932 if (!extract_cu_value (bytes, &offset)
1933 || !extract_cu_value (bytes + 8, &type_offset_in_tu))
1934 return 0;
1935 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1936 bytes += 3 * 8;
1937
1938 sig_type = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1939 struct signatured_type);
1940 sig_type->signature = signature;
1941 sig_type->type_offset.cu_off = type_offset_in_tu;
1942 sig_type->per_cu.debug_types_section = section;
1943 sig_type->per_cu.offset.sect_off = offset;
1944 sig_type->per_cu.objfile = objfile;
1945 sig_type->per_cu.v.quick
1946 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1947 struct dwarf2_per_cu_quick_data);
1948
1949 slot = htab_find_slot (sig_types_hash, sig_type, INSERT);
1950 *slot = sig_type;
1951
1952 dwarf2_per_objfile->all_type_units[i / 3] = &sig_type->per_cu;
1953 }
1954
1955 dwarf2_per_objfile->signatured_types = sig_types_hash;
1956
1957 return 1;
1958 }
1959
1960 /* Read the address map data from the mapped index, and use it to
1961 populate the objfile's psymtabs_addrmap. */
1962
1963 static void
1964 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1965 {
1966 const gdb_byte *iter, *end;
1967 struct obstack temp_obstack;
1968 struct addrmap *mutable_map;
1969 struct cleanup *cleanup;
1970 CORE_ADDR baseaddr;
1971
1972 obstack_init (&temp_obstack);
1973 cleanup = make_cleanup_obstack_free (&temp_obstack);
1974 mutable_map = addrmap_create_mutable (&temp_obstack);
1975
1976 iter = index->address_table;
1977 end = iter + index->address_table_size;
1978
1979 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1980
1981 while (iter < end)
1982 {
1983 ULONGEST hi, lo, cu_index;
1984 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985 iter += 8;
1986 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1987 iter += 8;
1988 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1989 iter += 4;
1990
1991 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1992 dw2_get_cu (cu_index));
1993 }
1994
1995 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1996 &objfile->objfile_obstack);
1997 do_cleanups (cleanup);
1998 }
1999
2000 /* The hash function for strings in the mapped index. This is the same as
2001 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2002 implementation. This is necessary because the hash function is tied to the
2003 format of the mapped index file. The hash values do not have to match with
2004 SYMBOL_HASH_NEXT.
2005
2006 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2007
2008 static hashval_t
2009 mapped_index_string_hash (int index_version, const void *p)
2010 {
2011 const unsigned char *str = (const unsigned char *) p;
2012 hashval_t r = 0;
2013 unsigned char c;
2014
2015 while ((c = *str++) != 0)
2016 {
2017 if (index_version >= 5)
2018 c = tolower (c);
2019 r = r * 67 + c - 113;
2020 }
2021
2022 return r;
2023 }
2024
2025 /* Find a slot in the mapped index INDEX for the object named NAME.
2026 If NAME is found, set *VEC_OUT to point to the CU vector in the
2027 constant pool and return 1. If NAME cannot be found, return 0. */
2028
2029 static int
2030 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2031 offset_type **vec_out)
2032 {
2033 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2034 offset_type hash;
2035 offset_type slot, step;
2036 int (*cmp) (const char *, const char *);
2037
2038 if (current_language->la_language == language_cplus
2039 || current_language->la_language == language_java
2040 || current_language->la_language == language_fortran)
2041 {
2042 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2043 not contain any. */
2044 const char *paren = strchr (name, '(');
2045
2046 if (paren)
2047 {
2048 char *dup;
2049
2050 dup = xmalloc (paren - name + 1);
2051 memcpy (dup, name, paren - name);
2052 dup[paren - name] = 0;
2053
2054 make_cleanup (xfree, dup);
2055 name = dup;
2056 }
2057 }
2058
2059 /* Index version 4 did not support case insensitive searches. But the
2060 indices for case insensitive languages are built in lowercase, therefore
2061 simulate our NAME being searched is also lowercased. */
2062 hash = mapped_index_string_hash ((index->version == 4
2063 && case_sensitivity == case_sensitive_off
2064 ? 5 : index->version),
2065 name);
2066
2067 slot = hash & (index->symbol_table_slots - 1);
2068 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2069 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2070
2071 for (;;)
2072 {
2073 /* Convert a slot number to an offset into the table. */
2074 offset_type i = 2 * slot;
2075 const char *str;
2076 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2077 {
2078 do_cleanups (back_to);
2079 return 0;
2080 }
2081
2082 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2083 if (!cmp (name, str))
2084 {
2085 *vec_out = (offset_type *) (index->constant_pool
2086 + MAYBE_SWAP (index->symbol_table[i + 1]));
2087 do_cleanups (back_to);
2088 return 1;
2089 }
2090
2091 slot = (slot + step) & (index->symbol_table_slots - 1);
2092 }
2093 }
2094
2095 /* Read the index file. If everything went ok, initialize the "quick"
2096 elements of all the CUs and return 1. Otherwise, return 0. */
2097
2098 static int
2099 dwarf2_read_index (struct objfile *objfile)
2100 {
2101 char *addr;
2102 struct mapped_index *map;
2103 offset_type *metadata;
2104 const gdb_byte *cu_list;
2105 const gdb_byte *types_list = NULL;
2106 offset_type version, cu_list_elements;
2107 offset_type types_list_elements = 0;
2108 int i;
2109
2110 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2111 return 0;
2112
2113 /* Older elfutils strip versions could keep the section in the main
2114 executable while splitting it for the separate debug info file. */
2115 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2116 & SEC_HAS_CONTENTS) == 0)
2117 return 0;
2118
2119 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2120
2121 addr = dwarf2_per_objfile->gdb_index.buffer;
2122 /* Version check. */
2123 version = MAYBE_SWAP (*(offset_type *) addr);
2124 /* Versions earlier than 3 emitted every copy of a psymbol. This
2125 causes the index to behave very poorly for certain requests. Version 3
2126 contained incomplete addrmap. So, it seems better to just ignore such
2127 indices. */
2128 if (version < 4)
2129 {
2130 static int warning_printed = 0;
2131 if (!warning_printed)
2132 {
2133 warning (_("Skipping obsolete .gdb_index section in %s."),
2134 objfile->name);
2135 warning_printed = 1;
2136 }
2137 return 0;
2138 }
2139 /* Index version 4 uses a different hash function than index version
2140 5 and later.
2141
2142 Versions earlier than 6 did not emit psymbols for inlined
2143 functions. Using these files will cause GDB not to be able to
2144 set breakpoints on inlined functions by name, so we ignore these
2145 indices unless the --use-deprecated-index-sections command line
2146 option was supplied. */
2147 if (version < 6 && !use_deprecated_index_sections)
2148 {
2149 static int warning_printed = 0;
2150 if (!warning_printed)
2151 {
2152 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2153 "--use-deprecated-index-sections to use them anyway"),
2154 objfile->name);
2155 warning_printed = 1;
2156 }
2157 return 0;
2158 }
2159 /* Indexes with higher version than the one supported by GDB may be no
2160 longer backward compatible. */
2161 if (version > 6)
2162 return 0;
2163
2164 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2165 map->version = version;
2166 map->total_size = dwarf2_per_objfile->gdb_index.size;
2167
2168 metadata = (offset_type *) (addr + sizeof (offset_type));
2169
2170 i = 0;
2171 cu_list = addr + MAYBE_SWAP (metadata[i]);
2172 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2173 / 8);
2174 ++i;
2175
2176 types_list = addr + MAYBE_SWAP (metadata[i]);
2177 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2178 - MAYBE_SWAP (metadata[i]))
2179 / 8);
2180 ++i;
2181
2182 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2183 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2184 - MAYBE_SWAP (metadata[i]));
2185 ++i;
2186
2187 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2188 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2189 - MAYBE_SWAP (metadata[i]))
2190 / (2 * sizeof (offset_type)));
2191 ++i;
2192
2193 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2194
2195 /* Don't use the index if it's empty. */
2196 if (map->symbol_table_slots == 0)
2197 return 0;
2198
2199 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2200 return 0;
2201
2202 if (types_list_elements)
2203 {
2204 struct dwarf2_section_info *section;
2205
2206 /* We can only handle a single .debug_types when we have an
2207 index. */
2208 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2209 return 0;
2210
2211 section = VEC_index (dwarf2_section_info_def,
2212 dwarf2_per_objfile->types, 0);
2213
2214 if (!create_signatured_type_table_from_index (objfile, section,
2215 types_list,
2216 types_list_elements))
2217 return 0;
2218 }
2219
2220 create_addrmap_from_index (objfile, map);
2221
2222 dwarf2_per_objfile->index_table = map;
2223 dwarf2_per_objfile->using_index = 1;
2224 dwarf2_per_objfile->quick_file_names_table =
2225 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2226
2227 return 1;
2228 }
2229
2230 /* A helper for the "quick" functions which sets the global
2231 dwarf2_per_objfile according to OBJFILE. */
2232
2233 static void
2234 dw2_setup (struct objfile *objfile)
2235 {
2236 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2237 gdb_assert (dwarf2_per_objfile);
2238 }
2239
2240 /* A helper for the "quick" functions which attempts to read the line
2241 table for THIS_CU. */
2242
2243 static struct quick_file_names *
2244 dw2_get_file_names (struct objfile *objfile,
2245 struct dwarf2_per_cu_data *this_cu)
2246 {
2247 bfd *abfd = objfile->obfd;
2248 struct line_header *lh;
2249 struct attribute *attr;
2250 struct cleanup *cleanups;
2251 struct die_info *comp_unit_die;
2252 struct dwarf2_section_info* sec;
2253 gdb_byte *info_ptr;
2254 int has_children, i;
2255 struct dwarf2_cu cu;
2256 unsigned int bytes_read;
2257 struct die_reader_specs reader_specs;
2258 char *name, *comp_dir;
2259 void **slot;
2260 struct quick_file_names *qfn;
2261 unsigned int line_offset;
2262
2263 if (this_cu->v.quick->file_names != NULL)
2264 return this_cu->v.quick->file_names;
2265 /* If we know there is no line data, no point in looking again. */
2266 if (this_cu->v.quick->no_file_data)
2267 return NULL;
2268
2269 init_one_comp_unit (&cu, this_cu);
2270 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2271
2272 if (this_cu->debug_types_section)
2273 sec = this_cu->debug_types_section;
2274 else
2275 sec = &dwarf2_per_objfile->info;
2276 dwarf2_read_section (objfile, sec);
2277 info_ptr = sec->buffer + this_cu->offset.sect_off;
2278
2279 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2280 this_cu->debug_types_section != NULL);
2281
2282 /* Skip dummy compilation units. */
2283 if (info_ptr >= (sec->buffer + sec->size)
2284 || peek_abbrev_code (abfd, info_ptr) == 0)
2285 {
2286 do_cleanups (cleanups);
2287 return NULL;
2288 }
2289
2290 dwarf2_read_abbrevs (&cu);
2291 make_cleanup (dwarf2_free_abbrev_table, &cu);
2292
2293 init_cu_die_reader (&reader_specs, &cu);
2294 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2295 &has_children);
2296
2297 lh = NULL;
2298 slot = NULL;
2299 line_offset = 0;
2300 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2301 if (attr)
2302 {
2303 struct quick_file_names find_entry;
2304
2305 line_offset = DW_UNSND (attr);
2306
2307 /* We may have already read in this line header (TU line header sharing).
2308 If we have we're done. */
2309 find_entry.offset = line_offset;
2310 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2311 &find_entry, INSERT);
2312 if (*slot != NULL)
2313 {
2314 do_cleanups (cleanups);
2315 this_cu->v.quick->file_names = *slot;
2316 return *slot;
2317 }
2318
2319 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2320 }
2321 if (lh == NULL)
2322 {
2323 do_cleanups (cleanups);
2324 this_cu->v.quick->no_file_data = 1;
2325 return NULL;
2326 }
2327
2328 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2329 qfn->offset = line_offset;
2330 gdb_assert (slot != NULL);
2331 *slot = qfn;
2332
2333 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2334
2335 qfn->num_file_names = lh->num_file_names;
2336 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2337 lh->num_file_names * sizeof (char *));
2338 for (i = 0; i < lh->num_file_names; ++i)
2339 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2340 qfn->real_names = NULL;
2341
2342 free_line_header (lh);
2343 do_cleanups (cleanups);
2344
2345 this_cu->v.quick->file_names = qfn;
2346 return qfn;
2347 }
2348
2349 /* A helper for the "quick" functions which computes and caches the
2350 real path for a given file name from the line table. */
2351
2352 static const char *
2353 dw2_get_real_path (struct objfile *objfile,
2354 struct quick_file_names *qfn, int index)
2355 {
2356 if (qfn->real_names == NULL)
2357 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2358 qfn->num_file_names, sizeof (char *));
2359
2360 if (qfn->real_names[index] == NULL)
2361 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2362
2363 return qfn->real_names[index];
2364 }
2365
2366 static struct symtab *
2367 dw2_find_last_source_symtab (struct objfile *objfile)
2368 {
2369 int index;
2370
2371 dw2_setup (objfile);
2372 index = dwarf2_per_objfile->n_comp_units - 1;
2373 return dw2_instantiate_symtab (dw2_get_cu (index));
2374 }
2375
2376 /* Traversal function for dw2_forget_cached_source_info. */
2377
2378 static int
2379 dw2_free_cached_file_names (void **slot, void *info)
2380 {
2381 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2382
2383 if (file_data->real_names)
2384 {
2385 int i;
2386
2387 for (i = 0; i < file_data->num_file_names; ++i)
2388 {
2389 xfree ((void*) file_data->real_names[i]);
2390 file_data->real_names[i] = NULL;
2391 }
2392 }
2393
2394 return 1;
2395 }
2396
2397 static void
2398 dw2_forget_cached_source_info (struct objfile *objfile)
2399 {
2400 dw2_setup (objfile);
2401
2402 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2403 dw2_free_cached_file_names, NULL);
2404 }
2405
2406 /* Helper function for dw2_map_symtabs_matching_filename that expands
2407 the symtabs and calls the iterator. */
2408
2409 static int
2410 dw2_map_expand_apply (struct objfile *objfile,
2411 struct dwarf2_per_cu_data *per_cu,
2412 const char *name,
2413 const char *full_path, const char *real_path,
2414 int (*callback) (struct symtab *, void *),
2415 void *data)
2416 {
2417 struct symtab *last_made = objfile->symtabs;
2418
2419 /* Don't visit already-expanded CUs. */
2420 if (per_cu->v.quick->symtab)
2421 return 0;
2422
2423 /* This may expand more than one symtab, and we want to iterate over
2424 all of them. */
2425 dw2_instantiate_symtab (per_cu);
2426
2427 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2428 objfile->symtabs, last_made);
2429 }
2430
2431 /* Implementation of the map_symtabs_matching_filename method. */
2432
2433 static int
2434 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2435 const char *full_path, const char *real_path,
2436 int (*callback) (struct symtab *, void *),
2437 void *data)
2438 {
2439 int i;
2440 const char *name_basename = lbasename (name);
2441 int name_len = strlen (name);
2442 int is_abs = IS_ABSOLUTE_PATH (name);
2443
2444 dw2_setup (objfile);
2445
2446 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2447 + dwarf2_per_objfile->n_type_units); ++i)
2448 {
2449 int j;
2450 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2451 struct quick_file_names *file_data;
2452
2453 /* We only need to look at symtabs not already expanded. */
2454 if (per_cu->v.quick->symtab)
2455 continue;
2456
2457 file_data = dw2_get_file_names (objfile, per_cu);
2458 if (file_data == NULL)
2459 continue;
2460
2461 for (j = 0; j < file_data->num_file_names; ++j)
2462 {
2463 const char *this_name = file_data->file_names[j];
2464
2465 if (FILENAME_CMP (name, this_name) == 0
2466 || (!is_abs && compare_filenames_for_search (this_name,
2467 name, name_len)))
2468 {
2469 if (dw2_map_expand_apply (objfile, per_cu,
2470 name, full_path, real_path,
2471 callback, data))
2472 return 1;
2473 }
2474
2475 /* Before we invoke realpath, which can get expensive when many
2476 files are involved, do a quick comparison of the basenames. */
2477 if (! basenames_may_differ
2478 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2479 continue;
2480
2481 if (full_path != NULL)
2482 {
2483 const char *this_real_name = dw2_get_real_path (objfile,
2484 file_data, j);
2485
2486 if (this_real_name != NULL
2487 && (FILENAME_CMP (full_path, this_real_name) == 0
2488 || (!is_abs
2489 && compare_filenames_for_search (this_real_name,
2490 name, name_len))))
2491 {
2492 if (dw2_map_expand_apply (objfile, per_cu,
2493 name, full_path, real_path,
2494 callback, data))
2495 return 1;
2496 }
2497 }
2498
2499 if (real_path != NULL)
2500 {
2501 const char *this_real_name = dw2_get_real_path (objfile,
2502 file_data, j);
2503
2504 if (this_real_name != NULL
2505 && (FILENAME_CMP (real_path, this_real_name) == 0
2506 || (!is_abs
2507 && compare_filenames_for_search (this_real_name,
2508 name, name_len))))
2509 {
2510 if (dw2_map_expand_apply (objfile, per_cu,
2511 name, full_path, real_path,
2512 callback, data))
2513 return 1;
2514 }
2515 }
2516 }
2517 }
2518
2519 return 0;
2520 }
2521
2522 static struct symtab *
2523 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2524 const char *name, domain_enum domain)
2525 {
2526 /* We do all the work in the pre_expand_symtabs_matching hook
2527 instead. */
2528 return NULL;
2529 }
2530
2531 /* A helper function that expands all symtabs that hold an object
2532 named NAME. */
2533
2534 static void
2535 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2536 {
2537 dw2_setup (objfile);
2538
2539 /* index_table is NULL if OBJF_READNOW. */
2540 if (dwarf2_per_objfile->index_table)
2541 {
2542 offset_type *vec;
2543
2544 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2545 name, &vec))
2546 {
2547 offset_type i, len = MAYBE_SWAP (*vec);
2548 for (i = 0; i < len; ++i)
2549 {
2550 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2551 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2552
2553 dw2_instantiate_symtab (per_cu);
2554 }
2555 }
2556 }
2557 }
2558
2559 static void
2560 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2561 enum block_enum block_kind, const char *name,
2562 domain_enum domain)
2563 {
2564 dw2_do_expand_symtabs_matching (objfile, name);
2565 }
2566
2567 static void
2568 dw2_print_stats (struct objfile *objfile)
2569 {
2570 int i, count;
2571
2572 dw2_setup (objfile);
2573 count = 0;
2574 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2575 + dwarf2_per_objfile->n_type_units); ++i)
2576 {
2577 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2578
2579 if (!per_cu->v.quick->symtab)
2580 ++count;
2581 }
2582 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2583 }
2584
2585 static void
2586 dw2_dump (struct objfile *objfile)
2587 {
2588 /* Nothing worth printing. */
2589 }
2590
2591 static void
2592 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2593 struct section_offsets *delta)
2594 {
2595 /* There's nothing to relocate here. */
2596 }
2597
2598 static void
2599 dw2_expand_symtabs_for_function (struct objfile *objfile,
2600 const char *func_name)
2601 {
2602 dw2_do_expand_symtabs_matching (objfile, func_name);
2603 }
2604
2605 static void
2606 dw2_expand_all_symtabs (struct objfile *objfile)
2607 {
2608 int i;
2609
2610 dw2_setup (objfile);
2611
2612 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2613 + dwarf2_per_objfile->n_type_units); ++i)
2614 {
2615 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2616
2617 dw2_instantiate_symtab (per_cu);
2618 }
2619 }
2620
2621 static void
2622 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2623 const char *filename)
2624 {
2625 int i;
2626
2627 dw2_setup (objfile);
2628
2629 /* We don't need to consider type units here.
2630 This is only called for examining code, e.g. expand_line_sal.
2631 There can be an order of magnitude (or more) more type units
2632 than comp units, and we avoid them if we can. */
2633
2634 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2635 {
2636 int j;
2637 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2638 struct quick_file_names *file_data;
2639
2640 /* We only need to look at symtabs not already expanded. */
2641 if (per_cu->v.quick->symtab)
2642 continue;
2643
2644 file_data = dw2_get_file_names (objfile, per_cu);
2645 if (file_data == NULL)
2646 continue;
2647
2648 for (j = 0; j < file_data->num_file_names; ++j)
2649 {
2650 const char *this_name = file_data->file_names[j];
2651 if (FILENAME_CMP (this_name, filename) == 0)
2652 {
2653 dw2_instantiate_symtab (per_cu);
2654 break;
2655 }
2656 }
2657 }
2658 }
2659
2660 static const char *
2661 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2662 {
2663 struct dwarf2_per_cu_data *per_cu;
2664 offset_type *vec;
2665 struct quick_file_names *file_data;
2666
2667 dw2_setup (objfile);
2668
2669 /* index_table is NULL if OBJF_READNOW. */
2670 if (!dwarf2_per_objfile->index_table)
2671 {
2672 struct symtab *s;
2673
2674 ALL_OBJFILE_SYMTABS (objfile, s)
2675 if (s->primary)
2676 {
2677 struct blockvector *bv = BLOCKVECTOR (s);
2678 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2679 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2680
2681 if (sym)
2682 return sym->symtab->filename;
2683 }
2684 return NULL;
2685 }
2686
2687 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2688 name, &vec))
2689 return NULL;
2690
2691 /* Note that this just looks at the very first one named NAME -- but
2692 actually we are looking for a function. find_main_filename
2693 should be rewritten so that it doesn't require a custom hook. It
2694 could just use the ordinary symbol tables. */
2695 /* vec[0] is the length, which must always be >0. */
2696 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2697
2698 file_data = dw2_get_file_names (objfile, per_cu);
2699 if (file_data == NULL)
2700 return NULL;
2701
2702 return file_data->file_names[file_data->num_file_names - 1];
2703 }
2704
2705 static void
2706 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2707 struct objfile *objfile, int global,
2708 int (*callback) (struct block *,
2709 struct symbol *, void *),
2710 void *data, symbol_compare_ftype *match,
2711 symbol_compare_ftype *ordered_compare)
2712 {
2713 /* Currently unimplemented; used for Ada. The function can be called if the
2714 current language is Ada for a non-Ada objfile using GNU index. As Ada
2715 does not look for non-Ada symbols this function should just return. */
2716 }
2717
2718 static void
2719 dw2_expand_symtabs_matching
2720 (struct objfile *objfile,
2721 int (*file_matcher) (const char *, void *),
2722 int (*name_matcher) (const char *, void *),
2723 enum search_domain kind,
2724 void *data)
2725 {
2726 int i;
2727 offset_type iter;
2728 struct mapped_index *index;
2729
2730 dw2_setup (objfile);
2731
2732 /* index_table is NULL if OBJF_READNOW. */
2733 if (!dwarf2_per_objfile->index_table)
2734 return;
2735 index = dwarf2_per_objfile->index_table;
2736
2737 if (file_matcher != NULL)
2738 {
2739 struct cleanup *cleanup;
2740 htab_t visited_found, visited_not_found;
2741
2742 visited_found = htab_create_alloc (10,
2743 htab_hash_pointer, htab_eq_pointer,
2744 NULL, xcalloc, xfree);
2745 cleanup = make_cleanup_htab_delete (visited_found);
2746 visited_not_found = htab_create_alloc (10,
2747 htab_hash_pointer, htab_eq_pointer,
2748 NULL, xcalloc, xfree);
2749 make_cleanup_htab_delete (visited_not_found);
2750
2751 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2752 + dwarf2_per_objfile->n_type_units); ++i)
2753 {
2754 int j;
2755 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2756 struct quick_file_names *file_data;
2757 void **slot;
2758
2759 per_cu->v.quick->mark = 0;
2760
2761 /* We only need to look at symtabs not already expanded. */
2762 if (per_cu->v.quick->symtab)
2763 continue;
2764
2765 file_data = dw2_get_file_names (objfile, per_cu);
2766 if (file_data == NULL)
2767 continue;
2768
2769 if (htab_find (visited_not_found, file_data) != NULL)
2770 continue;
2771 else if (htab_find (visited_found, file_data) != NULL)
2772 {
2773 per_cu->v.quick->mark = 1;
2774 continue;
2775 }
2776
2777 for (j = 0; j < file_data->num_file_names; ++j)
2778 {
2779 if (file_matcher (file_data->file_names[j], data))
2780 {
2781 per_cu->v.quick->mark = 1;
2782 break;
2783 }
2784 }
2785
2786 slot = htab_find_slot (per_cu->v.quick->mark
2787 ? visited_found
2788 : visited_not_found,
2789 file_data, INSERT);
2790 *slot = file_data;
2791 }
2792
2793 do_cleanups (cleanup);
2794 }
2795
2796 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2797 {
2798 offset_type idx = 2 * iter;
2799 const char *name;
2800 offset_type *vec, vec_len, vec_idx;
2801
2802 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2803 continue;
2804
2805 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2806
2807 if (! (*name_matcher) (name, data))
2808 continue;
2809
2810 /* The name was matched, now expand corresponding CUs that were
2811 marked. */
2812 vec = (offset_type *) (index->constant_pool
2813 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2814 vec_len = MAYBE_SWAP (vec[0]);
2815 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2816 {
2817 struct dwarf2_per_cu_data *per_cu;
2818
2819 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2820 if (file_matcher == NULL || per_cu->v.quick->mark)
2821 dw2_instantiate_symtab (per_cu);
2822 }
2823 }
2824 }
2825
2826 static struct symtab *
2827 dw2_find_pc_sect_symtab (struct objfile *objfile,
2828 struct minimal_symbol *msymbol,
2829 CORE_ADDR pc,
2830 struct obj_section *section,
2831 int warn_if_readin)
2832 {
2833 struct dwarf2_per_cu_data *data;
2834
2835 dw2_setup (objfile);
2836
2837 if (!objfile->psymtabs_addrmap)
2838 return NULL;
2839
2840 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2841 if (!data)
2842 return NULL;
2843
2844 if (warn_if_readin && data->v.quick->symtab)
2845 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2846 paddress (get_objfile_arch (objfile), pc));
2847
2848 return dw2_instantiate_symtab (data);
2849 }
2850
2851 static void
2852 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2853 void *data, int need_fullname)
2854 {
2855 int i;
2856 struct cleanup *cleanup;
2857 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2858 NULL, xcalloc, xfree);
2859
2860 cleanup = make_cleanup_htab_delete (visited);
2861 dw2_setup (objfile);
2862
2863 /* We can ignore file names coming from already-expanded CUs. */
2864 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2865 + dwarf2_per_objfile->n_type_units); ++i)
2866 {
2867 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2868
2869 if (per_cu->v.quick->symtab)
2870 {
2871 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2872 INSERT);
2873
2874 *slot = per_cu->v.quick->file_names;
2875 }
2876 }
2877
2878 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2879 + dwarf2_per_objfile->n_type_units); ++i)
2880 {
2881 int j;
2882 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2883 struct quick_file_names *file_data;
2884 void **slot;
2885
2886 /* We only need to look at symtabs not already expanded. */
2887 if (per_cu->v.quick->symtab)
2888 continue;
2889
2890 file_data = dw2_get_file_names (objfile, per_cu);
2891 if (file_data == NULL)
2892 continue;
2893
2894 slot = htab_find_slot (visited, file_data, INSERT);
2895 if (*slot)
2896 {
2897 /* Already visited. */
2898 continue;
2899 }
2900 *slot = file_data;
2901
2902 for (j = 0; j < file_data->num_file_names; ++j)
2903 {
2904 const char *this_real_name;
2905
2906 if (need_fullname)
2907 this_real_name = dw2_get_real_path (objfile, file_data, j);
2908 else
2909 this_real_name = NULL;
2910 (*fun) (file_data->file_names[j], this_real_name, data);
2911 }
2912 }
2913
2914 do_cleanups (cleanup);
2915 }
2916
2917 static int
2918 dw2_has_symbols (struct objfile *objfile)
2919 {
2920 return 1;
2921 }
2922
2923 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2924 {
2925 dw2_has_symbols,
2926 dw2_find_last_source_symtab,
2927 dw2_forget_cached_source_info,
2928 dw2_map_symtabs_matching_filename,
2929 dw2_lookup_symbol,
2930 dw2_pre_expand_symtabs_matching,
2931 dw2_print_stats,
2932 dw2_dump,
2933 dw2_relocate,
2934 dw2_expand_symtabs_for_function,
2935 dw2_expand_all_symtabs,
2936 dw2_expand_symtabs_with_filename,
2937 dw2_find_symbol_file,
2938 dw2_map_matching_symbols,
2939 dw2_expand_symtabs_matching,
2940 dw2_find_pc_sect_symtab,
2941 dw2_map_symbol_filenames
2942 };
2943
2944 /* Initialize for reading DWARF for this objfile. Return 0 if this
2945 file will use psymtabs, or 1 if using the GNU index. */
2946
2947 int
2948 dwarf2_initialize_objfile (struct objfile *objfile)
2949 {
2950 /* If we're about to read full symbols, don't bother with the
2951 indices. In this case we also don't care if some other debug
2952 format is making psymtabs, because they are all about to be
2953 expanded anyway. */
2954 if ((objfile->flags & OBJF_READNOW))
2955 {
2956 int i;
2957
2958 dwarf2_per_objfile->using_index = 1;
2959 create_all_comp_units (objfile);
2960 create_all_type_units (objfile);
2961 dwarf2_per_objfile->quick_file_names_table =
2962 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2963
2964 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2965 + dwarf2_per_objfile->n_type_units); ++i)
2966 {
2967 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2968
2969 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2970 struct dwarf2_per_cu_quick_data);
2971 }
2972
2973 /* Return 1 so that gdb sees the "quick" functions. However,
2974 these functions will be no-ops because we will have expanded
2975 all symtabs. */
2976 return 1;
2977 }
2978
2979 if (dwarf2_read_index (objfile))
2980 return 1;
2981
2982 return 0;
2983 }
2984
2985 \f
2986
2987 /* Build a partial symbol table. */
2988
2989 void
2990 dwarf2_build_psymtabs (struct objfile *objfile)
2991 {
2992 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2993 {
2994 init_psymbol_list (objfile, 1024);
2995 }
2996
2997 dwarf2_build_psymtabs_hard (objfile);
2998 }
2999
3000 /* Return TRUE if OFFSET is within CU_HEADER. */
3001
3002 static inline int
3003 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3004 {
3005 sect_offset bottom = { cu_header->offset.sect_off };
3006 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3007 + cu_header->initial_length_size) };
3008
3009 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3010 }
3011
3012 /* Read in the comp unit header information from the debug_info at info_ptr.
3013 NOTE: This leaves members offset, first_die_offset to be filled in
3014 by the caller. */
3015
3016 static gdb_byte *
3017 read_comp_unit_head (struct comp_unit_head *cu_header,
3018 gdb_byte *info_ptr, bfd *abfd)
3019 {
3020 int signed_addr;
3021 unsigned int bytes_read;
3022
3023 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3024 cu_header->initial_length_size = bytes_read;
3025 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3026 info_ptr += bytes_read;
3027 cu_header->version = read_2_bytes (abfd, info_ptr);
3028 info_ptr += 2;
3029 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3030 &bytes_read);
3031 info_ptr += bytes_read;
3032 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3033 info_ptr += 1;
3034 signed_addr = bfd_get_sign_extend_vma (abfd);
3035 if (signed_addr < 0)
3036 internal_error (__FILE__, __LINE__,
3037 _("read_comp_unit_head: dwarf from non elf file"));
3038 cu_header->signed_addr_p = signed_addr;
3039
3040 return info_ptr;
3041 }
3042
3043 /* Subroutine of read_and_check_comp_unit_head and
3044 read_and_check_type_unit_head to simplify them.
3045 Perform various error checking on the header. */
3046
3047 static void
3048 error_check_comp_unit_head (struct comp_unit_head *header,
3049 struct dwarf2_section_info *section)
3050 {
3051 bfd *abfd = section->asection->owner;
3052 const char *filename = bfd_get_filename (abfd);
3053
3054 if (header->version != 2 && header->version != 3 && header->version != 4)
3055 error (_("Dwarf Error: wrong version in compilation unit header "
3056 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3057 filename);
3058
3059 if (header->abbrev_offset.sect_off
3060 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3061 &dwarf2_per_objfile->abbrev))
3062 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3063 "(offset 0x%lx + 6) [in module %s]"),
3064 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3065 filename);
3066
3067 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3068 avoid potential 32-bit overflow. */
3069 if (((unsigned long) header->offset.sect_off
3070 + header->length + header->initial_length_size)
3071 > section->size)
3072 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3073 "(offset 0x%lx + 0) [in module %s]"),
3074 (long) header->length, (long) header->offset.sect_off,
3075 filename);
3076 }
3077
3078 /* Read in a CU/TU header and perform some basic error checking.
3079 The contents of the header are stored in HEADER.
3080 The result is a pointer to the start of the first DIE. */
3081
3082 static gdb_byte *
3083 read_and_check_comp_unit_head (struct comp_unit_head *header,
3084 struct dwarf2_section_info *section,
3085 gdb_byte *info_ptr,
3086 int is_debug_types_section)
3087 {
3088 gdb_byte *beg_of_comp_unit = info_ptr;
3089 bfd *abfd = section->asection->owner;
3090
3091 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3092
3093 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3094
3095 /* If we're reading a type unit, skip over the signature and
3096 type_offset fields. */
3097 if (is_debug_types_section)
3098 info_ptr += 8 /*signature*/ + header->offset_size;
3099
3100 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3101
3102 error_check_comp_unit_head (header, section);
3103
3104 return info_ptr;
3105 }
3106
3107 /* Read in the types comp unit header information from .debug_types entry at
3108 types_ptr. The result is a pointer to one past the end of the header. */
3109
3110 static gdb_byte *
3111 read_and_check_type_unit_head (struct comp_unit_head *header,
3112 struct dwarf2_section_info *section,
3113 gdb_byte *info_ptr,
3114 ULONGEST *signature, cu_offset *type_offset)
3115 {
3116 gdb_byte *beg_of_comp_unit = info_ptr;
3117 bfd *abfd = section->asection->owner;
3118
3119 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3120
3121 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3122
3123 /* If we're reading a type unit, skip over the signature and
3124 type_offset fields. */
3125 if (signature != NULL)
3126 *signature = read_8_bytes (abfd, info_ptr);
3127 info_ptr += 8;
3128 if (type_offset != NULL)
3129 type_offset->cu_off = read_offset_1 (abfd, info_ptr, header->offset_size);
3130 info_ptr += header->offset_size;
3131
3132 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3133
3134 error_check_comp_unit_head (header, section);
3135
3136 return info_ptr;
3137 }
3138
3139 /* Allocate a new partial symtab for file named NAME and mark this new
3140 partial symtab as being an include of PST. */
3141
3142 static void
3143 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3144 struct objfile *objfile)
3145 {
3146 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3147
3148 subpst->section_offsets = pst->section_offsets;
3149 subpst->textlow = 0;
3150 subpst->texthigh = 0;
3151
3152 subpst->dependencies = (struct partial_symtab **)
3153 obstack_alloc (&objfile->objfile_obstack,
3154 sizeof (struct partial_symtab *));
3155 subpst->dependencies[0] = pst;
3156 subpst->number_of_dependencies = 1;
3157
3158 subpst->globals_offset = 0;
3159 subpst->n_global_syms = 0;
3160 subpst->statics_offset = 0;
3161 subpst->n_static_syms = 0;
3162 subpst->symtab = NULL;
3163 subpst->read_symtab = pst->read_symtab;
3164 subpst->readin = 0;
3165
3166 /* No private part is necessary for include psymtabs. This property
3167 can be used to differentiate between such include psymtabs and
3168 the regular ones. */
3169 subpst->read_symtab_private = NULL;
3170 }
3171
3172 /* Read the Line Number Program data and extract the list of files
3173 included by the source file represented by PST. Build an include
3174 partial symtab for each of these included files. */
3175
3176 static void
3177 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3178 struct die_info *die,
3179 struct partial_symtab *pst)
3180 {
3181 struct objfile *objfile = cu->objfile;
3182 bfd *abfd = objfile->obfd;
3183 struct line_header *lh = NULL;
3184 struct attribute *attr;
3185
3186 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3187 if (attr)
3188 {
3189 unsigned int line_offset = DW_UNSND (attr);
3190
3191 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3192 }
3193 if (lh == NULL)
3194 return; /* No linetable, so no includes. */
3195
3196 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3197 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3198
3199 free_line_header (lh);
3200 }
3201
3202 static hashval_t
3203 hash_signatured_type (const void *item)
3204 {
3205 const struct signatured_type *sig_type = item;
3206
3207 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3208 return sig_type->signature;
3209 }
3210
3211 static int
3212 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3213 {
3214 const struct signatured_type *lhs = item_lhs;
3215 const struct signatured_type *rhs = item_rhs;
3216
3217 return lhs->signature == rhs->signature;
3218 }
3219
3220 /* Allocate a hash table for signatured types. */
3221
3222 static htab_t
3223 allocate_signatured_type_table (struct objfile *objfile)
3224 {
3225 return htab_create_alloc_ex (41,
3226 hash_signatured_type,
3227 eq_signatured_type,
3228 NULL,
3229 &objfile->objfile_obstack,
3230 hashtab_obstack_allocate,
3231 dummy_obstack_deallocate);
3232 }
3233
3234 /* A helper function to add a signatured type CU to a table. */
3235
3236 static int
3237 add_signatured_type_cu_to_table (void **slot, void *datum)
3238 {
3239 struct signatured_type *sigt = *slot;
3240 struct dwarf2_per_cu_data ***datap = datum;
3241
3242 **datap = &sigt->per_cu;
3243 ++*datap;
3244
3245 return 1;
3246 }
3247
3248 /* Create the hash table of all entries in the .debug_types section(s).
3249 The result is zero if there are no .debug_types sections,
3250 otherwise non-zero. */
3251
3252 static int
3253 create_all_type_units (struct objfile *objfile)
3254 {
3255 htab_t types_htab = NULL;
3256 struct dwarf2_per_cu_data **iter;
3257 int ix;
3258 struct dwarf2_section_info *section;
3259
3260 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3261 {
3262 dwarf2_per_objfile->signatured_types = NULL;
3263 return 0;
3264 }
3265
3266 for (ix = 0;
3267 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3268 ix, section);
3269 ++ix)
3270 {
3271 gdb_byte *info_ptr, *end_ptr;
3272
3273 dwarf2_read_section (objfile, section);
3274 info_ptr = section->buffer;
3275
3276 if (info_ptr == NULL)
3277 continue;
3278
3279 if (types_htab == NULL)
3280 types_htab = allocate_signatured_type_table (objfile);
3281
3282 if (dwarf2_die_debug)
3283 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3284
3285 end_ptr = info_ptr + section->size;
3286 while (info_ptr < end_ptr)
3287 {
3288 sect_offset offset;
3289 cu_offset type_offset;
3290 ULONGEST signature;
3291 struct signatured_type *sig_type;
3292 void **slot;
3293 gdb_byte *ptr = info_ptr;
3294 struct comp_unit_head header;
3295
3296 offset.sect_off = ptr - section->buffer;
3297
3298 /* We need to read the type's signature in order to build the hash
3299 table, but we don't need anything else just yet. */
3300
3301 ptr = read_and_check_type_unit_head (&header, section, ptr,
3302 &signature, &type_offset);
3303
3304 /* Skip dummy type units. */
3305 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3306 {
3307 info_ptr = info_ptr + header.initial_length_size + header.length;
3308 continue;
3309 }
3310
3311 sig_type = obstack_alloc (&objfile->objfile_obstack, sizeof (*sig_type));
3312 memset (sig_type, 0, sizeof (*sig_type));
3313 sig_type->signature = signature;
3314 sig_type->type_offset = type_offset;
3315 sig_type->per_cu.objfile = objfile;
3316 sig_type->per_cu.debug_types_section = section;
3317 sig_type->per_cu.offset = offset;
3318
3319 slot = htab_find_slot (types_htab, sig_type, INSERT);
3320 gdb_assert (slot != NULL);
3321 if (*slot != NULL)
3322 {
3323 const struct signatured_type *dup_sig = *slot;
3324
3325 complaint (&symfile_complaints,
3326 _("debug type entry at offset 0x%x is duplicate to the "
3327 "entry at offset 0x%x, signature 0x%s"),
3328 offset.sect_off, dup_sig->per_cu.offset.sect_off,
3329 phex (signature, sizeof (signature)));
3330 gdb_assert (signature == dup_sig->signature);
3331 }
3332 *slot = sig_type;
3333
3334 if (dwarf2_die_debug)
3335 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3336 offset.sect_off,
3337 phex (signature, sizeof (signature)));
3338
3339 info_ptr = info_ptr + header.initial_length_size + header.length;
3340 }
3341 }
3342
3343 dwarf2_per_objfile->signatured_types = types_htab;
3344
3345 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3346 dwarf2_per_objfile->all_type_units
3347 = obstack_alloc (&objfile->objfile_obstack,
3348 dwarf2_per_objfile->n_type_units
3349 * sizeof (struct dwarf2_per_cu_data *));
3350 iter = &dwarf2_per_objfile->all_type_units[0];
3351 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3352 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3353 == dwarf2_per_objfile->n_type_units);
3354
3355 return 1;
3356 }
3357
3358 /* Lookup a signature based type for DW_FORM_ref_sig8.
3359 Returns NULL if signature SIG is not present in the table. */
3360
3361 static struct signatured_type *
3362 lookup_signatured_type (ULONGEST sig)
3363 {
3364 struct signatured_type find_entry, *entry;
3365
3366 if (dwarf2_per_objfile->signatured_types == NULL)
3367 {
3368 complaint (&symfile_complaints,
3369 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3370 return NULL;
3371 }
3372
3373 find_entry.signature = sig;
3374 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3375 return entry;
3376 }
3377
3378 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3379
3380 static void
3381 init_cu_die_reader (struct die_reader_specs *reader,
3382 struct dwarf2_cu *cu)
3383 {
3384 reader->abfd = cu->objfile->obfd;
3385 reader->cu = cu;
3386 if (cu->per_cu->debug_types_section)
3387 {
3388 gdb_assert (cu->per_cu->debug_types_section->readin);
3389 reader->buffer = cu->per_cu->debug_types_section->buffer;
3390 }
3391 else
3392 {
3393 gdb_assert (dwarf2_per_objfile->info.readin);
3394 reader->buffer = dwarf2_per_objfile->info.buffer;
3395 }
3396 }
3397
3398 /* Find the base address of the compilation unit for range lists and
3399 location lists. It will normally be specified by DW_AT_low_pc.
3400 In DWARF-3 draft 4, the base address could be overridden by
3401 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3402 compilation units with discontinuous ranges. */
3403
3404 static void
3405 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3406 {
3407 struct attribute *attr;
3408
3409 cu->base_known = 0;
3410 cu->base_address = 0;
3411
3412 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3413 if (attr)
3414 {
3415 cu->base_address = DW_ADDR (attr);
3416 cu->base_known = 1;
3417 }
3418 else
3419 {
3420 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3421 if (attr)
3422 {
3423 cu->base_address = DW_ADDR (attr);
3424 cu->base_known = 1;
3425 }
3426 }
3427 }
3428
3429 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3430 to combine the common parts.
3431 Process compilation unit THIS_CU for a psymtab.
3432 SECTION is the section the CU/TU comes from,
3433 either .debug_info or .debug_types. */
3434
3435 static void
3436 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3437 struct dwarf2_section_info *section,
3438 int is_debug_types_section)
3439 {
3440 struct objfile *objfile = this_cu->objfile;
3441 bfd *abfd = objfile->obfd;
3442 gdb_byte *buffer = section->buffer;
3443 gdb_byte *info_ptr = buffer + this_cu->offset.sect_off;
3444 unsigned int buffer_size = section->size;
3445 gdb_byte *beg_of_comp_unit = info_ptr;
3446 struct die_info *comp_unit_die;
3447 struct partial_symtab *pst;
3448 CORE_ADDR baseaddr;
3449 struct cleanup *back_to_inner;
3450 struct dwarf2_cu cu;
3451 int has_children, has_pc_info;
3452 struct attribute *attr;
3453 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3454 struct die_reader_specs reader_specs;
3455 const char *filename;
3456
3457 /* If this compilation unit was already read in, free the
3458 cached copy in order to read it in again. This is
3459 necessary because we skipped some symbols when we first
3460 read in the compilation unit (see load_partial_dies).
3461 This problem could be avoided, but the benefit is
3462 unclear. */
3463 if (this_cu->cu != NULL)
3464 free_one_cached_comp_unit (this_cu->cu);
3465
3466 /* Note that this is a pointer to our stack frame, being
3467 added to a global data structure. It will be cleaned up
3468 in free_stack_comp_unit when we finish with this
3469 compilation unit. */
3470 init_one_comp_unit (&cu, this_cu);
3471 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3472
3473 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3474 is_debug_types_section);
3475
3476 /* Skip dummy compilation units. */
3477 if (info_ptr >= buffer + buffer_size
3478 || peek_abbrev_code (abfd, info_ptr) == 0)
3479 {
3480 do_cleanups (back_to_inner);
3481 return;
3482 }
3483
3484 cu.list_in_scope = &file_symbols;
3485
3486 /* Read the abbrevs for this compilation unit into a table. */
3487 dwarf2_read_abbrevs (&cu);
3488 make_cleanup (dwarf2_free_abbrev_table, &cu);
3489
3490 /* Read the compilation unit die. */
3491 init_cu_die_reader (&reader_specs, &cu);
3492 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3493 &has_children);
3494
3495 if (is_debug_types_section)
3496 {
3497 /* LENGTH has not been set yet for type units. */
3498 gdb_assert (this_cu->offset.sect_off == cu.header.offset.sect_off);
3499 this_cu->length = cu.header.length + cu.header.initial_length_size;
3500 }
3501 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3502 {
3503 do_cleanups (back_to_inner);
3504 return;
3505 }
3506
3507 prepare_one_comp_unit (&cu, comp_unit_die);
3508
3509 /* Allocate a new partial symbol table structure. */
3510 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3511 if (attr == NULL || !DW_STRING (attr))
3512 filename = "";
3513 else
3514 filename = DW_STRING (attr);
3515 pst = start_psymtab_common (objfile, objfile->section_offsets,
3516 filename,
3517 /* TEXTLOW and TEXTHIGH are set below. */
3518 0,
3519 objfile->global_psymbols.next,
3520 objfile->static_psymbols.next);
3521 pst->psymtabs_addrmap_supported = 1;
3522
3523 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3524 if (attr != NULL)
3525 pst->dirname = DW_STRING (attr);
3526
3527 pst->read_symtab_private = this_cu;
3528
3529 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3530
3531 /* Store the function that reads in the rest of the symbol table. */
3532 pst->read_symtab = dwarf2_psymtab_to_symtab;
3533
3534 this_cu->v.psymtab = pst;
3535
3536 dwarf2_find_base_address (comp_unit_die, &cu);
3537
3538 /* Possibly set the default values of LOWPC and HIGHPC from
3539 `DW_AT_ranges'. */
3540 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3541 &best_highpc, &cu, pst);
3542 if (has_pc_info == 1 && best_lowpc < best_highpc)
3543 /* Store the contiguous range if it is not empty; it can be empty for
3544 CUs with no code. */
3545 addrmap_set_empty (objfile->psymtabs_addrmap,
3546 best_lowpc + baseaddr,
3547 best_highpc + baseaddr - 1, pst);
3548
3549 /* Check if comp unit has_children.
3550 If so, read the rest of the partial symbols from this comp unit.
3551 If not, there's no more debug_info for this comp unit. */
3552 if (has_children)
3553 {
3554 struct partial_die_info *first_die;
3555 CORE_ADDR lowpc, highpc;
3556
3557 lowpc = ((CORE_ADDR) -1);
3558 highpc = ((CORE_ADDR) 0);
3559
3560 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3561
3562 scan_partial_symbols (first_die, &lowpc, &highpc,
3563 ! has_pc_info, &cu);
3564
3565 /* If we didn't find a lowpc, set it to highpc to avoid
3566 complaints from `maint check'. */
3567 if (lowpc == ((CORE_ADDR) -1))
3568 lowpc = highpc;
3569
3570 /* If the compilation unit didn't have an explicit address range,
3571 then use the information extracted from its child dies. */
3572 if (! has_pc_info)
3573 {
3574 best_lowpc = lowpc;
3575 best_highpc = highpc;
3576 }
3577 }
3578 pst->textlow = best_lowpc + baseaddr;
3579 pst->texthigh = best_highpc + baseaddr;
3580
3581 pst->n_global_syms = objfile->global_psymbols.next -
3582 (objfile->global_psymbols.list + pst->globals_offset);
3583 pst->n_static_syms = objfile->static_psymbols.next -
3584 (objfile->static_psymbols.list + pst->statics_offset);
3585 sort_pst_symbols (pst);
3586
3587 if (is_debug_types_section)
3588 {
3589 /* It's not clear we want to do anything with stmt lists here.
3590 Waiting to see what gcc ultimately does. */
3591 }
3592 else
3593 {
3594 /* Get the list of files included in the current compilation unit,
3595 and build a psymtab for each of them. */
3596 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3597 }
3598
3599 do_cleanups (back_to_inner);
3600 }
3601
3602 /* Traversal function for htab_traverse_noresize.
3603 Process one .debug_types comp-unit. */
3604
3605 static int
3606 process_type_comp_unit (void **slot, void *info)
3607 {
3608 struct signatured_type *entry = (struct signatured_type *) *slot;
3609 struct dwarf2_per_cu_data *this_cu;
3610
3611 gdb_assert (info == NULL);
3612 this_cu = &entry->per_cu;
3613
3614 gdb_assert (this_cu->debug_types_section->readin);
3615 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3616
3617 return 1;
3618 }
3619
3620 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3621 Build partial symbol tables for the .debug_types comp-units. */
3622
3623 static void
3624 build_type_psymtabs (struct objfile *objfile)
3625 {
3626 if (! create_all_type_units (objfile))
3627 return;
3628
3629 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3630 process_type_comp_unit, NULL);
3631 }
3632
3633 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3634
3635 static void
3636 psymtabs_addrmap_cleanup (void *o)
3637 {
3638 struct objfile *objfile = o;
3639
3640 objfile->psymtabs_addrmap = NULL;
3641 }
3642
3643 /* Build the partial symbol table by doing a quick pass through the
3644 .debug_info and .debug_abbrev sections. */
3645
3646 static void
3647 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3648 {
3649 struct cleanup *back_to, *addrmap_cleanup;
3650 struct obstack temp_obstack;
3651 int i;
3652
3653 dwarf2_per_objfile->reading_partial_symbols = 1;
3654
3655 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3656
3657 /* Any cached compilation units will be linked by the per-objfile
3658 read_in_chain. Make sure to free them when we're done. */
3659 back_to = make_cleanup (free_cached_comp_units, NULL);
3660
3661 build_type_psymtabs (objfile);
3662
3663 create_all_comp_units (objfile);
3664
3665 /* Create a temporary address map on a temporary obstack. We later
3666 copy this to the final obstack. */
3667 obstack_init (&temp_obstack);
3668 make_cleanup_obstack_free (&temp_obstack);
3669 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3670 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3671
3672 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3673 {
3674 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3675
3676 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3677 }
3678
3679 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3680 &objfile->objfile_obstack);
3681 discard_cleanups (addrmap_cleanup);
3682
3683 do_cleanups (back_to);
3684 }
3685
3686 /* Load the partial DIEs for a secondary CU into memory. */
3687
3688 static void
3689 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3690 {
3691 struct objfile *objfile = this_cu->objfile;
3692 bfd *abfd = objfile->obfd;
3693 gdb_byte *info_ptr;
3694 struct die_info *comp_unit_die;
3695 struct dwarf2_cu *cu;
3696 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3697 int has_children;
3698 struct die_reader_specs reader_specs;
3699 int read_cu = 0;
3700 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
3701
3702 gdb_assert (! this_cu->debug_types_section);
3703
3704 gdb_assert (section->readin);
3705 info_ptr = section->buffer + this_cu->offset.sect_off;
3706
3707 if (this_cu->cu == NULL)
3708 {
3709 cu = xmalloc (sizeof (*cu));
3710 init_one_comp_unit (cu, this_cu);
3711
3712 read_cu = 1;
3713
3714 /* If an error occurs while loading, release our storage. */
3715 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3716
3717 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3718 0);
3719
3720 /* Skip dummy compilation units. */
3721 if (info_ptr >= (section->buffer + section->size)
3722 || peek_abbrev_code (abfd, info_ptr) == 0)
3723 {
3724 do_cleanups (free_cu_cleanup);
3725 return;
3726 }
3727 }
3728 else
3729 {
3730 cu = this_cu->cu;
3731 info_ptr += cu->header.first_die_offset.cu_off;
3732 }
3733
3734 /* Read the abbrevs for this compilation unit into a table. */
3735 gdb_assert (cu->dwarf2_abbrevs == NULL);
3736 dwarf2_read_abbrevs (cu);
3737 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3738
3739 /* Read the compilation unit die. */
3740 init_cu_die_reader (&reader_specs, cu);
3741 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3742 &has_children);
3743
3744 prepare_one_comp_unit (cu, comp_unit_die);
3745
3746 /* Check if comp unit has_children.
3747 If so, read the rest of the partial symbols from this comp unit.
3748 If not, there's no more debug_info for this comp unit. */
3749 if (has_children)
3750 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
3751
3752 do_cleanups (free_abbrevs_cleanup);
3753
3754 if (read_cu)
3755 {
3756 /* We've successfully allocated this compilation unit. Let our
3757 caller clean it up when finished with it. */
3758 discard_cleanups (free_cu_cleanup);
3759
3760 /* Link this CU into read_in_chain. */
3761 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3762 dwarf2_per_objfile->read_in_chain = this_cu;
3763 }
3764 }
3765
3766 /* Create a list of all compilation units in OBJFILE.
3767 This is only done for -readnow and building partial symtabs. */
3768
3769 static void
3770 create_all_comp_units (struct objfile *objfile)
3771 {
3772 int n_allocated;
3773 int n_comp_units;
3774 struct dwarf2_per_cu_data **all_comp_units;
3775 gdb_byte *info_ptr;
3776
3777 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3778 info_ptr = dwarf2_per_objfile->info.buffer;
3779
3780 n_comp_units = 0;
3781 n_allocated = 10;
3782 all_comp_units = xmalloc (n_allocated
3783 * sizeof (struct dwarf2_per_cu_data *));
3784
3785 while (info_ptr < dwarf2_per_objfile->info.buffer
3786 + dwarf2_per_objfile->info.size)
3787 {
3788 unsigned int length, initial_length_size;
3789 struct dwarf2_per_cu_data *this_cu;
3790 sect_offset offset;
3791
3792 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
3793
3794 /* Read just enough information to find out where the next
3795 compilation unit is. */
3796 length = read_initial_length (objfile->obfd, info_ptr,
3797 &initial_length_size);
3798
3799 /* Save the compilation unit for later lookup. */
3800 this_cu = obstack_alloc (&objfile->objfile_obstack,
3801 sizeof (struct dwarf2_per_cu_data));
3802 memset (this_cu, 0, sizeof (*this_cu));
3803 this_cu->offset = offset;
3804 this_cu->length = length + initial_length_size;
3805 this_cu->objfile = objfile;
3806
3807 if (n_comp_units == n_allocated)
3808 {
3809 n_allocated *= 2;
3810 all_comp_units = xrealloc (all_comp_units,
3811 n_allocated
3812 * sizeof (struct dwarf2_per_cu_data *));
3813 }
3814 all_comp_units[n_comp_units++] = this_cu;
3815
3816 info_ptr = info_ptr + this_cu->length;
3817 }
3818
3819 dwarf2_per_objfile->all_comp_units
3820 = obstack_alloc (&objfile->objfile_obstack,
3821 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3822 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3823 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3824 xfree (all_comp_units);
3825 dwarf2_per_objfile->n_comp_units = n_comp_units;
3826 }
3827
3828 /* Process all loaded DIEs for compilation unit CU, starting at
3829 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3830 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3831 DW_AT_ranges). If NEED_PC is set, then this function will set
3832 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3833 and record the covered ranges in the addrmap. */
3834
3835 static void
3836 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3837 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3838 {
3839 struct partial_die_info *pdi;
3840
3841 /* Now, march along the PDI's, descending into ones which have
3842 interesting children but skipping the children of the other ones,
3843 until we reach the end of the compilation unit. */
3844
3845 pdi = first_die;
3846
3847 while (pdi != NULL)
3848 {
3849 fixup_partial_die (pdi, cu);
3850
3851 /* Anonymous namespaces or modules have no name but have interesting
3852 children, so we need to look at them. Ditto for anonymous
3853 enums. */
3854
3855 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3856 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3857 {
3858 switch (pdi->tag)
3859 {
3860 case DW_TAG_subprogram:
3861 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3862 break;
3863 case DW_TAG_constant:
3864 case DW_TAG_variable:
3865 case DW_TAG_typedef:
3866 case DW_TAG_union_type:
3867 if (!pdi->is_declaration)
3868 {
3869 add_partial_symbol (pdi, cu);
3870 }
3871 break;
3872 case DW_TAG_class_type:
3873 case DW_TAG_interface_type:
3874 case DW_TAG_structure_type:
3875 if (!pdi->is_declaration)
3876 {
3877 add_partial_symbol (pdi, cu);
3878 }
3879 break;
3880 case DW_TAG_enumeration_type:
3881 if (!pdi->is_declaration)
3882 add_partial_enumeration (pdi, cu);
3883 break;
3884 case DW_TAG_base_type:
3885 case DW_TAG_subrange_type:
3886 /* File scope base type definitions are added to the partial
3887 symbol table. */
3888 add_partial_symbol (pdi, cu);
3889 break;
3890 case DW_TAG_namespace:
3891 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3892 break;
3893 case DW_TAG_module:
3894 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3895 break;
3896 default:
3897 break;
3898 }
3899 }
3900
3901 /* If the die has a sibling, skip to the sibling. */
3902
3903 pdi = pdi->die_sibling;
3904 }
3905 }
3906
3907 /* Functions used to compute the fully scoped name of a partial DIE.
3908
3909 Normally, this is simple. For C++, the parent DIE's fully scoped
3910 name is concatenated with "::" and the partial DIE's name. For
3911 Java, the same thing occurs except that "." is used instead of "::".
3912 Enumerators are an exception; they use the scope of their parent
3913 enumeration type, i.e. the name of the enumeration type is not
3914 prepended to the enumerator.
3915
3916 There are two complexities. One is DW_AT_specification; in this
3917 case "parent" means the parent of the target of the specification,
3918 instead of the direct parent of the DIE. The other is compilers
3919 which do not emit DW_TAG_namespace; in this case we try to guess
3920 the fully qualified name of structure types from their members'
3921 linkage names. This must be done using the DIE's children rather
3922 than the children of any DW_AT_specification target. We only need
3923 to do this for structures at the top level, i.e. if the target of
3924 any DW_AT_specification (if any; otherwise the DIE itself) does not
3925 have a parent. */
3926
3927 /* Compute the scope prefix associated with PDI's parent, in
3928 compilation unit CU. The result will be allocated on CU's
3929 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3930 field. NULL is returned if no prefix is necessary. */
3931 static char *
3932 partial_die_parent_scope (struct partial_die_info *pdi,
3933 struct dwarf2_cu *cu)
3934 {
3935 char *grandparent_scope;
3936 struct partial_die_info *parent, *real_pdi;
3937
3938 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3939 then this means the parent of the specification DIE. */
3940
3941 real_pdi = pdi;
3942 while (real_pdi->has_specification)
3943 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3944
3945 parent = real_pdi->die_parent;
3946 if (parent == NULL)
3947 return NULL;
3948
3949 if (parent->scope_set)
3950 return parent->scope;
3951
3952 fixup_partial_die (parent, cu);
3953
3954 grandparent_scope = partial_die_parent_scope (parent, cu);
3955
3956 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3957 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3958 Work around this problem here. */
3959 if (cu->language == language_cplus
3960 && parent->tag == DW_TAG_namespace
3961 && strcmp (parent->name, "::") == 0
3962 && grandparent_scope == NULL)
3963 {
3964 parent->scope = NULL;
3965 parent->scope_set = 1;
3966 return NULL;
3967 }
3968
3969 if (pdi->tag == DW_TAG_enumerator)
3970 /* Enumerators should not get the name of the enumeration as a prefix. */
3971 parent->scope = grandparent_scope;
3972 else if (parent->tag == DW_TAG_namespace
3973 || parent->tag == DW_TAG_module
3974 || parent->tag == DW_TAG_structure_type
3975 || parent->tag == DW_TAG_class_type
3976 || parent->tag == DW_TAG_interface_type
3977 || parent->tag == DW_TAG_union_type
3978 || parent->tag == DW_TAG_enumeration_type)
3979 {
3980 if (grandparent_scope == NULL)
3981 parent->scope = parent->name;
3982 else
3983 parent->scope = typename_concat (&cu->comp_unit_obstack,
3984 grandparent_scope,
3985 parent->name, 0, cu);
3986 }
3987 else
3988 {
3989 /* FIXME drow/2004-04-01: What should we be doing with
3990 function-local names? For partial symbols, we should probably be
3991 ignoring them. */
3992 complaint (&symfile_complaints,
3993 _("unhandled containing DIE tag %d for DIE at %d"),
3994 parent->tag, pdi->offset.sect_off);
3995 parent->scope = grandparent_scope;
3996 }
3997
3998 parent->scope_set = 1;
3999 return parent->scope;
4000 }
4001
4002 /* Return the fully scoped name associated with PDI, from compilation unit
4003 CU. The result will be allocated with malloc. */
4004
4005 static char *
4006 partial_die_full_name (struct partial_die_info *pdi,
4007 struct dwarf2_cu *cu)
4008 {
4009 char *parent_scope;
4010
4011 /* If this is a template instantiation, we can not work out the
4012 template arguments from partial DIEs. So, unfortunately, we have
4013 to go through the full DIEs. At least any work we do building
4014 types here will be reused if full symbols are loaded later. */
4015 if (pdi->has_template_arguments)
4016 {
4017 fixup_partial_die (pdi, cu);
4018
4019 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4020 {
4021 struct die_info *die;
4022 struct attribute attr;
4023 struct dwarf2_cu *ref_cu = cu;
4024
4025 /* DW_FORM_ref_addr is using section offset. */
4026 attr.name = 0;
4027 attr.form = DW_FORM_ref_addr;
4028 attr.u.unsnd = pdi->offset.sect_off;
4029 die = follow_die_ref (NULL, &attr, &ref_cu);
4030
4031 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4032 }
4033 }
4034
4035 parent_scope = partial_die_parent_scope (pdi, cu);
4036 if (parent_scope == NULL)
4037 return NULL;
4038 else
4039 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4040 }
4041
4042 static void
4043 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4044 {
4045 struct objfile *objfile = cu->objfile;
4046 CORE_ADDR addr = 0;
4047 char *actual_name = NULL;
4048 CORE_ADDR baseaddr;
4049 int built_actual_name = 0;
4050
4051 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4052
4053 actual_name = partial_die_full_name (pdi, cu);
4054 if (actual_name)
4055 built_actual_name = 1;
4056
4057 if (actual_name == NULL)
4058 actual_name = pdi->name;
4059
4060 switch (pdi->tag)
4061 {
4062 case DW_TAG_subprogram:
4063 if (pdi->is_external || cu->language == language_ada)
4064 {
4065 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4066 of the global scope. But in Ada, we want to be able to access
4067 nested procedures globally. So all Ada subprograms are stored
4068 in the global scope. */
4069 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4070 mst_text, objfile); */
4071 add_psymbol_to_list (actual_name, strlen (actual_name),
4072 built_actual_name,
4073 VAR_DOMAIN, LOC_BLOCK,
4074 &objfile->global_psymbols,
4075 0, pdi->lowpc + baseaddr,
4076 cu->language, objfile);
4077 }
4078 else
4079 {
4080 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4081 mst_file_text, objfile); */
4082 add_psymbol_to_list (actual_name, strlen (actual_name),
4083 built_actual_name,
4084 VAR_DOMAIN, LOC_BLOCK,
4085 &objfile->static_psymbols,
4086 0, pdi->lowpc + baseaddr,
4087 cu->language, objfile);
4088 }
4089 break;
4090 case DW_TAG_constant:
4091 {
4092 struct psymbol_allocation_list *list;
4093
4094 if (pdi->is_external)
4095 list = &objfile->global_psymbols;
4096 else
4097 list = &objfile->static_psymbols;
4098 add_psymbol_to_list (actual_name, strlen (actual_name),
4099 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4100 list, 0, 0, cu->language, objfile);
4101 }
4102 break;
4103 case DW_TAG_variable:
4104 if (pdi->locdesc)
4105 addr = decode_locdesc (pdi->locdesc, cu);
4106
4107 if (pdi->locdesc
4108 && addr == 0
4109 && !dwarf2_per_objfile->has_section_at_zero)
4110 {
4111 /* A global or static variable may also have been stripped
4112 out by the linker if unused, in which case its address
4113 will be nullified; do not add such variables into partial
4114 symbol table then. */
4115 }
4116 else if (pdi->is_external)
4117 {
4118 /* Global Variable.
4119 Don't enter into the minimal symbol tables as there is
4120 a minimal symbol table entry from the ELF symbols already.
4121 Enter into partial symbol table if it has a location
4122 descriptor or a type.
4123 If the location descriptor is missing, new_symbol will create
4124 a LOC_UNRESOLVED symbol, the address of the variable will then
4125 be determined from the minimal symbol table whenever the variable
4126 is referenced.
4127 The address for the partial symbol table entry is not
4128 used by GDB, but it comes in handy for debugging partial symbol
4129 table building. */
4130
4131 if (pdi->locdesc || pdi->has_type)
4132 add_psymbol_to_list (actual_name, strlen (actual_name),
4133 built_actual_name,
4134 VAR_DOMAIN, LOC_STATIC,
4135 &objfile->global_psymbols,
4136 0, addr + baseaddr,
4137 cu->language, objfile);
4138 }
4139 else
4140 {
4141 /* Static Variable. Skip symbols without location descriptors. */
4142 if (pdi->locdesc == NULL)
4143 {
4144 if (built_actual_name)
4145 xfree (actual_name);
4146 return;
4147 }
4148 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4149 mst_file_data, objfile); */
4150 add_psymbol_to_list (actual_name, strlen (actual_name),
4151 built_actual_name,
4152 VAR_DOMAIN, LOC_STATIC,
4153 &objfile->static_psymbols,
4154 0, addr + baseaddr,
4155 cu->language, objfile);
4156 }
4157 break;
4158 case DW_TAG_typedef:
4159 case DW_TAG_base_type:
4160 case DW_TAG_subrange_type:
4161 add_psymbol_to_list (actual_name, strlen (actual_name),
4162 built_actual_name,
4163 VAR_DOMAIN, LOC_TYPEDEF,
4164 &objfile->static_psymbols,
4165 0, (CORE_ADDR) 0, cu->language, objfile);
4166 break;
4167 case DW_TAG_namespace:
4168 add_psymbol_to_list (actual_name, strlen (actual_name),
4169 built_actual_name,
4170 VAR_DOMAIN, LOC_TYPEDEF,
4171 &objfile->global_psymbols,
4172 0, (CORE_ADDR) 0, cu->language, objfile);
4173 break;
4174 case DW_TAG_class_type:
4175 case DW_TAG_interface_type:
4176 case DW_TAG_structure_type:
4177 case DW_TAG_union_type:
4178 case DW_TAG_enumeration_type:
4179 /* Skip external references. The DWARF standard says in the section
4180 about "Structure, Union, and Class Type Entries": "An incomplete
4181 structure, union or class type is represented by a structure,
4182 union or class entry that does not have a byte size attribute
4183 and that has a DW_AT_declaration attribute." */
4184 if (!pdi->has_byte_size && pdi->is_declaration)
4185 {
4186 if (built_actual_name)
4187 xfree (actual_name);
4188 return;
4189 }
4190
4191 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4192 static vs. global. */
4193 add_psymbol_to_list (actual_name, strlen (actual_name),
4194 built_actual_name,
4195 STRUCT_DOMAIN, LOC_TYPEDEF,
4196 (cu->language == language_cplus
4197 || cu->language == language_java)
4198 ? &objfile->global_psymbols
4199 : &objfile->static_psymbols,
4200 0, (CORE_ADDR) 0, cu->language, objfile);
4201
4202 break;
4203 case DW_TAG_enumerator:
4204 add_psymbol_to_list (actual_name, strlen (actual_name),
4205 built_actual_name,
4206 VAR_DOMAIN, LOC_CONST,
4207 (cu->language == language_cplus
4208 || cu->language == language_java)
4209 ? &objfile->global_psymbols
4210 : &objfile->static_psymbols,
4211 0, (CORE_ADDR) 0, cu->language, objfile);
4212 break;
4213 default:
4214 break;
4215 }
4216
4217 if (built_actual_name)
4218 xfree (actual_name);
4219 }
4220
4221 /* Read a partial die corresponding to a namespace; also, add a symbol
4222 corresponding to that namespace to the symbol table. NAMESPACE is
4223 the name of the enclosing namespace. */
4224
4225 static void
4226 add_partial_namespace (struct partial_die_info *pdi,
4227 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4228 int need_pc, struct dwarf2_cu *cu)
4229 {
4230 /* Add a symbol for the namespace. */
4231
4232 add_partial_symbol (pdi, cu);
4233
4234 /* Now scan partial symbols in that namespace. */
4235
4236 if (pdi->has_children)
4237 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4238 }
4239
4240 /* Read a partial die corresponding to a Fortran module. */
4241
4242 static void
4243 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4244 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4245 {
4246 /* Now scan partial symbols in that module. */
4247
4248 if (pdi->has_children)
4249 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4250 }
4251
4252 /* Read a partial die corresponding to a subprogram and create a partial
4253 symbol for that subprogram. When the CU language allows it, this
4254 routine also defines a partial symbol for each nested subprogram
4255 that this subprogram contains.
4256
4257 DIE my also be a lexical block, in which case we simply search
4258 recursively for suprograms defined inside that lexical block.
4259 Again, this is only performed when the CU language allows this
4260 type of definitions. */
4261
4262 static void
4263 add_partial_subprogram (struct partial_die_info *pdi,
4264 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4265 int need_pc, struct dwarf2_cu *cu)
4266 {
4267 if (pdi->tag == DW_TAG_subprogram)
4268 {
4269 if (pdi->has_pc_info)
4270 {
4271 if (pdi->lowpc < *lowpc)
4272 *lowpc = pdi->lowpc;
4273 if (pdi->highpc > *highpc)
4274 *highpc = pdi->highpc;
4275 if (need_pc)
4276 {
4277 CORE_ADDR baseaddr;
4278 struct objfile *objfile = cu->objfile;
4279
4280 baseaddr = ANOFFSET (objfile->section_offsets,
4281 SECT_OFF_TEXT (objfile));
4282 addrmap_set_empty (objfile->psymtabs_addrmap,
4283 pdi->lowpc + baseaddr,
4284 pdi->highpc - 1 + baseaddr,
4285 cu->per_cu->v.psymtab);
4286 }
4287 }
4288
4289 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4290 {
4291 if (!pdi->is_declaration)
4292 /* Ignore subprogram DIEs that do not have a name, they are
4293 illegal. Do not emit a complaint at this point, we will
4294 do so when we convert this psymtab into a symtab. */
4295 if (pdi->name)
4296 add_partial_symbol (pdi, cu);
4297 }
4298 }
4299
4300 if (! pdi->has_children)
4301 return;
4302
4303 if (cu->language == language_ada)
4304 {
4305 pdi = pdi->die_child;
4306 while (pdi != NULL)
4307 {
4308 fixup_partial_die (pdi, cu);
4309 if (pdi->tag == DW_TAG_subprogram
4310 || pdi->tag == DW_TAG_lexical_block)
4311 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4312 pdi = pdi->die_sibling;
4313 }
4314 }
4315 }
4316
4317 /* Read a partial die corresponding to an enumeration type. */
4318
4319 static void
4320 add_partial_enumeration (struct partial_die_info *enum_pdi,
4321 struct dwarf2_cu *cu)
4322 {
4323 struct partial_die_info *pdi;
4324
4325 if (enum_pdi->name != NULL)
4326 add_partial_symbol (enum_pdi, cu);
4327
4328 pdi = enum_pdi->die_child;
4329 while (pdi)
4330 {
4331 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4332 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4333 else
4334 add_partial_symbol (pdi, cu);
4335 pdi = pdi->die_sibling;
4336 }
4337 }
4338
4339 /* Return the initial uleb128 in the die at INFO_PTR. */
4340
4341 static unsigned int
4342 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4343 {
4344 unsigned int bytes_read;
4345
4346 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4347 }
4348
4349 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4350 Return the corresponding abbrev, or NULL if the number is zero (indicating
4351 an empty DIE). In either case *BYTES_READ will be set to the length of
4352 the initial number. */
4353
4354 static struct abbrev_info *
4355 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4356 struct dwarf2_cu *cu)
4357 {
4358 bfd *abfd = cu->objfile->obfd;
4359 unsigned int abbrev_number;
4360 struct abbrev_info *abbrev;
4361
4362 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4363
4364 if (abbrev_number == 0)
4365 return NULL;
4366
4367 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4368 if (!abbrev)
4369 {
4370 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4371 abbrev_number, bfd_get_filename (abfd));
4372 }
4373
4374 return abbrev;
4375 }
4376
4377 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4378 Returns a pointer to the end of a series of DIEs, terminated by an empty
4379 DIE. Any children of the skipped DIEs will also be skipped. */
4380
4381 static gdb_byte *
4382 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4383 {
4384 struct abbrev_info *abbrev;
4385 unsigned int bytes_read;
4386
4387 while (1)
4388 {
4389 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4390 if (abbrev == NULL)
4391 return info_ptr + bytes_read;
4392 else
4393 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4394 }
4395 }
4396
4397 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4398 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4399 abbrev corresponding to that skipped uleb128 should be passed in
4400 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4401 children. */
4402
4403 static gdb_byte *
4404 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4405 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4406 {
4407 unsigned int bytes_read;
4408 struct attribute attr;
4409 bfd *abfd = cu->objfile->obfd;
4410 unsigned int form, i;
4411
4412 for (i = 0; i < abbrev->num_attrs; i++)
4413 {
4414 /* The only abbrev we care about is DW_AT_sibling. */
4415 if (abbrev->attrs[i].name == DW_AT_sibling)
4416 {
4417 read_attribute (&attr, &abbrev->attrs[i],
4418 abfd, info_ptr, cu);
4419 if (attr.form == DW_FORM_ref_addr)
4420 complaint (&symfile_complaints,
4421 _("ignoring absolute DW_AT_sibling"));
4422 else
4423 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4424 }
4425
4426 /* If it isn't DW_AT_sibling, skip this attribute. */
4427 form = abbrev->attrs[i].form;
4428 skip_attribute:
4429 switch (form)
4430 {
4431 case DW_FORM_ref_addr:
4432 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4433 and later it is offset sized. */
4434 if (cu->header.version == 2)
4435 info_ptr += cu->header.addr_size;
4436 else
4437 info_ptr += cu->header.offset_size;
4438 break;
4439 case DW_FORM_addr:
4440 info_ptr += cu->header.addr_size;
4441 break;
4442 case DW_FORM_data1:
4443 case DW_FORM_ref1:
4444 case DW_FORM_flag:
4445 info_ptr += 1;
4446 break;
4447 case DW_FORM_flag_present:
4448 break;
4449 case DW_FORM_data2:
4450 case DW_FORM_ref2:
4451 info_ptr += 2;
4452 break;
4453 case DW_FORM_data4:
4454 case DW_FORM_ref4:
4455 info_ptr += 4;
4456 break;
4457 case DW_FORM_data8:
4458 case DW_FORM_ref8:
4459 case DW_FORM_ref_sig8:
4460 info_ptr += 8;
4461 break;
4462 case DW_FORM_string:
4463 read_direct_string (abfd, info_ptr, &bytes_read);
4464 info_ptr += bytes_read;
4465 break;
4466 case DW_FORM_sec_offset:
4467 case DW_FORM_strp:
4468 info_ptr += cu->header.offset_size;
4469 break;
4470 case DW_FORM_exprloc:
4471 case DW_FORM_block:
4472 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4473 info_ptr += bytes_read;
4474 break;
4475 case DW_FORM_block1:
4476 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4477 break;
4478 case DW_FORM_block2:
4479 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4480 break;
4481 case DW_FORM_block4:
4482 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4483 break;
4484 case DW_FORM_sdata:
4485 case DW_FORM_udata:
4486 case DW_FORM_ref_udata:
4487 info_ptr = skip_leb128 (abfd, info_ptr);
4488 break;
4489 case DW_FORM_indirect:
4490 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4491 info_ptr += bytes_read;
4492 /* We need to continue parsing from here, so just go back to
4493 the top. */
4494 goto skip_attribute;
4495
4496 default:
4497 error (_("Dwarf Error: Cannot handle %s "
4498 "in DWARF reader [in module %s]"),
4499 dwarf_form_name (form),
4500 bfd_get_filename (abfd));
4501 }
4502 }
4503
4504 if (abbrev->has_children)
4505 return skip_children (buffer, info_ptr, cu);
4506 else
4507 return info_ptr;
4508 }
4509
4510 /* Locate ORIG_PDI's sibling.
4511 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4512 in BUFFER. */
4513
4514 static gdb_byte *
4515 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4516 gdb_byte *buffer, gdb_byte *info_ptr,
4517 bfd *abfd, struct dwarf2_cu *cu)
4518 {
4519 /* Do we know the sibling already? */
4520
4521 if (orig_pdi->sibling)
4522 return orig_pdi->sibling;
4523
4524 /* Are there any children to deal with? */
4525
4526 if (!orig_pdi->has_children)
4527 return info_ptr;
4528
4529 /* Skip the children the long way. */
4530
4531 return skip_children (buffer, info_ptr, cu);
4532 }
4533
4534 /* Expand this partial symbol table into a full symbol table. */
4535
4536 static void
4537 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4538 {
4539 if (pst != NULL)
4540 {
4541 if (pst->readin)
4542 {
4543 warning (_("bug: psymtab for %s is already read in."),
4544 pst->filename);
4545 }
4546 else
4547 {
4548 if (info_verbose)
4549 {
4550 printf_filtered (_("Reading in symbols for %s..."),
4551 pst->filename);
4552 gdb_flush (gdb_stdout);
4553 }
4554
4555 /* Restore our global data. */
4556 dwarf2_per_objfile = objfile_data (pst->objfile,
4557 dwarf2_objfile_data_key);
4558
4559 /* If this psymtab is constructed from a debug-only objfile, the
4560 has_section_at_zero flag will not necessarily be correct. We
4561 can get the correct value for this flag by looking at the data
4562 associated with the (presumably stripped) associated objfile. */
4563 if (pst->objfile->separate_debug_objfile_backlink)
4564 {
4565 struct dwarf2_per_objfile *dpo_backlink
4566 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4567 dwarf2_objfile_data_key);
4568
4569 dwarf2_per_objfile->has_section_at_zero
4570 = dpo_backlink->has_section_at_zero;
4571 }
4572
4573 dwarf2_per_objfile->reading_partial_symbols = 0;
4574
4575 psymtab_to_symtab_1 (pst);
4576
4577 /* Finish up the debug error message. */
4578 if (info_verbose)
4579 printf_filtered (_("done.\n"));
4580 }
4581 }
4582 }
4583 \f
4584 /* Reading in full CUs. */
4585
4586 /* Add PER_CU to the queue. */
4587
4588 static void
4589 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4590 {
4591 struct dwarf2_queue_item *item;
4592
4593 per_cu->queued = 1;
4594 item = xmalloc (sizeof (*item));
4595 item->per_cu = per_cu;
4596 item->next = NULL;
4597
4598 if (dwarf2_queue == NULL)
4599 dwarf2_queue = item;
4600 else
4601 dwarf2_queue_tail->next = item;
4602
4603 dwarf2_queue_tail = item;
4604 }
4605
4606 /* Process the queue. */
4607
4608 static void
4609 process_queue (void)
4610 {
4611 struct dwarf2_queue_item *item, *next_item;
4612
4613 /* The queue starts out with one item, but following a DIE reference
4614 may load a new CU, adding it to the end of the queue. */
4615 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4616 {
4617 if (dwarf2_per_objfile->using_index
4618 ? !item->per_cu->v.quick->symtab
4619 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4620 process_full_comp_unit (item->per_cu);
4621
4622 item->per_cu->queued = 0;
4623 next_item = item->next;
4624 xfree (item);
4625 }
4626
4627 dwarf2_queue_tail = NULL;
4628 }
4629
4630 /* Free all allocated queue entries. This function only releases anything if
4631 an error was thrown; if the queue was processed then it would have been
4632 freed as we went along. */
4633
4634 static void
4635 dwarf2_release_queue (void *dummy)
4636 {
4637 struct dwarf2_queue_item *item, *last;
4638
4639 item = dwarf2_queue;
4640 while (item)
4641 {
4642 /* Anything still marked queued is likely to be in an
4643 inconsistent state, so discard it. */
4644 if (item->per_cu->queued)
4645 {
4646 if (item->per_cu->cu != NULL)
4647 free_one_cached_comp_unit (item->per_cu->cu);
4648 item->per_cu->queued = 0;
4649 }
4650
4651 last = item;
4652 item = item->next;
4653 xfree (last);
4654 }
4655
4656 dwarf2_queue = dwarf2_queue_tail = NULL;
4657 }
4658
4659 /* Read in full symbols for PST, and anything it depends on. */
4660
4661 static void
4662 psymtab_to_symtab_1 (struct partial_symtab *pst)
4663 {
4664 struct dwarf2_per_cu_data *per_cu;
4665 struct cleanup *back_to;
4666 int i;
4667
4668 for (i = 0; i < pst->number_of_dependencies; i++)
4669 if (!pst->dependencies[i]->readin)
4670 {
4671 /* Inform about additional files that need to be read in. */
4672 if (info_verbose)
4673 {
4674 /* FIXME: i18n: Need to make this a single string. */
4675 fputs_filtered (" ", gdb_stdout);
4676 wrap_here ("");
4677 fputs_filtered ("and ", gdb_stdout);
4678 wrap_here ("");
4679 printf_filtered ("%s...", pst->dependencies[i]->filename);
4680 wrap_here (""); /* Flush output. */
4681 gdb_flush (gdb_stdout);
4682 }
4683 psymtab_to_symtab_1 (pst->dependencies[i]);
4684 }
4685
4686 per_cu = pst->read_symtab_private;
4687
4688 if (per_cu == NULL)
4689 {
4690 /* It's an include file, no symbols to read for it.
4691 Everything is in the parent symtab. */
4692 pst->readin = 1;
4693 return;
4694 }
4695
4696 dw2_do_instantiate_symtab (per_cu);
4697 }
4698
4699 /* Load the DIEs associated with PER_CU into memory. */
4700
4701 static void
4702 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4703 {
4704 struct objfile *objfile = per_cu->objfile;
4705 bfd *abfd = objfile->obfd;
4706 struct dwarf2_cu *cu;
4707 sect_offset offset;
4708 gdb_byte *info_ptr, *beg_of_comp_unit;
4709 struct cleanup *free_cu_cleanup = NULL;
4710 struct attribute *attr;
4711 int read_cu = 0;
4712
4713 gdb_assert (! per_cu->debug_types_section);
4714
4715 /* Set local variables from the partial symbol table info. */
4716 offset = per_cu->offset;
4717
4718 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4719 info_ptr = dwarf2_per_objfile->info.buffer + offset.sect_off;
4720 beg_of_comp_unit = info_ptr;
4721
4722 if (per_cu->cu == NULL)
4723 {
4724 cu = xmalloc (sizeof (*cu));
4725 init_one_comp_unit (cu, per_cu);
4726
4727 read_cu = 1;
4728
4729 /* If an error occurs while loading, release our storage. */
4730 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4731
4732 /* Read in the comp_unit header. */
4733 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4734
4735 /* Skip dummy compilation units. */
4736 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4737 + dwarf2_per_objfile->info.size)
4738 || peek_abbrev_code (abfd, info_ptr) == 0)
4739 {
4740 do_cleanups (free_cu_cleanup);
4741 return;
4742 }
4743
4744 /* Complete the cu_header. */
4745 cu->header.offset = offset;
4746 cu->header.first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4747 }
4748 else
4749 {
4750 cu = per_cu->cu;
4751 info_ptr += cu->header.first_die_offset.cu_off;
4752 }
4753
4754 cu->dies = read_comp_unit (info_ptr, cu);
4755
4756 /* We try not to read any attributes in this function, because not
4757 all CUs needed for references have been loaded yet, and symbol
4758 table processing isn't initialized. But we have to set the CU language,
4759 or we won't be able to build types correctly. */
4760 prepare_one_comp_unit (cu, cu->dies);
4761
4762 /* Similarly, if we do not read the producer, we can not apply
4763 producer-specific interpretation. */
4764 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4765 if (attr)
4766 cu->producer = DW_STRING (attr);
4767
4768 if (read_cu)
4769 {
4770 /* We've successfully allocated this compilation unit. Let our
4771 caller clean it up when finished with it. */
4772 discard_cleanups (free_cu_cleanup);
4773
4774 /* Link this CU into read_in_chain. */
4775 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4776 dwarf2_per_objfile->read_in_chain = per_cu;
4777 }
4778 }
4779
4780 /* Add a DIE to the delayed physname list. */
4781
4782 static void
4783 add_to_method_list (struct type *type, int fnfield_index, int index,
4784 const char *name, struct die_info *die,
4785 struct dwarf2_cu *cu)
4786 {
4787 struct delayed_method_info mi;
4788 mi.type = type;
4789 mi.fnfield_index = fnfield_index;
4790 mi.index = index;
4791 mi.name = name;
4792 mi.die = die;
4793 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4794 }
4795
4796 /* A cleanup for freeing the delayed method list. */
4797
4798 static void
4799 free_delayed_list (void *ptr)
4800 {
4801 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4802 if (cu->method_list != NULL)
4803 {
4804 VEC_free (delayed_method_info, cu->method_list);
4805 cu->method_list = NULL;
4806 }
4807 }
4808
4809 /* Compute the physnames of any methods on the CU's method list.
4810
4811 The computation of method physnames is delayed in order to avoid the
4812 (bad) condition that one of the method's formal parameters is of an as yet
4813 incomplete type. */
4814
4815 static void
4816 compute_delayed_physnames (struct dwarf2_cu *cu)
4817 {
4818 int i;
4819 struct delayed_method_info *mi;
4820 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4821 {
4822 const char *physname;
4823 struct fn_fieldlist *fn_flp
4824 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4825 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4826 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4827 }
4828 }
4829
4830 /* Go objects should be embedded in a DW_TAG_module DIE,
4831 and it's not clear if/how imported objects will appear.
4832 To keep Go support simple until that's worked out,
4833 go back through what we've read and create something usable.
4834 We could do this while processing each DIE, and feels kinda cleaner,
4835 but that way is more invasive.
4836 This is to, for example, allow the user to type "p var" or "b main"
4837 without having to specify the package name, and allow lookups
4838 of module.object to work in contexts that use the expression
4839 parser. */
4840
4841 static void
4842 fixup_go_packaging (struct dwarf2_cu *cu)
4843 {
4844 char *package_name = NULL;
4845 struct pending *list;
4846 int i;
4847
4848 for (list = global_symbols; list != NULL; list = list->next)
4849 {
4850 for (i = 0; i < list->nsyms; ++i)
4851 {
4852 struct symbol *sym = list->symbol[i];
4853
4854 if (SYMBOL_LANGUAGE (sym) == language_go
4855 && SYMBOL_CLASS (sym) == LOC_BLOCK)
4856 {
4857 char *this_package_name = go_symbol_package_name (sym);
4858
4859 if (this_package_name == NULL)
4860 continue;
4861 if (package_name == NULL)
4862 package_name = this_package_name;
4863 else
4864 {
4865 if (strcmp (package_name, this_package_name) != 0)
4866 complaint (&symfile_complaints,
4867 _("Symtab %s has objects from two different Go packages: %s and %s"),
4868 (sym->symtab && sym->symtab->filename
4869 ? sym->symtab->filename
4870 : cu->objfile->name),
4871 this_package_name, package_name);
4872 xfree (this_package_name);
4873 }
4874 }
4875 }
4876 }
4877
4878 if (package_name != NULL)
4879 {
4880 struct objfile *objfile = cu->objfile;
4881 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
4882 package_name, objfile);
4883 struct symbol *sym;
4884
4885 TYPE_TAG_NAME (type) = TYPE_NAME (type);
4886
4887 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
4888 SYMBOL_SET_LANGUAGE (sym, language_go);
4889 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
4890 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
4891 e.g., "main" finds the "main" module and not C's main(). */
4892 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
4893 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4894 SYMBOL_TYPE (sym) = type;
4895
4896 add_symbol_to_list (sym, &global_symbols);
4897
4898 xfree (package_name);
4899 }
4900 }
4901
4902 /* Generate full symbol information for PER_CU, whose DIEs have
4903 already been loaded into memory. */
4904
4905 static void
4906 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4907 {
4908 struct dwarf2_cu *cu = per_cu->cu;
4909 struct objfile *objfile = per_cu->objfile;
4910 CORE_ADDR lowpc, highpc;
4911 struct symtab *symtab;
4912 struct cleanup *back_to, *delayed_list_cleanup;
4913 CORE_ADDR baseaddr;
4914
4915 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4916
4917 buildsym_init ();
4918 back_to = make_cleanup (really_free_pendings, NULL);
4919 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4920
4921 cu->list_in_scope = &file_symbols;
4922
4923 /* Do line number decoding in read_file_scope () */
4924 process_die (cu->dies, cu);
4925
4926 /* For now fudge the Go package. */
4927 if (cu->language == language_go)
4928 fixup_go_packaging (cu);
4929
4930 /* Now that we have processed all the DIEs in the CU, all the types
4931 should be complete, and it should now be safe to compute all of the
4932 physnames. */
4933 compute_delayed_physnames (cu);
4934 do_cleanups (delayed_list_cleanup);
4935
4936 /* Some compilers don't define a DW_AT_high_pc attribute for the
4937 compilation unit. If the DW_AT_high_pc is missing, synthesize
4938 it, by scanning the DIE's below the compilation unit. */
4939 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4940
4941 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4942
4943 if (symtab != NULL)
4944 {
4945 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4946
4947 /* Set symtab language to language from DW_AT_language. If the
4948 compilation is from a C file generated by language preprocessors, do
4949 not set the language if it was already deduced by start_subfile. */
4950 if (!(cu->language == language_c && symtab->language != language_c))
4951 symtab->language = cu->language;
4952
4953 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4954 produce DW_AT_location with location lists but it can be possibly
4955 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
4956 there were bugs in prologue debug info, fixed later in GCC-4.5
4957 by "unwind info for epilogues" patch (which is not directly related).
4958
4959 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4960 needed, it would be wrong due to missing DW_AT_producer there.
4961
4962 Still one can confuse GDB by using non-standard GCC compilation
4963 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4964 */
4965 if (cu->has_loclist && gcc_4_minor >= 5)
4966 symtab->locations_valid = 1;
4967
4968 if (gcc_4_minor >= 5)
4969 symtab->epilogue_unwind_valid = 1;
4970
4971 symtab->call_site_htab = cu->call_site_htab;
4972 }
4973
4974 if (dwarf2_per_objfile->using_index)
4975 per_cu->v.quick->symtab = symtab;
4976 else
4977 {
4978 struct partial_symtab *pst = per_cu->v.psymtab;
4979 pst->symtab = symtab;
4980 pst->readin = 1;
4981 }
4982
4983 do_cleanups (back_to);
4984 }
4985
4986 /* Process a die and its children. */
4987
4988 static void
4989 process_die (struct die_info *die, struct dwarf2_cu *cu)
4990 {
4991 switch (die->tag)
4992 {
4993 case DW_TAG_padding:
4994 break;
4995 case DW_TAG_compile_unit:
4996 read_file_scope (die, cu);
4997 break;
4998 case DW_TAG_type_unit:
4999 read_type_unit_scope (die, cu);
5000 break;
5001 case DW_TAG_subprogram:
5002 case DW_TAG_inlined_subroutine:
5003 read_func_scope (die, cu);
5004 break;
5005 case DW_TAG_lexical_block:
5006 case DW_TAG_try_block:
5007 case DW_TAG_catch_block:
5008 read_lexical_block_scope (die, cu);
5009 break;
5010 case DW_TAG_GNU_call_site:
5011 read_call_site_scope (die, cu);
5012 break;
5013 case DW_TAG_class_type:
5014 case DW_TAG_interface_type:
5015 case DW_TAG_structure_type:
5016 case DW_TAG_union_type:
5017 process_structure_scope (die, cu);
5018 break;
5019 case DW_TAG_enumeration_type:
5020 process_enumeration_scope (die, cu);
5021 break;
5022
5023 /* These dies have a type, but processing them does not create
5024 a symbol or recurse to process the children. Therefore we can
5025 read them on-demand through read_type_die. */
5026 case DW_TAG_subroutine_type:
5027 case DW_TAG_set_type:
5028 case DW_TAG_array_type:
5029 case DW_TAG_pointer_type:
5030 case DW_TAG_ptr_to_member_type:
5031 case DW_TAG_reference_type:
5032 case DW_TAG_string_type:
5033 break;
5034
5035 case DW_TAG_base_type:
5036 case DW_TAG_subrange_type:
5037 case DW_TAG_typedef:
5038 /* Add a typedef symbol for the type definition, if it has a
5039 DW_AT_name. */
5040 new_symbol (die, read_type_die (die, cu), cu);
5041 break;
5042 case DW_TAG_common_block:
5043 read_common_block (die, cu);
5044 break;
5045 case DW_TAG_common_inclusion:
5046 break;
5047 case DW_TAG_namespace:
5048 processing_has_namespace_info = 1;
5049 read_namespace (die, cu);
5050 break;
5051 case DW_TAG_module:
5052 processing_has_namespace_info = 1;
5053 read_module (die, cu);
5054 break;
5055 case DW_TAG_imported_declaration:
5056 case DW_TAG_imported_module:
5057 processing_has_namespace_info = 1;
5058 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5059 || cu->language != language_fortran))
5060 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5061 dwarf_tag_name (die->tag));
5062 read_import_statement (die, cu);
5063 break;
5064 default:
5065 new_symbol (die, NULL, cu);
5066 break;
5067 }
5068 }
5069
5070 /* A helper function for dwarf2_compute_name which determines whether DIE
5071 needs to have the name of the scope prepended to the name listed in the
5072 die. */
5073
5074 static int
5075 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5076 {
5077 struct attribute *attr;
5078
5079 switch (die->tag)
5080 {
5081 case DW_TAG_namespace:
5082 case DW_TAG_typedef:
5083 case DW_TAG_class_type:
5084 case DW_TAG_interface_type:
5085 case DW_TAG_structure_type:
5086 case DW_TAG_union_type:
5087 case DW_TAG_enumeration_type:
5088 case DW_TAG_enumerator:
5089 case DW_TAG_subprogram:
5090 case DW_TAG_member:
5091 return 1;
5092
5093 case DW_TAG_variable:
5094 case DW_TAG_constant:
5095 /* We only need to prefix "globally" visible variables. These include
5096 any variable marked with DW_AT_external or any variable that
5097 lives in a namespace. [Variables in anonymous namespaces
5098 require prefixing, but they are not DW_AT_external.] */
5099
5100 if (dwarf2_attr (die, DW_AT_specification, cu))
5101 {
5102 struct dwarf2_cu *spec_cu = cu;
5103
5104 return die_needs_namespace (die_specification (die, &spec_cu),
5105 spec_cu);
5106 }
5107
5108 attr = dwarf2_attr (die, DW_AT_external, cu);
5109 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5110 && die->parent->tag != DW_TAG_module)
5111 return 0;
5112 /* A variable in a lexical block of some kind does not need a
5113 namespace, even though in C++ such variables may be external
5114 and have a mangled name. */
5115 if (die->parent->tag == DW_TAG_lexical_block
5116 || die->parent->tag == DW_TAG_try_block
5117 || die->parent->tag == DW_TAG_catch_block
5118 || die->parent->tag == DW_TAG_subprogram)
5119 return 0;
5120 return 1;
5121
5122 default:
5123 return 0;
5124 }
5125 }
5126
5127 /* Retrieve the last character from a mem_file. */
5128
5129 static void
5130 do_ui_file_peek_last (void *object, const char *buffer, long length)
5131 {
5132 char *last_char_p = (char *) object;
5133
5134 if (length > 0)
5135 *last_char_p = buffer[length - 1];
5136 }
5137
5138 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5139 compute the physname for the object, which include a method's:
5140 - formal parameters (C++/Java),
5141 - receiver type (Go),
5142 - return type (Java).
5143
5144 The term "physname" is a bit confusing.
5145 For C++, for example, it is the demangled name.
5146 For Go, for example, it's the mangled name.
5147
5148 For Ada, return the DIE's linkage name rather than the fully qualified
5149 name. PHYSNAME is ignored..
5150
5151 The result is allocated on the objfile_obstack and canonicalized. */
5152
5153 static const char *
5154 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5155 int physname)
5156 {
5157 struct objfile *objfile = cu->objfile;
5158
5159 if (name == NULL)
5160 name = dwarf2_name (die, cu);
5161
5162 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5163 compute it by typename_concat inside GDB. */
5164 if (cu->language == language_ada
5165 || (cu->language == language_fortran && physname))
5166 {
5167 /* For Ada unit, we prefer the linkage name over the name, as
5168 the former contains the exported name, which the user expects
5169 to be able to reference. Ideally, we want the user to be able
5170 to reference this entity using either natural or linkage name,
5171 but we haven't started looking at this enhancement yet. */
5172 struct attribute *attr;
5173
5174 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5175 if (attr == NULL)
5176 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5177 if (attr && DW_STRING (attr))
5178 return DW_STRING (attr);
5179 }
5180
5181 /* These are the only languages we know how to qualify names in. */
5182 if (name != NULL
5183 && (cu->language == language_cplus || cu->language == language_java
5184 || cu->language == language_fortran))
5185 {
5186 if (die_needs_namespace (die, cu))
5187 {
5188 long length;
5189 const char *prefix;
5190 struct ui_file *buf;
5191
5192 prefix = determine_prefix (die, cu);
5193 buf = mem_fileopen ();
5194 if (*prefix != '\0')
5195 {
5196 char *prefixed_name = typename_concat (NULL, prefix, name,
5197 physname, cu);
5198
5199 fputs_unfiltered (prefixed_name, buf);
5200 xfree (prefixed_name);
5201 }
5202 else
5203 fputs_unfiltered (name, buf);
5204
5205 /* Template parameters may be specified in the DIE's DW_AT_name, or
5206 as children with DW_TAG_template_type_param or
5207 DW_TAG_value_type_param. If the latter, add them to the name
5208 here. If the name already has template parameters, then
5209 skip this step; some versions of GCC emit both, and
5210 it is more efficient to use the pre-computed name.
5211
5212 Something to keep in mind about this process: it is very
5213 unlikely, or in some cases downright impossible, to produce
5214 something that will match the mangled name of a function.
5215 If the definition of the function has the same debug info,
5216 we should be able to match up with it anyway. But fallbacks
5217 using the minimal symbol, for instance to find a method
5218 implemented in a stripped copy of libstdc++, will not work.
5219 If we do not have debug info for the definition, we will have to
5220 match them up some other way.
5221
5222 When we do name matching there is a related problem with function
5223 templates; two instantiated function templates are allowed to
5224 differ only by their return types, which we do not add here. */
5225
5226 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5227 {
5228 struct attribute *attr;
5229 struct die_info *child;
5230 int first = 1;
5231
5232 die->building_fullname = 1;
5233
5234 for (child = die->child; child != NULL; child = child->sibling)
5235 {
5236 struct type *type;
5237 LONGEST value;
5238 gdb_byte *bytes;
5239 struct dwarf2_locexpr_baton *baton;
5240 struct value *v;
5241
5242 if (child->tag != DW_TAG_template_type_param
5243 && child->tag != DW_TAG_template_value_param)
5244 continue;
5245
5246 if (first)
5247 {
5248 fputs_unfiltered ("<", buf);
5249 first = 0;
5250 }
5251 else
5252 fputs_unfiltered (", ", buf);
5253
5254 attr = dwarf2_attr (child, DW_AT_type, cu);
5255 if (attr == NULL)
5256 {
5257 complaint (&symfile_complaints,
5258 _("template parameter missing DW_AT_type"));
5259 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5260 continue;
5261 }
5262 type = die_type (child, cu);
5263
5264 if (child->tag == DW_TAG_template_type_param)
5265 {
5266 c_print_type (type, "", buf, -1, 0);
5267 continue;
5268 }
5269
5270 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5271 if (attr == NULL)
5272 {
5273 complaint (&symfile_complaints,
5274 _("template parameter missing "
5275 "DW_AT_const_value"));
5276 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5277 continue;
5278 }
5279
5280 dwarf2_const_value_attr (attr, type, name,
5281 &cu->comp_unit_obstack, cu,
5282 &value, &bytes, &baton);
5283
5284 if (TYPE_NOSIGN (type))
5285 /* GDB prints characters as NUMBER 'CHAR'. If that's
5286 changed, this can use value_print instead. */
5287 c_printchar (value, type, buf);
5288 else
5289 {
5290 struct value_print_options opts;
5291
5292 if (baton != NULL)
5293 v = dwarf2_evaluate_loc_desc (type, NULL,
5294 baton->data,
5295 baton->size,
5296 baton->per_cu);
5297 else if (bytes != NULL)
5298 {
5299 v = allocate_value (type);
5300 memcpy (value_contents_writeable (v), bytes,
5301 TYPE_LENGTH (type));
5302 }
5303 else
5304 v = value_from_longest (type, value);
5305
5306 /* Specify decimal so that we do not depend on
5307 the radix. */
5308 get_formatted_print_options (&opts, 'd');
5309 opts.raw = 1;
5310 value_print (v, buf, &opts);
5311 release_value (v);
5312 value_free (v);
5313 }
5314 }
5315
5316 die->building_fullname = 0;
5317
5318 if (!first)
5319 {
5320 /* Close the argument list, with a space if necessary
5321 (nested templates). */
5322 char last_char = '\0';
5323 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5324 if (last_char == '>')
5325 fputs_unfiltered (" >", buf);
5326 else
5327 fputs_unfiltered (">", buf);
5328 }
5329 }
5330
5331 /* For Java and C++ methods, append formal parameter type
5332 information, if PHYSNAME. */
5333
5334 if (physname && die->tag == DW_TAG_subprogram
5335 && (cu->language == language_cplus
5336 || cu->language == language_java))
5337 {
5338 struct type *type = read_type_die (die, cu);
5339
5340 c_type_print_args (type, buf, 1, cu->language);
5341
5342 if (cu->language == language_java)
5343 {
5344 /* For java, we must append the return type to method
5345 names. */
5346 if (die->tag == DW_TAG_subprogram)
5347 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5348 0, 0);
5349 }
5350 else if (cu->language == language_cplus)
5351 {
5352 /* Assume that an artificial first parameter is
5353 "this", but do not crash if it is not. RealView
5354 marks unnamed (and thus unused) parameters as
5355 artificial; there is no way to differentiate
5356 the two cases. */
5357 if (TYPE_NFIELDS (type) > 0
5358 && TYPE_FIELD_ARTIFICIAL (type, 0)
5359 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5360 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5361 0))))
5362 fputs_unfiltered (" const", buf);
5363 }
5364 }
5365
5366 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5367 &length);
5368 ui_file_delete (buf);
5369
5370 if (cu->language == language_cplus)
5371 {
5372 char *cname
5373 = dwarf2_canonicalize_name (name, cu,
5374 &objfile->objfile_obstack);
5375
5376 if (cname != NULL)
5377 name = cname;
5378 }
5379 }
5380 }
5381
5382 return name;
5383 }
5384
5385 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5386 If scope qualifiers are appropriate they will be added. The result
5387 will be allocated on the objfile_obstack, or NULL if the DIE does
5388 not have a name. NAME may either be from a previous call to
5389 dwarf2_name or NULL.
5390
5391 The output string will be canonicalized (if C++/Java). */
5392
5393 static const char *
5394 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5395 {
5396 return dwarf2_compute_name (name, die, cu, 0);
5397 }
5398
5399 /* Construct a physname for the given DIE in CU. NAME may either be
5400 from a previous call to dwarf2_name or NULL. The result will be
5401 allocated on the objfile_objstack or NULL if the DIE does not have a
5402 name.
5403
5404 The output string will be canonicalized (if C++/Java). */
5405
5406 static const char *
5407 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5408 {
5409 struct objfile *objfile = cu->objfile;
5410 struct attribute *attr;
5411 const char *retval, *mangled = NULL, *canon = NULL;
5412 struct cleanup *back_to;
5413 int need_copy = 1;
5414
5415 /* In this case dwarf2_compute_name is just a shortcut not building anything
5416 on its own. */
5417 if (!die_needs_namespace (die, cu))
5418 return dwarf2_compute_name (name, die, cu, 1);
5419
5420 back_to = make_cleanup (null_cleanup, NULL);
5421
5422 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5423 if (!attr)
5424 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5425
5426 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5427 has computed. */
5428 if (attr && DW_STRING (attr))
5429 {
5430 char *demangled;
5431
5432 mangled = DW_STRING (attr);
5433
5434 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5435 type. It is easier for GDB users to search for such functions as
5436 `name(params)' than `long name(params)'. In such case the minimal
5437 symbol names do not match the full symbol names but for template
5438 functions there is never a need to look up their definition from their
5439 declaration so the only disadvantage remains the minimal symbol
5440 variant `long name(params)' does not have the proper inferior type.
5441 */
5442
5443 if (cu->language == language_go)
5444 {
5445 /* This is a lie, but we already lie to the caller new_symbol_full.
5446 new_symbol_full assumes we return the mangled name.
5447 This just undoes that lie until things are cleaned up. */
5448 demangled = NULL;
5449 }
5450 else
5451 {
5452 demangled = cplus_demangle (mangled,
5453 (DMGL_PARAMS | DMGL_ANSI
5454 | (cu->language == language_java
5455 ? DMGL_JAVA | DMGL_RET_POSTFIX
5456 : DMGL_RET_DROP)));
5457 }
5458 if (demangled)
5459 {
5460 make_cleanup (xfree, demangled);
5461 canon = demangled;
5462 }
5463 else
5464 {
5465 canon = mangled;
5466 need_copy = 0;
5467 }
5468 }
5469
5470 if (canon == NULL || check_physname)
5471 {
5472 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5473
5474 if (canon != NULL && strcmp (physname, canon) != 0)
5475 {
5476 /* It may not mean a bug in GDB. The compiler could also
5477 compute DW_AT_linkage_name incorrectly. But in such case
5478 GDB would need to be bug-to-bug compatible. */
5479
5480 complaint (&symfile_complaints,
5481 _("Computed physname <%s> does not match demangled <%s> "
5482 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5483 physname, canon, mangled, die->offset.sect_off, objfile->name);
5484
5485 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5486 is available here - over computed PHYSNAME. It is safer
5487 against both buggy GDB and buggy compilers. */
5488
5489 retval = canon;
5490 }
5491 else
5492 {
5493 retval = physname;
5494 need_copy = 0;
5495 }
5496 }
5497 else
5498 retval = canon;
5499
5500 if (need_copy)
5501 retval = obsavestring (retval, strlen (retval),
5502 &objfile->objfile_obstack);
5503
5504 do_cleanups (back_to);
5505 return retval;
5506 }
5507
5508 /* Read the import statement specified by the given die and record it. */
5509
5510 static void
5511 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5512 {
5513 struct objfile *objfile = cu->objfile;
5514 struct attribute *import_attr;
5515 struct die_info *imported_die, *child_die;
5516 struct dwarf2_cu *imported_cu;
5517 const char *imported_name;
5518 const char *imported_name_prefix;
5519 const char *canonical_name;
5520 const char *import_alias;
5521 const char *imported_declaration = NULL;
5522 const char *import_prefix;
5523 VEC (const_char_ptr) *excludes = NULL;
5524 struct cleanup *cleanups;
5525
5526 char *temp;
5527
5528 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5529 if (import_attr == NULL)
5530 {
5531 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5532 dwarf_tag_name (die->tag));
5533 return;
5534 }
5535
5536 imported_cu = cu;
5537 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5538 imported_name = dwarf2_name (imported_die, imported_cu);
5539 if (imported_name == NULL)
5540 {
5541 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5542
5543 The import in the following code:
5544 namespace A
5545 {
5546 typedef int B;
5547 }
5548
5549 int main ()
5550 {
5551 using A::B;
5552 B b;
5553 return b;
5554 }
5555
5556 ...
5557 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5558 <52> DW_AT_decl_file : 1
5559 <53> DW_AT_decl_line : 6
5560 <54> DW_AT_import : <0x75>
5561 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5562 <59> DW_AT_name : B
5563 <5b> DW_AT_decl_file : 1
5564 <5c> DW_AT_decl_line : 2
5565 <5d> DW_AT_type : <0x6e>
5566 ...
5567 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5568 <76> DW_AT_byte_size : 4
5569 <77> DW_AT_encoding : 5 (signed)
5570
5571 imports the wrong die ( 0x75 instead of 0x58 ).
5572 This case will be ignored until the gcc bug is fixed. */
5573 return;
5574 }
5575
5576 /* Figure out the local name after import. */
5577 import_alias = dwarf2_name (die, cu);
5578
5579 /* Figure out where the statement is being imported to. */
5580 import_prefix = determine_prefix (die, cu);
5581
5582 /* Figure out what the scope of the imported die is and prepend it
5583 to the name of the imported die. */
5584 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5585
5586 if (imported_die->tag != DW_TAG_namespace
5587 && imported_die->tag != DW_TAG_module)
5588 {
5589 imported_declaration = imported_name;
5590 canonical_name = imported_name_prefix;
5591 }
5592 else if (strlen (imported_name_prefix) > 0)
5593 {
5594 temp = alloca (strlen (imported_name_prefix)
5595 + 2 + strlen (imported_name) + 1);
5596 strcpy (temp, imported_name_prefix);
5597 strcat (temp, "::");
5598 strcat (temp, imported_name);
5599 canonical_name = temp;
5600 }
5601 else
5602 canonical_name = imported_name;
5603
5604 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5605
5606 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5607 for (child_die = die->child; child_die && child_die->tag;
5608 child_die = sibling_die (child_die))
5609 {
5610 /* DWARF-4: A Fortran use statement with a “rename list” may be
5611 represented by an imported module entry with an import attribute
5612 referring to the module and owned entries corresponding to those
5613 entities that are renamed as part of being imported. */
5614
5615 if (child_die->tag != DW_TAG_imported_declaration)
5616 {
5617 complaint (&symfile_complaints,
5618 _("child DW_TAG_imported_declaration expected "
5619 "- DIE at 0x%x [in module %s]"),
5620 child_die->offset.sect_off, objfile->name);
5621 continue;
5622 }
5623
5624 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5625 if (import_attr == NULL)
5626 {
5627 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5628 dwarf_tag_name (child_die->tag));
5629 continue;
5630 }
5631
5632 imported_cu = cu;
5633 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5634 &imported_cu);
5635 imported_name = dwarf2_name (imported_die, imported_cu);
5636 if (imported_name == NULL)
5637 {
5638 complaint (&symfile_complaints,
5639 _("child DW_TAG_imported_declaration has unknown "
5640 "imported name - DIE at 0x%x [in module %s]"),
5641 child_die->offset.sect_off, objfile->name);
5642 continue;
5643 }
5644
5645 VEC_safe_push (const_char_ptr, excludes, imported_name);
5646
5647 process_die (child_die, cu);
5648 }
5649
5650 cp_add_using_directive (import_prefix,
5651 canonical_name,
5652 import_alias,
5653 imported_declaration,
5654 excludes,
5655 &objfile->objfile_obstack);
5656
5657 do_cleanups (cleanups);
5658 }
5659
5660 /* Cleanup function for read_file_scope. */
5661
5662 static void
5663 free_cu_line_header (void *arg)
5664 {
5665 struct dwarf2_cu *cu = arg;
5666
5667 free_line_header (cu->line_header);
5668 cu->line_header = NULL;
5669 }
5670
5671 static void
5672 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5673 char **name, char **comp_dir)
5674 {
5675 struct attribute *attr;
5676
5677 *name = NULL;
5678 *comp_dir = NULL;
5679
5680 /* Find the filename. Do not use dwarf2_name here, since the filename
5681 is not a source language identifier. */
5682 attr = dwarf2_attr (die, DW_AT_name, cu);
5683 if (attr)
5684 {
5685 *name = DW_STRING (attr);
5686 }
5687
5688 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5689 if (attr)
5690 *comp_dir = DW_STRING (attr);
5691 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5692 {
5693 *comp_dir = ldirname (*name);
5694 if (*comp_dir != NULL)
5695 make_cleanup (xfree, *comp_dir);
5696 }
5697 if (*comp_dir != NULL)
5698 {
5699 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5700 directory, get rid of it. */
5701 char *cp = strchr (*comp_dir, ':');
5702
5703 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5704 *comp_dir = cp + 1;
5705 }
5706
5707 if (*name == NULL)
5708 *name = "<unknown>";
5709 }
5710
5711 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
5712 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5713 COMP_DIR is the compilation directory.
5714 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
5715
5716 static void
5717 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5718 const char *comp_dir, int want_line_info)
5719 {
5720 struct attribute *attr;
5721 struct objfile *objfile = cu->objfile;
5722 bfd *abfd = objfile->obfd;
5723
5724 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5725 if (attr)
5726 {
5727 unsigned int line_offset = DW_UNSND (attr);
5728 struct line_header *line_header
5729 = dwarf_decode_line_header (line_offset, abfd, cu);
5730
5731 if (line_header)
5732 {
5733 cu->line_header = line_header;
5734 make_cleanup (free_cu_line_header, cu);
5735 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
5736 }
5737 }
5738 }
5739
5740 /* Process DW_TAG_compile_unit. */
5741
5742 static void
5743 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5744 {
5745 struct objfile *objfile = cu->objfile;
5746 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5747 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5748 CORE_ADDR highpc = ((CORE_ADDR) 0);
5749 struct attribute *attr;
5750 char *name = NULL;
5751 char *comp_dir = NULL;
5752 struct die_info *child_die;
5753 bfd *abfd = objfile->obfd;
5754 CORE_ADDR baseaddr;
5755
5756 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5757
5758 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5759
5760 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5761 from finish_block. */
5762 if (lowpc == ((CORE_ADDR) -1))
5763 lowpc = highpc;
5764 lowpc += baseaddr;
5765 highpc += baseaddr;
5766
5767 find_file_and_directory (die, cu, &name, &comp_dir);
5768
5769 attr = dwarf2_attr (die, DW_AT_language, cu);
5770 if (attr)
5771 {
5772 set_cu_language (DW_UNSND (attr), cu);
5773 }
5774
5775 attr = dwarf2_attr (die, DW_AT_producer, cu);
5776 if (attr)
5777 cu->producer = DW_STRING (attr);
5778
5779 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5780 standardised yet. As a workaround for the language detection we fall
5781 back to the DW_AT_producer string. */
5782 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5783 cu->language = language_opencl;
5784
5785 /* Similar hack for Go. */
5786 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
5787 set_cu_language (DW_LANG_Go, cu);
5788
5789 /* We assume that we're processing GCC output. */
5790 processing_gcc_compilation = 2;
5791
5792 processing_has_namespace_info = 0;
5793
5794 start_symtab (name, comp_dir, lowpc);
5795 record_debugformat ("DWARF 2");
5796 record_producer (cu->producer);
5797
5798 /* Decode line number information if present. We do this before
5799 processing child DIEs, so that the line header table is available
5800 for DW_AT_decl_file. */
5801 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
5802
5803 /* Process all dies in compilation unit. */
5804 if (die->child != NULL)
5805 {
5806 child_die = die->child;
5807 while (child_die && child_die->tag)
5808 {
5809 process_die (child_die, cu);
5810 child_die = sibling_die (child_die);
5811 }
5812 }
5813
5814 /* Decode macro information, if present. Dwarf 2 macro information
5815 refers to information in the line number info statement program
5816 header, so we can only read it if we've read the header
5817 successfully. */
5818 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5819 if (attr && cu->line_header)
5820 {
5821 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5822 complaint (&symfile_complaints,
5823 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5824
5825 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5826 comp_dir, abfd, cu,
5827 &dwarf2_per_objfile->macro, 1);
5828 }
5829 else
5830 {
5831 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5832 if (attr && cu->line_header)
5833 {
5834 unsigned int macro_offset = DW_UNSND (attr);
5835
5836 dwarf_decode_macros (cu->line_header, macro_offset,
5837 comp_dir, abfd, cu,
5838 &dwarf2_per_objfile->macinfo, 0);
5839 }
5840 }
5841
5842 do_cleanups (back_to);
5843 }
5844
5845 /* Process DW_TAG_type_unit.
5846 For TUs we want to skip the first top level sibling if it's not the
5847 actual type being defined by this TU. In this case the first top
5848 level sibling is there to provide context only. */
5849
5850 static void
5851 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5852 {
5853 struct objfile *objfile = cu->objfile;
5854 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5855 CORE_ADDR lowpc;
5856 struct attribute *attr;
5857 char *name = NULL;
5858 char *comp_dir = NULL;
5859 struct die_info *child_die;
5860 bfd *abfd = objfile->obfd;
5861
5862 /* start_symtab needs a low pc, but we don't really have one.
5863 Do what read_file_scope would do in the absence of such info. */
5864 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5865
5866 /* Find the filename. Do not use dwarf2_name here, since the filename
5867 is not a source language identifier. */
5868 attr = dwarf2_attr (die, DW_AT_name, cu);
5869 if (attr)
5870 name = DW_STRING (attr);
5871
5872 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5873 if (attr)
5874 comp_dir = DW_STRING (attr);
5875 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5876 {
5877 comp_dir = ldirname (name);
5878 if (comp_dir != NULL)
5879 make_cleanup (xfree, comp_dir);
5880 }
5881
5882 if (name == NULL)
5883 name = "<unknown>";
5884
5885 attr = dwarf2_attr (die, DW_AT_language, cu);
5886 if (attr)
5887 set_cu_language (DW_UNSND (attr), cu);
5888
5889 /* This isn't technically needed today. It is done for symmetry
5890 with read_file_scope. */
5891 attr = dwarf2_attr (die, DW_AT_producer, cu);
5892 if (attr)
5893 cu->producer = DW_STRING (attr);
5894
5895 /* We assume that we're processing GCC output. */
5896 processing_gcc_compilation = 2;
5897
5898 processing_has_namespace_info = 0;
5899
5900 start_symtab (name, comp_dir, lowpc);
5901 record_debugformat ("DWARF 2");
5902 record_producer (cu->producer);
5903
5904 /* Decode line number information if present. We do this before
5905 processing child DIEs, so that the line header table is available
5906 for DW_AT_decl_file.
5907 We don't need the pc/line-number mapping for type units. */
5908 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
5909
5910 /* Process the dies in the type unit. */
5911 if (die->child == NULL)
5912 {
5913 dump_die_for_error (die);
5914 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5915 bfd_get_filename (abfd));
5916 }
5917
5918 child_die = die->child;
5919
5920 while (child_die && child_die->tag)
5921 {
5922 process_die (child_die, cu);
5923
5924 child_die = sibling_die (child_die);
5925 }
5926
5927 do_cleanups (back_to);
5928 }
5929
5930 /* qsort helper for inherit_abstract_dies. */
5931
5932 static int
5933 unsigned_int_compar (const void *ap, const void *bp)
5934 {
5935 unsigned int a = *(unsigned int *) ap;
5936 unsigned int b = *(unsigned int *) bp;
5937
5938 return (a > b) - (b > a);
5939 }
5940
5941 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5942 Inherit only the children of the DW_AT_abstract_origin DIE not being
5943 already referenced by DW_AT_abstract_origin from the children of the
5944 current DIE. */
5945
5946 static void
5947 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5948 {
5949 struct die_info *child_die;
5950 unsigned die_children_count;
5951 /* CU offsets which were referenced by children of the current DIE. */
5952 sect_offset *offsets;
5953 sect_offset *offsets_end, *offsetp;
5954 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5955 struct die_info *origin_die;
5956 /* Iterator of the ORIGIN_DIE children. */
5957 struct die_info *origin_child_die;
5958 struct cleanup *cleanups;
5959 struct attribute *attr;
5960 struct dwarf2_cu *origin_cu;
5961 struct pending **origin_previous_list_in_scope;
5962
5963 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5964 if (!attr)
5965 return;
5966
5967 /* Note that following die references may follow to a die in a
5968 different cu. */
5969
5970 origin_cu = cu;
5971 origin_die = follow_die_ref (die, attr, &origin_cu);
5972
5973 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5974 symbols in. */
5975 origin_previous_list_in_scope = origin_cu->list_in_scope;
5976 origin_cu->list_in_scope = cu->list_in_scope;
5977
5978 if (die->tag != origin_die->tag
5979 && !(die->tag == DW_TAG_inlined_subroutine
5980 && origin_die->tag == DW_TAG_subprogram))
5981 complaint (&symfile_complaints,
5982 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5983 die->offset.sect_off, origin_die->offset.sect_off);
5984
5985 child_die = die->child;
5986 die_children_count = 0;
5987 while (child_die && child_die->tag)
5988 {
5989 child_die = sibling_die (child_die);
5990 die_children_count++;
5991 }
5992 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5993 cleanups = make_cleanup (xfree, offsets);
5994
5995 offsets_end = offsets;
5996 child_die = die->child;
5997 while (child_die && child_die->tag)
5998 {
5999 /* For each CHILD_DIE, find the corresponding child of
6000 ORIGIN_DIE. If there is more than one layer of
6001 DW_AT_abstract_origin, follow them all; there shouldn't be,
6002 but GCC versions at least through 4.4 generate this (GCC PR
6003 40573). */
6004 struct die_info *child_origin_die = child_die;
6005 struct dwarf2_cu *child_origin_cu = cu;
6006
6007 while (1)
6008 {
6009 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
6010 child_origin_cu);
6011 if (attr == NULL)
6012 break;
6013 child_origin_die = follow_die_ref (child_origin_die, attr,
6014 &child_origin_cu);
6015 }
6016
6017 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
6018 counterpart may exist. */
6019 if (child_origin_die != child_die)
6020 {
6021 if (child_die->tag != child_origin_die->tag
6022 && !(child_die->tag == DW_TAG_inlined_subroutine
6023 && child_origin_die->tag == DW_TAG_subprogram))
6024 complaint (&symfile_complaints,
6025 _("Child DIE 0x%x and its abstract origin 0x%x have "
6026 "different tags"), child_die->offset.sect_off,
6027 child_origin_die->offset.sect_off);
6028 if (child_origin_die->parent != origin_die)
6029 complaint (&symfile_complaints,
6030 _("Child DIE 0x%x and its abstract origin 0x%x have "
6031 "different parents"), child_die->offset.sect_off,
6032 child_origin_die->offset.sect_off);
6033 else
6034 *offsets_end++ = child_origin_die->offset;
6035 }
6036 child_die = sibling_die (child_die);
6037 }
6038 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
6039 unsigned_int_compar);
6040 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
6041 if (offsetp[-1].sect_off == offsetp->sect_off)
6042 complaint (&symfile_complaints,
6043 _("Multiple children of DIE 0x%x refer "
6044 "to DIE 0x%x as their abstract origin"),
6045 die->offset.sect_off, offsetp->sect_off);
6046
6047 offsetp = offsets;
6048 origin_child_die = origin_die->child;
6049 while (origin_child_die && origin_child_die->tag)
6050 {
6051 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
6052 while (offsetp < offsets_end
6053 && offsetp->sect_off < origin_child_die->offset.sect_off)
6054 offsetp++;
6055 if (offsetp >= offsets_end
6056 || offsetp->sect_off > origin_child_die->offset.sect_off)
6057 {
6058 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
6059 process_die (origin_child_die, origin_cu);
6060 }
6061 origin_child_die = sibling_die (origin_child_die);
6062 }
6063 origin_cu->list_in_scope = origin_previous_list_in_scope;
6064
6065 do_cleanups (cleanups);
6066 }
6067
6068 static void
6069 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
6070 {
6071 struct objfile *objfile = cu->objfile;
6072 struct context_stack *new;
6073 CORE_ADDR lowpc;
6074 CORE_ADDR highpc;
6075 struct die_info *child_die;
6076 struct attribute *attr, *call_line, *call_file;
6077 char *name;
6078 CORE_ADDR baseaddr;
6079 struct block *block;
6080 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
6081 VEC (symbolp) *template_args = NULL;
6082 struct template_symbol *templ_func = NULL;
6083
6084 if (inlined_func)
6085 {
6086 /* If we do not have call site information, we can't show the
6087 caller of this inlined function. That's too confusing, so
6088 only use the scope for local variables. */
6089 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
6090 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
6091 if (call_line == NULL || call_file == NULL)
6092 {
6093 read_lexical_block_scope (die, cu);
6094 return;
6095 }
6096 }
6097
6098 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6099
6100 name = dwarf2_name (die, cu);
6101
6102 /* Ignore functions with missing or empty names. These are actually
6103 illegal according to the DWARF standard. */
6104 if (name == NULL)
6105 {
6106 complaint (&symfile_complaints,
6107 _("missing name for subprogram DIE at %d"),
6108 die->offset.sect_off);
6109 return;
6110 }
6111
6112 /* Ignore functions with missing or invalid low and high pc attributes. */
6113 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6114 {
6115 attr = dwarf2_attr (die, DW_AT_external, cu);
6116 if (!attr || !DW_UNSND (attr))
6117 complaint (&symfile_complaints,
6118 _("cannot get low and high bounds "
6119 "for subprogram DIE at %d"),
6120 die->offset.sect_off);
6121 return;
6122 }
6123
6124 lowpc += baseaddr;
6125 highpc += baseaddr;
6126
6127 /* If we have any template arguments, then we must allocate a
6128 different sort of symbol. */
6129 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6130 {
6131 if (child_die->tag == DW_TAG_template_type_param
6132 || child_die->tag == DW_TAG_template_value_param)
6133 {
6134 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6135 struct template_symbol);
6136 templ_func->base.is_cplus_template_function = 1;
6137 break;
6138 }
6139 }
6140
6141 new = push_context (0, lowpc);
6142 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6143 (struct symbol *) templ_func);
6144
6145 /* If there is a location expression for DW_AT_frame_base, record
6146 it. */
6147 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6148 if (attr)
6149 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6150 expression is being recorded directly in the function's symbol
6151 and not in a separate frame-base object. I guess this hack is
6152 to avoid adding some sort of frame-base adjunct/annex to the
6153 function's symbol :-(. The problem with doing this is that it
6154 results in a function symbol with a location expression that
6155 has nothing to do with the location of the function, ouch! The
6156 relationship should be: a function's symbol has-a frame base; a
6157 frame-base has-a location expression. */
6158 dwarf2_symbol_mark_computed (attr, new->name, cu);
6159
6160 cu->list_in_scope = &local_symbols;
6161
6162 if (die->child != NULL)
6163 {
6164 child_die = die->child;
6165 while (child_die && child_die->tag)
6166 {
6167 if (child_die->tag == DW_TAG_template_type_param
6168 || child_die->tag == DW_TAG_template_value_param)
6169 {
6170 struct symbol *arg = new_symbol (child_die, NULL, cu);
6171
6172 if (arg != NULL)
6173 VEC_safe_push (symbolp, template_args, arg);
6174 }
6175 else
6176 process_die (child_die, cu);
6177 child_die = sibling_die (child_die);
6178 }
6179 }
6180
6181 inherit_abstract_dies (die, cu);
6182
6183 /* If we have a DW_AT_specification, we might need to import using
6184 directives from the context of the specification DIE. See the
6185 comment in determine_prefix. */
6186 if (cu->language == language_cplus
6187 && dwarf2_attr (die, DW_AT_specification, cu))
6188 {
6189 struct dwarf2_cu *spec_cu = cu;
6190 struct die_info *spec_die = die_specification (die, &spec_cu);
6191
6192 while (spec_die)
6193 {
6194 child_die = spec_die->child;
6195 while (child_die && child_die->tag)
6196 {
6197 if (child_die->tag == DW_TAG_imported_module)
6198 process_die (child_die, spec_cu);
6199 child_die = sibling_die (child_die);
6200 }
6201
6202 /* In some cases, GCC generates specification DIEs that
6203 themselves contain DW_AT_specification attributes. */
6204 spec_die = die_specification (spec_die, &spec_cu);
6205 }
6206 }
6207
6208 new = pop_context ();
6209 /* Make a block for the local symbols within. */
6210 block = finish_block (new->name, &local_symbols, new->old_blocks,
6211 lowpc, highpc, objfile);
6212
6213 /* For C++, set the block's scope. */
6214 if (cu->language == language_cplus || cu->language == language_fortran)
6215 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6216 determine_prefix (die, cu),
6217 processing_has_namespace_info);
6218
6219 /* If we have address ranges, record them. */
6220 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6221
6222 /* Attach template arguments to function. */
6223 if (! VEC_empty (symbolp, template_args))
6224 {
6225 gdb_assert (templ_func != NULL);
6226
6227 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6228 templ_func->template_arguments
6229 = obstack_alloc (&objfile->objfile_obstack,
6230 (templ_func->n_template_arguments
6231 * sizeof (struct symbol *)));
6232 memcpy (templ_func->template_arguments,
6233 VEC_address (symbolp, template_args),
6234 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6235 VEC_free (symbolp, template_args);
6236 }
6237
6238 /* In C++, we can have functions nested inside functions (e.g., when
6239 a function declares a class that has methods). This means that
6240 when we finish processing a function scope, we may need to go
6241 back to building a containing block's symbol lists. */
6242 local_symbols = new->locals;
6243 param_symbols = new->params;
6244 using_directives = new->using_directives;
6245
6246 /* If we've finished processing a top-level function, subsequent
6247 symbols go in the file symbol list. */
6248 if (outermost_context_p ())
6249 cu->list_in_scope = &file_symbols;
6250 }
6251
6252 /* Process all the DIES contained within a lexical block scope. Start
6253 a new scope, process the dies, and then close the scope. */
6254
6255 static void
6256 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6257 {
6258 struct objfile *objfile = cu->objfile;
6259 struct context_stack *new;
6260 CORE_ADDR lowpc, highpc;
6261 struct die_info *child_die;
6262 CORE_ADDR baseaddr;
6263
6264 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6265
6266 /* Ignore blocks with missing or invalid low and high pc attributes. */
6267 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6268 as multiple lexical blocks? Handling children in a sane way would
6269 be nasty. Might be easier to properly extend generic blocks to
6270 describe ranges. */
6271 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6272 return;
6273 lowpc += baseaddr;
6274 highpc += baseaddr;
6275
6276 push_context (0, lowpc);
6277 if (die->child != NULL)
6278 {
6279 child_die = die->child;
6280 while (child_die && child_die->tag)
6281 {
6282 process_die (child_die, cu);
6283 child_die = sibling_die (child_die);
6284 }
6285 }
6286 new = pop_context ();
6287
6288 if (local_symbols != NULL || using_directives != NULL)
6289 {
6290 struct block *block
6291 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6292 highpc, objfile);
6293
6294 /* Note that recording ranges after traversing children, as we
6295 do here, means that recording a parent's ranges entails
6296 walking across all its children's ranges as they appear in
6297 the address map, which is quadratic behavior.
6298
6299 It would be nicer to record the parent's ranges before
6300 traversing its children, simply overriding whatever you find
6301 there. But since we don't even decide whether to create a
6302 block until after we've traversed its children, that's hard
6303 to do. */
6304 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6305 }
6306 local_symbols = new->locals;
6307 using_directives = new->using_directives;
6308 }
6309
6310 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6311
6312 static void
6313 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6314 {
6315 struct objfile *objfile = cu->objfile;
6316 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6317 CORE_ADDR pc, baseaddr;
6318 struct attribute *attr;
6319 struct call_site *call_site, call_site_local;
6320 void **slot;
6321 int nparams;
6322 struct die_info *child_die;
6323
6324 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6325
6326 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6327 if (!attr)
6328 {
6329 complaint (&symfile_complaints,
6330 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6331 "DIE 0x%x [in module %s]"),
6332 die->offset.sect_off, objfile->name);
6333 return;
6334 }
6335 pc = DW_ADDR (attr) + baseaddr;
6336
6337 if (cu->call_site_htab == NULL)
6338 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6339 NULL, &objfile->objfile_obstack,
6340 hashtab_obstack_allocate, NULL);
6341 call_site_local.pc = pc;
6342 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6343 if (*slot != NULL)
6344 {
6345 complaint (&symfile_complaints,
6346 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6347 "DIE 0x%x [in module %s]"),
6348 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
6349 return;
6350 }
6351
6352 /* Count parameters at the caller. */
6353
6354 nparams = 0;
6355 for (child_die = die->child; child_die && child_die->tag;
6356 child_die = sibling_die (child_die))
6357 {
6358 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6359 {
6360 complaint (&symfile_complaints,
6361 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6362 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6363 child_die->tag, child_die->offset.sect_off, objfile->name);
6364 continue;
6365 }
6366
6367 nparams++;
6368 }
6369
6370 call_site = obstack_alloc (&objfile->objfile_obstack,
6371 (sizeof (*call_site)
6372 + (sizeof (*call_site->parameter)
6373 * (nparams - 1))));
6374 *slot = call_site;
6375 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6376 call_site->pc = pc;
6377
6378 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6379 {
6380 struct die_info *func_die;
6381
6382 /* Skip also over DW_TAG_inlined_subroutine. */
6383 for (func_die = die->parent;
6384 func_die && func_die->tag != DW_TAG_subprogram
6385 && func_die->tag != DW_TAG_subroutine_type;
6386 func_die = func_die->parent);
6387
6388 /* DW_AT_GNU_all_call_sites is a superset
6389 of DW_AT_GNU_all_tail_call_sites. */
6390 if (func_die
6391 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6392 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6393 {
6394 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6395 not complete. But keep CALL_SITE for look ups via call_site_htab,
6396 both the initial caller containing the real return address PC and
6397 the final callee containing the current PC of a chain of tail
6398 calls do not need to have the tail call list complete. But any
6399 function candidate for a virtual tail call frame searched via
6400 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6401 determined unambiguously. */
6402 }
6403 else
6404 {
6405 struct type *func_type = NULL;
6406
6407 if (func_die)
6408 func_type = get_die_type (func_die, cu);
6409 if (func_type != NULL)
6410 {
6411 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6412
6413 /* Enlist this call site to the function. */
6414 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6415 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6416 }
6417 else
6418 complaint (&symfile_complaints,
6419 _("Cannot find function owning DW_TAG_GNU_call_site "
6420 "DIE 0x%x [in module %s]"),
6421 die->offset.sect_off, objfile->name);
6422 }
6423 }
6424
6425 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6426 if (attr == NULL)
6427 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6428 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6429 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6430 /* Keep NULL DWARF_BLOCK. */;
6431 else if (attr_form_is_block (attr))
6432 {
6433 struct dwarf2_locexpr_baton *dlbaton;
6434
6435 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6436 dlbaton->data = DW_BLOCK (attr)->data;
6437 dlbaton->size = DW_BLOCK (attr)->size;
6438 dlbaton->per_cu = cu->per_cu;
6439
6440 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6441 }
6442 else if (is_ref_attr (attr))
6443 {
6444 struct dwarf2_cu *target_cu = cu;
6445 struct die_info *target_die;
6446
6447 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6448 gdb_assert (target_cu->objfile == objfile);
6449 if (die_is_declaration (target_die, target_cu))
6450 {
6451 const char *target_physname;
6452
6453 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6454 if (target_physname == NULL)
6455 complaint (&symfile_complaints,
6456 _("DW_AT_GNU_call_site_target target DIE has invalid "
6457 "physname, for referencing DIE 0x%x [in module %s]"),
6458 die->offset.sect_off, objfile->name);
6459 else
6460 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6461 }
6462 else
6463 {
6464 CORE_ADDR lowpc;
6465
6466 /* DW_AT_entry_pc should be preferred. */
6467 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6468 complaint (&symfile_complaints,
6469 _("DW_AT_GNU_call_site_target target DIE has invalid "
6470 "low pc, for referencing DIE 0x%x [in module %s]"),
6471 die->offset.sect_off, objfile->name);
6472 else
6473 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6474 }
6475 }
6476 else
6477 complaint (&symfile_complaints,
6478 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6479 "block nor reference, for DIE 0x%x [in module %s]"),
6480 die->offset.sect_off, objfile->name);
6481
6482 call_site->per_cu = cu->per_cu;
6483
6484 for (child_die = die->child;
6485 child_die && child_die->tag;
6486 child_die = sibling_die (child_die))
6487 {
6488 struct dwarf2_locexpr_baton *dlbaton;
6489 struct call_site_parameter *parameter;
6490
6491 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6492 {
6493 /* Already printed the complaint above. */
6494 continue;
6495 }
6496
6497 gdb_assert (call_site->parameter_count < nparams);
6498 parameter = &call_site->parameter[call_site->parameter_count];
6499
6500 /* DW_AT_location specifies the register number. Value of the data
6501 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6502
6503 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6504 if (!attr || !attr_form_is_block (attr))
6505 {
6506 complaint (&symfile_complaints,
6507 _("No DW_FORM_block* DW_AT_location for "
6508 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6509 child_die->offset.sect_off, objfile->name);
6510 continue;
6511 }
6512 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6513 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6514 if (parameter->dwarf_reg == -1
6515 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6516 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6517 &parameter->fb_offset))
6518 {
6519 complaint (&symfile_complaints,
6520 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6521 "for DW_FORM_block* DW_AT_location for "
6522 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6523 child_die->offset.sect_off, objfile->name);
6524 continue;
6525 }
6526
6527 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6528 if (!attr_form_is_block (attr))
6529 {
6530 complaint (&symfile_complaints,
6531 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6532 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6533 child_die->offset.sect_off, objfile->name);
6534 continue;
6535 }
6536 parameter->value = DW_BLOCK (attr)->data;
6537 parameter->value_size = DW_BLOCK (attr)->size;
6538
6539 /* Parameters are not pre-cleared by memset above. */
6540 parameter->data_value = NULL;
6541 parameter->data_value_size = 0;
6542 call_site->parameter_count++;
6543
6544 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6545 if (attr)
6546 {
6547 if (!attr_form_is_block (attr))
6548 complaint (&symfile_complaints,
6549 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6550 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6551 child_die->offset.sect_off, objfile->name);
6552 else
6553 {
6554 parameter->data_value = DW_BLOCK (attr)->data;
6555 parameter->data_value_size = DW_BLOCK (attr)->size;
6556 }
6557 }
6558 }
6559 }
6560
6561 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6562 Return 1 if the attributes are present and valid, otherwise, return 0.
6563 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6564
6565 static int
6566 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6567 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6568 struct partial_symtab *ranges_pst)
6569 {
6570 struct objfile *objfile = cu->objfile;
6571 struct comp_unit_head *cu_header = &cu->header;
6572 bfd *obfd = objfile->obfd;
6573 unsigned int addr_size = cu_header->addr_size;
6574 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6575 /* Base address selection entry. */
6576 CORE_ADDR base;
6577 int found_base;
6578 unsigned int dummy;
6579 gdb_byte *buffer;
6580 CORE_ADDR marker;
6581 int low_set;
6582 CORE_ADDR low = 0;
6583 CORE_ADDR high = 0;
6584 CORE_ADDR baseaddr;
6585
6586 found_base = cu->base_known;
6587 base = cu->base_address;
6588
6589 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6590 if (offset >= dwarf2_per_objfile->ranges.size)
6591 {
6592 complaint (&symfile_complaints,
6593 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6594 offset);
6595 return 0;
6596 }
6597 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6598
6599 /* Read in the largest possible address. */
6600 marker = read_address (obfd, buffer, cu, &dummy);
6601 if ((marker & mask) == mask)
6602 {
6603 /* If we found the largest possible address, then
6604 read the base address. */
6605 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6606 buffer += 2 * addr_size;
6607 offset += 2 * addr_size;
6608 found_base = 1;
6609 }
6610
6611 low_set = 0;
6612
6613 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6614
6615 while (1)
6616 {
6617 CORE_ADDR range_beginning, range_end;
6618
6619 range_beginning = read_address (obfd, buffer, cu, &dummy);
6620 buffer += addr_size;
6621 range_end = read_address (obfd, buffer, cu, &dummy);
6622 buffer += addr_size;
6623 offset += 2 * addr_size;
6624
6625 /* An end of list marker is a pair of zero addresses. */
6626 if (range_beginning == 0 && range_end == 0)
6627 /* Found the end of list entry. */
6628 break;
6629
6630 /* Each base address selection entry is a pair of 2 values.
6631 The first is the largest possible address, the second is
6632 the base address. Check for a base address here. */
6633 if ((range_beginning & mask) == mask)
6634 {
6635 /* If we found the largest possible address, then
6636 read the base address. */
6637 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6638 found_base = 1;
6639 continue;
6640 }
6641
6642 if (!found_base)
6643 {
6644 /* We have no valid base address for the ranges
6645 data. */
6646 complaint (&symfile_complaints,
6647 _("Invalid .debug_ranges data (no base address)"));
6648 return 0;
6649 }
6650
6651 if (range_beginning > range_end)
6652 {
6653 /* Inverted range entries are invalid. */
6654 complaint (&symfile_complaints,
6655 _("Invalid .debug_ranges data (inverted range)"));
6656 return 0;
6657 }
6658
6659 /* Empty range entries have no effect. */
6660 if (range_beginning == range_end)
6661 continue;
6662
6663 range_beginning += base;
6664 range_end += base;
6665
6666 if (ranges_pst != NULL)
6667 addrmap_set_empty (objfile->psymtabs_addrmap,
6668 range_beginning + baseaddr,
6669 range_end - 1 + baseaddr,
6670 ranges_pst);
6671
6672 /* FIXME: This is recording everything as a low-high
6673 segment of consecutive addresses. We should have a
6674 data structure for discontiguous block ranges
6675 instead. */
6676 if (! low_set)
6677 {
6678 low = range_beginning;
6679 high = range_end;
6680 low_set = 1;
6681 }
6682 else
6683 {
6684 if (range_beginning < low)
6685 low = range_beginning;
6686 if (range_end > high)
6687 high = range_end;
6688 }
6689 }
6690
6691 if (! low_set)
6692 /* If the first entry is an end-of-list marker, the range
6693 describes an empty scope, i.e. no instructions. */
6694 return 0;
6695
6696 if (low_return)
6697 *low_return = low;
6698 if (high_return)
6699 *high_return = high;
6700 return 1;
6701 }
6702
6703 /* Get low and high pc attributes from a die. Return 1 if the attributes
6704 are present and valid, otherwise, return 0. Return -1 if the range is
6705 discontinuous, i.e. derived from DW_AT_ranges information. */
6706
6707 static int
6708 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6709 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6710 struct partial_symtab *pst)
6711 {
6712 struct attribute *attr;
6713 struct attribute *attr_high;
6714 CORE_ADDR low = 0;
6715 CORE_ADDR high = 0;
6716 int ret = 0;
6717
6718 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
6719 if (attr_high)
6720 {
6721 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6722 if (attr)
6723 {
6724 low = DW_ADDR (attr);
6725 if (attr_high->form == DW_FORM_addr)
6726 high = DW_ADDR (attr_high);
6727 else
6728 high = low + DW_UNSND (attr_high);
6729 }
6730 else
6731 /* Found high w/o low attribute. */
6732 return 0;
6733
6734 /* Found consecutive range of addresses. */
6735 ret = 1;
6736 }
6737 else
6738 {
6739 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6740 if (attr != NULL)
6741 {
6742 /* Value of the DW_AT_ranges attribute is the offset in the
6743 .debug_ranges section. */
6744 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6745 return 0;
6746 /* Found discontinuous range of addresses. */
6747 ret = -1;
6748 }
6749 }
6750
6751 /* read_partial_die has also the strict LOW < HIGH requirement. */
6752 if (high <= low)
6753 return 0;
6754
6755 /* When using the GNU linker, .gnu.linkonce. sections are used to
6756 eliminate duplicate copies of functions and vtables and such.
6757 The linker will arbitrarily choose one and discard the others.
6758 The AT_*_pc values for such functions refer to local labels in
6759 these sections. If the section from that file was discarded, the
6760 labels are not in the output, so the relocs get a value of 0.
6761 If this is a discarded function, mark the pc bounds as invalid,
6762 so that GDB will ignore it. */
6763 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6764 return 0;
6765
6766 *lowpc = low;
6767 if (highpc)
6768 *highpc = high;
6769 return ret;
6770 }
6771
6772 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6773 its low and high PC addresses. Do nothing if these addresses could not
6774 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6775 and HIGHPC to the high address if greater than HIGHPC. */
6776
6777 static void
6778 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6779 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6780 struct dwarf2_cu *cu)
6781 {
6782 CORE_ADDR low, high;
6783 struct die_info *child = die->child;
6784
6785 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6786 {
6787 *lowpc = min (*lowpc, low);
6788 *highpc = max (*highpc, high);
6789 }
6790
6791 /* If the language does not allow nested subprograms (either inside
6792 subprograms or lexical blocks), we're done. */
6793 if (cu->language != language_ada)
6794 return;
6795
6796 /* Check all the children of the given DIE. If it contains nested
6797 subprograms, then check their pc bounds. Likewise, we need to
6798 check lexical blocks as well, as they may also contain subprogram
6799 definitions. */
6800 while (child && child->tag)
6801 {
6802 if (child->tag == DW_TAG_subprogram
6803 || child->tag == DW_TAG_lexical_block)
6804 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6805 child = sibling_die (child);
6806 }
6807 }
6808
6809 /* Get the low and high pc's represented by the scope DIE, and store
6810 them in *LOWPC and *HIGHPC. If the correct values can't be
6811 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6812
6813 static void
6814 get_scope_pc_bounds (struct die_info *die,
6815 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6816 struct dwarf2_cu *cu)
6817 {
6818 CORE_ADDR best_low = (CORE_ADDR) -1;
6819 CORE_ADDR best_high = (CORE_ADDR) 0;
6820 CORE_ADDR current_low, current_high;
6821
6822 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6823 {
6824 best_low = current_low;
6825 best_high = current_high;
6826 }
6827 else
6828 {
6829 struct die_info *child = die->child;
6830
6831 while (child && child->tag)
6832 {
6833 switch (child->tag) {
6834 case DW_TAG_subprogram:
6835 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6836 break;
6837 case DW_TAG_namespace:
6838 case DW_TAG_module:
6839 /* FIXME: carlton/2004-01-16: Should we do this for
6840 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6841 that current GCC's always emit the DIEs corresponding
6842 to definitions of methods of classes as children of a
6843 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6844 the DIEs giving the declarations, which could be
6845 anywhere). But I don't see any reason why the
6846 standards says that they have to be there. */
6847 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6848
6849 if (current_low != ((CORE_ADDR) -1))
6850 {
6851 best_low = min (best_low, current_low);
6852 best_high = max (best_high, current_high);
6853 }
6854 break;
6855 default:
6856 /* Ignore. */
6857 break;
6858 }
6859
6860 child = sibling_die (child);
6861 }
6862 }
6863
6864 *lowpc = best_low;
6865 *highpc = best_high;
6866 }
6867
6868 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6869 in DIE. */
6870
6871 static void
6872 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6873 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6874 {
6875 struct objfile *objfile = cu->objfile;
6876 struct attribute *attr;
6877 struct attribute *attr_high;
6878
6879 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
6880 if (attr_high)
6881 {
6882 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6883 if (attr)
6884 {
6885 CORE_ADDR low = DW_ADDR (attr);
6886 CORE_ADDR high;
6887 if (attr_high->form == DW_FORM_addr)
6888 high = DW_ADDR (attr_high);
6889 else
6890 high = low + DW_UNSND (attr_high);
6891
6892 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6893 }
6894 }
6895
6896 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6897 if (attr)
6898 {
6899 bfd *obfd = objfile->obfd;
6900
6901 /* The value of the DW_AT_ranges attribute is the offset of the
6902 address range list in the .debug_ranges section. */
6903 unsigned long offset = DW_UNSND (attr);
6904 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6905
6906 /* For some target architectures, but not others, the
6907 read_address function sign-extends the addresses it returns.
6908 To recognize base address selection entries, we need a
6909 mask. */
6910 unsigned int addr_size = cu->header.addr_size;
6911 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6912
6913 /* The base address, to which the next pair is relative. Note
6914 that this 'base' is a DWARF concept: most entries in a range
6915 list are relative, to reduce the number of relocs against the
6916 debugging information. This is separate from this function's
6917 'baseaddr' argument, which GDB uses to relocate debugging
6918 information from a shared library based on the address at
6919 which the library was loaded. */
6920 CORE_ADDR base = cu->base_address;
6921 int base_known = cu->base_known;
6922
6923 gdb_assert (dwarf2_per_objfile->ranges.readin);
6924 if (offset >= dwarf2_per_objfile->ranges.size)
6925 {
6926 complaint (&symfile_complaints,
6927 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6928 offset);
6929 return;
6930 }
6931
6932 for (;;)
6933 {
6934 unsigned int bytes_read;
6935 CORE_ADDR start, end;
6936
6937 start = read_address (obfd, buffer, cu, &bytes_read);
6938 buffer += bytes_read;
6939 end = read_address (obfd, buffer, cu, &bytes_read);
6940 buffer += bytes_read;
6941
6942 /* Did we find the end of the range list? */
6943 if (start == 0 && end == 0)
6944 break;
6945
6946 /* Did we find a base address selection entry? */
6947 else if ((start & base_select_mask) == base_select_mask)
6948 {
6949 base = end;
6950 base_known = 1;
6951 }
6952
6953 /* We found an ordinary address range. */
6954 else
6955 {
6956 if (!base_known)
6957 {
6958 complaint (&symfile_complaints,
6959 _("Invalid .debug_ranges data "
6960 "(no base address)"));
6961 return;
6962 }
6963
6964 if (start > end)
6965 {
6966 /* Inverted range entries are invalid. */
6967 complaint (&symfile_complaints,
6968 _("Invalid .debug_ranges data "
6969 "(inverted range)"));
6970 return;
6971 }
6972
6973 /* Empty range entries have no effect. */
6974 if (start == end)
6975 continue;
6976
6977 record_block_range (block,
6978 baseaddr + base + start,
6979 baseaddr + base + end - 1);
6980 }
6981 }
6982 }
6983 }
6984
6985 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6986 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6987 during 4.6.0 experimental. */
6988
6989 static int
6990 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6991 {
6992 const char *cs;
6993 int major, minor, release;
6994 int result = 0;
6995
6996 if (cu->producer == NULL)
6997 {
6998 /* For unknown compilers expect their behavior is DWARF version
6999 compliant.
7000
7001 GCC started to support .debug_types sections by -gdwarf-4 since
7002 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
7003 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
7004 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
7005 interpreted incorrectly by GDB now - GCC PR debug/48229. */
7006
7007 return 0;
7008 }
7009
7010 if (cu->checked_producer)
7011 return cu->producer_is_gxx_lt_4_6;
7012
7013 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
7014
7015 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
7016 {
7017 /* For non-GCC compilers expect their behavior is DWARF version
7018 compliant. */
7019 }
7020 else
7021 {
7022 cs = &cu->producer[strlen ("GNU ")];
7023 while (*cs && !isdigit (*cs))
7024 cs++;
7025 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
7026 {
7027 /* Not recognized as GCC. */
7028 }
7029 else
7030 result = major < 4 || (major == 4 && minor < 6);
7031 }
7032
7033 cu->checked_producer = 1;
7034 cu->producer_is_gxx_lt_4_6 = result;
7035
7036 return result;
7037 }
7038
7039 /* Return the default accessibility type if it is not overriden by
7040 DW_AT_accessibility. */
7041
7042 static enum dwarf_access_attribute
7043 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
7044 {
7045 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
7046 {
7047 /* The default DWARF 2 accessibility for members is public, the default
7048 accessibility for inheritance is private. */
7049
7050 if (die->tag != DW_TAG_inheritance)
7051 return DW_ACCESS_public;
7052 else
7053 return DW_ACCESS_private;
7054 }
7055 else
7056 {
7057 /* DWARF 3+ defines the default accessibility a different way. The same
7058 rules apply now for DW_TAG_inheritance as for the members and it only
7059 depends on the container kind. */
7060
7061 if (die->parent->tag == DW_TAG_class_type)
7062 return DW_ACCESS_private;
7063 else
7064 return DW_ACCESS_public;
7065 }
7066 }
7067
7068 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
7069 offset. If the attribute was not found return 0, otherwise return
7070 1. If it was found but could not properly be handled, set *OFFSET
7071 to 0. */
7072
7073 static int
7074 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
7075 LONGEST *offset)
7076 {
7077 struct attribute *attr;
7078
7079 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
7080 if (attr != NULL)
7081 {
7082 *offset = 0;
7083
7084 /* Note that we do not check for a section offset first here.
7085 This is because DW_AT_data_member_location is new in DWARF 4,
7086 so if we see it, we can assume that a constant form is really
7087 a constant and not a section offset. */
7088 if (attr_form_is_constant (attr))
7089 *offset = dwarf2_get_attr_constant_value (attr, 0);
7090 else if (attr_form_is_section_offset (attr))
7091 dwarf2_complex_location_expr_complaint ();
7092 else if (attr_form_is_block (attr))
7093 *offset = decode_locdesc (DW_BLOCK (attr), cu);
7094 else
7095 dwarf2_complex_location_expr_complaint ();
7096
7097 return 1;
7098 }
7099
7100 return 0;
7101 }
7102
7103 /* Add an aggregate field to the field list. */
7104
7105 static void
7106 dwarf2_add_field (struct field_info *fip, struct die_info *die,
7107 struct dwarf2_cu *cu)
7108 {
7109 struct objfile *objfile = cu->objfile;
7110 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7111 struct nextfield *new_field;
7112 struct attribute *attr;
7113 struct field *fp;
7114 char *fieldname = "";
7115
7116 /* Allocate a new field list entry and link it in. */
7117 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
7118 make_cleanup (xfree, new_field);
7119 memset (new_field, 0, sizeof (struct nextfield));
7120
7121 if (die->tag == DW_TAG_inheritance)
7122 {
7123 new_field->next = fip->baseclasses;
7124 fip->baseclasses = new_field;
7125 }
7126 else
7127 {
7128 new_field->next = fip->fields;
7129 fip->fields = new_field;
7130 }
7131 fip->nfields++;
7132
7133 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7134 if (attr)
7135 new_field->accessibility = DW_UNSND (attr);
7136 else
7137 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
7138 if (new_field->accessibility != DW_ACCESS_public)
7139 fip->non_public_fields = 1;
7140
7141 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7142 if (attr)
7143 new_field->virtuality = DW_UNSND (attr);
7144 else
7145 new_field->virtuality = DW_VIRTUALITY_none;
7146
7147 fp = &new_field->field;
7148
7149 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7150 {
7151 LONGEST offset;
7152
7153 /* Data member other than a C++ static data member. */
7154
7155 /* Get type of field. */
7156 fp->type = die_type (die, cu);
7157
7158 SET_FIELD_BITPOS (*fp, 0);
7159
7160 /* Get bit size of field (zero if none). */
7161 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7162 if (attr)
7163 {
7164 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7165 }
7166 else
7167 {
7168 FIELD_BITSIZE (*fp) = 0;
7169 }
7170
7171 /* Get bit offset of field. */
7172 if (handle_data_member_location (die, cu, &offset))
7173 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7174 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7175 if (attr)
7176 {
7177 if (gdbarch_bits_big_endian (gdbarch))
7178 {
7179 /* For big endian bits, the DW_AT_bit_offset gives the
7180 additional bit offset from the MSB of the containing
7181 anonymous object to the MSB of the field. We don't
7182 have to do anything special since we don't need to
7183 know the size of the anonymous object. */
7184 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
7185 }
7186 else
7187 {
7188 /* For little endian bits, compute the bit offset to the
7189 MSB of the anonymous object, subtract off the number of
7190 bits from the MSB of the field to the MSB of the
7191 object, and then subtract off the number of bits of
7192 the field itself. The result is the bit offset of
7193 the LSB of the field. */
7194 int anonymous_size;
7195 int bit_offset = DW_UNSND (attr);
7196
7197 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7198 if (attr)
7199 {
7200 /* The size of the anonymous object containing
7201 the bit field is explicit, so use the
7202 indicated size (in bytes). */
7203 anonymous_size = DW_UNSND (attr);
7204 }
7205 else
7206 {
7207 /* The size of the anonymous object containing
7208 the bit field must be inferred from the type
7209 attribute of the data member containing the
7210 bit field. */
7211 anonymous_size = TYPE_LENGTH (fp->type);
7212 }
7213 SET_FIELD_BITPOS (*fp,
7214 (FIELD_BITPOS (*fp)
7215 + anonymous_size * bits_per_byte
7216 - bit_offset - FIELD_BITSIZE (*fp)));
7217 }
7218 }
7219
7220 /* Get name of field. */
7221 fieldname = dwarf2_name (die, cu);
7222 if (fieldname == NULL)
7223 fieldname = "";
7224
7225 /* The name is already allocated along with this objfile, so we don't
7226 need to duplicate it for the type. */
7227 fp->name = fieldname;
7228
7229 /* Change accessibility for artificial fields (e.g. virtual table
7230 pointer or virtual base class pointer) to private. */
7231 if (dwarf2_attr (die, DW_AT_artificial, cu))
7232 {
7233 FIELD_ARTIFICIAL (*fp) = 1;
7234 new_field->accessibility = DW_ACCESS_private;
7235 fip->non_public_fields = 1;
7236 }
7237 }
7238 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7239 {
7240 /* C++ static member. */
7241
7242 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7243 is a declaration, but all versions of G++ as of this writing
7244 (so through at least 3.2.1) incorrectly generate
7245 DW_TAG_variable tags. */
7246
7247 const char *physname;
7248
7249 /* Get name of field. */
7250 fieldname = dwarf2_name (die, cu);
7251 if (fieldname == NULL)
7252 return;
7253
7254 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7255 if (attr
7256 /* Only create a symbol if this is an external value.
7257 new_symbol checks this and puts the value in the global symbol
7258 table, which we want. If it is not external, new_symbol
7259 will try to put the value in cu->list_in_scope which is wrong. */
7260 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7261 {
7262 /* A static const member, not much different than an enum as far as
7263 we're concerned, except that we can support more types. */
7264 new_symbol (die, NULL, cu);
7265 }
7266
7267 /* Get physical name. */
7268 physname = dwarf2_physname (fieldname, die, cu);
7269
7270 /* The name is already allocated along with this objfile, so we don't
7271 need to duplicate it for the type. */
7272 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7273 FIELD_TYPE (*fp) = die_type (die, cu);
7274 FIELD_NAME (*fp) = fieldname;
7275 }
7276 else if (die->tag == DW_TAG_inheritance)
7277 {
7278 LONGEST offset;
7279
7280 /* C++ base class field. */
7281 if (handle_data_member_location (die, cu, &offset))
7282 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7283 FIELD_BITSIZE (*fp) = 0;
7284 FIELD_TYPE (*fp) = die_type (die, cu);
7285 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7286 fip->nbaseclasses++;
7287 }
7288 }
7289
7290 /* Add a typedef defined in the scope of the FIP's class. */
7291
7292 static void
7293 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7294 struct dwarf2_cu *cu)
7295 {
7296 struct objfile *objfile = cu->objfile;
7297 struct typedef_field_list *new_field;
7298 struct attribute *attr;
7299 struct typedef_field *fp;
7300 char *fieldname = "";
7301
7302 /* Allocate a new field list entry and link it in. */
7303 new_field = xzalloc (sizeof (*new_field));
7304 make_cleanup (xfree, new_field);
7305
7306 gdb_assert (die->tag == DW_TAG_typedef);
7307
7308 fp = &new_field->field;
7309
7310 /* Get name of field. */
7311 fp->name = dwarf2_name (die, cu);
7312 if (fp->name == NULL)
7313 return;
7314
7315 fp->type = read_type_die (die, cu);
7316
7317 new_field->next = fip->typedef_field_list;
7318 fip->typedef_field_list = new_field;
7319 fip->typedef_field_list_count++;
7320 }
7321
7322 /* Create the vector of fields, and attach it to the type. */
7323
7324 static void
7325 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7326 struct dwarf2_cu *cu)
7327 {
7328 int nfields = fip->nfields;
7329
7330 /* Record the field count, allocate space for the array of fields,
7331 and create blank accessibility bitfields if necessary. */
7332 TYPE_NFIELDS (type) = nfields;
7333 TYPE_FIELDS (type) = (struct field *)
7334 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7335 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7336
7337 if (fip->non_public_fields && cu->language != language_ada)
7338 {
7339 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7340
7341 TYPE_FIELD_PRIVATE_BITS (type) =
7342 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7343 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7344
7345 TYPE_FIELD_PROTECTED_BITS (type) =
7346 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7347 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7348
7349 TYPE_FIELD_IGNORE_BITS (type) =
7350 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7351 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7352 }
7353
7354 /* If the type has baseclasses, allocate and clear a bit vector for
7355 TYPE_FIELD_VIRTUAL_BITS. */
7356 if (fip->nbaseclasses && cu->language != language_ada)
7357 {
7358 int num_bytes = B_BYTES (fip->nbaseclasses);
7359 unsigned char *pointer;
7360
7361 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7362 pointer = TYPE_ALLOC (type, num_bytes);
7363 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7364 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7365 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7366 }
7367
7368 /* Copy the saved-up fields into the field vector. Start from the head of
7369 the list, adding to the tail of the field array, so that they end up in
7370 the same order in the array in which they were added to the list. */
7371 while (nfields-- > 0)
7372 {
7373 struct nextfield *fieldp;
7374
7375 if (fip->fields)
7376 {
7377 fieldp = fip->fields;
7378 fip->fields = fieldp->next;
7379 }
7380 else
7381 {
7382 fieldp = fip->baseclasses;
7383 fip->baseclasses = fieldp->next;
7384 }
7385
7386 TYPE_FIELD (type, nfields) = fieldp->field;
7387 switch (fieldp->accessibility)
7388 {
7389 case DW_ACCESS_private:
7390 if (cu->language != language_ada)
7391 SET_TYPE_FIELD_PRIVATE (type, nfields);
7392 break;
7393
7394 case DW_ACCESS_protected:
7395 if (cu->language != language_ada)
7396 SET_TYPE_FIELD_PROTECTED (type, nfields);
7397 break;
7398
7399 case DW_ACCESS_public:
7400 break;
7401
7402 default:
7403 /* Unknown accessibility. Complain and treat it as public. */
7404 {
7405 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7406 fieldp->accessibility);
7407 }
7408 break;
7409 }
7410 if (nfields < fip->nbaseclasses)
7411 {
7412 switch (fieldp->virtuality)
7413 {
7414 case DW_VIRTUALITY_virtual:
7415 case DW_VIRTUALITY_pure_virtual:
7416 if (cu->language == language_ada)
7417 error (_("unexpected virtuality in component of Ada type"));
7418 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7419 break;
7420 }
7421 }
7422 }
7423 }
7424
7425 /* Add a member function to the proper fieldlist. */
7426
7427 static void
7428 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7429 struct type *type, struct dwarf2_cu *cu)
7430 {
7431 struct objfile *objfile = cu->objfile;
7432 struct attribute *attr;
7433 struct fnfieldlist *flp;
7434 int i;
7435 struct fn_field *fnp;
7436 char *fieldname;
7437 struct nextfnfield *new_fnfield;
7438 struct type *this_type;
7439 enum dwarf_access_attribute accessibility;
7440
7441 if (cu->language == language_ada)
7442 error (_("unexpected member function in Ada type"));
7443
7444 /* Get name of member function. */
7445 fieldname = dwarf2_name (die, cu);
7446 if (fieldname == NULL)
7447 return;
7448
7449 /* Look up member function name in fieldlist. */
7450 for (i = 0; i < fip->nfnfields; i++)
7451 {
7452 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7453 break;
7454 }
7455
7456 /* Create new list element if necessary. */
7457 if (i < fip->nfnfields)
7458 flp = &fip->fnfieldlists[i];
7459 else
7460 {
7461 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7462 {
7463 fip->fnfieldlists = (struct fnfieldlist *)
7464 xrealloc (fip->fnfieldlists,
7465 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7466 * sizeof (struct fnfieldlist));
7467 if (fip->nfnfields == 0)
7468 make_cleanup (free_current_contents, &fip->fnfieldlists);
7469 }
7470 flp = &fip->fnfieldlists[fip->nfnfields];
7471 flp->name = fieldname;
7472 flp->length = 0;
7473 flp->head = NULL;
7474 i = fip->nfnfields++;
7475 }
7476
7477 /* Create a new member function field and chain it to the field list
7478 entry. */
7479 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7480 make_cleanup (xfree, new_fnfield);
7481 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7482 new_fnfield->next = flp->head;
7483 flp->head = new_fnfield;
7484 flp->length++;
7485
7486 /* Fill in the member function field info. */
7487 fnp = &new_fnfield->fnfield;
7488
7489 /* Delay processing of the physname until later. */
7490 if (cu->language == language_cplus || cu->language == language_java)
7491 {
7492 add_to_method_list (type, i, flp->length - 1, fieldname,
7493 die, cu);
7494 }
7495 else
7496 {
7497 const char *physname = dwarf2_physname (fieldname, die, cu);
7498 fnp->physname = physname ? physname : "";
7499 }
7500
7501 fnp->type = alloc_type (objfile);
7502 this_type = read_type_die (die, cu);
7503 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7504 {
7505 int nparams = TYPE_NFIELDS (this_type);
7506
7507 /* TYPE is the domain of this method, and THIS_TYPE is the type
7508 of the method itself (TYPE_CODE_METHOD). */
7509 smash_to_method_type (fnp->type, type,
7510 TYPE_TARGET_TYPE (this_type),
7511 TYPE_FIELDS (this_type),
7512 TYPE_NFIELDS (this_type),
7513 TYPE_VARARGS (this_type));
7514
7515 /* Handle static member functions.
7516 Dwarf2 has no clean way to discern C++ static and non-static
7517 member functions. G++ helps GDB by marking the first
7518 parameter for non-static member functions (which is the this
7519 pointer) as artificial. We obtain this information from
7520 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7521 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7522 fnp->voffset = VOFFSET_STATIC;
7523 }
7524 else
7525 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7526 dwarf2_full_name (fieldname, die, cu));
7527
7528 /* Get fcontext from DW_AT_containing_type if present. */
7529 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7530 fnp->fcontext = die_containing_type (die, cu);
7531
7532 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7533 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7534
7535 /* Get accessibility. */
7536 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7537 if (attr)
7538 accessibility = DW_UNSND (attr);
7539 else
7540 accessibility = dwarf2_default_access_attribute (die, cu);
7541 switch (accessibility)
7542 {
7543 case DW_ACCESS_private:
7544 fnp->is_private = 1;
7545 break;
7546 case DW_ACCESS_protected:
7547 fnp->is_protected = 1;
7548 break;
7549 }
7550
7551 /* Check for artificial methods. */
7552 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7553 if (attr && DW_UNSND (attr) != 0)
7554 fnp->is_artificial = 1;
7555
7556 /* Get index in virtual function table if it is a virtual member
7557 function. For older versions of GCC, this is an offset in the
7558 appropriate virtual table, as specified by DW_AT_containing_type.
7559 For everyone else, it is an expression to be evaluated relative
7560 to the object address. */
7561
7562 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7563 if (attr)
7564 {
7565 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7566 {
7567 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7568 {
7569 /* Old-style GCC. */
7570 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7571 }
7572 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7573 || (DW_BLOCK (attr)->size > 1
7574 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7575 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7576 {
7577 struct dwarf_block blk;
7578 int offset;
7579
7580 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7581 ? 1 : 2);
7582 blk.size = DW_BLOCK (attr)->size - offset;
7583 blk.data = DW_BLOCK (attr)->data + offset;
7584 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7585 if ((fnp->voffset % cu->header.addr_size) != 0)
7586 dwarf2_complex_location_expr_complaint ();
7587 else
7588 fnp->voffset /= cu->header.addr_size;
7589 fnp->voffset += 2;
7590 }
7591 else
7592 dwarf2_complex_location_expr_complaint ();
7593
7594 if (!fnp->fcontext)
7595 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7596 }
7597 else if (attr_form_is_section_offset (attr))
7598 {
7599 dwarf2_complex_location_expr_complaint ();
7600 }
7601 else
7602 {
7603 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7604 fieldname);
7605 }
7606 }
7607 else
7608 {
7609 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7610 if (attr && DW_UNSND (attr))
7611 {
7612 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7613 complaint (&symfile_complaints,
7614 _("Member function \"%s\" (offset %d) is virtual "
7615 "but the vtable offset is not specified"),
7616 fieldname, die->offset.sect_off);
7617 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7618 TYPE_CPLUS_DYNAMIC (type) = 1;
7619 }
7620 }
7621 }
7622
7623 /* Create the vector of member function fields, and attach it to the type. */
7624
7625 static void
7626 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7627 struct dwarf2_cu *cu)
7628 {
7629 struct fnfieldlist *flp;
7630 int i;
7631
7632 if (cu->language == language_ada)
7633 error (_("unexpected member functions in Ada type"));
7634
7635 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7636 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7637 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7638
7639 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7640 {
7641 struct nextfnfield *nfp = flp->head;
7642 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7643 int k;
7644
7645 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7646 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7647 fn_flp->fn_fields = (struct fn_field *)
7648 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7649 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7650 fn_flp->fn_fields[k] = nfp->fnfield;
7651 }
7652
7653 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7654 }
7655
7656 /* Returns non-zero if NAME is the name of a vtable member in CU's
7657 language, zero otherwise. */
7658 static int
7659 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7660 {
7661 static const char vptr[] = "_vptr";
7662 static const char vtable[] = "vtable";
7663
7664 /* Look for the C++ and Java forms of the vtable. */
7665 if ((cu->language == language_java
7666 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7667 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7668 && is_cplus_marker (name[sizeof (vptr) - 1])))
7669 return 1;
7670
7671 return 0;
7672 }
7673
7674 /* GCC outputs unnamed structures that are really pointers to member
7675 functions, with the ABI-specified layout. If TYPE describes
7676 such a structure, smash it into a member function type.
7677
7678 GCC shouldn't do this; it should just output pointer to member DIEs.
7679 This is GCC PR debug/28767. */
7680
7681 static void
7682 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7683 {
7684 struct type *pfn_type, *domain_type, *new_type;
7685
7686 /* Check for a structure with no name and two children. */
7687 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7688 return;
7689
7690 /* Check for __pfn and __delta members. */
7691 if (TYPE_FIELD_NAME (type, 0) == NULL
7692 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7693 || TYPE_FIELD_NAME (type, 1) == NULL
7694 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7695 return;
7696
7697 /* Find the type of the method. */
7698 pfn_type = TYPE_FIELD_TYPE (type, 0);
7699 if (pfn_type == NULL
7700 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7701 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7702 return;
7703
7704 /* Look for the "this" argument. */
7705 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7706 if (TYPE_NFIELDS (pfn_type) == 0
7707 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7708 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7709 return;
7710
7711 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7712 new_type = alloc_type (objfile);
7713 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7714 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7715 TYPE_VARARGS (pfn_type));
7716 smash_to_methodptr_type (type, new_type);
7717 }
7718
7719 /* Called when we find the DIE that starts a structure or union scope
7720 (definition) to create a type for the structure or union. Fill in
7721 the type's name and general properties; the members will not be
7722 processed until process_structure_type.
7723
7724 NOTE: we need to call these functions regardless of whether or not the
7725 DIE has a DW_AT_name attribute, since it might be an anonymous
7726 structure or union. This gets the type entered into our set of
7727 user defined types.
7728
7729 However, if the structure is incomplete (an opaque struct/union)
7730 then suppress creating a symbol table entry for it since gdb only
7731 wants to find the one with the complete definition. Note that if
7732 it is complete, we just call new_symbol, which does it's own
7733 checking about whether the struct/union is anonymous or not (and
7734 suppresses creating a symbol table entry itself). */
7735
7736 static struct type *
7737 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7738 {
7739 struct objfile *objfile = cu->objfile;
7740 struct type *type;
7741 struct attribute *attr;
7742 char *name;
7743
7744 /* If the definition of this type lives in .debug_types, read that type.
7745 Don't follow DW_AT_specification though, that will take us back up
7746 the chain and we want to go down. */
7747 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7748 if (attr)
7749 {
7750 struct dwarf2_cu *type_cu = cu;
7751 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7752
7753 /* We could just recurse on read_structure_type, but we need to call
7754 get_die_type to ensure only one type for this DIE is created.
7755 This is important, for example, because for c++ classes we need
7756 TYPE_NAME set which is only done by new_symbol. Blech. */
7757 type = read_type_die (type_die, type_cu);
7758
7759 /* TYPE_CU may not be the same as CU.
7760 Ensure TYPE is recorded in CU's type_hash table. */
7761 return set_die_type (die, type, cu);
7762 }
7763
7764 type = alloc_type (objfile);
7765 INIT_CPLUS_SPECIFIC (type);
7766
7767 name = dwarf2_name (die, cu);
7768 if (name != NULL)
7769 {
7770 if (cu->language == language_cplus
7771 || cu->language == language_java)
7772 {
7773 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7774
7775 /* dwarf2_full_name might have already finished building the DIE's
7776 type. If so, there is no need to continue. */
7777 if (get_die_type (die, cu) != NULL)
7778 return get_die_type (die, cu);
7779
7780 TYPE_TAG_NAME (type) = full_name;
7781 if (die->tag == DW_TAG_structure_type
7782 || die->tag == DW_TAG_class_type)
7783 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7784 }
7785 else
7786 {
7787 /* The name is already allocated along with this objfile, so
7788 we don't need to duplicate it for the type. */
7789 TYPE_TAG_NAME (type) = (char *) name;
7790 if (die->tag == DW_TAG_class_type)
7791 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7792 }
7793 }
7794
7795 if (die->tag == DW_TAG_structure_type)
7796 {
7797 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7798 }
7799 else if (die->tag == DW_TAG_union_type)
7800 {
7801 TYPE_CODE (type) = TYPE_CODE_UNION;
7802 }
7803 else
7804 {
7805 TYPE_CODE (type) = TYPE_CODE_CLASS;
7806 }
7807
7808 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7809 TYPE_DECLARED_CLASS (type) = 1;
7810
7811 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7812 if (attr)
7813 {
7814 TYPE_LENGTH (type) = DW_UNSND (attr);
7815 }
7816 else
7817 {
7818 TYPE_LENGTH (type) = 0;
7819 }
7820
7821 TYPE_STUB_SUPPORTED (type) = 1;
7822 if (die_is_declaration (die, cu))
7823 TYPE_STUB (type) = 1;
7824 else if (attr == NULL && die->child == NULL
7825 && producer_is_realview (cu->producer))
7826 /* RealView does not output the required DW_AT_declaration
7827 on incomplete types. */
7828 TYPE_STUB (type) = 1;
7829
7830 /* We need to add the type field to the die immediately so we don't
7831 infinitely recurse when dealing with pointers to the structure
7832 type within the structure itself. */
7833 set_die_type (die, type, cu);
7834
7835 /* set_die_type should be already done. */
7836 set_descriptive_type (type, die, cu);
7837
7838 return type;
7839 }
7840
7841 /* Finish creating a structure or union type, including filling in
7842 its members and creating a symbol for it. */
7843
7844 static void
7845 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7846 {
7847 struct objfile *objfile = cu->objfile;
7848 struct die_info *child_die = die->child;
7849 struct type *type;
7850
7851 type = get_die_type (die, cu);
7852 if (type == NULL)
7853 type = read_structure_type (die, cu);
7854
7855 if (die->child != NULL && ! die_is_declaration (die, cu))
7856 {
7857 struct field_info fi;
7858 struct die_info *child_die;
7859 VEC (symbolp) *template_args = NULL;
7860 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7861
7862 memset (&fi, 0, sizeof (struct field_info));
7863
7864 child_die = die->child;
7865
7866 while (child_die && child_die->tag)
7867 {
7868 if (child_die->tag == DW_TAG_member
7869 || child_die->tag == DW_TAG_variable)
7870 {
7871 /* NOTE: carlton/2002-11-05: A C++ static data member
7872 should be a DW_TAG_member that is a declaration, but
7873 all versions of G++ as of this writing (so through at
7874 least 3.2.1) incorrectly generate DW_TAG_variable
7875 tags for them instead. */
7876 dwarf2_add_field (&fi, child_die, cu);
7877 }
7878 else if (child_die->tag == DW_TAG_subprogram)
7879 {
7880 /* C++ member function. */
7881 dwarf2_add_member_fn (&fi, child_die, type, cu);
7882 }
7883 else if (child_die->tag == DW_TAG_inheritance)
7884 {
7885 /* C++ base class field. */
7886 dwarf2_add_field (&fi, child_die, cu);
7887 }
7888 else if (child_die->tag == DW_TAG_typedef)
7889 dwarf2_add_typedef (&fi, child_die, cu);
7890 else if (child_die->tag == DW_TAG_template_type_param
7891 || child_die->tag == DW_TAG_template_value_param)
7892 {
7893 struct symbol *arg = new_symbol (child_die, NULL, cu);
7894
7895 if (arg != NULL)
7896 VEC_safe_push (symbolp, template_args, arg);
7897 }
7898
7899 child_die = sibling_die (child_die);
7900 }
7901
7902 /* Attach template arguments to type. */
7903 if (! VEC_empty (symbolp, template_args))
7904 {
7905 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7906 TYPE_N_TEMPLATE_ARGUMENTS (type)
7907 = VEC_length (symbolp, template_args);
7908 TYPE_TEMPLATE_ARGUMENTS (type)
7909 = obstack_alloc (&objfile->objfile_obstack,
7910 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7911 * sizeof (struct symbol *)));
7912 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7913 VEC_address (symbolp, template_args),
7914 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7915 * sizeof (struct symbol *)));
7916 VEC_free (symbolp, template_args);
7917 }
7918
7919 /* Attach fields and member functions to the type. */
7920 if (fi.nfields)
7921 dwarf2_attach_fields_to_type (&fi, type, cu);
7922 if (fi.nfnfields)
7923 {
7924 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7925
7926 /* Get the type which refers to the base class (possibly this
7927 class itself) which contains the vtable pointer for the current
7928 class from the DW_AT_containing_type attribute. This use of
7929 DW_AT_containing_type is a GNU extension. */
7930
7931 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7932 {
7933 struct type *t = die_containing_type (die, cu);
7934
7935 TYPE_VPTR_BASETYPE (type) = t;
7936 if (type == t)
7937 {
7938 int i;
7939
7940 /* Our own class provides vtbl ptr. */
7941 for (i = TYPE_NFIELDS (t) - 1;
7942 i >= TYPE_N_BASECLASSES (t);
7943 --i)
7944 {
7945 const char *fieldname = TYPE_FIELD_NAME (t, i);
7946
7947 if (is_vtable_name (fieldname, cu))
7948 {
7949 TYPE_VPTR_FIELDNO (type) = i;
7950 break;
7951 }
7952 }
7953
7954 /* Complain if virtual function table field not found. */
7955 if (i < TYPE_N_BASECLASSES (t))
7956 complaint (&symfile_complaints,
7957 _("virtual function table pointer "
7958 "not found when defining class '%s'"),
7959 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7960 "");
7961 }
7962 else
7963 {
7964 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7965 }
7966 }
7967 else if (cu->producer
7968 && strncmp (cu->producer,
7969 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7970 {
7971 /* The IBM XLC compiler does not provide direct indication
7972 of the containing type, but the vtable pointer is
7973 always named __vfp. */
7974
7975 int i;
7976
7977 for (i = TYPE_NFIELDS (type) - 1;
7978 i >= TYPE_N_BASECLASSES (type);
7979 --i)
7980 {
7981 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7982 {
7983 TYPE_VPTR_FIELDNO (type) = i;
7984 TYPE_VPTR_BASETYPE (type) = type;
7985 break;
7986 }
7987 }
7988 }
7989 }
7990
7991 /* Copy fi.typedef_field_list linked list elements content into the
7992 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7993 if (fi.typedef_field_list)
7994 {
7995 int i = fi.typedef_field_list_count;
7996
7997 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7998 TYPE_TYPEDEF_FIELD_ARRAY (type)
7999 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
8000 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
8001
8002 /* Reverse the list order to keep the debug info elements order. */
8003 while (--i >= 0)
8004 {
8005 struct typedef_field *dest, *src;
8006
8007 dest = &TYPE_TYPEDEF_FIELD (type, i);
8008 src = &fi.typedef_field_list->field;
8009 fi.typedef_field_list = fi.typedef_field_list->next;
8010 *dest = *src;
8011 }
8012 }
8013
8014 do_cleanups (back_to);
8015
8016 if (HAVE_CPLUS_STRUCT (type))
8017 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
8018 }
8019
8020 quirk_gcc_member_function_pointer (type, objfile);
8021
8022 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
8023 snapshots) has been known to create a die giving a declaration
8024 for a class that has, as a child, a die giving a definition for a
8025 nested class. So we have to process our children even if the
8026 current die is a declaration. Normally, of course, a declaration
8027 won't have any children at all. */
8028
8029 while (child_die != NULL && child_die->tag)
8030 {
8031 if (child_die->tag == DW_TAG_member
8032 || child_die->tag == DW_TAG_variable
8033 || child_die->tag == DW_TAG_inheritance
8034 || child_die->tag == DW_TAG_template_value_param
8035 || child_die->tag == DW_TAG_template_type_param)
8036 {
8037 /* Do nothing. */
8038 }
8039 else
8040 process_die (child_die, cu);
8041
8042 child_die = sibling_die (child_die);
8043 }
8044
8045 /* Do not consider external references. According to the DWARF standard,
8046 these DIEs are identified by the fact that they have no byte_size
8047 attribute, and a declaration attribute. */
8048 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
8049 || !die_is_declaration (die, cu))
8050 new_symbol (die, type, cu);
8051 }
8052
8053 /* Given a DW_AT_enumeration_type die, set its type. We do not
8054 complete the type's fields yet, or create any symbols. */
8055
8056 static struct type *
8057 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
8058 {
8059 struct objfile *objfile = cu->objfile;
8060 struct type *type;
8061 struct attribute *attr;
8062 const char *name;
8063
8064 /* If the definition of this type lives in .debug_types, read that type.
8065 Don't follow DW_AT_specification though, that will take us back up
8066 the chain and we want to go down. */
8067 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
8068 if (attr)
8069 {
8070 struct dwarf2_cu *type_cu = cu;
8071 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
8072
8073 type = read_type_die (type_die, type_cu);
8074
8075 /* TYPE_CU may not be the same as CU.
8076 Ensure TYPE is recorded in CU's type_hash table. */
8077 return set_die_type (die, type, cu);
8078 }
8079
8080 type = alloc_type (objfile);
8081
8082 TYPE_CODE (type) = TYPE_CODE_ENUM;
8083 name = dwarf2_full_name (NULL, die, cu);
8084 if (name != NULL)
8085 TYPE_TAG_NAME (type) = (char *) name;
8086
8087 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8088 if (attr)
8089 {
8090 TYPE_LENGTH (type) = DW_UNSND (attr);
8091 }
8092 else
8093 {
8094 TYPE_LENGTH (type) = 0;
8095 }
8096
8097 /* The enumeration DIE can be incomplete. In Ada, any type can be
8098 declared as private in the package spec, and then defined only
8099 inside the package body. Such types are known as Taft Amendment
8100 Types. When another package uses such a type, an incomplete DIE
8101 may be generated by the compiler. */
8102 if (die_is_declaration (die, cu))
8103 TYPE_STUB (type) = 1;
8104
8105 return set_die_type (die, type, cu);
8106 }
8107
8108 /* Given a pointer to a die which begins an enumeration, process all
8109 the dies that define the members of the enumeration, and create the
8110 symbol for the enumeration type.
8111
8112 NOTE: We reverse the order of the element list. */
8113
8114 static void
8115 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
8116 {
8117 struct type *this_type;
8118
8119 this_type = get_die_type (die, cu);
8120 if (this_type == NULL)
8121 this_type = read_enumeration_type (die, cu);
8122
8123 if (die->child != NULL)
8124 {
8125 struct die_info *child_die;
8126 struct symbol *sym;
8127 struct field *fields = NULL;
8128 int num_fields = 0;
8129 int unsigned_enum = 1;
8130 char *name;
8131 int flag_enum = 1;
8132 ULONGEST mask = 0;
8133
8134 child_die = die->child;
8135 while (child_die && child_die->tag)
8136 {
8137 if (child_die->tag != DW_TAG_enumerator)
8138 {
8139 process_die (child_die, cu);
8140 }
8141 else
8142 {
8143 name = dwarf2_name (child_die, cu);
8144 if (name)
8145 {
8146 sym = new_symbol (child_die, this_type, cu);
8147 if (SYMBOL_VALUE (sym) < 0)
8148 {
8149 unsigned_enum = 0;
8150 flag_enum = 0;
8151 }
8152 else if ((mask & SYMBOL_VALUE (sym)) != 0)
8153 flag_enum = 0;
8154 else
8155 mask |= SYMBOL_VALUE (sym);
8156
8157 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8158 {
8159 fields = (struct field *)
8160 xrealloc (fields,
8161 (num_fields + DW_FIELD_ALLOC_CHUNK)
8162 * sizeof (struct field));
8163 }
8164
8165 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8166 FIELD_TYPE (fields[num_fields]) = NULL;
8167 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
8168 FIELD_BITSIZE (fields[num_fields]) = 0;
8169
8170 num_fields++;
8171 }
8172 }
8173
8174 child_die = sibling_die (child_die);
8175 }
8176
8177 if (num_fields)
8178 {
8179 TYPE_NFIELDS (this_type) = num_fields;
8180 TYPE_FIELDS (this_type) = (struct field *)
8181 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8182 memcpy (TYPE_FIELDS (this_type), fields,
8183 sizeof (struct field) * num_fields);
8184 xfree (fields);
8185 }
8186 if (unsigned_enum)
8187 TYPE_UNSIGNED (this_type) = 1;
8188 if (flag_enum)
8189 TYPE_FLAG_ENUM (this_type) = 1;
8190 }
8191
8192 /* If we are reading an enum from a .debug_types unit, and the enum
8193 is a declaration, and the enum is not the signatured type in the
8194 unit, then we do not want to add a symbol for it. Adding a
8195 symbol would in some cases obscure the true definition of the
8196 enum, giving users an incomplete type when the definition is
8197 actually available. Note that we do not want to do this for all
8198 enums which are just declarations, because C++0x allows forward
8199 enum declarations. */
8200 if (cu->per_cu->debug_types_section
8201 && die_is_declaration (die, cu))
8202 {
8203 struct signatured_type *sig_type;
8204
8205 sig_type
8206 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8207 cu->per_cu->debug_types_section,
8208 cu->per_cu->offset);
8209 if (sig_type->per_cu.offset.sect_off + sig_type->type_offset.cu_off
8210 != die->offset.sect_off)
8211 return;
8212 }
8213
8214 new_symbol (die, this_type, cu);
8215 }
8216
8217 /* Extract all information from a DW_TAG_array_type DIE and put it in
8218 the DIE's type field. For now, this only handles one dimensional
8219 arrays. */
8220
8221 static struct type *
8222 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8223 {
8224 struct objfile *objfile = cu->objfile;
8225 struct die_info *child_die;
8226 struct type *type;
8227 struct type *element_type, *range_type, *index_type;
8228 struct type **range_types = NULL;
8229 struct attribute *attr;
8230 int ndim = 0;
8231 struct cleanup *back_to;
8232 char *name;
8233
8234 element_type = die_type (die, cu);
8235
8236 /* The die_type call above may have already set the type for this DIE. */
8237 type = get_die_type (die, cu);
8238 if (type)
8239 return type;
8240
8241 /* Irix 6.2 native cc creates array types without children for
8242 arrays with unspecified length. */
8243 if (die->child == NULL)
8244 {
8245 index_type = objfile_type (objfile)->builtin_int;
8246 range_type = create_range_type (NULL, index_type, 0, -1);
8247 type = create_array_type (NULL, element_type, range_type);
8248 return set_die_type (die, type, cu);
8249 }
8250
8251 back_to = make_cleanup (null_cleanup, NULL);
8252 child_die = die->child;
8253 while (child_die && child_die->tag)
8254 {
8255 if (child_die->tag == DW_TAG_subrange_type)
8256 {
8257 struct type *child_type = read_type_die (child_die, cu);
8258
8259 if (child_type != NULL)
8260 {
8261 /* The range type was succesfully read. Save it for the
8262 array type creation. */
8263 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8264 {
8265 range_types = (struct type **)
8266 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8267 * sizeof (struct type *));
8268 if (ndim == 0)
8269 make_cleanup (free_current_contents, &range_types);
8270 }
8271 range_types[ndim++] = child_type;
8272 }
8273 }
8274 child_die = sibling_die (child_die);
8275 }
8276
8277 /* Dwarf2 dimensions are output from left to right, create the
8278 necessary array types in backwards order. */
8279
8280 type = element_type;
8281
8282 if (read_array_order (die, cu) == DW_ORD_col_major)
8283 {
8284 int i = 0;
8285
8286 while (i < ndim)
8287 type = create_array_type (NULL, type, range_types[i++]);
8288 }
8289 else
8290 {
8291 while (ndim-- > 0)
8292 type = create_array_type (NULL, type, range_types[ndim]);
8293 }
8294
8295 /* Understand Dwarf2 support for vector types (like they occur on
8296 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8297 array type. This is not part of the Dwarf2/3 standard yet, but a
8298 custom vendor extension. The main difference between a regular
8299 array and the vector variant is that vectors are passed by value
8300 to functions. */
8301 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8302 if (attr)
8303 make_vector_type (type);
8304
8305 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8306 implementation may choose to implement triple vectors using this
8307 attribute. */
8308 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8309 if (attr)
8310 {
8311 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8312 TYPE_LENGTH (type) = DW_UNSND (attr);
8313 else
8314 complaint (&symfile_complaints,
8315 _("DW_AT_byte_size for array type smaller "
8316 "than the total size of elements"));
8317 }
8318
8319 name = dwarf2_name (die, cu);
8320 if (name)
8321 TYPE_NAME (type) = name;
8322
8323 /* Install the type in the die. */
8324 set_die_type (die, type, cu);
8325
8326 /* set_die_type should be already done. */
8327 set_descriptive_type (type, die, cu);
8328
8329 do_cleanups (back_to);
8330
8331 return type;
8332 }
8333
8334 static enum dwarf_array_dim_ordering
8335 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8336 {
8337 struct attribute *attr;
8338
8339 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8340
8341 if (attr) return DW_SND (attr);
8342
8343 /* GNU F77 is a special case, as at 08/2004 array type info is the
8344 opposite order to the dwarf2 specification, but data is still
8345 laid out as per normal fortran.
8346
8347 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8348 version checking. */
8349
8350 if (cu->language == language_fortran
8351 && cu->producer && strstr (cu->producer, "GNU F77"))
8352 {
8353 return DW_ORD_row_major;
8354 }
8355
8356 switch (cu->language_defn->la_array_ordering)
8357 {
8358 case array_column_major:
8359 return DW_ORD_col_major;
8360 case array_row_major:
8361 default:
8362 return DW_ORD_row_major;
8363 };
8364 }
8365
8366 /* Extract all information from a DW_TAG_set_type DIE and put it in
8367 the DIE's type field. */
8368
8369 static struct type *
8370 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8371 {
8372 struct type *domain_type, *set_type;
8373 struct attribute *attr;
8374
8375 domain_type = die_type (die, cu);
8376
8377 /* The die_type call above may have already set the type for this DIE. */
8378 set_type = get_die_type (die, cu);
8379 if (set_type)
8380 return set_type;
8381
8382 set_type = create_set_type (NULL, domain_type);
8383
8384 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8385 if (attr)
8386 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8387
8388 return set_die_type (die, set_type, cu);
8389 }
8390
8391 /* First cut: install each common block member as a global variable. */
8392
8393 static void
8394 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8395 {
8396 struct die_info *child_die;
8397 struct attribute *attr;
8398 struct symbol *sym;
8399 CORE_ADDR base = (CORE_ADDR) 0;
8400
8401 attr = dwarf2_attr (die, DW_AT_location, cu);
8402 if (attr)
8403 {
8404 /* Support the .debug_loc offsets. */
8405 if (attr_form_is_block (attr))
8406 {
8407 base = decode_locdesc (DW_BLOCK (attr), cu);
8408 }
8409 else if (attr_form_is_section_offset (attr))
8410 {
8411 dwarf2_complex_location_expr_complaint ();
8412 }
8413 else
8414 {
8415 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8416 "common block member");
8417 }
8418 }
8419 if (die->child != NULL)
8420 {
8421 child_die = die->child;
8422 while (child_die && child_die->tag)
8423 {
8424 LONGEST offset;
8425
8426 sym = new_symbol (child_die, NULL, cu);
8427 if (sym != NULL
8428 && handle_data_member_location (child_die, cu, &offset))
8429 {
8430 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8431 add_symbol_to_list (sym, &global_symbols);
8432 }
8433 child_die = sibling_die (child_die);
8434 }
8435 }
8436 }
8437
8438 /* Create a type for a C++ namespace. */
8439
8440 static struct type *
8441 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8442 {
8443 struct objfile *objfile = cu->objfile;
8444 const char *previous_prefix, *name;
8445 int is_anonymous;
8446 struct type *type;
8447
8448 /* For extensions, reuse the type of the original namespace. */
8449 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8450 {
8451 struct die_info *ext_die;
8452 struct dwarf2_cu *ext_cu = cu;
8453
8454 ext_die = dwarf2_extension (die, &ext_cu);
8455 type = read_type_die (ext_die, ext_cu);
8456
8457 /* EXT_CU may not be the same as CU.
8458 Ensure TYPE is recorded in CU's type_hash table. */
8459 return set_die_type (die, type, cu);
8460 }
8461
8462 name = namespace_name (die, &is_anonymous, cu);
8463
8464 /* Now build the name of the current namespace. */
8465
8466 previous_prefix = determine_prefix (die, cu);
8467 if (previous_prefix[0] != '\0')
8468 name = typename_concat (&objfile->objfile_obstack,
8469 previous_prefix, name, 0, cu);
8470
8471 /* Create the type. */
8472 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8473 objfile);
8474 TYPE_NAME (type) = (char *) name;
8475 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8476
8477 return set_die_type (die, type, cu);
8478 }
8479
8480 /* Read a C++ namespace. */
8481
8482 static void
8483 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8484 {
8485 struct objfile *objfile = cu->objfile;
8486 int is_anonymous;
8487
8488 /* Add a symbol associated to this if we haven't seen the namespace
8489 before. Also, add a using directive if it's an anonymous
8490 namespace. */
8491
8492 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8493 {
8494 struct type *type;
8495
8496 type = read_type_die (die, cu);
8497 new_symbol (die, type, cu);
8498
8499 namespace_name (die, &is_anonymous, cu);
8500 if (is_anonymous)
8501 {
8502 const char *previous_prefix = determine_prefix (die, cu);
8503
8504 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8505 NULL, NULL, &objfile->objfile_obstack);
8506 }
8507 }
8508
8509 if (die->child != NULL)
8510 {
8511 struct die_info *child_die = die->child;
8512
8513 while (child_die && child_die->tag)
8514 {
8515 process_die (child_die, cu);
8516 child_die = sibling_die (child_die);
8517 }
8518 }
8519 }
8520
8521 /* Read a Fortran module as type. This DIE can be only a declaration used for
8522 imported module. Still we need that type as local Fortran "use ... only"
8523 declaration imports depend on the created type in determine_prefix. */
8524
8525 static struct type *
8526 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8527 {
8528 struct objfile *objfile = cu->objfile;
8529 char *module_name;
8530 struct type *type;
8531
8532 module_name = dwarf2_name (die, cu);
8533 if (!module_name)
8534 complaint (&symfile_complaints,
8535 _("DW_TAG_module has no name, offset 0x%x"),
8536 die->offset.sect_off);
8537 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8538
8539 /* determine_prefix uses TYPE_TAG_NAME. */
8540 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8541
8542 return set_die_type (die, type, cu);
8543 }
8544
8545 /* Read a Fortran module. */
8546
8547 static void
8548 read_module (struct die_info *die, struct dwarf2_cu *cu)
8549 {
8550 struct die_info *child_die = die->child;
8551
8552 while (child_die && child_die->tag)
8553 {
8554 process_die (child_die, cu);
8555 child_die = sibling_die (child_die);
8556 }
8557 }
8558
8559 /* Return the name of the namespace represented by DIE. Set
8560 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8561 namespace. */
8562
8563 static const char *
8564 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8565 {
8566 struct die_info *current_die;
8567 const char *name = NULL;
8568
8569 /* Loop through the extensions until we find a name. */
8570
8571 for (current_die = die;
8572 current_die != NULL;
8573 current_die = dwarf2_extension (die, &cu))
8574 {
8575 name = dwarf2_name (current_die, cu);
8576 if (name != NULL)
8577 break;
8578 }
8579
8580 /* Is it an anonymous namespace? */
8581
8582 *is_anonymous = (name == NULL);
8583 if (*is_anonymous)
8584 name = CP_ANONYMOUS_NAMESPACE_STR;
8585
8586 return name;
8587 }
8588
8589 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8590 the user defined type vector. */
8591
8592 static struct type *
8593 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8594 {
8595 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8596 struct comp_unit_head *cu_header = &cu->header;
8597 struct type *type;
8598 struct attribute *attr_byte_size;
8599 struct attribute *attr_address_class;
8600 int byte_size, addr_class;
8601 struct type *target_type;
8602
8603 target_type = die_type (die, cu);
8604
8605 /* The die_type call above may have already set the type for this DIE. */
8606 type = get_die_type (die, cu);
8607 if (type)
8608 return type;
8609
8610 type = lookup_pointer_type (target_type);
8611
8612 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8613 if (attr_byte_size)
8614 byte_size = DW_UNSND (attr_byte_size);
8615 else
8616 byte_size = cu_header->addr_size;
8617
8618 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8619 if (attr_address_class)
8620 addr_class = DW_UNSND (attr_address_class);
8621 else
8622 addr_class = DW_ADDR_none;
8623
8624 /* If the pointer size or address class is different than the
8625 default, create a type variant marked as such and set the
8626 length accordingly. */
8627 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8628 {
8629 if (gdbarch_address_class_type_flags_p (gdbarch))
8630 {
8631 int type_flags;
8632
8633 type_flags = gdbarch_address_class_type_flags
8634 (gdbarch, byte_size, addr_class);
8635 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8636 == 0);
8637 type = make_type_with_address_space (type, type_flags);
8638 }
8639 else if (TYPE_LENGTH (type) != byte_size)
8640 {
8641 complaint (&symfile_complaints,
8642 _("invalid pointer size %d"), byte_size);
8643 }
8644 else
8645 {
8646 /* Should we also complain about unhandled address classes? */
8647 }
8648 }
8649
8650 TYPE_LENGTH (type) = byte_size;
8651 return set_die_type (die, type, cu);
8652 }
8653
8654 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8655 the user defined type vector. */
8656
8657 static struct type *
8658 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8659 {
8660 struct type *type;
8661 struct type *to_type;
8662 struct type *domain;
8663
8664 to_type = die_type (die, cu);
8665 domain = die_containing_type (die, cu);
8666
8667 /* The calls above may have already set the type for this DIE. */
8668 type = get_die_type (die, cu);
8669 if (type)
8670 return type;
8671
8672 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8673 type = lookup_methodptr_type (to_type);
8674 else
8675 type = lookup_memberptr_type (to_type, domain);
8676
8677 return set_die_type (die, type, cu);
8678 }
8679
8680 /* Extract all information from a DW_TAG_reference_type DIE and add to
8681 the user defined type vector. */
8682
8683 static struct type *
8684 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8685 {
8686 struct comp_unit_head *cu_header = &cu->header;
8687 struct type *type, *target_type;
8688 struct attribute *attr;
8689
8690 target_type = die_type (die, cu);
8691
8692 /* The die_type call above may have already set the type for this DIE. */
8693 type = get_die_type (die, cu);
8694 if (type)
8695 return type;
8696
8697 type = lookup_reference_type (target_type);
8698 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8699 if (attr)
8700 {
8701 TYPE_LENGTH (type) = DW_UNSND (attr);
8702 }
8703 else
8704 {
8705 TYPE_LENGTH (type) = cu_header->addr_size;
8706 }
8707 return set_die_type (die, type, cu);
8708 }
8709
8710 static struct type *
8711 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8712 {
8713 struct type *base_type, *cv_type;
8714
8715 base_type = die_type (die, cu);
8716
8717 /* The die_type call above may have already set the type for this DIE. */
8718 cv_type = get_die_type (die, cu);
8719 if (cv_type)
8720 return cv_type;
8721
8722 /* In case the const qualifier is applied to an array type, the element type
8723 is so qualified, not the array type (section 6.7.3 of C99). */
8724 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8725 {
8726 struct type *el_type, *inner_array;
8727
8728 base_type = copy_type (base_type);
8729 inner_array = base_type;
8730
8731 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8732 {
8733 TYPE_TARGET_TYPE (inner_array) =
8734 copy_type (TYPE_TARGET_TYPE (inner_array));
8735 inner_array = TYPE_TARGET_TYPE (inner_array);
8736 }
8737
8738 el_type = TYPE_TARGET_TYPE (inner_array);
8739 TYPE_TARGET_TYPE (inner_array) =
8740 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8741
8742 return set_die_type (die, base_type, cu);
8743 }
8744
8745 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8746 return set_die_type (die, cv_type, cu);
8747 }
8748
8749 static struct type *
8750 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8751 {
8752 struct type *base_type, *cv_type;
8753
8754 base_type = die_type (die, cu);
8755
8756 /* The die_type call above may have already set the type for this DIE. */
8757 cv_type = get_die_type (die, cu);
8758 if (cv_type)
8759 return cv_type;
8760
8761 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8762 return set_die_type (die, cv_type, cu);
8763 }
8764
8765 /* Extract all information from a DW_TAG_string_type DIE and add to
8766 the user defined type vector. It isn't really a user defined type,
8767 but it behaves like one, with other DIE's using an AT_user_def_type
8768 attribute to reference it. */
8769
8770 static struct type *
8771 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8772 {
8773 struct objfile *objfile = cu->objfile;
8774 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8775 struct type *type, *range_type, *index_type, *char_type;
8776 struct attribute *attr;
8777 unsigned int length;
8778
8779 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8780 if (attr)
8781 {
8782 length = DW_UNSND (attr);
8783 }
8784 else
8785 {
8786 /* Check for the DW_AT_byte_size attribute. */
8787 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8788 if (attr)
8789 {
8790 length = DW_UNSND (attr);
8791 }
8792 else
8793 {
8794 length = 1;
8795 }
8796 }
8797
8798 index_type = objfile_type (objfile)->builtin_int;
8799 range_type = create_range_type (NULL, index_type, 1, length);
8800 char_type = language_string_char_type (cu->language_defn, gdbarch);
8801 type = create_string_type (NULL, char_type, range_type);
8802
8803 return set_die_type (die, type, cu);
8804 }
8805
8806 /* Handle DIES due to C code like:
8807
8808 struct foo
8809 {
8810 int (*funcp)(int a, long l);
8811 int b;
8812 };
8813
8814 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8815
8816 static struct type *
8817 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8818 {
8819 struct objfile *objfile = cu->objfile;
8820 struct type *type; /* Type that this function returns. */
8821 struct type *ftype; /* Function that returns above type. */
8822 struct attribute *attr;
8823
8824 type = die_type (die, cu);
8825
8826 /* The die_type call above may have already set the type for this DIE. */
8827 ftype = get_die_type (die, cu);
8828 if (ftype)
8829 return ftype;
8830
8831 ftype = lookup_function_type (type);
8832
8833 /* All functions in C++, Pascal and Java have prototypes. */
8834 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8835 if ((attr && (DW_UNSND (attr) != 0))
8836 || cu->language == language_cplus
8837 || cu->language == language_java
8838 || cu->language == language_pascal)
8839 TYPE_PROTOTYPED (ftype) = 1;
8840 else if (producer_is_realview (cu->producer))
8841 /* RealView does not emit DW_AT_prototyped. We can not
8842 distinguish prototyped and unprototyped functions; default to
8843 prototyped, since that is more common in modern code (and
8844 RealView warns about unprototyped functions). */
8845 TYPE_PROTOTYPED (ftype) = 1;
8846
8847 /* Store the calling convention in the type if it's available in
8848 the subroutine die. Otherwise set the calling convention to
8849 the default value DW_CC_normal. */
8850 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8851 if (attr)
8852 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8853 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8854 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8855 else
8856 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8857
8858 /* We need to add the subroutine type to the die immediately so
8859 we don't infinitely recurse when dealing with parameters
8860 declared as the same subroutine type. */
8861 set_die_type (die, ftype, cu);
8862
8863 if (die->child != NULL)
8864 {
8865 struct type *void_type = objfile_type (objfile)->builtin_void;
8866 struct die_info *child_die;
8867 int nparams, iparams;
8868
8869 /* Count the number of parameters.
8870 FIXME: GDB currently ignores vararg functions, but knows about
8871 vararg member functions. */
8872 nparams = 0;
8873 child_die = die->child;
8874 while (child_die && child_die->tag)
8875 {
8876 if (child_die->tag == DW_TAG_formal_parameter)
8877 nparams++;
8878 else if (child_die->tag == DW_TAG_unspecified_parameters)
8879 TYPE_VARARGS (ftype) = 1;
8880 child_die = sibling_die (child_die);
8881 }
8882
8883 /* Allocate storage for parameters and fill them in. */
8884 TYPE_NFIELDS (ftype) = nparams;
8885 TYPE_FIELDS (ftype) = (struct field *)
8886 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8887
8888 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8889 even if we error out during the parameters reading below. */
8890 for (iparams = 0; iparams < nparams; iparams++)
8891 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8892
8893 iparams = 0;
8894 child_die = die->child;
8895 while (child_die && child_die->tag)
8896 {
8897 if (child_die->tag == DW_TAG_formal_parameter)
8898 {
8899 struct type *arg_type;
8900
8901 /* DWARF version 2 has no clean way to discern C++
8902 static and non-static member functions. G++ helps
8903 GDB by marking the first parameter for non-static
8904 member functions (which is the this pointer) as
8905 artificial. We pass this information to
8906 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8907
8908 DWARF version 3 added DW_AT_object_pointer, which GCC
8909 4.5 does not yet generate. */
8910 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8911 if (attr)
8912 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8913 else
8914 {
8915 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8916
8917 /* GCC/43521: In java, the formal parameter
8918 "this" is sometimes not marked with DW_AT_artificial. */
8919 if (cu->language == language_java)
8920 {
8921 const char *name = dwarf2_name (child_die, cu);
8922
8923 if (name && !strcmp (name, "this"))
8924 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8925 }
8926 }
8927 arg_type = die_type (child_die, cu);
8928
8929 /* RealView does not mark THIS as const, which the testsuite
8930 expects. GCC marks THIS as const in method definitions,
8931 but not in the class specifications (GCC PR 43053). */
8932 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8933 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8934 {
8935 int is_this = 0;
8936 struct dwarf2_cu *arg_cu = cu;
8937 const char *name = dwarf2_name (child_die, cu);
8938
8939 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8940 if (attr)
8941 {
8942 /* If the compiler emits this, use it. */
8943 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8944 is_this = 1;
8945 }
8946 else if (name && strcmp (name, "this") == 0)
8947 /* Function definitions will have the argument names. */
8948 is_this = 1;
8949 else if (name == NULL && iparams == 0)
8950 /* Declarations may not have the names, so like
8951 elsewhere in GDB, assume an artificial first
8952 argument is "this". */
8953 is_this = 1;
8954
8955 if (is_this)
8956 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8957 arg_type, 0);
8958 }
8959
8960 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8961 iparams++;
8962 }
8963 child_die = sibling_die (child_die);
8964 }
8965 }
8966
8967 return ftype;
8968 }
8969
8970 static struct type *
8971 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8972 {
8973 struct objfile *objfile = cu->objfile;
8974 const char *name = NULL;
8975 struct type *this_type, *target_type;
8976
8977 name = dwarf2_full_name (NULL, die, cu);
8978 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8979 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8980 TYPE_NAME (this_type) = (char *) name;
8981 set_die_type (die, this_type, cu);
8982 target_type = die_type (die, cu);
8983 if (target_type != this_type)
8984 TYPE_TARGET_TYPE (this_type) = target_type;
8985 else
8986 {
8987 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8988 spec and cause infinite loops in GDB. */
8989 complaint (&symfile_complaints,
8990 _("Self-referential DW_TAG_typedef "
8991 "- DIE at 0x%x [in module %s]"),
8992 die->offset.sect_off, objfile->name);
8993 TYPE_TARGET_TYPE (this_type) = NULL;
8994 }
8995 return this_type;
8996 }
8997
8998 /* Find a representation of a given base type and install
8999 it in the TYPE field of the die. */
9000
9001 static struct type *
9002 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
9003 {
9004 struct objfile *objfile = cu->objfile;
9005 struct type *type;
9006 struct attribute *attr;
9007 int encoding = 0, size = 0;
9008 char *name;
9009 enum type_code code = TYPE_CODE_INT;
9010 int type_flags = 0;
9011 struct type *target_type = NULL;
9012
9013 attr = dwarf2_attr (die, DW_AT_encoding, cu);
9014 if (attr)
9015 {
9016 encoding = DW_UNSND (attr);
9017 }
9018 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9019 if (attr)
9020 {
9021 size = DW_UNSND (attr);
9022 }
9023 name = dwarf2_name (die, cu);
9024 if (!name)
9025 {
9026 complaint (&symfile_complaints,
9027 _("DW_AT_name missing from DW_TAG_base_type"));
9028 }
9029
9030 switch (encoding)
9031 {
9032 case DW_ATE_address:
9033 /* Turn DW_ATE_address into a void * pointer. */
9034 code = TYPE_CODE_PTR;
9035 type_flags |= TYPE_FLAG_UNSIGNED;
9036 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
9037 break;
9038 case DW_ATE_boolean:
9039 code = TYPE_CODE_BOOL;
9040 type_flags |= TYPE_FLAG_UNSIGNED;
9041 break;
9042 case DW_ATE_complex_float:
9043 code = TYPE_CODE_COMPLEX;
9044 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
9045 break;
9046 case DW_ATE_decimal_float:
9047 code = TYPE_CODE_DECFLOAT;
9048 break;
9049 case DW_ATE_float:
9050 code = TYPE_CODE_FLT;
9051 break;
9052 case DW_ATE_signed:
9053 break;
9054 case DW_ATE_unsigned:
9055 type_flags |= TYPE_FLAG_UNSIGNED;
9056 if (cu->language == language_fortran
9057 && name
9058 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
9059 code = TYPE_CODE_CHAR;
9060 break;
9061 case DW_ATE_signed_char:
9062 if (cu->language == language_ada || cu->language == language_m2
9063 || cu->language == language_pascal
9064 || cu->language == language_fortran)
9065 code = TYPE_CODE_CHAR;
9066 break;
9067 case DW_ATE_unsigned_char:
9068 if (cu->language == language_ada || cu->language == language_m2
9069 || cu->language == language_pascal
9070 || cu->language == language_fortran)
9071 code = TYPE_CODE_CHAR;
9072 type_flags |= TYPE_FLAG_UNSIGNED;
9073 break;
9074 case DW_ATE_UTF:
9075 /* We just treat this as an integer and then recognize the
9076 type by name elsewhere. */
9077 break;
9078
9079 default:
9080 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
9081 dwarf_type_encoding_name (encoding));
9082 break;
9083 }
9084
9085 type = init_type (code, size, type_flags, NULL, objfile);
9086 TYPE_NAME (type) = name;
9087 TYPE_TARGET_TYPE (type) = target_type;
9088
9089 if (name && strcmp (name, "char") == 0)
9090 TYPE_NOSIGN (type) = 1;
9091
9092 return set_die_type (die, type, cu);
9093 }
9094
9095 /* Read the given DW_AT_subrange DIE. */
9096
9097 static struct type *
9098 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
9099 {
9100 struct type *base_type;
9101 struct type *range_type;
9102 struct attribute *attr;
9103 LONGEST low, high;
9104 int low_default_is_valid;
9105 char *name;
9106 LONGEST negative_mask;
9107
9108 base_type = die_type (die, cu);
9109 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
9110 check_typedef (base_type);
9111
9112 /* The die_type call above may have already set the type for this DIE. */
9113 range_type = get_die_type (die, cu);
9114 if (range_type)
9115 return range_type;
9116
9117 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
9118 omitting DW_AT_lower_bound. */
9119 switch (cu->language)
9120 {
9121 case language_c:
9122 case language_cplus:
9123 low = 0;
9124 low_default_is_valid = 1;
9125 break;
9126 case language_fortran:
9127 low = 1;
9128 low_default_is_valid = 1;
9129 break;
9130 case language_d:
9131 case language_java:
9132 case language_objc:
9133 low = 0;
9134 low_default_is_valid = (cu->header.version >= 4);
9135 break;
9136 case language_ada:
9137 case language_m2:
9138 case language_pascal:
9139 low = 1;
9140 low_default_is_valid = (cu->header.version >= 4);
9141 break;
9142 default:
9143 low = 0;
9144 low_default_is_valid = 0;
9145 break;
9146 }
9147
9148 /* FIXME: For variable sized arrays either of these could be
9149 a variable rather than a constant value. We'll allow it,
9150 but we don't know how to handle it. */
9151 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
9152 if (attr)
9153 low = dwarf2_get_attr_constant_value (attr, low);
9154 else if (!low_default_is_valid)
9155 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
9156 "- DIE at 0x%x [in module %s]"),
9157 die->offset.sect_off, cu->objfile->name);
9158
9159 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
9160 if (attr)
9161 {
9162 if (attr_form_is_block (attr) || is_ref_attr (attr))
9163 {
9164 /* GCC encodes arrays with unspecified or dynamic length
9165 with a DW_FORM_block1 attribute or a reference attribute.
9166 FIXME: GDB does not yet know how to handle dynamic
9167 arrays properly, treat them as arrays with unspecified
9168 length for now.
9169
9170 FIXME: jimb/2003-09-22: GDB does not really know
9171 how to handle arrays of unspecified length
9172 either; we just represent them as zero-length
9173 arrays. Choose an appropriate upper bound given
9174 the lower bound we've computed above. */
9175 high = low - 1;
9176 }
9177 else
9178 high = dwarf2_get_attr_constant_value (attr, 1);
9179 }
9180 else
9181 {
9182 attr = dwarf2_attr (die, DW_AT_count, cu);
9183 if (attr)
9184 {
9185 int count = dwarf2_get_attr_constant_value (attr, 1);
9186 high = low + count - 1;
9187 }
9188 else
9189 {
9190 /* Unspecified array length. */
9191 high = low - 1;
9192 }
9193 }
9194
9195 /* Dwarf-2 specifications explicitly allows to create subrange types
9196 without specifying a base type.
9197 In that case, the base type must be set to the type of
9198 the lower bound, upper bound or count, in that order, if any of these
9199 three attributes references an object that has a type.
9200 If no base type is found, the Dwarf-2 specifications say that
9201 a signed integer type of size equal to the size of an address should
9202 be used.
9203 For the following C code: `extern char gdb_int [];'
9204 GCC produces an empty range DIE.
9205 FIXME: muller/2010-05-28: Possible references to object for low bound,
9206 high bound or count are not yet handled by this code. */
9207 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9208 {
9209 struct objfile *objfile = cu->objfile;
9210 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9211 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9212 struct type *int_type = objfile_type (objfile)->builtin_int;
9213
9214 /* Test "int", "long int", and "long long int" objfile types,
9215 and select the first one having a size above or equal to the
9216 architecture address size. */
9217 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9218 base_type = int_type;
9219 else
9220 {
9221 int_type = objfile_type (objfile)->builtin_long;
9222 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9223 base_type = int_type;
9224 else
9225 {
9226 int_type = objfile_type (objfile)->builtin_long_long;
9227 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9228 base_type = int_type;
9229 }
9230 }
9231 }
9232
9233 negative_mask =
9234 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9235 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9236 low |= negative_mask;
9237 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9238 high |= negative_mask;
9239
9240 range_type = create_range_type (NULL, base_type, low, high);
9241
9242 /* Mark arrays with dynamic length at least as an array of unspecified
9243 length. GDB could check the boundary but before it gets implemented at
9244 least allow accessing the array elements. */
9245 if (attr && attr_form_is_block (attr))
9246 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9247
9248 /* Ada expects an empty array on no boundary attributes. */
9249 if (attr == NULL && cu->language != language_ada)
9250 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9251
9252 name = dwarf2_name (die, cu);
9253 if (name)
9254 TYPE_NAME (range_type) = name;
9255
9256 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9257 if (attr)
9258 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9259
9260 set_die_type (die, range_type, cu);
9261
9262 /* set_die_type should be already done. */
9263 set_descriptive_type (range_type, die, cu);
9264
9265 return range_type;
9266 }
9267
9268 static struct type *
9269 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9270 {
9271 struct type *type;
9272
9273 /* For now, we only support the C meaning of an unspecified type: void. */
9274
9275 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9276 TYPE_NAME (type) = dwarf2_name (die, cu);
9277
9278 return set_die_type (die, type, cu);
9279 }
9280
9281 /* Trivial hash function for die_info: the hash value of a DIE
9282 is its offset in .debug_info for this objfile. */
9283
9284 static hashval_t
9285 die_hash (const void *item)
9286 {
9287 const struct die_info *die = item;
9288
9289 return die->offset.sect_off;
9290 }
9291
9292 /* Trivial comparison function for die_info structures: two DIEs
9293 are equal if they have the same offset. */
9294
9295 static int
9296 die_eq (const void *item_lhs, const void *item_rhs)
9297 {
9298 const struct die_info *die_lhs = item_lhs;
9299 const struct die_info *die_rhs = item_rhs;
9300
9301 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
9302 }
9303
9304 /* Read a whole compilation unit into a linked list of dies. */
9305
9306 static struct die_info *
9307 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9308 {
9309 struct die_reader_specs reader_specs;
9310 int read_abbrevs = 0;
9311 struct cleanup *back_to = NULL;
9312 struct die_info *die;
9313
9314 if (cu->dwarf2_abbrevs == NULL)
9315 {
9316 dwarf2_read_abbrevs (cu);
9317 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9318 read_abbrevs = 1;
9319 }
9320
9321 gdb_assert (cu->die_hash == NULL);
9322 cu->die_hash
9323 = htab_create_alloc_ex (cu->header.length / 12,
9324 die_hash,
9325 die_eq,
9326 NULL,
9327 &cu->comp_unit_obstack,
9328 hashtab_obstack_allocate,
9329 dummy_obstack_deallocate);
9330
9331 init_cu_die_reader (&reader_specs, cu);
9332
9333 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9334
9335 if (read_abbrevs)
9336 do_cleanups (back_to);
9337
9338 return die;
9339 }
9340
9341 /* Main entry point for reading a DIE and all children.
9342 Read the DIE and dump it if requested. */
9343
9344 static struct die_info *
9345 read_die_and_children (const struct die_reader_specs *reader,
9346 gdb_byte *info_ptr,
9347 gdb_byte **new_info_ptr,
9348 struct die_info *parent)
9349 {
9350 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9351 new_info_ptr, parent);
9352
9353 if (dwarf2_die_debug)
9354 {
9355 fprintf_unfiltered (gdb_stdlog,
9356 "\nRead die from %s of %s:\n",
9357 (reader->cu->per_cu->debug_types_section
9358 ? ".debug_types"
9359 : ".debug_info"),
9360 reader->abfd->filename);
9361 dump_die (result, dwarf2_die_debug);
9362 }
9363
9364 return result;
9365 }
9366
9367 /* Read a single die and all its descendents. Set the die's sibling
9368 field to NULL; set other fields in the die correctly, and set all
9369 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9370 location of the info_ptr after reading all of those dies. PARENT
9371 is the parent of the die in question. */
9372
9373 static struct die_info *
9374 read_die_and_children_1 (const struct die_reader_specs *reader,
9375 gdb_byte *info_ptr,
9376 gdb_byte **new_info_ptr,
9377 struct die_info *parent)
9378 {
9379 struct die_info *die;
9380 gdb_byte *cur_ptr;
9381 int has_children;
9382
9383 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9384 if (die == NULL)
9385 {
9386 *new_info_ptr = cur_ptr;
9387 return NULL;
9388 }
9389 store_in_ref_table (die, reader->cu);
9390
9391 if (has_children)
9392 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9393 else
9394 {
9395 die->child = NULL;
9396 *new_info_ptr = cur_ptr;
9397 }
9398
9399 die->sibling = NULL;
9400 die->parent = parent;
9401 return die;
9402 }
9403
9404 /* Read a die, all of its descendents, and all of its siblings; set
9405 all of the fields of all of the dies correctly. Arguments are as
9406 in read_die_and_children. */
9407
9408 static struct die_info *
9409 read_die_and_siblings (const struct die_reader_specs *reader,
9410 gdb_byte *info_ptr,
9411 gdb_byte **new_info_ptr,
9412 struct die_info *parent)
9413 {
9414 struct die_info *first_die, *last_sibling;
9415 gdb_byte *cur_ptr;
9416
9417 cur_ptr = info_ptr;
9418 first_die = last_sibling = NULL;
9419
9420 while (1)
9421 {
9422 struct die_info *die
9423 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9424
9425 if (die == NULL)
9426 {
9427 *new_info_ptr = cur_ptr;
9428 return first_die;
9429 }
9430
9431 if (!first_die)
9432 first_die = die;
9433 else
9434 last_sibling->sibling = die;
9435
9436 last_sibling = die;
9437 }
9438 }
9439
9440 /* Read the die from the .debug_info section buffer. Set DIEP to
9441 point to a newly allocated die with its information, except for its
9442 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9443 whether the die has children or not. */
9444
9445 static gdb_byte *
9446 read_full_die (const struct die_reader_specs *reader,
9447 struct die_info **diep, gdb_byte *info_ptr,
9448 int *has_children)
9449 {
9450 unsigned int abbrev_number, bytes_read, i;
9451 sect_offset offset;
9452 struct abbrev_info *abbrev;
9453 struct die_info *die;
9454 struct dwarf2_cu *cu = reader->cu;
9455 bfd *abfd = reader->abfd;
9456
9457 offset.sect_off = info_ptr - reader->buffer;
9458 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9459 info_ptr += bytes_read;
9460 if (!abbrev_number)
9461 {
9462 *diep = NULL;
9463 *has_children = 0;
9464 return info_ptr;
9465 }
9466
9467 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9468 if (!abbrev)
9469 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9470 abbrev_number,
9471 bfd_get_filename (abfd));
9472
9473 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9474 die->offset = offset;
9475 die->tag = abbrev->tag;
9476 die->abbrev = abbrev_number;
9477
9478 die->num_attrs = abbrev->num_attrs;
9479
9480 for (i = 0; i < abbrev->num_attrs; ++i)
9481 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9482 abfd, info_ptr, cu);
9483
9484 *diep = die;
9485 *has_children = abbrev->has_children;
9486 return info_ptr;
9487 }
9488
9489 /* In DWARF version 2, the description of the debugging information is
9490 stored in a separate .debug_abbrev section. Before we read any
9491 dies from a section we read in all abbreviations and install them
9492 in a hash table. This function also sets flags in CU describing
9493 the data found in the abbrev table. */
9494
9495 static void
9496 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9497 {
9498 bfd *abfd = cu->objfile->obfd;
9499 struct comp_unit_head *cu_header = &cu->header;
9500 gdb_byte *abbrev_ptr;
9501 struct abbrev_info *cur_abbrev;
9502 unsigned int abbrev_number, bytes_read, abbrev_name;
9503 unsigned int abbrev_form, hash_number;
9504 struct attr_abbrev *cur_attrs;
9505 unsigned int allocated_attrs;
9506
9507 /* Initialize dwarf2 abbrevs. */
9508 obstack_init (&cu->abbrev_obstack);
9509 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9510 (ABBREV_HASH_SIZE
9511 * sizeof (struct abbrev_info *)));
9512 memset (cu->dwarf2_abbrevs, 0,
9513 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9514
9515 dwarf2_read_section (dwarf2_per_objfile->objfile,
9516 &dwarf2_per_objfile->abbrev);
9517 abbrev_ptr = (dwarf2_per_objfile->abbrev.buffer
9518 + cu_header->abbrev_offset.sect_off);
9519 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9520 abbrev_ptr += bytes_read;
9521
9522 allocated_attrs = ATTR_ALLOC_CHUNK;
9523 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9524
9525 /* Loop until we reach an abbrev number of 0. */
9526 while (abbrev_number)
9527 {
9528 cur_abbrev = dwarf_alloc_abbrev (cu);
9529
9530 /* read in abbrev header */
9531 cur_abbrev->number = abbrev_number;
9532 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9533 abbrev_ptr += bytes_read;
9534 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9535 abbrev_ptr += 1;
9536
9537 /* now read in declarations */
9538 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9539 abbrev_ptr += bytes_read;
9540 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9541 abbrev_ptr += bytes_read;
9542 while (abbrev_name)
9543 {
9544 if (cur_abbrev->num_attrs == allocated_attrs)
9545 {
9546 allocated_attrs += ATTR_ALLOC_CHUNK;
9547 cur_attrs
9548 = xrealloc (cur_attrs, (allocated_attrs
9549 * sizeof (struct attr_abbrev)));
9550 }
9551
9552 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9553 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9554 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9555 abbrev_ptr += bytes_read;
9556 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9557 abbrev_ptr += bytes_read;
9558 }
9559
9560 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9561 (cur_abbrev->num_attrs
9562 * sizeof (struct attr_abbrev)));
9563 memcpy (cur_abbrev->attrs, cur_attrs,
9564 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9565
9566 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9567 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9568 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9569
9570 /* Get next abbreviation.
9571 Under Irix6 the abbreviations for a compilation unit are not
9572 always properly terminated with an abbrev number of 0.
9573 Exit loop if we encounter an abbreviation which we have
9574 already read (which means we are about to read the abbreviations
9575 for the next compile unit) or if the end of the abbreviation
9576 table is reached. */
9577 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9578 >= dwarf2_per_objfile->abbrev.size)
9579 break;
9580 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9581 abbrev_ptr += bytes_read;
9582 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9583 break;
9584 }
9585
9586 xfree (cur_attrs);
9587 }
9588
9589 /* Release the memory used by the abbrev table for a compilation unit. */
9590
9591 static void
9592 dwarf2_free_abbrev_table (void *ptr_to_cu)
9593 {
9594 struct dwarf2_cu *cu = ptr_to_cu;
9595
9596 obstack_free (&cu->abbrev_obstack, NULL);
9597 cu->dwarf2_abbrevs = NULL;
9598 }
9599
9600 /* Lookup an abbrev_info structure in the abbrev hash table. */
9601
9602 static struct abbrev_info *
9603 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9604 {
9605 unsigned int hash_number;
9606 struct abbrev_info *abbrev;
9607
9608 hash_number = number % ABBREV_HASH_SIZE;
9609 abbrev = cu->dwarf2_abbrevs[hash_number];
9610
9611 while (abbrev)
9612 {
9613 if (abbrev->number == number)
9614 return abbrev;
9615 else
9616 abbrev = abbrev->next;
9617 }
9618 return NULL;
9619 }
9620
9621 /* Returns nonzero if TAG represents a type that we might generate a partial
9622 symbol for. */
9623
9624 static int
9625 is_type_tag_for_partial (int tag)
9626 {
9627 switch (tag)
9628 {
9629 #if 0
9630 /* Some types that would be reasonable to generate partial symbols for,
9631 that we don't at present. */
9632 case DW_TAG_array_type:
9633 case DW_TAG_file_type:
9634 case DW_TAG_ptr_to_member_type:
9635 case DW_TAG_set_type:
9636 case DW_TAG_string_type:
9637 case DW_TAG_subroutine_type:
9638 #endif
9639 case DW_TAG_base_type:
9640 case DW_TAG_class_type:
9641 case DW_TAG_interface_type:
9642 case DW_TAG_enumeration_type:
9643 case DW_TAG_structure_type:
9644 case DW_TAG_subrange_type:
9645 case DW_TAG_typedef:
9646 case DW_TAG_union_type:
9647 return 1;
9648 default:
9649 return 0;
9650 }
9651 }
9652
9653 /* Load all DIEs that are interesting for partial symbols into memory. */
9654
9655 static struct partial_die_info *
9656 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9657 int building_psymtab, struct dwarf2_cu *cu)
9658 {
9659 struct objfile *objfile = cu->objfile;
9660 struct partial_die_info *part_die;
9661 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9662 struct abbrev_info *abbrev;
9663 unsigned int bytes_read;
9664 unsigned int load_all = 0;
9665 int nesting_level = 1;
9666
9667 parent_die = NULL;
9668 last_die = NULL;
9669
9670 gdb_assert (cu->per_cu != NULL);
9671 if (cu->per_cu->load_all_dies)
9672 load_all = 1;
9673
9674 cu->partial_dies
9675 = htab_create_alloc_ex (cu->header.length / 12,
9676 partial_die_hash,
9677 partial_die_eq,
9678 NULL,
9679 &cu->comp_unit_obstack,
9680 hashtab_obstack_allocate,
9681 dummy_obstack_deallocate);
9682
9683 part_die = obstack_alloc (&cu->comp_unit_obstack,
9684 sizeof (struct partial_die_info));
9685
9686 while (1)
9687 {
9688 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9689
9690 /* A NULL abbrev means the end of a series of children. */
9691 if (abbrev == NULL)
9692 {
9693 if (--nesting_level == 0)
9694 {
9695 /* PART_DIE was probably the last thing allocated on the
9696 comp_unit_obstack, so we could call obstack_free
9697 here. We don't do that because the waste is small,
9698 and will be cleaned up when we're done with this
9699 compilation unit. This way, we're also more robust
9700 against other users of the comp_unit_obstack. */
9701 return first_die;
9702 }
9703 info_ptr += bytes_read;
9704 last_die = parent_die;
9705 parent_die = parent_die->die_parent;
9706 continue;
9707 }
9708
9709 /* Check for template arguments. We never save these; if
9710 they're seen, we just mark the parent, and go on our way. */
9711 if (parent_die != NULL
9712 && cu->language == language_cplus
9713 && (abbrev->tag == DW_TAG_template_type_param
9714 || abbrev->tag == DW_TAG_template_value_param))
9715 {
9716 parent_die->has_template_arguments = 1;
9717
9718 if (!load_all)
9719 {
9720 /* We don't need a partial DIE for the template argument. */
9721 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9722 cu);
9723 continue;
9724 }
9725 }
9726
9727 /* We only recurse into c++ subprograms looking for template arguments.
9728 Skip their other children. */
9729 if (!load_all
9730 && cu->language == language_cplus
9731 && parent_die != NULL
9732 && parent_die->tag == DW_TAG_subprogram)
9733 {
9734 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9735 continue;
9736 }
9737
9738 /* Check whether this DIE is interesting enough to save. Normally
9739 we would not be interested in members here, but there may be
9740 later variables referencing them via DW_AT_specification (for
9741 static members). */
9742 if (!load_all
9743 && !is_type_tag_for_partial (abbrev->tag)
9744 && abbrev->tag != DW_TAG_constant
9745 && abbrev->tag != DW_TAG_enumerator
9746 && abbrev->tag != DW_TAG_subprogram
9747 && abbrev->tag != DW_TAG_lexical_block
9748 && abbrev->tag != DW_TAG_variable
9749 && abbrev->tag != DW_TAG_namespace
9750 && abbrev->tag != DW_TAG_module
9751 && abbrev->tag != DW_TAG_member)
9752 {
9753 /* Otherwise we skip to the next sibling, if any. */
9754 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9755 continue;
9756 }
9757
9758 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9759 buffer, info_ptr, cu);
9760
9761 /* This two-pass algorithm for processing partial symbols has a
9762 high cost in cache pressure. Thus, handle some simple cases
9763 here which cover the majority of C partial symbols. DIEs
9764 which neither have specification tags in them, nor could have
9765 specification tags elsewhere pointing at them, can simply be
9766 processed and discarded.
9767
9768 This segment is also optional; scan_partial_symbols and
9769 add_partial_symbol will handle these DIEs if we chain
9770 them in normally. When compilers which do not emit large
9771 quantities of duplicate debug information are more common,
9772 this code can probably be removed. */
9773
9774 /* Any complete simple types at the top level (pretty much all
9775 of them, for a language without namespaces), can be processed
9776 directly. */
9777 if (parent_die == NULL
9778 && part_die->has_specification == 0
9779 && part_die->is_declaration == 0
9780 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9781 || part_die->tag == DW_TAG_base_type
9782 || part_die->tag == DW_TAG_subrange_type))
9783 {
9784 if (building_psymtab && part_die->name != NULL)
9785 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9786 VAR_DOMAIN, LOC_TYPEDEF,
9787 &objfile->static_psymbols,
9788 0, (CORE_ADDR) 0, cu->language, objfile);
9789 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9790 continue;
9791 }
9792
9793 /* The exception for DW_TAG_typedef with has_children above is
9794 a workaround of GCC PR debug/47510. In the case of this complaint
9795 type_name_no_tag_or_error will error on such types later.
9796
9797 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9798 it could not find the child DIEs referenced later, this is checked
9799 above. In correct DWARF DW_TAG_typedef should have no children. */
9800
9801 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9802 complaint (&symfile_complaints,
9803 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9804 "- DIE at 0x%x [in module %s]"),
9805 part_die->offset.sect_off, objfile->name);
9806
9807 /* If we're at the second level, and we're an enumerator, and
9808 our parent has no specification (meaning possibly lives in a
9809 namespace elsewhere), then we can add the partial symbol now
9810 instead of queueing it. */
9811 if (part_die->tag == DW_TAG_enumerator
9812 && parent_die != NULL
9813 && parent_die->die_parent == NULL
9814 && parent_die->tag == DW_TAG_enumeration_type
9815 && parent_die->has_specification == 0)
9816 {
9817 if (part_die->name == NULL)
9818 complaint (&symfile_complaints,
9819 _("malformed enumerator DIE ignored"));
9820 else if (building_psymtab)
9821 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9822 VAR_DOMAIN, LOC_CONST,
9823 (cu->language == language_cplus
9824 || cu->language == language_java)
9825 ? &objfile->global_psymbols
9826 : &objfile->static_psymbols,
9827 0, (CORE_ADDR) 0, cu->language, objfile);
9828
9829 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9830 continue;
9831 }
9832
9833 /* We'll save this DIE so link it in. */
9834 part_die->die_parent = parent_die;
9835 part_die->die_sibling = NULL;
9836 part_die->die_child = NULL;
9837
9838 if (last_die && last_die == parent_die)
9839 last_die->die_child = part_die;
9840 else if (last_die)
9841 last_die->die_sibling = part_die;
9842
9843 last_die = part_die;
9844
9845 if (first_die == NULL)
9846 first_die = part_die;
9847
9848 /* Maybe add the DIE to the hash table. Not all DIEs that we
9849 find interesting need to be in the hash table, because we
9850 also have the parent/sibling/child chains; only those that we
9851 might refer to by offset later during partial symbol reading.
9852
9853 For now this means things that might have be the target of a
9854 DW_AT_specification, DW_AT_abstract_origin, or
9855 DW_AT_extension. DW_AT_extension will refer only to
9856 namespaces; DW_AT_abstract_origin refers to functions (and
9857 many things under the function DIE, but we do not recurse
9858 into function DIEs during partial symbol reading) and
9859 possibly variables as well; DW_AT_specification refers to
9860 declarations. Declarations ought to have the DW_AT_declaration
9861 flag. It happens that GCC forgets to put it in sometimes, but
9862 only for functions, not for types.
9863
9864 Adding more things than necessary to the hash table is harmless
9865 except for the performance cost. Adding too few will result in
9866 wasted time in find_partial_die, when we reread the compilation
9867 unit with load_all_dies set. */
9868
9869 if (load_all
9870 || abbrev->tag == DW_TAG_constant
9871 || abbrev->tag == DW_TAG_subprogram
9872 || abbrev->tag == DW_TAG_variable
9873 || abbrev->tag == DW_TAG_namespace
9874 || part_die->is_declaration)
9875 {
9876 void **slot;
9877
9878 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9879 part_die->offset.sect_off, INSERT);
9880 *slot = part_die;
9881 }
9882
9883 part_die = obstack_alloc (&cu->comp_unit_obstack,
9884 sizeof (struct partial_die_info));
9885
9886 /* For some DIEs we want to follow their children (if any). For C
9887 we have no reason to follow the children of structures; for other
9888 languages we have to, so that we can get at method physnames
9889 to infer fully qualified class names, for DW_AT_specification,
9890 and for C++ template arguments. For C++, we also look one level
9891 inside functions to find template arguments (if the name of the
9892 function does not already contain the template arguments).
9893
9894 For Ada, we need to scan the children of subprograms and lexical
9895 blocks as well because Ada allows the definition of nested
9896 entities that could be interesting for the debugger, such as
9897 nested subprograms for instance. */
9898 if (last_die->has_children
9899 && (load_all
9900 || last_die->tag == DW_TAG_namespace
9901 || last_die->tag == DW_TAG_module
9902 || last_die->tag == DW_TAG_enumeration_type
9903 || (cu->language == language_cplus
9904 && last_die->tag == DW_TAG_subprogram
9905 && (last_die->name == NULL
9906 || strchr (last_die->name, '<') == NULL))
9907 || (cu->language != language_c
9908 && (last_die->tag == DW_TAG_class_type
9909 || last_die->tag == DW_TAG_interface_type
9910 || last_die->tag == DW_TAG_structure_type
9911 || last_die->tag == DW_TAG_union_type))
9912 || (cu->language == language_ada
9913 && (last_die->tag == DW_TAG_subprogram
9914 || last_die->tag == DW_TAG_lexical_block))))
9915 {
9916 nesting_level++;
9917 parent_die = last_die;
9918 continue;
9919 }
9920
9921 /* Otherwise we skip to the next sibling, if any. */
9922 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9923
9924 /* Back to the top, do it again. */
9925 }
9926 }
9927
9928 /* Read a minimal amount of information into the minimal die structure. */
9929
9930 static gdb_byte *
9931 read_partial_die (struct partial_die_info *part_die,
9932 struct abbrev_info *abbrev,
9933 unsigned int abbrev_len, bfd *abfd,
9934 gdb_byte *buffer, gdb_byte *info_ptr,
9935 struct dwarf2_cu *cu)
9936 {
9937 struct objfile *objfile = cu->objfile;
9938 unsigned int i;
9939 struct attribute attr;
9940 int has_low_pc_attr = 0;
9941 int has_high_pc_attr = 0;
9942 int high_pc_relative = 0;
9943
9944 memset (part_die, 0, sizeof (struct partial_die_info));
9945
9946 part_die->offset.sect_off = info_ptr - buffer;
9947
9948 info_ptr += abbrev_len;
9949
9950 if (abbrev == NULL)
9951 return info_ptr;
9952
9953 part_die->tag = abbrev->tag;
9954 part_die->has_children = abbrev->has_children;
9955
9956 for (i = 0; i < abbrev->num_attrs; ++i)
9957 {
9958 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9959
9960 /* Store the data if it is of an attribute we want to keep in a
9961 partial symbol table. */
9962 switch (attr.name)
9963 {
9964 case DW_AT_name:
9965 switch (part_die->tag)
9966 {
9967 case DW_TAG_compile_unit:
9968 case DW_TAG_type_unit:
9969 /* Compilation units have a DW_AT_name that is a filename, not
9970 a source language identifier. */
9971 case DW_TAG_enumeration_type:
9972 case DW_TAG_enumerator:
9973 /* These tags always have simple identifiers already; no need
9974 to canonicalize them. */
9975 part_die->name = DW_STRING (&attr);
9976 break;
9977 default:
9978 part_die->name
9979 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9980 &objfile->objfile_obstack);
9981 break;
9982 }
9983 break;
9984 case DW_AT_linkage_name:
9985 case DW_AT_MIPS_linkage_name:
9986 /* Note that both forms of linkage name might appear. We
9987 assume they will be the same, and we only store the last
9988 one we see. */
9989 if (cu->language == language_ada)
9990 part_die->name = DW_STRING (&attr);
9991 part_die->linkage_name = DW_STRING (&attr);
9992 break;
9993 case DW_AT_low_pc:
9994 has_low_pc_attr = 1;
9995 part_die->lowpc = DW_ADDR (&attr);
9996 break;
9997 case DW_AT_high_pc:
9998 has_high_pc_attr = 1;
9999 if (attr.form == DW_FORM_addr)
10000 part_die->highpc = DW_ADDR (&attr);
10001 else
10002 {
10003 high_pc_relative = 1;
10004 part_die->highpc = DW_UNSND (&attr);
10005 }
10006 break;
10007 case DW_AT_location:
10008 /* Support the .debug_loc offsets. */
10009 if (attr_form_is_block (&attr))
10010 {
10011 part_die->locdesc = DW_BLOCK (&attr);
10012 }
10013 else if (attr_form_is_section_offset (&attr))
10014 {
10015 dwarf2_complex_location_expr_complaint ();
10016 }
10017 else
10018 {
10019 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
10020 "partial symbol information");
10021 }
10022 break;
10023 case DW_AT_external:
10024 part_die->is_external = DW_UNSND (&attr);
10025 break;
10026 case DW_AT_declaration:
10027 part_die->is_declaration = DW_UNSND (&attr);
10028 break;
10029 case DW_AT_type:
10030 part_die->has_type = 1;
10031 break;
10032 case DW_AT_abstract_origin:
10033 case DW_AT_specification:
10034 case DW_AT_extension:
10035 part_die->has_specification = 1;
10036 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
10037 break;
10038 case DW_AT_sibling:
10039 /* Ignore absolute siblings, they might point outside of
10040 the current compile unit. */
10041 if (attr.form == DW_FORM_ref_addr)
10042 complaint (&symfile_complaints,
10043 _("ignoring absolute DW_AT_sibling"));
10044 else
10045 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
10046 break;
10047 case DW_AT_byte_size:
10048 part_die->has_byte_size = 1;
10049 break;
10050 case DW_AT_calling_convention:
10051 /* DWARF doesn't provide a way to identify a program's source-level
10052 entry point. DW_AT_calling_convention attributes are only meant
10053 to describe functions' calling conventions.
10054
10055 However, because it's a necessary piece of information in
10056 Fortran, and because DW_CC_program is the only piece of debugging
10057 information whose definition refers to a 'main program' at all,
10058 several compilers have begun marking Fortran main programs with
10059 DW_CC_program --- even when those functions use the standard
10060 calling conventions.
10061
10062 So until DWARF specifies a way to provide this information and
10063 compilers pick up the new representation, we'll support this
10064 practice. */
10065 if (DW_UNSND (&attr) == DW_CC_program
10066 && cu->language == language_fortran)
10067 {
10068 set_main_name (part_die->name);
10069
10070 /* As this DIE has a static linkage the name would be difficult
10071 to look up later. */
10072 language_of_main = language_fortran;
10073 }
10074 break;
10075 case DW_AT_inline:
10076 if (DW_UNSND (&attr) == DW_INL_inlined
10077 || DW_UNSND (&attr) == DW_INL_declared_inlined)
10078 part_die->may_be_inlined = 1;
10079 break;
10080 default:
10081 break;
10082 }
10083 }
10084
10085 if (high_pc_relative)
10086 part_die->highpc += part_die->lowpc;
10087
10088 if (has_low_pc_attr && has_high_pc_attr)
10089 {
10090 /* When using the GNU linker, .gnu.linkonce. sections are used to
10091 eliminate duplicate copies of functions and vtables and such.
10092 The linker will arbitrarily choose one and discard the others.
10093 The AT_*_pc values for such functions refer to local labels in
10094 these sections. If the section from that file was discarded, the
10095 labels are not in the output, so the relocs get a value of 0.
10096 If this is a discarded function, mark the pc bounds as invalid,
10097 so that GDB will ignore it. */
10098 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
10099 {
10100 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10101
10102 complaint (&symfile_complaints,
10103 _("DW_AT_low_pc %s is zero "
10104 "for DIE at 0x%x [in module %s]"),
10105 paddress (gdbarch, part_die->lowpc),
10106 part_die->offset.sect_off, objfile->name);
10107 }
10108 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
10109 else if (part_die->lowpc >= part_die->highpc)
10110 {
10111 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10112
10113 complaint (&symfile_complaints,
10114 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
10115 "for DIE at 0x%x [in module %s]"),
10116 paddress (gdbarch, part_die->lowpc),
10117 paddress (gdbarch, part_die->highpc),
10118 part_die->offset.sect_off, objfile->name);
10119 }
10120 else
10121 part_die->has_pc_info = 1;
10122 }
10123
10124 return info_ptr;
10125 }
10126
10127 /* Find a cached partial DIE at OFFSET in CU. */
10128
10129 static struct partial_die_info *
10130 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
10131 {
10132 struct partial_die_info *lookup_die = NULL;
10133 struct partial_die_info part_die;
10134
10135 part_die.offset = offset;
10136 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
10137 offset.sect_off);
10138
10139 return lookup_die;
10140 }
10141
10142 /* Find a partial DIE at OFFSET, which may or may not be in CU,
10143 except in the case of .debug_types DIEs which do not reference
10144 outside their CU (they do however referencing other types via
10145 DW_FORM_ref_sig8). */
10146
10147 static struct partial_die_info *
10148 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
10149 {
10150 struct objfile *objfile = cu->objfile;
10151 struct dwarf2_per_cu_data *per_cu = NULL;
10152 struct partial_die_info *pd = NULL;
10153
10154 if (offset_in_cu_p (&cu->header, offset))
10155 {
10156 pd = find_partial_die_in_comp_unit (offset, cu);
10157 if (pd != NULL)
10158 return pd;
10159 /* We missed recording what we needed.
10160 Load all dies and try again. */
10161 per_cu = cu->per_cu;
10162 }
10163 else
10164 {
10165 /* TUs don't reference other CUs/TUs (except via type signatures). */
10166 if (cu->per_cu->debug_types_section)
10167 {
10168 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
10169 " external reference to offset 0x%lx [in module %s].\n"),
10170 (long) cu->header.offset.sect_off, (long) offset.sect_off,
10171 bfd_get_filename (objfile->obfd));
10172 }
10173 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
10174
10175 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
10176 load_partial_comp_unit (per_cu);
10177
10178 per_cu->cu->last_used = 0;
10179 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10180 }
10181
10182 if (pd == NULL && per_cu->load_all_dies == 0)
10183 {
10184 struct cleanup *back_to;
10185 struct partial_die_info comp_unit_die;
10186 struct abbrev_info *abbrev;
10187 unsigned int bytes_read;
10188 char *info_ptr;
10189 struct dwarf2_section_info *sec;
10190
10191 per_cu->load_all_dies = 1;
10192
10193 if (per_cu->debug_types_section)
10194 sec = per_cu->debug_types_section;
10195 else
10196 sec = &dwarf2_per_objfile->info;
10197
10198 /* Re-read the DIEs, this time reading all of them.
10199 NOTE: We don't discard the previous set of DIEs.
10200 This doesn't happen very often so it's (hopefully) not a problem. */
10201 back_to = make_cleanup (null_cleanup, 0);
10202 if (per_cu->cu->dwarf2_abbrevs == NULL)
10203 {
10204 dwarf2_read_abbrevs (per_cu->cu);
10205 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10206 }
10207 info_ptr = (sec->buffer
10208 + per_cu->cu->header.offset.sect_off
10209 + per_cu->cu->header.first_die_offset.cu_off);
10210 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10211 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10212 objfile->obfd, sec->buffer, info_ptr,
10213 per_cu->cu);
10214 if (comp_unit_die.has_children)
10215 load_partial_dies (objfile->obfd, sec->buffer, info_ptr, 0,
10216 per_cu->cu);
10217 do_cleanups (back_to);
10218
10219 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10220 }
10221
10222 if (pd == NULL)
10223 internal_error (__FILE__, __LINE__,
10224 _("could not find partial DIE 0x%x "
10225 "in cache [from module %s]\n"),
10226 offset.sect_off, bfd_get_filename (objfile->obfd));
10227 return pd;
10228 }
10229
10230 /* See if we can figure out if the class lives in a namespace. We do
10231 this by looking for a member function; its demangled name will
10232 contain namespace info, if there is any. */
10233
10234 static void
10235 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10236 struct dwarf2_cu *cu)
10237 {
10238 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10239 what template types look like, because the demangler
10240 frequently doesn't give the same name as the debug info. We
10241 could fix this by only using the demangled name to get the
10242 prefix (but see comment in read_structure_type). */
10243
10244 struct partial_die_info *real_pdi;
10245 struct partial_die_info *child_pdi;
10246
10247 /* If this DIE (this DIE's specification, if any) has a parent, then
10248 we should not do this. We'll prepend the parent's fully qualified
10249 name when we create the partial symbol. */
10250
10251 real_pdi = struct_pdi;
10252 while (real_pdi->has_specification)
10253 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10254
10255 if (real_pdi->die_parent != NULL)
10256 return;
10257
10258 for (child_pdi = struct_pdi->die_child;
10259 child_pdi != NULL;
10260 child_pdi = child_pdi->die_sibling)
10261 {
10262 if (child_pdi->tag == DW_TAG_subprogram
10263 && child_pdi->linkage_name != NULL)
10264 {
10265 char *actual_class_name
10266 = language_class_name_from_physname (cu->language_defn,
10267 child_pdi->linkage_name);
10268 if (actual_class_name != NULL)
10269 {
10270 struct_pdi->name
10271 = obsavestring (actual_class_name,
10272 strlen (actual_class_name),
10273 &cu->objfile->objfile_obstack);
10274 xfree (actual_class_name);
10275 }
10276 break;
10277 }
10278 }
10279 }
10280
10281 /* Adjust PART_DIE before generating a symbol for it. This function
10282 may set the is_external flag or change the DIE's name. */
10283
10284 static void
10285 fixup_partial_die (struct partial_die_info *part_die,
10286 struct dwarf2_cu *cu)
10287 {
10288 /* Once we've fixed up a die, there's no point in doing so again.
10289 This also avoids a memory leak if we were to call
10290 guess_partial_die_structure_name multiple times. */
10291 if (part_die->fixup_called)
10292 return;
10293
10294 /* If we found a reference attribute and the DIE has no name, try
10295 to find a name in the referred to DIE. */
10296
10297 if (part_die->name == NULL && part_die->has_specification)
10298 {
10299 struct partial_die_info *spec_die;
10300
10301 spec_die = find_partial_die (part_die->spec_offset, cu);
10302
10303 fixup_partial_die (spec_die, cu);
10304
10305 if (spec_die->name)
10306 {
10307 part_die->name = spec_die->name;
10308
10309 /* Copy DW_AT_external attribute if it is set. */
10310 if (spec_die->is_external)
10311 part_die->is_external = spec_die->is_external;
10312 }
10313 }
10314
10315 /* Set default names for some unnamed DIEs. */
10316
10317 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10318 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10319
10320 /* If there is no parent die to provide a namespace, and there are
10321 children, see if we can determine the namespace from their linkage
10322 name. */
10323 if (cu->language == language_cplus
10324 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10325 && part_die->die_parent == NULL
10326 && part_die->has_children
10327 && (part_die->tag == DW_TAG_class_type
10328 || part_die->tag == DW_TAG_structure_type
10329 || part_die->tag == DW_TAG_union_type))
10330 guess_partial_die_structure_name (part_die, cu);
10331
10332 /* GCC might emit a nameless struct or union that has a linkage
10333 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10334 if (part_die->name == NULL
10335 && (part_die->tag == DW_TAG_class_type
10336 || part_die->tag == DW_TAG_interface_type
10337 || part_die->tag == DW_TAG_structure_type
10338 || part_die->tag == DW_TAG_union_type)
10339 && part_die->linkage_name != NULL)
10340 {
10341 char *demangled;
10342
10343 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10344 if (demangled)
10345 {
10346 const char *base;
10347
10348 /* Strip any leading namespaces/classes, keep only the base name.
10349 DW_AT_name for named DIEs does not contain the prefixes. */
10350 base = strrchr (demangled, ':');
10351 if (base && base > demangled && base[-1] == ':')
10352 base++;
10353 else
10354 base = demangled;
10355
10356 part_die->name = obsavestring (base, strlen (base),
10357 &cu->objfile->objfile_obstack);
10358 xfree (demangled);
10359 }
10360 }
10361
10362 part_die->fixup_called = 1;
10363 }
10364
10365 /* Read an attribute value described by an attribute form. */
10366
10367 static gdb_byte *
10368 read_attribute_value (struct attribute *attr, unsigned form,
10369 bfd *abfd, gdb_byte *info_ptr,
10370 struct dwarf2_cu *cu)
10371 {
10372 struct comp_unit_head *cu_header = &cu->header;
10373 unsigned int bytes_read;
10374 struct dwarf_block *blk;
10375
10376 attr->form = form;
10377 switch (form)
10378 {
10379 case DW_FORM_ref_addr:
10380 if (cu->header.version == 2)
10381 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10382 else
10383 DW_UNSND (attr) = read_offset (abfd, info_ptr,
10384 &cu->header, &bytes_read);
10385 info_ptr += bytes_read;
10386 break;
10387 case DW_FORM_addr:
10388 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10389 info_ptr += bytes_read;
10390 break;
10391 case DW_FORM_block2:
10392 blk = dwarf_alloc_block (cu);
10393 blk->size = read_2_bytes (abfd, info_ptr);
10394 info_ptr += 2;
10395 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10396 info_ptr += blk->size;
10397 DW_BLOCK (attr) = blk;
10398 break;
10399 case DW_FORM_block4:
10400 blk = dwarf_alloc_block (cu);
10401 blk->size = read_4_bytes (abfd, info_ptr);
10402 info_ptr += 4;
10403 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10404 info_ptr += blk->size;
10405 DW_BLOCK (attr) = blk;
10406 break;
10407 case DW_FORM_data2:
10408 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10409 info_ptr += 2;
10410 break;
10411 case DW_FORM_data4:
10412 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10413 info_ptr += 4;
10414 break;
10415 case DW_FORM_data8:
10416 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10417 info_ptr += 8;
10418 break;
10419 case DW_FORM_sec_offset:
10420 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10421 info_ptr += bytes_read;
10422 break;
10423 case DW_FORM_string:
10424 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10425 DW_STRING_IS_CANONICAL (attr) = 0;
10426 info_ptr += bytes_read;
10427 break;
10428 case DW_FORM_strp:
10429 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10430 &bytes_read);
10431 DW_STRING_IS_CANONICAL (attr) = 0;
10432 info_ptr += bytes_read;
10433 break;
10434 case DW_FORM_exprloc:
10435 case DW_FORM_block:
10436 blk = dwarf_alloc_block (cu);
10437 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10438 info_ptr += bytes_read;
10439 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10440 info_ptr += blk->size;
10441 DW_BLOCK (attr) = blk;
10442 break;
10443 case DW_FORM_block1:
10444 blk = dwarf_alloc_block (cu);
10445 blk->size = read_1_byte (abfd, info_ptr);
10446 info_ptr += 1;
10447 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10448 info_ptr += blk->size;
10449 DW_BLOCK (attr) = blk;
10450 break;
10451 case DW_FORM_data1:
10452 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10453 info_ptr += 1;
10454 break;
10455 case DW_FORM_flag:
10456 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10457 info_ptr += 1;
10458 break;
10459 case DW_FORM_flag_present:
10460 DW_UNSND (attr) = 1;
10461 break;
10462 case DW_FORM_sdata:
10463 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10464 info_ptr += bytes_read;
10465 break;
10466 case DW_FORM_udata:
10467 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10468 info_ptr += bytes_read;
10469 break;
10470 case DW_FORM_ref1:
10471 DW_UNSND (attr) = (cu->header.offset.sect_off
10472 + read_1_byte (abfd, info_ptr));
10473 info_ptr += 1;
10474 break;
10475 case DW_FORM_ref2:
10476 DW_UNSND (attr) = (cu->header.offset.sect_off
10477 + read_2_bytes (abfd, info_ptr));
10478 info_ptr += 2;
10479 break;
10480 case DW_FORM_ref4:
10481 DW_UNSND (attr) = (cu->header.offset.sect_off
10482 + read_4_bytes (abfd, info_ptr));
10483 info_ptr += 4;
10484 break;
10485 case DW_FORM_ref8:
10486 DW_UNSND (attr) = (cu->header.offset.sect_off
10487 + read_8_bytes (abfd, info_ptr));
10488 info_ptr += 8;
10489 break;
10490 case DW_FORM_ref_sig8:
10491 /* Convert the signature to something we can record in DW_UNSND
10492 for later lookup.
10493 NOTE: This is NULL if the type wasn't found. */
10494 DW_SIGNATURED_TYPE (attr) =
10495 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
10496 info_ptr += 8;
10497 break;
10498 case DW_FORM_ref_udata:
10499 DW_UNSND (attr) = (cu->header.offset.sect_off
10500 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10501 info_ptr += bytes_read;
10502 break;
10503 case DW_FORM_indirect:
10504 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10505 info_ptr += bytes_read;
10506 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10507 break;
10508 default:
10509 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10510 dwarf_form_name (form),
10511 bfd_get_filename (abfd));
10512 }
10513
10514 /* We have seen instances where the compiler tried to emit a byte
10515 size attribute of -1 which ended up being encoded as an unsigned
10516 0xffffffff. Although 0xffffffff is technically a valid size value,
10517 an object of this size seems pretty unlikely so we can relatively
10518 safely treat these cases as if the size attribute was invalid and
10519 treat them as zero by default. */
10520 if (attr->name == DW_AT_byte_size
10521 && form == DW_FORM_data4
10522 && DW_UNSND (attr) >= 0xffffffff)
10523 {
10524 complaint
10525 (&symfile_complaints,
10526 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10527 hex_string (DW_UNSND (attr)));
10528 DW_UNSND (attr) = 0;
10529 }
10530
10531 return info_ptr;
10532 }
10533
10534 /* Read an attribute described by an abbreviated attribute. */
10535
10536 static gdb_byte *
10537 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10538 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10539 {
10540 attr->name = abbrev->name;
10541 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10542 }
10543
10544 /* Read dwarf information from a buffer. */
10545
10546 static unsigned int
10547 read_1_byte (bfd *abfd, gdb_byte *buf)
10548 {
10549 return bfd_get_8 (abfd, buf);
10550 }
10551
10552 static int
10553 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10554 {
10555 return bfd_get_signed_8 (abfd, buf);
10556 }
10557
10558 static unsigned int
10559 read_2_bytes (bfd *abfd, gdb_byte *buf)
10560 {
10561 return bfd_get_16 (abfd, buf);
10562 }
10563
10564 static int
10565 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10566 {
10567 return bfd_get_signed_16 (abfd, buf);
10568 }
10569
10570 static unsigned int
10571 read_4_bytes (bfd *abfd, gdb_byte *buf)
10572 {
10573 return bfd_get_32 (abfd, buf);
10574 }
10575
10576 static int
10577 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10578 {
10579 return bfd_get_signed_32 (abfd, buf);
10580 }
10581
10582 static ULONGEST
10583 read_8_bytes (bfd *abfd, gdb_byte *buf)
10584 {
10585 return bfd_get_64 (abfd, buf);
10586 }
10587
10588 static CORE_ADDR
10589 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10590 unsigned int *bytes_read)
10591 {
10592 struct comp_unit_head *cu_header = &cu->header;
10593 CORE_ADDR retval = 0;
10594
10595 if (cu_header->signed_addr_p)
10596 {
10597 switch (cu_header->addr_size)
10598 {
10599 case 2:
10600 retval = bfd_get_signed_16 (abfd, buf);
10601 break;
10602 case 4:
10603 retval = bfd_get_signed_32 (abfd, buf);
10604 break;
10605 case 8:
10606 retval = bfd_get_signed_64 (abfd, buf);
10607 break;
10608 default:
10609 internal_error (__FILE__, __LINE__,
10610 _("read_address: bad switch, signed [in module %s]"),
10611 bfd_get_filename (abfd));
10612 }
10613 }
10614 else
10615 {
10616 switch (cu_header->addr_size)
10617 {
10618 case 2:
10619 retval = bfd_get_16 (abfd, buf);
10620 break;
10621 case 4:
10622 retval = bfd_get_32 (abfd, buf);
10623 break;
10624 case 8:
10625 retval = bfd_get_64 (abfd, buf);
10626 break;
10627 default:
10628 internal_error (__FILE__, __LINE__,
10629 _("read_address: bad switch, "
10630 "unsigned [in module %s]"),
10631 bfd_get_filename (abfd));
10632 }
10633 }
10634
10635 *bytes_read = cu_header->addr_size;
10636 return retval;
10637 }
10638
10639 /* Read the initial length from a section. The (draft) DWARF 3
10640 specification allows the initial length to take up either 4 bytes
10641 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10642 bytes describe the length and all offsets will be 8 bytes in length
10643 instead of 4.
10644
10645 An older, non-standard 64-bit format is also handled by this
10646 function. The older format in question stores the initial length
10647 as an 8-byte quantity without an escape value. Lengths greater
10648 than 2^32 aren't very common which means that the initial 4 bytes
10649 is almost always zero. Since a length value of zero doesn't make
10650 sense for the 32-bit format, this initial zero can be considered to
10651 be an escape value which indicates the presence of the older 64-bit
10652 format. As written, the code can't detect (old format) lengths
10653 greater than 4GB. If it becomes necessary to handle lengths
10654 somewhat larger than 4GB, we could allow other small values (such
10655 as the non-sensical values of 1, 2, and 3) to also be used as
10656 escape values indicating the presence of the old format.
10657
10658 The value returned via bytes_read should be used to increment the
10659 relevant pointer after calling read_initial_length().
10660
10661 [ Note: read_initial_length() and read_offset() are based on the
10662 document entitled "DWARF Debugging Information Format", revision
10663 3, draft 8, dated November 19, 2001. This document was obtained
10664 from:
10665
10666 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10667
10668 This document is only a draft and is subject to change. (So beware.)
10669
10670 Details regarding the older, non-standard 64-bit format were
10671 determined empirically by examining 64-bit ELF files produced by
10672 the SGI toolchain on an IRIX 6.5 machine.
10673
10674 - Kevin, July 16, 2002
10675 ] */
10676
10677 static LONGEST
10678 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10679 {
10680 LONGEST length = bfd_get_32 (abfd, buf);
10681
10682 if (length == 0xffffffff)
10683 {
10684 length = bfd_get_64 (abfd, buf + 4);
10685 *bytes_read = 12;
10686 }
10687 else if (length == 0)
10688 {
10689 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10690 length = bfd_get_64 (abfd, buf);
10691 *bytes_read = 8;
10692 }
10693 else
10694 {
10695 *bytes_read = 4;
10696 }
10697
10698 return length;
10699 }
10700
10701 /* Cover function for read_initial_length.
10702 Returns the length of the object at BUF, and stores the size of the
10703 initial length in *BYTES_READ and stores the size that offsets will be in
10704 *OFFSET_SIZE.
10705 If the initial length size is not equivalent to that specified in
10706 CU_HEADER then issue a complaint.
10707 This is useful when reading non-comp-unit headers. */
10708
10709 static LONGEST
10710 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10711 const struct comp_unit_head *cu_header,
10712 unsigned int *bytes_read,
10713 unsigned int *offset_size)
10714 {
10715 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10716
10717 gdb_assert (cu_header->initial_length_size == 4
10718 || cu_header->initial_length_size == 8
10719 || cu_header->initial_length_size == 12);
10720
10721 if (cu_header->initial_length_size != *bytes_read)
10722 complaint (&symfile_complaints,
10723 _("intermixed 32-bit and 64-bit DWARF sections"));
10724
10725 *offset_size = (*bytes_read == 4) ? 4 : 8;
10726 return length;
10727 }
10728
10729 /* Read an offset from the data stream. The size of the offset is
10730 given by cu_header->offset_size. */
10731
10732 static LONGEST
10733 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10734 unsigned int *bytes_read)
10735 {
10736 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10737
10738 *bytes_read = cu_header->offset_size;
10739 return offset;
10740 }
10741
10742 /* Read an offset from the data stream. */
10743
10744 static LONGEST
10745 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10746 {
10747 LONGEST retval = 0;
10748
10749 switch (offset_size)
10750 {
10751 case 4:
10752 retval = bfd_get_32 (abfd, buf);
10753 break;
10754 case 8:
10755 retval = bfd_get_64 (abfd, buf);
10756 break;
10757 default:
10758 internal_error (__FILE__, __LINE__,
10759 _("read_offset_1: bad switch [in module %s]"),
10760 bfd_get_filename (abfd));
10761 }
10762
10763 return retval;
10764 }
10765
10766 static gdb_byte *
10767 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10768 {
10769 /* If the size of a host char is 8 bits, we can return a pointer
10770 to the buffer, otherwise we have to copy the data to a buffer
10771 allocated on the temporary obstack. */
10772 gdb_assert (HOST_CHAR_BIT == 8);
10773 return buf;
10774 }
10775
10776 static char *
10777 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10778 {
10779 /* If the size of a host char is 8 bits, we can return a pointer
10780 to the string, otherwise we have to copy the string to a buffer
10781 allocated on the temporary obstack. */
10782 gdb_assert (HOST_CHAR_BIT == 8);
10783 if (*buf == '\0')
10784 {
10785 *bytes_read_ptr = 1;
10786 return NULL;
10787 }
10788 *bytes_read_ptr = strlen ((char *) buf) + 1;
10789 return (char *) buf;
10790 }
10791
10792 static char *
10793 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10794 {
10795 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10796 if (dwarf2_per_objfile->str.buffer == NULL)
10797 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10798 bfd_get_filename (abfd));
10799 if (str_offset >= dwarf2_per_objfile->str.size)
10800 error (_("DW_FORM_strp pointing outside of "
10801 ".debug_str section [in module %s]"),
10802 bfd_get_filename (abfd));
10803 gdb_assert (HOST_CHAR_BIT == 8);
10804 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10805 return NULL;
10806 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10807 }
10808
10809 static char *
10810 read_indirect_string (bfd *abfd, gdb_byte *buf,
10811 const struct comp_unit_head *cu_header,
10812 unsigned int *bytes_read_ptr)
10813 {
10814 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10815
10816 return read_indirect_string_at_offset (abfd, str_offset);
10817 }
10818
10819 static ULONGEST
10820 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10821 {
10822 ULONGEST result;
10823 unsigned int num_read;
10824 int i, shift;
10825 unsigned char byte;
10826
10827 result = 0;
10828 shift = 0;
10829 num_read = 0;
10830 i = 0;
10831 while (1)
10832 {
10833 byte = bfd_get_8 (abfd, buf);
10834 buf++;
10835 num_read++;
10836 result |= ((ULONGEST) (byte & 127) << shift);
10837 if ((byte & 128) == 0)
10838 {
10839 break;
10840 }
10841 shift += 7;
10842 }
10843 *bytes_read_ptr = num_read;
10844 return result;
10845 }
10846
10847 static LONGEST
10848 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10849 {
10850 LONGEST result;
10851 int i, shift, num_read;
10852 unsigned char byte;
10853
10854 result = 0;
10855 shift = 0;
10856 num_read = 0;
10857 i = 0;
10858 while (1)
10859 {
10860 byte = bfd_get_8 (abfd, buf);
10861 buf++;
10862 num_read++;
10863 result |= ((LONGEST) (byte & 127) << shift);
10864 shift += 7;
10865 if ((byte & 128) == 0)
10866 {
10867 break;
10868 }
10869 }
10870 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10871 result |= -(((LONGEST) 1) << shift);
10872 *bytes_read_ptr = num_read;
10873 return result;
10874 }
10875
10876 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10877
10878 static gdb_byte *
10879 skip_leb128 (bfd *abfd, gdb_byte *buf)
10880 {
10881 int byte;
10882
10883 while (1)
10884 {
10885 byte = bfd_get_8 (abfd, buf);
10886 buf++;
10887 if ((byte & 128) == 0)
10888 return buf;
10889 }
10890 }
10891
10892 static void
10893 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10894 {
10895 switch (lang)
10896 {
10897 case DW_LANG_C89:
10898 case DW_LANG_C99:
10899 case DW_LANG_C:
10900 cu->language = language_c;
10901 break;
10902 case DW_LANG_C_plus_plus:
10903 cu->language = language_cplus;
10904 break;
10905 case DW_LANG_D:
10906 cu->language = language_d;
10907 break;
10908 case DW_LANG_Fortran77:
10909 case DW_LANG_Fortran90:
10910 case DW_LANG_Fortran95:
10911 cu->language = language_fortran;
10912 break;
10913 case DW_LANG_Go:
10914 cu->language = language_go;
10915 break;
10916 case DW_LANG_Mips_Assembler:
10917 cu->language = language_asm;
10918 break;
10919 case DW_LANG_Java:
10920 cu->language = language_java;
10921 break;
10922 case DW_LANG_Ada83:
10923 case DW_LANG_Ada95:
10924 cu->language = language_ada;
10925 break;
10926 case DW_LANG_Modula2:
10927 cu->language = language_m2;
10928 break;
10929 case DW_LANG_Pascal83:
10930 cu->language = language_pascal;
10931 break;
10932 case DW_LANG_ObjC:
10933 cu->language = language_objc;
10934 break;
10935 case DW_LANG_Cobol74:
10936 case DW_LANG_Cobol85:
10937 default:
10938 cu->language = language_minimal;
10939 break;
10940 }
10941 cu->language_defn = language_def (cu->language);
10942 }
10943
10944 /* Return the named attribute or NULL if not there. */
10945
10946 static struct attribute *
10947 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10948 {
10949 for (;;)
10950 {
10951 unsigned int i;
10952 struct attribute *spec = NULL;
10953
10954 for (i = 0; i < die->num_attrs; ++i)
10955 {
10956 if (die->attrs[i].name == name)
10957 return &die->attrs[i];
10958 if (die->attrs[i].name == DW_AT_specification
10959 || die->attrs[i].name == DW_AT_abstract_origin)
10960 spec = &die->attrs[i];
10961 }
10962
10963 if (!spec)
10964 break;
10965
10966 die = follow_die_ref (die, spec, &cu);
10967 }
10968
10969 return NULL;
10970 }
10971
10972 /* Return the named attribute or NULL if not there,
10973 but do not follow DW_AT_specification, etc.
10974 This is for use in contexts where we're reading .debug_types dies.
10975 Following DW_AT_specification, DW_AT_abstract_origin will take us
10976 back up the chain, and we want to go down. */
10977
10978 static struct attribute *
10979 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10980 struct dwarf2_cu *cu)
10981 {
10982 unsigned int i;
10983
10984 for (i = 0; i < die->num_attrs; ++i)
10985 if (die->attrs[i].name == name)
10986 return &die->attrs[i];
10987
10988 return NULL;
10989 }
10990
10991 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10992 and holds a non-zero value. This function should only be used for
10993 DW_FORM_flag or DW_FORM_flag_present attributes. */
10994
10995 static int
10996 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10997 {
10998 struct attribute *attr = dwarf2_attr (die, name, cu);
10999
11000 return (attr && DW_UNSND (attr));
11001 }
11002
11003 static int
11004 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
11005 {
11006 /* A DIE is a declaration if it has a DW_AT_declaration attribute
11007 which value is non-zero. However, we have to be careful with
11008 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
11009 (via dwarf2_flag_true_p) follows this attribute. So we may
11010 end up accidently finding a declaration attribute that belongs
11011 to a different DIE referenced by the specification attribute,
11012 even though the given DIE does not have a declaration attribute. */
11013 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
11014 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
11015 }
11016
11017 /* Return the die giving the specification for DIE, if there is
11018 one. *SPEC_CU is the CU containing DIE on input, and the CU
11019 containing the return value on output. If there is no
11020 specification, but there is an abstract origin, that is
11021 returned. */
11022
11023 static struct die_info *
11024 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
11025 {
11026 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
11027 *spec_cu);
11028
11029 if (spec_attr == NULL)
11030 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
11031
11032 if (spec_attr == NULL)
11033 return NULL;
11034 else
11035 return follow_die_ref (die, spec_attr, spec_cu);
11036 }
11037
11038 /* Free the line_header structure *LH, and any arrays and strings it
11039 refers to.
11040 NOTE: This is also used as a "cleanup" function. */
11041
11042 static void
11043 free_line_header (struct line_header *lh)
11044 {
11045 if (lh->standard_opcode_lengths)
11046 xfree (lh->standard_opcode_lengths);
11047
11048 /* Remember that all the lh->file_names[i].name pointers are
11049 pointers into debug_line_buffer, and don't need to be freed. */
11050 if (lh->file_names)
11051 xfree (lh->file_names);
11052
11053 /* Similarly for the include directory names. */
11054 if (lh->include_dirs)
11055 xfree (lh->include_dirs);
11056
11057 xfree (lh);
11058 }
11059
11060 /* Add an entry to LH's include directory table. */
11061
11062 static void
11063 add_include_dir (struct line_header *lh, char *include_dir)
11064 {
11065 /* Grow the array if necessary. */
11066 if (lh->include_dirs_size == 0)
11067 {
11068 lh->include_dirs_size = 1; /* for testing */
11069 lh->include_dirs = xmalloc (lh->include_dirs_size
11070 * sizeof (*lh->include_dirs));
11071 }
11072 else if (lh->num_include_dirs >= lh->include_dirs_size)
11073 {
11074 lh->include_dirs_size *= 2;
11075 lh->include_dirs = xrealloc (lh->include_dirs,
11076 (lh->include_dirs_size
11077 * sizeof (*lh->include_dirs)));
11078 }
11079
11080 lh->include_dirs[lh->num_include_dirs++] = include_dir;
11081 }
11082
11083 /* Add an entry to LH's file name table. */
11084
11085 static void
11086 add_file_name (struct line_header *lh,
11087 char *name,
11088 unsigned int dir_index,
11089 unsigned int mod_time,
11090 unsigned int length)
11091 {
11092 struct file_entry *fe;
11093
11094 /* Grow the array if necessary. */
11095 if (lh->file_names_size == 0)
11096 {
11097 lh->file_names_size = 1; /* for testing */
11098 lh->file_names = xmalloc (lh->file_names_size
11099 * sizeof (*lh->file_names));
11100 }
11101 else if (lh->num_file_names >= lh->file_names_size)
11102 {
11103 lh->file_names_size *= 2;
11104 lh->file_names = xrealloc (lh->file_names,
11105 (lh->file_names_size
11106 * sizeof (*lh->file_names)));
11107 }
11108
11109 fe = &lh->file_names[lh->num_file_names++];
11110 fe->name = name;
11111 fe->dir_index = dir_index;
11112 fe->mod_time = mod_time;
11113 fe->length = length;
11114 fe->included_p = 0;
11115 fe->symtab = NULL;
11116 }
11117
11118 /* Read the statement program header starting at OFFSET in
11119 .debug_line, according to the endianness of ABFD. Return a pointer
11120 to a struct line_header, allocated using xmalloc.
11121
11122 NOTE: the strings in the include directory and file name tables of
11123 the returned object point into debug_line_buffer, and must not be
11124 freed. */
11125
11126 static struct line_header *
11127 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
11128 struct dwarf2_cu *cu)
11129 {
11130 struct cleanup *back_to;
11131 struct line_header *lh;
11132 gdb_byte *line_ptr;
11133 unsigned int bytes_read, offset_size;
11134 int i;
11135 char *cur_dir, *cur_file;
11136
11137 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
11138 if (dwarf2_per_objfile->line.buffer == NULL)
11139 {
11140 complaint (&symfile_complaints, _("missing .debug_line section"));
11141 return 0;
11142 }
11143
11144 /* Make sure that at least there's room for the total_length field.
11145 That could be 12 bytes long, but we're just going to fudge that. */
11146 if (offset + 4 >= dwarf2_per_objfile->line.size)
11147 {
11148 dwarf2_statement_list_fits_in_line_number_section_complaint ();
11149 return 0;
11150 }
11151
11152 lh = xmalloc (sizeof (*lh));
11153 memset (lh, 0, sizeof (*lh));
11154 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
11155 (void *) lh);
11156
11157 line_ptr = dwarf2_per_objfile->line.buffer + offset;
11158
11159 /* Read in the header. */
11160 lh->total_length =
11161 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
11162 &bytes_read, &offset_size);
11163 line_ptr += bytes_read;
11164 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
11165 + dwarf2_per_objfile->line.size))
11166 {
11167 dwarf2_statement_list_fits_in_line_number_section_complaint ();
11168 return 0;
11169 }
11170 lh->statement_program_end = line_ptr + lh->total_length;
11171 lh->version = read_2_bytes (abfd, line_ptr);
11172 line_ptr += 2;
11173 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
11174 line_ptr += offset_size;
11175 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
11176 line_ptr += 1;
11177 if (lh->version >= 4)
11178 {
11179 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
11180 line_ptr += 1;
11181 }
11182 else
11183 lh->maximum_ops_per_instruction = 1;
11184
11185 if (lh->maximum_ops_per_instruction == 0)
11186 {
11187 lh->maximum_ops_per_instruction = 1;
11188 complaint (&symfile_complaints,
11189 _("invalid maximum_ops_per_instruction "
11190 "in `.debug_line' section"));
11191 }
11192
11193 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11194 line_ptr += 1;
11195 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11196 line_ptr += 1;
11197 lh->line_range = read_1_byte (abfd, line_ptr);
11198 line_ptr += 1;
11199 lh->opcode_base = read_1_byte (abfd, line_ptr);
11200 line_ptr += 1;
11201 lh->standard_opcode_lengths
11202 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
11203
11204 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11205 for (i = 1; i < lh->opcode_base; ++i)
11206 {
11207 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11208 line_ptr += 1;
11209 }
11210
11211 /* Read directory table. */
11212 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11213 {
11214 line_ptr += bytes_read;
11215 add_include_dir (lh, cur_dir);
11216 }
11217 line_ptr += bytes_read;
11218
11219 /* Read file name table. */
11220 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11221 {
11222 unsigned int dir_index, mod_time, length;
11223
11224 line_ptr += bytes_read;
11225 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11226 line_ptr += bytes_read;
11227 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11228 line_ptr += bytes_read;
11229 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11230 line_ptr += bytes_read;
11231
11232 add_file_name (lh, cur_file, dir_index, mod_time, length);
11233 }
11234 line_ptr += bytes_read;
11235 lh->statement_program_start = line_ptr;
11236
11237 if (line_ptr > (dwarf2_per_objfile->line.buffer
11238 + dwarf2_per_objfile->line.size))
11239 complaint (&symfile_complaints,
11240 _("line number info header doesn't "
11241 "fit in `.debug_line' section"));
11242
11243 discard_cleanups (back_to);
11244 return lh;
11245 }
11246
11247 /* Subroutine of dwarf_decode_lines to simplify it.
11248 Return the file name of the psymtab for included file FILE_INDEX
11249 in line header LH of PST.
11250 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11251 If space for the result is malloc'd, it will be freed by a cleanup.
11252 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11253
11254 static char *
11255 psymtab_include_file_name (const struct line_header *lh, int file_index,
11256 const struct partial_symtab *pst,
11257 const char *comp_dir)
11258 {
11259 const struct file_entry fe = lh->file_names [file_index];
11260 char *include_name = fe.name;
11261 char *include_name_to_compare = include_name;
11262 char *dir_name = NULL;
11263 const char *pst_filename;
11264 char *copied_name = NULL;
11265 int file_is_pst;
11266
11267 if (fe.dir_index)
11268 dir_name = lh->include_dirs[fe.dir_index - 1];
11269
11270 if (!IS_ABSOLUTE_PATH (include_name)
11271 && (dir_name != NULL || comp_dir != NULL))
11272 {
11273 /* Avoid creating a duplicate psymtab for PST.
11274 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11275 Before we do the comparison, however, we need to account
11276 for DIR_NAME and COMP_DIR.
11277 First prepend dir_name (if non-NULL). If we still don't
11278 have an absolute path prepend comp_dir (if non-NULL).
11279 However, the directory we record in the include-file's
11280 psymtab does not contain COMP_DIR (to match the
11281 corresponding symtab(s)).
11282
11283 Example:
11284
11285 bash$ cd /tmp
11286 bash$ gcc -g ./hello.c
11287 include_name = "hello.c"
11288 dir_name = "."
11289 DW_AT_comp_dir = comp_dir = "/tmp"
11290 DW_AT_name = "./hello.c" */
11291
11292 if (dir_name != NULL)
11293 {
11294 include_name = concat (dir_name, SLASH_STRING,
11295 include_name, (char *)NULL);
11296 include_name_to_compare = include_name;
11297 make_cleanup (xfree, include_name);
11298 }
11299 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11300 {
11301 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11302 include_name, (char *)NULL);
11303 }
11304 }
11305
11306 pst_filename = pst->filename;
11307 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11308 {
11309 copied_name = concat (pst->dirname, SLASH_STRING,
11310 pst_filename, (char *)NULL);
11311 pst_filename = copied_name;
11312 }
11313
11314 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11315
11316 if (include_name_to_compare != include_name)
11317 xfree (include_name_to_compare);
11318 if (copied_name != NULL)
11319 xfree (copied_name);
11320
11321 if (file_is_pst)
11322 return NULL;
11323 return include_name;
11324 }
11325
11326 /* Ignore this record_line request. */
11327
11328 static void
11329 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11330 {
11331 return;
11332 }
11333
11334 /* Subroutine of dwarf_decode_lines to simplify it.
11335 Process the line number information in LH. */
11336
11337 static void
11338 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11339 struct dwarf2_cu *cu, struct partial_symtab *pst)
11340 {
11341 gdb_byte *line_ptr, *extended_end;
11342 gdb_byte *line_end;
11343 unsigned int bytes_read, extended_len;
11344 unsigned char op_code, extended_op, adj_opcode;
11345 CORE_ADDR baseaddr;
11346 struct objfile *objfile = cu->objfile;
11347 bfd *abfd = objfile->obfd;
11348 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11349 const int decode_for_pst_p = (pst != NULL);
11350 struct subfile *last_subfile = NULL;
11351 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11352 = record_line;
11353
11354 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11355
11356 line_ptr = lh->statement_program_start;
11357 line_end = lh->statement_program_end;
11358
11359 /* Read the statement sequences until there's nothing left. */
11360 while (line_ptr < line_end)
11361 {
11362 /* state machine registers */
11363 CORE_ADDR address = 0;
11364 unsigned int file = 1;
11365 unsigned int line = 1;
11366 unsigned int column = 0;
11367 int is_stmt = lh->default_is_stmt;
11368 int basic_block = 0;
11369 int end_sequence = 0;
11370 CORE_ADDR addr;
11371 unsigned char op_index = 0;
11372
11373 if (!decode_for_pst_p && lh->num_file_names >= file)
11374 {
11375 /* Start a subfile for the current file of the state machine. */
11376 /* lh->include_dirs and lh->file_names are 0-based, but the
11377 directory and file name numbers in the statement program
11378 are 1-based. */
11379 struct file_entry *fe = &lh->file_names[file - 1];
11380 char *dir = NULL;
11381
11382 if (fe->dir_index)
11383 dir = lh->include_dirs[fe->dir_index - 1];
11384
11385 dwarf2_start_subfile (fe->name, dir, comp_dir);
11386 }
11387
11388 /* Decode the table. */
11389 while (!end_sequence)
11390 {
11391 op_code = read_1_byte (abfd, line_ptr);
11392 line_ptr += 1;
11393 if (line_ptr > line_end)
11394 {
11395 dwarf2_debug_line_missing_end_sequence_complaint ();
11396 break;
11397 }
11398
11399 if (op_code >= lh->opcode_base)
11400 {
11401 /* Special operand. */
11402 adj_opcode = op_code - lh->opcode_base;
11403 address += (((op_index + (adj_opcode / lh->line_range))
11404 / lh->maximum_ops_per_instruction)
11405 * lh->minimum_instruction_length);
11406 op_index = ((op_index + (adj_opcode / lh->line_range))
11407 % lh->maximum_ops_per_instruction);
11408 line += lh->line_base + (adj_opcode % lh->line_range);
11409 if (lh->num_file_names < file || file == 0)
11410 dwarf2_debug_line_missing_file_complaint ();
11411 /* For now we ignore lines not starting on an
11412 instruction boundary. */
11413 else if (op_index == 0)
11414 {
11415 lh->file_names[file - 1].included_p = 1;
11416 if (!decode_for_pst_p && is_stmt)
11417 {
11418 if (last_subfile != current_subfile)
11419 {
11420 addr = gdbarch_addr_bits_remove (gdbarch, address);
11421 if (last_subfile)
11422 (*p_record_line) (last_subfile, 0, addr);
11423 last_subfile = current_subfile;
11424 }
11425 /* Append row to matrix using current values. */
11426 addr = gdbarch_addr_bits_remove (gdbarch, address);
11427 (*p_record_line) (current_subfile, line, addr);
11428 }
11429 }
11430 basic_block = 0;
11431 }
11432 else switch (op_code)
11433 {
11434 case DW_LNS_extended_op:
11435 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11436 &bytes_read);
11437 line_ptr += bytes_read;
11438 extended_end = line_ptr + extended_len;
11439 extended_op = read_1_byte (abfd, line_ptr);
11440 line_ptr += 1;
11441 switch (extended_op)
11442 {
11443 case DW_LNE_end_sequence:
11444 p_record_line = record_line;
11445 end_sequence = 1;
11446 break;
11447 case DW_LNE_set_address:
11448 address = read_address (abfd, line_ptr, cu, &bytes_read);
11449
11450 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11451 {
11452 /* This line table is for a function which has been
11453 GCd by the linker. Ignore it. PR gdb/12528 */
11454
11455 long line_offset
11456 = line_ptr - dwarf2_per_objfile->line.buffer;
11457
11458 complaint (&symfile_complaints,
11459 _(".debug_line address at offset 0x%lx is 0 "
11460 "[in module %s]"),
11461 line_offset, objfile->name);
11462 p_record_line = noop_record_line;
11463 }
11464
11465 op_index = 0;
11466 line_ptr += bytes_read;
11467 address += baseaddr;
11468 break;
11469 case DW_LNE_define_file:
11470 {
11471 char *cur_file;
11472 unsigned int dir_index, mod_time, length;
11473
11474 cur_file = read_direct_string (abfd, line_ptr,
11475 &bytes_read);
11476 line_ptr += bytes_read;
11477 dir_index =
11478 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11479 line_ptr += bytes_read;
11480 mod_time =
11481 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11482 line_ptr += bytes_read;
11483 length =
11484 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11485 line_ptr += bytes_read;
11486 add_file_name (lh, cur_file, dir_index, mod_time, length);
11487 }
11488 break;
11489 case DW_LNE_set_discriminator:
11490 /* The discriminator is not interesting to the debugger;
11491 just ignore it. */
11492 line_ptr = extended_end;
11493 break;
11494 default:
11495 complaint (&symfile_complaints,
11496 _("mangled .debug_line section"));
11497 return;
11498 }
11499 /* Make sure that we parsed the extended op correctly. If e.g.
11500 we expected a different address size than the producer used,
11501 we may have read the wrong number of bytes. */
11502 if (line_ptr != extended_end)
11503 {
11504 complaint (&symfile_complaints,
11505 _("mangled .debug_line section"));
11506 return;
11507 }
11508 break;
11509 case DW_LNS_copy:
11510 if (lh->num_file_names < file || file == 0)
11511 dwarf2_debug_line_missing_file_complaint ();
11512 else
11513 {
11514 lh->file_names[file - 1].included_p = 1;
11515 if (!decode_for_pst_p && is_stmt)
11516 {
11517 if (last_subfile != current_subfile)
11518 {
11519 addr = gdbarch_addr_bits_remove (gdbarch, address);
11520 if (last_subfile)
11521 (*p_record_line) (last_subfile, 0, addr);
11522 last_subfile = current_subfile;
11523 }
11524 addr = gdbarch_addr_bits_remove (gdbarch, address);
11525 (*p_record_line) (current_subfile, line, addr);
11526 }
11527 }
11528 basic_block = 0;
11529 break;
11530 case DW_LNS_advance_pc:
11531 {
11532 CORE_ADDR adjust
11533 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11534
11535 address += (((op_index + adjust)
11536 / lh->maximum_ops_per_instruction)
11537 * lh->minimum_instruction_length);
11538 op_index = ((op_index + adjust)
11539 % lh->maximum_ops_per_instruction);
11540 line_ptr += bytes_read;
11541 }
11542 break;
11543 case DW_LNS_advance_line:
11544 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11545 line_ptr += bytes_read;
11546 break;
11547 case DW_LNS_set_file:
11548 {
11549 /* The arrays lh->include_dirs and lh->file_names are
11550 0-based, but the directory and file name numbers in
11551 the statement program are 1-based. */
11552 struct file_entry *fe;
11553 char *dir = NULL;
11554
11555 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11556 line_ptr += bytes_read;
11557 if (lh->num_file_names < file || file == 0)
11558 dwarf2_debug_line_missing_file_complaint ();
11559 else
11560 {
11561 fe = &lh->file_names[file - 1];
11562 if (fe->dir_index)
11563 dir = lh->include_dirs[fe->dir_index - 1];
11564 if (!decode_for_pst_p)
11565 {
11566 last_subfile = current_subfile;
11567 dwarf2_start_subfile (fe->name, dir, comp_dir);
11568 }
11569 }
11570 }
11571 break;
11572 case DW_LNS_set_column:
11573 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11574 line_ptr += bytes_read;
11575 break;
11576 case DW_LNS_negate_stmt:
11577 is_stmt = (!is_stmt);
11578 break;
11579 case DW_LNS_set_basic_block:
11580 basic_block = 1;
11581 break;
11582 /* Add to the address register of the state machine the
11583 address increment value corresponding to special opcode
11584 255. I.e., this value is scaled by the minimum
11585 instruction length since special opcode 255 would have
11586 scaled the increment. */
11587 case DW_LNS_const_add_pc:
11588 {
11589 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11590
11591 address += (((op_index + adjust)
11592 / lh->maximum_ops_per_instruction)
11593 * lh->minimum_instruction_length);
11594 op_index = ((op_index + adjust)
11595 % lh->maximum_ops_per_instruction);
11596 }
11597 break;
11598 case DW_LNS_fixed_advance_pc:
11599 address += read_2_bytes (abfd, line_ptr);
11600 op_index = 0;
11601 line_ptr += 2;
11602 break;
11603 default:
11604 {
11605 /* Unknown standard opcode, ignore it. */
11606 int i;
11607
11608 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11609 {
11610 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11611 line_ptr += bytes_read;
11612 }
11613 }
11614 }
11615 }
11616 if (lh->num_file_names < file || file == 0)
11617 dwarf2_debug_line_missing_file_complaint ();
11618 else
11619 {
11620 lh->file_names[file - 1].included_p = 1;
11621 if (!decode_for_pst_p)
11622 {
11623 addr = gdbarch_addr_bits_remove (gdbarch, address);
11624 (*p_record_line) (current_subfile, 0, addr);
11625 }
11626 }
11627 }
11628 }
11629
11630 /* Decode the Line Number Program (LNP) for the given line_header
11631 structure and CU. The actual information extracted and the type
11632 of structures created from the LNP depends on the value of PST.
11633
11634 1. If PST is NULL, then this procedure uses the data from the program
11635 to create all necessary symbol tables, and their linetables.
11636
11637 2. If PST is not NULL, this procedure reads the program to determine
11638 the list of files included by the unit represented by PST, and
11639 builds all the associated partial symbol tables.
11640
11641 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11642 It is used for relative paths in the line table.
11643 NOTE: When processing partial symtabs (pst != NULL),
11644 comp_dir == pst->dirname.
11645
11646 NOTE: It is important that psymtabs have the same file name (via strcmp)
11647 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11648 symtab we don't use it in the name of the psymtabs we create.
11649 E.g. expand_line_sal requires this when finding psymtabs to expand.
11650 A good testcase for this is mb-inline.exp. */
11651
11652 static void
11653 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11654 struct dwarf2_cu *cu, struct partial_symtab *pst,
11655 int want_line_info)
11656 {
11657 struct objfile *objfile = cu->objfile;
11658 const int decode_for_pst_p = (pst != NULL);
11659 struct subfile *first_subfile = current_subfile;
11660
11661 if (want_line_info)
11662 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
11663
11664 if (decode_for_pst_p)
11665 {
11666 int file_index;
11667
11668 /* Now that we're done scanning the Line Header Program, we can
11669 create the psymtab of each included file. */
11670 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11671 if (lh->file_names[file_index].included_p == 1)
11672 {
11673 char *include_name =
11674 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11675 if (include_name != NULL)
11676 dwarf2_create_include_psymtab (include_name, pst, objfile);
11677 }
11678 }
11679 else
11680 {
11681 /* Make sure a symtab is created for every file, even files
11682 which contain only variables (i.e. no code with associated
11683 line numbers). */
11684 int i;
11685
11686 for (i = 0; i < lh->num_file_names; i++)
11687 {
11688 char *dir = NULL;
11689 struct file_entry *fe;
11690
11691 fe = &lh->file_names[i];
11692 if (fe->dir_index)
11693 dir = lh->include_dirs[fe->dir_index - 1];
11694 dwarf2_start_subfile (fe->name, dir, comp_dir);
11695
11696 /* Skip the main file; we don't need it, and it must be
11697 allocated last, so that it will show up before the
11698 non-primary symtabs in the objfile's symtab list. */
11699 if (current_subfile == first_subfile)
11700 continue;
11701
11702 if (current_subfile->symtab == NULL)
11703 current_subfile->symtab = allocate_symtab (current_subfile->name,
11704 objfile);
11705 fe->symtab = current_subfile->symtab;
11706 }
11707 }
11708 }
11709
11710 /* Start a subfile for DWARF. FILENAME is the name of the file and
11711 DIRNAME the name of the source directory which contains FILENAME
11712 or NULL if not known. COMP_DIR is the compilation directory for the
11713 linetable's compilation unit or NULL if not known.
11714 This routine tries to keep line numbers from identical absolute and
11715 relative file names in a common subfile.
11716
11717 Using the `list' example from the GDB testsuite, which resides in
11718 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11719 of /srcdir/list0.c yields the following debugging information for list0.c:
11720
11721 DW_AT_name: /srcdir/list0.c
11722 DW_AT_comp_dir: /compdir
11723 files.files[0].name: list0.h
11724 files.files[0].dir: /srcdir
11725 files.files[1].name: list0.c
11726 files.files[1].dir: /srcdir
11727
11728 The line number information for list0.c has to end up in a single
11729 subfile, so that `break /srcdir/list0.c:1' works as expected.
11730 start_subfile will ensure that this happens provided that we pass the
11731 concatenation of files.files[1].dir and files.files[1].name as the
11732 subfile's name. */
11733
11734 static void
11735 dwarf2_start_subfile (char *filename, const char *dirname,
11736 const char *comp_dir)
11737 {
11738 char *fullname;
11739
11740 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11741 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11742 second argument to start_subfile. To be consistent, we do the
11743 same here. In order not to lose the line information directory,
11744 we concatenate it to the filename when it makes sense.
11745 Note that the Dwarf3 standard says (speaking of filenames in line
11746 information): ``The directory index is ignored for file names
11747 that represent full path names''. Thus ignoring dirname in the
11748 `else' branch below isn't an issue. */
11749
11750 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11751 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11752 else
11753 fullname = filename;
11754
11755 start_subfile (fullname, comp_dir);
11756
11757 if (fullname != filename)
11758 xfree (fullname);
11759 }
11760
11761 static void
11762 var_decode_location (struct attribute *attr, struct symbol *sym,
11763 struct dwarf2_cu *cu)
11764 {
11765 struct objfile *objfile = cu->objfile;
11766 struct comp_unit_head *cu_header = &cu->header;
11767
11768 /* NOTE drow/2003-01-30: There used to be a comment and some special
11769 code here to turn a symbol with DW_AT_external and a
11770 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11771 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11772 with some versions of binutils) where shared libraries could have
11773 relocations against symbols in their debug information - the
11774 minimal symbol would have the right address, but the debug info
11775 would not. It's no longer necessary, because we will explicitly
11776 apply relocations when we read in the debug information now. */
11777
11778 /* A DW_AT_location attribute with no contents indicates that a
11779 variable has been optimized away. */
11780 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11781 {
11782 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11783 return;
11784 }
11785
11786 /* Handle one degenerate form of location expression specially, to
11787 preserve GDB's previous behavior when section offsets are
11788 specified. If this is just a DW_OP_addr then mark this symbol
11789 as LOC_STATIC. */
11790
11791 if (attr_form_is_block (attr)
11792 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11793 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11794 {
11795 unsigned int dummy;
11796
11797 SYMBOL_VALUE_ADDRESS (sym) =
11798 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11799 SYMBOL_CLASS (sym) = LOC_STATIC;
11800 fixup_symbol_section (sym, objfile);
11801 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11802 SYMBOL_SECTION (sym));
11803 return;
11804 }
11805
11806 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11807 expression evaluator, and use LOC_COMPUTED only when necessary
11808 (i.e. when the value of a register or memory location is
11809 referenced, or a thread-local block, etc.). Then again, it might
11810 not be worthwhile. I'm assuming that it isn't unless performance
11811 or memory numbers show me otherwise. */
11812
11813 dwarf2_symbol_mark_computed (attr, sym, cu);
11814 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11815
11816 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11817 cu->has_loclist = 1;
11818 }
11819
11820 /* Given a pointer to a DWARF information entry, figure out if we need
11821 to make a symbol table entry for it, and if so, create a new entry
11822 and return a pointer to it.
11823 If TYPE is NULL, determine symbol type from the die, otherwise
11824 used the passed type.
11825 If SPACE is not NULL, use it to hold the new symbol. If it is
11826 NULL, allocate a new symbol on the objfile's obstack. */
11827
11828 static struct symbol *
11829 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11830 struct symbol *space)
11831 {
11832 struct objfile *objfile = cu->objfile;
11833 struct symbol *sym = NULL;
11834 char *name;
11835 struct attribute *attr = NULL;
11836 struct attribute *attr2 = NULL;
11837 CORE_ADDR baseaddr;
11838 struct pending **list_to_add = NULL;
11839
11840 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11841
11842 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11843
11844 name = dwarf2_name (die, cu);
11845 if (name)
11846 {
11847 const char *linkagename;
11848 int suppress_add = 0;
11849
11850 if (space)
11851 sym = space;
11852 else
11853 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11854 OBJSTAT (objfile, n_syms++);
11855
11856 /* Cache this symbol's name and the name's demangled form (if any). */
11857 SYMBOL_SET_LANGUAGE (sym, cu->language);
11858 linkagename = dwarf2_physname (name, die, cu);
11859 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11860
11861 /* Fortran does not have mangling standard and the mangling does differ
11862 between gfortran, iFort etc. */
11863 if (cu->language == language_fortran
11864 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11865 symbol_set_demangled_name (&(sym->ginfo),
11866 (char *) dwarf2_full_name (name, die, cu),
11867 NULL);
11868
11869 /* Default assumptions.
11870 Use the passed type or decode it from the die. */
11871 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11872 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11873 if (type != NULL)
11874 SYMBOL_TYPE (sym) = type;
11875 else
11876 SYMBOL_TYPE (sym) = die_type (die, cu);
11877 attr = dwarf2_attr (die,
11878 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11879 cu);
11880 if (attr)
11881 {
11882 SYMBOL_LINE (sym) = DW_UNSND (attr);
11883 }
11884
11885 attr = dwarf2_attr (die,
11886 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11887 cu);
11888 if (attr)
11889 {
11890 int file_index = DW_UNSND (attr);
11891
11892 if (cu->line_header == NULL
11893 || file_index > cu->line_header->num_file_names)
11894 complaint (&symfile_complaints,
11895 _("file index out of range"));
11896 else if (file_index > 0)
11897 {
11898 struct file_entry *fe;
11899
11900 fe = &cu->line_header->file_names[file_index - 1];
11901 SYMBOL_SYMTAB (sym) = fe->symtab;
11902 }
11903 }
11904
11905 switch (die->tag)
11906 {
11907 case DW_TAG_label:
11908 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11909 if (attr)
11910 {
11911 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11912 }
11913 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11914 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11915 SYMBOL_CLASS (sym) = LOC_LABEL;
11916 add_symbol_to_list (sym, cu->list_in_scope);
11917 break;
11918 case DW_TAG_subprogram:
11919 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11920 finish_block. */
11921 SYMBOL_CLASS (sym) = LOC_BLOCK;
11922 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11923 if ((attr2 && (DW_UNSND (attr2) != 0))
11924 || cu->language == language_ada)
11925 {
11926 /* Subprograms marked external are stored as a global symbol.
11927 Ada subprograms, whether marked external or not, are always
11928 stored as a global symbol, because we want to be able to
11929 access them globally. For instance, we want to be able
11930 to break on a nested subprogram without having to
11931 specify the context. */
11932 list_to_add = &global_symbols;
11933 }
11934 else
11935 {
11936 list_to_add = cu->list_in_scope;
11937 }
11938 break;
11939 case DW_TAG_inlined_subroutine:
11940 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11941 finish_block. */
11942 SYMBOL_CLASS (sym) = LOC_BLOCK;
11943 SYMBOL_INLINED (sym) = 1;
11944 list_to_add = cu->list_in_scope;
11945 break;
11946 case DW_TAG_template_value_param:
11947 suppress_add = 1;
11948 /* Fall through. */
11949 case DW_TAG_constant:
11950 case DW_TAG_variable:
11951 case DW_TAG_member:
11952 /* Compilation with minimal debug info may result in
11953 variables with missing type entries. Change the
11954 misleading `void' type to something sensible. */
11955 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11956 SYMBOL_TYPE (sym)
11957 = objfile_type (objfile)->nodebug_data_symbol;
11958
11959 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11960 /* In the case of DW_TAG_member, we should only be called for
11961 static const members. */
11962 if (die->tag == DW_TAG_member)
11963 {
11964 /* dwarf2_add_field uses die_is_declaration,
11965 so we do the same. */
11966 gdb_assert (die_is_declaration (die, cu));
11967 gdb_assert (attr);
11968 }
11969 if (attr)
11970 {
11971 dwarf2_const_value (attr, sym, cu);
11972 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11973 if (!suppress_add)
11974 {
11975 if (attr2 && (DW_UNSND (attr2) != 0))
11976 list_to_add = &global_symbols;
11977 else
11978 list_to_add = cu->list_in_scope;
11979 }
11980 break;
11981 }
11982 attr = dwarf2_attr (die, DW_AT_location, cu);
11983 if (attr)
11984 {
11985 var_decode_location (attr, sym, cu);
11986 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11987 if (SYMBOL_CLASS (sym) == LOC_STATIC
11988 && SYMBOL_VALUE_ADDRESS (sym) == 0
11989 && !dwarf2_per_objfile->has_section_at_zero)
11990 {
11991 /* When a static variable is eliminated by the linker,
11992 the corresponding debug information is not stripped
11993 out, but the variable address is set to null;
11994 do not add such variables into symbol table. */
11995 }
11996 else if (attr2 && (DW_UNSND (attr2) != 0))
11997 {
11998 /* Workaround gfortran PR debug/40040 - it uses
11999 DW_AT_location for variables in -fPIC libraries which may
12000 get overriden by other libraries/executable and get
12001 a different address. Resolve it by the minimal symbol
12002 which may come from inferior's executable using copy
12003 relocation. Make this workaround only for gfortran as for
12004 other compilers GDB cannot guess the minimal symbol
12005 Fortran mangling kind. */
12006 if (cu->language == language_fortran && die->parent
12007 && die->parent->tag == DW_TAG_module
12008 && cu->producer
12009 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
12010 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
12011
12012 /* A variable with DW_AT_external is never static,
12013 but it may be block-scoped. */
12014 list_to_add = (cu->list_in_scope == &file_symbols
12015 ? &global_symbols : cu->list_in_scope);
12016 }
12017 else
12018 list_to_add = cu->list_in_scope;
12019 }
12020 else
12021 {
12022 /* We do not know the address of this symbol.
12023 If it is an external symbol and we have type information
12024 for it, enter the symbol as a LOC_UNRESOLVED symbol.
12025 The address of the variable will then be determined from
12026 the minimal symbol table whenever the variable is
12027 referenced. */
12028 attr2 = dwarf2_attr (die, DW_AT_external, cu);
12029 if (attr2 && (DW_UNSND (attr2) != 0)
12030 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
12031 {
12032 /* A variable with DW_AT_external is never static, but it
12033 may be block-scoped. */
12034 list_to_add = (cu->list_in_scope == &file_symbols
12035 ? &global_symbols : cu->list_in_scope);
12036
12037 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
12038 }
12039 else if (!die_is_declaration (die, cu))
12040 {
12041 /* Use the default LOC_OPTIMIZED_OUT class. */
12042 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
12043 if (!suppress_add)
12044 list_to_add = cu->list_in_scope;
12045 }
12046 }
12047 break;
12048 case DW_TAG_formal_parameter:
12049 /* If we are inside a function, mark this as an argument. If
12050 not, we might be looking at an argument to an inlined function
12051 when we do not have enough information to show inlined frames;
12052 pretend it's a local variable in that case so that the user can
12053 still see it. */
12054 if (context_stack_depth > 0
12055 && context_stack[context_stack_depth - 1].name != NULL)
12056 SYMBOL_IS_ARGUMENT (sym) = 1;
12057 attr = dwarf2_attr (die, DW_AT_location, cu);
12058 if (attr)
12059 {
12060 var_decode_location (attr, sym, cu);
12061 }
12062 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12063 if (attr)
12064 {
12065 dwarf2_const_value (attr, sym, cu);
12066 }
12067
12068 list_to_add = cu->list_in_scope;
12069 break;
12070 case DW_TAG_unspecified_parameters:
12071 /* From varargs functions; gdb doesn't seem to have any
12072 interest in this information, so just ignore it for now.
12073 (FIXME?) */
12074 break;
12075 case DW_TAG_template_type_param:
12076 suppress_add = 1;
12077 /* Fall through. */
12078 case DW_TAG_class_type:
12079 case DW_TAG_interface_type:
12080 case DW_TAG_structure_type:
12081 case DW_TAG_union_type:
12082 case DW_TAG_set_type:
12083 case DW_TAG_enumeration_type:
12084 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12085 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
12086
12087 {
12088 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
12089 really ever be static objects: otherwise, if you try
12090 to, say, break of a class's method and you're in a file
12091 which doesn't mention that class, it won't work unless
12092 the check for all static symbols in lookup_symbol_aux
12093 saves you. See the OtherFileClass tests in
12094 gdb.c++/namespace.exp. */
12095
12096 if (!suppress_add)
12097 {
12098 list_to_add = (cu->list_in_scope == &file_symbols
12099 && (cu->language == language_cplus
12100 || cu->language == language_java)
12101 ? &global_symbols : cu->list_in_scope);
12102
12103 /* The semantics of C++ state that "struct foo {
12104 ... }" also defines a typedef for "foo". A Java
12105 class declaration also defines a typedef for the
12106 class. */
12107 if (cu->language == language_cplus
12108 || cu->language == language_java
12109 || cu->language == language_ada)
12110 {
12111 /* The symbol's name is already allocated along
12112 with this objfile, so we don't need to
12113 duplicate it for the type. */
12114 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
12115 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
12116 }
12117 }
12118 }
12119 break;
12120 case DW_TAG_typedef:
12121 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12122 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
12123 list_to_add = cu->list_in_scope;
12124 break;
12125 case DW_TAG_base_type:
12126 case DW_TAG_subrange_type:
12127 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12128 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
12129 list_to_add = cu->list_in_scope;
12130 break;
12131 case DW_TAG_enumerator:
12132 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12133 if (attr)
12134 {
12135 dwarf2_const_value (attr, sym, cu);
12136 }
12137 {
12138 /* NOTE: carlton/2003-11-10: See comment above in the
12139 DW_TAG_class_type, etc. block. */
12140
12141 list_to_add = (cu->list_in_scope == &file_symbols
12142 && (cu->language == language_cplus
12143 || cu->language == language_java)
12144 ? &global_symbols : cu->list_in_scope);
12145 }
12146 break;
12147 case DW_TAG_namespace:
12148 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12149 list_to_add = &global_symbols;
12150 break;
12151 default:
12152 /* Not a tag we recognize. Hopefully we aren't processing
12153 trash data, but since we must specifically ignore things
12154 we don't recognize, there is nothing else we should do at
12155 this point. */
12156 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
12157 dwarf_tag_name (die->tag));
12158 break;
12159 }
12160
12161 if (suppress_add)
12162 {
12163 sym->hash_next = objfile->template_symbols;
12164 objfile->template_symbols = sym;
12165 list_to_add = NULL;
12166 }
12167
12168 if (list_to_add != NULL)
12169 add_symbol_to_list (sym, list_to_add);
12170
12171 /* For the benefit of old versions of GCC, check for anonymous
12172 namespaces based on the demangled name. */
12173 if (!processing_has_namespace_info
12174 && cu->language == language_cplus)
12175 cp_scan_for_anonymous_namespaces (sym, objfile);
12176 }
12177 return (sym);
12178 }
12179
12180 /* A wrapper for new_symbol_full that always allocates a new symbol. */
12181
12182 static struct symbol *
12183 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12184 {
12185 return new_symbol_full (die, type, cu, NULL);
12186 }
12187
12188 /* Given an attr with a DW_FORM_dataN value in host byte order,
12189 zero-extend it as appropriate for the symbol's type. The DWARF
12190 standard (v4) is not entirely clear about the meaning of using
12191 DW_FORM_dataN for a constant with a signed type, where the type is
12192 wider than the data. The conclusion of a discussion on the DWARF
12193 list was that this is unspecified. We choose to always zero-extend
12194 because that is the interpretation long in use by GCC. */
12195
12196 static gdb_byte *
12197 dwarf2_const_value_data (struct attribute *attr, struct type *type,
12198 const char *name, struct obstack *obstack,
12199 struct dwarf2_cu *cu, LONGEST *value, int bits)
12200 {
12201 struct objfile *objfile = cu->objfile;
12202 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12203 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
12204 LONGEST l = DW_UNSND (attr);
12205
12206 if (bits < sizeof (*value) * 8)
12207 {
12208 l &= ((LONGEST) 1 << bits) - 1;
12209 *value = l;
12210 }
12211 else if (bits == sizeof (*value) * 8)
12212 *value = l;
12213 else
12214 {
12215 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12216 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12217 return bytes;
12218 }
12219
12220 return NULL;
12221 }
12222
12223 /* Read a constant value from an attribute. Either set *VALUE, or if
12224 the value does not fit in *VALUE, set *BYTES - either already
12225 allocated on the objfile obstack, or newly allocated on OBSTACK,
12226 or, set *BATON, if we translated the constant to a location
12227 expression. */
12228
12229 static void
12230 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12231 const char *name, struct obstack *obstack,
12232 struct dwarf2_cu *cu,
12233 LONGEST *value, gdb_byte **bytes,
12234 struct dwarf2_locexpr_baton **baton)
12235 {
12236 struct objfile *objfile = cu->objfile;
12237 struct comp_unit_head *cu_header = &cu->header;
12238 struct dwarf_block *blk;
12239 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12240 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12241
12242 *value = 0;
12243 *bytes = NULL;
12244 *baton = NULL;
12245
12246 switch (attr->form)
12247 {
12248 case DW_FORM_addr:
12249 {
12250 gdb_byte *data;
12251
12252 if (TYPE_LENGTH (type) != cu_header->addr_size)
12253 dwarf2_const_value_length_mismatch_complaint (name,
12254 cu_header->addr_size,
12255 TYPE_LENGTH (type));
12256 /* Symbols of this form are reasonably rare, so we just
12257 piggyback on the existing location code rather than writing
12258 a new implementation of symbol_computed_ops. */
12259 *baton = obstack_alloc (&objfile->objfile_obstack,
12260 sizeof (struct dwarf2_locexpr_baton));
12261 (*baton)->per_cu = cu->per_cu;
12262 gdb_assert ((*baton)->per_cu);
12263
12264 (*baton)->size = 2 + cu_header->addr_size;
12265 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12266 (*baton)->data = data;
12267
12268 data[0] = DW_OP_addr;
12269 store_unsigned_integer (&data[1], cu_header->addr_size,
12270 byte_order, DW_ADDR (attr));
12271 data[cu_header->addr_size + 1] = DW_OP_stack_value;
12272 }
12273 break;
12274 case DW_FORM_string:
12275 case DW_FORM_strp:
12276 /* DW_STRING is already allocated on the objfile obstack, point
12277 directly to it. */
12278 *bytes = (gdb_byte *) DW_STRING (attr);
12279 break;
12280 case DW_FORM_block1:
12281 case DW_FORM_block2:
12282 case DW_FORM_block4:
12283 case DW_FORM_block:
12284 case DW_FORM_exprloc:
12285 blk = DW_BLOCK (attr);
12286 if (TYPE_LENGTH (type) != blk->size)
12287 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12288 TYPE_LENGTH (type));
12289 *bytes = blk->data;
12290 break;
12291
12292 /* The DW_AT_const_value attributes are supposed to carry the
12293 symbol's value "represented as it would be on the target
12294 architecture." By the time we get here, it's already been
12295 converted to host endianness, so we just need to sign- or
12296 zero-extend it as appropriate. */
12297 case DW_FORM_data1:
12298 *bytes = dwarf2_const_value_data (attr, type, name,
12299 obstack, cu, value, 8);
12300 break;
12301 case DW_FORM_data2:
12302 *bytes = dwarf2_const_value_data (attr, type, name,
12303 obstack, cu, value, 16);
12304 break;
12305 case DW_FORM_data4:
12306 *bytes = dwarf2_const_value_data (attr, type, name,
12307 obstack, cu, value, 32);
12308 break;
12309 case DW_FORM_data8:
12310 *bytes = dwarf2_const_value_data (attr, type, name,
12311 obstack, cu, value, 64);
12312 break;
12313
12314 case DW_FORM_sdata:
12315 *value = DW_SND (attr);
12316 break;
12317
12318 case DW_FORM_udata:
12319 *value = DW_UNSND (attr);
12320 break;
12321
12322 default:
12323 complaint (&symfile_complaints,
12324 _("unsupported const value attribute form: '%s'"),
12325 dwarf_form_name (attr->form));
12326 *value = 0;
12327 break;
12328 }
12329 }
12330
12331
12332 /* Copy constant value from an attribute to a symbol. */
12333
12334 static void
12335 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12336 struct dwarf2_cu *cu)
12337 {
12338 struct objfile *objfile = cu->objfile;
12339 struct comp_unit_head *cu_header = &cu->header;
12340 LONGEST value;
12341 gdb_byte *bytes;
12342 struct dwarf2_locexpr_baton *baton;
12343
12344 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12345 SYMBOL_PRINT_NAME (sym),
12346 &objfile->objfile_obstack, cu,
12347 &value, &bytes, &baton);
12348
12349 if (baton != NULL)
12350 {
12351 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12352 SYMBOL_LOCATION_BATON (sym) = baton;
12353 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12354 }
12355 else if (bytes != NULL)
12356 {
12357 SYMBOL_VALUE_BYTES (sym) = bytes;
12358 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12359 }
12360 else
12361 {
12362 SYMBOL_VALUE (sym) = value;
12363 SYMBOL_CLASS (sym) = LOC_CONST;
12364 }
12365 }
12366
12367 /* Return the type of the die in question using its DW_AT_type attribute. */
12368
12369 static struct type *
12370 die_type (struct die_info *die, struct dwarf2_cu *cu)
12371 {
12372 struct attribute *type_attr;
12373
12374 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12375 if (!type_attr)
12376 {
12377 /* A missing DW_AT_type represents a void type. */
12378 return objfile_type (cu->objfile)->builtin_void;
12379 }
12380
12381 return lookup_die_type (die, type_attr, cu);
12382 }
12383
12384 /* True iff CU's producer generates GNAT Ada auxiliary information
12385 that allows to find parallel types through that information instead
12386 of having to do expensive parallel lookups by type name. */
12387
12388 static int
12389 need_gnat_info (struct dwarf2_cu *cu)
12390 {
12391 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12392 of GNAT produces this auxiliary information, without any indication
12393 that it is produced. Part of enhancing the FSF version of GNAT
12394 to produce that information will be to put in place an indicator
12395 that we can use in order to determine whether the descriptive type
12396 info is available or not. One suggestion that has been made is
12397 to use a new attribute, attached to the CU die. For now, assume
12398 that the descriptive type info is not available. */
12399 return 0;
12400 }
12401
12402 /* Return the auxiliary type of the die in question using its
12403 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12404 attribute is not present. */
12405
12406 static struct type *
12407 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12408 {
12409 struct attribute *type_attr;
12410
12411 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12412 if (!type_attr)
12413 return NULL;
12414
12415 return lookup_die_type (die, type_attr, cu);
12416 }
12417
12418 /* If DIE has a descriptive_type attribute, then set the TYPE's
12419 descriptive type accordingly. */
12420
12421 static void
12422 set_descriptive_type (struct type *type, struct die_info *die,
12423 struct dwarf2_cu *cu)
12424 {
12425 struct type *descriptive_type = die_descriptive_type (die, cu);
12426
12427 if (descriptive_type)
12428 {
12429 ALLOCATE_GNAT_AUX_TYPE (type);
12430 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12431 }
12432 }
12433
12434 /* Return the containing type of the die in question using its
12435 DW_AT_containing_type attribute. */
12436
12437 static struct type *
12438 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12439 {
12440 struct attribute *type_attr;
12441
12442 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12443 if (!type_attr)
12444 error (_("Dwarf Error: Problem turning containing type into gdb type "
12445 "[in module %s]"), cu->objfile->name);
12446
12447 return lookup_die_type (die, type_attr, cu);
12448 }
12449
12450 /* Look up the type of DIE in CU using its type attribute ATTR.
12451 If there is no type substitute an error marker. */
12452
12453 static struct type *
12454 lookup_die_type (struct die_info *die, struct attribute *attr,
12455 struct dwarf2_cu *cu)
12456 {
12457 struct objfile *objfile = cu->objfile;
12458 struct type *this_type;
12459
12460 /* First see if we have it cached. */
12461
12462 if (is_ref_attr (attr))
12463 {
12464 sect_offset offset = dwarf2_get_ref_die_offset (attr);
12465
12466 this_type = get_die_type_at_offset (offset, cu->per_cu);
12467 }
12468 else if (attr->form == DW_FORM_ref_sig8)
12469 {
12470 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12471 struct dwarf2_cu *sig_cu;
12472 sect_offset offset;
12473
12474 /* sig_type will be NULL if the signatured type is missing from
12475 the debug info. */
12476 if (sig_type == NULL)
12477 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12478 "at 0x%x [in module %s]"),
12479 die->offset.sect_off, objfile->name);
12480
12481 gdb_assert (sig_type->per_cu.debug_types_section);
12482 offset.sect_off = (sig_type->per_cu.offset.sect_off
12483 + sig_type->type_offset.cu_off);
12484 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12485 }
12486 else
12487 {
12488 dump_die_for_error (die);
12489 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12490 dwarf_attr_name (attr->name), objfile->name);
12491 }
12492
12493 /* If not cached we need to read it in. */
12494
12495 if (this_type == NULL)
12496 {
12497 struct die_info *type_die;
12498 struct dwarf2_cu *type_cu = cu;
12499
12500 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12501 /* If the type is cached, we should have found it above. */
12502 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12503 this_type = read_type_die_1 (type_die, type_cu);
12504 }
12505
12506 /* If we still don't have a type use an error marker. */
12507
12508 if (this_type == NULL)
12509 {
12510 char *message, *saved;
12511
12512 /* read_type_die already issued a complaint. */
12513 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12514 objfile->name,
12515 cu->header.offset.sect_off,
12516 die->offset.sect_off);
12517 saved = obstack_copy0 (&objfile->objfile_obstack,
12518 message, strlen (message));
12519 xfree (message);
12520
12521 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12522 }
12523
12524 return this_type;
12525 }
12526
12527 /* Return the type in DIE, CU.
12528 Returns NULL for invalid types.
12529
12530 This first does a lookup in the appropriate type_hash table,
12531 and only reads the die in if necessary.
12532
12533 NOTE: This can be called when reading in partial or full symbols. */
12534
12535 static struct type *
12536 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12537 {
12538 struct type *this_type;
12539
12540 this_type = get_die_type (die, cu);
12541 if (this_type)
12542 return this_type;
12543
12544 return read_type_die_1 (die, cu);
12545 }
12546
12547 /* Read the type in DIE, CU.
12548 Returns NULL for invalid types. */
12549
12550 static struct type *
12551 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12552 {
12553 struct type *this_type = NULL;
12554
12555 switch (die->tag)
12556 {
12557 case DW_TAG_class_type:
12558 case DW_TAG_interface_type:
12559 case DW_TAG_structure_type:
12560 case DW_TAG_union_type:
12561 this_type = read_structure_type (die, cu);
12562 break;
12563 case DW_TAG_enumeration_type:
12564 this_type = read_enumeration_type (die, cu);
12565 break;
12566 case DW_TAG_subprogram:
12567 case DW_TAG_subroutine_type:
12568 case DW_TAG_inlined_subroutine:
12569 this_type = read_subroutine_type (die, cu);
12570 break;
12571 case DW_TAG_array_type:
12572 this_type = read_array_type (die, cu);
12573 break;
12574 case DW_TAG_set_type:
12575 this_type = read_set_type (die, cu);
12576 break;
12577 case DW_TAG_pointer_type:
12578 this_type = read_tag_pointer_type (die, cu);
12579 break;
12580 case DW_TAG_ptr_to_member_type:
12581 this_type = read_tag_ptr_to_member_type (die, cu);
12582 break;
12583 case DW_TAG_reference_type:
12584 this_type = read_tag_reference_type (die, cu);
12585 break;
12586 case DW_TAG_const_type:
12587 this_type = read_tag_const_type (die, cu);
12588 break;
12589 case DW_TAG_volatile_type:
12590 this_type = read_tag_volatile_type (die, cu);
12591 break;
12592 case DW_TAG_string_type:
12593 this_type = read_tag_string_type (die, cu);
12594 break;
12595 case DW_TAG_typedef:
12596 this_type = read_typedef (die, cu);
12597 break;
12598 case DW_TAG_subrange_type:
12599 this_type = read_subrange_type (die, cu);
12600 break;
12601 case DW_TAG_base_type:
12602 this_type = read_base_type (die, cu);
12603 break;
12604 case DW_TAG_unspecified_type:
12605 this_type = read_unspecified_type (die, cu);
12606 break;
12607 case DW_TAG_namespace:
12608 this_type = read_namespace_type (die, cu);
12609 break;
12610 case DW_TAG_module:
12611 this_type = read_module_type (die, cu);
12612 break;
12613 default:
12614 complaint (&symfile_complaints,
12615 _("unexpected tag in read_type_die: '%s'"),
12616 dwarf_tag_name (die->tag));
12617 break;
12618 }
12619
12620 return this_type;
12621 }
12622
12623 /* See if we can figure out if the class lives in a namespace. We do
12624 this by looking for a member function; its demangled name will
12625 contain namespace info, if there is any.
12626 Return the computed name or NULL.
12627 Space for the result is allocated on the objfile's obstack.
12628 This is the full-die version of guess_partial_die_structure_name.
12629 In this case we know DIE has no useful parent. */
12630
12631 static char *
12632 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12633 {
12634 struct die_info *spec_die;
12635 struct dwarf2_cu *spec_cu;
12636 struct die_info *child;
12637
12638 spec_cu = cu;
12639 spec_die = die_specification (die, &spec_cu);
12640 if (spec_die != NULL)
12641 {
12642 die = spec_die;
12643 cu = spec_cu;
12644 }
12645
12646 for (child = die->child;
12647 child != NULL;
12648 child = child->sibling)
12649 {
12650 if (child->tag == DW_TAG_subprogram)
12651 {
12652 struct attribute *attr;
12653
12654 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12655 if (attr == NULL)
12656 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12657 if (attr != NULL)
12658 {
12659 char *actual_name
12660 = language_class_name_from_physname (cu->language_defn,
12661 DW_STRING (attr));
12662 char *name = NULL;
12663
12664 if (actual_name != NULL)
12665 {
12666 char *die_name = dwarf2_name (die, cu);
12667
12668 if (die_name != NULL
12669 && strcmp (die_name, actual_name) != 0)
12670 {
12671 /* Strip off the class name from the full name.
12672 We want the prefix. */
12673 int die_name_len = strlen (die_name);
12674 int actual_name_len = strlen (actual_name);
12675
12676 /* Test for '::' as a sanity check. */
12677 if (actual_name_len > die_name_len + 2
12678 && actual_name[actual_name_len
12679 - die_name_len - 1] == ':')
12680 name =
12681 obsavestring (actual_name,
12682 actual_name_len - die_name_len - 2,
12683 &cu->objfile->objfile_obstack);
12684 }
12685 }
12686 xfree (actual_name);
12687 return name;
12688 }
12689 }
12690 }
12691
12692 return NULL;
12693 }
12694
12695 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12696 prefix part in such case. See
12697 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12698
12699 static char *
12700 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12701 {
12702 struct attribute *attr;
12703 char *base;
12704
12705 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12706 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12707 return NULL;
12708
12709 attr = dwarf2_attr (die, DW_AT_name, cu);
12710 if (attr != NULL && DW_STRING (attr) != NULL)
12711 return NULL;
12712
12713 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12714 if (attr == NULL)
12715 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12716 if (attr == NULL || DW_STRING (attr) == NULL)
12717 return NULL;
12718
12719 /* dwarf2_name had to be already called. */
12720 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12721
12722 /* Strip the base name, keep any leading namespaces/classes. */
12723 base = strrchr (DW_STRING (attr), ':');
12724 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12725 return "";
12726
12727 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12728 &cu->objfile->objfile_obstack);
12729 }
12730
12731 /* Return the name of the namespace/class that DIE is defined within,
12732 or "" if we can't tell. The caller should not xfree the result.
12733
12734 For example, if we're within the method foo() in the following
12735 code:
12736
12737 namespace N {
12738 class C {
12739 void foo () {
12740 }
12741 };
12742 }
12743
12744 then determine_prefix on foo's die will return "N::C". */
12745
12746 static const char *
12747 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12748 {
12749 struct die_info *parent, *spec_die;
12750 struct dwarf2_cu *spec_cu;
12751 struct type *parent_type;
12752 char *retval;
12753
12754 if (cu->language != language_cplus && cu->language != language_java
12755 && cu->language != language_fortran)
12756 return "";
12757
12758 retval = anonymous_struct_prefix (die, cu);
12759 if (retval)
12760 return retval;
12761
12762 /* We have to be careful in the presence of DW_AT_specification.
12763 For example, with GCC 3.4, given the code
12764
12765 namespace N {
12766 void foo() {
12767 // Definition of N::foo.
12768 }
12769 }
12770
12771 then we'll have a tree of DIEs like this:
12772
12773 1: DW_TAG_compile_unit
12774 2: DW_TAG_namespace // N
12775 3: DW_TAG_subprogram // declaration of N::foo
12776 4: DW_TAG_subprogram // definition of N::foo
12777 DW_AT_specification // refers to die #3
12778
12779 Thus, when processing die #4, we have to pretend that we're in
12780 the context of its DW_AT_specification, namely the contex of die
12781 #3. */
12782 spec_cu = cu;
12783 spec_die = die_specification (die, &spec_cu);
12784 if (spec_die == NULL)
12785 parent = die->parent;
12786 else
12787 {
12788 parent = spec_die->parent;
12789 cu = spec_cu;
12790 }
12791
12792 if (parent == NULL)
12793 return "";
12794 else if (parent->building_fullname)
12795 {
12796 const char *name;
12797 const char *parent_name;
12798
12799 /* It has been seen on RealView 2.2 built binaries,
12800 DW_TAG_template_type_param types actually _defined_ as
12801 children of the parent class:
12802
12803 enum E {};
12804 template class <class Enum> Class{};
12805 Class<enum E> class_e;
12806
12807 1: DW_TAG_class_type (Class)
12808 2: DW_TAG_enumeration_type (E)
12809 3: DW_TAG_enumerator (enum1:0)
12810 3: DW_TAG_enumerator (enum2:1)
12811 ...
12812 2: DW_TAG_template_type_param
12813 DW_AT_type DW_FORM_ref_udata (E)
12814
12815 Besides being broken debug info, it can put GDB into an
12816 infinite loop. Consider:
12817
12818 When we're building the full name for Class<E>, we'll start
12819 at Class, and go look over its template type parameters,
12820 finding E. We'll then try to build the full name of E, and
12821 reach here. We're now trying to build the full name of E,
12822 and look over the parent DIE for containing scope. In the
12823 broken case, if we followed the parent DIE of E, we'd again
12824 find Class, and once again go look at its template type
12825 arguments, etc., etc. Simply don't consider such parent die
12826 as source-level parent of this die (it can't be, the language
12827 doesn't allow it), and break the loop here. */
12828 name = dwarf2_name (die, cu);
12829 parent_name = dwarf2_name (parent, cu);
12830 complaint (&symfile_complaints,
12831 _("template param type '%s' defined within parent '%s'"),
12832 name ? name : "<unknown>",
12833 parent_name ? parent_name : "<unknown>");
12834 return "";
12835 }
12836 else
12837 switch (parent->tag)
12838 {
12839 case DW_TAG_namespace:
12840 parent_type = read_type_die (parent, cu);
12841 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12842 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12843 Work around this problem here. */
12844 if (cu->language == language_cplus
12845 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12846 return "";
12847 /* We give a name to even anonymous namespaces. */
12848 return TYPE_TAG_NAME (parent_type);
12849 case DW_TAG_class_type:
12850 case DW_TAG_interface_type:
12851 case DW_TAG_structure_type:
12852 case DW_TAG_union_type:
12853 case DW_TAG_module:
12854 parent_type = read_type_die (parent, cu);
12855 if (TYPE_TAG_NAME (parent_type) != NULL)
12856 return TYPE_TAG_NAME (parent_type);
12857 else
12858 /* An anonymous structure is only allowed non-static data
12859 members; no typedefs, no member functions, et cetera.
12860 So it does not need a prefix. */
12861 return "";
12862 case DW_TAG_compile_unit:
12863 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12864 if (cu->language == language_cplus
12865 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12866 && die->child != NULL
12867 && (die->tag == DW_TAG_class_type
12868 || die->tag == DW_TAG_structure_type
12869 || die->tag == DW_TAG_union_type))
12870 {
12871 char *name = guess_full_die_structure_name (die, cu);
12872 if (name != NULL)
12873 return name;
12874 }
12875 return "";
12876 default:
12877 return determine_prefix (parent, cu);
12878 }
12879 }
12880
12881 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12882 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12883 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12884 an obconcat, otherwise allocate storage for the result. The CU argument is
12885 used to determine the language and hence, the appropriate separator. */
12886
12887 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12888
12889 static char *
12890 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12891 int physname, struct dwarf2_cu *cu)
12892 {
12893 const char *lead = "";
12894 const char *sep;
12895
12896 if (suffix == NULL || suffix[0] == '\0'
12897 || prefix == NULL || prefix[0] == '\0')
12898 sep = "";
12899 else if (cu->language == language_java)
12900 sep = ".";
12901 else if (cu->language == language_fortran && physname)
12902 {
12903 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12904 DW_AT_MIPS_linkage_name is preferred and used instead. */
12905
12906 lead = "__";
12907 sep = "_MOD_";
12908 }
12909 else
12910 sep = "::";
12911
12912 if (prefix == NULL)
12913 prefix = "";
12914 if (suffix == NULL)
12915 suffix = "";
12916
12917 if (obs == NULL)
12918 {
12919 char *retval
12920 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12921
12922 strcpy (retval, lead);
12923 strcat (retval, prefix);
12924 strcat (retval, sep);
12925 strcat (retval, suffix);
12926 return retval;
12927 }
12928 else
12929 {
12930 /* We have an obstack. */
12931 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12932 }
12933 }
12934
12935 /* Return sibling of die, NULL if no sibling. */
12936
12937 static struct die_info *
12938 sibling_die (struct die_info *die)
12939 {
12940 return die->sibling;
12941 }
12942
12943 /* Get name of a die, return NULL if not found. */
12944
12945 static char *
12946 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12947 struct obstack *obstack)
12948 {
12949 if (name && cu->language == language_cplus)
12950 {
12951 char *canon_name = cp_canonicalize_string (name);
12952
12953 if (canon_name != NULL)
12954 {
12955 if (strcmp (canon_name, name) != 0)
12956 name = obsavestring (canon_name, strlen (canon_name),
12957 obstack);
12958 xfree (canon_name);
12959 }
12960 }
12961
12962 return name;
12963 }
12964
12965 /* Get name of a die, return NULL if not found. */
12966
12967 static char *
12968 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12969 {
12970 struct attribute *attr;
12971
12972 attr = dwarf2_attr (die, DW_AT_name, cu);
12973 if ((!attr || !DW_STRING (attr))
12974 && die->tag != DW_TAG_class_type
12975 && die->tag != DW_TAG_interface_type
12976 && die->tag != DW_TAG_structure_type
12977 && die->tag != DW_TAG_union_type)
12978 return NULL;
12979
12980 switch (die->tag)
12981 {
12982 case DW_TAG_compile_unit:
12983 /* Compilation units have a DW_AT_name that is a filename, not
12984 a source language identifier. */
12985 case DW_TAG_enumeration_type:
12986 case DW_TAG_enumerator:
12987 /* These tags always have simple identifiers already; no need
12988 to canonicalize them. */
12989 return DW_STRING (attr);
12990
12991 case DW_TAG_subprogram:
12992 /* Java constructors will all be named "<init>", so return
12993 the class name when we see this special case. */
12994 if (cu->language == language_java
12995 && DW_STRING (attr) != NULL
12996 && strcmp (DW_STRING (attr), "<init>") == 0)
12997 {
12998 struct dwarf2_cu *spec_cu = cu;
12999 struct die_info *spec_die;
13000
13001 /* GCJ will output '<init>' for Java constructor names.
13002 For this special case, return the name of the parent class. */
13003
13004 /* GCJ may output suprogram DIEs with AT_specification set.
13005 If so, use the name of the specified DIE. */
13006 spec_die = die_specification (die, &spec_cu);
13007 if (spec_die != NULL)
13008 return dwarf2_name (spec_die, spec_cu);
13009
13010 do
13011 {
13012 die = die->parent;
13013 if (die->tag == DW_TAG_class_type)
13014 return dwarf2_name (die, cu);
13015 }
13016 while (die->tag != DW_TAG_compile_unit);
13017 }
13018 break;
13019
13020 case DW_TAG_class_type:
13021 case DW_TAG_interface_type:
13022 case DW_TAG_structure_type:
13023 case DW_TAG_union_type:
13024 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
13025 structures or unions. These were of the form "._%d" in GCC 4.1,
13026 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
13027 and GCC 4.4. We work around this problem by ignoring these. */
13028 if (attr && DW_STRING (attr)
13029 && (strncmp (DW_STRING (attr), "._", 2) == 0
13030 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
13031 return NULL;
13032
13033 /* GCC might emit a nameless typedef that has a linkage name. See
13034 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13035 if (!attr || DW_STRING (attr) == NULL)
13036 {
13037 char *demangled = NULL;
13038
13039 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
13040 if (attr == NULL)
13041 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
13042
13043 if (attr == NULL || DW_STRING (attr) == NULL)
13044 return NULL;
13045
13046 /* Avoid demangling DW_STRING (attr) the second time on a second
13047 call for the same DIE. */
13048 if (!DW_STRING_IS_CANONICAL (attr))
13049 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
13050
13051 if (demangled)
13052 {
13053 char *base;
13054
13055 /* FIXME: we already did this for the partial symbol... */
13056 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
13057 &cu->objfile->objfile_obstack);
13058 DW_STRING_IS_CANONICAL (attr) = 1;
13059 xfree (demangled);
13060
13061 /* Strip any leading namespaces/classes, keep only the base name.
13062 DW_AT_name for named DIEs does not contain the prefixes. */
13063 base = strrchr (DW_STRING (attr), ':');
13064 if (base && base > DW_STRING (attr) && base[-1] == ':')
13065 return &base[1];
13066 else
13067 return DW_STRING (attr);
13068 }
13069 }
13070 break;
13071
13072 default:
13073 break;
13074 }
13075
13076 if (!DW_STRING_IS_CANONICAL (attr))
13077 {
13078 DW_STRING (attr)
13079 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
13080 &cu->objfile->objfile_obstack);
13081 DW_STRING_IS_CANONICAL (attr) = 1;
13082 }
13083 return DW_STRING (attr);
13084 }
13085
13086 /* Return the die that this die in an extension of, or NULL if there
13087 is none. *EXT_CU is the CU containing DIE on input, and the CU
13088 containing the return value on output. */
13089
13090 static struct die_info *
13091 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
13092 {
13093 struct attribute *attr;
13094
13095 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
13096 if (attr == NULL)
13097 return NULL;
13098
13099 return follow_die_ref (die, attr, ext_cu);
13100 }
13101
13102 /* Convert a DIE tag into its string name. */
13103
13104 static char *
13105 dwarf_tag_name (unsigned tag)
13106 {
13107 switch (tag)
13108 {
13109 case DW_TAG_padding:
13110 return "DW_TAG_padding";
13111 case DW_TAG_array_type:
13112 return "DW_TAG_array_type";
13113 case DW_TAG_class_type:
13114 return "DW_TAG_class_type";
13115 case DW_TAG_entry_point:
13116 return "DW_TAG_entry_point";
13117 case DW_TAG_enumeration_type:
13118 return "DW_TAG_enumeration_type";
13119 case DW_TAG_formal_parameter:
13120 return "DW_TAG_formal_parameter";
13121 case DW_TAG_imported_declaration:
13122 return "DW_TAG_imported_declaration";
13123 case DW_TAG_label:
13124 return "DW_TAG_label";
13125 case DW_TAG_lexical_block:
13126 return "DW_TAG_lexical_block";
13127 case DW_TAG_member:
13128 return "DW_TAG_member";
13129 case DW_TAG_pointer_type:
13130 return "DW_TAG_pointer_type";
13131 case DW_TAG_reference_type:
13132 return "DW_TAG_reference_type";
13133 case DW_TAG_compile_unit:
13134 return "DW_TAG_compile_unit";
13135 case DW_TAG_string_type:
13136 return "DW_TAG_string_type";
13137 case DW_TAG_structure_type:
13138 return "DW_TAG_structure_type";
13139 case DW_TAG_subroutine_type:
13140 return "DW_TAG_subroutine_type";
13141 case DW_TAG_typedef:
13142 return "DW_TAG_typedef";
13143 case DW_TAG_union_type:
13144 return "DW_TAG_union_type";
13145 case DW_TAG_unspecified_parameters:
13146 return "DW_TAG_unspecified_parameters";
13147 case DW_TAG_variant:
13148 return "DW_TAG_variant";
13149 case DW_TAG_common_block:
13150 return "DW_TAG_common_block";
13151 case DW_TAG_common_inclusion:
13152 return "DW_TAG_common_inclusion";
13153 case DW_TAG_inheritance:
13154 return "DW_TAG_inheritance";
13155 case DW_TAG_inlined_subroutine:
13156 return "DW_TAG_inlined_subroutine";
13157 case DW_TAG_module:
13158 return "DW_TAG_module";
13159 case DW_TAG_ptr_to_member_type:
13160 return "DW_TAG_ptr_to_member_type";
13161 case DW_TAG_set_type:
13162 return "DW_TAG_set_type";
13163 case DW_TAG_subrange_type:
13164 return "DW_TAG_subrange_type";
13165 case DW_TAG_with_stmt:
13166 return "DW_TAG_with_stmt";
13167 case DW_TAG_access_declaration:
13168 return "DW_TAG_access_declaration";
13169 case DW_TAG_base_type:
13170 return "DW_TAG_base_type";
13171 case DW_TAG_catch_block:
13172 return "DW_TAG_catch_block";
13173 case DW_TAG_const_type:
13174 return "DW_TAG_const_type";
13175 case DW_TAG_constant:
13176 return "DW_TAG_constant";
13177 case DW_TAG_enumerator:
13178 return "DW_TAG_enumerator";
13179 case DW_TAG_file_type:
13180 return "DW_TAG_file_type";
13181 case DW_TAG_friend:
13182 return "DW_TAG_friend";
13183 case DW_TAG_namelist:
13184 return "DW_TAG_namelist";
13185 case DW_TAG_namelist_item:
13186 return "DW_TAG_namelist_item";
13187 case DW_TAG_packed_type:
13188 return "DW_TAG_packed_type";
13189 case DW_TAG_subprogram:
13190 return "DW_TAG_subprogram";
13191 case DW_TAG_template_type_param:
13192 return "DW_TAG_template_type_param";
13193 case DW_TAG_template_value_param:
13194 return "DW_TAG_template_value_param";
13195 case DW_TAG_thrown_type:
13196 return "DW_TAG_thrown_type";
13197 case DW_TAG_try_block:
13198 return "DW_TAG_try_block";
13199 case DW_TAG_variant_part:
13200 return "DW_TAG_variant_part";
13201 case DW_TAG_variable:
13202 return "DW_TAG_variable";
13203 case DW_TAG_volatile_type:
13204 return "DW_TAG_volatile_type";
13205 case DW_TAG_dwarf_procedure:
13206 return "DW_TAG_dwarf_procedure";
13207 case DW_TAG_restrict_type:
13208 return "DW_TAG_restrict_type";
13209 case DW_TAG_interface_type:
13210 return "DW_TAG_interface_type";
13211 case DW_TAG_namespace:
13212 return "DW_TAG_namespace";
13213 case DW_TAG_imported_module:
13214 return "DW_TAG_imported_module";
13215 case DW_TAG_unspecified_type:
13216 return "DW_TAG_unspecified_type";
13217 case DW_TAG_partial_unit:
13218 return "DW_TAG_partial_unit";
13219 case DW_TAG_imported_unit:
13220 return "DW_TAG_imported_unit";
13221 case DW_TAG_condition:
13222 return "DW_TAG_condition";
13223 case DW_TAG_shared_type:
13224 return "DW_TAG_shared_type";
13225 case DW_TAG_type_unit:
13226 return "DW_TAG_type_unit";
13227 case DW_TAG_MIPS_loop:
13228 return "DW_TAG_MIPS_loop";
13229 case DW_TAG_HP_array_descriptor:
13230 return "DW_TAG_HP_array_descriptor";
13231 case DW_TAG_format_label:
13232 return "DW_TAG_format_label";
13233 case DW_TAG_function_template:
13234 return "DW_TAG_function_template";
13235 case DW_TAG_class_template:
13236 return "DW_TAG_class_template";
13237 case DW_TAG_GNU_BINCL:
13238 return "DW_TAG_GNU_BINCL";
13239 case DW_TAG_GNU_EINCL:
13240 return "DW_TAG_GNU_EINCL";
13241 case DW_TAG_upc_shared_type:
13242 return "DW_TAG_upc_shared_type";
13243 case DW_TAG_upc_strict_type:
13244 return "DW_TAG_upc_strict_type";
13245 case DW_TAG_upc_relaxed_type:
13246 return "DW_TAG_upc_relaxed_type";
13247 case DW_TAG_PGI_kanji_type:
13248 return "DW_TAG_PGI_kanji_type";
13249 case DW_TAG_PGI_interface_block:
13250 return "DW_TAG_PGI_interface_block";
13251 case DW_TAG_GNU_call_site:
13252 return "DW_TAG_GNU_call_site";
13253 default:
13254 return "DW_TAG_<unknown>";
13255 }
13256 }
13257
13258 /* Convert a DWARF attribute code into its string name. */
13259
13260 static char *
13261 dwarf_attr_name (unsigned attr)
13262 {
13263 switch (attr)
13264 {
13265 case DW_AT_sibling:
13266 return "DW_AT_sibling";
13267 case DW_AT_location:
13268 return "DW_AT_location";
13269 case DW_AT_name:
13270 return "DW_AT_name";
13271 case DW_AT_ordering:
13272 return "DW_AT_ordering";
13273 case DW_AT_subscr_data:
13274 return "DW_AT_subscr_data";
13275 case DW_AT_byte_size:
13276 return "DW_AT_byte_size";
13277 case DW_AT_bit_offset:
13278 return "DW_AT_bit_offset";
13279 case DW_AT_bit_size:
13280 return "DW_AT_bit_size";
13281 case DW_AT_element_list:
13282 return "DW_AT_element_list";
13283 case DW_AT_stmt_list:
13284 return "DW_AT_stmt_list";
13285 case DW_AT_low_pc:
13286 return "DW_AT_low_pc";
13287 case DW_AT_high_pc:
13288 return "DW_AT_high_pc";
13289 case DW_AT_language:
13290 return "DW_AT_language";
13291 case DW_AT_member:
13292 return "DW_AT_member";
13293 case DW_AT_discr:
13294 return "DW_AT_discr";
13295 case DW_AT_discr_value:
13296 return "DW_AT_discr_value";
13297 case DW_AT_visibility:
13298 return "DW_AT_visibility";
13299 case DW_AT_import:
13300 return "DW_AT_import";
13301 case DW_AT_string_length:
13302 return "DW_AT_string_length";
13303 case DW_AT_common_reference:
13304 return "DW_AT_common_reference";
13305 case DW_AT_comp_dir:
13306 return "DW_AT_comp_dir";
13307 case DW_AT_const_value:
13308 return "DW_AT_const_value";
13309 case DW_AT_containing_type:
13310 return "DW_AT_containing_type";
13311 case DW_AT_default_value:
13312 return "DW_AT_default_value";
13313 case DW_AT_inline:
13314 return "DW_AT_inline";
13315 case DW_AT_is_optional:
13316 return "DW_AT_is_optional";
13317 case DW_AT_lower_bound:
13318 return "DW_AT_lower_bound";
13319 case DW_AT_producer:
13320 return "DW_AT_producer";
13321 case DW_AT_prototyped:
13322 return "DW_AT_prototyped";
13323 case DW_AT_return_addr:
13324 return "DW_AT_return_addr";
13325 case DW_AT_start_scope:
13326 return "DW_AT_start_scope";
13327 case DW_AT_bit_stride:
13328 return "DW_AT_bit_stride";
13329 case DW_AT_upper_bound:
13330 return "DW_AT_upper_bound";
13331 case DW_AT_abstract_origin:
13332 return "DW_AT_abstract_origin";
13333 case DW_AT_accessibility:
13334 return "DW_AT_accessibility";
13335 case DW_AT_address_class:
13336 return "DW_AT_address_class";
13337 case DW_AT_artificial:
13338 return "DW_AT_artificial";
13339 case DW_AT_base_types:
13340 return "DW_AT_base_types";
13341 case DW_AT_calling_convention:
13342 return "DW_AT_calling_convention";
13343 case DW_AT_count:
13344 return "DW_AT_count";
13345 case DW_AT_data_member_location:
13346 return "DW_AT_data_member_location";
13347 case DW_AT_decl_column:
13348 return "DW_AT_decl_column";
13349 case DW_AT_decl_file:
13350 return "DW_AT_decl_file";
13351 case DW_AT_decl_line:
13352 return "DW_AT_decl_line";
13353 case DW_AT_declaration:
13354 return "DW_AT_declaration";
13355 case DW_AT_discr_list:
13356 return "DW_AT_discr_list";
13357 case DW_AT_encoding:
13358 return "DW_AT_encoding";
13359 case DW_AT_external:
13360 return "DW_AT_external";
13361 case DW_AT_frame_base:
13362 return "DW_AT_frame_base";
13363 case DW_AT_friend:
13364 return "DW_AT_friend";
13365 case DW_AT_identifier_case:
13366 return "DW_AT_identifier_case";
13367 case DW_AT_macro_info:
13368 return "DW_AT_macro_info";
13369 case DW_AT_namelist_items:
13370 return "DW_AT_namelist_items";
13371 case DW_AT_priority:
13372 return "DW_AT_priority";
13373 case DW_AT_segment:
13374 return "DW_AT_segment";
13375 case DW_AT_specification:
13376 return "DW_AT_specification";
13377 case DW_AT_static_link:
13378 return "DW_AT_static_link";
13379 case DW_AT_type:
13380 return "DW_AT_type";
13381 case DW_AT_use_location:
13382 return "DW_AT_use_location";
13383 case DW_AT_variable_parameter:
13384 return "DW_AT_variable_parameter";
13385 case DW_AT_virtuality:
13386 return "DW_AT_virtuality";
13387 case DW_AT_vtable_elem_location:
13388 return "DW_AT_vtable_elem_location";
13389 /* DWARF 3 values. */
13390 case DW_AT_allocated:
13391 return "DW_AT_allocated";
13392 case DW_AT_associated:
13393 return "DW_AT_associated";
13394 case DW_AT_data_location:
13395 return "DW_AT_data_location";
13396 case DW_AT_byte_stride:
13397 return "DW_AT_byte_stride";
13398 case DW_AT_entry_pc:
13399 return "DW_AT_entry_pc";
13400 case DW_AT_use_UTF8:
13401 return "DW_AT_use_UTF8";
13402 case DW_AT_extension:
13403 return "DW_AT_extension";
13404 case DW_AT_ranges:
13405 return "DW_AT_ranges";
13406 case DW_AT_trampoline:
13407 return "DW_AT_trampoline";
13408 case DW_AT_call_column:
13409 return "DW_AT_call_column";
13410 case DW_AT_call_file:
13411 return "DW_AT_call_file";
13412 case DW_AT_call_line:
13413 return "DW_AT_call_line";
13414 case DW_AT_description:
13415 return "DW_AT_description";
13416 case DW_AT_binary_scale:
13417 return "DW_AT_binary_scale";
13418 case DW_AT_decimal_scale:
13419 return "DW_AT_decimal_scale";
13420 case DW_AT_small:
13421 return "DW_AT_small";
13422 case DW_AT_decimal_sign:
13423 return "DW_AT_decimal_sign";
13424 case DW_AT_digit_count:
13425 return "DW_AT_digit_count";
13426 case DW_AT_picture_string:
13427 return "DW_AT_picture_string";
13428 case DW_AT_mutable:
13429 return "DW_AT_mutable";
13430 case DW_AT_threads_scaled:
13431 return "DW_AT_threads_scaled";
13432 case DW_AT_explicit:
13433 return "DW_AT_explicit";
13434 case DW_AT_object_pointer:
13435 return "DW_AT_object_pointer";
13436 case DW_AT_endianity:
13437 return "DW_AT_endianity";
13438 case DW_AT_elemental:
13439 return "DW_AT_elemental";
13440 case DW_AT_pure:
13441 return "DW_AT_pure";
13442 case DW_AT_recursive:
13443 return "DW_AT_recursive";
13444 /* DWARF 4 values. */
13445 case DW_AT_signature:
13446 return "DW_AT_signature";
13447 case DW_AT_linkage_name:
13448 return "DW_AT_linkage_name";
13449 /* SGI/MIPS extensions. */
13450 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13451 case DW_AT_MIPS_fde:
13452 return "DW_AT_MIPS_fde";
13453 #endif
13454 case DW_AT_MIPS_loop_begin:
13455 return "DW_AT_MIPS_loop_begin";
13456 case DW_AT_MIPS_tail_loop_begin:
13457 return "DW_AT_MIPS_tail_loop_begin";
13458 case DW_AT_MIPS_epilog_begin:
13459 return "DW_AT_MIPS_epilog_begin";
13460 case DW_AT_MIPS_loop_unroll_factor:
13461 return "DW_AT_MIPS_loop_unroll_factor";
13462 case DW_AT_MIPS_software_pipeline_depth:
13463 return "DW_AT_MIPS_software_pipeline_depth";
13464 case DW_AT_MIPS_linkage_name:
13465 return "DW_AT_MIPS_linkage_name";
13466 case DW_AT_MIPS_stride:
13467 return "DW_AT_MIPS_stride";
13468 case DW_AT_MIPS_abstract_name:
13469 return "DW_AT_MIPS_abstract_name";
13470 case DW_AT_MIPS_clone_origin:
13471 return "DW_AT_MIPS_clone_origin";
13472 case DW_AT_MIPS_has_inlines:
13473 return "DW_AT_MIPS_has_inlines";
13474 /* HP extensions. */
13475 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13476 case DW_AT_HP_block_index:
13477 return "DW_AT_HP_block_index";
13478 #endif
13479 case DW_AT_HP_unmodifiable:
13480 return "DW_AT_HP_unmodifiable";
13481 case DW_AT_HP_actuals_stmt_list:
13482 return "DW_AT_HP_actuals_stmt_list";
13483 case DW_AT_HP_proc_per_section:
13484 return "DW_AT_HP_proc_per_section";
13485 case DW_AT_HP_raw_data_ptr:
13486 return "DW_AT_HP_raw_data_ptr";
13487 case DW_AT_HP_pass_by_reference:
13488 return "DW_AT_HP_pass_by_reference";
13489 case DW_AT_HP_opt_level:
13490 return "DW_AT_HP_opt_level";
13491 case DW_AT_HP_prof_version_id:
13492 return "DW_AT_HP_prof_version_id";
13493 case DW_AT_HP_opt_flags:
13494 return "DW_AT_HP_opt_flags";
13495 case DW_AT_HP_cold_region_low_pc:
13496 return "DW_AT_HP_cold_region_low_pc";
13497 case DW_AT_HP_cold_region_high_pc:
13498 return "DW_AT_HP_cold_region_high_pc";
13499 case DW_AT_HP_all_variables_modifiable:
13500 return "DW_AT_HP_all_variables_modifiable";
13501 case DW_AT_HP_linkage_name:
13502 return "DW_AT_HP_linkage_name";
13503 case DW_AT_HP_prof_flags:
13504 return "DW_AT_HP_prof_flags";
13505 /* GNU extensions. */
13506 case DW_AT_sf_names:
13507 return "DW_AT_sf_names";
13508 case DW_AT_src_info:
13509 return "DW_AT_src_info";
13510 case DW_AT_mac_info:
13511 return "DW_AT_mac_info";
13512 case DW_AT_src_coords:
13513 return "DW_AT_src_coords";
13514 case DW_AT_body_begin:
13515 return "DW_AT_body_begin";
13516 case DW_AT_body_end:
13517 return "DW_AT_body_end";
13518 case DW_AT_GNU_vector:
13519 return "DW_AT_GNU_vector";
13520 case DW_AT_GNU_odr_signature:
13521 return "DW_AT_GNU_odr_signature";
13522 /* VMS extensions. */
13523 case DW_AT_VMS_rtnbeg_pd_address:
13524 return "DW_AT_VMS_rtnbeg_pd_address";
13525 /* UPC extension. */
13526 case DW_AT_upc_threads_scaled:
13527 return "DW_AT_upc_threads_scaled";
13528 /* PGI (STMicroelectronics) extensions. */
13529 case DW_AT_PGI_lbase:
13530 return "DW_AT_PGI_lbase";
13531 case DW_AT_PGI_soffset:
13532 return "DW_AT_PGI_soffset";
13533 case DW_AT_PGI_lstride:
13534 return "DW_AT_PGI_lstride";
13535 default:
13536 return "DW_AT_<unknown>";
13537 }
13538 }
13539
13540 /* Convert a DWARF value form code into its string name. */
13541
13542 static char *
13543 dwarf_form_name (unsigned form)
13544 {
13545 switch (form)
13546 {
13547 case DW_FORM_addr:
13548 return "DW_FORM_addr";
13549 case DW_FORM_block2:
13550 return "DW_FORM_block2";
13551 case DW_FORM_block4:
13552 return "DW_FORM_block4";
13553 case DW_FORM_data2:
13554 return "DW_FORM_data2";
13555 case DW_FORM_data4:
13556 return "DW_FORM_data4";
13557 case DW_FORM_data8:
13558 return "DW_FORM_data8";
13559 case DW_FORM_string:
13560 return "DW_FORM_string";
13561 case DW_FORM_block:
13562 return "DW_FORM_block";
13563 case DW_FORM_block1:
13564 return "DW_FORM_block1";
13565 case DW_FORM_data1:
13566 return "DW_FORM_data1";
13567 case DW_FORM_flag:
13568 return "DW_FORM_flag";
13569 case DW_FORM_sdata:
13570 return "DW_FORM_sdata";
13571 case DW_FORM_strp:
13572 return "DW_FORM_strp";
13573 case DW_FORM_udata:
13574 return "DW_FORM_udata";
13575 case DW_FORM_ref_addr:
13576 return "DW_FORM_ref_addr";
13577 case DW_FORM_ref1:
13578 return "DW_FORM_ref1";
13579 case DW_FORM_ref2:
13580 return "DW_FORM_ref2";
13581 case DW_FORM_ref4:
13582 return "DW_FORM_ref4";
13583 case DW_FORM_ref8:
13584 return "DW_FORM_ref8";
13585 case DW_FORM_ref_udata:
13586 return "DW_FORM_ref_udata";
13587 case DW_FORM_indirect:
13588 return "DW_FORM_indirect";
13589 case DW_FORM_sec_offset:
13590 return "DW_FORM_sec_offset";
13591 case DW_FORM_exprloc:
13592 return "DW_FORM_exprloc";
13593 case DW_FORM_flag_present:
13594 return "DW_FORM_flag_present";
13595 case DW_FORM_ref_sig8:
13596 return "DW_FORM_ref_sig8";
13597 default:
13598 return "DW_FORM_<unknown>";
13599 }
13600 }
13601
13602 /* Convert a DWARF stack opcode into its string name. */
13603
13604 const char *
13605 dwarf_stack_op_name (unsigned op)
13606 {
13607 switch (op)
13608 {
13609 case DW_OP_addr:
13610 return "DW_OP_addr";
13611 case DW_OP_deref:
13612 return "DW_OP_deref";
13613 case DW_OP_const1u:
13614 return "DW_OP_const1u";
13615 case DW_OP_const1s:
13616 return "DW_OP_const1s";
13617 case DW_OP_const2u:
13618 return "DW_OP_const2u";
13619 case DW_OP_const2s:
13620 return "DW_OP_const2s";
13621 case DW_OP_const4u:
13622 return "DW_OP_const4u";
13623 case DW_OP_const4s:
13624 return "DW_OP_const4s";
13625 case DW_OP_const8u:
13626 return "DW_OP_const8u";
13627 case DW_OP_const8s:
13628 return "DW_OP_const8s";
13629 case DW_OP_constu:
13630 return "DW_OP_constu";
13631 case DW_OP_consts:
13632 return "DW_OP_consts";
13633 case DW_OP_dup:
13634 return "DW_OP_dup";
13635 case DW_OP_drop:
13636 return "DW_OP_drop";
13637 case DW_OP_over:
13638 return "DW_OP_over";
13639 case DW_OP_pick:
13640 return "DW_OP_pick";
13641 case DW_OP_swap:
13642 return "DW_OP_swap";
13643 case DW_OP_rot:
13644 return "DW_OP_rot";
13645 case DW_OP_xderef:
13646 return "DW_OP_xderef";
13647 case DW_OP_abs:
13648 return "DW_OP_abs";
13649 case DW_OP_and:
13650 return "DW_OP_and";
13651 case DW_OP_div:
13652 return "DW_OP_div";
13653 case DW_OP_minus:
13654 return "DW_OP_minus";
13655 case DW_OP_mod:
13656 return "DW_OP_mod";
13657 case DW_OP_mul:
13658 return "DW_OP_mul";
13659 case DW_OP_neg:
13660 return "DW_OP_neg";
13661 case DW_OP_not:
13662 return "DW_OP_not";
13663 case DW_OP_or:
13664 return "DW_OP_or";
13665 case DW_OP_plus:
13666 return "DW_OP_plus";
13667 case DW_OP_plus_uconst:
13668 return "DW_OP_plus_uconst";
13669 case DW_OP_shl:
13670 return "DW_OP_shl";
13671 case DW_OP_shr:
13672 return "DW_OP_shr";
13673 case DW_OP_shra:
13674 return "DW_OP_shra";
13675 case DW_OP_xor:
13676 return "DW_OP_xor";
13677 case DW_OP_bra:
13678 return "DW_OP_bra";
13679 case DW_OP_eq:
13680 return "DW_OP_eq";
13681 case DW_OP_ge:
13682 return "DW_OP_ge";
13683 case DW_OP_gt:
13684 return "DW_OP_gt";
13685 case DW_OP_le:
13686 return "DW_OP_le";
13687 case DW_OP_lt:
13688 return "DW_OP_lt";
13689 case DW_OP_ne:
13690 return "DW_OP_ne";
13691 case DW_OP_skip:
13692 return "DW_OP_skip";
13693 case DW_OP_lit0:
13694 return "DW_OP_lit0";
13695 case DW_OP_lit1:
13696 return "DW_OP_lit1";
13697 case DW_OP_lit2:
13698 return "DW_OP_lit2";
13699 case DW_OP_lit3:
13700 return "DW_OP_lit3";
13701 case DW_OP_lit4:
13702 return "DW_OP_lit4";
13703 case DW_OP_lit5:
13704 return "DW_OP_lit5";
13705 case DW_OP_lit6:
13706 return "DW_OP_lit6";
13707 case DW_OP_lit7:
13708 return "DW_OP_lit7";
13709 case DW_OP_lit8:
13710 return "DW_OP_lit8";
13711 case DW_OP_lit9:
13712 return "DW_OP_lit9";
13713 case DW_OP_lit10:
13714 return "DW_OP_lit10";
13715 case DW_OP_lit11:
13716 return "DW_OP_lit11";
13717 case DW_OP_lit12:
13718 return "DW_OP_lit12";
13719 case DW_OP_lit13:
13720 return "DW_OP_lit13";
13721 case DW_OP_lit14:
13722 return "DW_OP_lit14";
13723 case DW_OP_lit15:
13724 return "DW_OP_lit15";
13725 case DW_OP_lit16:
13726 return "DW_OP_lit16";
13727 case DW_OP_lit17:
13728 return "DW_OP_lit17";
13729 case DW_OP_lit18:
13730 return "DW_OP_lit18";
13731 case DW_OP_lit19:
13732 return "DW_OP_lit19";
13733 case DW_OP_lit20:
13734 return "DW_OP_lit20";
13735 case DW_OP_lit21:
13736 return "DW_OP_lit21";
13737 case DW_OP_lit22:
13738 return "DW_OP_lit22";
13739 case DW_OP_lit23:
13740 return "DW_OP_lit23";
13741 case DW_OP_lit24:
13742 return "DW_OP_lit24";
13743 case DW_OP_lit25:
13744 return "DW_OP_lit25";
13745 case DW_OP_lit26:
13746 return "DW_OP_lit26";
13747 case DW_OP_lit27:
13748 return "DW_OP_lit27";
13749 case DW_OP_lit28:
13750 return "DW_OP_lit28";
13751 case DW_OP_lit29:
13752 return "DW_OP_lit29";
13753 case DW_OP_lit30:
13754 return "DW_OP_lit30";
13755 case DW_OP_lit31:
13756 return "DW_OP_lit31";
13757 case DW_OP_reg0:
13758 return "DW_OP_reg0";
13759 case DW_OP_reg1:
13760 return "DW_OP_reg1";
13761 case DW_OP_reg2:
13762 return "DW_OP_reg2";
13763 case DW_OP_reg3:
13764 return "DW_OP_reg3";
13765 case DW_OP_reg4:
13766 return "DW_OP_reg4";
13767 case DW_OP_reg5:
13768 return "DW_OP_reg5";
13769 case DW_OP_reg6:
13770 return "DW_OP_reg6";
13771 case DW_OP_reg7:
13772 return "DW_OP_reg7";
13773 case DW_OP_reg8:
13774 return "DW_OP_reg8";
13775 case DW_OP_reg9:
13776 return "DW_OP_reg9";
13777 case DW_OP_reg10:
13778 return "DW_OP_reg10";
13779 case DW_OP_reg11:
13780 return "DW_OP_reg11";
13781 case DW_OP_reg12:
13782 return "DW_OP_reg12";
13783 case DW_OP_reg13:
13784 return "DW_OP_reg13";
13785 case DW_OP_reg14:
13786 return "DW_OP_reg14";
13787 case DW_OP_reg15:
13788 return "DW_OP_reg15";
13789 case DW_OP_reg16:
13790 return "DW_OP_reg16";
13791 case DW_OP_reg17:
13792 return "DW_OP_reg17";
13793 case DW_OP_reg18:
13794 return "DW_OP_reg18";
13795 case DW_OP_reg19:
13796 return "DW_OP_reg19";
13797 case DW_OP_reg20:
13798 return "DW_OP_reg20";
13799 case DW_OP_reg21:
13800 return "DW_OP_reg21";
13801 case DW_OP_reg22:
13802 return "DW_OP_reg22";
13803 case DW_OP_reg23:
13804 return "DW_OP_reg23";
13805 case DW_OP_reg24:
13806 return "DW_OP_reg24";
13807 case DW_OP_reg25:
13808 return "DW_OP_reg25";
13809 case DW_OP_reg26:
13810 return "DW_OP_reg26";
13811 case DW_OP_reg27:
13812 return "DW_OP_reg27";
13813 case DW_OP_reg28:
13814 return "DW_OP_reg28";
13815 case DW_OP_reg29:
13816 return "DW_OP_reg29";
13817 case DW_OP_reg30:
13818 return "DW_OP_reg30";
13819 case DW_OP_reg31:
13820 return "DW_OP_reg31";
13821 case DW_OP_breg0:
13822 return "DW_OP_breg0";
13823 case DW_OP_breg1:
13824 return "DW_OP_breg1";
13825 case DW_OP_breg2:
13826 return "DW_OP_breg2";
13827 case DW_OP_breg3:
13828 return "DW_OP_breg3";
13829 case DW_OP_breg4:
13830 return "DW_OP_breg4";
13831 case DW_OP_breg5:
13832 return "DW_OP_breg5";
13833 case DW_OP_breg6:
13834 return "DW_OP_breg6";
13835 case DW_OP_breg7:
13836 return "DW_OP_breg7";
13837 case DW_OP_breg8:
13838 return "DW_OP_breg8";
13839 case DW_OP_breg9:
13840 return "DW_OP_breg9";
13841 case DW_OP_breg10:
13842 return "DW_OP_breg10";
13843 case DW_OP_breg11:
13844 return "DW_OP_breg11";
13845 case DW_OP_breg12:
13846 return "DW_OP_breg12";
13847 case DW_OP_breg13:
13848 return "DW_OP_breg13";
13849 case DW_OP_breg14:
13850 return "DW_OP_breg14";
13851 case DW_OP_breg15:
13852 return "DW_OP_breg15";
13853 case DW_OP_breg16:
13854 return "DW_OP_breg16";
13855 case DW_OP_breg17:
13856 return "DW_OP_breg17";
13857 case DW_OP_breg18:
13858 return "DW_OP_breg18";
13859 case DW_OP_breg19:
13860 return "DW_OP_breg19";
13861 case DW_OP_breg20:
13862 return "DW_OP_breg20";
13863 case DW_OP_breg21:
13864 return "DW_OP_breg21";
13865 case DW_OP_breg22:
13866 return "DW_OP_breg22";
13867 case DW_OP_breg23:
13868 return "DW_OP_breg23";
13869 case DW_OP_breg24:
13870 return "DW_OP_breg24";
13871 case DW_OP_breg25:
13872 return "DW_OP_breg25";
13873 case DW_OP_breg26:
13874 return "DW_OP_breg26";
13875 case DW_OP_breg27:
13876 return "DW_OP_breg27";
13877 case DW_OP_breg28:
13878 return "DW_OP_breg28";
13879 case DW_OP_breg29:
13880 return "DW_OP_breg29";
13881 case DW_OP_breg30:
13882 return "DW_OP_breg30";
13883 case DW_OP_breg31:
13884 return "DW_OP_breg31";
13885 case DW_OP_regx:
13886 return "DW_OP_regx";
13887 case DW_OP_fbreg:
13888 return "DW_OP_fbreg";
13889 case DW_OP_bregx:
13890 return "DW_OP_bregx";
13891 case DW_OP_piece:
13892 return "DW_OP_piece";
13893 case DW_OP_deref_size:
13894 return "DW_OP_deref_size";
13895 case DW_OP_xderef_size:
13896 return "DW_OP_xderef_size";
13897 case DW_OP_nop:
13898 return "DW_OP_nop";
13899 /* DWARF 3 extensions. */
13900 case DW_OP_push_object_address:
13901 return "DW_OP_push_object_address";
13902 case DW_OP_call2:
13903 return "DW_OP_call2";
13904 case DW_OP_call4:
13905 return "DW_OP_call4";
13906 case DW_OP_call_ref:
13907 return "DW_OP_call_ref";
13908 case DW_OP_form_tls_address:
13909 return "DW_OP_form_tls_address";
13910 case DW_OP_call_frame_cfa:
13911 return "DW_OP_call_frame_cfa";
13912 case DW_OP_bit_piece:
13913 return "DW_OP_bit_piece";
13914 /* DWARF 4 extensions. */
13915 case DW_OP_implicit_value:
13916 return "DW_OP_implicit_value";
13917 case DW_OP_stack_value:
13918 return "DW_OP_stack_value";
13919 /* GNU extensions. */
13920 case DW_OP_GNU_push_tls_address:
13921 return "DW_OP_GNU_push_tls_address";
13922 case DW_OP_GNU_uninit:
13923 return "DW_OP_GNU_uninit";
13924 case DW_OP_GNU_encoded_addr:
13925 return "DW_OP_GNU_encoded_addr";
13926 case DW_OP_GNU_implicit_pointer:
13927 return "DW_OP_GNU_implicit_pointer";
13928 case DW_OP_GNU_entry_value:
13929 return "DW_OP_GNU_entry_value";
13930 case DW_OP_GNU_const_type:
13931 return "DW_OP_GNU_const_type";
13932 case DW_OP_GNU_regval_type:
13933 return "DW_OP_GNU_regval_type";
13934 case DW_OP_GNU_deref_type:
13935 return "DW_OP_GNU_deref_type";
13936 case DW_OP_GNU_convert:
13937 return "DW_OP_GNU_convert";
13938 case DW_OP_GNU_reinterpret:
13939 return "DW_OP_GNU_reinterpret";
13940 case DW_OP_GNU_parameter_ref:
13941 return "DW_OP_GNU_parameter_ref";
13942 default:
13943 return NULL;
13944 }
13945 }
13946
13947 static char *
13948 dwarf_bool_name (unsigned mybool)
13949 {
13950 if (mybool)
13951 return "TRUE";
13952 else
13953 return "FALSE";
13954 }
13955
13956 /* Convert a DWARF type code into its string name. */
13957
13958 static char *
13959 dwarf_type_encoding_name (unsigned enc)
13960 {
13961 switch (enc)
13962 {
13963 case DW_ATE_void:
13964 return "DW_ATE_void";
13965 case DW_ATE_address:
13966 return "DW_ATE_address";
13967 case DW_ATE_boolean:
13968 return "DW_ATE_boolean";
13969 case DW_ATE_complex_float:
13970 return "DW_ATE_complex_float";
13971 case DW_ATE_float:
13972 return "DW_ATE_float";
13973 case DW_ATE_signed:
13974 return "DW_ATE_signed";
13975 case DW_ATE_signed_char:
13976 return "DW_ATE_signed_char";
13977 case DW_ATE_unsigned:
13978 return "DW_ATE_unsigned";
13979 case DW_ATE_unsigned_char:
13980 return "DW_ATE_unsigned_char";
13981 /* DWARF 3. */
13982 case DW_ATE_imaginary_float:
13983 return "DW_ATE_imaginary_float";
13984 case DW_ATE_packed_decimal:
13985 return "DW_ATE_packed_decimal";
13986 case DW_ATE_numeric_string:
13987 return "DW_ATE_numeric_string";
13988 case DW_ATE_edited:
13989 return "DW_ATE_edited";
13990 case DW_ATE_signed_fixed:
13991 return "DW_ATE_signed_fixed";
13992 case DW_ATE_unsigned_fixed:
13993 return "DW_ATE_unsigned_fixed";
13994 case DW_ATE_decimal_float:
13995 return "DW_ATE_decimal_float";
13996 /* DWARF 4. */
13997 case DW_ATE_UTF:
13998 return "DW_ATE_UTF";
13999 /* HP extensions. */
14000 case DW_ATE_HP_float80:
14001 return "DW_ATE_HP_float80";
14002 case DW_ATE_HP_complex_float80:
14003 return "DW_ATE_HP_complex_float80";
14004 case DW_ATE_HP_float128:
14005 return "DW_ATE_HP_float128";
14006 case DW_ATE_HP_complex_float128:
14007 return "DW_ATE_HP_complex_float128";
14008 case DW_ATE_HP_floathpintel:
14009 return "DW_ATE_HP_floathpintel";
14010 case DW_ATE_HP_imaginary_float80:
14011 return "DW_ATE_HP_imaginary_float80";
14012 case DW_ATE_HP_imaginary_float128:
14013 return "DW_ATE_HP_imaginary_float128";
14014 default:
14015 return "DW_ATE_<unknown>";
14016 }
14017 }
14018
14019 /* Convert a DWARF call frame info operation to its string name. */
14020
14021 #if 0
14022 static char *
14023 dwarf_cfi_name (unsigned cfi_opc)
14024 {
14025 switch (cfi_opc)
14026 {
14027 case DW_CFA_advance_loc:
14028 return "DW_CFA_advance_loc";
14029 case DW_CFA_offset:
14030 return "DW_CFA_offset";
14031 case DW_CFA_restore:
14032 return "DW_CFA_restore";
14033 case DW_CFA_nop:
14034 return "DW_CFA_nop";
14035 case DW_CFA_set_loc:
14036 return "DW_CFA_set_loc";
14037 case DW_CFA_advance_loc1:
14038 return "DW_CFA_advance_loc1";
14039 case DW_CFA_advance_loc2:
14040 return "DW_CFA_advance_loc2";
14041 case DW_CFA_advance_loc4:
14042 return "DW_CFA_advance_loc4";
14043 case DW_CFA_offset_extended:
14044 return "DW_CFA_offset_extended";
14045 case DW_CFA_restore_extended:
14046 return "DW_CFA_restore_extended";
14047 case DW_CFA_undefined:
14048 return "DW_CFA_undefined";
14049 case DW_CFA_same_value:
14050 return "DW_CFA_same_value";
14051 case DW_CFA_register:
14052 return "DW_CFA_register";
14053 case DW_CFA_remember_state:
14054 return "DW_CFA_remember_state";
14055 case DW_CFA_restore_state:
14056 return "DW_CFA_restore_state";
14057 case DW_CFA_def_cfa:
14058 return "DW_CFA_def_cfa";
14059 case DW_CFA_def_cfa_register:
14060 return "DW_CFA_def_cfa_register";
14061 case DW_CFA_def_cfa_offset:
14062 return "DW_CFA_def_cfa_offset";
14063 /* DWARF 3. */
14064 case DW_CFA_def_cfa_expression:
14065 return "DW_CFA_def_cfa_expression";
14066 case DW_CFA_expression:
14067 return "DW_CFA_expression";
14068 case DW_CFA_offset_extended_sf:
14069 return "DW_CFA_offset_extended_sf";
14070 case DW_CFA_def_cfa_sf:
14071 return "DW_CFA_def_cfa_sf";
14072 case DW_CFA_def_cfa_offset_sf:
14073 return "DW_CFA_def_cfa_offset_sf";
14074 case DW_CFA_val_offset:
14075 return "DW_CFA_val_offset";
14076 case DW_CFA_val_offset_sf:
14077 return "DW_CFA_val_offset_sf";
14078 case DW_CFA_val_expression:
14079 return "DW_CFA_val_expression";
14080 /* SGI/MIPS specific. */
14081 case DW_CFA_MIPS_advance_loc8:
14082 return "DW_CFA_MIPS_advance_loc8";
14083 /* GNU extensions. */
14084 case DW_CFA_GNU_window_save:
14085 return "DW_CFA_GNU_window_save";
14086 case DW_CFA_GNU_args_size:
14087 return "DW_CFA_GNU_args_size";
14088 case DW_CFA_GNU_negative_offset_extended:
14089 return "DW_CFA_GNU_negative_offset_extended";
14090 default:
14091 return "DW_CFA_<unknown>";
14092 }
14093 }
14094 #endif
14095
14096 static void
14097 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14098 {
14099 unsigned int i;
14100
14101 print_spaces (indent, f);
14102 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14103 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14104
14105 if (die->parent != NULL)
14106 {
14107 print_spaces (indent, f);
14108 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14109 die->parent->offset.sect_off);
14110 }
14111
14112 print_spaces (indent, f);
14113 fprintf_unfiltered (f, " has children: %s\n",
14114 dwarf_bool_name (die->child != NULL));
14115
14116 print_spaces (indent, f);
14117 fprintf_unfiltered (f, " attributes:\n");
14118
14119 for (i = 0; i < die->num_attrs; ++i)
14120 {
14121 print_spaces (indent, f);
14122 fprintf_unfiltered (f, " %s (%s) ",
14123 dwarf_attr_name (die->attrs[i].name),
14124 dwarf_form_name (die->attrs[i].form));
14125
14126 switch (die->attrs[i].form)
14127 {
14128 case DW_FORM_addr:
14129 fprintf_unfiltered (f, "address: ");
14130 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14131 break;
14132 case DW_FORM_block2:
14133 case DW_FORM_block4:
14134 case DW_FORM_block:
14135 case DW_FORM_block1:
14136 fprintf_unfiltered (f, "block: size %d",
14137 DW_BLOCK (&die->attrs[i])->size);
14138 break;
14139 case DW_FORM_exprloc:
14140 fprintf_unfiltered (f, "expression: size %u",
14141 DW_BLOCK (&die->attrs[i])->size);
14142 break;
14143 case DW_FORM_ref_addr:
14144 fprintf_unfiltered (f, "ref address: ");
14145 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14146 break;
14147 case DW_FORM_ref1:
14148 case DW_FORM_ref2:
14149 case DW_FORM_ref4:
14150 case DW_FORM_ref8:
14151 case DW_FORM_ref_udata:
14152 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14153 (long) (DW_UNSND (&die->attrs[i])));
14154 break;
14155 case DW_FORM_data1:
14156 case DW_FORM_data2:
14157 case DW_FORM_data4:
14158 case DW_FORM_data8:
14159 case DW_FORM_udata:
14160 case DW_FORM_sdata:
14161 fprintf_unfiltered (f, "constant: %s",
14162 pulongest (DW_UNSND (&die->attrs[i])));
14163 break;
14164 case DW_FORM_sec_offset:
14165 fprintf_unfiltered (f, "section offset: %s",
14166 pulongest (DW_UNSND (&die->attrs[i])));
14167 break;
14168 case DW_FORM_ref_sig8:
14169 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14170 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14171 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14172 else
14173 fprintf_unfiltered (f, "signatured type, offset: unknown");
14174 break;
14175 case DW_FORM_string:
14176 case DW_FORM_strp:
14177 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14178 DW_STRING (&die->attrs[i])
14179 ? DW_STRING (&die->attrs[i]) : "",
14180 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14181 break;
14182 case DW_FORM_flag:
14183 if (DW_UNSND (&die->attrs[i]))
14184 fprintf_unfiltered (f, "flag: TRUE");
14185 else
14186 fprintf_unfiltered (f, "flag: FALSE");
14187 break;
14188 case DW_FORM_flag_present:
14189 fprintf_unfiltered (f, "flag: TRUE");
14190 break;
14191 case DW_FORM_indirect:
14192 /* The reader will have reduced the indirect form to
14193 the "base form" so this form should not occur. */
14194 fprintf_unfiltered (f,
14195 "unexpected attribute form: DW_FORM_indirect");
14196 break;
14197 default:
14198 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14199 die->attrs[i].form);
14200 break;
14201 }
14202 fprintf_unfiltered (f, "\n");
14203 }
14204 }
14205
14206 static void
14207 dump_die_for_error (struct die_info *die)
14208 {
14209 dump_die_shallow (gdb_stderr, 0, die);
14210 }
14211
14212 static void
14213 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14214 {
14215 int indent = level * 4;
14216
14217 gdb_assert (die != NULL);
14218
14219 if (level >= max_level)
14220 return;
14221
14222 dump_die_shallow (f, indent, die);
14223
14224 if (die->child != NULL)
14225 {
14226 print_spaces (indent, f);
14227 fprintf_unfiltered (f, " Children:");
14228 if (level + 1 < max_level)
14229 {
14230 fprintf_unfiltered (f, "\n");
14231 dump_die_1 (f, level + 1, max_level, die->child);
14232 }
14233 else
14234 {
14235 fprintf_unfiltered (f,
14236 " [not printed, max nesting level reached]\n");
14237 }
14238 }
14239
14240 if (die->sibling != NULL && level > 0)
14241 {
14242 dump_die_1 (f, level, max_level, die->sibling);
14243 }
14244 }
14245
14246 /* This is called from the pdie macro in gdbinit.in.
14247 It's not static so gcc will keep a copy callable from gdb. */
14248
14249 void
14250 dump_die (struct die_info *die, int max_level)
14251 {
14252 dump_die_1 (gdb_stdlog, 0, max_level, die);
14253 }
14254
14255 static void
14256 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14257 {
14258 void **slot;
14259
14260 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14261 INSERT);
14262
14263 *slot = die;
14264 }
14265
14266 /* DW_ADDR is always stored already as sect_offset; despite for the forms
14267 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14268
14269 static int
14270 is_ref_attr (struct attribute *attr)
14271 {
14272 switch (attr->form)
14273 {
14274 case DW_FORM_ref_addr:
14275 case DW_FORM_ref1:
14276 case DW_FORM_ref2:
14277 case DW_FORM_ref4:
14278 case DW_FORM_ref8:
14279 case DW_FORM_ref_udata:
14280 return 1;
14281 default:
14282 return 0;
14283 }
14284 }
14285
14286 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14287 required kind. */
14288
14289 static sect_offset
14290 dwarf2_get_ref_die_offset (struct attribute *attr)
14291 {
14292 sect_offset retval = { DW_UNSND (attr) };
14293
14294 if (is_ref_attr (attr))
14295 return retval;
14296
14297 retval.sect_off = 0;
14298 complaint (&symfile_complaints,
14299 _("unsupported die ref attribute form: '%s'"),
14300 dwarf_form_name (attr->form));
14301 return retval;
14302 }
14303
14304 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14305 * the value held by the attribute is not constant. */
14306
14307 static LONGEST
14308 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14309 {
14310 if (attr->form == DW_FORM_sdata)
14311 return DW_SND (attr);
14312 else if (attr->form == DW_FORM_udata
14313 || attr->form == DW_FORM_data1
14314 || attr->form == DW_FORM_data2
14315 || attr->form == DW_FORM_data4
14316 || attr->form == DW_FORM_data8)
14317 return DW_UNSND (attr);
14318 else
14319 {
14320 complaint (&symfile_complaints,
14321 _("Attribute value is not a constant (%s)"),
14322 dwarf_form_name (attr->form));
14323 return default_value;
14324 }
14325 }
14326
14327 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14328 unit and add it to our queue.
14329 The result is non-zero if PER_CU was queued, otherwise the result is zero
14330 meaning either PER_CU is already queued or it is already loaded. */
14331
14332 static int
14333 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14334 struct dwarf2_per_cu_data *per_cu)
14335 {
14336 /* We may arrive here during partial symbol reading, if we need full
14337 DIEs to process an unusual case (e.g. template arguments). Do
14338 not queue PER_CU, just tell our caller to load its DIEs. */
14339 if (dwarf2_per_objfile->reading_partial_symbols)
14340 {
14341 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14342 return 1;
14343 return 0;
14344 }
14345
14346 /* Mark the dependence relation so that we don't flush PER_CU
14347 too early. */
14348 dwarf2_add_dependence (this_cu, per_cu);
14349
14350 /* If it's already on the queue, we have nothing to do. */
14351 if (per_cu->queued)
14352 return 0;
14353
14354 /* If the compilation unit is already loaded, just mark it as
14355 used. */
14356 if (per_cu->cu != NULL)
14357 {
14358 per_cu->cu->last_used = 0;
14359 return 0;
14360 }
14361
14362 /* Add it to the queue. */
14363 queue_comp_unit (per_cu);
14364
14365 return 1;
14366 }
14367
14368 /* Follow reference or signature attribute ATTR of SRC_DIE.
14369 On entry *REF_CU is the CU of SRC_DIE.
14370 On exit *REF_CU is the CU of the result. */
14371
14372 static struct die_info *
14373 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14374 struct dwarf2_cu **ref_cu)
14375 {
14376 struct die_info *die;
14377
14378 if (is_ref_attr (attr))
14379 die = follow_die_ref (src_die, attr, ref_cu);
14380 else if (attr->form == DW_FORM_ref_sig8)
14381 die = follow_die_sig (src_die, attr, ref_cu);
14382 else
14383 {
14384 dump_die_for_error (src_die);
14385 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14386 (*ref_cu)->objfile->name);
14387 }
14388
14389 return die;
14390 }
14391
14392 /* Follow reference OFFSET.
14393 On entry *REF_CU is the CU of the source die referencing OFFSET.
14394 On exit *REF_CU is the CU of the result.
14395 Returns NULL if OFFSET is invalid. */
14396
14397 static struct die_info *
14398 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
14399 {
14400 struct die_info temp_die;
14401 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14402
14403 gdb_assert (cu->per_cu != NULL);
14404
14405 target_cu = cu;
14406
14407 if (cu->per_cu->debug_types_section)
14408 {
14409 /* .debug_types CUs cannot reference anything outside their CU.
14410 If they need to, they have to reference a signatured type via
14411 DW_FORM_ref_sig8. */
14412 if (! offset_in_cu_p (&cu->header, offset))
14413 return NULL;
14414 }
14415 else if (! offset_in_cu_p (&cu->header, offset))
14416 {
14417 struct dwarf2_per_cu_data *per_cu;
14418
14419 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14420
14421 /* If necessary, add it to the queue and load its DIEs. */
14422 if (maybe_queue_comp_unit (cu, per_cu))
14423 load_full_comp_unit (per_cu);
14424
14425 target_cu = per_cu->cu;
14426 }
14427 else if (cu->dies == NULL)
14428 {
14429 /* We're loading full DIEs during partial symbol reading. */
14430 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14431 load_full_comp_unit (cu->per_cu);
14432 }
14433
14434 *ref_cu = target_cu;
14435 temp_die.offset = offset;
14436 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
14437 }
14438
14439 /* Follow reference attribute ATTR of SRC_DIE.
14440 On entry *REF_CU is the CU of SRC_DIE.
14441 On exit *REF_CU is the CU of the result. */
14442
14443 static struct die_info *
14444 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14445 struct dwarf2_cu **ref_cu)
14446 {
14447 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14448 struct dwarf2_cu *cu = *ref_cu;
14449 struct die_info *die;
14450
14451 die = follow_die_offset (offset, ref_cu);
14452 if (!die)
14453 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14454 "at 0x%x [in module %s]"),
14455 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
14456
14457 return die;
14458 }
14459
14460 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14461 Returned value is intended for DW_OP_call*. Returned
14462 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14463
14464 struct dwarf2_locexpr_baton
14465 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
14466 struct dwarf2_per_cu_data *per_cu,
14467 CORE_ADDR (*get_frame_pc) (void *baton),
14468 void *baton)
14469 {
14470 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
14471 struct dwarf2_cu *cu;
14472 struct die_info *die;
14473 struct attribute *attr;
14474 struct dwarf2_locexpr_baton retval;
14475
14476 dw2_setup (per_cu->objfile);
14477
14478 if (per_cu->cu == NULL)
14479 load_cu (per_cu);
14480 cu = per_cu->cu;
14481
14482 die = follow_die_offset (offset, &cu);
14483 if (!die)
14484 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14485 offset.sect_off, per_cu->objfile->name);
14486
14487 attr = dwarf2_attr (die, DW_AT_location, cu);
14488 if (!attr)
14489 {
14490 /* DWARF: "If there is no such attribute, then there is no effect.".
14491 DATA is ignored if SIZE is 0. */
14492
14493 retval.data = NULL;
14494 retval.size = 0;
14495 }
14496 else if (attr_form_is_section_offset (attr))
14497 {
14498 struct dwarf2_loclist_baton loclist_baton;
14499 CORE_ADDR pc = (*get_frame_pc) (baton);
14500 size_t size;
14501
14502 fill_in_loclist_baton (cu, &loclist_baton, attr);
14503
14504 retval.data = dwarf2_find_location_expression (&loclist_baton,
14505 &size, pc);
14506 retval.size = size;
14507 }
14508 else
14509 {
14510 if (!attr_form_is_block (attr))
14511 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14512 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14513 offset.sect_off, per_cu->objfile->name);
14514
14515 retval.data = DW_BLOCK (attr)->data;
14516 retval.size = DW_BLOCK (attr)->size;
14517 }
14518 retval.per_cu = cu->per_cu;
14519
14520 age_cached_comp_units ();
14521
14522 return retval;
14523 }
14524
14525 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14526 PER_CU. */
14527
14528 struct type *
14529 dwarf2_get_die_type (cu_offset die_offset,
14530 struct dwarf2_per_cu_data *per_cu)
14531 {
14532 sect_offset die_offset_sect;
14533
14534 dw2_setup (per_cu->objfile);
14535
14536 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
14537 return get_die_type_at_offset (die_offset_sect, per_cu);
14538 }
14539
14540 /* Follow the signature attribute ATTR in SRC_DIE.
14541 On entry *REF_CU is the CU of SRC_DIE.
14542 On exit *REF_CU is the CU of the result. */
14543
14544 static struct die_info *
14545 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14546 struct dwarf2_cu **ref_cu)
14547 {
14548 struct objfile *objfile = (*ref_cu)->objfile;
14549 struct die_info temp_die;
14550 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14551 struct dwarf2_cu *sig_cu;
14552 struct die_info *die;
14553
14554 /* sig_type will be NULL if the signatured type is missing from
14555 the debug info. */
14556 if (sig_type == NULL)
14557 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14558 "at 0x%x [in module %s]"),
14559 src_die->offset.sect_off, objfile->name);
14560
14561 /* If necessary, add it to the queue and load its DIEs. */
14562
14563 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14564 read_signatured_type (sig_type);
14565
14566 gdb_assert (sig_type->per_cu.cu != NULL);
14567
14568 sig_cu = sig_type->per_cu.cu;
14569 temp_die.offset.sect_off = (sig_type->per_cu.offset.sect_off
14570 + sig_type->type_offset.cu_off);
14571 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
14572 temp_die.offset.sect_off);
14573 if (die)
14574 {
14575 *ref_cu = sig_cu;
14576 return die;
14577 }
14578
14579 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14580 "from DIE at 0x%x [in module %s]"),
14581 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
14582 }
14583
14584 /* Given an offset of a signatured type, return its signatured_type. */
14585
14586 static struct signatured_type *
14587 lookup_signatured_type_at_offset (struct objfile *objfile,
14588 struct dwarf2_section_info *section,
14589 sect_offset offset)
14590 {
14591 gdb_byte *info_ptr = section->buffer + offset.sect_off;
14592 unsigned int length, initial_length_size;
14593 unsigned int sig_offset;
14594 struct signatured_type find_entry, *sig_type;
14595
14596 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14597 sig_offset = (initial_length_size
14598 + 2 /*version*/
14599 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14600 + 1 /*address_size*/);
14601 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14602 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14603
14604 /* This is only used to lookup previously recorded types.
14605 If we didn't find it, it's our bug. */
14606 gdb_assert (sig_type != NULL);
14607 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
14608
14609 return sig_type;
14610 }
14611
14612 /* Load the DIEs associated with type unit PER_CU into memory. */
14613
14614 static void
14615 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14616 {
14617 struct objfile *objfile = per_cu->objfile;
14618 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14619 sect_offset offset = per_cu->offset;
14620 struct signatured_type *sig_type;
14621
14622 dwarf2_read_section (objfile, sect);
14623
14624 /* We have the section offset, but we need the signature to do the
14625 hash table lookup. */
14626 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14627 the signature to assert we found the right one.
14628 Ok, but it's a lot of work. We should simplify things so any needed
14629 assert doesn't require all this clumsiness. */
14630 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
14631
14632 gdb_assert (sig_type->per_cu.cu == NULL);
14633
14634 read_signatured_type (sig_type);
14635
14636 gdb_assert (sig_type->per_cu.cu != NULL);
14637 }
14638
14639 /* Read in a signatured type and build its CU and DIEs. */
14640
14641 static void
14642 read_signatured_type (struct signatured_type *sig_type)
14643 {
14644 struct objfile *objfile = sig_type->per_cu.objfile;
14645 gdb_byte *types_ptr;
14646 struct die_reader_specs reader_specs;
14647 struct dwarf2_cu *cu;
14648 ULONGEST signature;
14649 struct cleanup *back_to, *free_cu_cleanup;
14650 struct dwarf2_section_info *section = sig_type->per_cu.debug_types_section;
14651
14652 dwarf2_read_section (objfile, section);
14653 types_ptr = section->buffer + sig_type->per_cu.offset.sect_off;
14654
14655 gdb_assert (sig_type->per_cu.cu == NULL);
14656
14657 cu = xmalloc (sizeof (*cu));
14658 init_one_comp_unit (cu, &sig_type->per_cu);
14659
14660 /* If an error occurs while loading, release our storage. */
14661 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14662
14663 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14664 &signature, NULL);
14665 gdb_assert (signature == sig_type->signature);
14666
14667 cu->die_hash
14668 = htab_create_alloc_ex (cu->header.length / 12,
14669 die_hash,
14670 die_eq,
14671 NULL,
14672 &cu->comp_unit_obstack,
14673 hashtab_obstack_allocate,
14674 dummy_obstack_deallocate);
14675
14676 dwarf2_read_abbrevs (cu);
14677 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14678
14679 init_cu_die_reader (&reader_specs, cu);
14680
14681 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14682 NULL /*parent*/);
14683
14684 /* We try not to read any attributes in this function, because not
14685 all CUs needed for references have been loaded yet, and symbol
14686 table processing isn't initialized. But we have to set the CU language,
14687 or we won't be able to build types correctly. */
14688 prepare_one_comp_unit (cu, cu->dies);
14689
14690 do_cleanups (back_to);
14691
14692 /* We've successfully allocated this compilation unit. Let our caller
14693 clean it up when finished with it. */
14694 discard_cleanups (free_cu_cleanup);
14695
14696 /* Link this TU into read_in_chain. */
14697 sig_type->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14698 dwarf2_per_objfile->read_in_chain = &sig_type->per_cu;
14699 }
14700
14701 /* Decode simple location descriptions.
14702 Given a pointer to a dwarf block that defines a location, compute
14703 the location and return the value.
14704
14705 NOTE drow/2003-11-18: This function is called in two situations
14706 now: for the address of static or global variables (partial symbols
14707 only) and for offsets into structures which are expected to be
14708 (more or less) constant. The partial symbol case should go away,
14709 and only the constant case should remain. That will let this
14710 function complain more accurately. A few special modes are allowed
14711 without complaint for global variables (for instance, global
14712 register values and thread-local values).
14713
14714 A location description containing no operations indicates that the
14715 object is optimized out. The return value is 0 for that case.
14716 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14717 callers will only want a very basic result and this can become a
14718 complaint.
14719
14720 Note that stack[0] is unused except as a default error return. */
14721
14722 static CORE_ADDR
14723 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14724 {
14725 struct objfile *objfile = cu->objfile;
14726 int i;
14727 int size = blk->size;
14728 gdb_byte *data = blk->data;
14729 CORE_ADDR stack[64];
14730 int stacki;
14731 unsigned int bytes_read, unsnd;
14732 gdb_byte op;
14733
14734 i = 0;
14735 stacki = 0;
14736 stack[stacki] = 0;
14737 stack[++stacki] = 0;
14738
14739 while (i < size)
14740 {
14741 op = data[i++];
14742 switch (op)
14743 {
14744 case DW_OP_lit0:
14745 case DW_OP_lit1:
14746 case DW_OP_lit2:
14747 case DW_OP_lit3:
14748 case DW_OP_lit4:
14749 case DW_OP_lit5:
14750 case DW_OP_lit6:
14751 case DW_OP_lit7:
14752 case DW_OP_lit8:
14753 case DW_OP_lit9:
14754 case DW_OP_lit10:
14755 case DW_OP_lit11:
14756 case DW_OP_lit12:
14757 case DW_OP_lit13:
14758 case DW_OP_lit14:
14759 case DW_OP_lit15:
14760 case DW_OP_lit16:
14761 case DW_OP_lit17:
14762 case DW_OP_lit18:
14763 case DW_OP_lit19:
14764 case DW_OP_lit20:
14765 case DW_OP_lit21:
14766 case DW_OP_lit22:
14767 case DW_OP_lit23:
14768 case DW_OP_lit24:
14769 case DW_OP_lit25:
14770 case DW_OP_lit26:
14771 case DW_OP_lit27:
14772 case DW_OP_lit28:
14773 case DW_OP_lit29:
14774 case DW_OP_lit30:
14775 case DW_OP_lit31:
14776 stack[++stacki] = op - DW_OP_lit0;
14777 break;
14778
14779 case DW_OP_reg0:
14780 case DW_OP_reg1:
14781 case DW_OP_reg2:
14782 case DW_OP_reg3:
14783 case DW_OP_reg4:
14784 case DW_OP_reg5:
14785 case DW_OP_reg6:
14786 case DW_OP_reg7:
14787 case DW_OP_reg8:
14788 case DW_OP_reg9:
14789 case DW_OP_reg10:
14790 case DW_OP_reg11:
14791 case DW_OP_reg12:
14792 case DW_OP_reg13:
14793 case DW_OP_reg14:
14794 case DW_OP_reg15:
14795 case DW_OP_reg16:
14796 case DW_OP_reg17:
14797 case DW_OP_reg18:
14798 case DW_OP_reg19:
14799 case DW_OP_reg20:
14800 case DW_OP_reg21:
14801 case DW_OP_reg22:
14802 case DW_OP_reg23:
14803 case DW_OP_reg24:
14804 case DW_OP_reg25:
14805 case DW_OP_reg26:
14806 case DW_OP_reg27:
14807 case DW_OP_reg28:
14808 case DW_OP_reg29:
14809 case DW_OP_reg30:
14810 case DW_OP_reg31:
14811 stack[++stacki] = op - DW_OP_reg0;
14812 if (i < size)
14813 dwarf2_complex_location_expr_complaint ();
14814 break;
14815
14816 case DW_OP_regx:
14817 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14818 i += bytes_read;
14819 stack[++stacki] = unsnd;
14820 if (i < size)
14821 dwarf2_complex_location_expr_complaint ();
14822 break;
14823
14824 case DW_OP_addr:
14825 stack[++stacki] = read_address (objfile->obfd, &data[i],
14826 cu, &bytes_read);
14827 i += bytes_read;
14828 break;
14829
14830 case DW_OP_const1u:
14831 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14832 i += 1;
14833 break;
14834
14835 case DW_OP_const1s:
14836 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14837 i += 1;
14838 break;
14839
14840 case DW_OP_const2u:
14841 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14842 i += 2;
14843 break;
14844
14845 case DW_OP_const2s:
14846 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14847 i += 2;
14848 break;
14849
14850 case DW_OP_const4u:
14851 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14852 i += 4;
14853 break;
14854
14855 case DW_OP_const4s:
14856 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14857 i += 4;
14858 break;
14859
14860 case DW_OP_const8u:
14861 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14862 i += 8;
14863 break;
14864
14865 case DW_OP_constu:
14866 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14867 &bytes_read);
14868 i += bytes_read;
14869 break;
14870
14871 case DW_OP_consts:
14872 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14873 i += bytes_read;
14874 break;
14875
14876 case DW_OP_dup:
14877 stack[stacki + 1] = stack[stacki];
14878 stacki++;
14879 break;
14880
14881 case DW_OP_plus:
14882 stack[stacki - 1] += stack[stacki];
14883 stacki--;
14884 break;
14885
14886 case DW_OP_plus_uconst:
14887 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14888 &bytes_read);
14889 i += bytes_read;
14890 break;
14891
14892 case DW_OP_minus:
14893 stack[stacki - 1] -= stack[stacki];
14894 stacki--;
14895 break;
14896
14897 case DW_OP_deref:
14898 /* If we're not the last op, then we definitely can't encode
14899 this using GDB's address_class enum. This is valid for partial
14900 global symbols, although the variable's address will be bogus
14901 in the psymtab. */
14902 if (i < size)
14903 dwarf2_complex_location_expr_complaint ();
14904 break;
14905
14906 case DW_OP_GNU_push_tls_address:
14907 /* The top of the stack has the offset from the beginning
14908 of the thread control block at which the variable is located. */
14909 /* Nothing should follow this operator, so the top of stack would
14910 be returned. */
14911 /* This is valid for partial global symbols, but the variable's
14912 address will be bogus in the psymtab. Make it always at least
14913 non-zero to not look as a variable garbage collected by linker
14914 which have DW_OP_addr 0. */
14915 if (i < size)
14916 dwarf2_complex_location_expr_complaint ();
14917 stack[stacki]++;
14918 break;
14919
14920 case DW_OP_GNU_uninit:
14921 break;
14922
14923 default:
14924 {
14925 const char *name = dwarf_stack_op_name (op);
14926
14927 if (name)
14928 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14929 name);
14930 else
14931 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14932 op);
14933 }
14934
14935 return (stack[stacki]);
14936 }
14937
14938 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14939 outside of the allocated space. Also enforce minimum>0. */
14940 if (stacki >= ARRAY_SIZE (stack) - 1)
14941 {
14942 complaint (&symfile_complaints,
14943 _("location description stack overflow"));
14944 return 0;
14945 }
14946
14947 if (stacki <= 0)
14948 {
14949 complaint (&symfile_complaints,
14950 _("location description stack underflow"));
14951 return 0;
14952 }
14953 }
14954 return (stack[stacki]);
14955 }
14956
14957 /* memory allocation interface */
14958
14959 static struct dwarf_block *
14960 dwarf_alloc_block (struct dwarf2_cu *cu)
14961 {
14962 struct dwarf_block *blk;
14963
14964 blk = (struct dwarf_block *)
14965 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14966 return (blk);
14967 }
14968
14969 static struct abbrev_info *
14970 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14971 {
14972 struct abbrev_info *abbrev;
14973
14974 abbrev = (struct abbrev_info *)
14975 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14976 memset (abbrev, 0, sizeof (struct abbrev_info));
14977 return (abbrev);
14978 }
14979
14980 static struct die_info *
14981 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14982 {
14983 struct die_info *die;
14984 size_t size = sizeof (struct die_info);
14985
14986 if (num_attrs > 1)
14987 size += (num_attrs - 1) * sizeof (struct attribute);
14988
14989 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14990 memset (die, 0, sizeof (struct die_info));
14991 return (die);
14992 }
14993
14994 \f
14995 /* Macro support. */
14996
14997 /* Return the full name of file number I in *LH's file name table.
14998 Use COMP_DIR as the name of the current directory of the
14999 compilation. The result is allocated using xmalloc; the caller is
15000 responsible for freeing it. */
15001 static char *
15002 file_full_name (int file, struct line_header *lh, const char *comp_dir)
15003 {
15004 /* Is the file number a valid index into the line header's file name
15005 table? Remember that file numbers start with one, not zero. */
15006 if (1 <= file && file <= lh->num_file_names)
15007 {
15008 struct file_entry *fe = &lh->file_names[file - 1];
15009
15010 if (IS_ABSOLUTE_PATH (fe->name))
15011 return xstrdup (fe->name);
15012 else
15013 {
15014 const char *dir;
15015 int dir_len;
15016 char *full_name;
15017
15018 if (fe->dir_index)
15019 dir = lh->include_dirs[fe->dir_index - 1];
15020 else
15021 dir = comp_dir;
15022
15023 if (dir)
15024 {
15025 dir_len = strlen (dir);
15026 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15027 strcpy (full_name, dir);
15028 full_name[dir_len] = '/';
15029 strcpy (full_name + dir_len + 1, fe->name);
15030 return full_name;
15031 }
15032 else
15033 return xstrdup (fe->name);
15034 }
15035 }
15036 else
15037 {
15038 /* The compiler produced a bogus file number. We can at least
15039 record the macro definitions made in the file, even if we
15040 won't be able to find the file by name. */
15041 char fake_name[80];
15042
15043 sprintf (fake_name, "<bad macro file number %d>", file);
15044
15045 complaint (&symfile_complaints,
15046 _("bad file number in macro information (%d)"),
15047 file);
15048
15049 return xstrdup (fake_name);
15050 }
15051 }
15052
15053
15054 static struct macro_source_file *
15055 macro_start_file (int file, int line,
15056 struct macro_source_file *current_file,
15057 const char *comp_dir,
15058 struct line_header *lh, struct objfile *objfile)
15059 {
15060 /* The full name of this source file. */
15061 char *full_name = file_full_name (file, lh, comp_dir);
15062
15063 /* We don't create a macro table for this compilation unit
15064 at all until we actually get a filename. */
15065 if (! pending_macros)
15066 pending_macros = new_macro_table (&objfile->objfile_obstack,
15067 objfile->macro_cache);
15068
15069 if (! current_file)
15070 /* If we have no current file, then this must be the start_file
15071 directive for the compilation unit's main source file. */
15072 current_file = macro_set_main (pending_macros, full_name);
15073 else
15074 current_file = macro_include (current_file, line, full_name);
15075
15076 xfree (full_name);
15077
15078 return current_file;
15079 }
15080
15081
15082 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15083 followed by a null byte. */
15084 static char *
15085 copy_string (const char *buf, int len)
15086 {
15087 char *s = xmalloc (len + 1);
15088
15089 memcpy (s, buf, len);
15090 s[len] = '\0';
15091 return s;
15092 }
15093
15094
15095 static const char *
15096 consume_improper_spaces (const char *p, const char *body)
15097 {
15098 if (*p == ' ')
15099 {
15100 complaint (&symfile_complaints,
15101 _("macro definition contains spaces "
15102 "in formal argument list:\n`%s'"),
15103 body);
15104
15105 while (*p == ' ')
15106 p++;
15107 }
15108
15109 return p;
15110 }
15111
15112
15113 static void
15114 parse_macro_definition (struct macro_source_file *file, int line,
15115 const char *body)
15116 {
15117 const char *p;
15118
15119 /* The body string takes one of two forms. For object-like macro
15120 definitions, it should be:
15121
15122 <macro name> " " <definition>
15123
15124 For function-like macro definitions, it should be:
15125
15126 <macro name> "() " <definition>
15127 or
15128 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15129
15130 Spaces may appear only where explicitly indicated, and in the
15131 <definition>.
15132
15133 The Dwarf 2 spec says that an object-like macro's name is always
15134 followed by a space, but versions of GCC around March 2002 omit
15135 the space when the macro's definition is the empty string.
15136
15137 The Dwarf 2 spec says that there should be no spaces between the
15138 formal arguments in a function-like macro's formal argument list,
15139 but versions of GCC around March 2002 include spaces after the
15140 commas. */
15141
15142
15143 /* Find the extent of the macro name. The macro name is terminated
15144 by either a space or null character (for an object-like macro) or
15145 an opening paren (for a function-like macro). */
15146 for (p = body; *p; p++)
15147 if (*p == ' ' || *p == '(')
15148 break;
15149
15150 if (*p == ' ' || *p == '\0')
15151 {
15152 /* It's an object-like macro. */
15153 int name_len = p - body;
15154 char *name = copy_string (body, name_len);
15155 const char *replacement;
15156
15157 if (*p == ' ')
15158 replacement = body + name_len + 1;
15159 else
15160 {
15161 dwarf2_macro_malformed_definition_complaint (body);
15162 replacement = body + name_len;
15163 }
15164
15165 macro_define_object (file, line, name, replacement);
15166
15167 xfree (name);
15168 }
15169 else if (*p == '(')
15170 {
15171 /* It's a function-like macro. */
15172 char *name = copy_string (body, p - body);
15173 int argc = 0;
15174 int argv_size = 1;
15175 char **argv = xmalloc (argv_size * sizeof (*argv));
15176
15177 p++;
15178
15179 p = consume_improper_spaces (p, body);
15180
15181 /* Parse the formal argument list. */
15182 while (*p && *p != ')')
15183 {
15184 /* Find the extent of the current argument name. */
15185 const char *arg_start = p;
15186
15187 while (*p && *p != ',' && *p != ')' && *p != ' ')
15188 p++;
15189
15190 if (! *p || p == arg_start)
15191 dwarf2_macro_malformed_definition_complaint (body);
15192 else
15193 {
15194 /* Make sure argv has room for the new argument. */
15195 if (argc >= argv_size)
15196 {
15197 argv_size *= 2;
15198 argv = xrealloc (argv, argv_size * sizeof (*argv));
15199 }
15200
15201 argv[argc++] = copy_string (arg_start, p - arg_start);
15202 }
15203
15204 p = consume_improper_spaces (p, body);
15205
15206 /* Consume the comma, if present. */
15207 if (*p == ',')
15208 {
15209 p++;
15210
15211 p = consume_improper_spaces (p, body);
15212 }
15213 }
15214
15215 if (*p == ')')
15216 {
15217 p++;
15218
15219 if (*p == ' ')
15220 /* Perfectly formed definition, no complaints. */
15221 macro_define_function (file, line, name,
15222 argc, (const char **) argv,
15223 p + 1);
15224 else if (*p == '\0')
15225 {
15226 /* Complain, but do define it. */
15227 dwarf2_macro_malformed_definition_complaint (body);
15228 macro_define_function (file, line, name,
15229 argc, (const char **) argv,
15230 p);
15231 }
15232 else
15233 /* Just complain. */
15234 dwarf2_macro_malformed_definition_complaint (body);
15235 }
15236 else
15237 /* Just complain. */
15238 dwarf2_macro_malformed_definition_complaint (body);
15239
15240 xfree (name);
15241 {
15242 int i;
15243
15244 for (i = 0; i < argc; i++)
15245 xfree (argv[i]);
15246 }
15247 xfree (argv);
15248 }
15249 else
15250 dwarf2_macro_malformed_definition_complaint (body);
15251 }
15252
15253 /* Skip some bytes from BYTES according to the form given in FORM.
15254 Returns the new pointer. */
15255
15256 static gdb_byte *
15257 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15258 enum dwarf_form form,
15259 unsigned int offset_size,
15260 struct dwarf2_section_info *section)
15261 {
15262 unsigned int bytes_read;
15263
15264 switch (form)
15265 {
15266 case DW_FORM_data1:
15267 case DW_FORM_flag:
15268 ++bytes;
15269 break;
15270
15271 case DW_FORM_data2:
15272 bytes += 2;
15273 break;
15274
15275 case DW_FORM_data4:
15276 bytes += 4;
15277 break;
15278
15279 case DW_FORM_data8:
15280 bytes += 8;
15281 break;
15282
15283 case DW_FORM_string:
15284 read_direct_string (abfd, bytes, &bytes_read);
15285 bytes += bytes_read;
15286 break;
15287
15288 case DW_FORM_sec_offset:
15289 case DW_FORM_strp:
15290 bytes += offset_size;
15291 break;
15292
15293 case DW_FORM_block:
15294 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15295 bytes += bytes_read;
15296 break;
15297
15298 case DW_FORM_block1:
15299 bytes += 1 + read_1_byte (abfd, bytes);
15300 break;
15301 case DW_FORM_block2:
15302 bytes += 2 + read_2_bytes (abfd, bytes);
15303 break;
15304 case DW_FORM_block4:
15305 bytes += 4 + read_4_bytes (abfd, bytes);
15306 break;
15307
15308 case DW_FORM_sdata:
15309 case DW_FORM_udata:
15310 bytes = skip_leb128 (abfd, bytes);
15311 break;
15312
15313 default:
15314 {
15315 complain:
15316 complaint (&symfile_complaints,
15317 _("invalid form 0x%x in `%s'"),
15318 form,
15319 section->asection->name);
15320 return NULL;
15321 }
15322 }
15323
15324 return bytes;
15325 }
15326
15327 /* A helper for dwarf_decode_macros that handles skipping an unknown
15328 opcode. Returns an updated pointer to the macro data buffer; or,
15329 on error, issues a complaint and returns NULL. */
15330
15331 static gdb_byte *
15332 skip_unknown_opcode (unsigned int opcode,
15333 gdb_byte **opcode_definitions,
15334 gdb_byte *mac_ptr,
15335 bfd *abfd,
15336 unsigned int offset_size,
15337 struct dwarf2_section_info *section)
15338 {
15339 unsigned int bytes_read, i;
15340 unsigned long arg;
15341 gdb_byte *defn;
15342
15343 if (opcode_definitions[opcode] == NULL)
15344 {
15345 complaint (&symfile_complaints,
15346 _("unrecognized DW_MACFINO opcode 0x%x"),
15347 opcode);
15348 return NULL;
15349 }
15350
15351 defn = opcode_definitions[opcode];
15352 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15353 defn += bytes_read;
15354
15355 for (i = 0; i < arg; ++i)
15356 {
15357 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15358 if (mac_ptr == NULL)
15359 {
15360 /* skip_form_bytes already issued the complaint. */
15361 return NULL;
15362 }
15363 }
15364
15365 return mac_ptr;
15366 }
15367
15368 /* A helper function which parses the header of a macro section.
15369 If the macro section is the extended (for now called "GNU") type,
15370 then this updates *OFFSET_SIZE. Returns a pointer to just after
15371 the header, or issues a complaint and returns NULL on error. */
15372
15373 static gdb_byte *
15374 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15375 bfd *abfd,
15376 gdb_byte *mac_ptr,
15377 unsigned int *offset_size,
15378 int section_is_gnu)
15379 {
15380 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15381
15382 if (section_is_gnu)
15383 {
15384 unsigned int version, flags;
15385
15386 version = read_2_bytes (abfd, mac_ptr);
15387 if (version != 4)
15388 {
15389 complaint (&symfile_complaints,
15390 _("unrecognized version `%d' in .debug_macro section"),
15391 version);
15392 return NULL;
15393 }
15394 mac_ptr += 2;
15395
15396 flags = read_1_byte (abfd, mac_ptr);
15397 ++mac_ptr;
15398 *offset_size = (flags & 1) ? 8 : 4;
15399
15400 if ((flags & 2) != 0)
15401 /* We don't need the line table offset. */
15402 mac_ptr += *offset_size;
15403
15404 /* Vendor opcode descriptions. */
15405 if ((flags & 4) != 0)
15406 {
15407 unsigned int i, count;
15408
15409 count = read_1_byte (abfd, mac_ptr);
15410 ++mac_ptr;
15411 for (i = 0; i < count; ++i)
15412 {
15413 unsigned int opcode, bytes_read;
15414 unsigned long arg;
15415
15416 opcode = read_1_byte (abfd, mac_ptr);
15417 ++mac_ptr;
15418 opcode_definitions[opcode] = mac_ptr;
15419 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15420 mac_ptr += bytes_read;
15421 mac_ptr += arg;
15422 }
15423 }
15424 }
15425
15426 return mac_ptr;
15427 }
15428
15429 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15430 including DW_MACRO_GNU_transparent_include. */
15431
15432 static void
15433 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15434 struct macro_source_file *current_file,
15435 struct line_header *lh, char *comp_dir,
15436 struct dwarf2_section_info *section,
15437 int section_is_gnu,
15438 unsigned int offset_size,
15439 struct objfile *objfile,
15440 htab_t include_hash)
15441 {
15442 enum dwarf_macro_record_type macinfo_type;
15443 int at_commandline;
15444 gdb_byte *opcode_definitions[256];
15445
15446 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15447 &offset_size, section_is_gnu);
15448 if (mac_ptr == NULL)
15449 {
15450 /* We already issued a complaint. */
15451 return;
15452 }
15453
15454 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15455 GDB is still reading the definitions from command line. First
15456 DW_MACINFO_start_file will need to be ignored as it was already executed
15457 to create CURRENT_FILE for the main source holding also the command line
15458 definitions. On first met DW_MACINFO_start_file this flag is reset to
15459 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15460
15461 at_commandline = 1;
15462
15463 do
15464 {
15465 /* Do we at least have room for a macinfo type byte? */
15466 if (mac_ptr >= mac_end)
15467 {
15468 dwarf2_macros_too_long_complaint (section);
15469 break;
15470 }
15471
15472 macinfo_type = read_1_byte (abfd, mac_ptr);
15473 mac_ptr++;
15474
15475 /* Note that we rely on the fact that the corresponding GNU and
15476 DWARF constants are the same. */
15477 switch (macinfo_type)
15478 {
15479 /* A zero macinfo type indicates the end of the macro
15480 information. */
15481 case 0:
15482 break;
15483
15484 case DW_MACRO_GNU_define:
15485 case DW_MACRO_GNU_undef:
15486 case DW_MACRO_GNU_define_indirect:
15487 case DW_MACRO_GNU_undef_indirect:
15488 {
15489 unsigned int bytes_read;
15490 int line;
15491 char *body;
15492 int is_define;
15493
15494 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15495 mac_ptr += bytes_read;
15496
15497 if (macinfo_type == DW_MACRO_GNU_define
15498 || macinfo_type == DW_MACRO_GNU_undef)
15499 {
15500 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15501 mac_ptr += bytes_read;
15502 }
15503 else
15504 {
15505 LONGEST str_offset;
15506
15507 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15508 mac_ptr += offset_size;
15509
15510 body = read_indirect_string_at_offset (abfd, str_offset);
15511 }
15512
15513 is_define = (macinfo_type == DW_MACRO_GNU_define
15514 || macinfo_type == DW_MACRO_GNU_define_indirect);
15515 if (! current_file)
15516 {
15517 /* DWARF violation as no main source is present. */
15518 complaint (&symfile_complaints,
15519 _("debug info with no main source gives macro %s "
15520 "on line %d: %s"),
15521 is_define ? _("definition") : _("undefinition"),
15522 line, body);
15523 break;
15524 }
15525 if ((line == 0 && !at_commandline)
15526 || (line != 0 && at_commandline))
15527 complaint (&symfile_complaints,
15528 _("debug info gives %s macro %s with %s line %d: %s"),
15529 at_commandline ? _("command-line") : _("in-file"),
15530 is_define ? _("definition") : _("undefinition"),
15531 line == 0 ? _("zero") : _("non-zero"), line, body);
15532
15533 if (is_define)
15534 parse_macro_definition (current_file, line, body);
15535 else
15536 {
15537 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15538 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15539 macro_undef (current_file, line, body);
15540 }
15541 }
15542 break;
15543
15544 case DW_MACRO_GNU_start_file:
15545 {
15546 unsigned int bytes_read;
15547 int line, file;
15548
15549 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15550 mac_ptr += bytes_read;
15551 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15552 mac_ptr += bytes_read;
15553
15554 if ((line == 0 && !at_commandline)
15555 || (line != 0 && at_commandline))
15556 complaint (&symfile_complaints,
15557 _("debug info gives source %d included "
15558 "from %s at %s line %d"),
15559 file, at_commandline ? _("command-line") : _("file"),
15560 line == 0 ? _("zero") : _("non-zero"), line);
15561
15562 if (at_commandline)
15563 {
15564 /* This DW_MACRO_GNU_start_file was executed in the
15565 pass one. */
15566 at_commandline = 0;
15567 }
15568 else
15569 current_file = macro_start_file (file, line,
15570 current_file, comp_dir,
15571 lh, objfile);
15572 }
15573 break;
15574
15575 case DW_MACRO_GNU_end_file:
15576 if (! current_file)
15577 complaint (&symfile_complaints,
15578 _("macro debug info has an unmatched "
15579 "`close_file' directive"));
15580 else
15581 {
15582 current_file = current_file->included_by;
15583 if (! current_file)
15584 {
15585 enum dwarf_macro_record_type next_type;
15586
15587 /* GCC circa March 2002 doesn't produce the zero
15588 type byte marking the end of the compilation
15589 unit. Complain if it's not there, but exit no
15590 matter what. */
15591
15592 /* Do we at least have room for a macinfo type byte? */
15593 if (mac_ptr >= mac_end)
15594 {
15595 dwarf2_macros_too_long_complaint (section);
15596 return;
15597 }
15598
15599 /* We don't increment mac_ptr here, so this is just
15600 a look-ahead. */
15601 next_type = read_1_byte (abfd, mac_ptr);
15602 if (next_type != 0)
15603 complaint (&symfile_complaints,
15604 _("no terminating 0-type entry for "
15605 "macros in `.debug_macinfo' section"));
15606
15607 return;
15608 }
15609 }
15610 break;
15611
15612 case DW_MACRO_GNU_transparent_include:
15613 {
15614 LONGEST offset;
15615 void **slot;
15616
15617 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15618 mac_ptr += offset_size;
15619
15620 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15621 if (*slot != NULL)
15622 {
15623 /* This has actually happened; see
15624 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15625 complaint (&symfile_complaints,
15626 _("recursive DW_MACRO_GNU_transparent_include in "
15627 ".debug_macro section"));
15628 }
15629 else
15630 {
15631 *slot = mac_ptr;
15632
15633 dwarf_decode_macro_bytes (abfd,
15634 section->buffer + offset,
15635 mac_end, current_file,
15636 lh, comp_dir,
15637 section, section_is_gnu,
15638 offset_size, objfile, include_hash);
15639
15640 htab_remove_elt (include_hash, mac_ptr);
15641 }
15642 }
15643 break;
15644
15645 case DW_MACINFO_vendor_ext:
15646 if (!section_is_gnu)
15647 {
15648 unsigned int bytes_read;
15649 int constant;
15650
15651 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15652 mac_ptr += bytes_read;
15653 read_direct_string (abfd, mac_ptr, &bytes_read);
15654 mac_ptr += bytes_read;
15655
15656 /* We don't recognize any vendor extensions. */
15657 break;
15658 }
15659 /* FALLTHROUGH */
15660
15661 default:
15662 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15663 mac_ptr, abfd, offset_size,
15664 section);
15665 if (mac_ptr == NULL)
15666 return;
15667 break;
15668 }
15669 } while (macinfo_type != 0);
15670 }
15671
15672 static void
15673 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15674 char *comp_dir, bfd *abfd,
15675 struct dwarf2_cu *cu,
15676 struct dwarf2_section_info *section,
15677 int section_is_gnu)
15678 {
15679 struct objfile *objfile = dwarf2_per_objfile->objfile;
15680 gdb_byte *mac_ptr, *mac_end;
15681 struct macro_source_file *current_file = 0;
15682 enum dwarf_macro_record_type macinfo_type;
15683 unsigned int offset_size = cu->header.offset_size;
15684 gdb_byte *opcode_definitions[256];
15685 struct cleanup *cleanup;
15686 htab_t include_hash;
15687 void **slot;
15688
15689 dwarf2_read_section (objfile, section);
15690 if (section->buffer == NULL)
15691 {
15692 complaint (&symfile_complaints, _("missing %s section"),
15693 section->asection->name);
15694 return;
15695 }
15696
15697 /* First pass: Find the name of the base filename.
15698 This filename is needed in order to process all macros whose definition
15699 (or undefinition) comes from the command line. These macros are defined
15700 before the first DW_MACINFO_start_file entry, and yet still need to be
15701 associated to the base file.
15702
15703 To determine the base file name, we scan the macro definitions until we
15704 reach the first DW_MACINFO_start_file entry. We then initialize
15705 CURRENT_FILE accordingly so that any macro definition found before the
15706 first DW_MACINFO_start_file can still be associated to the base file. */
15707
15708 mac_ptr = section->buffer + offset;
15709 mac_end = section->buffer + section->size;
15710
15711 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15712 &offset_size, section_is_gnu);
15713 if (mac_ptr == NULL)
15714 {
15715 /* We already issued a complaint. */
15716 return;
15717 }
15718
15719 do
15720 {
15721 /* Do we at least have room for a macinfo type byte? */
15722 if (mac_ptr >= mac_end)
15723 {
15724 /* Complaint is printed during the second pass as GDB will probably
15725 stop the first pass earlier upon finding
15726 DW_MACINFO_start_file. */
15727 break;
15728 }
15729
15730 macinfo_type = read_1_byte (abfd, mac_ptr);
15731 mac_ptr++;
15732
15733 /* Note that we rely on the fact that the corresponding GNU and
15734 DWARF constants are the same. */
15735 switch (macinfo_type)
15736 {
15737 /* A zero macinfo type indicates the end of the macro
15738 information. */
15739 case 0:
15740 break;
15741
15742 case DW_MACRO_GNU_define:
15743 case DW_MACRO_GNU_undef:
15744 /* Only skip the data by MAC_PTR. */
15745 {
15746 unsigned int bytes_read;
15747
15748 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15749 mac_ptr += bytes_read;
15750 read_direct_string (abfd, mac_ptr, &bytes_read);
15751 mac_ptr += bytes_read;
15752 }
15753 break;
15754
15755 case DW_MACRO_GNU_start_file:
15756 {
15757 unsigned int bytes_read;
15758 int line, file;
15759
15760 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15761 mac_ptr += bytes_read;
15762 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15763 mac_ptr += bytes_read;
15764
15765 current_file = macro_start_file (file, line, current_file,
15766 comp_dir, lh, objfile);
15767 }
15768 break;
15769
15770 case DW_MACRO_GNU_end_file:
15771 /* No data to skip by MAC_PTR. */
15772 break;
15773
15774 case DW_MACRO_GNU_define_indirect:
15775 case DW_MACRO_GNU_undef_indirect:
15776 {
15777 unsigned int bytes_read;
15778
15779 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15780 mac_ptr += bytes_read;
15781 mac_ptr += offset_size;
15782 }
15783 break;
15784
15785 case DW_MACRO_GNU_transparent_include:
15786 /* Note that, according to the spec, a transparent include
15787 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15788 skip this opcode. */
15789 mac_ptr += offset_size;
15790 break;
15791
15792 case DW_MACINFO_vendor_ext:
15793 /* Only skip the data by MAC_PTR. */
15794 if (!section_is_gnu)
15795 {
15796 unsigned int bytes_read;
15797
15798 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15799 mac_ptr += bytes_read;
15800 read_direct_string (abfd, mac_ptr, &bytes_read);
15801 mac_ptr += bytes_read;
15802 }
15803 /* FALLTHROUGH */
15804
15805 default:
15806 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15807 mac_ptr, abfd, offset_size,
15808 section);
15809 if (mac_ptr == NULL)
15810 return;
15811 break;
15812 }
15813 } while (macinfo_type != 0 && current_file == NULL);
15814
15815 /* Second pass: Process all entries.
15816
15817 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15818 command-line macro definitions/undefinitions. This flag is unset when we
15819 reach the first DW_MACINFO_start_file entry. */
15820
15821 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15822 NULL, xcalloc, xfree);
15823 cleanup = make_cleanup_htab_delete (include_hash);
15824 mac_ptr = section->buffer + offset;
15825 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15826 *slot = mac_ptr;
15827 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
15828 current_file, lh, comp_dir, section, section_is_gnu,
15829 offset_size, objfile, include_hash);
15830 do_cleanups (cleanup);
15831 }
15832
15833 /* Check if the attribute's form is a DW_FORM_block*
15834 if so return true else false. */
15835
15836 static int
15837 attr_form_is_block (struct attribute *attr)
15838 {
15839 return (attr == NULL ? 0 :
15840 attr->form == DW_FORM_block1
15841 || attr->form == DW_FORM_block2
15842 || attr->form == DW_FORM_block4
15843 || attr->form == DW_FORM_block
15844 || attr->form == DW_FORM_exprloc);
15845 }
15846
15847 /* Return non-zero if ATTR's value is a section offset --- classes
15848 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15849 You may use DW_UNSND (attr) to retrieve such offsets.
15850
15851 Section 7.5.4, "Attribute Encodings", explains that no attribute
15852 may have a value that belongs to more than one of these classes; it
15853 would be ambiguous if we did, because we use the same forms for all
15854 of them. */
15855
15856 static int
15857 attr_form_is_section_offset (struct attribute *attr)
15858 {
15859 return (attr->form == DW_FORM_data4
15860 || attr->form == DW_FORM_data8
15861 || attr->form == DW_FORM_sec_offset);
15862 }
15863
15864
15865 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15866 zero otherwise. When this function returns true, you can apply
15867 dwarf2_get_attr_constant_value to it.
15868
15869 However, note that for some attributes you must check
15870 attr_form_is_section_offset before using this test. DW_FORM_data4
15871 and DW_FORM_data8 are members of both the constant class, and of
15872 the classes that contain offsets into other debug sections
15873 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15874 that, if an attribute's can be either a constant or one of the
15875 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15876 taken as section offsets, not constants. */
15877
15878 static int
15879 attr_form_is_constant (struct attribute *attr)
15880 {
15881 switch (attr->form)
15882 {
15883 case DW_FORM_sdata:
15884 case DW_FORM_udata:
15885 case DW_FORM_data1:
15886 case DW_FORM_data2:
15887 case DW_FORM_data4:
15888 case DW_FORM_data8:
15889 return 1;
15890 default:
15891 return 0;
15892 }
15893 }
15894
15895 /* A helper function that fills in a dwarf2_loclist_baton. */
15896
15897 static void
15898 fill_in_loclist_baton (struct dwarf2_cu *cu,
15899 struct dwarf2_loclist_baton *baton,
15900 struct attribute *attr)
15901 {
15902 dwarf2_read_section (dwarf2_per_objfile->objfile,
15903 &dwarf2_per_objfile->loc);
15904
15905 baton->per_cu = cu->per_cu;
15906 gdb_assert (baton->per_cu);
15907 /* We don't know how long the location list is, but make sure we
15908 don't run off the edge of the section. */
15909 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15910 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15911 baton->base_address = cu->base_address;
15912 }
15913
15914 static void
15915 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15916 struct dwarf2_cu *cu)
15917 {
15918 struct objfile *objfile = dwarf2_per_objfile->objfile;
15919
15920 if (attr_form_is_section_offset (attr)
15921 /* ".debug_loc" may not exist at all, or the offset may be outside
15922 the section. If so, fall through to the complaint in the
15923 other branch. */
15924 && DW_UNSND (attr) < dwarf2_section_size (objfile,
15925 &dwarf2_per_objfile->loc))
15926 {
15927 struct dwarf2_loclist_baton *baton;
15928
15929 baton = obstack_alloc (&objfile->objfile_obstack,
15930 sizeof (struct dwarf2_loclist_baton));
15931
15932 fill_in_loclist_baton (cu, baton, attr);
15933
15934 if (cu->base_known == 0)
15935 complaint (&symfile_complaints,
15936 _("Location list used without "
15937 "specifying the CU base address."));
15938
15939 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15940 SYMBOL_LOCATION_BATON (sym) = baton;
15941 }
15942 else
15943 {
15944 struct dwarf2_locexpr_baton *baton;
15945
15946 baton = obstack_alloc (&objfile->objfile_obstack,
15947 sizeof (struct dwarf2_locexpr_baton));
15948 baton->per_cu = cu->per_cu;
15949 gdb_assert (baton->per_cu);
15950
15951 if (attr_form_is_block (attr))
15952 {
15953 /* Note that we're just copying the block's data pointer
15954 here, not the actual data. We're still pointing into the
15955 info_buffer for SYM's objfile; right now we never release
15956 that buffer, but when we do clean up properly this may
15957 need to change. */
15958 baton->size = DW_BLOCK (attr)->size;
15959 baton->data = DW_BLOCK (attr)->data;
15960 }
15961 else
15962 {
15963 dwarf2_invalid_attrib_class_complaint ("location description",
15964 SYMBOL_NATURAL_NAME (sym));
15965 baton->size = 0;
15966 }
15967
15968 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15969 SYMBOL_LOCATION_BATON (sym) = baton;
15970 }
15971 }
15972
15973 /* Return the OBJFILE associated with the compilation unit CU. If CU
15974 came from a separate debuginfo file, then the master objfile is
15975 returned. */
15976
15977 struct objfile *
15978 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15979 {
15980 struct objfile *objfile = per_cu->objfile;
15981
15982 /* Return the master objfile, so that we can report and look up the
15983 correct file containing this variable. */
15984 if (objfile->separate_debug_objfile_backlink)
15985 objfile = objfile->separate_debug_objfile_backlink;
15986
15987 return objfile;
15988 }
15989
15990 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15991 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15992 CU_HEADERP first. */
15993
15994 static const struct comp_unit_head *
15995 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15996 struct dwarf2_per_cu_data *per_cu)
15997 {
15998 struct objfile *objfile;
15999 struct dwarf2_per_objfile *per_objfile;
16000 gdb_byte *info_ptr;
16001
16002 if (per_cu->cu)
16003 return &per_cu->cu->header;
16004
16005 objfile = per_cu->objfile;
16006 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16007 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
16008
16009 memset (cu_headerp, 0, sizeof (*cu_headerp));
16010 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16011
16012 return cu_headerp;
16013 }
16014
16015 /* Return the address size given in the compilation unit header for CU. */
16016
16017 int
16018 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16019 {
16020 struct comp_unit_head cu_header_local;
16021 const struct comp_unit_head *cu_headerp;
16022
16023 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16024
16025 return cu_headerp->addr_size;
16026 }
16027
16028 /* Return the offset size given in the compilation unit header for CU. */
16029
16030 int
16031 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16032 {
16033 struct comp_unit_head cu_header_local;
16034 const struct comp_unit_head *cu_headerp;
16035
16036 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16037
16038 return cu_headerp->offset_size;
16039 }
16040
16041 /* See its dwarf2loc.h declaration. */
16042
16043 int
16044 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16045 {
16046 struct comp_unit_head cu_header_local;
16047 const struct comp_unit_head *cu_headerp;
16048
16049 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16050
16051 if (cu_headerp->version == 2)
16052 return cu_headerp->addr_size;
16053 else
16054 return cu_headerp->offset_size;
16055 }
16056
16057 /* Return the text offset of the CU. The returned offset comes from
16058 this CU's objfile. If this objfile came from a separate debuginfo
16059 file, then the offset may be different from the corresponding
16060 offset in the parent objfile. */
16061
16062 CORE_ADDR
16063 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16064 {
16065 struct objfile *objfile = per_cu->objfile;
16066
16067 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16068 }
16069
16070 /* Locate the .debug_info compilation unit from CU's objfile which contains
16071 the DIE at OFFSET. Raises an error on failure. */
16072
16073 static struct dwarf2_per_cu_data *
16074 dwarf2_find_containing_comp_unit (sect_offset offset,
16075 struct objfile *objfile)
16076 {
16077 struct dwarf2_per_cu_data *this_cu;
16078 int low, high;
16079
16080 low = 0;
16081 high = dwarf2_per_objfile->n_comp_units - 1;
16082 while (high > low)
16083 {
16084 int mid = low + (high - low) / 2;
16085
16086 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16087 >= offset.sect_off)
16088 high = mid;
16089 else
16090 low = mid + 1;
16091 }
16092 gdb_assert (low == high);
16093 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16094 > offset.sect_off)
16095 {
16096 if (low == 0)
16097 error (_("Dwarf Error: could not find partial DIE containing "
16098 "offset 0x%lx [in module %s]"),
16099 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16100
16101 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16102 <= offset.sect_off);
16103 return dwarf2_per_objfile->all_comp_units[low-1];
16104 }
16105 else
16106 {
16107 this_cu = dwarf2_per_objfile->all_comp_units[low];
16108 if (low == dwarf2_per_objfile->n_comp_units - 1
16109 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16110 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16111 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16112 return this_cu;
16113 }
16114 }
16115
16116 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16117
16118 static void
16119 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16120 {
16121 memset (cu, 0, sizeof (*cu));
16122 per_cu->cu = cu;
16123 cu->per_cu = per_cu;
16124 cu->objfile = per_cu->objfile;
16125 obstack_init (&cu->comp_unit_obstack);
16126 }
16127
16128 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16129
16130 static void
16131 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
16132 {
16133 struct attribute *attr;
16134
16135 /* Set the language we're debugging. */
16136 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16137 if (attr)
16138 set_cu_language (DW_UNSND (attr), cu);
16139 else
16140 {
16141 cu->language = language_minimal;
16142 cu->language_defn = language_def (cu->language);
16143 }
16144 }
16145
16146 /* Release one cached compilation unit, CU. We unlink it from the tree
16147 of compilation units, but we don't remove it from the read_in_chain;
16148 the caller is responsible for that.
16149 NOTE: DATA is a void * because this function is also used as a
16150 cleanup routine. */
16151
16152 static void
16153 free_heap_comp_unit (void *data)
16154 {
16155 struct dwarf2_cu *cu = data;
16156
16157 gdb_assert (cu->per_cu != NULL);
16158 cu->per_cu->cu = NULL;
16159 cu->per_cu = NULL;
16160
16161 obstack_free (&cu->comp_unit_obstack, NULL);
16162
16163 xfree (cu);
16164 }
16165
16166 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16167 when we're finished with it. We can't free the pointer itself, but be
16168 sure to unlink it from the cache. Also release any associated storage
16169 and perform cache maintenance.
16170
16171 Only used during partial symbol parsing. */
16172
16173 static void
16174 free_stack_comp_unit (void *data)
16175 {
16176 struct dwarf2_cu *cu = data;
16177
16178 gdb_assert (cu->per_cu != NULL);
16179 cu->per_cu->cu = NULL;
16180 cu->per_cu = NULL;
16181
16182 obstack_free (&cu->comp_unit_obstack, NULL);
16183 cu->partial_dies = NULL;
16184
16185 /* The previous code only did this if per_cu != NULL.
16186 But that would always succeed, so now we just unconditionally do
16187 the aging. This seems like the wrong place to do such aging,
16188 but cleaning that up is left for later. */
16189 age_cached_comp_units ();
16190 }
16191
16192 /* Free all cached compilation units. */
16193
16194 static void
16195 free_cached_comp_units (void *data)
16196 {
16197 struct dwarf2_per_cu_data *per_cu, **last_chain;
16198
16199 per_cu = dwarf2_per_objfile->read_in_chain;
16200 last_chain = &dwarf2_per_objfile->read_in_chain;
16201 while (per_cu != NULL)
16202 {
16203 struct dwarf2_per_cu_data *next_cu;
16204
16205 next_cu = per_cu->cu->read_in_chain;
16206
16207 free_heap_comp_unit (per_cu->cu);
16208 *last_chain = next_cu;
16209
16210 per_cu = next_cu;
16211 }
16212 }
16213
16214 /* Increase the age counter on each cached compilation unit, and free
16215 any that are too old. */
16216
16217 static void
16218 age_cached_comp_units (void)
16219 {
16220 struct dwarf2_per_cu_data *per_cu, **last_chain;
16221
16222 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16223 per_cu = dwarf2_per_objfile->read_in_chain;
16224 while (per_cu != NULL)
16225 {
16226 per_cu->cu->last_used ++;
16227 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16228 dwarf2_mark (per_cu->cu);
16229 per_cu = per_cu->cu->read_in_chain;
16230 }
16231
16232 per_cu = dwarf2_per_objfile->read_in_chain;
16233 last_chain = &dwarf2_per_objfile->read_in_chain;
16234 while (per_cu != NULL)
16235 {
16236 struct dwarf2_per_cu_data *next_cu;
16237
16238 next_cu = per_cu->cu->read_in_chain;
16239
16240 if (!per_cu->cu->mark)
16241 {
16242 free_heap_comp_unit (per_cu->cu);
16243 *last_chain = next_cu;
16244 }
16245 else
16246 last_chain = &per_cu->cu->read_in_chain;
16247
16248 per_cu = next_cu;
16249 }
16250 }
16251
16252 /* Remove a single compilation unit from the cache. */
16253
16254 static void
16255 free_one_cached_comp_unit (void *target_cu)
16256 {
16257 struct dwarf2_per_cu_data *per_cu, **last_chain;
16258
16259 per_cu = dwarf2_per_objfile->read_in_chain;
16260 last_chain = &dwarf2_per_objfile->read_in_chain;
16261 while (per_cu != NULL)
16262 {
16263 struct dwarf2_per_cu_data *next_cu;
16264
16265 next_cu = per_cu->cu->read_in_chain;
16266
16267 if (per_cu->cu == target_cu)
16268 {
16269 free_heap_comp_unit (per_cu->cu);
16270 *last_chain = next_cu;
16271 break;
16272 }
16273 else
16274 last_chain = &per_cu->cu->read_in_chain;
16275
16276 per_cu = next_cu;
16277 }
16278 }
16279
16280 /* Release all extra memory associated with OBJFILE. */
16281
16282 void
16283 dwarf2_free_objfile (struct objfile *objfile)
16284 {
16285 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16286
16287 if (dwarf2_per_objfile == NULL)
16288 return;
16289
16290 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16291 free_cached_comp_units (NULL);
16292
16293 if (dwarf2_per_objfile->quick_file_names_table)
16294 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16295
16296 /* Everything else should be on the objfile obstack. */
16297 }
16298
16299 /* A pair of DIE offset and GDB type pointer. We store these
16300 in a hash table separate from the DIEs, and preserve them
16301 when the DIEs are flushed out of cache. */
16302
16303 struct dwarf2_offset_and_type
16304 {
16305 sect_offset offset;
16306 struct type *type;
16307 };
16308
16309 /* Hash function for a dwarf2_offset_and_type. */
16310
16311 static hashval_t
16312 offset_and_type_hash (const void *item)
16313 {
16314 const struct dwarf2_offset_and_type *ofs = item;
16315
16316 return ofs->offset.sect_off;
16317 }
16318
16319 /* Equality function for a dwarf2_offset_and_type. */
16320
16321 static int
16322 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16323 {
16324 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16325 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16326
16327 return ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off;
16328 }
16329
16330 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16331 table if necessary. For convenience, return TYPE.
16332
16333 The DIEs reading must have careful ordering to:
16334 * Not cause infite loops trying to read in DIEs as a prerequisite for
16335 reading current DIE.
16336 * Not trying to dereference contents of still incompletely read in types
16337 while reading in other DIEs.
16338 * Enable referencing still incompletely read in types just by a pointer to
16339 the type without accessing its fields.
16340
16341 Therefore caller should follow these rules:
16342 * Try to fetch any prerequisite types we may need to build this DIE type
16343 before building the type and calling set_die_type.
16344 * After building type call set_die_type for current DIE as soon as
16345 possible before fetching more types to complete the current type.
16346 * Make the type as complete as possible before fetching more types. */
16347
16348 static struct type *
16349 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16350 {
16351 struct dwarf2_offset_and_type **slot, ofs;
16352 struct objfile *objfile = cu->objfile;
16353 htab_t *type_hash_ptr;
16354
16355 /* For Ada types, make sure that the gnat-specific data is always
16356 initialized (if not already set). There are a few types where
16357 we should not be doing so, because the type-specific area is
16358 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16359 where the type-specific area is used to store the floatformat).
16360 But this is not a problem, because the gnat-specific information
16361 is actually not needed for these types. */
16362 if (need_gnat_info (cu)
16363 && TYPE_CODE (type) != TYPE_CODE_FUNC
16364 && TYPE_CODE (type) != TYPE_CODE_FLT
16365 && !HAVE_GNAT_AUX_INFO (type))
16366 INIT_GNAT_SPECIFIC (type);
16367
16368 if (cu->per_cu->debug_types_section)
16369 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16370 else
16371 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16372
16373 if (*type_hash_ptr == NULL)
16374 {
16375 *type_hash_ptr
16376 = htab_create_alloc_ex (127,
16377 offset_and_type_hash,
16378 offset_and_type_eq,
16379 NULL,
16380 &objfile->objfile_obstack,
16381 hashtab_obstack_allocate,
16382 dummy_obstack_deallocate);
16383 }
16384
16385 ofs.offset = die->offset;
16386 ofs.type = type;
16387 slot = (struct dwarf2_offset_and_type **)
16388 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset.sect_off,
16389 INSERT);
16390 if (*slot)
16391 complaint (&symfile_complaints,
16392 _("A problem internal to GDB: DIE 0x%x has type already set"),
16393 die->offset.sect_off);
16394 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16395 **slot = ofs;
16396 return type;
16397 }
16398
16399 /* Look up the type for the die at OFFSET in the appropriate type_hash
16400 table, or return NULL if the die does not have a saved type. */
16401
16402 static struct type *
16403 get_die_type_at_offset (sect_offset offset,
16404 struct dwarf2_per_cu_data *per_cu)
16405 {
16406 struct dwarf2_offset_and_type *slot, ofs;
16407 htab_t type_hash;
16408
16409 if (per_cu->debug_types_section)
16410 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16411 else
16412 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16413 if (type_hash == NULL)
16414 return NULL;
16415
16416 ofs.offset = offset;
16417 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset.sect_off);
16418 if (slot)
16419 return slot->type;
16420 else
16421 return NULL;
16422 }
16423
16424 /* Look up the type for DIE in the appropriate type_hash table,
16425 or return NULL if DIE does not have a saved type. */
16426
16427 static struct type *
16428 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16429 {
16430 return get_die_type_at_offset (die->offset, cu->per_cu);
16431 }
16432
16433 /* Add a dependence relationship from CU to REF_PER_CU. */
16434
16435 static void
16436 dwarf2_add_dependence (struct dwarf2_cu *cu,
16437 struct dwarf2_per_cu_data *ref_per_cu)
16438 {
16439 void **slot;
16440
16441 if (cu->dependencies == NULL)
16442 cu->dependencies
16443 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16444 NULL, &cu->comp_unit_obstack,
16445 hashtab_obstack_allocate,
16446 dummy_obstack_deallocate);
16447
16448 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16449 if (*slot == NULL)
16450 *slot = ref_per_cu;
16451 }
16452
16453 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16454 Set the mark field in every compilation unit in the
16455 cache that we must keep because we are keeping CU. */
16456
16457 static int
16458 dwarf2_mark_helper (void **slot, void *data)
16459 {
16460 struct dwarf2_per_cu_data *per_cu;
16461
16462 per_cu = (struct dwarf2_per_cu_data *) *slot;
16463
16464 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16465 reading of the chain. As such dependencies remain valid it is not much
16466 useful to track and undo them during QUIT cleanups. */
16467 if (per_cu->cu == NULL)
16468 return 1;
16469
16470 if (per_cu->cu->mark)
16471 return 1;
16472 per_cu->cu->mark = 1;
16473
16474 if (per_cu->cu->dependencies != NULL)
16475 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16476
16477 return 1;
16478 }
16479
16480 /* Set the mark field in CU and in every other compilation unit in the
16481 cache that we must keep because we are keeping CU. */
16482
16483 static void
16484 dwarf2_mark (struct dwarf2_cu *cu)
16485 {
16486 if (cu->mark)
16487 return;
16488 cu->mark = 1;
16489 if (cu->dependencies != NULL)
16490 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16491 }
16492
16493 static void
16494 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16495 {
16496 while (per_cu)
16497 {
16498 per_cu->cu->mark = 0;
16499 per_cu = per_cu->cu->read_in_chain;
16500 }
16501 }
16502
16503 /* Trivial hash function for partial_die_info: the hash value of a DIE
16504 is its offset in .debug_info for this objfile. */
16505
16506 static hashval_t
16507 partial_die_hash (const void *item)
16508 {
16509 const struct partial_die_info *part_die = item;
16510
16511 return part_die->offset.sect_off;
16512 }
16513
16514 /* Trivial comparison function for partial_die_info structures: two DIEs
16515 are equal if they have the same offset. */
16516
16517 static int
16518 partial_die_eq (const void *item_lhs, const void *item_rhs)
16519 {
16520 const struct partial_die_info *part_die_lhs = item_lhs;
16521 const struct partial_die_info *part_die_rhs = item_rhs;
16522
16523 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
16524 }
16525
16526 static struct cmd_list_element *set_dwarf2_cmdlist;
16527 static struct cmd_list_element *show_dwarf2_cmdlist;
16528
16529 static void
16530 set_dwarf2_cmd (char *args, int from_tty)
16531 {
16532 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16533 }
16534
16535 static void
16536 show_dwarf2_cmd (char *args, int from_tty)
16537 {
16538 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16539 }
16540
16541 /* If section described by INFO was mmapped, munmap it now. */
16542
16543 static void
16544 munmap_section_buffer (struct dwarf2_section_info *info)
16545 {
16546 if (info->map_addr != NULL)
16547 {
16548 #ifdef HAVE_MMAP
16549 int res;
16550
16551 res = munmap (info->map_addr, info->map_len);
16552 gdb_assert (res == 0);
16553 #else
16554 /* Without HAVE_MMAP, we should never be here to begin with. */
16555 gdb_assert_not_reached ("no mmap support");
16556 #endif
16557 }
16558 }
16559
16560 /* munmap debug sections for OBJFILE, if necessary. */
16561
16562 static void
16563 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16564 {
16565 struct dwarf2_per_objfile *data = d;
16566 int ix;
16567 struct dwarf2_section_info *section;
16568
16569 /* This is sorted according to the order they're defined in to make it easier
16570 to keep in sync. */
16571 munmap_section_buffer (&data->info);
16572 munmap_section_buffer (&data->abbrev);
16573 munmap_section_buffer (&data->line);
16574 munmap_section_buffer (&data->loc);
16575 munmap_section_buffer (&data->macinfo);
16576 munmap_section_buffer (&data->macro);
16577 munmap_section_buffer (&data->str);
16578 munmap_section_buffer (&data->ranges);
16579 munmap_section_buffer (&data->frame);
16580 munmap_section_buffer (&data->eh_frame);
16581 munmap_section_buffer (&data->gdb_index);
16582
16583 for (ix = 0;
16584 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16585 ++ix)
16586 munmap_section_buffer (section);
16587
16588 VEC_free (dwarf2_section_info_def, data->types);
16589 }
16590
16591 \f
16592 /* The "save gdb-index" command. */
16593
16594 /* The contents of the hash table we create when building the string
16595 table. */
16596 struct strtab_entry
16597 {
16598 offset_type offset;
16599 const char *str;
16600 };
16601
16602 /* Hash function for a strtab_entry.
16603
16604 Function is used only during write_hash_table so no index format backward
16605 compatibility is needed. */
16606
16607 static hashval_t
16608 hash_strtab_entry (const void *e)
16609 {
16610 const struct strtab_entry *entry = e;
16611 return mapped_index_string_hash (INT_MAX, entry->str);
16612 }
16613
16614 /* Equality function for a strtab_entry. */
16615
16616 static int
16617 eq_strtab_entry (const void *a, const void *b)
16618 {
16619 const struct strtab_entry *ea = a;
16620 const struct strtab_entry *eb = b;
16621 return !strcmp (ea->str, eb->str);
16622 }
16623
16624 /* Create a strtab_entry hash table. */
16625
16626 static htab_t
16627 create_strtab (void)
16628 {
16629 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16630 xfree, xcalloc, xfree);
16631 }
16632
16633 /* Add a string to the constant pool. Return the string's offset in
16634 host order. */
16635
16636 static offset_type
16637 add_string (htab_t table, struct obstack *cpool, const char *str)
16638 {
16639 void **slot;
16640 struct strtab_entry entry;
16641 struct strtab_entry *result;
16642
16643 entry.str = str;
16644 slot = htab_find_slot (table, &entry, INSERT);
16645 if (*slot)
16646 result = *slot;
16647 else
16648 {
16649 result = XNEW (struct strtab_entry);
16650 result->offset = obstack_object_size (cpool);
16651 result->str = str;
16652 obstack_grow_str0 (cpool, str);
16653 *slot = result;
16654 }
16655 return result->offset;
16656 }
16657
16658 /* An entry in the symbol table. */
16659 struct symtab_index_entry
16660 {
16661 /* The name of the symbol. */
16662 const char *name;
16663 /* The offset of the name in the constant pool. */
16664 offset_type index_offset;
16665 /* A sorted vector of the indices of all the CUs that hold an object
16666 of this name. */
16667 VEC (offset_type) *cu_indices;
16668 };
16669
16670 /* The symbol table. This is a power-of-2-sized hash table. */
16671 struct mapped_symtab
16672 {
16673 offset_type n_elements;
16674 offset_type size;
16675 struct symtab_index_entry **data;
16676 };
16677
16678 /* Hash function for a symtab_index_entry. */
16679
16680 static hashval_t
16681 hash_symtab_entry (const void *e)
16682 {
16683 const struct symtab_index_entry *entry = e;
16684 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16685 sizeof (offset_type) * VEC_length (offset_type,
16686 entry->cu_indices),
16687 0);
16688 }
16689
16690 /* Equality function for a symtab_index_entry. */
16691
16692 static int
16693 eq_symtab_entry (const void *a, const void *b)
16694 {
16695 const struct symtab_index_entry *ea = a;
16696 const struct symtab_index_entry *eb = b;
16697 int len = VEC_length (offset_type, ea->cu_indices);
16698 if (len != VEC_length (offset_type, eb->cu_indices))
16699 return 0;
16700 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16701 VEC_address (offset_type, eb->cu_indices),
16702 sizeof (offset_type) * len);
16703 }
16704
16705 /* Destroy a symtab_index_entry. */
16706
16707 static void
16708 delete_symtab_entry (void *p)
16709 {
16710 struct symtab_index_entry *entry = p;
16711 VEC_free (offset_type, entry->cu_indices);
16712 xfree (entry);
16713 }
16714
16715 /* Create a hash table holding symtab_index_entry objects. */
16716
16717 static htab_t
16718 create_symbol_hash_table (void)
16719 {
16720 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16721 delete_symtab_entry, xcalloc, xfree);
16722 }
16723
16724 /* Create a new mapped symtab object. */
16725
16726 static struct mapped_symtab *
16727 create_mapped_symtab (void)
16728 {
16729 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16730 symtab->n_elements = 0;
16731 symtab->size = 1024;
16732 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16733 return symtab;
16734 }
16735
16736 /* Destroy a mapped_symtab. */
16737
16738 static void
16739 cleanup_mapped_symtab (void *p)
16740 {
16741 struct mapped_symtab *symtab = p;
16742 /* The contents of the array are freed when the other hash table is
16743 destroyed. */
16744 xfree (symtab->data);
16745 xfree (symtab);
16746 }
16747
16748 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16749 the slot.
16750
16751 Function is used only during write_hash_table so no index format backward
16752 compatibility is needed. */
16753
16754 static struct symtab_index_entry **
16755 find_slot (struct mapped_symtab *symtab, const char *name)
16756 {
16757 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16758
16759 index = hash & (symtab->size - 1);
16760 step = ((hash * 17) & (symtab->size - 1)) | 1;
16761
16762 for (;;)
16763 {
16764 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16765 return &symtab->data[index];
16766 index = (index + step) & (symtab->size - 1);
16767 }
16768 }
16769
16770 /* Expand SYMTAB's hash table. */
16771
16772 static void
16773 hash_expand (struct mapped_symtab *symtab)
16774 {
16775 offset_type old_size = symtab->size;
16776 offset_type i;
16777 struct symtab_index_entry **old_entries = symtab->data;
16778
16779 symtab->size *= 2;
16780 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16781
16782 for (i = 0; i < old_size; ++i)
16783 {
16784 if (old_entries[i])
16785 {
16786 struct symtab_index_entry **slot = find_slot (symtab,
16787 old_entries[i]->name);
16788 *slot = old_entries[i];
16789 }
16790 }
16791
16792 xfree (old_entries);
16793 }
16794
16795 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16796 is the index of the CU in which the symbol appears. */
16797
16798 static void
16799 add_index_entry (struct mapped_symtab *symtab, const char *name,
16800 offset_type cu_index)
16801 {
16802 struct symtab_index_entry **slot;
16803
16804 ++symtab->n_elements;
16805 if (4 * symtab->n_elements / 3 >= symtab->size)
16806 hash_expand (symtab);
16807
16808 slot = find_slot (symtab, name);
16809 if (!*slot)
16810 {
16811 *slot = XNEW (struct symtab_index_entry);
16812 (*slot)->name = name;
16813 (*slot)->cu_indices = NULL;
16814 }
16815 /* Don't push an index twice. Due to how we add entries we only
16816 have to check the last one. */
16817 if (VEC_empty (offset_type, (*slot)->cu_indices)
16818 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16819 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16820 }
16821
16822 /* Add a vector of indices to the constant pool. */
16823
16824 static offset_type
16825 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16826 struct symtab_index_entry *entry)
16827 {
16828 void **slot;
16829
16830 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16831 if (!*slot)
16832 {
16833 offset_type len = VEC_length (offset_type, entry->cu_indices);
16834 offset_type val = MAYBE_SWAP (len);
16835 offset_type iter;
16836 int i;
16837
16838 *slot = entry;
16839 entry->index_offset = obstack_object_size (cpool);
16840
16841 obstack_grow (cpool, &val, sizeof (val));
16842 for (i = 0;
16843 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16844 ++i)
16845 {
16846 val = MAYBE_SWAP (iter);
16847 obstack_grow (cpool, &val, sizeof (val));
16848 }
16849 }
16850 else
16851 {
16852 struct symtab_index_entry *old_entry = *slot;
16853 entry->index_offset = old_entry->index_offset;
16854 entry = old_entry;
16855 }
16856 return entry->index_offset;
16857 }
16858
16859 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16860 constant pool entries going into the obstack CPOOL. */
16861
16862 static void
16863 write_hash_table (struct mapped_symtab *symtab,
16864 struct obstack *output, struct obstack *cpool)
16865 {
16866 offset_type i;
16867 htab_t symbol_hash_table;
16868 htab_t str_table;
16869
16870 symbol_hash_table = create_symbol_hash_table ();
16871 str_table = create_strtab ();
16872
16873 /* We add all the index vectors to the constant pool first, to
16874 ensure alignment is ok. */
16875 for (i = 0; i < symtab->size; ++i)
16876 {
16877 if (symtab->data[i])
16878 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16879 }
16880
16881 /* Now write out the hash table. */
16882 for (i = 0; i < symtab->size; ++i)
16883 {
16884 offset_type str_off, vec_off;
16885
16886 if (symtab->data[i])
16887 {
16888 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16889 vec_off = symtab->data[i]->index_offset;
16890 }
16891 else
16892 {
16893 /* While 0 is a valid constant pool index, it is not valid
16894 to have 0 for both offsets. */
16895 str_off = 0;
16896 vec_off = 0;
16897 }
16898
16899 str_off = MAYBE_SWAP (str_off);
16900 vec_off = MAYBE_SWAP (vec_off);
16901
16902 obstack_grow (output, &str_off, sizeof (str_off));
16903 obstack_grow (output, &vec_off, sizeof (vec_off));
16904 }
16905
16906 htab_delete (str_table);
16907 htab_delete (symbol_hash_table);
16908 }
16909
16910 /* Struct to map psymtab to CU index in the index file. */
16911 struct psymtab_cu_index_map
16912 {
16913 struct partial_symtab *psymtab;
16914 unsigned int cu_index;
16915 };
16916
16917 static hashval_t
16918 hash_psymtab_cu_index (const void *item)
16919 {
16920 const struct psymtab_cu_index_map *map = item;
16921
16922 return htab_hash_pointer (map->psymtab);
16923 }
16924
16925 static int
16926 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16927 {
16928 const struct psymtab_cu_index_map *lhs = item_lhs;
16929 const struct psymtab_cu_index_map *rhs = item_rhs;
16930
16931 return lhs->psymtab == rhs->psymtab;
16932 }
16933
16934 /* Helper struct for building the address table. */
16935 struct addrmap_index_data
16936 {
16937 struct objfile *objfile;
16938 struct obstack *addr_obstack;
16939 htab_t cu_index_htab;
16940
16941 /* Non-zero if the previous_* fields are valid.
16942 We can't write an entry until we see the next entry (since it is only then
16943 that we know the end of the entry). */
16944 int previous_valid;
16945 /* Index of the CU in the table of all CUs in the index file. */
16946 unsigned int previous_cu_index;
16947 /* Start address of the CU. */
16948 CORE_ADDR previous_cu_start;
16949 };
16950
16951 /* Write an address entry to OBSTACK. */
16952
16953 static void
16954 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16955 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16956 {
16957 offset_type cu_index_to_write;
16958 char addr[8];
16959 CORE_ADDR baseaddr;
16960
16961 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16962
16963 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16964 obstack_grow (obstack, addr, 8);
16965 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16966 obstack_grow (obstack, addr, 8);
16967 cu_index_to_write = MAYBE_SWAP (cu_index);
16968 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16969 }
16970
16971 /* Worker function for traversing an addrmap to build the address table. */
16972
16973 static int
16974 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16975 {
16976 struct addrmap_index_data *data = datap;
16977 struct partial_symtab *pst = obj;
16978 offset_type cu_index;
16979 void **slot;
16980
16981 if (data->previous_valid)
16982 add_address_entry (data->objfile, data->addr_obstack,
16983 data->previous_cu_start, start_addr,
16984 data->previous_cu_index);
16985
16986 data->previous_cu_start = start_addr;
16987 if (pst != NULL)
16988 {
16989 struct psymtab_cu_index_map find_map, *map;
16990 find_map.psymtab = pst;
16991 map = htab_find (data->cu_index_htab, &find_map);
16992 gdb_assert (map != NULL);
16993 data->previous_cu_index = map->cu_index;
16994 data->previous_valid = 1;
16995 }
16996 else
16997 data->previous_valid = 0;
16998
16999 return 0;
17000 }
17001
17002 /* Write OBJFILE's address map to OBSTACK.
17003 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17004 in the index file. */
17005
17006 static void
17007 write_address_map (struct objfile *objfile, struct obstack *obstack,
17008 htab_t cu_index_htab)
17009 {
17010 struct addrmap_index_data addrmap_index_data;
17011
17012 /* When writing the address table, we have to cope with the fact that
17013 the addrmap iterator only provides the start of a region; we have to
17014 wait until the next invocation to get the start of the next region. */
17015
17016 addrmap_index_data.objfile = objfile;
17017 addrmap_index_data.addr_obstack = obstack;
17018 addrmap_index_data.cu_index_htab = cu_index_htab;
17019 addrmap_index_data.previous_valid = 0;
17020
17021 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17022 &addrmap_index_data);
17023
17024 /* It's highly unlikely the last entry (end address = 0xff...ff)
17025 is valid, but we should still handle it.
17026 The end address is recorded as the start of the next region, but that
17027 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17028 anyway. */
17029 if (addrmap_index_data.previous_valid)
17030 add_address_entry (objfile, obstack,
17031 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17032 addrmap_index_data.previous_cu_index);
17033 }
17034
17035 /* Add a list of partial symbols to SYMTAB. */
17036
17037 static void
17038 write_psymbols (struct mapped_symtab *symtab,
17039 htab_t psyms_seen,
17040 struct partial_symbol **psymp,
17041 int count,
17042 offset_type cu_index,
17043 int is_static)
17044 {
17045 for (; count-- > 0; ++psymp)
17046 {
17047 void **slot, *lookup;
17048
17049 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17050 error (_("Ada is not currently supported by the index"));
17051
17052 /* We only want to add a given psymbol once. However, we also
17053 want to account for whether it is global or static. So, we
17054 may add it twice, using slightly different values. */
17055 if (is_static)
17056 {
17057 uintptr_t val = 1 | (uintptr_t) *psymp;
17058
17059 lookup = (void *) val;
17060 }
17061 else
17062 lookup = *psymp;
17063
17064 /* Only add a given psymbol once. */
17065 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17066 if (!*slot)
17067 {
17068 *slot = lookup;
17069 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
17070 }
17071 }
17072 }
17073
17074 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17075 exception if there is an error. */
17076
17077 static void
17078 write_obstack (FILE *file, struct obstack *obstack)
17079 {
17080 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17081 file)
17082 != obstack_object_size (obstack))
17083 error (_("couldn't data write to file"));
17084 }
17085
17086 /* Unlink a file if the argument is not NULL. */
17087
17088 static void
17089 unlink_if_set (void *p)
17090 {
17091 char **filename = p;
17092 if (*filename)
17093 unlink (*filename);
17094 }
17095
17096 /* A helper struct used when iterating over debug_types. */
17097 struct signatured_type_index_data
17098 {
17099 struct objfile *objfile;
17100 struct mapped_symtab *symtab;
17101 struct obstack *types_list;
17102 htab_t psyms_seen;
17103 int cu_index;
17104 };
17105
17106 /* A helper function that writes a single signatured_type to an
17107 obstack. */
17108
17109 static int
17110 write_one_signatured_type (void **slot, void *d)
17111 {
17112 struct signatured_type_index_data *info = d;
17113 struct signatured_type *entry = (struct signatured_type *) *slot;
17114 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17115 struct partial_symtab *psymtab = per_cu->v.psymtab;
17116 gdb_byte val[8];
17117
17118 write_psymbols (info->symtab,
17119 info->psyms_seen,
17120 info->objfile->global_psymbols.list
17121 + psymtab->globals_offset,
17122 psymtab->n_global_syms, info->cu_index,
17123 0);
17124 write_psymbols (info->symtab,
17125 info->psyms_seen,
17126 info->objfile->static_psymbols.list
17127 + psymtab->statics_offset,
17128 psymtab->n_static_syms, info->cu_index,
17129 1);
17130
17131 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17132 entry->per_cu.offset.sect_off);
17133 obstack_grow (info->types_list, val, 8);
17134 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset.cu_off);
17135 obstack_grow (info->types_list, val, 8);
17136 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17137 obstack_grow (info->types_list, val, 8);
17138
17139 ++info->cu_index;
17140
17141 return 1;
17142 }
17143
17144 /* Create an index file for OBJFILE in the directory DIR. */
17145
17146 static void
17147 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17148 {
17149 struct cleanup *cleanup;
17150 char *filename, *cleanup_filename;
17151 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17152 struct obstack cu_list, types_cu_list;
17153 int i;
17154 FILE *out_file;
17155 struct mapped_symtab *symtab;
17156 offset_type val, size_of_contents, total_len;
17157 struct stat st;
17158 char buf[8];
17159 htab_t psyms_seen;
17160 htab_t cu_index_htab;
17161 struct psymtab_cu_index_map *psymtab_cu_index_map;
17162
17163 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
17164 return;
17165
17166 if (dwarf2_per_objfile->using_index)
17167 error (_("Cannot use an index to create the index"));
17168
17169 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17170 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17171
17172 if (stat (objfile->name, &st) < 0)
17173 perror_with_name (objfile->name);
17174
17175 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17176 INDEX_SUFFIX, (char *) NULL);
17177 cleanup = make_cleanup (xfree, filename);
17178
17179 out_file = fopen (filename, "wb");
17180 if (!out_file)
17181 error (_("Can't open `%s' for writing"), filename);
17182
17183 cleanup_filename = filename;
17184 make_cleanup (unlink_if_set, &cleanup_filename);
17185
17186 symtab = create_mapped_symtab ();
17187 make_cleanup (cleanup_mapped_symtab, symtab);
17188
17189 obstack_init (&addr_obstack);
17190 make_cleanup_obstack_free (&addr_obstack);
17191
17192 obstack_init (&cu_list);
17193 make_cleanup_obstack_free (&cu_list);
17194
17195 obstack_init (&types_cu_list);
17196 make_cleanup_obstack_free (&types_cu_list);
17197
17198 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17199 NULL, xcalloc, xfree);
17200 make_cleanup_htab_delete (psyms_seen);
17201
17202 /* While we're scanning CU's create a table that maps a psymtab pointer
17203 (which is what addrmap records) to its index (which is what is recorded
17204 in the index file). This will later be needed to write the address
17205 table. */
17206 cu_index_htab = htab_create_alloc (100,
17207 hash_psymtab_cu_index,
17208 eq_psymtab_cu_index,
17209 NULL, xcalloc, xfree);
17210 make_cleanup_htab_delete (cu_index_htab);
17211 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17212 xmalloc (sizeof (struct psymtab_cu_index_map)
17213 * dwarf2_per_objfile->n_comp_units);
17214 make_cleanup (xfree, psymtab_cu_index_map);
17215
17216 /* The CU list is already sorted, so we don't need to do additional
17217 work here. Also, the debug_types entries do not appear in
17218 all_comp_units, but only in their own hash table. */
17219 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17220 {
17221 struct dwarf2_per_cu_data *per_cu
17222 = dwarf2_per_objfile->all_comp_units[i];
17223 struct partial_symtab *psymtab = per_cu->v.psymtab;
17224 gdb_byte val[8];
17225 struct psymtab_cu_index_map *map;
17226 void **slot;
17227
17228 write_psymbols (symtab,
17229 psyms_seen,
17230 objfile->global_psymbols.list + psymtab->globals_offset,
17231 psymtab->n_global_syms, i,
17232 0);
17233 write_psymbols (symtab,
17234 psyms_seen,
17235 objfile->static_psymbols.list + psymtab->statics_offset,
17236 psymtab->n_static_syms, i,
17237 1);
17238
17239 map = &psymtab_cu_index_map[i];
17240 map->psymtab = psymtab;
17241 map->cu_index = i;
17242 slot = htab_find_slot (cu_index_htab, map, INSERT);
17243 gdb_assert (slot != NULL);
17244 gdb_assert (*slot == NULL);
17245 *slot = map;
17246
17247 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17248 per_cu->offset.sect_off);
17249 obstack_grow (&cu_list, val, 8);
17250 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17251 obstack_grow (&cu_list, val, 8);
17252 }
17253
17254 /* Dump the address map. */
17255 write_address_map (objfile, &addr_obstack, cu_index_htab);
17256
17257 /* Write out the .debug_type entries, if any. */
17258 if (dwarf2_per_objfile->signatured_types)
17259 {
17260 struct signatured_type_index_data sig_data;
17261
17262 sig_data.objfile = objfile;
17263 sig_data.symtab = symtab;
17264 sig_data.types_list = &types_cu_list;
17265 sig_data.psyms_seen = psyms_seen;
17266 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17267 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17268 write_one_signatured_type, &sig_data);
17269 }
17270
17271 obstack_init (&constant_pool);
17272 make_cleanup_obstack_free (&constant_pool);
17273 obstack_init (&symtab_obstack);
17274 make_cleanup_obstack_free (&symtab_obstack);
17275 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17276
17277 obstack_init (&contents);
17278 make_cleanup_obstack_free (&contents);
17279 size_of_contents = 6 * sizeof (offset_type);
17280 total_len = size_of_contents;
17281
17282 /* The version number. */
17283 val = MAYBE_SWAP (6);
17284 obstack_grow (&contents, &val, sizeof (val));
17285
17286 /* The offset of the CU list from the start of the file. */
17287 val = MAYBE_SWAP (total_len);
17288 obstack_grow (&contents, &val, sizeof (val));
17289 total_len += obstack_object_size (&cu_list);
17290
17291 /* The offset of the types CU list from the start of the file. */
17292 val = MAYBE_SWAP (total_len);
17293 obstack_grow (&contents, &val, sizeof (val));
17294 total_len += obstack_object_size (&types_cu_list);
17295
17296 /* The offset of the address table from the start of the file. */
17297 val = MAYBE_SWAP (total_len);
17298 obstack_grow (&contents, &val, sizeof (val));
17299 total_len += obstack_object_size (&addr_obstack);
17300
17301 /* The offset of the symbol table from the start of the file. */
17302 val = MAYBE_SWAP (total_len);
17303 obstack_grow (&contents, &val, sizeof (val));
17304 total_len += obstack_object_size (&symtab_obstack);
17305
17306 /* The offset of the constant pool from the start of the file. */
17307 val = MAYBE_SWAP (total_len);
17308 obstack_grow (&contents, &val, sizeof (val));
17309 total_len += obstack_object_size (&constant_pool);
17310
17311 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17312
17313 write_obstack (out_file, &contents);
17314 write_obstack (out_file, &cu_list);
17315 write_obstack (out_file, &types_cu_list);
17316 write_obstack (out_file, &addr_obstack);
17317 write_obstack (out_file, &symtab_obstack);
17318 write_obstack (out_file, &constant_pool);
17319
17320 fclose (out_file);
17321
17322 /* We want to keep the file, so we set cleanup_filename to NULL
17323 here. See unlink_if_set. */
17324 cleanup_filename = NULL;
17325
17326 do_cleanups (cleanup);
17327 }
17328
17329 /* Implementation of the `save gdb-index' command.
17330
17331 Note that the file format used by this command is documented in the
17332 GDB manual. Any changes here must be documented there. */
17333
17334 static void
17335 save_gdb_index_command (char *arg, int from_tty)
17336 {
17337 struct objfile *objfile;
17338
17339 if (!arg || !*arg)
17340 error (_("usage: save gdb-index DIRECTORY"));
17341
17342 ALL_OBJFILES (objfile)
17343 {
17344 struct stat st;
17345
17346 /* If the objfile does not correspond to an actual file, skip it. */
17347 if (stat (objfile->name, &st) < 0)
17348 continue;
17349
17350 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17351 if (dwarf2_per_objfile)
17352 {
17353 volatile struct gdb_exception except;
17354
17355 TRY_CATCH (except, RETURN_MASK_ERROR)
17356 {
17357 write_psymtabs_to_index (objfile, arg);
17358 }
17359 if (except.reason < 0)
17360 exception_fprintf (gdb_stderr, except,
17361 _("Error while writing index for `%s': "),
17362 objfile->name);
17363 }
17364 }
17365 }
17366
17367 \f
17368
17369 int dwarf2_always_disassemble;
17370
17371 static void
17372 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17373 struct cmd_list_element *c, const char *value)
17374 {
17375 fprintf_filtered (file,
17376 _("Whether to always disassemble "
17377 "DWARF expressions is %s.\n"),
17378 value);
17379 }
17380
17381 static void
17382 show_check_physname (struct ui_file *file, int from_tty,
17383 struct cmd_list_element *c, const char *value)
17384 {
17385 fprintf_filtered (file,
17386 _("Whether to check \"physname\" is %s.\n"),
17387 value);
17388 }
17389
17390 void _initialize_dwarf2_read (void);
17391
17392 void
17393 _initialize_dwarf2_read (void)
17394 {
17395 struct cmd_list_element *c;
17396
17397 dwarf2_objfile_data_key
17398 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17399
17400 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17401 Set DWARF 2 specific variables.\n\
17402 Configure DWARF 2 variables such as the cache size"),
17403 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17404 0/*allow-unknown*/, &maintenance_set_cmdlist);
17405
17406 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17407 Show DWARF 2 specific variables\n\
17408 Show DWARF 2 variables such as the cache size"),
17409 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17410 0/*allow-unknown*/, &maintenance_show_cmdlist);
17411
17412 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17413 &dwarf2_max_cache_age, _("\
17414 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17415 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17416 A higher limit means that cached compilation units will be stored\n\
17417 in memory longer, and more total memory will be used. Zero disables\n\
17418 caching, which can slow down startup."),
17419 NULL,
17420 show_dwarf2_max_cache_age,
17421 &set_dwarf2_cmdlist,
17422 &show_dwarf2_cmdlist);
17423
17424 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17425 &dwarf2_always_disassemble, _("\
17426 Set whether `info address' always disassembles DWARF expressions."), _("\
17427 Show whether `info address' always disassembles DWARF expressions."), _("\
17428 When enabled, DWARF expressions are always printed in an assembly-like\n\
17429 syntax. When disabled, expressions will be printed in a more\n\
17430 conversational style, when possible."),
17431 NULL,
17432 show_dwarf2_always_disassemble,
17433 &set_dwarf2_cmdlist,
17434 &show_dwarf2_cmdlist);
17435
17436 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17437 Set debugging of the dwarf2 DIE reader."), _("\
17438 Show debugging of the dwarf2 DIE reader."), _("\
17439 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17440 The value is the maximum depth to print."),
17441 NULL,
17442 NULL,
17443 &setdebuglist, &showdebuglist);
17444
17445 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17446 Set cross-checking of \"physname\" code against demangler."), _("\
17447 Show cross-checking of \"physname\" code against demangler."), _("\
17448 When enabled, GDB's internal \"physname\" code is checked against\n\
17449 the demangler."),
17450 NULL, show_check_physname,
17451 &setdebuglist, &showdebuglist);
17452
17453 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17454 _("\
17455 Save a gdb-index file.\n\
17456 Usage: save gdb-index DIRECTORY"),
17457 &save_cmdlist);
17458 set_cmd_completer (c, filename_completer);
17459 }