7ae3b8616cd5a079fd6429acd4ae91d9b8e9c78a
[deliverable/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 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2196 return 0;
2197
2198 if (types_list_elements)
2199 {
2200 struct dwarf2_section_info *section;
2201
2202 /* We can only handle a single .debug_types when we have an
2203 index. */
2204 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2205 return 0;
2206
2207 section = VEC_index (dwarf2_section_info_def,
2208 dwarf2_per_objfile->types, 0);
2209
2210 if (!create_signatured_type_table_from_index (objfile, section,
2211 types_list,
2212 types_list_elements))
2213 return 0;
2214 }
2215
2216 create_addrmap_from_index (objfile, map);
2217
2218 dwarf2_per_objfile->index_table = map;
2219 dwarf2_per_objfile->using_index = 1;
2220 dwarf2_per_objfile->quick_file_names_table =
2221 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2222
2223 return 1;
2224 }
2225
2226 /* A helper for the "quick" functions which sets the global
2227 dwarf2_per_objfile according to OBJFILE. */
2228
2229 static void
2230 dw2_setup (struct objfile *objfile)
2231 {
2232 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2233 gdb_assert (dwarf2_per_objfile);
2234 }
2235
2236 /* A helper for the "quick" functions which attempts to read the line
2237 table for THIS_CU. */
2238
2239 static struct quick_file_names *
2240 dw2_get_file_names (struct objfile *objfile,
2241 struct dwarf2_per_cu_data *this_cu)
2242 {
2243 bfd *abfd = objfile->obfd;
2244 struct line_header *lh;
2245 struct attribute *attr;
2246 struct cleanup *cleanups;
2247 struct die_info *comp_unit_die;
2248 struct dwarf2_section_info* sec;
2249 gdb_byte *info_ptr;
2250 int has_children, i;
2251 struct dwarf2_cu cu;
2252 unsigned int bytes_read;
2253 struct die_reader_specs reader_specs;
2254 char *name, *comp_dir;
2255 void **slot;
2256 struct quick_file_names *qfn;
2257 unsigned int line_offset;
2258
2259 if (this_cu->v.quick->file_names != NULL)
2260 return this_cu->v.quick->file_names;
2261 /* If we know there is no line data, no point in looking again. */
2262 if (this_cu->v.quick->no_file_data)
2263 return NULL;
2264
2265 init_one_comp_unit (&cu, this_cu);
2266 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2267
2268 if (this_cu->debug_types_section)
2269 sec = this_cu->debug_types_section;
2270 else
2271 sec = &dwarf2_per_objfile->info;
2272 dwarf2_read_section (objfile, sec);
2273 info_ptr = sec->buffer + this_cu->offset.sect_off;
2274
2275 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2276 this_cu->debug_types_section != NULL);
2277
2278 /* Skip dummy compilation units. */
2279 if (info_ptr >= (sec->buffer + sec->size)
2280 || peek_abbrev_code (abfd, info_ptr) == 0)
2281 {
2282 do_cleanups (cleanups);
2283 return NULL;
2284 }
2285
2286 dwarf2_read_abbrevs (&cu);
2287 make_cleanup (dwarf2_free_abbrev_table, &cu);
2288
2289 init_cu_die_reader (&reader_specs, &cu);
2290 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2291 &has_children);
2292
2293 lh = NULL;
2294 slot = NULL;
2295 line_offset = 0;
2296 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2297 if (attr)
2298 {
2299 struct quick_file_names find_entry;
2300
2301 line_offset = DW_UNSND (attr);
2302
2303 /* We may have already read in this line header (TU line header sharing).
2304 If we have we're done. */
2305 find_entry.offset = line_offset;
2306 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2307 &find_entry, INSERT);
2308 if (*slot != NULL)
2309 {
2310 do_cleanups (cleanups);
2311 this_cu->v.quick->file_names = *slot;
2312 return *slot;
2313 }
2314
2315 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2316 }
2317 if (lh == NULL)
2318 {
2319 do_cleanups (cleanups);
2320 this_cu->v.quick->no_file_data = 1;
2321 return NULL;
2322 }
2323
2324 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2325 qfn->offset = line_offset;
2326 gdb_assert (slot != NULL);
2327 *slot = qfn;
2328
2329 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2330
2331 qfn->num_file_names = lh->num_file_names;
2332 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2333 lh->num_file_names * sizeof (char *));
2334 for (i = 0; i < lh->num_file_names; ++i)
2335 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2336 qfn->real_names = NULL;
2337
2338 free_line_header (lh);
2339 do_cleanups (cleanups);
2340
2341 this_cu->v.quick->file_names = qfn;
2342 return qfn;
2343 }
2344
2345 /* A helper for the "quick" functions which computes and caches the
2346 real path for a given file name from the line table. */
2347
2348 static const char *
2349 dw2_get_real_path (struct objfile *objfile,
2350 struct quick_file_names *qfn, int index)
2351 {
2352 if (qfn->real_names == NULL)
2353 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2354 qfn->num_file_names, sizeof (char *));
2355
2356 if (qfn->real_names[index] == NULL)
2357 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2358
2359 return qfn->real_names[index];
2360 }
2361
2362 static struct symtab *
2363 dw2_find_last_source_symtab (struct objfile *objfile)
2364 {
2365 int index;
2366
2367 dw2_setup (objfile);
2368 index = dwarf2_per_objfile->n_comp_units - 1;
2369 return dw2_instantiate_symtab (dw2_get_cu (index));
2370 }
2371
2372 /* Traversal function for dw2_forget_cached_source_info. */
2373
2374 static int
2375 dw2_free_cached_file_names (void **slot, void *info)
2376 {
2377 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2378
2379 if (file_data->real_names)
2380 {
2381 int i;
2382
2383 for (i = 0; i < file_data->num_file_names; ++i)
2384 {
2385 xfree ((void*) file_data->real_names[i]);
2386 file_data->real_names[i] = NULL;
2387 }
2388 }
2389
2390 return 1;
2391 }
2392
2393 static void
2394 dw2_forget_cached_source_info (struct objfile *objfile)
2395 {
2396 dw2_setup (objfile);
2397
2398 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2399 dw2_free_cached_file_names, NULL);
2400 }
2401
2402 /* Helper function for dw2_map_symtabs_matching_filename that expands
2403 the symtabs and calls the iterator. */
2404
2405 static int
2406 dw2_map_expand_apply (struct objfile *objfile,
2407 struct dwarf2_per_cu_data *per_cu,
2408 const char *name,
2409 const char *full_path, const char *real_path,
2410 int (*callback) (struct symtab *, void *),
2411 void *data)
2412 {
2413 struct symtab *last_made = objfile->symtabs;
2414
2415 /* Don't visit already-expanded CUs. */
2416 if (per_cu->v.quick->symtab)
2417 return 0;
2418
2419 /* This may expand more than one symtab, and we want to iterate over
2420 all of them. */
2421 dw2_instantiate_symtab (per_cu);
2422
2423 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2424 objfile->symtabs, last_made);
2425 }
2426
2427 /* Implementation of the map_symtabs_matching_filename method. */
2428
2429 static int
2430 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2431 const char *full_path, const char *real_path,
2432 int (*callback) (struct symtab *, void *),
2433 void *data)
2434 {
2435 int i;
2436 const char *name_basename = lbasename (name);
2437 int name_len = strlen (name);
2438 int is_abs = IS_ABSOLUTE_PATH (name);
2439
2440 dw2_setup (objfile);
2441
2442 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2443 + dwarf2_per_objfile->n_type_units); ++i)
2444 {
2445 int j;
2446 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2447 struct quick_file_names *file_data;
2448
2449 /* We only need to look at symtabs not already expanded. */
2450 if (per_cu->v.quick->symtab)
2451 continue;
2452
2453 file_data = dw2_get_file_names (objfile, per_cu);
2454 if (file_data == NULL)
2455 continue;
2456
2457 for (j = 0; j < file_data->num_file_names; ++j)
2458 {
2459 const char *this_name = file_data->file_names[j];
2460
2461 if (FILENAME_CMP (name, this_name) == 0
2462 || (!is_abs && compare_filenames_for_search (this_name,
2463 name, name_len)))
2464 {
2465 if (dw2_map_expand_apply (objfile, per_cu,
2466 name, full_path, real_path,
2467 callback, data))
2468 return 1;
2469 }
2470
2471 /* Before we invoke realpath, which can get expensive when many
2472 files are involved, do a quick comparison of the basenames. */
2473 if (! basenames_may_differ
2474 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2475 continue;
2476
2477 if (full_path != NULL)
2478 {
2479 const char *this_real_name = dw2_get_real_path (objfile,
2480 file_data, j);
2481
2482 if (this_real_name != NULL
2483 && (FILENAME_CMP (full_path, this_real_name) == 0
2484 || (!is_abs
2485 && compare_filenames_for_search (this_real_name,
2486 name, name_len))))
2487 {
2488 if (dw2_map_expand_apply (objfile, per_cu,
2489 name, full_path, real_path,
2490 callback, data))
2491 return 1;
2492 }
2493 }
2494
2495 if (real_path != NULL)
2496 {
2497 const char *this_real_name = dw2_get_real_path (objfile,
2498 file_data, j);
2499
2500 if (this_real_name != NULL
2501 && (FILENAME_CMP (real_path, this_real_name) == 0
2502 || (!is_abs
2503 && compare_filenames_for_search (this_real_name,
2504 name, name_len))))
2505 {
2506 if (dw2_map_expand_apply (objfile, per_cu,
2507 name, full_path, real_path,
2508 callback, data))
2509 return 1;
2510 }
2511 }
2512 }
2513 }
2514
2515 return 0;
2516 }
2517
2518 static struct symtab *
2519 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2520 const char *name, domain_enum domain)
2521 {
2522 /* We do all the work in the pre_expand_symtabs_matching hook
2523 instead. */
2524 return NULL;
2525 }
2526
2527 /* A helper function that expands all symtabs that hold an object
2528 named NAME. */
2529
2530 static void
2531 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2532 {
2533 dw2_setup (objfile);
2534
2535 /* index_table is NULL if OBJF_READNOW. */
2536 if (dwarf2_per_objfile->index_table)
2537 {
2538 offset_type *vec;
2539
2540 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2541 name, &vec))
2542 {
2543 offset_type i, len = MAYBE_SWAP (*vec);
2544 for (i = 0; i < len; ++i)
2545 {
2546 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2547 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2548
2549 dw2_instantiate_symtab (per_cu);
2550 }
2551 }
2552 }
2553 }
2554
2555 static void
2556 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2557 enum block_enum block_kind, const char *name,
2558 domain_enum domain)
2559 {
2560 dw2_do_expand_symtabs_matching (objfile, name);
2561 }
2562
2563 static void
2564 dw2_print_stats (struct objfile *objfile)
2565 {
2566 int i, count;
2567
2568 dw2_setup (objfile);
2569 count = 0;
2570 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2571 + dwarf2_per_objfile->n_type_units); ++i)
2572 {
2573 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2574
2575 if (!per_cu->v.quick->symtab)
2576 ++count;
2577 }
2578 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2579 }
2580
2581 static void
2582 dw2_dump (struct objfile *objfile)
2583 {
2584 /* Nothing worth printing. */
2585 }
2586
2587 static void
2588 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2589 struct section_offsets *delta)
2590 {
2591 /* There's nothing to relocate here. */
2592 }
2593
2594 static void
2595 dw2_expand_symtabs_for_function (struct objfile *objfile,
2596 const char *func_name)
2597 {
2598 dw2_do_expand_symtabs_matching (objfile, func_name);
2599 }
2600
2601 static void
2602 dw2_expand_all_symtabs (struct objfile *objfile)
2603 {
2604 int i;
2605
2606 dw2_setup (objfile);
2607
2608 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2609 + dwarf2_per_objfile->n_type_units); ++i)
2610 {
2611 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2612
2613 dw2_instantiate_symtab (per_cu);
2614 }
2615 }
2616
2617 static void
2618 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2619 const char *filename)
2620 {
2621 int i;
2622
2623 dw2_setup (objfile);
2624
2625 /* We don't need to consider type units here.
2626 This is only called for examining code, e.g. expand_line_sal.
2627 There can be an order of magnitude (or more) more type units
2628 than comp units, and we avoid them if we can. */
2629
2630 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2631 {
2632 int j;
2633 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2634 struct quick_file_names *file_data;
2635
2636 /* We only need to look at symtabs not already expanded. */
2637 if (per_cu->v.quick->symtab)
2638 continue;
2639
2640 file_data = dw2_get_file_names (objfile, per_cu);
2641 if (file_data == NULL)
2642 continue;
2643
2644 for (j = 0; j < file_data->num_file_names; ++j)
2645 {
2646 const char *this_name = file_data->file_names[j];
2647 if (FILENAME_CMP (this_name, filename) == 0)
2648 {
2649 dw2_instantiate_symtab (per_cu);
2650 break;
2651 }
2652 }
2653 }
2654 }
2655
2656 static const char *
2657 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2658 {
2659 struct dwarf2_per_cu_data *per_cu;
2660 offset_type *vec;
2661 struct quick_file_names *file_data;
2662
2663 dw2_setup (objfile);
2664
2665 /* index_table is NULL if OBJF_READNOW. */
2666 if (!dwarf2_per_objfile->index_table)
2667 {
2668 struct symtab *s;
2669
2670 ALL_OBJFILE_SYMTABS (objfile, s)
2671 if (s->primary)
2672 {
2673 struct blockvector *bv = BLOCKVECTOR (s);
2674 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2675 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2676
2677 if (sym)
2678 return sym->symtab->filename;
2679 }
2680 return NULL;
2681 }
2682
2683 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2684 name, &vec))
2685 return NULL;
2686
2687 /* Note that this just looks at the very first one named NAME -- but
2688 actually we are looking for a function. find_main_filename
2689 should be rewritten so that it doesn't require a custom hook. It
2690 could just use the ordinary symbol tables. */
2691 /* vec[0] is the length, which must always be >0. */
2692 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2693
2694 file_data = dw2_get_file_names (objfile, per_cu);
2695 if (file_data == NULL)
2696 return NULL;
2697
2698 return file_data->file_names[file_data->num_file_names - 1];
2699 }
2700
2701 static void
2702 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2703 struct objfile *objfile, int global,
2704 int (*callback) (struct block *,
2705 struct symbol *, void *),
2706 void *data, symbol_compare_ftype *match,
2707 symbol_compare_ftype *ordered_compare)
2708 {
2709 /* Currently unimplemented; used for Ada. The function can be called if the
2710 current language is Ada for a non-Ada objfile using GNU index. As Ada
2711 does not look for non-Ada symbols this function should just return. */
2712 }
2713
2714 static void
2715 dw2_expand_symtabs_matching
2716 (struct objfile *objfile,
2717 int (*file_matcher) (const char *, void *),
2718 int (*name_matcher) (const char *, void *),
2719 enum search_domain kind,
2720 void *data)
2721 {
2722 int i;
2723 offset_type iter;
2724 struct mapped_index *index;
2725
2726 dw2_setup (objfile);
2727
2728 /* index_table is NULL if OBJF_READNOW. */
2729 if (!dwarf2_per_objfile->index_table)
2730 return;
2731 index = dwarf2_per_objfile->index_table;
2732
2733 if (file_matcher != NULL)
2734 {
2735 struct cleanup *cleanup;
2736 htab_t visited_found, visited_not_found;
2737
2738 visited_found = htab_create_alloc (10,
2739 htab_hash_pointer, htab_eq_pointer,
2740 NULL, xcalloc, xfree);
2741 cleanup = make_cleanup_htab_delete (visited_found);
2742 visited_not_found = htab_create_alloc (10,
2743 htab_hash_pointer, htab_eq_pointer,
2744 NULL, xcalloc, xfree);
2745 make_cleanup_htab_delete (visited_not_found);
2746
2747 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2748 + dwarf2_per_objfile->n_type_units); ++i)
2749 {
2750 int j;
2751 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2752 struct quick_file_names *file_data;
2753 void **slot;
2754
2755 per_cu->v.quick->mark = 0;
2756
2757 /* We only need to look at symtabs not already expanded. */
2758 if (per_cu->v.quick->symtab)
2759 continue;
2760
2761 file_data = dw2_get_file_names (objfile, per_cu);
2762 if (file_data == NULL)
2763 continue;
2764
2765 if (htab_find (visited_not_found, file_data) != NULL)
2766 continue;
2767 else if (htab_find (visited_found, file_data) != NULL)
2768 {
2769 per_cu->v.quick->mark = 1;
2770 continue;
2771 }
2772
2773 for (j = 0; j < file_data->num_file_names; ++j)
2774 {
2775 if (file_matcher (file_data->file_names[j], data))
2776 {
2777 per_cu->v.quick->mark = 1;
2778 break;
2779 }
2780 }
2781
2782 slot = htab_find_slot (per_cu->v.quick->mark
2783 ? visited_found
2784 : visited_not_found,
2785 file_data, INSERT);
2786 *slot = file_data;
2787 }
2788
2789 do_cleanups (cleanup);
2790 }
2791
2792 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2793 {
2794 offset_type idx = 2 * iter;
2795 const char *name;
2796 offset_type *vec, vec_len, vec_idx;
2797
2798 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2799 continue;
2800
2801 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2802
2803 if (! (*name_matcher) (name, data))
2804 continue;
2805
2806 /* The name was matched, now expand corresponding CUs that were
2807 marked. */
2808 vec = (offset_type *) (index->constant_pool
2809 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2810 vec_len = MAYBE_SWAP (vec[0]);
2811 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2812 {
2813 struct dwarf2_per_cu_data *per_cu;
2814
2815 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2816 if (file_matcher == NULL || per_cu->v.quick->mark)
2817 dw2_instantiate_symtab (per_cu);
2818 }
2819 }
2820 }
2821
2822 static struct symtab *
2823 dw2_find_pc_sect_symtab (struct objfile *objfile,
2824 struct minimal_symbol *msymbol,
2825 CORE_ADDR pc,
2826 struct obj_section *section,
2827 int warn_if_readin)
2828 {
2829 struct dwarf2_per_cu_data *data;
2830
2831 dw2_setup (objfile);
2832
2833 if (!objfile->psymtabs_addrmap)
2834 return NULL;
2835
2836 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2837 if (!data)
2838 return NULL;
2839
2840 if (warn_if_readin && data->v.quick->symtab)
2841 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2842 paddress (get_objfile_arch (objfile), pc));
2843
2844 return dw2_instantiate_symtab (data);
2845 }
2846
2847 static void
2848 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2849 void *data, int need_fullname)
2850 {
2851 int i;
2852 struct cleanup *cleanup;
2853 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2854 NULL, xcalloc, xfree);
2855
2856 cleanup = make_cleanup_htab_delete (visited);
2857 dw2_setup (objfile);
2858
2859 /* We can ignore file names coming from already-expanded CUs. */
2860 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2861 + dwarf2_per_objfile->n_type_units); ++i)
2862 {
2863 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2864
2865 if (per_cu->v.quick->symtab)
2866 {
2867 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2868 INSERT);
2869
2870 *slot = per_cu->v.quick->file_names;
2871 }
2872 }
2873
2874 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2875 + dwarf2_per_objfile->n_type_units); ++i)
2876 {
2877 int j;
2878 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2879 struct quick_file_names *file_data;
2880 void **slot;
2881
2882 /* We only need to look at symtabs not already expanded. */
2883 if (per_cu->v.quick->symtab)
2884 continue;
2885
2886 file_data = dw2_get_file_names (objfile, per_cu);
2887 if (file_data == NULL)
2888 continue;
2889
2890 slot = htab_find_slot (visited, file_data, INSERT);
2891 if (*slot)
2892 {
2893 /* Already visited. */
2894 continue;
2895 }
2896 *slot = file_data;
2897
2898 for (j = 0; j < file_data->num_file_names; ++j)
2899 {
2900 const char *this_real_name;
2901
2902 if (need_fullname)
2903 this_real_name = dw2_get_real_path (objfile, file_data, j);
2904 else
2905 this_real_name = NULL;
2906 (*fun) (file_data->file_names[j], this_real_name, data);
2907 }
2908 }
2909
2910 do_cleanups (cleanup);
2911 }
2912
2913 static int
2914 dw2_has_symbols (struct objfile *objfile)
2915 {
2916 return 1;
2917 }
2918
2919 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2920 {
2921 dw2_has_symbols,
2922 dw2_find_last_source_symtab,
2923 dw2_forget_cached_source_info,
2924 dw2_map_symtabs_matching_filename,
2925 dw2_lookup_symbol,
2926 dw2_pre_expand_symtabs_matching,
2927 dw2_print_stats,
2928 dw2_dump,
2929 dw2_relocate,
2930 dw2_expand_symtabs_for_function,
2931 dw2_expand_all_symtabs,
2932 dw2_expand_symtabs_with_filename,
2933 dw2_find_symbol_file,
2934 dw2_map_matching_symbols,
2935 dw2_expand_symtabs_matching,
2936 dw2_find_pc_sect_symtab,
2937 dw2_map_symbol_filenames
2938 };
2939
2940 /* Initialize for reading DWARF for this objfile. Return 0 if this
2941 file will use psymtabs, or 1 if using the GNU index. */
2942
2943 int
2944 dwarf2_initialize_objfile (struct objfile *objfile)
2945 {
2946 /* If we're about to read full symbols, don't bother with the
2947 indices. In this case we also don't care if some other debug
2948 format is making psymtabs, because they are all about to be
2949 expanded anyway. */
2950 if ((objfile->flags & OBJF_READNOW))
2951 {
2952 int i;
2953
2954 dwarf2_per_objfile->using_index = 1;
2955 create_all_comp_units (objfile);
2956 create_all_type_units (objfile);
2957 dwarf2_per_objfile->quick_file_names_table =
2958 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2959
2960 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2961 + dwarf2_per_objfile->n_type_units); ++i)
2962 {
2963 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2964
2965 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2966 struct dwarf2_per_cu_quick_data);
2967 }
2968
2969 /* Return 1 so that gdb sees the "quick" functions. However,
2970 these functions will be no-ops because we will have expanded
2971 all symtabs. */
2972 return 1;
2973 }
2974
2975 if (dwarf2_read_index (objfile))
2976 return 1;
2977
2978 return 0;
2979 }
2980
2981 \f
2982
2983 /* Build a partial symbol table. */
2984
2985 void
2986 dwarf2_build_psymtabs (struct objfile *objfile)
2987 {
2988 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2989 {
2990 init_psymbol_list (objfile, 1024);
2991 }
2992
2993 dwarf2_build_psymtabs_hard (objfile);
2994 }
2995
2996 /* Return TRUE if OFFSET is within CU_HEADER. */
2997
2998 static inline int
2999 offset_in_cu_p (const struct comp_unit_head *cu_header, sect_offset offset)
3000 {
3001 sect_offset bottom = { cu_header->offset.sect_off };
3002 sect_offset top = { (cu_header->offset.sect_off + cu_header->length
3003 + cu_header->initial_length_size) };
3004
3005 return (offset.sect_off >= bottom.sect_off && offset.sect_off < top.sect_off);
3006 }
3007
3008 /* Read in the comp unit header information from the debug_info at info_ptr.
3009 NOTE: This leaves members offset, first_die_offset to be filled in
3010 by the caller. */
3011
3012 static gdb_byte *
3013 read_comp_unit_head (struct comp_unit_head *cu_header,
3014 gdb_byte *info_ptr, bfd *abfd)
3015 {
3016 int signed_addr;
3017 unsigned int bytes_read;
3018
3019 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3020 cu_header->initial_length_size = bytes_read;
3021 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3022 info_ptr += bytes_read;
3023 cu_header->version = read_2_bytes (abfd, info_ptr);
3024 info_ptr += 2;
3025 cu_header->abbrev_offset.sect_off = read_offset (abfd, info_ptr, cu_header,
3026 &bytes_read);
3027 info_ptr += bytes_read;
3028 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3029 info_ptr += 1;
3030 signed_addr = bfd_get_sign_extend_vma (abfd);
3031 if (signed_addr < 0)
3032 internal_error (__FILE__, __LINE__,
3033 _("read_comp_unit_head: dwarf from non elf file"));
3034 cu_header->signed_addr_p = signed_addr;
3035
3036 return info_ptr;
3037 }
3038
3039 /* Subroutine of read_and_check_comp_unit_head and
3040 read_and_check_type_unit_head to simplify them.
3041 Perform various error checking on the header. */
3042
3043 static void
3044 error_check_comp_unit_head (struct comp_unit_head *header,
3045 struct dwarf2_section_info *section)
3046 {
3047 bfd *abfd = section->asection->owner;
3048 const char *filename = bfd_get_filename (abfd);
3049
3050 if (header->version != 2 && header->version != 3 && header->version != 4)
3051 error (_("Dwarf Error: wrong version in compilation unit header "
3052 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3053 filename);
3054
3055 if (header->abbrev_offset.sect_off
3056 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3057 &dwarf2_per_objfile->abbrev))
3058 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3059 "(offset 0x%lx + 6) [in module %s]"),
3060 (long) header->abbrev_offset.sect_off, (long) header->offset.sect_off,
3061 filename);
3062
3063 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3064 avoid potential 32-bit overflow. */
3065 if (((unsigned long) header->offset.sect_off
3066 + header->length + header->initial_length_size)
3067 > section->size)
3068 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3069 "(offset 0x%lx + 0) [in module %s]"),
3070 (long) header->length, (long) header->offset.sect_off,
3071 filename);
3072 }
3073
3074 /* Read in a CU/TU header and perform some basic error checking.
3075 The contents of the header are stored in HEADER.
3076 The result is a pointer to the start of the first DIE. */
3077
3078 static gdb_byte *
3079 read_and_check_comp_unit_head (struct comp_unit_head *header,
3080 struct dwarf2_section_info *section,
3081 gdb_byte *info_ptr,
3082 int is_debug_types_section)
3083 {
3084 gdb_byte *beg_of_comp_unit = info_ptr;
3085 bfd *abfd = section->asection->owner;
3086
3087 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3088
3089 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3090
3091 /* If we're reading a type unit, skip over the signature and
3092 type_offset fields. */
3093 if (is_debug_types_section)
3094 info_ptr += 8 /*signature*/ + header->offset_size;
3095
3096 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3097
3098 error_check_comp_unit_head (header, section);
3099
3100 return info_ptr;
3101 }
3102
3103 /* Read in the types comp unit header information from .debug_types entry at
3104 types_ptr. The result is a pointer to one past the end of the header. */
3105
3106 static gdb_byte *
3107 read_and_check_type_unit_head (struct comp_unit_head *header,
3108 struct dwarf2_section_info *section,
3109 gdb_byte *info_ptr,
3110 ULONGEST *signature, cu_offset *type_offset)
3111 {
3112 gdb_byte *beg_of_comp_unit = info_ptr;
3113 bfd *abfd = section->asection->owner;
3114
3115 header->offset.sect_off = beg_of_comp_unit - section->buffer;
3116
3117 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3118
3119 /* If we're reading a type unit, skip over the signature and
3120 type_offset fields. */
3121 if (signature != NULL)
3122 *signature = read_8_bytes (abfd, info_ptr);
3123 info_ptr += 8;
3124 if (type_offset != NULL)
3125 type_offset->cu_off = read_offset_1 (abfd, info_ptr, header->offset_size);
3126 info_ptr += header->offset_size;
3127
3128 header->first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
3129
3130 error_check_comp_unit_head (header, section);
3131
3132 return info_ptr;
3133 }
3134
3135 /* Allocate a new partial symtab for file named NAME and mark this new
3136 partial symtab as being an include of PST. */
3137
3138 static void
3139 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3140 struct objfile *objfile)
3141 {
3142 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3143
3144 subpst->section_offsets = pst->section_offsets;
3145 subpst->textlow = 0;
3146 subpst->texthigh = 0;
3147
3148 subpst->dependencies = (struct partial_symtab **)
3149 obstack_alloc (&objfile->objfile_obstack,
3150 sizeof (struct partial_symtab *));
3151 subpst->dependencies[0] = pst;
3152 subpst->number_of_dependencies = 1;
3153
3154 subpst->globals_offset = 0;
3155 subpst->n_global_syms = 0;
3156 subpst->statics_offset = 0;
3157 subpst->n_static_syms = 0;
3158 subpst->symtab = NULL;
3159 subpst->read_symtab = pst->read_symtab;
3160 subpst->readin = 0;
3161
3162 /* No private part is necessary for include psymtabs. This property
3163 can be used to differentiate between such include psymtabs and
3164 the regular ones. */
3165 subpst->read_symtab_private = NULL;
3166 }
3167
3168 /* Read the Line Number Program data and extract the list of files
3169 included by the source file represented by PST. Build an include
3170 partial symtab for each of these included files. */
3171
3172 static void
3173 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3174 struct die_info *die,
3175 struct partial_symtab *pst)
3176 {
3177 struct objfile *objfile = cu->objfile;
3178 bfd *abfd = objfile->obfd;
3179 struct line_header *lh = NULL;
3180 struct attribute *attr;
3181
3182 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3183 if (attr)
3184 {
3185 unsigned int line_offset = DW_UNSND (attr);
3186
3187 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3188 }
3189 if (lh == NULL)
3190 return; /* No linetable, so no includes. */
3191
3192 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3193 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3194
3195 free_line_header (lh);
3196 }
3197
3198 static hashval_t
3199 hash_signatured_type (const void *item)
3200 {
3201 const struct signatured_type *sig_type = item;
3202
3203 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3204 return sig_type->signature;
3205 }
3206
3207 static int
3208 eq_signatured_type (const void *item_lhs, const void *item_rhs)
3209 {
3210 const struct signatured_type *lhs = item_lhs;
3211 const struct signatured_type *rhs = item_rhs;
3212
3213 return lhs->signature == rhs->signature;
3214 }
3215
3216 /* Allocate a hash table for signatured types. */
3217
3218 static htab_t
3219 allocate_signatured_type_table (struct objfile *objfile)
3220 {
3221 return htab_create_alloc_ex (41,
3222 hash_signatured_type,
3223 eq_signatured_type,
3224 NULL,
3225 &objfile->objfile_obstack,
3226 hashtab_obstack_allocate,
3227 dummy_obstack_deallocate);
3228 }
3229
3230 /* A helper function to add a signatured type CU to a table. */
3231
3232 static int
3233 add_signatured_type_cu_to_table (void **slot, void *datum)
3234 {
3235 struct signatured_type *sigt = *slot;
3236 struct dwarf2_per_cu_data ***datap = datum;
3237
3238 **datap = &sigt->per_cu;
3239 ++*datap;
3240
3241 return 1;
3242 }
3243
3244 /* Create the hash table of all entries in the .debug_types section(s).
3245 The result is zero if there are no .debug_types sections,
3246 otherwise non-zero. */
3247
3248 static int
3249 create_all_type_units (struct objfile *objfile)
3250 {
3251 htab_t types_htab = NULL;
3252 struct dwarf2_per_cu_data **iter;
3253 int ix;
3254 struct dwarf2_section_info *section;
3255
3256 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3257 {
3258 dwarf2_per_objfile->signatured_types = NULL;
3259 return 0;
3260 }
3261
3262 for (ix = 0;
3263 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3264 ix, section);
3265 ++ix)
3266 {
3267 gdb_byte *info_ptr, *end_ptr;
3268
3269 dwarf2_read_section (objfile, section);
3270 info_ptr = section->buffer;
3271
3272 if (info_ptr == NULL)
3273 continue;
3274
3275 if (types_htab == NULL)
3276 types_htab = allocate_signatured_type_table (objfile);
3277
3278 if (dwarf2_die_debug)
3279 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3280
3281 end_ptr = info_ptr + section->size;
3282 while (info_ptr < end_ptr)
3283 {
3284 sect_offset offset;
3285 cu_offset type_offset;
3286 ULONGEST signature;
3287 struct signatured_type *sig_type;
3288 void **slot;
3289 gdb_byte *ptr = info_ptr;
3290 struct comp_unit_head header;
3291
3292 offset.sect_off = ptr - section->buffer;
3293
3294 /* We need to read the type's signature in order to build the hash
3295 table, but we don't need anything else just yet. */
3296
3297 ptr = read_and_check_type_unit_head (&header, section, ptr,
3298 &signature, &type_offset);
3299
3300 /* Skip dummy type units. */
3301 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3302 {
3303 info_ptr = info_ptr + header.initial_length_size + header.length;
3304 continue;
3305 }
3306
3307 sig_type = obstack_alloc (&objfile->objfile_obstack, sizeof (*sig_type));
3308 memset (sig_type, 0, sizeof (*sig_type));
3309 sig_type->signature = signature;
3310 sig_type->type_offset = type_offset;
3311 sig_type->per_cu.objfile = objfile;
3312 sig_type->per_cu.debug_types_section = section;
3313 sig_type->per_cu.offset = offset;
3314
3315 slot = htab_find_slot (types_htab, sig_type, INSERT);
3316 gdb_assert (slot != NULL);
3317 if (*slot != NULL)
3318 {
3319 const struct signatured_type *dup_sig = *slot;
3320
3321 complaint (&symfile_complaints,
3322 _("debug type entry at offset 0x%x is duplicate to the "
3323 "entry at offset 0x%x, signature 0x%s"),
3324 offset.sect_off, dup_sig->per_cu.offset.sect_off,
3325 phex (signature, sizeof (signature)));
3326 gdb_assert (signature == dup_sig->signature);
3327 }
3328 *slot = sig_type;
3329
3330 if (dwarf2_die_debug)
3331 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3332 offset.sect_off,
3333 phex (signature, sizeof (signature)));
3334
3335 info_ptr = info_ptr + header.initial_length_size + header.length;
3336 }
3337 }
3338
3339 dwarf2_per_objfile->signatured_types = types_htab;
3340
3341 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3342 dwarf2_per_objfile->all_type_units
3343 = obstack_alloc (&objfile->objfile_obstack,
3344 dwarf2_per_objfile->n_type_units
3345 * sizeof (struct dwarf2_per_cu_data *));
3346 iter = &dwarf2_per_objfile->all_type_units[0];
3347 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3348 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3349 == dwarf2_per_objfile->n_type_units);
3350
3351 return 1;
3352 }
3353
3354 /* Lookup a signature based type for DW_FORM_ref_sig8.
3355 Returns NULL if signature SIG is not present in the table. */
3356
3357 static struct signatured_type *
3358 lookup_signatured_type (ULONGEST sig)
3359 {
3360 struct signatured_type find_entry, *entry;
3361
3362 if (dwarf2_per_objfile->signatured_types == NULL)
3363 {
3364 complaint (&symfile_complaints,
3365 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3366 return NULL;
3367 }
3368
3369 find_entry.signature = sig;
3370 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3371 return entry;
3372 }
3373
3374 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3375
3376 static void
3377 init_cu_die_reader (struct die_reader_specs *reader,
3378 struct dwarf2_cu *cu)
3379 {
3380 reader->abfd = cu->objfile->obfd;
3381 reader->cu = cu;
3382 if (cu->per_cu->debug_types_section)
3383 {
3384 gdb_assert (cu->per_cu->debug_types_section->readin);
3385 reader->buffer = cu->per_cu->debug_types_section->buffer;
3386 }
3387 else
3388 {
3389 gdb_assert (dwarf2_per_objfile->info.readin);
3390 reader->buffer = dwarf2_per_objfile->info.buffer;
3391 }
3392 }
3393
3394 /* Find the base address of the compilation unit for range lists and
3395 location lists. It will normally be specified by DW_AT_low_pc.
3396 In DWARF-3 draft 4, the base address could be overridden by
3397 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3398 compilation units with discontinuous ranges. */
3399
3400 static void
3401 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3402 {
3403 struct attribute *attr;
3404
3405 cu->base_known = 0;
3406 cu->base_address = 0;
3407
3408 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3409 if (attr)
3410 {
3411 cu->base_address = DW_ADDR (attr);
3412 cu->base_known = 1;
3413 }
3414 else
3415 {
3416 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3417 if (attr)
3418 {
3419 cu->base_address = DW_ADDR (attr);
3420 cu->base_known = 1;
3421 }
3422 }
3423 }
3424
3425 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3426 to combine the common parts.
3427 Process compilation unit THIS_CU for a psymtab.
3428 SECTION is the section the CU/TU comes from,
3429 either .debug_info or .debug_types. */
3430
3431 static void
3432 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3433 struct dwarf2_section_info *section,
3434 int is_debug_types_section)
3435 {
3436 struct objfile *objfile = this_cu->objfile;
3437 bfd *abfd = objfile->obfd;
3438 gdb_byte *buffer = section->buffer;
3439 gdb_byte *info_ptr = buffer + this_cu->offset.sect_off;
3440 unsigned int buffer_size = section->size;
3441 gdb_byte *beg_of_comp_unit = info_ptr;
3442 struct die_info *comp_unit_die;
3443 struct partial_symtab *pst;
3444 CORE_ADDR baseaddr;
3445 struct cleanup *back_to_inner;
3446 struct dwarf2_cu cu;
3447 int has_children, has_pc_info;
3448 struct attribute *attr;
3449 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3450 struct die_reader_specs reader_specs;
3451 const char *filename;
3452
3453 /* If this compilation unit was already read in, free the
3454 cached copy in order to read it in again. This is
3455 necessary because we skipped some symbols when we first
3456 read in the compilation unit (see load_partial_dies).
3457 This problem could be avoided, but the benefit is
3458 unclear. */
3459 if (this_cu->cu != NULL)
3460 free_one_cached_comp_unit (this_cu->cu);
3461
3462 /* Note that this is a pointer to our stack frame, being
3463 added to a global data structure. It will be cleaned up
3464 in free_stack_comp_unit when we finish with this
3465 compilation unit. */
3466 init_one_comp_unit (&cu, this_cu);
3467 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3468
3469 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3470 is_debug_types_section);
3471
3472 /* Skip dummy compilation units. */
3473 if (info_ptr >= buffer + buffer_size
3474 || peek_abbrev_code (abfd, info_ptr) == 0)
3475 {
3476 do_cleanups (back_to_inner);
3477 return;
3478 }
3479
3480 cu.list_in_scope = &file_symbols;
3481
3482 /* Read the abbrevs for this compilation unit into a table. */
3483 dwarf2_read_abbrevs (&cu);
3484 make_cleanup (dwarf2_free_abbrev_table, &cu);
3485
3486 /* Read the compilation unit die. */
3487 init_cu_die_reader (&reader_specs, &cu);
3488 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3489 &has_children);
3490
3491 if (is_debug_types_section)
3492 {
3493 /* LENGTH has not been set yet for type units. */
3494 gdb_assert (this_cu->offset.sect_off == cu.header.offset.sect_off);
3495 this_cu->length = cu.header.length + cu.header.initial_length_size;
3496 }
3497 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3498 {
3499 do_cleanups (back_to_inner);
3500 return;
3501 }
3502
3503 prepare_one_comp_unit (&cu, comp_unit_die);
3504
3505 /* Allocate a new partial symbol table structure. */
3506 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3507 if (attr == NULL || !DW_STRING (attr))
3508 filename = "";
3509 else
3510 filename = DW_STRING (attr);
3511 pst = start_psymtab_common (objfile, objfile->section_offsets,
3512 filename,
3513 /* TEXTLOW and TEXTHIGH are set below. */
3514 0,
3515 objfile->global_psymbols.next,
3516 objfile->static_psymbols.next);
3517 pst->psymtabs_addrmap_supported = 1;
3518
3519 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3520 if (attr != NULL)
3521 pst->dirname = DW_STRING (attr);
3522
3523 pst->read_symtab_private = this_cu;
3524
3525 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3526
3527 /* Store the function that reads in the rest of the symbol table. */
3528 pst->read_symtab = dwarf2_psymtab_to_symtab;
3529
3530 this_cu->v.psymtab = pst;
3531
3532 dwarf2_find_base_address (comp_unit_die, &cu);
3533
3534 /* Possibly set the default values of LOWPC and HIGHPC from
3535 `DW_AT_ranges'. */
3536 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3537 &best_highpc, &cu, pst);
3538 if (has_pc_info == 1 && best_lowpc < best_highpc)
3539 /* Store the contiguous range if it is not empty; it can be empty for
3540 CUs with no code. */
3541 addrmap_set_empty (objfile->psymtabs_addrmap,
3542 best_lowpc + baseaddr,
3543 best_highpc + baseaddr - 1, pst);
3544
3545 /* Check if comp unit has_children.
3546 If so, read the rest of the partial symbols from this comp unit.
3547 If not, there's no more debug_info for this comp unit. */
3548 if (has_children)
3549 {
3550 struct partial_die_info *first_die;
3551 CORE_ADDR lowpc, highpc;
3552
3553 lowpc = ((CORE_ADDR) -1);
3554 highpc = ((CORE_ADDR) 0);
3555
3556 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3557
3558 scan_partial_symbols (first_die, &lowpc, &highpc,
3559 ! has_pc_info, &cu);
3560
3561 /* If we didn't find a lowpc, set it to highpc to avoid
3562 complaints from `maint check'. */
3563 if (lowpc == ((CORE_ADDR) -1))
3564 lowpc = highpc;
3565
3566 /* If the compilation unit didn't have an explicit address range,
3567 then use the information extracted from its child dies. */
3568 if (! has_pc_info)
3569 {
3570 best_lowpc = lowpc;
3571 best_highpc = highpc;
3572 }
3573 }
3574 pst->textlow = best_lowpc + baseaddr;
3575 pst->texthigh = best_highpc + baseaddr;
3576
3577 pst->n_global_syms = objfile->global_psymbols.next -
3578 (objfile->global_psymbols.list + pst->globals_offset);
3579 pst->n_static_syms = objfile->static_psymbols.next -
3580 (objfile->static_psymbols.list + pst->statics_offset);
3581 sort_pst_symbols (pst);
3582
3583 if (is_debug_types_section)
3584 {
3585 /* It's not clear we want to do anything with stmt lists here.
3586 Waiting to see what gcc ultimately does. */
3587 }
3588 else
3589 {
3590 /* Get the list of files included in the current compilation unit,
3591 and build a psymtab for each of them. */
3592 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3593 }
3594
3595 do_cleanups (back_to_inner);
3596 }
3597
3598 /* Traversal function for htab_traverse_noresize.
3599 Process one .debug_types comp-unit. */
3600
3601 static int
3602 process_type_comp_unit (void **slot, void *info)
3603 {
3604 struct signatured_type *entry = (struct signatured_type *) *slot;
3605 struct dwarf2_per_cu_data *this_cu;
3606
3607 gdb_assert (info == NULL);
3608 this_cu = &entry->per_cu;
3609
3610 gdb_assert (this_cu->debug_types_section->readin);
3611 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3612
3613 return 1;
3614 }
3615
3616 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3617 Build partial symbol tables for the .debug_types comp-units. */
3618
3619 static void
3620 build_type_psymtabs (struct objfile *objfile)
3621 {
3622 if (! create_all_type_units (objfile))
3623 return;
3624
3625 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3626 process_type_comp_unit, NULL);
3627 }
3628
3629 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3630
3631 static void
3632 psymtabs_addrmap_cleanup (void *o)
3633 {
3634 struct objfile *objfile = o;
3635
3636 objfile->psymtabs_addrmap = NULL;
3637 }
3638
3639 /* Build the partial symbol table by doing a quick pass through the
3640 .debug_info and .debug_abbrev sections. */
3641
3642 static void
3643 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3644 {
3645 struct cleanup *back_to, *addrmap_cleanup;
3646 struct obstack temp_obstack;
3647 int i;
3648
3649 dwarf2_per_objfile->reading_partial_symbols = 1;
3650
3651 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3652
3653 /* Any cached compilation units will be linked by the per-objfile
3654 read_in_chain. Make sure to free them when we're done. */
3655 back_to = make_cleanup (free_cached_comp_units, NULL);
3656
3657 build_type_psymtabs (objfile);
3658
3659 create_all_comp_units (objfile);
3660
3661 /* Create a temporary address map on a temporary obstack. We later
3662 copy this to the final obstack. */
3663 obstack_init (&temp_obstack);
3664 make_cleanup_obstack_free (&temp_obstack);
3665 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3666 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3667
3668 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3669 {
3670 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3671
3672 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3673 }
3674
3675 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3676 &objfile->objfile_obstack);
3677 discard_cleanups (addrmap_cleanup);
3678
3679 do_cleanups (back_to);
3680 }
3681
3682 /* Load the partial DIEs for a secondary CU into memory. */
3683
3684 static void
3685 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3686 {
3687 struct objfile *objfile = this_cu->objfile;
3688 bfd *abfd = objfile->obfd;
3689 gdb_byte *info_ptr;
3690 struct die_info *comp_unit_die;
3691 struct dwarf2_cu *cu;
3692 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3693 int has_children;
3694 struct die_reader_specs reader_specs;
3695 int read_cu = 0;
3696 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
3697
3698 gdb_assert (! this_cu->debug_types_section);
3699
3700 gdb_assert (section->readin);
3701 info_ptr = section->buffer + this_cu->offset.sect_off;
3702
3703 if (this_cu->cu == NULL)
3704 {
3705 cu = xmalloc (sizeof (*cu));
3706 init_one_comp_unit (cu, this_cu);
3707
3708 read_cu = 1;
3709
3710 /* If an error occurs while loading, release our storage. */
3711 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3712
3713 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3714 0);
3715
3716 /* Skip dummy compilation units. */
3717 if (info_ptr >= (section->buffer + section->size)
3718 || peek_abbrev_code (abfd, info_ptr) == 0)
3719 {
3720 do_cleanups (free_cu_cleanup);
3721 return;
3722 }
3723 }
3724 else
3725 {
3726 cu = this_cu->cu;
3727 info_ptr += cu->header.first_die_offset.cu_off;
3728 }
3729
3730 /* Read the abbrevs for this compilation unit into a table. */
3731 gdb_assert (cu->dwarf2_abbrevs == NULL);
3732 dwarf2_read_abbrevs (cu);
3733 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3734
3735 /* Read the compilation unit die. */
3736 init_cu_die_reader (&reader_specs, cu);
3737 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3738 &has_children);
3739
3740 prepare_one_comp_unit (cu, comp_unit_die);
3741
3742 /* Check if comp unit has_children.
3743 If so, read the rest of the partial symbols from this comp unit.
3744 If not, there's no more debug_info for this comp unit. */
3745 if (has_children)
3746 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
3747
3748 do_cleanups (free_abbrevs_cleanup);
3749
3750 if (read_cu)
3751 {
3752 /* We've successfully allocated this compilation unit. Let our
3753 caller clean it up when finished with it. */
3754 discard_cleanups (free_cu_cleanup);
3755
3756 /* Link this CU into read_in_chain. */
3757 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3758 dwarf2_per_objfile->read_in_chain = this_cu;
3759 }
3760 }
3761
3762 /* Create a list of all compilation units in OBJFILE.
3763 This is only done for -readnow and building partial symtabs. */
3764
3765 static void
3766 create_all_comp_units (struct objfile *objfile)
3767 {
3768 int n_allocated;
3769 int n_comp_units;
3770 struct dwarf2_per_cu_data **all_comp_units;
3771 gdb_byte *info_ptr;
3772
3773 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3774 info_ptr = dwarf2_per_objfile->info.buffer;
3775
3776 n_comp_units = 0;
3777 n_allocated = 10;
3778 all_comp_units = xmalloc (n_allocated
3779 * sizeof (struct dwarf2_per_cu_data *));
3780
3781 while (info_ptr < dwarf2_per_objfile->info.buffer
3782 + dwarf2_per_objfile->info.size)
3783 {
3784 unsigned int length, initial_length_size;
3785 struct dwarf2_per_cu_data *this_cu;
3786 sect_offset offset;
3787
3788 offset.sect_off = info_ptr - dwarf2_per_objfile->info.buffer;
3789
3790 /* Read just enough information to find out where the next
3791 compilation unit is. */
3792 length = read_initial_length (objfile->obfd, info_ptr,
3793 &initial_length_size);
3794
3795 /* Save the compilation unit for later lookup. */
3796 this_cu = obstack_alloc (&objfile->objfile_obstack,
3797 sizeof (struct dwarf2_per_cu_data));
3798 memset (this_cu, 0, sizeof (*this_cu));
3799 this_cu->offset = offset;
3800 this_cu->length = length + initial_length_size;
3801 this_cu->objfile = objfile;
3802
3803 if (n_comp_units == n_allocated)
3804 {
3805 n_allocated *= 2;
3806 all_comp_units = xrealloc (all_comp_units,
3807 n_allocated
3808 * sizeof (struct dwarf2_per_cu_data *));
3809 }
3810 all_comp_units[n_comp_units++] = this_cu;
3811
3812 info_ptr = info_ptr + this_cu->length;
3813 }
3814
3815 dwarf2_per_objfile->all_comp_units
3816 = obstack_alloc (&objfile->objfile_obstack,
3817 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3818 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3819 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3820 xfree (all_comp_units);
3821 dwarf2_per_objfile->n_comp_units = n_comp_units;
3822 }
3823
3824 /* Process all loaded DIEs for compilation unit CU, starting at
3825 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3826 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3827 DW_AT_ranges). If NEED_PC is set, then this function will set
3828 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3829 and record the covered ranges in the addrmap. */
3830
3831 static void
3832 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3833 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3834 {
3835 struct partial_die_info *pdi;
3836
3837 /* Now, march along the PDI's, descending into ones which have
3838 interesting children but skipping the children of the other ones,
3839 until we reach the end of the compilation unit. */
3840
3841 pdi = first_die;
3842
3843 while (pdi != NULL)
3844 {
3845 fixup_partial_die (pdi, cu);
3846
3847 /* Anonymous namespaces or modules have no name but have interesting
3848 children, so we need to look at them. Ditto for anonymous
3849 enums. */
3850
3851 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3852 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3853 {
3854 switch (pdi->tag)
3855 {
3856 case DW_TAG_subprogram:
3857 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3858 break;
3859 case DW_TAG_constant:
3860 case DW_TAG_variable:
3861 case DW_TAG_typedef:
3862 case DW_TAG_union_type:
3863 if (!pdi->is_declaration)
3864 {
3865 add_partial_symbol (pdi, cu);
3866 }
3867 break;
3868 case DW_TAG_class_type:
3869 case DW_TAG_interface_type:
3870 case DW_TAG_structure_type:
3871 if (!pdi->is_declaration)
3872 {
3873 add_partial_symbol (pdi, cu);
3874 }
3875 break;
3876 case DW_TAG_enumeration_type:
3877 if (!pdi->is_declaration)
3878 add_partial_enumeration (pdi, cu);
3879 break;
3880 case DW_TAG_base_type:
3881 case DW_TAG_subrange_type:
3882 /* File scope base type definitions are added to the partial
3883 symbol table. */
3884 add_partial_symbol (pdi, cu);
3885 break;
3886 case DW_TAG_namespace:
3887 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3888 break;
3889 case DW_TAG_module:
3890 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3891 break;
3892 default:
3893 break;
3894 }
3895 }
3896
3897 /* If the die has a sibling, skip to the sibling. */
3898
3899 pdi = pdi->die_sibling;
3900 }
3901 }
3902
3903 /* Functions used to compute the fully scoped name of a partial DIE.
3904
3905 Normally, this is simple. For C++, the parent DIE's fully scoped
3906 name is concatenated with "::" and the partial DIE's name. For
3907 Java, the same thing occurs except that "." is used instead of "::".
3908 Enumerators are an exception; they use the scope of their parent
3909 enumeration type, i.e. the name of the enumeration type is not
3910 prepended to the enumerator.
3911
3912 There are two complexities. One is DW_AT_specification; in this
3913 case "parent" means the parent of the target of the specification,
3914 instead of the direct parent of the DIE. The other is compilers
3915 which do not emit DW_TAG_namespace; in this case we try to guess
3916 the fully qualified name of structure types from their members'
3917 linkage names. This must be done using the DIE's children rather
3918 than the children of any DW_AT_specification target. We only need
3919 to do this for structures at the top level, i.e. if the target of
3920 any DW_AT_specification (if any; otherwise the DIE itself) does not
3921 have a parent. */
3922
3923 /* Compute the scope prefix associated with PDI's parent, in
3924 compilation unit CU. The result will be allocated on CU's
3925 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3926 field. NULL is returned if no prefix is necessary. */
3927 static char *
3928 partial_die_parent_scope (struct partial_die_info *pdi,
3929 struct dwarf2_cu *cu)
3930 {
3931 char *grandparent_scope;
3932 struct partial_die_info *parent, *real_pdi;
3933
3934 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3935 then this means the parent of the specification DIE. */
3936
3937 real_pdi = pdi;
3938 while (real_pdi->has_specification)
3939 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3940
3941 parent = real_pdi->die_parent;
3942 if (parent == NULL)
3943 return NULL;
3944
3945 if (parent->scope_set)
3946 return parent->scope;
3947
3948 fixup_partial_die (parent, cu);
3949
3950 grandparent_scope = partial_die_parent_scope (parent, cu);
3951
3952 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3953 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3954 Work around this problem here. */
3955 if (cu->language == language_cplus
3956 && parent->tag == DW_TAG_namespace
3957 && strcmp (parent->name, "::") == 0
3958 && grandparent_scope == NULL)
3959 {
3960 parent->scope = NULL;
3961 parent->scope_set = 1;
3962 return NULL;
3963 }
3964
3965 if (pdi->tag == DW_TAG_enumerator)
3966 /* Enumerators should not get the name of the enumeration as a prefix. */
3967 parent->scope = grandparent_scope;
3968 else if (parent->tag == DW_TAG_namespace
3969 || parent->tag == DW_TAG_module
3970 || parent->tag == DW_TAG_structure_type
3971 || parent->tag == DW_TAG_class_type
3972 || parent->tag == DW_TAG_interface_type
3973 || parent->tag == DW_TAG_union_type
3974 || parent->tag == DW_TAG_enumeration_type)
3975 {
3976 if (grandparent_scope == NULL)
3977 parent->scope = parent->name;
3978 else
3979 parent->scope = typename_concat (&cu->comp_unit_obstack,
3980 grandparent_scope,
3981 parent->name, 0, cu);
3982 }
3983 else
3984 {
3985 /* FIXME drow/2004-04-01: What should we be doing with
3986 function-local names? For partial symbols, we should probably be
3987 ignoring them. */
3988 complaint (&symfile_complaints,
3989 _("unhandled containing DIE tag %d for DIE at %d"),
3990 parent->tag, pdi->offset.sect_off);
3991 parent->scope = grandparent_scope;
3992 }
3993
3994 parent->scope_set = 1;
3995 return parent->scope;
3996 }
3997
3998 /* Return the fully scoped name associated with PDI, from compilation unit
3999 CU. The result will be allocated with malloc. */
4000
4001 static char *
4002 partial_die_full_name (struct partial_die_info *pdi,
4003 struct dwarf2_cu *cu)
4004 {
4005 char *parent_scope;
4006
4007 /* If this is a template instantiation, we can not work out the
4008 template arguments from partial DIEs. So, unfortunately, we have
4009 to go through the full DIEs. At least any work we do building
4010 types here will be reused if full symbols are loaded later. */
4011 if (pdi->has_template_arguments)
4012 {
4013 fixup_partial_die (pdi, cu);
4014
4015 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4016 {
4017 struct die_info *die;
4018 struct attribute attr;
4019 struct dwarf2_cu *ref_cu = cu;
4020
4021 /* DW_FORM_ref_addr is using section offset. */
4022 attr.name = 0;
4023 attr.form = DW_FORM_ref_addr;
4024 attr.u.unsnd = pdi->offset.sect_off;
4025 die = follow_die_ref (NULL, &attr, &ref_cu);
4026
4027 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4028 }
4029 }
4030
4031 parent_scope = partial_die_parent_scope (pdi, cu);
4032 if (parent_scope == NULL)
4033 return NULL;
4034 else
4035 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4036 }
4037
4038 static void
4039 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4040 {
4041 struct objfile *objfile = cu->objfile;
4042 CORE_ADDR addr = 0;
4043 char *actual_name = NULL;
4044 CORE_ADDR baseaddr;
4045 int built_actual_name = 0;
4046
4047 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4048
4049 actual_name = partial_die_full_name (pdi, cu);
4050 if (actual_name)
4051 built_actual_name = 1;
4052
4053 if (actual_name == NULL)
4054 actual_name = pdi->name;
4055
4056 switch (pdi->tag)
4057 {
4058 case DW_TAG_subprogram:
4059 if (pdi->is_external || cu->language == language_ada)
4060 {
4061 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4062 of the global scope. But in Ada, we want to be able to access
4063 nested procedures globally. So all Ada subprograms are stored
4064 in the global scope. */
4065 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4066 mst_text, objfile); */
4067 add_psymbol_to_list (actual_name, strlen (actual_name),
4068 built_actual_name,
4069 VAR_DOMAIN, LOC_BLOCK,
4070 &objfile->global_psymbols,
4071 0, pdi->lowpc + baseaddr,
4072 cu->language, objfile);
4073 }
4074 else
4075 {
4076 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4077 mst_file_text, objfile); */
4078 add_psymbol_to_list (actual_name, strlen (actual_name),
4079 built_actual_name,
4080 VAR_DOMAIN, LOC_BLOCK,
4081 &objfile->static_psymbols,
4082 0, pdi->lowpc + baseaddr,
4083 cu->language, objfile);
4084 }
4085 break;
4086 case DW_TAG_constant:
4087 {
4088 struct psymbol_allocation_list *list;
4089
4090 if (pdi->is_external)
4091 list = &objfile->global_psymbols;
4092 else
4093 list = &objfile->static_psymbols;
4094 add_psymbol_to_list (actual_name, strlen (actual_name),
4095 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4096 list, 0, 0, cu->language, objfile);
4097 }
4098 break;
4099 case DW_TAG_variable:
4100 if (pdi->locdesc)
4101 addr = decode_locdesc (pdi->locdesc, cu);
4102
4103 if (pdi->locdesc
4104 && addr == 0
4105 && !dwarf2_per_objfile->has_section_at_zero)
4106 {
4107 /* A global or static variable may also have been stripped
4108 out by the linker if unused, in which case its address
4109 will be nullified; do not add such variables into partial
4110 symbol table then. */
4111 }
4112 else if (pdi->is_external)
4113 {
4114 /* Global Variable.
4115 Don't enter into the minimal symbol tables as there is
4116 a minimal symbol table entry from the ELF symbols already.
4117 Enter into partial symbol table if it has a location
4118 descriptor or a type.
4119 If the location descriptor is missing, new_symbol will create
4120 a LOC_UNRESOLVED symbol, the address of the variable will then
4121 be determined from the minimal symbol table whenever the variable
4122 is referenced.
4123 The address for the partial symbol table entry is not
4124 used by GDB, but it comes in handy for debugging partial symbol
4125 table building. */
4126
4127 if (pdi->locdesc || pdi->has_type)
4128 add_psymbol_to_list (actual_name, strlen (actual_name),
4129 built_actual_name,
4130 VAR_DOMAIN, LOC_STATIC,
4131 &objfile->global_psymbols,
4132 0, addr + baseaddr,
4133 cu->language, objfile);
4134 }
4135 else
4136 {
4137 /* Static Variable. Skip symbols without location descriptors. */
4138 if (pdi->locdesc == NULL)
4139 {
4140 if (built_actual_name)
4141 xfree (actual_name);
4142 return;
4143 }
4144 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4145 mst_file_data, objfile); */
4146 add_psymbol_to_list (actual_name, strlen (actual_name),
4147 built_actual_name,
4148 VAR_DOMAIN, LOC_STATIC,
4149 &objfile->static_psymbols,
4150 0, addr + baseaddr,
4151 cu->language, objfile);
4152 }
4153 break;
4154 case DW_TAG_typedef:
4155 case DW_TAG_base_type:
4156 case DW_TAG_subrange_type:
4157 add_psymbol_to_list (actual_name, strlen (actual_name),
4158 built_actual_name,
4159 VAR_DOMAIN, LOC_TYPEDEF,
4160 &objfile->static_psymbols,
4161 0, (CORE_ADDR) 0, cu->language, objfile);
4162 break;
4163 case DW_TAG_namespace:
4164 add_psymbol_to_list (actual_name, strlen (actual_name),
4165 built_actual_name,
4166 VAR_DOMAIN, LOC_TYPEDEF,
4167 &objfile->global_psymbols,
4168 0, (CORE_ADDR) 0, cu->language, objfile);
4169 break;
4170 case DW_TAG_class_type:
4171 case DW_TAG_interface_type:
4172 case DW_TAG_structure_type:
4173 case DW_TAG_union_type:
4174 case DW_TAG_enumeration_type:
4175 /* Skip external references. The DWARF standard says in the section
4176 about "Structure, Union, and Class Type Entries": "An incomplete
4177 structure, union or class type is represented by a structure,
4178 union or class entry that does not have a byte size attribute
4179 and that has a DW_AT_declaration attribute." */
4180 if (!pdi->has_byte_size && pdi->is_declaration)
4181 {
4182 if (built_actual_name)
4183 xfree (actual_name);
4184 return;
4185 }
4186
4187 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4188 static vs. global. */
4189 add_psymbol_to_list (actual_name, strlen (actual_name),
4190 built_actual_name,
4191 STRUCT_DOMAIN, LOC_TYPEDEF,
4192 (cu->language == language_cplus
4193 || cu->language == language_java)
4194 ? &objfile->global_psymbols
4195 : &objfile->static_psymbols,
4196 0, (CORE_ADDR) 0, cu->language, objfile);
4197
4198 break;
4199 case DW_TAG_enumerator:
4200 add_psymbol_to_list (actual_name, strlen (actual_name),
4201 built_actual_name,
4202 VAR_DOMAIN, LOC_CONST,
4203 (cu->language == language_cplus
4204 || cu->language == language_java)
4205 ? &objfile->global_psymbols
4206 : &objfile->static_psymbols,
4207 0, (CORE_ADDR) 0, cu->language, objfile);
4208 break;
4209 default:
4210 break;
4211 }
4212
4213 if (built_actual_name)
4214 xfree (actual_name);
4215 }
4216
4217 /* Read a partial die corresponding to a namespace; also, add a symbol
4218 corresponding to that namespace to the symbol table. NAMESPACE is
4219 the name of the enclosing namespace. */
4220
4221 static void
4222 add_partial_namespace (struct partial_die_info *pdi,
4223 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4224 int need_pc, struct dwarf2_cu *cu)
4225 {
4226 /* Add a symbol for the namespace. */
4227
4228 add_partial_symbol (pdi, cu);
4229
4230 /* Now scan partial symbols in that namespace. */
4231
4232 if (pdi->has_children)
4233 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4234 }
4235
4236 /* Read a partial die corresponding to a Fortran module. */
4237
4238 static void
4239 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4240 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4241 {
4242 /* Now scan partial symbols in that module. */
4243
4244 if (pdi->has_children)
4245 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4246 }
4247
4248 /* Read a partial die corresponding to a subprogram and create a partial
4249 symbol for that subprogram. When the CU language allows it, this
4250 routine also defines a partial symbol for each nested subprogram
4251 that this subprogram contains.
4252
4253 DIE my also be a lexical block, in which case we simply search
4254 recursively for suprograms defined inside that lexical block.
4255 Again, this is only performed when the CU language allows this
4256 type of definitions. */
4257
4258 static void
4259 add_partial_subprogram (struct partial_die_info *pdi,
4260 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4261 int need_pc, struct dwarf2_cu *cu)
4262 {
4263 if (pdi->tag == DW_TAG_subprogram)
4264 {
4265 if (pdi->has_pc_info)
4266 {
4267 if (pdi->lowpc < *lowpc)
4268 *lowpc = pdi->lowpc;
4269 if (pdi->highpc > *highpc)
4270 *highpc = pdi->highpc;
4271 if (need_pc)
4272 {
4273 CORE_ADDR baseaddr;
4274 struct objfile *objfile = cu->objfile;
4275
4276 baseaddr = ANOFFSET (objfile->section_offsets,
4277 SECT_OFF_TEXT (objfile));
4278 addrmap_set_empty (objfile->psymtabs_addrmap,
4279 pdi->lowpc + baseaddr,
4280 pdi->highpc - 1 + baseaddr,
4281 cu->per_cu->v.psymtab);
4282 }
4283 }
4284
4285 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4286 {
4287 if (!pdi->is_declaration)
4288 /* Ignore subprogram DIEs that do not have a name, they are
4289 illegal. Do not emit a complaint at this point, we will
4290 do so when we convert this psymtab into a symtab. */
4291 if (pdi->name)
4292 add_partial_symbol (pdi, cu);
4293 }
4294 }
4295
4296 if (! pdi->has_children)
4297 return;
4298
4299 if (cu->language == language_ada)
4300 {
4301 pdi = pdi->die_child;
4302 while (pdi != NULL)
4303 {
4304 fixup_partial_die (pdi, cu);
4305 if (pdi->tag == DW_TAG_subprogram
4306 || pdi->tag == DW_TAG_lexical_block)
4307 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4308 pdi = pdi->die_sibling;
4309 }
4310 }
4311 }
4312
4313 /* Read a partial die corresponding to an enumeration type. */
4314
4315 static void
4316 add_partial_enumeration (struct partial_die_info *enum_pdi,
4317 struct dwarf2_cu *cu)
4318 {
4319 struct partial_die_info *pdi;
4320
4321 if (enum_pdi->name != NULL)
4322 add_partial_symbol (enum_pdi, cu);
4323
4324 pdi = enum_pdi->die_child;
4325 while (pdi)
4326 {
4327 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4328 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4329 else
4330 add_partial_symbol (pdi, cu);
4331 pdi = pdi->die_sibling;
4332 }
4333 }
4334
4335 /* Return the initial uleb128 in the die at INFO_PTR. */
4336
4337 static unsigned int
4338 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4339 {
4340 unsigned int bytes_read;
4341
4342 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4343 }
4344
4345 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4346 Return the corresponding abbrev, or NULL if the number is zero (indicating
4347 an empty DIE). In either case *BYTES_READ will be set to the length of
4348 the initial number. */
4349
4350 static struct abbrev_info *
4351 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4352 struct dwarf2_cu *cu)
4353 {
4354 bfd *abfd = cu->objfile->obfd;
4355 unsigned int abbrev_number;
4356 struct abbrev_info *abbrev;
4357
4358 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4359
4360 if (abbrev_number == 0)
4361 return NULL;
4362
4363 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4364 if (!abbrev)
4365 {
4366 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4367 abbrev_number, bfd_get_filename (abfd));
4368 }
4369
4370 return abbrev;
4371 }
4372
4373 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4374 Returns a pointer to the end of a series of DIEs, terminated by an empty
4375 DIE. Any children of the skipped DIEs will also be skipped. */
4376
4377 static gdb_byte *
4378 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4379 {
4380 struct abbrev_info *abbrev;
4381 unsigned int bytes_read;
4382
4383 while (1)
4384 {
4385 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4386 if (abbrev == NULL)
4387 return info_ptr + bytes_read;
4388 else
4389 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4390 }
4391 }
4392
4393 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4394 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4395 abbrev corresponding to that skipped uleb128 should be passed in
4396 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4397 children. */
4398
4399 static gdb_byte *
4400 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4401 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4402 {
4403 unsigned int bytes_read;
4404 struct attribute attr;
4405 bfd *abfd = cu->objfile->obfd;
4406 unsigned int form, i;
4407
4408 for (i = 0; i < abbrev->num_attrs; i++)
4409 {
4410 /* The only abbrev we care about is DW_AT_sibling. */
4411 if (abbrev->attrs[i].name == DW_AT_sibling)
4412 {
4413 read_attribute (&attr, &abbrev->attrs[i],
4414 abfd, info_ptr, cu);
4415 if (attr.form == DW_FORM_ref_addr)
4416 complaint (&symfile_complaints,
4417 _("ignoring absolute DW_AT_sibling"));
4418 else
4419 return buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
4420 }
4421
4422 /* If it isn't DW_AT_sibling, skip this attribute. */
4423 form = abbrev->attrs[i].form;
4424 skip_attribute:
4425 switch (form)
4426 {
4427 case DW_FORM_ref_addr:
4428 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4429 and later it is offset sized. */
4430 if (cu->header.version == 2)
4431 info_ptr += cu->header.addr_size;
4432 else
4433 info_ptr += cu->header.offset_size;
4434 break;
4435 case DW_FORM_addr:
4436 info_ptr += cu->header.addr_size;
4437 break;
4438 case DW_FORM_data1:
4439 case DW_FORM_ref1:
4440 case DW_FORM_flag:
4441 info_ptr += 1;
4442 break;
4443 case DW_FORM_flag_present:
4444 break;
4445 case DW_FORM_data2:
4446 case DW_FORM_ref2:
4447 info_ptr += 2;
4448 break;
4449 case DW_FORM_data4:
4450 case DW_FORM_ref4:
4451 info_ptr += 4;
4452 break;
4453 case DW_FORM_data8:
4454 case DW_FORM_ref8:
4455 case DW_FORM_ref_sig8:
4456 info_ptr += 8;
4457 break;
4458 case DW_FORM_string:
4459 read_direct_string (abfd, info_ptr, &bytes_read);
4460 info_ptr += bytes_read;
4461 break;
4462 case DW_FORM_sec_offset:
4463 case DW_FORM_strp:
4464 info_ptr += cu->header.offset_size;
4465 break;
4466 case DW_FORM_exprloc:
4467 case DW_FORM_block:
4468 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4469 info_ptr += bytes_read;
4470 break;
4471 case DW_FORM_block1:
4472 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4473 break;
4474 case DW_FORM_block2:
4475 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4476 break;
4477 case DW_FORM_block4:
4478 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4479 break;
4480 case DW_FORM_sdata:
4481 case DW_FORM_udata:
4482 case DW_FORM_ref_udata:
4483 info_ptr = skip_leb128 (abfd, info_ptr);
4484 break;
4485 case DW_FORM_indirect:
4486 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4487 info_ptr += bytes_read;
4488 /* We need to continue parsing from here, so just go back to
4489 the top. */
4490 goto skip_attribute;
4491
4492 default:
4493 error (_("Dwarf Error: Cannot handle %s "
4494 "in DWARF reader [in module %s]"),
4495 dwarf_form_name (form),
4496 bfd_get_filename (abfd));
4497 }
4498 }
4499
4500 if (abbrev->has_children)
4501 return skip_children (buffer, info_ptr, cu);
4502 else
4503 return info_ptr;
4504 }
4505
4506 /* Locate ORIG_PDI's sibling.
4507 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4508 in BUFFER. */
4509
4510 static gdb_byte *
4511 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4512 gdb_byte *buffer, gdb_byte *info_ptr,
4513 bfd *abfd, struct dwarf2_cu *cu)
4514 {
4515 /* Do we know the sibling already? */
4516
4517 if (orig_pdi->sibling)
4518 return orig_pdi->sibling;
4519
4520 /* Are there any children to deal with? */
4521
4522 if (!orig_pdi->has_children)
4523 return info_ptr;
4524
4525 /* Skip the children the long way. */
4526
4527 return skip_children (buffer, info_ptr, cu);
4528 }
4529
4530 /* Expand this partial symbol table into a full symbol table. */
4531
4532 static void
4533 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4534 {
4535 if (pst != NULL)
4536 {
4537 if (pst->readin)
4538 {
4539 warning (_("bug: psymtab for %s is already read in."),
4540 pst->filename);
4541 }
4542 else
4543 {
4544 if (info_verbose)
4545 {
4546 printf_filtered (_("Reading in symbols for %s..."),
4547 pst->filename);
4548 gdb_flush (gdb_stdout);
4549 }
4550
4551 /* Restore our global data. */
4552 dwarf2_per_objfile = objfile_data (pst->objfile,
4553 dwarf2_objfile_data_key);
4554
4555 /* If this psymtab is constructed from a debug-only objfile, the
4556 has_section_at_zero flag will not necessarily be correct. We
4557 can get the correct value for this flag by looking at the data
4558 associated with the (presumably stripped) associated objfile. */
4559 if (pst->objfile->separate_debug_objfile_backlink)
4560 {
4561 struct dwarf2_per_objfile *dpo_backlink
4562 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4563 dwarf2_objfile_data_key);
4564
4565 dwarf2_per_objfile->has_section_at_zero
4566 = dpo_backlink->has_section_at_zero;
4567 }
4568
4569 dwarf2_per_objfile->reading_partial_symbols = 0;
4570
4571 psymtab_to_symtab_1 (pst);
4572
4573 /* Finish up the debug error message. */
4574 if (info_verbose)
4575 printf_filtered (_("done.\n"));
4576 }
4577 }
4578 }
4579 \f
4580 /* Reading in full CUs. */
4581
4582 /* Add PER_CU to the queue. */
4583
4584 static void
4585 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4586 {
4587 struct dwarf2_queue_item *item;
4588
4589 per_cu->queued = 1;
4590 item = xmalloc (sizeof (*item));
4591 item->per_cu = per_cu;
4592 item->next = NULL;
4593
4594 if (dwarf2_queue == NULL)
4595 dwarf2_queue = item;
4596 else
4597 dwarf2_queue_tail->next = item;
4598
4599 dwarf2_queue_tail = item;
4600 }
4601
4602 /* Process the queue. */
4603
4604 static void
4605 process_queue (void)
4606 {
4607 struct dwarf2_queue_item *item, *next_item;
4608
4609 /* The queue starts out with one item, but following a DIE reference
4610 may load a new CU, adding it to the end of the queue. */
4611 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4612 {
4613 if (dwarf2_per_objfile->using_index
4614 ? !item->per_cu->v.quick->symtab
4615 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4616 process_full_comp_unit (item->per_cu);
4617
4618 item->per_cu->queued = 0;
4619 next_item = item->next;
4620 xfree (item);
4621 }
4622
4623 dwarf2_queue_tail = NULL;
4624 }
4625
4626 /* Free all allocated queue entries. This function only releases anything if
4627 an error was thrown; if the queue was processed then it would have been
4628 freed as we went along. */
4629
4630 static void
4631 dwarf2_release_queue (void *dummy)
4632 {
4633 struct dwarf2_queue_item *item, *last;
4634
4635 item = dwarf2_queue;
4636 while (item)
4637 {
4638 /* Anything still marked queued is likely to be in an
4639 inconsistent state, so discard it. */
4640 if (item->per_cu->queued)
4641 {
4642 if (item->per_cu->cu != NULL)
4643 free_one_cached_comp_unit (item->per_cu->cu);
4644 item->per_cu->queued = 0;
4645 }
4646
4647 last = item;
4648 item = item->next;
4649 xfree (last);
4650 }
4651
4652 dwarf2_queue = dwarf2_queue_tail = NULL;
4653 }
4654
4655 /* Read in full symbols for PST, and anything it depends on. */
4656
4657 static void
4658 psymtab_to_symtab_1 (struct partial_symtab *pst)
4659 {
4660 struct dwarf2_per_cu_data *per_cu;
4661 struct cleanup *back_to;
4662 int i;
4663
4664 for (i = 0; i < pst->number_of_dependencies; i++)
4665 if (!pst->dependencies[i]->readin)
4666 {
4667 /* Inform about additional files that need to be read in. */
4668 if (info_verbose)
4669 {
4670 /* FIXME: i18n: Need to make this a single string. */
4671 fputs_filtered (" ", gdb_stdout);
4672 wrap_here ("");
4673 fputs_filtered ("and ", gdb_stdout);
4674 wrap_here ("");
4675 printf_filtered ("%s...", pst->dependencies[i]->filename);
4676 wrap_here (""); /* Flush output. */
4677 gdb_flush (gdb_stdout);
4678 }
4679 psymtab_to_symtab_1 (pst->dependencies[i]);
4680 }
4681
4682 per_cu = pst->read_symtab_private;
4683
4684 if (per_cu == NULL)
4685 {
4686 /* It's an include file, no symbols to read for it.
4687 Everything is in the parent symtab. */
4688 pst->readin = 1;
4689 return;
4690 }
4691
4692 dw2_do_instantiate_symtab (per_cu);
4693 }
4694
4695 /* Load the DIEs associated with PER_CU into memory. */
4696
4697 static void
4698 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4699 {
4700 struct objfile *objfile = per_cu->objfile;
4701 bfd *abfd = objfile->obfd;
4702 struct dwarf2_cu *cu;
4703 sect_offset offset;
4704 gdb_byte *info_ptr, *beg_of_comp_unit;
4705 struct cleanup *free_cu_cleanup = NULL;
4706 struct attribute *attr;
4707 int read_cu = 0;
4708
4709 gdb_assert (! per_cu->debug_types_section);
4710
4711 /* Set local variables from the partial symbol table info. */
4712 offset = per_cu->offset;
4713
4714 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4715 info_ptr = dwarf2_per_objfile->info.buffer + offset.sect_off;
4716 beg_of_comp_unit = info_ptr;
4717
4718 if (per_cu->cu == NULL)
4719 {
4720 cu = xmalloc (sizeof (*cu));
4721 init_one_comp_unit (cu, per_cu);
4722
4723 read_cu = 1;
4724
4725 /* If an error occurs while loading, release our storage. */
4726 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4727
4728 /* Read in the comp_unit header. */
4729 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4730
4731 /* Skip dummy compilation units. */
4732 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4733 + dwarf2_per_objfile->info.size)
4734 || peek_abbrev_code (abfd, info_ptr) == 0)
4735 {
4736 do_cleanups (free_cu_cleanup);
4737 return;
4738 }
4739
4740 /* Complete the cu_header. */
4741 cu->header.offset = offset;
4742 cu->header.first_die_offset.cu_off = info_ptr - beg_of_comp_unit;
4743 }
4744 else
4745 {
4746 cu = per_cu->cu;
4747 info_ptr += cu->header.first_die_offset.cu_off;
4748 }
4749
4750 cu->dies = read_comp_unit (info_ptr, cu);
4751
4752 /* We try not to read any attributes in this function, because not
4753 all CUs needed for references have been loaded yet, and symbol
4754 table processing isn't initialized. But we have to set the CU language,
4755 or we won't be able to build types correctly. */
4756 prepare_one_comp_unit (cu, cu->dies);
4757
4758 /* Similarly, if we do not read the producer, we can not apply
4759 producer-specific interpretation. */
4760 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4761 if (attr)
4762 cu->producer = DW_STRING (attr);
4763
4764 if (read_cu)
4765 {
4766 /* We've successfully allocated this compilation unit. Let our
4767 caller clean it up when finished with it. */
4768 discard_cleanups (free_cu_cleanup);
4769
4770 /* Link this CU into read_in_chain. */
4771 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4772 dwarf2_per_objfile->read_in_chain = per_cu;
4773 }
4774 }
4775
4776 /* Add a DIE to the delayed physname list. */
4777
4778 static void
4779 add_to_method_list (struct type *type, int fnfield_index, int index,
4780 const char *name, struct die_info *die,
4781 struct dwarf2_cu *cu)
4782 {
4783 struct delayed_method_info mi;
4784 mi.type = type;
4785 mi.fnfield_index = fnfield_index;
4786 mi.index = index;
4787 mi.name = name;
4788 mi.die = die;
4789 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4790 }
4791
4792 /* A cleanup for freeing the delayed method list. */
4793
4794 static void
4795 free_delayed_list (void *ptr)
4796 {
4797 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4798 if (cu->method_list != NULL)
4799 {
4800 VEC_free (delayed_method_info, cu->method_list);
4801 cu->method_list = NULL;
4802 }
4803 }
4804
4805 /* Compute the physnames of any methods on the CU's method list.
4806
4807 The computation of method physnames is delayed in order to avoid the
4808 (bad) condition that one of the method's formal parameters is of an as yet
4809 incomplete type. */
4810
4811 static void
4812 compute_delayed_physnames (struct dwarf2_cu *cu)
4813 {
4814 int i;
4815 struct delayed_method_info *mi;
4816 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4817 {
4818 const char *physname;
4819 struct fn_fieldlist *fn_flp
4820 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4821 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4822 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4823 }
4824 }
4825
4826 /* Go objects should be embedded in a DW_TAG_module DIE,
4827 and it's not clear if/how imported objects will appear.
4828 To keep Go support simple until that's worked out,
4829 go back through what we've read and create something usable.
4830 We could do this while processing each DIE, and feels kinda cleaner,
4831 but that way is more invasive.
4832 This is to, for example, allow the user to type "p var" or "b main"
4833 without having to specify the package name, and allow lookups
4834 of module.object to work in contexts that use the expression
4835 parser. */
4836
4837 static void
4838 fixup_go_packaging (struct dwarf2_cu *cu)
4839 {
4840 char *package_name = NULL;
4841 struct pending *list;
4842 int i;
4843
4844 for (list = global_symbols; list != NULL; list = list->next)
4845 {
4846 for (i = 0; i < list->nsyms; ++i)
4847 {
4848 struct symbol *sym = list->symbol[i];
4849
4850 if (SYMBOL_LANGUAGE (sym) == language_go
4851 && SYMBOL_CLASS (sym) == LOC_BLOCK)
4852 {
4853 char *this_package_name = go_symbol_package_name (sym);
4854
4855 if (this_package_name == NULL)
4856 continue;
4857 if (package_name == NULL)
4858 package_name = this_package_name;
4859 else
4860 {
4861 if (strcmp (package_name, this_package_name) != 0)
4862 complaint (&symfile_complaints,
4863 _("Symtab %s has objects from two different Go packages: %s and %s"),
4864 (sym->symtab && sym->symtab->filename
4865 ? sym->symtab->filename
4866 : cu->objfile->name),
4867 this_package_name, package_name);
4868 xfree (this_package_name);
4869 }
4870 }
4871 }
4872 }
4873
4874 if (package_name != NULL)
4875 {
4876 struct objfile *objfile = cu->objfile;
4877 struct type *type = init_type (TYPE_CODE_MODULE, 0, 0,
4878 package_name, objfile);
4879 struct symbol *sym;
4880
4881 TYPE_TAG_NAME (type) = TYPE_NAME (type);
4882
4883 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
4884 SYMBOL_SET_LANGUAGE (sym, language_go);
4885 SYMBOL_SET_NAMES (sym, package_name, strlen (package_name), 1, objfile);
4886 /* This is not VAR_DOMAIN because we want a way to ensure a lookup of,
4887 e.g., "main" finds the "main" module and not C's main(). */
4888 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
4889 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
4890 SYMBOL_TYPE (sym) = type;
4891
4892 add_symbol_to_list (sym, &global_symbols);
4893
4894 xfree (package_name);
4895 }
4896 }
4897
4898 /* Generate full symbol information for PER_CU, whose DIEs have
4899 already been loaded into memory. */
4900
4901 static void
4902 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4903 {
4904 struct dwarf2_cu *cu = per_cu->cu;
4905 struct objfile *objfile = per_cu->objfile;
4906 CORE_ADDR lowpc, highpc;
4907 struct symtab *symtab;
4908 struct cleanup *back_to, *delayed_list_cleanup;
4909 CORE_ADDR baseaddr;
4910
4911 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4912
4913 buildsym_init ();
4914 back_to = make_cleanup (really_free_pendings, NULL);
4915 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4916
4917 cu->list_in_scope = &file_symbols;
4918
4919 /* Do line number decoding in read_file_scope () */
4920 process_die (cu->dies, cu);
4921
4922 /* For now fudge the Go package. */
4923 if (cu->language == language_go)
4924 fixup_go_packaging (cu);
4925
4926 /* Now that we have processed all the DIEs in the CU, all the types
4927 should be complete, and it should now be safe to compute all of the
4928 physnames. */
4929 compute_delayed_physnames (cu);
4930 do_cleanups (delayed_list_cleanup);
4931
4932 /* Some compilers don't define a DW_AT_high_pc attribute for the
4933 compilation unit. If the DW_AT_high_pc is missing, synthesize
4934 it, by scanning the DIE's below the compilation unit. */
4935 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4936
4937 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4938
4939 if (symtab != NULL)
4940 {
4941 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4942
4943 /* Set symtab language to language from DW_AT_language. If the
4944 compilation is from a C file generated by language preprocessors, do
4945 not set the language if it was already deduced by start_subfile. */
4946 if (!(cu->language == language_c && symtab->language != language_c))
4947 symtab->language = cu->language;
4948
4949 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4950 produce DW_AT_location with location lists but it can be possibly
4951 invalid without -fvar-tracking. Still up to GCC-4.4.x incl. 4.4.0
4952 there were bugs in prologue debug info, fixed later in GCC-4.5
4953 by "unwind info for epilogues" patch (which is not directly related).
4954
4955 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4956 needed, it would be wrong due to missing DW_AT_producer there.
4957
4958 Still one can confuse GDB by using non-standard GCC compilation
4959 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4960 */
4961 if (cu->has_loclist && gcc_4_minor >= 5)
4962 symtab->locations_valid = 1;
4963
4964 if (gcc_4_minor >= 5)
4965 symtab->epilogue_unwind_valid = 1;
4966
4967 symtab->call_site_htab = cu->call_site_htab;
4968 }
4969
4970 if (dwarf2_per_objfile->using_index)
4971 per_cu->v.quick->symtab = symtab;
4972 else
4973 {
4974 struct partial_symtab *pst = per_cu->v.psymtab;
4975 pst->symtab = symtab;
4976 pst->readin = 1;
4977 }
4978
4979 do_cleanups (back_to);
4980 }
4981
4982 /* Process a die and its children. */
4983
4984 static void
4985 process_die (struct die_info *die, struct dwarf2_cu *cu)
4986 {
4987 switch (die->tag)
4988 {
4989 case DW_TAG_padding:
4990 break;
4991 case DW_TAG_compile_unit:
4992 read_file_scope (die, cu);
4993 break;
4994 case DW_TAG_type_unit:
4995 read_type_unit_scope (die, cu);
4996 break;
4997 case DW_TAG_subprogram:
4998 case DW_TAG_inlined_subroutine:
4999 read_func_scope (die, cu);
5000 break;
5001 case DW_TAG_lexical_block:
5002 case DW_TAG_try_block:
5003 case DW_TAG_catch_block:
5004 read_lexical_block_scope (die, cu);
5005 break;
5006 case DW_TAG_GNU_call_site:
5007 read_call_site_scope (die, cu);
5008 break;
5009 case DW_TAG_class_type:
5010 case DW_TAG_interface_type:
5011 case DW_TAG_structure_type:
5012 case DW_TAG_union_type:
5013 process_structure_scope (die, cu);
5014 break;
5015 case DW_TAG_enumeration_type:
5016 process_enumeration_scope (die, cu);
5017 break;
5018
5019 /* These dies have a type, but processing them does not create
5020 a symbol or recurse to process the children. Therefore we can
5021 read them on-demand through read_type_die. */
5022 case DW_TAG_subroutine_type:
5023 case DW_TAG_set_type:
5024 case DW_TAG_array_type:
5025 case DW_TAG_pointer_type:
5026 case DW_TAG_ptr_to_member_type:
5027 case DW_TAG_reference_type:
5028 case DW_TAG_string_type:
5029 break;
5030
5031 case DW_TAG_base_type:
5032 case DW_TAG_subrange_type:
5033 case DW_TAG_typedef:
5034 /* Add a typedef symbol for the type definition, if it has a
5035 DW_AT_name. */
5036 new_symbol (die, read_type_die (die, cu), cu);
5037 break;
5038 case DW_TAG_common_block:
5039 read_common_block (die, cu);
5040 break;
5041 case DW_TAG_common_inclusion:
5042 break;
5043 case DW_TAG_namespace:
5044 processing_has_namespace_info = 1;
5045 read_namespace (die, cu);
5046 break;
5047 case DW_TAG_module:
5048 processing_has_namespace_info = 1;
5049 read_module (die, cu);
5050 break;
5051 case DW_TAG_imported_declaration:
5052 case DW_TAG_imported_module:
5053 processing_has_namespace_info = 1;
5054 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
5055 || cu->language != language_fortran))
5056 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
5057 dwarf_tag_name (die->tag));
5058 read_import_statement (die, cu);
5059 break;
5060 default:
5061 new_symbol (die, NULL, cu);
5062 break;
5063 }
5064 }
5065
5066 /* A helper function for dwarf2_compute_name which determines whether DIE
5067 needs to have the name of the scope prepended to the name listed in the
5068 die. */
5069
5070 static int
5071 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
5072 {
5073 struct attribute *attr;
5074
5075 switch (die->tag)
5076 {
5077 case DW_TAG_namespace:
5078 case DW_TAG_typedef:
5079 case DW_TAG_class_type:
5080 case DW_TAG_interface_type:
5081 case DW_TAG_structure_type:
5082 case DW_TAG_union_type:
5083 case DW_TAG_enumeration_type:
5084 case DW_TAG_enumerator:
5085 case DW_TAG_subprogram:
5086 case DW_TAG_member:
5087 return 1;
5088
5089 case DW_TAG_variable:
5090 case DW_TAG_constant:
5091 /* We only need to prefix "globally" visible variables. These include
5092 any variable marked with DW_AT_external or any variable that
5093 lives in a namespace. [Variables in anonymous namespaces
5094 require prefixing, but they are not DW_AT_external.] */
5095
5096 if (dwarf2_attr (die, DW_AT_specification, cu))
5097 {
5098 struct dwarf2_cu *spec_cu = cu;
5099
5100 return die_needs_namespace (die_specification (die, &spec_cu),
5101 spec_cu);
5102 }
5103
5104 attr = dwarf2_attr (die, DW_AT_external, cu);
5105 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5106 && die->parent->tag != DW_TAG_module)
5107 return 0;
5108 /* A variable in a lexical block of some kind does not need a
5109 namespace, even though in C++ such variables may be external
5110 and have a mangled name. */
5111 if (die->parent->tag == DW_TAG_lexical_block
5112 || die->parent->tag == DW_TAG_try_block
5113 || die->parent->tag == DW_TAG_catch_block
5114 || die->parent->tag == DW_TAG_subprogram)
5115 return 0;
5116 return 1;
5117
5118 default:
5119 return 0;
5120 }
5121 }
5122
5123 /* Retrieve the last character from a mem_file. */
5124
5125 static void
5126 do_ui_file_peek_last (void *object, const char *buffer, long length)
5127 {
5128 char *last_char_p = (char *) object;
5129
5130 if (length > 0)
5131 *last_char_p = buffer[length - 1];
5132 }
5133
5134 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5135 compute the physname for the object, which include a method's:
5136 - formal parameters (C++/Java),
5137 - receiver type (Go),
5138 - return type (Java).
5139
5140 The term "physname" is a bit confusing.
5141 For C++, for example, it is the demangled name.
5142 For Go, for example, it's the mangled name.
5143
5144 For Ada, return the DIE's linkage name rather than the fully qualified
5145 name. PHYSNAME is ignored..
5146
5147 The result is allocated on the objfile_obstack and canonicalized. */
5148
5149 static const char *
5150 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5151 int physname)
5152 {
5153 struct objfile *objfile = cu->objfile;
5154
5155 if (name == NULL)
5156 name = dwarf2_name (die, cu);
5157
5158 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5159 compute it by typename_concat inside GDB. */
5160 if (cu->language == language_ada
5161 || (cu->language == language_fortran && physname))
5162 {
5163 /* For Ada unit, we prefer the linkage name over the name, as
5164 the former contains the exported name, which the user expects
5165 to be able to reference. Ideally, we want the user to be able
5166 to reference this entity using either natural or linkage name,
5167 but we haven't started looking at this enhancement yet. */
5168 struct attribute *attr;
5169
5170 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5171 if (attr == NULL)
5172 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5173 if (attr && DW_STRING (attr))
5174 return DW_STRING (attr);
5175 }
5176
5177 /* These are the only languages we know how to qualify names in. */
5178 if (name != NULL
5179 && (cu->language == language_cplus || cu->language == language_java
5180 || cu->language == language_fortran))
5181 {
5182 if (die_needs_namespace (die, cu))
5183 {
5184 long length;
5185 const char *prefix;
5186 struct ui_file *buf;
5187
5188 prefix = determine_prefix (die, cu);
5189 buf = mem_fileopen ();
5190 if (*prefix != '\0')
5191 {
5192 char *prefixed_name = typename_concat (NULL, prefix, name,
5193 physname, cu);
5194
5195 fputs_unfiltered (prefixed_name, buf);
5196 xfree (prefixed_name);
5197 }
5198 else
5199 fputs_unfiltered (name, buf);
5200
5201 /* Template parameters may be specified in the DIE's DW_AT_name, or
5202 as children with DW_TAG_template_type_param or
5203 DW_TAG_value_type_param. If the latter, add them to the name
5204 here. If the name already has template parameters, then
5205 skip this step; some versions of GCC emit both, and
5206 it is more efficient to use the pre-computed name.
5207
5208 Something to keep in mind about this process: it is very
5209 unlikely, or in some cases downright impossible, to produce
5210 something that will match the mangled name of a function.
5211 If the definition of the function has the same debug info,
5212 we should be able to match up with it anyway. But fallbacks
5213 using the minimal symbol, for instance to find a method
5214 implemented in a stripped copy of libstdc++, will not work.
5215 If we do not have debug info for the definition, we will have to
5216 match them up some other way.
5217
5218 When we do name matching there is a related problem with function
5219 templates; two instantiated function templates are allowed to
5220 differ only by their return types, which we do not add here. */
5221
5222 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5223 {
5224 struct attribute *attr;
5225 struct die_info *child;
5226 int first = 1;
5227
5228 die->building_fullname = 1;
5229
5230 for (child = die->child; child != NULL; child = child->sibling)
5231 {
5232 struct type *type;
5233 LONGEST value;
5234 gdb_byte *bytes;
5235 struct dwarf2_locexpr_baton *baton;
5236 struct value *v;
5237
5238 if (child->tag != DW_TAG_template_type_param
5239 && child->tag != DW_TAG_template_value_param)
5240 continue;
5241
5242 if (first)
5243 {
5244 fputs_unfiltered ("<", buf);
5245 first = 0;
5246 }
5247 else
5248 fputs_unfiltered (", ", buf);
5249
5250 attr = dwarf2_attr (child, DW_AT_type, cu);
5251 if (attr == NULL)
5252 {
5253 complaint (&symfile_complaints,
5254 _("template parameter missing DW_AT_type"));
5255 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5256 continue;
5257 }
5258 type = die_type (child, cu);
5259
5260 if (child->tag == DW_TAG_template_type_param)
5261 {
5262 c_print_type (type, "", buf, -1, 0);
5263 continue;
5264 }
5265
5266 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5267 if (attr == NULL)
5268 {
5269 complaint (&symfile_complaints,
5270 _("template parameter missing "
5271 "DW_AT_const_value"));
5272 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5273 continue;
5274 }
5275
5276 dwarf2_const_value_attr (attr, type, name,
5277 &cu->comp_unit_obstack, cu,
5278 &value, &bytes, &baton);
5279
5280 if (TYPE_NOSIGN (type))
5281 /* GDB prints characters as NUMBER 'CHAR'. If that's
5282 changed, this can use value_print instead. */
5283 c_printchar (value, type, buf);
5284 else
5285 {
5286 struct value_print_options opts;
5287
5288 if (baton != NULL)
5289 v = dwarf2_evaluate_loc_desc (type, NULL,
5290 baton->data,
5291 baton->size,
5292 baton->per_cu);
5293 else if (bytes != NULL)
5294 {
5295 v = allocate_value (type);
5296 memcpy (value_contents_writeable (v), bytes,
5297 TYPE_LENGTH (type));
5298 }
5299 else
5300 v = value_from_longest (type, value);
5301
5302 /* Specify decimal so that we do not depend on
5303 the radix. */
5304 get_formatted_print_options (&opts, 'd');
5305 opts.raw = 1;
5306 value_print (v, buf, &opts);
5307 release_value (v);
5308 value_free (v);
5309 }
5310 }
5311
5312 die->building_fullname = 0;
5313
5314 if (!first)
5315 {
5316 /* Close the argument list, with a space if necessary
5317 (nested templates). */
5318 char last_char = '\0';
5319 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5320 if (last_char == '>')
5321 fputs_unfiltered (" >", buf);
5322 else
5323 fputs_unfiltered (">", buf);
5324 }
5325 }
5326
5327 /* For Java and C++ methods, append formal parameter type
5328 information, if PHYSNAME. */
5329
5330 if (physname && die->tag == DW_TAG_subprogram
5331 && (cu->language == language_cplus
5332 || cu->language == language_java))
5333 {
5334 struct type *type = read_type_die (die, cu);
5335
5336 c_type_print_args (type, buf, 1, cu->language);
5337
5338 if (cu->language == language_java)
5339 {
5340 /* For java, we must append the return type to method
5341 names. */
5342 if (die->tag == DW_TAG_subprogram)
5343 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5344 0, 0);
5345 }
5346 else if (cu->language == language_cplus)
5347 {
5348 /* Assume that an artificial first parameter is
5349 "this", but do not crash if it is not. RealView
5350 marks unnamed (and thus unused) parameters as
5351 artificial; there is no way to differentiate
5352 the two cases. */
5353 if (TYPE_NFIELDS (type) > 0
5354 && TYPE_FIELD_ARTIFICIAL (type, 0)
5355 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5356 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5357 0))))
5358 fputs_unfiltered (" const", buf);
5359 }
5360 }
5361
5362 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5363 &length);
5364 ui_file_delete (buf);
5365
5366 if (cu->language == language_cplus)
5367 {
5368 char *cname
5369 = dwarf2_canonicalize_name (name, cu,
5370 &objfile->objfile_obstack);
5371
5372 if (cname != NULL)
5373 name = cname;
5374 }
5375 }
5376 }
5377
5378 return name;
5379 }
5380
5381 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5382 If scope qualifiers are appropriate they will be added. The result
5383 will be allocated on the objfile_obstack, or NULL if the DIE does
5384 not have a name. NAME may either be from a previous call to
5385 dwarf2_name or NULL.
5386
5387 The output string will be canonicalized (if C++/Java). */
5388
5389 static const char *
5390 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5391 {
5392 return dwarf2_compute_name (name, die, cu, 0);
5393 }
5394
5395 /* Construct a physname for the given DIE in CU. NAME may either be
5396 from a previous call to dwarf2_name or NULL. The result will be
5397 allocated on the objfile_objstack or NULL if the DIE does not have a
5398 name.
5399
5400 The output string will be canonicalized (if C++/Java). */
5401
5402 static const char *
5403 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5404 {
5405 struct objfile *objfile = cu->objfile;
5406 struct attribute *attr;
5407 const char *retval, *mangled = NULL, *canon = NULL;
5408 struct cleanup *back_to;
5409 int need_copy = 1;
5410
5411 /* In this case dwarf2_compute_name is just a shortcut not building anything
5412 on its own. */
5413 if (!die_needs_namespace (die, cu))
5414 return dwarf2_compute_name (name, die, cu, 1);
5415
5416 back_to = make_cleanup (null_cleanup, NULL);
5417
5418 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5419 if (!attr)
5420 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5421
5422 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5423 has computed. */
5424 if (attr && DW_STRING (attr))
5425 {
5426 char *demangled;
5427
5428 mangled = DW_STRING (attr);
5429
5430 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5431 type. It is easier for GDB users to search for such functions as
5432 `name(params)' than `long name(params)'. In such case the minimal
5433 symbol names do not match the full symbol names but for template
5434 functions there is never a need to look up their definition from their
5435 declaration so the only disadvantage remains the minimal symbol
5436 variant `long name(params)' does not have the proper inferior type.
5437 */
5438
5439 if (cu->language == language_go)
5440 {
5441 /* This is a lie, but we already lie to the caller new_symbol_full.
5442 new_symbol_full assumes we return the mangled name.
5443 This just undoes that lie until things are cleaned up. */
5444 demangled = NULL;
5445 }
5446 else
5447 {
5448 demangled = cplus_demangle (mangled,
5449 (DMGL_PARAMS | DMGL_ANSI
5450 | (cu->language == language_java
5451 ? DMGL_JAVA | DMGL_RET_POSTFIX
5452 : DMGL_RET_DROP)));
5453 }
5454 if (demangled)
5455 {
5456 make_cleanup (xfree, demangled);
5457 canon = demangled;
5458 }
5459 else
5460 {
5461 canon = mangled;
5462 need_copy = 0;
5463 }
5464 }
5465
5466 if (canon == NULL || check_physname)
5467 {
5468 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5469
5470 if (canon != NULL && strcmp (physname, canon) != 0)
5471 {
5472 /* It may not mean a bug in GDB. The compiler could also
5473 compute DW_AT_linkage_name incorrectly. But in such case
5474 GDB would need to be bug-to-bug compatible. */
5475
5476 complaint (&symfile_complaints,
5477 _("Computed physname <%s> does not match demangled <%s> "
5478 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5479 physname, canon, mangled, die->offset.sect_off, objfile->name);
5480
5481 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5482 is available here - over computed PHYSNAME. It is safer
5483 against both buggy GDB and buggy compilers. */
5484
5485 retval = canon;
5486 }
5487 else
5488 {
5489 retval = physname;
5490 need_copy = 0;
5491 }
5492 }
5493 else
5494 retval = canon;
5495
5496 if (need_copy)
5497 retval = obsavestring (retval, strlen (retval),
5498 &objfile->objfile_obstack);
5499
5500 do_cleanups (back_to);
5501 return retval;
5502 }
5503
5504 /* Read the import statement specified by the given die and record it. */
5505
5506 static void
5507 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5508 {
5509 struct objfile *objfile = cu->objfile;
5510 struct attribute *import_attr;
5511 struct die_info *imported_die, *child_die;
5512 struct dwarf2_cu *imported_cu;
5513 const char *imported_name;
5514 const char *imported_name_prefix;
5515 const char *canonical_name;
5516 const char *import_alias;
5517 const char *imported_declaration = NULL;
5518 const char *import_prefix;
5519 VEC (const_char_ptr) *excludes = NULL;
5520 struct cleanup *cleanups;
5521
5522 char *temp;
5523
5524 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5525 if (import_attr == NULL)
5526 {
5527 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5528 dwarf_tag_name (die->tag));
5529 return;
5530 }
5531
5532 imported_cu = cu;
5533 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5534 imported_name = dwarf2_name (imported_die, imported_cu);
5535 if (imported_name == NULL)
5536 {
5537 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5538
5539 The import in the following code:
5540 namespace A
5541 {
5542 typedef int B;
5543 }
5544
5545 int main ()
5546 {
5547 using A::B;
5548 B b;
5549 return b;
5550 }
5551
5552 ...
5553 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5554 <52> DW_AT_decl_file : 1
5555 <53> DW_AT_decl_line : 6
5556 <54> DW_AT_import : <0x75>
5557 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5558 <59> DW_AT_name : B
5559 <5b> DW_AT_decl_file : 1
5560 <5c> DW_AT_decl_line : 2
5561 <5d> DW_AT_type : <0x6e>
5562 ...
5563 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5564 <76> DW_AT_byte_size : 4
5565 <77> DW_AT_encoding : 5 (signed)
5566
5567 imports the wrong die ( 0x75 instead of 0x58 ).
5568 This case will be ignored until the gcc bug is fixed. */
5569 return;
5570 }
5571
5572 /* Figure out the local name after import. */
5573 import_alias = dwarf2_name (die, cu);
5574
5575 /* Figure out where the statement is being imported to. */
5576 import_prefix = determine_prefix (die, cu);
5577
5578 /* Figure out what the scope of the imported die is and prepend it
5579 to the name of the imported die. */
5580 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5581
5582 if (imported_die->tag != DW_TAG_namespace
5583 && imported_die->tag != DW_TAG_module)
5584 {
5585 imported_declaration = imported_name;
5586 canonical_name = imported_name_prefix;
5587 }
5588 else if (strlen (imported_name_prefix) > 0)
5589 {
5590 temp = alloca (strlen (imported_name_prefix)
5591 + 2 + strlen (imported_name) + 1);
5592 strcpy (temp, imported_name_prefix);
5593 strcat (temp, "::");
5594 strcat (temp, imported_name);
5595 canonical_name = temp;
5596 }
5597 else
5598 canonical_name = imported_name;
5599
5600 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5601
5602 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5603 for (child_die = die->child; child_die && child_die->tag;
5604 child_die = sibling_die (child_die))
5605 {
5606 /* DWARF-4: A Fortran use statement with a “rename list” may be
5607 represented by an imported module entry with an import attribute
5608 referring to the module and owned entries corresponding to those
5609 entities that are renamed as part of being imported. */
5610
5611 if (child_die->tag != DW_TAG_imported_declaration)
5612 {
5613 complaint (&symfile_complaints,
5614 _("child DW_TAG_imported_declaration expected "
5615 "- DIE at 0x%x [in module %s]"),
5616 child_die->offset.sect_off, objfile->name);
5617 continue;
5618 }
5619
5620 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5621 if (import_attr == NULL)
5622 {
5623 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5624 dwarf_tag_name (child_die->tag));
5625 continue;
5626 }
5627
5628 imported_cu = cu;
5629 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5630 &imported_cu);
5631 imported_name = dwarf2_name (imported_die, imported_cu);
5632 if (imported_name == NULL)
5633 {
5634 complaint (&symfile_complaints,
5635 _("child DW_TAG_imported_declaration has unknown "
5636 "imported name - DIE at 0x%x [in module %s]"),
5637 child_die->offset.sect_off, objfile->name);
5638 continue;
5639 }
5640
5641 VEC_safe_push (const_char_ptr, excludes, imported_name);
5642
5643 process_die (child_die, cu);
5644 }
5645
5646 cp_add_using_directive (import_prefix,
5647 canonical_name,
5648 import_alias,
5649 imported_declaration,
5650 excludes,
5651 &objfile->objfile_obstack);
5652
5653 do_cleanups (cleanups);
5654 }
5655
5656 /* Cleanup function for read_file_scope. */
5657
5658 static void
5659 free_cu_line_header (void *arg)
5660 {
5661 struct dwarf2_cu *cu = arg;
5662
5663 free_line_header (cu->line_header);
5664 cu->line_header = NULL;
5665 }
5666
5667 static void
5668 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5669 char **name, char **comp_dir)
5670 {
5671 struct attribute *attr;
5672
5673 *name = NULL;
5674 *comp_dir = NULL;
5675
5676 /* Find the filename. Do not use dwarf2_name here, since the filename
5677 is not a source language identifier. */
5678 attr = dwarf2_attr (die, DW_AT_name, cu);
5679 if (attr)
5680 {
5681 *name = DW_STRING (attr);
5682 }
5683
5684 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5685 if (attr)
5686 *comp_dir = DW_STRING (attr);
5687 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5688 {
5689 *comp_dir = ldirname (*name);
5690 if (*comp_dir != NULL)
5691 make_cleanup (xfree, *comp_dir);
5692 }
5693 if (*comp_dir != NULL)
5694 {
5695 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5696 directory, get rid of it. */
5697 char *cp = strchr (*comp_dir, ':');
5698
5699 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5700 *comp_dir = cp + 1;
5701 }
5702
5703 if (*name == NULL)
5704 *name = "<unknown>";
5705 }
5706
5707 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
5708 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5709 COMP_DIR is the compilation directory.
5710 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
5711
5712 static void
5713 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5714 const char *comp_dir, int want_line_info)
5715 {
5716 struct attribute *attr;
5717 struct objfile *objfile = cu->objfile;
5718 bfd *abfd = objfile->obfd;
5719
5720 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5721 if (attr)
5722 {
5723 unsigned int line_offset = DW_UNSND (attr);
5724 struct line_header *line_header
5725 = dwarf_decode_line_header (line_offset, abfd, cu);
5726
5727 if (line_header)
5728 {
5729 cu->line_header = line_header;
5730 make_cleanup (free_cu_line_header, cu);
5731 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
5732 }
5733 }
5734 }
5735
5736 /* Process DW_TAG_compile_unit. */
5737
5738 static void
5739 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5740 {
5741 struct objfile *objfile = cu->objfile;
5742 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5743 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5744 CORE_ADDR highpc = ((CORE_ADDR) 0);
5745 struct attribute *attr;
5746 char *name = NULL;
5747 char *comp_dir = NULL;
5748 struct die_info *child_die;
5749 bfd *abfd = objfile->obfd;
5750 CORE_ADDR baseaddr;
5751
5752 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5753
5754 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5755
5756 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5757 from finish_block. */
5758 if (lowpc == ((CORE_ADDR) -1))
5759 lowpc = highpc;
5760 lowpc += baseaddr;
5761 highpc += baseaddr;
5762
5763 find_file_and_directory (die, cu, &name, &comp_dir);
5764
5765 attr = dwarf2_attr (die, DW_AT_language, cu);
5766 if (attr)
5767 {
5768 set_cu_language (DW_UNSND (attr), cu);
5769 }
5770
5771 attr = dwarf2_attr (die, DW_AT_producer, cu);
5772 if (attr)
5773 cu->producer = DW_STRING (attr);
5774
5775 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5776 standardised yet. As a workaround for the language detection we fall
5777 back to the DW_AT_producer string. */
5778 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5779 cu->language = language_opencl;
5780
5781 /* Similar hack for Go. */
5782 if (cu->producer && strstr (cu->producer, "GNU Go ") != NULL)
5783 set_cu_language (DW_LANG_Go, cu);
5784
5785 /* We assume that we're processing GCC output. */
5786 processing_gcc_compilation = 2;
5787
5788 processing_has_namespace_info = 0;
5789
5790 start_symtab (name, comp_dir, lowpc);
5791 record_debugformat ("DWARF 2");
5792 record_producer (cu->producer);
5793
5794 /* Decode line number information if present. We do this before
5795 processing child DIEs, so that the line header table is available
5796 for DW_AT_decl_file. */
5797 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
5798
5799 /* Process all dies in compilation unit. */
5800 if (die->child != NULL)
5801 {
5802 child_die = die->child;
5803 while (child_die && child_die->tag)
5804 {
5805 process_die (child_die, cu);
5806 child_die = sibling_die (child_die);
5807 }
5808 }
5809
5810 /* Decode macro information, if present. Dwarf 2 macro information
5811 refers to information in the line number info statement program
5812 header, so we can only read it if we've read the header
5813 successfully. */
5814 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5815 if (attr && cu->line_header)
5816 {
5817 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5818 complaint (&symfile_complaints,
5819 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5820
5821 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5822 comp_dir, abfd, cu,
5823 &dwarf2_per_objfile->macro, 1);
5824 }
5825 else
5826 {
5827 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5828 if (attr && cu->line_header)
5829 {
5830 unsigned int macro_offset = DW_UNSND (attr);
5831
5832 dwarf_decode_macros (cu->line_header, macro_offset,
5833 comp_dir, abfd, cu,
5834 &dwarf2_per_objfile->macinfo, 0);
5835 }
5836 }
5837
5838 do_cleanups (back_to);
5839 }
5840
5841 /* Process DW_TAG_type_unit.
5842 For TUs we want to skip the first top level sibling if it's not the
5843 actual type being defined by this TU. In this case the first top
5844 level sibling is there to provide context only. */
5845
5846 static void
5847 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5848 {
5849 struct objfile *objfile = cu->objfile;
5850 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5851 CORE_ADDR lowpc;
5852 struct attribute *attr;
5853 char *name = NULL;
5854 char *comp_dir = NULL;
5855 struct die_info *child_die;
5856 bfd *abfd = objfile->obfd;
5857
5858 /* start_symtab needs a low pc, but we don't really have one.
5859 Do what read_file_scope would do in the absence of such info. */
5860 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5861
5862 /* Find the filename. Do not use dwarf2_name here, since the filename
5863 is not a source language identifier. */
5864 attr = dwarf2_attr (die, DW_AT_name, cu);
5865 if (attr)
5866 name = DW_STRING (attr);
5867
5868 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5869 if (attr)
5870 comp_dir = DW_STRING (attr);
5871 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5872 {
5873 comp_dir = ldirname (name);
5874 if (comp_dir != NULL)
5875 make_cleanup (xfree, comp_dir);
5876 }
5877
5878 if (name == NULL)
5879 name = "<unknown>";
5880
5881 attr = dwarf2_attr (die, DW_AT_language, cu);
5882 if (attr)
5883 set_cu_language (DW_UNSND (attr), cu);
5884
5885 /* This isn't technically needed today. It is done for symmetry
5886 with read_file_scope. */
5887 attr = dwarf2_attr (die, DW_AT_producer, cu);
5888 if (attr)
5889 cu->producer = DW_STRING (attr);
5890
5891 /* We assume that we're processing GCC output. */
5892 processing_gcc_compilation = 2;
5893
5894 processing_has_namespace_info = 0;
5895
5896 start_symtab (name, comp_dir, lowpc);
5897 record_debugformat ("DWARF 2");
5898 record_producer (cu->producer);
5899
5900 /* Decode line number information if present. We do this before
5901 processing child DIEs, so that the line header table is available
5902 for DW_AT_decl_file.
5903 We don't need the pc/line-number mapping for type units. */
5904 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
5905
5906 /* Process the dies in the type unit. */
5907 if (die->child == NULL)
5908 {
5909 dump_die_for_error (die);
5910 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5911 bfd_get_filename (abfd));
5912 }
5913
5914 child_die = die->child;
5915
5916 while (child_die && child_die->tag)
5917 {
5918 process_die (child_die, cu);
5919
5920 child_die = sibling_die (child_die);
5921 }
5922
5923 do_cleanups (back_to);
5924 }
5925
5926 /* qsort helper for inherit_abstract_dies. */
5927
5928 static int
5929 unsigned_int_compar (const void *ap, const void *bp)
5930 {
5931 unsigned int a = *(unsigned int *) ap;
5932 unsigned int b = *(unsigned int *) bp;
5933
5934 return (a > b) - (b > a);
5935 }
5936
5937 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5938 Inherit only the children of the DW_AT_abstract_origin DIE not being
5939 already referenced by DW_AT_abstract_origin from the children of the
5940 current DIE. */
5941
5942 static void
5943 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5944 {
5945 struct die_info *child_die;
5946 unsigned die_children_count;
5947 /* CU offsets which were referenced by children of the current DIE. */
5948 sect_offset *offsets;
5949 sect_offset *offsets_end, *offsetp;
5950 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5951 struct die_info *origin_die;
5952 /* Iterator of the ORIGIN_DIE children. */
5953 struct die_info *origin_child_die;
5954 struct cleanup *cleanups;
5955 struct attribute *attr;
5956 struct dwarf2_cu *origin_cu;
5957 struct pending **origin_previous_list_in_scope;
5958
5959 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5960 if (!attr)
5961 return;
5962
5963 /* Note that following die references may follow to a die in a
5964 different cu. */
5965
5966 origin_cu = cu;
5967 origin_die = follow_die_ref (die, attr, &origin_cu);
5968
5969 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5970 symbols in. */
5971 origin_previous_list_in_scope = origin_cu->list_in_scope;
5972 origin_cu->list_in_scope = cu->list_in_scope;
5973
5974 if (die->tag != origin_die->tag
5975 && !(die->tag == DW_TAG_inlined_subroutine
5976 && origin_die->tag == DW_TAG_subprogram))
5977 complaint (&symfile_complaints,
5978 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5979 die->offset.sect_off, origin_die->offset.sect_off);
5980
5981 child_die = die->child;
5982 die_children_count = 0;
5983 while (child_die && child_die->tag)
5984 {
5985 child_die = sibling_die (child_die);
5986 die_children_count++;
5987 }
5988 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5989 cleanups = make_cleanup (xfree, offsets);
5990
5991 offsets_end = offsets;
5992 child_die = die->child;
5993 while (child_die && child_die->tag)
5994 {
5995 /* For each CHILD_DIE, find the corresponding child of
5996 ORIGIN_DIE. If there is more than one layer of
5997 DW_AT_abstract_origin, follow them all; there shouldn't be,
5998 but GCC versions at least through 4.4 generate this (GCC PR
5999 40573). */
6000 struct die_info *child_origin_die = child_die;
6001 struct dwarf2_cu *child_origin_cu = cu;
6002
6003 while (1)
6004 {
6005 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
6006 child_origin_cu);
6007 if (attr == NULL)
6008 break;
6009 child_origin_die = follow_die_ref (child_origin_die, attr,
6010 &child_origin_cu);
6011 }
6012
6013 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
6014 counterpart may exist. */
6015 if (child_origin_die != child_die)
6016 {
6017 if (child_die->tag != child_origin_die->tag
6018 && !(child_die->tag == DW_TAG_inlined_subroutine
6019 && child_origin_die->tag == DW_TAG_subprogram))
6020 complaint (&symfile_complaints,
6021 _("Child DIE 0x%x and its abstract origin 0x%x have "
6022 "different tags"), child_die->offset.sect_off,
6023 child_origin_die->offset.sect_off);
6024 if (child_origin_die->parent != origin_die)
6025 complaint (&symfile_complaints,
6026 _("Child DIE 0x%x and its abstract origin 0x%x have "
6027 "different parents"), child_die->offset.sect_off,
6028 child_origin_die->offset.sect_off);
6029 else
6030 *offsets_end++ = child_origin_die->offset;
6031 }
6032 child_die = sibling_die (child_die);
6033 }
6034 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
6035 unsigned_int_compar);
6036 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
6037 if (offsetp[-1].sect_off == offsetp->sect_off)
6038 complaint (&symfile_complaints,
6039 _("Multiple children of DIE 0x%x refer "
6040 "to DIE 0x%x as their abstract origin"),
6041 die->offset.sect_off, offsetp->sect_off);
6042
6043 offsetp = offsets;
6044 origin_child_die = origin_die->child;
6045 while (origin_child_die && origin_child_die->tag)
6046 {
6047 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
6048 while (offsetp < offsets_end
6049 && offsetp->sect_off < origin_child_die->offset.sect_off)
6050 offsetp++;
6051 if (offsetp >= offsets_end
6052 || offsetp->sect_off > origin_child_die->offset.sect_off)
6053 {
6054 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
6055 process_die (origin_child_die, origin_cu);
6056 }
6057 origin_child_die = sibling_die (origin_child_die);
6058 }
6059 origin_cu->list_in_scope = origin_previous_list_in_scope;
6060
6061 do_cleanups (cleanups);
6062 }
6063
6064 static void
6065 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
6066 {
6067 struct objfile *objfile = cu->objfile;
6068 struct context_stack *new;
6069 CORE_ADDR lowpc;
6070 CORE_ADDR highpc;
6071 struct die_info *child_die;
6072 struct attribute *attr, *call_line, *call_file;
6073 char *name;
6074 CORE_ADDR baseaddr;
6075 struct block *block;
6076 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
6077 VEC (symbolp) *template_args = NULL;
6078 struct template_symbol *templ_func = NULL;
6079
6080 if (inlined_func)
6081 {
6082 /* If we do not have call site information, we can't show the
6083 caller of this inlined function. That's too confusing, so
6084 only use the scope for local variables. */
6085 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
6086 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
6087 if (call_line == NULL || call_file == NULL)
6088 {
6089 read_lexical_block_scope (die, cu);
6090 return;
6091 }
6092 }
6093
6094 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6095
6096 name = dwarf2_name (die, cu);
6097
6098 /* Ignore functions with missing or empty names. These are actually
6099 illegal according to the DWARF standard. */
6100 if (name == NULL)
6101 {
6102 complaint (&symfile_complaints,
6103 _("missing name for subprogram DIE at %d"),
6104 die->offset.sect_off);
6105 return;
6106 }
6107
6108 /* Ignore functions with missing or invalid low and high pc attributes. */
6109 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6110 {
6111 attr = dwarf2_attr (die, DW_AT_external, cu);
6112 if (!attr || !DW_UNSND (attr))
6113 complaint (&symfile_complaints,
6114 _("cannot get low and high bounds "
6115 "for subprogram DIE at %d"),
6116 die->offset.sect_off);
6117 return;
6118 }
6119
6120 lowpc += baseaddr;
6121 highpc += baseaddr;
6122
6123 /* If we have any template arguments, then we must allocate a
6124 different sort of symbol. */
6125 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6126 {
6127 if (child_die->tag == DW_TAG_template_type_param
6128 || child_die->tag == DW_TAG_template_value_param)
6129 {
6130 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6131 struct template_symbol);
6132 templ_func->base.is_cplus_template_function = 1;
6133 break;
6134 }
6135 }
6136
6137 new = push_context (0, lowpc);
6138 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6139 (struct symbol *) templ_func);
6140
6141 /* If there is a location expression for DW_AT_frame_base, record
6142 it. */
6143 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6144 if (attr)
6145 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6146 expression is being recorded directly in the function's symbol
6147 and not in a separate frame-base object. I guess this hack is
6148 to avoid adding some sort of frame-base adjunct/annex to the
6149 function's symbol :-(. The problem with doing this is that it
6150 results in a function symbol with a location expression that
6151 has nothing to do with the location of the function, ouch! The
6152 relationship should be: a function's symbol has-a frame base; a
6153 frame-base has-a location expression. */
6154 dwarf2_symbol_mark_computed (attr, new->name, cu);
6155
6156 cu->list_in_scope = &local_symbols;
6157
6158 if (die->child != NULL)
6159 {
6160 child_die = die->child;
6161 while (child_die && child_die->tag)
6162 {
6163 if (child_die->tag == DW_TAG_template_type_param
6164 || child_die->tag == DW_TAG_template_value_param)
6165 {
6166 struct symbol *arg = new_symbol (child_die, NULL, cu);
6167
6168 if (arg != NULL)
6169 VEC_safe_push (symbolp, template_args, arg);
6170 }
6171 else
6172 process_die (child_die, cu);
6173 child_die = sibling_die (child_die);
6174 }
6175 }
6176
6177 inherit_abstract_dies (die, cu);
6178
6179 /* If we have a DW_AT_specification, we might need to import using
6180 directives from the context of the specification DIE. See the
6181 comment in determine_prefix. */
6182 if (cu->language == language_cplus
6183 && dwarf2_attr (die, DW_AT_specification, cu))
6184 {
6185 struct dwarf2_cu *spec_cu = cu;
6186 struct die_info *spec_die = die_specification (die, &spec_cu);
6187
6188 while (spec_die)
6189 {
6190 child_die = spec_die->child;
6191 while (child_die && child_die->tag)
6192 {
6193 if (child_die->tag == DW_TAG_imported_module)
6194 process_die (child_die, spec_cu);
6195 child_die = sibling_die (child_die);
6196 }
6197
6198 /* In some cases, GCC generates specification DIEs that
6199 themselves contain DW_AT_specification attributes. */
6200 spec_die = die_specification (spec_die, &spec_cu);
6201 }
6202 }
6203
6204 new = pop_context ();
6205 /* Make a block for the local symbols within. */
6206 block = finish_block (new->name, &local_symbols, new->old_blocks,
6207 lowpc, highpc, objfile);
6208
6209 /* For C++, set the block's scope. */
6210 if (cu->language == language_cplus || cu->language == language_fortran)
6211 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6212 determine_prefix (die, cu),
6213 processing_has_namespace_info);
6214
6215 /* If we have address ranges, record them. */
6216 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6217
6218 /* Attach template arguments to function. */
6219 if (! VEC_empty (symbolp, template_args))
6220 {
6221 gdb_assert (templ_func != NULL);
6222
6223 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6224 templ_func->template_arguments
6225 = obstack_alloc (&objfile->objfile_obstack,
6226 (templ_func->n_template_arguments
6227 * sizeof (struct symbol *)));
6228 memcpy (templ_func->template_arguments,
6229 VEC_address (symbolp, template_args),
6230 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6231 VEC_free (symbolp, template_args);
6232 }
6233
6234 /* In C++, we can have functions nested inside functions (e.g., when
6235 a function declares a class that has methods). This means that
6236 when we finish processing a function scope, we may need to go
6237 back to building a containing block's symbol lists. */
6238 local_symbols = new->locals;
6239 param_symbols = new->params;
6240 using_directives = new->using_directives;
6241
6242 /* If we've finished processing a top-level function, subsequent
6243 symbols go in the file symbol list. */
6244 if (outermost_context_p ())
6245 cu->list_in_scope = &file_symbols;
6246 }
6247
6248 /* Process all the DIES contained within a lexical block scope. Start
6249 a new scope, process the dies, and then close the scope. */
6250
6251 static void
6252 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6253 {
6254 struct objfile *objfile = cu->objfile;
6255 struct context_stack *new;
6256 CORE_ADDR lowpc, highpc;
6257 struct die_info *child_die;
6258 CORE_ADDR baseaddr;
6259
6260 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6261
6262 /* Ignore blocks with missing or invalid low and high pc attributes. */
6263 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6264 as multiple lexical blocks? Handling children in a sane way would
6265 be nasty. Might be easier to properly extend generic blocks to
6266 describe ranges. */
6267 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6268 return;
6269 lowpc += baseaddr;
6270 highpc += baseaddr;
6271
6272 push_context (0, lowpc);
6273 if (die->child != NULL)
6274 {
6275 child_die = die->child;
6276 while (child_die && child_die->tag)
6277 {
6278 process_die (child_die, cu);
6279 child_die = sibling_die (child_die);
6280 }
6281 }
6282 new = pop_context ();
6283
6284 if (local_symbols != NULL || using_directives != NULL)
6285 {
6286 struct block *block
6287 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6288 highpc, objfile);
6289
6290 /* Note that recording ranges after traversing children, as we
6291 do here, means that recording a parent's ranges entails
6292 walking across all its children's ranges as they appear in
6293 the address map, which is quadratic behavior.
6294
6295 It would be nicer to record the parent's ranges before
6296 traversing its children, simply overriding whatever you find
6297 there. But since we don't even decide whether to create a
6298 block until after we've traversed its children, that's hard
6299 to do. */
6300 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6301 }
6302 local_symbols = new->locals;
6303 using_directives = new->using_directives;
6304 }
6305
6306 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6307
6308 static void
6309 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6310 {
6311 struct objfile *objfile = cu->objfile;
6312 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6313 CORE_ADDR pc, baseaddr;
6314 struct attribute *attr;
6315 struct call_site *call_site, call_site_local;
6316 void **slot;
6317 int nparams;
6318 struct die_info *child_die;
6319
6320 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6321
6322 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6323 if (!attr)
6324 {
6325 complaint (&symfile_complaints,
6326 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6327 "DIE 0x%x [in module %s]"),
6328 die->offset.sect_off, objfile->name);
6329 return;
6330 }
6331 pc = DW_ADDR (attr) + baseaddr;
6332
6333 if (cu->call_site_htab == NULL)
6334 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6335 NULL, &objfile->objfile_obstack,
6336 hashtab_obstack_allocate, NULL);
6337 call_site_local.pc = pc;
6338 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6339 if (*slot != NULL)
6340 {
6341 complaint (&symfile_complaints,
6342 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6343 "DIE 0x%x [in module %s]"),
6344 paddress (gdbarch, pc), die->offset.sect_off, objfile->name);
6345 return;
6346 }
6347
6348 /* Count parameters at the caller. */
6349
6350 nparams = 0;
6351 for (child_die = die->child; child_die && child_die->tag;
6352 child_die = sibling_die (child_die))
6353 {
6354 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6355 {
6356 complaint (&symfile_complaints,
6357 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6358 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6359 child_die->tag, child_die->offset.sect_off, objfile->name);
6360 continue;
6361 }
6362
6363 nparams++;
6364 }
6365
6366 call_site = obstack_alloc (&objfile->objfile_obstack,
6367 (sizeof (*call_site)
6368 + (sizeof (*call_site->parameter)
6369 * (nparams - 1))));
6370 *slot = call_site;
6371 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6372 call_site->pc = pc;
6373
6374 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6375 {
6376 struct die_info *func_die;
6377
6378 /* Skip also over DW_TAG_inlined_subroutine. */
6379 for (func_die = die->parent;
6380 func_die && func_die->tag != DW_TAG_subprogram
6381 && func_die->tag != DW_TAG_subroutine_type;
6382 func_die = func_die->parent);
6383
6384 /* DW_AT_GNU_all_call_sites is a superset
6385 of DW_AT_GNU_all_tail_call_sites. */
6386 if (func_die
6387 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6388 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6389 {
6390 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6391 not complete. But keep CALL_SITE for look ups via call_site_htab,
6392 both the initial caller containing the real return address PC and
6393 the final callee containing the current PC of a chain of tail
6394 calls do not need to have the tail call list complete. But any
6395 function candidate for a virtual tail call frame searched via
6396 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6397 determined unambiguously. */
6398 }
6399 else
6400 {
6401 struct type *func_type = NULL;
6402
6403 if (func_die)
6404 func_type = get_die_type (func_die, cu);
6405 if (func_type != NULL)
6406 {
6407 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6408
6409 /* Enlist this call site to the function. */
6410 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6411 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6412 }
6413 else
6414 complaint (&symfile_complaints,
6415 _("Cannot find function owning DW_TAG_GNU_call_site "
6416 "DIE 0x%x [in module %s]"),
6417 die->offset.sect_off, objfile->name);
6418 }
6419 }
6420
6421 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6422 if (attr == NULL)
6423 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6424 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6425 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6426 /* Keep NULL DWARF_BLOCK. */;
6427 else if (attr_form_is_block (attr))
6428 {
6429 struct dwarf2_locexpr_baton *dlbaton;
6430
6431 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6432 dlbaton->data = DW_BLOCK (attr)->data;
6433 dlbaton->size = DW_BLOCK (attr)->size;
6434 dlbaton->per_cu = cu->per_cu;
6435
6436 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6437 }
6438 else if (is_ref_attr (attr))
6439 {
6440 struct dwarf2_cu *target_cu = cu;
6441 struct die_info *target_die;
6442
6443 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6444 gdb_assert (target_cu->objfile == objfile);
6445 if (die_is_declaration (target_die, target_cu))
6446 {
6447 const char *target_physname;
6448
6449 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6450 if (target_physname == NULL)
6451 complaint (&symfile_complaints,
6452 _("DW_AT_GNU_call_site_target target DIE has invalid "
6453 "physname, for referencing DIE 0x%x [in module %s]"),
6454 die->offset.sect_off, objfile->name);
6455 else
6456 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6457 }
6458 else
6459 {
6460 CORE_ADDR lowpc;
6461
6462 /* DW_AT_entry_pc should be preferred. */
6463 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6464 complaint (&symfile_complaints,
6465 _("DW_AT_GNU_call_site_target target DIE has invalid "
6466 "low pc, for referencing DIE 0x%x [in module %s]"),
6467 die->offset.sect_off, objfile->name);
6468 else
6469 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6470 }
6471 }
6472 else
6473 complaint (&symfile_complaints,
6474 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6475 "block nor reference, for DIE 0x%x [in module %s]"),
6476 die->offset.sect_off, objfile->name);
6477
6478 call_site->per_cu = cu->per_cu;
6479
6480 for (child_die = die->child;
6481 child_die && child_die->tag;
6482 child_die = sibling_die (child_die))
6483 {
6484 struct dwarf2_locexpr_baton *dlbaton;
6485 struct call_site_parameter *parameter;
6486
6487 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6488 {
6489 /* Already printed the complaint above. */
6490 continue;
6491 }
6492
6493 gdb_assert (call_site->parameter_count < nparams);
6494 parameter = &call_site->parameter[call_site->parameter_count];
6495
6496 /* DW_AT_location specifies the register number. Value of the data
6497 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6498
6499 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6500 if (!attr || !attr_form_is_block (attr))
6501 {
6502 complaint (&symfile_complaints,
6503 _("No DW_FORM_block* DW_AT_location for "
6504 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6505 child_die->offset.sect_off, objfile->name);
6506 continue;
6507 }
6508 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6509 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6510 if (parameter->dwarf_reg == -1
6511 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6512 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6513 &parameter->fb_offset))
6514 {
6515 complaint (&symfile_complaints,
6516 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6517 "for DW_FORM_block* DW_AT_location for "
6518 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6519 child_die->offset.sect_off, objfile->name);
6520 continue;
6521 }
6522
6523 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6524 if (!attr_form_is_block (attr))
6525 {
6526 complaint (&symfile_complaints,
6527 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6528 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6529 child_die->offset.sect_off, objfile->name);
6530 continue;
6531 }
6532 parameter->value = DW_BLOCK (attr)->data;
6533 parameter->value_size = DW_BLOCK (attr)->size;
6534
6535 /* Parameters are not pre-cleared by memset above. */
6536 parameter->data_value = NULL;
6537 parameter->data_value_size = 0;
6538 call_site->parameter_count++;
6539
6540 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6541 if (attr)
6542 {
6543 if (!attr_form_is_block (attr))
6544 complaint (&symfile_complaints,
6545 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6546 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6547 child_die->offset.sect_off, objfile->name);
6548 else
6549 {
6550 parameter->data_value = DW_BLOCK (attr)->data;
6551 parameter->data_value_size = DW_BLOCK (attr)->size;
6552 }
6553 }
6554 }
6555 }
6556
6557 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6558 Return 1 if the attributes are present and valid, otherwise, return 0.
6559 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6560
6561 static int
6562 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6563 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6564 struct partial_symtab *ranges_pst)
6565 {
6566 struct objfile *objfile = cu->objfile;
6567 struct comp_unit_head *cu_header = &cu->header;
6568 bfd *obfd = objfile->obfd;
6569 unsigned int addr_size = cu_header->addr_size;
6570 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6571 /* Base address selection entry. */
6572 CORE_ADDR base;
6573 int found_base;
6574 unsigned int dummy;
6575 gdb_byte *buffer;
6576 CORE_ADDR marker;
6577 int low_set;
6578 CORE_ADDR low = 0;
6579 CORE_ADDR high = 0;
6580 CORE_ADDR baseaddr;
6581
6582 found_base = cu->base_known;
6583 base = cu->base_address;
6584
6585 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6586 if (offset >= dwarf2_per_objfile->ranges.size)
6587 {
6588 complaint (&symfile_complaints,
6589 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6590 offset);
6591 return 0;
6592 }
6593 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6594
6595 /* Read in the largest possible address. */
6596 marker = read_address (obfd, buffer, cu, &dummy);
6597 if ((marker & mask) == mask)
6598 {
6599 /* If we found the largest possible address, then
6600 read the base address. */
6601 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6602 buffer += 2 * addr_size;
6603 offset += 2 * addr_size;
6604 found_base = 1;
6605 }
6606
6607 low_set = 0;
6608
6609 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6610
6611 while (1)
6612 {
6613 CORE_ADDR range_beginning, range_end;
6614
6615 range_beginning = read_address (obfd, buffer, cu, &dummy);
6616 buffer += addr_size;
6617 range_end = read_address (obfd, buffer, cu, &dummy);
6618 buffer += addr_size;
6619 offset += 2 * addr_size;
6620
6621 /* An end of list marker is a pair of zero addresses. */
6622 if (range_beginning == 0 && range_end == 0)
6623 /* Found the end of list entry. */
6624 break;
6625
6626 /* Each base address selection entry is a pair of 2 values.
6627 The first is the largest possible address, the second is
6628 the base address. Check for a base address here. */
6629 if ((range_beginning & mask) == mask)
6630 {
6631 /* If we found the largest possible address, then
6632 read the base address. */
6633 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6634 found_base = 1;
6635 continue;
6636 }
6637
6638 if (!found_base)
6639 {
6640 /* We have no valid base address for the ranges
6641 data. */
6642 complaint (&symfile_complaints,
6643 _("Invalid .debug_ranges data (no base address)"));
6644 return 0;
6645 }
6646
6647 if (range_beginning > range_end)
6648 {
6649 /* Inverted range entries are invalid. */
6650 complaint (&symfile_complaints,
6651 _("Invalid .debug_ranges data (inverted range)"));
6652 return 0;
6653 }
6654
6655 /* Empty range entries have no effect. */
6656 if (range_beginning == range_end)
6657 continue;
6658
6659 range_beginning += base;
6660 range_end += base;
6661
6662 if (ranges_pst != NULL)
6663 addrmap_set_empty (objfile->psymtabs_addrmap,
6664 range_beginning + baseaddr,
6665 range_end - 1 + baseaddr,
6666 ranges_pst);
6667
6668 /* FIXME: This is recording everything as a low-high
6669 segment of consecutive addresses. We should have a
6670 data structure for discontiguous block ranges
6671 instead. */
6672 if (! low_set)
6673 {
6674 low = range_beginning;
6675 high = range_end;
6676 low_set = 1;
6677 }
6678 else
6679 {
6680 if (range_beginning < low)
6681 low = range_beginning;
6682 if (range_end > high)
6683 high = range_end;
6684 }
6685 }
6686
6687 if (! low_set)
6688 /* If the first entry is an end-of-list marker, the range
6689 describes an empty scope, i.e. no instructions. */
6690 return 0;
6691
6692 if (low_return)
6693 *low_return = low;
6694 if (high_return)
6695 *high_return = high;
6696 return 1;
6697 }
6698
6699 /* Get low and high pc attributes from a die. Return 1 if the attributes
6700 are present and valid, otherwise, return 0. Return -1 if the range is
6701 discontinuous, i.e. derived from DW_AT_ranges information. */
6702
6703 static int
6704 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6705 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6706 struct partial_symtab *pst)
6707 {
6708 struct attribute *attr;
6709 struct attribute *attr_high;
6710 CORE_ADDR low = 0;
6711 CORE_ADDR high = 0;
6712 int ret = 0;
6713
6714 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
6715 if (attr_high)
6716 {
6717 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6718 if (attr)
6719 {
6720 low = DW_ADDR (attr);
6721 if (attr_high->form == DW_FORM_addr)
6722 high = DW_ADDR (attr_high);
6723 else
6724 high = low + DW_UNSND (attr_high);
6725 }
6726 else
6727 /* Found high w/o low attribute. */
6728 return 0;
6729
6730 /* Found consecutive range of addresses. */
6731 ret = 1;
6732 }
6733 else
6734 {
6735 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6736 if (attr != NULL)
6737 {
6738 /* Value of the DW_AT_ranges attribute is the offset in the
6739 .debug_ranges section. */
6740 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6741 return 0;
6742 /* Found discontinuous range of addresses. */
6743 ret = -1;
6744 }
6745 }
6746
6747 /* read_partial_die has also the strict LOW < HIGH requirement. */
6748 if (high <= low)
6749 return 0;
6750
6751 /* When using the GNU linker, .gnu.linkonce. sections are used to
6752 eliminate duplicate copies of functions and vtables and such.
6753 The linker will arbitrarily choose one and discard the others.
6754 The AT_*_pc values for such functions refer to local labels in
6755 these sections. If the section from that file was discarded, the
6756 labels are not in the output, so the relocs get a value of 0.
6757 If this is a discarded function, mark the pc bounds as invalid,
6758 so that GDB will ignore it. */
6759 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6760 return 0;
6761
6762 *lowpc = low;
6763 if (highpc)
6764 *highpc = high;
6765 return ret;
6766 }
6767
6768 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6769 its low and high PC addresses. Do nothing if these addresses could not
6770 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6771 and HIGHPC to the high address if greater than HIGHPC. */
6772
6773 static void
6774 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6775 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6776 struct dwarf2_cu *cu)
6777 {
6778 CORE_ADDR low, high;
6779 struct die_info *child = die->child;
6780
6781 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6782 {
6783 *lowpc = min (*lowpc, low);
6784 *highpc = max (*highpc, high);
6785 }
6786
6787 /* If the language does not allow nested subprograms (either inside
6788 subprograms or lexical blocks), we're done. */
6789 if (cu->language != language_ada)
6790 return;
6791
6792 /* Check all the children of the given DIE. If it contains nested
6793 subprograms, then check their pc bounds. Likewise, we need to
6794 check lexical blocks as well, as they may also contain subprogram
6795 definitions. */
6796 while (child && child->tag)
6797 {
6798 if (child->tag == DW_TAG_subprogram
6799 || child->tag == DW_TAG_lexical_block)
6800 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6801 child = sibling_die (child);
6802 }
6803 }
6804
6805 /* Get the low and high pc's represented by the scope DIE, and store
6806 them in *LOWPC and *HIGHPC. If the correct values can't be
6807 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6808
6809 static void
6810 get_scope_pc_bounds (struct die_info *die,
6811 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6812 struct dwarf2_cu *cu)
6813 {
6814 CORE_ADDR best_low = (CORE_ADDR) -1;
6815 CORE_ADDR best_high = (CORE_ADDR) 0;
6816 CORE_ADDR current_low, current_high;
6817
6818 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6819 {
6820 best_low = current_low;
6821 best_high = current_high;
6822 }
6823 else
6824 {
6825 struct die_info *child = die->child;
6826
6827 while (child && child->tag)
6828 {
6829 switch (child->tag) {
6830 case DW_TAG_subprogram:
6831 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6832 break;
6833 case DW_TAG_namespace:
6834 case DW_TAG_module:
6835 /* FIXME: carlton/2004-01-16: Should we do this for
6836 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6837 that current GCC's always emit the DIEs corresponding
6838 to definitions of methods of classes as children of a
6839 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6840 the DIEs giving the declarations, which could be
6841 anywhere). But I don't see any reason why the
6842 standards says that they have to be there. */
6843 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6844
6845 if (current_low != ((CORE_ADDR) -1))
6846 {
6847 best_low = min (best_low, current_low);
6848 best_high = max (best_high, current_high);
6849 }
6850 break;
6851 default:
6852 /* Ignore. */
6853 break;
6854 }
6855
6856 child = sibling_die (child);
6857 }
6858 }
6859
6860 *lowpc = best_low;
6861 *highpc = best_high;
6862 }
6863
6864 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6865 in DIE. */
6866
6867 static void
6868 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6869 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6870 {
6871 struct objfile *objfile = cu->objfile;
6872 struct attribute *attr;
6873 struct attribute *attr_high;
6874
6875 attr_high = dwarf2_attr (die, DW_AT_high_pc, cu);
6876 if (attr_high)
6877 {
6878 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6879 if (attr)
6880 {
6881 CORE_ADDR low = DW_ADDR (attr);
6882 CORE_ADDR high;
6883 if (attr_high->form == DW_FORM_addr)
6884 high = DW_ADDR (attr_high);
6885 else
6886 high = low + DW_UNSND (attr_high);
6887
6888 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6889 }
6890 }
6891
6892 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6893 if (attr)
6894 {
6895 bfd *obfd = objfile->obfd;
6896
6897 /* The value of the DW_AT_ranges attribute is the offset of the
6898 address range list in the .debug_ranges section. */
6899 unsigned long offset = DW_UNSND (attr);
6900 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6901
6902 /* For some target architectures, but not others, the
6903 read_address function sign-extends the addresses it returns.
6904 To recognize base address selection entries, we need a
6905 mask. */
6906 unsigned int addr_size = cu->header.addr_size;
6907 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6908
6909 /* The base address, to which the next pair is relative. Note
6910 that this 'base' is a DWARF concept: most entries in a range
6911 list are relative, to reduce the number of relocs against the
6912 debugging information. This is separate from this function's
6913 'baseaddr' argument, which GDB uses to relocate debugging
6914 information from a shared library based on the address at
6915 which the library was loaded. */
6916 CORE_ADDR base = cu->base_address;
6917 int base_known = cu->base_known;
6918
6919 gdb_assert (dwarf2_per_objfile->ranges.readin);
6920 if (offset >= dwarf2_per_objfile->ranges.size)
6921 {
6922 complaint (&symfile_complaints,
6923 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6924 offset);
6925 return;
6926 }
6927
6928 for (;;)
6929 {
6930 unsigned int bytes_read;
6931 CORE_ADDR start, end;
6932
6933 start = read_address (obfd, buffer, cu, &bytes_read);
6934 buffer += bytes_read;
6935 end = read_address (obfd, buffer, cu, &bytes_read);
6936 buffer += bytes_read;
6937
6938 /* Did we find the end of the range list? */
6939 if (start == 0 && end == 0)
6940 break;
6941
6942 /* Did we find a base address selection entry? */
6943 else if ((start & base_select_mask) == base_select_mask)
6944 {
6945 base = end;
6946 base_known = 1;
6947 }
6948
6949 /* We found an ordinary address range. */
6950 else
6951 {
6952 if (!base_known)
6953 {
6954 complaint (&symfile_complaints,
6955 _("Invalid .debug_ranges data "
6956 "(no base address)"));
6957 return;
6958 }
6959
6960 if (start > end)
6961 {
6962 /* Inverted range entries are invalid. */
6963 complaint (&symfile_complaints,
6964 _("Invalid .debug_ranges data "
6965 "(inverted range)"));
6966 return;
6967 }
6968
6969 /* Empty range entries have no effect. */
6970 if (start == end)
6971 continue;
6972
6973 record_block_range (block,
6974 baseaddr + base + start,
6975 baseaddr + base + end - 1);
6976 }
6977 }
6978 }
6979 }
6980
6981 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6982 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6983 during 4.6.0 experimental. */
6984
6985 static int
6986 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6987 {
6988 const char *cs;
6989 int major, minor, release;
6990 int result = 0;
6991
6992 if (cu->producer == NULL)
6993 {
6994 /* For unknown compilers expect their behavior is DWARF version
6995 compliant.
6996
6997 GCC started to support .debug_types sections by -gdwarf-4 since
6998 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6999 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
7000 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
7001 interpreted incorrectly by GDB now - GCC PR debug/48229. */
7002
7003 return 0;
7004 }
7005
7006 if (cu->checked_producer)
7007 return cu->producer_is_gxx_lt_4_6;
7008
7009 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
7010
7011 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
7012 {
7013 /* For non-GCC compilers expect their behavior is DWARF version
7014 compliant. */
7015 }
7016 else
7017 {
7018 cs = &cu->producer[strlen ("GNU ")];
7019 while (*cs && !isdigit (*cs))
7020 cs++;
7021 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
7022 {
7023 /* Not recognized as GCC. */
7024 }
7025 else
7026 result = major < 4 || (major == 4 && minor < 6);
7027 }
7028
7029 cu->checked_producer = 1;
7030 cu->producer_is_gxx_lt_4_6 = result;
7031
7032 return result;
7033 }
7034
7035 /* Return the default accessibility type if it is not overriden by
7036 DW_AT_accessibility. */
7037
7038 static enum dwarf_access_attribute
7039 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
7040 {
7041 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
7042 {
7043 /* The default DWARF 2 accessibility for members is public, the default
7044 accessibility for inheritance is private. */
7045
7046 if (die->tag != DW_TAG_inheritance)
7047 return DW_ACCESS_public;
7048 else
7049 return DW_ACCESS_private;
7050 }
7051 else
7052 {
7053 /* DWARF 3+ defines the default accessibility a different way. The same
7054 rules apply now for DW_TAG_inheritance as for the members and it only
7055 depends on the container kind. */
7056
7057 if (die->parent->tag == DW_TAG_class_type)
7058 return DW_ACCESS_private;
7059 else
7060 return DW_ACCESS_public;
7061 }
7062 }
7063
7064 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
7065 offset. If the attribute was not found return 0, otherwise return
7066 1. If it was found but could not properly be handled, set *OFFSET
7067 to 0. */
7068
7069 static int
7070 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
7071 LONGEST *offset)
7072 {
7073 struct attribute *attr;
7074
7075 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
7076 if (attr != NULL)
7077 {
7078 *offset = 0;
7079
7080 /* Note that we do not check for a section offset first here.
7081 This is because DW_AT_data_member_location is new in DWARF 4,
7082 so if we see it, we can assume that a constant form is really
7083 a constant and not a section offset. */
7084 if (attr_form_is_constant (attr))
7085 *offset = dwarf2_get_attr_constant_value (attr, 0);
7086 else if (attr_form_is_section_offset (attr))
7087 dwarf2_complex_location_expr_complaint ();
7088 else if (attr_form_is_block (attr))
7089 *offset = decode_locdesc (DW_BLOCK (attr), cu);
7090 else
7091 dwarf2_complex_location_expr_complaint ();
7092
7093 return 1;
7094 }
7095
7096 return 0;
7097 }
7098
7099 /* Add an aggregate field to the field list. */
7100
7101 static void
7102 dwarf2_add_field (struct field_info *fip, struct die_info *die,
7103 struct dwarf2_cu *cu)
7104 {
7105 struct objfile *objfile = cu->objfile;
7106 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7107 struct nextfield *new_field;
7108 struct attribute *attr;
7109 struct field *fp;
7110 char *fieldname = "";
7111
7112 /* Allocate a new field list entry and link it in. */
7113 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
7114 make_cleanup (xfree, new_field);
7115 memset (new_field, 0, sizeof (struct nextfield));
7116
7117 if (die->tag == DW_TAG_inheritance)
7118 {
7119 new_field->next = fip->baseclasses;
7120 fip->baseclasses = new_field;
7121 }
7122 else
7123 {
7124 new_field->next = fip->fields;
7125 fip->fields = new_field;
7126 }
7127 fip->nfields++;
7128
7129 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7130 if (attr)
7131 new_field->accessibility = DW_UNSND (attr);
7132 else
7133 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
7134 if (new_field->accessibility != DW_ACCESS_public)
7135 fip->non_public_fields = 1;
7136
7137 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7138 if (attr)
7139 new_field->virtuality = DW_UNSND (attr);
7140 else
7141 new_field->virtuality = DW_VIRTUALITY_none;
7142
7143 fp = &new_field->field;
7144
7145 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7146 {
7147 LONGEST offset;
7148
7149 /* Data member other than a C++ static data member. */
7150
7151 /* Get type of field. */
7152 fp->type = die_type (die, cu);
7153
7154 SET_FIELD_BITPOS (*fp, 0);
7155
7156 /* Get bit size of field (zero if none). */
7157 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7158 if (attr)
7159 {
7160 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7161 }
7162 else
7163 {
7164 FIELD_BITSIZE (*fp) = 0;
7165 }
7166
7167 /* Get bit offset of field. */
7168 if (handle_data_member_location (die, cu, &offset))
7169 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7170 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7171 if (attr)
7172 {
7173 if (gdbarch_bits_big_endian (gdbarch))
7174 {
7175 /* For big endian bits, the DW_AT_bit_offset gives the
7176 additional bit offset from the MSB of the containing
7177 anonymous object to the MSB of the field. We don't
7178 have to do anything special since we don't need to
7179 know the size of the anonymous object. */
7180 SET_FIELD_BITPOS (*fp, FIELD_BITPOS (*fp) + DW_UNSND (attr));
7181 }
7182 else
7183 {
7184 /* For little endian bits, compute the bit offset to the
7185 MSB of the anonymous object, subtract off the number of
7186 bits from the MSB of the field to the MSB of the
7187 object, and then subtract off the number of bits of
7188 the field itself. The result is the bit offset of
7189 the LSB of the field. */
7190 int anonymous_size;
7191 int bit_offset = DW_UNSND (attr);
7192
7193 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7194 if (attr)
7195 {
7196 /* The size of the anonymous object containing
7197 the bit field is explicit, so use the
7198 indicated size (in bytes). */
7199 anonymous_size = DW_UNSND (attr);
7200 }
7201 else
7202 {
7203 /* The size of the anonymous object containing
7204 the bit field must be inferred from the type
7205 attribute of the data member containing the
7206 bit field. */
7207 anonymous_size = TYPE_LENGTH (fp->type);
7208 }
7209 SET_FIELD_BITPOS (*fp,
7210 (FIELD_BITPOS (*fp)
7211 + anonymous_size * bits_per_byte
7212 - bit_offset - FIELD_BITSIZE (*fp)));
7213 }
7214 }
7215
7216 /* Get name of field. */
7217 fieldname = dwarf2_name (die, cu);
7218 if (fieldname == NULL)
7219 fieldname = "";
7220
7221 /* The name is already allocated along with this objfile, so we don't
7222 need to duplicate it for the type. */
7223 fp->name = fieldname;
7224
7225 /* Change accessibility for artificial fields (e.g. virtual table
7226 pointer or virtual base class pointer) to private. */
7227 if (dwarf2_attr (die, DW_AT_artificial, cu))
7228 {
7229 FIELD_ARTIFICIAL (*fp) = 1;
7230 new_field->accessibility = DW_ACCESS_private;
7231 fip->non_public_fields = 1;
7232 }
7233 }
7234 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7235 {
7236 /* C++ static member. */
7237
7238 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7239 is a declaration, but all versions of G++ as of this writing
7240 (so through at least 3.2.1) incorrectly generate
7241 DW_TAG_variable tags. */
7242
7243 const char *physname;
7244
7245 /* Get name of field. */
7246 fieldname = dwarf2_name (die, cu);
7247 if (fieldname == NULL)
7248 return;
7249
7250 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7251 if (attr
7252 /* Only create a symbol if this is an external value.
7253 new_symbol checks this and puts the value in the global symbol
7254 table, which we want. If it is not external, new_symbol
7255 will try to put the value in cu->list_in_scope which is wrong. */
7256 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7257 {
7258 /* A static const member, not much different than an enum as far as
7259 we're concerned, except that we can support more types. */
7260 new_symbol (die, NULL, cu);
7261 }
7262
7263 /* Get physical name. */
7264 physname = dwarf2_physname (fieldname, die, cu);
7265
7266 /* The name is already allocated along with this objfile, so we don't
7267 need to duplicate it for the type. */
7268 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7269 FIELD_TYPE (*fp) = die_type (die, cu);
7270 FIELD_NAME (*fp) = fieldname;
7271 }
7272 else if (die->tag == DW_TAG_inheritance)
7273 {
7274 LONGEST offset;
7275
7276 /* C++ base class field. */
7277 if (handle_data_member_location (die, cu, &offset))
7278 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7279 FIELD_BITSIZE (*fp) = 0;
7280 FIELD_TYPE (*fp) = die_type (die, cu);
7281 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7282 fip->nbaseclasses++;
7283 }
7284 }
7285
7286 /* Add a typedef defined in the scope of the FIP's class. */
7287
7288 static void
7289 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7290 struct dwarf2_cu *cu)
7291 {
7292 struct objfile *objfile = cu->objfile;
7293 struct typedef_field_list *new_field;
7294 struct attribute *attr;
7295 struct typedef_field *fp;
7296 char *fieldname = "";
7297
7298 /* Allocate a new field list entry and link it in. */
7299 new_field = xzalloc (sizeof (*new_field));
7300 make_cleanup (xfree, new_field);
7301
7302 gdb_assert (die->tag == DW_TAG_typedef);
7303
7304 fp = &new_field->field;
7305
7306 /* Get name of field. */
7307 fp->name = dwarf2_name (die, cu);
7308 if (fp->name == NULL)
7309 return;
7310
7311 fp->type = read_type_die (die, cu);
7312
7313 new_field->next = fip->typedef_field_list;
7314 fip->typedef_field_list = new_field;
7315 fip->typedef_field_list_count++;
7316 }
7317
7318 /* Create the vector of fields, and attach it to the type. */
7319
7320 static void
7321 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7322 struct dwarf2_cu *cu)
7323 {
7324 int nfields = fip->nfields;
7325
7326 /* Record the field count, allocate space for the array of fields,
7327 and create blank accessibility bitfields if necessary. */
7328 TYPE_NFIELDS (type) = nfields;
7329 TYPE_FIELDS (type) = (struct field *)
7330 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7331 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7332
7333 if (fip->non_public_fields && cu->language != language_ada)
7334 {
7335 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7336
7337 TYPE_FIELD_PRIVATE_BITS (type) =
7338 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7339 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7340
7341 TYPE_FIELD_PROTECTED_BITS (type) =
7342 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7343 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7344
7345 TYPE_FIELD_IGNORE_BITS (type) =
7346 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7347 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7348 }
7349
7350 /* If the type has baseclasses, allocate and clear a bit vector for
7351 TYPE_FIELD_VIRTUAL_BITS. */
7352 if (fip->nbaseclasses && cu->language != language_ada)
7353 {
7354 int num_bytes = B_BYTES (fip->nbaseclasses);
7355 unsigned char *pointer;
7356
7357 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7358 pointer = TYPE_ALLOC (type, num_bytes);
7359 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7360 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7361 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7362 }
7363
7364 /* Copy the saved-up fields into the field vector. Start from the head of
7365 the list, adding to the tail of the field array, so that they end up in
7366 the same order in the array in which they were added to the list. */
7367 while (nfields-- > 0)
7368 {
7369 struct nextfield *fieldp;
7370
7371 if (fip->fields)
7372 {
7373 fieldp = fip->fields;
7374 fip->fields = fieldp->next;
7375 }
7376 else
7377 {
7378 fieldp = fip->baseclasses;
7379 fip->baseclasses = fieldp->next;
7380 }
7381
7382 TYPE_FIELD (type, nfields) = fieldp->field;
7383 switch (fieldp->accessibility)
7384 {
7385 case DW_ACCESS_private:
7386 if (cu->language != language_ada)
7387 SET_TYPE_FIELD_PRIVATE (type, nfields);
7388 break;
7389
7390 case DW_ACCESS_protected:
7391 if (cu->language != language_ada)
7392 SET_TYPE_FIELD_PROTECTED (type, nfields);
7393 break;
7394
7395 case DW_ACCESS_public:
7396 break;
7397
7398 default:
7399 /* Unknown accessibility. Complain and treat it as public. */
7400 {
7401 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7402 fieldp->accessibility);
7403 }
7404 break;
7405 }
7406 if (nfields < fip->nbaseclasses)
7407 {
7408 switch (fieldp->virtuality)
7409 {
7410 case DW_VIRTUALITY_virtual:
7411 case DW_VIRTUALITY_pure_virtual:
7412 if (cu->language == language_ada)
7413 error (_("unexpected virtuality in component of Ada type"));
7414 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7415 break;
7416 }
7417 }
7418 }
7419 }
7420
7421 /* Add a member function to the proper fieldlist. */
7422
7423 static void
7424 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7425 struct type *type, struct dwarf2_cu *cu)
7426 {
7427 struct objfile *objfile = cu->objfile;
7428 struct attribute *attr;
7429 struct fnfieldlist *flp;
7430 int i;
7431 struct fn_field *fnp;
7432 char *fieldname;
7433 struct nextfnfield *new_fnfield;
7434 struct type *this_type;
7435 enum dwarf_access_attribute accessibility;
7436
7437 if (cu->language == language_ada)
7438 error (_("unexpected member function in Ada type"));
7439
7440 /* Get name of member function. */
7441 fieldname = dwarf2_name (die, cu);
7442 if (fieldname == NULL)
7443 return;
7444
7445 /* Look up member function name in fieldlist. */
7446 for (i = 0; i < fip->nfnfields; i++)
7447 {
7448 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7449 break;
7450 }
7451
7452 /* Create new list element if necessary. */
7453 if (i < fip->nfnfields)
7454 flp = &fip->fnfieldlists[i];
7455 else
7456 {
7457 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7458 {
7459 fip->fnfieldlists = (struct fnfieldlist *)
7460 xrealloc (fip->fnfieldlists,
7461 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7462 * sizeof (struct fnfieldlist));
7463 if (fip->nfnfields == 0)
7464 make_cleanup (free_current_contents, &fip->fnfieldlists);
7465 }
7466 flp = &fip->fnfieldlists[fip->nfnfields];
7467 flp->name = fieldname;
7468 flp->length = 0;
7469 flp->head = NULL;
7470 i = fip->nfnfields++;
7471 }
7472
7473 /* Create a new member function field and chain it to the field list
7474 entry. */
7475 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7476 make_cleanup (xfree, new_fnfield);
7477 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7478 new_fnfield->next = flp->head;
7479 flp->head = new_fnfield;
7480 flp->length++;
7481
7482 /* Fill in the member function field info. */
7483 fnp = &new_fnfield->fnfield;
7484
7485 /* Delay processing of the physname until later. */
7486 if (cu->language == language_cplus || cu->language == language_java)
7487 {
7488 add_to_method_list (type, i, flp->length - 1, fieldname,
7489 die, cu);
7490 }
7491 else
7492 {
7493 const char *physname = dwarf2_physname (fieldname, die, cu);
7494 fnp->physname = physname ? physname : "";
7495 }
7496
7497 fnp->type = alloc_type (objfile);
7498 this_type = read_type_die (die, cu);
7499 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7500 {
7501 int nparams = TYPE_NFIELDS (this_type);
7502
7503 /* TYPE is the domain of this method, and THIS_TYPE is the type
7504 of the method itself (TYPE_CODE_METHOD). */
7505 smash_to_method_type (fnp->type, type,
7506 TYPE_TARGET_TYPE (this_type),
7507 TYPE_FIELDS (this_type),
7508 TYPE_NFIELDS (this_type),
7509 TYPE_VARARGS (this_type));
7510
7511 /* Handle static member functions.
7512 Dwarf2 has no clean way to discern C++ static and non-static
7513 member functions. G++ helps GDB by marking the first
7514 parameter for non-static member functions (which is the this
7515 pointer) as artificial. We obtain this information from
7516 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7517 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7518 fnp->voffset = VOFFSET_STATIC;
7519 }
7520 else
7521 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7522 dwarf2_full_name (fieldname, die, cu));
7523
7524 /* Get fcontext from DW_AT_containing_type if present. */
7525 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7526 fnp->fcontext = die_containing_type (die, cu);
7527
7528 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7529 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7530
7531 /* Get accessibility. */
7532 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7533 if (attr)
7534 accessibility = DW_UNSND (attr);
7535 else
7536 accessibility = dwarf2_default_access_attribute (die, cu);
7537 switch (accessibility)
7538 {
7539 case DW_ACCESS_private:
7540 fnp->is_private = 1;
7541 break;
7542 case DW_ACCESS_protected:
7543 fnp->is_protected = 1;
7544 break;
7545 }
7546
7547 /* Check for artificial methods. */
7548 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7549 if (attr && DW_UNSND (attr) != 0)
7550 fnp->is_artificial = 1;
7551
7552 /* Get index in virtual function table if it is a virtual member
7553 function. For older versions of GCC, this is an offset in the
7554 appropriate virtual table, as specified by DW_AT_containing_type.
7555 For everyone else, it is an expression to be evaluated relative
7556 to the object address. */
7557
7558 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7559 if (attr)
7560 {
7561 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7562 {
7563 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7564 {
7565 /* Old-style GCC. */
7566 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7567 }
7568 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7569 || (DW_BLOCK (attr)->size > 1
7570 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7571 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7572 {
7573 struct dwarf_block blk;
7574 int offset;
7575
7576 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7577 ? 1 : 2);
7578 blk.size = DW_BLOCK (attr)->size - offset;
7579 blk.data = DW_BLOCK (attr)->data + offset;
7580 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7581 if ((fnp->voffset % cu->header.addr_size) != 0)
7582 dwarf2_complex_location_expr_complaint ();
7583 else
7584 fnp->voffset /= cu->header.addr_size;
7585 fnp->voffset += 2;
7586 }
7587 else
7588 dwarf2_complex_location_expr_complaint ();
7589
7590 if (!fnp->fcontext)
7591 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7592 }
7593 else if (attr_form_is_section_offset (attr))
7594 {
7595 dwarf2_complex_location_expr_complaint ();
7596 }
7597 else
7598 {
7599 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7600 fieldname);
7601 }
7602 }
7603 else
7604 {
7605 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7606 if (attr && DW_UNSND (attr))
7607 {
7608 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7609 complaint (&symfile_complaints,
7610 _("Member function \"%s\" (offset %d) is virtual "
7611 "but the vtable offset is not specified"),
7612 fieldname, die->offset.sect_off);
7613 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7614 TYPE_CPLUS_DYNAMIC (type) = 1;
7615 }
7616 }
7617 }
7618
7619 /* Create the vector of member function fields, and attach it to the type. */
7620
7621 static void
7622 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7623 struct dwarf2_cu *cu)
7624 {
7625 struct fnfieldlist *flp;
7626 int i;
7627
7628 if (cu->language == language_ada)
7629 error (_("unexpected member functions in Ada type"));
7630
7631 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7632 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7633 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7634
7635 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7636 {
7637 struct nextfnfield *nfp = flp->head;
7638 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7639 int k;
7640
7641 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7642 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7643 fn_flp->fn_fields = (struct fn_field *)
7644 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7645 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7646 fn_flp->fn_fields[k] = nfp->fnfield;
7647 }
7648
7649 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7650 }
7651
7652 /* Returns non-zero if NAME is the name of a vtable member in CU's
7653 language, zero otherwise. */
7654 static int
7655 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7656 {
7657 static const char vptr[] = "_vptr";
7658 static const char vtable[] = "vtable";
7659
7660 /* Look for the C++ and Java forms of the vtable. */
7661 if ((cu->language == language_java
7662 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7663 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7664 && is_cplus_marker (name[sizeof (vptr) - 1])))
7665 return 1;
7666
7667 return 0;
7668 }
7669
7670 /* GCC outputs unnamed structures that are really pointers to member
7671 functions, with the ABI-specified layout. If TYPE describes
7672 such a structure, smash it into a member function type.
7673
7674 GCC shouldn't do this; it should just output pointer to member DIEs.
7675 This is GCC PR debug/28767. */
7676
7677 static void
7678 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7679 {
7680 struct type *pfn_type, *domain_type, *new_type;
7681
7682 /* Check for a structure with no name and two children. */
7683 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7684 return;
7685
7686 /* Check for __pfn and __delta members. */
7687 if (TYPE_FIELD_NAME (type, 0) == NULL
7688 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7689 || TYPE_FIELD_NAME (type, 1) == NULL
7690 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7691 return;
7692
7693 /* Find the type of the method. */
7694 pfn_type = TYPE_FIELD_TYPE (type, 0);
7695 if (pfn_type == NULL
7696 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7697 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7698 return;
7699
7700 /* Look for the "this" argument. */
7701 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7702 if (TYPE_NFIELDS (pfn_type) == 0
7703 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7704 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7705 return;
7706
7707 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7708 new_type = alloc_type (objfile);
7709 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7710 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7711 TYPE_VARARGS (pfn_type));
7712 smash_to_methodptr_type (type, new_type);
7713 }
7714
7715 /* Called when we find the DIE that starts a structure or union scope
7716 (definition) to create a type for the structure or union. Fill in
7717 the type's name and general properties; the members will not be
7718 processed until process_structure_type.
7719
7720 NOTE: we need to call these functions regardless of whether or not the
7721 DIE has a DW_AT_name attribute, since it might be an anonymous
7722 structure or union. This gets the type entered into our set of
7723 user defined types.
7724
7725 However, if the structure is incomplete (an opaque struct/union)
7726 then suppress creating a symbol table entry for it since gdb only
7727 wants to find the one with the complete definition. Note that if
7728 it is complete, we just call new_symbol, which does it's own
7729 checking about whether the struct/union is anonymous or not (and
7730 suppresses creating a symbol table entry itself). */
7731
7732 static struct type *
7733 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7734 {
7735 struct objfile *objfile = cu->objfile;
7736 struct type *type;
7737 struct attribute *attr;
7738 char *name;
7739
7740 /* If the definition of this type lives in .debug_types, read that type.
7741 Don't follow DW_AT_specification though, that will take us back up
7742 the chain and we want to go down. */
7743 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7744 if (attr)
7745 {
7746 struct dwarf2_cu *type_cu = cu;
7747 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7748
7749 /* We could just recurse on read_structure_type, but we need to call
7750 get_die_type to ensure only one type for this DIE is created.
7751 This is important, for example, because for c++ classes we need
7752 TYPE_NAME set which is only done by new_symbol. Blech. */
7753 type = read_type_die (type_die, type_cu);
7754
7755 /* TYPE_CU may not be the same as CU.
7756 Ensure TYPE is recorded in CU's type_hash table. */
7757 return set_die_type (die, type, cu);
7758 }
7759
7760 type = alloc_type (objfile);
7761 INIT_CPLUS_SPECIFIC (type);
7762
7763 name = dwarf2_name (die, cu);
7764 if (name != NULL)
7765 {
7766 if (cu->language == language_cplus
7767 || cu->language == language_java)
7768 {
7769 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7770
7771 /* dwarf2_full_name might have already finished building the DIE's
7772 type. If so, there is no need to continue. */
7773 if (get_die_type (die, cu) != NULL)
7774 return get_die_type (die, cu);
7775
7776 TYPE_TAG_NAME (type) = full_name;
7777 if (die->tag == DW_TAG_structure_type
7778 || die->tag == DW_TAG_class_type)
7779 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7780 }
7781 else
7782 {
7783 /* The name is already allocated along with this objfile, so
7784 we don't need to duplicate it for the type. */
7785 TYPE_TAG_NAME (type) = (char *) name;
7786 if (die->tag == DW_TAG_class_type)
7787 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7788 }
7789 }
7790
7791 if (die->tag == DW_TAG_structure_type)
7792 {
7793 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7794 }
7795 else if (die->tag == DW_TAG_union_type)
7796 {
7797 TYPE_CODE (type) = TYPE_CODE_UNION;
7798 }
7799 else
7800 {
7801 TYPE_CODE (type) = TYPE_CODE_CLASS;
7802 }
7803
7804 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7805 TYPE_DECLARED_CLASS (type) = 1;
7806
7807 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7808 if (attr)
7809 {
7810 TYPE_LENGTH (type) = DW_UNSND (attr);
7811 }
7812 else
7813 {
7814 TYPE_LENGTH (type) = 0;
7815 }
7816
7817 TYPE_STUB_SUPPORTED (type) = 1;
7818 if (die_is_declaration (die, cu))
7819 TYPE_STUB (type) = 1;
7820 else if (attr == NULL && die->child == NULL
7821 && producer_is_realview (cu->producer))
7822 /* RealView does not output the required DW_AT_declaration
7823 on incomplete types. */
7824 TYPE_STUB (type) = 1;
7825
7826 /* We need to add the type field to the die immediately so we don't
7827 infinitely recurse when dealing with pointers to the structure
7828 type within the structure itself. */
7829 set_die_type (die, type, cu);
7830
7831 /* set_die_type should be already done. */
7832 set_descriptive_type (type, die, cu);
7833
7834 return type;
7835 }
7836
7837 /* Finish creating a structure or union type, including filling in
7838 its members and creating a symbol for it. */
7839
7840 static void
7841 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7842 {
7843 struct objfile *objfile = cu->objfile;
7844 struct die_info *child_die = die->child;
7845 struct type *type;
7846
7847 type = get_die_type (die, cu);
7848 if (type == NULL)
7849 type = read_structure_type (die, cu);
7850
7851 if (die->child != NULL && ! die_is_declaration (die, cu))
7852 {
7853 struct field_info fi;
7854 struct die_info *child_die;
7855 VEC (symbolp) *template_args = NULL;
7856 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7857
7858 memset (&fi, 0, sizeof (struct field_info));
7859
7860 child_die = die->child;
7861
7862 while (child_die && child_die->tag)
7863 {
7864 if (child_die->tag == DW_TAG_member
7865 || child_die->tag == DW_TAG_variable)
7866 {
7867 /* NOTE: carlton/2002-11-05: A C++ static data member
7868 should be a DW_TAG_member that is a declaration, but
7869 all versions of G++ as of this writing (so through at
7870 least 3.2.1) incorrectly generate DW_TAG_variable
7871 tags for them instead. */
7872 dwarf2_add_field (&fi, child_die, cu);
7873 }
7874 else if (child_die->tag == DW_TAG_subprogram)
7875 {
7876 /* C++ member function. */
7877 dwarf2_add_member_fn (&fi, child_die, type, cu);
7878 }
7879 else if (child_die->tag == DW_TAG_inheritance)
7880 {
7881 /* C++ base class field. */
7882 dwarf2_add_field (&fi, child_die, cu);
7883 }
7884 else if (child_die->tag == DW_TAG_typedef)
7885 dwarf2_add_typedef (&fi, child_die, cu);
7886 else if (child_die->tag == DW_TAG_template_type_param
7887 || child_die->tag == DW_TAG_template_value_param)
7888 {
7889 struct symbol *arg = new_symbol (child_die, NULL, cu);
7890
7891 if (arg != NULL)
7892 VEC_safe_push (symbolp, template_args, arg);
7893 }
7894
7895 child_die = sibling_die (child_die);
7896 }
7897
7898 /* Attach template arguments to type. */
7899 if (! VEC_empty (symbolp, template_args))
7900 {
7901 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7902 TYPE_N_TEMPLATE_ARGUMENTS (type)
7903 = VEC_length (symbolp, template_args);
7904 TYPE_TEMPLATE_ARGUMENTS (type)
7905 = obstack_alloc (&objfile->objfile_obstack,
7906 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7907 * sizeof (struct symbol *)));
7908 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7909 VEC_address (symbolp, template_args),
7910 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7911 * sizeof (struct symbol *)));
7912 VEC_free (symbolp, template_args);
7913 }
7914
7915 /* Attach fields and member functions to the type. */
7916 if (fi.nfields)
7917 dwarf2_attach_fields_to_type (&fi, type, cu);
7918 if (fi.nfnfields)
7919 {
7920 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7921
7922 /* Get the type which refers to the base class (possibly this
7923 class itself) which contains the vtable pointer for the current
7924 class from the DW_AT_containing_type attribute. This use of
7925 DW_AT_containing_type is a GNU extension. */
7926
7927 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7928 {
7929 struct type *t = die_containing_type (die, cu);
7930
7931 TYPE_VPTR_BASETYPE (type) = t;
7932 if (type == t)
7933 {
7934 int i;
7935
7936 /* Our own class provides vtbl ptr. */
7937 for (i = TYPE_NFIELDS (t) - 1;
7938 i >= TYPE_N_BASECLASSES (t);
7939 --i)
7940 {
7941 const char *fieldname = TYPE_FIELD_NAME (t, i);
7942
7943 if (is_vtable_name (fieldname, cu))
7944 {
7945 TYPE_VPTR_FIELDNO (type) = i;
7946 break;
7947 }
7948 }
7949
7950 /* Complain if virtual function table field not found. */
7951 if (i < TYPE_N_BASECLASSES (t))
7952 complaint (&symfile_complaints,
7953 _("virtual function table pointer "
7954 "not found when defining class '%s'"),
7955 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7956 "");
7957 }
7958 else
7959 {
7960 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7961 }
7962 }
7963 else if (cu->producer
7964 && strncmp (cu->producer,
7965 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7966 {
7967 /* The IBM XLC compiler does not provide direct indication
7968 of the containing type, but the vtable pointer is
7969 always named __vfp. */
7970
7971 int i;
7972
7973 for (i = TYPE_NFIELDS (type) - 1;
7974 i >= TYPE_N_BASECLASSES (type);
7975 --i)
7976 {
7977 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7978 {
7979 TYPE_VPTR_FIELDNO (type) = i;
7980 TYPE_VPTR_BASETYPE (type) = type;
7981 break;
7982 }
7983 }
7984 }
7985 }
7986
7987 /* Copy fi.typedef_field_list linked list elements content into the
7988 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7989 if (fi.typedef_field_list)
7990 {
7991 int i = fi.typedef_field_list_count;
7992
7993 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7994 TYPE_TYPEDEF_FIELD_ARRAY (type)
7995 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7996 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7997
7998 /* Reverse the list order to keep the debug info elements order. */
7999 while (--i >= 0)
8000 {
8001 struct typedef_field *dest, *src;
8002
8003 dest = &TYPE_TYPEDEF_FIELD (type, i);
8004 src = &fi.typedef_field_list->field;
8005 fi.typedef_field_list = fi.typedef_field_list->next;
8006 *dest = *src;
8007 }
8008 }
8009
8010 do_cleanups (back_to);
8011
8012 if (HAVE_CPLUS_STRUCT (type))
8013 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
8014 }
8015
8016 quirk_gcc_member_function_pointer (type, objfile);
8017
8018 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
8019 snapshots) has been known to create a die giving a declaration
8020 for a class that has, as a child, a die giving a definition for a
8021 nested class. So we have to process our children even if the
8022 current die is a declaration. Normally, of course, a declaration
8023 won't have any children at all. */
8024
8025 while (child_die != NULL && child_die->tag)
8026 {
8027 if (child_die->tag == DW_TAG_member
8028 || child_die->tag == DW_TAG_variable
8029 || child_die->tag == DW_TAG_inheritance
8030 || child_die->tag == DW_TAG_template_value_param
8031 || child_die->tag == DW_TAG_template_type_param)
8032 {
8033 /* Do nothing. */
8034 }
8035 else
8036 process_die (child_die, cu);
8037
8038 child_die = sibling_die (child_die);
8039 }
8040
8041 /* Do not consider external references. According to the DWARF standard,
8042 these DIEs are identified by the fact that they have no byte_size
8043 attribute, and a declaration attribute. */
8044 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
8045 || !die_is_declaration (die, cu))
8046 new_symbol (die, type, cu);
8047 }
8048
8049 /* Given a DW_AT_enumeration_type die, set its type. We do not
8050 complete the type's fields yet, or create any symbols. */
8051
8052 static struct type *
8053 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
8054 {
8055 struct objfile *objfile = cu->objfile;
8056 struct type *type;
8057 struct attribute *attr;
8058 const char *name;
8059
8060 /* If the definition of this type lives in .debug_types, read that type.
8061 Don't follow DW_AT_specification though, that will take us back up
8062 the chain and we want to go down. */
8063 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
8064 if (attr)
8065 {
8066 struct dwarf2_cu *type_cu = cu;
8067 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
8068
8069 type = read_type_die (type_die, type_cu);
8070
8071 /* TYPE_CU may not be the same as CU.
8072 Ensure TYPE is recorded in CU's type_hash table. */
8073 return set_die_type (die, type, cu);
8074 }
8075
8076 type = alloc_type (objfile);
8077
8078 TYPE_CODE (type) = TYPE_CODE_ENUM;
8079 name = dwarf2_full_name (NULL, die, cu);
8080 if (name != NULL)
8081 TYPE_TAG_NAME (type) = (char *) name;
8082
8083 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8084 if (attr)
8085 {
8086 TYPE_LENGTH (type) = DW_UNSND (attr);
8087 }
8088 else
8089 {
8090 TYPE_LENGTH (type) = 0;
8091 }
8092
8093 /* The enumeration DIE can be incomplete. In Ada, any type can be
8094 declared as private in the package spec, and then defined only
8095 inside the package body. Such types are known as Taft Amendment
8096 Types. When another package uses such a type, an incomplete DIE
8097 may be generated by the compiler. */
8098 if (die_is_declaration (die, cu))
8099 TYPE_STUB (type) = 1;
8100
8101 return set_die_type (die, type, cu);
8102 }
8103
8104 /* Given a pointer to a die which begins an enumeration, process all
8105 the dies that define the members of the enumeration, and create the
8106 symbol for the enumeration type.
8107
8108 NOTE: We reverse the order of the element list. */
8109
8110 static void
8111 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
8112 {
8113 struct type *this_type;
8114
8115 this_type = get_die_type (die, cu);
8116 if (this_type == NULL)
8117 this_type = read_enumeration_type (die, cu);
8118
8119 if (die->child != NULL)
8120 {
8121 struct die_info *child_die;
8122 struct symbol *sym;
8123 struct field *fields = NULL;
8124 int num_fields = 0;
8125 int unsigned_enum = 1;
8126 char *name;
8127 int flag_enum = 1;
8128 ULONGEST mask = 0;
8129
8130 child_die = die->child;
8131 while (child_die && child_die->tag)
8132 {
8133 if (child_die->tag != DW_TAG_enumerator)
8134 {
8135 process_die (child_die, cu);
8136 }
8137 else
8138 {
8139 name = dwarf2_name (child_die, cu);
8140 if (name)
8141 {
8142 sym = new_symbol (child_die, this_type, cu);
8143 if (SYMBOL_VALUE (sym) < 0)
8144 {
8145 unsigned_enum = 0;
8146 flag_enum = 0;
8147 }
8148 else if ((mask & SYMBOL_VALUE (sym)) != 0)
8149 flag_enum = 0;
8150 else
8151 mask |= SYMBOL_VALUE (sym);
8152
8153 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8154 {
8155 fields = (struct field *)
8156 xrealloc (fields,
8157 (num_fields + DW_FIELD_ALLOC_CHUNK)
8158 * sizeof (struct field));
8159 }
8160
8161 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8162 FIELD_TYPE (fields[num_fields]) = NULL;
8163 SET_FIELD_ENUMVAL (fields[num_fields], SYMBOL_VALUE (sym));
8164 FIELD_BITSIZE (fields[num_fields]) = 0;
8165
8166 num_fields++;
8167 }
8168 }
8169
8170 child_die = sibling_die (child_die);
8171 }
8172
8173 if (num_fields)
8174 {
8175 TYPE_NFIELDS (this_type) = num_fields;
8176 TYPE_FIELDS (this_type) = (struct field *)
8177 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8178 memcpy (TYPE_FIELDS (this_type), fields,
8179 sizeof (struct field) * num_fields);
8180 xfree (fields);
8181 }
8182 if (unsigned_enum)
8183 TYPE_UNSIGNED (this_type) = 1;
8184 if (flag_enum)
8185 TYPE_FLAG_ENUM (this_type) = 1;
8186 }
8187
8188 /* If we are reading an enum from a .debug_types unit, and the enum
8189 is a declaration, and the enum is not the signatured type in the
8190 unit, then we do not want to add a symbol for it. Adding a
8191 symbol would in some cases obscure the true definition of the
8192 enum, giving users an incomplete type when the definition is
8193 actually available. Note that we do not want to do this for all
8194 enums which are just declarations, because C++0x allows forward
8195 enum declarations. */
8196 if (cu->per_cu->debug_types_section
8197 && die_is_declaration (die, cu))
8198 {
8199 struct signatured_type *sig_type;
8200
8201 sig_type
8202 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8203 cu->per_cu->debug_types_section,
8204 cu->per_cu->offset);
8205 if (sig_type->per_cu.offset.sect_off + sig_type->type_offset.cu_off
8206 != die->offset.sect_off)
8207 return;
8208 }
8209
8210 new_symbol (die, this_type, cu);
8211 }
8212
8213 /* Extract all information from a DW_TAG_array_type DIE and put it in
8214 the DIE's type field. For now, this only handles one dimensional
8215 arrays. */
8216
8217 static struct type *
8218 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8219 {
8220 struct objfile *objfile = cu->objfile;
8221 struct die_info *child_die;
8222 struct type *type;
8223 struct type *element_type, *range_type, *index_type;
8224 struct type **range_types = NULL;
8225 struct attribute *attr;
8226 int ndim = 0;
8227 struct cleanup *back_to;
8228 char *name;
8229
8230 element_type = die_type (die, cu);
8231
8232 /* The die_type call above may have already set the type for this DIE. */
8233 type = get_die_type (die, cu);
8234 if (type)
8235 return type;
8236
8237 /* Irix 6.2 native cc creates array types without children for
8238 arrays with unspecified length. */
8239 if (die->child == NULL)
8240 {
8241 index_type = objfile_type (objfile)->builtin_int;
8242 range_type = create_range_type (NULL, index_type, 0, -1);
8243 type = create_array_type (NULL, element_type, range_type);
8244 return set_die_type (die, type, cu);
8245 }
8246
8247 back_to = make_cleanup (null_cleanup, NULL);
8248 child_die = die->child;
8249 while (child_die && child_die->tag)
8250 {
8251 if (child_die->tag == DW_TAG_subrange_type)
8252 {
8253 struct type *child_type = read_type_die (child_die, cu);
8254
8255 if (child_type != NULL)
8256 {
8257 /* The range type was succesfully read. Save it for the
8258 array type creation. */
8259 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8260 {
8261 range_types = (struct type **)
8262 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8263 * sizeof (struct type *));
8264 if (ndim == 0)
8265 make_cleanup (free_current_contents, &range_types);
8266 }
8267 range_types[ndim++] = child_type;
8268 }
8269 }
8270 child_die = sibling_die (child_die);
8271 }
8272
8273 /* Dwarf2 dimensions are output from left to right, create the
8274 necessary array types in backwards order. */
8275
8276 type = element_type;
8277
8278 if (read_array_order (die, cu) == DW_ORD_col_major)
8279 {
8280 int i = 0;
8281
8282 while (i < ndim)
8283 type = create_array_type (NULL, type, range_types[i++]);
8284 }
8285 else
8286 {
8287 while (ndim-- > 0)
8288 type = create_array_type (NULL, type, range_types[ndim]);
8289 }
8290
8291 /* Understand Dwarf2 support for vector types (like they occur on
8292 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8293 array type. This is not part of the Dwarf2/3 standard yet, but a
8294 custom vendor extension. The main difference between a regular
8295 array and the vector variant is that vectors are passed by value
8296 to functions. */
8297 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8298 if (attr)
8299 make_vector_type (type);
8300
8301 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8302 implementation may choose to implement triple vectors using this
8303 attribute. */
8304 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8305 if (attr)
8306 {
8307 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8308 TYPE_LENGTH (type) = DW_UNSND (attr);
8309 else
8310 complaint (&symfile_complaints,
8311 _("DW_AT_byte_size for array type smaller "
8312 "than the total size of elements"));
8313 }
8314
8315 name = dwarf2_name (die, cu);
8316 if (name)
8317 TYPE_NAME (type) = name;
8318
8319 /* Install the type in the die. */
8320 set_die_type (die, type, cu);
8321
8322 /* set_die_type should be already done. */
8323 set_descriptive_type (type, die, cu);
8324
8325 do_cleanups (back_to);
8326
8327 return type;
8328 }
8329
8330 static enum dwarf_array_dim_ordering
8331 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8332 {
8333 struct attribute *attr;
8334
8335 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8336
8337 if (attr) return DW_SND (attr);
8338
8339 /* GNU F77 is a special case, as at 08/2004 array type info is the
8340 opposite order to the dwarf2 specification, but data is still
8341 laid out as per normal fortran.
8342
8343 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8344 version checking. */
8345
8346 if (cu->language == language_fortran
8347 && cu->producer && strstr (cu->producer, "GNU F77"))
8348 {
8349 return DW_ORD_row_major;
8350 }
8351
8352 switch (cu->language_defn->la_array_ordering)
8353 {
8354 case array_column_major:
8355 return DW_ORD_col_major;
8356 case array_row_major:
8357 default:
8358 return DW_ORD_row_major;
8359 };
8360 }
8361
8362 /* Extract all information from a DW_TAG_set_type DIE and put it in
8363 the DIE's type field. */
8364
8365 static struct type *
8366 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8367 {
8368 struct type *domain_type, *set_type;
8369 struct attribute *attr;
8370
8371 domain_type = die_type (die, cu);
8372
8373 /* The die_type call above may have already set the type for this DIE. */
8374 set_type = get_die_type (die, cu);
8375 if (set_type)
8376 return set_type;
8377
8378 set_type = create_set_type (NULL, domain_type);
8379
8380 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8381 if (attr)
8382 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8383
8384 return set_die_type (die, set_type, cu);
8385 }
8386
8387 /* First cut: install each common block member as a global variable. */
8388
8389 static void
8390 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8391 {
8392 struct die_info *child_die;
8393 struct attribute *attr;
8394 struct symbol *sym;
8395 CORE_ADDR base = (CORE_ADDR) 0;
8396
8397 attr = dwarf2_attr (die, DW_AT_location, cu);
8398 if (attr)
8399 {
8400 /* Support the .debug_loc offsets. */
8401 if (attr_form_is_block (attr))
8402 {
8403 base = decode_locdesc (DW_BLOCK (attr), cu);
8404 }
8405 else if (attr_form_is_section_offset (attr))
8406 {
8407 dwarf2_complex_location_expr_complaint ();
8408 }
8409 else
8410 {
8411 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8412 "common block member");
8413 }
8414 }
8415 if (die->child != NULL)
8416 {
8417 child_die = die->child;
8418 while (child_die && child_die->tag)
8419 {
8420 LONGEST offset;
8421
8422 sym = new_symbol (child_die, NULL, cu);
8423 if (sym != NULL
8424 && handle_data_member_location (child_die, cu, &offset))
8425 {
8426 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8427 add_symbol_to_list (sym, &global_symbols);
8428 }
8429 child_die = sibling_die (child_die);
8430 }
8431 }
8432 }
8433
8434 /* Create a type for a C++ namespace. */
8435
8436 static struct type *
8437 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8438 {
8439 struct objfile *objfile = cu->objfile;
8440 const char *previous_prefix, *name;
8441 int is_anonymous;
8442 struct type *type;
8443
8444 /* For extensions, reuse the type of the original namespace. */
8445 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8446 {
8447 struct die_info *ext_die;
8448 struct dwarf2_cu *ext_cu = cu;
8449
8450 ext_die = dwarf2_extension (die, &ext_cu);
8451 type = read_type_die (ext_die, ext_cu);
8452
8453 /* EXT_CU may not be the same as CU.
8454 Ensure TYPE is recorded in CU's type_hash table. */
8455 return set_die_type (die, type, cu);
8456 }
8457
8458 name = namespace_name (die, &is_anonymous, cu);
8459
8460 /* Now build the name of the current namespace. */
8461
8462 previous_prefix = determine_prefix (die, cu);
8463 if (previous_prefix[0] != '\0')
8464 name = typename_concat (&objfile->objfile_obstack,
8465 previous_prefix, name, 0, cu);
8466
8467 /* Create the type. */
8468 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8469 objfile);
8470 TYPE_NAME (type) = (char *) name;
8471 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8472
8473 return set_die_type (die, type, cu);
8474 }
8475
8476 /* Read a C++ namespace. */
8477
8478 static void
8479 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8480 {
8481 struct objfile *objfile = cu->objfile;
8482 int is_anonymous;
8483
8484 /* Add a symbol associated to this if we haven't seen the namespace
8485 before. Also, add a using directive if it's an anonymous
8486 namespace. */
8487
8488 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8489 {
8490 struct type *type;
8491
8492 type = read_type_die (die, cu);
8493 new_symbol (die, type, cu);
8494
8495 namespace_name (die, &is_anonymous, cu);
8496 if (is_anonymous)
8497 {
8498 const char *previous_prefix = determine_prefix (die, cu);
8499
8500 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8501 NULL, NULL, &objfile->objfile_obstack);
8502 }
8503 }
8504
8505 if (die->child != NULL)
8506 {
8507 struct die_info *child_die = die->child;
8508
8509 while (child_die && child_die->tag)
8510 {
8511 process_die (child_die, cu);
8512 child_die = sibling_die (child_die);
8513 }
8514 }
8515 }
8516
8517 /* Read a Fortran module as type. This DIE can be only a declaration used for
8518 imported module. Still we need that type as local Fortran "use ... only"
8519 declaration imports depend on the created type in determine_prefix. */
8520
8521 static struct type *
8522 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8523 {
8524 struct objfile *objfile = cu->objfile;
8525 char *module_name;
8526 struct type *type;
8527
8528 module_name = dwarf2_name (die, cu);
8529 if (!module_name)
8530 complaint (&symfile_complaints,
8531 _("DW_TAG_module has no name, offset 0x%x"),
8532 die->offset.sect_off);
8533 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8534
8535 /* determine_prefix uses TYPE_TAG_NAME. */
8536 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8537
8538 return set_die_type (die, type, cu);
8539 }
8540
8541 /* Read a Fortran module. */
8542
8543 static void
8544 read_module (struct die_info *die, struct dwarf2_cu *cu)
8545 {
8546 struct die_info *child_die = die->child;
8547
8548 while (child_die && child_die->tag)
8549 {
8550 process_die (child_die, cu);
8551 child_die = sibling_die (child_die);
8552 }
8553 }
8554
8555 /* Return the name of the namespace represented by DIE. Set
8556 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8557 namespace. */
8558
8559 static const char *
8560 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8561 {
8562 struct die_info *current_die;
8563 const char *name = NULL;
8564
8565 /* Loop through the extensions until we find a name. */
8566
8567 for (current_die = die;
8568 current_die != NULL;
8569 current_die = dwarf2_extension (die, &cu))
8570 {
8571 name = dwarf2_name (current_die, cu);
8572 if (name != NULL)
8573 break;
8574 }
8575
8576 /* Is it an anonymous namespace? */
8577
8578 *is_anonymous = (name == NULL);
8579 if (*is_anonymous)
8580 name = CP_ANONYMOUS_NAMESPACE_STR;
8581
8582 return name;
8583 }
8584
8585 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8586 the user defined type vector. */
8587
8588 static struct type *
8589 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8590 {
8591 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8592 struct comp_unit_head *cu_header = &cu->header;
8593 struct type *type;
8594 struct attribute *attr_byte_size;
8595 struct attribute *attr_address_class;
8596 int byte_size, addr_class;
8597 struct type *target_type;
8598
8599 target_type = die_type (die, cu);
8600
8601 /* The die_type call above may have already set the type for this DIE. */
8602 type = get_die_type (die, cu);
8603 if (type)
8604 return type;
8605
8606 type = lookup_pointer_type (target_type);
8607
8608 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8609 if (attr_byte_size)
8610 byte_size = DW_UNSND (attr_byte_size);
8611 else
8612 byte_size = cu_header->addr_size;
8613
8614 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8615 if (attr_address_class)
8616 addr_class = DW_UNSND (attr_address_class);
8617 else
8618 addr_class = DW_ADDR_none;
8619
8620 /* If the pointer size or address class is different than the
8621 default, create a type variant marked as such and set the
8622 length accordingly. */
8623 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8624 {
8625 if (gdbarch_address_class_type_flags_p (gdbarch))
8626 {
8627 int type_flags;
8628
8629 type_flags = gdbarch_address_class_type_flags
8630 (gdbarch, byte_size, addr_class);
8631 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8632 == 0);
8633 type = make_type_with_address_space (type, type_flags);
8634 }
8635 else if (TYPE_LENGTH (type) != byte_size)
8636 {
8637 complaint (&symfile_complaints,
8638 _("invalid pointer size %d"), byte_size);
8639 }
8640 else
8641 {
8642 /* Should we also complain about unhandled address classes? */
8643 }
8644 }
8645
8646 TYPE_LENGTH (type) = byte_size;
8647 return set_die_type (die, type, cu);
8648 }
8649
8650 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8651 the user defined type vector. */
8652
8653 static struct type *
8654 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8655 {
8656 struct type *type;
8657 struct type *to_type;
8658 struct type *domain;
8659
8660 to_type = die_type (die, cu);
8661 domain = die_containing_type (die, cu);
8662
8663 /* The calls above may have already set the type for this DIE. */
8664 type = get_die_type (die, cu);
8665 if (type)
8666 return type;
8667
8668 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8669 type = lookup_methodptr_type (to_type);
8670 else
8671 type = lookup_memberptr_type (to_type, domain);
8672
8673 return set_die_type (die, type, cu);
8674 }
8675
8676 /* Extract all information from a DW_TAG_reference_type DIE and add to
8677 the user defined type vector. */
8678
8679 static struct type *
8680 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8681 {
8682 struct comp_unit_head *cu_header = &cu->header;
8683 struct type *type, *target_type;
8684 struct attribute *attr;
8685
8686 target_type = die_type (die, cu);
8687
8688 /* The die_type call above may have already set the type for this DIE. */
8689 type = get_die_type (die, cu);
8690 if (type)
8691 return type;
8692
8693 type = lookup_reference_type (target_type);
8694 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8695 if (attr)
8696 {
8697 TYPE_LENGTH (type) = DW_UNSND (attr);
8698 }
8699 else
8700 {
8701 TYPE_LENGTH (type) = cu_header->addr_size;
8702 }
8703 return set_die_type (die, type, cu);
8704 }
8705
8706 static struct type *
8707 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8708 {
8709 struct type *base_type, *cv_type;
8710
8711 base_type = die_type (die, cu);
8712
8713 /* The die_type call above may have already set the type for this DIE. */
8714 cv_type = get_die_type (die, cu);
8715 if (cv_type)
8716 return cv_type;
8717
8718 /* In case the const qualifier is applied to an array type, the element type
8719 is so qualified, not the array type (section 6.7.3 of C99). */
8720 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8721 {
8722 struct type *el_type, *inner_array;
8723
8724 base_type = copy_type (base_type);
8725 inner_array = base_type;
8726
8727 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8728 {
8729 TYPE_TARGET_TYPE (inner_array) =
8730 copy_type (TYPE_TARGET_TYPE (inner_array));
8731 inner_array = TYPE_TARGET_TYPE (inner_array);
8732 }
8733
8734 el_type = TYPE_TARGET_TYPE (inner_array);
8735 TYPE_TARGET_TYPE (inner_array) =
8736 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8737
8738 return set_die_type (die, base_type, cu);
8739 }
8740
8741 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8742 return set_die_type (die, cv_type, cu);
8743 }
8744
8745 static struct type *
8746 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8747 {
8748 struct type *base_type, *cv_type;
8749
8750 base_type = die_type (die, cu);
8751
8752 /* The die_type call above may have already set the type for this DIE. */
8753 cv_type = get_die_type (die, cu);
8754 if (cv_type)
8755 return cv_type;
8756
8757 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8758 return set_die_type (die, cv_type, cu);
8759 }
8760
8761 /* Extract all information from a DW_TAG_string_type DIE and add to
8762 the user defined type vector. It isn't really a user defined type,
8763 but it behaves like one, with other DIE's using an AT_user_def_type
8764 attribute to reference it. */
8765
8766 static struct type *
8767 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8768 {
8769 struct objfile *objfile = cu->objfile;
8770 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8771 struct type *type, *range_type, *index_type, *char_type;
8772 struct attribute *attr;
8773 unsigned int length;
8774
8775 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8776 if (attr)
8777 {
8778 length = DW_UNSND (attr);
8779 }
8780 else
8781 {
8782 /* Check for the DW_AT_byte_size attribute. */
8783 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8784 if (attr)
8785 {
8786 length = DW_UNSND (attr);
8787 }
8788 else
8789 {
8790 length = 1;
8791 }
8792 }
8793
8794 index_type = objfile_type (objfile)->builtin_int;
8795 range_type = create_range_type (NULL, index_type, 1, length);
8796 char_type = language_string_char_type (cu->language_defn, gdbarch);
8797 type = create_string_type (NULL, char_type, range_type);
8798
8799 return set_die_type (die, type, cu);
8800 }
8801
8802 /* Handle DIES due to C code like:
8803
8804 struct foo
8805 {
8806 int (*funcp)(int a, long l);
8807 int b;
8808 };
8809
8810 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8811
8812 static struct type *
8813 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8814 {
8815 struct objfile *objfile = cu->objfile;
8816 struct type *type; /* Type that this function returns. */
8817 struct type *ftype; /* Function that returns above type. */
8818 struct attribute *attr;
8819
8820 type = die_type (die, cu);
8821
8822 /* The die_type call above may have already set the type for this DIE. */
8823 ftype = get_die_type (die, cu);
8824 if (ftype)
8825 return ftype;
8826
8827 ftype = lookup_function_type (type);
8828
8829 /* All functions in C++, Pascal and Java have prototypes. */
8830 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8831 if ((attr && (DW_UNSND (attr) != 0))
8832 || cu->language == language_cplus
8833 || cu->language == language_java
8834 || cu->language == language_pascal)
8835 TYPE_PROTOTYPED (ftype) = 1;
8836 else if (producer_is_realview (cu->producer))
8837 /* RealView does not emit DW_AT_prototyped. We can not
8838 distinguish prototyped and unprototyped functions; default to
8839 prototyped, since that is more common in modern code (and
8840 RealView warns about unprototyped functions). */
8841 TYPE_PROTOTYPED (ftype) = 1;
8842
8843 /* Store the calling convention in the type if it's available in
8844 the subroutine die. Otherwise set the calling convention to
8845 the default value DW_CC_normal. */
8846 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8847 if (attr)
8848 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8849 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8850 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8851 else
8852 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8853
8854 /* We need to add the subroutine type to the die immediately so
8855 we don't infinitely recurse when dealing with parameters
8856 declared as the same subroutine type. */
8857 set_die_type (die, ftype, cu);
8858
8859 if (die->child != NULL)
8860 {
8861 struct type *void_type = objfile_type (objfile)->builtin_void;
8862 struct die_info *child_die;
8863 int nparams, iparams;
8864
8865 /* Count the number of parameters.
8866 FIXME: GDB currently ignores vararg functions, but knows about
8867 vararg member functions. */
8868 nparams = 0;
8869 child_die = die->child;
8870 while (child_die && child_die->tag)
8871 {
8872 if (child_die->tag == DW_TAG_formal_parameter)
8873 nparams++;
8874 else if (child_die->tag == DW_TAG_unspecified_parameters)
8875 TYPE_VARARGS (ftype) = 1;
8876 child_die = sibling_die (child_die);
8877 }
8878
8879 /* Allocate storage for parameters and fill them in. */
8880 TYPE_NFIELDS (ftype) = nparams;
8881 TYPE_FIELDS (ftype) = (struct field *)
8882 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8883
8884 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8885 even if we error out during the parameters reading below. */
8886 for (iparams = 0; iparams < nparams; iparams++)
8887 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8888
8889 iparams = 0;
8890 child_die = die->child;
8891 while (child_die && child_die->tag)
8892 {
8893 if (child_die->tag == DW_TAG_formal_parameter)
8894 {
8895 struct type *arg_type;
8896
8897 /* DWARF version 2 has no clean way to discern C++
8898 static and non-static member functions. G++ helps
8899 GDB by marking the first parameter for non-static
8900 member functions (which is the this pointer) as
8901 artificial. We pass this information to
8902 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8903
8904 DWARF version 3 added DW_AT_object_pointer, which GCC
8905 4.5 does not yet generate. */
8906 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8907 if (attr)
8908 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8909 else
8910 {
8911 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8912
8913 /* GCC/43521: In java, the formal parameter
8914 "this" is sometimes not marked with DW_AT_artificial. */
8915 if (cu->language == language_java)
8916 {
8917 const char *name = dwarf2_name (child_die, cu);
8918
8919 if (name && !strcmp (name, "this"))
8920 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8921 }
8922 }
8923 arg_type = die_type (child_die, cu);
8924
8925 /* RealView does not mark THIS as const, which the testsuite
8926 expects. GCC marks THIS as const in method definitions,
8927 but not in the class specifications (GCC PR 43053). */
8928 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8929 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8930 {
8931 int is_this = 0;
8932 struct dwarf2_cu *arg_cu = cu;
8933 const char *name = dwarf2_name (child_die, cu);
8934
8935 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8936 if (attr)
8937 {
8938 /* If the compiler emits this, use it. */
8939 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8940 is_this = 1;
8941 }
8942 else if (name && strcmp (name, "this") == 0)
8943 /* Function definitions will have the argument names. */
8944 is_this = 1;
8945 else if (name == NULL && iparams == 0)
8946 /* Declarations may not have the names, so like
8947 elsewhere in GDB, assume an artificial first
8948 argument is "this". */
8949 is_this = 1;
8950
8951 if (is_this)
8952 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8953 arg_type, 0);
8954 }
8955
8956 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8957 iparams++;
8958 }
8959 child_die = sibling_die (child_die);
8960 }
8961 }
8962
8963 return ftype;
8964 }
8965
8966 static struct type *
8967 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8968 {
8969 struct objfile *objfile = cu->objfile;
8970 const char *name = NULL;
8971 struct type *this_type, *target_type;
8972
8973 name = dwarf2_full_name (NULL, die, cu);
8974 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8975 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8976 TYPE_NAME (this_type) = (char *) name;
8977 set_die_type (die, this_type, cu);
8978 target_type = die_type (die, cu);
8979 if (target_type != this_type)
8980 TYPE_TARGET_TYPE (this_type) = target_type;
8981 else
8982 {
8983 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8984 spec and cause infinite loops in GDB. */
8985 complaint (&symfile_complaints,
8986 _("Self-referential DW_TAG_typedef "
8987 "- DIE at 0x%x [in module %s]"),
8988 die->offset.sect_off, objfile->name);
8989 TYPE_TARGET_TYPE (this_type) = NULL;
8990 }
8991 return this_type;
8992 }
8993
8994 /* Find a representation of a given base type and install
8995 it in the TYPE field of the die. */
8996
8997 static struct type *
8998 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8999 {
9000 struct objfile *objfile = cu->objfile;
9001 struct type *type;
9002 struct attribute *attr;
9003 int encoding = 0, size = 0;
9004 char *name;
9005 enum type_code code = TYPE_CODE_INT;
9006 int type_flags = 0;
9007 struct type *target_type = NULL;
9008
9009 attr = dwarf2_attr (die, DW_AT_encoding, cu);
9010 if (attr)
9011 {
9012 encoding = DW_UNSND (attr);
9013 }
9014 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9015 if (attr)
9016 {
9017 size = DW_UNSND (attr);
9018 }
9019 name = dwarf2_name (die, cu);
9020 if (!name)
9021 {
9022 complaint (&symfile_complaints,
9023 _("DW_AT_name missing from DW_TAG_base_type"));
9024 }
9025
9026 switch (encoding)
9027 {
9028 case DW_ATE_address:
9029 /* Turn DW_ATE_address into a void * pointer. */
9030 code = TYPE_CODE_PTR;
9031 type_flags |= TYPE_FLAG_UNSIGNED;
9032 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
9033 break;
9034 case DW_ATE_boolean:
9035 code = TYPE_CODE_BOOL;
9036 type_flags |= TYPE_FLAG_UNSIGNED;
9037 break;
9038 case DW_ATE_complex_float:
9039 code = TYPE_CODE_COMPLEX;
9040 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
9041 break;
9042 case DW_ATE_decimal_float:
9043 code = TYPE_CODE_DECFLOAT;
9044 break;
9045 case DW_ATE_float:
9046 code = TYPE_CODE_FLT;
9047 break;
9048 case DW_ATE_signed:
9049 break;
9050 case DW_ATE_unsigned:
9051 type_flags |= TYPE_FLAG_UNSIGNED;
9052 if (cu->language == language_fortran
9053 && name
9054 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
9055 code = TYPE_CODE_CHAR;
9056 break;
9057 case DW_ATE_signed_char:
9058 if (cu->language == language_ada || cu->language == language_m2
9059 || cu->language == language_pascal
9060 || cu->language == language_fortran)
9061 code = TYPE_CODE_CHAR;
9062 break;
9063 case DW_ATE_unsigned_char:
9064 if (cu->language == language_ada || cu->language == language_m2
9065 || cu->language == language_pascal
9066 || cu->language == language_fortran)
9067 code = TYPE_CODE_CHAR;
9068 type_flags |= TYPE_FLAG_UNSIGNED;
9069 break;
9070 case DW_ATE_UTF:
9071 /* We just treat this as an integer and then recognize the
9072 type by name elsewhere. */
9073 break;
9074
9075 default:
9076 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
9077 dwarf_type_encoding_name (encoding));
9078 break;
9079 }
9080
9081 type = init_type (code, size, type_flags, NULL, objfile);
9082 TYPE_NAME (type) = name;
9083 TYPE_TARGET_TYPE (type) = target_type;
9084
9085 if (name && strcmp (name, "char") == 0)
9086 TYPE_NOSIGN (type) = 1;
9087
9088 return set_die_type (die, type, cu);
9089 }
9090
9091 /* Read the given DW_AT_subrange DIE. */
9092
9093 static struct type *
9094 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
9095 {
9096 struct type *base_type;
9097 struct type *range_type;
9098 struct attribute *attr;
9099 LONGEST low, high;
9100 int low_default_is_valid;
9101 char *name;
9102 LONGEST negative_mask;
9103
9104 base_type = die_type (die, cu);
9105 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
9106 check_typedef (base_type);
9107
9108 /* The die_type call above may have already set the type for this DIE. */
9109 range_type = get_die_type (die, cu);
9110 if (range_type)
9111 return range_type;
9112
9113 /* Set LOW_DEFAULT_IS_VALID if current language and DWARF version allow
9114 omitting DW_AT_lower_bound. */
9115 switch (cu->language)
9116 {
9117 case language_c:
9118 case language_cplus:
9119 low = 0;
9120 low_default_is_valid = 1;
9121 break;
9122 case language_fortran:
9123 low = 1;
9124 low_default_is_valid = 1;
9125 break;
9126 case language_d:
9127 case language_java:
9128 case language_objc:
9129 low = 0;
9130 low_default_is_valid = (cu->header.version >= 4);
9131 break;
9132 case language_ada:
9133 case language_m2:
9134 case language_pascal:
9135 low = 1;
9136 low_default_is_valid = (cu->header.version >= 4);
9137 break;
9138 default:
9139 low = 0;
9140 low_default_is_valid = 0;
9141 break;
9142 }
9143
9144 /* FIXME: For variable sized arrays either of these could be
9145 a variable rather than a constant value. We'll allow it,
9146 but we don't know how to handle it. */
9147 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
9148 if (attr)
9149 low = dwarf2_get_attr_constant_value (attr, low);
9150 else if (!low_default_is_valid)
9151 complaint (&symfile_complaints, _("Missing DW_AT_lower_bound "
9152 "- DIE at 0x%x [in module %s]"),
9153 die->offset.sect_off, cu->objfile->name);
9154
9155 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
9156 if (attr)
9157 {
9158 if (attr_form_is_block (attr) || is_ref_attr (attr))
9159 {
9160 /* GCC encodes arrays with unspecified or dynamic length
9161 with a DW_FORM_block1 attribute or a reference attribute.
9162 FIXME: GDB does not yet know how to handle dynamic
9163 arrays properly, treat them as arrays with unspecified
9164 length for now.
9165
9166 FIXME: jimb/2003-09-22: GDB does not really know
9167 how to handle arrays of unspecified length
9168 either; we just represent them as zero-length
9169 arrays. Choose an appropriate upper bound given
9170 the lower bound we've computed above. */
9171 high = low - 1;
9172 }
9173 else
9174 high = dwarf2_get_attr_constant_value (attr, 1);
9175 }
9176 else
9177 {
9178 attr = dwarf2_attr (die, DW_AT_count, cu);
9179 if (attr)
9180 {
9181 int count = dwarf2_get_attr_constant_value (attr, 1);
9182 high = low + count - 1;
9183 }
9184 else
9185 {
9186 /* Unspecified array length. */
9187 high = low - 1;
9188 }
9189 }
9190
9191 /* Dwarf-2 specifications explicitly allows to create subrange types
9192 without specifying a base type.
9193 In that case, the base type must be set to the type of
9194 the lower bound, upper bound or count, in that order, if any of these
9195 three attributes references an object that has a type.
9196 If no base type is found, the Dwarf-2 specifications say that
9197 a signed integer type of size equal to the size of an address should
9198 be used.
9199 For the following C code: `extern char gdb_int [];'
9200 GCC produces an empty range DIE.
9201 FIXME: muller/2010-05-28: Possible references to object for low bound,
9202 high bound or count are not yet handled by this code. */
9203 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9204 {
9205 struct objfile *objfile = cu->objfile;
9206 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9207 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9208 struct type *int_type = objfile_type (objfile)->builtin_int;
9209
9210 /* Test "int", "long int", and "long long int" objfile types,
9211 and select the first one having a size above or equal to the
9212 architecture address size. */
9213 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9214 base_type = int_type;
9215 else
9216 {
9217 int_type = objfile_type (objfile)->builtin_long;
9218 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9219 base_type = int_type;
9220 else
9221 {
9222 int_type = objfile_type (objfile)->builtin_long_long;
9223 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9224 base_type = int_type;
9225 }
9226 }
9227 }
9228
9229 negative_mask =
9230 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9231 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9232 low |= negative_mask;
9233 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9234 high |= negative_mask;
9235
9236 range_type = create_range_type (NULL, base_type, low, high);
9237
9238 /* Mark arrays with dynamic length at least as an array of unspecified
9239 length. GDB could check the boundary but before it gets implemented at
9240 least allow accessing the array elements. */
9241 if (attr && attr_form_is_block (attr))
9242 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9243
9244 /* Ada expects an empty array on no boundary attributes. */
9245 if (attr == NULL && cu->language != language_ada)
9246 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9247
9248 name = dwarf2_name (die, cu);
9249 if (name)
9250 TYPE_NAME (range_type) = name;
9251
9252 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9253 if (attr)
9254 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9255
9256 set_die_type (die, range_type, cu);
9257
9258 /* set_die_type should be already done. */
9259 set_descriptive_type (range_type, die, cu);
9260
9261 return range_type;
9262 }
9263
9264 static struct type *
9265 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9266 {
9267 struct type *type;
9268
9269 /* For now, we only support the C meaning of an unspecified type: void. */
9270
9271 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9272 TYPE_NAME (type) = dwarf2_name (die, cu);
9273
9274 return set_die_type (die, type, cu);
9275 }
9276
9277 /* Trivial hash function for die_info: the hash value of a DIE
9278 is its offset in .debug_info for this objfile. */
9279
9280 static hashval_t
9281 die_hash (const void *item)
9282 {
9283 const struct die_info *die = item;
9284
9285 return die->offset.sect_off;
9286 }
9287
9288 /* Trivial comparison function for die_info structures: two DIEs
9289 are equal if they have the same offset. */
9290
9291 static int
9292 die_eq (const void *item_lhs, const void *item_rhs)
9293 {
9294 const struct die_info *die_lhs = item_lhs;
9295 const struct die_info *die_rhs = item_rhs;
9296
9297 return die_lhs->offset.sect_off == die_rhs->offset.sect_off;
9298 }
9299
9300 /* Read a whole compilation unit into a linked list of dies. */
9301
9302 static struct die_info *
9303 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9304 {
9305 struct die_reader_specs reader_specs;
9306 int read_abbrevs = 0;
9307 struct cleanup *back_to = NULL;
9308 struct die_info *die;
9309
9310 if (cu->dwarf2_abbrevs == NULL)
9311 {
9312 dwarf2_read_abbrevs (cu);
9313 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9314 read_abbrevs = 1;
9315 }
9316
9317 gdb_assert (cu->die_hash == NULL);
9318 cu->die_hash
9319 = htab_create_alloc_ex (cu->header.length / 12,
9320 die_hash,
9321 die_eq,
9322 NULL,
9323 &cu->comp_unit_obstack,
9324 hashtab_obstack_allocate,
9325 dummy_obstack_deallocate);
9326
9327 init_cu_die_reader (&reader_specs, cu);
9328
9329 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9330
9331 if (read_abbrevs)
9332 do_cleanups (back_to);
9333
9334 return die;
9335 }
9336
9337 /* Main entry point for reading a DIE and all children.
9338 Read the DIE and dump it if requested. */
9339
9340 static struct die_info *
9341 read_die_and_children (const struct die_reader_specs *reader,
9342 gdb_byte *info_ptr,
9343 gdb_byte **new_info_ptr,
9344 struct die_info *parent)
9345 {
9346 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9347 new_info_ptr, parent);
9348
9349 if (dwarf2_die_debug)
9350 {
9351 fprintf_unfiltered (gdb_stdlog,
9352 "\nRead die from %s of %s:\n",
9353 (reader->cu->per_cu->debug_types_section
9354 ? ".debug_types"
9355 : ".debug_info"),
9356 reader->abfd->filename);
9357 dump_die (result, dwarf2_die_debug);
9358 }
9359
9360 return result;
9361 }
9362
9363 /* Read a single die and all its descendents. Set the die's sibling
9364 field to NULL; set other fields in the die correctly, and set all
9365 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9366 location of the info_ptr after reading all of those dies. PARENT
9367 is the parent of the die in question. */
9368
9369 static struct die_info *
9370 read_die_and_children_1 (const struct die_reader_specs *reader,
9371 gdb_byte *info_ptr,
9372 gdb_byte **new_info_ptr,
9373 struct die_info *parent)
9374 {
9375 struct die_info *die;
9376 gdb_byte *cur_ptr;
9377 int has_children;
9378
9379 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9380 if (die == NULL)
9381 {
9382 *new_info_ptr = cur_ptr;
9383 return NULL;
9384 }
9385 store_in_ref_table (die, reader->cu);
9386
9387 if (has_children)
9388 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9389 else
9390 {
9391 die->child = NULL;
9392 *new_info_ptr = cur_ptr;
9393 }
9394
9395 die->sibling = NULL;
9396 die->parent = parent;
9397 return die;
9398 }
9399
9400 /* Read a die, all of its descendents, and all of its siblings; set
9401 all of the fields of all of the dies correctly. Arguments are as
9402 in read_die_and_children. */
9403
9404 static struct die_info *
9405 read_die_and_siblings (const struct die_reader_specs *reader,
9406 gdb_byte *info_ptr,
9407 gdb_byte **new_info_ptr,
9408 struct die_info *parent)
9409 {
9410 struct die_info *first_die, *last_sibling;
9411 gdb_byte *cur_ptr;
9412
9413 cur_ptr = info_ptr;
9414 first_die = last_sibling = NULL;
9415
9416 while (1)
9417 {
9418 struct die_info *die
9419 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9420
9421 if (die == NULL)
9422 {
9423 *new_info_ptr = cur_ptr;
9424 return first_die;
9425 }
9426
9427 if (!first_die)
9428 first_die = die;
9429 else
9430 last_sibling->sibling = die;
9431
9432 last_sibling = die;
9433 }
9434 }
9435
9436 /* Read the die from the .debug_info section buffer. Set DIEP to
9437 point to a newly allocated die with its information, except for its
9438 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9439 whether the die has children or not. */
9440
9441 static gdb_byte *
9442 read_full_die (const struct die_reader_specs *reader,
9443 struct die_info **diep, gdb_byte *info_ptr,
9444 int *has_children)
9445 {
9446 unsigned int abbrev_number, bytes_read, i;
9447 sect_offset offset;
9448 struct abbrev_info *abbrev;
9449 struct die_info *die;
9450 struct dwarf2_cu *cu = reader->cu;
9451 bfd *abfd = reader->abfd;
9452
9453 offset.sect_off = info_ptr - reader->buffer;
9454 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9455 info_ptr += bytes_read;
9456 if (!abbrev_number)
9457 {
9458 *diep = NULL;
9459 *has_children = 0;
9460 return info_ptr;
9461 }
9462
9463 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9464 if (!abbrev)
9465 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9466 abbrev_number,
9467 bfd_get_filename (abfd));
9468
9469 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9470 die->offset = offset;
9471 die->tag = abbrev->tag;
9472 die->abbrev = abbrev_number;
9473
9474 die->num_attrs = abbrev->num_attrs;
9475
9476 for (i = 0; i < abbrev->num_attrs; ++i)
9477 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9478 abfd, info_ptr, cu);
9479
9480 *diep = die;
9481 *has_children = abbrev->has_children;
9482 return info_ptr;
9483 }
9484
9485 /* In DWARF version 2, the description of the debugging information is
9486 stored in a separate .debug_abbrev section. Before we read any
9487 dies from a section we read in all abbreviations and install them
9488 in a hash table. This function also sets flags in CU describing
9489 the data found in the abbrev table. */
9490
9491 static void
9492 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9493 {
9494 bfd *abfd = cu->objfile->obfd;
9495 struct comp_unit_head *cu_header = &cu->header;
9496 gdb_byte *abbrev_ptr;
9497 struct abbrev_info *cur_abbrev;
9498 unsigned int abbrev_number, bytes_read, abbrev_name;
9499 unsigned int abbrev_form, hash_number;
9500 struct attr_abbrev *cur_attrs;
9501 unsigned int allocated_attrs;
9502
9503 /* Initialize dwarf2 abbrevs. */
9504 obstack_init (&cu->abbrev_obstack);
9505 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9506 (ABBREV_HASH_SIZE
9507 * sizeof (struct abbrev_info *)));
9508 memset (cu->dwarf2_abbrevs, 0,
9509 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9510
9511 dwarf2_read_section (dwarf2_per_objfile->objfile,
9512 &dwarf2_per_objfile->abbrev);
9513 abbrev_ptr = (dwarf2_per_objfile->abbrev.buffer
9514 + cu_header->abbrev_offset.sect_off);
9515 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9516 abbrev_ptr += bytes_read;
9517
9518 allocated_attrs = ATTR_ALLOC_CHUNK;
9519 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9520
9521 /* Loop until we reach an abbrev number of 0. */
9522 while (abbrev_number)
9523 {
9524 cur_abbrev = dwarf_alloc_abbrev (cu);
9525
9526 /* read in abbrev header */
9527 cur_abbrev->number = abbrev_number;
9528 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9529 abbrev_ptr += bytes_read;
9530 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9531 abbrev_ptr += 1;
9532
9533 /* now read in declarations */
9534 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9535 abbrev_ptr += bytes_read;
9536 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9537 abbrev_ptr += bytes_read;
9538 while (abbrev_name)
9539 {
9540 if (cur_abbrev->num_attrs == allocated_attrs)
9541 {
9542 allocated_attrs += ATTR_ALLOC_CHUNK;
9543 cur_attrs
9544 = xrealloc (cur_attrs, (allocated_attrs
9545 * sizeof (struct attr_abbrev)));
9546 }
9547
9548 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9549 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9550 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9551 abbrev_ptr += bytes_read;
9552 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9553 abbrev_ptr += bytes_read;
9554 }
9555
9556 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9557 (cur_abbrev->num_attrs
9558 * sizeof (struct attr_abbrev)));
9559 memcpy (cur_abbrev->attrs, cur_attrs,
9560 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9561
9562 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9563 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9564 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9565
9566 /* Get next abbreviation.
9567 Under Irix6 the abbreviations for a compilation unit are not
9568 always properly terminated with an abbrev number of 0.
9569 Exit loop if we encounter an abbreviation which we have
9570 already read (which means we are about to read the abbreviations
9571 for the next compile unit) or if the end of the abbreviation
9572 table is reached. */
9573 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9574 >= dwarf2_per_objfile->abbrev.size)
9575 break;
9576 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9577 abbrev_ptr += bytes_read;
9578 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9579 break;
9580 }
9581
9582 xfree (cur_attrs);
9583 }
9584
9585 /* Release the memory used by the abbrev table for a compilation unit. */
9586
9587 static void
9588 dwarf2_free_abbrev_table (void *ptr_to_cu)
9589 {
9590 struct dwarf2_cu *cu = ptr_to_cu;
9591
9592 obstack_free (&cu->abbrev_obstack, NULL);
9593 cu->dwarf2_abbrevs = NULL;
9594 }
9595
9596 /* Lookup an abbrev_info structure in the abbrev hash table. */
9597
9598 static struct abbrev_info *
9599 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9600 {
9601 unsigned int hash_number;
9602 struct abbrev_info *abbrev;
9603
9604 hash_number = number % ABBREV_HASH_SIZE;
9605 abbrev = cu->dwarf2_abbrevs[hash_number];
9606
9607 while (abbrev)
9608 {
9609 if (abbrev->number == number)
9610 return abbrev;
9611 else
9612 abbrev = abbrev->next;
9613 }
9614 return NULL;
9615 }
9616
9617 /* Returns nonzero if TAG represents a type that we might generate a partial
9618 symbol for. */
9619
9620 static int
9621 is_type_tag_for_partial (int tag)
9622 {
9623 switch (tag)
9624 {
9625 #if 0
9626 /* Some types that would be reasonable to generate partial symbols for,
9627 that we don't at present. */
9628 case DW_TAG_array_type:
9629 case DW_TAG_file_type:
9630 case DW_TAG_ptr_to_member_type:
9631 case DW_TAG_set_type:
9632 case DW_TAG_string_type:
9633 case DW_TAG_subroutine_type:
9634 #endif
9635 case DW_TAG_base_type:
9636 case DW_TAG_class_type:
9637 case DW_TAG_interface_type:
9638 case DW_TAG_enumeration_type:
9639 case DW_TAG_structure_type:
9640 case DW_TAG_subrange_type:
9641 case DW_TAG_typedef:
9642 case DW_TAG_union_type:
9643 return 1;
9644 default:
9645 return 0;
9646 }
9647 }
9648
9649 /* Load all DIEs that are interesting for partial symbols into memory. */
9650
9651 static struct partial_die_info *
9652 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9653 int building_psymtab, struct dwarf2_cu *cu)
9654 {
9655 struct objfile *objfile = cu->objfile;
9656 struct partial_die_info *part_die;
9657 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9658 struct abbrev_info *abbrev;
9659 unsigned int bytes_read;
9660 unsigned int load_all = 0;
9661 int nesting_level = 1;
9662
9663 parent_die = NULL;
9664 last_die = NULL;
9665
9666 gdb_assert (cu->per_cu != NULL);
9667 if (cu->per_cu->load_all_dies)
9668 load_all = 1;
9669
9670 cu->partial_dies
9671 = htab_create_alloc_ex (cu->header.length / 12,
9672 partial_die_hash,
9673 partial_die_eq,
9674 NULL,
9675 &cu->comp_unit_obstack,
9676 hashtab_obstack_allocate,
9677 dummy_obstack_deallocate);
9678
9679 part_die = obstack_alloc (&cu->comp_unit_obstack,
9680 sizeof (struct partial_die_info));
9681
9682 while (1)
9683 {
9684 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9685
9686 /* A NULL abbrev means the end of a series of children. */
9687 if (abbrev == NULL)
9688 {
9689 if (--nesting_level == 0)
9690 {
9691 /* PART_DIE was probably the last thing allocated on the
9692 comp_unit_obstack, so we could call obstack_free
9693 here. We don't do that because the waste is small,
9694 and will be cleaned up when we're done with this
9695 compilation unit. This way, we're also more robust
9696 against other users of the comp_unit_obstack. */
9697 return first_die;
9698 }
9699 info_ptr += bytes_read;
9700 last_die = parent_die;
9701 parent_die = parent_die->die_parent;
9702 continue;
9703 }
9704
9705 /* Check for template arguments. We never save these; if
9706 they're seen, we just mark the parent, and go on our way. */
9707 if (parent_die != NULL
9708 && cu->language == language_cplus
9709 && (abbrev->tag == DW_TAG_template_type_param
9710 || abbrev->tag == DW_TAG_template_value_param))
9711 {
9712 parent_die->has_template_arguments = 1;
9713
9714 if (!load_all)
9715 {
9716 /* We don't need a partial DIE for the template argument. */
9717 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9718 cu);
9719 continue;
9720 }
9721 }
9722
9723 /* We only recurse into c++ subprograms looking for template arguments.
9724 Skip their other children. */
9725 if (!load_all
9726 && cu->language == language_cplus
9727 && parent_die != NULL
9728 && parent_die->tag == DW_TAG_subprogram)
9729 {
9730 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9731 continue;
9732 }
9733
9734 /* Check whether this DIE is interesting enough to save. Normally
9735 we would not be interested in members here, but there may be
9736 later variables referencing them via DW_AT_specification (for
9737 static members). */
9738 if (!load_all
9739 && !is_type_tag_for_partial (abbrev->tag)
9740 && abbrev->tag != DW_TAG_constant
9741 && abbrev->tag != DW_TAG_enumerator
9742 && abbrev->tag != DW_TAG_subprogram
9743 && abbrev->tag != DW_TAG_lexical_block
9744 && abbrev->tag != DW_TAG_variable
9745 && abbrev->tag != DW_TAG_namespace
9746 && abbrev->tag != DW_TAG_module
9747 && abbrev->tag != DW_TAG_member)
9748 {
9749 /* Otherwise we skip to the next sibling, if any. */
9750 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9751 continue;
9752 }
9753
9754 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9755 buffer, info_ptr, cu);
9756
9757 /* This two-pass algorithm for processing partial symbols has a
9758 high cost in cache pressure. Thus, handle some simple cases
9759 here which cover the majority of C partial symbols. DIEs
9760 which neither have specification tags in them, nor could have
9761 specification tags elsewhere pointing at them, can simply be
9762 processed and discarded.
9763
9764 This segment is also optional; scan_partial_symbols and
9765 add_partial_symbol will handle these DIEs if we chain
9766 them in normally. When compilers which do not emit large
9767 quantities of duplicate debug information are more common,
9768 this code can probably be removed. */
9769
9770 /* Any complete simple types at the top level (pretty much all
9771 of them, for a language without namespaces), can be processed
9772 directly. */
9773 if (parent_die == NULL
9774 && part_die->has_specification == 0
9775 && part_die->is_declaration == 0
9776 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9777 || part_die->tag == DW_TAG_base_type
9778 || part_die->tag == DW_TAG_subrange_type))
9779 {
9780 if (building_psymtab && part_die->name != NULL)
9781 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9782 VAR_DOMAIN, LOC_TYPEDEF,
9783 &objfile->static_psymbols,
9784 0, (CORE_ADDR) 0, cu->language, objfile);
9785 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9786 continue;
9787 }
9788
9789 /* The exception for DW_TAG_typedef with has_children above is
9790 a workaround of GCC PR debug/47510. In the case of this complaint
9791 type_name_no_tag_or_error will error on such types later.
9792
9793 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9794 it could not find the child DIEs referenced later, this is checked
9795 above. In correct DWARF DW_TAG_typedef should have no children. */
9796
9797 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9798 complaint (&symfile_complaints,
9799 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9800 "- DIE at 0x%x [in module %s]"),
9801 part_die->offset.sect_off, objfile->name);
9802
9803 /* If we're at the second level, and we're an enumerator, and
9804 our parent has no specification (meaning possibly lives in a
9805 namespace elsewhere), then we can add the partial symbol now
9806 instead of queueing it. */
9807 if (part_die->tag == DW_TAG_enumerator
9808 && parent_die != NULL
9809 && parent_die->die_parent == NULL
9810 && parent_die->tag == DW_TAG_enumeration_type
9811 && parent_die->has_specification == 0)
9812 {
9813 if (part_die->name == NULL)
9814 complaint (&symfile_complaints,
9815 _("malformed enumerator DIE ignored"));
9816 else if (building_psymtab)
9817 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9818 VAR_DOMAIN, LOC_CONST,
9819 (cu->language == language_cplus
9820 || cu->language == language_java)
9821 ? &objfile->global_psymbols
9822 : &objfile->static_psymbols,
9823 0, (CORE_ADDR) 0, cu->language, objfile);
9824
9825 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9826 continue;
9827 }
9828
9829 /* We'll save this DIE so link it in. */
9830 part_die->die_parent = parent_die;
9831 part_die->die_sibling = NULL;
9832 part_die->die_child = NULL;
9833
9834 if (last_die && last_die == parent_die)
9835 last_die->die_child = part_die;
9836 else if (last_die)
9837 last_die->die_sibling = part_die;
9838
9839 last_die = part_die;
9840
9841 if (first_die == NULL)
9842 first_die = part_die;
9843
9844 /* Maybe add the DIE to the hash table. Not all DIEs that we
9845 find interesting need to be in the hash table, because we
9846 also have the parent/sibling/child chains; only those that we
9847 might refer to by offset later during partial symbol reading.
9848
9849 For now this means things that might have be the target of a
9850 DW_AT_specification, DW_AT_abstract_origin, or
9851 DW_AT_extension. DW_AT_extension will refer only to
9852 namespaces; DW_AT_abstract_origin refers to functions (and
9853 many things under the function DIE, but we do not recurse
9854 into function DIEs during partial symbol reading) and
9855 possibly variables as well; DW_AT_specification refers to
9856 declarations. Declarations ought to have the DW_AT_declaration
9857 flag. It happens that GCC forgets to put it in sometimes, but
9858 only for functions, not for types.
9859
9860 Adding more things than necessary to the hash table is harmless
9861 except for the performance cost. Adding too few will result in
9862 wasted time in find_partial_die, when we reread the compilation
9863 unit with load_all_dies set. */
9864
9865 if (load_all
9866 || abbrev->tag == DW_TAG_constant
9867 || abbrev->tag == DW_TAG_subprogram
9868 || abbrev->tag == DW_TAG_variable
9869 || abbrev->tag == DW_TAG_namespace
9870 || part_die->is_declaration)
9871 {
9872 void **slot;
9873
9874 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9875 part_die->offset.sect_off, INSERT);
9876 *slot = part_die;
9877 }
9878
9879 part_die = obstack_alloc (&cu->comp_unit_obstack,
9880 sizeof (struct partial_die_info));
9881
9882 /* For some DIEs we want to follow their children (if any). For C
9883 we have no reason to follow the children of structures; for other
9884 languages we have to, so that we can get at method physnames
9885 to infer fully qualified class names, for DW_AT_specification,
9886 and for C++ template arguments. For C++, we also look one level
9887 inside functions to find template arguments (if the name of the
9888 function does not already contain the template arguments).
9889
9890 For Ada, we need to scan the children of subprograms and lexical
9891 blocks as well because Ada allows the definition of nested
9892 entities that could be interesting for the debugger, such as
9893 nested subprograms for instance. */
9894 if (last_die->has_children
9895 && (load_all
9896 || last_die->tag == DW_TAG_namespace
9897 || last_die->tag == DW_TAG_module
9898 || last_die->tag == DW_TAG_enumeration_type
9899 || (cu->language == language_cplus
9900 && last_die->tag == DW_TAG_subprogram
9901 && (last_die->name == NULL
9902 || strchr (last_die->name, '<') == NULL))
9903 || (cu->language != language_c
9904 && (last_die->tag == DW_TAG_class_type
9905 || last_die->tag == DW_TAG_interface_type
9906 || last_die->tag == DW_TAG_structure_type
9907 || last_die->tag == DW_TAG_union_type))
9908 || (cu->language == language_ada
9909 && (last_die->tag == DW_TAG_subprogram
9910 || last_die->tag == DW_TAG_lexical_block))))
9911 {
9912 nesting_level++;
9913 parent_die = last_die;
9914 continue;
9915 }
9916
9917 /* Otherwise we skip to the next sibling, if any. */
9918 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9919
9920 /* Back to the top, do it again. */
9921 }
9922 }
9923
9924 /* Read a minimal amount of information into the minimal die structure. */
9925
9926 static gdb_byte *
9927 read_partial_die (struct partial_die_info *part_die,
9928 struct abbrev_info *abbrev,
9929 unsigned int abbrev_len, bfd *abfd,
9930 gdb_byte *buffer, gdb_byte *info_ptr,
9931 struct dwarf2_cu *cu)
9932 {
9933 struct objfile *objfile = cu->objfile;
9934 unsigned int i;
9935 struct attribute attr;
9936 int has_low_pc_attr = 0;
9937 int has_high_pc_attr = 0;
9938 int high_pc_relative = 0;
9939
9940 memset (part_die, 0, sizeof (struct partial_die_info));
9941
9942 part_die->offset.sect_off = info_ptr - buffer;
9943
9944 info_ptr += abbrev_len;
9945
9946 if (abbrev == NULL)
9947 return info_ptr;
9948
9949 part_die->tag = abbrev->tag;
9950 part_die->has_children = abbrev->has_children;
9951
9952 for (i = 0; i < abbrev->num_attrs; ++i)
9953 {
9954 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9955
9956 /* Store the data if it is of an attribute we want to keep in a
9957 partial symbol table. */
9958 switch (attr.name)
9959 {
9960 case DW_AT_name:
9961 switch (part_die->tag)
9962 {
9963 case DW_TAG_compile_unit:
9964 case DW_TAG_type_unit:
9965 /* Compilation units have a DW_AT_name that is a filename, not
9966 a source language identifier. */
9967 case DW_TAG_enumeration_type:
9968 case DW_TAG_enumerator:
9969 /* These tags always have simple identifiers already; no need
9970 to canonicalize them. */
9971 part_die->name = DW_STRING (&attr);
9972 break;
9973 default:
9974 part_die->name
9975 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9976 &objfile->objfile_obstack);
9977 break;
9978 }
9979 break;
9980 case DW_AT_linkage_name:
9981 case DW_AT_MIPS_linkage_name:
9982 /* Note that both forms of linkage name might appear. We
9983 assume they will be the same, and we only store the last
9984 one we see. */
9985 if (cu->language == language_ada)
9986 part_die->name = DW_STRING (&attr);
9987 part_die->linkage_name = DW_STRING (&attr);
9988 break;
9989 case DW_AT_low_pc:
9990 has_low_pc_attr = 1;
9991 part_die->lowpc = DW_ADDR (&attr);
9992 break;
9993 case DW_AT_high_pc:
9994 has_high_pc_attr = 1;
9995 if (attr.form == DW_FORM_addr)
9996 part_die->highpc = DW_ADDR (&attr);
9997 else
9998 {
9999 high_pc_relative = 1;
10000 part_die->highpc = DW_UNSND (&attr);
10001 }
10002 break;
10003 case DW_AT_location:
10004 /* Support the .debug_loc offsets. */
10005 if (attr_form_is_block (&attr))
10006 {
10007 part_die->locdesc = DW_BLOCK (&attr);
10008 }
10009 else if (attr_form_is_section_offset (&attr))
10010 {
10011 dwarf2_complex_location_expr_complaint ();
10012 }
10013 else
10014 {
10015 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
10016 "partial symbol information");
10017 }
10018 break;
10019 case DW_AT_external:
10020 part_die->is_external = DW_UNSND (&attr);
10021 break;
10022 case DW_AT_declaration:
10023 part_die->is_declaration = DW_UNSND (&attr);
10024 break;
10025 case DW_AT_type:
10026 part_die->has_type = 1;
10027 break;
10028 case DW_AT_abstract_origin:
10029 case DW_AT_specification:
10030 case DW_AT_extension:
10031 part_die->has_specification = 1;
10032 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
10033 break;
10034 case DW_AT_sibling:
10035 /* Ignore absolute siblings, they might point outside of
10036 the current compile unit. */
10037 if (attr.form == DW_FORM_ref_addr)
10038 complaint (&symfile_complaints,
10039 _("ignoring absolute DW_AT_sibling"));
10040 else
10041 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr).sect_off;
10042 break;
10043 case DW_AT_byte_size:
10044 part_die->has_byte_size = 1;
10045 break;
10046 case DW_AT_calling_convention:
10047 /* DWARF doesn't provide a way to identify a program's source-level
10048 entry point. DW_AT_calling_convention attributes are only meant
10049 to describe functions' calling conventions.
10050
10051 However, because it's a necessary piece of information in
10052 Fortran, and because DW_CC_program is the only piece of debugging
10053 information whose definition refers to a 'main program' at all,
10054 several compilers have begun marking Fortran main programs with
10055 DW_CC_program --- even when those functions use the standard
10056 calling conventions.
10057
10058 So until DWARF specifies a way to provide this information and
10059 compilers pick up the new representation, we'll support this
10060 practice. */
10061 if (DW_UNSND (&attr) == DW_CC_program
10062 && cu->language == language_fortran)
10063 {
10064 set_main_name (part_die->name);
10065
10066 /* As this DIE has a static linkage the name would be difficult
10067 to look up later. */
10068 language_of_main = language_fortran;
10069 }
10070 break;
10071 case DW_AT_inline:
10072 if (DW_UNSND (&attr) == DW_INL_inlined
10073 || DW_UNSND (&attr) == DW_INL_declared_inlined)
10074 part_die->may_be_inlined = 1;
10075 break;
10076 default:
10077 break;
10078 }
10079 }
10080
10081 if (high_pc_relative)
10082 part_die->highpc += part_die->lowpc;
10083
10084 if (has_low_pc_attr && has_high_pc_attr)
10085 {
10086 /* When using the GNU linker, .gnu.linkonce. sections are used to
10087 eliminate duplicate copies of functions and vtables and such.
10088 The linker will arbitrarily choose one and discard the others.
10089 The AT_*_pc values for such functions refer to local labels in
10090 these sections. If the section from that file was discarded, the
10091 labels are not in the output, so the relocs get a value of 0.
10092 If this is a discarded function, mark the pc bounds as invalid,
10093 so that GDB will ignore it. */
10094 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
10095 {
10096 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10097
10098 complaint (&symfile_complaints,
10099 _("DW_AT_low_pc %s is zero "
10100 "for DIE at 0x%x [in module %s]"),
10101 paddress (gdbarch, part_die->lowpc),
10102 part_die->offset.sect_off, objfile->name);
10103 }
10104 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
10105 else if (part_die->lowpc >= part_die->highpc)
10106 {
10107 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10108
10109 complaint (&symfile_complaints,
10110 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
10111 "for DIE at 0x%x [in module %s]"),
10112 paddress (gdbarch, part_die->lowpc),
10113 paddress (gdbarch, part_die->highpc),
10114 part_die->offset.sect_off, objfile->name);
10115 }
10116 else
10117 part_die->has_pc_info = 1;
10118 }
10119
10120 return info_ptr;
10121 }
10122
10123 /* Find a cached partial DIE at OFFSET in CU. */
10124
10125 static struct partial_die_info *
10126 find_partial_die_in_comp_unit (sect_offset offset, struct dwarf2_cu *cu)
10127 {
10128 struct partial_die_info *lookup_die = NULL;
10129 struct partial_die_info part_die;
10130
10131 part_die.offset = offset;
10132 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die,
10133 offset.sect_off);
10134
10135 return lookup_die;
10136 }
10137
10138 /* Find a partial DIE at OFFSET, which may or may not be in CU,
10139 except in the case of .debug_types DIEs which do not reference
10140 outside their CU (they do however referencing other types via
10141 DW_FORM_ref_sig8). */
10142
10143 static struct partial_die_info *
10144 find_partial_die (sect_offset offset, struct dwarf2_cu *cu)
10145 {
10146 struct objfile *objfile = cu->objfile;
10147 struct dwarf2_per_cu_data *per_cu = NULL;
10148 struct partial_die_info *pd = NULL;
10149
10150 if (offset_in_cu_p (&cu->header, offset))
10151 {
10152 pd = find_partial_die_in_comp_unit (offset, cu);
10153 if (pd != NULL)
10154 return pd;
10155 /* We missed recording what we needed.
10156 Load all dies and try again. */
10157 per_cu = cu->per_cu;
10158 }
10159 else
10160 {
10161 /* TUs don't reference other CUs/TUs (except via type signatures). */
10162 if (cu->per_cu->debug_types_section)
10163 {
10164 error (_("Dwarf Error: Type Unit at offset 0x%lx contains"
10165 " external reference to offset 0x%lx [in module %s].\n"),
10166 (long) cu->header.offset.sect_off, (long) offset.sect_off,
10167 bfd_get_filename (objfile->obfd));
10168 }
10169 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
10170
10171 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
10172 load_partial_comp_unit (per_cu);
10173
10174 per_cu->cu->last_used = 0;
10175 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10176 }
10177
10178 if (pd == NULL && per_cu->load_all_dies == 0)
10179 {
10180 struct cleanup *back_to;
10181 struct partial_die_info comp_unit_die;
10182 struct abbrev_info *abbrev;
10183 unsigned int bytes_read;
10184 char *info_ptr;
10185 struct dwarf2_section_info *sec;
10186
10187 per_cu->load_all_dies = 1;
10188
10189 if (per_cu->debug_types_section)
10190 sec = per_cu->debug_types_section;
10191 else
10192 sec = &dwarf2_per_objfile->info;
10193
10194 /* Re-read the DIEs, this time reading all of them.
10195 NOTE: We don't discard the previous set of DIEs.
10196 This doesn't happen very often so it's (hopefully) not a problem. */
10197 back_to = make_cleanup (null_cleanup, 0);
10198 if (per_cu->cu->dwarf2_abbrevs == NULL)
10199 {
10200 dwarf2_read_abbrevs (per_cu->cu);
10201 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10202 }
10203 info_ptr = (sec->buffer
10204 + per_cu->cu->header.offset.sect_off
10205 + per_cu->cu->header.first_die_offset.cu_off);
10206 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10207 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10208 objfile->obfd, sec->buffer, info_ptr,
10209 per_cu->cu);
10210 if (comp_unit_die.has_children)
10211 load_partial_dies (objfile->obfd, sec->buffer, info_ptr, 0,
10212 per_cu->cu);
10213 do_cleanups (back_to);
10214
10215 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10216 }
10217
10218 if (pd == NULL)
10219 internal_error (__FILE__, __LINE__,
10220 _("could not find partial DIE 0x%x "
10221 "in cache [from module %s]\n"),
10222 offset.sect_off, bfd_get_filename (objfile->obfd));
10223 return pd;
10224 }
10225
10226 /* See if we can figure out if the class lives in a namespace. We do
10227 this by looking for a member function; its demangled name will
10228 contain namespace info, if there is any. */
10229
10230 static void
10231 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10232 struct dwarf2_cu *cu)
10233 {
10234 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10235 what template types look like, because the demangler
10236 frequently doesn't give the same name as the debug info. We
10237 could fix this by only using the demangled name to get the
10238 prefix (but see comment in read_structure_type). */
10239
10240 struct partial_die_info *real_pdi;
10241 struct partial_die_info *child_pdi;
10242
10243 /* If this DIE (this DIE's specification, if any) has a parent, then
10244 we should not do this. We'll prepend the parent's fully qualified
10245 name when we create the partial symbol. */
10246
10247 real_pdi = struct_pdi;
10248 while (real_pdi->has_specification)
10249 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10250
10251 if (real_pdi->die_parent != NULL)
10252 return;
10253
10254 for (child_pdi = struct_pdi->die_child;
10255 child_pdi != NULL;
10256 child_pdi = child_pdi->die_sibling)
10257 {
10258 if (child_pdi->tag == DW_TAG_subprogram
10259 && child_pdi->linkage_name != NULL)
10260 {
10261 char *actual_class_name
10262 = language_class_name_from_physname (cu->language_defn,
10263 child_pdi->linkage_name);
10264 if (actual_class_name != NULL)
10265 {
10266 struct_pdi->name
10267 = obsavestring (actual_class_name,
10268 strlen (actual_class_name),
10269 &cu->objfile->objfile_obstack);
10270 xfree (actual_class_name);
10271 }
10272 break;
10273 }
10274 }
10275 }
10276
10277 /* Adjust PART_DIE before generating a symbol for it. This function
10278 may set the is_external flag or change the DIE's name. */
10279
10280 static void
10281 fixup_partial_die (struct partial_die_info *part_die,
10282 struct dwarf2_cu *cu)
10283 {
10284 /* Once we've fixed up a die, there's no point in doing so again.
10285 This also avoids a memory leak if we were to call
10286 guess_partial_die_structure_name multiple times. */
10287 if (part_die->fixup_called)
10288 return;
10289
10290 /* If we found a reference attribute and the DIE has no name, try
10291 to find a name in the referred to DIE. */
10292
10293 if (part_die->name == NULL && part_die->has_specification)
10294 {
10295 struct partial_die_info *spec_die;
10296
10297 spec_die = find_partial_die (part_die->spec_offset, cu);
10298
10299 fixup_partial_die (spec_die, cu);
10300
10301 if (spec_die->name)
10302 {
10303 part_die->name = spec_die->name;
10304
10305 /* Copy DW_AT_external attribute if it is set. */
10306 if (spec_die->is_external)
10307 part_die->is_external = spec_die->is_external;
10308 }
10309 }
10310
10311 /* Set default names for some unnamed DIEs. */
10312
10313 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10314 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10315
10316 /* If there is no parent die to provide a namespace, and there are
10317 children, see if we can determine the namespace from their linkage
10318 name. */
10319 if (cu->language == language_cplus
10320 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10321 && part_die->die_parent == NULL
10322 && part_die->has_children
10323 && (part_die->tag == DW_TAG_class_type
10324 || part_die->tag == DW_TAG_structure_type
10325 || part_die->tag == DW_TAG_union_type))
10326 guess_partial_die_structure_name (part_die, cu);
10327
10328 /* GCC might emit a nameless struct or union that has a linkage
10329 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10330 if (part_die->name == NULL
10331 && (part_die->tag == DW_TAG_class_type
10332 || part_die->tag == DW_TAG_interface_type
10333 || part_die->tag == DW_TAG_structure_type
10334 || part_die->tag == DW_TAG_union_type)
10335 && part_die->linkage_name != NULL)
10336 {
10337 char *demangled;
10338
10339 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10340 if (demangled)
10341 {
10342 const char *base;
10343
10344 /* Strip any leading namespaces/classes, keep only the base name.
10345 DW_AT_name for named DIEs does not contain the prefixes. */
10346 base = strrchr (demangled, ':');
10347 if (base && base > demangled && base[-1] == ':')
10348 base++;
10349 else
10350 base = demangled;
10351
10352 part_die->name = obsavestring (base, strlen (base),
10353 &cu->objfile->objfile_obstack);
10354 xfree (demangled);
10355 }
10356 }
10357
10358 part_die->fixup_called = 1;
10359 }
10360
10361 /* Read an attribute value described by an attribute form. */
10362
10363 static gdb_byte *
10364 read_attribute_value (struct attribute *attr, unsigned form,
10365 bfd *abfd, gdb_byte *info_ptr,
10366 struct dwarf2_cu *cu)
10367 {
10368 struct comp_unit_head *cu_header = &cu->header;
10369 unsigned int bytes_read;
10370 struct dwarf_block *blk;
10371
10372 attr->form = form;
10373 switch (form)
10374 {
10375 case DW_FORM_ref_addr:
10376 if (cu->header.version == 2)
10377 DW_UNSND (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10378 else
10379 DW_UNSND (attr) = read_offset (abfd, info_ptr,
10380 &cu->header, &bytes_read);
10381 info_ptr += bytes_read;
10382 break;
10383 case DW_FORM_addr:
10384 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10385 info_ptr += bytes_read;
10386 break;
10387 case DW_FORM_block2:
10388 blk = dwarf_alloc_block (cu);
10389 blk->size = read_2_bytes (abfd, info_ptr);
10390 info_ptr += 2;
10391 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10392 info_ptr += blk->size;
10393 DW_BLOCK (attr) = blk;
10394 break;
10395 case DW_FORM_block4:
10396 blk = dwarf_alloc_block (cu);
10397 blk->size = read_4_bytes (abfd, info_ptr);
10398 info_ptr += 4;
10399 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10400 info_ptr += blk->size;
10401 DW_BLOCK (attr) = blk;
10402 break;
10403 case DW_FORM_data2:
10404 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10405 info_ptr += 2;
10406 break;
10407 case DW_FORM_data4:
10408 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10409 info_ptr += 4;
10410 break;
10411 case DW_FORM_data8:
10412 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10413 info_ptr += 8;
10414 break;
10415 case DW_FORM_sec_offset:
10416 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10417 info_ptr += bytes_read;
10418 break;
10419 case DW_FORM_string:
10420 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10421 DW_STRING_IS_CANONICAL (attr) = 0;
10422 info_ptr += bytes_read;
10423 break;
10424 case DW_FORM_strp:
10425 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10426 &bytes_read);
10427 DW_STRING_IS_CANONICAL (attr) = 0;
10428 info_ptr += bytes_read;
10429 break;
10430 case DW_FORM_exprloc:
10431 case DW_FORM_block:
10432 blk = dwarf_alloc_block (cu);
10433 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10434 info_ptr += bytes_read;
10435 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10436 info_ptr += blk->size;
10437 DW_BLOCK (attr) = blk;
10438 break;
10439 case DW_FORM_block1:
10440 blk = dwarf_alloc_block (cu);
10441 blk->size = read_1_byte (abfd, info_ptr);
10442 info_ptr += 1;
10443 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10444 info_ptr += blk->size;
10445 DW_BLOCK (attr) = blk;
10446 break;
10447 case DW_FORM_data1:
10448 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10449 info_ptr += 1;
10450 break;
10451 case DW_FORM_flag:
10452 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10453 info_ptr += 1;
10454 break;
10455 case DW_FORM_flag_present:
10456 DW_UNSND (attr) = 1;
10457 break;
10458 case DW_FORM_sdata:
10459 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10460 info_ptr += bytes_read;
10461 break;
10462 case DW_FORM_udata:
10463 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10464 info_ptr += bytes_read;
10465 break;
10466 case DW_FORM_ref1:
10467 DW_UNSND (attr) = (cu->header.offset.sect_off
10468 + read_1_byte (abfd, info_ptr));
10469 info_ptr += 1;
10470 break;
10471 case DW_FORM_ref2:
10472 DW_UNSND (attr) = (cu->header.offset.sect_off
10473 + read_2_bytes (abfd, info_ptr));
10474 info_ptr += 2;
10475 break;
10476 case DW_FORM_ref4:
10477 DW_UNSND (attr) = (cu->header.offset.sect_off
10478 + read_4_bytes (abfd, info_ptr));
10479 info_ptr += 4;
10480 break;
10481 case DW_FORM_ref8:
10482 DW_UNSND (attr) = (cu->header.offset.sect_off
10483 + read_8_bytes (abfd, info_ptr));
10484 info_ptr += 8;
10485 break;
10486 case DW_FORM_ref_sig8:
10487 /* Convert the signature to something we can record in DW_UNSND
10488 for later lookup.
10489 NOTE: This is NULL if the type wasn't found. */
10490 DW_SIGNATURED_TYPE (attr) =
10491 lookup_signatured_type (read_8_bytes (abfd, info_ptr));
10492 info_ptr += 8;
10493 break;
10494 case DW_FORM_ref_udata:
10495 DW_UNSND (attr) = (cu->header.offset.sect_off
10496 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10497 info_ptr += bytes_read;
10498 break;
10499 case DW_FORM_indirect:
10500 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10501 info_ptr += bytes_read;
10502 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10503 break;
10504 default:
10505 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10506 dwarf_form_name (form),
10507 bfd_get_filename (abfd));
10508 }
10509
10510 /* We have seen instances where the compiler tried to emit a byte
10511 size attribute of -1 which ended up being encoded as an unsigned
10512 0xffffffff. Although 0xffffffff is technically a valid size value,
10513 an object of this size seems pretty unlikely so we can relatively
10514 safely treat these cases as if the size attribute was invalid and
10515 treat them as zero by default. */
10516 if (attr->name == DW_AT_byte_size
10517 && form == DW_FORM_data4
10518 && DW_UNSND (attr) >= 0xffffffff)
10519 {
10520 complaint
10521 (&symfile_complaints,
10522 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10523 hex_string (DW_UNSND (attr)));
10524 DW_UNSND (attr) = 0;
10525 }
10526
10527 return info_ptr;
10528 }
10529
10530 /* Read an attribute described by an abbreviated attribute. */
10531
10532 static gdb_byte *
10533 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10534 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10535 {
10536 attr->name = abbrev->name;
10537 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10538 }
10539
10540 /* Read dwarf information from a buffer. */
10541
10542 static unsigned int
10543 read_1_byte (bfd *abfd, gdb_byte *buf)
10544 {
10545 return bfd_get_8 (abfd, buf);
10546 }
10547
10548 static int
10549 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10550 {
10551 return bfd_get_signed_8 (abfd, buf);
10552 }
10553
10554 static unsigned int
10555 read_2_bytes (bfd *abfd, gdb_byte *buf)
10556 {
10557 return bfd_get_16 (abfd, buf);
10558 }
10559
10560 static int
10561 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10562 {
10563 return bfd_get_signed_16 (abfd, buf);
10564 }
10565
10566 static unsigned int
10567 read_4_bytes (bfd *abfd, gdb_byte *buf)
10568 {
10569 return bfd_get_32 (abfd, buf);
10570 }
10571
10572 static int
10573 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10574 {
10575 return bfd_get_signed_32 (abfd, buf);
10576 }
10577
10578 static ULONGEST
10579 read_8_bytes (bfd *abfd, gdb_byte *buf)
10580 {
10581 return bfd_get_64 (abfd, buf);
10582 }
10583
10584 static CORE_ADDR
10585 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10586 unsigned int *bytes_read)
10587 {
10588 struct comp_unit_head *cu_header = &cu->header;
10589 CORE_ADDR retval = 0;
10590
10591 if (cu_header->signed_addr_p)
10592 {
10593 switch (cu_header->addr_size)
10594 {
10595 case 2:
10596 retval = bfd_get_signed_16 (abfd, buf);
10597 break;
10598 case 4:
10599 retval = bfd_get_signed_32 (abfd, buf);
10600 break;
10601 case 8:
10602 retval = bfd_get_signed_64 (abfd, buf);
10603 break;
10604 default:
10605 internal_error (__FILE__, __LINE__,
10606 _("read_address: bad switch, signed [in module %s]"),
10607 bfd_get_filename (abfd));
10608 }
10609 }
10610 else
10611 {
10612 switch (cu_header->addr_size)
10613 {
10614 case 2:
10615 retval = bfd_get_16 (abfd, buf);
10616 break;
10617 case 4:
10618 retval = bfd_get_32 (abfd, buf);
10619 break;
10620 case 8:
10621 retval = bfd_get_64 (abfd, buf);
10622 break;
10623 default:
10624 internal_error (__FILE__, __LINE__,
10625 _("read_address: bad switch, "
10626 "unsigned [in module %s]"),
10627 bfd_get_filename (abfd));
10628 }
10629 }
10630
10631 *bytes_read = cu_header->addr_size;
10632 return retval;
10633 }
10634
10635 /* Read the initial length from a section. The (draft) DWARF 3
10636 specification allows the initial length to take up either 4 bytes
10637 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10638 bytes describe the length and all offsets will be 8 bytes in length
10639 instead of 4.
10640
10641 An older, non-standard 64-bit format is also handled by this
10642 function. The older format in question stores the initial length
10643 as an 8-byte quantity without an escape value. Lengths greater
10644 than 2^32 aren't very common which means that the initial 4 bytes
10645 is almost always zero. Since a length value of zero doesn't make
10646 sense for the 32-bit format, this initial zero can be considered to
10647 be an escape value which indicates the presence of the older 64-bit
10648 format. As written, the code can't detect (old format) lengths
10649 greater than 4GB. If it becomes necessary to handle lengths
10650 somewhat larger than 4GB, we could allow other small values (such
10651 as the non-sensical values of 1, 2, and 3) to also be used as
10652 escape values indicating the presence of the old format.
10653
10654 The value returned via bytes_read should be used to increment the
10655 relevant pointer after calling read_initial_length().
10656
10657 [ Note: read_initial_length() and read_offset() are based on the
10658 document entitled "DWARF Debugging Information Format", revision
10659 3, draft 8, dated November 19, 2001. This document was obtained
10660 from:
10661
10662 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10663
10664 This document is only a draft and is subject to change. (So beware.)
10665
10666 Details regarding the older, non-standard 64-bit format were
10667 determined empirically by examining 64-bit ELF files produced by
10668 the SGI toolchain on an IRIX 6.5 machine.
10669
10670 - Kevin, July 16, 2002
10671 ] */
10672
10673 static LONGEST
10674 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10675 {
10676 LONGEST length = bfd_get_32 (abfd, buf);
10677
10678 if (length == 0xffffffff)
10679 {
10680 length = bfd_get_64 (abfd, buf + 4);
10681 *bytes_read = 12;
10682 }
10683 else if (length == 0)
10684 {
10685 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10686 length = bfd_get_64 (abfd, buf);
10687 *bytes_read = 8;
10688 }
10689 else
10690 {
10691 *bytes_read = 4;
10692 }
10693
10694 return length;
10695 }
10696
10697 /* Cover function for read_initial_length.
10698 Returns the length of the object at BUF, and stores the size of the
10699 initial length in *BYTES_READ and stores the size that offsets will be in
10700 *OFFSET_SIZE.
10701 If the initial length size is not equivalent to that specified in
10702 CU_HEADER then issue a complaint.
10703 This is useful when reading non-comp-unit headers. */
10704
10705 static LONGEST
10706 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10707 const struct comp_unit_head *cu_header,
10708 unsigned int *bytes_read,
10709 unsigned int *offset_size)
10710 {
10711 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10712
10713 gdb_assert (cu_header->initial_length_size == 4
10714 || cu_header->initial_length_size == 8
10715 || cu_header->initial_length_size == 12);
10716
10717 if (cu_header->initial_length_size != *bytes_read)
10718 complaint (&symfile_complaints,
10719 _("intermixed 32-bit and 64-bit DWARF sections"));
10720
10721 *offset_size = (*bytes_read == 4) ? 4 : 8;
10722 return length;
10723 }
10724
10725 /* Read an offset from the data stream. The size of the offset is
10726 given by cu_header->offset_size. */
10727
10728 static LONGEST
10729 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10730 unsigned int *bytes_read)
10731 {
10732 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10733
10734 *bytes_read = cu_header->offset_size;
10735 return offset;
10736 }
10737
10738 /* Read an offset from the data stream. */
10739
10740 static LONGEST
10741 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10742 {
10743 LONGEST retval = 0;
10744
10745 switch (offset_size)
10746 {
10747 case 4:
10748 retval = bfd_get_32 (abfd, buf);
10749 break;
10750 case 8:
10751 retval = bfd_get_64 (abfd, buf);
10752 break;
10753 default:
10754 internal_error (__FILE__, __LINE__,
10755 _("read_offset_1: bad switch [in module %s]"),
10756 bfd_get_filename (abfd));
10757 }
10758
10759 return retval;
10760 }
10761
10762 static gdb_byte *
10763 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10764 {
10765 /* If the size of a host char is 8 bits, we can return a pointer
10766 to the buffer, otherwise we have to copy the data to a buffer
10767 allocated on the temporary obstack. */
10768 gdb_assert (HOST_CHAR_BIT == 8);
10769 return buf;
10770 }
10771
10772 static char *
10773 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10774 {
10775 /* If the size of a host char is 8 bits, we can return a pointer
10776 to the string, otherwise we have to copy the string to a buffer
10777 allocated on the temporary obstack. */
10778 gdb_assert (HOST_CHAR_BIT == 8);
10779 if (*buf == '\0')
10780 {
10781 *bytes_read_ptr = 1;
10782 return NULL;
10783 }
10784 *bytes_read_ptr = strlen ((char *) buf) + 1;
10785 return (char *) buf;
10786 }
10787
10788 static char *
10789 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10790 {
10791 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10792 if (dwarf2_per_objfile->str.buffer == NULL)
10793 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10794 bfd_get_filename (abfd));
10795 if (str_offset >= dwarf2_per_objfile->str.size)
10796 error (_("DW_FORM_strp pointing outside of "
10797 ".debug_str section [in module %s]"),
10798 bfd_get_filename (abfd));
10799 gdb_assert (HOST_CHAR_BIT == 8);
10800 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10801 return NULL;
10802 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10803 }
10804
10805 static char *
10806 read_indirect_string (bfd *abfd, gdb_byte *buf,
10807 const struct comp_unit_head *cu_header,
10808 unsigned int *bytes_read_ptr)
10809 {
10810 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10811
10812 return read_indirect_string_at_offset (abfd, str_offset);
10813 }
10814
10815 static ULONGEST
10816 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10817 {
10818 ULONGEST result;
10819 unsigned int num_read;
10820 int i, shift;
10821 unsigned char byte;
10822
10823 result = 0;
10824 shift = 0;
10825 num_read = 0;
10826 i = 0;
10827 while (1)
10828 {
10829 byte = bfd_get_8 (abfd, buf);
10830 buf++;
10831 num_read++;
10832 result |= ((ULONGEST) (byte & 127) << shift);
10833 if ((byte & 128) == 0)
10834 {
10835 break;
10836 }
10837 shift += 7;
10838 }
10839 *bytes_read_ptr = num_read;
10840 return result;
10841 }
10842
10843 static LONGEST
10844 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10845 {
10846 LONGEST result;
10847 int i, shift, num_read;
10848 unsigned char byte;
10849
10850 result = 0;
10851 shift = 0;
10852 num_read = 0;
10853 i = 0;
10854 while (1)
10855 {
10856 byte = bfd_get_8 (abfd, buf);
10857 buf++;
10858 num_read++;
10859 result |= ((LONGEST) (byte & 127) << shift);
10860 shift += 7;
10861 if ((byte & 128) == 0)
10862 {
10863 break;
10864 }
10865 }
10866 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10867 result |= -(((LONGEST) 1) << shift);
10868 *bytes_read_ptr = num_read;
10869 return result;
10870 }
10871
10872 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10873
10874 static gdb_byte *
10875 skip_leb128 (bfd *abfd, gdb_byte *buf)
10876 {
10877 int byte;
10878
10879 while (1)
10880 {
10881 byte = bfd_get_8 (abfd, buf);
10882 buf++;
10883 if ((byte & 128) == 0)
10884 return buf;
10885 }
10886 }
10887
10888 static void
10889 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10890 {
10891 switch (lang)
10892 {
10893 case DW_LANG_C89:
10894 case DW_LANG_C99:
10895 case DW_LANG_C:
10896 cu->language = language_c;
10897 break;
10898 case DW_LANG_C_plus_plus:
10899 cu->language = language_cplus;
10900 break;
10901 case DW_LANG_D:
10902 cu->language = language_d;
10903 break;
10904 case DW_LANG_Fortran77:
10905 case DW_LANG_Fortran90:
10906 case DW_LANG_Fortran95:
10907 cu->language = language_fortran;
10908 break;
10909 case DW_LANG_Go:
10910 cu->language = language_go;
10911 break;
10912 case DW_LANG_Mips_Assembler:
10913 cu->language = language_asm;
10914 break;
10915 case DW_LANG_Java:
10916 cu->language = language_java;
10917 break;
10918 case DW_LANG_Ada83:
10919 case DW_LANG_Ada95:
10920 cu->language = language_ada;
10921 break;
10922 case DW_LANG_Modula2:
10923 cu->language = language_m2;
10924 break;
10925 case DW_LANG_Pascal83:
10926 cu->language = language_pascal;
10927 break;
10928 case DW_LANG_ObjC:
10929 cu->language = language_objc;
10930 break;
10931 case DW_LANG_Cobol74:
10932 case DW_LANG_Cobol85:
10933 default:
10934 cu->language = language_minimal;
10935 break;
10936 }
10937 cu->language_defn = language_def (cu->language);
10938 }
10939
10940 /* Return the named attribute or NULL if not there. */
10941
10942 static struct attribute *
10943 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10944 {
10945 for (;;)
10946 {
10947 unsigned int i;
10948 struct attribute *spec = NULL;
10949
10950 for (i = 0; i < die->num_attrs; ++i)
10951 {
10952 if (die->attrs[i].name == name)
10953 return &die->attrs[i];
10954 if (die->attrs[i].name == DW_AT_specification
10955 || die->attrs[i].name == DW_AT_abstract_origin)
10956 spec = &die->attrs[i];
10957 }
10958
10959 if (!spec)
10960 break;
10961
10962 die = follow_die_ref (die, spec, &cu);
10963 }
10964
10965 return NULL;
10966 }
10967
10968 /* Return the named attribute or NULL if not there,
10969 but do not follow DW_AT_specification, etc.
10970 This is for use in contexts where we're reading .debug_types dies.
10971 Following DW_AT_specification, DW_AT_abstract_origin will take us
10972 back up the chain, and we want to go down. */
10973
10974 static struct attribute *
10975 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10976 struct dwarf2_cu *cu)
10977 {
10978 unsigned int i;
10979
10980 for (i = 0; i < die->num_attrs; ++i)
10981 if (die->attrs[i].name == name)
10982 return &die->attrs[i];
10983
10984 return NULL;
10985 }
10986
10987 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10988 and holds a non-zero value. This function should only be used for
10989 DW_FORM_flag or DW_FORM_flag_present attributes. */
10990
10991 static int
10992 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10993 {
10994 struct attribute *attr = dwarf2_attr (die, name, cu);
10995
10996 return (attr && DW_UNSND (attr));
10997 }
10998
10999 static int
11000 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
11001 {
11002 /* A DIE is a declaration if it has a DW_AT_declaration attribute
11003 which value is non-zero. However, we have to be careful with
11004 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
11005 (via dwarf2_flag_true_p) follows this attribute. So we may
11006 end up accidently finding a declaration attribute that belongs
11007 to a different DIE referenced by the specification attribute,
11008 even though the given DIE does not have a declaration attribute. */
11009 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
11010 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
11011 }
11012
11013 /* Return the die giving the specification for DIE, if there is
11014 one. *SPEC_CU is the CU containing DIE on input, and the CU
11015 containing the return value on output. If there is no
11016 specification, but there is an abstract origin, that is
11017 returned. */
11018
11019 static struct die_info *
11020 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
11021 {
11022 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
11023 *spec_cu);
11024
11025 if (spec_attr == NULL)
11026 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
11027
11028 if (spec_attr == NULL)
11029 return NULL;
11030 else
11031 return follow_die_ref (die, spec_attr, spec_cu);
11032 }
11033
11034 /* Free the line_header structure *LH, and any arrays and strings it
11035 refers to.
11036 NOTE: This is also used as a "cleanup" function. */
11037
11038 static void
11039 free_line_header (struct line_header *lh)
11040 {
11041 if (lh->standard_opcode_lengths)
11042 xfree (lh->standard_opcode_lengths);
11043
11044 /* Remember that all the lh->file_names[i].name pointers are
11045 pointers into debug_line_buffer, and don't need to be freed. */
11046 if (lh->file_names)
11047 xfree (lh->file_names);
11048
11049 /* Similarly for the include directory names. */
11050 if (lh->include_dirs)
11051 xfree (lh->include_dirs);
11052
11053 xfree (lh);
11054 }
11055
11056 /* Add an entry to LH's include directory table. */
11057
11058 static void
11059 add_include_dir (struct line_header *lh, char *include_dir)
11060 {
11061 /* Grow the array if necessary. */
11062 if (lh->include_dirs_size == 0)
11063 {
11064 lh->include_dirs_size = 1; /* for testing */
11065 lh->include_dirs = xmalloc (lh->include_dirs_size
11066 * sizeof (*lh->include_dirs));
11067 }
11068 else if (lh->num_include_dirs >= lh->include_dirs_size)
11069 {
11070 lh->include_dirs_size *= 2;
11071 lh->include_dirs = xrealloc (lh->include_dirs,
11072 (lh->include_dirs_size
11073 * sizeof (*lh->include_dirs)));
11074 }
11075
11076 lh->include_dirs[lh->num_include_dirs++] = include_dir;
11077 }
11078
11079 /* Add an entry to LH's file name table. */
11080
11081 static void
11082 add_file_name (struct line_header *lh,
11083 char *name,
11084 unsigned int dir_index,
11085 unsigned int mod_time,
11086 unsigned int length)
11087 {
11088 struct file_entry *fe;
11089
11090 /* Grow the array if necessary. */
11091 if (lh->file_names_size == 0)
11092 {
11093 lh->file_names_size = 1; /* for testing */
11094 lh->file_names = xmalloc (lh->file_names_size
11095 * sizeof (*lh->file_names));
11096 }
11097 else if (lh->num_file_names >= lh->file_names_size)
11098 {
11099 lh->file_names_size *= 2;
11100 lh->file_names = xrealloc (lh->file_names,
11101 (lh->file_names_size
11102 * sizeof (*lh->file_names)));
11103 }
11104
11105 fe = &lh->file_names[lh->num_file_names++];
11106 fe->name = name;
11107 fe->dir_index = dir_index;
11108 fe->mod_time = mod_time;
11109 fe->length = length;
11110 fe->included_p = 0;
11111 fe->symtab = NULL;
11112 }
11113
11114 /* Read the statement program header starting at OFFSET in
11115 .debug_line, according to the endianness of ABFD. Return a pointer
11116 to a struct line_header, allocated using xmalloc.
11117
11118 NOTE: the strings in the include directory and file name tables of
11119 the returned object point into debug_line_buffer, and must not be
11120 freed. */
11121
11122 static struct line_header *
11123 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
11124 struct dwarf2_cu *cu)
11125 {
11126 struct cleanup *back_to;
11127 struct line_header *lh;
11128 gdb_byte *line_ptr;
11129 unsigned int bytes_read, offset_size;
11130 int i;
11131 char *cur_dir, *cur_file;
11132
11133 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
11134 if (dwarf2_per_objfile->line.buffer == NULL)
11135 {
11136 complaint (&symfile_complaints, _("missing .debug_line section"));
11137 return 0;
11138 }
11139
11140 /* Make sure that at least there's room for the total_length field.
11141 That could be 12 bytes long, but we're just going to fudge that. */
11142 if (offset + 4 >= dwarf2_per_objfile->line.size)
11143 {
11144 dwarf2_statement_list_fits_in_line_number_section_complaint ();
11145 return 0;
11146 }
11147
11148 lh = xmalloc (sizeof (*lh));
11149 memset (lh, 0, sizeof (*lh));
11150 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
11151 (void *) lh);
11152
11153 line_ptr = dwarf2_per_objfile->line.buffer + offset;
11154
11155 /* Read in the header. */
11156 lh->total_length =
11157 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
11158 &bytes_read, &offset_size);
11159 line_ptr += bytes_read;
11160 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
11161 + dwarf2_per_objfile->line.size))
11162 {
11163 dwarf2_statement_list_fits_in_line_number_section_complaint ();
11164 return 0;
11165 }
11166 lh->statement_program_end = line_ptr + lh->total_length;
11167 lh->version = read_2_bytes (abfd, line_ptr);
11168 line_ptr += 2;
11169 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
11170 line_ptr += offset_size;
11171 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
11172 line_ptr += 1;
11173 if (lh->version >= 4)
11174 {
11175 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
11176 line_ptr += 1;
11177 }
11178 else
11179 lh->maximum_ops_per_instruction = 1;
11180
11181 if (lh->maximum_ops_per_instruction == 0)
11182 {
11183 lh->maximum_ops_per_instruction = 1;
11184 complaint (&symfile_complaints,
11185 _("invalid maximum_ops_per_instruction "
11186 "in `.debug_line' section"));
11187 }
11188
11189 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11190 line_ptr += 1;
11191 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11192 line_ptr += 1;
11193 lh->line_range = read_1_byte (abfd, line_ptr);
11194 line_ptr += 1;
11195 lh->opcode_base = read_1_byte (abfd, line_ptr);
11196 line_ptr += 1;
11197 lh->standard_opcode_lengths
11198 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
11199
11200 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11201 for (i = 1; i < lh->opcode_base; ++i)
11202 {
11203 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11204 line_ptr += 1;
11205 }
11206
11207 /* Read directory table. */
11208 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11209 {
11210 line_ptr += bytes_read;
11211 add_include_dir (lh, cur_dir);
11212 }
11213 line_ptr += bytes_read;
11214
11215 /* Read file name table. */
11216 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11217 {
11218 unsigned int dir_index, mod_time, length;
11219
11220 line_ptr += bytes_read;
11221 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11222 line_ptr += bytes_read;
11223 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11224 line_ptr += bytes_read;
11225 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11226 line_ptr += bytes_read;
11227
11228 add_file_name (lh, cur_file, dir_index, mod_time, length);
11229 }
11230 line_ptr += bytes_read;
11231 lh->statement_program_start = line_ptr;
11232
11233 if (line_ptr > (dwarf2_per_objfile->line.buffer
11234 + dwarf2_per_objfile->line.size))
11235 complaint (&symfile_complaints,
11236 _("line number info header doesn't "
11237 "fit in `.debug_line' section"));
11238
11239 discard_cleanups (back_to);
11240 return lh;
11241 }
11242
11243 /* Subroutine of dwarf_decode_lines to simplify it.
11244 Return the file name of the psymtab for included file FILE_INDEX
11245 in line header LH of PST.
11246 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11247 If space for the result is malloc'd, it will be freed by a cleanup.
11248 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11249
11250 static char *
11251 psymtab_include_file_name (const struct line_header *lh, int file_index,
11252 const struct partial_symtab *pst,
11253 const char *comp_dir)
11254 {
11255 const struct file_entry fe = lh->file_names [file_index];
11256 char *include_name = fe.name;
11257 char *include_name_to_compare = include_name;
11258 char *dir_name = NULL;
11259 const char *pst_filename;
11260 char *copied_name = NULL;
11261 int file_is_pst;
11262
11263 if (fe.dir_index)
11264 dir_name = lh->include_dirs[fe.dir_index - 1];
11265
11266 if (!IS_ABSOLUTE_PATH (include_name)
11267 && (dir_name != NULL || comp_dir != NULL))
11268 {
11269 /* Avoid creating a duplicate psymtab for PST.
11270 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11271 Before we do the comparison, however, we need to account
11272 for DIR_NAME and COMP_DIR.
11273 First prepend dir_name (if non-NULL). If we still don't
11274 have an absolute path prepend comp_dir (if non-NULL).
11275 However, the directory we record in the include-file's
11276 psymtab does not contain COMP_DIR (to match the
11277 corresponding symtab(s)).
11278
11279 Example:
11280
11281 bash$ cd /tmp
11282 bash$ gcc -g ./hello.c
11283 include_name = "hello.c"
11284 dir_name = "."
11285 DW_AT_comp_dir = comp_dir = "/tmp"
11286 DW_AT_name = "./hello.c" */
11287
11288 if (dir_name != NULL)
11289 {
11290 include_name = concat (dir_name, SLASH_STRING,
11291 include_name, (char *)NULL);
11292 include_name_to_compare = include_name;
11293 make_cleanup (xfree, include_name);
11294 }
11295 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11296 {
11297 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11298 include_name, (char *)NULL);
11299 }
11300 }
11301
11302 pst_filename = pst->filename;
11303 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11304 {
11305 copied_name = concat (pst->dirname, SLASH_STRING,
11306 pst_filename, (char *)NULL);
11307 pst_filename = copied_name;
11308 }
11309
11310 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11311
11312 if (include_name_to_compare != include_name)
11313 xfree (include_name_to_compare);
11314 if (copied_name != NULL)
11315 xfree (copied_name);
11316
11317 if (file_is_pst)
11318 return NULL;
11319 return include_name;
11320 }
11321
11322 /* Ignore this record_line request. */
11323
11324 static void
11325 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11326 {
11327 return;
11328 }
11329
11330 /* Subroutine of dwarf_decode_lines to simplify it.
11331 Process the line number information in LH. */
11332
11333 static void
11334 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11335 struct dwarf2_cu *cu, struct partial_symtab *pst)
11336 {
11337 gdb_byte *line_ptr, *extended_end;
11338 gdb_byte *line_end;
11339 unsigned int bytes_read, extended_len;
11340 unsigned char op_code, extended_op, adj_opcode;
11341 CORE_ADDR baseaddr;
11342 struct objfile *objfile = cu->objfile;
11343 bfd *abfd = objfile->obfd;
11344 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11345 const int decode_for_pst_p = (pst != NULL);
11346 struct subfile *last_subfile = NULL;
11347 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11348 = record_line;
11349
11350 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11351
11352 line_ptr = lh->statement_program_start;
11353 line_end = lh->statement_program_end;
11354
11355 /* Read the statement sequences until there's nothing left. */
11356 while (line_ptr < line_end)
11357 {
11358 /* state machine registers */
11359 CORE_ADDR address = 0;
11360 unsigned int file = 1;
11361 unsigned int line = 1;
11362 unsigned int column = 0;
11363 int is_stmt = lh->default_is_stmt;
11364 int basic_block = 0;
11365 int end_sequence = 0;
11366 CORE_ADDR addr;
11367 unsigned char op_index = 0;
11368
11369 if (!decode_for_pst_p && lh->num_file_names >= file)
11370 {
11371 /* Start a subfile for the current file of the state machine. */
11372 /* lh->include_dirs and lh->file_names are 0-based, but the
11373 directory and file name numbers in the statement program
11374 are 1-based. */
11375 struct file_entry *fe = &lh->file_names[file - 1];
11376 char *dir = NULL;
11377
11378 if (fe->dir_index)
11379 dir = lh->include_dirs[fe->dir_index - 1];
11380
11381 dwarf2_start_subfile (fe->name, dir, comp_dir);
11382 }
11383
11384 /* Decode the table. */
11385 while (!end_sequence)
11386 {
11387 op_code = read_1_byte (abfd, line_ptr);
11388 line_ptr += 1;
11389 if (line_ptr > line_end)
11390 {
11391 dwarf2_debug_line_missing_end_sequence_complaint ();
11392 break;
11393 }
11394
11395 if (op_code >= lh->opcode_base)
11396 {
11397 /* Special operand. */
11398 adj_opcode = op_code - lh->opcode_base;
11399 address += (((op_index + (adj_opcode / lh->line_range))
11400 / lh->maximum_ops_per_instruction)
11401 * lh->minimum_instruction_length);
11402 op_index = ((op_index + (adj_opcode / lh->line_range))
11403 % lh->maximum_ops_per_instruction);
11404 line += lh->line_base + (adj_opcode % lh->line_range);
11405 if (lh->num_file_names < file || file == 0)
11406 dwarf2_debug_line_missing_file_complaint ();
11407 /* For now we ignore lines not starting on an
11408 instruction boundary. */
11409 else if (op_index == 0)
11410 {
11411 lh->file_names[file - 1].included_p = 1;
11412 if (!decode_for_pst_p && is_stmt)
11413 {
11414 if (last_subfile != current_subfile)
11415 {
11416 addr = gdbarch_addr_bits_remove (gdbarch, address);
11417 if (last_subfile)
11418 (*p_record_line) (last_subfile, 0, addr);
11419 last_subfile = current_subfile;
11420 }
11421 /* Append row to matrix using current values. */
11422 addr = gdbarch_addr_bits_remove (gdbarch, address);
11423 (*p_record_line) (current_subfile, line, addr);
11424 }
11425 }
11426 basic_block = 0;
11427 }
11428 else switch (op_code)
11429 {
11430 case DW_LNS_extended_op:
11431 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11432 &bytes_read);
11433 line_ptr += bytes_read;
11434 extended_end = line_ptr + extended_len;
11435 extended_op = read_1_byte (abfd, line_ptr);
11436 line_ptr += 1;
11437 switch (extended_op)
11438 {
11439 case DW_LNE_end_sequence:
11440 p_record_line = record_line;
11441 end_sequence = 1;
11442 break;
11443 case DW_LNE_set_address:
11444 address = read_address (abfd, line_ptr, cu, &bytes_read);
11445
11446 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11447 {
11448 /* This line table is for a function which has been
11449 GCd by the linker. Ignore it. PR gdb/12528 */
11450
11451 long line_offset
11452 = line_ptr - dwarf2_per_objfile->line.buffer;
11453
11454 complaint (&symfile_complaints,
11455 _(".debug_line address at offset 0x%lx is 0 "
11456 "[in module %s]"),
11457 line_offset, objfile->name);
11458 p_record_line = noop_record_line;
11459 }
11460
11461 op_index = 0;
11462 line_ptr += bytes_read;
11463 address += baseaddr;
11464 break;
11465 case DW_LNE_define_file:
11466 {
11467 char *cur_file;
11468 unsigned int dir_index, mod_time, length;
11469
11470 cur_file = read_direct_string (abfd, line_ptr,
11471 &bytes_read);
11472 line_ptr += bytes_read;
11473 dir_index =
11474 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11475 line_ptr += bytes_read;
11476 mod_time =
11477 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11478 line_ptr += bytes_read;
11479 length =
11480 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11481 line_ptr += bytes_read;
11482 add_file_name (lh, cur_file, dir_index, mod_time, length);
11483 }
11484 break;
11485 case DW_LNE_set_discriminator:
11486 /* The discriminator is not interesting to the debugger;
11487 just ignore it. */
11488 line_ptr = extended_end;
11489 break;
11490 default:
11491 complaint (&symfile_complaints,
11492 _("mangled .debug_line section"));
11493 return;
11494 }
11495 /* Make sure that we parsed the extended op correctly. If e.g.
11496 we expected a different address size than the producer used,
11497 we may have read the wrong number of bytes. */
11498 if (line_ptr != extended_end)
11499 {
11500 complaint (&symfile_complaints,
11501 _("mangled .debug_line section"));
11502 return;
11503 }
11504 break;
11505 case DW_LNS_copy:
11506 if (lh->num_file_names < file || file == 0)
11507 dwarf2_debug_line_missing_file_complaint ();
11508 else
11509 {
11510 lh->file_names[file - 1].included_p = 1;
11511 if (!decode_for_pst_p && is_stmt)
11512 {
11513 if (last_subfile != current_subfile)
11514 {
11515 addr = gdbarch_addr_bits_remove (gdbarch, address);
11516 if (last_subfile)
11517 (*p_record_line) (last_subfile, 0, addr);
11518 last_subfile = current_subfile;
11519 }
11520 addr = gdbarch_addr_bits_remove (gdbarch, address);
11521 (*p_record_line) (current_subfile, line, addr);
11522 }
11523 }
11524 basic_block = 0;
11525 break;
11526 case DW_LNS_advance_pc:
11527 {
11528 CORE_ADDR adjust
11529 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11530
11531 address += (((op_index + adjust)
11532 / lh->maximum_ops_per_instruction)
11533 * lh->minimum_instruction_length);
11534 op_index = ((op_index + adjust)
11535 % lh->maximum_ops_per_instruction);
11536 line_ptr += bytes_read;
11537 }
11538 break;
11539 case DW_LNS_advance_line:
11540 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11541 line_ptr += bytes_read;
11542 break;
11543 case DW_LNS_set_file:
11544 {
11545 /* The arrays lh->include_dirs and lh->file_names are
11546 0-based, but the directory and file name numbers in
11547 the statement program are 1-based. */
11548 struct file_entry *fe;
11549 char *dir = NULL;
11550
11551 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11552 line_ptr += bytes_read;
11553 if (lh->num_file_names < file || file == 0)
11554 dwarf2_debug_line_missing_file_complaint ();
11555 else
11556 {
11557 fe = &lh->file_names[file - 1];
11558 if (fe->dir_index)
11559 dir = lh->include_dirs[fe->dir_index - 1];
11560 if (!decode_for_pst_p)
11561 {
11562 last_subfile = current_subfile;
11563 dwarf2_start_subfile (fe->name, dir, comp_dir);
11564 }
11565 }
11566 }
11567 break;
11568 case DW_LNS_set_column:
11569 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11570 line_ptr += bytes_read;
11571 break;
11572 case DW_LNS_negate_stmt:
11573 is_stmt = (!is_stmt);
11574 break;
11575 case DW_LNS_set_basic_block:
11576 basic_block = 1;
11577 break;
11578 /* Add to the address register of the state machine the
11579 address increment value corresponding to special opcode
11580 255. I.e., this value is scaled by the minimum
11581 instruction length since special opcode 255 would have
11582 scaled the increment. */
11583 case DW_LNS_const_add_pc:
11584 {
11585 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11586
11587 address += (((op_index + adjust)
11588 / lh->maximum_ops_per_instruction)
11589 * lh->minimum_instruction_length);
11590 op_index = ((op_index + adjust)
11591 % lh->maximum_ops_per_instruction);
11592 }
11593 break;
11594 case DW_LNS_fixed_advance_pc:
11595 address += read_2_bytes (abfd, line_ptr);
11596 op_index = 0;
11597 line_ptr += 2;
11598 break;
11599 default:
11600 {
11601 /* Unknown standard opcode, ignore it. */
11602 int i;
11603
11604 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11605 {
11606 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11607 line_ptr += bytes_read;
11608 }
11609 }
11610 }
11611 }
11612 if (lh->num_file_names < file || file == 0)
11613 dwarf2_debug_line_missing_file_complaint ();
11614 else
11615 {
11616 lh->file_names[file - 1].included_p = 1;
11617 if (!decode_for_pst_p)
11618 {
11619 addr = gdbarch_addr_bits_remove (gdbarch, address);
11620 (*p_record_line) (current_subfile, 0, addr);
11621 }
11622 }
11623 }
11624 }
11625
11626 /* Decode the Line Number Program (LNP) for the given line_header
11627 structure and CU. The actual information extracted and the type
11628 of structures created from the LNP depends on the value of PST.
11629
11630 1. If PST is NULL, then this procedure uses the data from the program
11631 to create all necessary symbol tables, and their linetables.
11632
11633 2. If PST is not NULL, this procedure reads the program to determine
11634 the list of files included by the unit represented by PST, and
11635 builds all the associated partial symbol tables.
11636
11637 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11638 It is used for relative paths in the line table.
11639 NOTE: When processing partial symtabs (pst != NULL),
11640 comp_dir == pst->dirname.
11641
11642 NOTE: It is important that psymtabs have the same file name (via strcmp)
11643 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11644 symtab we don't use it in the name of the psymtabs we create.
11645 E.g. expand_line_sal requires this when finding psymtabs to expand.
11646 A good testcase for this is mb-inline.exp. */
11647
11648 static void
11649 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11650 struct dwarf2_cu *cu, struct partial_symtab *pst,
11651 int want_line_info)
11652 {
11653 struct objfile *objfile = cu->objfile;
11654 const int decode_for_pst_p = (pst != NULL);
11655 struct subfile *first_subfile = current_subfile;
11656
11657 if (want_line_info)
11658 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
11659
11660 if (decode_for_pst_p)
11661 {
11662 int file_index;
11663
11664 /* Now that we're done scanning the Line Header Program, we can
11665 create the psymtab of each included file. */
11666 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11667 if (lh->file_names[file_index].included_p == 1)
11668 {
11669 char *include_name =
11670 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11671 if (include_name != NULL)
11672 dwarf2_create_include_psymtab (include_name, pst, objfile);
11673 }
11674 }
11675 else
11676 {
11677 /* Make sure a symtab is created for every file, even files
11678 which contain only variables (i.e. no code with associated
11679 line numbers). */
11680 int i;
11681
11682 for (i = 0; i < lh->num_file_names; i++)
11683 {
11684 char *dir = NULL;
11685 struct file_entry *fe;
11686
11687 fe = &lh->file_names[i];
11688 if (fe->dir_index)
11689 dir = lh->include_dirs[fe->dir_index - 1];
11690 dwarf2_start_subfile (fe->name, dir, comp_dir);
11691
11692 /* Skip the main file; we don't need it, and it must be
11693 allocated last, so that it will show up before the
11694 non-primary symtabs in the objfile's symtab list. */
11695 if (current_subfile == first_subfile)
11696 continue;
11697
11698 if (current_subfile->symtab == NULL)
11699 current_subfile->symtab = allocate_symtab (current_subfile->name,
11700 objfile);
11701 fe->symtab = current_subfile->symtab;
11702 }
11703 }
11704 }
11705
11706 /* Start a subfile for DWARF. FILENAME is the name of the file and
11707 DIRNAME the name of the source directory which contains FILENAME
11708 or NULL if not known. COMP_DIR is the compilation directory for the
11709 linetable's compilation unit or NULL if not known.
11710 This routine tries to keep line numbers from identical absolute and
11711 relative file names in a common subfile.
11712
11713 Using the `list' example from the GDB testsuite, which resides in
11714 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11715 of /srcdir/list0.c yields the following debugging information for list0.c:
11716
11717 DW_AT_name: /srcdir/list0.c
11718 DW_AT_comp_dir: /compdir
11719 files.files[0].name: list0.h
11720 files.files[0].dir: /srcdir
11721 files.files[1].name: list0.c
11722 files.files[1].dir: /srcdir
11723
11724 The line number information for list0.c has to end up in a single
11725 subfile, so that `break /srcdir/list0.c:1' works as expected.
11726 start_subfile will ensure that this happens provided that we pass the
11727 concatenation of files.files[1].dir and files.files[1].name as the
11728 subfile's name. */
11729
11730 static void
11731 dwarf2_start_subfile (char *filename, const char *dirname,
11732 const char *comp_dir)
11733 {
11734 char *fullname;
11735
11736 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11737 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11738 second argument to start_subfile. To be consistent, we do the
11739 same here. In order not to lose the line information directory,
11740 we concatenate it to the filename when it makes sense.
11741 Note that the Dwarf3 standard says (speaking of filenames in line
11742 information): ``The directory index is ignored for file names
11743 that represent full path names''. Thus ignoring dirname in the
11744 `else' branch below isn't an issue. */
11745
11746 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11747 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11748 else
11749 fullname = filename;
11750
11751 start_subfile (fullname, comp_dir);
11752
11753 if (fullname != filename)
11754 xfree (fullname);
11755 }
11756
11757 static void
11758 var_decode_location (struct attribute *attr, struct symbol *sym,
11759 struct dwarf2_cu *cu)
11760 {
11761 struct objfile *objfile = cu->objfile;
11762 struct comp_unit_head *cu_header = &cu->header;
11763
11764 /* NOTE drow/2003-01-30: There used to be a comment and some special
11765 code here to turn a symbol with DW_AT_external and a
11766 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11767 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11768 with some versions of binutils) where shared libraries could have
11769 relocations against symbols in their debug information - the
11770 minimal symbol would have the right address, but the debug info
11771 would not. It's no longer necessary, because we will explicitly
11772 apply relocations when we read in the debug information now. */
11773
11774 /* A DW_AT_location attribute with no contents indicates that a
11775 variable has been optimized away. */
11776 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11777 {
11778 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11779 return;
11780 }
11781
11782 /* Handle one degenerate form of location expression specially, to
11783 preserve GDB's previous behavior when section offsets are
11784 specified. If this is just a DW_OP_addr then mark this symbol
11785 as LOC_STATIC. */
11786
11787 if (attr_form_is_block (attr)
11788 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11789 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11790 {
11791 unsigned int dummy;
11792
11793 SYMBOL_VALUE_ADDRESS (sym) =
11794 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11795 SYMBOL_CLASS (sym) = LOC_STATIC;
11796 fixup_symbol_section (sym, objfile);
11797 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11798 SYMBOL_SECTION (sym));
11799 return;
11800 }
11801
11802 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11803 expression evaluator, and use LOC_COMPUTED only when necessary
11804 (i.e. when the value of a register or memory location is
11805 referenced, or a thread-local block, etc.). Then again, it might
11806 not be worthwhile. I'm assuming that it isn't unless performance
11807 or memory numbers show me otherwise. */
11808
11809 dwarf2_symbol_mark_computed (attr, sym, cu);
11810 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11811
11812 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11813 cu->has_loclist = 1;
11814 }
11815
11816 /* Given a pointer to a DWARF information entry, figure out if we need
11817 to make a symbol table entry for it, and if so, create a new entry
11818 and return a pointer to it.
11819 If TYPE is NULL, determine symbol type from the die, otherwise
11820 used the passed type.
11821 If SPACE is not NULL, use it to hold the new symbol. If it is
11822 NULL, allocate a new symbol on the objfile's obstack. */
11823
11824 static struct symbol *
11825 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11826 struct symbol *space)
11827 {
11828 struct objfile *objfile = cu->objfile;
11829 struct symbol *sym = NULL;
11830 char *name;
11831 struct attribute *attr = NULL;
11832 struct attribute *attr2 = NULL;
11833 CORE_ADDR baseaddr;
11834 struct pending **list_to_add = NULL;
11835
11836 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11837
11838 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11839
11840 name = dwarf2_name (die, cu);
11841 if (name)
11842 {
11843 const char *linkagename;
11844 int suppress_add = 0;
11845
11846 if (space)
11847 sym = space;
11848 else
11849 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11850 OBJSTAT (objfile, n_syms++);
11851
11852 /* Cache this symbol's name and the name's demangled form (if any). */
11853 SYMBOL_SET_LANGUAGE (sym, cu->language);
11854 linkagename = dwarf2_physname (name, die, cu);
11855 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11856
11857 /* Fortran does not have mangling standard and the mangling does differ
11858 between gfortran, iFort etc. */
11859 if (cu->language == language_fortran
11860 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11861 symbol_set_demangled_name (&(sym->ginfo),
11862 (char *) dwarf2_full_name (name, die, cu),
11863 NULL);
11864
11865 /* Default assumptions.
11866 Use the passed type or decode it from the die. */
11867 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11868 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11869 if (type != NULL)
11870 SYMBOL_TYPE (sym) = type;
11871 else
11872 SYMBOL_TYPE (sym) = die_type (die, cu);
11873 attr = dwarf2_attr (die,
11874 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11875 cu);
11876 if (attr)
11877 {
11878 SYMBOL_LINE (sym) = DW_UNSND (attr);
11879 }
11880
11881 attr = dwarf2_attr (die,
11882 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11883 cu);
11884 if (attr)
11885 {
11886 int file_index = DW_UNSND (attr);
11887
11888 if (cu->line_header == NULL
11889 || file_index > cu->line_header->num_file_names)
11890 complaint (&symfile_complaints,
11891 _("file index out of range"));
11892 else if (file_index > 0)
11893 {
11894 struct file_entry *fe;
11895
11896 fe = &cu->line_header->file_names[file_index - 1];
11897 SYMBOL_SYMTAB (sym) = fe->symtab;
11898 }
11899 }
11900
11901 switch (die->tag)
11902 {
11903 case DW_TAG_label:
11904 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11905 if (attr)
11906 {
11907 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11908 }
11909 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11910 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11911 SYMBOL_CLASS (sym) = LOC_LABEL;
11912 add_symbol_to_list (sym, cu->list_in_scope);
11913 break;
11914 case DW_TAG_subprogram:
11915 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11916 finish_block. */
11917 SYMBOL_CLASS (sym) = LOC_BLOCK;
11918 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11919 if ((attr2 && (DW_UNSND (attr2) != 0))
11920 || cu->language == language_ada)
11921 {
11922 /* Subprograms marked external are stored as a global symbol.
11923 Ada subprograms, whether marked external or not, are always
11924 stored as a global symbol, because we want to be able to
11925 access them globally. For instance, we want to be able
11926 to break on a nested subprogram without having to
11927 specify the context. */
11928 list_to_add = &global_symbols;
11929 }
11930 else
11931 {
11932 list_to_add = cu->list_in_scope;
11933 }
11934 break;
11935 case DW_TAG_inlined_subroutine:
11936 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11937 finish_block. */
11938 SYMBOL_CLASS (sym) = LOC_BLOCK;
11939 SYMBOL_INLINED (sym) = 1;
11940 list_to_add = cu->list_in_scope;
11941 break;
11942 case DW_TAG_template_value_param:
11943 suppress_add = 1;
11944 /* Fall through. */
11945 case DW_TAG_constant:
11946 case DW_TAG_variable:
11947 case DW_TAG_member:
11948 /* Compilation with minimal debug info may result in
11949 variables with missing type entries. Change the
11950 misleading `void' type to something sensible. */
11951 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11952 SYMBOL_TYPE (sym)
11953 = objfile_type (objfile)->nodebug_data_symbol;
11954
11955 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11956 /* In the case of DW_TAG_member, we should only be called for
11957 static const members. */
11958 if (die->tag == DW_TAG_member)
11959 {
11960 /* dwarf2_add_field uses die_is_declaration,
11961 so we do the same. */
11962 gdb_assert (die_is_declaration (die, cu));
11963 gdb_assert (attr);
11964 }
11965 if (attr)
11966 {
11967 dwarf2_const_value (attr, sym, cu);
11968 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11969 if (!suppress_add)
11970 {
11971 if (attr2 && (DW_UNSND (attr2) != 0))
11972 list_to_add = &global_symbols;
11973 else
11974 list_to_add = cu->list_in_scope;
11975 }
11976 break;
11977 }
11978 attr = dwarf2_attr (die, DW_AT_location, cu);
11979 if (attr)
11980 {
11981 var_decode_location (attr, sym, cu);
11982 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11983 if (SYMBOL_CLASS (sym) == LOC_STATIC
11984 && SYMBOL_VALUE_ADDRESS (sym) == 0
11985 && !dwarf2_per_objfile->has_section_at_zero)
11986 {
11987 /* When a static variable is eliminated by the linker,
11988 the corresponding debug information is not stripped
11989 out, but the variable address is set to null;
11990 do not add such variables into symbol table. */
11991 }
11992 else if (attr2 && (DW_UNSND (attr2) != 0))
11993 {
11994 /* Workaround gfortran PR debug/40040 - it uses
11995 DW_AT_location for variables in -fPIC libraries which may
11996 get overriden by other libraries/executable and get
11997 a different address. Resolve it by the minimal symbol
11998 which may come from inferior's executable using copy
11999 relocation. Make this workaround only for gfortran as for
12000 other compilers GDB cannot guess the minimal symbol
12001 Fortran mangling kind. */
12002 if (cu->language == language_fortran && die->parent
12003 && die->parent->tag == DW_TAG_module
12004 && cu->producer
12005 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
12006 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
12007
12008 /* A variable with DW_AT_external is never static,
12009 but it may be block-scoped. */
12010 list_to_add = (cu->list_in_scope == &file_symbols
12011 ? &global_symbols : cu->list_in_scope);
12012 }
12013 else
12014 list_to_add = cu->list_in_scope;
12015 }
12016 else
12017 {
12018 /* We do not know the address of this symbol.
12019 If it is an external symbol and we have type information
12020 for it, enter the symbol as a LOC_UNRESOLVED symbol.
12021 The address of the variable will then be determined from
12022 the minimal symbol table whenever the variable is
12023 referenced. */
12024 attr2 = dwarf2_attr (die, DW_AT_external, cu);
12025 if (attr2 && (DW_UNSND (attr2) != 0)
12026 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
12027 {
12028 /* A variable with DW_AT_external is never static, but it
12029 may be block-scoped. */
12030 list_to_add = (cu->list_in_scope == &file_symbols
12031 ? &global_symbols : cu->list_in_scope);
12032
12033 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
12034 }
12035 else if (!die_is_declaration (die, cu))
12036 {
12037 /* Use the default LOC_OPTIMIZED_OUT class. */
12038 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
12039 if (!suppress_add)
12040 list_to_add = cu->list_in_scope;
12041 }
12042 }
12043 break;
12044 case DW_TAG_formal_parameter:
12045 /* If we are inside a function, mark this as an argument. If
12046 not, we might be looking at an argument to an inlined function
12047 when we do not have enough information to show inlined frames;
12048 pretend it's a local variable in that case so that the user can
12049 still see it. */
12050 if (context_stack_depth > 0
12051 && context_stack[context_stack_depth - 1].name != NULL)
12052 SYMBOL_IS_ARGUMENT (sym) = 1;
12053 attr = dwarf2_attr (die, DW_AT_location, cu);
12054 if (attr)
12055 {
12056 var_decode_location (attr, sym, cu);
12057 }
12058 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12059 if (attr)
12060 {
12061 dwarf2_const_value (attr, sym, cu);
12062 }
12063
12064 list_to_add = cu->list_in_scope;
12065 break;
12066 case DW_TAG_unspecified_parameters:
12067 /* From varargs functions; gdb doesn't seem to have any
12068 interest in this information, so just ignore it for now.
12069 (FIXME?) */
12070 break;
12071 case DW_TAG_template_type_param:
12072 suppress_add = 1;
12073 /* Fall through. */
12074 case DW_TAG_class_type:
12075 case DW_TAG_interface_type:
12076 case DW_TAG_structure_type:
12077 case DW_TAG_union_type:
12078 case DW_TAG_set_type:
12079 case DW_TAG_enumeration_type:
12080 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12081 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
12082
12083 {
12084 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
12085 really ever be static objects: otherwise, if you try
12086 to, say, break of a class's method and you're in a file
12087 which doesn't mention that class, it won't work unless
12088 the check for all static symbols in lookup_symbol_aux
12089 saves you. See the OtherFileClass tests in
12090 gdb.c++/namespace.exp. */
12091
12092 if (!suppress_add)
12093 {
12094 list_to_add = (cu->list_in_scope == &file_symbols
12095 && (cu->language == language_cplus
12096 || cu->language == language_java)
12097 ? &global_symbols : cu->list_in_scope);
12098
12099 /* The semantics of C++ state that "struct foo {
12100 ... }" also defines a typedef for "foo". A Java
12101 class declaration also defines a typedef for the
12102 class. */
12103 if (cu->language == language_cplus
12104 || cu->language == language_java
12105 || cu->language == language_ada)
12106 {
12107 /* The symbol's name is already allocated along
12108 with this objfile, so we don't need to
12109 duplicate it for the type. */
12110 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
12111 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
12112 }
12113 }
12114 }
12115 break;
12116 case DW_TAG_typedef:
12117 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12118 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
12119 list_to_add = cu->list_in_scope;
12120 break;
12121 case DW_TAG_base_type:
12122 case DW_TAG_subrange_type:
12123 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12124 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
12125 list_to_add = cu->list_in_scope;
12126 break;
12127 case DW_TAG_enumerator:
12128 attr = dwarf2_attr (die, DW_AT_const_value, cu);
12129 if (attr)
12130 {
12131 dwarf2_const_value (attr, sym, cu);
12132 }
12133 {
12134 /* NOTE: carlton/2003-11-10: See comment above in the
12135 DW_TAG_class_type, etc. block. */
12136
12137 list_to_add = (cu->list_in_scope == &file_symbols
12138 && (cu->language == language_cplus
12139 || cu->language == language_java)
12140 ? &global_symbols : cu->list_in_scope);
12141 }
12142 break;
12143 case DW_TAG_namespace:
12144 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
12145 list_to_add = &global_symbols;
12146 break;
12147 default:
12148 /* Not a tag we recognize. Hopefully we aren't processing
12149 trash data, but since we must specifically ignore things
12150 we don't recognize, there is nothing else we should do at
12151 this point. */
12152 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
12153 dwarf_tag_name (die->tag));
12154 break;
12155 }
12156
12157 if (suppress_add)
12158 {
12159 sym->hash_next = objfile->template_symbols;
12160 objfile->template_symbols = sym;
12161 list_to_add = NULL;
12162 }
12163
12164 if (list_to_add != NULL)
12165 add_symbol_to_list (sym, list_to_add);
12166
12167 /* For the benefit of old versions of GCC, check for anonymous
12168 namespaces based on the demangled name. */
12169 if (!processing_has_namespace_info
12170 && cu->language == language_cplus)
12171 cp_scan_for_anonymous_namespaces (sym, objfile);
12172 }
12173 return (sym);
12174 }
12175
12176 /* A wrapper for new_symbol_full that always allocates a new symbol. */
12177
12178 static struct symbol *
12179 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12180 {
12181 return new_symbol_full (die, type, cu, NULL);
12182 }
12183
12184 /* Given an attr with a DW_FORM_dataN value in host byte order,
12185 zero-extend it as appropriate for the symbol's type. The DWARF
12186 standard (v4) is not entirely clear about the meaning of using
12187 DW_FORM_dataN for a constant with a signed type, where the type is
12188 wider than the data. The conclusion of a discussion on the DWARF
12189 list was that this is unspecified. We choose to always zero-extend
12190 because that is the interpretation long in use by GCC. */
12191
12192 static gdb_byte *
12193 dwarf2_const_value_data (struct attribute *attr, struct type *type,
12194 const char *name, struct obstack *obstack,
12195 struct dwarf2_cu *cu, LONGEST *value, int bits)
12196 {
12197 struct objfile *objfile = cu->objfile;
12198 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12199 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
12200 LONGEST l = DW_UNSND (attr);
12201
12202 if (bits < sizeof (*value) * 8)
12203 {
12204 l &= ((LONGEST) 1 << bits) - 1;
12205 *value = l;
12206 }
12207 else if (bits == sizeof (*value) * 8)
12208 *value = l;
12209 else
12210 {
12211 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12212 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12213 return bytes;
12214 }
12215
12216 return NULL;
12217 }
12218
12219 /* Read a constant value from an attribute. Either set *VALUE, or if
12220 the value does not fit in *VALUE, set *BYTES - either already
12221 allocated on the objfile obstack, or newly allocated on OBSTACK,
12222 or, set *BATON, if we translated the constant to a location
12223 expression. */
12224
12225 static void
12226 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12227 const char *name, struct obstack *obstack,
12228 struct dwarf2_cu *cu,
12229 LONGEST *value, gdb_byte **bytes,
12230 struct dwarf2_locexpr_baton **baton)
12231 {
12232 struct objfile *objfile = cu->objfile;
12233 struct comp_unit_head *cu_header = &cu->header;
12234 struct dwarf_block *blk;
12235 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12236 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12237
12238 *value = 0;
12239 *bytes = NULL;
12240 *baton = NULL;
12241
12242 switch (attr->form)
12243 {
12244 case DW_FORM_addr:
12245 {
12246 gdb_byte *data;
12247
12248 if (TYPE_LENGTH (type) != cu_header->addr_size)
12249 dwarf2_const_value_length_mismatch_complaint (name,
12250 cu_header->addr_size,
12251 TYPE_LENGTH (type));
12252 /* Symbols of this form are reasonably rare, so we just
12253 piggyback on the existing location code rather than writing
12254 a new implementation of symbol_computed_ops. */
12255 *baton = obstack_alloc (&objfile->objfile_obstack,
12256 sizeof (struct dwarf2_locexpr_baton));
12257 (*baton)->per_cu = cu->per_cu;
12258 gdb_assert ((*baton)->per_cu);
12259
12260 (*baton)->size = 2 + cu_header->addr_size;
12261 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12262 (*baton)->data = data;
12263
12264 data[0] = DW_OP_addr;
12265 store_unsigned_integer (&data[1], cu_header->addr_size,
12266 byte_order, DW_ADDR (attr));
12267 data[cu_header->addr_size + 1] = DW_OP_stack_value;
12268 }
12269 break;
12270 case DW_FORM_string:
12271 case DW_FORM_strp:
12272 /* DW_STRING is already allocated on the objfile obstack, point
12273 directly to it. */
12274 *bytes = (gdb_byte *) DW_STRING (attr);
12275 break;
12276 case DW_FORM_block1:
12277 case DW_FORM_block2:
12278 case DW_FORM_block4:
12279 case DW_FORM_block:
12280 case DW_FORM_exprloc:
12281 blk = DW_BLOCK (attr);
12282 if (TYPE_LENGTH (type) != blk->size)
12283 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12284 TYPE_LENGTH (type));
12285 *bytes = blk->data;
12286 break;
12287
12288 /* The DW_AT_const_value attributes are supposed to carry the
12289 symbol's value "represented as it would be on the target
12290 architecture." By the time we get here, it's already been
12291 converted to host endianness, so we just need to sign- or
12292 zero-extend it as appropriate. */
12293 case DW_FORM_data1:
12294 *bytes = dwarf2_const_value_data (attr, type, name,
12295 obstack, cu, value, 8);
12296 break;
12297 case DW_FORM_data2:
12298 *bytes = dwarf2_const_value_data (attr, type, name,
12299 obstack, cu, value, 16);
12300 break;
12301 case DW_FORM_data4:
12302 *bytes = dwarf2_const_value_data (attr, type, name,
12303 obstack, cu, value, 32);
12304 break;
12305 case DW_FORM_data8:
12306 *bytes = dwarf2_const_value_data (attr, type, name,
12307 obstack, cu, value, 64);
12308 break;
12309
12310 case DW_FORM_sdata:
12311 *value = DW_SND (attr);
12312 break;
12313
12314 case DW_FORM_udata:
12315 *value = DW_UNSND (attr);
12316 break;
12317
12318 default:
12319 complaint (&symfile_complaints,
12320 _("unsupported const value attribute form: '%s'"),
12321 dwarf_form_name (attr->form));
12322 *value = 0;
12323 break;
12324 }
12325 }
12326
12327
12328 /* Copy constant value from an attribute to a symbol. */
12329
12330 static void
12331 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12332 struct dwarf2_cu *cu)
12333 {
12334 struct objfile *objfile = cu->objfile;
12335 struct comp_unit_head *cu_header = &cu->header;
12336 LONGEST value;
12337 gdb_byte *bytes;
12338 struct dwarf2_locexpr_baton *baton;
12339
12340 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12341 SYMBOL_PRINT_NAME (sym),
12342 &objfile->objfile_obstack, cu,
12343 &value, &bytes, &baton);
12344
12345 if (baton != NULL)
12346 {
12347 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12348 SYMBOL_LOCATION_BATON (sym) = baton;
12349 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12350 }
12351 else if (bytes != NULL)
12352 {
12353 SYMBOL_VALUE_BYTES (sym) = bytes;
12354 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12355 }
12356 else
12357 {
12358 SYMBOL_VALUE (sym) = value;
12359 SYMBOL_CLASS (sym) = LOC_CONST;
12360 }
12361 }
12362
12363 /* Return the type of the die in question using its DW_AT_type attribute. */
12364
12365 static struct type *
12366 die_type (struct die_info *die, struct dwarf2_cu *cu)
12367 {
12368 struct attribute *type_attr;
12369
12370 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12371 if (!type_attr)
12372 {
12373 /* A missing DW_AT_type represents a void type. */
12374 return objfile_type (cu->objfile)->builtin_void;
12375 }
12376
12377 return lookup_die_type (die, type_attr, cu);
12378 }
12379
12380 /* True iff CU's producer generates GNAT Ada auxiliary information
12381 that allows to find parallel types through that information instead
12382 of having to do expensive parallel lookups by type name. */
12383
12384 static int
12385 need_gnat_info (struct dwarf2_cu *cu)
12386 {
12387 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12388 of GNAT produces this auxiliary information, without any indication
12389 that it is produced. Part of enhancing the FSF version of GNAT
12390 to produce that information will be to put in place an indicator
12391 that we can use in order to determine whether the descriptive type
12392 info is available or not. One suggestion that has been made is
12393 to use a new attribute, attached to the CU die. For now, assume
12394 that the descriptive type info is not available. */
12395 return 0;
12396 }
12397
12398 /* Return the auxiliary type of the die in question using its
12399 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12400 attribute is not present. */
12401
12402 static struct type *
12403 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12404 {
12405 struct attribute *type_attr;
12406
12407 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12408 if (!type_attr)
12409 return NULL;
12410
12411 return lookup_die_type (die, type_attr, cu);
12412 }
12413
12414 /* If DIE has a descriptive_type attribute, then set the TYPE's
12415 descriptive type accordingly. */
12416
12417 static void
12418 set_descriptive_type (struct type *type, struct die_info *die,
12419 struct dwarf2_cu *cu)
12420 {
12421 struct type *descriptive_type = die_descriptive_type (die, cu);
12422
12423 if (descriptive_type)
12424 {
12425 ALLOCATE_GNAT_AUX_TYPE (type);
12426 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12427 }
12428 }
12429
12430 /* Return the containing type of the die in question using its
12431 DW_AT_containing_type attribute. */
12432
12433 static struct type *
12434 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12435 {
12436 struct attribute *type_attr;
12437
12438 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12439 if (!type_attr)
12440 error (_("Dwarf Error: Problem turning containing type into gdb type "
12441 "[in module %s]"), cu->objfile->name);
12442
12443 return lookup_die_type (die, type_attr, cu);
12444 }
12445
12446 /* Look up the type of DIE in CU using its type attribute ATTR.
12447 If there is no type substitute an error marker. */
12448
12449 static struct type *
12450 lookup_die_type (struct die_info *die, struct attribute *attr,
12451 struct dwarf2_cu *cu)
12452 {
12453 struct objfile *objfile = cu->objfile;
12454 struct type *this_type;
12455
12456 /* First see if we have it cached. */
12457
12458 if (is_ref_attr (attr))
12459 {
12460 sect_offset offset = dwarf2_get_ref_die_offset (attr);
12461
12462 this_type = get_die_type_at_offset (offset, cu->per_cu);
12463 }
12464 else if (attr->form == DW_FORM_ref_sig8)
12465 {
12466 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12467 struct dwarf2_cu *sig_cu;
12468 sect_offset offset;
12469
12470 /* sig_type will be NULL if the signatured type is missing from
12471 the debug info. */
12472 if (sig_type == NULL)
12473 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12474 "at 0x%x [in module %s]"),
12475 die->offset.sect_off, objfile->name);
12476
12477 gdb_assert (sig_type->per_cu.debug_types_section);
12478 offset.sect_off = (sig_type->per_cu.offset.sect_off
12479 + sig_type->type_offset.cu_off);
12480 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12481 }
12482 else
12483 {
12484 dump_die_for_error (die);
12485 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12486 dwarf_attr_name (attr->name), objfile->name);
12487 }
12488
12489 /* If not cached we need to read it in. */
12490
12491 if (this_type == NULL)
12492 {
12493 struct die_info *type_die;
12494 struct dwarf2_cu *type_cu = cu;
12495
12496 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12497 /* If the type is cached, we should have found it above. */
12498 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12499 this_type = read_type_die_1 (type_die, type_cu);
12500 }
12501
12502 /* If we still don't have a type use an error marker. */
12503
12504 if (this_type == NULL)
12505 {
12506 char *message, *saved;
12507
12508 /* read_type_die already issued a complaint. */
12509 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12510 objfile->name,
12511 cu->header.offset.sect_off,
12512 die->offset.sect_off);
12513 saved = obstack_copy0 (&objfile->objfile_obstack,
12514 message, strlen (message));
12515 xfree (message);
12516
12517 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12518 }
12519
12520 return this_type;
12521 }
12522
12523 /* Return the type in DIE, CU.
12524 Returns NULL for invalid types.
12525
12526 This first does a lookup in the appropriate type_hash table,
12527 and only reads the die in if necessary.
12528
12529 NOTE: This can be called when reading in partial or full symbols. */
12530
12531 static struct type *
12532 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12533 {
12534 struct type *this_type;
12535
12536 this_type = get_die_type (die, cu);
12537 if (this_type)
12538 return this_type;
12539
12540 return read_type_die_1 (die, cu);
12541 }
12542
12543 /* Read the type in DIE, CU.
12544 Returns NULL for invalid types. */
12545
12546 static struct type *
12547 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12548 {
12549 struct type *this_type = NULL;
12550
12551 switch (die->tag)
12552 {
12553 case DW_TAG_class_type:
12554 case DW_TAG_interface_type:
12555 case DW_TAG_structure_type:
12556 case DW_TAG_union_type:
12557 this_type = read_structure_type (die, cu);
12558 break;
12559 case DW_TAG_enumeration_type:
12560 this_type = read_enumeration_type (die, cu);
12561 break;
12562 case DW_TAG_subprogram:
12563 case DW_TAG_subroutine_type:
12564 case DW_TAG_inlined_subroutine:
12565 this_type = read_subroutine_type (die, cu);
12566 break;
12567 case DW_TAG_array_type:
12568 this_type = read_array_type (die, cu);
12569 break;
12570 case DW_TAG_set_type:
12571 this_type = read_set_type (die, cu);
12572 break;
12573 case DW_TAG_pointer_type:
12574 this_type = read_tag_pointer_type (die, cu);
12575 break;
12576 case DW_TAG_ptr_to_member_type:
12577 this_type = read_tag_ptr_to_member_type (die, cu);
12578 break;
12579 case DW_TAG_reference_type:
12580 this_type = read_tag_reference_type (die, cu);
12581 break;
12582 case DW_TAG_const_type:
12583 this_type = read_tag_const_type (die, cu);
12584 break;
12585 case DW_TAG_volatile_type:
12586 this_type = read_tag_volatile_type (die, cu);
12587 break;
12588 case DW_TAG_string_type:
12589 this_type = read_tag_string_type (die, cu);
12590 break;
12591 case DW_TAG_typedef:
12592 this_type = read_typedef (die, cu);
12593 break;
12594 case DW_TAG_subrange_type:
12595 this_type = read_subrange_type (die, cu);
12596 break;
12597 case DW_TAG_base_type:
12598 this_type = read_base_type (die, cu);
12599 break;
12600 case DW_TAG_unspecified_type:
12601 this_type = read_unspecified_type (die, cu);
12602 break;
12603 case DW_TAG_namespace:
12604 this_type = read_namespace_type (die, cu);
12605 break;
12606 case DW_TAG_module:
12607 this_type = read_module_type (die, cu);
12608 break;
12609 default:
12610 complaint (&symfile_complaints,
12611 _("unexpected tag in read_type_die: '%s'"),
12612 dwarf_tag_name (die->tag));
12613 break;
12614 }
12615
12616 return this_type;
12617 }
12618
12619 /* See if we can figure out if the class lives in a namespace. We do
12620 this by looking for a member function; its demangled name will
12621 contain namespace info, if there is any.
12622 Return the computed name or NULL.
12623 Space for the result is allocated on the objfile's obstack.
12624 This is the full-die version of guess_partial_die_structure_name.
12625 In this case we know DIE has no useful parent. */
12626
12627 static char *
12628 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12629 {
12630 struct die_info *spec_die;
12631 struct dwarf2_cu *spec_cu;
12632 struct die_info *child;
12633
12634 spec_cu = cu;
12635 spec_die = die_specification (die, &spec_cu);
12636 if (spec_die != NULL)
12637 {
12638 die = spec_die;
12639 cu = spec_cu;
12640 }
12641
12642 for (child = die->child;
12643 child != NULL;
12644 child = child->sibling)
12645 {
12646 if (child->tag == DW_TAG_subprogram)
12647 {
12648 struct attribute *attr;
12649
12650 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12651 if (attr == NULL)
12652 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12653 if (attr != NULL)
12654 {
12655 char *actual_name
12656 = language_class_name_from_physname (cu->language_defn,
12657 DW_STRING (attr));
12658 char *name = NULL;
12659
12660 if (actual_name != NULL)
12661 {
12662 char *die_name = dwarf2_name (die, cu);
12663
12664 if (die_name != NULL
12665 && strcmp (die_name, actual_name) != 0)
12666 {
12667 /* Strip off the class name from the full name.
12668 We want the prefix. */
12669 int die_name_len = strlen (die_name);
12670 int actual_name_len = strlen (actual_name);
12671
12672 /* Test for '::' as a sanity check. */
12673 if (actual_name_len > die_name_len + 2
12674 && actual_name[actual_name_len
12675 - die_name_len - 1] == ':')
12676 name =
12677 obsavestring (actual_name,
12678 actual_name_len - die_name_len - 2,
12679 &cu->objfile->objfile_obstack);
12680 }
12681 }
12682 xfree (actual_name);
12683 return name;
12684 }
12685 }
12686 }
12687
12688 return NULL;
12689 }
12690
12691 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12692 prefix part in such case. See
12693 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12694
12695 static char *
12696 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12697 {
12698 struct attribute *attr;
12699 char *base;
12700
12701 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12702 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12703 return NULL;
12704
12705 attr = dwarf2_attr (die, DW_AT_name, cu);
12706 if (attr != NULL && DW_STRING (attr) != NULL)
12707 return NULL;
12708
12709 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12710 if (attr == NULL)
12711 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12712 if (attr == NULL || DW_STRING (attr) == NULL)
12713 return NULL;
12714
12715 /* dwarf2_name had to be already called. */
12716 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12717
12718 /* Strip the base name, keep any leading namespaces/classes. */
12719 base = strrchr (DW_STRING (attr), ':');
12720 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12721 return "";
12722
12723 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12724 &cu->objfile->objfile_obstack);
12725 }
12726
12727 /* Return the name of the namespace/class that DIE is defined within,
12728 or "" if we can't tell. The caller should not xfree the result.
12729
12730 For example, if we're within the method foo() in the following
12731 code:
12732
12733 namespace N {
12734 class C {
12735 void foo () {
12736 }
12737 };
12738 }
12739
12740 then determine_prefix on foo's die will return "N::C". */
12741
12742 static const char *
12743 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12744 {
12745 struct die_info *parent, *spec_die;
12746 struct dwarf2_cu *spec_cu;
12747 struct type *parent_type;
12748 char *retval;
12749
12750 if (cu->language != language_cplus && cu->language != language_java
12751 && cu->language != language_fortran)
12752 return "";
12753
12754 retval = anonymous_struct_prefix (die, cu);
12755 if (retval)
12756 return retval;
12757
12758 /* We have to be careful in the presence of DW_AT_specification.
12759 For example, with GCC 3.4, given the code
12760
12761 namespace N {
12762 void foo() {
12763 // Definition of N::foo.
12764 }
12765 }
12766
12767 then we'll have a tree of DIEs like this:
12768
12769 1: DW_TAG_compile_unit
12770 2: DW_TAG_namespace // N
12771 3: DW_TAG_subprogram // declaration of N::foo
12772 4: DW_TAG_subprogram // definition of N::foo
12773 DW_AT_specification // refers to die #3
12774
12775 Thus, when processing die #4, we have to pretend that we're in
12776 the context of its DW_AT_specification, namely the contex of die
12777 #3. */
12778 spec_cu = cu;
12779 spec_die = die_specification (die, &spec_cu);
12780 if (spec_die == NULL)
12781 parent = die->parent;
12782 else
12783 {
12784 parent = spec_die->parent;
12785 cu = spec_cu;
12786 }
12787
12788 if (parent == NULL)
12789 return "";
12790 else if (parent->building_fullname)
12791 {
12792 const char *name;
12793 const char *parent_name;
12794
12795 /* It has been seen on RealView 2.2 built binaries,
12796 DW_TAG_template_type_param types actually _defined_ as
12797 children of the parent class:
12798
12799 enum E {};
12800 template class <class Enum> Class{};
12801 Class<enum E> class_e;
12802
12803 1: DW_TAG_class_type (Class)
12804 2: DW_TAG_enumeration_type (E)
12805 3: DW_TAG_enumerator (enum1:0)
12806 3: DW_TAG_enumerator (enum2:1)
12807 ...
12808 2: DW_TAG_template_type_param
12809 DW_AT_type DW_FORM_ref_udata (E)
12810
12811 Besides being broken debug info, it can put GDB into an
12812 infinite loop. Consider:
12813
12814 When we're building the full name for Class<E>, we'll start
12815 at Class, and go look over its template type parameters,
12816 finding E. We'll then try to build the full name of E, and
12817 reach here. We're now trying to build the full name of E,
12818 and look over the parent DIE for containing scope. In the
12819 broken case, if we followed the parent DIE of E, we'd again
12820 find Class, and once again go look at its template type
12821 arguments, etc., etc. Simply don't consider such parent die
12822 as source-level parent of this die (it can't be, the language
12823 doesn't allow it), and break the loop here. */
12824 name = dwarf2_name (die, cu);
12825 parent_name = dwarf2_name (parent, cu);
12826 complaint (&symfile_complaints,
12827 _("template param type '%s' defined within parent '%s'"),
12828 name ? name : "<unknown>",
12829 parent_name ? parent_name : "<unknown>");
12830 return "";
12831 }
12832 else
12833 switch (parent->tag)
12834 {
12835 case DW_TAG_namespace:
12836 parent_type = read_type_die (parent, cu);
12837 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12838 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12839 Work around this problem here. */
12840 if (cu->language == language_cplus
12841 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12842 return "";
12843 /* We give a name to even anonymous namespaces. */
12844 return TYPE_TAG_NAME (parent_type);
12845 case DW_TAG_class_type:
12846 case DW_TAG_interface_type:
12847 case DW_TAG_structure_type:
12848 case DW_TAG_union_type:
12849 case DW_TAG_module:
12850 parent_type = read_type_die (parent, cu);
12851 if (TYPE_TAG_NAME (parent_type) != NULL)
12852 return TYPE_TAG_NAME (parent_type);
12853 else
12854 /* An anonymous structure is only allowed non-static data
12855 members; no typedefs, no member functions, et cetera.
12856 So it does not need a prefix. */
12857 return "";
12858 case DW_TAG_compile_unit:
12859 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12860 if (cu->language == language_cplus
12861 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12862 && die->child != NULL
12863 && (die->tag == DW_TAG_class_type
12864 || die->tag == DW_TAG_structure_type
12865 || die->tag == DW_TAG_union_type))
12866 {
12867 char *name = guess_full_die_structure_name (die, cu);
12868 if (name != NULL)
12869 return name;
12870 }
12871 return "";
12872 default:
12873 return determine_prefix (parent, cu);
12874 }
12875 }
12876
12877 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12878 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12879 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12880 an obconcat, otherwise allocate storage for the result. The CU argument is
12881 used to determine the language and hence, the appropriate separator. */
12882
12883 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12884
12885 static char *
12886 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12887 int physname, struct dwarf2_cu *cu)
12888 {
12889 const char *lead = "";
12890 const char *sep;
12891
12892 if (suffix == NULL || suffix[0] == '\0'
12893 || prefix == NULL || prefix[0] == '\0')
12894 sep = "";
12895 else if (cu->language == language_java)
12896 sep = ".";
12897 else if (cu->language == language_fortran && physname)
12898 {
12899 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12900 DW_AT_MIPS_linkage_name is preferred and used instead. */
12901
12902 lead = "__";
12903 sep = "_MOD_";
12904 }
12905 else
12906 sep = "::";
12907
12908 if (prefix == NULL)
12909 prefix = "";
12910 if (suffix == NULL)
12911 suffix = "";
12912
12913 if (obs == NULL)
12914 {
12915 char *retval
12916 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12917
12918 strcpy (retval, lead);
12919 strcat (retval, prefix);
12920 strcat (retval, sep);
12921 strcat (retval, suffix);
12922 return retval;
12923 }
12924 else
12925 {
12926 /* We have an obstack. */
12927 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12928 }
12929 }
12930
12931 /* Return sibling of die, NULL if no sibling. */
12932
12933 static struct die_info *
12934 sibling_die (struct die_info *die)
12935 {
12936 return die->sibling;
12937 }
12938
12939 /* Get name of a die, return NULL if not found. */
12940
12941 static char *
12942 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12943 struct obstack *obstack)
12944 {
12945 if (name && cu->language == language_cplus)
12946 {
12947 char *canon_name = cp_canonicalize_string (name);
12948
12949 if (canon_name != NULL)
12950 {
12951 if (strcmp (canon_name, name) != 0)
12952 name = obsavestring (canon_name, strlen (canon_name),
12953 obstack);
12954 xfree (canon_name);
12955 }
12956 }
12957
12958 return name;
12959 }
12960
12961 /* Get name of a die, return NULL if not found. */
12962
12963 static char *
12964 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12965 {
12966 struct attribute *attr;
12967
12968 attr = dwarf2_attr (die, DW_AT_name, cu);
12969 if ((!attr || !DW_STRING (attr))
12970 && die->tag != DW_TAG_class_type
12971 && die->tag != DW_TAG_interface_type
12972 && die->tag != DW_TAG_structure_type
12973 && die->tag != DW_TAG_union_type)
12974 return NULL;
12975
12976 switch (die->tag)
12977 {
12978 case DW_TAG_compile_unit:
12979 /* Compilation units have a DW_AT_name that is a filename, not
12980 a source language identifier. */
12981 case DW_TAG_enumeration_type:
12982 case DW_TAG_enumerator:
12983 /* These tags always have simple identifiers already; no need
12984 to canonicalize them. */
12985 return DW_STRING (attr);
12986
12987 case DW_TAG_subprogram:
12988 /* Java constructors will all be named "<init>", so return
12989 the class name when we see this special case. */
12990 if (cu->language == language_java
12991 && DW_STRING (attr) != NULL
12992 && strcmp (DW_STRING (attr), "<init>") == 0)
12993 {
12994 struct dwarf2_cu *spec_cu = cu;
12995 struct die_info *spec_die;
12996
12997 /* GCJ will output '<init>' for Java constructor names.
12998 For this special case, return the name of the parent class. */
12999
13000 /* GCJ may output suprogram DIEs with AT_specification set.
13001 If so, use the name of the specified DIE. */
13002 spec_die = die_specification (die, &spec_cu);
13003 if (spec_die != NULL)
13004 return dwarf2_name (spec_die, spec_cu);
13005
13006 do
13007 {
13008 die = die->parent;
13009 if (die->tag == DW_TAG_class_type)
13010 return dwarf2_name (die, cu);
13011 }
13012 while (die->tag != DW_TAG_compile_unit);
13013 }
13014 break;
13015
13016 case DW_TAG_class_type:
13017 case DW_TAG_interface_type:
13018 case DW_TAG_structure_type:
13019 case DW_TAG_union_type:
13020 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
13021 structures or unions. These were of the form "._%d" in GCC 4.1,
13022 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
13023 and GCC 4.4. We work around this problem by ignoring these. */
13024 if (attr && DW_STRING (attr)
13025 && (strncmp (DW_STRING (attr), "._", 2) == 0
13026 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
13027 return NULL;
13028
13029 /* GCC might emit a nameless typedef that has a linkage name. See
13030 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
13031 if (!attr || DW_STRING (attr) == NULL)
13032 {
13033 char *demangled = NULL;
13034
13035 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
13036 if (attr == NULL)
13037 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
13038
13039 if (attr == NULL || DW_STRING (attr) == NULL)
13040 return NULL;
13041
13042 /* Avoid demangling DW_STRING (attr) the second time on a second
13043 call for the same DIE. */
13044 if (!DW_STRING_IS_CANONICAL (attr))
13045 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
13046
13047 if (demangled)
13048 {
13049 char *base;
13050
13051 /* FIXME: we already did this for the partial symbol... */
13052 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
13053 &cu->objfile->objfile_obstack);
13054 DW_STRING_IS_CANONICAL (attr) = 1;
13055 xfree (demangled);
13056
13057 /* Strip any leading namespaces/classes, keep only the base name.
13058 DW_AT_name for named DIEs does not contain the prefixes. */
13059 base = strrchr (DW_STRING (attr), ':');
13060 if (base && base > DW_STRING (attr) && base[-1] == ':')
13061 return &base[1];
13062 else
13063 return DW_STRING (attr);
13064 }
13065 }
13066 break;
13067
13068 default:
13069 break;
13070 }
13071
13072 if (!DW_STRING_IS_CANONICAL (attr))
13073 {
13074 DW_STRING (attr)
13075 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
13076 &cu->objfile->objfile_obstack);
13077 DW_STRING_IS_CANONICAL (attr) = 1;
13078 }
13079 return DW_STRING (attr);
13080 }
13081
13082 /* Return the die that this die in an extension of, or NULL if there
13083 is none. *EXT_CU is the CU containing DIE on input, and the CU
13084 containing the return value on output. */
13085
13086 static struct die_info *
13087 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
13088 {
13089 struct attribute *attr;
13090
13091 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
13092 if (attr == NULL)
13093 return NULL;
13094
13095 return follow_die_ref (die, attr, ext_cu);
13096 }
13097
13098 /* Convert a DIE tag into its string name. */
13099
13100 static char *
13101 dwarf_tag_name (unsigned tag)
13102 {
13103 switch (tag)
13104 {
13105 case DW_TAG_padding:
13106 return "DW_TAG_padding";
13107 case DW_TAG_array_type:
13108 return "DW_TAG_array_type";
13109 case DW_TAG_class_type:
13110 return "DW_TAG_class_type";
13111 case DW_TAG_entry_point:
13112 return "DW_TAG_entry_point";
13113 case DW_TAG_enumeration_type:
13114 return "DW_TAG_enumeration_type";
13115 case DW_TAG_formal_parameter:
13116 return "DW_TAG_formal_parameter";
13117 case DW_TAG_imported_declaration:
13118 return "DW_TAG_imported_declaration";
13119 case DW_TAG_label:
13120 return "DW_TAG_label";
13121 case DW_TAG_lexical_block:
13122 return "DW_TAG_lexical_block";
13123 case DW_TAG_member:
13124 return "DW_TAG_member";
13125 case DW_TAG_pointer_type:
13126 return "DW_TAG_pointer_type";
13127 case DW_TAG_reference_type:
13128 return "DW_TAG_reference_type";
13129 case DW_TAG_compile_unit:
13130 return "DW_TAG_compile_unit";
13131 case DW_TAG_string_type:
13132 return "DW_TAG_string_type";
13133 case DW_TAG_structure_type:
13134 return "DW_TAG_structure_type";
13135 case DW_TAG_subroutine_type:
13136 return "DW_TAG_subroutine_type";
13137 case DW_TAG_typedef:
13138 return "DW_TAG_typedef";
13139 case DW_TAG_union_type:
13140 return "DW_TAG_union_type";
13141 case DW_TAG_unspecified_parameters:
13142 return "DW_TAG_unspecified_parameters";
13143 case DW_TAG_variant:
13144 return "DW_TAG_variant";
13145 case DW_TAG_common_block:
13146 return "DW_TAG_common_block";
13147 case DW_TAG_common_inclusion:
13148 return "DW_TAG_common_inclusion";
13149 case DW_TAG_inheritance:
13150 return "DW_TAG_inheritance";
13151 case DW_TAG_inlined_subroutine:
13152 return "DW_TAG_inlined_subroutine";
13153 case DW_TAG_module:
13154 return "DW_TAG_module";
13155 case DW_TAG_ptr_to_member_type:
13156 return "DW_TAG_ptr_to_member_type";
13157 case DW_TAG_set_type:
13158 return "DW_TAG_set_type";
13159 case DW_TAG_subrange_type:
13160 return "DW_TAG_subrange_type";
13161 case DW_TAG_with_stmt:
13162 return "DW_TAG_with_stmt";
13163 case DW_TAG_access_declaration:
13164 return "DW_TAG_access_declaration";
13165 case DW_TAG_base_type:
13166 return "DW_TAG_base_type";
13167 case DW_TAG_catch_block:
13168 return "DW_TAG_catch_block";
13169 case DW_TAG_const_type:
13170 return "DW_TAG_const_type";
13171 case DW_TAG_constant:
13172 return "DW_TAG_constant";
13173 case DW_TAG_enumerator:
13174 return "DW_TAG_enumerator";
13175 case DW_TAG_file_type:
13176 return "DW_TAG_file_type";
13177 case DW_TAG_friend:
13178 return "DW_TAG_friend";
13179 case DW_TAG_namelist:
13180 return "DW_TAG_namelist";
13181 case DW_TAG_namelist_item:
13182 return "DW_TAG_namelist_item";
13183 case DW_TAG_packed_type:
13184 return "DW_TAG_packed_type";
13185 case DW_TAG_subprogram:
13186 return "DW_TAG_subprogram";
13187 case DW_TAG_template_type_param:
13188 return "DW_TAG_template_type_param";
13189 case DW_TAG_template_value_param:
13190 return "DW_TAG_template_value_param";
13191 case DW_TAG_thrown_type:
13192 return "DW_TAG_thrown_type";
13193 case DW_TAG_try_block:
13194 return "DW_TAG_try_block";
13195 case DW_TAG_variant_part:
13196 return "DW_TAG_variant_part";
13197 case DW_TAG_variable:
13198 return "DW_TAG_variable";
13199 case DW_TAG_volatile_type:
13200 return "DW_TAG_volatile_type";
13201 case DW_TAG_dwarf_procedure:
13202 return "DW_TAG_dwarf_procedure";
13203 case DW_TAG_restrict_type:
13204 return "DW_TAG_restrict_type";
13205 case DW_TAG_interface_type:
13206 return "DW_TAG_interface_type";
13207 case DW_TAG_namespace:
13208 return "DW_TAG_namespace";
13209 case DW_TAG_imported_module:
13210 return "DW_TAG_imported_module";
13211 case DW_TAG_unspecified_type:
13212 return "DW_TAG_unspecified_type";
13213 case DW_TAG_partial_unit:
13214 return "DW_TAG_partial_unit";
13215 case DW_TAG_imported_unit:
13216 return "DW_TAG_imported_unit";
13217 case DW_TAG_condition:
13218 return "DW_TAG_condition";
13219 case DW_TAG_shared_type:
13220 return "DW_TAG_shared_type";
13221 case DW_TAG_type_unit:
13222 return "DW_TAG_type_unit";
13223 case DW_TAG_MIPS_loop:
13224 return "DW_TAG_MIPS_loop";
13225 case DW_TAG_HP_array_descriptor:
13226 return "DW_TAG_HP_array_descriptor";
13227 case DW_TAG_format_label:
13228 return "DW_TAG_format_label";
13229 case DW_TAG_function_template:
13230 return "DW_TAG_function_template";
13231 case DW_TAG_class_template:
13232 return "DW_TAG_class_template";
13233 case DW_TAG_GNU_BINCL:
13234 return "DW_TAG_GNU_BINCL";
13235 case DW_TAG_GNU_EINCL:
13236 return "DW_TAG_GNU_EINCL";
13237 case DW_TAG_upc_shared_type:
13238 return "DW_TAG_upc_shared_type";
13239 case DW_TAG_upc_strict_type:
13240 return "DW_TAG_upc_strict_type";
13241 case DW_TAG_upc_relaxed_type:
13242 return "DW_TAG_upc_relaxed_type";
13243 case DW_TAG_PGI_kanji_type:
13244 return "DW_TAG_PGI_kanji_type";
13245 case DW_TAG_PGI_interface_block:
13246 return "DW_TAG_PGI_interface_block";
13247 case DW_TAG_GNU_call_site:
13248 return "DW_TAG_GNU_call_site";
13249 default:
13250 return "DW_TAG_<unknown>";
13251 }
13252 }
13253
13254 /* Convert a DWARF attribute code into its string name. */
13255
13256 static char *
13257 dwarf_attr_name (unsigned attr)
13258 {
13259 switch (attr)
13260 {
13261 case DW_AT_sibling:
13262 return "DW_AT_sibling";
13263 case DW_AT_location:
13264 return "DW_AT_location";
13265 case DW_AT_name:
13266 return "DW_AT_name";
13267 case DW_AT_ordering:
13268 return "DW_AT_ordering";
13269 case DW_AT_subscr_data:
13270 return "DW_AT_subscr_data";
13271 case DW_AT_byte_size:
13272 return "DW_AT_byte_size";
13273 case DW_AT_bit_offset:
13274 return "DW_AT_bit_offset";
13275 case DW_AT_bit_size:
13276 return "DW_AT_bit_size";
13277 case DW_AT_element_list:
13278 return "DW_AT_element_list";
13279 case DW_AT_stmt_list:
13280 return "DW_AT_stmt_list";
13281 case DW_AT_low_pc:
13282 return "DW_AT_low_pc";
13283 case DW_AT_high_pc:
13284 return "DW_AT_high_pc";
13285 case DW_AT_language:
13286 return "DW_AT_language";
13287 case DW_AT_member:
13288 return "DW_AT_member";
13289 case DW_AT_discr:
13290 return "DW_AT_discr";
13291 case DW_AT_discr_value:
13292 return "DW_AT_discr_value";
13293 case DW_AT_visibility:
13294 return "DW_AT_visibility";
13295 case DW_AT_import:
13296 return "DW_AT_import";
13297 case DW_AT_string_length:
13298 return "DW_AT_string_length";
13299 case DW_AT_common_reference:
13300 return "DW_AT_common_reference";
13301 case DW_AT_comp_dir:
13302 return "DW_AT_comp_dir";
13303 case DW_AT_const_value:
13304 return "DW_AT_const_value";
13305 case DW_AT_containing_type:
13306 return "DW_AT_containing_type";
13307 case DW_AT_default_value:
13308 return "DW_AT_default_value";
13309 case DW_AT_inline:
13310 return "DW_AT_inline";
13311 case DW_AT_is_optional:
13312 return "DW_AT_is_optional";
13313 case DW_AT_lower_bound:
13314 return "DW_AT_lower_bound";
13315 case DW_AT_producer:
13316 return "DW_AT_producer";
13317 case DW_AT_prototyped:
13318 return "DW_AT_prototyped";
13319 case DW_AT_return_addr:
13320 return "DW_AT_return_addr";
13321 case DW_AT_start_scope:
13322 return "DW_AT_start_scope";
13323 case DW_AT_bit_stride:
13324 return "DW_AT_bit_stride";
13325 case DW_AT_upper_bound:
13326 return "DW_AT_upper_bound";
13327 case DW_AT_abstract_origin:
13328 return "DW_AT_abstract_origin";
13329 case DW_AT_accessibility:
13330 return "DW_AT_accessibility";
13331 case DW_AT_address_class:
13332 return "DW_AT_address_class";
13333 case DW_AT_artificial:
13334 return "DW_AT_artificial";
13335 case DW_AT_base_types:
13336 return "DW_AT_base_types";
13337 case DW_AT_calling_convention:
13338 return "DW_AT_calling_convention";
13339 case DW_AT_count:
13340 return "DW_AT_count";
13341 case DW_AT_data_member_location:
13342 return "DW_AT_data_member_location";
13343 case DW_AT_decl_column:
13344 return "DW_AT_decl_column";
13345 case DW_AT_decl_file:
13346 return "DW_AT_decl_file";
13347 case DW_AT_decl_line:
13348 return "DW_AT_decl_line";
13349 case DW_AT_declaration:
13350 return "DW_AT_declaration";
13351 case DW_AT_discr_list:
13352 return "DW_AT_discr_list";
13353 case DW_AT_encoding:
13354 return "DW_AT_encoding";
13355 case DW_AT_external:
13356 return "DW_AT_external";
13357 case DW_AT_frame_base:
13358 return "DW_AT_frame_base";
13359 case DW_AT_friend:
13360 return "DW_AT_friend";
13361 case DW_AT_identifier_case:
13362 return "DW_AT_identifier_case";
13363 case DW_AT_macro_info:
13364 return "DW_AT_macro_info";
13365 case DW_AT_namelist_items:
13366 return "DW_AT_namelist_items";
13367 case DW_AT_priority:
13368 return "DW_AT_priority";
13369 case DW_AT_segment:
13370 return "DW_AT_segment";
13371 case DW_AT_specification:
13372 return "DW_AT_specification";
13373 case DW_AT_static_link:
13374 return "DW_AT_static_link";
13375 case DW_AT_type:
13376 return "DW_AT_type";
13377 case DW_AT_use_location:
13378 return "DW_AT_use_location";
13379 case DW_AT_variable_parameter:
13380 return "DW_AT_variable_parameter";
13381 case DW_AT_virtuality:
13382 return "DW_AT_virtuality";
13383 case DW_AT_vtable_elem_location:
13384 return "DW_AT_vtable_elem_location";
13385 /* DWARF 3 values. */
13386 case DW_AT_allocated:
13387 return "DW_AT_allocated";
13388 case DW_AT_associated:
13389 return "DW_AT_associated";
13390 case DW_AT_data_location:
13391 return "DW_AT_data_location";
13392 case DW_AT_byte_stride:
13393 return "DW_AT_byte_stride";
13394 case DW_AT_entry_pc:
13395 return "DW_AT_entry_pc";
13396 case DW_AT_use_UTF8:
13397 return "DW_AT_use_UTF8";
13398 case DW_AT_extension:
13399 return "DW_AT_extension";
13400 case DW_AT_ranges:
13401 return "DW_AT_ranges";
13402 case DW_AT_trampoline:
13403 return "DW_AT_trampoline";
13404 case DW_AT_call_column:
13405 return "DW_AT_call_column";
13406 case DW_AT_call_file:
13407 return "DW_AT_call_file";
13408 case DW_AT_call_line:
13409 return "DW_AT_call_line";
13410 case DW_AT_description:
13411 return "DW_AT_description";
13412 case DW_AT_binary_scale:
13413 return "DW_AT_binary_scale";
13414 case DW_AT_decimal_scale:
13415 return "DW_AT_decimal_scale";
13416 case DW_AT_small:
13417 return "DW_AT_small";
13418 case DW_AT_decimal_sign:
13419 return "DW_AT_decimal_sign";
13420 case DW_AT_digit_count:
13421 return "DW_AT_digit_count";
13422 case DW_AT_picture_string:
13423 return "DW_AT_picture_string";
13424 case DW_AT_mutable:
13425 return "DW_AT_mutable";
13426 case DW_AT_threads_scaled:
13427 return "DW_AT_threads_scaled";
13428 case DW_AT_explicit:
13429 return "DW_AT_explicit";
13430 case DW_AT_object_pointer:
13431 return "DW_AT_object_pointer";
13432 case DW_AT_endianity:
13433 return "DW_AT_endianity";
13434 case DW_AT_elemental:
13435 return "DW_AT_elemental";
13436 case DW_AT_pure:
13437 return "DW_AT_pure";
13438 case DW_AT_recursive:
13439 return "DW_AT_recursive";
13440 /* DWARF 4 values. */
13441 case DW_AT_signature:
13442 return "DW_AT_signature";
13443 case DW_AT_linkage_name:
13444 return "DW_AT_linkage_name";
13445 /* SGI/MIPS extensions. */
13446 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13447 case DW_AT_MIPS_fde:
13448 return "DW_AT_MIPS_fde";
13449 #endif
13450 case DW_AT_MIPS_loop_begin:
13451 return "DW_AT_MIPS_loop_begin";
13452 case DW_AT_MIPS_tail_loop_begin:
13453 return "DW_AT_MIPS_tail_loop_begin";
13454 case DW_AT_MIPS_epilog_begin:
13455 return "DW_AT_MIPS_epilog_begin";
13456 case DW_AT_MIPS_loop_unroll_factor:
13457 return "DW_AT_MIPS_loop_unroll_factor";
13458 case DW_AT_MIPS_software_pipeline_depth:
13459 return "DW_AT_MIPS_software_pipeline_depth";
13460 case DW_AT_MIPS_linkage_name:
13461 return "DW_AT_MIPS_linkage_name";
13462 case DW_AT_MIPS_stride:
13463 return "DW_AT_MIPS_stride";
13464 case DW_AT_MIPS_abstract_name:
13465 return "DW_AT_MIPS_abstract_name";
13466 case DW_AT_MIPS_clone_origin:
13467 return "DW_AT_MIPS_clone_origin";
13468 case DW_AT_MIPS_has_inlines:
13469 return "DW_AT_MIPS_has_inlines";
13470 /* HP extensions. */
13471 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13472 case DW_AT_HP_block_index:
13473 return "DW_AT_HP_block_index";
13474 #endif
13475 case DW_AT_HP_unmodifiable:
13476 return "DW_AT_HP_unmodifiable";
13477 case DW_AT_HP_actuals_stmt_list:
13478 return "DW_AT_HP_actuals_stmt_list";
13479 case DW_AT_HP_proc_per_section:
13480 return "DW_AT_HP_proc_per_section";
13481 case DW_AT_HP_raw_data_ptr:
13482 return "DW_AT_HP_raw_data_ptr";
13483 case DW_AT_HP_pass_by_reference:
13484 return "DW_AT_HP_pass_by_reference";
13485 case DW_AT_HP_opt_level:
13486 return "DW_AT_HP_opt_level";
13487 case DW_AT_HP_prof_version_id:
13488 return "DW_AT_HP_prof_version_id";
13489 case DW_AT_HP_opt_flags:
13490 return "DW_AT_HP_opt_flags";
13491 case DW_AT_HP_cold_region_low_pc:
13492 return "DW_AT_HP_cold_region_low_pc";
13493 case DW_AT_HP_cold_region_high_pc:
13494 return "DW_AT_HP_cold_region_high_pc";
13495 case DW_AT_HP_all_variables_modifiable:
13496 return "DW_AT_HP_all_variables_modifiable";
13497 case DW_AT_HP_linkage_name:
13498 return "DW_AT_HP_linkage_name";
13499 case DW_AT_HP_prof_flags:
13500 return "DW_AT_HP_prof_flags";
13501 /* GNU extensions. */
13502 case DW_AT_sf_names:
13503 return "DW_AT_sf_names";
13504 case DW_AT_src_info:
13505 return "DW_AT_src_info";
13506 case DW_AT_mac_info:
13507 return "DW_AT_mac_info";
13508 case DW_AT_src_coords:
13509 return "DW_AT_src_coords";
13510 case DW_AT_body_begin:
13511 return "DW_AT_body_begin";
13512 case DW_AT_body_end:
13513 return "DW_AT_body_end";
13514 case DW_AT_GNU_vector:
13515 return "DW_AT_GNU_vector";
13516 case DW_AT_GNU_odr_signature:
13517 return "DW_AT_GNU_odr_signature";
13518 /* VMS extensions. */
13519 case DW_AT_VMS_rtnbeg_pd_address:
13520 return "DW_AT_VMS_rtnbeg_pd_address";
13521 /* UPC extension. */
13522 case DW_AT_upc_threads_scaled:
13523 return "DW_AT_upc_threads_scaled";
13524 /* PGI (STMicroelectronics) extensions. */
13525 case DW_AT_PGI_lbase:
13526 return "DW_AT_PGI_lbase";
13527 case DW_AT_PGI_soffset:
13528 return "DW_AT_PGI_soffset";
13529 case DW_AT_PGI_lstride:
13530 return "DW_AT_PGI_lstride";
13531 default:
13532 return "DW_AT_<unknown>";
13533 }
13534 }
13535
13536 /* Convert a DWARF value form code into its string name. */
13537
13538 static char *
13539 dwarf_form_name (unsigned form)
13540 {
13541 switch (form)
13542 {
13543 case DW_FORM_addr:
13544 return "DW_FORM_addr";
13545 case DW_FORM_block2:
13546 return "DW_FORM_block2";
13547 case DW_FORM_block4:
13548 return "DW_FORM_block4";
13549 case DW_FORM_data2:
13550 return "DW_FORM_data2";
13551 case DW_FORM_data4:
13552 return "DW_FORM_data4";
13553 case DW_FORM_data8:
13554 return "DW_FORM_data8";
13555 case DW_FORM_string:
13556 return "DW_FORM_string";
13557 case DW_FORM_block:
13558 return "DW_FORM_block";
13559 case DW_FORM_block1:
13560 return "DW_FORM_block1";
13561 case DW_FORM_data1:
13562 return "DW_FORM_data1";
13563 case DW_FORM_flag:
13564 return "DW_FORM_flag";
13565 case DW_FORM_sdata:
13566 return "DW_FORM_sdata";
13567 case DW_FORM_strp:
13568 return "DW_FORM_strp";
13569 case DW_FORM_udata:
13570 return "DW_FORM_udata";
13571 case DW_FORM_ref_addr:
13572 return "DW_FORM_ref_addr";
13573 case DW_FORM_ref1:
13574 return "DW_FORM_ref1";
13575 case DW_FORM_ref2:
13576 return "DW_FORM_ref2";
13577 case DW_FORM_ref4:
13578 return "DW_FORM_ref4";
13579 case DW_FORM_ref8:
13580 return "DW_FORM_ref8";
13581 case DW_FORM_ref_udata:
13582 return "DW_FORM_ref_udata";
13583 case DW_FORM_indirect:
13584 return "DW_FORM_indirect";
13585 case DW_FORM_sec_offset:
13586 return "DW_FORM_sec_offset";
13587 case DW_FORM_exprloc:
13588 return "DW_FORM_exprloc";
13589 case DW_FORM_flag_present:
13590 return "DW_FORM_flag_present";
13591 case DW_FORM_ref_sig8:
13592 return "DW_FORM_ref_sig8";
13593 default:
13594 return "DW_FORM_<unknown>";
13595 }
13596 }
13597
13598 /* Convert a DWARF stack opcode into its string name. */
13599
13600 const char *
13601 dwarf_stack_op_name (unsigned op)
13602 {
13603 switch (op)
13604 {
13605 case DW_OP_addr:
13606 return "DW_OP_addr";
13607 case DW_OP_deref:
13608 return "DW_OP_deref";
13609 case DW_OP_const1u:
13610 return "DW_OP_const1u";
13611 case DW_OP_const1s:
13612 return "DW_OP_const1s";
13613 case DW_OP_const2u:
13614 return "DW_OP_const2u";
13615 case DW_OP_const2s:
13616 return "DW_OP_const2s";
13617 case DW_OP_const4u:
13618 return "DW_OP_const4u";
13619 case DW_OP_const4s:
13620 return "DW_OP_const4s";
13621 case DW_OP_const8u:
13622 return "DW_OP_const8u";
13623 case DW_OP_const8s:
13624 return "DW_OP_const8s";
13625 case DW_OP_constu:
13626 return "DW_OP_constu";
13627 case DW_OP_consts:
13628 return "DW_OP_consts";
13629 case DW_OP_dup:
13630 return "DW_OP_dup";
13631 case DW_OP_drop:
13632 return "DW_OP_drop";
13633 case DW_OP_over:
13634 return "DW_OP_over";
13635 case DW_OP_pick:
13636 return "DW_OP_pick";
13637 case DW_OP_swap:
13638 return "DW_OP_swap";
13639 case DW_OP_rot:
13640 return "DW_OP_rot";
13641 case DW_OP_xderef:
13642 return "DW_OP_xderef";
13643 case DW_OP_abs:
13644 return "DW_OP_abs";
13645 case DW_OP_and:
13646 return "DW_OP_and";
13647 case DW_OP_div:
13648 return "DW_OP_div";
13649 case DW_OP_minus:
13650 return "DW_OP_minus";
13651 case DW_OP_mod:
13652 return "DW_OP_mod";
13653 case DW_OP_mul:
13654 return "DW_OP_mul";
13655 case DW_OP_neg:
13656 return "DW_OP_neg";
13657 case DW_OP_not:
13658 return "DW_OP_not";
13659 case DW_OP_or:
13660 return "DW_OP_or";
13661 case DW_OP_plus:
13662 return "DW_OP_plus";
13663 case DW_OP_plus_uconst:
13664 return "DW_OP_plus_uconst";
13665 case DW_OP_shl:
13666 return "DW_OP_shl";
13667 case DW_OP_shr:
13668 return "DW_OP_shr";
13669 case DW_OP_shra:
13670 return "DW_OP_shra";
13671 case DW_OP_xor:
13672 return "DW_OP_xor";
13673 case DW_OP_bra:
13674 return "DW_OP_bra";
13675 case DW_OP_eq:
13676 return "DW_OP_eq";
13677 case DW_OP_ge:
13678 return "DW_OP_ge";
13679 case DW_OP_gt:
13680 return "DW_OP_gt";
13681 case DW_OP_le:
13682 return "DW_OP_le";
13683 case DW_OP_lt:
13684 return "DW_OP_lt";
13685 case DW_OP_ne:
13686 return "DW_OP_ne";
13687 case DW_OP_skip:
13688 return "DW_OP_skip";
13689 case DW_OP_lit0:
13690 return "DW_OP_lit0";
13691 case DW_OP_lit1:
13692 return "DW_OP_lit1";
13693 case DW_OP_lit2:
13694 return "DW_OP_lit2";
13695 case DW_OP_lit3:
13696 return "DW_OP_lit3";
13697 case DW_OP_lit4:
13698 return "DW_OP_lit4";
13699 case DW_OP_lit5:
13700 return "DW_OP_lit5";
13701 case DW_OP_lit6:
13702 return "DW_OP_lit6";
13703 case DW_OP_lit7:
13704 return "DW_OP_lit7";
13705 case DW_OP_lit8:
13706 return "DW_OP_lit8";
13707 case DW_OP_lit9:
13708 return "DW_OP_lit9";
13709 case DW_OP_lit10:
13710 return "DW_OP_lit10";
13711 case DW_OP_lit11:
13712 return "DW_OP_lit11";
13713 case DW_OP_lit12:
13714 return "DW_OP_lit12";
13715 case DW_OP_lit13:
13716 return "DW_OP_lit13";
13717 case DW_OP_lit14:
13718 return "DW_OP_lit14";
13719 case DW_OP_lit15:
13720 return "DW_OP_lit15";
13721 case DW_OP_lit16:
13722 return "DW_OP_lit16";
13723 case DW_OP_lit17:
13724 return "DW_OP_lit17";
13725 case DW_OP_lit18:
13726 return "DW_OP_lit18";
13727 case DW_OP_lit19:
13728 return "DW_OP_lit19";
13729 case DW_OP_lit20:
13730 return "DW_OP_lit20";
13731 case DW_OP_lit21:
13732 return "DW_OP_lit21";
13733 case DW_OP_lit22:
13734 return "DW_OP_lit22";
13735 case DW_OP_lit23:
13736 return "DW_OP_lit23";
13737 case DW_OP_lit24:
13738 return "DW_OP_lit24";
13739 case DW_OP_lit25:
13740 return "DW_OP_lit25";
13741 case DW_OP_lit26:
13742 return "DW_OP_lit26";
13743 case DW_OP_lit27:
13744 return "DW_OP_lit27";
13745 case DW_OP_lit28:
13746 return "DW_OP_lit28";
13747 case DW_OP_lit29:
13748 return "DW_OP_lit29";
13749 case DW_OP_lit30:
13750 return "DW_OP_lit30";
13751 case DW_OP_lit31:
13752 return "DW_OP_lit31";
13753 case DW_OP_reg0:
13754 return "DW_OP_reg0";
13755 case DW_OP_reg1:
13756 return "DW_OP_reg1";
13757 case DW_OP_reg2:
13758 return "DW_OP_reg2";
13759 case DW_OP_reg3:
13760 return "DW_OP_reg3";
13761 case DW_OP_reg4:
13762 return "DW_OP_reg4";
13763 case DW_OP_reg5:
13764 return "DW_OP_reg5";
13765 case DW_OP_reg6:
13766 return "DW_OP_reg6";
13767 case DW_OP_reg7:
13768 return "DW_OP_reg7";
13769 case DW_OP_reg8:
13770 return "DW_OP_reg8";
13771 case DW_OP_reg9:
13772 return "DW_OP_reg9";
13773 case DW_OP_reg10:
13774 return "DW_OP_reg10";
13775 case DW_OP_reg11:
13776 return "DW_OP_reg11";
13777 case DW_OP_reg12:
13778 return "DW_OP_reg12";
13779 case DW_OP_reg13:
13780 return "DW_OP_reg13";
13781 case DW_OP_reg14:
13782 return "DW_OP_reg14";
13783 case DW_OP_reg15:
13784 return "DW_OP_reg15";
13785 case DW_OP_reg16:
13786 return "DW_OP_reg16";
13787 case DW_OP_reg17:
13788 return "DW_OP_reg17";
13789 case DW_OP_reg18:
13790 return "DW_OP_reg18";
13791 case DW_OP_reg19:
13792 return "DW_OP_reg19";
13793 case DW_OP_reg20:
13794 return "DW_OP_reg20";
13795 case DW_OP_reg21:
13796 return "DW_OP_reg21";
13797 case DW_OP_reg22:
13798 return "DW_OP_reg22";
13799 case DW_OP_reg23:
13800 return "DW_OP_reg23";
13801 case DW_OP_reg24:
13802 return "DW_OP_reg24";
13803 case DW_OP_reg25:
13804 return "DW_OP_reg25";
13805 case DW_OP_reg26:
13806 return "DW_OP_reg26";
13807 case DW_OP_reg27:
13808 return "DW_OP_reg27";
13809 case DW_OP_reg28:
13810 return "DW_OP_reg28";
13811 case DW_OP_reg29:
13812 return "DW_OP_reg29";
13813 case DW_OP_reg30:
13814 return "DW_OP_reg30";
13815 case DW_OP_reg31:
13816 return "DW_OP_reg31";
13817 case DW_OP_breg0:
13818 return "DW_OP_breg0";
13819 case DW_OP_breg1:
13820 return "DW_OP_breg1";
13821 case DW_OP_breg2:
13822 return "DW_OP_breg2";
13823 case DW_OP_breg3:
13824 return "DW_OP_breg3";
13825 case DW_OP_breg4:
13826 return "DW_OP_breg4";
13827 case DW_OP_breg5:
13828 return "DW_OP_breg5";
13829 case DW_OP_breg6:
13830 return "DW_OP_breg6";
13831 case DW_OP_breg7:
13832 return "DW_OP_breg7";
13833 case DW_OP_breg8:
13834 return "DW_OP_breg8";
13835 case DW_OP_breg9:
13836 return "DW_OP_breg9";
13837 case DW_OP_breg10:
13838 return "DW_OP_breg10";
13839 case DW_OP_breg11:
13840 return "DW_OP_breg11";
13841 case DW_OP_breg12:
13842 return "DW_OP_breg12";
13843 case DW_OP_breg13:
13844 return "DW_OP_breg13";
13845 case DW_OP_breg14:
13846 return "DW_OP_breg14";
13847 case DW_OP_breg15:
13848 return "DW_OP_breg15";
13849 case DW_OP_breg16:
13850 return "DW_OP_breg16";
13851 case DW_OP_breg17:
13852 return "DW_OP_breg17";
13853 case DW_OP_breg18:
13854 return "DW_OP_breg18";
13855 case DW_OP_breg19:
13856 return "DW_OP_breg19";
13857 case DW_OP_breg20:
13858 return "DW_OP_breg20";
13859 case DW_OP_breg21:
13860 return "DW_OP_breg21";
13861 case DW_OP_breg22:
13862 return "DW_OP_breg22";
13863 case DW_OP_breg23:
13864 return "DW_OP_breg23";
13865 case DW_OP_breg24:
13866 return "DW_OP_breg24";
13867 case DW_OP_breg25:
13868 return "DW_OP_breg25";
13869 case DW_OP_breg26:
13870 return "DW_OP_breg26";
13871 case DW_OP_breg27:
13872 return "DW_OP_breg27";
13873 case DW_OP_breg28:
13874 return "DW_OP_breg28";
13875 case DW_OP_breg29:
13876 return "DW_OP_breg29";
13877 case DW_OP_breg30:
13878 return "DW_OP_breg30";
13879 case DW_OP_breg31:
13880 return "DW_OP_breg31";
13881 case DW_OP_regx:
13882 return "DW_OP_regx";
13883 case DW_OP_fbreg:
13884 return "DW_OP_fbreg";
13885 case DW_OP_bregx:
13886 return "DW_OP_bregx";
13887 case DW_OP_piece:
13888 return "DW_OP_piece";
13889 case DW_OP_deref_size:
13890 return "DW_OP_deref_size";
13891 case DW_OP_xderef_size:
13892 return "DW_OP_xderef_size";
13893 case DW_OP_nop:
13894 return "DW_OP_nop";
13895 /* DWARF 3 extensions. */
13896 case DW_OP_push_object_address:
13897 return "DW_OP_push_object_address";
13898 case DW_OP_call2:
13899 return "DW_OP_call2";
13900 case DW_OP_call4:
13901 return "DW_OP_call4";
13902 case DW_OP_call_ref:
13903 return "DW_OP_call_ref";
13904 case DW_OP_form_tls_address:
13905 return "DW_OP_form_tls_address";
13906 case DW_OP_call_frame_cfa:
13907 return "DW_OP_call_frame_cfa";
13908 case DW_OP_bit_piece:
13909 return "DW_OP_bit_piece";
13910 /* DWARF 4 extensions. */
13911 case DW_OP_implicit_value:
13912 return "DW_OP_implicit_value";
13913 case DW_OP_stack_value:
13914 return "DW_OP_stack_value";
13915 /* GNU extensions. */
13916 case DW_OP_GNU_push_tls_address:
13917 return "DW_OP_GNU_push_tls_address";
13918 case DW_OP_GNU_uninit:
13919 return "DW_OP_GNU_uninit";
13920 case DW_OP_GNU_encoded_addr:
13921 return "DW_OP_GNU_encoded_addr";
13922 case DW_OP_GNU_implicit_pointer:
13923 return "DW_OP_GNU_implicit_pointer";
13924 case DW_OP_GNU_entry_value:
13925 return "DW_OP_GNU_entry_value";
13926 case DW_OP_GNU_const_type:
13927 return "DW_OP_GNU_const_type";
13928 case DW_OP_GNU_regval_type:
13929 return "DW_OP_GNU_regval_type";
13930 case DW_OP_GNU_deref_type:
13931 return "DW_OP_GNU_deref_type";
13932 case DW_OP_GNU_convert:
13933 return "DW_OP_GNU_convert";
13934 case DW_OP_GNU_reinterpret:
13935 return "DW_OP_GNU_reinterpret";
13936 case DW_OP_GNU_parameter_ref:
13937 return "DW_OP_GNU_parameter_ref";
13938 default:
13939 return NULL;
13940 }
13941 }
13942
13943 static char *
13944 dwarf_bool_name (unsigned mybool)
13945 {
13946 if (mybool)
13947 return "TRUE";
13948 else
13949 return "FALSE";
13950 }
13951
13952 /* Convert a DWARF type code into its string name. */
13953
13954 static char *
13955 dwarf_type_encoding_name (unsigned enc)
13956 {
13957 switch (enc)
13958 {
13959 case DW_ATE_void:
13960 return "DW_ATE_void";
13961 case DW_ATE_address:
13962 return "DW_ATE_address";
13963 case DW_ATE_boolean:
13964 return "DW_ATE_boolean";
13965 case DW_ATE_complex_float:
13966 return "DW_ATE_complex_float";
13967 case DW_ATE_float:
13968 return "DW_ATE_float";
13969 case DW_ATE_signed:
13970 return "DW_ATE_signed";
13971 case DW_ATE_signed_char:
13972 return "DW_ATE_signed_char";
13973 case DW_ATE_unsigned:
13974 return "DW_ATE_unsigned";
13975 case DW_ATE_unsigned_char:
13976 return "DW_ATE_unsigned_char";
13977 /* DWARF 3. */
13978 case DW_ATE_imaginary_float:
13979 return "DW_ATE_imaginary_float";
13980 case DW_ATE_packed_decimal:
13981 return "DW_ATE_packed_decimal";
13982 case DW_ATE_numeric_string:
13983 return "DW_ATE_numeric_string";
13984 case DW_ATE_edited:
13985 return "DW_ATE_edited";
13986 case DW_ATE_signed_fixed:
13987 return "DW_ATE_signed_fixed";
13988 case DW_ATE_unsigned_fixed:
13989 return "DW_ATE_unsigned_fixed";
13990 case DW_ATE_decimal_float:
13991 return "DW_ATE_decimal_float";
13992 /* DWARF 4. */
13993 case DW_ATE_UTF:
13994 return "DW_ATE_UTF";
13995 /* HP extensions. */
13996 case DW_ATE_HP_float80:
13997 return "DW_ATE_HP_float80";
13998 case DW_ATE_HP_complex_float80:
13999 return "DW_ATE_HP_complex_float80";
14000 case DW_ATE_HP_float128:
14001 return "DW_ATE_HP_float128";
14002 case DW_ATE_HP_complex_float128:
14003 return "DW_ATE_HP_complex_float128";
14004 case DW_ATE_HP_floathpintel:
14005 return "DW_ATE_HP_floathpintel";
14006 case DW_ATE_HP_imaginary_float80:
14007 return "DW_ATE_HP_imaginary_float80";
14008 case DW_ATE_HP_imaginary_float128:
14009 return "DW_ATE_HP_imaginary_float128";
14010 default:
14011 return "DW_ATE_<unknown>";
14012 }
14013 }
14014
14015 /* Convert a DWARF call frame info operation to its string name. */
14016
14017 #if 0
14018 static char *
14019 dwarf_cfi_name (unsigned cfi_opc)
14020 {
14021 switch (cfi_opc)
14022 {
14023 case DW_CFA_advance_loc:
14024 return "DW_CFA_advance_loc";
14025 case DW_CFA_offset:
14026 return "DW_CFA_offset";
14027 case DW_CFA_restore:
14028 return "DW_CFA_restore";
14029 case DW_CFA_nop:
14030 return "DW_CFA_nop";
14031 case DW_CFA_set_loc:
14032 return "DW_CFA_set_loc";
14033 case DW_CFA_advance_loc1:
14034 return "DW_CFA_advance_loc1";
14035 case DW_CFA_advance_loc2:
14036 return "DW_CFA_advance_loc2";
14037 case DW_CFA_advance_loc4:
14038 return "DW_CFA_advance_loc4";
14039 case DW_CFA_offset_extended:
14040 return "DW_CFA_offset_extended";
14041 case DW_CFA_restore_extended:
14042 return "DW_CFA_restore_extended";
14043 case DW_CFA_undefined:
14044 return "DW_CFA_undefined";
14045 case DW_CFA_same_value:
14046 return "DW_CFA_same_value";
14047 case DW_CFA_register:
14048 return "DW_CFA_register";
14049 case DW_CFA_remember_state:
14050 return "DW_CFA_remember_state";
14051 case DW_CFA_restore_state:
14052 return "DW_CFA_restore_state";
14053 case DW_CFA_def_cfa:
14054 return "DW_CFA_def_cfa";
14055 case DW_CFA_def_cfa_register:
14056 return "DW_CFA_def_cfa_register";
14057 case DW_CFA_def_cfa_offset:
14058 return "DW_CFA_def_cfa_offset";
14059 /* DWARF 3. */
14060 case DW_CFA_def_cfa_expression:
14061 return "DW_CFA_def_cfa_expression";
14062 case DW_CFA_expression:
14063 return "DW_CFA_expression";
14064 case DW_CFA_offset_extended_sf:
14065 return "DW_CFA_offset_extended_sf";
14066 case DW_CFA_def_cfa_sf:
14067 return "DW_CFA_def_cfa_sf";
14068 case DW_CFA_def_cfa_offset_sf:
14069 return "DW_CFA_def_cfa_offset_sf";
14070 case DW_CFA_val_offset:
14071 return "DW_CFA_val_offset";
14072 case DW_CFA_val_offset_sf:
14073 return "DW_CFA_val_offset_sf";
14074 case DW_CFA_val_expression:
14075 return "DW_CFA_val_expression";
14076 /* SGI/MIPS specific. */
14077 case DW_CFA_MIPS_advance_loc8:
14078 return "DW_CFA_MIPS_advance_loc8";
14079 /* GNU extensions. */
14080 case DW_CFA_GNU_window_save:
14081 return "DW_CFA_GNU_window_save";
14082 case DW_CFA_GNU_args_size:
14083 return "DW_CFA_GNU_args_size";
14084 case DW_CFA_GNU_negative_offset_extended:
14085 return "DW_CFA_GNU_negative_offset_extended";
14086 default:
14087 return "DW_CFA_<unknown>";
14088 }
14089 }
14090 #endif
14091
14092 static void
14093 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
14094 {
14095 unsigned int i;
14096
14097 print_spaces (indent, f);
14098 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
14099 dwarf_tag_name (die->tag), die->abbrev, die->offset.sect_off);
14100
14101 if (die->parent != NULL)
14102 {
14103 print_spaces (indent, f);
14104 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
14105 die->parent->offset.sect_off);
14106 }
14107
14108 print_spaces (indent, f);
14109 fprintf_unfiltered (f, " has children: %s\n",
14110 dwarf_bool_name (die->child != NULL));
14111
14112 print_spaces (indent, f);
14113 fprintf_unfiltered (f, " attributes:\n");
14114
14115 for (i = 0; i < die->num_attrs; ++i)
14116 {
14117 print_spaces (indent, f);
14118 fprintf_unfiltered (f, " %s (%s) ",
14119 dwarf_attr_name (die->attrs[i].name),
14120 dwarf_form_name (die->attrs[i].form));
14121
14122 switch (die->attrs[i].form)
14123 {
14124 case DW_FORM_addr:
14125 fprintf_unfiltered (f, "address: ");
14126 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
14127 break;
14128 case DW_FORM_block2:
14129 case DW_FORM_block4:
14130 case DW_FORM_block:
14131 case DW_FORM_block1:
14132 fprintf_unfiltered (f, "block: size %d",
14133 DW_BLOCK (&die->attrs[i])->size);
14134 break;
14135 case DW_FORM_exprloc:
14136 fprintf_unfiltered (f, "expression: size %u",
14137 DW_BLOCK (&die->attrs[i])->size);
14138 break;
14139 case DW_FORM_ref_addr:
14140 fprintf_unfiltered (f, "ref address: ");
14141 fputs_filtered (hex_string (DW_UNSND (&die->attrs[i])), f);
14142 break;
14143 case DW_FORM_ref1:
14144 case DW_FORM_ref2:
14145 case DW_FORM_ref4:
14146 case DW_FORM_ref8:
14147 case DW_FORM_ref_udata:
14148 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
14149 (long) (DW_UNSND (&die->attrs[i])));
14150 break;
14151 case DW_FORM_data1:
14152 case DW_FORM_data2:
14153 case DW_FORM_data4:
14154 case DW_FORM_data8:
14155 case DW_FORM_udata:
14156 case DW_FORM_sdata:
14157 fprintf_unfiltered (f, "constant: %s",
14158 pulongest (DW_UNSND (&die->attrs[i])));
14159 break;
14160 case DW_FORM_sec_offset:
14161 fprintf_unfiltered (f, "section offset: %s",
14162 pulongest (DW_UNSND (&die->attrs[i])));
14163 break;
14164 case DW_FORM_ref_sig8:
14165 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
14166 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
14167 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset.sect_off);
14168 else
14169 fprintf_unfiltered (f, "signatured type, offset: unknown");
14170 break;
14171 case DW_FORM_string:
14172 case DW_FORM_strp:
14173 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
14174 DW_STRING (&die->attrs[i])
14175 ? DW_STRING (&die->attrs[i]) : "",
14176 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
14177 break;
14178 case DW_FORM_flag:
14179 if (DW_UNSND (&die->attrs[i]))
14180 fprintf_unfiltered (f, "flag: TRUE");
14181 else
14182 fprintf_unfiltered (f, "flag: FALSE");
14183 break;
14184 case DW_FORM_flag_present:
14185 fprintf_unfiltered (f, "flag: TRUE");
14186 break;
14187 case DW_FORM_indirect:
14188 /* The reader will have reduced the indirect form to
14189 the "base form" so this form should not occur. */
14190 fprintf_unfiltered (f,
14191 "unexpected attribute form: DW_FORM_indirect");
14192 break;
14193 default:
14194 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14195 die->attrs[i].form);
14196 break;
14197 }
14198 fprintf_unfiltered (f, "\n");
14199 }
14200 }
14201
14202 static void
14203 dump_die_for_error (struct die_info *die)
14204 {
14205 dump_die_shallow (gdb_stderr, 0, die);
14206 }
14207
14208 static void
14209 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14210 {
14211 int indent = level * 4;
14212
14213 gdb_assert (die != NULL);
14214
14215 if (level >= max_level)
14216 return;
14217
14218 dump_die_shallow (f, indent, die);
14219
14220 if (die->child != NULL)
14221 {
14222 print_spaces (indent, f);
14223 fprintf_unfiltered (f, " Children:");
14224 if (level + 1 < max_level)
14225 {
14226 fprintf_unfiltered (f, "\n");
14227 dump_die_1 (f, level + 1, max_level, die->child);
14228 }
14229 else
14230 {
14231 fprintf_unfiltered (f,
14232 " [not printed, max nesting level reached]\n");
14233 }
14234 }
14235
14236 if (die->sibling != NULL && level > 0)
14237 {
14238 dump_die_1 (f, level, max_level, die->sibling);
14239 }
14240 }
14241
14242 /* This is called from the pdie macro in gdbinit.in.
14243 It's not static so gcc will keep a copy callable from gdb. */
14244
14245 void
14246 dump_die (struct die_info *die, int max_level)
14247 {
14248 dump_die_1 (gdb_stdlog, 0, max_level, die);
14249 }
14250
14251 static void
14252 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14253 {
14254 void **slot;
14255
14256 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset.sect_off,
14257 INSERT);
14258
14259 *slot = die;
14260 }
14261
14262 /* DW_ADDR is always stored already as sect_offset; despite for the forms
14263 besides DW_FORM_ref_addr it is stored as cu_offset in the DWARF file. */
14264
14265 static int
14266 is_ref_attr (struct attribute *attr)
14267 {
14268 switch (attr->form)
14269 {
14270 case DW_FORM_ref_addr:
14271 case DW_FORM_ref1:
14272 case DW_FORM_ref2:
14273 case DW_FORM_ref4:
14274 case DW_FORM_ref8:
14275 case DW_FORM_ref_udata:
14276 return 1;
14277 default:
14278 return 0;
14279 }
14280 }
14281
14282 /* Return DIE offset of ATTR. Return 0 with complaint if ATTR is not of the
14283 required kind. */
14284
14285 static sect_offset
14286 dwarf2_get_ref_die_offset (struct attribute *attr)
14287 {
14288 sect_offset retval = { DW_UNSND (attr) };
14289
14290 if (is_ref_attr (attr))
14291 return retval;
14292
14293 retval.sect_off = 0;
14294 complaint (&symfile_complaints,
14295 _("unsupported die ref attribute form: '%s'"),
14296 dwarf_form_name (attr->form));
14297 return retval;
14298 }
14299
14300 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14301 * the value held by the attribute is not constant. */
14302
14303 static LONGEST
14304 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14305 {
14306 if (attr->form == DW_FORM_sdata)
14307 return DW_SND (attr);
14308 else if (attr->form == DW_FORM_udata
14309 || attr->form == DW_FORM_data1
14310 || attr->form == DW_FORM_data2
14311 || attr->form == DW_FORM_data4
14312 || attr->form == DW_FORM_data8)
14313 return DW_UNSND (attr);
14314 else
14315 {
14316 complaint (&symfile_complaints,
14317 _("Attribute value is not a constant (%s)"),
14318 dwarf_form_name (attr->form));
14319 return default_value;
14320 }
14321 }
14322
14323 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14324 unit and add it to our queue.
14325 The result is non-zero if PER_CU was queued, otherwise the result is zero
14326 meaning either PER_CU is already queued or it is already loaded. */
14327
14328 static int
14329 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14330 struct dwarf2_per_cu_data *per_cu)
14331 {
14332 /* We may arrive here during partial symbol reading, if we need full
14333 DIEs to process an unusual case (e.g. template arguments). Do
14334 not queue PER_CU, just tell our caller to load its DIEs. */
14335 if (dwarf2_per_objfile->reading_partial_symbols)
14336 {
14337 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14338 return 1;
14339 return 0;
14340 }
14341
14342 /* Mark the dependence relation so that we don't flush PER_CU
14343 too early. */
14344 dwarf2_add_dependence (this_cu, per_cu);
14345
14346 /* If it's already on the queue, we have nothing to do. */
14347 if (per_cu->queued)
14348 return 0;
14349
14350 /* If the compilation unit is already loaded, just mark it as
14351 used. */
14352 if (per_cu->cu != NULL)
14353 {
14354 per_cu->cu->last_used = 0;
14355 return 0;
14356 }
14357
14358 /* Add it to the queue. */
14359 queue_comp_unit (per_cu);
14360
14361 return 1;
14362 }
14363
14364 /* Follow reference or signature attribute ATTR of SRC_DIE.
14365 On entry *REF_CU is the CU of SRC_DIE.
14366 On exit *REF_CU is the CU of the result. */
14367
14368 static struct die_info *
14369 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14370 struct dwarf2_cu **ref_cu)
14371 {
14372 struct die_info *die;
14373
14374 if (is_ref_attr (attr))
14375 die = follow_die_ref (src_die, attr, ref_cu);
14376 else if (attr->form == DW_FORM_ref_sig8)
14377 die = follow_die_sig (src_die, attr, ref_cu);
14378 else
14379 {
14380 dump_die_for_error (src_die);
14381 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14382 (*ref_cu)->objfile->name);
14383 }
14384
14385 return die;
14386 }
14387
14388 /* Follow reference OFFSET.
14389 On entry *REF_CU is the CU of the source die referencing OFFSET.
14390 On exit *REF_CU is the CU of the result.
14391 Returns NULL if OFFSET is invalid. */
14392
14393 static struct die_info *
14394 follow_die_offset (sect_offset offset, struct dwarf2_cu **ref_cu)
14395 {
14396 struct die_info temp_die;
14397 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14398
14399 gdb_assert (cu->per_cu != NULL);
14400
14401 target_cu = cu;
14402
14403 if (cu->per_cu->debug_types_section)
14404 {
14405 /* .debug_types CUs cannot reference anything outside their CU.
14406 If they need to, they have to reference a signatured type via
14407 DW_FORM_ref_sig8. */
14408 if (! offset_in_cu_p (&cu->header, offset))
14409 return NULL;
14410 }
14411 else if (! offset_in_cu_p (&cu->header, offset))
14412 {
14413 struct dwarf2_per_cu_data *per_cu;
14414
14415 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14416
14417 /* If necessary, add it to the queue and load its DIEs. */
14418 if (maybe_queue_comp_unit (cu, per_cu))
14419 load_full_comp_unit (per_cu);
14420
14421 target_cu = per_cu->cu;
14422 }
14423 else if (cu->dies == NULL)
14424 {
14425 /* We're loading full DIEs during partial symbol reading. */
14426 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14427 load_full_comp_unit (cu->per_cu);
14428 }
14429
14430 *ref_cu = target_cu;
14431 temp_die.offset = offset;
14432 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset.sect_off);
14433 }
14434
14435 /* Follow reference attribute ATTR of SRC_DIE.
14436 On entry *REF_CU is the CU of SRC_DIE.
14437 On exit *REF_CU is the CU of the result. */
14438
14439 static struct die_info *
14440 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14441 struct dwarf2_cu **ref_cu)
14442 {
14443 sect_offset offset = dwarf2_get_ref_die_offset (attr);
14444 struct dwarf2_cu *cu = *ref_cu;
14445 struct die_info *die;
14446
14447 die = follow_die_offset (offset, ref_cu);
14448 if (!die)
14449 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14450 "at 0x%x [in module %s]"),
14451 offset.sect_off, src_die->offset.sect_off, cu->objfile->name);
14452
14453 return die;
14454 }
14455
14456 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14457 Returned value is intended for DW_OP_call*. Returned
14458 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14459
14460 struct dwarf2_locexpr_baton
14461 dwarf2_fetch_die_location_block (cu_offset offset_in_cu,
14462 struct dwarf2_per_cu_data *per_cu,
14463 CORE_ADDR (*get_frame_pc) (void *baton),
14464 void *baton)
14465 {
14466 sect_offset offset = { per_cu->offset.sect_off + offset_in_cu.cu_off };
14467 struct dwarf2_cu *cu;
14468 struct die_info *die;
14469 struct attribute *attr;
14470 struct dwarf2_locexpr_baton retval;
14471
14472 dw2_setup (per_cu->objfile);
14473
14474 if (per_cu->cu == NULL)
14475 load_cu (per_cu);
14476 cu = per_cu->cu;
14477
14478 die = follow_die_offset (offset, &cu);
14479 if (!die)
14480 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14481 offset.sect_off, per_cu->objfile->name);
14482
14483 attr = dwarf2_attr (die, DW_AT_location, cu);
14484 if (!attr)
14485 {
14486 /* DWARF: "If there is no such attribute, then there is no effect.".
14487 DATA is ignored if SIZE is 0. */
14488
14489 retval.data = NULL;
14490 retval.size = 0;
14491 }
14492 else if (attr_form_is_section_offset (attr))
14493 {
14494 struct dwarf2_loclist_baton loclist_baton;
14495 CORE_ADDR pc = (*get_frame_pc) (baton);
14496 size_t size;
14497
14498 fill_in_loclist_baton (cu, &loclist_baton, attr);
14499
14500 retval.data = dwarf2_find_location_expression (&loclist_baton,
14501 &size, pc);
14502 retval.size = size;
14503 }
14504 else
14505 {
14506 if (!attr_form_is_block (attr))
14507 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14508 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14509 offset.sect_off, per_cu->objfile->name);
14510
14511 retval.data = DW_BLOCK (attr)->data;
14512 retval.size = DW_BLOCK (attr)->size;
14513 }
14514 retval.per_cu = cu->per_cu;
14515
14516 age_cached_comp_units ();
14517
14518 return retval;
14519 }
14520
14521 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14522 PER_CU. */
14523
14524 struct type *
14525 dwarf2_get_die_type (cu_offset die_offset,
14526 struct dwarf2_per_cu_data *per_cu)
14527 {
14528 sect_offset die_offset_sect;
14529
14530 dw2_setup (per_cu->objfile);
14531
14532 die_offset_sect.sect_off = per_cu->offset.sect_off + die_offset.cu_off;
14533 return get_die_type_at_offset (die_offset_sect, per_cu);
14534 }
14535
14536 /* Follow the signature attribute ATTR in SRC_DIE.
14537 On entry *REF_CU is the CU of SRC_DIE.
14538 On exit *REF_CU is the CU of the result. */
14539
14540 static struct die_info *
14541 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14542 struct dwarf2_cu **ref_cu)
14543 {
14544 struct objfile *objfile = (*ref_cu)->objfile;
14545 struct die_info temp_die;
14546 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14547 struct dwarf2_cu *sig_cu;
14548 struct die_info *die;
14549
14550 /* sig_type will be NULL if the signatured type is missing from
14551 the debug info. */
14552 if (sig_type == NULL)
14553 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14554 "at 0x%x [in module %s]"),
14555 src_die->offset.sect_off, objfile->name);
14556
14557 /* If necessary, add it to the queue and load its DIEs. */
14558
14559 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14560 read_signatured_type (sig_type);
14561
14562 gdb_assert (sig_type->per_cu.cu != NULL);
14563
14564 sig_cu = sig_type->per_cu.cu;
14565 temp_die.offset.sect_off = (sig_type->per_cu.offset.sect_off
14566 + sig_type->type_offset.cu_off);
14567 die = htab_find_with_hash (sig_cu->die_hash, &temp_die,
14568 temp_die.offset.sect_off);
14569 if (die)
14570 {
14571 *ref_cu = sig_cu;
14572 return die;
14573 }
14574
14575 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14576 "from DIE at 0x%x [in module %s]"),
14577 temp_die.offset.sect_off, src_die->offset.sect_off, objfile->name);
14578 }
14579
14580 /* Given an offset of a signatured type, return its signatured_type. */
14581
14582 static struct signatured_type *
14583 lookup_signatured_type_at_offset (struct objfile *objfile,
14584 struct dwarf2_section_info *section,
14585 sect_offset offset)
14586 {
14587 gdb_byte *info_ptr = section->buffer + offset.sect_off;
14588 unsigned int length, initial_length_size;
14589 unsigned int sig_offset;
14590 struct signatured_type find_entry, *sig_type;
14591
14592 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14593 sig_offset = (initial_length_size
14594 + 2 /*version*/
14595 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14596 + 1 /*address_size*/);
14597 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14598 sig_type = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14599
14600 /* This is only used to lookup previously recorded types.
14601 If we didn't find it, it's our bug. */
14602 gdb_assert (sig_type != NULL);
14603 gdb_assert (offset.sect_off == sig_type->per_cu.offset.sect_off);
14604
14605 return sig_type;
14606 }
14607
14608 /* Load the DIEs associated with type unit PER_CU into memory. */
14609
14610 static void
14611 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14612 {
14613 struct objfile *objfile = per_cu->objfile;
14614 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14615 sect_offset offset = per_cu->offset;
14616 struct signatured_type *sig_type;
14617
14618 dwarf2_read_section (objfile, sect);
14619
14620 /* We have the section offset, but we need the signature to do the
14621 hash table lookup. */
14622 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14623 the signature to assert we found the right one.
14624 Ok, but it's a lot of work. We should simplify things so any needed
14625 assert doesn't require all this clumsiness. */
14626 sig_type = lookup_signatured_type_at_offset (objfile, sect, offset);
14627
14628 gdb_assert (sig_type->per_cu.cu == NULL);
14629
14630 read_signatured_type (sig_type);
14631
14632 gdb_assert (sig_type->per_cu.cu != NULL);
14633 }
14634
14635 /* Read in a signatured type and build its CU and DIEs. */
14636
14637 static void
14638 read_signatured_type (struct signatured_type *sig_type)
14639 {
14640 struct objfile *objfile = sig_type->per_cu.objfile;
14641 gdb_byte *types_ptr;
14642 struct die_reader_specs reader_specs;
14643 struct dwarf2_cu *cu;
14644 ULONGEST signature;
14645 struct cleanup *back_to, *free_cu_cleanup;
14646 struct dwarf2_section_info *section = sig_type->per_cu.debug_types_section;
14647
14648 dwarf2_read_section (objfile, section);
14649 types_ptr = section->buffer + sig_type->per_cu.offset.sect_off;
14650
14651 gdb_assert (sig_type->per_cu.cu == NULL);
14652
14653 cu = xmalloc (sizeof (*cu));
14654 init_one_comp_unit (cu, &sig_type->per_cu);
14655
14656 /* If an error occurs while loading, release our storage. */
14657 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14658
14659 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14660 &signature, NULL);
14661 gdb_assert (signature == sig_type->signature);
14662
14663 cu->die_hash
14664 = htab_create_alloc_ex (cu->header.length / 12,
14665 die_hash,
14666 die_eq,
14667 NULL,
14668 &cu->comp_unit_obstack,
14669 hashtab_obstack_allocate,
14670 dummy_obstack_deallocate);
14671
14672 dwarf2_read_abbrevs (cu);
14673 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14674
14675 init_cu_die_reader (&reader_specs, cu);
14676
14677 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14678 NULL /*parent*/);
14679
14680 /* We try not to read any attributes in this function, because not
14681 all CUs needed for references have been loaded yet, and symbol
14682 table processing isn't initialized. But we have to set the CU language,
14683 or we won't be able to build types correctly. */
14684 prepare_one_comp_unit (cu, cu->dies);
14685
14686 do_cleanups (back_to);
14687
14688 /* We've successfully allocated this compilation unit. Let our caller
14689 clean it up when finished with it. */
14690 discard_cleanups (free_cu_cleanup);
14691
14692 /* Link this TU into read_in_chain. */
14693 sig_type->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14694 dwarf2_per_objfile->read_in_chain = &sig_type->per_cu;
14695 }
14696
14697 /* Decode simple location descriptions.
14698 Given a pointer to a dwarf block that defines a location, compute
14699 the location and return the value.
14700
14701 NOTE drow/2003-11-18: This function is called in two situations
14702 now: for the address of static or global variables (partial symbols
14703 only) and for offsets into structures which are expected to be
14704 (more or less) constant. The partial symbol case should go away,
14705 and only the constant case should remain. That will let this
14706 function complain more accurately. A few special modes are allowed
14707 without complaint for global variables (for instance, global
14708 register values and thread-local values).
14709
14710 A location description containing no operations indicates that the
14711 object is optimized out. The return value is 0 for that case.
14712 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14713 callers will only want a very basic result and this can become a
14714 complaint.
14715
14716 Note that stack[0] is unused except as a default error return. */
14717
14718 static CORE_ADDR
14719 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14720 {
14721 struct objfile *objfile = cu->objfile;
14722 int i;
14723 int size = blk->size;
14724 gdb_byte *data = blk->data;
14725 CORE_ADDR stack[64];
14726 int stacki;
14727 unsigned int bytes_read, unsnd;
14728 gdb_byte op;
14729
14730 i = 0;
14731 stacki = 0;
14732 stack[stacki] = 0;
14733 stack[++stacki] = 0;
14734
14735 while (i < size)
14736 {
14737 op = data[i++];
14738 switch (op)
14739 {
14740 case DW_OP_lit0:
14741 case DW_OP_lit1:
14742 case DW_OP_lit2:
14743 case DW_OP_lit3:
14744 case DW_OP_lit4:
14745 case DW_OP_lit5:
14746 case DW_OP_lit6:
14747 case DW_OP_lit7:
14748 case DW_OP_lit8:
14749 case DW_OP_lit9:
14750 case DW_OP_lit10:
14751 case DW_OP_lit11:
14752 case DW_OP_lit12:
14753 case DW_OP_lit13:
14754 case DW_OP_lit14:
14755 case DW_OP_lit15:
14756 case DW_OP_lit16:
14757 case DW_OP_lit17:
14758 case DW_OP_lit18:
14759 case DW_OP_lit19:
14760 case DW_OP_lit20:
14761 case DW_OP_lit21:
14762 case DW_OP_lit22:
14763 case DW_OP_lit23:
14764 case DW_OP_lit24:
14765 case DW_OP_lit25:
14766 case DW_OP_lit26:
14767 case DW_OP_lit27:
14768 case DW_OP_lit28:
14769 case DW_OP_lit29:
14770 case DW_OP_lit30:
14771 case DW_OP_lit31:
14772 stack[++stacki] = op - DW_OP_lit0;
14773 break;
14774
14775 case DW_OP_reg0:
14776 case DW_OP_reg1:
14777 case DW_OP_reg2:
14778 case DW_OP_reg3:
14779 case DW_OP_reg4:
14780 case DW_OP_reg5:
14781 case DW_OP_reg6:
14782 case DW_OP_reg7:
14783 case DW_OP_reg8:
14784 case DW_OP_reg9:
14785 case DW_OP_reg10:
14786 case DW_OP_reg11:
14787 case DW_OP_reg12:
14788 case DW_OP_reg13:
14789 case DW_OP_reg14:
14790 case DW_OP_reg15:
14791 case DW_OP_reg16:
14792 case DW_OP_reg17:
14793 case DW_OP_reg18:
14794 case DW_OP_reg19:
14795 case DW_OP_reg20:
14796 case DW_OP_reg21:
14797 case DW_OP_reg22:
14798 case DW_OP_reg23:
14799 case DW_OP_reg24:
14800 case DW_OP_reg25:
14801 case DW_OP_reg26:
14802 case DW_OP_reg27:
14803 case DW_OP_reg28:
14804 case DW_OP_reg29:
14805 case DW_OP_reg30:
14806 case DW_OP_reg31:
14807 stack[++stacki] = op - DW_OP_reg0;
14808 if (i < size)
14809 dwarf2_complex_location_expr_complaint ();
14810 break;
14811
14812 case DW_OP_regx:
14813 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14814 i += bytes_read;
14815 stack[++stacki] = unsnd;
14816 if (i < size)
14817 dwarf2_complex_location_expr_complaint ();
14818 break;
14819
14820 case DW_OP_addr:
14821 stack[++stacki] = read_address (objfile->obfd, &data[i],
14822 cu, &bytes_read);
14823 i += bytes_read;
14824 break;
14825
14826 case DW_OP_const1u:
14827 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14828 i += 1;
14829 break;
14830
14831 case DW_OP_const1s:
14832 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14833 i += 1;
14834 break;
14835
14836 case DW_OP_const2u:
14837 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14838 i += 2;
14839 break;
14840
14841 case DW_OP_const2s:
14842 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14843 i += 2;
14844 break;
14845
14846 case DW_OP_const4u:
14847 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14848 i += 4;
14849 break;
14850
14851 case DW_OP_const4s:
14852 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14853 i += 4;
14854 break;
14855
14856 case DW_OP_const8u:
14857 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14858 i += 8;
14859 break;
14860
14861 case DW_OP_constu:
14862 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14863 &bytes_read);
14864 i += bytes_read;
14865 break;
14866
14867 case DW_OP_consts:
14868 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14869 i += bytes_read;
14870 break;
14871
14872 case DW_OP_dup:
14873 stack[stacki + 1] = stack[stacki];
14874 stacki++;
14875 break;
14876
14877 case DW_OP_plus:
14878 stack[stacki - 1] += stack[stacki];
14879 stacki--;
14880 break;
14881
14882 case DW_OP_plus_uconst:
14883 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14884 &bytes_read);
14885 i += bytes_read;
14886 break;
14887
14888 case DW_OP_minus:
14889 stack[stacki - 1] -= stack[stacki];
14890 stacki--;
14891 break;
14892
14893 case DW_OP_deref:
14894 /* If we're not the last op, then we definitely can't encode
14895 this using GDB's address_class enum. This is valid for partial
14896 global symbols, although the variable's address will be bogus
14897 in the psymtab. */
14898 if (i < size)
14899 dwarf2_complex_location_expr_complaint ();
14900 break;
14901
14902 case DW_OP_GNU_push_tls_address:
14903 /* The top of the stack has the offset from the beginning
14904 of the thread control block at which the variable is located. */
14905 /* Nothing should follow this operator, so the top of stack would
14906 be returned. */
14907 /* This is valid for partial global symbols, but the variable's
14908 address will be bogus in the psymtab. Make it always at least
14909 non-zero to not look as a variable garbage collected by linker
14910 which have DW_OP_addr 0. */
14911 if (i < size)
14912 dwarf2_complex_location_expr_complaint ();
14913 stack[stacki]++;
14914 break;
14915
14916 case DW_OP_GNU_uninit:
14917 break;
14918
14919 default:
14920 {
14921 const char *name = dwarf_stack_op_name (op);
14922
14923 if (name)
14924 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14925 name);
14926 else
14927 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14928 op);
14929 }
14930
14931 return (stack[stacki]);
14932 }
14933
14934 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14935 outside of the allocated space. Also enforce minimum>0. */
14936 if (stacki >= ARRAY_SIZE (stack) - 1)
14937 {
14938 complaint (&symfile_complaints,
14939 _("location description stack overflow"));
14940 return 0;
14941 }
14942
14943 if (stacki <= 0)
14944 {
14945 complaint (&symfile_complaints,
14946 _("location description stack underflow"));
14947 return 0;
14948 }
14949 }
14950 return (stack[stacki]);
14951 }
14952
14953 /* memory allocation interface */
14954
14955 static struct dwarf_block *
14956 dwarf_alloc_block (struct dwarf2_cu *cu)
14957 {
14958 struct dwarf_block *blk;
14959
14960 blk = (struct dwarf_block *)
14961 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14962 return (blk);
14963 }
14964
14965 static struct abbrev_info *
14966 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14967 {
14968 struct abbrev_info *abbrev;
14969
14970 abbrev = (struct abbrev_info *)
14971 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14972 memset (abbrev, 0, sizeof (struct abbrev_info));
14973 return (abbrev);
14974 }
14975
14976 static struct die_info *
14977 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14978 {
14979 struct die_info *die;
14980 size_t size = sizeof (struct die_info);
14981
14982 if (num_attrs > 1)
14983 size += (num_attrs - 1) * sizeof (struct attribute);
14984
14985 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14986 memset (die, 0, sizeof (struct die_info));
14987 return (die);
14988 }
14989
14990 \f
14991 /* Macro support. */
14992
14993 /* Return the full name of file number I in *LH's file name table.
14994 Use COMP_DIR as the name of the current directory of the
14995 compilation. The result is allocated using xmalloc; the caller is
14996 responsible for freeing it. */
14997 static char *
14998 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14999 {
15000 /* Is the file number a valid index into the line header's file name
15001 table? Remember that file numbers start with one, not zero. */
15002 if (1 <= file && file <= lh->num_file_names)
15003 {
15004 struct file_entry *fe = &lh->file_names[file - 1];
15005
15006 if (IS_ABSOLUTE_PATH (fe->name))
15007 return xstrdup (fe->name);
15008 else
15009 {
15010 const char *dir;
15011 int dir_len;
15012 char *full_name;
15013
15014 if (fe->dir_index)
15015 dir = lh->include_dirs[fe->dir_index - 1];
15016 else
15017 dir = comp_dir;
15018
15019 if (dir)
15020 {
15021 dir_len = strlen (dir);
15022 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
15023 strcpy (full_name, dir);
15024 full_name[dir_len] = '/';
15025 strcpy (full_name + dir_len + 1, fe->name);
15026 return full_name;
15027 }
15028 else
15029 return xstrdup (fe->name);
15030 }
15031 }
15032 else
15033 {
15034 /* The compiler produced a bogus file number. We can at least
15035 record the macro definitions made in the file, even if we
15036 won't be able to find the file by name. */
15037 char fake_name[80];
15038
15039 sprintf (fake_name, "<bad macro file number %d>", file);
15040
15041 complaint (&symfile_complaints,
15042 _("bad file number in macro information (%d)"),
15043 file);
15044
15045 return xstrdup (fake_name);
15046 }
15047 }
15048
15049
15050 static struct macro_source_file *
15051 macro_start_file (int file, int line,
15052 struct macro_source_file *current_file,
15053 const char *comp_dir,
15054 struct line_header *lh, struct objfile *objfile)
15055 {
15056 /* The full name of this source file. */
15057 char *full_name = file_full_name (file, lh, comp_dir);
15058
15059 /* We don't create a macro table for this compilation unit
15060 at all until we actually get a filename. */
15061 if (! pending_macros)
15062 pending_macros = new_macro_table (&objfile->objfile_obstack,
15063 objfile->macro_cache);
15064
15065 if (! current_file)
15066 /* If we have no current file, then this must be the start_file
15067 directive for the compilation unit's main source file. */
15068 current_file = macro_set_main (pending_macros, full_name);
15069 else
15070 current_file = macro_include (current_file, line, full_name);
15071
15072 xfree (full_name);
15073
15074 return current_file;
15075 }
15076
15077
15078 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
15079 followed by a null byte. */
15080 static char *
15081 copy_string (const char *buf, int len)
15082 {
15083 char *s = xmalloc (len + 1);
15084
15085 memcpy (s, buf, len);
15086 s[len] = '\0';
15087 return s;
15088 }
15089
15090
15091 static const char *
15092 consume_improper_spaces (const char *p, const char *body)
15093 {
15094 if (*p == ' ')
15095 {
15096 complaint (&symfile_complaints,
15097 _("macro definition contains spaces "
15098 "in formal argument list:\n`%s'"),
15099 body);
15100
15101 while (*p == ' ')
15102 p++;
15103 }
15104
15105 return p;
15106 }
15107
15108
15109 static void
15110 parse_macro_definition (struct macro_source_file *file, int line,
15111 const char *body)
15112 {
15113 const char *p;
15114
15115 /* The body string takes one of two forms. For object-like macro
15116 definitions, it should be:
15117
15118 <macro name> " " <definition>
15119
15120 For function-like macro definitions, it should be:
15121
15122 <macro name> "() " <definition>
15123 or
15124 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
15125
15126 Spaces may appear only where explicitly indicated, and in the
15127 <definition>.
15128
15129 The Dwarf 2 spec says that an object-like macro's name is always
15130 followed by a space, but versions of GCC around March 2002 omit
15131 the space when the macro's definition is the empty string.
15132
15133 The Dwarf 2 spec says that there should be no spaces between the
15134 formal arguments in a function-like macro's formal argument list,
15135 but versions of GCC around March 2002 include spaces after the
15136 commas. */
15137
15138
15139 /* Find the extent of the macro name. The macro name is terminated
15140 by either a space or null character (for an object-like macro) or
15141 an opening paren (for a function-like macro). */
15142 for (p = body; *p; p++)
15143 if (*p == ' ' || *p == '(')
15144 break;
15145
15146 if (*p == ' ' || *p == '\0')
15147 {
15148 /* It's an object-like macro. */
15149 int name_len = p - body;
15150 char *name = copy_string (body, name_len);
15151 const char *replacement;
15152
15153 if (*p == ' ')
15154 replacement = body + name_len + 1;
15155 else
15156 {
15157 dwarf2_macro_malformed_definition_complaint (body);
15158 replacement = body + name_len;
15159 }
15160
15161 macro_define_object (file, line, name, replacement);
15162
15163 xfree (name);
15164 }
15165 else if (*p == '(')
15166 {
15167 /* It's a function-like macro. */
15168 char *name = copy_string (body, p - body);
15169 int argc = 0;
15170 int argv_size = 1;
15171 char **argv = xmalloc (argv_size * sizeof (*argv));
15172
15173 p++;
15174
15175 p = consume_improper_spaces (p, body);
15176
15177 /* Parse the formal argument list. */
15178 while (*p && *p != ')')
15179 {
15180 /* Find the extent of the current argument name. */
15181 const char *arg_start = p;
15182
15183 while (*p && *p != ',' && *p != ')' && *p != ' ')
15184 p++;
15185
15186 if (! *p || p == arg_start)
15187 dwarf2_macro_malformed_definition_complaint (body);
15188 else
15189 {
15190 /* Make sure argv has room for the new argument. */
15191 if (argc >= argv_size)
15192 {
15193 argv_size *= 2;
15194 argv = xrealloc (argv, argv_size * sizeof (*argv));
15195 }
15196
15197 argv[argc++] = copy_string (arg_start, p - arg_start);
15198 }
15199
15200 p = consume_improper_spaces (p, body);
15201
15202 /* Consume the comma, if present. */
15203 if (*p == ',')
15204 {
15205 p++;
15206
15207 p = consume_improper_spaces (p, body);
15208 }
15209 }
15210
15211 if (*p == ')')
15212 {
15213 p++;
15214
15215 if (*p == ' ')
15216 /* Perfectly formed definition, no complaints. */
15217 macro_define_function (file, line, name,
15218 argc, (const char **) argv,
15219 p + 1);
15220 else if (*p == '\0')
15221 {
15222 /* Complain, but do define it. */
15223 dwarf2_macro_malformed_definition_complaint (body);
15224 macro_define_function (file, line, name,
15225 argc, (const char **) argv,
15226 p);
15227 }
15228 else
15229 /* Just complain. */
15230 dwarf2_macro_malformed_definition_complaint (body);
15231 }
15232 else
15233 /* Just complain. */
15234 dwarf2_macro_malformed_definition_complaint (body);
15235
15236 xfree (name);
15237 {
15238 int i;
15239
15240 for (i = 0; i < argc; i++)
15241 xfree (argv[i]);
15242 }
15243 xfree (argv);
15244 }
15245 else
15246 dwarf2_macro_malformed_definition_complaint (body);
15247 }
15248
15249 /* Skip some bytes from BYTES according to the form given in FORM.
15250 Returns the new pointer. */
15251
15252 static gdb_byte *
15253 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15254 enum dwarf_form form,
15255 unsigned int offset_size,
15256 struct dwarf2_section_info *section)
15257 {
15258 unsigned int bytes_read;
15259
15260 switch (form)
15261 {
15262 case DW_FORM_data1:
15263 case DW_FORM_flag:
15264 ++bytes;
15265 break;
15266
15267 case DW_FORM_data2:
15268 bytes += 2;
15269 break;
15270
15271 case DW_FORM_data4:
15272 bytes += 4;
15273 break;
15274
15275 case DW_FORM_data8:
15276 bytes += 8;
15277 break;
15278
15279 case DW_FORM_string:
15280 read_direct_string (abfd, bytes, &bytes_read);
15281 bytes += bytes_read;
15282 break;
15283
15284 case DW_FORM_sec_offset:
15285 case DW_FORM_strp:
15286 bytes += offset_size;
15287 break;
15288
15289 case DW_FORM_block:
15290 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15291 bytes += bytes_read;
15292 break;
15293
15294 case DW_FORM_block1:
15295 bytes += 1 + read_1_byte (abfd, bytes);
15296 break;
15297 case DW_FORM_block2:
15298 bytes += 2 + read_2_bytes (abfd, bytes);
15299 break;
15300 case DW_FORM_block4:
15301 bytes += 4 + read_4_bytes (abfd, bytes);
15302 break;
15303
15304 case DW_FORM_sdata:
15305 case DW_FORM_udata:
15306 bytes = skip_leb128 (abfd, bytes);
15307 break;
15308
15309 default:
15310 {
15311 complain:
15312 complaint (&symfile_complaints,
15313 _("invalid form 0x%x in `%s'"),
15314 form,
15315 section->asection->name);
15316 return NULL;
15317 }
15318 }
15319
15320 return bytes;
15321 }
15322
15323 /* A helper for dwarf_decode_macros that handles skipping an unknown
15324 opcode. Returns an updated pointer to the macro data buffer; or,
15325 on error, issues a complaint and returns NULL. */
15326
15327 static gdb_byte *
15328 skip_unknown_opcode (unsigned int opcode,
15329 gdb_byte **opcode_definitions,
15330 gdb_byte *mac_ptr,
15331 bfd *abfd,
15332 unsigned int offset_size,
15333 struct dwarf2_section_info *section)
15334 {
15335 unsigned int bytes_read, i;
15336 unsigned long arg;
15337 gdb_byte *defn;
15338
15339 if (opcode_definitions[opcode] == NULL)
15340 {
15341 complaint (&symfile_complaints,
15342 _("unrecognized DW_MACFINO opcode 0x%x"),
15343 opcode);
15344 return NULL;
15345 }
15346
15347 defn = opcode_definitions[opcode];
15348 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15349 defn += bytes_read;
15350
15351 for (i = 0; i < arg; ++i)
15352 {
15353 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15354 if (mac_ptr == NULL)
15355 {
15356 /* skip_form_bytes already issued the complaint. */
15357 return NULL;
15358 }
15359 }
15360
15361 return mac_ptr;
15362 }
15363
15364 /* A helper function which parses the header of a macro section.
15365 If the macro section is the extended (for now called "GNU") type,
15366 then this updates *OFFSET_SIZE. Returns a pointer to just after
15367 the header, or issues a complaint and returns NULL on error. */
15368
15369 static gdb_byte *
15370 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15371 bfd *abfd,
15372 gdb_byte *mac_ptr,
15373 unsigned int *offset_size,
15374 int section_is_gnu)
15375 {
15376 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15377
15378 if (section_is_gnu)
15379 {
15380 unsigned int version, flags;
15381
15382 version = read_2_bytes (abfd, mac_ptr);
15383 if (version != 4)
15384 {
15385 complaint (&symfile_complaints,
15386 _("unrecognized version `%d' in .debug_macro section"),
15387 version);
15388 return NULL;
15389 }
15390 mac_ptr += 2;
15391
15392 flags = read_1_byte (abfd, mac_ptr);
15393 ++mac_ptr;
15394 *offset_size = (flags & 1) ? 8 : 4;
15395
15396 if ((flags & 2) != 0)
15397 /* We don't need the line table offset. */
15398 mac_ptr += *offset_size;
15399
15400 /* Vendor opcode descriptions. */
15401 if ((flags & 4) != 0)
15402 {
15403 unsigned int i, count;
15404
15405 count = read_1_byte (abfd, mac_ptr);
15406 ++mac_ptr;
15407 for (i = 0; i < count; ++i)
15408 {
15409 unsigned int opcode, bytes_read;
15410 unsigned long arg;
15411
15412 opcode = read_1_byte (abfd, mac_ptr);
15413 ++mac_ptr;
15414 opcode_definitions[opcode] = mac_ptr;
15415 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15416 mac_ptr += bytes_read;
15417 mac_ptr += arg;
15418 }
15419 }
15420 }
15421
15422 return mac_ptr;
15423 }
15424
15425 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15426 including DW_MACRO_GNU_transparent_include. */
15427
15428 static void
15429 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15430 struct macro_source_file *current_file,
15431 struct line_header *lh, char *comp_dir,
15432 struct dwarf2_section_info *section,
15433 int section_is_gnu,
15434 unsigned int offset_size,
15435 struct objfile *objfile,
15436 htab_t include_hash)
15437 {
15438 enum dwarf_macro_record_type macinfo_type;
15439 int at_commandline;
15440 gdb_byte *opcode_definitions[256];
15441
15442 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15443 &offset_size, section_is_gnu);
15444 if (mac_ptr == NULL)
15445 {
15446 /* We already issued a complaint. */
15447 return;
15448 }
15449
15450 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15451 GDB is still reading the definitions from command line. First
15452 DW_MACINFO_start_file will need to be ignored as it was already executed
15453 to create CURRENT_FILE for the main source holding also the command line
15454 definitions. On first met DW_MACINFO_start_file this flag is reset to
15455 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15456
15457 at_commandline = 1;
15458
15459 do
15460 {
15461 /* Do we at least have room for a macinfo type byte? */
15462 if (mac_ptr >= mac_end)
15463 {
15464 dwarf2_macros_too_long_complaint (section);
15465 break;
15466 }
15467
15468 macinfo_type = read_1_byte (abfd, mac_ptr);
15469 mac_ptr++;
15470
15471 /* Note that we rely on the fact that the corresponding GNU and
15472 DWARF constants are the same. */
15473 switch (macinfo_type)
15474 {
15475 /* A zero macinfo type indicates the end of the macro
15476 information. */
15477 case 0:
15478 break;
15479
15480 case DW_MACRO_GNU_define:
15481 case DW_MACRO_GNU_undef:
15482 case DW_MACRO_GNU_define_indirect:
15483 case DW_MACRO_GNU_undef_indirect:
15484 {
15485 unsigned int bytes_read;
15486 int line;
15487 char *body;
15488 int is_define;
15489
15490 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15491 mac_ptr += bytes_read;
15492
15493 if (macinfo_type == DW_MACRO_GNU_define
15494 || macinfo_type == DW_MACRO_GNU_undef)
15495 {
15496 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15497 mac_ptr += bytes_read;
15498 }
15499 else
15500 {
15501 LONGEST str_offset;
15502
15503 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15504 mac_ptr += offset_size;
15505
15506 body = read_indirect_string_at_offset (abfd, str_offset);
15507 }
15508
15509 is_define = (macinfo_type == DW_MACRO_GNU_define
15510 || macinfo_type == DW_MACRO_GNU_define_indirect);
15511 if (! current_file)
15512 {
15513 /* DWARF violation as no main source is present. */
15514 complaint (&symfile_complaints,
15515 _("debug info with no main source gives macro %s "
15516 "on line %d: %s"),
15517 is_define ? _("definition") : _("undefinition"),
15518 line, body);
15519 break;
15520 }
15521 if ((line == 0 && !at_commandline)
15522 || (line != 0 && at_commandline))
15523 complaint (&symfile_complaints,
15524 _("debug info gives %s macro %s with %s line %d: %s"),
15525 at_commandline ? _("command-line") : _("in-file"),
15526 is_define ? _("definition") : _("undefinition"),
15527 line == 0 ? _("zero") : _("non-zero"), line, body);
15528
15529 if (is_define)
15530 parse_macro_definition (current_file, line, body);
15531 else
15532 {
15533 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15534 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15535 macro_undef (current_file, line, body);
15536 }
15537 }
15538 break;
15539
15540 case DW_MACRO_GNU_start_file:
15541 {
15542 unsigned int bytes_read;
15543 int line, file;
15544
15545 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15546 mac_ptr += bytes_read;
15547 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15548 mac_ptr += bytes_read;
15549
15550 if ((line == 0 && !at_commandline)
15551 || (line != 0 && at_commandline))
15552 complaint (&symfile_complaints,
15553 _("debug info gives source %d included "
15554 "from %s at %s line %d"),
15555 file, at_commandline ? _("command-line") : _("file"),
15556 line == 0 ? _("zero") : _("non-zero"), line);
15557
15558 if (at_commandline)
15559 {
15560 /* This DW_MACRO_GNU_start_file was executed in the
15561 pass one. */
15562 at_commandline = 0;
15563 }
15564 else
15565 current_file = macro_start_file (file, line,
15566 current_file, comp_dir,
15567 lh, objfile);
15568 }
15569 break;
15570
15571 case DW_MACRO_GNU_end_file:
15572 if (! current_file)
15573 complaint (&symfile_complaints,
15574 _("macro debug info has an unmatched "
15575 "`close_file' directive"));
15576 else
15577 {
15578 current_file = current_file->included_by;
15579 if (! current_file)
15580 {
15581 enum dwarf_macro_record_type next_type;
15582
15583 /* GCC circa March 2002 doesn't produce the zero
15584 type byte marking the end of the compilation
15585 unit. Complain if it's not there, but exit no
15586 matter what. */
15587
15588 /* Do we at least have room for a macinfo type byte? */
15589 if (mac_ptr >= mac_end)
15590 {
15591 dwarf2_macros_too_long_complaint (section);
15592 return;
15593 }
15594
15595 /* We don't increment mac_ptr here, so this is just
15596 a look-ahead. */
15597 next_type = read_1_byte (abfd, mac_ptr);
15598 if (next_type != 0)
15599 complaint (&symfile_complaints,
15600 _("no terminating 0-type entry for "
15601 "macros in `.debug_macinfo' section"));
15602
15603 return;
15604 }
15605 }
15606 break;
15607
15608 case DW_MACRO_GNU_transparent_include:
15609 {
15610 LONGEST offset;
15611 void **slot;
15612
15613 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15614 mac_ptr += offset_size;
15615
15616 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15617 if (*slot != NULL)
15618 {
15619 /* This has actually happened; see
15620 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15621 complaint (&symfile_complaints,
15622 _("recursive DW_MACRO_GNU_transparent_include in "
15623 ".debug_macro section"));
15624 }
15625 else
15626 {
15627 *slot = mac_ptr;
15628
15629 dwarf_decode_macro_bytes (abfd,
15630 section->buffer + offset,
15631 mac_end, current_file,
15632 lh, comp_dir,
15633 section, section_is_gnu,
15634 offset_size, objfile, include_hash);
15635
15636 htab_remove_elt (include_hash, mac_ptr);
15637 }
15638 }
15639 break;
15640
15641 case DW_MACINFO_vendor_ext:
15642 if (!section_is_gnu)
15643 {
15644 unsigned int bytes_read;
15645 int constant;
15646
15647 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15648 mac_ptr += bytes_read;
15649 read_direct_string (abfd, mac_ptr, &bytes_read);
15650 mac_ptr += bytes_read;
15651
15652 /* We don't recognize any vendor extensions. */
15653 break;
15654 }
15655 /* FALLTHROUGH */
15656
15657 default:
15658 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15659 mac_ptr, abfd, offset_size,
15660 section);
15661 if (mac_ptr == NULL)
15662 return;
15663 break;
15664 }
15665 } while (macinfo_type != 0);
15666 }
15667
15668 static void
15669 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15670 char *comp_dir, bfd *abfd,
15671 struct dwarf2_cu *cu,
15672 struct dwarf2_section_info *section,
15673 int section_is_gnu)
15674 {
15675 struct objfile *objfile = dwarf2_per_objfile->objfile;
15676 gdb_byte *mac_ptr, *mac_end;
15677 struct macro_source_file *current_file = 0;
15678 enum dwarf_macro_record_type macinfo_type;
15679 unsigned int offset_size = cu->header.offset_size;
15680 gdb_byte *opcode_definitions[256];
15681 struct cleanup *cleanup;
15682 htab_t include_hash;
15683 void **slot;
15684
15685 dwarf2_read_section (objfile, section);
15686 if (section->buffer == NULL)
15687 {
15688 complaint (&symfile_complaints, _("missing %s section"),
15689 section->asection->name);
15690 return;
15691 }
15692
15693 /* First pass: Find the name of the base filename.
15694 This filename is needed in order to process all macros whose definition
15695 (or undefinition) comes from the command line. These macros are defined
15696 before the first DW_MACINFO_start_file entry, and yet still need to be
15697 associated to the base file.
15698
15699 To determine the base file name, we scan the macro definitions until we
15700 reach the first DW_MACINFO_start_file entry. We then initialize
15701 CURRENT_FILE accordingly so that any macro definition found before the
15702 first DW_MACINFO_start_file can still be associated to the base file. */
15703
15704 mac_ptr = section->buffer + offset;
15705 mac_end = section->buffer + section->size;
15706
15707 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15708 &offset_size, section_is_gnu);
15709 if (mac_ptr == NULL)
15710 {
15711 /* We already issued a complaint. */
15712 return;
15713 }
15714
15715 do
15716 {
15717 /* Do we at least have room for a macinfo type byte? */
15718 if (mac_ptr >= mac_end)
15719 {
15720 /* Complaint is printed during the second pass as GDB will probably
15721 stop the first pass earlier upon finding
15722 DW_MACINFO_start_file. */
15723 break;
15724 }
15725
15726 macinfo_type = read_1_byte (abfd, mac_ptr);
15727 mac_ptr++;
15728
15729 /* Note that we rely on the fact that the corresponding GNU and
15730 DWARF constants are the same. */
15731 switch (macinfo_type)
15732 {
15733 /* A zero macinfo type indicates the end of the macro
15734 information. */
15735 case 0:
15736 break;
15737
15738 case DW_MACRO_GNU_define:
15739 case DW_MACRO_GNU_undef:
15740 /* Only skip the data by MAC_PTR. */
15741 {
15742 unsigned int bytes_read;
15743
15744 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15745 mac_ptr += bytes_read;
15746 read_direct_string (abfd, mac_ptr, &bytes_read);
15747 mac_ptr += bytes_read;
15748 }
15749 break;
15750
15751 case DW_MACRO_GNU_start_file:
15752 {
15753 unsigned int bytes_read;
15754 int line, file;
15755
15756 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15757 mac_ptr += bytes_read;
15758 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15759 mac_ptr += bytes_read;
15760
15761 current_file = macro_start_file (file, line, current_file,
15762 comp_dir, lh, objfile);
15763 }
15764 break;
15765
15766 case DW_MACRO_GNU_end_file:
15767 /* No data to skip by MAC_PTR. */
15768 break;
15769
15770 case DW_MACRO_GNU_define_indirect:
15771 case DW_MACRO_GNU_undef_indirect:
15772 {
15773 unsigned int bytes_read;
15774
15775 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15776 mac_ptr += bytes_read;
15777 mac_ptr += offset_size;
15778 }
15779 break;
15780
15781 case DW_MACRO_GNU_transparent_include:
15782 /* Note that, according to the spec, a transparent include
15783 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15784 skip this opcode. */
15785 mac_ptr += offset_size;
15786 break;
15787
15788 case DW_MACINFO_vendor_ext:
15789 /* Only skip the data by MAC_PTR. */
15790 if (!section_is_gnu)
15791 {
15792 unsigned int bytes_read;
15793
15794 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15795 mac_ptr += bytes_read;
15796 read_direct_string (abfd, mac_ptr, &bytes_read);
15797 mac_ptr += bytes_read;
15798 }
15799 /* FALLTHROUGH */
15800
15801 default:
15802 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15803 mac_ptr, abfd, offset_size,
15804 section);
15805 if (mac_ptr == NULL)
15806 return;
15807 break;
15808 }
15809 } while (macinfo_type != 0 && current_file == NULL);
15810
15811 /* Second pass: Process all entries.
15812
15813 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15814 command-line macro definitions/undefinitions. This flag is unset when we
15815 reach the first DW_MACINFO_start_file entry. */
15816
15817 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15818 NULL, xcalloc, xfree);
15819 cleanup = make_cleanup_htab_delete (include_hash);
15820 mac_ptr = section->buffer + offset;
15821 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15822 *slot = mac_ptr;
15823 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
15824 current_file, lh, comp_dir, section, section_is_gnu,
15825 offset_size, objfile, include_hash);
15826 do_cleanups (cleanup);
15827 }
15828
15829 /* Check if the attribute's form is a DW_FORM_block*
15830 if so return true else false. */
15831
15832 static int
15833 attr_form_is_block (struct attribute *attr)
15834 {
15835 return (attr == NULL ? 0 :
15836 attr->form == DW_FORM_block1
15837 || attr->form == DW_FORM_block2
15838 || attr->form == DW_FORM_block4
15839 || attr->form == DW_FORM_block
15840 || attr->form == DW_FORM_exprloc);
15841 }
15842
15843 /* Return non-zero if ATTR's value is a section offset --- classes
15844 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15845 You may use DW_UNSND (attr) to retrieve such offsets.
15846
15847 Section 7.5.4, "Attribute Encodings", explains that no attribute
15848 may have a value that belongs to more than one of these classes; it
15849 would be ambiguous if we did, because we use the same forms for all
15850 of them. */
15851
15852 static int
15853 attr_form_is_section_offset (struct attribute *attr)
15854 {
15855 return (attr->form == DW_FORM_data4
15856 || attr->form == DW_FORM_data8
15857 || attr->form == DW_FORM_sec_offset);
15858 }
15859
15860
15861 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15862 zero otherwise. When this function returns true, you can apply
15863 dwarf2_get_attr_constant_value to it.
15864
15865 However, note that for some attributes you must check
15866 attr_form_is_section_offset before using this test. DW_FORM_data4
15867 and DW_FORM_data8 are members of both the constant class, and of
15868 the classes that contain offsets into other debug sections
15869 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15870 that, if an attribute's can be either a constant or one of the
15871 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15872 taken as section offsets, not constants. */
15873
15874 static int
15875 attr_form_is_constant (struct attribute *attr)
15876 {
15877 switch (attr->form)
15878 {
15879 case DW_FORM_sdata:
15880 case DW_FORM_udata:
15881 case DW_FORM_data1:
15882 case DW_FORM_data2:
15883 case DW_FORM_data4:
15884 case DW_FORM_data8:
15885 return 1;
15886 default:
15887 return 0;
15888 }
15889 }
15890
15891 /* A helper function that fills in a dwarf2_loclist_baton. */
15892
15893 static void
15894 fill_in_loclist_baton (struct dwarf2_cu *cu,
15895 struct dwarf2_loclist_baton *baton,
15896 struct attribute *attr)
15897 {
15898 dwarf2_read_section (dwarf2_per_objfile->objfile,
15899 &dwarf2_per_objfile->loc);
15900
15901 baton->per_cu = cu->per_cu;
15902 gdb_assert (baton->per_cu);
15903 /* We don't know how long the location list is, but make sure we
15904 don't run off the edge of the section. */
15905 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15906 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15907 baton->base_address = cu->base_address;
15908 }
15909
15910 static void
15911 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15912 struct dwarf2_cu *cu)
15913 {
15914 struct objfile *objfile = dwarf2_per_objfile->objfile;
15915
15916 if (attr_form_is_section_offset (attr)
15917 /* ".debug_loc" may not exist at all, or the offset may be outside
15918 the section. If so, fall through to the complaint in the
15919 other branch. */
15920 && DW_UNSND (attr) < dwarf2_section_size (objfile,
15921 &dwarf2_per_objfile->loc))
15922 {
15923 struct dwarf2_loclist_baton *baton;
15924
15925 baton = obstack_alloc (&objfile->objfile_obstack,
15926 sizeof (struct dwarf2_loclist_baton));
15927
15928 fill_in_loclist_baton (cu, baton, attr);
15929
15930 if (cu->base_known == 0)
15931 complaint (&symfile_complaints,
15932 _("Location list used without "
15933 "specifying the CU base address."));
15934
15935 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15936 SYMBOL_LOCATION_BATON (sym) = baton;
15937 }
15938 else
15939 {
15940 struct dwarf2_locexpr_baton *baton;
15941
15942 baton = obstack_alloc (&objfile->objfile_obstack,
15943 sizeof (struct dwarf2_locexpr_baton));
15944 baton->per_cu = cu->per_cu;
15945 gdb_assert (baton->per_cu);
15946
15947 if (attr_form_is_block (attr))
15948 {
15949 /* Note that we're just copying the block's data pointer
15950 here, not the actual data. We're still pointing into the
15951 info_buffer for SYM's objfile; right now we never release
15952 that buffer, but when we do clean up properly this may
15953 need to change. */
15954 baton->size = DW_BLOCK (attr)->size;
15955 baton->data = DW_BLOCK (attr)->data;
15956 }
15957 else
15958 {
15959 dwarf2_invalid_attrib_class_complaint ("location description",
15960 SYMBOL_NATURAL_NAME (sym));
15961 baton->size = 0;
15962 }
15963
15964 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15965 SYMBOL_LOCATION_BATON (sym) = baton;
15966 }
15967 }
15968
15969 /* Return the OBJFILE associated with the compilation unit CU. If CU
15970 came from a separate debuginfo file, then the master objfile is
15971 returned. */
15972
15973 struct objfile *
15974 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15975 {
15976 struct objfile *objfile = per_cu->objfile;
15977
15978 /* Return the master objfile, so that we can report and look up the
15979 correct file containing this variable. */
15980 if (objfile->separate_debug_objfile_backlink)
15981 objfile = objfile->separate_debug_objfile_backlink;
15982
15983 return objfile;
15984 }
15985
15986 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15987 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15988 CU_HEADERP first. */
15989
15990 static const struct comp_unit_head *
15991 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15992 struct dwarf2_per_cu_data *per_cu)
15993 {
15994 struct objfile *objfile;
15995 struct dwarf2_per_objfile *per_objfile;
15996 gdb_byte *info_ptr;
15997
15998 if (per_cu->cu)
15999 return &per_cu->cu->header;
16000
16001 objfile = per_cu->objfile;
16002 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16003 info_ptr = per_objfile->info.buffer + per_cu->offset.sect_off;
16004
16005 memset (cu_headerp, 0, sizeof (*cu_headerp));
16006 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
16007
16008 return cu_headerp;
16009 }
16010
16011 /* Return the address size given in the compilation unit header for CU. */
16012
16013 int
16014 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
16015 {
16016 struct comp_unit_head cu_header_local;
16017 const struct comp_unit_head *cu_headerp;
16018
16019 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16020
16021 return cu_headerp->addr_size;
16022 }
16023
16024 /* Return the offset size given in the compilation unit header for CU. */
16025
16026 int
16027 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
16028 {
16029 struct comp_unit_head cu_header_local;
16030 const struct comp_unit_head *cu_headerp;
16031
16032 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16033
16034 return cu_headerp->offset_size;
16035 }
16036
16037 /* See its dwarf2loc.h declaration. */
16038
16039 int
16040 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
16041 {
16042 struct comp_unit_head cu_header_local;
16043 const struct comp_unit_head *cu_headerp;
16044
16045 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
16046
16047 if (cu_headerp->version == 2)
16048 return cu_headerp->addr_size;
16049 else
16050 return cu_headerp->offset_size;
16051 }
16052
16053 /* Return the text offset of the CU. The returned offset comes from
16054 this CU's objfile. If this objfile came from a separate debuginfo
16055 file, then the offset may be different from the corresponding
16056 offset in the parent objfile. */
16057
16058 CORE_ADDR
16059 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
16060 {
16061 struct objfile *objfile = per_cu->objfile;
16062
16063 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16064 }
16065
16066 /* Locate the .debug_info compilation unit from CU's objfile which contains
16067 the DIE at OFFSET. Raises an error on failure. */
16068
16069 static struct dwarf2_per_cu_data *
16070 dwarf2_find_containing_comp_unit (sect_offset offset,
16071 struct objfile *objfile)
16072 {
16073 struct dwarf2_per_cu_data *this_cu;
16074 int low, high;
16075
16076 low = 0;
16077 high = dwarf2_per_objfile->n_comp_units - 1;
16078 while (high > low)
16079 {
16080 int mid = low + (high - low) / 2;
16081
16082 if (dwarf2_per_objfile->all_comp_units[mid]->offset.sect_off
16083 >= offset.sect_off)
16084 high = mid;
16085 else
16086 low = mid + 1;
16087 }
16088 gdb_assert (low == high);
16089 if (dwarf2_per_objfile->all_comp_units[low]->offset.sect_off
16090 > offset.sect_off)
16091 {
16092 if (low == 0)
16093 error (_("Dwarf Error: could not find partial DIE containing "
16094 "offset 0x%lx [in module %s]"),
16095 (long) offset.sect_off, bfd_get_filename (objfile->obfd));
16096
16097 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset.sect_off
16098 <= offset.sect_off);
16099 return dwarf2_per_objfile->all_comp_units[low-1];
16100 }
16101 else
16102 {
16103 this_cu = dwarf2_per_objfile->all_comp_units[low];
16104 if (low == dwarf2_per_objfile->n_comp_units - 1
16105 && offset.sect_off >= this_cu->offset.sect_off + this_cu->length)
16106 error (_("invalid dwarf2 offset %u"), offset.sect_off);
16107 gdb_assert (offset.sect_off < this_cu->offset.sect_off + this_cu->length);
16108 return this_cu;
16109 }
16110 }
16111
16112 /* Initialize dwarf2_cu CU, owned by PER_CU. */
16113
16114 static void
16115 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
16116 {
16117 memset (cu, 0, sizeof (*cu));
16118 per_cu->cu = cu;
16119 cu->per_cu = per_cu;
16120 cu->objfile = per_cu->objfile;
16121 obstack_init (&cu->comp_unit_obstack);
16122 }
16123
16124 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
16125
16126 static void
16127 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
16128 {
16129 struct attribute *attr;
16130
16131 /* Set the language we're debugging. */
16132 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
16133 if (attr)
16134 set_cu_language (DW_UNSND (attr), cu);
16135 else
16136 {
16137 cu->language = language_minimal;
16138 cu->language_defn = language_def (cu->language);
16139 }
16140 }
16141
16142 /* Release one cached compilation unit, CU. We unlink it from the tree
16143 of compilation units, but we don't remove it from the read_in_chain;
16144 the caller is responsible for that.
16145 NOTE: DATA is a void * because this function is also used as a
16146 cleanup routine. */
16147
16148 static void
16149 free_heap_comp_unit (void *data)
16150 {
16151 struct dwarf2_cu *cu = data;
16152
16153 gdb_assert (cu->per_cu != NULL);
16154 cu->per_cu->cu = NULL;
16155 cu->per_cu = NULL;
16156
16157 obstack_free (&cu->comp_unit_obstack, NULL);
16158
16159 xfree (cu);
16160 }
16161
16162 /* This cleanup function is passed the address of a dwarf2_cu on the stack
16163 when we're finished with it. We can't free the pointer itself, but be
16164 sure to unlink it from the cache. Also release any associated storage
16165 and perform cache maintenance.
16166
16167 Only used during partial symbol parsing. */
16168
16169 static void
16170 free_stack_comp_unit (void *data)
16171 {
16172 struct dwarf2_cu *cu = data;
16173
16174 gdb_assert (cu->per_cu != NULL);
16175 cu->per_cu->cu = NULL;
16176 cu->per_cu = NULL;
16177
16178 obstack_free (&cu->comp_unit_obstack, NULL);
16179 cu->partial_dies = NULL;
16180
16181 /* The previous code only did this if per_cu != NULL.
16182 But that would always succeed, so now we just unconditionally do
16183 the aging. This seems like the wrong place to do such aging,
16184 but cleaning that up is left for later. */
16185 age_cached_comp_units ();
16186 }
16187
16188 /* Free all cached compilation units. */
16189
16190 static void
16191 free_cached_comp_units (void *data)
16192 {
16193 struct dwarf2_per_cu_data *per_cu, **last_chain;
16194
16195 per_cu = dwarf2_per_objfile->read_in_chain;
16196 last_chain = &dwarf2_per_objfile->read_in_chain;
16197 while (per_cu != NULL)
16198 {
16199 struct dwarf2_per_cu_data *next_cu;
16200
16201 next_cu = per_cu->cu->read_in_chain;
16202
16203 free_heap_comp_unit (per_cu->cu);
16204 *last_chain = next_cu;
16205
16206 per_cu = next_cu;
16207 }
16208 }
16209
16210 /* Increase the age counter on each cached compilation unit, and free
16211 any that are too old. */
16212
16213 static void
16214 age_cached_comp_units (void)
16215 {
16216 struct dwarf2_per_cu_data *per_cu, **last_chain;
16217
16218 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16219 per_cu = dwarf2_per_objfile->read_in_chain;
16220 while (per_cu != NULL)
16221 {
16222 per_cu->cu->last_used ++;
16223 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16224 dwarf2_mark (per_cu->cu);
16225 per_cu = per_cu->cu->read_in_chain;
16226 }
16227
16228 per_cu = dwarf2_per_objfile->read_in_chain;
16229 last_chain = &dwarf2_per_objfile->read_in_chain;
16230 while (per_cu != NULL)
16231 {
16232 struct dwarf2_per_cu_data *next_cu;
16233
16234 next_cu = per_cu->cu->read_in_chain;
16235
16236 if (!per_cu->cu->mark)
16237 {
16238 free_heap_comp_unit (per_cu->cu);
16239 *last_chain = next_cu;
16240 }
16241 else
16242 last_chain = &per_cu->cu->read_in_chain;
16243
16244 per_cu = next_cu;
16245 }
16246 }
16247
16248 /* Remove a single compilation unit from the cache. */
16249
16250 static void
16251 free_one_cached_comp_unit (void *target_cu)
16252 {
16253 struct dwarf2_per_cu_data *per_cu, **last_chain;
16254
16255 per_cu = dwarf2_per_objfile->read_in_chain;
16256 last_chain = &dwarf2_per_objfile->read_in_chain;
16257 while (per_cu != NULL)
16258 {
16259 struct dwarf2_per_cu_data *next_cu;
16260
16261 next_cu = per_cu->cu->read_in_chain;
16262
16263 if (per_cu->cu == target_cu)
16264 {
16265 free_heap_comp_unit (per_cu->cu);
16266 *last_chain = next_cu;
16267 break;
16268 }
16269 else
16270 last_chain = &per_cu->cu->read_in_chain;
16271
16272 per_cu = next_cu;
16273 }
16274 }
16275
16276 /* Release all extra memory associated with OBJFILE. */
16277
16278 void
16279 dwarf2_free_objfile (struct objfile *objfile)
16280 {
16281 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16282
16283 if (dwarf2_per_objfile == NULL)
16284 return;
16285
16286 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16287 free_cached_comp_units (NULL);
16288
16289 if (dwarf2_per_objfile->quick_file_names_table)
16290 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16291
16292 /* Everything else should be on the objfile obstack. */
16293 }
16294
16295 /* A pair of DIE offset and GDB type pointer. We store these
16296 in a hash table separate from the DIEs, and preserve them
16297 when the DIEs are flushed out of cache. */
16298
16299 struct dwarf2_offset_and_type
16300 {
16301 sect_offset offset;
16302 struct type *type;
16303 };
16304
16305 /* Hash function for a dwarf2_offset_and_type. */
16306
16307 static hashval_t
16308 offset_and_type_hash (const void *item)
16309 {
16310 const struct dwarf2_offset_and_type *ofs = item;
16311
16312 return ofs->offset.sect_off;
16313 }
16314
16315 /* Equality function for a dwarf2_offset_and_type. */
16316
16317 static int
16318 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16319 {
16320 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16321 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16322
16323 return ofs_lhs->offset.sect_off == ofs_rhs->offset.sect_off;
16324 }
16325
16326 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16327 table if necessary. For convenience, return TYPE.
16328
16329 The DIEs reading must have careful ordering to:
16330 * Not cause infite loops trying to read in DIEs as a prerequisite for
16331 reading current DIE.
16332 * Not trying to dereference contents of still incompletely read in types
16333 while reading in other DIEs.
16334 * Enable referencing still incompletely read in types just by a pointer to
16335 the type without accessing its fields.
16336
16337 Therefore caller should follow these rules:
16338 * Try to fetch any prerequisite types we may need to build this DIE type
16339 before building the type and calling set_die_type.
16340 * After building type call set_die_type for current DIE as soon as
16341 possible before fetching more types to complete the current type.
16342 * Make the type as complete as possible before fetching more types. */
16343
16344 static struct type *
16345 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16346 {
16347 struct dwarf2_offset_and_type **slot, ofs;
16348 struct objfile *objfile = cu->objfile;
16349 htab_t *type_hash_ptr;
16350
16351 /* For Ada types, make sure that the gnat-specific data is always
16352 initialized (if not already set). There are a few types where
16353 we should not be doing so, because the type-specific area is
16354 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16355 where the type-specific area is used to store the floatformat).
16356 But this is not a problem, because the gnat-specific information
16357 is actually not needed for these types. */
16358 if (need_gnat_info (cu)
16359 && TYPE_CODE (type) != TYPE_CODE_FUNC
16360 && TYPE_CODE (type) != TYPE_CODE_FLT
16361 && !HAVE_GNAT_AUX_INFO (type))
16362 INIT_GNAT_SPECIFIC (type);
16363
16364 if (cu->per_cu->debug_types_section)
16365 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16366 else
16367 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16368
16369 if (*type_hash_ptr == NULL)
16370 {
16371 *type_hash_ptr
16372 = htab_create_alloc_ex (127,
16373 offset_and_type_hash,
16374 offset_and_type_eq,
16375 NULL,
16376 &objfile->objfile_obstack,
16377 hashtab_obstack_allocate,
16378 dummy_obstack_deallocate);
16379 }
16380
16381 ofs.offset = die->offset;
16382 ofs.type = type;
16383 slot = (struct dwarf2_offset_and_type **)
16384 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset.sect_off,
16385 INSERT);
16386 if (*slot)
16387 complaint (&symfile_complaints,
16388 _("A problem internal to GDB: DIE 0x%x has type already set"),
16389 die->offset.sect_off);
16390 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16391 **slot = ofs;
16392 return type;
16393 }
16394
16395 /* Look up the type for the die at OFFSET in the appropriate type_hash
16396 table, or return NULL if the die does not have a saved type. */
16397
16398 static struct type *
16399 get_die_type_at_offset (sect_offset offset,
16400 struct dwarf2_per_cu_data *per_cu)
16401 {
16402 struct dwarf2_offset_and_type *slot, ofs;
16403 htab_t type_hash;
16404
16405 if (per_cu->debug_types_section)
16406 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16407 else
16408 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16409 if (type_hash == NULL)
16410 return NULL;
16411
16412 ofs.offset = offset;
16413 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset.sect_off);
16414 if (slot)
16415 return slot->type;
16416 else
16417 return NULL;
16418 }
16419
16420 /* Look up the type for DIE in the appropriate type_hash table,
16421 or return NULL if DIE does not have a saved type. */
16422
16423 static struct type *
16424 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16425 {
16426 return get_die_type_at_offset (die->offset, cu->per_cu);
16427 }
16428
16429 /* Add a dependence relationship from CU to REF_PER_CU. */
16430
16431 static void
16432 dwarf2_add_dependence (struct dwarf2_cu *cu,
16433 struct dwarf2_per_cu_data *ref_per_cu)
16434 {
16435 void **slot;
16436
16437 if (cu->dependencies == NULL)
16438 cu->dependencies
16439 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16440 NULL, &cu->comp_unit_obstack,
16441 hashtab_obstack_allocate,
16442 dummy_obstack_deallocate);
16443
16444 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16445 if (*slot == NULL)
16446 *slot = ref_per_cu;
16447 }
16448
16449 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16450 Set the mark field in every compilation unit in the
16451 cache that we must keep because we are keeping CU. */
16452
16453 static int
16454 dwarf2_mark_helper (void **slot, void *data)
16455 {
16456 struct dwarf2_per_cu_data *per_cu;
16457
16458 per_cu = (struct dwarf2_per_cu_data *) *slot;
16459
16460 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16461 reading of the chain. As such dependencies remain valid it is not much
16462 useful to track and undo them during QUIT cleanups. */
16463 if (per_cu->cu == NULL)
16464 return 1;
16465
16466 if (per_cu->cu->mark)
16467 return 1;
16468 per_cu->cu->mark = 1;
16469
16470 if (per_cu->cu->dependencies != NULL)
16471 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16472
16473 return 1;
16474 }
16475
16476 /* Set the mark field in CU and in every other compilation unit in the
16477 cache that we must keep because we are keeping CU. */
16478
16479 static void
16480 dwarf2_mark (struct dwarf2_cu *cu)
16481 {
16482 if (cu->mark)
16483 return;
16484 cu->mark = 1;
16485 if (cu->dependencies != NULL)
16486 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16487 }
16488
16489 static void
16490 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16491 {
16492 while (per_cu)
16493 {
16494 per_cu->cu->mark = 0;
16495 per_cu = per_cu->cu->read_in_chain;
16496 }
16497 }
16498
16499 /* Trivial hash function for partial_die_info: the hash value of a DIE
16500 is its offset in .debug_info for this objfile. */
16501
16502 static hashval_t
16503 partial_die_hash (const void *item)
16504 {
16505 const struct partial_die_info *part_die = item;
16506
16507 return part_die->offset.sect_off;
16508 }
16509
16510 /* Trivial comparison function for partial_die_info structures: two DIEs
16511 are equal if they have the same offset. */
16512
16513 static int
16514 partial_die_eq (const void *item_lhs, const void *item_rhs)
16515 {
16516 const struct partial_die_info *part_die_lhs = item_lhs;
16517 const struct partial_die_info *part_die_rhs = item_rhs;
16518
16519 return part_die_lhs->offset.sect_off == part_die_rhs->offset.sect_off;
16520 }
16521
16522 static struct cmd_list_element *set_dwarf2_cmdlist;
16523 static struct cmd_list_element *show_dwarf2_cmdlist;
16524
16525 static void
16526 set_dwarf2_cmd (char *args, int from_tty)
16527 {
16528 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16529 }
16530
16531 static void
16532 show_dwarf2_cmd (char *args, int from_tty)
16533 {
16534 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16535 }
16536
16537 /* If section described by INFO was mmapped, munmap it now. */
16538
16539 static void
16540 munmap_section_buffer (struct dwarf2_section_info *info)
16541 {
16542 if (info->map_addr != NULL)
16543 {
16544 #ifdef HAVE_MMAP
16545 int res;
16546
16547 res = munmap (info->map_addr, info->map_len);
16548 gdb_assert (res == 0);
16549 #else
16550 /* Without HAVE_MMAP, we should never be here to begin with. */
16551 gdb_assert_not_reached ("no mmap support");
16552 #endif
16553 }
16554 }
16555
16556 /* munmap debug sections for OBJFILE, if necessary. */
16557
16558 static void
16559 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16560 {
16561 struct dwarf2_per_objfile *data = d;
16562 int ix;
16563 struct dwarf2_section_info *section;
16564
16565 /* This is sorted according to the order they're defined in to make it easier
16566 to keep in sync. */
16567 munmap_section_buffer (&data->info);
16568 munmap_section_buffer (&data->abbrev);
16569 munmap_section_buffer (&data->line);
16570 munmap_section_buffer (&data->loc);
16571 munmap_section_buffer (&data->macinfo);
16572 munmap_section_buffer (&data->macro);
16573 munmap_section_buffer (&data->str);
16574 munmap_section_buffer (&data->ranges);
16575 munmap_section_buffer (&data->frame);
16576 munmap_section_buffer (&data->eh_frame);
16577 munmap_section_buffer (&data->gdb_index);
16578
16579 for (ix = 0;
16580 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16581 ++ix)
16582 munmap_section_buffer (section);
16583
16584 VEC_free (dwarf2_section_info_def, data->types);
16585 }
16586
16587 \f
16588 /* The "save gdb-index" command. */
16589
16590 /* The contents of the hash table we create when building the string
16591 table. */
16592 struct strtab_entry
16593 {
16594 offset_type offset;
16595 const char *str;
16596 };
16597
16598 /* Hash function for a strtab_entry.
16599
16600 Function is used only during write_hash_table so no index format backward
16601 compatibility is needed. */
16602
16603 static hashval_t
16604 hash_strtab_entry (const void *e)
16605 {
16606 const struct strtab_entry *entry = e;
16607 return mapped_index_string_hash (INT_MAX, entry->str);
16608 }
16609
16610 /* Equality function for a strtab_entry. */
16611
16612 static int
16613 eq_strtab_entry (const void *a, const void *b)
16614 {
16615 const struct strtab_entry *ea = a;
16616 const struct strtab_entry *eb = b;
16617 return !strcmp (ea->str, eb->str);
16618 }
16619
16620 /* Create a strtab_entry hash table. */
16621
16622 static htab_t
16623 create_strtab (void)
16624 {
16625 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16626 xfree, xcalloc, xfree);
16627 }
16628
16629 /* Add a string to the constant pool. Return the string's offset in
16630 host order. */
16631
16632 static offset_type
16633 add_string (htab_t table, struct obstack *cpool, const char *str)
16634 {
16635 void **slot;
16636 struct strtab_entry entry;
16637 struct strtab_entry *result;
16638
16639 entry.str = str;
16640 slot = htab_find_slot (table, &entry, INSERT);
16641 if (*slot)
16642 result = *slot;
16643 else
16644 {
16645 result = XNEW (struct strtab_entry);
16646 result->offset = obstack_object_size (cpool);
16647 result->str = str;
16648 obstack_grow_str0 (cpool, str);
16649 *slot = result;
16650 }
16651 return result->offset;
16652 }
16653
16654 /* An entry in the symbol table. */
16655 struct symtab_index_entry
16656 {
16657 /* The name of the symbol. */
16658 const char *name;
16659 /* The offset of the name in the constant pool. */
16660 offset_type index_offset;
16661 /* A sorted vector of the indices of all the CUs that hold an object
16662 of this name. */
16663 VEC (offset_type) *cu_indices;
16664 };
16665
16666 /* The symbol table. This is a power-of-2-sized hash table. */
16667 struct mapped_symtab
16668 {
16669 offset_type n_elements;
16670 offset_type size;
16671 struct symtab_index_entry **data;
16672 };
16673
16674 /* Hash function for a symtab_index_entry. */
16675
16676 static hashval_t
16677 hash_symtab_entry (const void *e)
16678 {
16679 const struct symtab_index_entry *entry = e;
16680 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16681 sizeof (offset_type) * VEC_length (offset_type,
16682 entry->cu_indices),
16683 0);
16684 }
16685
16686 /* Equality function for a symtab_index_entry. */
16687
16688 static int
16689 eq_symtab_entry (const void *a, const void *b)
16690 {
16691 const struct symtab_index_entry *ea = a;
16692 const struct symtab_index_entry *eb = b;
16693 int len = VEC_length (offset_type, ea->cu_indices);
16694 if (len != VEC_length (offset_type, eb->cu_indices))
16695 return 0;
16696 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16697 VEC_address (offset_type, eb->cu_indices),
16698 sizeof (offset_type) * len);
16699 }
16700
16701 /* Destroy a symtab_index_entry. */
16702
16703 static void
16704 delete_symtab_entry (void *p)
16705 {
16706 struct symtab_index_entry *entry = p;
16707 VEC_free (offset_type, entry->cu_indices);
16708 xfree (entry);
16709 }
16710
16711 /* Create a hash table holding symtab_index_entry objects. */
16712
16713 static htab_t
16714 create_symbol_hash_table (void)
16715 {
16716 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16717 delete_symtab_entry, xcalloc, xfree);
16718 }
16719
16720 /* Create a new mapped symtab object. */
16721
16722 static struct mapped_symtab *
16723 create_mapped_symtab (void)
16724 {
16725 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16726 symtab->n_elements = 0;
16727 symtab->size = 1024;
16728 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16729 return symtab;
16730 }
16731
16732 /* Destroy a mapped_symtab. */
16733
16734 static void
16735 cleanup_mapped_symtab (void *p)
16736 {
16737 struct mapped_symtab *symtab = p;
16738 /* The contents of the array are freed when the other hash table is
16739 destroyed. */
16740 xfree (symtab->data);
16741 xfree (symtab);
16742 }
16743
16744 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16745 the slot.
16746
16747 Function is used only during write_hash_table so no index format backward
16748 compatibility is needed. */
16749
16750 static struct symtab_index_entry **
16751 find_slot (struct mapped_symtab *symtab, const char *name)
16752 {
16753 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16754
16755 index = hash & (symtab->size - 1);
16756 step = ((hash * 17) & (symtab->size - 1)) | 1;
16757
16758 for (;;)
16759 {
16760 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16761 return &symtab->data[index];
16762 index = (index + step) & (symtab->size - 1);
16763 }
16764 }
16765
16766 /* Expand SYMTAB's hash table. */
16767
16768 static void
16769 hash_expand (struct mapped_symtab *symtab)
16770 {
16771 offset_type old_size = symtab->size;
16772 offset_type i;
16773 struct symtab_index_entry **old_entries = symtab->data;
16774
16775 symtab->size *= 2;
16776 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16777
16778 for (i = 0; i < old_size; ++i)
16779 {
16780 if (old_entries[i])
16781 {
16782 struct symtab_index_entry **slot = find_slot (symtab,
16783 old_entries[i]->name);
16784 *slot = old_entries[i];
16785 }
16786 }
16787
16788 xfree (old_entries);
16789 }
16790
16791 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16792 is the index of the CU in which the symbol appears. */
16793
16794 static void
16795 add_index_entry (struct mapped_symtab *symtab, const char *name,
16796 offset_type cu_index)
16797 {
16798 struct symtab_index_entry **slot;
16799
16800 ++symtab->n_elements;
16801 if (4 * symtab->n_elements / 3 >= symtab->size)
16802 hash_expand (symtab);
16803
16804 slot = find_slot (symtab, name);
16805 if (!*slot)
16806 {
16807 *slot = XNEW (struct symtab_index_entry);
16808 (*slot)->name = name;
16809 (*slot)->cu_indices = NULL;
16810 }
16811 /* Don't push an index twice. Due to how we add entries we only
16812 have to check the last one. */
16813 if (VEC_empty (offset_type, (*slot)->cu_indices)
16814 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16815 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16816 }
16817
16818 /* Add a vector of indices to the constant pool. */
16819
16820 static offset_type
16821 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16822 struct symtab_index_entry *entry)
16823 {
16824 void **slot;
16825
16826 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16827 if (!*slot)
16828 {
16829 offset_type len = VEC_length (offset_type, entry->cu_indices);
16830 offset_type val = MAYBE_SWAP (len);
16831 offset_type iter;
16832 int i;
16833
16834 *slot = entry;
16835 entry->index_offset = obstack_object_size (cpool);
16836
16837 obstack_grow (cpool, &val, sizeof (val));
16838 for (i = 0;
16839 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16840 ++i)
16841 {
16842 val = MAYBE_SWAP (iter);
16843 obstack_grow (cpool, &val, sizeof (val));
16844 }
16845 }
16846 else
16847 {
16848 struct symtab_index_entry *old_entry = *slot;
16849 entry->index_offset = old_entry->index_offset;
16850 entry = old_entry;
16851 }
16852 return entry->index_offset;
16853 }
16854
16855 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16856 constant pool entries going into the obstack CPOOL. */
16857
16858 static void
16859 write_hash_table (struct mapped_symtab *symtab,
16860 struct obstack *output, struct obstack *cpool)
16861 {
16862 offset_type i;
16863 htab_t symbol_hash_table;
16864 htab_t str_table;
16865
16866 symbol_hash_table = create_symbol_hash_table ();
16867 str_table = create_strtab ();
16868
16869 /* We add all the index vectors to the constant pool first, to
16870 ensure alignment is ok. */
16871 for (i = 0; i < symtab->size; ++i)
16872 {
16873 if (symtab->data[i])
16874 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16875 }
16876
16877 /* Now write out the hash table. */
16878 for (i = 0; i < symtab->size; ++i)
16879 {
16880 offset_type str_off, vec_off;
16881
16882 if (symtab->data[i])
16883 {
16884 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16885 vec_off = symtab->data[i]->index_offset;
16886 }
16887 else
16888 {
16889 /* While 0 is a valid constant pool index, it is not valid
16890 to have 0 for both offsets. */
16891 str_off = 0;
16892 vec_off = 0;
16893 }
16894
16895 str_off = MAYBE_SWAP (str_off);
16896 vec_off = MAYBE_SWAP (vec_off);
16897
16898 obstack_grow (output, &str_off, sizeof (str_off));
16899 obstack_grow (output, &vec_off, sizeof (vec_off));
16900 }
16901
16902 htab_delete (str_table);
16903 htab_delete (symbol_hash_table);
16904 }
16905
16906 /* Struct to map psymtab to CU index in the index file. */
16907 struct psymtab_cu_index_map
16908 {
16909 struct partial_symtab *psymtab;
16910 unsigned int cu_index;
16911 };
16912
16913 static hashval_t
16914 hash_psymtab_cu_index (const void *item)
16915 {
16916 const struct psymtab_cu_index_map *map = item;
16917
16918 return htab_hash_pointer (map->psymtab);
16919 }
16920
16921 static int
16922 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16923 {
16924 const struct psymtab_cu_index_map *lhs = item_lhs;
16925 const struct psymtab_cu_index_map *rhs = item_rhs;
16926
16927 return lhs->psymtab == rhs->psymtab;
16928 }
16929
16930 /* Helper struct for building the address table. */
16931 struct addrmap_index_data
16932 {
16933 struct objfile *objfile;
16934 struct obstack *addr_obstack;
16935 htab_t cu_index_htab;
16936
16937 /* Non-zero if the previous_* fields are valid.
16938 We can't write an entry until we see the next entry (since it is only then
16939 that we know the end of the entry). */
16940 int previous_valid;
16941 /* Index of the CU in the table of all CUs in the index file. */
16942 unsigned int previous_cu_index;
16943 /* Start address of the CU. */
16944 CORE_ADDR previous_cu_start;
16945 };
16946
16947 /* Write an address entry to OBSTACK. */
16948
16949 static void
16950 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16951 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16952 {
16953 offset_type cu_index_to_write;
16954 char addr[8];
16955 CORE_ADDR baseaddr;
16956
16957 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16958
16959 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16960 obstack_grow (obstack, addr, 8);
16961 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16962 obstack_grow (obstack, addr, 8);
16963 cu_index_to_write = MAYBE_SWAP (cu_index);
16964 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16965 }
16966
16967 /* Worker function for traversing an addrmap to build the address table. */
16968
16969 static int
16970 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16971 {
16972 struct addrmap_index_data *data = datap;
16973 struct partial_symtab *pst = obj;
16974 offset_type cu_index;
16975 void **slot;
16976
16977 if (data->previous_valid)
16978 add_address_entry (data->objfile, data->addr_obstack,
16979 data->previous_cu_start, start_addr,
16980 data->previous_cu_index);
16981
16982 data->previous_cu_start = start_addr;
16983 if (pst != NULL)
16984 {
16985 struct psymtab_cu_index_map find_map, *map;
16986 find_map.psymtab = pst;
16987 map = htab_find (data->cu_index_htab, &find_map);
16988 gdb_assert (map != NULL);
16989 data->previous_cu_index = map->cu_index;
16990 data->previous_valid = 1;
16991 }
16992 else
16993 data->previous_valid = 0;
16994
16995 return 0;
16996 }
16997
16998 /* Write OBJFILE's address map to OBSTACK.
16999 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
17000 in the index file. */
17001
17002 static void
17003 write_address_map (struct objfile *objfile, struct obstack *obstack,
17004 htab_t cu_index_htab)
17005 {
17006 struct addrmap_index_data addrmap_index_data;
17007
17008 /* When writing the address table, we have to cope with the fact that
17009 the addrmap iterator only provides the start of a region; we have to
17010 wait until the next invocation to get the start of the next region. */
17011
17012 addrmap_index_data.objfile = objfile;
17013 addrmap_index_data.addr_obstack = obstack;
17014 addrmap_index_data.cu_index_htab = cu_index_htab;
17015 addrmap_index_data.previous_valid = 0;
17016
17017 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
17018 &addrmap_index_data);
17019
17020 /* It's highly unlikely the last entry (end address = 0xff...ff)
17021 is valid, but we should still handle it.
17022 The end address is recorded as the start of the next region, but that
17023 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
17024 anyway. */
17025 if (addrmap_index_data.previous_valid)
17026 add_address_entry (objfile, obstack,
17027 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
17028 addrmap_index_data.previous_cu_index);
17029 }
17030
17031 /* Add a list of partial symbols to SYMTAB. */
17032
17033 static void
17034 write_psymbols (struct mapped_symtab *symtab,
17035 htab_t psyms_seen,
17036 struct partial_symbol **psymp,
17037 int count,
17038 offset_type cu_index,
17039 int is_static)
17040 {
17041 for (; count-- > 0; ++psymp)
17042 {
17043 void **slot, *lookup;
17044
17045 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
17046 error (_("Ada is not currently supported by the index"));
17047
17048 /* We only want to add a given psymbol once. However, we also
17049 want to account for whether it is global or static. So, we
17050 may add it twice, using slightly different values. */
17051 if (is_static)
17052 {
17053 uintptr_t val = 1 | (uintptr_t) *psymp;
17054
17055 lookup = (void *) val;
17056 }
17057 else
17058 lookup = *psymp;
17059
17060 /* Only add a given psymbol once. */
17061 slot = htab_find_slot (psyms_seen, lookup, INSERT);
17062 if (!*slot)
17063 {
17064 *slot = lookup;
17065 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
17066 }
17067 }
17068 }
17069
17070 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
17071 exception if there is an error. */
17072
17073 static void
17074 write_obstack (FILE *file, struct obstack *obstack)
17075 {
17076 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
17077 file)
17078 != obstack_object_size (obstack))
17079 error (_("couldn't data write to file"));
17080 }
17081
17082 /* Unlink a file if the argument is not NULL. */
17083
17084 static void
17085 unlink_if_set (void *p)
17086 {
17087 char **filename = p;
17088 if (*filename)
17089 unlink (*filename);
17090 }
17091
17092 /* A helper struct used when iterating over debug_types. */
17093 struct signatured_type_index_data
17094 {
17095 struct objfile *objfile;
17096 struct mapped_symtab *symtab;
17097 struct obstack *types_list;
17098 htab_t psyms_seen;
17099 int cu_index;
17100 };
17101
17102 /* A helper function that writes a single signatured_type to an
17103 obstack. */
17104
17105 static int
17106 write_one_signatured_type (void **slot, void *d)
17107 {
17108 struct signatured_type_index_data *info = d;
17109 struct signatured_type *entry = (struct signatured_type *) *slot;
17110 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
17111 struct partial_symtab *psymtab = per_cu->v.psymtab;
17112 gdb_byte val[8];
17113
17114 write_psymbols (info->symtab,
17115 info->psyms_seen,
17116 info->objfile->global_psymbols.list
17117 + psymtab->globals_offset,
17118 psymtab->n_global_syms, info->cu_index,
17119 0);
17120 write_psymbols (info->symtab,
17121 info->psyms_seen,
17122 info->objfile->static_psymbols.list
17123 + psymtab->statics_offset,
17124 psymtab->n_static_syms, info->cu_index,
17125 1);
17126
17127 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17128 entry->per_cu.offset.sect_off);
17129 obstack_grow (info->types_list, val, 8);
17130 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset.cu_off);
17131 obstack_grow (info->types_list, val, 8);
17132 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
17133 obstack_grow (info->types_list, val, 8);
17134
17135 ++info->cu_index;
17136
17137 return 1;
17138 }
17139
17140 /* Create an index file for OBJFILE in the directory DIR. */
17141
17142 static void
17143 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
17144 {
17145 struct cleanup *cleanup;
17146 char *filename, *cleanup_filename;
17147 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
17148 struct obstack cu_list, types_cu_list;
17149 int i;
17150 FILE *out_file;
17151 struct mapped_symtab *symtab;
17152 offset_type val, size_of_contents, total_len;
17153 struct stat st;
17154 char buf[8];
17155 htab_t psyms_seen;
17156 htab_t cu_index_htab;
17157 struct psymtab_cu_index_map *psymtab_cu_index_map;
17158
17159 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
17160 return;
17161
17162 if (dwarf2_per_objfile->using_index)
17163 error (_("Cannot use an index to create the index"));
17164
17165 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
17166 error (_("Cannot make an index when the file has multiple .debug_types sections"));
17167
17168 if (stat (objfile->name, &st) < 0)
17169 perror_with_name (objfile->name);
17170
17171 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
17172 INDEX_SUFFIX, (char *) NULL);
17173 cleanup = make_cleanup (xfree, filename);
17174
17175 out_file = fopen (filename, "wb");
17176 if (!out_file)
17177 error (_("Can't open `%s' for writing"), filename);
17178
17179 cleanup_filename = filename;
17180 make_cleanup (unlink_if_set, &cleanup_filename);
17181
17182 symtab = create_mapped_symtab ();
17183 make_cleanup (cleanup_mapped_symtab, symtab);
17184
17185 obstack_init (&addr_obstack);
17186 make_cleanup_obstack_free (&addr_obstack);
17187
17188 obstack_init (&cu_list);
17189 make_cleanup_obstack_free (&cu_list);
17190
17191 obstack_init (&types_cu_list);
17192 make_cleanup_obstack_free (&types_cu_list);
17193
17194 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
17195 NULL, xcalloc, xfree);
17196 make_cleanup_htab_delete (psyms_seen);
17197
17198 /* While we're scanning CU's create a table that maps a psymtab pointer
17199 (which is what addrmap records) to its index (which is what is recorded
17200 in the index file). This will later be needed to write the address
17201 table. */
17202 cu_index_htab = htab_create_alloc (100,
17203 hash_psymtab_cu_index,
17204 eq_psymtab_cu_index,
17205 NULL, xcalloc, xfree);
17206 make_cleanup_htab_delete (cu_index_htab);
17207 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17208 xmalloc (sizeof (struct psymtab_cu_index_map)
17209 * dwarf2_per_objfile->n_comp_units);
17210 make_cleanup (xfree, psymtab_cu_index_map);
17211
17212 /* The CU list is already sorted, so we don't need to do additional
17213 work here. Also, the debug_types entries do not appear in
17214 all_comp_units, but only in their own hash table. */
17215 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17216 {
17217 struct dwarf2_per_cu_data *per_cu
17218 = dwarf2_per_objfile->all_comp_units[i];
17219 struct partial_symtab *psymtab = per_cu->v.psymtab;
17220 gdb_byte val[8];
17221 struct psymtab_cu_index_map *map;
17222 void **slot;
17223
17224 write_psymbols (symtab,
17225 psyms_seen,
17226 objfile->global_psymbols.list + psymtab->globals_offset,
17227 psymtab->n_global_syms, i,
17228 0);
17229 write_psymbols (symtab,
17230 psyms_seen,
17231 objfile->static_psymbols.list + psymtab->statics_offset,
17232 psymtab->n_static_syms, i,
17233 1);
17234
17235 map = &psymtab_cu_index_map[i];
17236 map->psymtab = psymtab;
17237 map->cu_index = i;
17238 slot = htab_find_slot (cu_index_htab, map, INSERT);
17239 gdb_assert (slot != NULL);
17240 gdb_assert (*slot == NULL);
17241 *slot = map;
17242
17243 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE,
17244 per_cu->offset.sect_off);
17245 obstack_grow (&cu_list, val, 8);
17246 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17247 obstack_grow (&cu_list, val, 8);
17248 }
17249
17250 /* Dump the address map. */
17251 write_address_map (objfile, &addr_obstack, cu_index_htab);
17252
17253 /* Write out the .debug_type entries, if any. */
17254 if (dwarf2_per_objfile->signatured_types)
17255 {
17256 struct signatured_type_index_data sig_data;
17257
17258 sig_data.objfile = objfile;
17259 sig_data.symtab = symtab;
17260 sig_data.types_list = &types_cu_list;
17261 sig_data.psyms_seen = psyms_seen;
17262 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17263 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17264 write_one_signatured_type, &sig_data);
17265 }
17266
17267 obstack_init (&constant_pool);
17268 make_cleanup_obstack_free (&constant_pool);
17269 obstack_init (&symtab_obstack);
17270 make_cleanup_obstack_free (&symtab_obstack);
17271 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17272
17273 obstack_init (&contents);
17274 make_cleanup_obstack_free (&contents);
17275 size_of_contents = 6 * sizeof (offset_type);
17276 total_len = size_of_contents;
17277
17278 /* The version number. */
17279 val = MAYBE_SWAP (6);
17280 obstack_grow (&contents, &val, sizeof (val));
17281
17282 /* The offset of the CU list from the start of the file. */
17283 val = MAYBE_SWAP (total_len);
17284 obstack_grow (&contents, &val, sizeof (val));
17285 total_len += obstack_object_size (&cu_list);
17286
17287 /* The offset of the types CU list from the start of the file. */
17288 val = MAYBE_SWAP (total_len);
17289 obstack_grow (&contents, &val, sizeof (val));
17290 total_len += obstack_object_size (&types_cu_list);
17291
17292 /* The offset of the address table from the start of the file. */
17293 val = MAYBE_SWAP (total_len);
17294 obstack_grow (&contents, &val, sizeof (val));
17295 total_len += obstack_object_size (&addr_obstack);
17296
17297 /* The offset of the symbol table from the start of the file. */
17298 val = MAYBE_SWAP (total_len);
17299 obstack_grow (&contents, &val, sizeof (val));
17300 total_len += obstack_object_size (&symtab_obstack);
17301
17302 /* The offset of the constant pool from the start of the file. */
17303 val = MAYBE_SWAP (total_len);
17304 obstack_grow (&contents, &val, sizeof (val));
17305 total_len += obstack_object_size (&constant_pool);
17306
17307 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17308
17309 write_obstack (out_file, &contents);
17310 write_obstack (out_file, &cu_list);
17311 write_obstack (out_file, &types_cu_list);
17312 write_obstack (out_file, &addr_obstack);
17313 write_obstack (out_file, &symtab_obstack);
17314 write_obstack (out_file, &constant_pool);
17315
17316 fclose (out_file);
17317
17318 /* We want to keep the file, so we set cleanup_filename to NULL
17319 here. See unlink_if_set. */
17320 cleanup_filename = NULL;
17321
17322 do_cleanups (cleanup);
17323 }
17324
17325 /* Implementation of the `save gdb-index' command.
17326
17327 Note that the file format used by this command is documented in the
17328 GDB manual. Any changes here must be documented there. */
17329
17330 static void
17331 save_gdb_index_command (char *arg, int from_tty)
17332 {
17333 struct objfile *objfile;
17334
17335 if (!arg || !*arg)
17336 error (_("usage: save gdb-index DIRECTORY"));
17337
17338 ALL_OBJFILES (objfile)
17339 {
17340 struct stat st;
17341
17342 /* If the objfile does not correspond to an actual file, skip it. */
17343 if (stat (objfile->name, &st) < 0)
17344 continue;
17345
17346 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17347 if (dwarf2_per_objfile)
17348 {
17349 volatile struct gdb_exception except;
17350
17351 TRY_CATCH (except, RETURN_MASK_ERROR)
17352 {
17353 write_psymtabs_to_index (objfile, arg);
17354 }
17355 if (except.reason < 0)
17356 exception_fprintf (gdb_stderr, except,
17357 _("Error while writing index for `%s': "),
17358 objfile->name);
17359 }
17360 }
17361 }
17362
17363 \f
17364
17365 int dwarf2_always_disassemble;
17366
17367 static void
17368 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17369 struct cmd_list_element *c, const char *value)
17370 {
17371 fprintf_filtered (file,
17372 _("Whether to always disassemble "
17373 "DWARF expressions is %s.\n"),
17374 value);
17375 }
17376
17377 static void
17378 show_check_physname (struct ui_file *file, int from_tty,
17379 struct cmd_list_element *c, const char *value)
17380 {
17381 fprintf_filtered (file,
17382 _("Whether to check \"physname\" is %s.\n"),
17383 value);
17384 }
17385
17386 void _initialize_dwarf2_read (void);
17387
17388 void
17389 _initialize_dwarf2_read (void)
17390 {
17391 struct cmd_list_element *c;
17392
17393 dwarf2_objfile_data_key
17394 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17395
17396 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17397 Set DWARF 2 specific variables.\n\
17398 Configure DWARF 2 variables such as the cache size"),
17399 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17400 0/*allow-unknown*/, &maintenance_set_cmdlist);
17401
17402 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17403 Show DWARF 2 specific variables\n\
17404 Show DWARF 2 variables such as the cache size"),
17405 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17406 0/*allow-unknown*/, &maintenance_show_cmdlist);
17407
17408 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17409 &dwarf2_max_cache_age, _("\
17410 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17411 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17412 A higher limit means that cached compilation units will be stored\n\
17413 in memory longer, and more total memory will be used. Zero disables\n\
17414 caching, which can slow down startup."),
17415 NULL,
17416 show_dwarf2_max_cache_age,
17417 &set_dwarf2_cmdlist,
17418 &show_dwarf2_cmdlist);
17419
17420 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17421 &dwarf2_always_disassemble, _("\
17422 Set whether `info address' always disassembles DWARF expressions."), _("\
17423 Show whether `info address' always disassembles DWARF expressions."), _("\
17424 When enabled, DWARF expressions are always printed in an assembly-like\n\
17425 syntax. When disabled, expressions will be printed in a more\n\
17426 conversational style, when possible."),
17427 NULL,
17428 show_dwarf2_always_disassemble,
17429 &set_dwarf2_cmdlist,
17430 &show_dwarf2_cmdlist);
17431
17432 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17433 Set debugging of the dwarf2 DIE reader."), _("\
17434 Show debugging of the dwarf2 DIE reader."), _("\
17435 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17436 The value is the maximum depth to print."),
17437 NULL,
17438 NULL,
17439 &setdebuglist, &showdebuglist);
17440
17441 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17442 Set cross-checking of \"physname\" code against demangler."), _("\
17443 Show cross-checking of \"physname\" code against demangler."), _("\
17444 When enabled, GDB's internal \"physname\" code is checked against\n\
17445 the demangler."),
17446 NULL, show_check_physname,
17447 &setdebuglist, &showdebuglist);
17448
17449 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17450 _("\
17451 Save a gdb-index file.\n\
17452 Usage: save gdb-index DIRECTORY"),
17453 &save_cmdlist);
17454 set_cmd_completer (c, filename_completer);
17455 }
This page took 0.596599 seconds and 4 git commands to generate.