2012-01-27 Pedro Alves <palves@redhat.com>
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "symtab.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h" /* for DOSish file names */
42 #include "macrotab.h"
43 #include "language.h"
44 #include "complaints.h"
45 #include "bcache.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
49 #include "hashtab.h"
50 #include "command.h"
51 #include "gdbcmd.h"
52 #include "block.h"
53 #include "addrmap.h"
54 #include "typeprint.h"
55 #include "jv-lang.h"
56 #include "psympriv.h"
57 #include "exceptions.h"
58 #include "gdb_stat.h"
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "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 flag will be set if this compilation unit might include
382 inter-compilation-unit references. */
383 unsigned int has_form_ref_addr : 1;
384
385 /* This flag will be set if this compilation unit includes any
386 DW_TAG_namespace DIEs. If we know that there are explicit
387 DIEs for namespaces, we don't need to try to infer them
388 from mangled names. */
389 unsigned int has_namespace_info : 1;
390
391 /* This CU references .debug_loc. See the symtab->locations_valid field.
392 This test is imperfect as there may exist optimized debug code not using
393 any location list and still facing inlining issues if handled as
394 unoptimized code. For a future better test see GCC PR other/32998. */
395 unsigned int has_loclist : 1;
396 };
397
398 /* Persistent data held for a compilation unit, even when not
399 processing it. We put a pointer to this structure in the
400 read_symtab_private field of the psymtab. */
401
402 struct dwarf2_per_cu_data
403 {
404 /* The start offset and length of this compilation unit. 2**29-1
405 bytes should suffice to store the length of any compilation unit
406 - if it doesn't, GDB will fall over anyway.
407 NOTE: Unlike comp_unit_head.length, this length includes
408 initial_length_size. */
409 unsigned int offset;
410 unsigned int length : 29;
411
412 /* Flag indicating this compilation unit will be read in before
413 any of the current compilation units are processed. */
414 unsigned int queued : 1;
415
416 /* This flag will be set if we need to load absolutely all DIEs
417 for this compilation unit, instead of just the ones we think
418 are interesting. It gets set if we look for a DIE in the
419 hash table and don't find it. */
420 unsigned int load_all_dies : 1;
421
422 /* Non-null if this CU is from .debug_types; in which case it points
423 to the section. Otherwise it's from .debug_info. */
424 struct dwarf2_section_info *debug_types_section;
425
426 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
427 of the CU cache it gets reset to NULL again. */
428 struct dwarf2_cu *cu;
429
430 /* The corresponding objfile.
431 Normally we can get the objfile from dwarf2_per_objfile.
432 However we can enter this file with just a "per_cu" handle. */
433 struct objfile *objfile;
434
435 /* When using partial symbol tables, the 'psymtab' field is active.
436 Otherwise the 'quick' field is active. */
437 union
438 {
439 /* The partial symbol table associated with this compilation unit,
440 or NULL for partial units (which do not have an associated
441 symtab). */
442 struct partial_symtab *psymtab;
443
444 /* Data needed by the "quick" functions. */
445 struct dwarf2_per_cu_quick_data *quick;
446 } v;
447 };
448
449 /* Entry in the signatured_types hash table. */
450
451 struct signatured_type
452 {
453 ULONGEST signature;
454
455 /* Offset in .debug_types of the type defined by this TU. */
456 unsigned int type_offset;
457
458 /* The CU(/TU) of this type. */
459 struct dwarf2_per_cu_data per_cu;
460 };
461
462 /* Struct used to pass misc. parameters to read_die_and_children, et
463 al. which are used for both .debug_info and .debug_types dies.
464 All parameters here are unchanging for the life of the call. This
465 struct exists to abstract away the constant parameters of die
466 reading. */
467
468 struct die_reader_specs
469 {
470 /* The bfd of this objfile. */
471 bfd* abfd;
472
473 /* The CU of the DIE we are parsing. */
474 struct dwarf2_cu *cu;
475
476 /* Pointer to start of section buffer.
477 This is either the start of .debug_info or .debug_types. */
478 const gdb_byte *buffer;
479 };
480
481 /* The line number information for a compilation unit (found in the
482 .debug_line section) begins with a "statement program header",
483 which contains the following information. */
484 struct line_header
485 {
486 unsigned int total_length;
487 unsigned short version;
488 unsigned int header_length;
489 unsigned char minimum_instruction_length;
490 unsigned char maximum_ops_per_instruction;
491 unsigned char default_is_stmt;
492 int line_base;
493 unsigned char line_range;
494 unsigned char opcode_base;
495
496 /* standard_opcode_lengths[i] is the number of operands for the
497 standard opcode whose value is i. This means that
498 standard_opcode_lengths[0] is unused, and the last meaningful
499 element is standard_opcode_lengths[opcode_base - 1]. */
500 unsigned char *standard_opcode_lengths;
501
502 /* The include_directories table. NOTE! These strings are not
503 allocated with xmalloc; instead, they are pointers into
504 debug_line_buffer. If you try to free them, `free' will get
505 indigestion. */
506 unsigned int num_include_dirs, include_dirs_size;
507 char **include_dirs;
508
509 /* The file_names table. NOTE! These strings are not allocated
510 with xmalloc; instead, they are pointers into debug_line_buffer.
511 Don't try to free them directly. */
512 unsigned int num_file_names, file_names_size;
513 struct file_entry
514 {
515 char *name;
516 unsigned int dir_index;
517 unsigned int mod_time;
518 unsigned int length;
519 int included_p; /* Non-zero if referenced by the Line Number Program. */
520 struct symtab *symtab; /* The associated symbol table, if any. */
521 } *file_names;
522
523 /* The start and end of the statement program following this
524 header. These point into dwarf2_per_objfile->line_buffer. */
525 gdb_byte *statement_program_start, *statement_program_end;
526 };
527
528 /* When we construct a partial symbol table entry we only
529 need this much information. */
530 struct partial_die_info
531 {
532 /* Offset of this DIE. */
533 unsigned int offset;
534
535 /* DWARF-2 tag for this DIE. */
536 ENUM_BITFIELD(dwarf_tag) tag : 16;
537
538 /* Assorted flags describing the data found in this DIE. */
539 unsigned int has_children : 1;
540 unsigned int is_external : 1;
541 unsigned int is_declaration : 1;
542 unsigned int has_type : 1;
543 unsigned int has_specification : 1;
544 unsigned int has_pc_info : 1;
545
546 /* Flag set if the SCOPE field of this structure has been
547 computed. */
548 unsigned int scope_set : 1;
549
550 /* Flag set if the DIE has a byte_size attribute. */
551 unsigned int has_byte_size : 1;
552
553 /* Flag set if any of the DIE's children are template arguments. */
554 unsigned int has_template_arguments : 1;
555
556 /* Flag set if fixup_partial_die has been called on this die. */
557 unsigned int fixup_called : 1;
558
559 /* The name of this DIE. Normally the value of DW_AT_name, but
560 sometimes a default name for unnamed DIEs. */
561 char *name;
562
563 /* The linkage name, if present. */
564 const char *linkage_name;
565
566 /* The scope to prepend to our children. This is generally
567 allocated on the comp_unit_obstack, so will disappear
568 when this compilation unit leaves the cache. */
569 char *scope;
570
571 /* The location description associated with this DIE, if any. */
572 struct dwarf_block *locdesc;
573
574 /* If HAS_PC_INFO, the PC range associated with this DIE. */
575 CORE_ADDR lowpc;
576 CORE_ADDR highpc;
577
578 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
579 DW_AT_sibling, if any. */
580 /* NOTE: This member isn't strictly necessary, read_partial_die could
581 return DW_AT_sibling values to its caller load_partial_dies. */
582 gdb_byte *sibling;
583
584 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
585 DW_AT_specification (or DW_AT_abstract_origin or
586 DW_AT_extension). */
587 unsigned int spec_offset;
588
589 /* Pointers to this DIE's parent, first child, and next sibling,
590 if any. */
591 struct partial_die_info *die_parent, *die_child, *die_sibling;
592 };
593
594 /* This data structure holds the information of an abbrev. */
595 struct abbrev_info
596 {
597 unsigned int number; /* number identifying abbrev */
598 enum dwarf_tag tag; /* dwarf tag */
599 unsigned short has_children; /* boolean */
600 unsigned short num_attrs; /* number of attributes */
601 struct attr_abbrev *attrs; /* an array of attribute descriptions */
602 struct abbrev_info *next; /* next in chain */
603 };
604
605 struct attr_abbrev
606 {
607 ENUM_BITFIELD(dwarf_attribute) name : 16;
608 ENUM_BITFIELD(dwarf_form) form : 16;
609 };
610
611 /* Attributes have a name and a value. */
612 struct attribute
613 {
614 ENUM_BITFIELD(dwarf_attribute) name : 16;
615 ENUM_BITFIELD(dwarf_form) form : 15;
616
617 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
618 field should be in u.str (existing only for DW_STRING) but it is kept
619 here for better struct attribute alignment. */
620 unsigned int string_is_canonical : 1;
621
622 union
623 {
624 char *str;
625 struct dwarf_block *blk;
626 ULONGEST unsnd;
627 LONGEST snd;
628 CORE_ADDR addr;
629 struct signatured_type *signatured_type;
630 }
631 u;
632 };
633
634 /* This data structure holds a complete die structure. */
635 struct die_info
636 {
637 /* DWARF-2 tag for this DIE. */
638 ENUM_BITFIELD(dwarf_tag) tag : 16;
639
640 /* Number of attributes */
641 unsigned char num_attrs;
642
643 /* True if we're presently building the full type name for the
644 type derived from this DIE. */
645 unsigned char building_fullname : 1;
646
647 /* Abbrev number */
648 unsigned int abbrev;
649
650 /* Offset in .debug_info or .debug_types section. */
651 unsigned int offset;
652
653 /* The dies in a compilation unit form an n-ary tree. PARENT
654 points to this die's parent; CHILD points to the first child of
655 this node; and all the children of a given node are chained
656 together via their SIBLING fields. */
657 struct die_info *child; /* Its first child, if any. */
658 struct die_info *sibling; /* Its next sibling, if any. */
659 struct die_info *parent; /* Its parent, if any. */
660
661 /* An array of attributes, with NUM_ATTRS elements. There may be
662 zero, but it's not common and zero-sized arrays are not
663 sufficiently portable C. */
664 struct attribute attrs[1];
665 };
666
667 /* Get at parts of an attribute structure. */
668
669 #define DW_STRING(attr) ((attr)->u.str)
670 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
671 #define DW_UNSND(attr) ((attr)->u.unsnd)
672 #define DW_BLOCK(attr) ((attr)->u.blk)
673 #define DW_SND(attr) ((attr)->u.snd)
674 #define DW_ADDR(attr) ((attr)->u.addr)
675 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
676
677 /* Blocks are a bunch of untyped bytes. */
678 struct dwarf_block
679 {
680 unsigned int size;
681
682 /* Valid only if SIZE is not zero. */
683 gdb_byte *data;
684 };
685
686 #ifndef ATTR_ALLOC_CHUNK
687 #define ATTR_ALLOC_CHUNK 4
688 #endif
689
690 /* Allocate fields for structs, unions and enums in this size. */
691 #ifndef DW_FIELD_ALLOC_CHUNK
692 #define DW_FIELD_ALLOC_CHUNK 4
693 #endif
694
695 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
696 but this would require a corresponding change in unpack_field_as_long
697 and friends. */
698 static int bits_per_byte = 8;
699
700 /* The routines that read and process dies for a C struct or C++ class
701 pass lists of data member fields and lists of member function fields
702 in an instance of a field_info structure, as defined below. */
703 struct field_info
704 {
705 /* List of data member and baseclasses fields. */
706 struct nextfield
707 {
708 struct nextfield *next;
709 int accessibility;
710 int virtuality;
711 struct field field;
712 }
713 *fields, *baseclasses;
714
715 /* Number of fields (including baseclasses). */
716 int nfields;
717
718 /* Number of baseclasses. */
719 int nbaseclasses;
720
721 /* Set if the accesibility of one of the fields is not public. */
722 int non_public_fields;
723
724 /* Member function fields array, entries are allocated in the order they
725 are encountered in the object file. */
726 struct nextfnfield
727 {
728 struct nextfnfield *next;
729 struct fn_field fnfield;
730 }
731 *fnfields;
732
733 /* Member function fieldlist array, contains name of possibly overloaded
734 member function, number of overloaded member functions and a pointer
735 to the head of the member function field chain. */
736 struct fnfieldlist
737 {
738 char *name;
739 int length;
740 struct nextfnfield *head;
741 }
742 *fnfieldlists;
743
744 /* Number of entries in the fnfieldlists array. */
745 int nfnfields;
746
747 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
748 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
749 struct typedef_field_list
750 {
751 struct typedef_field field;
752 struct typedef_field_list *next;
753 }
754 *typedef_field_list;
755 unsigned typedef_field_list_count;
756 };
757
758 /* One item on the queue of compilation units to read in full symbols
759 for. */
760 struct dwarf2_queue_item
761 {
762 struct dwarf2_per_cu_data *per_cu;
763 struct dwarf2_queue_item *next;
764 };
765
766 /* The current queue. */
767 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
768
769 /* Loaded secondary compilation units are kept in memory until they
770 have not been referenced for the processing of this many
771 compilation units. Set this to zero to disable caching. Cache
772 sizes of up to at least twenty will improve startup time for
773 typical inter-CU-reference binaries, at an obvious memory cost. */
774 static int dwarf2_max_cache_age = 5;
775 static void
776 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
777 struct cmd_list_element *c, const char *value)
778 {
779 fprintf_filtered (file, _("The upper bound on the age of cached "
780 "dwarf2 compilation units is %s.\n"),
781 value);
782 }
783
784
785 /* Various complaints about symbol reading that don't abort the process. */
786
787 static void
788 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
789 {
790 complaint (&symfile_complaints,
791 _("statement list doesn't fit in .debug_line section"));
792 }
793
794 static void
795 dwarf2_debug_line_missing_file_complaint (void)
796 {
797 complaint (&symfile_complaints,
798 _(".debug_line section has line data without a file"));
799 }
800
801 static void
802 dwarf2_debug_line_missing_end_sequence_complaint (void)
803 {
804 complaint (&symfile_complaints,
805 _(".debug_line section has line "
806 "program sequence without an end"));
807 }
808
809 static void
810 dwarf2_complex_location_expr_complaint (void)
811 {
812 complaint (&symfile_complaints, _("location expression too complex"));
813 }
814
815 static void
816 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
817 int arg3)
818 {
819 complaint (&symfile_complaints,
820 _("const value length mismatch for '%s', got %d, expected %d"),
821 arg1, arg2, arg3);
822 }
823
824 static void
825 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
826 {
827 complaint (&symfile_complaints,
828 _("macro info runs off end of `%s' section"),
829 section->asection->name);
830 }
831
832 static void
833 dwarf2_macro_malformed_definition_complaint (const char *arg1)
834 {
835 complaint (&symfile_complaints,
836 _("macro debug info contains a "
837 "malformed macro definition:\n`%s'"),
838 arg1);
839 }
840
841 static void
842 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
843 {
844 complaint (&symfile_complaints,
845 _("invalid attribute class or form for '%s' in '%s'"),
846 arg1, arg2);
847 }
848
849 /* local function prototypes */
850
851 static void dwarf2_locate_sections (bfd *, asection *, void *);
852
853 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
854 struct objfile *);
855
856 static void dwarf2_find_base_address (struct die_info *die,
857 struct dwarf2_cu *cu);
858
859 static void dwarf2_build_psymtabs_hard (struct objfile *);
860
861 static void scan_partial_symbols (struct partial_die_info *,
862 CORE_ADDR *, CORE_ADDR *,
863 int, struct dwarf2_cu *);
864
865 static void add_partial_symbol (struct partial_die_info *,
866 struct dwarf2_cu *);
867
868 static void add_partial_namespace (struct partial_die_info *pdi,
869 CORE_ADDR *lowpc, CORE_ADDR *highpc,
870 int need_pc, struct dwarf2_cu *cu);
871
872 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
873 CORE_ADDR *highpc, int need_pc,
874 struct dwarf2_cu *cu);
875
876 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
877 struct dwarf2_cu *cu);
878
879 static void add_partial_subprogram (struct partial_die_info *pdi,
880 CORE_ADDR *lowpc, CORE_ADDR *highpc,
881 int need_pc, struct dwarf2_cu *cu);
882
883 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
884 gdb_byte *buffer, gdb_byte *info_ptr,
885 bfd *abfd, struct dwarf2_cu *cu);
886
887 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
888
889 static void psymtab_to_symtab_1 (struct partial_symtab *);
890
891 static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
892
893 static void dwarf2_free_abbrev_table (void *);
894
895 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
896
897 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
898 struct dwarf2_cu *);
899
900 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
901 struct dwarf2_cu *);
902
903 static struct partial_die_info *load_partial_dies (bfd *,
904 gdb_byte *, gdb_byte *,
905 int, struct dwarf2_cu *);
906
907 static gdb_byte *read_partial_die (struct partial_die_info *,
908 struct abbrev_info *abbrev,
909 unsigned int, bfd *,
910 gdb_byte *, gdb_byte *,
911 struct dwarf2_cu *);
912
913 static struct partial_die_info *find_partial_die (unsigned int,
914 struct dwarf2_cu *);
915
916 static void fixup_partial_die (struct partial_die_info *,
917 struct dwarf2_cu *);
918
919 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
920 bfd *, gdb_byte *, struct dwarf2_cu *);
921
922 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
923 bfd *, gdb_byte *, struct dwarf2_cu *);
924
925 static unsigned int read_1_byte (bfd *, gdb_byte *);
926
927 static int read_1_signed_byte (bfd *, gdb_byte *);
928
929 static unsigned int read_2_bytes (bfd *, gdb_byte *);
930
931 static unsigned int read_4_bytes (bfd *, gdb_byte *);
932
933 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
934
935 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
936 unsigned int *);
937
938 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
939
940 static LONGEST read_checked_initial_length_and_offset
941 (bfd *, gdb_byte *, const struct comp_unit_head *,
942 unsigned int *, unsigned int *);
943
944 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
945 unsigned int *);
946
947 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
948
949 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
950
951 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
952
953 static char *read_indirect_string (bfd *, gdb_byte *,
954 const struct comp_unit_head *,
955 unsigned int *);
956
957 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
958
959 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
960
961 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
962
963 static void set_cu_language (unsigned int, struct dwarf2_cu *);
964
965 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
966 struct dwarf2_cu *);
967
968 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
969 unsigned int,
970 struct dwarf2_cu *);
971
972 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
973 struct dwarf2_cu *cu);
974
975 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
976
977 static struct die_info *die_specification (struct die_info *die,
978 struct dwarf2_cu **);
979
980 static void free_line_header (struct line_header *lh);
981
982 static void add_file_name (struct line_header *, char *, unsigned int,
983 unsigned int, unsigned int);
984
985 static struct line_header *(dwarf_decode_line_header
986 (unsigned int offset,
987 bfd *abfd, struct dwarf2_cu *cu));
988
989 static void dwarf_decode_lines (struct line_header *, const char *,
990 struct dwarf2_cu *, struct partial_symtab *,
991 int);
992
993 static void dwarf2_start_subfile (char *, const char *, const char *);
994
995 static struct symbol *new_symbol (struct die_info *, struct type *,
996 struct dwarf2_cu *);
997
998 static struct symbol *new_symbol_full (struct die_info *, struct type *,
999 struct dwarf2_cu *, struct symbol *);
1000
1001 static void dwarf2_const_value (struct attribute *, struct symbol *,
1002 struct dwarf2_cu *);
1003
1004 static void dwarf2_const_value_attr (struct attribute *attr,
1005 struct type *type,
1006 const char *name,
1007 struct obstack *obstack,
1008 struct dwarf2_cu *cu, long *value,
1009 gdb_byte **bytes,
1010 struct dwarf2_locexpr_baton **baton);
1011
1012 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1013
1014 static int need_gnat_info (struct dwarf2_cu *);
1015
1016 static struct type *die_descriptive_type (struct die_info *,
1017 struct dwarf2_cu *);
1018
1019 static void set_descriptive_type (struct type *, struct die_info *,
1020 struct dwarf2_cu *);
1021
1022 static struct type *die_containing_type (struct die_info *,
1023 struct dwarf2_cu *);
1024
1025 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1026 struct dwarf2_cu *);
1027
1028 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1029
1030 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1031
1032 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1033
1034 static char *typename_concat (struct obstack *obs, const char *prefix,
1035 const char *suffix, int physname,
1036 struct dwarf2_cu *cu);
1037
1038 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1039
1040 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1041
1042 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1043
1044 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1045
1046 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1047
1048 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1049 struct dwarf2_cu *, struct partial_symtab *);
1050
1051 static int dwarf2_get_pc_bounds (struct die_info *,
1052 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1053 struct partial_symtab *);
1054
1055 static void get_scope_pc_bounds (struct die_info *,
1056 CORE_ADDR *, CORE_ADDR *,
1057 struct dwarf2_cu *);
1058
1059 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1060 CORE_ADDR, struct dwarf2_cu *);
1061
1062 static void dwarf2_add_field (struct field_info *, struct die_info *,
1063 struct dwarf2_cu *);
1064
1065 static void dwarf2_attach_fields_to_type (struct field_info *,
1066 struct type *, struct dwarf2_cu *);
1067
1068 static void dwarf2_add_member_fn (struct field_info *,
1069 struct die_info *, struct type *,
1070 struct dwarf2_cu *);
1071
1072 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1073 struct type *,
1074 struct dwarf2_cu *);
1075
1076 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1077
1078 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1079
1080 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1081
1082 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1083
1084 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1085
1086 static struct type *read_module_type (struct die_info *die,
1087 struct dwarf2_cu *cu);
1088
1089 static const char *namespace_name (struct die_info *die,
1090 int *is_anonymous, struct dwarf2_cu *);
1091
1092 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1093
1094 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1095
1096 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1097 struct dwarf2_cu *);
1098
1099 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1100
1101 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1102 gdb_byte *info_ptr,
1103 gdb_byte **new_info_ptr,
1104 struct die_info *parent);
1105
1106 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1107 gdb_byte *info_ptr,
1108 gdb_byte **new_info_ptr,
1109 struct die_info *parent);
1110
1111 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1112 gdb_byte *info_ptr,
1113 gdb_byte **new_info_ptr,
1114 struct die_info *parent);
1115
1116 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1117 struct die_info **, gdb_byte *,
1118 int *);
1119
1120 static void process_die (struct die_info *, struct dwarf2_cu *);
1121
1122 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1123 struct obstack *);
1124
1125 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1126
1127 static const char *dwarf2_full_name (char *name,
1128 struct die_info *die,
1129 struct dwarf2_cu *cu);
1130
1131 static struct die_info *dwarf2_extension (struct die_info *die,
1132 struct dwarf2_cu **);
1133
1134 static char *dwarf_tag_name (unsigned int);
1135
1136 static char *dwarf_attr_name (unsigned int);
1137
1138 static char *dwarf_form_name (unsigned int);
1139
1140 static char *dwarf_bool_name (unsigned int);
1141
1142 static char *dwarf_type_encoding_name (unsigned int);
1143
1144 #if 0
1145 static char *dwarf_cfi_name (unsigned int);
1146 #endif
1147
1148 static struct die_info *sibling_die (struct die_info *);
1149
1150 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1151
1152 static void dump_die_for_error (struct die_info *);
1153
1154 static void dump_die_1 (struct ui_file *, int level, int max_level,
1155 struct die_info *);
1156
1157 /*static*/ void dump_die (struct die_info *, int max_level);
1158
1159 static void store_in_ref_table (struct die_info *,
1160 struct dwarf2_cu *);
1161
1162 static int is_ref_attr (struct attribute *);
1163
1164 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1165
1166 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1167
1168 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1169 struct attribute *,
1170 struct dwarf2_cu **);
1171
1172 static struct die_info *follow_die_ref (struct die_info *,
1173 struct attribute *,
1174 struct dwarf2_cu **);
1175
1176 static struct die_info *follow_die_sig (struct die_info *,
1177 struct attribute *,
1178 struct dwarf2_cu **);
1179
1180 static struct signatured_type *lookup_signatured_type_at_offset
1181 (struct objfile *objfile,
1182 struct dwarf2_section_info *section,
1183 unsigned int offset);
1184
1185 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1186
1187 static void read_signatured_type (struct signatured_type *type_sig);
1188
1189 /* memory allocation interface */
1190
1191 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1192
1193 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1194
1195 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1196
1197 static void dwarf_decode_macros (struct line_header *, unsigned int,
1198 char *, bfd *, struct dwarf2_cu *,
1199 struct dwarf2_section_info *,
1200 int);
1201
1202 static int attr_form_is_block (struct attribute *);
1203
1204 static int attr_form_is_section_offset (struct attribute *);
1205
1206 static int attr_form_is_constant (struct attribute *);
1207
1208 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1209 struct dwarf2_loclist_baton *baton,
1210 struct attribute *attr);
1211
1212 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1213 struct symbol *sym,
1214 struct dwarf2_cu *cu);
1215
1216 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1217 struct abbrev_info *abbrev,
1218 struct dwarf2_cu *cu);
1219
1220 static void free_stack_comp_unit (void *);
1221
1222 static hashval_t partial_die_hash (const void *item);
1223
1224 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1225
1226 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1227 (unsigned int offset, struct objfile *objfile);
1228
1229 static void init_one_comp_unit (struct dwarf2_cu *cu,
1230 struct dwarf2_per_cu_data *per_cu);
1231
1232 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1233 struct die_info *comp_unit_die);
1234
1235 static void free_heap_comp_unit (void *);
1236
1237 static void free_cached_comp_units (void *);
1238
1239 static void age_cached_comp_units (void);
1240
1241 static void free_one_cached_comp_unit (void *);
1242
1243 static struct type *set_die_type (struct die_info *, struct type *,
1244 struct dwarf2_cu *);
1245
1246 static void create_all_comp_units (struct objfile *);
1247
1248 static int create_debug_types_hash_table (struct objfile *objfile);
1249
1250 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1251
1252 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1253
1254 static void dwarf2_add_dependence (struct dwarf2_cu *,
1255 struct dwarf2_per_cu_data *);
1256
1257 static void dwarf2_mark (struct dwarf2_cu *);
1258
1259 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1260
1261 static struct type *get_die_type_at_offset (unsigned int,
1262 struct dwarf2_per_cu_data *per_cu);
1263
1264 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1265
1266 static void dwarf2_release_queue (void *dummy);
1267
1268 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1269
1270 static void process_queue (void);
1271
1272 static void find_file_and_directory (struct die_info *die,
1273 struct dwarf2_cu *cu,
1274 char **name, char **comp_dir);
1275
1276 static char *file_full_name (int file, struct line_header *lh,
1277 const char *comp_dir);
1278
1279 static gdb_byte *read_and_check_comp_unit_head
1280 (struct comp_unit_head *header,
1281 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1282 int is_debug_types_section);
1283
1284 static void init_cu_die_reader (struct die_reader_specs *reader,
1285 struct dwarf2_cu *cu);
1286
1287 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1288
1289 #if WORDS_BIGENDIAN
1290
1291 /* Convert VALUE between big- and little-endian. */
1292 static offset_type
1293 byte_swap (offset_type value)
1294 {
1295 offset_type result;
1296
1297 result = (value & 0xff) << 24;
1298 result |= (value & 0xff00) << 8;
1299 result |= (value & 0xff0000) >> 8;
1300 result |= (value & 0xff000000) >> 24;
1301 return result;
1302 }
1303
1304 #define MAYBE_SWAP(V) byte_swap (V)
1305
1306 #else
1307 #define MAYBE_SWAP(V) (V)
1308 #endif /* WORDS_BIGENDIAN */
1309
1310 /* The suffix for an index file. */
1311 #define INDEX_SUFFIX ".gdb-index"
1312
1313 static const char *dwarf2_physname (char *name, struct die_info *die,
1314 struct dwarf2_cu *cu);
1315
1316 /* Try to locate the sections we need for DWARF 2 debugging
1317 information and return true if we have enough to do something.
1318 NAMES points to the dwarf2 section names, or is NULL if the standard
1319 ELF names are used. */
1320
1321 int
1322 dwarf2_has_info (struct objfile *objfile,
1323 const struct dwarf2_debug_sections *names)
1324 {
1325 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1326 if (!dwarf2_per_objfile)
1327 {
1328 /* Initialize per-objfile state. */
1329 struct dwarf2_per_objfile *data
1330 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1331
1332 memset (data, 0, sizeof (*data));
1333 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1334 dwarf2_per_objfile = data;
1335
1336 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1337 (void *) names);
1338 dwarf2_per_objfile->objfile = objfile;
1339 }
1340 return (dwarf2_per_objfile->info.asection != NULL
1341 && dwarf2_per_objfile->abbrev.asection != NULL);
1342 }
1343
1344 /* When loading sections, we look either for uncompressed section or for
1345 compressed section names. */
1346
1347 static int
1348 section_is_p (const char *section_name,
1349 const struct dwarf2_section_names *names)
1350 {
1351 if (names->normal != NULL
1352 && strcmp (section_name, names->normal) == 0)
1353 return 1;
1354 if (names->compressed != NULL
1355 && strcmp (section_name, names->compressed) == 0)
1356 return 1;
1357 return 0;
1358 }
1359
1360 /* This function is mapped across the sections and remembers the
1361 offset and size of each of the debugging sections we are interested
1362 in. */
1363
1364 static void
1365 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1366 {
1367 const struct dwarf2_debug_sections *names;
1368
1369 if (vnames == NULL)
1370 names = &dwarf2_elf_names;
1371 else
1372 names = (const struct dwarf2_debug_sections *) vnames;
1373
1374 if (section_is_p (sectp->name, &names->info))
1375 {
1376 dwarf2_per_objfile->info.asection = sectp;
1377 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1378 }
1379 else if (section_is_p (sectp->name, &names->abbrev))
1380 {
1381 dwarf2_per_objfile->abbrev.asection = sectp;
1382 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1383 }
1384 else if (section_is_p (sectp->name, &names->line))
1385 {
1386 dwarf2_per_objfile->line.asection = sectp;
1387 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1388 }
1389 else if (section_is_p (sectp->name, &names->loc))
1390 {
1391 dwarf2_per_objfile->loc.asection = sectp;
1392 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1393 }
1394 else if (section_is_p (sectp->name, &names->macinfo))
1395 {
1396 dwarf2_per_objfile->macinfo.asection = sectp;
1397 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1398 }
1399 else if (section_is_p (sectp->name, &names->macro))
1400 {
1401 dwarf2_per_objfile->macro.asection = sectp;
1402 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1403 }
1404 else if (section_is_p (sectp->name, &names->str))
1405 {
1406 dwarf2_per_objfile->str.asection = sectp;
1407 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1408 }
1409 else if (section_is_p (sectp->name, &names->frame))
1410 {
1411 dwarf2_per_objfile->frame.asection = sectp;
1412 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1413 }
1414 else if (section_is_p (sectp->name, &names->eh_frame))
1415 {
1416 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1417
1418 if (aflag & SEC_HAS_CONTENTS)
1419 {
1420 dwarf2_per_objfile->eh_frame.asection = sectp;
1421 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1422 }
1423 }
1424 else if (section_is_p (sectp->name, &names->ranges))
1425 {
1426 dwarf2_per_objfile->ranges.asection = sectp;
1427 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1428 }
1429 else if (section_is_p (sectp->name, &names->types))
1430 {
1431 struct dwarf2_section_info type_section;
1432
1433 memset (&type_section, 0, sizeof (type_section));
1434 type_section.asection = sectp;
1435 type_section.size = bfd_get_section_size (sectp);
1436
1437 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1438 &type_section);
1439 }
1440 else if (section_is_p (sectp->name, &names->gdb_index))
1441 {
1442 dwarf2_per_objfile->gdb_index.asection = sectp;
1443 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1444 }
1445
1446 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1447 && bfd_section_vma (abfd, sectp) == 0)
1448 dwarf2_per_objfile->has_section_at_zero = 1;
1449 }
1450
1451 /* Decompress a section that was compressed using zlib. Store the
1452 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1453
1454 static void
1455 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1456 gdb_byte **outbuf, bfd_size_type *outsize)
1457 {
1458 bfd *abfd = objfile->obfd;
1459 #ifndef HAVE_ZLIB_H
1460 error (_("Support for zlib-compressed DWARF data (from '%s') "
1461 "is disabled in this copy of GDB"),
1462 bfd_get_filename (abfd));
1463 #else
1464 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1465 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1466 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1467 bfd_size_type uncompressed_size;
1468 gdb_byte *uncompressed_buffer;
1469 z_stream strm;
1470 int rc;
1471 int header_size = 12;
1472
1473 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1474 || bfd_bread (compressed_buffer,
1475 compressed_size, abfd) != compressed_size)
1476 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1477 bfd_get_filename (abfd));
1478
1479 /* Read the zlib header. In this case, it should be "ZLIB" followed
1480 by the uncompressed section size, 8 bytes in big-endian order. */
1481 if (compressed_size < header_size
1482 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1483 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1484 bfd_get_filename (abfd));
1485 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[11];
1493
1494 /* It is possible the section consists of several compressed
1495 buffers concatenated together, so we uncompress in a loop. */
1496 strm.zalloc = NULL;
1497 strm.zfree = NULL;
1498 strm.opaque = NULL;
1499 strm.avail_in = compressed_size - header_size;
1500 strm.next_in = (Bytef*) compressed_buffer + header_size;
1501 strm.avail_out = uncompressed_size;
1502 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1503 uncompressed_size);
1504 rc = inflateInit (&strm);
1505 while (strm.avail_in > 0)
1506 {
1507 if (rc != Z_OK)
1508 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1509 bfd_get_filename (abfd), rc);
1510 strm.next_out = ((Bytef*) uncompressed_buffer
1511 + (uncompressed_size - strm.avail_out));
1512 rc = inflate (&strm, Z_FINISH);
1513 if (rc != Z_STREAM_END)
1514 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1515 bfd_get_filename (abfd), rc);
1516 rc = inflateReset (&strm);
1517 }
1518 rc = inflateEnd (&strm);
1519 if (rc != Z_OK
1520 || strm.avail_out != 0)
1521 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1522 bfd_get_filename (abfd), rc);
1523
1524 do_cleanups (cleanup);
1525 *outbuf = uncompressed_buffer;
1526 *outsize = uncompressed_size;
1527 #endif
1528 }
1529
1530 /* A helper function that decides whether a section is empty. */
1531
1532 static int
1533 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1534 {
1535 return info->asection == NULL || info->size == 0;
1536 }
1537
1538 /* Read the contents of the section INFO from object file specified by
1539 OBJFILE, store info about the section into INFO.
1540 If the section is compressed, uncompress it before returning. */
1541
1542 static void
1543 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1544 {
1545 bfd *abfd = objfile->obfd;
1546 asection *sectp = info->asection;
1547 gdb_byte *buf, *retbuf;
1548 unsigned char header[4];
1549
1550 if (info->readin)
1551 return;
1552 info->buffer = NULL;
1553 info->map_addr = NULL;
1554 info->readin = 1;
1555
1556 if (dwarf2_section_empty_p (info))
1557 return;
1558
1559 /* Check if the file has a 4-byte header indicating compression. */
1560 if (info->size > sizeof (header)
1561 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1562 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1563 {
1564 /* Upon decompression, update the buffer and its size. */
1565 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1566 {
1567 zlib_decompress_section (objfile, sectp, &info->buffer,
1568 &info->size);
1569 return;
1570 }
1571 }
1572
1573 #ifdef HAVE_MMAP
1574 if (pagesize == 0)
1575 pagesize = getpagesize ();
1576
1577 /* Only try to mmap sections which are large enough: we don't want to
1578 waste space due to fragmentation. Also, only try mmap for sections
1579 without relocations. */
1580
1581 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1582 {
1583 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1584 MAP_PRIVATE, sectp->filepos,
1585 &info->map_addr, &info->map_len);
1586
1587 if ((caddr_t)info->buffer != MAP_FAILED)
1588 {
1589 #if HAVE_POSIX_MADVISE
1590 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1591 #endif
1592 return;
1593 }
1594 }
1595 #endif
1596
1597 /* If we get here, we are a normal, not-compressed section. */
1598 info->buffer = buf
1599 = obstack_alloc (&objfile->objfile_obstack, info->size);
1600
1601 /* When debugging .o files, we may need to apply relocations; see
1602 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1603 We never compress sections in .o files, so we only need to
1604 try this when the section is not compressed. */
1605 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1606 if (retbuf != NULL)
1607 {
1608 info->buffer = retbuf;
1609 return;
1610 }
1611
1612 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1613 || bfd_bread (buf, info->size, abfd) != info->size)
1614 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1615 bfd_get_filename (abfd));
1616 }
1617
1618 /* A helper function that returns the size of a section in a safe way.
1619 If you are positive that the section has been read before using the
1620 size, then it is safe to refer to the dwarf2_section_info object's
1621 "size" field directly. In other cases, you must call this
1622 function, because for compressed sections the size field is not set
1623 correctly until the section has been read. */
1624
1625 static bfd_size_type
1626 dwarf2_section_size (struct objfile *objfile,
1627 struct dwarf2_section_info *info)
1628 {
1629 if (!info->readin)
1630 dwarf2_read_section (objfile, info);
1631 return info->size;
1632 }
1633
1634 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1635 SECTION_NAME. */
1636
1637 void
1638 dwarf2_get_section_info (struct objfile *objfile,
1639 enum dwarf2_section_enum sect,
1640 asection **sectp, gdb_byte **bufp,
1641 bfd_size_type *sizep)
1642 {
1643 struct dwarf2_per_objfile *data
1644 = objfile_data (objfile, dwarf2_objfile_data_key);
1645 struct dwarf2_section_info *info;
1646
1647 /* We may see an objfile without any DWARF, in which case we just
1648 return nothing. */
1649 if (data == NULL)
1650 {
1651 *sectp = NULL;
1652 *bufp = NULL;
1653 *sizep = 0;
1654 return;
1655 }
1656 switch (sect)
1657 {
1658 case DWARF2_DEBUG_FRAME:
1659 info = &data->frame;
1660 break;
1661 case DWARF2_EH_FRAME:
1662 info = &data->eh_frame;
1663 break;
1664 default:
1665 gdb_assert_not_reached ("unexpected section");
1666 }
1667
1668 dwarf2_read_section (objfile, info);
1669
1670 *sectp = info->asection;
1671 *bufp = info->buffer;
1672 *sizep = info->size;
1673 }
1674
1675 \f
1676 /* DWARF quick_symbols_functions support. */
1677
1678 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1679 unique line tables, so we maintain a separate table of all .debug_line
1680 derived entries to support the sharing.
1681 All the quick functions need is the list of file names. We discard the
1682 line_header when we're done and don't need to record it here. */
1683 struct quick_file_names
1684 {
1685 /* The offset in .debug_line of the line table. We hash on this. */
1686 unsigned int offset;
1687
1688 /* The number of entries in file_names, real_names. */
1689 unsigned int num_file_names;
1690
1691 /* The file names from the line table, after being run through
1692 file_full_name. */
1693 const char **file_names;
1694
1695 /* The file names from the line table after being run through
1696 gdb_realpath. These are computed lazily. */
1697 const char **real_names;
1698 };
1699
1700 /* When using the index (and thus not using psymtabs), each CU has an
1701 object of this type. This is used to hold information needed by
1702 the various "quick" methods. */
1703 struct dwarf2_per_cu_quick_data
1704 {
1705 /* The file table. This can be NULL if there was no file table
1706 or it's currently not read in.
1707 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1708 struct quick_file_names *file_names;
1709
1710 /* The corresponding symbol table. This is NULL if symbols for this
1711 CU have not yet been read. */
1712 struct symtab *symtab;
1713
1714 /* A temporary mark bit used when iterating over all CUs in
1715 expand_symtabs_matching. */
1716 unsigned int mark : 1;
1717
1718 /* True if we've tried to read the file table and found there isn't one.
1719 There will be no point in trying to read it again next time. */
1720 unsigned int no_file_data : 1;
1721 };
1722
1723 /* Hash function for a quick_file_names. */
1724
1725 static hashval_t
1726 hash_file_name_entry (const void *e)
1727 {
1728 const struct quick_file_names *file_data = e;
1729
1730 return file_data->offset;
1731 }
1732
1733 /* Equality function for a quick_file_names. */
1734
1735 static int
1736 eq_file_name_entry (const void *a, const void *b)
1737 {
1738 const struct quick_file_names *ea = a;
1739 const struct quick_file_names *eb = b;
1740
1741 return ea->offset == eb->offset;
1742 }
1743
1744 /* Delete function for a quick_file_names. */
1745
1746 static void
1747 delete_file_name_entry (void *e)
1748 {
1749 struct quick_file_names *file_data = e;
1750 int i;
1751
1752 for (i = 0; i < file_data->num_file_names; ++i)
1753 {
1754 xfree ((void*) file_data->file_names[i]);
1755 if (file_data->real_names)
1756 xfree ((void*) file_data->real_names[i]);
1757 }
1758
1759 /* The space for the struct itself lives on objfile_obstack,
1760 so we don't free it here. */
1761 }
1762
1763 /* Create a quick_file_names hash table. */
1764
1765 static htab_t
1766 create_quick_file_names_table (unsigned int nr_initial_entries)
1767 {
1768 return htab_create_alloc (nr_initial_entries,
1769 hash_file_name_entry, eq_file_name_entry,
1770 delete_file_name_entry, xcalloc, xfree);
1771 }
1772
1773 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1774 have to be created afterwards. You should call age_cached_comp_units after
1775 processing PER_CU->CU. dw2_setup must have been already called. */
1776
1777 static void
1778 load_cu (struct dwarf2_per_cu_data *per_cu)
1779 {
1780 if (per_cu->debug_types_section)
1781 load_full_type_unit (per_cu);
1782 else
1783 load_full_comp_unit (per_cu);
1784
1785 gdb_assert (per_cu->cu != NULL);
1786
1787 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1788 }
1789
1790 /* Read in the symbols for PER_CU. */
1791
1792 static void
1793 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1794 {
1795 struct cleanup *back_to;
1796
1797 back_to = make_cleanup (dwarf2_release_queue, NULL);
1798
1799 queue_comp_unit (per_cu);
1800
1801 load_cu (per_cu);
1802
1803 process_queue ();
1804
1805 /* Age the cache, releasing compilation units that have not
1806 been used recently. */
1807 age_cached_comp_units ();
1808
1809 do_cleanups (back_to);
1810 }
1811
1812 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1813 the objfile from which this CU came. Returns the resulting symbol
1814 table. */
1815
1816 static struct symtab *
1817 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1818 {
1819 if (!per_cu->v.quick->symtab)
1820 {
1821 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1822 increment_reading_symtab ();
1823 dw2_do_instantiate_symtab (per_cu);
1824 do_cleanups (back_to);
1825 }
1826 return per_cu->v.quick->symtab;
1827 }
1828
1829 /* Return the CU given its index. */
1830
1831 static struct dwarf2_per_cu_data *
1832 dw2_get_cu (int index)
1833 {
1834 if (index >= dwarf2_per_objfile->n_comp_units)
1835 {
1836 index -= dwarf2_per_objfile->n_comp_units;
1837 return dwarf2_per_objfile->all_type_units[index];
1838 }
1839 return dwarf2_per_objfile->all_comp_units[index];
1840 }
1841
1842 /* A helper function that knows how to read a 64-bit value in a way
1843 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1844 otherwise. */
1845
1846 static int
1847 extract_cu_value (const char *bytes, ULONGEST *result)
1848 {
1849 if (sizeof (ULONGEST) < 8)
1850 {
1851 int i;
1852
1853 /* Ignore the upper 4 bytes if they are all zero. */
1854 for (i = 0; i < 4; ++i)
1855 if (bytes[i + 4] != 0)
1856 return 0;
1857
1858 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1859 }
1860 else
1861 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1862 return 1;
1863 }
1864
1865 /* Read the CU list from the mapped index, and use it to create all
1866 the CU objects for this objfile. Return 0 if something went wrong,
1867 1 if everything went ok. */
1868
1869 static int
1870 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1871 offset_type cu_list_elements)
1872 {
1873 offset_type i;
1874
1875 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1876 dwarf2_per_objfile->all_comp_units
1877 = obstack_alloc (&objfile->objfile_obstack,
1878 dwarf2_per_objfile->n_comp_units
1879 * sizeof (struct dwarf2_per_cu_data *));
1880
1881 for (i = 0; i < cu_list_elements; i += 2)
1882 {
1883 struct dwarf2_per_cu_data *the_cu;
1884 ULONGEST offset, length;
1885
1886 if (!extract_cu_value (cu_list, &offset)
1887 || !extract_cu_value (cu_list + 8, &length))
1888 return 0;
1889 cu_list += 2 * 8;
1890
1891 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1892 struct dwarf2_per_cu_data);
1893 the_cu->offset = offset;
1894 the_cu->length = length;
1895 the_cu->objfile = objfile;
1896 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1897 struct dwarf2_per_cu_quick_data);
1898 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1899 }
1900
1901 return 1;
1902 }
1903
1904 /* Create the signatured type hash table from the index. */
1905
1906 static int
1907 create_signatured_type_table_from_index (struct objfile *objfile,
1908 struct dwarf2_section_info *section,
1909 const gdb_byte *bytes,
1910 offset_type elements)
1911 {
1912 offset_type i;
1913 htab_t sig_types_hash;
1914
1915 dwarf2_per_objfile->n_type_units = elements / 3;
1916 dwarf2_per_objfile->all_type_units
1917 = obstack_alloc (&objfile->objfile_obstack,
1918 dwarf2_per_objfile->n_type_units
1919 * sizeof (struct dwarf2_per_cu_data *));
1920
1921 sig_types_hash = allocate_signatured_type_table (objfile);
1922
1923 for (i = 0; i < elements; i += 3)
1924 {
1925 struct signatured_type *type_sig;
1926 ULONGEST offset, type_offset, signature;
1927 void **slot;
1928
1929 if (!extract_cu_value (bytes, &offset)
1930 || !extract_cu_value (bytes + 8, &type_offset))
1931 return 0;
1932 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1933 bytes += 3 * 8;
1934
1935 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1936 struct signatured_type);
1937 type_sig->signature = signature;
1938 type_sig->type_offset = type_offset;
1939 type_sig->per_cu.debug_types_section = section;
1940 type_sig->per_cu.offset = offset;
1941 type_sig->per_cu.objfile = objfile;
1942 type_sig->per_cu.v.quick
1943 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1944 struct dwarf2_per_cu_quick_data);
1945
1946 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1947 *slot = type_sig;
1948
1949 dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1950 }
1951
1952 dwarf2_per_objfile->signatured_types = sig_types_hash;
1953
1954 return 1;
1955 }
1956
1957 /* Read the address map data from the mapped index, and use it to
1958 populate the objfile's psymtabs_addrmap. */
1959
1960 static void
1961 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1962 {
1963 const gdb_byte *iter, *end;
1964 struct obstack temp_obstack;
1965 struct addrmap *mutable_map;
1966 struct cleanup *cleanup;
1967 CORE_ADDR baseaddr;
1968
1969 obstack_init (&temp_obstack);
1970 cleanup = make_cleanup_obstack_free (&temp_obstack);
1971 mutable_map = addrmap_create_mutable (&temp_obstack);
1972
1973 iter = index->address_table;
1974 end = iter + index->address_table_size;
1975
1976 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1977
1978 while (iter < end)
1979 {
1980 ULONGEST hi, lo, cu_index;
1981 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1982 iter += 8;
1983 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1984 iter += 8;
1985 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1986 iter += 4;
1987
1988 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1989 dw2_get_cu (cu_index));
1990 }
1991
1992 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1993 &objfile->objfile_obstack);
1994 do_cleanups (cleanup);
1995 }
1996
1997 /* The hash function for strings in the mapped index. This is the same as
1998 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1999 implementation. This is necessary because the hash function is tied to the
2000 format of the mapped index file. The hash values do not have to match with
2001 SYMBOL_HASH_NEXT.
2002
2003 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2004
2005 static hashval_t
2006 mapped_index_string_hash (int index_version, const void *p)
2007 {
2008 const unsigned char *str = (const unsigned char *) p;
2009 hashval_t r = 0;
2010 unsigned char c;
2011
2012 while ((c = *str++) != 0)
2013 {
2014 if (index_version >= 5)
2015 c = tolower (c);
2016 r = r * 67 + c - 113;
2017 }
2018
2019 return r;
2020 }
2021
2022 /* Find a slot in the mapped index INDEX for the object named NAME.
2023 If NAME is found, set *VEC_OUT to point to the CU vector in the
2024 constant pool and return 1. If NAME cannot be found, return 0. */
2025
2026 static int
2027 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2028 offset_type **vec_out)
2029 {
2030 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2031 offset_type hash;
2032 offset_type slot, step;
2033 int (*cmp) (const char *, const char *);
2034
2035 if (current_language->la_language == language_cplus
2036 || current_language->la_language == language_java
2037 || current_language->la_language == language_fortran)
2038 {
2039 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2040 not contain any. */
2041 const char *paren = strchr (name, '(');
2042
2043 if (paren)
2044 {
2045 char *dup;
2046
2047 dup = xmalloc (paren - name + 1);
2048 memcpy (dup, name, paren - name);
2049 dup[paren - name] = 0;
2050
2051 make_cleanup (xfree, dup);
2052 name = dup;
2053 }
2054 }
2055
2056 /* Index version 4 did not support case insensitive searches. But the
2057 indexes for case insensitive languages are built in lowercase, therefore
2058 simulate our NAME being searched is also lowercased. */
2059 hash = mapped_index_string_hash ((index->version == 4
2060 && case_sensitivity == case_sensitive_off
2061 ? 5 : index->version),
2062 name);
2063
2064 slot = hash & (index->symbol_table_slots - 1);
2065 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2066 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2067
2068 for (;;)
2069 {
2070 /* Convert a slot number to an offset into the table. */
2071 offset_type i = 2 * slot;
2072 const char *str;
2073 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2074 {
2075 do_cleanups (back_to);
2076 return 0;
2077 }
2078
2079 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2080 if (!cmp (name, str))
2081 {
2082 *vec_out = (offset_type *) (index->constant_pool
2083 + MAYBE_SWAP (index->symbol_table[i + 1]));
2084 do_cleanups (back_to);
2085 return 1;
2086 }
2087
2088 slot = (slot + step) & (index->symbol_table_slots - 1);
2089 }
2090 }
2091
2092 /* Read the index file. If everything went ok, initialize the "quick"
2093 elements of all the CUs and return 1. Otherwise, return 0. */
2094
2095 static int
2096 dwarf2_read_index (struct objfile *objfile)
2097 {
2098 char *addr;
2099 struct mapped_index *map;
2100 offset_type *metadata;
2101 const gdb_byte *cu_list;
2102 const gdb_byte *types_list = NULL;
2103 offset_type version, cu_list_elements;
2104 offset_type types_list_elements = 0;
2105 int i;
2106
2107 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2108 return 0;
2109
2110 /* Older elfutils strip versions could keep the section in the main
2111 executable while splitting it for the separate debug info file. */
2112 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2113 & SEC_HAS_CONTENTS) == 0)
2114 return 0;
2115
2116 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2117
2118 addr = dwarf2_per_objfile->gdb_index.buffer;
2119 /* Version check. */
2120 version = MAYBE_SWAP (*(offset_type *) addr);
2121 /* Versions earlier than 3 emitted every copy of a psymbol. This
2122 causes the index to behave very poorly for certain requests. Version 3
2123 contained incomplete addrmap. So, it seems better to just ignore such
2124 indices. Index version 4 uses a different hash function than index
2125 version 5 and later. */
2126 if (version < 4)
2127 return 0;
2128 /* Indexes with higher version than the one supported by GDB may be no
2129 longer backward compatible. */
2130 if (version > 5)
2131 return 0;
2132
2133 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2134 map->version = version;
2135 map->total_size = dwarf2_per_objfile->gdb_index.size;
2136
2137 metadata = (offset_type *) (addr + sizeof (offset_type));
2138
2139 i = 0;
2140 cu_list = addr + MAYBE_SWAP (metadata[i]);
2141 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2142 / 8);
2143 ++i;
2144
2145 types_list = addr + MAYBE_SWAP (metadata[i]);
2146 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2147 - MAYBE_SWAP (metadata[i]))
2148 / 8);
2149 ++i;
2150
2151 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2152 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2153 - MAYBE_SWAP (metadata[i]));
2154 ++i;
2155
2156 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2157 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2158 - MAYBE_SWAP (metadata[i]))
2159 / (2 * sizeof (offset_type)));
2160 ++i;
2161
2162 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2163
2164 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2165 return 0;
2166
2167 if (types_list_elements)
2168 {
2169 struct dwarf2_section_info *section;
2170
2171 /* We can only handle a single .debug_types when we have an
2172 index. */
2173 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2174 return 0;
2175
2176 section = VEC_index (dwarf2_section_info_def,
2177 dwarf2_per_objfile->types, 0);
2178
2179 if (!create_signatured_type_table_from_index (objfile, section,
2180 types_list,
2181 types_list_elements))
2182 return 0;
2183 }
2184
2185 create_addrmap_from_index (objfile, map);
2186
2187 dwarf2_per_objfile->index_table = map;
2188 dwarf2_per_objfile->using_index = 1;
2189 dwarf2_per_objfile->quick_file_names_table =
2190 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2191
2192 return 1;
2193 }
2194
2195 /* A helper for the "quick" functions which sets the global
2196 dwarf2_per_objfile according to OBJFILE. */
2197
2198 static void
2199 dw2_setup (struct objfile *objfile)
2200 {
2201 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2202 gdb_assert (dwarf2_per_objfile);
2203 }
2204
2205 /* A helper for the "quick" functions which attempts to read the line
2206 table for THIS_CU. */
2207
2208 static struct quick_file_names *
2209 dw2_get_file_names (struct objfile *objfile,
2210 struct dwarf2_per_cu_data *this_cu)
2211 {
2212 bfd *abfd = objfile->obfd;
2213 struct line_header *lh;
2214 struct attribute *attr;
2215 struct cleanup *cleanups;
2216 struct die_info *comp_unit_die;
2217 struct dwarf2_section_info* sec;
2218 gdb_byte *info_ptr;
2219 int has_children, i;
2220 struct dwarf2_cu cu;
2221 unsigned int bytes_read;
2222 struct die_reader_specs reader_specs;
2223 char *name, *comp_dir;
2224 void **slot;
2225 struct quick_file_names *qfn;
2226 unsigned int line_offset;
2227
2228 if (this_cu->v.quick->file_names != NULL)
2229 return this_cu->v.quick->file_names;
2230 /* If we know there is no line data, no point in looking again. */
2231 if (this_cu->v.quick->no_file_data)
2232 return NULL;
2233
2234 init_one_comp_unit (&cu, this_cu);
2235 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2236
2237 if (this_cu->debug_types_section)
2238 sec = this_cu->debug_types_section;
2239 else
2240 sec = &dwarf2_per_objfile->info;
2241 dwarf2_read_section (objfile, sec);
2242 info_ptr = sec->buffer + this_cu->offset;
2243
2244 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2245 this_cu->debug_types_section != NULL);
2246
2247 /* Skip dummy compilation units. */
2248 if (info_ptr >= (sec->buffer + sec->size)
2249 || peek_abbrev_code (abfd, info_ptr) == 0)
2250 {
2251 do_cleanups (cleanups);
2252 return NULL;
2253 }
2254
2255 dwarf2_read_abbrevs (&cu);
2256 make_cleanup (dwarf2_free_abbrev_table, &cu);
2257
2258 init_cu_die_reader (&reader_specs, &cu);
2259 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2260 &has_children);
2261
2262 lh = NULL;
2263 slot = NULL;
2264 line_offset = 0;
2265 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2266 if (attr)
2267 {
2268 struct quick_file_names find_entry;
2269
2270 line_offset = DW_UNSND (attr);
2271
2272 /* We may have already read in this line header (TU line header sharing).
2273 If we have we're done. */
2274 find_entry.offset = line_offset;
2275 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2276 &find_entry, INSERT);
2277 if (*slot != NULL)
2278 {
2279 do_cleanups (cleanups);
2280 this_cu->v.quick->file_names = *slot;
2281 return *slot;
2282 }
2283
2284 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2285 }
2286 if (lh == NULL)
2287 {
2288 do_cleanups (cleanups);
2289 this_cu->v.quick->no_file_data = 1;
2290 return NULL;
2291 }
2292
2293 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2294 qfn->offset = line_offset;
2295 gdb_assert (slot != NULL);
2296 *slot = qfn;
2297
2298 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2299
2300 qfn->num_file_names = lh->num_file_names;
2301 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2302 lh->num_file_names * sizeof (char *));
2303 for (i = 0; i < lh->num_file_names; ++i)
2304 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2305 qfn->real_names = NULL;
2306
2307 free_line_header (lh);
2308 do_cleanups (cleanups);
2309
2310 this_cu->v.quick->file_names = qfn;
2311 return qfn;
2312 }
2313
2314 /* A helper for the "quick" functions which computes and caches the
2315 real path for a given file name from the line table. */
2316
2317 static const char *
2318 dw2_get_real_path (struct objfile *objfile,
2319 struct quick_file_names *qfn, int index)
2320 {
2321 if (qfn->real_names == NULL)
2322 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2323 qfn->num_file_names, sizeof (char *));
2324
2325 if (qfn->real_names[index] == NULL)
2326 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2327
2328 return qfn->real_names[index];
2329 }
2330
2331 static struct symtab *
2332 dw2_find_last_source_symtab (struct objfile *objfile)
2333 {
2334 int index;
2335
2336 dw2_setup (objfile);
2337 index = dwarf2_per_objfile->n_comp_units - 1;
2338 return dw2_instantiate_symtab (dw2_get_cu (index));
2339 }
2340
2341 /* Traversal function for dw2_forget_cached_source_info. */
2342
2343 static int
2344 dw2_free_cached_file_names (void **slot, void *info)
2345 {
2346 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2347
2348 if (file_data->real_names)
2349 {
2350 int i;
2351
2352 for (i = 0; i < file_data->num_file_names; ++i)
2353 {
2354 xfree ((void*) file_data->real_names[i]);
2355 file_data->real_names[i] = NULL;
2356 }
2357 }
2358
2359 return 1;
2360 }
2361
2362 static void
2363 dw2_forget_cached_source_info (struct objfile *objfile)
2364 {
2365 dw2_setup (objfile);
2366
2367 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2368 dw2_free_cached_file_names, NULL);
2369 }
2370
2371 /* Helper function for dw2_map_symtabs_matching_filename that expands
2372 the symtabs and calls the iterator. */
2373
2374 static int
2375 dw2_map_expand_apply (struct objfile *objfile,
2376 struct dwarf2_per_cu_data *per_cu,
2377 const char *name,
2378 const char *full_path, const char *real_path,
2379 int (*callback) (struct symtab *, void *),
2380 void *data)
2381 {
2382 struct symtab *last_made = objfile->symtabs;
2383
2384 /* Don't visit already-expanded CUs. */
2385 if (per_cu->v.quick->symtab)
2386 return 0;
2387
2388 /* This may expand more than one symtab, and we want to iterate over
2389 all of them. */
2390 dw2_instantiate_symtab (per_cu);
2391
2392 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2393 objfile->symtabs, last_made);
2394 }
2395
2396 /* Implementation of the map_symtabs_matching_filename method. */
2397
2398 static int
2399 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2400 const char *full_path, const char *real_path,
2401 int (*callback) (struct symtab *, void *),
2402 void *data)
2403 {
2404 int i;
2405 const char *name_basename = lbasename (name);
2406 int name_len = strlen (name);
2407 int is_abs = IS_ABSOLUTE_PATH (name);
2408
2409 dw2_setup (objfile);
2410
2411 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2412 + dwarf2_per_objfile->n_type_units); ++i)
2413 {
2414 int j;
2415 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2416 struct quick_file_names *file_data;
2417
2418 /* We only need to look at symtabs not already expanded. */
2419 if (per_cu->v.quick->symtab)
2420 continue;
2421
2422 file_data = dw2_get_file_names (objfile, per_cu);
2423 if (file_data == NULL)
2424 continue;
2425
2426 for (j = 0; j < file_data->num_file_names; ++j)
2427 {
2428 const char *this_name = file_data->file_names[j];
2429
2430 if (FILENAME_CMP (name, this_name) == 0
2431 || (!is_abs && compare_filenames_for_search (this_name,
2432 name, name_len)))
2433 {
2434 if (dw2_map_expand_apply (objfile, per_cu,
2435 name, full_path, real_path,
2436 callback, data))
2437 return 1;
2438 }
2439
2440 {
2441 if (dw2_map_expand_apply (objfile, per_cu,
2442 name, full_path, real_path,
2443 callback, data))
2444 return 1;
2445 }
2446
2447 /* Before we invoke realpath, which can get expensive when many
2448 files are involved, do a quick comparison of the basenames. */
2449 if (! basenames_may_differ
2450 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2451 continue;
2452
2453 if (full_path != NULL)
2454 {
2455 const char *this_real_name = dw2_get_real_path (objfile,
2456 file_data, j);
2457
2458 if (this_real_name != NULL
2459 && (FILENAME_CMP (full_path, this_real_name) == 0
2460 || (!is_abs
2461 && compare_filenames_for_search (this_real_name,
2462 name, name_len))))
2463 {
2464 if (dw2_map_expand_apply (objfile, per_cu,
2465 name, full_path, real_path,
2466 callback, data))
2467 return 1;
2468 }
2469 }
2470
2471 if (real_path != NULL)
2472 {
2473 const char *this_real_name = dw2_get_real_path (objfile,
2474 file_data, j);
2475
2476 if (this_real_name != NULL
2477 && (FILENAME_CMP (real_path, this_real_name) == 0
2478 || (!is_abs
2479 && compare_filenames_for_search (this_real_name,
2480 name, name_len))))
2481 {
2482 if (dw2_map_expand_apply (objfile, per_cu,
2483 name, full_path, real_path,
2484 callback, data))
2485 return 1;
2486 }
2487 }
2488 }
2489 }
2490
2491 return 0;
2492 }
2493
2494 static struct symtab *
2495 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2496 const char *name, domain_enum domain)
2497 {
2498 /* We do all the work in the pre_expand_symtabs_matching hook
2499 instead. */
2500 return NULL;
2501 }
2502
2503 /* A helper function that expands all symtabs that hold an object
2504 named NAME. */
2505
2506 static void
2507 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2508 {
2509 dw2_setup (objfile);
2510
2511 /* index_table is NULL if OBJF_READNOW. */
2512 if (dwarf2_per_objfile->index_table)
2513 {
2514 offset_type *vec;
2515
2516 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2517 name, &vec))
2518 {
2519 offset_type i, len = MAYBE_SWAP (*vec);
2520 for (i = 0; i < len; ++i)
2521 {
2522 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2523 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2524
2525 dw2_instantiate_symtab (per_cu);
2526 }
2527 }
2528 }
2529 }
2530
2531 static void
2532 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2533 enum block_enum block_kind, const char *name,
2534 domain_enum domain)
2535 {
2536 dw2_do_expand_symtabs_matching (objfile, name);
2537 }
2538
2539 static void
2540 dw2_print_stats (struct objfile *objfile)
2541 {
2542 int i, count;
2543
2544 dw2_setup (objfile);
2545 count = 0;
2546 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2547 + dwarf2_per_objfile->n_type_units); ++i)
2548 {
2549 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2550
2551 if (!per_cu->v.quick->symtab)
2552 ++count;
2553 }
2554 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2555 }
2556
2557 static void
2558 dw2_dump (struct objfile *objfile)
2559 {
2560 /* Nothing worth printing. */
2561 }
2562
2563 static void
2564 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2565 struct section_offsets *delta)
2566 {
2567 /* There's nothing to relocate here. */
2568 }
2569
2570 static void
2571 dw2_expand_symtabs_for_function (struct objfile *objfile,
2572 const char *func_name)
2573 {
2574 dw2_do_expand_symtabs_matching (objfile, func_name);
2575 }
2576
2577 static void
2578 dw2_expand_all_symtabs (struct objfile *objfile)
2579 {
2580 int i;
2581
2582 dw2_setup (objfile);
2583
2584 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2585 + dwarf2_per_objfile->n_type_units); ++i)
2586 {
2587 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2588
2589 dw2_instantiate_symtab (per_cu);
2590 }
2591 }
2592
2593 static void
2594 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2595 const char *filename)
2596 {
2597 int i;
2598
2599 dw2_setup (objfile);
2600
2601 /* We don't need to consider type units here.
2602 This is only called for examining code, e.g. expand_line_sal.
2603 There can be an order of magnitude (or more) more type units
2604 than comp units, and we avoid them if we can. */
2605
2606 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2607 {
2608 int j;
2609 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2610 struct quick_file_names *file_data;
2611
2612 /* We only need to look at symtabs not already expanded. */
2613 if (per_cu->v.quick->symtab)
2614 continue;
2615
2616 file_data = dw2_get_file_names (objfile, per_cu);
2617 if (file_data == NULL)
2618 continue;
2619
2620 for (j = 0; j < file_data->num_file_names; ++j)
2621 {
2622 const char *this_name = file_data->file_names[j];
2623 if (FILENAME_CMP (this_name, filename) == 0)
2624 {
2625 dw2_instantiate_symtab (per_cu);
2626 break;
2627 }
2628 }
2629 }
2630 }
2631
2632 static const char *
2633 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2634 {
2635 struct dwarf2_per_cu_data *per_cu;
2636 offset_type *vec;
2637 struct quick_file_names *file_data;
2638
2639 dw2_setup (objfile);
2640
2641 /* index_table is NULL if OBJF_READNOW. */
2642 if (!dwarf2_per_objfile->index_table)
2643 {
2644 struct symtab *s;
2645
2646 ALL_OBJFILE_SYMTABS (objfile, s)
2647 if (s->primary)
2648 {
2649 struct blockvector *bv = BLOCKVECTOR (s);
2650 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2651 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2652
2653 if (sym)
2654 return sym->symtab->filename;
2655 }
2656 return NULL;
2657 }
2658
2659 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2660 name, &vec))
2661 return NULL;
2662
2663 /* Note that this just looks at the very first one named NAME -- but
2664 actually we are looking for a function. find_main_filename
2665 should be rewritten so that it doesn't require a custom hook. It
2666 could just use the ordinary symbol tables. */
2667 /* vec[0] is the length, which must always be >0. */
2668 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2669
2670 file_data = dw2_get_file_names (objfile, per_cu);
2671 if (file_data == NULL)
2672 return NULL;
2673
2674 return file_data->file_names[file_data->num_file_names - 1];
2675 }
2676
2677 static void
2678 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2679 struct objfile *objfile, int global,
2680 int (*callback) (struct block *,
2681 struct symbol *, void *),
2682 void *data, symbol_compare_ftype *match,
2683 symbol_compare_ftype *ordered_compare)
2684 {
2685 /* Currently unimplemented; used for Ada. The function can be called if the
2686 current language is Ada for a non-Ada objfile using GNU index. As Ada
2687 does not look for non-Ada symbols this function should just return. */
2688 }
2689
2690 static void
2691 dw2_expand_symtabs_matching
2692 (struct objfile *objfile,
2693 int (*file_matcher) (const char *, void *),
2694 int (*name_matcher) (const char *, void *),
2695 enum search_domain kind,
2696 void *data)
2697 {
2698 int i;
2699 offset_type iter;
2700 struct mapped_index *index;
2701
2702 dw2_setup (objfile);
2703
2704 /* index_table is NULL if OBJF_READNOW. */
2705 if (!dwarf2_per_objfile->index_table)
2706 return;
2707 index = dwarf2_per_objfile->index_table;
2708
2709 if (file_matcher != NULL)
2710 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2711 + dwarf2_per_objfile->n_type_units); ++i)
2712 {
2713 int j;
2714 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2715 struct quick_file_names *file_data;
2716
2717 per_cu->v.quick->mark = 0;
2718
2719 /* We only need to look at symtabs not already expanded. */
2720 if (per_cu->v.quick->symtab)
2721 continue;
2722
2723 file_data = dw2_get_file_names (objfile, per_cu);
2724 if (file_data == NULL)
2725 continue;
2726
2727 for (j = 0; j < file_data->num_file_names; ++j)
2728 {
2729 if (file_matcher (file_data->file_names[j], data))
2730 {
2731 per_cu->v.quick->mark = 1;
2732 break;
2733 }
2734 }
2735 }
2736
2737 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2738 {
2739 offset_type idx = 2 * iter;
2740 const char *name;
2741 offset_type *vec, vec_len, vec_idx;
2742
2743 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2744 continue;
2745
2746 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2747
2748 if (! (*name_matcher) (name, data))
2749 continue;
2750
2751 /* The name was matched, now expand corresponding CUs that were
2752 marked. */
2753 vec = (offset_type *) (index->constant_pool
2754 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2755 vec_len = MAYBE_SWAP (vec[0]);
2756 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2757 {
2758 struct dwarf2_per_cu_data *per_cu;
2759
2760 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2761 if (file_matcher == NULL || per_cu->v.quick->mark)
2762 dw2_instantiate_symtab (per_cu);
2763 }
2764 }
2765 }
2766
2767 static struct symtab *
2768 dw2_find_pc_sect_symtab (struct objfile *objfile,
2769 struct minimal_symbol *msymbol,
2770 CORE_ADDR pc,
2771 struct obj_section *section,
2772 int warn_if_readin)
2773 {
2774 struct dwarf2_per_cu_data *data;
2775
2776 dw2_setup (objfile);
2777
2778 if (!objfile->psymtabs_addrmap)
2779 return NULL;
2780
2781 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2782 if (!data)
2783 return NULL;
2784
2785 if (warn_if_readin && data->v.quick->symtab)
2786 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2787 paddress (get_objfile_arch (objfile), pc));
2788
2789 return dw2_instantiate_symtab (data);
2790 }
2791
2792 static void
2793 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2794 void *data, int need_fullname)
2795 {
2796 int i;
2797
2798 dw2_setup (objfile);
2799
2800 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2801 + dwarf2_per_objfile->n_type_units); ++i)
2802 {
2803 int j;
2804 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2805 struct quick_file_names *file_data;
2806
2807 /* We only need to look at symtabs not already expanded. */
2808 if (per_cu->v.quick->symtab)
2809 continue;
2810
2811 file_data = dw2_get_file_names (objfile, per_cu);
2812 if (file_data == NULL)
2813 continue;
2814
2815 for (j = 0; j < file_data->num_file_names; ++j)
2816 {
2817 const char *this_real_name;
2818
2819 if (need_fullname)
2820 this_real_name = dw2_get_real_path (objfile, file_data, j);
2821 else
2822 this_real_name = NULL;
2823 (*fun) (file_data->file_names[j], this_real_name, data);
2824 }
2825 }
2826 }
2827
2828 static int
2829 dw2_has_symbols (struct objfile *objfile)
2830 {
2831 return 1;
2832 }
2833
2834 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2835 {
2836 dw2_has_symbols,
2837 dw2_find_last_source_symtab,
2838 dw2_forget_cached_source_info,
2839 dw2_map_symtabs_matching_filename,
2840 dw2_lookup_symbol,
2841 dw2_pre_expand_symtabs_matching,
2842 dw2_print_stats,
2843 dw2_dump,
2844 dw2_relocate,
2845 dw2_expand_symtabs_for_function,
2846 dw2_expand_all_symtabs,
2847 dw2_expand_symtabs_with_filename,
2848 dw2_find_symbol_file,
2849 dw2_map_matching_symbols,
2850 dw2_expand_symtabs_matching,
2851 dw2_find_pc_sect_symtab,
2852 dw2_map_symbol_filenames
2853 };
2854
2855 /* Initialize for reading DWARF for this objfile. Return 0 if this
2856 file will use psymtabs, or 1 if using the GNU index. */
2857
2858 int
2859 dwarf2_initialize_objfile (struct objfile *objfile)
2860 {
2861 /* If we're about to read full symbols, don't bother with the
2862 indices. In this case we also don't care if some other debug
2863 format is making psymtabs, because they are all about to be
2864 expanded anyway. */
2865 if ((objfile->flags & OBJF_READNOW))
2866 {
2867 int i;
2868
2869 dwarf2_per_objfile->using_index = 1;
2870 create_all_comp_units (objfile);
2871 create_debug_types_hash_table (objfile);
2872 dwarf2_per_objfile->quick_file_names_table =
2873 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2874
2875 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2876 + dwarf2_per_objfile->n_type_units); ++i)
2877 {
2878 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2879
2880 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2881 struct dwarf2_per_cu_quick_data);
2882 }
2883
2884 /* Return 1 so that gdb sees the "quick" functions. However,
2885 these functions will be no-ops because we will have expanded
2886 all symtabs. */
2887 return 1;
2888 }
2889
2890 if (dwarf2_read_index (objfile))
2891 return 1;
2892
2893 return 0;
2894 }
2895
2896 \f
2897
2898 /* Build a partial symbol table. */
2899
2900 void
2901 dwarf2_build_psymtabs (struct objfile *objfile)
2902 {
2903 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2904 {
2905 init_psymbol_list (objfile, 1024);
2906 }
2907
2908 dwarf2_build_psymtabs_hard (objfile);
2909 }
2910
2911 /* Return TRUE if OFFSET is within CU_HEADER. */
2912
2913 static inline int
2914 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2915 {
2916 unsigned int bottom = cu_header->offset;
2917 unsigned int top = (cu_header->offset
2918 + cu_header->length
2919 + cu_header->initial_length_size);
2920
2921 return (offset >= bottom && offset < top);
2922 }
2923
2924 /* Read in the comp unit header information from the debug_info at info_ptr.
2925 NOTE: This leaves members offset, first_die_offset to be filled in
2926 by the caller. */
2927
2928 static gdb_byte *
2929 read_comp_unit_head (struct comp_unit_head *cu_header,
2930 gdb_byte *info_ptr, bfd *abfd)
2931 {
2932 int signed_addr;
2933 unsigned int bytes_read;
2934
2935 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2936 cu_header->initial_length_size = bytes_read;
2937 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2938 info_ptr += bytes_read;
2939 cu_header->version = read_2_bytes (abfd, info_ptr);
2940 info_ptr += 2;
2941 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2942 &bytes_read);
2943 info_ptr += bytes_read;
2944 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2945 info_ptr += 1;
2946 signed_addr = bfd_get_sign_extend_vma (abfd);
2947 if (signed_addr < 0)
2948 internal_error (__FILE__, __LINE__,
2949 _("read_comp_unit_head: dwarf from non elf file"));
2950 cu_header->signed_addr_p = signed_addr;
2951
2952 return info_ptr;
2953 }
2954
2955 /* Subroutine of read_and_check_comp_unit_head and
2956 read_and_check_type_unit_head to simplify them.
2957 Perform various error checking on the header. */
2958
2959 static void
2960 error_check_comp_unit_head (struct comp_unit_head *header,
2961 struct dwarf2_section_info *section)
2962 {
2963 bfd *abfd = section->asection->owner;
2964 const char *filename = bfd_get_filename (abfd);
2965
2966 if (header->version != 2 && header->version != 3 && header->version != 4)
2967 error (_("Dwarf Error: wrong version in compilation unit header "
2968 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2969 filename);
2970
2971 if (header->abbrev_offset
2972 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2973 &dwarf2_per_objfile->abbrev))
2974 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2975 "(offset 0x%lx + 6) [in module %s]"),
2976 (long) header->abbrev_offset, (long) header->offset,
2977 filename);
2978
2979 /* Cast to unsigned long to use 64-bit arithmetic when possible to
2980 avoid potential 32-bit overflow. */
2981 if (((unsigned long) header->offset
2982 + header->length + header->initial_length_size)
2983 > section->size)
2984 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2985 "(offset 0x%lx + 0) [in module %s]"),
2986 (long) header->length, (long) header->offset,
2987 filename);
2988 }
2989
2990 /* Read in a CU/TU header and perform some basic error checking.
2991 The contents of the header are stored in HEADER.
2992 The result is a pointer to the start of the first DIE. */
2993
2994 static gdb_byte *
2995 read_and_check_comp_unit_head (struct comp_unit_head *header,
2996 struct dwarf2_section_info *section,
2997 gdb_byte *info_ptr,
2998 int is_debug_types_section)
2999 {
3000 gdb_byte *beg_of_comp_unit = info_ptr;
3001 bfd *abfd = section->asection->owner;
3002
3003 header->offset = beg_of_comp_unit - section->buffer;
3004
3005 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3006
3007 /* If we're reading a type unit, skip over the signature and
3008 type_offset fields. */
3009 if (is_debug_types_section)
3010 info_ptr += 8 /*signature*/ + header->offset_size;
3011
3012 header->first_die_offset = info_ptr - beg_of_comp_unit;
3013
3014 error_check_comp_unit_head (header, section);
3015
3016 return info_ptr;
3017 }
3018
3019 /* Read in the types comp unit header information from .debug_types entry at
3020 types_ptr. The result is a pointer to one past the end of the header. */
3021
3022 static gdb_byte *
3023 read_and_check_type_unit_head (struct comp_unit_head *header,
3024 struct dwarf2_section_info *section,
3025 gdb_byte *info_ptr,
3026 ULONGEST *signature, unsigned int *type_offset)
3027 {
3028 gdb_byte *beg_of_comp_unit = info_ptr;
3029 bfd *abfd = section->asection->owner;
3030
3031 header->offset = beg_of_comp_unit - section->buffer;
3032
3033 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3034
3035 /* If we're reading a type unit, skip over the signature and
3036 type_offset fields. */
3037 if (signature != NULL)
3038 *signature = read_8_bytes (abfd, info_ptr);
3039 info_ptr += 8;
3040 if (type_offset != NULL)
3041 *type_offset = read_offset_1 (abfd, info_ptr, header->offset_size);
3042 info_ptr += header->offset_size;
3043
3044 header->first_die_offset = info_ptr - beg_of_comp_unit;
3045
3046 error_check_comp_unit_head (header, section);
3047
3048 return info_ptr;
3049 }
3050
3051 /* Allocate a new partial symtab for file named NAME and mark this new
3052 partial symtab as being an include of PST. */
3053
3054 static void
3055 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3056 struct objfile *objfile)
3057 {
3058 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3059
3060 subpst->section_offsets = pst->section_offsets;
3061 subpst->textlow = 0;
3062 subpst->texthigh = 0;
3063
3064 subpst->dependencies = (struct partial_symtab **)
3065 obstack_alloc (&objfile->objfile_obstack,
3066 sizeof (struct partial_symtab *));
3067 subpst->dependencies[0] = pst;
3068 subpst->number_of_dependencies = 1;
3069
3070 subpst->globals_offset = 0;
3071 subpst->n_global_syms = 0;
3072 subpst->statics_offset = 0;
3073 subpst->n_static_syms = 0;
3074 subpst->symtab = NULL;
3075 subpst->read_symtab = pst->read_symtab;
3076 subpst->readin = 0;
3077
3078 /* No private part is necessary for include psymtabs. This property
3079 can be used to differentiate between such include psymtabs and
3080 the regular ones. */
3081 subpst->read_symtab_private = NULL;
3082 }
3083
3084 /* Read the Line Number Program data and extract the list of files
3085 included by the source file represented by PST. Build an include
3086 partial symtab for each of these included files. */
3087
3088 static void
3089 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3090 struct die_info *die,
3091 struct partial_symtab *pst)
3092 {
3093 struct objfile *objfile = cu->objfile;
3094 bfd *abfd = objfile->obfd;
3095 struct line_header *lh = NULL;
3096 struct attribute *attr;
3097
3098 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3099 if (attr)
3100 {
3101 unsigned int line_offset = DW_UNSND (attr);
3102
3103 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3104 }
3105 if (lh == NULL)
3106 return; /* No linetable, so no includes. */
3107
3108 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3109 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3110
3111 free_line_header (lh);
3112 }
3113
3114 static hashval_t
3115 hash_type_signature (const void *item)
3116 {
3117 const struct signatured_type *type_sig = item;
3118
3119 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3120 return type_sig->signature;
3121 }
3122
3123 static int
3124 eq_type_signature (const void *item_lhs, const void *item_rhs)
3125 {
3126 const struct signatured_type *lhs = item_lhs;
3127 const struct signatured_type *rhs = item_rhs;
3128
3129 return lhs->signature == rhs->signature;
3130 }
3131
3132 /* Allocate a hash table for signatured types. */
3133
3134 static htab_t
3135 allocate_signatured_type_table (struct objfile *objfile)
3136 {
3137 return htab_create_alloc_ex (41,
3138 hash_type_signature,
3139 eq_type_signature,
3140 NULL,
3141 &objfile->objfile_obstack,
3142 hashtab_obstack_allocate,
3143 dummy_obstack_deallocate);
3144 }
3145
3146 /* A helper function to add a signatured type CU to a table. */
3147
3148 static int
3149 add_signatured_type_cu_to_table (void **slot, void *datum)
3150 {
3151 struct signatured_type *sigt = *slot;
3152 struct dwarf2_per_cu_data ***datap = datum;
3153
3154 **datap = &sigt->per_cu;
3155 ++*datap;
3156
3157 return 1;
3158 }
3159
3160 /* Create the hash table of all entries in the .debug_types section(s).
3161 The result is zero if there is an error (e.g. missing .debug_types section),
3162 otherwise non-zero. */
3163
3164 static int
3165 create_debug_types_hash_table (struct objfile *objfile)
3166 {
3167 htab_t types_htab = NULL;
3168 struct dwarf2_per_cu_data **iter;
3169 int ix;
3170 struct dwarf2_section_info *section;
3171
3172 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3173 {
3174 dwarf2_per_objfile->signatured_types = NULL;
3175 return 0;
3176 }
3177
3178 for (ix = 0;
3179 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3180 ix, section);
3181 ++ix)
3182 {
3183 gdb_byte *info_ptr, *end_ptr;
3184
3185 dwarf2_read_section (objfile, section);
3186 info_ptr = section->buffer;
3187
3188 if (info_ptr == NULL)
3189 continue;
3190
3191 if (types_htab == NULL)
3192 types_htab = allocate_signatured_type_table (objfile);
3193
3194 if (dwarf2_die_debug)
3195 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3196
3197 end_ptr = info_ptr + section->size;
3198 while (info_ptr < end_ptr)
3199 {
3200 unsigned int offset;
3201 unsigned int type_offset;
3202 ULONGEST signature;
3203 struct signatured_type *type_sig;
3204 void **slot;
3205 gdb_byte *ptr = info_ptr;
3206 struct comp_unit_head header;
3207
3208 offset = ptr - section->buffer;
3209
3210 /* We need to read the type's signature in order to build the hash
3211 table, but we don't need anything else just yet. */
3212
3213 ptr = read_and_check_type_unit_head (&header, section, ptr,
3214 &signature, &type_offset);
3215
3216 /* Skip dummy type units. */
3217 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3218 {
3219 info_ptr = info_ptr + header.initial_length_size + header.length;
3220 continue;
3221 }
3222
3223 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3224 memset (type_sig, 0, sizeof (*type_sig));
3225 type_sig->signature = signature;
3226 type_sig->type_offset = type_offset;
3227 type_sig->per_cu.objfile = objfile;
3228 type_sig->per_cu.debug_types_section = section;
3229 type_sig->per_cu.offset = offset;
3230
3231 slot = htab_find_slot (types_htab, type_sig, INSERT);
3232 gdb_assert (slot != NULL);
3233 if (*slot != NULL)
3234 {
3235 const struct signatured_type *dup_sig = *slot;
3236
3237 complaint (&symfile_complaints,
3238 _("debug type entry at offset 0x%x is duplicate to the "
3239 "entry at offset 0x%x, signature 0x%s"),
3240 offset, dup_sig->per_cu.offset,
3241 phex (signature, sizeof (signature)));
3242 gdb_assert (signature == dup_sig->signature);
3243 }
3244 *slot = type_sig;
3245
3246 if (dwarf2_die_debug)
3247 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3248 offset, phex (signature, sizeof (signature)));
3249
3250 info_ptr = info_ptr + header.initial_length_size + header.length;
3251 }
3252 }
3253
3254 dwarf2_per_objfile->signatured_types = types_htab;
3255
3256 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3257 dwarf2_per_objfile->all_type_units
3258 = obstack_alloc (&objfile->objfile_obstack,
3259 dwarf2_per_objfile->n_type_units
3260 * sizeof (struct dwarf2_per_cu_data *));
3261 iter = &dwarf2_per_objfile->all_type_units[0];
3262 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3263 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3264 == dwarf2_per_objfile->n_type_units);
3265
3266 return 1;
3267 }
3268
3269 /* Lookup a signature based type.
3270 Returns NULL if SIG is not present in the table. */
3271
3272 static struct signatured_type *
3273 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3274 {
3275 struct signatured_type find_entry, *entry;
3276
3277 if (dwarf2_per_objfile->signatured_types == NULL)
3278 {
3279 complaint (&symfile_complaints,
3280 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3281 return 0;
3282 }
3283
3284 find_entry.signature = sig;
3285 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3286 return entry;
3287 }
3288
3289 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3290
3291 static void
3292 init_cu_die_reader (struct die_reader_specs *reader,
3293 struct dwarf2_cu *cu)
3294 {
3295 reader->abfd = cu->objfile->obfd;
3296 reader->cu = cu;
3297 if (cu->per_cu->debug_types_section)
3298 {
3299 gdb_assert (cu->per_cu->debug_types_section->readin);
3300 reader->buffer = cu->per_cu->debug_types_section->buffer;
3301 }
3302 else
3303 {
3304 gdb_assert (dwarf2_per_objfile->info.readin);
3305 reader->buffer = dwarf2_per_objfile->info.buffer;
3306 }
3307 }
3308
3309 /* Find the base address of the compilation unit for range lists and
3310 location lists. It will normally be specified by DW_AT_low_pc.
3311 In DWARF-3 draft 4, the base address could be overridden by
3312 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3313 compilation units with discontinuous ranges. */
3314
3315 static void
3316 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3317 {
3318 struct attribute *attr;
3319
3320 cu->base_known = 0;
3321 cu->base_address = 0;
3322
3323 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3324 if (attr)
3325 {
3326 cu->base_address = DW_ADDR (attr);
3327 cu->base_known = 1;
3328 }
3329 else
3330 {
3331 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3332 if (attr)
3333 {
3334 cu->base_address = DW_ADDR (attr);
3335 cu->base_known = 1;
3336 }
3337 }
3338 }
3339
3340 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3341 to combine the common parts.
3342 Process compilation unit THIS_CU for a psymtab.
3343 SECTION is the section the CU/TU comes from,
3344 either .debug_info or .debug_types. */
3345
3346 void
3347 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3348 struct dwarf2_section_info *section,
3349 int is_debug_types_section)
3350 {
3351 struct objfile *objfile = this_cu->objfile;
3352 bfd *abfd = objfile->obfd;
3353 gdb_byte *buffer = section->buffer;
3354 gdb_byte *info_ptr = buffer + this_cu->offset;
3355 unsigned int buffer_size = section->size;
3356 gdb_byte *beg_of_comp_unit = info_ptr;
3357 struct die_info *comp_unit_die;
3358 struct partial_symtab *pst;
3359 CORE_ADDR baseaddr;
3360 struct cleanup *back_to_inner;
3361 struct dwarf2_cu cu;
3362 int has_children, has_pc_info;
3363 struct attribute *attr;
3364 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3365 struct die_reader_specs reader_specs;
3366 const char *filename;
3367
3368 /* If this compilation unit was already read in, free the
3369 cached copy in order to read it in again. This is
3370 necessary because we skipped some symbols when we first
3371 read in the compilation unit (see load_partial_dies).
3372 This problem could be avoided, but the benefit is
3373 unclear. */
3374 if (this_cu->cu != NULL)
3375 free_one_cached_comp_unit (this_cu->cu);
3376
3377 /* Note that this is a pointer to our stack frame, being
3378 added to a global data structure. It will be cleaned up
3379 in free_stack_comp_unit when we finish with this
3380 compilation unit. */
3381 init_one_comp_unit (&cu, this_cu);
3382 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3383
3384 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3385 is_debug_types_section);
3386
3387 /* Skip dummy compilation units. */
3388 if (info_ptr >= buffer + buffer_size
3389 || peek_abbrev_code (abfd, info_ptr) == 0)
3390 {
3391 do_cleanups (back_to_inner);
3392 return;
3393 }
3394
3395 cu.list_in_scope = &file_symbols;
3396
3397 /* Read the abbrevs for this compilation unit into a table. */
3398 dwarf2_read_abbrevs (&cu);
3399 make_cleanup (dwarf2_free_abbrev_table, &cu);
3400
3401 /* Read the compilation unit die. */
3402 init_cu_die_reader (&reader_specs, &cu);
3403 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3404 &has_children);
3405
3406 if (is_debug_types_section)
3407 {
3408 /* LENGTH has not been set yet for type units. */
3409 gdb_assert (this_cu->offset == cu.header.offset);
3410 this_cu->length = cu.header.length + cu.header.initial_length_size;
3411 }
3412 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3413 {
3414 do_cleanups (back_to_inner);
3415 return;
3416 }
3417
3418 prepare_one_comp_unit (&cu, comp_unit_die);
3419
3420 /* Allocate a new partial symbol table structure. */
3421 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3422 if (attr == NULL || !DW_STRING (attr))
3423 filename = "";
3424 else
3425 filename = DW_STRING (attr);
3426 pst = start_psymtab_common (objfile, objfile->section_offsets,
3427 filename,
3428 /* TEXTLOW and TEXTHIGH are set below. */
3429 0,
3430 objfile->global_psymbols.next,
3431 objfile->static_psymbols.next);
3432 pst->psymtabs_addrmap_supported = 1;
3433
3434 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3435 if (attr != NULL)
3436 pst->dirname = DW_STRING (attr);
3437
3438 pst->read_symtab_private = this_cu;
3439
3440 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3441
3442 /* Store the function that reads in the rest of the symbol table. */
3443 pst->read_symtab = dwarf2_psymtab_to_symtab;
3444
3445 this_cu->v.psymtab = pst;
3446
3447 dwarf2_find_base_address (comp_unit_die, &cu);
3448
3449 /* Possibly set the default values of LOWPC and HIGHPC from
3450 `DW_AT_ranges'. */
3451 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3452 &best_highpc, &cu, pst);
3453 if (has_pc_info == 1 && best_lowpc < best_highpc)
3454 /* Store the contiguous range if it is not empty; it can be empty for
3455 CUs with no code. */
3456 addrmap_set_empty (objfile->psymtabs_addrmap,
3457 best_lowpc + baseaddr,
3458 best_highpc + baseaddr - 1, pst);
3459
3460 /* Check if comp unit has_children.
3461 If so, read the rest of the partial symbols from this comp unit.
3462 If not, there's no more debug_info for this comp unit. */
3463 if (has_children)
3464 {
3465 struct partial_die_info *first_die;
3466 CORE_ADDR lowpc, highpc;
3467
3468 lowpc = ((CORE_ADDR) -1);
3469 highpc = ((CORE_ADDR) 0);
3470
3471 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3472
3473 scan_partial_symbols (first_die, &lowpc, &highpc,
3474 ! has_pc_info, &cu);
3475
3476 /* If we didn't find a lowpc, set it to highpc to avoid
3477 complaints from `maint check'. */
3478 if (lowpc == ((CORE_ADDR) -1))
3479 lowpc = highpc;
3480
3481 /* If the compilation unit didn't have an explicit address range,
3482 then use the information extracted from its child dies. */
3483 if (! has_pc_info)
3484 {
3485 best_lowpc = lowpc;
3486 best_highpc = highpc;
3487 }
3488 }
3489 pst->textlow = best_lowpc + baseaddr;
3490 pst->texthigh = best_highpc + baseaddr;
3491
3492 pst->n_global_syms = objfile->global_psymbols.next -
3493 (objfile->global_psymbols.list + pst->globals_offset);
3494 pst->n_static_syms = objfile->static_psymbols.next -
3495 (objfile->static_psymbols.list + pst->statics_offset);
3496 sort_pst_symbols (pst);
3497
3498 if (is_debug_types_section)
3499 {
3500 /* It's not clear we want to do anything with stmt lists here.
3501 Waiting to see what gcc ultimately does. */
3502 }
3503 else
3504 {
3505 /* Get the list of files included in the current compilation unit,
3506 and build a psymtab for each of them. */
3507 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3508 }
3509
3510 do_cleanups (back_to_inner);
3511 }
3512
3513 /* Traversal function for htab_traverse_noresize.
3514 Process one .debug_types comp-unit. */
3515
3516 static int
3517 process_type_comp_unit (void **slot, void *info)
3518 {
3519 struct signatured_type *entry = (struct signatured_type *) *slot;
3520 struct dwarf2_per_cu_data *this_cu;
3521
3522 gdb_assert (info == NULL);
3523 this_cu = &entry->per_cu;
3524
3525 gdb_assert (this_cu->debug_types_section->readin);
3526 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3527
3528 return 1;
3529 }
3530
3531 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3532 Build partial symbol tables for the .debug_types comp-units. */
3533
3534 static void
3535 build_type_psymtabs (struct objfile *objfile)
3536 {
3537 if (! create_debug_types_hash_table (objfile))
3538 return;
3539
3540 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3541 process_type_comp_unit, NULL);
3542 }
3543
3544 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3545
3546 static void
3547 psymtabs_addrmap_cleanup (void *o)
3548 {
3549 struct objfile *objfile = o;
3550
3551 objfile->psymtabs_addrmap = NULL;
3552 }
3553
3554 /* Build the partial symbol table by doing a quick pass through the
3555 .debug_info and .debug_abbrev sections. */
3556
3557 static void
3558 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3559 {
3560 struct cleanup *back_to, *addrmap_cleanup;
3561 struct obstack temp_obstack;
3562 int i;
3563
3564 dwarf2_per_objfile->reading_partial_symbols = 1;
3565
3566 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3567
3568 /* Any cached compilation units will be linked by the per-objfile
3569 read_in_chain. Make sure to free them when we're done. */
3570 back_to = make_cleanup (free_cached_comp_units, NULL);
3571
3572 build_type_psymtabs (objfile);
3573
3574 create_all_comp_units (objfile);
3575
3576 /* Create a temporary address map on a temporary obstack. We later
3577 copy this to the final obstack. */
3578 obstack_init (&temp_obstack);
3579 make_cleanup_obstack_free (&temp_obstack);
3580 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3581 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3582
3583 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3584 {
3585 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3586
3587 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3588 }
3589
3590 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3591 &objfile->objfile_obstack);
3592 discard_cleanups (addrmap_cleanup);
3593
3594 do_cleanups (back_to);
3595 }
3596
3597 /* Load the partial DIEs for a secondary CU into memory. */
3598
3599 static void
3600 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3601 {
3602 struct objfile *objfile = this_cu->objfile;
3603 bfd *abfd = objfile->obfd;
3604 gdb_byte *info_ptr;
3605 struct die_info *comp_unit_die;
3606 struct dwarf2_cu *cu;
3607 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3608 int has_children;
3609 struct die_reader_specs reader_specs;
3610 int read_cu = 0;
3611 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
3612
3613 gdb_assert (! this_cu->debug_types_section);
3614
3615 gdb_assert (section->readin);
3616 info_ptr = section->buffer + this_cu->offset;
3617
3618 if (this_cu->cu == NULL)
3619 {
3620 cu = xmalloc (sizeof (*cu));
3621 init_one_comp_unit (cu, this_cu);
3622
3623 read_cu = 1;
3624
3625 /* If an error occurs while loading, release our storage. */
3626 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3627
3628 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3629 0);
3630
3631 /* Skip dummy compilation units. */
3632 if (info_ptr >= (section->buffer + section->size)
3633 || peek_abbrev_code (abfd, info_ptr) == 0)
3634 {
3635 do_cleanups (free_cu_cleanup);
3636 return;
3637 }
3638
3639 /* Link this CU into read_in_chain. */
3640 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3641 dwarf2_per_objfile->read_in_chain = this_cu;
3642 }
3643 else
3644 {
3645 cu = this_cu->cu;
3646 info_ptr += cu->header.first_die_offset;
3647 }
3648
3649 /* Read the abbrevs for this compilation unit into a table. */
3650 gdb_assert (cu->dwarf2_abbrevs == NULL);
3651 dwarf2_read_abbrevs (cu);
3652 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3653
3654 /* Read the compilation unit die. */
3655 init_cu_die_reader (&reader_specs, cu);
3656 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3657 &has_children);
3658
3659 prepare_one_comp_unit (cu, comp_unit_die);
3660
3661 /* Check if comp unit has_children.
3662 If so, read the rest of the partial symbols from this comp unit.
3663 If not, there's no more debug_info for this comp unit. */
3664 if (has_children)
3665 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
3666
3667 do_cleanups (free_abbrevs_cleanup);
3668
3669 if (read_cu)
3670 {
3671 /* We've successfully allocated this compilation unit. Let our
3672 caller clean it up when finished with it. */
3673 discard_cleanups (free_cu_cleanup);
3674 }
3675 }
3676
3677 /* Create a list of all compilation units in OBJFILE.
3678 This is only done for -readnow and building partial symtabs. */
3679
3680 static void
3681 create_all_comp_units (struct objfile *objfile)
3682 {
3683 int n_allocated;
3684 int n_comp_units;
3685 struct dwarf2_per_cu_data **all_comp_units;
3686 gdb_byte *info_ptr;
3687
3688 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3689 info_ptr = dwarf2_per_objfile->info.buffer;
3690
3691 n_comp_units = 0;
3692 n_allocated = 10;
3693 all_comp_units = xmalloc (n_allocated
3694 * sizeof (struct dwarf2_per_cu_data *));
3695
3696 while (info_ptr < dwarf2_per_objfile->info.buffer
3697 + dwarf2_per_objfile->info.size)
3698 {
3699 unsigned int length, initial_length_size;
3700 struct dwarf2_per_cu_data *this_cu;
3701 unsigned int offset;
3702
3703 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3704
3705 /* Read just enough information to find out where the next
3706 compilation unit is. */
3707 length = read_initial_length (objfile->obfd, info_ptr,
3708 &initial_length_size);
3709
3710 /* Save the compilation unit for later lookup. */
3711 this_cu = obstack_alloc (&objfile->objfile_obstack,
3712 sizeof (struct dwarf2_per_cu_data));
3713 memset (this_cu, 0, sizeof (*this_cu));
3714 this_cu->offset = offset;
3715 this_cu->length = length + initial_length_size;
3716 this_cu->objfile = objfile;
3717
3718 if (n_comp_units == n_allocated)
3719 {
3720 n_allocated *= 2;
3721 all_comp_units = xrealloc (all_comp_units,
3722 n_allocated
3723 * sizeof (struct dwarf2_per_cu_data *));
3724 }
3725 all_comp_units[n_comp_units++] = this_cu;
3726
3727 info_ptr = info_ptr + this_cu->length;
3728 }
3729
3730 dwarf2_per_objfile->all_comp_units
3731 = obstack_alloc (&objfile->objfile_obstack,
3732 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3733 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3734 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3735 xfree (all_comp_units);
3736 dwarf2_per_objfile->n_comp_units = n_comp_units;
3737 }
3738
3739 /* Process all loaded DIEs for compilation unit CU, starting at
3740 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3741 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3742 DW_AT_ranges). If NEED_PC is set, then this function will set
3743 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3744 and record the covered ranges in the addrmap. */
3745
3746 static void
3747 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3748 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3749 {
3750 struct partial_die_info *pdi;
3751
3752 /* Now, march along the PDI's, descending into ones which have
3753 interesting children but skipping the children of the other ones,
3754 until we reach the end of the compilation unit. */
3755
3756 pdi = first_die;
3757
3758 while (pdi != NULL)
3759 {
3760 fixup_partial_die (pdi, cu);
3761
3762 /* Anonymous namespaces or modules have no name but have interesting
3763 children, so we need to look at them. Ditto for anonymous
3764 enums. */
3765
3766 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3767 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3768 {
3769 switch (pdi->tag)
3770 {
3771 case DW_TAG_subprogram:
3772 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3773 break;
3774 case DW_TAG_constant:
3775 case DW_TAG_variable:
3776 case DW_TAG_typedef:
3777 case DW_TAG_union_type:
3778 if (!pdi->is_declaration)
3779 {
3780 add_partial_symbol (pdi, cu);
3781 }
3782 break;
3783 case DW_TAG_class_type:
3784 case DW_TAG_interface_type:
3785 case DW_TAG_structure_type:
3786 if (!pdi->is_declaration)
3787 {
3788 add_partial_symbol (pdi, cu);
3789 }
3790 break;
3791 case DW_TAG_enumeration_type:
3792 if (!pdi->is_declaration)
3793 add_partial_enumeration (pdi, cu);
3794 break;
3795 case DW_TAG_base_type:
3796 case DW_TAG_subrange_type:
3797 /* File scope base type definitions are added to the partial
3798 symbol table. */
3799 add_partial_symbol (pdi, cu);
3800 break;
3801 case DW_TAG_namespace:
3802 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3803 break;
3804 case DW_TAG_module:
3805 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3806 break;
3807 default:
3808 break;
3809 }
3810 }
3811
3812 /* If the die has a sibling, skip to the sibling. */
3813
3814 pdi = pdi->die_sibling;
3815 }
3816 }
3817
3818 /* Functions used to compute the fully scoped name of a partial DIE.
3819
3820 Normally, this is simple. For C++, the parent DIE's fully scoped
3821 name is concatenated with "::" and the partial DIE's name. For
3822 Java, the same thing occurs except that "." is used instead of "::".
3823 Enumerators are an exception; they use the scope of their parent
3824 enumeration type, i.e. the name of the enumeration type is not
3825 prepended to the enumerator.
3826
3827 There are two complexities. One is DW_AT_specification; in this
3828 case "parent" means the parent of the target of the specification,
3829 instead of the direct parent of the DIE. The other is compilers
3830 which do not emit DW_TAG_namespace; in this case we try to guess
3831 the fully qualified name of structure types from their members'
3832 linkage names. This must be done using the DIE's children rather
3833 than the children of any DW_AT_specification target. We only need
3834 to do this for structures at the top level, i.e. if the target of
3835 any DW_AT_specification (if any; otherwise the DIE itself) does not
3836 have a parent. */
3837
3838 /* Compute the scope prefix associated with PDI's parent, in
3839 compilation unit CU. The result will be allocated on CU's
3840 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3841 field. NULL is returned if no prefix is necessary. */
3842 static char *
3843 partial_die_parent_scope (struct partial_die_info *pdi,
3844 struct dwarf2_cu *cu)
3845 {
3846 char *grandparent_scope;
3847 struct partial_die_info *parent, *real_pdi;
3848
3849 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3850 then this means the parent of the specification DIE. */
3851
3852 real_pdi = pdi;
3853 while (real_pdi->has_specification)
3854 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3855
3856 parent = real_pdi->die_parent;
3857 if (parent == NULL)
3858 return NULL;
3859
3860 if (parent->scope_set)
3861 return parent->scope;
3862
3863 fixup_partial_die (parent, cu);
3864
3865 grandparent_scope = partial_die_parent_scope (parent, cu);
3866
3867 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3868 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3869 Work around this problem here. */
3870 if (cu->language == language_cplus
3871 && parent->tag == DW_TAG_namespace
3872 && strcmp (parent->name, "::") == 0
3873 && grandparent_scope == NULL)
3874 {
3875 parent->scope = NULL;
3876 parent->scope_set = 1;
3877 return NULL;
3878 }
3879
3880 if (pdi->tag == DW_TAG_enumerator)
3881 /* Enumerators should not get the name of the enumeration as a prefix. */
3882 parent->scope = grandparent_scope;
3883 else if (parent->tag == DW_TAG_namespace
3884 || parent->tag == DW_TAG_module
3885 || parent->tag == DW_TAG_structure_type
3886 || parent->tag == DW_TAG_class_type
3887 || parent->tag == DW_TAG_interface_type
3888 || parent->tag == DW_TAG_union_type
3889 || parent->tag == DW_TAG_enumeration_type)
3890 {
3891 if (grandparent_scope == NULL)
3892 parent->scope = parent->name;
3893 else
3894 parent->scope = typename_concat (&cu->comp_unit_obstack,
3895 grandparent_scope,
3896 parent->name, 0, cu);
3897 }
3898 else
3899 {
3900 /* FIXME drow/2004-04-01: What should we be doing with
3901 function-local names? For partial symbols, we should probably be
3902 ignoring them. */
3903 complaint (&symfile_complaints,
3904 _("unhandled containing DIE tag %d for DIE at %d"),
3905 parent->tag, pdi->offset);
3906 parent->scope = grandparent_scope;
3907 }
3908
3909 parent->scope_set = 1;
3910 return parent->scope;
3911 }
3912
3913 /* Return the fully scoped name associated with PDI, from compilation unit
3914 CU. The result will be allocated with malloc. */
3915 static char *
3916 partial_die_full_name (struct partial_die_info *pdi,
3917 struct dwarf2_cu *cu)
3918 {
3919 char *parent_scope;
3920
3921 /* If this is a template instantiation, we can not work out the
3922 template arguments from partial DIEs. So, unfortunately, we have
3923 to go through the full DIEs. At least any work we do building
3924 types here will be reused if full symbols are loaded later. */
3925 if (pdi->has_template_arguments)
3926 {
3927 fixup_partial_die (pdi, cu);
3928
3929 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3930 {
3931 struct die_info *die;
3932 struct attribute attr;
3933 struct dwarf2_cu *ref_cu = cu;
3934
3935 attr.name = 0;
3936 attr.form = DW_FORM_ref_addr;
3937 attr.u.addr = pdi->offset;
3938 die = follow_die_ref (NULL, &attr, &ref_cu);
3939
3940 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3941 }
3942 }
3943
3944 parent_scope = partial_die_parent_scope (pdi, cu);
3945 if (parent_scope == NULL)
3946 return NULL;
3947 else
3948 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3949 }
3950
3951 static void
3952 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3953 {
3954 struct objfile *objfile = cu->objfile;
3955 CORE_ADDR addr = 0;
3956 char *actual_name = NULL;
3957 CORE_ADDR baseaddr;
3958 int built_actual_name = 0;
3959
3960 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3961
3962 actual_name = partial_die_full_name (pdi, cu);
3963 if (actual_name)
3964 built_actual_name = 1;
3965
3966 if (actual_name == NULL)
3967 actual_name = pdi->name;
3968
3969 switch (pdi->tag)
3970 {
3971 case DW_TAG_subprogram:
3972 if (pdi->is_external || cu->language == language_ada)
3973 {
3974 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3975 of the global scope. But in Ada, we want to be able to access
3976 nested procedures globally. So all Ada subprograms are stored
3977 in the global scope. */
3978 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3979 mst_text, objfile); */
3980 add_psymbol_to_list (actual_name, strlen (actual_name),
3981 built_actual_name,
3982 VAR_DOMAIN, LOC_BLOCK,
3983 &objfile->global_psymbols,
3984 0, pdi->lowpc + baseaddr,
3985 cu->language, objfile);
3986 }
3987 else
3988 {
3989 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3990 mst_file_text, objfile); */
3991 add_psymbol_to_list (actual_name, strlen (actual_name),
3992 built_actual_name,
3993 VAR_DOMAIN, LOC_BLOCK,
3994 &objfile->static_psymbols,
3995 0, pdi->lowpc + baseaddr,
3996 cu->language, objfile);
3997 }
3998 break;
3999 case DW_TAG_constant:
4000 {
4001 struct psymbol_allocation_list *list;
4002
4003 if (pdi->is_external)
4004 list = &objfile->global_psymbols;
4005 else
4006 list = &objfile->static_psymbols;
4007 add_psymbol_to_list (actual_name, strlen (actual_name),
4008 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4009 list, 0, 0, cu->language, objfile);
4010 }
4011 break;
4012 case DW_TAG_variable:
4013 if (pdi->locdesc)
4014 addr = decode_locdesc (pdi->locdesc, cu);
4015
4016 if (pdi->locdesc
4017 && addr == 0
4018 && !dwarf2_per_objfile->has_section_at_zero)
4019 {
4020 /* A global or static variable may also have been stripped
4021 out by the linker if unused, in which case its address
4022 will be nullified; do not add such variables into partial
4023 symbol table then. */
4024 }
4025 else if (pdi->is_external)
4026 {
4027 /* Global Variable.
4028 Don't enter into the minimal symbol tables as there is
4029 a minimal symbol table entry from the ELF symbols already.
4030 Enter into partial symbol table if it has a location
4031 descriptor or a type.
4032 If the location descriptor is missing, new_symbol will create
4033 a LOC_UNRESOLVED symbol, the address of the variable will then
4034 be determined from the minimal symbol table whenever the variable
4035 is referenced.
4036 The address for the partial symbol table entry is not
4037 used by GDB, but it comes in handy for debugging partial symbol
4038 table building. */
4039
4040 if (pdi->locdesc || pdi->has_type)
4041 add_psymbol_to_list (actual_name, strlen (actual_name),
4042 built_actual_name,
4043 VAR_DOMAIN, LOC_STATIC,
4044 &objfile->global_psymbols,
4045 0, addr + baseaddr,
4046 cu->language, objfile);
4047 }
4048 else
4049 {
4050 /* Static Variable. Skip symbols without location descriptors. */
4051 if (pdi->locdesc == NULL)
4052 {
4053 if (built_actual_name)
4054 xfree (actual_name);
4055 return;
4056 }
4057 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4058 mst_file_data, objfile); */
4059 add_psymbol_to_list (actual_name, strlen (actual_name),
4060 built_actual_name,
4061 VAR_DOMAIN, LOC_STATIC,
4062 &objfile->static_psymbols,
4063 0, addr + baseaddr,
4064 cu->language, objfile);
4065 }
4066 break;
4067 case DW_TAG_typedef:
4068 case DW_TAG_base_type:
4069 case DW_TAG_subrange_type:
4070 add_psymbol_to_list (actual_name, strlen (actual_name),
4071 built_actual_name,
4072 VAR_DOMAIN, LOC_TYPEDEF,
4073 &objfile->static_psymbols,
4074 0, (CORE_ADDR) 0, cu->language, objfile);
4075 break;
4076 case DW_TAG_namespace:
4077 add_psymbol_to_list (actual_name, strlen (actual_name),
4078 built_actual_name,
4079 VAR_DOMAIN, LOC_TYPEDEF,
4080 &objfile->global_psymbols,
4081 0, (CORE_ADDR) 0, cu->language, objfile);
4082 break;
4083 case DW_TAG_class_type:
4084 case DW_TAG_interface_type:
4085 case DW_TAG_structure_type:
4086 case DW_TAG_union_type:
4087 case DW_TAG_enumeration_type:
4088 /* Skip external references. The DWARF standard says in the section
4089 about "Structure, Union, and Class Type Entries": "An incomplete
4090 structure, union or class type is represented by a structure,
4091 union or class entry that does not have a byte size attribute
4092 and that has a DW_AT_declaration attribute." */
4093 if (!pdi->has_byte_size && pdi->is_declaration)
4094 {
4095 if (built_actual_name)
4096 xfree (actual_name);
4097 return;
4098 }
4099
4100 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4101 static vs. global. */
4102 add_psymbol_to_list (actual_name, strlen (actual_name),
4103 built_actual_name,
4104 STRUCT_DOMAIN, LOC_TYPEDEF,
4105 (cu->language == language_cplus
4106 || cu->language == language_java)
4107 ? &objfile->global_psymbols
4108 : &objfile->static_psymbols,
4109 0, (CORE_ADDR) 0, cu->language, objfile);
4110
4111 break;
4112 case DW_TAG_enumerator:
4113 add_psymbol_to_list (actual_name, strlen (actual_name),
4114 built_actual_name,
4115 VAR_DOMAIN, LOC_CONST,
4116 (cu->language == language_cplus
4117 || cu->language == language_java)
4118 ? &objfile->global_psymbols
4119 : &objfile->static_psymbols,
4120 0, (CORE_ADDR) 0, cu->language, objfile);
4121 break;
4122 default:
4123 break;
4124 }
4125
4126 if (built_actual_name)
4127 xfree (actual_name);
4128 }
4129
4130 /* Read a partial die corresponding to a namespace; also, add a symbol
4131 corresponding to that namespace to the symbol table. NAMESPACE is
4132 the name of the enclosing namespace. */
4133
4134 static void
4135 add_partial_namespace (struct partial_die_info *pdi,
4136 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4137 int need_pc, struct dwarf2_cu *cu)
4138 {
4139 /* Add a symbol for the namespace. */
4140
4141 add_partial_symbol (pdi, cu);
4142
4143 /* Now scan partial symbols in that namespace. */
4144
4145 if (pdi->has_children)
4146 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4147 }
4148
4149 /* Read a partial die corresponding to a Fortran module. */
4150
4151 static void
4152 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4153 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4154 {
4155 /* Now scan partial symbols in that module. */
4156
4157 if (pdi->has_children)
4158 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4159 }
4160
4161 /* Read a partial die corresponding to a subprogram and create a partial
4162 symbol for that subprogram. When the CU language allows it, this
4163 routine also defines a partial symbol for each nested subprogram
4164 that this subprogram contains.
4165
4166 DIE my also be a lexical block, in which case we simply search
4167 recursively for suprograms defined inside that lexical block.
4168 Again, this is only performed when the CU language allows this
4169 type of definitions. */
4170
4171 static void
4172 add_partial_subprogram (struct partial_die_info *pdi,
4173 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4174 int need_pc, struct dwarf2_cu *cu)
4175 {
4176 if (pdi->tag == DW_TAG_subprogram)
4177 {
4178 if (pdi->has_pc_info)
4179 {
4180 if (pdi->lowpc < *lowpc)
4181 *lowpc = pdi->lowpc;
4182 if (pdi->highpc > *highpc)
4183 *highpc = pdi->highpc;
4184 if (need_pc)
4185 {
4186 CORE_ADDR baseaddr;
4187 struct objfile *objfile = cu->objfile;
4188
4189 baseaddr = ANOFFSET (objfile->section_offsets,
4190 SECT_OFF_TEXT (objfile));
4191 addrmap_set_empty (objfile->psymtabs_addrmap,
4192 pdi->lowpc + baseaddr,
4193 pdi->highpc - 1 + baseaddr,
4194 cu->per_cu->v.psymtab);
4195 }
4196 if (!pdi->is_declaration)
4197 /* Ignore subprogram DIEs that do not have a name, they are
4198 illegal. Do not emit a complaint at this point, we will
4199 do so when we convert this psymtab into a symtab. */
4200 if (pdi->name)
4201 add_partial_symbol (pdi, cu);
4202 }
4203 }
4204
4205 if (! pdi->has_children)
4206 return;
4207
4208 if (cu->language == language_ada)
4209 {
4210 pdi = pdi->die_child;
4211 while (pdi != NULL)
4212 {
4213 fixup_partial_die (pdi, cu);
4214 if (pdi->tag == DW_TAG_subprogram
4215 || pdi->tag == DW_TAG_lexical_block)
4216 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4217 pdi = pdi->die_sibling;
4218 }
4219 }
4220 }
4221
4222 /* Read a partial die corresponding to an enumeration type. */
4223
4224 static void
4225 add_partial_enumeration (struct partial_die_info *enum_pdi,
4226 struct dwarf2_cu *cu)
4227 {
4228 struct partial_die_info *pdi;
4229
4230 if (enum_pdi->name != NULL)
4231 add_partial_symbol (enum_pdi, cu);
4232
4233 pdi = enum_pdi->die_child;
4234 while (pdi)
4235 {
4236 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4237 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4238 else
4239 add_partial_symbol (pdi, cu);
4240 pdi = pdi->die_sibling;
4241 }
4242 }
4243
4244 /* Return the initial uleb128 in the die at INFO_PTR. */
4245
4246 static unsigned int
4247 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4248 {
4249 unsigned int bytes_read;
4250
4251 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4252 }
4253
4254 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4255 Return the corresponding abbrev, or NULL if the number is zero (indicating
4256 an empty DIE). In either case *BYTES_READ will be set to the length of
4257 the initial number. */
4258
4259 static struct abbrev_info *
4260 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4261 struct dwarf2_cu *cu)
4262 {
4263 bfd *abfd = cu->objfile->obfd;
4264 unsigned int abbrev_number;
4265 struct abbrev_info *abbrev;
4266
4267 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4268
4269 if (abbrev_number == 0)
4270 return NULL;
4271
4272 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4273 if (!abbrev)
4274 {
4275 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4276 abbrev_number, bfd_get_filename (abfd));
4277 }
4278
4279 return abbrev;
4280 }
4281
4282 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4283 Returns a pointer to the end of a series of DIEs, terminated by an empty
4284 DIE. Any children of the skipped DIEs will also be skipped. */
4285
4286 static gdb_byte *
4287 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4288 {
4289 struct abbrev_info *abbrev;
4290 unsigned int bytes_read;
4291
4292 while (1)
4293 {
4294 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4295 if (abbrev == NULL)
4296 return info_ptr + bytes_read;
4297 else
4298 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4299 }
4300 }
4301
4302 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4303 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4304 abbrev corresponding to that skipped uleb128 should be passed in
4305 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4306 children. */
4307
4308 static gdb_byte *
4309 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4310 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4311 {
4312 unsigned int bytes_read;
4313 struct attribute attr;
4314 bfd *abfd = cu->objfile->obfd;
4315 unsigned int form, i;
4316
4317 for (i = 0; i < abbrev->num_attrs; i++)
4318 {
4319 /* The only abbrev we care about is DW_AT_sibling. */
4320 if (abbrev->attrs[i].name == DW_AT_sibling)
4321 {
4322 read_attribute (&attr, &abbrev->attrs[i],
4323 abfd, info_ptr, cu);
4324 if (attr.form == DW_FORM_ref_addr)
4325 complaint (&symfile_complaints,
4326 _("ignoring absolute DW_AT_sibling"));
4327 else
4328 return buffer + dwarf2_get_ref_die_offset (&attr);
4329 }
4330
4331 /* If it isn't DW_AT_sibling, skip this attribute. */
4332 form = abbrev->attrs[i].form;
4333 skip_attribute:
4334 switch (form)
4335 {
4336 case DW_FORM_ref_addr:
4337 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4338 and later it is offset sized. */
4339 if (cu->header.version == 2)
4340 info_ptr += cu->header.addr_size;
4341 else
4342 info_ptr += cu->header.offset_size;
4343 break;
4344 case DW_FORM_addr:
4345 info_ptr += cu->header.addr_size;
4346 break;
4347 case DW_FORM_data1:
4348 case DW_FORM_ref1:
4349 case DW_FORM_flag:
4350 info_ptr += 1;
4351 break;
4352 case DW_FORM_flag_present:
4353 break;
4354 case DW_FORM_data2:
4355 case DW_FORM_ref2:
4356 info_ptr += 2;
4357 break;
4358 case DW_FORM_data4:
4359 case DW_FORM_ref4:
4360 info_ptr += 4;
4361 break;
4362 case DW_FORM_data8:
4363 case DW_FORM_ref8:
4364 case DW_FORM_ref_sig8:
4365 info_ptr += 8;
4366 break;
4367 case DW_FORM_string:
4368 read_direct_string (abfd, info_ptr, &bytes_read);
4369 info_ptr += bytes_read;
4370 break;
4371 case DW_FORM_sec_offset:
4372 case DW_FORM_strp:
4373 info_ptr += cu->header.offset_size;
4374 break;
4375 case DW_FORM_exprloc:
4376 case DW_FORM_block:
4377 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4378 info_ptr += bytes_read;
4379 break;
4380 case DW_FORM_block1:
4381 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4382 break;
4383 case DW_FORM_block2:
4384 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4385 break;
4386 case DW_FORM_block4:
4387 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4388 break;
4389 case DW_FORM_sdata:
4390 case DW_FORM_udata:
4391 case DW_FORM_ref_udata:
4392 info_ptr = skip_leb128 (abfd, info_ptr);
4393 break;
4394 case DW_FORM_indirect:
4395 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4396 info_ptr += bytes_read;
4397 /* We need to continue parsing from here, so just go back to
4398 the top. */
4399 goto skip_attribute;
4400
4401 default:
4402 error (_("Dwarf Error: Cannot handle %s "
4403 "in DWARF reader [in module %s]"),
4404 dwarf_form_name (form),
4405 bfd_get_filename (abfd));
4406 }
4407 }
4408
4409 if (abbrev->has_children)
4410 return skip_children (buffer, info_ptr, cu);
4411 else
4412 return info_ptr;
4413 }
4414
4415 /* Locate ORIG_PDI's sibling.
4416 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4417 in BUFFER. */
4418
4419 static gdb_byte *
4420 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4421 gdb_byte *buffer, gdb_byte *info_ptr,
4422 bfd *abfd, struct dwarf2_cu *cu)
4423 {
4424 /* Do we know the sibling already? */
4425
4426 if (orig_pdi->sibling)
4427 return orig_pdi->sibling;
4428
4429 /* Are there any children to deal with? */
4430
4431 if (!orig_pdi->has_children)
4432 return info_ptr;
4433
4434 /* Skip the children the long way. */
4435
4436 return skip_children (buffer, info_ptr, cu);
4437 }
4438
4439 /* Expand this partial symbol table into a full symbol table. */
4440
4441 static void
4442 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4443 {
4444 if (pst != NULL)
4445 {
4446 if (pst->readin)
4447 {
4448 warning (_("bug: psymtab for %s is already read in."),
4449 pst->filename);
4450 }
4451 else
4452 {
4453 if (info_verbose)
4454 {
4455 printf_filtered (_("Reading in symbols for %s..."),
4456 pst->filename);
4457 gdb_flush (gdb_stdout);
4458 }
4459
4460 /* Restore our global data. */
4461 dwarf2_per_objfile = objfile_data (pst->objfile,
4462 dwarf2_objfile_data_key);
4463
4464 /* If this psymtab is constructed from a debug-only objfile, the
4465 has_section_at_zero flag will not necessarily be correct. We
4466 can get the correct value for this flag by looking at the data
4467 associated with the (presumably stripped) associated objfile. */
4468 if (pst->objfile->separate_debug_objfile_backlink)
4469 {
4470 struct dwarf2_per_objfile *dpo_backlink
4471 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4472 dwarf2_objfile_data_key);
4473
4474 dwarf2_per_objfile->has_section_at_zero
4475 = dpo_backlink->has_section_at_zero;
4476 }
4477
4478 dwarf2_per_objfile->reading_partial_symbols = 0;
4479
4480 psymtab_to_symtab_1 (pst);
4481
4482 /* Finish up the debug error message. */
4483 if (info_verbose)
4484 printf_filtered (_("done.\n"));
4485 }
4486 }
4487 }
4488 \f
4489 /* Reading in full CUs. */
4490
4491 /* Add PER_CU to the queue. */
4492
4493 static void
4494 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4495 {
4496 struct dwarf2_queue_item *item;
4497
4498 per_cu->queued = 1;
4499 item = xmalloc (sizeof (*item));
4500 item->per_cu = per_cu;
4501 item->next = NULL;
4502
4503 if (dwarf2_queue == NULL)
4504 dwarf2_queue = item;
4505 else
4506 dwarf2_queue_tail->next = item;
4507
4508 dwarf2_queue_tail = item;
4509 }
4510
4511 /* Process the queue. */
4512
4513 static void
4514 process_queue (void)
4515 {
4516 struct dwarf2_queue_item *item, *next_item;
4517
4518 /* The queue starts out with one item, but following a DIE reference
4519 may load a new CU, adding it to the end of the queue. */
4520 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4521 {
4522 if (dwarf2_per_objfile->using_index
4523 ? !item->per_cu->v.quick->symtab
4524 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4525 process_full_comp_unit (item->per_cu);
4526
4527 item->per_cu->queued = 0;
4528 next_item = item->next;
4529 xfree (item);
4530 }
4531
4532 dwarf2_queue_tail = NULL;
4533 }
4534
4535 /* Free all allocated queue entries. This function only releases anything if
4536 an error was thrown; if the queue was processed then it would have been
4537 freed as we went along. */
4538
4539 static void
4540 dwarf2_release_queue (void *dummy)
4541 {
4542 struct dwarf2_queue_item *item, *last;
4543
4544 item = dwarf2_queue;
4545 while (item)
4546 {
4547 /* Anything still marked queued is likely to be in an
4548 inconsistent state, so discard it. */
4549 if (item->per_cu->queued)
4550 {
4551 if (item->per_cu->cu != NULL)
4552 free_one_cached_comp_unit (item->per_cu->cu);
4553 item->per_cu->queued = 0;
4554 }
4555
4556 last = item;
4557 item = item->next;
4558 xfree (last);
4559 }
4560
4561 dwarf2_queue = dwarf2_queue_tail = NULL;
4562 }
4563
4564 /* Read in full symbols for PST, and anything it depends on. */
4565
4566 static void
4567 psymtab_to_symtab_1 (struct partial_symtab *pst)
4568 {
4569 struct dwarf2_per_cu_data *per_cu;
4570 struct cleanup *back_to;
4571 int i;
4572
4573 for (i = 0; i < pst->number_of_dependencies; i++)
4574 if (!pst->dependencies[i]->readin)
4575 {
4576 /* Inform about additional files that need to be read in. */
4577 if (info_verbose)
4578 {
4579 /* FIXME: i18n: Need to make this a single string. */
4580 fputs_filtered (" ", gdb_stdout);
4581 wrap_here ("");
4582 fputs_filtered ("and ", gdb_stdout);
4583 wrap_here ("");
4584 printf_filtered ("%s...", pst->dependencies[i]->filename);
4585 wrap_here (""); /* Flush output. */
4586 gdb_flush (gdb_stdout);
4587 }
4588 psymtab_to_symtab_1 (pst->dependencies[i]);
4589 }
4590
4591 per_cu = pst->read_symtab_private;
4592
4593 if (per_cu == NULL)
4594 {
4595 /* It's an include file, no symbols to read for it.
4596 Everything is in the parent symtab. */
4597 pst->readin = 1;
4598 return;
4599 }
4600
4601 dw2_do_instantiate_symtab (per_cu);
4602 }
4603
4604 /* Load the DIEs associated with PER_CU into memory. */
4605
4606 static void
4607 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4608 {
4609 struct objfile *objfile = per_cu->objfile;
4610 bfd *abfd = objfile->obfd;
4611 struct dwarf2_cu *cu;
4612 unsigned int offset;
4613 gdb_byte *info_ptr, *beg_of_comp_unit;
4614 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4615 struct attribute *attr;
4616 int read_cu = 0;
4617
4618 gdb_assert (! per_cu->debug_types_section);
4619
4620 /* Set local variables from the partial symbol table info. */
4621 offset = per_cu->offset;
4622
4623 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4624 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4625 beg_of_comp_unit = info_ptr;
4626
4627 if (per_cu->cu == NULL)
4628 {
4629 cu = xmalloc (sizeof (*cu));
4630 init_one_comp_unit (cu, per_cu);
4631
4632 read_cu = 1;
4633
4634 /* If an error occurs while loading, release our storage. */
4635 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4636
4637 /* Read in the comp_unit header. */
4638 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4639
4640 /* Skip dummy compilation units. */
4641 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4642 + dwarf2_per_objfile->info.size)
4643 || peek_abbrev_code (abfd, info_ptr) == 0)
4644 {
4645 do_cleanups (free_cu_cleanup);
4646 return;
4647 }
4648
4649 /* Complete the cu_header. */
4650 cu->header.offset = offset;
4651 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4652
4653 /* Read the abbrevs for this compilation unit. */
4654 dwarf2_read_abbrevs (cu);
4655 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4656
4657 /* Link this CU into read_in_chain. */
4658 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4659 dwarf2_per_objfile->read_in_chain = per_cu;
4660 }
4661 else
4662 {
4663 cu = per_cu->cu;
4664 info_ptr += cu->header.first_die_offset;
4665 }
4666
4667 cu->dies = read_comp_unit (info_ptr, cu);
4668
4669 /* We try not to read any attributes in this function, because not
4670 all CUs needed for references have been loaded yet, and symbol
4671 table processing isn't initialized. But we have to set the CU language,
4672 or we won't be able to build types correctly. */
4673 prepare_one_comp_unit (cu, cu->dies);
4674
4675 /* Similarly, if we do not read the producer, we can not apply
4676 producer-specific interpretation. */
4677 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4678 if (attr)
4679 cu->producer = DW_STRING (attr);
4680
4681 if (read_cu)
4682 {
4683 do_cleanups (free_abbrevs_cleanup);
4684
4685 /* We've successfully allocated this compilation unit. Let our
4686 caller clean it up when finished with it. */
4687 discard_cleanups (free_cu_cleanup);
4688 }
4689 }
4690
4691 /* Add a DIE to the delayed physname list. */
4692
4693 static void
4694 add_to_method_list (struct type *type, int fnfield_index, int index,
4695 const char *name, struct die_info *die,
4696 struct dwarf2_cu *cu)
4697 {
4698 struct delayed_method_info mi;
4699 mi.type = type;
4700 mi.fnfield_index = fnfield_index;
4701 mi.index = index;
4702 mi.name = name;
4703 mi.die = die;
4704 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4705 }
4706
4707 /* A cleanup for freeing the delayed method list. */
4708
4709 static void
4710 free_delayed_list (void *ptr)
4711 {
4712 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4713 if (cu->method_list != NULL)
4714 {
4715 VEC_free (delayed_method_info, cu->method_list);
4716 cu->method_list = NULL;
4717 }
4718 }
4719
4720 /* Compute the physnames of any methods on the CU's method list.
4721
4722 The computation of method physnames is delayed in order to avoid the
4723 (bad) condition that one of the method's formal parameters is of an as yet
4724 incomplete type. */
4725
4726 static void
4727 compute_delayed_physnames (struct dwarf2_cu *cu)
4728 {
4729 int i;
4730 struct delayed_method_info *mi;
4731 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4732 {
4733 const char *physname;
4734 struct fn_fieldlist *fn_flp
4735 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4736 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4737 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4738 }
4739 }
4740
4741 /* Generate full symbol information for PER_CU, whose DIEs have
4742 already been loaded into memory. */
4743
4744 static void
4745 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4746 {
4747 struct dwarf2_cu *cu = per_cu->cu;
4748 struct objfile *objfile = per_cu->objfile;
4749 CORE_ADDR lowpc, highpc;
4750 struct symtab *symtab;
4751 struct cleanup *back_to, *delayed_list_cleanup;
4752 CORE_ADDR baseaddr;
4753
4754 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4755
4756 buildsym_init ();
4757 back_to = make_cleanup (really_free_pendings, NULL);
4758 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4759
4760 cu->list_in_scope = &file_symbols;
4761
4762 /* Do line number decoding in read_file_scope () */
4763 process_die (cu->dies, cu);
4764
4765 /* Now that we have processed all the DIEs in the CU, all the types
4766 should be complete, and it should now be safe to compute all of the
4767 physnames. */
4768 compute_delayed_physnames (cu);
4769 do_cleanups (delayed_list_cleanup);
4770
4771 /* Some compilers don't define a DW_AT_high_pc attribute for the
4772 compilation unit. If the DW_AT_high_pc is missing, synthesize
4773 it, by scanning the DIE's below the compilation unit. */
4774 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4775
4776 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4777
4778 if (symtab != NULL)
4779 {
4780 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4781
4782 /* Set symtab language to language from DW_AT_language. If the
4783 compilation is from a C file generated by language preprocessors, do
4784 not set the language if it was already deduced by start_subfile. */
4785 if (!(cu->language == language_c && symtab->language != language_c))
4786 symtab->language = cu->language;
4787
4788 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4789 produce DW_AT_location with location lists but it can be possibly
4790 invalid without -fvar-tracking.
4791
4792 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4793 needed, it would be wrong due to missing DW_AT_producer there.
4794
4795 Still one can confuse GDB by using non-standard GCC compilation
4796 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4797 */
4798 if (cu->has_loclist && gcc_4_minor >= 0)
4799 symtab->locations_valid = 1;
4800
4801 if (gcc_4_minor >= 5)
4802 symtab->epilogue_unwind_valid = 1;
4803
4804 symtab->call_site_htab = cu->call_site_htab;
4805 }
4806
4807 if (dwarf2_per_objfile->using_index)
4808 per_cu->v.quick->symtab = symtab;
4809 else
4810 {
4811 struct partial_symtab *pst = per_cu->v.psymtab;
4812 pst->symtab = symtab;
4813 pst->readin = 1;
4814 }
4815
4816 do_cleanups (back_to);
4817 }
4818
4819 /* Process a die and its children. */
4820
4821 static void
4822 process_die (struct die_info *die, struct dwarf2_cu *cu)
4823 {
4824 switch (die->tag)
4825 {
4826 case DW_TAG_padding:
4827 break;
4828 case DW_TAG_compile_unit:
4829 read_file_scope (die, cu);
4830 break;
4831 case DW_TAG_type_unit:
4832 read_type_unit_scope (die, cu);
4833 break;
4834 case DW_TAG_subprogram:
4835 case DW_TAG_inlined_subroutine:
4836 read_func_scope (die, cu);
4837 break;
4838 case DW_TAG_lexical_block:
4839 case DW_TAG_try_block:
4840 case DW_TAG_catch_block:
4841 read_lexical_block_scope (die, cu);
4842 break;
4843 case DW_TAG_GNU_call_site:
4844 read_call_site_scope (die, cu);
4845 break;
4846 case DW_TAG_class_type:
4847 case DW_TAG_interface_type:
4848 case DW_TAG_structure_type:
4849 case DW_TAG_union_type:
4850 process_structure_scope (die, cu);
4851 break;
4852 case DW_TAG_enumeration_type:
4853 process_enumeration_scope (die, cu);
4854 break;
4855
4856 /* These dies have a type, but processing them does not create
4857 a symbol or recurse to process the children. Therefore we can
4858 read them on-demand through read_type_die. */
4859 case DW_TAG_subroutine_type:
4860 case DW_TAG_set_type:
4861 case DW_TAG_array_type:
4862 case DW_TAG_pointer_type:
4863 case DW_TAG_ptr_to_member_type:
4864 case DW_TAG_reference_type:
4865 case DW_TAG_string_type:
4866 break;
4867
4868 case DW_TAG_base_type:
4869 case DW_TAG_subrange_type:
4870 case DW_TAG_typedef:
4871 /* Add a typedef symbol for the type definition, if it has a
4872 DW_AT_name. */
4873 new_symbol (die, read_type_die (die, cu), cu);
4874 break;
4875 case DW_TAG_common_block:
4876 read_common_block (die, cu);
4877 break;
4878 case DW_TAG_common_inclusion:
4879 break;
4880 case DW_TAG_namespace:
4881 processing_has_namespace_info = 1;
4882 read_namespace (die, cu);
4883 break;
4884 case DW_TAG_module:
4885 processing_has_namespace_info = 1;
4886 read_module (die, cu);
4887 break;
4888 case DW_TAG_imported_declaration:
4889 case DW_TAG_imported_module:
4890 processing_has_namespace_info = 1;
4891 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4892 || cu->language != language_fortran))
4893 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4894 dwarf_tag_name (die->tag));
4895 read_import_statement (die, cu);
4896 break;
4897 default:
4898 new_symbol (die, NULL, cu);
4899 break;
4900 }
4901 }
4902
4903 /* A helper function for dwarf2_compute_name which determines whether DIE
4904 needs to have the name of the scope prepended to the name listed in the
4905 die. */
4906
4907 static int
4908 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4909 {
4910 struct attribute *attr;
4911
4912 switch (die->tag)
4913 {
4914 case DW_TAG_namespace:
4915 case DW_TAG_typedef:
4916 case DW_TAG_class_type:
4917 case DW_TAG_interface_type:
4918 case DW_TAG_structure_type:
4919 case DW_TAG_union_type:
4920 case DW_TAG_enumeration_type:
4921 case DW_TAG_enumerator:
4922 case DW_TAG_subprogram:
4923 case DW_TAG_member:
4924 return 1;
4925
4926 case DW_TAG_variable:
4927 case DW_TAG_constant:
4928 /* We only need to prefix "globally" visible variables. These include
4929 any variable marked with DW_AT_external or any variable that
4930 lives in a namespace. [Variables in anonymous namespaces
4931 require prefixing, but they are not DW_AT_external.] */
4932
4933 if (dwarf2_attr (die, DW_AT_specification, cu))
4934 {
4935 struct dwarf2_cu *spec_cu = cu;
4936
4937 return die_needs_namespace (die_specification (die, &spec_cu),
4938 spec_cu);
4939 }
4940
4941 attr = dwarf2_attr (die, DW_AT_external, cu);
4942 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4943 && die->parent->tag != DW_TAG_module)
4944 return 0;
4945 /* A variable in a lexical block of some kind does not need a
4946 namespace, even though in C++ such variables may be external
4947 and have a mangled name. */
4948 if (die->parent->tag == DW_TAG_lexical_block
4949 || die->parent->tag == DW_TAG_try_block
4950 || die->parent->tag == DW_TAG_catch_block
4951 || die->parent->tag == DW_TAG_subprogram)
4952 return 0;
4953 return 1;
4954
4955 default:
4956 return 0;
4957 }
4958 }
4959
4960 /* Retrieve the last character from a mem_file. */
4961
4962 static void
4963 do_ui_file_peek_last (void *object, const char *buffer, long length)
4964 {
4965 char *last_char_p = (char *) object;
4966
4967 if (length > 0)
4968 *last_char_p = buffer[length - 1];
4969 }
4970
4971 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4972 compute the physname for the object, which include a method's
4973 formal parameters (C++/Java) and return type (Java).
4974
4975 For Ada, return the DIE's linkage name rather than the fully qualified
4976 name. PHYSNAME is ignored..
4977
4978 The result is allocated on the objfile_obstack and canonicalized. */
4979
4980 static const char *
4981 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4982 int physname)
4983 {
4984 struct objfile *objfile = cu->objfile;
4985
4986 if (name == NULL)
4987 name = dwarf2_name (die, cu);
4988
4989 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4990 compute it by typename_concat inside GDB. */
4991 if (cu->language == language_ada
4992 || (cu->language == language_fortran && physname))
4993 {
4994 /* For Ada unit, we prefer the linkage name over the name, as
4995 the former contains the exported name, which the user expects
4996 to be able to reference. Ideally, we want the user to be able
4997 to reference this entity using either natural or linkage name,
4998 but we haven't started looking at this enhancement yet. */
4999 struct attribute *attr;
5000
5001 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5002 if (attr == NULL)
5003 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5004 if (attr && DW_STRING (attr))
5005 return DW_STRING (attr);
5006 }
5007
5008 /* These are the only languages we know how to qualify names in. */
5009 if (name != NULL
5010 && (cu->language == language_cplus || cu->language == language_java
5011 || cu->language == language_fortran))
5012 {
5013 if (die_needs_namespace (die, cu))
5014 {
5015 long length;
5016 char *prefix;
5017 struct ui_file *buf;
5018
5019 prefix = determine_prefix (die, cu);
5020 buf = mem_fileopen ();
5021 if (*prefix != '\0')
5022 {
5023 char *prefixed_name = typename_concat (NULL, prefix, name,
5024 physname, cu);
5025
5026 fputs_unfiltered (prefixed_name, buf);
5027 xfree (prefixed_name);
5028 }
5029 else
5030 fputs_unfiltered (name, buf);
5031
5032 /* Template parameters may be specified in the DIE's DW_AT_name, or
5033 as children with DW_TAG_template_type_param or
5034 DW_TAG_value_type_param. If the latter, add them to the name
5035 here. If the name already has template parameters, then
5036 skip this step; some versions of GCC emit both, and
5037 it is more efficient to use the pre-computed name.
5038
5039 Something to keep in mind about this process: it is very
5040 unlikely, or in some cases downright impossible, to produce
5041 something that will match the mangled name of a function.
5042 If the definition of the function has the same debug info,
5043 we should be able to match up with it anyway. But fallbacks
5044 using the minimal symbol, for instance to find a method
5045 implemented in a stripped copy of libstdc++, will not work.
5046 If we do not have debug info for the definition, we will have to
5047 match them up some other way.
5048
5049 When we do name matching there is a related problem with function
5050 templates; two instantiated function templates are allowed to
5051 differ only by their return types, which we do not add here. */
5052
5053 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5054 {
5055 struct attribute *attr;
5056 struct die_info *child;
5057 int first = 1;
5058
5059 die->building_fullname = 1;
5060
5061 for (child = die->child; child != NULL; child = child->sibling)
5062 {
5063 struct type *type;
5064 long value;
5065 gdb_byte *bytes;
5066 struct dwarf2_locexpr_baton *baton;
5067 struct value *v;
5068
5069 if (child->tag != DW_TAG_template_type_param
5070 && child->tag != DW_TAG_template_value_param)
5071 continue;
5072
5073 if (first)
5074 {
5075 fputs_unfiltered ("<", buf);
5076 first = 0;
5077 }
5078 else
5079 fputs_unfiltered (", ", buf);
5080
5081 attr = dwarf2_attr (child, DW_AT_type, cu);
5082 if (attr == NULL)
5083 {
5084 complaint (&symfile_complaints,
5085 _("template parameter missing DW_AT_type"));
5086 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5087 continue;
5088 }
5089 type = die_type (child, cu);
5090
5091 if (child->tag == DW_TAG_template_type_param)
5092 {
5093 c_print_type (type, "", buf, -1, 0);
5094 continue;
5095 }
5096
5097 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5098 if (attr == NULL)
5099 {
5100 complaint (&symfile_complaints,
5101 _("template parameter missing "
5102 "DW_AT_const_value"));
5103 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5104 continue;
5105 }
5106
5107 dwarf2_const_value_attr (attr, type, name,
5108 &cu->comp_unit_obstack, cu,
5109 &value, &bytes, &baton);
5110
5111 if (TYPE_NOSIGN (type))
5112 /* GDB prints characters as NUMBER 'CHAR'. If that's
5113 changed, this can use value_print instead. */
5114 c_printchar (value, type, buf);
5115 else
5116 {
5117 struct value_print_options opts;
5118
5119 if (baton != NULL)
5120 v = dwarf2_evaluate_loc_desc (type, NULL,
5121 baton->data,
5122 baton->size,
5123 baton->per_cu);
5124 else if (bytes != NULL)
5125 {
5126 v = allocate_value (type);
5127 memcpy (value_contents_writeable (v), bytes,
5128 TYPE_LENGTH (type));
5129 }
5130 else
5131 v = value_from_longest (type, value);
5132
5133 /* Specify decimal so that we do not depend on
5134 the radix. */
5135 get_formatted_print_options (&opts, 'd');
5136 opts.raw = 1;
5137 value_print (v, buf, &opts);
5138 release_value (v);
5139 value_free (v);
5140 }
5141 }
5142
5143 die->building_fullname = 0;
5144
5145 if (!first)
5146 {
5147 /* Close the argument list, with a space if necessary
5148 (nested templates). */
5149 char last_char = '\0';
5150 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5151 if (last_char == '>')
5152 fputs_unfiltered (" >", buf);
5153 else
5154 fputs_unfiltered (">", buf);
5155 }
5156 }
5157
5158 /* For Java and C++ methods, append formal parameter type
5159 information, if PHYSNAME. */
5160
5161 if (physname && die->tag == DW_TAG_subprogram
5162 && (cu->language == language_cplus
5163 || cu->language == language_java))
5164 {
5165 struct type *type = read_type_die (die, cu);
5166
5167 c_type_print_args (type, buf, 1, cu->language);
5168
5169 if (cu->language == language_java)
5170 {
5171 /* For java, we must append the return type to method
5172 names. */
5173 if (die->tag == DW_TAG_subprogram)
5174 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5175 0, 0);
5176 }
5177 else if (cu->language == language_cplus)
5178 {
5179 /* Assume that an artificial first parameter is
5180 "this", but do not crash if it is not. RealView
5181 marks unnamed (and thus unused) parameters as
5182 artificial; there is no way to differentiate
5183 the two cases. */
5184 if (TYPE_NFIELDS (type) > 0
5185 && TYPE_FIELD_ARTIFICIAL (type, 0)
5186 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5187 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5188 0))))
5189 fputs_unfiltered (" const", buf);
5190 }
5191 }
5192
5193 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5194 &length);
5195 ui_file_delete (buf);
5196
5197 if (cu->language == language_cplus)
5198 {
5199 char *cname
5200 = dwarf2_canonicalize_name (name, cu,
5201 &objfile->objfile_obstack);
5202
5203 if (cname != NULL)
5204 name = cname;
5205 }
5206 }
5207 }
5208
5209 return name;
5210 }
5211
5212 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5213 If scope qualifiers are appropriate they will be added. The result
5214 will be allocated on the objfile_obstack, or NULL if the DIE does
5215 not have a name. NAME may either be from a previous call to
5216 dwarf2_name or NULL.
5217
5218 The output string will be canonicalized (if C++/Java). */
5219
5220 static const char *
5221 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5222 {
5223 return dwarf2_compute_name (name, die, cu, 0);
5224 }
5225
5226 /* Construct a physname for the given DIE in CU. NAME may either be
5227 from a previous call to dwarf2_name or NULL. The result will be
5228 allocated on the objfile_objstack or NULL if the DIE does not have a
5229 name.
5230
5231 The output string will be canonicalized (if C++/Java). */
5232
5233 static const char *
5234 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5235 {
5236 struct objfile *objfile = cu->objfile;
5237 struct attribute *attr;
5238 const char *retval, *mangled = NULL, *canon = NULL;
5239 struct cleanup *back_to;
5240 int need_copy = 1;
5241
5242 /* In this case dwarf2_compute_name is just a shortcut not building anything
5243 on its own. */
5244 if (!die_needs_namespace (die, cu))
5245 return dwarf2_compute_name (name, die, cu, 1);
5246
5247 back_to = make_cleanup (null_cleanup, NULL);
5248
5249 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5250 if (!attr)
5251 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5252
5253 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5254 has computed. */
5255 if (attr && DW_STRING (attr))
5256 {
5257 char *demangled;
5258
5259 mangled = DW_STRING (attr);
5260
5261 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5262 type. It is easier for GDB users to search for such functions as
5263 `name(params)' than `long name(params)'. In such case the minimal
5264 symbol names do not match the full symbol names but for template
5265 functions there is never a need to look up their definition from their
5266 declaration so the only disadvantage remains the minimal symbol
5267 variant `long name(params)' does not have the proper inferior type.
5268 */
5269
5270 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5271 | (cu->language == language_java
5272 ? DMGL_JAVA | DMGL_RET_POSTFIX
5273 : DMGL_RET_DROP)));
5274 if (demangled)
5275 {
5276 make_cleanup (xfree, demangled);
5277 canon = demangled;
5278 }
5279 else
5280 {
5281 canon = mangled;
5282 need_copy = 0;
5283 }
5284 }
5285
5286 if (canon == NULL || check_physname)
5287 {
5288 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5289
5290 if (canon != NULL && strcmp (physname, canon) != 0)
5291 {
5292 /* It may not mean a bug in GDB. The compiler could also
5293 compute DW_AT_linkage_name incorrectly. But in such case
5294 GDB would need to be bug-to-bug compatible. */
5295
5296 complaint (&symfile_complaints,
5297 _("Computed physname <%s> does not match demangled <%s> "
5298 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5299 physname, canon, mangled, die->offset, objfile->name);
5300
5301 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5302 is available here - over computed PHYSNAME. It is safer
5303 against both buggy GDB and buggy compilers. */
5304
5305 retval = canon;
5306 }
5307 else
5308 {
5309 retval = physname;
5310 need_copy = 0;
5311 }
5312 }
5313 else
5314 retval = canon;
5315
5316 if (need_copy)
5317 retval = obsavestring (retval, strlen (retval),
5318 &objfile->objfile_obstack);
5319
5320 do_cleanups (back_to);
5321 return retval;
5322 }
5323
5324 /* Read the import statement specified by the given die and record it. */
5325
5326 static void
5327 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5328 {
5329 struct objfile *objfile = cu->objfile;
5330 struct attribute *import_attr;
5331 struct die_info *imported_die, *child_die;
5332 struct dwarf2_cu *imported_cu;
5333 const char *imported_name;
5334 const char *imported_name_prefix;
5335 const char *canonical_name;
5336 const char *import_alias;
5337 const char *imported_declaration = NULL;
5338 const char *import_prefix;
5339 VEC (const_char_ptr) *excludes = NULL;
5340 struct cleanup *cleanups;
5341
5342 char *temp;
5343
5344 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5345 if (import_attr == NULL)
5346 {
5347 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5348 dwarf_tag_name (die->tag));
5349 return;
5350 }
5351
5352 imported_cu = cu;
5353 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5354 imported_name = dwarf2_name (imported_die, imported_cu);
5355 if (imported_name == NULL)
5356 {
5357 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5358
5359 The import in the following code:
5360 namespace A
5361 {
5362 typedef int B;
5363 }
5364
5365 int main ()
5366 {
5367 using A::B;
5368 B b;
5369 return b;
5370 }
5371
5372 ...
5373 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5374 <52> DW_AT_decl_file : 1
5375 <53> DW_AT_decl_line : 6
5376 <54> DW_AT_import : <0x75>
5377 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5378 <59> DW_AT_name : B
5379 <5b> DW_AT_decl_file : 1
5380 <5c> DW_AT_decl_line : 2
5381 <5d> DW_AT_type : <0x6e>
5382 ...
5383 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5384 <76> DW_AT_byte_size : 4
5385 <77> DW_AT_encoding : 5 (signed)
5386
5387 imports the wrong die ( 0x75 instead of 0x58 ).
5388 This case will be ignored until the gcc bug is fixed. */
5389 return;
5390 }
5391
5392 /* Figure out the local name after import. */
5393 import_alias = dwarf2_name (die, cu);
5394
5395 /* Figure out where the statement is being imported to. */
5396 import_prefix = determine_prefix (die, cu);
5397
5398 /* Figure out what the scope of the imported die is and prepend it
5399 to the name of the imported die. */
5400 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5401
5402 if (imported_die->tag != DW_TAG_namespace
5403 && imported_die->tag != DW_TAG_module)
5404 {
5405 imported_declaration = imported_name;
5406 canonical_name = imported_name_prefix;
5407 }
5408 else if (strlen (imported_name_prefix) > 0)
5409 {
5410 temp = alloca (strlen (imported_name_prefix)
5411 + 2 + strlen (imported_name) + 1);
5412 strcpy (temp, imported_name_prefix);
5413 strcat (temp, "::");
5414 strcat (temp, imported_name);
5415 canonical_name = temp;
5416 }
5417 else
5418 canonical_name = imported_name;
5419
5420 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5421
5422 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5423 for (child_die = die->child; child_die && child_die->tag;
5424 child_die = sibling_die (child_die))
5425 {
5426 /* DWARF-4: A Fortran use statement with a “rename list” may be
5427 represented by an imported module entry with an import attribute
5428 referring to the module and owned entries corresponding to those
5429 entities that are renamed as part of being imported. */
5430
5431 if (child_die->tag != DW_TAG_imported_declaration)
5432 {
5433 complaint (&symfile_complaints,
5434 _("child DW_TAG_imported_declaration expected "
5435 "- DIE at 0x%x [in module %s]"),
5436 child_die->offset, objfile->name);
5437 continue;
5438 }
5439
5440 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5441 if (import_attr == NULL)
5442 {
5443 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5444 dwarf_tag_name (child_die->tag));
5445 continue;
5446 }
5447
5448 imported_cu = cu;
5449 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5450 &imported_cu);
5451 imported_name = dwarf2_name (imported_die, imported_cu);
5452 if (imported_name == NULL)
5453 {
5454 complaint (&symfile_complaints,
5455 _("child DW_TAG_imported_declaration has unknown "
5456 "imported name - DIE at 0x%x [in module %s]"),
5457 child_die->offset, objfile->name);
5458 continue;
5459 }
5460
5461 VEC_safe_push (const_char_ptr, excludes, imported_name);
5462
5463 process_die (child_die, cu);
5464 }
5465
5466 cp_add_using_directive (import_prefix,
5467 canonical_name,
5468 import_alias,
5469 imported_declaration,
5470 excludes,
5471 &objfile->objfile_obstack);
5472
5473 do_cleanups (cleanups);
5474 }
5475
5476 /* Cleanup function for read_file_scope. */
5477
5478 static void
5479 free_cu_line_header (void *arg)
5480 {
5481 struct dwarf2_cu *cu = arg;
5482
5483 free_line_header (cu->line_header);
5484 cu->line_header = NULL;
5485 }
5486
5487 static void
5488 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5489 char **name, char **comp_dir)
5490 {
5491 struct attribute *attr;
5492
5493 *name = NULL;
5494 *comp_dir = NULL;
5495
5496 /* Find the filename. Do not use dwarf2_name here, since the filename
5497 is not a source language identifier. */
5498 attr = dwarf2_attr (die, DW_AT_name, cu);
5499 if (attr)
5500 {
5501 *name = DW_STRING (attr);
5502 }
5503
5504 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5505 if (attr)
5506 *comp_dir = DW_STRING (attr);
5507 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5508 {
5509 *comp_dir = ldirname (*name);
5510 if (*comp_dir != NULL)
5511 make_cleanup (xfree, *comp_dir);
5512 }
5513 if (*comp_dir != NULL)
5514 {
5515 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5516 directory, get rid of it. */
5517 char *cp = strchr (*comp_dir, ':');
5518
5519 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5520 *comp_dir = cp + 1;
5521 }
5522
5523 if (*name == NULL)
5524 *name = "<unknown>";
5525 }
5526
5527 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
5528 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5529 COMP_DIR is the compilation directory.
5530 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
5531
5532 static void
5533 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5534 const char *comp_dir, int want_line_info)
5535 {
5536 struct attribute *attr;
5537 struct objfile *objfile = cu->objfile;
5538 bfd *abfd = objfile->obfd;
5539
5540 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5541 if (attr)
5542 {
5543 unsigned int line_offset = DW_UNSND (attr);
5544 struct line_header *line_header
5545 = dwarf_decode_line_header (line_offset, abfd, cu);
5546
5547 if (line_header)
5548 {
5549 cu->line_header = line_header;
5550 make_cleanup (free_cu_line_header, cu);
5551 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
5552 }
5553 }
5554 }
5555
5556 /* Process DW_TAG_compile_unit. */
5557
5558 static void
5559 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5560 {
5561 struct objfile *objfile = cu->objfile;
5562 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5563 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5564 CORE_ADDR highpc = ((CORE_ADDR) 0);
5565 struct attribute *attr;
5566 char *name = NULL;
5567 char *comp_dir = NULL;
5568 struct die_info *child_die;
5569 bfd *abfd = objfile->obfd;
5570 CORE_ADDR baseaddr;
5571
5572 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5573
5574 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5575
5576 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5577 from finish_block. */
5578 if (lowpc == ((CORE_ADDR) -1))
5579 lowpc = highpc;
5580 lowpc += baseaddr;
5581 highpc += baseaddr;
5582
5583 find_file_and_directory (die, cu, &name, &comp_dir);
5584
5585 attr = dwarf2_attr (die, DW_AT_language, cu);
5586 if (attr)
5587 {
5588 set_cu_language (DW_UNSND (attr), cu);
5589 }
5590
5591 attr = dwarf2_attr (die, DW_AT_producer, cu);
5592 if (attr)
5593 cu->producer = DW_STRING (attr);
5594
5595 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5596 standardised yet. As a workaround for the language detection we fall
5597 back to the DW_AT_producer string. */
5598 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5599 cu->language = language_opencl;
5600
5601 /* We assume that we're processing GCC output. */
5602 processing_gcc_compilation = 2;
5603
5604 processing_has_namespace_info = 0;
5605
5606 start_symtab (name, comp_dir, lowpc);
5607 record_debugformat ("DWARF 2");
5608 record_producer (cu->producer);
5609
5610 /* Decode line number information if present. We do this before
5611 processing child DIEs, so that the line header table is available
5612 for DW_AT_decl_file. */
5613 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
5614
5615 /* Process all dies in compilation unit. */
5616 if (die->child != NULL)
5617 {
5618 child_die = die->child;
5619 while (child_die && child_die->tag)
5620 {
5621 process_die (child_die, cu);
5622 child_die = sibling_die (child_die);
5623 }
5624 }
5625
5626 /* Decode macro information, if present. Dwarf 2 macro information
5627 refers to information in the line number info statement program
5628 header, so we can only read it if we've read the header
5629 successfully. */
5630 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5631 if (attr && cu->line_header)
5632 {
5633 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5634 complaint (&symfile_complaints,
5635 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5636
5637 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5638 comp_dir, abfd, cu,
5639 &dwarf2_per_objfile->macro, 1);
5640 }
5641 else
5642 {
5643 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5644 if (attr && cu->line_header)
5645 {
5646 unsigned int macro_offset = DW_UNSND (attr);
5647
5648 dwarf_decode_macros (cu->line_header, macro_offset,
5649 comp_dir, abfd, cu,
5650 &dwarf2_per_objfile->macinfo, 0);
5651 }
5652 }
5653
5654 do_cleanups (back_to);
5655 }
5656
5657 /* Process DW_TAG_type_unit.
5658 For TUs we want to skip the first top level sibling if it's not the
5659 actual type being defined by this TU. In this case the first top
5660 level sibling is there to provide context only. */
5661
5662 static void
5663 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5664 {
5665 struct objfile *objfile = cu->objfile;
5666 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5667 CORE_ADDR lowpc;
5668 struct attribute *attr;
5669 char *name = NULL;
5670 char *comp_dir = NULL;
5671 struct die_info *child_die;
5672 bfd *abfd = objfile->obfd;
5673
5674 /* start_symtab needs a low pc, but we don't really have one.
5675 Do what read_file_scope would do in the absence of such info. */
5676 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5677
5678 /* Find the filename. Do not use dwarf2_name here, since the filename
5679 is not a source language identifier. */
5680 attr = dwarf2_attr (die, DW_AT_name, cu);
5681 if (attr)
5682 name = DW_STRING (attr);
5683
5684 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5685 if (attr)
5686 comp_dir = DW_STRING (attr);
5687 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5688 {
5689 comp_dir = ldirname (name);
5690 if (comp_dir != NULL)
5691 make_cleanup (xfree, comp_dir);
5692 }
5693
5694 if (name == NULL)
5695 name = "<unknown>";
5696
5697 attr = dwarf2_attr (die, DW_AT_language, cu);
5698 if (attr)
5699 set_cu_language (DW_UNSND (attr), cu);
5700
5701 /* This isn't technically needed today. It is done for symmetry
5702 with read_file_scope. */
5703 attr = dwarf2_attr (die, DW_AT_producer, cu);
5704 if (attr)
5705 cu->producer = DW_STRING (attr);
5706
5707 /* We assume that we're processing GCC output. */
5708 processing_gcc_compilation = 2;
5709
5710 processing_has_namespace_info = 0;
5711
5712 start_symtab (name, comp_dir, lowpc);
5713 record_debugformat ("DWARF 2");
5714 record_producer (cu->producer);
5715
5716 /* Decode line number information if present. We do this before
5717 processing child DIEs, so that the line header table is available
5718 for DW_AT_decl_file.
5719 We don't need the pc/line-number mapping for type units. */
5720 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
5721
5722 /* Process the dies in the type unit. */
5723 if (die->child == NULL)
5724 {
5725 dump_die_for_error (die);
5726 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5727 bfd_get_filename (abfd));
5728 }
5729
5730 child_die = die->child;
5731
5732 while (child_die && child_die->tag)
5733 {
5734 process_die (child_die, cu);
5735
5736 child_die = sibling_die (child_die);
5737 }
5738
5739 do_cleanups (back_to);
5740 }
5741
5742 /* qsort helper for inherit_abstract_dies. */
5743
5744 static int
5745 unsigned_int_compar (const void *ap, const void *bp)
5746 {
5747 unsigned int a = *(unsigned int *) ap;
5748 unsigned int b = *(unsigned int *) bp;
5749
5750 return (a > b) - (b > a);
5751 }
5752
5753 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5754 Inherit only the children of the DW_AT_abstract_origin DIE not being
5755 already referenced by DW_AT_abstract_origin from the children of the
5756 current DIE. */
5757
5758 static void
5759 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5760 {
5761 struct die_info *child_die;
5762 unsigned die_children_count;
5763 /* CU offsets which were referenced by children of the current DIE. */
5764 unsigned *offsets;
5765 unsigned *offsets_end, *offsetp;
5766 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5767 struct die_info *origin_die;
5768 /* Iterator of the ORIGIN_DIE children. */
5769 struct die_info *origin_child_die;
5770 struct cleanup *cleanups;
5771 struct attribute *attr;
5772 struct dwarf2_cu *origin_cu;
5773 struct pending **origin_previous_list_in_scope;
5774
5775 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5776 if (!attr)
5777 return;
5778
5779 /* Note that following die references may follow to a die in a
5780 different cu. */
5781
5782 origin_cu = cu;
5783 origin_die = follow_die_ref (die, attr, &origin_cu);
5784
5785 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5786 symbols in. */
5787 origin_previous_list_in_scope = origin_cu->list_in_scope;
5788 origin_cu->list_in_scope = cu->list_in_scope;
5789
5790 if (die->tag != origin_die->tag
5791 && !(die->tag == DW_TAG_inlined_subroutine
5792 && origin_die->tag == DW_TAG_subprogram))
5793 complaint (&symfile_complaints,
5794 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5795 die->offset, origin_die->offset);
5796
5797 child_die = die->child;
5798 die_children_count = 0;
5799 while (child_die && child_die->tag)
5800 {
5801 child_die = sibling_die (child_die);
5802 die_children_count++;
5803 }
5804 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5805 cleanups = make_cleanup (xfree, offsets);
5806
5807 offsets_end = offsets;
5808 child_die = die->child;
5809 while (child_die && child_die->tag)
5810 {
5811 /* For each CHILD_DIE, find the corresponding child of
5812 ORIGIN_DIE. If there is more than one layer of
5813 DW_AT_abstract_origin, follow them all; there shouldn't be,
5814 but GCC versions at least through 4.4 generate this (GCC PR
5815 40573). */
5816 struct die_info *child_origin_die = child_die;
5817 struct dwarf2_cu *child_origin_cu = cu;
5818
5819 while (1)
5820 {
5821 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5822 child_origin_cu);
5823 if (attr == NULL)
5824 break;
5825 child_origin_die = follow_die_ref (child_origin_die, attr,
5826 &child_origin_cu);
5827 }
5828
5829 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5830 counterpart may exist. */
5831 if (child_origin_die != child_die)
5832 {
5833 if (child_die->tag != child_origin_die->tag
5834 && !(child_die->tag == DW_TAG_inlined_subroutine
5835 && child_origin_die->tag == DW_TAG_subprogram))
5836 complaint (&symfile_complaints,
5837 _("Child DIE 0x%x and its abstract origin 0x%x have "
5838 "different tags"), child_die->offset,
5839 child_origin_die->offset);
5840 if (child_origin_die->parent != origin_die)
5841 complaint (&symfile_complaints,
5842 _("Child DIE 0x%x and its abstract origin 0x%x have "
5843 "different parents"), child_die->offset,
5844 child_origin_die->offset);
5845 else
5846 *offsets_end++ = child_origin_die->offset;
5847 }
5848 child_die = sibling_die (child_die);
5849 }
5850 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5851 unsigned_int_compar);
5852 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5853 if (offsetp[-1] == *offsetp)
5854 complaint (&symfile_complaints,
5855 _("Multiple children of DIE 0x%x refer "
5856 "to DIE 0x%x as their abstract origin"),
5857 die->offset, *offsetp);
5858
5859 offsetp = offsets;
5860 origin_child_die = origin_die->child;
5861 while (origin_child_die && origin_child_die->tag)
5862 {
5863 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5864 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5865 offsetp++;
5866 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5867 {
5868 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5869 process_die (origin_child_die, origin_cu);
5870 }
5871 origin_child_die = sibling_die (origin_child_die);
5872 }
5873 origin_cu->list_in_scope = origin_previous_list_in_scope;
5874
5875 do_cleanups (cleanups);
5876 }
5877
5878 static void
5879 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5880 {
5881 struct objfile *objfile = cu->objfile;
5882 struct context_stack *new;
5883 CORE_ADDR lowpc;
5884 CORE_ADDR highpc;
5885 struct die_info *child_die;
5886 struct attribute *attr, *call_line, *call_file;
5887 char *name;
5888 CORE_ADDR baseaddr;
5889 struct block *block;
5890 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5891 VEC (symbolp) *template_args = NULL;
5892 struct template_symbol *templ_func = NULL;
5893
5894 if (inlined_func)
5895 {
5896 /* If we do not have call site information, we can't show the
5897 caller of this inlined function. That's too confusing, so
5898 only use the scope for local variables. */
5899 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5900 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5901 if (call_line == NULL || call_file == NULL)
5902 {
5903 read_lexical_block_scope (die, cu);
5904 return;
5905 }
5906 }
5907
5908 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5909
5910 name = dwarf2_name (die, cu);
5911
5912 /* Ignore functions with missing or empty names. These are actually
5913 illegal according to the DWARF standard. */
5914 if (name == NULL)
5915 {
5916 complaint (&symfile_complaints,
5917 _("missing name for subprogram DIE at %d"), die->offset);
5918 return;
5919 }
5920
5921 /* Ignore functions with missing or invalid low and high pc attributes. */
5922 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5923 {
5924 attr = dwarf2_attr (die, DW_AT_external, cu);
5925 if (!attr || !DW_UNSND (attr))
5926 complaint (&symfile_complaints,
5927 _("cannot get low and high bounds "
5928 "for subprogram DIE at %d"),
5929 die->offset);
5930 return;
5931 }
5932
5933 lowpc += baseaddr;
5934 highpc += baseaddr;
5935
5936 /* If we have any template arguments, then we must allocate a
5937 different sort of symbol. */
5938 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5939 {
5940 if (child_die->tag == DW_TAG_template_type_param
5941 || child_die->tag == DW_TAG_template_value_param)
5942 {
5943 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5944 struct template_symbol);
5945 templ_func->base.is_cplus_template_function = 1;
5946 break;
5947 }
5948 }
5949
5950 new = push_context (0, lowpc);
5951 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5952 (struct symbol *) templ_func);
5953
5954 /* If there is a location expression for DW_AT_frame_base, record
5955 it. */
5956 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5957 if (attr)
5958 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5959 expression is being recorded directly in the function's symbol
5960 and not in a separate frame-base object. I guess this hack is
5961 to avoid adding some sort of frame-base adjunct/annex to the
5962 function's symbol :-(. The problem with doing this is that it
5963 results in a function symbol with a location expression that
5964 has nothing to do with the location of the function, ouch! The
5965 relationship should be: a function's symbol has-a frame base; a
5966 frame-base has-a location expression. */
5967 dwarf2_symbol_mark_computed (attr, new->name, cu);
5968
5969 cu->list_in_scope = &local_symbols;
5970
5971 if (die->child != NULL)
5972 {
5973 child_die = die->child;
5974 while (child_die && child_die->tag)
5975 {
5976 if (child_die->tag == DW_TAG_template_type_param
5977 || child_die->tag == DW_TAG_template_value_param)
5978 {
5979 struct symbol *arg = new_symbol (child_die, NULL, cu);
5980
5981 if (arg != NULL)
5982 VEC_safe_push (symbolp, template_args, arg);
5983 }
5984 else
5985 process_die (child_die, cu);
5986 child_die = sibling_die (child_die);
5987 }
5988 }
5989
5990 inherit_abstract_dies (die, cu);
5991
5992 /* If we have a DW_AT_specification, we might need to import using
5993 directives from the context of the specification DIE. See the
5994 comment in determine_prefix. */
5995 if (cu->language == language_cplus
5996 && dwarf2_attr (die, DW_AT_specification, cu))
5997 {
5998 struct dwarf2_cu *spec_cu = cu;
5999 struct die_info *spec_die = die_specification (die, &spec_cu);
6000
6001 while (spec_die)
6002 {
6003 child_die = spec_die->child;
6004 while (child_die && child_die->tag)
6005 {
6006 if (child_die->tag == DW_TAG_imported_module)
6007 process_die (child_die, spec_cu);
6008 child_die = sibling_die (child_die);
6009 }
6010
6011 /* In some cases, GCC generates specification DIEs that
6012 themselves contain DW_AT_specification attributes. */
6013 spec_die = die_specification (spec_die, &spec_cu);
6014 }
6015 }
6016
6017 new = pop_context ();
6018 /* Make a block for the local symbols within. */
6019 block = finish_block (new->name, &local_symbols, new->old_blocks,
6020 lowpc, highpc, objfile);
6021
6022 /* For C++, set the block's scope. */
6023 if (cu->language == language_cplus || cu->language == language_fortran)
6024 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6025 determine_prefix (die, cu),
6026 processing_has_namespace_info);
6027
6028 /* If we have address ranges, record them. */
6029 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6030
6031 /* Attach template arguments to function. */
6032 if (! VEC_empty (symbolp, template_args))
6033 {
6034 gdb_assert (templ_func != NULL);
6035
6036 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6037 templ_func->template_arguments
6038 = obstack_alloc (&objfile->objfile_obstack,
6039 (templ_func->n_template_arguments
6040 * sizeof (struct symbol *)));
6041 memcpy (templ_func->template_arguments,
6042 VEC_address (symbolp, template_args),
6043 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6044 VEC_free (symbolp, template_args);
6045 }
6046
6047 /* In C++, we can have functions nested inside functions (e.g., when
6048 a function declares a class that has methods). This means that
6049 when we finish processing a function scope, we may need to go
6050 back to building a containing block's symbol lists. */
6051 local_symbols = new->locals;
6052 param_symbols = new->params;
6053 using_directives = new->using_directives;
6054
6055 /* If we've finished processing a top-level function, subsequent
6056 symbols go in the file symbol list. */
6057 if (outermost_context_p ())
6058 cu->list_in_scope = &file_symbols;
6059 }
6060
6061 /* Process all the DIES contained within a lexical block scope. Start
6062 a new scope, process the dies, and then close the scope. */
6063
6064 static void
6065 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6066 {
6067 struct objfile *objfile = cu->objfile;
6068 struct context_stack *new;
6069 CORE_ADDR lowpc, highpc;
6070 struct die_info *child_die;
6071 CORE_ADDR baseaddr;
6072
6073 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6074
6075 /* Ignore blocks with missing or invalid low and high pc attributes. */
6076 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6077 as multiple lexical blocks? Handling children in a sane way would
6078 be nasty. Might be easier to properly extend generic blocks to
6079 describe ranges. */
6080 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6081 return;
6082 lowpc += baseaddr;
6083 highpc += baseaddr;
6084
6085 push_context (0, lowpc);
6086 if (die->child != NULL)
6087 {
6088 child_die = die->child;
6089 while (child_die && child_die->tag)
6090 {
6091 process_die (child_die, cu);
6092 child_die = sibling_die (child_die);
6093 }
6094 }
6095 new = pop_context ();
6096
6097 if (local_symbols != NULL || using_directives != NULL)
6098 {
6099 struct block *block
6100 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6101 highpc, objfile);
6102
6103 /* Note that recording ranges after traversing children, as we
6104 do here, means that recording a parent's ranges entails
6105 walking across all its children's ranges as they appear in
6106 the address map, which is quadratic behavior.
6107
6108 It would be nicer to record the parent's ranges before
6109 traversing its children, simply overriding whatever you find
6110 there. But since we don't even decide whether to create a
6111 block until after we've traversed its children, that's hard
6112 to do. */
6113 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6114 }
6115 local_symbols = new->locals;
6116 using_directives = new->using_directives;
6117 }
6118
6119 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6120
6121 static void
6122 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6123 {
6124 struct objfile *objfile = cu->objfile;
6125 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6126 CORE_ADDR pc, baseaddr;
6127 struct attribute *attr;
6128 struct call_site *call_site, call_site_local;
6129 void **slot;
6130 int nparams;
6131 struct die_info *child_die;
6132
6133 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6134
6135 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6136 if (!attr)
6137 {
6138 complaint (&symfile_complaints,
6139 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6140 "DIE 0x%x [in module %s]"),
6141 die->offset, objfile->name);
6142 return;
6143 }
6144 pc = DW_ADDR (attr) + baseaddr;
6145
6146 if (cu->call_site_htab == NULL)
6147 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6148 NULL, &objfile->objfile_obstack,
6149 hashtab_obstack_allocate, NULL);
6150 call_site_local.pc = pc;
6151 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6152 if (*slot != NULL)
6153 {
6154 complaint (&symfile_complaints,
6155 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6156 "DIE 0x%x [in module %s]"),
6157 paddress (gdbarch, pc), die->offset, objfile->name);
6158 return;
6159 }
6160
6161 /* Count parameters at the caller. */
6162
6163 nparams = 0;
6164 for (child_die = die->child; child_die && child_die->tag;
6165 child_die = sibling_die (child_die))
6166 {
6167 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6168 {
6169 complaint (&symfile_complaints,
6170 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6171 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6172 child_die->tag, child_die->offset, objfile->name);
6173 continue;
6174 }
6175
6176 nparams++;
6177 }
6178
6179 call_site = obstack_alloc (&objfile->objfile_obstack,
6180 (sizeof (*call_site)
6181 + (sizeof (*call_site->parameter)
6182 * (nparams - 1))));
6183 *slot = call_site;
6184 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6185 call_site->pc = pc;
6186
6187 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6188 {
6189 struct die_info *func_die;
6190
6191 /* Skip also over DW_TAG_inlined_subroutine. */
6192 for (func_die = die->parent;
6193 func_die && func_die->tag != DW_TAG_subprogram
6194 && func_die->tag != DW_TAG_subroutine_type;
6195 func_die = func_die->parent);
6196
6197 /* DW_AT_GNU_all_call_sites is a superset
6198 of DW_AT_GNU_all_tail_call_sites. */
6199 if (func_die
6200 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6201 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6202 {
6203 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6204 not complete. But keep CALL_SITE for look ups via call_site_htab,
6205 both the initial caller containing the real return address PC and
6206 the final callee containing the current PC of a chain of tail
6207 calls do not need to have the tail call list complete. But any
6208 function candidate for a virtual tail call frame searched via
6209 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6210 determined unambiguously. */
6211 }
6212 else
6213 {
6214 struct type *func_type = NULL;
6215
6216 if (func_die)
6217 func_type = get_die_type (func_die, cu);
6218 if (func_type != NULL)
6219 {
6220 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6221
6222 /* Enlist this call site to the function. */
6223 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6224 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6225 }
6226 else
6227 complaint (&symfile_complaints,
6228 _("Cannot find function owning DW_TAG_GNU_call_site "
6229 "DIE 0x%x [in module %s]"),
6230 die->offset, objfile->name);
6231 }
6232 }
6233
6234 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6235 if (attr == NULL)
6236 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6237 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6238 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6239 /* Keep NULL DWARF_BLOCK. */;
6240 else if (attr_form_is_block (attr))
6241 {
6242 struct dwarf2_locexpr_baton *dlbaton;
6243
6244 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6245 dlbaton->data = DW_BLOCK (attr)->data;
6246 dlbaton->size = DW_BLOCK (attr)->size;
6247 dlbaton->per_cu = cu->per_cu;
6248
6249 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6250 }
6251 else if (is_ref_attr (attr))
6252 {
6253 struct dwarf2_cu *target_cu = cu;
6254 struct die_info *target_die;
6255
6256 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6257 gdb_assert (target_cu->objfile == objfile);
6258 if (die_is_declaration (target_die, target_cu))
6259 {
6260 const char *target_physname;
6261
6262 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6263 if (target_physname == NULL)
6264 complaint (&symfile_complaints,
6265 _("DW_AT_GNU_call_site_target target DIE has invalid "
6266 "physname, for referencing DIE 0x%x [in module %s]"),
6267 die->offset, objfile->name);
6268 else
6269 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6270 }
6271 else
6272 {
6273 CORE_ADDR lowpc;
6274
6275 /* DW_AT_entry_pc should be preferred. */
6276 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6277 complaint (&symfile_complaints,
6278 _("DW_AT_GNU_call_site_target target DIE has invalid "
6279 "low pc, for referencing DIE 0x%x [in module %s]"),
6280 die->offset, objfile->name);
6281 else
6282 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6283 }
6284 }
6285 else
6286 complaint (&symfile_complaints,
6287 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6288 "block nor reference, for DIE 0x%x [in module %s]"),
6289 die->offset, objfile->name);
6290
6291 call_site->per_cu = cu->per_cu;
6292
6293 for (child_die = die->child;
6294 child_die && child_die->tag;
6295 child_die = sibling_die (child_die))
6296 {
6297 struct dwarf2_locexpr_baton *dlbaton;
6298 struct call_site_parameter *parameter;
6299
6300 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6301 {
6302 /* Already printed the complaint above. */
6303 continue;
6304 }
6305
6306 gdb_assert (call_site->parameter_count < nparams);
6307 parameter = &call_site->parameter[call_site->parameter_count];
6308
6309 /* DW_AT_location specifies the register number. Value of the data
6310 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6311
6312 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6313 if (!attr || !attr_form_is_block (attr))
6314 {
6315 complaint (&symfile_complaints,
6316 _("No DW_FORM_block* DW_AT_location for "
6317 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6318 child_die->offset, objfile->name);
6319 continue;
6320 }
6321 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6322 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6323 if (parameter->dwarf_reg == -1
6324 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6325 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6326 &parameter->fb_offset))
6327 {
6328 complaint (&symfile_complaints,
6329 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6330 "for DW_FORM_block* DW_AT_location for "
6331 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6332 child_die->offset, objfile->name);
6333 continue;
6334 }
6335
6336 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6337 if (!attr_form_is_block (attr))
6338 {
6339 complaint (&symfile_complaints,
6340 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6341 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6342 child_die->offset, objfile->name);
6343 continue;
6344 }
6345 parameter->value = DW_BLOCK (attr)->data;
6346 parameter->value_size = DW_BLOCK (attr)->size;
6347
6348 /* Parameters are not pre-cleared by memset above. */
6349 parameter->data_value = NULL;
6350 parameter->data_value_size = 0;
6351 call_site->parameter_count++;
6352
6353 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6354 if (attr)
6355 {
6356 if (!attr_form_is_block (attr))
6357 complaint (&symfile_complaints,
6358 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6359 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6360 child_die->offset, objfile->name);
6361 else
6362 {
6363 parameter->data_value = DW_BLOCK (attr)->data;
6364 parameter->data_value_size = DW_BLOCK (attr)->size;
6365 }
6366 }
6367 }
6368 }
6369
6370 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6371 Return 1 if the attributes are present and valid, otherwise, return 0.
6372 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6373
6374 static int
6375 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6376 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6377 struct partial_symtab *ranges_pst)
6378 {
6379 struct objfile *objfile = cu->objfile;
6380 struct comp_unit_head *cu_header = &cu->header;
6381 bfd *obfd = objfile->obfd;
6382 unsigned int addr_size = cu_header->addr_size;
6383 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6384 /* Base address selection entry. */
6385 CORE_ADDR base;
6386 int found_base;
6387 unsigned int dummy;
6388 gdb_byte *buffer;
6389 CORE_ADDR marker;
6390 int low_set;
6391 CORE_ADDR low = 0;
6392 CORE_ADDR high = 0;
6393 CORE_ADDR baseaddr;
6394
6395 found_base = cu->base_known;
6396 base = cu->base_address;
6397
6398 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6399 if (offset >= dwarf2_per_objfile->ranges.size)
6400 {
6401 complaint (&symfile_complaints,
6402 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6403 offset);
6404 return 0;
6405 }
6406 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6407
6408 /* Read in the largest possible address. */
6409 marker = read_address (obfd, buffer, cu, &dummy);
6410 if ((marker & mask) == mask)
6411 {
6412 /* If we found the largest possible address, then
6413 read the base address. */
6414 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6415 buffer += 2 * addr_size;
6416 offset += 2 * addr_size;
6417 found_base = 1;
6418 }
6419
6420 low_set = 0;
6421
6422 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6423
6424 while (1)
6425 {
6426 CORE_ADDR range_beginning, range_end;
6427
6428 range_beginning = read_address (obfd, buffer, cu, &dummy);
6429 buffer += addr_size;
6430 range_end = read_address (obfd, buffer, cu, &dummy);
6431 buffer += addr_size;
6432 offset += 2 * addr_size;
6433
6434 /* An end of list marker is a pair of zero addresses. */
6435 if (range_beginning == 0 && range_end == 0)
6436 /* Found the end of list entry. */
6437 break;
6438
6439 /* Each base address selection entry is a pair of 2 values.
6440 The first is the largest possible address, the second is
6441 the base address. Check for a base address here. */
6442 if ((range_beginning & mask) == mask)
6443 {
6444 /* If we found the largest possible address, then
6445 read the base address. */
6446 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6447 found_base = 1;
6448 continue;
6449 }
6450
6451 if (!found_base)
6452 {
6453 /* We have no valid base address for the ranges
6454 data. */
6455 complaint (&symfile_complaints,
6456 _("Invalid .debug_ranges data (no base address)"));
6457 return 0;
6458 }
6459
6460 if (range_beginning > range_end)
6461 {
6462 /* Inverted range entries are invalid. */
6463 complaint (&symfile_complaints,
6464 _("Invalid .debug_ranges data (inverted range)"));
6465 return 0;
6466 }
6467
6468 /* Empty range entries have no effect. */
6469 if (range_beginning == range_end)
6470 continue;
6471
6472 range_beginning += base;
6473 range_end += base;
6474
6475 if (ranges_pst != NULL)
6476 addrmap_set_empty (objfile->psymtabs_addrmap,
6477 range_beginning + baseaddr,
6478 range_end - 1 + baseaddr,
6479 ranges_pst);
6480
6481 /* FIXME: This is recording everything as a low-high
6482 segment of consecutive addresses. We should have a
6483 data structure for discontiguous block ranges
6484 instead. */
6485 if (! low_set)
6486 {
6487 low = range_beginning;
6488 high = range_end;
6489 low_set = 1;
6490 }
6491 else
6492 {
6493 if (range_beginning < low)
6494 low = range_beginning;
6495 if (range_end > high)
6496 high = range_end;
6497 }
6498 }
6499
6500 if (! low_set)
6501 /* If the first entry is an end-of-list marker, the range
6502 describes an empty scope, i.e. no instructions. */
6503 return 0;
6504
6505 if (low_return)
6506 *low_return = low;
6507 if (high_return)
6508 *high_return = high;
6509 return 1;
6510 }
6511
6512 /* Get low and high pc attributes from a die. Return 1 if the attributes
6513 are present and valid, otherwise, return 0. Return -1 if the range is
6514 discontinuous, i.e. derived from DW_AT_ranges information. */
6515 static int
6516 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6517 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6518 struct partial_symtab *pst)
6519 {
6520 struct attribute *attr;
6521 CORE_ADDR low = 0;
6522 CORE_ADDR high = 0;
6523 int ret = 0;
6524
6525 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6526 if (attr)
6527 {
6528 high = DW_ADDR (attr);
6529 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6530 if (attr)
6531 low = DW_ADDR (attr);
6532 else
6533 /* Found high w/o low attribute. */
6534 return 0;
6535
6536 /* Found consecutive range of addresses. */
6537 ret = 1;
6538 }
6539 else
6540 {
6541 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6542 if (attr != NULL)
6543 {
6544 /* Value of the DW_AT_ranges attribute is the offset in the
6545 .debug_ranges section. */
6546 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6547 return 0;
6548 /* Found discontinuous range of addresses. */
6549 ret = -1;
6550 }
6551 }
6552
6553 /* read_partial_die has also the strict LOW < HIGH requirement. */
6554 if (high <= low)
6555 return 0;
6556
6557 /* When using the GNU linker, .gnu.linkonce. sections are used to
6558 eliminate duplicate copies of functions and vtables and such.
6559 The linker will arbitrarily choose one and discard the others.
6560 The AT_*_pc values for such functions refer to local labels in
6561 these sections. If the section from that file was discarded, the
6562 labels are not in the output, so the relocs get a value of 0.
6563 If this is a discarded function, mark the pc bounds as invalid,
6564 so that GDB will ignore it. */
6565 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6566 return 0;
6567
6568 *lowpc = low;
6569 if (highpc)
6570 *highpc = high;
6571 return ret;
6572 }
6573
6574 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6575 its low and high PC addresses. Do nothing if these addresses could not
6576 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6577 and HIGHPC to the high address if greater than HIGHPC. */
6578
6579 static void
6580 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6581 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6582 struct dwarf2_cu *cu)
6583 {
6584 CORE_ADDR low, high;
6585 struct die_info *child = die->child;
6586
6587 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6588 {
6589 *lowpc = min (*lowpc, low);
6590 *highpc = max (*highpc, high);
6591 }
6592
6593 /* If the language does not allow nested subprograms (either inside
6594 subprograms or lexical blocks), we're done. */
6595 if (cu->language != language_ada)
6596 return;
6597
6598 /* Check all the children of the given DIE. If it contains nested
6599 subprograms, then check their pc bounds. Likewise, we need to
6600 check lexical blocks as well, as they may also contain subprogram
6601 definitions. */
6602 while (child && child->tag)
6603 {
6604 if (child->tag == DW_TAG_subprogram
6605 || child->tag == DW_TAG_lexical_block)
6606 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6607 child = sibling_die (child);
6608 }
6609 }
6610
6611 /* Get the low and high pc's represented by the scope DIE, and store
6612 them in *LOWPC and *HIGHPC. If the correct values can't be
6613 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6614
6615 static void
6616 get_scope_pc_bounds (struct die_info *die,
6617 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6618 struct dwarf2_cu *cu)
6619 {
6620 CORE_ADDR best_low = (CORE_ADDR) -1;
6621 CORE_ADDR best_high = (CORE_ADDR) 0;
6622 CORE_ADDR current_low, current_high;
6623
6624 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6625 {
6626 best_low = current_low;
6627 best_high = current_high;
6628 }
6629 else
6630 {
6631 struct die_info *child = die->child;
6632
6633 while (child && child->tag)
6634 {
6635 switch (child->tag) {
6636 case DW_TAG_subprogram:
6637 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6638 break;
6639 case DW_TAG_namespace:
6640 case DW_TAG_module:
6641 /* FIXME: carlton/2004-01-16: Should we do this for
6642 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6643 that current GCC's always emit the DIEs corresponding
6644 to definitions of methods of classes as children of a
6645 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6646 the DIEs giving the declarations, which could be
6647 anywhere). But I don't see any reason why the
6648 standards says that they have to be there. */
6649 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6650
6651 if (current_low != ((CORE_ADDR) -1))
6652 {
6653 best_low = min (best_low, current_low);
6654 best_high = max (best_high, current_high);
6655 }
6656 break;
6657 default:
6658 /* Ignore. */
6659 break;
6660 }
6661
6662 child = sibling_die (child);
6663 }
6664 }
6665
6666 *lowpc = best_low;
6667 *highpc = best_high;
6668 }
6669
6670 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6671 in DIE. */
6672 static void
6673 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6674 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6675 {
6676 struct objfile *objfile = cu->objfile;
6677 struct attribute *attr;
6678
6679 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6680 if (attr)
6681 {
6682 CORE_ADDR high = DW_ADDR (attr);
6683
6684 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6685 if (attr)
6686 {
6687 CORE_ADDR low = DW_ADDR (attr);
6688
6689 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6690 }
6691 }
6692
6693 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6694 if (attr)
6695 {
6696 bfd *obfd = objfile->obfd;
6697
6698 /* The value of the DW_AT_ranges attribute is the offset of the
6699 address range list in the .debug_ranges section. */
6700 unsigned long offset = DW_UNSND (attr);
6701 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6702
6703 /* For some target architectures, but not others, the
6704 read_address function sign-extends the addresses it returns.
6705 To recognize base address selection entries, we need a
6706 mask. */
6707 unsigned int addr_size = cu->header.addr_size;
6708 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6709
6710 /* The base address, to which the next pair is relative. Note
6711 that this 'base' is a DWARF concept: most entries in a range
6712 list are relative, to reduce the number of relocs against the
6713 debugging information. This is separate from this function's
6714 'baseaddr' argument, which GDB uses to relocate debugging
6715 information from a shared library based on the address at
6716 which the library was loaded. */
6717 CORE_ADDR base = cu->base_address;
6718 int base_known = cu->base_known;
6719
6720 gdb_assert (dwarf2_per_objfile->ranges.readin);
6721 if (offset >= dwarf2_per_objfile->ranges.size)
6722 {
6723 complaint (&symfile_complaints,
6724 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6725 offset);
6726 return;
6727 }
6728
6729 for (;;)
6730 {
6731 unsigned int bytes_read;
6732 CORE_ADDR start, end;
6733
6734 start = read_address (obfd, buffer, cu, &bytes_read);
6735 buffer += bytes_read;
6736 end = read_address (obfd, buffer, cu, &bytes_read);
6737 buffer += bytes_read;
6738
6739 /* Did we find the end of the range list? */
6740 if (start == 0 && end == 0)
6741 break;
6742
6743 /* Did we find a base address selection entry? */
6744 else if ((start & base_select_mask) == base_select_mask)
6745 {
6746 base = end;
6747 base_known = 1;
6748 }
6749
6750 /* We found an ordinary address range. */
6751 else
6752 {
6753 if (!base_known)
6754 {
6755 complaint (&symfile_complaints,
6756 _("Invalid .debug_ranges data "
6757 "(no base address)"));
6758 return;
6759 }
6760
6761 if (start > end)
6762 {
6763 /* Inverted range entries are invalid. */
6764 complaint (&symfile_complaints,
6765 _("Invalid .debug_ranges data "
6766 "(inverted range)"));
6767 return;
6768 }
6769
6770 /* Empty range entries have no effect. */
6771 if (start == end)
6772 continue;
6773
6774 record_block_range (block,
6775 baseaddr + base + start,
6776 baseaddr + base + end - 1);
6777 }
6778 }
6779 }
6780 }
6781
6782 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6783 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6784 during 4.6.0 experimental. */
6785
6786 static int
6787 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6788 {
6789 const char *cs;
6790 int major, minor, release;
6791
6792 if (cu->producer == NULL)
6793 {
6794 /* For unknown compilers expect their behavior is DWARF version
6795 compliant.
6796
6797 GCC started to support .debug_types sections by -gdwarf-4 since
6798 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6799 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6800 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6801 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6802
6803 return 0;
6804 }
6805
6806 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6807
6808 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6809 {
6810 /* For non-GCC compilers expect their behavior is DWARF version
6811 compliant. */
6812
6813 return 0;
6814 }
6815 cs = &cu->producer[strlen ("GNU ")];
6816 while (*cs && !isdigit (*cs))
6817 cs++;
6818 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6819 {
6820 /* Not recognized as GCC. */
6821
6822 return 0;
6823 }
6824
6825 return major < 4 || (major == 4 && minor < 6);
6826 }
6827
6828 /* Return the default accessibility type if it is not overriden by
6829 DW_AT_accessibility. */
6830
6831 static enum dwarf_access_attribute
6832 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6833 {
6834 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6835 {
6836 /* The default DWARF 2 accessibility for members is public, the default
6837 accessibility for inheritance is private. */
6838
6839 if (die->tag != DW_TAG_inheritance)
6840 return DW_ACCESS_public;
6841 else
6842 return DW_ACCESS_private;
6843 }
6844 else
6845 {
6846 /* DWARF 3+ defines the default accessibility a different way. The same
6847 rules apply now for DW_TAG_inheritance as for the members and it only
6848 depends on the container kind. */
6849
6850 if (die->parent->tag == DW_TAG_class_type)
6851 return DW_ACCESS_private;
6852 else
6853 return DW_ACCESS_public;
6854 }
6855 }
6856
6857 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6858 offset. If the attribute was not found return 0, otherwise return
6859 1. If it was found but could not properly be handled, set *OFFSET
6860 to 0. */
6861
6862 static int
6863 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6864 LONGEST *offset)
6865 {
6866 struct attribute *attr;
6867
6868 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6869 if (attr != NULL)
6870 {
6871 *offset = 0;
6872
6873 /* Note that we do not check for a section offset first here.
6874 This is because DW_AT_data_member_location is new in DWARF 4,
6875 so if we see it, we can assume that a constant form is really
6876 a constant and not a section offset. */
6877 if (attr_form_is_constant (attr))
6878 *offset = dwarf2_get_attr_constant_value (attr, 0);
6879 else if (attr_form_is_section_offset (attr))
6880 dwarf2_complex_location_expr_complaint ();
6881 else if (attr_form_is_block (attr))
6882 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6883 else
6884 dwarf2_complex_location_expr_complaint ();
6885
6886 return 1;
6887 }
6888
6889 return 0;
6890 }
6891
6892 /* Add an aggregate field to the field list. */
6893
6894 static void
6895 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6896 struct dwarf2_cu *cu)
6897 {
6898 struct objfile *objfile = cu->objfile;
6899 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6900 struct nextfield *new_field;
6901 struct attribute *attr;
6902 struct field *fp;
6903 char *fieldname = "";
6904
6905 /* Allocate a new field list entry and link it in. */
6906 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6907 make_cleanup (xfree, new_field);
6908 memset (new_field, 0, sizeof (struct nextfield));
6909
6910 if (die->tag == DW_TAG_inheritance)
6911 {
6912 new_field->next = fip->baseclasses;
6913 fip->baseclasses = new_field;
6914 }
6915 else
6916 {
6917 new_field->next = fip->fields;
6918 fip->fields = new_field;
6919 }
6920 fip->nfields++;
6921
6922 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6923 if (attr)
6924 new_field->accessibility = DW_UNSND (attr);
6925 else
6926 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6927 if (new_field->accessibility != DW_ACCESS_public)
6928 fip->non_public_fields = 1;
6929
6930 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6931 if (attr)
6932 new_field->virtuality = DW_UNSND (attr);
6933 else
6934 new_field->virtuality = DW_VIRTUALITY_none;
6935
6936 fp = &new_field->field;
6937
6938 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6939 {
6940 LONGEST offset;
6941
6942 /* Data member other than a C++ static data member. */
6943
6944 /* Get type of field. */
6945 fp->type = die_type (die, cu);
6946
6947 SET_FIELD_BITPOS (*fp, 0);
6948
6949 /* Get bit size of field (zero if none). */
6950 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6951 if (attr)
6952 {
6953 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6954 }
6955 else
6956 {
6957 FIELD_BITSIZE (*fp) = 0;
6958 }
6959
6960 /* Get bit offset of field. */
6961 if (handle_data_member_location (die, cu, &offset))
6962 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6963 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6964 if (attr)
6965 {
6966 if (gdbarch_bits_big_endian (gdbarch))
6967 {
6968 /* For big endian bits, the DW_AT_bit_offset gives the
6969 additional bit offset from the MSB of the containing
6970 anonymous object to the MSB of the field. We don't
6971 have to do anything special since we don't need to
6972 know the size of the anonymous object. */
6973 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6974 }
6975 else
6976 {
6977 /* For little endian bits, compute the bit offset to the
6978 MSB of the anonymous object, subtract off the number of
6979 bits from the MSB of the field to the MSB of the
6980 object, and then subtract off the number of bits of
6981 the field itself. The result is the bit offset of
6982 the LSB of the field. */
6983 int anonymous_size;
6984 int bit_offset = DW_UNSND (attr);
6985
6986 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6987 if (attr)
6988 {
6989 /* The size of the anonymous object containing
6990 the bit field is explicit, so use the
6991 indicated size (in bytes). */
6992 anonymous_size = DW_UNSND (attr);
6993 }
6994 else
6995 {
6996 /* The size of the anonymous object containing
6997 the bit field must be inferred from the type
6998 attribute of the data member containing the
6999 bit field. */
7000 anonymous_size = TYPE_LENGTH (fp->type);
7001 }
7002 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7003 - bit_offset - FIELD_BITSIZE (*fp);
7004 }
7005 }
7006
7007 /* Get name of field. */
7008 fieldname = dwarf2_name (die, cu);
7009 if (fieldname == NULL)
7010 fieldname = "";
7011
7012 /* The name is already allocated along with this objfile, so we don't
7013 need to duplicate it for the type. */
7014 fp->name = fieldname;
7015
7016 /* Change accessibility for artificial fields (e.g. virtual table
7017 pointer or virtual base class pointer) to private. */
7018 if (dwarf2_attr (die, DW_AT_artificial, cu))
7019 {
7020 FIELD_ARTIFICIAL (*fp) = 1;
7021 new_field->accessibility = DW_ACCESS_private;
7022 fip->non_public_fields = 1;
7023 }
7024 }
7025 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7026 {
7027 /* C++ static member. */
7028
7029 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7030 is a declaration, but all versions of G++ as of this writing
7031 (so through at least 3.2.1) incorrectly generate
7032 DW_TAG_variable tags. */
7033
7034 const char *physname;
7035
7036 /* Get name of field. */
7037 fieldname = dwarf2_name (die, cu);
7038 if (fieldname == NULL)
7039 return;
7040
7041 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7042 if (attr
7043 /* Only create a symbol if this is an external value.
7044 new_symbol checks this and puts the value in the global symbol
7045 table, which we want. If it is not external, new_symbol
7046 will try to put the value in cu->list_in_scope which is wrong. */
7047 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7048 {
7049 /* A static const member, not much different than an enum as far as
7050 we're concerned, except that we can support more types. */
7051 new_symbol (die, NULL, cu);
7052 }
7053
7054 /* Get physical name. */
7055 physname = dwarf2_physname (fieldname, die, cu);
7056
7057 /* The name is already allocated along with this objfile, so we don't
7058 need to duplicate it for the type. */
7059 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7060 FIELD_TYPE (*fp) = die_type (die, cu);
7061 FIELD_NAME (*fp) = fieldname;
7062 }
7063 else if (die->tag == DW_TAG_inheritance)
7064 {
7065 LONGEST offset;
7066
7067 /* C++ base class field. */
7068 if (handle_data_member_location (die, cu, &offset))
7069 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7070 FIELD_BITSIZE (*fp) = 0;
7071 FIELD_TYPE (*fp) = die_type (die, cu);
7072 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7073 fip->nbaseclasses++;
7074 }
7075 }
7076
7077 /* Add a typedef defined in the scope of the FIP's class. */
7078
7079 static void
7080 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7081 struct dwarf2_cu *cu)
7082 {
7083 struct objfile *objfile = cu->objfile;
7084 struct typedef_field_list *new_field;
7085 struct attribute *attr;
7086 struct typedef_field *fp;
7087 char *fieldname = "";
7088
7089 /* Allocate a new field list entry and link it in. */
7090 new_field = xzalloc (sizeof (*new_field));
7091 make_cleanup (xfree, new_field);
7092
7093 gdb_assert (die->tag == DW_TAG_typedef);
7094
7095 fp = &new_field->field;
7096
7097 /* Get name of field. */
7098 fp->name = dwarf2_name (die, cu);
7099 if (fp->name == NULL)
7100 return;
7101
7102 fp->type = read_type_die (die, cu);
7103
7104 new_field->next = fip->typedef_field_list;
7105 fip->typedef_field_list = new_field;
7106 fip->typedef_field_list_count++;
7107 }
7108
7109 /* Create the vector of fields, and attach it to the type. */
7110
7111 static void
7112 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7113 struct dwarf2_cu *cu)
7114 {
7115 int nfields = fip->nfields;
7116
7117 /* Record the field count, allocate space for the array of fields,
7118 and create blank accessibility bitfields if necessary. */
7119 TYPE_NFIELDS (type) = nfields;
7120 TYPE_FIELDS (type) = (struct field *)
7121 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7122 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7123
7124 if (fip->non_public_fields && cu->language != language_ada)
7125 {
7126 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7127
7128 TYPE_FIELD_PRIVATE_BITS (type) =
7129 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7130 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7131
7132 TYPE_FIELD_PROTECTED_BITS (type) =
7133 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7134 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7135
7136 TYPE_FIELD_IGNORE_BITS (type) =
7137 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7138 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7139 }
7140
7141 /* If the type has baseclasses, allocate and clear a bit vector for
7142 TYPE_FIELD_VIRTUAL_BITS. */
7143 if (fip->nbaseclasses && cu->language != language_ada)
7144 {
7145 int num_bytes = B_BYTES (fip->nbaseclasses);
7146 unsigned char *pointer;
7147
7148 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7149 pointer = TYPE_ALLOC (type, num_bytes);
7150 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7151 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7152 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7153 }
7154
7155 /* Copy the saved-up fields into the field vector. Start from the head of
7156 the list, adding to the tail of the field array, so that they end up in
7157 the same order in the array in which they were added to the list. */
7158 while (nfields-- > 0)
7159 {
7160 struct nextfield *fieldp;
7161
7162 if (fip->fields)
7163 {
7164 fieldp = fip->fields;
7165 fip->fields = fieldp->next;
7166 }
7167 else
7168 {
7169 fieldp = fip->baseclasses;
7170 fip->baseclasses = fieldp->next;
7171 }
7172
7173 TYPE_FIELD (type, nfields) = fieldp->field;
7174 switch (fieldp->accessibility)
7175 {
7176 case DW_ACCESS_private:
7177 if (cu->language != language_ada)
7178 SET_TYPE_FIELD_PRIVATE (type, nfields);
7179 break;
7180
7181 case DW_ACCESS_protected:
7182 if (cu->language != language_ada)
7183 SET_TYPE_FIELD_PROTECTED (type, nfields);
7184 break;
7185
7186 case DW_ACCESS_public:
7187 break;
7188
7189 default:
7190 /* Unknown accessibility. Complain and treat it as public. */
7191 {
7192 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7193 fieldp->accessibility);
7194 }
7195 break;
7196 }
7197 if (nfields < fip->nbaseclasses)
7198 {
7199 switch (fieldp->virtuality)
7200 {
7201 case DW_VIRTUALITY_virtual:
7202 case DW_VIRTUALITY_pure_virtual:
7203 if (cu->language == language_ada)
7204 error (_("unexpected virtuality in component of Ada type"));
7205 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7206 break;
7207 }
7208 }
7209 }
7210 }
7211
7212 /* Add a member function to the proper fieldlist. */
7213
7214 static void
7215 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7216 struct type *type, struct dwarf2_cu *cu)
7217 {
7218 struct objfile *objfile = cu->objfile;
7219 struct attribute *attr;
7220 struct fnfieldlist *flp;
7221 int i;
7222 struct fn_field *fnp;
7223 char *fieldname;
7224 struct nextfnfield *new_fnfield;
7225 struct type *this_type;
7226 enum dwarf_access_attribute accessibility;
7227
7228 if (cu->language == language_ada)
7229 error (_("unexpected member function in Ada type"));
7230
7231 /* Get name of member function. */
7232 fieldname = dwarf2_name (die, cu);
7233 if (fieldname == NULL)
7234 return;
7235
7236 /* Look up member function name in fieldlist. */
7237 for (i = 0; i < fip->nfnfields; i++)
7238 {
7239 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7240 break;
7241 }
7242
7243 /* Create new list element if necessary. */
7244 if (i < fip->nfnfields)
7245 flp = &fip->fnfieldlists[i];
7246 else
7247 {
7248 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7249 {
7250 fip->fnfieldlists = (struct fnfieldlist *)
7251 xrealloc (fip->fnfieldlists,
7252 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7253 * sizeof (struct fnfieldlist));
7254 if (fip->nfnfields == 0)
7255 make_cleanup (free_current_contents, &fip->fnfieldlists);
7256 }
7257 flp = &fip->fnfieldlists[fip->nfnfields];
7258 flp->name = fieldname;
7259 flp->length = 0;
7260 flp->head = NULL;
7261 i = fip->nfnfields++;
7262 }
7263
7264 /* Create a new member function field and chain it to the field list
7265 entry. */
7266 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7267 make_cleanup (xfree, new_fnfield);
7268 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7269 new_fnfield->next = flp->head;
7270 flp->head = new_fnfield;
7271 flp->length++;
7272
7273 /* Fill in the member function field info. */
7274 fnp = &new_fnfield->fnfield;
7275
7276 /* Delay processing of the physname until later. */
7277 if (cu->language == language_cplus || cu->language == language_java)
7278 {
7279 add_to_method_list (type, i, flp->length - 1, fieldname,
7280 die, cu);
7281 }
7282 else
7283 {
7284 const char *physname = dwarf2_physname (fieldname, die, cu);
7285 fnp->physname = physname ? physname : "";
7286 }
7287
7288 fnp->type = alloc_type (objfile);
7289 this_type = read_type_die (die, cu);
7290 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7291 {
7292 int nparams = TYPE_NFIELDS (this_type);
7293
7294 /* TYPE is the domain of this method, and THIS_TYPE is the type
7295 of the method itself (TYPE_CODE_METHOD). */
7296 smash_to_method_type (fnp->type, type,
7297 TYPE_TARGET_TYPE (this_type),
7298 TYPE_FIELDS (this_type),
7299 TYPE_NFIELDS (this_type),
7300 TYPE_VARARGS (this_type));
7301
7302 /* Handle static member functions.
7303 Dwarf2 has no clean way to discern C++ static and non-static
7304 member functions. G++ helps GDB by marking the first
7305 parameter for non-static member functions (which is the this
7306 pointer) as artificial. We obtain this information from
7307 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7308 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7309 fnp->voffset = VOFFSET_STATIC;
7310 }
7311 else
7312 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7313 dwarf2_full_name (fieldname, die, cu));
7314
7315 /* Get fcontext from DW_AT_containing_type if present. */
7316 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7317 fnp->fcontext = die_containing_type (die, cu);
7318
7319 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7320 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7321
7322 /* Get accessibility. */
7323 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7324 if (attr)
7325 accessibility = DW_UNSND (attr);
7326 else
7327 accessibility = dwarf2_default_access_attribute (die, cu);
7328 switch (accessibility)
7329 {
7330 case DW_ACCESS_private:
7331 fnp->is_private = 1;
7332 break;
7333 case DW_ACCESS_protected:
7334 fnp->is_protected = 1;
7335 break;
7336 }
7337
7338 /* Check for artificial methods. */
7339 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7340 if (attr && DW_UNSND (attr) != 0)
7341 fnp->is_artificial = 1;
7342
7343 /* Get index in virtual function table if it is a virtual member
7344 function. For older versions of GCC, this is an offset in the
7345 appropriate virtual table, as specified by DW_AT_containing_type.
7346 For everyone else, it is an expression to be evaluated relative
7347 to the object address. */
7348
7349 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7350 if (attr)
7351 {
7352 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7353 {
7354 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7355 {
7356 /* Old-style GCC. */
7357 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7358 }
7359 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7360 || (DW_BLOCK (attr)->size > 1
7361 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7362 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7363 {
7364 struct dwarf_block blk;
7365 int offset;
7366
7367 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7368 ? 1 : 2);
7369 blk.size = DW_BLOCK (attr)->size - offset;
7370 blk.data = DW_BLOCK (attr)->data + offset;
7371 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7372 if ((fnp->voffset % cu->header.addr_size) != 0)
7373 dwarf2_complex_location_expr_complaint ();
7374 else
7375 fnp->voffset /= cu->header.addr_size;
7376 fnp->voffset += 2;
7377 }
7378 else
7379 dwarf2_complex_location_expr_complaint ();
7380
7381 if (!fnp->fcontext)
7382 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7383 }
7384 else if (attr_form_is_section_offset (attr))
7385 {
7386 dwarf2_complex_location_expr_complaint ();
7387 }
7388 else
7389 {
7390 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7391 fieldname);
7392 }
7393 }
7394 else
7395 {
7396 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7397 if (attr && DW_UNSND (attr))
7398 {
7399 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7400 complaint (&symfile_complaints,
7401 _("Member function \"%s\" (offset %d) is virtual "
7402 "but the vtable offset is not specified"),
7403 fieldname, die->offset);
7404 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7405 TYPE_CPLUS_DYNAMIC (type) = 1;
7406 }
7407 }
7408 }
7409
7410 /* Create the vector of member function fields, and attach it to the type. */
7411
7412 static void
7413 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7414 struct dwarf2_cu *cu)
7415 {
7416 struct fnfieldlist *flp;
7417 int i;
7418
7419 if (cu->language == language_ada)
7420 error (_("unexpected member functions in Ada type"));
7421
7422 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7423 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7424 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7425
7426 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7427 {
7428 struct nextfnfield *nfp = flp->head;
7429 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7430 int k;
7431
7432 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7433 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7434 fn_flp->fn_fields = (struct fn_field *)
7435 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7436 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7437 fn_flp->fn_fields[k] = nfp->fnfield;
7438 }
7439
7440 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7441 }
7442
7443 /* Returns non-zero if NAME is the name of a vtable member in CU's
7444 language, zero otherwise. */
7445 static int
7446 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7447 {
7448 static const char vptr[] = "_vptr";
7449 static const char vtable[] = "vtable";
7450
7451 /* Look for the C++ and Java forms of the vtable. */
7452 if ((cu->language == language_java
7453 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7454 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7455 && is_cplus_marker (name[sizeof (vptr) - 1])))
7456 return 1;
7457
7458 return 0;
7459 }
7460
7461 /* GCC outputs unnamed structures that are really pointers to member
7462 functions, with the ABI-specified layout. If TYPE describes
7463 such a structure, smash it into a member function type.
7464
7465 GCC shouldn't do this; it should just output pointer to member DIEs.
7466 This is GCC PR debug/28767. */
7467
7468 static void
7469 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7470 {
7471 struct type *pfn_type, *domain_type, *new_type;
7472
7473 /* Check for a structure with no name and two children. */
7474 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7475 return;
7476
7477 /* Check for __pfn and __delta members. */
7478 if (TYPE_FIELD_NAME (type, 0) == NULL
7479 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7480 || TYPE_FIELD_NAME (type, 1) == NULL
7481 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7482 return;
7483
7484 /* Find the type of the method. */
7485 pfn_type = TYPE_FIELD_TYPE (type, 0);
7486 if (pfn_type == NULL
7487 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7488 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7489 return;
7490
7491 /* Look for the "this" argument. */
7492 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7493 if (TYPE_NFIELDS (pfn_type) == 0
7494 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7495 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7496 return;
7497
7498 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7499 new_type = alloc_type (objfile);
7500 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7501 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7502 TYPE_VARARGS (pfn_type));
7503 smash_to_methodptr_type (type, new_type);
7504 }
7505
7506 /* Called when we find the DIE that starts a structure or union scope
7507 (definition) to create a type for the structure or union. Fill in
7508 the type's name and general properties; the members will not be
7509 processed until process_structure_type.
7510
7511 NOTE: we need to call these functions regardless of whether or not the
7512 DIE has a DW_AT_name attribute, since it might be an anonymous
7513 structure or union. This gets the type entered into our set of
7514 user defined types.
7515
7516 However, if the structure is incomplete (an opaque struct/union)
7517 then suppress creating a symbol table entry for it since gdb only
7518 wants to find the one with the complete definition. Note that if
7519 it is complete, we just call new_symbol, which does it's own
7520 checking about whether the struct/union is anonymous or not (and
7521 suppresses creating a symbol table entry itself). */
7522
7523 static struct type *
7524 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7525 {
7526 struct objfile *objfile = cu->objfile;
7527 struct type *type;
7528 struct attribute *attr;
7529 char *name;
7530
7531 /* If the definition of this type lives in .debug_types, read that type.
7532 Don't follow DW_AT_specification though, that will take us back up
7533 the chain and we want to go down. */
7534 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7535 if (attr)
7536 {
7537 struct dwarf2_cu *type_cu = cu;
7538 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7539
7540 /* We could just recurse on read_structure_type, but we need to call
7541 get_die_type to ensure only one type for this DIE is created.
7542 This is important, for example, because for c++ classes we need
7543 TYPE_NAME set which is only done by new_symbol. Blech. */
7544 type = read_type_die (type_die, type_cu);
7545
7546 /* TYPE_CU may not be the same as CU.
7547 Ensure TYPE is recorded in CU's type_hash table. */
7548 return set_die_type (die, type, cu);
7549 }
7550
7551 type = alloc_type (objfile);
7552 INIT_CPLUS_SPECIFIC (type);
7553
7554 name = dwarf2_name (die, cu);
7555 if (name != NULL)
7556 {
7557 if (cu->language == language_cplus
7558 || cu->language == language_java)
7559 {
7560 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7561
7562 /* dwarf2_full_name might have already finished building the DIE's
7563 type. If so, there is no need to continue. */
7564 if (get_die_type (die, cu) != NULL)
7565 return get_die_type (die, cu);
7566
7567 TYPE_TAG_NAME (type) = full_name;
7568 if (die->tag == DW_TAG_structure_type
7569 || die->tag == DW_TAG_class_type)
7570 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7571 }
7572 else
7573 {
7574 /* The name is already allocated along with this objfile, so
7575 we don't need to duplicate it for the type. */
7576 TYPE_TAG_NAME (type) = (char *) name;
7577 if (die->tag == DW_TAG_class_type)
7578 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7579 }
7580 }
7581
7582 if (die->tag == DW_TAG_structure_type)
7583 {
7584 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7585 }
7586 else if (die->tag == DW_TAG_union_type)
7587 {
7588 TYPE_CODE (type) = TYPE_CODE_UNION;
7589 }
7590 else
7591 {
7592 TYPE_CODE (type) = TYPE_CODE_CLASS;
7593 }
7594
7595 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7596 TYPE_DECLARED_CLASS (type) = 1;
7597
7598 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7599 if (attr)
7600 {
7601 TYPE_LENGTH (type) = DW_UNSND (attr);
7602 }
7603 else
7604 {
7605 TYPE_LENGTH (type) = 0;
7606 }
7607
7608 TYPE_STUB_SUPPORTED (type) = 1;
7609 if (die_is_declaration (die, cu))
7610 TYPE_STUB (type) = 1;
7611 else if (attr == NULL && die->child == NULL
7612 && producer_is_realview (cu->producer))
7613 /* RealView does not output the required DW_AT_declaration
7614 on incomplete types. */
7615 TYPE_STUB (type) = 1;
7616
7617 /* We need to add the type field to the die immediately so we don't
7618 infinitely recurse when dealing with pointers to the structure
7619 type within the structure itself. */
7620 set_die_type (die, type, cu);
7621
7622 /* set_die_type should be already done. */
7623 set_descriptive_type (type, die, cu);
7624
7625 return type;
7626 }
7627
7628 /* Finish creating a structure or union type, including filling in
7629 its members and creating a symbol for it. */
7630
7631 static void
7632 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7633 {
7634 struct objfile *objfile = cu->objfile;
7635 struct die_info *child_die = die->child;
7636 struct type *type;
7637
7638 type = get_die_type (die, cu);
7639 if (type == NULL)
7640 type = read_structure_type (die, cu);
7641
7642 if (die->child != NULL && ! die_is_declaration (die, cu))
7643 {
7644 struct field_info fi;
7645 struct die_info *child_die;
7646 VEC (symbolp) *template_args = NULL;
7647 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7648
7649 memset (&fi, 0, sizeof (struct field_info));
7650
7651 child_die = die->child;
7652
7653 while (child_die && child_die->tag)
7654 {
7655 if (child_die->tag == DW_TAG_member
7656 || child_die->tag == DW_TAG_variable)
7657 {
7658 /* NOTE: carlton/2002-11-05: A C++ static data member
7659 should be a DW_TAG_member that is a declaration, but
7660 all versions of G++ as of this writing (so through at
7661 least 3.2.1) incorrectly generate DW_TAG_variable
7662 tags for them instead. */
7663 dwarf2_add_field (&fi, child_die, cu);
7664 }
7665 else if (child_die->tag == DW_TAG_subprogram)
7666 {
7667 /* C++ member function. */
7668 dwarf2_add_member_fn (&fi, child_die, type, cu);
7669 }
7670 else if (child_die->tag == DW_TAG_inheritance)
7671 {
7672 /* C++ base class field. */
7673 dwarf2_add_field (&fi, child_die, cu);
7674 }
7675 else if (child_die->tag == DW_TAG_typedef)
7676 dwarf2_add_typedef (&fi, child_die, cu);
7677 else if (child_die->tag == DW_TAG_template_type_param
7678 || child_die->tag == DW_TAG_template_value_param)
7679 {
7680 struct symbol *arg = new_symbol (child_die, NULL, cu);
7681
7682 if (arg != NULL)
7683 VEC_safe_push (symbolp, template_args, arg);
7684 }
7685
7686 child_die = sibling_die (child_die);
7687 }
7688
7689 /* Attach template arguments to type. */
7690 if (! VEC_empty (symbolp, template_args))
7691 {
7692 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7693 TYPE_N_TEMPLATE_ARGUMENTS (type)
7694 = VEC_length (symbolp, template_args);
7695 TYPE_TEMPLATE_ARGUMENTS (type)
7696 = obstack_alloc (&objfile->objfile_obstack,
7697 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7698 * sizeof (struct symbol *)));
7699 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7700 VEC_address (symbolp, template_args),
7701 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7702 * sizeof (struct symbol *)));
7703 VEC_free (symbolp, template_args);
7704 }
7705
7706 /* Attach fields and member functions to the type. */
7707 if (fi.nfields)
7708 dwarf2_attach_fields_to_type (&fi, type, cu);
7709 if (fi.nfnfields)
7710 {
7711 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7712
7713 /* Get the type which refers to the base class (possibly this
7714 class itself) which contains the vtable pointer for the current
7715 class from the DW_AT_containing_type attribute. This use of
7716 DW_AT_containing_type is a GNU extension. */
7717
7718 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7719 {
7720 struct type *t = die_containing_type (die, cu);
7721
7722 TYPE_VPTR_BASETYPE (type) = t;
7723 if (type == t)
7724 {
7725 int i;
7726
7727 /* Our own class provides vtbl ptr. */
7728 for (i = TYPE_NFIELDS (t) - 1;
7729 i >= TYPE_N_BASECLASSES (t);
7730 --i)
7731 {
7732 char *fieldname = TYPE_FIELD_NAME (t, i);
7733
7734 if (is_vtable_name (fieldname, cu))
7735 {
7736 TYPE_VPTR_FIELDNO (type) = i;
7737 break;
7738 }
7739 }
7740
7741 /* Complain if virtual function table field not found. */
7742 if (i < TYPE_N_BASECLASSES (t))
7743 complaint (&symfile_complaints,
7744 _("virtual function table pointer "
7745 "not found when defining class '%s'"),
7746 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7747 "");
7748 }
7749 else
7750 {
7751 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7752 }
7753 }
7754 else if (cu->producer
7755 && strncmp (cu->producer,
7756 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7757 {
7758 /* The IBM XLC compiler does not provide direct indication
7759 of the containing type, but the vtable pointer is
7760 always named __vfp. */
7761
7762 int i;
7763
7764 for (i = TYPE_NFIELDS (type) - 1;
7765 i >= TYPE_N_BASECLASSES (type);
7766 --i)
7767 {
7768 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7769 {
7770 TYPE_VPTR_FIELDNO (type) = i;
7771 TYPE_VPTR_BASETYPE (type) = type;
7772 break;
7773 }
7774 }
7775 }
7776 }
7777
7778 /* Copy fi.typedef_field_list linked list elements content into the
7779 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7780 if (fi.typedef_field_list)
7781 {
7782 int i = fi.typedef_field_list_count;
7783
7784 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7785 TYPE_TYPEDEF_FIELD_ARRAY (type)
7786 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7787 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7788
7789 /* Reverse the list order to keep the debug info elements order. */
7790 while (--i >= 0)
7791 {
7792 struct typedef_field *dest, *src;
7793
7794 dest = &TYPE_TYPEDEF_FIELD (type, i);
7795 src = &fi.typedef_field_list->field;
7796 fi.typedef_field_list = fi.typedef_field_list->next;
7797 *dest = *src;
7798 }
7799 }
7800
7801 do_cleanups (back_to);
7802
7803 if (HAVE_CPLUS_STRUCT (type))
7804 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7805 }
7806
7807 quirk_gcc_member_function_pointer (type, objfile);
7808
7809 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7810 snapshots) has been known to create a die giving a declaration
7811 for a class that has, as a child, a die giving a definition for a
7812 nested class. So we have to process our children even if the
7813 current die is a declaration. Normally, of course, a declaration
7814 won't have any children at all. */
7815
7816 while (child_die != NULL && child_die->tag)
7817 {
7818 if (child_die->tag == DW_TAG_member
7819 || child_die->tag == DW_TAG_variable
7820 || child_die->tag == DW_TAG_inheritance
7821 || child_die->tag == DW_TAG_template_value_param
7822 || child_die->tag == DW_TAG_template_type_param)
7823 {
7824 /* Do nothing. */
7825 }
7826 else
7827 process_die (child_die, cu);
7828
7829 child_die = sibling_die (child_die);
7830 }
7831
7832 /* Do not consider external references. According to the DWARF standard,
7833 these DIEs are identified by the fact that they have no byte_size
7834 attribute, and a declaration attribute. */
7835 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7836 || !die_is_declaration (die, cu))
7837 new_symbol (die, type, cu);
7838 }
7839
7840 /* Given a DW_AT_enumeration_type die, set its type. We do not
7841 complete the type's fields yet, or create any symbols. */
7842
7843 static struct type *
7844 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7845 {
7846 struct objfile *objfile = cu->objfile;
7847 struct type *type;
7848 struct attribute *attr;
7849 const char *name;
7850
7851 /* If the definition of this type lives in .debug_types, read that type.
7852 Don't follow DW_AT_specification though, that will take us back up
7853 the chain and we want to go down. */
7854 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7855 if (attr)
7856 {
7857 struct dwarf2_cu *type_cu = cu;
7858 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7859
7860 type = read_type_die (type_die, type_cu);
7861
7862 /* TYPE_CU may not be the same as CU.
7863 Ensure TYPE is recorded in CU's type_hash table. */
7864 return set_die_type (die, type, cu);
7865 }
7866
7867 type = alloc_type (objfile);
7868
7869 TYPE_CODE (type) = TYPE_CODE_ENUM;
7870 name = dwarf2_full_name (NULL, die, cu);
7871 if (name != NULL)
7872 TYPE_TAG_NAME (type) = (char *) name;
7873
7874 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7875 if (attr)
7876 {
7877 TYPE_LENGTH (type) = DW_UNSND (attr);
7878 }
7879 else
7880 {
7881 TYPE_LENGTH (type) = 0;
7882 }
7883
7884 /* The enumeration DIE can be incomplete. In Ada, any type can be
7885 declared as private in the package spec, and then defined only
7886 inside the package body. Such types are known as Taft Amendment
7887 Types. When another package uses such a type, an incomplete DIE
7888 may be generated by the compiler. */
7889 if (die_is_declaration (die, cu))
7890 TYPE_STUB (type) = 1;
7891
7892 return set_die_type (die, type, cu);
7893 }
7894
7895 /* Given a pointer to a die which begins an enumeration, process all
7896 the dies that define the members of the enumeration, and create the
7897 symbol for the enumeration type.
7898
7899 NOTE: We reverse the order of the element list. */
7900
7901 static void
7902 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7903 {
7904 struct type *this_type;
7905
7906 this_type = get_die_type (die, cu);
7907 if (this_type == NULL)
7908 this_type = read_enumeration_type (die, cu);
7909
7910 if (die->child != NULL)
7911 {
7912 struct die_info *child_die;
7913 struct symbol *sym;
7914 struct field *fields = NULL;
7915 int num_fields = 0;
7916 int unsigned_enum = 1;
7917 char *name;
7918 int flag_enum = 1;
7919 ULONGEST mask = 0;
7920
7921 child_die = die->child;
7922 while (child_die && child_die->tag)
7923 {
7924 if (child_die->tag != DW_TAG_enumerator)
7925 {
7926 process_die (child_die, cu);
7927 }
7928 else
7929 {
7930 name = dwarf2_name (child_die, cu);
7931 if (name)
7932 {
7933 sym = new_symbol (child_die, this_type, cu);
7934 if (SYMBOL_VALUE (sym) < 0)
7935 {
7936 unsigned_enum = 0;
7937 flag_enum = 0;
7938 }
7939 else if ((mask & SYMBOL_VALUE (sym)) != 0)
7940 flag_enum = 0;
7941 else
7942 mask |= SYMBOL_VALUE (sym);
7943
7944 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7945 {
7946 fields = (struct field *)
7947 xrealloc (fields,
7948 (num_fields + DW_FIELD_ALLOC_CHUNK)
7949 * sizeof (struct field));
7950 }
7951
7952 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7953 FIELD_TYPE (fields[num_fields]) = NULL;
7954 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7955 FIELD_BITSIZE (fields[num_fields]) = 0;
7956
7957 num_fields++;
7958 }
7959 }
7960
7961 child_die = sibling_die (child_die);
7962 }
7963
7964 if (num_fields)
7965 {
7966 TYPE_NFIELDS (this_type) = num_fields;
7967 TYPE_FIELDS (this_type) = (struct field *)
7968 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7969 memcpy (TYPE_FIELDS (this_type), fields,
7970 sizeof (struct field) * num_fields);
7971 xfree (fields);
7972 }
7973 if (unsigned_enum)
7974 TYPE_UNSIGNED (this_type) = 1;
7975 if (flag_enum)
7976 TYPE_FLAG_ENUM (this_type) = 1;
7977 }
7978
7979 /* If we are reading an enum from a .debug_types unit, and the enum
7980 is a declaration, and the enum is not the signatured type in the
7981 unit, then we do not want to add a symbol for it. Adding a
7982 symbol would in some cases obscure the true definition of the
7983 enum, giving users an incomplete type when the definition is
7984 actually available. Note that we do not want to do this for all
7985 enums which are just declarations, because C++0x allows forward
7986 enum declarations. */
7987 if (cu->per_cu->debug_types_section
7988 && die_is_declaration (die, cu))
7989 {
7990 struct signatured_type *type_sig;
7991
7992 type_sig
7993 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7994 cu->per_cu->debug_types_section,
7995 cu->per_cu->offset);
7996 if (type_sig->type_offset != die->offset)
7997 return;
7998 }
7999
8000 new_symbol (die, this_type, cu);
8001 }
8002
8003 /* Extract all information from a DW_TAG_array_type DIE and put it in
8004 the DIE's type field. For now, this only handles one dimensional
8005 arrays. */
8006
8007 static struct type *
8008 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8009 {
8010 struct objfile *objfile = cu->objfile;
8011 struct die_info *child_die;
8012 struct type *type;
8013 struct type *element_type, *range_type, *index_type;
8014 struct type **range_types = NULL;
8015 struct attribute *attr;
8016 int ndim = 0;
8017 struct cleanup *back_to;
8018 char *name;
8019
8020 element_type = die_type (die, cu);
8021
8022 /* The die_type call above may have already set the type for this DIE. */
8023 type = get_die_type (die, cu);
8024 if (type)
8025 return type;
8026
8027 /* Irix 6.2 native cc creates array types without children for
8028 arrays with unspecified length. */
8029 if (die->child == NULL)
8030 {
8031 index_type = objfile_type (objfile)->builtin_int;
8032 range_type = create_range_type (NULL, index_type, 0, -1);
8033 type = create_array_type (NULL, element_type, range_type);
8034 return set_die_type (die, type, cu);
8035 }
8036
8037 back_to = make_cleanup (null_cleanup, NULL);
8038 child_die = die->child;
8039 while (child_die && child_die->tag)
8040 {
8041 if (child_die->tag == DW_TAG_subrange_type)
8042 {
8043 struct type *child_type = read_type_die (child_die, cu);
8044
8045 if (child_type != NULL)
8046 {
8047 /* The range type was succesfully read. Save it for the
8048 array type creation. */
8049 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8050 {
8051 range_types = (struct type **)
8052 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8053 * sizeof (struct type *));
8054 if (ndim == 0)
8055 make_cleanup (free_current_contents, &range_types);
8056 }
8057 range_types[ndim++] = child_type;
8058 }
8059 }
8060 child_die = sibling_die (child_die);
8061 }
8062
8063 /* Dwarf2 dimensions are output from left to right, create the
8064 necessary array types in backwards order. */
8065
8066 type = element_type;
8067
8068 if (read_array_order (die, cu) == DW_ORD_col_major)
8069 {
8070 int i = 0;
8071
8072 while (i < ndim)
8073 type = create_array_type (NULL, type, range_types[i++]);
8074 }
8075 else
8076 {
8077 while (ndim-- > 0)
8078 type = create_array_type (NULL, type, range_types[ndim]);
8079 }
8080
8081 /* Understand Dwarf2 support for vector types (like they occur on
8082 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8083 array type. This is not part of the Dwarf2/3 standard yet, but a
8084 custom vendor extension. The main difference between a regular
8085 array and the vector variant is that vectors are passed by value
8086 to functions. */
8087 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8088 if (attr)
8089 make_vector_type (type);
8090
8091 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8092 implementation may choose to implement triple vectors using this
8093 attribute. */
8094 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8095 if (attr)
8096 {
8097 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8098 TYPE_LENGTH (type) = DW_UNSND (attr);
8099 else
8100 complaint (&symfile_complaints,
8101 _("DW_AT_byte_size for array type smaller "
8102 "than the total size of elements"));
8103 }
8104
8105 name = dwarf2_name (die, cu);
8106 if (name)
8107 TYPE_NAME (type) = name;
8108
8109 /* Install the type in the die. */
8110 set_die_type (die, type, cu);
8111
8112 /* set_die_type should be already done. */
8113 set_descriptive_type (type, die, cu);
8114
8115 do_cleanups (back_to);
8116
8117 return type;
8118 }
8119
8120 static enum dwarf_array_dim_ordering
8121 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8122 {
8123 struct attribute *attr;
8124
8125 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8126
8127 if (attr) return DW_SND (attr);
8128
8129 /* GNU F77 is a special case, as at 08/2004 array type info is the
8130 opposite order to the dwarf2 specification, but data is still
8131 laid out as per normal fortran.
8132
8133 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8134 version checking. */
8135
8136 if (cu->language == language_fortran
8137 && cu->producer && strstr (cu->producer, "GNU F77"))
8138 {
8139 return DW_ORD_row_major;
8140 }
8141
8142 switch (cu->language_defn->la_array_ordering)
8143 {
8144 case array_column_major:
8145 return DW_ORD_col_major;
8146 case array_row_major:
8147 default:
8148 return DW_ORD_row_major;
8149 };
8150 }
8151
8152 /* Extract all information from a DW_TAG_set_type DIE and put it in
8153 the DIE's type field. */
8154
8155 static struct type *
8156 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8157 {
8158 struct type *domain_type, *set_type;
8159 struct attribute *attr;
8160
8161 domain_type = die_type (die, cu);
8162
8163 /* The die_type call above may have already set the type for this DIE. */
8164 set_type = get_die_type (die, cu);
8165 if (set_type)
8166 return set_type;
8167
8168 set_type = create_set_type (NULL, domain_type);
8169
8170 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8171 if (attr)
8172 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8173
8174 return set_die_type (die, set_type, cu);
8175 }
8176
8177 /* First cut: install each common block member as a global variable. */
8178
8179 static void
8180 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8181 {
8182 struct die_info *child_die;
8183 struct attribute *attr;
8184 struct symbol *sym;
8185 CORE_ADDR base = (CORE_ADDR) 0;
8186
8187 attr = dwarf2_attr (die, DW_AT_location, cu);
8188 if (attr)
8189 {
8190 /* Support the .debug_loc offsets. */
8191 if (attr_form_is_block (attr))
8192 {
8193 base = decode_locdesc (DW_BLOCK (attr), cu);
8194 }
8195 else if (attr_form_is_section_offset (attr))
8196 {
8197 dwarf2_complex_location_expr_complaint ();
8198 }
8199 else
8200 {
8201 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8202 "common block member");
8203 }
8204 }
8205 if (die->child != NULL)
8206 {
8207 child_die = die->child;
8208 while (child_die && child_die->tag)
8209 {
8210 LONGEST offset;
8211
8212 sym = new_symbol (child_die, NULL, cu);
8213 if (sym != NULL
8214 && handle_data_member_location (child_die, cu, &offset))
8215 {
8216 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8217 add_symbol_to_list (sym, &global_symbols);
8218 }
8219 child_die = sibling_die (child_die);
8220 }
8221 }
8222 }
8223
8224 /* Create a type for a C++ namespace. */
8225
8226 static struct type *
8227 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8228 {
8229 struct objfile *objfile = cu->objfile;
8230 const char *previous_prefix, *name;
8231 int is_anonymous;
8232 struct type *type;
8233
8234 /* For extensions, reuse the type of the original namespace. */
8235 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8236 {
8237 struct die_info *ext_die;
8238 struct dwarf2_cu *ext_cu = cu;
8239
8240 ext_die = dwarf2_extension (die, &ext_cu);
8241 type = read_type_die (ext_die, ext_cu);
8242
8243 /* EXT_CU may not be the same as CU.
8244 Ensure TYPE is recorded in CU's type_hash table. */
8245 return set_die_type (die, type, cu);
8246 }
8247
8248 name = namespace_name (die, &is_anonymous, cu);
8249
8250 /* Now build the name of the current namespace. */
8251
8252 previous_prefix = determine_prefix (die, cu);
8253 if (previous_prefix[0] != '\0')
8254 name = typename_concat (&objfile->objfile_obstack,
8255 previous_prefix, name, 0, cu);
8256
8257 /* Create the type. */
8258 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8259 objfile);
8260 TYPE_NAME (type) = (char *) name;
8261 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8262
8263 return set_die_type (die, type, cu);
8264 }
8265
8266 /* Read a C++ namespace. */
8267
8268 static void
8269 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8270 {
8271 struct objfile *objfile = cu->objfile;
8272 int is_anonymous;
8273
8274 /* Add a symbol associated to this if we haven't seen the namespace
8275 before. Also, add a using directive if it's an anonymous
8276 namespace. */
8277
8278 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8279 {
8280 struct type *type;
8281
8282 type = read_type_die (die, cu);
8283 new_symbol (die, type, cu);
8284
8285 namespace_name (die, &is_anonymous, cu);
8286 if (is_anonymous)
8287 {
8288 const char *previous_prefix = determine_prefix (die, cu);
8289
8290 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8291 NULL, NULL, &objfile->objfile_obstack);
8292 }
8293 }
8294
8295 if (die->child != NULL)
8296 {
8297 struct die_info *child_die = die->child;
8298
8299 while (child_die && child_die->tag)
8300 {
8301 process_die (child_die, cu);
8302 child_die = sibling_die (child_die);
8303 }
8304 }
8305 }
8306
8307 /* Read a Fortran module as type. This DIE can be only a declaration used for
8308 imported module. Still we need that type as local Fortran "use ... only"
8309 declaration imports depend on the created type in determine_prefix. */
8310
8311 static struct type *
8312 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8313 {
8314 struct objfile *objfile = cu->objfile;
8315 char *module_name;
8316 struct type *type;
8317
8318 module_name = dwarf2_name (die, cu);
8319 if (!module_name)
8320 complaint (&symfile_complaints,
8321 _("DW_TAG_module has no name, offset 0x%x"),
8322 die->offset);
8323 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8324
8325 /* determine_prefix uses TYPE_TAG_NAME. */
8326 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8327
8328 return set_die_type (die, type, cu);
8329 }
8330
8331 /* Read a Fortran module. */
8332
8333 static void
8334 read_module (struct die_info *die, struct dwarf2_cu *cu)
8335 {
8336 struct die_info *child_die = die->child;
8337
8338 while (child_die && child_die->tag)
8339 {
8340 process_die (child_die, cu);
8341 child_die = sibling_die (child_die);
8342 }
8343 }
8344
8345 /* Return the name of the namespace represented by DIE. Set
8346 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8347 namespace. */
8348
8349 static const char *
8350 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8351 {
8352 struct die_info *current_die;
8353 const char *name = NULL;
8354
8355 /* Loop through the extensions until we find a name. */
8356
8357 for (current_die = die;
8358 current_die != NULL;
8359 current_die = dwarf2_extension (die, &cu))
8360 {
8361 name = dwarf2_name (current_die, cu);
8362 if (name != NULL)
8363 break;
8364 }
8365
8366 /* Is it an anonymous namespace? */
8367
8368 *is_anonymous = (name == NULL);
8369 if (*is_anonymous)
8370 name = CP_ANONYMOUS_NAMESPACE_STR;
8371
8372 return name;
8373 }
8374
8375 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8376 the user defined type vector. */
8377
8378 static struct type *
8379 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8380 {
8381 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8382 struct comp_unit_head *cu_header = &cu->header;
8383 struct type *type;
8384 struct attribute *attr_byte_size;
8385 struct attribute *attr_address_class;
8386 int byte_size, addr_class;
8387 struct type *target_type;
8388
8389 target_type = die_type (die, cu);
8390
8391 /* The die_type call above may have already set the type for this DIE. */
8392 type = get_die_type (die, cu);
8393 if (type)
8394 return type;
8395
8396 type = lookup_pointer_type (target_type);
8397
8398 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8399 if (attr_byte_size)
8400 byte_size = DW_UNSND (attr_byte_size);
8401 else
8402 byte_size = cu_header->addr_size;
8403
8404 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8405 if (attr_address_class)
8406 addr_class = DW_UNSND (attr_address_class);
8407 else
8408 addr_class = DW_ADDR_none;
8409
8410 /* If the pointer size or address class is different than the
8411 default, create a type variant marked as such and set the
8412 length accordingly. */
8413 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8414 {
8415 if (gdbarch_address_class_type_flags_p (gdbarch))
8416 {
8417 int type_flags;
8418
8419 type_flags = gdbarch_address_class_type_flags
8420 (gdbarch, byte_size, addr_class);
8421 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8422 == 0);
8423 type = make_type_with_address_space (type, type_flags);
8424 }
8425 else if (TYPE_LENGTH (type) != byte_size)
8426 {
8427 complaint (&symfile_complaints,
8428 _("invalid pointer size %d"), byte_size);
8429 }
8430 else
8431 {
8432 /* Should we also complain about unhandled address classes? */
8433 }
8434 }
8435
8436 TYPE_LENGTH (type) = byte_size;
8437 return set_die_type (die, type, cu);
8438 }
8439
8440 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8441 the user defined type vector. */
8442
8443 static struct type *
8444 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8445 {
8446 struct type *type;
8447 struct type *to_type;
8448 struct type *domain;
8449
8450 to_type = die_type (die, cu);
8451 domain = die_containing_type (die, cu);
8452
8453 /* The calls above may have already set the type for this DIE. */
8454 type = get_die_type (die, cu);
8455 if (type)
8456 return type;
8457
8458 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8459 type = lookup_methodptr_type (to_type);
8460 else
8461 type = lookup_memberptr_type (to_type, domain);
8462
8463 return set_die_type (die, type, cu);
8464 }
8465
8466 /* Extract all information from a DW_TAG_reference_type DIE and add to
8467 the user defined type vector. */
8468
8469 static struct type *
8470 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8471 {
8472 struct comp_unit_head *cu_header = &cu->header;
8473 struct type *type, *target_type;
8474 struct attribute *attr;
8475
8476 target_type = die_type (die, cu);
8477
8478 /* The die_type call above may have already set the type for this DIE. */
8479 type = get_die_type (die, cu);
8480 if (type)
8481 return type;
8482
8483 type = lookup_reference_type (target_type);
8484 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8485 if (attr)
8486 {
8487 TYPE_LENGTH (type) = DW_UNSND (attr);
8488 }
8489 else
8490 {
8491 TYPE_LENGTH (type) = cu_header->addr_size;
8492 }
8493 return set_die_type (die, type, cu);
8494 }
8495
8496 static struct type *
8497 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8498 {
8499 struct type *base_type, *cv_type;
8500
8501 base_type = die_type (die, cu);
8502
8503 /* The die_type call above may have already set the type for this DIE. */
8504 cv_type = get_die_type (die, cu);
8505 if (cv_type)
8506 return cv_type;
8507
8508 /* In case the const qualifier is applied to an array type, the element type
8509 is so qualified, not the array type (section 6.7.3 of C99). */
8510 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8511 {
8512 struct type *el_type, *inner_array;
8513
8514 base_type = copy_type (base_type);
8515 inner_array = base_type;
8516
8517 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8518 {
8519 TYPE_TARGET_TYPE (inner_array) =
8520 copy_type (TYPE_TARGET_TYPE (inner_array));
8521 inner_array = TYPE_TARGET_TYPE (inner_array);
8522 }
8523
8524 el_type = TYPE_TARGET_TYPE (inner_array);
8525 TYPE_TARGET_TYPE (inner_array) =
8526 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8527
8528 return set_die_type (die, base_type, cu);
8529 }
8530
8531 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8532 return set_die_type (die, cv_type, cu);
8533 }
8534
8535 static struct type *
8536 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8537 {
8538 struct type *base_type, *cv_type;
8539
8540 base_type = die_type (die, cu);
8541
8542 /* The die_type call above may have already set the type for this DIE. */
8543 cv_type = get_die_type (die, cu);
8544 if (cv_type)
8545 return cv_type;
8546
8547 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8548 return set_die_type (die, cv_type, cu);
8549 }
8550
8551 /* Extract all information from a DW_TAG_string_type DIE and add to
8552 the user defined type vector. It isn't really a user defined type,
8553 but it behaves like one, with other DIE's using an AT_user_def_type
8554 attribute to reference it. */
8555
8556 static struct type *
8557 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8558 {
8559 struct objfile *objfile = cu->objfile;
8560 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8561 struct type *type, *range_type, *index_type, *char_type;
8562 struct attribute *attr;
8563 unsigned int length;
8564
8565 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8566 if (attr)
8567 {
8568 length = DW_UNSND (attr);
8569 }
8570 else
8571 {
8572 /* Check for the DW_AT_byte_size attribute. */
8573 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8574 if (attr)
8575 {
8576 length = DW_UNSND (attr);
8577 }
8578 else
8579 {
8580 length = 1;
8581 }
8582 }
8583
8584 index_type = objfile_type (objfile)->builtin_int;
8585 range_type = create_range_type (NULL, index_type, 1, length);
8586 char_type = language_string_char_type (cu->language_defn, gdbarch);
8587 type = create_string_type (NULL, char_type, range_type);
8588
8589 return set_die_type (die, type, cu);
8590 }
8591
8592 /* Handle DIES due to C code like:
8593
8594 struct foo
8595 {
8596 int (*funcp)(int a, long l);
8597 int b;
8598 };
8599
8600 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8601
8602 static struct type *
8603 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8604 {
8605 struct objfile *objfile = cu->objfile;
8606 struct type *type; /* Type that this function returns. */
8607 struct type *ftype; /* Function that returns above type. */
8608 struct attribute *attr;
8609
8610 type = die_type (die, cu);
8611
8612 /* The die_type call above may have already set the type for this DIE. */
8613 ftype = get_die_type (die, cu);
8614 if (ftype)
8615 return ftype;
8616
8617 ftype = lookup_function_type (type);
8618
8619 /* All functions in C++, Pascal and Java have prototypes. */
8620 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8621 if ((attr && (DW_UNSND (attr) != 0))
8622 || cu->language == language_cplus
8623 || cu->language == language_java
8624 || cu->language == language_pascal)
8625 TYPE_PROTOTYPED (ftype) = 1;
8626 else if (producer_is_realview (cu->producer))
8627 /* RealView does not emit DW_AT_prototyped. We can not
8628 distinguish prototyped and unprototyped functions; default to
8629 prototyped, since that is more common in modern code (and
8630 RealView warns about unprototyped functions). */
8631 TYPE_PROTOTYPED (ftype) = 1;
8632
8633 /* Store the calling convention in the type if it's available in
8634 the subroutine die. Otherwise set the calling convention to
8635 the default value DW_CC_normal. */
8636 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8637 if (attr)
8638 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8639 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8640 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8641 else
8642 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8643
8644 /* We need to add the subroutine type to the die immediately so
8645 we don't infinitely recurse when dealing with parameters
8646 declared as the same subroutine type. */
8647 set_die_type (die, ftype, cu);
8648
8649 if (die->child != NULL)
8650 {
8651 struct type *void_type = objfile_type (objfile)->builtin_void;
8652 struct die_info *child_die;
8653 int nparams, iparams;
8654
8655 /* Count the number of parameters.
8656 FIXME: GDB currently ignores vararg functions, but knows about
8657 vararg member functions. */
8658 nparams = 0;
8659 child_die = die->child;
8660 while (child_die && child_die->tag)
8661 {
8662 if (child_die->tag == DW_TAG_formal_parameter)
8663 nparams++;
8664 else if (child_die->tag == DW_TAG_unspecified_parameters)
8665 TYPE_VARARGS (ftype) = 1;
8666 child_die = sibling_die (child_die);
8667 }
8668
8669 /* Allocate storage for parameters and fill them in. */
8670 TYPE_NFIELDS (ftype) = nparams;
8671 TYPE_FIELDS (ftype) = (struct field *)
8672 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8673
8674 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8675 even if we error out during the parameters reading below. */
8676 for (iparams = 0; iparams < nparams; iparams++)
8677 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8678
8679 iparams = 0;
8680 child_die = die->child;
8681 while (child_die && child_die->tag)
8682 {
8683 if (child_die->tag == DW_TAG_formal_parameter)
8684 {
8685 struct type *arg_type;
8686
8687 /* DWARF version 2 has no clean way to discern C++
8688 static and non-static member functions. G++ helps
8689 GDB by marking the first parameter for non-static
8690 member functions (which is the this pointer) as
8691 artificial. We pass this information to
8692 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8693
8694 DWARF version 3 added DW_AT_object_pointer, which GCC
8695 4.5 does not yet generate. */
8696 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8697 if (attr)
8698 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8699 else
8700 {
8701 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8702
8703 /* GCC/43521: In java, the formal parameter
8704 "this" is sometimes not marked with DW_AT_artificial. */
8705 if (cu->language == language_java)
8706 {
8707 const char *name = dwarf2_name (child_die, cu);
8708
8709 if (name && !strcmp (name, "this"))
8710 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8711 }
8712 }
8713 arg_type = die_type (child_die, cu);
8714
8715 /* RealView does not mark THIS as const, which the testsuite
8716 expects. GCC marks THIS as const in method definitions,
8717 but not in the class specifications (GCC PR 43053). */
8718 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8719 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8720 {
8721 int is_this = 0;
8722 struct dwarf2_cu *arg_cu = cu;
8723 const char *name = dwarf2_name (child_die, cu);
8724
8725 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8726 if (attr)
8727 {
8728 /* If the compiler emits this, use it. */
8729 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8730 is_this = 1;
8731 }
8732 else if (name && strcmp (name, "this") == 0)
8733 /* Function definitions will have the argument names. */
8734 is_this = 1;
8735 else if (name == NULL && iparams == 0)
8736 /* Declarations may not have the names, so like
8737 elsewhere in GDB, assume an artificial first
8738 argument is "this". */
8739 is_this = 1;
8740
8741 if (is_this)
8742 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8743 arg_type, 0);
8744 }
8745
8746 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8747 iparams++;
8748 }
8749 child_die = sibling_die (child_die);
8750 }
8751 }
8752
8753 return ftype;
8754 }
8755
8756 static struct type *
8757 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8758 {
8759 struct objfile *objfile = cu->objfile;
8760 const char *name = NULL;
8761 struct type *this_type, *target_type;
8762
8763 name = dwarf2_full_name (NULL, die, cu);
8764 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8765 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8766 TYPE_NAME (this_type) = (char *) name;
8767 set_die_type (die, this_type, cu);
8768 target_type = die_type (die, cu);
8769 if (target_type != this_type)
8770 TYPE_TARGET_TYPE (this_type) = target_type;
8771 else
8772 {
8773 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8774 spec and cause infinite loops in GDB. */
8775 complaint (&symfile_complaints,
8776 _("Self-referential DW_TAG_typedef "
8777 "- DIE at 0x%x [in module %s]"),
8778 die->offset, objfile->name);
8779 TYPE_TARGET_TYPE (this_type) = NULL;
8780 }
8781 return this_type;
8782 }
8783
8784 /* Find a representation of a given base type and install
8785 it in the TYPE field of the die. */
8786
8787 static struct type *
8788 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8789 {
8790 struct objfile *objfile = cu->objfile;
8791 struct type *type;
8792 struct attribute *attr;
8793 int encoding = 0, size = 0;
8794 char *name;
8795 enum type_code code = TYPE_CODE_INT;
8796 int type_flags = 0;
8797 struct type *target_type = NULL;
8798
8799 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8800 if (attr)
8801 {
8802 encoding = DW_UNSND (attr);
8803 }
8804 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8805 if (attr)
8806 {
8807 size = DW_UNSND (attr);
8808 }
8809 name = dwarf2_name (die, cu);
8810 if (!name)
8811 {
8812 complaint (&symfile_complaints,
8813 _("DW_AT_name missing from DW_TAG_base_type"));
8814 }
8815
8816 switch (encoding)
8817 {
8818 case DW_ATE_address:
8819 /* Turn DW_ATE_address into a void * pointer. */
8820 code = TYPE_CODE_PTR;
8821 type_flags |= TYPE_FLAG_UNSIGNED;
8822 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8823 break;
8824 case DW_ATE_boolean:
8825 code = TYPE_CODE_BOOL;
8826 type_flags |= TYPE_FLAG_UNSIGNED;
8827 break;
8828 case DW_ATE_complex_float:
8829 code = TYPE_CODE_COMPLEX;
8830 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8831 break;
8832 case DW_ATE_decimal_float:
8833 code = TYPE_CODE_DECFLOAT;
8834 break;
8835 case DW_ATE_float:
8836 code = TYPE_CODE_FLT;
8837 break;
8838 case DW_ATE_signed:
8839 break;
8840 case DW_ATE_unsigned:
8841 type_flags |= TYPE_FLAG_UNSIGNED;
8842 if (cu->language == language_fortran
8843 && name
8844 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8845 code = TYPE_CODE_CHAR;
8846 break;
8847 case DW_ATE_signed_char:
8848 if (cu->language == language_ada || cu->language == language_m2
8849 || cu->language == language_pascal
8850 || cu->language == language_fortran)
8851 code = TYPE_CODE_CHAR;
8852 break;
8853 case DW_ATE_unsigned_char:
8854 if (cu->language == language_ada || cu->language == language_m2
8855 || cu->language == language_pascal
8856 || cu->language == language_fortran)
8857 code = TYPE_CODE_CHAR;
8858 type_flags |= TYPE_FLAG_UNSIGNED;
8859 break;
8860 case DW_ATE_UTF:
8861 /* We just treat this as an integer and then recognize the
8862 type by name elsewhere. */
8863 break;
8864
8865 default:
8866 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8867 dwarf_type_encoding_name (encoding));
8868 break;
8869 }
8870
8871 type = init_type (code, size, type_flags, NULL, objfile);
8872 TYPE_NAME (type) = name;
8873 TYPE_TARGET_TYPE (type) = target_type;
8874
8875 if (name && strcmp (name, "char") == 0)
8876 TYPE_NOSIGN (type) = 1;
8877
8878 return set_die_type (die, type, cu);
8879 }
8880
8881 /* Read the given DW_AT_subrange DIE. */
8882
8883 static struct type *
8884 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8885 {
8886 struct type *base_type;
8887 struct type *range_type;
8888 struct attribute *attr;
8889 LONGEST low = 0;
8890 LONGEST high = -1;
8891 char *name;
8892 LONGEST negative_mask;
8893
8894 base_type = die_type (die, cu);
8895 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8896 check_typedef (base_type);
8897
8898 /* The die_type call above may have already set the type for this DIE. */
8899 range_type = get_die_type (die, cu);
8900 if (range_type)
8901 return range_type;
8902
8903 if (cu->language == language_fortran)
8904 {
8905 /* FORTRAN implies a lower bound of 1, if not given. */
8906 low = 1;
8907 }
8908
8909 /* FIXME: For variable sized arrays either of these could be
8910 a variable rather than a constant value. We'll allow it,
8911 but we don't know how to handle it. */
8912 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8913 if (attr)
8914 low = dwarf2_get_attr_constant_value (attr, 0);
8915
8916 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8917 if (attr)
8918 {
8919 if (attr_form_is_block (attr) || is_ref_attr (attr))
8920 {
8921 /* GCC encodes arrays with unspecified or dynamic length
8922 with a DW_FORM_block1 attribute or a reference attribute.
8923 FIXME: GDB does not yet know how to handle dynamic
8924 arrays properly, treat them as arrays with unspecified
8925 length for now.
8926
8927 FIXME: jimb/2003-09-22: GDB does not really know
8928 how to handle arrays of unspecified length
8929 either; we just represent them as zero-length
8930 arrays. Choose an appropriate upper bound given
8931 the lower bound we've computed above. */
8932 high = low - 1;
8933 }
8934 else
8935 high = dwarf2_get_attr_constant_value (attr, 1);
8936 }
8937 else
8938 {
8939 attr = dwarf2_attr (die, DW_AT_count, cu);
8940 if (attr)
8941 {
8942 int count = dwarf2_get_attr_constant_value (attr, 1);
8943 high = low + count - 1;
8944 }
8945 else
8946 {
8947 /* Unspecified array length. */
8948 high = low - 1;
8949 }
8950 }
8951
8952 /* Dwarf-2 specifications explicitly allows to create subrange types
8953 without specifying a base type.
8954 In that case, the base type must be set to the type of
8955 the lower bound, upper bound or count, in that order, if any of these
8956 three attributes references an object that has a type.
8957 If no base type is found, the Dwarf-2 specifications say that
8958 a signed integer type of size equal to the size of an address should
8959 be used.
8960 For the following C code: `extern char gdb_int [];'
8961 GCC produces an empty range DIE.
8962 FIXME: muller/2010-05-28: Possible references to object for low bound,
8963 high bound or count are not yet handled by this code. */
8964 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8965 {
8966 struct objfile *objfile = cu->objfile;
8967 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8968 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8969 struct type *int_type = objfile_type (objfile)->builtin_int;
8970
8971 /* Test "int", "long int", and "long long int" objfile types,
8972 and select the first one having a size above or equal to the
8973 architecture address size. */
8974 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8975 base_type = int_type;
8976 else
8977 {
8978 int_type = objfile_type (objfile)->builtin_long;
8979 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8980 base_type = int_type;
8981 else
8982 {
8983 int_type = objfile_type (objfile)->builtin_long_long;
8984 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8985 base_type = int_type;
8986 }
8987 }
8988 }
8989
8990 negative_mask =
8991 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8992 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8993 low |= negative_mask;
8994 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8995 high |= negative_mask;
8996
8997 range_type = create_range_type (NULL, base_type, low, high);
8998
8999 /* Mark arrays with dynamic length at least as an array of unspecified
9000 length. GDB could check the boundary but before it gets implemented at
9001 least allow accessing the array elements. */
9002 if (attr && attr_form_is_block (attr))
9003 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9004
9005 /* Ada expects an empty array on no boundary attributes. */
9006 if (attr == NULL && cu->language != language_ada)
9007 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9008
9009 name = dwarf2_name (die, cu);
9010 if (name)
9011 TYPE_NAME (range_type) = name;
9012
9013 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9014 if (attr)
9015 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9016
9017 set_die_type (die, range_type, cu);
9018
9019 /* set_die_type should be already done. */
9020 set_descriptive_type (range_type, die, cu);
9021
9022 return range_type;
9023 }
9024
9025 static struct type *
9026 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9027 {
9028 struct type *type;
9029
9030 /* For now, we only support the C meaning of an unspecified type: void. */
9031
9032 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9033 TYPE_NAME (type) = dwarf2_name (die, cu);
9034
9035 return set_die_type (die, type, cu);
9036 }
9037
9038 /* Trivial hash function for die_info: the hash value of a DIE
9039 is its offset in .debug_info for this objfile. */
9040
9041 static hashval_t
9042 die_hash (const void *item)
9043 {
9044 const struct die_info *die = item;
9045
9046 return die->offset;
9047 }
9048
9049 /* Trivial comparison function for die_info structures: two DIEs
9050 are equal if they have the same offset. */
9051
9052 static int
9053 die_eq (const void *item_lhs, const void *item_rhs)
9054 {
9055 const struct die_info *die_lhs = item_lhs;
9056 const struct die_info *die_rhs = item_rhs;
9057
9058 return die_lhs->offset == die_rhs->offset;
9059 }
9060
9061 /* Read a whole compilation unit into a linked list of dies. */
9062
9063 static struct die_info *
9064 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9065 {
9066 struct die_reader_specs reader_specs;
9067 int read_abbrevs = 0;
9068 struct cleanup *back_to = NULL;
9069 struct die_info *die;
9070
9071 if (cu->dwarf2_abbrevs == NULL)
9072 {
9073 dwarf2_read_abbrevs (cu);
9074 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9075 read_abbrevs = 1;
9076 }
9077
9078 gdb_assert (cu->die_hash == NULL);
9079 cu->die_hash
9080 = htab_create_alloc_ex (cu->header.length / 12,
9081 die_hash,
9082 die_eq,
9083 NULL,
9084 &cu->comp_unit_obstack,
9085 hashtab_obstack_allocate,
9086 dummy_obstack_deallocate);
9087
9088 init_cu_die_reader (&reader_specs, cu);
9089
9090 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9091
9092 if (read_abbrevs)
9093 do_cleanups (back_to);
9094
9095 return die;
9096 }
9097
9098 /* Main entry point for reading a DIE and all children.
9099 Read the DIE and dump it if requested. */
9100
9101 static struct die_info *
9102 read_die_and_children (const struct die_reader_specs *reader,
9103 gdb_byte *info_ptr,
9104 gdb_byte **new_info_ptr,
9105 struct die_info *parent)
9106 {
9107 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9108 new_info_ptr, parent);
9109
9110 if (dwarf2_die_debug)
9111 {
9112 fprintf_unfiltered (gdb_stdlog,
9113 "\nRead die from %s of %s:\n",
9114 (reader->cu->per_cu->debug_types_section
9115 ? ".debug_types"
9116 : ".debug_info"),
9117 reader->abfd->filename);
9118 dump_die (result, dwarf2_die_debug);
9119 }
9120
9121 return result;
9122 }
9123
9124 /* Read a single die and all its descendents. Set the die's sibling
9125 field to NULL; set other fields in the die correctly, and set all
9126 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9127 location of the info_ptr after reading all of those dies. PARENT
9128 is the parent of the die in question. */
9129
9130 static struct die_info *
9131 read_die_and_children_1 (const struct die_reader_specs *reader,
9132 gdb_byte *info_ptr,
9133 gdb_byte **new_info_ptr,
9134 struct die_info *parent)
9135 {
9136 struct die_info *die;
9137 gdb_byte *cur_ptr;
9138 int has_children;
9139
9140 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9141 if (die == NULL)
9142 {
9143 *new_info_ptr = cur_ptr;
9144 return NULL;
9145 }
9146 store_in_ref_table (die, reader->cu);
9147
9148 if (has_children)
9149 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9150 else
9151 {
9152 die->child = NULL;
9153 *new_info_ptr = cur_ptr;
9154 }
9155
9156 die->sibling = NULL;
9157 die->parent = parent;
9158 return die;
9159 }
9160
9161 /* Read a die, all of its descendents, and all of its siblings; set
9162 all of the fields of all of the dies correctly. Arguments are as
9163 in read_die_and_children. */
9164
9165 static struct die_info *
9166 read_die_and_siblings (const struct die_reader_specs *reader,
9167 gdb_byte *info_ptr,
9168 gdb_byte **new_info_ptr,
9169 struct die_info *parent)
9170 {
9171 struct die_info *first_die, *last_sibling;
9172 gdb_byte *cur_ptr;
9173
9174 cur_ptr = info_ptr;
9175 first_die = last_sibling = NULL;
9176
9177 while (1)
9178 {
9179 struct die_info *die
9180 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9181
9182 if (die == NULL)
9183 {
9184 *new_info_ptr = cur_ptr;
9185 return first_die;
9186 }
9187
9188 if (!first_die)
9189 first_die = die;
9190 else
9191 last_sibling->sibling = die;
9192
9193 last_sibling = die;
9194 }
9195 }
9196
9197 /* Read the die from the .debug_info section buffer. Set DIEP to
9198 point to a newly allocated die with its information, except for its
9199 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9200 whether the die has children or not. */
9201
9202 static gdb_byte *
9203 read_full_die (const struct die_reader_specs *reader,
9204 struct die_info **diep, gdb_byte *info_ptr,
9205 int *has_children)
9206 {
9207 unsigned int abbrev_number, bytes_read, i, offset;
9208 struct abbrev_info *abbrev;
9209 struct die_info *die;
9210 struct dwarf2_cu *cu = reader->cu;
9211 bfd *abfd = reader->abfd;
9212
9213 offset = info_ptr - reader->buffer;
9214 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9215 info_ptr += bytes_read;
9216 if (!abbrev_number)
9217 {
9218 *diep = NULL;
9219 *has_children = 0;
9220 return info_ptr;
9221 }
9222
9223 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9224 if (!abbrev)
9225 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9226 abbrev_number,
9227 bfd_get_filename (abfd));
9228
9229 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9230 die->offset = offset;
9231 die->tag = abbrev->tag;
9232 die->abbrev = abbrev_number;
9233
9234 die->num_attrs = abbrev->num_attrs;
9235
9236 for (i = 0; i < abbrev->num_attrs; ++i)
9237 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9238 abfd, info_ptr, cu);
9239
9240 *diep = die;
9241 *has_children = abbrev->has_children;
9242 return info_ptr;
9243 }
9244
9245 /* In DWARF version 2, the description of the debugging information is
9246 stored in a separate .debug_abbrev section. Before we read any
9247 dies from a section we read in all abbreviations and install them
9248 in a hash table. This function also sets flags in CU describing
9249 the data found in the abbrev table. */
9250
9251 static void
9252 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9253 {
9254 bfd *abfd = cu->objfile->obfd;
9255 struct comp_unit_head *cu_header = &cu->header;
9256 gdb_byte *abbrev_ptr;
9257 struct abbrev_info *cur_abbrev;
9258 unsigned int abbrev_number, bytes_read, abbrev_name;
9259 unsigned int abbrev_form, hash_number;
9260 struct attr_abbrev *cur_attrs;
9261 unsigned int allocated_attrs;
9262
9263 /* Initialize dwarf2 abbrevs. */
9264 obstack_init (&cu->abbrev_obstack);
9265 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9266 (ABBREV_HASH_SIZE
9267 * sizeof (struct abbrev_info *)));
9268 memset (cu->dwarf2_abbrevs, 0,
9269 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9270
9271 dwarf2_read_section (dwarf2_per_objfile->objfile,
9272 &dwarf2_per_objfile->abbrev);
9273 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9274 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9275 abbrev_ptr += bytes_read;
9276
9277 allocated_attrs = ATTR_ALLOC_CHUNK;
9278 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9279
9280 /* Loop until we reach an abbrev number of 0. */
9281 while (abbrev_number)
9282 {
9283 cur_abbrev = dwarf_alloc_abbrev (cu);
9284
9285 /* read in abbrev header */
9286 cur_abbrev->number = abbrev_number;
9287 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9288 abbrev_ptr += bytes_read;
9289 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9290 abbrev_ptr += 1;
9291
9292 if (cur_abbrev->tag == DW_TAG_namespace)
9293 cu->has_namespace_info = 1;
9294
9295 /* now read in declarations */
9296 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9297 abbrev_ptr += bytes_read;
9298 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9299 abbrev_ptr += bytes_read;
9300 while (abbrev_name)
9301 {
9302 if (cur_abbrev->num_attrs == allocated_attrs)
9303 {
9304 allocated_attrs += ATTR_ALLOC_CHUNK;
9305 cur_attrs
9306 = xrealloc (cur_attrs, (allocated_attrs
9307 * sizeof (struct attr_abbrev)));
9308 }
9309
9310 /* Record whether this compilation unit might have
9311 inter-compilation-unit references. If we don't know what form
9312 this attribute will have, then it might potentially be a
9313 DW_FORM_ref_addr, so we conservatively expect inter-CU
9314 references. */
9315
9316 if (abbrev_form == DW_FORM_ref_addr
9317 || abbrev_form == DW_FORM_indirect)
9318 cu->has_form_ref_addr = 1;
9319
9320 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9321 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9322 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9323 abbrev_ptr += bytes_read;
9324 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9325 abbrev_ptr += bytes_read;
9326 }
9327
9328 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9329 (cur_abbrev->num_attrs
9330 * sizeof (struct attr_abbrev)));
9331 memcpy (cur_abbrev->attrs, cur_attrs,
9332 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9333
9334 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9335 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9336 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9337
9338 /* Get next abbreviation.
9339 Under Irix6 the abbreviations for a compilation unit are not
9340 always properly terminated with an abbrev number of 0.
9341 Exit loop if we encounter an abbreviation which we have
9342 already read (which means we are about to read the abbreviations
9343 for the next compile unit) or if the end of the abbreviation
9344 table is reached. */
9345 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9346 >= dwarf2_per_objfile->abbrev.size)
9347 break;
9348 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9349 abbrev_ptr += bytes_read;
9350 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9351 break;
9352 }
9353
9354 xfree (cur_attrs);
9355 }
9356
9357 /* Release the memory used by the abbrev table for a compilation unit. */
9358
9359 static void
9360 dwarf2_free_abbrev_table (void *ptr_to_cu)
9361 {
9362 struct dwarf2_cu *cu = ptr_to_cu;
9363
9364 obstack_free (&cu->abbrev_obstack, NULL);
9365 cu->dwarf2_abbrevs = NULL;
9366 }
9367
9368 /* Lookup an abbrev_info structure in the abbrev hash table. */
9369
9370 static struct abbrev_info *
9371 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9372 {
9373 unsigned int hash_number;
9374 struct abbrev_info *abbrev;
9375
9376 hash_number = number % ABBREV_HASH_SIZE;
9377 abbrev = cu->dwarf2_abbrevs[hash_number];
9378
9379 while (abbrev)
9380 {
9381 if (abbrev->number == number)
9382 return abbrev;
9383 else
9384 abbrev = abbrev->next;
9385 }
9386 return NULL;
9387 }
9388
9389 /* Returns nonzero if TAG represents a type that we might generate a partial
9390 symbol for. */
9391
9392 static int
9393 is_type_tag_for_partial (int tag)
9394 {
9395 switch (tag)
9396 {
9397 #if 0
9398 /* Some types that would be reasonable to generate partial symbols for,
9399 that we don't at present. */
9400 case DW_TAG_array_type:
9401 case DW_TAG_file_type:
9402 case DW_TAG_ptr_to_member_type:
9403 case DW_TAG_set_type:
9404 case DW_TAG_string_type:
9405 case DW_TAG_subroutine_type:
9406 #endif
9407 case DW_TAG_base_type:
9408 case DW_TAG_class_type:
9409 case DW_TAG_interface_type:
9410 case DW_TAG_enumeration_type:
9411 case DW_TAG_structure_type:
9412 case DW_TAG_subrange_type:
9413 case DW_TAG_typedef:
9414 case DW_TAG_union_type:
9415 return 1;
9416 default:
9417 return 0;
9418 }
9419 }
9420
9421 /* Load all DIEs that are interesting for partial symbols into memory. */
9422
9423 static struct partial_die_info *
9424 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9425 int building_psymtab, struct dwarf2_cu *cu)
9426 {
9427 struct objfile *objfile = cu->objfile;
9428 struct partial_die_info *part_die;
9429 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9430 struct abbrev_info *abbrev;
9431 unsigned int bytes_read;
9432 unsigned int load_all = 0;
9433
9434 int nesting_level = 1;
9435
9436 parent_die = NULL;
9437 last_die = NULL;
9438
9439 if (cu->per_cu && cu->per_cu->load_all_dies)
9440 load_all = 1;
9441
9442 cu->partial_dies
9443 = htab_create_alloc_ex (cu->header.length / 12,
9444 partial_die_hash,
9445 partial_die_eq,
9446 NULL,
9447 &cu->comp_unit_obstack,
9448 hashtab_obstack_allocate,
9449 dummy_obstack_deallocate);
9450
9451 part_die = obstack_alloc (&cu->comp_unit_obstack,
9452 sizeof (struct partial_die_info));
9453
9454 while (1)
9455 {
9456 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9457
9458 /* A NULL abbrev means the end of a series of children. */
9459 if (abbrev == NULL)
9460 {
9461 if (--nesting_level == 0)
9462 {
9463 /* PART_DIE was probably the last thing allocated on the
9464 comp_unit_obstack, so we could call obstack_free
9465 here. We don't do that because the waste is small,
9466 and will be cleaned up when we're done with this
9467 compilation unit. This way, we're also more robust
9468 against other users of the comp_unit_obstack. */
9469 return first_die;
9470 }
9471 info_ptr += bytes_read;
9472 last_die = parent_die;
9473 parent_die = parent_die->die_parent;
9474 continue;
9475 }
9476
9477 /* Check for template arguments. We never save these; if
9478 they're seen, we just mark the parent, and go on our way. */
9479 if (parent_die != NULL
9480 && cu->language == language_cplus
9481 && (abbrev->tag == DW_TAG_template_type_param
9482 || abbrev->tag == DW_TAG_template_value_param))
9483 {
9484 parent_die->has_template_arguments = 1;
9485
9486 if (!load_all)
9487 {
9488 /* We don't need a partial DIE for the template argument. */
9489 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9490 cu);
9491 continue;
9492 }
9493 }
9494
9495 /* We only recurse into subprograms looking for template arguments.
9496 Skip their other children. */
9497 if (!load_all
9498 && cu->language == language_cplus
9499 && parent_die != NULL
9500 && parent_die->tag == DW_TAG_subprogram)
9501 {
9502 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9503 continue;
9504 }
9505
9506 /* Check whether this DIE is interesting enough to save. Normally
9507 we would not be interested in members here, but there may be
9508 later variables referencing them via DW_AT_specification (for
9509 static members). */
9510 if (!load_all
9511 && !is_type_tag_for_partial (abbrev->tag)
9512 && abbrev->tag != DW_TAG_constant
9513 && abbrev->tag != DW_TAG_enumerator
9514 && abbrev->tag != DW_TAG_subprogram
9515 && abbrev->tag != DW_TAG_lexical_block
9516 && abbrev->tag != DW_TAG_variable
9517 && abbrev->tag != DW_TAG_namespace
9518 && abbrev->tag != DW_TAG_module
9519 && abbrev->tag != DW_TAG_member)
9520 {
9521 /* Otherwise we skip to the next sibling, if any. */
9522 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9523 continue;
9524 }
9525
9526 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9527 buffer, info_ptr, cu);
9528
9529 /* This two-pass algorithm for processing partial symbols has a
9530 high cost in cache pressure. Thus, handle some simple cases
9531 here which cover the majority of C partial symbols. DIEs
9532 which neither have specification tags in them, nor could have
9533 specification tags elsewhere pointing at them, can simply be
9534 processed and discarded.
9535
9536 This segment is also optional; scan_partial_symbols and
9537 add_partial_symbol will handle these DIEs if we chain
9538 them in normally. When compilers which do not emit large
9539 quantities of duplicate debug information are more common,
9540 this code can probably be removed. */
9541
9542 /* Any complete simple types at the top level (pretty much all
9543 of them, for a language without namespaces), can be processed
9544 directly. */
9545 if (parent_die == NULL
9546 && part_die->has_specification == 0
9547 && part_die->is_declaration == 0
9548 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9549 || part_die->tag == DW_TAG_base_type
9550 || part_die->tag == DW_TAG_subrange_type))
9551 {
9552 if (building_psymtab && part_die->name != NULL)
9553 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9554 VAR_DOMAIN, LOC_TYPEDEF,
9555 &objfile->static_psymbols,
9556 0, (CORE_ADDR) 0, cu->language, objfile);
9557 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9558 continue;
9559 }
9560
9561 /* The exception for DW_TAG_typedef with has_children above is
9562 a workaround of GCC PR debug/47510. In the case of this complaint
9563 type_name_no_tag_or_error will error on such types later.
9564
9565 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9566 it could not find the child DIEs referenced later, this is checked
9567 above. In correct DWARF DW_TAG_typedef should have no children. */
9568
9569 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9570 complaint (&symfile_complaints,
9571 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9572 "- DIE at 0x%x [in module %s]"),
9573 part_die->offset, objfile->name);
9574
9575 /* If we're at the second level, and we're an enumerator, and
9576 our parent has no specification (meaning possibly lives in a
9577 namespace elsewhere), then we can add the partial symbol now
9578 instead of queueing it. */
9579 if (part_die->tag == DW_TAG_enumerator
9580 && parent_die != NULL
9581 && parent_die->die_parent == NULL
9582 && parent_die->tag == DW_TAG_enumeration_type
9583 && parent_die->has_specification == 0)
9584 {
9585 if (part_die->name == NULL)
9586 complaint (&symfile_complaints,
9587 _("malformed enumerator DIE ignored"));
9588 else if (building_psymtab)
9589 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9590 VAR_DOMAIN, LOC_CONST,
9591 (cu->language == language_cplus
9592 || cu->language == language_java)
9593 ? &objfile->global_psymbols
9594 : &objfile->static_psymbols,
9595 0, (CORE_ADDR) 0, cu->language, objfile);
9596
9597 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9598 continue;
9599 }
9600
9601 /* We'll save this DIE so link it in. */
9602 part_die->die_parent = parent_die;
9603 part_die->die_sibling = NULL;
9604 part_die->die_child = NULL;
9605
9606 if (last_die && last_die == parent_die)
9607 last_die->die_child = part_die;
9608 else if (last_die)
9609 last_die->die_sibling = part_die;
9610
9611 last_die = part_die;
9612
9613 if (first_die == NULL)
9614 first_die = part_die;
9615
9616 /* Maybe add the DIE to the hash table. Not all DIEs that we
9617 find interesting need to be in the hash table, because we
9618 also have the parent/sibling/child chains; only those that we
9619 might refer to by offset later during partial symbol reading.
9620
9621 For now this means things that might have be the target of a
9622 DW_AT_specification, DW_AT_abstract_origin, or
9623 DW_AT_extension. DW_AT_extension will refer only to
9624 namespaces; DW_AT_abstract_origin refers to functions (and
9625 many things under the function DIE, but we do not recurse
9626 into function DIEs during partial symbol reading) and
9627 possibly variables as well; DW_AT_specification refers to
9628 declarations. Declarations ought to have the DW_AT_declaration
9629 flag. It happens that GCC forgets to put it in sometimes, but
9630 only for functions, not for types.
9631
9632 Adding more things than necessary to the hash table is harmless
9633 except for the performance cost. Adding too few will result in
9634 wasted time in find_partial_die, when we reread the compilation
9635 unit with load_all_dies set. */
9636
9637 if (load_all
9638 || abbrev->tag == DW_TAG_constant
9639 || abbrev->tag == DW_TAG_subprogram
9640 || abbrev->tag == DW_TAG_variable
9641 || abbrev->tag == DW_TAG_namespace
9642 || part_die->is_declaration)
9643 {
9644 void **slot;
9645
9646 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9647 part_die->offset, INSERT);
9648 *slot = part_die;
9649 }
9650
9651 part_die = obstack_alloc (&cu->comp_unit_obstack,
9652 sizeof (struct partial_die_info));
9653
9654 /* For some DIEs we want to follow their children (if any). For C
9655 we have no reason to follow the children of structures; for other
9656 languages we have to, so that we can get at method physnames
9657 to infer fully qualified class names, for DW_AT_specification,
9658 and for C++ template arguments. For C++, we also look one level
9659 inside functions to find template arguments (if the name of the
9660 function does not already contain the template arguments).
9661
9662 For Ada, we need to scan the children of subprograms and lexical
9663 blocks as well because Ada allows the definition of nested
9664 entities that could be interesting for the debugger, such as
9665 nested subprograms for instance. */
9666 if (last_die->has_children
9667 && (load_all
9668 || last_die->tag == DW_TAG_namespace
9669 || last_die->tag == DW_TAG_module
9670 || last_die->tag == DW_TAG_enumeration_type
9671 || (cu->language == language_cplus
9672 && last_die->tag == DW_TAG_subprogram
9673 && (last_die->name == NULL
9674 || strchr (last_die->name, '<') == NULL))
9675 || (cu->language != language_c
9676 && (last_die->tag == DW_TAG_class_type
9677 || last_die->tag == DW_TAG_interface_type
9678 || last_die->tag == DW_TAG_structure_type
9679 || last_die->tag == DW_TAG_union_type))
9680 || (cu->language == language_ada
9681 && (last_die->tag == DW_TAG_subprogram
9682 || last_die->tag == DW_TAG_lexical_block))))
9683 {
9684 nesting_level++;
9685 parent_die = last_die;
9686 continue;
9687 }
9688
9689 /* Otherwise we skip to the next sibling, if any. */
9690 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9691
9692 /* Back to the top, do it again. */
9693 }
9694 }
9695
9696 /* Read a minimal amount of information into the minimal die structure. */
9697
9698 static gdb_byte *
9699 read_partial_die (struct partial_die_info *part_die,
9700 struct abbrev_info *abbrev,
9701 unsigned int abbrev_len, bfd *abfd,
9702 gdb_byte *buffer, gdb_byte *info_ptr,
9703 struct dwarf2_cu *cu)
9704 {
9705 struct objfile *objfile = cu->objfile;
9706 unsigned int i;
9707 struct attribute attr;
9708 int has_low_pc_attr = 0;
9709 int has_high_pc_attr = 0;
9710
9711 memset (part_die, 0, sizeof (struct partial_die_info));
9712
9713 part_die->offset = info_ptr - buffer;
9714
9715 info_ptr += abbrev_len;
9716
9717 if (abbrev == NULL)
9718 return info_ptr;
9719
9720 part_die->tag = abbrev->tag;
9721 part_die->has_children = abbrev->has_children;
9722
9723 for (i = 0; i < abbrev->num_attrs; ++i)
9724 {
9725 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9726
9727 /* Store the data if it is of an attribute we want to keep in a
9728 partial symbol table. */
9729 switch (attr.name)
9730 {
9731 case DW_AT_name:
9732 switch (part_die->tag)
9733 {
9734 case DW_TAG_compile_unit:
9735 case DW_TAG_type_unit:
9736 /* Compilation units have a DW_AT_name that is a filename, not
9737 a source language identifier. */
9738 case DW_TAG_enumeration_type:
9739 case DW_TAG_enumerator:
9740 /* These tags always have simple identifiers already; no need
9741 to canonicalize them. */
9742 part_die->name = DW_STRING (&attr);
9743 break;
9744 default:
9745 part_die->name
9746 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9747 &objfile->objfile_obstack);
9748 break;
9749 }
9750 break;
9751 case DW_AT_linkage_name:
9752 case DW_AT_MIPS_linkage_name:
9753 /* Note that both forms of linkage name might appear. We
9754 assume they will be the same, and we only store the last
9755 one we see. */
9756 if (cu->language == language_ada)
9757 part_die->name = DW_STRING (&attr);
9758 part_die->linkage_name = DW_STRING (&attr);
9759 break;
9760 case DW_AT_low_pc:
9761 has_low_pc_attr = 1;
9762 part_die->lowpc = DW_ADDR (&attr);
9763 break;
9764 case DW_AT_high_pc:
9765 has_high_pc_attr = 1;
9766 part_die->highpc = DW_ADDR (&attr);
9767 break;
9768 case DW_AT_location:
9769 /* Support the .debug_loc offsets. */
9770 if (attr_form_is_block (&attr))
9771 {
9772 part_die->locdesc = DW_BLOCK (&attr);
9773 }
9774 else if (attr_form_is_section_offset (&attr))
9775 {
9776 dwarf2_complex_location_expr_complaint ();
9777 }
9778 else
9779 {
9780 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9781 "partial symbol information");
9782 }
9783 break;
9784 case DW_AT_external:
9785 part_die->is_external = DW_UNSND (&attr);
9786 break;
9787 case DW_AT_declaration:
9788 part_die->is_declaration = DW_UNSND (&attr);
9789 break;
9790 case DW_AT_type:
9791 part_die->has_type = 1;
9792 break;
9793 case DW_AT_abstract_origin:
9794 case DW_AT_specification:
9795 case DW_AT_extension:
9796 part_die->has_specification = 1;
9797 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9798 break;
9799 case DW_AT_sibling:
9800 /* Ignore absolute siblings, they might point outside of
9801 the current compile unit. */
9802 if (attr.form == DW_FORM_ref_addr)
9803 complaint (&symfile_complaints,
9804 _("ignoring absolute DW_AT_sibling"));
9805 else
9806 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9807 break;
9808 case DW_AT_byte_size:
9809 part_die->has_byte_size = 1;
9810 break;
9811 case DW_AT_calling_convention:
9812 /* DWARF doesn't provide a way to identify a program's source-level
9813 entry point. DW_AT_calling_convention attributes are only meant
9814 to describe functions' calling conventions.
9815
9816 However, because it's a necessary piece of information in
9817 Fortran, and because DW_CC_program is the only piece of debugging
9818 information whose definition refers to a 'main program' at all,
9819 several compilers have begun marking Fortran main programs with
9820 DW_CC_program --- even when those functions use the standard
9821 calling conventions.
9822
9823 So until DWARF specifies a way to provide this information and
9824 compilers pick up the new representation, we'll support this
9825 practice. */
9826 if (DW_UNSND (&attr) == DW_CC_program
9827 && cu->language == language_fortran)
9828 {
9829 set_main_name (part_die->name);
9830
9831 /* As this DIE has a static linkage the name would be difficult
9832 to look up later. */
9833 language_of_main = language_fortran;
9834 }
9835 break;
9836 default:
9837 break;
9838 }
9839 }
9840
9841 if (has_low_pc_attr && has_high_pc_attr)
9842 {
9843 /* When using the GNU linker, .gnu.linkonce. sections are used to
9844 eliminate duplicate copies of functions and vtables and such.
9845 The linker will arbitrarily choose one and discard the others.
9846 The AT_*_pc values for such functions refer to local labels in
9847 these sections. If the section from that file was discarded, the
9848 labels are not in the output, so the relocs get a value of 0.
9849 If this is a discarded function, mark the pc bounds as invalid,
9850 so that GDB will ignore it. */
9851 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9852 {
9853 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9854
9855 complaint (&symfile_complaints,
9856 _("DW_AT_low_pc %s is zero "
9857 "for DIE at 0x%x [in module %s]"),
9858 paddress (gdbarch, part_die->lowpc),
9859 part_die->offset, objfile->name);
9860 }
9861 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9862 else if (part_die->lowpc >= part_die->highpc)
9863 {
9864 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9865
9866 complaint (&symfile_complaints,
9867 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9868 "for DIE at 0x%x [in module %s]"),
9869 paddress (gdbarch, part_die->lowpc),
9870 paddress (gdbarch, part_die->highpc),
9871 part_die->offset, objfile->name);
9872 }
9873 else
9874 part_die->has_pc_info = 1;
9875 }
9876
9877 return info_ptr;
9878 }
9879
9880 /* Find a cached partial DIE at OFFSET in CU. */
9881
9882 static struct partial_die_info *
9883 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9884 {
9885 struct partial_die_info *lookup_die = NULL;
9886 struct partial_die_info part_die;
9887
9888 part_die.offset = offset;
9889 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9890
9891 return lookup_die;
9892 }
9893
9894 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9895 except in the case of .debug_types DIEs which do not reference
9896 outside their CU (they do however referencing other types via
9897 DW_FORM_ref_sig8). */
9898
9899 static struct partial_die_info *
9900 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9901 {
9902 struct objfile *objfile = cu->objfile;
9903 struct dwarf2_per_cu_data *per_cu = NULL;
9904 struct partial_die_info *pd = NULL;
9905
9906 if (cu->per_cu->debug_types_section)
9907 {
9908 pd = find_partial_die_in_comp_unit (offset, cu);
9909 if (pd != NULL)
9910 return pd;
9911 goto not_found;
9912 }
9913
9914 if (offset_in_cu_p (&cu->header, offset))
9915 {
9916 pd = find_partial_die_in_comp_unit (offset, cu);
9917 if (pd != NULL)
9918 return pd;
9919 }
9920
9921 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9922
9923 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9924 load_partial_comp_unit (per_cu);
9925
9926 per_cu->cu->last_used = 0;
9927 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9928
9929 if (pd == NULL && per_cu->load_all_dies == 0)
9930 {
9931 struct cleanup *back_to;
9932 struct partial_die_info comp_unit_die;
9933 struct abbrev_info *abbrev;
9934 unsigned int bytes_read;
9935 char *info_ptr;
9936
9937 per_cu->load_all_dies = 1;
9938
9939 /* Re-read the DIEs. */
9940 back_to = make_cleanup (null_cleanup, 0);
9941 if (per_cu->cu->dwarf2_abbrevs == NULL)
9942 {
9943 dwarf2_read_abbrevs (per_cu->cu);
9944 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9945 }
9946 info_ptr = (dwarf2_per_objfile->info.buffer
9947 + per_cu->cu->header.offset
9948 + per_cu->cu->header.first_die_offset);
9949 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9950 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9951 objfile->obfd,
9952 dwarf2_per_objfile->info.buffer, info_ptr,
9953 per_cu->cu);
9954 if (comp_unit_die.has_children)
9955 load_partial_dies (objfile->obfd,
9956 dwarf2_per_objfile->info.buffer, info_ptr,
9957 0, per_cu->cu);
9958 do_cleanups (back_to);
9959
9960 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9961 }
9962
9963 not_found:
9964
9965 if (pd == NULL)
9966 internal_error (__FILE__, __LINE__,
9967 _("could not find partial DIE 0x%x "
9968 "in cache [from module %s]\n"),
9969 offset, bfd_get_filename (objfile->obfd));
9970 return pd;
9971 }
9972
9973 /* See if we can figure out if the class lives in a namespace. We do
9974 this by looking for a member function; its demangled name will
9975 contain namespace info, if there is any. */
9976
9977 static void
9978 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9979 struct dwarf2_cu *cu)
9980 {
9981 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9982 what template types look like, because the demangler
9983 frequently doesn't give the same name as the debug info. We
9984 could fix this by only using the demangled name to get the
9985 prefix (but see comment in read_structure_type). */
9986
9987 struct partial_die_info *real_pdi;
9988 struct partial_die_info *child_pdi;
9989
9990 /* If this DIE (this DIE's specification, if any) has a parent, then
9991 we should not do this. We'll prepend the parent's fully qualified
9992 name when we create the partial symbol. */
9993
9994 real_pdi = struct_pdi;
9995 while (real_pdi->has_specification)
9996 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9997
9998 if (real_pdi->die_parent != NULL)
9999 return;
10000
10001 for (child_pdi = struct_pdi->die_child;
10002 child_pdi != NULL;
10003 child_pdi = child_pdi->die_sibling)
10004 {
10005 if (child_pdi->tag == DW_TAG_subprogram
10006 && child_pdi->linkage_name != NULL)
10007 {
10008 char *actual_class_name
10009 = language_class_name_from_physname (cu->language_defn,
10010 child_pdi->linkage_name);
10011 if (actual_class_name != NULL)
10012 {
10013 struct_pdi->name
10014 = obsavestring (actual_class_name,
10015 strlen (actual_class_name),
10016 &cu->objfile->objfile_obstack);
10017 xfree (actual_class_name);
10018 }
10019 break;
10020 }
10021 }
10022 }
10023
10024 /* Adjust PART_DIE before generating a symbol for it. This function
10025 may set the is_external flag or change the DIE's name. */
10026
10027 static void
10028 fixup_partial_die (struct partial_die_info *part_die,
10029 struct dwarf2_cu *cu)
10030 {
10031 /* Once we've fixed up a die, there's no point in doing so again.
10032 This also avoids a memory leak if we were to call
10033 guess_partial_die_structure_name multiple times. */
10034 if (part_die->fixup_called)
10035 return;
10036
10037 /* If we found a reference attribute and the DIE has no name, try
10038 to find a name in the referred to DIE. */
10039
10040 if (part_die->name == NULL && part_die->has_specification)
10041 {
10042 struct partial_die_info *spec_die;
10043
10044 spec_die = find_partial_die (part_die->spec_offset, cu);
10045
10046 fixup_partial_die (spec_die, cu);
10047
10048 if (spec_die->name)
10049 {
10050 part_die->name = spec_die->name;
10051
10052 /* Copy DW_AT_external attribute if it is set. */
10053 if (spec_die->is_external)
10054 part_die->is_external = spec_die->is_external;
10055 }
10056 }
10057
10058 /* Set default names for some unnamed DIEs. */
10059
10060 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10061 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10062
10063 /* If there is no parent die to provide a namespace, and there are
10064 children, see if we can determine the namespace from their linkage
10065 name.
10066 NOTE: We need to do this even if cu->has_namespace_info != 0.
10067 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10068 if (cu->language == language_cplus
10069 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10070 && part_die->die_parent == NULL
10071 && part_die->has_children
10072 && (part_die->tag == DW_TAG_class_type
10073 || part_die->tag == DW_TAG_structure_type
10074 || part_die->tag == DW_TAG_union_type))
10075 guess_partial_die_structure_name (part_die, cu);
10076
10077 /* GCC might emit a nameless struct or union that has a linkage
10078 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10079 if (part_die->name == NULL
10080 && (part_die->tag == DW_TAG_class_type
10081 || part_die->tag == DW_TAG_interface_type
10082 || part_die->tag == DW_TAG_structure_type
10083 || part_die->tag == DW_TAG_union_type)
10084 && part_die->linkage_name != NULL)
10085 {
10086 char *demangled;
10087
10088 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10089 if (demangled)
10090 {
10091 const char *base;
10092
10093 /* Strip any leading namespaces/classes, keep only the base name.
10094 DW_AT_name for named DIEs does not contain the prefixes. */
10095 base = strrchr (demangled, ':');
10096 if (base && base > demangled && base[-1] == ':')
10097 base++;
10098 else
10099 base = demangled;
10100
10101 part_die->name = obsavestring (base, strlen (base),
10102 &cu->objfile->objfile_obstack);
10103 xfree (demangled);
10104 }
10105 }
10106
10107 part_die->fixup_called = 1;
10108 }
10109
10110 /* Read an attribute value described by an attribute form. */
10111
10112 static gdb_byte *
10113 read_attribute_value (struct attribute *attr, unsigned form,
10114 bfd *abfd, gdb_byte *info_ptr,
10115 struct dwarf2_cu *cu)
10116 {
10117 struct comp_unit_head *cu_header = &cu->header;
10118 unsigned int bytes_read;
10119 struct dwarf_block *blk;
10120
10121 attr->form = form;
10122 switch (form)
10123 {
10124 case DW_FORM_ref_addr:
10125 if (cu->header.version == 2)
10126 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10127 else
10128 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10129 &cu->header, &bytes_read);
10130 info_ptr += bytes_read;
10131 break;
10132 case DW_FORM_addr:
10133 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10134 info_ptr += bytes_read;
10135 break;
10136 case DW_FORM_block2:
10137 blk = dwarf_alloc_block (cu);
10138 blk->size = read_2_bytes (abfd, info_ptr);
10139 info_ptr += 2;
10140 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10141 info_ptr += blk->size;
10142 DW_BLOCK (attr) = blk;
10143 break;
10144 case DW_FORM_block4:
10145 blk = dwarf_alloc_block (cu);
10146 blk->size = read_4_bytes (abfd, info_ptr);
10147 info_ptr += 4;
10148 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10149 info_ptr += blk->size;
10150 DW_BLOCK (attr) = blk;
10151 break;
10152 case DW_FORM_data2:
10153 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10154 info_ptr += 2;
10155 break;
10156 case DW_FORM_data4:
10157 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10158 info_ptr += 4;
10159 break;
10160 case DW_FORM_data8:
10161 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10162 info_ptr += 8;
10163 break;
10164 case DW_FORM_sec_offset:
10165 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10166 info_ptr += bytes_read;
10167 break;
10168 case DW_FORM_string:
10169 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10170 DW_STRING_IS_CANONICAL (attr) = 0;
10171 info_ptr += bytes_read;
10172 break;
10173 case DW_FORM_strp:
10174 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10175 &bytes_read);
10176 DW_STRING_IS_CANONICAL (attr) = 0;
10177 info_ptr += bytes_read;
10178 break;
10179 case DW_FORM_exprloc:
10180 case DW_FORM_block:
10181 blk = dwarf_alloc_block (cu);
10182 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10183 info_ptr += bytes_read;
10184 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10185 info_ptr += blk->size;
10186 DW_BLOCK (attr) = blk;
10187 break;
10188 case DW_FORM_block1:
10189 blk = dwarf_alloc_block (cu);
10190 blk->size = read_1_byte (abfd, info_ptr);
10191 info_ptr += 1;
10192 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10193 info_ptr += blk->size;
10194 DW_BLOCK (attr) = blk;
10195 break;
10196 case DW_FORM_data1:
10197 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10198 info_ptr += 1;
10199 break;
10200 case DW_FORM_flag:
10201 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10202 info_ptr += 1;
10203 break;
10204 case DW_FORM_flag_present:
10205 DW_UNSND (attr) = 1;
10206 break;
10207 case DW_FORM_sdata:
10208 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10209 info_ptr += bytes_read;
10210 break;
10211 case DW_FORM_udata:
10212 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10213 info_ptr += bytes_read;
10214 break;
10215 case DW_FORM_ref1:
10216 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10217 info_ptr += 1;
10218 break;
10219 case DW_FORM_ref2:
10220 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10221 info_ptr += 2;
10222 break;
10223 case DW_FORM_ref4:
10224 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10225 info_ptr += 4;
10226 break;
10227 case DW_FORM_ref8:
10228 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10229 info_ptr += 8;
10230 break;
10231 case DW_FORM_ref_sig8:
10232 /* Convert the signature to something we can record in DW_UNSND
10233 for later lookup.
10234 NOTE: This is NULL if the type wasn't found. */
10235 DW_SIGNATURED_TYPE (attr) =
10236 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10237 info_ptr += 8;
10238 break;
10239 case DW_FORM_ref_udata:
10240 DW_ADDR (attr) = (cu->header.offset
10241 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10242 info_ptr += bytes_read;
10243 break;
10244 case DW_FORM_indirect:
10245 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10246 info_ptr += bytes_read;
10247 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10248 break;
10249 default:
10250 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10251 dwarf_form_name (form),
10252 bfd_get_filename (abfd));
10253 }
10254
10255 /* We have seen instances where the compiler tried to emit a byte
10256 size attribute of -1 which ended up being encoded as an unsigned
10257 0xffffffff. Although 0xffffffff is technically a valid size value,
10258 an object of this size seems pretty unlikely so we can relatively
10259 safely treat these cases as if the size attribute was invalid and
10260 treat them as zero by default. */
10261 if (attr->name == DW_AT_byte_size
10262 && form == DW_FORM_data4
10263 && DW_UNSND (attr) >= 0xffffffff)
10264 {
10265 complaint
10266 (&symfile_complaints,
10267 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10268 hex_string (DW_UNSND (attr)));
10269 DW_UNSND (attr) = 0;
10270 }
10271
10272 return info_ptr;
10273 }
10274
10275 /* Read an attribute described by an abbreviated attribute. */
10276
10277 static gdb_byte *
10278 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10279 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10280 {
10281 attr->name = abbrev->name;
10282 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10283 }
10284
10285 /* Read dwarf information from a buffer. */
10286
10287 static unsigned int
10288 read_1_byte (bfd *abfd, gdb_byte *buf)
10289 {
10290 return bfd_get_8 (abfd, buf);
10291 }
10292
10293 static int
10294 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10295 {
10296 return bfd_get_signed_8 (abfd, buf);
10297 }
10298
10299 static unsigned int
10300 read_2_bytes (bfd *abfd, gdb_byte *buf)
10301 {
10302 return bfd_get_16 (abfd, buf);
10303 }
10304
10305 static int
10306 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10307 {
10308 return bfd_get_signed_16 (abfd, buf);
10309 }
10310
10311 static unsigned int
10312 read_4_bytes (bfd *abfd, gdb_byte *buf)
10313 {
10314 return bfd_get_32 (abfd, buf);
10315 }
10316
10317 static int
10318 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10319 {
10320 return bfd_get_signed_32 (abfd, buf);
10321 }
10322
10323 static ULONGEST
10324 read_8_bytes (bfd *abfd, gdb_byte *buf)
10325 {
10326 return bfd_get_64 (abfd, buf);
10327 }
10328
10329 static CORE_ADDR
10330 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10331 unsigned int *bytes_read)
10332 {
10333 struct comp_unit_head *cu_header = &cu->header;
10334 CORE_ADDR retval = 0;
10335
10336 if (cu_header->signed_addr_p)
10337 {
10338 switch (cu_header->addr_size)
10339 {
10340 case 2:
10341 retval = bfd_get_signed_16 (abfd, buf);
10342 break;
10343 case 4:
10344 retval = bfd_get_signed_32 (abfd, buf);
10345 break;
10346 case 8:
10347 retval = bfd_get_signed_64 (abfd, buf);
10348 break;
10349 default:
10350 internal_error (__FILE__, __LINE__,
10351 _("read_address: bad switch, signed [in module %s]"),
10352 bfd_get_filename (abfd));
10353 }
10354 }
10355 else
10356 {
10357 switch (cu_header->addr_size)
10358 {
10359 case 2:
10360 retval = bfd_get_16 (abfd, buf);
10361 break;
10362 case 4:
10363 retval = bfd_get_32 (abfd, buf);
10364 break;
10365 case 8:
10366 retval = bfd_get_64 (abfd, buf);
10367 break;
10368 default:
10369 internal_error (__FILE__, __LINE__,
10370 _("read_address: bad switch, "
10371 "unsigned [in module %s]"),
10372 bfd_get_filename (abfd));
10373 }
10374 }
10375
10376 *bytes_read = cu_header->addr_size;
10377 return retval;
10378 }
10379
10380 /* Read the initial length from a section. The (draft) DWARF 3
10381 specification allows the initial length to take up either 4 bytes
10382 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10383 bytes describe the length and all offsets will be 8 bytes in length
10384 instead of 4.
10385
10386 An older, non-standard 64-bit format is also handled by this
10387 function. The older format in question stores the initial length
10388 as an 8-byte quantity without an escape value. Lengths greater
10389 than 2^32 aren't very common which means that the initial 4 bytes
10390 is almost always zero. Since a length value of zero doesn't make
10391 sense for the 32-bit format, this initial zero can be considered to
10392 be an escape value which indicates the presence of the older 64-bit
10393 format. As written, the code can't detect (old format) lengths
10394 greater than 4GB. If it becomes necessary to handle lengths
10395 somewhat larger than 4GB, we could allow other small values (such
10396 as the non-sensical values of 1, 2, and 3) to also be used as
10397 escape values indicating the presence of the old format.
10398
10399 The value returned via bytes_read should be used to increment the
10400 relevant pointer after calling read_initial_length().
10401
10402 [ Note: read_initial_length() and read_offset() are based on the
10403 document entitled "DWARF Debugging Information Format", revision
10404 3, draft 8, dated November 19, 2001. This document was obtained
10405 from:
10406
10407 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10408
10409 This document is only a draft and is subject to change. (So beware.)
10410
10411 Details regarding the older, non-standard 64-bit format were
10412 determined empirically by examining 64-bit ELF files produced by
10413 the SGI toolchain on an IRIX 6.5 machine.
10414
10415 - Kevin, July 16, 2002
10416 ] */
10417
10418 static LONGEST
10419 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10420 {
10421 LONGEST length = bfd_get_32 (abfd, buf);
10422
10423 if (length == 0xffffffff)
10424 {
10425 length = bfd_get_64 (abfd, buf + 4);
10426 *bytes_read = 12;
10427 }
10428 else if (length == 0)
10429 {
10430 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10431 length = bfd_get_64 (abfd, buf);
10432 *bytes_read = 8;
10433 }
10434 else
10435 {
10436 *bytes_read = 4;
10437 }
10438
10439 return length;
10440 }
10441
10442 /* Cover function for read_initial_length.
10443 Returns the length of the object at BUF, and stores the size of the
10444 initial length in *BYTES_READ and stores the size that offsets will be in
10445 *OFFSET_SIZE.
10446 If the initial length size is not equivalent to that specified in
10447 CU_HEADER then issue a complaint.
10448 This is useful when reading non-comp-unit headers. */
10449
10450 static LONGEST
10451 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10452 const struct comp_unit_head *cu_header,
10453 unsigned int *bytes_read,
10454 unsigned int *offset_size)
10455 {
10456 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10457
10458 gdb_assert (cu_header->initial_length_size == 4
10459 || cu_header->initial_length_size == 8
10460 || cu_header->initial_length_size == 12);
10461
10462 if (cu_header->initial_length_size != *bytes_read)
10463 complaint (&symfile_complaints,
10464 _("intermixed 32-bit and 64-bit DWARF sections"));
10465
10466 *offset_size = (*bytes_read == 4) ? 4 : 8;
10467 return length;
10468 }
10469
10470 /* Read an offset from the data stream. The size of the offset is
10471 given by cu_header->offset_size. */
10472
10473 static LONGEST
10474 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10475 unsigned int *bytes_read)
10476 {
10477 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10478
10479 *bytes_read = cu_header->offset_size;
10480 return offset;
10481 }
10482
10483 /* Read an offset from the data stream. */
10484
10485 static LONGEST
10486 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10487 {
10488 LONGEST retval = 0;
10489
10490 switch (offset_size)
10491 {
10492 case 4:
10493 retval = bfd_get_32 (abfd, buf);
10494 break;
10495 case 8:
10496 retval = bfd_get_64 (abfd, buf);
10497 break;
10498 default:
10499 internal_error (__FILE__, __LINE__,
10500 _("read_offset_1: bad switch [in module %s]"),
10501 bfd_get_filename (abfd));
10502 }
10503
10504 return retval;
10505 }
10506
10507 static gdb_byte *
10508 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10509 {
10510 /* If the size of a host char is 8 bits, we can return a pointer
10511 to the buffer, otherwise we have to copy the data to a buffer
10512 allocated on the temporary obstack. */
10513 gdb_assert (HOST_CHAR_BIT == 8);
10514 return buf;
10515 }
10516
10517 static char *
10518 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10519 {
10520 /* If the size of a host char is 8 bits, we can return a pointer
10521 to the string, otherwise we have to copy the string to a buffer
10522 allocated on the temporary obstack. */
10523 gdb_assert (HOST_CHAR_BIT == 8);
10524 if (*buf == '\0')
10525 {
10526 *bytes_read_ptr = 1;
10527 return NULL;
10528 }
10529 *bytes_read_ptr = strlen ((char *) buf) + 1;
10530 return (char *) buf;
10531 }
10532
10533 static char *
10534 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10535 {
10536 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10537 if (dwarf2_per_objfile->str.buffer == NULL)
10538 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10539 bfd_get_filename (abfd));
10540 if (str_offset >= dwarf2_per_objfile->str.size)
10541 error (_("DW_FORM_strp pointing outside of "
10542 ".debug_str section [in module %s]"),
10543 bfd_get_filename (abfd));
10544 gdb_assert (HOST_CHAR_BIT == 8);
10545 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10546 return NULL;
10547 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10548 }
10549
10550 static char *
10551 read_indirect_string (bfd *abfd, gdb_byte *buf,
10552 const struct comp_unit_head *cu_header,
10553 unsigned int *bytes_read_ptr)
10554 {
10555 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10556
10557 return read_indirect_string_at_offset (abfd, str_offset);
10558 }
10559
10560 static unsigned long
10561 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10562 {
10563 unsigned long result;
10564 unsigned int num_read;
10565 int i, shift;
10566 unsigned char byte;
10567
10568 result = 0;
10569 shift = 0;
10570 num_read = 0;
10571 i = 0;
10572 while (1)
10573 {
10574 byte = bfd_get_8 (abfd, buf);
10575 buf++;
10576 num_read++;
10577 result |= ((unsigned long)(byte & 127) << shift);
10578 if ((byte & 128) == 0)
10579 {
10580 break;
10581 }
10582 shift += 7;
10583 }
10584 *bytes_read_ptr = num_read;
10585 return result;
10586 }
10587
10588 static long
10589 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10590 {
10591 long result;
10592 int i, shift, num_read;
10593 unsigned char byte;
10594
10595 result = 0;
10596 shift = 0;
10597 num_read = 0;
10598 i = 0;
10599 while (1)
10600 {
10601 byte = bfd_get_8 (abfd, buf);
10602 buf++;
10603 num_read++;
10604 result |= ((long)(byte & 127) << shift);
10605 shift += 7;
10606 if ((byte & 128) == 0)
10607 {
10608 break;
10609 }
10610 }
10611 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10612 result |= -(((long)1) << shift);
10613 *bytes_read_ptr = num_read;
10614 return result;
10615 }
10616
10617 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10618
10619 static gdb_byte *
10620 skip_leb128 (bfd *abfd, gdb_byte *buf)
10621 {
10622 int byte;
10623
10624 while (1)
10625 {
10626 byte = bfd_get_8 (abfd, buf);
10627 buf++;
10628 if ((byte & 128) == 0)
10629 return buf;
10630 }
10631 }
10632
10633 static void
10634 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10635 {
10636 switch (lang)
10637 {
10638 case DW_LANG_C89:
10639 case DW_LANG_C99:
10640 case DW_LANG_C:
10641 cu->language = language_c;
10642 break;
10643 case DW_LANG_C_plus_plus:
10644 cu->language = language_cplus;
10645 break;
10646 case DW_LANG_D:
10647 cu->language = language_d;
10648 break;
10649 case DW_LANG_Fortran77:
10650 case DW_LANG_Fortran90:
10651 case DW_LANG_Fortran95:
10652 cu->language = language_fortran;
10653 break;
10654 case DW_LANG_Mips_Assembler:
10655 cu->language = language_asm;
10656 break;
10657 case DW_LANG_Java:
10658 cu->language = language_java;
10659 break;
10660 case DW_LANG_Ada83:
10661 case DW_LANG_Ada95:
10662 cu->language = language_ada;
10663 break;
10664 case DW_LANG_Modula2:
10665 cu->language = language_m2;
10666 break;
10667 case DW_LANG_Pascal83:
10668 cu->language = language_pascal;
10669 break;
10670 case DW_LANG_ObjC:
10671 cu->language = language_objc;
10672 break;
10673 case DW_LANG_Cobol74:
10674 case DW_LANG_Cobol85:
10675 default:
10676 cu->language = language_minimal;
10677 break;
10678 }
10679 cu->language_defn = language_def (cu->language);
10680 }
10681
10682 /* Return the named attribute or NULL if not there. */
10683
10684 static struct attribute *
10685 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10686 {
10687 unsigned int i;
10688 struct attribute *spec = NULL;
10689
10690 for (i = 0; i < die->num_attrs; ++i)
10691 {
10692 if (die->attrs[i].name == name)
10693 return &die->attrs[i];
10694 if (die->attrs[i].name == DW_AT_specification
10695 || die->attrs[i].name == DW_AT_abstract_origin)
10696 spec = &die->attrs[i];
10697 }
10698
10699 if (spec)
10700 {
10701 die = follow_die_ref (die, spec, &cu);
10702 return dwarf2_attr (die, name, cu);
10703 }
10704
10705 return NULL;
10706 }
10707
10708 /* Return the named attribute or NULL if not there,
10709 but do not follow DW_AT_specification, etc.
10710 This is for use in contexts where we're reading .debug_types dies.
10711 Following DW_AT_specification, DW_AT_abstract_origin will take us
10712 back up the chain, and we want to go down. */
10713
10714 static struct attribute *
10715 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10716 struct dwarf2_cu *cu)
10717 {
10718 unsigned int i;
10719
10720 for (i = 0; i < die->num_attrs; ++i)
10721 if (die->attrs[i].name == name)
10722 return &die->attrs[i];
10723
10724 return NULL;
10725 }
10726
10727 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10728 and holds a non-zero value. This function should only be used for
10729 DW_FORM_flag or DW_FORM_flag_present attributes. */
10730
10731 static int
10732 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10733 {
10734 struct attribute *attr = dwarf2_attr (die, name, cu);
10735
10736 return (attr && DW_UNSND (attr));
10737 }
10738
10739 static int
10740 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10741 {
10742 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10743 which value is non-zero. However, we have to be careful with
10744 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10745 (via dwarf2_flag_true_p) follows this attribute. So we may
10746 end up accidently finding a declaration attribute that belongs
10747 to a different DIE referenced by the specification attribute,
10748 even though the given DIE does not have a declaration attribute. */
10749 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10750 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10751 }
10752
10753 /* Return the die giving the specification for DIE, if there is
10754 one. *SPEC_CU is the CU containing DIE on input, and the CU
10755 containing the return value on output. If there is no
10756 specification, but there is an abstract origin, that is
10757 returned. */
10758
10759 static struct die_info *
10760 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10761 {
10762 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10763 *spec_cu);
10764
10765 if (spec_attr == NULL)
10766 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10767
10768 if (spec_attr == NULL)
10769 return NULL;
10770 else
10771 return follow_die_ref (die, spec_attr, spec_cu);
10772 }
10773
10774 /* Free the line_header structure *LH, and any arrays and strings it
10775 refers to.
10776 NOTE: This is also used as a "cleanup" function. */
10777
10778 static void
10779 free_line_header (struct line_header *lh)
10780 {
10781 if (lh->standard_opcode_lengths)
10782 xfree (lh->standard_opcode_lengths);
10783
10784 /* Remember that all the lh->file_names[i].name pointers are
10785 pointers into debug_line_buffer, and don't need to be freed. */
10786 if (lh->file_names)
10787 xfree (lh->file_names);
10788
10789 /* Similarly for the include directory names. */
10790 if (lh->include_dirs)
10791 xfree (lh->include_dirs);
10792
10793 xfree (lh);
10794 }
10795
10796 /* Add an entry to LH's include directory table. */
10797
10798 static void
10799 add_include_dir (struct line_header *lh, char *include_dir)
10800 {
10801 /* Grow the array if necessary. */
10802 if (lh->include_dirs_size == 0)
10803 {
10804 lh->include_dirs_size = 1; /* for testing */
10805 lh->include_dirs = xmalloc (lh->include_dirs_size
10806 * sizeof (*lh->include_dirs));
10807 }
10808 else if (lh->num_include_dirs >= lh->include_dirs_size)
10809 {
10810 lh->include_dirs_size *= 2;
10811 lh->include_dirs = xrealloc (lh->include_dirs,
10812 (lh->include_dirs_size
10813 * sizeof (*lh->include_dirs)));
10814 }
10815
10816 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10817 }
10818
10819 /* Add an entry to LH's file name table. */
10820
10821 static void
10822 add_file_name (struct line_header *lh,
10823 char *name,
10824 unsigned int dir_index,
10825 unsigned int mod_time,
10826 unsigned int length)
10827 {
10828 struct file_entry *fe;
10829
10830 /* Grow the array if necessary. */
10831 if (lh->file_names_size == 0)
10832 {
10833 lh->file_names_size = 1; /* for testing */
10834 lh->file_names = xmalloc (lh->file_names_size
10835 * sizeof (*lh->file_names));
10836 }
10837 else if (lh->num_file_names >= lh->file_names_size)
10838 {
10839 lh->file_names_size *= 2;
10840 lh->file_names = xrealloc (lh->file_names,
10841 (lh->file_names_size
10842 * sizeof (*lh->file_names)));
10843 }
10844
10845 fe = &lh->file_names[lh->num_file_names++];
10846 fe->name = name;
10847 fe->dir_index = dir_index;
10848 fe->mod_time = mod_time;
10849 fe->length = length;
10850 fe->included_p = 0;
10851 fe->symtab = NULL;
10852 }
10853
10854 /* Read the statement program header starting at OFFSET in
10855 .debug_line, according to the endianness of ABFD. Return a pointer
10856 to a struct line_header, allocated using xmalloc.
10857
10858 NOTE: the strings in the include directory and file name tables of
10859 the returned object point into debug_line_buffer, and must not be
10860 freed. */
10861
10862 static struct line_header *
10863 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10864 struct dwarf2_cu *cu)
10865 {
10866 struct cleanup *back_to;
10867 struct line_header *lh;
10868 gdb_byte *line_ptr;
10869 unsigned int bytes_read, offset_size;
10870 int i;
10871 char *cur_dir, *cur_file;
10872
10873 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10874 if (dwarf2_per_objfile->line.buffer == NULL)
10875 {
10876 complaint (&symfile_complaints, _("missing .debug_line section"));
10877 return 0;
10878 }
10879
10880 /* Make sure that at least there's room for the total_length field.
10881 That could be 12 bytes long, but we're just going to fudge that. */
10882 if (offset + 4 >= dwarf2_per_objfile->line.size)
10883 {
10884 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10885 return 0;
10886 }
10887
10888 lh = xmalloc (sizeof (*lh));
10889 memset (lh, 0, sizeof (*lh));
10890 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10891 (void *) lh);
10892
10893 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10894
10895 /* Read in the header. */
10896 lh->total_length =
10897 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10898 &bytes_read, &offset_size);
10899 line_ptr += bytes_read;
10900 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10901 + dwarf2_per_objfile->line.size))
10902 {
10903 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10904 return 0;
10905 }
10906 lh->statement_program_end = line_ptr + lh->total_length;
10907 lh->version = read_2_bytes (abfd, line_ptr);
10908 line_ptr += 2;
10909 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10910 line_ptr += offset_size;
10911 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10912 line_ptr += 1;
10913 if (lh->version >= 4)
10914 {
10915 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10916 line_ptr += 1;
10917 }
10918 else
10919 lh->maximum_ops_per_instruction = 1;
10920
10921 if (lh->maximum_ops_per_instruction == 0)
10922 {
10923 lh->maximum_ops_per_instruction = 1;
10924 complaint (&symfile_complaints,
10925 _("invalid maximum_ops_per_instruction "
10926 "in `.debug_line' section"));
10927 }
10928
10929 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10930 line_ptr += 1;
10931 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10932 line_ptr += 1;
10933 lh->line_range = read_1_byte (abfd, line_ptr);
10934 line_ptr += 1;
10935 lh->opcode_base = read_1_byte (abfd, line_ptr);
10936 line_ptr += 1;
10937 lh->standard_opcode_lengths
10938 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10939
10940 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10941 for (i = 1; i < lh->opcode_base; ++i)
10942 {
10943 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10944 line_ptr += 1;
10945 }
10946
10947 /* Read directory table. */
10948 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10949 {
10950 line_ptr += bytes_read;
10951 add_include_dir (lh, cur_dir);
10952 }
10953 line_ptr += bytes_read;
10954
10955 /* Read file name table. */
10956 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10957 {
10958 unsigned int dir_index, mod_time, length;
10959
10960 line_ptr += bytes_read;
10961 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10962 line_ptr += bytes_read;
10963 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10964 line_ptr += bytes_read;
10965 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10966 line_ptr += bytes_read;
10967
10968 add_file_name (lh, cur_file, dir_index, mod_time, length);
10969 }
10970 line_ptr += bytes_read;
10971 lh->statement_program_start = line_ptr;
10972
10973 if (line_ptr > (dwarf2_per_objfile->line.buffer
10974 + dwarf2_per_objfile->line.size))
10975 complaint (&symfile_complaints,
10976 _("line number info header doesn't "
10977 "fit in `.debug_line' section"));
10978
10979 discard_cleanups (back_to);
10980 return lh;
10981 }
10982
10983 /* Subroutine of dwarf_decode_lines to simplify it.
10984 Return the file name of the psymtab for included file FILE_INDEX
10985 in line header LH of PST.
10986 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10987 If space for the result is malloc'd, it will be freed by a cleanup.
10988 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10989
10990 static char *
10991 psymtab_include_file_name (const struct line_header *lh, int file_index,
10992 const struct partial_symtab *pst,
10993 const char *comp_dir)
10994 {
10995 const struct file_entry fe = lh->file_names [file_index];
10996 char *include_name = fe.name;
10997 char *include_name_to_compare = include_name;
10998 char *dir_name = NULL;
10999 const char *pst_filename;
11000 char *copied_name = NULL;
11001 int file_is_pst;
11002
11003 if (fe.dir_index)
11004 dir_name = lh->include_dirs[fe.dir_index - 1];
11005
11006 if (!IS_ABSOLUTE_PATH (include_name)
11007 && (dir_name != NULL || comp_dir != NULL))
11008 {
11009 /* Avoid creating a duplicate psymtab for PST.
11010 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11011 Before we do the comparison, however, we need to account
11012 for DIR_NAME and COMP_DIR.
11013 First prepend dir_name (if non-NULL). If we still don't
11014 have an absolute path prepend comp_dir (if non-NULL).
11015 However, the directory we record in the include-file's
11016 psymtab does not contain COMP_DIR (to match the
11017 corresponding symtab(s)).
11018
11019 Example:
11020
11021 bash$ cd /tmp
11022 bash$ gcc -g ./hello.c
11023 include_name = "hello.c"
11024 dir_name = "."
11025 DW_AT_comp_dir = comp_dir = "/tmp"
11026 DW_AT_name = "./hello.c" */
11027
11028 if (dir_name != NULL)
11029 {
11030 include_name = concat (dir_name, SLASH_STRING,
11031 include_name, (char *)NULL);
11032 include_name_to_compare = include_name;
11033 make_cleanup (xfree, include_name);
11034 }
11035 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11036 {
11037 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11038 include_name, (char *)NULL);
11039 }
11040 }
11041
11042 pst_filename = pst->filename;
11043 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11044 {
11045 copied_name = concat (pst->dirname, SLASH_STRING,
11046 pst_filename, (char *)NULL);
11047 pst_filename = copied_name;
11048 }
11049
11050 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11051
11052 if (include_name_to_compare != include_name)
11053 xfree (include_name_to_compare);
11054 if (copied_name != NULL)
11055 xfree (copied_name);
11056
11057 if (file_is_pst)
11058 return NULL;
11059 return include_name;
11060 }
11061
11062 /* Ignore this record_line request. */
11063
11064 static void
11065 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11066 {
11067 return;
11068 }
11069
11070 /* Subroutine of dwarf_decode_lines to simplify it.
11071 Process the line number information in LH. */
11072
11073 static void
11074 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11075 struct dwarf2_cu *cu, struct partial_symtab *pst)
11076 {
11077 gdb_byte *line_ptr, *extended_end;
11078 gdb_byte *line_end;
11079 unsigned int bytes_read, extended_len;
11080 unsigned char op_code, extended_op, adj_opcode;
11081 CORE_ADDR baseaddr;
11082 struct objfile *objfile = cu->objfile;
11083 bfd *abfd = objfile->obfd;
11084 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11085 const int decode_for_pst_p = (pst != NULL);
11086 struct subfile *last_subfile = NULL;
11087 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11088 = record_line;
11089
11090 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11091
11092 line_ptr = lh->statement_program_start;
11093 line_end = lh->statement_program_end;
11094
11095 /* Read the statement sequences until there's nothing left. */
11096 while (line_ptr < line_end)
11097 {
11098 /* state machine registers */
11099 CORE_ADDR address = 0;
11100 unsigned int file = 1;
11101 unsigned int line = 1;
11102 unsigned int column = 0;
11103 int is_stmt = lh->default_is_stmt;
11104 int basic_block = 0;
11105 int end_sequence = 0;
11106 CORE_ADDR addr;
11107 unsigned char op_index = 0;
11108
11109 if (!decode_for_pst_p && lh->num_file_names >= file)
11110 {
11111 /* Start a subfile for the current file of the state machine. */
11112 /* lh->include_dirs and lh->file_names are 0-based, but the
11113 directory and file name numbers in the statement program
11114 are 1-based. */
11115 struct file_entry *fe = &lh->file_names[file - 1];
11116 char *dir = NULL;
11117
11118 if (fe->dir_index)
11119 dir = lh->include_dirs[fe->dir_index - 1];
11120
11121 dwarf2_start_subfile (fe->name, dir, comp_dir);
11122 }
11123
11124 /* Decode the table. */
11125 while (!end_sequence)
11126 {
11127 op_code = read_1_byte (abfd, line_ptr);
11128 line_ptr += 1;
11129 if (line_ptr > line_end)
11130 {
11131 dwarf2_debug_line_missing_end_sequence_complaint ();
11132 break;
11133 }
11134
11135 if (op_code >= lh->opcode_base)
11136 {
11137 /* Special operand. */
11138 adj_opcode = op_code - lh->opcode_base;
11139 address += (((op_index + (adj_opcode / lh->line_range))
11140 / lh->maximum_ops_per_instruction)
11141 * lh->minimum_instruction_length);
11142 op_index = ((op_index + (adj_opcode / lh->line_range))
11143 % lh->maximum_ops_per_instruction);
11144 line += lh->line_base + (adj_opcode % lh->line_range);
11145 if (lh->num_file_names < file || file == 0)
11146 dwarf2_debug_line_missing_file_complaint ();
11147 /* For now we ignore lines not starting on an
11148 instruction boundary. */
11149 else if (op_index == 0)
11150 {
11151 lh->file_names[file - 1].included_p = 1;
11152 if (!decode_for_pst_p && is_stmt)
11153 {
11154 if (last_subfile != current_subfile)
11155 {
11156 addr = gdbarch_addr_bits_remove (gdbarch, address);
11157 if (last_subfile)
11158 (*p_record_line) (last_subfile, 0, addr);
11159 last_subfile = current_subfile;
11160 }
11161 /* Append row to matrix using current values. */
11162 addr = gdbarch_addr_bits_remove (gdbarch, address);
11163 (*p_record_line) (current_subfile, line, addr);
11164 }
11165 }
11166 basic_block = 0;
11167 }
11168 else switch (op_code)
11169 {
11170 case DW_LNS_extended_op:
11171 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11172 &bytes_read);
11173 line_ptr += bytes_read;
11174 extended_end = line_ptr + extended_len;
11175 extended_op = read_1_byte (abfd, line_ptr);
11176 line_ptr += 1;
11177 switch (extended_op)
11178 {
11179 case DW_LNE_end_sequence:
11180 p_record_line = record_line;
11181 end_sequence = 1;
11182 break;
11183 case DW_LNE_set_address:
11184 address = read_address (abfd, line_ptr, cu, &bytes_read);
11185
11186 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11187 {
11188 /* This line table is for a function which has been
11189 GCd by the linker. Ignore it. PR gdb/12528 */
11190
11191 long line_offset
11192 = line_ptr - dwarf2_per_objfile->line.buffer;
11193
11194 complaint (&symfile_complaints,
11195 _(".debug_line address at offset 0x%lx is 0 "
11196 "[in module %s]"),
11197 line_offset, objfile->name);
11198 p_record_line = noop_record_line;
11199 }
11200
11201 op_index = 0;
11202 line_ptr += bytes_read;
11203 address += baseaddr;
11204 break;
11205 case DW_LNE_define_file:
11206 {
11207 char *cur_file;
11208 unsigned int dir_index, mod_time, length;
11209
11210 cur_file = read_direct_string (abfd, line_ptr,
11211 &bytes_read);
11212 line_ptr += bytes_read;
11213 dir_index =
11214 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11215 line_ptr += bytes_read;
11216 mod_time =
11217 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11218 line_ptr += bytes_read;
11219 length =
11220 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11221 line_ptr += bytes_read;
11222 add_file_name (lh, cur_file, dir_index, mod_time, length);
11223 }
11224 break;
11225 case DW_LNE_set_discriminator:
11226 /* The discriminator is not interesting to the debugger;
11227 just ignore it. */
11228 line_ptr = extended_end;
11229 break;
11230 default:
11231 complaint (&symfile_complaints,
11232 _("mangled .debug_line section"));
11233 return;
11234 }
11235 /* Make sure that we parsed the extended op correctly. If e.g.
11236 we expected a different address size than the producer used,
11237 we may have read the wrong number of bytes. */
11238 if (line_ptr != extended_end)
11239 {
11240 complaint (&symfile_complaints,
11241 _("mangled .debug_line section"));
11242 return;
11243 }
11244 break;
11245 case DW_LNS_copy:
11246 if (lh->num_file_names < file || file == 0)
11247 dwarf2_debug_line_missing_file_complaint ();
11248 else
11249 {
11250 lh->file_names[file - 1].included_p = 1;
11251 if (!decode_for_pst_p && is_stmt)
11252 {
11253 if (last_subfile != current_subfile)
11254 {
11255 addr = gdbarch_addr_bits_remove (gdbarch, address);
11256 if (last_subfile)
11257 (*p_record_line) (last_subfile, 0, addr);
11258 last_subfile = current_subfile;
11259 }
11260 addr = gdbarch_addr_bits_remove (gdbarch, address);
11261 (*p_record_line) (current_subfile, line, addr);
11262 }
11263 }
11264 basic_block = 0;
11265 break;
11266 case DW_LNS_advance_pc:
11267 {
11268 CORE_ADDR adjust
11269 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11270
11271 address += (((op_index + adjust)
11272 / lh->maximum_ops_per_instruction)
11273 * lh->minimum_instruction_length);
11274 op_index = ((op_index + adjust)
11275 % lh->maximum_ops_per_instruction);
11276 line_ptr += bytes_read;
11277 }
11278 break;
11279 case DW_LNS_advance_line:
11280 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11281 line_ptr += bytes_read;
11282 break;
11283 case DW_LNS_set_file:
11284 {
11285 /* The arrays lh->include_dirs and lh->file_names are
11286 0-based, but the directory and file name numbers in
11287 the statement program are 1-based. */
11288 struct file_entry *fe;
11289 char *dir = NULL;
11290
11291 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11292 line_ptr += bytes_read;
11293 if (lh->num_file_names < file || file == 0)
11294 dwarf2_debug_line_missing_file_complaint ();
11295 else
11296 {
11297 fe = &lh->file_names[file - 1];
11298 if (fe->dir_index)
11299 dir = lh->include_dirs[fe->dir_index - 1];
11300 if (!decode_for_pst_p)
11301 {
11302 last_subfile = current_subfile;
11303 dwarf2_start_subfile (fe->name, dir, comp_dir);
11304 }
11305 }
11306 }
11307 break;
11308 case DW_LNS_set_column:
11309 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11310 line_ptr += bytes_read;
11311 break;
11312 case DW_LNS_negate_stmt:
11313 is_stmt = (!is_stmt);
11314 break;
11315 case DW_LNS_set_basic_block:
11316 basic_block = 1;
11317 break;
11318 /* Add to the address register of the state machine the
11319 address increment value corresponding to special opcode
11320 255. I.e., this value is scaled by the minimum
11321 instruction length since special opcode 255 would have
11322 scaled the increment. */
11323 case DW_LNS_const_add_pc:
11324 {
11325 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11326
11327 address += (((op_index + adjust)
11328 / lh->maximum_ops_per_instruction)
11329 * lh->minimum_instruction_length);
11330 op_index = ((op_index + adjust)
11331 % lh->maximum_ops_per_instruction);
11332 }
11333 break;
11334 case DW_LNS_fixed_advance_pc:
11335 address += read_2_bytes (abfd, line_ptr);
11336 op_index = 0;
11337 line_ptr += 2;
11338 break;
11339 default:
11340 {
11341 /* Unknown standard opcode, ignore it. */
11342 int i;
11343
11344 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11345 {
11346 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11347 line_ptr += bytes_read;
11348 }
11349 }
11350 }
11351 }
11352 if (lh->num_file_names < file || file == 0)
11353 dwarf2_debug_line_missing_file_complaint ();
11354 else
11355 {
11356 lh->file_names[file - 1].included_p = 1;
11357 if (!decode_for_pst_p)
11358 {
11359 addr = gdbarch_addr_bits_remove (gdbarch, address);
11360 (*p_record_line) (current_subfile, 0, addr);
11361 }
11362 }
11363 }
11364 }
11365
11366 /* Decode the Line Number Program (LNP) for the given line_header
11367 structure and CU. The actual information extracted and the type
11368 of structures created from the LNP depends on the value of PST.
11369
11370 1. If PST is NULL, then this procedure uses the data from the program
11371 to create all necessary symbol tables, and their linetables.
11372
11373 2. If PST is not NULL, this procedure reads the program to determine
11374 the list of files included by the unit represented by PST, and
11375 builds all the associated partial symbol tables.
11376
11377 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11378 It is used for relative paths in the line table.
11379 NOTE: When processing partial symtabs (pst != NULL),
11380 comp_dir == pst->dirname.
11381
11382 NOTE: It is important that psymtabs have the same file name (via strcmp)
11383 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11384 symtab we don't use it in the name of the psymtabs we create.
11385 E.g. expand_line_sal requires this when finding psymtabs to expand.
11386 A good testcase for this is mb-inline.exp. */
11387
11388 static void
11389 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11390 struct dwarf2_cu *cu, struct partial_symtab *pst,
11391 int want_line_info)
11392 {
11393 struct objfile *objfile = cu->objfile;
11394 const int decode_for_pst_p = (pst != NULL);
11395 struct subfile *first_subfile = current_subfile;
11396
11397 if (want_line_info)
11398 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
11399
11400 if (decode_for_pst_p)
11401 {
11402 int file_index;
11403
11404 /* Now that we're done scanning the Line Header Program, we can
11405 create the psymtab of each included file. */
11406 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11407 if (lh->file_names[file_index].included_p == 1)
11408 {
11409 char *include_name =
11410 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11411 if (include_name != NULL)
11412 dwarf2_create_include_psymtab (include_name, pst, objfile);
11413 }
11414 }
11415 else
11416 {
11417 /* Make sure a symtab is created for every file, even files
11418 which contain only variables (i.e. no code with associated
11419 line numbers). */
11420 int i;
11421
11422 for (i = 0; i < lh->num_file_names; i++)
11423 {
11424 char *dir = NULL;
11425 struct file_entry *fe;
11426
11427 fe = &lh->file_names[i];
11428 if (fe->dir_index)
11429 dir = lh->include_dirs[fe->dir_index - 1];
11430 dwarf2_start_subfile (fe->name, dir, comp_dir);
11431
11432 /* Skip the main file; we don't need it, and it must be
11433 allocated last, so that it will show up before the
11434 non-primary symtabs in the objfile's symtab list. */
11435 if (current_subfile == first_subfile)
11436 continue;
11437
11438 if (current_subfile->symtab == NULL)
11439 current_subfile->symtab = allocate_symtab (current_subfile->name,
11440 objfile);
11441 fe->symtab = current_subfile->symtab;
11442 }
11443 }
11444 }
11445
11446 /* Start a subfile for DWARF. FILENAME is the name of the file and
11447 DIRNAME the name of the source directory which contains FILENAME
11448 or NULL if not known. COMP_DIR is the compilation directory for the
11449 linetable's compilation unit or NULL if not known.
11450 This routine tries to keep line numbers from identical absolute and
11451 relative file names in a common subfile.
11452
11453 Using the `list' example from the GDB testsuite, which resides in
11454 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11455 of /srcdir/list0.c yields the following debugging information for list0.c:
11456
11457 DW_AT_name: /srcdir/list0.c
11458 DW_AT_comp_dir: /compdir
11459 files.files[0].name: list0.h
11460 files.files[0].dir: /srcdir
11461 files.files[1].name: list0.c
11462 files.files[1].dir: /srcdir
11463
11464 The line number information for list0.c has to end up in a single
11465 subfile, so that `break /srcdir/list0.c:1' works as expected.
11466 start_subfile will ensure that this happens provided that we pass the
11467 concatenation of files.files[1].dir and files.files[1].name as the
11468 subfile's name. */
11469
11470 static void
11471 dwarf2_start_subfile (char *filename, const char *dirname,
11472 const char *comp_dir)
11473 {
11474 char *fullname;
11475
11476 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11477 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11478 second argument to start_subfile. To be consistent, we do the
11479 same here. In order not to lose the line information directory,
11480 we concatenate it to the filename when it makes sense.
11481 Note that the Dwarf3 standard says (speaking of filenames in line
11482 information): ``The directory index is ignored for file names
11483 that represent full path names''. Thus ignoring dirname in the
11484 `else' branch below isn't an issue. */
11485
11486 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11487 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11488 else
11489 fullname = filename;
11490
11491 start_subfile (fullname, comp_dir);
11492
11493 if (fullname != filename)
11494 xfree (fullname);
11495 }
11496
11497 static void
11498 var_decode_location (struct attribute *attr, struct symbol *sym,
11499 struct dwarf2_cu *cu)
11500 {
11501 struct objfile *objfile = cu->objfile;
11502 struct comp_unit_head *cu_header = &cu->header;
11503
11504 /* NOTE drow/2003-01-30: There used to be a comment and some special
11505 code here to turn a symbol with DW_AT_external and a
11506 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11507 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11508 with some versions of binutils) where shared libraries could have
11509 relocations against symbols in their debug information - the
11510 minimal symbol would have the right address, but the debug info
11511 would not. It's no longer necessary, because we will explicitly
11512 apply relocations when we read in the debug information now. */
11513
11514 /* A DW_AT_location attribute with no contents indicates that a
11515 variable has been optimized away. */
11516 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11517 {
11518 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11519 return;
11520 }
11521
11522 /* Handle one degenerate form of location expression specially, to
11523 preserve GDB's previous behavior when section offsets are
11524 specified. If this is just a DW_OP_addr then mark this symbol
11525 as LOC_STATIC. */
11526
11527 if (attr_form_is_block (attr)
11528 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11529 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11530 {
11531 unsigned int dummy;
11532
11533 SYMBOL_VALUE_ADDRESS (sym) =
11534 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11535 SYMBOL_CLASS (sym) = LOC_STATIC;
11536 fixup_symbol_section (sym, objfile);
11537 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11538 SYMBOL_SECTION (sym));
11539 return;
11540 }
11541
11542 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11543 expression evaluator, and use LOC_COMPUTED only when necessary
11544 (i.e. when the value of a register or memory location is
11545 referenced, or a thread-local block, etc.). Then again, it might
11546 not be worthwhile. I'm assuming that it isn't unless performance
11547 or memory numbers show me otherwise. */
11548
11549 dwarf2_symbol_mark_computed (attr, sym, cu);
11550 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11551
11552 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11553 cu->has_loclist = 1;
11554 }
11555
11556 /* Given a pointer to a DWARF information entry, figure out if we need
11557 to make a symbol table entry for it, and if so, create a new entry
11558 and return a pointer to it.
11559 If TYPE is NULL, determine symbol type from the die, otherwise
11560 used the passed type.
11561 If SPACE is not NULL, use it to hold the new symbol. If it is
11562 NULL, allocate a new symbol on the objfile's obstack. */
11563
11564 static struct symbol *
11565 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11566 struct symbol *space)
11567 {
11568 struct objfile *objfile = cu->objfile;
11569 struct symbol *sym = NULL;
11570 char *name;
11571 struct attribute *attr = NULL;
11572 struct attribute *attr2 = NULL;
11573 CORE_ADDR baseaddr;
11574 struct pending **list_to_add = NULL;
11575
11576 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11577
11578 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11579
11580 name = dwarf2_name (die, cu);
11581 if (name)
11582 {
11583 const char *linkagename;
11584 int suppress_add = 0;
11585
11586 if (space)
11587 sym = space;
11588 else
11589 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11590 OBJSTAT (objfile, n_syms++);
11591
11592 /* Cache this symbol's name and the name's demangled form (if any). */
11593 SYMBOL_SET_LANGUAGE (sym, cu->language);
11594 linkagename = dwarf2_physname (name, die, cu);
11595 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11596
11597 /* Fortran does not have mangling standard and the mangling does differ
11598 between gfortran, iFort etc. */
11599 if (cu->language == language_fortran
11600 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11601 symbol_set_demangled_name (&(sym->ginfo),
11602 (char *) dwarf2_full_name (name, die, cu),
11603 NULL);
11604
11605 /* Default assumptions.
11606 Use the passed type or decode it from the die. */
11607 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11608 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11609 if (type != NULL)
11610 SYMBOL_TYPE (sym) = type;
11611 else
11612 SYMBOL_TYPE (sym) = die_type (die, cu);
11613 attr = dwarf2_attr (die,
11614 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11615 cu);
11616 if (attr)
11617 {
11618 SYMBOL_LINE (sym) = DW_UNSND (attr);
11619 }
11620
11621 attr = dwarf2_attr (die,
11622 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11623 cu);
11624 if (attr)
11625 {
11626 int file_index = DW_UNSND (attr);
11627
11628 if (cu->line_header == NULL
11629 || file_index > cu->line_header->num_file_names)
11630 complaint (&symfile_complaints,
11631 _("file index out of range"));
11632 else if (file_index > 0)
11633 {
11634 struct file_entry *fe;
11635
11636 fe = &cu->line_header->file_names[file_index - 1];
11637 SYMBOL_SYMTAB (sym) = fe->symtab;
11638 }
11639 }
11640
11641 switch (die->tag)
11642 {
11643 case DW_TAG_label:
11644 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11645 if (attr)
11646 {
11647 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11648 }
11649 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11650 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11651 SYMBOL_CLASS (sym) = LOC_LABEL;
11652 add_symbol_to_list (sym, cu->list_in_scope);
11653 break;
11654 case DW_TAG_subprogram:
11655 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11656 finish_block. */
11657 SYMBOL_CLASS (sym) = LOC_BLOCK;
11658 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11659 if ((attr2 && (DW_UNSND (attr2) != 0))
11660 || cu->language == language_ada)
11661 {
11662 /* Subprograms marked external are stored as a global symbol.
11663 Ada subprograms, whether marked external or not, are always
11664 stored as a global symbol, because we want to be able to
11665 access them globally. For instance, we want to be able
11666 to break on a nested subprogram without having to
11667 specify the context. */
11668 list_to_add = &global_symbols;
11669 }
11670 else
11671 {
11672 list_to_add = cu->list_in_scope;
11673 }
11674 break;
11675 case DW_TAG_inlined_subroutine:
11676 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11677 finish_block. */
11678 SYMBOL_CLASS (sym) = LOC_BLOCK;
11679 SYMBOL_INLINED (sym) = 1;
11680 /* Do not add the symbol to any lists. It will be found via
11681 BLOCK_FUNCTION from the blockvector. */
11682 break;
11683 case DW_TAG_template_value_param:
11684 suppress_add = 1;
11685 /* Fall through. */
11686 case DW_TAG_constant:
11687 case DW_TAG_variable:
11688 case DW_TAG_member:
11689 /* Compilation with minimal debug info may result in
11690 variables with missing type entries. Change the
11691 misleading `void' type to something sensible. */
11692 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11693 SYMBOL_TYPE (sym)
11694 = objfile_type (objfile)->nodebug_data_symbol;
11695
11696 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11697 /* In the case of DW_TAG_member, we should only be called for
11698 static const members. */
11699 if (die->tag == DW_TAG_member)
11700 {
11701 /* dwarf2_add_field uses die_is_declaration,
11702 so we do the same. */
11703 gdb_assert (die_is_declaration (die, cu));
11704 gdb_assert (attr);
11705 }
11706 if (attr)
11707 {
11708 dwarf2_const_value (attr, sym, cu);
11709 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11710 if (!suppress_add)
11711 {
11712 if (attr2 && (DW_UNSND (attr2) != 0))
11713 list_to_add = &global_symbols;
11714 else
11715 list_to_add = cu->list_in_scope;
11716 }
11717 break;
11718 }
11719 attr = dwarf2_attr (die, DW_AT_location, cu);
11720 if (attr)
11721 {
11722 var_decode_location (attr, sym, cu);
11723 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11724 if (SYMBOL_CLASS (sym) == LOC_STATIC
11725 && SYMBOL_VALUE_ADDRESS (sym) == 0
11726 && !dwarf2_per_objfile->has_section_at_zero)
11727 {
11728 /* When a static variable is eliminated by the linker,
11729 the corresponding debug information is not stripped
11730 out, but the variable address is set to null;
11731 do not add such variables into symbol table. */
11732 }
11733 else if (attr2 && (DW_UNSND (attr2) != 0))
11734 {
11735 /* Workaround gfortran PR debug/40040 - it uses
11736 DW_AT_location for variables in -fPIC libraries which may
11737 get overriden by other libraries/executable and get
11738 a different address. Resolve it by the minimal symbol
11739 which may come from inferior's executable using copy
11740 relocation. Make this workaround only for gfortran as for
11741 other compilers GDB cannot guess the minimal symbol
11742 Fortran mangling kind. */
11743 if (cu->language == language_fortran && die->parent
11744 && die->parent->tag == DW_TAG_module
11745 && cu->producer
11746 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11747 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11748
11749 /* A variable with DW_AT_external is never static,
11750 but it may be block-scoped. */
11751 list_to_add = (cu->list_in_scope == &file_symbols
11752 ? &global_symbols : cu->list_in_scope);
11753 }
11754 else
11755 list_to_add = cu->list_in_scope;
11756 }
11757 else
11758 {
11759 /* We do not know the address of this symbol.
11760 If it is an external symbol and we have type information
11761 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11762 The address of the variable will then be determined from
11763 the minimal symbol table whenever the variable is
11764 referenced. */
11765 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11766 if (attr2 && (DW_UNSND (attr2) != 0)
11767 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11768 {
11769 /* A variable with DW_AT_external is never static, but it
11770 may be block-scoped. */
11771 list_to_add = (cu->list_in_scope == &file_symbols
11772 ? &global_symbols : cu->list_in_scope);
11773
11774 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11775 }
11776 else if (!die_is_declaration (die, cu))
11777 {
11778 /* Use the default LOC_OPTIMIZED_OUT class. */
11779 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11780 if (!suppress_add)
11781 list_to_add = cu->list_in_scope;
11782 }
11783 }
11784 break;
11785 case DW_TAG_formal_parameter:
11786 /* If we are inside a function, mark this as an argument. If
11787 not, we might be looking at an argument to an inlined function
11788 when we do not have enough information to show inlined frames;
11789 pretend it's a local variable in that case so that the user can
11790 still see it. */
11791 if (context_stack_depth > 0
11792 && context_stack[context_stack_depth - 1].name != NULL)
11793 SYMBOL_IS_ARGUMENT (sym) = 1;
11794 attr = dwarf2_attr (die, DW_AT_location, cu);
11795 if (attr)
11796 {
11797 var_decode_location (attr, sym, cu);
11798 }
11799 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11800 if (attr)
11801 {
11802 dwarf2_const_value (attr, sym, cu);
11803 }
11804
11805 list_to_add = cu->list_in_scope;
11806 break;
11807 case DW_TAG_unspecified_parameters:
11808 /* From varargs functions; gdb doesn't seem to have any
11809 interest in this information, so just ignore it for now.
11810 (FIXME?) */
11811 break;
11812 case DW_TAG_template_type_param:
11813 suppress_add = 1;
11814 /* Fall through. */
11815 case DW_TAG_class_type:
11816 case DW_TAG_interface_type:
11817 case DW_TAG_structure_type:
11818 case DW_TAG_union_type:
11819 case DW_TAG_set_type:
11820 case DW_TAG_enumeration_type:
11821 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11822 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11823
11824 {
11825 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11826 really ever be static objects: otherwise, if you try
11827 to, say, break of a class's method and you're in a file
11828 which doesn't mention that class, it won't work unless
11829 the check for all static symbols in lookup_symbol_aux
11830 saves you. See the OtherFileClass tests in
11831 gdb.c++/namespace.exp. */
11832
11833 if (!suppress_add)
11834 {
11835 list_to_add = (cu->list_in_scope == &file_symbols
11836 && (cu->language == language_cplus
11837 || cu->language == language_java)
11838 ? &global_symbols : cu->list_in_scope);
11839
11840 /* The semantics of C++ state that "struct foo {
11841 ... }" also defines a typedef for "foo". A Java
11842 class declaration also defines a typedef for the
11843 class. */
11844 if (cu->language == language_cplus
11845 || cu->language == language_java
11846 || cu->language == language_ada)
11847 {
11848 /* The symbol's name is already allocated along
11849 with this objfile, so we don't need to
11850 duplicate it for the type. */
11851 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11852 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11853 }
11854 }
11855 }
11856 break;
11857 case DW_TAG_typedef:
11858 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11859 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11860 list_to_add = cu->list_in_scope;
11861 break;
11862 case DW_TAG_base_type:
11863 case DW_TAG_subrange_type:
11864 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11865 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11866 list_to_add = cu->list_in_scope;
11867 break;
11868 case DW_TAG_enumerator:
11869 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11870 if (attr)
11871 {
11872 dwarf2_const_value (attr, sym, cu);
11873 }
11874 {
11875 /* NOTE: carlton/2003-11-10: See comment above in the
11876 DW_TAG_class_type, etc. block. */
11877
11878 list_to_add = (cu->list_in_scope == &file_symbols
11879 && (cu->language == language_cplus
11880 || cu->language == language_java)
11881 ? &global_symbols : cu->list_in_scope);
11882 }
11883 break;
11884 case DW_TAG_namespace:
11885 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11886 list_to_add = &global_symbols;
11887 break;
11888 default:
11889 /* Not a tag we recognize. Hopefully we aren't processing
11890 trash data, but since we must specifically ignore things
11891 we don't recognize, there is nothing else we should do at
11892 this point. */
11893 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11894 dwarf_tag_name (die->tag));
11895 break;
11896 }
11897
11898 if (suppress_add)
11899 {
11900 sym->hash_next = objfile->template_symbols;
11901 objfile->template_symbols = sym;
11902 list_to_add = NULL;
11903 }
11904
11905 if (list_to_add != NULL)
11906 add_symbol_to_list (sym, list_to_add);
11907
11908 /* For the benefit of old versions of GCC, check for anonymous
11909 namespaces based on the demangled name. */
11910 if (!processing_has_namespace_info
11911 && cu->language == language_cplus)
11912 cp_scan_for_anonymous_namespaces (sym, objfile);
11913 }
11914 return (sym);
11915 }
11916
11917 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11918
11919 static struct symbol *
11920 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11921 {
11922 return new_symbol_full (die, type, cu, NULL);
11923 }
11924
11925 /* Given an attr with a DW_FORM_dataN value in host byte order,
11926 zero-extend it as appropriate for the symbol's type. The DWARF
11927 standard (v4) is not entirely clear about the meaning of using
11928 DW_FORM_dataN for a constant with a signed type, where the type is
11929 wider than the data. The conclusion of a discussion on the DWARF
11930 list was that this is unspecified. We choose to always zero-extend
11931 because that is the interpretation long in use by GCC. */
11932
11933 static gdb_byte *
11934 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11935 const char *name, struct obstack *obstack,
11936 struct dwarf2_cu *cu, long *value, int bits)
11937 {
11938 struct objfile *objfile = cu->objfile;
11939 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11940 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11941 LONGEST l = DW_UNSND (attr);
11942
11943 if (bits < sizeof (*value) * 8)
11944 {
11945 l &= ((LONGEST) 1 << bits) - 1;
11946 *value = l;
11947 }
11948 else if (bits == sizeof (*value) * 8)
11949 *value = l;
11950 else
11951 {
11952 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11953 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11954 return bytes;
11955 }
11956
11957 return NULL;
11958 }
11959
11960 /* Read a constant value from an attribute. Either set *VALUE, or if
11961 the value does not fit in *VALUE, set *BYTES - either already
11962 allocated on the objfile obstack, or newly allocated on OBSTACK,
11963 or, set *BATON, if we translated the constant to a location
11964 expression. */
11965
11966 static void
11967 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11968 const char *name, struct obstack *obstack,
11969 struct dwarf2_cu *cu,
11970 long *value, gdb_byte **bytes,
11971 struct dwarf2_locexpr_baton **baton)
11972 {
11973 struct objfile *objfile = cu->objfile;
11974 struct comp_unit_head *cu_header = &cu->header;
11975 struct dwarf_block *blk;
11976 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11977 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11978
11979 *value = 0;
11980 *bytes = NULL;
11981 *baton = NULL;
11982
11983 switch (attr->form)
11984 {
11985 case DW_FORM_addr:
11986 {
11987 gdb_byte *data;
11988
11989 if (TYPE_LENGTH (type) != cu_header->addr_size)
11990 dwarf2_const_value_length_mismatch_complaint (name,
11991 cu_header->addr_size,
11992 TYPE_LENGTH (type));
11993 /* Symbols of this form are reasonably rare, so we just
11994 piggyback on the existing location code rather than writing
11995 a new implementation of symbol_computed_ops. */
11996 *baton = obstack_alloc (&objfile->objfile_obstack,
11997 sizeof (struct dwarf2_locexpr_baton));
11998 (*baton)->per_cu = cu->per_cu;
11999 gdb_assert ((*baton)->per_cu);
12000
12001 (*baton)->size = 2 + cu_header->addr_size;
12002 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12003 (*baton)->data = data;
12004
12005 data[0] = DW_OP_addr;
12006 store_unsigned_integer (&data[1], cu_header->addr_size,
12007 byte_order, DW_ADDR (attr));
12008 data[cu_header->addr_size + 1] = DW_OP_stack_value;
12009 }
12010 break;
12011 case DW_FORM_string:
12012 case DW_FORM_strp:
12013 /* DW_STRING is already allocated on the objfile obstack, point
12014 directly to it. */
12015 *bytes = (gdb_byte *) DW_STRING (attr);
12016 break;
12017 case DW_FORM_block1:
12018 case DW_FORM_block2:
12019 case DW_FORM_block4:
12020 case DW_FORM_block:
12021 case DW_FORM_exprloc:
12022 blk = DW_BLOCK (attr);
12023 if (TYPE_LENGTH (type) != blk->size)
12024 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12025 TYPE_LENGTH (type));
12026 *bytes = blk->data;
12027 break;
12028
12029 /* The DW_AT_const_value attributes are supposed to carry the
12030 symbol's value "represented as it would be on the target
12031 architecture." By the time we get here, it's already been
12032 converted to host endianness, so we just need to sign- or
12033 zero-extend it as appropriate. */
12034 case DW_FORM_data1:
12035 *bytes = dwarf2_const_value_data (attr, type, name,
12036 obstack, cu, value, 8);
12037 break;
12038 case DW_FORM_data2:
12039 *bytes = dwarf2_const_value_data (attr, type, name,
12040 obstack, cu, value, 16);
12041 break;
12042 case DW_FORM_data4:
12043 *bytes = dwarf2_const_value_data (attr, type, name,
12044 obstack, cu, value, 32);
12045 break;
12046 case DW_FORM_data8:
12047 *bytes = dwarf2_const_value_data (attr, type, name,
12048 obstack, cu, value, 64);
12049 break;
12050
12051 case DW_FORM_sdata:
12052 *value = DW_SND (attr);
12053 break;
12054
12055 case DW_FORM_udata:
12056 *value = DW_UNSND (attr);
12057 break;
12058
12059 default:
12060 complaint (&symfile_complaints,
12061 _("unsupported const value attribute form: '%s'"),
12062 dwarf_form_name (attr->form));
12063 *value = 0;
12064 break;
12065 }
12066 }
12067
12068
12069 /* Copy constant value from an attribute to a symbol. */
12070
12071 static void
12072 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12073 struct dwarf2_cu *cu)
12074 {
12075 struct objfile *objfile = cu->objfile;
12076 struct comp_unit_head *cu_header = &cu->header;
12077 long value;
12078 gdb_byte *bytes;
12079 struct dwarf2_locexpr_baton *baton;
12080
12081 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12082 SYMBOL_PRINT_NAME (sym),
12083 &objfile->objfile_obstack, cu,
12084 &value, &bytes, &baton);
12085
12086 if (baton != NULL)
12087 {
12088 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12089 SYMBOL_LOCATION_BATON (sym) = baton;
12090 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12091 }
12092 else if (bytes != NULL)
12093 {
12094 SYMBOL_VALUE_BYTES (sym) = bytes;
12095 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12096 }
12097 else
12098 {
12099 SYMBOL_VALUE (sym) = value;
12100 SYMBOL_CLASS (sym) = LOC_CONST;
12101 }
12102 }
12103
12104 /* Return the type of the die in question using its DW_AT_type attribute. */
12105
12106 static struct type *
12107 die_type (struct die_info *die, struct dwarf2_cu *cu)
12108 {
12109 struct attribute *type_attr;
12110
12111 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12112 if (!type_attr)
12113 {
12114 /* A missing DW_AT_type represents a void type. */
12115 return objfile_type (cu->objfile)->builtin_void;
12116 }
12117
12118 return lookup_die_type (die, type_attr, cu);
12119 }
12120
12121 /* True iff CU's producer generates GNAT Ada auxiliary information
12122 that allows to find parallel types through that information instead
12123 of having to do expensive parallel lookups by type name. */
12124
12125 static int
12126 need_gnat_info (struct dwarf2_cu *cu)
12127 {
12128 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12129 of GNAT produces this auxiliary information, without any indication
12130 that it is produced. Part of enhancing the FSF version of GNAT
12131 to produce that information will be to put in place an indicator
12132 that we can use in order to determine whether the descriptive type
12133 info is available or not. One suggestion that has been made is
12134 to use a new attribute, attached to the CU die. For now, assume
12135 that the descriptive type info is not available. */
12136 return 0;
12137 }
12138
12139 /* Return the auxiliary type of the die in question using its
12140 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12141 attribute is not present. */
12142
12143 static struct type *
12144 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12145 {
12146 struct attribute *type_attr;
12147
12148 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12149 if (!type_attr)
12150 return NULL;
12151
12152 return lookup_die_type (die, type_attr, cu);
12153 }
12154
12155 /* If DIE has a descriptive_type attribute, then set the TYPE's
12156 descriptive type accordingly. */
12157
12158 static void
12159 set_descriptive_type (struct type *type, struct die_info *die,
12160 struct dwarf2_cu *cu)
12161 {
12162 struct type *descriptive_type = die_descriptive_type (die, cu);
12163
12164 if (descriptive_type)
12165 {
12166 ALLOCATE_GNAT_AUX_TYPE (type);
12167 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12168 }
12169 }
12170
12171 /* Return the containing type of the die in question using its
12172 DW_AT_containing_type attribute. */
12173
12174 static struct type *
12175 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12176 {
12177 struct attribute *type_attr;
12178
12179 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12180 if (!type_attr)
12181 error (_("Dwarf Error: Problem turning containing type into gdb type "
12182 "[in module %s]"), cu->objfile->name);
12183
12184 return lookup_die_type (die, type_attr, cu);
12185 }
12186
12187 /* Look up the type of DIE in CU using its type attribute ATTR.
12188 If there is no type substitute an error marker. */
12189
12190 static struct type *
12191 lookup_die_type (struct die_info *die, struct attribute *attr,
12192 struct dwarf2_cu *cu)
12193 {
12194 struct objfile *objfile = cu->objfile;
12195 struct type *this_type;
12196
12197 /* First see if we have it cached. */
12198
12199 if (is_ref_attr (attr))
12200 {
12201 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12202
12203 this_type = get_die_type_at_offset (offset, cu->per_cu);
12204 }
12205 else if (attr->form == DW_FORM_ref_sig8)
12206 {
12207 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12208 struct dwarf2_cu *sig_cu;
12209 unsigned int offset;
12210
12211 /* sig_type will be NULL if the signatured type is missing from
12212 the debug info. */
12213 if (sig_type == NULL)
12214 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12215 "at 0x%x [in module %s]"),
12216 die->offset, objfile->name);
12217
12218 gdb_assert (sig_type->per_cu.debug_types_section);
12219 offset = sig_type->per_cu.offset + sig_type->type_offset;
12220 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12221 }
12222 else
12223 {
12224 dump_die_for_error (die);
12225 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12226 dwarf_attr_name (attr->name), objfile->name);
12227 }
12228
12229 /* If not cached we need to read it in. */
12230
12231 if (this_type == NULL)
12232 {
12233 struct die_info *type_die;
12234 struct dwarf2_cu *type_cu = cu;
12235
12236 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12237 /* If the type is cached, we should have found it above. */
12238 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12239 this_type = read_type_die_1 (type_die, type_cu);
12240 }
12241
12242 /* If we still don't have a type use an error marker. */
12243
12244 if (this_type == NULL)
12245 {
12246 char *message, *saved;
12247
12248 /* read_type_die already issued a complaint. */
12249 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12250 objfile->name,
12251 cu->header.offset,
12252 die->offset);
12253 saved = obstack_copy0 (&objfile->objfile_obstack,
12254 message, strlen (message));
12255 xfree (message);
12256
12257 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12258 }
12259
12260 return this_type;
12261 }
12262
12263 /* Return the type in DIE, CU.
12264 Returns NULL for invalid types.
12265
12266 This first does a lookup in the appropriate type_hash table,
12267 and only reads the die in if necessary.
12268
12269 NOTE: This can be called when reading in partial or full symbols. */
12270
12271 static struct type *
12272 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12273 {
12274 struct type *this_type;
12275
12276 this_type = get_die_type (die, cu);
12277 if (this_type)
12278 return this_type;
12279
12280 return read_type_die_1 (die, cu);
12281 }
12282
12283 /* Read the type in DIE, CU.
12284 Returns NULL for invalid types. */
12285
12286 static struct type *
12287 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12288 {
12289 struct type *this_type = NULL;
12290
12291 switch (die->tag)
12292 {
12293 case DW_TAG_class_type:
12294 case DW_TAG_interface_type:
12295 case DW_TAG_structure_type:
12296 case DW_TAG_union_type:
12297 this_type = read_structure_type (die, cu);
12298 break;
12299 case DW_TAG_enumeration_type:
12300 this_type = read_enumeration_type (die, cu);
12301 break;
12302 case DW_TAG_subprogram:
12303 case DW_TAG_subroutine_type:
12304 case DW_TAG_inlined_subroutine:
12305 this_type = read_subroutine_type (die, cu);
12306 break;
12307 case DW_TAG_array_type:
12308 this_type = read_array_type (die, cu);
12309 break;
12310 case DW_TAG_set_type:
12311 this_type = read_set_type (die, cu);
12312 break;
12313 case DW_TAG_pointer_type:
12314 this_type = read_tag_pointer_type (die, cu);
12315 break;
12316 case DW_TAG_ptr_to_member_type:
12317 this_type = read_tag_ptr_to_member_type (die, cu);
12318 break;
12319 case DW_TAG_reference_type:
12320 this_type = read_tag_reference_type (die, cu);
12321 break;
12322 case DW_TAG_const_type:
12323 this_type = read_tag_const_type (die, cu);
12324 break;
12325 case DW_TAG_volatile_type:
12326 this_type = read_tag_volatile_type (die, cu);
12327 break;
12328 case DW_TAG_string_type:
12329 this_type = read_tag_string_type (die, cu);
12330 break;
12331 case DW_TAG_typedef:
12332 this_type = read_typedef (die, cu);
12333 break;
12334 case DW_TAG_subrange_type:
12335 this_type = read_subrange_type (die, cu);
12336 break;
12337 case DW_TAG_base_type:
12338 this_type = read_base_type (die, cu);
12339 break;
12340 case DW_TAG_unspecified_type:
12341 this_type = read_unspecified_type (die, cu);
12342 break;
12343 case DW_TAG_namespace:
12344 this_type = read_namespace_type (die, cu);
12345 break;
12346 case DW_TAG_module:
12347 this_type = read_module_type (die, cu);
12348 break;
12349 default:
12350 complaint (&symfile_complaints,
12351 _("unexpected tag in read_type_die: '%s'"),
12352 dwarf_tag_name (die->tag));
12353 break;
12354 }
12355
12356 return this_type;
12357 }
12358
12359 /* See if we can figure out if the class lives in a namespace. We do
12360 this by looking for a member function; its demangled name will
12361 contain namespace info, if there is any.
12362 Return the computed name or NULL.
12363 Space for the result is allocated on the objfile's obstack.
12364 This is the full-die version of guess_partial_die_structure_name.
12365 In this case we know DIE has no useful parent. */
12366
12367 static char *
12368 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12369 {
12370 struct die_info *spec_die;
12371 struct dwarf2_cu *spec_cu;
12372 struct die_info *child;
12373
12374 spec_cu = cu;
12375 spec_die = die_specification (die, &spec_cu);
12376 if (spec_die != NULL)
12377 {
12378 die = spec_die;
12379 cu = spec_cu;
12380 }
12381
12382 for (child = die->child;
12383 child != NULL;
12384 child = child->sibling)
12385 {
12386 if (child->tag == DW_TAG_subprogram)
12387 {
12388 struct attribute *attr;
12389
12390 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12391 if (attr == NULL)
12392 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12393 if (attr != NULL)
12394 {
12395 char *actual_name
12396 = language_class_name_from_physname (cu->language_defn,
12397 DW_STRING (attr));
12398 char *name = NULL;
12399
12400 if (actual_name != NULL)
12401 {
12402 char *die_name = dwarf2_name (die, cu);
12403
12404 if (die_name != NULL
12405 && strcmp (die_name, actual_name) != 0)
12406 {
12407 /* Strip off the class name from the full name.
12408 We want the prefix. */
12409 int die_name_len = strlen (die_name);
12410 int actual_name_len = strlen (actual_name);
12411
12412 /* Test for '::' as a sanity check. */
12413 if (actual_name_len > die_name_len + 2
12414 && actual_name[actual_name_len
12415 - die_name_len - 1] == ':')
12416 name =
12417 obsavestring (actual_name,
12418 actual_name_len - die_name_len - 2,
12419 &cu->objfile->objfile_obstack);
12420 }
12421 }
12422 xfree (actual_name);
12423 return name;
12424 }
12425 }
12426 }
12427
12428 return NULL;
12429 }
12430
12431 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12432 prefix part in such case. See
12433 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12434
12435 static char *
12436 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12437 {
12438 struct attribute *attr;
12439 char *base;
12440
12441 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12442 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12443 return NULL;
12444
12445 attr = dwarf2_attr (die, DW_AT_name, cu);
12446 if (attr != NULL && DW_STRING (attr) != NULL)
12447 return NULL;
12448
12449 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12450 if (attr == NULL)
12451 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12452 if (attr == NULL || DW_STRING (attr) == NULL)
12453 return NULL;
12454
12455 /* dwarf2_name had to be already called. */
12456 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12457
12458 /* Strip the base name, keep any leading namespaces/classes. */
12459 base = strrchr (DW_STRING (attr), ':');
12460 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12461 return "";
12462
12463 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12464 &cu->objfile->objfile_obstack);
12465 }
12466
12467 /* Return the name of the namespace/class that DIE is defined within,
12468 or "" if we can't tell. The caller should not xfree the result.
12469
12470 For example, if we're within the method foo() in the following
12471 code:
12472
12473 namespace N {
12474 class C {
12475 void foo () {
12476 }
12477 };
12478 }
12479
12480 then determine_prefix on foo's die will return "N::C". */
12481
12482 static char *
12483 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12484 {
12485 struct die_info *parent, *spec_die;
12486 struct dwarf2_cu *spec_cu;
12487 struct type *parent_type;
12488 char *retval;
12489
12490 if (cu->language != language_cplus && cu->language != language_java
12491 && cu->language != language_fortran)
12492 return "";
12493
12494 retval = anonymous_struct_prefix (die, cu);
12495 if (retval)
12496 return retval;
12497
12498 /* We have to be careful in the presence of DW_AT_specification.
12499 For example, with GCC 3.4, given the code
12500
12501 namespace N {
12502 void foo() {
12503 // Definition of N::foo.
12504 }
12505 }
12506
12507 then we'll have a tree of DIEs like this:
12508
12509 1: DW_TAG_compile_unit
12510 2: DW_TAG_namespace // N
12511 3: DW_TAG_subprogram // declaration of N::foo
12512 4: DW_TAG_subprogram // definition of N::foo
12513 DW_AT_specification // refers to die #3
12514
12515 Thus, when processing die #4, we have to pretend that we're in
12516 the context of its DW_AT_specification, namely the contex of die
12517 #3. */
12518 spec_cu = cu;
12519 spec_die = die_specification (die, &spec_cu);
12520 if (spec_die == NULL)
12521 parent = die->parent;
12522 else
12523 {
12524 parent = spec_die->parent;
12525 cu = spec_cu;
12526 }
12527
12528 if (parent == NULL)
12529 return "";
12530 else if (parent->building_fullname)
12531 {
12532 const char *name;
12533 const char *parent_name;
12534
12535 /* It has been seen on RealView 2.2 built binaries,
12536 DW_TAG_template_type_param types actually _defined_ as
12537 children of the parent class:
12538
12539 enum E {};
12540 template class <class Enum> Class{};
12541 Class<enum E> class_e;
12542
12543 1: DW_TAG_class_type (Class)
12544 2: DW_TAG_enumeration_type (E)
12545 3: DW_TAG_enumerator (enum1:0)
12546 3: DW_TAG_enumerator (enum2:1)
12547 ...
12548 2: DW_TAG_template_type_param
12549 DW_AT_type DW_FORM_ref_udata (E)
12550
12551 Besides being broken debug info, it can put GDB into an
12552 infinite loop. Consider:
12553
12554 When we're building the full name for Class<E>, we'll start
12555 at Class, and go look over its template type parameters,
12556 finding E. We'll then try to build the full name of E, and
12557 reach here. We're now trying to build the full name of E,
12558 and look over the parent DIE for containing scope. In the
12559 broken case, if we followed the parent DIE of E, we'd again
12560 find Class, and once again go look at its template type
12561 arguments, etc., etc. Simply don't consider such parent die
12562 as source-level parent of this die (it can't be, the language
12563 doesn't allow it), and break the loop here. */
12564 name = dwarf2_name (die, cu);
12565 parent_name = dwarf2_name (parent, cu);
12566 complaint (&symfile_complaints,
12567 _("template param type '%s' defined within parent '%s'"),
12568 name ? name : "<unknown>",
12569 parent_name ? parent_name : "<unknown>");
12570 return "";
12571 }
12572 else
12573 switch (parent->tag)
12574 {
12575 case DW_TAG_namespace:
12576 parent_type = read_type_die (parent, cu);
12577 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12578 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12579 Work around this problem here. */
12580 if (cu->language == language_cplus
12581 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12582 return "";
12583 /* We give a name to even anonymous namespaces. */
12584 return TYPE_TAG_NAME (parent_type);
12585 case DW_TAG_class_type:
12586 case DW_TAG_interface_type:
12587 case DW_TAG_structure_type:
12588 case DW_TAG_union_type:
12589 case DW_TAG_module:
12590 parent_type = read_type_die (parent, cu);
12591 if (TYPE_TAG_NAME (parent_type) != NULL)
12592 return TYPE_TAG_NAME (parent_type);
12593 else
12594 /* An anonymous structure is only allowed non-static data
12595 members; no typedefs, no member functions, et cetera.
12596 So it does not need a prefix. */
12597 return "";
12598 case DW_TAG_compile_unit:
12599 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12600 if (cu->language == language_cplus
12601 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12602 && die->child != NULL
12603 && (die->tag == DW_TAG_class_type
12604 || die->tag == DW_TAG_structure_type
12605 || die->tag == DW_TAG_union_type))
12606 {
12607 char *name = guess_full_die_structure_name (die, cu);
12608 if (name != NULL)
12609 return name;
12610 }
12611 return "";
12612 default:
12613 return determine_prefix (parent, cu);
12614 }
12615 }
12616
12617 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12618 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12619 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12620 an obconcat, otherwise allocate storage for the result. The CU argument is
12621 used to determine the language and hence, the appropriate separator. */
12622
12623 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12624
12625 static char *
12626 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12627 int physname, struct dwarf2_cu *cu)
12628 {
12629 const char *lead = "";
12630 const char *sep;
12631
12632 if (suffix == NULL || suffix[0] == '\0'
12633 || prefix == NULL || prefix[0] == '\0')
12634 sep = "";
12635 else if (cu->language == language_java)
12636 sep = ".";
12637 else if (cu->language == language_fortran && physname)
12638 {
12639 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12640 DW_AT_MIPS_linkage_name is preferred and used instead. */
12641
12642 lead = "__";
12643 sep = "_MOD_";
12644 }
12645 else
12646 sep = "::";
12647
12648 if (prefix == NULL)
12649 prefix = "";
12650 if (suffix == NULL)
12651 suffix = "";
12652
12653 if (obs == NULL)
12654 {
12655 char *retval
12656 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12657
12658 strcpy (retval, lead);
12659 strcat (retval, prefix);
12660 strcat (retval, sep);
12661 strcat (retval, suffix);
12662 return retval;
12663 }
12664 else
12665 {
12666 /* We have an obstack. */
12667 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12668 }
12669 }
12670
12671 /* Return sibling of die, NULL if no sibling. */
12672
12673 static struct die_info *
12674 sibling_die (struct die_info *die)
12675 {
12676 return die->sibling;
12677 }
12678
12679 /* Get name of a die, return NULL if not found. */
12680
12681 static char *
12682 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12683 struct obstack *obstack)
12684 {
12685 if (name && cu->language == language_cplus)
12686 {
12687 char *canon_name = cp_canonicalize_string (name);
12688
12689 if (canon_name != NULL)
12690 {
12691 if (strcmp (canon_name, name) != 0)
12692 name = obsavestring (canon_name, strlen (canon_name),
12693 obstack);
12694 xfree (canon_name);
12695 }
12696 }
12697
12698 return name;
12699 }
12700
12701 /* Get name of a die, return NULL if not found. */
12702
12703 static char *
12704 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12705 {
12706 struct attribute *attr;
12707
12708 attr = dwarf2_attr (die, DW_AT_name, cu);
12709 if ((!attr || !DW_STRING (attr))
12710 && die->tag != DW_TAG_class_type
12711 && die->tag != DW_TAG_interface_type
12712 && die->tag != DW_TAG_structure_type
12713 && die->tag != DW_TAG_union_type)
12714 return NULL;
12715
12716 switch (die->tag)
12717 {
12718 case DW_TAG_compile_unit:
12719 /* Compilation units have a DW_AT_name that is a filename, not
12720 a source language identifier. */
12721 case DW_TAG_enumeration_type:
12722 case DW_TAG_enumerator:
12723 /* These tags always have simple identifiers already; no need
12724 to canonicalize them. */
12725 return DW_STRING (attr);
12726
12727 case DW_TAG_subprogram:
12728 /* Java constructors will all be named "<init>", so return
12729 the class name when we see this special case. */
12730 if (cu->language == language_java
12731 && DW_STRING (attr) != NULL
12732 && strcmp (DW_STRING (attr), "<init>") == 0)
12733 {
12734 struct dwarf2_cu *spec_cu = cu;
12735 struct die_info *spec_die;
12736
12737 /* GCJ will output '<init>' for Java constructor names.
12738 For this special case, return the name of the parent class. */
12739
12740 /* GCJ may output suprogram DIEs with AT_specification set.
12741 If so, use the name of the specified DIE. */
12742 spec_die = die_specification (die, &spec_cu);
12743 if (spec_die != NULL)
12744 return dwarf2_name (spec_die, spec_cu);
12745
12746 do
12747 {
12748 die = die->parent;
12749 if (die->tag == DW_TAG_class_type)
12750 return dwarf2_name (die, cu);
12751 }
12752 while (die->tag != DW_TAG_compile_unit);
12753 }
12754 break;
12755
12756 case DW_TAG_class_type:
12757 case DW_TAG_interface_type:
12758 case DW_TAG_structure_type:
12759 case DW_TAG_union_type:
12760 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12761 structures or unions. These were of the form "._%d" in GCC 4.1,
12762 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12763 and GCC 4.4. We work around this problem by ignoring these. */
12764 if (attr && DW_STRING (attr)
12765 && (strncmp (DW_STRING (attr), "._", 2) == 0
12766 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12767 return NULL;
12768
12769 /* GCC might emit a nameless typedef that has a linkage name. See
12770 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12771 if (!attr || DW_STRING (attr) == NULL)
12772 {
12773 char *demangled = NULL;
12774
12775 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12776 if (attr == NULL)
12777 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12778
12779 if (attr == NULL || DW_STRING (attr) == NULL)
12780 return NULL;
12781
12782 /* Avoid demangling DW_STRING (attr) the second time on a second
12783 call for the same DIE. */
12784 if (!DW_STRING_IS_CANONICAL (attr))
12785 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12786
12787 if (demangled)
12788 {
12789 char *base;
12790
12791 /* FIXME: we already did this for the partial symbol... */
12792 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12793 &cu->objfile->objfile_obstack);
12794 DW_STRING_IS_CANONICAL (attr) = 1;
12795 xfree (demangled);
12796
12797 /* Strip any leading namespaces/classes, keep only the base name.
12798 DW_AT_name for named DIEs does not contain the prefixes. */
12799 base = strrchr (DW_STRING (attr), ':');
12800 if (base && base > DW_STRING (attr) && base[-1] == ':')
12801 return &base[1];
12802 else
12803 return DW_STRING (attr);
12804 }
12805 }
12806 break;
12807
12808 default:
12809 break;
12810 }
12811
12812 if (!DW_STRING_IS_CANONICAL (attr))
12813 {
12814 DW_STRING (attr)
12815 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12816 &cu->objfile->objfile_obstack);
12817 DW_STRING_IS_CANONICAL (attr) = 1;
12818 }
12819 return DW_STRING (attr);
12820 }
12821
12822 /* Return the die that this die in an extension of, or NULL if there
12823 is none. *EXT_CU is the CU containing DIE on input, and the CU
12824 containing the return value on output. */
12825
12826 static struct die_info *
12827 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12828 {
12829 struct attribute *attr;
12830
12831 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12832 if (attr == NULL)
12833 return NULL;
12834
12835 return follow_die_ref (die, attr, ext_cu);
12836 }
12837
12838 /* Convert a DIE tag into its string name. */
12839
12840 static char *
12841 dwarf_tag_name (unsigned tag)
12842 {
12843 switch (tag)
12844 {
12845 case DW_TAG_padding:
12846 return "DW_TAG_padding";
12847 case DW_TAG_array_type:
12848 return "DW_TAG_array_type";
12849 case DW_TAG_class_type:
12850 return "DW_TAG_class_type";
12851 case DW_TAG_entry_point:
12852 return "DW_TAG_entry_point";
12853 case DW_TAG_enumeration_type:
12854 return "DW_TAG_enumeration_type";
12855 case DW_TAG_formal_parameter:
12856 return "DW_TAG_formal_parameter";
12857 case DW_TAG_imported_declaration:
12858 return "DW_TAG_imported_declaration";
12859 case DW_TAG_label:
12860 return "DW_TAG_label";
12861 case DW_TAG_lexical_block:
12862 return "DW_TAG_lexical_block";
12863 case DW_TAG_member:
12864 return "DW_TAG_member";
12865 case DW_TAG_pointer_type:
12866 return "DW_TAG_pointer_type";
12867 case DW_TAG_reference_type:
12868 return "DW_TAG_reference_type";
12869 case DW_TAG_compile_unit:
12870 return "DW_TAG_compile_unit";
12871 case DW_TAG_string_type:
12872 return "DW_TAG_string_type";
12873 case DW_TAG_structure_type:
12874 return "DW_TAG_structure_type";
12875 case DW_TAG_subroutine_type:
12876 return "DW_TAG_subroutine_type";
12877 case DW_TAG_typedef:
12878 return "DW_TAG_typedef";
12879 case DW_TAG_union_type:
12880 return "DW_TAG_union_type";
12881 case DW_TAG_unspecified_parameters:
12882 return "DW_TAG_unspecified_parameters";
12883 case DW_TAG_variant:
12884 return "DW_TAG_variant";
12885 case DW_TAG_common_block:
12886 return "DW_TAG_common_block";
12887 case DW_TAG_common_inclusion:
12888 return "DW_TAG_common_inclusion";
12889 case DW_TAG_inheritance:
12890 return "DW_TAG_inheritance";
12891 case DW_TAG_inlined_subroutine:
12892 return "DW_TAG_inlined_subroutine";
12893 case DW_TAG_module:
12894 return "DW_TAG_module";
12895 case DW_TAG_ptr_to_member_type:
12896 return "DW_TAG_ptr_to_member_type";
12897 case DW_TAG_set_type:
12898 return "DW_TAG_set_type";
12899 case DW_TAG_subrange_type:
12900 return "DW_TAG_subrange_type";
12901 case DW_TAG_with_stmt:
12902 return "DW_TAG_with_stmt";
12903 case DW_TAG_access_declaration:
12904 return "DW_TAG_access_declaration";
12905 case DW_TAG_base_type:
12906 return "DW_TAG_base_type";
12907 case DW_TAG_catch_block:
12908 return "DW_TAG_catch_block";
12909 case DW_TAG_const_type:
12910 return "DW_TAG_const_type";
12911 case DW_TAG_constant:
12912 return "DW_TAG_constant";
12913 case DW_TAG_enumerator:
12914 return "DW_TAG_enumerator";
12915 case DW_TAG_file_type:
12916 return "DW_TAG_file_type";
12917 case DW_TAG_friend:
12918 return "DW_TAG_friend";
12919 case DW_TAG_namelist:
12920 return "DW_TAG_namelist";
12921 case DW_TAG_namelist_item:
12922 return "DW_TAG_namelist_item";
12923 case DW_TAG_packed_type:
12924 return "DW_TAG_packed_type";
12925 case DW_TAG_subprogram:
12926 return "DW_TAG_subprogram";
12927 case DW_TAG_template_type_param:
12928 return "DW_TAG_template_type_param";
12929 case DW_TAG_template_value_param:
12930 return "DW_TAG_template_value_param";
12931 case DW_TAG_thrown_type:
12932 return "DW_TAG_thrown_type";
12933 case DW_TAG_try_block:
12934 return "DW_TAG_try_block";
12935 case DW_TAG_variant_part:
12936 return "DW_TAG_variant_part";
12937 case DW_TAG_variable:
12938 return "DW_TAG_variable";
12939 case DW_TAG_volatile_type:
12940 return "DW_TAG_volatile_type";
12941 case DW_TAG_dwarf_procedure:
12942 return "DW_TAG_dwarf_procedure";
12943 case DW_TAG_restrict_type:
12944 return "DW_TAG_restrict_type";
12945 case DW_TAG_interface_type:
12946 return "DW_TAG_interface_type";
12947 case DW_TAG_namespace:
12948 return "DW_TAG_namespace";
12949 case DW_TAG_imported_module:
12950 return "DW_TAG_imported_module";
12951 case DW_TAG_unspecified_type:
12952 return "DW_TAG_unspecified_type";
12953 case DW_TAG_partial_unit:
12954 return "DW_TAG_partial_unit";
12955 case DW_TAG_imported_unit:
12956 return "DW_TAG_imported_unit";
12957 case DW_TAG_condition:
12958 return "DW_TAG_condition";
12959 case DW_TAG_shared_type:
12960 return "DW_TAG_shared_type";
12961 case DW_TAG_type_unit:
12962 return "DW_TAG_type_unit";
12963 case DW_TAG_MIPS_loop:
12964 return "DW_TAG_MIPS_loop";
12965 case DW_TAG_HP_array_descriptor:
12966 return "DW_TAG_HP_array_descriptor";
12967 case DW_TAG_format_label:
12968 return "DW_TAG_format_label";
12969 case DW_TAG_function_template:
12970 return "DW_TAG_function_template";
12971 case DW_TAG_class_template:
12972 return "DW_TAG_class_template";
12973 case DW_TAG_GNU_BINCL:
12974 return "DW_TAG_GNU_BINCL";
12975 case DW_TAG_GNU_EINCL:
12976 return "DW_TAG_GNU_EINCL";
12977 case DW_TAG_upc_shared_type:
12978 return "DW_TAG_upc_shared_type";
12979 case DW_TAG_upc_strict_type:
12980 return "DW_TAG_upc_strict_type";
12981 case DW_TAG_upc_relaxed_type:
12982 return "DW_TAG_upc_relaxed_type";
12983 case DW_TAG_PGI_kanji_type:
12984 return "DW_TAG_PGI_kanji_type";
12985 case DW_TAG_PGI_interface_block:
12986 return "DW_TAG_PGI_interface_block";
12987 case DW_TAG_GNU_call_site:
12988 return "DW_TAG_GNU_call_site";
12989 default:
12990 return "DW_TAG_<unknown>";
12991 }
12992 }
12993
12994 /* Convert a DWARF attribute code into its string name. */
12995
12996 static char *
12997 dwarf_attr_name (unsigned attr)
12998 {
12999 switch (attr)
13000 {
13001 case DW_AT_sibling:
13002 return "DW_AT_sibling";
13003 case DW_AT_location:
13004 return "DW_AT_location";
13005 case DW_AT_name:
13006 return "DW_AT_name";
13007 case DW_AT_ordering:
13008 return "DW_AT_ordering";
13009 case DW_AT_subscr_data:
13010 return "DW_AT_subscr_data";
13011 case DW_AT_byte_size:
13012 return "DW_AT_byte_size";
13013 case DW_AT_bit_offset:
13014 return "DW_AT_bit_offset";
13015 case DW_AT_bit_size:
13016 return "DW_AT_bit_size";
13017 case DW_AT_element_list:
13018 return "DW_AT_element_list";
13019 case DW_AT_stmt_list:
13020 return "DW_AT_stmt_list";
13021 case DW_AT_low_pc:
13022 return "DW_AT_low_pc";
13023 case DW_AT_high_pc:
13024 return "DW_AT_high_pc";
13025 case DW_AT_language:
13026 return "DW_AT_language";
13027 case DW_AT_member:
13028 return "DW_AT_member";
13029 case DW_AT_discr:
13030 return "DW_AT_discr";
13031 case DW_AT_discr_value:
13032 return "DW_AT_discr_value";
13033 case DW_AT_visibility:
13034 return "DW_AT_visibility";
13035 case DW_AT_import:
13036 return "DW_AT_import";
13037 case DW_AT_string_length:
13038 return "DW_AT_string_length";
13039 case DW_AT_common_reference:
13040 return "DW_AT_common_reference";
13041 case DW_AT_comp_dir:
13042 return "DW_AT_comp_dir";
13043 case DW_AT_const_value:
13044 return "DW_AT_const_value";
13045 case DW_AT_containing_type:
13046 return "DW_AT_containing_type";
13047 case DW_AT_default_value:
13048 return "DW_AT_default_value";
13049 case DW_AT_inline:
13050 return "DW_AT_inline";
13051 case DW_AT_is_optional:
13052 return "DW_AT_is_optional";
13053 case DW_AT_lower_bound:
13054 return "DW_AT_lower_bound";
13055 case DW_AT_producer:
13056 return "DW_AT_producer";
13057 case DW_AT_prototyped:
13058 return "DW_AT_prototyped";
13059 case DW_AT_return_addr:
13060 return "DW_AT_return_addr";
13061 case DW_AT_start_scope:
13062 return "DW_AT_start_scope";
13063 case DW_AT_bit_stride:
13064 return "DW_AT_bit_stride";
13065 case DW_AT_upper_bound:
13066 return "DW_AT_upper_bound";
13067 case DW_AT_abstract_origin:
13068 return "DW_AT_abstract_origin";
13069 case DW_AT_accessibility:
13070 return "DW_AT_accessibility";
13071 case DW_AT_address_class:
13072 return "DW_AT_address_class";
13073 case DW_AT_artificial:
13074 return "DW_AT_artificial";
13075 case DW_AT_base_types:
13076 return "DW_AT_base_types";
13077 case DW_AT_calling_convention:
13078 return "DW_AT_calling_convention";
13079 case DW_AT_count:
13080 return "DW_AT_count";
13081 case DW_AT_data_member_location:
13082 return "DW_AT_data_member_location";
13083 case DW_AT_decl_column:
13084 return "DW_AT_decl_column";
13085 case DW_AT_decl_file:
13086 return "DW_AT_decl_file";
13087 case DW_AT_decl_line:
13088 return "DW_AT_decl_line";
13089 case DW_AT_declaration:
13090 return "DW_AT_declaration";
13091 case DW_AT_discr_list:
13092 return "DW_AT_discr_list";
13093 case DW_AT_encoding:
13094 return "DW_AT_encoding";
13095 case DW_AT_external:
13096 return "DW_AT_external";
13097 case DW_AT_frame_base:
13098 return "DW_AT_frame_base";
13099 case DW_AT_friend:
13100 return "DW_AT_friend";
13101 case DW_AT_identifier_case:
13102 return "DW_AT_identifier_case";
13103 case DW_AT_macro_info:
13104 return "DW_AT_macro_info";
13105 case DW_AT_namelist_items:
13106 return "DW_AT_namelist_items";
13107 case DW_AT_priority:
13108 return "DW_AT_priority";
13109 case DW_AT_segment:
13110 return "DW_AT_segment";
13111 case DW_AT_specification:
13112 return "DW_AT_specification";
13113 case DW_AT_static_link:
13114 return "DW_AT_static_link";
13115 case DW_AT_type:
13116 return "DW_AT_type";
13117 case DW_AT_use_location:
13118 return "DW_AT_use_location";
13119 case DW_AT_variable_parameter:
13120 return "DW_AT_variable_parameter";
13121 case DW_AT_virtuality:
13122 return "DW_AT_virtuality";
13123 case DW_AT_vtable_elem_location:
13124 return "DW_AT_vtable_elem_location";
13125 /* DWARF 3 values. */
13126 case DW_AT_allocated:
13127 return "DW_AT_allocated";
13128 case DW_AT_associated:
13129 return "DW_AT_associated";
13130 case DW_AT_data_location:
13131 return "DW_AT_data_location";
13132 case DW_AT_byte_stride:
13133 return "DW_AT_byte_stride";
13134 case DW_AT_entry_pc:
13135 return "DW_AT_entry_pc";
13136 case DW_AT_use_UTF8:
13137 return "DW_AT_use_UTF8";
13138 case DW_AT_extension:
13139 return "DW_AT_extension";
13140 case DW_AT_ranges:
13141 return "DW_AT_ranges";
13142 case DW_AT_trampoline:
13143 return "DW_AT_trampoline";
13144 case DW_AT_call_column:
13145 return "DW_AT_call_column";
13146 case DW_AT_call_file:
13147 return "DW_AT_call_file";
13148 case DW_AT_call_line:
13149 return "DW_AT_call_line";
13150 case DW_AT_description:
13151 return "DW_AT_description";
13152 case DW_AT_binary_scale:
13153 return "DW_AT_binary_scale";
13154 case DW_AT_decimal_scale:
13155 return "DW_AT_decimal_scale";
13156 case DW_AT_small:
13157 return "DW_AT_small";
13158 case DW_AT_decimal_sign:
13159 return "DW_AT_decimal_sign";
13160 case DW_AT_digit_count:
13161 return "DW_AT_digit_count";
13162 case DW_AT_picture_string:
13163 return "DW_AT_picture_string";
13164 case DW_AT_mutable:
13165 return "DW_AT_mutable";
13166 case DW_AT_threads_scaled:
13167 return "DW_AT_threads_scaled";
13168 case DW_AT_explicit:
13169 return "DW_AT_explicit";
13170 case DW_AT_object_pointer:
13171 return "DW_AT_object_pointer";
13172 case DW_AT_endianity:
13173 return "DW_AT_endianity";
13174 case DW_AT_elemental:
13175 return "DW_AT_elemental";
13176 case DW_AT_pure:
13177 return "DW_AT_pure";
13178 case DW_AT_recursive:
13179 return "DW_AT_recursive";
13180 /* DWARF 4 values. */
13181 case DW_AT_signature:
13182 return "DW_AT_signature";
13183 case DW_AT_linkage_name:
13184 return "DW_AT_linkage_name";
13185 /* SGI/MIPS extensions. */
13186 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13187 case DW_AT_MIPS_fde:
13188 return "DW_AT_MIPS_fde";
13189 #endif
13190 case DW_AT_MIPS_loop_begin:
13191 return "DW_AT_MIPS_loop_begin";
13192 case DW_AT_MIPS_tail_loop_begin:
13193 return "DW_AT_MIPS_tail_loop_begin";
13194 case DW_AT_MIPS_epilog_begin:
13195 return "DW_AT_MIPS_epilog_begin";
13196 case DW_AT_MIPS_loop_unroll_factor:
13197 return "DW_AT_MIPS_loop_unroll_factor";
13198 case DW_AT_MIPS_software_pipeline_depth:
13199 return "DW_AT_MIPS_software_pipeline_depth";
13200 case DW_AT_MIPS_linkage_name:
13201 return "DW_AT_MIPS_linkage_name";
13202 case DW_AT_MIPS_stride:
13203 return "DW_AT_MIPS_stride";
13204 case DW_AT_MIPS_abstract_name:
13205 return "DW_AT_MIPS_abstract_name";
13206 case DW_AT_MIPS_clone_origin:
13207 return "DW_AT_MIPS_clone_origin";
13208 case DW_AT_MIPS_has_inlines:
13209 return "DW_AT_MIPS_has_inlines";
13210 /* HP extensions. */
13211 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13212 case DW_AT_HP_block_index:
13213 return "DW_AT_HP_block_index";
13214 #endif
13215 case DW_AT_HP_unmodifiable:
13216 return "DW_AT_HP_unmodifiable";
13217 case DW_AT_HP_actuals_stmt_list:
13218 return "DW_AT_HP_actuals_stmt_list";
13219 case DW_AT_HP_proc_per_section:
13220 return "DW_AT_HP_proc_per_section";
13221 case DW_AT_HP_raw_data_ptr:
13222 return "DW_AT_HP_raw_data_ptr";
13223 case DW_AT_HP_pass_by_reference:
13224 return "DW_AT_HP_pass_by_reference";
13225 case DW_AT_HP_opt_level:
13226 return "DW_AT_HP_opt_level";
13227 case DW_AT_HP_prof_version_id:
13228 return "DW_AT_HP_prof_version_id";
13229 case DW_AT_HP_opt_flags:
13230 return "DW_AT_HP_opt_flags";
13231 case DW_AT_HP_cold_region_low_pc:
13232 return "DW_AT_HP_cold_region_low_pc";
13233 case DW_AT_HP_cold_region_high_pc:
13234 return "DW_AT_HP_cold_region_high_pc";
13235 case DW_AT_HP_all_variables_modifiable:
13236 return "DW_AT_HP_all_variables_modifiable";
13237 case DW_AT_HP_linkage_name:
13238 return "DW_AT_HP_linkage_name";
13239 case DW_AT_HP_prof_flags:
13240 return "DW_AT_HP_prof_flags";
13241 /* GNU extensions. */
13242 case DW_AT_sf_names:
13243 return "DW_AT_sf_names";
13244 case DW_AT_src_info:
13245 return "DW_AT_src_info";
13246 case DW_AT_mac_info:
13247 return "DW_AT_mac_info";
13248 case DW_AT_src_coords:
13249 return "DW_AT_src_coords";
13250 case DW_AT_body_begin:
13251 return "DW_AT_body_begin";
13252 case DW_AT_body_end:
13253 return "DW_AT_body_end";
13254 case DW_AT_GNU_vector:
13255 return "DW_AT_GNU_vector";
13256 case DW_AT_GNU_odr_signature:
13257 return "DW_AT_GNU_odr_signature";
13258 /* VMS extensions. */
13259 case DW_AT_VMS_rtnbeg_pd_address:
13260 return "DW_AT_VMS_rtnbeg_pd_address";
13261 /* UPC extension. */
13262 case DW_AT_upc_threads_scaled:
13263 return "DW_AT_upc_threads_scaled";
13264 /* PGI (STMicroelectronics) extensions. */
13265 case DW_AT_PGI_lbase:
13266 return "DW_AT_PGI_lbase";
13267 case DW_AT_PGI_soffset:
13268 return "DW_AT_PGI_soffset";
13269 case DW_AT_PGI_lstride:
13270 return "DW_AT_PGI_lstride";
13271 default:
13272 return "DW_AT_<unknown>";
13273 }
13274 }
13275
13276 /* Convert a DWARF value form code into its string name. */
13277
13278 static char *
13279 dwarf_form_name (unsigned form)
13280 {
13281 switch (form)
13282 {
13283 case DW_FORM_addr:
13284 return "DW_FORM_addr";
13285 case DW_FORM_block2:
13286 return "DW_FORM_block2";
13287 case DW_FORM_block4:
13288 return "DW_FORM_block4";
13289 case DW_FORM_data2:
13290 return "DW_FORM_data2";
13291 case DW_FORM_data4:
13292 return "DW_FORM_data4";
13293 case DW_FORM_data8:
13294 return "DW_FORM_data8";
13295 case DW_FORM_string:
13296 return "DW_FORM_string";
13297 case DW_FORM_block:
13298 return "DW_FORM_block";
13299 case DW_FORM_block1:
13300 return "DW_FORM_block1";
13301 case DW_FORM_data1:
13302 return "DW_FORM_data1";
13303 case DW_FORM_flag:
13304 return "DW_FORM_flag";
13305 case DW_FORM_sdata:
13306 return "DW_FORM_sdata";
13307 case DW_FORM_strp:
13308 return "DW_FORM_strp";
13309 case DW_FORM_udata:
13310 return "DW_FORM_udata";
13311 case DW_FORM_ref_addr:
13312 return "DW_FORM_ref_addr";
13313 case DW_FORM_ref1:
13314 return "DW_FORM_ref1";
13315 case DW_FORM_ref2:
13316 return "DW_FORM_ref2";
13317 case DW_FORM_ref4:
13318 return "DW_FORM_ref4";
13319 case DW_FORM_ref8:
13320 return "DW_FORM_ref8";
13321 case DW_FORM_ref_udata:
13322 return "DW_FORM_ref_udata";
13323 case DW_FORM_indirect:
13324 return "DW_FORM_indirect";
13325 case DW_FORM_sec_offset:
13326 return "DW_FORM_sec_offset";
13327 case DW_FORM_exprloc:
13328 return "DW_FORM_exprloc";
13329 case DW_FORM_flag_present:
13330 return "DW_FORM_flag_present";
13331 case DW_FORM_ref_sig8:
13332 return "DW_FORM_ref_sig8";
13333 default:
13334 return "DW_FORM_<unknown>";
13335 }
13336 }
13337
13338 /* Convert a DWARF stack opcode into its string name. */
13339
13340 const char *
13341 dwarf_stack_op_name (unsigned op)
13342 {
13343 switch (op)
13344 {
13345 case DW_OP_addr:
13346 return "DW_OP_addr";
13347 case DW_OP_deref:
13348 return "DW_OP_deref";
13349 case DW_OP_const1u:
13350 return "DW_OP_const1u";
13351 case DW_OP_const1s:
13352 return "DW_OP_const1s";
13353 case DW_OP_const2u:
13354 return "DW_OP_const2u";
13355 case DW_OP_const2s:
13356 return "DW_OP_const2s";
13357 case DW_OP_const4u:
13358 return "DW_OP_const4u";
13359 case DW_OP_const4s:
13360 return "DW_OP_const4s";
13361 case DW_OP_const8u:
13362 return "DW_OP_const8u";
13363 case DW_OP_const8s:
13364 return "DW_OP_const8s";
13365 case DW_OP_constu:
13366 return "DW_OP_constu";
13367 case DW_OP_consts:
13368 return "DW_OP_consts";
13369 case DW_OP_dup:
13370 return "DW_OP_dup";
13371 case DW_OP_drop:
13372 return "DW_OP_drop";
13373 case DW_OP_over:
13374 return "DW_OP_over";
13375 case DW_OP_pick:
13376 return "DW_OP_pick";
13377 case DW_OP_swap:
13378 return "DW_OP_swap";
13379 case DW_OP_rot:
13380 return "DW_OP_rot";
13381 case DW_OP_xderef:
13382 return "DW_OP_xderef";
13383 case DW_OP_abs:
13384 return "DW_OP_abs";
13385 case DW_OP_and:
13386 return "DW_OP_and";
13387 case DW_OP_div:
13388 return "DW_OP_div";
13389 case DW_OP_minus:
13390 return "DW_OP_minus";
13391 case DW_OP_mod:
13392 return "DW_OP_mod";
13393 case DW_OP_mul:
13394 return "DW_OP_mul";
13395 case DW_OP_neg:
13396 return "DW_OP_neg";
13397 case DW_OP_not:
13398 return "DW_OP_not";
13399 case DW_OP_or:
13400 return "DW_OP_or";
13401 case DW_OP_plus:
13402 return "DW_OP_plus";
13403 case DW_OP_plus_uconst:
13404 return "DW_OP_plus_uconst";
13405 case DW_OP_shl:
13406 return "DW_OP_shl";
13407 case DW_OP_shr:
13408 return "DW_OP_shr";
13409 case DW_OP_shra:
13410 return "DW_OP_shra";
13411 case DW_OP_xor:
13412 return "DW_OP_xor";
13413 case DW_OP_bra:
13414 return "DW_OP_bra";
13415 case DW_OP_eq:
13416 return "DW_OP_eq";
13417 case DW_OP_ge:
13418 return "DW_OP_ge";
13419 case DW_OP_gt:
13420 return "DW_OP_gt";
13421 case DW_OP_le:
13422 return "DW_OP_le";
13423 case DW_OP_lt:
13424 return "DW_OP_lt";
13425 case DW_OP_ne:
13426 return "DW_OP_ne";
13427 case DW_OP_skip:
13428 return "DW_OP_skip";
13429 case DW_OP_lit0:
13430 return "DW_OP_lit0";
13431 case DW_OP_lit1:
13432 return "DW_OP_lit1";
13433 case DW_OP_lit2:
13434 return "DW_OP_lit2";
13435 case DW_OP_lit3:
13436 return "DW_OP_lit3";
13437 case DW_OP_lit4:
13438 return "DW_OP_lit4";
13439 case DW_OP_lit5:
13440 return "DW_OP_lit5";
13441 case DW_OP_lit6:
13442 return "DW_OP_lit6";
13443 case DW_OP_lit7:
13444 return "DW_OP_lit7";
13445 case DW_OP_lit8:
13446 return "DW_OP_lit8";
13447 case DW_OP_lit9:
13448 return "DW_OP_lit9";
13449 case DW_OP_lit10:
13450 return "DW_OP_lit10";
13451 case DW_OP_lit11:
13452 return "DW_OP_lit11";
13453 case DW_OP_lit12:
13454 return "DW_OP_lit12";
13455 case DW_OP_lit13:
13456 return "DW_OP_lit13";
13457 case DW_OP_lit14:
13458 return "DW_OP_lit14";
13459 case DW_OP_lit15:
13460 return "DW_OP_lit15";
13461 case DW_OP_lit16:
13462 return "DW_OP_lit16";
13463 case DW_OP_lit17:
13464 return "DW_OP_lit17";
13465 case DW_OP_lit18:
13466 return "DW_OP_lit18";
13467 case DW_OP_lit19:
13468 return "DW_OP_lit19";
13469 case DW_OP_lit20:
13470 return "DW_OP_lit20";
13471 case DW_OP_lit21:
13472 return "DW_OP_lit21";
13473 case DW_OP_lit22:
13474 return "DW_OP_lit22";
13475 case DW_OP_lit23:
13476 return "DW_OP_lit23";
13477 case DW_OP_lit24:
13478 return "DW_OP_lit24";
13479 case DW_OP_lit25:
13480 return "DW_OP_lit25";
13481 case DW_OP_lit26:
13482 return "DW_OP_lit26";
13483 case DW_OP_lit27:
13484 return "DW_OP_lit27";
13485 case DW_OP_lit28:
13486 return "DW_OP_lit28";
13487 case DW_OP_lit29:
13488 return "DW_OP_lit29";
13489 case DW_OP_lit30:
13490 return "DW_OP_lit30";
13491 case DW_OP_lit31:
13492 return "DW_OP_lit31";
13493 case DW_OP_reg0:
13494 return "DW_OP_reg0";
13495 case DW_OP_reg1:
13496 return "DW_OP_reg1";
13497 case DW_OP_reg2:
13498 return "DW_OP_reg2";
13499 case DW_OP_reg3:
13500 return "DW_OP_reg3";
13501 case DW_OP_reg4:
13502 return "DW_OP_reg4";
13503 case DW_OP_reg5:
13504 return "DW_OP_reg5";
13505 case DW_OP_reg6:
13506 return "DW_OP_reg6";
13507 case DW_OP_reg7:
13508 return "DW_OP_reg7";
13509 case DW_OP_reg8:
13510 return "DW_OP_reg8";
13511 case DW_OP_reg9:
13512 return "DW_OP_reg9";
13513 case DW_OP_reg10:
13514 return "DW_OP_reg10";
13515 case DW_OP_reg11:
13516 return "DW_OP_reg11";
13517 case DW_OP_reg12:
13518 return "DW_OP_reg12";
13519 case DW_OP_reg13:
13520 return "DW_OP_reg13";
13521 case DW_OP_reg14:
13522 return "DW_OP_reg14";
13523 case DW_OP_reg15:
13524 return "DW_OP_reg15";
13525 case DW_OP_reg16:
13526 return "DW_OP_reg16";
13527 case DW_OP_reg17:
13528 return "DW_OP_reg17";
13529 case DW_OP_reg18:
13530 return "DW_OP_reg18";
13531 case DW_OP_reg19:
13532 return "DW_OP_reg19";
13533 case DW_OP_reg20:
13534 return "DW_OP_reg20";
13535 case DW_OP_reg21:
13536 return "DW_OP_reg21";
13537 case DW_OP_reg22:
13538 return "DW_OP_reg22";
13539 case DW_OP_reg23:
13540 return "DW_OP_reg23";
13541 case DW_OP_reg24:
13542 return "DW_OP_reg24";
13543 case DW_OP_reg25:
13544 return "DW_OP_reg25";
13545 case DW_OP_reg26:
13546 return "DW_OP_reg26";
13547 case DW_OP_reg27:
13548 return "DW_OP_reg27";
13549 case DW_OP_reg28:
13550 return "DW_OP_reg28";
13551 case DW_OP_reg29:
13552 return "DW_OP_reg29";
13553 case DW_OP_reg30:
13554 return "DW_OP_reg30";
13555 case DW_OP_reg31:
13556 return "DW_OP_reg31";
13557 case DW_OP_breg0:
13558 return "DW_OP_breg0";
13559 case DW_OP_breg1:
13560 return "DW_OP_breg1";
13561 case DW_OP_breg2:
13562 return "DW_OP_breg2";
13563 case DW_OP_breg3:
13564 return "DW_OP_breg3";
13565 case DW_OP_breg4:
13566 return "DW_OP_breg4";
13567 case DW_OP_breg5:
13568 return "DW_OP_breg5";
13569 case DW_OP_breg6:
13570 return "DW_OP_breg6";
13571 case DW_OP_breg7:
13572 return "DW_OP_breg7";
13573 case DW_OP_breg8:
13574 return "DW_OP_breg8";
13575 case DW_OP_breg9:
13576 return "DW_OP_breg9";
13577 case DW_OP_breg10:
13578 return "DW_OP_breg10";
13579 case DW_OP_breg11:
13580 return "DW_OP_breg11";
13581 case DW_OP_breg12:
13582 return "DW_OP_breg12";
13583 case DW_OP_breg13:
13584 return "DW_OP_breg13";
13585 case DW_OP_breg14:
13586 return "DW_OP_breg14";
13587 case DW_OP_breg15:
13588 return "DW_OP_breg15";
13589 case DW_OP_breg16:
13590 return "DW_OP_breg16";
13591 case DW_OP_breg17:
13592 return "DW_OP_breg17";
13593 case DW_OP_breg18:
13594 return "DW_OP_breg18";
13595 case DW_OP_breg19:
13596 return "DW_OP_breg19";
13597 case DW_OP_breg20:
13598 return "DW_OP_breg20";
13599 case DW_OP_breg21:
13600 return "DW_OP_breg21";
13601 case DW_OP_breg22:
13602 return "DW_OP_breg22";
13603 case DW_OP_breg23:
13604 return "DW_OP_breg23";
13605 case DW_OP_breg24:
13606 return "DW_OP_breg24";
13607 case DW_OP_breg25:
13608 return "DW_OP_breg25";
13609 case DW_OP_breg26:
13610 return "DW_OP_breg26";
13611 case DW_OP_breg27:
13612 return "DW_OP_breg27";
13613 case DW_OP_breg28:
13614 return "DW_OP_breg28";
13615 case DW_OP_breg29:
13616 return "DW_OP_breg29";
13617 case DW_OP_breg30:
13618 return "DW_OP_breg30";
13619 case DW_OP_breg31:
13620 return "DW_OP_breg31";
13621 case DW_OP_regx:
13622 return "DW_OP_regx";
13623 case DW_OP_fbreg:
13624 return "DW_OP_fbreg";
13625 case DW_OP_bregx:
13626 return "DW_OP_bregx";
13627 case DW_OP_piece:
13628 return "DW_OP_piece";
13629 case DW_OP_deref_size:
13630 return "DW_OP_deref_size";
13631 case DW_OP_xderef_size:
13632 return "DW_OP_xderef_size";
13633 case DW_OP_nop:
13634 return "DW_OP_nop";
13635 /* DWARF 3 extensions. */
13636 case DW_OP_push_object_address:
13637 return "DW_OP_push_object_address";
13638 case DW_OP_call2:
13639 return "DW_OP_call2";
13640 case DW_OP_call4:
13641 return "DW_OP_call4";
13642 case DW_OP_call_ref:
13643 return "DW_OP_call_ref";
13644 case DW_OP_form_tls_address:
13645 return "DW_OP_form_tls_address";
13646 case DW_OP_call_frame_cfa:
13647 return "DW_OP_call_frame_cfa";
13648 case DW_OP_bit_piece:
13649 return "DW_OP_bit_piece";
13650 /* DWARF 4 extensions. */
13651 case DW_OP_implicit_value:
13652 return "DW_OP_implicit_value";
13653 case DW_OP_stack_value:
13654 return "DW_OP_stack_value";
13655 /* GNU extensions. */
13656 case DW_OP_GNU_push_tls_address:
13657 return "DW_OP_GNU_push_tls_address";
13658 case DW_OP_GNU_uninit:
13659 return "DW_OP_GNU_uninit";
13660 case DW_OP_GNU_implicit_pointer:
13661 return "DW_OP_GNU_implicit_pointer";
13662 case DW_OP_GNU_entry_value:
13663 return "DW_OP_GNU_entry_value";
13664 case DW_OP_GNU_const_type:
13665 return "DW_OP_GNU_const_type";
13666 case DW_OP_GNU_regval_type:
13667 return "DW_OP_GNU_regval_type";
13668 case DW_OP_GNU_deref_type:
13669 return "DW_OP_GNU_deref_type";
13670 case DW_OP_GNU_convert:
13671 return "DW_OP_GNU_convert";
13672 case DW_OP_GNU_reinterpret:
13673 return "DW_OP_GNU_reinterpret";
13674 default:
13675 return NULL;
13676 }
13677 }
13678
13679 static char *
13680 dwarf_bool_name (unsigned mybool)
13681 {
13682 if (mybool)
13683 return "TRUE";
13684 else
13685 return "FALSE";
13686 }
13687
13688 /* Convert a DWARF type code into its string name. */
13689
13690 static char *
13691 dwarf_type_encoding_name (unsigned enc)
13692 {
13693 switch (enc)
13694 {
13695 case DW_ATE_void:
13696 return "DW_ATE_void";
13697 case DW_ATE_address:
13698 return "DW_ATE_address";
13699 case DW_ATE_boolean:
13700 return "DW_ATE_boolean";
13701 case DW_ATE_complex_float:
13702 return "DW_ATE_complex_float";
13703 case DW_ATE_float:
13704 return "DW_ATE_float";
13705 case DW_ATE_signed:
13706 return "DW_ATE_signed";
13707 case DW_ATE_signed_char:
13708 return "DW_ATE_signed_char";
13709 case DW_ATE_unsigned:
13710 return "DW_ATE_unsigned";
13711 case DW_ATE_unsigned_char:
13712 return "DW_ATE_unsigned_char";
13713 /* DWARF 3. */
13714 case DW_ATE_imaginary_float:
13715 return "DW_ATE_imaginary_float";
13716 case DW_ATE_packed_decimal:
13717 return "DW_ATE_packed_decimal";
13718 case DW_ATE_numeric_string:
13719 return "DW_ATE_numeric_string";
13720 case DW_ATE_edited:
13721 return "DW_ATE_edited";
13722 case DW_ATE_signed_fixed:
13723 return "DW_ATE_signed_fixed";
13724 case DW_ATE_unsigned_fixed:
13725 return "DW_ATE_unsigned_fixed";
13726 case DW_ATE_decimal_float:
13727 return "DW_ATE_decimal_float";
13728 /* DWARF 4. */
13729 case DW_ATE_UTF:
13730 return "DW_ATE_UTF";
13731 /* HP extensions. */
13732 case DW_ATE_HP_float80:
13733 return "DW_ATE_HP_float80";
13734 case DW_ATE_HP_complex_float80:
13735 return "DW_ATE_HP_complex_float80";
13736 case DW_ATE_HP_float128:
13737 return "DW_ATE_HP_float128";
13738 case DW_ATE_HP_complex_float128:
13739 return "DW_ATE_HP_complex_float128";
13740 case DW_ATE_HP_floathpintel:
13741 return "DW_ATE_HP_floathpintel";
13742 case DW_ATE_HP_imaginary_float80:
13743 return "DW_ATE_HP_imaginary_float80";
13744 case DW_ATE_HP_imaginary_float128:
13745 return "DW_ATE_HP_imaginary_float128";
13746 default:
13747 return "DW_ATE_<unknown>";
13748 }
13749 }
13750
13751 /* Convert a DWARF call frame info operation to its string name. */
13752
13753 #if 0
13754 static char *
13755 dwarf_cfi_name (unsigned cfi_opc)
13756 {
13757 switch (cfi_opc)
13758 {
13759 case DW_CFA_advance_loc:
13760 return "DW_CFA_advance_loc";
13761 case DW_CFA_offset:
13762 return "DW_CFA_offset";
13763 case DW_CFA_restore:
13764 return "DW_CFA_restore";
13765 case DW_CFA_nop:
13766 return "DW_CFA_nop";
13767 case DW_CFA_set_loc:
13768 return "DW_CFA_set_loc";
13769 case DW_CFA_advance_loc1:
13770 return "DW_CFA_advance_loc1";
13771 case DW_CFA_advance_loc2:
13772 return "DW_CFA_advance_loc2";
13773 case DW_CFA_advance_loc4:
13774 return "DW_CFA_advance_loc4";
13775 case DW_CFA_offset_extended:
13776 return "DW_CFA_offset_extended";
13777 case DW_CFA_restore_extended:
13778 return "DW_CFA_restore_extended";
13779 case DW_CFA_undefined:
13780 return "DW_CFA_undefined";
13781 case DW_CFA_same_value:
13782 return "DW_CFA_same_value";
13783 case DW_CFA_register:
13784 return "DW_CFA_register";
13785 case DW_CFA_remember_state:
13786 return "DW_CFA_remember_state";
13787 case DW_CFA_restore_state:
13788 return "DW_CFA_restore_state";
13789 case DW_CFA_def_cfa:
13790 return "DW_CFA_def_cfa";
13791 case DW_CFA_def_cfa_register:
13792 return "DW_CFA_def_cfa_register";
13793 case DW_CFA_def_cfa_offset:
13794 return "DW_CFA_def_cfa_offset";
13795 /* DWARF 3. */
13796 case DW_CFA_def_cfa_expression:
13797 return "DW_CFA_def_cfa_expression";
13798 case DW_CFA_expression:
13799 return "DW_CFA_expression";
13800 case DW_CFA_offset_extended_sf:
13801 return "DW_CFA_offset_extended_sf";
13802 case DW_CFA_def_cfa_sf:
13803 return "DW_CFA_def_cfa_sf";
13804 case DW_CFA_def_cfa_offset_sf:
13805 return "DW_CFA_def_cfa_offset_sf";
13806 case DW_CFA_val_offset:
13807 return "DW_CFA_val_offset";
13808 case DW_CFA_val_offset_sf:
13809 return "DW_CFA_val_offset_sf";
13810 case DW_CFA_val_expression:
13811 return "DW_CFA_val_expression";
13812 /* SGI/MIPS specific. */
13813 case DW_CFA_MIPS_advance_loc8:
13814 return "DW_CFA_MIPS_advance_loc8";
13815 /* GNU extensions. */
13816 case DW_CFA_GNU_window_save:
13817 return "DW_CFA_GNU_window_save";
13818 case DW_CFA_GNU_args_size:
13819 return "DW_CFA_GNU_args_size";
13820 case DW_CFA_GNU_negative_offset_extended:
13821 return "DW_CFA_GNU_negative_offset_extended";
13822 default:
13823 return "DW_CFA_<unknown>";
13824 }
13825 }
13826 #endif
13827
13828 static void
13829 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13830 {
13831 unsigned int i;
13832
13833 print_spaces (indent, f);
13834 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13835 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13836
13837 if (die->parent != NULL)
13838 {
13839 print_spaces (indent, f);
13840 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13841 die->parent->offset);
13842 }
13843
13844 print_spaces (indent, f);
13845 fprintf_unfiltered (f, " has children: %s\n",
13846 dwarf_bool_name (die->child != NULL));
13847
13848 print_spaces (indent, f);
13849 fprintf_unfiltered (f, " attributes:\n");
13850
13851 for (i = 0; i < die->num_attrs; ++i)
13852 {
13853 print_spaces (indent, f);
13854 fprintf_unfiltered (f, " %s (%s) ",
13855 dwarf_attr_name (die->attrs[i].name),
13856 dwarf_form_name (die->attrs[i].form));
13857
13858 switch (die->attrs[i].form)
13859 {
13860 case DW_FORM_ref_addr:
13861 case DW_FORM_addr:
13862 fprintf_unfiltered (f, "address: ");
13863 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13864 break;
13865 case DW_FORM_block2:
13866 case DW_FORM_block4:
13867 case DW_FORM_block:
13868 case DW_FORM_block1:
13869 fprintf_unfiltered (f, "block: size %d",
13870 DW_BLOCK (&die->attrs[i])->size);
13871 break;
13872 case DW_FORM_exprloc:
13873 fprintf_unfiltered (f, "expression: size %u",
13874 DW_BLOCK (&die->attrs[i])->size);
13875 break;
13876 case DW_FORM_ref1:
13877 case DW_FORM_ref2:
13878 case DW_FORM_ref4:
13879 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13880 (long) (DW_ADDR (&die->attrs[i])));
13881 break;
13882 case DW_FORM_data1:
13883 case DW_FORM_data2:
13884 case DW_FORM_data4:
13885 case DW_FORM_data8:
13886 case DW_FORM_udata:
13887 case DW_FORM_sdata:
13888 fprintf_unfiltered (f, "constant: %s",
13889 pulongest (DW_UNSND (&die->attrs[i])));
13890 break;
13891 case DW_FORM_sec_offset:
13892 fprintf_unfiltered (f, "section offset: %s",
13893 pulongest (DW_UNSND (&die->attrs[i])));
13894 break;
13895 case DW_FORM_ref_sig8:
13896 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13897 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13898 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13899 else
13900 fprintf_unfiltered (f, "signatured type, offset: unknown");
13901 break;
13902 case DW_FORM_string:
13903 case DW_FORM_strp:
13904 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13905 DW_STRING (&die->attrs[i])
13906 ? DW_STRING (&die->attrs[i]) : "",
13907 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13908 break;
13909 case DW_FORM_flag:
13910 if (DW_UNSND (&die->attrs[i]))
13911 fprintf_unfiltered (f, "flag: TRUE");
13912 else
13913 fprintf_unfiltered (f, "flag: FALSE");
13914 break;
13915 case DW_FORM_flag_present:
13916 fprintf_unfiltered (f, "flag: TRUE");
13917 break;
13918 case DW_FORM_indirect:
13919 /* The reader will have reduced the indirect form to
13920 the "base form" so this form should not occur. */
13921 fprintf_unfiltered (f,
13922 "unexpected attribute form: DW_FORM_indirect");
13923 break;
13924 default:
13925 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13926 die->attrs[i].form);
13927 break;
13928 }
13929 fprintf_unfiltered (f, "\n");
13930 }
13931 }
13932
13933 static void
13934 dump_die_for_error (struct die_info *die)
13935 {
13936 dump_die_shallow (gdb_stderr, 0, die);
13937 }
13938
13939 static void
13940 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13941 {
13942 int indent = level * 4;
13943
13944 gdb_assert (die != NULL);
13945
13946 if (level >= max_level)
13947 return;
13948
13949 dump_die_shallow (f, indent, die);
13950
13951 if (die->child != NULL)
13952 {
13953 print_spaces (indent, f);
13954 fprintf_unfiltered (f, " Children:");
13955 if (level + 1 < max_level)
13956 {
13957 fprintf_unfiltered (f, "\n");
13958 dump_die_1 (f, level + 1, max_level, die->child);
13959 }
13960 else
13961 {
13962 fprintf_unfiltered (f,
13963 " [not printed, max nesting level reached]\n");
13964 }
13965 }
13966
13967 if (die->sibling != NULL && level > 0)
13968 {
13969 dump_die_1 (f, level, max_level, die->sibling);
13970 }
13971 }
13972
13973 /* This is called from the pdie macro in gdbinit.in.
13974 It's not static so gcc will keep a copy callable from gdb. */
13975
13976 void
13977 dump_die (struct die_info *die, int max_level)
13978 {
13979 dump_die_1 (gdb_stdlog, 0, max_level, die);
13980 }
13981
13982 static void
13983 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13984 {
13985 void **slot;
13986
13987 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13988
13989 *slot = die;
13990 }
13991
13992 static int
13993 is_ref_attr (struct attribute *attr)
13994 {
13995 switch (attr->form)
13996 {
13997 case DW_FORM_ref_addr:
13998 case DW_FORM_ref1:
13999 case DW_FORM_ref2:
14000 case DW_FORM_ref4:
14001 case DW_FORM_ref8:
14002 case DW_FORM_ref_udata:
14003 return 1;
14004 default:
14005 return 0;
14006 }
14007 }
14008
14009 static unsigned int
14010 dwarf2_get_ref_die_offset (struct attribute *attr)
14011 {
14012 if (is_ref_attr (attr))
14013 return DW_ADDR (attr);
14014
14015 complaint (&symfile_complaints,
14016 _("unsupported die ref attribute form: '%s'"),
14017 dwarf_form_name (attr->form));
14018 return 0;
14019 }
14020
14021 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14022 * the value held by the attribute is not constant. */
14023
14024 static LONGEST
14025 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14026 {
14027 if (attr->form == DW_FORM_sdata)
14028 return DW_SND (attr);
14029 else if (attr->form == DW_FORM_udata
14030 || attr->form == DW_FORM_data1
14031 || attr->form == DW_FORM_data2
14032 || attr->form == DW_FORM_data4
14033 || attr->form == DW_FORM_data8)
14034 return DW_UNSND (attr);
14035 else
14036 {
14037 complaint (&symfile_complaints,
14038 _("Attribute value is not a constant (%s)"),
14039 dwarf_form_name (attr->form));
14040 return default_value;
14041 }
14042 }
14043
14044 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14045 unit and add it to our queue.
14046 The result is non-zero if PER_CU was queued, otherwise the result is zero
14047 meaning either PER_CU is already queued or it is already loaded. */
14048
14049 static int
14050 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14051 struct dwarf2_per_cu_data *per_cu)
14052 {
14053 /* We may arrive here during partial symbol reading, if we need full
14054 DIEs to process an unusual case (e.g. template arguments). Do
14055 not queue PER_CU, just tell our caller to load its DIEs. */
14056 if (dwarf2_per_objfile->reading_partial_symbols)
14057 {
14058 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14059 return 1;
14060 return 0;
14061 }
14062
14063 /* Mark the dependence relation so that we don't flush PER_CU
14064 too early. */
14065 dwarf2_add_dependence (this_cu, per_cu);
14066
14067 /* If it's already on the queue, we have nothing to do. */
14068 if (per_cu->queued)
14069 return 0;
14070
14071 /* If the compilation unit is already loaded, just mark it as
14072 used. */
14073 if (per_cu->cu != NULL)
14074 {
14075 per_cu->cu->last_used = 0;
14076 return 0;
14077 }
14078
14079 /* Add it to the queue. */
14080 queue_comp_unit (per_cu);
14081
14082 return 1;
14083 }
14084
14085 /* Follow reference or signature attribute ATTR of SRC_DIE.
14086 On entry *REF_CU is the CU of SRC_DIE.
14087 On exit *REF_CU is the CU of the result. */
14088
14089 static struct die_info *
14090 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14091 struct dwarf2_cu **ref_cu)
14092 {
14093 struct die_info *die;
14094
14095 if (is_ref_attr (attr))
14096 die = follow_die_ref (src_die, attr, ref_cu);
14097 else if (attr->form == DW_FORM_ref_sig8)
14098 die = follow_die_sig (src_die, attr, ref_cu);
14099 else
14100 {
14101 dump_die_for_error (src_die);
14102 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14103 (*ref_cu)->objfile->name);
14104 }
14105
14106 return die;
14107 }
14108
14109 /* Follow reference OFFSET.
14110 On entry *REF_CU is the CU of the source die referencing OFFSET.
14111 On exit *REF_CU is the CU of the result.
14112 Returns NULL if OFFSET is invalid. */
14113
14114 static struct die_info *
14115 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14116 {
14117 struct die_info temp_die;
14118 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14119
14120 gdb_assert (cu->per_cu != NULL);
14121
14122 target_cu = cu;
14123
14124 if (cu->per_cu->debug_types_section)
14125 {
14126 /* .debug_types CUs cannot reference anything outside their CU.
14127 If they need to, they have to reference a signatured type via
14128 DW_FORM_ref_sig8. */
14129 if (! offset_in_cu_p (&cu->header, offset))
14130 return NULL;
14131 }
14132 else if (! offset_in_cu_p (&cu->header, offset))
14133 {
14134 struct dwarf2_per_cu_data *per_cu;
14135
14136 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14137
14138 /* If necessary, add it to the queue and load its DIEs. */
14139 if (maybe_queue_comp_unit (cu, per_cu))
14140 load_full_comp_unit (per_cu);
14141
14142 target_cu = per_cu->cu;
14143 }
14144 else if (cu->dies == NULL)
14145 {
14146 /* We're loading full DIEs during partial symbol reading. */
14147 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14148 load_full_comp_unit (cu->per_cu);
14149 }
14150
14151 *ref_cu = target_cu;
14152 temp_die.offset = offset;
14153 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14154 }
14155
14156 /* Follow reference attribute ATTR of SRC_DIE.
14157 On entry *REF_CU is the CU of SRC_DIE.
14158 On exit *REF_CU is the CU of the result. */
14159
14160 static struct die_info *
14161 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14162 struct dwarf2_cu **ref_cu)
14163 {
14164 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14165 struct dwarf2_cu *cu = *ref_cu;
14166 struct die_info *die;
14167
14168 die = follow_die_offset (offset, ref_cu);
14169 if (!die)
14170 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14171 "at 0x%x [in module %s]"),
14172 offset, src_die->offset, cu->objfile->name);
14173
14174 return die;
14175 }
14176
14177 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14178 Returned value is intended for DW_OP_call*. Returned
14179 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14180
14181 struct dwarf2_locexpr_baton
14182 dwarf2_fetch_die_location_block (unsigned int offset,
14183 struct dwarf2_per_cu_data *per_cu,
14184 CORE_ADDR (*get_frame_pc) (void *baton),
14185 void *baton)
14186 {
14187 struct dwarf2_cu *cu;
14188 struct die_info *die;
14189 struct attribute *attr;
14190 struct dwarf2_locexpr_baton retval;
14191
14192 dw2_setup (per_cu->objfile);
14193
14194 if (per_cu->cu == NULL)
14195 load_cu (per_cu);
14196 cu = per_cu->cu;
14197
14198 die = follow_die_offset (offset, &cu);
14199 if (!die)
14200 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14201 offset, per_cu->objfile->name);
14202
14203 attr = dwarf2_attr (die, DW_AT_location, cu);
14204 if (!attr)
14205 {
14206 /* DWARF: "If there is no such attribute, then there is no effect.".
14207 DATA is ignored if SIZE is 0. */
14208
14209 retval.data = NULL;
14210 retval.size = 0;
14211 }
14212 else if (attr_form_is_section_offset (attr))
14213 {
14214 struct dwarf2_loclist_baton loclist_baton;
14215 CORE_ADDR pc = (*get_frame_pc) (baton);
14216 size_t size;
14217
14218 fill_in_loclist_baton (cu, &loclist_baton, attr);
14219
14220 retval.data = dwarf2_find_location_expression (&loclist_baton,
14221 &size, pc);
14222 retval.size = size;
14223 }
14224 else
14225 {
14226 if (!attr_form_is_block (attr))
14227 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14228 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14229 offset, per_cu->objfile->name);
14230
14231 retval.data = DW_BLOCK (attr)->data;
14232 retval.size = DW_BLOCK (attr)->size;
14233 }
14234 retval.per_cu = cu->per_cu;
14235
14236 age_cached_comp_units ();
14237
14238 return retval;
14239 }
14240
14241 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14242 PER_CU. */
14243
14244 struct type *
14245 dwarf2_get_die_type (unsigned int die_offset,
14246 struct dwarf2_per_cu_data *per_cu)
14247 {
14248 dw2_setup (per_cu->objfile);
14249 return get_die_type_at_offset (die_offset, per_cu);
14250 }
14251
14252 /* Follow the signature attribute ATTR in SRC_DIE.
14253 On entry *REF_CU is the CU of SRC_DIE.
14254 On exit *REF_CU is the CU of the result. */
14255
14256 static struct die_info *
14257 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14258 struct dwarf2_cu **ref_cu)
14259 {
14260 struct objfile *objfile = (*ref_cu)->objfile;
14261 struct die_info temp_die;
14262 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14263 struct dwarf2_cu *sig_cu;
14264 struct die_info *die;
14265
14266 /* sig_type will be NULL if the signatured type is missing from
14267 the debug info. */
14268 if (sig_type == NULL)
14269 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14270 "at 0x%x [in module %s]"),
14271 src_die->offset, objfile->name);
14272
14273 /* If necessary, add it to the queue and load its DIEs. */
14274
14275 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14276 read_signatured_type (sig_type);
14277
14278 gdb_assert (sig_type->per_cu.cu != NULL);
14279
14280 sig_cu = sig_type->per_cu.cu;
14281 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14282 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14283 if (die)
14284 {
14285 *ref_cu = sig_cu;
14286 return die;
14287 }
14288
14289 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14290 "from DIE at 0x%x [in module %s]"),
14291 sig_type->type_offset, src_die->offset, objfile->name);
14292 }
14293
14294 /* Given an offset of a signatured type, return its signatured_type. */
14295
14296 static struct signatured_type *
14297 lookup_signatured_type_at_offset (struct objfile *objfile,
14298 struct dwarf2_section_info *section,
14299 unsigned int offset)
14300 {
14301 gdb_byte *info_ptr = section->buffer + offset;
14302 unsigned int length, initial_length_size;
14303 unsigned int sig_offset;
14304 struct signatured_type find_entry, *type_sig;
14305
14306 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14307 sig_offset = (initial_length_size
14308 + 2 /*version*/
14309 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14310 + 1 /*address_size*/);
14311 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14312 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14313
14314 /* This is only used to lookup previously recorded types.
14315 If we didn't find it, it's our bug. */
14316 gdb_assert (type_sig != NULL);
14317 gdb_assert (offset == type_sig->per_cu.offset);
14318
14319 return type_sig;
14320 }
14321
14322 /* Load the DIEs associated with type unit PER_CU into memory. */
14323
14324 static void
14325 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14326 {
14327 struct objfile *objfile = per_cu->objfile;
14328 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14329 unsigned int offset = per_cu->offset;
14330 struct signatured_type *type_sig;
14331
14332 dwarf2_read_section (objfile, sect);
14333
14334 /* We have the section offset, but we need the signature to do the
14335 hash table lookup. */
14336 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14337 the signature to assert we found the right one.
14338 Ok, but it's a lot of work. We should simplify things so any needed
14339 assert doesn't require all this clumsiness. */
14340 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14341
14342 gdb_assert (type_sig->per_cu.cu == NULL);
14343
14344 read_signatured_type (type_sig);
14345
14346 gdb_assert (type_sig->per_cu.cu != NULL);
14347 }
14348
14349 /* Read in a signatured type and build its CU and DIEs. */
14350
14351 static void
14352 read_signatured_type (struct signatured_type *type_sig)
14353 {
14354 struct objfile *objfile = type_sig->per_cu.objfile;
14355 gdb_byte *types_ptr;
14356 struct die_reader_specs reader_specs;
14357 struct dwarf2_cu *cu;
14358 ULONGEST signature;
14359 struct cleanup *back_to, *free_cu_cleanup;
14360 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14361
14362 dwarf2_read_section (objfile, section);
14363 types_ptr = section->buffer + type_sig->per_cu.offset;
14364
14365 gdb_assert (type_sig->per_cu.cu == NULL);
14366
14367 cu = xmalloc (sizeof (*cu));
14368 init_one_comp_unit (cu, &type_sig->per_cu);
14369
14370 /* If an error occurs while loading, release our storage. */
14371 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14372
14373 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14374 &signature, NULL);
14375 gdb_assert (signature == type_sig->signature);
14376
14377 cu->die_hash
14378 = htab_create_alloc_ex (cu->header.length / 12,
14379 die_hash,
14380 die_eq,
14381 NULL,
14382 &cu->comp_unit_obstack,
14383 hashtab_obstack_allocate,
14384 dummy_obstack_deallocate);
14385
14386 dwarf2_read_abbrevs (cu);
14387 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14388
14389 init_cu_die_reader (&reader_specs, cu);
14390
14391 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14392 NULL /*parent*/);
14393
14394 /* We try not to read any attributes in this function, because not
14395 all CUs needed for references have been loaded yet, and symbol
14396 table processing isn't initialized. But we have to set the CU language,
14397 or we won't be able to build types correctly. */
14398 prepare_one_comp_unit (cu, cu->dies);
14399
14400 do_cleanups (back_to);
14401
14402 /* We've successfully allocated this compilation unit. Let our caller
14403 clean it up when finished with it. */
14404 discard_cleanups (free_cu_cleanup);
14405
14406 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14407 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14408 }
14409
14410 /* Decode simple location descriptions.
14411 Given a pointer to a dwarf block that defines a location, compute
14412 the location and return the value.
14413
14414 NOTE drow/2003-11-18: This function is called in two situations
14415 now: for the address of static or global variables (partial symbols
14416 only) and for offsets into structures which are expected to be
14417 (more or less) constant. The partial symbol case should go away,
14418 and only the constant case should remain. That will let this
14419 function complain more accurately. A few special modes are allowed
14420 without complaint for global variables (for instance, global
14421 register values and thread-local values).
14422
14423 A location description containing no operations indicates that the
14424 object is optimized out. The return value is 0 for that case.
14425 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14426 callers will only want a very basic result and this can become a
14427 complaint.
14428
14429 Note that stack[0] is unused except as a default error return. */
14430
14431 static CORE_ADDR
14432 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14433 {
14434 struct objfile *objfile = cu->objfile;
14435 int i;
14436 int size = blk->size;
14437 gdb_byte *data = blk->data;
14438 CORE_ADDR stack[64];
14439 int stacki;
14440 unsigned int bytes_read, unsnd;
14441 gdb_byte op;
14442
14443 i = 0;
14444 stacki = 0;
14445 stack[stacki] = 0;
14446 stack[++stacki] = 0;
14447
14448 while (i < size)
14449 {
14450 op = data[i++];
14451 switch (op)
14452 {
14453 case DW_OP_lit0:
14454 case DW_OP_lit1:
14455 case DW_OP_lit2:
14456 case DW_OP_lit3:
14457 case DW_OP_lit4:
14458 case DW_OP_lit5:
14459 case DW_OP_lit6:
14460 case DW_OP_lit7:
14461 case DW_OP_lit8:
14462 case DW_OP_lit9:
14463 case DW_OP_lit10:
14464 case DW_OP_lit11:
14465 case DW_OP_lit12:
14466 case DW_OP_lit13:
14467 case DW_OP_lit14:
14468 case DW_OP_lit15:
14469 case DW_OP_lit16:
14470 case DW_OP_lit17:
14471 case DW_OP_lit18:
14472 case DW_OP_lit19:
14473 case DW_OP_lit20:
14474 case DW_OP_lit21:
14475 case DW_OP_lit22:
14476 case DW_OP_lit23:
14477 case DW_OP_lit24:
14478 case DW_OP_lit25:
14479 case DW_OP_lit26:
14480 case DW_OP_lit27:
14481 case DW_OP_lit28:
14482 case DW_OP_lit29:
14483 case DW_OP_lit30:
14484 case DW_OP_lit31:
14485 stack[++stacki] = op - DW_OP_lit0;
14486 break;
14487
14488 case DW_OP_reg0:
14489 case DW_OP_reg1:
14490 case DW_OP_reg2:
14491 case DW_OP_reg3:
14492 case DW_OP_reg4:
14493 case DW_OP_reg5:
14494 case DW_OP_reg6:
14495 case DW_OP_reg7:
14496 case DW_OP_reg8:
14497 case DW_OP_reg9:
14498 case DW_OP_reg10:
14499 case DW_OP_reg11:
14500 case DW_OP_reg12:
14501 case DW_OP_reg13:
14502 case DW_OP_reg14:
14503 case DW_OP_reg15:
14504 case DW_OP_reg16:
14505 case DW_OP_reg17:
14506 case DW_OP_reg18:
14507 case DW_OP_reg19:
14508 case DW_OP_reg20:
14509 case DW_OP_reg21:
14510 case DW_OP_reg22:
14511 case DW_OP_reg23:
14512 case DW_OP_reg24:
14513 case DW_OP_reg25:
14514 case DW_OP_reg26:
14515 case DW_OP_reg27:
14516 case DW_OP_reg28:
14517 case DW_OP_reg29:
14518 case DW_OP_reg30:
14519 case DW_OP_reg31:
14520 stack[++stacki] = op - DW_OP_reg0;
14521 if (i < size)
14522 dwarf2_complex_location_expr_complaint ();
14523 break;
14524
14525 case DW_OP_regx:
14526 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14527 i += bytes_read;
14528 stack[++stacki] = unsnd;
14529 if (i < size)
14530 dwarf2_complex_location_expr_complaint ();
14531 break;
14532
14533 case DW_OP_addr:
14534 stack[++stacki] = read_address (objfile->obfd, &data[i],
14535 cu, &bytes_read);
14536 i += bytes_read;
14537 break;
14538
14539 case DW_OP_const1u:
14540 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14541 i += 1;
14542 break;
14543
14544 case DW_OP_const1s:
14545 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14546 i += 1;
14547 break;
14548
14549 case DW_OP_const2u:
14550 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14551 i += 2;
14552 break;
14553
14554 case DW_OP_const2s:
14555 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14556 i += 2;
14557 break;
14558
14559 case DW_OP_const4u:
14560 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14561 i += 4;
14562 break;
14563
14564 case DW_OP_const4s:
14565 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14566 i += 4;
14567 break;
14568
14569 case DW_OP_const8u:
14570 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14571 i += 8;
14572 break;
14573
14574 case DW_OP_constu:
14575 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14576 &bytes_read);
14577 i += bytes_read;
14578 break;
14579
14580 case DW_OP_consts:
14581 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14582 i += bytes_read;
14583 break;
14584
14585 case DW_OP_dup:
14586 stack[stacki + 1] = stack[stacki];
14587 stacki++;
14588 break;
14589
14590 case DW_OP_plus:
14591 stack[stacki - 1] += stack[stacki];
14592 stacki--;
14593 break;
14594
14595 case DW_OP_plus_uconst:
14596 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14597 &bytes_read);
14598 i += bytes_read;
14599 break;
14600
14601 case DW_OP_minus:
14602 stack[stacki - 1] -= stack[stacki];
14603 stacki--;
14604 break;
14605
14606 case DW_OP_deref:
14607 /* If we're not the last op, then we definitely can't encode
14608 this using GDB's address_class enum. This is valid for partial
14609 global symbols, although the variable's address will be bogus
14610 in the psymtab. */
14611 if (i < size)
14612 dwarf2_complex_location_expr_complaint ();
14613 break;
14614
14615 case DW_OP_GNU_push_tls_address:
14616 /* The top of the stack has the offset from the beginning
14617 of the thread control block at which the variable is located. */
14618 /* Nothing should follow this operator, so the top of stack would
14619 be returned. */
14620 /* This is valid for partial global symbols, but the variable's
14621 address will be bogus in the psymtab. Make it always at least
14622 non-zero to not look as a variable garbage collected by linker
14623 which have DW_OP_addr 0. */
14624 if (i < size)
14625 dwarf2_complex_location_expr_complaint ();
14626 stack[stacki]++;
14627 break;
14628
14629 case DW_OP_GNU_uninit:
14630 break;
14631
14632 default:
14633 {
14634 const char *name = dwarf_stack_op_name (op);
14635
14636 if (name)
14637 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14638 name);
14639 else
14640 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14641 op);
14642 }
14643
14644 return (stack[stacki]);
14645 }
14646
14647 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14648 outside of the allocated space. Also enforce minimum>0. */
14649 if (stacki >= ARRAY_SIZE (stack) - 1)
14650 {
14651 complaint (&symfile_complaints,
14652 _("location description stack overflow"));
14653 return 0;
14654 }
14655
14656 if (stacki <= 0)
14657 {
14658 complaint (&symfile_complaints,
14659 _("location description stack underflow"));
14660 return 0;
14661 }
14662 }
14663 return (stack[stacki]);
14664 }
14665
14666 /* memory allocation interface */
14667
14668 static struct dwarf_block *
14669 dwarf_alloc_block (struct dwarf2_cu *cu)
14670 {
14671 struct dwarf_block *blk;
14672
14673 blk = (struct dwarf_block *)
14674 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14675 return (blk);
14676 }
14677
14678 static struct abbrev_info *
14679 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14680 {
14681 struct abbrev_info *abbrev;
14682
14683 abbrev = (struct abbrev_info *)
14684 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14685 memset (abbrev, 0, sizeof (struct abbrev_info));
14686 return (abbrev);
14687 }
14688
14689 static struct die_info *
14690 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14691 {
14692 struct die_info *die;
14693 size_t size = sizeof (struct die_info);
14694
14695 if (num_attrs > 1)
14696 size += (num_attrs - 1) * sizeof (struct attribute);
14697
14698 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14699 memset (die, 0, sizeof (struct die_info));
14700 return (die);
14701 }
14702
14703 \f
14704 /* Macro support. */
14705
14706 /* Return the full name of file number I in *LH's file name table.
14707 Use COMP_DIR as the name of the current directory of the
14708 compilation. The result is allocated using xmalloc; the caller is
14709 responsible for freeing it. */
14710 static char *
14711 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14712 {
14713 /* Is the file number a valid index into the line header's file name
14714 table? Remember that file numbers start with one, not zero. */
14715 if (1 <= file && file <= lh->num_file_names)
14716 {
14717 struct file_entry *fe = &lh->file_names[file - 1];
14718
14719 if (IS_ABSOLUTE_PATH (fe->name))
14720 return xstrdup (fe->name);
14721 else
14722 {
14723 const char *dir;
14724 int dir_len;
14725 char *full_name;
14726
14727 if (fe->dir_index)
14728 dir = lh->include_dirs[fe->dir_index - 1];
14729 else
14730 dir = comp_dir;
14731
14732 if (dir)
14733 {
14734 dir_len = strlen (dir);
14735 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14736 strcpy (full_name, dir);
14737 full_name[dir_len] = '/';
14738 strcpy (full_name + dir_len + 1, fe->name);
14739 return full_name;
14740 }
14741 else
14742 return xstrdup (fe->name);
14743 }
14744 }
14745 else
14746 {
14747 /* The compiler produced a bogus file number. We can at least
14748 record the macro definitions made in the file, even if we
14749 won't be able to find the file by name. */
14750 char fake_name[80];
14751
14752 sprintf (fake_name, "<bad macro file number %d>", file);
14753
14754 complaint (&symfile_complaints,
14755 _("bad file number in macro information (%d)"),
14756 file);
14757
14758 return xstrdup (fake_name);
14759 }
14760 }
14761
14762
14763 static struct macro_source_file *
14764 macro_start_file (int file, int line,
14765 struct macro_source_file *current_file,
14766 const char *comp_dir,
14767 struct line_header *lh, struct objfile *objfile)
14768 {
14769 /* The full name of this source file. */
14770 char *full_name = file_full_name (file, lh, comp_dir);
14771
14772 /* We don't create a macro table for this compilation unit
14773 at all until we actually get a filename. */
14774 if (! pending_macros)
14775 pending_macros = new_macro_table (&objfile->objfile_obstack,
14776 objfile->macro_cache);
14777
14778 if (! current_file)
14779 /* If we have no current file, then this must be the start_file
14780 directive for the compilation unit's main source file. */
14781 current_file = macro_set_main (pending_macros, full_name);
14782 else
14783 current_file = macro_include (current_file, line, full_name);
14784
14785 xfree (full_name);
14786
14787 return current_file;
14788 }
14789
14790
14791 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14792 followed by a null byte. */
14793 static char *
14794 copy_string (const char *buf, int len)
14795 {
14796 char *s = xmalloc (len + 1);
14797
14798 memcpy (s, buf, len);
14799 s[len] = '\0';
14800 return s;
14801 }
14802
14803
14804 static const char *
14805 consume_improper_spaces (const char *p, const char *body)
14806 {
14807 if (*p == ' ')
14808 {
14809 complaint (&symfile_complaints,
14810 _("macro definition contains spaces "
14811 "in formal argument list:\n`%s'"),
14812 body);
14813
14814 while (*p == ' ')
14815 p++;
14816 }
14817
14818 return p;
14819 }
14820
14821
14822 static void
14823 parse_macro_definition (struct macro_source_file *file, int line,
14824 const char *body)
14825 {
14826 const char *p;
14827
14828 /* The body string takes one of two forms. For object-like macro
14829 definitions, it should be:
14830
14831 <macro name> " " <definition>
14832
14833 For function-like macro definitions, it should be:
14834
14835 <macro name> "() " <definition>
14836 or
14837 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14838
14839 Spaces may appear only where explicitly indicated, and in the
14840 <definition>.
14841
14842 The Dwarf 2 spec says that an object-like macro's name is always
14843 followed by a space, but versions of GCC around March 2002 omit
14844 the space when the macro's definition is the empty string.
14845
14846 The Dwarf 2 spec says that there should be no spaces between the
14847 formal arguments in a function-like macro's formal argument list,
14848 but versions of GCC around March 2002 include spaces after the
14849 commas. */
14850
14851
14852 /* Find the extent of the macro name. The macro name is terminated
14853 by either a space or null character (for an object-like macro) or
14854 an opening paren (for a function-like macro). */
14855 for (p = body; *p; p++)
14856 if (*p == ' ' || *p == '(')
14857 break;
14858
14859 if (*p == ' ' || *p == '\0')
14860 {
14861 /* It's an object-like macro. */
14862 int name_len = p - body;
14863 char *name = copy_string (body, name_len);
14864 const char *replacement;
14865
14866 if (*p == ' ')
14867 replacement = body + name_len + 1;
14868 else
14869 {
14870 dwarf2_macro_malformed_definition_complaint (body);
14871 replacement = body + name_len;
14872 }
14873
14874 macro_define_object (file, line, name, replacement);
14875
14876 xfree (name);
14877 }
14878 else if (*p == '(')
14879 {
14880 /* It's a function-like macro. */
14881 char *name = copy_string (body, p - body);
14882 int argc = 0;
14883 int argv_size = 1;
14884 char **argv = xmalloc (argv_size * sizeof (*argv));
14885
14886 p++;
14887
14888 p = consume_improper_spaces (p, body);
14889
14890 /* Parse the formal argument list. */
14891 while (*p && *p != ')')
14892 {
14893 /* Find the extent of the current argument name. */
14894 const char *arg_start = p;
14895
14896 while (*p && *p != ',' && *p != ')' && *p != ' ')
14897 p++;
14898
14899 if (! *p || p == arg_start)
14900 dwarf2_macro_malformed_definition_complaint (body);
14901 else
14902 {
14903 /* Make sure argv has room for the new argument. */
14904 if (argc >= argv_size)
14905 {
14906 argv_size *= 2;
14907 argv = xrealloc (argv, argv_size * sizeof (*argv));
14908 }
14909
14910 argv[argc++] = copy_string (arg_start, p - arg_start);
14911 }
14912
14913 p = consume_improper_spaces (p, body);
14914
14915 /* Consume the comma, if present. */
14916 if (*p == ',')
14917 {
14918 p++;
14919
14920 p = consume_improper_spaces (p, body);
14921 }
14922 }
14923
14924 if (*p == ')')
14925 {
14926 p++;
14927
14928 if (*p == ' ')
14929 /* Perfectly formed definition, no complaints. */
14930 macro_define_function (file, line, name,
14931 argc, (const char **) argv,
14932 p + 1);
14933 else if (*p == '\0')
14934 {
14935 /* Complain, but do define it. */
14936 dwarf2_macro_malformed_definition_complaint (body);
14937 macro_define_function (file, line, name,
14938 argc, (const char **) argv,
14939 p);
14940 }
14941 else
14942 /* Just complain. */
14943 dwarf2_macro_malformed_definition_complaint (body);
14944 }
14945 else
14946 /* Just complain. */
14947 dwarf2_macro_malformed_definition_complaint (body);
14948
14949 xfree (name);
14950 {
14951 int i;
14952
14953 for (i = 0; i < argc; i++)
14954 xfree (argv[i]);
14955 }
14956 xfree (argv);
14957 }
14958 else
14959 dwarf2_macro_malformed_definition_complaint (body);
14960 }
14961
14962 /* Skip some bytes from BYTES according to the form given in FORM.
14963 Returns the new pointer. */
14964
14965 static gdb_byte *
14966 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14967 enum dwarf_form form,
14968 unsigned int offset_size,
14969 struct dwarf2_section_info *section)
14970 {
14971 unsigned int bytes_read;
14972
14973 switch (form)
14974 {
14975 case DW_FORM_data1:
14976 case DW_FORM_flag:
14977 ++bytes;
14978 break;
14979
14980 case DW_FORM_data2:
14981 bytes += 2;
14982 break;
14983
14984 case DW_FORM_data4:
14985 bytes += 4;
14986 break;
14987
14988 case DW_FORM_data8:
14989 bytes += 8;
14990 break;
14991
14992 case DW_FORM_string:
14993 read_direct_string (abfd, bytes, &bytes_read);
14994 bytes += bytes_read;
14995 break;
14996
14997 case DW_FORM_sec_offset:
14998 case DW_FORM_strp:
14999 bytes += offset_size;
15000 break;
15001
15002 case DW_FORM_block:
15003 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15004 bytes += bytes_read;
15005 break;
15006
15007 case DW_FORM_block1:
15008 bytes += 1 + read_1_byte (abfd, bytes);
15009 break;
15010 case DW_FORM_block2:
15011 bytes += 2 + read_2_bytes (abfd, bytes);
15012 break;
15013 case DW_FORM_block4:
15014 bytes += 4 + read_4_bytes (abfd, bytes);
15015 break;
15016
15017 case DW_FORM_sdata:
15018 case DW_FORM_udata:
15019 bytes = skip_leb128 (abfd, bytes);
15020 break;
15021
15022 default:
15023 {
15024 complain:
15025 complaint (&symfile_complaints,
15026 _("invalid form 0x%x in `%s'"),
15027 form,
15028 section->asection->name);
15029 return NULL;
15030 }
15031 }
15032
15033 return bytes;
15034 }
15035
15036 /* A helper for dwarf_decode_macros that handles skipping an unknown
15037 opcode. Returns an updated pointer to the macro data buffer; or,
15038 on error, issues a complaint and returns NULL. */
15039
15040 static gdb_byte *
15041 skip_unknown_opcode (unsigned int opcode,
15042 gdb_byte **opcode_definitions,
15043 gdb_byte *mac_ptr,
15044 bfd *abfd,
15045 unsigned int offset_size,
15046 struct dwarf2_section_info *section)
15047 {
15048 unsigned int bytes_read, i;
15049 unsigned long arg;
15050 gdb_byte *defn;
15051
15052 if (opcode_definitions[opcode] == NULL)
15053 {
15054 complaint (&symfile_complaints,
15055 _("unrecognized DW_MACFINO opcode 0x%x"),
15056 opcode);
15057 return NULL;
15058 }
15059
15060 defn = opcode_definitions[opcode];
15061 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15062 defn += bytes_read;
15063
15064 for (i = 0; i < arg; ++i)
15065 {
15066 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15067 if (mac_ptr == NULL)
15068 {
15069 /* skip_form_bytes already issued the complaint. */
15070 return NULL;
15071 }
15072 }
15073
15074 return mac_ptr;
15075 }
15076
15077 /* A helper function which parses the header of a macro section.
15078 If the macro section is the extended (for now called "GNU") type,
15079 then this updates *OFFSET_SIZE. Returns a pointer to just after
15080 the header, or issues a complaint and returns NULL on error. */
15081
15082 static gdb_byte *
15083 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15084 bfd *abfd,
15085 gdb_byte *mac_ptr,
15086 unsigned int *offset_size,
15087 int section_is_gnu)
15088 {
15089 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15090
15091 if (section_is_gnu)
15092 {
15093 unsigned int version, flags;
15094
15095 version = read_2_bytes (abfd, mac_ptr);
15096 if (version != 4)
15097 {
15098 complaint (&symfile_complaints,
15099 _("unrecognized version `%d' in .debug_macro section"),
15100 version);
15101 return NULL;
15102 }
15103 mac_ptr += 2;
15104
15105 flags = read_1_byte (abfd, mac_ptr);
15106 ++mac_ptr;
15107 *offset_size = (flags & 1) ? 8 : 4;
15108
15109 if ((flags & 2) != 0)
15110 /* We don't need the line table offset. */
15111 mac_ptr += *offset_size;
15112
15113 /* Vendor opcode descriptions. */
15114 if ((flags & 4) != 0)
15115 {
15116 unsigned int i, count;
15117
15118 count = read_1_byte (abfd, mac_ptr);
15119 ++mac_ptr;
15120 for (i = 0; i < count; ++i)
15121 {
15122 unsigned int opcode, bytes_read;
15123 unsigned long arg;
15124
15125 opcode = read_1_byte (abfd, mac_ptr);
15126 ++mac_ptr;
15127 opcode_definitions[opcode] = mac_ptr;
15128 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15129 mac_ptr += bytes_read;
15130 mac_ptr += arg;
15131 }
15132 }
15133 }
15134
15135 return mac_ptr;
15136 }
15137
15138 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15139 including DW_GNU_MACINFO_transparent_include. */
15140
15141 static void
15142 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15143 struct macro_source_file *current_file,
15144 struct line_header *lh, char *comp_dir,
15145 struct dwarf2_section_info *section,
15146 int section_is_gnu,
15147 unsigned int offset_size,
15148 struct objfile *objfile)
15149 {
15150 enum dwarf_macro_record_type macinfo_type;
15151 int at_commandline;
15152 gdb_byte *opcode_definitions[256];
15153
15154 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15155 &offset_size, section_is_gnu);
15156 if (mac_ptr == NULL)
15157 {
15158 /* We already issued a complaint. */
15159 return;
15160 }
15161
15162 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15163 GDB is still reading the definitions from command line. First
15164 DW_MACINFO_start_file will need to be ignored as it was already executed
15165 to create CURRENT_FILE for the main source holding also the command line
15166 definitions. On first met DW_MACINFO_start_file this flag is reset to
15167 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15168
15169 at_commandline = 1;
15170
15171 do
15172 {
15173 /* Do we at least have room for a macinfo type byte? */
15174 if (mac_ptr >= mac_end)
15175 {
15176 dwarf2_macros_too_long_complaint (section);
15177 break;
15178 }
15179
15180 macinfo_type = read_1_byte (abfd, mac_ptr);
15181 mac_ptr++;
15182
15183 /* Note that we rely on the fact that the corresponding GNU and
15184 DWARF constants are the same. */
15185 switch (macinfo_type)
15186 {
15187 /* A zero macinfo type indicates the end of the macro
15188 information. */
15189 case 0:
15190 break;
15191
15192 case DW_MACRO_GNU_define:
15193 case DW_MACRO_GNU_undef:
15194 case DW_MACRO_GNU_define_indirect:
15195 case DW_MACRO_GNU_undef_indirect:
15196 {
15197 unsigned int bytes_read;
15198 int line;
15199 char *body;
15200 int is_define;
15201
15202 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15203 mac_ptr += bytes_read;
15204
15205 if (macinfo_type == DW_MACRO_GNU_define
15206 || macinfo_type == DW_MACRO_GNU_undef)
15207 {
15208 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15209 mac_ptr += bytes_read;
15210 }
15211 else
15212 {
15213 LONGEST str_offset;
15214
15215 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15216 mac_ptr += offset_size;
15217
15218 body = read_indirect_string_at_offset (abfd, str_offset);
15219 }
15220
15221 is_define = (macinfo_type == DW_MACRO_GNU_define
15222 || macinfo_type == DW_MACRO_GNU_define_indirect);
15223 if (! current_file)
15224 {
15225 /* DWARF violation as no main source is present. */
15226 complaint (&symfile_complaints,
15227 _("debug info with no main source gives macro %s "
15228 "on line %d: %s"),
15229 is_define ? _("definition") : _("undefinition"),
15230 line, body);
15231 break;
15232 }
15233 if ((line == 0 && !at_commandline)
15234 || (line != 0 && at_commandline))
15235 complaint (&symfile_complaints,
15236 _("debug info gives %s macro %s with %s line %d: %s"),
15237 at_commandline ? _("command-line") : _("in-file"),
15238 is_define ? _("definition") : _("undefinition"),
15239 line == 0 ? _("zero") : _("non-zero"), line, body);
15240
15241 if (is_define)
15242 parse_macro_definition (current_file, line, body);
15243 else
15244 {
15245 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15246 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15247 macro_undef (current_file, line, body);
15248 }
15249 }
15250 break;
15251
15252 case DW_MACRO_GNU_start_file:
15253 {
15254 unsigned int bytes_read;
15255 int line, file;
15256
15257 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15258 mac_ptr += bytes_read;
15259 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15260 mac_ptr += bytes_read;
15261
15262 if ((line == 0 && !at_commandline)
15263 || (line != 0 && at_commandline))
15264 complaint (&symfile_complaints,
15265 _("debug info gives source %d included "
15266 "from %s at %s line %d"),
15267 file, at_commandline ? _("command-line") : _("file"),
15268 line == 0 ? _("zero") : _("non-zero"), line);
15269
15270 if (at_commandline)
15271 {
15272 /* This DW_MACRO_GNU_start_file was executed in the
15273 pass one. */
15274 at_commandline = 0;
15275 }
15276 else
15277 current_file = macro_start_file (file, line,
15278 current_file, comp_dir,
15279 lh, objfile);
15280 }
15281 break;
15282
15283 case DW_MACRO_GNU_end_file:
15284 if (! current_file)
15285 complaint (&symfile_complaints,
15286 _("macro debug info has an unmatched "
15287 "`close_file' directive"));
15288 else
15289 {
15290 current_file = current_file->included_by;
15291 if (! current_file)
15292 {
15293 enum dwarf_macro_record_type next_type;
15294
15295 /* GCC circa March 2002 doesn't produce the zero
15296 type byte marking the end of the compilation
15297 unit. Complain if it's not there, but exit no
15298 matter what. */
15299
15300 /* Do we at least have room for a macinfo type byte? */
15301 if (mac_ptr >= mac_end)
15302 {
15303 dwarf2_macros_too_long_complaint (section);
15304 return;
15305 }
15306
15307 /* We don't increment mac_ptr here, so this is just
15308 a look-ahead. */
15309 next_type = read_1_byte (abfd, mac_ptr);
15310 if (next_type != 0)
15311 complaint (&symfile_complaints,
15312 _("no terminating 0-type entry for "
15313 "macros in `.debug_macinfo' section"));
15314
15315 return;
15316 }
15317 }
15318 break;
15319
15320 case DW_MACRO_GNU_transparent_include:
15321 {
15322 LONGEST offset;
15323
15324 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15325 mac_ptr += offset_size;
15326
15327 dwarf_decode_macro_bytes (abfd,
15328 section->buffer + offset,
15329 mac_end, current_file,
15330 lh, comp_dir,
15331 section, section_is_gnu,
15332 offset_size, objfile);
15333 }
15334 break;
15335
15336 case DW_MACINFO_vendor_ext:
15337 if (!section_is_gnu)
15338 {
15339 unsigned int bytes_read;
15340 int constant;
15341
15342 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15343 mac_ptr += bytes_read;
15344 read_direct_string (abfd, mac_ptr, &bytes_read);
15345 mac_ptr += bytes_read;
15346
15347 /* We don't recognize any vendor extensions. */
15348 break;
15349 }
15350 /* FALLTHROUGH */
15351
15352 default:
15353 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15354 mac_ptr, abfd, offset_size,
15355 section);
15356 if (mac_ptr == NULL)
15357 return;
15358 break;
15359 }
15360 } while (macinfo_type != 0);
15361 }
15362
15363 static void
15364 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15365 char *comp_dir, bfd *abfd,
15366 struct dwarf2_cu *cu,
15367 struct dwarf2_section_info *section,
15368 int section_is_gnu)
15369 {
15370 struct objfile *objfile = dwarf2_per_objfile->objfile;
15371 gdb_byte *mac_ptr, *mac_end;
15372 struct macro_source_file *current_file = 0;
15373 enum dwarf_macro_record_type macinfo_type;
15374 unsigned int offset_size = cu->header.offset_size;
15375 gdb_byte *opcode_definitions[256];
15376
15377 dwarf2_read_section (objfile, section);
15378 if (section->buffer == NULL)
15379 {
15380 complaint (&symfile_complaints, _("missing %s section"),
15381 section->asection->name);
15382 return;
15383 }
15384
15385 /* First pass: Find the name of the base filename.
15386 This filename is needed in order to process all macros whose definition
15387 (or undefinition) comes from the command line. These macros are defined
15388 before the first DW_MACINFO_start_file entry, and yet still need to be
15389 associated to the base file.
15390
15391 To determine the base file name, we scan the macro definitions until we
15392 reach the first DW_MACINFO_start_file entry. We then initialize
15393 CURRENT_FILE accordingly so that any macro definition found before the
15394 first DW_MACINFO_start_file can still be associated to the base file. */
15395
15396 mac_ptr = section->buffer + offset;
15397 mac_end = section->buffer + section->size;
15398
15399 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15400 &offset_size, section_is_gnu);
15401 if (mac_ptr == NULL)
15402 {
15403 /* We already issued a complaint. */
15404 return;
15405 }
15406
15407 do
15408 {
15409 /* Do we at least have room for a macinfo type byte? */
15410 if (mac_ptr >= mac_end)
15411 {
15412 /* Complaint is printed during the second pass as GDB will probably
15413 stop the first pass earlier upon finding
15414 DW_MACINFO_start_file. */
15415 break;
15416 }
15417
15418 macinfo_type = read_1_byte (abfd, mac_ptr);
15419 mac_ptr++;
15420
15421 /* Note that we rely on the fact that the corresponding GNU and
15422 DWARF constants are the same. */
15423 switch (macinfo_type)
15424 {
15425 /* A zero macinfo type indicates the end of the macro
15426 information. */
15427 case 0:
15428 break;
15429
15430 case DW_MACRO_GNU_define:
15431 case DW_MACRO_GNU_undef:
15432 /* Only skip the data by MAC_PTR. */
15433 {
15434 unsigned int bytes_read;
15435
15436 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15437 mac_ptr += bytes_read;
15438 read_direct_string (abfd, mac_ptr, &bytes_read);
15439 mac_ptr += bytes_read;
15440 }
15441 break;
15442
15443 case DW_MACRO_GNU_start_file:
15444 {
15445 unsigned int bytes_read;
15446 int line, file;
15447
15448 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15449 mac_ptr += bytes_read;
15450 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15451 mac_ptr += bytes_read;
15452
15453 current_file = macro_start_file (file, line, current_file,
15454 comp_dir, lh, objfile);
15455 }
15456 break;
15457
15458 case DW_MACRO_GNU_end_file:
15459 /* No data to skip by MAC_PTR. */
15460 break;
15461
15462 case DW_MACRO_GNU_define_indirect:
15463 case DW_MACRO_GNU_undef_indirect:
15464 {
15465 unsigned int bytes_read;
15466
15467 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15468 mac_ptr += bytes_read;
15469 mac_ptr += offset_size;
15470 }
15471 break;
15472
15473 case DW_MACRO_GNU_transparent_include:
15474 /* Note that, according to the spec, a transparent include
15475 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15476 skip this opcode. */
15477 mac_ptr += offset_size;
15478 break;
15479
15480 case DW_MACINFO_vendor_ext:
15481 /* Only skip the data by MAC_PTR. */
15482 if (!section_is_gnu)
15483 {
15484 unsigned int bytes_read;
15485
15486 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15487 mac_ptr += bytes_read;
15488 read_direct_string (abfd, mac_ptr, &bytes_read);
15489 mac_ptr += bytes_read;
15490 }
15491 /* FALLTHROUGH */
15492
15493 default:
15494 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15495 mac_ptr, abfd, offset_size,
15496 section);
15497 if (mac_ptr == NULL)
15498 return;
15499 break;
15500 }
15501 } while (macinfo_type != 0 && current_file == NULL);
15502
15503 /* Second pass: Process all entries.
15504
15505 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15506 command-line macro definitions/undefinitions. This flag is unset when we
15507 reach the first DW_MACINFO_start_file entry. */
15508
15509 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15510 current_file, lh, comp_dir, section, section_is_gnu,
15511 offset_size, objfile);
15512 }
15513
15514 /* Check if the attribute's form is a DW_FORM_block*
15515 if so return true else false. */
15516 static int
15517 attr_form_is_block (struct attribute *attr)
15518 {
15519 return (attr == NULL ? 0 :
15520 attr->form == DW_FORM_block1
15521 || attr->form == DW_FORM_block2
15522 || attr->form == DW_FORM_block4
15523 || attr->form == DW_FORM_block
15524 || attr->form == DW_FORM_exprloc);
15525 }
15526
15527 /* Return non-zero if ATTR's value is a section offset --- classes
15528 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15529 You may use DW_UNSND (attr) to retrieve such offsets.
15530
15531 Section 7.5.4, "Attribute Encodings", explains that no attribute
15532 may have a value that belongs to more than one of these classes; it
15533 would be ambiguous if we did, because we use the same forms for all
15534 of them. */
15535 static int
15536 attr_form_is_section_offset (struct attribute *attr)
15537 {
15538 return (attr->form == DW_FORM_data4
15539 || attr->form == DW_FORM_data8
15540 || attr->form == DW_FORM_sec_offset);
15541 }
15542
15543
15544 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15545 zero otherwise. When this function returns true, you can apply
15546 dwarf2_get_attr_constant_value to it.
15547
15548 However, note that for some attributes you must check
15549 attr_form_is_section_offset before using this test. DW_FORM_data4
15550 and DW_FORM_data8 are members of both the constant class, and of
15551 the classes that contain offsets into other debug sections
15552 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15553 that, if an attribute's can be either a constant or one of the
15554 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15555 taken as section offsets, not constants. */
15556 static int
15557 attr_form_is_constant (struct attribute *attr)
15558 {
15559 switch (attr->form)
15560 {
15561 case DW_FORM_sdata:
15562 case DW_FORM_udata:
15563 case DW_FORM_data1:
15564 case DW_FORM_data2:
15565 case DW_FORM_data4:
15566 case DW_FORM_data8:
15567 return 1;
15568 default:
15569 return 0;
15570 }
15571 }
15572
15573 /* A helper function that fills in a dwarf2_loclist_baton. */
15574
15575 static void
15576 fill_in_loclist_baton (struct dwarf2_cu *cu,
15577 struct dwarf2_loclist_baton *baton,
15578 struct attribute *attr)
15579 {
15580 dwarf2_read_section (dwarf2_per_objfile->objfile,
15581 &dwarf2_per_objfile->loc);
15582
15583 baton->per_cu = cu->per_cu;
15584 gdb_assert (baton->per_cu);
15585 /* We don't know how long the location list is, but make sure we
15586 don't run off the edge of the section. */
15587 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15588 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15589 baton->base_address = cu->base_address;
15590 }
15591
15592 static void
15593 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15594 struct dwarf2_cu *cu)
15595 {
15596 struct objfile *objfile = dwarf2_per_objfile->objfile;
15597
15598 if (attr_form_is_section_offset (attr)
15599 /* ".debug_loc" may not exist at all, or the offset may be outside
15600 the section. If so, fall through to the complaint in the
15601 other branch. */
15602 && DW_UNSND (attr) < dwarf2_section_size (objfile,
15603 &dwarf2_per_objfile->loc))
15604 {
15605 struct dwarf2_loclist_baton *baton;
15606
15607 baton = obstack_alloc (&objfile->objfile_obstack,
15608 sizeof (struct dwarf2_loclist_baton));
15609
15610 fill_in_loclist_baton (cu, baton, attr);
15611
15612 if (cu->base_known == 0)
15613 complaint (&symfile_complaints,
15614 _("Location list used without "
15615 "specifying the CU base address."));
15616
15617 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15618 SYMBOL_LOCATION_BATON (sym) = baton;
15619 }
15620 else
15621 {
15622 struct dwarf2_locexpr_baton *baton;
15623
15624 baton = obstack_alloc (&objfile->objfile_obstack,
15625 sizeof (struct dwarf2_locexpr_baton));
15626 baton->per_cu = cu->per_cu;
15627 gdb_assert (baton->per_cu);
15628
15629 if (attr_form_is_block (attr))
15630 {
15631 /* Note that we're just copying the block's data pointer
15632 here, not the actual data. We're still pointing into the
15633 info_buffer for SYM's objfile; right now we never release
15634 that buffer, but when we do clean up properly this may
15635 need to change. */
15636 baton->size = DW_BLOCK (attr)->size;
15637 baton->data = DW_BLOCK (attr)->data;
15638 }
15639 else
15640 {
15641 dwarf2_invalid_attrib_class_complaint ("location description",
15642 SYMBOL_NATURAL_NAME (sym));
15643 baton->size = 0;
15644 }
15645
15646 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15647 SYMBOL_LOCATION_BATON (sym) = baton;
15648 }
15649 }
15650
15651 /* Return the OBJFILE associated with the compilation unit CU. If CU
15652 came from a separate debuginfo file, then the master objfile is
15653 returned. */
15654
15655 struct objfile *
15656 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15657 {
15658 struct objfile *objfile = per_cu->objfile;
15659
15660 /* Return the master objfile, so that we can report and look up the
15661 correct file containing this variable. */
15662 if (objfile->separate_debug_objfile_backlink)
15663 objfile = objfile->separate_debug_objfile_backlink;
15664
15665 return objfile;
15666 }
15667
15668 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15669 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15670 CU_HEADERP first. */
15671
15672 static const struct comp_unit_head *
15673 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15674 struct dwarf2_per_cu_data *per_cu)
15675 {
15676 struct objfile *objfile;
15677 struct dwarf2_per_objfile *per_objfile;
15678 gdb_byte *info_ptr;
15679
15680 if (per_cu->cu)
15681 return &per_cu->cu->header;
15682
15683 objfile = per_cu->objfile;
15684 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15685 info_ptr = per_objfile->info.buffer + per_cu->offset;
15686
15687 memset (cu_headerp, 0, sizeof (*cu_headerp));
15688 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15689
15690 return cu_headerp;
15691 }
15692
15693 /* Return the address size given in the compilation unit header for CU. */
15694
15695 int
15696 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15697 {
15698 struct comp_unit_head cu_header_local;
15699 const struct comp_unit_head *cu_headerp;
15700
15701 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15702
15703 return cu_headerp->addr_size;
15704 }
15705
15706 /* Return the offset size given in the compilation unit header for CU. */
15707
15708 int
15709 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15710 {
15711 struct comp_unit_head cu_header_local;
15712 const struct comp_unit_head *cu_headerp;
15713
15714 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15715
15716 return cu_headerp->offset_size;
15717 }
15718
15719 /* See its dwarf2loc.h declaration. */
15720
15721 int
15722 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15723 {
15724 struct comp_unit_head cu_header_local;
15725 const struct comp_unit_head *cu_headerp;
15726
15727 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15728
15729 if (cu_headerp->version == 2)
15730 return cu_headerp->addr_size;
15731 else
15732 return cu_headerp->offset_size;
15733 }
15734
15735 /* Return the text offset of the CU. The returned offset comes from
15736 this CU's objfile. If this objfile came from a separate debuginfo
15737 file, then the offset may be different from the corresponding
15738 offset in the parent objfile. */
15739
15740 CORE_ADDR
15741 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15742 {
15743 struct objfile *objfile = per_cu->objfile;
15744
15745 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15746 }
15747
15748 /* Locate the .debug_info compilation unit from CU's objfile which contains
15749 the DIE at OFFSET. Raises an error on failure. */
15750
15751 static struct dwarf2_per_cu_data *
15752 dwarf2_find_containing_comp_unit (unsigned int offset,
15753 struct objfile *objfile)
15754 {
15755 struct dwarf2_per_cu_data *this_cu;
15756 int low, high;
15757
15758 low = 0;
15759 high = dwarf2_per_objfile->n_comp_units - 1;
15760 while (high > low)
15761 {
15762 int mid = low + (high - low) / 2;
15763
15764 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15765 high = mid;
15766 else
15767 low = mid + 1;
15768 }
15769 gdb_assert (low == high);
15770 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15771 {
15772 if (low == 0)
15773 error (_("Dwarf Error: could not find partial DIE containing "
15774 "offset 0x%lx [in module %s]"),
15775 (long) offset, bfd_get_filename (objfile->obfd));
15776
15777 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15778 return dwarf2_per_objfile->all_comp_units[low-1];
15779 }
15780 else
15781 {
15782 this_cu = dwarf2_per_objfile->all_comp_units[low];
15783 if (low == dwarf2_per_objfile->n_comp_units - 1
15784 && offset >= this_cu->offset + this_cu->length)
15785 error (_("invalid dwarf2 offset %u"), offset);
15786 gdb_assert (offset < this_cu->offset + this_cu->length);
15787 return this_cu;
15788 }
15789 }
15790
15791 /* Initialize dwarf2_cu CU, owned by PER_CU. */
15792
15793 static void
15794 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15795 {
15796 memset (cu, 0, sizeof (*cu));
15797 per_cu->cu = cu;
15798 cu->per_cu = per_cu;
15799 cu->objfile = per_cu->objfile;
15800 obstack_init (&cu->comp_unit_obstack);
15801 }
15802
15803 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15804
15805 static void
15806 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15807 {
15808 struct attribute *attr;
15809
15810 /* Set the language we're debugging. */
15811 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15812 if (attr)
15813 set_cu_language (DW_UNSND (attr), cu);
15814 else
15815 {
15816 cu->language = language_minimal;
15817 cu->language_defn = language_def (cu->language);
15818 }
15819 }
15820
15821 /* Release one cached compilation unit, CU. We unlink it from the tree
15822 of compilation units, but we don't remove it from the read_in_chain;
15823 the caller is responsible for that.
15824 NOTE: DATA is a void * because this function is also used as a
15825 cleanup routine. */
15826
15827 static void
15828 free_heap_comp_unit (void *data)
15829 {
15830 struct dwarf2_cu *cu = data;
15831
15832 gdb_assert (cu->per_cu != NULL);
15833 cu->per_cu->cu = NULL;
15834 cu->per_cu = NULL;
15835
15836 obstack_free (&cu->comp_unit_obstack, NULL);
15837
15838 xfree (cu);
15839 }
15840
15841 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15842 when we're finished with it. We can't free the pointer itself, but be
15843 sure to unlink it from the cache. Also release any associated storage
15844 and perform cache maintenance.
15845
15846 Only used during partial symbol parsing. */
15847
15848 static void
15849 free_stack_comp_unit (void *data)
15850 {
15851 struct dwarf2_cu *cu = data;
15852
15853 gdb_assert (cu->per_cu != NULL);
15854 cu->per_cu->cu = NULL;
15855 cu->per_cu = NULL;
15856
15857 obstack_free (&cu->comp_unit_obstack, NULL);
15858 cu->partial_dies = NULL;
15859
15860 /* The previous code only did this if per_cu != NULL.
15861 But that would always succeed, so now we just unconditionally do
15862 the aging. This seems like the wrong place to do such aging,
15863 but cleaning that up is left for later. */
15864 age_cached_comp_units ();
15865 }
15866
15867 /* Free all cached compilation units. */
15868
15869 static void
15870 free_cached_comp_units (void *data)
15871 {
15872 struct dwarf2_per_cu_data *per_cu, **last_chain;
15873
15874 per_cu = dwarf2_per_objfile->read_in_chain;
15875 last_chain = &dwarf2_per_objfile->read_in_chain;
15876 while (per_cu != NULL)
15877 {
15878 struct dwarf2_per_cu_data *next_cu;
15879
15880 next_cu = per_cu->cu->read_in_chain;
15881
15882 free_heap_comp_unit (per_cu->cu);
15883 *last_chain = next_cu;
15884
15885 per_cu = next_cu;
15886 }
15887 }
15888
15889 /* Increase the age counter on each cached compilation unit, and free
15890 any that are too old. */
15891
15892 static void
15893 age_cached_comp_units (void)
15894 {
15895 struct dwarf2_per_cu_data *per_cu, **last_chain;
15896
15897 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15898 per_cu = dwarf2_per_objfile->read_in_chain;
15899 while (per_cu != NULL)
15900 {
15901 per_cu->cu->last_used ++;
15902 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15903 dwarf2_mark (per_cu->cu);
15904 per_cu = per_cu->cu->read_in_chain;
15905 }
15906
15907 per_cu = dwarf2_per_objfile->read_in_chain;
15908 last_chain = &dwarf2_per_objfile->read_in_chain;
15909 while (per_cu != NULL)
15910 {
15911 struct dwarf2_per_cu_data *next_cu;
15912
15913 next_cu = per_cu->cu->read_in_chain;
15914
15915 if (!per_cu->cu->mark)
15916 {
15917 free_heap_comp_unit (per_cu->cu);
15918 *last_chain = next_cu;
15919 }
15920 else
15921 last_chain = &per_cu->cu->read_in_chain;
15922
15923 per_cu = next_cu;
15924 }
15925 }
15926
15927 /* Remove a single compilation unit from the cache. */
15928
15929 static void
15930 free_one_cached_comp_unit (void *target_cu)
15931 {
15932 struct dwarf2_per_cu_data *per_cu, **last_chain;
15933
15934 per_cu = dwarf2_per_objfile->read_in_chain;
15935 last_chain = &dwarf2_per_objfile->read_in_chain;
15936 while (per_cu != NULL)
15937 {
15938 struct dwarf2_per_cu_data *next_cu;
15939
15940 next_cu = per_cu->cu->read_in_chain;
15941
15942 if (per_cu->cu == target_cu)
15943 {
15944 free_heap_comp_unit (per_cu->cu);
15945 *last_chain = next_cu;
15946 break;
15947 }
15948 else
15949 last_chain = &per_cu->cu->read_in_chain;
15950
15951 per_cu = next_cu;
15952 }
15953 }
15954
15955 /* Release all extra memory associated with OBJFILE. */
15956
15957 void
15958 dwarf2_free_objfile (struct objfile *objfile)
15959 {
15960 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15961
15962 if (dwarf2_per_objfile == NULL)
15963 return;
15964
15965 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15966 free_cached_comp_units (NULL);
15967
15968 if (dwarf2_per_objfile->quick_file_names_table)
15969 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15970
15971 /* Everything else should be on the objfile obstack. */
15972 }
15973
15974 /* A pair of DIE offset and GDB type pointer. We store these
15975 in a hash table separate from the DIEs, and preserve them
15976 when the DIEs are flushed out of cache. */
15977
15978 struct dwarf2_offset_and_type
15979 {
15980 unsigned int offset;
15981 struct type *type;
15982 };
15983
15984 /* Hash function for a dwarf2_offset_and_type. */
15985
15986 static hashval_t
15987 offset_and_type_hash (const void *item)
15988 {
15989 const struct dwarf2_offset_and_type *ofs = item;
15990
15991 return ofs->offset;
15992 }
15993
15994 /* Equality function for a dwarf2_offset_and_type. */
15995
15996 static int
15997 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15998 {
15999 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16000 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16001
16002 return ofs_lhs->offset == ofs_rhs->offset;
16003 }
16004
16005 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16006 table if necessary. For convenience, return TYPE.
16007
16008 The DIEs reading must have careful ordering to:
16009 * Not cause infite loops trying to read in DIEs as a prerequisite for
16010 reading current DIE.
16011 * Not trying to dereference contents of still incompletely read in types
16012 while reading in other DIEs.
16013 * Enable referencing still incompletely read in types just by a pointer to
16014 the type without accessing its fields.
16015
16016 Therefore caller should follow these rules:
16017 * Try to fetch any prerequisite types we may need to build this DIE type
16018 before building the type and calling set_die_type.
16019 * After building type call set_die_type for current DIE as soon as
16020 possible before fetching more types to complete the current type.
16021 * Make the type as complete as possible before fetching more types. */
16022
16023 static struct type *
16024 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16025 {
16026 struct dwarf2_offset_and_type **slot, ofs;
16027 struct objfile *objfile = cu->objfile;
16028 htab_t *type_hash_ptr;
16029
16030 /* For Ada types, make sure that the gnat-specific data is always
16031 initialized (if not already set). There are a few types where
16032 we should not be doing so, because the type-specific area is
16033 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16034 where the type-specific area is used to store the floatformat).
16035 But this is not a problem, because the gnat-specific information
16036 is actually not needed for these types. */
16037 if (need_gnat_info (cu)
16038 && TYPE_CODE (type) != TYPE_CODE_FUNC
16039 && TYPE_CODE (type) != TYPE_CODE_FLT
16040 && !HAVE_GNAT_AUX_INFO (type))
16041 INIT_GNAT_SPECIFIC (type);
16042
16043 if (cu->per_cu->debug_types_section)
16044 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16045 else
16046 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16047
16048 if (*type_hash_ptr == NULL)
16049 {
16050 *type_hash_ptr
16051 = htab_create_alloc_ex (127,
16052 offset_and_type_hash,
16053 offset_and_type_eq,
16054 NULL,
16055 &objfile->objfile_obstack,
16056 hashtab_obstack_allocate,
16057 dummy_obstack_deallocate);
16058 }
16059
16060 ofs.offset = die->offset;
16061 ofs.type = type;
16062 slot = (struct dwarf2_offset_and_type **)
16063 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16064 if (*slot)
16065 complaint (&symfile_complaints,
16066 _("A problem internal to GDB: DIE 0x%x has type already set"),
16067 die->offset);
16068 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16069 **slot = ofs;
16070 return type;
16071 }
16072
16073 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16074 table, or return NULL if the die does not have a saved type. */
16075
16076 static struct type *
16077 get_die_type_at_offset (unsigned int offset,
16078 struct dwarf2_per_cu_data *per_cu)
16079 {
16080 struct dwarf2_offset_and_type *slot, ofs;
16081 htab_t type_hash;
16082
16083 if (per_cu->debug_types_section)
16084 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16085 else
16086 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16087 if (type_hash == NULL)
16088 return NULL;
16089
16090 ofs.offset = offset;
16091 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16092 if (slot)
16093 return slot->type;
16094 else
16095 return NULL;
16096 }
16097
16098 /* Look up the type for DIE in the appropriate type_hash table,
16099 or return NULL if DIE does not have a saved type. */
16100
16101 static struct type *
16102 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16103 {
16104 return get_die_type_at_offset (die->offset, cu->per_cu);
16105 }
16106
16107 /* Add a dependence relationship from CU to REF_PER_CU. */
16108
16109 static void
16110 dwarf2_add_dependence (struct dwarf2_cu *cu,
16111 struct dwarf2_per_cu_data *ref_per_cu)
16112 {
16113 void **slot;
16114
16115 if (cu->dependencies == NULL)
16116 cu->dependencies
16117 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16118 NULL, &cu->comp_unit_obstack,
16119 hashtab_obstack_allocate,
16120 dummy_obstack_deallocate);
16121
16122 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16123 if (*slot == NULL)
16124 *slot = ref_per_cu;
16125 }
16126
16127 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16128 Set the mark field in every compilation unit in the
16129 cache that we must keep because we are keeping CU. */
16130
16131 static int
16132 dwarf2_mark_helper (void **slot, void *data)
16133 {
16134 struct dwarf2_per_cu_data *per_cu;
16135
16136 per_cu = (struct dwarf2_per_cu_data *) *slot;
16137
16138 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16139 reading of the chain. As such dependencies remain valid it is not much
16140 useful to track and undo them during QUIT cleanups. */
16141 if (per_cu->cu == NULL)
16142 return 1;
16143
16144 if (per_cu->cu->mark)
16145 return 1;
16146 per_cu->cu->mark = 1;
16147
16148 if (per_cu->cu->dependencies != NULL)
16149 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16150
16151 return 1;
16152 }
16153
16154 /* Set the mark field in CU and in every other compilation unit in the
16155 cache that we must keep because we are keeping CU. */
16156
16157 static void
16158 dwarf2_mark (struct dwarf2_cu *cu)
16159 {
16160 if (cu->mark)
16161 return;
16162 cu->mark = 1;
16163 if (cu->dependencies != NULL)
16164 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16165 }
16166
16167 static void
16168 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16169 {
16170 while (per_cu)
16171 {
16172 per_cu->cu->mark = 0;
16173 per_cu = per_cu->cu->read_in_chain;
16174 }
16175 }
16176
16177 /* Trivial hash function for partial_die_info: the hash value of a DIE
16178 is its offset in .debug_info for this objfile. */
16179
16180 static hashval_t
16181 partial_die_hash (const void *item)
16182 {
16183 const struct partial_die_info *part_die = item;
16184
16185 return part_die->offset;
16186 }
16187
16188 /* Trivial comparison function for partial_die_info structures: two DIEs
16189 are equal if they have the same offset. */
16190
16191 static int
16192 partial_die_eq (const void *item_lhs, const void *item_rhs)
16193 {
16194 const struct partial_die_info *part_die_lhs = item_lhs;
16195 const struct partial_die_info *part_die_rhs = item_rhs;
16196
16197 return part_die_lhs->offset == part_die_rhs->offset;
16198 }
16199
16200 static struct cmd_list_element *set_dwarf2_cmdlist;
16201 static struct cmd_list_element *show_dwarf2_cmdlist;
16202
16203 static void
16204 set_dwarf2_cmd (char *args, int from_tty)
16205 {
16206 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16207 }
16208
16209 static void
16210 show_dwarf2_cmd (char *args, int from_tty)
16211 {
16212 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16213 }
16214
16215 /* If section described by INFO was mmapped, munmap it now. */
16216
16217 static void
16218 munmap_section_buffer (struct dwarf2_section_info *info)
16219 {
16220 if (info->map_addr != NULL)
16221 {
16222 #ifdef HAVE_MMAP
16223 int res;
16224
16225 res = munmap (info->map_addr, info->map_len);
16226 gdb_assert (res == 0);
16227 #else
16228 /* Without HAVE_MMAP, we should never be here to begin with. */
16229 gdb_assert_not_reached ("no mmap support");
16230 #endif
16231 }
16232 }
16233
16234 /* munmap debug sections for OBJFILE, if necessary. */
16235
16236 static void
16237 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16238 {
16239 struct dwarf2_per_objfile *data = d;
16240 int ix;
16241 struct dwarf2_section_info *section;
16242
16243 /* This is sorted according to the order they're defined in to make it easier
16244 to keep in sync. */
16245 munmap_section_buffer (&data->info);
16246 munmap_section_buffer (&data->abbrev);
16247 munmap_section_buffer (&data->line);
16248 munmap_section_buffer (&data->loc);
16249 munmap_section_buffer (&data->macinfo);
16250 munmap_section_buffer (&data->macro);
16251 munmap_section_buffer (&data->str);
16252 munmap_section_buffer (&data->ranges);
16253 munmap_section_buffer (&data->frame);
16254 munmap_section_buffer (&data->eh_frame);
16255 munmap_section_buffer (&data->gdb_index);
16256
16257 for (ix = 0;
16258 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16259 ++ix)
16260 munmap_section_buffer (section);
16261
16262 VEC_free (dwarf2_section_info_def, data->types);
16263 }
16264
16265 \f
16266 /* The "save gdb-index" command. */
16267
16268 /* The contents of the hash table we create when building the string
16269 table. */
16270 struct strtab_entry
16271 {
16272 offset_type offset;
16273 const char *str;
16274 };
16275
16276 /* Hash function for a strtab_entry.
16277
16278 Function is used only during write_hash_table so no index format backward
16279 compatibility is needed. */
16280
16281 static hashval_t
16282 hash_strtab_entry (const void *e)
16283 {
16284 const struct strtab_entry *entry = e;
16285 return mapped_index_string_hash (INT_MAX, entry->str);
16286 }
16287
16288 /* Equality function for a strtab_entry. */
16289
16290 static int
16291 eq_strtab_entry (const void *a, const void *b)
16292 {
16293 const struct strtab_entry *ea = a;
16294 const struct strtab_entry *eb = b;
16295 return !strcmp (ea->str, eb->str);
16296 }
16297
16298 /* Create a strtab_entry hash table. */
16299
16300 static htab_t
16301 create_strtab (void)
16302 {
16303 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16304 xfree, xcalloc, xfree);
16305 }
16306
16307 /* Add a string to the constant pool. Return the string's offset in
16308 host order. */
16309
16310 static offset_type
16311 add_string (htab_t table, struct obstack *cpool, const char *str)
16312 {
16313 void **slot;
16314 struct strtab_entry entry;
16315 struct strtab_entry *result;
16316
16317 entry.str = str;
16318 slot = htab_find_slot (table, &entry, INSERT);
16319 if (*slot)
16320 result = *slot;
16321 else
16322 {
16323 result = XNEW (struct strtab_entry);
16324 result->offset = obstack_object_size (cpool);
16325 result->str = str;
16326 obstack_grow_str0 (cpool, str);
16327 *slot = result;
16328 }
16329 return result->offset;
16330 }
16331
16332 /* An entry in the symbol table. */
16333 struct symtab_index_entry
16334 {
16335 /* The name of the symbol. */
16336 const char *name;
16337 /* The offset of the name in the constant pool. */
16338 offset_type index_offset;
16339 /* A sorted vector of the indices of all the CUs that hold an object
16340 of this name. */
16341 VEC (offset_type) *cu_indices;
16342 };
16343
16344 /* The symbol table. This is a power-of-2-sized hash table. */
16345 struct mapped_symtab
16346 {
16347 offset_type n_elements;
16348 offset_type size;
16349 struct symtab_index_entry **data;
16350 };
16351
16352 /* Hash function for a symtab_index_entry. */
16353
16354 static hashval_t
16355 hash_symtab_entry (const void *e)
16356 {
16357 const struct symtab_index_entry *entry = e;
16358 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16359 sizeof (offset_type) * VEC_length (offset_type,
16360 entry->cu_indices),
16361 0);
16362 }
16363
16364 /* Equality function for a symtab_index_entry. */
16365
16366 static int
16367 eq_symtab_entry (const void *a, const void *b)
16368 {
16369 const struct symtab_index_entry *ea = a;
16370 const struct symtab_index_entry *eb = b;
16371 int len = VEC_length (offset_type, ea->cu_indices);
16372 if (len != VEC_length (offset_type, eb->cu_indices))
16373 return 0;
16374 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16375 VEC_address (offset_type, eb->cu_indices),
16376 sizeof (offset_type) * len);
16377 }
16378
16379 /* Destroy a symtab_index_entry. */
16380
16381 static void
16382 delete_symtab_entry (void *p)
16383 {
16384 struct symtab_index_entry *entry = p;
16385 VEC_free (offset_type, entry->cu_indices);
16386 xfree (entry);
16387 }
16388
16389 /* Create a hash table holding symtab_index_entry objects. */
16390
16391 static htab_t
16392 create_symbol_hash_table (void)
16393 {
16394 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16395 delete_symtab_entry, xcalloc, xfree);
16396 }
16397
16398 /* Create a new mapped symtab object. */
16399
16400 static struct mapped_symtab *
16401 create_mapped_symtab (void)
16402 {
16403 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16404 symtab->n_elements = 0;
16405 symtab->size = 1024;
16406 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16407 return symtab;
16408 }
16409
16410 /* Destroy a mapped_symtab. */
16411
16412 static void
16413 cleanup_mapped_symtab (void *p)
16414 {
16415 struct mapped_symtab *symtab = p;
16416 /* The contents of the array are freed when the other hash table is
16417 destroyed. */
16418 xfree (symtab->data);
16419 xfree (symtab);
16420 }
16421
16422 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16423 the slot.
16424
16425 Function is used only during write_hash_table so no index format backward
16426 compatibility is needed. */
16427
16428 static struct symtab_index_entry **
16429 find_slot (struct mapped_symtab *symtab, const char *name)
16430 {
16431 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16432
16433 index = hash & (symtab->size - 1);
16434 step = ((hash * 17) & (symtab->size - 1)) | 1;
16435
16436 for (;;)
16437 {
16438 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16439 return &symtab->data[index];
16440 index = (index + step) & (symtab->size - 1);
16441 }
16442 }
16443
16444 /* Expand SYMTAB's hash table. */
16445
16446 static void
16447 hash_expand (struct mapped_symtab *symtab)
16448 {
16449 offset_type old_size = symtab->size;
16450 offset_type i;
16451 struct symtab_index_entry **old_entries = symtab->data;
16452
16453 symtab->size *= 2;
16454 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16455
16456 for (i = 0; i < old_size; ++i)
16457 {
16458 if (old_entries[i])
16459 {
16460 struct symtab_index_entry **slot = find_slot (symtab,
16461 old_entries[i]->name);
16462 *slot = old_entries[i];
16463 }
16464 }
16465
16466 xfree (old_entries);
16467 }
16468
16469 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16470 is the index of the CU in which the symbol appears. */
16471
16472 static void
16473 add_index_entry (struct mapped_symtab *symtab, const char *name,
16474 offset_type cu_index)
16475 {
16476 struct symtab_index_entry **slot;
16477
16478 ++symtab->n_elements;
16479 if (4 * symtab->n_elements / 3 >= symtab->size)
16480 hash_expand (symtab);
16481
16482 slot = find_slot (symtab, name);
16483 if (!*slot)
16484 {
16485 *slot = XNEW (struct symtab_index_entry);
16486 (*slot)->name = name;
16487 (*slot)->cu_indices = NULL;
16488 }
16489 /* Don't push an index twice. Due to how we add entries we only
16490 have to check the last one. */
16491 if (VEC_empty (offset_type, (*slot)->cu_indices)
16492 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16493 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16494 }
16495
16496 /* Add a vector of indices to the constant pool. */
16497
16498 static offset_type
16499 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16500 struct symtab_index_entry *entry)
16501 {
16502 void **slot;
16503
16504 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16505 if (!*slot)
16506 {
16507 offset_type len = VEC_length (offset_type, entry->cu_indices);
16508 offset_type val = MAYBE_SWAP (len);
16509 offset_type iter;
16510 int i;
16511
16512 *slot = entry;
16513 entry->index_offset = obstack_object_size (cpool);
16514
16515 obstack_grow (cpool, &val, sizeof (val));
16516 for (i = 0;
16517 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16518 ++i)
16519 {
16520 val = MAYBE_SWAP (iter);
16521 obstack_grow (cpool, &val, sizeof (val));
16522 }
16523 }
16524 else
16525 {
16526 struct symtab_index_entry *old_entry = *slot;
16527 entry->index_offset = old_entry->index_offset;
16528 entry = old_entry;
16529 }
16530 return entry->index_offset;
16531 }
16532
16533 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16534 constant pool entries going into the obstack CPOOL. */
16535
16536 static void
16537 write_hash_table (struct mapped_symtab *symtab,
16538 struct obstack *output, struct obstack *cpool)
16539 {
16540 offset_type i;
16541 htab_t symbol_hash_table;
16542 htab_t str_table;
16543
16544 symbol_hash_table = create_symbol_hash_table ();
16545 str_table = create_strtab ();
16546
16547 /* We add all the index vectors to the constant pool first, to
16548 ensure alignment is ok. */
16549 for (i = 0; i < symtab->size; ++i)
16550 {
16551 if (symtab->data[i])
16552 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16553 }
16554
16555 /* Now write out the hash table. */
16556 for (i = 0; i < symtab->size; ++i)
16557 {
16558 offset_type str_off, vec_off;
16559
16560 if (symtab->data[i])
16561 {
16562 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16563 vec_off = symtab->data[i]->index_offset;
16564 }
16565 else
16566 {
16567 /* While 0 is a valid constant pool index, it is not valid
16568 to have 0 for both offsets. */
16569 str_off = 0;
16570 vec_off = 0;
16571 }
16572
16573 str_off = MAYBE_SWAP (str_off);
16574 vec_off = MAYBE_SWAP (vec_off);
16575
16576 obstack_grow (output, &str_off, sizeof (str_off));
16577 obstack_grow (output, &vec_off, sizeof (vec_off));
16578 }
16579
16580 htab_delete (str_table);
16581 htab_delete (symbol_hash_table);
16582 }
16583
16584 /* Struct to map psymtab to CU index in the index file. */
16585 struct psymtab_cu_index_map
16586 {
16587 struct partial_symtab *psymtab;
16588 unsigned int cu_index;
16589 };
16590
16591 static hashval_t
16592 hash_psymtab_cu_index (const void *item)
16593 {
16594 const struct psymtab_cu_index_map *map = item;
16595
16596 return htab_hash_pointer (map->psymtab);
16597 }
16598
16599 static int
16600 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16601 {
16602 const struct psymtab_cu_index_map *lhs = item_lhs;
16603 const struct psymtab_cu_index_map *rhs = item_rhs;
16604
16605 return lhs->psymtab == rhs->psymtab;
16606 }
16607
16608 /* Helper struct for building the address table. */
16609 struct addrmap_index_data
16610 {
16611 struct objfile *objfile;
16612 struct obstack *addr_obstack;
16613 htab_t cu_index_htab;
16614
16615 /* Non-zero if the previous_* fields are valid.
16616 We can't write an entry until we see the next entry (since it is only then
16617 that we know the end of the entry). */
16618 int previous_valid;
16619 /* Index of the CU in the table of all CUs in the index file. */
16620 unsigned int previous_cu_index;
16621 /* Start address of the CU. */
16622 CORE_ADDR previous_cu_start;
16623 };
16624
16625 /* Write an address entry to OBSTACK. */
16626
16627 static void
16628 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16629 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16630 {
16631 offset_type cu_index_to_write;
16632 char addr[8];
16633 CORE_ADDR baseaddr;
16634
16635 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16636
16637 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16638 obstack_grow (obstack, addr, 8);
16639 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16640 obstack_grow (obstack, addr, 8);
16641 cu_index_to_write = MAYBE_SWAP (cu_index);
16642 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16643 }
16644
16645 /* Worker function for traversing an addrmap to build the address table. */
16646
16647 static int
16648 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16649 {
16650 struct addrmap_index_data *data = datap;
16651 struct partial_symtab *pst = obj;
16652 offset_type cu_index;
16653 void **slot;
16654
16655 if (data->previous_valid)
16656 add_address_entry (data->objfile, data->addr_obstack,
16657 data->previous_cu_start, start_addr,
16658 data->previous_cu_index);
16659
16660 data->previous_cu_start = start_addr;
16661 if (pst != NULL)
16662 {
16663 struct psymtab_cu_index_map find_map, *map;
16664 find_map.psymtab = pst;
16665 map = htab_find (data->cu_index_htab, &find_map);
16666 gdb_assert (map != NULL);
16667 data->previous_cu_index = map->cu_index;
16668 data->previous_valid = 1;
16669 }
16670 else
16671 data->previous_valid = 0;
16672
16673 return 0;
16674 }
16675
16676 /* Write OBJFILE's address map to OBSTACK.
16677 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16678 in the index file. */
16679
16680 static void
16681 write_address_map (struct objfile *objfile, struct obstack *obstack,
16682 htab_t cu_index_htab)
16683 {
16684 struct addrmap_index_data addrmap_index_data;
16685
16686 /* When writing the address table, we have to cope with the fact that
16687 the addrmap iterator only provides the start of a region; we have to
16688 wait until the next invocation to get the start of the next region. */
16689
16690 addrmap_index_data.objfile = objfile;
16691 addrmap_index_data.addr_obstack = obstack;
16692 addrmap_index_data.cu_index_htab = cu_index_htab;
16693 addrmap_index_data.previous_valid = 0;
16694
16695 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16696 &addrmap_index_data);
16697
16698 /* It's highly unlikely the last entry (end address = 0xff...ff)
16699 is valid, but we should still handle it.
16700 The end address is recorded as the start of the next region, but that
16701 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16702 anyway. */
16703 if (addrmap_index_data.previous_valid)
16704 add_address_entry (objfile, obstack,
16705 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16706 addrmap_index_data.previous_cu_index);
16707 }
16708
16709 /* Add a list of partial symbols to SYMTAB. */
16710
16711 static void
16712 write_psymbols (struct mapped_symtab *symtab,
16713 htab_t psyms_seen,
16714 struct partial_symbol **psymp,
16715 int count,
16716 offset_type cu_index,
16717 int is_static)
16718 {
16719 for (; count-- > 0; ++psymp)
16720 {
16721 void **slot, *lookup;
16722
16723 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16724 error (_("Ada is not currently supported by the index"));
16725
16726 /* We only want to add a given psymbol once. However, we also
16727 want to account for whether it is global or static. So, we
16728 may add it twice, using slightly different values. */
16729 if (is_static)
16730 {
16731 uintptr_t val = 1 | (uintptr_t) *psymp;
16732
16733 lookup = (void *) val;
16734 }
16735 else
16736 lookup = *psymp;
16737
16738 /* Only add a given psymbol once. */
16739 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16740 if (!*slot)
16741 {
16742 *slot = lookup;
16743 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16744 }
16745 }
16746 }
16747
16748 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16749 exception if there is an error. */
16750
16751 static void
16752 write_obstack (FILE *file, struct obstack *obstack)
16753 {
16754 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16755 file)
16756 != obstack_object_size (obstack))
16757 error (_("couldn't data write to file"));
16758 }
16759
16760 /* Unlink a file if the argument is not NULL. */
16761
16762 static void
16763 unlink_if_set (void *p)
16764 {
16765 char **filename = p;
16766 if (*filename)
16767 unlink (*filename);
16768 }
16769
16770 /* A helper struct used when iterating over debug_types. */
16771 struct signatured_type_index_data
16772 {
16773 struct objfile *objfile;
16774 struct mapped_symtab *symtab;
16775 struct obstack *types_list;
16776 htab_t psyms_seen;
16777 int cu_index;
16778 };
16779
16780 /* A helper function that writes a single signatured_type to an
16781 obstack. */
16782
16783 static int
16784 write_one_signatured_type (void **slot, void *d)
16785 {
16786 struct signatured_type_index_data *info = d;
16787 struct signatured_type *entry = (struct signatured_type *) *slot;
16788 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16789 struct partial_symtab *psymtab = per_cu->v.psymtab;
16790 gdb_byte val[8];
16791
16792 write_psymbols (info->symtab,
16793 info->psyms_seen,
16794 info->objfile->global_psymbols.list
16795 + psymtab->globals_offset,
16796 psymtab->n_global_syms, info->cu_index,
16797 0);
16798 write_psymbols (info->symtab,
16799 info->psyms_seen,
16800 info->objfile->static_psymbols.list
16801 + psymtab->statics_offset,
16802 psymtab->n_static_syms, info->cu_index,
16803 1);
16804
16805 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16806 obstack_grow (info->types_list, val, 8);
16807 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16808 obstack_grow (info->types_list, val, 8);
16809 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16810 obstack_grow (info->types_list, val, 8);
16811
16812 ++info->cu_index;
16813
16814 return 1;
16815 }
16816
16817 /* Create an index file for OBJFILE in the directory DIR. */
16818
16819 static void
16820 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16821 {
16822 struct cleanup *cleanup;
16823 char *filename, *cleanup_filename;
16824 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16825 struct obstack cu_list, types_cu_list;
16826 int i;
16827 FILE *out_file;
16828 struct mapped_symtab *symtab;
16829 offset_type val, size_of_contents, total_len;
16830 struct stat st;
16831 char buf[8];
16832 htab_t psyms_seen;
16833 htab_t cu_index_htab;
16834 struct psymtab_cu_index_map *psymtab_cu_index_map;
16835
16836 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16837 return;
16838
16839 if (dwarf2_per_objfile->using_index)
16840 error (_("Cannot use an index to create the index"));
16841
16842 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16843 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16844
16845 if (stat (objfile->name, &st) < 0)
16846 perror_with_name (objfile->name);
16847
16848 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16849 INDEX_SUFFIX, (char *) NULL);
16850 cleanup = make_cleanup (xfree, filename);
16851
16852 out_file = fopen (filename, "wb");
16853 if (!out_file)
16854 error (_("Can't open `%s' for writing"), filename);
16855
16856 cleanup_filename = filename;
16857 make_cleanup (unlink_if_set, &cleanup_filename);
16858
16859 symtab = create_mapped_symtab ();
16860 make_cleanup (cleanup_mapped_symtab, symtab);
16861
16862 obstack_init (&addr_obstack);
16863 make_cleanup_obstack_free (&addr_obstack);
16864
16865 obstack_init (&cu_list);
16866 make_cleanup_obstack_free (&cu_list);
16867
16868 obstack_init (&types_cu_list);
16869 make_cleanup_obstack_free (&types_cu_list);
16870
16871 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16872 NULL, xcalloc, xfree);
16873 make_cleanup_htab_delete (psyms_seen);
16874
16875 /* While we're scanning CU's create a table that maps a psymtab pointer
16876 (which is what addrmap records) to its index (which is what is recorded
16877 in the index file). This will later be needed to write the address
16878 table. */
16879 cu_index_htab = htab_create_alloc (100,
16880 hash_psymtab_cu_index,
16881 eq_psymtab_cu_index,
16882 NULL, xcalloc, xfree);
16883 make_cleanup_htab_delete (cu_index_htab);
16884 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16885 xmalloc (sizeof (struct psymtab_cu_index_map)
16886 * dwarf2_per_objfile->n_comp_units);
16887 make_cleanup (xfree, psymtab_cu_index_map);
16888
16889 /* The CU list is already sorted, so we don't need to do additional
16890 work here. Also, the debug_types entries do not appear in
16891 all_comp_units, but only in their own hash table. */
16892 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16893 {
16894 struct dwarf2_per_cu_data *per_cu
16895 = dwarf2_per_objfile->all_comp_units[i];
16896 struct partial_symtab *psymtab = per_cu->v.psymtab;
16897 gdb_byte val[8];
16898 struct psymtab_cu_index_map *map;
16899 void **slot;
16900
16901 write_psymbols (symtab,
16902 psyms_seen,
16903 objfile->global_psymbols.list + psymtab->globals_offset,
16904 psymtab->n_global_syms, i,
16905 0);
16906 write_psymbols (symtab,
16907 psyms_seen,
16908 objfile->static_psymbols.list + psymtab->statics_offset,
16909 psymtab->n_static_syms, i,
16910 1);
16911
16912 map = &psymtab_cu_index_map[i];
16913 map->psymtab = psymtab;
16914 map->cu_index = i;
16915 slot = htab_find_slot (cu_index_htab, map, INSERT);
16916 gdb_assert (slot != NULL);
16917 gdb_assert (*slot == NULL);
16918 *slot = map;
16919
16920 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16921 obstack_grow (&cu_list, val, 8);
16922 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16923 obstack_grow (&cu_list, val, 8);
16924 }
16925
16926 /* Dump the address map. */
16927 write_address_map (objfile, &addr_obstack, cu_index_htab);
16928
16929 /* Write out the .debug_type entries, if any. */
16930 if (dwarf2_per_objfile->signatured_types)
16931 {
16932 struct signatured_type_index_data sig_data;
16933
16934 sig_data.objfile = objfile;
16935 sig_data.symtab = symtab;
16936 sig_data.types_list = &types_cu_list;
16937 sig_data.psyms_seen = psyms_seen;
16938 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16939 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16940 write_one_signatured_type, &sig_data);
16941 }
16942
16943 obstack_init (&constant_pool);
16944 make_cleanup_obstack_free (&constant_pool);
16945 obstack_init (&symtab_obstack);
16946 make_cleanup_obstack_free (&symtab_obstack);
16947 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16948
16949 obstack_init (&contents);
16950 make_cleanup_obstack_free (&contents);
16951 size_of_contents = 6 * sizeof (offset_type);
16952 total_len = size_of_contents;
16953
16954 /* The version number. */
16955 val = MAYBE_SWAP (5);
16956 obstack_grow (&contents, &val, sizeof (val));
16957
16958 /* The offset of the CU list from the start of the file. */
16959 val = MAYBE_SWAP (total_len);
16960 obstack_grow (&contents, &val, sizeof (val));
16961 total_len += obstack_object_size (&cu_list);
16962
16963 /* The offset of the types CU list from the start of the file. */
16964 val = MAYBE_SWAP (total_len);
16965 obstack_grow (&contents, &val, sizeof (val));
16966 total_len += obstack_object_size (&types_cu_list);
16967
16968 /* The offset of the address table from the start of the file. */
16969 val = MAYBE_SWAP (total_len);
16970 obstack_grow (&contents, &val, sizeof (val));
16971 total_len += obstack_object_size (&addr_obstack);
16972
16973 /* The offset of the symbol table from the start of the file. */
16974 val = MAYBE_SWAP (total_len);
16975 obstack_grow (&contents, &val, sizeof (val));
16976 total_len += obstack_object_size (&symtab_obstack);
16977
16978 /* The offset of the constant pool from the start of the file. */
16979 val = MAYBE_SWAP (total_len);
16980 obstack_grow (&contents, &val, sizeof (val));
16981 total_len += obstack_object_size (&constant_pool);
16982
16983 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16984
16985 write_obstack (out_file, &contents);
16986 write_obstack (out_file, &cu_list);
16987 write_obstack (out_file, &types_cu_list);
16988 write_obstack (out_file, &addr_obstack);
16989 write_obstack (out_file, &symtab_obstack);
16990 write_obstack (out_file, &constant_pool);
16991
16992 fclose (out_file);
16993
16994 /* We want to keep the file, so we set cleanup_filename to NULL
16995 here. See unlink_if_set. */
16996 cleanup_filename = NULL;
16997
16998 do_cleanups (cleanup);
16999 }
17000
17001 /* Implementation of the `save gdb-index' command.
17002
17003 Note that the file format used by this command is documented in the
17004 GDB manual. Any changes here must be documented there. */
17005
17006 static void
17007 save_gdb_index_command (char *arg, int from_tty)
17008 {
17009 struct objfile *objfile;
17010
17011 if (!arg || !*arg)
17012 error (_("usage: save gdb-index DIRECTORY"));
17013
17014 ALL_OBJFILES (objfile)
17015 {
17016 struct stat st;
17017
17018 /* If the objfile does not correspond to an actual file, skip it. */
17019 if (stat (objfile->name, &st) < 0)
17020 continue;
17021
17022 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17023 if (dwarf2_per_objfile)
17024 {
17025 volatile struct gdb_exception except;
17026
17027 TRY_CATCH (except, RETURN_MASK_ERROR)
17028 {
17029 write_psymtabs_to_index (objfile, arg);
17030 }
17031 if (except.reason < 0)
17032 exception_fprintf (gdb_stderr, except,
17033 _("Error while writing index for `%s': "),
17034 objfile->name);
17035 }
17036 }
17037 }
17038
17039 \f
17040
17041 int dwarf2_always_disassemble;
17042
17043 static void
17044 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17045 struct cmd_list_element *c, const char *value)
17046 {
17047 fprintf_filtered (file,
17048 _("Whether to always disassemble "
17049 "DWARF expressions is %s.\n"),
17050 value);
17051 }
17052
17053 static void
17054 show_check_physname (struct ui_file *file, int from_tty,
17055 struct cmd_list_element *c, const char *value)
17056 {
17057 fprintf_filtered (file,
17058 _("Whether to check \"physname\" is %s.\n"),
17059 value);
17060 }
17061
17062 void _initialize_dwarf2_read (void);
17063
17064 void
17065 _initialize_dwarf2_read (void)
17066 {
17067 struct cmd_list_element *c;
17068
17069 dwarf2_objfile_data_key
17070 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17071
17072 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17073 Set DWARF 2 specific variables.\n\
17074 Configure DWARF 2 variables such as the cache size"),
17075 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17076 0/*allow-unknown*/, &maintenance_set_cmdlist);
17077
17078 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17079 Show DWARF 2 specific variables\n\
17080 Show DWARF 2 variables such as the cache size"),
17081 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17082 0/*allow-unknown*/, &maintenance_show_cmdlist);
17083
17084 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17085 &dwarf2_max_cache_age, _("\
17086 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17087 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17088 A higher limit means that cached compilation units will be stored\n\
17089 in memory longer, and more total memory will be used. Zero disables\n\
17090 caching, which can slow down startup."),
17091 NULL,
17092 show_dwarf2_max_cache_age,
17093 &set_dwarf2_cmdlist,
17094 &show_dwarf2_cmdlist);
17095
17096 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17097 &dwarf2_always_disassemble, _("\
17098 Set whether `info address' always disassembles DWARF expressions."), _("\
17099 Show whether `info address' always disassembles DWARF expressions."), _("\
17100 When enabled, DWARF expressions are always printed in an assembly-like\n\
17101 syntax. When disabled, expressions will be printed in a more\n\
17102 conversational style, when possible."),
17103 NULL,
17104 show_dwarf2_always_disassemble,
17105 &set_dwarf2_cmdlist,
17106 &show_dwarf2_cmdlist);
17107
17108 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17109 Set debugging of the dwarf2 DIE reader."), _("\
17110 Show debugging of the dwarf2 DIE reader."), _("\
17111 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17112 The value is the maximum depth to print."),
17113 NULL,
17114 NULL,
17115 &setdebuglist, &showdebuglist);
17116
17117 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17118 Set cross-checking of \"physname\" code against demangler."), _("\
17119 Show cross-checking of \"physname\" code against demangler."), _("\
17120 When enabled, GDB's internal \"physname\" code is checked against\n\
17121 the demangler."),
17122 NULL, show_check_physname,
17123 &setdebuglist, &showdebuglist);
17124
17125 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17126 _("\
17127 Save a gdb-index file.\n\
17128 Usage: save gdb-index DIRECTORY"),
17129 &save_cmdlist);
17130 set_cmd_completer (c, filename_completer);
17131 }
This page took 0.854792 seconds and 4 git commands to generate.