* dwarf2read.c (load_partial_comp_unit): Defer adding cu to
[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 "valprint.h"
63 #include <ctype.h>
64
65 #include <fcntl.h>
66 #include "gdb_string.h"
67 #include "gdb_assert.h"
68 #include <sys/types.h>
69 #ifdef HAVE_ZLIB_H
70 #include <zlib.h>
71 #endif
72 #ifdef HAVE_MMAP
73 #include <sys/mman.h>
74 #ifndef MAP_FAILED
75 #define MAP_FAILED ((void *) -1)
76 #endif
77 #endif
78
79 typedef struct symbol *symbolp;
80 DEF_VEC_P (symbolp);
81
82 /* When non-zero, dump DIEs after they are read in. */
83 static int dwarf2_die_debug = 0;
84
85 /* When non-zero, cross-check physname against demangler. */
86 static int check_physname = 0;
87
88 static int pagesize;
89
90 /* When set, the file that we're processing is known to have debugging
91 info for C++ namespaces. GCC 3.3.x did not produce this information,
92 but later versions do. */
93
94 static int processing_has_namespace_info;
95
96 static const struct objfile_data *dwarf2_objfile_data_key;
97
98 struct dwarf2_section_info
99 {
100 asection *asection;
101 gdb_byte *buffer;
102 bfd_size_type size;
103 /* Not NULL if the section was actually mmapped. */
104 void *map_addr;
105 /* Page aligned size of mmapped area. */
106 bfd_size_type map_len;
107 /* True if we have tried to read this section. */
108 int readin;
109 };
110
111 typedef struct dwarf2_section_info dwarf2_section_info_def;
112 DEF_VEC_O (dwarf2_section_info_def);
113
114 /* All offsets in the index are of this type. It must be
115 architecture-independent. */
116 typedef uint32_t offset_type;
117
118 DEF_VEC_I (offset_type);
119
120 /* A description of the mapped index. The file format is described in
121 a comment by the code that writes the index. */
122 struct mapped_index
123 {
124 /* Index data format version. */
125 int version;
126
127 /* The total length of the buffer. */
128 off_t total_size;
129
130 /* A pointer to the address table data. */
131 const gdb_byte *address_table;
132
133 /* Size of the address table data in bytes. */
134 offset_type address_table_size;
135
136 /* The symbol table, implemented as a hash table. */
137 const offset_type *symbol_table;
138
139 /* Size in slots, each slot is 2 offset_types. */
140 offset_type symbol_table_slots;
141
142 /* A pointer to the constant pool. */
143 const char *constant_pool;
144 };
145
146 /* Collection of data recorded per objfile.
147 This hangs off of dwarf2_objfile_data_key. */
148
149 struct dwarf2_per_objfile
150 {
151 struct dwarf2_section_info info;
152 struct dwarf2_section_info abbrev;
153 struct dwarf2_section_info line;
154 struct dwarf2_section_info loc;
155 struct dwarf2_section_info macinfo;
156 struct dwarf2_section_info macro;
157 struct dwarf2_section_info str;
158 struct dwarf2_section_info ranges;
159 struct dwarf2_section_info frame;
160 struct dwarf2_section_info eh_frame;
161 struct dwarf2_section_info gdb_index;
162
163 VEC (dwarf2_section_info_def) *types;
164
165 /* Back link. */
166 struct objfile *objfile;
167
168 /* Table of all the compilation units. This is used to locate
169 the target compilation unit of a particular reference. */
170 struct dwarf2_per_cu_data **all_comp_units;
171
172 /* The number of compilation units in ALL_COMP_UNITS. */
173 int n_comp_units;
174
175 /* The number of .debug_types-related CUs. */
176 int n_type_units;
177
178 /* The .debug_types-related CUs (TUs). */
179 struct dwarf2_per_cu_data **all_type_units;
180
181 /* A chain of compilation units that are currently read in, so that
182 they can be freed later. */
183 struct dwarf2_per_cu_data *read_in_chain;
184
185 /* A table mapping .debug_types signatures to its signatured_type entry.
186 This is NULL if the .debug_types section hasn't been read in yet. */
187 htab_t signatured_types;
188
189 /* A flag indicating wether this objfile has a section loaded at a
190 VMA of 0. */
191 int has_section_at_zero;
192
193 /* True if we are using the mapped index,
194 or we are faking it for OBJF_READNOW's sake. */
195 unsigned char using_index;
196
197 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
198 struct mapped_index *index_table;
199
200 /* When using index_table, this keeps track of all quick_file_names entries.
201 TUs can share line table entries with CUs or other TUs, and there can be
202 a lot more TUs than unique line tables, so we maintain a separate table
203 of all line table entries to support the sharing. */
204 htab_t quick_file_names_table;
205
206 /* Set during partial symbol reading, to prevent queueing of full
207 symbols. */
208 int reading_partial_symbols;
209
210 /* Table mapping type .debug_info DIE offsets to types.
211 This is NULL if not allocated yet.
212 It (currently) makes sense to allocate debug_types_type_hash lazily.
213 To keep things simple we allocate both lazily. */
214 htab_t debug_info_type_hash;
215
216 /* Table mapping type .debug_types DIE offsets to types.
217 This is NULL if not allocated yet. */
218 htab_t debug_types_type_hash;
219 };
220
221 static struct dwarf2_per_objfile *dwarf2_per_objfile;
222
223 /* Default names of the debugging sections. */
224
225 /* Note that if the debugging section has been compressed, it might
226 have a name like .zdebug_info. */
227
228 static const struct dwarf2_debug_sections dwarf2_elf_names =
229 {
230 { ".debug_info", ".zdebug_info" },
231 { ".debug_abbrev", ".zdebug_abbrev" },
232 { ".debug_line", ".zdebug_line" },
233 { ".debug_loc", ".zdebug_loc" },
234 { ".debug_macinfo", ".zdebug_macinfo" },
235 { ".debug_macro", ".zdebug_macro" },
236 { ".debug_str", ".zdebug_str" },
237 { ".debug_ranges", ".zdebug_ranges" },
238 { ".debug_types", ".zdebug_types" },
239 { ".debug_frame", ".zdebug_frame" },
240 { ".eh_frame", NULL },
241 { ".gdb_index", ".zgdb_index" },
242 23
243 };
244
245 /* local data types */
246
247 /* We hold several abbreviation tables in memory at the same time. */
248 #ifndef ABBREV_HASH_SIZE
249 #define ABBREV_HASH_SIZE 121
250 #endif
251
252 /* The data in a compilation unit header, after target2host
253 translation, looks like this. */
254 struct comp_unit_head
255 {
256 unsigned int length;
257 short version;
258 unsigned char addr_size;
259 unsigned char signed_addr_p;
260 unsigned int abbrev_offset;
261
262 /* Size of file offsets; either 4 or 8. */
263 unsigned int offset_size;
264
265 /* Size of the length field; either 4 or 12. */
266 unsigned int initial_length_size;
267
268 /* Offset to the first byte of this compilation unit header in the
269 .debug_info section, for resolving relative reference dies. */
270 unsigned int offset;
271
272 /* Offset to first die in this cu from the start of the cu.
273 This will be the first byte following the compilation unit header. */
274 unsigned int first_die_offset;
275 };
276
277 /* Type used for delaying computation of method physnames.
278 See comments for compute_delayed_physnames. */
279 struct delayed_method_info
280 {
281 /* The type to which the method is attached, i.e., its parent class. */
282 struct type *type;
283
284 /* The index of the method in the type's function fieldlists. */
285 int fnfield_index;
286
287 /* The index of the method in the fieldlist. */
288 int index;
289
290 /* The name of the DIE. */
291 const char *name;
292
293 /* The DIE associated with this method. */
294 struct die_info *die;
295 };
296
297 typedef struct delayed_method_info delayed_method_info;
298 DEF_VEC_O (delayed_method_info);
299
300 /* Internal state when decoding a particular compilation unit. */
301 struct dwarf2_cu
302 {
303 /* The objfile containing this compilation unit. */
304 struct objfile *objfile;
305
306 /* The header of the compilation unit. */
307 struct comp_unit_head header;
308
309 /* Base address of this compilation unit. */
310 CORE_ADDR base_address;
311
312 /* Non-zero if base_address has been set. */
313 int base_known;
314
315 /* The language we are debugging. */
316 enum language language;
317 const struct language_defn *language_defn;
318
319 const char *producer;
320
321 /* The generic symbol table building routines have separate lists for
322 file scope symbols and all all other scopes (local scopes). So
323 we need to select the right one to pass to add_symbol_to_list().
324 We do it by keeping a pointer to the correct list in list_in_scope.
325
326 FIXME: The original dwarf code just treated the file scope as the
327 first local scope, and all other local scopes as nested local
328 scopes, and worked fine. Check to see if we really need to
329 distinguish these in buildsym.c. */
330 struct pending **list_in_scope;
331
332 /* DWARF abbreviation table associated with this compilation unit. */
333 struct abbrev_info **dwarf2_abbrevs;
334
335 /* Storage for the abbrev table. */
336 struct obstack abbrev_obstack;
337
338 /* Hash table holding all the loaded partial DIEs. */
339 htab_t partial_dies;
340
341 /* Storage for things with the same lifetime as this read-in compilation
342 unit, including partial DIEs. */
343 struct obstack comp_unit_obstack;
344
345 /* When multiple dwarf2_cu structures are living in memory, this field
346 chains them all together, so that they can be released efficiently.
347 We will probably also want a generation counter so that most-recently-used
348 compilation units are cached... */
349 struct dwarf2_per_cu_data *read_in_chain;
350
351 /* Backchain to our per_cu entry if the tree has been built. */
352 struct dwarf2_per_cu_data *per_cu;
353
354 /* How many compilation units ago was this CU last referenced? */
355 int last_used;
356
357 /* A hash table of die offsets for following references. */
358 htab_t die_hash;
359
360 /* Full DIEs if read in. */
361 struct die_info *dies;
362
363 /* A set of pointers to dwarf2_per_cu_data objects for compilation
364 units referenced by this one. Only set during full symbol processing;
365 partial symbol tables do not have dependencies. */
366 htab_t dependencies;
367
368 /* Header data from the line table, during full symbol processing. */
369 struct line_header *line_header;
370
371 /* A list of methods which need to have physnames computed
372 after all type information has been read. */
373 VEC (delayed_method_info) *method_list;
374
375 /* To be copied to symtab->call_site_htab. */
376 htab_t call_site_htab;
377
378 /* Mark used when releasing cached dies. */
379 unsigned int mark : 1;
380
381 /* This CU references .debug_loc. See the symtab->locations_valid field.
382 This test is imperfect as there may exist optimized debug code not using
383 any location list and still facing inlining issues if handled as
384 unoptimized code. For a future better test see GCC PR other/32998. */
385 unsigned int has_loclist : 1;
386
387 /* These cache the results of producer_is_gxx_lt_4_6.
388 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
389 information is cached because profiling CU expansion showed
390 excessive time spent in producer_is_gxx_lt_4_6. */
391 unsigned int checked_producer : 1;
392 unsigned int producer_is_gxx_lt_4_6 : 1;
393 };
394
395 /* Persistent data held for a compilation unit, even when not
396 processing it. We put a pointer to this structure in the
397 read_symtab_private field of the psymtab. */
398
399 struct dwarf2_per_cu_data
400 {
401 /* The start offset and length of this compilation unit. 2**29-1
402 bytes should suffice to store the length of any compilation unit
403 - if it doesn't, GDB will fall over anyway.
404 NOTE: Unlike comp_unit_head.length, this length includes
405 initial_length_size. */
406 unsigned int offset;
407 unsigned int length : 29;
408
409 /* Flag indicating this compilation unit will be read in before
410 any of the current compilation units are processed. */
411 unsigned int queued : 1;
412
413 /* This flag will be set if we need to load absolutely all DIEs
414 for this compilation unit, instead of just the ones we think
415 are interesting. It gets set if we look for a DIE in the
416 hash table and don't find it. */
417 unsigned int load_all_dies : 1;
418
419 /* Non-null if this CU is from .debug_types; in which case it points
420 to the section. Otherwise it's from .debug_info. */
421 struct dwarf2_section_info *debug_types_section;
422
423 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
424 of the CU cache it gets reset to NULL again. */
425 struct dwarf2_cu *cu;
426
427 /* The corresponding objfile.
428 Normally we can get the objfile from dwarf2_per_objfile.
429 However we can enter this file with just a "per_cu" handle. */
430 struct objfile *objfile;
431
432 /* When using partial symbol tables, the 'psymtab' field is active.
433 Otherwise the 'quick' field is active. */
434 union
435 {
436 /* The partial symbol table associated with this compilation unit,
437 or NULL for partial units (which do not have an associated
438 symtab). */
439 struct partial_symtab *psymtab;
440
441 /* Data needed by the "quick" functions. */
442 struct dwarf2_per_cu_quick_data *quick;
443 } v;
444 };
445
446 /* Entry in the signatured_types hash table. */
447
448 struct signatured_type
449 {
450 ULONGEST signature;
451
452 /* Offset in .debug_types of the type defined by this TU. */
453 unsigned int type_offset;
454
455 /* The CU(/TU) of this type. */
456 struct dwarf2_per_cu_data per_cu;
457 };
458
459 /* Struct used to pass misc. parameters to read_die_and_children, et
460 al. which are used for both .debug_info and .debug_types dies.
461 All parameters here are unchanging for the life of the call. This
462 struct exists to abstract away the constant parameters of die
463 reading. */
464
465 struct die_reader_specs
466 {
467 /* The bfd of this objfile. */
468 bfd* abfd;
469
470 /* The CU of the DIE we are parsing. */
471 struct dwarf2_cu *cu;
472
473 /* Pointer to start of section buffer.
474 This is either the start of .debug_info or .debug_types. */
475 const gdb_byte *buffer;
476 };
477
478 /* The line number information for a compilation unit (found in the
479 .debug_line section) begins with a "statement program header",
480 which contains the following information. */
481 struct line_header
482 {
483 unsigned int total_length;
484 unsigned short version;
485 unsigned int header_length;
486 unsigned char minimum_instruction_length;
487 unsigned char maximum_ops_per_instruction;
488 unsigned char default_is_stmt;
489 int line_base;
490 unsigned char line_range;
491 unsigned char opcode_base;
492
493 /* standard_opcode_lengths[i] is the number of operands for the
494 standard opcode whose value is i. This means that
495 standard_opcode_lengths[0] is unused, and the last meaningful
496 element is standard_opcode_lengths[opcode_base - 1]. */
497 unsigned char *standard_opcode_lengths;
498
499 /* The include_directories table. NOTE! These strings are not
500 allocated with xmalloc; instead, they are pointers into
501 debug_line_buffer. If you try to free them, `free' will get
502 indigestion. */
503 unsigned int num_include_dirs, include_dirs_size;
504 char **include_dirs;
505
506 /* The file_names table. NOTE! These strings are not allocated
507 with xmalloc; instead, they are pointers into debug_line_buffer.
508 Don't try to free them directly. */
509 unsigned int num_file_names, file_names_size;
510 struct file_entry
511 {
512 char *name;
513 unsigned int dir_index;
514 unsigned int mod_time;
515 unsigned int length;
516 int included_p; /* Non-zero if referenced by the Line Number Program. */
517 struct symtab *symtab; /* The associated symbol table, if any. */
518 } *file_names;
519
520 /* The start and end of the statement program following this
521 header. These point into dwarf2_per_objfile->line_buffer. */
522 gdb_byte *statement_program_start, *statement_program_end;
523 };
524
525 /* When we construct a partial symbol table entry we only
526 need this much information. */
527 struct partial_die_info
528 {
529 /* Offset of this DIE. */
530 unsigned int offset;
531
532 /* DWARF-2 tag for this DIE. */
533 ENUM_BITFIELD(dwarf_tag) tag : 16;
534
535 /* Assorted flags describing the data found in this DIE. */
536 unsigned int has_children : 1;
537 unsigned int is_external : 1;
538 unsigned int is_declaration : 1;
539 unsigned int has_type : 1;
540 unsigned int has_specification : 1;
541 unsigned int has_pc_info : 1;
542
543 /* Flag set if the SCOPE field of this structure has been
544 computed. */
545 unsigned int scope_set : 1;
546
547 /* Flag set if the DIE has a byte_size attribute. */
548 unsigned int has_byte_size : 1;
549
550 /* Flag set if any of the DIE's children are template arguments. */
551 unsigned int has_template_arguments : 1;
552
553 /* Flag set if fixup_partial_die has been called on this die. */
554 unsigned int fixup_called : 1;
555
556 /* The name of this DIE. Normally the value of DW_AT_name, but
557 sometimes a default name for unnamed DIEs. */
558 char *name;
559
560 /* The linkage name, if present. */
561 const char *linkage_name;
562
563 /* The scope to prepend to our children. This is generally
564 allocated on the comp_unit_obstack, so will disappear
565 when this compilation unit leaves the cache. */
566 char *scope;
567
568 /* The location description associated with this DIE, if any. */
569 struct dwarf_block *locdesc;
570
571 /* If HAS_PC_INFO, the PC range associated with this DIE. */
572 CORE_ADDR lowpc;
573 CORE_ADDR highpc;
574
575 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
576 DW_AT_sibling, if any. */
577 /* NOTE: This member isn't strictly necessary, read_partial_die could
578 return DW_AT_sibling values to its caller load_partial_dies. */
579 gdb_byte *sibling;
580
581 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
582 DW_AT_specification (or DW_AT_abstract_origin or
583 DW_AT_extension). */
584 unsigned int spec_offset;
585
586 /* Pointers to this DIE's parent, first child, and next sibling,
587 if any. */
588 struct partial_die_info *die_parent, *die_child, *die_sibling;
589 };
590
591 /* This data structure holds the information of an abbrev. */
592 struct abbrev_info
593 {
594 unsigned int number; /* number identifying abbrev */
595 enum dwarf_tag tag; /* dwarf tag */
596 unsigned short has_children; /* boolean */
597 unsigned short num_attrs; /* number of attributes */
598 struct attr_abbrev *attrs; /* an array of attribute descriptions */
599 struct abbrev_info *next; /* next in chain */
600 };
601
602 struct attr_abbrev
603 {
604 ENUM_BITFIELD(dwarf_attribute) name : 16;
605 ENUM_BITFIELD(dwarf_form) form : 16;
606 };
607
608 /* Attributes have a name and a value. */
609 struct attribute
610 {
611 ENUM_BITFIELD(dwarf_attribute) name : 16;
612 ENUM_BITFIELD(dwarf_form) form : 15;
613
614 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
615 field should be in u.str (existing only for DW_STRING) but it is kept
616 here for better struct attribute alignment. */
617 unsigned int string_is_canonical : 1;
618
619 union
620 {
621 char *str;
622 struct dwarf_block *blk;
623 ULONGEST unsnd;
624 LONGEST snd;
625 CORE_ADDR addr;
626 struct signatured_type *signatured_type;
627 }
628 u;
629 };
630
631 /* This data structure holds a complete die structure. */
632 struct die_info
633 {
634 /* DWARF-2 tag for this DIE. */
635 ENUM_BITFIELD(dwarf_tag) tag : 16;
636
637 /* Number of attributes */
638 unsigned char num_attrs;
639
640 /* True if we're presently building the full type name for the
641 type derived from this DIE. */
642 unsigned char building_fullname : 1;
643
644 /* Abbrev number */
645 unsigned int abbrev;
646
647 /* Offset in .debug_info or .debug_types section. */
648 unsigned int offset;
649
650 /* The dies in a compilation unit form an n-ary tree. PARENT
651 points to this die's parent; CHILD points to the first child of
652 this node; and all the children of a given node are chained
653 together via their SIBLING fields. */
654 struct die_info *child; /* Its first child, if any. */
655 struct die_info *sibling; /* Its next sibling, if any. */
656 struct die_info *parent; /* Its parent, if any. */
657
658 /* An array of attributes, with NUM_ATTRS elements. There may be
659 zero, but it's not common and zero-sized arrays are not
660 sufficiently portable C. */
661 struct attribute attrs[1];
662 };
663
664 /* Get at parts of an attribute structure. */
665
666 #define DW_STRING(attr) ((attr)->u.str)
667 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
668 #define DW_UNSND(attr) ((attr)->u.unsnd)
669 #define DW_BLOCK(attr) ((attr)->u.blk)
670 #define DW_SND(attr) ((attr)->u.snd)
671 #define DW_ADDR(attr) ((attr)->u.addr)
672 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
673
674 /* Blocks are a bunch of untyped bytes. */
675 struct dwarf_block
676 {
677 unsigned int size;
678
679 /* Valid only if SIZE is not zero. */
680 gdb_byte *data;
681 };
682
683 #ifndef ATTR_ALLOC_CHUNK
684 #define ATTR_ALLOC_CHUNK 4
685 #endif
686
687 /* Allocate fields for structs, unions and enums in this size. */
688 #ifndef DW_FIELD_ALLOC_CHUNK
689 #define DW_FIELD_ALLOC_CHUNK 4
690 #endif
691
692 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
693 but this would require a corresponding change in unpack_field_as_long
694 and friends. */
695 static int bits_per_byte = 8;
696
697 /* The routines that read and process dies for a C struct or C++ class
698 pass lists of data member fields and lists of member function fields
699 in an instance of a field_info structure, as defined below. */
700 struct field_info
701 {
702 /* List of data member and baseclasses fields. */
703 struct nextfield
704 {
705 struct nextfield *next;
706 int accessibility;
707 int virtuality;
708 struct field field;
709 }
710 *fields, *baseclasses;
711
712 /* Number of fields (including baseclasses). */
713 int nfields;
714
715 /* Number of baseclasses. */
716 int nbaseclasses;
717
718 /* Set if the accesibility of one of the fields is not public. */
719 int non_public_fields;
720
721 /* Member function fields array, entries are allocated in the order they
722 are encountered in the object file. */
723 struct nextfnfield
724 {
725 struct nextfnfield *next;
726 struct fn_field fnfield;
727 }
728 *fnfields;
729
730 /* Member function fieldlist array, contains name of possibly overloaded
731 member function, number of overloaded member functions and a pointer
732 to the head of the member function field chain. */
733 struct fnfieldlist
734 {
735 char *name;
736 int length;
737 struct nextfnfield *head;
738 }
739 *fnfieldlists;
740
741 /* Number of entries in the fnfieldlists array. */
742 int nfnfields;
743
744 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
745 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
746 struct typedef_field_list
747 {
748 struct typedef_field field;
749 struct typedef_field_list *next;
750 }
751 *typedef_field_list;
752 unsigned typedef_field_list_count;
753 };
754
755 /* One item on the queue of compilation units to read in full symbols
756 for. */
757 struct dwarf2_queue_item
758 {
759 struct dwarf2_per_cu_data *per_cu;
760 struct dwarf2_queue_item *next;
761 };
762
763 /* The current queue. */
764 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
765
766 /* Loaded secondary compilation units are kept in memory until they
767 have not been referenced for the processing of this many
768 compilation units. Set this to zero to disable caching. Cache
769 sizes of up to at least twenty will improve startup time for
770 typical inter-CU-reference binaries, at an obvious memory cost. */
771 static int dwarf2_max_cache_age = 5;
772 static void
773 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
774 struct cmd_list_element *c, const char *value)
775 {
776 fprintf_filtered (file, _("The upper bound on the age of cached "
777 "dwarf2 compilation units is %s.\n"),
778 value);
779 }
780
781
782 /* Various complaints about symbol reading that don't abort the process. */
783
784 static void
785 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
786 {
787 complaint (&symfile_complaints,
788 _("statement list doesn't fit in .debug_line section"));
789 }
790
791 static void
792 dwarf2_debug_line_missing_file_complaint (void)
793 {
794 complaint (&symfile_complaints,
795 _(".debug_line section has line data without a file"));
796 }
797
798 static void
799 dwarf2_debug_line_missing_end_sequence_complaint (void)
800 {
801 complaint (&symfile_complaints,
802 _(".debug_line section has line "
803 "program sequence without an end"));
804 }
805
806 static void
807 dwarf2_complex_location_expr_complaint (void)
808 {
809 complaint (&symfile_complaints, _("location expression too complex"));
810 }
811
812 static void
813 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
814 int arg3)
815 {
816 complaint (&symfile_complaints,
817 _("const value length mismatch for '%s', got %d, expected %d"),
818 arg1, arg2, arg3);
819 }
820
821 static void
822 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
823 {
824 complaint (&symfile_complaints,
825 _("macro info runs off end of `%s' section"),
826 section->asection->name);
827 }
828
829 static void
830 dwarf2_macro_malformed_definition_complaint (const char *arg1)
831 {
832 complaint (&symfile_complaints,
833 _("macro debug info contains a "
834 "malformed macro definition:\n`%s'"),
835 arg1);
836 }
837
838 static void
839 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
840 {
841 complaint (&symfile_complaints,
842 _("invalid attribute class or form for '%s' in '%s'"),
843 arg1, arg2);
844 }
845
846 /* local function prototypes */
847
848 static void dwarf2_locate_sections (bfd *, asection *, void *);
849
850 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
851 struct objfile *);
852
853 static void dwarf2_find_base_address (struct die_info *die,
854 struct dwarf2_cu *cu);
855
856 static void dwarf2_build_psymtabs_hard (struct objfile *);
857
858 static void scan_partial_symbols (struct partial_die_info *,
859 CORE_ADDR *, CORE_ADDR *,
860 int, struct dwarf2_cu *);
861
862 static void add_partial_symbol (struct partial_die_info *,
863 struct dwarf2_cu *);
864
865 static void add_partial_namespace (struct partial_die_info *pdi,
866 CORE_ADDR *lowpc, CORE_ADDR *highpc,
867 int need_pc, struct dwarf2_cu *cu);
868
869 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
870 CORE_ADDR *highpc, int need_pc,
871 struct dwarf2_cu *cu);
872
873 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
874 struct dwarf2_cu *cu);
875
876 static void add_partial_subprogram (struct partial_die_info *pdi,
877 CORE_ADDR *lowpc, CORE_ADDR *highpc,
878 int need_pc, struct dwarf2_cu *cu);
879
880 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
881 gdb_byte *buffer, gdb_byte *info_ptr,
882 bfd *abfd, struct dwarf2_cu *cu);
883
884 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
885
886 static void psymtab_to_symtab_1 (struct partial_symtab *);
887
888 static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
889
890 static void dwarf2_free_abbrev_table (void *);
891
892 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
893
894 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
895 struct dwarf2_cu *);
896
897 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
898 struct dwarf2_cu *);
899
900 static struct partial_die_info *load_partial_dies (bfd *,
901 gdb_byte *, gdb_byte *,
902 int, struct dwarf2_cu *);
903
904 static gdb_byte *read_partial_die (struct partial_die_info *,
905 struct abbrev_info *abbrev,
906 unsigned int, bfd *,
907 gdb_byte *, gdb_byte *,
908 struct dwarf2_cu *);
909
910 static struct partial_die_info *find_partial_die (unsigned int,
911 struct dwarf2_cu *);
912
913 static void fixup_partial_die (struct partial_die_info *,
914 struct dwarf2_cu *);
915
916 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
917 bfd *, gdb_byte *, struct dwarf2_cu *);
918
919 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
920 bfd *, gdb_byte *, struct dwarf2_cu *);
921
922 static unsigned int read_1_byte (bfd *, gdb_byte *);
923
924 static int read_1_signed_byte (bfd *, gdb_byte *);
925
926 static unsigned int read_2_bytes (bfd *, gdb_byte *);
927
928 static unsigned int read_4_bytes (bfd *, gdb_byte *);
929
930 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
931
932 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
933 unsigned int *);
934
935 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
936
937 static LONGEST read_checked_initial_length_and_offset
938 (bfd *, gdb_byte *, const struct comp_unit_head *,
939 unsigned int *, unsigned int *);
940
941 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
942 unsigned int *);
943
944 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
945
946 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
947
948 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
949
950 static char *read_indirect_string (bfd *, gdb_byte *,
951 const struct comp_unit_head *,
952 unsigned int *);
953
954 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
955
956 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
957
958 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
959
960 static void set_cu_language (unsigned int, struct dwarf2_cu *);
961
962 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
963 struct dwarf2_cu *);
964
965 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
966 unsigned int,
967 struct dwarf2_cu *);
968
969 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
970 struct dwarf2_cu *cu);
971
972 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
973
974 static struct die_info *die_specification (struct die_info *die,
975 struct dwarf2_cu **);
976
977 static void free_line_header (struct line_header *lh);
978
979 static void add_file_name (struct line_header *, char *, unsigned int,
980 unsigned int, unsigned int);
981
982 static struct line_header *(dwarf_decode_line_header
983 (unsigned int offset,
984 bfd *abfd, struct dwarf2_cu *cu));
985
986 static void dwarf_decode_lines (struct line_header *, const char *,
987 struct dwarf2_cu *, struct partial_symtab *,
988 int);
989
990 static void dwarf2_start_subfile (char *, const char *, const char *);
991
992 static struct symbol *new_symbol (struct die_info *, struct type *,
993 struct dwarf2_cu *);
994
995 static struct symbol *new_symbol_full (struct die_info *, struct type *,
996 struct dwarf2_cu *, struct symbol *);
997
998 static void dwarf2_const_value (struct attribute *, struct symbol *,
999 struct dwarf2_cu *);
1000
1001 static void dwarf2_const_value_attr (struct attribute *attr,
1002 struct type *type,
1003 const char *name,
1004 struct obstack *obstack,
1005 struct dwarf2_cu *cu, long *value,
1006 gdb_byte **bytes,
1007 struct dwarf2_locexpr_baton **baton);
1008
1009 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1010
1011 static int need_gnat_info (struct dwarf2_cu *);
1012
1013 static struct type *die_descriptive_type (struct die_info *,
1014 struct dwarf2_cu *);
1015
1016 static void set_descriptive_type (struct type *, struct die_info *,
1017 struct dwarf2_cu *);
1018
1019 static struct type *die_containing_type (struct die_info *,
1020 struct dwarf2_cu *);
1021
1022 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1023 struct dwarf2_cu *);
1024
1025 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1026
1027 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1028
1029 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1030
1031 static char *typename_concat (struct obstack *obs, const char *prefix,
1032 const char *suffix, int physname,
1033 struct dwarf2_cu *cu);
1034
1035 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1036
1037 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1038
1039 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1040
1041 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1042
1043 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1044
1045 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1046 struct dwarf2_cu *, struct partial_symtab *);
1047
1048 static int dwarf2_get_pc_bounds (struct die_info *,
1049 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1050 struct partial_symtab *);
1051
1052 static void get_scope_pc_bounds (struct die_info *,
1053 CORE_ADDR *, CORE_ADDR *,
1054 struct dwarf2_cu *);
1055
1056 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1057 CORE_ADDR, struct dwarf2_cu *);
1058
1059 static void dwarf2_add_field (struct field_info *, struct die_info *,
1060 struct dwarf2_cu *);
1061
1062 static void dwarf2_attach_fields_to_type (struct field_info *,
1063 struct type *, struct dwarf2_cu *);
1064
1065 static void dwarf2_add_member_fn (struct field_info *,
1066 struct die_info *, struct type *,
1067 struct dwarf2_cu *);
1068
1069 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1070 struct type *,
1071 struct dwarf2_cu *);
1072
1073 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1074
1075 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1076
1077 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1078
1079 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1080
1081 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1082
1083 static struct type *read_module_type (struct die_info *die,
1084 struct dwarf2_cu *cu);
1085
1086 static const char *namespace_name (struct die_info *die,
1087 int *is_anonymous, struct dwarf2_cu *);
1088
1089 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1090
1091 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1092
1093 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1094 struct dwarf2_cu *);
1095
1096 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1097
1098 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1099 gdb_byte *info_ptr,
1100 gdb_byte **new_info_ptr,
1101 struct die_info *parent);
1102
1103 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1104 gdb_byte *info_ptr,
1105 gdb_byte **new_info_ptr,
1106 struct die_info *parent);
1107
1108 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1109 gdb_byte *info_ptr,
1110 gdb_byte **new_info_ptr,
1111 struct die_info *parent);
1112
1113 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1114 struct die_info **, gdb_byte *,
1115 int *);
1116
1117 static void process_die (struct die_info *, struct dwarf2_cu *);
1118
1119 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1120 struct obstack *);
1121
1122 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1123
1124 static const char *dwarf2_full_name (char *name,
1125 struct die_info *die,
1126 struct dwarf2_cu *cu);
1127
1128 static struct die_info *dwarf2_extension (struct die_info *die,
1129 struct dwarf2_cu **);
1130
1131 static char *dwarf_tag_name (unsigned int);
1132
1133 static char *dwarf_attr_name (unsigned int);
1134
1135 static char *dwarf_form_name (unsigned int);
1136
1137 static char *dwarf_bool_name (unsigned int);
1138
1139 static char *dwarf_type_encoding_name (unsigned int);
1140
1141 #if 0
1142 static char *dwarf_cfi_name (unsigned int);
1143 #endif
1144
1145 static struct die_info *sibling_die (struct die_info *);
1146
1147 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1148
1149 static void dump_die_for_error (struct die_info *);
1150
1151 static void dump_die_1 (struct ui_file *, int level, int max_level,
1152 struct die_info *);
1153
1154 /*static*/ void dump_die (struct die_info *, int max_level);
1155
1156 static void store_in_ref_table (struct die_info *,
1157 struct dwarf2_cu *);
1158
1159 static int is_ref_attr (struct attribute *);
1160
1161 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1162
1163 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1164
1165 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1166 struct attribute *,
1167 struct dwarf2_cu **);
1168
1169 static struct die_info *follow_die_ref (struct die_info *,
1170 struct attribute *,
1171 struct dwarf2_cu **);
1172
1173 static struct die_info *follow_die_sig (struct die_info *,
1174 struct attribute *,
1175 struct dwarf2_cu **);
1176
1177 static struct signatured_type *lookup_signatured_type_at_offset
1178 (struct objfile *objfile,
1179 struct dwarf2_section_info *section,
1180 unsigned int offset);
1181
1182 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1183
1184 static void read_signatured_type (struct signatured_type *type_sig);
1185
1186 /* memory allocation interface */
1187
1188 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1189
1190 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1191
1192 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1193
1194 static void dwarf_decode_macros (struct line_header *, unsigned int,
1195 char *, bfd *, struct dwarf2_cu *,
1196 struct dwarf2_section_info *,
1197 int);
1198
1199 static int attr_form_is_block (struct attribute *);
1200
1201 static int attr_form_is_section_offset (struct attribute *);
1202
1203 static int attr_form_is_constant (struct attribute *);
1204
1205 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1206 struct dwarf2_loclist_baton *baton,
1207 struct attribute *attr);
1208
1209 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1210 struct symbol *sym,
1211 struct dwarf2_cu *cu);
1212
1213 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1214 struct abbrev_info *abbrev,
1215 struct dwarf2_cu *cu);
1216
1217 static void free_stack_comp_unit (void *);
1218
1219 static hashval_t partial_die_hash (const void *item);
1220
1221 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1222
1223 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1224 (unsigned int offset, struct objfile *objfile);
1225
1226 static void init_one_comp_unit (struct dwarf2_cu *cu,
1227 struct dwarf2_per_cu_data *per_cu);
1228
1229 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1230 struct die_info *comp_unit_die);
1231
1232 static void free_heap_comp_unit (void *);
1233
1234 static void free_cached_comp_units (void *);
1235
1236 static void age_cached_comp_units (void);
1237
1238 static void free_one_cached_comp_unit (void *);
1239
1240 static struct type *set_die_type (struct die_info *, struct type *,
1241 struct dwarf2_cu *);
1242
1243 static void create_all_comp_units (struct objfile *);
1244
1245 static int create_debug_types_hash_table (struct objfile *objfile);
1246
1247 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1248
1249 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1250
1251 static void dwarf2_add_dependence (struct dwarf2_cu *,
1252 struct dwarf2_per_cu_data *);
1253
1254 static void dwarf2_mark (struct dwarf2_cu *);
1255
1256 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1257
1258 static struct type *get_die_type_at_offset (unsigned int,
1259 struct dwarf2_per_cu_data *per_cu);
1260
1261 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1262
1263 static void dwarf2_release_queue (void *dummy);
1264
1265 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1266
1267 static void process_queue (void);
1268
1269 static void find_file_and_directory (struct die_info *die,
1270 struct dwarf2_cu *cu,
1271 char **name, char **comp_dir);
1272
1273 static char *file_full_name (int file, struct line_header *lh,
1274 const char *comp_dir);
1275
1276 static gdb_byte *read_and_check_comp_unit_head
1277 (struct comp_unit_head *header,
1278 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1279 int is_debug_types_section);
1280
1281 static void init_cu_die_reader (struct die_reader_specs *reader,
1282 struct dwarf2_cu *cu);
1283
1284 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1285
1286 #if WORDS_BIGENDIAN
1287
1288 /* Convert VALUE between big- and little-endian. */
1289 static offset_type
1290 byte_swap (offset_type value)
1291 {
1292 offset_type result;
1293
1294 result = (value & 0xff) << 24;
1295 result |= (value & 0xff00) << 8;
1296 result |= (value & 0xff0000) >> 8;
1297 result |= (value & 0xff000000) >> 24;
1298 return result;
1299 }
1300
1301 #define MAYBE_SWAP(V) byte_swap (V)
1302
1303 #else
1304 #define MAYBE_SWAP(V) (V)
1305 #endif /* WORDS_BIGENDIAN */
1306
1307 /* The suffix for an index file. */
1308 #define INDEX_SUFFIX ".gdb-index"
1309
1310 static const char *dwarf2_physname (char *name, struct die_info *die,
1311 struct dwarf2_cu *cu);
1312
1313 /* Try to locate the sections we need for DWARF 2 debugging
1314 information and return true if we have enough to do something.
1315 NAMES points to the dwarf2 section names, or is NULL if the standard
1316 ELF names are used. */
1317
1318 int
1319 dwarf2_has_info (struct objfile *objfile,
1320 const struct dwarf2_debug_sections *names)
1321 {
1322 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1323 if (!dwarf2_per_objfile)
1324 {
1325 /* Initialize per-objfile state. */
1326 struct dwarf2_per_objfile *data
1327 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1328
1329 memset (data, 0, sizeof (*data));
1330 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1331 dwarf2_per_objfile = data;
1332
1333 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1334 (void *) names);
1335 dwarf2_per_objfile->objfile = objfile;
1336 }
1337 return (dwarf2_per_objfile->info.asection != NULL
1338 && dwarf2_per_objfile->abbrev.asection != NULL);
1339 }
1340
1341 /* When loading sections, we look either for uncompressed section or for
1342 compressed section names. */
1343
1344 static int
1345 section_is_p (const char *section_name,
1346 const struct dwarf2_section_names *names)
1347 {
1348 if (names->normal != NULL
1349 && strcmp (section_name, names->normal) == 0)
1350 return 1;
1351 if (names->compressed != NULL
1352 && strcmp (section_name, names->compressed) == 0)
1353 return 1;
1354 return 0;
1355 }
1356
1357 /* This function is mapped across the sections and remembers the
1358 offset and size of each of the debugging sections we are interested
1359 in. */
1360
1361 static void
1362 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1363 {
1364 const struct dwarf2_debug_sections *names;
1365
1366 if (vnames == NULL)
1367 names = &dwarf2_elf_names;
1368 else
1369 names = (const struct dwarf2_debug_sections *) vnames;
1370
1371 if (section_is_p (sectp->name, &names->info))
1372 {
1373 dwarf2_per_objfile->info.asection = sectp;
1374 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1375 }
1376 else if (section_is_p (sectp->name, &names->abbrev))
1377 {
1378 dwarf2_per_objfile->abbrev.asection = sectp;
1379 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1380 }
1381 else if (section_is_p (sectp->name, &names->line))
1382 {
1383 dwarf2_per_objfile->line.asection = sectp;
1384 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1385 }
1386 else if (section_is_p (sectp->name, &names->loc))
1387 {
1388 dwarf2_per_objfile->loc.asection = sectp;
1389 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1390 }
1391 else if (section_is_p (sectp->name, &names->macinfo))
1392 {
1393 dwarf2_per_objfile->macinfo.asection = sectp;
1394 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1395 }
1396 else if (section_is_p (sectp->name, &names->macro))
1397 {
1398 dwarf2_per_objfile->macro.asection = sectp;
1399 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1400 }
1401 else if (section_is_p (sectp->name, &names->str))
1402 {
1403 dwarf2_per_objfile->str.asection = sectp;
1404 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1405 }
1406 else if (section_is_p (sectp->name, &names->frame))
1407 {
1408 dwarf2_per_objfile->frame.asection = sectp;
1409 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1410 }
1411 else if (section_is_p (sectp->name, &names->eh_frame))
1412 {
1413 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1414
1415 if (aflag & SEC_HAS_CONTENTS)
1416 {
1417 dwarf2_per_objfile->eh_frame.asection = sectp;
1418 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1419 }
1420 }
1421 else if (section_is_p (sectp->name, &names->ranges))
1422 {
1423 dwarf2_per_objfile->ranges.asection = sectp;
1424 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1425 }
1426 else if (section_is_p (sectp->name, &names->types))
1427 {
1428 struct dwarf2_section_info type_section;
1429
1430 memset (&type_section, 0, sizeof (type_section));
1431 type_section.asection = sectp;
1432 type_section.size = bfd_get_section_size (sectp);
1433
1434 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1435 &type_section);
1436 }
1437 else if (section_is_p (sectp->name, &names->gdb_index))
1438 {
1439 dwarf2_per_objfile->gdb_index.asection = sectp;
1440 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1441 }
1442
1443 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1444 && bfd_section_vma (abfd, sectp) == 0)
1445 dwarf2_per_objfile->has_section_at_zero = 1;
1446 }
1447
1448 /* Decompress a section that was compressed using zlib. Store the
1449 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1450
1451 static void
1452 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1453 gdb_byte **outbuf, bfd_size_type *outsize)
1454 {
1455 bfd *abfd = objfile->obfd;
1456 #ifndef HAVE_ZLIB_H
1457 error (_("Support for zlib-compressed DWARF data (from '%s') "
1458 "is disabled in this copy of GDB"),
1459 bfd_get_filename (abfd));
1460 #else
1461 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1462 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1463 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1464 bfd_size_type uncompressed_size;
1465 gdb_byte *uncompressed_buffer;
1466 z_stream strm;
1467 int rc;
1468 int header_size = 12;
1469
1470 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1471 || bfd_bread (compressed_buffer,
1472 compressed_size, abfd) != compressed_size)
1473 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1474 bfd_get_filename (abfd));
1475
1476 /* Read the zlib header. In this case, it should be "ZLIB" followed
1477 by the uncompressed section size, 8 bytes in big-endian order. */
1478 if (compressed_size < header_size
1479 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1480 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1481 bfd_get_filename (abfd));
1482 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1483 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1484 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1485 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[11];
1490
1491 /* It is possible the section consists of several compressed
1492 buffers concatenated together, so we uncompress in a loop. */
1493 strm.zalloc = NULL;
1494 strm.zfree = NULL;
1495 strm.opaque = NULL;
1496 strm.avail_in = compressed_size - header_size;
1497 strm.next_in = (Bytef*) compressed_buffer + header_size;
1498 strm.avail_out = uncompressed_size;
1499 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1500 uncompressed_size);
1501 rc = inflateInit (&strm);
1502 while (strm.avail_in > 0)
1503 {
1504 if (rc != Z_OK)
1505 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1506 bfd_get_filename (abfd), rc);
1507 strm.next_out = ((Bytef*) uncompressed_buffer
1508 + (uncompressed_size - strm.avail_out));
1509 rc = inflate (&strm, Z_FINISH);
1510 if (rc != Z_STREAM_END)
1511 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1512 bfd_get_filename (abfd), rc);
1513 rc = inflateReset (&strm);
1514 }
1515 rc = inflateEnd (&strm);
1516 if (rc != Z_OK
1517 || strm.avail_out != 0)
1518 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1519 bfd_get_filename (abfd), rc);
1520
1521 do_cleanups (cleanup);
1522 *outbuf = uncompressed_buffer;
1523 *outsize = uncompressed_size;
1524 #endif
1525 }
1526
1527 /* A helper function that decides whether a section is empty. */
1528
1529 static int
1530 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1531 {
1532 return info->asection == NULL || info->size == 0;
1533 }
1534
1535 /* Read the contents of the section INFO from object file specified by
1536 OBJFILE, store info about the section into INFO.
1537 If the section is compressed, uncompress it before returning. */
1538
1539 static void
1540 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1541 {
1542 bfd *abfd = objfile->obfd;
1543 asection *sectp = info->asection;
1544 gdb_byte *buf, *retbuf;
1545 unsigned char header[4];
1546
1547 if (info->readin)
1548 return;
1549 info->buffer = NULL;
1550 info->map_addr = NULL;
1551 info->readin = 1;
1552
1553 if (dwarf2_section_empty_p (info))
1554 return;
1555
1556 /* Check if the file has a 4-byte header indicating compression. */
1557 if (info->size > sizeof (header)
1558 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1559 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1560 {
1561 /* Upon decompression, update the buffer and its size. */
1562 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1563 {
1564 zlib_decompress_section (objfile, sectp, &info->buffer,
1565 &info->size);
1566 return;
1567 }
1568 }
1569
1570 #ifdef HAVE_MMAP
1571 if (pagesize == 0)
1572 pagesize = getpagesize ();
1573
1574 /* Only try to mmap sections which are large enough: we don't want to
1575 waste space due to fragmentation. Also, only try mmap for sections
1576 without relocations. */
1577
1578 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1579 {
1580 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1581 MAP_PRIVATE, sectp->filepos,
1582 &info->map_addr, &info->map_len);
1583
1584 if ((caddr_t)info->buffer != MAP_FAILED)
1585 {
1586 #if HAVE_POSIX_MADVISE
1587 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1588 #endif
1589 return;
1590 }
1591 }
1592 #endif
1593
1594 /* If we get here, we are a normal, not-compressed section. */
1595 info->buffer = buf
1596 = obstack_alloc (&objfile->objfile_obstack, info->size);
1597
1598 /* When debugging .o files, we may need to apply relocations; see
1599 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1600 We never compress sections in .o files, so we only need to
1601 try this when the section is not compressed. */
1602 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1603 if (retbuf != NULL)
1604 {
1605 info->buffer = retbuf;
1606 return;
1607 }
1608
1609 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1610 || bfd_bread (buf, info->size, abfd) != info->size)
1611 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1612 bfd_get_filename (abfd));
1613 }
1614
1615 /* A helper function that returns the size of a section in a safe way.
1616 If you are positive that the section has been read before using the
1617 size, then it is safe to refer to the dwarf2_section_info object's
1618 "size" field directly. In other cases, you must call this
1619 function, because for compressed sections the size field is not set
1620 correctly until the section has been read. */
1621
1622 static bfd_size_type
1623 dwarf2_section_size (struct objfile *objfile,
1624 struct dwarf2_section_info *info)
1625 {
1626 if (!info->readin)
1627 dwarf2_read_section (objfile, info);
1628 return info->size;
1629 }
1630
1631 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1632 SECTION_NAME. */
1633
1634 void
1635 dwarf2_get_section_info (struct objfile *objfile,
1636 enum dwarf2_section_enum sect,
1637 asection **sectp, gdb_byte **bufp,
1638 bfd_size_type *sizep)
1639 {
1640 struct dwarf2_per_objfile *data
1641 = objfile_data (objfile, dwarf2_objfile_data_key);
1642 struct dwarf2_section_info *info;
1643
1644 /* We may see an objfile without any DWARF, in which case we just
1645 return nothing. */
1646 if (data == NULL)
1647 {
1648 *sectp = NULL;
1649 *bufp = NULL;
1650 *sizep = 0;
1651 return;
1652 }
1653 switch (sect)
1654 {
1655 case DWARF2_DEBUG_FRAME:
1656 info = &data->frame;
1657 break;
1658 case DWARF2_EH_FRAME:
1659 info = &data->eh_frame;
1660 break;
1661 default:
1662 gdb_assert_not_reached ("unexpected section");
1663 }
1664
1665 dwarf2_read_section (objfile, info);
1666
1667 *sectp = info->asection;
1668 *bufp = info->buffer;
1669 *sizep = info->size;
1670 }
1671
1672 \f
1673 /* DWARF quick_symbols_functions support. */
1674
1675 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1676 unique line tables, so we maintain a separate table of all .debug_line
1677 derived entries to support the sharing.
1678 All the quick functions need is the list of file names. We discard the
1679 line_header when we're done and don't need to record it here. */
1680 struct quick_file_names
1681 {
1682 /* The offset in .debug_line of the line table. We hash on this. */
1683 unsigned int offset;
1684
1685 /* The number of entries in file_names, real_names. */
1686 unsigned int num_file_names;
1687
1688 /* The file names from the line table, after being run through
1689 file_full_name. */
1690 const char **file_names;
1691
1692 /* The file names from the line table after being run through
1693 gdb_realpath. These are computed lazily. */
1694 const char **real_names;
1695 };
1696
1697 /* When using the index (and thus not using psymtabs), each CU has an
1698 object of this type. This is used to hold information needed by
1699 the various "quick" methods. */
1700 struct dwarf2_per_cu_quick_data
1701 {
1702 /* The file table. This can be NULL if there was no file table
1703 or it's currently not read in.
1704 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1705 struct quick_file_names *file_names;
1706
1707 /* The corresponding symbol table. This is NULL if symbols for this
1708 CU have not yet been read. */
1709 struct symtab *symtab;
1710
1711 /* A temporary mark bit used when iterating over all CUs in
1712 expand_symtabs_matching. */
1713 unsigned int mark : 1;
1714
1715 /* True if we've tried to read the file table and found there isn't one.
1716 There will be no point in trying to read it again next time. */
1717 unsigned int no_file_data : 1;
1718 };
1719
1720 /* Hash function for a quick_file_names. */
1721
1722 static hashval_t
1723 hash_file_name_entry (const void *e)
1724 {
1725 const struct quick_file_names *file_data = e;
1726
1727 return file_data->offset;
1728 }
1729
1730 /* Equality function for a quick_file_names. */
1731
1732 static int
1733 eq_file_name_entry (const void *a, const void *b)
1734 {
1735 const struct quick_file_names *ea = a;
1736 const struct quick_file_names *eb = b;
1737
1738 return ea->offset == eb->offset;
1739 }
1740
1741 /* Delete function for a quick_file_names. */
1742
1743 static void
1744 delete_file_name_entry (void *e)
1745 {
1746 struct quick_file_names *file_data = e;
1747 int i;
1748
1749 for (i = 0; i < file_data->num_file_names; ++i)
1750 {
1751 xfree ((void*) file_data->file_names[i]);
1752 if (file_data->real_names)
1753 xfree ((void*) file_data->real_names[i]);
1754 }
1755
1756 /* The space for the struct itself lives on objfile_obstack,
1757 so we don't free it here. */
1758 }
1759
1760 /* Create a quick_file_names hash table. */
1761
1762 static htab_t
1763 create_quick_file_names_table (unsigned int nr_initial_entries)
1764 {
1765 return htab_create_alloc (nr_initial_entries,
1766 hash_file_name_entry, eq_file_name_entry,
1767 delete_file_name_entry, xcalloc, xfree);
1768 }
1769
1770 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1771 have to be created afterwards. You should call age_cached_comp_units after
1772 processing PER_CU->CU. dw2_setup must have been already called. */
1773
1774 static void
1775 load_cu (struct dwarf2_per_cu_data *per_cu)
1776 {
1777 if (per_cu->debug_types_section)
1778 load_full_type_unit (per_cu);
1779 else
1780 load_full_comp_unit (per_cu);
1781
1782 gdb_assert (per_cu->cu != NULL);
1783
1784 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1785 }
1786
1787 /* Read in the symbols for PER_CU. */
1788
1789 static void
1790 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1791 {
1792 struct cleanup *back_to;
1793
1794 back_to = make_cleanup (dwarf2_release_queue, NULL);
1795
1796 queue_comp_unit (per_cu);
1797
1798 load_cu (per_cu);
1799
1800 process_queue ();
1801
1802 /* Age the cache, releasing compilation units that have not
1803 been used recently. */
1804 age_cached_comp_units ();
1805
1806 do_cleanups (back_to);
1807 }
1808
1809 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1810 the objfile from which this CU came. Returns the resulting symbol
1811 table. */
1812
1813 static struct symtab *
1814 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1815 {
1816 if (!per_cu->v.quick->symtab)
1817 {
1818 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1819 increment_reading_symtab ();
1820 dw2_do_instantiate_symtab (per_cu);
1821 do_cleanups (back_to);
1822 }
1823 return per_cu->v.quick->symtab;
1824 }
1825
1826 /* Return the CU given its index. */
1827
1828 static struct dwarf2_per_cu_data *
1829 dw2_get_cu (int index)
1830 {
1831 if (index >= dwarf2_per_objfile->n_comp_units)
1832 {
1833 index -= dwarf2_per_objfile->n_comp_units;
1834 return dwarf2_per_objfile->all_type_units[index];
1835 }
1836 return dwarf2_per_objfile->all_comp_units[index];
1837 }
1838
1839 /* A helper function that knows how to read a 64-bit value in a way
1840 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1841 otherwise. */
1842
1843 static int
1844 extract_cu_value (const char *bytes, ULONGEST *result)
1845 {
1846 if (sizeof (ULONGEST) < 8)
1847 {
1848 int i;
1849
1850 /* Ignore the upper 4 bytes if they are all zero. */
1851 for (i = 0; i < 4; ++i)
1852 if (bytes[i + 4] != 0)
1853 return 0;
1854
1855 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1856 }
1857 else
1858 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1859 return 1;
1860 }
1861
1862 /* Read the CU list from the mapped index, and use it to create all
1863 the CU objects for this objfile. Return 0 if something went wrong,
1864 1 if everything went ok. */
1865
1866 static int
1867 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1868 offset_type cu_list_elements)
1869 {
1870 offset_type i;
1871
1872 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1873 dwarf2_per_objfile->all_comp_units
1874 = obstack_alloc (&objfile->objfile_obstack,
1875 dwarf2_per_objfile->n_comp_units
1876 * sizeof (struct dwarf2_per_cu_data *));
1877
1878 for (i = 0; i < cu_list_elements; i += 2)
1879 {
1880 struct dwarf2_per_cu_data *the_cu;
1881 ULONGEST offset, length;
1882
1883 if (!extract_cu_value (cu_list, &offset)
1884 || !extract_cu_value (cu_list + 8, &length))
1885 return 0;
1886 cu_list += 2 * 8;
1887
1888 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1889 struct dwarf2_per_cu_data);
1890 the_cu->offset = offset;
1891 the_cu->length = length;
1892 the_cu->objfile = objfile;
1893 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1894 struct dwarf2_per_cu_quick_data);
1895 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1896 }
1897
1898 return 1;
1899 }
1900
1901 /* Create the signatured type hash table from the index. */
1902
1903 static int
1904 create_signatured_type_table_from_index (struct objfile *objfile,
1905 struct dwarf2_section_info *section,
1906 const gdb_byte *bytes,
1907 offset_type elements)
1908 {
1909 offset_type i;
1910 htab_t sig_types_hash;
1911
1912 dwarf2_per_objfile->n_type_units = elements / 3;
1913 dwarf2_per_objfile->all_type_units
1914 = obstack_alloc (&objfile->objfile_obstack,
1915 dwarf2_per_objfile->n_type_units
1916 * sizeof (struct dwarf2_per_cu_data *));
1917
1918 sig_types_hash = allocate_signatured_type_table (objfile);
1919
1920 for (i = 0; i < elements; i += 3)
1921 {
1922 struct signatured_type *type_sig;
1923 ULONGEST offset, type_offset, signature;
1924 void **slot;
1925
1926 if (!extract_cu_value (bytes, &offset)
1927 || !extract_cu_value (bytes + 8, &type_offset))
1928 return 0;
1929 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1930 bytes += 3 * 8;
1931
1932 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1933 struct signatured_type);
1934 type_sig->signature = signature;
1935 type_sig->type_offset = type_offset;
1936 type_sig->per_cu.debug_types_section = section;
1937 type_sig->per_cu.offset = offset;
1938 type_sig->per_cu.objfile = objfile;
1939 type_sig->per_cu.v.quick
1940 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1941 struct dwarf2_per_cu_quick_data);
1942
1943 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1944 *slot = type_sig;
1945
1946 dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1947 }
1948
1949 dwarf2_per_objfile->signatured_types = sig_types_hash;
1950
1951 return 1;
1952 }
1953
1954 /* Read the address map data from the mapped index, and use it to
1955 populate the objfile's psymtabs_addrmap. */
1956
1957 static void
1958 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1959 {
1960 const gdb_byte *iter, *end;
1961 struct obstack temp_obstack;
1962 struct addrmap *mutable_map;
1963 struct cleanup *cleanup;
1964 CORE_ADDR baseaddr;
1965
1966 obstack_init (&temp_obstack);
1967 cleanup = make_cleanup_obstack_free (&temp_obstack);
1968 mutable_map = addrmap_create_mutable (&temp_obstack);
1969
1970 iter = index->address_table;
1971 end = iter + index->address_table_size;
1972
1973 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1974
1975 while (iter < end)
1976 {
1977 ULONGEST hi, lo, cu_index;
1978 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1979 iter += 8;
1980 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1981 iter += 8;
1982 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1983 iter += 4;
1984
1985 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1986 dw2_get_cu (cu_index));
1987 }
1988
1989 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1990 &objfile->objfile_obstack);
1991 do_cleanups (cleanup);
1992 }
1993
1994 /* The hash function for strings in the mapped index. This is the same as
1995 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1996 implementation. This is necessary because the hash function is tied to the
1997 format of the mapped index file. The hash values do not have to match with
1998 SYMBOL_HASH_NEXT.
1999
2000 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2001
2002 static hashval_t
2003 mapped_index_string_hash (int index_version, const void *p)
2004 {
2005 const unsigned char *str = (const unsigned char *) p;
2006 hashval_t r = 0;
2007 unsigned char c;
2008
2009 while ((c = *str++) != 0)
2010 {
2011 if (index_version >= 5)
2012 c = tolower (c);
2013 r = r * 67 + c - 113;
2014 }
2015
2016 return r;
2017 }
2018
2019 /* Find a slot in the mapped index INDEX for the object named NAME.
2020 If NAME is found, set *VEC_OUT to point to the CU vector in the
2021 constant pool and return 1. If NAME cannot be found, return 0. */
2022
2023 static int
2024 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2025 offset_type **vec_out)
2026 {
2027 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2028 offset_type hash;
2029 offset_type slot, step;
2030 int (*cmp) (const char *, const char *);
2031
2032 if (current_language->la_language == language_cplus
2033 || current_language->la_language == language_java
2034 || current_language->la_language == language_fortran)
2035 {
2036 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2037 not contain any. */
2038 const char *paren = strchr (name, '(');
2039
2040 if (paren)
2041 {
2042 char *dup;
2043
2044 dup = xmalloc (paren - name + 1);
2045 memcpy (dup, name, paren - name);
2046 dup[paren - name] = 0;
2047
2048 make_cleanup (xfree, dup);
2049 name = dup;
2050 }
2051 }
2052
2053 /* Index version 4 did not support case insensitive searches. But the
2054 indices for case insensitive languages are built in lowercase, therefore
2055 simulate our NAME being searched is also lowercased. */
2056 hash = mapped_index_string_hash ((index->version == 4
2057 && case_sensitivity == case_sensitive_off
2058 ? 5 : index->version),
2059 name);
2060
2061 slot = hash & (index->symbol_table_slots - 1);
2062 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2063 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2064
2065 for (;;)
2066 {
2067 /* Convert a slot number to an offset into the table. */
2068 offset_type i = 2 * slot;
2069 const char *str;
2070 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2071 {
2072 do_cleanups (back_to);
2073 return 0;
2074 }
2075
2076 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2077 if (!cmp (name, str))
2078 {
2079 *vec_out = (offset_type *) (index->constant_pool
2080 + MAYBE_SWAP (index->symbol_table[i + 1]));
2081 do_cleanups (back_to);
2082 return 1;
2083 }
2084
2085 slot = (slot + step) & (index->symbol_table_slots - 1);
2086 }
2087 }
2088
2089 /* Read the index file. If everything went ok, initialize the "quick"
2090 elements of all the CUs and return 1. Otherwise, return 0. */
2091
2092 static int
2093 dwarf2_read_index (struct objfile *objfile)
2094 {
2095 char *addr;
2096 struct mapped_index *map;
2097 offset_type *metadata;
2098 const gdb_byte *cu_list;
2099 const gdb_byte *types_list = NULL;
2100 offset_type version, cu_list_elements;
2101 offset_type types_list_elements = 0;
2102 int i;
2103
2104 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2105 return 0;
2106
2107 /* Older elfutils strip versions could keep the section in the main
2108 executable while splitting it for the separate debug info file. */
2109 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2110 & SEC_HAS_CONTENTS) == 0)
2111 return 0;
2112
2113 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2114
2115 addr = dwarf2_per_objfile->gdb_index.buffer;
2116 /* Version check. */
2117 version = MAYBE_SWAP (*(offset_type *) addr);
2118 /* Versions earlier than 3 emitted every copy of a psymbol. This
2119 causes the index to behave very poorly for certain requests. Version 3
2120 contained incomplete addrmap. So, it seems better to just ignore such
2121 indices. Index version 4 uses a different hash function than index
2122 version 5 and later. */
2123 if (version < 4)
2124 return 0;
2125 /* Indices with higher version than the one supported by GDB may be no
2126 longer backward compatible. */
2127 if (version > 5)
2128 return 0;
2129
2130 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2131 map->version = version;
2132 map->total_size = dwarf2_per_objfile->gdb_index.size;
2133
2134 metadata = (offset_type *) (addr + sizeof (offset_type));
2135
2136 i = 0;
2137 cu_list = addr + MAYBE_SWAP (metadata[i]);
2138 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2139 / 8);
2140 ++i;
2141
2142 types_list = addr + MAYBE_SWAP (metadata[i]);
2143 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2144 - MAYBE_SWAP (metadata[i]))
2145 / 8);
2146 ++i;
2147
2148 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2149 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2150 - MAYBE_SWAP (metadata[i]));
2151 ++i;
2152
2153 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2154 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2155 - MAYBE_SWAP (metadata[i]))
2156 / (2 * sizeof (offset_type)));
2157 ++i;
2158
2159 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2160
2161 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2162 return 0;
2163
2164 if (types_list_elements)
2165 {
2166 struct dwarf2_section_info *section;
2167
2168 /* We can only handle a single .debug_types when we have an
2169 index. */
2170 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2171 return 0;
2172
2173 section = VEC_index (dwarf2_section_info_def,
2174 dwarf2_per_objfile->types, 0);
2175
2176 if (!create_signatured_type_table_from_index (objfile, section,
2177 types_list,
2178 types_list_elements))
2179 return 0;
2180 }
2181
2182 create_addrmap_from_index (objfile, map);
2183
2184 dwarf2_per_objfile->index_table = map;
2185 dwarf2_per_objfile->using_index = 1;
2186 dwarf2_per_objfile->quick_file_names_table =
2187 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2188
2189 return 1;
2190 }
2191
2192 /* A helper for the "quick" functions which sets the global
2193 dwarf2_per_objfile according to OBJFILE. */
2194
2195 static void
2196 dw2_setup (struct objfile *objfile)
2197 {
2198 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2199 gdb_assert (dwarf2_per_objfile);
2200 }
2201
2202 /* A helper for the "quick" functions which attempts to read the line
2203 table for THIS_CU. */
2204
2205 static struct quick_file_names *
2206 dw2_get_file_names (struct objfile *objfile,
2207 struct dwarf2_per_cu_data *this_cu)
2208 {
2209 bfd *abfd = objfile->obfd;
2210 struct line_header *lh;
2211 struct attribute *attr;
2212 struct cleanup *cleanups;
2213 struct die_info *comp_unit_die;
2214 struct dwarf2_section_info* sec;
2215 gdb_byte *info_ptr;
2216 int has_children, i;
2217 struct dwarf2_cu cu;
2218 unsigned int bytes_read;
2219 struct die_reader_specs reader_specs;
2220 char *name, *comp_dir;
2221 void **slot;
2222 struct quick_file_names *qfn;
2223 unsigned int line_offset;
2224
2225 if (this_cu->v.quick->file_names != NULL)
2226 return this_cu->v.quick->file_names;
2227 /* If we know there is no line data, no point in looking again. */
2228 if (this_cu->v.quick->no_file_data)
2229 return NULL;
2230
2231 init_one_comp_unit (&cu, this_cu);
2232 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2233
2234 if (this_cu->debug_types_section)
2235 sec = this_cu->debug_types_section;
2236 else
2237 sec = &dwarf2_per_objfile->info;
2238 dwarf2_read_section (objfile, sec);
2239 info_ptr = sec->buffer + this_cu->offset;
2240
2241 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2242 this_cu->debug_types_section != NULL);
2243
2244 /* Skip dummy compilation units. */
2245 if (info_ptr >= (sec->buffer + sec->size)
2246 || peek_abbrev_code (abfd, info_ptr) == 0)
2247 {
2248 do_cleanups (cleanups);
2249 return NULL;
2250 }
2251
2252 dwarf2_read_abbrevs (&cu);
2253 make_cleanup (dwarf2_free_abbrev_table, &cu);
2254
2255 init_cu_die_reader (&reader_specs, &cu);
2256 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2257 &has_children);
2258
2259 lh = NULL;
2260 slot = NULL;
2261 line_offset = 0;
2262 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2263 if (attr)
2264 {
2265 struct quick_file_names find_entry;
2266
2267 line_offset = DW_UNSND (attr);
2268
2269 /* We may have already read in this line header (TU line header sharing).
2270 If we have we're done. */
2271 find_entry.offset = line_offset;
2272 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2273 &find_entry, INSERT);
2274 if (*slot != NULL)
2275 {
2276 do_cleanups (cleanups);
2277 this_cu->v.quick->file_names = *slot;
2278 return *slot;
2279 }
2280
2281 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2282 }
2283 if (lh == NULL)
2284 {
2285 do_cleanups (cleanups);
2286 this_cu->v.quick->no_file_data = 1;
2287 return NULL;
2288 }
2289
2290 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2291 qfn->offset = line_offset;
2292 gdb_assert (slot != NULL);
2293 *slot = qfn;
2294
2295 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2296
2297 qfn->num_file_names = lh->num_file_names;
2298 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2299 lh->num_file_names * sizeof (char *));
2300 for (i = 0; i < lh->num_file_names; ++i)
2301 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2302 qfn->real_names = NULL;
2303
2304 free_line_header (lh);
2305 do_cleanups (cleanups);
2306
2307 this_cu->v.quick->file_names = qfn;
2308 return qfn;
2309 }
2310
2311 /* A helper for the "quick" functions which computes and caches the
2312 real path for a given file name from the line table. */
2313
2314 static const char *
2315 dw2_get_real_path (struct objfile *objfile,
2316 struct quick_file_names *qfn, int index)
2317 {
2318 if (qfn->real_names == NULL)
2319 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2320 qfn->num_file_names, sizeof (char *));
2321
2322 if (qfn->real_names[index] == NULL)
2323 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2324
2325 return qfn->real_names[index];
2326 }
2327
2328 static struct symtab *
2329 dw2_find_last_source_symtab (struct objfile *objfile)
2330 {
2331 int index;
2332
2333 dw2_setup (objfile);
2334 index = dwarf2_per_objfile->n_comp_units - 1;
2335 return dw2_instantiate_symtab (dw2_get_cu (index));
2336 }
2337
2338 /* Traversal function for dw2_forget_cached_source_info. */
2339
2340 static int
2341 dw2_free_cached_file_names (void **slot, void *info)
2342 {
2343 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2344
2345 if (file_data->real_names)
2346 {
2347 int i;
2348
2349 for (i = 0; i < file_data->num_file_names; ++i)
2350 {
2351 xfree ((void*) file_data->real_names[i]);
2352 file_data->real_names[i] = NULL;
2353 }
2354 }
2355
2356 return 1;
2357 }
2358
2359 static void
2360 dw2_forget_cached_source_info (struct objfile *objfile)
2361 {
2362 dw2_setup (objfile);
2363
2364 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2365 dw2_free_cached_file_names, NULL);
2366 }
2367
2368 /* Helper function for dw2_map_symtabs_matching_filename that expands
2369 the symtabs and calls the iterator. */
2370
2371 static int
2372 dw2_map_expand_apply (struct objfile *objfile,
2373 struct dwarf2_per_cu_data *per_cu,
2374 const char *name,
2375 const char *full_path, const char *real_path,
2376 int (*callback) (struct symtab *, void *),
2377 void *data)
2378 {
2379 struct symtab *last_made = objfile->symtabs;
2380
2381 /* Don't visit already-expanded CUs. */
2382 if (per_cu->v.quick->symtab)
2383 return 0;
2384
2385 /* This may expand more than one symtab, and we want to iterate over
2386 all of them. */
2387 dw2_instantiate_symtab (per_cu);
2388
2389 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2390 objfile->symtabs, last_made);
2391 }
2392
2393 /* Implementation of the map_symtabs_matching_filename method. */
2394
2395 static int
2396 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2397 const char *full_path, const char *real_path,
2398 int (*callback) (struct symtab *, void *),
2399 void *data)
2400 {
2401 int i;
2402 const char *name_basename = lbasename (name);
2403 int name_len = strlen (name);
2404 int is_abs = IS_ABSOLUTE_PATH (name);
2405
2406 dw2_setup (objfile);
2407
2408 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2409 + dwarf2_per_objfile->n_type_units); ++i)
2410 {
2411 int j;
2412 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2413 struct quick_file_names *file_data;
2414
2415 /* We only need to look at symtabs not already expanded. */
2416 if (per_cu->v.quick->symtab)
2417 continue;
2418
2419 file_data = dw2_get_file_names (objfile, per_cu);
2420 if (file_data == NULL)
2421 continue;
2422
2423 for (j = 0; j < file_data->num_file_names; ++j)
2424 {
2425 const char *this_name = file_data->file_names[j];
2426
2427 if (FILENAME_CMP (name, this_name) == 0
2428 || (!is_abs && compare_filenames_for_search (this_name,
2429 name, name_len)))
2430 {
2431 if (dw2_map_expand_apply (objfile, per_cu,
2432 name, full_path, real_path,
2433 callback, data))
2434 return 1;
2435 }
2436
2437 /* Before we invoke realpath, which can get expensive when many
2438 files are involved, do a quick comparison of the basenames. */
2439 if (! basenames_may_differ
2440 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2441 continue;
2442
2443 if (full_path != NULL)
2444 {
2445 const char *this_real_name = dw2_get_real_path (objfile,
2446 file_data, j);
2447
2448 if (this_real_name != NULL
2449 && (FILENAME_CMP (full_path, this_real_name) == 0
2450 || (!is_abs
2451 && compare_filenames_for_search (this_real_name,
2452 name, name_len))))
2453 {
2454 if (dw2_map_expand_apply (objfile, per_cu,
2455 name, full_path, real_path,
2456 callback, data))
2457 return 1;
2458 }
2459 }
2460
2461 if (real_path != NULL)
2462 {
2463 const char *this_real_name = dw2_get_real_path (objfile,
2464 file_data, j);
2465
2466 if (this_real_name != NULL
2467 && (FILENAME_CMP (real_path, this_real_name) == 0
2468 || (!is_abs
2469 && compare_filenames_for_search (this_real_name,
2470 name, name_len))))
2471 {
2472 if (dw2_map_expand_apply (objfile, per_cu,
2473 name, full_path, real_path,
2474 callback, data))
2475 return 1;
2476 }
2477 }
2478 }
2479 }
2480
2481 return 0;
2482 }
2483
2484 static struct symtab *
2485 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2486 const char *name, domain_enum domain)
2487 {
2488 /* We do all the work in the pre_expand_symtabs_matching hook
2489 instead. */
2490 return NULL;
2491 }
2492
2493 /* A helper function that expands all symtabs that hold an object
2494 named NAME. */
2495
2496 static void
2497 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2498 {
2499 dw2_setup (objfile);
2500
2501 /* index_table is NULL if OBJF_READNOW. */
2502 if (dwarf2_per_objfile->index_table)
2503 {
2504 offset_type *vec;
2505
2506 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2507 name, &vec))
2508 {
2509 offset_type i, len = MAYBE_SWAP (*vec);
2510 for (i = 0; i < len; ++i)
2511 {
2512 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2513 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2514
2515 dw2_instantiate_symtab (per_cu);
2516 }
2517 }
2518 }
2519 }
2520
2521 static void
2522 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2523 enum block_enum block_kind, const char *name,
2524 domain_enum domain)
2525 {
2526 dw2_do_expand_symtabs_matching (objfile, name);
2527 }
2528
2529 static void
2530 dw2_print_stats (struct objfile *objfile)
2531 {
2532 int i, count;
2533
2534 dw2_setup (objfile);
2535 count = 0;
2536 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2537 + dwarf2_per_objfile->n_type_units); ++i)
2538 {
2539 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2540
2541 if (!per_cu->v.quick->symtab)
2542 ++count;
2543 }
2544 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2545 }
2546
2547 static void
2548 dw2_dump (struct objfile *objfile)
2549 {
2550 /* Nothing worth printing. */
2551 }
2552
2553 static void
2554 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2555 struct section_offsets *delta)
2556 {
2557 /* There's nothing to relocate here. */
2558 }
2559
2560 static void
2561 dw2_expand_symtabs_for_function (struct objfile *objfile,
2562 const char *func_name)
2563 {
2564 dw2_do_expand_symtabs_matching (objfile, func_name);
2565 }
2566
2567 static void
2568 dw2_expand_all_symtabs (struct objfile *objfile)
2569 {
2570 int i;
2571
2572 dw2_setup (objfile);
2573
2574 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2575 + dwarf2_per_objfile->n_type_units); ++i)
2576 {
2577 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2578
2579 dw2_instantiate_symtab (per_cu);
2580 }
2581 }
2582
2583 static void
2584 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2585 const char *filename)
2586 {
2587 int i;
2588
2589 dw2_setup (objfile);
2590
2591 /* We don't need to consider type units here.
2592 This is only called for examining code, e.g. expand_line_sal.
2593 There can be an order of magnitude (or more) more type units
2594 than comp units, and we avoid them if we can. */
2595
2596 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2597 {
2598 int j;
2599 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2600 struct quick_file_names *file_data;
2601
2602 /* We only need to look at symtabs not already expanded. */
2603 if (per_cu->v.quick->symtab)
2604 continue;
2605
2606 file_data = dw2_get_file_names (objfile, per_cu);
2607 if (file_data == NULL)
2608 continue;
2609
2610 for (j = 0; j < file_data->num_file_names; ++j)
2611 {
2612 const char *this_name = file_data->file_names[j];
2613 if (FILENAME_CMP (this_name, filename) == 0)
2614 {
2615 dw2_instantiate_symtab (per_cu);
2616 break;
2617 }
2618 }
2619 }
2620 }
2621
2622 static const char *
2623 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2624 {
2625 struct dwarf2_per_cu_data *per_cu;
2626 offset_type *vec;
2627 struct quick_file_names *file_data;
2628
2629 dw2_setup (objfile);
2630
2631 /* index_table is NULL if OBJF_READNOW. */
2632 if (!dwarf2_per_objfile->index_table)
2633 {
2634 struct symtab *s;
2635
2636 ALL_OBJFILE_SYMTABS (objfile, s)
2637 if (s->primary)
2638 {
2639 struct blockvector *bv = BLOCKVECTOR (s);
2640 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2641 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2642
2643 if (sym)
2644 return sym->symtab->filename;
2645 }
2646 return NULL;
2647 }
2648
2649 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2650 name, &vec))
2651 return NULL;
2652
2653 /* Note that this just looks at the very first one named NAME -- but
2654 actually we are looking for a function. find_main_filename
2655 should be rewritten so that it doesn't require a custom hook. It
2656 could just use the ordinary symbol tables. */
2657 /* vec[0] is the length, which must always be >0. */
2658 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2659
2660 file_data = dw2_get_file_names (objfile, per_cu);
2661 if (file_data == NULL)
2662 return NULL;
2663
2664 return file_data->file_names[file_data->num_file_names - 1];
2665 }
2666
2667 static void
2668 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2669 struct objfile *objfile, int global,
2670 int (*callback) (struct block *,
2671 struct symbol *, void *),
2672 void *data, symbol_compare_ftype *match,
2673 symbol_compare_ftype *ordered_compare)
2674 {
2675 /* Currently unimplemented; used for Ada. The function can be called if the
2676 current language is Ada for a non-Ada objfile using GNU index. As Ada
2677 does not look for non-Ada symbols this function should just return. */
2678 }
2679
2680 static void
2681 dw2_expand_symtabs_matching
2682 (struct objfile *objfile,
2683 int (*file_matcher) (const char *, void *),
2684 int (*name_matcher) (const char *, void *),
2685 enum search_domain kind,
2686 void *data)
2687 {
2688 int i;
2689 offset_type iter;
2690 struct mapped_index *index;
2691
2692 dw2_setup (objfile);
2693
2694 /* index_table is NULL if OBJF_READNOW. */
2695 if (!dwarf2_per_objfile->index_table)
2696 return;
2697 index = dwarf2_per_objfile->index_table;
2698
2699 if (file_matcher != NULL)
2700 {
2701 struct cleanup *cleanup;
2702 htab_t visited_found, visited_not_found;
2703
2704 visited_found = htab_create_alloc (10,
2705 htab_hash_pointer, htab_eq_pointer,
2706 NULL, xcalloc, xfree);
2707 cleanup = make_cleanup_htab_delete (visited_found);
2708 visited_not_found = htab_create_alloc (10,
2709 htab_hash_pointer, htab_eq_pointer,
2710 NULL, xcalloc, xfree);
2711 make_cleanup_htab_delete (visited_not_found);
2712
2713 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2714 + dwarf2_per_objfile->n_type_units); ++i)
2715 {
2716 int j;
2717 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2718 struct quick_file_names *file_data;
2719 void **slot;
2720
2721 per_cu->v.quick->mark = 0;
2722
2723 /* We only need to look at symtabs not already expanded. */
2724 if (per_cu->v.quick->symtab)
2725 continue;
2726
2727 file_data = dw2_get_file_names (objfile, per_cu);
2728 if (file_data == NULL)
2729 continue;
2730
2731 if (htab_find (visited_not_found, file_data) != NULL)
2732 continue;
2733 else if (htab_find (visited_found, file_data) != NULL)
2734 {
2735 per_cu->v.quick->mark = 1;
2736 continue;
2737 }
2738
2739 for (j = 0; j < file_data->num_file_names; ++j)
2740 {
2741 if (file_matcher (file_data->file_names[j], data))
2742 {
2743 per_cu->v.quick->mark = 1;
2744 break;
2745 }
2746 }
2747
2748 slot = htab_find_slot (per_cu->v.quick->mark
2749 ? visited_found
2750 : visited_not_found,
2751 file_data, INSERT);
2752 *slot = file_data;
2753 }
2754
2755 do_cleanups (cleanup);
2756 }
2757
2758 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2759 {
2760 offset_type idx = 2 * iter;
2761 const char *name;
2762 offset_type *vec, vec_len, vec_idx;
2763
2764 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2765 continue;
2766
2767 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2768
2769 if (! (*name_matcher) (name, data))
2770 continue;
2771
2772 /* The name was matched, now expand corresponding CUs that were
2773 marked. */
2774 vec = (offset_type *) (index->constant_pool
2775 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2776 vec_len = MAYBE_SWAP (vec[0]);
2777 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2778 {
2779 struct dwarf2_per_cu_data *per_cu;
2780
2781 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2782 if (file_matcher == NULL || per_cu->v.quick->mark)
2783 dw2_instantiate_symtab (per_cu);
2784 }
2785 }
2786 }
2787
2788 static struct symtab *
2789 dw2_find_pc_sect_symtab (struct objfile *objfile,
2790 struct minimal_symbol *msymbol,
2791 CORE_ADDR pc,
2792 struct obj_section *section,
2793 int warn_if_readin)
2794 {
2795 struct dwarf2_per_cu_data *data;
2796
2797 dw2_setup (objfile);
2798
2799 if (!objfile->psymtabs_addrmap)
2800 return NULL;
2801
2802 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2803 if (!data)
2804 return NULL;
2805
2806 if (warn_if_readin && data->v.quick->symtab)
2807 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2808 paddress (get_objfile_arch (objfile), pc));
2809
2810 return dw2_instantiate_symtab (data);
2811 }
2812
2813 static void
2814 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2815 void *data, int need_fullname)
2816 {
2817 int i;
2818 struct cleanup *cleanup;
2819 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2820 NULL, xcalloc, xfree);
2821
2822 cleanup = make_cleanup_htab_delete (visited);
2823 dw2_setup (objfile);
2824
2825 /* We can ignore file names coming from already-expanded CUs. */
2826 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2827 + dwarf2_per_objfile->n_type_units); ++i)
2828 {
2829 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2830
2831 if (per_cu->v.quick->symtab)
2832 {
2833 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2834 INSERT);
2835
2836 *slot = per_cu->v.quick->file_names;
2837 }
2838 }
2839
2840 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2841 + dwarf2_per_objfile->n_type_units); ++i)
2842 {
2843 int j;
2844 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2845 struct quick_file_names *file_data;
2846 void **slot;
2847
2848 /* We only need to look at symtabs not already expanded. */
2849 if (per_cu->v.quick->symtab)
2850 continue;
2851
2852 file_data = dw2_get_file_names (objfile, per_cu);
2853 if (file_data == NULL)
2854 continue;
2855
2856 slot = htab_find_slot (visited, file_data, INSERT);
2857 if (*slot)
2858 {
2859 /* Already visited. */
2860 continue;
2861 }
2862 *slot = file_data;
2863
2864 for (j = 0; j < file_data->num_file_names; ++j)
2865 {
2866 const char *this_real_name;
2867
2868 if (need_fullname)
2869 this_real_name = dw2_get_real_path (objfile, file_data, j);
2870 else
2871 this_real_name = NULL;
2872 (*fun) (file_data->file_names[j], this_real_name, data);
2873 }
2874 }
2875
2876 do_cleanups (cleanup);
2877 }
2878
2879 static int
2880 dw2_has_symbols (struct objfile *objfile)
2881 {
2882 return 1;
2883 }
2884
2885 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2886 {
2887 dw2_has_symbols,
2888 dw2_find_last_source_symtab,
2889 dw2_forget_cached_source_info,
2890 dw2_map_symtabs_matching_filename,
2891 dw2_lookup_symbol,
2892 dw2_pre_expand_symtabs_matching,
2893 dw2_print_stats,
2894 dw2_dump,
2895 dw2_relocate,
2896 dw2_expand_symtabs_for_function,
2897 dw2_expand_all_symtabs,
2898 dw2_expand_symtabs_with_filename,
2899 dw2_find_symbol_file,
2900 dw2_map_matching_symbols,
2901 dw2_expand_symtabs_matching,
2902 dw2_find_pc_sect_symtab,
2903 dw2_map_symbol_filenames
2904 };
2905
2906 /* Initialize for reading DWARF for this objfile. Return 0 if this
2907 file will use psymtabs, or 1 if using the GNU index. */
2908
2909 int
2910 dwarf2_initialize_objfile (struct objfile *objfile)
2911 {
2912 /* If we're about to read full symbols, don't bother with the
2913 indices. In this case we also don't care if some other debug
2914 format is making psymtabs, because they are all about to be
2915 expanded anyway. */
2916 if ((objfile->flags & OBJF_READNOW))
2917 {
2918 int i;
2919
2920 dwarf2_per_objfile->using_index = 1;
2921 create_all_comp_units (objfile);
2922 create_debug_types_hash_table (objfile);
2923 dwarf2_per_objfile->quick_file_names_table =
2924 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2925
2926 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2927 + dwarf2_per_objfile->n_type_units); ++i)
2928 {
2929 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2930
2931 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2932 struct dwarf2_per_cu_quick_data);
2933 }
2934
2935 /* Return 1 so that gdb sees the "quick" functions. However,
2936 these functions will be no-ops because we will have expanded
2937 all symtabs. */
2938 return 1;
2939 }
2940
2941 if (dwarf2_read_index (objfile))
2942 return 1;
2943
2944 return 0;
2945 }
2946
2947 \f
2948
2949 /* Build a partial symbol table. */
2950
2951 void
2952 dwarf2_build_psymtabs (struct objfile *objfile)
2953 {
2954 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2955 {
2956 init_psymbol_list (objfile, 1024);
2957 }
2958
2959 dwarf2_build_psymtabs_hard (objfile);
2960 }
2961
2962 /* Return TRUE if OFFSET is within CU_HEADER. */
2963
2964 static inline int
2965 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2966 {
2967 unsigned int bottom = cu_header->offset;
2968 unsigned int top = (cu_header->offset
2969 + cu_header->length
2970 + cu_header->initial_length_size);
2971
2972 return (offset >= bottom && offset < top);
2973 }
2974
2975 /* Read in the comp unit header information from the debug_info at info_ptr.
2976 NOTE: This leaves members offset, first_die_offset to be filled in
2977 by the caller. */
2978
2979 static gdb_byte *
2980 read_comp_unit_head (struct comp_unit_head *cu_header,
2981 gdb_byte *info_ptr, bfd *abfd)
2982 {
2983 int signed_addr;
2984 unsigned int bytes_read;
2985
2986 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2987 cu_header->initial_length_size = bytes_read;
2988 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2989 info_ptr += bytes_read;
2990 cu_header->version = read_2_bytes (abfd, info_ptr);
2991 info_ptr += 2;
2992 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2993 &bytes_read);
2994 info_ptr += bytes_read;
2995 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2996 info_ptr += 1;
2997 signed_addr = bfd_get_sign_extend_vma (abfd);
2998 if (signed_addr < 0)
2999 internal_error (__FILE__, __LINE__,
3000 _("read_comp_unit_head: dwarf from non elf file"));
3001 cu_header->signed_addr_p = signed_addr;
3002
3003 return info_ptr;
3004 }
3005
3006 /* Subroutine of read_and_check_comp_unit_head and
3007 read_and_check_type_unit_head to simplify them.
3008 Perform various error checking on the header. */
3009
3010 static void
3011 error_check_comp_unit_head (struct comp_unit_head *header,
3012 struct dwarf2_section_info *section)
3013 {
3014 bfd *abfd = section->asection->owner;
3015 const char *filename = bfd_get_filename (abfd);
3016
3017 if (header->version != 2 && header->version != 3 && header->version != 4)
3018 error (_("Dwarf Error: wrong version in compilation unit header "
3019 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3020 filename);
3021
3022 if (header->abbrev_offset
3023 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3024 &dwarf2_per_objfile->abbrev))
3025 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3026 "(offset 0x%lx + 6) [in module %s]"),
3027 (long) header->abbrev_offset, (long) header->offset,
3028 filename);
3029
3030 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3031 avoid potential 32-bit overflow. */
3032 if (((unsigned long) header->offset
3033 + header->length + header->initial_length_size)
3034 > section->size)
3035 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3036 "(offset 0x%lx + 0) [in module %s]"),
3037 (long) header->length, (long) header->offset,
3038 filename);
3039 }
3040
3041 /* Read in a CU/TU header and perform some basic error checking.
3042 The contents of the header are stored in HEADER.
3043 The result is a pointer to the start of the first DIE. */
3044
3045 static gdb_byte *
3046 read_and_check_comp_unit_head (struct comp_unit_head *header,
3047 struct dwarf2_section_info *section,
3048 gdb_byte *info_ptr,
3049 int is_debug_types_section)
3050 {
3051 gdb_byte *beg_of_comp_unit = info_ptr;
3052 bfd *abfd = section->asection->owner;
3053
3054 header->offset = beg_of_comp_unit - section->buffer;
3055
3056 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3057
3058 /* If we're reading a type unit, skip over the signature and
3059 type_offset fields. */
3060 if (is_debug_types_section)
3061 info_ptr += 8 /*signature*/ + header->offset_size;
3062
3063 header->first_die_offset = info_ptr - beg_of_comp_unit;
3064
3065 error_check_comp_unit_head (header, section);
3066
3067 return info_ptr;
3068 }
3069
3070 /* Read in the types comp unit header information from .debug_types entry at
3071 types_ptr. The result is a pointer to one past the end of the header. */
3072
3073 static gdb_byte *
3074 read_and_check_type_unit_head (struct comp_unit_head *header,
3075 struct dwarf2_section_info *section,
3076 gdb_byte *info_ptr,
3077 ULONGEST *signature, unsigned int *type_offset)
3078 {
3079 gdb_byte *beg_of_comp_unit = info_ptr;
3080 bfd *abfd = section->asection->owner;
3081
3082 header->offset = beg_of_comp_unit - section->buffer;
3083
3084 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3085
3086 /* If we're reading a type unit, skip over the signature and
3087 type_offset fields. */
3088 if (signature != NULL)
3089 *signature = read_8_bytes (abfd, info_ptr);
3090 info_ptr += 8;
3091 if (type_offset != NULL)
3092 *type_offset = read_offset_1 (abfd, info_ptr, header->offset_size);
3093 info_ptr += header->offset_size;
3094
3095 header->first_die_offset = info_ptr - beg_of_comp_unit;
3096
3097 error_check_comp_unit_head (header, section);
3098
3099 return info_ptr;
3100 }
3101
3102 /* Allocate a new partial symtab for file named NAME and mark this new
3103 partial symtab as being an include of PST. */
3104
3105 static void
3106 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3107 struct objfile *objfile)
3108 {
3109 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3110
3111 subpst->section_offsets = pst->section_offsets;
3112 subpst->textlow = 0;
3113 subpst->texthigh = 0;
3114
3115 subpst->dependencies = (struct partial_symtab **)
3116 obstack_alloc (&objfile->objfile_obstack,
3117 sizeof (struct partial_symtab *));
3118 subpst->dependencies[0] = pst;
3119 subpst->number_of_dependencies = 1;
3120
3121 subpst->globals_offset = 0;
3122 subpst->n_global_syms = 0;
3123 subpst->statics_offset = 0;
3124 subpst->n_static_syms = 0;
3125 subpst->symtab = NULL;
3126 subpst->read_symtab = pst->read_symtab;
3127 subpst->readin = 0;
3128
3129 /* No private part is necessary for include psymtabs. This property
3130 can be used to differentiate between such include psymtabs and
3131 the regular ones. */
3132 subpst->read_symtab_private = NULL;
3133 }
3134
3135 /* Read the Line Number Program data and extract the list of files
3136 included by the source file represented by PST. Build an include
3137 partial symtab for each of these included files. */
3138
3139 static void
3140 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3141 struct die_info *die,
3142 struct partial_symtab *pst)
3143 {
3144 struct objfile *objfile = cu->objfile;
3145 bfd *abfd = objfile->obfd;
3146 struct line_header *lh = NULL;
3147 struct attribute *attr;
3148
3149 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3150 if (attr)
3151 {
3152 unsigned int line_offset = DW_UNSND (attr);
3153
3154 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3155 }
3156 if (lh == NULL)
3157 return; /* No linetable, so no includes. */
3158
3159 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3160 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3161
3162 free_line_header (lh);
3163 }
3164
3165 static hashval_t
3166 hash_type_signature (const void *item)
3167 {
3168 const struct signatured_type *type_sig = item;
3169
3170 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3171 return type_sig->signature;
3172 }
3173
3174 static int
3175 eq_type_signature (const void *item_lhs, const void *item_rhs)
3176 {
3177 const struct signatured_type *lhs = item_lhs;
3178 const struct signatured_type *rhs = item_rhs;
3179
3180 return lhs->signature == rhs->signature;
3181 }
3182
3183 /* Allocate a hash table for signatured types. */
3184
3185 static htab_t
3186 allocate_signatured_type_table (struct objfile *objfile)
3187 {
3188 return htab_create_alloc_ex (41,
3189 hash_type_signature,
3190 eq_type_signature,
3191 NULL,
3192 &objfile->objfile_obstack,
3193 hashtab_obstack_allocate,
3194 dummy_obstack_deallocate);
3195 }
3196
3197 /* A helper function to add a signatured type CU to a table. */
3198
3199 static int
3200 add_signatured_type_cu_to_table (void **slot, void *datum)
3201 {
3202 struct signatured_type *sigt = *slot;
3203 struct dwarf2_per_cu_data ***datap = datum;
3204
3205 **datap = &sigt->per_cu;
3206 ++*datap;
3207
3208 return 1;
3209 }
3210
3211 /* Create the hash table of all entries in the .debug_types section(s).
3212 The result is zero if there is an error (e.g. missing .debug_types section),
3213 otherwise non-zero. */
3214
3215 static int
3216 create_debug_types_hash_table (struct objfile *objfile)
3217 {
3218 htab_t types_htab = NULL;
3219 struct dwarf2_per_cu_data **iter;
3220 int ix;
3221 struct dwarf2_section_info *section;
3222
3223 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3224 {
3225 dwarf2_per_objfile->signatured_types = NULL;
3226 return 0;
3227 }
3228
3229 for (ix = 0;
3230 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3231 ix, section);
3232 ++ix)
3233 {
3234 gdb_byte *info_ptr, *end_ptr;
3235
3236 dwarf2_read_section (objfile, section);
3237 info_ptr = section->buffer;
3238
3239 if (info_ptr == NULL)
3240 continue;
3241
3242 if (types_htab == NULL)
3243 types_htab = allocate_signatured_type_table (objfile);
3244
3245 if (dwarf2_die_debug)
3246 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3247
3248 end_ptr = info_ptr + section->size;
3249 while (info_ptr < end_ptr)
3250 {
3251 unsigned int offset;
3252 unsigned int type_offset;
3253 ULONGEST signature;
3254 struct signatured_type *type_sig;
3255 void **slot;
3256 gdb_byte *ptr = info_ptr;
3257 struct comp_unit_head header;
3258
3259 offset = ptr - section->buffer;
3260
3261 /* We need to read the type's signature in order to build the hash
3262 table, but we don't need anything else just yet. */
3263
3264 ptr = read_and_check_type_unit_head (&header, section, ptr,
3265 &signature, &type_offset);
3266
3267 /* Skip dummy type units. */
3268 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3269 {
3270 info_ptr = info_ptr + header.initial_length_size + header.length;
3271 continue;
3272 }
3273
3274 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3275 memset (type_sig, 0, sizeof (*type_sig));
3276 type_sig->signature = signature;
3277 type_sig->type_offset = type_offset;
3278 type_sig->per_cu.objfile = objfile;
3279 type_sig->per_cu.debug_types_section = section;
3280 type_sig->per_cu.offset = offset;
3281
3282 slot = htab_find_slot (types_htab, type_sig, INSERT);
3283 gdb_assert (slot != NULL);
3284 if (*slot != NULL)
3285 {
3286 const struct signatured_type *dup_sig = *slot;
3287
3288 complaint (&symfile_complaints,
3289 _("debug type entry at offset 0x%x is duplicate to the "
3290 "entry at offset 0x%x, signature 0x%s"),
3291 offset, dup_sig->per_cu.offset,
3292 phex (signature, sizeof (signature)));
3293 gdb_assert (signature == dup_sig->signature);
3294 }
3295 *slot = type_sig;
3296
3297 if (dwarf2_die_debug)
3298 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3299 offset, phex (signature, sizeof (signature)));
3300
3301 info_ptr = info_ptr + header.initial_length_size + header.length;
3302 }
3303 }
3304
3305 dwarf2_per_objfile->signatured_types = types_htab;
3306
3307 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3308 dwarf2_per_objfile->all_type_units
3309 = obstack_alloc (&objfile->objfile_obstack,
3310 dwarf2_per_objfile->n_type_units
3311 * sizeof (struct dwarf2_per_cu_data *));
3312 iter = &dwarf2_per_objfile->all_type_units[0];
3313 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3314 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3315 == dwarf2_per_objfile->n_type_units);
3316
3317 return 1;
3318 }
3319
3320 /* Lookup a signature based type.
3321 Returns NULL if SIG is not present in the table. */
3322
3323 static struct signatured_type *
3324 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3325 {
3326 struct signatured_type find_entry, *entry;
3327
3328 if (dwarf2_per_objfile->signatured_types == NULL)
3329 {
3330 complaint (&symfile_complaints,
3331 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3332 return 0;
3333 }
3334
3335 find_entry.signature = sig;
3336 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3337 return entry;
3338 }
3339
3340 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3341
3342 static void
3343 init_cu_die_reader (struct die_reader_specs *reader,
3344 struct dwarf2_cu *cu)
3345 {
3346 reader->abfd = cu->objfile->obfd;
3347 reader->cu = cu;
3348 if (cu->per_cu->debug_types_section)
3349 {
3350 gdb_assert (cu->per_cu->debug_types_section->readin);
3351 reader->buffer = cu->per_cu->debug_types_section->buffer;
3352 }
3353 else
3354 {
3355 gdb_assert (dwarf2_per_objfile->info.readin);
3356 reader->buffer = dwarf2_per_objfile->info.buffer;
3357 }
3358 }
3359
3360 /* Find the base address of the compilation unit for range lists and
3361 location lists. It will normally be specified by DW_AT_low_pc.
3362 In DWARF-3 draft 4, the base address could be overridden by
3363 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3364 compilation units with discontinuous ranges. */
3365
3366 static void
3367 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3368 {
3369 struct attribute *attr;
3370
3371 cu->base_known = 0;
3372 cu->base_address = 0;
3373
3374 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3375 if (attr)
3376 {
3377 cu->base_address = DW_ADDR (attr);
3378 cu->base_known = 1;
3379 }
3380 else
3381 {
3382 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3383 if (attr)
3384 {
3385 cu->base_address = DW_ADDR (attr);
3386 cu->base_known = 1;
3387 }
3388 }
3389 }
3390
3391 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3392 to combine the common parts.
3393 Process compilation unit THIS_CU for a psymtab.
3394 SECTION is the section the CU/TU comes from,
3395 either .debug_info or .debug_types. */
3396
3397 static void
3398 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3399 struct dwarf2_section_info *section,
3400 int is_debug_types_section)
3401 {
3402 struct objfile *objfile = this_cu->objfile;
3403 bfd *abfd = objfile->obfd;
3404 gdb_byte *buffer = section->buffer;
3405 gdb_byte *info_ptr = buffer + this_cu->offset;
3406 unsigned int buffer_size = section->size;
3407 gdb_byte *beg_of_comp_unit = info_ptr;
3408 struct die_info *comp_unit_die;
3409 struct partial_symtab *pst;
3410 CORE_ADDR baseaddr;
3411 struct cleanup *back_to_inner;
3412 struct dwarf2_cu cu;
3413 int has_children, has_pc_info;
3414 struct attribute *attr;
3415 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3416 struct die_reader_specs reader_specs;
3417 const char *filename;
3418
3419 /* If this compilation unit was already read in, free the
3420 cached copy in order to read it in again. This is
3421 necessary because we skipped some symbols when we first
3422 read in the compilation unit (see load_partial_dies).
3423 This problem could be avoided, but the benefit is
3424 unclear. */
3425 if (this_cu->cu != NULL)
3426 free_one_cached_comp_unit (this_cu->cu);
3427
3428 /* Note that this is a pointer to our stack frame, being
3429 added to a global data structure. It will be cleaned up
3430 in free_stack_comp_unit when we finish with this
3431 compilation unit. */
3432 init_one_comp_unit (&cu, this_cu);
3433 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3434
3435 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3436 is_debug_types_section);
3437
3438 /* Skip dummy compilation units. */
3439 if (info_ptr >= buffer + buffer_size
3440 || peek_abbrev_code (abfd, info_ptr) == 0)
3441 {
3442 do_cleanups (back_to_inner);
3443 return;
3444 }
3445
3446 cu.list_in_scope = &file_symbols;
3447
3448 /* Read the abbrevs for this compilation unit into a table. */
3449 dwarf2_read_abbrevs (&cu);
3450 make_cleanup (dwarf2_free_abbrev_table, &cu);
3451
3452 /* Read the compilation unit die. */
3453 init_cu_die_reader (&reader_specs, &cu);
3454 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3455 &has_children);
3456
3457 if (is_debug_types_section)
3458 {
3459 /* LENGTH has not been set yet for type units. */
3460 gdb_assert (this_cu->offset == cu.header.offset);
3461 this_cu->length = cu.header.length + cu.header.initial_length_size;
3462 }
3463 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3464 {
3465 do_cleanups (back_to_inner);
3466 return;
3467 }
3468
3469 prepare_one_comp_unit (&cu, comp_unit_die);
3470
3471 /* Allocate a new partial symbol table structure. */
3472 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3473 if (attr == NULL || !DW_STRING (attr))
3474 filename = "";
3475 else
3476 filename = DW_STRING (attr);
3477 pst = start_psymtab_common (objfile, objfile->section_offsets,
3478 filename,
3479 /* TEXTLOW and TEXTHIGH are set below. */
3480 0,
3481 objfile->global_psymbols.next,
3482 objfile->static_psymbols.next);
3483 pst->psymtabs_addrmap_supported = 1;
3484
3485 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3486 if (attr != NULL)
3487 pst->dirname = DW_STRING (attr);
3488
3489 pst->read_symtab_private = this_cu;
3490
3491 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3492
3493 /* Store the function that reads in the rest of the symbol table. */
3494 pst->read_symtab = dwarf2_psymtab_to_symtab;
3495
3496 this_cu->v.psymtab = pst;
3497
3498 dwarf2_find_base_address (comp_unit_die, &cu);
3499
3500 /* Possibly set the default values of LOWPC and HIGHPC from
3501 `DW_AT_ranges'. */
3502 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3503 &best_highpc, &cu, pst);
3504 if (has_pc_info == 1 && best_lowpc < best_highpc)
3505 /* Store the contiguous range if it is not empty; it can be empty for
3506 CUs with no code. */
3507 addrmap_set_empty (objfile->psymtabs_addrmap,
3508 best_lowpc + baseaddr,
3509 best_highpc + baseaddr - 1, pst);
3510
3511 /* Check if comp unit has_children.
3512 If so, read the rest of the partial symbols from this comp unit.
3513 If not, there's no more debug_info for this comp unit. */
3514 if (has_children)
3515 {
3516 struct partial_die_info *first_die;
3517 CORE_ADDR lowpc, highpc;
3518
3519 lowpc = ((CORE_ADDR) -1);
3520 highpc = ((CORE_ADDR) 0);
3521
3522 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3523
3524 scan_partial_symbols (first_die, &lowpc, &highpc,
3525 ! has_pc_info, &cu);
3526
3527 /* If we didn't find a lowpc, set it to highpc to avoid
3528 complaints from `maint check'. */
3529 if (lowpc == ((CORE_ADDR) -1))
3530 lowpc = highpc;
3531
3532 /* If the compilation unit didn't have an explicit address range,
3533 then use the information extracted from its child dies. */
3534 if (! has_pc_info)
3535 {
3536 best_lowpc = lowpc;
3537 best_highpc = highpc;
3538 }
3539 }
3540 pst->textlow = best_lowpc + baseaddr;
3541 pst->texthigh = best_highpc + baseaddr;
3542
3543 pst->n_global_syms = objfile->global_psymbols.next -
3544 (objfile->global_psymbols.list + pst->globals_offset);
3545 pst->n_static_syms = objfile->static_psymbols.next -
3546 (objfile->static_psymbols.list + pst->statics_offset);
3547 sort_pst_symbols (pst);
3548
3549 if (is_debug_types_section)
3550 {
3551 /* It's not clear we want to do anything with stmt lists here.
3552 Waiting to see what gcc ultimately does. */
3553 }
3554 else
3555 {
3556 /* Get the list of files included in the current compilation unit,
3557 and build a psymtab for each of them. */
3558 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3559 }
3560
3561 do_cleanups (back_to_inner);
3562 }
3563
3564 /* Traversal function for htab_traverse_noresize.
3565 Process one .debug_types comp-unit. */
3566
3567 static int
3568 process_type_comp_unit (void **slot, void *info)
3569 {
3570 struct signatured_type *entry = (struct signatured_type *) *slot;
3571 struct dwarf2_per_cu_data *this_cu;
3572
3573 gdb_assert (info == NULL);
3574 this_cu = &entry->per_cu;
3575
3576 gdb_assert (this_cu->debug_types_section->readin);
3577 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3578
3579 return 1;
3580 }
3581
3582 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3583 Build partial symbol tables for the .debug_types comp-units. */
3584
3585 static void
3586 build_type_psymtabs (struct objfile *objfile)
3587 {
3588 if (! create_debug_types_hash_table (objfile))
3589 return;
3590
3591 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3592 process_type_comp_unit, NULL);
3593 }
3594
3595 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3596
3597 static void
3598 psymtabs_addrmap_cleanup (void *o)
3599 {
3600 struct objfile *objfile = o;
3601
3602 objfile->psymtabs_addrmap = NULL;
3603 }
3604
3605 /* Build the partial symbol table by doing a quick pass through the
3606 .debug_info and .debug_abbrev sections. */
3607
3608 static void
3609 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3610 {
3611 struct cleanup *back_to, *addrmap_cleanup;
3612 struct obstack temp_obstack;
3613 int i;
3614
3615 dwarf2_per_objfile->reading_partial_symbols = 1;
3616
3617 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3618
3619 /* Any cached compilation units will be linked by the per-objfile
3620 read_in_chain. Make sure to free them when we're done. */
3621 back_to = make_cleanup (free_cached_comp_units, NULL);
3622
3623 build_type_psymtabs (objfile);
3624
3625 create_all_comp_units (objfile);
3626
3627 /* Create a temporary address map on a temporary obstack. We later
3628 copy this to the final obstack. */
3629 obstack_init (&temp_obstack);
3630 make_cleanup_obstack_free (&temp_obstack);
3631 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3632 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3633
3634 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3635 {
3636 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3637
3638 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3639 }
3640
3641 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3642 &objfile->objfile_obstack);
3643 discard_cleanups (addrmap_cleanup);
3644
3645 do_cleanups (back_to);
3646 }
3647
3648 /* Load the partial DIEs for a secondary CU into memory. */
3649
3650 static void
3651 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3652 {
3653 struct objfile *objfile = this_cu->objfile;
3654 bfd *abfd = objfile->obfd;
3655 gdb_byte *info_ptr;
3656 struct die_info *comp_unit_die;
3657 struct dwarf2_cu *cu;
3658 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3659 int has_children;
3660 struct die_reader_specs reader_specs;
3661 int read_cu = 0;
3662 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
3663
3664 gdb_assert (! this_cu->debug_types_section);
3665
3666 gdb_assert (section->readin);
3667 info_ptr = section->buffer + this_cu->offset;
3668
3669 if (this_cu->cu == NULL)
3670 {
3671 cu = xmalloc (sizeof (*cu));
3672 init_one_comp_unit (cu, this_cu);
3673
3674 read_cu = 1;
3675
3676 /* If an error occurs while loading, release our storage. */
3677 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3678
3679 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3680 0);
3681
3682 /* Skip dummy compilation units. */
3683 if (info_ptr >= (section->buffer + section->size)
3684 || peek_abbrev_code (abfd, info_ptr) == 0)
3685 {
3686 do_cleanups (free_cu_cleanup);
3687 return;
3688 }
3689 }
3690 else
3691 {
3692 cu = this_cu->cu;
3693 info_ptr += cu->header.first_die_offset;
3694 }
3695
3696 /* Read the abbrevs for this compilation unit into a table. */
3697 gdb_assert (cu->dwarf2_abbrevs == NULL);
3698 dwarf2_read_abbrevs (cu);
3699 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3700
3701 /* Read the compilation unit die. */
3702 init_cu_die_reader (&reader_specs, cu);
3703 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3704 &has_children);
3705
3706 prepare_one_comp_unit (cu, comp_unit_die);
3707
3708 /* Check if comp unit has_children.
3709 If so, read the rest of the partial symbols from this comp unit.
3710 If not, there's no more debug_info for this comp unit. */
3711 if (has_children)
3712 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
3713
3714 do_cleanups (free_abbrevs_cleanup);
3715
3716 if (read_cu)
3717 {
3718 /* We've successfully allocated this compilation unit. Let our
3719 caller clean it up when finished with it. */
3720 discard_cleanups (free_cu_cleanup);
3721
3722 /* Link this CU into read_in_chain. */
3723 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3724 dwarf2_per_objfile->read_in_chain = this_cu;
3725 }
3726 }
3727
3728 /* Create a list of all compilation units in OBJFILE.
3729 This is only done for -readnow and building partial symtabs. */
3730
3731 static void
3732 create_all_comp_units (struct objfile *objfile)
3733 {
3734 int n_allocated;
3735 int n_comp_units;
3736 struct dwarf2_per_cu_data **all_comp_units;
3737 gdb_byte *info_ptr;
3738
3739 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3740 info_ptr = dwarf2_per_objfile->info.buffer;
3741
3742 n_comp_units = 0;
3743 n_allocated = 10;
3744 all_comp_units = xmalloc (n_allocated
3745 * sizeof (struct dwarf2_per_cu_data *));
3746
3747 while (info_ptr < dwarf2_per_objfile->info.buffer
3748 + dwarf2_per_objfile->info.size)
3749 {
3750 unsigned int length, initial_length_size;
3751 struct dwarf2_per_cu_data *this_cu;
3752 unsigned int offset;
3753
3754 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3755
3756 /* Read just enough information to find out where the next
3757 compilation unit is. */
3758 length = read_initial_length (objfile->obfd, info_ptr,
3759 &initial_length_size);
3760
3761 /* Save the compilation unit for later lookup. */
3762 this_cu = obstack_alloc (&objfile->objfile_obstack,
3763 sizeof (struct dwarf2_per_cu_data));
3764 memset (this_cu, 0, sizeof (*this_cu));
3765 this_cu->offset = offset;
3766 this_cu->length = length + initial_length_size;
3767 this_cu->objfile = objfile;
3768
3769 if (n_comp_units == n_allocated)
3770 {
3771 n_allocated *= 2;
3772 all_comp_units = xrealloc (all_comp_units,
3773 n_allocated
3774 * sizeof (struct dwarf2_per_cu_data *));
3775 }
3776 all_comp_units[n_comp_units++] = this_cu;
3777
3778 info_ptr = info_ptr + this_cu->length;
3779 }
3780
3781 dwarf2_per_objfile->all_comp_units
3782 = obstack_alloc (&objfile->objfile_obstack,
3783 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3784 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3785 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3786 xfree (all_comp_units);
3787 dwarf2_per_objfile->n_comp_units = n_comp_units;
3788 }
3789
3790 /* Process all loaded DIEs for compilation unit CU, starting at
3791 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3792 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3793 DW_AT_ranges). If NEED_PC is set, then this function will set
3794 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3795 and record the covered ranges in the addrmap. */
3796
3797 static void
3798 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3799 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3800 {
3801 struct partial_die_info *pdi;
3802
3803 /* Now, march along the PDI's, descending into ones which have
3804 interesting children but skipping the children of the other ones,
3805 until we reach the end of the compilation unit. */
3806
3807 pdi = first_die;
3808
3809 while (pdi != NULL)
3810 {
3811 fixup_partial_die (pdi, cu);
3812
3813 /* Anonymous namespaces or modules have no name but have interesting
3814 children, so we need to look at them. Ditto for anonymous
3815 enums. */
3816
3817 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3818 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3819 {
3820 switch (pdi->tag)
3821 {
3822 case DW_TAG_subprogram:
3823 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3824 break;
3825 case DW_TAG_constant:
3826 case DW_TAG_variable:
3827 case DW_TAG_typedef:
3828 case DW_TAG_union_type:
3829 if (!pdi->is_declaration)
3830 {
3831 add_partial_symbol (pdi, cu);
3832 }
3833 break;
3834 case DW_TAG_class_type:
3835 case DW_TAG_interface_type:
3836 case DW_TAG_structure_type:
3837 if (!pdi->is_declaration)
3838 {
3839 add_partial_symbol (pdi, cu);
3840 }
3841 break;
3842 case DW_TAG_enumeration_type:
3843 if (!pdi->is_declaration)
3844 add_partial_enumeration (pdi, cu);
3845 break;
3846 case DW_TAG_base_type:
3847 case DW_TAG_subrange_type:
3848 /* File scope base type definitions are added to the partial
3849 symbol table. */
3850 add_partial_symbol (pdi, cu);
3851 break;
3852 case DW_TAG_namespace:
3853 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3854 break;
3855 case DW_TAG_module:
3856 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3857 break;
3858 default:
3859 break;
3860 }
3861 }
3862
3863 /* If the die has a sibling, skip to the sibling. */
3864
3865 pdi = pdi->die_sibling;
3866 }
3867 }
3868
3869 /* Functions used to compute the fully scoped name of a partial DIE.
3870
3871 Normally, this is simple. For C++, the parent DIE's fully scoped
3872 name is concatenated with "::" and the partial DIE's name. For
3873 Java, the same thing occurs except that "." is used instead of "::".
3874 Enumerators are an exception; they use the scope of their parent
3875 enumeration type, i.e. the name of the enumeration type is not
3876 prepended to the enumerator.
3877
3878 There are two complexities. One is DW_AT_specification; in this
3879 case "parent" means the parent of the target of the specification,
3880 instead of the direct parent of the DIE. The other is compilers
3881 which do not emit DW_TAG_namespace; in this case we try to guess
3882 the fully qualified name of structure types from their members'
3883 linkage names. This must be done using the DIE's children rather
3884 than the children of any DW_AT_specification target. We only need
3885 to do this for structures at the top level, i.e. if the target of
3886 any DW_AT_specification (if any; otherwise the DIE itself) does not
3887 have a parent. */
3888
3889 /* Compute the scope prefix associated with PDI's parent, in
3890 compilation unit CU. The result will be allocated on CU's
3891 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3892 field. NULL is returned if no prefix is necessary. */
3893 static char *
3894 partial_die_parent_scope (struct partial_die_info *pdi,
3895 struct dwarf2_cu *cu)
3896 {
3897 char *grandparent_scope;
3898 struct partial_die_info *parent, *real_pdi;
3899
3900 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3901 then this means the parent of the specification DIE. */
3902
3903 real_pdi = pdi;
3904 while (real_pdi->has_specification)
3905 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3906
3907 parent = real_pdi->die_parent;
3908 if (parent == NULL)
3909 return NULL;
3910
3911 if (parent->scope_set)
3912 return parent->scope;
3913
3914 fixup_partial_die (parent, cu);
3915
3916 grandparent_scope = partial_die_parent_scope (parent, cu);
3917
3918 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3919 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3920 Work around this problem here. */
3921 if (cu->language == language_cplus
3922 && parent->tag == DW_TAG_namespace
3923 && strcmp (parent->name, "::") == 0
3924 && grandparent_scope == NULL)
3925 {
3926 parent->scope = NULL;
3927 parent->scope_set = 1;
3928 return NULL;
3929 }
3930
3931 if (pdi->tag == DW_TAG_enumerator)
3932 /* Enumerators should not get the name of the enumeration as a prefix. */
3933 parent->scope = grandparent_scope;
3934 else if (parent->tag == DW_TAG_namespace
3935 || parent->tag == DW_TAG_module
3936 || parent->tag == DW_TAG_structure_type
3937 || parent->tag == DW_TAG_class_type
3938 || parent->tag == DW_TAG_interface_type
3939 || parent->tag == DW_TAG_union_type
3940 || parent->tag == DW_TAG_enumeration_type)
3941 {
3942 if (grandparent_scope == NULL)
3943 parent->scope = parent->name;
3944 else
3945 parent->scope = typename_concat (&cu->comp_unit_obstack,
3946 grandparent_scope,
3947 parent->name, 0, cu);
3948 }
3949 else
3950 {
3951 /* FIXME drow/2004-04-01: What should we be doing with
3952 function-local names? For partial symbols, we should probably be
3953 ignoring them. */
3954 complaint (&symfile_complaints,
3955 _("unhandled containing DIE tag %d for DIE at %d"),
3956 parent->tag, pdi->offset);
3957 parent->scope = grandparent_scope;
3958 }
3959
3960 parent->scope_set = 1;
3961 return parent->scope;
3962 }
3963
3964 /* Return the fully scoped name associated with PDI, from compilation unit
3965 CU. The result will be allocated with malloc. */
3966 static char *
3967 partial_die_full_name (struct partial_die_info *pdi,
3968 struct dwarf2_cu *cu)
3969 {
3970 char *parent_scope;
3971
3972 /* If this is a template instantiation, we can not work out the
3973 template arguments from partial DIEs. So, unfortunately, we have
3974 to go through the full DIEs. At least any work we do building
3975 types here will be reused if full symbols are loaded later. */
3976 if (pdi->has_template_arguments)
3977 {
3978 fixup_partial_die (pdi, cu);
3979
3980 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3981 {
3982 struct die_info *die;
3983 struct attribute attr;
3984 struct dwarf2_cu *ref_cu = cu;
3985
3986 attr.name = 0;
3987 attr.form = DW_FORM_ref_addr;
3988 attr.u.addr = pdi->offset;
3989 die = follow_die_ref (NULL, &attr, &ref_cu);
3990
3991 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3992 }
3993 }
3994
3995 parent_scope = partial_die_parent_scope (pdi, cu);
3996 if (parent_scope == NULL)
3997 return NULL;
3998 else
3999 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4000 }
4001
4002 static void
4003 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4004 {
4005 struct objfile *objfile = cu->objfile;
4006 CORE_ADDR addr = 0;
4007 char *actual_name = NULL;
4008 CORE_ADDR baseaddr;
4009 int built_actual_name = 0;
4010
4011 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4012
4013 actual_name = partial_die_full_name (pdi, cu);
4014 if (actual_name)
4015 built_actual_name = 1;
4016
4017 if (actual_name == NULL)
4018 actual_name = pdi->name;
4019
4020 switch (pdi->tag)
4021 {
4022 case DW_TAG_subprogram:
4023 if (pdi->is_external || cu->language == language_ada)
4024 {
4025 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4026 of the global scope. But in Ada, we want to be able to access
4027 nested procedures globally. So all Ada subprograms are stored
4028 in the global scope. */
4029 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4030 mst_text, objfile); */
4031 add_psymbol_to_list (actual_name, strlen (actual_name),
4032 built_actual_name,
4033 VAR_DOMAIN, LOC_BLOCK,
4034 &objfile->global_psymbols,
4035 0, pdi->lowpc + baseaddr,
4036 cu->language, objfile);
4037 }
4038 else
4039 {
4040 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4041 mst_file_text, objfile); */
4042 add_psymbol_to_list (actual_name, strlen (actual_name),
4043 built_actual_name,
4044 VAR_DOMAIN, LOC_BLOCK,
4045 &objfile->static_psymbols,
4046 0, pdi->lowpc + baseaddr,
4047 cu->language, objfile);
4048 }
4049 break;
4050 case DW_TAG_constant:
4051 {
4052 struct psymbol_allocation_list *list;
4053
4054 if (pdi->is_external)
4055 list = &objfile->global_psymbols;
4056 else
4057 list = &objfile->static_psymbols;
4058 add_psymbol_to_list (actual_name, strlen (actual_name),
4059 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4060 list, 0, 0, cu->language, objfile);
4061 }
4062 break;
4063 case DW_TAG_variable:
4064 if (pdi->locdesc)
4065 addr = decode_locdesc (pdi->locdesc, cu);
4066
4067 if (pdi->locdesc
4068 && addr == 0
4069 && !dwarf2_per_objfile->has_section_at_zero)
4070 {
4071 /* A global or static variable may also have been stripped
4072 out by the linker if unused, in which case its address
4073 will be nullified; do not add such variables into partial
4074 symbol table then. */
4075 }
4076 else if (pdi->is_external)
4077 {
4078 /* Global Variable.
4079 Don't enter into the minimal symbol tables as there is
4080 a minimal symbol table entry from the ELF symbols already.
4081 Enter into partial symbol table if it has a location
4082 descriptor or a type.
4083 If the location descriptor is missing, new_symbol will create
4084 a LOC_UNRESOLVED symbol, the address of the variable will then
4085 be determined from the minimal symbol table whenever the variable
4086 is referenced.
4087 The address for the partial symbol table entry is not
4088 used by GDB, but it comes in handy for debugging partial symbol
4089 table building. */
4090
4091 if (pdi->locdesc || pdi->has_type)
4092 add_psymbol_to_list (actual_name, strlen (actual_name),
4093 built_actual_name,
4094 VAR_DOMAIN, LOC_STATIC,
4095 &objfile->global_psymbols,
4096 0, addr + baseaddr,
4097 cu->language, objfile);
4098 }
4099 else
4100 {
4101 /* Static Variable. Skip symbols without location descriptors. */
4102 if (pdi->locdesc == NULL)
4103 {
4104 if (built_actual_name)
4105 xfree (actual_name);
4106 return;
4107 }
4108 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4109 mst_file_data, objfile); */
4110 add_psymbol_to_list (actual_name, strlen (actual_name),
4111 built_actual_name,
4112 VAR_DOMAIN, LOC_STATIC,
4113 &objfile->static_psymbols,
4114 0, addr + baseaddr,
4115 cu->language, objfile);
4116 }
4117 break;
4118 case DW_TAG_typedef:
4119 case DW_TAG_base_type:
4120 case DW_TAG_subrange_type:
4121 add_psymbol_to_list (actual_name, strlen (actual_name),
4122 built_actual_name,
4123 VAR_DOMAIN, LOC_TYPEDEF,
4124 &objfile->static_psymbols,
4125 0, (CORE_ADDR) 0, cu->language, objfile);
4126 break;
4127 case DW_TAG_namespace:
4128 add_psymbol_to_list (actual_name, strlen (actual_name),
4129 built_actual_name,
4130 VAR_DOMAIN, LOC_TYPEDEF,
4131 &objfile->global_psymbols,
4132 0, (CORE_ADDR) 0, cu->language, objfile);
4133 break;
4134 case DW_TAG_class_type:
4135 case DW_TAG_interface_type:
4136 case DW_TAG_structure_type:
4137 case DW_TAG_union_type:
4138 case DW_TAG_enumeration_type:
4139 /* Skip external references. The DWARF standard says in the section
4140 about "Structure, Union, and Class Type Entries": "An incomplete
4141 structure, union or class type is represented by a structure,
4142 union or class entry that does not have a byte size attribute
4143 and that has a DW_AT_declaration attribute." */
4144 if (!pdi->has_byte_size && pdi->is_declaration)
4145 {
4146 if (built_actual_name)
4147 xfree (actual_name);
4148 return;
4149 }
4150
4151 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4152 static vs. global. */
4153 add_psymbol_to_list (actual_name, strlen (actual_name),
4154 built_actual_name,
4155 STRUCT_DOMAIN, LOC_TYPEDEF,
4156 (cu->language == language_cplus
4157 || cu->language == language_java)
4158 ? &objfile->global_psymbols
4159 : &objfile->static_psymbols,
4160 0, (CORE_ADDR) 0, cu->language, objfile);
4161
4162 break;
4163 case DW_TAG_enumerator:
4164 add_psymbol_to_list (actual_name, strlen (actual_name),
4165 built_actual_name,
4166 VAR_DOMAIN, LOC_CONST,
4167 (cu->language == language_cplus
4168 || cu->language == language_java)
4169 ? &objfile->global_psymbols
4170 : &objfile->static_psymbols,
4171 0, (CORE_ADDR) 0, cu->language, objfile);
4172 break;
4173 default:
4174 break;
4175 }
4176
4177 if (built_actual_name)
4178 xfree (actual_name);
4179 }
4180
4181 /* Read a partial die corresponding to a namespace; also, add a symbol
4182 corresponding to that namespace to the symbol table. NAMESPACE is
4183 the name of the enclosing namespace. */
4184
4185 static void
4186 add_partial_namespace (struct partial_die_info *pdi,
4187 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4188 int need_pc, struct dwarf2_cu *cu)
4189 {
4190 /* Add a symbol for the namespace. */
4191
4192 add_partial_symbol (pdi, cu);
4193
4194 /* Now scan partial symbols in that namespace. */
4195
4196 if (pdi->has_children)
4197 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4198 }
4199
4200 /* Read a partial die corresponding to a Fortran module. */
4201
4202 static void
4203 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4204 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4205 {
4206 /* Now scan partial symbols in that module. */
4207
4208 if (pdi->has_children)
4209 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4210 }
4211
4212 /* Read a partial die corresponding to a subprogram and create a partial
4213 symbol for that subprogram. When the CU language allows it, this
4214 routine also defines a partial symbol for each nested subprogram
4215 that this subprogram contains.
4216
4217 DIE my also be a lexical block, in which case we simply search
4218 recursively for suprograms defined inside that lexical block.
4219 Again, this is only performed when the CU language allows this
4220 type of definitions. */
4221
4222 static void
4223 add_partial_subprogram (struct partial_die_info *pdi,
4224 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4225 int need_pc, struct dwarf2_cu *cu)
4226 {
4227 if (pdi->tag == DW_TAG_subprogram)
4228 {
4229 if (pdi->has_pc_info)
4230 {
4231 if (pdi->lowpc < *lowpc)
4232 *lowpc = pdi->lowpc;
4233 if (pdi->highpc > *highpc)
4234 *highpc = pdi->highpc;
4235 if (need_pc)
4236 {
4237 CORE_ADDR baseaddr;
4238 struct objfile *objfile = cu->objfile;
4239
4240 baseaddr = ANOFFSET (objfile->section_offsets,
4241 SECT_OFF_TEXT (objfile));
4242 addrmap_set_empty (objfile->psymtabs_addrmap,
4243 pdi->lowpc + baseaddr,
4244 pdi->highpc - 1 + baseaddr,
4245 cu->per_cu->v.psymtab);
4246 }
4247 if (!pdi->is_declaration)
4248 /* Ignore subprogram DIEs that do not have a name, they are
4249 illegal. Do not emit a complaint at this point, we will
4250 do so when we convert this psymtab into a symtab. */
4251 if (pdi->name)
4252 add_partial_symbol (pdi, cu);
4253 }
4254 }
4255
4256 if (! pdi->has_children)
4257 return;
4258
4259 if (cu->language == language_ada)
4260 {
4261 pdi = pdi->die_child;
4262 while (pdi != NULL)
4263 {
4264 fixup_partial_die (pdi, cu);
4265 if (pdi->tag == DW_TAG_subprogram
4266 || pdi->tag == DW_TAG_lexical_block)
4267 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4268 pdi = pdi->die_sibling;
4269 }
4270 }
4271 }
4272
4273 /* Read a partial die corresponding to an enumeration type. */
4274
4275 static void
4276 add_partial_enumeration (struct partial_die_info *enum_pdi,
4277 struct dwarf2_cu *cu)
4278 {
4279 struct partial_die_info *pdi;
4280
4281 if (enum_pdi->name != NULL)
4282 add_partial_symbol (enum_pdi, cu);
4283
4284 pdi = enum_pdi->die_child;
4285 while (pdi)
4286 {
4287 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4288 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4289 else
4290 add_partial_symbol (pdi, cu);
4291 pdi = pdi->die_sibling;
4292 }
4293 }
4294
4295 /* Return the initial uleb128 in the die at INFO_PTR. */
4296
4297 static unsigned int
4298 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4299 {
4300 unsigned int bytes_read;
4301
4302 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4303 }
4304
4305 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4306 Return the corresponding abbrev, or NULL if the number is zero (indicating
4307 an empty DIE). In either case *BYTES_READ will be set to the length of
4308 the initial number. */
4309
4310 static struct abbrev_info *
4311 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4312 struct dwarf2_cu *cu)
4313 {
4314 bfd *abfd = cu->objfile->obfd;
4315 unsigned int abbrev_number;
4316 struct abbrev_info *abbrev;
4317
4318 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4319
4320 if (abbrev_number == 0)
4321 return NULL;
4322
4323 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4324 if (!abbrev)
4325 {
4326 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4327 abbrev_number, bfd_get_filename (abfd));
4328 }
4329
4330 return abbrev;
4331 }
4332
4333 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4334 Returns a pointer to the end of a series of DIEs, terminated by an empty
4335 DIE. Any children of the skipped DIEs will also be skipped. */
4336
4337 static gdb_byte *
4338 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4339 {
4340 struct abbrev_info *abbrev;
4341 unsigned int bytes_read;
4342
4343 while (1)
4344 {
4345 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4346 if (abbrev == NULL)
4347 return info_ptr + bytes_read;
4348 else
4349 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4350 }
4351 }
4352
4353 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4354 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4355 abbrev corresponding to that skipped uleb128 should be passed in
4356 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4357 children. */
4358
4359 static gdb_byte *
4360 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4361 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4362 {
4363 unsigned int bytes_read;
4364 struct attribute attr;
4365 bfd *abfd = cu->objfile->obfd;
4366 unsigned int form, i;
4367
4368 for (i = 0; i < abbrev->num_attrs; i++)
4369 {
4370 /* The only abbrev we care about is DW_AT_sibling. */
4371 if (abbrev->attrs[i].name == DW_AT_sibling)
4372 {
4373 read_attribute (&attr, &abbrev->attrs[i],
4374 abfd, info_ptr, cu);
4375 if (attr.form == DW_FORM_ref_addr)
4376 complaint (&symfile_complaints,
4377 _("ignoring absolute DW_AT_sibling"));
4378 else
4379 return buffer + dwarf2_get_ref_die_offset (&attr);
4380 }
4381
4382 /* If it isn't DW_AT_sibling, skip this attribute. */
4383 form = abbrev->attrs[i].form;
4384 skip_attribute:
4385 switch (form)
4386 {
4387 case DW_FORM_ref_addr:
4388 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4389 and later it is offset sized. */
4390 if (cu->header.version == 2)
4391 info_ptr += cu->header.addr_size;
4392 else
4393 info_ptr += cu->header.offset_size;
4394 break;
4395 case DW_FORM_addr:
4396 info_ptr += cu->header.addr_size;
4397 break;
4398 case DW_FORM_data1:
4399 case DW_FORM_ref1:
4400 case DW_FORM_flag:
4401 info_ptr += 1;
4402 break;
4403 case DW_FORM_flag_present:
4404 break;
4405 case DW_FORM_data2:
4406 case DW_FORM_ref2:
4407 info_ptr += 2;
4408 break;
4409 case DW_FORM_data4:
4410 case DW_FORM_ref4:
4411 info_ptr += 4;
4412 break;
4413 case DW_FORM_data8:
4414 case DW_FORM_ref8:
4415 case DW_FORM_ref_sig8:
4416 info_ptr += 8;
4417 break;
4418 case DW_FORM_string:
4419 read_direct_string (abfd, info_ptr, &bytes_read);
4420 info_ptr += bytes_read;
4421 break;
4422 case DW_FORM_sec_offset:
4423 case DW_FORM_strp:
4424 info_ptr += cu->header.offset_size;
4425 break;
4426 case DW_FORM_exprloc:
4427 case DW_FORM_block:
4428 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4429 info_ptr += bytes_read;
4430 break;
4431 case DW_FORM_block1:
4432 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4433 break;
4434 case DW_FORM_block2:
4435 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4436 break;
4437 case DW_FORM_block4:
4438 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4439 break;
4440 case DW_FORM_sdata:
4441 case DW_FORM_udata:
4442 case DW_FORM_ref_udata:
4443 info_ptr = skip_leb128 (abfd, info_ptr);
4444 break;
4445 case DW_FORM_indirect:
4446 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4447 info_ptr += bytes_read;
4448 /* We need to continue parsing from here, so just go back to
4449 the top. */
4450 goto skip_attribute;
4451
4452 default:
4453 error (_("Dwarf Error: Cannot handle %s "
4454 "in DWARF reader [in module %s]"),
4455 dwarf_form_name (form),
4456 bfd_get_filename (abfd));
4457 }
4458 }
4459
4460 if (abbrev->has_children)
4461 return skip_children (buffer, info_ptr, cu);
4462 else
4463 return info_ptr;
4464 }
4465
4466 /* Locate ORIG_PDI's sibling.
4467 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4468 in BUFFER. */
4469
4470 static gdb_byte *
4471 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4472 gdb_byte *buffer, gdb_byte *info_ptr,
4473 bfd *abfd, struct dwarf2_cu *cu)
4474 {
4475 /* Do we know the sibling already? */
4476
4477 if (orig_pdi->sibling)
4478 return orig_pdi->sibling;
4479
4480 /* Are there any children to deal with? */
4481
4482 if (!orig_pdi->has_children)
4483 return info_ptr;
4484
4485 /* Skip the children the long way. */
4486
4487 return skip_children (buffer, info_ptr, cu);
4488 }
4489
4490 /* Expand this partial symbol table into a full symbol table. */
4491
4492 static void
4493 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4494 {
4495 if (pst != NULL)
4496 {
4497 if (pst->readin)
4498 {
4499 warning (_("bug: psymtab for %s is already read in."),
4500 pst->filename);
4501 }
4502 else
4503 {
4504 if (info_verbose)
4505 {
4506 printf_filtered (_("Reading in symbols for %s..."),
4507 pst->filename);
4508 gdb_flush (gdb_stdout);
4509 }
4510
4511 /* Restore our global data. */
4512 dwarf2_per_objfile = objfile_data (pst->objfile,
4513 dwarf2_objfile_data_key);
4514
4515 /* If this psymtab is constructed from a debug-only objfile, the
4516 has_section_at_zero flag will not necessarily be correct. We
4517 can get the correct value for this flag by looking at the data
4518 associated with the (presumably stripped) associated objfile. */
4519 if (pst->objfile->separate_debug_objfile_backlink)
4520 {
4521 struct dwarf2_per_objfile *dpo_backlink
4522 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4523 dwarf2_objfile_data_key);
4524
4525 dwarf2_per_objfile->has_section_at_zero
4526 = dpo_backlink->has_section_at_zero;
4527 }
4528
4529 dwarf2_per_objfile->reading_partial_symbols = 0;
4530
4531 psymtab_to_symtab_1 (pst);
4532
4533 /* Finish up the debug error message. */
4534 if (info_verbose)
4535 printf_filtered (_("done.\n"));
4536 }
4537 }
4538 }
4539 \f
4540 /* Reading in full CUs. */
4541
4542 /* Add PER_CU to the queue. */
4543
4544 static void
4545 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4546 {
4547 struct dwarf2_queue_item *item;
4548
4549 per_cu->queued = 1;
4550 item = xmalloc (sizeof (*item));
4551 item->per_cu = per_cu;
4552 item->next = NULL;
4553
4554 if (dwarf2_queue == NULL)
4555 dwarf2_queue = item;
4556 else
4557 dwarf2_queue_tail->next = item;
4558
4559 dwarf2_queue_tail = item;
4560 }
4561
4562 /* Process the queue. */
4563
4564 static void
4565 process_queue (void)
4566 {
4567 struct dwarf2_queue_item *item, *next_item;
4568
4569 /* The queue starts out with one item, but following a DIE reference
4570 may load a new CU, adding it to the end of the queue. */
4571 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4572 {
4573 if (dwarf2_per_objfile->using_index
4574 ? !item->per_cu->v.quick->symtab
4575 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4576 process_full_comp_unit (item->per_cu);
4577
4578 item->per_cu->queued = 0;
4579 next_item = item->next;
4580 xfree (item);
4581 }
4582
4583 dwarf2_queue_tail = NULL;
4584 }
4585
4586 /* Free all allocated queue entries. This function only releases anything if
4587 an error was thrown; if the queue was processed then it would have been
4588 freed as we went along. */
4589
4590 static void
4591 dwarf2_release_queue (void *dummy)
4592 {
4593 struct dwarf2_queue_item *item, *last;
4594
4595 item = dwarf2_queue;
4596 while (item)
4597 {
4598 /* Anything still marked queued is likely to be in an
4599 inconsistent state, so discard it. */
4600 if (item->per_cu->queued)
4601 {
4602 if (item->per_cu->cu != NULL)
4603 free_one_cached_comp_unit (item->per_cu->cu);
4604 item->per_cu->queued = 0;
4605 }
4606
4607 last = item;
4608 item = item->next;
4609 xfree (last);
4610 }
4611
4612 dwarf2_queue = dwarf2_queue_tail = NULL;
4613 }
4614
4615 /* Read in full symbols for PST, and anything it depends on. */
4616
4617 static void
4618 psymtab_to_symtab_1 (struct partial_symtab *pst)
4619 {
4620 struct dwarf2_per_cu_data *per_cu;
4621 struct cleanup *back_to;
4622 int i;
4623
4624 for (i = 0; i < pst->number_of_dependencies; i++)
4625 if (!pst->dependencies[i]->readin)
4626 {
4627 /* Inform about additional files that need to be read in. */
4628 if (info_verbose)
4629 {
4630 /* FIXME: i18n: Need to make this a single string. */
4631 fputs_filtered (" ", gdb_stdout);
4632 wrap_here ("");
4633 fputs_filtered ("and ", gdb_stdout);
4634 wrap_here ("");
4635 printf_filtered ("%s...", pst->dependencies[i]->filename);
4636 wrap_here (""); /* Flush output. */
4637 gdb_flush (gdb_stdout);
4638 }
4639 psymtab_to_symtab_1 (pst->dependencies[i]);
4640 }
4641
4642 per_cu = pst->read_symtab_private;
4643
4644 if (per_cu == NULL)
4645 {
4646 /* It's an include file, no symbols to read for it.
4647 Everything is in the parent symtab. */
4648 pst->readin = 1;
4649 return;
4650 }
4651
4652 dw2_do_instantiate_symtab (per_cu);
4653 }
4654
4655 /* Load the DIEs associated with PER_CU into memory. */
4656
4657 static void
4658 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4659 {
4660 struct objfile *objfile = per_cu->objfile;
4661 bfd *abfd = objfile->obfd;
4662 struct dwarf2_cu *cu;
4663 unsigned int offset;
4664 gdb_byte *info_ptr, *beg_of_comp_unit;
4665 struct cleanup *free_cu_cleanup = NULL;
4666 struct attribute *attr;
4667 int read_cu = 0;
4668
4669 gdb_assert (! per_cu->debug_types_section);
4670
4671 /* Set local variables from the partial symbol table info. */
4672 offset = per_cu->offset;
4673
4674 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4675 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4676 beg_of_comp_unit = info_ptr;
4677
4678 if (per_cu->cu == NULL)
4679 {
4680 cu = xmalloc (sizeof (*cu));
4681 init_one_comp_unit (cu, per_cu);
4682
4683 read_cu = 1;
4684
4685 /* If an error occurs while loading, release our storage. */
4686 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4687
4688 /* Read in the comp_unit header. */
4689 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4690
4691 /* Skip dummy compilation units. */
4692 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4693 + dwarf2_per_objfile->info.size)
4694 || peek_abbrev_code (abfd, info_ptr) == 0)
4695 {
4696 do_cleanups (free_cu_cleanup);
4697 return;
4698 }
4699
4700 /* Complete the cu_header. */
4701 cu->header.offset = offset;
4702 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4703 }
4704 else
4705 {
4706 cu = per_cu->cu;
4707 info_ptr += cu->header.first_die_offset;
4708 }
4709
4710 cu->dies = read_comp_unit (info_ptr, cu);
4711
4712 /* We try not to read any attributes in this function, because not
4713 all CUs needed for references have been loaded yet, and symbol
4714 table processing isn't initialized. But we have to set the CU language,
4715 or we won't be able to build types correctly. */
4716 prepare_one_comp_unit (cu, cu->dies);
4717
4718 /* Similarly, if we do not read the producer, we can not apply
4719 producer-specific interpretation. */
4720 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4721 if (attr)
4722 cu->producer = DW_STRING (attr);
4723
4724 if (read_cu)
4725 {
4726 /* We've successfully allocated this compilation unit. Let our
4727 caller clean it up when finished with it. */
4728 discard_cleanups (free_cu_cleanup);
4729
4730 /* Link this CU into read_in_chain. */
4731 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4732 dwarf2_per_objfile->read_in_chain = per_cu;
4733 }
4734 }
4735
4736 /* Add a DIE to the delayed physname list. */
4737
4738 static void
4739 add_to_method_list (struct type *type, int fnfield_index, int index,
4740 const char *name, struct die_info *die,
4741 struct dwarf2_cu *cu)
4742 {
4743 struct delayed_method_info mi;
4744 mi.type = type;
4745 mi.fnfield_index = fnfield_index;
4746 mi.index = index;
4747 mi.name = name;
4748 mi.die = die;
4749 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4750 }
4751
4752 /* A cleanup for freeing the delayed method list. */
4753
4754 static void
4755 free_delayed_list (void *ptr)
4756 {
4757 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4758 if (cu->method_list != NULL)
4759 {
4760 VEC_free (delayed_method_info, cu->method_list);
4761 cu->method_list = NULL;
4762 }
4763 }
4764
4765 /* Compute the physnames of any methods on the CU's method list.
4766
4767 The computation of method physnames is delayed in order to avoid the
4768 (bad) condition that one of the method's formal parameters is of an as yet
4769 incomplete type. */
4770
4771 static void
4772 compute_delayed_physnames (struct dwarf2_cu *cu)
4773 {
4774 int i;
4775 struct delayed_method_info *mi;
4776 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4777 {
4778 const char *physname;
4779 struct fn_fieldlist *fn_flp
4780 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4781 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4782 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4783 }
4784 }
4785
4786 /* Generate full symbol information for PER_CU, whose DIEs have
4787 already been loaded into memory. */
4788
4789 static void
4790 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4791 {
4792 struct dwarf2_cu *cu = per_cu->cu;
4793 struct objfile *objfile = per_cu->objfile;
4794 CORE_ADDR lowpc, highpc;
4795 struct symtab *symtab;
4796 struct cleanup *back_to, *delayed_list_cleanup;
4797 CORE_ADDR baseaddr;
4798
4799 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4800
4801 buildsym_init ();
4802 back_to = make_cleanup (really_free_pendings, NULL);
4803 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4804
4805 cu->list_in_scope = &file_symbols;
4806
4807 /* Do line number decoding in read_file_scope () */
4808 process_die (cu->dies, cu);
4809
4810 /* Now that we have processed all the DIEs in the CU, all the types
4811 should be complete, and it should now be safe to compute all of the
4812 physnames. */
4813 compute_delayed_physnames (cu);
4814 do_cleanups (delayed_list_cleanup);
4815
4816 /* Some compilers don't define a DW_AT_high_pc attribute for the
4817 compilation unit. If the DW_AT_high_pc is missing, synthesize
4818 it, by scanning the DIE's below the compilation unit. */
4819 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4820
4821 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4822
4823 if (symtab != NULL)
4824 {
4825 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4826
4827 /* Set symtab language to language from DW_AT_language. If the
4828 compilation is from a C file generated by language preprocessors, do
4829 not set the language if it was already deduced by start_subfile. */
4830 if (!(cu->language == language_c && symtab->language != language_c))
4831 symtab->language = cu->language;
4832
4833 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4834 produce DW_AT_location with location lists but it can be possibly
4835 invalid without -fvar-tracking.
4836
4837 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4838 needed, it would be wrong due to missing DW_AT_producer there.
4839
4840 Still one can confuse GDB by using non-standard GCC compilation
4841 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4842 */
4843 if (cu->has_loclist && gcc_4_minor >= 0)
4844 symtab->locations_valid = 1;
4845
4846 if (gcc_4_minor >= 5)
4847 symtab->epilogue_unwind_valid = 1;
4848
4849 symtab->call_site_htab = cu->call_site_htab;
4850 }
4851
4852 if (dwarf2_per_objfile->using_index)
4853 per_cu->v.quick->symtab = symtab;
4854 else
4855 {
4856 struct partial_symtab *pst = per_cu->v.psymtab;
4857 pst->symtab = symtab;
4858 pst->readin = 1;
4859 }
4860
4861 do_cleanups (back_to);
4862 }
4863
4864 /* Process a die and its children. */
4865
4866 static void
4867 process_die (struct die_info *die, struct dwarf2_cu *cu)
4868 {
4869 switch (die->tag)
4870 {
4871 case DW_TAG_padding:
4872 break;
4873 case DW_TAG_compile_unit:
4874 read_file_scope (die, cu);
4875 break;
4876 case DW_TAG_type_unit:
4877 read_type_unit_scope (die, cu);
4878 break;
4879 case DW_TAG_subprogram:
4880 case DW_TAG_inlined_subroutine:
4881 read_func_scope (die, cu);
4882 break;
4883 case DW_TAG_lexical_block:
4884 case DW_TAG_try_block:
4885 case DW_TAG_catch_block:
4886 read_lexical_block_scope (die, cu);
4887 break;
4888 case DW_TAG_GNU_call_site:
4889 read_call_site_scope (die, cu);
4890 break;
4891 case DW_TAG_class_type:
4892 case DW_TAG_interface_type:
4893 case DW_TAG_structure_type:
4894 case DW_TAG_union_type:
4895 process_structure_scope (die, cu);
4896 break;
4897 case DW_TAG_enumeration_type:
4898 process_enumeration_scope (die, cu);
4899 break;
4900
4901 /* These dies have a type, but processing them does not create
4902 a symbol or recurse to process the children. Therefore we can
4903 read them on-demand through read_type_die. */
4904 case DW_TAG_subroutine_type:
4905 case DW_TAG_set_type:
4906 case DW_TAG_array_type:
4907 case DW_TAG_pointer_type:
4908 case DW_TAG_ptr_to_member_type:
4909 case DW_TAG_reference_type:
4910 case DW_TAG_string_type:
4911 break;
4912
4913 case DW_TAG_base_type:
4914 case DW_TAG_subrange_type:
4915 case DW_TAG_typedef:
4916 /* Add a typedef symbol for the type definition, if it has a
4917 DW_AT_name. */
4918 new_symbol (die, read_type_die (die, cu), cu);
4919 break;
4920 case DW_TAG_common_block:
4921 read_common_block (die, cu);
4922 break;
4923 case DW_TAG_common_inclusion:
4924 break;
4925 case DW_TAG_namespace:
4926 processing_has_namespace_info = 1;
4927 read_namespace (die, cu);
4928 break;
4929 case DW_TAG_module:
4930 processing_has_namespace_info = 1;
4931 read_module (die, cu);
4932 break;
4933 case DW_TAG_imported_declaration:
4934 case DW_TAG_imported_module:
4935 processing_has_namespace_info = 1;
4936 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4937 || cu->language != language_fortran))
4938 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4939 dwarf_tag_name (die->tag));
4940 read_import_statement (die, cu);
4941 break;
4942 default:
4943 new_symbol (die, NULL, cu);
4944 break;
4945 }
4946 }
4947
4948 /* A helper function for dwarf2_compute_name which determines whether DIE
4949 needs to have the name of the scope prepended to the name listed in the
4950 die. */
4951
4952 static int
4953 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4954 {
4955 struct attribute *attr;
4956
4957 switch (die->tag)
4958 {
4959 case DW_TAG_namespace:
4960 case DW_TAG_typedef:
4961 case DW_TAG_class_type:
4962 case DW_TAG_interface_type:
4963 case DW_TAG_structure_type:
4964 case DW_TAG_union_type:
4965 case DW_TAG_enumeration_type:
4966 case DW_TAG_enumerator:
4967 case DW_TAG_subprogram:
4968 case DW_TAG_member:
4969 return 1;
4970
4971 case DW_TAG_variable:
4972 case DW_TAG_constant:
4973 /* We only need to prefix "globally" visible variables. These include
4974 any variable marked with DW_AT_external or any variable that
4975 lives in a namespace. [Variables in anonymous namespaces
4976 require prefixing, but they are not DW_AT_external.] */
4977
4978 if (dwarf2_attr (die, DW_AT_specification, cu))
4979 {
4980 struct dwarf2_cu *spec_cu = cu;
4981
4982 return die_needs_namespace (die_specification (die, &spec_cu),
4983 spec_cu);
4984 }
4985
4986 attr = dwarf2_attr (die, DW_AT_external, cu);
4987 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4988 && die->parent->tag != DW_TAG_module)
4989 return 0;
4990 /* A variable in a lexical block of some kind does not need a
4991 namespace, even though in C++ such variables may be external
4992 and have a mangled name. */
4993 if (die->parent->tag == DW_TAG_lexical_block
4994 || die->parent->tag == DW_TAG_try_block
4995 || die->parent->tag == DW_TAG_catch_block
4996 || die->parent->tag == DW_TAG_subprogram)
4997 return 0;
4998 return 1;
4999
5000 default:
5001 return 0;
5002 }
5003 }
5004
5005 /* Retrieve the last character from a mem_file. */
5006
5007 static void
5008 do_ui_file_peek_last (void *object, const char *buffer, long length)
5009 {
5010 char *last_char_p = (char *) object;
5011
5012 if (length > 0)
5013 *last_char_p = buffer[length - 1];
5014 }
5015
5016 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5017 compute the physname for the object, which include a method's
5018 formal parameters (C++/Java) and return type (Java).
5019
5020 For Ada, return the DIE's linkage name rather than the fully qualified
5021 name. PHYSNAME is ignored..
5022
5023 The result is allocated on the objfile_obstack and canonicalized. */
5024
5025 static const char *
5026 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5027 int physname)
5028 {
5029 struct objfile *objfile = cu->objfile;
5030
5031 if (name == NULL)
5032 name = dwarf2_name (die, cu);
5033
5034 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5035 compute it by typename_concat inside GDB. */
5036 if (cu->language == language_ada
5037 || (cu->language == language_fortran && physname))
5038 {
5039 /* For Ada unit, we prefer the linkage name over the name, as
5040 the former contains the exported name, which the user expects
5041 to be able to reference. Ideally, we want the user to be able
5042 to reference this entity using either natural or linkage name,
5043 but we haven't started looking at this enhancement yet. */
5044 struct attribute *attr;
5045
5046 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5047 if (attr == NULL)
5048 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5049 if (attr && DW_STRING (attr))
5050 return DW_STRING (attr);
5051 }
5052
5053 /* These are the only languages we know how to qualify names in. */
5054 if (name != NULL
5055 && (cu->language == language_cplus || cu->language == language_java
5056 || cu->language == language_fortran))
5057 {
5058 if (die_needs_namespace (die, cu))
5059 {
5060 long length;
5061 const char *prefix;
5062 struct ui_file *buf;
5063
5064 prefix = determine_prefix (die, cu);
5065 buf = mem_fileopen ();
5066 if (*prefix != '\0')
5067 {
5068 char *prefixed_name = typename_concat (NULL, prefix, name,
5069 physname, cu);
5070
5071 fputs_unfiltered (prefixed_name, buf);
5072 xfree (prefixed_name);
5073 }
5074 else
5075 fputs_unfiltered (name, buf);
5076
5077 /* Template parameters may be specified in the DIE's DW_AT_name, or
5078 as children with DW_TAG_template_type_param or
5079 DW_TAG_value_type_param. If the latter, add them to the name
5080 here. If the name already has template parameters, then
5081 skip this step; some versions of GCC emit both, and
5082 it is more efficient to use the pre-computed name.
5083
5084 Something to keep in mind about this process: it is very
5085 unlikely, or in some cases downright impossible, to produce
5086 something that will match the mangled name of a function.
5087 If the definition of the function has the same debug info,
5088 we should be able to match up with it anyway. But fallbacks
5089 using the minimal symbol, for instance to find a method
5090 implemented in a stripped copy of libstdc++, will not work.
5091 If we do not have debug info for the definition, we will have to
5092 match them up some other way.
5093
5094 When we do name matching there is a related problem with function
5095 templates; two instantiated function templates are allowed to
5096 differ only by their return types, which we do not add here. */
5097
5098 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5099 {
5100 struct attribute *attr;
5101 struct die_info *child;
5102 int first = 1;
5103
5104 die->building_fullname = 1;
5105
5106 for (child = die->child; child != NULL; child = child->sibling)
5107 {
5108 struct type *type;
5109 long value;
5110 gdb_byte *bytes;
5111 struct dwarf2_locexpr_baton *baton;
5112 struct value *v;
5113
5114 if (child->tag != DW_TAG_template_type_param
5115 && child->tag != DW_TAG_template_value_param)
5116 continue;
5117
5118 if (first)
5119 {
5120 fputs_unfiltered ("<", buf);
5121 first = 0;
5122 }
5123 else
5124 fputs_unfiltered (", ", buf);
5125
5126 attr = dwarf2_attr (child, DW_AT_type, cu);
5127 if (attr == NULL)
5128 {
5129 complaint (&symfile_complaints,
5130 _("template parameter missing DW_AT_type"));
5131 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5132 continue;
5133 }
5134 type = die_type (child, cu);
5135
5136 if (child->tag == DW_TAG_template_type_param)
5137 {
5138 c_print_type (type, "", buf, -1, 0);
5139 continue;
5140 }
5141
5142 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5143 if (attr == NULL)
5144 {
5145 complaint (&symfile_complaints,
5146 _("template parameter missing "
5147 "DW_AT_const_value"));
5148 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5149 continue;
5150 }
5151
5152 dwarf2_const_value_attr (attr, type, name,
5153 &cu->comp_unit_obstack, cu,
5154 &value, &bytes, &baton);
5155
5156 if (TYPE_NOSIGN (type))
5157 /* GDB prints characters as NUMBER 'CHAR'. If that's
5158 changed, this can use value_print instead. */
5159 c_printchar (value, type, buf);
5160 else
5161 {
5162 struct value_print_options opts;
5163
5164 if (baton != NULL)
5165 v = dwarf2_evaluate_loc_desc (type, NULL,
5166 baton->data,
5167 baton->size,
5168 baton->per_cu);
5169 else if (bytes != NULL)
5170 {
5171 v = allocate_value (type);
5172 memcpy (value_contents_writeable (v), bytes,
5173 TYPE_LENGTH (type));
5174 }
5175 else
5176 v = value_from_longest (type, value);
5177
5178 /* Specify decimal so that we do not depend on
5179 the radix. */
5180 get_formatted_print_options (&opts, 'd');
5181 opts.raw = 1;
5182 value_print (v, buf, &opts);
5183 release_value (v);
5184 value_free (v);
5185 }
5186 }
5187
5188 die->building_fullname = 0;
5189
5190 if (!first)
5191 {
5192 /* Close the argument list, with a space if necessary
5193 (nested templates). */
5194 char last_char = '\0';
5195 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5196 if (last_char == '>')
5197 fputs_unfiltered (" >", buf);
5198 else
5199 fputs_unfiltered (">", buf);
5200 }
5201 }
5202
5203 /* For Java and C++ methods, append formal parameter type
5204 information, if PHYSNAME. */
5205
5206 if (physname && die->tag == DW_TAG_subprogram
5207 && (cu->language == language_cplus
5208 || cu->language == language_java))
5209 {
5210 struct type *type = read_type_die (die, cu);
5211
5212 c_type_print_args (type, buf, 1, cu->language);
5213
5214 if (cu->language == language_java)
5215 {
5216 /* For java, we must append the return type to method
5217 names. */
5218 if (die->tag == DW_TAG_subprogram)
5219 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5220 0, 0);
5221 }
5222 else if (cu->language == language_cplus)
5223 {
5224 /* Assume that an artificial first parameter is
5225 "this", but do not crash if it is not. RealView
5226 marks unnamed (and thus unused) parameters as
5227 artificial; there is no way to differentiate
5228 the two cases. */
5229 if (TYPE_NFIELDS (type) > 0
5230 && TYPE_FIELD_ARTIFICIAL (type, 0)
5231 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5232 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5233 0))))
5234 fputs_unfiltered (" const", buf);
5235 }
5236 }
5237
5238 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5239 &length);
5240 ui_file_delete (buf);
5241
5242 if (cu->language == language_cplus)
5243 {
5244 char *cname
5245 = dwarf2_canonicalize_name (name, cu,
5246 &objfile->objfile_obstack);
5247
5248 if (cname != NULL)
5249 name = cname;
5250 }
5251 }
5252 }
5253
5254 return name;
5255 }
5256
5257 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5258 If scope qualifiers are appropriate they will be added. The result
5259 will be allocated on the objfile_obstack, or NULL if the DIE does
5260 not have a name. NAME may either be from a previous call to
5261 dwarf2_name or NULL.
5262
5263 The output string will be canonicalized (if C++/Java). */
5264
5265 static const char *
5266 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5267 {
5268 return dwarf2_compute_name (name, die, cu, 0);
5269 }
5270
5271 /* Construct a physname for the given DIE in CU. NAME may either be
5272 from a previous call to dwarf2_name or NULL. The result will be
5273 allocated on the objfile_objstack or NULL if the DIE does not have a
5274 name.
5275
5276 The output string will be canonicalized (if C++/Java). */
5277
5278 static const char *
5279 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5280 {
5281 struct objfile *objfile = cu->objfile;
5282 struct attribute *attr;
5283 const char *retval, *mangled = NULL, *canon = NULL;
5284 struct cleanup *back_to;
5285 int need_copy = 1;
5286
5287 /* In this case dwarf2_compute_name is just a shortcut not building anything
5288 on its own. */
5289 if (!die_needs_namespace (die, cu))
5290 return dwarf2_compute_name (name, die, cu, 1);
5291
5292 back_to = make_cleanup (null_cleanup, NULL);
5293
5294 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5295 if (!attr)
5296 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5297
5298 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5299 has computed. */
5300 if (attr && DW_STRING (attr))
5301 {
5302 char *demangled;
5303
5304 mangled = DW_STRING (attr);
5305
5306 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5307 type. It is easier for GDB users to search for such functions as
5308 `name(params)' than `long name(params)'. In such case the minimal
5309 symbol names do not match the full symbol names but for template
5310 functions there is never a need to look up their definition from their
5311 declaration so the only disadvantage remains the minimal symbol
5312 variant `long name(params)' does not have the proper inferior type.
5313 */
5314
5315 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5316 | (cu->language == language_java
5317 ? DMGL_JAVA | DMGL_RET_POSTFIX
5318 : DMGL_RET_DROP)));
5319 if (demangled)
5320 {
5321 make_cleanup (xfree, demangled);
5322 canon = demangled;
5323 }
5324 else
5325 {
5326 canon = mangled;
5327 need_copy = 0;
5328 }
5329 }
5330
5331 if (canon == NULL || check_physname)
5332 {
5333 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5334
5335 if (canon != NULL && strcmp (physname, canon) != 0)
5336 {
5337 /* It may not mean a bug in GDB. The compiler could also
5338 compute DW_AT_linkage_name incorrectly. But in such case
5339 GDB would need to be bug-to-bug compatible. */
5340
5341 complaint (&symfile_complaints,
5342 _("Computed physname <%s> does not match demangled <%s> "
5343 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5344 physname, canon, mangled, die->offset, objfile->name);
5345
5346 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5347 is available here - over computed PHYSNAME. It is safer
5348 against both buggy GDB and buggy compilers. */
5349
5350 retval = canon;
5351 }
5352 else
5353 {
5354 retval = physname;
5355 need_copy = 0;
5356 }
5357 }
5358 else
5359 retval = canon;
5360
5361 if (need_copy)
5362 retval = obsavestring (retval, strlen (retval),
5363 &objfile->objfile_obstack);
5364
5365 do_cleanups (back_to);
5366 return retval;
5367 }
5368
5369 /* Read the import statement specified by the given die and record it. */
5370
5371 static void
5372 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5373 {
5374 struct objfile *objfile = cu->objfile;
5375 struct attribute *import_attr;
5376 struct die_info *imported_die, *child_die;
5377 struct dwarf2_cu *imported_cu;
5378 const char *imported_name;
5379 const char *imported_name_prefix;
5380 const char *canonical_name;
5381 const char *import_alias;
5382 const char *imported_declaration = NULL;
5383 const char *import_prefix;
5384 VEC (const_char_ptr) *excludes = NULL;
5385 struct cleanup *cleanups;
5386
5387 char *temp;
5388
5389 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5390 if (import_attr == NULL)
5391 {
5392 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5393 dwarf_tag_name (die->tag));
5394 return;
5395 }
5396
5397 imported_cu = cu;
5398 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5399 imported_name = dwarf2_name (imported_die, imported_cu);
5400 if (imported_name == NULL)
5401 {
5402 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5403
5404 The import in the following code:
5405 namespace A
5406 {
5407 typedef int B;
5408 }
5409
5410 int main ()
5411 {
5412 using A::B;
5413 B b;
5414 return b;
5415 }
5416
5417 ...
5418 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5419 <52> DW_AT_decl_file : 1
5420 <53> DW_AT_decl_line : 6
5421 <54> DW_AT_import : <0x75>
5422 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5423 <59> DW_AT_name : B
5424 <5b> DW_AT_decl_file : 1
5425 <5c> DW_AT_decl_line : 2
5426 <5d> DW_AT_type : <0x6e>
5427 ...
5428 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5429 <76> DW_AT_byte_size : 4
5430 <77> DW_AT_encoding : 5 (signed)
5431
5432 imports the wrong die ( 0x75 instead of 0x58 ).
5433 This case will be ignored until the gcc bug is fixed. */
5434 return;
5435 }
5436
5437 /* Figure out the local name after import. */
5438 import_alias = dwarf2_name (die, cu);
5439
5440 /* Figure out where the statement is being imported to. */
5441 import_prefix = determine_prefix (die, cu);
5442
5443 /* Figure out what the scope of the imported die is and prepend it
5444 to the name of the imported die. */
5445 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5446
5447 if (imported_die->tag != DW_TAG_namespace
5448 && imported_die->tag != DW_TAG_module)
5449 {
5450 imported_declaration = imported_name;
5451 canonical_name = imported_name_prefix;
5452 }
5453 else if (strlen (imported_name_prefix) > 0)
5454 {
5455 temp = alloca (strlen (imported_name_prefix)
5456 + 2 + strlen (imported_name) + 1);
5457 strcpy (temp, imported_name_prefix);
5458 strcat (temp, "::");
5459 strcat (temp, imported_name);
5460 canonical_name = temp;
5461 }
5462 else
5463 canonical_name = imported_name;
5464
5465 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5466
5467 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5468 for (child_die = die->child; child_die && child_die->tag;
5469 child_die = sibling_die (child_die))
5470 {
5471 /* DWARF-4: A Fortran use statement with a “rename list” may be
5472 represented by an imported module entry with an import attribute
5473 referring to the module and owned entries corresponding to those
5474 entities that are renamed as part of being imported. */
5475
5476 if (child_die->tag != DW_TAG_imported_declaration)
5477 {
5478 complaint (&symfile_complaints,
5479 _("child DW_TAG_imported_declaration expected "
5480 "- DIE at 0x%x [in module %s]"),
5481 child_die->offset, objfile->name);
5482 continue;
5483 }
5484
5485 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5486 if (import_attr == NULL)
5487 {
5488 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5489 dwarf_tag_name (child_die->tag));
5490 continue;
5491 }
5492
5493 imported_cu = cu;
5494 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5495 &imported_cu);
5496 imported_name = dwarf2_name (imported_die, imported_cu);
5497 if (imported_name == NULL)
5498 {
5499 complaint (&symfile_complaints,
5500 _("child DW_TAG_imported_declaration has unknown "
5501 "imported name - DIE at 0x%x [in module %s]"),
5502 child_die->offset, objfile->name);
5503 continue;
5504 }
5505
5506 VEC_safe_push (const_char_ptr, excludes, imported_name);
5507
5508 process_die (child_die, cu);
5509 }
5510
5511 cp_add_using_directive (import_prefix,
5512 canonical_name,
5513 import_alias,
5514 imported_declaration,
5515 excludes,
5516 &objfile->objfile_obstack);
5517
5518 do_cleanups (cleanups);
5519 }
5520
5521 /* Cleanup function for read_file_scope. */
5522
5523 static void
5524 free_cu_line_header (void *arg)
5525 {
5526 struct dwarf2_cu *cu = arg;
5527
5528 free_line_header (cu->line_header);
5529 cu->line_header = NULL;
5530 }
5531
5532 static void
5533 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5534 char **name, char **comp_dir)
5535 {
5536 struct attribute *attr;
5537
5538 *name = NULL;
5539 *comp_dir = NULL;
5540
5541 /* Find the filename. Do not use dwarf2_name here, since the filename
5542 is not a source language identifier. */
5543 attr = dwarf2_attr (die, DW_AT_name, cu);
5544 if (attr)
5545 {
5546 *name = DW_STRING (attr);
5547 }
5548
5549 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5550 if (attr)
5551 *comp_dir = DW_STRING (attr);
5552 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5553 {
5554 *comp_dir = ldirname (*name);
5555 if (*comp_dir != NULL)
5556 make_cleanup (xfree, *comp_dir);
5557 }
5558 if (*comp_dir != NULL)
5559 {
5560 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5561 directory, get rid of it. */
5562 char *cp = strchr (*comp_dir, ':');
5563
5564 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5565 *comp_dir = cp + 1;
5566 }
5567
5568 if (*name == NULL)
5569 *name = "<unknown>";
5570 }
5571
5572 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
5573 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5574 COMP_DIR is the compilation directory.
5575 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
5576
5577 static void
5578 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5579 const char *comp_dir, int want_line_info)
5580 {
5581 struct attribute *attr;
5582 struct objfile *objfile = cu->objfile;
5583 bfd *abfd = objfile->obfd;
5584
5585 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5586 if (attr)
5587 {
5588 unsigned int line_offset = DW_UNSND (attr);
5589 struct line_header *line_header
5590 = dwarf_decode_line_header (line_offset, abfd, cu);
5591
5592 if (line_header)
5593 {
5594 cu->line_header = line_header;
5595 make_cleanup (free_cu_line_header, cu);
5596 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
5597 }
5598 }
5599 }
5600
5601 /* Process DW_TAG_compile_unit. */
5602
5603 static void
5604 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5605 {
5606 struct objfile *objfile = cu->objfile;
5607 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5608 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5609 CORE_ADDR highpc = ((CORE_ADDR) 0);
5610 struct attribute *attr;
5611 char *name = NULL;
5612 char *comp_dir = NULL;
5613 struct die_info *child_die;
5614 bfd *abfd = objfile->obfd;
5615 CORE_ADDR baseaddr;
5616
5617 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5618
5619 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5620
5621 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5622 from finish_block. */
5623 if (lowpc == ((CORE_ADDR) -1))
5624 lowpc = highpc;
5625 lowpc += baseaddr;
5626 highpc += baseaddr;
5627
5628 find_file_and_directory (die, cu, &name, &comp_dir);
5629
5630 attr = dwarf2_attr (die, DW_AT_language, cu);
5631 if (attr)
5632 {
5633 set_cu_language (DW_UNSND (attr), cu);
5634 }
5635
5636 attr = dwarf2_attr (die, DW_AT_producer, cu);
5637 if (attr)
5638 cu->producer = DW_STRING (attr);
5639
5640 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5641 standardised yet. As a workaround for the language detection we fall
5642 back to the DW_AT_producer string. */
5643 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5644 cu->language = language_opencl;
5645
5646 /* We assume that we're processing GCC output. */
5647 processing_gcc_compilation = 2;
5648
5649 processing_has_namespace_info = 0;
5650
5651 start_symtab (name, comp_dir, lowpc);
5652 record_debugformat ("DWARF 2");
5653 record_producer (cu->producer);
5654
5655 /* Decode line number information if present. We do this before
5656 processing child DIEs, so that the line header table is available
5657 for DW_AT_decl_file. */
5658 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
5659
5660 /* Process all dies in compilation unit. */
5661 if (die->child != NULL)
5662 {
5663 child_die = die->child;
5664 while (child_die && child_die->tag)
5665 {
5666 process_die (child_die, cu);
5667 child_die = sibling_die (child_die);
5668 }
5669 }
5670
5671 /* Decode macro information, if present. Dwarf 2 macro information
5672 refers to information in the line number info statement program
5673 header, so we can only read it if we've read the header
5674 successfully. */
5675 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5676 if (attr && cu->line_header)
5677 {
5678 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5679 complaint (&symfile_complaints,
5680 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5681
5682 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5683 comp_dir, abfd, cu,
5684 &dwarf2_per_objfile->macro, 1);
5685 }
5686 else
5687 {
5688 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5689 if (attr && cu->line_header)
5690 {
5691 unsigned int macro_offset = DW_UNSND (attr);
5692
5693 dwarf_decode_macros (cu->line_header, macro_offset,
5694 comp_dir, abfd, cu,
5695 &dwarf2_per_objfile->macinfo, 0);
5696 }
5697 }
5698
5699 do_cleanups (back_to);
5700 }
5701
5702 /* Process DW_TAG_type_unit.
5703 For TUs we want to skip the first top level sibling if it's not the
5704 actual type being defined by this TU. In this case the first top
5705 level sibling is there to provide context only. */
5706
5707 static void
5708 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5709 {
5710 struct objfile *objfile = cu->objfile;
5711 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5712 CORE_ADDR lowpc;
5713 struct attribute *attr;
5714 char *name = NULL;
5715 char *comp_dir = NULL;
5716 struct die_info *child_die;
5717 bfd *abfd = objfile->obfd;
5718
5719 /* start_symtab needs a low pc, but we don't really have one.
5720 Do what read_file_scope would do in the absence of such info. */
5721 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5722
5723 /* Find the filename. Do not use dwarf2_name here, since the filename
5724 is not a source language identifier. */
5725 attr = dwarf2_attr (die, DW_AT_name, cu);
5726 if (attr)
5727 name = DW_STRING (attr);
5728
5729 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5730 if (attr)
5731 comp_dir = DW_STRING (attr);
5732 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5733 {
5734 comp_dir = ldirname (name);
5735 if (comp_dir != NULL)
5736 make_cleanup (xfree, comp_dir);
5737 }
5738
5739 if (name == NULL)
5740 name = "<unknown>";
5741
5742 attr = dwarf2_attr (die, DW_AT_language, cu);
5743 if (attr)
5744 set_cu_language (DW_UNSND (attr), cu);
5745
5746 /* This isn't technically needed today. It is done for symmetry
5747 with read_file_scope. */
5748 attr = dwarf2_attr (die, DW_AT_producer, cu);
5749 if (attr)
5750 cu->producer = DW_STRING (attr);
5751
5752 /* We assume that we're processing GCC output. */
5753 processing_gcc_compilation = 2;
5754
5755 processing_has_namespace_info = 0;
5756
5757 start_symtab (name, comp_dir, lowpc);
5758 record_debugformat ("DWARF 2");
5759 record_producer (cu->producer);
5760
5761 /* Decode line number information if present. We do this before
5762 processing child DIEs, so that the line header table is available
5763 for DW_AT_decl_file.
5764 We don't need the pc/line-number mapping for type units. */
5765 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
5766
5767 /* Process the dies in the type unit. */
5768 if (die->child == NULL)
5769 {
5770 dump_die_for_error (die);
5771 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5772 bfd_get_filename (abfd));
5773 }
5774
5775 child_die = die->child;
5776
5777 while (child_die && child_die->tag)
5778 {
5779 process_die (child_die, cu);
5780
5781 child_die = sibling_die (child_die);
5782 }
5783
5784 do_cleanups (back_to);
5785 }
5786
5787 /* qsort helper for inherit_abstract_dies. */
5788
5789 static int
5790 unsigned_int_compar (const void *ap, const void *bp)
5791 {
5792 unsigned int a = *(unsigned int *) ap;
5793 unsigned int b = *(unsigned int *) bp;
5794
5795 return (a > b) - (b > a);
5796 }
5797
5798 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5799 Inherit only the children of the DW_AT_abstract_origin DIE not being
5800 already referenced by DW_AT_abstract_origin from the children of the
5801 current DIE. */
5802
5803 static void
5804 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5805 {
5806 struct die_info *child_die;
5807 unsigned die_children_count;
5808 /* CU offsets which were referenced by children of the current DIE. */
5809 unsigned *offsets;
5810 unsigned *offsets_end, *offsetp;
5811 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5812 struct die_info *origin_die;
5813 /* Iterator of the ORIGIN_DIE children. */
5814 struct die_info *origin_child_die;
5815 struct cleanup *cleanups;
5816 struct attribute *attr;
5817 struct dwarf2_cu *origin_cu;
5818 struct pending **origin_previous_list_in_scope;
5819
5820 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5821 if (!attr)
5822 return;
5823
5824 /* Note that following die references may follow to a die in a
5825 different cu. */
5826
5827 origin_cu = cu;
5828 origin_die = follow_die_ref (die, attr, &origin_cu);
5829
5830 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5831 symbols in. */
5832 origin_previous_list_in_scope = origin_cu->list_in_scope;
5833 origin_cu->list_in_scope = cu->list_in_scope;
5834
5835 if (die->tag != origin_die->tag
5836 && !(die->tag == DW_TAG_inlined_subroutine
5837 && origin_die->tag == DW_TAG_subprogram))
5838 complaint (&symfile_complaints,
5839 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5840 die->offset, origin_die->offset);
5841
5842 child_die = die->child;
5843 die_children_count = 0;
5844 while (child_die && child_die->tag)
5845 {
5846 child_die = sibling_die (child_die);
5847 die_children_count++;
5848 }
5849 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5850 cleanups = make_cleanup (xfree, offsets);
5851
5852 offsets_end = offsets;
5853 child_die = die->child;
5854 while (child_die && child_die->tag)
5855 {
5856 /* For each CHILD_DIE, find the corresponding child of
5857 ORIGIN_DIE. If there is more than one layer of
5858 DW_AT_abstract_origin, follow them all; there shouldn't be,
5859 but GCC versions at least through 4.4 generate this (GCC PR
5860 40573). */
5861 struct die_info *child_origin_die = child_die;
5862 struct dwarf2_cu *child_origin_cu = cu;
5863
5864 while (1)
5865 {
5866 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5867 child_origin_cu);
5868 if (attr == NULL)
5869 break;
5870 child_origin_die = follow_die_ref (child_origin_die, attr,
5871 &child_origin_cu);
5872 }
5873
5874 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5875 counterpart may exist. */
5876 if (child_origin_die != child_die)
5877 {
5878 if (child_die->tag != child_origin_die->tag
5879 && !(child_die->tag == DW_TAG_inlined_subroutine
5880 && child_origin_die->tag == DW_TAG_subprogram))
5881 complaint (&symfile_complaints,
5882 _("Child DIE 0x%x and its abstract origin 0x%x have "
5883 "different tags"), child_die->offset,
5884 child_origin_die->offset);
5885 if (child_origin_die->parent != origin_die)
5886 complaint (&symfile_complaints,
5887 _("Child DIE 0x%x and its abstract origin 0x%x have "
5888 "different parents"), child_die->offset,
5889 child_origin_die->offset);
5890 else
5891 *offsets_end++ = child_origin_die->offset;
5892 }
5893 child_die = sibling_die (child_die);
5894 }
5895 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5896 unsigned_int_compar);
5897 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5898 if (offsetp[-1] == *offsetp)
5899 complaint (&symfile_complaints,
5900 _("Multiple children of DIE 0x%x refer "
5901 "to DIE 0x%x as their abstract origin"),
5902 die->offset, *offsetp);
5903
5904 offsetp = offsets;
5905 origin_child_die = origin_die->child;
5906 while (origin_child_die && origin_child_die->tag)
5907 {
5908 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5909 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5910 offsetp++;
5911 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5912 {
5913 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5914 process_die (origin_child_die, origin_cu);
5915 }
5916 origin_child_die = sibling_die (origin_child_die);
5917 }
5918 origin_cu->list_in_scope = origin_previous_list_in_scope;
5919
5920 do_cleanups (cleanups);
5921 }
5922
5923 static void
5924 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5925 {
5926 struct objfile *objfile = cu->objfile;
5927 struct context_stack *new;
5928 CORE_ADDR lowpc;
5929 CORE_ADDR highpc;
5930 struct die_info *child_die;
5931 struct attribute *attr, *call_line, *call_file;
5932 char *name;
5933 CORE_ADDR baseaddr;
5934 struct block *block;
5935 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5936 VEC (symbolp) *template_args = NULL;
5937 struct template_symbol *templ_func = NULL;
5938
5939 if (inlined_func)
5940 {
5941 /* If we do not have call site information, we can't show the
5942 caller of this inlined function. That's too confusing, so
5943 only use the scope for local variables. */
5944 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5945 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5946 if (call_line == NULL || call_file == NULL)
5947 {
5948 read_lexical_block_scope (die, cu);
5949 return;
5950 }
5951 }
5952
5953 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5954
5955 name = dwarf2_name (die, cu);
5956
5957 /* Ignore functions with missing or empty names. These are actually
5958 illegal according to the DWARF standard. */
5959 if (name == NULL)
5960 {
5961 complaint (&symfile_complaints,
5962 _("missing name for subprogram DIE at %d"), die->offset);
5963 return;
5964 }
5965
5966 /* Ignore functions with missing or invalid low and high pc attributes. */
5967 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5968 {
5969 attr = dwarf2_attr (die, DW_AT_external, cu);
5970 if (!attr || !DW_UNSND (attr))
5971 complaint (&symfile_complaints,
5972 _("cannot get low and high bounds "
5973 "for subprogram DIE at %d"),
5974 die->offset);
5975 return;
5976 }
5977
5978 lowpc += baseaddr;
5979 highpc += baseaddr;
5980
5981 /* If we have any template arguments, then we must allocate a
5982 different sort of symbol. */
5983 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5984 {
5985 if (child_die->tag == DW_TAG_template_type_param
5986 || child_die->tag == DW_TAG_template_value_param)
5987 {
5988 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5989 struct template_symbol);
5990 templ_func->base.is_cplus_template_function = 1;
5991 break;
5992 }
5993 }
5994
5995 new = push_context (0, lowpc);
5996 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5997 (struct symbol *) templ_func);
5998
5999 /* If there is a location expression for DW_AT_frame_base, record
6000 it. */
6001 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6002 if (attr)
6003 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6004 expression is being recorded directly in the function's symbol
6005 and not in a separate frame-base object. I guess this hack is
6006 to avoid adding some sort of frame-base adjunct/annex to the
6007 function's symbol :-(. The problem with doing this is that it
6008 results in a function symbol with a location expression that
6009 has nothing to do with the location of the function, ouch! The
6010 relationship should be: a function's symbol has-a frame base; a
6011 frame-base has-a location expression. */
6012 dwarf2_symbol_mark_computed (attr, new->name, cu);
6013
6014 cu->list_in_scope = &local_symbols;
6015
6016 if (die->child != NULL)
6017 {
6018 child_die = die->child;
6019 while (child_die && child_die->tag)
6020 {
6021 if (child_die->tag == DW_TAG_template_type_param
6022 || child_die->tag == DW_TAG_template_value_param)
6023 {
6024 struct symbol *arg = new_symbol (child_die, NULL, cu);
6025
6026 if (arg != NULL)
6027 VEC_safe_push (symbolp, template_args, arg);
6028 }
6029 else
6030 process_die (child_die, cu);
6031 child_die = sibling_die (child_die);
6032 }
6033 }
6034
6035 inherit_abstract_dies (die, cu);
6036
6037 /* If we have a DW_AT_specification, we might need to import using
6038 directives from the context of the specification DIE. See the
6039 comment in determine_prefix. */
6040 if (cu->language == language_cplus
6041 && dwarf2_attr (die, DW_AT_specification, cu))
6042 {
6043 struct dwarf2_cu *spec_cu = cu;
6044 struct die_info *spec_die = die_specification (die, &spec_cu);
6045
6046 while (spec_die)
6047 {
6048 child_die = spec_die->child;
6049 while (child_die && child_die->tag)
6050 {
6051 if (child_die->tag == DW_TAG_imported_module)
6052 process_die (child_die, spec_cu);
6053 child_die = sibling_die (child_die);
6054 }
6055
6056 /* In some cases, GCC generates specification DIEs that
6057 themselves contain DW_AT_specification attributes. */
6058 spec_die = die_specification (spec_die, &spec_cu);
6059 }
6060 }
6061
6062 new = pop_context ();
6063 /* Make a block for the local symbols within. */
6064 block = finish_block (new->name, &local_symbols, new->old_blocks,
6065 lowpc, highpc, objfile);
6066
6067 /* For C++, set the block's scope. */
6068 if (cu->language == language_cplus || cu->language == language_fortran)
6069 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6070 determine_prefix (die, cu),
6071 processing_has_namespace_info);
6072
6073 /* If we have address ranges, record them. */
6074 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6075
6076 /* Attach template arguments to function. */
6077 if (! VEC_empty (symbolp, template_args))
6078 {
6079 gdb_assert (templ_func != NULL);
6080
6081 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6082 templ_func->template_arguments
6083 = obstack_alloc (&objfile->objfile_obstack,
6084 (templ_func->n_template_arguments
6085 * sizeof (struct symbol *)));
6086 memcpy (templ_func->template_arguments,
6087 VEC_address (symbolp, template_args),
6088 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6089 VEC_free (symbolp, template_args);
6090 }
6091
6092 /* In C++, we can have functions nested inside functions (e.g., when
6093 a function declares a class that has methods). This means that
6094 when we finish processing a function scope, we may need to go
6095 back to building a containing block's symbol lists. */
6096 local_symbols = new->locals;
6097 param_symbols = new->params;
6098 using_directives = new->using_directives;
6099
6100 /* If we've finished processing a top-level function, subsequent
6101 symbols go in the file symbol list. */
6102 if (outermost_context_p ())
6103 cu->list_in_scope = &file_symbols;
6104 }
6105
6106 /* Process all the DIES contained within a lexical block scope. Start
6107 a new scope, process the dies, and then close the scope. */
6108
6109 static void
6110 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6111 {
6112 struct objfile *objfile = cu->objfile;
6113 struct context_stack *new;
6114 CORE_ADDR lowpc, highpc;
6115 struct die_info *child_die;
6116 CORE_ADDR baseaddr;
6117
6118 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6119
6120 /* Ignore blocks with missing or invalid low and high pc attributes. */
6121 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6122 as multiple lexical blocks? Handling children in a sane way would
6123 be nasty. Might be easier to properly extend generic blocks to
6124 describe ranges. */
6125 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6126 return;
6127 lowpc += baseaddr;
6128 highpc += baseaddr;
6129
6130 push_context (0, lowpc);
6131 if (die->child != NULL)
6132 {
6133 child_die = die->child;
6134 while (child_die && child_die->tag)
6135 {
6136 process_die (child_die, cu);
6137 child_die = sibling_die (child_die);
6138 }
6139 }
6140 new = pop_context ();
6141
6142 if (local_symbols != NULL || using_directives != NULL)
6143 {
6144 struct block *block
6145 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6146 highpc, objfile);
6147
6148 /* Note that recording ranges after traversing children, as we
6149 do here, means that recording a parent's ranges entails
6150 walking across all its children's ranges as they appear in
6151 the address map, which is quadratic behavior.
6152
6153 It would be nicer to record the parent's ranges before
6154 traversing its children, simply overriding whatever you find
6155 there. But since we don't even decide whether to create a
6156 block until after we've traversed its children, that's hard
6157 to do. */
6158 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6159 }
6160 local_symbols = new->locals;
6161 using_directives = new->using_directives;
6162 }
6163
6164 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6165
6166 static void
6167 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6168 {
6169 struct objfile *objfile = cu->objfile;
6170 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6171 CORE_ADDR pc, baseaddr;
6172 struct attribute *attr;
6173 struct call_site *call_site, call_site_local;
6174 void **slot;
6175 int nparams;
6176 struct die_info *child_die;
6177
6178 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6179
6180 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6181 if (!attr)
6182 {
6183 complaint (&symfile_complaints,
6184 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6185 "DIE 0x%x [in module %s]"),
6186 die->offset, objfile->name);
6187 return;
6188 }
6189 pc = DW_ADDR (attr) + baseaddr;
6190
6191 if (cu->call_site_htab == NULL)
6192 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6193 NULL, &objfile->objfile_obstack,
6194 hashtab_obstack_allocate, NULL);
6195 call_site_local.pc = pc;
6196 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6197 if (*slot != NULL)
6198 {
6199 complaint (&symfile_complaints,
6200 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6201 "DIE 0x%x [in module %s]"),
6202 paddress (gdbarch, pc), die->offset, objfile->name);
6203 return;
6204 }
6205
6206 /* Count parameters at the caller. */
6207
6208 nparams = 0;
6209 for (child_die = die->child; child_die && child_die->tag;
6210 child_die = sibling_die (child_die))
6211 {
6212 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6213 {
6214 complaint (&symfile_complaints,
6215 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6216 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6217 child_die->tag, child_die->offset, objfile->name);
6218 continue;
6219 }
6220
6221 nparams++;
6222 }
6223
6224 call_site = obstack_alloc (&objfile->objfile_obstack,
6225 (sizeof (*call_site)
6226 + (sizeof (*call_site->parameter)
6227 * (nparams - 1))));
6228 *slot = call_site;
6229 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6230 call_site->pc = pc;
6231
6232 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6233 {
6234 struct die_info *func_die;
6235
6236 /* Skip also over DW_TAG_inlined_subroutine. */
6237 for (func_die = die->parent;
6238 func_die && func_die->tag != DW_TAG_subprogram
6239 && func_die->tag != DW_TAG_subroutine_type;
6240 func_die = func_die->parent);
6241
6242 /* DW_AT_GNU_all_call_sites is a superset
6243 of DW_AT_GNU_all_tail_call_sites. */
6244 if (func_die
6245 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6246 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6247 {
6248 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6249 not complete. But keep CALL_SITE for look ups via call_site_htab,
6250 both the initial caller containing the real return address PC and
6251 the final callee containing the current PC of a chain of tail
6252 calls do not need to have the tail call list complete. But any
6253 function candidate for a virtual tail call frame searched via
6254 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6255 determined unambiguously. */
6256 }
6257 else
6258 {
6259 struct type *func_type = NULL;
6260
6261 if (func_die)
6262 func_type = get_die_type (func_die, cu);
6263 if (func_type != NULL)
6264 {
6265 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6266
6267 /* Enlist this call site to the function. */
6268 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6269 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6270 }
6271 else
6272 complaint (&symfile_complaints,
6273 _("Cannot find function owning DW_TAG_GNU_call_site "
6274 "DIE 0x%x [in module %s]"),
6275 die->offset, objfile->name);
6276 }
6277 }
6278
6279 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6280 if (attr == NULL)
6281 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6282 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6283 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6284 /* Keep NULL DWARF_BLOCK. */;
6285 else if (attr_form_is_block (attr))
6286 {
6287 struct dwarf2_locexpr_baton *dlbaton;
6288
6289 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6290 dlbaton->data = DW_BLOCK (attr)->data;
6291 dlbaton->size = DW_BLOCK (attr)->size;
6292 dlbaton->per_cu = cu->per_cu;
6293
6294 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6295 }
6296 else if (is_ref_attr (attr))
6297 {
6298 struct dwarf2_cu *target_cu = cu;
6299 struct die_info *target_die;
6300
6301 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6302 gdb_assert (target_cu->objfile == objfile);
6303 if (die_is_declaration (target_die, target_cu))
6304 {
6305 const char *target_physname;
6306
6307 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6308 if (target_physname == NULL)
6309 complaint (&symfile_complaints,
6310 _("DW_AT_GNU_call_site_target target DIE has invalid "
6311 "physname, for referencing DIE 0x%x [in module %s]"),
6312 die->offset, objfile->name);
6313 else
6314 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6315 }
6316 else
6317 {
6318 CORE_ADDR lowpc;
6319
6320 /* DW_AT_entry_pc should be preferred. */
6321 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6322 complaint (&symfile_complaints,
6323 _("DW_AT_GNU_call_site_target target DIE has invalid "
6324 "low pc, for referencing DIE 0x%x [in module %s]"),
6325 die->offset, objfile->name);
6326 else
6327 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6328 }
6329 }
6330 else
6331 complaint (&symfile_complaints,
6332 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6333 "block nor reference, for DIE 0x%x [in module %s]"),
6334 die->offset, objfile->name);
6335
6336 call_site->per_cu = cu->per_cu;
6337
6338 for (child_die = die->child;
6339 child_die && child_die->tag;
6340 child_die = sibling_die (child_die))
6341 {
6342 struct dwarf2_locexpr_baton *dlbaton;
6343 struct call_site_parameter *parameter;
6344
6345 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6346 {
6347 /* Already printed the complaint above. */
6348 continue;
6349 }
6350
6351 gdb_assert (call_site->parameter_count < nparams);
6352 parameter = &call_site->parameter[call_site->parameter_count];
6353
6354 /* DW_AT_location specifies the register number. Value of the data
6355 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6356
6357 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6358 if (!attr || !attr_form_is_block (attr))
6359 {
6360 complaint (&symfile_complaints,
6361 _("No DW_FORM_block* DW_AT_location for "
6362 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6363 child_die->offset, objfile->name);
6364 continue;
6365 }
6366 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6367 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6368 if (parameter->dwarf_reg == -1
6369 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6370 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6371 &parameter->fb_offset))
6372 {
6373 complaint (&symfile_complaints,
6374 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6375 "for DW_FORM_block* DW_AT_location for "
6376 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6377 child_die->offset, objfile->name);
6378 continue;
6379 }
6380
6381 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6382 if (!attr_form_is_block (attr))
6383 {
6384 complaint (&symfile_complaints,
6385 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6386 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6387 child_die->offset, objfile->name);
6388 continue;
6389 }
6390 parameter->value = DW_BLOCK (attr)->data;
6391 parameter->value_size = DW_BLOCK (attr)->size;
6392
6393 /* Parameters are not pre-cleared by memset above. */
6394 parameter->data_value = NULL;
6395 parameter->data_value_size = 0;
6396 call_site->parameter_count++;
6397
6398 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6399 if (attr)
6400 {
6401 if (!attr_form_is_block (attr))
6402 complaint (&symfile_complaints,
6403 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6404 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6405 child_die->offset, objfile->name);
6406 else
6407 {
6408 parameter->data_value = DW_BLOCK (attr)->data;
6409 parameter->data_value_size = DW_BLOCK (attr)->size;
6410 }
6411 }
6412 }
6413 }
6414
6415 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6416 Return 1 if the attributes are present and valid, otherwise, return 0.
6417 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6418
6419 static int
6420 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6421 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6422 struct partial_symtab *ranges_pst)
6423 {
6424 struct objfile *objfile = cu->objfile;
6425 struct comp_unit_head *cu_header = &cu->header;
6426 bfd *obfd = objfile->obfd;
6427 unsigned int addr_size = cu_header->addr_size;
6428 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6429 /* Base address selection entry. */
6430 CORE_ADDR base;
6431 int found_base;
6432 unsigned int dummy;
6433 gdb_byte *buffer;
6434 CORE_ADDR marker;
6435 int low_set;
6436 CORE_ADDR low = 0;
6437 CORE_ADDR high = 0;
6438 CORE_ADDR baseaddr;
6439
6440 found_base = cu->base_known;
6441 base = cu->base_address;
6442
6443 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6444 if (offset >= dwarf2_per_objfile->ranges.size)
6445 {
6446 complaint (&symfile_complaints,
6447 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6448 offset);
6449 return 0;
6450 }
6451 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6452
6453 /* Read in the largest possible address. */
6454 marker = read_address (obfd, buffer, cu, &dummy);
6455 if ((marker & mask) == mask)
6456 {
6457 /* If we found the largest possible address, then
6458 read the base address. */
6459 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6460 buffer += 2 * addr_size;
6461 offset += 2 * addr_size;
6462 found_base = 1;
6463 }
6464
6465 low_set = 0;
6466
6467 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6468
6469 while (1)
6470 {
6471 CORE_ADDR range_beginning, range_end;
6472
6473 range_beginning = read_address (obfd, buffer, cu, &dummy);
6474 buffer += addr_size;
6475 range_end = read_address (obfd, buffer, cu, &dummy);
6476 buffer += addr_size;
6477 offset += 2 * addr_size;
6478
6479 /* An end of list marker is a pair of zero addresses. */
6480 if (range_beginning == 0 && range_end == 0)
6481 /* Found the end of list entry. */
6482 break;
6483
6484 /* Each base address selection entry is a pair of 2 values.
6485 The first is the largest possible address, the second is
6486 the base address. Check for a base address here. */
6487 if ((range_beginning & mask) == mask)
6488 {
6489 /* If we found the largest possible address, then
6490 read the base address. */
6491 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6492 found_base = 1;
6493 continue;
6494 }
6495
6496 if (!found_base)
6497 {
6498 /* We have no valid base address for the ranges
6499 data. */
6500 complaint (&symfile_complaints,
6501 _("Invalid .debug_ranges data (no base address)"));
6502 return 0;
6503 }
6504
6505 if (range_beginning > range_end)
6506 {
6507 /* Inverted range entries are invalid. */
6508 complaint (&symfile_complaints,
6509 _("Invalid .debug_ranges data (inverted range)"));
6510 return 0;
6511 }
6512
6513 /* Empty range entries have no effect. */
6514 if (range_beginning == range_end)
6515 continue;
6516
6517 range_beginning += base;
6518 range_end += base;
6519
6520 if (ranges_pst != NULL)
6521 addrmap_set_empty (objfile->psymtabs_addrmap,
6522 range_beginning + baseaddr,
6523 range_end - 1 + baseaddr,
6524 ranges_pst);
6525
6526 /* FIXME: This is recording everything as a low-high
6527 segment of consecutive addresses. We should have a
6528 data structure for discontiguous block ranges
6529 instead. */
6530 if (! low_set)
6531 {
6532 low = range_beginning;
6533 high = range_end;
6534 low_set = 1;
6535 }
6536 else
6537 {
6538 if (range_beginning < low)
6539 low = range_beginning;
6540 if (range_end > high)
6541 high = range_end;
6542 }
6543 }
6544
6545 if (! low_set)
6546 /* If the first entry is an end-of-list marker, the range
6547 describes an empty scope, i.e. no instructions. */
6548 return 0;
6549
6550 if (low_return)
6551 *low_return = low;
6552 if (high_return)
6553 *high_return = high;
6554 return 1;
6555 }
6556
6557 /* Get low and high pc attributes from a die. Return 1 if the attributes
6558 are present and valid, otherwise, return 0. Return -1 if the range is
6559 discontinuous, i.e. derived from DW_AT_ranges information. */
6560 static int
6561 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6562 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6563 struct partial_symtab *pst)
6564 {
6565 struct attribute *attr;
6566 CORE_ADDR low = 0;
6567 CORE_ADDR high = 0;
6568 int ret = 0;
6569
6570 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6571 if (attr)
6572 {
6573 high = DW_ADDR (attr);
6574 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6575 if (attr)
6576 low = DW_ADDR (attr);
6577 else
6578 /* Found high w/o low attribute. */
6579 return 0;
6580
6581 /* Found consecutive range of addresses. */
6582 ret = 1;
6583 }
6584 else
6585 {
6586 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6587 if (attr != NULL)
6588 {
6589 /* Value of the DW_AT_ranges attribute is the offset in the
6590 .debug_ranges section. */
6591 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6592 return 0;
6593 /* Found discontinuous range of addresses. */
6594 ret = -1;
6595 }
6596 }
6597
6598 /* read_partial_die has also the strict LOW < HIGH requirement. */
6599 if (high <= low)
6600 return 0;
6601
6602 /* When using the GNU linker, .gnu.linkonce. sections are used to
6603 eliminate duplicate copies of functions and vtables and such.
6604 The linker will arbitrarily choose one and discard the others.
6605 The AT_*_pc values for such functions refer to local labels in
6606 these sections. If the section from that file was discarded, the
6607 labels are not in the output, so the relocs get a value of 0.
6608 If this is a discarded function, mark the pc bounds as invalid,
6609 so that GDB will ignore it. */
6610 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6611 return 0;
6612
6613 *lowpc = low;
6614 if (highpc)
6615 *highpc = high;
6616 return ret;
6617 }
6618
6619 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6620 its low and high PC addresses. Do nothing if these addresses could not
6621 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6622 and HIGHPC to the high address if greater than HIGHPC. */
6623
6624 static void
6625 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6626 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6627 struct dwarf2_cu *cu)
6628 {
6629 CORE_ADDR low, high;
6630 struct die_info *child = die->child;
6631
6632 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6633 {
6634 *lowpc = min (*lowpc, low);
6635 *highpc = max (*highpc, high);
6636 }
6637
6638 /* If the language does not allow nested subprograms (either inside
6639 subprograms or lexical blocks), we're done. */
6640 if (cu->language != language_ada)
6641 return;
6642
6643 /* Check all the children of the given DIE. If it contains nested
6644 subprograms, then check their pc bounds. Likewise, we need to
6645 check lexical blocks as well, as they may also contain subprogram
6646 definitions. */
6647 while (child && child->tag)
6648 {
6649 if (child->tag == DW_TAG_subprogram
6650 || child->tag == DW_TAG_lexical_block)
6651 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6652 child = sibling_die (child);
6653 }
6654 }
6655
6656 /* Get the low and high pc's represented by the scope DIE, and store
6657 them in *LOWPC and *HIGHPC. If the correct values can't be
6658 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6659
6660 static void
6661 get_scope_pc_bounds (struct die_info *die,
6662 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6663 struct dwarf2_cu *cu)
6664 {
6665 CORE_ADDR best_low = (CORE_ADDR) -1;
6666 CORE_ADDR best_high = (CORE_ADDR) 0;
6667 CORE_ADDR current_low, current_high;
6668
6669 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6670 {
6671 best_low = current_low;
6672 best_high = current_high;
6673 }
6674 else
6675 {
6676 struct die_info *child = die->child;
6677
6678 while (child && child->tag)
6679 {
6680 switch (child->tag) {
6681 case DW_TAG_subprogram:
6682 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6683 break;
6684 case DW_TAG_namespace:
6685 case DW_TAG_module:
6686 /* FIXME: carlton/2004-01-16: Should we do this for
6687 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6688 that current GCC's always emit the DIEs corresponding
6689 to definitions of methods of classes as children of a
6690 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6691 the DIEs giving the declarations, which could be
6692 anywhere). But I don't see any reason why the
6693 standards says that they have to be there. */
6694 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6695
6696 if (current_low != ((CORE_ADDR) -1))
6697 {
6698 best_low = min (best_low, current_low);
6699 best_high = max (best_high, current_high);
6700 }
6701 break;
6702 default:
6703 /* Ignore. */
6704 break;
6705 }
6706
6707 child = sibling_die (child);
6708 }
6709 }
6710
6711 *lowpc = best_low;
6712 *highpc = best_high;
6713 }
6714
6715 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6716 in DIE. */
6717 static void
6718 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6719 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6720 {
6721 struct objfile *objfile = cu->objfile;
6722 struct attribute *attr;
6723
6724 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6725 if (attr)
6726 {
6727 CORE_ADDR high = DW_ADDR (attr);
6728
6729 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6730 if (attr)
6731 {
6732 CORE_ADDR low = DW_ADDR (attr);
6733
6734 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6735 }
6736 }
6737
6738 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6739 if (attr)
6740 {
6741 bfd *obfd = objfile->obfd;
6742
6743 /* The value of the DW_AT_ranges attribute is the offset of the
6744 address range list in the .debug_ranges section. */
6745 unsigned long offset = DW_UNSND (attr);
6746 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6747
6748 /* For some target architectures, but not others, the
6749 read_address function sign-extends the addresses it returns.
6750 To recognize base address selection entries, we need a
6751 mask. */
6752 unsigned int addr_size = cu->header.addr_size;
6753 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6754
6755 /* The base address, to which the next pair is relative. Note
6756 that this 'base' is a DWARF concept: most entries in a range
6757 list are relative, to reduce the number of relocs against the
6758 debugging information. This is separate from this function's
6759 'baseaddr' argument, which GDB uses to relocate debugging
6760 information from a shared library based on the address at
6761 which the library was loaded. */
6762 CORE_ADDR base = cu->base_address;
6763 int base_known = cu->base_known;
6764
6765 gdb_assert (dwarf2_per_objfile->ranges.readin);
6766 if (offset >= dwarf2_per_objfile->ranges.size)
6767 {
6768 complaint (&symfile_complaints,
6769 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6770 offset);
6771 return;
6772 }
6773
6774 for (;;)
6775 {
6776 unsigned int bytes_read;
6777 CORE_ADDR start, end;
6778
6779 start = read_address (obfd, buffer, cu, &bytes_read);
6780 buffer += bytes_read;
6781 end = read_address (obfd, buffer, cu, &bytes_read);
6782 buffer += bytes_read;
6783
6784 /* Did we find the end of the range list? */
6785 if (start == 0 && end == 0)
6786 break;
6787
6788 /* Did we find a base address selection entry? */
6789 else if ((start & base_select_mask) == base_select_mask)
6790 {
6791 base = end;
6792 base_known = 1;
6793 }
6794
6795 /* We found an ordinary address range. */
6796 else
6797 {
6798 if (!base_known)
6799 {
6800 complaint (&symfile_complaints,
6801 _("Invalid .debug_ranges data "
6802 "(no base address)"));
6803 return;
6804 }
6805
6806 if (start > end)
6807 {
6808 /* Inverted range entries are invalid. */
6809 complaint (&symfile_complaints,
6810 _("Invalid .debug_ranges data "
6811 "(inverted range)"));
6812 return;
6813 }
6814
6815 /* Empty range entries have no effect. */
6816 if (start == end)
6817 continue;
6818
6819 record_block_range (block,
6820 baseaddr + base + start,
6821 baseaddr + base + end - 1);
6822 }
6823 }
6824 }
6825 }
6826
6827 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6828 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6829 during 4.6.0 experimental. */
6830
6831 static int
6832 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6833 {
6834 const char *cs;
6835 int major, minor, release;
6836 int result = 0;
6837
6838 if (cu->producer == NULL)
6839 {
6840 /* For unknown compilers expect their behavior is DWARF version
6841 compliant.
6842
6843 GCC started to support .debug_types sections by -gdwarf-4 since
6844 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6845 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6846 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6847 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6848
6849 return 0;
6850 }
6851
6852 if (cu->checked_producer)
6853 return cu->producer_is_gxx_lt_4_6;
6854
6855 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6856
6857 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6858 {
6859 /* For non-GCC compilers expect their behavior is DWARF version
6860 compliant. */
6861 }
6862 else
6863 {
6864 cs = &cu->producer[strlen ("GNU ")];
6865 while (*cs && !isdigit (*cs))
6866 cs++;
6867 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6868 {
6869 /* Not recognized as GCC. */
6870 }
6871 else
6872 result = major < 4 || (major == 4 && minor < 6);
6873 }
6874
6875 cu->checked_producer = 1;
6876 cu->producer_is_gxx_lt_4_6 = result;
6877
6878 return result;
6879 }
6880
6881 /* Return the default accessibility type if it is not overriden by
6882 DW_AT_accessibility. */
6883
6884 static enum dwarf_access_attribute
6885 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6886 {
6887 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6888 {
6889 /* The default DWARF 2 accessibility for members is public, the default
6890 accessibility for inheritance is private. */
6891
6892 if (die->tag != DW_TAG_inheritance)
6893 return DW_ACCESS_public;
6894 else
6895 return DW_ACCESS_private;
6896 }
6897 else
6898 {
6899 /* DWARF 3+ defines the default accessibility a different way. The same
6900 rules apply now for DW_TAG_inheritance as for the members and it only
6901 depends on the container kind. */
6902
6903 if (die->parent->tag == DW_TAG_class_type)
6904 return DW_ACCESS_private;
6905 else
6906 return DW_ACCESS_public;
6907 }
6908 }
6909
6910 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6911 offset. If the attribute was not found return 0, otherwise return
6912 1. If it was found but could not properly be handled, set *OFFSET
6913 to 0. */
6914
6915 static int
6916 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6917 LONGEST *offset)
6918 {
6919 struct attribute *attr;
6920
6921 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6922 if (attr != NULL)
6923 {
6924 *offset = 0;
6925
6926 /* Note that we do not check for a section offset first here.
6927 This is because DW_AT_data_member_location is new in DWARF 4,
6928 so if we see it, we can assume that a constant form is really
6929 a constant and not a section offset. */
6930 if (attr_form_is_constant (attr))
6931 *offset = dwarf2_get_attr_constant_value (attr, 0);
6932 else if (attr_form_is_section_offset (attr))
6933 dwarf2_complex_location_expr_complaint ();
6934 else if (attr_form_is_block (attr))
6935 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6936 else
6937 dwarf2_complex_location_expr_complaint ();
6938
6939 return 1;
6940 }
6941
6942 return 0;
6943 }
6944
6945 /* Add an aggregate field to the field list. */
6946
6947 static void
6948 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6949 struct dwarf2_cu *cu)
6950 {
6951 struct objfile *objfile = cu->objfile;
6952 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6953 struct nextfield *new_field;
6954 struct attribute *attr;
6955 struct field *fp;
6956 char *fieldname = "";
6957
6958 /* Allocate a new field list entry and link it in. */
6959 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6960 make_cleanup (xfree, new_field);
6961 memset (new_field, 0, sizeof (struct nextfield));
6962
6963 if (die->tag == DW_TAG_inheritance)
6964 {
6965 new_field->next = fip->baseclasses;
6966 fip->baseclasses = new_field;
6967 }
6968 else
6969 {
6970 new_field->next = fip->fields;
6971 fip->fields = new_field;
6972 }
6973 fip->nfields++;
6974
6975 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6976 if (attr)
6977 new_field->accessibility = DW_UNSND (attr);
6978 else
6979 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6980 if (new_field->accessibility != DW_ACCESS_public)
6981 fip->non_public_fields = 1;
6982
6983 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6984 if (attr)
6985 new_field->virtuality = DW_UNSND (attr);
6986 else
6987 new_field->virtuality = DW_VIRTUALITY_none;
6988
6989 fp = &new_field->field;
6990
6991 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6992 {
6993 LONGEST offset;
6994
6995 /* Data member other than a C++ static data member. */
6996
6997 /* Get type of field. */
6998 fp->type = die_type (die, cu);
6999
7000 SET_FIELD_BITPOS (*fp, 0);
7001
7002 /* Get bit size of field (zero if none). */
7003 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7004 if (attr)
7005 {
7006 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7007 }
7008 else
7009 {
7010 FIELD_BITSIZE (*fp) = 0;
7011 }
7012
7013 /* Get bit offset of field. */
7014 if (handle_data_member_location (die, cu, &offset))
7015 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7016 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7017 if (attr)
7018 {
7019 if (gdbarch_bits_big_endian (gdbarch))
7020 {
7021 /* For big endian bits, the DW_AT_bit_offset gives the
7022 additional bit offset from the MSB of the containing
7023 anonymous object to the MSB of the field. We don't
7024 have to do anything special since we don't need to
7025 know the size of the anonymous object. */
7026 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7027 }
7028 else
7029 {
7030 /* For little endian bits, compute the bit offset to the
7031 MSB of the anonymous object, subtract off the number of
7032 bits from the MSB of the field to the MSB of the
7033 object, and then subtract off the number of bits of
7034 the field itself. The result is the bit offset of
7035 the LSB of the field. */
7036 int anonymous_size;
7037 int bit_offset = DW_UNSND (attr);
7038
7039 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7040 if (attr)
7041 {
7042 /* The size of the anonymous object containing
7043 the bit field is explicit, so use the
7044 indicated size (in bytes). */
7045 anonymous_size = DW_UNSND (attr);
7046 }
7047 else
7048 {
7049 /* The size of the anonymous object containing
7050 the bit field must be inferred from the type
7051 attribute of the data member containing the
7052 bit field. */
7053 anonymous_size = TYPE_LENGTH (fp->type);
7054 }
7055 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7056 - bit_offset - FIELD_BITSIZE (*fp);
7057 }
7058 }
7059
7060 /* Get name of field. */
7061 fieldname = dwarf2_name (die, cu);
7062 if (fieldname == NULL)
7063 fieldname = "";
7064
7065 /* The name is already allocated along with this objfile, so we don't
7066 need to duplicate it for the type. */
7067 fp->name = fieldname;
7068
7069 /* Change accessibility for artificial fields (e.g. virtual table
7070 pointer or virtual base class pointer) to private. */
7071 if (dwarf2_attr (die, DW_AT_artificial, cu))
7072 {
7073 FIELD_ARTIFICIAL (*fp) = 1;
7074 new_field->accessibility = DW_ACCESS_private;
7075 fip->non_public_fields = 1;
7076 }
7077 }
7078 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7079 {
7080 /* C++ static member. */
7081
7082 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7083 is a declaration, but all versions of G++ as of this writing
7084 (so through at least 3.2.1) incorrectly generate
7085 DW_TAG_variable tags. */
7086
7087 const char *physname;
7088
7089 /* Get name of field. */
7090 fieldname = dwarf2_name (die, cu);
7091 if (fieldname == NULL)
7092 return;
7093
7094 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7095 if (attr
7096 /* Only create a symbol if this is an external value.
7097 new_symbol checks this and puts the value in the global symbol
7098 table, which we want. If it is not external, new_symbol
7099 will try to put the value in cu->list_in_scope which is wrong. */
7100 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7101 {
7102 /* A static const member, not much different than an enum as far as
7103 we're concerned, except that we can support more types. */
7104 new_symbol (die, NULL, cu);
7105 }
7106
7107 /* Get physical name. */
7108 physname = dwarf2_physname (fieldname, die, cu);
7109
7110 /* The name is already allocated along with this objfile, so we don't
7111 need to duplicate it for the type. */
7112 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7113 FIELD_TYPE (*fp) = die_type (die, cu);
7114 FIELD_NAME (*fp) = fieldname;
7115 }
7116 else if (die->tag == DW_TAG_inheritance)
7117 {
7118 LONGEST offset;
7119
7120 /* C++ base class field. */
7121 if (handle_data_member_location (die, cu, &offset))
7122 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7123 FIELD_BITSIZE (*fp) = 0;
7124 FIELD_TYPE (*fp) = die_type (die, cu);
7125 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7126 fip->nbaseclasses++;
7127 }
7128 }
7129
7130 /* Add a typedef defined in the scope of the FIP's class. */
7131
7132 static void
7133 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7134 struct dwarf2_cu *cu)
7135 {
7136 struct objfile *objfile = cu->objfile;
7137 struct typedef_field_list *new_field;
7138 struct attribute *attr;
7139 struct typedef_field *fp;
7140 char *fieldname = "";
7141
7142 /* Allocate a new field list entry and link it in. */
7143 new_field = xzalloc (sizeof (*new_field));
7144 make_cleanup (xfree, new_field);
7145
7146 gdb_assert (die->tag == DW_TAG_typedef);
7147
7148 fp = &new_field->field;
7149
7150 /* Get name of field. */
7151 fp->name = dwarf2_name (die, cu);
7152 if (fp->name == NULL)
7153 return;
7154
7155 fp->type = read_type_die (die, cu);
7156
7157 new_field->next = fip->typedef_field_list;
7158 fip->typedef_field_list = new_field;
7159 fip->typedef_field_list_count++;
7160 }
7161
7162 /* Create the vector of fields, and attach it to the type. */
7163
7164 static void
7165 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7166 struct dwarf2_cu *cu)
7167 {
7168 int nfields = fip->nfields;
7169
7170 /* Record the field count, allocate space for the array of fields,
7171 and create blank accessibility bitfields if necessary. */
7172 TYPE_NFIELDS (type) = nfields;
7173 TYPE_FIELDS (type) = (struct field *)
7174 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7175 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7176
7177 if (fip->non_public_fields && cu->language != language_ada)
7178 {
7179 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7180
7181 TYPE_FIELD_PRIVATE_BITS (type) =
7182 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7183 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7184
7185 TYPE_FIELD_PROTECTED_BITS (type) =
7186 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7187 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7188
7189 TYPE_FIELD_IGNORE_BITS (type) =
7190 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7191 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7192 }
7193
7194 /* If the type has baseclasses, allocate and clear a bit vector for
7195 TYPE_FIELD_VIRTUAL_BITS. */
7196 if (fip->nbaseclasses && cu->language != language_ada)
7197 {
7198 int num_bytes = B_BYTES (fip->nbaseclasses);
7199 unsigned char *pointer;
7200
7201 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7202 pointer = TYPE_ALLOC (type, num_bytes);
7203 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7204 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7205 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7206 }
7207
7208 /* Copy the saved-up fields into the field vector. Start from the head of
7209 the list, adding to the tail of the field array, so that they end up in
7210 the same order in the array in which they were added to the list. */
7211 while (nfields-- > 0)
7212 {
7213 struct nextfield *fieldp;
7214
7215 if (fip->fields)
7216 {
7217 fieldp = fip->fields;
7218 fip->fields = fieldp->next;
7219 }
7220 else
7221 {
7222 fieldp = fip->baseclasses;
7223 fip->baseclasses = fieldp->next;
7224 }
7225
7226 TYPE_FIELD (type, nfields) = fieldp->field;
7227 switch (fieldp->accessibility)
7228 {
7229 case DW_ACCESS_private:
7230 if (cu->language != language_ada)
7231 SET_TYPE_FIELD_PRIVATE (type, nfields);
7232 break;
7233
7234 case DW_ACCESS_protected:
7235 if (cu->language != language_ada)
7236 SET_TYPE_FIELD_PROTECTED (type, nfields);
7237 break;
7238
7239 case DW_ACCESS_public:
7240 break;
7241
7242 default:
7243 /* Unknown accessibility. Complain and treat it as public. */
7244 {
7245 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7246 fieldp->accessibility);
7247 }
7248 break;
7249 }
7250 if (nfields < fip->nbaseclasses)
7251 {
7252 switch (fieldp->virtuality)
7253 {
7254 case DW_VIRTUALITY_virtual:
7255 case DW_VIRTUALITY_pure_virtual:
7256 if (cu->language == language_ada)
7257 error (_("unexpected virtuality in component of Ada type"));
7258 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7259 break;
7260 }
7261 }
7262 }
7263 }
7264
7265 /* Add a member function to the proper fieldlist. */
7266
7267 static void
7268 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7269 struct type *type, struct dwarf2_cu *cu)
7270 {
7271 struct objfile *objfile = cu->objfile;
7272 struct attribute *attr;
7273 struct fnfieldlist *flp;
7274 int i;
7275 struct fn_field *fnp;
7276 char *fieldname;
7277 struct nextfnfield *new_fnfield;
7278 struct type *this_type;
7279 enum dwarf_access_attribute accessibility;
7280
7281 if (cu->language == language_ada)
7282 error (_("unexpected member function in Ada type"));
7283
7284 /* Get name of member function. */
7285 fieldname = dwarf2_name (die, cu);
7286 if (fieldname == NULL)
7287 return;
7288
7289 /* Look up member function name in fieldlist. */
7290 for (i = 0; i < fip->nfnfields; i++)
7291 {
7292 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7293 break;
7294 }
7295
7296 /* Create new list element if necessary. */
7297 if (i < fip->nfnfields)
7298 flp = &fip->fnfieldlists[i];
7299 else
7300 {
7301 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7302 {
7303 fip->fnfieldlists = (struct fnfieldlist *)
7304 xrealloc (fip->fnfieldlists,
7305 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7306 * sizeof (struct fnfieldlist));
7307 if (fip->nfnfields == 0)
7308 make_cleanup (free_current_contents, &fip->fnfieldlists);
7309 }
7310 flp = &fip->fnfieldlists[fip->nfnfields];
7311 flp->name = fieldname;
7312 flp->length = 0;
7313 flp->head = NULL;
7314 i = fip->nfnfields++;
7315 }
7316
7317 /* Create a new member function field and chain it to the field list
7318 entry. */
7319 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7320 make_cleanup (xfree, new_fnfield);
7321 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7322 new_fnfield->next = flp->head;
7323 flp->head = new_fnfield;
7324 flp->length++;
7325
7326 /* Fill in the member function field info. */
7327 fnp = &new_fnfield->fnfield;
7328
7329 /* Delay processing of the physname until later. */
7330 if (cu->language == language_cplus || cu->language == language_java)
7331 {
7332 add_to_method_list (type, i, flp->length - 1, fieldname,
7333 die, cu);
7334 }
7335 else
7336 {
7337 const char *physname = dwarf2_physname (fieldname, die, cu);
7338 fnp->physname = physname ? physname : "";
7339 }
7340
7341 fnp->type = alloc_type (objfile);
7342 this_type = read_type_die (die, cu);
7343 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7344 {
7345 int nparams = TYPE_NFIELDS (this_type);
7346
7347 /* TYPE is the domain of this method, and THIS_TYPE is the type
7348 of the method itself (TYPE_CODE_METHOD). */
7349 smash_to_method_type (fnp->type, type,
7350 TYPE_TARGET_TYPE (this_type),
7351 TYPE_FIELDS (this_type),
7352 TYPE_NFIELDS (this_type),
7353 TYPE_VARARGS (this_type));
7354
7355 /* Handle static member functions.
7356 Dwarf2 has no clean way to discern C++ static and non-static
7357 member functions. G++ helps GDB by marking the first
7358 parameter for non-static member functions (which is the this
7359 pointer) as artificial. We obtain this information from
7360 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7361 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7362 fnp->voffset = VOFFSET_STATIC;
7363 }
7364 else
7365 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7366 dwarf2_full_name (fieldname, die, cu));
7367
7368 /* Get fcontext from DW_AT_containing_type if present. */
7369 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7370 fnp->fcontext = die_containing_type (die, cu);
7371
7372 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7373 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7374
7375 /* Get accessibility. */
7376 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7377 if (attr)
7378 accessibility = DW_UNSND (attr);
7379 else
7380 accessibility = dwarf2_default_access_attribute (die, cu);
7381 switch (accessibility)
7382 {
7383 case DW_ACCESS_private:
7384 fnp->is_private = 1;
7385 break;
7386 case DW_ACCESS_protected:
7387 fnp->is_protected = 1;
7388 break;
7389 }
7390
7391 /* Check for artificial methods. */
7392 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7393 if (attr && DW_UNSND (attr) != 0)
7394 fnp->is_artificial = 1;
7395
7396 /* Get index in virtual function table if it is a virtual member
7397 function. For older versions of GCC, this is an offset in the
7398 appropriate virtual table, as specified by DW_AT_containing_type.
7399 For everyone else, it is an expression to be evaluated relative
7400 to the object address. */
7401
7402 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7403 if (attr)
7404 {
7405 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7406 {
7407 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7408 {
7409 /* Old-style GCC. */
7410 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7411 }
7412 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7413 || (DW_BLOCK (attr)->size > 1
7414 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7415 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7416 {
7417 struct dwarf_block blk;
7418 int offset;
7419
7420 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7421 ? 1 : 2);
7422 blk.size = DW_BLOCK (attr)->size - offset;
7423 blk.data = DW_BLOCK (attr)->data + offset;
7424 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7425 if ((fnp->voffset % cu->header.addr_size) != 0)
7426 dwarf2_complex_location_expr_complaint ();
7427 else
7428 fnp->voffset /= cu->header.addr_size;
7429 fnp->voffset += 2;
7430 }
7431 else
7432 dwarf2_complex_location_expr_complaint ();
7433
7434 if (!fnp->fcontext)
7435 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7436 }
7437 else if (attr_form_is_section_offset (attr))
7438 {
7439 dwarf2_complex_location_expr_complaint ();
7440 }
7441 else
7442 {
7443 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7444 fieldname);
7445 }
7446 }
7447 else
7448 {
7449 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7450 if (attr && DW_UNSND (attr))
7451 {
7452 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7453 complaint (&symfile_complaints,
7454 _("Member function \"%s\" (offset %d) is virtual "
7455 "but the vtable offset is not specified"),
7456 fieldname, die->offset);
7457 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7458 TYPE_CPLUS_DYNAMIC (type) = 1;
7459 }
7460 }
7461 }
7462
7463 /* Create the vector of member function fields, and attach it to the type. */
7464
7465 static void
7466 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7467 struct dwarf2_cu *cu)
7468 {
7469 struct fnfieldlist *flp;
7470 int i;
7471
7472 if (cu->language == language_ada)
7473 error (_("unexpected member functions in Ada type"));
7474
7475 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7476 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7477 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7478
7479 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7480 {
7481 struct nextfnfield *nfp = flp->head;
7482 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7483 int k;
7484
7485 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7486 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7487 fn_flp->fn_fields = (struct fn_field *)
7488 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7489 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7490 fn_flp->fn_fields[k] = nfp->fnfield;
7491 }
7492
7493 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7494 }
7495
7496 /* Returns non-zero if NAME is the name of a vtable member in CU's
7497 language, zero otherwise. */
7498 static int
7499 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7500 {
7501 static const char vptr[] = "_vptr";
7502 static const char vtable[] = "vtable";
7503
7504 /* Look for the C++ and Java forms of the vtable. */
7505 if ((cu->language == language_java
7506 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7507 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7508 && is_cplus_marker (name[sizeof (vptr) - 1])))
7509 return 1;
7510
7511 return 0;
7512 }
7513
7514 /* GCC outputs unnamed structures that are really pointers to member
7515 functions, with the ABI-specified layout. If TYPE describes
7516 such a structure, smash it into a member function type.
7517
7518 GCC shouldn't do this; it should just output pointer to member DIEs.
7519 This is GCC PR debug/28767. */
7520
7521 static void
7522 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7523 {
7524 struct type *pfn_type, *domain_type, *new_type;
7525
7526 /* Check for a structure with no name and two children. */
7527 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7528 return;
7529
7530 /* Check for __pfn and __delta members. */
7531 if (TYPE_FIELD_NAME (type, 0) == NULL
7532 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7533 || TYPE_FIELD_NAME (type, 1) == NULL
7534 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7535 return;
7536
7537 /* Find the type of the method. */
7538 pfn_type = TYPE_FIELD_TYPE (type, 0);
7539 if (pfn_type == NULL
7540 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7541 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7542 return;
7543
7544 /* Look for the "this" argument. */
7545 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7546 if (TYPE_NFIELDS (pfn_type) == 0
7547 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7548 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7549 return;
7550
7551 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7552 new_type = alloc_type (objfile);
7553 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7554 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7555 TYPE_VARARGS (pfn_type));
7556 smash_to_methodptr_type (type, new_type);
7557 }
7558
7559 /* Called when we find the DIE that starts a structure or union scope
7560 (definition) to create a type for the structure or union. Fill in
7561 the type's name and general properties; the members will not be
7562 processed until process_structure_type.
7563
7564 NOTE: we need to call these functions regardless of whether or not the
7565 DIE has a DW_AT_name attribute, since it might be an anonymous
7566 structure or union. This gets the type entered into our set of
7567 user defined types.
7568
7569 However, if the structure is incomplete (an opaque struct/union)
7570 then suppress creating a symbol table entry for it since gdb only
7571 wants to find the one with the complete definition. Note that if
7572 it is complete, we just call new_symbol, which does it's own
7573 checking about whether the struct/union is anonymous or not (and
7574 suppresses creating a symbol table entry itself). */
7575
7576 static struct type *
7577 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7578 {
7579 struct objfile *objfile = cu->objfile;
7580 struct type *type;
7581 struct attribute *attr;
7582 char *name;
7583
7584 /* If the definition of this type lives in .debug_types, read that type.
7585 Don't follow DW_AT_specification though, that will take us back up
7586 the chain and we want to go down. */
7587 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7588 if (attr)
7589 {
7590 struct dwarf2_cu *type_cu = cu;
7591 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7592
7593 /* We could just recurse on read_structure_type, but we need to call
7594 get_die_type to ensure only one type for this DIE is created.
7595 This is important, for example, because for c++ classes we need
7596 TYPE_NAME set which is only done by new_symbol. Blech. */
7597 type = read_type_die (type_die, type_cu);
7598
7599 /* TYPE_CU may not be the same as CU.
7600 Ensure TYPE is recorded in CU's type_hash table. */
7601 return set_die_type (die, type, cu);
7602 }
7603
7604 type = alloc_type (objfile);
7605 INIT_CPLUS_SPECIFIC (type);
7606
7607 name = dwarf2_name (die, cu);
7608 if (name != NULL)
7609 {
7610 if (cu->language == language_cplus
7611 || cu->language == language_java)
7612 {
7613 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7614
7615 /* dwarf2_full_name might have already finished building the DIE's
7616 type. If so, there is no need to continue. */
7617 if (get_die_type (die, cu) != NULL)
7618 return get_die_type (die, cu);
7619
7620 TYPE_TAG_NAME (type) = full_name;
7621 if (die->tag == DW_TAG_structure_type
7622 || die->tag == DW_TAG_class_type)
7623 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7624 }
7625 else
7626 {
7627 /* The name is already allocated along with this objfile, so
7628 we don't need to duplicate it for the type. */
7629 TYPE_TAG_NAME (type) = (char *) name;
7630 if (die->tag == DW_TAG_class_type)
7631 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7632 }
7633 }
7634
7635 if (die->tag == DW_TAG_structure_type)
7636 {
7637 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7638 }
7639 else if (die->tag == DW_TAG_union_type)
7640 {
7641 TYPE_CODE (type) = TYPE_CODE_UNION;
7642 }
7643 else
7644 {
7645 TYPE_CODE (type) = TYPE_CODE_CLASS;
7646 }
7647
7648 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7649 TYPE_DECLARED_CLASS (type) = 1;
7650
7651 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7652 if (attr)
7653 {
7654 TYPE_LENGTH (type) = DW_UNSND (attr);
7655 }
7656 else
7657 {
7658 TYPE_LENGTH (type) = 0;
7659 }
7660
7661 TYPE_STUB_SUPPORTED (type) = 1;
7662 if (die_is_declaration (die, cu))
7663 TYPE_STUB (type) = 1;
7664 else if (attr == NULL && die->child == NULL
7665 && producer_is_realview (cu->producer))
7666 /* RealView does not output the required DW_AT_declaration
7667 on incomplete types. */
7668 TYPE_STUB (type) = 1;
7669
7670 /* We need to add the type field to the die immediately so we don't
7671 infinitely recurse when dealing with pointers to the structure
7672 type within the structure itself. */
7673 set_die_type (die, type, cu);
7674
7675 /* set_die_type should be already done. */
7676 set_descriptive_type (type, die, cu);
7677
7678 return type;
7679 }
7680
7681 /* Finish creating a structure or union type, including filling in
7682 its members and creating a symbol for it. */
7683
7684 static void
7685 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7686 {
7687 struct objfile *objfile = cu->objfile;
7688 struct die_info *child_die = die->child;
7689 struct type *type;
7690
7691 type = get_die_type (die, cu);
7692 if (type == NULL)
7693 type = read_structure_type (die, cu);
7694
7695 if (die->child != NULL && ! die_is_declaration (die, cu))
7696 {
7697 struct field_info fi;
7698 struct die_info *child_die;
7699 VEC (symbolp) *template_args = NULL;
7700 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7701
7702 memset (&fi, 0, sizeof (struct field_info));
7703
7704 child_die = die->child;
7705
7706 while (child_die && child_die->tag)
7707 {
7708 if (child_die->tag == DW_TAG_member
7709 || child_die->tag == DW_TAG_variable)
7710 {
7711 /* NOTE: carlton/2002-11-05: A C++ static data member
7712 should be a DW_TAG_member that is a declaration, but
7713 all versions of G++ as of this writing (so through at
7714 least 3.2.1) incorrectly generate DW_TAG_variable
7715 tags for them instead. */
7716 dwarf2_add_field (&fi, child_die, cu);
7717 }
7718 else if (child_die->tag == DW_TAG_subprogram)
7719 {
7720 /* C++ member function. */
7721 dwarf2_add_member_fn (&fi, child_die, type, cu);
7722 }
7723 else if (child_die->tag == DW_TAG_inheritance)
7724 {
7725 /* C++ base class field. */
7726 dwarf2_add_field (&fi, child_die, cu);
7727 }
7728 else if (child_die->tag == DW_TAG_typedef)
7729 dwarf2_add_typedef (&fi, child_die, cu);
7730 else if (child_die->tag == DW_TAG_template_type_param
7731 || child_die->tag == DW_TAG_template_value_param)
7732 {
7733 struct symbol *arg = new_symbol (child_die, NULL, cu);
7734
7735 if (arg != NULL)
7736 VEC_safe_push (symbolp, template_args, arg);
7737 }
7738
7739 child_die = sibling_die (child_die);
7740 }
7741
7742 /* Attach template arguments to type. */
7743 if (! VEC_empty (symbolp, template_args))
7744 {
7745 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7746 TYPE_N_TEMPLATE_ARGUMENTS (type)
7747 = VEC_length (symbolp, template_args);
7748 TYPE_TEMPLATE_ARGUMENTS (type)
7749 = obstack_alloc (&objfile->objfile_obstack,
7750 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7751 * sizeof (struct symbol *)));
7752 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7753 VEC_address (symbolp, template_args),
7754 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7755 * sizeof (struct symbol *)));
7756 VEC_free (symbolp, template_args);
7757 }
7758
7759 /* Attach fields and member functions to the type. */
7760 if (fi.nfields)
7761 dwarf2_attach_fields_to_type (&fi, type, cu);
7762 if (fi.nfnfields)
7763 {
7764 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7765
7766 /* Get the type which refers to the base class (possibly this
7767 class itself) which contains the vtable pointer for the current
7768 class from the DW_AT_containing_type attribute. This use of
7769 DW_AT_containing_type is a GNU extension. */
7770
7771 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7772 {
7773 struct type *t = die_containing_type (die, cu);
7774
7775 TYPE_VPTR_BASETYPE (type) = t;
7776 if (type == t)
7777 {
7778 int i;
7779
7780 /* Our own class provides vtbl ptr. */
7781 for (i = TYPE_NFIELDS (t) - 1;
7782 i >= TYPE_N_BASECLASSES (t);
7783 --i)
7784 {
7785 const char *fieldname = TYPE_FIELD_NAME (t, i);
7786
7787 if (is_vtable_name (fieldname, cu))
7788 {
7789 TYPE_VPTR_FIELDNO (type) = i;
7790 break;
7791 }
7792 }
7793
7794 /* Complain if virtual function table field not found. */
7795 if (i < TYPE_N_BASECLASSES (t))
7796 complaint (&symfile_complaints,
7797 _("virtual function table pointer "
7798 "not found when defining class '%s'"),
7799 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7800 "");
7801 }
7802 else
7803 {
7804 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7805 }
7806 }
7807 else if (cu->producer
7808 && strncmp (cu->producer,
7809 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7810 {
7811 /* The IBM XLC compiler does not provide direct indication
7812 of the containing type, but the vtable pointer is
7813 always named __vfp. */
7814
7815 int i;
7816
7817 for (i = TYPE_NFIELDS (type) - 1;
7818 i >= TYPE_N_BASECLASSES (type);
7819 --i)
7820 {
7821 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7822 {
7823 TYPE_VPTR_FIELDNO (type) = i;
7824 TYPE_VPTR_BASETYPE (type) = type;
7825 break;
7826 }
7827 }
7828 }
7829 }
7830
7831 /* Copy fi.typedef_field_list linked list elements content into the
7832 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7833 if (fi.typedef_field_list)
7834 {
7835 int i = fi.typedef_field_list_count;
7836
7837 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7838 TYPE_TYPEDEF_FIELD_ARRAY (type)
7839 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7840 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7841
7842 /* Reverse the list order to keep the debug info elements order. */
7843 while (--i >= 0)
7844 {
7845 struct typedef_field *dest, *src;
7846
7847 dest = &TYPE_TYPEDEF_FIELD (type, i);
7848 src = &fi.typedef_field_list->field;
7849 fi.typedef_field_list = fi.typedef_field_list->next;
7850 *dest = *src;
7851 }
7852 }
7853
7854 do_cleanups (back_to);
7855
7856 if (HAVE_CPLUS_STRUCT (type))
7857 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7858 }
7859
7860 quirk_gcc_member_function_pointer (type, objfile);
7861
7862 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7863 snapshots) has been known to create a die giving a declaration
7864 for a class that has, as a child, a die giving a definition for a
7865 nested class. So we have to process our children even if the
7866 current die is a declaration. Normally, of course, a declaration
7867 won't have any children at all. */
7868
7869 while (child_die != NULL && child_die->tag)
7870 {
7871 if (child_die->tag == DW_TAG_member
7872 || child_die->tag == DW_TAG_variable
7873 || child_die->tag == DW_TAG_inheritance
7874 || child_die->tag == DW_TAG_template_value_param
7875 || child_die->tag == DW_TAG_template_type_param)
7876 {
7877 /* Do nothing. */
7878 }
7879 else
7880 process_die (child_die, cu);
7881
7882 child_die = sibling_die (child_die);
7883 }
7884
7885 /* Do not consider external references. According to the DWARF standard,
7886 these DIEs are identified by the fact that they have no byte_size
7887 attribute, and a declaration attribute. */
7888 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7889 || !die_is_declaration (die, cu))
7890 new_symbol (die, type, cu);
7891 }
7892
7893 /* Given a DW_AT_enumeration_type die, set its type. We do not
7894 complete the type's fields yet, or create any symbols. */
7895
7896 static struct type *
7897 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7898 {
7899 struct objfile *objfile = cu->objfile;
7900 struct type *type;
7901 struct attribute *attr;
7902 const char *name;
7903
7904 /* If the definition of this type lives in .debug_types, read that type.
7905 Don't follow DW_AT_specification though, that will take us back up
7906 the chain and we want to go down. */
7907 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7908 if (attr)
7909 {
7910 struct dwarf2_cu *type_cu = cu;
7911 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7912
7913 type = read_type_die (type_die, type_cu);
7914
7915 /* TYPE_CU may not be the same as CU.
7916 Ensure TYPE is recorded in CU's type_hash table. */
7917 return set_die_type (die, type, cu);
7918 }
7919
7920 type = alloc_type (objfile);
7921
7922 TYPE_CODE (type) = TYPE_CODE_ENUM;
7923 name = dwarf2_full_name (NULL, die, cu);
7924 if (name != NULL)
7925 TYPE_TAG_NAME (type) = (char *) name;
7926
7927 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7928 if (attr)
7929 {
7930 TYPE_LENGTH (type) = DW_UNSND (attr);
7931 }
7932 else
7933 {
7934 TYPE_LENGTH (type) = 0;
7935 }
7936
7937 /* The enumeration DIE can be incomplete. In Ada, any type can be
7938 declared as private in the package spec, and then defined only
7939 inside the package body. Such types are known as Taft Amendment
7940 Types. When another package uses such a type, an incomplete DIE
7941 may be generated by the compiler. */
7942 if (die_is_declaration (die, cu))
7943 TYPE_STUB (type) = 1;
7944
7945 return set_die_type (die, type, cu);
7946 }
7947
7948 /* Given a pointer to a die which begins an enumeration, process all
7949 the dies that define the members of the enumeration, and create the
7950 symbol for the enumeration type.
7951
7952 NOTE: We reverse the order of the element list. */
7953
7954 static void
7955 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7956 {
7957 struct type *this_type;
7958
7959 this_type = get_die_type (die, cu);
7960 if (this_type == NULL)
7961 this_type = read_enumeration_type (die, cu);
7962
7963 if (die->child != NULL)
7964 {
7965 struct die_info *child_die;
7966 struct symbol *sym;
7967 struct field *fields = NULL;
7968 int num_fields = 0;
7969 int unsigned_enum = 1;
7970 char *name;
7971 int flag_enum = 1;
7972 ULONGEST mask = 0;
7973
7974 child_die = die->child;
7975 while (child_die && child_die->tag)
7976 {
7977 if (child_die->tag != DW_TAG_enumerator)
7978 {
7979 process_die (child_die, cu);
7980 }
7981 else
7982 {
7983 name = dwarf2_name (child_die, cu);
7984 if (name)
7985 {
7986 sym = new_symbol (child_die, this_type, cu);
7987 if (SYMBOL_VALUE (sym) < 0)
7988 {
7989 unsigned_enum = 0;
7990 flag_enum = 0;
7991 }
7992 else if ((mask & SYMBOL_VALUE (sym)) != 0)
7993 flag_enum = 0;
7994 else
7995 mask |= SYMBOL_VALUE (sym);
7996
7997 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7998 {
7999 fields = (struct field *)
8000 xrealloc (fields,
8001 (num_fields + DW_FIELD_ALLOC_CHUNK)
8002 * sizeof (struct field));
8003 }
8004
8005 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8006 FIELD_TYPE (fields[num_fields]) = NULL;
8007 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8008 FIELD_BITSIZE (fields[num_fields]) = 0;
8009
8010 num_fields++;
8011 }
8012 }
8013
8014 child_die = sibling_die (child_die);
8015 }
8016
8017 if (num_fields)
8018 {
8019 TYPE_NFIELDS (this_type) = num_fields;
8020 TYPE_FIELDS (this_type) = (struct field *)
8021 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8022 memcpy (TYPE_FIELDS (this_type), fields,
8023 sizeof (struct field) * num_fields);
8024 xfree (fields);
8025 }
8026 if (unsigned_enum)
8027 TYPE_UNSIGNED (this_type) = 1;
8028 if (flag_enum)
8029 TYPE_FLAG_ENUM (this_type) = 1;
8030 }
8031
8032 /* If we are reading an enum from a .debug_types unit, and the enum
8033 is a declaration, and the enum is not the signatured type in the
8034 unit, then we do not want to add a symbol for it. Adding a
8035 symbol would in some cases obscure the true definition of the
8036 enum, giving users an incomplete type when the definition is
8037 actually available. Note that we do not want to do this for all
8038 enums which are just declarations, because C++0x allows forward
8039 enum declarations. */
8040 if (cu->per_cu->debug_types_section
8041 && die_is_declaration (die, cu))
8042 {
8043 struct signatured_type *type_sig;
8044
8045 type_sig
8046 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8047 cu->per_cu->debug_types_section,
8048 cu->per_cu->offset);
8049 if (type_sig->per_cu.offset + type_sig->type_offset
8050 != die->offset)
8051 return;
8052 }
8053
8054 new_symbol (die, this_type, cu);
8055 }
8056
8057 /* Extract all information from a DW_TAG_array_type DIE and put it in
8058 the DIE's type field. For now, this only handles one dimensional
8059 arrays. */
8060
8061 static struct type *
8062 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8063 {
8064 struct objfile *objfile = cu->objfile;
8065 struct die_info *child_die;
8066 struct type *type;
8067 struct type *element_type, *range_type, *index_type;
8068 struct type **range_types = NULL;
8069 struct attribute *attr;
8070 int ndim = 0;
8071 struct cleanup *back_to;
8072 char *name;
8073
8074 element_type = die_type (die, cu);
8075
8076 /* The die_type call above may have already set the type for this DIE. */
8077 type = get_die_type (die, cu);
8078 if (type)
8079 return type;
8080
8081 /* Irix 6.2 native cc creates array types without children for
8082 arrays with unspecified length. */
8083 if (die->child == NULL)
8084 {
8085 index_type = objfile_type (objfile)->builtin_int;
8086 range_type = create_range_type (NULL, index_type, 0, -1);
8087 type = create_array_type (NULL, element_type, range_type);
8088 return set_die_type (die, type, cu);
8089 }
8090
8091 back_to = make_cleanup (null_cleanup, NULL);
8092 child_die = die->child;
8093 while (child_die && child_die->tag)
8094 {
8095 if (child_die->tag == DW_TAG_subrange_type)
8096 {
8097 struct type *child_type = read_type_die (child_die, cu);
8098
8099 if (child_type != NULL)
8100 {
8101 /* The range type was succesfully read. Save it for the
8102 array type creation. */
8103 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8104 {
8105 range_types = (struct type **)
8106 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8107 * sizeof (struct type *));
8108 if (ndim == 0)
8109 make_cleanup (free_current_contents, &range_types);
8110 }
8111 range_types[ndim++] = child_type;
8112 }
8113 }
8114 child_die = sibling_die (child_die);
8115 }
8116
8117 /* Dwarf2 dimensions are output from left to right, create the
8118 necessary array types in backwards order. */
8119
8120 type = element_type;
8121
8122 if (read_array_order (die, cu) == DW_ORD_col_major)
8123 {
8124 int i = 0;
8125
8126 while (i < ndim)
8127 type = create_array_type (NULL, type, range_types[i++]);
8128 }
8129 else
8130 {
8131 while (ndim-- > 0)
8132 type = create_array_type (NULL, type, range_types[ndim]);
8133 }
8134
8135 /* Understand Dwarf2 support for vector types (like they occur on
8136 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8137 array type. This is not part of the Dwarf2/3 standard yet, but a
8138 custom vendor extension. The main difference between a regular
8139 array and the vector variant is that vectors are passed by value
8140 to functions. */
8141 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8142 if (attr)
8143 make_vector_type (type);
8144
8145 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8146 implementation may choose to implement triple vectors using this
8147 attribute. */
8148 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8149 if (attr)
8150 {
8151 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8152 TYPE_LENGTH (type) = DW_UNSND (attr);
8153 else
8154 complaint (&symfile_complaints,
8155 _("DW_AT_byte_size for array type smaller "
8156 "than the total size of elements"));
8157 }
8158
8159 name = dwarf2_name (die, cu);
8160 if (name)
8161 TYPE_NAME (type) = name;
8162
8163 /* Install the type in the die. */
8164 set_die_type (die, type, cu);
8165
8166 /* set_die_type should be already done. */
8167 set_descriptive_type (type, die, cu);
8168
8169 do_cleanups (back_to);
8170
8171 return type;
8172 }
8173
8174 static enum dwarf_array_dim_ordering
8175 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8176 {
8177 struct attribute *attr;
8178
8179 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8180
8181 if (attr) return DW_SND (attr);
8182
8183 /* GNU F77 is a special case, as at 08/2004 array type info is the
8184 opposite order to the dwarf2 specification, but data is still
8185 laid out as per normal fortran.
8186
8187 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8188 version checking. */
8189
8190 if (cu->language == language_fortran
8191 && cu->producer && strstr (cu->producer, "GNU F77"))
8192 {
8193 return DW_ORD_row_major;
8194 }
8195
8196 switch (cu->language_defn->la_array_ordering)
8197 {
8198 case array_column_major:
8199 return DW_ORD_col_major;
8200 case array_row_major:
8201 default:
8202 return DW_ORD_row_major;
8203 };
8204 }
8205
8206 /* Extract all information from a DW_TAG_set_type DIE and put it in
8207 the DIE's type field. */
8208
8209 static struct type *
8210 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8211 {
8212 struct type *domain_type, *set_type;
8213 struct attribute *attr;
8214
8215 domain_type = die_type (die, cu);
8216
8217 /* The die_type call above may have already set the type for this DIE. */
8218 set_type = get_die_type (die, cu);
8219 if (set_type)
8220 return set_type;
8221
8222 set_type = create_set_type (NULL, domain_type);
8223
8224 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8225 if (attr)
8226 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8227
8228 return set_die_type (die, set_type, cu);
8229 }
8230
8231 /* First cut: install each common block member as a global variable. */
8232
8233 static void
8234 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8235 {
8236 struct die_info *child_die;
8237 struct attribute *attr;
8238 struct symbol *sym;
8239 CORE_ADDR base = (CORE_ADDR) 0;
8240
8241 attr = dwarf2_attr (die, DW_AT_location, cu);
8242 if (attr)
8243 {
8244 /* Support the .debug_loc offsets. */
8245 if (attr_form_is_block (attr))
8246 {
8247 base = decode_locdesc (DW_BLOCK (attr), cu);
8248 }
8249 else if (attr_form_is_section_offset (attr))
8250 {
8251 dwarf2_complex_location_expr_complaint ();
8252 }
8253 else
8254 {
8255 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8256 "common block member");
8257 }
8258 }
8259 if (die->child != NULL)
8260 {
8261 child_die = die->child;
8262 while (child_die && child_die->tag)
8263 {
8264 LONGEST offset;
8265
8266 sym = new_symbol (child_die, NULL, cu);
8267 if (sym != NULL
8268 && handle_data_member_location (child_die, cu, &offset))
8269 {
8270 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8271 add_symbol_to_list (sym, &global_symbols);
8272 }
8273 child_die = sibling_die (child_die);
8274 }
8275 }
8276 }
8277
8278 /* Create a type for a C++ namespace. */
8279
8280 static struct type *
8281 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8282 {
8283 struct objfile *objfile = cu->objfile;
8284 const char *previous_prefix, *name;
8285 int is_anonymous;
8286 struct type *type;
8287
8288 /* For extensions, reuse the type of the original namespace. */
8289 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8290 {
8291 struct die_info *ext_die;
8292 struct dwarf2_cu *ext_cu = cu;
8293
8294 ext_die = dwarf2_extension (die, &ext_cu);
8295 type = read_type_die (ext_die, ext_cu);
8296
8297 /* EXT_CU may not be the same as CU.
8298 Ensure TYPE is recorded in CU's type_hash table. */
8299 return set_die_type (die, type, cu);
8300 }
8301
8302 name = namespace_name (die, &is_anonymous, cu);
8303
8304 /* Now build the name of the current namespace. */
8305
8306 previous_prefix = determine_prefix (die, cu);
8307 if (previous_prefix[0] != '\0')
8308 name = typename_concat (&objfile->objfile_obstack,
8309 previous_prefix, name, 0, cu);
8310
8311 /* Create the type. */
8312 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8313 objfile);
8314 TYPE_NAME (type) = (char *) name;
8315 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8316
8317 return set_die_type (die, type, cu);
8318 }
8319
8320 /* Read a C++ namespace. */
8321
8322 static void
8323 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8324 {
8325 struct objfile *objfile = cu->objfile;
8326 int is_anonymous;
8327
8328 /* Add a symbol associated to this if we haven't seen the namespace
8329 before. Also, add a using directive if it's an anonymous
8330 namespace. */
8331
8332 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8333 {
8334 struct type *type;
8335
8336 type = read_type_die (die, cu);
8337 new_symbol (die, type, cu);
8338
8339 namespace_name (die, &is_anonymous, cu);
8340 if (is_anonymous)
8341 {
8342 const char *previous_prefix = determine_prefix (die, cu);
8343
8344 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8345 NULL, NULL, &objfile->objfile_obstack);
8346 }
8347 }
8348
8349 if (die->child != NULL)
8350 {
8351 struct die_info *child_die = die->child;
8352
8353 while (child_die && child_die->tag)
8354 {
8355 process_die (child_die, cu);
8356 child_die = sibling_die (child_die);
8357 }
8358 }
8359 }
8360
8361 /* Read a Fortran module as type. This DIE can be only a declaration used for
8362 imported module. Still we need that type as local Fortran "use ... only"
8363 declaration imports depend on the created type in determine_prefix. */
8364
8365 static struct type *
8366 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8367 {
8368 struct objfile *objfile = cu->objfile;
8369 char *module_name;
8370 struct type *type;
8371
8372 module_name = dwarf2_name (die, cu);
8373 if (!module_name)
8374 complaint (&symfile_complaints,
8375 _("DW_TAG_module has no name, offset 0x%x"),
8376 die->offset);
8377 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8378
8379 /* determine_prefix uses TYPE_TAG_NAME. */
8380 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8381
8382 return set_die_type (die, type, cu);
8383 }
8384
8385 /* Read a Fortran module. */
8386
8387 static void
8388 read_module (struct die_info *die, struct dwarf2_cu *cu)
8389 {
8390 struct die_info *child_die = die->child;
8391
8392 while (child_die && child_die->tag)
8393 {
8394 process_die (child_die, cu);
8395 child_die = sibling_die (child_die);
8396 }
8397 }
8398
8399 /* Return the name of the namespace represented by DIE. Set
8400 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8401 namespace. */
8402
8403 static const char *
8404 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8405 {
8406 struct die_info *current_die;
8407 const char *name = NULL;
8408
8409 /* Loop through the extensions until we find a name. */
8410
8411 for (current_die = die;
8412 current_die != NULL;
8413 current_die = dwarf2_extension (die, &cu))
8414 {
8415 name = dwarf2_name (current_die, cu);
8416 if (name != NULL)
8417 break;
8418 }
8419
8420 /* Is it an anonymous namespace? */
8421
8422 *is_anonymous = (name == NULL);
8423 if (*is_anonymous)
8424 name = CP_ANONYMOUS_NAMESPACE_STR;
8425
8426 return name;
8427 }
8428
8429 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8430 the user defined type vector. */
8431
8432 static struct type *
8433 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8434 {
8435 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8436 struct comp_unit_head *cu_header = &cu->header;
8437 struct type *type;
8438 struct attribute *attr_byte_size;
8439 struct attribute *attr_address_class;
8440 int byte_size, addr_class;
8441 struct type *target_type;
8442
8443 target_type = die_type (die, cu);
8444
8445 /* The die_type call above may have already set the type for this DIE. */
8446 type = get_die_type (die, cu);
8447 if (type)
8448 return type;
8449
8450 type = lookup_pointer_type (target_type);
8451
8452 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8453 if (attr_byte_size)
8454 byte_size = DW_UNSND (attr_byte_size);
8455 else
8456 byte_size = cu_header->addr_size;
8457
8458 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8459 if (attr_address_class)
8460 addr_class = DW_UNSND (attr_address_class);
8461 else
8462 addr_class = DW_ADDR_none;
8463
8464 /* If the pointer size or address class is different than the
8465 default, create a type variant marked as such and set the
8466 length accordingly. */
8467 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8468 {
8469 if (gdbarch_address_class_type_flags_p (gdbarch))
8470 {
8471 int type_flags;
8472
8473 type_flags = gdbarch_address_class_type_flags
8474 (gdbarch, byte_size, addr_class);
8475 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8476 == 0);
8477 type = make_type_with_address_space (type, type_flags);
8478 }
8479 else if (TYPE_LENGTH (type) != byte_size)
8480 {
8481 complaint (&symfile_complaints,
8482 _("invalid pointer size %d"), byte_size);
8483 }
8484 else
8485 {
8486 /* Should we also complain about unhandled address classes? */
8487 }
8488 }
8489
8490 TYPE_LENGTH (type) = byte_size;
8491 return set_die_type (die, type, cu);
8492 }
8493
8494 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8495 the user defined type vector. */
8496
8497 static struct type *
8498 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8499 {
8500 struct type *type;
8501 struct type *to_type;
8502 struct type *domain;
8503
8504 to_type = die_type (die, cu);
8505 domain = die_containing_type (die, cu);
8506
8507 /* The calls above may have already set the type for this DIE. */
8508 type = get_die_type (die, cu);
8509 if (type)
8510 return type;
8511
8512 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8513 type = lookup_methodptr_type (to_type);
8514 else
8515 type = lookup_memberptr_type (to_type, domain);
8516
8517 return set_die_type (die, type, cu);
8518 }
8519
8520 /* Extract all information from a DW_TAG_reference_type DIE and add to
8521 the user defined type vector. */
8522
8523 static struct type *
8524 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8525 {
8526 struct comp_unit_head *cu_header = &cu->header;
8527 struct type *type, *target_type;
8528 struct attribute *attr;
8529
8530 target_type = die_type (die, cu);
8531
8532 /* The die_type call above may have already set the type for this DIE. */
8533 type = get_die_type (die, cu);
8534 if (type)
8535 return type;
8536
8537 type = lookup_reference_type (target_type);
8538 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8539 if (attr)
8540 {
8541 TYPE_LENGTH (type) = DW_UNSND (attr);
8542 }
8543 else
8544 {
8545 TYPE_LENGTH (type) = cu_header->addr_size;
8546 }
8547 return set_die_type (die, type, cu);
8548 }
8549
8550 static struct type *
8551 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8552 {
8553 struct type *base_type, *cv_type;
8554
8555 base_type = die_type (die, cu);
8556
8557 /* The die_type call above may have already set the type for this DIE. */
8558 cv_type = get_die_type (die, cu);
8559 if (cv_type)
8560 return cv_type;
8561
8562 /* In case the const qualifier is applied to an array type, the element type
8563 is so qualified, not the array type (section 6.7.3 of C99). */
8564 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8565 {
8566 struct type *el_type, *inner_array;
8567
8568 base_type = copy_type (base_type);
8569 inner_array = base_type;
8570
8571 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8572 {
8573 TYPE_TARGET_TYPE (inner_array) =
8574 copy_type (TYPE_TARGET_TYPE (inner_array));
8575 inner_array = TYPE_TARGET_TYPE (inner_array);
8576 }
8577
8578 el_type = TYPE_TARGET_TYPE (inner_array);
8579 TYPE_TARGET_TYPE (inner_array) =
8580 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8581
8582 return set_die_type (die, base_type, cu);
8583 }
8584
8585 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8586 return set_die_type (die, cv_type, cu);
8587 }
8588
8589 static struct type *
8590 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8591 {
8592 struct type *base_type, *cv_type;
8593
8594 base_type = die_type (die, cu);
8595
8596 /* The die_type call above may have already set the type for this DIE. */
8597 cv_type = get_die_type (die, cu);
8598 if (cv_type)
8599 return cv_type;
8600
8601 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8602 return set_die_type (die, cv_type, cu);
8603 }
8604
8605 /* Extract all information from a DW_TAG_string_type DIE and add to
8606 the user defined type vector. It isn't really a user defined type,
8607 but it behaves like one, with other DIE's using an AT_user_def_type
8608 attribute to reference it. */
8609
8610 static struct type *
8611 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8612 {
8613 struct objfile *objfile = cu->objfile;
8614 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8615 struct type *type, *range_type, *index_type, *char_type;
8616 struct attribute *attr;
8617 unsigned int length;
8618
8619 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8620 if (attr)
8621 {
8622 length = DW_UNSND (attr);
8623 }
8624 else
8625 {
8626 /* Check for the DW_AT_byte_size attribute. */
8627 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8628 if (attr)
8629 {
8630 length = DW_UNSND (attr);
8631 }
8632 else
8633 {
8634 length = 1;
8635 }
8636 }
8637
8638 index_type = objfile_type (objfile)->builtin_int;
8639 range_type = create_range_type (NULL, index_type, 1, length);
8640 char_type = language_string_char_type (cu->language_defn, gdbarch);
8641 type = create_string_type (NULL, char_type, range_type);
8642
8643 return set_die_type (die, type, cu);
8644 }
8645
8646 /* Handle DIES due to C code like:
8647
8648 struct foo
8649 {
8650 int (*funcp)(int a, long l);
8651 int b;
8652 };
8653
8654 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8655
8656 static struct type *
8657 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8658 {
8659 struct objfile *objfile = cu->objfile;
8660 struct type *type; /* Type that this function returns. */
8661 struct type *ftype; /* Function that returns above type. */
8662 struct attribute *attr;
8663
8664 type = die_type (die, cu);
8665
8666 /* The die_type call above may have already set the type for this DIE. */
8667 ftype = get_die_type (die, cu);
8668 if (ftype)
8669 return ftype;
8670
8671 ftype = lookup_function_type (type);
8672
8673 /* All functions in C++, Pascal and Java have prototypes. */
8674 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8675 if ((attr && (DW_UNSND (attr) != 0))
8676 || cu->language == language_cplus
8677 || cu->language == language_java
8678 || cu->language == language_pascal)
8679 TYPE_PROTOTYPED (ftype) = 1;
8680 else if (producer_is_realview (cu->producer))
8681 /* RealView does not emit DW_AT_prototyped. We can not
8682 distinguish prototyped and unprototyped functions; default to
8683 prototyped, since that is more common in modern code (and
8684 RealView warns about unprototyped functions). */
8685 TYPE_PROTOTYPED (ftype) = 1;
8686
8687 /* Store the calling convention in the type if it's available in
8688 the subroutine die. Otherwise set the calling convention to
8689 the default value DW_CC_normal. */
8690 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8691 if (attr)
8692 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8693 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8694 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8695 else
8696 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8697
8698 /* We need to add the subroutine type to the die immediately so
8699 we don't infinitely recurse when dealing with parameters
8700 declared as the same subroutine type. */
8701 set_die_type (die, ftype, cu);
8702
8703 if (die->child != NULL)
8704 {
8705 struct type *void_type = objfile_type (objfile)->builtin_void;
8706 struct die_info *child_die;
8707 int nparams, iparams;
8708
8709 /* Count the number of parameters.
8710 FIXME: GDB currently ignores vararg functions, but knows about
8711 vararg member functions. */
8712 nparams = 0;
8713 child_die = die->child;
8714 while (child_die && child_die->tag)
8715 {
8716 if (child_die->tag == DW_TAG_formal_parameter)
8717 nparams++;
8718 else if (child_die->tag == DW_TAG_unspecified_parameters)
8719 TYPE_VARARGS (ftype) = 1;
8720 child_die = sibling_die (child_die);
8721 }
8722
8723 /* Allocate storage for parameters and fill them in. */
8724 TYPE_NFIELDS (ftype) = nparams;
8725 TYPE_FIELDS (ftype) = (struct field *)
8726 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8727
8728 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8729 even if we error out during the parameters reading below. */
8730 for (iparams = 0; iparams < nparams; iparams++)
8731 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8732
8733 iparams = 0;
8734 child_die = die->child;
8735 while (child_die && child_die->tag)
8736 {
8737 if (child_die->tag == DW_TAG_formal_parameter)
8738 {
8739 struct type *arg_type;
8740
8741 /* DWARF version 2 has no clean way to discern C++
8742 static and non-static member functions. G++ helps
8743 GDB by marking the first parameter for non-static
8744 member functions (which is the this pointer) as
8745 artificial. We pass this information to
8746 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8747
8748 DWARF version 3 added DW_AT_object_pointer, which GCC
8749 4.5 does not yet generate. */
8750 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8751 if (attr)
8752 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8753 else
8754 {
8755 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8756
8757 /* GCC/43521: In java, the formal parameter
8758 "this" is sometimes not marked with DW_AT_artificial. */
8759 if (cu->language == language_java)
8760 {
8761 const char *name = dwarf2_name (child_die, cu);
8762
8763 if (name && !strcmp (name, "this"))
8764 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8765 }
8766 }
8767 arg_type = die_type (child_die, cu);
8768
8769 /* RealView does not mark THIS as const, which the testsuite
8770 expects. GCC marks THIS as const in method definitions,
8771 but not in the class specifications (GCC PR 43053). */
8772 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8773 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8774 {
8775 int is_this = 0;
8776 struct dwarf2_cu *arg_cu = cu;
8777 const char *name = dwarf2_name (child_die, cu);
8778
8779 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8780 if (attr)
8781 {
8782 /* If the compiler emits this, use it. */
8783 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8784 is_this = 1;
8785 }
8786 else if (name && strcmp (name, "this") == 0)
8787 /* Function definitions will have the argument names. */
8788 is_this = 1;
8789 else if (name == NULL && iparams == 0)
8790 /* Declarations may not have the names, so like
8791 elsewhere in GDB, assume an artificial first
8792 argument is "this". */
8793 is_this = 1;
8794
8795 if (is_this)
8796 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8797 arg_type, 0);
8798 }
8799
8800 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8801 iparams++;
8802 }
8803 child_die = sibling_die (child_die);
8804 }
8805 }
8806
8807 return ftype;
8808 }
8809
8810 static struct type *
8811 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8812 {
8813 struct objfile *objfile = cu->objfile;
8814 const char *name = NULL;
8815 struct type *this_type, *target_type;
8816
8817 name = dwarf2_full_name (NULL, die, cu);
8818 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8819 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8820 TYPE_NAME (this_type) = (char *) name;
8821 set_die_type (die, this_type, cu);
8822 target_type = die_type (die, cu);
8823 if (target_type != this_type)
8824 TYPE_TARGET_TYPE (this_type) = target_type;
8825 else
8826 {
8827 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8828 spec and cause infinite loops in GDB. */
8829 complaint (&symfile_complaints,
8830 _("Self-referential DW_TAG_typedef "
8831 "- DIE at 0x%x [in module %s]"),
8832 die->offset, objfile->name);
8833 TYPE_TARGET_TYPE (this_type) = NULL;
8834 }
8835 return this_type;
8836 }
8837
8838 /* Find a representation of a given base type and install
8839 it in the TYPE field of the die. */
8840
8841 static struct type *
8842 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8843 {
8844 struct objfile *objfile = cu->objfile;
8845 struct type *type;
8846 struct attribute *attr;
8847 int encoding = 0, size = 0;
8848 char *name;
8849 enum type_code code = TYPE_CODE_INT;
8850 int type_flags = 0;
8851 struct type *target_type = NULL;
8852
8853 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8854 if (attr)
8855 {
8856 encoding = DW_UNSND (attr);
8857 }
8858 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8859 if (attr)
8860 {
8861 size = DW_UNSND (attr);
8862 }
8863 name = dwarf2_name (die, cu);
8864 if (!name)
8865 {
8866 complaint (&symfile_complaints,
8867 _("DW_AT_name missing from DW_TAG_base_type"));
8868 }
8869
8870 switch (encoding)
8871 {
8872 case DW_ATE_address:
8873 /* Turn DW_ATE_address into a void * pointer. */
8874 code = TYPE_CODE_PTR;
8875 type_flags |= TYPE_FLAG_UNSIGNED;
8876 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8877 break;
8878 case DW_ATE_boolean:
8879 code = TYPE_CODE_BOOL;
8880 type_flags |= TYPE_FLAG_UNSIGNED;
8881 break;
8882 case DW_ATE_complex_float:
8883 code = TYPE_CODE_COMPLEX;
8884 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8885 break;
8886 case DW_ATE_decimal_float:
8887 code = TYPE_CODE_DECFLOAT;
8888 break;
8889 case DW_ATE_float:
8890 code = TYPE_CODE_FLT;
8891 break;
8892 case DW_ATE_signed:
8893 break;
8894 case DW_ATE_unsigned:
8895 type_flags |= TYPE_FLAG_UNSIGNED;
8896 if (cu->language == language_fortran
8897 && name
8898 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8899 code = TYPE_CODE_CHAR;
8900 break;
8901 case DW_ATE_signed_char:
8902 if (cu->language == language_ada || cu->language == language_m2
8903 || cu->language == language_pascal
8904 || cu->language == language_fortran)
8905 code = TYPE_CODE_CHAR;
8906 break;
8907 case DW_ATE_unsigned_char:
8908 if (cu->language == language_ada || cu->language == language_m2
8909 || cu->language == language_pascal
8910 || cu->language == language_fortran)
8911 code = TYPE_CODE_CHAR;
8912 type_flags |= TYPE_FLAG_UNSIGNED;
8913 break;
8914 case DW_ATE_UTF:
8915 /* We just treat this as an integer and then recognize the
8916 type by name elsewhere. */
8917 break;
8918
8919 default:
8920 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8921 dwarf_type_encoding_name (encoding));
8922 break;
8923 }
8924
8925 type = init_type (code, size, type_flags, NULL, objfile);
8926 TYPE_NAME (type) = name;
8927 TYPE_TARGET_TYPE (type) = target_type;
8928
8929 if (name && strcmp (name, "char") == 0)
8930 TYPE_NOSIGN (type) = 1;
8931
8932 return set_die_type (die, type, cu);
8933 }
8934
8935 /* Read the given DW_AT_subrange DIE. */
8936
8937 static struct type *
8938 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8939 {
8940 struct type *base_type;
8941 struct type *range_type;
8942 struct attribute *attr;
8943 LONGEST low = 0;
8944 LONGEST high = -1;
8945 char *name;
8946 LONGEST negative_mask;
8947
8948 base_type = die_type (die, cu);
8949 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8950 check_typedef (base_type);
8951
8952 /* The die_type call above may have already set the type for this DIE. */
8953 range_type = get_die_type (die, cu);
8954 if (range_type)
8955 return range_type;
8956
8957 if (cu->language == language_fortran)
8958 {
8959 /* FORTRAN implies a lower bound of 1, if not given. */
8960 low = 1;
8961 }
8962
8963 /* FIXME: For variable sized arrays either of these could be
8964 a variable rather than a constant value. We'll allow it,
8965 but we don't know how to handle it. */
8966 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8967 if (attr)
8968 low = dwarf2_get_attr_constant_value (attr, 0);
8969
8970 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8971 if (attr)
8972 {
8973 if (attr_form_is_block (attr) || is_ref_attr (attr))
8974 {
8975 /* GCC encodes arrays with unspecified or dynamic length
8976 with a DW_FORM_block1 attribute or a reference attribute.
8977 FIXME: GDB does not yet know how to handle dynamic
8978 arrays properly, treat them as arrays with unspecified
8979 length for now.
8980
8981 FIXME: jimb/2003-09-22: GDB does not really know
8982 how to handle arrays of unspecified length
8983 either; we just represent them as zero-length
8984 arrays. Choose an appropriate upper bound given
8985 the lower bound we've computed above. */
8986 high = low - 1;
8987 }
8988 else
8989 high = dwarf2_get_attr_constant_value (attr, 1);
8990 }
8991 else
8992 {
8993 attr = dwarf2_attr (die, DW_AT_count, cu);
8994 if (attr)
8995 {
8996 int count = dwarf2_get_attr_constant_value (attr, 1);
8997 high = low + count - 1;
8998 }
8999 else
9000 {
9001 /* Unspecified array length. */
9002 high = low - 1;
9003 }
9004 }
9005
9006 /* Dwarf-2 specifications explicitly allows to create subrange types
9007 without specifying a base type.
9008 In that case, the base type must be set to the type of
9009 the lower bound, upper bound or count, in that order, if any of these
9010 three attributes references an object that has a type.
9011 If no base type is found, the Dwarf-2 specifications say that
9012 a signed integer type of size equal to the size of an address should
9013 be used.
9014 For the following C code: `extern char gdb_int [];'
9015 GCC produces an empty range DIE.
9016 FIXME: muller/2010-05-28: Possible references to object for low bound,
9017 high bound or count are not yet handled by this code. */
9018 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9019 {
9020 struct objfile *objfile = cu->objfile;
9021 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9022 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9023 struct type *int_type = objfile_type (objfile)->builtin_int;
9024
9025 /* Test "int", "long int", and "long long int" objfile types,
9026 and select the first one having a size above or equal to the
9027 architecture address size. */
9028 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9029 base_type = int_type;
9030 else
9031 {
9032 int_type = objfile_type (objfile)->builtin_long;
9033 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9034 base_type = int_type;
9035 else
9036 {
9037 int_type = objfile_type (objfile)->builtin_long_long;
9038 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9039 base_type = int_type;
9040 }
9041 }
9042 }
9043
9044 negative_mask =
9045 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9046 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9047 low |= negative_mask;
9048 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9049 high |= negative_mask;
9050
9051 range_type = create_range_type (NULL, base_type, low, high);
9052
9053 /* Mark arrays with dynamic length at least as an array of unspecified
9054 length. GDB could check the boundary but before it gets implemented at
9055 least allow accessing the array elements. */
9056 if (attr && attr_form_is_block (attr))
9057 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9058
9059 /* Ada expects an empty array on no boundary attributes. */
9060 if (attr == NULL && cu->language != language_ada)
9061 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9062
9063 name = dwarf2_name (die, cu);
9064 if (name)
9065 TYPE_NAME (range_type) = name;
9066
9067 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9068 if (attr)
9069 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9070
9071 set_die_type (die, range_type, cu);
9072
9073 /* set_die_type should be already done. */
9074 set_descriptive_type (range_type, die, cu);
9075
9076 return range_type;
9077 }
9078
9079 static struct type *
9080 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9081 {
9082 struct type *type;
9083
9084 /* For now, we only support the C meaning of an unspecified type: void. */
9085
9086 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9087 TYPE_NAME (type) = dwarf2_name (die, cu);
9088
9089 return set_die_type (die, type, cu);
9090 }
9091
9092 /* Trivial hash function for die_info: the hash value of a DIE
9093 is its offset in .debug_info for this objfile. */
9094
9095 static hashval_t
9096 die_hash (const void *item)
9097 {
9098 const struct die_info *die = item;
9099
9100 return die->offset;
9101 }
9102
9103 /* Trivial comparison function for die_info structures: two DIEs
9104 are equal if they have the same offset. */
9105
9106 static int
9107 die_eq (const void *item_lhs, const void *item_rhs)
9108 {
9109 const struct die_info *die_lhs = item_lhs;
9110 const struct die_info *die_rhs = item_rhs;
9111
9112 return die_lhs->offset == die_rhs->offset;
9113 }
9114
9115 /* Read a whole compilation unit into a linked list of dies. */
9116
9117 static struct die_info *
9118 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9119 {
9120 struct die_reader_specs reader_specs;
9121 int read_abbrevs = 0;
9122 struct cleanup *back_to = NULL;
9123 struct die_info *die;
9124
9125 if (cu->dwarf2_abbrevs == NULL)
9126 {
9127 dwarf2_read_abbrevs (cu);
9128 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9129 read_abbrevs = 1;
9130 }
9131
9132 gdb_assert (cu->die_hash == NULL);
9133 cu->die_hash
9134 = htab_create_alloc_ex (cu->header.length / 12,
9135 die_hash,
9136 die_eq,
9137 NULL,
9138 &cu->comp_unit_obstack,
9139 hashtab_obstack_allocate,
9140 dummy_obstack_deallocate);
9141
9142 init_cu_die_reader (&reader_specs, cu);
9143
9144 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9145
9146 if (read_abbrevs)
9147 do_cleanups (back_to);
9148
9149 return die;
9150 }
9151
9152 /* Main entry point for reading a DIE and all children.
9153 Read the DIE and dump it if requested. */
9154
9155 static struct die_info *
9156 read_die_and_children (const struct die_reader_specs *reader,
9157 gdb_byte *info_ptr,
9158 gdb_byte **new_info_ptr,
9159 struct die_info *parent)
9160 {
9161 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9162 new_info_ptr, parent);
9163
9164 if (dwarf2_die_debug)
9165 {
9166 fprintf_unfiltered (gdb_stdlog,
9167 "\nRead die from %s of %s:\n",
9168 (reader->cu->per_cu->debug_types_section
9169 ? ".debug_types"
9170 : ".debug_info"),
9171 reader->abfd->filename);
9172 dump_die (result, dwarf2_die_debug);
9173 }
9174
9175 return result;
9176 }
9177
9178 /* Read a single die and all its descendents. Set the die's sibling
9179 field to NULL; set other fields in the die correctly, and set all
9180 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9181 location of the info_ptr after reading all of those dies. PARENT
9182 is the parent of the die in question. */
9183
9184 static struct die_info *
9185 read_die_and_children_1 (const struct die_reader_specs *reader,
9186 gdb_byte *info_ptr,
9187 gdb_byte **new_info_ptr,
9188 struct die_info *parent)
9189 {
9190 struct die_info *die;
9191 gdb_byte *cur_ptr;
9192 int has_children;
9193
9194 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9195 if (die == NULL)
9196 {
9197 *new_info_ptr = cur_ptr;
9198 return NULL;
9199 }
9200 store_in_ref_table (die, reader->cu);
9201
9202 if (has_children)
9203 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9204 else
9205 {
9206 die->child = NULL;
9207 *new_info_ptr = cur_ptr;
9208 }
9209
9210 die->sibling = NULL;
9211 die->parent = parent;
9212 return die;
9213 }
9214
9215 /* Read a die, all of its descendents, and all of its siblings; set
9216 all of the fields of all of the dies correctly. Arguments are as
9217 in read_die_and_children. */
9218
9219 static struct die_info *
9220 read_die_and_siblings (const struct die_reader_specs *reader,
9221 gdb_byte *info_ptr,
9222 gdb_byte **new_info_ptr,
9223 struct die_info *parent)
9224 {
9225 struct die_info *first_die, *last_sibling;
9226 gdb_byte *cur_ptr;
9227
9228 cur_ptr = info_ptr;
9229 first_die = last_sibling = NULL;
9230
9231 while (1)
9232 {
9233 struct die_info *die
9234 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9235
9236 if (die == NULL)
9237 {
9238 *new_info_ptr = cur_ptr;
9239 return first_die;
9240 }
9241
9242 if (!first_die)
9243 first_die = die;
9244 else
9245 last_sibling->sibling = die;
9246
9247 last_sibling = die;
9248 }
9249 }
9250
9251 /* Read the die from the .debug_info section buffer. Set DIEP to
9252 point to a newly allocated die with its information, except for its
9253 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9254 whether the die has children or not. */
9255
9256 static gdb_byte *
9257 read_full_die (const struct die_reader_specs *reader,
9258 struct die_info **diep, gdb_byte *info_ptr,
9259 int *has_children)
9260 {
9261 unsigned int abbrev_number, bytes_read, i, offset;
9262 struct abbrev_info *abbrev;
9263 struct die_info *die;
9264 struct dwarf2_cu *cu = reader->cu;
9265 bfd *abfd = reader->abfd;
9266
9267 offset = info_ptr - reader->buffer;
9268 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9269 info_ptr += bytes_read;
9270 if (!abbrev_number)
9271 {
9272 *diep = NULL;
9273 *has_children = 0;
9274 return info_ptr;
9275 }
9276
9277 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9278 if (!abbrev)
9279 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9280 abbrev_number,
9281 bfd_get_filename (abfd));
9282
9283 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9284 die->offset = offset;
9285 die->tag = abbrev->tag;
9286 die->abbrev = abbrev_number;
9287
9288 die->num_attrs = abbrev->num_attrs;
9289
9290 for (i = 0; i < abbrev->num_attrs; ++i)
9291 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9292 abfd, info_ptr, cu);
9293
9294 *diep = die;
9295 *has_children = abbrev->has_children;
9296 return info_ptr;
9297 }
9298
9299 /* In DWARF version 2, the description of the debugging information is
9300 stored in a separate .debug_abbrev section. Before we read any
9301 dies from a section we read in all abbreviations and install them
9302 in a hash table. This function also sets flags in CU describing
9303 the data found in the abbrev table. */
9304
9305 static void
9306 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9307 {
9308 bfd *abfd = cu->objfile->obfd;
9309 struct comp_unit_head *cu_header = &cu->header;
9310 gdb_byte *abbrev_ptr;
9311 struct abbrev_info *cur_abbrev;
9312 unsigned int abbrev_number, bytes_read, abbrev_name;
9313 unsigned int abbrev_form, hash_number;
9314 struct attr_abbrev *cur_attrs;
9315 unsigned int allocated_attrs;
9316
9317 /* Initialize dwarf2 abbrevs. */
9318 obstack_init (&cu->abbrev_obstack);
9319 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9320 (ABBREV_HASH_SIZE
9321 * sizeof (struct abbrev_info *)));
9322 memset (cu->dwarf2_abbrevs, 0,
9323 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9324
9325 dwarf2_read_section (dwarf2_per_objfile->objfile,
9326 &dwarf2_per_objfile->abbrev);
9327 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9328 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9329 abbrev_ptr += bytes_read;
9330
9331 allocated_attrs = ATTR_ALLOC_CHUNK;
9332 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9333
9334 /* Loop until we reach an abbrev number of 0. */
9335 while (abbrev_number)
9336 {
9337 cur_abbrev = dwarf_alloc_abbrev (cu);
9338
9339 /* read in abbrev header */
9340 cur_abbrev->number = abbrev_number;
9341 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9342 abbrev_ptr += bytes_read;
9343 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9344 abbrev_ptr += 1;
9345
9346 /* now read in declarations */
9347 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9348 abbrev_ptr += bytes_read;
9349 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9350 abbrev_ptr += bytes_read;
9351 while (abbrev_name)
9352 {
9353 if (cur_abbrev->num_attrs == allocated_attrs)
9354 {
9355 allocated_attrs += ATTR_ALLOC_CHUNK;
9356 cur_attrs
9357 = xrealloc (cur_attrs, (allocated_attrs
9358 * sizeof (struct attr_abbrev)));
9359 }
9360
9361 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9362 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9363 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9364 abbrev_ptr += bytes_read;
9365 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9366 abbrev_ptr += bytes_read;
9367 }
9368
9369 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9370 (cur_abbrev->num_attrs
9371 * sizeof (struct attr_abbrev)));
9372 memcpy (cur_abbrev->attrs, cur_attrs,
9373 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9374
9375 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9376 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9377 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9378
9379 /* Get next abbreviation.
9380 Under Irix6 the abbreviations for a compilation unit are not
9381 always properly terminated with an abbrev number of 0.
9382 Exit loop if we encounter an abbreviation which we have
9383 already read (which means we are about to read the abbreviations
9384 for the next compile unit) or if the end of the abbreviation
9385 table is reached. */
9386 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9387 >= dwarf2_per_objfile->abbrev.size)
9388 break;
9389 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9390 abbrev_ptr += bytes_read;
9391 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9392 break;
9393 }
9394
9395 xfree (cur_attrs);
9396 }
9397
9398 /* Release the memory used by the abbrev table for a compilation unit. */
9399
9400 static void
9401 dwarf2_free_abbrev_table (void *ptr_to_cu)
9402 {
9403 struct dwarf2_cu *cu = ptr_to_cu;
9404
9405 obstack_free (&cu->abbrev_obstack, NULL);
9406 cu->dwarf2_abbrevs = NULL;
9407 }
9408
9409 /* Lookup an abbrev_info structure in the abbrev hash table. */
9410
9411 static struct abbrev_info *
9412 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9413 {
9414 unsigned int hash_number;
9415 struct abbrev_info *abbrev;
9416
9417 hash_number = number % ABBREV_HASH_SIZE;
9418 abbrev = cu->dwarf2_abbrevs[hash_number];
9419
9420 while (abbrev)
9421 {
9422 if (abbrev->number == number)
9423 return abbrev;
9424 else
9425 abbrev = abbrev->next;
9426 }
9427 return NULL;
9428 }
9429
9430 /* Returns nonzero if TAG represents a type that we might generate a partial
9431 symbol for. */
9432
9433 static int
9434 is_type_tag_for_partial (int tag)
9435 {
9436 switch (tag)
9437 {
9438 #if 0
9439 /* Some types that would be reasonable to generate partial symbols for,
9440 that we don't at present. */
9441 case DW_TAG_array_type:
9442 case DW_TAG_file_type:
9443 case DW_TAG_ptr_to_member_type:
9444 case DW_TAG_set_type:
9445 case DW_TAG_string_type:
9446 case DW_TAG_subroutine_type:
9447 #endif
9448 case DW_TAG_base_type:
9449 case DW_TAG_class_type:
9450 case DW_TAG_interface_type:
9451 case DW_TAG_enumeration_type:
9452 case DW_TAG_structure_type:
9453 case DW_TAG_subrange_type:
9454 case DW_TAG_typedef:
9455 case DW_TAG_union_type:
9456 return 1;
9457 default:
9458 return 0;
9459 }
9460 }
9461
9462 /* Load all DIEs that are interesting for partial symbols into memory. */
9463
9464 static struct partial_die_info *
9465 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9466 int building_psymtab, struct dwarf2_cu *cu)
9467 {
9468 struct objfile *objfile = cu->objfile;
9469 struct partial_die_info *part_die;
9470 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9471 struct abbrev_info *abbrev;
9472 unsigned int bytes_read;
9473 unsigned int load_all = 0;
9474
9475 int nesting_level = 1;
9476
9477 parent_die = NULL;
9478 last_die = NULL;
9479
9480 if (cu->per_cu && cu->per_cu->load_all_dies)
9481 load_all = 1;
9482
9483 cu->partial_dies
9484 = htab_create_alloc_ex (cu->header.length / 12,
9485 partial_die_hash,
9486 partial_die_eq,
9487 NULL,
9488 &cu->comp_unit_obstack,
9489 hashtab_obstack_allocate,
9490 dummy_obstack_deallocate);
9491
9492 part_die = obstack_alloc (&cu->comp_unit_obstack,
9493 sizeof (struct partial_die_info));
9494
9495 while (1)
9496 {
9497 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9498
9499 /* A NULL abbrev means the end of a series of children. */
9500 if (abbrev == NULL)
9501 {
9502 if (--nesting_level == 0)
9503 {
9504 /* PART_DIE was probably the last thing allocated on the
9505 comp_unit_obstack, so we could call obstack_free
9506 here. We don't do that because the waste is small,
9507 and will be cleaned up when we're done with this
9508 compilation unit. This way, we're also more robust
9509 against other users of the comp_unit_obstack. */
9510 return first_die;
9511 }
9512 info_ptr += bytes_read;
9513 last_die = parent_die;
9514 parent_die = parent_die->die_parent;
9515 continue;
9516 }
9517
9518 /* Check for template arguments. We never save these; if
9519 they're seen, we just mark the parent, and go on our way. */
9520 if (parent_die != NULL
9521 && cu->language == language_cplus
9522 && (abbrev->tag == DW_TAG_template_type_param
9523 || abbrev->tag == DW_TAG_template_value_param))
9524 {
9525 parent_die->has_template_arguments = 1;
9526
9527 if (!load_all)
9528 {
9529 /* We don't need a partial DIE for the template argument. */
9530 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9531 cu);
9532 continue;
9533 }
9534 }
9535
9536 /* We only recurse into subprograms looking for template arguments.
9537 Skip their other children. */
9538 if (!load_all
9539 && cu->language == language_cplus
9540 && parent_die != NULL
9541 && parent_die->tag == DW_TAG_subprogram)
9542 {
9543 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9544 continue;
9545 }
9546
9547 /* Check whether this DIE is interesting enough to save. Normally
9548 we would not be interested in members here, but there may be
9549 later variables referencing them via DW_AT_specification (for
9550 static members). */
9551 if (!load_all
9552 && !is_type_tag_for_partial (abbrev->tag)
9553 && abbrev->tag != DW_TAG_constant
9554 && abbrev->tag != DW_TAG_enumerator
9555 && abbrev->tag != DW_TAG_subprogram
9556 && abbrev->tag != DW_TAG_lexical_block
9557 && abbrev->tag != DW_TAG_variable
9558 && abbrev->tag != DW_TAG_namespace
9559 && abbrev->tag != DW_TAG_module
9560 && abbrev->tag != DW_TAG_member)
9561 {
9562 /* Otherwise we skip to the next sibling, if any. */
9563 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9564 continue;
9565 }
9566
9567 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9568 buffer, info_ptr, cu);
9569
9570 /* This two-pass algorithm for processing partial symbols has a
9571 high cost in cache pressure. Thus, handle some simple cases
9572 here which cover the majority of C partial symbols. DIEs
9573 which neither have specification tags in them, nor could have
9574 specification tags elsewhere pointing at them, can simply be
9575 processed and discarded.
9576
9577 This segment is also optional; scan_partial_symbols and
9578 add_partial_symbol will handle these DIEs if we chain
9579 them in normally. When compilers which do not emit large
9580 quantities of duplicate debug information are more common,
9581 this code can probably be removed. */
9582
9583 /* Any complete simple types at the top level (pretty much all
9584 of them, for a language without namespaces), can be processed
9585 directly. */
9586 if (parent_die == NULL
9587 && part_die->has_specification == 0
9588 && part_die->is_declaration == 0
9589 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9590 || part_die->tag == DW_TAG_base_type
9591 || part_die->tag == DW_TAG_subrange_type))
9592 {
9593 if (building_psymtab && part_die->name != NULL)
9594 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9595 VAR_DOMAIN, LOC_TYPEDEF,
9596 &objfile->static_psymbols,
9597 0, (CORE_ADDR) 0, cu->language, objfile);
9598 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9599 continue;
9600 }
9601
9602 /* The exception for DW_TAG_typedef with has_children above is
9603 a workaround of GCC PR debug/47510. In the case of this complaint
9604 type_name_no_tag_or_error will error on such types later.
9605
9606 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9607 it could not find the child DIEs referenced later, this is checked
9608 above. In correct DWARF DW_TAG_typedef should have no children. */
9609
9610 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9611 complaint (&symfile_complaints,
9612 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9613 "- DIE at 0x%x [in module %s]"),
9614 part_die->offset, objfile->name);
9615
9616 /* If we're at the second level, and we're an enumerator, and
9617 our parent has no specification (meaning possibly lives in a
9618 namespace elsewhere), then we can add the partial symbol now
9619 instead of queueing it. */
9620 if (part_die->tag == DW_TAG_enumerator
9621 && parent_die != NULL
9622 && parent_die->die_parent == NULL
9623 && parent_die->tag == DW_TAG_enumeration_type
9624 && parent_die->has_specification == 0)
9625 {
9626 if (part_die->name == NULL)
9627 complaint (&symfile_complaints,
9628 _("malformed enumerator DIE ignored"));
9629 else if (building_psymtab)
9630 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9631 VAR_DOMAIN, LOC_CONST,
9632 (cu->language == language_cplus
9633 || cu->language == language_java)
9634 ? &objfile->global_psymbols
9635 : &objfile->static_psymbols,
9636 0, (CORE_ADDR) 0, cu->language, objfile);
9637
9638 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9639 continue;
9640 }
9641
9642 /* We'll save this DIE so link it in. */
9643 part_die->die_parent = parent_die;
9644 part_die->die_sibling = NULL;
9645 part_die->die_child = NULL;
9646
9647 if (last_die && last_die == parent_die)
9648 last_die->die_child = part_die;
9649 else if (last_die)
9650 last_die->die_sibling = part_die;
9651
9652 last_die = part_die;
9653
9654 if (first_die == NULL)
9655 first_die = part_die;
9656
9657 /* Maybe add the DIE to the hash table. Not all DIEs that we
9658 find interesting need to be in the hash table, because we
9659 also have the parent/sibling/child chains; only those that we
9660 might refer to by offset later during partial symbol reading.
9661
9662 For now this means things that might have be the target of a
9663 DW_AT_specification, DW_AT_abstract_origin, or
9664 DW_AT_extension. DW_AT_extension will refer only to
9665 namespaces; DW_AT_abstract_origin refers to functions (and
9666 many things under the function DIE, but we do not recurse
9667 into function DIEs during partial symbol reading) and
9668 possibly variables as well; DW_AT_specification refers to
9669 declarations. Declarations ought to have the DW_AT_declaration
9670 flag. It happens that GCC forgets to put it in sometimes, but
9671 only for functions, not for types.
9672
9673 Adding more things than necessary to the hash table is harmless
9674 except for the performance cost. Adding too few will result in
9675 wasted time in find_partial_die, when we reread the compilation
9676 unit with load_all_dies set. */
9677
9678 if (load_all
9679 || abbrev->tag == DW_TAG_constant
9680 || abbrev->tag == DW_TAG_subprogram
9681 || abbrev->tag == DW_TAG_variable
9682 || abbrev->tag == DW_TAG_namespace
9683 || part_die->is_declaration)
9684 {
9685 void **slot;
9686
9687 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9688 part_die->offset, INSERT);
9689 *slot = part_die;
9690 }
9691
9692 part_die = obstack_alloc (&cu->comp_unit_obstack,
9693 sizeof (struct partial_die_info));
9694
9695 /* For some DIEs we want to follow their children (if any). For C
9696 we have no reason to follow the children of structures; for other
9697 languages we have to, so that we can get at method physnames
9698 to infer fully qualified class names, for DW_AT_specification,
9699 and for C++ template arguments. For C++, we also look one level
9700 inside functions to find template arguments (if the name of the
9701 function does not already contain the template arguments).
9702
9703 For Ada, we need to scan the children of subprograms and lexical
9704 blocks as well because Ada allows the definition of nested
9705 entities that could be interesting for the debugger, such as
9706 nested subprograms for instance. */
9707 if (last_die->has_children
9708 && (load_all
9709 || last_die->tag == DW_TAG_namespace
9710 || last_die->tag == DW_TAG_module
9711 || last_die->tag == DW_TAG_enumeration_type
9712 || (cu->language == language_cplus
9713 && last_die->tag == DW_TAG_subprogram
9714 && (last_die->name == NULL
9715 || strchr (last_die->name, '<') == NULL))
9716 || (cu->language != language_c
9717 && (last_die->tag == DW_TAG_class_type
9718 || last_die->tag == DW_TAG_interface_type
9719 || last_die->tag == DW_TAG_structure_type
9720 || last_die->tag == DW_TAG_union_type))
9721 || (cu->language == language_ada
9722 && (last_die->tag == DW_TAG_subprogram
9723 || last_die->tag == DW_TAG_lexical_block))))
9724 {
9725 nesting_level++;
9726 parent_die = last_die;
9727 continue;
9728 }
9729
9730 /* Otherwise we skip to the next sibling, if any. */
9731 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9732
9733 /* Back to the top, do it again. */
9734 }
9735 }
9736
9737 /* Read a minimal amount of information into the minimal die structure. */
9738
9739 static gdb_byte *
9740 read_partial_die (struct partial_die_info *part_die,
9741 struct abbrev_info *abbrev,
9742 unsigned int abbrev_len, bfd *abfd,
9743 gdb_byte *buffer, gdb_byte *info_ptr,
9744 struct dwarf2_cu *cu)
9745 {
9746 struct objfile *objfile = cu->objfile;
9747 unsigned int i;
9748 struct attribute attr;
9749 int has_low_pc_attr = 0;
9750 int has_high_pc_attr = 0;
9751
9752 memset (part_die, 0, sizeof (struct partial_die_info));
9753
9754 part_die->offset = info_ptr - buffer;
9755
9756 info_ptr += abbrev_len;
9757
9758 if (abbrev == NULL)
9759 return info_ptr;
9760
9761 part_die->tag = abbrev->tag;
9762 part_die->has_children = abbrev->has_children;
9763
9764 for (i = 0; i < abbrev->num_attrs; ++i)
9765 {
9766 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9767
9768 /* Store the data if it is of an attribute we want to keep in a
9769 partial symbol table. */
9770 switch (attr.name)
9771 {
9772 case DW_AT_name:
9773 switch (part_die->tag)
9774 {
9775 case DW_TAG_compile_unit:
9776 case DW_TAG_type_unit:
9777 /* Compilation units have a DW_AT_name that is a filename, not
9778 a source language identifier. */
9779 case DW_TAG_enumeration_type:
9780 case DW_TAG_enumerator:
9781 /* These tags always have simple identifiers already; no need
9782 to canonicalize them. */
9783 part_die->name = DW_STRING (&attr);
9784 break;
9785 default:
9786 part_die->name
9787 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9788 &objfile->objfile_obstack);
9789 break;
9790 }
9791 break;
9792 case DW_AT_linkage_name:
9793 case DW_AT_MIPS_linkage_name:
9794 /* Note that both forms of linkage name might appear. We
9795 assume they will be the same, and we only store the last
9796 one we see. */
9797 if (cu->language == language_ada)
9798 part_die->name = DW_STRING (&attr);
9799 part_die->linkage_name = DW_STRING (&attr);
9800 break;
9801 case DW_AT_low_pc:
9802 has_low_pc_attr = 1;
9803 part_die->lowpc = DW_ADDR (&attr);
9804 break;
9805 case DW_AT_high_pc:
9806 has_high_pc_attr = 1;
9807 part_die->highpc = DW_ADDR (&attr);
9808 break;
9809 case DW_AT_location:
9810 /* Support the .debug_loc offsets. */
9811 if (attr_form_is_block (&attr))
9812 {
9813 part_die->locdesc = DW_BLOCK (&attr);
9814 }
9815 else if (attr_form_is_section_offset (&attr))
9816 {
9817 dwarf2_complex_location_expr_complaint ();
9818 }
9819 else
9820 {
9821 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9822 "partial symbol information");
9823 }
9824 break;
9825 case DW_AT_external:
9826 part_die->is_external = DW_UNSND (&attr);
9827 break;
9828 case DW_AT_declaration:
9829 part_die->is_declaration = DW_UNSND (&attr);
9830 break;
9831 case DW_AT_type:
9832 part_die->has_type = 1;
9833 break;
9834 case DW_AT_abstract_origin:
9835 case DW_AT_specification:
9836 case DW_AT_extension:
9837 part_die->has_specification = 1;
9838 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9839 break;
9840 case DW_AT_sibling:
9841 /* Ignore absolute siblings, they might point outside of
9842 the current compile unit. */
9843 if (attr.form == DW_FORM_ref_addr)
9844 complaint (&symfile_complaints,
9845 _("ignoring absolute DW_AT_sibling"));
9846 else
9847 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9848 break;
9849 case DW_AT_byte_size:
9850 part_die->has_byte_size = 1;
9851 break;
9852 case DW_AT_calling_convention:
9853 /* DWARF doesn't provide a way to identify a program's source-level
9854 entry point. DW_AT_calling_convention attributes are only meant
9855 to describe functions' calling conventions.
9856
9857 However, because it's a necessary piece of information in
9858 Fortran, and because DW_CC_program is the only piece of debugging
9859 information whose definition refers to a 'main program' at all,
9860 several compilers have begun marking Fortran main programs with
9861 DW_CC_program --- even when those functions use the standard
9862 calling conventions.
9863
9864 So until DWARF specifies a way to provide this information and
9865 compilers pick up the new representation, we'll support this
9866 practice. */
9867 if (DW_UNSND (&attr) == DW_CC_program
9868 && cu->language == language_fortran)
9869 {
9870 set_main_name (part_die->name);
9871
9872 /* As this DIE has a static linkage the name would be difficult
9873 to look up later. */
9874 language_of_main = language_fortran;
9875 }
9876 break;
9877 default:
9878 break;
9879 }
9880 }
9881
9882 if (has_low_pc_attr && has_high_pc_attr)
9883 {
9884 /* When using the GNU linker, .gnu.linkonce. sections are used to
9885 eliminate duplicate copies of functions and vtables and such.
9886 The linker will arbitrarily choose one and discard the others.
9887 The AT_*_pc values for such functions refer to local labels in
9888 these sections. If the section from that file was discarded, the
9889 labels are not in the output, so the relocs get a value of 0.
9890 If this is a discarded function, mark the pc bounds as invalid,
9891 so that GDB will ignore it. */
9892 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9893 {
9894 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9895
9896 complaint (&symfile_complaints,
9897 _("DW_AT_low_pc %s is zero "
9898 "for DIE at 0x%x [in module %s]"),
9899 paddress (gdbarch, part_die->lowpc),
9900 part_die->offset, objfile->name);
9901 }
9902 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9903 else if (part_die->lowpc >= part_die->highpc)
9904 {
9905 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9906
9907 complaint (&symfile_complaints,
9908 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9909 "for DIE at 0x%x [in module %s]"),
9910 paddress (gdbarch, part_die->lowpc),
9911 paddress (gdbarch, part_die->highpc),
9912 part_die->offset, objfile->name);
9913 }
9914 else
9915 part_die->has_pc_info = 1;
9916 }
9917
9918 return info_ptr;
9919 }
9920
9921 /* Find a cached partial DIE at OFFSET in CU. */
9922
9923 static struct partial_die_info *
9924 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9925 {
9926 struct partial_die_info *lookup_die = NULL;
9927 struct partial_die_info part_die;
9928
9929 part_die.offset = offset;
9930 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9931
9932 return lookup_die;
9933 }
9934
9935 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9936 except in the case of .debug_types DIEs which do not reference
9937 outside their CU (they do however referencing other types via
9938 DW_FORM_ref_sig8). */
9939
9940 static struct partial_die_info *
9941 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9942 {
9943 struct objfile *objfile = cu->objfile;
9944 struct dwarf2_per_cu_data *per_cu = NULL;
9945 struct partial_die_info *pd = NULL;
9946
9947 if (cu->per_cu->debug_types_section)
9948 {
9949 pd = find_partial_die_in_comp_unit (offset, cu);
9950 if (pd != NULL)
9951 return pd;
9952 goto not_found;
9953 }
9954
9955 if (offset_in_cu_p (&cu->header, offset))
9956 {
9957 pd = find_partial_die_in_comp_unit (offset, cu);
9958 if (pd != NULL)
9959 return pd;
9960 }
9961
9962 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9963
9964 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9965 load_partial_comp_unit (per_cu);
9966
9967 per_cu->cu->last_used = 0;
9968 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9969
9970 if (pd == NULL && per_cu->load_all_dies == 0)
9971 {
9972 struct cleanup *back_to;
9973 struct partial_die_info comp_unit_die;
9974 struct abbrev_info *abbrev;
9975 unsigned int bytes_read;
9976 char *info_ptr;
9977
9978 per_cu->load_all_dies = 1;
9979
9980 /* Re-read the DIEs. */
9981 back_to = make_cleanup (null_cleanup, 0);
9982 if (per_cu->cu->dwarf2_abbrevs == NULL)
9983 {
9984 dwarf2_read_abbrevs (per_cu->cu);
9985 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9986 }
9987 info_ptr = (dwarf2_per_objfile->info.buffer
9988 + per_cu->cu->header.offset
9989 + per_cu->cu->header.first_die_offset);
9990 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9991 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9992 objfile->obfd,
9993 dwarf2_per_objfile->info.buffer, info_ptr,
9994 per_cu->cu);
9995 if (comp_unit_die.has_children)
9996 load_partial_dies (objfile->obfd,
9997 dwarf2_per_objfile->info.buffer, info_ptr,
9998 0, per_cu->cu);
9999 do_cleanups (back_to);
10000
10001 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10002 }
10003
10004 not_found:
10005
10006 if (pd == NULL)
10007 internal_error (__FILE__, __LINE__,
10008 _("could not find partial DIE 0x%x "
10009 "in cache [from module %s]\n"),
10010 offset, bfd_get_filename (objfile->obfd));
10011 return pd;
10012 }
10013
10014 /* See if we can figure out if the class lives in a namespace. We do
10015 this by looking for a member function; its demangled name will
10016 contain namespace info, if there is any. */
10017
10018 static void
10019 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10020 struct dwarf2_cu *cu)
10021 {
10022 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10023 what template types look like, because the demangler
10024 frequently doesn't give the same name as the debug info. We
10025 could fix this by only using the demangled name to get the
10026 prefix (but see comment in read_structure_type). */
10027
10028 struct partial_die_info *real_pdi;
10029 struct partial_die_info *child_pdi;
10030
10031 /* If this DIE (this DIE's specification, if any) has a parent, then
10032 we should not do this. We'll prepend the parent's fully qualified
10033 name when we create the partial symbol. */
10034
10035 real_pdi = struct_pdi;
10036 while (real_pdi->has_specification)
10037 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10038
10039 if (real_pdi->die_parent != NULL)
10040 return;
10041
10042 for (child_pdi = struct_pdi->die_child;
10043 child_pdi != NULL;
10044 child_pdi = child_pdi->die_sibling)
10045 {
10046 if (child_pdi->tag == DW_TAG_subprogram
10047 && child_pdi->linkage_name != NULL)
10048 {
10049 char *actual_class_name
10050 = language_class_name_from_physname (cu->language_defn,
10051 child_pdi->linkage_name);
10052 if (actual_class_name != NULL)
10053 {
10054 struct_pdi->name
10055 = obsavestring (actual_class_name,
10056 strlen (actual_class_name),
10057 &cu->objfile->objfile_obstack);
10058 xfree (actual_class_name);
10059 }
10060 break;
10061 }
10062 }
10063 }
10064
10065 /* Adjust PART_DIE before generating a symbol for it. This function
10066 may set the is_external flag or change the DIE's name. */
10067
10068 static void
10069 fixup_partial_die (struct partial_die_info *part_die,
10070 struct dwarf2_cu *cu)
10071 {
10072 /* Once we've fixed up a die, there's no point in doing so again.
10073 This also avoids a memory leak if we were to call
10074 guess_partial_die_structure_name multiple times. */
10075 if (part_die->fixup_called)
10076 return;
10077
10078 /* If we found a reference attribute and the DIE has no name, try
10079 to find a name in the referred to DIE. */
10080
10081 if (part_die->name == NULL && part_die->has_specification)
10082 {
10083 struct partial_die_info *spec_die;
10084
10085 spec_die = find_partial_die (part_die->spec_offset, cu);
10086
10087 fixup_partial_die (spec_die, cu);
10088
10089 if (spec_die->name)
10090 {
10091 part_die->name = spec_die->name;
10092
10093 /* Copy DW_AT_external attribute if it is set. */
10094 if (spec_die->is_external)
10095 part_die->is_external = spec_die->is_external;
10096 }
10097 }
10098
10099 /* Set default names for some unnamed DIEs. */
10100
10101 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10102 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10103
10104 /* If there is no parent die to provide a namespace, and there are
10105 children, see if we can determine the namespace from their linkage
10106 name. */
10107 if (cu->language == language_cplus
10108 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10109 && part_die->die_parent == NULL
10110 && part_die->has_children
10111 && (part_die->tag == DW_TAG_class_type
10112 || part_die->tag == DW_TAG_structure_type
10113 || part_die->tag == DW_TAG_union_type))
10114 guess_partial_die_structure_name (part_die, cu);
10115
10116 /* GCC might emit a nameless struct or union that has a linkage
10117 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10118 if (part_die->name == NULL
10119 && (part_die->tag == DW_TAG_class_type
10120 || part_die->tag == DW_TAG_interface_type
10121 || part_die->tag == DW_TAG_structure_type
10122 || part_die->tag == DW_TAG_union_type)
10123 && part_die->linkage_name != NULL)
10124 {
10125 char *demangled;
10126
10127 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10128 if (demangled)
10129 {
10130 const char *base;
10131
10132 /* Strip any leading namespaces/classes, keep only the base name.
10133 DW_AT_name for named DIEs does not contain the prefixes. */
10134 base = strrchr (demangled, ':');
10135 if (base && base > demangled && base[-1] == ':')
10136 base++;
10137 else
10138 base = demangled;
10139
10140 part_die->name = obsavestring (base, strlen (base),
10141 &cu->objfile->objfile_obstack);
10142 xfree (demangled);
10143 }
10144 }
10145
10146 part_die->fixup_called = 1;
10147 }
10148
10149 /* Read an attribute value described by an attribute form. */
10150
10151 static gdb_byte *
10152 read_attribute_value (struct attribute *attr, unsigned form,
10153 bfd *abfd, gdb_byte *info_ptr,
10154 struct dwarf2_cu *cu)
10155 {
10156 struct comp_unit_head *cu_header = &cu->header;
10157 unsigned int bytes_read;
10158 struct dwarf_block *blk;
10159
10160 attr->form = form;
10161 switch (form)
10162 {
10163 case DW_FORM_ref_addr:
10164 if (cu->header.version == 2)
10165 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10166 else
10167 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10168 &cu->header, &bytes_read);
10169 info_ptr += bytes_read;
10170 break;
10171 case DW_FORM_addr:
10172 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10173 info_ptr += bytes_read;
10174 break;
10175 case DW_FORM_block2:
10176 blk = dwarf_alloc_block (cu);
10177 blk->size = read_2_bytes (abfd, info_ptr);
10178 info_ptr += 2;
10179 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10180 info_ptr += blk->size;
10181 DW_BLOCK (attr) = blk;
10182 break;
10183 case DW_FORM_block4:
10184 blk = dwarf_alloc_block (cu);
10185 blk->size = read_4_bytes (abfd, info_ptr);
10186 info_ptr += 4;
10187 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10188 info_ptr += blk->size;
10189 DW_BLOCK (attr) = blk;
10190 break;
10191 case DW_FORM_data2:
10192 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10193 info_ptr += 2;
10194 break;
10195 case DW_FORM_data4:
10196 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10197 info_ptr += 4;
10198 break;
10199 case DW_FORM_data8:
10200 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10201 info_ptr += 8;
10202 break;
10203 case DW_FORM_sec_offset:
10204 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10205 info_ptr += bytes_read;
10206 break;
10207 case DW_FORM_string:
10208 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10209 DW_STRING_IS_CANONICAL (attr) = 0;
10210 info_ptr += bytes_read;
10211 break;
10212 case DW_FORM_strp:
10213 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10214 &bytes_read);
10215 DW_STRING_IS_CANONICAL (attr) = 0;
10216 info_ptr += bytes_read;
10217 break;
10218 case DW_FORM_exprloc:
10219 case DW_FORM_block:
10220 blk = dwarf_alloc_block (cu);
10221 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10222 info_ptr += bytes_read;
10223 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10224 info_ptr += blk->size;
10225 DW_BLOCK (attr) = blk;
10226 break;
10227 case DW_FORM_block1:
10228 blk = dwarf_alloc_block (cu);
10229 blk->size = read_1_byte (abfd, info_ptr);
10230 info_ptr += 1;
10231 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10232 info_ptr += blk->size;
10233 DW_BLOCK (attr) = blk;
10234 break;
10235 case DW_FORM_data1:
10236 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10237 info_ptr += 1;
10238 break;
10239 case DW_FORM_flag:
10240 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10241 info_ptr += 1;
10242 break;
10243 case DW_FORM_flag_present:
10244 DW_UNSND (attr) = 1;
10245 break;
10246 case DW_FORM_sdata:
10247 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10248 info_ptr += bytes_read;
10249 break;
10250 case DW_FORM_udata:
10251 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10252 info_ptr += bytes_read;
10253 break;
10254 case DW_FORM_ref1:
10255 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10256 info_ptr += 1;
10257 break;
10258 case DW_FORM_ref2:
10259 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10260 info_ptr += 2;
10261 break;
10262 case DW_FORM_ref4:
10263 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10264 info_ptr += 4;
10265 break;
10266 case DW_FORM_ref8:
10267 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10268 info_ptr += 8;
10269 break;
10270 case DW_FORM_ref_sig8:
10271 /* Convert the signature to something we can record in DW_UNSND
10272 for later lookup.
10273 NOTE: This is NULL if the type wasn't found. */
10274 DW_SIGNATURED_TYPE (attr) =
10275 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10276 info_ptr += 8;
10277 break;
10278 case DW_FORM_ref_udata:
10279 DW_ADDR (attr) = (cu->header.offset
10280 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10281 info_ptr += bytes_read;
10282 break;
10283 case DW_FORM_indirect:
10284 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10285 info_ptr += bytes_read;
10286 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10287 break;
10288 default:
10289 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10290 dwarf_form_name (form),
10291 bfd_get_filename (abfd));
10292 }
10293
10294 /* We have seen instances where the compiler tried to emit a byte
10295 size attribute of -1 which ended up being encoded as an unsigned
10296 0xffffffff. Although 0xffffffff is technically a valid size value,
10297 an object of this size seems pretty unlikely so we can relatively
10298 safely treat these cases as if the size attribute was invalid and
10299 treat them as zero by default. */
10300 if (attr->name == DW_AT_byte_size
10301 && form == DW_FORM_data4
10302 && DW_UNSND (attr) >= 0xffffffff)
10303 {
10304 complaint
10305 (&symfile_complaints,
10306 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10307 hex_string (DW_UNSND (attr)));
10308 DW_UNSND (attr) = 0;
10309 }
10310
10311 return info_ptr;
10312 }
10313
10314 /* Read an attribute described by an abbreviated attribute. */
10315
10316 static gdb_byte *
10317 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10318 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10319 {
10320 attr->name = abbrev->name;
10321 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10322 }
10323
10324 /* Read dwarf information from a buffer. */
10325
10326 static unsigned int
10327 read_1_byte (bfd *abfd, gdb_byte *buf)
10328 {
10329 return bfd_get_8 (abfd, buf);
10330 }
10331
10332 static int
10333 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10334 {
10335 return bfd_get_signed_8 (abfd, buf);
10336 }
10337
10338 static unsigned int
10339 read_2_bytes (bfd *abfd, gdb_byte *buf)
10340 {
10341 return bfd_get_16 (abfd, buf);
10342 }
10343
10344 static int
10345 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10346 {
10347 return bfd_get_signed_16 (abfd, buf);
10348 }
10349
10350 static unsigned int
10351 read_4_bytes (bfd *abfd, gdb_byte *buf)
10352 {
10353 return bfd_get_32 (abfd, buf);
10354 }
10355
10356 static int
10357 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10358 {
10359 return bfd_get_signed_32 (abfd, buf);
10360 }
10361
10362 static ULONGEST
10363 read_8_bytes (bfd *abfd, gdb_byte *buf)
10364 {
10365 return bfd_get_64 (abfd, buf);
10366 }
10367
10368 static CORE_ADDR
10369 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10370 unsigned int *bytes_read)
10371 {
10372 struct comp_unit_head *cu_header = &cu->header;
10373 CORE_ADDR retval = 0;
10374
10375 if (cu_header->signed_addr_p)
10376 {
10377 switch (cu_header->addr_size)
10378 {
10379 case 2:
10380 retval = bfd_get_signed_16 (abfd, buf);
10381 break;
10382 case 4:
10383 retval = bfd_get_signed_32 (abfd, buf);
10384 break;
10385 case 8:
10386 retval = bfd_get_signed_64 (abfd, buf);
10387 break;
10388 default:
10389 internal_error (__FILE__, __LINE__,
10390 _("read_address: bad switch, signed [in module %s]"),
10391 bfd_get_filename (abfd));
10392 }
10393 }
10394 else
10395 {
10396 switch (cu_header->addr_size)
10397 {
10398 case 2:
10399 retval = bfd_get_16 (abfd, buf);
10400 break;
10401 case 4:
10402 retval = bfd_get_32 (abfd, buf);
10403 break;
10404 case 8:
10405 retval = bfd_get_64 (abfd, buf);
10406 break;
10407 default:
10408 internal_error (__FILE__, __LINE__,
10409 _("read_address: bad switch, "
10410 "unsigned [in module %s]"),
10411 bfd_get_filename (abfd));
10412 }
10413 }
10414
10415 *bytes_read = cu_header->addr_size;
10416 return retval;
10417 }
10418
10419 /* Read the initial length from a section. The (draft) DWARF 3
10420 specification allows the initial length to take up either 4 bytes
10421 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10422 bytes describe the length and all offsets will be 8 bytes in length
10423 instead of 4.
10424
10425 An older, non-standard 64-bit format is also handled by this
10426 function. The older format in question stores the initial length
10427 as an 8-byte quantity without an escape value. Lengths greater
10428 than 2^32 aren't very common which means that the initial 4 bytes
10429 is almost always zero. Since a length value of zero doesn't make
10430 sense for the 32-bit format, this initial zero can be considered to
10431 be an escape value which indicates the presence of the older 64-bit
10432 format. As written, the code can't detect (old format) lengths
10433 greater than 4GB. If it becomes necessary to handle lengths
10434 somewhat larger than 4GB, we could allow other small values (such
10435 as the non-sensical values of 1, 2, and 3) to also be used as
10436 escape values indicating the presence of the old format.
10437
10438 The value returned via bytes_read should be used to increment the
10439 relevant pointer after calling read_initial_length().
10440
10441 [ Note: read_initial_length() and read_offset() are based on the
10442 document entitled "DWARF Debugging Information Format", revision
10443 3, draft 8, dated November 19, 2001. This document was obtained
10444 from:
10445
10446 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10447
10448 This document is only a draft and is subject to change. (So beware.)
10449
10450 Details regarding the older, non-standard 64-bit format were
10451 determined empirically by examining 64-bit ELF files produced by
10452 the SGI toolchain on an IRIX 6.5 machine.
10453
10454 - Kevin, July 16, 2002
10455 ] */
10456
10457 static LONGEST
10458 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10459 {
10460 LONGEST length = bfd_get_32 (abfd, buf);
10461
10462 if (length == 0xffffffff)
10463 {
10464 length = bfd_get_64 (abfd, buf + 4);
10465 *bytes_read = 12;
10466 }
10467 else if (length == 0)
10468 {
10469 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10470 length = bfd_get_64 (abfd, buf);
10471 *bytes_read = 8;
10472 }
10473 else
10474 {
10475 *bytes_read = 4;
10476 }
10477
10478 return length;
10479 }
10480
10481 /* Cover function for read_initial_length.
10482 Returns the length of the object at BUF, and stores the size of the
10483 initial length in *BYTES_READ and stores the size that offsets will be in
10484 *OFFSET_SIZE.
10485 If the initial length size is not equivalent to that specified in
10486 CU_HEADER then issue a complaint.
10487 This is useful when reading non-comp-unit headers. */
10488
10489 static LONGEST
10490 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10491 const struct comp_unit_head *cu_header,
10492 unsigned int *bytes_read,
10493 unsigned int *offset_size)
10494 {
10495 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10496
10497 gdb_assert (cu_header->initial_length_size == 4
10498 || cu_header->initial_length_size == 8
10499 || cu_header->initial_length_size == 12);
10500
10501 if (cu_header->initial_length_size != *bytes_read)
10502 complaint (&symfile_complaints,
10503 _("intermixed 32-bit and 64-bit DWARF sections"));
10504
10505 *offset_size = (*bytes_read == 4) ? 4 : 8;
10506 return length;
10507 }
10508
10509 /* Read an offset from the data stream. The size of the offset is
10510 given by cu_header->offset_size. */
10511
10512 static LONGEST
10513 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10514 unsigned int *bytes_read)
10515 {
10516 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10517
10518 *bytes_read = cu_header->offset_size;
10519 return offset;
10520 }
10521
10522 /* Read an offset from the data stream. */
10523
10524 static LONGEST
10525 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10526 {
10527 LONGEST retval = 0;
10528
10529 switch (offset_size)
10530 {
10531 case 4:
10532 retval = bfd_get_32 (abfd, buf);
10533 break;
10534 case 8:
10535 retval = bfd_get_64 (abfd, buf);
10536 break;
10537 default:
10538 internal_error (__FILE__, __LINE__,
10539 _("read_offset_1: bad switch [in module %s]"),
10540 bfd_get_filename (abfd));
10541 }
10542
10543 return retval;
10544 }
10545
10546 static gdb_byte *
10547 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10548 {
10549 /* If the size of a host char is 8 bits, we can return a pointer
10550 to the buffer, otherwise we have to copy the data to a buffer
10551 allocated on the temporary obstack. */
10552 gdb_assert (HOST_CHAR_BIT == 8);
10553 return buf;
10554 }
10555
10556 static char *
10557 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10558 {
10559 /* If the size of a host char is 8 bits, we can return a pointer
10560 to the string, otherwise we have to copy the string to a buffer
10561 allocated on the temporary obstack. */
10562 gdb_assert (HOST_CHAR_BIT == 8);
10563 if (*buf == '\0')
10564 {
10565 *bytes_read_ptr = 1;
10566 return NULL;
10567 }
10568 *bytes_read_ptr = strlen ((char *) buf) + 1;
10569 return (char *) buf;
10570 }
10571
10572 static char *
10573 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10574 {
10575 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10576 if (dwarf2_per_objfile->str.buffer == NULL)
10577 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10578 bfd_get_filename (abfd));
10579 if (str_offset >= dwarf2_per_objfile->str.size)
10580 error (_("DW_FORM_strp pointing outside of "
10581 ".debug_str section [in module %s]"),
10582 bfd_get_filename (abfd));
10583 gdb_assert (HOST_CHAR_BIT == 8);
10584 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10585 return NULL;
10586 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10587 }
10588
10589 static char *
10590 read_indirect_string (bfd *abfd, gdb_byte *buf,
10591 const struct comp_unit_head *cu_header,
10592 unsigned int *bytes_read_ptr)
10593 {
10594 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10595
10596 return read_indirect_string_at_offset (abfd, str_offset);
10597 }
10598
10599 static unsigned long
10600 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10601 {
10602 unsigned long result;
10603 unsigned int num_read;
10604 int i, shift;
10605 unsigned char byte;
10606
10607 result = 0;
10608 shift = 0;
10609 num_read = 0;
10610 i = 0;
10611 while (1)
10612 {
10613 byte = bfd_get_8 (abfd, buf);
10614 buf++;
10615 num_read++;
10616 result |= ((unsigned long)(byte & 127) << shift);
10617 if ((byte & 128) == 0)
10618 {
10619 break;
10620 }
10621 shift += 7;
10622 }
10623 *bytes_read_ptr = num_read;
10624 return result;
10625 }
10626
10627 static long
10628 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10629 {
10630 long result;
10631 int i, shift, num_read;
10632 unsigned char byte;
10633
10634 result = 0;
10635 shift = 0;
10636 num_read = 0;
10637 i = 0;
10638 while (1)
10639 {
10640 byte = bfd_get_8 (abfd, buf);
10641 buf++;
10642 num_read++;
10643 result |= ((long)(byte & 127) << shift);
10644 shift += 7;
10645 if ((byte & 128) == 0)
10646 {
10647 break;
10648 }
10649 }
10650 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10651 result |= -(((long)1) << shift);
10652 *bytes_read_ptr = num_read;
10653 return result;
10654 }
10655
10656 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10657
10658 static gdb_byte *
10659 skip_leb128 (bfd *abfd, gdb_byte *buf)
10660 {
10661 int byte;
10662
10663 while (1)
10664 {
10665 byte = bfd_get_8 (abfd, buf);
10666 buf++;
10667 if ((byte & 128) == 0)
10668 return buf;
10669 }
10670 }
10671
10672 static void
10673 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10674 {
10675 switch (lang)
10676 {
10677 case DW_LANG_C89:
10678 case DW_LANG_C99:
10679 case DW_LANG_C:
10680 cu->language = language_c;
10681 break;
10682 case DW_LANG_C_plus_plus:
10683 cu->language = language_cplus;
10684 break;
10685 case DW_LANG_D:
10686 cu->language = language_d;
10687 break;
10688 case DW_LANG_Fortran77:
10689 case DW_LANG_Fortran90:
10690 case DW_LANG_Fortran95:
10691 cu->language = language_fortran;
10692 break;
10693 case DW_LANG_Mips_Assembler:
10694 cu->language = language_asm;
10695 break;
10696 case DW_LANG_Java:
10697 cu->language = language_java;
10698 break;
10699 case DW_LANG_Ada83:
10700 case DW_LANG_Ada95:
10701 cu->language = language_ada;
10702 break;
10703 case DW_LANG_Modula2:
10704 cu->language = language_m2;
10705 break;
10706 case DW_LANG_Pascal83:
10707 cu->language = language_pascal;
10708 break;
10709 case DW_LANG_ObjC:
10710 cu->language = language_objc;
10711 break;
10712 case DW_LANG_Cobol74:
10713 case DW_LANG_Cobol85:
10714 default:
10715 cu->language = language_minimal;
10716 break;
10717 }
10718 cu->language_defn = language_def (cu->language);
10719 }
10720
10721 /* Return the named attribute or NULL if not there. */
10722
10723 static struct attribute *
10724 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10725 {
10726 for (;;)
10727 {
10728 unsigned int i;
10729 struct attribute *spec = NULL;
10730
10731 for (i = 0; i < die->num_attrs; ++i)
10732 {
10733 if (die->attrs[i].name == name)
10734 return &die->attrs[i];
10735 if (die->attrs[i].name == DW_AT_specification
10736 || die->attrs[i].name == DW_AT_abstract_origin)
10737 spec = &die->attrs[i];
10738 }
10739
10740 if (!spec)
10741 break;
10742
10743 die = follow_die_ref (die, spec, &cu);
10744 }
10745
10746 return NULL;
10747 }
10748
10749 /* Return the named attribute or NULL if not there,
10750 but do not follow DW_AT_specification, etc.
10751 This is for use in contexts where we're reading .debug_types dies.
10752 Following DW_AT_specification, DW_AT_abstract_origin will take us
10753 back up the chain, and we want to go down. */
10754
10755 static struct attribute *
10756 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10757 struct dwarf2_cu *cu)
10758 {
10759 unsigned int i;
10760
10761 for (i = 0; i < die->num_attrs; ++i)
10762 if (die->attrs[i].name == name)
10763 return &die->attrs[i];
10764
10765 return NULL;
10766 }
10767
10768 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10769 and holds a non-zero value. This function should only be used for
10770 DW_FORM_flag or DW_FORM_flag_present attributes. */
10771
10772 static int
10773 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10774 {
10775 struct attribute *attr = dwarf2_attr (die, name, cu);
10776
10777 return (attr && DW_UNSND (attr));
10778 }
10779
10780 static int
10781 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10782 {
10783 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10784 which value is non-zero. However, we have to be careful with
10785 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10786 (via dwarf2_flag_true_p) follows this attribute. So we may
10787 end up accidently finding a declaration attribute that belongs
10788 to a different DIE referenced by the specification attribute,
10789 even though the given DIE does not have a declaration attribute. */
10790 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10791 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10792 }
10793
10794 /* Return the die giving the specification for DIE, if there is
10795 one. *SPEC_CU is the CU containing DIE on input, and the CU
10796 containing the return value on output. If there is no
10797 specification, but there is an abstract origin, that is
10798 returned. */
10799
10800 static struct die_info *
10801 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10802 {
10803 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10804 *spec_cu);
10805
10806 if (spec_attr == NULL)
10807 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10808
10809 if (spec_attr == NULL)
10810 return NULL;
10811 else
10812 return follow_die_ref (die, spec_attr, spec_cu);
10813 }
10814
10815 /* Free the line_header structure *LH, and any arrays and strings it
10816 refers to.
10817 NOTE: This is also used as a "cleanup" function. */
10818
10819 static void
10820 free_line_header (struct line_header *lh)
10821 {
10822 if (lh->standard_opcode_lengths)
10823 xfree (lh->standard_opcode_lengths);
10824
10825 /* Remember that all the lh->file_names[i].name pointers are
10826 pointers into debug_line_buffer, and don't need to be freed. */
10827 if (lh->file_names)
10828 xfree (lh->file_names);
10829
10830 /* Similarly for the include directory names. */
10831 if (lh->include_dirs)
10832 xfree (lh->include_dirs);
10833
10834 xfree (lh);
10835 }
10836
10837 /* Add an entry to LH's include directory table. */
10838
10839 static void
10840 add_include_dir (struct line_header *lh, char *include_dir)
10841 {
10842 /* Grow the array if necessary. */
10843 if (lh->include_dirs_size == 0)
10844 {
10845 lh->include_dirs_size = 1; /* for testing */
10846 lh->include_dirs = xmalloc (lh->include_dirs_size
10847 * sizeof (*lh->include_dirs));
10848 }
10849 else if (lh->num_include_dirs >= lh->include_dirs_size)
10850 {
10851 lh->include_dirs_size *= 2;
10852 lh->include_dirs = xrealloc (lh->include_dirs,
10853 (lh->include_dirs_size
10854 * sizeof (*lh->include_dirs)));
10855 }
10856
10857 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10858 }
10859
10860 /* Add an entry to LH's file name table. */
10861
10862 static void
10863 add_file_name (struct line_header *lh,
10864 char *name,
10865 unsigned int dir_index,
10866 unsigned int mod_time,
10867 unsigned int length)
10868 {
10869 struct file_entry *fe;
10870
10871 /* Grow the array if necessary. */
10872 if (lh->file_names_size == 0)
10873 {
10874 lh->file_names_size = 1; /* for testing */
10875 lh->file_names = xmalloc (lh->file_names_size
10876 * sizeof (*lh->file_names));
10877 }
10878 else if (lh->num_file_names >= lh->file_names_size)
10879 {
10880 lh->file_names_size *= 2;
10881 lh->file_names = xrealloc (lh->file_names,
10882 (lh->file_names_size
10883 * sizeof (*lh->file_names)));
10884 }
10885
10886 fe = &lh->file_names[lh->num_file_names++];
10887 fe->name = name;
10888 fe->dir_index = dir_index;
10889 fe->mod_time = mod_time;
10890 fe->length = length;
10891 fe->included_p = 0;
10892 fe->symtab = NULL;
10893 }
10894
10895 /* Read the statement program header starting at OFFSET in
10896 .debug_line, according to the endianness of ABFD. Return a pointer
10897 to a struct line_header, allocated using xmalloc.
10898
10899 NOTE: the strings in the include directory and file name tables of
10900 the returned object point into debug_line_buffer, and must not be
10901 freed. */
10902
10903 static struct line_header *
10904 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10905 struct dwarf2_cu *cu)
10906 {
10907 struct cleanup *back_to;
10908 struct line_header *lh;
10909 gdb_byte *line_ptr;
10910 unsigned int bytes_read, offset_size;
10911 int i;
10912 char *cur_dir, *cur_file;
10913
10914 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10915 if (dwarf2_per_objfile->line.buffer == NULL)
10916 {
10917 complaint (&symfile_complaints, _("missing .debug_line section"));
10918 return 0;
10919 }
10920
10921 /* Make sure that at least there's room for the total_length field.
10922 That could be 12 bytes long, but we're just going to fudge that. */
10923 if (offset + 4 >= dwarf2_per_objfile->line.size)
10924 {
10925 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10926 return 0;
10927 }
10928
10929 lh = xmalloc (sizeof (*lh));
10930 memset (lh, 0, sizeof (*lh));
10931 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10932 (void *) lh);
10933
10934 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10935
10936 /* Read in the header. */
10937 lh->total_length =
10938 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10939 &bytes_read, &offset_size);
10940 line_ptr += bytes_read;
10941 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10942 + dwarf2_per_objfile->line.size))
10943 {
10944 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10945 return 0;
10946 }
10947 lh->statement_program_end = line_ptr + lh->total_length;
10948 lh->version = read_2_bytes (abfd, line_ptr);
10949 line_ptr += 2;
10950 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10951 line_ptr += offset_size;
10952 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10953 line_ptr += 1;
10954 if (lh->version >= 4)
10955 {
10956 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10957 line_ptr += 1;
10958 }
10959 else
10960 lh->maximum_ops_per_instruction = 1;
10961
10962 if (lh->maximum_ops_per_instruction == 0)
10963 {
10964 lh->maximum_ops_per_instruction = 1;
10965 complaint (&symfile_complaints,
10966 _("invalid maximum_ops_per_instruction "
10967 "in `.debug_line' section"));
10968 }
10969
10970 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10971 line_ptr += 1;
10972 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10973 line_ptr += 1;
10974 lh->line_range = read_1_byte (abfd, line_ptr);
10975 line_ptr += 1;
10976 lh->opcode_base = read_1_byte (abfd, line_ptr);
10977 line_ptr += 1;
10978 lh->standard_opcode_lengths
10979 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10980
10981 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10982 for (i = 1; i < lh->opcode_base; ++i)
10983 {
10984 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10985 line_ptr += 1;
10986 }
10987
10988 /* Read directory table. */
10989 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10990 {
10991 line_ptr += bytes_read;
10992 add_include_dir (lh, cur_dir);
10993 }
10994 line_ptr += bytes_read;
10995
10996 /* Read file name table. */
10997 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10998 {
10999 unsigned int dir_index, mod_time, length;
11000
11001 line_ptr += bytes_read;
11002 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11003 line_ptr += bytes_read;
11004 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11005 line_ptr += bytes_read;
11006 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11007 line_ptr += bytes_read;
11008
11009 add_file_name (lh, cur_file, dir_index, mod_time, length);
11010 }
11011 line_ptr += bytes_read;
11012 lh->statement_program_start = line_ptr;
11013
11014 if (line_ptr > (dwarf2_per_objfile->line.buffer
11015 + dwarf2_per_objfile->line.size))
11016 complaint (&symfile_complaints,
11017 _("line number info header doesn't "
11018 "fit in `.debug_line' section"));
11019
11020 discard_cleanups (back_to);
11021 return lh;
11022 }
11023
11024 /* Subroutine of dwarf_decode_lines to simplify it.
11025 Return the file name of the psymtab for included file FILE_INDEX
11026 in line header LH of PST.
11027 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11028 If space for the result is malloc'd, it will be freed by a cleanup.
11029 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11030
11031 static char *
11032 psymtab_include_file_name (const struct line_header *lh, int file_index,
11033 const struct partial_symtab *pst,
11034 const char *comp_dir)
11035 {
11036 const struct file_entry fe = lh->file_names [file_index];
11037 char *include_name = fe.name;
11038 char *include_name_to_compare = include_name;
11039 char *dir_name = NULL;
11040 const char *pst_filename;
11041 char *copied_name = NULL;
11042 int file_is_pst;
11043
11044 if (fe.dir_index)
11045 dir_name = lh->include_dirs[fe.dir_index - 1];
11046
11047 if (!IS_ABSOLUTE_PATH (include_name)
11048 && (dir_name != NULL || comp_dir != NULL))
11049 {
11050 /* Avoid creating a duplicate psymtab for PST.
11051 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11052 Before we do the comparison, however, we need to account
11053 for DIR_NAME and COMP_DIR.
11054 First prepend dir_name (if non-NULL). If we still don't
11055 have an absolute path prepend comp_dir (if non-NULL).
11056 However, the directory we record in the include-file's
11057 psymtab does not contain COMP_DIR (to match the
11058 corresponding symtab(s)).
11059
11060 Example:
11061
11062 bash$ cd /tmp
11063 bash$ gcc -g ./hello.c
11064 include_name = "hello.c"
11065 dir_name = "."
11066 DW_AT_comp_dir = comp_dir = "/tmp"
11067 DW_AT_name = "./hello.c" */
11068
11069 if (dir_name != NULL)
11070 {
11071 include_name = concat (dir_name, SLASH_STRING,
11072 include_name, (char *)NULL);
11073 include_name_to_compare = include_name;
11074 make_cleanup (xfree, include_name);
11075 }
11076 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11077 {
11078 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11079 include_name, (char *)NULL);
11080 }
11081 }
11082
11083 pst_filename = pst->filename;
11084 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11085 {
11086 copied_name = concat (pst->dirname, SLASH_STRING,
11087 pst_filename, (char *)NULL);
11088 pst_filename = copied_name;
11089 }
11090
11091 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11092
11093 if (include_name_to_compare != include_name)
11094 xfree (include_name_to_compare);
11095 if (copied_name != NULL)
11096 xfree (copied_name);
11097
11098 if (file_is_pst)
11099 return NULL;
11100 return include_name;
11101 }
11102
11103 /* Ignore this record_line request. */
11104
11105 static void
11106 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11107 {
11108 return;
11109 }
11110
11111 /* Subroutine of dwarf_decode_lines to simplify it.
11112 Process the line number information in LH. */
11113
11114 static void
11115 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11116 struct dwarf2_cu *cu, struct partial_symtab *pst)
11117 {
11118 gdb_byte *line_ptr, *extended_end;
11119 gdb_byte *line_end;
11120 unsigned int bytes_read, extended_len;
11121 unsigned char op_code, extended_op, adj_opcode;
11122 CORE_ADDR baseaddr;
11123 struct objfile *objfile = cu->objfile;
11124 bfd *abfd = objfile->obfd;
11125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11126 const int decode_for_pst_p = (pst != NULL);
11127 struct subfile *last_subfile = NULL;
11128 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11129 = record_line;
11130
11131 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11132
11133 line_ptr = lh->statement_program_start;
11134 line_end = lh->statement_program_end;
11135
11136 /* Read the statement sequences until there's nothing left. */
11137 while (line_ptr < line_end)
11138 {
11139 /* state machine registers */
11140 CORE_ADDR address = 0;
11141 unsigned int file = 1;
11142 unsigned int line = 1;
11143 unsigned int column = 0;
11144 int is_stmt = lh->default_is_stmt;
11145 int basic_block = 0;
11146 int end_sequence = 0;
11147 CORE_ADDR addr;
11148 unsigned char op_index = 0;
11149
11150 if (!decode_for_pst_p && lh->num_file_names >= file)
11151 {
11152 /* Start a subfile for the current file of the state machine. */
11153 /* lh->include_dirs and lh->file_names are 0-based, but the
11154 directory and file name numbers in the statement program
11155 are 1-based. */
11156 struct file_entry *fe = &lh->file_names[file - 1];
11157 char *dir = NULL;
11158
11159 if (fe->dir_index)
11160 dir = lh->include_dirs[fe->dir_index - 1];
11161
11162 dwarf2_start_subfile (fe->name, dir, comp_dir);
11163 }
11164
11165 /* Decode the table. */
11166 while (!end_sequence)
11167 {
11168 op_code = read_1_byte (abfd, line_ptr);
11169 line_ptr += 1;
11170 if (line_ptr > line_end)
11171 {
11172 dwarf2_debug_line_missing_end_sequence_complaint ();
11173 break;
11174 }
11175
11176 if (op_code >= lh->opcode_base)
11177 {
11178 /* Special operand. */
11179 adj_opcode = op_code - lh->opcode_base;
11180 address += (((op_index + (adj_opcode / lh->line_range))
11181 / lh->maximum_ops_per_instruction)
11182 * lh->minimum_instruction_length);
11183 op_index = ((op_index + (adj_opcode / lh->line_range))
11184 % lh->maximum_ops_per_instruction);
11185 line += lh->line_base + (adj_opcode % lh->line_range);
11186 if (lh->num_file_names < file || file == 0)
11187 dwarf2_debug_line_missing_file_complaint ();
11188 /* For now we ignore lines not starting on an
11189 instruction boundary. */
11190 else if (op_index == 0)
11191 {
11192 lh->file_names[file - 1].included_p = 1;
11193 if (!decode_for_pst_p && is_stmt)
11194 {
11195 if (last_subfile != current_subfile)
11196 {
11197 addr = gdbarch_addr_bits_remove (gdbarch, address);
11198 if (last_subfile)
11199 (*p_record_line) (last_subfile, 0, addr);
11200 last_subfile = current_subfile;
11201 }
11202 /* Append row to matrix using current values. */
11203 addr = gdbarch_addr_bits_remove (gdbarch, address);
11204 (*p_record_line) (current_subfile, line, addr);
11205 }
11206 }
11207 basic_block = 0;
11208 }
11209 else switch (op_code)
11210 {
11211 case DW_LNS_extended_op:
11212 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11213 &bytes_read);
11214 line_ptr += bytes_read;
11215 extended_end = line_ptr + extended_len;
11216 extended_op = read_1_byte (abfd, line_ptr);
11217 line_ptr += 1;
11218 switch (extended_op)
11219 {
11220 case DW_LNE_end_sequence:
11221 p_record_line = record_line;
11222 end_sequence = 1;
11223 break;
11224 case DW_LNE_set_address:
11225 address = read_address (abfd, line_ptr, cu, &bytes_read);
11226
11227 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11228 {
11229 /* This line table is for a function which has been
11230 GCd by the linker. Ignore it. PR gdb/12528 */
11231
11232 long line_offset
11233 = line_ptr - dwarf2_per_objfile->line.buffer;
11234
11235 complaint (&symfile_complaints,
11236 _(".debug_line address at offset 0x%lx is 0 "
11237 "[in module %s]"),
11238 line_offset, objfile->name);
11239 p_record_line = noop_record_line;
11240 }
11241
11242 op_index = 0;
11243 line_ptr += bytes_read;
11244 address += baseaddr;
11245 break;
11246 case DW_LNE_define_file:
11247 {
11248 char *cur_file;
11249 unsigned int dir_index, mod_time, length;
11250
11251 cur_file = read_direct_string (abfd, line_ptr,
11252 &bytes_read);
11253 line_ptr += bytes_read;
11254 dir_index =
11255 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11256 line_ptr += bytes_read;
11257 mod_time =
11258 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11259 line_ptr += bytes_read;
11260 length =
11261 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11262 line_ptr += bytes_read;
11263 add_file_name (lh, cur_file, dir_index, mod_time, length);
11264 }
11265 break;
11266 case DW_LNE_set_discriminator:
11267 /* The discriminator is not interesting to the debugger;
11268 just ignore it. */
11269 line_ptr = extended_end;
11270 break;
11271 default:
11272 complaint (&symfile_complaints,
11273 _("mangled .debug_line section"));
11274 return;
11275 }
11276 /* Make sure that we parsed the extended op correctly. If e.g.
11277 we expected a different address size than the producer used,
11278 we may have read the wrong number of bytes. */
11279 if (line_ptr != extended_end)
11280 {
11281 complaint (&symfile_complaints,
11282 _("mangled .debug_line section"));
11283 return;
11284 }
11285 break;
11286 case DW_LNS_copy:
11287 if (lh->num_file_names < file || file == 0)
11288 dwarf2_debug_line_missing_file_complaint ();
11289 else
11290 {
11291 lh->file_names[file - 1].included_p = 1;
11292 if (!decode_for_pst_p && is_stmt)
11293 {
11294 if (last_subfile != current_subfile)
11295 {
11296 addr = gdbarch_addr_bits_remove (gdbarch, address);
11297 if (last_subfile)
11298 (*p_record_line) (last_subfile, 0, addr);
11299 last_subfile = current_subfile;
11300 }
11301 addr = gdbarch_addr_bits_remove (gdbarch, address);
11302 (*p_record_line) (current_subfile, line, addr);
11303 }
11304 }
11305 basic_block = 0;
11306 break;
11307 case DW_LNS_advance_pc:
11308 {
11309 CORE_ADDR adjust
11310 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11311
11312 address += (((op_index + adjust)
11313 / lh->maximum_ops_per_instruction)
11314 * lh->minimum_instruction_length);
11315 op_index = ((op_index + adjust)
11316 % lh->maximum_ops_per_instruction);
11317 line_ptr += bytes_read;
11318 }
11319 break;
11320 case DW_LNS_advance_line:
11321 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11322 line_ptr += bytes_read;
11323 break;
11324 case DW_LNS_set_file:
11325 {
11326 /* The arrays lh->include_dirs and lh->file_names are
11327 0-based, but the directory and file name numbers in
11328 the statement program are 1-based. */
11329 struct file_entry *fe;
11330 char *dir = NULL;
11331
11332 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11333 line_ptr += bytes_read;
11334 if (lh->num_file_names < file || file == 0)
11335 dwarf2_debug_line_missing_file_complaint ();
11336 else
11337 {
11338 fe = &lh->file_names[file - 1];
11339 if (fe->dir_index)
11340 dir = lh->include_dirs[fe->dir_index - 1];
11341 if (!decode_for_pst_p)
11342 {
11343 last_subfile = current_subfile;
11344 dwarf2_start_subfile (fe->name, dir, comp_dir);
11345 }
11346 }
11347 }
11348 break;
11349 case DW_LNS_set_column:
11350 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11351 line_ptr += bytes_read;
11352 break;
11353 case DW_LNS_negate_stmt:
11354 is_stmt = (!is_stmt);
11355 break;
11356 case DW_LNS_set_basic_block:
11357 basic_block = 1;
11358 break;
11359 /* Add to the address register of the state machine the
11360 address increment value corresponding to special opcode
11361 255. I.e., this value is scaled by the minimum
11362 instruction length since special opcode 255 would have
11363 scaled the increment. */
11364 case DW_LNS_const_add_pc:
11365 {
11366 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11367
11368 address += (((op_index + adjust)
11369 / lh->maximum_ops_per_instruction)
11370 * lh->minimum_instruction_length);
11371 op_index = ((op_index + adjust)
11372 % lh->maximum_ops_per_instruction);
11373 }
11374 break;
11375 case DW_LNS_fixed_advance_pc:
11376 address += read_2_bytes (abfd, line_ptr);
11377 op_index = 0;
11378 line_ptr += 2;
11379 break;
11380 default:
11381 {
11382 /* Unknown standard opcode, ignore it. */
11383 int i;
11384
11385 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11386 {
11387 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11388 line_ptr += bytes_read;
11389 }
11390 }
11391 }
11392 }
11393 if (lh->num_file_names < file || file == 0)
11394 dwarf2_debug_line_missing_file_complaint ();
11395 else
11396 {
11397 lh->file_names[file - 1].included_p = 1;
11398 if (!decode_for_pst_p)
11399 {
11400 addr = gdbarch_addr_bits_remove (gdbarch, address);
11401 (*p_record_line) (current_subfile, 0, addr);
11402 }
11403 }
11404 }
11405 }
11406
11407 /* Decode the Line Number Program (LNP) for the given line_header
11408 structure and CU. The actual information extracted and the type
11409 of structures created from the LNP depends on the value of PST.
11410
11411 1. If PST is NULL, then this procedure uses the data from the program
11412 to create all necessary symbol tables, and their linetables.
11413
11414 2. If PST is not NULL, this procedure reads the program to determine
11415 the list of files included by the unit represented by PST, and
11416 builds all the associated partial symbol tables.
11417
11418 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11419 It is used for relative paths in the line table.
11420 NOTE: When processing partial symtabs (pst != NULL),
11421 comp_dir == pst->dirname.
11422
11423 NOTE: It is important that psymtabs have the same file name (via strcmp)
11424 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11425 symtab we don't use it in the name of the psymtabs we create.
11426 E.g. expand_line_sal requires this when finding psymtabs to expand.
11427 A good testcase for this is mb-inline.exp. */
11428
11429 static void
11430 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11431 struct dwarf2_cu *cu, struct partial_symtab *pst,
11432 int want_line_info)
11433 {
11434 struct objfile *objfile = cu->objfile;
11435 const int decode_for_pst_p = (pst != NULL);
11436 struct subfile *first_subfile = current_subfile;
11437
11438 if (want_line_info)
11439 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
11440
11441 if (decode_for_pst_p)
11442 {
11443 int file_index;
11444
11445 /* Now that we're done scanning the Line Header Program, we can
11446 create the psymtab of each included file. */
11447 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11448 if (lh->file_names[file_index].included_p == 1)
11449 {
11450 char *include_name =
11451 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11452 if (include_name != NULL)
11453 dwarf2_create_include_psymtab (include_name, pst, objfile);
11454 }
11455 }
11456 else
11457 {
11458 /* Make sure a symtab is created for every file, even files
11459 which contain only variables (i.e. no code with associated
11460 line numbers). */
11461 int i;
11462
11463 for (i = 0; i < lh->num_file_names; i++)
11464 {
11465 char *dir = NULL;
11466 struct file_entry *fe;
11467
11468 fe = &lh->file_names[i];
11469 if (fe->dir_index)
11470 dir = lh->include_dirs[fe->dir_index - 1];
11471 dwarf2_start_subfile (fe->name, dir, comp_dir);
11472
11473 /* Skip the main file; we don't need it, and it must be
11474 allocated last, so that it will show up before the
11475 non-primary symtabs in the objfile's symtab list. */
11476 if (current_subfile == first_subfile)
11477 continue;
11478
11479 if (current_subfile->symtab == NULL)
11480 current_subfile->symtab = allocate_symtab (current_subfile->name,
11481 objfile);
11482 fe->symtab = current_subfile->symtab;
11483 }
11484 }
11485 }
11486
11487 /* Start a subfile for DWARF. FILENAME is the name of the file and
11488 DIRNAME the name of the source directory which contains FILENAME
11489 or NULL if not known. COMP_DIR is the compilation directory for the
11490 linetable's compilation unit or NULL if not known.
11491 This routine tries to keep line numbers from identical absolute and
11492 relative file names in a common subfile.
11493
11494 Using the `list' example from the GDB testsuite, which resides in
11495 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11496 of /srcdir/list0.c yields the following debugging information for list0.c:
11497
11498 DW_AT_name: /srcdir/list0.c
11499 DW_AT_comp_dir: /compdir
11500 files.files[0].name: list0.h
11501 files.files[0].dir: /srcdir
11502 files.files[1].name: list0.c
11503 files.files[1].dir: /srcdir
11504
11505 The line number information for list0.c has to end up in a single
11506 subfile, so that `break /srcdir/list0.c:1' works as expected.
11507 start_subfile will ensure that this happens provided that we pass the
11508 concatenation of files.files[1].dir and files.files[1].name as the
11509 subfile's name. */
11510
11511 static void
11512 dwarf2_start_subfile (char *filename, const char *dirname,
11513 const char *comp_dir)
11514 {
11515 char *fullname;
11516
11517 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11518 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11519 second argument to start_subfile. To be consistent, we do the
11520 same here. In order not to lose the line information directory,
11521 we concatenate it to the filename when it makes sense.
11522 Note that the Dwarf3 standard says (speaking of filenames in line
11523 information): ``The directory index is ignored for file names
11524 that represent full path names''. Thus ignoring dirname in the
11525 `else' branch below isn't an issue. */
11526
11527 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11528 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11529 else
11530 fullname = filename;
11531
11532 start_subfile (fullname, comp_dir);
11533
11534 if (fullname != filename)
11535 xfree (fullname);
11536 }
11537
11538 static void
11539 var_decode_location (struct attribute *attr, struct symbol *sym,
11540 struct dwarf2_cu *cu)
11541 {
11542 struct objfile *objfile = cu->objfile;
11543 struct comp_unit_head *cu_header = &cu->header;
11544
11545 /* NOTE drow/2003-01-30: There used to be a comment and some special
11546 code here to turn a symbol with DW_AT_external and a
11547 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11548 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11549 with some versions of binutils) where shared libraries could have
11550 relocations against symbols in their debug information - the
11551 minimal symbol would have the right address, but the debug info
11552 would not. It's no longer necessary, because we will explicitly
11553 apply relocations when we read in the debug information now. */
11554
11555 /* A DW_AT_location attribute with no contents indicates that a
11556 variable has been optimized away. */
11557 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11558 {
11559 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11560 return;
11561 }
11562
11563 /* Handle one degenerate form of location expression specially, to
11564 preserve GDB's previous behavior when section offsets are
11565 specified. If this is just a DW_OP_addr then mark this symbol
11566 as LOC_STATIC. */
11567
11568 if (attr_form_is_block (attr)
11569 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11570 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11571 {
11572 unsigned int dummy;
11573
11574 SYMBOL_VALUE_ADDRESS (sym) =
11575 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11576 SYMBOL_CLASS (sym) = LOC_STATIC;
11577 fixup_symbol_section (sym, objfile);
11578 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11579 SYMBOL_SECTION (sym));
11580 return;
11581 }
11582
11583 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11584 expression evaluator, and use LOC_COMPUTED only when necessary
11585 (i.e. when the value of a register or memory location is
11586 referenced, or a thread-local block, etc.). Then again, it might
11587 not be worthwhile. I'm assuming that it isn't unless performance
11588 or memory numbers show me otherwise. */
11589
11590 dwarf2_symbol_mark_computed (attr, sym, cu);
11591 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11592
11593 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11594 cu->has_loclist = 1;
11595 }
11596
11597 /* Given a pointer to a DWARF information entry, figure out if we need
11598 to make a symbol table entry for it, and if so, create a new entry
11599 and return a pointer to it.
11600 If TYPE is NULL, determine symbol type from the die, otherwise
11601 used the passed type.
11602 If SPACE is not NULL, use it to hold the new symbol. If it is
11603 NULL, allocate a new symbol on the objfile's obstack. */
11604
11605 static struct symbol *
11606 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11607 struct symbol *space)
11608 {
11609 struct objfile *objfile = cu->objfile;
11610 struct symbol *sym = NULL;
11611 char *name;
11612 struct attribute *attr = NULL;
11613 struct attribute *attr2 = NULL;
11614 CORE_ADDR baseaddr;
11615 struct pending **list_to_add = NULL;
11616
11617 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11618
11619 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11620
11621 name = dwarf2_name (die, cu);
11622 if (name)
11623 {
11624 const char *linkagename;
11625 int suppress_add = 0;
11626
11627 if (space)
11628 sym = space;
11629 else
11630 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11631 OBJSTAT (objfile, n_syms++);
11632
11633 /* Cache this symbol's name and the name's demangled form (if any). */
11634 SYMBOL_SET_LANGUAGE (sym, cu->language);
11635 linkagename = dwarf2_physname (name, die, cu);
11636 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11637
11638 /* Fortran does not have mangling standard and the mangling does differ
11639 between gfortran, iFort etc. */
11640 if (cu->language == language_fortran
11641 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11642 symbol_set_demangled_name (&(sym->ginfo),
11643 (char *) dwarf2_full_name (name, die, cu),
11644 NULL);
11645
11646 /* Default assumptions.
11647 Use the passed type or decode it from the die. */
11648 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11649 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11650 if (type != NULL)
11651 SYMBOL_TYPE (sym) = type;
11652 else
11653 SYMBOL_TYPE (sym) = die_type (die, cu);
11654 attr = dwarf2_attr (die,
11655 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11656 cu);
11657 if (attr)
11658 {
11659 SYMBOL_LINE (sym) = DW_UNSND (attr);
11660 }
11661
11662 attr = dwarf2_attr (die,
11663 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11664 cu);
11665 if (attr)
11666 {
11667 int file_index = DW_UNSND (attr);
11668
11669 if (cu->line_header == NULL
11670 || file_index > cu->line_header->num_file_names)
11671 complaint (&symfile_complaints,
11672 _("file index out of range"));
11673 else if (file_index > 0)
11674 {
11675 struct file_entry *fe;
11676
11677 fe = &cu->line_header->file_names[file_index - 1];
11678 SYMBOL_SYMTAB (sym) = fe->symtab;
11679 }
11680 }
11681
11682 switch (die->tag)
11683 {
11684 case DW_TAG_label:
11685 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11686 if (attr)
11687 {
11688 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11689 }
11690 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11691 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11692 SYMBOL_CLASS (sym) = LOC_LABEL;
11693 add_symbol_to_list (sym, cu->list_in_scope);
11694 break;
11695 case DW_TAG_subprogram:
11696 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11697 finish_block. */
11698 SYMBOL_CLASS (sym) = LOC_BLOCK;
11699 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11700 if ((attr2 && (DW_UNSND (attr2) != 0))
11701 || cu->language == language_ada)
11702 {
11703 /* Subprograms marked external are stored as a global symbol.
11704 Ada subprograms, whether marked external or not, are always
11705 stored as a global symbol, because we want to be able to
11706 access them globally. For instance, we want to be able
11707 to break on a nested subprogram without having to
11708 specify the context. */
11709 list_to_add = &global_symbols;
11710 }
11711 else
11712 {
11713 list_to_add = cu->list_in_scope;
11714 }
11715 break;
11716 case DW_TAG_inlined_subroutine:
11717 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11718 finish_block. */
11719 SYMBOL_CLASS (sym) = LOC_BLOCK;
11720 SYMBOL_INLINED (sym) = 1;
11721 /* Do not add the symbol to any lists. It will be found via
11722 BLOCK_FUNCTION from the blockvector. */
11723 break;
11724 case DW_TAG_template_value_param:
11725 suppress_add = 1;
11726 /* Fall through. */
11727 case DW_TAG_constant:
11728 case DW_TAG_variable:
11729 case DW_TAG_member:
11730 /* Compilation with minimal debug info may result in
11731 variables with missing type entries. Change the
11732 misleading `void' type to something sensible. */
11733 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11734 SYMBOL_TYPE (sym)
11735 = objfile_type (objfile)->nodebug_data_symbol;
11736
11737 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11738 /* In the case of DW_TAG_member, we should only be called for
11739 static const members. */
11740 if (die->tag == DW_TAG_member)
11741 {
11742 /* dwarf2_add_field uses die_is_declaration,
11743 so we do the same. */
11744 gdb_assert (die_is_declaration (die, cu));
11745 gdb_assert (attr);
11746 }
11747 if (attr)
11748 {
11749 dwarf2_const_value (attr, sym, cu);
11750 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11751 if (!suppress_add)
11752 {
11753 if (attr2 && (DW_UNSND (attr2) != 0))
11754 list_to_add = &global_symbols;
11755 else
11756 list_to_add = cu->list_in_scope;
11757 }
11758 break;
11759 }
11760 attr = dwarf2_attr (die, DW_AT_location, cu);
11761 if (attr)
11762 {
11763 var_decode_location (attr, sym, cu);
11764 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11765 if (SYMBOL_CLASS (sym) == LOC_STATIC
11766 && SYMBOL_VALUE_ADDRESS (sym) == 0
11767 && !dwarf2_per_objfile->has_section_at_zero)
11768 {
11769 /* When a static variable is eliminated by the linker,
11770 the corresponding debug information is not stripped
11771 out, but the variable address is set to null;
11772 do not add such variables into symbol table. */
11773 }
11774 else if (attr2 && (DW_UNSND (attr2) != 0))
11775 {
11776 /* Workaround gfortran PR debug/40040 - it uses
11777 DW_AT_location for variables in -fPIC libraries which may
11778 get overriden by other libraries/executable and get
11779 a different address. Resolve it by the minimal symbol
11780 which may come from inferior's executable using copy
11781 relocation. Make this workaround only for gfortran as for
11782 other compilers GDB cannot guess the minimal symbol
11783 Fortran mangling kind. */
11784 if (cu->language == language_fortran && die->parent
11785 && die->parent->tag == DW_TAG_module
11786 && cu->producer
11787 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11788 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11789
11790 /* A variable with DW_AT_external is never static,
11791 but it may be block-scoped. */
11792 list_to_add = (cu->list_in_scope == &file_symbols
11793 ? &global_symbols : cu->list_in_scope);
11794 }
11795 else
11796 list_to_add = cu->list_in_scope;
11797 }
11798 else
11799 {
11800 /* We do not know the address of this symbol.
11801 If it is an external symbol and we have type information
11802 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11803 The address of the variable will then be determined from
11804 the minimal symbol table whenever the variable is
11805 referenced. */
11806 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11807 if (attr2 && (DW_UNSND (attr2) != 0)
11808 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11809 {
11810 /* A variable with DW_AT_external is never static, but it
11811 may be block-scoped. */
11812 list_to_add = (cu->list_in_scope == &file_symbols
11813 ? &global_symbols : cu->list_in_scope);
11814
11815 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11816 }
11817 else if (!die_is_declaration (die, cu))
11818 {
11819 /* Use the default LOC_OPTIMIZED_OUT class. */
11820 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11821 if (!suppress_add)
11822 list_to_add = cu->list_in_scope;
11823 }
11824 }
11825 break;
11826 case DW_TAG_formal_parameter:
11827 /* If we are inside a function, mark this as an argument. If
11828 not, we might be looking at an argument to an inlined function
11829 when we do not have enough information to show inlined frames;
11830 pretend it's a local variable in that case so that the user can
11831 still see it. */
11832 if (context_stack_depth > 0
11833 && context_stack[context_stack_depth - 1].name != NULL)
11834 SYMBOL_IS_ARGUMENT (sym) = 1;
11835 attr = dwarf2_attr (die, DW_AT_location, cu);
11836 if (attr)
11837 {
11838 var_decode_location (attr, sym, cu);
11839 }
11840 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11841 if (attr)
11842 {
11843 dwarf2_const_value (attr, sym, cu);
11844 }
11845
11846 list_to_add = cu->list_in_scope;
11847 break;
11848 case DW_TAG_unspecified_parameters:
11849 /* From varargs functions; gdb doesn't seem to have any
11850 interest in this information, so just ignore it for now.
11851 (FIXME?) */
11852 break;
11853 case DW_TAG_template_type_param:
11854 suppress_add = 1;
11855 /* Fall through. */
11856 case DW_TAG_class_type:
11857 case DW_TAG_interface_type:
11858 case DW_TAG_structure_type:
11859 case DW_TAG_union_type:
11860 case DW_TAG_set_type:
11861 case DW_TAG_enumeration_type:
11862 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11863 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11864
11865 {
11866 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11867 really ever be static objects: otherwise, if you try
11868 to, say, break of a class's method and you're in a file
11869 which doesn't mention that class, it won't work unless
11870 the check for all static symbols in lookup_symbol_aux
11871 saves you. See the OtherFileClass tests in
11872 gdb.c++/namespace.exp. */
11873
11874 if (!suppress_add)
11875 {
11876 list_to_add = (cu->list_in_scope == &file_symbols
11877 && (cu->language == language_cplus
11878 || cu->language == language_java)
11879 ? &global_symbols : cu->list_in_scope);
11880
11881 /* The semantics of C++ state that "struct foo {
11882 ... }" also defines a typedef for "foo". A Java
11883 class declaration also defines a typedef for the
11884 class. */
11885 if (cu->language == language_cplus
11886 || cu->language == language_java
11887 || cu->language == language_ada)
11888 {
11889 /* The symbol's name is already allocated along
11890 with this objfile, so we don't need to
11891 duplicate it for the type. */
11892 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11893 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11894 }
11895 }
11896 }
11897 break;
11898 case DW_TAG_typedef:
11899 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11900 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11901 list_to_add = cu->list_in_scope;
11902 break;
11903 case DW_TAG_base_type:
11904 case DW_TAG_subrange_type:
11905 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11906 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11907 list_to_add = cu->list_in_scope;
11908 break;
11909 case DW_TAG_enumerator:
11910 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11911 if (attr)
11912 {
11913 dwarf2_const_value (attr, sym, cu);
11914 }
11915 {
11916 /* NOTE: carlton/2003-11-10: See comment above in the
11917 DW_TAG_class_type, etc. block. */
11918
11919 list_to_add = (cu->list_in_scope == &file_symbols
11920 && (cu->language == language_cplus
11921 || cu->language == language_java)
11922 ? &global_symbols : cu->list_in_scope);
11923 }
11924 break;
11925 case DW_TAG_namespace:
11926 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11927 list_to_add = &global_symbols;
11928 break;
11929 default:
11930 /* Not a tag we recognize. Hopefully we aren't processing
11931 trash data, but since we must specifically ignore things
11932 we don't recognize, there is nothing else we should do at
11933 this point. */
11934 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11935 dwarf_tag_name (die->tag));
11936 break;
11937 }
11938
11939 if (suppress_add)
11940 {
11941 sym->hash_next = objfile->template_symbols;
11942 objfile->template_symbols = sym;
11943 list_to_add = NULL;
11944 }
11945
11946 if (list_to_add != NULL)
11947 add_symbol_to_list (sym, list_to_add);
11948
11949 /* For the benefit of old versions of GCC, check for anonymous
11950 namespaces based on the demangled name. */
11951 if (!processing_has_namespace_info
11952 && cu->language == language_cplus)
11953 cp_scan_for_anonymous_namespaces (sym, objfile);
11954 }
11955 return (sym);
11956 }
11957
11958 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11959
11960 static struct symbol *
11961 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11962 {
11963 return new_symbol_full (die, type, cu, NULL);
11964 }
11965
11966 /* Given an attr with a DW_FORM_dataN value in host byte order,
11967 zero-extend it as appropriate for the symbol's type. The DWARF
11968 standard (v4) is not entirely clear about the meaning of using
11969 DW_FORM_dataN for a constant with a signed type, where the type is
11970 wider than the data. The conclusion of a discussion on the DWARF
11971 list was that this is unspecified. We choose to always zero-extend
11972 because that is the interpretation long in use by GCC. */
11973
11974 static gdb_byte *
11975 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11976 const char *name, struct obstack *obstack,
11977 struct dwarf2_cu *cu, long *value, int bits)
11978 {
11979 struct objfile *objfile = cu->objfile;
11980 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11981 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11982 LONGEST l = DW_UNSND (attr);
11983
11984 if (bits < sizeof (*value) * 8)
11985 {
11986 l &= ((LONGEST) 1 << bits) - 1;
11987 *value = l;
11988 }
11989 else if (bits == sizeof (*value) * 8)
11990 *value = l;
11991 else
11992 {
11993 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11994 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11995 return bytes;
11996 }
11997
11998 return NULL;
11999 }
12000
12001 /* Read a constant value from an attribute. Either set *VALUE, or if
12002 the value does not fit in *VALUE, set *BYTES - either already
12003 allocated on the objfile obstack, or newly allocated on OBSTACK,
12004 or, set *BATON, if we translated the constant to a location
12005 expression. */
12006
12007 static void
12008 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12009 const char *name, struct obstack *obstack,
12010 struct dwarf2_cu *cu,
12011 long *value, gdb_byte **bytes,
12012 struct dwarf2_locexpr_baton **baton)
12013 {
12014 struct objfile *objfile = cu->objfile;
12015 struct comp_unit_head *cu_header = &cu->header;
12016 struct dwarf_block *blk;
12017 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12018 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12019
12020 *value = 0;
12021 *bytes = NULL;
12022 *baton = NULL;
12023
12024 switch (attr->form)
12025 {
12026 case DW_FORM_addr:
12027 {
12028 gdb_byte *data;
12029
12030 if (TYPE_LENGTH (type) != cu_header->addr_size)
12031 dwarf2_const_value_length_mismatch_complaint (name,
12032 cu_header->addr_size,
12033 TYPE_LENGTH (type));
12034 /* Symbols of this form are reasonably rare, so we just
12035 piggyback on the existing location code rather than writing
12036 a new implementation of symbol_computed_ops. */
12037 *baton = obstack_alloc (&objfile->objfile_obstack,
12038 sizeof (struct dwarf2_locexpr_baton));
12039 (*baton)->per_cu = cu->per_cu;
12040 gdb_assert ((*baton)->per_cu);
12041
12042 (*baton)->size = 2 + cu_header->addr_size;
12043 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12044 (*baton)->data = data;
12045
12046 data[0] = DW_OP_addr;
12047 store_unsigned_integer (&data[1], cu_header->addr_size,
12048 byte_order, DW_ADDR (attr));
12049 data[cu_header->addr_size + 1] = DW_OP_stack_value;
12050 }
12051 break;
12052 case DW_FORM_string:
12053 case DW_FORM_strp:
12054 /* DW_STRING is already allocated on the objfile obstack, point
12055 directly to it. */
12056 *bytes = (gdb_byte *) DW_STRING (attr);
12057 break;
12058 case DW_FORM_block1:
12059 case DW_FORM_block2:
12060 case DW_FORM_block4:
12061 case DW_FORM_block:
12062 case DW_FORM_exprloc:
12063 blk = DW_BLOCK (attr);
12064 if (TYPE_LENGTH (type) != blk->size)
12065 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12066 TYPE_LENGTH (type));
12067 *bytes = blk->data;
12068 break;
12069
12070 /* The DW_AT_const_value attributes are supposed to carry the
12071 symbol's value "represented as it would be on the target
12072 architecture." By the time we get here, it's already been
12073 converted to host endianness, so we just need to sign- or
12074 zero-extend it as appropriate. */
12075 case DW_FORM_data1:
12076 *bytes = dwarf2_const_value_data (attr, type, name,
12077 obstack, cu, value, 8);
12078 break;
12079 case DW_FORM_data2:
12080 *bytes = dwarf2_const_value_data (attr, type, name,
12081 obstack, cu, value, 16);
12082 break;
12083 case DW_FORM_data4:
12084 *bytes = dwarf2_const_value_data (attr, type, name,
12085 obstack, cu, value, 32);
12086 break;
12087 case DW_FORM_data8:
12088 *bytes = dwarf2_const_value_data (attr, type, name,
12089 obstack, cu, value, 64);
12090 break;
12091
12092 case DW_FORM_sdata:
12093 *value = DW_SND (attr);
12094 break;
12095
12096 case DW_FORM_udata:
12097 *value = DW_UNSND (attr);
12098 break;
12099
12100 default:
12101 complaint (&symfile_complaints,
12102 _("unsupported const value attribute form: '%s'"),
12103 dwarf_form_name (attr->form));
12104 *value = 0;
12105 break;
12106 }
12107 }
12108
12109
12110 /* Copy constant value from an attribute to a symbol. */
12111
12112 static void
12113 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12114 struct dwarf2_cu *cu)
12115 {
12116 struct objfile *objfile = cu->objfile;
12117 struct comp_unit_head *cu_header = &cu->header;
12118 long value;
12119 gdb_byte *bytes;
12120 struct dwarf2_locexpr_baton *baton;
12121
12122 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12123 SYMBOL_PRINT_NAME (sym),
12124 &objfile->objfile_obstack, cu,
12125 &value, &bytes, &baton);
12126
12127 if (baton != NULL)
12128 {
12129 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12130 SYMBOL_LOCATION_BATON (sym) = baton;
12131 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12132 }
12133 else if (bytes != NULL)
12134 {
12135 SYMBOL_VALUE_BYTES (sym) = bytes;
12136 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12137 }
12138 else
12139 {
12140 SYMBOL_VALUE (sym) = value;
12141 SYMBOL_CLASS (sym) = LOC_CONST;
12142 }
12143 }
12144
12145 /* Return the type of the die in question using its DW_AT_type attribute. */
12146
12147 static struct type *
12148 die_type (struct die_info *die, struct dwarf2_cu *cu)
12149 {
12150 struct attribute *type_attr;
12151
12152 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12153 if (!type_attr)
12154 {
12155 /* A missing DW_AT_type represents a void type. */
12156 return objfile_type (cu->objfile)->builtin_void;
12157 }
12158
12159 return lookup_die_type (die, type_attr, cu);
12160 }
12161
12162 /* True iff CU's producer generates GNAT Ada auxiliary information
12163 that allows to find parallel types through that information instead
12164 of having to do expensive parallel lookups by type name. */
12165
12166 static int
12167 need_gnat_info (struct dwarf2_cu *cu)
12168 {
12169 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12170 of GNAT produces this auxiliary information, without any indication
12171 that it is produced. Part of enhancing the FSF version of GNAT
12172 to produce that information will be to put in place an indicator
12173 that we can use in order to determine whether the descriptive type
12174 info is available or not. One suggestion that has been made is
12175 to use a new attribute, attached to the CU die. For now, assume
12176 that the descriptive type info is not available. */
12177 return 0;
12178 }
12179
12180 /* Return the auxiliary type of the die in question using its
12181 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12182 attribute is not present. */
12183
12184 static struct type *
12185 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12186 {
12187 struct attribute *type_attr;
12188
12189 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12190 if (!type_attr)
12191 return NULL;
12192
12193 return lookup_die_type (die, type_attr, cu);
12194 }
12195
12196 /* If DIE has a descriptive_type attribute, then set the TYPE's
12197 descriptive type accordingly. */
12198
12199 static void
12200 set_descriptive_type (struct type *type, struct die_info *die,
12201 struct dwarf2_cu *cu)
12202 {
12203 struct type *descriptive_type = die_descriptive_type (die, cu);
12204
12205 if (descriptive_type)
12206 {
12207 ALLOCATE_GNAT_AUX_TYPE (type);
12208 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12209 }
12210 }
12211
12212 /* Return the containing type of the die in question using its
12213 DW_AT_containing_type attribute. */
12214
12215 static struct type *
12216 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12217 {
12218 struct attribute *type_attr;
12219
12220 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12221 if (!type_attr)
12222 error (_("Dwarf Error: Problem turning containing type into gdb type "
12223 "[in module %s]"), cu->objfile->name);
12224
12225 return lookup_die_type (die, type_attr, cu);
12226 }
12227
12228 /* Look up the type of DIE in CU using its type attribute ATTR.
12229 If there is no type substitute an error marker. */
12230
12231 static struct type *
12232 lookup_die_type (struct die_info *die, struct attribute *attr,
12233 struct dwarf2_cu *cu)
12234 {
12235 struct objfile *objfile = cu->objfile;
12236 struct type *this_type;
12237
12238 /* First see if we have it cached. */
12239
12240 if (is_ref_attr (attr))
12241 {
12242 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12243
12244 this_type = get_die_type_at_offset (offset, cu->per_cu);
12245 }
12246 else if (attr->form == DW_FORM_ref_sig8)
12247 {
12248 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12249 struct dwarf2_cu *sig_cu;
12250 unsigned int offset;
12251
12252 /* sig_type will be NULL if the signatured type is missing from
12253 the debug info. */
12254 if (sig_type == NULL)
12255 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12256 "at 0x%x [in module %s]"),
12257 die->offset, objfile->name);
12258
12259 gdb_assert (sig_type->per_cu.debug_types_section);
12260 offset = sig_type->per_cu.offset + sig_type->type_offset;
12261 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12262 }
12263 else
12264 {
12265 dump_die_for_error (die);
12266 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12267 dwarf_attr_name (attr->name), objfile->name);
12268 }
12269
12270 /* If not cached we need to read it in. */
12271
12272 if (this_type == NULL)
12273 {
12274 struct die_info *type_die;
12275 struct dwarf2_cu *type_cu = cu;
12276
12277 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12278 /* If the type is cached, we should have found it above. */
12279 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12280 this_type = read_type_die_1 (type_die, type_cu);
12281 }
12282
12283 /* If we still don't have a type use an error marker. */
12284
12285 if (this_type == NULL)
12286 {
12287 char *message, *saved;
12288
12289 /* read_type_die already issued a complaint. */
12290 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12291 objfile->name,
12292 cu->header.offset,
12293 die->offset);
12294 saved = obstack_copy0 (&objfile->objfile_obstack,
12295 message, strlen (message));
12296 xfree (message);
12297
12298 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12299 }
12300
12301 return this_type;
12302 }
12303
12304 /* Return the type in DIE, CU.
12305 Returns NULL for invalid types.
12306
12307 This first does a lookup in the appropriate type_hash table,
12308 and only reads the die in if necessary.
12309
12310 NOTE: This can be called when reading in partial or full symbols. */
12311
12312 static struct type *
12313 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12314 {
12315 struct type *this_type;
12316
12317 this_type = get_die_type (die, cu);
12318 if (this_type)
12319 return this_type;
12320
12321 return read_type_die_1 (die, cu);
12322 }
12323
12324 /* Read the type in DIE, CU.
12325 Returns NULL for invalid types. */
12326
12327 static struct type *
12328 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12329 {
12330 struct type *this_type = NULL;
12331
12332 switch (die->tag)
12333 {
12334 case DW_TAG_class_type:
12335 case DW_TAG_interface_type:
12336 case DW_TAG_structure_type:
12337 case DW_TAG_union_type:
12338 this_type = read_structure_type (die, cu);
12339 break;
12340 case DW_TAG_enumeration_type:
12341 this_type = read_enumeration_type (die, cu);
12342 break;
12343 case DW_TAG_subprogram:
12344 case DW_TAG_subroutine_type:
12345 case DW_TAG_inlined_subroutine:
12346 this_type = read_subroutine_type (die, cu);
12347 break;
12348 case DW_TAG_array_type:
12349 this_type = read_array_type (die, cu);
12350 break;
12351 case DW_TAG_set_type:
12352 this_type = read_set_type (die, cu);
12353 break;
12354 case DW_TAG_pointer_type:
12355 this_type = read_tag_pointer_type (die, cu);
12356 break;
12357 case DW_TAG_ptr_to_member_type:
12358 this_type = read_tag_ptr_to_member_type (die, cu);
12359 break;
12360 case DW_TAG_reference_type:
12361 this_type = read_tag_reference_type (die, cu);
12362 break;
12363 case DW_TAG_const_type:
12364 this_type = read_tag_const_type (die, cu);
12365 break;
12366 case DW_TAG_volatile_type:
12367 this_type = read_tag_volatile_type (die, cu);
12368 break;
12369 case DW_TAG_string_type:
12370 this_type = read_tag_string_type (die, cu);
12371 break;
12372 case DW_TAG_typedef:
12373 this_type = read_typedef (die, cu);
12374 break;
12375 case DW_TAG_subrange_type:
12376 this_type = read_subrange_type (die, cu);
12377 break;
12378 case DW_TAG_base_type:
12379 this_type = read_base_type (die, cu);
12380 break;
12381 case DW_TAG_unspecified_type:
12382 this_type = read_unspecified_type (die, cu);
12383 break;
12384 case DW_TAG_namespace:
12385 this_type = read_namespace_type (die, cu);
12386 break;
12387 case DW_TAG_module:
12388 this_type = read_module_type (die, cu);
12389 break;
12390 default:
12391 complaint (&symfile_complaints,
12392 _("unexpected tag in read_type_die: '%s'"),
12393 dwarf_tag_name (die->tag));
12394 break;
12395 }
12396
12397 return this_type;
12398 }
12399
12400 /* See if we can figure out if the class lives in a namespace. We do
12401 this by looking for a member function; its demangled name will
12402 contain namespace info, if there is any.
12403 Return the computed name or NULL.
12404 Space for the result is allocated on the objfile's obstack.
12405 This is the full-die version of guess_partial_die_structure_name.
12406 In this case we know DIE has no useful parent. */
12407
12408 static char *
12409 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12410 {
12411 struct die_info *spec_die;
12412 struct dwarf2_cu *spec_cu;
12413 struct die_info *child;
12414
12415 spec_cu = cu;
12416 spec_die = die_specification (die, &spec_cu);
12417 if (spec_die != NULL)
12418 {
12419 die = spec_die;
12420 cu = spec_cu;
12421 }
12422
12423 for (child = die->child;
12424 child != NULL;
12425 child = child->sibling)
12426 {
12427 if (child->tag == DW_TAG_subprogram)
12428 {
12429 struct attribute *attr;
12430
12431 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12432 if (attr == NULL)
12433 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12434 if (attr != NULL)
12435 {
12436 char *actual_name
12437 = language_class_name_from_physname (cu->language_defn,
12438 DW_STRING (attr));
12439 char *name = NULL;
12440
12441 if (actual_name != NULL)
12442 {
12443 char *die_name = dwarf2_name (die, cu);
12444
12445 if (die_name != NULL
12446 && strcmp (die_name, actual_name) != 0)
12447 {
12448 /* Strip off the class name from the full name.
12449 We want the prefix. */
12450 int die_name_len = strlen (die_name);
12451 int actual_name_len = strlen (actual_name);
12452
12453 /* Test for '::' as a sanity check. */
12454 if (actual_name_len > die_name_len + 2
12455 && actual_name[actual_name_len
12456 - die_name_len - 1] == ':')
12457 name =
12458 obsavestring (actual_name,
12459 actual_name_len - die_name_len - 2,
12460 &cu->objfile->objfile_obstack);
12461 }
12462 }
12463 xfree (actual_name);
12464 return name;
12465 }
12466 }
12467 }
12468
12469 return NULL;
12470 }
12471
12472 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12473 prefix part in such case. See
12474 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12475
12476 static char *
12477 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12478 {
12479 struct attribute *attr;
12480 char *base;
12481
12482 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12483 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12484 return NULL;
12485
12486 attr = dwarf2_attr (die, DW_AT_name, cu);
12487 if (attr != NULL && DW_STRING (attr) != NULL)
12488 return NULL;
12489
12490 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12491 if (attr == NULL)
12492 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12493 if (attr == NULL || DW_STRING (attr) == NULL)
12494 return NULL;
12495
12496 /* dwarf2_name had to be already called. */
12497 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12498
12499 /* Strip the base name, keep any leading namespaces/classes. */
12500 base = strrchr (DW_STRING (attr), ':');
12501 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12502 return "";
12503
12504 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12505 &cu->objfile->objfile_obstack);
12506 }
12507
12508 /* Return the name of the namespace/class that DIE is defined within,
12509 or "" if we can't tell. The caller should not xfree the result.
12510
12511 For example, if we're within the method foo() in the following
12512 code:
12513
12514 namespace N {
12515 class C {
12516 void foo () {
12517 }
12518 };
12519 }
12520
12521 then determine_prefix on foo's die will return "N::C". */
12522
12523 static const char *
12524 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12525 {
12526 struct die_info *parent, *spec_die;
12527 struct dwarf2_cu *spec_cu;
12528 struct type *parent_type;
12529 char *retval;
12530
12531 if (cu->language != language_cplus && cu->language != language_java
12532 && cu->language != language_fortran)
12533 return "";
12534
12535 retval = anonymous_struct_prefix (die, cu);
12536 if (retval)
12537 return retval;
12538
12539 /* We have to be careful in the presence of DW_AT_specification.
12540 For example, with GCC 3.4, given the code
12541
12542 namespace N {
12543 void foo() {
12544 // Definition of N::foo.
12545 }
12546 }
12547
12548 then we'll have a tree of DIEs like this:
12549
12550 1: DW_TAG_compile_unit
12551 2: DW_TAG_namespace // N
12552 3: DW_TAG_subprogram // declaration of N::foo
12553 4: DW_TAG_subprogram // definition of N::foo
12554 DW_AT_specification // refers to die #3
12555
12556 Thus, when processing die #4, we have to pretend that we're in
12557 the context of its DW_AT_specification, namely the contex of die
12558 #3. */
12559 spec_cu = cu;
12560 spec_die = die_specification (die, &spec_cu);
12561 if (spec_die == NULL)
12562 parent = die->parent;
12563 else
12564 {
12565 parent = spec_die->parent;
12566 cu = spec_cu;
12567 }
12568
12569 if (parent == NULL)
12570 return "";
12571 else if (parent->building_fullname)
12572 {
12573 const char *name;
12574 const char *parent_name;
12575
12576 /* It has been seen on RealView 2.2 built binaries,
12577 DW_TAG_template_type_param types actually _defined_ as
12578 children of the parent class:
12579
12580 enum E {};
12581 template class <class Enum> Class{};
12582 Class<enum E> class_e;
12583
12584 1: DW_TAG_class_type (Class)
12585 2: DW_TAG_enumeration_type (E)
12586 3: DW_TAG_enumerator (enum1:0)
12587 3: DW_TAG_enumerator (enum2:1)
12588 ...
12589 2: DW_TAG_template_type_param
12590 DW_AT_type DW_FORM_ref_udata (E)
12591
12592 Besides being broken debug info, it can put GDB into an
12593 infinite loop. Consider:
12594
12595 When we're building the full name for Class<E>, we'll start
12596 at Class, and go look over its template type parameters,
12597 finding E. We'll then try to build the full name of E, and
12598 reach here. We're now trying to build the full name of E,
12599 and look over the parent DIE for containing scope. In the
12600 broken case, if we followed the parent DIE of E, we'd again
12601 find Class, and once again go look at its template type
12602 arguments, etc., etc. Simply don't consider such parent die
12603 as source-level parent of this die (it can't be, the language
12604 doesn't allow it), and break the loop here. */
12605 name = dwarf2_name (die, cu);
12606 parent_name = dwarf2_name (parent, cu);
12607 complaint (&symfile_complaints,
12608 _("template param type '%s' defined within parent '%s'"),
12609 name ? name : "<unknown>",
12610 parent_name ? parent_name : "<unknown>");
12611 return "";
12612 }
12613 else
12614 switch (parent->tag)
12615 {
12616 case DW_TAG_namespace:
12617 parent_type = read_type_die (parent, cu);
12618 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12619 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12620 Work around this problem here. */
12621 if (cu->language == language_cplus
12622 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12623 return "";
12624 /* We give a name to even anonymous namespaces. */
12625 return TYPE_TAG_NAME (parent_type);
12626 case DW_TAG_class_type:
12627 case DW_TAG_interface_type:
12628 case DW_TAG_structure_type:
12629 case DW_TAG_union_type:
12630 case DW_TAG_module:
12631 parent_type = read_type_die (parent, cu);
12632 if (TYPE_TAG_NAME (parent_type) != NULL)
12633 return TYPE_TAG_NAME (parent_type);
12634 else
12635 /* An anonymous structure is only allowed non-static data
12636 members; no typedefs, no member functions, et cetera.
12637 So it does not need a prefix. */
12638 return "";
12639 case DW_TAG_compile_unit:
12640 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12641 if (cu->language == language_cplus
12642 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12643 && die->child != NULL
12644 && (die->tag == DW_TAG_class_type
12645 || die->tag == DW_TAG_structure_type
12646 || die->tag == DW_TAG_union_type))
12647 {
12648 char *name = guess_full_die_structure_name (die, cu);
12649 if (name != NULL)
12650 return name;
12651 }
12652 return "";
12653 default:
12654 return determine_prefix (parent, cu);
12655 }
12656 }
12657
12658 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12659 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12660 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12661 an obconcat, otherwise allocate storage for the result. The CU argument is
12662 used to determine the language and hence, the appropriate separator. */
12663
12664 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12665
12666 static char *
12667 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12668 int physname, struct dwarf2_cu *cu)
12669 {
12670 const char *lead = "";
12671 const char *sep;
12672
12673 if (suffix == NULL || suffix[0] == '\0'
12674 || prefix == NULL || prefix[0] == '\0')
12675 sep = "";
12676 else if (cu->language == language_java)
12677 sep = ".";
12678 else if (cu->language == language_fortran && physname)
12679 {
12680 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12681 DW_AT_MIPS_linkage_name is preferred and used instead. */
12682
12683 lead = "__";
12684 sep = "_MOD_";
12685 }
12686 else
12687 sep = "::";
12688
12689 if (prefix == NULL)
12690 prefix = "";
12691 if (suffix == NULL)
12692 suffix = "";
12693
12694 if (obs == NULL)
12695 {
12696 char *retval
12697 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12698
12699 strcpy (retval, lead);
12700 strcat (retval, prefix);
12701 strcat (retval, sep);
12702 strcat (retval, suffix);
12703 return retval;
12704 }
12705 else
12706 {
12707 /* We have an obstack. */
12708 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12709 }
12710 }
12711
12712 /* Return sibling of die, NULL if no sibling. */
12713
12714 static struct die_info *
12715 sibling_die (struct die_info *die)
12716 {
12717 return die->sibling;
12718 }
12719
12720 /* Get name of a die, return NULL if not found. */
12721
12722 static char *
12723 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12724 struct obstack *obstack)
12725 {
12726 if (name && cu->language == language_cplus)
12727 {
12728 char *canon_name = cp_canonicalize_string (name);
12729
12730 if (canon_name != NULL)
12731 {
12732 if (strcmp (canon_name, name) != 0)
12733 name = obsavestring (canon_name, strlen (canon_name),
12734 obstack);
12735 xfree (canon_name);
12736 }
12737 }
12738
12739 return name;
12740 }
12741
12742 /* Get name of a die, return NULL if not found. */
12743
12744 static char *
12745 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12746 {
12747 struct attribute *attr;
12748
12749 attr = dwarf2_attr (die, DW_AT_name, cu);
12750 if ((!attr || !DW_STRING (attr))
12751 && die->tag != DW_TAG_class_type
12752 && die->tag != DW_TAG_interface_type
12753 && die->tag != DW_TAG_structure_type
12754 && die->tag != DW_TAG_union_type)
12755 return NULL;
12756
12757 switch (die->tag)
12758 {
12759 case DW_TAG_compile_unit:
12760 /* Compilation units have a DW_AT_name that is a filename, not
12761 a source language identifier. */
12762 case DW_TAG_enumeration_type:
12763 case DW_TAG_enumerator:
12764 /* These tags always have simple identifiers already; no need
12765 to canonicalize them. */
12766 return DW_STRING (attr);
12767
12768 case DW_TAG_subprogram:
12769 /* Java constructors will all be named "<init>", so return
12770 the class name when we see this special case. */
12771 if (cu->language == language_java
12772 && DW_STRING (attr) != NULL
12773 && strcmp (DW_STRING (attr), "<init>") == 0)
12774 {
12775 struct dwarf2_cu *spec_cu = cu;
12776 struct die_info *spec_die;
12777
12778 /* GCJ will output '<init>' for Java constructor names.
12779 For this special case, return the name of the parent class. */
12780
12781 /* GCJ may output suprogram DIEs with AT_specification set.
12782 If so, use the name of the specified DIE. */
12783 spec_die = die_specification (die, &spec_cu);
12784 if (spec_die != NULL)
12785 return dwarf2_name (spec_die, spec_cu);
12786
12787 do
12788 {
12789 die = die->parent;
12790 if (die->tag == DW_TAG_class_type)
12791 return dwarf2_name (die, cu);
12792 }
12793 while (die->tag != DW_TAG_compile_unit);
12794 }
12795 break;
12796
12797 case DW_TAG_class_type:
12798 case DW_TAG_interface_type:
12799 case DW_TAG_structure_type:
12800 case DW_TAG_union_type:
12801 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12802 structures or unions. These were of the form "._%d" in GCC 4.1,
12803 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12804 and GCC 4.4. We work around this problem by ignoring these. */
12805 if (attr && DW_STRING (attr)
12806 && (strncmp (DW_STRING (attr), "._", 2) == 0
12807 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12808 return NULL;
12809
12810 /* GCC might emit a nameless typedef that has a linkage name. See
12811 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12812 if (!attr || DW_STRING (attr) == NULL)
12813 {
12814 char *demangled = NULL;
12815
12816 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12817 if (attr == NULL)
12818 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12819
12820 if (attr == NULL || DW_STRING (attr) == NULL)
12821 return NULL;
12822
12823 /* Avoid demangling DW_STRING (attr) the second time on a second
12824 call for the same DIE. */
12825 if (!DW_STRING_IS_CANONICAL (attr))
12826 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12827
12828 if (demangled)
12829 {
12830 char *base;
12831
12832 /* FIXME: we already did this for the partial symbol... */
12833 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12834 &cu->objfile->objfile_obstack);
12835 DW_STRING_IS_CANONICAL (attr) = 1;
12836 xfree (demangled);
12837
12838 /* Strip any leading namespaces/classes, keep only the base name.
12839 DW_AT_name for named DIEs does not contain the prefixes. */
12840 base = strrchr (DW_STRING (attr), ':');
12841 if (base && base > DW_STRING (attr) && base[-1] == ':')
12842 return &base[1];
12843 else
12844 return DW_STRING (attr);
12845 }
12846 }
12847 break;
12848
12849 default:
12850 break;
12851 }
12852
12853 if (!DW_STRING_IS_CANONICAL (attr))
12854 {
12855 DW_STRING (attr)
12856 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12857 &cu->objfile->objfile_obstack);
12858 DW_STRING_IS_CANONICAL (attr) = 1;
12859 }
12860 return DW_STRING (attr);
12861 }
12862
12863 /* Return the die that this die in an extension of, or NULL if there
12864 is none. *EXT_CU is the CU containing DIE on input, and the CU
12865 containing the return value on output. */
12866
12867 static struct die_info *
12868 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12869 {
12870 struct attribute *attr;
12871
12872 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12873 if (attr == NULL)
12874 return NULL;
12875
12876 return follow_die_ref (die, attr, ext_cu);
12877 }
12878
12879 /* Convert a DIE tag into its string name. */
12880
12881 static char *
12882 dwarf_tag_name (unsigned tag)
12883 {
12884 switch (tag)
12885 {
12886 case DW_TAG_padding:
12887 return "DW_TAG_padding";
12888 case DW_TAG_array_type:
12889 return "DW_TAG_array_type";
12890 case DW_TAG_class_type:
12891 return "DW_TAG_class_type";
12892 case DW_TAG_entry_point:
12893 return "DW_TAG_entry_point";
12894 case DW_TAG_enumeration_type:
12895 return "DW_TAG_enumeration_type";
12896 case DW_TAG_formal_parameter:
12897 return "DW_TAG_formal_parameter";
12898 case DW_TAG_imported_declaration:
12899 return "DW_TAG_imported_declaration";
12900 case DW_TAG_label:
12901 return "DW_TAG_label";
12902 case DW_TAG_lexical_block:
12903 return "DW_TAG_lexical_block";
12904 case DW_TAG_member:
12905 return "DW_TAG_member";
12906 case DW_TAG_pointer_type:
12907 return "DW_TAG_pointer_type";
12908 case DW_TAG_reference_type:
12909 return "DW_TAG_reference_type";
12910 case DW_TAG_compile_unit:
12911 return "DW_TAG_compile_unit";
12912 case DW_TAG_string_type:
12913 return "DW_TAG_string_type";
12914 case DW_TAG_structure_type:
12915 return "DW_TAG_structure_type";
12916 case DW_TAG_subroutine_type:
12917 return "DW_TAG_subroutine_type";
12918 case DW_TAG_typedef:
12919 return "DW_TAG_typedef";
12920 case DW_TAG_union_type:
12921 return "DW_TAG_union_type";
12922 case DW_TAG_unspecified_parameters:
12923 return "DW_TAG_unspecified_parameters";
12924 case DW_TAG_variant:
12925 return "DW_TAG_variant";
12926 case DW_TAG_common_block:
12927 return "DW_TAG_common_block";
12928 case DW_TAG_common_inclusion:
12929 return "DW_TAG_common_inclusion";
12930 case DW_TAG_inheritance:
12931 return "DW_TAG_inheritance";
12932 case DW_TAG_inlined_subroutine:
12933 return "DW_TAG_inlined_subroutine";
12934 case DW_TAG_module:
12935 return "DW_TAG_module";
12936 case DW_TAG_ptr_to_member_type:
12937 return "DW_TAG_ptr_to_member_type";
12938 case DW_TAG_set_type:
12939 return "DW_TAG_set_type";
12940 case DW_TAG_subrange_type:
12941 return "DW_TAG_subrange_type";
12942 case DW_TAG_with_stmt:
12943 return "DW_TAG_with_stmt";
12944 case DW_TAG_access_declaration:
12945 return "DW_TAG_access_declaration";
12946 case DW_TAG_base_type:
12947 return "DW_TAG_base_type";
12948 case DW_TAG_catch_block:
12949 return "DW_TAG_catch_block";
12950 case DW_TAG_const_type:
12951 return "DW_TAG_const_type";
12952 case DW_TAG_constant:
12953 return "DW_TAG_constant";
12954 case DW_TAG_enumerator:
12955 return "DW_TAG_enumerator";
12956 case DW_TAG_file_type:
12957 return "DW_TAG_file_type";
12958 case DW_TAG_friend:
12959 return "DW_TAG_friend";
12960 case DW_TAG_namelist:
12961 return "DW_TAG_namelist";
12962 case DW_TAG_namelist_item:
12963 return "DW_TAG_namelist_item";
12964 case DW_TAG_packed_type:
12965 return "DW_TAG_packed_type";
12966 case DW_TAG_subprogram:
12967 return "DW_TAG_subprogram";
12968 case DW_TAG_template_type_param:
12969 return "DW_TAG_template_type_param";
12970 case DW_TAG_template_value_param:
12971 return "DW_TAG_template_value_param";
12972 case DW_TAG_thrown_type:
12973 return "DW_TAG_thrown_type";
12974 case DW_TAG_try_block:
12975 return "DW_TAG_try_block";
12976 case DW_TAG_variant_part:
12977 return "DW_TAG_variant_part";
12978 case DW_TAG_variable:
12979 return "DW_TAG_variable";
12980 case DW_TAG_volatile_type:
12981 return "DW_TAG_volatile_type";
12982 case DW_TAG_dwarf_procedure:
12983 return "DW_TAG_dwarf_procedure";
12984 case DW_TAG_restrict_type:
12985 return "DW_TAG_restrict_type";
12986 case DW_TAG_interface_type:
12987 return "DW_TAG_interface_type";
12988 case DW_TAG_namespace:
12989 return "DW_TAG_namespace";
12990 case DW_TAG_imported_module:
12991 return "DW_TAG_imported_module";
12992 case DW_TAG_unspecified_type:
12993 return "DW_TAG_unspecified_type";
12994 case DW_TAG_partial_unit:
12995 return "DW_TAG_partial_unit";
12996 case DW_TAG_imported_unit:
12997 return "DW_TAG_imported_unit";
12998 case DW_TAG_condition:
12999 return "DW_TAG_condition";
13000 case DW_TAG_shared_type:
13001 return "DW_TAG_shared_type";
13002 case DW_TAG_type_unit:
13003 return "DW_TAG_type_unit";
13004 case DW_TAG_MIPS_loop:
13005 return "DW_TAG_MIPS_loop";
13006 case DW_TAG_HP_array_descriptor:
13007 return "DW_TAG_HP_array_descriptor";
13008 case DW_TAG_format_label:
13009 return "DW_TAG_format_label";
13010 case DW_TAG_function_template:
13011 return "DW_TAG_function_template";
13012 case DW_TAG_class_template:
13013 return "DW_TAG_class_template";
13014 case DW_TAG_GNU_BINCL:
13015 return "DW_TAG_GNU_BINCL";
13016 case DW_TAG_GNU_EINCL:
13017 return "DW_TAG_GNU_EINCL";
13018 case DW_TAG_upc_shared_type:
13019 return "DW_TAG_upc_shared_type";
13020 case DW_TAG_upc_strict_type:
13021 return "DW_TAG_upc_strict_type";
13022 case DW_TAG_upc_relaxed_type:
13023 return "DW_TAG_upc_relaxed_type";
13024 case DW_TAG_PGI_kanji_type:
13025 return "DW_TAG_PGI_kanji_type";
13026 case DW_TAG_PGI_interface_block:
13027 return "DW_TAG_PGI_interface_block";
13028 case DW_TAG_GNU_call_site:
13029 return "DW_TAG_GNU_call_site";
13030 default:
13031 return "DW_TAG_<unknown>";
13032 }
13033 }
13034
13035 /* Convert a DWARF attribute code into its string name. */
13036
13037 static char *
13038 dwarf_attr_name (unsigned attr)
13039 {
13040 switch (attr)
13041 {
13042 case DW_AT_sibling:
13043 return "DW_AT_sibling";
13044 case DW_AT_location:
13045 return "DW_AT_location";
13046 case DW_AT_name:
13047 return "DW_AT_name";
13048 case DW_AT_ordering:
13049 return "DW_AT_ordering";
13050 case DW_AT_subscr_data:
13051 return "DW_AT_subscr_data";
13052 case DW_AT_byte_size:
13053 return "DW_AT_byte_size";
13054 case DW_AT_bit_offset:
13055 return "DW_AT_bit_offset";
13056 case DW_AT_bit_size:
13057 return "DW_AT_bit_size";
13058 case DW_AT_element_list:
13059 return "DW_AT_element_list";
13060 case DW_AT_stmt_list:
13061 return "DW_AT_stmt_list";
13062 case DW_AT_low_pc:
13063 return "DW_AT_low_pc";
13064 case DW_AT_high_pc:
13065 return "DW_AT_high_pc";
13066 case DW_AT_language:
13067 return "DW_AT_language";
13068 case DW_AT_member:
13069 return "DW_AT_member";
13070 case DW_AT_discr:
13071 return "DW_AT_discr";
13072 case DW_AT_discr_value:
13073 return "DW_AT_discr_value";
13074 case DW_AT_visibility:
13075 return "DW_AT_visibility";
13076 case DW_AT_import:
13077 return "DW_AT_import";
13078 case DW_AT_string_length:
13079 return "DW_AT_string_length";
13080 case DW_AT_common_reference:
13081 return "DW_AT_common_reference";
13082 case DW_AT_comp_dir:
13083 return "DW_AT_comp_dir";
13084 case DW_AT_const_value:
13085 return "DW_AT_const_value";
13086 case DW_AT_containing_type:
13087 return "DW_AT_containing_type";
13088 case DW_AT_default_value:
13089 return "DW_AT_default_value";
13090 case DW_AT_inline:
13091 return "DW_AT_inline";
13092 case DW_AT_is_optional:
13093 return "DW_AT_is_optional";
13094 case DW_AT_lower_bound:
13095 return "DW_AT_lower_bound";
13096 case DW_AT_producer:
13097 return "DW_AT_producer";
13098 case DW_AT_prototyped:
13099 return "DW_AT_prototyped";
13100 case DW_AT_return_addr:
13101 return "DW_AT_return_addr";
13102 case DW_AT_start_scope:
13103 return "DW_AT_start_scope";
13104 case DW_AT_bit_stride:
13105 return "DW_AT_bit_stride";
13106 case DW_AT_upper_bound:
13107 return "DW_AT_upper_bound";
13108 case DW_AT_abstract_origin:
13109 return "DW_AT_abstract_origin";
13110 case DW_AT_accessibility:
13111 return "DW_AT_accessibility";
13112 case DW_AT_address_class:
13113 return "DW_AT_address_class";
13114 case DW_AT_artificial:
13115 return "DW_AT_artificial";
13116 case DW_AT_base_types:
13117 return "DW_AT_base_types";
13118 case DW_AT_calling_convention:
13119 return "DW_AT_calling_convention";
13120 case DW_AT_count:
13121 return "DW_AT_count";
13122 case DW_AT_data_member_location:
13123 return "DW_AT_data_member_location";
13124 case DW_AT_decl_column:
13125 return "DW_AT_decl_column";
13126 case DW_AT_decl_file:
13127 return "DW_AT_decl_file";
13128 case DW_AT_decl_line:
13129 return "DW_AT_decl_line";
13130 case DW_AT_declaration:
13131 return "DW_AT_declaration";
13132 case DW_AT_discr_list:
13133 return "DW_AT_discr_list";
13134 case DW_AT_encoding:
13135 return "DW_AT_encoding";
13136 case DW_AT_external:
13137 return "DW_AT_external";
13138 case DW_AT_frame_base:
13139 return "DW_AT_frame_base";
13140 case DW_AT_friend:
13141 return "DW_AT_friend";
13142 case DW_AT_identifier_case:
13143 return "DW_AT_identifier_case";
13144 case DW_AT_macro_info:
13145 return "DW_AT_macro_info";
13146 case DW_AT_namelist_items:
13147 return "DW_AT_namelist_items";
13148 case DW_AT_priority:
13149 return "DW_AT_priority";
13150 case DW_AT_segment:
13151 return "DW_AT_segment";
13152 case DW_AT_specification:
13153 return "DW_AT_specification";
13154 case DW_AT_static_link:
13155 return "DW_AT_static_link";
13156 case DW_AT_type:
13157 return "DW_AT_type";
13158 case DW_AT_use_location:
13159 return "DW_AT_use_location";
13160 case DW_AT_variable_parameter:
13161 return "DW_AT_variable_parameter";
13162 case DW_AT_virtuality:
13163 return "DW_AT_virtuality";
13164 case DW_AT_vtable_elem_location:
13165 return "DW_AT_vtable_elem_location";
13166 /* DWARF 3 values. */
13167 case DW_AT_allocated:
13168 return "DW_AT_allocated";
13169 case DW_AT_associated:
13170 return "DW_AT_associated";
13171 case DW_AT_data_location:
13172 return "DW_AT_data_location";
13173 case DW_AT_byte_stride:
13174 return "DW_AT_byte_stride";
13175 case DW_AT_entry_pc:
13176 return "DW_AT_entry_pc";
13177 case DW_AT_use_UTF8:
13178 return "DW_AT_use_UTF8";
13179 case DW_AT_extension:
13180 return "DW_AT_extension";
13181 case DW_AT_ranges:
13182 return "DW_AT_ranges";
13183 case DW_AT_trampoline:
13184 return "DW_AT_trampoline";
13185 case DW_AT_call_column:
13186 return "DW_AT_call_column";
13187 case DW_AT_call_file:
13188 return "DW_AT_call_file";
13189 case DW_AT_call_line:
13190 return "DW_AT_call_line";
13191 case DW_AT_description:
13192 return "DW_AT_description";
13193 case DW_AT_binary_scale:
13194 return "DW_AT_binary_scale";
13195 case DW_AT_decimal_scale:
13196 return "DW_AT_decimal_scale";
13197 case DW_AT_small:
13198 return "DW_AT_small";
13199 case DW_AT_decimal_sign:
13200 return "DW_AT_decimal_sign";
13201 case DW_AT_digit_count:
13202 return "DW_AT_digit_count";
13203 case DW_AT_picture_string:
13204 return "DW_AT_picture_string";
13205 case DW_AT_mutable:
13206 return "DW_AT_mutable";
13207 case DW_AT_threads_scaled:
13208 return "DW_AT_threads_scaled";
13209 case DW_AT_explicit:
13210 return "DW_AT_explicit";
13211 case DW_AT_object_pointer:
13212 return "DW_AT_object_pointer";
13213 case DW_AT_endianity:
13214 return "DW_AT_endianity";
13215 case DW_AT_elemental:
13216 return "DW_AT_elemental";
13217 case DW_AT_pure:
13218 return "DW_AT_pure";
13219 case DW_AT_recursive:
13220 return "DW_AT_recursive";
13221 /* DWARF 4 values. */
13222 case DW_AT_signature:
13223 return "DW_AT_signature";
13224 case DW_AT_linkage_name:
13225 return "DW_AT_linkage_name";
13226 /* SGI/MIPS extensions. */
13227 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13228 case DW_AT_MIPS_fde:
13229 return "DW_AT_MIPS_fde";
13230 #endif
13231 case DW_AT_MIPS_loop_begin:
13232 return "DW_AT_MIPS_loop_begin";
13233 case DW_AT_MIPS_tail_loop_begin:
13234 return "DW_AT_MIPS_tail_loop_begin";
13235 case DW_AT_MIPS_epilog_begin:
13236 return "DW_AT_MIPS_epilog_begin";
13237 case DW_AT_MIPS_loop_unroll_factor:
13238 return "DW_AT_MIPS_loop_unroll_factor";
13239 case DW_AT_MIPS_software_pipeline_depth:
13240 return "DW_AT_MIPS_software_pipeline_depth";
13241 case DW_AT_MIPS_linkage_name:
13242 return "DW_AT_MIPS_linkage_name";
13243 case DW_AT_MIPS_stride:
13244 return "DW_AT_MIPS_stride";
13245 case DW_AT_MIPS_abstract_name:
13246 return "DW_AT_MIPS_abstract_name";
13247 case DW_AT_MIPS_clone_origin:
13248 return "DW_AT_MIPS_clone_origin";
13249 case DW_AT_MIPS_has_inlines:
13250 return "DW_AT_MIPS_has_inlines";
13251 /* HP extensions. */
13252 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13253 case DW_AT_HP_block_index:
13254 return "DW_AT_HP_block_index";
13255 #endif
13256 case DW_AT_HP_unmodifiable:
13257 return "DW_AT_HP_unmodifiable";
13258 case DW_AT_HP_actuals_stmt_list:
13259 return "DW_AT_HP_actuals_stmt_list";
13260 case DW_AT_HP_proc_per_section:
13261 return "DW_AT_HP_proc_per_section";
13262 case DW_AT_HP_raw_data_ptr:
13263 return "DW_AT_HP_raw_data_ptr";
13264 case DW_AT_HP_pass_by_reference:
13265 return "DW_AT_HP_pass_by_reference";
13266 case DW_AT_HP_opt_level:
13267 return "DW_AT_HP_opt_level";
13268 case DW_AT_HP_prof_version_id:
13269 return "DW_AT_HP_prof_version_id";
13270 case DW_AT_HP_opt_flags:
13271 return "DW_AT_HP_opt_flags";
13272 case DW_AT_HP_cold_region_low_pc:
13273 return "DW_AT_HP_cold_region_low_pc";
13274 case DW_AT_HP_cold_region_high_pc:
13275 return "DW_AT_HP_cold_region_high_pc";
13276 case DW_AT_HP_all_variables_modifiable:
13277 return "DW_AT_HP_all_variables_modifiable";
13278 case DW_AT_HP_linkage_name:
13279 return "DW_AT_HP_linkage_name";
13280 case DW_AT_HP_prof_flags:
13281 return "DW_AT_HP_prof_flags";
13282 /* GNU extensions. */
13283 case DW_AT_sf_names:
13284 return "DW_AT_sf_names";
13285 case DW_AT_src_info:
13286 return "DW_AT_src_info";
13287 case DW_AT_mac_info:
13288 return "DW_AT_mac_info";
13289 case DW_AT_src_coords:
13290 return "DW_AT_src_coords";
13291 case DW_AT_body_begin:
13292 return "DW_AT_body_begin";
13293 case DW_AT_body_end:
13294 return "DW_AT_body_end";
13295 case DW_AT_GNU_vector:
13296 return "DW_AT_GNU_vector";
13297 case DW_AT_GNU_odr_signature:
13298 return "DW_AT_GNU_odr_signature";
13299 /* VMS extensions. */
13300 case DW_AT_VMS_rtnbeg_pd_address:
13301 return "DW_AT_VMS_rtnbeg_pd_address";
13302 /* UPC extension. */
13303 case DW_AT_upc_threads_scaled:
13304 return "DW_AT_upc_threads_scaled";
13305 /* PGI (STMicroelectronics) extensions. */
13306 case DW_AT_PGI_lbase:
13307 return "DW_AT_PGI_lbase";
13308 case DW_AT_PGI_soffset:
13309 return "DW_AT_PGI_soffset";
13310 case DW_AT_PGI_lstride:
13311 return "DW_AT_PGI_lstride";
13312 default:
13313 return "DW_AT_<unknown>";
13314 }
13315 }
13316
13317 /* Convert a DWARF value form code into its string name. */
13318
13319 static char *
13320 dwarf_form_name (unsigned form)
13321 {
13322 switch (form)
13323 {
13324 case DW_FORM_addr:
13325 return "DW_FORM_addr";
13326 case DW_FORM_block2:
13327 return "DW_FORM_block2";
13328 case DW_FORM_block4:
13329 return "DW_FORM_block4";
13330 case DW_FORM_data2:
13331 return "DW_FORM_data2";
13332 case DW_FORM_data4:
13333 return "DW_FORM_data4";
13334 case DW_FORM_data8:
13335 return "DW_FORM_data8";
13336 case DW_FORM_string:
13337 return "DW_FORM_string";
13338 case DW_FORM_block:
13339 return "DW_FORM_block";
13340 case DW_FORM_block1:
13341 return "DW_FORM_block1";
13342 case DW_FORM_data1:
13343 return "DW_FORM_data1";
13344 case DW_FORM_flag:
13345 return "DW_FORM_flag";
13346 case DW_FORM_sdata:
13347 return "DW_FORM_sdata";
13348 case DW_FORM_strp:
13349 return "DW_FORM_strp";
13350 case DW_FORM_udata:
13351 return "DW_FORM_udata";
13352 case DW_FORM_ref_addr:
13353 return "DW_FORM_ref_addr";
13354 case DW_FORM_ref1:
13355 return "DW_FORM_ref1";
13356 case DW_FORM_ref2:
13357 return "DW_FORM_ref2";
13358 case DW_FORM_ref4:
13359 return "DW_FORM_ref4";
13360 case DW_FORM_ref8:
13361 return "DW_FORM_ref8";
13362 case DW_FORM_ref_udata:
13363 return "DW_FORM_ref_udata";
13364 case DW_FORM_indirect:
13365 return "DW_FORM_indirect";
13366 case DW_FORM_sec_offset:
13367 return "DW_FORM_sec_offset";
13368 case DW_FORM_exprloc:
13369 return "DW_FORM_exprloc";
13370 case DW_FORM_flag_present:
13371 return "DW_FORM_flag_present";
13372 case DW_FORM_ref_sig8:
13373 return "DW_FORM_ref_sig8";
13374 default:
13375 return "DW_FORM_<unknown>";
13376 }
13377 }
13378
13379 /* Convert a DWARF stack opcode into its string name. */
13380
13381 const char *
13382 dwarf_stack_op_name (unsigned op)
13383 {
13384 switch (op)
13385 {
13386 case DW_OP_addr:
13387 return "DW_OP_addr";
13388 case DW_OP_deref:
13389 return "DW_OP_deref";
13390 case DW_OP_const1u:
13391 return "DW_OP_const1u";
13392 case DW_OP_const1s:
13393 return "DW_OP_const1s";
13394 case DW_OP_const2u:
13395 return "DW_OP_const2u";
13396 case DW_OP_const2s:
13397 return "DW_OP_const2s";
13398 case DW_OP_const4u:
13399 return "DW_OP_const4u";
13400 case DW_OP_const4s:
13401 return "DW_OP_const4s";
13402 case DW_OP_const8u:
13403 return "DW_OP_const8u";
13404 case DW_OP_const8s:
13405 return "DW_OP_const8s";
13406 case DW_OP_constu:
13407 return "DW_OP_constu";
13408 case DW_OP_consts:
13409 return "DW_OP_consts";
13410 case DW_OP_dup:
13411 return "DW_OP_dup";
13412 case DW_OP_drop:
13413 return "DW_OP_drop";
13414 case DW_OP_over:
13415 return "DW_OP_over";
13416 case DW_OP_pick:
13417 return "DW_OP_pick";
13418 case DW_OP_swap:
13419 return "DW_OP_swap";
13420 case DW_OP_rot:
13421 return "DW_OP_rot";
13422 case DW_OP_xderef:
13423 return "DW_OP_xderef";
13424 case DW_OP_abs:
13425 return "DW_OP_abs";
13426 case DW_OP_and:
13427 return "DW_OP_and";
13428 case DW_OP_div:
13429 return "DW_OP_div";
13430 case DW_OP_minus:
13431 return "DW_OP_minus";
13432 case DW_OP_mod:
13433 return "DW_OP_mod";
13434 case DW_OP_mul:
13435 return "DW_OP_mul";
13436 case DW_OP_neg:
13437 return "DW_OP_neg";
13438 case DW_OP_not:
13439 return "DW_OP_not";
13440 case DW_OP_or:
13441 return "DW_OP_or";
13442 case DW_OP_plus:
13443 return "DW_OP_plus";
13444 case DW_OP_plus_uconst:
13445 return "DW_OP_plus_uconst";
13446 case DW_OP_shl:
13447 return "DW_OP_shl";
13448 case DW_OP_shr:
13449 return "DW_OP_shr";
13450 case DW_OP_shra:
13451 return "DW_OP_shra";
13452 case DW_OP_xor:
13453 return "DW_OP_xor";
13454 case DW_OP_bra:
13455 return "DW_OP_bra";
13456 case DW_OP_eq:
13457 return "DW_OP_eq";
13458 case DW_OP_ge:
13459 return "DW_OP_ge";
13460 case DW_OP_gt:
13461 return "DW_OP_gt";
13462 case DW_OP_le:
13463 return "DW_OP_le";
13464 case DW_OP_lt:
13465 return "DW_OP_lt";
13466 case DW_OP_ne:
13467 return "DW_OP_ne";
13468 case DW_OP_skip:
13469 return "DW_OP_skip";
13470 case DW_OP_lit0:
13471 return "DW_OP_lit0";
13472 case DW_OP_lit1:
13473 return "DW_OP_lit1";
13474 case DW_OP_lit2:
13475 return "DW_OP_lit2";
13476 case DW_OP_lit3:
13477 return "DW_OP_lit3";
13478 case DW_OP_lit4:
13479 return "DW_OP_lit4";
13480 case DW_OP_lit5:
13481 return "DW_OP_lit5";
13482 case DW_OP_lit6:
13483 return "DW_OP_lit6";
13484 case DW_OP_lit7:
13485 return "DW_OP_lit7";
13486 case DW_OP_lit8:
13487 return "DW_OP_lit8";
13488 case DW_OP_lit9:
13489 return "DW_OP_lit9";
13490 case DW_OP_lit10:
13491 return "DW_OP_lit10";
13492 case DW_OP_lit11:
13493 return "DW_OP_lit11";
13494 case DW_OP_lit12:
13495 return "DW_OP_lit12";
13496 case DW_OP_lit13:
13497 return "DW_OP_lit13";
13498 case DW_OP_lit14:
13499 return "DW_OP_lit14";
13500 case DW_OP_lit15:
13501 return "DW_OP_lit15";
13502 case DW_OP_lit16:
13503 return "DW_OP_lit16";
13504 case DW_OP_lit17:
13505 return "DW_OP_lit17";
13506 case DW_OP_lit18:
13507 return "DW_OP_lit18";
13508 case DW_OP_lit19:
13509 return "DW_OP_lit19";
13510 case DW_OP_lit20:
13511 return "DW_OP_lit20";
13512 case DW_OP_lit21:
13513 return "DW_OP_lit21";
13514 case DW_OP_lit22:
13515 return "DW_OP_lit22";
13516 case DW_OP_lit23:
13517 return "DW_OP_lit23";
13518 case DW_OP_lit24:
13519 return "DW_OP_lit24";
13520 case DW_OP_lit25:
13521 return "DW_OP_lit25";
13522 case DW_OP_lit26:
13523 return "DW_OP_lit26";
13524 case DW_OP_lit27:
13525 return "DW_OP_lit27";
13526 case DW_OP_lit28:
13527 return "DW_OP_lit28";
13528 case DW_OP_lit29:
13529 return "DW_OP_lit29";
13530 case DW_OP_lit30:
13531 return "DW_OP_lit30";
13532 case DW_OP_lit31:
13533 return "DW_OP_lit31";
13534 case DW_OP_reg0:
13535 return "DW_OP_reg0";
13536 case DW_OP_reg1:
13537 return "DW_OP_reg1";
13538 case DW_OP_reg2:
13539 return "DW_OP_reg2";
13540 case DW_OP_reg3:
13541 return "DW_OP_reg3";
13542 case DW_OP_reg4:
13543 return "DW_OP_reg4";
13544 case DW_OP_reg5:
13545 return "DW_OP_reg5";
13546 case DW_OP_reg6:
13547 return "DW_OP_reg6";
13548 case DW_OP_reg7:
13549 return "DW_OP_reg7";
13550 case DW_OP_reg8:
13551 return "DW_OP_reg8";
13552 case DW_OP_reg9:
13553 return "DW_OP_reg9";
13554 case DW_OP_reg10:
13555 return "DW_OP_reg10";
13556 case DW_OP_reg11:
13557 return "DW_OP_reg11";
13558 case DW_OP_reg12:
13559 return "DW_OP_reg12";
13560 case DW_OP_reg13:
13561 return "DW_OP_reg13";
13562 case DW_OP_reg14:
13563 return "DW_OP_reg14";
13564 case DW_OP_reg15:
13565 return "DW_OP_reg15";
13566 case DW_OP_reg16:
13567 return "DW_OP_reg16";
13568 case DW_OP_reg17:
13569 return "DW_OP_reg17";
13570 case DW_OP_reg18:
13571 return "DW_OP_reg18";
13572 case DW_OP_reg19:
13573 return "DW_OP_reg19";
13574 case DW_OP_reg20:
13575 return "DW_OP_reg20";
13576 case DW_OP_reg21:
13577 return "DW_OP_reg21";
13578 case DW_OP_reg22:
13579 return "DW_OP_reg22";
13580 case DW_OP_reg23:
13581 return "DW_OP_reg23";
13582 case DW_OP_reg24:
13583 return "DW_OP_reg24";
13584 case DW_OP_reg25:
13585 return "DW_OP_reg25";
13586 case DW_OP_reg26:
13587 return "DW_OP_reg26";
13588 case DW_OP_reg27:
13589 return "DW_OP_reg27";
13590 case DW_OP_reg28:
13591 return "DW_OP_reg28";
13592 case DW_OP_reg29:
13593 return "DW_OP_reg29";
13594 case DW_OP_reg30:
13595 return "DW_OP_reg30";
13596 case DW_OP_reg31:
13597 return "DW_OP_reg31";
13598 case DW_OP_breg0:
13599 return "DW_OP_breg0";
13600 case DW_OP_breg1:
13601 return "DW_OP_breg1";
13602 case DW_OP_breg2:
13603 return "DW_OP_breg2";
13604 case DW_OP_breg3:
13605 return "DW_OP_breg3";
13606 case DW_OP_breg4:
13607 return "DW_OP_breg4";
13608 case DW_OP_breg5:
13609 return "DW_OP_breg5";
13610 case DW_OP_breg6:
13611 return "DW_OP_breg6";
13612 case DW_OP_breg7:
13613 return "DW_OP_breg7";
13614 case DW_OP_breg8:
13615 return "DW_OP_breg8";
13616 case DW_OP_breg9:
13617 return "DW_OP_breg9";
13618 case DW_OP_breg10:
13619 return "DW_OP_breg10";
13620 case DW_OP_breg11:
13621 return "DW_OP_breg11";
13622 case DW_OP_breg12:
13623 return "DW_OP_breg12";
13624 case DW_OP_breg13:
13625 return "DW_OP_breg13";
13626 case DW_OP_breg14:
13627 return "DW_OP_breg14";
13628 case DW_OP_breg15:
13629 return "DW_OP_breg15";
13630 case DW_OP_breg16:
13631 return "DW_OP_breg16";
13632 case DW_OP_breg17:
13633 return "DW_OP_breg17";
13634 case DW_OP_breg18:
13635 return "DW_OP_breg18";
13636 case DW_OP_breg19:
13637 return "DW_OP_breg19";
13638 case DW_OP_breg20:
13639 return "DW_OP_breg20";
13640 case DW_OP_breg21:
13641 return "DW_OP_breg21";
13642 case DW_OP_breg22:
13643 return "DW_OP_breg22";
13644 case DW_OP_breg23:
13645 return "DW_OP_breg23";
13646 case DW_OP_breg24:
13647 return "DW_OP_breg24";
13648 case DW_OP_breg25:
13649 return "DW_OP_breg25";
13650 case DW_OP_breg26:
13651 return "DW_OP_breg26";
13652 case DW_OP_breg27:
13653 return "DW_OP_breg27";
13654 case DW_OP_breg28:
13655 return "DW_OP_breg28";
13656 case DW_OP_breg29:
13657 return "DW_OP_breg29";
13658 case DW_OP_breg30:
13659 return "DW_OP_breg30";
13660 case DW_OP_breg31:
13661 return "DW_OP_breg31";
13662 case DW_OP_regx:
13663 return "DW_OP_regx";
13664 case DW_OP_fbreg:
13665 return "DW_OP_fbreg";
13666 case DW_OP_bregx:
13667 return "DW_OP_bregx";
13668 case DW_OP_piece:
13669 return "DW_OP_piece";
13670 case DW_OP_deref_size:
13671 return "DW_OP_deref_size";
13672 case DW_OP_xderef_size:
13673 return "DW_OP_xderef_size";
13674 case DW_OP_nop:
13675 return "DW_OP_nop";
13676 /* DWARF 3 extensions. */
13677 case DW_OP_push_object_address:
13678 return "DW_OP_push_object_address";
13679 case DW_OP_call2:
13680 return "DW_OP_call2";
13681 case DW_OP_call4:
13682 return "DW_OP_call4";
13683 case DW_OP_call_ref:
13684 return "DW_OP_call_ref";
13685 case DW_OP_form_tls_address:
13686 return "DW_OP_form_tls_address";
13687 case DW_OP_call_frame_cfa:
13688 return "DW_OP_call_frame_cfa";
13689 case DW_OP_bit_piece:
13690 return "DW_OP_bit_piece";
13691 /* DWARF 4 extensions. */
13692 case DW_OP_implicit_value:
13693 return "DW_OP_implicit_value";
13694 case DW_OP_stack_value:
13695 return "DW_OP_stack_value";
13696 /* GNU extensions. */
13697 case DW_OP_GNU_push_tls_address:
13698 return "DW_OP_GNU_push_tls_address";
13699 case DW_OP_GNU_uninit:
13700 return "DW_OP_GNU_uninit";
13701 case DW_OP_GNU_implicit_pointer:
13702 return "DW_OP_GNU_implicit_pointer";
13703 case DW_OP_GNU_entry_value:
13704 return "DW_OP_GNU_entry_value";
13705 case DW_OP_GNU_const_type:
13706 return "DW_OP_GNU_const_type";
13707 case DW_OP_GNU_regval_type:
13708 return "DW_OP_GNU_regval_type";
13709 case DW_OP_GNU_deref_type:
13710 return "DW_OP_GNU_deref_type";
13711 case DW_OP_GNU_convert:
13712 return "DW_OP_GNU_convert";
13713 case DW_OP_GNU_reinterpret:
13714 return "DW_OP_GNU_reinterpret";
13715 default:
13716 return NULL;
13717 }
13718 }
13719
13720 static char *
13721 dwarf_bool_name (unsigned mybool)
13722 {
13723 if (mybool)
13724 return "TRUE";
13725 else
13726 return "FALSE";
13727 }
13728
13729 /* Convert a DWARF type code into its string name. */
13730
13731 static char *
13732 dwarf_type_encoding_name (unsigned enc)
13733 {
13734 switch (enc)
13735 {
13736 case DW_ATE_void:
13737 return "DW_ATE_void";
13738 case DW_ATE_address:
13739 return "DW_ATE_address";
13740 case DW_ATE_boolean:
13741 return "DW_ATE_boolean";
13742 case DW_ATE_complex_float:
13743 return "DW_ATE_complex_float";
13744 case DW_ATE_float:
13745 return "DW_ATE_float";
13746 case DW_ATE_signed:
13747 return "DW_ATE_signed";
13748 case DW_ATE_signed_char:
13749 return "DW_ATE_signed_char";
13750 case DW_ATE_unsigned:
13751 return "DW_ATE_unsigned";
13752 case DW_ATE_unsigned_char:
13753 return "DW_ATE_unsigned_char";
13754 /* DWARF 3. */
13755 case DW_ATE_imaginary_float:
13756 return "DW_ATE_imaginary_float";
13757 case DW_ATE_packed_decimal:
13758 return "DW_ATE_packed_decimal";
13759 case DW_ATE_numeric_string:
13760 return "DW_ATE_numeric_string";
13761 case DW_ATE_edited:
13762 return "DW_ATE_edited";
13763 case DW_ATE_signed_fixed:
13764 return "DW_ATE_signed_fixed";
13765 case DW_ATE_unsigned_fixed:
13766 return "DW_ATE_unsigned_fixed";
13767 case DW_ATE_decimal_float:
13768 return "DW_ATE_decimal_float";
13769 /* DWARF 4. */
13770 case DW_ATE_UTF:
13771 return "DW_ATE_UTF";
13772 /* HP extensions. */
13773 case DW_ATE_HP_float80:
13774 return "DW_ATE_HP_float80";
13775 case DW_ATE_HP_complex_float80:
13776 return "DW_ATE_HP_complex_float80";
13777 case DW_ATE_HP_float128:
13778 return "DW_ATE_HP_float128";
13779 case DW_ATE_HP_complex_float128:
13780 return "DW_ATE_HP_complex_float128";
13781 case DW_ATE_HP_floathpintel:
13782 return "DW_ATE_HP_floathpintel";
13783 case DW_ATE_HP_imaginary_float80:
13784 return "DW_ATE_HP_imaginary_float80";
13785 case DW_ATE_HP_imaginary_float128:
13786 return "DW_ATE_HP_imaginary_float128";
13787 default:
13788 return "DW_ATE_<unknown>";
13789 }
13790 }
13791
13792 /* Convert a DWARF call frame info operation to its string name. */
13793
13794 #if 0
13795 static char *
13796 dwarf_cfi_name (unsigned cfi_opc)
13797 {
13798 switch (cfi_opc)
13799 {
13800 case DW_CFA_advance_loc:
13801 return "DW_CFA_advance_loc";
13802 case DW_CFA_offset:
13803 return "DW_CFA_offset";
13804 case DW_CFA_restore:
13805 return "DW_CFA_restore";
13806 case DW_CFA_nop:
13807 return "DW_CFA_nop";
13808 case DW_CFA_set_loc:
13809 return "DW_CFA_set_loc";
13810 case DW_CFA_advance_loc1:
13811 return "DW_CFA_advance_loc1";
13812 case DW_CFA_advance_loc2:
13813 return "DW_CFA_advance_loc2";
13814 case DW_CFA_advance_loc4:
13815 return "DW_CFA_advance_loc4";
13816 case DW_CFA_offset_extended:
13817 return "DW_CFA_offset_extended";
13818 case DW_CFA_restore_extended:
13819 return "DW_CFA_restore_extended";
13820 case DW_CFA_undefined:
13821 return "DW_CFA_undefined";
13822 case DW_CFA_same_value:
13823 return "DW_CFA_same_value";
13824 case DW_CFA_register:
13825 return "DW_CFA_register";
13826 case DW_CFA_remember_state:
13827 return "DW_CFA_remember_state";
13828 case DW_CFA_restore_state:
13829 return "DW_CFA_restore_state";
13830 case DW_CFA_def_cfa:
13831 return "DW_CFA_def_cfa";
13832 case DW_CFA_def_cfa_register:
13833 return "DW_CFA_def_cfa_register";
13834 case DW_CFA_def_cfa_offset:
13835 return "DW_CFA_def_cfa_offset";
13836 /* DWARF 3. */
13837 case DW_CFA_def_cfa_expression:
13838 return "DW_CFA_def_cfa_expression";
13839 case DW_CFA_expression:
13840 return "DW_CFA_expression";
13841 case DW_CFA_offset_extended_sf:
13842 return "DW_CFA_offset_extended_sf";
13843 case DW_CFA_def_cfa_sf:
13844 return "DW_CFA_def_cfa_sf";
13845 case DW_CFA_def_cfa_offset_sf:
13846 return "DW_CFA_def_cfa_offset_sf";
13847 case DW_CFA_val_offset:
13848 return "DW_CFA_val_offset";
13849 case DW_CFA_val_offset_sf:
13850 return "DW_CFA_val_offset_sf";
13851 case DW_CFA_val_expression:
13852 return "DW_CFA_val_expression";
13853 /* SGI/MIPS specific. */
13854 case DW_CFA_MIPS_advance_loc8:
13855 return "DW_CFA_MIPS_advance_loc8";
13856 /* GNU extensions. */
13857 case DW_CFA_GNU_window_save:
13858 return "DW_CFA_GNU_window_save";
13859 case DW_CFA_GNU_args_size:
13860 return "DW_CFA_GNU_args_size";
13861 case DW_CFA_GNU_negative_offset_extended:
13862 return "DW_CFA_GNU_negative_offset_extended";
13863 default:
13864 return "DW_CFA_<unknown>";
13865 }
13866 }
13867 #endif
13868
13869 static void
13870 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13871 {
13872 unsigned int i;
13873
13874 print_spaces (indent, f);
13875 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13876 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13877
13878 if (die->parent != NULL)
13879 {
13880 print_spaces (indent, f);
13881 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13882 die->parent->offset);
13883 }
13884
13885 print_spaces (indent, f);
13886 fprintf_unfiltered (f, " has children: %s\n",
13887 dwarf_bool_name (die->child != NULL));
13888
13889 print_spaces (indent, f);
13890 fprintf_unfiltered (f, " attributes:\n");
13891
13892 for (i = 0; i < die->num_attrs; ++i)
13893 {
13894 print_spaces (indent, f);
13895 fprintf_unfiltered (f, " %s (%s) ",
13896 dwarf_attr_name (die->attrs[i].name),
13897 dwarf_form_name (die->attrs[i].form));
13898
13899 switch (die->attrs[i].form)
13900 {
13901 case DW_FORM_ref_addr:
13902 case DW_FORM_addr:
13903 fprintf_unfiltered (f, "address: ");
13904 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13905 break;
13906 case DW_FORM_block2:
13907 case DW_FORM_block4:
13908 case DW_FORM_block:
13909 case DW_FORM_block1:
13910 fprintf_unfiltered (f, "block: size %d",
13911 DW_BLOCK (&die->attrs[i])->size);
13912 break;
13913 case DW_FORM_exprloc:
13914 fprintf_unfiltered (f, "expression: size %u",
13915 DW_BLOCK (&die->attrs[i])->size);
13916 break;
13917 case DW_FORM_ref1:
13918 case DW_FORM_ref2:
13919 case DW_FORM_ref4:
13920 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13921 (long) (DW_ADDR (&die->attrs[i])));
13922 break;
13923 case DW_FORM_data1:
13924 case DW_FORM_data2:
13925 case DW_FORM_data4:
13926 case DW_FORM_data8:
13927 case DW_FORM_udata:
13928 case DW_FORM_sdata:
13929 fprintf_unfiltered (f, "constant: %s",
13930 pulongest (DW_UNSND (&die->attrs[i])));
13931 break;
13932 case DW_FORM_sec_offset:
13933 fprintf_unfiltered (f, "section offset: %s",
13934 pulongest (DW_UNSND (&die->attrs[i])));
13935 break;
13936 case DW_FORM_ref_sig8:
13937 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13938 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13939 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13940 else
13941 fprintf_unfiltered (f, "signatured type, offset: unknown");
13942 break;
13943 case DW_FORM_string:
13944 case DW_FORM_strp:
13945 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13946 DW_STRING (&die->attrs[i])
13947 ? DW_STRING (&die->attrs[i]) : "",
13948 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13949 break;
13950 case DW_FORM_flag:
13951 if (DW_UNSND (&die->attrs[i]))
13952 fprintf_unfiltered (f, "flag: TRUE");
13953 else
13954 fprintf_unfiltered (f, "flag: FALSE");
13955 break;
13956 case DW_FORM_flag_present:
13957 fprintf_unfiltered (f, "flag: TRUE");
13958 break;
13959 case DW_FORM_indirect:
13960 /* The reader will have reduced the indirect form to
13961 the "base form" so this form should not occur. */
13962 fprintf_unfiltered (f,
13963 "unexpected attribute form: DW_FORM_indirect");
13964 break;
13965 default:
13966 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13967 die->attrs[i].form);
13968 break;
13969 }
13970 fprintf_unfiltered (f, "\n");
13971 }
13972 }
13973
13974 static void
13975 dump_die_for_error (struct die_info *die)
13976 {
13977 dump_die_shallow (gdb_stderr, 0, die);
13978 }
13979
13980 static void
13981 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13982 {
13983 int indent = level * 4;
13984
13985 gdb_assert (die != NULL);
13986
13987 if (level >= max_level)
13988 return;
13989
13990 dump_die_shallow (f, indent, die);
13991
13992 if (die->child != NULL)
13993 {
13994 print_spaces (indent, f);
13995 fprintf_unfiltered (f, " Children:");
13996 if (level + 1 < max_level)
13997 {
13998 fprintf_unfiltered (f, "\n");
13999 dump_die_1 (f, level + 1, max_level, die->child);
14000 }
14001 else
14002 {
14003 fprintf_unfiltered (f,
14004 " [not printed, max nesting level reached]\n");
14005 }
14006 }
14007
14008 if (die->sibling != NULL && level > 0)
14009 {
14010 dump_die_1 (f, level, max_level, die->sibling);
14011 }
14012 }
14013
14014 /* This is called from the pdie macro in gdbinit.in.
14015 It's not static so gcc will keep a copy callable from gdb. */
14016
14017 void
14018 dump_die (struct die_info *die, int max_level)
14019 {
14020 dump_die_1 (gdb_stdlog, 0, max_level, die);
14021 }
14022
14023 static void
14024 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14025 {
14026 void **slot;
14027
14028 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14029
14030 *slot = die;
14031 }
14032
14033 static int
14034 is_ref_attr (struct attribute *attr)
14035 {
14036 switch (attr->form)
14037 {
14038 case DW_FORM_ref_addr:
14039 case DW_FORM_ref1:
14040 case DW_FORM_ref2:
14041 case DW_FORM_ref4:
14042 case DW_FORM_ref8:
14043 case DW_FORM_ref_udata:
14044 return 1;
14045 default:
14046 return 0;
14047 }
14048 }
14049
14050 static unsigned int
14051 dwarf2_get_ref_die_offset (struct attribute *attr)
14052 {
14053 if (is_ref_attr (attr))
14054 return DW_ADDR (attr);
14055
14056 complaint (&symfile_complaints,
14057 _("unsupported die ref attribute form: '%s'"),
14058 dwarf_form_name (attr->form));
14059 return 0;
14060 }
14061
14062 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14063 * the value held by the attribute is not constant. */
14064
14065 static LONGEST
14066 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14067 {
14068 if (attr->form == DW_FORM_sdata)
14069 return DW_SND (attr);
14070 else if (attr->form == DW_FORM_udata
14071 || attr->form == DW_FORM_data1
14072 || attr->form == DW_FORM_data2
14073 || attr->form == DW_FORM_data4
14074 || attr->form == DW_FORM_data8)
14075 return DW_UNSND (attr);
14076 else
14077 {
14078 complaint (&symfile_complaints,
14079 _("Attribute value is not a constant (%s)"),
14080 dwarf_form_name (attr->form));
14081 return default_value;
14082 }
14083 }
14084
14085 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14086 unit and add it to our queue.
14087 The result is non-zero if PER_CU was queued, otherwise the result is zero
14088 meaning either PER_CU is already queued or it is already loaded. */
14089
14090 static int
14091 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14092 struct dwarf2_per_cu_data *per_cu)
14093 {
14094 /* We may arrive here during partial symbol reading, if we need full
14095 DIEs to process an unusual case (e.g. template arguments). Do
14096 not queue PER_CU, just tell our caller to load its DIEs. */
14097 if (dwarf2_per_objfile->reading_partial_symbols)
14098 {
14099 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14100 return 1;
14101 return 0;
14102 }
14103
14104 /* Mark the dependence relation so that we don't flush PER_CU
14105 too early. */
14106 dwarf2_add_dependence (this_cu, per_cu);
14107
14108 /* If it's already on the queue, we have nothing to do. */
14109 if (per_cu->queued)
14110 return 0;
14111
14112 /* If the compilation unit is already loaded, just mark it as
14113 used. */
14114 if (per_cu->cu != NULL)
14115 {
14116 per_cu->cu->last_used = 0;
14117 return 0;
14118 }
14119
14120 /* Add it to the queue. */
14121 queue_comp_unit (per_cu);
14122
14123 return 1;
14124 }
14125
14126 /* Follow reference or signature attribute ATTR of SRC_DIE.
14127 On entry *REF_CU is the CU of SRC_DIE.
14128 On exit *REF_CU is the CU of the result. */
14129
14130 static struct die_info *
14131 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14132 struct dwarf2_cu **ref_cu)
14133 {
14134 struct die_info *die;
14135
14136 if (is_ref_attr (attr))
14137 die = follow_die_ref (src_die, attr, ref_cu);
14138 else if (attr->form == DW_FORM_ref_sig8)
14139 die = follow_die_sig (src_die, attr, ref_cu);
14140 else
14141 {
14142 dump_die_for_error (src_die);
14143 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14144 (*ref_cu)->objfile->name);
14145 }
14146
14147 return die;
14148 }
14149
14150 /* Follow reference OFFSET.
14151 On entry *REF_CU is the CU of the source die referencing OFFSET.
14152 On exit *REF_CU is the CU of the result.
14153 Returns NULL if OFFSET is invalid. */
14154
14155 static struct die_info *
14156 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14157 {
14158 struct die_info temp_die;
14159 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14160
14161 gdb_assert (cu->per_cu != NULL);
14162
14163 target_cu = cu;
14164
14165 if (cu->per_cu->debug_types_section)
14166 {
14167 /* .debug_types CUs cannot reference anything outside their CU.
14168 If they need to, they have to reference a signatured type via
14169 DW_FORM_ref_sig8. */
14170 if (! offset_in_cu_p (&cu->header, offset))
14171 return NULL;
14172 }
14173 else if (! offset_in_cu_p (&cu->header, offset))
14174 {
14175 struct dwarf2_per_cu_data *per_cu;
14176
14177 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14178
14179 /* If necessary, add it to the queue and load its DIEs. */
14180 if (maybe_queue_comp_unit (cu, per_cu))
14181 load_full_comp_unit (per_cu);
14182
14183 target_cu = per_cu->cu;
14184 }
14185 else if (cu->dies == NULL)
14186 {
14187 /* We're loading full DIEs during partial symbol reading. */
14188 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14189 load_full_comp_unit (cu->per_cu);
14190 }
14191
14192 *ref_cu = target_cu;
14193 temp_die.offset = offset;
14194 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14195 }
14196
14197 /* Follow reference attribute ATTR of SRC_DIE.
14198 On entry *REF_CU is the CU of SRC_DIE.
14199 On exit *REF_CU is the CU of the result. */
14200
14201 static struct die_info *
14202 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14203 struct dwarf2_cu **ref_cu)
14204 {
14205 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14206 struct dwarf2_cu *cu = *ref_cu;
14207 struct die_info *die;
14208
14209 die = follow_die_offset (offset, ref_cu);
14210 if (!die)
14211 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14212 "at 0x%x [in module %s]"),
14213 offset, src_die->offset, cu->objfile->name);
14214
14215 return die;
14216 }
14217
14218 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14219 Returned value is intended for DW_OP_call*. Returned
14220 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14221
14222 struct dwarf2_locexpr_baton
14223 dwarf2_fetch_die_location_block (unsigned int offset_in_cu,
14224 struct dwarf2_per_cu_data *per_cu,
14225 CORE_ADDR (*get_frame_pc) (void *baton),
14226 void *baton)
14227 {
14228 unsigned int offset = per_cu->offset + offset_in_cu;
14229 struct dwarf2_cu *cu;
14230 struct die_info *die;
14231 struct attribute *attr;
14232 struct dwarf2_locexpr_baton retval;
14233
14234 dw2_setup (per_cu->objfile);
14235
14236 if (per_cu->cu == NULL)
14237 load_cu (per_cu);
14238 cu = per_cu->cu;
14239
14240 die = follow_die_offset (offset, &cu);
14241 if (!die)
14242 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14243 offset, per_cu->objfile->name);
14244
14245 attr = dwarf2_attr (die, DW_AT_location, cu);
14246 if (!attr)
14247 {
14248 /* DWARF: "If there is no such attribute, then there is no effect.".
14249 DATA is ignored if SIZE is 0. */
14250
14251 retval.data = NULL;
14252 retval.size = 0;
14253 }
14254 else if (attr_form_is_section_offset (attr))
14255 {
14256 struct dwarf2_loclist_baton loclist_baton;
14257 CORE_ADDR pc = (*get_frame_pc) (baton);
14258 size_t size;
14259
14260 fill_in_loclist_baton (cu, &loclist_baton, attr);
14261
14262 retval.data = dwarf2_find_location_expression (&loclist_baton,
14263 &size, pc);
14264 retval.size = size;
14265 }
14266 else
14267 {
14268 if (!attr_form_is_block (attr))
14269 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14270 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14271 offset, per_cu->objfile->name);
14272
14273 retval.data = DW_BLOCK (attr)->data;
14274 retval.size = DW_BLOCK (attr)->size;
14275 }
14276 retval.per_cu = cu->per_cu;
14277
14278 age_cached_comp_units ();
14279
14280 return retval;
14281 }
14282
14283 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14284 PER_CU. */
14285
14286 struct type *
14287 dwarf2_get_die_type (unsigned int die_offset,
14288 struct dwarf2_per_cu_data *per_cu)
14289 {
14290 dw2_setup (per_cu->objfile);
14291 return get_die_type_at_offset (per_cu->offset + die_offset, per_cu);
14292 }
14293
14294 /* Follow the signature attribute ATTR in SRC_DIE.
14295 On entry *REF_CU is the CU of SRC_DIE.
14296 On exit *REF_CU is the CU of the result. */
14297
14298 static struct die_info *
14299 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14300 struct dwarf2_cu **ref_cu)
14301 {
14302 struct objfile *objfile = (*ref_cu)->objfile;
14303 struct die_info temp_die;
14304 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14305 struct dwarf2_cu *sig_cu;
14306 struct die_info *die;
14307
14308 /* sig_type will be NULL if the signatured type is missing from
14309 the debug info. */
14310 if (sig_type == NULL)
14311 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14312 "at 0x%x [in module %s]"),
14313 src_die->offset, objfile->name);
14314
14315 /* If necessary, add it to the queue and load its DIEs. */
14316
14317 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14318 read_signatured_type (sig_type);
14319
14320 gdb_assert (sig_type->per_cu.cu != NULL);
14321
14322 sig_cu = sig_type->per_cu.cu;
14323 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14324 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14325 if (die)
14326 {
14327 *ref_cu = sig_cu;
14328 return die;
14329 }
14330
14331 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14332 "from DIE at 0x%x [in module %s]"),
14333 sig_type->type_offset, src_die->offset, objfile->name);
14334 }
14335
14336 /* Given an offset of a signatured type, return its signatured_type. */
14337
14338 static struct signatured_type *
14339 lookup_signatured_type_at_offset (struct objfile *objfile,
14340 struct dwarf2_section_info *section,
14341 unsigned int offset)
14342 {
14343 gdb_byte *info_ptr = section->buffer + offset;
14344 unsigned int length, initial_length_size;
14345 unsigned int sig_offset;
14346 struct signatured_type find_entry, *type_sig;
14347
14348 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14349 sig_offset = (initial_length_size
14350 + 2 /*version*/
14351 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14352 + 1 /*address_size*/);
14353 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14354 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14355
14356 /* This is only used to lookup previously recorded types.
14357 If we didn't find it, it's our bug. */
14358 gdb_assert (type_sig != NULL);
14359 gdb_assert (offset == type_sig->per_cu.offset);
14360
14361 return type_sig;
14362 }
14363
14364 /* Load the DIEs associated with type unit PER_CU into memory. */
14365
14366 static void
14367 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14368 {
14369 struct objfile *objfile = per_cu->objfile;
14370 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14371 unsigned int offset = per_cu->offset;
14372 struct signatured_type *type_sig;
14373
14374 dwarf2_read_section (objfile, sect);
14375
14376 /* We have the section offset, but we need the signature to do the
14377 hash table lookup. */
14378 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14379 the signature to assert we found the right one.
14380 Ok, but it's a lot of work. We should simplify things so any needed
14381 assert doesn't require all this clumsiness. */
14382 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14383
14384 gdb_assert (type_sig->per_cu.cu == NULL);
14385
14386 read_signatured_type (type_sig);
14387
14388 gdb_assert (type_sig->per_cu.cu != NULL);
14389 }
14390
14391 /* Read in a signatured type and build its CU and DIEs. */
14392
14393 static void
14394 read_signatured_type (struct signatured_type *type_sig)
14395 {
14396 struct objfile *objfile = type_sig->per_cu.objfile;
14397 gdb_byte *types_ptr;
14398 struct die_reader_specs reader_specs;
14399 struct dwarf2_cu *cu;
14400 ULONGEST signature;
14401 struct cleanup *back_to, *free_cu_cleanup;
14402 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14403
14404 dwarf2_read_section (objfile, section);
14405 types_ptr = section->buffer + type_sig->per_cu.offset;
14406
14407 gdb_assert (type_sig->per_cu.cu == NULL);
14408
14409 cu = xmalloc (sizeof (*cu));
14410 init_one_comp_unit (cu, &type_sig->per_cu);
14411
14412 /* If an error occurs while loading, release our storage. */
14413 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14414
14415 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14416 &signature, NULL);
14417 gdb_assert (signature == type_sig->signature);
14418
14419 cu->die_hash
14420 = htab_create_alloc_ex (cu->header.length / 12,
14421 die_hash,
14422 die_eq,
14423 NULL,
14424 &cu->comp_unit_obstack,
14425 hashtab_obstack_allocate,
14426 dummy_obstack_deallocate);
14427
14428 dwarf2_read_abbrevs (cu);
14429 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14430
14431 init_cu_die_reader (&reader_specs, cu);
14432
14433 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14434 NULL /*parent*/);
14435
14436 /* We try not to read any attributes in this function, because not
14437 all CUs needed for references have been loaded yet, and symbol
14438 table processing isn't initialized. But we have to set the CU language,
14439 or we won't be able to build types correctly. */
14440 prepare_one_comp_unit (cu, cu->dies);
14441
14442 do_cleanups (back_to);
14443
14444 /* We've successfully allocated this compilation unit. Let our caller
14445 clean it up when finished with it. */
14446 discard_cleanups (free_cu_cleanup);
14447
14448 /* Link this TU into read_in_chain. */
14449 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14450 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14451 }
14452
14453 /* Decode simple location descriptions.
14454 Given a pointer to a dwarf block that defines a location, compute
14455 the location and return the value.
14456
14457 NOTE drow/2003-11-18: This function is called in two situations
14458 now: for the address of static or global variables (partial symbols
14459 only) and for offsets into structures which are expected to be
14460 (more or less) constant. The partial symbol case should go away,
14461 and only the constant case should remain. That will let this
14462 function complain more accurately. A few special modes are allowed
14463 without complaint for global variables (for instance, global
14464 register values and thread-local values).
14465
14466 A location description containing no operations indicates that the
14467 object is optimized out. The return value is 0 for that case.
14468 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14469 callers will only want a very basic result and this can become a
14470 complaint.
14471
14472 Note that stack[0] is unused except as a default error return. */
14473
14474 static CORE_ADDR
14475 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14476 {
14477 struct objfile *objfile = cu->objfile;
14478 int i;
14479 int size = blk->size;
14480 gdb_byte *data = blk->data;
14481 CORE_ADDR stack[64];
14482 int stacki;
14483 unsigned int bytes_read, unsnd;
14484 gdb_byte op;
14485
14486 i = 0;
14487 stacki = 0;
14488 stack[stacki] = 0;
14489 stack[++stacki] = 0;
14490
14491 while (i < size)
14492 {
14493 op = data[i++];
14494 switch (op)
14495 {
14496 case DW_OP_lit0:
14497 case DW_OP_lit1:
14498 case DW_OP_lit2:
14499 case DW_OP_lit3:
14500 case DW_OP_lit4:
14501 case DW_OP_lit5:
14502 case DW_OP_lit6:
14503 case DW_OP_lit7:
14504 case DW_OP_lit8:
14505 case DW_OP_lit9:
14506 case DW_OP_lit10:
14507 case DW_OP_lit11:
14508 case DW_OP_lit12:
14509 case DW_OP_lit13:
14510 case DW_OP_lit14:
14511 case DW_OP_lit15:
14512 case DW_OP_lit16:
14513 case DW_OP_lit17:
14514 case DW_OP_lit18:
14515 case DW_OP_lit19:
14516 case DW_OP_lit20:
14517 case DW_OP_lit21:
14518 case DW_OP_lit22:
14519 case DW_OP_lit23:
14520 case DW_OP_lit24:
14521 case DW_OP_lit25:
14522 case DW_OP_lit26:
14523 case DW_OP_lit27:
14524 case DW_OP_lit28:
14525 case DW_OP_lit29:
14526 case DW_OP_lit30:
14527 case DW_OP_lit31:
14528 stack[++stacki] = op - DW_OP_lit0;
14529 break;
14530
14531 case DW_OP_reg0:
14532 case DW_OP_reg1:
14533 case DW_OP_reg2:
14534 case DW_OP_reg3:
14535 case DW_OP_reg4:
14536 case DW_OP_reg5:
14537 case DW_OP_reg6:
14538 case DW_OP_reg7:
14539 case DW_OP_reg8:
14540 case DW_OP_reg9:
14541 case DW_OP_reg10:
14542 case DW_OP_reg11:
14543 case DW_OP_reg12:
14544 case DW_OP_reg13:
14545 case DW_OP_reg14:
14546 case DW_OP_reg15:
14547 case DW_OP_reg16:
14548 case DW_OP_reg17:
14549 case DW_OP_reg18:
14550 case DW_OP_reg19:
14551 case DW_OP_reg20:
14552 case DW_OP_reg21:
14553 case DW_OP_reg22:
14554 case DW_OP_reg23:
14555 case DW_OP_reg24:
14556 case DW_OP_reg25:
14557 case DW_OP_reg26:
14558 case DW_OP_reg27:
14559 case DW_OP_reg28:
14560 case DW_OP_reg29:
14561 case DW_OP_reg30:
14562 case DW_OP_reg31:
14563 stack[++stacki] = op - DW_OP_reg0;
14564 if (i < size)
14565 dwarf2_complex_location_expr_complaint ();
14566 break;
14567
14568 case DW_OP_regx:
14569 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14570 i += bytes_read;
14571 stack[++stacki] = unsnd;
14572 if (i < size)
14573 dwarf2_complex_location_expr_complaint ();
14574 break;
14575
14576 case DW_OP_addr:
14577 stack[++stacki] = read_address (objfile->obfd, &data[i],
14578 cu, &bytes_read);
14579 i += bytes_read;
14580 break;
14581
14582 case DW_OP_const1u:
14583 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14584 i += 1;
14585 break;
14586
14587 case DW_OP_const1s:
14588 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14589 i += 1;
14590 break;
14591
14592 case DW_OP_const2u:
14593 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14594 i += 2;
14595 break;
14596
14597 case DW_OP_const2s:
14598 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14599 i += 2;
14600 break;
14601
14602 case DW_OP_const4u:
14603 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14604 i += 4;
14605 break;
14606
14607 case DW_OP_const4s:
14608 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14609 i += 4;
14610 break;
14611
14612 case DW_OP_const8u:
14613 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14614 i += 8;
14615 break;
14616
14617 case DW_OP_constu:
14618 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14619 &bytes_read);
14620 i += bytes_read;
14621 break;
14622
14623 case DW_OP_consts:
14624 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14625 i += bytes_read;
14626 break;
14627
14628 case DW_OP_dup:
14629 stack[stacki + 1] = stack[stacki];
14630 stacki++;
14631 break;
14632
14633 case DW_OP_plus:
14634 stack[stacki - 1] += stack[stacki];
14635 stacki--;
14636 break;
14637
14638 case DW_OP_plus_uconst:
14639 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14640 &bytes_read);
14641 i += bytes_read;
14642 break;
14643
14644 case DW_OP_minus:
14645 stack[stacki - 1] -= stack[stacki];
14646 stacki--;
14647 break;
14648
14649 case DW_OP_deref:
14650 /* If we're not the last op, then we definitely can't encode
14651 this using GDB's address_class enum. This is valid for partial
14652 global symbols, although the variable's address will be bogus
14653 in the psymtab. */
14654 if (i < size)
14655 dwarf2_complex_location_expr_complaint ();
14656 break;
14657
14658 case DW_OP_GNU_push_tls_address:
14659 /* The top of the stack has the offset from the beginning
14660 of the thread control block at which the variable is located. */
14661 /* Nothing should follow this operator, so the top of stack would
14662 be returned. */
14663 /* This is valid for partial global symbols, but the variable's
14664 address will be bogus in the psymtab. Make it always at least
14665 non-zero to not look as a variable garbage collected by linker
14666 which have DW_OP_addr 0. */
14667 if (i < size)
14668 dwarf2_complex_location_expr_complaint ();
14669 stack[stacki]++;
14670 break;
14671
14672 case DW_OP_GNU_uninit:
14673 break;
14674
14675 default:
14676 {
14677 const char *name = dwarf_stack_op_name (op);
14678
14679 if (name)
14680 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14681 name);
14682 else
14683 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14684 op);
14685 }
14686
14687 return (stack[stacki]);
14688 }
14689
14690 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14691 outside of the allocated space. Also enforce minimum>0. */
14692 if (stacki >= ARRAY_SIZE (stack) - 1)
14693 {
14694 complaint (&symfile_complaints,
14695 _("location description stack overflow"));
14696 return 0;
14697 }
14698
14699 if (stacki <= 0)
14700 {
14701 complaint (&symfile_complaints,
14702 _("location description stack underflow"));
14703 return 0;
14704 }
14705 }
14706 return (stack[stacki]);
14707 }
14708
14709 /* memory allocation interface */
14710
14711 static struct dwarf_block *
14712 dwarf_alloc_block (struct dwarf2_cu *cu)
14713 {
14714 struct dwarf_block *blk;
14715
14716 blk = (struct dwarf_block *)
14717 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14718 return (blk);
14719 }
14720
14721 static struct abbrev_info *
14722 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14723 {
14724 struct abbrev_info *abbrev;
14725
14726 abbrev = (struct abbrev_info *)
14727 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14728 memset (abbrev, 0, sizeof (struct abbrev_info));
14729 return (abbrev);
14730 }
14731
14732 static struct die_info *
14733 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14734 {
14735 struct die_info *die;
14736 size_t size = sizeof (struct die_info);
14737
14738 if (num_attrs > 1)
14739 size += (num_attrs - 1) * sizeof (struct attribute);
14740
14741 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14742 memset (die, 0, sizeof (struct die_info));
14743 return (die);
14744 }
14745
14746 \f
14747 /* Macro support. */
14748
14749 /* Return the full name of file number I in *LH's file name table.
14750 Use COMP_DIR as the name of the current directory of the
14751 compilation. The result is allocated using xmalloc; the caller is
14752 responsible for freeing it. */
14753 static char *
14754 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14755 {
14756 /* Is the file number a valid index into the line header's file name
14757 table? Remember that file numbers start with one, not zero. */
14758 if (1 <= file && file <= lh->num_file_names)
14759 {
14760 struct file_entry *fe = &lh->file_names[file - 1];
14761
14762 if (IS_ABSOLUTE_PATH (fe->name))
14763 return xstrdup (fe->name);
14764 else
14765 {
14766 const char *dir;
14767 int dir_len;
14768 char *full_name;
14769
14770 if (fe->dir_index)
14771 dir = lh->include_dirs[fe->dir_index - 1];
14772 else
14773 dir = comp_dir;
14774
14775 if (dir)
14776 {
14777 dir_len = strlen (dir);
14778 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14779 strcpy (full_name, dir);
14780 full_name[dir_len] = '/';
14781 strcpy (full_name + dir_len + 1, fe->name);
14782 return full_name;
14783 }
14784 else
14785 return xstrdup (fe->name);
14786 }
14787 }
14788 else
14789 {
14790 /* The compiler produced a bogus file number. We can at least
14791 record the macro definitions made in the file, even if we
14792 won't be able to find the file by name. */
14793 char fake_name[80];
14794
14795 sprintf (fake_name, "<bad macro file number %d>", file);
14796
14797 complaint (&symfile_complaints,
14798 _("bad file number in macro information (%d)"),
14799 file);
14800
14801 return xstrdup (fake_name);
14802 }
14803 }
14804
14805
14806 static struct macro_source_file *
14807 macro_start_file (int file, int line,
14808 struct macro_source_file *current_file,
14809 const char *comp_dir,
14810 struct line_header *lh, struct objfile *objfile)
14811 {
14812 /* The full name of this source file. */
14813 char *full_name = file_full_name (file, lh, comp_dir);
14814
14815 /* We don't create a macro table for this compilation unit
14816 at all until we actually get a filename. */
14817 if (! pending_macros)
14818 pending_macros = new_macro_table (&objfile->objfile_obstack,
14819 objfile->macro_cache);
14820
14821 if (! current_file)
14822 /* If we have no current file, then this must be the start_file
14823 directive for the compilation unit's main source file. */
14824 current_file = macro_set_main (pending_macros, full_name);
14825 else
14826 current_file = macro_include (current_file, line, full_name);
14827
14828 xfree (full_name);
14829
14830 return current_file;
14831 }
14832
14833
14834 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14835 followed by a null byte. */
14836 static char *
14837 copy_string (const char *buf, int len)
14838 {
14839 char *s = xmalloc (len + 1);
14840
14841 memcpy (s, buf, len);
14842 s[len] = '\0';
14843 return s;
14844 }
14845
14846
14847 static const char *
14848 consume_improper_spaces (const char *p, const char *body)
14849 {
14850 if (*p == ' ')
14851 {
14852 complaint (&symfile_complaints,
14853 _("macro definition contains spaces "
14854 "in formal argument list:\n`%s'"),
14855 body);
14856
14857 while (*p == ' ')
14858 p++;
14859 }
14860
14861 return p;
14862 }
14863
14864
14865 static void
14866 parse_macro_definition (struct macro_source_file *file, int line,
14867 const char *body)
14868 {
14869 const char *p;
14870
14871 /* The body string takes one of two forms. For object-like macro
14872 definitions, it should be:
14873
14874 <macro name> " " <definition>
14875
14876 For function-like macro definitions, it should be:
14877
14878 <macro name> "() " <definition>
14879 or
14880 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14881
14882 Spaces may appear only where explicitly indicated, and in the
14883 <definition>.
14884
14885 The Dwarf 2 spec says that an object-like macro's name is always
14886 followed by a space, but versions of GCC around March 2002 omit
14887 the space when the macro's definition is the empty string.
14888
14889 The Dwarf 2 spec says that there should be no spaces between the
14890 formal arguments in a function-like macro's formal argument list,
14891 but versions of GCC around March 2002 include spaces after the
14892 commas. */
14893
14894
14895 /* Find the extent of the macro name. The macro name is terminated
14896 by either a space or null character (for an object-like macro) or
14897 an opening paren (for a function-like macro). */
14898 for (p = body; *p; p++)
14899 if (*p == ' ' || *p == '(')
14900 break;
14901
14902 if (*p == ' ' || *p == '\0')
14903 {
14904 /* It's an object-like macro. */
14905 int name_len = p - body;
14906 char *name = copy_string (body, name_len);
14907 const char *replacement;
14908
14909 if (*p == ' ')
14910 replacement = body + name_len + 1;
14911 else
14912 {
14913 dwarf2_macro_malformed_definition_complaint (body);
14914 replacement = body + name_len;
14915 }
14916
14917 macro_define_object (file, line, name, replacement);
14918
14919 xfree (name);
14920 }
14921 else if (*p == '(')
14922 {
14923 /* It's a function-like macro. */
14924 char *name = copy_string (body, p - body);
14925 int argc = 0;
14926 int argv_size = 1;
14927 char **argv = xmalloc (argv_size * sizeof (*argv));
14928
14929 p++;
14930
14931 p = consume_improper_spaces (p, body);
14932
14933 /* Parse the formal argument list. */
14934 while (*p && *p != ')')
14935 {
14936 /* Find the extent of the current argument name. */
14937 const char *arg_start = p;
14938
14939 while (*p && *p != ',' && *p != ')' && *p != ' ')
14940 p++;
14941
14942 if (! *p || p == arg_start)
14943 dwarf2_macro_malformed_definition_complaint (body);
14944 else
14945 {
14946 /* Make sure argv has room for the new argument. */
14947 if (argc >= argv_size)
14948 {
14949 argv_size *= 2;
14950 argv = xrealloc (argv, argv_size * sizeof (*argv));
14951 }
14952
14953 argv[argc++] = copy_string (arg_start, p - arg_start);
14954 }
14955
14956 p = consume_improper_spaces (p, body);
14957
14958 /* Consume the comma, if present. */
14959 if (*p == ',')
14960 {
14961 p++;
14962
14963 p = consume_improper_spaces (p, body);
14964 }
14965 }
14966
14967 if (*p == ')')
14968 {
14969 p++;
14970
14971 if (*p == ' ')
14972 /* Perfectly formed definition, no complaints. */
14973 macro_define_function (file, line, name,
14974 argc, (const char **) argv,
14975 p + 1);
14976 else if (*p == '\0')
14977 {
14978 /* Complain, but do define it. */
14979 dwarf2_macro_malformed_definition_complaint (body);
14980 macro_define_function (file, line, name,
14981 argc, (const char **) argv,
14982 p);
14983 }
14984 else
14985 /* Just complain. */
14986 dwarf2_macro_malformed_definition_complaint (body);
14987 }
14988 else
14989 /* Just complain. */
14990 dwarf2_macro_malformed_definition_complaint (body);
14991
14992 xfree (name);
14993 {
14994 int i;
14995
14996 for (i = 0; i < argc; i++)
14997 xfree (argv[i]);
14998 }
14999 xfree (argv);
15000 }
15001 else
15002 dwarf2_macro_malformed_definition_complaint (body);
15003 }
15004
15005 /* Skip some bytes from BYTES according to the form given in FORM.
15006 Returns the new pointer. */
15007
15008 static gdb_byte *
15009 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15010 enum dwarf_form form,
15011 unsigned int offset_size,
15012 struct dwarf2_section_info *section)
15013 {
15014 unsigned int bytes_read;
15015
15016 switch (form)
15017 {
15018 case DW_FORM_data1:
15019 case DW_FORM_flag:
15020 ++bytes;
15021 break;
15022
15023 case DW_FORM_data2:
15024 bytes += 2;
15025 break;
15026
15027 case DW_FORM_data4:
15028 bytes += 4;
15029 break;
15030
15031 case DW_FORM_data8:
15032 bytes += 8;
15033 break;
15034
15035 case DW_FORM_string:
15036 read_direct_string (abfd, bytes, &bytes_read);
15037 bytes += bytes_read;
15038 break;
15039
15040 case DW_FORM_sec_offset:
15041 case DW_FORM_strp:
15042 bytes += offset_size;
15043 break;
15044
15045 case DW_FORM_block:
15046 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15047 bytes += bytes_read;
15048 break;
15049
15050 case DW_FORM_block1:
15051 bytes += 1 + read_1_byte (abfd, bytes);
15052 break;
15053 case DW_FORM_block2:
15054 bytes += 2 + read_2_bytes (abfd, bytes);
15055 break;
15056 case DW_FORM_block4:
15057 bytes += 4 + read_4_bytes (abfd, bytes);
15058 break;
15059
15060 case DW_FORM_sdata:
15061 case DW_FORM_udata:
15062 bytes = skip_leb128 (abfd, bytes);
15063 break;
15064
15065 default:
15066 {
15067 complain:
15068 complaint (&symfile_complaints,
15069 _("invalid form 0x%x in `%s'"),
15070 form,
15071 section->asection->name);
15072 return NULL;
15073 }
15074 }
15075
15076 return bytes;
15077 }
15078
15079 /* A helper for dwarf_decode_macros that handles skipping an unknown
15080 opcode. Returns an updated pointer to the macro data buffer; or,
15081 on error, issues a complaint and returns NULL. */
15082
15083 static gdb_byte *
15084 skip_unknown_opcode (unsigned int opcode,
15085 gdb_byte **opcode_definitions,
15086 gdb_byte *mac_ptr,
15087 bfd *abfd,
15088 unsigned int offset_size,
15089 struct dwarf2_section_info *section)
15090 {
15091 unsigned int bytes_read, i;
15092 unsigned long arg;
15093 gdb_byte *defn;
15094
15095 if (opcode_definitions[opcode] == NULL)
15096 {
15097 complaint (&symfile_complaints,
15098 _("unrecognized DW_MACFINO opcode 0x%x"),
15099 opcode);
15100 return NULL;
15101 }
15102
15103 defn = opcode_definitions[opcode];
15104 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15105 defn += bytes_read;
15106
15107 for (i = 0; i < arg; ++i)
15108 {
15109 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15110 if (mac_ptr == NULL)
15111 {
15112 /* skip_form_bytes already issued the complaint. */
15113 return NULL;
15114 }
15115 }
15116
15117 return mac_ptr;
15118 }
15119
15120 /* A helper function which parses the header of a macro section.
15121 If the macro section is the extended (for now called "GNU") type,
15122 then this updates *OFFSET_SIZE. Returns a pointer to just after
15123 the header, or issues a complaint and returns NULL on error. */
15124
15125 static gdb_byte *
15126 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15127 bfd *abfd,
15128 gdb_byte *mac_ptr,
15129 unsigned int *offset_size,
15130 int section_is_gnu)
15131 {
15132 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15133
15134 if (section_is_gnu)
15135 {
15136 unsigned int version, flags;
15137
15138 version = read_2_bytes (abfd, mac_ptr);
15139 if (version != 4)
15140 {
15141 complaint (&symfile_complaints,
15142 _("unrecognized version `%d' in .debug_macro section"),
15143 version);
15144 return NULL;
15145 }
15146 mac_ptr += 2;
15147
15148 flags = read_1_byte (abfd, mac_ptr);
15149 ++mac_ptr;
15150 *offset_size = (flags & 1) ? 8 : 4;
15151
15152 if ((flags & 2) != 0)
15153 /* We don't need the line table offset. */
15154 mac_ptr += *offset_size;
15155
15156 /* Vendor opcode descriptions. */
15157 if ((flags & 4) != 0)
15158 {
15159 unsigned int i, count;
15160
15161 count = read_1_byte (abfd, mac_ptr);
15162 ++mac_ptr;
15163 for (i = 0; i < count; ++i)
15164 {
15165 unsigned int opcode, bytes_read;
15166 unsigned long arg;
15167
15168 opcode = read_1_byte (abfd, mac_ptr);
15169 ++mac_ptr;
15170 opcode_definitions[opcode] = mac_ptr;
15171 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15172 mac_ptr += bytes_read;
15173 mac_ptr += arg;
15174 }
15175 }
15176 }
15177
15178 return mac_ptr;
15179 }
15180
15181 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15182 including DW_MACRO_GNU_transparent_include. */
15183
15184 static void
15185 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15186 struct macro_source_file *current_file,
15187 struct line_header *lh, char *comp_dir,
15188 struct dwarf2_section_info *section,
15189 int section_is_gnu,
15190 unsigned int offset_size,
15191 struct objfile *objfile,
15192 htab_t include_hash)
15193 {
15194 enum dwarf_macro_record_type macinfo_type;
15195 int at_commandline;
15196 gdb_byte *opcode_definitions[256];
15197
15198 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15199 &offset_size, section_is_gnu);
15200 if (mac_ptr == NULL)
15201 {
15202 /* We already issued a complaint. */
15203 return;
15204 }
15205
15206 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15207 GDB is still reading the definitions from command line. First
15208 DW_MACINFO_start_file will need to be ignored as it was already executed
15209 to create CURRENT_FILE for the main source holding also the command line
15210 definitions. On first met DW_MACINFO_start_file this flag is reset to
15211 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15212
15213 at_commandline = 1;
15214
15215 do
15216 {
15217 /* Do we at least have room for a macinfo type byte? */
15218 if (mac_ptr >= mac_end)
15219 {
15220 dwarf2_macros_too_long_complaint (section);
15221 break;
15222 }
15223
15224 macinfo_type = read_1_byte (abfd, mac_ptr);
15225 mac_ptr++;
15226
15227 /* Note that we rely on the fact that the corresponding GNU and
15228 DWARF constants are the same. */
15229 switch (macinfo_type)
15230 {
15231 /* A zero macinfo type indicates the end of the macro
15232 information. */
15233 case 0:
15234 break;
15235
15236 case DW_MACRO_GNU_define:
15237 case DW_MACRO_GNU_undef:
15238 case DW_MACRO_GNU_define_indirect:
15239 case DW_MACRO_GNU_undef_indirect:
15240 {
15241 unsigned int bytes_read;
15242 int line;
15243 char *body;
15244 int is_define;
15245
15246 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15247 mac_ptr += bytes_read;
15248
15249 if (macinfo_type == DW_MACRO_GNU_define
15250 || macinfo_type == DW_MACRO_GNU_undef)
15251 {
15252 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15253 mac_ptr += bytes_read;
15254 }
15255 else
15256 {
15257 LONGEST str_offset;
15258
15259 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15260 mac_ptr += offset_size;
15261
15262 body = read_indirect_string_at_offset (abfd, str_offset);
15263 }
15264
15265 is_define = (macinfo_type == DW_MACRO_GNU_define
15266 || macinfo_type == DW_MACRO_GNU_define_indirect);
15267 if (! current_file)
15268 {
15269 /* DWARF violation as no main source is present. */
15270 complaint (&symfile_complaints,
15271 _("debug info with no main source gives macro %s "
15272 "on line %d: %s"),
15273 is_define ? _("definition") : _("undefinition"),
15274 line, body);
15275 break;
15276 }
15277 if ((line == 0 && !at_commandline)
15278 || (line != 0 && at_commandline))
15279 complaint (&symfile_complaints,
15280 _("debug info gives %s macro %s with %s line %d: %s"),
15281 at_commandline ? _("command-line") : _("in-file"),
15282 is_define ? _("definition") : _("undefinition"),
15283 line == 0 ? _("zero") : _("non-zero"), line, body);
15284
15285 if (is_define)
15286 parse_macro_definition (current_file, line, body);
15287 else
15288 {
15289 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15290 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15291 macro_undef (current_file, line, body);
15292 }
15293 }
15294 break;
15295
15296 case DW_MACRO_GNU_start_file:
15297 {
15298 unsigned int bytes_read;
15299 int line, file;
15300
15301 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15302 mac_ptr += bytes_read;
15303 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15304 mac_ptr += bytes_read;
15305
15306 if ((line == 0 && !at_commandline)
15307 || (line != 0 && at_commandline))
15308 complaint (&symfile_complaints,
15309 _("debug info gives source %d included "
15310 "from %s at %s line %d"),
15311 file, at_commandline ? _("command-line") : _("file"),
15312 line == 0 ? _("zero") : _("non-zero"), line);
15313
15314 if (at_commandline)
15315 {
15316 /* This DW_MACRO_GNU_start_file was executed in the
15317 pass one. */
15318 at_commandline = 0;
15319 }
15320 else
15321 current_file = macro_start_file (file, line,
15322 current_file, comp_dir,
15323 lh, objfile);
15324 }
15325 break;
15326
15327 case DW_MACRO_GNU_end_file:
15328 if (! current_file)
15329 complaint (&symfile_complaints,
15330 _("macro debug info has an unmatched "
15331 "`close_file' directive"));
15332 else
15333 {
15334 current_file = current_file->included_by;
15335 if (! current_file)
15336 {
15337 enum dwarf_macro_record_type next_type;
15338
15339 /* GCC circa March 2002 doesn't produce the zero
15340 type byte marking the end of the compilation
15341 unit. Complain if it's not there, but exit no
15342 matter what. */
15343
15344 /* Do we at least have room for a macinfo type byte? */
15345 if (mac_ptr >= mac_end)
15346 {
15347 dwarf2_macros_too_long_complaint (section);
15348 return;
15349 }
15350
15351 /* We don't increment mac_ptr here, so this is just
15352 a look-ahead. */
15353 next_type = read_1_byte (abfd, mac_ptr);
15354 if (next_type != 0)
15355 complaint (&symfile_complaints,
15356 _("no terminating 0-type entry for "
15357 "macros in `.debug_macinfo' section"));
15358
15359 return;
15360 }
15361 }
15362 break;
15363
15364 case DW_MACRO_GNU_transparent_include:
15365 {
15366 LONGEST offset;
15367 void **slot;
15368
15369 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15370 mac_ptr += offset_size;
15371
15372 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15373 if (*slot != NULL)
15374 {
15375 /* This has actually happened; see
15376 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15377 complaint (&symfile_complaints,
15378 _("recursive DW_MACRO_GNU_transparent_include in "
15379 ".debug_macro section"));
15380 }
15381 else
15382 {
15383 *slot = mac_ptr;
15384
15385 dwarf_decode_macro_bytes (abfd,
15386 section->buffer + offset,
15387 mac_end, current_file,
15388 lh, comp_dir,
15389 section, section_is_gnu,
15390 offset_size, objfile, include_hash);
15391
15392 htab_remove_elt (include_hash, mac_ptr);
15393 }
15394 }
15395 break;
15396
15397 case DW_MACINFO_vendor_ext:
15398 if (!section_is_gnu)
15399 {
15400 unsigned int bytes_read;
15401 int constant;
15402
15403 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15404 mac_ptr += bytes_read;
15405 read_direct_string (abfd, mac_ptr, &bytes_read);
15406 mac_ptr += bytes_read;
15407
15408 /* We don't recognize any vendor extensions. */
15409 break;
15410 }
15411 /* FALLTHROUGH */
15412
15413 default:
15414 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15415 mac_ptr, abfd, offset_size,
15416 section);
15417 if (mac_ptr == NULL)
15418 return;
15419 break;
15420 }
15421 } while (macinfo_type != 0);
15422 }
15423
15424 static void
15425 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15426 char *comp_dir, bfd *abfd,
15427 struct dwarf2_cu *cu,
15428 struct dwarf2_section_info *section,
15429 int section_is_gnu)
15430 {
15431 struct objfile *objfile = dwarf2_per_objfile->objfile;
15432 gdb_byte *mac_ptr, *mac_end;
15433 struct macro_source_file *current_file = 0;
15434 enum dwarf_macro_record_type macinfo_type;
15435 unsigned int offset_size = cu->header.offset_size;
15436 gdb_byte *opcode_definitions[256];
15437 struct cleanup *cleanup;
15438 htab_t include_hash;
15439 void **slot;
15440
15441 dwarf2_read_section (objfile, section);
15442 if (section->buffer == NULL)
15443 {
15444 complaint (&symfile_complaints, _("missing %s section"),
15445 section->asection->name);
15446 return;
15447 }
15448
15449 /* First pass: Find the name of the base filename.
15450 This filename is needed in order to process all macros whose definition
15451 (or undefinition) comes from the command line. These macros are defined
15452 before the first DW_MACINFO_start_file entry, and yet still need to be
15453 associated to the base file.
15454
15455 To determine the base file name, we scan the macro definitions until we
15456 reach the first DW_MACINFO_start_file entry. We then initialize
15457 CURRENT_FILE accordingly so that any macro definition found before the
15458 first DW_MACINFO_start_file can still be associated to the base file. */
15459
15460 mac_ptr = section->buffer + offset;
15461 mac_end = section->buffer + section->size;
15462
15463 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15464 &offset_size, section_is_gnu);
15465 if (mac_ptr == NULL)
15466 {
15467 /* We already issued a complaint. */
15468 return;
15469 }
15470
15471 do
15472 {
15473 /* Do we at least have room for a macinfo type byte? */
15474 if (mac_ptr >= mac_end)
15475 {
15476 /* Complaint is printed during the second pass as GDB will probably
15477 stop the first pass earlier upon finding
15478 DW_MACINFO_start_file. */
15479 break;
15480 }
15481
15482 macinfo_type = read_1_byte (abfd, mac_ptr);
15483 mac_ptr++;
15484
15485 /* Note that we rely on the fact that the corresponding GNU and
15486 DWARF constants are the same. */
15487 switch (macinfo_type)
15488 {
15489 /* A zero macinfo type indicates the end of the macro
15490 information. */
15491 case 0:
15492 break;
15493
15494 case DW_MACRO_GNU_define:
15495 case DW_MACRO_GNU_undef:
15496 /* Only skip the data by MAC_PTR. */
15497 {
15498 unsigned int bytes_read;
15499
15500 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15501 mac_ptr += bytes_read;
15502 read_direct_string (abfd, mac_ptr, &bytes_read);
15503 mac_ptr += bytes_read;
15504 }
15505 break;
15506
15507 case DW_MACRO_GNU_start_file:
15508 {
15509 unsigned int bytes_read;
15510 int line, file;
15511
15512 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15513 mac_ptr += bytes_read;
15514 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15515 mac_ptr += bytes_read;
15516
15517 current_file = macro_start_file (file, line, current_file,
15518 comp_dir, lh, objfile);
15519 }
15520 break;
15521
15522 case DW_MACRO_GNU_end_file:
15523 /* No data to skip by MAC_PTR. */
15524 break;
15525
15526 case DW_MACRO_GNU_define_indirect:
15527 case DW_MACRO_GNU_undef_indirect:
15528 {
15529 unsigned int bytes_read;
15530
15531 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15532 mac_ptr += bytes_read;
15533 mac_ptr += offset_size;
15534 }
15535 break;
15536
15537 case DW_MACRO_GNU_transparent_include:
15538 /* Note that, according to the spec, a transparent include
15539 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15540 skip this opcode. */
15541 mac_ptr += offset_size;
15542 break;
15543
15544 case DW_MACINFO_vendor_ext:
15545 /* Only skip the data by MAC_PTR. */
15546 if (!section_is_gnu)
15547 {
15548 unsigned int bytes_read;
15549
15550 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15551 mac_ptr += bytes_read;
15552 read_direct_string (abfd, mac_ptr, &bytes_read);
15553 mac_ptr += bytes_read;
15554 }
15555 /* FALLTHROUGH */
15556
15557 default:
15558 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15559 mac_ptr, abfd, offset_size,
15560 section);
15561 if (mac_ptr == NULL)
15562 return;
15563 break;
15564 }
15565 } while (macinfo_type != 0 && current_file == NULL);
15566
15567 /* Second pass: Process all entries.
15568
15569 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15570 command-line macro definitions/undefinitions. This flag is unset when we
15571 reach the first DW_MACINFO_start_file entry. */
15572
15573 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15574 NULL, xcalloc, xfree);
15575 cleanup = make_cleanup_htab_delete (include_hash);
15576 mac_ptr = section->buffer + offset;
15577 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15578 *slot = mac_ptr;
15579 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
15580 current_file, lh, comp_dir, section, section_is_gnu,
15581 offset_size, objfile, include_hash);
15582 do_cleanups (cleanup);
15583 }
15584
15585 /* Check if the attribute's form is a DW_FORM_block*
15586 if so return true else false. */
15587 static int
15588 attr_form_is_block (struct attribute *attr)
15589 {
15590 return (attr == NULL ? 0 :
15591 attr->form == DW_FORM_block1
15592 || attr->form == DW_FORM_block2
15593 || attr->form == DW_FORM_block4
15594 || attr->form == DW_FORM_block
15595 || attr->form == DW_FORM_exprloc);
15596 }
15597
15598 /* Return non-zero if ATTR's value is a section offset --- classes
15599 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15600 You may use DW_UNSND (attr) to retrieve such offsets.
15601
15602 Section 7.5.4, "Attribute Encodings", explains that no attribute
15603 may have a value that belongs to more than one of these classes; it
15604 would be ambiguous if we did, because we use the same forms for all
15605 of them. */
15606 static int
15607 attr_form_is_section_offset (struct attribute *attr)
15608 {
15609 return (attr->form == DW_FORM_data4
15610 || attr->form == DW_FORM_data8
15611 || attr->form == DW_FORM_sec_offset);
15612 }
15613
15614
15615 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15616 zero otherwise. When this function returns true, you can apply
15617 dwarf2_get_attr_constant_value to it.
15618
15619 However, note that for some attributes you must check
15620 attr_form_is_section_offset before using this test. DW_FORM_data4
15621 and DW_FORM_data8 are members of both the constant class, and of
15622 the classes that contain offsets into other debug sections
15623 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15624 that, if an attribute's can be either a constant or one of the
15625 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15626 taken as section offsets, not constants. */
15627 static int
15628 attr_form_is_constant (struct attribute *attr)
15629 {
15630 switch (attr->form)
15631 {
15632 case DW_FORM_sdata:
15633 case DW_FORM_udata:
15634 case DW_FORM_data1:
15635 case DW_FORM_data2:
15636 case DW_FORM_data4:
15637 case DW_FORM_data8:
15638 return 1;
15639 default:
15640 return 0;
15641 }
15642 }
15643
15644 /* A helper function that fills in a dwarf2_loclist_baton. */
15645
15646 static void
15647 fill_in_loclist_baton (struct dwarf2_cu *cu,
15648 struct dwarf2_loclist_baton *baton,
15649 struct attribute *attr)
15650 {
15651 dwarf2_read_section (dwarf2_per_objfile->objfile,
15652 &dwarf2_per_objfile->loc);
15653
15654 baton->per_cu = cu->per_cu;
15655 gdb_assert (baton->per_cu);
15656 /* We don't know how long the location list is, but make sure we
15657 don't run off the edge of the section. */
15658 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15659 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15660 baton->base_address = cu->base_address;
15661 }
15662
15663 static void
15664 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15665 struct dwarf2_cu *cu)
15666 {
15667 struct objfile *objfile = dwarf2_per_objfile->objfile;
15668
15669 if (attr_form_is_section_offset (attr)
15670 /* ".debug_loc" may not exist at all, or the offset may be outside
15671 the section. If so, fall through to the complaint in the
15672 other branch. */
15673 && DW_UNSND (attr) < dwarf2_section_size (objfile,
15674 &dwarf2_per_objfile->loc))
15675 {
15676 struct dwarf2_loclist_baton *baton;
15677
15678 baton = obstack_alloc (&objfile->objfile_obstack,
15679 sizeof (struct dwarf2_loclist_baton));
15680
15681 fill_in_loclist_baton (cu, baton, attr);
15682
15683 if (cu->base_known == 0)
15684 complaint (&symfile_complaints,
15685 _("Location list used without "
15686 "specifying the CU base address."));
15687
15688 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15689 SYMBOL_LOCATION_BATON (sym) = baton;
15690 }
15691 else
15692 {
15693 struct dwarf2_locexpr_baton *baton;
15694
15695 baton = obstack_alloc (&objfile->objfile_obstack,
15696 sizeof (struct dwarf2_locexpr_baton));
15697 baton->per_cu = cu->per_cu;
15698 gdb_assert (baton->per_cu);
15699
15700 if (attr_form_is_block (attr))
15701 {
15702 /* Note that we're just copying the block's data pointer
15703 here, not the actual data. We're still pointing into the
15704 info_buffer for SYM's objfile; right now we never release
15705 that buffer, but when we do clean up properly this may
15706 need to change. */
15707 baton->size = DW_BLOCK (attr)->size;
15708 baton->data = DW_BLOCK (attr)->data;
15709 }
15710 else
15711 {
15712 dwarf2_invalid_attrib_class_complaint ("location description",
15713 SYMBOL_NATURAL_NAME (sym));
15714 baton->size = 0;
15715 }
15716
15717 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15718 SYMBOL_LOCATION_BATON (sym) = baton;
15719 }
15720 }
15721
15722 /* Return the OBJFILE associated with the compilation unit CU. If CU
15723 came from a separate debuginfo file, then the master objfile is
15724 returned. */
15725
15726 struct objfile *
15727 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15728 {
15729 struct objfile *objfile = per_cu->objfile;
15730
15731 /* Return the master objfile, so that we can report and look up the
15732 correct file containing this variable. */
15733 if (objfile->separate_debug_objfile_backlink)
15734 objfile = objfile->separate_debug_objfile_backlink;
15735
15736 return objfile;
15737 }
15738
15739 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15740 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15741 CU_HEADERP first. */
15742
15743 static const struct comp_unit_head *
15744 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15745 struct dwarf2_per_cu_data *per_cu)
15746 {
15747 struct objfile *objfile;
15748 struct dwarf2_per_objfile *per_objfile;
15749 gdb_byte *info_ptr;
15750
15751 if (per_cu->cu)
15752 return &per_cu->cu->header;
15753
15754 objfile = per_cu->objfile;
15755 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15756 info_ptr = per_objfile->info.buffer + per_cu->offset;
15757
15758 memset (cu_headerp, 0, sizeof (*cu_headerp));
15759 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15760
15761 return cu_headerp;
15762 }
15763
15764 /* Return the address size given in the compilation unit header for CU. */
15765
15766 int
15767 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15768 {
15769 struct comp_unit_head cu_header_local;
15770 const struct comp_unit_head *cu_headerp;
15771
15772 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15773
15774 return cu_headerp->addr_size;
15775 }
15776
15777 /* Return the offset size given in the compilation unit header for CU. */
15778
15779 int
15780 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15781 {
15782 struct comp_unit_head cu_header_local;
15783 const struct comp_unit_head *cu_headerp;
15784
15785 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15786
15787 return cu_headerp->offset_size;
15788 }
15789
15790 /* See its dwarf2loc.h declaration. */
15791
15792 int
15793 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15794 {
15795 struct comp_unit_head cu_header_local;
15796 const struct comp_unit_head *cu_headerp;
15797
15798 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15799
15800 if (cu_headerp->version == 2)
15801 return cu_headerp->addr_size;
15802 else
15803 return cu_headerp->offset_size;
15804 }
15805
15806 /* Return the text offset of the CU. The returned offset comes from
15807 this CU's objfile. If this objfile came from a separate debuginfo
15808 file, then the offset may be different from the corresponding
15809 offset in the parent objfile. */
15810
15811 CORE_ADDR
15812 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15813 {
15814 struct objfile *objfile = per_cu->objfile;
15815
15816 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15817 }
15818
15819 /* Locate the .debug_info compilation unit from CU's objfile which contains
15820 the DIE at OFFSET. Raises an error on failure. */
15821
15822 static struct dwarf2_per_cu_data *
15823 dwarf2_find_containing_comp_unit (unsigned int offset,
15824 struct objfile *objfile)
15825 {
15826 struct dwarf2_per_cu_data *this_cu;
15827 int low, high;
15828
15829 low = 0;
15830 high = dwarf2_per_objfile->n_comp_units - 1;
15831 while (high > low)
15832 {
15833 int mid = low + (high - low) / 2;
15834
15835 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15836 high = mid;
15837 else
15838 low = mid + 1;
15839 }
15840 gdb_assert (low == high);
15841 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15842 {
15843 if (low == 0)
15844 error (_("Dwarf Error: could not find partial DIE containing "
15845 "offset 0x%lx [in module %s]"),
15846 (long) offset, bfd_get_filename (objfile->obfd));
15847
15848 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15849 return dwarf2_per_objfile->all_comp_units[low-1];
15850 }
15851 else
15852 {
15853 this_cu = dwarf2_per_objfile->all_comp_units[low];
15854 if (low == dwarf2_per_objfile->n_comp_units - 1
15855 && offset >= this_cu->offset + this_cu->length)
15856 error (_("invalid dwarf2 offset %u"), offset);
15857 gdb_assert (offset < this_cu->offset + this_cu->length);
15858 return this_cu;
15859 }
15860 }
15861
15862 /* Initialize dwarf2_cu CU, owned by PER_CU. */
15863
15864 static void
15865 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15866 {
15867 memset (cu, 0, sizeof (*cu));
15868 per_cu->cu = cu;
15869 cu->per_cu = per_cu;
15870 cu->objfile = per_cu->objfile;
15871 obstack_init (&cu->comp_unit_obstack);
15872 }
15873
15874 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15875
15876 static void
15877 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15878 {
15879 struct attribute *attr;
15880
15881 /* Set the language we're debugging. */
15882 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15883 if (attr)
15884 set_cu_language (DW_UNSND (attr), cu);
15885 else
15886 {
15887 cu->language = language_minimal;
15888 cu->language_defn = language_def (cu->language);
15889 }
15890 }
15891
15892 /* Release one cached compilation unit, CU. We unlink it from the tree
15893 of compilation units, but we don't remove it from the read_in_chain;
15894 the caller is responsible for that.
15895 NOTE: DATA is a void * because this function is also used as a
15896 cleanup routine. */
15897
15898 static void
15899 free_heap_comp_unit (void *data)
15900 {
15901 struct dwarf2_cu *cu = data;
15902
15903 gdb_assert (cu->per_cu != NULL);
15904 cu->per_cu->cu = NULL;
15905 cu->per_cu = NULL;
15906
15907 obstack_free (&cu->comp_unit_obstack, NULL);
15908
15909 xfree (cu);
15910 }
15911
15912 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15913 when we're finished with it. We can't free the pointer itself, but be
15914 sure to unlink it from the cache. Also release any associated storage
15915 and perform cache maintenance.
15916
15917 Only used during partial symbol parsing. */
15918
15919 static void
15920 free_stack_comp_unit (void *data)
15921 {
15922 struct dwarf2_cu *cu = data;
15923
15924 gdb_assert (cu->per_cu != NULL);
15925 cu->per_cu->cu = NULL;
15926 cu->per_cu = NULL;
15927
15928 obstack_free (&cu->comp_unit_obstack, NULL);
15929 cu->partial_dies = NULL;
15930
15931 /* The previous code only did this if per_cu != NULL.
15932 But that would always succeed, so now we just unconditionally do
15933 the aging. This seems like the wrong place to do such aging,
15934 but cleaning that up is left for later. */
15935 age_cached_comp_units ();
15936 }
15937
15938 /* Free all cached compilation units. */
15939
15940 static void
15941 free_cached_comp_units (void *data)
15942 {
15943 struct dwarf2_per_cu_data *per_cu, **last_chain;
15944
15945 per_cu = dwarf2_per_objfile->read_in_chain;
15946 last_chain = &dwarf2_per_objfile->read_in_chain;
15947 while (per_cu != NULL)
15948 {
15949 struct dwarf2_per_cu_data *next_cu;
15950
15951 next_cu = per_cu->cu->read_in_chain;
15952
15953 free_heap_comp_unit (per_cu->cu);
15954 *last_chain = next_cu;
15955
15956 per_cu = next_cu;
15957 }
15958 }
15959
15960 /* Increase the age counter on each cached compilation unit, and free
15961 any that are too old. */
15962
15963 static void
15964 age_cached_comp_units (void)
15965 {
15966 struct dwarf2_per_cu_data *per_cu, **last_chain;
15967
15968 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15969 per_cu = dwarf2_per_objfile->read_in_chain;
15970 while (per_cu != NULL)
15971 {
15972 per_cu->cu->last_used ++;
15973 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15974 dwarf2_mark (per_cu->cu);
15975 per_cu = per_cu->cu->read_in_chain;
15976 }
15977
15978 per_cu = dwarf2_per_objfile->read_in_chain;
15979 last_chain = &dwarf2_per_objfile->read_in_chain;
15980 while (per_cu != NULL)
15981 {
15982 struct dwarf2_per_cu_data *next_cu;
15983
15984 next_cu = per_cu->cu->read_in_chain;
15985
15986 if (!per_cu->cu->mark)
15987 {
15988 free_heap_comp_unit (per_cu->cu);
15989 *last_chain = next_cu;
15990 }
15991 else
15992 last_chain = &per_cu->cu->read_in_chain;
15993
15994 per_cu = next_cu;
15995 }
15996 }
15997
15998 /* Remove a single compilation unit from the cache. */
15999
16000 static void
16001 free_one_cached_comp_unit (void *target_cu)
16002 {
16003 struct dwarf2_per_cu_data *per_cu, **last_chain;
16004
16005 per_cu = dwarf2_per_objfile->read_in_chain;
16006 last_chain = &dwarf2_per_objfile->read_in_chain;
16007 while (per_cu != NULL)
16008 {
16009 struct dwarf2_per_cu_data *next_cu;
16010
16011 next_cu = per_cu->cu->read_in_chain;
16012
16013 if (per_cu->cu == target_cu)
16014 {
16015 free_heap_comp_unit (per_cu->cu);
16016 *last_chain = next_cu;
16017 break;
16018 }
16019 else
16020 last_chain = &per_cu->cu->read_in_chain;
16021
16022 per_cu = next_cu;
16023 }
16024 }
16025
16026 /* Release all extra memory associated with OBJFILE. */
16027
16028 void
16029 dwarf2_free_objfile (struct objfile *objfile)
16030 {
16031 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16032
16033 if (dwarf2_per_objfile == NULL)
16034 return;
16035
16036 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16037 free_cached_comp_units (NULL);
16038
16039 if (dwarf2_per_objfile->quick_file_names_table)
16040 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16041
16042 /* Everything else should be on the objfile obstack. */
16043 }
16044
16045 /* A pair of DIE offset and GDB type pointer. We store these
16046 in a hash table separate from the DIEs, and preserve them
16047 when the DIEs are flushed out of cache. */
16048
16049 struct dwarf2_offset_and_type
16050 {
16051 unsigned int offset;
16052 struct type *type;
16053 };
16054
16055 /* Hash function for a dwarf2_offset_and_type. */
16056
16057 static hashval_t
16058 offset_and_type_hash (const void *item)
16059 {
16060 const struct dwarf2_offset_and_type *ofs = item;
16061
16062 return ofs->offset;
16063 }
16064
16065 /* Equality function for a dwarf2_offset_and_type. */
16066
16067 static int
16068 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16069 {
16070 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16071 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16072
16073 return ofs_lhs->offset == ofs_rhs->offset;
16074 }
16075
16076 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16077 table if necessary. For convenience, return TYPE.
16078
16079 The DIEs reading must have careful ordering to:
16080 * Not cause infite loops trying to read in DIEs as a prerequisite for
16081 reading current DIE.
16082 * Not trying to dereference contents of still incompletely read in types
16083 while reading in other DIEs.
16084 * Enable referencing still incompletely read in types just by a pointer to
16085 the type without accessing its fields.
16086
16087 Therefore caller should follow these rules:
16088 * Try to fetch any prerequisite types we may need to build this DIE type
16089 before building the type and calling set_die_type.
16090 * After building type call set_die_type for current DIE as soon as
16091 possible before fetching more types to complete the current type.
16092 * Make the type as complete as possible before fetching more types. */
16093
16094 static struct type *
16095 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16096 {
16097 struct dwarf2_offset_and_type **slot, ofs;
16098 struct objfile *objfile = cu->objfile;
16099 htab_t *type_hash_ptr;
16100
16101 /* For Ada types, make sure that the gnat-specific data is always
16102 initialized (if not already set). There are a few types where
16103 we should not be doing so, because the type-specific area is
16104 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16105 where the type-specific area is used to store the floatformat).
16106 But this is not a problem, because the gnat-specific information
16107 is actually not needed for these types. */
16108 if (need_gnat_info (cu)
16109 && TYPE_CODE (type) != TYPE_CODE_FUNC
16110 && TYPE_CODE (type) != TYPE_CODE_FLT
16111 && !HAVE_GNAT_AUX_INFO (type))
16112 INIT_GNAT_SPECIFIC (type);
16113
16114 if (cu->per_cu->debug_types_section)
16115 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16116 else
16117 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16118
16119 if (*type_hash_ptr == NULL)
16120 {
16121 *type_hash_ptr
16122 = htab_create_alloc_ex (127,
16123 offset_and_type_hash,
16124 offset_and_type_eq,
16125 NULL,
16126 &objfile->objfile_obstack,
16127 hashtab_obstack_allocate,
16128 dummy_obstack_deallocate);
16129 }
16130
16131 ofs.offset = die->offset;
16132 ofs.type = type;
16133 slot = (struct dwarf2_offset_and_type **)
16134 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16135 if (*slot)
16136 complaint (&symfile_complaints,
16137 _("A problem internal to GDB: DIE 0x%x has type already set"),
16138 die->offset);
16139 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16140 **slot = ofs;
16141 return type;
16142 }
16143
16144 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16145 table, or return NULL if the die does not have a saved type. */
16146
16147 static struct type *
16148 get_die_type_at_offset (unsigned int offset,
16149 struct dwarf2_per_cu_data *per_cu)
16150 {
16151 struct dwarf2_offset_and_type *slot, ofs;
16152 htab_t type_hash;
16153
16154 if (per_cu->debug_types_section)
16155 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16156 else
16157 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16158 if (type_hash == NULL)
16159 return NULL;
16160
16161 ofs.offset = offset;
16162 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16163 if (slot)
16164 return slot->type;
16165 else
16166 return NULL;
16167 }
16168
16169 /* Look up the type for DIE in the appropriate type_hash table,
16170 or return NULL if DIE does not have a saved type. */
16171
16172 static struct type *
16173 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16174 {
16175 return get_die_type_at_offset (die->offset, cu->per_cu);
16176 }
16177
16178 /* Add a dependence relationship from CU to REF_PER_CU. */
16179
16180 static void
16181 dwarf2_add_dependence (struct dwarf2_cu *cu,
16182 struct dwarf2_per_cu_data *ref_per_cu)
16183 {
16184 void **slot;
16185
16186 if (cu->dependencies == NULL)
16187 cu->dependencies
16188 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16189 NULL, &cu->comp_unit_obstack,
16190 hashtab_obstack_allocate,
16191 dummy_obstack_deallocate);
16192
16193 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16194 if (*slot == NULL)
16195 *slot = ref_per_cu;
16196 }
16197
16198 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16199 Set the mark field in every compilation unit in the
16200 cache that we must keep because we are keeping CU. */
16201
16202 static int
16203 dwarf2_mark_helper (void **slot, void *data)
16204 {
16205 struct dwarf2_per_cu_data *per_cu;
16206
16207 per_cu = (struct dwarf2_per_cu_data *) *slot;
16208
16209 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16210 reading of the chain. As such dependencies remain valid it is not much
16211 useful to track and undo them during QUIT cleanups. */
16212 if (per_cu->cu == NULL)
16213 return 1;
16214
16215 if (per_cu->cu->mark)
16216 return 1;
16217 per_cu->cu->mark = 1;
16218
16219 if (per_cu->cu->dependencies != NULL)
16220 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16221
16222 return 1;
16223 }
16224
16225 /* Set the mark field in CU and in every other compilation unit in the
16226 cache that we must keep because we are keeping CU. */
16227
16228 static void
16229 dwarf2_mark (struct dwarf2_cu *cu)
16230 {
16231 if (cu->mark)
16232 return;
16233 cu->mark = 1;
16234 if (cu->dependencies != NULL)
16235 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16236 }
16237
16238 static void
16239 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16240 {
16241 while (per_cu)
16242 {
16243 per_cu->cu->mark = 0;
16244 per_cu = per_cu->cu->read_in_chain;
16245 }
16246 }
16247
16248 /* Trivial hash function for partial_die_info: the hash value of a DIE
16249 is its offset in .debug_info for this objfile. */
16250
16251 static hashval_t
16252 partial_die_hash (const void *item)
16253 {
16254 const struct partial_die_info *part_die = item;
16255
16256 return part_die->offset;
16257 }
16258
16259 /* Trivial comparison function for partial_die_info structures: two DIEs
16260 are equal if they have the same offset. */
16261
16262 static int
16263 partial_die_eq (const void *item_lhs, const void *item_rhs)
16264 {
16265 const struct partial_die_info *part_die_lhs = item_lhs;
16266 const struct partial_die_info *part_die_rhs = item_rhs;
16267
16268 return part_die_lhs->offset == part_die_rhs->offset;
16269 }
16270
16271 static struct cmd_list_element *set_dwarf2_cmdlist;
16272 static struct cmd_list_element *show_dwarf2_cmdlist;
16273
16274 static void
16275 set_dwarf2_cmd (char *args, int from_tty)
16276 {
16277 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16278 }
16279
16280 static void
16281 show_dwarf2_cmd (char *args, int from_tty)
16282 {
16283 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16284 }
16285
16286 /* If section described by INFO was mmapped, munmap it now. */
16287
16288 static void
16289 munmap_section_buffer (struct dwarf2_section_info *info)
16290 {
16291 if (info->map_addr != NULL)
16292 {
16293 #ifdef HAVE_MMAP
16294 int res;
16295
16296 res = munmap (info->map_addr, info->map_len);
16297 gdb_assert (res == 0);
16298 #else
16299 /* Without HAVE_MMAP, we should never be here to begin with. */
16300 gdb_assert_not_reached ("no mmap support");
16301 #endif
16302 }
16303 }
16304
16305 /* munmap debug sections for OBJFILE, if necessary. */
16306
16307 static void
16308 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16309 {
16310 struct dwarf2_per_objfile *data = d;
16311 int ix;
16312 struct dwarf2_section_info *section;
16313
16314 /* This is sorted according to the order they're defined in to make it easier
16315 to keep in sync. */
16316 munmap_section_buffer (&data->info);
16317 munmap_section_buffer (&data->abbrev);
16318 munmap_section_buffer (&data->line);
16319 munmap_section_buffer (&data->loc);
16320 munmap_section_buffer (&data->macinfo);
16321 munmap_section_buffer (&data->macro);
16322 munmap_section_buffer (&data->str);
16323 munmap_section_buffer (&data->ranges);
16324 munmap_section_buffer (&data->frame);
16325 munmap_section_buffer (&data->eh_frame);
16326 munmap_section_buffer (&data->gdb_index);
16327
16328 for (ix = 0;
16329 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16330 ++ix)
16331 munmap_section_buffer (section);
16332
16333 VEC_free (dwarf2_section_info_def, data->types);
16334 }
16335
16336 \f
16337 /* The "save gdb-index" command. */
16338
16339 /* The contents of the hash table we create when building the string
16340 table. */
16341 struct strtab_entry
16342 {
16343 offset_type offset;
16344 const char *str;
16345 };
16346
16347 /* Hash function for a strtab_entry.
16348
16349 Function is used only during write_hash_table so no index format backward
16350 compatibility is needed. */
16351
16352 static hashval_t
16353 hash_strtab_entry (const void *e)
16354 {
16355 const struct strtab_entry *entry = e;
16356 return mapped_index_string_hash (INT_MAX, entry->str);
16357 }
16358
16359 /* Equality function for a strtab_entry. */
16360
16361 static int
16362 eq_strtab_entry (const void *a, const void *b)
16363 {
16364 const struct strtab_entry *ea = a;
16365 const struct strtab_entry *eb = b;
16366 return !strcmp (ea->str, eb->str);
16367 }
16368
16369 /* Create a strtab_entry hash table. */
16370
16371 static htab_t
16372 create_strtab (void)
16373 {
16374 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16375 xfree, xcalloc, xfree);
16376 }
16377
16378 /* Add a string to the constant pool. Return the string's offset in
16379 host order. */
16380
16381 static offset_type
16382 add_string (htab_t table, struct obstack *cpool, const char *str)
16383 {
16384 void **slot;
16385 struct strtab_entry entry;
16386 struct strtab_entry *result;
16387
16388 entry.str = str;
16389 slot = htab_find_slot (table, &entry, INSERT);
16390 if (*slot)
16391 result = *slot;
16392 else
16393 {
16394 result = XNEW (struct strtab_entry);
16395 result->offset = obstack_object_size (cpool);
16396 result->str = str;
16397 obstack_grow_str0 (cpool, str);
16398 *slot = result;
16399 }
16400 return result->offset;
16401 }
16402
16403 /* An entry in the symbol table. */
16404 struct symtab_index_entry
16405 {
16406 /* The name of the symbol. */
16407 const char *name;
16408 /* The offset of the name in the constant pool. */
16409 offset_type index_offset;
16410 /* A sorted vector of the indices of all the CUs that hold an object
16411 of this name. */
16412 VEC (offset_type) *cu_indices;
16413 };
16414
16415 /* The symbol table. This is a power-of-2-sized hash table. */
16416 struct mapped_symtab
16417 {
16418 offset_type n_elements;
16419 offset_type size;
16420 struct symtab_index_entry **data;
16421 };
16422
16423 /* Hash function for a symtab_index_entry. */
16424
16425 static hashval_t
16426 hash_symtab_entry (const void *e)
16427 {
16428 const struct symtab_index_entry *entry = e;
16429 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16430 sizeof (offset_type) * VEC_length (offset_type,
16431 entry->cu_indices),
16432 0);
16433 }
16434
16435 /* Equality function for a symtab_index_entry. */
16436
16437 static int
16438 eq_symtab_entry (const void *a, const void *b)
16439 {
16440 const struct symtab_index_entry *ea = a;
16441 const struct symtab_index_entry *eb = b;
16442 int len = VEC_length (offset_type, ea->cu_indices);
16443 if (len != VEC_length (offset_type, eb->cu_indices))
16444 return 0;
16445 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16446 VEC_address (offset_type, eb->cu_indices),
16447 sizeof (offset_type) * len);
16448 }
16449
16450 /* Destroy a symtab_index_entry. */
16451
16452 static void
16453 delete_symtab_entry (void *p)
16454 {
16455 struct symtab_index_entry *entry = p;
16456 VEC_free (offset_type, entry->cu_indices);
16457 xfree (entry);
16458 }
16459
16460 /* Create a hash table holding symtab_index_entry objects. */
16461
16462 static htab_t
16463 create_symbol_hash_table (void)
16464 {
16465 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16466 delete_symtab_entry, xcalloc, xfree);
16467 }
16468
16469 /* Create a new mapped symtab object. */
16470
16471 static struct mapped_symtab *
16472 create_mapped_symtab (void)
16473 {
16474 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16475 symtab->n_elements = 0;
16476 symtab->size = 1024;
16477 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16478 return symtab;
16479 }
16480
16481 /* Destroy a mapped_symtab. */
16482
16483 static void
16484 cleanup_mapped_symtab (void *p)
16485 {
16486 struct mapped_symtab *symtab = p;
16487 /* The contents of the array are freed when the other hash table is
16488 destroyed. */
16489 xfree (symtab->data);
16490 xfree (symtab);
16491 }
16492
16493 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16494 the slot.
16495
16496 Function is used only during write_hash_table so no index format backward
16497 compatibility is needed. */
16498
16499 static struct symtab_index_entry **
16500 find_slot (struct mapped_symtab *symtab, const char *name)
16501 {
16502 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16503
16504 index = hash & (symtab->size - 1);
16505 step = ((hash * 17) & (symtab->size - 1)) | 1;
16506
16507 for (;;)
16508 {
16509 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16510 return &symtab->data[index];
16511 index = (index + step) & (symtab->size - 1);
16512 }
16513 }
16514
16515 /* Expand SYMTAB's hash table. */
16516
16517 static void
16518 hash_expand (struct mapped_symtab *symtab)
16519 {
16520 offset_type old_size = symtab->size;
16521 offset_type i;
16522 struct symtab_index_entry **old_entries = symtab->data;
16523
16524 symtab->size *= 2;
16525 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16526
16527 for (i = 0; i < old_size; ++i)
16528 {
16529 if (old_entries[i])
16530 {
16531 struct symtab_index_entry **slot = find_slot (symtab,
16532 old_entries[i]->name);
16533 *slot = old_entries[i];
16534 }
16535 }
16536
16537 xfree (old_entries);
16538 }
16539
16540 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16541 is the index of the CU in which the symbol appears. */
16542
16543 static void
16544 add_index_entry (struct mapped_symtab *symtab, const char *name,
16545 offset_type cu_index)
16546 {
16547 struct symtab_index_entry **slot;
16548
16549 ++symtab->n_elements;
16550 if (4 * symtab->n_elements / 3 >= symtab->size)
16551 hash_expand (symtab);
16552
16553 slot = find_slot (symtab, name);
16554 if (!*slot)
16555 {
16556 *slot = XNEW (struct symtab_index_entry);
16557 (*slot)->name = name;
16558 (*slot)->cu_indices = NULL;
16559 }
16560 /* Don't push an index twice. Due to how we add entries we only
16561 have to check the last one. */
16562 if (VEC_empty (offset_type, (*slot)->cu_indices)
16563 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16564 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16565 }
16566
16567 /* Add a vector of indices to the constant pool. */
16568
16569 static offset_type
16570 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16571 struct symtab_index_entry *entry)
16572 {
16573 void **slot;
16574
16575 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16576 if (!*slot)
16577 {
16578 offset_type len = VEC_length (offset_type, entry->cu_indices);
16579 offset_type val = MAYBE_SWAP (len);
16580 offset_type iter;
16581 int i;
16582
16583 *slot = entry;
16584 entry->index_offset = obstack_object_size (cpool);
16585
16586 obstack_grow (cpool, &val, sizeof (val));
16587 for (i = 0;
16588 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16589 ++i)
16590 {
16591 val = MAYBE_SWAP (iter);
16592 obstack_grow (cpool, &val, sizeof (val));
16593 }
16594 }
16595 else
16596 {
16597 struct symtab_index_entry *old_entry = *slot;
16598 entry->index_offset = old_entry->index_offset;
16599 entry = old_entry;
16600 }
16601 return entry->index_offset;
16602 }
16603
16604 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16605 constant pool entries going into the obstack CPOOL. */
16606
16607 static void
16608 write_hash_table (struct mapped_symtab *symtab,
16609 struct obstack *output, struct obstack *cpool)
16610 {
16611 offset_type i;
16612 htab_t symbol_hash_table;
16613 htab_t str_table;
16614
16615 symbol_hash_table = create_symbol_hash_table ();
16616 str_table = create_strtab ();
16617
16618 /* We add all the index vectors to the constant pool first, to
16619 ensure alignment is ok. */
16620 for (i = 0; i < symtab->size; ++i)
16621 {
16622 if (symtab->data[i])
16623 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16624 }
16625
16626 /* Now write out the hash table. */
16627 for (i = 0; i < symtab->size; ++i)
16628 {
16629 offset_type str_off, vec_off;
16630
16631 if (symtab->data[i])
16632 {
16633 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16634 vec_off = symtab->data[i]->index_offset;
16635 }
16636 else
16637 {
16638 /* While 0 is a valid constant pool index, it is not valid
16639 to have 0 for both offsets. */
16640 str_off = 0;
16641 vec_off = 0;
16642 }
16643
16644 str_off = MAYBE_SWAP (str_off);
16645 vec_off = MAYBE_SWAP (vec_off);
16646
16647 obstack_grow (output, &str_off, sizeof (str_off));
16648 obstack_grow (output, &vec_off, sizeof (vec_off));
16649 }
16650
16651 htab_delete (str_table);
16652 htab_delete (symbol_hash_table);
16653 }
16654
16655 /* Struct to map psymtab to CU index in the index file. */
16656 struct psymtab_cu_index_map
16657 {
16658 struct partial_symtab *psymtab;
16659 unsigned int cu_index;
16660 };
16661
16662 static hashval_t
16663 hash_psymtab_cu_index (const void *item)
16664 {
16665 const struct psymtab_cu_index_map *map = item;
16666
16667 return htab_hash_pointer (map->psymtab);
16668 }
16669
16670 static int
16671 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16672 {
16673 const struct psymtab_cu_index_map *lhs = item_lhs;
16674 const struct psymtab_cu_index_map *rhs = item_rhs;
16675
16676 return lhs->psymtab == rhs->psymtab;
16677 }
16678
16679 /* Helper struct for building the address table. */
16680 struct addrmap_index_data
16681 {
16682 struct objfile *objfile;
16683 struct obstack *addr_obstack;
16684 htab_t cu_index_htab;
16685
16686 /* Non-zero if the previous_* fields are valid.
16687 We can't write an entry until we see the next entry (since it is only then
16688 that we know the end of the entry). */
16689 int previous_valid;
16690 /* Index of the CU in the table of all CUs in the index file. */
16691 unsigned int previous_cu_index;
16692 /* Start address of the CU. */
16693 CORE_ADDR previous_cu_start;
16694 };
16695
16696 /* Write an address entry to OBSTACK. */
16697
16698 static void
16699 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16700 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16701 {
16702 offset_type cu_index_to_write;
16703 char addr[8];
16704 CORE_ADDR baseaddr;
16705
16706 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16707
16708 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16709 obstack_grow (obstack, addr, 8);
16710 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16711 obstack_grow (obstack, addr, 8);
16712 cu_index_to_write = MAYBE_SWAP (cu_index);
16713 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16714 }
16715
16716 /* Worker function for traversing an addrmap to build the address table. */
16717
16718 static int
16719 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16720 {
16721 struct addrmap_index_data *data = datap;
16722 struct partial_symtab *pst = obj;
16723 offset_type cu_index;
16724 void **slot;
16725
16726 if (data->previous_valid)
16727 add_address_entry (data->objfile, data->addr_obstack,
16728 data->previous_cu_start, start_addr,
16729 data->previous_cu_index);
16730
16731 data->previous_cu_start = start_addr;
16732 if (pst != NULL)
16733 {
16734 struct psymtab_cu_index_map find_map, *map;
16735 find_map.psymtab = pst;
16736 map = htab_find (data->cu_index_htab, &find_map);
16737 gdb_assert (map != NULL);
16738 data->previous_cu_index = map->cu_index;
16739 data->previous_valid = 1;
16740 }
16741 else
16742 data->previous_valid = 0;
16743
16744 return 0;
16745 }
16746
16747 /* Write OBJFILE's address map to OBSTACK.
16748 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16749 in the index file. */
16750
16751 static void
16752 write_address_map (struct objfile *objfile, struct obstack *obstack,
16753 htab_t cu_index_htab)
16754 {
16755 struct addrmap_index_data addrmap_index_data;
16756
16757 /* When writing the address table, we have to cope with the fact that
16758 the addrmap iterator only provides the start of a region; we have to
16759 wait until the next invocation to get the start of the next region. */
16760
16761 addrmap_index_data.objfile = objfile;
16762 addrmap_index_data.addr_obstack = obstack;
16763 addrmap_index_data.cu_index_htab = cu_index_htab;
16764 addrmap_index_data.previous_valid = 0;
16765
16766 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16767 &addrmap_index_data);
16768
16769 /* It's highly unlikely the last entry (end address = 0xff...ff)
16770 is valid, but we should still handle it.
16771 The end address is recorded as the start of the next region, but that
16772 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16773 anyway. */
16774 if (addrmap_index_data.previous_valid)
16775 add_address_entry (objfile, obstack,
16776 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16777 addrmap_index_data.previous_cu_index);
16778 }
16779
16780 /* Add a list of partial symbols to SYMTAB. */
16781
16782 static void
16783 write_psymbols (struct mapped_symtab *symtab,
16784 htab_t psyms_seen,
16785 struct partial_symbol **psymp,
16786 int count,
16787 offset_type cu_index,
16788 int is_static)
16789 {
16790 for (; count-- > 0; ++psymp)
16791 {
16792 void **slot, *lookup;
16793
16794 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16795 error (_("Ada is not currently supported by the index"));
16796
16797 /* We only want to add a given psymbol once. However, we also
16798 want to account for whether it is global or static. So, we
16799 may add it twice, using slightly different values. */
16800 if (is_static)
16801 {
16802 uintptr_t val = 1 | (uintptr_t) *psymp;
16803
16804 lookup = (void *) val;
16805 }
16806 else
16807 lookup = *psymp;
16808
16809 /* Only add a given psymbol once. */
16810 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16811 if (!*slot)
16812 {
16813 *slot = lookup;
16814 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16815 }
16816 }
16817 }
16818
16819 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16820 exception if there is an error. */
16821
16822 static void
16823 write_obstack (FILE *file, struct obstack *obstack)
16824 {
16825 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16826 file)
16827 != obstack_object_size (obstack))
16828 error (_("couldn't data write to file"));
16829 }
16830
16831 /* Unlink a file if the argument is not NULL. */
16832
16833 static void
16834 unlink_if_set (void *p)
16835 {
16836 char **filename = p;
16837 if (*filename)
16838 unlink (*filename);
16839 }
16840
16841 /* A helper struct used when iterating over debug_types. */
16842 struct signatured_type_index_data
16843 {
16844 struct objfile *objfile;
16845 struct mapped_symtab *symtab;
16846 struct obstack *types_list;
16847 htab_t psyms_seen;
16848 int cu_index;
16849 };
16850
16851 /* A helper function that writes a single signatured_type to an
16852 obstack. */
16853
16854 static int
16855 write_one_signatured_type (void **slot, void *d)
16856 {
16857 struct signatured_type_index_data *info = d;
16858 struct signatured_type *entry = (struct signatured_type *) *slot;
16859 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16860 struct partial_symtab *psymtab = per_cu->v.psymtab;
16861 gdb_byte val[8];
16862
16863 write_psymbols (info->symtab,
16864 info->psyms_seen,
16865 info->objfile->global_psymbols.list
16866 + psymtab->globals_offset,
16867 psymtab->n_global_syms, info->cu_index,
16868 0);
16869 write_psymbols (info->symtab,
16870 info->psyms_seen,
16871 info->objfile->static_psymbols.list
16872 + psymtab->statics_offset,
16873 psymtab->n_static_syms, info->cu_index,
16874 1);
16875
16876 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16877 obstack_grow (info->types_list, val, 8);
16878 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16879 obstack_grow (info->types_list, val, 8);
16880 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16881 obstack_grow (info->types_list, val, 8);
16882
16883 ++info->cu_index;
16884
16885 return 1;
16886 }
16887
16888 /* Create an index file for OBJFILE in the directory DIR. */
16889
16890 static void
16891 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16892 {
16893 struct cleanup *cleanup;
16894 char *filename, *cleanup_filename;
16895 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16896 struct obstack cu_list, types_cu_list;
16897 int i;
16898 FILE *out_file;
16899 struct mapped_symtab *symtab;
16900 offset_type val, size_of_contents, total_len;
16901 struct stat st;
16902 char buf[8];
16903 htab_t psyms_seen;
16904 htab_t cu_index_htab;
16905 struct psymtab_cu_index_map *psymtab_cu_index_map;
16906
16907 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16908 return;
16909
16910 if (dwarf2_per_objfile->using_index)
16911 error (_("Cannot use an index to create the index"));
16912
16913 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16914 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16915
16916 if (stat (objfile->name, &st) < 0)
16917 perror_with_name (objfile->name);
16918
16919 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16920 INDEX_SUFFIX, (char *) NULL);
16921 cleanup = make_cleanup (xfree, filename);
16922
16923 out_file = fopen (filename, "wb");
16924 if (!out_file)
16925 error (_("Can't open `%s' for writing"), filename);
16926
16927 cleanup_filename = filename;
16928 make_cleanup (unlink_if_set, &cleanup_filename);
16929
16930 symtab = create_mapped_symtab ();
16931 make_cleanup (cleanup_mapped_symtab, symtab);
16932
16933 obstack_init (&addr_obstack);
16934 make_cleanup_obstack_free (&addr_obstack);
16935
16936 obstack_init (&cu_list);
16937 make_cleanup_obstack_free (&cu_list);
16938
16939 obstack_init (&types_cu_list);
16940 make_cleanup_obstack_free (&types_cu_list);
16941
16942 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16943 NULL, xcalloc, xfree);
16944 make_cleanup_htab_delete (psyms_seen);
16945
16946 /* While we're scanning CU's create a table that maps a psymtab pointer
16947 (which is what addrmap records) to its index (which is what is recorded
16948 in the index file). This will later be needed to write the address
16949 table. */
16950 cu_index_htab = htab_create_alloc (100,
16951 hash_psymtab_cu_index,
16952 eq_psymtab_cu_index,
16953 NULL, xcalloc, xfree);
16954 make_cleanup_htab_delete (cu_index_htab);
16955 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16956 xmalloc (sizeof (struct psymtab_cu_index_map)
16957 * dwarf2_per_objfile->n_comp_units);
16958 make_cleanup (xfree, psymtab_cu_index_map);
16959
16960 /* The CU list is already sorted, so we don't need to do additional
16961 work here. Also, the debug_types entries do not appear in
16962 all_comp_units, but only in their own hash table. */
16963 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16964 {
16965 struct dwarf2_per_cu_data *per_cu
16966 = dwarf2_per_objfile->all_comp_units[i];
16967 struct partial_symtab *psymtab = per_cu->v.psymtab;
16968 gdb_byte val[8];
16969 struct psymtab_cu_index_map *map;
16970 void **slot;
16971
16972 write_psymbols (symtab,
16973 psyms_seen,
16974 objfile->global_psymbols.list + psymtab->globals_offset,
16975 psymtab->n_global_syms, i,
16976 0);
16977 write_psymbols (symtab,
16978 psyms_seen,
16979 objfile->static_psymbols.list + psymtab->statics_offset,
16980 psymtab->n_static_syms, i,
16981 1);
16982
16983 map = &psymtab_cu_index_map[i];
16984 map->psymtab = psymtab;
16985 map->cu_index = i;
16986 slot = htab_find_slot (cu_index_htab, map, INSERT);
16987 gdb_assert (slot != NULL);
16988 gdb_assert (*slot == NULL);
16989 *slot = map;
16990
16991 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16992 obstack_grow (&cu_list, val, 8);
16993 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16994 obstack_grow (&cu_list, val, 8);
16995 }
16996
16997 /* Dump the address map. */
16998 write_address_map (objfile, &addr_obstack, cu_index_htab);
16999
17000 /* Write out the .debug_type entries, if any. */
17001 if (dwarf2_per_objfile->signatured_types)
17002 {
17003 struct signatured_type_index_data sig_data;
17004
17005 sig_data.objfile = objfile;
17006 sig_data.symtab = symtab;
17007 sig_data.types_list = &types_cu_list;
17008 sig_data.psyms_seen = psyms_seen;
17009 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17010 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17011 write_one_signatured_type, &sig_data);
17012 }
17013
17014 obstack_init (&constant_pool);
17015 make_cleanup_obstack_free (&constant_pool);
17016 obstack_init (&symtab_obstack);
17017 make_cleanup_obstack_free (&symtab_obstack);
17018 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17019
17020 obstack_init (&contents);
17021 make_cleanup_obstack_free (&contents);
17022 size_of_contents = 6 * sizeof (offset_type);
17023 total_len = size_of_contents;
17024
17025 /* The version number. */
17026 val = MAYBE_SWAP (5);
17027 obstack_grow (&contents, &val, sizeof (val));
17028
17029 /* The offset of the CU list from the start of the file. */
17030 val = MAYBE_SWAP (total_len);
17031 obstack_grow (&contents, &val, sizeof (val));
17032 total_len += obstack_object_size (&cu_list);
17033
17034 /* The offset of the types CU list from the start of the file. */
17035 val = MAYBE_SWAP (total_len);
17036 obstack_grow (&contents, &val, sizeof (val));
17037 total_len += obstack_object_size (&types_cu_list);
17038
17039 /* The offset of the address table from the start of the file. */
17040 val = MAYBE_SWAP (total_len);
17041 obstack_grow (&contents, &val, sizeof (val));
17042 total_len += obstack_object_size (&addr_obstack);
17043
17044 /* The offset of the symbol table from the start of the file. */
17045 val = MAYBE_SWAP (total_len);
17046 obstack_grow (&contents, &val, sizeof (val));
17047 total_len += obstack_object_size (&symtab_obstack);
17048
17049 /* The offset of the constant pool from the start of the file. */
17050 val = MAYBE_SWAP (total_len);
17051 obstack_grow (&contents, &val, sizeof (val));
17052 total_len += obstack_object_size (&constant_pool);
17053
17054 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17055
17056 write_obstack (out_file, &contents);
17057 write_obstack (out_file, &cu_list);
17058 write_obstack (out_file, &types_cu_list);
17059 write_obstack (out_file, &addr_obstack);
17060 write_obstack (out_file, &symtab_obstack);
17061 write_obstack (out_file, &constant_pool);
17062
17063 fclose (out_file);
17064
17065 /* We want to keep the file, so we set cleanup_filename to NULL
17066 here. See unlink_if_set. */
17067 cleanup_filename = NULL;
17068
17069 do_cleanups (cleanup);
17070 }
17071
17072 /* Implementation of the `save gdb-index' command.
17073
17074 Note that the file format used by this command is documented in the
17075 GDB manual. Any changes here must be documented there. */
17076
17077 static void
17078 save_gdb_index_command (char *arg, int from_tty)
17079 {
17080 struct objfile *objfile;
17081
17082 if (!arg || !*arg)
17083 error (_("usage: save gdb-index DIRECTORY"));
17084
17085 ALL_OBJFILES (objfile)
17086 {
17087 struct stat st;
17088
17089 /* If the objfile does not correspond to an actual file, skip it. */
17090 if (stat (objfile->name, &st) < 0)
17091 continue;
17092
17093 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17094 if (dwarf2_per_objfile)
17095 {
17096 volatile struct gdb_exception except;
17097
17098 TRY_CATCH (except, RETURN_MASK_ERROR)
17099 {
17100 write_psymtabs_to_index (objfile, arg);
17101 }
17102 if (except.reason < 0)
17103 exception_fprintf (gdb_stderr, except,
17104 _("Error while writing index for `%s': "),
17105 objfile->name);
17106 }
17107 }
17108 }
17109
17110 \f
17111
17112 int dwarf2_always_disassemble;
17113
17114 static void
17115 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17116 struct cmd_list_element *c, const char *value)
17117 {
17118 fprintf_filtered (file,
17119 _("Whether to always disassemble "
17120 "DWARF expressions is %s.\n"),
17121 value);
17122 }
17123
17124 static void
17125 show_check_physname (struct ui_file *file, int from_tty,
17126 struct cmd_list_element *c, const char *value)
17127 {
17128 fprintf_filtered (file,
17129 _("Whether to check \"physname\" is %s.\n"),
17130 value);
17131 }
17132
17133 void _initialize_dwarf2_read (void);
17134
17135 void
17136 _initialize_dwarf2_read (void)
17137 {
17138 struct cmd_list_element *c;
17139
17140 dwarf2_objfile_data_key
17141 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17142
17143 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17144 Set DWARF 2 specific variables.\n\
17145 Configure DWARF 2 variables such as the cache size"),
17146 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17147 0/*allow-unknown*/, &maintenance_set_cmdlist);
17148
17149 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17150 Show DWARF 2 specific variables\n\
17151 Show DWARF 2 variables such as the cache size"),
17152 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17153 0/*allow-unknown*/, &maintenance_show_cmdlist);
17154
17155 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17156 &dwarf2_max_cache_age, _("\
17157 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17158 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17159 A higher limit means that cached compilation units will be stored\n\
17160 in memory longer, and more total memory will be used. Zero disables\n\
17161 caching, which can slow down startup."),
17162 NULL,
17163 show_dwarf2_max_cache_age,
17164 &set_dwarf2_cmdlist,
17165 &show_dwarf2_cmdlist);
17166
17167 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17168 &dwarf2_always_disassemble, _("\
17169 Set whether `info address' always disassembles DWARF expressions."), _("\
17170 Show whether `info address' always disassembles DWARF expressions."), _("\
17171 When enabled, DWARF expressions are always printed in an assembly-like\n\
17172 syntax. When disabled, expressions will be printed in a more\n\
17173 conversational style, when possible."),
17174 NULL,
17175 show_dwarf2_always_disassemble,
17176 &set_dwarf2_cmdlist,
17177 &show_dwarf2_cmdlist);
17178
17179 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17180 Set debugging of the dwarf2 DIE reader."), _("\
17181 Show debugging of the dwarf2 DIE reader."), _("\
17182 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17183 The value is the maximum depth to print."),
17184 NULL,
17185 NULL,
17186 &setdebuglist, &showdebuglist);
17187
17188 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17189 Set cross-checking of \"physname\" code against demangler."), _("\
17190 Show cross-checking of \"physname\" code against demangler."), _("\
17191 When enabled, GDB's internal \"physname\" code is checked against\n\
17192 the demangler."),
17193 NULL, show_check_physname,
17194 &setdebuglist, &showdebuglist);
17195
17196 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17197 _("\
17198 Save a gdb-index file.\n\
17199 Usage: save gdb-index DIRECTORY"),
17200 &save_cmdlist);
17201 set_cmd_completer (c, filename_completer);
17202 }