71cae8966033ee9f169003b90d6fe32252fe6521
[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 /* A list 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_comp_units;
177
178 /* The .debug_types-related CUs. */
179 struct dwarf2_per_cu_data **type_comp_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 *, bfd *,
990 struct dwarf2_cu *, struct partial_symtab *);
991
992 static void dwarf2_start_subfile (char *, const char *, const char *);
993
994 static struct symbol *new_symbol (struct die_info *, struct type *,
995 struct dwarf2_cu *);
996
997 static struct symbol *new_symbol_full (struct die_info *, struct type *,
998 struct dwarf2_cu *, struct symbol *);
999
1000 static void dwarf2_const_value (struct attribute *, struct symbol *,
1001 struct dwarf2_cu *);
1002
1003 static void dwarf2_const_value_attr (struct attribute *attr,
1004 struct type *type,
1005 const char *name,
1006 struct obstack *obstack,
1007 struct dwarf2_cu *cu, long *value,
1008 gdb_byte **bytes,
1009 struct dwarf2_locexpr_baton **baton);
1010
1011 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1012
1013 static int need_gnat_info (struct dwarf2_cu *);
1014
1015 static struct type *die_descriptive_type (struct die_info *,
1016 struct dwarf2_cu *);
1017
1018 static void set_descriptive_type (struct type *, struct die_info *,
1019 struct dwarf2_cu *);
1020
1021 static struct type *die_containing_type (struct die_info *,
1022 struct dwarf2_cu *);
1023
1024 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1025 struct dwarf2_cu *);
1026
1027 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1028
1029 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1030
1031 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1032
1033 static char *typename_concat (struct obstack *obs, const char *prefix,
1034 const char *suffix, int physname,
1035 struct dwarf2_cu *cu);
1036
1037 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1038
1039 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1040
1041 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1042
1043 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1044
1045 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1046
1047 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1048 struct dwarf2_cu *, struct partial_symtab *);
1049
1050 static int dwarf2_get_pc_bounds (struct die_info *,
1051 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1052 struct partial_symtab *);
1053
1054 static void get_scope_pc_bounds (struct die_info *,
1055 CORE_ADDR *, CORE_ADDR *,
1056 struct dwarf2_cu *);
1057
1058 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1059 CORE_ADDR, struct dwarf2_cu *);
1060
1061 static void dwarf2_add_field (struct field_info *, struct die_info *,
1062 struct dwarf2_cu *);
1063
1064 static void dwarf2_attach_fields_to_type (struct field_info *,
1065 struct type *, struct dwarf2_cu *);
1066
1067 static void dwarf2_add_member_fn (struct field_info *,
1068 struct die_info *, struct type *,
1069 struct dwarf2_cu *);
1070
1071 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1072 struct type *,
1073 struct dwarf2_cu *);
1074
1075 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1076
1077 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1078
1079 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1080
1081 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1082
1083 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1084
1085 static struct type *read_module_type (struct die_info *die,
1086 struct dwarf2_cu *cu);
1087
1088 static const char *namespace_name (struct die_info *die,
1089 int *is_anonymous, struct dwarf2_cu *);
1090
1091 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1092
1093 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1094
1095 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1096 struct dwarf2_cu *);
1097
1098 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1099
1100 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1101 gdb_byte *info_ptr,
1102 gdb_byte **new_info_ptr,
1103 struct die_info *parent);
1104
1105 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1106 gdb_byte *info_ptr,
1107 gdb_byte **new_info_ptr,
1108 struct die_info *parent);
1109
1110 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1111 gdb_byte *info_ptr,
1112 gdb_byte **new_info_ptr,
1113 struct die_info *parent);
1114
1115 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1116 struct die_info **, gdb_byte *,
1117 int *);
1118
1119 static void process_die (struct die_info *, struct dwarf2_cu *);
1120
1121 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1122 struct obstack *);
1123
1124 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1125
1126 static const char *dwarf2_full_name (char *name,
1127 struct die_info *die,
1128 struct dwarf2_cu *cu);
1129
1130 static struct die_info *dwarf2_extension (struct die_info *die,
1131 struct dwarf2_cu **);
1132
1133 static char *dwarf_tag_name (unsigned int);
1134
1135 static char *dwarf_attr_name (unsigned int);
1136
1137 static char *dwarf_form_name (unsigned int);
1138
1139 static char *dwarf_bool_name (unsigned int);
1140
1141 static char *dwarf_type_encoding_name (unsigned int);
1142
1143 #if 0
1144 static char *dwarf_cfi_name (unsigned int);
1145 #endif
1146
1147 static struct die_info *sibling_die (struct die_info *);
1148
1149 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1150
1151 static void dump_die_for_error (struct die_info *);
1152
1153 static void dump_die_1 (struct ui_file *, int level, int max_level,
1154 struct die_info *);
1155
1156 /*static*/ void dump_die (struct die_info *, int max_level);
1157
1158 static void store_in_ref_table (struct die_info *,
1159 struct dwarf2_cu *);
1160
1161 static int is_ref_attr (struct attribute *);
1162
1163 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1164
1165 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1166
1167 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1168 struct attribute *,
1169 struct dwarf2_cu **);
1170
1171 static struct die_info *follow_die_ref (struct die_info *,
1172 struct attribute *,
1173 struct dwarf2_cu **);
1174
1175 static struct die_info *follow_die_sig (struct die_info *,
1176 struct attribute *,
1177 struct dwarf2_cu **);
1178
1179 static struct signatured_type *lookup_signatured_type_at_offset
1180 (struct objfile *objfile,
1181 struct dwarf2_section_info *section,
1182 unsigned int offset);
1183
1184 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1185
1186 static void read_signatured_type (struct signatured_type *type_sig);
1187
1188 /* memory allocation interface */
1189
1190 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1191
1192 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1193
1194 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1195
1196 static void dwarf_decode_macros (struct line_header *, unsigned int,
1197 char *, bfd *, struct dwarf2_cu *,
1198 struct dwarf2_section_info *,
1199 int);
1200
1201 static int attr_form_is_block (struct attribute *);
1202
1203 static int attr_form_is_section_offset (struct attribute *);
1204
1205 static int attr_form_is_constant (struct attribute *);
1206
1207 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1208 struct dwarf2_loclist_baton *baton,
1209 struct attribute *attr);
1210
1211 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1212 struct symbol *sym,
1213 struct dwarf2_cu *cu);
1214
1215 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1216 struct abbrev_info *abbrev,
1217 struct dwarf2_cu *cu);
1218
1219 static void free_stack_comp_unit (void *);
1220
1221 static hashval_t partial_die_hash (const void *item);
1222
1223 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1224
1225 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1226 (unsigned int offset, struct objfile *objfile);
1227
1228 static void init_one_comp_unit (struct dwarf2_cu *cu,
1229 struct dwarf2_per_cu_data *per_cu);
1230
1231 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1232 struct die_info *comp_unit_die);
1233
1234 static void free_heap_comp_unit (void *);
1235
1236 static void free_cached_comp_units (void *);
1237
1238 static void age_cached_comp_units (void);
1239
1240 static void free_one_cached_comp_unit (void *);
1241
1242 static struct type *set_die_type (struct die_info *, struct type *,
1243 struct dwarf2_cu *);
1244
1245 static void create_all_comp_units (struct objfile *);
1246
1247 static int create_debug_types_hash_table (struct objfile *objfile);
1248
1249 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1250
1251 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1252
1253 static void dwarf2_add_dependence (struct dwarf2_cu *,
1254 struct dwarf2_per_cu_data *);
1255
1256 static void dwarf2_mark (struct dwarf2_cu *);
1257
1258 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1259
1260 static struct type *get_die_type_at_offset (unsigned int,
1261 struct dwarf2_per_cu_data *per_cu);
1262
1263 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1264
1265 static void dwarf2_release_queue (void *dummy);
1266
1267 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1268
1269 static void process_queue (void);
1270
1271 static void find_file_and_directory (struct die_info *die,
1272 struct dwarf2_cu *cu,
1273 char **name, char **comp_dir);
1274
1275 static char *file_full_name (int file, struct line_header *lh,
1276 const char *comp_dir);
1277
1278 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1279 gdb_byte *info_ptr,
1280 gdb_byte *buffer,
1281 unsigned int buffer_size,
1282 bfd *abfd,
1283 int is_debug_types_section);
1284
1285 static void init_cu_die_reader (struct die_reader_specs *reader,
1286 struct dwarf2_cu *cu);
1287
1288 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1289
1290 #if WORDS_BIGENDIAN
1291
1292 /* Convert VALUE between big- and little-endian. */
1293 static offset_type
1294 byte_swap (offset_type value)
1295 {
1296 offset_type result;
1297
1298 result = (value & 0xff) << 24;
1299 result |= (value & 0xff00) << 8;
1300 result |= (value & 0xff0000) >> 8;
1301 result |= (value & 0xff000000) >> 24;
1302 return result;
1303 }
1304
1305 #define MAYBE_SWAP(V) byte_swap (V)
1306
1307 #else
1308 #define MAYBE_SWAP(V) (V)
1309 #endif /* WORDS_BIGENDIAN */
1310
1311 /* The suffix for an index file. */
1312 #define INDEX_SUFFIX ".gdb-index"
1313
1314 static const char *dwarf2_physname (char *name, struct die_info *die,
1315 struct dwarf2_cu *cu);
1316
1317 /* Try to locate the sections we need for DWARF 2 debugging
1318 information and return true if we have enough to do something.
1319 NAMES points to the dwarf2 section names, or is NULL if the standard
1320 ELF names are used. */
1321
1322 int
1323 dwarf2_has_info (struct objfile *objfile,
1324 const struct dwarf2_debug_sections *names)
1325 {
1326 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1327 if (!dwarf2_per_objfile)
1328 {
1329 /* Initialize per-objfile state. */
1330 struct dwarf2_per_objfile *data
1331 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1332
1333 memset (data, 0, sizeof (*data));
1334 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1335 dwarf2_per_objfile = data;
1336
1337 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1338 (void *) names);
1339 dwarf2_per_objfile->objfile = objfile;
1340 }
1341 return (dwarf2_per_objfile->info.asection != NULL
1342 && dwarf2_per_objfile->abbrev.asection != NULL);
1343 }
1344
1345 /* When loading sections, we look either for uncompressed section or for
1346 compressed section names. */
1347
1348 static int
1349 section_is_p (const char *section_name,
1350 const struct dwarf2_section_names *names)
1351 {
1352 if (names->normal != NULL
1353 && strcmp (section_name, names->normal) == 0)
1354 return 1;
1355 if (names->compressed != NULL
1356 && strcmp (section_name, names->compressed) == 0)
1357 return 1;
1358 return 0;
1359 }
1360
1361 /* This function is mapped across the sections and remembers the
1362 offset and size of each of the debugging sections we are interested
1363 in. */
1364
1365 static void
1366 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1367 {
1368 const struct dwarf2_debug_sections *names;
1369
1370 if (vnames == NULL)
1371 names = &dwarf2_elf_names;
1372 else
1373 names = (const struct dwarf2_debug_sections *) vnames;
1374
1375 if (section_is_p (sectp->name, &names->info))
1376 {
1377 dwarf2_per_objfile->info.asection = sectp;
1378 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1379 }
1380 else if (section_is_p (sectp->name, &names->abbrev))
1381 {
1382 dwarf2_per_objfile->abbrev.asection = sectp;
1383 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1384 }
1385 else if (section_is_p (sectp->name, &names->line))
1386 {
1387 dwarf2_per_objfile->line.asection = sectp;
1388 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1389 }
1390 else if (section_is_p (sectp->name, &names->loc))
1391 {
1392 dwarf2_per_objfile->loc.asection = sectp;
1393 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1394 }
1395 else if (section_is_p (sectp->name, &names->macinfo))
1396 {
1397 dwarf2_per_objfile->macinfo.asection = sectp;
1398 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1399 }
1400 else if (section_is_p (sectp->name, &names->macro))
1401 {
1402 dwarf2_per_objfile->macro.asection = sectp;
1403 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1404 }
1405 else if (section_is_p (sectp->name, &names->str))
1406 {
1407 dwarf2_per_objfile->str.asection = sectp;
1408 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1409 }
1410 else if (section_is_p (sectp->name, &names->frame))
1411 {
1412 dwarf2_per_objfile->frame.asection = sectp;
1413 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1414 }
1415 else if (section_is_p (sectp->name, &names->eh_frame))
1416 {
1417 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1418
1419 if (aflag & SEC_HAS_CONTENTS)
1420 {
1421 dwarf2_per_objfile->eh_frame.asection = sectp;
1422 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1423 }
1424 }
1425 else if (section_is_p (sectp->name, &names->ranges))
1426 {
1427 dwarf2_per_objfile->ranges.asection = sectp;
1428 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1429 }
1430 else if (section_is_p (sectp->name, &names->types))
1431 {
1432 struct dwarf2_section_info type_section;
1433
1434 memset (&type_section, 0, sizeof (type_section));
1435 type_section.asection = sectp;
1436 type_section.size = bfd_get_section_size (sectp);
1437
1438 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1439 &type_section);
1440 }
1441 else if (section_is_p (sectp->name, &names->gdb_index))
1442 {
1443 dwarf2_per_objfile->gdb_index.asection = sectp;
1444 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1445 }
1446
1447 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1448 && bfd_section_vma (abfd, sectp) == 0)
1449 dwarf2_per_objfile->has_section_at_zero = 1;
1450 }
1451
1452 /* Decompress a section that was compressed using zlib. Store the
1453 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1454
1455 static void
1456 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1457 gdb_byte **outbuf, bfd_size_type *outsize)
1458 {
1459 bfd *abfd = objfile->obfd;
1460 #ifndef HAVE_ZLIB_H
1461 error (_("Support for zlib-compressed DWARF data (from '%s') "
1462 "is disabled in this copy of GDB"),
1463 bfd_get_filename (abfd));
1464 #else
1465 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1466 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1467 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1468 bfd_size_type uncompressed_size;
1469 gdb_byte *uncompressed_buffer;
1470 z_stream strm;
1471 int rc;
1472 int header_size = 12;
1473
1474 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1475 || bfd_bread (compressed_buffer,
1476 compressed_size, abfd) != compressed_size)
1477 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1478 bfd_get_filename (abfd));
1479
1480 /* Read the zlib header. In this case, it should be "ZLIB" followed
1481 by the uncompressed section size, 8 bytes in big-endian order. */
1482 if (compressed_size < header_size
1483 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1484 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1485 bfd_get_filename (abfd));
1486 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1493 uncompressed_size += compressed_buffer[11];
1494
1495 /* It is possible the section consists of several compressed
1496 buffers concatenated together, so we uncompress in a loop. */
1497 strm.zalloc = NULL;
1498 strm.zfree = NULL;
1499 strm.opaque = NULL;
1500 strm.avail_in = compressed_size - header_size;
1501 strm.next_in = (Bytef*) compressed_buffer + header_size;
1502 strm.avail_out = uncompressed_size;
1503 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1504 uncompressed_size);
1505 rc = inflateInit (&strm);
1506 while (strm.avail_in > 0)
1507 {
1508 if (rc != Z_OK)
1509 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1510 bfd_get_filename (abfd), rc);
1511 strm.next_out = ((Bytef*) uncompressed_buffer
1512 + (uncompressed_size - strm.avail_out));
1513 rc = inflate (&strm, Z_FINISH);
1514 if (rc != Z_STREAM_END)
1515 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1516 bfd_get_filename (abfd), rc);
1517 rc = inflateReset (&strm);
1518 }
1519 rc = inflateEnd (&strm);
1520 if (rc != Z_OK
1521 || strm.avail_out != 0)
1522 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1523 bfd_get_filename (abfd), rc);
1524
1525 do_cleanups (cleanup);
1526 *outbuf = uncompressed_buffer;
1527 *outsize = uncompressed_size;
1528 #endif
1529 }
1530
1531 /* A helper function that decides whether a section is empty. */
1532
1533 static int
1534 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1535 {
1536 return info->asection == NULL || info->size == 0;
1537 }
1538
1539 /* Read the contents of the section INFO from object file specified by
1540 OBJFILE, store info about the section into INFO.
1541 If the section is compressed, uncompress it before returning. */
1542
1543 static void
1544 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1545 {
1546 bfd *abfd = objfile->obfd;
1547 asection *sectp = info->asection;
1548 gdb_byte *buf, *retbuf;
1549 unsigned char header[4];
1550
1551 if (info->readin)
1552 return;
1553 info->buffer = NULL;
1554 info->map_addr = NULL;
1555 info->readin = 1;
1556
1557 if (dwarf2_section_empty_p (info))
1558 return;
1559
1560 /* Check if the file has a 4-byte header indicating compression. */
1561 if (info->size > sizeof (header)
1562 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1563 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1564 {
1565 /* Upon decompression, update the buffer and its size. */
1566 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1567 {
1568 zlib_decompress_section (objfile, sectp, &info->buffer,
1569 &info->size);
1570 return;
1571 }
1572 }
1573
1574 #ifdef HAVE_MMAP
1575 if (pagesize == 0)
1576 pagesize = getpagesize ();
1577
1578 /* Only try to mmap sections which are large enough: we don't want to
1579 waste space due to fragmentation. Also, only try mmap for sections
1580 without relocations. */
1581
1582 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1583 {
1584 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1585 MAP_PRIVATE, sectp->filepos,
1586 &info->map_addr, &info->map_len);
1587
1588 if ((caddr_t)info->buffer != MAP_FAILED)
1589 {
1590 #if HAVE_POSIX_MADVISE
1591 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1592 #endif
1593 return;
1594 }
1595 }
1596 #endif
1597
1598 /* If we get here, we are a normal, not-compressed section. */
1599 info->buffer = buf
1600 = obstack_alloc (&objfile->objfile_obstack, info->size);
1601
1602 /* When debugging .o files, we may need to apply relocations; see
1603 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1604 We never compress sections in .o files, so we only need to
1605 try this when the section is not compressed. */
1606 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1607 if (retbuf != NULL)
1608 {
1609 info->buffer = retbuf;
1610 return;
1611 }
1612
1613 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1614 || bfd_bread (buf, info->size, abfd) != info->size)
1615 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1616 bfd_get_filename (abfd));
1617 }
1618
1619 /* A helper function that returns the size of a section in a safe way.
1620 If you are positive that the section has been read before using the
1621 size, then it is safe to refer to the dwarf2_section_info object's
1622 "size" field directly. In other cases, you must call this
1623 function, because for compressed sections the size field is not set
1624 correctly until the section has been read. */
1625
1626 static bfd_size_type
1627 dwarf2_section_size (struct objfile *objfile,
1628 struct dwarf2_section_info *info)
1629 {
1630 if (!info->readin)
1631 dwarf2_read_section (objfile, info);
1632 return info->size;
1633 }
1634
1635 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1636 SECTION_NAME. */
1637
1638 void
1639 dwarf2_get_section_info (struct objfile *objfile,
1640 enum dwarf2_section_enum sect,
1641 asection **sectp, gdb_byte **bufp,
1642 bfd_size_type *sizep)
1643 {
1644 struct dwarf2_per_objfile *data
1645 = objfile_data (objfile, dwarf2_objfile_data_key);
1646 struct dwarf2_section_info *info;
1647
1648 /* We may see an objfile without any DWARF, in which case we just
1649 return nothing. */
1650 if (data == NULL)
1651 {
1652 *sectp = NULL;
1653 *bufp = NULL;
1654 *sizep = 0;
1655 return;
1656 }
1657 switch (sect)
1658 {
1659 case DWARF2_DEBUG_FRAME:
1660 info = &data->frame;
1661 break;
1662 case DWARF2_EH_FRAME:
1663 info = &data->eh_frame;
1664 break;
1665 default:
1666 gdb_assert_not_reached ("unexpected section");
1667 }
1668
1669 dwarf2_read_section (objfile, info);
1670
1671 *sectp = info->asection;
1672 *bufp = info->buffer;
1673 *sizep = info->size;
1674 }
1675
1676 \f
1677 /* DWARF quick_symbols_functions support. */
1678
1679 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1680 unique line tables, so we maintain a separate table of all .debug_line
1681 derived entries to support the sharing.
1682 All the quick functions need is the list of file names. We discard the
1683 line_header when we're done and don't need to record it here. */
1684 struct quick_file_names
1685 {
1686 /* The offset in .debug_line of the line table. We hash on this. */
1687 unsigned int offset;
1688
1689 /* The number of entries in file_names, real_names. */
1690 unsigned int num_file_names;
1691
1692 /* The file names from the line table, after being run through
1693 file_full_name. */
1694 const char **file_names;
1695
1696 /* The file names from the line table after being run through
1697 gdb_realpath. These are computed lazily. */
1698 const char **real_names;
1699 };
1700
1701 /* When using the index (and thus not using psymtabs), each CU has an
1702 object of this type. This is used to hold information needed by
1703 the various "quick" methods. */
1704 struct dwarf2_per_cu_quick_data
1705 {
1706 /* The file table. This can be NULL if there was no file table
1707 or it's currently not read in.
1708 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1709 struct quick_file_names *file_names;
1710
1711 /* The corresponding symbol table. This is NULL if symbols for this
1712 CU have not yet been read. */
1713 struct symtab *symtab;
1714
1715 /* A temporary mark bit used when iterating over all CUs in
1716 expand_symtabs_matching. */
1717 unsigned int mark : 1;
1718
1719 /* True if we've tried to read the file table and found there isn't one.
1720 There will be no point in trying to read it again next time. */
1721 unsigned int no_file_data : 1;
1722 };
1723
1724 /* Hash function for a quick_file_names. */
1725
1726 static hashval_t
1727 hash_file_name_entry (const void *e)
1728 {
1729 const struct quick_file_names *file_data = e;
1730
1731 return file_data->offset;
1732 }
1733
1734 /* Equality function for a quick_file_names. */
1735
1736 static int
1737 eq_file_name_entry (const void *a, const void *b)
1738 {
1739 const struct quick_file_names *ea = a;
1740 const struct quick_file_names *eb = b;
1741
1742 return ea->offset == eb->offset;
1743 }
1744
1745 /* Delete function for a quick_file_names. */
1746
1747 static void
1748 delete_file_name_entry (void *e)
1749 {
1750 struct quick_file_names *file_data = e;
1751 int i;
1752
1753 for (i = 0; i < file_data->num_file_names; ++i)
1754 {
1755 xfree ((void*) file_data->file_names[i]);
1756 if (file_data->real_names)
1757 xfree ((void*) file_data->real_names[i]);
1758 }
1759
1760 /* The space for the struct itself lives on objfile_obstack,
1761 so we don't free it here. */
1762 }
1763
1764 /* Create a quick_file_names hash table. */
1765
1766 static htab_t
1767 create_quick_file_names_table (unsigned int nr_initial_entries)
1768 {
1769 return htab_create_alloc (nr_initial_entries,
1770 hash_file_name_entry, eq_file_name_entry,
1771 delete_file_name_entry, xcalloc, xfree);
1772 }
1773
1774 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1775 have to be created afterwards. You should call age_cached_comp_units after
1776 processing PER_CU->CU. dw2_setup must have been already called. */
1777
1778 static void
1779 load_cu (struct dwarf2_per_cu_data *per_cu)
1780 {
1781 if (per_cu->debug_types_section)
1782 load_full_type_unit (per_cu);
1783 else
1784 load_full_comp_unit (per_cu);
1785
1786 gdb_assert (per_cu->cu != NULL);
1787
1788 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1789 }
1790
1791 /* Read in the symbols for PER_CU. */
1792
1793 static void
1794 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1795 {
1796 struct cleanup *back_to;
1797
1798 back_to = make_cleanup (dwarf2_release_queue, NULL);
1799
1800 queue_comp_unit (per_cu);
1801
1802 load_cu (per_cu);
1803
1804 process_queue ();
1805
1806 /* Age the cache, releasing compilation units that have not
1807 been used recently. */
1808 age_cached_comp_units ();
1809
1810 do_cleanups (back_to);
1811 }
1812
1813 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1814 the objfile from which this CU came. Returns the resulting symbol
1815 table. */
1816
1817 static struct symtab *
1818 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1819 {
1820 if (!per_cu->v.quick->symtab)
1821 {
1822 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1823 increment_reading_symtab ();
1824 dw2_do_instantiate_symtab (per_cu);
1825 do_cleanups (back_to);
1826 }
1827 return per_cu->v.quick->symtab;
1828 }
1829
1830 /* Return the CU given its index. */
1831
1832 static struct dwarf2_per_cu_data *
1833 dw2_get_cu (int index)
1834 {
1835 if (index >= dwarf2_per_objfile->n_comp_units)
1836 {
1837 index -= dwarf2_per_objfile->n_comp_units;
1838 return dwarf2_per_objfile->type_comp_units[index];
1839 }
1840 return dwarf2_per_objfile->all_comp_units[index];
1841 }
1842
1843 /* A helper function that knows how to read a 64-bit value in a way
1844 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1845 otherwise. */
1846
1847 static int
1848 extract_cu_value (const char *bytes, ULONGEST *result)
1849 {
1850 if (sizeof (ULONGEST) < 8)
1851 {
1852 int i;
1853
1854 /* Ignore the upper 4 bytes if they are all zero. */
1855 for (i = 0; i < 4; ++i)
1856 if (bytes[i + 4] != 0)
1857 return 0;
1858
1859 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1860 }
1861 else
1862 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1863 return 1;
1864 }
1865
1866 /* Read the CU list from the mapped index, and use it to create all
1867 the CU objects for this objfile. Return 0 if something went wrong,
1868 1 if everything went ok. */
1869
1870 static int
1871 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1872 offset_type cu_list_elements)
1873 {
1874 offset_type i;
1875
1876 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1877 dwarf2_per_objfile->all_comp_units
1878 = obstack_alloc (&objfile->objfile_obstack,
1879 dwarf2_per_objfile->n_comp_units
1880 * sizeof (struct dwarf2_per_cu_data *));
1881
1882 for (i = 0; i < cu_list_elements; i += 2)
1883 {
1884 struct dwarf2_per_cu_data *the_cu;
1885 ULONGEST offset, length;
1886
1887 if (!extract_cu_value (cu_list, &offset)
1888 || !extract_cu_value (cu_list + 8, &length))
1889 return 0;
1890 cu_list += 2 * 8;
1891
1892 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1893 struct dwarf2_per_cu_data);
1894 the_cu->offset = offset;
1895 the_cu->length = length;
1896 the_cu->objfile = objfile;
1897 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1898 struct dwarf2_per_cu_quick_data);
1899 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1900 }
1901
1902 return 1;
1903 }
1904
1905 /* Create the signatured type hash table from the index. */
1906
1907 static int
1908 create_signatured_type_table_from_index (struct objfile *objfile,
1909 struct dwarf2_section_info *section,
1910 const gdb_byte *bytes,
1911 offset_type elements)
1912 {
1913 offset_type i;
1914 htab_t sig_types_hash;
1915
1916 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1917 dwarf2_per_objfile->type_comp_units
1918 = obstack_alloc (&objfile->objfile_obstack,
1919 dwarf2_per_objfile->n_type_comp_units
1920 * sizeof (struct dwarf2_per_cu_data *));
1921
1922 sig_types_hash = allocate_signatured_type_table (objfile);
1923
1924 for (i = 0; i < elements; i += 3)
1925 {
1926 struct signatured_type *type_sig;
1927 ULONGEST offset, type_offset, signature;
1928 void **slot;
1929
1930 if (!extract_cu_value (bytes, &offset)
1931 || !extract_cu_value (bytes + 8, &type_offset))
1932 return 0;
1933 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1934 bytes += 3 * 8;
1935
1936 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1937 struct signatured_type);
1938 type_sig->signature = signature;
1939 type_sig->type_offset = type_offset;
1940 type_sig->per_cu.debug_types_section = section;
1941 type_sig->per_cu.offset = offset;
1942 type_sig->per_cu.objfile = objfile;
1943 type_sig->per_cu.v.quick
1944 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1945 struct dwarf2_per_cu_quick_data);
1946
1947 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1948 *slot = type_sig;
1949
1950 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1951 }
1952
1953 dwarf2_per_objfile->signatured_types = sig_types_hash;
1954
1955 return 1;
1956 }
1957
1958 /* Read the address map data from the mapped index, and use it to
1959 populate the objfile's psymtabs_addrmap. */
1960
1961 static void
1962 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1963 {
1964 const gdb_byte *iter, *end;
1965 struct obstack temp_obstack;
1966 struct addrmap *mutable_map;
1967 struct cleanup *cleanup;
1968 CORE_ADDR baseaddr;
1969
1970 obstack_init (&temp_obstack);
1971 cleanup = make_cleanup_obstack_free (&temp_obstack);
1972 mutable_map = addrmap_create_mutable (&temp_obstack);
1973
1974 iter = index->address_table;
1975 end = iter + index->address_table_size;
1976
1977 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1978
1979 while (iter < end)
1980 {
1981 ULONGEST hi, lo, cu_index;
1982 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1983 iter += 8;
1984 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985 iter += 8;
1986 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1987 iter += 4;
1988
1989 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1990 dw2_get_cu (cu_index));
1991 }
1992
1993 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1994 &objfile->objfile_obstack);
1995 do_cleanups (cleanup);
1996 }
1997
1998 /* The hash function for strings in the mapped index. This is the same as
1999 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2000 implementation. This is necessary because the hash function is tied to the
2001 format of the mapped index file. The hash values do not have to match with
2002 SYMBOL_HASH_NEXT.
2003
2004 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2005
2006 static hashval_t
2007 mapped_index_string_hash (int index_version, const void *p)
2008 {
2009 const unsigned char *str = (const unsigned char *) p;
2010 hashval_t r = 0;
2011 unsigned char c;
2012
2013 while ((c = *str++) != 0)
2014 {
2015 if (index_version >= 5)
2016 c = tolower (c);
2017 r = r * 67 + c - 113;
2018 }
2019
2020 return r;
2021 }
2022
2023 /* Find a slot in the mapped index INDEX for the object named NAME.
2024 If NAME is found, set *VEC_OUT to point to the CU vector in the
2025 constant pool and return 1. If NAME cannot be found, return 0. */
2026
2027 static int
2028 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2029 offset_type **vec_out)
2030 {
2031 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2032 offset_type hash;
2033 offset_type slot, step;
2034 int (*cmp) (const char *, const char *);
2035
2036 if (current_language->la_language == language_cplus
2037 || current_language->la_language == language_java
2038 || current_language->la_language == language_fortran)
2039 {
2040 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2041 not contain any. */
2042 const char *paren = strchr (name, '(');
2043
2044 if (paren)
2045 {
2046 char *dup;
2047
2048 dup = xmalloc (paren - name + 1);
2049 memcpy (dup, name, paren - name);
2050 dup[paren - name] = 0;
2051
2052 make_cleanup (xfree, dup);
2053 name = dup;
2054 }
2055 }
2056
2057 /* Index version 4 did not support case insensitive searches. But the
2058 indexes for case insensitive languages are built in lowercase, therefore
2059 simulate our NAME being searched is also lowercased. */
2060 hash = mapped_index_string_hash ((index->version == 4
2061 && case_sensitivity == case_sensitive_off
2062 ? 5 : index->version),
2063 name);
2064
2065 slot = hash & (index->symbol_table_slots - 1);
2066 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2067 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2068
2069 for (;;)
2070 {
2071 /* Convert a slot number to an offset into the table. */
2072 offset_type i = 2 * slot;
2073 const char *str;
2074 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2075 {
2076 do_cleanups (back_to);
2077 return 0;
2078 }
2079
2080 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2081 if (!cmp (name, str))
2082 {
2083 *vec_out = (offset_type *) (index->constant_pool
2084 + MAYBE_SWAP (index->symbol_table[i + 1]));
2085 do_cleanups (back_to);
2086 return 1;
2087 }
2088
2089 slot = (slot + step) & (index->symbol_table_slots - 1);
2090 }
2091 }
2092
2093 /* Read the index file. If everything went ok, initialize the "quick"
2094 elements of all the CUs and return 1. Otherwise, return 0. */
2095
2096 static int
2097 dwarf2_read_index (struct objfile *objfile)
2098 {
2099 char *addr;
2100 struct mapped_index *map;
2101 offset_type *metadata;
2102 const gdb_byte *cu_list;
2103 const gdb_byte *types_list = NULL;
2104 offset_type version, cu_list_elements;
2105 offset_type types_list_elements = 0;
2106 int i;
2107
2108 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2109 return 0;
2110
2111 /* Older elfutils strip versions could keep the section in the main
2112 executable while splitting it for the separate debug info file. */
2113 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2114 & SEC_HAS_CONTENTS) == 0)
2115 return 0;
2116
2117 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2118
2119 addr = dwarf2_per_objfile->gdb_index.buffer;
2120 /* Version check. */
2121 version = MAYBE_SWAP (*(offset_type *) addr);
2122 /* Versions earlier than 3 emitted every copy of a psymbol. This
2123 causes the index to behave very poorly for certain requests. Version 3
2124 contained incomplete addrmap. So, it seems better to just ignore such
2125 indices. Index version 4 uses a different hash function than index
2126 version 5 and later. */
2127 if (version < 4)
2128 return 0;
2129 /* Indexes with higher version than the one supported by GDB may be no
2130 longer backward compatible. */
2131 if (version > 5)
2132 return 0;
2133
2134 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2135 map->version = version;
2136 map->total_size = dwarf2_per_objfile->gdb_index.size;
2137
2138 metadata = (offset_type *) (addr + sizeof (offset_type));
2139
2140 i = 0;
2141 cu_list = addr + MAYBE_SWAP (metadata[i]);
2142 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2143 / 8);
2144 ++i;
2145
2146 types_list = addr + MAYBE_SWAP (metadata[i]);
2147 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2148 - MAYBE_SWAP (metadata[i]))
2149 / 8);
2150 ++i;
2151
2152 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2153 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2154 - MAYBE_SWAP (metadata[i]));
2155 ++i;
2156
2157 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2158 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2159 - MAYBE_SWAP (metadata[i]))
2160 / (2 * sizeof (offset_type)));
2161 ++i;
2162
2163 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2164
2165 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2166 return 0;
2167
2168 if (types_list_elements)
2169 {
2170 struct dwarf2_section_info *section;
2171
2172 /* We can only handle a single .debug_types when we have an
2173 index. */
2174 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2175 return 0;
2176
2177 section = VEC_index (dwarf2_section_info_def,
2178 dwarf2_per_objfile->types, 0);
2179
2180 if (!create_signatured_type_table_from_index (objfile, section,
2181 types_list,
2182 types_list_elements))
2183 return 0;
2184 }
2185
2186 create_addrmap_from_index (objfile, map);
2187
2188 dwarf2_per_objfile->index_table = map;
2189 dwarf2_per_objfile->using_index = 1;
2190 dwarf2_per_objfile->quick_file_names_table =
2191 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2192
2193 return 1;
2194 }
2195
2196 /* A helper for the "quick" functions which sets the global
2197 dwarf2_per_objfile according to OBJFILE. */
2198
2199 static void
2200 dw2_setup (struct objfile *objfile)
2201 {
2202 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2203 gdb_assert (dwarf2_per_objfile);
2204 }
2205
2206 /* A helper for the "quick" functions which attempts to read the line
2207 table for THIS_CU. */
2208
2209 static struct quick_file_names *
2210 dw2_get_file_names (struct objfile *objfile,
2211 struct dwarf2_per_cu_data *this_cu)
2212 {
2213 bfd *abfd = objfile->obfd;
2214 struct line_header *lh;
2215 struct attribute *attr;
2216 struct cleanup *cleanups;
2217 struct die_info *comp_unit_die;
2218 struct dwarf2_section_info* sec;
2219 gdb_byte *info_ptr, *buffer;
2220 int has_children, i;
2221 struct dwarf2_cu cu;
2222 unsigned int bytes_read, buffer_size;
2223 struct die_reader_specs reader_specs;
2224 char *name, *comp_dir;
2225 void **slot;
2226 struct quick_file_names *qfn;
2227 unsigned int line_offset;
2228
2229 if (this_cu->v.quick->file_names != NULL)
2230 return this_cu->v.quick->file_names;
2231 /* If we know there is no line data, no point in looking again. */
2232 if (this_cu->v.quick->no_file_data)
2233 return NULL;
2234
2235 init_one_comp_unit (&cu, this_cu);
2236 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2237
2238 if (this_cu->debug_types_section)
2239 sec = this_cu->debug_types_section;
2240 else
2241 sec = &dwarf2_per_objfile->info;
2242 dwarf2_read_section (objfile, sec);
2243 buffer_size = sec->size;
2244 buffer = sec->buffer;
2245 info_ptr = buffer + this_cu->offset;
2246
2247 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2248 buffer, buffer_size,
2249 abfd,
2250 this_cu->debug_types_section != NULL);
2251
2252 /* Skip dummy compilation units. */
2253 if (info_ptr >= buffer + buffer_size
2254 || peek_abbrev_code (abfd, info_ptr) == 0)
2255 {
2256 do_cleanups (cleanups);
2257 return NULL;
2258 }
2259
2260 dwarf2_read_abbrevs (&cu);
2261 make_cleanup (dwarf2_free_abbrev_table, &cu);
2262
2263 init_cu_die_reader (&reader_specs, &cu);
2264 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2265 &has_children);
2266
2267 lh = NULL;
2268 slot = NULL;
2269 line_offset = 0;
2270 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2271 if (attr)
2272 {
2273 struct quick_file_names find_entry;
2274
2275 line_offset = DW_UNSND (attr);
2276
2277 /* We may have already read in this line header (TU line header sharing).
2278 If we have we're done. */
2279 find_entry.offset = line_offset;
2280 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2281 &find_entry, INSERT);
2282 if (*slot != NULL)
2283 {
2284 do_cleanups (cleanups);
2285 this_cu->v.quick->file_names = *slot;
2286 return *slot;
2287 }
2288
2289 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2290 }
2291 if (lh == NULL)
2292 {
2293 do_cleanups (cleanups);
2294 this_cu->v.quick->no_file_data = 1;
2295 return NULL;
2296 }
2297
2298 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2299 qfn->offset = line_offset;
2300 gdb_assert (slot != NULL);
2301 *slot = qfn;
2302
2303 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2304
2305 qfn->num_file_names = lh->num_file_names;
2306 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2307 lh->num_file_names * sizeof (char *));
2308 for (i = 0; i < lh->num_file_names; ++i)
2309 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2310 qfn->real_names = NULL;
2311
2312 free_line_header (lh);
2313 do_cleanups (cleanups);
2314
2315 this_cu->v.quick->file_names = qfn;
2316 return qfn;
2317 }
2318
2319 /* A helper for the "quick" functions which computes and caches the
2320 real path for a given file name from the line table. */
2321
2322 static const char *
2323 dw2_get_real_path (struct objfile *objfile,
2324 struct quick_file_names *qfn, int index)
2325 {
2326 if (qfn->real_names == NULL)
2327 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2328 qfn->num_file_names, sizeof (char *));
2329
2330 if (qfn->real_names[index] == NULL)
2331 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2332
2333 return qfn->real_names[index];
2334 }
2335
2336 static struct symtab *
2337 dw2_find_last_source_symtab (struct objfile *objfile)
2338 {
2339 int index;
2340
2341 dw2_setup (objfile);
2342 index = dwarf2_per_objfile->n_comp_units - 1;
2343 return dw2_instantiate_symtab (dw2_get_cu (index));
2344 }
2345
2346 /* Traversal function for dw2_forget_cached_source_info. */
2347
2348 static int
2349 dw2_free_cached_file_names (void **slot, void *info)
2350 {
2351 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2352
2353 if (file_data->real_names)
2354 {
2355 int i;
2356
2357 for (i = 0; i < file_data->num_file_names; ++i)
2358 {
2359 xfree ((void*) file_data->real_names[i]);
2360 file_data->real_names[i] = NULL;
2361 }
2362 }
2363
2364 return 1;
2365 }
2366
2367 static void
2368 dw2_forget_cached_source_info (struct objfile *objfile)
2369 {
2370 dw2_setup (objfile);
2371
2372 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2373 dw2_free_cached_file_names, NULL);
2374 }
2375
2376 /* Helper function for dw2_map_symtabs_matching_filename that expands
2377 the symtabs and calls the iterator. */
2378
2379 static int
2380 dw2_map_expand_apply (struct objfile *objfile,
2381 struct dwarf2_per_cu_data *per_cu,
2382 const char *name,
2383 const char *full_path, const char *real_path,
2384 int (*callback) (struct symtab *, void *),
2385 void *data)
2386 {
2387 struct symtab *last_made = objfile->symtabs;
2388
2389 /* Don't visit already-expanded CUs. */
2390 if (per_cu->v.quick->symtab)
2391 return 0;
2392
2393 /* This may expand more than one symtab, and we want to iterate over
2394 all of them. */
2395 dw2_instantiate_symtab (per_cu);
2396
2397 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2398 objfile->symtabs, last_made);
2399 }
2400
2401 /* Implementation of the map_symtabs_matching_filename method. */
2402
2403 static int
2404 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2405 const char *full_path, const char *real_path,
2406 int (*callback) (struct symtab *, void *),
2407 void *data)
2408 {
2409 int i;
2410 const char *name_basename = lbasename (name);
2411 int check_basename = name_basename == name;
2412 struct dwarf2_per_cu_data *base_cu = NULL;
2413
2414 dw2_setup (objfile);
2415
2416 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2417 + dwarf2_per_objfile->n_type_comp_units); ++i)
2418 {
2419 int j;
2420 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2421 struct quick_file_names *file_data;
2422
2423 /* We only need to look at symtabs not already expanded. */
2424 if (per_cu->v.quick->symtab)
2425 continue;
2426
2427 file_data = dw2_get_file_names (objfile, per_cu);
2428 if (file_data == NULL)
2429 continue;
2430
2431 for (j = 0; j < file_data->num_file_names; ++j)
2432 {
2433 const char *this_name = file_data->file_names[j];
2434
2435 if (FILENAME_CMP (name, this_name) == 0)
2436 {
2437 if (dw2_map_expand_apply (objfile, per_cu,
2438 name, full_path, real_path,
2439 callback, data))
2440 return 1;
2441 }
2442
2443 if (check_basename && ! base_cu
2444 && FILENAME_CMP (lbasename (this_name), name) == 0)
2445 base_cu = per_cu;
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 {
2461 if (dw2_map_expand_apply (objfile, per_cu,
2462 name, full_path, real_path,
2463 callback, data))
2464 return 1;
2465 }
2466 }
2467
2468 if (real_path != NULL)
2469 {
2470 const char *this_real_name = dw2_get_real_path (objfile,
2471 file_data, j);
2472
2473 if (this_real_name != NULL
2474 && FILENAME_CMP (real_path, this_real_name) == 0)
2475 {
2476 if (dw2_map_expand_apply (objfile, per_cu,
2477 name, full_path, real_path,
2478 callback, data))
2479 return 1;
2480 }
2481 }
2482 }
2483 }
2484
2485 if (base_cu)
2486 {
2487 if (dw2_map_expand_apply (objfile, base_cu,
2488 name, full_path, real_path,
2489 callback, data))
2490 return 1;
2491 }
2492
2493 return 0;
2494 }
2495
2496 static struct symtab *
2497 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2498 const char *name, domain_enum domain)
2499 {
2500 /* We do all the work in the pre_expand_symtabs_matching hook
2501 instead. */
2502 return NULL;
2503 }
2504
2505 /* A helper function that expands all symtabs that hold an object
2506 named NAME. */
2507
2508 static void
2509 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2510 {
2511 dw2_setup (objfile);
2512
2513 /* index_table is NULL if OBJF_READNOW. */
2514 if (dwarf2_per_objfile->index_table)
2515 {
2516 offset_type *vec;
2517
2518 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2519 name, &vec))
2520 {
2521 offset_type i, len = MAYBE_SWAP (*vec);
2522 for (i = 0; i < len; ++i)
2523 {
2524 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2525 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2526
2527 dw2_instantiate_symtab (per_cu);
2528 }
2529 }
2530 }
2531 }
2532
2533 static void
2534 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2535 enum block_enum block_kind, const char *name,
2536 domain_enum domain)
2537 {
2538 dw2_do_expand_symtabs_matching (objfile, name);
2539 }
2540
2541 static void
2542 dw2_print_stats (struct objfile *objfile)
2543 {
2544 int i, count;
2545
2546 dw2_setup (objfile);
2547 count = 0;
2548 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2549 + dwarf2_per_objfile->n_type_comp_units); ++i)
2550 {
2551 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2552
2553 if (!per_cu->v.quick->symtab)
2554 ++count;
2555 }
2556 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2557 }
2558
2559 static void
2560 dw2_dump (struct objfile *objfile)
2561 {
2562 /* Nothing worth printing. */
2563 }
2564
2565 static void
2566 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2567 struct section_offsets *delta)
2568 {
2569 /* There's nothing to relocate here. */
2570 }
2571
2572 static void
2573 dw2_expand_symtabs_for_function (struct objfile *objfile,
2574 const char *func_name)
2575 {
2576 dw2_do_expand_symtabs_matching (objfile, func_name);
2577 }
2578
2579 static void
2580 dw2_expand_all_symtabs (struct objfile *objfile)
2581 {
2582 int i;
2583
2584 dw2_setup (objfile);
2585
2586 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2587 + dwarf2_per_objfile->n_type_comp_units); ++i)
2588 {
2589 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2590
2591 dw2_instantiate_symtab (per_cu);
2592 }
2593 }
2594
2595 static void
2596 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2597 const char *filename)
2598 {
2599 int i;
2600
2601 dw2_setup (objfile);
2602
2603 /* We don't need to consider type units here.
2604 This is only called for examining code, e.g. expand_line_sal.
2605 There can be an order of magnitude (or more) more type units
2606 than comp units, and we avoid them if we can. */
2607
2608 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2609 {
2610 int j;
2611 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2612 struct quick_file_names *file_data;
2613
2614 /* We only need to look at symtabs not already expanded. */
2615 if (per_cu->v.quick->symtab)
2616 continue;
2617
2618 file_data = dw2_get_file_names (objfile, per_cu);
2619 if (file_data == NULL)
2620 continue;
2621
2622 for (j = 0; j < file_data->num_file_names; ++j)
2623 {
2624 const char *this_name = file_data->file_names[j];
2625 if (FILENAME_CMP (this_name, filename) == 0)
2626 {
2627 dw2_instantiate_symtab (per_cu);
2628 break;
2629 }
2630 }
2631 }
2632 }
2633
2634 static const char *
2635 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2636 {
2637 struct dwarf2_per_cu_data *per_cu;
2638 offset_type *vec;
2639 struct quick_file_names *file_data;
2640
2641 dw2_setup (objfile);
2642
2643 /* index_table is NULL if OBJF_READNOW. */
2644 if (!dwarf2_per_objfile->index_table)
2645 {
2646 struct symtab *s;
2647
2648 ALL_OBJFILE_SYMTABS (objfile, s)
2649 if (s->primary)
2650 {
2651 struct blockvector *bv = BLOCKVECTOR (s);
2652 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2653 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2654
2655 if (sym)
2656 return sym->symtab->filename;
2657 }
2658 return NULL;
2659 }
2660
2661 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2662 name, &vec))
2663 return NULL;
2664
2665 /* Note that this just looks at the very first one named NAME -- but
2666 actually we are looking for a function. find_main_filename
2667 should be rewritten so that it doesn't require a custom hook. It
2668 could just use the ordinary symbol tables. */
2669 /* vec[0] is the length, which must always be >0. */
2670 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2671
2672 file_data = dw2_get_file_names (objfile, per_cu);
2673 if (file_data == NULL)
2674 return NULL;
2675
2676 return file_data->file_names[file_data->num_file_names - 1];
2677 }
2678
2679 static void
2680 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2681 struct objfile *objfile, int global,
2682 int (*callback) (struct block *,
2683 struct symbol *, void *),
2684 void *data, symbol_compare_ftype *match,
2685 symbol_compare_ftype *ordered_compare)
2686 {
2687 /* Currently unimplemented; used for Ada. The function can be called if the
2688 current language is Ada for a non-Ada objfile using GNU index. As Ada
2689 does not look for non-Ada symbols this function should just return. */
2690 }
2691
2692 static void
2693 dw2_expand_symtabs_matching
2694 (struct objfile *objfile,
2695 int (*file_matcher) (const char *, void *),
2696 int (*name_matcher) (const struct language_defn *, const char *, void *),
2697 enum search_domain kind,
2698 void *data)
2699 {
2700 int i;
2701 offset_type iter;
2702 struct mapped_index *index;
2703
2704 dw2_setup (objfile);
2705
2706 /* index_table is NULL if OBJF_READNOW. */
2707 if (!dwarf2_per_objfile->index_table)
2708 return;
2709 index = dwarf2_per_objfile->index_table;
2710
2711 if (file_matcher != NULL)
2712 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2713 + dwarf2_per_objfile->n_type_comp_units); ++i)
2714 {
2715 int j;
2716 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2717 struct quick_file_names *file_data;
2718
2719 per_cu->v.quick->mark = 0;
2720
2721 /* We only need to look at symtabs not already expanded. */
2722 if (per_cu->v.quick->symtab)
2723 continue;
2724
2725 file_data = dw2_get_file_names (objfile, per_cu);
2726 if (file_data == NULL)
2727 continue;
2728
2729 for (j = 0; j < file_data->num_file_names; ++j)
2730 {
2731 if (file_matcher (file_data->file_names[j], data))
2732 {
2733 per_cu->v.quick->mark = 1;
2734 break;
2735 }
2736 }
2737 }
2738
2739 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2740 {
2741 offset_type idx = 2 * iter;
2742 const char *name;
2743 offset_type *vec, vec_len, vec_idx;
2744
2745 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2746 continue;
2747
2748 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2749
2750 if (! (*name_matcher) (current_language, name, data))
2751 continue;
2752
2753 /* The name was matched, now expand corresponding CUs that were
2754 marked. */
2755 vec = (offset_type *) (index->constant_pool
2756 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2757 vec_len = MAYBE_SWAP (vec[0]);
2758 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2759 {
2760 struct dwarf2_per_cu_data *per_cu;
2761
2762 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2763 if (file_matcher == NULL || per_cu->v.quick->mark)
2764 dw2_instantiate_symtab (per_cu);
2765 }
2766 }
2767 }
2768
2769 static struct symtab *
2770 dw2_find_pc_sect_symtab (struct objfile *objfile,
2771 struct minimal_symbol *msymbol,
2772 CORE_ADDR pc,
2773 struct obj_section *section,
2774 int warn_if_readin)
2775 {
2776 struct dwarf2_per_cu_data *data;
2777
2778 dw2_setup (objfile);
2779
2780 if (!objfile->psymtabs_addrmap)
2781 return NULL;
2782
2783 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2784 if (!data)
2785 return NULL;
2786
2787 if (warn_if_readin && data->v.quick->symtab)
2788 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2789 paddress (get_objfile_arch (objfile), pc));
2790
2791 return dw2_instantiate_symtab (data);
2792 }
2793
2794 static void
2795 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2796 void *data, int need_fullname)
2797 {
2798 int i;
2799
2800 dw2_setup (objfile);
2801
2802 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2803 + dwarf2_per_objfile->n_type_comp_units); ++i)
2804 {
2805 int j;
2806 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2807 struct quick_file_names *file_data;
2808
2809 /* We only need to look at symtabs not already expanded. */
2810 if (per_cu->v.quick->symtab)
2811 continue;
2812
2813 file_data = dw2_get_file_names (objfile, per_cu);
2814 if (file_data == NULL)
2815 continue;
2816
2817 for (j = 0; j < file_data->num_file_names; ++j)
2818 {
2819 const char *this_real_name;
2820
2821 if (need_fullname)
2822 this_real_name = dw2_get_real_path (objfile, file_data, j);
2823 else
2824 this_real_name = NULL;
2825 (*fun) (file_data->file_names[j], this_real_name, data);
2826 }
2827 }
2828 }
2829
2830 static int
2831 dw2_has_symbols (struct objfile *objfile)
2832 {
2833 return 1;
2834 }
2835
2836 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2837 {
2838 dw2_has_symbols,
2839 dw2_find_last_source_symtab,
2840 dw2_forget_cached_source_info,
2841 dw2_map_symtabs_matching_filename,
2842 dw2_lookup_symbol,
2843 dw2_pre_expand_symtabs_matching,
2844 dw2_print_stats,
2845 dw2_dump,
2846 dw2_relocate,
2847 dw2_expand_symtabs_for_function,
2848 dw2_expand_all_symtabs,
2849 dw2_expand_symtabs_with_filename,
2850 dw2_find_symbol_file,
2851 dw2_map_matching_symbols,
2852 dw2_expand_symtabs_matching,
2853 dw2_find_pc_sect_symtab,
2854 dw2_map_symbol_filenames
2855 };
2856
2857 /* Initialize for reading DWARF for this objfile. Return 0 if this
2858 file will use psymtabs, or 1 if using the GNU index. */
2859
2860 int
2861 dwarf2_initialize_objfile (struct objfile *objfile)
2862 {
2863 /* If we're about to read full symbols, don't bother with the
2864 indices. In this case we also don't care if some other debug
2865 format is making psymtabs, because they are all about to be
2866 expanded anyway. */
2867 if ((objfile->flags & OBJF_READNOW))
2868 {
2869 int i;
2870
2871 dwarf2_per_objfile->using_index = 1;
2872 create_all_comp_units (objfile);
2873 create_debug_types_hash_table (objfile);
2874 dwarf2_per_objfile->quick_file_names_table =
2875 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2876
2877 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2878 + dwarf2_per_objfile->n_type_comp_units); ++i)
2879 {
2880 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2881
2882 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2883 struct dwarf2_per_cu_quick_data);
2884 }
2885
2886 /* Return 1 so that gdb sees the "quick" functions. However,
2887 these functions will be no-ops because we will have expanded
2888 all symtabs. */
2889 return 1;
2890 }
2891
2892 if (dwarf2_read_index (objfile))
2893 return 1;
2894
2895 return 0;
2896 }
2897
2898 \f
2899
2900 /* Build a partial symbol table. */
2901
2902 void
2903 dwarf2_build_psymtabs (struct objfile *objfile)
2904 {
2905 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2906 {
2907 init_psymbol_list (objfile, 1024);
2908 }
2909
2910 dwarf2_build_psymtabs_hard (objfile);
2911 }
2912
2913 /* Return TRUE if OFFSET is within CU_HEADER. */
2914
2915 static inline int
2916 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2917 {
2918 unsigned int bottom = cu_header->offset;
2919 unsigned int top = (cu_header->offset
2920 + cu_header->length
2921 + cu_header->initial_length_size);
2922
2923 return (offset >= bottom && offset < top);
2924 }
2925
2926 /* Read in the comp unit header information from the debug_info at info_ptr.
2927 NOTE: This leaves members offset, first_die_offset to be filled in
2928 by the caller. */
2929
2930 static gdb_byte *
2931 read_comp_unit_head (struct comp_unit_head *cu_header,
2932 gdb_byte *info_ptr, bfd *abfd)
2933 {
2934 int signed_addr;
2935 unsigned int bytes_read;
2936
2937 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2938 cu_header->initial_length_size = bytes_read;
2939 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2940 info_ptr += bytes_read;
2941 cu_header->version = read_2_bytes (abfd, info_ptr);
2942 info_ptr += 2;
2943 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2944 &bytes_read);
2945 info_ptr += bytes_read;
2946 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2947 info_ptr += 1;
2948 signed_addr = bfd_get_sign_extend_vma (abfd);
2949 if (signed_addr < 0)
2950 internal_error (__FILE__, __LINE__,
2951 _("read_comp_unit_head: dwarf from non elf file"));
2952 cu_header->signed_addr_p = signed_addr;
2953
2954 return info_ptr;
2955 }
2956
2957 /* Read in a CU header and perform some basic error checking. */
2958
2959 static gdb_byte *
2960 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2961 gdb_byte *buffer, unsigned int buffer_size,
2962 bfd *abfd, int is_debug_types_section)
2963 {
2964 gdb_byte *beg_of_comp_unit = info_ptr;
2965
2966 header->offset = beg_of_comp_unit - buffer;
2967
2968 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2969
2970 /* If we're reading a type unit, skip over the signature and
2971 type_offset fields. */
2972 if (is_debug_types_section)
2973 info_ptr += 8 /*signature*/ + header->offset_size;
2974
2975 header->first_die_offset = info_ptr - beg_of_comp_unit;
2976
2977 if (header->version != 2 && header->version != 3 && header->version != 4)
2978 error (_("Dwarf Error: wrong version in compilation unit header "
2979 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2980 bfd_get_filename (abfd));
2981
2982 if (header->abbrev_offset
2983 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2984 &dwarf2_per_objfile->abbrev))
2985 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2986 "(offset 0x%lx + 6) [in module %s]"),
2987 (long) header->abbrev_offset,
2988 (long) (beg_of_comp_unit - buffer),
2989 bfd_get_filename (abfd));
2990
2991 if (beg_of_comp_unit + header->length + header->initial_length_size
2992 > buffer + buffer_size)
2993 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2994 "(offset 0x%lx + 0) [in module %s]"),
2995 (long) header->length,
2996 (long) (beg_of_comp_unit - buffer),
2997 bfd_get_filename (abfd));
2998
2999 return info_ptr;
3000 }
3001
3002 /* Read in the types comp unit header information from .debug_types entry at
3003 types_ptr. The result is a pointer to one past the end of the header. */
3004
3005 static gdb_byte *
3006 read_type_comp_unit_head (struct comp_unit_head *cu_header,
3007 struct dwarf2_section_info *section,
3008 ULONGEST *signature,
3009 gdb_byte *types_ptr, bfd *abfd)
3010 {
3011 gdb_byte *initial_types_ptr = types_ptr;
3012
3013 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
3014 cu_header->offset = types_ptr - section->buffer;
3015
3016 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
3017
3018 *signature = read_8_bytes (abfd, types_ptr);
3019 types_ptr += 8;
3020 types_ptr += cu_header->offset_size;
3021 cu_header->first_die_offset = types_ptr - initial_types_ptr;
3022
3023 return types_ptr;
3024 }
3025
3026 /* Allocate a new partial symtab for file named NAME and mark this new
3027 partial symtab as being an include of PST. */
3028
3029 static void
3030 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3031 struct objfile *objfile)
3032 {
3033 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3034
3035 subpst->section_offsets = pst->section_offsets;
3036 subpst->textlow = 0;
3037 subpst->texthigh = 0;
3038
3039 subpst->dependencies = (struct partial_symtab **)
3040 obstack_alloc (&objfile->objfile_obstack,
3041 sizeof (struct partial_symtab *));
3042 subpst->dependencies[0] = pst;
3043 subpst->number_of_dependencies = 1;
3044
3045 subpst->globals_offset = 0;
3046 subpst->n_global_syms = 0;
3047 subpst->statics_offset = 0;
3048 subpst->n_static_syms = 0;
3049 subpst->symtab = NULL;
3050 subpst->read_symtab = pst->read_symtab;
3051 subpst->readin = 0;
3052
3053 /* No private part is necessary for include psymtabs. This property
3054 can be used to differentiate between such include psymtabs and
3055 the regular ones. */
3056 subpst->read_symtab_private = NULL;
3057 }
3058
3059 /* Read the Line Number Program data and extract the list of files
3060 included by the source file represented by PST. Build an include
3061 partial symtab for each of these included files. */
3062
3063 static void
3064 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3065 struct die_info *die,
3066 struct partial_symtab *pst)
3067 {
3068 struct objfile *objfile = cu->objfile;
3069 bfd *abfd = objfile->obfd;
3070 struct line_header *lh = NULL;
3071 struct attribute *attr;
3072
3073 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3074 if (attr)
3075 {
3076 unsigned int line_offset = DW_UNSND (attr);
3077
3078 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3079 }
3080 if (lh == NULL)
3081 return; /* No linetable, so no includes. */
3082
3083 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3084 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3085
3086 free_line_header (lh);
3087 }
3088
3089 static hashval_t
3090 hash_type_signature (const void *item)
3091 {
3092 const struct signatured_type *type_sig = item;
3093
3094 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3095 return type_sig->signature;
3096 }
3097
3098 static int
3099 eq_type_signature (const void *item_lhs, const void *item_rhs)
3100 {
3101 const struct signatured_type *lhs = item_lhs;
3102 const struct signatured_type *rhs = item_rhs;
3103
3104 return lhs->signature == rhs->signature;
3105 }
3106
3107 /* Allocate a hash table for signatured types. */
3108
3109 static htab_t
3110 allocate_signatured_type_table (struct objfile *objfile)
3111 {
3112 return htab_create_alloc_ex (41,
3113 hash_type_signature,
3114 eq_type_signature,
3115 NULL,
3116 &objfile->objfile_obstack,
3117 hashtab_obstack_allocate,
3118 dummy_obstack_deallocate);
3119 }
3120
3121 /* A helper function to add a signatured type CU to a list. */
3122
3123 static int
3124 add_signatured_type_cu_to_list (void **slot, void *datum)
3125 {
3126 struct signatured_type *sigt = *slot;
3127 struct dwarf2_per_cu_data ***datap = datum;
3128
3129 **datap = &sigt->per_cu;
3130 ++*datap;
3131
3132 return 1;
3133 }
3134
3135 /* Create the hash table of all entries in the .debug_types section.
3136 The result is zero if there is an error (e.g. missing .debug_types section),
3137 otherwise non-zero. */
3138
3139 static int
3140 create_debug_types_hash_table (struct objfile *objfile)
3141 {
3142 htab_t types_htab = NULL;
3143 struct dwarf2_per_cu_data **iter;
3144 int ix;
3145 struct dwarf2_section_info *section;
3146
3147 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3148 {
3149 dwarf2_per_objfile->signatured_types = NULL;
3150 return 0;
3151 }
3152
3153 for (ix = 0;
3154 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3155 ix, section);
3156 ++ix)
3157 {
3158 gdb_byte *info_ptr, *end_ptr;
3159
3160 dwarf2_read_section (objfile, section);
3161 info_ptr = section->buffer;
3162
3163 if (info_ptr == NULL)
3164 continue;
3165
3166 if (types_htab == NULL)
3167 types_htab = allocate_signatured_type_table (objfile);
3168
3169 if (dwarf2_die_debug)
3170 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3171
3172 end_ptr = info_ptr + section->size;
3173 while (info_ptr < end_ptr)
3174 {
3175 unsigned int offset;
3176 unsigned int offset_size;
3177 unsigned int type_offset;
3178 unsigned int length, initial_length_size;
3179 unsigned short version;
3180 ULONGEST signature;
3181 struct signatured_type *type_sig;
3182 void **slot;
3183 gdb_byte *ptr = info_ptr;
3184
3185 offset = ptr - section->buffer;
3186
3187 /* We need to read the type's signature in order to build the hash
3188 table, but we don't need to read anything else just yet. */
3189
3190 /* Sanity check to ensure entire cu is present. */
3191 length = read_initial_length (objfile->obfd, ptr,
3192 &initial_length_size);
3193 if (ptr + length + initial_length_size > end_ptr)
3194 {
3195 complaint (&symfile_complaints,
3196 _("debug type entry runs off end "
3197 "of `.debug_types' section, ignored"));
3198 break;
3199 }
3200
3201 offset_size = initial_length_size == 4 ? 4 : 8;
3202 ptr += initial_length_size;
3203 version = bfd_get_16 (objfile->obfd, ptr);
3204 ptr += 2;
3205 ptr += offset_size; /* abbrev offset */
3206 ptr += 1; /* address size */
3207 signature = bfd_get_64 (objfile->obfd, ptr);
3208 ptr += 8;
3209 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3210 ptr += offset_size;
3211
3212 /* Skip dummy type units. */
3213 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3214 {
3215 info_ptr = info_ptr + initial_length_size + length;
3216 continue;
3217 }
3218
3219 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3220 memset (type_sig, 0, sizeof (*type_sig));
3221 type_sig->signature = signature;
3222 type_sig->type_offset = type_offset;
3223 type_sig->per_cu.objfile = objfile;
3224 type_sig->per_cu.debug_types_section = section;
3225 type_sig->per_cu.offset = offset;
3226
3227 slot = htab_find_slot (types_htab, type_sig, INSERT);
3228 gdb_assert (slot != NULL);
3229 if (*slot != NULL)
3230 {
3231 const struct signatured_type *dup_sig = *slot;
3232
3233 complaint (&symfile_complaints,
3234 _("debug type entry at offset 0x%x is duplicate to the "
3235 "entry at offset 0x%x, signature 0x%s"),
3236 offset, dup_sig->per_cu.offset,
3237 phex (signature, sizeof (signature)));
3238 gdb_assert (signature == dup_sig->signature);
3239 }
3240 *slot = type_sig;
3241
3242 if (dwarf2_die_debug)
3243 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3244 offset, phex (signature, sizeof (signature)));
3245
3246 info_ptr = info_ptr + initial_length_size + length;
3247 }
3248 }
3249
3250 dwarf2_per_objfile->signatured_types = types_htab;
3251
3252 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3253 dwarf2_per_objfile->type_comp_units
3254 = obstack_alloc (&objfile->objfile_obstack,
3255 dwarf2_per_objfile->n_type_comp_units
3256 * sizeof (struct dwarf2_per_cu_data *));
3257 iter = &dwarf2_per_objfile->type_comp_units[0];
3258 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3259 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3260 == dwarf2_per_objfile->n_type_comp_units);
3261
3262 return 1;
3263 }
3264
3265 /* Lookup a signature based type.
3266 Returns NULL if SIG is not present in the table. */
3267
3268 static struct signatured_type *
3269 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3270 {
3271 struct signatured_type find_entry, *entry;
3272
3273 if (dwarf2_per_objfile->signatured_types == NULL)
3274 {
3275 complaint (&symfile_complaints,
3276 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3277 return 0;
3278 }
3279
3280 find_entry.signature = sig;
3281 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3282 return entry;
3283 }
3284
3285 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3286
3287 static void
3288 init_cu_die_reader (struct die_reader_specs *reader,
3289 struct dwarf2_cu *cu)
3290 {
3291 reader->abfd = cu->objfile->obfd;
3292 reader->cu = cu;
3293 if (cu->per_cu->debug_types_section)
3294 {
3295 gdb_assert (cu->per_cu->debug_types_section->readin);
3296 reader->buffer = cu->per_cu->debug_types_section->buffer;
3297 }
3298 else
3299 {
3300 gdb_assert (dwarf2_per_objfile->info.readin);
3301 reader->buffer = dwarf2_per_objfile->info.buffer;
3302 }
3303 }
3304
3305 /* Find the base address of the compilation unit for range lists and
3306 location lists. It will normally be specified by DW_AT_low_pc.
3307 In DWARF-3 draft 4, the base address could be overridden by
3308 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3309 compilation units with discontinuous ranges. */
3310
3311 static void
3312 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3313 {
3314 struct attribute *attr;
3315
3316 cu->base_known = 0;
3317 cu->base_address = 0;
3318
3319 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3320 if (attr)
3321 {
3322 cu->base_address = DW_ADDR (attr);
3323 cu->base_known = 1;
3324 }
3325 else
3326 {
3327 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3328 if (attr)
3329 {
3330 cu->base_address = DW_ADDR (attr);
3331 cu->base_known = 1;
3332 }
3333 }
3334 }
3335
3336 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3337 to combine the common parts.
3338 Process compilation unit THIS_CU for a psymtab.
3339 SECTION is the section the CU/TU comes from,
3340 either .debug_info or .debug_types. */
3341
3342 void
3343 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3344 struct dwarf2_section_info *section,
3345 int is_debug_types_section)
3346 {
3347 struct objfile *objfile = this_cu->objfile;
3348 bfd *abfd = objfile->obfd;
3349 gdb_byte *buffer = section->buffer;
3350 gdb_byte *info_ptr = buffer + this_cu->offset;
3351 unsigned int buffer_size = section->size;
3352 gdb_byte *beg_of_comp_unit = info_ptr;
3353 struct die_info *comp_unit_die;
3354 struct partial_symtab *pst;
3355 CORE_ADDR baseaddr;
3356 struct cleanup *back_to_inner;
3357 struct dwarf2_cu cu;
3358 int has_children, has_pc_info;
3359 struct attribute *attr;
3360 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3361 struct die_reader_specs reader_specs;
3362 const char *filename;
3363
3364 /* If this compilation unit was already read in, free the
3365 cached copy in order to read it in again. This is
3366 necessary because we skipped some symbols when we first
3367 read in the compilation unit (see load_partial_dies).
3368 This problem could be avoided, but the benefit is
3369 unclear. */
3370 if (this_cu->cu != NULL)
3371 free_one_cached_comp_unit (this_cu->cu);
3372
3373 /* Note that this is a pointer to our stack frame, being
3374 added to a global data structure. It will be cleaned up
3375 in free_stack_comp_unit when we finish with this
3376 compilation unit. */
3377 init_one_comp_unit (&cu, this_cu);
3378 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3379
3380 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3381 buffer, buffer_size,
3382 abfd,
3383 is_debug_types_section);
3384
3385 /* Skip dummy compilation units. */
3386 if (info_ptr >= buffer + buffer_size
3387 || peek_abbrev_code (abfd, info_ptr) == 0)
3388 {
3389 do_cleanups (back_to_inner);
3390 return;
3391 }
3392
3393 cu.list_in_scope = &file_symbols;
3394
3395 /* Read the abbrevs for this compilation unit into a table. */
3396 dwarf2_read_abbrevs (&cu);
3397 make_cleanup (dwarf2_free_abbrev_table, &cu);
3398
3399 /* Read the compilation unit die. */
3400 init_cu_die_reader (&reader_specs, &cu);
3401 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3402 &has_children);
3403
3404 if (is_debug_types_section)
3405 {
3406 /* LENGTH has not been set yet for type units. */
3407 gdb_assert (this_cu->offset == cu.header.offset);
3408 this_cu->length = cu.header.length + cu.header.initial_length_size;
3409 }
3410 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3411 {
3412 do_cleanups (back_to_inner);
3413 return;
3414 }
3415
3416 prepare_one_comp_unit (&cu, comp_unit_die);
3417
3418 /* Allocate a new partial symbol table structure. */
3419 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3420 if (attr == NULL || !DW_STRING (attr))
3421 filename = "";
3422 else
3423 filename = DW_STRING (attr);
3424 pst = start_psymtab_common (objfile, objfile->section_offsets,
3425 filename,
3426 /* TEXTLOW and TEXTHIGH are set below. */
3427 0,
3428 objfile->global_psymbols.next,
3429 objfile->static_psymbols.next);
3430 pst->psymtabs_addrmap_supported = 1;
3431
3432 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3433 if (attr != NULL)
3434 pst->dirname = DW_STRING (attr);
3435
3436 pst->read_symtab_private = this_cu;
3437
3438 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3439
3440 /* Store the function that reads in the rest of the symbol table. */
3441 pst->read_symtab = dwarf2_psymtab_to_symtab;
3442
3443 this_cu->v.psymtab = pst;
3444
3445 dwarf2_find_base_address (comp_unit_die, &cu);
3446
3447 /* Possibly set the default values of LOWPC and HIGHPC from
3448 `DW_AT_ranges'. */
3449 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3450 &best_highpc, &cu, pst);
3451 if (has_pc_info == 1 && best_lowpc < best_highpc)
3452 /* Store the contiguous range if it is not empty; it can be empty for
3453 CUs with no code. */
3454 addrmap_set_empty (objfile->psymtabs_addrmap,
3455 best_lowpc + baseaddr,
3456 best_highpc + baseaddr - 1, pst);
3457
3458 /* Check if comp unit has_children.
3459 If so, read the rest of the partial symbols from this comp unit.
3460 If not, there's no more debug_info for this comp unit. */
3461 if (has_children)
3462 {
3463 struct partial_die_info *first_die;
3464 CORE_ADDR lowpc, highpc;
3465
3466 lowpc = ((CORE_ADDR) -1);
3467 highpc = ((CORE_ADDR) 0);
3468
3469 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3470
3471 scan_partial_symbols (first_die, &lowpc, &highpc,
3472 ! has_pc_info, &cu);
3473
3474 /* If we didn't find a lowpc, set it to highpc to avoid
3475 complaints from `maint check'. */
3476 if (lowpc == ((CORE_ADDR) -1))
3477 lowpc = highpc;
3478
3479 /* If the compilation unit didn't have an explicit address range,
3480 then use the information extracted from its child dies. */
3481 if (! has_pc_info)
3482 {
3483 best_lowpc = lowpc;
3484 best_highpc = highpc;
3485 }
3486 }
3487 pst->textlow = best_lowpc + baseaddr;
3488 pst->texthigh = best_highpc + baseaddr;
3489
3490 pst->n_global_syms = objfile->global_psymbols.next -
3491 (objfile->global_psymbols.list + pst->globals_offset);
3492 pst->n_static_syms = objfile->static_psymbols.next -
3493 (objfile->static_psymbols.list + pst->statics_offset);
3494 sort_pst_symbols (pst);
3495
3496 if (is_debug_types_section)
3497 {
3498 /* It's not clear we want to do anything with stmt lists here.
3499 Waiting to see what gcc ultimately does. */
3500 }
3501 else
3502 {
3503 /* Get the list of files included in the current compilation unit,
3504 and build a psymtab for each of them. */
3505 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3506 }
3507
3508 do_cleanups (back_to_inner);
3509 }
3510
3511 /* Traversal function for htab_traverse_noresize.
3512 Process one .debug_types comp-unit. */
3513
3514 static int
3515 process_type_comp_unit (void **slot, void *info)
3516 {
3517 struct signatured_type *entry = (struct signatured_type *) *slot;
3518 struct dwarf2_per_cu_data *this_cu;
3519
3520 gdb_assert (info == NULL);
3521 this_cu = &entry->per_cu;
3522
3523 gdb_assert (this_cu->debug_types_section->readin);
3524 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3525
3526 return 1;
3527 }
3528
3529 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3530 Build partial symbol tables for the .debug_types comp-units. */
3531
3532 static void
3533 build_type_psymtabs (struct objfile *objfile)
3534 {
3535 if (! create_debug_types_hash_table (objfile))
3536 return;
3537
3538 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3539 process_type_comp_unit, NULL);
3540 }
3541
3542 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3543
3544 static void
3545 psymtabs_addrmap_cleanup (void *o)
3546 {
3547 struct objfile *objfile = o;
3548
3549 objfile->psymtabs_addrmap = NULL;
3550 }
3551
3552 /* Build the partial symbol table by doing a quick pass through the
3553 .debug_info and .debug_abbrev sections. */
3554
3555 static void
3556 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3557 {
3558 struct cleanup *back_to, *addrmap_cleanup;
3559 struct obstack temp_obstack;
3560 int i;
3561
3562 dwarf2_per_objfile->reading_partial_symbols = 1;
3563
3564 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3565
3566 /* Any cached compilation units will be linked by the per-objfile
3567 read_in_chain. Make sure to free them when we're done. */
3568 back_to = make_cleanup (free_cached_comp_units, NULL);
3569
3570 build_type_psymtabs (objfile);
3571
3572 create_all_comp_units (objfile);
3573
3574 /* Create a temporary address map on a temporary obstack. We later
3575 copy this to the final obstack. */
3576 obstack_init (&temp_obstack);
3577 make_cleanup_obstack_free (&temp_obstack);
3578 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3579 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3580
3581 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3582 {
3583 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3584
3585 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3586 }
3587
3588 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3589 &objfile->objfile_obstack);
3590 discard_cleanups (addrmap_cleanup);
3591
3592 do_cleanups (back_to);
3593 }
3594
3595 /* Load the partial DIEs for a secondary CU into memory. */
3596
3597 static void
3598 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3599 {
3600 struct objfile *objfile = this_cu->objfile;
3601 bfd *abfd = objfile->obfd;
3602 gdb_byte *info_ptr;
3603 struct die_info *comp_unit_die;
3604 struct dwarf2_cu *cu;
3605 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3606 int has_children;
3607 struct die_reader_specs reader_specs;
3608 int read_cu = 0;
3609
3610 gdb_assert (! this_cu->debug_types_section);
3611
3612 gdb_assert (dwarf2_per_objfile->info.readin);
3613 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3614
3615 if (this_cu->cu == NULL)
3616 {
3617 cu = xmalloc (sizeof (*cu));
3618 init_one_comp_unit (cu, this_cu);
3619
3620 read_cu = 1;
3621
3622 /* If an error occurs while loading, release our storage. */
3623 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3624
3625 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3626 dwarf2_per_objfile->info.buffer,
3627 dwarf2_per_objfile->info.size,
3628 abfd, 0);
3629
3630 /* Skip dummy compilation units. */
3631 if (info_ptr >= (dwarf2_per_objfile->info.buffer
3632 + dwarf2_per_objfile->info.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, dwarf2_per_objfile->info.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 const struct partial_symbol *psym = NULL;
3958 CORE_ADDR baseaddr;
3959 int built_actual_name = 0;
3960
3961 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3962
3963 actual_name = partial_die_full_name (pdi, cu);
3964 if (actual_name)
3965 built_actual_name = 1;
3966
3967 if (actual_name == NULL)
3968 actual_name = pdi->name;
3969
3970 switch (pdi->tag)
3971 {
3972 case DW_TAG_subprogram:
3973 if (pdi->is_external || cu->language == language_ada)
3974 {
3975 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3976 of the global scope. But in Ada, we want to be able to access
3977 nested procedures globally. So all Ada subprograms are stored
3978 in the global scope. */
3979 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3980 mst_text, objfile); */
3981 add_psymbol_to_list (actual_name, strlen (actual_name),
3982 built_actual_name,
3983 VAR_DOMAIN, LOC_BLOCK,
3984 &objfile->global_psymbols,
3985 0, pdi->lowpc + baseaddr,
3986 cu->language, objfile);
3987 }
3988 else
3989 {
3990 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3991 mst_file_text, objfile); */
3992 add_psymbol_to_list (actual_name, strlen (actual_name),
3993 built_actual_name,
3994 VAR_DOMAIN, LOC_BLOCK,
3995 &objfile->static_psymbols,
3996 0, pdi->lowpc + baseaddr,
3997 cu->language, objfile);
3998 }
3999 break;
4000 case DW_TAG_constant:
4001 {
4002 struct psymbol_allocation_list *list;
4003
4004 if (pdi->is_external)
4005 list = &objfile->global_psymbols;
4006 else
4007 list = &objfile->static_psymbols;
4008 add_psymbol_to_list (actual_name, strlen (actual_name),
4009 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4010 list, 0, 0, cu->language, objfile);
4011 }
4012 break;
4013 case DW_TAG_variable:
4014 if (pdi->locdesc)
4015 addr = decode_locdesc (pdi->locdesc, cu);
4016
4017 if (pdi->locdesc
4018 && addr == 0
4019 && !dwarf2_per_objfile->has_section_at_zero)
4020 {
4021 /* A global or static variable may also have been stripped
4022 out by the linker if unused, in which case its address
4023 will be nullified; do not add such variables into partial
4024 symbol table then. */
4025 }
4026 else if (pdi->is_external)
4027 {
4028 /* Global Variable.
4029 Don't enter into the minimal symbol tables as there is
4030 a minimal symbol table entry from the ELF symbols already.
4031 Enter into partial symbol table if it has a location
4032 descriptor or a type.
4033 If the location descriptor is missing, new_symbol will create
4034 a LOC_UNRESOLVED symbol, the address of the variable will then
4035 be determined from the minimal symbol table whenever the variable
4036 is referenced.
4037 The address for the partial symbol table entry is not
4038 used by GDB, but it comes in handy for debugging partial symbol
4039 table building. */
4040
4041 if (pdi->locdesc || pdi->has_type)
4042 add_psymbol_to_list (actual_name, strlen (actual_name),
4043 built_actual_name,
4044 VAR_DOMAIN, LOC_STATIC,
4045 &objfile->global_psymbols,
4046 0, addr + baseaddr,
4047 cu->language, objfile);
4048 }
4049 else
4050 {
4051 /* Static Variable. Skip symbols without location descriptors. */
4052 if (pdi->locdesc == NULL)
4053 {
4054 if (built_actual_name)
4055 xfree (actual_name);
4056 return;
4057 }
4058 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4059 mst_file_data, objfile); */
4060 add_psymbol_to_list (actual_name, strlen (actual_name),
4061 built_actual_name,
4062 VAR_DOMAIN, LOC_STATIC,
4063 &objfile->static_psymbols,
4064 0, addr + baseaddr,
4065 cu->language, objfile);
4066 }
4067 break;
4068 case DW_TAG_typedef:
4069 case DW_TAG_base_type:
4070 case DW_TAG_subrange_type:
4071 add_psymbol_to_list (actual_name, strlen (actual_name),
4072 built_actual_name,
4073 VAR_DOMAIN, LOC_TYPEDEF,
4074 &objfile->static_psymbols,
4075 0, (CORE_ADDR) 0, cu->language, objfile);
4076 break;
4077 case DW_TAG_namespace:
4078 add_psymbol_to_list (actual_name, strlen (actual_name),
4079 built_actual_name,
4080 VAR_DOMAIN, LOC_TYPEDEF,
4081 &objfile->global_psymbols,
4082 0, (CORE_ADDR) 0, cu->language, objfile);
4083 break;
4084 case DW_TAG_class_type:
4085 case DW_TAG_interface_type:
4086 case DW_TAG_structure_type:
4087 case DW_TAG_union_type:
4088 case DW_TAG_enumeration_type:
4089 /* Skip external references. The DWARF standard says in the section
4090 about "Structure, Union, and Class Type Entries": "An incomplete
4091 structure, union or class type is represented by a structure,
4092 union or class entry that does not have a byte size attribute
4093 and that has a DW_AT_declaration attribute." */
4094 if (!pdi->has_byte_size && pdi->is_declaration)
4095 {
4096 if (built_actual_name)
4097 xfree (actual_name);
4098 return;
4099 }
4100
4101 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4102 static vs. global. */
4103 add_psymbol_to_list (actual_name, strlen (actual_name),
4104 built_actual_name,
4105 STRUCT_DOMAIN, LOC_TYPEDEF,
4106 (cu->language == language_cplus
4107 || cu->language == language_java)
4108 ? &objfile->global_psymbols
4109 : &objfile->static_psymbols,
4110 0, (CORE_ADDR) 0, cu->language, objfile);
4111
4112 break;
4113 case DW_TAG_enumerator:
4114 add_psymbol_to_list (actual_name, strlen (actual_name),
4115 built_actual_name,
4116 VAR_DOMAIN, LOC_CONST,
4117 (cu->language == language_cplus
4118 || cu->language == language_java)
4119 ? &objfile->global_psymbols
4120 : &objfile->static_psymbols,
4121 0, (CORE_ADDR) 0, cu->language, objfile);
4122 break;
4123 default:
4124 break;
4125 }
4126
4127 if (built_actual_name)
4128 xfree (actual_name);
4129 }
4130
4131 /* Read a partial die corresponding to a namespace; also, add a symbol
4132 corresponding to that namespace to the symbol table. NAMESPACE is
4133 the name of the enclosing namespace. */
4134
4135 static void
4136 add_partial_namespace (struct partial_die_info *pdi,
4137 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4138 int need_pc, struct dwarf2_cu *cu)
4139 {
4140 /* Add a symbol for the namespace. */
4141
4142 add_partial_symbol (pdi, cu);
4143
4144 /* Now scan partial symbols in that namespace. */
4145
4146 if (pdi->has_children)
4147 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4148 }
4149
4150 /* Read a partial die corresponding to a Fortran module. */
4151
4152 static void
4153 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4154 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4155 {
4156 /* Now scan partial symbols in that module. */
4157
4158 if (pdi->has_children)
4159 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4160 }
4161
4162 /* Read a partial die corresponding to a subprogram and create a partial
4163 symbol for that subprogram. When the CU language allows it, this
4164 routine also defines a partial symbol for each nested subprogram
4165 that this subprogram contains.
4166
4167 DIE my also be a lexical block, in which case we simply search
4168 recursively for suprograms defined inside that lexical block.
4169 Again, this is only performed when the CU language allows this
4170 type of definitions. */
4171
4172 static void
4173 add_partial_subprogram (struct partial_die_info *pdi,
4174 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4175 int need_pc, struct dwarf2_cu *cu)
4176 {
4177 if (pdi->tag == DW_TAG_subprogram)
4178 {
4179 if (pdi->has_pc_info)
4180 {
4181 if (pdi->lowpc < *lowpc)
4182 *lowpc = pdi->lowpc;
4183 if (pdi->highpc > *highpc)
4184 *highpc = pdi->highpc;
4185 if (need_pc)
4186 {
4187 CORE_ADDR baseaddr;
4188 struct objfile *objfile = cu->objfile;
4189
4190 baseaddr = ANOFFSET (objfile->section_offsets,
4191 SECT_OFF_TEXT (objfile));
4192 addrmap_set_empty (objfile->psymtabs_addrmap,
4193 pdi->lowpc + baseaddr,
4194 pdi->highpc - 1 + baseaddr,
4195 cu->per_cu->v.psymtab);
4196 }
4197 if (!pdi->is_declaration)
4198 /* Ignore subprogram DIEs that do not have a name, they are
4199 illegal. Do not emit a complaint at this point, we will
4200 do so when we convert this psymtab into a symtab. */
4201 if (pdi->name)
4202 add_partial_symbol (pdi, cu);
4203 }
4204 }
4205
4206 if (! pdi->has_children)
4207 return;
4208
4209 if (cu->language == language_ada)
4210 {
4211 pdi = pdi->die_child;
4212 while (pdi != NULL)
4213 {
4214 fixup_partial_die (pdi, cu);
4215 if (pdi->tag == DW_TAG_subprogram
4216 || pdi->tag == DW_TAG_lexical_block)
4217 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4218 pdi = pdi->die_sibling;
4219 }
4220 }
4221 }
4222
4223 /* Read a partial die corresponding to an enumeration type. */
4224
4225 static void
4226 add_partial_enumeration (struct partial_die_info *enum_pdi,
4227 struct dwarf2_cu *cu)
4228 {
4229 struct partial_die_info *pdi;
4230
4231 if (enum_pdi->name != NULL)
4232 add_partial_symbol (enum_pdi, cu);
4233
4234 pdi = enum_pdi->die_child;
4235 while (pdi)
4236 {
4237 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4238 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4239 else
4240 add_partial_symbol (pdi, cu);
4241 pdi = pdi->die_sibling;
4242 }
4243 }
4244
4245 /* Return the initial uleb128 in the die at INFO_PTR. */
4246
4247 static unsigned int
4248 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4249 {
4250 unsigned int bytes_read;
4251
4252 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4253 }
4254
4255 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4256 Return the corresponding abbrev, or NULL if the number is zero (indicating
4257 an empty DIE). In either case *BYTES_READ will be set to the length of
4258 the initial number. */
4259
4260 static struct abbrev_info *
4261 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4262 struct dwarf2_cu *cu)
4263 {
4264 bfd *abfd = cu->objfile->obfd;
4265 unsigned int abbrev_number;
4266 struct abbrev_info *abbrev;
4267
4268 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4269
4270 if (abbrev_number == 0)
4271 return NULL;
4272
4273 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4274 if (!abbrev)
4275 {
4276 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4277 abbrev_number, bfd_get_filename (abfd));
4278 }
4279
4280 return abbrev;
4281 }
4282
4283 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4284 Returns a pointer to the end of a series of DIEs, terminated by an empty
4285 DIE. Any children of the skipped DIEs will also be skipped. */
4286
4287 static gdb_byte *
4288 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4289 {
4290 struct abbrev_info *abbrev;
4291 unsigned int bytes_read;
4292
4293 while (1)
4294 {
4295 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4296 if (abbrev == NULL)
4297 return info_ptr + bytes_read;
4298 else
4299 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4300 }
4301 }
4302
4303 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4304 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4305 abbrev corresponding to that skipped uleb128 should be passed in
4306 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4307 children. */
4308
4309 static gdb_byte *
4310 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4311 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4312 {
4313 unsigned int bytes_read;
4314 struct attribute attr;
4315 bfd *abfd = cu->objfile->obfd;
4316 unsigned int form, i;
4317
4318 for (i = 0; i < abbrev->num_attrs; i++)
4319 {
4320 /* The only abbrev we care about is DW_AT_sibling. */
4321 if (abbrev->attrs[i].name == DW_AT_sibling)
4322 {
4323 read_attribute (&attr, &abbrev->attrs[i],
4324 abfd, info_ptr, cu);
4325 if (attr.form == DW_FORM_ref_addr)
4326 complaint (&symfile_complaints,
4327 _("ignoring absolute DW_AT_sibling"));
4328 else
4329 return buffer + dwarf2_get_ref_die_offset (&attr);
4330 }
4331
4332 /* If it isn't DW_AT_sibling, skip this attribute. */
4333 form = abbrev->attrs[i].form;
4334 skip_attribute:
4335 switch (form)
4336 {
4337 case DW_FORM_ref_addr:
4338 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4339 and later it is offset sized. */
4340 if (cu->header.version == 2)
4341 info_ptr += cu->header.addr_size;
4342 else
4343 info_ptr += cu->header.offset_size;
4344 break;
4345 case DW_FORM_addr:
4346 info_ptr += cu->header.addr_size;
4347 break;
4348 case DW_FORM_data1:
4349 case DW_FORM_ref1:
4350 case DW_FORM_flag:
4351 info_ptr += 1;
4352 break;
4353 case DW_FORM_flag_present:
4354 break;
4355 case DW_FORM_data2:
4356 case DW_FORM_ref2:
4357 info_ptr += 2;
4358 break;
4359 case DW_FORM_data4:
4360 case DW_FORM_ref4:
4361 info_ptr += 4;
4362 break;
4363 case DW_FORM_data8:
4364 case DW_FORM_ref8:
4365 case DW_FORM_ref_sig8:
4366 info_ptr += 8;
4367 break;
4368 case DW_FORM_string:
4369 read_direct_string (abfd, info_ptr, &bytes_read);
4370 info_ptr += bytes_read;
4371 break;
4372 case DW_FORM_sec_offset:
4373 case DW_FORM_strp:
4374 info_ptr += cu->header.offset_size;
4375 break;
4376 case DW_FORM_exprloc:
4377 case DW_FORM_block:
4378 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4379 info_ptr += bytes_read;
4380 break;
4381 case DW_FORM_block1:
4382 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4383 break;
4384 case DW_FORM_block2:
4385 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4386 break;
4387 case DW_FORM_block4:
4388 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4389 break;
4390 case DW_FORM_sdata:
4391 case DW_FORM_udata:
4392 case DW_FORM_ref_udata:
4393 info_ptr = skip_leb128 (abfd, info_ptr);
4394 break;
4395 case DW_FORM_indirect:
4396 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4397 info_ptr += bytes_read;
4398 /* We need to continue parsing from here, so just go back to
4399 the top. */
4400 goto skip_attribute;
4401
4402 default:
4403 error (_("Dwarf Error: Cannot handle %s "
4404 "in DWARF reader [in module %s]"),
4405 dwarf_form_name (form),
4406 bfd_get_filename (abfd));
4407 }
4408 }
4409
4410 if (abbrev->has_children)
4411 return skip_children (buffer, info_ptr, cu);
4412 else
4413 return info_ptr;
4414 }
4415
4416 /* Locate ORIG_PDI's sibling.
4417 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4418 in BUFFER. */
4419
4420 static gdb_byte *
4421 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4422 gdb_byte *buffer, gdb_byte *info_ptr,
4423 bfd *abfd, struct dwarf2_cu *cu)
4424 {
4425 /* Do we know the sibling already? */
4426
4427 if (orig_pdi->sibling)
4428 return orig_pdi->sibling;
4429
4430 /* Are there any children to deal with? */
4431
4432 if (!orig_pdi->has_children)
4433 return info_ptr;
4434
4435 /* Skip the children the long way. */
4436
4437 return skip_children (buffer, info_ptr, cu);
4438 }
4439
4440 /* Expand this partial symbol table into a full symbol table. */
4441
4442 static void
4443 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4444 {
4445 if (pst != NULL)
4446 {
4447 if (pst->readin)
4448 {
4449 warning (_("bug: psymtab for %s is already read in."),
4450 pst->filename);
4451 }
4452 else
4453 {
4454 if (info_verbose)
4455 {
4456 printf_filtered (_("Reading in symbols for %s..."),
4457 pst->filename);
4458 gdb_flush (gdb_stdout);
4459 }
4460
4461 /* Restore our global data. */
4462 dwarf2_per_objfile = objfile_data (pst->objfile,
4463 dwarf2_objfile_data_key);
4464
4465 /* If this psymtab is constructed from a debug-only objfile, the
4466 has_section_at_zero flag will not necessarily be correct. We
4467 can get the correct value for this flag by looking at the data
4468 associated with the (presumably stripped) associated objfile. */
4469 if (pst->objfile->separate_debug_objfile_backlink)
4470 {
4471 struct dwarf2_per_objfile *dpo_backlink
4472 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4473 dwarf2_objfile_data_key);
4474
4475 dwarf2_per_objfile->has_section_at_zero
4476 = dpo_backlink->has_section_at_zero;
4477 }
4478
4479 dwarf2_per_objfile->reading_partial_symbols = 0;
4480
4481 psymtab_to_symtab_1 (pst);
4482
4483 /* Finish up the debug error message. */
4484 if (info_verbose)
4485 printf_filtered (_("done.\n"));
4486 }
4487 }
4488 }
4489 \f
4490 /* Reading in full CUs. */
4491
4492 /* Add PER_CU to the queue. */
4493
4494 static void
4495 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4496 {
4497 struct dwarf2_queue_item *item;
4498
4499 per_cu->queued = 1;
4500 item = xmalloc (sizeof (*item));
4501 item->per_cu = per_cu;
4502 item->next = NULL;
4503
4504 if (dwarf2_queue == NULL)
4505 dwarf2_queue = item;
4506 else
4507 dwarf2_queue_tail->next = item;
4508
4509 dwarf2_queue_tail = item;
4510 }
4511
4512 /* Process the queue. */
4513
4514 static void
4515 process_queue (void)
4516 {
4517 struct dwarf2_queue_item *item, *next_item;
4518
4519 /* The queue starts out with one item, but following a DIE reference
4520 may load a new CU, adding it to the end of the queue. */
4521 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4522 {
4523 if (dwarf2_per_objfile->using_index
4524 ? !item->per_cu->v.quick->symtab
4525 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4526 process_full_comp_unit (item->per_cu);
4527
4528 item->per_cu->queued = 0;
4529 next_item = item->next;
4530 xfree (item);
4531 }
4532
4533 dwarf2_queue_tail = NULL;
4534 }
4535
4536 /* Free all allocated queue entries. This function only releases anything if
4537 an error was thrown; if the queue was processed then it would have been
4538 freed as we went along. */
4539
4540 static void
4541 dwarf2_release_queue (void *dummy)
4542 {
4543 struct dwarf2_queue_item *item, *last;
4544
4545 item = dwarf2_queue;
4546 while (item)
4547 {
4548 /* Anything still marked queued is likely to be in an
4549 inconsistent state, so discard it. */
4550 if (item->per_cu->queued)
4551 {
4552 if (item->per_cu->cu != NULL)
4553 free_one_cached_comp_unit (item->per_cu->cu);
4554 item->per_cu->queued = 0;
4555 }
4556
4557 last = item;
4558 item = item->next;
4559 xfree (last);
4560 }
4561
4562 dwarf2_queue = dwarf2_queue_tail = NULL;
4563 }
4564
4565 /* Read in full symbols for PST, and anything it depends on. */
4566
4567 static void
4568 psymtab_to_symtab_1 (struct partial_symtab *pst)
4569 {
4570 struct dwarf2_per_cu_data *per_cu;
4571 struct cleanup *back_to;
4572 int i;
4573
4574 for (i = 0; i < pst->number_of_dependencies; i++)
4575 if (!pst->dependencies[i]->readin)
4576 {
4577 /* Inform about additional files that need to be read in. */
4578 if (info_verbose)
4579 {
4580 /* FIXME: i18n: Need to make this a single string. */
4581 fputs_filtered (" ", gdb_stdout);
4582 wrap_here ("");
4583 fputs_filtered ("and ", gdb_stdout);
4584 wrap_here ("");
4585 printf_filtered ("%s...", pst->dependencies[i]->filename);
4586 wrap_here (""); /* Flush output. */
4587 gdb_flush (gdb_stdout);
4588 }
4589 psymtab_to_symtab_1 (pst->dependencies[i]);
4590 }
4591
4592 per_cu = pst->read_symtab_private;
4593
4594 if (per_cu == NULL)
4595 {
4596 /* It's an include file, no symbols to read for it.
4597 Everything is in the parent symtab. */
4598 pst->readin = 1;
4599 return;
4600 }
4601
4602 dw2_do_instantiate_symtab (per_cu);
4603 }
4604
4605 /* Load the DIEs associated with PER_CU into memory. */
4606
4607 static void
4608 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4609 {
4610 struct objfile *objfile = per_cu->objfile;
4611 bfd *abfd = objfile->obfd;
4612 struct dwarf2_cu *cu;
4613 unsigned int offset;
4614 gdb_byte *info_ptr, *beg_of_comp_unit;
4615 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4616 struct attribute *attr;
4617 int read_cu = 0;
4618
4619 gdb_assert (! per_cu->debug_types_section);
4620
4621 /* Set local variables from the partial symbol table info. */
4622 offset = per_cu->offset;
4623
4624 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4625 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4626 beg_of_comp_unit = info_ptr;
4627
4628 if (per_cu->cu == NULL)
4629 {
4630 cu = xmalloc (sizeof (*cu));
4631 init_one_comp_unit (cu, per_cu);
4632
4633 read_cu = 1;
4634
4635 /* If an error occurs while loading, release our storage. */
4636 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4637
4638 /* Read in the comp_unit header. */
4639 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4640
4641 /* Skip dummy compilation units. */
4642 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4643 + dwarf2_per_objfile->info.size)
4644 || peek_abbrev_code (abfd, info_ptr) == 0)
4645 {
4646 do_cleanups (free_cu_cleanup);
4647 return;
4648 }
4649
4650 /* Complete the cu_header. */
4651 cu->header.offset = offset;
4652 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4653
4654 /* Read the abbrevs for this compilation unit. */
4655 dwarf2_read_abbrevs (cu);
4656 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4657
4658 /* Link this CU into read_in_chain. */
4659 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4660 dwarf2_per_objfile->read_in_chain = per_cu;
4661 }
4662 else
4663 {
4664 cu = per_cu->cu;
4665 info_ptr += cu->header.first_die_offset;
4666 }
4667
4668 cu->dies = read_comp_unit (info_ptr, cu);
4669
4670 /* We try not to read any attributes in this function, because not
4671 all CUs needed for references have been loaded yet, and symbol
4672 table processing isn't initialized. But we have to set the CU language,
4673 or we won't be able to build types correctly. */
4674 prepare_one_comp_unit (cu, cu->dies);
4675
4676 /* Similarly, if we do not read the producer, we can not apply
4677 producer-specific interpretation. */
4678 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4679 if (attr)
4680 cu->producer = DW_STRING (attr);
4681
4682 if (read_cu)
4683 {
4684 do_cleanups (free_abbrevs_cleanup);
4685
4686 /* We've successfully allocated this compilation unit. Let our
4687 caller clean it up when finished with it. */
4688 discard_cleanups (free_cu_cleanup);
4689 }
4690 }
4691
4692 /* Add a DIE to the delayed physname list. */
4693
4694 static void
4695 add_to_method_list (struct type *type, int fnfield_index, int index,
4696 const char *name, struct die_info *die,
4697 struct dwarf2_cu *cu)
4698 {
4699 struct delayed_method_info mi;
4700 mi.type = type;
4701 mi.fnfield_index = fnfield_index;
4702 mi.index = index;
4703 mi.name = name;
4704 mi.die = die;
4705 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4706 }
4707
4708 /* A cleanup for freeing the delayed method list. */
4709
4710 static void
4711 free_delayed_list (void *ptr)
4712 {
4713 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4714 if (cu->method_list != NULL)
4715 {
4716 VEC_free (delayed_method_info, cu->method_list);
4717 cu->method_list = NULL;
4718 }
4719 }
4720
4721 /* Compute the physnames of any methods on the CU's method list.
4722
4723 The computation of method physnames is delayed in order to avoid the
4724 (bad) condition that one of the method's formal parameters is of an as yet
4725 incomplete type. */
4726
4727 static void
4728 compute_delayed_physnames (struct dwarf2_cu *cu)
4729 {
4730 int i;
4731 struct delayed_method_info *mi;
4732 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4733 {
4734 const char *physname;
4735 struct fn_fieldlist *fn_flp
4736 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4737 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4738 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4739 }
4740 }
4741
4742 /* Generate full symbol information for PER_CU, whose DIEs have
4743 already been loaded into memory. */
4744
4745 static void
4746 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4747 {
4748 struct dwarf2_cu *cu = per_cu->cu;
4749 struct objfile *objfile = per_cu->objfile;
4750 CORE_ADDR lowpc, highpc;
4751 struct symtab *symtab;
4752 struct cleanup *back_to, *delayed_list_cleanup;
4753 CORE_ADDR baseaddr;
4754
4755 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4756
4757 buildsym_init ();
4758 back_to = make_cleanup (really_free_pendings, NULL);
4759 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4760
4761 cu->list_in_scope = &file_symbols;
4762
4763 /* Do line number decoding in read_file_scope () */
4764 process_die (cu->dies, cu);
4765
4766 /* Now that we have processed all the DIEs in the CU, all the types
4767 should be complete, and it should now be safe to compute all of the
4768 physnames. */
4769 compute_delayed_physnames (cu);
4770 do_cleanups (delayed_list_cleanup);
4771
4772 /* Some compilers don't define a DW_AT_high_pc attribute for the
4773 compilation unit. If the DW_AT_high_pc is missing, synthesize
4774 it, by scanning the DIE's below the compilation unit. */
4775 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4776
4777 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4778
4779 if (symtab != NULL)
4780 {
4781 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4782
4783 /* Set symtab language to language from DW_AT_language. If the
4784 compilation is from a C file generated by language preprocessors, do
4785 not set the language if it was already deduced by start_subfile. */
4786 if (!(cu->language == language_c && symtab->language != language_c))
4787 symtab->language = cu->language;
4788
4789 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4790 produce DW_AT_location with location lists but it can be possibly
4791 invalid without -fvar-tracking.
4792
4793 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4794 needed, it would be wrong due to missing DW_AT_producer there.
4795
4796 Still one can confuse GDB by using non-standard GCC compilation
4797 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4798 */
4799 if (cu->has_loclist && gcc_4_minor >= 0)
4800 symtab->locations_valid = 1;
4801
4802 if (gcc_4_minor >= 5)
4803 symtab->epilogue_unwind_valid = 1;
4804
4805 symtab->call_site_htab = cu->call_site_htab;
4806 }
4807
4808 if (dwarf2_per_objfile->using_index)
4809 per_cu->v.quick->symtab = symtab;
4810 else
4811 {
4812 struct partial_symtab *pst = per_cu->v.psymtab;
4813 pst->symtab = symtab;
4814 pst->readin = 1;
4815 }
4816
4817 do_cleanups (back_to);
4818 }
4819
4820 /* Process a die and its children. */
4821
4822 static void
4823 process_die (struct die_info *die, struct dwarf2_cu *cu)
4824 {
4825 switch (die->tag)
4826 {
4827 case DW_TAG_padding:
4828 break;
4829 case DW_TAG_compile_unit:
4830 read_file_scope (die, cu);
4831 break;
4832 case DW_TAG_type_unit:
4833 read_type_unit_scope (die, cu);
4834 break;
4835 case DW_TAG_subprogram:
4836 case DW_TAG_inlined_subroutine:
4837 read_func_scope (die, cu);
4838 break;
4839 case DW_TAG_lexical_block:
4840 case DW_TAG_try_block:
4841 case DW_TAG_catch_block:
4842 read_lexical_block_scope (die, cu);
4843 break;
4844 case DW_TAG_GNU_call_site:
4845 read_call_site_scope (die, cu);
4846 break;
4847 case DW_TAG_class_type:
4848 case DW_TAG_interface_type:
4849 case DW_TAG_structure_type:
4850 case DW_TAG_union_type:
4851 process_structure_scope (die, cu);
4852 break;
4853 case DW_TAG_enumeration_type:
4854 process_enumeration_scope (die, cu);
4855 break;
4856
4857 /* These dies have a type, but processing them does not create
4858 a symbol or recurse to process the children. Therefore we can
4859 read them on-demand through read_type_die. */
4860 case DW_TAG_subroutine_type:
4861 case DW_TAG_set_type:
4862 case DW_TAG_array_type:
4863 case DW_TAG_pointer_type:
4864 case DW_TAG_ptr_to_member_type:
4865 case DW_TAG_reference_type:
4866 case DW_TAG_string_type:
4867 break;
4868
4869 case DW_TAG_base_type:
4870 case DW_TAG_subrange_type:
4871 case DW_TAG_typedef:
4872 /* Add a typedef symbol for the type definition, if it has a
4873 DW_AT_name. */
4874 new_symbol (die, read_type_die (die, cu), cu);
4875 break;
4876 case DW_TAG_common_block:
4877 read_common_block (die, cu);
4878 break;
4879 case DW_TAG_common_inclusion:
4880 break;
4881 case DW_TAG_namespace:
4882 processing_has_namespace_info = 1;
4883 read_namespace (die, cu);
4884 break;
4885 case DW_TAG_module:
4886 processing_has_namespace_info = 1;
4887 read_module (die, cu);
4888 break;
4889 case DW_TAG_imported_declaration:
4890 case DW_TAG_imported_module:
4891 processing_has_namespace_info = 1;
4892 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4893 || cu->language != language_fortran))
4894 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4895 dwarf_tag_name (die->tag));
4896 read_import_statement (die, cu);
4897 break;
4898 default:
4899 new_symbol (die, NULL, cu);
4900 break;
4901 }
4902 }
4903
4904 /* A helper function for dwarf2_compute_name which determines whether DIE
4905 needs to have the name of the scope prepended to the name listed in the
4906 die. */
4907
4908 static int
4909 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4910 {
4911 struct attribute *attr;
4912
4913 switch (die->tag)
4914 {
4915 case DW_TAG_namespace:
4916 case DW_TAG_typedef:
4917 case DW_TAG_class_type:
4918 case DW_TAG_interface_type:
4919 case DW_TAG_structure_type:
4920 case DW_TAG_union_type:
4921 case DW_TAG_enumeration_type:
4922 case DW_TAG_enumerator:
4923 case DW_TAG_subprogram:
4924 case DW_TAG_member:
4925 return 1;
4926
4927 case DW_TAG_variable:
4928 case DW_TAG_constant:
4929 /* We only need to prefix "globally" visible variables. These include
4930 any variable marked with DW_AT_external or any variable that
4931 lives in a namespace. [Variables in anonymous namespaces
4932 require prefixing, but they are not DW_AT_external.] */
4933
4934 if (dwarf2_attr (die, DW_AT_specification, cu))
4935 {
4936 struct dwarf2_cu *spec_cu = cu;
4937
4938 return die_needs_namespace (die_specification (die, &spec_cu),
4939 spec_cu);
4940 }
4941
4942 attr = dwarf2_attr (die, DW_AT_external, cu);
4943 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4944 && die->parent->tag != DW_TAG_module)
4945 return 0;
4946 /* A variable in a lexical block of some kind does not need a
4947 namespace, even though in C++ such variables may be external
4948 and have a mangled name. */
4949 if (die->parent->tag == DW_TAG_lexical_block
4950 || die->parent->tag == DW_TAG_try_block
4951 || die->parent->tag == DW_TAG_catch_block
4952 || die->parent->tag == DW_TAG_subprogram)
4953 return 0;
4954 return 1;
4955
4956 default:
4957 return 0;
4958 }
4959 }
4960
4961 /* Retrieve the last character from a mem_file. */
4962
4963 static void
4964 do_ui_file_peek_last (void *object, const char *buffer, long length)
4965 {
4966 char *last_char_p = (char *) object;
4967
4968 if (length > 0)
4969 *last_char_p = buffer[length - 1];
4970 }
4971
4972 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4973 compute the physname for the object, which include a method's
4974 formal parameters (C++/Java) and return type (Java).
4975
4976 For Ada, return the DIE's linkage name rather than the fully qualified
4977 name. PHYSNAME is ignored..
4978
4979 The result is allocated on the objfile_obstack and canonicalized. */
4980
4981 static const char *
4982 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4983 int physname)
4984 {
4985 struct objfile *objfile = cu->objfile;
4986
4987 if (name == NULL)
4988 name = dwarf2_name (die, cu);
4989
4990 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4991 compute it by typename_concat inside GDB. */
4992 if (cu->language == language_ada
4993 || (cu->language == language_fortran && physname))
4994 {
4995 /* For Ada unit, we prefer the linkage name over the name, as
4996 the former contains the exported name, which the user expects
4997 to be able to reference. Ideally, we want the user to be able
4998 to reference this entity using either natural or linkage name,
4999 but we haven't started looking at this enhancement yet. */
5000 struct attribute *attr;
5001
5002 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5003 if (attr == NULL)
5004 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5005 if (attr && DW_STRING (attr))
5006 return DW_STRING (attr);
5007 }
5008
5009 /* These are the only languages we know how to qualify names in. */
5010 if (name != NULL
5011 && (cu->language == language_cplus || cu->language == language_java
5012 || cu->language == language_fortran))
5013 {
5014 if (die_needs_namespace (die, cu))
5015 {
5016 long length;
5017 char *prefix;
5018 struct ui_file *buf;
5019
5020 prefix = determine_prefix (die, cu);
5021 buf = mem_fileopen ();
5022 if (*prefix != '\0')
5023 {
5024 char *prefixed_name = typename_concat (NULL, prefix, name,
5025 physname, cu);
5026
5027 fputs_unfiltered (prefixed_name, buf);
5028 xfree (prefixed_name);
5029 }
5030 else
5031 fputs_unfiltered (name, buf);
5032
5033 /* Template parameters may be specified in the DIE's DW_AT_name, or
5034 as children with DW_TAG_template_type_param or
5035 DW_TAG_value_type_param. If the latter, add them to the name
5036 here. If the name already has template parameters, then
5037 skip this step; some versions of GCC emit both, and
5038 it is more efficient to use the pre-computed name.
5039
5040 Something to keep in mind about this process: it is very
5041 unlikely, or in some cases downright impossible, to produce
5042 something that will match the mangled name of a function.
5043 If the definition of the function has the same debug info,
5044 we should be able to match up with it anyway. But fallbacks
5045 using the minimal symbol, for instance to find a method
5046 implemented in a stripped copy of libstdc++, will not work.
5047 If we do not have debug info for the definition, we will have to
5048 match them up some other way.
5049
5050 When we do name matching there is a related problem with function
5051 templates; two instantiated function templates are allowed to
5052 differ only by their return types, which we do not add here. */
5053
5054 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5055 {
5056 struct attribute *attr;
5057 struct die_info *child;
5058 int first = 1;
5059
5060 die->building_fullname = 1;
5061
5062 for (child = die->child; child != NULL; child = child->sibling)
5063 {
5064 struct type *type;
5065 long value;
5066 gdb_byte *bytes;
5067 struct dwarf2_locexpr_baton *baton;
5068 struct value *v;
5069
5070 if (child->tag != DW_TAG_template_type_param
5071 && child->tag != DW_TAG_template_value_param)
5072 continue;
5073
5074 if (first)
5075 {
5076 fputs_unfiltered ("<", buf);
5077 first = 0;
5078 }
5079 else
5080 fputs_unfiltered (", ", buf);
5081
5082 attr = dwarf2_attr (child, DW_AT_type, cu);
5083 if (attr == NULL)
5084 {
5085 complaint (&symfile_complaints,
5086 _("template parameter missing DW_AT_type"));
5087 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5088 continue;
5089 }
5090 type = die_type (child, cu);
5091
5092 if (child->tag == DW_TAG_template_type_param)
5093 {
5094 c_print_type (type, "", buf, -1, 0);
5095 continue;
5096 }
5097
5098 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5099 if (attr == NULL)
5100 {
5101 complaint (&symfile_complaints,
5102 _("template parameter missing "
5103 "DW_AT_const_value"));
5104 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5105 continue;
5106 }
5107
5108 dwarf2_const_value_attr (attr, type, name,
5109 &cu->comp_unit_obstack, cu,
5110 &value, &bytes, &baton);
5111
5112 if (TYPE_NOSIGN (type))
5113 /* GDB prints characters as NUMBER 'CHAR'. If that's
5114 changed, this can use value_print instead. */
5115 c_printchar (value, type, buf);
5116 else
5117 {
5118 struct value_print_options opts;
5119
5120 if (baton != NULL)
5121 v = dwarf2_evaluate_loc_desc (type, NULL,
5122 baton->data,
5123 baton->size,
5124 baton->per_cu);
5125 else if (bytes != NULL)
5126 {
5127 v = allocate_value (type);
5128 memcpy (value_contents_writeable (v), bytes,
5129 TYPE_LENGTH (type));
5130 }
5131 else
5132 v = value_from_longest (type, value);
5133
5134 /* Specify decimal so that we do not depend on
5135 the radix. */
5136 get_formatted_print_options (&opts, 'd');
5137 opts.raw = 1;
5138 value_print (v, buf, &opts);
5139 release_value (v);
5140 value_free (v);
5141 }
5142 }
5143
5144 die->building_fullname = 0;
5145
5146 if (!first)
5147 {
5148 /* Close the argument list, with a space if necessary
5149 (nested templates). */
5150 char last_char = '\0';
5151 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5152 if (last_char == '>')
5153 fputs_unfiltered (" >", buf);
5154 else
5155 fputs_unfiltered (">", buf);
5156 }
5157 }
5158
5159 /* For Java and C++ methods, append formal parameter type
5160 information, if PHYSNAME. */
5161
5162 if (physname && die->tag == DW_TAG_subprogram
5163 && (cu->language == language_cplus
5164 || cu->language == language_java))
5165 {
5166 struct type *type = read_type_die (die, cu);
5167
5168 c_type_print_args (type, buf, 1, cu->language);
5169
5170 if (cu->language == language_java)
5171 {
5172 /* For java, we must append the return type to method
5173 names. */
5174 if (die->tag == DW_TAG_subprogram)
5175 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5176 0, 0);
5177 }
5178 else if (cu->language == language_cplus)
5179 {
5180 /* Assume that an artificial first parameter is
5181 "this", but do not crash if it is not. RealView
5182 marks unnamed (and thus unused) parameters as
5183 artificial; there is no way to differentiate
5184 the two cases. */
5185 if (TYPE_NFIELDS (type) > 0
5186 && TYPE_FIELD_ARTIFICIAL (type, 0)
5187 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5188 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5189 0))))
5190 fputs_unfiltered (" const", buf);
5191 }
5192 }
5193
5194 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5195 &length);
5196 ui_file_delete (buf);
5197
5198 if (cu->language == language_cplus)
5199 {
5200 char *cname
5201 = dwarf2_canonicalize_name (name, cu,
5202 &objfile->objfile_obstack);
5203
5204 if (cname != NULL)
5205 name = cname;
5206 }
5207 }
5208 }
5209
5210 return name;
5211 }
5212
5213 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5214 If scope qualifiers are appropriate they will be added. The result
5215 will be allocated on the objfile_obstack, or NULL if the DIE does
5216 not have a name. NAME may either be from a previous call to
5217 dwarf2_name or NULL.
5218
5219 The output string will be canonicalized (if C++/Java). */
5220
5221 static const char *
5222 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5223 {
5224 return dwarf2_compute_name (name, die, cu, 0);
5225 }
5226
5227 /* Construct a physname for the given DIE in CU. NAME may either be
5228 from a previous call to dwarf2_name or NULL. The result will be
5229 allocated on the objfile_objstack or NULL if the DIE does not have a
5230 name.
5231
5232 The output string will be canonicalized (if C++/Java). */
5233
5234 static const char *
5235 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5236 {
5237 struct objfile *objfile = cu->objfile;
5238 struct attribute *attr;
5239 const char *retval, *mangled = NULL, *canon = NULL;
5240 struct cleanup *back_to;
5241 int need_copy = 1;
5242
5243 /* In this case dwarf2_compute_name is just a shortcut not building anything
5244 on its own. */
5245 if (!die_needs_namespace (die, cu))
5246 return dwarf2_compute_name (name, die, cu, 1);
5247
5248 back_to = make_cleanup (null_cleanup, NULL);
5249
5250 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5251 if (!attr)
5252 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5253
5254 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5255 has computed. */
5256 if (attr && DW_STRING (attr))
5257 {
5258 char *demangled;
5259
5260 mangled = DW_STRING (attr);
5261
5262 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5263 type. It is easier for GDB users to search for such functions as
5264 `name(params)' than `long name(params)'. In such case the minimal
5265 symbol names do not match the full symbol names but for template
5266 functions there is never a need to look up their definition from their
5267 declaration so the only disadvantage remains the minimal symbol
5268 variant `long name(params)' does not have the proper inferior type.
5269 */
5270
5271 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5272 | (cu->language == language_java
5273 ? DMGL_JAVA | DMGL_RET_POSTFIX
5274 : DMGL_RET_DROP)));
5275 if (demangled)
5276 {
5277 make_cleanup (xfree, demangled);
5278 canon = demangled;
5279 }
5280 else
5281 {
5282 canon = mangled;
5283 need_copy = 0;
5284 }
5285 }
5286
5287 if (canon == NULL || check_physname)
5288 {
5289 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5290
5291 if (canon != NULL && strcmp (physname, canon) != 0)
5292 {
5293 /* It may not mean a bug in GDB. The compiler could also
5294 compute DW_AT_linkage_name incorrectly. But in such case
5295 GDB would need to be bug-to-bug compatible. */
5296
5297 complaint (&symfile_complaints,
5298 _("Computed physname <%s> does not match demangled <%s> "
5299 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5300 physname, canon, mangled, die->offset, objfile->name);
5301
5302 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5303 is available here - over computed PHYSNAME. It is safer
5304 against both buggy GDB and buggy compilers. */
5305
5306 retval = canon;
5307 }
5308 else
5309 {
5310 retval = physname;
5311 need_copy = 0;
5312 }
5313 }
5314 else
5315 retval = canon;
5316
5317 if (need_copy)
5318 retval = obsavestring (retval, strlen (retval),
5319 &objfile->objfile_obstack);
5320
5321 do_cleanups (back_to);
5322 return retval;
5323 }
5324
5325 /* Read the import statement specified by the given die and record it. */
5326
5327 static void
5328 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5329 {
5330 struct objfile *objfile = cu->objfile;
5331 struct attribute *import_attr;
5332 struct die_info *imported_die, *child_die;
5333 struct dwarf2_cu *imported_cu;
5334 const char *imported_name;
5335 const char *imported_name_prefix;
5336 const char *canonical_name;
5337 const char *import_alias;
5338 const char *imported_declaration = NULL;
5339 const char *import_prefix;
5340 VEC (const_char_ptr) *excludes = NULL;
5341 struct cleanup *cleanups;
5342
5343 char *temp;
5344
5345 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5346 if (import_attr == NULL)
5347 {
5348 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5349 dwarf_tag_name (die->tag));
5350 return;
5351 }
5352
5353 imported_cu = cu;
5354 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5355 imported_name = dwarf2_name (imported_die, imported_cu);
5356 if (imported_name == NULL)
5357 {
5358 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5359
5360 The import in the following code:
5361 namespace A
5362 {
5363 typedef int B;
5364 }
5365
5366 int main ()
5367 {
5368 using A::B;
5369 B b;
5370 return b;
5371 }
5372
5373 ...
5374 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5375 <52> DW_AT_decl_file : 1
5376 <53> DW_AT_decl_line : 6
5377 <54> DW_AT_import : <0x75>
5378 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5379 <59> DW_AT_name : B
5380 <5b> DW_AT_decl_file : 1
5381 <5c> DW_AT_decl_line : 2
5382 <5d> DW_AT_type : <0x6e>
5383 ...
5384 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5385 <76> DW_AT_byte_size : 4
5386 <77> DW_AT_encoding : 5 (signed)
5387
5388 imports the wrong die ( 0x75 instead of 0x58 ).
5389 This case will be ignored until the gcc bug is fixed. */
5390 return;
5391 }
5392
5393 /* Figure out the local name after import. */
5394 import_alias = dwarf2_name (die, cu);
5395
5396 /* Figure out where the statement is being imported to. */
5397 import_prefix = determine_prefix (die, cu);
5398
5399 /* Figure out what the scope of the imported die is and prepend it
5400 to the name of the imported die. */
5401 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5402
5403 if (imported_die->tag != DW_TAG_namespace
5404 && imported_die->tag != DW_TAG_module)
5405 {
5406 imported_declaration = imported_name;
5407 canonical_name = imported_name_prefix;
5408 }
5409 else if (strlen (imported_name_prefix) > 0)
5410 {
5411 temp = alloca (strlen (imported_name_prefix)
5412 + 2 + strlen (imported_name) + 1);
5413 strcpy (temp, imported_name_prefix);
5414 strcat (temp, "::");
5415 strcat (temp, imported_name);
5416 canonical_name = temp;
5417 }
5418 else
5419 canonical_name = imported_name;
5420
5421 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5422
5423 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5424 for (child_die = die->child; child_die && child_die->tag;
5425 child_die = sibling_die (child_die))
5426 {
5427 /* DWARF-4: A Fortran use statement with a “rename list” may be
5428 represented by an imported module entry with an import attribute
5429 referring to the module and owned entries corresponding to those
5430 entities that are renamed as part of being imported. */
5431
5432 if (child_die->tag != DW_TAG_imported_declaration)
5433 {
5434 complaint (&symfile_complaints,
5435 _("child DW_TAG_imported_declaration expected "
5436 "- DIE at 0x%x [in module %s]"),
5437 child_die->offset, objfile->name);
5438 continue;
5439 }
5440
5441 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5442 if (import_attr == NULL)
5443 {
5444 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5445 dwarf_tag_name (child_die->tag));
5446 continue;
5447 }
5448
5449 imported_cu = cu;
5450 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5451 &imported_cu);
5452 imported_name = dwarf2_name (imported_die, imported_cu);
5453 if (imported_name == NULL)
5454 {
5455 complaint (&symfile_complaints,
5456 _("child DW_TAG_imported_declaration has unknown "
5457 "imported name - DIE at 0x%x [in module %s]"),
5458 child_die->offset, objfile->name);
5459 continue;
5460 }
5461
5462 VEC_safe_push (const_char_ptr, excludes, imported_name);
5463
5464 process_die (child_die, cu);
5465 }
5466
5467 cp_add_using_directive (import_prefix,
5468 canonical_name,
5469 import_alias,
5470 imported_declaration,
5471 excludes,
5472 &objfile->objfile_obstack);
5473
5474 do_cleanups (cleanups);
5475 }
5476
5477 /* Cleanup function for read_file_scope. */
5478
5479 static void
5480 free_cu_line_header (void *arg)
5481 {
5482 struct dwarf2_cu *cu = arg;
5483
5484 free_line_header (cu->line_header);
5485 cu->line_header = NULL;
5486 }
5487
5488 static void
5489 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5490 char **name, char **comp_dir)
5491 {
5492 struct attribute *attr;
5493
5494 *name = NULL;
5495 *comp_dir = NULL;
5496
5497 /* Find the filename. Do not use dwarf2_name here, since the filename
5498 is not a source language identifier. */
5499 attr = dwarf2_attr (die, DW_AT_name, cu);
5500 if (attr)
5501 {
5502 *name = DW_STRING (attr);
5503 }
5504
5505 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5506 if (attr)
5507 *comp_dir = DW_STRING (attr);
5508 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5509 {
5510 *comp_dir = ldirname (*name);
5511 if (*comp_dir != NULL)
5512 make_cleanup (xfree, *comp_dir);
5513 }
5514 if (*comp_dir != NULL)
5515 {
5516 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5517 directory, get rid of it. */
5518 char *cp = strchr (*comp_dir, ':');
5519
5520 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5521 *comp_dir = cp + 1;
5522 }
5523
5524 if (*name == NULL)
5525 *name = "<unknown>";
5526 }
5527
5528 /* Handle DW_AT_stmt_list for a compilation unit. */
5529
5530 static void
5531 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5532 const char *comp_dir)
5533 {
5534 struct attribute *attr;
5535 struct objfile *objfile = cu->objfile;
5536 bfd *abfd = objfile->obfd;
5537
5538 /* Decode line number information if present. We do this before
5539 processing child DIEs, so that the line header table is available
5540 for DW_AT_decl_file. */
5541 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5542 if (attr)
5543 {
5544 unsigned int line_offset = DW_UNSND (attr);
5545 struct line_header *line_header
5546 = dwarf_decode_line_header (line_offset, abfd, cu);
5547
5548 if (line_header)
5549 {
5550 cu->line_header = line_header;
5551 make_cleanup (free_cu_line_header, cu);
5552 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5553 }
5554 }
5555 }
5556
5557 /* Process DW_TAG_compile_unit. */
5558
5559 static void
5560 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5561 {
5562 struct objfile *objfile = cu->objfile;
5563 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5564 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5565 CORE_ADDR highpc = ((CORE_ADDR) 0);
5566 struct attribute *attr;
5567 char *name = NULL;
5568 char *comp_dir = NULL;
5569 struct die_info *child_die;
5570 bfd *abfd = objfile->obfd;
5571 CORE_ADDR baseaddr;
5572
5573 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5574
5575 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5576
5577 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5578 from finish_block. */
5579 if (lowpc == ((CORE_ADDR) -1))
5580 lowpc = highpc;
5581 lowpc += baseaddr;
5582 highpc += baseaddr;
5583
5584 find_file_and_directory (die, cu, &name, &comp_dir);
5585
5586 attr = dwarf2_attr (die, DW_AT_language, cu);
5587 if (attr)
5588 {
5589 set_cu_language (DW_UNSND (attr), cu);
5590 }
5591
5592 attr = dwarf2_attr (die, DW_AT_producer, cu);
5593 if (attr)
5594 cu->producer = DW_STRING (attr);
5595
5596 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5597 standardised yet. As a workaround for the language detection we fall
5598 back to the DW_AT_producer string. */
5599 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5600 cu->language = language_opencl;
5601
5602 /* We assume that we're processing GCC output. */
5603 processing_gcc_compilation = 2;
5604
5605 processing_has_namespace_info = 0;
5606
5607 start_symtab (name, comp_dir, lowpc);
5608 record_debugformat ("DWARF 2");
5609 record_producer (cu->producer);
5610
5611 handle_DW_AT_stmt_list (die, cu, comp_dir);
5612
5613 /* Process all dies in compilation unit. */
5614 if (die->child != NULL)
5615 {
5616 child_die = die->child;
5617 while (child_die && child_die->tag)
5618 {
5619 process_die (child_die, cu);
5620 child_die = sibling_die (child_die);
5621 }
5622 }
5623
5624 /* Decode macro information, if present. Dwarf 2 macro information
5625 refers to information in the line number info statement program
5626 header, so we can only read it if we've read the header
5627 successfully. */
5628 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5629 if (attr && cu->line_header)
5630 {
5631 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5632 complaint (&symfile_complaints,
5633 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5634
5635 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5636 comp_dir, abfd, cu,
5637 &dwarf2_per_objfile->macro, 1);
5638 }
5639 else
5640 {
5641 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5642 if (attr && cu->line_header)
5643 {
5644 unsigned int macro_offset = DW_UNSND (attr);
5645
5646 dwarf_decode_macros (cu->line_header, macro_offset,
5647 comp_dir, abfd, cu,
5648 &dwarf2_per_objfile->macinfo, 0);
5649 }
5650 }
5651
5652 do_cleanups (back_to);
5653 }
5654
5655 /* Process DW_TAG_type_unit.
5656 For TUs we want to skip the first top level sibling if it's not the
5657 actual type being defined by this TU. In this case the first top
5658 level sibling is there to provide context only. */
5659
5660 static void
5661 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5662 {
5663 struct objfile *objfile = cu->objfile;
5664 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5665 CORE_ADDR lowpc;
5666 struct attribute *attr;
5667 char *name = NULL;
5668 char *comp_dir = NULL;
5669 struct die_info *child_die;
5670 bfd *abfd = objfile->obfd;
5671
5672 /* start_symtab needs a low pc, but we don't really have one.
5673 Do what read_file_scope would do in the absence of such info. */
5674 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5675
5676 /* Find the filename. Do not use dwarf2_name here, since the filename
5677 is not a source language identifier. */
5678 attr = dwarf2_attr (die, DW_AT_name, cu);
5679 if (attr)
5680 name = DW_STRING (attr);
5681
5682 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5683 if (attr)
5684 comp_dir = DW_STRING (attr);
5685 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5686 {
5687 comp_dir = ldirname (name);
5688 if (comp_dir != NULL)
5689 make_cleanup (xfree, comp_dir);
5690 }
5691
5692 if (name == NULL)
5693 name = "<unknown>";
5694
5695 attr = dwarf2_attr (die, DW_AT_language, cu);
5696 if (attr)
5697 set_cu_language (DW_UNSND (attr), cu);
5698
5699 /* This isn't technically needed today. It is done for symmetry
5700 with read_file_scope. */
5701 attr = dwarf2_attr (die, DW_AT_producer, cu);
5702 if (attr)
5703 cu->producer = DW_STRING (attr);
5704
5705 /* We assume that we're processing GCC output. */
5706 processing_gcc_compilation = 2;
5707
5708 processing_has_namespace_info = 0;
5709
5710 start_symtab (name, comp_dir, lowpc);
5711 record_debugformat ("DWARF 2");
5712 record_producer (cu->producer);
5713
5714 handle_DW_AT_stmt_list (die, cu, comp_dir);
5715
5716 /* Process the dies in the type unit. */
5717 if (die->child == NULL)
5718 {
5719 dump_die_for_error (die);
5720 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5721 bfd_get_filename (abfd));
5722 }
5723
5724 child_die = die->child;
5725
5726 while (child_die && child_die->tag)
5727 {
5728 process_die (child_die, cu);
5729
5730 child_die = sibling_die (child_die);
5731 }
5732
5733 do_cleanups (back_to);
5734 }
5735
5736 /* qsort helper for inherit_abstract_dies. */
5737
5738 static int
5739 unsigned_int_compar (const void *ap, const void *bp)
5740 {
5741 unsigned int a = *(unsigned int *) ap;
5742 unsigned int b = *(unsigned int *) bp;
5743
5744 return (a > b) - (b > a);
5745 }
5746
5747 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5748 Inherit only the children of the DW_AT_abstract_origin DIE not being
5749 already referenced by DW_AT_abstract_origin from the children of the
5750 current DIE. */
5751
5752 static void
5753 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5754 {
5755 struct die_info *child_die;
5756 unsigned die_children_count;
5757 /* CU offsets which were referenced by children of the current DIE. */
5758 unsigned *offsets;
5759 unsigned *offsets_end, *offsetp;
5760 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5761 struct die_info *origin_die;
5762 /* Iterator of the ORIGIN_DIE children. */
5763 struct die_info *origin_child_die;
5764 struct cleanup *cleanups;
5765 struct attribute *attr;
5766 struct dwarf2_cu *origin_cu;
5767 struct pending **origin_previous_list_in_scope;
5768
5769 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5770 if (!attr)
5771 return;
5772
5773 /* Note that following die references may follow to a die in a
5774 different cu. */
5775
5776 origin_cu = cu;
5777 origin_die = follow_die_ref (die, attr, &origin_cu);
5778
5779 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5780 symbols in. */
5781 origin_previous_list_in_scope = origin_cu->list_in_scope;
5782 origin_cu->list_in_scope = cu->list_in_scope;
5783
5784 if (die->tag != origin_die->tag
5785 && !(die->tag == DW_TAG_inlined_subroutine
5786 && origin_die->tag == DW_TAG_subprogram))
5787 complaint (&symfile_complaints,
5788 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5789 die->offset, origin_die->offset);
5790
5791 child_die = die->child;
5792 die_children_count = 0;
5793 while (child_die && child_die->tag)
5794 {
5795 child_die = sibling_die (child_die);
5796 die_children_count++;
5797 }
5798 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5799 cleanups = make_cleanup (xfree, offsets);
5800
5801 offsets_end = offsets;
5802 child_die = die->child;
5803 while (child_die && child_die->tag)
5804 {
5805 /* For each CHILD_DIE, find the corresponding child of
5806 ORIGIN_DIE. If there is more than one layer of
5807 DW_AT_abstract_origin, follow them all; there shouldn't be,
5808 but GCC versions at least through 4.4 generate this (GCC PR
5809 40573). */
5810 struct die_info *child_origin_die = child_die;
5811 struct dwarf2_cu *child_origin_cu = cu;
5812
5813 while (1)
5814 {
5815 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5816 child_origin_cu);
5817 if (attr == NULL)
5818 break;
5819 child_origin_die = follow_die_ref (child_origin_die, attr,
5820 &child_origin_cu);
5821 }
5822
5823 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5824 counterpart may exist. */
5825 if (child_origin_die != child_die)
5826 {
5827 if (child_die->tag != child_origin_die->tag
5828 && !(child_die->tag == DW_TAG_inlined_subroutine
5829 && child_origin_die->tag == DW_TAG_subprogram))
5830 complaint (&symfile_complaints,
5831 _("Child DIE 0x%x and its abstract origin 0x%x have "
5832 "different tags"), child_die->offset,
5833 child_origin_die->offset);
5834 if (child_origin_die->parent != origin_die)
5835 complaint (&symfile_complaints,
5836 _("Child DIE 0x%x and its abstract origin 0x%x have "
5837 "different parents"), child_die->offset,
5838 child_origin_die->offset);
5839 else
5840 *offsets_end++ = child_origin_die->offset;
5841 }
5842 child_die = sibling_die (child_die);
5843 }
5844 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5845 unsigned_int_compar);
5846 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5847 if (offsetp[-1] == *offsetp)
5848 complaint (&symfile_complaints,
5849 _("Multiple children of DIE 0x%x refer "
5850 "to DIE 0x%x as their abstract origin"),
5851 die->offset, *offsetp);
5852
5853 offsetp = offsets;
5854 origin_child_die = origin_die->child;
5855 while (origin_child_die && origin_child_die->tag)
5856 {
5857 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5858 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5859 offsetp++;
5860 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5861 {
5862 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5863 process_die (origin_child_die, origin_cu);
5864 }
5865 origin_child_die = sibling_die (origin_child_die);
5866 }
5867 origin_cu->list_in_scope = origin_previous_list_in_scope;
5868
5869 do_cleanups (cleanups);
5870 }
5871
5872 static void
5873 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5874 {
5875 struct objfile *objfile = cu->objfile;
5876 struct context_stack *new;
5877 CORE_ADDR lowpc;
5878 CORE_ADDR highpc;
5879 struct die_info *child_die;
5880 struct attribute *attr, *call_line, *call_file;
5881 char *name;
5882 CORE_ADDR baseaddr;
5883 struct block *block;
5884 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5885 VEC (symbolp) *template_args = NULL;
5886 struct template_symbol *templ_func = NULL;
5887
5888 if (inlined_func)
5889 {
5890 /* If we do not have call site information, we can't show the
5891 caller of this inlined function. That's too confusing, so
5892 only use the scope for local variables. */
5893 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5894 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5895 if (call_line == NULL || call_file == NULL)
5896 {
5897 read_lexical_block_scope (die, cu);
5898 return;
5899 }
5900 }
5901
5902 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5903
5904 name = dwarf2_name (die, cu);
5905
5906 /* Ignore functions with missing or empty names. These are actually
5907 illegal according to the DWARF standard. */
5908 if (name == NULL)
5909 {
5910 complaint (&symfile_complaints,
5911 _("missing name for subprogram DIE at %d"), die->offset);
5912 return;
5913 }
5914
5915 /* Ignore functions with missing or invalid low and high pc attributes. */
5916 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5917 {
5918 attr = dwarf2_attr (die, DW_AT_external, cu);
5919 if (!attr || !DW_UNSND (attr))
5920 complaint (&symfile_complaints,
5921 _("cannot get low and high bounds "
5922 "for subprogram DIE at %d"),
5923 die->offset);
5924 return;
5925 }
5926
5927 lowpc += baseaddr;
5928 highpc += baseaddr;
5929
5930 /* If we have any template arguments, then we must allocate a
5931 different sort of symbol. */
5932 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5933 {
5934 if (child_die->tag == DW_TAG_template_type_param
5935 || child_die->tag == DW_TAG_template_value_param)
5936 {
5937 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5938 struct template_symbol);
5939 templ_func->base.is_cplus_template_function = 1;
5940 break;
5941 }
5942 }
5943
5944 new = push_context (0, lowpc);
5945 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5946 (struct symbol *) templ_func);
5947
5948 /* If there is a location expression for DW_AT_frame_base, record
5949 it. */
5950 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5951 if (attr)
5952 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5953 expression is being recorded directly in the function's symbol
5954 and not in a separate frame-base object. I guess this hack is
5955 to avoid adding some sort of frame-base adjunct/annex to the
5956 function's symbol :-(. The problem with doing this is that it
5957 results in a function symbol with a location expression that
5958 has nothing to do with the location of the function, ouch! The
5959 relationship should be: a function's symbol has-a frame base; a
5960 frame-base has-a location expression. */
5961 dwarf2_symbol_mark_computed (attr, new->name, cu);
5962
5963 cu->list_in_scope = &local_symbols;
5964
5965 if (die->child != NULL)
5966 {
5967 child_die = die->child;
5968 while (child_die && child_die->tag)
5969 {
5970 if (child_die->tag == DW_TAG_template_type_param
5971 || child_die->tag == DW_TAG_template_value_param)
5972 {
5973 struct symbol *arg = new_symbol (child_die, NULL, cu);
5974
5975 if (arg != NULL)
5976 VEC_safe_push (symbolp, template_args, arg);
5977 }
5978 else
5979 process_die (child_die, cu);
5980 child_die = sibling_die (child_die);
5981 }
5982 }
5983
5984 inherit_abstract_dies (die, cu);
5985
5986 /* If we have a DW_AT_specification, we might need to import using
5987 directives from the context of the specification DIE. See the
5988 comment in determine_prefix. */
5989 if (cu->language == language_cplus
5990 && dwarf2_attr (die, DW_AT_specification, cu))
5991 {
5992 struct dwarf2_cu *spec_cu = cu;
5993 struct die_info *spec_die = die_specification (die, &spec_cu);
5994
5995 while (spec_die)
5996 {
5997 child_die = spec_die->child;
5998 while (child_die && child_die->tag)
5999 {
6000 if (child_die->tag == DW_TAG_imported_module)
6001 process_die (child_die, spec_cu);
6002 child_die = sibling_die (child_die);
6003 }
6004
6005 /* In some cases, GCC generates specification DIEs that
6006 themselves contain DW_AT_specification attributes. */
6007 spec_die = die_specification (spec_die, &spec_cu);
6008 }
6009 }
6010
6011 new = pop_context ();
6012 /* Make a block for the local symbols within. */
6013 block = finish_block (new->name, &local_symbols, new->old_blocks,
6014 lowpc, highpc, objfile);
6015
6016 /* For C++, set the block's scope. */
6017 if (cu->language == language_cplus || cu->language == language_fortran)
6018 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6019 determine_prefix (die, cu),
6020 processing_has_namespace_info);
6021
6022 /* If we have address ranges, record them. */
6023 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6024
6025 /* Attach template arguments to function. */
6026 if (! VEC_empty (symbolp, template_args))
6027 {
6028 gdb_assert (templ_func != NULL);
6029
6030 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6031 templ_func->template_arguments
6032 = obstack_alloc (&objfile->objfile_obstack,
6033 (templ_func->n_template_arguments
6034 * sizeof (struct symbol *)));
6035 memcpy (templ_func->template_arguments,
6036 VEC_address (symbolp, template_args),
6037 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6038 VEC_free (symbolp, template_args);
6039 }
6040
6041 /* In C++, we can have functions nested inside functions (e.g., when
6042 a function declares a class that has methods). This means that
6043 when we finish processing a function scope, we may need to go
6044 back to building a containing block's symbol lists. */
6045 local_symbols = new->locals;
6046 param_symbols = new->params;
6047 using_directives = new->using_directives;
6048
6049 /* If we've finished processing a top-level function, subsequent
6050 symbols go in the file symbol list. */
6051 if (outermost_context_p ())
6052 cu->list_in_scope = &file_symbols;
6053 }
6054
6055 /* Process all the DIES contained within a lexical block scope. Start
6056 a new scope, process the dies, and then close the scope. */
6057
6058 static void
6059 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6060 {
6061 struct objfile *objfile = cu->objfile;
6062 struct context_stack *new;
6063 CORE_ADDR lowpc, highpc;
6064 struct die_info *child_die;
6065 CORE_ADDR baseaddr;
6066
6067 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6068
6069 /* Ignore blocks with missing or invalid low and high pc attributes. */
6070 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6071 as multiple lexical blocks? Handling children in a sane way would
6072 be nasty. Might be easier to properly extend generic blocks to
6073 describe ranges. */
6074 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6075 return;
6076 lowpc += baseaddr;
6077 highpc += baseaddr;
6078
6079 push_context (0, lowpc);
6080 if (die->child != NULL)
6081 {
6082 child_die = die->child;
6083 while (child_die && child_die->tag)
6084 {
6085 process_die (child_die, cu);
6086 child_die = sibling_die (child_die);
6087 }
6088 }
6089 new = pop_context ();
6090
6091 if (local_symbols != NULL || using_directives != NULL)
6092 {
6093 struct block *block
6094 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6095 highpc, objfile);
6096
6097 /* Note that recording ranges after traversing children, as we
6098 do here, means that recording a parent's ranges entails
6099 walking across all its children's ranges as they appear in
6100 the address map, which is quadratic behavior.
6101
6102 It would be nicer to record the parent's ranges before
6103 traversing its children, simply overriding whatever you find
6104 there. But since we don't even decide whether to create a
6105 block until after we've traversed its children, that's hard
6106 to do. */
6107 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6108 }
6109 local_symbols = new->locals;
6110 using_directives = new->using_directives;
6111 }
6112
6113 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6114
6115 static void
6116 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6117 {
6118 struct objfile *objfile = cu->objfile;
6119 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6120 CORE_ADDR pc, baseaddr;
6121 struct attribute *attr;
6122 struct call_site *call_site, call_site_local;
6123 void **slot;
6124 int nparams;
6125 struct die_info *child_die;
6126
6127 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6128
6129 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6130 if (!attr)
6131 {
6132 complaint (&symfile_complaints,
6133 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6134 "DIE 0x%x [in module %s]"),
6135 die->offset, objfile->name);
6136 return;
6137 }
6138 pc = DW_ADDR (attr) + baseaddr;
6139
6140 if (cu->call_site_htab == NULL)
6141 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6142 NULL, &objfile->objfile_obstack,
6143 hashtab_obstack_allocate, NULL);
6144 call_site_local.pc = pc;
6145 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6146 if (*slot != NULL)
6147 {
6148 complaint (&symfile_complaints,
6149 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6150 "DIE 0x%x [in module %s]"),
6151 paddress (gdbarch, pc), die->offset, objfile->name);
6152 return;
6153 }
6154
6155 /* Count parameters at the caller. */
6156
6157 nparams = 0;
6158 for (child_die = die->child; child_die && child_die->tag;
6159 child_die = sibling_die (child_die))
6160 {
6161 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6162 {
6163 complaint (&symfile_complaints,
6164 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6165 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6166 child_die->tag, child_die->offset, objfile->name);
6167 continue;
6168 }
6169
6170 nparams++;
6171 }
6172
6173 call_site = obstack_alloc (&objfile->objfile_obstack,
6174 (sizeof (*call_site)
6175 + (sizeof (*call_site->parameter)
6176 * (nparams - 1))));
6177 *slot = call_site;
6178 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6179 call_site->pc = pc;
6180
6181 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6182 {
6183 struct die_info *func_die;
6184
6185 /* Skip also over DW_TAG_inlined_subroutine. */
6186 for (func_die = die->parent;
6187 func_die && func_die->tag != DW_TAG_subprogram
6188 && func_die->tag != DW_TAG_subroutine_type;
6189 func_die = func_die->parent);
6190
6191 /* DW_AT_GNU_all_call_sites is a superset
6192 of DW_AT_GNU_all_tail_call_sites. */
6193 if (func_die
6194 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6195 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6196 {
6197 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6198 not complete. But keep CALL_SITE for look ups via call_site_htab,
6199 both the initial caller containing the real return address PC and
6200 the final callee containing the current PC of a chain of tail
6201 calls do not need to have the tail call list complete. But any
6202 function candidate for a virtual tail call frame searched via
6203 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6204 determined unambiguously. */
6205 }
6206 else
6207 {
6208 struct type *func_type = NULL;
6209
6210 if (func_die)
6211 func_type = get_die_type (func_die, cu);
6212 if (func_type != NULL)
6213 {
6214 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6215
6216 /* Enlist this call site to the function. */
6217 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6218 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6219 }
6220 else
6221 complaint (&symfile_complaints,
6222 _("Cannot find function owning DW_TAG_GNU_call_site "
6223 "DIE 0x%x [in module %s]"),
6224 die->offset, objfile->name);
6225 }
6226 }
6227
6228 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6229 if (attr == NULL)
6230 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6231 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6232 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6233 /* Keep NULL DWARF_BLOCK. */;
6234 else if (attr_form_is_block (attr))
6235 {
6236 struct dwarf2_locexpr_baton *dlbaton;
6237
6238 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6239 dlbaton->data = DW_BLOCK (attr)->data;
6240 dlbaton->size = DW_BLOCK (attr)->size;
6241 dlbaton->per_cu = cu->per_cu;
6242
6243 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6244 }
6245 else if (is_ref_attr (attr))
6246 {
6247 struct dwarf2_cu *target_cu = cu;
6248 struct die_info *target_die;
6249
6250 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6251 gdb_assert (target_cu->objfile == objfile);
6252 if (die_is_declaration (target_die, target_cu))
6253 {
6254 const char *target_physname;
6255
6256 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6257 if (target_physname == NULL)
6258 complaint (&symfile_complaints,
6259 _("DW_AT_GNU_call_site_target target DIE has invalid "
6260 "physname, for referencing DIE 0x%x [in module %s]"),
6261 die->offset, objfile->name);
6262 else
6263 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6264 }
6265 else
6266 {
6267 CORE_ADDR lowpc;
6268
6269 /* DW_AT_entry_pc should be preferred. */
6270 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6271 complaint (&symfile_complaints,
6272 _("DW_AT_GNU_call_site_target target DIE has invalid "
6273 "low pc, for referencing DIE 0x%x [in module %s]"),
6274 die->offset, objfile->name);
6275 else
6276 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6277 }
6278 }
6279 else
6280 complaint (&symfile_complaints,
6281 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6282 "block nor reference, for DIE 0x%x [in module %s]"),
6283 die->offset, objfile->name);
6284
6285 call_site->per_cu = cu->per_cu;
6286
6287 for (child_die = die->child;
6288 child_die && child_die->tag;
6289 child_die = sibling_die (child_die))
6290 {
6291 struct dwarf2_locexpr_baton *dlbaton;
6292 struct call_site_parameter *parameter;
6293
6294 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6295 {
6296 /* Already printed the complaint above. */
6297 continue;
6298 }
6299
6300 gdb_assert (call_site->parameter_count < nparams);
6301 parameter = &call_site->parameter[call_site->parameter_count];
6302
6303 /* DW_AT_location specifies the register number. Value of the data
6304 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6305
6306 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6307 if (!attr || !attr_form_is_block (attr))
6308 {
6309 complaint (&symfile_complaints,
6310 _("No DW_FORM_block* DW_AT_location for "
6311 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6312 child_die->offset, objfile->name);
6313 continue;
6314 }
6315 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6316 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6317 if (parameter->dwarf_reg == -1
6318 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6319 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6320 &parameter->fb_offset))
6321 {
6322 complaint (&symfile_complaints,
6323 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6324 "for DW_FORM_block* DW_AT_location for "
6325 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6326 child_die->offset, objfile->name);
6327 continue;
6328 }
6329
6330 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6331 if (!attr_form_is_block (attr))
6332 {
6333 complaint (&symfile_complaints,
6334 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6335 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6336 child_die->offset, objfile->name);
6337 continue;
6338 }
6339 parameter->value = DW_BLOCK (attr)->data;
6340 parameter->value_size = DW_BLOCK (attr)->size;
6341
6342 /* Parameters are not pre-cleared by memset above. */
6343 parameter->data_value = NULL;
6344 parameter->data_value_size = 0;
6345 call_site->parameter_count++;
6346
6347 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6348 if (attr)
6349 {
6350 if (!attr_form_is_block (attr))
6351 complaint (&symfile_complaints,
6352 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6353 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6354 child_die->offset, objfile->name);
6355 else
6356 {
6357 parameter->data_value = DW_BLOCK (attr)->data;
6358 parameter->data_value_size = DW_BLOCK (attr)->size;
6359 }
6360 }
6361 }
6362 }
6363
6364 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6365 Return 1 if the attributes are present and valid, otherwise, return 0.
6366 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6367
6368 static int
6369 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6370 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6371 struct partial_symtab *ranges_pst)
6372 {
6373 struct objfile *objfile = cu->objfile;
6374 struct comp_unit_head *cu_header = &cu->header;
6375 bfd *obfd = objfile->obfd;
6376 unsigned int addr_size = cu_header->addr_size;
6377 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6378 /* Base address selection entry. */
6379 CORE_ADDR base;
6380 int found_base;
6381 unsigned int dummy;
6382 gdb_byte *buffer;
6383 CORE_ADDR marker;
6384 int low_set;
6385 CORE_ADDR low = 0;
6386 CORE_ADDR high = 0;
6387 CORE_ADDR baseaddr;
6388
6389 found_base = cu->base_known;
6390 base = cu->base_address;
6391
6392 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6393 if (offset >= dwarf2_per_objfile->ranges.size)
6394 {
6395 complaint (&symfile_complaints,
6396 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6397 offset);
6398 return 0;
6399 }
6400 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6401
6402 /* Read in the largest possible address. */
6403 marker = read_address (obfd, buffer, cu, &dummy);
6404 if ((marker & mask) == mask)
6405 {
6406 /* If we found the largest possible address, then
6407 read the base address. */
6408 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6409 buffer += 2 * addr_size;
6410 offset += 2 * addr_size;
6411 found_base = 1;
6412 }
6413
6414 low_set = 0;
6415
6416 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6417
6418 while (1)
6419 {
6420 CORE_ADDR range_beginning, range_end;
6421
6422 range_beginning = read_address (obfd, buffer, cu, &dummy);
6423 buffer += addr_size;
6424 range_end = read_address (obfd, buffer, cu, &dummy);
6425 buffer += addr_size;
6426 offset += 2 * addr_size;
6427
6428 /* An end of list marker is a pair of zero addresses. */
6429 if (range_beginning == 0 && range_end == 0)
6430 /* Found the end of list entry. */
6431 break;
6432
6433 /* Each base address selection entry is a pair of 2 values.
6434 The first is the largest possible address, the second is
6435 the base address. Check for a base address here. */
6436 if ((range_beginning & mask) == mask)
6437 {
6438 /* If we found the largest possible address, then
6439 read the base address. */
6440 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6441 found_base = 1;
6442 continue;
6443 }
6444
6445 if (!found_base)
6446 {
6447 /* We have no valid base address for the ranges
6448 data. */
6449 complaint (&symfile_complaints,
6450 _("Invalid .debug_ranges data (no base address)"));
6451 return 0;
6452 }
6453
6454 if (range_beginning > range_end)
6455 {
6456 /* Inverted range entries are invalid. */
6457 complaint (&symfile_complaints,
6458 _("Invalid .debug_ranges data (inverted range)"));
6459 return 0;
6460 }
6461
6462 /* Empty range entries have no effect. */
6463 if (range_beginning == range_end)
6464 continue;
6465
6466 range_beginning += base;
6467 range_end += base;
6468
6469 if (ranges_pst != NULL)
6470 addrmap_set_empty (objfile->psymtabs_addrmap,
6471 range_beginning + baseaddr,
6472 range_end - 1 + baseaddr,
6473 ranges_pst);
6474
6475 /* FIXME: This is recording everything as a low-high
6476 segment of consecutive addresses. We should have a
6477 data structure for discontiguous block ranges
6478 instead. */
6479 if (! low_set)
6480 {
6481 low = range_beginning;
6482 high = range_end;
6483 low_set = 1;
6484 }
6485 else
6486 {
6487 if (range_beginning < low)
6488 low = range_beginning;
6489 if (range_end > high)
6490 high = range_end;
6491 }
6492 }
6493
6494 if (! low_set)
6495 /* If the first entry is an end-of-list marker, the range
6496 describes an empty scope, i.e. no instructions. */
6497 return 0;
6498
6499 if (low_return)
6500 *low_return = low;
6501 if (high_return)
6502 *high_return = high;
6503 return 1;
6504 }
6505
6506 /* Get low and high pc attributes from a die. Return 1 if the attributes
6507 are present and valid, otherwise, return 0. Return -1 if the range is
6508 discontinuous, i.e. derived from DW_AT_ranges information. */
6509 static int
6510 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6511 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6512 struct partial_symtab *pst)
6513 {
6514 struct attribute *attr;
6515 CORE_ADDR low = 0;
6516 CORE_ADDR high = 0;
6517 int ret = 0;
6518
6519 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6520 if (attr)
6521 {
6522 high = DW_ADDR (attr);
6523 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6524 if (attr)
6525 low = DW_ADDR (attr);
6526 else
6527 /* Found high w/o low attribute. */
6528 return 0;
6529
6530 /* Found consecutive range of addresses. */
6531 ret = 1;
6532 }
6533 else
6534 {
6535 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6536 if (attr != NULL)
6537 {
6538 /* Value of the DW_AT_ranges attribute is the offset in the
6539 .debug_ranges section. */
6540 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6541 return 0;
6542 /* Found discontinuous range of addresses. */
6543 ret = -1;
6544 }
6545 }
6546
6547 /* read_partial_die has also the strict LOW < HIGH requirement. */
6548 if (high <= low)
6549 return 0;
6550
6551 /* When using the GNU linker, .gnu.linkonce. sections are used to
6552 eliminate duplicate copies of functions and vtables and such.
6553 The linker will arbitrarily choose one and discard the others.
6554 The AT_*_pc values for such functions refer to local labels in
6555 these sections. If the section from that file was discarded, the
6556 labels are not in the output, so the relocs get a value of 0.
6557 If this is a discarded function, mark the pc bounds as invalid,
6558 so that GDB will ignore it. */
6559 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6560 return 0;
6561
6562 *lowpc = low;
6563 if (highpc)
6564 *highpc = high;
6565 return ret;
6566 }
6567
6568 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6569 its low and high PC addresses. Do nothing if these addresses could not
6570 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6571 and HIGHPC to the high address if greater than HIGHPC. */
6572
6573 static void
6574 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6575 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6576 struct dwarf2_cu *cu)
6577 {
6578 CORE_ADDR low, high;
6579 struct die_info *child = die->child;
6580
6581 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6582 {
6583 *lowpc = min (*lowpc, low);
6584 *highpc = max (*highpc, high);
6585 }
6586
6587 /* If the language does not allow nested subprograms (either inside
6588 subprograms or lexical blocks), we're done. */
6589 if (cu->language != language_ada)
6590 return;
6591
6592 /* Check all the children of the given DIE. If it contains nested
6593 subprograms, then check their pc bounds. Likewise, we need to
6594 check lexical blocks as well, as they may also contain subprogram
6595 definitions. */
6596 while (child && child->tag)
6597 {
6598 if (child->tag == DW_TAG_subprogram
6599 || child->tag == DW_TAG_lexical_block)
6600 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6601 child = sibling_die (child);
6602 }
6603 }
6604
6605 /* Get the low and high pc's represented by the scope DIE, and store
6606 them in *LOWPC and *HIGHPC. If the correct values can't be
6607 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6608
6609 static void
6610 get_scope_pc_bounds (struct die_info *die,
6611 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6612 struct dwarf2_cu *cu)
6613 {
6614 CORE_ADDR best_low = (CORE_ADDR) -1;
6615 CORE_ADDR best_high = (CORE_ADDR) 0;
6616 CORE_ADDR current_low, current_high;
6617
6618 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6619 {
6620 best_low = current_low;
6621 best_high = current_high;
6622 }
6623 else
6624 {
6625 struct die_info *child = die->child;
6626
6627 while (child && child->tag)
6628 {
6629 switch (child->tag) {
6630 case DW_TAG_subprogram:
6631 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6632 break;
6633 case DW_TAG_namespace:
6634 case DW_TAG_module:
6635 /* FIXME: carlton/2004-01-16: Should we do this for
6636 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6637 that current GCC's always emit the DIEs corresponding
6638 to definitions of methods of classes as children of a
6639 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6640 the DIEs giving the declarations, which could be
6641 anywhere). But I don't see any reason why the
6642 standards says that they have to be there. */
6643 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6644
6645 if (current_low != ((CORE_ADDR) -1))
6646 {
6647 best_low = min (best_low, current_low);
6648 best_high = max (best_high, current_high);
6649 }
6650 break;
6651 default:
6652 /* Ignore. */
6653 break;
6654 }
6655
6656 child = sibling_die (child);
6657 }
6658 }
6659
6660 *lowpc = best_low;
6661 *highpc = best_high;
6662 }
6663
6664 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6665 in DIE. */
6666 static void
6667 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6668 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6669 {
6670 struct objfile *objfile = cu->objfile;
6671 struct attribute *attr;
6672
6673 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6674 if (attr)
6675 {
6676 CORE_ADDR high = DW_ADDR (attr);
6677
6678 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6679 if (attr)
6680 {
6681 CORE_ADDR low = DW_ADDR (attr);
6682
6683 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6684 }
6685 }
6686
6687 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6688 if (attr)
6689 {
6690 bfd *obfd = objfile->obfd;
6691
6692 /* The value of the DW_AT_ranges attribute is the offset of the
6693 address range list in the .debug_ranges section. */
6694 unsigned long offset = DW_UNSND (attr);
6695 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6696
6697 /* For some target architectures, but not others, the
6698 read_address function sign-extends the addresses it returns.
6699 To recognize base address selection entries, we need a
6700 mask. */
6701 unsigned int addr_size = cu->header.addr_size;
6702 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6703
6704 /* The base address, to which the next pair is relative. Note
6705 that this 'base' is a DWARF concept: most entries in a range
6706 list are relative, to reduce the number of relocs against the
6707 debugging information. This is separate from this function's
6708 'baseaddr' argument, which GDB uses to relocate debugging
6709 information from a shared library based on the address at
6710 which the library was loaded. */
6711 CORE_ADDR base = cu->base_address;
6712 int base_known = cu->base_known;
6713
6714 gdb_assert (dwarf2_per_objfile->ranges.readin);
6715 if (offset >= dwarf2_per_objfile->ranges.size)
6716 {
6717 complaint (&symfile_complaints,
6718 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6719 offset);
6720 return;
6721 }
6722
6723 for (;;)
6724 {
6725 unsigned int bytes_read;
6726 CORE_ADDR start, end;
6727
6728 start = read_address (obfd, buffer, cu, &bytes_read);
6729 buffer += bytes_read;
6730 end = read_address (obfd, buffer, cu, &bytes_read);
6731 buffer += bytes_read;
6732
6733 /* Did we find the end of the range list? */
6734 if (start == 0 && end == 0)
6735 break;
6736
6737 /* Did we find a base address selection entry? */
6738 else if ((start & base_select_mask) == base_select_mask)
6739 {
6740 base = end;
6741 base_known = 1;
6742 }
6743
6744 /* We found an ordinary address range. */
6745 else
6746 {
6747 if (!base_known)
6748 {
6749 complaint (&symfile_complaints,
6750 _("Invalid .debug_ranges data "
6751 "(no base address)"));
6752 return;
6753 }
6754
6755 if (start > end)
6756 {
6757 /* Inverted range entries are invalid. */
6758 complaint (&symfile_complaints,
6759 _("Invalid .debug_ranges data "
6760 "(inverted range)"));
6761 return;
6762 }
6763
6764 /* Empty range entries have no effect. */
6765 if (start == end)
6766 continue;
6767
6768 record_block_range (block,
6769 baseaddr + base + start,
6770 baseaddr + base + end - 1);
6771 }
6772 }
6773 }
6774 }
6775
6776 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6777 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6778 during 4.6.0 experimental. */
6779
6780 static int
6781 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6782 {
6783 const char *cs;
6784 int major, minor, release;
6785
6786 if (cu->producer == NULL)
6787 {
6788 /* For unknown compilers expect their behavior is DWARF version
6789 compliant.
6790
6791 GCC started to support .debug_types sections by -gdwarf-4 since
6792 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6793 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6794 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6795 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6796
6797 return 0;
6798 }
6799
6800 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6801
6802 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6803 {
6804 /* For non-GCC compilers expect their behavior is DWARF version
6805 compliant. */
6806
6807 return 0;
6808 }
6809 cs = &cu->producer[strlen ("GNU ")];
6810 while (*cs && !isdigit (*cs))
6811 cs++;
6812 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6813 {
6814 /* Not recognized as GCC. */
6815
6816 return 0;
6817 }
6818
6819 return major < 4 || (major == 4 && minor < 6);
6820 }
6821
6822 /* Return the default accessibility type if it is not overriden by
6823 DW_AT_accessibility. */
6824
6825 static enum dwarf_access_attribute
6826 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6827 {
6828 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6829 {
6830 /* The default DWARF 2 accessibility for members is public, the default
6831 accessibility for inheritance is private. */
6832
6833 if (die->tag != DW_TAG_inheritance)
6834 return DW_ACCESS_public;
6835 else
6836 return DW_ACCESS_private;
6837 }
6838 else
6839 {
6840 /* DWARF 3+ defines the default accessibility a different way. The same
6841 rules apply now for DW_TAG_inheritance as for the members and it only
6842 depends on the container kind. */
6843
6844 if (die->parent->tag == DW_TAG_class_type)
6845 return DW_ACCESS_private;
6846 else
6847 return DW_ACCESS_public;
6848 }
6849 }
6850
6851 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6852 offset. If the attribute was not found return 0, otherwise return
6853 1. If it was found but could not properly be handled, set *OFFSET
6854 to 0. */
6855
6856 static int
6857 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6858 LONGEST *offset)
6859 {
6860 struct attribute *attr;
6861
6862 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6863 if (attr != NULL)
6864 {
6865 *offset = 0;
6866
6867 /* Note that we do not check for a section offset first here.
6868 This is because DW_AT_data_member_location is new in DWARF 4,
6869 so if we see it, we can assume that a constant form is really
6870 a constant and not a section offset. */
6871 if (attr_form_is_constant (attr))
6872 *offset = dwarf2_get_attr_constant_value (attr, 0);
6873 else if (attr_form_is_section_offset (attr))
6874 dwarf2_complex_location_expr_complaint ();
6875 else if (attr_form_is_block (attr))
6876 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6877 else
6878 dwarf2_complex_location_expr_complaint ();
6879
6880 return 1;
6881 }
6882
6883 return 0;
6884 }
6885
6886 /* Add an aggregate field to the field list. */
6887
6888 static void
6889 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6890 struct dwarf2_cu *cu)
6891 {
6892 struct objfile *objfile = cu->objfile;
6893 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6894 struct nextfield *new_field;
6895 struct attribute *attr;
6896 struct field *fp;
6897 char *fieldname = "";
6898
6899 /* Allocate a new field list entry and link it in. */
6900 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6901 make_cleanup (xfree, new_field);
6902 memset (new_field, 0, sizeof (struct nextfield));
6903
6904 if (die->tag == DW_TAG_inheritance)
6905 {
6906 new_field->next = fip->baseclasses;
6907 fip->baseclasses = new_field;
6908 }
6909 else
6910 {
6911 new_field->next = fip->fields;
6912 fip->fields = new_field;
6913 }
6914 fip->nfields++;
6915
6916 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6917 if (attr)
6918 new_field->accessibility = DW_UNSND (attr);
6919 else
6920 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6921 if (new_field->accessibility != DW_ACCESS_public)
6922 fip->non_public_fields = 1;
6923
6924 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6925 if (attr)
6926 new_field->virtuality = DW_UNSND (attr);
6927 else
6928 new_field->virtuality = DW_VIRTUALITY_none;
6929
6930 fp = &new_field->field;
6931
6932 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6933 {
6934 LONGEST offset;
6935
6936 /* Data member other than a C++ static data member. */
6937
6938 /* Get type of field. */
6939 fp->type = die_type (die, cu);
6940
6941 SET_FIELD_BITPOS (*fp, 0);
6942
6943 /* Get bit size of field (zero if none). */
6944 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6945 if (attr)
6946 {
6947 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6948 }
6949 else
6950 {
6951 FIELD_BITSIZE (*fp) = 0;
6952 }
6953
6954 /* Get bit offset of field. */
6955 if (handle_data_member_location (die, cu, &offset))
6956 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6957 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6958 if (attr)
6959 {
6960 if (gdbarch_bits_big_endian (gdbarch))
6961 {
6962 /* For big endian bits, the DW_AT_bit_offset gives the
6963 additional bit offset from the MSB of the containing
6964 anonymous object to the MSB of the field. We don't
6965 have to do anything special since we don't need to
6966 know the size of the anonymous object. */
6967 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6968 }
6969 else
6970 {
6971 /* For little endian bits, compute the bit offset to the
6972 MSB of the anonymous object, subtract off the number of
6973 bits from the MSB of the field to the MSB of the
6974 object, and then subtract off the number of bits of
6975 the field itself. The result is the bit offset of
6976 the LSB of the field. */
6977 int anonymous_size;
6978 int bit_offset = DW_UNSND (attr);
6979
6980 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6981 if (attr)
6982 {
6983 /* The size of the anonymous object containing
6984 the bit field is explicit, so use the
6985 indicated size (in bytes). */
6986 anonymous_size = DW_UNSND (attr);
6987 }
6988 else
6989 {
6990 /* The size of the anonymous object containing
6991 the bit field must be inferred from the type
6992 attribute of the data member containing the
6993 bit field. */
6994 anonymous_size = TYPE_LENGTH (fp->type);
6995 }
6996 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6997 - bit_offset - FIELD_BITSIZE (*fp);
6998 }
6999 }
7000
7001 /* Get name of field. */
7002 fieldname = dwarf2_name (die, cu);
7003 if (fieldname == NULL)
7004 fieldname = "";
7005
7006 /* The name is already allocated along with this objfile, so we don't
7007 need to duplicate it for the type. */
7008 fp->name = fieldname;
7009
7010 /* Change accessibility for artificial fields (e.g. virtual table
7011 pointer or virtual base class pointer) to private. */
7012 if (dwarf2_attr (die, DW_AT_artificial, cu))
7013 {
7014 FIELD_ARTIFICIAL (*fp) = 1;
7015 new_field->accessibility = DW_ACCESS_private;
7016 fip->non_public_fields = 1;
7017 }
7018 }
7019 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7020 {
7021 /* C++ static member. */
7022
7023 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7024 is a declaration, but all versions of G++ as of this writing
7025 (so through at least 3.2.1) incorrectly generate
7026 DW_TAG_variable tags. */
7027
7028 const char *physname;
7029
7030 /* Get name of field. */
7031 fieldname = dwarf2_name (die, cu);
7032 if (fieldname == NULL)
7033 return;
7034
7035 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7036 if (attr
7037 /* Only create a symbol if this is an external value.
7038 new_symbol checks this and puts the value in the global symbol
7039 table, which we want. If it is not external, new_symbol
7040 will try to put the value in cu->list_in_scope which is wrong. */
7041 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7042 {
7043 /* A static const member, not much different than an enum as far as
7044 we're concerned, except that we can support more types. */
7045 new_symbol (die, NULL, cu);
7046 }
7047
7048 /* Get physical name. */
7049 physname = dwarf2_physname (fieldname, die, cu);
7050
7051 /* The name is already allocated along with this objfile, so we don't
7052 need to duplicate it for the type. */
7053 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7054 FIELD_TYPE (*fp) = die_type (die, cu);
7055 FIELD_NAME (*fp) = fieldname;
7056 }
7057 else if (die->tag == DW_TAG_inheritance)
7058 {
7059 LONGEST offset;
7060
7061 /* C++ base class field. */
7062 if (handle_data_member_location (die, cu, &offset))
7063 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7064 FIELD_BITSIZE (*fp) = 0;
7065 FIELD_TYPE (*fp) = die_type (die, cu);
7066 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7067 fip->nbaseclasses++;
7068 }
7069 }
7070
7071 /* Add a typedef defined in the scope of the FIP's class. */
7072
7073 static void
7074 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7075 struct dwarf2_cu *cu)
7076 {
7077 struct objfile *objfile = cu->objfile;
7078 struct typedef_field_list *new_field;
7079 struct attribute *attr;
7080 struct typedef_field *fp;
7081 char *fieldname = "";
7082
7083 /* Allocate a new field list entry and link it in. */
7084 new_field = xzalloc (sizeof (*new_field));
7085 make_cleanup (xfree, new_field);
7086
7087 gdb_assert (die->tag == DW_TAG_typedef);
7088
7089 fp = &new_field->field;
7090
7091 /* Get name of field. */
7092 fp->name = dwarf2_name (die, cu);
7093 if (fp->name == NULL)
7094 return;
7095
7096 fp->type = read_type_die (die, cu);
7097
7098 new_field->next = fip->typedef_field_list;
7099 fip->typedef_field_list = new_field;
7100 fip->typedef_field_list_count++;
7101 }
7102
7103 /* Create the vector of fields, and attach it to the type. */
7104
7105 static void
7106 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7107 struct dwarf2_cu *cu)
7108 {
7109 int nfields = fip->nfields;
7110
7111 /* Record the field count, allocate space for the array of fields,
7112 and create blank accessibility bitfields if necessary. */
7113 TYPE_NFIELDS (type) = nfields;
7114 TYPE_FIELDS (type) = (struct field *)
7115 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7116 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7117
7118 if (fip->non_public_fields && cu->language != language_ada)
7119 {
7120 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7121
7122 TYPE_FIELD_PRIVATE_BITS (type) =
7123 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7124 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7125
7126 TYPE_FIELD_PROTECTED_BITS (type) =
7127 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7128 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7129
7130 TYPE_FIELD_IGNORE_BITS (type) =
7131 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7132 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7133 }
7134
7135 /* If the type has baseclasses, allocate and clear a bit vector for
7136 TYPE_FIELD_VIRTUAL_BITS. */
7137 if (fip->nbaseclasses && cu->language != language_ada)
7138 {
7139 int num_bytes = B_BYTES (fip->nbaseclasses);
7140 unsigned char *pointer;
7141
7142 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7143 pointer = TYPE_ALLOC (type, num_bytes);
7144 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7145 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7146 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7147 }
7148
7149 /* Copy the saved-up fields into the field vector. Start from the head of
7150 the list, adding to the tail of the field array, so that they end up in
7151 the same order in the array in which they were added to the list. */
7152 while (nfields-- > 0)
7153 {
7154 struct nextfield *fieldp;
7155
7156 if (fip->fields)
7157 {
7158 fieldp = fip->fields;
7159 fip->fields = fieldp->next;
7160 }
7161 else
7162 {
7163 fieldp = fip->baseclasses;
7164 fip->baseclasses = fieldp->next;
7165 }
7166
7167 TYPE_FIELD (type, nfields) = fieldp->field;
7168 switch (fieldp->accessibility)
7169 {
7170 case DW_ACCESS_private:
7171 if (cu->language != language_ada)
7172 SET_TYPE_FIELD_PRIVATE (type, nfields);
7173 break;
7174
7175 case DW_ACCESS_protected:
7176 if (cu->language != language_ada)
7177 SET_TYPE_FIELD_PROTECTED (type, nfields);
7178 break;
7179
7180 case DW_ACCESS_public:
7181 break;
7182
7183 default:
7184 /* Unknown accessibility. Complain and treat it as public. */
7185 {
7186 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7187 fieldp->accessibility);
7188 }
7189 break;
7190 }
7191 if (nfields < fip->nbaseclasses)
7192 {
7193 switch (fieldp->virtuality)
7194 {
7195 case DW_VIRTUALITY_virtual:
7196 case DW_VIRTUALITY_pure_virtual:
7197 if (cu->language == language_ada)
7198 error (_("unexpected virtuality in component of Ada type"));
7199 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7200 break;
7201 }
7202 }
7203 }
7204 }
7205
7206 /* Add a member function to the proper fieldlist. */
7207
7208 static void
7209 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7210 struct type *type, struct dwarf2_cu *cu)
7211 {
7212 struct objfile *objfile = cu->objfile;
7213 struct attribute *attr;
7214 struct fnfieldlist *flp;
7215 int i;
7216 struct fn_field *fnp;
7217 char *fieldname;
7218 struct nextfnfield *new_fnfield;
7219 struct type *this_type;
7220 enum dwarf_access_attribute accessibility;
7221
7222 if (cu->language == language_ada)
7223 error (_("unexpected member function in Ada type"));
7224
7225 /* Get name of member function. */
7226 fieldname = dwarf2_name (die, cu);
7227 if (fieldname == NULL)
7228 return;
7229
7230 /* Look up member function name in fieldlist. */
7231 for (i = 0; i < fip->nfnfields; i++)
7232 {
7233 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7234 break;
7235 }
7236
7237 /* Create new list element if necessary. */
7238 if (i < fip->nfnfields)
7239 flp = &fip->fnfieldlists[i];
7240 else
7241 {
7242 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7243 {
7244 fip->fnfieldlists = (struct fnfieldlist *)
7245 xrealloc (fip->fnfieldlists,
7246 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7247 * sizeof (struct fnfieldlist));
7248 if (fip->nfnfields == 0)
7249 make_cleanup (free_current_contents, &fip->fnfieldlists);
7250 }
7251 flp = &fip->fnfieldlists[fip->nfnfields];
7252 flp->name = fieldname;
7253 flp->length = 0;
7254 flp->head = NULL;
7255 i = fip->nfnfields++;
7256 }
7257
7258 /* Create a new member function field and chain it to the field list
7259 entry. */
7260 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7261 make_cleanup (xfree, new_fnfield);
7262 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7263 new_fnfield->next = flp->head;
7264 flp->head = new_fnfield;
7265 flp->length++;
7266
7267 /* Fill in the member function field info. */
7268 fnp = &new_fnfield->fnfield;
7269
7270 /* Delay processing of the physname until later. */
7271 if (cu->language == language_cplus || cu->language == language_java)
7272 {
7273 add_to_method_list (type, i, flp->length - 1, fieldname,
7274 die, cu);
7275 }
7276 else
7277 {
7278 const char *physname = dwarf2_physname (fieldname, die, cu);
7279 fnp->physname = physname ? physname : "";
7280 }
7281
7282 fnp->type = alloc_type (objfile);
7283 this_type = read_type_die (die, cu);
7284 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7285 {
7286 int nparams = TYPE_NFIELDS (this_type);
7287
7288 /* TYPE is the domain of this method, and THIS_TYPE is the type
7289 of the method itself (TYPE_CODE_METHOD). */
7290 smash_to_method_type (fnp->type, type,
7291 TYPE_TARGET_TYPE (this_type),
7292 TYPE_FIELDS (this_type),
7293 TYPE_NFIELDS (this_type),
7294 TYPE_VARARGS (this_type));
7295
7296 /* Handle static member functions.
7297 Dwarf2 has no clean way to discern C++ static and non-static
7298 member functions. G++ helps GDB by marking the first
7299 parameter for non-static member functions (which is the this
7300 pointer) as artificial. We obtain this information from
7301 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7302 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7303 fnp->voffset = VOFFSET_STATIC;
7304 }
7305 else
7306 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7307 dwarf2_full_name (fieldname, die, cu));
7308
7309 /* Get fcontext from DW_AT_containing_type if present. */
7310 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7311 fnp->fcontext = die_containing_type (die, cu);
7312
7313 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7314 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7315
7316 /* Get accessibility. */
7317 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7318 if (attr)
7319 accessibility = DW_UNSND (attr);
7320 else
7321 accessibility = dwarf2_default_access_attribute (die, cu);
7322 switch (accessibility)
7323 {
7324 case DW_ACCESS_private:
7325 fnp->is_private = 1;
7326 break;
7327 case DW_ACCESS_protected:
7328 fnp->is_protected = 1;
7329 break;
7330 }
7331
7332 /* Check for artificial methods. */
7333 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7334 if (attr && DW_UNSND (attr) != 0)
7335 fnp->is_artificial = 1;
7336
7337 /* Get index in virtual function table if it is a virtual member
7338 function. For older versions of GCC, this is an offset in the
7339 appropriate virtual table, as specified by DW_AT_containing_type.
7340 For everyone else, it is an expression to be evaluated relative
7341 to the object address. */
7342
7343 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7344 if (attr)
7345 {
7346 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7347 {
7348 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7349 {
7350 /* Old-style GCC. */
7351 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7352 }
7353 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7354 || (DW_BLOCK (attr)->size > 1
7355 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7356 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7357 {
7358 struct dwarf_block blk;
7359 int offset;
7360
7361 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7362 ? 1 : 2);
7363 blk.size = DW_BLOCK (attr)->size - offset;
7364 blk.data = DW_BLOCK (attr)->data + offset;
7365 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7366 if ((fnp->voffset % cu->header.addr_size) != 0)
7367 dwarf2_complex_location_expr_complaint ();
7368 else
7369 fnp->voffset /= cu->header.addr_size;
7370 fnp->voffset += 2;
7371 }
7372 else
7373 dwarf2_complex_location_expr_complaint ();
7374
7375 if (!fnp->fcontext)
7376 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7377 }
7378 else if (attr_form_is_section_offset (attr))
7379 {
7380 dwarf2_complex_location_expr_complaint ();
7381 }
7382 else
7383 {
7384 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7385 fieldname);
7386 }
7387 }
7388 else
7389 {
7390 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7391 if (attr && DW_UNSND (attr))
7392 {
7393 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7394 complaint (&symfile_complaints,
7395 _("Member function \"%s\" (offset %d) is virtual "
7396 "but the vtable offset is not specified"),
7397 fieldname, die->offset);
7398 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7399 TYPE_CPLUS_DYNAMIC (type) = 1;
7400 }
7401 }
7402 }
7403
7404 /* Create the vector of member function fields, and attach it to the type. */
7405
7406 static void
7407 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7408 struct dwarf2_cu *cu)
7409 {
7410 struct fnfieldlist *flp;
7411 int i;
7412
7413 if (cu->language == language_ada)
7414 error (_("unexpected member functions in Ada type"));
7415
7416 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7417 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7418 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7419
7420 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7421 {
7422 struct nextfnfield *nfp = flp->head;
7423 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7424 int k;
7425
7426 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7427 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7428 fn_flp->fn_fields = (struct fn_field *)
7429 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7430 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7431 fn_flp->fn_fields[k] = nfp->fnfield;
7432 }
7433
7434 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7435 }
7436
7437 /* Returns non-zero if NAME is the name of a vtable member in CU's
7438 language, zero otherwise. */
7439 static int
7440 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7441 {
7442 static const char vptr[] = "_vptr";
7443 static const char vtable[] = "vtable";
7444
7445 /* Look for the C++ and Java forms of the vtable. */
7446 if ((cu->language == language_java
7447 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7448 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7449 && is_cplus_marker (name[sizeof (vptr) - 1])))
7450 return 1;
7451
7452 return 0;
7453 }
7454
7455 /* GCC outputs unnamed structures that are really pointers to member
7456 functions, with the ABI-specified layout. If TYPE describes
7457 such a structure, smash it into a member function type.
7458
7459 GCC shouldn't do this; it should just output pointer to member DIEs.
7460 This is GCC PR debug/28767. */
7461
7462 static void
7463 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7464 {
7465 struct type *pfn_type, *domain_type, *new_type;
7466
7467 /* Check for a structure with no name and two children. */
7468 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7469 return;
7470
7471 /* Check for __pfn and __delta members. */
7472 if (TYPE_FIELD_NAME (type, 0) == NULL
7473 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7474 || TYPE_FIELD_NAME (type, 1) == NULL
7475 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7476 return;
7477
7478 /* Find the type of the method. */
7479 pfn_type = TYPE_FIELD_TYPE (type, 0);
7480 if (pfn_type == NULL
7481 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7482 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7483 return;
7484
7485 /* Look for the "this" argument. */
7486 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7487 if (TYPE_NFIELDS (pfn_type) == 0
7488 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7489 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7490 return;
7491
7492 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7493 new_type = alloc_type (objfile);
7494 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7495 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7496 TYPE_VARARGS (pfn_type));
7497 smash_to_methodptr_type (type, new_type);
7498 }
7499
7500 /* Called when we find the DIE that starts a structure or union scope
7501 (definition) to create a type for the structure or union. Fill in
7502 the type's name and general properties; the members will not be
7503 processed until process_structure_type.
7504
7505 NOTE: we need to call these functions regardless of whether or not the
7506 DIE has a DW_AT_name attribute, since it might be an anonymous
7507 structure or union. This gets the type entered into our set of
7508 user defined types.
7509
7510 However, if the structure is incomplete (an opaque struct/union)
7511 then suppress creating a symbol table entry for it since gdb only
7512 wants to find the one with the complete definition. Note that if
7513 it is complete, we just call new_symbol, which does it's own
7514 checking about whether the struct/union is anonymous or not (and
7515 suppresses creating a symbol table entry itself). */
7516
7517 static struct type *
7518 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7519 {
7520 struct objfile *objfile = cu->objfile;
7521 struct type *type;
7522 struct attribute *attr;
7523 char *name;
7524
7525 /* If the definition of this type lives in .debug_types, read that type.
7526 Don't follow DW_AT_specification though, that will take us back up
7527 the chain and we want to go down. */
7528 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7529 if (attr)
7530 {
7531 struct dwarf2_cu *type_cu = cu;
7532 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7533
7534 /* We could just recurse on read_structure_type, but we need to call
7535 get_die_type to ensure only one type for this DIE is created.
7536 This is important, for example, because for c++ classes we need
7537 TYPE_NAME set which is only done by new_symbol. Blech. */
7538 type = read_type_die (type_die, type_cu);
7539
7540 /* TYPE_CU may not be the same as CU.
7541 Ensure TYPE is recorded in CU's type_hash table. */
7542 return set_die_type (die, type, cu);
7543 }
7544
7545 type = alloc_type (objfile);
7546 INIT_CPLUS_SPECIFIC (type);
7547
7548 name = dwarf2_name (die, cu);
7549 if (name != NULL)
7550 {
7551 if (cu->language == language_cplus
7552 || cu->language == language_java)
7553 {
7554 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7555
7556 /* dwarf2_full_name might have already finished building the DIE's
7557 type. If so, there is no need to continue. */
7558 if (get_die_type (die, cu) != NULL)
7559 return get_die_type (die, cu);
7560
7561 TYPE_TAG_NAME (type) = full_name;
7562 if (die->tag == DW_TAG_structure_type
7563 || die->tag == DW_TAG_class_type)
7564 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7565 }
7566 else
7567 {
7568 /* The name is already allocated along with this objfile, so
7569 we don't need to duplicate it for the type. */
7570 TYPE_TAG_NAME (type) = (char *) name;
7571 if (die->tag == DW_TAG_class_type)
7572 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7573 }
7574 }
7575
7576 if (die->tag == DW_TAG_structure_type)
7577 {
7578 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7579 }
7580 else if (die->tag == DW_TAG_union_type)
7581 {
7582 TYPE_CODE (type) = TYPE_CODE_UNION;
7583 }
7584 else
7585 {
7586 TYPE_CODE (type) = TYPE_CODE_CLASS;
7587 }
7588
7589 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7590 TYPE_DECLARED_CLASS (type) = 1;
7591
7592 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7593 if (attr)
7594 {
7595 TYPE_LENGTH (type) = DW_UNSND (attr);
7596 }
7597 else
7598 {
7599 TYPE_LENGTH (type) = 0;
7600 }
7601
7602 TYPE_STUB_SUPPORTED (type) = 1;
7603 if (die_is_declaration (die, cu))
7604 TYPE_STUB (type) = 1;
7605 else if (attr == NULL && die->child == NULL
7606 && producer_is_realview (cu->producer))
7607 /* RealView does not output the required DW_AT_declaration
7608 on incomplete types. */
7609 TYPE_STUB (type) = 1;
7610
7611 /* We need to add the type field to the die immediately so we don't
7612 infinitely recurse when dealing with pointers to the structure
7613 type within the structure itself. */
7614 set_die_type (die, type, cu);
7615
7616 /* set_die_type should be already done. */
7617 set_descriptive_type (type, die, cu);
7618
7619 return type;
7620 }
7621
7622 /* Finish creating a structure or union type, including filling in
7623 its members and creating a symbol for it. */
7624
7625 static void
7626 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7627 {
7628 struct objfile *objfile = cu->objfile;
7629 struct die_info *child_die = die->child;
7630 struct type *type;
7631
7632 type = get_die_type (die, cu);
7633 if (type == NULL)
7634 type = read_structure_type (die, cu);
7635
7636 if (die->child != NULL && ! die_is_declaration (die, cu))
7637 {
7638 struct field_info fi;
7639 struct die_info *child_die;
7640 VEC (symbolp) *template_args = NULL;
7641 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7642
7643 memset (&fi, 0, sizeof (struct field_info));
7644
7645 child_die = die->child;
7646
7647 while (child_die && child_die->tag)
7648 {
7649 if (child_die->tag == DW_TAG_member
7650 || child_die->tag == DW_TAG_variable)
7651 {
7652 /* NOTE: carlton/2002-11-05: A C++ static data member
7653 should be a DW_TAG_member that is a declaration, but
7654 all versions of G++ as of this writing (so through at
7655 least 3.2.1) incorrectly generate DW_TAG_variable
7656 tags for them instead. */
7657 dwarf2_add_field (&fi, child_die, cu);
7658 }
7659 else if (child_die->tag == DW_TAG_subprogram)
7660 {
7661 /* C++ member function. */
7662 dwarf2_add_member_fn (&fi, child_die, type, cu);
7663 }
7664 else if (child_die->tag == DW_TAG_inheritance)
7665 {
7666 /* C++ base class field. */
7667 dwarf2_add_field (&fi, child_die, cu);
7668 }
7669 else if (child_die->tag == DW_TAG_typedef)
7670 dwarf2_add_typedef (&fi, child_die, cu);
7671 else if (child_die->tag == DW_TAG_template_type_param
7672 || child_die->tag == DW_TAG_template_value_param)
7673 {
7674 struct symbol *arg = new_symbol (child_die, NULL, cu);
7675
7676 if (arg != NULL)
7677 VEC_safe_push (symbolp, template_args, arg);
7678 }
7679
7680 child_die = sibling_die (child_die);
7681 }
7682
7683 /* Attach template arguments to type. */
7684 if (! VEC_empty (symbolp, template_args))
7685 {
7686 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7687 TYPE_N_TEMPLATE_ARGUMENTS (type)
7688 = VEC_length (symbolp, template_args);
7689 TYPE_TEMPLATE_ARGUMENTS (type)
7690 = obstack_alloc (&objfile->objfile_obstack,
7691 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7692 * sizeof (struct symbol *)));
7693 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7694 VEC_address (symbolp, template_args),
7695 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7696 * sizeof (struct symbol *)));
7697 VEC_free (symbolp, template_args);
7698 }
7699
7700 /* Attach fields and member functions to the type. */
7701 if (fi.nfields)
7702 dwarf2_attach_fields_to_type (&fi, type, cu);
7703 if (fi.nfnfields)
7704 {
7705 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7706
7707 /* Get the type which refers to the base class (possibly this
7708 class itself) which contains the vtable pointer for the current
7709 class from the DW_AT_containing_type attribute. This use of
7710 DW_AT_containing_type is a GNU extension. */
7711
7712 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7713 {
7714 struct type *t = die_containing_type (die, cu);
7715
7716 TYPE_VPTR_BASETYPE (type) = t;
7717 if (type == t)
7718 {
7719 int i;
7720
7721 /* Our own class provides vtbl ptr. */
7722 for (i = TYPE_NFIELDS (t) - 1;
7723 i >= TYPE_N_BASECLASSES (t);
7724 --i)
7725 {
7726 char *fieldname = TYPE_FIELD_NAME (t, i);
7727
7728 if (is_vtable_name (fieldname, cu))
7729 {
7730 TYPE_VPTR_FIELDNO (type) = i;
7731 break;
7732 }
7733 }
7734
7735 /* Complain if virtual function table field not found. */
7736 if (i < TYPE_N_BASECLASSES (t))
7737 complaint (&symfile_complaints,
7738 _("virtual function table pointer "
7739 "not found when defining class '%s'"),
7740 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7741 "");
7742 }
7743 else
7744 {
7745 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7746 }
7747 }
7748 else if (cu->producer
7749 && strncmp (cu->producer,
7750 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7751 {
7752 /* The IBM XLC compiler does not provide direct indication
7753 of the containing type, but the vtable pointer is
7754 always named __vfp. */
7755
7756 int i;
7757
7758 for (i = TYPE_NFIELDS (type) - 1;
7759 i >= TYPE_N_BASECLASSES (type);
7760 --i)
7761 {
7762 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7763 {
7764 TYPE_VPTR_FIELDNO (type) = i;
7765 TYPE_VPTR_BASETYPE (type) = type;
7766 break;
7767 }
7768 }
7769 }
7770 }
7771
7772 /* Copy fi.typedef_field_list linked list elements content into the
7773 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7774 if (fi.typedef_field_list)
7775 {
7776 int i = fi.typedef_field_list_count;
7777
7778 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7779 TYPE_TYPEDEF_FIELD_ARRAY (type)
7780 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7781 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7782
7783 /* Reverse the list order to keep the debug info elements order. */
7784 while (--i >= 0)
7785 {
7786 struct typedef_field *dest, *src;
7787
7788 dest = &TYPE_TYPEDEF_FIELD (type, i);
7789 src = &fi.typedef_field_list->field;
7790 fi.typedef_field_list = fi.typedef_field_list->next;
7791 *dest = *src;
7792 }
7793 }
7794
7795 do_cleanups (back_to);
7796
7797 if (HAVE_CPLUS_STRUCT (type))
7798 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7799 }
7800
7801 quirk_gcc_member_function_pointer (type, objfile);
7802
7803 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7804 snapshots) has been known to create a die giving a declaration
7805 for a class that has, as a child, a die giving a definition for a
7806 nested class. So we have to process our children even if the
7807 current die is a declaration. Normally, of course, a declaration
7808 won't have any children at all. */
7809
7810 while (child_die != NULL && child_die->tag)
7811 {
7812 if (child_die->tag == DW_TAG_member
7813 || child_die->tag == DW_TAG_variable
7814 || child_die->tag == DW_TAG_inheritance
7815 || child_die->tag == DW_TAG_template_value_param
7816 || child_die->tag == DW_TAG_template_type_param)
7817 {
7818 /* Do nothing. */
7819 }
7820 else
7821 process_die (child_die, cu);
7822
7823 child_die = sibling_die (child_die);
7824 }
7825
7826 /* Do not consider external references. According to the DWARF standard,
7827 these DIEs are identified by the fact that they have no byte_size
7828 attribute, and a declaration attribute. */
7829 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7830 || !die_is_declaration (die, cu))
7831 new_symbol (die, type, cu);
7832 }
7833
7834 /* Given a DW_AT_enumeration_type die, set its type. We do not
7835 complete the type's fields yet, or create any symbols. */
7836
7837 static struct type *
7838 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7839 {
7840 struct objfile *objfile = cu->objfile;
7841 struct type *type;
7842 struct attribute *attr;
7843 const char *name;
7844
7845 /* If the definition of this type lives in .debug_types, read that type.
7846 Don't follow DW_AT_specification though, that will take us back up
7847 the chain and we want to go down. */
7848 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7849 if (attr)
7850 {
7851 struct dwarf2_cu *type_cu = cu;
7852 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7853
7854 type = read_type_die (type_die, type_cu);
7855
7856 /* TYPE_CU may not be the same as CU.
7857 Ensure TYPE is recorded in CU's type_hash table. */
7858 return set_die_type (die, type, cu);
7859 }
7860
7861 type = alloc_type (objfile);
7862
7863 TYPE_CODE (type) = TYPE_CODE_ENUM;
7864 name = dwarf2_full_name (NULL, die, cu);
7865 if (name != NULL)
7866 TYPE_TAG_NAME (type) = (char *) name;
7867
7868 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7869 if (attr)
7870 {
7871 TYPE_LENGTH (type) = DW_UNSND (attr);
7872 }
7873 else
7874 {
7875 TYPE_LENGTH (type) = 0;
7876 }
7877
7878 /* The enumeration DIE can be incomplete. In Ada, any type can be
7879 declared as private in the package spec, and then defined only
7880 inside the package body. Such types are known as Taft Amendment
7881 Types. When another package uses such a type, an incomplete DIE
7882 may be generated by the compiler. */
7883 if (die_is_declaration (die, cu))
7884 TYPE_STUB (type) = 1;
7885
7886 return set_die_type (die, type, cu);
7887 }
7888
7889 /* Given a pointer to a die which begins an enumeration, process all
7890 the dies that define the members of the enumeration, and create the
7891 symbol for the enumeration type.
7892
7893 NOTE: We reverse the order of the element list. */
7894
7895 static void
7896 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7897 {
7898 struct type *this_type;
7899
7900 this_type = get_die_type (die, cu);
7901 if (this_type == NULL)
7902 this_type = read_enumeration_type (die, cu);
7903
7904 if (die->child != NULL)
7905 {
7906 struct die_info *child_die;
7907 struct symbol *sym;
7908 struct field *fields = NULL;
7909 int num_fields = 0;
7910 int unsigned_enum = 1;
7911 char *name;
7912
7913 child_die = die->child;
7914 while (child_die && child_die->tag)
7915 {
7916 if (child_die->tag != DW_TAG_enumerator)
7917 {
7918 process_die (child_die, cu);
7919 }
7920 else
7921 {
7922 name = dwarf2_name (child_die, cu);
7923 if (name)
7924 {
7925 sym = new_symbol (child_die, this_type, cu);
7926 if (SYMBOL_VALUE (sym) < 0)
7927 unsigned_enum = 0;
7928
7929 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7930 {
7931 fields = (struct field *)
7932 xrealloc (fields,
7933 (num_fields + DW_FIELD_ALLOC_CHUNK)
7934 * sizeof (struct field));
7935 }
7936
7937 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7938 FIELD_TYPE (fields[num_fields]) = NULL;
7939 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7940 FIELD_BITSIZE (fields[num_fields]) = 0;
7941
7942 num_fields++;
7943 }
7944 }
7945
7946 child_die = sibling_die (child_die);
7947 }
7948
7949 if (num_fields)
7950 {
7951 TYPE_NFIELDS (this_type) = num_fields;
7952 TYPE_FIELDS (this_type) = (struct field *)
7953 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7954 memcpy (TYPE_FIELDS (this_type), fields,
7955 sizeof (struct field) * num_fields);
7956 xfree (fields);
7957 }
7958 if (unsigned_enum)
7959 TYPE_UNSIGNED (this_type) = 1;
7960 }
7961
7962 /* If we are reading an enum from a .debug_types unit, and the enum
7963 is a declaration, and the enum is not the signatured type in the
7964 unit, then we do not want to add a symbol for it. Adding a
7965 symbol would in some cases obscure the true definition of the
7966 enum, giving users an incomplete type when the definition is
7967 actually available. Note that we do not want to do this for all
7968 enums which are just declarations, because C++0x allows forward
7969 enum declarations. */
7970 if (cu->per_cu->debug_types_section
7971 && die_is_declaration (die, cu))
7972 {
7973 struct signatured_type *type_sig;
7974
7975 type_sig
7976 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7977 cu->per_cu->debug_types_section,
7978 cu->per_cu->offset);
7979 if (type_sig->type_offset != die->offset)
7980 return;
7981 }
7982
7983 new_symbol (die, this_type, cu);
7984 }
7985
7986 /* Extract all information from a DW_TAG_array_type DIE and put it in
7987 the DIE's type field. For now, this only handles one dimensional
7988 arrays. */
7989
7990 static struct type *
7991 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7992 {
7993 struct objfile *objfile = cu->objfile;
7994 struct die_info *child_die;
7995 struct type *type;
7996 struct type *element_type, *range_type, *index_type;
7997 struct type **range_types = NULL;
7998 struct attribute *attr;
7999 int ndim = 0;
8000 struct cleanup *back_to;
8001 char *name;
8002
8003 element_type = die_type (die, cu);
8004
8005 /* The die_type call above may have already set the type for this DIE. */
8006 type = get_die_type (die, cu);
8007 if (type)
8008 return type;
8009
8010 /* Irix 6.2 native cc creates array types without children for
8011 arrays with unspecified length. */
8012 if (die->child == NULL)
8013 {
8014 index_type = objfile_type (objfile)->builtin_int;
8015 range_type = create_range_type (NULL, index_type, 0, -1);
8016 type = create_array_type (NULL, element_type, range_type);
8017 return set_die_type (die, type, cu);
8018 }
8019
8020 back_to = make_cleanup (null_cleanup, NULL);
8021 child_die = die->child;
8022 while (child_die && child_die->tag)
8023 {
8024 if (child_die->tag == DW_TAG_subrange_type)
8025 {
8026 struct type *child_type = read_type_die (child_die, cu);
8027
8028 if (child_type != NULL)
8029 {
8030 /* The range type was succesfully read. Save it for the
8031 array type creation. */
8032 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8033 {
8034 range_types = (struct type **)
8035 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8036 * sizeof (struct type *));
8037 if (ndim == 0)
8038 make_cleanup (free_current_contents, &range_types);
8039 }
8040 range_types[ndim++] = child_type;
8041 }
8042 }
8043 child_die = sibling_die (child_die);
8044 }
8045
8046 /* Dwarf2 dimensions are output from left to right, create the
8047 necessary array types in backwards order. */
8048
8049 type = element_type;
8050
8051 if (read_array_order (die, cu) == DW_ORD_col_major)
8052 {
8053 int i = 0;
8054
8055 while (i < ndim)
8056 type = create_array_type (NULL, type, range_types[i++]);
8057 }
8058 else
8059 {
8060 while (ndim-- > 0)
8061 type = create_array_type (NULL, type, range_types[ndim]);
8062 }
8063
8064 /* Understand Dwarf2 support for vector types (like they occur on
8065 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8066 array type. This is not part of the Dwarf2/3 standard yet, but a
8067 custom vendor extension. The main difference between a regular
8068 array and the vector variant is that vectors are passed by value
8069 to functions. */
8070 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8071 if (attr)
8072 make_vector_type (type);
8073
8074 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8075 implementation may choose to implement triple vectors using this
8076 attribute. */
8077 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8078 if (attr)
8079 {
8080 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8081 TYPE_LENGTH (type) = DW_UNSND (attr);
8082 else
8083 complaint (&symfile_complaints,
8084 _("DW_AT_byte_size for array type smaller "
8085 "than the total size of elements"));
8086 }
8087
8088 name = dwarf2_name (die, cu);
8089 if (name)
8090 TYPE_NAME (type) = name;
8091
8092 /* Install the type in the die. */
8093 set_die_type (die, type, cu);
8094
8095 /* set_die_type should be already done. */
8096 set_descriptive_type (type, die, cu);
8097
8098 do_cleanups (back_to);
8099
8100 return type;
8101 }
8102
8103 static enum dwarf_array_dim_ordering
8104 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8105 {
8106 struct attribute *attr;
8107
8108 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8109
8110 if (attr) return DW_SND (attr);
8111
8112 /* GNU F77 is a special case, as at 08/2004 array type info is the
8113 opposite order to the dwarf2 specification, but data is still
8114 laid out as per normal fortran.
8115
8116 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8117 version checking. */
8118
8119 if (cu->language == language_fortran
8120 && cu->producer && strstr (cu->producer, "GNU F77"))
8121 {
8122 return DW_ORD_row_major;
8123 }
8124
8125 switch (cu->language_defn->la_array_ordering)
8126 {
8127 case array_column_major:
8128 return DW_ORD_col_major;
8129 case array_row_major:
8130 default:
8131 return DW_ORD_row_major;
8132 };
8133 }
8134
8135 /* Extract all information from a DW_TAG_set_type DIE and put it in
8136 the DIE's type field. */
8137
8138 static struct type *
8139 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8140 {
8141 struct type *domain_type, *set_type;
8142 struct attribute *attr;
8143
8144 domain_type = die_type (die, cu);
8145
8146 /* The die_type call above may have already set the type for this DIE. */
8147 set_type = get_die_type (die, cu);
8148 if (set_type)
8149 return set_type;
8150
8151 set_type = create_set_type (NULL, domain_type);
8152
8153 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8154 if (attr)
8155 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8156
8157 return set_die_type (die, set_type, cu);
8158 }
8159
8160 /* First cut: install each common block member as a global variable. */
8161
8162 static void
8163 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8164 {
8165 struct die_info *child_die;
8166 struct attribute *attr;
8167 struct symbol *sym;
8168 CORE_ADDR base = (CORE_ADDR) 0;
8169
8170 attr = dwarf2_attr (die, DW_AT_location, cu);
8171 if (attr)
8172 {
8173 /* Support the .debug_loc offsets. */
8174 if (attr_form_is_block (attr))
8175 {
8176 base = decode_locdesc (DW_BLOCK (attr), cu);
8177 }
8178 else if (attr_form_is_section_offset (attr))
8179 {
8180 dwarf2_complex_location_expr_complaint ();
8181 }
8182 else
8183 {
8184 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8185 "common block member");
8186 }
8187 }
8188 if (die->child != NULL)
8189 {
8190 child_die = die->child;
8191 while (child_die && child_die->tag)
8192 {
8193 LONGEST offset;
8194
8195 sym = new_symbol (child_die, NULL, cu);
8196 if (sym != NULL
8197 && handle_data_member_location (child_die, cu, &offset))
8198 {
8199 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8200 add_symbol_to_list (sym, &global_symbols);
8201 }
8202 child_die = sibling_die (child_die);
8203 }
8204 }
8205 }
8206
8207 /* Create a type for a C++ namespace. */
8208
8209 static struct type *
8210 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8211 {
8212 struct objfile *objfile = cu->objfile;
8213 const char *previous_prefix, *name;
8214 int is_anonymous;
8215 struct type *type;
8216
8217 /* For extensions, reuse the type of the original namespace. */
8218 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8219 {
8220 struct die_info *ext_die;
8221 struct dwarf2_cu *ext_cu = cu;
8222
8223 ext_die = dwarf2_extension (die, &ext_cu);
8224 type = read_type_die (ext_die, ext_cu);
8225
8226 /* EXT_CU may not be the same as CU.
8227 Ensure TYPE is recorded in CU's type_hash table. */
8228 return set_die_type (die, type, cu);
8229 }
8230
8231 name = namespace_name (die, &is_anonymous, cu);
8232
8233 /* Now build the name of the current namespace. */
8234
8235 previous_prefix = determine_prefix (die, cu);
8236 if (previous_prefix[0] != '\0')
8237 name = typename_concat (&objfile->objfile_obstack,
8238 previous_prefix, name, 0, cu);
8239
8240 /* Create the type. */
8241 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8242 objfile);
8243 TYPE_NAME (type) = (char *) name;
8244 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8245
8246 return set_die_type (die, type, cu);
8247 }
8248
8249 /* Read a C++ namespace. */
8250
8251 static void
8252 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8253 {
8254 struct objfile *objfile = cu->objfile;
8255 int is_anonymous;
8256
8257 /* Add a symbol associated to this if we haven't seen the namespace
8258 before. Also, add a using directive if it's an anonymous
8259 namespace. */
8260
8261 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8262 {
8263 struct type *type;
8264
8265 type = read_type_die (die, cu);
8266 new_symbol (die, type, cu);
8267
8268 namespace_name (die, &is_anonymous, cu);
8269 if (is_anonymous)
8270 {
8271 const char *previous_prefix = determine_prefix (die, cu);
8272
8273 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8274 NULL, NULL, &objfile->objfile_obstack);
8275 }
8276 }
8277
8278 if (die->child != NULL)
8279 {
8280 struct die_info *child_die = die->child;
8281
8282 while (child_die && child_die->tag)
8283 {
8284 process_die (child_die, cu);
8285 child_die = sibling_die (child_die);
8286 }
8287 }
8288 }
8289
8290 /* Read a Fortran module as type. This DIE can be only a declaration used for
8291 imported module. Still we need that type as local Fortran "use ... only"
8292 declaration imports depend on the created type in determine_prefix. */
8293
8294 static struct type *
8295 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8296 {
8297 struct objfile *objfile = cu->objfile;
8298 char *module_name;
8299 struct type *type;
8300
8301 module_name = dwarf2_name (die, cu);
8302 if (!module_name)
8303 complaint (&symfile_complaints,
8304 _("DW_TAG_module has no name, offset 0x%x"),
8305 die->offset);
8306 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8307
8308 /* determine_prefix uses TYPE_TAG_NAME. */
8309 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8310
8311 return set_die_type (die, type, cu);
8312 }
8313
8314 /* Read a Fortran module. */
8315
8316 static void
8317 read_module (struct die_info *die, struct dwarf2_cu *cu)
8318 {
8319 struct die_info *child_die = die->child;
8320
8321 while (child_die && child_die->tag)
8322 {
8323 process_die (child_die, cu);
8324 child_die = sibling_die (child_die);
8325 }
8326 }
8327
8328 /* Return the name of the namespace represented by DIE. Set
8329 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8330 namespace. */
8331
8332 static const char *
8333 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8334 {
8335 struct die_info *current_die;
8336 const char *name = NULL;
8337
8338 /* Loop through the extensions until we find a name. */
8339
8340 for (current_die = die;
8341 current_die != NULL;
8342 current_die = dwarf2_extension (die, &cu))
8343 {
8344 name = dwarf2_name (current_die, cu);
8345 if (name != NULL)
8346 break;
8347 }
8348
8349 /* Is it an anonymous namespace? */
8350
8351 *is_anonymous = (name == NULL);
8352 if (*is_anonymous)
8353 name = CP_ANONYMOUS_NAMESPACE_STR;
8354
8355 return name;
8356 }
8357
8358 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8359 the user defined type vector. */
8360
8361 static struct type *
8362 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8363 {
8364 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8365 struct comp_unit_head *cu_header = &cu->header;
8366 struct type *type;
8367 struct attribute *attr_byte_size;
8368 struct attribute *attr_address_class;
8369 int byte_size, addr_class;
8370 struct type *target_type;
8371
8372 target_type = die_type (die, cu);
8373
8374 /* The die_type call above may have already set the type for this DIE. */
8375 type = get_die_type (die, cu);
8376 if (type)
8377 return type;
8378
8379 type = lookup_pointer_type (target_type);
8380
8381 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8382 if (attr_byte_size)
8383 byte_size = DW_UNSND (attr_byte_size);
8384 else
8385 byte_size = cu_header->addr_size;
8386
8387 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8388 if (attr_address_class)
8389 addr_class = DW_UNSND (attr_address_class);
8390 else
8391 addr_class = DW_ADDR_none;
8392
8393 /* If the pointer size or address class is different than the
8394 default, create a type variant marked as such and set the
8395 length accordingly. */
8396 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8397 {
8398 if (gdbarch_address_class_type_flags_p (gdbarch))
8399 {
8400 int type_flags;
8401
8402 type_flags = gdbarch_address_class_type_flags
8403 (gdbarch, byte_size, addr_class);
8404 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8405 == 0);
8406 type = make_type_with_address_space (type, type_flags);
8407 }
8408 else if (TYPE_LENGTH (type) != byte_size)
8409 {
8410 complaint (&symfile_complaints,
8411 _("invalid pointer size %d"), byte_size);
8412 }
8413 else
8414 {
8415 /* Should we also complain about unhandled address classes? */
8416 }
8417 }
8418
8419 TYPE_LENGTH (type) = byte_size;
8420 return set_die_type (die, type, cu);
8421 }
8422
8423 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8424 the user defined type vector. */
8425
8426 static struct type *
8427 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8428 {
8429 struct type *type;
8430 struct type *to_type;
8431 struct type *domain;
8432
8433 to_type = die_type (die, cu);
8434 domain = die_containing_type (die, cu);
8435
8436 /* The calls above may have already set the type for this DIE. */
8437 type = get_die_type (die, cu);
8438 if (type)
8439 return type;
8440
8441 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8442 type = lookup_methodptr_type (to_type);
8443 else
8444 type = lookup_memberptr_type (to_type, domain);
8445
8446 return set_die_type (die, type, cu);
8447 }
8448
8449 /* Extract all information from a DW_TAG_reference_type DIE and add to
8450 the user defined type vector. */
8451
8452 static struct type *
8453 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8454 {
8455 struct comp_unit_head *cu_header = &cu->header;
8456 struct type *type, *target_type;
8457 struct attribute *attr;
8458
8459 target_type = die_type (die, cu);
8460
8461 /* The die_type call above may have already set the type for this DIE. */
8462 type = get_die_type (die, cu);
8463 if (type)
8464 return type;
8465
8466 type = lookup_reference_type (target_type);
8467 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8468 if (attr)
8469 {
8470 TYPE_LENGTH (type) = DW_UNSND (attr);
8471 }
8472 else
8473 {
8474 TYPE_LENGTH (type) = cu_header->addr_size;
8475 }
8476 return set_die_type (die, type, cu);
8477 }
8478
8479 static struct type *
8480 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8481 {
8482 struct type *base_type, *cv_type;
8483
8484 base_type = die_type (die, cu);
8485
8486 /* The die_type call above may have already set the type for this DIE. */
8487 cv_type = get_die_type (die, cu);
8488 if (cv_type)
8489 return cv_type;
8490
8491 /* In case the const qualifier is applied to an array type, the element type
8492 is so qualified, not the array type (section 6.7.3 of C99). */
8493 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8494 {
8495 struct type *el_type, *inner_array;
8496
8497 base_type = copy_type (base_type);
8498 inner_array = base_type;
8499
8500 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8501 {
8502 TYPE_TARGET_TYPE (inner_array) =
8503 copy_type (TYPE_TARGET_TYPE (inner_array));
8504 inner_array = TYPE_TARGET_TYPE (inner_array);
8505 }
8506
8507 el_type = TYPE_TARGET_TYPE (inner_array);
8508 TYPE_TARGET_TYPE (inner_array) =
8509 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8510
8511 return set_die_type (die, base_type, cu);
8512 }
8513
8514 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8515 return set_die_type (die, cv_type, cu);
8516 }
8517
8518 static struct type *
8519 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8520 {
8521 struct type *base_type, *cv_type;
8522
8523 base_type = die_type (die, cu);
8524
8525 /* The die_type call above may have already set the type for this DIE. */
8526 cv_type = get_die_type (die, cu);
8527 if (cv_type)
8528 return cv_type;
8529
8530 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8531 return set_die_type (die, cv_type, cu);
8532 }
8533
8534 /* Extract all information from a DW_TAG_string_type DIE and add to
8535 the user defined type vector. It isn't really a user defined type,
8536 but it behaves like one, with other DIE's using an AT_user_def_type
8537 attribute to reference it. */
8538
8539 static struct type *
8540 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8541 {
8542 struct objfile *objfile = cu->objfile;
8543 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8544 struct type *type, *range_type, *index_type, *char_type;
8545 struct attribute *attr;
8546 unsigned int length;
8547
8548 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8549 if (attr)
8550 {
8551 length = DW_UNSND (attr);
8552 }
8553 else
8554 {
8555 /* Check for the DW_AT_byte_size attribute. */
8556 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8557 if (attr)
8558 {
8559 length = DW_UNSND (attr);
8560 }
8561 else
8562 {
8563 length = 1;
8564 }
8565 }
8566
8567 index_type = objfile_type (objfile)->builtin_int;
8568 range_type = create_range_type (NULL, index_type, 1, length);
8569 char_type = language_string_char_type (cu->language_defn, gdbarch);
8570 type = create_string_type (NULL, char_type, range_type);
8571
8572 return set_die_type (die, type, cu);
8573 }
8574
8575 /* Handle DIES due to C code like:
8576
8577 struct foo
8578 {
8579 int (*funcp)(int a, long l);
8580 int b;
8581 };
8582
8583 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8584
8585 static struct type *
8586 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8587 {
8588 struct objfile *objfile = cu->objfile;
8589 struct type *type; /* Type that this function returns. */
8590 struct type *ftype; /* Function that returns above type. */
8591 struct attribute *attr;
8592
8593 type = die_type (die, cu);
8594
8595 /* The die_type call above may have already set the type for this DIE. */
8596 ftype = get_die_type (die, cu);
8597 if (ftype)
8598 return ftype;
8599
8600 ftype = lookup_function_type (type);
8601
8602 /* All functions in C++, Pascal and Java have prototypes. */
8603 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8604 if ((attr && (DW_UNSND (attr) != 0))
8605 || cu->language == language_cplus
8606 || cu->language == language_java
8607 || cu->language == language_pascal)
8608 TYPE_PROTOTYPED (ftype) = 1;
8609 else if (producer_is_realview (cu->producer))
8610 /* RealView does not emit DW_AT_prototyped. We can not
8611 distinguish prototyped and unprototyped functions; default to
8612 prototyped, since that is more common in modern code (and
8613 RealView warns about unprototyped functions). */
8614 TYPE_PROTOTYPED (ftype) = 1;
8615
8616 /* Store the calling convention in the type if it's available in
8617 the subroutine die. Otherwise set the calling convention to
8618 the default value DW_CC_normal. */
8619 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8620 if (attr)
8621 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8622 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8623 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8624 else
8625 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8626
8627 /* We need to add the subroutine type to the die immediately so
8628 we don't infinitely recurse when dealing with parameters
8629 declared as the same subroutine type. */
8630 set_die_type (die, ftype, cu);
8631
8632 if (die->child != NULL)
8633 {
8634 struct type *void_type = objfile_type (objfile)->builtin_void;
8635 struct die_info *child_die;
8636 int nparams, iparams;
8637
8638 /* Count the number of parameters.
8639 FIXME: GDB currently ignores vararg functions, but knows about
8640 vararg member functions. */
8641 nparams = 0;
8642 child_die = die->child;
8643 while (child_die && child_die->tag)
8644 {
8645 if (child_die->tag == DW_TAG_formal_parameter)
8646 nparams++;
8647 else if (child_die->tag == DW_TAG_unspecified_parameters)
8648 TYPE_VARARGS (ftype) = 1;
8649 child_die = sibling_die (child_die);
8650 }
8651
8652 /* Allocate storage for parameters and fill them in. */
8653 TYPE_NFIELDS (ftype) = nparams;
8654 TYPE_FIELDS (ftype) = (struct field *)
8655 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8656
8657 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8658 even if we error out during the parameters reading below. */
8659 for (iparams = 0; iparams < nparams; iparams++)
8660 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8661
8662 iparams = 0;
8663 child_die = die->child;
8664 while (child_die && child_die->tag)
8665 {
8666 if (child_die->tag == DW_TAG_formal_parameter)
8667 {
8668 struct type *arg_type;
8669
8670 /* DWARF version 2 has no clean way to discern C++
8671 static and non-static member functions. G++ helps
8672 GDB by marking the first parameter for non-static
8673 member functions (which is the this pointer) as
8674 artificial. We pass this information to
8675 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8676
8677 DWARF version 3 added DW_AT_object_pointer, which GCC
8678 4.5 does not yet generate. */
8679 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8680 if (attr)
8681 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8682 else
8683 {
8684 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8685
8686 /* GCC/43521: In java, the formal parameter
8687 "this" is sometimes not marked with DW_AT_artificial. */
8688 if (cu->language == language_java)
8689 {
8690 const char *name = dwarf2_name (child_die, cu);
8691
8692 if (name && !strcmp (name, "this"))
8693 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8694 }
8695 }
8696 arg_type = die_type (child_die, cu);
8697
8698 /* RealView does not mark THIS as const, which the testsuite
8699 expects. GCC marks THIS as const in method definitions,
8700 but not in the class specifications (GCC PR 43053). */
8701 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8702 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8703 {
8704 int is_this = 0;
8705 struct dwarf2_cu *arg_cu = cu;
8706 const char *name = dwarf2_name (child_die, cu);
8707
8708 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8709 if (attr)
8710 {
8711 /* If the compiler emits this, use it. */
8712 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8713 is_this = 1;
8714 }
8715 else if (name && strcmp (name, "this") == 0)
8716 /* Function definitions will have the argument names. */
8717 is_this = 1;
8718 else if (name == NULL && iparams == 0)
8719 /* Declarations may not have the names, so like
8720 elsewhere in GDB, assume an artificial first
8721 argument is "this". */
8722 is_this = 1;
8723
8724 if (is_this)
8725 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8726 arg_type, 0);
8727 }
8728
8729 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8730 iparams++;
8731 }
8732 child_die = sibling_die (child_die);
8733 }
8734 }
8735
8736 return ftype;
8737 }
8738
8739 static struct type *
8740 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8741 {
8742 struct objfile *objfile = cu->objfile;
8743 const char *name = NULL;
8744 struct type *this_type, *target_type;
8745
8746 name = dwarf2_full_name (NULL, die, cu);
8747 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8748 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8749 TYPE_NAME (this_type) = (char *) name;
8750 set_die_type (die, this_type, cu);
8751 target_type = die_type (die, cu);
8752 if (target_type != this_type)
8753 TYPE_TARGET_TYPE (this_type) = target_type;
8754 else
8755 {
8756 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8757 spec and cause infinite loops in GDB. */
8758 complaint (&symfile_complaints,
8759 _("Self-referential DW_TAG_typedef "
8760 "- DIE at 0x%x [in module %s]"),
8761 die->offset, objfile->name);
8762 TYPE_TARGET_TYPE (this_type) = NULL;
8763 }
8764 return this_type;
8765 }
8766
8767 /* Find a representation of a given base type and install
8768 it in the TYPE field of the die. */
8769
8770 static struct type *
8771 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8772 {
8773 struct objfile *objfile = cu->objfile;
8774 struct type *type;
8775 struct attribute *attr;
8776 int encoding = 0, size = 0;
8777 char *name;
8778 enum type_code code = TYPE_CODE_INT;
8779 int type_flags = 0;
8780 struct type *target_type = NULL;
8781
8782 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8783 if (attr)
8784 {
8785 encoding = DW_UNSND (attr);
8786 }
8787 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8788 if (attr)
8789 {
8790 size = DW_UNSND (attr);
8791 }
8792 name = dwarf2_name (die, cu);
8793 if (!name)
8794 {
8795 complaint (&symfile_complaints,
8796 _("DW_AT_name missing from DW_TAG_base_type"));
8797 }
8798
8799 switch (encoding)
8800 {
8801 case DW_ATE_address:
8802 /* Turn DW_ATE_address into a void * pointer. */
8803 code = TYPE_CODE_PTR;
8804 type_flags |= TYPE_FLAG_UNSIGNED;
8805 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8806 break;
8807 case DW_ATE_boolean:
8808 code = TYPE_CODE_BOOL;
8809 type_flags |= TYPE_FLAG_UNSIGNED;
8810 break;
8811 case DW_ATE_complex_float:
8812 code = TYPE_CODE_COMPLEX;
8813 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8814 break;
8815 case DW_ATE_decimal_float:
8816 code = TYPE_CODE_DECFLOAT;
8817 break;
8818 case DW_ATE_float:
8819 code = TYPE_CODE_FLT;
8820 break;
8821 case DW_ATE_signed:
8822 break;
8823 case DW_ATE_unsigned:
8824 type_flags |= TYPE_FLAG_UNSIGNED;
8825 if (cu->language == language_fortran
8826 && name
8827 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8828 code = TYPE_CODE_CHAR;
8829 break;
8830 case DW_ATE_signed_char:
8831 if (cu->language == language_ada || cu->language == language_m2
8832 || cu->language == language_pascal
8833 || cu->language == language_fortran)
8834 code = TYPE_CODE_CHAR;
8835 break;
8836 case DW_ATE_unsigned_char:
8837 if (cu->language == language_ada || cu->language == language_m2
8838 || cu->language == language_pascal
8839 || cu->language == language_fortran)
8840 code = TYPE_CODE_CHAR;
8841 type_flags |= TYPE_FLAG_UNSIGNED;
8842 break;
8843 case DW_ATE_UTF:
8844 /* We just treat this as an integer and then recognize the
8845 type by name elsewhere. */
8846 break;
8847
8848 default:
8849 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8850 dwarf_type_encoding_name (encoding));
8851 break;
8852 }
8853
8854 type = init_type (code, size, type_flags, NULL, objfile);
8855 TYPE_NAME (type) = name;
8856 TYPE_TARGET_TYPE (type) = target_type;
8857
8858 if (name && strcmp (name, "char") == 0)
8859 TYPE_NOSIGN (type) = 1;
8860
8861 return set_die_type (die, type, cu);
8862 }
8863
8864 /* Read the given DW_AT_subrange DIE. */
8865
8866 static struct type *
8867 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8868 {
8869 struct type *base_type;
8870 struct type *range_type;
8871 struct attribute *attr;
8872 LONGEST low = 0;
8873 LONGEST high = -1;
8874 char *name;
8875 LONGEST negative_mask;
8876
8877 base_type = die_type (die, cu);
8878 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8879 check_typedef (base_type);
8880
8881 /* The die_type call above may have already set the type for this DIE. */
8882 range_type = get_die_type (die, cu);
8883 if (range_type)
8884 return range_type;
8885
8886 if (cu->language == language_fortran)
8887 {
8888 /* FORTRAN implies a lower bound of 1, if not given. */
8889 low = 1;
8890 }
8891
8892 /* FIXME: For variable sized arrays either of these could be
8893 a variable rather than a constant value. We'll allow it,
8894 but we don't know how to handle it. */
8895 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8896 if (attr)
8897 low = dwarf2_get_attr_constant_value (attr, 0);
8898
8899 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8900 if (attr)
8901 {
8902 if (attr_form_is_block (attr) || is_ref_attr (attr))
8903 {
8904 /* GCC encodes arrays with unspecified or dynamic length
8905 with a DW_FORM_block1 attribute or a reference attribute.
8906 FIXME: GDB does not yet know how to handle dynamic
8907 arrays properly, treat them as arrays with unspecified
8908 length for now.
8909
8910 FIXME: jimb/2003-09-22: GDB does not really know
8911 how to handle arrays of unspecified length
8912 either; we just represent them as zero-length
8913 arrays. Choose an appropriate upper bound given
8914 the lower bound we've computed above. */
8915 high = low - 1;
8916 }
8917 else
8918 high = dwarf2_get_attr_constant_value (attr, 1);
8919 }
8920 else
8921 {
8922 attr = dwarf2_attr (die, DW_AT_count, cu);
8923 if (attr)
8924 {
8925 int count = dwarf2_get_attr_constant_value (attr, 1);
8926 high = low + count - 1;
8927 }
8928 else
8929 {
8930 /* Unspecified array length. */
8931 high = low - 1;
8932 }
8933 }
8934
8935 /* Dwarf-2 specifications explicitly allows to create subrange types
8936 without specifying a base type.
8937 In that case, the base type must be set to the type of
8938 the lower bound, upper bound or count, in that order, if any of these
8939 three attributes references an object that has a type.
8940 If no base type is found, the Dwarf-2 specifications say that
8941 a signed integer type of size equal to the size of an address should
8942 be used.
8943 For the following C code: `extern char gdb_int [];'
8944 GCC produces an empty range DIE.
8945 FIXME: muller/2010-05-28: Possible references to object for low bound,
8946 high bound or count are not yet handled by this code. */
8947 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8948 {
8949 struct objfile *objfile = cu->objfile;
8950 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8951 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8952 struct type *int_type = objfile_type (objfile)->builtin_int;
8953
8954 /* Test "int", "long int", and "long long int" objfile types,
8955 and select the first one having a size above or equal to the
8956 architecture address size. */
8957 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8958 base_type = int_type;
8959 else
8960 {
8961 int_type = objfile_type (objfile)->builtin_long;
8962 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8963 base_type = int_type;
8964 else
8965 {
8966 int_type = objfile_type (objfile)->builtin_long_long;
8967 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8968 base_type = int_type;
8969 }
8970 }
8971 }
8972
8973 negative_mask =
8974 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8975 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8976 low |= negative_mask;
8977 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8978 high |= negative_mask;
8979
8980 range_type = create_range_type (NULL, base_type, low, high);
8981
8982 /* Mark arrays with dynamic length at least as an array of unspecified
8983 length. GDB could check the boundary but before it gets implemented at
8984 least allow accessing the array elements. */
8985 if (attr && attr_form_is_block (attr))
8986 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8987
8988 /* Ada expects an empty array on no boundary attributes. */
8989 if (attr == NULL && cu->language != language_ada)
8990 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8991
8992 name = dwarf2_name (die, cu);
8993 if (name)
8994 TYPE_NAME (range_type) = name;
8995
8996 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8997 if (attr)
8998 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8999
9000 set_die_type (die, range_type, cu);
9001
9002 /* set_die_type should be already done. */
9003 set_descriptive_type (range_type, die, cu);
9004
9005 return range_type;
9006 }
9007
9008 static struct type *
9009 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9010 {
9011 struct type *type;
9012
9013 /* For now, we only support the C meaning of an unspecified type: void. */
9014
9015 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9016 TYPE_NAME (type) = dwarf2_name (die, cu);
9017
9018 return set_die_type (die, type, cu);
9019 }
9020
9021 /* Trivial hash function for die_info: the hash value of a DIE
9022 is its offset in .debug_info for this objfile. */
9023
9024 static hashval_t
9025 die_hash (const void *item)
9026 {
9027 const struct die_info *die = item;
9028
9029 return die->offset;
9030 }
9031
9032 /* Trivial comparison function for die_info structures: two DIEs
9033 are equal if they have the same offset. */
9034
9035 static int
9036 die_eq (const void *item_lhs, const void *item_rhs)
9037 {
9038 const struct die_info *die_lhs = item_lhs;
9039 const struct die_info *die_rhs = item_rhs;
9040
9041 return die_lhs->offset == die_rhs->offset;
9042 }
9043
9044 /* Read a whole compilation unit into a linked list of dies. */
9045
9046 static struct die_info *
9047 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9048 {
9049 struct die_reader_specs reader_specs;
9050 int read_abbrevs = 0;
9051 struct cleanup *back_to = NULL;
9052 struct die_info *die;
9053
9054 if (cu->dwarf2_abbrevs == NULL)
9055 {
9056 dwarf2_read_abbrevs (cu);
9057 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9058 read_abbrevs = 1;
9059 }
9060
9061 gdb_assert (cu->die_hash == NULL);
9062 cu->die_hash
9063 = htab_create_alloc_ex (cu->header.length / 12,
9064 die_hash,
9065 die_eq,
9066 NULL,
9067 &cu->comp_unit_obstack,
9068 hashtab_obstack_allocate,
9069 dummy_obstack_deallocate);
9070
9071 init_cu_die_reader (&reader_specs, cu);
9072
9073 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9074
9075 if (read_abbrevs)
9076 do_cleanups (back_to);
9077
9078 return die;
9079 }
9080
9081 /* Main entry point for reading a DIE and all children.
9082 Read the DIE and dump it if requested. */
9083
9084 static struct die_info *
9085 read_die_and_children (const struct die_reader_specs *reader,
9086 gdb_byte *info_ptr,
9087 gdb_byte **new_info_ptr,
9088 struct die_info *parent)
9089 {
9090 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9091 new_info_ptr, parent);
9092
9093 if (dwarf2_die_debug)
9094 {
9095 fprintf_unfiltered (gdb_stdlog,
9096 "\nRead die from %s of %s:\n",
9097 (reader->cu->per_cu->debug_types_section
9098 ? ".debug_types"
9099 : ".debug_info"),
9100 reader->abfd->filename);
9101 dump_die (result, dwarf2_die_debug);
9102 }
9103
9104 return result;
9105 }
9106
9107 /* Read a single die and all its descendents. Set the die's sibling
9108 field to NULL; set other fields in the die correctly, and set all
9109 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9110 location of the info_ptr after reading all of those dies. PARENT
9111 is the parent of the die in question. */
9112
9113 static struct die_info *
9114 read_die_and_children_1 (const struct die_reader_specs *reader,
9115 gdb_byte *info_ptr,
9116 gdb_byte **new_info_ptr,
9117 struct die_info *parent)
9118 {
9119 struct die_info *die;
9120 gdb_byte *cur_ptr;
9121 int has_children;
9122
9123 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9124 if (die == NULL)
9125 {
9126 *new_info_ptr = cur_ptr;
9127 return NULL;
9128 }
9129 store_in_ref_table (die, reader->cu);
9130
9131 if (has_children)
9132 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9133 else
9134 {
9135 die->child = NULL;
9136 *new_info_ptr = cur_ptr;
9137 }
9138
9139 die->sibling = NULL;
9140 die->parent = parent;
9141 return die;
9142 }
9143
9144 /* Read a die, all of its descendents, and all of its siblings; set
9145 all of the fields of all of the dies correctly. Arguments are as
9146 in read_die_and_children. */
9147
9148 static struct die_info *
9149 read_die_and_siblings (const struct die_reader_specs *reader,
9150 gdb_byte *info_ptr,
9151 gdb_byte **new_info_ptr,
9152 struct die_info *parent)
9153 {
9154 struct die_info *first_die, *last_sibling;
9155 gdb_byte *cur_ptr;
9156
9157 cur_ptr = info_ptr;
9158 first_die = last_sibling = NULL;
9159
9160 while (1)
9161 {
9162 struct die_info *die
9163 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9164
9165 if (die == NULL)
9166 {
9167 *new_info_ptr = cur_ptr;
9168 return first_die;
9169 }
9170
9171 if (!first_die)
9172 first_die = die;
9173 else
9174 last_sibling->sibling = die;
9175
9176 last_sibling = die;
9177 }
9178 }
9179
9180 /* Read the die from the .debug_info section buffer. Set DIEP to
9181 point to a newly allocated die with its information, except for its
9182 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9183 whether the die has children or not. */
9184
9185 static gdb_byte *
9186 read_full_die (const struct die_reader_specs *reader,
9187 struct die_info **diep, gdb_byte *info_ptr,
9188 int *has_children)
9189 {
9190 unsigned int abbrev_number, bytes_read, i, offset;
9191 struct abbrev_info *abbrev;
9192 struct die_info *die;
9193 struct dwarf2_cu *cu = reader->cu;
9194 bfd *abfd = reader->abfd;
9195
9196 offset = info_ptr - reader->buffer;
9197 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9198 info_ptr += bytes_read;
9199 if (!abbrev_number)
9200 {
9201 *diep = NULL;
9202 *has_children = 0;
9203 return info_ptr;
9204 }
9205
9206 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9207 if (!abbrev)
9208 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9209 abbrev_number,
9210 bfd_get_filename (abfd));
9211
9212 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9213 die->offset = offset;
9214 die->tag = abbrev->tag;
9215 die->abbrev = abbrev_number;
9216
9217 die->num_attrs = abbrev->num_attrs;
9218
9219 for (i = 0; i < abbrev->num_attrs; ++i)
9220 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9221 abfd, info_ptr, cu);
9222
9223 *diep = die;
9224 *has_children = abbrev->has_children;
9225 return info_ptr;
9226 }
9227
9228 /* In DWARF version 2, the description of the debugging information is
9229 stored in a separate .debug_abbrev section. Before we read any
9230 dies from a section we read in all abbreviations and install them
9231 in a hash table. This function also sets flags in CU describing
9232 the data found in the abbrev table. */
9233
9234 static void
9235 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9236 {
9237 bfd *abfd = cu->objfile->obfd;
9238 struct comp_unit_head *cu_header = &cu->header;
9239 gdb_byte *abbrev_ptr;
9240 struct abbrev_info *cur_abbrev;
9241 unsigned int abbrev_number, bytes_read, abbrev_name;
9242 unsigned int abbrev_form, hash_number;
9243 struct attr_abbrev *cur_attrs;
9244 unsigned int allocated_attrs;
9245
9246 /* Initialize dwarf2 abbrevs. */
9247 obstack_init (&cu->abbrev_obstack);
9248 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9249 (ABBREV_HASH_SIZE
9250 * sizeof (struct abbrev_info *)));
9251 memset (cu->dwarf2_abbrevs, 0,
9252 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9253
9254 dwarf2_read_section (dwarf2_per_objfile->objfile,
9255 &dwarf2_per_objfile->abbrev);
9256 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9257 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9258 abbrev_ptr += bytes_read;
9259
9260 allocated_attrs = ATTR_ALLOC_CHUNK;
9261 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9262
9263 /* Loop until we reach an abbrev number of 0. */
9264 while (abbrev_number)
9265 {
9266 cur_abbrev = dwarf_alloc_abbrev (cu);
9267
9268 /* read in abbrev header */
9269 cur_abbrev->number = abbrev_number;
9270 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9271 abbrev_ptr += bytes_read;
9272 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9273 abbrev_ptr += 1;
9274
9275 if (cur_abbrev->tag == DW_TAG_namespace)
9276 cu->has_namespace_info = 1;
9277
9278 /* now read in declarations */
9279 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9280 abbrev_ptr += bytes_read;
9281 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9282 abbrev_ptr += bytes_read;
9283 while (abbrev_name)
9284 {
9285 if (cur_abbrev->num_attrs == allocated_attrs)
9286 {
9287 allocated_attrs += ATTR_ALLOC_CHUNK;
9288 cur_attrs
9289 = xrealloc (cur_attrs, (allocated_attrs
9290 * sizeof (struct attr_abbrev)));
9291 }
9292
9293 /* Record whether this compilation unit might have
9294 inter-compilation-unit references. If we don't know what form
9295 this attribute will have, then it might potentially be a
9296 DW_FORM_ref_addr, so we conservatively expect inter-CU
9297 references. */
9298
9299 if (abbrev_form == DW_FORM_ref_addr
9300 || abbrev_form == DW_FORM_indirect)
9301 cu->has_form_ref_addr = 1;
9302
9303 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9304 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9305 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9306 abbrev_ptr += bytes_read;
9307 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9308 abbrev_ptr += bytes_read;
9309 }
9310
9311 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9312 (cur_abbrev->num_attrs
9313 * sizeof (struct attr_abbrev)));
9314 memcpy (cur_abbrev->attrs, cur_attrs,
9315 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9316
9317 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9318 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9319 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9320
9321 /* Get next abbreviation.
9322 Under Irix6 the abbreviations for a compilation unit are not
9323 always properly terminated with an abbrev number of 0.
9324 Exit loop if we encounter an abbreviation which we have
9325 already read (which means we are about to read the abbreviations
9326 for the next compile unit) or if the end of the abbreviation
9327 table is reached. */
9328 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9329 >= dwarf2_per_objfile->abbrev.size)
9330 break;
9331 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9332 abbrev_ptr += bytes_read;
9333 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9334 break;
9335 }
9336
9337 xfree (cur_attrs);
9338 }
9339
9340 /* Release the memory used by the abbrev table for a compilation unit. */
9341
9342 static void
9343 dwarf2_free_abbrev_table (void *ptr_to_cu)
9344 {
9345 struct dwarf2_cu *cu = ptr_to_cu;
9346
9347 obstack_free (&cu->abbrev_obstack, NULL);
9348 cu->dwarf2_abbrevs = NULL;
9349 }
9350
9351 /* Lookup an abbrev_info structure in the abbrev hash table. */
9352
9353 static struct abbrev_info *
9354 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9355 {
9356 unsigned int hash_number;
9357 struct abbrev_info *abbrev;
9358
9359 hash_number = number % ABBREV_HASH_SIZE;
9360 abbrev = cu->dwarf2_abbrevs[hash_number];
9361
9362 while (abbrev)
9363 {
9364 if (abbrev->number == number)
9365 return abbrev;
9366 else
9367 abbrev = abbrev->next;
9368 }
9369 return NULL;
9370 }
9371
9372 /* Returns nonzero if TAG represents a type that we might generate a partial
9373 symbol for. */
9374
9375 static int
9376 is_type_tag_for_partial (int tag)
9377 {
9378 switch (tag)
9379 {
9380 #if 0
9381 /* Some types that would be reasonable to generate partial symbols for,
9382 that we don't at present. */
9383 case DW_TAG_array_type:
9384 case DW_TAG_file_type:
9385 case DW_TAG_ptr_to_member_type:
9386 case DW_TAG_set_type:
9387 case DW_TAG_string_type:
9388 case DW_TAG_subroutine_type:
9389 #endif
9390 case DW_TAG_base_type:
9391 case DW_TAG_class_type:
9392 case DW_TAG_interface_type:
9393 case DW_TAG_enumeration_type:
9394 case DW_TAG_structure_type:
9395 case DW_TAG_subrange_type:
9396 case DW_TAG_typedef:
9397 case DW_TAG_union_type:
9398 return 1;
9399 default:
9400 return 0;
9401 }
9402 }
9403
9404 /* Load all DIEs that are interesting for partial symbols into memory. */
9405
9406 static struct partial_die_info *
9407 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9408 int building_psymtab, struct dwarf2_cu *cu)
9409 {
9410 struct objfile *objfile = cu->objfile;
9411 struct partial_die_info *part_die;
9412 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9413 struct abbrev_info *abbrev;
9414 unsigned int bytes_read;
9415 unsigned int load_all = 0;
9416
9417 int nesting_level = 1;
9418
9419 parent_die = NULL;
9420 last_die = NULL;
9421
9422 if (cu->per_cu && cu->per_cu->load_all_dies)
9423 load_all = 1;
9424
9425 cu->partial_dies
9426 = htab_create_alloc_ex (cu->header.length / 12,
9427 partial_die_hash,
9428 partial_die_eq,
9429 NULL,
9430 &cu->comp_unit_obstack,
9431 hashtab_obstack_allocate,
9432 dummy_obstack_deallocate);
9433
9434 part_die = obstack_alloc (&cu->comp_unit_obstack,
9435 sizeof (struct partial_die_info));
9436
9437 while (1)
9438 {
9439 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9440
9441 /* A NULL abbrev means the end of a series of children. */
9442 if (abbrev == NULL)
9443 {
9444 if (--nesting_level == 0)
9445 {
9446 /* PART_DIE was probably the last thing allocated on the
9447 comp_unit_obstack, so we could call obstack_free
9448 here. We don't do that because the waste is small,
9449 and will be cleaned up when we're done with this
9450 compilation unit. This way, we're also more robust
9451 against other users of the comp_unit_obstack. */
9452 return first_die;
9453 }
9454 info_ptr += bytes_read;
9455 last_die = parent_die;
9456 parent_die = parent_die->die_parent;
9457 continue;
9458 }
9459
9460 /* Check for template arguments. We never save these; if
9461 they're seen, we just mark the parent, and go on our way. */
9462 if (parent_die != NULL
9463 && cu->language == language_cplus
9464 && (abbrev->tag == DW_TAG_template_type_param
9465 || abbrev->tag == DW_TAG_template_value_param))
9466 {
9467 parent_die->has_template_arguments = 1;
9468
9469 if (!load_all)
9470 {
9471 /* We don't need a partial DIE for the template argument. */
9472 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9473 cu);
9474 continue;
9475 }
9476 }
9477
9478 /* We only recurse into subprograms looking for template arguments.
9479 Skip their other children. */
9480 if (!load_all
9481 && cu->language == language_cplus
9482 && parent_die != NULL
9483 && parent_die->tag == DW_TAG_subprogram)
9484 {
9485 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9486 continue;
9487 }
9488
9489 /* Check whether this DIE is interesting enough to save. Normally
9490 we would not be interested in members here, but there may be
9491 later variables referencing them via DW_AT_specification (for
9492 static members). */
9493 if (!load_all
9494 && !is_type_tag_for_partial (abbrev->tag)
9495 && abbrev->tag != DW_TAG_constant
9496 && abbrev->tag != DW_TAG_enumerator
9497 && abbrev->tag != DW_TAG_subprogram
9498 && abbrev->tag != DW_TAG_lexical_block
9499 && abbrev->tag != DW_TAG_variable
9500 && abbrev->tag != DW_TAG_namespace
9501 && abbrev->tag != DW_TAG_module
9502 && abbrev->tag != DW_TAG_member)
9503 {
9504 /* Otherwise we skip to the next sibling, if any. */
9505 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9506 continue;
9507 }
9508
9509 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9510 buffer, info_ptr, cu);
9511
9512 /* This two-pass algorithm for processing partial symbols has a
9513 high cost in cache pressure. Thus, handle some simple cases
9514 here which cover the majority of C partial symbols. DIEs
9515 which neither have specification tags in them, nor could have
9516 specification tags elsewhere pointing at them, can simply be
9517 processed and discarded.
9518
9519 This segment is also optional; scan_partial_symbols and
9520 add_partial_symbol will handle these DIEs if we chain
9521 them in normally. When compilers which do not emit large
9522 quantities of duplicate debug information are more common,
9523 this code can probably be removed. */
9524
9525 /* Any complete simple types at the top level (pretty much all
9526 of them, for a language without namespaces), can be processed
9527 directly. */
9528 if (parent_die == NULL
9529 && part_die->has_specification == 0
9530 && part_die->is_declaration == 0
9531 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9532 || part_die->tag == DW_TAG_base_type
9533 || part_die->tag == DW_TAG_subrange_type))
9534 {
9535 if (building_psymtab && part_die->name != NULL)
9536 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9537 VAR_DOMAIN, LOC_TYPEDEF,
9538 &objfile->static_psymbols,
9539 0, (CORE_ADDR) 0, cu->language, objfile);
9540 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9541 continue;
9542 }
9543
9544 /* The exception for DW_TAG_typedef with has_children above is
9545 a workaround of GCC PR debug/47510. In the case of this complaint
9546 type_name_no_tag_or_error will error on such types later.
9547
9548 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9549 it could not find the child DIEs referenced later, this is checked
9550 above. In correct DWARF DW_TAG_typedef should have no children. */
9551
9552 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9553 complaint (&symfile_complaints,
9554 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9555 "- DIE at 0x%x [in module %s]"),
9556 part_die->offset, objfile->name);
9557
9558 /* If we're at the second level, and we're an enumerator, and
9559 our parent has no specification (meaning possibly lives in a
9560 namespace elsewhere), then we can add the partial symbol now
9561 instead of queueing it. */
9562 if (part_die->tag == DW_TAG_enumerator
9563 && parent_die != NULL
9564 && parent_die->die_parent == NULL
9565 && parent_die->tag == DW_TAG_enumeration_type
9566 && parent_die->has_specification == 0)
9567 {
9568 if (part_die->name == NULL)
9569 complaint (&symfile_complaints,
9570 _("malformed enumerator DIE ignored"));
9571 else if (building_psymtab)
9572 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9573 VAR_DOMAIN, LOC_CONST,
9574 (cu->language == language_cplus
9575 || cu->language == language_java)
9576 ? &objfile->global_psymbols
9577 : &objfile->static_psymbols,
9578 0, (CORE_ADDR) 0, cu->language, objfile);
9579
9580 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9581 continue;
9582 }
9583
9584 /* We'll save this DIE so link it in. */
9585 part_die->die_parent = parent_die;
9586 part_die->die_sibling = NULL;
9587 part_die->die_child = NULL;
9588
9589 if (last_die && last_die == parent_die)
9590 last_die->die_child = part_die;
9591 else if (last_die)
9592 last_die->die_sibling = part_die;
9593
9594 last_die = part_die;
9595
9596 if (first_die == NULL)
9597 first_die = part_die;
9598
9599 /* Maybe add the DIE to the hash table. Not all DIEs that we
9600 find interesting need to be in the hash table, because we
9601 also have the parent/sibling/child chains; only those that we
9602 might refer to by offset later during partial symbol reading.
9603
9604 For now this means things that might have be the target of a
9605 DW_AT_specification, DW_AT_abstract_origin, or
9606 DW_AT_extension. DW_AT_extension will refer only to
9607 namespaces; DW_AT_abstract_origin refers to functions (and
9608 many things under the function DIE, but we do not recurse
9609 into function DIEs during partial symbol reading) and
9610 possibly variables as well; DW_AT_specification refers to
9611 declarations. Declarations ought to have the DW_AT_declaration
9612 flag. It happens that GCC forgets to put it in sometimes, but
9613 only for functions, not for types.
9614
9615 Adding more things than necessary to the hash table is harmless
9616 except for the performance cost. Adding too few will result in
9617 wasted time in find_partial_die, when we reread the compilation
9618 unit with load_all_dies set. */
9619
9620 if (load_all
9621 || abbrev->tag == DW_TAG_constant
9622 || abbrev->tag == DW_TAG_subprogram
9623 || abbrev->tag == DW_TAG_variable
9624 || abbrev->tag == DW_TAG_namespace
9625 || part_die->is_declaration)
9626 {
9627 void **slot;
9628
9629 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9630 part_die->offset, INSERT);
9631 *slot = part_die;
9632 }
9633
9634 part_die = obstack_alloc (&cu->comp_unit_obstack,
9635 sizeof (struct partial_die_info));
9636
9637 /* For some DIEs we want to follow their children (if any). For C
9638 we have no reason to follow the children of structures; for other
9639 languages we have to, so that we can get at method physnames
9640 to infer fully qualified class names, for DW_AT_specification,
9641 and for C++ template arguments. For C++, we also look one level
9642 inside functions to find template arguments (if the name of the
9643 function does not already contain the template arguments).
9644
9645 For Ada, we need to scan the children of subprograms and lexical
9646 blocks as well because Ada allows the definition of nested
9647 entities that could be interesting for the debugger, such as
9648 nested subprograms for instance. */
9649 if (last_die->has_children
9650 && (load_all
9651 || last_die->tag == DW_TAG_namespace
9652 || last_die->tag == DW_TAG_module
9653 || last_die->tag == DW_TAG_enumeration_type
9654 || (cu->language == language_cplus
9655 && last_die->tag == DW_TAG_subprogram
9656 && (last_die->name == NULL
9657 || strchr (last_die->name, '<') == NULL))
9658 || (cu->language != language_c
9659 && (last_die->tag == DW_TAG_class_type
9660 || last_die->tag == DW_TAG_interface_type
9661 || last_die->tag == DW_TAG_structure_type
9662 || last_die->tag == DW_TAG_union_type))
9663 || (cu->language == language_ada
9664 && (last_die->tag == DW_TAG_subprogram
9665 || last_die->tag == DW_TAG_lexical_block))))
9666 {
9667 nesting_level++;
9668 parent_die = last_die;
9669 continue;
9670 }
9671
9672 /* Otherwise we skip to the next sibling, if any. */
9673 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9674
9675 /* Back to the top, do it again. */
9676 }
9677 }
9678
9679 /* Read a minimal amount of information into the minimal die structure. */
9680
9681 static gdb_byte *
9682 read_partial_die (struct partial_die_info *part_die,
9683 struct abbrev_info *abbrev,
9684 unsigned int abbrev_len, bfd *abfd,
9685 gdb_byte *buffer, gdb_byte *info_ptr,
9686 struct dwarf2_cu *cu)
9687 {
9688 struct objfile *objfile = cu->objfile;
9689 unsigned int i;
9690 struct attribute attr;
9691 int has_low_pc_attr = 0;
9692 int has_high_pc_attr = 0;
9693
9694 memset (part_die, 0, sizeof (struct partial_die_info));
9695
9696 part_die->offset = info_ptr - buffer;
9697
9698 info_ptr += abbrev_len;
9699
9700 if (abbrev == NULL)
9701 return info_ptr;
9702
9703 part_die->tag = abbrev->tag;
9704 part_die->has_children = abbrev->has_children;
9705
9706 for (i = 0; i < abbrev->num_attrs; ++i)
9707 {
9708 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9709
9710 /* Store the data if it is of an attribute we want to keep in a
9711 partial symbol table. */
9712 switch (attr.name)
9713 {
9714 case DW_AT_name:
9715 switch (part_die->tag)
9716 {
9717 case DW_TAG_compile_unit:
9718 case DW_TAG_type_unit:
9719 /* Compilation units have a DW_AT_name that is a filename, not
9720 a source language identifier. */
9721 case DW_TAG_enumeration_type:
9722 case DW_TAG_enumerator:
9723 /* These tags always have simple identifiers already; no need
9724 to canonicalize them. */
9725 part_die->name = DW_STRING (&attr);
9726 break;
9727 default:
9728 part_die->name
9729 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9730 &objfile->objfile_obstack);
9731 break;
9732 }
9733 break;
9734 case DW_AT_linkage_name:
9735 case DW_AT_MIPS_linkage_name:
9736 /* Note that both forms of linkage name might appear. We
9737 assume they will be the same, and we only store the last
9738 one we see. */
9739 if (cu->language == language_ada)
9740 part_die->name = DW_STRING (&attr);
9741 part_die->linkage_name = DW_STRING (&attr);
9742 break;
9743 case DW_AT_low_pc:
9744 has_low_pc_attr = 1;
9745 part_die->lowpc = DW_ADDR (&attr);
9746 break;
9747 case DW_AT_high_pc:
9748 has_high_pc_attr = 1;
9749 part_die->highpc = DW_ADDR (&attr);
9750 break;
9751 case DW_AT_location:
9752 /* Support the .debug_loc offsets. */
9753 if (attr_form_is_block (&attr))
9754 {
9755 part_die->locdesc = DW_BLOCK (&attr);
9756 }
9757 else if (attr_form_is_section_offset (&attr))
9758 {
9759 dwarf2_complex_location_expr_complaint ();
9760 }
9761 else
9762 {
9763 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9764 "partial symbol information");
9765 }
9766 break;
9767 case DW_AT_external:
9768 part_die->is_external = DW_UNSND (&attr);
9769 break;
9770 case DW_AT_declaration:
9771 part_die->is_declaration = DW_UNSND (&attr);
9772 break;
9773 case DW_AT_type:
9774 part_die->has_type = 1;
9775 break;
9776 case DW_AT_abstract_origin:
9777 case DW_AT_specification:
9778 case DW_AT_extension:
9779 part_die->has_specification = 1;
9780 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9781 break;
9782 case DW_AT_sibling:
9783 /* Ignore absolute siblings, they might point outside of
9784 the current compile unit. */
9785 if (attr.form == DW_FORM_ref_addr)
9786 complaint (&symfile_complaints,
9787 _("ignoring absolute DW_AT_sibling"));
9788 else
9789 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9790 break;
9791 case DW_AT_byte_size:
9792 part_die->has_byte_size = 1;
9793 break;
9794 case DW_AT_calling_convention:
9795 /* DWARF doesn't provide a way to identify a program's source-level
9796 entry point. DW_AT_calling_convention attributes are only meant
9797 to describe functions' calling conventions.
9798
9799 However, because it's a necessary piece of information in
9800 Fortran, and because DW_CC_program is the only piece of debugging
9801 information whose definition refers to a 'main program' at all,
9802 several compilers have begun marking Fortran main programs with
9803 DW_CC_program --- even when those functions use the standard
9804 calling conventions.
9805
9806 So until DWARF specifies a way to provide this information and
9807 compilers pick up the new representation, we'll support this
9808 practice. */
9809 if (DW_UNSND (&attr) == DW_CC_program
9810 && cu->language == language_fortran)
9811 {
9812 set_main_name (part_die->name);
9813
9814 /* As this DIE has a static linkage the name would be difficult
9815 to look up later. */
9816 language_of_main = language_fortran;
9817 }
9818 break;
9819 default:
9820 break;
9821 }
9822 }
9823
9824 if (has_low_pc_attr && has_high_pc_attr)
9825 {
9826 /* When using the GNU linker, .gnu.linkonce. sections are used to
9827 eliminate duplicate copies of functions and vtables and such.
9828 The linker will arbitrarily choose one and discard the others.
9829 The AT_*_pc values for such functions refer to local labels in
9830 these sections. If the section from that file was discarded, the
9831 labels are not in the output, so the relocs get a value of 0.
9832 If this is a discarded function, mark the pc bounds as invalid,
9833 so that GDB will ignore it. */
9834 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9835 {
9836 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9837
9838 complaint (&symfile_complaints,
9839 _("DW_AT_low_pc %s is zero "
9840 "for DIE at 0x%x [in module %s]"),
9841 paddress (gdbarch, part_die->lowpc),
9842 part_die->offset, objfile->name);
9843 }
9844 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9845 else if (part_die->lowpc >= part_die->highpc)
9846 {
9847 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9848
9849 complaint (&symfile_complaints,
9850 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9851 "for DIE at 0x%x [in module %s]"),
9852 paddress (gdbarch, part_die->lowpc),
9853 paddress (gdbarch, part_die->highpc),
9854 part_die->offset, objfile->name);
9855 }
9856 else
9857 part_die->has_pc_info = 1;
9858 }
9859
9860 return info_ptr;
9861 }
9862
9863 /* Find a cached partial DIE at OFFSET in CU. */
9864
9865 static struct partial_die_info *
9866 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9867 {
9868 struct partial_die_info *lookup_die = NULL;
9869 struct partial_die_info part_die;
9870
9871 part_die.offset = offset;
9872 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9873
9874 return lookup_die;
9875 }
9876
9877 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9878 except in the case of .debug_types DIEs which do not reference
9879 outside their CU (they do however referencing other types via
9880 DW_FORM_ref_sig8). */
9881
9882 static struct partial_die_info *
9883 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9884 {
9885 struct objfile *objfile = cu->objfile;
9886 struct dwarf2_per_cu_data *per_cu = NULL;
9887 struct partial_die_info *pd = NULL;
9888
9889 if (cu->per_cu->debug_types_section)
9890 {
9891 pd = find_partial_die_in_comp_unit (offset, cu);
9892 if (pd != NULL)
9893 return pd;
9894 goto not_found;
9895 }
9896
9897 if (offset_in_cu_p (&cu->header, offset))
9898 {
9899 pd = find_partial_die_in_comp_unit (offset, cu);
9900 if (pd != NULL)
9901 return pd;
9902 }
9903
9904 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
9905
9906 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9907 load_partial_comp_unit (per_cu);
9908
9909 per_cu->cu->last_used = 0;
9910 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9911
9912 if (pd == NULL && per_cu->load_all_dies == 0)
9913 {
9914 struct cleanup *back_to;
9915 struct partial_die_info comp_unit_die;
9916 struct abbrev_info *abbrev;
9917 unsigned int bytes_read;
9918 char *info_ptr;
9919
9920 per_cu->load_all_dies = 1;
9921
9922 /* Re-read the DIEs. */
9923 back_to = make_cleanup (null_cleanup, 0);
9924 if (per_cu->cu->dwarf2_abbrevs == NULL)
9925 {
9926 dwarf2_read_abbrevs (per_cu->cu);
9927 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9928 }
9929 info_ptr = (dwarf2_per_objfile->info.buffer
9930 + per_cu->cu->header.offset
9931 + per_cu->cu->header.first_die_offset);
9932 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9933 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9934 objfile->obfd,
9935 dwarf2_per_objfile->info.buffer, info_ptr,
9936 per_cu->cu);
9937 if (comp_unit_die.has_children)
9938 load_partial_dies (objfile->obfd,
9939 dwarf2_per_objfile->info.buffer, info_ptr,
9940 0, per_cu->cu);
9941 do_cleanups (back_to);
9942
9943 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9944 }
9945
9946 not_found:
9947
9948 if (pd == NULL)
9949 internal_error (__FILE__, __LINE__,
9950 _("could not find partial DIE 0x%x "
9951 "in cache [from module %s]\n"),
9952 offset, bfd_get_filename (objfile->obfd));
9953 return pd;
9954 }
9955
9956 /* See if we can figure out if the class lives in a namespace. We do
9957 this by looking for a member function; its demangled name will
9958 contain namespace info, if there is any. */
9959
9960 static void
9961 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9962 struct dwarf2_cu *cu)
9963 {
9964 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9965 what template types look like, because the demangler
9966 frequently doesn't give the same name as the debug info. We
9967 could fix this by only using the demangled name to get the
9968 prefix (but see comment in read_structure_type). */
9969
9970 struct partial_die_info *real_pdi;
9971 struct partial_die_info *child_pdi;
9972
9973 /* If this DIE (this DIE's specification, if any) has a parent, then
9974 we should not do this. We'll prepend the parent's fully qualified
9975 name when we create the partial symbol. */
9976
9977 real_pdi = struct_pdi;
9978 while (real_pdi->has_specification)
9979 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9980
9981 if (real_pdi->die_parent != NULL)
9982 return;
9983
9984 for (child_pdi = struct_pdi->die_child;
9985 child_pdi != NULL;
9986 child_pdi = child_pdi->die_sibling)
9987 {
9988 if (child_pdi->tag == DW_TAG_subprogram
9989 && child_pdi->linkage_name != NULL)
9990 {
9991 char *actual_class_name
9992 = language_class_name_from_physname (cu->language_defn,
9993 child_pdi->linkage_name);
9994 if (actual_class_name != NULL)
9995 {
9996 struct_pdi->name
9997 = obsavestring (actual_class_name,
9998 strlen (actual_class_name),
9999 &cu->objfile->objfile_obstack);
10000 xfree (actual_class_name);
10001 }
10002 break;
10003 }
10004 }
10005 }
10006
10007 /* Adjust PART_DIE before generating a symbol for it. This function
10008 may set the is_external flag or change the DIE's name. */
10009
10010 static void
10011 fixup_partial_die (struct partial_die_info *part_die,
10012 struct dwarf2_cu *cu)
10013 {
10014 /* Once we've fixed up a die, there's no point in doing so again.
10015 This also avoids a memory leak if we were to call
10016 guess_partial_die_structure_name multiple times. */
10017 if (part_die->fixup_called)
10018 return;
10019
10020 /* If we found a reference attribute and the DIE has no name, try
10021 to find a name in the referred to DIE. */
10022
10023 if (part_die->name == NULL && part_die->has_specification)
10024 {
10025 struct partial_die_info *spec_die;
10026
10027 spec_die = find_partial_die (part_die->spec_offset, cu);
10028
10029 fixup_partial_die (spec_die, cu);
10030
10031 if (spec_die->name)
10032 {
10033 part_die->name = spec_die->name;
10034
10035 /* Copy DW_AT_external attribute if it is set. */
10036 if (spec_die->is_external)
10037 part_die->is_external = spec_die->is_external;
10038 }
10039 }
10040
10041 /* Set default names for some unnamed DIEs. */
10042
10043 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10044 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10045
10046 /* If there is no parent die to provide a namespace, and there are
10047 children, see if we can determine the namespace from their linkage
10048 name.
10049 NOTE: We need to do this even if cu->has_namespace_info != 0.
10050 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
10051 if (cu->language == language_cplus
10052 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10053 && part_die->die_parent == NULL
10054 && part_die->has_children
10055 && (part_die->tag == DW_TAG_class_type
10056 || part_die->tag == DW_TAG_structure_type
10057 || part_die->tag == DW_TAG_union_type))
10058 guess_partial_die_structure_name (part_die, cu);
10059
10060 /* GCC might emit a nameless struct or union that has a linkage
10061 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10062 if (part_die->name == NULL
10063 && (part_die->tag == DW_TAG_class_type
10064 || part_die->tag == DW_TAG_interface_type
10065 || part_die->tag == DW_TAG_structure_type
10066 || part_die->tag == DW_TAG_union_type)
10067 && part_die->linkage_name != NULL)
10068 {
10069 char *demangled;
10070
10071 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10072 if (demangled)
10073 {
10074 const char *base;
10075
10076 /* Strip any leading namespaces/classes, keep only the base name.
10077 DW_AT_name for named DIEs does not contain the prefixes. */
10078 base = strrchr (demangled, ':');
10079 if (base && base > demangled && base[-1] == ':')
10080 base++;
10081 else
10082 base = demangled;
10083
10084 part_die->name = obsavestring (base, strlen (base),
10085 &cu->objfile->objfile_obstack);
10086 xfree (demangled);
10087 }
10088 }
10089
10090 part_die->fixup_called = 1;
10091 }
10092
10093 /* Read an attribute value described by an attribute form. */
10094
10095 static gdb_byte *
10096 read_attribute_value (struct attribute *attr, unsigned form,
10097 bfd *abfd, gdb_byte *info_ptr,
10098 struct dwarf2_cu *cu)
10099 {
10100 struct comp_unit_head *cu_header = &cu->header;
10101 unsigned int bytes_read;
10102 struct dwarf_block *blk;
10103
10104 attr->form = form;
10105 switch (form)
10106 {
10107 case DW_FORM_ref_addr:
10108 if (cu->header.version == 2)
10109 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10110 else
10111 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10112 &cu->header, &bytes_read);
10113 info_ptr += bytes_read;
10114 break;
10115 case DW_FORM_addr:
10116 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10117 info_ptr += bytes_read;
10118 break;
10119 case DW_FORM_block2:
10120 blk = dwarf_alloc_block (cu);
10121 blk->size = read_2_bytes (abfd, info_ptr);
10122 info_ptr += 2;
10123 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10124 info_ptr += blk->size;
10125 DW_BLOCK (attr) = blk;
10126 break;
10127 case DW_FORM_block4:
10128 blk = dwarf_alloc_block (cu);
10129 blk->size = read_4_bytes (abfd, info_ptr);
10130 info_ptr += 4;
10131 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10132 info_ptr += blk->size;
10133 DW_BLOCK (attr) = blk;
10134 break;
10135 case DW_FORM_data2:
10136 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10137 info_ptr += 2;
10138 break;
10139 case DW_FORM_data4:
10140 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10141 info_ptr += 4;
10142 break;
10143 case DW_FORM_data8:
10144 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10145 info_ptr += 8;
10146 break;
10147 case DW_FORM_sec_offset:
10148 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10149 info_ptr += bytes_read;
10150 break;
10151 case DW_FORM_string:
10152 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10153 DW_STRING_IS_CANONICAL (attr) = 0;
10154 info_ptr += bytes_read;
10155 break;
10156 case DW_FORM_strp:
10157 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10158 &bytes_read);
10159 DW_STRING_IS_CANONICAL (attr) = 0;
10160 info_ptr += bytes_read;
10161 break;
10162 case DW_FORM_exprloc:
10163 case DW_FORM_block:
10164 blk = dwarf_alloc_block (cu);
10165 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10166 info_ptr += bytes_read;
10167 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10168 info_ptr += blk->size;
10169 DW_BLOCK (attr) = blk;
10170 break;
10171 case DW_FORM_block1:
10172 blk = dwarf_alloc_block (cu);
10173 blk->size = read_1_byte (abfd, info_ptr);
10174 info_ptr += 1;
10175 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10176 info_ptr += blk->size;
10177 DW_BLOCK (attr) = blk;
10178 break;
10179 case DW_FORM_data1:
10180 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10181 info_ptr += 1;
10182 break;
10183 case DW_FORM_flag:
10184 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10185 info_ptr += 1;
10186 break;
10187 case DW_FORM_flag_present:
10188 DW_UNSND (attr) = 1;
10189 break;
10190 case DW_FORM_sdata:
10191 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10192 info_ptr += bytes_read;
10193 break;
10194 case DW_FORM_udata:
10195 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10196 info_ptr += bytes_read;
10197 break;
10198 case DW_FORM_ref1:
10199 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10200 info_ptr += 1;
10201 break;
10202 case DW_FORM_ref2:
10203 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10204 info_ptr += 2;
10205 break;
10206 case DW_FORM_ref4:
10207 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10208 info_ptr += 4;
10209 break;
10210 case DW_FORM_ref8:
10211 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10212 info_ptr += 8;
10213 break;
10214 case DW_FORM_ref_sig8:
10215 /* Convert the signature to something we can record in DW_UNSND
10216 for later lookup.
10217 NOTE: This is NULL if the type wasn't found. */
10218 DW_SIGNATURED_TYPE (attr) =
10219 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10220 info_ptr += 8;
10221 break;
10222 case DW_FORM_ref_udata:
10223 DW_ADDR (attr) = (cu->header.offset
10224 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10225 info_ptr += bytes_read;
10226 break;
10227 case DW_FORM_indirect:
10228 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10229 info_ptr += bytes_read;
10230 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10231 break;
10232 default:
10233 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10234 dwarf_form_name (form),
10235 bfd_get_filename (abfd));
10236 }
10237
10238 /* We have seen instances where the compiler tried to emit a byte
10239 size attribute of -1 which ended up being encoded as an unsigned
10240 0xffffffff. Although 0xffffffff is technically a valid size value,
10241 an object of this size seems pretty unlikely so we can relatively
10242 safely treat these cases as if the size attribute was invalid and
10243 treat them as zero by default. */
10244 if (attr->name == DW_AT_byte_size
10245 && form == DW_FORM_data4
10246 && DW_UNSND (attr) >= 0xffffffff)
10247 {
10248 complaint
10249 (&symfile_complaints,
10250 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10251 hex_string (DW_UNSND (attr)));
10252 DW_UNSND (attr) = 0;
10253 }
10254
10255 return info_ptr;
10256 }
10257
10258 /* Read an attribute described by an abbreviated attribute. */
10259
10260 static gdb_byte *
10261 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10262 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10263 {
10264 attr->name = abbrev->name;
10265 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10266 }
10267
10268 /* Read dwarf information from a buffer. */
10269
10270 static unsigned int
10271 read_1_byte (bfd *abfd, gdb_byte *buf)
10272 {
10273 return bfd_get_8 (abfd, buf);
10274 }
10275
10276 static int
10277 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10278 {
10279 return bfd_get_signed_8 (abfd, buf);
10280 }
10281
10282 static unsigned int
10283 read_2_bytes (bfd *abfd, gdb_byte *buf)
10284 {
10285 return bfd_get_16 (abfd, buf);
10286 }
10287
10288 static int
10289 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10290 {
10291 return bfd_get_signed_16 (abfd, buf);
10292 }
10293
10294 static unsigned int
10295 read_4_bytes (bfd *abfd, gdb_byte *buf)
10296 {
10297 return bfd_get_32 (abfd, buf);
10298 }
10299
10300 static int
10301 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10302 {
10303 return bfd_get_signed_32 (abfd, buf);
10304 }
10305
10306 static ULONGEST
10307 read_8_bytes (bfd *abfd, gdb_byte *buf)
10308 {
10309 return bfd_get_64 (abfd, buf);
10310 }
10311
10312 static CORE_ADDR
10313 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10314 unsigned int *bytes_read)
10315 {
10316 struct comp_unit_head *cu_header = &cu->header;
10317 CORE_ADDR retval = 0;
10318
10319 if (cu_header->signed_addr_p)
10320 {
10321 switch (cu_header->addr_size)
10322 {
10323 case 2:
10324 retval = bfd_get_signed_16 (abfd, buf);
10325 break;
10326 case 4:
10327 retval = bfd_get_signed_32 (abfd, buf);
10328 break;
10329 case 8:
10330 retval = bfd_get_signed_64 (abfd, buf);
10331 break;
10332 default:
10333 internal_error (__FILE__, __LINE__,
10334 _("read_address: bad switch, signed [in module %s]"),
10335 bfd_get_filename (abfd));
10336 }
10337 }
10338 else
10339 {
10340 switch (cu_header->addr_size)
10341 {
10342 case 2:
10343 retval = bfd_get_16 (abfd, buf);
10344 break;
10345 case 4:
10346 retval = bfd_get_32 (abfd, buf);
10347 break;
10348 case 8:
10349 retval = bfd_get_64 (abfd, buf);
10350 break;
10351 default:
10352 internal_error (__FILE__, __LINE__,
10353 _("read_address: bad switch, "
10354 "unsigned [in module %s]"),
10355 bfd_get_filename (abfd));
10356 }
10357 }
10358
10359 *bytes_read = cu_header->addr_size;
10360 return retval;
10361 }
10362
10363 /* Read the initial length from a section. The (draft) DWARF 3
10364 specification allows the initial length to take up either 4 bytes
10365 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10366 bytes describe the length and all offsets will be 8 bytes in length
10367 instead of 4.
10368
10369 An older, non-standard 64-bit format is also handled by this
10370 function. The older format in question stores the initial length
10371 as an 8-byte quantity without an escape value. Lengths greater
10372 than 2^32 aren't very common which means that the initial 4 bytes
10373 is almost always zero. Since a length value of zero doesn't make
10374 sense for the 32-bit format, this initial zero can be considered to
10375 be an escape value which indicates the presence of the older 64-bit
10376 format. As written, the code can't detect (old format) lengths
10377 greater than 4GB. If it becomes necessary to handle lengths
10378 somewhat larger than 4GB, we could allow other small values (such
10379 as the non-sensical values of 1, 2, and 3) to also be used as
10380 escape values indicating the presence of the old format.
10381
10382 The value returned via bytes_read should be used to increment the
10383 relevant pointer after calling read_initial_length().
10384
10385 [ Note: read_initial_length() and read_offset() are based on the
10386 document entitled "DWARF Debugging Information Format", revision
10387 3, draft 8, dated November 19, 2001. This document was obtained
10388 from:
10389
10390 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10391
10392 This document is only a draft and is subject to change. (So beware.)
10393
10394 Details regarding the older, non-standard 64-bit format were
10395 determined empirically by examining 64-bit ELF files produced by
10396 the SGI toolchain on an IRIX 6.5 machine.
10397
10398 - Kevin, July 16, 2002
10399 ] */
10400
10401 static LONGEST
10402 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10403 {
10404 LONGEST length = bfd_get_32 (abfd, buf);
10405
10406 if (length == 0xffffffff)
10407 {
10408 length = bfd_get_64 (abfd, buf + 4);
10409 *bytes_read = 12;
10410 }
10411 else if (length == 0)
10412 {
10413 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10414 length = bfd_get_64 (abfd, buf);
10415 *bytes_read = 8;
10416 }
10417 else
10418 {
10419 *bytes_read = 4;
10420 }
10421
10422 return length;
10423 }
10424
10425 /* Cover function for read_initial_length.
10426 Returns the length of the object at BUF, and stores the size of the
10427 initial length in *BYTES_READ and stores the size that offsets will be in
10428 *OFFSET_SIZE.
10429 If the initial length size is not equivalent to that specified in
10430 CU_HEADER then issue a complaint.
10431 This is useful when reading non-comp-unit headers. */
10432
10433 static LONGEST
10434 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10435 const struct comp_unit_head *cu_header,
10436 unsigned int *bytes_read,
10437 unsigned int *offset_size)
10438 {
10439 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10440
10441 gdb_assert (cu_header->initial_length_size == 4
10442 || cu_header->initial_length_size == 8
10443 || cu_header->initial_length_size == 12);
10444
10445 if (cu_header->initial_length_size != *bytes_read)
10446 complaint (&symfile_complaints,
10447 _("intermixed 32-bit and 64-bit DWARF sections"));
10448
10449 *offset_size = (*bytes_read == 4) ? 4 : 8;
10450 return length;
10451 }
10452
10453 /* Read an offset from the data stream. The size of the offset is
10454 given by cu_header->offset_size. */
10455
10456 static LONGEST
10457 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10458 unsigned int *bytes_read)
10459 {
10460 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10461
10462 *bytes_read = cu_header->offset_size;
10463 return offset;
10464 }
10465
10466 /* Read an offset from the data stream. */
10467
10468 static LONGEST
10469 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10470 {
10471 LONGEST retval = 0;
10472
10473 switch (offset_size)
10474 {
10475 case 4:
10476 retval = bfd_get_32 (abfd, buf);
10477 break;
10478 case 8:
10479 retval = bfd_get_64 (abfd, buf);
10480 break;
10481 default:
10482 internal_error (__FILE__, __LINE__,
10483 _("read_offset_1: bad switch [in module %s]"),
10484 bfd_get_filename (abfd));
10485 }
10486
10487 return retval;
10488 }
10489
10490 static gdb_byte *
10491 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10492 {
10493 /* If the size of a host char is 8 bits, we can return a pointer
10494 to the buffer, otherwise we have to copy the data to a buffer
10495 allocated on the temporary obstack. */
10496 gdb_assert (HOST_CHAR_BIT == 8);
10497 return buf;
10498 }
10499
10500 static char *
10501 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10502 {
10503 /* If the size of a host char is 8 bits, we can return a pointer
10504 to the string, otherwise we have to copy the string to a buffer
10505 allocated on the temporary obstack. */
10506 gdb_assert (HOST_CHAR_BIT == 8);
10507 if (*buf == '\0')
10508 {
10509 *bytes_read_ptr = 1;
10510 return NULL;
10511 }
10512 *bytes_read_ptr = strlen ((char *) buf) + 1;
10513 return (char *) buf;
10514 }
10515
10516 static char *
10517 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10518 {
10519 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10520 if (dwarf2_per_objfile->str.buffer == NULL)
10521 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10522 bfd_get_filename (abfd));
10523 if (str_offset >= dwarf2_per_objfile->str.size)
10524 error (_("DW_FORM_strp pointing outside of "
10525 ".debug_str section [in module %s]"),
10526 bfd_get_filename (abfd));
10527 gdb_assert (HOST_CHAR_BIT == 8);
10528 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10529 return NULL;
10530 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10531 }
10532
10533 static char *
10534 read_indirect_string (bfd *abfd, gdb_byte *buf,
10535 const struct comp_unit_head *cu_header,
10536 unsigned int *bytes_read_ptr)
10537 {
10538 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10539
10540 return read_indirect_string_at_offset (abfd, str_offset);
10541 }
10542
10543 static unsigned long
10544 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10545 {
10546 unsigned long result;
10547 unsigned int num_read;
10548 int i, shift;
10549 unsigned char byte;
10550
10551 result = 0;
10552 shift = 0;
10553 num_read = 0;
10554 i = 0;
10555 while (1)
10556 {
10557 byte = bfd_get_8 (abfd, buf);
10558 buf++;
10559 num_read++;
10560 result |= ((unsigned long)(byte & 127) << shift);
10561 if ((byte & 128) == 0)
10562 {
10563 break;
10564 }
10565 shift += 7;
10566 }
10567 *bytes_read_ptr = num_read;
10568 return result;
10569 }
10570
10571 static long
10572 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10573 {
10574 long result;
10575 int i, shift, num_read;
10576 unsigned char byte;
10577
10578 result = 0;
10579 shift = 0;
10580 num_read = 0;
10581 i = 0;
10582 while (1)
10583 {
10584 byte = bfd_get_8 (abfd, buf);
10585 buf++;
10586 num_read++;
10587 result |= ((long)(byte & 127) << shift);
10588 shift += 7;
10589 if ((byte & 128) == 0)
10590 {
10591 break;
10592 }
10593 }
10594 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10595 result |= -(((long)1) << shift);
10596 *bytes_read_ptr = num_read;
10597 return result;
10598 }
10599
10600 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10601
10602 static gdb_byte *
10603 skip_leb128 (bfd *abfd, gdb_byte *buf)
10604 {
10605 int byte;
10606
10607 while (1)
10608 {
10609 byte = bfd_get_8 (abfd, buf);
10610 buf++;
10611 if ((byte & 128) == 0)
10612 return buf;
10613 }
10614 }
10615
10616 static void
10617 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10618 {
10619 switch (lang)
10620 {
10621 case DW_LANG_C89:
10622 case DW_LANG_C99:
10623 case DW_LANG_C:
10624 cu->language = language_c;
10625 break;
10626 case DW_LANG_C_plus_plus:
10627 cu->language = language_cplus;
10628 break;
10629 case DW_LANG_D:
10630 cu->language = language_d;
10631 break;
10632 case DW_LANG_Fortran77:
10633 case DW_LANG_Fortran90:
10634 case DW_LANG_Fortran95:
10635 cu->language = language_fortran;
10636 break;
10637 case DW_LANG_Mips_Assembler:
10638 cu->language = language_asm;
10639 break;
10640 case DW_LANG_Java:
10641 cu->language = language_java;
10642 break;
10643 case DW_LANG_Ada83:
10644 case DW_LANG_Ada95:
10645 cu->language = language_ada;
10646 break;
10647 case DW_LANG_Modula2:
10648 cu->language = language_m2;
10649 break;
10650 case DW_LANG_Pascal83:
10651 cu->language = language_pascal;
10652 break;
10653 case DW_LANG_ObjC:
10654 cu->language = language_objc;
10655 break;
10656 case DW_LANG_Cobol74:
10657 case DW_LANG_Cobol85:
10658 default:
10659 cu->language = language_minimal;
10660 break;
10661 }
10662 cu->language_defn = language_def (cu->language);
10663 }
10664
10665 /* Return the named attribute or NULL if not there. */
10666
10667 static struct attribute *
10668 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10669 {
10670 unsigned int i;
10671 struct attribute *spec = NULL;
10672
10673 for (i = 0; i < die->num_attrs; ++i)
10674 {
10675 if (die->attrs[i].name == name)
10676 return &die->attrs[i];
10677 if (die->attrs[i].name == DW_AT_specification
10678 || die->attrs[i].name == DW_AT_abstract_origin)
10679 spec = &die->attrs[i];
10680 }
10681
10682 if (spec)
10683 {
10684 die = follow_die_ref (die, spec, &cu);
10685 return dwarf2_attr (die, name, cu);
10686 }
10687
10688 return NULL;
10689 }
10690
10691 /* Return the named attribute or NULL if not there,
10692 but do not follow DW_AT_specification, etc.
10693 This is for use in contexts where we're reading .debug_types dies.
10694 Following DW_AT_specification, DW_AT_abstract_origin will take us
10695 back up the chain, and we want to go down. */
10696
10697 static struct attribute *
10698 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10699 struct dwarf2_cu *cu)
10700 {
10701 unsigned int i;
10702
10703 for (i = 0; i < die->num_attrs; ++i)
10704 if (die->attrs[i].name == name)
10705 return &die->attrs[i];
10706
10707 return NULL;
10708 }
10709
10710 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10711 and holds a non-zero value. This function should only be used for
10712 DW_FORM_flag or DW_FORM_flag_present attributes. */
10713
10714 static int
10715 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10716 {
10717 struct attribute *attr = dwarf2_attr (die, name, cu);
10718
10719 return (attr && DW_UNSND (attr));
10720 }
10721
10722 static int
10723 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10724 {
10725 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10726 which value is non-zero. However, we have to be careful with
10727 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10728 (via dwarf2_flag_true_p) follows this attribute. So we may
10729 end up accidently finding a declaration attribute that belongs
10730 to a different DIE referenced by the specification attribute,
10731 even though the given DIE does not have a declaration attribute. */
10732 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10733 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10734 }
10735
10736 /* Return the die giving the specification for DIE, if there is
10737 one. *SPEC_CU is the CU containing DIE on input, and the CU
10738 containing the return value on output. If there is no
10739 specification, but there is an abstract origin, that is
10740 returned. */
10741
10742 static struct die_info *
10743 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10744 {
10745 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10746 *spec_cu);
10747
10748 if (spec_attr == NULL)
10749 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10750
10751 if (spec_attr == NULL)
10752 return NULL;
10753 else
10754 return follow_die_ref (die, spec_attr, spec_cu);
10755 }
10756
10757 /* Free the line_header structure *LH, and any arrays and strings it
10758 refers to.
10759 NOTE: This is also used as a "cleanup" function. */
10760
10761 static void
10762 free_line_header (struct line_header *lh)
10763 {
10764 if (lh->standard_opcode_lengths)
10765 xfree (lh->standard_opcode_lengths);
10766
10767 /* Remember that all the lh->file_names[i].name pointers are
10768 pointers into debug_line_buffer, and don't need to be freed. */
10769 if (lh->file_names)
10770 xfree (lh->file_names);
10771
10772 /* Similarly for the include directory names. */
10773 if (lh->include_dirs)
10774 xfree (lh->include_dirs);
10775
10776 xfree (lh);
10777 }
10778
10779 /* Add an entry to LH's include directory table. */
10780
10781 static void
10782 add_include_dir (struct line_header *lh, char *include_dir)
10783 {
10784 /* Grow the array if necessary. */
10785 if (lh->include_dirs_size == 0)
10786 {
10787 lh->include_dirs_size = 1; /* for testing */
10788 lh->include_dirs = xmalloc (lh->include_dirs_size
10789 * sizeof (*lh->include_dirs));
10790 }
10791 else if (lh->num_include_dirs >= lh->include_dirs_size)
10792 {
10793 lh->include_dirs_size *= 2;
10794 lh->include_dirs = xrealloc (lh->include_dirs,
10795 (lh->include_dirs_size
10796 * sizeof (*lh->include_dirs)));
10797 }
10798
10799 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10800 }
10801
10802 /* Add an entry to LH's file name table. */
10803
10804 static void
10805 add_file_name (struct line_header *lh,
10806 char *name,
10807 unsigned int dir_index,
10808 unsigned int mod_time,
10809 unsigned int length)
10810 {
10811 struct file_entry *fe;
10812
10813 /* Grow the array if necessary. */
10814 if (lh->file_names_size == 0)
10815 {
10816 lh->file_names_size = 1; /* for testing */
10817 lh->file_names = xmalloc (lh->file_names_size
10818 * sizeof (*lh->file_names));
10819 }
10820 else if (lh->num_file_names >= lh->file_names_size)
10821 {
10822 lh->file_names_size *= 2;
10823 lh->file_names = xrealloc (lh->file_names,
10824 (lh->file_names_size
10825 * sizeof (*lh->file_names)));
10826 }
10827
10828 fe = &lh->file_names[lh->num_file_names++];
10829 fe->name = name;
10830 fe->dir_index = dir_index;
10831 fe->mod_time = mod_time;
10832 fe->length = length;
10833 fe->included_p = 0;
10834 fe->symtab = NULL;
10835 }
10836
10837 /* Read the statement program header starting at OFFSET in
10838 .debug_line, according to the endianness of ABFD. Return a pointer
10839 to a struct line_header, allocated using xmalloc.
10840
10841 NOTE: the strings in the include directory and file name tables of
10842 the returned object point into debug_line_buffer, and must not be
10843 freed. */
10844
10845 static struct line_header *
10846 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10847 struct dwarf2_cu *cu)
10848 {
10849 struct cleanup *back_to;
10850 struct line_header *lh;
10851 gdb_byte *line_ptr;
10852 unsigned int bytes_read, offset_size;
10853 int i;
10854 char *cur_dir, *cur_file;
10855
10856 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10857 if (dwarf2_per_objfile->line.buffer == NULL)
10858 {
10859 complaint (&symfile_complaints, _("missing .debug_line section"));
10860 return 0;
10861 }
10862
10863 /* Make sure that at least there's room for the total_length field.
10864 That could be 12 bytes long, but we're just going to fudge that. */
10865 if (offset + 4 >= dwarf2_per_objfile->line.size)
10866 {
10867 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10868 return 0;
10869 }
10870
10871 lh = xmalloc (sizeof (*lh));
10872 memset (lh, 0, sizeof (*lh));
10873 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10874 (void *) lh);
10875
10876 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10877
10878 /* Read in the header. */
10879 lh->total_length =
10880 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10881 &bytes_read, &offset_size);
10882 line_ptr += bytes_read;
10883 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10884 + dwarf2_per_objfile->line.size))
10885 {
10886 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10887 return 0;
10888 }
10889 lh->statement_program_end = line_ptr + lh->total_length;
10890 lh->version = read_2_bytes (abfd, line_ptr);
10891 line_ptr += 2;
10892 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10893 line_ptr += offset_size;
10894 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10895 line_ptr += 1;
10896 if (lh->version >= 4)
10897 {
10898 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10899 line_ptr += 1;
10900 }
10901 else
10902 lh->maximum_ops_per_instruction = 1;
10903
10904 if (lh->maximum_ops_per_instruction == 0)
10905 {
10906 lh->maximum_ops_per_instruction = 1;
10907 complaint (&symfile_complaints,
10908 _("invalid maximum_ops_per_instruction "
10909 "in `.debug_line' section"));
10910 }
10911
10912 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10913 line_ptr += 1;
10914 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10915 line_ptr += 1;
10916 lh->line_range = read_1_byte (abfd, line_ptr);
10917 line_ptr += 1;
10918 lh->opcode_base = read_1_byte (abfd, line_ptr);
10919 line_ptr += 1;
10920 lh->standard_opcode_lengths
10921 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10922
10923 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10924 for (i = 1; i < lh->opcode_base; ++i)
10925 {
10926 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10927 line_ptr += 1;
10928 }
10929
10930 /* Read directory table. */
10931 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10932 {
10933 line_ptr += bytes_read;
10934 add_include_dir (lh, cur_dir);
10935 }
10936 line_ptr += bytes_read;
10937
10938 /* Read file name table. */
10939 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10940 {
10941 unsigned int dir_index, mod_time, length;
10942
10943 line_ptr += bytes_read;
10944 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10945 line_ptr += bytes_read;
10946 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10947 line_ptr += bytes_read;
10948 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10949 line_ptr += bytes_read;
10950
10951 add_file_name (lh, cur_file, dir_index, mod_time, length);
10952 }
10953 line_ptr += bytes_read;
10954 lh->statement_program_start = line_ptr;
10955
10956 if (line_ptr > (dwarf2_per_objfile->line.buffer
10957 + dwarf2_per_objfile->line.size))
10958 complaint (&symfile_complaints,
10959 _("line number info header doesn't "
10960 "fit in `.debug_line' section"));
10961
10962 discard_cleanups (back_to);
10963 return lh;
10964 }
10965
10966 /* Subroutine of dwarf_decode_lines to simplify it.
10967 Return the file name of the psymtab for included file FILE_INDEX
10968 in line header LH of PST.
10969 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10970 If space for the result is malloc'd, it will be freed by a cleanup.
10971 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10972
10973 static char *
10974 psymtab_include_file_name (const struct line_header *lh, int file_index,
10975 const struct partial_symtab *pst,
10976 const char *comp_dir)
10977 {
10978 const struct file_entry fe = lh->file_names [file_index];
10979 char *include_name = fe.name;
10980 char *include_name_to_compare = include_name;
10981 char *dir_name = NULL;
10982 const char *pst_filename;
10983 char *copied_name = NULL;
10984 int file_is_pst;
10985
10986 if (fe.dir_index)
10987 dir_name = lh->include_dirs[fe.dir_index - 1];
10988
10989 if (!IS_ABSOLUTE_PATH (include_name)
10990 && (dir_name != NULL || comp_dir != NULL))
10991 {
10992 /* Avoid creating a duplicate psymtab for PST.
10993 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10994 Before we do the comparison, however, we need to account
10995 for DIR_NAME and COMP_DIR.
10996 First prepend dir_name (if non-NULL). If we still don't
10997 have an absolute path prepend comp_dir (if non-NULL).
10998 However, the directory we record in the include-file's
10999 psymtab does not contain COMP_DIR (to match the
11000 corresponding symtab(s)).
11001
11002 Example:
11003
11004 bash$ cd /tmp
11005 bash$ gcc -g ./hello.c
11006 include_name = "hello.c"
11007 dir_name = "."
11008 DW_AT_comp_dir = comp_dir = "/tmp"
11009 DW_AT_name = "./hello.c" */
11010
11011 if (dir_name != NULL)
11012 {
11013 include_name = concat (dir_name, SLASH_STRING,
11014 include_name, (char *)NULL);
11015 include_name_to_compare = include_name;
11016 make_cleanup (xfree, include_name);
11017 }
11018 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11019 {
11020 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11021 include_name, (char *)NULL);
11022 }
11023 }
11024
11025 pst_filename = pst->filename;
11026 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11027 {
11028 copied_name = concat (pst->dirname, SLASH_STRING,
11029 pst_filename, (char *)NULL);
11030 pst_filename = copied_name;
11031 }
11032
11033 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11034
11035 if (include_name_to_compare != include_name)
11036 xfree (include_name_to_compare);
11037 if (copied_name != NULL)
11038 xfree (copied_name);
11039
11040 if (file_is_pst)
11041 return NULL;
11042 return include_name;
11043 }
11044
11045 /* Ignore this record_line request. */
11046
11047 static void
11048 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11049 {
11050 return;
11051 }
11052
11053 /* Decode the Line Number Program (LNP) for the given line_header
11054 structure and CU. The actual information extracted and the type
11055 of structures created from the LNP depends on the value of PST.
11056
11057 1. If PST is NULL, then this procedure uses the data from the program
11058 to create all necessary symbol tables, and their linetables.
11059
11060 2. If PST is not NULL, this procedure reads the program to determine
11061 the list of files included by the unit represented by PST, and
11062 builds all the associated partial symbol tables.
11063
11064 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11065 It is used for relative paths in the line table.
11066 NOTE: When processing partial symtabs (pst != NULL),
11067 comp_dir == pst->dirname.
11068
11069 NOTE: It is important that psymtabs have the same file name (via strcmp)
11070 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11071 symtab we don't use it in the name of the psymtabs we create.
11072 E.g. expand_line_sal requires this when finding psymtabs to expand.
11073 A good testcase for this is mb-inline.exp. */
11074
11075 static void
11076 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
11077 struct dwarf2_cu *cu, struct partial_symtab *pst)
11078 {
11079 gdb_byte *line_ptr, *extended_end;
11080 gdb_byte *line_end;
11081 unsigned int bytes_read, extended_len;
11082 unsigned char op_code, extended_op, adj_opcode;
11083 CORE_ADDR baseaddr;
11084 struct objfile *objfile = cu->objfile;
11085 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11086 const int decode_for_pst_p = (pst != NULL);
11087 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
11088 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11089 = record_line;
11090
11091 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11092
11093 line_ptr = lh->statement_program_start;
11094 line_end = lh->statement_program_end;
11095
11096 /* Read the statement sequences until there's nothing left. */
11097 while (line_ptr < line_end)
11098 {
11099 /* state machine registers */
11100 CORE_ADDR address = 0;
11101 unsigned int file = 1;
11102 unsigned int line = 1;
11103 unsigned int column = 0;
11104 int is_stmt = lh->default_is_stmt;
11105 int basic_block = 0;
11106 int end_sequence = 0;
11107 CORE_ADDR addr;
11108 unsigned char op_index = 0;
11109
11110 if (!decode_for_pst_p && lh->num_file_names >= file)
11111 {
11112 /* Start a subfile for the current file of the state machine. */
11113 /* lh->include_dirs and lh->file_names are 0-based, but the
11114 directory and file name numbers in the statement program
11115 are 1-based. */
11116 struct file_entry *fe = &lh->file_names[file - 1];
11117 char *dir = NULL;
11118
11119 if (fe->dir_index)
11120 dir = lh->include_dirs[fe->dir_index - 1];
11121
11122 dwarf2_start_subfile (fe->name, dir, comp_dir);
11123 }
11124
11125 /* Decode the table. */
11126 while (!end_sequence)
11127 {
11128 op_code = read_1_byte (abfd, line_ptr);
11129 line_ptr += 1;
11130 if (line_ptr > line_end)
11131 {
11132 dwarf2_debug_line_missing_end_sequence_complaint ();
11133 break;
11134 }
11135
11136 if (op_code >= lh->opcode_base)
11137 {
11138 /* Special operand. */
11139 adj_opcode = op_code - lh->opcode_base;
11140 address += (((op_index + (adj_opcode / lh->line_range))
11141 / lh->maximum_ops_per_instruction)
11142 * lh->minimum_instruction_length);
11143 op_index = ((op_index + (adj_opcode / lh->line_range))
11144 % lh->maximum_ops_per_instruction);
11145 line += lh->line_base + (adj_opcode % lh->line_range);
11146 if (lh->num_file_names < file || file == 0)
11147 dwarf2_debug_line_missing_file_complaint ();
11148 /* For now we ignore lines not starting on an
11149 instruction boundary. */
11150 else if (op_index == 0)
11151 {
11152 lh->file_names[file - 1].included_p = 1;
11153 if (!decode_for_pst_p && is_stmt)
11154 {
11155 if (last_subfile != current_subfile)
11156 {
11157 addr = gdbarch_addr_bits_remove (gdbarch, address);
11158 if (last_subfile)
11159 (*p_record_line) (last_subfile, 0, addr);
11160 last_subfile = current_subfile;
11161 }
11162 /* Append row to matrix using current values. */
11163 addr = gdbarch_addr_bits_remove (gdbarch, address);
11164 (*p_record_line) (current_subfile, line, addr);
11165 }
11166 }
11167 basic_block = 0;
11168 }
11169 else switch (op_code)
11170 {
11171 case DW_LNS_extended_op:
11172 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11173 &bytes_read);
11174 line_ptr += bytes_read;
11175 extended_end = line_ptr + extended_len;
11176 extended_op = read_1_byte (abfd, line_ptr);
11177 line_ptr += 1;
11178 switch (extended_op)
11179 {
11180 case DW_LNE_end_sequence:
11181 p_record_line = record_line;
11182 end_sequence = 1;
11183 break;
11184 case DW_LNE_set_address:
11185 address = read_address (abfd, line_ptr, cu, &bytes_read);
11186
11187 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11188 {
11189 /* This line table is for a function which has been
11190 GCd by the linker. Ignore it. PR gdb/12528 */
11191
11192 long line_offset
11193 = line_ptr - dwarf2_per_objfile->line.buffer;
11194
11195 complaint (&symfile_complaints,
11196 _(".debug_line address at offset 0x%lx is 0 "
11197 "[in module %s]"),
11198 line_offset, objfile->name);
11199 p_record_line = noop_record_line;
11200 }
11201
11202 op_index = 0;
11203 line_ptr += bytes_read;
11204 address += baseaddr;
11205 break;
11206 case DW_LNE_define_file:
11207 {
11208 char *cur_file;
11209 unsigned int dir_index, mod_time, length;
11210
11211 cur_file = read_direct_string (abfd, line_ptr,
11212 &bytes_read);
11213 line_ptr += bytes_read;
11214 dir_index =
11215 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11216 line_ptr += bytes_read;
11217 mod_time =
11218 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11219 line_ptr += bytes_read;
11220 length =
11221 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11222 line_ptr += bytes_read;
11223 add_file_name (lh, cur_file, dir_index, mod_time, length);
11224 }
11225 break;
11226 case DW_LNE_set_discriminator:
11227 /* The discriminator is not interesting to the debugger;
11228 just ignore it. */
11229 line_ptr = extended_end;
11230 break;
11231 default:
11232 complaint (&symfile_complaints,
11233 _("mangled .debug_line section"));
11234 return;
11235 }
11236 /* Make sure that we parsed the extended op correctly. If e.g.
11237 we expected a different address size than the producer used,
11238 we may have read the wrong number of bytes. */
11239 if (line_ptr != extended_end)
11240 {
11241 complaint (&symfile_complaints,
11242 _("mangled .debug_line section"));
11243 return;
11244 }
11245 break;
11246 case DW_LNS_copy:
11247 if (lh->num_file_names < file || file == 0)
11248 dwarf2_debug_line_missing_file_complaint ();
11249 else
11250 {
11251 lh->file_names[file - 1].included_p = 1;
11252 if (!decode_for_pst_p && is_stmt)
11253 {
11254 if (last_subfile != current_subfile)
11255 {
11256 addr = gdbarch_addr_bits_remove (gdbarch, address);
11257 if (last_subfile)
11258 (*p_record_line) (last_subfile, 0, addr);
11259 last_subfile = current_subfile;
11260 }
11261 addr = gdbarch_addr_bits_remove (gdbarch, address);
11262 (*p_record_line) (current_subfile, line, addr);
11263 }
11264 }
11265 basic_block = 0;
11266 break;
11267 case DW_LNS_advance_pc:
11268 {
11269 CORE_ADDR adjust
11270 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11271
11272 address += (((op_index + adjust)
11273 / lh->maximum_ops_per_instruction)
11274 * lh->minimum_instruction_length);
11275 op_index = ((op_index + adjust)
11276 % lh->maximum_ops_per_instruction);
11277 line_ptr += bytes_read;
11278 }
11279 break;
11280 case DW_LNS_advance_line:
11281 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11282 line_ptr += bytes_read;
11283 break;
11284 case DW_LNS_set_file:
11285 {
11286 /* The arrays lh->include_dirs and lh->file_names are
11287 0-based, but the directory and file name numbers in
11288 the statement program are 1-based. */
11289 struct file_entry *fe;
11290 char *dir = NULL;
11291
11292 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11293 line_ptr += bytes_read;
11294 if (lh->num_file_names < file || file == 0)
11295 dwarf2_debug_line_missing_file_complaint ();
11296 else
11297 {
11298 fe = &lh->file_names[file - 1];
11299 if (fe->dir_index)
11300 dir = lh->include_dirs[fe->dir_index - 1];
11301 if (!decode_for_pst_p)
11302 {
11303 last_subfile = current_subfile;
11304 dwarf2_start_subfile (fe->name, dir, comp_dir);
11305 }
11306 }
11307 }
11308 break;
11309 case DW_LNS_set_column:
11310 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11311 line_ptr += bytes_read;
11312 break;
11313 case DW_LNS_negate_stmt:
11314 is_stmt = (!is_stmt);
11315 break;
11316 case DW_LNS_set_basic_block:
11317 basic_block = 1;
11318 break;
11319 /* Add to the address register of the state machine the
11320 address increment value corresponding to special opcode
11321 255. I.e., this value is scaled by the minimum
11322 instruction length since special opcode 255 would have
11323 scaled the increment. */
11324 case DW_LNS_const_add_pc:
11325 {
11326 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11327
11328 address += (((op_index + adjust)
11329 / lh->maximum_ops_per_instruction)
11330 * lh->minimum_instruction_length);
11331 op_index = ((op_index + adjust)
11332 % lh->maximum_ops_per_instruction);
11333 }
11334 break;
11335 case DW_LNS_fixed_advance_pc:
11336 address += read_2_bytes (abfd, line_ptr);
11337 op_index = 0;
11338 line_ptr += 2;
11339 break;
11340 default:
11341 {
11342 /* Unknown standard opcode, ignore it. */
11343 int i;
11344
11345 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11346 {
11347 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11348 line_ptr += bytes_read;
11349 }
11350 }
11351 }
11352 }
11353 if (lh->num_file_names < file || file == 0)
11354 dwarf2_debug_line_missing_file_complaint ();
11355 else
11356 {
11357 lh->file_names[file - 1].included_p = 1;
11358 if (!decode_for_pst_p)
11359 {
11360 addr = gdbarch_addr_bits_remove (gdbarch, address);
11361 (*p_record_line) (current_subfile, 0, addr);
11362 }
11363 }
11364 }
11365
11366 if (decode_for_pst_p)
11367 {
11368 int file_index;
11369
11370 /* Now that we're done scanning the Line Header Program, we can
11371 create the psymtab of each included file. */
11372 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11373 if (lh->file_names[file_index].included_p == 1)
11374 {
11375 char *include_name =
11376 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11377 if (include_name != NULL)
11378 dwarf2_create_include_psymtab (include_name, pst, objfile);
11379 }
11380 }
11381 else
11382 {
11383 /* Make sure a symtab is created for every file, even files
11384 which contain only variables (i.e. no code with associated
11385 line numbers). */
11386
11387 int i;
11388 struct file_entry *fe;
11389
11390 for (i = 0; i < lh->num_file_names; i++)
11391 {
11392 char *dir = NULL;
11393
11394 fe = &lh->file_names[i];
11395 if (fe->dir_index)
11396 dir = lh->include_dirs[fe->dir_index - 1];
11397 dwarf2_start_subfile (fe->name, dir, comp_dir);
11398
11399 /* Skip the main file; we don't need it, and it must be
11400 allocated last, so that it will show up before the
11401 non-primary symtabs in the objfile's symtab list. */
11402 if (current_subfile == first_subfile)
11403 continue;
11404
11405 if (current_subfile->symtab == NULL)
11406 current_subfile->symtab = allocate_symtab (current_subfile->name,
11407 objfile);
11408 fe->symtab = current_subfile->symtab;
11409 }
11410 }
11411 }
11412
11413 /* Start a subfile for DWARF. FILENAME is the name of the file and
11414 DIRNAME the name of the source directory which contains FILENAME
11415 or NULL if not known. COMP_DIR is the compilation directory for the
11416 linetable's compilation unit or NULL if not known.
11417 This routine tries to keep line numbers from identical absolute and
11418 relative file names in a common subfile.
11419
11420 Using the `list' example from the GDB testsuite, which resides in
11421 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11422 of /srcdir/list0.c yields the following debugging information for list0.c:
11423
11424 DW_AT_name: /srcdir/list0.c
11425 DW_AT_comp_dir: /compdir
11426 files.files[0].name: list0.h
11427 files.files[0].dir: /srcdir
11428 files.files[1].name: list0.c
11429 files.files[1].dir: /srcdir
11430
11431 The line number information for list0.c has to end up in a single
11432 subfile, so that `break /srcdir/list0.c:1' works as expected.
11433 start_subfile will ensure that this happens provided that we pass the
11434 concatenation of files.files[1].dir and files.files[1].name as the
11435 subfile's name. */
11436
11437 static void
11438 dwarf2_start_subfile (char *filename, const char *dirname,
11439 const char *comp_dir)
11440 {
11441 char *fullname;
11442
11443 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11444 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11445 second argument to start_subfile. To be consistent, we do the
11446 same here. In order not to lose the line information directory,
11447 we concatenate it to the filename when it makes sense.
11448 Note that the Dwarf3 standard says (speaking of filenames in line
11449 information): ``The directory index is ignored for file names
11450 that represent full path names''. Thus ignoring dirname in the
11451 `else' branch below isn't an issue. */
11452
11453 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11454 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11455 else
11456 fullname = filename;
11457
11458 start_subfile (fullname, comp_dir);
11459
11460 if (fullname != filename)
11461 xfree (fullname);
11462 }
11463
11464 static void
11465 var_decode_location (struct attribute *attr, struct symbol *sym,
11466 struct dwarf2_cu *cu)
11467 {
11468 struct objfile *objfile = cu->objfile;
11469 struct comp_unit_head *cu_header = &cu->header;
11470
11471 /* NOTE drow/2003-01-30: There used to be a comment and some special
11472 code here to turn a symbol with DW_AT_external and a
11473 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11474 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11475 with some versions of binutils) where shared libraries could have
11476 relocations against symbols in their debug information - the
11477 minimal symbol would have the right address, but the debug info
11478 would not. It's no longer necessary, because we will explicitly
11479 apply relocations when we read in the debug information now. */
11480
11481 /* A DW_AT_location attribute with no contents indicates that a
11482 variable has been optimized away. */
11483 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11484 {
11485 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11486 return;
11487 }
11488
11489 /* Handle one degenerate form of location expression specially, to
11490 preserve GDB's previous behavior when section offsets are
11491 specified. If this is just a DW_OP_addr then mark this symbol
11492 as LOC_STATIC. */
11493
11494 if (attr_form_is_block (attr)
11495 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11496 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11497 {
11498 unsigned int dummy;
11499
11500 SYMBOL_VALUE_ADDRESS (sym) =
11501 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11502 SYMBOL_CLASS (sym) = LOC_STATIC;
11503 fixup_symbol_section (sym, objfile);
11504 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11505 SYMBOL_SECTION (sym));
11506 return;
11507 }
11508
11509 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11510 expression evaluator, and use LOC_COMPUTED only when necessary
11511 (i.e. when the value of a register or memory location is
11512 referenced, or a thread-local block, etc.). Then again, it might
11513 not be worthwhile. I'm assuming that it isn't unless performance
11514 or memory numbers show me otherwise. */
11515
11516 dwarf2_symbol_mark_computed (attr, sym, cu);
11517 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11518
11519 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11520 cu->has_loclist = 1;
11521 }
11522
11523 /* Given a pointer to a DWARF information entry, figure out if we need
11524 to make a symbol table entry for it, and if so, create a new entry
11525 and return a pointer to it.
11526 If TYPE is NULL, determine symbol type from the die, otherwise
11527 used the passed type.
11528 If SPACE is not NULL, use it to hold the new symbol. If it is
11529 NULL, allocate a new symbol on the objfile's obstack. */
11530
11531 static struct symbol *
11532 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11533 struct symbol *space)
11534 {
11535 struct objfile *objfile = cu->objfile;
11536 struct symbol *sym = NULL;
11537 char *name;
11538 struct attribute *attr = NULL;
11539 struct attribute *attr2 = NULL;
11540 CORE_ADDR baseaddr;
11541 struct pending **list_to_add = NULL;
11542
11543 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11544
11545 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11546
11547 name = dwarf2_name (die, cu);
11548 if (name)
11549 {
11550 const char *linkagename;
11551 int suppress_add = 0;
11552
11553 if (space)
11554 sym = space;
11555 else
11556 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11557 OBJSTAT (objfile, n_syms++);
11558
11559 /* Cache this symbol's name and the name's demangled form (if any). */
11560 SYMBOL_SET_LANGUAGE (sym, cu->language);
11561 linkagename = dwarf2_physname (name, die, cu);
11562 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11563
11564 /* Fortran does not have mangling standard and the mangling does differ
11565 between gfortran, iFort etc. */
11566 if (cu->language == language_fortran
11567 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11568 symbol_set_demangled_name (&(sym->ginfo),
11569 (char *) dwarf2_full_name (name, die, cu),
11570 NULL);
11571
11572 /* Default assumptions.
11573 Use the passed type or decode it from the die. */
11574 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11575 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11576 if (type != NULL)
11577 SYMBOL_TYPE (sym) = type;
11578 else
11579 SYMBOL_TYPE (sym) = die_type (die, cu);
11580 attr = dwarf2_attr (die,
11581 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11582 cu);
11583 if (attr)
11584 {
11585 SYMBOL_LINE (sym) = DW_UNSND (attr);
11586 }
11587
11588 attr = dwarf2_attr (die,
11589 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11590 cu);
11591 if (attr)
11592 {
11593 int file_index = DW_UNSND (attr);
11594
11595 if (cu->line_header == NULL
11596 || file_index > cu->line_header->num_file_names)
11597 complaint (&symfile_complaints,
11598 _("file index out of range"));
11599 else if (file_index > 0)
11600 {
11601 struct file_entry *fe;
11602
11603 fe = &cu->line_header->file_names[file_index - 1];
11604 SYMBOL_SYMTAB (sym) = fe->symtab;
11605 }
11606 }
11607
11608 switch (die->tag)
11609 {
11610 case DW_TAG_label:
11611 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11612 if (attr)
11613 {
11614 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11615 }
11616 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11617 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11618 SYMBOL_CLASS (sym) = LOC_LABEL;
11619 add_symbol_to_list (sym, cu->list_in_scope);
11620 break;
11621 case DW_TAG_subprogram:
11622 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11623 finish_block. */
11624 SYMBOL_CLASS (sym) = LOC_BLOCK;
11625 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11626 if ((attr2 && (DW_UNSND (attr2) != 0))
11627 || cu->language == language_ada)
11628 {
11629 /* Subprograms marked external are stored as a global symbol.
11630 Ada subprograms, whether marked external or not, are always
11631 stored as a global symbol, because we want to be able to
11632 access them globally. For instance, we want to be able
11633 to break on a nested subprogram without having to
11634 specify the context. */
11635 list_to_add = &global_symbols;
11636 }
11637 else
11638 {
11639 list_to_add = cu->list_in_scope;
11640 }
11641 break;
11642 case DW_TAG_inlined_subroutine:
11643 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11644 finish_block. */
11645 SYMBOL_CLASS (sym) = LOC_BLOCK;
11646 SYMBOL_INLINED (sym) = 1;
11647 /* Do not add the symbol to any lists. It will be found via
11648 BLOCK_FUNCTION from the blockvector. */
11649 break;
11650 case DW_TAG_template_value_param:
11651 suppress_add = 1;
11652 /* Fall through. */
11653 case DW_TAG_constant:
11654 case DW_TAG_variable:
11655 case DW_TAG_member:
11656 /* Compilation with minimal debug info may result in
11657 variables with missing type entries. Change the
11658 misleading `void' type to something sensible. */
11659 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11660 SYMBOL_TYPE (sym)
11661 = objfile_type (objfile)->nodebug_data_symbol;
11662
11663 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11664 /* In the case of DW_TAG_member, we should only be called for
11665 static const members. */
11666 if (die->tag == DW_TAG_member)
11667 {
11668 /* dwarf2_add_field uses die_is_declaration,
11669 so we do the same. */
11670 gdb_assert (die_is_declaration (die, cu));
11671 gdb_assert (attr);
11672 }
11673 if (attr)
11674 {
11675 dwarf2_const_value (attr, sym, cu);
11676 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11677 if (!suppress_add)
11678 {
11679 if (attr2 && (DW_UNSND (attr2) != 0))
11680 list_to_add = &global_symbols;
11681 else
11682 list_to_add = cu->list_in_scope;
11683 }
11684 break;
11685 }
11686 attr = dwarf2_attr (die, DW_AT_location, cu);
11687 if (attr)
11688 {
11689 var_decode_location (attr, sym, cu);
11690 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11691 if (SYMBOL_CLASS (sym) == LOC_STATIC
11692 && SYMBOL_VALUE_ADDRESS (sym) == 0
11693 && !dwarf2_per_objfile->has_section_at_zero)
11694 {
11695 /* When a static variable is eliminated by the linker,
11696 the corresponding debug information is not stripped
11697 out, but the variable address is set to null;
11698 do not add such variables into symbol table. */
11699 }
11700 else if (attr2 && (DW_UNSND (attr2) != 0))
11701 {
11702 /* Workaround gfortran PR debug/40040 - it uses
11703 DW_AT_location for variables in -fPIC libraries which may
11704 get overriden by other libraries/executable and get
11705 a different address. Resolve it by the minimal symbol
11706 which may come from inferior's executable using copy
11707 relocation. Make this workaround only for gfortran as for
11708 other compilers GDB cannot guess the minimal symbol
11709 Fortran mangling kind. */
11710 if (cu->language == language_fortran && die->parent
11711 && die->parent->tag == DW_TAG_module
11712 && cu->producer
11713 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11714 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11715
11716 /* A variable with DW_AT_external is never static,
11717 but it may be block-scoped. */
11718 list_to_add = (cu->list_in_scope == &file_symbols
11719 ? &global_symbols : cu->list_in_scope);
11720 }
11721 else
11722 list_to_add = cu->list_in_scope;
11723 }
11724 else
11725 {
11726 /* We do not know the address of this symbol.
11727 If it is an external symbol and we have type information
11728 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11729 The address of the variable will then be determined from
11730 the minimal symbol table whenever the variable is
11731 referenced. */
11732 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11733 if (attr2 && (DW_UNSND (attr2) != 0)
11734 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11735 {
11736 /* A variable with DW_AT_external is never static, but it
11737 may be block-scoped. */
11738 list_to_add = (cu->list_in_scope == &file_symbols
11739 ? &global_symbols : cu->list_in_scope);
11740
11741 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11742 }
11743 else if (!die_is_declaration (die, cu))
11744 {
11745 /* Use the default LOC_OPTIMIZED_OUT class. */
11746 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11747 if (!suppress_add)
11748 list_to_add = cu->list_in_scope;
11749 }
11750 }
11751 break;
11752 case DW_TAG_formal_parameter:
11753 /* If we are inside a function, mark this as an argument. If
11754 not, we might be looking at an argument to an inlined function
11755 when we do not have enough information to show inlined frames;
11756 pretend it's a local variable in that case so that the user can
11757 still see it. */
11758 if (context_stack_depth > 0
11759 && context_stack[context_stack_depth - 1].name != NULL)
11760 SYMBOL_IS_ARGUMENT (sym) = 1;
11761 attr = dwarf2_attr (die, DW_AT_location, cu);
11762 if (attr)
11763 {
11764 var_decode_location (attr, sym, cu);
11765 }
11766 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11767 if (attr)
11768 {
11769 dwarf2_const_value (attr, sym, cu);
11770 }
11771
11772 list_to_add = cu->list_in_scope;
11773 break;
11774 case DW_TAG_unspecified_parameters:
11775 /* From varargs functions; gdb doesn't seem to have any
11776 interest in this information, so just ignore it for now.
11777 (FIXME?) */
11778 break;
11779 case DW_TAG_template_type_param:
11780 suppress_add = 1;
11781 /* Fall through. */
11782 case DW_TAG_class_type:
11783 case DW_TAG_interface_type:
11784 case DW_TAG_structure_type:
11785 case DW_TAG_union_type:
11786 case DW_TAG_set_type:
11787 case DW_TAG_enumeration_type:
11788 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11789 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11790
11791 {
11792 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11793 really ever be static objects: otherwise, if you try
11794 to, say, break of a class's method and you're in a file
11795 which doesn't mention that class, it won't work unless
11796 the check for all static symbols in lookup_symbol_aux
11797 saves you. See the OtherFileClass tests in
11798 gdb.c++/namespace.exp. */
11799
11800 if (!suppress_add)
11801 {
11802 list_to_add = (cu->list_in_scope == &file_symbols
11803 && (cu->language == language_cplus
11804 || cu->language == language_java)
11805 ? &global_symbols : cu->list_in_scope);
11806
11807 /* The semantics of C++ state that "struct foo {
11808 ... }" also defines a typedef for "foo". A Java
11809 class declaration also defines a typedef for the
11810 class. */
11811 if (cu->language == language_cplus
11812 || cu->language == language_java
11813 || cu->language == language_ada)
11814 {
11815 /* The symbol's name is already allocated along
11816 with this objfile, so we don't need to
11817 duplicate it for the type. */
11818 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11819 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11820 }
11821 }
11822 }
11823 break;
11824 case DW_TAG_typedef:
11825 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11826 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11827 list_to_add = cu->list_in_scope;
11828 break;
11829 case DW_TAG_base_type:
11830 case DW_TAG_subrange_type:
11831 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11832 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11833 list_to_add = cu->list_in_scope;
11834 break;
11835 case DW_TAG_enumerator:
11836 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11837 if (attr)
11838 {
11839 dwarf2_const_value (attr, sym, cu);
11840 }
11841 {
11842 /* NOTE: carlton/2003-11-10: See comment above in the
11843 DW_TAG_class_type, etc. block. */
11844
11845 list_to_add = (cu->list_in_scope == &file_symbols
11846 && (cu->language == language_cplus
11847 || cu->language == language_java)
11848 ? &global_symbols : cu->list_in_scope);
11849 }
11850 break;
11851 case DW_TAG_namespace:
11852 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11853 list_to_add = &global_symbols;
11854 break;
11855 default:
11856 /* Not a tag we recognize. Hopefully we aren't processing
11857 trash data, but since we must specifically ignore things
11858 we don't recognize, there is nothing else we should do at
11859 this point. */
11860 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11861 dwarf_tag_name (die->tag));
11862 break;
11863 }
11864
11865 if (suppress_add)
11866 {
11867 sym->hash_next = objfile->template_symbols;
11868 objfile->template_symbols = sym;
11869 list_to_add = NULL;
11870 }
11871
11872 if (list_to_add != NULL)
11873 add_symbol_to_list (sym, list_to_add);
11874
11875 /* For the benefit of old versions of GCC, check for anonymous
11876 namespaces based on the demangled name. */
11877 if (!processing_has_namespace_info
11878 && cu->language == language_cplus)
11879 cp_scan_for_anonymous_namespaces (sym, objfile);
11880 }
11881 return (sym);
11882 }
11883
11884 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11885
11886 static struct symbol *
11887 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11888 {
11889 return new_symbol_full (die, type, cu, NULL);
11890 }
11891
11892 /* Given an attr with a DW_FORM_dataN value in host byte order,
11893 zero-extend it as appropriate for the symbol's type. The DWARF
11894 standard (v4) is not entirely clear about the meaning of using
11895 DW_FORM_dataN for a constant with a signed type, where the type is
11896 wider than the data. The conclusion of a discussion on the DWARF
11897 list was that this is unspecified. We choose to always zero-extend
11898 because that is the interpretation long in use by GCC. */
11899
11900 static gdb_byte *
11901 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11902 const char *name, struct obstack *obstack,
11903 struct dwarf2_cu *cu, long *value, int bits)
11904 {
11905 struct objfile *objfile = cu->objfile;
11906 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11907 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11908 LONGEST l = DW_UNSND (attr);
11909
11910 if (bits < sizeof (*value) * 8)
11911 {
11912 l &= ((LONGEST) 1 << bits) - 1;
11913 *value = l;
11914 }
11915 else if (bits == sizeof (*value) * 8)
11916 *value = l;
11917 else
11918 {
11919 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11920 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11921 return bytes;
11922 }
11923
11924 return NULL;
11925 }
11926
11927 /* Read a constant value from an attribute. Either set *VALUE, or if
11928 the value does not fit in *VALUE, set *BYTES - either already
11929 allocated on the objfile obstack, or newly allocated on OBSTACK,
11930 or, set *BATON, if we translated the constant to a location
11931 expression. */
11932
11933 static void
11934 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11935 const char *name, struct obstack *obstack,
11936 struct dwarf2_cu *cu,
11937 long *value, gdb_byte **bytes,
11938 struct dwarf2_locexpr_baton **baton)
11939 {
11940 struct objfile *objfile = cu->objfile;
11941 struct comp_unit_head *cu_header = &cu->header;
11942 struct dwarf_block *blk;
11943 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11944 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11945
11946 *value = 0;
11947 *bytes = NULL;
11948 *baton = NULL;
11949
11950 switch (attr->form)
11951 {
11952 case DW_FORM_addr:
11953 {
11954 gdb_byte *data;
11955
11956 if (TYPE_LENGTH (type) != cu_header->addr_size)
11957 dwarf2_const_value_length_mismatch_complaint (name,
11958 cu_header->addr_size,
11959 TYPE_LENGTH (type));
11960 /* Symbols of this form are reasonably rare, so we just
11961 piggyback on the existing location code rather than writing
11962 a new implementation of symbol_computed_ops. */
11963 *baton = obstack_alloc (&objfile->objfile_obstack,
11964 sizeof (struct dwarf2_locexpr_baton));
11965 (*baton)->per_cu = cu->per_cu;
11966 gdb_assert ((*baton)->per_cu);
11967
11968 (*baton)->size = 2 + cu_header->addr_size;
11969 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11970 (*baton)->data = data;
11971
11972 data[0] = DW_OP_addr;
11973 store_unsigned_integer (&data[1], cu_header->addr_size,
11974 byte_order, DW_ADDR (attr));
11975 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11976 }
11977 break;
11978 case DW_FORM_string:
11979 case DW_FORM_strp:
11980 /* DW_STRING is already allocated on the objfile obstack, point
11981 directly to it. */
11982 *bytes = (gdb_byte *) DW_STRING (attr);
11983 break;
11984 case DW_FORM_block1:
11985 case DW_FORM_block2:
11986 case DW_FORM_block4:
11987 case DW_FORM_block:
11988 case DW_FORM_exprloc:
11989 blk = DW_BLOCK (attr);
11990 if (TYPE_LENGTH (type) != blk->size)
11991 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11992 TYPE_LENGTH (type));
11993 *bytes = blk->data;
11994 break;
11995
11996 /* The DW_AT_const_value attributes are supposed to carry the
11997 symbol's value "represented as it would be on the target
11998 architecture." By the time we get here, it's already been
11999 converted to host endianness, so we just need to sign- or
12000 zero-extend it as appropriate. */
12001 case DW_FORM_data1:
12002 *bytes = dwarf2_const_value_data (attr, type, name,
12003 obstack, cu, value, 8);
12004 break;
12005 case DW_FORM_data2:
12006 *bytes = dwarf2_const_value_data (attr, type, name,
12007 obstack, cu, value, 16);
12008 break;
12009 case DW_FORM_data4:
12010 *bytes = dwarf2_const_value_data (attr, type, name,
12011 obstack, cu, value, 32);
12012 break;
12013 case DW_FORM_data8:
12014 *bytes = dwarf2_const_value_data (attr, type, name,
12015 obstack, cu, value, 64);
12016 break;
12017
12018 case DW_FORM_sdata:
12019 *value = DW_SND (attr);
12020 break;
12021
12022 case DW_FORM_udata:
12023 *value = DW_UNSND (attr);
12024 break;
12025
12026 default:
12027 complaint (&symfile_complaints,
12028 _("unsupported const value attribute form: '%s'"),
12029 dwarf_form_name (attr->form));
12030 *value = 0;
12031 break;
12032 }
12033 }
12034
12035
12036 /* Copy constant value from an attribute to a symbol. */
12037
12038 static void
12039 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12040 struct dwarf2_cu *cu)
12041 {
12042 struct objfile *objfile = cu->objfile;
12043 struct comp_unit_head *cu_header = &cu->header;
12044 long value;
12045 gdb_byte *bytes;
12046 struct dwarf2_locexpr_baton *baton;
12047
12048 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12049 SYMBOL_PRINT_NAME (sym),
12050 &objfile->objfile_obstack, cu,
12051 &value, &bytes, &baton);
12052
12053 if (baton != NULL)
12054 {
12055 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12056 SYMBOL_LOCATION_BATON (sym) = baton;
12057 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12058 }
12059 else if (bytes != NULL)
12060 {
12061 SYMBOL_VALUE_BYTES (sym) = bytes;
12062 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12063 }
12064 else
12065 {
12066 SYMBOL_VALUE (sym) = value;
12067 SYMBOL_CLASS (sym) = LOC_CONST;
12068 }
12069 }
12070
12071 /* Return the type of the die in question using its DW_AT_type attribute. */
12072
12073 static struct type *
12074 die_type (struct die_info *die, struct dwarf2_cu *cu)
12075 {
12076 struct attribute *type_attr;
12077
12078 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12079 if (!type_attr)
12080 {
12081 /* A missing DW_AT_type represents a void type. */
12082 return objfile_type (cu->objfile)->builtin_void;
12083 }
12084
12085 return lookup_die_type (die, type_attr, cu);
12086 }
12087
12088 /* True iff CU's producer generates GNAT Ada auxiliary information
12089 that allows to find parallel types through that information instead
12090 of having to do expensive parallel lookups by type name. */
12091
12092 static int
12093 need_gnat_info (struct dwarf2_cu *cu)
12094 {
12095 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12096 of GNAT produces this auxiliary information, without any indication
12097 that it is produced. Part of enhancing the FSF version of GNAT
12098 to produce that information will be to put in place an indicator
12099 that we can use in order to determine whether the descriptive type
12100 info is available or not. One suggestion that has been made is
12101 to use a new attribute, attached to the CU die. For now, assume
12102 that the descriptive type info is not available. */
12103 return 0;
12104 }
12105
12106 /* Return the auxiliary type of the die in question using its
12107 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12108 attribute is not present. */
12109
12110 static struct type *
12111 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12112 {
12113 struct attribute *type_attr;
12114
12115 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12116 if (!type_attr)
12117 return NULL;
12118
12119 return lookup_die_type (die, type_attr, cu);
12120 }
12121
12122 /* If DIE has a descriptive_type attribute, then set the TYPE's
12123 descriptive type accordingly. */
12124
12125 static void
12126 set_descriptive_type (struct type *type, struct die_info *die,
12127 struct dwarf2_cu *cu)
12128 {
12129 struct type *descriptive_type = die_descriptive_type (die, cu);
12130
12131 if (descriptive_type)
12132 {
12133 ALLOCATE_GNAT_AUX_TYPE (type);
12134 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12135 }
12136 }
12137
12138 /* Return the containing type of the die in question using its
12139 DW_AT_containing_type attribute. */
12140
12141 static struct type *
12142 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12143 {
12144 struct attribute *type_attr;
12145
12146 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12147 if (!type_attr)
12148 error (_("Dwarf Error: Problem turning containing type into gdb type "
12149 "[in module %s]"), cu->objfile->name);
12150
12151 return lookup_die_type (die, type_attr, cu);
12152 }
12153
12154 /* Look up the type of DIE in CU using its type attribute ATTR.
12155 If there is no type substitute an error marker. */
12156
12157 static struct type *
12158 lookup_die_type (struct die_info *die, struct attribute *attr,
12159 struct dwarf2_cu *cu)
12160 {
12161 struct objfile *objfile = cu->objfile;
12162 struct type *this_type;
12163
12164 /* First see if we have it cached. */
12165
12166 if (is_ref_attr (attr))
12167 {
12168 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12169
12170 this_type = get_die_type_at_offset (offset, cu->per_cu);
12171 }
12172 else if (attr->form == DW_FORM_ref_sig8)
12173 {
12174 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12175 struct dwarf2_cu *sig_cu;
12176 unsigned int offset;
12177
12178 /* sig_type will be NULL if the signatured type is missing from
12179 the debug info. */
12180 if (sig_type == NULL)
12181 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12182 "at 0x%x [in module %s]"),
12183 die->offset, objfile->name);
12184
12185 gdb_assert (sig_type->per_cu.debug_types_section);
12186 offset = sig_type->per_cu.offset + sig_type->type_offset;
12187 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12188 }
12189 else
12190 {
12191 dump_die_for_error (die);
12192 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12193 dwarf_attr_name (attr->name), objfile->name);
12194 }
12195
12196 /* If not cached we need to read it in. */
12197
12198 if (this_type == NULL)
12199 {
12200 struct die_info *type_die;
12201 struct dwarf2_cu *type_cu = cu;
12202
12203 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12204 /* If the type is cached, we should have found it above. */
12205 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12206 this_type = read_type_die_1 (type_die, type_cu);
12207 }
12208
12209 /* If we still don't have a type use an error marker. */
12210
12211 if (this_type == NULL)
12212 {
12213 char *message, *saved;
12214
12215 /* read_type_die already issued a complaint. */
12216 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12217 objfile->name,
12218 cu->header.offset,
12219 die->offset);
12220 saved = obstack_copy0 (&objfile->objfile_obstack,
12221 message, strlen (message));
12222 xfree (message);
12223
12224 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12225 }
12226
12227 return this_type;
12228 }
12229
12230 /* Return the type in DIE, CU.
12231 Returns NULL for invalid types.
12232
12233 This first does a lookup in the appropriate type_hash table,
12234 and only reads the die in if necessary.
12235
12236 NOTE: This can be called when reading in partial or full symbols. */
12237
12238 static struct type *
12239 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12240 {
12241 struct type *this_type;
12242
12243 this_type = get_die_type (die, cu);
12244 if (this_type)
12245 return this_type;
12246
12247 return read_type_die_1 (die, cu);
12248 }
12249
12250 /* Read the type in DIE, CU.
12251 Returns NULL for invalid types. */
12252
12253 static struct type *
12254 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12255 {
12256 struct type *this_type = NULL;
12257
12258 switch (die->tag)
12259 {
12260 case DW_TAG_class_type:
12261 case DW_TAG_interface_type:
12262 case DW_TAG_structure_type:
12263 case DW_TAG_union_type:
12264 this_type = read_structure_type (die, cu);
12265 break;
12266 case DW_TAG_enumeration_type:
12267 this_type = read_enumeration_type (die, cu);
12268 break;
12269 case DW_TAG_subprogram:
12270 case DW_TAG_subroutine_type:
12271 case DW_TAG_inlined_subroutine:
12272 this_type = read_subroutine_type (die, cu);
12273 break;
12274 case DW_TAG_array_type:
12275 this_type = read_array_type (die, cu);
12276 break;
12277 case DW_TAG_set_type:
12278 this_type = read_set_type (die, cu);
12279 break;
12280 case DW_TAG_pointer_type:
12281 this_type = read_tag_pointer_type (die, cu);
12282 break;
12283 case DW_TAG_ptr_to_member_type:
12284 this_type = read_tag_ptr_to_member_type (die, cu);
12285 break;
12286 case DW_TAG_reference_type:
12287 this_type = read_tag_reference_type (die, cu);
12288 break;
12289 case DW_TAG_const_type:
12290 this_type = read_tag_const_type (die, cu);
12291 break;
12292 case DW_TAG_volatile_type:
12293 this_type = read_tag_volatile_type (die, cu);
12294 break;
12295 case DW_TAG_string_type:
12296 this_type = read_tag_string_type (die, cu);
12297 break;
12298 case DW_TAG_typedef:
12299 this_type = read_typedef (die, cu);
12300 break;
12301 case DW_TAG_subrange_type:
12302 this_type = read_subrange_type (die, cu);
12303 break;
12304 case DW_TAG_base_type:
12305 this_type = read_base_type (die, cu);
12306 break;
12307 case DW_TAG_unspecified_type:
12308 this_type = read_unspecified_type (die, cu);
12309 break;
12310 case DW_TAG_namespace:
12311 this_type = read_namespace_type (die, cu);
12312 break;
12313 case DW_TAG_module:
12314 this_type = read_module_type (die, cu);
12315 break;
12316 default:
12317 complaint (&symfile_complaints,
12318 _("unexpected tag in read_type_die: '%s'"),
12319 dwarf_tag_name (die->tag));
12320 break;
12321 }
12322
12323 return this_type;
12324 }
12325
12326 /* See if we can figure out if the class lives in a namespace. We do
12327 this by looking for a member function; its demangled name will
12328 contain namespace info, if there is any.
12329 Return the computed name or NULL.
12330 Space for the result is allocated on the objfile's obstack.
12331 This is the full-die version of guess_partial_die_structure_name.
12332 In this case we know DIE has no useful parent. */
12333
12334 static char *
12335 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12336 {
12337 struct die_info *spec_die;
12338 struct dwarf2_cu *spec_cu;
12339 struct die_info *child;
12340
12341 spec_cu = cu;
12342 spec_die = die_specification (die, &spec_cu);
12343 if (spec_die != NULL)
12344 {
12345 die = spec_die;
12346 cu = spec_cu;
12347 }
12348
12349 for (child = die->child;
12350 child != NULL;
12351 child = child->sibling)
12352 {
12353 if (child->tag == DW_TAG_subprogram)
12354 {
12355 struct attribute *attr;
12356
12357 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12358 if (attr == NULL)
12359 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12360 if (attr != NULL)
12361 {
12362 char *actual_name
12363 = language_class_name_from_physname (cu->language_defn,
12364 DW_STRING (attr));
12365 char *name = NULL;
12366
12367 if (actual_name != NULL)
12368 {
12369 char *die_name = dwarf2_name (die, cu);
12370
12371 if (die_name != NULL
12372 && strcmp (die_name, actual_name) != 0)
12373 {
12374 /* Strip off the class name from the full name.
12375 We want the prefix. */
12376 int die_name_len = strlen (die_name);
12377 int actual_name_len = strlen (actual_name);
12378
12379 /* Test for '::' as a sanity check. */
12380 if (actual_name_len > die_name_len + 2
12381 && actual_name[actual_name_len
12382 - die_name_len - 1] == ':')
12383 name =
12384 obsavestring (actual_name,
12385 actual_name_len - die_name_len - 2,
12386 &cu->objfile->objfile_obstack);
12387 }
12388 }
12389 xfree (actual_name);
12390 return name;
12391 }
12392 }
12393 }
12394
12395 return NULL;
12396 }
12397
12398 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12399 prefix part in such case. See
12400 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12401
12402 static char *
12403 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12404 {
12405 struct attribute *attr;
12406 char *base;
12407
12408 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12409 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12410 return NULL;
12411
12412 attr = dwarf2_attr (die, DW_AT_name, cu);
12413 if (attr != NULL && DW_STRING (attr) != NULL)
12414 return NULL;
12415
12416 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12417 if (attr == NULL)
12418 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12419 if (attr == NULL || DW_STRING (attr) == NULL)
12420 return NULL;
12421
12422 /* dwarf2_name had to be already called. */
12423 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12424
12425 /* Strip the base name, keep any leading namespaces/classes. */
12426 base = strrchr (DW_STRING (attr), ':');
12427 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12428 return "";
12429
12430 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12431 &cu->objfile->objfile_obstack);
12432 }
12433
12434 /* Return the name of the namespace/class that DIE is defined within,
12435 or "" if we can't tell. The caller should not xfree the result.
12436
12437 For example, if we're within the method foo() in the following
12438 code:
12439
12440 namespace N {
12441 class C {
12442 void foo () {
12443 }
12444 };
12445 }
12446
12447 then determine_prefix on foo's die will return "N::C". */
12448
12449 static char *
12450 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12451 {
12452 struct die_info *parent, *spec_die;
12453 struct dwarf2_cu *spec_cu;
12454 struct type *parent_type;
12455 char *retval;
12456
12457 if (cu->language != language_cplus && cu->language != language_java
12458 && cu->language != language_fortran)
12459 return "";
12460
12461 retval = anonymous_struct_prefix (die, cu);
12462 if (retval)
12463 return retval;
12464
12465 /* We have to be careful in the presence of DW_AT_specification.
12466 For example, with GCC 3.4, given the code
12467
12468 namespace N {
12469 void foo() {
12470 // Definition of N::foo.
12471 }
12472 }
12473
12474 then we'll have a tree of DIEs like this:
12475
12476 1: DW_TAG_compile_unit
12477 2: DW_TAG_namespace // N
12478 3: DW_TAG_subprogram // declaration of N::foo
12479 4: DW_TAG_subprogram // definition of N::foo
12480 DW_AT_specification // refers to die #3
12481
12482 Thus, when processing die #4, we have to pretend that we're in
12483 the context of its DW_AT_specification, namely the contex of die
12484 #3. */
12485 spec_cu = cu;
12486 spec_die = die_specification (die, &spec_cu);
12487 if (spec_die == NULL)
12488 parent = die->parent;
12489 else
12490 {
12491 parent = spec_die->parent;
12492 cu = spec_cu;
12493 }
12494
12495 if (parent == NULL)
12496 return "";
12497 else if (parent->building_fullname)
12498 {
12499 const char *name;
12500 const char *parent_name;
12501
12502 /* It has been seen on RealView 2.2 built binaries,
12503 DW_TAG_template_type_param types actually _defined_ as
12504 children of the parent class:
12505
12506 enum E {};
12507 template class <class Enum> Class{};
12508 Class<enum E> class_e;
12509
12510 1: DW_TAG_class_type (Class)
12511 2: DW_TAG_enumeration_type (E)
12512 3: DW_TAG_enumerator (enum1:0)
12513 3: DW_TAG_enumerator (enum2:1)
12514 ...
12515 2: DW_TAG_template_type_param
12516 DW_AT_type DW_FORM_ref_udata (E)
12517
12518 Besides being broken debug info, it can put GDB into an
12519 infinite loop. Consider:
12520
12521 When we're building the full name for Class<E>, we'll start
12522 at Class, and go look over its template type parameters,
12523 finding E. We'll then try to build the full name of E, and
12524 reach here. We're now trying to build the full name of E,
12525 and look over the parent DIE for containing scope. In the
12526 broken case, if we followed the parent DIE of E, we'd again
12527 find Class, and once again go look at its template type
12528 arguments, etc., etc. Simply don't consider such parent die
12529 as source-level parent of this die (it can't be, the language
12530 doesn't allow it), and break the loop here. */
12531 name = dwarf2_name (die, cu);
12532 parent_name = dwarf2_name (parent, cu);
12533 complaint (&symfile_complaints,
12534 _("template param type '%s' defined within parent '%s'"),
12535 name ? name : "<unknown>",
12536 parent_name ? parent_name : "<unknown>");
12537 return "";
12538 }
12539 else
12540 switch (parent->tag)
12541 {
12542 case DW_TAG_namespace:
12543 parent_type = read_type_die (parent, cu);
12544 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12545 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12546 Work around this problem here. */
12547 if (cu->language == language_cplus
12548 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12549 return "";
12550 /* We give a name to even anonymous namespaces. */
12551 return TYPE_TAG_NAME (parent_type);
12552 case DW_TAG_class_type:
12553 case DW_TAG_interface_type:
12554 case DW_TAG_structure_type:
12555 case DW_TAG_union_type:
12556 case DW_TAG_module:
12557 parent_type = read_type_die (parent, cu);
12558 if (TYPE_TAG_NAME (parent_type) != NULL)
12559 return TYPE_TAG_NAME (parent_type);
12560 else
12561 /* An anonymous structure is only allowed non-static data
12562 members; no typedefs, no member functions, et cetera.
12563 So it does not need a prefix. */
12564 return "";
12565 case DW_TAG_compile_unit:
12566 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12567 if (cu->language == language_cplus
12568 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12569 && die->child != NULL
12570 && (die->tag == DW_TAG_class_type
12571 || die->tag == DW_TAG_structure_type
12572 || die->tag == DW_TAG_union_type))
12573 {
12574 char *name = guess_full_die_structure_name (die, cu);
12575 if (name != NULL)
12576 return name;
12577 }
12578 return "";
12579 default:
12580 return determine_prefix (parent, cu);
12581 }
12582 }
12583
12584 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12585 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12586 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12587 an obconcat, otherwise allocate storage for the result. The CU argument is
12588 used to determine the language and hence, the appropriate separator. */
12589
12590 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12591
12592 static char *
12593 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12594 int physname, struct dwarf2_cu *cu)
12595 {
12596 const char *lead = "";
12597 const char *sep;
12598
12599 if (suffix == NULL || suffix[0] == '\0'
12600 || prefix == NULL || prefix[0] == '\0')
12601 sep = "";
12602 else if (cu->language == language_java)
12603 sep = ".";
12604 else if (cu->language == language_fortran && physname)
12605 {
12606 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12607 DW_AT_MIPS_linkage_name is preferred and used instead. */
12608
12609 lead = "__";
12610 sep = "_MOD_";
12611 }
12612 else
12613 sep = "::";
12614
12615 if (prefix == NULL)
12616 prefix = "";
12617 if (suffix == NULL)
12618 suffix = "";
12619
12620 if (obs == NULL)
12621 {
12622 char *retval
12623 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12624
12625 strcpy (retval, lead);
12626 strcat (retval, prefix);
12627 strcat (retval, sep);
12628 strcat (retval, suffix);
12629 return retval;
12630 }
12631 else
12632 {
12633 /* We have an obstack. */
12634 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12635 }
12636 }
12637
12638 /* Return sibling of die, NULL if no sibling. */
12639
12640 static struct die_info *
12641 sibling_die (struct die_info *die)
12642 {
12643 return die->sibling;
12644 }
12645
12646 /* Get name of a die, return NULL if not found. */
12647
12648 static char *
12649 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12650 struct obstack *obstack)
12651 {
12652 if (name && cu->language == language_cplus)
12653 {
12654 char *canon_name = cp_canonicalize_string (name);
12655
12656 if (canon_name != NULL)
12657 {
12658 if (strcmp (canon_name, name) != 0)
12659 name = obsavestring (canon_name, strlen (canon_name),
12660 obstack);
12661 xfree (canon_name);
12662 }
12663 }
12664
12665 return name;
12666 }
12667
12668 /* Get name of a die, return NULL if not found. */
12669
12670 static char *
12671 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12672 {
12673 struct attribute *attr;
12674
12675 attr = dwarf2_attr (die, DW_AT_name, cu);
12676 if ((!attr || !DW_STRING (attr))
12677 && die->tag != DW_TAG_class_type
12678 && die->tag != DW_TAG_interface_type
12679 && die->tag != DW_TAG_structure_type
12680 && die->tag != DW_TAG_union_type)
12681 return NULL;
12682
12683 switch (die->tag)
12684 {
12685 case DW_TAG_compile_unit:
12686 /* Compilation units have a DW_AT_name that is a filename, not
12687 a source language identifier. */
12688 case DW_TAG_enumeration_type:
12689 case DW_TAG_enumerator:
12690 /* These tags always have simple identifiers already; no need
12691 to canonicalize them. */
12692 return DW_STRING (attr);
12693
12694 case DW_TAG_subprogram:
12695 /* Java constructors will all be named "<init>", so return
12696 the class name when we see this special case. */
12697 if (cu->language == language_java
12698 && DW_STRING (attr) != NULL
12699 && strcmp (DW_STRING (attr), "<init>") == 0)
12700 {
12701 struct dwarf2_cu *spec_cu = cu;
12702 struct die_info *spec_die;
12703
12704 /* GCJ will output '<init>' for Java constructor names.
12705 For this special case, return the name of the parent class. */
12706
12707 /* GCJ may output suprogram DIEs with AT_specification set.
12708 If so, use the name of the specified DIE. */
12709 spec_die = die_specification (die, &spec_cu);
12710 if (spec_die != NULL)
12711 return dwarf2_name (spec_die, spec_cu);
12712
12713 do
12714 {
12715 die = die->parent;
12716 if (die->tag == DW_TAG_class_type)
12717 return dwarf2_name (die, cu);
12718 }
12719 while (die->tag != DW_TAG_compile_unit);
12720 }
12721 break;
12722
12723 case DW_TAG_class_type:
12724 case DW_TAG_interface_type:
12725 case DW_TAG_structure_type:
12726 case DW_TAG_union_type:
12727 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12728 structures or unions. These were of the form "._%d" in GCC 4.1,
12729 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12730 and GCC 4.4. We work around this problem by ignoring these. */
12731 if (attr && DW_STRING (attr)
12732 && (strncmp (DW_STRING (attr), "._", 2) == 0
12733 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12734 return NULL;
12735
12736 /* GCC might emit a nameless typedef that has a linkage name. See
12737 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12738 if (!attr || DW_STRING (attr) == NULL)
12739 {
12740 char *demangled = NULL;
12741
12742 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12743 if (attr == NULL)
12744 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12745
12746 if (attr == NULL || DW_STRING (attr) == NULL)
12747 return NULL;
12748
12749 /* Avoid demangling DW_STRING (attr) the second time on a second
12750 call for the same DIE. */
12751 if (!DW_STRING_IS_CANONICAL (attr))
12752 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12753
12754 if (demangled)
12755 {
12756 char *base;
12757
12758 /* FIXME: we already did this for the partial symbol... */
12759 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12760 &cu->objfile->objfile_obstack);
12761 DW_STRING_IS_CANONICAL (attr) = 1;
12762 xfree (demangled);
12763
12764 /* Strip any leading namespaces/classes, keep only the base name.
12765 DW_AT_name for named DIEs does not contain the prefixes. */
12766 base = strrchr (DW_STRING (attr), ':');
12767 if (base && base > DW_STRING (attr) && base[-1] == ':')
12768 return &base[1];
12769 else
12770 return DW_STRING (attr);
12771 }
12772 }
12773 break;
12774
12775 default:
12776 break;
12777 }
12778
12779 if (!DW_STRING_IS_CANONICAL (attr))
12780 {
12781 DW_STRING (attr)
12782 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12783 &cu->objfile->objfile_obstack);
12784 DW_STRING_IS_CANONICAL (attr) = 1;
12785 }
12786 return DW_STRING (attr);
12787 }
12788
12789 /* Return the die that this die in an extension of, or NULL if there
12790 is none. *EXT_CU is the CU containing DIE on input, and the CU
12791 containing the return value on output. */
12792
12793 static struct die_info *
12794 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12795 {
12796 struct attribute *attr;
12797
12798 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12799 if (attr == NULL)
12800 return NULL;
12801
12802 return follow_die_ref (die, attr, ext_cu);
12803 }
12804
12805 /* Convert a DIE tag into its string name. */
12806
12807 static char *
12808 dwarf_tag_name (unsigned tag)
12809 {
12810 switch (tag)
12811 {
12812 case DW_TAG_padding:
12813 return "DW_TAG_padding";
12814 case DW_TAG_array_type:
12815 return "DW_TAG_array_type";
12816 case DW_TAG_class_type:
12817 return "DW_TAG_class_type";
12818 case DW_TAG_entry_point:
12819 return "DW_TAG_entry_point";
12820 case DW_TAG_enumeration_type:
12821 return "DW_TAG_enumeration_type";
12822 case DW_TAG_formal_parameter:
12823 return "DW_TAG_formal_parameter";
12824 case DW_TAG_imported_declaration:
12825 return "DW_TAG_imported_declaration";
12826 case DW_TAG_label:
12827 return "DW_TAG_label";
12828 case DW_TAG_lexical_block:
12829 return "DW_TAG_lexical_block";
12830 case DW_TAG_member:
12831 return "DW_TAG_member";
12832 case DW_TAG_pointer_type:
12833 return "DW_TAG_pointer_type";
12834 case DW_TAG_reference_type:
12835 return "DW_TAG_reference_type";
12836 case DW_TAG_compile_unit:
12837 return "DW_TAG_compile_unit";
12838 case DW_TAG_string_type:
12839 return "DW_TAG_string_type";
12840 case DW_TAG_structure_type:
12841 return "DW_TAG_structure_type";
12842 case DW_TAG_subroutine_type:
12843 return "DW_TAG_subroutine_type";
12844 case DW_TAG_typedef:
12845 return "DW_TAG_typedef";
12846 case DW_TAG_union_type:
12847 return "DW_TAG_union_type";
12848 case DW_TAG_unspecified_parameters:
12849 return "DW_TAG_unspecified_parameters";
12850 case DW_TAG_variant:
12851 return "DW_TAG_variant";
12852 case DW_TAG_common_block:
12853 return "DW_TAG_common_block";
12854 case DW_TAG_common_inclusion:
12855 return "DW_TAG_common_inclusion";
12856 case DW_TAG_inheritance:
12857 return "DW_TAG_inheritance";
12858 case DW_TAG_inlined_subroutine:
12859 return "DW_TAG_inlined_subroutine";
12860 case DW_TAG_module:
12861 return "DW_TAG_module";
12862 case DW_TAG_ptr_to_member_type:
12863 return "DW_TAG_ptr_to_member_type";
12864 case DW_TAG_set_type:
12865 return "DW_TAG_set_type";
12866 case DW_TAG_subrange_type:
12867 return "DW_TAG_subrange_type";
12868 case DW_TAG_with_stmt:
12869 return "DW_TAG_with_stmt";
12870 case DW_TAG_access_declaration:
12871 return "DW_TAG_access_declaration";
12872 case DW_TAG_base_type:
12873 return "DW_TAG_base_type";
12874 case DW_TAG_catch_block:
12875 return "DW_TAG_catch_block";
12876 case DW_TAG_const_type:
12877 return "DW_TAG_const_type";
12878 case DW_TAG_constant:
12879 return "DW_TAG_constant";
12880 case DW_TAG_enumerator:
12881 return "DW_TAG_enumerator";
12882 case DW_TAG_file_type:
12883 return "DW_TAG_file_type";
12884 case DW_TAG_friend:
12885 return "DW_TAG_friend";
12886 case DW_TAG_namelist:
12887 return "DW_TAG_namelist";
12888 case DW_TAG_namelist_item:
12889 return "DW_TAG_namelist_item";
12890 case DW_TAG_packed_type:
12891 return "DW_TAG_packed_type";
12892 case DW_TAG_subprogram:
12893 return "DW_TAG_subprogram";
12894 case DW_TAG_template_type_param:
12895 return "DW_TAG_template_type_param";
12896 case DW_TAG_template_value_param:
12897 return "DW_TAG_template_value_param";
12898 case DW_TAG_thrown_type:
12899 return "DW_TAG_thrown_type";
12900 case DW_TAG_try_block:
12901 return "DW_TAG_try_block";
12902 case DW_TAG_variant_part:
12903 return "DW_TAG_variant_part";
12904 case DW_TAG_variable:
12905 return "DW_TAG_variable";
12906 case DW_TAG_volatile_type:
12907 return "DW_TAG_volatile_type";
12908 case DW_TAG_dwarf_procedure:
12909 return "DW_TAG_dwarf_procedure";
12910 case DW_TAG_restrict_type:
12911 return "DW_TAG_restrict_type";
12912 case DW_TAG_interface_type:
12913 return "DW_TAG_interface_type";
12914 case DW_TAG_namespace:
12915 return "DW_TAG_namespace";
12916 case DW_TAG_imported_module:
12917 return "DW_TAG_imported_module";
12918 case DW_TAG_unspecified_type:
12919 return "DW_TAG_unspecified_type";
12920 case DW_TAG_partial_unit:
12921 return "DW_TAG_partial_unit";
12922 case DW_TAG_imported_unit:
12923 return "DW_TAG_imported_unit";
12924 case DW_TAG_condition:
12925 return "DW_TAG_condition";
12926 case DW_TAG_shared_type:
12927 return "DW_TAG_shared_type";
12928 case DW_TAG_type_unit:
12929 return "DW_TAG_type_unit";
12930 case DW_TAG_MIPS_loop:
12931 return "DW_TAG_MIPS_loop";
12932 case DW_TAG_HP_array_descriptor:
12933 return "DW_TAG_HP_array_descriptor";
12934 case DW_TAG_format_label:
12935 return "DW_TAG_format_label";
12936 case DW_TAG_function_template:
12937 return "DW_TAG_function_template";
12938 case DW_TAG_class_template:
12939 return "DW_TAG_class_template";
12940 case DW_TAG_GNU_BINCL:
12941 return "DW_TAG_GNU_BINCL";
12942 case DW_TAG_GNU_EINCL:
12943 return "DW_TAG_GNU_EINCL";
12944 case DW_TAG_upc_shared_type:
12945 return "DW_TAG_upc_shared_type";
12946 case DW_TAG_upc_strict_type:
12947 return "DW_TAG_upc_strict_type";
12948 case DW_TAG_upc_relaxed_type:
12949 return "DW_TAG_upc_relaxed_type";
12950 case DW_TAG_PGI_kanji_type:
12951 return "DW_TAG_PGI_kanji_type";
12952 case DW_TAG_PGI_interface_block:
12953 return "DW_TAG_PGI_interface_block";
12954 case DW_TAG_GNU_call_site:
12955 return "DW_TAG_GNU_call_site";
12956 default:
12957 return "DW_TAG_<unknown>";
12958 }
12959 }
12960
12961 /* Convert a DWARF attribute code into its string name. */
12962
12963 static char *
12964 dwarf_attr_name (unsigned attr)
12965 {
12966 switch (attr)
12967 {
12968 case DW_AT_sibling:
12969 return "DW_AT_sibling";
12970 case DW_AT_location:
12971 return "DW_AT_location";
12972 case DW_AT_name:
12973 return "DW_AT_name";
12974 case DW_AT_ordering:
12975 return "DW_AT_ordering";
12976 case DW_AT_subscr_data:
12977 return "DW_AT_subscr_data";
12978 case DW_AT_byte_size:
12979 return "DW_AT_byte_size";
12980 case DW_AT_bit_offset:
12981 return "DW_AT_bit_offset";
12982 case DW_AT_bit_size:
12983 return "DW_AT_bit_size";
12984 case DW_AT_element_list:
12985 return "DW_AT_element_list";
12986 case DW_AT_stmt_list:
12987 return "DW_AT_stmt_list";
12988 case DW_AT_low_pc:
12989 return "DW_AT_low_pc";
12990 case DW_AT_high_pc:
12991 return "DW_AT_high_pc";
12992 case DW_AT_language:
12993 return "DW_AT_language";
12994 case DW_AT_member:
12995 return "DW_AT_member";
12996 case DW_AT_discr:
12997 return "DW_AT_discr";
12998 case DW_AT_discr_value:
12999 return "DW_AT_discr_value";
13000 case DW_AT_visibility:
13001 return "DW_AT_visibility";
13002 case DW_AT_import:
13003 return "DW_AT_import";
13004 case DW_AT_string_length:
13005 return "DW_AT_string_length";
13006 case DW_AT_common_reference:
13007 return "DW_AT_common_reference";
13008 case DW_AT_comp_dir:
13009 return "DW_AT_comp_dir";
13010 case DW_AT_const_value:
13011 return "DW_AT_const_value";
13012 case DW_AT_containing_type:
13013 return "DW_AT_containing_type";
13014 case DW_AT_default_value:
13015 return "DW_AT_default_value";
13016 case DW_AT_inline:
13017 return "DW_AT_inline";
13018 case DW_AT_is_optional:
13019 return "DW_AT_is_optional";
13020 case DW_AT_lower_bound:
13021 return "DW_AT_lower_bound";
13022 case DW_AT_producer:
13023 return "DW_AT_producer";
13024 case DW_AT_prototyped:
13025 return "DW_AT_prototyped";
13026 case DW_AT_return_addr:
13027 return "DW_AT_return_addr";
13028 case DW_AT_start_scope:
13029 return "DW_AT_start_scope";
13030 case DW_AT_bit_stride:
13031 return "DW_AT_bit_stride";
13032 case DW_AT_upper_bound:
13033 return "DW_AT_upper_bound";
13034 case DW_AT_abstract_origin:
13035 return "DW_AT_abstract_origin";
13036 case DW_AT_accessibility:
13037 return "DW_AT_accessibility";
13038 case DW_AT_address_class:
13039 return "DW_AT_address_class";
13040 case DW_AT_artificial:
13041 return "DW_AT_artificial";
13042 case DW_AT_base_types:
13043 return "DW_AT_base_types";
13044 case DW_AT_calling_convention:
13045 return "DW_AT_calling_convention";
13046 case DW_AT_count:
13047 return "DW_AT_count";
13048 case DW_AT_data_member_location:
13049 return "DW_AT_data_member_location";
13050 case DW_AT_decl_column:
13051 return "DW_AT_decl_column";
13052 case DW_AT_decl_file:
13053 return "DW_AT_decl_file";
13054 case DW_AT_decl_line:
13055 return "DW_AT_decl_line";
13056 case DW_AT_declaration:
13057 return "DW_AT_declaration";
13058 case DW_AT_discr_list:
13059 return "DW_AT_discr_list";
13060 case DW_AT_encoding:
13061 return "DW_AT_encoding";
13062 case DW_AT_external:
13063 return "DW_AT_external";
13064 case DW_AT_frame_base:
13065 return "DW_AT_frame_base";
13066 case DW_AT_friend:
13067 return "DW_AT_friend";
13068 case DW_AT_identifier_case:
13069 return "DW_AT_identifier_case";
13070 case DW_AT_macro_info:
13071 return "DW_AT_macro_info";
13072 case DW_AT_namelist_items:
13073 return "DW_AT_namelist_items";
13074 case DW_AT_priority:
13075 return "DW_AT_priority";
13076 case DW_AT_segment:
13077 return "DW_AT_segment";
13078 case DW_AT_specification:
13079 return "DW_AT_specification";
13080 case DW_AT_static_link:
13081 return "DW_AT_static_link";
13082 case DW_AT_type:
13083 return "DW_AT_type";
13084 case DW_AT_use_location:
13085 return "DW_AT_use_location";
13086 case DW_AT_variable_parameter:
13087 return "DW_AT_variable_parameter";
13088 case DW_AT_virtuality:
13089 return "DW_AT_virtuality";
13090 case DW_AT_vtable_elem_location:
13091 return "DW_AT_vtable_elem_location";
13092 /* DWARF 3 values. */
13093 case DW_AT_allocated:
13094 return "DW_AT_allocated";
13095 case DW_AT_associated:
13096 return "DW_AT_associated";
13097 case DW_AT_data_location:
13098 return "DW_AT_data_location";
13099 case DW_AT_byte_stride:
13100 return "DW_AT_byte_stride";
13101 case DW_AT_entry_pc:
13102 return "DW_AT_entry_pc";
13103 case DW_AT_use_UTF8:
13104 return "DW_AT_use_UTF8";
13105 case DW_AT_extension:
13106 return "DW_AT_extension";
13107 case DW_AT_ranges:
13108 return "DW_AT_ranges";
13109 case DW_AT_trampoline:
13110 return "DW_AT_trampoline";
13111 case DW_AT_call_column:
13112 return "DW_AT_call_column";
13113 case DW_AT_call_file:
13114 return "DW_AT_call_file";
13115 case DW_AT_call_line:
13116 return "DW_AT_call_line";
13117 case DW_AT_description:
13118 return "DW_AT_description";
13119 case DW_AT_binary_scale:
13120 return "DW_AT_binary_scale";
13121 case DW_AT_decimal_scale:
13122 return "DW_AT_decimal_scale";
13123 case DW_AT_small:
13124 return "DW_AT_small";
13125 case DW_AT_decimal_sign:
13126 return "DW_AT_decimal_sign";
13127 case DW_AT_digit_count:
13128 return "DW_AT_digit_count";
13129 case DW_AT_picture_string:
13130 return "DW_AT_picture_string";
13131 case DW_AT_mutable:
13132 return "DW_AT_mutable";
13133 case DW_AT_threads_scaled:
13134 return "DW_AT_threads_scaled";
13135 case DW_AT_explicit:
13136 return "DW_AT_explicit";
13137 case DW_AT_object_pointer:
13138 return "DW_AT_object_pointer";
13139 case DW_AT_endianity:
13140 return "DW_AT_endianity";
13141 case DW_AT_elemental:
13142 return "DW_AT_elemental";
13143 case DW_AT_pure:
13144 return "DW_AT_pure";
13145 case DW_AT_recursive:
13146 return "DW_AT_recursive";
13147 /* DWARF 4 values. */
13148 case DW_AT_signature:
13149 return "DW_AT_signature";
13150 case DW_AT_linkage_name:
13151 return "DW_AT_linkage_name";
13152 /* SGI/MIPS extensions. */
13153 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13154 case DW_AT_MIPS_fde:
13155 return "DW_AT_MIPS_fde";
13156 #endif
13157 case DW_AT_MIPS_loop_begin:
13158 return "DW_AT_MIPS_loop_begin";
13159 case DW_AT_MIPS_tail_loop_begin:
13160 return "DW_AT_MIPS_tail_loop_begin";
13161 case DW_AT_MIPS_epilog_begin:
13162 return "DW_AT_MIPS_epilog_begin";
13163 case DW_AT_MIPS_loop_unroll_factor:
13164 return "DW_AT_MIPS_loop_unroll_factor";
13165 case DW_AT_MIPS_software_pipeline_depth:
13166 return "DW_AT_MIPS_software_pipeline_depth";
13167 case DW_AT_MIPS_linkage_name:
13168 return "DW_AT_MIPS_linkage_name";
13169 case DW_AT_MIPS_stride:
13170 return "DW_AT_MIPS_stride";
13171 case DW_AT_MIPS_abstract_name:
13172 return "DW_AT_MIPS_abstract_name";
13173 case DW_AT_MIPS_clone_origin:
13174 return "DW_AT_MIPS_clone_origin";
13175 case DW_AT_MIPS_has_inlines:
13176 return "DW_AT_MIPS_has_inlines";
13177 /* HP extensions. */
13178 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13179 case DW_AT_HP_block_index:
13180 return "DW_AT_HP_block_index";
13181 #endif
13182 case DW_AT_HP_unmodifiable:
13183 return "DW_AT_HP_unmodifiable";
13184 case DW_AT_HP_actuals_stmt_list:
13185 return "DW_AT_HP_actuals_stmt_list";
13186 case DW_AT_HP_proc_per_section:
13187 return "DW_AT_HP_proc_per_section";
13188 case DW_AT_HP_raw_data_ptr:
13189 return "DW_AT_HP_raw_data_ptr";
13190 case DW_AT_HP_pass_by_reference:
13191 return "DW_AT_HP_pass_by_reference";
13192 case DW_AT_HP_opt_level:
13193 return "DW_AT_HP_opt_level";
13194 case DW_AT_HP_prof_version_id:
13195 return "DW_AT_HP_prof_version_id";
13196 case DW_AT_HP_opt_flags:
13197 return "DW_AT_HP_opt_flags";
13198 case DW_AT_HP_cold_region_low_pc:
13199 return "DW_AT_HP_cold_region_low_pc";
13200 case DW_AT_HP_cold_region_high_pc:
13201 return "DW_AT_HP_cold_region_high_pc";
13202 case DW_AT_HP_all_variables_modifiable:
13203 return "DW_AT_HP_all_variables_modifiable";
13204 case DW_AT_HP_linkage_name:
13205 return "DW_AT_HP_linkage_name";
13206 case DW_AT_HP_prof_flags:
13207 return "DW_AT_HP_prof_flags";
13208 /* GNU extensions. */
13209 case DW_AT_sf_names:
13210 return "DW_AT_sf_names";
13211 case DW_AT_src_info:
13212 return "DW_AT_src_info";
13213 case DW_AT_mac_info:
13214 return "DW_AT_mac_info";
13215 case DW_AT_src_coords:
13216 return "DW_AT_src_coords";
13217 case DW_AT_body_begin:
13218 return "DW_AT_body_begin";
13219 case DW_AT_body_end:
13220 return "DW_AT_body_end";
13221 case DW_AT_GNU_vector:
13222 return "DW_AT_GNU_vector";
13223 case DW_AT_GNU_odr_signature:
13224 return "DW_AT_GNU_odr_signature";
13225 /* VMS extensions. */
13226 case DW_AT_VMS_rtnbeg_pd_address:
13227 return "DW_AT_VMS_rtnbeg_pd_address";
13228 /* UPC extension. */
13229 case DW_AT_upc_threads_scaled:
13230 return "DW_AT_upc_threads_scaled";
13231 /* PGI (STMicroelectronics) extensions. */
13232 case DW_AT_PGI_lbase:
13233 return "DW_AT_PGI_lbase";
13234 case DW_AT_PGI_soffset:
13235 return "DW_AT_PGI_soffset";
13236 case DW_AT_PGI_lstride:
13237 return "DW_AT_PGI_lstride";
13238 default:
13239 return "DW_AT_<unknown>";
13240 }
13241 }
13242
13243 /* Convert a DWARF value form code into its string name. */
13244
13245 static char *
13246 dwarf_form_name (unsigned form)
13247 {
13248 switch (form)
13249 {
13250 case DW_FORM_addr:
13251 return "DW_FORM_addr";
13252 case DW_FORM_block2:
13253 return "DW_FORM_block2";
13254 case DW_FORM_block4:
13255 return "DW_FORM_block4";
13256 case DW_FORM_data2:
13257 return "DW_FORM_data2";
13258 case DW_FORM_data4:
13259 return "DW_FORM_data4";
13260 case DW_FORM_data8:
13261 return "DW_FORM_data8";
13262 case DW_FORM_string:
13263 return "DW_FORM_string";
13264 case DW_FORM_block:
13265 return "DW_FORM_block";
13266 case DW_FORM_block1:
13267 return "DW_FORM_block1";
13268 case DW_FORM_data1:
13269 return "DW_FORM_data1";
13270 case DW_FORM_flag:
13271 return "DW_FORM_flag";
13272 case DW_FORM_sdata:
13273 return "DW_FORM_sdata";
13274 case DW_FORM_strp:
13275 return "DW_FORM_strp";
13276 case DW_FORM_udata:
13277 return "DW_FORM_udata";
13278 case DW_FORM_ref_addr:
13279 return "DW_FORM_ref_addr";
13280 case DW_FORM_ref1:
13281 return "DW_FORM_ref1";
13282 case DW_FORM_ref2:
13283 return "DW_FORM_ref2";
13284 case DW_FORM_ref4:
13285 return "DW_FORM_ref4";
13286 case DW_FORM_ref8:
13287 return "DW_FORM_ref8";
13288 case DW_FORM_ref_udata:
13289 return "DW_FORM_ref_udata";
13290 case DW_FORM_indirect:
13291 return "DW_FORM_indirect";
13292 case DW_FORM_sec_offset:
13293 return "DW_FORM_sec_offset";
13294 case DW_FORM_exprloc:
13295 return "DW_FORM_exprloc";
13296 case DW_FORM_flag_present:
13297 return "DW_FORM_flag_present";
13298 case DW_FORM_ref_sig8:
13299 return "DW_FORM_ref_sig8";
13300 default:
13301 return "DW_FORM_<unknown>";
13302 }
13303 }
13304
13305 /* Convert a DWARF stack opcode into its string name. */
13306
13307 const char *
13308 dwarf_stack_op_name (unsigned op)
13309 {
13310 switch (op)
13311 {
13312 case DW_OP_addr:
13313 return "DW_OP_addr";
13314 case DW_OP_deref:
13315 return "DW_OP_deref";
13316 case DW_OP_const1u:
13317 return "DW_OP_const1u";
13318 case DW_OP_const1s:
13319 return "DW_OP_const1s";
13320 case DW_OP_const2u:
13321 return "DW_OP_const2u";
13322 case DW_OP_const2s:
13323 return "DW_OP_const2s";
13324 case DW_OP_const4u:
13325 return "DW_OP_const4u";
13326 case DW_OP_const4s:
13327 return "DW_OP_const4s";
13328 case DW_OP_const8u:
13329 return "DW_OP_const8u";
13330 case DW_OP_const8s:
13331 return "DW_OP_const8s";
13332 case DW_OP_constu:
13333 return "DW_OP_constu";
13334 case DW_OP_consts:
13335 return "DW_OP_consts";
13336 case DW_OP_dup:
13337 return "DW_OP_dup";
13338 case DW_OP_drop:
13339 return "DW_OP_drop";
13340 case DW_OP_over:
13341 return "DW_OP_over";
13342 case DW_OP_pick:
13343 return "DW_OP_pick";
13344 case DW_OP_swap:
13345 return "DW_OP_swap";
13346 case DW_OP_rot:
13347 return "DW_OP_rot";
13348 case DW_OP_xderef:
13349 return "DW_OP_xderef";
13350 case DW_OP_abs:
13351 return "DW_OP_abs";
13352 case DW_OP_and:
13353 return "DW_OP_and";
13354 case DW_OP_div:
13355 return "DW_OP_div";
13356 case DW_OP_minus:
13357 return "DW_OP_minus";
13358 case DW_OP_mod:
13359 return "DW_OP_mod";
13360 case DW_OP_mul:
13361 return "DW_OP_mul";
13362 case DW_OP_neg:
13363 return "DW_OP_neg";
13364 case DW_OP_not:
13365 return "DW_OP_not";
13366 case DW_OP_or:
13367 return "DW_OP_or";
13368 case DW_OP_plus:
13369 return "DW_OP_plus";
13370 case DW_OP_plus_uconst:
13371 return "DW_OP_plus_uconst";
13372 case DW_OP_shl:
13373 return "DW_OP_shl";
13374 case DW_OP_shr:
13375 return "DW_OP_shr";
13376 case DW_OP_shra:
13377 return "DW_OP_shra";
13378 case DW_OP_xor:
13379 return "DW_OP_xor";
13380 case DW_OP_bra:
13381 return "DW_OP_bra";
13382 case DW_OP_eq:
13383 return "DW_OP_eq";
13384 case DW_OP_ge:
13385 return "DW_OP_ge";
13386 case DW_OP_gt:
13387 return "DW_OP_gt";
13388 case DW_OP_le:
13389 return "DW_OP_le";
13390 case DW_OP_lt:
13391 return "DW_OP_lt";
13392 case DW_OP_ne:
13393 return "DW_OP_ne";
13394 case DW_OP_skip:
13395 return "DW_OP_skip";
13396 case DW_OP_lit0:
13397 return "DW_OP_lit0";
13398 case DW_OP_lit1:
13399 return "DW_OP_lit1";
13400 case DW_OP_lit2:
13401 return "DW_OP_lit2";
13402 case DW_OP_lit3:
13403 return "DW_OP_lit3";
13404 case DW_OP_lit4:
13405 return "DW_OP_lit4";
13406 case DW_OP_lit5:
13407 return "DW_OP_lit5";
13408 case DW_OP_lit6:
13409 return "DW_OP_lit6";
13410 case DW_OP_lit7:
13411 return "DW_OP_lit7";
13412 case DW_OP_lit8:
13413 return "DW_OP_lit8";
13414 case DW_OP_lit9:
13415 return "DW_OP_lit9";
13416 case DW_OP_lit10:
13417 return "DW_OP_lit10";
13418 case DW_OP_lit11:
13419 return "DW_OP_lit11";
13420 case DW_OP_lit12:
13421 return "DW_OP_lit12";
13422 case DW_OP_lit13:
13423 return "DW_OP_lit13";
13424 case DW_OP_lit14:
13425 return "DW_OP_lit14";
13426 case DW_OP_lit15:
13427 return "DW_OP_lit15";
13428 case DW_OP_lit16:
13429 return "DW_OP_lit16";
13430 case DW_OP_lit17:
13431 return "DW_OP_lit17";
13432 case DW_OP_lit18:
13433 return "DW_OP_lit18";
13434 case DW_OP_lit19:
13435 return "DW_OP_lit19";
13436 case DW_OP_lit20:
13437 return "DW_OP_lit20";
13438 case DW_OP_lit21:
13439 return "DW_OP_lit21";
13440 case DW_OP_lit22:
13441 return "DW_OP_lit22";
13442 case DW_OP_lit23:
13443 return "DW_OP_lit23";
13444 case DW_OP_lit24:
13445 return "DW_OP_lit24";
13446 case DW_OP_lit25:
13447 return "DW_OP_lit25";
13448 case DW_OP_lit26:
13449 return "DW_OP_lit26";
13450 case DW_OP_lit27:
13451 return "DW_OP_lit27";
13452 case DW_OP_lit28:
13453 return "DW_OP_lit28";
13454 case DW_OP_lit29:
13455 return "DW_OP_lit29";
13456 case DW_OP_lit30:
13457 return "DW_OP_lit30";
13458 case DW_OP_lit31:
13459 return "DW_OP_lit31";
13460 case DW_OP_reg0:
13461 return "DW_OP_reg0";
13462 case DW_OP_reg1:
13463 return "DW_OP_reg1";
13464 case DW_OP_reg2:
13465 return "DW_OP_reg2";
13466 case DW_OP_reg3:
13467 return "DW_OP_reg3";
13468 case DW_OP_reg4:
13469 return "DW_OP_reg4";
13470 case DW_OP_reg5:
13471 return "DW_OP_reg5";
13472 case DW_OP_reg6:
13473 return "DW_OP_reg6";
13474 case DW_OP_reg7:
13475 return "DW_OP_reg7";
13476 case DW_OP_reg8:
13477 return "DW_OP_reg8";
13478 case DW_OP_reg9:
13479 return "DW_OP_reg9";
13480 case DW_OP_reg10:
13481 return "DW_OP_reg10";
13482 case DW_OP_reg11:
13483 return "DW_OP_reg11";
13484 case DW_OP_reg12:
13485 return "DW_OP_reg12";
13486 case DW_OP_reg13:
13487 return "DW_OP_reg13";
13488 case DW_OP_reg14:
13489 return "DW_OP_reg14";
13490 case DW_OP_reg15:
13491 return "DW_OP_reg15";
13492 case DW_OP_reg16:
13493 return "DW_OP_reg16";
13494 case DW_OP_reg17:
13495 return "DW_OP_reg17";
13496 case DW_OP_reg18:
13497 return "DW_OP_reg18";
13498 case DW_OP_reg19:
13499 return "DW_OP_reg19";
13500 case DW_OP_reg20:
13501 return "DW_OP_reg20";
13502 case DW_OP_reg21:
13503 return "DW_OP_reg21";
13504 case DW_OP_reg22:
13505 return "DW_OP_reg22";
13506 case DW_OP_reg23:
13507 return "DW_OP_reg23";
13508 case DW_OP_reg24:
13509 return "DW_OP_reg24";
13510 case DW_OP_reg25:
13511 return "DW_OP_reg25";
13512 case DW_OP_reg26:
13513 return "DW_OP_reg26";
13514 case DW_OP_reg27:
13515 return "DW_OP_reg27";
13516 case DW_OP_reg28:
13517 return "DW_OP_reg28";
13518 case DW_OP_reg29:
13519 return "DW_OP_reg29";
13520 case DW_OP_reg30:
13521 return "DW_OP_reg30";
13522 case DW_OP_reg31:
13523 return "DW_OP_reg31";
13524 case DW_OP_breg0:
13525 return "DW_OP_breg0";
13526 case DW_OP_breg1:
13527 return "DW_OP_breg1";
13528 case DW_OP_breg2:
13529 return "DW_OP_breg2";
13530 case DW_OP_breg3:
13531 return "DW_OP_breg3";
13532 case DW_OP_breg4:
13533 return "DW_OP_breg4";
13534 case DW_OP_breg5:
13535 return "DW_OP_breg5";
13536 case DW_OP_breg6:
13537 return "DW_OP_breg6";
13538 case DW_OP_breg7:
13539 return "DW_OP_breg7";
13540 case DW_OP_breg8:
13541 return "DW_OP_breg8";
13542 case DW_OP_breg9:
13543 return "DW_OP_breg9";
13544 case DW_OP_breg10:
13545 return "DW_OP_breg10";
13546 case DW_OP_breg11:
13547 return "DW_OP_breg11";
13548 case DW_OP_breg12:
13549 return "DW_OP_breg12";
13550 case DW_OP_breg13:
13551 return "DW_OP_breg13";
13552 case DW_OP_breg14:
13553 return "DW_OP_breg14";
13554 case DW_OP_breg15:
13555 return "DW_OP_breg15";
13556 case DW_OP_breg16:
13557 return "DW_OP_breg16";
13558 case DW_OP_breg17:
13559 return "DW_OP_breg17";
13560 case DW_OP_breg18:
13561 return "DW_OP_breg18";
13562 case DW_OP_breg19:
13563 return "DW_OP_breg19";
13564 case DW_OP_breg20:
13565 return "DW_OP_breg20";
13566 case DW_OP_breg21:
13567 return "DW_OP_breg21";
13568 case DW_OP_breg22:
13569 return "DW_OP_breg22";
13570 case DW_OP_breg23:
13571 return "DW_OP_breg23";
13572 case DW_OP_breg24:
13573 return "DW_OP_breg24";
13574 case DW_OP_breg25:
13575 return "DW_OP_breg25";
13576 case DW_OP_breg26:
13577 return "DW_OP_breg26";
13578 case DW_OP_breg27:
13579 return "DW_OP_breg27";
13580 case DW_OP_breg28:
13581 return "DW_OP_breg28";
13582 case DW_OP_breg29:
13583 return "DW_OP_breg29";
13584 case DW_OP_breg30:
13585 return "DW_OP_breg30";
13586 case DW_OP_breg31:
13587 return "DW_OP_breg31";
13588 case DW_OP_regx:
13589 return "DW_OP_regx";
13590 case DW_OP_fbreg:
13591 return "DW_OP_fbreg";
13592 case DW_OP_bregx:
13593 return "DW_OP_bregx";
13594 case DW_OP_piece:
13595 return "DW_OP_piece";
13596 case DW_OP_deref_size:
13597 return "DW_OP_deref_size";
13598 case DW_OP_xderef_size:
13599 return "DW_OP_xderef_size";
13600 case DW_OP_nop:
13601 return "DW_OP_nop";
13602 /* DWARF 3 extensions. */
13603 case DW_OP_push_object_address:
13604 return "DW_OP_push_object_address";
13605 case DW_OP_call2:
13606 return "DW_OP_call2";
13607 case DW_OP_call4:
13608 return "DW_OP_call4";
13609 case DW_OP_call_ref:
13610 return "DW_OP_call_ref";
13611 case DW_OP_form_tls_address:
13612 return "DW_OP_form_tls_address";
13613 case DW_OP_call_frame_cfa:
13614 return "DW_OP_call_frame_cfa";
13615 case DW_OP_bit_piece:
13616 return "DW_OP_bit_piece";
13617 /* DWARF 4 extensions. */
13618 case DW_OP_implicit_value:
13619 return "DW_OP_implicit_value";
13620 case DW_OP_stack_value:
13621 return "DW_OP_stack_value";
13622 /* GNU extensions. */
13623 case DW_OP_GNU_push_tls_address:
13624 return "DW_OP_GNU_push_tls_address";
13625 case DW_OP_GNU_uninit:
13626 return "DW_OP_GNU_uninit";
13627 case DW_OP_GNU_implicit_pointer:
13628 return "DW_OP_GNU_implicit_pointer";
13629 case DW_OP_GNU_entry_value:
13630 return "DW_OP_GNU_entry_value";
13631 case DW_OP_GNU_const_type:
13632 return "DW_OP_GNU_const_type";
13633 case DW_OP_GNU_regval_type:
13634 return "DW_OP_GNU_regval_type";
13635 case DW_OP_GNU_deref_type:
13636 return "DW_OP_GNU_deref_type";
13637 case DW_OP_GNU_convert:
13638 return "DW_OP_GNU_convert";
13639 case DW_OP_GNU_reinterpret:
13640 return "DW_OP_GNU_reinterpret";
13641 default:
13642 return NULL;
13643 }
13644 }
13645
13646 static char *
13647 dwarf_bool_name (unsigned mybool)
13648 {
13649 if (mybool)
13650 return "TRUE";
13651 else
13652 return "FALSE";
13653 }
13654
13655 /* Convert a DWARF type code into its string name. */
13656
13657 static char *
13658 dwarf_type_encoding_name (unsigned enc)
13659 {
13660 switch (enc)
13661 {
13662 case DW_ATE_void:
13663 return "DW_ATE_void";
13664 case DW_ATE_address:
13665 return "DW_ATE_address";
13666 case DW_ATE_boolean:
13667 return "DW_ATE_boolean";
13668 case DW_ATE_complex_float:
13669 return "DW_ATE_complex_float";
13670 case DW_ATE_float:
13671 return "DW_ATE_float";
13672 case DW_ATE_signed:
13673 return "DW_ATE_signed";
13674 case DW_ATE_signed_char:
13675 return "DW_ATE_signed_char";
13676 case DW_ATE_unsigned:
13677 return "DW_ATE_unsigned";
13678 case DW_ATE_unsigned_char:
13679 return "DW_ATE_unsigned_char";
13680 /* DWARF 3. */
13681 case DW_ATE_imaginary_float:
13682 return "DW_ATE_imaginary_float";
13683 case DW_ATE_packed_decimal:
13684 return "DW_ATE_packed_decimal";
13685 case DW_ATE_numeric_string:
13686 return "DW_ATE_numeric_string";
13687 case DW_ATE_edited:
13688 return "DW_ATE_edited";
13689 case DW_ATE_signed_fixed:
13690 return "DW_ATE_signed_fixed";
13691 case DW_ATE_unsigned_fixed:
13692 return "DW_ATE_unsigned_fixed";
13693 case DW_ATE_decimal_float:
13694 return "DW_ATE_decimal_float";
13695 /* DWARF 4. */
13696 case DW_ATE_UTF:
13697 return "DW_ATE_UTF";
13698 /* HP extensions. */
13699 case DW_ATE_HP_float80:
13700 return "DW_ATE_HP_float80";
13701 case DW_ATE_HP_complex_float80:
13702 return "DW_ATE_HP_complex_float80";
13703 case DW_ATE_HP_float128:
13704 return "DW_ATE_HP_float128";
13705 case DW_ATE_HP_complex_float128:
13706 return "DW_ATE_HP_complex_float128";
13707 case DW_ATE_HP_floathpintel:
13708 return "DW_ATE_HP_floathpintel";
13709 case DW_ATE_HP_imaginary_float80:
13710 return "DW_ATE_HP_imaginary_float80";
13711 case DW_ATE_HP_imaginary_float128:
13712 return "DW_ATE_HP_imaginary_float128";
13713 default:
13714 return "DW_ATE_<unknown>";
13715 }
13716 }
13717
13718 /* Convert a DWARF call frame info operation to its string name. */
13719
13720 #if 0
13721 static char *
13722 dwarf_cfi_name (unsigned cfi_opc)
13723 {
13724 switch (cfi_opc)
13725 {
13726 case DW_CFA_advance_loc:
13727 return "DW_CFA_advance_loc";
13728 case DW_CFA_offset:
13729 return "DW_CFA_offset";
13730 case DW_CFA_restore:
13731 return "DW_CFA_restore";
13732 case DW_CFA_nop:
13733 return "DW_CFA_nop";
13734 case DW_CFA_set_loc:
13735 return "DW_CFA_set_loc";
13736 case DW_CFA_advance_loc1:
13737 return "DW_CFA_advance_loc1";
13738 case DW_CFA_advance_loc2:
13739 return "DW_CFA_advance_loc2";
13740 case DW_CFA_advance_loc4:
13741 return "DW_CFA_advance_loc4";
13742 case DW_CFA_offset_extended:
13743 return "DW_CFA_offset_extended";
13744 case DW_CFA_restore_extended:
13745 return "DW_CFA_restore_extended";
13746 case DW_CFA_undefined:
13747 return "DW_CFA_undefined";
13748 case DW_CFA_same_value:
13749 return "DW_CFA_same_value";
13750 case DW_CFA_register:
13751 return "DW_CFA_register";
13752 case DW_CFA_remember_state:
13753 return "DW_CFA_remember_state";
13754 case DW_CFA_restore_state:
13755 return "DW_CFA_restore_state";
13756 case DW_CFA_def_cfa:
13757 return "DW_CFA_def_cfa";
13758 case DW_CFA_def_cfa_register:
13759 return "DW_CFA_def_cfa_register";
13760 case DW_CFA_def_cfa_offset:
13761 return "DW_CFA_def_cfa_offset";
13762 /* DWARF 3. */
13763 case DW_CFA_def_cfa_expression:
13764 return "DW_CFA_def_cfa_expression";
13765 case DW_CFA_expression:
13766 return "DW_CFA_expression";
13767 case DW_CFA_offset_extended_sf:
13768 return "DW_CFA_offset_extended_sf";
13769 case DW_CFA_def_cfa_sf:
13770 return "DW_CFA_def_cfa_sf";
13771 case DW_CFA_def_cfa_offset_sf:
13772 return "DW_CFA_def_cfa_offset_sf";
13773 case DW_CFA_val_offset:
13774 return "DW_CFA_val_offset";
13775 case DW_CFA_val_offset_sf:
13776 return "DW_CFA_val_offset_sf";
13777 case DW_CFA_val_expression:
13778 return "DW_CFA_val_expression";
13779 /* SGI/MIPS specific. */
13780 case DW_CFA_MIPS_advance_loc8:
13781 return "DW_CFA_MIPS_advance_loc8";
13782 /* GNU extensions. */
13783 case DW_CFA_GNU_window_save:
13784 return "DW_CFA_GNU_window_save";
13785 case DW_CFA_GNU_args_size:
13786 return "DW_CFA_GNU_args_size";
13787 case DW_CFA_GNU_negative_offset_extended:
13788 return "DW_CFA_GNU_negative_offset_extended";
13789 default:
13790 return "DW_CFA_<unknown>";
13791 }
13792 }
13793 #endif
13794
13795 static void
13796 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13797 {
13798 unsigned int i;
13799
13800 print_spaces (indent, f);
13801 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13802 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13803
13804 if (die->parent != NULL)
13805 {
13806 print_spaces (indent, f);
13807 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13808 die->parent->offset);
13809 }
13810
13811 print_spaces (indent, f);
13812 fprintf_unfiltered (f, " has children: %s\n",
13813 dwarf_bool_name (die->child != NULL));
13814
13815 print_spaces (indent, f);
13816 fprintf_unfiltered (f, " attributes:\n");
13817
13818 for (i = 0; i < die->num_attrs; ++i)
13819 {
13820 print_spaces (indent, f);
13821 fprintf_unfiltered (f, " %s (%s) ",
13822 dwarf_attr_name (die->attrs[i].name),
13823 dwarf_form_name (die->attrs[i].form));
13824
13825 switch (die->attrs[i].form)
13826 {
13827 case DW_FORM_ref_addr:
13828 case DW_FORM_addr:
13829 fprintf_unfiltered (f, "address: ");
13830 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13831 break;
13832 case DW_FORM_block2:
13833 case DW_FORM_block4:
13834 case DW_FORM_block:
13835 case DW_FORM_block1:
13836 fprintf_unfiltered (f, "block: size %d",
13837 DW_BLOCK (&die->attrs[i])->size);
13838 break;
13839 case DW_FORM_exprloc:
13840 fprintf_unfiltered (f, "expression: size %u",
13841 DW_BLOCK (&die->attrs[i])->size);
13842 break;
13843 case DW_FORM_ref1:
13844 case DW_FORM_ref2:
13845 case DW_FORM_ref4:
13846 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13847 (long) (DW_ADDR (&die->attrs[i])));
13848 break;
13849 case DW_FORM_data1:
13850 case DW_FORM_data2:
13851 case DW_FORM_data4:
13852 case DW_FORM_data8:
13853 case DW_FORM_udata:
13854 case DW_FORM_sdata:
13855 fprintf_unfiltered (f, "constant: %s",
13856 pulongest (DW_UNSND (&die->attrs[i])));
13857 break;
13858 case DW_FORM_sec_offset:
13859 fprintf_unfiltered (f, "section offset: %s",
13860 pulongest (DW_UNSND (&die->attrs[i])));
13861 break;
13862 case DW_FORM_ref_sig8:
13863 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13864 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13865 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13866 else
13867 fprintf_unfiltered (f, "signatured type, offset: unknown");
13868 break;
13869 case DW_FORM_string:
13870 case DW_FORM_strp:
13871 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13872 DW_STRING (&die->attrs[i])
13873 ? DW_STRING (&die->attrs[i]) : "",
13874 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13875 break;
13876 case DW_FORM_flag:
13877 if (DW_UNSND (&die->attrs[i]))
13878 fprintf_unfiltered (f, "flag: TRUE");
13879 else
13880 fprintf_unfiltered (f, "flag: FALSE");
13881 break;
13882 case DW_FORM_flag_present:
13883 fprintf_unfiltered (f, "flag: TRUE");
13884 break;
13885 case DW_FORM_indirect:
13886 /* The reader will have reduced the indirect form to
13887 the "base form" so this form should not occur. */
13888 fprintf_unfiltered (f,
13889 "unexpected attribute form: DW_FORM_indirect");
13890 break;
13891 default:
13892 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13893 die->attrs[i].form);
13894 break;
13895 }
13896 fprintf_unfiltered (f, "\n");
13897 }
13898 }
13899
13900 static void
13901 dump_die_for_error (struct die_info *die)
13902 {
13903 dump_die_shallow (gdb_stderr, 0, die);
13904 }
13905
13906 static void
13907 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13908 {
13909 int indent = level * 4;
13910
13911 gdb_assert (die != NULL);
13912
13913 if (level >= max_level)
13914 return;
13915
13916 dump_die_shallow (f, indent, die);
13917
13918 if (die->child != NULL)
13919 {
13920 print_spaces (indent, f);
13921 fprintf_unfiltered (f, " Children:");
13922 if (level + 1 < max_level)
13923 {
13924 fprintf_unfiltered (f, "\n");
13925 dump_die_1 (f, level + 1, max_level, die->child);
13926 }
13927 else
13928 {
13929 fprintf_unfiltered (f,
13930 " [not printed, max nesting level reached]\n");
13931 }
13932 }
13933
13934 if (die->sibling != NULL && level > 0)
13935 {
13936 dump_die_1 (f, level, max_level, die->sibling);
13937 }
13938 }
13939
13940 /* This is called from the pdie macro in gdbinit.in.
13941 It's not static so gcc will keep a copy callable from gdb. */
13942
13943 void
13944 dump_die (struct die_info *die, int max_level)
13945 {
13946 dump_die_1 (gdb_stdlog, 0, max_level, die);
13947 }
13948
13949 static void
13950 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13951 {
13952 void **slot;
13953
13954 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13955
13956 *slot = die;
13957 }
13958
13959 static int
13960 is_ref_attr (struct attribute *attr)
13961 {
13962 switch (attr->form)
13963 {
13964 case DW_FORM_ref_addr:
13965 case DW_FORM_ref1:
13966 case DW_FORM_ref2:
13967 case DW_FORM_ref4:
13968 case DW_FORM_ref8:
13969 case DW_FORM_ref_udata:
13970 return 1;
13971 default:
13972 return 0;
13973 }
13974 }
13975
13976 static unsigned int
13977 dwarf2_get_ref_die_offset (struct attribute *attr)
13978 {
13979 if (is_ref_attr (attr))
13980 return DW_ADDR (attr);
13981
13982 complaint (&symfile_complaints,
13983 _("unsupported die ref attribute form: '%s'"),
13984 dwarf_form_name (attr->form));
13985 return 0;
13986 }
13987
13988 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13989 * the value held by the attribute is not constant. */
13990
13991 static LONGEST
13992 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13993 {
13994 if (attr->form == DW_FORM_sdata)
13995 return DW_SND (attr);
13996 else if (attr->form == DW_FORM_udata
13997 || attr->form == DW_FORM_data1
13998 || attr->form == DW_FORM_data2
13999 || attr->form == DW_FORM_data4
14000 || attr->form == DW_FORM_data8)
14001 return DW_UNSND (attr);
14002 else
14003 {
14004 complaint (&symfile_complaints,
14005 _("Attribute value is not a constant (%s)"),
14006 dwarf_form_name (attr->form));
14007 return default_value;
14008 }
14009 }
14010
14011 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14012 unit and add it to our queue.
14013 The result is non-zero if PER_CU was queued, otherwise the result is zero
14014 meaning either PER_CU is already queued or it is already loaded. */
14015
14016 static int
14017 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14018 struct dwarf2_per_cu_data *per_cu)
14019 {
14020 /* We may arrive here during partial symbol reading, if we need full
14021 DIEs to process an unusual case (e.g. template arguments). Do
14022 not queue PER_CU, just tell our caller to load its DIEs. */
14023 if (dwarf2_per_objfile->reading_partial_symbols)
14024 {
14025 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14026 return 1;
14027 return 0;
14028 }
14029
14030 /* Mark the dependence relation so that we don't flush PER_CU
14031 too early. */
14032 dwarf2_add_dependence (this_cu, per_cu);
14033
14034 /* If it's already on the queue, we have nothing to do. */
14035 if (per_cu->queued)
14036 return 0;
14037
14038 /* If the compilation unit is already loaded, just mark it as
14039 used. */
14040 if (per_cu->cu != NULL)
14041 {
14042 per_cu->cu->last_used = 0;
14043 return 0;
14044 }
14045
14046 /* Add it to the queue. */
14047 queue_comp_unit (per_cu);
14048
14049 return 1;
14050 }
14051
14052 /* Follow reference or signature attribute ATTR of SRC_DIE.
14053 On entry *REF_CU is the CU of SRC_DIE.
14054 On exit *REF_CU is the CU of the result. */
14055
14056 static struct die_info *
14057 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14058 struct dwarf2_cu **ref_cu)
14059 {
14060 struct die_info *die;
14061
14062 if (is_ref_attr (attr))
14063 die = follow_die_ref (src_die, attr, ref_cu);
14064 else if (attr->form == DW_FORM_ref_sig8)
14065 die = follow_die_sig (src_die, attr, ref_cu);
14066 else
14067 {
14068 dump_die_for_error (src_die);
14069 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14070 (*ref_cu)->objfile->name);
14071 }
14072
14073 return die;
14074 }
14075
14076 /* Follow reference OFFSET.
14077 On entry *REF_CU is the CU of the source die referencing OFFSET.
14078 On exit *REF_CU is the CU of the result.
14079 Returns NULL if OFFSET is invalid. */
14080
14081 static struct die_info *
14082 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14083 {
14084 struct die_info temp_die;
14085 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14086
14087 gdb_assert (cu->per_cu != NULL);
14088
14089 target_cu = cu;
14090
14091 if (cu->per_cu->debug_types_section)
14092 {
14093 /* .debug_types CUs cannot reference anything outside their CU.
14094 If they need to, they have to reference a signatured type via
14095 DW_FORM_ref_sig8. */
14096 if (! offset_in_cu_p (&cu->header, offset))
14097 return NULL;
14098 }
14099 else if (! offset_in_cu_p (&cu->header, offset))
14100 {
14101 struct dwarf2_per_cu_data *per_cu;
14102
14103 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14104
14105 /* If necessary, add it to the queue and load its DIEs. */
14106 if (maybe_queue_comp_unit (cu, per_cu))
14107 load_full_comp_unit (per_cu);
14108
14109 target_cu = per_cu->cu;
14110 }
14111 else if (cu->dies == NULL)
14112 {
14113 /* We're loading full DIEs during partial symbol reading. */
14114 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14115 load_full_comp_unit (cu->per_cu);
14116 }
14117
14118 *ref_cu = target_cu;
14119 temp_die.offset = offset;
14120 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14121 }
14122
14123 /* Follow reference attribute ATTR of SRC_DIE.
14124 On entry *REF_CU is the CU of SRC_DIE.
14125 On exit *REF_CU is the CU of the result. */
14126
14127 static struct die_info *
14128 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14129 struct dwarf2_cu **ref_cu)
14130 {
14131 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14132 struct dwarf2_cu *cu = *ref_cu;
14133 struct die_info *die;
14134
14135 die = follow_die_offset (offset, ref_cu);
14136 if (!die)
14137 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14138 "at 0x%x [in module %s]"),
14139 offset, src_die->offset, cu->objfile->name);
14140
14141 return die;
14142 }
14143
14144 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14145 Returned value is intended for DW_OP_call*. Returned
14146 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14147
14148 struct dwarf2_locexpr_baton
14149 dwarf2_fetch_die_location_block (unsigned int offset,
14150 struct dwarf2_per_cu_data *per_cu,
14151 CORE_ADDR (*get_frame_pc) (void *baton),
14152 void *baton)
14153 {
14154 struct dwarf2_cu *cu;
14155 struct die_info *die;
14156 struct attribute *attr;
14157 struct dwarf2_locexpr_baton retval;
14158
14159 dw2_setup (per_cu->objfile);
14160
14161 if (per_cu->cu == NULL)
14162 load_cu (per_cu);
14163 cu = per_cu->cu;
14164
14165 die = follow_die_offset (offset, &cu);
14166 if (!die)
14167 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14168 offset, per_cu->objfile->name);
14169
14170 attr = dwarf2_attr (die, DW_AT_location, cu);
14171 if (!attr)
14172 {
14173 /* DWARF: "If there is no such attribute, then there is no effect.".
14174 DATA is ignored if SIZE is 0. */
14175
14176 retval.data = NULL;
14177 retval.size = 0;
14178 }
14179 else if (attr_form_is_section_offset (attr))
14180 {
14181 struct dwarf2_loclist_baton loclist_baton;
14182 CORE_ADDR pc = (*get_frame_pc) (baton);
14183 size_t size;
14184
14185 fill_in_loclist_baton (cu, &loclist_baton, attr);
14186
14187 retval.data = dwarf2_find_location_expression (&loclist_baton,
14188 &size, pc);
14189 retval.size = size;
14190 }
14191 else
14192 {
14193 if (!attr_form_is_block (attr))
14194 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14195 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14196 offset, per_cu->objfile->name);
14197
14198 retval.data = DW_BLOCK (attr)->data;
14199 retval.size = DW_BLOCK (attr)->size;
14200 }
14201 retval.per_cu = cu->per_cu;
14202
14203 age_cached_comp_units ();
14204
14205 return retval;
14206 }
14207
14208 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14209 PER_CU. */
14210
14211 struct type *
14212 dwarf2_get_die_type (unsigned int die_offset,
14213 struct dwarf2_per_cu_data *per_cu)
14214 {
14215 dw2_setup (per_cu->objfile);
14216 return get_die_type_at_offset (die_offset, per_cu);
14217 }
14218
14219 /* Follow the signature attribute ATTR in SRC_DIE.
14220 On entry *REF_CU is the CU of SRC_DIE.
14221 On exit *REF_CU is the CU of the result. */
14222
14223 static struct die_info *
14224 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14225 struct dwarf2_cu **ref_cu)
14226 {
14227 struct objfile *objfile = (*ref_cu)->objfile;
14228 struct die_info temp_die;
14229 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14230 struct dwarf2_cu *sig_cu;
14231 struct die_info *die;
14232
14233 /* sig_type will be NULL if the signatured type is missing from
14234 the debug info. */
14235 if (sig_type == NULL)
14236 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14237 "at 0x%x [in module %s]"),
14238 src_die->offset, objfile->name);
14239
14240 /* If necessary, add it to the queue and load its DIEs. */
14241
14242 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14243 read_signatured_type (sig_type);
14244
14245 gdb_assert (sig_type->per_cu.cu != NULL);
14246
14247 sig_cu = sig_type->per_cu.cu;
14248 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14249 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14250 if (die)
14251 {
14252 *ref_cu = sig_cu;
14253 return die;
14254 }
14255
14256 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14257 "from DIE at 0x%x [in module %s]"),
14258 sig_type->type_offset, src_die->offset, objfile->name);
14259 }
14260
14261 /* Given an offset of a signatured type, return its signatured_type. */
14262
14263 static struct signatured_type *
14264 lookup_signatured_type_at_offset (struct objfile *objfile,
14265 struct dwarf2_section_info *section,
14266 unsigned int offset)
14267 {
14268 gdb_byte *info_ptr = section->buffer + offset;
14269 unsigned int length, initial_length_size;
14270 unsigned int sig_offset;
14271 struct signatured_type find_entry, *type_sig;
14272
14273 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14274 sig_offset = (initial_length_size
14275 + 2 /*version*/
14276 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14277 + 1 /*address_size*/);
14278 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14279 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14280
14281 /* This is only used to lookup previously recorded types.
14282 If we didn't find it, it's our bug. */
14283 gdb_assert (type_sig != NULL);
14284 gdb_assert (offset == type_sig->per_cu.offset);
14285
14286 return type_sig;
14287 }
14288
14289 /* Load the DIEs associated with type unit PER_CU into memory. */
14290
14291 static void
14292 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14293 {
14294 struct objfile *objfile = per_cu->objfile;
14295 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14296 unsigned int offset = per_cu->offset;
14297 struct signatured_type *type_sig;
14298
14299 dwarf2_read_section (objfile, sect);
14300
14301 /* We have the section offset, but we need the signature to do the
14302 hash table lookup. */
14303 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14304 the signature to assert we found the right one.
14305 Ok, but it's a lot of work. We should simplify things so any needed
14306 assert doesn't require all this clumsiness. */
14307 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14308
14309 gdb_assert (type_sig->per_cu.cu == NULL);
14310
14311 read_signatured_type (type_sig);
14312
14313 gdb_assert (type_sig->per_cu.cu != NULL);
14314 }
14315
14316 /* Read in a signatured type and build its CU and DIEs. */
14317
14318 static void
14319 read_signatured_type (struct signatured_type *type_sig)
14320 {
14321 struct objfile *objfile = type_sig->per_cu.objfile;
14322 gdb_byte *types_ptr;
14323 struct die_reader_specs reader_specs;
14324 struct dwarf2_cu *cu;
14325 ULONGEST signature;
14326 struct cleanup *back_to, *free_cu_cleanup;
14327 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14328
14329 dwarf2_read_section (objfile, section);
14330 types_ptr = section->buffer + type_sig->per_cu.offset;
14331
14332 gdb_assert (type_sig->per_cu.cu == NULL);
14333
14334 cu = xmalloc (sizeof (*cu));
14335 init_one_comp_unit (cu, &type_sig->per_cu);
14336
14337 /* If an error occurs while loading, release our storage. */
14338 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14339
14340 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14341 types_ptr, objfile->obfd);
14342 gdb_assert (signature == type_sig->signature);
14343
14344 cu->die_hash
14345 = htab_create_alloc_ex (cu->header.length / 12,
14346 die_hash,
14347 die_eq,
14348 NULL,
14349 &cu->comp_unit_obstack,
14350 hashtab_obstack_allocate,
14351 dummy_obstack_deallocate);
14352
14353 dwarf2_read_abbrevs (cu);
14354 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14355
14356 init_cu_die_reader (&reader_specs, cu);
14357
14358 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14359 NULL /*parent*/);
14360
14361 /* We try not to read any attributes in this function, because not
14362 all CUs needed for references have been loaded yet, and symbol
14363 table processing isn't initialized. But we have to set the CU language,
14364 or we won't be able to build types correctly. */
14365 prepare_one_comp_unit (cu, cu->dies);
14366
14367 do_cleanups (back_to);
14368
14369 /* We've successfully allocated this compilation unit. Let our caller
14370 clean it up when finished with it. */
14371 discard_cleanups (free_cu_cleanup);
14372
14373 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14374 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14375 }
14376
14377 /* Decode simple location descriptions.
14378 Given a pointer to a dwarf block that defines a location, compute
14379 the location and return the value.
14380
14381 NOTE drow/2003-11-18: This function is called in two situations
14382 now: for the address of static or global variables (partial symbols
14383 only) and for offsets into structures which are expected to be
14384 (more or less) constant. The partial symbol case should go away,
14385 and only the constant case should remain. That will let this
14386 function complain more accurately. A few special modes are allowed
14387 without complaint for global variables (for instance, global
14388 register values and thread-local values).
14389
14390 A location description containing no operations indicates that the
14391 object is optimized out. The return value is 0 for that case.
14392 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14393 callers will only want a very basic result and this can become a
14394 complaint.
14395
14396 Note that stack[0] is unused except as a default error return. */
14397
14398 static CORE_ADDR
14399 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14400 {
14401 struct objfile *objfile = cu->objfile;
14402 int i;
14403 int size = blk->size;
14404 gdb_byte *data = blk->data;
14405 CORE_ADDR stack[64];
14406 int stacki;
14407 unsigned int bytes_read, unsnd;
14408 gdb_byte op;
14409
14410 i = 0;
14411 stacki = 0;
14412 stack[stacki] = 0;
14413 stack[++stacki] = 0;
14414
14415 while (i < size)
14416 {
14417 op = data[i++];
14418 switch (op)
14419 {
14420 case DW_OP_lit0:
14421 case DW_OP_lit1:
14422 case DW_OP_lit2:
14423 case DW_OP_lit3:
14424 case DW_OP_lit4:
14425 case DW_OP_lit5:
14426 case DW_OP_lit6:
14427 case DW_OP_lit7:
14428 case DW_OP_lit8:
14429 case DW_OP_lit9:
14430 case DW_OP_lit10:
14431 case DW_OP_lit11:
14432 case DW_OP_lit12:
14433 case DW_OP_lit13:
14434 case DW_OP_lit14:
14435 case DW_OP_lit15:
14436 case DW_OP_lit16:
14437 case DW_OP_lit17:
14438 case DW_OP_lit18:
14439 case DW_OP_lit19:
14440 case DW_OP_lit20:
14441 case DW_OP_lit21:
14442 case DW_OP_lit22:
14443 case DW_OP_lit23:
14444 case DW_OP_lit24:
14445 case DW_OP_lit25:
14446 case DW_OP_lit26:
14447 case DW_OP_lit27:
14448 case DW_OP_lit28:
14449 case DW_OP_lit29:
14450 case DW_OP_lit30:
14451 case DW_OP_lit31:
14452 stack[++stacki] = op - DW_OP_lit0;
14453 break;
14454
14455 case DW_OP_reg0:
14456 case DW_OP_reg1:
14457 case DW_OP_reg2:
14458 case DW_OP_reg3:
14459 case DW_OP_reg4:
14460 case DW_OP_reg5:
14461 case DW_OP_reg6:
14462 case DW_OP_reg7:
14463 case DW_OP_reg8:
14464 case DW_OP_reg9:
14465 case DW_OP_reg10:
14466 case DW_OP_reg11:
14467 case DW_OP_reg12:
14468 case DW_OP_reg13:
14469 case DW_OP_reg14:
14470 case DW_OP_reg15:
14471 case DW_OP_reg16:
14472 case DW_OP_reg17:
14473 case DW_OP_reg18:
14474 case DW_OP_reg19:
14475 case DW_OP_reg20:
14476 case DW_OP_reg21:
14477 case DW_OP_reg22:
14478 case DW_OP_reg23:
14479 case DW_OP_reg24:
14480 case DW_OP_reg25:
14481 case DW_OP_reg26:
14482 case DW_OP_reg27:
14483 case DW_OP_reg28:
14484 case DW_OP_reg29:
14485 case DW_OP_reg30:
14486 case DW_OP_reg31:
14487 stack[++stacki] = op - DW_OP_reg0;
14488 if (i < size)
14489 dwarf2_complex_location_expr_complaint ();
14490 break;
14491
14492 case DW_OP_regx:
14493 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14494 i += bytes_read;
14495 stack[++stacki] = unsnd;
14496 if (i < size)
14497 dwarf2_complex_location_expr_complaint ();
14498 break;
14499
14500 case DW_OP_addr:
14501 stack[++stacki] = read_address (objfile->obfd, &data[i],
14502 cu, &bytes_read);
14503 i += bytes_read;
14504 break;
14505
14506 case DW_OP_const1u:
14507 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14508 i += 1;
14509 break;
14510
14511 case DW_OP_const1s:
14512 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14513 i += 1;
14514 break;
14515
14516 case DW_OP_const2u:
14517 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14518 i += 2;
14519 break;
14520
14521 case DW_OP_const2s:
14522 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14523 i += 2;
14524 break;
14525
14526 case DW_OP_const4u:
14527 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14528 i += 4;
14529 break;
14530
14531 case DW_OP_const4s:
14532 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14533 i += 4;
14534 break;
14535
14536 case DW_OP_const8u:
14537 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14538 i += 8;
14539 break;
14540
14541 case DW_OP_constu:
14542 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14543 &bytes_read);
14544 i += bytes_read;
14545 break;
14546
14547 case DW_OP_consts:
14548 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14549 i += bytes_read;
14550 break;
14551
14552 case DW_OP_dup:
14553 stack[stacki + 1] = stack[stacki];
14554 stacki++;
14555 break;
14556
14557 case DW_OP_plus:
14558 stack[stacki - 1] += stack[stacki];
14559 stacki--;
14560 break;
14561
14562 case DW_OP_plus_uconst:
14563 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14564 &bytes_read);
14565 i += bytes_read;
14566 break;
14567
14568 case DW_OP_minus:
14569 stack[stacki - 1] -= stack[stacki];
14570 stacki--;
14571 break;
14572
14573 case DW_OP_deref:
14574 /* If we're not the last op, then we definitely can't encode
14575 this using GDB's address_class enum. This is valid for partial
14576 global symbols, although the variable's address will be bogus
14577 in the psymtab. */
14578 if (i < size)
14579 dwarf2_complex_location_expr_complaint ();
14580 break;
14581
14582 case DW_OP_GNU_push_tls_address:
14583 /* The top of the stack has the offset from the beginning
14584 of the thread control block at which the variable is located. */
14585 /* Nothing should follow this operator, so the top of stack would
14586 be returned. */
14587 /* This is valid for partial global symbols, but the variable's
14588 address will be bogus in the psymtab. Make it always at least
14589 non-zero to not look as a variable garbage collected by linker
14590 which have DW_OP_addr 0. */
14591 if (i < size)
14592 dwarf2_complex_location_expr_complaint ();
14593 stack[stacki]++;
14594 break;
14595
14596 case DW_OP_GNU_uninit:
14597 break;
14598
14599 default:
14600 {
14601 const char *name = dwarf_stack_op_name (op);
14602
14603 if (name)
14604 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14605 name);
14606 else
14607 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14608 op);
14609 }
14610
14611 return (stack[stacki]);
14612 }
14613
14614 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14615 outside of the allocated space. Also enforce minimum>0. */
14616 if (stacki >= ARRAY_SIZE (stack) - 1)
14617 {
14618 complaint (&symfile_complaints,
14619 _("location description stack overflow"));
14620 return 0;
14621 }
14622
14623 if (stacki <= 0)
14624 {
14625 complaint (&symfile_complaints,
14626 _("location description stack underflow"));
14627 return 0;
14628 }
14629 }
14630 return (stack[stacki]);
14631 }
14632
14633 /* memory allocation interface */
14634
14635 static struct dwarf_block *
14636 dwarf_alloc_block (struct dwarf2_cu *cu)
14637 {
14638 struct dwarf_block *blk;
14639
14640 blk = (struct dwarf_block *)
14641 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14642 return (blk);
14643 }
14644
14645 static struct abbrev_info *
14646 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14647 {
14648 struct abbrev_info *abbrev;
14649
14650 abbrev = (struct abbrev_info *)
14651 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14652 memset (abbrev, 0, sizeof (struct abbrev_info));
14653 return (abbrev);
14654 }
14655
14656 static struct die_info *
14657 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14658 {
14659 struct die_info *die;
14660 size_t size = sizeof (struct die_info);
14661
14662 if (num_attrs > 1)
14663 size += (num_attrs - 1) * sizeof (struct attribute);
14664
14665 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14666 memset (die, 0, sizeof (struct die_info));
14667 return (die);
14668 }
14669
14670 \f
14671 /* Macro support. */
14672
14673 /* Return the full name of file number I in *LH's file name table.
14674 Use COMP_DIR as the name of the current directory of the
14675 compilation. The result is allocated using xmalloc; the caller is
14676 responsible for freeing it. */
14677 static char *
14678 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14679 {
14680 /* Is the file number a valid index into the line header's file name
14681 table? Remember that file numbers start with one, not zero. */
14682 if (1 <= file && file <= lh->num_file_names)
14683 {
14684 struct file_entry *fe = &lh->file_names[file - 1];
14685
14686 if (IS_ABSOLUTE_PATH (fe->name))
14687 return xstrdup (fe->name);
14688 else
14689 {
14690 const char *dir;
14691 int dir_len;
14692 char *full_name;
14693
14694 if (fe->dir_index)
14695 dir = lh->include_dirs[fe->dir_index - 1];
14696 else
14697 dir = comp_dir;
14698
14699 if (dir)
14700 {
14701 dir_len = strlen (dir);
14702 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14703 strcpy (full_name, dir);
14704 full_name[dir_len] = '/';
14705 strcpy (full_name + dir_len + 1, fe->name);
14706 return full_name;
14707 }
14708 else
14709 return xstrdup (fe->name);
14710 }
14711 }
14712 else
14713 {
14714 /* The compiler produced a bogus file number. We can at least
14715 record the macro definitions made in the file, even if we
14716 won't be able to find the file by name. */
14717 char fake_name[80];
14718
14719 sprintf (fake_name, "<bad macro file number %d>", file);
14720
14721 complaint (&symfile_complaints,
14722 _("bad file number in macro information (%d)"),
14723 file);
14724
14725 return xstrdup (fake_name);
14726 }
14727 }
14728
14729
14730 static struct macro_source_file *
14731 macro_start_file (int file, int line,
14732 struct macro_source_file *current_file,
14733 const char *comp_dir,
14734 struct line_header *lh, struct objfile *objfile)
14735 {
14736 /* The full name of this source file. */
14737 char *full_name = file_full_name (file, lh, comp_dir);
14738
14739 /* We don't create a macro table for this compilation unit
14740 at all until we actually get a filename. */
14741 if (! pending_macros)
14742 pending_macros = new_macro_table (&objfile->objfile_obstack,
14743 objfile->macro_cache);
14744
14745 if (! current_file)
14746 /* If we have no current file, then this must be the start_file
14747 directive for the compilation unit's main source file. */
14748 current_file = macro_set_main (pending_macros, full_name);
14749 else
14750 current_file = macro_include (current_file, line, full_name);
14751
14752 xfree (full_name);
14753
14754 return current_file;
14755 }
14756
14757
14758 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14759 followed by a null byte. */
14760 static char *
14761 copy_string (const char *buf, int len)
14762 {
14763 char *s = xmalloc (len + 1);
14764
14765 memcpy (s, buf, len);
14766 s[len] = '\0';
14767 return s;
14768 }
14769
14770
14771 static const char *
14772 consume_improper_spaces (const char *p, const char *body)
14773 {
14774 if (*p == ' ')
14775 {
14776 complaint (&symfile_complaints,
14777 _("macro definition contains spaces "
14778 "in formal argument list:\n`%s'"),
14779 body);
14780
14781 while (*p == ' ')
14782 p++;
14783 }
14784
14785 return p;
14786 }
14787
14788
14789 static void
14790 parse_macro_definition (struct macro_source_file *file, int line,
14791 const char *body)
14792 {
14793 const char *p;
14794
14795 /* The body string takes one of two forms. For object-like macro
14796 definitions, it should be:
14797
14798 <macro name> " " <definition>
14799
14800 For function-like macro definitions, it should be:
14801
14802 <macro name> "() " <definition>
14803 or
14804 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14805
14806 Spaces may appear only where explicitly indicated, and in the
14807 <definition>.
14808
14809 The Dwarf 2 spec says that an object-like macro's name is always
14810 followed by a space, but versions of GCC around March 2002 omit
14811 the space when the macro's definition is the empty string.
14812
14813 The Dwarf 2 spec says that there should be no spaces between the
14814 formal arguments in a function-like macro's formal argument list,
14815 but versions of GCC around March 2002 include spaces after the
14816 commas. */
14817
14818
14819 /* Find the extent of the macro name. The macro name is terminated
14820 by either a space or null character (for an object-like macro) or
14821 an opening paren (for a function-like macro). */
14822 for (p = body; *p; p++)
14823 if (*p == ' ' || *p == '(')
14824 break;
14825
14826 if (*p == ' ' || *p == '\0')
14827 {
14828 /* It's an object-like macro. */
14829 int name_len = p - body;
14830 char *name = copy_string (body, name_len);
14831 const char *replacement;
14832
14833 if (*p == ' ')
14834 replacement = body + name_len + 1;
14835 else
14836 {
14837 dwarf2_macro_malformed_definition_complaint (body);
14838 replacement = body + name_len;
14839 }
14840
14841 macro_define_object (file, line, name, replacement);
14842
14843 xfree (name);
14844 }
14845 else if (*p == '(')
14846 {
14847 /* It's a function-like macro. */
14848 char *name = copy_string (body, p - body);
14849 int argc = 0;
14850 int argv_size = 1;
14851 char **argv = xmalloc (argv_size * sizeof (*argv));
14852
14853 p++;
14854
14855 p = consume_improper_spaces (p, body);
14856
14857 /* Parse the formal argument list. */
14858 while (*p && *p != ')')
14859 {
14860 /* Find the extent of the current argument name. */
14861 const char *arg_start = p;
14862
14863 while (*p && *p != ',' && *p != ')' && *p != ' ')
14864 p++;
14865
14866 if (! *p || p == arg_start)
14867 dwarf2_macro_malformed_definition_complaint (body);
14868 else
14869 {
14870 /* Make sure argv has room for the new argument. */
14871 if (argc >= argv_size)
14872 {
14873 argv_size *= 2;
14874 argv = xrealloc (argv, argv_size * sizeof (*argv));
14875 }
14876
14877 argv[argc++] = copy_string (arg_start, p - arg_start);
14878 }
14879
14880 p = consume_improper_spaces (p, body);
14881
14882 /* Consume the comma, if present. */
14883 if (*p == ',')
14884 {
14885 p++;
14886
14887 p = consume_improper_spaces (p, body);
14888 }
14889 }
14890
14891 if (*p == ')')
14892 {
14893 p++;
14894
14895 if (*p == ' ')
14896 /* Perfectly formed definition, no complaints. */
14897 macro_define_function (file, line, name,
14898 argc, (const char **) argv,
14899 p + 1);
14900 else if (*p == '\0')
14901 {
14902 /* Complain, but do define it. */
14903 dwarf2_macro_malformed_definition_complaint (body);
14904 macro_define_function (file, line, name,
14905 argc, (const char **) argv,
14906 p);
14907 }
14908 else
14909 /* Just complain. */
14910 dwarf2_macro_malformed_definition_complaint (body);
14911 }
14912 else
14913 /* Just complain. */
14914 dwarf2_macro_malformed_definition_complaint (body);
14915
14916 xfree (name);
14917 {
14918 int i;
14919
14920 for (i = 0; i < argc; i++)
14921 xfree (argv[i]);
14922 }
14923 xfree (argv);
14924 }
14925 else
14926 dwarf2_macro_malformed_definition_complaint (body);
14927 }
14928
14929 /* Skip some bytes from BYTES according to the form given in FORM.
14930 Returns the new pointer. */
14931
14932 static gdb_byte *
14933 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14934 enum dwarf_form form,
14935 unsigned int offset_size,
14936 struct dwarf2_section_info *section)
14937 {
14938 unsigned int bytes_read;
14939
14940 switch (form)
14941 {
14942 case DW_FORM_data1:
14943 case DW_FORM_flag:
14944 ++bytes;
14945 break;
14946
14947 case DW_FORM_data2:
14948 bytes += 2;
14949 break;
14950
14951 case DW_FORM_data4:
14952 bytes += 4;
14953 break;
14954
14955 case DW_FORM_data8:
14956 bytes += 8;
14957 break;
14958
14959 case DW_FORM_string:
14960 read_direct_string (abfd, bytes, &bytes_read);
14961 bytes += bytes_read;
14962 break;
14963
14964 case DW_FORM_sec_offset:
14965 case DW_FORM_strp:
14966 bytes += offset_size;
14967 break;
14968
14969 case DW_FORM_block:
14970 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14971 bytes += bytes_read;
14972 break;
14973
14974 case DW_FORM_block1:
14975 bytes += 1 + read_1_byte (abfd, bytes);
14976 break;
14977 case DW_FORM_block2:
14978 bytes += 2 + read_2_bytes (abfd, bytes);
14979 break;
14980 case DW_FORM_block4:
14981 bytes += 4 + read_4_bytes (abfd, bytes);
14982 break;
14983
14984 case DW_FORM_sdata:
14985 case DW_FORM_udata:
14986 bytes = skip_leb128 (abfd, bytes);
14987 break;
14988
14989 default:
14990 {
14991 complain:
14992 complaint (&symfile_complaints,
14993 _("invalid form 0x%x in `%s'"),
14994 form,
14995 section->asection->name);
14996 return NULL;
14997 }
14998 }
14999
15000 return bytes;
15001 }
15002
15003 /* A helper for dwarf_decode_macros that handles skipping an unknown
15004 opcode. Returns an updated pointer to the macro data buffer; or,
15005 on error, issues a complaint and returns NULL. */
15006
15007 static gdb_byte *
15008 skip_unknown_opcode (unsigned int opcode,
15009 gdb_byte **opcode_definitions,
15010 gdb_byte *mac_ptr,
15011 bfd *abfd,
15012 unsigned int offset_size,
15013 struct dwarf2_section_info *section)
15014 {
15015 unsigned int bytes_read, i;
15016 unsigned long arg;
15017 gdb_byte *defn;
15018
15019 if (opcode_definitions[opcode] == NULL)
15020 {
15021 complaint (&symfile_complaints,
15022 _("unrecognized DW_MACFINO opcode 0x%x"),
15023 opcode);
15024 return NULL;
15025 }
15026
15027 defn = opcode_definitions[opcode];
15028 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15029 defn += bytes_read;
15030
15031 for (i = 0; i < arg; ++i)
15032 {
15033 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15034 if (mac_ptr == NULL)
15035 {
15036 /* skip_form_bytes already issued the complaint. */
15037 return NULL;
15038 }
15039 }
15040
15041 return mac_ptr;
15042 }
15043
15044 /* A helper function which parses the header of a macro section.
15045 If the macro section is the extended (for now called "GNU") type,
15046 then this updates *OFFSET_SIZE. Returns a pointer to just after
15047 the header, or issues a complaint and returns NULL on error. */
15048
15049 static gdb_byte *
15050 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15051 bfd *abfd,
15052 gdb_byte *mac_ptr,
15053 unsigned int *offset_size,
15054 int section_is_gnu)
15055 {
15056 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15057
15058 if (section_is_gnu)
15059 {
15060 unsigned int version, flags;
15061
15062 version = read_2_bytes (abfd, mac_ptr);
15063 if (version != 4)
15064 {
15065 complaint (&symfile_complaints,
15066 _("unrecognized version `%d' in .debug_macro section"),
15067 version);
15068 return NULL;
15069 }
15070 mac_ptr += 2;
15071
15072 flags = read_1_byte (abfd, mac_ptr);
15073 ++mac_ptr;
15074 *offset_size = (flags & 1) ? 8 : 4;
15075
15076 if ((flags & 2) != 0)
15077 /* We don't need the line table offset. */
15078 mac_ptr += *offset_size;
15079
15080 /* Vendor opcode descriptions. */
15081 if ((flags & 4) != 0)
15082 {
15083 unsigned int i, count;
15084
15085 count = read_1_byte (abfd, mac_ptr);
15086 ++mac_ptr;
15087 for (i = 0; i < count; ++i)
15088 {
15089 unsigned int opcode, bytes_read;
15090 unsigned long arg;
15091
15092 opcode = read_1_byte (abfd, mac_ptr);
15093 ++mac_ptr;
15094 opcode_definitions[opcode] = mac_ptr;
15095 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15096 mac_ptr += bytes_read;
15097 mac_ptr += arg;
15098 }
15099 }
15100 }
15101
15102 return mac_ptr;
15103 }
15104
15105 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15106 including DW_GNU_MACINFO_transparent_include. */
15107
15108 static void
15109 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15110 struct macro_source_file *current_file,
15111 struct line_header *lh, char *comp_dir,
15112 struct dwarf2_section_info *section,
15113 int section_is_gnu,
15114 unsigned int offset_size,
15115 struct objfile *objfile)
15116 {
15117 enum dwarf_macro_record_type macinfo_type;
15118 int at_commandline;
15119 gdb_byte *opcode_definitions[256];
15120
15121 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15122 &offset_size, section_is_gnu);
15123 if (mac_ptr == NULL)
15124 {
15125 /* We already issued a complaint. */
15126 return;
15127 }
15128
15129 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15130 GDB is still reading the definitions from command line. First
15131 DW_MACINFO_start_file will need to be ignored as it was already executed
15132 to create CURRENT_FILE for the main source holding also the command line
15133 definitions. On first met DW_MACINFO_start_file this flag is reset to
15134 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15135
15136 at_commandline = 1;
15137
15138 do
15139 {
15140 /* Do we at least have room for a macinfo type byte? */
15141 if (mac_ptr >= mac_end)
15142 {
15143 dwarf2_macros_too_long_complaint (section);
15144 break;
15145 }
15146
15147 macinfo_type = read_1_byte (abfd, mac_ptr);
15148 mac_ptr++;
15149
15150 /* Note that we rely on the fact that the corresponding GNU and
15151 DWARF constants are the same. */
15152 switch (macinfo_type)
15153 {
15154 /* A zero macinfo type indicates the end of the macro
15155 information. */
15156 case 0:
15157 break;
15158
15159 case DW_MACRO_GNU_define:
15160 case DW_MACRO_GNU_undef:
15161 case DW_MACRO_GNU_define_indirect:
15162 case DW_MACRO_GNU_undef_indirect:
15163 {
15164 unsigned int bytes_read;
15165 int line;
15166 char *body;
15167 int is_define;
15168
15169 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15170 mac_ptr += bytes_read;
15171
15172 if (macinfo_type == DW_MACRO_GNU_define
15173 || macinfo_type == DW_MACRO_GNU_undef)
15174 {
15175 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15176 mac_ptr += bytes_read;
15177 }
15178 else
15179 {
15180 LONGEST str_offset;
15181
15182 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15183 mac_ptr += offset_size;
15184
15185 body = read_indirect_string_at_offset (abfd, str_offset);
15186 }
15187
15188 is_define = (macinfo_type == DW_MACRO_GNU_define
15189 || macinfo_type == DW_MACRO_GNU_define_indirect);
15190 if (! current_file)
15191 {
15192 /* DWARF violation as no main source is present. */
15193 complaint (&symfile_complaints,
15194 _("debug info with no main source gives macro %s "
15195 "on line %d: %s"),
15196 is_define ? _("definition") : _("undefinition"),
15197 line, body);
15198 break;
15199 }
15200 if ((line == 0 && !at_commandline)
15201 || (line != 0 && at_commandline))
15202 complaint (&symfile_complaints,
15203 _("debug info gives %s macro %s with %s line %d: %s"),
15204 at_commandline ? _("command-line") : _("in-file"),
15205 is_define ? _("definition") : _("undefinition"),
15206 line == 0 ? _("zero") : _("non-zero"), line, body);
15207
15208 if (is_define)
15209 parse_macro_definition (current_file, line, body);
15210 else
15211 {
15212 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15213 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15214 macro_undef (current_file, line, body);
15215 }
15216 }
15217 break;
15218
15219 case DW_MACRO_GNU_start_file:
15220 {
15221 unsigned int bytes_read;
15222 int line, file;
15223
15224 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15225 mac_ptr += bytes_read;
15226 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15227 mac_ptr += bytes_read;
15228
15229 if ((line == 0 && !at_commandline)
15230 || (line != 0 && at_commandline))
15231 complaint (&symfile_complaints,
15232 _("debug info gives source %d included "
15233 "from %s at %s line %d"),
15234 file, at_commandline ? _("command-line") : _("file"),
15235 line == 0 ? _("zero") : _("non-zero"), line);
15236
15237 if (at_commandline)
15238 {
15239 /* This DW_MACRO_GNU_start_file was executed in the
15240 pass one. */
15241 at_commandline = 0;
15242 }
15243 else
15244 current_file = macro_start_file (file, line,
15245 current_file, comp_dir,
15246 lh, objfile);
15247 }
15248 break;
15249
15250 case DW_MACRO_GNU_end_file:
15251 if (! current_file)
15252 complaint (&symfile_complaints,
15253 _("macro debug info has an unmatched "
15254 "`close_file' directive"));
15255 else
15256 {
15257 current_file = current_file->included_by;
15258 if (! current_file)
15259 {
15260 enum dwarf_macro_record_type next_type;
15261
15262 /* GCC circa March 2002 doesn't produce the zero
15263 type byte marking the end of the compilation
15264 unit. Complain if it's not there, but exit no
15265 matter what. */
15266
15267 /* Do we at least have room for a macinfo type byte? */
15268 if (mac_ptr >= mac_end)
15269 {
15270 dwarf2_macros_too_long_complaint (section);
15271 return;
15272 }
15273
15274 /* We don't increment mac_ptr here, so this is just
15275 a look-ahead. */
15276 next_type = read_1_byte (abfd, mac_ptr);
15277 if (next_type != 0)
15278 complaint (&symfile_complaints,
15279 _("no terminating 0-type entry for "
15280 "macros in `.debug_macinfo' section"));
15281
15282 return;
15283 }
15284 }
15285 break;
15286
15287 case DW_MACRO_GNU_transparent_include:
15288 {
15289 LONGEST offset;
15290
15291 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15292 mac_ptr += offset_size;
15293
15294 dwarf_decode_macro_bytes (abfd,
15295 section->buffer + offset,
15296 mac_end, current_file,
15297 lh, comp_dir,
15298 section, section_is_gnu,
15299 offset_size, objfile);
15300 }
15301 break;
15302
15303 case DW_MACINFO_vendor_ext:
15304 if (!section_is_gnu)
15305 {
15306 unsigned int bytes_read;
15307 int constant;
15308
15309 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15310 mac_ptr += bytes_read;
15311 read_direct_string (abfd, mac_ptr, &bytes_read);
15312 mac_ptr += bytes_read;
15313
15314 /* We don't recognize any vendor extensions. */
15315 break;
15316 }
15317 /* FALLTHROUGH */
15318
15319 default:
15320 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15321 mac_ptr, abfd, offset_size,
15322 section);
15323 if (mac_ptr == NULL)
15324 return;
15325 break;
15326 }
15327 } while (macinfo_type != 0);
15328 }
15329
15330 static void
15331 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15332 char *comp_dir, bfd *abfd,
15333 struct dwarf2_cu *cu,
15334 struct dwarf2_section_info *section,
15335 int section_is_gnu)
15336 {
15337 struct objfile *objfile = dwarf2_per_objfile->objfile;
15338 gdb_byte *mac_ptr, *mac_end;
15339 struct macro_source_file *current_file = 0;
15340 enum dwarf_macro_record_type macinfo_type;
15341 unsigned int offset_size = cu->header.offset_size;
15342 gdb_byte *opcode_definitions[256];
15343
15344 dwarf2_read_section (objfile, section);
15345 if (section->buffer == NULL)
15346 {
15347 complaint (&symfile_complaints, _("missing %s section"),
15348 section->asection->name);
15349 return;
15350 }
15351
15352 /* First pass: Find the name of the base filename.
15353 This filename is needed in order to process all macros whose definition
15354 (or undefinition) comes from the command line. These macros are defined
15355 before the first DW_MACINFO_start_file entry, and yet still need to be
15356 associated to the base file.
15357
15358 To determine the base file name, we scan the macro definitions until we
15359 reach the first DW_MACINFO_start_file entry. We then initialize
15360 CURRENT_FILE accordingly so that any macro definition found before the
15361 first DW_MACINFO_start_file can still be associated to the base file. */
15362
15363 mac_ptr = section->buffer + offset;
15364 mac_end = section->buffer + section->size;
15365
15366 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15367 &offset_size, section_is_gnu);
15368 if (mac_ptr == NULL)
15369 {
15370 /* We already issued a complaint. */
15371 return;
15372 }
15373
15374 do
15375 {
15376 /* Do we at least have room for a macinfo type byte? */
15377 if (mac_ptr >= mac_end)
15378 {
15379 /* Complaint is printed during the second pass as GDB will probably
15380 stop the first pass earlier upon finding
15381 DW_MACINFO_start_file. */
15382 break;
15383 }
15384
15385 macinfo_type = read_1_byte (abfd, mac_ptr);
15386 mac_ptr++;
15387
15388 /* Note that we rely on the fact that the corresponding GNU and
15389 DWARF constants are the same. */
15390 switch (macinfo_type)
15391 {
15392 /* A zero macinfo type indicates the end of the macro
15393 information. */
15394 case 0:
15395 break;
15396
15397 case DW_MACRO_GNU_define:
15398 case DW_MACRO_GNU_undef:
15399 /* Only skip the data by MAC_PTR. */
15400 {
15401 unsigned int bytes_read;
15402
15403 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15404 mac_ptr += bytes_read;
15405 read_direct_string (abfd, mac_ptr, &bytes_read);
15406 mac_ptr += bytes_read;
15407 }
15408 break;
15409
15410 case DW_MACRO_GNU_start_file:
15411 {
15412 unsigned int bytes_read;
15413 int line, file;
15414
15415 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15416 mac_ptr += bytes_read;
15417 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15418 mac_ptr += bytes_read;
15419
15420 current_file = macro_start_file (file, line, current_file,
15421 comp_dir, lh, objfile);
15422 }
15423 break;
15424
15425 case DW_MACRO_GNU_end_file:
15426 /* No data to skip by MAC_PTR. */
15427 break;
15428
15429 case DW_MACRO_GNU_define_indirect:
15430 case DW_MACRO_GNU_undef_indirect:
15431 {
15432 unsigned int bytes_read;
15433
15434 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15435 mac_ptr += bytes_read;
15436 mac_ptr += offset_size;
15437 }
15438 break;
15439
15440 case DW_MACRO_GNU_transparent_include:
15441 /* Note that, according to the spec, a transparent include
15442 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15443 skip this opcode. */
15444 mac_ptr += offset_size;
15445 break;
15446
15447 case DW_MACINFO_vendor_ext:
15448 /* Only skip the data by MAC_PTR. */
15449 if (!section_is_gnu)
15450 {
15451 unsigned int bytes_read;
15452
15453 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15454 mac_ptr += bytes_read;
15455 read_direct_string (abfd, mac_ptr, &bytes_read);
15456 mac_ptr += bytes_read;
15457 }
15458 /* FALLTHROUGH */
15459
15460 default:
15461 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15462 mac_ptr, abfd, offset_size,
15463 section);
15464 if (mac_ptr == NULL)
15465 return;
15466 break;
15467 }
15468 } while (macinfo_type != 0 && current_file == NULL);
15469
15470 /* Second pass: Process all entries.
15471
15472 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15473 command-line macro definitions/undefinitions. This flag is unset when we
15474 reach the first DW_MACINFO_start_file entry. */
15475
15476 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15477 current_file, lh, comp_dir, section, section_is_gnu,
15478 offset_size, objfile);
15479 }
15480
15481 /* Check if the attribute's form is a DW_FORM_block*
15482 if so return true else false. */
15483 static int
15484 attr_form_is_block (struct attribute *attr)
15485 {
15486 return (attr == NULL ? 0 :
15487 attr->form == DW_FORM_block1
15488 || attr->form == DW_FORM_block2
15489 || attr->form == DW_FORM_block4
15490 || attr->form == DW_FORM_block
15491 || attr->form == DW_FORM_exprloc);
15492 }
15493
15494 /* Return non-zero if ATTR's value is a section offset --- classes
15495 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15496 You may use DW_UNSND (attr) to retrieve such offsets.
15497
15498 Section 7.5.4, "Attribute Encodings", explains that no attribute
15499 may have a value that belongs to more than one of these classes; it
15500 would be ambiguous if we did, because we use the same forms for all
15501 of them. */
15502 static int
15503 attr_form_is_section_offset (struct attribute *attr)
15504 {
15505 return (attr->form == DW_FORM_data4
15506 || attr->form == DW_FORM_data8
15507 || attr->form == DW_FORM_sec_offset);
15508 }
15509
15510
15511 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15512 zero otherwise. When this function returns true, you can apply
15513 dwarf2_get_attr_constant_value to it.
15514
15515 However, note that for some attributes you must check
15516 attr_form_is_section_offset before using this test. DW_FORM_data4
15517 and DW_FORM_data8 are members of both the constant class, and of
15518 the classes that contain offsets into other debug sections
15519 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15520 that, if an attribute's can be either a constant or one of the
15521 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15522 taken as section offsets, not constants. */
15523 static int
15524 attr_form_is_constant (struct attribute *attr)
15525 {
15526 switch (attr->form)
15527 {
15528 case DW_FORM_sdata:
15529 case DW_FORM_udata:
15530 case DW_FORM_data1:
15531 case DW_FORM_data2:
15532 case DW_FORM_data4:
15533 case DW_FORM_data8:
15534 return 1;
15535 default:
15536 return 0;
15537 }
15538 }
15539
15540 /* A helper function that fills in a dwarf2_loclist_baton. */
15541
15542 static void
15543 fill_in_loclist_baton (struct dwarf2_cu *cu,
15544 struct dwarf2_loclist_baton *baton,
15545 struct attribute *attr)
15546 {
15547 dwarf2_read_section (dwarf2_per_objfile->objfile,
15548 &dwarf2_per_objfile->loc);
15549
15550 baton->per_cu = cu->per_cu;
15551 gdb_assert (baton->per_cu);
15552 /* We don't know how long the location list is, but make sure we
15553 don't run off the edge of the section. */
15554 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15555 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15556 baton->base_address = cu->base_address;
15557 }
15558
15559 static void
15560 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15561 struct dwarf2_cu *cu)
15562 {
15563 struct objfile *objfile = dwarf2_per_objfile->objfile;
15564
15565 if (attr_form_is_section_offset (attr)
15566 /* ".debug_loc" may not exist at all, or the offset may be outside
15567 the section. If so, fall through to the complaint in the
15568 other branch. */
15569 && DW_UNSND (attr) < dwarf2_section_size (objfile,
15570 &dwarf2_per_objfile->loc))
15571 {
15572 struct dwarf2_loclist_baton *baton;
15573
15574 baton = obstack_alloc (&objfile->objfile_obstack,
15575 sizeof (struct dwarf2_loclist_baton));
15576
15577 fill_in_loclist_baton (cu, baton, attr);
15578
15579 if (cu->base_known == 0)
15580 complaint (&symfile_complaints,
15581 _("Location list used without "
15582 "specifying the CU base address."));
15583
15584 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15585 SYMBOL_LOCATION_BATON (sym) = baton;
15586 }
15587 else
15588 {
15589 struct dwarf2_locexpr_baton *baton;
15590
15591 baton = obstack_alloc (&objfile->objfile_obstack,
15592 sizeof (struct dwarf2_locexpr_baton));
15593 baton->per_cu = cu->per_cu;
15594 gdb_assert (baton->per_cu);
15595
15596 if (attr_form_is_block (attr))
15597 {
15598 /* Note that we're just copying the block's data pointer
15599 here, not the actual data. We're still pointing into the
15600 info_buffer for SYM's objfile; right now we never release
15601 that buffer, but when we do clean up properly this may
15602 need to change. */
15603 baton->size = DW_BLOCK (attr)->size;
15604 baton->data = DW_BLOCK (attr)->data;
15605 }
15606 else
15607 {
15608 dwarf2_invalid_attrib_class_complaint ("location description",
15609 SYMBOL_NATURAL_NAME (sym));
15610 baton->size = 0;
15611 }
15612
15613 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15614 SYMBOL_LOCATION_BATON (sym) = baton;
15615 }
15616 }
15617
15618 /* Return the OBJFILE associated with the compilation unit CU. If CU
15619 came from a separate debuginfo file, then the master objfile is
15620 returned. */
15621
15622 struct objfile *
15623 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15624 {
15625 struct objfile *objfile = per_cu->objfile;
15626
15627 /* Return the master objfile, so that we can report and look up the
15628 correct file containing this variable. */
15629 if (objfile->separate_debug_objfile_backlink)
15630 objfile = objfile->separate_debug_objfile_backlink;
15631
15632 return objfile;
15633 }
15634
15635 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15636 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15637 CU_HEADERP first. */
15638
15639 static const struct comp_unit_head *
15640 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15641 struct dwarf2_per_cu_data *per_cu)
15642 {
15643 struct objfile *objfile;
15644 struct dwarf2_per_objfile *per_objfile;
15645 gdb_byte *info_ptr;
15646
15647 if (per_cu->cu)
15648 return &per_cu->cu->header;
15649
15650 objfile = per_cu->objfile;
15651 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15652 info_ptr = per_objfile->info.buffer + per_cu->offset;
15653
15654 memset (cu_headerp, 0, sizeof (*cu_headerp));
15655 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15656
15657 return cu_headerp;
15658 }
15659
15660 /* Return the address size given in the compilation unit header for CU. */
15661
15662 int
15663 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15664 {
15665 struct comp_unit_head cu_header_local;
15666 const struct comp_unit_head *cu_headerp;
15667
15668 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15669
15670 return cu_headerp->addr_size;
15671 }
15672
15673 /* Return the offset size given in the compilation unit header for CU. */
15674
15675 int
15676 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15677 {
15678 struct comp_unit_head cu_header_local;
15679 const struct comp_unit_head *cu_headerp;
15680
15681 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15682
15683 return cu_headerp->offset_size;
15684 }
15685
15686 /* See its dwarf2loc.h declaration. */
15687
15688 int
15689 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15690 {
15691 struct comp_unit_head cu_header_local;
15692 const struct comp_unit_head *cu_headerp;
15693
15694 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15695
15696 if (cu_headerp->version == 2)
15697 return cu_headerp->addr_size;
15698 else
15699 return cu_headerp->offset_size;
15700 }
15701
15702 /* Return the text offset of the CU. The returned offset comes from
15703 this CU's objfile. If this objfile came from a separate debuginfo
15704 file, then the offset may be different from the corresponding
15705 offset in the parent objfile. */
15706
15707 CORE_ADDR
15708 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15709 {
15710 struct objfile *objfile = per_cu->objfile;
15711
15712 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15713 }
15714
15715 /* Locate the .debug_info compilation unit from CU's objfile which contains
15716 the DIE at OFFSET. Raises an error on failure. */
15717
15718 static struct dwarf2_per_cu_data *
15719 dwarf2_find_containing_comp_unit (unsigned int offset,
15720 struct objfile *objfile)
15721 {
15722 struct dwarf2_per_cu_data *this_cu;
15723 int low, high;
15724
15725 low = 0;
15726 high = dwarf2_per_objfile->n_comp_units - 1;
15727 while (high > low)
15728 {
15729 int mid = low + (high - low) / 2;
15730
15731 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15732 high = mid;
15733 else
15734 low = mid + 1;
15735 }
15736 gdb_assert (low == high);
15737 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15738 {
15739 if (low == 0)
15740 error (_("Dwarf Error: could not find partial DIE containing "
15741 "offset 0x%lx [in module %s]"),
15742 (long) offset, bfd_get_filename (objfile->obfd));
15743
15744 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15745 return dwarf2_per_objfile->all_comp_units[low-1];
15746 }
15747 else
15748 {
15749 this_cu = dwarf2_per_objfile->all_comp_units[low];
15750 if (low == dwarf2_per_objfile->n_comp_units - 1
15751 && offset >= this_cu->offset + this_cu->length)
15752 error (_("invalid dwarf2 offset %u"), offset);
15753 gdb_assert (offset < this_cu->offset + this_cu->length);
15754 return this_cu;
15755 }
15756 }
15757
15758 /* Initialize dwarf2_cu CU, owned by PER_CU. */
15759
15760 static void
15761 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15762 {
15763 memset (cu, 0, sizeof (*cu));
15764 per_cu->cu = cu;
15765 cu->per_cu = per_cu;
15766 cu->objfile = per_cu->objfile;
15767 obstack_init (&cu->comp_unit_obstack);
15768 }
15769
15770 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15771
15772 static void
15773 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15774 {
15775 struct attribute *attr;
15776
15777 /* Set the language we're debugging. */
15778 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15779 if (attr)
15780 set_cu_language (DW_UNSND (attr), cu);
15781 else
15782 {
15783 cu->language = language_minimal;
15784 cu->language_defn = language_def (cu->language);
15785 }
15786 }
15787
15788 /* Release one cached compilation unit, CU. We unlink it from the tree
15789 of compilation units, but we don't remove it from the read_in_chain;
15790 the caller is responsible for that.
15791 NOTE: DATA is a void * because this function is also used as a
15792 cleanup routine. */
15793
15794 static void
15795 free_heap_comp_unit (void *data)
15796 {
15797 struct dwarf2_cu *cu = data;
15798
15799 gdb_assert (cu->per_cu != NULL);
15800 cu->per_cu->cu = NULL;
15801 cu->per_cu = NULL;
15802
15803 obstack_free (&cu->comp_unit_obstack, NULL);
15804
15805 xfree (cu);
15806 }
15807
15808 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15809 when we're finished with it. We can't free the pointer itself, but be
15810 sure to unlink it from the cache. Also release any associated storage
15811 and perform cache maintenance.
15812
15813 Only used during partial symbol parsing. */
15814
15815 static void
15816 free_stack_comp_unit (void *data)
15817 {
15818 struct dwarf2_cu *cu = data;
15819
15820 gdb_assert (cu->per_cu != NULL);
15821 cu->per_cu->cu = NULL;
15822 cu->per_cu = NULL;
15823
15824 obstack_free (&cu->comp_unit_obstack, NULL);
15825 cu->partial_dies = NULL;
15826
15827 /* The previous code only did this if per_cu != NULL.
15828 But that would always succeed, so now we just unconditionally do
15829 the aging. This seems like the wrong place to do such aging,
15830 but cleaning that up is left for later. */
15831 age_cached_comp_units ();
15832 }
15833
15834 /* Free all cached compilation units. */
15835
15836 static void
15837 free_cached_comp_units (void *data)
15838 {
15839 struct dwarf2_per_cu_data *per_cu, **last_chain;
15840
15841 per_cu = dwarf2_per_objfile->read_in_chain;
15842 last_chain = &dwarf2_per_objfile->read_in_chain;
15843 while (per_cu != NULL)
15844 {
15845 struct dwarf2_per_cu_data *next_cu;
15846
15847 next_cu = per_cu->cu->read_in_chain;
15848
15849 free_heap_comp_unit (per_cu->cu);
15850 *last_chain = next_cu;
15851
15852 per_cu = next_cu;
15853 }
15854 }
15855
15856 /* Increase the age counter on each cached compilation unit, and free
15857 any that are too old. */
15858
15859 static void
15860 age_cached_comp_units (void)
15861 {
15862 struct dwarf2_per_cu_data *per_cu, **last_chain;
15863
15864 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15865 per_cu = dwarf2_per_objfile->read_in_chain;
15866 while (per_cu != NULL)
15867 {
15868 per_cu->cu->last_used ++;
15869 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15870 dwarf2_mark (per_cu->cu);
15871 per_cu = per_cu->cu->read_in_chain;
15872 }
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 if (!per_cu->cu->mark)
15883 {
15884 free_heap_comp_unit (per_cu->cu);
15885 *last_chain = next_cu;
15886 }
15887 else
15888 last_chain = &per_cu->cu->read_in_chain;
15889
15890 per_cu = next_cu;
15891 }
15892 }
15893
15894 /* Remove a single compilation unit from the cache. */
15895
15896 static void
15897 free_one_cached_comp_unit (void *target_cu)
15898 {
15899 struct dwarf2_per_cu_data *per_cu, **last_chain;
15900
15901 per_cu = dwarf2_per_objfile->read_in_chain;
15902 last_chain = &dwarf2_per_objfile->read_in_chain;
15903 while (per_cu != NULL)
15904 {
15905 struct dwarf2_per_cu_data *next_cu;
15906
15907 next_cu = per_cu->cu->read_in_chain;
15908
15909 if (per_cu->cu == target_cu)
15910 {
15911 free_heap_comp_unit (per_cu->cu);
15912 *last_chain = next_cu;
15913 break;
15914 }
15915 else
15916 last_chain = &per_cu->cu->read_in_chain;
15917
15918 per_cu = next_cu;
15919 }
15920 }
15921
15922 /* Release all extra memory associated with OBJFILE. */
15923
15924 void
15925 dwarf2_free_objfile (struct objfile *objfile)
15926 {
15927 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15928
15929 if (dwarf2_per_objfile == NULL)
15930 return;
15931
15932 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15933 free_cached_comp_units (NULL);
15934
15935 if (dwarf2_per_objfile->quick_file_names_table)
15936 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15937
15938 /* Everything else should be on the objfile obstack. */
15939 }
15940
15941 /* A pair of DIE offset and GDB type pointer. We store these
15942 in a hash table separate from the DIEs, and preserve them
15943 when the DIEs are flushed out of cache. */
15944
15945 struct dwarf2_offset_and_type
15946 {
15947 unsigned int offset;
15948 struct type *type;
15949 };
15950
15951 /* Hash function for a dwarf2_offset_and_type. */
15952
15953 static hashval_t
15954 offset_and_type_hash (const void *item)
15955 {
15956 const struct dwarf2_offset_and_type *ofs = item;
15957
15958 return ofs->offset;
15959 }
15960
15961 /* Equality function for a dwarf2_offset_and_type. */
15962
15963 static int
15964 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15965 {
15966 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15967 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15968
15969 return ofs_lhs->offset == ofs_rhs->offset;
15970 }
15971
15972 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15973 table if necessary. For convenience, return TYPE.
15974
15975 The DIEs reading must have careful ordering to:
15976 * Not cause infite loops trying to read in DIEs as a prerequisite for
15977 reading current DIE.
15978 * Not trying to dereference contents of still incompletely read in types
15979 while reading in other DIEs.
15980 * Enable referencing still incompletely read in types just by a pointer to
15981 the type without accessing its fields.
15982
15983 Therefore caller should follow these rules:
15984 * Try to fetch any prerequisite types we may need to build this DIE type
15985 before building the type and calling set_die_type.
15986 * After building type call set_die_type for current DIE as soon as
15987 possible before fetching more types to complete the current type.
15988 * Make the type as complete as possible before fetching more types. */
15989
15990 static struct type *
15991 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15992 {
15993 struct dwarf2_offset_and_type **slot, ofs;
15994 struct objfile *objfile = cu->objfile;
15995 htab_t *type_hash_ptr;
15996
15997 /* For Ada types, make sure that the gnat-specific data is always
15998 initialized (if not already set). There are a few types where
15999 we should not be doing so, because the type-specific area is
16000 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16001 where the type-specific area is used to store the floatformat).
16002 But this is not a problem, because the gnat-specific information
16003 is actually not needed for these types. */
16004 if (need_gnat_info (cu)
16005 && TYPE_CODE (type) != TYPE_CODE_FUNC
16006 && TYPE_CODE (type) != TYPE_CODE_FLT
16007 && !HAVE_GNAT_AUX_INFO (type))
16008 INIT_GNAT_SPECIFIC (type);
16009
16010 if (cu->per_cu->debug_types_section)
16011 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16012 else
16013 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16014
16015 if (*type_hash_ptr == NULL)
16016 {
16017 *type_hash_ptr
16018 = htab_create_alloc_ex (127,
16019 offset_and_type_hash,
16020 offset_and_type_eq,
16021 NULL,
16022 &objfile->objfile_obstack,
16023 hashtab_obstack_allocate,
16024 dummy_obstack_deallocate);
16025 }
16026
16027 ofs.offset = die->offset;
16028 ofs.type = type;
16029 slot = (struct dwarf2_offset_and_type **)
16030 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16031 if (*slot)
16032 complaint (&symfile_complaints,
16033 _("A problem internal to GDB: DIE 0x%x has type already set"),
16034 die->offset);
16035 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16036 **slot = ofs;
16037 return type;
16038 }
16039
16040 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16041 table, or return NULL if the die does not have a saved type. */
16042
16043 static struct type *
16044 get_die_type_at_offset (unsigned int offset,
16045 struct dwarf2_per_cu_data *per_cu)
16046 {
16047 struct dwarf2_offset_and_type *slot, ofs;
16048 htab_t type_hash;
16049
16050 if (per_cu->debug_types_section)
16051 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16052 else
16053 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16054 if (type_hash == NULL)
16055 return NULL;
16056
16057 ofs.offset = offset;
16058 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16059 if (slot)
16060 return slot->type;
16061 else
16062 return NULL;
16063 }
16064
16065 /* Look up the type for DIE in the appropriate type_hash table,
16066 or return NULL if DIE does not have a saved type. */
16067
16068 static struct type *
16069 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16070 {
16071 return get_die_type_at_offset (die->offset, cu->per_cu);
16072 }
16073
16074 /* Add a dependence relationship from CU to REF_PER_CU. */
16075
16076 static void
16077 dwarf2_add_dependence (struct dwarf2_cu *cu,
16078 struct dwarf2_per_cu_data *ref_per_cu)
16079 {
16080 void **slot;
16081
16082 if (cu->dependencies == NULL)
16083 cu->dependencies
16084 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16085 NULL, &cu->comp_unit_obstack,
16086 hashtab_obstack_allocate,
16087 dummy_obstack_deallocate);
16088
16089 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16090 if (*slot == NULL)
16091 *slot = ref_per_cu;
16092 }
16093
16094 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16095 Set the mark field in every compilation unit in the
16096 cache that we must keep because we are keeping CU. */
16097
16098 static int
16099 dwarf2_mark_helper (void **slot, void *data)
16100 {
16101 struct dwarf2_per_cu_data *per_cu;
16102
16103 per_cu = (struct dwarf2_per_cu_data *) *slot;
16104
16105 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16106 reading of the chain. As such dependencies remain valid it is not much
16107 useful to track and undo them during QUIT cleanups. */
16108 if (per_cu->cu == NULL)
16109 return 1;
16110
16111 if (per_cu->cu->mark)
16112 return 1;
16113 per_cu->cu->mark = 1;
16114
16115 if (per_cu->cu->dependencies != NULL)
16116 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16117
16118 return 1;
16119 }
16120
16121 /* Set the mark field in CU and in every other compilation unit in the
16122 cache that we must keep because we are keeping CU. */
16123
16124 static void
16125 dwarf2_mark (struct dwarf2_cu *cu)
16126 {
16127 if (cu->mark)
16128 return;
16129 cu->mark = 1;
16130 if (cu->dependencies != NULL)
16131 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16132 }
16133
16134 static void
16135 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16136 {
16137 while (per_cu)
16138 {
16139 per_cu->cu->mark = 0;
16140 per_cu = per_cu->cu->read_in_chain;
16141 }
16142 }
16143
16144 /* Trivial hash function for partial_die_info: the hash value of a DIE
16145 is its offset in .debug_info for this objfile. */
16146
16147 static hashval_t
16148 partial_die_hash (const void *item)
16149 {
16150 const struct partial_die_info *part_die = item;
16151
16152 return part_die->offset;
16153 }
16154
16155 /* Trivial comparison function for partial_die_info structures: two DIEs
16156 are equal if they have the same offset. */
16157
16158 static int
16159 partial_die_eq (const void *item_lhs, const void *item_rhs)
16160 {
16161 const struct partial_die_info *part_die_lhs = item_lhs;
16162 const struct partial_die_info *part_die_rhs = item_rhs;
16163
16164 return part_die_lhs->offset == part_die_rhs->offset;
16165 }
16166
16167 static struct cmd_list_element *set_dwarf2_cmdlist;
16168 static struct cmd_list_element *show_dwarf2_cmdlist;
16169
16170 static void
16171 set_dwarf2_cmd (char *args, int from_tty)
16172 {
16173 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16174 }
16175
16176 static void
16177 show_dwarf2_cmd (char *args, int from_tty)
16178 {
16179 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16180 }
16181
16182 /* If section described by INFO was mmapped, munmap it now. */
16183
16184 static void
16185 munmap_section_buffer (struct dwarf2_section_info *info)
16186 {
16187 if (info->map_addr != NULL)
16188 {
16189 #ifdef HAVE_MMAP
16190 int res;
16191
16192 res = munmap (info->map_addr, info->map_len);
16193 gdb_assert (res == 0);
16194 #else
16195 /* Without HAVE_MMAP, we should never be here to begin with. */
16196 gdb_assert_not_reached ("no mmap support");
16197 #endif
16198 }
16199 }
16200
16201 /* munmap debug sections for OBJFILE, if necessary. */
16202
16203 static void
16204 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16205 {
16206 struct dwarf2_per_objfile *data = d;
16207 int ix;
16208 struct dwarf2_section_info *section;
16209
16210 /* This is sorted according to the order they're defined in to make it easier
16211 to keep in sync. */
16212 munmap_section_buffer (&data->info);
16213 munmap_section_buffer (&data->abbrev);
16214 munmap_section_buffer (&data->line);
16215 munmap_section_buffer (&data->loc);
16216 munmap_section_buffer (&data->macinfo);
16217 munmap_section_buffer (&data->macro);
16218 munmap_section_buffer (&data->str);
16219 munmap_section_buffer (&data->ranges);
16220 munmap_section_buffer (&data->frame);
16221 munmap_section_buffer (&data->eh_frame);
16222 munmap_section_buffer (&data->gdb_index);
16223
16224 for (ix = 0;
16225 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16226 ++ix)
16227 munmap_section_buffer (section);
16228
16229 VEC_free (dwarf2_section_info_def, data->types);
16230 }
16231
16232 \f
16233 /* The "save gdb-index" command. */
16234
16235 /* The contents of the hash table we create when building the string
16236 table. */
16237 struct strtab_entry
16238 {
16239 offset_type offset;
16240 const char *str;
16241 };
16242
16243 /* Hash function for a strtab_entry.
16244
16245 Function is used only during write_hash_table so no index format backward
16246 compatibility is needed. */
16247
16248 static hashval_t
16249 hash_strtab_entry (const void *e)
16250 {
16251 const struct strtab_entry *entry = e;
16252 return mapped_index_string_hash (INT_MAX, entry->str);
16253 }
16254
16255 /* Equality function for a strtab_entry. */
16256
16257 static int
16258 eq_strtab_entry (const void *a, const void *b)
16259 {
16260 const struct strtab_entry *ea = a;
16261 const struct strtab_entry *eb = b;
16262 return !strcmp (ea->str, eb->str);
16263 }
16264
16265 /* Create a strtab_entry hash table. */
16266
16267 static htab_t
16268 create_strtab (void)
16269 {
16270 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16271 xfree, xcalloc, xfree);
16272 }
16273
16274 /* Add a string to the constant pool. Return the string's offset in
16275 host order. */
16276
16277 static offset_type
16278 add_string (htab_t table, struct obstack *cpool, const char *str)
16279 {
16280 void **slot;
16281 struct strtab_entry entry;
16282 struct strtab_entry *result;
16283
16284 entry.str = str;
16285 slot = htab_find_slot (table, &entry, INSERT);
16286 if (*slot)
16287 result = *slot;
16288 else
16289 {
16290 result = XNEW (struct strtab_entry);
16291 result->offset = obstack_object_size (cpool);
16292 result->str = str;
16293 obstack_grow_str0 (cpool, str);
16294 *slot = result;
16295 }
16296 return result->offset;
16297 }
16298
16299 /* An entry in the symbol table. */
16300 struct symtab_index_entry
16301 {
16302 /* The name of the symbol. */
16303 const char *name;
16304 /* The offset of the name in the constant pool. */
16305 offset_type index_offset;
16306 /* A sorted vector of the indices of all the CUs that hold an object
16307 of this name. */
16308 VEC (offset_type) *cu_indices;
16309 };
16310
16311 /* The symbol table. This is a power-of-2-sized hash table. */
16312 struct mapped_symtab
16313 {
16314 offset_type n_elements;
16315 offset_type size;
16316 struct symtab_index_entry **data;
16317 };
16318
16319 /* Hash function for a symtab_index_entry. */
16320
16321 static hashval_t
16322 hash_symtab_entry (const void *e)
16323 {
16324 const struct symtab_index_entry *entry = e;
16325 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16326 sizeof (offset_type) * VEC_length (offset_type,
16327 entry->cu_indices),
16328 0);
16329 }
16330
16331 /* Equality function for a symtab_index_entry. */
16332
16333 static int
16334 eq_symtab_entry (const void *a, const void *b)
16335 {
16336 const struct symtab_index_entry *ea = a;
16337 const struct symtab_index_entry *eb = b;
16338 int len = VEC_length (offset_type, ea->cu_indices);
16339 if (len != VEC_length (offset_type, eb->cu_indices))
16340 return 0;
16341 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16342 VEC_address (offset_type, eb->cu_indices),
16343 sizeof (offset_type) * len);
16344 }
16345
16346 /* Destroy a symtab_index_entry. */
16347
16348 static void
16349 delete_symtab_entry (void *p)
16350 {
16351 struct symtab_index_entry *entry = p;
16352 VEC_free (offset_type, entry->cu_indices);
16353 xfree (entry);
16354 }
16355
16356 /* Create a hash table holding symtab_index_entry objects. */
16357
16358 static htab_t
16359 create_symbol_hash_table (void)
16360 {
16361 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16362 delete_symtab_entry, xcalloc, xfree);
16363 }
16364
16365 /* Create a new mapped symtab object. */
16366
16367 static struct mapped_symtab *
16368 create_mapped_symtab (void)
16369 {
16370 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16371 symtab->n_elements = 0;
16372 symtab->size = 1024;
16373 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16374 return symtab;
16375 }
16376
16377 /* Destroy a mapped_symtab. */
16378
16379 static void
16380 cleanup_mapped_symtab (void *p)
16381 {
16382 struct mapped_symtab *symtab = p;
16383 /* The contents of the array are freed when the other hash table is
16384 destroyed. */
16385 xfree (symtab->data);
16386 xfree (symtab);
16387 }
16388
16389 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16390 the slot.
16391
16392 Function is used only during write_hash_table so no index format backward
16393 compatibility is needed. */
16394
16395 static struct symtab_index_entry **
16396 find_slot (struct mapped_symtab *symtab, const char *name)
16397 {
16398 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16399
16400 index = hash & (symtab->size - 1);
16401 step = ((hash * 17) & (symtab->size - 1)) | 1;
16402
16403 for (;;)
16404 {
16405 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16406 return &symtab->data[index];
16407 index = (index + step) & (symtab->size - 1);
16408 }
16409 }
16410
16411 /* Expand SYMTAB's hash table. */
16412
16413 static void
16414 hash_expand (struct mapped_symtab *symtab)
16415 {
16416 offset_type old_size = symtab->size;
16417 offset_type i;
16418 struct symtab_index_entry **old_entries = symtab->data;
16419
16420 symtab->size *= 2;
16421 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16422
16423 for (i = 0; i < old_size; ++i)
16424 {
16425 if (old_entries[i])
16426 {
16427 struct symtab_index_entry **slot = find_slot (symtab,
16428 old_entries[i]->name);
16429 *slot = old_entries[i];
16430 }
16431 }
16432
16433 xfree (old_entries);
16434 }
16435
16436 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16437 is the index of the CU in which the symbol appears. */
16438
16439 static void
16440 add_index_entry (struct mapped_symtab *symtab, const char *name,
16441 offset_type cu_index)
16442 {
16443 struct symtab_index_entry **slot;
16444
16445 ++symtab->n_elements;
16446 if (4 * symtab->n_elements / 3 >= symtab->size)
16447 hash_expand (symtab);
16448
16449 slot = find_slot (symtab, name);
16450 if (!*slot)
16451 {
16452 *slot = XNEW (struct symtab_index_entry);
16453 (*slot)->name = name;
16454 (*slot)->cu_indices = NULL;
16455 }
16456 /* Don't push an index twice. Due to how we add entries we only
16457 have to check the last one. */
16458 if (VEC_empty (offset_type, (*slot)->cu_indices)
16459 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16460 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16461 }
16462
16463 /* Add a vector of indices to the constant pool. */
16464
16465 static offset_type
16466 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16467 struct symtab_index_entry *entry)
16468 {
16469 void **slot;
16470
16471 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16472 if (!*slot)
16473 {
16474 offset_type len = VEC_length (offset_type, entry->cu_indices);
16475 offset_type val = MAYBE_SWAP (len);
16476 offset_type iter;
16477 int i;
16478
16479 *slot = entry;
16480 entry->index_offset = obstack_object_size (cpool);
16481
16482 obstack_grow (cpool, &val, sizeof (val));
16483 for (i = 0;
16484 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16485 ++i)
16486 {
16487 val = MAYBE_SWAP (iter);
16488 obstack_grow (cpool, &val, sizeof (val));
16489 }
16490 }
16491 else
16492 {
16493 struct symtab_index_entry *old_entry = *slot;
16494 entry->index_offset = old_entry->index_offset;
16495 entry = old_entry;
16496 }
16497 return entry->index_offset;
16498 }
16499
16500 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16501 constant pool entries going into the obstack CPOOL. */
16502
16503 static void
16504 write_hash_table (struct mapped_symtab *symtab,
16505 struct obstack *output, struct obstack *cpool)
16506 {
16507 offset_type i;
16508 htab_t symbol_hash_table;
16509 htab_t str_table;
16510
16511 symbol_hash_table = create_symbol_hash_table ();
16512 str_table = create_strtab ();
16513
16514 /* We add all the index vectors to the constant pool first, to
16515 ensure alignment is ok. */
16516 for (i = 0; i < symtab->size; ++i)
16517 {
16518 if (symtab->data[i])
16519 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16520 }
16521
16522 /* Now write out the hash table. */
16523 for (i = 0; i < symtab->size; ++i)
16524 {
16525 offset_type str_off, vec_off;
16526
16527 if (symtab->data[i])
16528 {
16529 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16530 vec_off = symtab->data[i]->index_offset;
16531 }
16532 else
16533 {
16534 /* While 0 is a valid constant pool index, it is not valid
16535 to have 0 for both offsets. */
16536 str_off = 0;
16537 vec_off = 0;
16538 }
16539
16540 str_off = MAYBE_SWAP (str_off);
16541 vec_off = MAYBE_SWAP (vec_off);
16542
16543 obstack_grow (output, &str_off, sizeof (str_off));
16544 obstack_grow (output, &vec_off, sizeof (vec_off));
16545 }
16546
16547 htab_delete (str_table);
16548 htab_delete (symbol_hash_table);
16549 }
16550
16551 /* Struct to map psymtab to CU index in the index file. */
16552 struct psymtab_cu_index_map
16553 {
16554 struct partial_symtab *psymtab;
16555 unsigned int cu_index;
16556 };
16557
16558 static hashval_t
16559 hash_psymtab_cu_index (const void *item)
16560 {
16561 const struct psymtab_cu_index_map *map = item;
16562
16563 return htab_hash_pointer (map->psymtab);
16564 }
16565
16566 static int
16567 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16568 {
16569 const struct psymtab_cu_index_map *lhs = item_lhs;
16570 const struct psymtab_cu_index_map *rhs = item_rhs;
16571
16572 return lhs->psymtab == rhs->psymtab;
16573 }
16574
16575 /* Helper struct for building the address table. */
16576 struct addrmap_index_data
16577 {
16578 struct objfile *objfile;
16579 struct obstack *addr_obstack;
16580 htab_t cu_index_htab;
16581
16582 /* Non-zero if the previous_* fields are valid.
16583 We can't write an entry until we see the next entry (since it is only then
16584 that we know the end of the entry). */
16585 int previous_valid;
16586 /* Index of the CU in the table of all CUs in the index file. */
16587 unsigned int previous_cu_index;
16588 /* Start address of the CU. */
16589 CORE_ADDR previous_cu_start;
16590 };
16591
16592 /* Write an address entry to OBSTACK. */
16593
16594 static void
16595 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16596 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16597 {
16598 offset_type cu_index_to_write;
16599 char addr[8];
16600 CORE_ADDR baseaddr;
16601
16602 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16603
16604 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16605 obstack_grow (obstack, addr, 8);
16606 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16607 obstack_grow (obstack, addr, 8);
16608 cu_index_to_write = MAYBE_SWAP (cu_index);
16609 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16610 }
16611
16612 /* Worker function for traversing an addrmap to build the address table. */
16613
16614 static int
16615 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16616 {
16617 struct addrmap_index_data *data = datap;
16618 struct partial_symtab *pst = obj;
16619 offset_type cu_index;
16620 void **slot;
16621
16622 if (data->previous_valid)
16623 add_address_entry (data->objfile, data->addr_obstack,
16624 data->previous_cu_start, start_addr,
16625 data->previous_cu_index);
16626
16627 data->previous_cu_start = start_addr;
16628 if (pst != NULL)
16629 {
16630 struct psymtab_cu_index_map find_map, *map;
16631 find_map.psymtab = pst;
16632 map = htab_find (data->cu_index_htab, &find_map);
16633 gdb_assert (map != NULL);
16634 data->previous_cu_index = map->cu_index;
16635 data->previous_valid = 1;
16636 }
16637 else
16638 data->previous_valid = 0;
16639
16640 return 0;
16641 }
16642
16643 /* Write OBJFILE's address map to OBSTACK.
16644 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16645 in the index file. */
16646
16647 static void
16648 write_address_map (struct objfile *objfile, struct obstack *obstack,
16649 htab_t cu_index_htab)
16650 {
16651 struct addrmap_index_data addrmap_index_data;
16652
16653 /* When writing the address table, we have to cope with the fact that
16654 the addrmap iterator only provides the start of a region; we have to
16655 wait until the next invocation to get the start of the next region. */
16656
16657 addrmap_index_data.objfile = objfile;
16658 addrmap_index_data.addr_obstack = obstack;
16659 addrmap_index_data.cu_index_htab = cu_index_htab;
16660 addrmap_index_data.previous_valid = 0;
16661
16662 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16663 &addrmap_index_data);
16664
16665 /* It's highly unlikely the last entry (end address = 0xff...ff)
16666 is valid, but we should still handle it.
16667 The end address is recorded as the start of the next region, but that
16668 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16669 anyway. */
16670 if (addrmap_index_data.previous_valid)
16671 add_address_entry (objfile, obstack,
16672 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16673 addrmap_index_data.previous_cu_index);
16674 }
16675
16676 /* Add a list of partial symbols to SYMTAB. */
16677
16678 static void
16679 write_psymbols (struct mapped_symtab *symtab,
16680 htab_t psyms_seen,
16681 struct partial_symbol **psymp,
16682 int count,
16683 offset_type cu_index,
16684 int is_static)
16685 {
16686 for (; count-- > 0; ++psymp)
16687 {
16688 void **slot, *lookup;
16689
16690 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16691 error (_("Ada is not currently supported by the index"));
16692
16693 /* We only want to add a given psymbol once. However, we also
16694 want to account for whether it is global or static. So, we
16695 may add it twice, using slightly different values. */
16696 if (is_static)
16697 {
16698 uintptr_t val = 1 | (uintptr_t) *psymp;
16699
16700 lookup = (void *) val;
16701 }
16702 else
16703 lookup = *psymp;
16704
16705 /* Only add a given psymbol once. */
16706 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16707 if (!*slot)
16708 {
16709 *slot = lookup;
16710 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16711 }
16712 }
16713 }
16714
16715 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16716 exception if there is an error. */
16717
16718 static void
16719 write_obstack (FILE *file, struct obstack *obstack)
16720 {
16721 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16722 file)
16723 != obstack_object_size (obstack))
16724 error (_("couldn't data write to file"));
16725 }
16726
16727 /* Unlink a file if the argument is not NULL. */
16728
16729 static void
16730 unlink_if_set (void *p)
16731 {
16732 char **filename = p;
16733 if (*filename)
16734 unlink (*filename);
16735 }
16736
16737 /* A helper struct used when iterating over debug_types. */
16738 struct signatured_type_index_data
16739 {
16740 struct objfile *objfile;
16741 struct mapped_symtab *symtab;
16742 struct obstack *types_list;
16743 htab_t psyms_seen;
16744 int cu_index;
16745 };
16746
16747 /* A helper function that writes a single signatured_type to an
16748 obstack. */
16749
16750 static int
16751 write_one_signatured_type (void **slot, void *d)
16752 {
16753 struct signatured_type_index_data *info = d;
16754 struct signatured_type *entry = (struct signatured_type *) *slot;
16755 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16756 struct partial_symtab *psymtab = per_cu->v.psymtab;
16757 gdb_byte val[8];
16758
16759 write_psymbols (info->symtab,
16760 info->psyms_seen,
16761 info->objfile->global_psymbols.list
16762 + psymtab->globals_offset,
16763 psymtab->n_global_syms, info->cu_index,
16764 0);
16765 write_psymbols (info->symtab,
16766 info->psyms_seen,
16767 info->objfile->static_psymbols.list
16768 + psymtab->statics_offset,
16769 psymtab->n_static_syms, info->cu_index,
16770 1);
16771
16772 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16773 obstack_grow (info->types_list, val, 8);
16774 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16775 obstack_grow (info->types_list, val, 8);
16776 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16777 obstack_grow (info->types_list, val, 8);
16778
16779 ++info->cu_index;
16780
16781 return 1;
16782 }
16783
16784 /* Create an index file for OBJFILE in the directory DIR. */
16785
16786 static void
16787 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16788 {
16789 struct cleanup *cleanup;
16790 char *filename, *cleanup_filename;
16791 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16792 struct obstack cu_list, types_cu_list;
16793 int i;
16794 FILE *out_file;
16795 struct mapped_symtab *symtab;
16796 offset_type val, size_of_contents, total_len;
16797 struct stat st;
16798 char buf[8];
16799 htab_t psyms_seen;
16800 htab_t cu_index_htab;
16801 struct psymtab_cu_index_map *psymtab_cu_index_map;
16802
16803 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16804 return;
16805
16806 if (dwarf2_per_objfile->using_index)
16807 error (_("Cannot use an index to create the index"));
16808
16809 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16810 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16811
16812 if (stat (objfile->name, &st) < 0)
16813 perror_with_name (objfile->name);
16814
16815 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16816 INDEX_SUFFIX, (char *) NULL);
16817 cleanup = make_cleanup (xfree, filename);
16818
16819 out_file = fopen (filename, "wb");
16820 if (!out_file)
16821 error (_("Can't open `%s' for writing"), filename);
16822
16823 cleanup_filename = filename;
16824 make_cleanup (unlink_if_set, &cleanup_filename);
16825
16826 symtab = create_mapped_symtab ();
16827 make_cleanup (cleanup_mapped_symtab, symtab);
16828
16829 obstack_init (&addr_obstack);
16830 make_cleanup_obstack_free (&addr_obstack);
16831
16832 obstack_init (&cu_list);
16833 make_cleanup_obstack_free (&cu_list);
16834
16835 obstack_init (&types_cu_list);
16836 make_cleanup_obstack_free (&types_cu_list);
16837
16838 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16839 NULL, xcalloc, xfree);
16840 make_cleanup_htab_delete (psyms_seen);
16841
16842 /* While we're scanning CU's create a table that maps a psymtab pointer
16843 (which is what addrmap records) to its index (which is what is recorded
16844 in the index file). This will later be needed to write the address
16845 table. */
16846 cu_index_htab = htab_create_alloc (100,
16847 hash_psymtab_cu_index,
16848 eq_psymtab_cu_index,
16849 NULL, xcalloc, xfree);
16850 make_cleanup_htab_delete (cu_index_htab);
16851 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16852 xmalloc (sizeof (struct psymtab_cu_index_map)
16853 * dwarf2_per_objfile->n_comp_units);
16854 make_cleanup (xfree, psymtab_cu_index_map);
16855
16856 /* The CU list is already sorted, so we don't need to do additional
16857 work here. Also, the debug_types entries do not appear in
16858 all_comp_units, but only in their own hash table. */
16859 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16860 {
16861 struct dwarf2_per_cu_data *per_cu
16862 = dwarf2_per_objfile->all_comp_units[i];
16863 struct partial_symtab *psymtab = per_cu->v.psymtab;
16864 gdb_byte val[8];
16865 struct psymtab_cu_index_map *map;
16866 void **slot;
16867
16868 write_psymbols (symtab,
16869 psyms_seen,
16870 objfile->global_psymbols.list + psymtab->globals_offset,
16871 psymtab->n_global_syms, i,
16872 0);
16873 write_psymbols (symtab,
16874 psyms_seen,
16875 objfile->static_psymbols.list + psymtab->statics_offset,
16876 psymtab->n_static_syms, i,
16877 1);
16878
16879 map = &psymtab_cu_index_map[i];
16880 map->psymtab = psymtab;
16881 map->cu_index = i;
16882 slot = htab_find_slot (cu_index_htab, map, INSERT);
16883 gdb_assert (slot != NULL);
16884 gdb_assert (*slot == NULL);
16885 *slot = map;
16886
16887 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16888 obstack_grow (&cu_list, val, 8);
16889 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16890 obstack_grow (&cu_list, val, 8);
16891 }
16892
16893 /* Dump the address map. */
16894 write_address_map (objfile, &addr_obstack, cu_index_htab);
16895
16896 /* Write out the .debug_type entries, if any. */
16897 if (dwarf2_per_objfile->signatured_types)
16898 {
16899 struct signatured_type_index_data sig_data;
16900
16901 sig_data.objfile = objfile;
16902 sig_data.symtab = symtab;
16903 sig_data.types_list = &types_cu_list;
16904 sig_data.psyms_seen = psyms_seen;
16905 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16906 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16907 write_one_signatured_type, &sig_data);
16908 }
16909
16910 obstack_init (&constant_pool);
16911 make_cleanup_obstack_free (&constant_pool);
16912 obstack_init (&symtab_obstack);
16913 make_cleanup_obstack_free (&symtab_obstack);
16914 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16915
16916 obstack_init (&contents);
16917 make_cleanup_obstack_free (&contents);
16918 size_of_contents = 6 * sizeof (offset_type);
16919 total_len = size_of_contents;
16920
16921 /* The version number. */
16922 val = MAYBE_SWAP (5);
16923 obstack_grow (&contents, &val, sizeof (val));
16924
16925 /* The offset of the CU list from the start of the file. */
16926 val = MAYBE_SWAP (total_len);
16927 obstack_grow (&contents, &val, sizeof (val));
16928 total_len += obstack_object_size (&cu_list);
16929
16930 /* The offset of the types CU list from the start of the file. */
16931 val = MAYBE_SWAP (total_len);
16932 obstack_grow (&contents, &val, sizeof (val));
16933 total_len += obstack_object_size (&types_cu_list);
16934
16935 /* The offset of the address table from the start of the file. */
16936 val = MAYBE_SWAP (total_len);
16937 obstack_grow (&contents, &val, sizeof (val));
16938 total_len += obstack_object_size (&addr_obstack);
16939
16940 /* The offset of the symbol table from the start of the file. */
16941 val = MAYBE_SWAP (total_len);
16942 obstack_grow (&contents, &val, sizeof (val));
16943 total_len += obstack_object_size (&symtab_obstack);
16944
16945 /* The offset of the constant pool from the start of the file. */
16946 val = MAYBE_SWAP (total_len);
16947 obstack_grow (&contents, &val, sizeof (val));
16948 total_len += obstack_object_size (&constant_pool);
16949
16950 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16951
16952 write_obstack (out_file, &contents);
16953 write_obstack (out_file, &cu_list);
16954 write_obstack (out_file, &types_cu_list);
16955 write_obstack (out_file, &addr_obstack);
16956 write_obstack (out_file, &symtab_obstack);
16957 write_obstack (out_file, &constant_pool);
16958
16959 fclose (out_file);
16960
16961 /* We want to keep the file, so we set cleanup_filename to NULL
16962 here. See unlink_if_set. */
16963 cleanup_filename = NULL;
16964
16965 do_cleanups (cleanup);
16966 }
16967
16968 /* Implementation of the `save gdb-index' command.
16969
16970 Note that the file format used by this command is documented in the
16971 GDB manual. Any changes here must be documented there. */
16972
16973 static void
16974 save_gdb_index_command (char *arg, int from_tty)
16975 {
16976 struct objfile *objfile;
16977
16978 if (!arg || !*arg)
16979 error (_("usage: save gdb-index DIRECTORY"));
16980
16981 ALL_OBJFILES (objfile)
16982 {
16983 struct stat st;
16984
16985 /* If the objfile does not correspond to an actual file, skip it. */
16986 if (stat (objfile->name, &st) < 0)
16987 continue;
16988
16989 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16990 if (dwarf2_per_objfile)
16991 {
16992 volatile struct gdb_exception except;
16993
16994 TRY_CATCH (except, RETURN_MASK_ERROR)
16995 {
16996 write_psymtabs_to_index (objfile, arg);
16997 }
16998 if (except.reason < 0)
16999 exception_fprintf (gdb_stderr, except,
17000 _("Error while writing index for `%s': "),
17001 objfile->name);
17002 }
17003 }
17004 }
17005
17006 \f
17007
17008 int dwarf2_always_disassemble;
17009
17010 static void
17011 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17012 struct cmd_list_element *c, const char *value)
17013 {
17014 fprintf_filtered (file,
17015 _("Whether to always disassemble "
17016 "DWARF expressions is %s.\n"),
17017 value);
17018 }
17019
17020 static void
17021 show_check_physname (struct ui_file *file, int from_tty,
17022 struct cmd_list_element *c, const char *value)
17023 {
17024 fprintf_filtered (file,
17025 _("Whether to check \"physname\" is %s.\n"),
17026 value);
17027 }
17028
17029 void _initialize_dwarf2_read (void);
17030
17031 void
17032 _initialize_dwarf2_read (void)
17033 {
17034 struct cmd_list_element *c;
17035
17036 dwarf2_objfile_data_key
17037 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17038
17039 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17040 Set DWARF 2 specific variables.\n\
17041 Configure DWARF 2 variables such as the cache size"),
17042 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17043 0/*allow-unknown*/, &maintenance_set_cmdlist);
17044
17045 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17046 Show DWARF 2 specific variables\n\
17047 Show DWARF 2 variables such as the cache size"),
17048 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17049 0/*allow-unknown*/, &maintenance_show_cmdlist);
17050
17051 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17052 &dwarf2_max_cache_age, _("\
17053 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17054 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17055 A higher limit means that cached compilation units will be stored\n\
17056 in memory longer, and more total memory will be used. Zero disables\n\
17057 caching, which can slow down startup."),
17058 NULL,
17059 show_dwarf2_max_cache_age,
17060 &set_dwarf2_cmdlist,
17061 &show_dwarf2_cmdlist);
17062
17063 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17064 &dwarf2_always_disassemble, _("\
17065 Set whether `info address' always disassembles DWARF expressions."), _("\
17066 Show whether `info address' always disassembles DWARF expressions."), _("\
17067 When enabled, DWARF expressions are always printed in an assembly-like\n\
17068 syntax. When disabled, expressions will be printed in a more\n\
17069 conversational style, when possible."),
17070 NULL,
17071 show_dwarf2_always_disassemble,
17072 &set_dwarf2_cmdlist,
17073 &show_dwarf2_cmdlist);
17074
17075 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17076 Set debugging of the dwarf2 DIE reader."), _("\
17077 Show debugging of the dwarf2 DIE reader."), _("\
17078 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17079 The value is the maximum depth to print."),
17080 NULL,
17081 NULL,
17082 &setdebuglist, &showdebuglist);
17083
17084 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17085 Set cross-checking of \"physname\" code against demangler."), _("\
17086 Show cross-checking of \"physname\" code against demangler."), _("\
17087 When enabled, GDB's internal \"physname\" code is checked against\n\
17088 the demangler."),
17089 NULL, show_check_physname,
17090 &setdebuglist, &showdebuglist);
17091
17092 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17093 _("\
17094 Save a gdb-index file.\n\
17095 Usage: save gdb-index DIRECTORY"),
17096 &save_cmdlist);
17097 set_cmd_completer (c, filename_completer);
17098 }