gdb/ChangeLog
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 support.
13
14 This file is part of GDB.
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h" /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60 #include <ctype.h>
61
62 #include <fcntl.h>
63 #include "gdb_string.h"
64 #include "gdb_assert.h"
65 #include <sys/types.h>
66 #ifdef HAVE_ZLIB_H
67 #include <zlib.h>
68 #endif
69 #ifdef HAVE_MMAP
70 #include <sys/mman.h>
71 #ifndef MAP_FAILED
72 #define MAP_FAILED ((void *) -1)
73 #endif
74 #endif
75
76 typedef struct symbol *symbolp;
77 DEF_VEC_P (symbolp);
78
79 #if 0
80 /* .debug_info header for a compilation unit
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83 typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92 _COMP_UNIT_HEADER;
93 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
94 #endif
95
96 /* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99 typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117 _STATEMENT_PROLOGUE;
118
119 /* When non-zero, dump DIEs after they are read in. */
120 static int dwarf2_die_debug = 0;
121
122 /* When non-zero, cross-check physname against demangler. */
123 static int check_physname = 0;
124
125 static int pagesize;
126
127 /* When set, the file that we're processing is known to have debugging
128 info for C++ namespaces. GCC 3.3.x did not produce this information,
129 but later versions do. */
130
131 static int processing_has_namespace_info;
132
133 static const struct objfile_data *dwarf2_objfile_data_key;
134
135 struct dwarf2_section_info
136 {
137 asection *asection;
138 gdb_byte *buffer;
139 bfd_size_type size;
140 /* Not NULL if the section was actually mmapped. */
141 void *map_addr;
142 /* Page aligned size of mmapped area. */
143 bfd_size_type map_len;
144 /* True if we have tried to read this section. */
145 int readin;
146 };
147
148 typedef struct dwarf2_section_info dwarf2_section_info_def;
149 DEF_VEC_O (dwarf2_section_info_def);
150
151 /* All offsets in the index are of this type. It must be
152 architecture-independent. */
153 typedef uint32_t offset_type;
154
155 DEF_VEC_I (offset_type);
156
157 /* A description of the mapped index. The file format is described in
158 a comment by the code that writes the index. */
159 struct mapped_index
160 {
161 /* Index data format version. */
162 int version;
163
164 /* The total length of the buffer. */
165 off_t total_size;
166
167 /* A pointer to the address table data. */
168 const gdb_byte *address_table;
169
170 /* Size of the address table data in bytes. */
171 offset_type address_table_size;
172
173 /* The symbol table, implemented as a hash table. */
174 const offset_type *symbol_table;
175
176 /* Size in slots, each slot is 2 offset_types. */
177 offset_type symbol_table_slots;
178
179 /* A pointer to the constant pool. */
180 const char *constant_pool;
181 };
182
183 struct dwarf2_per_objfile
184 {
185 struct dwarf2_section_info info;
186 struct dwarf2_section_info abbrev;
187 struct dwarf2_section_info line;
188 struct dwarf2_section_info loc;
189 struct dwarf2_section_info macinfo;
190 struct dwarf2_section_info macro;
191 struct dwarf2_section_info str;
192 struct dwarf2_section_info ranges;
193 struct dwarf2_section_info frame;
194 struct dwarf2_section_info eh_frame;
195 struct dwarf2_section_info gdb_index;
196
197 VEC (dwarf2_section_info_def) *types;
198
199 /* Back link. */
200 struct objfile *objfile;
201
202 /* A list of all the compilation units. This is used to locate
203 the target compilation unit of a particular reference. */
204 struct dwarf2_per_cu_data **all_comp_units;
205
206 /* The number of compilation units in ALL_COMP_UNITS. */
207 int n_comp_units;
208
209 /* The number of .debug_types-related CUs. */
210 int n_type_comp_units;
211
212 /* The .debug_types-related CUs. */
213 struct dwarf2_per_cu_data **type_comp_units;
214
215 /* A chain of compilation units that are currently read in, so that
216 they can be freed later. */
217 struct dwarf2_per_cu_data *read_in_chain;
218
219 /* A table mapping .debug_types signatures to its signatured_type entry.
220 This is NULL if the .debug_types section hasn't been read in yet. */
221 htab_t signatured_types;
222
223 /* A flag indicating wether this objfile has a section loaded at a
224 VMA of 0. */
225 int has_section_at_zero;
226
227 /* True if we are using the mapped index,
228 or we are faking it for OBJF_READNOW's sake. */
229 unsigned char using_index;
230
231 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
232 struct mapped_index *index_table;
233
234 /* When using index_table, this keeps track of all quick_file_names entries.
235 TUs can share line table entries with CUs or other TUs, and there can be
236 a lot more TUs than unique line tables, so we maintain a separate table
237 of all line table entries to support the sharing. */
238 htab_t quick_file_names_table;
239
240 /* Set during partial symbol reading, to prevent queueing of full
241 symbols. */
242 int reading_partial_symbols;
243
244 /* Table mapping type .debug_info DIE offsets to types.
245 This is NULL if not allocated yet.
246 It (currently) makes sense to allocate debug_types_type_hash lazily.
247 To keep things simple we allocate both lazily. */
248 htab_t debug_info_type_hash;
249
250 /* Table mapping type .debug_types DIE offsets to types.
251 This is NULL if not allocated yet. */
252 htab_t debug_types_type_hash;
253 };
254
255 static struct dwarf2_per_objfile *dwarf2_per_objfile;
256
257 /* Default names of the debugging sections. */
258
259 /* Note that if the debugging section has been compressed, it might
260 have a name like .zdebug_info. */
261
262 static const struct dwarf2_debug_sections dwarf2_elf_names = {
263 { ".debug_info", ".zdebug_info" },
264 { ".debug_abbrev", ".zdebug_abbrev" },
265 { ".debug_line", ".zdebug_line" },
266 { ".debug_loc", ".zdebug_loc" },
267 { ".debug_macinfo", ".zdebug_macinfo" },
268 { ".debug_macro", ".zdebug_macro" },
269 { ".debug_str", ".zdebug_str" },
270 { ".debug_ranges", ".zdebug_ranges" },
271 { ".debug_types", ".zdebug_types" },
272 { ".debug_frame", ".zdebug_frame" },
273 { ".eh_frame", NULL },
274 { ".gdb_index", ".zgdb_index" },
275 23
276 };
277
278 /* local data types */
279
280 /* We hold several abbreviation tables in memory at the same time. */
281 #ifndef ABBREV_HASH_SIZE
282 #define ABBREV_HASH_SIZE 121
283 #endif
284
285 /* The data in a compilation unit header, after target2host
286 translation, looks like this. */
287 struct comp_unit_head
288 {
289 unsigned int length;
290 short version;
291 unsigned char addr_size;
292 unsigned char signed_addr_p;
293 unsigned int abbrev_offset;
294
295 /* Size of file offsets; either 4 or 8. */
296 unsigned int offset_size;
297
298 /* Size of the length field; either 4 or 12. */
299 unsigned int initial_length_size;
300
301 /* Offset to the first byte of this compilation unit header in the
302 .debug_info section, for resolving relative reference dies. */
303 unsigned int offset;
304
305 /* Offset to first die in this cu from the start of the cu.
306 This will be the first byte following the compilation unit header. */
307 unsigned int first_die_offset;
308 };
309
310 /* Type used for delaying computation of method physnames.
311 See comments for compute_delayed_physnames. */
312 struct delayed_method_info
313 {
314 /* The type to which the method is attached, i.e., its parent class. */
315 struct type *type;
316
317 /* The index of the method in the type's function fieldlists. */
318 int fnfield_index;
319
320 /* The index of the method in the fieldlist. */
321 int index;
322
323 /* The name of the DIE. */
324 const char *name;
325
326 /* The DIE associated with this method. */
327 struct die_info *die;
328 };
329
330 typedef struct delayed_method_info delayed_method_info;
331 DEF_VEC_O (delayed_method_info);
332
333 /* Internal state when decoding a particular compilation unit. */
334 struct dwarf2_cu
335 {
336 /* The objfile containing this compilation unit. */
337 struct objfile *objfile;
338
339 /* The header of the compilation unit. */
340 struct comp_unit_head header;
341
342 /* Base address of this compilation unit. */
343 CORE_ADDR base_address;
344
345 /* Non-zero if base_address has been set. */
346 int base_known;
347
348 struct function_range *first_fn, *last_fn, *cached_fn;
349
350 /* The language we are debugging. */
351 enum language language;
352 const struct language_defn *language_defn;
353
354 const char *producer;
355
356 /* The generic symbol table building routines have separate lists for
357 file scope symbols and all all other scopes (local scopes). So
358 we need to select the right one to pass to add_symbol_to_list().
359 We do it by keeping a pointer to the correct list in list_in_scope.
360
361 FIXME: The original dwarf code just treated the file scope as the
362 first local scope, and all other local scopes as nested local
363 scopes, and worked fine. Check to see if we really need to
364 distinguish these in buildsym.c. */
365 struct pending **list_in_scope;
366
367 /* DWARF abbreviation table associated with this compilation unit. */
368 struct abbrev_info **dwarf2_abbrevs;
369
370 /* Storage for the abbrev table. */
371 struct obstack abbrev_obstack;
372
373 /* Hash table holding all the loaded partial DIEs. */
374 htab_t partial_dies;
375
376 /* Storage for things with the same lifetime as this read-in compilation
377 unit, including partial DIEs. */
378 struct obstack comp_unit_obstack;
379
380 /* When multiple dwarf2_cu structures are living in memory, this field
381 chains them all together, so that they can be released efficiently.
382 We will probably also want a generation counter so that most-recently-used
383 compilation units are cached... */
384 struct dwarf2_per_cu_data *read_in_chain;
385
386 /* Backchain to our per_cu entry if the tree has been built. */
387 struct dwarf2_per_cu_data *per_cu;
388
389 /* How many compilation units ago was this CU last referenced? */
390 int last_used;
391
392 /* A hash table of die offsets for following references. */
393 htab_t die_hash;
394
395 /* Full DIEs if read in. */
396 struct die_info *dies;
397
398 /* A set of pointers to dwarf2_per_cu_data objects for compilation
399 units referenced by this one. Only set during full symbol processing;
400 partial symbol tables do not have dependencies. */
401 htab_t dependencies;
402
403 /* Header data from the line table, during full symbol processing. */
404 struct line_header *line_header;
405
406 /* A list of methods which need to have physnames computed
407 after all type information has been read. */
408 VEC (delayed_method_info) *method_list;
409
410 /* Mark used when releasing cached dies. */
411 unsigned int mark : 1;
412
413 /* This flag will be set if this compilation unit might include
414 inter-compilation-unit references. */
415 unsigned int has_form_ref_addr : 1;
416
417 /* This flag will be set if this compilation unit includes any
418 DW_TAG_namespace DIEs. If we know that there are explicit
419 DIEs for namespaces, we don't need to try to infer them
420 from mangled names. */
421 unsigned int has_namespace_info : 1;
422
423 /* This CU references .debug_loc. See the symtab->locations_valid field.
424 This test is imperfect as there may exist optimized debug code not using
425 any location list and still facing inlining issues if handled as
426 unoptimized code. For a future better test see GCC PR other/32998. */
427
428 unsigned int has_loclist : 1;
429 };
430
431 /* Persistent data held for a compilation unit, even when not
432 processing it. We put a pointer to this structure in the
433 read_symtab_private field of the psymtab. If we encounter
434 inter-compilation-unit references, we also maintain a sorted
435 list of all compilation units. */
436
437 struct dwarf2_per_cu_data
438 {
439 /* The start offset and length of this compilation unit. 2**29-1
440 bytes should suffice to store the length of any compilation unit
441 - if it doesn't, GDB will fall over anyway.
442 NOTE: Unlike comp_unit_head.length, this length includes
443 initial_length_size. */
444 unsigned int offset;
445 unsigned int length : 29;
446
447 /* Flag indicating this compilation unit will be read in before
448 any of the current compilation units are processed. */
449 unsigned int queued : 1;
450
451 /* This flag will be set if we need to load absolutely all DIEs
452 for this compilation unit, instead of just the ones we think
453 are interesting. It gets set if we look for a DIE in the
454 hash table and don't find it. */
455 unsigned int load_all_dies : 1;
456
457 /* Non-null if this CU is from .debug_types; in which case it points
458 to the section. Otherwise it's from .debug_info. */
459 struct dwarf2_section_info *debug_type_section;
460
461 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
462 of the CU cache it gets reset to NULL again. */
463 struct dwarf2_cu *cu;
464
465 /* The corresponding objfile. */
466 struct objfile *objfile;
467
468 /* When using partial symbol tables, the 'psymtab' field is active.
469 Otherwise the 'quick' field is active. */
470 union
471 {
472 /* The partial symbol table associated with this compilation unit,
473 or NULL for partial units (which do not have an associated
474 symtab). */
475 struct partial_symtab *psymtab;
476
477 /* Data needed by the "quick" functions. */
478 struct dwarf2_per_cu_quick_data *quick;
479 } v;
480 };
481
482 /* Entry in the signatured_types hash table. */
483
484 struct signatured_type
485 {
486 ULONGEST signature;
487
488 /* Offset in .debug_types of the type defined by this TU. */
489 unsigned int type_offset;
490
491 /* The CU(/TU) of this type. */
492 struct dwarf2_per_cu_data per_cu;
493 };
494
495 /* Struct used to pass misc. parameters to read_die_and_children, et
496 al. which are used for both .debug_info and .debug_types dies.
497 All parameters here are unchanging for the life of the call. This
498 struct exists to abstract away the constant parameters of die
499 reading. */
500
501 struct die_reader_specs
502 {
503 /* The bfd of this objfile. */
504 bfd* abfd;
505
506 /* The CU of the DIE we are parsing. */
507 struct dwarf2_cu *cu;
508
509 /* Pointer to start of section buffer.
510 This is either the start of .debug_info or .debug_types. */
511 const gdb_byte *buffer;
512 };
513
514 /* The line number information for a compilation unit (found in the
515 .debug_line section) begins with a "statement program header",
516 which contains the following information. */
517 struct line_header
518 {
519 unsigned int total_length;
520 unsigned short version;
521 unsigned int header_length;
522 unsigned char minimum_instruction_length;
523 unsigned char maximum_ops_per_instruction;
524 unsigned char default_is_stmt;
525 int line_base;
526 unsigned char line_range;
527 unsigned char opcode_base;
528
529 /* standard_opcode_lengths[i] is the number of operands for the
530 standard opcode whose value is i. This means that
531 standard_opcode_lengths[0] is unused, and the last meaningful
532 element is standard_opcode_lengths[opcode_base - 1]. */
533 unsigned char *standard_opcode_lengths;
534
535 /* The include_directories table. NOTE! These strings are not
536 allocated with xmalloc; instead, they are pointers into
537 debug_line_buffer. If you try to free them, `free' will get
538 indigestion. */
539 unsigned int num_include_dirs, include_dirs_size;
540 char **include_dirs;
541
542 /* The file_names table. NOTE! These strings are not allocated
543 with xmalloc; instead, they are pointers into debug_line_buffer.
544 Don't try to free them directly. */
545 unsigned int num_file_names, file_names_size;
546 struct file_entry
547 {
548 char *name;
549 unsigned int dir_index;
550 unsigned int mod_time;
551 unsigned int length;
552 int included_p; /* Non-zero if referenced by the Line Number Program. */
553 struct symtab *symtab; /* The associated symbol table, if any. */
554 } *file_names;
555
556 /* The start and end of the statement program following this
557 header. These point into dwarf2_per_objfile->line_buffer. */
558 gdb_byte *statement_program_start, *statement_program_end;
559 };
560
561 /* When we construct a partial symbol table entry we only
562 need this much information. */
563 struct partial_die_info
564 {
565 /* Offset of this DIE. */
566 unsigned int offset;
567
568 /* DWARF-2 tag for this DIE. */
569 ENUM_BITFIELD(dwarf_tag) tag : 16;
570
571 /* Assorted flags describing the data found in this DIE. */
572 unsigned int has_children : 1;
573 unsigned int is_external : 1;
574 unsigned int is_declaration : 1;
575 unsigned int has_type : 1;
576 unsigned int has_specification : 1;
577 unsigned int has_pc_info : 1;
578
579 /* Flag set if the SCOPE field of this structure has been
580 computed. */
581 unsigned int scope_set : 1;
582
583 /* Flag set if the DIE has a byte_size attribute. */
584 unsigned int has_byte_size : 1;
585
586 /* Flag set if any of the DIE's children are template arguments. */
587 unsigned int has_template_arguments : 1;
588
589 /* Flag set if fixup_partial_die has been called on this die. */
590 unsigned int fixup_called : 1;
591
592 /* The name of this DIE. Normally the value of DW_AT_name, but
593 sometimes a default name for unnamed DIEs. */
594 char *name;
595
596 /* The linkage name, if present. */
597 const char *linkage_name;
598
599 /* The scope to prepend to our children. This is generally
600 allocated on the comp_unit_obstack, so will disappear
601 when this compilation unit leaves the cache. */
602 char *scope;
603
604 /* The location description associated with this DIE, if any. */
605 struct dwarf_block *locdesc;
606
607 /* If HAS_PC_INFO, the PC range associated with this DIE. */
608 CORE_ADDR lowpc;
609 CORE_ADDR highpc;
610
611 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
612 DW_AT_sibling, if any. */
613 /* NOTE: This member isn't strictly necessary, read_partial_die could
614 return DW_AT_sibling values to its caller load_partial_dies. */
615 gdb_byte *sibling;
616
617 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
618 DW_AT_specification (or DW_AT_abstract_origin or
619 DW_AT_extension). */
620 unsigned int spec_offset;
621
622 /* Pointers to this DIE's parent, first child, and next sibling,
623 if any. */
624 struct partial_die_info *die_parent, *die_child, *die_sibling;
625 };
626
627 /* This data structure holds the information of an abbrev. */
628 struct abbrev_info
629 {
630 unsigned int number; /* number identifying abbrev */
631 enum dwarf_tag tag; /* dwarf tag */
632 unsigned short has_children; /* boolean */
633 unsigned short num_attrs; /* number of attributes */
634 struct attr_abbrev *attrs; /* an array of attribute descriptions */
635 struct abbrev_info *next; /* next in chain */
636 };
637
638 struct attr_abbrev
639 {
640 ENUM_BITFIELD(dwarf_attribute) name : 16;
641 ENUM_BITFIELD(dwarf_form) form : 16;
642 };
643
644 /* Attributes have a name and a value. */
645 struct attribute
646 {
647 ENUM_BITFIELD(dwarf_attribute) name : 16;
648 ENUM_BITFIELD(dwarf_form) form : 15;
649
650 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
651 field should be in u.str (existing only for DW_STRING) but it is kept
652 here for better struct attribute alignment. */
653 unsigned int string_is_canonical : 1;
654
655 union
656 {
657 char *str;
658 struct dwarf_block *blk;
659 ULONGEST unsnd;
660 LONGEST snd;
661 CORE_ADDR addr;
662 struct signatured_type *signatured_type;
663 }
664 u;
665 };
666
667 /* This data structure holds a complete die structure. */
668 struct die_info
669 {
670 /* DWARF-2 tag for this DIE. */
671 ENUM_BITFIELD(dwarf_tag) tag : 16;
672
673 /* Number of attributes */
674 unsigned char num_attrs;
675
676 /* True if we're presently building the full type name for the
677 type derived from this DIE. */
678 unsigned char building_fullname : 1;
679
680 /* Abbrev number */
681 unsigned int abbrev;
682
683 /* Offset in .debug_info or .debug_types section. */
684 unsigned int offset;
685
686 /* The dies in a compilation unit form an n-ary tree. PARENT
687 points to this die's parent; CHILD points to the first child of
688 this node; and all the children of a given node are chained
689 together via their SIBLING fields. */
690 struct die_info *child; /* Its first child, if any. */
691 struct die_info *sibling; /* Its next sibling, if any. */
692 struct die_info *parent; /* Its parent, if any. */
693
694 /* An array of attributes, with NUM_ATTRS elements. There may be
695 zero, but it's not common and zero-sized arrays are not
696 sufficiently portable C. */
697 struct attribute attrs[1];
698 };
699
700 struct function_range
701 {
702 const char *name;
703 CORE_ADDR lowpc, highpc;
704 int seen_line;
705 struct function_range *next;
706 };
707
708 /* Get at parts of an attribute structure. */
709
710 #define DW_STRING(attr) ((attr)->u.str)
711 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
712 #define DW_UNSND(attr) ((attr)->u.unsnd)
713 #define DW_BLOCK(attr) ((attr)->u.blk)
714 #define DW_SND(attr) ((attr)->u.snd)
715 #define DW_ADDR(attr) ((attr)->u.addr)
716 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
717
718 /* Blocks are a bunch of untyped bytes. */
719 struct dwarf_block
720 {
721 unsigned int size;
722 gdb_byte *data;
723 };
724
725 #ifndef ATTR_ALLOC_CHUNK
726 #define ATTR_ALLOC_CHUNK 4
727 #endif
728
729 /* Allocate fields for structs, unions and enums in this size. */
730 #ifndef DW_FIELD_ALLOC_CHUNK
731 #define DW_FIELD_ALLOC_CHUNK 4
732 #endif
733
734 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
735 but this would require a corresponding change in unpack_field_as_long
736 and friends. */
737 static int bits_per_byte = 8;
738
739 /* The routines that read and process dies for a C struct or C++ class
740 pass lists of data member fields and lists of member function fields
741 in an instance of a field_info structure, as defined below. */
742 struct field_info
743 {
744 /* List of data member and baseclasses fields. */
745 struct nextfield
746 {
747 struct nextfield *next;
748 int accessibility;
749 int virtuality;
750 struct field field;
751 }
752 *fields, *baseclasses;
753
754 /* Number of fields (including baseclasses). */
755 int nfields;
756
757 /* Number of baseclasses. */
758 int nbaseclasses;
759
760 /* Set if the accesibility of one of the fields is not public. */
761 int non_public_fields;
762
763 /* Member function fields array, entries are allocated in the order they
764 are encountered in the object file. */
765 struct nextfnfield
766 {
767 struct nextfnfield *next;
768 struct fn_field fnfield;
769 }
770 *fnfields;
771
772 /* Member function fieldlist array, contains name of possibly overloaded
773 member function, number of overloaded member functions and a pointer
774 to the head of the member function field chain. */
775 struct fnfieldlist
776 {
777 char *name;
778 int length;
779 struct nextfnfield *head;
780 }
781 *fnfieldlists;
782
783 /* Number of entries in the fnfieldlists array. */
784 int nfnfields;
785
786 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
787 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
788 struct typedef_field_list
789 {
790 struct typedef_field field;
791 struct typedef_field_list *next;
792 }
793 *typedef_field_list;
794 unsigned typedef_field_list_count;
795 };
796
797 /* One item on the queue of compilation units to read in full symbols
798 for. */
799 struct dwarf2_queue_item
800 {
801 struct dwarf2_per_cu_data *per_cu;
802 struct dwarf2_queue_item *next;
803 };
804
805 /* The current queue. */
806 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
807
808 /* Loaded secondary compilation units are kept in memory until they
809 have not been referenced for the processing of this many
810 compilation units. Set this to zero to disable caching. Cache
811 sizes of up to at least twenty will improve startup time for
812 typical inter-CU-reference binaries, at an obvious memory cost. */
813 static int dwarf2_max_cache_age = 5;
814 static void
815 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
816 struct cmd_list_element *c, const char *value)
817 {
818 fprintf_filtered (file, _("The upper bound on the age of cached "
819 "dwarf2 compilation units is %s.\n"),
820 value);
821 }
822
823
824 /* Various complaints about symbol reading that don't abort the process. */
825
826 static void
827 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
828 {
829 complaint (&symfile_complaints,
830 _("statement list doesn't fit in .debug_line section"));
831 }
832
833 static void
834 dwarf2_debug_line_missing_file_complaint (void)
835 {
836 complaint (&symfile_complaints,
837 _(".debug_line section has line data without a file"));
838 }
839
840 static void
841 dwarf2_debug_line_missing_end_sequence_complaint (void)
842 {
843 complaint (&symfile_complaints,
844 _(".debug_line section has line "
845 "program sequence without an end"));
846 }
847
848 static void
849 dwarf2_complex_location_expr_complaint (void)
850 {
851 complaint (&symfile_complaints, _("location expression too complex"));
852 }
853
854 static void
855 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
856 int arg3)
857 {
858 complaint (&symfile_complaints,
859 _("const value length mismatch for '%s', got %d, expected %d"),
860 arg1, arg2, arg3);
861 }
862
863 static void
864 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
865 {
866 complaint (&symfile_complaints,
867 _("macro info runs off end of `%s' section"),
868 section->asection->name);
869 }
870
871 static void
872 dwarf2_macro_malformed_definition_complaint (const char *arg1)
873 {
874 complaint (&symfile_complaints,
875 _("macro debug info contains a "
876 "malformed macro definition:\n`%s'"),
877 arg1);
878 }
879
880 static void
881 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
882 {
883 complaint (&symfile_complaints,
884 _("invalid attribute class or form for '%s' in '%s'"),
885 arg1, arg2);
886 }
887
888 /* local function prototypes */
889
890 static void dwarf2_locate_sections (bfd *, asection *, void *);
891
892 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
893 struct objfile *);
894
895 static void dwarf2_find_base_address (struct die_info *die,
896 struct dwarf2_cu *cu);
897
898 static void dwarf2_build_psymtabs_hard (struct objfile *);
899
900 static void scan_partial_symbols (struct partial_die_info *,
901 CORE_ADDR *, CORE_ADDR *,
902 int, struct dwarf2_cu *);
903
904 static void add_partial_symbol (struct partial_die_info *,
905 struct dwarf2_cu *);
906
907 static void add_partial_namespace (struct partial_die_info *pdi,
908 CORE_ADDR *lowpc, CORE_ADDR *highpc,
909 int need_pc, struct dwarf2_cu *cu);
910
911 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
912 CORE_ADDR *highpc, int need_pc,
913 struct dwarf2_cu *cu);
914
915 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
916 struct dwarf2_cu *cu);
917
918 static void add_partial_subprogram (struct partial_die_info *pdi,
919 CORE_ADDR *lowpc, CORE_ADDR *highpc,
920 int need_pc, struct dwarf2_cu *cu);
921
922 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
923 gdb_byte *buffer, gdb_byte *info_ptr,
924 bfd *abfd, struct dwarf2_cu *cu);
925
926 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
927
928 static void psymtab_to_symtab_1 (struct partial_symtab *);
929
930 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
931
932 static void dwarf2_free_abbrev_table (void *);
933
934 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
935 struct dwarf2_cu *);
936
937 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
938 struct dwarf2_cu *);
939
940 static struct partial_die_info *load_partial_dies (bfd *,
941 gdb_byte *, gdb_byte *,
942 int, struct dwarf2_cu *);
943
944 static gdb_byte *read_partial_die (struct partial_die_info *,
945 struct abbrev_info *abbrev,
946 unsigned int, bfd *,
947 gdb_byte *, gdb_byte *,
948 struct dwarf2_cu *);
949
950 static struct partial_die_info *find_partial_die (unsigned int,
951 struct dwarf2_cu *);
952
953 static void fixup_partial_die (struct partial_die_info *,
954 struct dwarf2_cu *);
955
956 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
957 bfd *, gdb_byte *, struct dwarf2_cu *);
958
959 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
960 bfd *, gdb_byte *, struct dwarf2_cu *);
961
962 static unsigned int read_1_byte (bfd *, gdb_byte *);
963
964 static int read_1_signed_byte (bfd *, gdb_byte *);
965
966 static unsigned int read_2_bytes (bfd *, gdb_byte *);
967
968 static unsigned int read_4_bytes (bfd *, gdb_byte *);
969
970 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
971
972 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
973 unsigned int *);
974
975 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
976
977 static LONGEST read_checked_initial_length_and_offset
978 (bfd *, gdb_byte *, const struct comp_unit_head *,
979 unsigned int *, unsigned int *);
980
981 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
982 unsigned int *);
983
984 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
985
986 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
987
988 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
989
990 static char *read_indirect_string (bfd *, gdb_byte *,
991 const struct comp_unit_head *,
992 unsigned int *);
993
994 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
995
996 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
997
998 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
999
1000 static void set_cu_language (unsigned int, struct dwarf2_cu *);
1001
1002 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
1003 struct dwarf2_cu *);
1004
1005 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
1006 unsigned int,
1007 struct dwarf2_cu *);
1008
1009 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
1010 struct dwarf2_cu *cu);
1011
1012 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1013
1014 static struct die_info *die_specification (struct die_info *die,
1015 struct dwarf2_cu **);
1016
1017 static void free_line_header (struct line_header *lh);
1018
1019 static void add_file_name (struct line_header *, char *, unsigned int,
1020 unsigned int, unsigned int);
1021
1022 static struct line_header *(dwarf_decode_line_header
1023 (unsigned int offset,
1024 bfd *abfd, struct dwarf2_cu *cu));
1025
1026 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1027 struct dwarf2_cu *, struct partial_symtab *);
1028
1029 static void dwarf2_start_subfile (char *, const char *, const char *);
1030
1031 static struct symbol *new_symbol (struct die_info *, struct type *,
1032 struct dwarf2_cu *);
1033
1034 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1035 struct dwarf2_cu *, struct symbol *);
1036
1037 static void dwarf2_const_value (struct attribute *, struct symbol *,
1038 struct dwarf2_cu *);
1039
1040 static void dwarf2_const_value_attr (struct attribute *attr,
1041 struct type *type,
1042 const char *name,
1043 struct obstack *obstack,
1044 struct dwarf2_cu *cu, long *value,
1045 gdb_byte **bytes,
1046 struct dwarf2_locexpr_baton **baton);
1047
1048 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1049
1050 static int need_gnat_info (struct dwarf2_cu *);
1051
1052 static struct type *die_descriptive_type (struct die_info *,
1053 struct dwarf2_cu *);
1054
1055 static void set_descriptive_type (struct type *, struct die_info *,
1056 struct dwarf2_cu *);
1057
1058 static struct type *die_containing_type (struct die_info *,
1059 struct dwarf2_cu *);
1060
1061 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1062 struct dwarf2_cu *);
1063
1064 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1065
1066 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1067
1068 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1069
1070 static char *typename_concat (struct obstack *obs, const char *prefix,
1071 const char *suffix, int physname,
1072 struct dwarf2_cu *cu);
1073
1074 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1075
1076 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1077
1078 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1079
1080 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1081
1082 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1083 struct dwarf2_cu *, struct partial_symtab *);
1084
1085 static int dwarf2_get_pc_bounds (struct die_info *,
1086 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1087 struct partial_symtab *);
1088
1089 static void get_scope_pc_bounds (struct die_info *,
1090 CORE_ADDR *, CORE_ADDR *,
1091 struct dwarf2_cu *);
1092
1093 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1094 CORE_ADDR, struct dwarf2_cu *);
1095
1096 static void dwarf2_add_field (struct field_info *, struct die_info *,
1097 struct dwarf2_cu *);
1098
1099 static void dwarf2_attach_fields_to_type (struct field_info *,
1100 struct type *, struct dwarf2_cu *);
1101
1102 static void dwarf2_add_member_fn (struct field_info *,
1103 struct die_info *, struct type *,
1104 struct dwarf2_cu *);
1105
1106 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1107 struct type *,
1108 struct dwarf2_cu *);
1109
1110 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1111
1112 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1113
1114 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1115
1116 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1117
1118 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1119
1120 static struct type *read_module_type (struct die_info *die,
1121 struct dwarf2_cu *cu);
1122
1123 static const char *namespace_name (struct die_info *die,
1124 int *is_anonymous, struct dwarf2_cu *);
1125
1126 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1127
1128 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1129
1130 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1131 struct dwarf2_cu *);
1132
1133 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1134
1135 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1136 gdb_byte *info_ptr,
1137 gdb_byte **new_info_ptr,
1138 struct die_info *parent);
1139
1140 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1141 gdb_byte *info_ptr,
1142 gdb_byte **new_info_ptr,
1143 struct die_info *parent);
1144
1145 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1146 gdb_byte *info_ptr,
1147 gdb_byte **new_info_ptr,
1148 struct die_info *parent);
1149
1150 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1151 struct die_info **, gdb_byte *,
1152 int *);
1153
1154 static void process_die (struct die_info *, struct dwarf2_cu *);
1155
1156 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1157 struct obstack *);
1158
1159 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1160
1161 static const char *dwarf2_full_name (char *name,
1162 struct die_info *die,
1163 struct dwarf2_cu *cu);
1164
1165 static struct die_info *dwarf2_extension (struct die_info *die,
1166 struct dwarf2_cu **);
1167
1168 static char *dwarf_tag_name (unsigned int);
1169
1170 static char *dwarf_attr_name (unsigned int);
1171
1172 static char *dwarf_form_name (unsigned int);
1173
1174 static char *dwarf_bool_name (unsigned int);
1175
1176 static char *dwarf_type_encoding_name (unsigned int);
1177
1178 #if 0
1179 static char *dwarf_cfi_name (unsigned int);
1180 #endif
1181
1182 static struct die_info *sibling_die (struct die_info *);
1183
1184 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1185
1186 static void dump_die_for_error (struct die_info *);
1187
1188 static void dump_die_1 (struct ui_file *, int level, int max_level,
1189 struct die_info *);
1190
1191 /*static*/ void dump_die (struct die_info *, int max_level);
1192
1193 static void store_in_ref_table (struct die_info *,
1194 struct dwarf2_cu *);
1195
1196 static int is_ref_attr (struct attribute *);
1197
1198 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1199
1200 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1201
1202 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1203 struct attribute *,
1204 struct dwarf2_cu **);
1205
1206 static struct die_info *follow_die_ref (struct die_info *,
1207 struct attribute *,
1208 struct dwarf2_cu **);
1209
1210 static struct die_info *follow_die_sig (struct die_info *,
1211 struct attribute *,
1212 struct dwarf2_cu **);
1213
1214 static struct signatured_type *lookup_signatured_type_at_offset
1215 (struct objfile *objfile,
1216 struct dwarf2_section_info *section,
1217 unsigned int offset);
1218
1219 static void read_signatured_type_at_offset (struct objfile *objfile,
1220 struct dwarf2_section_info *sect,
1221 unsigned int offset);
1222
1223 static void read_signatured_type (struct objfile *,
1224 struct signatured_type *type_sig);
1225
1226 /* memory allocation interface */
1227
1228 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1229
1230 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1231
1232 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1233
1234 static void initialize_cu_func_list (struct dwarf2_cu *);
1235
1236 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1237 struct dwarf2_cu *);
1238
1239 static void dwarf_decode_macros (struct line_header *, unsigned int,
1240 char *, bfd *, struct dwarf2_cu *,
1241 struct dwarf2_section_info *,
1242 int);
1243
1244 static int attr_form_is_block (struct attribute *);
1245
1246 static int attr_form_is_section_offset (struct attribute *);
1247
1248 static int attr_form_is_constant (struct attribute *);
1249
1250 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1251 struct dwarf2_loclist_baton *baton,
1252 struct attribute *attr);
1253
1254 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1255 struct symbol *sym,
1256 struct dwarf2_cu *cu);
1257
1258 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1259 struct abbrev_info *abbrev,
1260 struct dwarf2_cu *cu);
1261
1262 static void free_stack_comp_unit (void *);
1263
1264 static hashval_t partial_die_hash (const void *item);
1265
1266 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1267
1268 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1269 (unsigned int offset, struct objfile *objfile);
1270
1271 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1272 (unsigned int offset, struct objfile *objfile);
1273
1274 static void init_one_comp_unit (struct dwarf2_cu *cu,
1275 struct objfile *objfile);
1276
1277 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1278 struct die_info *comp_unit_die);
1279
1280 static void free_one_comp_unit (void *);
1281
1282 static void free_cached_comp_units (void *);
1283
1284 static void age_cached_comp_units (void);
1285
1286 static void free_one_cached_comp_unit (void *);
1287
1288 static struct type *set_die_type (struct die_info *, struct type *,
1289 struct dwarf2_cu *);
1290
1291 static void create_all_comp_units (struct objfile *);
1292
1293 static int create_debug_types_hash_table (struct objfile *objfile);
1294
1295 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1296 struct objfile *);
1297
1298 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1299
1300 static void dwarf2_add_dependence (struct dwarf2_cu *,
1301 struct dwarf2_per_cu_data *);
1302
1303 static void dwarf2_mark (struct dwarf2_cu *);
1304
1305 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1306
1307 static struct type *get_die_type_at_offset (unsigned int,
1308 struct dwarf2_per_cu_data *per_cu);
1309
1310 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1311
1312 static void dwarf2_release_queue (void *dummy);
1313
1314 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1315 struct objfile *objfile);
1316
1317 static void process_queue (struct objfile *objfile);
1318
1319 static void find_file_and_directory (struct die_info *die,
1320 struct dwarf2_cu *cu,
1321 char **name, char **comp_dir);
1322
1323 static char *file_full_name (int file, struct line_header *lh,
1324 const char *comp_dir);
1325
1326 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1327 gdb_byte *info_ptr,
1328 gdb_byte *buffer,
1329 unsigned int buffer_size,
1330 bfd *abfd);
1331
1332 static void init_cu_die_reader (struct die_reader_specs *reader,
1333 struct dwarf2_cu *cu);
1334
1335 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1336
1337 #if WORDS_BIGENDIAN
1338
1339 /* Convert VALUE between big- and little-endian. */
1340 static offset_type
1341 byte_swap (offset_type value)
1342 {
1343 offset_type result;
1344
1345 result = (value & 0xff) << 24;
1346 result |= (value & 0xff00) << 8;
1347 result |= (value & 0xff0000) >> 8;
1348 result |= (value & 0xff000000) >> 24;
1349 return result;
1350 }
1351
1352 #define MAYBE_SWAP(V) byte_swap (V)
1353
1354 #else
1355 #define MAYBE_SWAP(V) (V)
1356 #endif /* WORDS_BIGENDIAN */
1357
1358 /* The suffix for an index file. */
1359 #define INDEX_SUFFIX ".gdb-index"
1360
1361 static const char *dwarf2_physname (char *name, struct die_info *die,
1362 struct dwarf2_cu *cu);
1363
1364 /* Try to locate the sections we need for DWARF 2 debugging
1365 information and return true if we have enough to do something.
1366 NAMES points to the dwarf2 section names, or is NULL if the standard
1367 ELF names are used. */
1368
1369 int
1370 dwarf2_has_info (struct objfile *objfile,
1371 const struct dwarf2_debug_sections *names)
1372 {
1373 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1374 if (!dwarf2_per_objfile)
1375 {
1376 /* Initialize per-objfile state. */
1377 struct dwarf2_per_objfile *data
1378 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1379
1380 memset (data, 0, sizeof (*data));
1381 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1382 dwarf2_per_objfile = data;
1383
1384 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1385 (void *) names);
1386 dwarf2_per_objfile->objfile = objfile;
1387 }
1388 return (dwarf2_per_objfile->info.asection != NULL
1389 && dwarf2_per_objfile->abbrev.asection != NULL);
1390 }
1391
1392 /* When loading sections, we look either for uncompressed section or for
1393 compressed section names. */
1394
1395 static int
1396 section_is_p (const char *section_name,
1397 const struct dwarf2_section_names *names)
1398 {
1399 if (names->normal != NULL
1400 && strcmp (section_name, names->normal) == 0)
1401 return 1;
1402 if (names->compressed != NULL
1403 && strcmp (section_name, names->compressed) == 0)
1404 return 1;
1405 return 0;
1406 }
1407
1408 /* This function is mapped across the sections and remembers the
1409 offset and size of each of the debugging sections we are interested
1410 in. */
1411
1412 static void
1413 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1414 {
1415 const struct dwarf2_debug_sections *names;
1416
1417 if (vnames == NULL)
1418 names = &dwarf2_elf_names;
1419 else
1420 names = (const struct dwarf2_debug_sections *) vnames;
1421
1422 if (section_is_p (sectp->name, &names->info))
1423 {
1424 dwarf2_per_objfile->info.asection = sectp;
1425 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1426 }
1427 else if (section_is_p (sectp->name, &names->abbrev))
1428 {
1429 dwarf2_per_objfile->abbrev.asection = sectp;
1430 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1431 }
1432 else if (section_is_p (sectp->name, &names->line))
1433 {
1434 dwarf2_per_objfile->line.asection = sectp;
1435 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1436 }
1437 else if (section_is_p (sectp->name, &names->loc))
1438 {
1439 dwarf2_per_objfile->loc.asection = sectp;
1440 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1441 }
1442 else if (section_is_p (sectp->name, &names->macinfo))
1443 {
1444 dwarf2_per_objfile->macinfo.asection = sectp;
1445 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1446 }
1447 else if (section_is_p (sectp->name, &names->macro))
1448 {
1449 dwarf2_per_objfile->macro.asection = sectp;
1450 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1451 }
1452 else if (section_is_p (sectp->name, &names->str))
1453 {
1454 dwarf2_per_objfile->str.asection = sectp;
1455 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1456 }
1457 else if (section_is_p (sectp->name, &names->frame))
1458 {
1459 dwarf2_per_objfile->frame.asection = sectp;
1460 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1461 }
1462 else if (section_is_p (sectp->name, &names->eh_frame))
1463 {
1464 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1465
1466 if (aflag & SEC_HAS_CONTENTS)
1467 {
1468 dwarf2_per_objfile->eh_frame.asection = sectp;
1469 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1470 }
1471 }
1472 else if (section_is_p (sectp->name, &names->ranges))
1473 {
1474 dwarf2_per_objfile->ranges.asection = sectp;
1475 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1476 }
1477 else if (section_is_p (sectp->name, &names->types))
1478 {
1479 struct dwarf2_section_info type_section;
1480
1481 memset (&type_section, 0, sizeof (type_section));
1482 type_section.asection = sectp;
1483 type_section.size = bfd_get_section_size (sectp);
1484
1485 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1486 &type_section);
1487 }
1488 else if (section_is_p (sectp->name, &names->gdb_index))
1489 {
1490 dwarf2_per_objfile->gdb_index.asection = sectp;
1491 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1492 }
1493
1494 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1495 && bfd_section_vma (abfd, sectp) == 0)
1496 dwarf2_per_objfile->has_section_at_zero = 1;
1497 }
1498
1499 /* Decompress a section that was compressed using zlib. Store the
1500 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1501
1502 static void
1503 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1504 gdb_byte **outbuf, bfd_size_type *outsize)
1505 {
1506 bfd *abfd = objfile->obfd;
1507 #ifndef HAVE_ZLIB_H
1508 error (_("Support for zlib-compressed DWARF data (from '%s') "
1509 "is disabled in this copy of GDB"),
1510 bfd_get_filename (abfd));
1511 #else
1512 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1513 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1514 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1515 bfd_size_type uncompressed_size;
1516 gdb_byte *uncompressed_buffer;
1517 z_stream strm;
1518 int rc;
1519 int header_size = 12;
1520
1521 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1522 || bfd_bread (compressed_buffer,
1523 compressed_size, abfd) != compressed_size)
1524 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1525 bfd_get_filename (abfd));
1526
1527 /* Read the zlib header. In this case, it should be "ZLIB" followed
1528 by the uncompressed section size, 8 bytes in big-endian order. */
1529 if (compressed_size < header_size
1530 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1531 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1532 bfd_get_filename (abfd));
1533 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1534 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1535 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1536 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1537 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1538 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1539 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1540 uncompressed_size += compressed_buffer[11];
1541
1542 /* It is possible the section consists of several compressed
1543 buffers concatenated together, so we uncompress in a loop. */
1544 strm.zalloc = NULL;
1545 strm.zfree = NULL;
1546 strm.opaque = NULL;
1547 strm.avail_in = compressed_size - header_size;
1548 strm.next_in = (Bytef*) compressed_buffer + header_size;
1549 strm.avail_out = uncompressed_size;
1550 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1551 uncompressed_size);
1552 rc = inflateInit (&strm);
1553 while (strm.avail_in > 0)
1554 {
1555 if (rc != Z_OK)
1556 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1557 bfd_get_filename (abfd), rc);
1558 strm.next_out = ((Bytef*) uncompressed_buffer
1559 + (uncompressed_size - strm.avail_out));
1560 rc = inflate (&strm, Z_FINISH);
1561 if (rc != Z_STREAM_END)
1562 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1563 bfd_get_filename (abfd), rc);
1564 rc = inflateReset (&strm);
1565 }
1566 rc = inflateEnd (&strm);
1567 if (rc != Z_OK
1568 || strm.avail_out != 0)
1569 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1570 bfd_get_filename (abfd), rc);
1571
1572 do_cleanups (cleanup);
1573 *outbuf = uncompressed_buffer;
1574 *outsize = uncompressed_size;
1575 #endif
1576 }
1577
1578 /* A helper function that decides whether a section is empty. */
1579
1580 static int
1581 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1582 {
1583 return info->asection == NULL || info->size == 0;
1584 }
1585
1586 /* Read the contents of the section SECTP from object file specified by
1587 OBJFILE, store info about the section into INFO.
1588 If the section is compressed, uncompress it before returning. */
1589
1590 static void
1591 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1592 {
1593 bfd *abfd = objfile->obfd;
1594 asection *sectp = info->asection;
1595 gdb_byte *buf, *retbuf;
1596 unsigned char header[4];
1597
1598 if (info->readin)
1599 return;
1600 info->buffer = NULL;
1601 info->map_addr = NULL;
1602 info->readin = 1;
1603
1604 if (dwarf2_section_empty_p (info))
1605 return;
1606
1607 /* Check if the file has a 4-byte header indicating compression. */
1608 if (info->size > sizeof (header)
1609 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1610 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1611 {
1612 /* Upon decompression, update the buffer and its size. */
1613 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1614 {
1615 zlib_decompress_section (objfile, sectp, &info->buffer,
1616 &info->size);
1617 return;
1618 }
1619 }
1620
1621 #ifdef HAVE_MMAP
1622 if (pagesize == 0)
1623 pagesize = getpagesize ();
1624
1625 /* Only try to mmap sections which are large enough: we don't want to
1626 waste space due to fragmentation. Also, only try mmap for sections
1627 without relocations. */
1628
1629 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1630 {
1631 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1632 MAP_PRIVATE, sectp->filepos,
1633 &info->map_addr, &info->map_len);
1634
1635 if ((caddr_t)info->buffer != MAP_FAILED)
1636 {
1637 #if HAVE_POSIX_MADVISE
1638 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1639 #endif
1640 return;
1641 }
1642 }
1643 #endif
1644
1645 /* If we get here, we are a normal, not-compressed section. */
1646 info->buffer = buf
1647 = obstack_alloc (&objfile->objfile_obstack, info->size);
1648
1649 /* When debugging .o files, we may need to apply relocations; see
1650 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1651 We never compress sections in .o files, so we only need to
1652 try this when the section is not compressed. */
1653 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1654 if (retbuf != NULL)
1655 {
1656 info->buffer = retbuf;
1657 return;
1658 }
1659
1660 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1661 || bfd_bread (buf, info->size, abfd) != info->size)
1662 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1663 bfd_get_filename (abfd));
1664 }
1665
1666 /* A helper function that returns the size of a section in a safe way.
1667 If you are positive that the section has been read before using the
1668 size, then it is safe to refer to the dwarf2_section_info object's
1669 "size" field directly. In other cases, you must call this
1670 function, because for compressed sections the size field is not set
1671 correctly until the section has been read. */
1672
1673 static bfd_size_type
1674 dwarf2_section_size (struct objfile *objfile,
1675 struct dwarf2_section_info *info)
1676 {
1677 if (!info->readin)
1678 dwarf2_read_section (objfile, info);
1679 return info->size;
1680 }
1681
1682 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1683 SECTION_NAME. */
1684
1685 void
1686 dwarf2_get_section_info (struct objfile *objfile,
1687 enum dwarf2_section_enum sect,
1688 asection **sectp, gdb_byte **bufp,
1689 bfd_size_type *sizep)
1690 {
1691 struct dwarf2_per_objfile *data
1692 = objfile_data (objfile, dwarf2_objfile_data_key);
1693 struct dwarf2_section_info *info;
1694
1695 /* We may see an objfile without any DWARF, in which case we just
1696 return nothing. */
1697 if (data == NULL)
1698 {
1699 *sectp = NULL;
1700 *bufp = NULL;
1701 *sizep = 0;
1702 return;
1703 }
1704 switch (sect)
1705 {
1706 case DWARF2_DEBUG_FRAME:
1707 info = &data->frame;
1708 break;
1709 case DWARF2_EH_FRAME:
1710 info = &data->eh_frame;
1711 break;
1712 default:
1713 gdb_assert_not_reached ("unexpected section");
1714 }
1715
1716 dwarf2_read_section (objfile, info);
1717
1718 *sectp = info->asection;
1719 *bufp = info->buffer;
1720 *sizep = info->size;
1721 }
1722
1723 \f
1724 /* DWARF quick_symbols_functions support. */
1725
1726 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1727 unique line tables, so we maintain a separate table of all .debug_line
1728 derived entries to support the sharing.
1729 All the quick functions need is the list of file names. We discard the
1730 line_header when we're done and don't need to record it here. */
1731 struct quick_file_names
1732 {
1733 /* The offset in .debug_line of the line table. We hash on this. */
1734 unsigned int offset;
1735
1736 /* The number of entries in file_names, real_names. */
1737 unsigned int num_file_names;
1738
1739 /* The file names from the line table, after being run through
1740 file_full_name. */
1741 const char **file_names;
1742
1743 /* The file names from the line table after being run through
1744 gdb_realpath. These are computed lazily. */
1745 const char **real_names;
1746 };
1747
1748 /* When using the index (and thus not using psymtabs), each CU has an
1749 object of this type. This is used to hold information needed by
1750 the various "quick" methods. */
1751 struct dwarf2_per_cu_quick_data
1752 {
1753 /* The file table. This can be NULL if there was no file table
1754 or it's currently not read in.
1755 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1756 struct quick_file_names *file_names;
1757
1758 /* The corresponding symbol table. This is NULL if symbols for this
1759 CU have not yet been read. */
1760 struct symtab *symtab;
1761
1762 /* A temporary mark bit used when iterating over all CUs in
1763 expand_symtabs_matching. */
1764 unsigned int mark : 1;
1765
1766 /* True if we've tried to read the file table and found there isn't one.
1767 There will be no point in trying to read it again next time. */
1768 unsigned int no_file_data : 1;
1769 };
1770
1771 /* Hash function for a quick_file_names. */
1772
1773 static hashval_t
1774 hash_file_name_entry (const void *e)
1775 {
1776 const struct quick_file_names *file_data = e;
1777
1778 return file_data->offset;
1779 }
1780
1781 /* Equality function for a quick_file_names. */
1782
1783 static int
1784 eq_file_name_entry (const void *a, const void *b)
1785 {
1786 const struct quick_file_names *ea = a;
1787 const struct quick_file_names *eb = b;
1788
1789 return ea->offset == eb->offset;
1790 }
1791
1792 /* Delete function for a quick_file_names. */
1793
1794 static void
1795 delete_file_name_entry (void *e)
1796 {
1797 struct quick_file_names *file_data = e;
1798 int i;
1799
1800 for (i = 0; i < file_data->num_file_names; ++i)
1801 {
1802 xfree ((void*) file_data->file_names[i]);
1803 if (file_data->real_names)
1804 xfree ((void*) file_data->real_names[i]);
1805 }
1806
1807 /* The space for the struct itself lives on objfile_obstack,
1808 so we don't free it here. */
1809 }
1810
1811 /* Create a quick_file_names hash table. */
1812
1813 static htab_t
1814 create_quick_file_names_table (unsigned int nr_initial_entries)
1815 {
1816 return htab_create_alloc (nr_initial_entries,
1817 hash_file_name_entry, eq_file_name_entry,
1818 delete_file_name_entry, xcalloc, xfree);
1819 }
1820
1821 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1822 have to be created afterwards. You should call age_cached_comp_units after
1823 processing PER_CU->CU. dw2_setup must have been already called. */
1824
1825 static void
1826 load_cu (struct dwarf2_per_cu_data *per_cu)
1827 {
1828 if (per_cu->debug_type_section)
1829 read_signatured_type_at_offset (per_cu->objfile,
1830 per_cu->debug_type_section,
1831 per_cu->offset);
1832 else
1833 load_full_comp_unit (per_cu, per_cu->objfile);
1834
1835 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1836
1837 gdb_assert (per_cu->cu != NULL);
1838 }
1839
1840 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1841 this CU came. */
1842
1843 static void
1844 dw2_do_instantiate_symtab (struct objfile *objfile,
1845 struct dwarf2_per_cu_data *per_cu)
1846 {
1847 struct cleanup *back_to;
1848
1849 back_to = make_cleanup (dwarf2_release_queue, NULL);
1850
1851 queue_comp_unit (per_cu, objfile);
1852
1853 load_cu (per_cu);
1854
1855 process_queue (objfile);
1856
1857 /* Age the cache, releasing compilation units that have not
1858 been used recently. */
1859 age_cached_comp_units ();
1860
1861 do_cleanups (back_to);
1862 }
1863
1864 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1865 the objfile from which this CU came. Returns the resulting symbol
1866 table. */
1867
1868 static struct symtab *
1869 dw2_instantiate_symtab (struct objfile *objfile,
1870 struct dwarf2_per_cu_data *per_cu)
1871 {
1872 if (!per_cu->v.quick->symtab)
1873 {
1874 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1875 increment_reading_symtab ();
1876 dw2_do_instantiate_symtab (objfile, per_cu);
1877 do_cleanups (back_to);
1878 }
1879 return per_cu->v.quick->symtab;
1880 }
1881
1882 /* Return the CU given its index. */
1883
1884 static struct dwarf2_per_cu_data *
1885 dw2_get_cu (int index)
1886 {
1887 if (index >= dwarf2_per_objfile->n_comp_units)
1888 {
1889 index -= dwarf2_per_objfile->n_comp_units;
1890 return dwarf2_per_objfile->type_comp_units[index];
1891 }
1892 return dwarf2_per_objfile->all_comp_units[index];
1893 }
1894
1895 /* A helper function that knows how to read a 64-bit value in a way
1896 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1897 otherwise. */
1898
1899 static int
1900 extract_cu_value (const char *bytes, ULONGEST *result)
1901 {
1902 if (sizeof (ULONGEST) < 8)
1903 {
1904 int i;
1905
1906 /* Ignore the upper 4 bytes if they are all zero. */
1907 for (i = 0; i < 4; ++i)
1908 if (bytes[i + 4] != 0)
1909 return 0;
1910
1911 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1912 }
1913 else
1914 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1915 return 1;
1916 }
1917
1918 /* Read the CU list from the mapped index, and use it to create all
1919 the CU objects for this objfile. Return 0 if something went wrong,
1920 1 if everything went ok. */
1921
1922 static int
1923 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1924 offset_type cu_list_elements)
1925 {
1926 offset_type i;
1927
1928 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1929 dwarf2_per_objfile->all_comp_units
1930 = obstack_alloc (&objfile->objfile_obstack,
1931 dwarf2_per_objfile->n_comp_units
1932 * sizeof (struct dwarf2_per_cu_data *));
1933
1934 for (i = 0; i < cu_list_elements; i += 2)
1935 {
1936 struct dwarf2_per_cu_data *the_cu;
1937 ULONGEST offset, length;
1938
1939 if (!extract_cu_value (cu_list, &offset)
1940 || !extract_cu_value (cu_list + 8, &length))
1941 return 0;
1942 cu_list += 2 * 8;
1943
1944 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1945 struct dwarf2_per_cu_data);
1946 the_cu->offset = offset;
1947 the_cu->length = length;
1948 the_cu->objfile = objfile;
1949 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1950 struct dwarf2_per_cu_quick_data);
1951 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1952 }
1953
1954 return 1;
1955 }
1956
1957 /* Create the signatured type hash table from the index. */
1958
1959 static int
1960 create_signatured_type_table_from_index (struct objfile *objfile,
1961 struct dwarf2_section_info *section,
1962 const gdb_byte *bytes,
1963 offset_type elements)
1964 {
1965 offset_type i;
1966 htab_t sig_types_hash;
1967
1968 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1969 dwarf2_per_objfile->type_comp_units
1970 = obstack_alloc (&objfile->objfile_obstack,
1971 dwarf2_per_objfile->n_type_comp_units
1972 * sizeof (struct dwarf2_per_cu_data *));
1973
1974 sig_types_hash = allocate_signatured_type_table (objfile);
1975
1976 for (i = 0; i < elements; i += 3)
1977 {
1978 struct signatured_type *type_sig;
1979 ULONGEST offset, type_offset, signature;
1980 void **slot;
1981
1982 if (!extract_cu_value (bytes, &offset)
1983 || !extract_cu_value (bytes + 8, &type_offset))
1984 return 0;
1985 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1986 bytes += 3 * 8;
1987
1988 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1989 struct signatured_type);
1990 type_sig->signature = signature;
1991 type_sig->type_offset = type_offset;
1992 type_sig->per_cu.debug_type_section = section;
1993 type_sig->per_cu.offset = offset;
1994 type_sig->per_cu.objfile = objfile;
1995 type_sig->per_cu.v.quick
1996 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1997 struct dwarf2_per_cu_quick_data);
1998
1999 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
2000 *slot = type_sig;
2001
2002 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
2003 }
2004
2005 dwarf2_per_objfile->signatured_types = sig_types_hash;
2006
2007 return 1;
2008 }
2009
2010 /* Read the address map data from the mapped index, and use it to
2011 populate the objfile's psymtabs_addrmap. */
2012
2013 static void
2014 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
2015 {
2016 const gdb_byte *iter, *end;
2017 struct obstack temp_obstack;
2018 struct addrmap *mutable_map;
2019 struct cleanup *cleanup;
2020 CORE_ADDR baseaddr;
2021
2022 obstack_init (&temp_obstack);
2023 cleanup = make_cleanup_obstack_free (&temp_obstack);
2024 mutable_map = addrmap_create_mutable (&temp_obstack);
2025
2026 iter = index->address_table;
2027 end = iter + index->address_table_size;
2028
2029 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
2030
2031 while (iter < end)
2032 {
2033 ULONGEST hi, lo, cu_index;
2034 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2035 iter += 8;
2036 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
2037 iter += 8;
2038 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
2039 iter += 4;
2040
2041 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
2042 dw2_get_cu (cu_index));
2043 }
2044
2045 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
2046 &objfile->objfile_obstack);
2047 do_cleanups (cleanup);
2048 }
2049
2050 /* The hash function for strings in the mapped index. This is the same as
2051 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2052 implementation. This is necessary because the hash function is tied to the
2053 format of the mapped index file. The hash values do not have to match with
2054 SYMBOL_HASH_NEXT.
2055
2056 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2057
2058 static hashval_t
2059 mapped_index_string_hash (int index_version, const void *p)
2060 {
2061 const unsigned char *str = (const unsigned char *) p;
2062 hashval_t r = 0;
2063 unsigned char c;
2064
2065 while ((c = *str++) != 0)
2066 {
2067 if (index_version >= 5)
2068 c = tolower (c);
2069 r = r * 67 + c - 113;
2070 }
2071
2072 return r;
2073 }
2074
2075 /* Find a slot in the mapped index INDEX for the object named NAME.
2076 If NAME is found, set *VEC_OUT to point to the CU vector in the
2077 constant pool and return 1. If NAME cannot be found, return 0. */
2078
2079 static int
2080 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2081 offset_type **vec_out)
2082 {
2083 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2084 offset_type hash;
2085 offset_type slot, step;
2086 int (*cmp) (const char *, const char *);
2087
2088 if (current_language->la_language == language_cplus
2089 || current_language->la_language == language_java
2090 || current_language->la_language == language_fortran)
2091 {
2092 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2093 not contain any. */
2094 const char *paren = strchr (name, '(');
2095
2096 if (paren)
2097 {
2098 char *dup;
2099
2100 dup = xmalloc (paren - name + 1);
2101 memcpy (dup, name, paren - name);
2102 dup[paren - name] = 0;
2103
2104 make_cleanup (xfree, dup);
2105 name = dup;
2106 }
2107 }
2108
2109 /* Index version 4 did not support case insensitive searches. But the
2110 indexes for case insensitive languages are built in lowercase, therefore
2111 simulate our NAME being searched is also lowercased. */
2112 hash = mapped_index_string_hash ((index->version == 4
2113 && case_sensitivity == case_sensitive_off
2114 ? 5 : index->version),
2115 name);
2116
2117 slot = hash & (index->symbol_table_slots - 1);
2118 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2119 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2120
2121 for (;;)
2122 {
2123 /* Convert a slot number to an offset into the table. */
2124 offset_type i = 2 * slot;
2125 const char *str;
2126 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2127 {
2128 do_cleanups (back_to);
2129 return 0;
2130 }
2131
2132 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2133 if (!cmp (name, str))
2134 {
2135 *vec_out = (offset_type *) (index->constant_pool
2136 + MAYBE_SWAP (index->symbol_table[i + 1]));
2137 do_cleanups (back_to);
2138 return 1;
2139 }
2140
2141 slot = (slot + step) & (index->symbol_table_slots - 1);
2142 }
2143 }
2144
2145 /* Read the index file. If everything went ok, initialize the "quick"
2146 elements of all the CUs and return 1. Otherwise, return 0. */
2147
2148 static int
2149 dwarf2_read_index (struct objfile *objfile)
2150 {
2151 char *addr;
2152 struct mapped_index *map;
2153 offset_type *metadata;
2154 const gdb_byte *cu_list;
2155 const gdb_byte *types_list = NULL;
2156 offset_type version, cu_list_elements;
2157 offset_type types_list_elements = 0;
2158 int i;
2159
2160 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2161 return 0;
2162
2163 /* Older elfutils strip versions could keep the section in the main
2164 executable while splitting it for the separate debug info file. */
2165 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2166 & SEC_HAS_CONTENTS) == 0)
2167 return 0;
2168
2169 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2170
2171 addr = dwarf2_per_objfile->gdb_index.buffer;
2172 /* Version check. */
2173 version = MAYBE_SWAP (*(offset_type *) addr);
2174 /* Versions earlier than 3 emitted every copy of a psymbol. This
2175 causes the index to behave very poorly for certain requests. Version 3
2176 contained incomplete addrmap. So, it seems better to just ignore such
2177 indices. Index version 4 uses a different hash function than index
2178 version 5 and later. */
2179 if (version < 4)
2180 return 0;
2181 /* Indexes with higher version than the one supported by GDB may be no
2182 longer backward compatible. */
2183 if (version > 5)
2184 return 0;
2185
2186 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2187 map->version = version;
2188 map->total_size = dwarf2_per_objfile->gdb_index.size;
2189
2190 metadata = (offset_type *) (addr + sizeof (offset_type));
2191
2192 i = 0;
2193 cu_list = addr + MAYBE_SWAP (metadata[i]);
2194 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2195 / 8);
2196 ++i;
2197
2198 types_list = addr + MAYBE_SWAP (metadata[i]);
2199 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2200 - MAYBE_SWAP (metadata[i]))
2201 / 8);
2202 ++i;
2203
2204 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2205 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2206 - MAYBE_SWAP (metadata[i]));
2207 ++i;
2208
2209 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2210 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2211 - MAYBE_SWAP (metadata[i]))
2212 / (2 * sizeof (offset_type)));
2213 ++i;
2214
2215 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2216
2217 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2218 return 0;
2219
2220 if (types_list_elements)
2221 {
2222 struct dwarf2_section_info *section;
2223
2224 /* We can only handle a single .debug_types when we have an
2225 index. */
2226 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2227 return 0;
2228
2229 section = VEC_index (dwarf2_section_info_def,
2230 dwarf2_per_objfile->types, 0);
2231
2232 if (!create_signatured_type_table_from_index (objfile, section,
2233 types_list,
2234 types_list_elements))
2235 return 0;
2236 }
2237
2238 create_addrmap_from_index (objfile, map);
2239
2240 dwarf2_per_objfile->index_table = map;
2241 dwarf2_per_objfile->using_index = 1;
2242 dwarf2_per_objfile->quick_file_names_table =
2243 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2244
2245 return 1;
2246 }
2247
2248 /* A helper for the "quick" functions which sets the global
2249 dwarf2_per_objfile according to OBJFILE. */
2250
2251 static void
2252 dw2_setup (struct objfile *objfile)
2253 {
2254 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2255 gdb_assert (dwarf2_per_objfile);
2256 }
2257
2258 /* A helper for the "quick" functions which attempts to read the line
2259 table for THIS_CU. */
2260
2261 static struct quick_file_names *
2262 dw2_get_file_names (struct objfile *objfile,
2263 struct dwarf2_per_cu_data *this_cu)
2264 {
2265 bfd *abfd = objfile->obfd;
2266 struct line_header *lh;
2267 struct attribute *attr;
2268 struct cleanup *cleanups;
2269 struct die_info *comp_unit_die;
2270 struct dwarf2_section_info* sec;
2271 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2272 int has_children, i;
2273 struct dwarf2_cu cu;
2274 unsigned int bytes_read, buffer_size;
2275 struct die_reader_specs reader_specs;
2276 char *name, *comp_dir;
2277 void **slot;
2278 struct quick_file_names *qfn;
2279 unsigned int line_offset;
2280
2281 if (this_cu->v.quick->file_names != NULL)
2282 return this_cu->v.quick->file_names;
2283 /* If we know there is no line data, no point in looking again. */
2284 if (this_cu->v.quick->no_file_data)
2285 return NULL;
2286
2287 init_one_comp_unit (&cu, objfile);
2288 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2289
2290 if (this_cu->debug_type_section)
2291 sec = this_cu->debug_type_section;
2292 else
2293 sec = &dwarf2_per_objfile->info;
2294 dwarf2_read_section (objfile, sec);
2295 buffer_size = sec->size;
2296 buffer = sec->buffer;
2297 info_ptr = buffer + this_cu->offset;
2298 beg_of_comp_unit = info_ptr;
2299
2300 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2301 buffer, buffer_size,
2302 abfd);
2303
2304 /* Complete the cu_header. */
2305 cu.header.offset = beg_of_comp_unit - buffer;
2306 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2307
2308 this_cu->cu = &cu;
2309 cu.per_cu = this_cu;
2310
2311 dwarf2_read_abbrevs (abfd, &cu);
2312 make_cleanup (dwarf2_free_abbrev_table, &cu);
2313
2314 if (this_cu->debug_type_section)
2315 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2316 init_cu_die_reader (&reader_specs, &cu);
2317 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2318 &has_children);
2319
2320 lh = NULL;
2321 slot = NULL;
2322 line_offset = 0;
2323 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2324 if (attr)
2325 {
2326 struct quick_file_names find_entry;
2327
2328 line_offset = DW_UNSND (attr);
2329
2330 /* We may have already read in this line header (TU line header sharing).
2331 If we have we're done. */
2332 find_entry.offset = line_offset;
2333 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2334 &find_entry, INSERT);
2335 if (*slot != NULL)
2336 {
2337 do_cleanups (cleanups);
2338 this_cu->v.quick->file_names = *slot;
2339 return *slot;
2340 }
2341
2342 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2343 }
2344 if (lh == NULL)
2345 {
2346 do_cleanups (cleanups);
2347 this_cu->v.quick->no_file_data = 1;
2348 return NULL;
2349 }
2350
2351 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2352 qfn->offset = line_offset;
2353 gdb_assert (slot != NULL);
2354 *slot = qfn;
2355
2356 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2357
2358 qfn->num_file_names = lh->num_file_names;
2359 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2360 lh->num_file_names * sizeof (char *));
2361 for (i = 0; i < lh->num_file_names; ++i)
2362 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2363 qfn->real_names = NULL;
2364
2365 free_line_header (lh);
2366 do_cleanups (cleanups);
2367
2368 this_cu->v.quick->file_names = qfn;
2369 return qfn;
2370 }
2371
2372 /* A helper for the "quick" functions which computes and caches the
2373 real path for a given file name from the line table. */
2374
2375 static const char *
2376 dw2_get_real_path (struct objfile *objfile,
2377 struct quick_file_names *qfn, int index)
2378 {
2379 if (qfn->real_names == NULL)
2380 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2381 qfn->num_file_names, sizeof (char *));
2382
2383 if (qfn->real_names[index] == NULL)
2384 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2385
2386 return qfn->real_names[index];
2387 }
2388
2389 static struct symtab *
2390 dw2_find_last_source_symtab (struct objfile *objfile)
2391 {
2392 int index;
2393
2394 dw2_setup (objfile);
2395 index = dwarf2_per_objfile->n_comp_units - 1;
2396 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2397 }
2398
2399 /* Traversal function for dw2_forget_cached_source_info. */
2400
2401 static int
2402 dw2_free_cached_file_names (void **slot, void *info)
2403 {
2404 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2405
2406 if (file_data->real_names)
2407 {
2408 int i;
2409
2410 for (i = 0; i < file_data->num_file_names; ++i)
2411 {
2412 xfree ((void*) file_data->real_names[i]);
2413 file_data->real_names[i] = NULL;
2414 }
2415 }
2416
2417 return 1;
2418 }
2419
2420 static void
2421 dw2_forget_cached_source_info (struct objfile *objfile)
2422 {
2423 dw2_setup (objfile);
2424
2425 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2426 dw2_free_cached_file_names, NULL);
2427 }
2428
2429 static int
2430 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2431 const char *full_path, const char *real_path,
2432 struct symtab **result)
2433 {
2434 int i;
2435 int check_basename = lbasename (name) == name;
2436 struct dwarf2_per_cu_data *base_cu = NULL;
2437
2438 dw2_setup (objfile);
2439
2440 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2441 + dwarf2_per_objfile->n_type_comp_units); ++i)
2442 {
2443 int j;
2444 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2445 struct quick_file_names *file_data;
2446
2447 if (per_cu->v.quick->symtab)
2448 continue;
2449
2450 file_data = dw2_get_file_names (objfile, per_cu);
2451 if (file_data == NULL)
2452 continue;
2453
2454 for (j = 0; j < file_data->num_file_names; ++j)
2455 {
2456 const char *this_name = file_data->file_names[j];
2457
2458 if (FILENAME_CMP (name, this_name) == 0)
2459 {
2460 *result = dw2_instantiate_symtab (objfile, per_cu);
2461 return 1;
2462 }
2463
2464 if (check_basename && ! base_cu
2465 && FILENAME_CMP (lbasename (this_name), name) == 0)
2466 base_cu = per_cu;
2467
2468 if (full_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 (full_path, this_real_name) == 0)
2475 {
2476 *result = dw2_instantiate_symtab (objfile, per_cu);
2477 return 1;
2478 }
2479 }
2480
2481 if (real_path != NULL)
2482 {
2483 const char *this_real_name = dw2_get_real_path (objfile,
2484 file_data, j);
2485
2486 if (this_real_name != NULL
2487 && FILENAME_CMP (real_path, this_real_name) == 0)
2488 {
2489 *result = dw2_instantiate_symtab (objfile, per_cu);
2490 return 1;
2491 }
2492 }
2493 }
2494 }
2495
2496 if (base_cu)
2497 {
2498 *result = dw2_instantiate_symtab (objfile, base_cu);
2499 return 1;
2500 }
2501
2502 return 0;
2503 }
2504
2505 static struct symtab *
2506 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2507 const char *name, domain_enum domain)
2508 {
2509 /* We do all the work in the pre_expand_symtabs_matching hook
2510 instead. */
2511 return NULL;
2512 }
2513
2514 /* A helper function that expands all symtabs that hold an object
2515 named NAME. */
2516
2517 static void
2518 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2519 {
2520 dw2_setup (objfile);
2521
2522 /* index_table is NULL if OBJF_READNOW. */
2523 if (dwarf2_per_objfile->index_table)
2524 {
2525 offset_type *vec;
2526
2527 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2528 name, &vec))
2529 {
2530 offset_type i, len = MAYBE_SWAP (*vec);
2531 for (i = 0; i < len; ++i)
2532 {
2533 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2534 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2535
2536 dw2_instantiate_symtab (objfile, per_cu);
2537 }
2538 }
2539 }
2540 }
2541
2542 static void
2543 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2544 enum block_enum block_kind, const char *name,
2545 domain_enum domain)
2546 {
2547 dw2_do_expand_symtabs_matching (objfile, name);
2548 }
2549
2550 static void
2551 dw2_print_stats (struct objfile *objfile)
2552 {
2553 int i, count;
2554
2555 dw2_setup (objfile);
2556 count = 0;
2557 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2558 + dwarf2_per_objfile->n_type_comp_units); ++i)
2559 {
2560 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2561
2562 if (!per_cu->v.quick->symtab)
2563 ++count;
2564 }
2565 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2566 }
2567
2568 static void
2569 dw2_dump (struct objfile *objfile)
2570 {
2571 /* Nothing worth printing. */
2572 }
2573
2574 static void
2575 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2576 struct section_offsets *delta)
2577 {
2578 /* There's nothing to relocate here. */
2579 }
2580
2581 static void
2582 dw2_expand_symtabs_for_function (struct objfile *objfile,
2583 const char *func_name)
2584 {
2585 dw2_do_expand_symtabs_matching (objfile, func_name);
2586 }
2587
2588 static void
2589 dw2_expand_all_symtabs (struct objfile *objfile)
2590 {
2591 int i;
2592
2593 dw2_setup (objfile);
2594
2595 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2596 + dwarf2_per_objfile->n_type_comp_units); ++i)
2597 {
2598 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2599
2600 dw2_instantiate_symtab (objfile, per_cu);
2601 }
2602 }
2603
2604 static void
2605 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2606 const char *filename)
2607 {
2608 int i;
2609
2610 dw2_setup (objfile);
2611
2612 /* We don't need to consider type units here.
2613 This is only called for examining code, e.g. expand_line_sal.
2614 There can be an order of magnitude (or more) more type units
2615 than comp units, and we avoid them if we can. */
2616
2617 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2618 {
2619 int j;
2620 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2621 struct quick_file_names *file_data;
2622
2623 if (per_cu->v.quick->symtab)
2624 continue;
2625
2626 file_data = dw2_get_file_names (objfile, per_cu);
2627 if (file_data == NULL)
2628 continue;
2629
2630 for (j = 0; j < file_data->num_file_names; ++j)
2631 {
2632 const char *this_name = file_data->file_names[j];
2633 if (FILENAME_CMP (this_name, filename) == 0)
2634 {
2635 dw2_instantiate_symtab (objfile, per_cu);
2636 break;
2637 }
2638 }
2639 }
2640 }
2641
2642 static const char *
2643 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2644 {
2645 struct dwarf2_per_cu_data *per_cu;
2646 offset_type *vec;
2647 struct quick_file_names *file_data;
2648
2649 dw2_setup (objfile);
2650
2651 /* index_table is NULL if OBJF_READNOW. */
2652 if (!dwarf2_per_objfile->index_table)
2653 return NULL;
2654
2655 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2656 name, &vec))
2657 return NULL;
2658
2659 /* Note that this just looks at the very first one named NAME -- but
2660 actually we are looking for a function. find_main_filename
2661 should be rewritten so that it doesn't require a custom hook. It
2662 could just use the ordinary symbol tables. */
2663 /* vec[0] is the length, which must always be >0. */
2664 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2665
2666 file_data = dw2_get_file_names (objfile, per_cu);
2667 if (file_data == NULL)
2668 return NULL;
2669
2670 return file_data->file_names[file_data->num_file_names - 1];
2671 }
2672
2673 static void
2674 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2675 struct objfile *objfile, int global,
2676 int (*callback) (struct block *,
2677 struct symbol *, void *),
2678 void *data, symbol_compare_ftype *match,
2679 symbol_compare_ftype *ordered_compare)
2680 {
2681 /* Currently unimplemented; used for Ada. The function can be called if the
2682 current language is Ada for a non-Ada objfile using GNU index. As Ada
2683 does not look for non-Ada symbols this function should just return. */
2684 }
2685
2686 static void
2687 dw2_expand_symtabs_matching (struct objfile *objfile,
2688 int (*file_matcher) (const char *, void *),
2689 int (*name_matcher) (const char *, void *),
2690 enum search_domain kind,
2691 void *data)
2692 {
2693 int i;
2694 offset_type iter;
2695 struct mapped_index *index;
2696
2697 dw2_setup (objfile);
2698
2699 /* index_table is NULL if OBJF_READNOW. */
2700 if (!dwarf2_per_objfile->index_table)
2701 return;
2702 index = dwarf2_per_objfile->index_table;
2703
2704 if (file_matcher != NULL)
2705 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2706 + dwarf2_per_objfile->n_type_comp_units); ++i)
2707 {
2708 int j;
2709 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2710 struct quick_file_names *file_data;
2711
2712 per_cu->v.quick->mark = 0;
2713 if (per_cu->v.quick->symtab)
2714 continue;
2715
2716 file_data = dw2_get_file_names (objfile, per_cu);
2717 if (file_data == NULL)
2718 continue;
2719
2720 for (j = 0; j < file_data->num_file_names; ++j)
2721 {
2722 if (file_matcher (file_data->file_names[j], data))
2723 {
2724 per_cu->v.quick->mark = 1;
2725 break;
2726 }
2727 }
2728 }
2729
2730 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2731 {
2732 offset_type idx = 2 * iter;
2733 const char *name;
2734 offset_type *vec, vec_len, vec_idx;
2735
2736 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2737 continue;
2738
2739 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2740
2741 if (! (*name_matcher) (name, data))
2742 continue;
2743
2744 /* The name was matched, now expand corresponding CUs that were
2745 marked. */
2746 vec = (offset_type *) (index->constant_pool
2747 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2748 vec_len = MAYBE_SWAP (vec[0]);
2749 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2750 {
2751 struct dwarf2_per_cu_data *per_cu;
2752
2753 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2754 if (file_matcher == NULL || per_cu->v.quick->mark)
2755 dw2_instantiate_symtab (objfile, per_cu);
2756 }
2757 }
2758 }
2759
2760 static struct symtab *
2761 dw2_find_pc_sect_symtab (struct objfile *objfile,
2762 struct minimal_symbol *msymbol,
2763 CORE_ADDR pc,
2764 struct obj_section *section,
2765 int warn_if_readin)
2766 {
2767 struct dwarf2_per_cu_data *data;
2768
2769 dw2_setup (objfile);
2770
2771 if (!objfile->psymtabs_addrmap)
2772 return NULL;
2773
2774 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2775 if (!data)
2776 return NULL;
2777
2778 if (warn_if_readin && data->v.quick->symtab)
2779 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2780 paddress (get_objfile_arch (objfile), pc));
2781
2782 return dw2_instantiate_symtab (objfile, data);
2783 }
2784
2785 static void
2786 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2787 void *data)
2788 {
2789 int i;
2790
2791 dw2_setup (objfile);
2792
2793 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2794 + dwarf2_per_objfile->n_type_comp_units); ++i)
2795 {
2796 int j;
2797 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2798 struct quick_file_names *file_data;
2799
2800 if (per_cu->v.quick->symtab)
2801 continue;
2802
2803 file_data = dw2_get_file_names (objfile, per_cu);
2804 if (file_data == NULL)
2805 continue;
2806
2807 for (j = 0; j < file_data->num_file_names; ++j)
2808 {
2809 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2810 j);
2811 (*fun) (file_data->file_names[j], this_real_name, data);
2812 }
2813 }
2814 }
2815
2816 static int
2817 dw2_has_symbols (struct objfile *objfile)
2818 {
2819 return 1;
2820 }
2821
2822 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2823 {
2824 dw2_has_symbols,
2825 dw2_find_last_source_symtab,
2826 dw2_forget_cached_source_info,
2827 dw2_lookup_symtab,
2828 dw2_lookup_symbol,
2829 dw2_pre_expand_symtabs_matching,
2830 dw2_print_stats,
2831 dw2_dump,
2832 dw2_relocate,
2833 dw2_expand_symtabs_for_function,
2834 dw2_expand_all_symtabs,
2835 dw2_expand_symtabs_with_filename,
2836 dw2_find_symbol_file,
2837 dw2_map_matching_symbols,
2838 dw2_expand_symtabs_matching,
2839 dw2_find_pc_sect_symtab,
2840 dw2_map_symbol_filenames
2841 };
2842
2843 /* Initialize for reading DWARF for this objfile. Return 0 if this
2844 file will use psymtabs, or 1 if using the GNU index. */
2845
2846 int
2847 dwarf2_initialize_objfile (struct objfile *objfile)
2848 {
2849 /* If we're about to read full symbols, don't bother with the
2850 indices. In this case we also don't care if some other debug
2851 format is making psymtabs, because they are all about to be
2852 expanded anyway. */
2853 if ((objfile->flags & OBJF_READNOW))
2854 {
2855 int i;
2856
2857 dwarf2_per_objfile->using_index = 1;
2858 create_all_comp_units (objfile);
2859 create_debug_types_hash_table (objfile);
2860 dwarf2_per_objfile->quick_file_names_table =
2861 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2862
2863 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2864 + dwarf2_per_objfile->n_type_comp_units); ++i)
2865 {
2866 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2867
2868 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2869 struct dwarf2_per_cu_quick_data);
2870 }
2871
2872 /* Return 1 so that gdb sees the "quick" functions. However,
2873 these functions will be no-ops because we will have expanded
2874 all symtabs. */
2875 return 1;
2876 }
2877
2878 if (dwarf2_read_index (objfile))
2879 return 1;
2880
2881 return 0;
2882 }
2883
2884 \f
2885
2886 /* Build a partial symbol table. */
2887
2888 void
2889 dwarf2_build_psymtabs (struct objfile *objfile)
2890 {
2891 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2892 {
2893 init_psymbol_list (objfile, 1024);
2894 }
2895
2896 dwarf2_build_psymtabs_hard (objfile);
2897 }
2898
2899 /* Return TRUE if OFFSET is within CU_HEADER. */
2900
2901 static inline int
2902 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2903 {
2904 unsigned int bottom = cu_header->offset;
2905 unsigned int top = (cu_header->offset
2906 + cu_header->length
2907 + cu_header->initial_length_size);
2908
2909 return (offset >= bottom && offset < top);
2910 }
2911
2912 /* Read in the comp unit header information from the debug_info at info_ptr.
2913 NOTE: This leaves members offset, first_die_offset to be filled in
2914 by the caller. */
2915
2916 static gdb_byte *
2917 read_comp_unit_head (struct comp_unit_head *cu_header,
2918 gdb_byte *info_ptr, bfd *abfd)
2919 {
2920 int signed_addr;
2921 unsigned int bytes_read;
2922
2923 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2924 cu_header->initial_length_size = bytes_read;
2925 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2926 info_ptr += bytes_read;
2927 cu_header->version = read_2_bytes (abfd, info_ptr);
2928 info_ptr += 2;
2929 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2930 &bytes_read);
2931 info_ptr += bytes_read;
2932 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2933 info_ptr += 1;
2934 signed_addr = bfd_get_sign_extend_vma (abfd);
2935 if (signed_addr < 0)
2936 internal_error (__FILE__, __LINE__,
2937 _("read_comp_unit_head: dwarf from non elf file"));
2938 cu_header->signed_addr_p = signed_addr;
2939
2940 return info_ptr;
2941 }
2942
2943 static gdb_byte *
2944 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2945 gdb_byte *buffer, unsigned int buffer_size,
2946 bfd *abfd)
2947 {
2948 gdb_byte *beg_of_comp_unit = info_ptr;
2949
2950 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2951
2952 if (header->version != 2 && header->version != 3 && header->version != 4)
2953 error (_("Dwarf Error: wrong version in compilation unit header "
2954 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2955 bfd_get_filename (abfd));
2956
2957 if (header->abbrev_offset
2958 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2959 &dwarf2_per_objfile->abbrev))
2960 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2961 "(offset 0x%lx + 6) [in module %s]"),
2962 (long) header->abbrev_offset,
2963 (long) (beg_of_comp_unit - buffer),
2964 bfd_get_filename (abfd));
2965
2966 if (beg_of_comp_unit + header->length + header->initial_length_size
2967 > buffer + buffer_size)
2968 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2969 "(offset 0x%lx + 0) [in module %s]"),
2970 (long) header->length,
2971 (long) (beg_of_comp_unit - buffer),
2972 bfd_get_filename (abfd));
2973
2974 return info_ptr;
2975 }
2976
2977 /* Read in the types comp unit header information from .debug_types entry at
2978 types_ptr. The result is a pointer to one past the end of the header. */
2979
2980 static gdb_byte *
2981 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2982 struct dwarf2_section_info *section,
2983 ULONGEST *signature,
2984 gdb_byte *types_ptr, bfd *abfd)
2985 {
2986 gdb_byte *initial_types_ptr = types_ptr;
2987
2988 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
2989 cu_header->offset = types_ptr - section->buffer;
2990
2991 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2992
2993 *signature = read_8_bytes (abfd, types_ptr);
2994 types_ptr += 8;
2995 types_ptr += cu_header->offset_size;
2996 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2997
2998 return types_ptr;
2999 }
3000
3001 /* Allocate a new partial symtab for file named NAME and mark this new
3002 partial symtab as being an include of PST. */
3003
3004 static void
3005 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3006 struct objfile *objfile)
3007 {
3008 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3009
3010 subpst->section_offsets = pst->section_offsets;
3011 subpst->textlow = 0;
3012 subpst->texthigh = 0;
3013
3014 subpst->dependencies = (struct partial_symtab **)
3015 obstack_alloc (&objfile->objfile_obstack,
3016 sizeof (struct partial_symtab *));
3017 subpst->dependencies[0] = pst;
3018 subpst->number_of_dependencies = 1;
3019
3020 subpst->globals_offset = 0;
3021 subpst->n_global_syms = 0;
3022 subpst->statics_offset = 0;
3023 subpst->n_static_syms = 0;
3024 subpst->symtab = NULL;
3025 subpst->read_symtab = pst->read_symtab;
3026 subpst->readin = 0;
3027
3028 /* No private part is necessary for include psymtabs. This property
3029 can be used to differentiate between such include psymtabs and
3030 the regular ones. */
3031 subpst->read_symtab_private = NULL;
3032 }
3033
3034 /* Read the Line Number Program data and extract the list of files
3035 included by the source file represented by PST. Build an include
3036 partial symtab for each of these included files. */
3037
3038 static void
3039 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3040 struct die_info *die,
3041 struct partial_symtab *pst)
3042 {
3043 struct objfile *objfile = cu->objfile;
3044 bfd *abfd = objfile->obfd;
3045 struct line_header *lh = NULL;
3046 struct attribute *attr;
3047
3048 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3049 if (attr)
3050 {
3051 unsigned int line_offset = DW_UNSND (attr);
3052
3053 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3054 }
3055 if (lh == NULL)
3056 return; /* No linetable, so no includes. */
3057
3058 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3059 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3060
3061 free_line_header (lh);
3062 }
3063
3064 static hashval_t
3065 hash_type_signature (const void *item)
3066 {
3067 const struct signatured_type *type_sig = item;
3068
3069 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3070 return type_sig->signature;
3071 }
3072
3073 static int
3074 eq_type_signature (const void *item_lhs, const void *item_rhs)
3075 {
3076 const struct signatured_type *lhs = item_lhs;
3077 const struct signatured_type *rhs = item_rhs;
3078
3079 return lhs->signature == rhs->signature;
3080 }
3081
3082 /* Allocate a hash table for signatured types. */
3083
3084 static htab_t
3085 allocate_signatured_type_table (struct objfile *objfile)
3086 {
3087 return htab_create_alloc_ex (41,
3088 hash_type_signature,
3089 eq_type_signature,
3090 NULL,
3091 &objfile->objfile_obstack,
3092 hashtab_obstack_allocate,
3093 dummy_obstack_deallocate);
3094 }
3095
3096 /* A helper function to add a signatured type CU to a list. */
3097
3098 static int
3099 add_signatured_type_cu_to_list (void **slot, void *datum)
3100 {
3101 struct signatured_type *sigt = *slot;
3102 struct dwarf2_per_cu_data ***datap = datum;
3103
3104 **datap = &sigt->per_cu;
3105 ++*datap;
3106
3107 return 1;
3108 }
3109
3110 /* Create the hash table of all entries in the .debug_types section.
3111 The result is zero if there is an error (e.g. missing .debug_types section),
3112 otherwise non-zero. */
3113
3114 static int
3115 create_debug_types_hash_table (struct objfile *objfile)
3116 {
3117 htab_t types_htab = NULL;
3118 struct dwarf2_per_cu_data **iter;
3119 int ix;
3120 struct dwarf2_section_info *section;
3121
3122 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3123 {
3124 dwarf2_per_objfile->signatured_types = NULL;
3125 return 0;
3126 }
3127
3128 for (ix = 0;
3129 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3130 ix, section);
3131 ++ix)
3132 {
3133 gdb_byte *info_ptr, *end_ptr;
3134
3135 dwarf2_read_section (objfile, section);
3136 info_ptr = section->buffer;
3137
3138 if (info_ptr == NULL)
3139 continue;
3140
3141 if (types_htab == NULL)
3142 types_htab = allocate_signatured_type_table (objfile);
3143
3144 if (dwarf2_die_debug)
3145 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3146
3147 end_ptr = info_ptr + section->size;
3148 while (info_ptr < end_ptr)
3149 {
3150 unsigned int offset;
3151 unsigned int offset_size;
3152 unsigned int type_offset;
3153 unsigned int length, initial_length_size;
3154 unsigned short version;
3155 ULONGEST signature;
3156 struct signatured_type *type_sig;
3157 void **slot;
3158 gdb_byte *ptr = info_ptr;
3159
3160 offset = ptr - section->buffer;
3161
3162 /* We need to read the type's signature in order to build the hash
3163 table, but we don't need to read anything else just yet. */
3164
3165 /* Sanity check to ensure entire cu is present. */
3166 length = read_initial_length (objfile->obfd, ptr,
3167 &initial_length_size);
3168 if (ptr + length + initial_length_size > end_ptr)
3169 {
3170 complaint (&symfile_complaints,
3171 _("debug type entry runs off end "
3172 "of `.debug_types' section, ignored"));
3173 break;
3174 }
3175
3176 offset_size = initial_length_size == 4 ? 4 : 8;
3177 ptr += initial_length_size;
3178 version = bfd_get_16 (objfile->obfd, ptr);
3179 ptr += 2;
3180 ptr += offset_size; /* abbrev offset */
3181 ptr += 1; /* address size */
3182 signature = bfd_get_64 (objfile->obfd, ptr);
3183 ptr += 8;
3184 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3185
3186 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3187 memset (type_sig, 0, sizeof (*type_sig));
3188 type_sig->signature = signature;
3189 type_sig->type_offset = type_offset;
3190 type_sig->per_cu.objfile = objfile;
3191 type_sig->per_cu.debug_type_section = section;
3192 type_sig->per_cu.offset = offset;
3193
3194 slot = htab_find_slot (types_htab, type_sig, INSERT);
3195 gdb_assert (slot != NULL);
3196 if (*slot != NULL)
3197 {
3198 const struct signatured_type *dup_sig = *slot;
3199
3200 complaint (&symfile_complaints,
3201 _("debug type entry at offset 0x%x is duplicate to the "
3202 "entry at offset 0x%x, signature 0x%s"),
3203 offset, dup_sig->per_cu.offset,
3204 phex (signature, sizeof (signature)));
3205 gdb_assert (signature == dup_sig->signature);
3206 }
3207 *slot = type_sig;
3208
3209 if (dwarf2_die_debug)
3210 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3211 offset, phex (signature, sizeof (signature)));
3212
3213 info_ptr = info_ptr + initial_length_size + length;
3214 }
3215 }
3216
3217 dwarf2_per_objfile->signatured_types = types_htab;
3218
3219 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3220 dwarf2_per_objfile->type_comp_units
3221 = obstack_alloc (&objfile->objfile_obstack,
3222 dwarf2_per_objfile->n_type_comp_units
3223 * sizeof (struct dwarf2_per_cu_data *));
3224 iter = &dwarf2_per_objfile->type_comp_units[0];
3225 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3226 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3227 == dwarf2_per_objfile->n_type_comp_units);
3228
3229 return 1;
3230 }
3231
3232 /* Lookup a signature based type.
3233 Returns NULL if SIG is not present in the table. */
3234
3235 static struct signatured_type *
3236 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3237 {
3238 struct signatured_type find_entry, *entry;
3239
3240 if (dwarf2_per_objfile->signatured_types == NULL)
3241 {
3242 complaint (&symfile_complaints,
3243 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3244 return 0;
3245 }
3246
3247 find_entry.signature = sig;
3248 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3249 return entry;
3250 }
3251
3252 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3253
3254 static void
3255 init_cu_die_reader (struct die_reader_specs *reader,
3256 struct dwarf2_cu *cu)
3257 {
3258 reader->abfd = cu->objfile->obfd;
3259 reader->cu = cu;
3260 if (cu->per_cu->debug_type_section)
3261 {
3262 gdb_assert (cu->per_cu->debug_type_section->readin);
3263 reader->buffer = cu->per_cu->debug_type_section->buffer;
3264 }
3265 else
3266 {
3267 gdb_assert (dwarf2_per_objfile->info.readin);
3268 reader->buffer = dwarf2_per_objfile->info.buffer;
3269 }
3270 }
3271
3272 /* Find the base address of the compilation unit for range lists and
3273 location lists. It will normally be specified by DW_AT_low_pc.
3274 In DWARF-3 draft 4, the base address could be overridden by
3275 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3276 compilation units with discontinuous ranges. */
3277
3278 static void
3279 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3280 {
3281 struct attribute *attr;
3282
3283 cu->base_known = 0;
3284 cu->base_address = 0;
3285
3286 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3287 if (attr)
3288 {
3289 cu->base_address = DW_ADDR (attr);
3290 cu->base_known = 1;
3291 }
3292 else
3293 {
3294 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3295 if (attr)
3296 {
3297 cu->base_address = DW_ADDR (attr);
3298 cu->base_known = 1;
3299 }
3300 }
3301 }
3302
3303 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3304 to combine the common parts.
3305 Process a compilation unit for a psymtab.
3306 BUFFER is a pointer to the beginning of the dwarf section buffer,
3307 either .debug_info or debug_types.
3308 INFO_PTR is a pointer to the start of the CU.
3309 Returns a pointer to the next CU. */
3310
3311 static gdb_byte *
3312 process_psymtab_comp_unit (struct objfile *objfile,
3313 struct dwarf2_per_cu_data *this_cu,
3314 gdb_byte *buffer, gdb_byte *info_ptr,
3315 unsigned int buffer_size)
3316 {
3317 bfd *abfd = objfile->obfd;
3318 gdb_byte *beg_of_comp_unit = info_ptr;
3319 struct die_info *comp_unit_die;
3320 struct partial_symtab *pst;
3321 CORE_ADDR baseaddr;
3322 struct cleanup *back_to_inner;
3323 struct dwarf2_cu cu;
3324 int has_children, has_pc_info;
3325 struct attribute *attr;
3326 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3327 struct die_reader_specs reader_specs;
3328 const char *filename;
3329
3330 init_one_comp_unit (&cu, objfile);
3331 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3332
3333 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3334 buffer, buffer_size,
3335 abfd);
3336
3337 /* Complete the cu_header. */
3338 cu.header.offset = beg_of_comp_unit - buffer;
3339 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3340
3341 cu.list_in_scope = &file_symbols;
3342
3343 /* If this compilation unit was already read in, free the
3344 cached copy in order to read it in again. This is
3345 necessary because we skipped some symbols when we first
3346 read in the compilation unit (see load_partial_dies).
3347 This problem could be avoided, but the benefit is
3348 unclear. */
3349 if (this_cu->cu != NULL)
3350 free_one_cached_comp_unit (this_cu->cu);
3351
3352 /* Note that this is a pointer to our stack frame, being
3353 added to a global data structure. It will be cleaned up
3354 in free_stack_comp_unit when we finish with this
3355 compilation unit. */
3356 this_cu->cu = &cu;
3357 cu.per_cu = this_cu;
3358
3359 /* Read the abbrevs for this compilation unit into a table. */
3360 dwarf2_read_abbrevs (abfd, &cu);
3361 make_cleanup (dwarf2_free_abbrev_table, &cu);
3362
3363 /* Read the compilation unit die. */
3364 if (this_cu->debug_type_section)
3365 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3366 init_cu_die_reader (&reader_specs, &cu);
3367 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3368 &has_children);
3369
3370 if (this_cu->debug_type_section)
3371 {
3372 /* LENGTH has not been set yet for type units. */
3373 gdb_assert (this_cu->offset == cu.header.offset);
3374 this_cu->length = cu.header.length + cu.header.initial_length_size;
3375 }
3376 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3377 {
3378 info_ptr = (beg_of_comp_unit + cu.header.length
3379 + cu.header.initial_length_size);
3380 do_cleanups (back_to_inner);
3381 return info_ptr;
3382 }
3383
3384 prepare_one_comp_unit (&cu, comp_unit_die);
3385
3386 /* Allocate a new partial symbol table structure. */
3387 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3388 if (attr == NULL || !DW_STRING (attr))
3389 filename = "";
3390 else
3391 filename = DW_STRING (attr);
3392 pst = start_psymtab_common (objfile, objfile->section_offsets,
3393 filename,
3394 /* TEXTLOW and TEXTHIGH are set below. */
3395 0,
3396 objfile->global_psymbols.next,
3397 objfile->static_psymbols.next);
3398
3399 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3400 if (attr != NULL)
3401 pst->dirname = DW_STRING (attr);
3402
3403 pst->read_symtab_private = this_cu;
3404
3405 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3406
3407 /* Store the function that reads in the rest of the symbol table. */
3408 pst->read_symtab = dwarf2_psymtab_to_symtab;
3409
3410 this_cu->v.psymtab = pst;
3411
3412 dwarf2_find_base_address (comp_unit_die, &cu);
3413
3414 /* Possibly set the default values of LOWPC and HIGHPC from
3415 `DW_AT_ranges'. */
3416 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3417 &best_highpc, &cu, pst);
3418 if (has_pc_info == 1 && best_lowpc < best_highpc)
3419 /* Store the contiguous range if it is not empty; it can be empty for
3420 CUs with no code. */
3421 addrmap_set_empty (objfile->psymtabs_addrmap,
3422 best_lowpc + baseaddr,
3423 best_highpc + baseaddr - 1, pst);
3424
3425 /* Check if comp unit has_children.
3426 If so, read the rest of the partial symbols from this comp unit.
3427 If not, there's no more debug_info for this comp unit. */
3428 if (has_children)
3429 {
3430 struct partial_die_info *first_die;
3431 CORE_ADDR lowpc, highpc;
3432
3433 lowpc = ((CORE_ADDR) -1);
3434 highpc = ((CORE_ADDR) 0);
3435
3436 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3437
3438 scan_partial_symbols (first_die, &lowpc, &highpc,
3439 ! has_pc_info, &cu);
3440
3441 /* If we didn't find a lowpc, set it to highpc to avoid
3442 complaints from `maint check'. */
3443 if (lowpc == ((CORE_ADDR) -1))
3444 lowpc = highpc;
3445
3446 /* If the compilation unit didn't have an explicit address range,
3447 then use the information extracted from its child dies. */
3448 if (! has_pc_info)
3449 {
3450 best_lowpc = lowpc;
3451 best_highpc = highpc;
3452 }
3453 }
3454 pst->textlow = best_lowpc + baseaddr;
3455 pst->texthigh = best_highpc + baseaddr;
3456
3457 pst->n_global_syms = objfile->global_psymbols.next -
3458 (objfile->global_psymbols.list + pst->globals_offset);
3459 pst->n_static_syms = objfile->static_psymbols.next -
3460 (objfile->static_psymbols.list + pst->statics_offset);
3461 sort_pst_symbols (pst);
3462
3463 info_ptr = (beg_of_comp_unit + cu.header.length
3464 + cu.header.initial_length_size);
3465
3466 if (this_cu->debug_type_section)
3467 {
3468 /* It's not clear we want to do anything with stmt lists here.
3469 Waiting to see what gcc ultimately does. */
3470 }
3471 else
3472 {
3473 /* Get the list of files included in the current compilation unit,
3474 and build a psymtab for each of them. */
3475 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3476 }
3477
3478 do_cleanups (back_to_inner);
3479
3480 return info_ptr;
3481 }
3482
3483 /* Traversal function for htab_traverse_noresize.
3484 Process one .debug_types comp-unit. */
3485
3486 static int
3487 process_type_comp_unit (void **slot, void *info)
3488 {
3489 struct signatured_type *entry = (struct signatured_type *) *slot;
3490 struct objfile *objfile = (struct objfile *) info;
3491 struct dwarf2_per_cu_data *this_cu;
3492
3493 this_cu = &entry->per_cu;
3494
3495 gdb_assert (this_cu->debug_type_section->readin);
3496 process_psymtab_comp_unit (objfile, this_cu,
3497 this_cu->debug_type_section->buffer,
3498 (this_cu->debug_type_section->buffer
3499 + this_cu->offset),
3500 this_cu->debug_type_section->size);
3501
3502 return 1;
3503 }
3504
3505 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3506 Build partial symbol tables for the .debug_types comp-units. */
3507
3508 static void
3509 build_type_psymtabs (struct objfile *objfile)
3510 {
3511 if (! create_debug_types_hash_table (objfile))
3512 return;
3513
3514 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3515 process_type_comp_unit, objfile);
3516 }
3517
3518 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3519
3520 static void
3521 psymtabs_addrmap_cleanup (void *o)
3522 {
3523 struct objfile *objfile = o;
3524
3525 objfile->psymtabs_addrmap = NULL;
3526 }
3527
3528 /* Build the partial symbol table by doing a quick pass through the
3529 .debug_info and .debug_abbrev sections. */
3530
3531 static void
3532 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3533 {
3534 gdb_byte *info_ptr;
3535 struct cleanup *back_to, *addrmap_cleanup;
3536 struct obstack temp_obstack;
3537
3538 dwarf2_per_objfile->reading_partial_symbols = 1;
3539
3540 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3541 info_ptr = dwarf2_per_objfile->info.buffer;
3542
3543 /* Any cached compilation units will be linked by the per-objfile
3544 read_in_chain. Make sure to free them when we're done. */
3545 back_to = make_cleanup (free_cached_comp_units, NULL);
3546
3547 build_type_psymtabs (objfile);
3548
3549 create_all_comp_units (objfile);
3550
3551 /* Create a temporary address map on a temporary obstack. We later
3552 copy this to the final obstack. */
3553 obstack_init (&temp_obstack);
3554 make_cleanup_obstack_free (&temp_obstack);
3555 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3556 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3557
3558 /* Since the objects we're extracting from .debug_info vary in
3559 length, only the individual functions to extract them (like
3560 read_comp_unit_head and load_partial_die) can really know whether
3561 the buffer is large enough to hold another complete object.
3562
3563 At the moment, they don't actually check that. If .debug_info
3564 holds just one extra byte after the last compilation unit's dies,
3565 then read_comp_unit_head will happily read off the end of the
3566 buffer. read_partial_die is similarly casual. Those functions
3567 should be fixed.
3568
3569 For this loop condition, simply checking whether there's any data
3570 left at all should be sufficient. */
3571
3572 while (info_ptr < (dwarf2_per_objfile->info.buffer
3573 + dwarf2_per_objfile->info.size))
3574 {
3575 struct dwarf2_per_cu_data *this_cu;
3576
3577 this_cu = dwarf2_find_comp_unit (info_ptr
3578 - dwarf2_per_objfile->info.buffer,
3579 objfile);
3580
3581 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3582 dwarf2_per_objfile->info.buffer,
3583 info_ptr,
3584 dwarf2_per_objfile->info.size);
3585 }
3586
3587 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3588 &objfile->objfile_obstack);
3589 discard_cleanups (addrmap_cleanup);
3590
3591 do_cleanups (back_to);
3592 }
3593
3594 /* Load the partial DIEs for a secondary CU into memory. */
3595
3596 static void
3597 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3598 struct objfile *objfile)
3599 {
3600 bfd *abfd = objfile->obfd;
3601 gdb_byte *info_ptr, *beg_of_comp_unit;
3602 struct die_info *comp_unit_die;
3603 struct dwarf2_cu *cu;
3604 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3605 int has_children;
3606 struct die_reader_specs reader_specs;
3607 int read_cu = 0;
3608
3609 gdb_assert (! this_cu->debug_type_section);
3610
3611 gdb_assert (dwarf2_per_objfile->info.readin);
3612 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3613 beg_of_comp_unit = info_ptr;
3614
3615 if (this_cu->cu == NULL)
3616 {
3617 cu = xmalloc (sizeof (*cu));
3618 init_one_comp_unit (cu, objfile);
3619
3620 read_cu = 1;
3621
3622 /* If an error occurs while loading, release our storage. */
3623 free_cu_cleanup = make_cleanup (free_one_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);
3629
3630 /* Complete the cu_header. */
3631 cu->header.offset = this_cu->offset;
3632 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3633
3634 /* Link this compilation unit into the compilation unit tree. */
3635 this_cu->cu = cu;
3636 cu->per_cu = this_cu;
3637
3638 /* Link this CU into read_in_chain. */
3639 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3640 dwarf2_per_objfile->read_in_chain = this_cu;
3641 }
3642 else
3643 {
3644 cu = this_cu->cu;
3645 info_ptr += cu->header.first_die_offset;
3646 }
3647
3648 /* Read the abbrevs for this compilation unit into a table. */
3649 gdb_assert (cu->dwarf2_abbrevs == NULL);
3650 dwarf2_read_abbrevs (abfd, cu);
3651 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3652
3653 /* Read the compilation unit die. */
3654 init_cu_die_reader (&reader_specs, cu);
3655 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3656 &has_children);
3657
3658 prepare_one_comp_unit (cu, comp_unit_die);
3659
3660 /* Check if comp unit has_children.
3661 If so, read the rest of the partial symbols from this comp unit.
3662 If not, there's no more debug_info for this comp unit. */
3663 if (has_children)
3664 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3665
3666 do_cleanups (free_abbrevs_cleanup);
3667
3668 if (read_cu)
3669 {
3670 /* We've successfully allocated this compilation unit. Let our
3671 caller clean it up when finished with it. */
3672 discard_cleanups (free_cu_cleanup);
3673 }
3674 }
3675
3676 /* Create a list of all compilation units in OBJFILE. We do this only
3677 if an inter-comp-unit reference is found; presumably if there is one,
3678 there will be many, and one will occur early in the .debug_info section.
3679 So there's no point in building this list incrementally. */
3680
3681 static void
3682 create_all_comp_units (struct objfile *objfile)
3683 {
3684 int n_allocated;
3685 int n_comp_units;
3686 struct dwarf2_per_cu_data **all_comp_units;
3687 gdb_byte *info_ptr;
3688
3689 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3690 info_ptr = dwarf2_per_objfile->info.buffer;
3691
3692 n_comp_units = 0;
3693 n_allocated = 10;
3694 all_comp_units = xmalloc (n_allocated
3695 * sizeof (struct dwarf2_per_cu_data *));
3696
3697 while (info_ptr < dwarf2_per_objfile->info.buffer
3698 + dwarf2_per_objfile->info.size)
3699 {
3700 unsigned int length, initial_length_size;
3701 struct dwarf2_per_cu_data *this_cu;
3702 unsigned int offset;
3703
3704 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3705
3706 /* Read just enough information to find out where the next
3707 compilation unit is. */
3708 length = read_initial_length (objfile->obfd, info_ptr,
3709 &initial_length_size);
3710
3711 /* Save the compilation unit for later lookup. */
3712 this_cu = obstack_alloc (&objfile->objfile_obstack,
3713 sizeof (struct dwarf2_per_cu_data));
3714 memset (this_cu, 0, sizeof (*this_cu));
3715 this_cu->offset = offset;
3716 this_cu->length = length + initial_length_size;
3717 this_cu->objfile = objfile;
3718
3719 if (n_comp_units == n_allocated)
3720 {
3721 n_allocated *= 2;
3722 all_comp_units = xrealloc (all_comp_units,
3723 n_allocated
3724 * sizeof (struct dwarf2_per_cu_data *));
3725 }
3726 all_comp_units[n_comp_units++] = this_cu;
3727
3728 info_ptr = info_ptr + this_cu->length;
3729 }
3730
3731 dwarf2_per_objfile->all_comp_units
3732 = obstack_alloc (&objfile->objfile_obstack,
3733 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3734 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3735 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3736 xfree (all_comp_units);
3737 dwarf2_per_objfile->n_comp_units = n_comp_units;
3738 }
3739
3740 /* Process all loaded DIEs for compilation unit CU, starting at
3741 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3742 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3743 DW_AT_ranges). If NEED_PC is set, then this function will set
3744 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3745 and record the covered ranges in the addrmap. */
3746
3747 static void
3748 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3749 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3750 {
3751 struct partial_die_info *pdi;
3752
3753 /* Now, march along the PDI's, descending into ones which have
3754 interesting children but skipping the children of the other ones,
3755 until we reach the end of the compilation unit. */
3756
3757 pdi = first_die;
3758
3759 while (pdi != NULL)
3760 {
3761 fixup_partial_die (pdi, cu);
3762
3763 /* Anonymous namespaces or modules have no name but have interesting
3764 children, so we need to look at them. Ditto for anonymous
3765 enums. */
3766
3767 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3768 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3769 {
3770 switch (pdi->tag)
3771 {
3772 case DW_TAG_subprogram:
3773 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3774 break;
3775 case DW_TAG_constant:
3776 case DW_TAG_variable:
3777 case DW_TAG_typedef:
3778 case DW_TAG_union_type:
3779 if (!pdi->is_declaration)
3780 {
3781 add_partial_symbol (pdi, cu);
3782 }
3783 break;
3784 case DW_TAG_class_type:
3785 case DW_TAG_interface_type:
3786 case DW_TAG_structure_type:
3787 if (!pdi->is_declaration)
3788 {
3789 add_partial_symbol (pdi, cu);
3790 }
3791 break;
3792 case DW_TAG_enumeration_type:
3793 if (!pdi->is_declaration)
3794 add_partial_enumeration (pdi, cu);
3795 break;
3796 case DW_TAG_base_type:
3797 case DW_TAG_subrange_type:
3798 /* File scope base type definitions are added to the partial
3799 symbol table. */
3800 add_partial_symbol (pdi, cu);
3801 break;
3802 case DW_TAG_namespace:
3803 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3804 break;
3805 case DW_TAG_module:
3806 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3807 break;
3808 default:
3809 break;
3810 }
3811 }
3812
3813 /* If the die has a sibling, skip to the sibling. */
3814
3815 pdi = pdi->die_sibling;
3816 }
3817 }
3818
3819 /* Functions used to compute the fully scoped name of a partial DIE.
3820
3821 Normally, this is simple. For C++, the parent DIE's fully scoped
3822 name is concatenated with "::" and the partial DIE's name. For
3823 Java, the same thing occurs except that "." is used instead of "::".
3824 Enumerators are an exception; they use the scope of their parent
3825 enumeration type, i.e. the name of the enumeration type is not
3826 prepended to the enumerator.
3827
3828 There are two complexities. One is DW_AT_specification; in this
3829 case "parent" means the parent of the target of the specification,
3830 instead of the direct parent of the DIE. The other is compilers
3831 which do not emit DW_TAG_namespace; in this case we try to guess
3832 the fully qualified name of structure types from their members'
3833 linkage names. This must be done using the DIE's children rather
3834 than the children of any DW_AT_specification target. We only need
3835 to do this for structures at the top level, i.e. if the target of
3836 any DW_AT_specification (if any; otherwise the DIE itself) does not
3837 have a parent. */
3838
3839 /* Compute the scope prefix associated with PDI's parent, in
3840 compilation unit CU. The result will be allocated on CU's
3841 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3842 field. NULL is returned if no prefix is necessary. */
3843 static char *
3844 partial_die_parent_scope (struct partial_die_info *pdi,
3845 struct dwarf2_cu *cu)
3846 {
3847 char *grandparent_scope;
3848 struct partial_die_info *parent, *real_pdi;
3849
3850 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3851 then this means the parent of the specification DIE. */
3852
3853 real_pdi = pdi;
3854 while (real_pdi->has_specification)
3855 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3856
3857 parent = real_pdi->die_parent;
3858 if (parent == NULL)
3859 return NULL;
3860
3861 if (parent->scope_set)
3862 return parent->scope;
3863
3864 fixup_partial_die (parent, cu);
3865
3866 grandparent_scope = partial_die_parent_scope (parent, cu);
3867
3868 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3869 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3870 Work around this problem here. */
3871 if (cu->language == language_cplus
3872 && parent->tag == DW_TAG_namespace
3873 && strcmp (parent->name, "::") == 0
3874 && grandparent_scope == NULL)
3875 {
3876 parent->scope = NULL;
3877 parent->scope_set = 1;
3878 return NULL;
3879 }
3880
3881 if (pdi->tag == DW_TAG_enumerator)
3882 /* Enumerators should not get the name of the enumeration as a prefix. */
3883 parent->scope = grandparent_scope;
3884 else if (parent->tag == DW_TAG_namespace
3885 || parent->tag == DW_TAG_module
3886 || parent->tag == DW_TAG_structure_type
3887 || parent->tag == DW_TAG_class_type
3888 || parent->tag == DW_TAG_interface_type
3889 || parent->tag == DW_TAG_union_type
3890 || parent->tag == DW_TAG_enumeration_type)
3891 {
3892 if (grandparent_scope == NULL)
3893 parent->scope = parent->name;
3894 else
3895 parent->scope = typename_concat (&cu->comp_unit_obstack,
3896 grandparent_scope,
3897 parent->name, 0, cu);
3898 }
3899 else
3900 {
3901 /* FIXME drow/2004-04-01: What should we be doing with
3902 function-local names? For partial symbols, we should probably be
3903 ignoring them. */
3904 complaint (&symfile_complaints,
3905 _("unhandled containing DIE tag %d for DIE at %d"),
3906 parent->tag, pdi->offset);
3907 parent->scope = grandparent_scope;
3908 }
3909
3910 parent->scope_set = 1;
3911 return parent->scope;
3912 }
3913
3914 /* Return the fully scoped name associated with PDI, from compilation unit
3915 CU. The result will be allocated with malloc. */
3916 static char *
3917 partial_die_full_name (struct partial_die_info *pdi,
3918 struct dwarf2_cu *cu)
3919 {
3920 char *parent_scope;
3921
3922 /* If this is a template instantiation, we can not work out the
3923 template arguments from partial DIEs. So, unfortunately, we have
3924 to go through the full DIEs. At least any work we do building
3925 types here will be reused if full symbols are loaded later. */
3926 if (pdi->has_template_arguments)
3927 {
3928 fixup_partial_die (pdi, cu);
3929
3930 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3931 {
3932 struct die_info *die;
3933 struct attribute attr;
3934 struct dwarf2_cu *ref_cu = cu;
3935
3936 attr.name = 0;
3937 attr.form = DW_FORM_ref_addr;
3938 attr.u.addr = pdi->offset;
3939 die = follow_die_ref (NULL, &attr, &ref_cu);
3940
3941 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3942 }
3943 }
3944
3945 parent_scope = partial_die_parent_scope (pdi, cu);
3946 if (parent_scope == NULL)
3947 return NULL;
3948 else
3949 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3950 }
3951
3952 static void
3953 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3954 {
3955 struct objfile *objfile = cu->objfile;
3956 CORE_ADDR addr = 0;
3957 char *actual_name = NULL;
3958 const struct partial_symbol *psym = NULL;
3959 CORE_ADDR baseaddr;
3960 int built_actual_name = 0;
3961
3962 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3963
3964 actual_name = partial_die_full_name (pdi, cu);
3965 if (actual_name)
3966 built_actual_name = 1;
3967
3968 if (actual_name == NULL)
3969 actual_name = pdi->name;
3970
3971 switch (pdi->tag)
3972 {
3973 case DW_TAG_subprogram:
3974 if (pdi->is_external || cu->language == language_ada)
3975 {
3976 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3977 of the global scope. But in Ada, we want to be able to access
3978 nested procedures globally. So all Ada subprograms are stored
3979 in the global scope. */
3980 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3981 mst_text, objfile); */
3982 add_psymbol_to_list (actual_name, strlen (actual_name),
3983 built_actual_name,
3984 VAR_DOMAIN, LOC_BLOCK,
3985 &objfile->global_psymbols,
3986 0, pdi->lowpc + baseaddr,
3987 cu->language, objfile);
3988 }
3989 else
3990 {
3991 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3992 mst_file_text, objfile); */
3993 add_psymbol_to_list (actual_name, strlen (actual_name),
3994 built_actual_name,
3995 VAR_DOMAIN, LOC_BLOCK,
3996 &objfile->static_psymbols,
3997 0, pdi->lowpc + baseaddr,
3998 cu->language, objfile);
3999 }
4000 break;
4001 case DW_TAG_constant:
4002 {
4003 struct psymbol_allocation_list *list;
4004
4005 if (pdi->is_external)
4006 list = &objfile->global_psymbols;
4007 else
4008 list = &objfile->static_psymbols;
4009 add_psymbol_to_list (actual_name, strlen (actual_name),
4010 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4011 list, 0, 0, cu->language, objfile);
4012 }
4013 break;
4014 case DW_TAG_variable:
4015 if (pdi->locdesc)
4016 addr = decode_locdesc (pdi->locdesc, cu);
4017
4018 if (pdi->locdesc
4019 && addr == 0
4020 && !dwarf2_per_objfile->has_section_at_zero)
4021 {
4022 /* A global or static variable may also have been stripped
4023 out by the linker if unused, in which case its address
4024 will be nullified; do not add such variables into partial
4025 symbol table then. */
4026 }
4027 else if (pdi->is_external)
4028 {
4029 /* Global Variable.
4030 Don't enter into the minimal symbol tables as there is
4031 a minimal symbol table entry from the ELF symbols already.
4032 Enter into partial symbol table if it has a location
4033 descriptor or a type.
4034 If the location descriptor is missing, new_symbol will create
4035 a LOC_UNRESOLVED symbol, the address of the variable will then
4036 be determined from the minimal symbol table whenever the variable
4037 is referenced.
4038 The address for the partial symbol table entry is not
4039 used by GDB, but it comes in handy for debugging partial symbol
4040 table building. */
4041
4042 if (pdi->locdesc || pdi->has_type)
4043 add_psymbol_to_list (actual_name, strlen (actual_name),
4044 built_actual_name,
4045 VAR_DOMAIN, LOC_STATIC,
4046 &objfile->global_psymbols,
4047 0, addr + baseaddr,
4048 cu->language, objfile);
4049 }
4050 else
4051 {
4052 /* Static Variable. Skip symbols without location descriptors. */
4053 if (pdi->locdesc == NULL)
4054 {
4055 if (built_actual_name)
4056 xfree (actual_name);
4057 return;
4058 }
4059 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4060 mst_file_data, objfile); */
4061 add_psymbol_to_list (actual_name, strlen (actual_name),
4062 built_actual_name,
4063 VAR_DOMAIN, LOC_STATIC,
4064 &objfile->static_psymbols,
4065 0, addr + baseaddr,
4066 cu->language, objfile);
4067 }
4068 break;
4069 case DW_TAG_typedef:
4070 case DW_TAG_base_type:
4071 case DW_TAG_subrange_type:
4072 add_psymbol_to_list (actual_name, strlen (actual_name),
4073 built_actual_name,
4074 VAR_DOMAIN, LOC_TYPEDEF,
4075 &objfile->static_psymbols,
4076 0, (CORE_ADDR) 0, cu->language, objfile);
4077 break;
4078 case DW_TAG_namespace:
4079 add_psymbol_to_list (actual_name, strlen (actual_name),
4080 built_actual_name,
4081 VAR_DOMAIN, LOC_TYPEDEF,
4082 &objfile->global_psymbols,
4083 0, (CORE_ADDR) 0, cu->language, objfile);
4084 break;
4085 case DW_TAG_class_type:
4086 case DW_TAG_interface_type:
4087 case DW_TAG_structure_type:
4088 case DW_TAG_union_type:
4089 case DW_TAG_enumeration_type:
4090 /* Skip external references. The DWARF standard says in the section
4091 about "Structure, Union, and Class Type Entries": "An incomplete
4092 structure, union or class type is represented by a structure,
4093 union or class entry that does not have a byte size attribute
4094 and that has a DW_AT_declaration attribute." */
4095 if (!pdi->has_byte_size && pdi->is_declaration)
4096 {
4097 if (built_actual_name)
4098 xfree (actual_name);
4099 return;
4100 }
4101
4102 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4103 static vs. global. */
4104 add_psymbol_to_list (actual_name, strlen (actual_name),
4105 built_actual_name,
4106 STRUCT_DOMAIN, LOC_TYPEDEF,
4107 (cu->language == language_cplus
4108 || cu->language == language_java)
4109 ? &objfile->global_psymbols
4110 : &objfile->static_psymbols,
4111 0, (CORE_ADDR) 0, cu->language, objfile);
4112
4113 break;
4114 case DW_TAG_enumerator:
4115 add_psymbol_to_list (actual_name, strlen (actual_name),
4116 built_actual_name,
4117 VAR_DOMAIN, LOC_CONST,
4118 (cu->language == language_cplus
4119 || cu->language == language_java)
4120 ? &objfile->global_psymbols
4121 : &objfile->static_psymbols,
4122 0, (CORE_ADDR) 0, cu->language, objfile);
4123 break;
4124 default:
4125 break;
4126 }
4127
4128 if (built_actual_name)
4129 xfree (actual_name);
4130 }
4131
4132 /* Read a partial die corresponding to a namespace; also, add a symbol
4133 corresponding to that namespace to the symbol table. NAMESPACE is
4134 the name of the enclosing namespace. */
4135
4136 static void
4137 add_partial_namespace (struct partial_die_info *pdi,
4138 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4139 int need_pc, struct dwarf2_cu *cu)
4140 {
4141 /* Add a symbol for the namespace. */
4142
4143 add_partial_symbol (pdi, cu);
4144
4145 /* Now scan partial symbols in that namespace. */
4146
4147 if (pdi->has_children)
4148 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4149 }
4150
4151 /* Read a partial die corresponding to a Fortran module. */
4152
4153 static void
4154 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4155 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4156 {
4157 /* Now scan partial symbols in that module. */
4158
4159 if (pdi->has_children)
4160 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4161 }
4162
4163 /* Read a partial die corresponding to a subprogram and create a partial
4164 symbol for that subprogram. When the CU language allows it, this
4165 routine also defines a partial symbol for each nested subprogram
4166 that this subprogram contains.
4167
4168 DIE my also be a lexical block, in which case we simply search
4169 recursively for suprograms defined inside that lexical block.
4170 Again, this is only performed when the CU language allows this
4171 type of definitions. */
4172
4173 static void
4174 add_partial_subprogram (struct partial_die_info *pdi,
4175 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4176 int need_pc, struct dwarf2_cu *cu)
4177 {
4178 if (pdi->tag == DW_TAG_subprogram)
4179 {
4180 if (pdi->has_pc_info)
4181 {
4182 if (pdi->lowpc < *lowpc)
4183 *lowpc = pdi->lowpc;
4184 if (pdi->highpc > *highpc)
4185 *highpc = pdi->highpc;
4186 if (need_pc)
4187 {
4188 CORE_ADDR baseaddr;
4189 struct objfile *objfile = cu->objfile;
4190
4191 baseaddr = ANOFFSET (objfile->section_offsets,
4192 SECT_OFF_TEXT (objfile));
4193 addrmap_set_empty (objfile->psymtabs_addrmap,
4194 pdi->lowpc + baseaddr,
4195 pdi->highpc - 1 + baseaddr,
4196 cu->per_cu->v.psymtab);
4197 }
4198 if (!pdi->is_declaration)
4199 /* Ignore subprogram DIEs that do not have a name, they are
4200 illegal. Do not emit a complaint at this point, we will
4201 do so when we convert this psymtab into a symtab. */
4202 if (pdi->name)
4203 add_partial_symbol (pdi, cu);
4204 }
4205 }
4206
4207 if (! pdi->has_children)
4208 return;
4209
4210 if (cu->language == language_ada)
4211 {
4212 pdi = pdi->die_child;
4213 while (pdi != NULL)
4214 {
4215 fixup_partial_die (pdi, cu);
4216 if (pdi->tag == DW_TAG_subprogram
4217 || pdi->tag == DW_TAG_lexical_block)
4218 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4219 pdi = pdi->die_sibling;
4220 }
4221 }
4222 }
4223
4224 /* Read a partial die corresponding to an enumeration type. */
4225
4226 static void
4227 add_partial_enumeration (struct partial_die_info *enum_pdi,
4228 struct dwarf2_cu *cu)
4229 {
4230 struct partial_die_info *pdi;
4231
4232 if (enum_pdi->name != NULL)
4233 add_partial_symbol (enum_pdi, cu);
4234
4235 pdi = enum_pdi->die_child;
4236 while (pdi)
4237 {
4238 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4239 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4240 else
4241 add_partial_symbol (pdi, cu);
4242 pdi = pdi->die_sibling;
4243 }
4244 }
4245
4246 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4247 Return the corresponding abbrev, or NULL if the number is zero (indicating
4248 an empty DIE). In either case *BYTES_READ will be set to the length of
4249 the initial number. */
4250
4251 static struct abbrev_info *
4252 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4253 struct dwarf2_cu *cu)
4254 {
4255 bfd *abfd = cu->objfile->obfd;
4256 unsigned int abbrev_number;
4257 struct abbrev_info *abbrev;
4258
4259 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4260
4261 if (abbrev_number == 0)
4262 return NULL;
4263
4264 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4265 if (!abbrev)
4266 {
4267 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4268 abbrev_number, bfd_get_filename (abfd));
4269 }
4270
4271 return abbrev;
4272 }
4273
4274 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4275 Returns a pointer to the end of a series of DIEs, terminated by an empty
4276 DIE. Any children of the skipped DIEs will also be skipped. */
4277
4278 static gdb_byte *
4279 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4280 {
4281 struct abbrev_info *abbrev;
4282 unsigned int bytes_read;
4283
4284 while (1)
4285 {
4286 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4287 if (abbrev == NULL)
4288 return info_ptr + bytes_read;
4289 else
4290 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4291 }
4292 }
4293
4294 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4295 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4296 abbrev corresponding to that skipped uleb128 should be passed in
4297 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4298 children. */
4299
4300 static gdb_byte *
4301 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4302 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4303 {
4304 unsigned int bytes_read;
4305 struct attribute attr;
4306 bfd *abfd = cu->objfile->obfd;
4307 unsigned int form, i;
4308
4309 for (i = 0; i < abbrev->num_attrs; i++)
4310 {
4311 /* The only abbrev we care about is DW_AT_sibling. */
4312 if (abbrev->attrs[i].name == DW_AT_sibling)
4313 {
4314 read_attribute (&attr, &abbrev->attrs[i],
4315 abfd, info_ptr, cu);
4316 if (attr.form == DW_FORM_ref_addr)
4317 complaint (&symfile_complaints,
4318 _("ignoring absolute DW_AT_sibling"));
4319 else
4320 return buffer + dwarf2_get_ref_die_offset (&attr);
4321 }
4322
4323 /* If it isn't DW_AT_sibling, skip this attribute. */
4324 form = abbrev->attrs[i].form;
4325 skip_attribute:
4326 switch (form)
4327 {
4328 case DW_FORM_ref_addr:
4329 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4330 and later it is offset sized. */
4331 if (cu->header.version == 2)
4332 info_ptr += cu->header.addr_size;
4333 else
4334 info_ptr += cu->header.offset_size;
4335 break;
4336 case DW_FORM_addr:
4337 info_ptr += cu->header.addr_size;
4338 break;
4339 case DW_FORM_data1:
4340 case DW_FORM_ref1:
4341 case DW_FORM_flag:
4342 info_ptr += 1;
4343 break;
4344 case DW_FORM_flag_present:
4345 break;
4346 case DW_FORM_data2:
4347 case DW_FORM_ref2:
4348 info_ptr += 2;
4349 break;
4350 case DW_FORM_data4:
4351 case DW_FORM_ref4:
4352 info_ptr += 4;
4353 break;
4354 case DW_FORM_data8:
4355 case DW_FORM_ref8:
4356 case DW_FORM_ref_sig8:
4357 info_ptr += 8;
4358 break;
4359 case DW_FORM_string:
4360 read_direct_string (abfd, info_ptr, &bytes_read);
4361 info_ptr += bytes_read;
4362 break;
4363 case DW_FORM_sec_offset:
4364 case DW_FORM_strp:
4365 info_ptr += cu->header.offset_size;
4366 break;
4367 case DW_FORM_exprloc:
4368 case DW_FORM_block:
4369 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4370 info_ptr += bytes_read;
4371 break;
4372 case DW_FORM_block1:
4373 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4374 break;
4375 case DW_FORM_block2:
4376 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4377 break;
4378 case DW_FORM_block4:
4379 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4380 break;
4381 case DW_FORM_sdata:
4382 case DW_FORM_udata:
4383 case DW_FORM_ref_udata:
4384 info_ptr = skip_leb128 (abfd, info_ptr);
4385 break;
4386 case DW_FORM_indirect:
4387 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4388 info_ptr += bytes_read;
4389 /* We need to continue parsing from here, so just go back to
4390 the top. */
4391 goto skip_attribute;
4392
4393 default:
4394 error (_("Dwarf Error: Cannot handle %s "
4395 "in DWARF reader [in module %s]"),
4396 dwarf_form_name (form),
4397 bfd_get_filename (abfd));
4398 }
4399 }
4400
4401 if (abbrev->has_children)
4402 return skip_children (buffer, info_ptr, cu);
4403 else
4404 return info_ptr;
4405 }
4406
4407 /* Locate ORIG_PDI's sibling.
4408 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4409 in BUFFER. */
4410
4411 static gdb_byte *
4412 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4413 gdb_byte *buffer, gdb_byte *info_ptr,
4414 bfd *abfd, struct dwarf2_cu *cu)
4415 {
4416 /* Do we know the sibling already? */
4417
4418 if (orig_pdi->sibling)
4419 return orig_pdi->sibling;
4420
4421 /* Are there any children to deal with? */
4422
4423 if (!orig_pdi->has_children)
4424 return info_ptr;
4425
4426 /* Skip the children the long way. */
4427
4428 return skip_children (buffer, info_ptr, cu);
4429 }
4430
4431 /* Expand this partial symbol table into a full symbol table. */
4432
4433 static void
4434 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4435 {
4436 if (pst != NULL)
4437 {
4438 if (pst->readin)
4439 {
4440 warning (_("bug: psymtab for %s is already read in."),
4441 pst->filename);
4442 }
4443 else
4444 {
4445 if (info_verbose)
4446 {
4447 printf_filtered (_("Reading in symbols for %s..."),
4448 pst->filename);
4449 gdb_flush (gdb_stdout);
4450 }
4451
4452 /* Restore our global data. */
4453 dwarf2_per_objfile = objfile_data (pst->objfile,
4454 dwarf2_objfile_data_key);
4455
4456 /* If this psymtab is constructed from a debug-only objfile, the
4457 has_section_at_zero flag will not necessarily be correct. We
4458 can get the correct value for this flag by looking at the data
4459 associated with the (presumably stripped) associated objfile. */
4460 if (pst->objfile->separate_debug_objfile_backlink)
4461 {
4462 struct dwarf2_per_objfile *dpo_backlink
4463 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4464 dwarf2_objfile_data_key);
4465
4466 dwarf2_per_objfile->has_section_at_zero
4467 = dpo_backlink->has_section_at_zero;
4468 }
4469
4470 dwarf2_per_objfile->reading_partial_symbols = 0;
4471
4472 psymtab_to_symtab_1 (pst);
4473
4474 /* Finish up the debug error message. */
4475 if (info_verbose)
4476 printf_filtered (_("done.\n"));
4477 }
4478 }
4479 }
4480
4481 /* Add PER_CU to the queue. */
4482
4483 static void
4484 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4485 {
4486 struct dwarf2_queue_item *item;
4487
4488 per_cu->queued = 1;
4489 item = xmalloc (sizeof (*item));
4490 item->per_cu = per_cu;
4491 item->next = NULL;
4492
4493 if (dwarf2_queue == NULL)
4494 dwarf2_queue = item;
4495 else
4496 dwarf2_queue_tail->next = item;
4497
4498 dwarf2_queue_tail = item;
4499 }
4500
4501 /* Process the queue. */
4502
4503 static void
4504 process_queue (struct objfile *objfile)
4505 {
4506 struct dwarf2_queue_item *item, *next_item;
4507
4508 /* The queue starts out with one item, but following a DIE reference
4509 may load a new CU, adding it to the end of the queue. */
4510 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4511 {
4512 if (dwarf2_per_objfile->using_index
4513 ? !item->per_cu->v.quick->symtab
4514 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4515 process_full_comp_unit (item->per_cu);
4516
4517 item->per_cu->queued = 0;
4518 next_item = item->next;
4519 xfree (item);
4520 }
4521
4522 dwarf2_queue_tail = NULL;
4523 }
4524
4525 /* Free all allocated queue entries. This function only releases anything if
4526 an error was thrown; if the queue was processed then it would have been
4527 freed as we went along. */
4528
4529 static void
4530 dwarf2_release_queue (void *dummy)
4531 {
4532 struct dwarf2_queue_item *item, *last;
4533
4534 item = dwarf2_queue;
4535 while (item)
4536 {
4537 /* Anything still marked queued is likely to be in an
4538 inconsistent state, so discard it. */
4539 if (item->per_cu->queued)
4540 {
4541 if (item->per_cu->cu != NULL)
4542 free_one_cached_comp_unit (item->per_cu->cu);
4543 item->per_cu->queued = 0;
4544 }
4545
4546 last = item;
4547 item = item->next;
4548 xfree (last);
4549 }
4550
4551 dwarf2_queue = dwarf2_queue_tail = NULL;
4552 }
4553
4554 /* Read in full symbols for PST, and anything it depends on. */
4555
4556 static void
4557 psymtab_to_symtab_1 (struct partial_symtab *pst)
4558 {
4559 struct dwarf2_per_cu_data *per_cu;
4560 struct cleanup *back_to;
4561 int i;
4562
4563 for (i = 0; i < pst->number_of_dependencies; i++)
4564 if (!pst->dependencies[i]->readin)
4565 {
4566 /* Inform about additional files that need to be read in. */
4567 if (info_verbose)
4568 {
4569 /* FIXME: i18n: Need to make this a single string. */
4570 fputs_filtered (" ", gdb_stdout);
4571 wrap_here ("");
4572 fputs_filtered ("and ", gdb_stdout);
4573 wrap_here ("");
4574 printf_filtered ("%s...", pst->dependencies[i]->filename);
4575 wrap_here (""); /* Flush output. */
4576 gdb_flush (gdb_stdout);
4577 }
4578 psymtab_to_symtab_1 (pst->dependencies[i]);
4579 }
4580
4581 per_cu = pst->read_symtab_private;
4582
4583 if (per_cu == NULL)
4584 {
4585 /* It's an include file, no symbols to read for it.
4586 Everything is in the parent symtab. */
4587 pst->readin = 1;
4588 return;
4589 }
4590
4591 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4592 }
4593
4594 /* Load the DIEs associated with PER_CU into memory. */
4595
4596 static void
4597 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4598 struct objfile *objfile)
4599 {
4600 bfd *abfd = objfile->obfd;
4601 struct dwarf2_cu *cu;
4602 unsigned int offset;
4603 gdb_byte *info_ptr, *beg_of_comp_unit;
4604 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4605 struct attribute *attr;
4606 int read_cu = 0;
4607
4608 gdb_assert (! per_cu->debug_type_section);
4609
4610 /* Set local variables from the partial symbol table info. */
4611 offset = per_cu->offset;
4612
4613 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4614 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4615 beg_of_comp_unit = info_ptr;
4616
4617 if (per_cu->cu == NULL)
4618 {
4619 cu = xmalloc (sizeof (*cu));
4620 init_one_comp_unit (cu, objfile);
4621
4622 read_cu = 1;
4623
4624 /* If an error occurs while loading, release our storage. */
4625 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4626
4627 /* Read in the comp_unit header. */
4628 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4629
4630 /* Complete the cu_header. */
4631 cu->header.offset = offset;
4632 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4633
4634 /* Read the abbrevs for this compilation unit. */
4635 dwarf2_read_abbrevs (abfd, cu);
4636 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4637
4638 /* Link this compilation unit into the compilation unit tree. */
4639 per_cu->cu = cu;
4640 cu->per_cu = per_cu;
4641
4642 /* Link this CU into read_in_chain. */
4643 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4644 dwarf2_per_objfile->read_in_chain = per_cu;
4645 }
4646 else
4647 {
4648 cu = per_cu->cu;
4649 info_ptr += cu->header.first_die_offset;
4650 }
4651
4652 cu->dies = read_comp_unit (info_ptr, cu);
4653
4654 /* We try not to read any attributes in this function, because not
4655 all objfiles needed for references have been loaded yet, and symbol
4656 table processing isn't initialized. But we have to set the CU language,
4657 or we won't be able to build types correctly. */
4658 prepare_one_comp_unit (cu, cu->dies);
4659
4660 /* Similarly, if we do not read the producer, we can not apply
4661 producer-specific interpretation. */
4662 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4663 if (attr)
4664 cu->producer = DW_STRING (attr);
4665
4666 if (read_cu)
4667 {
4668 do_cleanups (free_abbrevs_cleanup);
4669
4670 /* We've successfully allocated this compilation unit. Let our
4671 caller clean it up when finished with it. */
4672 discard_cleanups (free_cu_cleanup);
4673 }
4674 }
4675
4676 /* Add a DIE to the delayed physname list. */
4677
4678 static void
4679 add_to_method_list (struct type *type, int fnfield_index, int index,
4680 const char *name, struct die_info *die,
4681 struct dwarf2_cu *cu)
4682 {
4683 struct delayed_method_info mi;
4684 mi.type = type;
4685 mi.fnfield_index = fnfield_index;
4686 mi.index = index;
4687 mi.name = name;
4688 mi.die = die;
4689 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4690 }
4691
4692 /* A cleanup for freeing the delayed method list. */
4693
4694 static void
4695 free_delayed_list (void *ptr)
4696 {
4697 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4698 if (cu->method_list != NULL)
4699 {
4700 VEC_free (delayed_method_info, cu->method_list);
4701 cu->method_list = NULL;
4702 }
4703 }
4704
4705 /* Compute the physnames of any methods on the CU's method list.
4706
4707 The computation of method physnames is delayed in order to avoid the
4708 (bad) condition that one of the method's formal parameters is of an as yet
4709 incomplete type. */
4710
4711 static void
4712 compute_delayed_physnames (struct dwarf2_cu *cu)
4713 {
4714 int i;
4715 struct delayed_method_info *mi;
4716 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4717 {
4718 const char *physname;
4719 struct fn_fieldlist *fn_flp
4720 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4721 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4722 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4723 }
4724 }
4725
4726 /* Generate full symbol information for PST and CU, whose DIEs have
4727 already been loaded into memory. */
4728
4729 static void
4730 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4731 {
4732 struct dwarf2_cu *cu = per_cu->cu;
4733 struct objfile *objfile = per_cu->objfile;
4734 CORE_ADDR lowpc, highpc;
4735 struct symtab *symtab;
4736 struct cleanup *back_to, *delayed_list_cleanup;
4737 CORE_ADDR baseaddr;
4738
4739 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4740
4741 buildsym_init ();
4742 back_to = make_cleanup (really_free_pendings, NULL);
4743 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4744
4745 cu->list_in_scope = &file_symbols;
4746
4747 /* Do line number decoding in read_file_scope () */
4748 process_die (cu->dies, cu);
4749
4750 /* Now that we have processed all the DIEs in the CU, all the types
4751 should be complete, and it should now be safe to compute all of the
4752 physnames. */
4753 compute_delayed_physnames (cu);
4754 do_cleanups (delayed_list_cleanup);
4755
4756 /* Some compilers don't define a DW_AT_high_pc attribute for the
4757 compilation unit. If the DW_AT_high_pc is missing, synthesize
4758 it, by scanning the DIE's below the compilation unit. */
4759 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4760
4761 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4762
4763 if (symtab != NULL)
4764 {
4765 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4766
4767 /* Set symtab language to language from DW_AT_language. If the
4768 compilation is from a C file generated by language preprocessors, do
4769 not set the language if it was already deduced by start_subfile. */
4770 if (!(cu->language == language_c && symtab->language != language_c))
4771 symtab->language = cu->language;
4772
4773 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4774 produce DW_AT_location with location lists but it can be possibly
4775 invalid without -fvar-tracking.
4776
4777 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4778 needed, it would be wrong due to missing DW_AT_producer there.
4779
4780 Still one can confuse GDB by using non-standard GCC compilation
4781 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4782 */
4783 if (cu->has_loclist && gcc_4_minor >= 0)
4784 symtab->locations_valid = 1;
4785
4786 if (gcc_4_minor >= 5)
4787 symtab->epilogue_unwind_valid = 1;
4788 }
4789
4790 if (dwarf2_per_objfile->using_index)
4791 per_cu->v.quick->symtab = symtab;
4792 else
4793 {
4794 struct partial_symtab *pst = per_cu->v.psymtab;
4795 pst->symtab = symtab;
4796 pst->readin = 1;
4797 }
4798
4799 do_cleanups (back_to);
4800 }
4801
4802 /* Process a die and its children. */
4803
4804 static void
4805 process_die (struct die_info *die, struct dwarf2_cu *cu)
4806 {
4807 switch (die->tag)
4808 {
4809 case DW_TAG_padding:
4810 break;
4811 case DW_TAG_compile_unit:
4812 read_file_scope (die, cu);
4813 break;
4814 case DW_TAG_type_unit:
4815 read_type_unit_scope (die, cu);
4816 break;
4817 case DW_TAG_subprogram:
4818 case DW_TAG_inlined_subroutine:
4819 read_func_scope (die, cu);
4820 break;
4821 case DW_TAG_lexical_block:
4822 case DW_TAG_try_block:
4823 case DW_TAG_catch_block:
4824 read_lexical_block_scope (die, cu);
4825 break;
4826 case DW_TAG_class_type:
4827 case DW_TAG_interface_type:
4828 case DW_TAG_structure_type:
4829 case DW_TAG_union_type:
4830 process_structure_scope (die, cu);
4831 break;
4832 case DW_TAG_enumeration_type:
4833 process_enumeration_scope (die, cu);
4834 break;
4835
4836 /* These dies have a type, but processing them does not create
4837 a symbol or recurse to process the children. Therefore we can
4838 read them on-demand through read_type_die. */
4839 case DW_TAG_subroutine_type:
4840 case DW_TAG_set_type:
4841 case DW_TAG_array_type:
4842 case DW_TAG_pointer_type:
4843 case DW_TAG_ptr_to_member_type:
4844 case DW_TAG_reference_type:
4845 case DW_TAG_string_type:
4846 break;
4847
4848 case DW_TAG_base_type:
4849 case DW_TAG_subrange_type:
4850 case DW_TAG_typedef:
4851 /* Add a typedef symbol for the type definition, if it has a
4852 DW_AT_name. */
4853 new_symbol (die, read_type_die (die, cu), cu);
4854 break;
4855 case DW_TAG_common_block:
4856 read_common_block (die, cu);
4857 break;
4858 case DW_TAG_common_inclusion:
4859 break;
4860 case DW_TAG_namespace:
4861 processing_has_namespace_info = 1;
4862 read_namespace (die, cu);
4863 break;
4864 case DW_TAG_module:
4865 processing_has_namespace_info = 1;
4866 read_module (die, cu);
4867 break;
4868 case DW_TAG_imported_declaration:
4869 case DW_TAG_imported_module:
4870 processing_has_namespace_info = 1;
4871 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4872 || cu->language != language_fortran))
4873 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4874 dwarf_tag_name (die->tag));
4875 read_import_statement (die, cu);
4876 break;
4877 default:
4878 new_symbol (die, NULL, cu);
4879 break;
4880 }
4881 }
4882
4883 /* A helper function for dwarf2_compute_name which determines whether DIE
4884 needs to have the name of the scope prepended to the name listed in the
4885 die. */
4886
4887 static int
4888 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4889 {
4890 struct attribute *attr;
4891
4892 switch (die->tag)
4893 {
4894 case DW_TAG_namespace:
4895 case DW_TAG_typedef:
4896 case DW_TAG_class_type:
4897 case DW_TAG_interface_type:
4898 case DW_TAG_structure_type:
4899 case DW_TAG_union_type:
4900 case DW_TAG_enumeration_type:
4901 case DW_TAG_enumerator:
4902 case DW_TAG_subprogram:
4903 case DW_TAG_member:
4904 return 1;
4905
4906 case DW_TAG_variable:
4907 case DW_TAG_constant:
4908 /* We only need to prefix "globally" visible variables. These include
4909 any variable marked with DW_AT_external or any variable that
4910 lives in a namespace. [Variables in anonymous namespaces
4911 require prefixing, but they are not DW_AT_external.] */
4912
4913 if (dwarf2_attr (die, DW_AT_specification, cu))
4914 {
4915 struct dwarf2_cu *spec_cu = cu;
4916
4917 return die_needs_namespace (die_specification (die, &spec_cu),
4918 spec_cu);
4919 }
4920
4921 attr = dwarf2_attr (die, DW_AT_external, cu);
4922 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4923 && die->parent->tag != DW_TAG_module)
4924 return 0;
4925 /* A variable in a lexical block of some kind does not need a
4926 namespace, even though in C++ such variables may be external
4927 and have a mangled name. */
4928 if (die->parent->tag == DW_TAG_lexical_block
4929 || die->parent->tag == DW_TAG_try_block
4930 || die->parent->tag == DW_TAG_catch_block
4931 || die->parent->tag == DW_TAG_subprogram)
4932 return 0;
4933 return 1;
4934
4935 default:
4936 return 0;
4937 }
4938 }
4939
4940 /* Retrieve the last character from a mem_file. */
4941
4942 static void
4943 do_ui_file_peek_last (void *object, const char *buffer, long length)
4944 {
4945 char *last_char_p = (char *) object;
4946
4947 if (length > 0)
4948 *last_char_p = buffer[length - 1];
4949 }
4950
4951 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4952 compute the physname for the object, which include a method's
4953 formal parameters (C++/Java) and return type (Java).
4954
4955 For Ada, return the DIE's linkage name rather than the fully qualified
4956 name. PHYSNAME is ignored..
4957
4958 The result is allocated on the objfile_obstack and canonicalized. */
4959
4960 static const char *
4961 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4962 int physname)
4963 {
4964 if (name == NULL)
4965 name = dwarf2_name (die, cu);
4966
4967 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4968 compute it by typename_concat inside GDB. */
4969 if (cu->language == language_ada
4970 || (cu->language == language_fortran && physname))
4971 {
4972 /* For Ada unit, we prefer the linkage name over the name, as
4973 the former contains the exported name, which the user expects
4974 to be able to reference. Ideally, we want the user to be able
4975 to reference this entity using either natural or linkage name,
4976 but we haven't started looking at this enhancement yet. */
4977 struct attribute *attr;
4978
4979 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4980 if (attr == NULL)
4981 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4982 if (attr && DW_STRING (attr))
4983 return DW_STRING (attr);
4984 }
4985
4986 /* These are the only languages we know how to qualify names in. */
4987 if (name != NULL
4988 && (cu->language == language_cplus || cu->language == language_java
4989 || cu->language == language_fortran))
4990 {
4991 if (die_needs_namespace (die, cu))
4992 {
4993 long length;
4994 char *prefix;
4995 struct ui_file *buf;
4996
4997 prefix = determine_prefix (die, cu);
4998 buf = mem_fileopen ();
4999 if (*prefix != '\0')
5000 {
5001 char *prefixed_name = typename_concat (NULL, prefix, name,
5002 physname, cu);
5003
5004 fputs_unfiltered (prefixed_name, buf);
5005 xfree (prefixed_name);
5006 }
5007 else
5008 fputs_unfiltered (name, buf);
5009
5010 /* Template parameters may be specified in the DIE's DW_AT_name, or
5011 as children with DW_TAG_template_type_param or
5012 DW_TAG_value_type_param. If the latter, add them to the name
5013 here. If the name already has template parameters, then
5014 skip this step; some versions of GCC emit both, and
5015 it is more efficient to use the pre-computed name.
5016
5017 Something to keep in mind about this process: it is very
5018 unlikely, or in some cases downright impossible, to produce
5019 something that will match the mangled name of a function.
5020 If the definition of the function has the same debug info,
5021 we should be able to match up with it anyway. But fallbacks
5022 using the minimal symbol, for instance to find a method
5023 implemented in a stripped copy of libstdc++, will not work.
5024 If we do not have debug info for the definition, we will have to
5025 match them up some other way.
5026
5027 When we do name matching there is a related problem with function
5028 templates; two instantiated function templates are allowed to
5029 differ only by their return types, which we do not add here. */
5030
5031 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5032 {
5033 struct attribute *attr;
5034 struct die_info *child;
5035 int first = 1;
5036
5037 die->building_fullname = 1;
5038
5039 for (child = die->child; child != NULL; child = child->sibling)
5040 {
5041 struct type *type;
5042 long value;
5043 gdb_byte *bytes;
5044 struct dwarf2_locexpr_baton *baton;
5045 struct value *v;
5046
5047 if (child->tag != DW_TAG_template_type_param
5048 && child->tag != DW_TAG_template_value_param)
5049 continue;
5050
5051 if (first)
5052 {
5053 fputs_unfiltered ("<", buf);
5054 first = 0;
5055 }
5056 else
5057 fputs_unfiltered (", ", buf);
5058
5059 attr = dwarf2_attr (child, DW_AT_type, cu);
5060 if (attr == NULL)
5061 {
5062 complaint (&symfile_complaints,
5063 _("template parameter missing DW_AT_type"));
5064 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5065 continue;
5066 }
5067 type = die_type (child, cu);
5068
5069 if (child->tag == DW_TAG_template_type_param)
5070 {
5071 c_print_type (type, "", buf, -1, 0);
5072 continue;
5073 }
5074
5075 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5076 if (attr == NULL)
5077 {
5078 complaint (&symfile_complaints,
5079 _("template parameter missing "
5080 "DW_AT_const_value"));
5081 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5082 continue;
5083 }
5084
5085 dwarf2_const_value_attr (attr, type, name,
5086 &cu->comp_unit_obstack, cu,
5087 &value, &bytes, &baton);
5088
5089 if (TYPE_NOSIGN (type))
5090 /* GDB prints characters as NUMBER 'CHAR'. If that's
5091 changed, this can use value_print instead. */
5092 c_printchar (value, type, buf);
5093 else
5094 {
5095 struct value_print_options opts;
5096
5097 if (baton != NULL)
5098 v = dwarf2_evaluate_loc_desc (type, NULL,
5099 baton->data,
5100 baton->size,
5101 baton->per_cu);
5102 else if (bytes != NULL)
5103 {
5104 v = allocate_value (type);
5105 memcpy (value_contents_writeable (v), bytes,
5106 TYPE_LENGTH (type));
5107 }
5108 else
5109 v = value_from_longest (type, value);
5110
5111 /* Specify decimal so that we do not depend on
5112 the radix. */
5113 get_formatted_print_options (&opts, 'd');
5114 opts.raw = 1;
5115 value_print (v, buf, &opts);
5116 release_value (v);
5117 value_free (v);
5118 }
5119 }
5120
5121 die->building_fullname = 0;
5122
5123 if (!first)
5124 {
5125 /* Close the argument list, with a space if necessary
5126 (nested templates). */
5127 char last_char = '\0';
5128 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5129 if (last_char == '>')
5130 fputs_unfiltered (" >", buf);
5131 else
5132 fputs_unfiltered (">", buf);
5133 }
5134 }
5135
5136 /* For Java and C++ methods, append formal parameter type
5137 information, if PHYSNAME. */
5138
5139 if (physname && die->tag == DW_TAG_subprogram
5140 && (cu->language == language_cplus
5141 || cu->language == language_java))
5142 {
5143 struct type *type = read_type_die (die, cu);
5144
5145 c_type_print_args (type, buf, 1, cu->language);
5146
5147 if (cu->language == language_java)
5148 {
5149 /* For java, we must append the return type to method
5150 names. */
5151 if (die->tag == DW_TAG_subprogram)
5152 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5153 0, 0);
5154 }
5155 else if (cu->language == language_cplus)
5156 {
5157 /* Assume that an artificial first parameter is
5158 "this", but do not crash if it is not. RealView
5159 marks unnamed (and thus unused) parameters as
5160 artificial; there is no way to differentiate
5161 the two cases. */
5162 if (TYPE_NFIELDS (type) > 0
5163 && TYPE_FIELD_ARTIFICIAL (type, 0)
5164 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5165 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5166 0))))
5167 fputs_unfiltered (" const", buf);
5168 }
5169 }
5170
5171 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5172 &length);
5173 ui_file_delete (buf);
5174
5175 if (cu->language == language_cplus)
5176 {
5177 char *cname
5178 = dwarf2_canonicalize_name (name, cu,
5179 &cu->objfile->objfile_obstack);
5180
5181 if (cname != NULL)
5182 name = cname;
5183 }
5184 }
5185 }
5186
5187 return name;
5188 }
5189
5190 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5191 If scope qualifiers are appropriate they will be added. The result
5192 will be allocated on the objfile_obstack, or NULL if the DIE does
5193 not have a name. NAME may either be from a previous call to
5194 dwarf2_name or NULL.
5195
5196 The output string will be canonicalized (if C++/Java). */
5197
5198 static const char *
5199 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5200 {
5201 return dwarf2_compute_name (name, die, cu, 0);
5202 }
5203
5204 /* Construct a physname for the given DIE in CU. NAME may either be
5205 from a previous call to dwarf2_name or NULL. The result will be
5206 allocated on the objfile_objstack or NULL if the DIE does not have a
5207 name.
5208
5209 The output string will be canonicalized (if C++/Java). */
5210
5211 static const char *
5212 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5213 {
5214 struct attribute *attr;
5215 const char *retval, *mangled = NULL, *canon = NULL;
5216 struct cleanup *back_to;
5217 int need_copy = 1;
5218
5219 /* In this case dwarf2_compute_name is just a shortcut not building anything
5220 on its own. */
5221 if (!die_needs_namespace (die, cu))
5222 return dwarf2_compute_name (name, die, cu, 1);
5223
5224 back_to = make_cleanup (null_cleanup, NULL);
5225
5226 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5227 if (!attr)
5228 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5229
5230 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5231 has computed. */
5232 if (attr && DW_STRING (attr))
5233 {
5234 char *demangled;
5235
5236 mangled = DW_STRING (attr);
5237
5238 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5239 type. It is easier for GDB users to search for such functions as
5240 `name(params)' than `long name(params)'. In such case the minimal
5241 symbol names do not match the full symbol names but for template
5242 functions there is never a need to look up their definition from their
5243 declaration so the only disadvantage remains the minimal symbol
5244 variant `long name(params)' does not have the proper inferior type.
5245 */
5246
5247 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5248 | (cu->language == language_java
5249 ? DMGL_JAVA | DMGL_RET_POSTFIX
5250 : DMGL_RET_DROP)));
5251 if (demangled)
5252 {
5253 make_cleanup (xfree, demangled);
5254 canon = demangled;
5255 }
5256 else
5257 {
5258 canon = mangled;
5259 need_copy = 0;
5260 }
5261 }
5262
5263 if (canon == NULL || check_physname)
5264 {
5265 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5266
5267 if (canon != NULL && strcmp (physname, canon) != 0)
5268 {
5269 /* It may not mean a bug in GDB. The compiler could also
5270 compute DW_AT_linkage_name incorrectly. But in such case
5271 GDB would need to be bug-to-bug compatible. */
5272
5273 complaint (&symfile_complaints,
5274 _("Computed physname <%s> does not match demangled <%s> "
5275 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5276 physname, canon, mangled, die->offset, cu->objfile->name);
5277
5278 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5279 is available here - over computed PHYSNAME. It is safer
5280 against both buggy GDB and buggy compilers. */
5281
5282 retval = canon;
5283 }
5284 else
5285 {
5286 retval = physname;
5287 need_copy = 0;
5288 }
5289 }
5290 else
5291 retval = canon;
5292
5293 if (need_copy)
5294 retval = obsavestring (retval, strlen (retval),
5295 &cu->objfile->objfile_obstack);
5296
5297 do_cleanups (back_to);
5298 return retval;
5299 }
5300
5301 /* Read the import statement specified by the given die and record it. */
5302
5303 static void
5304 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5305 {
5306 struct attribute *import_attr;
5307 struct die_info *imported_die, *child_die;
5308 struct dwarf2_cu *imported_cu;
5309 const char *imported_name;
5310 const char *imported_name_prefix;
5311 const char *canonical_name;
5312 const char *import_alias;
5313 const char *imported_declaration = NULL;
5314 const char *import_prefix;
5315 VEC (const_char_ptr) *excludes = NULL;
5316 struct cleanup *cleanups;
5317
5318 char *temp;
5319
5320 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5321 if (import_attr == NULL)
5322 {
5323 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5324 dwarf_tag_name (die->tag));
5325 return;
5326 }
5327
5328 imported_cu = cu;
5329 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5330 imported_name = dwarf2_name (imported_die, imported_cu);
5331 if (imported_name == NULL)
5332 {
5333 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5334
5335 The import in the following code:
5336 namespace A
5337 {
5338 typedef int B;
5339 }
5340
5341 int main ()
5342 {
5343 using A::B;
5344 B b;
5345 return b;
5346 }
5347
5348 ...
5349 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5350 <52> DW_AT_decl_file : 1
5351 <53> DW_AT_decl_line : 6
5352 <54> DW_AT_import : <0x75>
5353 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5354 <59> DW_AT_name : B
5355 <5b> DW_AT_decl_file : 1
5356 <5c> DW_AT_decl_line : 2
5357 <5d> DW_AT_type : <0x6e>
5358 ...
5359 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5360 <76> DW_AT_byte_size : 4
5361 <77> DW_AT_encoding : 5 (signed)
5362
5363 imports the wrong die ( 0x75 instead of 0x58 ).
5364 This case will be ignored until the gcc bug is fixed. */
5365 return;
5366 }
5367
5368 /* Figure out the local name after import. */
5369 import_alias = dwarf2_name (die, cu);
5370
5371 /* Figure out where the statement is being imported to. */
5372 import_prefix = determine_prefix (die, cu);
5373
5374 /* Figure out what the scope of the imported die is and prepend it
5375 to the name of the imported die. */
5376 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5377
5378 if (imported_die->tag != DW_TAG_namespace
5379 && imported_die->tag != DW_TAG_module)
5380 {
5381 imported_declaration = imported_name;
5382 canonical_name = imported_name_prefix;
5383 }
5384 else if (strlen (imported_name_prefix) > 0)
5385 {
5386 temp = alloca (strlen (imported_name_prefix)
5387 + 2 + strlen (imported_name) + 1);
5388 strcpy (temp, imported_name_prefix);
5389 strcat (temp, "::");
5390 strcat (temp, imported_name);
5391 canonical_name = temp;
5392 }
5393 else
5394 canonical_name = imported_name;
5395
5396 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5397
5398 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5399 for (child_die = die->child; child_die && child_die->tag;
5400 child_die = sibling_die (child_die))
5401 {
5402 /* DWARF-4: A Fortran use statement with a “rename list” may be
5403 represented by an imported module entry with an import attribute
5404 referring to the module and owned entries corresponding to those
5405 entities that are renamed as part of being imported. */
5406
5407 if (child_die->tag != DW_TAG_imported_declaration)
5408 {
5409 complaint (&symfile_complaints,
5410 _("child DW_TAG_imported_declaration expected "
5411 "- DIE at 0x%x [in module %s]"),
5412 child_die->offset, cu->objfile->name);
5413 continue;
5414 }
5415
5416 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5417 if (import_attr == NULL)
5418 {
5419 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5420 dwarf_tag_name (child_die->tag));
5421 continue;
5422 }
5423
5424 imported_cu = cu;
5425 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5426 &imported_cu);
5427 imported_name = dwarf2_name (imported_die, imported_cu);
5428 if (imported_name == NULL)
5429 {
5430 complaint (&symfile_complaints,
5431 _("child DW_TAG_imported_declaration has unknown "
5432 "imported name - DIE at 0x%x [in module %s]"),
5433 child_die->offset, cu->objfile->name);
5434 continue;
5435 }
5436
5437 VEC_safe_push (const_char_ptr, excludes, imported_name);
5438
5439 process_die (child_die, cu);
5440 }
5441
5442 cp_add_using_directive (import_prefix,
5443 canonical_name,
5444 import_alias,
5445 imported_declaration,
5446 excludes,
5447 &cu->objfile->objfile_obstack);
5448
5449 do_cleanups (cleanups);
5450 }
5451
5452 static void
5453 initialize_cu_func_list (struct dwarf2_cu *cu)
5454 {
5455 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5456 }
5457
5458 /* Cleanup function for read_file_scope. */
5459
5460 static void
5461 free_cu_line_header (void *arg)
5462 {
5463 struct dwarf2_cu *cu = arg;
5464
5465 free_line_header (cu->line_header);
5466 cu->line_header = NULL;
5467 }
5468
5469 static void
5470 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5471 char **name, char **comp_dir)
5472 {
5473 struct attribute *attr;
5474
5475 *name = NULL;
5476 *comp_dir = NULL;
5477
5478 /* Find the filename. Do not use dwarf2_name here, since the filename
5479 is not a source language identifier. */
5480 attr = dwarf2_attr (die, DW_AT_name, cu);
5481 if (attr)
5482 {
5483 *name = DW_STRING (attr);
5484 }
5485
5486 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5487 if (attr)
5488 *comp_dir = DW_STRING (attr);
5489 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5490 {
5491 *comp_dir = ldirname (*name);
5492 if (*comp_dir != NULL)
5493 make_cleanup (xfree, *comp_dir);
5494 }
5495 if (*comp_dir != NULL)
5496 {
5497 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5498 directory, get rid of it. */
5499 char *cp = strchr (*comp_dir, ':');
5500
5501 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5502 *comp_dir = cp + 1;
5503 }
5504
5505 if (*name == NULL)
5506 *name = "<unknown>";
5507 }
5508
5509 /* Handle DW_AT_stmt_list for a compilation unit. */
5510
5511 static void
5512 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5513 const char *comp_dir)
5514 {
5515 struct attribute *attr;
5516 struct objfile *objfile = cu->objfile;
5517 bfd *abfd = objfile->obfd;
5518
5519 /* Decode line number information if present. We do this before
5520 processing child DIEs, so that the line header table is available
5521 for DW_AT_decl_file. */
5522 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5523 if (attr)
5524 {
5525 unsigned int line_offset = DW_UNSND (attr);
5526 struct line_header *line_header
5527 = dwarf_decode_line_header (line_offset, abfd, cu);
5528
5529 if (line_header)
5530 {
5531 cu->line_header = line_header;
5532 make_cleanup (free_cu_line_header, cu);
5533 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5534 }
5535 }
5536 }
5537
5538 /* Process DW_TAG_compile_unit. */
5539
5540 static void
5541 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5542 {
5543 struct objfile *objfile = cu->objfile;
5544 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5545 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5546 CORE_ADDR highpc = ((CORE_ADDR) 0);
5547 struct attribute *attr;
5548 char *name = NULL;
5549 char *comp_dir = NULL;
5550 struct die_info *child_die;
5551 bfd *abfd = objfile->obfd;
5552 CORE_ADDR baseaddr;
5553
5554 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5555
5556 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5557
5558 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5559 from finish_block. */
5560 if (lowpc == ((CORE_ADDR) -1))
5561 lowpc = highpc;
5562 lowpc += baseaddr;
5563 highpc += baseaddr;
5564
5565 find_file_and_directory (die, cu, &name, &comp_dir);
5566
5567 attr = dwarf2_attr (die, DW_AT_language, cu);
5568 if (attr)
5569 {
5570 set_cu_language (DW_UNSND (attr), cu);
5571 }
5572
5573 attr = dwarf2_attr (die, DW_AT_producer, cu);
5574 if (attr)
5575 cu->producer = DW_STRING (attr);
5576
5577 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5578 standardised yet. As a workaround for the language detection we fall
5579 back to the DW_AT_producer string. */
5580 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5581 cu->language = language_opencl;
5582
5583 /* We assume that we're processing GCC output. */
5584 processing_gcc_compilation = 2;
5585
5586 processing_has_namespace_info = 0;
5587
5588 start_symtab (name, comp_dir, lowpc);
5589 record_debugformat ("DWARF 2");
5590 record_producer (cu->producer);
5591
5592 initialize_cu_func_list (cu);
5593
5594 handle_DW_AT_stmt_list (die, cu, comp_dir);
5595
5596 /* Process all dies in compilation unit. */
5597 if (die->child != NULL)
5598 {
5599 child_die = die->child;
5600 while (child_die && child_die->tag)
5601 {
5602 process_die (child_die, cu);
5603 child_die = sibling_die (child_die);
5604 }
5605 }
5606
5607 /* Decode macro information, if present. Dwarf 2 macro information
5608 refers to information in the line number info statement program
5609 header, so we can only read it if we've read the header
5610 successfully. */
5611 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5612 if (attr && cu->line_header)
5613 {
5614 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5615 complaint (&symfile_complaints,
5616 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5617
5618 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5619 comp_dir, abfd, cu,
5620 &dwarf2_per_objfile->macro, 1);
5621 }
5622 else
5623 {
5624 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5625 if (attr && cu->line_header)
5626 {
5627 unsigned int macro_offset = DW_UNSND (attr);
5628
5629 dwarf_decode_macros (cu->line_header, macro_offset,
5630 comp_dir, abfd, cu,
5631 &dwarf2_per_objfile->macinfo, 0);
5632 }
5633 }
5634 do_cleanups (back_to);
5635 }
5636
5637 /* Process DW_TAG_type_unit.
5638 For TUs we want to skip the first top level sibling if it's not the
5639 actual type being defined by this TU. In this case the first top
5640 level sibling is there to provide context only. */
5641
5642 static void
5643 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5644 {
5645 struct objfile *objfile = cu->objfile;
5646 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5647 CORE_ADDR lowpc;
5648 struct attribute *attr;
5649 char *name = NULL;
5650 char *comp_dir = NULL;
5651 struct die_info *child_die;
5652 bfd *abfd = objfile->obfd;
5653
5654 /* start_symtab needs a low pc, but we don't really have one.
5655 Do what read_file_scope would do in the absence of such info. */
5656 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5657
5658 /* Find the filename. Do not use dwarf2_name here, since the filename
5659 is not a source language identifier. */
5660 attr = dwarf2_attr (die, DW_AT_name, cu);
5661 if (attr)
5662 name = DW_STRING (attr);
5663
5664 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5665 if (attr)
5666 comp_dir = DW_STRING (attr);
5667 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5668 {
5669 comp_dir = ldirname (name);
5670 if (comp_dir != NULL)
5671 make_cleanup (xfree, comp_dir);
5672 }
5673
5674 if (name == NULL)
5675 name = "<unknown>";
5676
5677 attr = dwarf2_attr (die, DW_AT_language, cu);
5678 if (attr)
5679 set_cu_language (DW_UNSND (attr), cu);
5680
5681 /* This isn't technically needed today. It is done for symmetry
5682 with read_file_scope. */
5683 attr = dwarf2_attr (die, DW_AT_producer, cu);
5684 if (attr)
5685 cu->producer = DW_STRING (attr);
5686
5687 /* We assume that we're processing GCC output. */
5688 processing_gcc_compilation = 2;
5689
5690 processing_has_namespace_info = 0;
5691
5692 start_symtab (name, comp_dir, lowpc);
5693 record_debugformat ("DWARF 2");
5694 record_producer (cu->producer);
5695
5696 handle_DW_AT_stmt_list (die, cu, comp_dir);
5697
5698 /* Process the dies in the type unit. */
5699 if (die->child == NULL)
5700 {
5701 dump_die_for_error (die);
5702 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5703 bfd_get_filename (abfd));
5704 }
5705
5706 child_die = die->child;
5707
5708 while (child_die && child_die->tag)
5709 {
5710 process_die (child_die, cu);
5711
5712 child_die = sibling_die (child_die);
5713 }
5714
5715 do_cleanups (back_to);
5716 }
5717
5718 static void
5719 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5720 struct dwarf2_cu *cu)
5721 {
5722 struct function_range *thisfn;
5723
5724 thisfn = (struct function_range *)
5725 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5726 thisfn->name = name;
5727 thisfn->lowpc = lowpc;
5728 thisfn->highpc = highpc;
5729 thisfn->seen_line = 0;
5730 thisfn->next = NULL;
5731
5732 if (cu->last_fn == NULL)
5733 cu->first_fn = thisfn;
5734 else
5735 cu->last_fn->next = thisfn;
5736
5737 cu->last_fn = thisfn;
5738 }
5739
5740 /* qsort helper for inherit_abstract_dies. */
5741
5742 static int
5743 unsigned_int_compar (const void *ap, const void *bp)
5744 {
5745 unsigned int a = *(unsigned int *) ap;
5746 unsigned int b = *(unsigned int *) bp;
5747
5748 return (a > b) - (b > a);
5749 }
5750
5751 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5752 Inherit only the children of the DW_AT_abstract_origin DIE not being
5753 already referenced by DW_AT_abstract_origin from the children of the
5754 current DIE. */
5755
5756 static void
5757 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5758 {
5759 struct die_info *child_die;
5760 unsigned die_children_count;
5761 /* CU offsets which were referenced by children of the current DIE. */
5762 unsigned *offsets;
5763 unsigned *offsets_end, *offsetp;
5764 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5765 struct die_info *origin_die;
5766 /* Iterator of the ORIGIN_DIE children. */
5767 struct die_info *origin_child_die;
5768 struct cleanup *cleanups;
5769 struct attribute *attr;
5770 struct dwarf2_cu *origin_cu;
5771 struct pending **origin_previous_list_in_scope;
5772
5773 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5774 if (!attr)
5775 return;
5776
5777 /* Note that following die references may follow to a die in a
5778 different cu. */
5779
5780 origin_cu = cu;
5781 origin_die = follow_die_ref (die, attr, &origin_cu);
5782
5783 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5784 symbols in. */
5785 origin_previous_list_in_scope = origin_cu->list_in_scope;
5786 origin_cu->list_in_scope = cu->list_in_scope;
5787
5788 if (die->tag != origin_die->tag
5789 && !(die->tag == DW_TAG_inlined_subroutine
5790 && origin_die->tag == DW_TAG_subprogram))
5791 complaint (&symfile_complaints,
5792 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5793 die->offset, origin_die->offset);
5794
5795 child_die = die->child;
5796 die_children_count = 0;
5797 while (child_die && child_die->tag)
5798 {
5799 child_die = sibling_die (child_die);
5800 die_children_count++;
5801 }
5802 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5803 cleanups = make_cleanup (xfree, offsets);
5804
5805 offsets_end = offsets;
5806 child_die = die->child;
5807 while (child_die && child_die->tag)
5808 {
5809 /* For each CHILD_DIE, find the corresponding child of
5810 ORIGIN_DIE. If there is more than one layer of
5811 DW_AT_abstract_origin, follow them all; there shouldn't be,
5812 but GCC versions at least through 4.4 generate this (GCC PR
5813 40573). */
5814 struct die_info *child_origin_die = child_die;
5815 struct dwarf2_cu *child_origin_cu = cu;
5816
5817 while (1)
5818 {
5819 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5820 child_origin_cu);
5821 if (attr == NULL)
5822 break;
5823 child_origin_die = follow_die_ref (child_origin_die, attr,
5824 &child_origin_cu);
5825 }
5826
5827 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5828 counterpart may exist. */
5829 if (child_origin_die != child_die)
5830 {
5831 if (child_die->tag != child_origin_die->tag
5832 && !(child_die->tag == DW_TAG_inlined_subroutine
5833 && child_origin_die->tag == DW_TAG_subprogram))
5834 complaint (&symfile_complaints,
5835 _("Child DIE 0x%x and its abstract origin 0x%x have "
5836 "different tags"), child_die->offset,
5837 child_origin_die->offset);
5838 if (child_origin_die->parent != origin_die)
5839 complaint (&symfile_complaints,
5840 _("Child DIE 0x%x and its abstract origin 0x%x have "
5841 "different parents"), child_die->offset,
5842 child_origin_die->offset);
5843 else
5844 *offsets_end++ = child_origin_die->offset;
5845 }
5846 child_die = sibling_die (child_die);
5847 }
5848 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5849 unsigned_int_compar);
5850 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5851 if (offsetp[-1] == *offsetp)
5852 complaint (&symfile_complaints,
5853 _("Multiple children of DIE 0x%x refer "
5854 "to DIE 0x%x as their abstract origin"),
5855 die->offset, *offsetp);
5856
5857 offsetp = offsets;
5858 origin_child_die = origin_die->child;
5859 while (origin_child_die && origin_child_die->tag)
5860 {
5861 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5862 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5863 offsetp++;
5864 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5865 {
5866 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5867 process_die (origin_child_die, origin_cu);
5868 }
5869 origin_child_die = sibling_die (origin_child_die);
5870 }
5871 origin_cu->list_in_scope = origin_previous_list_in_scope;
5872
5873 do_cleanups (cleanups);
5874 }
5875
5876 static void
5877 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5878 {
5879 struct objfile *objfile = cu->objfile;
5880 struct context_stack *new;
5881 CORE_ADDR lowpc;
5882 CORE_ADDR highpc;
5883 struct die_info *child_die;
5884 struct attribute *attr, *call_line, *call_file;
5885 char *name;
5886 CORE_ADDR baseaddr;
5887 struct block *block;
5888 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5889 VEC (symbolp) *template_args = NULL;
5890 struct template_symbol *templ_func = NULL;
5891
5892 if (inlined_func)
5893 {
5894 /* If we do not have call site information, we can't show the
5895 caller of this inlined function. That's too confusing, so
5896 only use the scope for local variables. */
5897 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5898 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5899 if (call_line == NULL || call_file == NULL)
5900 {
5901 read_lexical_block_scope (die, cu);
5902 return;
5903 }
5904 }
5905
5906 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5907
5908 name = dwarf2_name (die, cu);
5909
5910 /* Ignore functions with missing or empty names. These are actually
5911 illegal according to the DWARF standard. */
5912 if (name == NULL)
5913 {
5914 complaint (&symfile_complaints,
5915 _("missing name for subprogram DIE at %d"), die->offset);
5916 return;
5917 }
5918
5919 /* Ignore functions with missing or invalid low and high pc attributes. */
5920 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5921 {
5922 attr = dwarf2_attr (die, DW_AT_external, cu);
5923 if (!attr || !DW_UNSND (attr))
5924 complaint (&symfile_complaints,
5925 _("cannot get low and high bounds "
5926 "for subprogram DIE at %d"),
5927 die->offset);
5928 return;
5929 }
5930
5931 lowpc += baseaddr;
5932 highpc += baseaddr;
5933
5934 /* Record the function range for dwarf_decode_lines. */
5935 add_to_cu_func_list (name, lowpc, highpc, cu);
5936
5937 /* If we have any template arguments, then we must allocate a
5938 different sort of symbol. */
5939 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5940 {
5941 if (child_die->tag == DW_TAG_template_type_param
5942 || child_die->tag == DW_TAG_template_value_param)
5943 {
5944 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5945 struct template_symbol);
5946 templ_func->base.is_cplus_template_function = 1;
5947 break;
5948 }
5949 }
5950
5951 new = push_context (0, lowpc);
5952 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5953 (struct symbol *) templ_func);
5954
5955 /* If there is a location expression for DW_AT_frame_base, record
5956 it. */
5957 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5958 if (attr)
5959 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5960 expression is being recorded directly in the function's symbol
5961 and not in a separate frame-base object. I guess this hack is
5962 to avoid adding some sort of frame-base adjunct/annex to the
5963 function's symbol :-(. The problem with doing this is that it
5964 results in a function symbol with a location expression that
5965 has nothing to do with the location of the function, ouch! The
5966 relationship should be: a function's symbol has-a frame base; a
5967 frame-base has-a location expression. */
5968 dwarf2_symbol_mark_computed (attr, new->name, cu);
5969
5970 cu->list_in_scope = &local_symbols;
5971
5972 if (die->child != NULL)
5973 {
5974 child_die = die->child;
5975 while (child_die && child_die->tag)
5976 {
5977 if (child_die->tag == DW_TAG_template_type_param
5978 || child_die->tag == DW_TAG_template_value_param)
5979 {
5980 struct symbol *arg = new_symbol (child_die, NULL, cu);
5981
5982 if (arg != NULL)
5983 VEC_safe_push (symbolp, template_args, arg);
5984 }
5985 else
5986 process_die (child_die, cu);
5987 child_die = sibling_die (child_die);
5988 }
5989 }
5990
5991 inherit_abstract_dies (die, cu);
5992
5993 /* If we have a DW_AT_specification, we might need to import using
5994 directives from the context of the specification DIE. See the
5995 comment in determine_prefix. */
5996 if (cu->language == language_cplus
5997 && dwarf2_attr (die, DW_AT_specification, cu))
5998 {
5999 struct dwarf2_cu *spec_cu = cu;
6000 struct die_info *spec_die = die_specification (die, &spec_cu);
6001
6002 while (spec_die)
6003 {
6004 child_die = spec_die->child;
6005 while (child_die && child_die->tag)
6006 {
6007 if (child_die->tag == DW_TAG_imported_module)
6008 process_die (child_die, spec_cu);
6009 child_die = sibling_die (child_die);
6010 }
6011
6012 /* In some cases, GCC generates specification DIEs that
6013 themselves contain DW_AT_specification attributes. */
6014 spec_die = die_specification (spec_die, &spec_cu);
6015 }
6016 }
6017
6018 new = pop_context ();
6019 /* Make a block for the local symbols within. */
6020 block = finish_block (new->name, &local_symbols, new->old_blocks,
6021 lowpc, highpc, objfile);
6022
6023 /* For C++, set the block's scope. */
6024 if (cu->language == language_cplus || cu->language == language_fortran)
6025 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6026 determine_prefix (die, cu),
6027 processing_has_namespace_info);
6028
6029 /* If we have address ranges, record them. */
6030 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6031
6032 /* Attach template arguments to function. */
6033 if (! VEC_empty (symbolp, template_args))
6034 {
6035 gdb_assert (templ_func != NULL);
6036
6037 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6038 templ_func->template_arguments
6039 = obstack_alloc (&objfile->objfile_obstack,
6040 (templ_func->n_template_arguments
6041 * sizeof (struct symbol *)));
6042 memcpy (templ_func->template_arguments,
6043 VEC_address (symbolp, template_args),
6044 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6045 VEC_free (symbolp, template_args);
6046 }
6047
6048 /* In C++, we can have functions nested inside functions (e.g., when
6049 a function declares a class that has methods). This means that
6050 when we finish processing a function scope, we may need to go
6051 back to building a containing block's symbol lists. */
6052 local_symbols = new->locals;
6053 param_symbols = new->params;
6054 using_directives = new->using_directives;
6055
6056 /* If we've finished processing a top-level function, subsequent
6057 symbols go in the file symbol list. */
6058 if (outermost_context_p ())
6059 cu->list_in_scope = &file_symbols;
6060 }
6061
6062 /* Process all the DIES contained within a lexical block scope. Start
6063 a new scope, process the dies, and then close the scope. */
6064
6065 static void
6066 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6067 {
6068 struct objfile *objfile = cu->objfile;
6069 struct context_stack *new;
6070 CORE_ADDR lowpc, highpc;
6071 struct die_info *child_die;
6072 CORE_ADDR baseaddr;
6073
6074 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6075
6076 /* Ignore blocks with missing or invalid low and high pc attributes. */
6077 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6078 as multiple lexical blocks? Handling children in a sane way would
6079 be nasty. Might be easier to properly extend generic blocks to
6080 describe ranges. */
6081 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6082 return;
6083 lowpc += baseaddr;
6084 highpc += baseaddr;
6085
6086 push_context (0, lowpc);
6087 if (die->child != NULL)
6088 {
6089 child_die = die->child;
6090 while (child_die && child_die->tag)
6091 {
6092 process_die (child_die, cu);
6093 child_die = sibling_die (child_die);
6094 }
6095 }
6096 new = pop_context ();
6097
6098 if (local_symbols != NULL || using_directives != NULL)
6099 {
6100 struct block *block
6101 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6102 highpc, objfile);
6103
6104 /* Note that recording ranges after traversing children, as we
6105 do here, means that recording a parent's ranges entails
6106 walking across all its children's ranges as they appear in
6107 the address map, which is quadratic behavior.
6108
6109 It would be nicer to record the parent's ranges before
6110 traversing its children, simply overriding whatever you find
6111 there. But since we don't even decide whether to create a
6112 block until after we've traversed its children, that's hard
6113 to do. */
6114 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6115 }
6116 local_symbols = new->locals;
6117 using_directives = new->using_directives;
6118 }
6119
6120 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6121 Return 1 if the attributes are present and valid, otherwise, return 0.
6122 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6123
6124 static int
6125 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6126 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6127 struct partial_symtab *ranges_pst)
6128 {
6129 struct objfile *objfile = cu->objfile;
6130 struct comp_unit_head *cu_header = &cu->header;
6131 bfd *obfd = objfile->obfd;
6132 unsigned int addr_size = cu_header->addr_size;
6133 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6134 /* Base address selection entry. */
6135 CORE_ADDR base;
6136 int found_base;
6137 unsigned int dummy;
6138 gdb_byte *buffer;
6139 CORE_ADDR marker;
6140 int low_set;
6141 CORE_ADDR low = 0;
6142 CORE_ADDR high = 0;
6143 CORE_ADDR baseaddr;
6144
6145 found_base = cu->base_known;
6146 base = cu->base_address;
6147
6148 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6149 if (offset >= dwarf2_per_objfile->ranges.size)
6150 {
6151 complaint (&symfile_complaints,
6152 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6153 offset);
6154 return 0;
6155 }
6156 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6157
6158 /* Read in the largest possible address. */
6159 marker = read_address (obfd, buffer, cu, &dummy);
6160 if ((marker & mask) == mask)
6161 {
6162 /* If we found the largest possible address, then
6163 read the base address. */
6164 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6165 buffer += 2 * addr_size;
6166 offset += 2 * addr_size;
6167 found_base = 1;
6168 }
6169
6170 low_set = 0;
6171
6172 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6173
6174 while (1)
6175 {
6176 CORE_ADDR range_beginning, range_end;
6177
6178 range_beginning = read_address (obfd, buffer, cu, &dummy);
6179 buffer += addr_size;
6180 range_end = read_address (obfd, buffer, cu, &dummy);
6181 buffer += addr_size;
6182 offset += 2 * addr_size;
6183
6184 /* An end of list marker is a pair of zero addresses. */
6185 if (range_beginning == 0 && range_end == 0)
6186 /* Found the end of list entry. */
6187 break;
6188
6189 /* Each base address selection entry is a pair of 2 values.
6190 The first is the largest possible address, the second is
6191 the base address. Check for a base address here. */
6192 if ((range_beginning & mask) == mask)
6193 {
6194 /* If we found the largest possible address, then
6195 read the base address. */
6196 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6197 found_base = 1;
6198 continue;
6199 }
6200
6201 if (!found_base)
6202 {
6203 /* We have no valid base address for the ranges
6204 data. */
6205 complaint (&symfile_complaints,
6206 _("Invalid .debug_ranges data (no base address)"));
6207 return 0;
6208 }
6209
6210 if (range_beginning > range_end)
6211 {
6212 /* Inverted range entries are invalid. */
6213 complaint (&symfile_complaints,
6214 _("Invalid .debug_ranges data (inverted range)"));
6215 return 0;
6216 }
6217
6218 /* Empty range entries have no effect. */
6219 if (range_beginning == range_end)
6220 continue;
6221
6222 range_beginning += base;
6223 range_end += base;
6224
6225 if (ranges_pst != NULL)
6226 addrmap_set_empty (objfile->psymtabs_addrmap,
6227 range_beginning + baseaddr,
6228 range_end - 1 + baseaddr,
6229 ranges_pst);
6230
6231 /* FIXME: This is recording everything as a low-high
6232 segment of consecutive addresses. We should have a
6233 data structure for discontiguous block ranges
6234 instead. */
6235 if (! low_set)
6236 {
6237 low = range_beginning;
6238 high = range_end;
6239 low_set = 1;
6240 }
6241 else
6242 {
6243 if (range_beginning < low)
6244 low = range_beginning;
6245 if (range_end > high)
6246 high = range_end;
6247 }
6248 }
6249
6250 if (! low_set)
6251 /* If the first entry is an end-of-list marker, the range
6252 describes an empty scope, i.e. no instructions. */
6253 return 0;
6254
6255 if (low_return)
6256 *low_return = low;
6257 if (high_return)
6258 *high_return = high;
6259 return 1;
6260 }
6261
6262 /* Get low and high pc attributes from a die. Return 1 if the attributes
6263 are present and valid, otherwise, return 0. Return -1 if the range is
6264 discontinuous, i.e. derived from DW_AT_ranges information. */
6265 static int
6266 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6267 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6268 struct partial_symtab *pst)
6269 {
6270 struct attribute *attr;
6271 CORE_ADDR low = 0;
6272 CORE_ADDR high = 0;
6273 int ret = 0;
6274
6275 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6276 if (attr)
6277 {
6278 high = DW_ADDR (attr);
6279 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6280 if (attr)
6281 low = DW_ADDR (attr);
6282 else
6283 /* Found high w/o low attribute. */
6284 return 0;
6285
6286 /* Found consecutive range of addresses. */
6287 ret = 1;
6288 }
6289 else
6290 {
6291 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6292 if (attr != NULL)
6293 {
6294 /* Value of the DW_AT_ranges attribute is the offset in the
6295 .debug_ranges section. */
6296 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6297 return 0;
6298 /* Found discontinuous range of addresses. */
6299 ret = -1;
6300 }
6301 }
6302
6303 /* read_partial_die has also the strict LOW < HIGH requirement. */
6304 if (high <= low)
6305 return 0;
6306
6307 /* When using the GNU linker, .gnu.linkonce. sections are used to
6308 eliminate duplicate copies of functions and vtables and such.
6309 The linker will arbitrarily choose one and discard the others.
6310 The AT_*_pc values for such functions refer to local labels in
6311 these sections. If the section from that file was discarded, the
6312 labels are not in the output, so the relocs get a value of 0.
6313 If this is a discarded function, mark the pc bounds as invalid,
6314 so that GDB will ignore it. */
6315 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6316 return 0;
6317
6318 *lowpc = low;
6319 *highpc = high;
6320 return ret;
6321 }
6322
6323 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6324 its low and high PC addresses. Do nothing if these addresses could not
6325 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6326 and HIGHPC to the high address if greater than HIGHPC. */
6327
6328 static void
6329 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6330 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6331 struct dwarf2_cu *cu)
6332 {
6333 CORE_ADDR low, high;
6334 struct die_info *child = die->child;
6335
6336 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6337 {
6338 *lowpc = min (*lowpc, low);
6339 *highpc = max (*highpc, high);
6340 }
6341
6342 /* If the language does not allow nested subprograms (either inside
6343 subprograms or lexical blocks), we're done. */
6344 if (cu->language != language_ada)
6345 return;
6346
6347 /* Check all the children of the given DIE. If it contains nested
6348 subprograms, then check their pc bounds. Likewise, we need to
6349 check lexical blocks as well, as they may also contain subprogram
6350 definitions. */
6351 while (child && child->tag)
6352 {
6353 if (child->tag == DW_TAG_subprogram
6354 || child->tag == DW_TAG_lexical_block)
6355 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6356 child = sibling_die (child);
6357 }
6358 }
6359
6360 /* Get the low and high pc's represented by the scope DIE, and store
6361 them in *LOWPC and *HIGHPC. If the correct values can't be
6362 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6363
6364 static void
6365 get_scope_pc_bounds (struct die_info *die,
6366 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6367 struct dwarf2_cu *cu)
6368 {
6369 CORE_ADDR best_low = (CORE_ADDR) -1;
6370 CORE_ADDR best_high = (CORE_ADDR) 0;
6371 CORE_ADDR current_low, current_high;
6372
6373 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6374 {
6375 best_low = current_low;
6376 best_high = current_high;
6377 }
6378 else
6379 {
6380 struct die_info *child = die->child;
6381
6382 while (child && child->tag)
6383 {
6384 switch (child->tag) {
6385 case DW_TAG_subprogram:
6386 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6387 break;
6388 case DW_TAG_namespace:
6389 case DW_TAG_module:
6390 /* FIXME: carlton/2004-01-16: Should we do this for
6391 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6392 that current GCC's always emit the DIEs corresponding
6393 to definitions of methods of classes as children of a
6394 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6395 the DIEs giving the declarations, which could be
6396 anywhere). But I don't see any reason why the
6397 standards says that they have to be there. */
6398 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6399
6400 if (current_low != ((CORE_ADDR) -1))
6401 {
6402 best_low = min (best_low, current_low);
6403 best_high = max (best_high, current_high);
6404 }
6405 break;
6406 default:
6407 /* Ignore. */
6408 break;
6409 }
6410
6411 child = sibling_die (child);
6412 }
6413 }
6414
6415 *lowpc = best_low;
6416 *highpc = best_high;
6417 }
6418
6419 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6420 in DIE. */
6421 static void
6422 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6423 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6424 {
6425 struct attribute *attr;
6426
6427 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6428 if (attr)
6429 {
6430 CORE_ADDR high = DW_ADDR (attr);
6431
6432 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6433 if (attr)
6434 {
6435 CORE_ADDR low = DW_ADDR (attr);
6436
6437 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6438 }
6439 }
6440
6441 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6442 if (attr)
6443 {
6444 bfd *obfd = cu->objfile->obfd;
6445
6446 /* The value of the DW_AT_ranges attribute is the offset of the
6447 address range list in the .debug_ranges section. */
6448 unsigned long offset = DW_UNSND (attr);
6449 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6450
6451 /* For some target architectures, but not others, the
6452 read_address function sign-extends the addresses it returns.
6453 To recognize base address selection entries, we need a
6454 mask. */
6455 unsigned int addr_size = cu->header.addr_size;
6456 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6457
6458 /* The base address, to which the next pair is relative. Note
6459 that this 'base' is a DWARF concept: most entries in a range
6460 list are relative, to reduce the number of relocs against the
6461 debugging information. This is separate from this function's
6462 'baseaddr' argument, which GDB uses to relocate debugging
6463 information from a shared library based on the address at
6464 which the library was loaded. */
6465 CORE_ADDR base = cu->base_address;
6466 int base_known = cu->base_known;
6467
6468 gdb_assert (dwarf2_per_objfile->ranges.readin);
6469 if (offset >= dwarf2_per_objfile->ranges.size)
6470 {
6471 complaint (&symfile_complaints,
6472 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6473 offset);
6474 return;
6475 }
6476
6477 for (;;)
6478 {
6479 unsigned int bytes_read;
6480 CORE_ADDR start, end;
6481
6482 start = read_address (obfd, buffer, cu, &bytes_read);
6483 buffer += bytes_read;
6484 end = read_address (obfd, buffer, cu, &bytes_read);
6485 buffer += bytes_read;
6486
6487 /* Did we find the end of the range list? */
6488 if (start == 0 && end == 0)
6489 break;
6490
6491 /* Did we find a base address selection entry? */
6492 else if ((start & base_select_mask) == base_select_mask)
6493 {
6494 base = end;
6495 base_known = 1;
6496 }
6497
6498 /* We found an ordinary address range. */
6499 else
6500 {
6501 if (!base_known)
6502 {
6503 complaint (&symfile_complaints,
6504 _("Invalid .debug_ranges data "
6505 "(no base address)"));
6506 return;
6507 }
6508
6509 if (start > end)
6510 {
6511 /* Inverted range entries are invalid. */
6512 complaint (&symfile_complaints,
6513 _("Invalid .debug_ranges data "
6514 "(inverted range)"));
6515 return;
6516 }
6517
6518 /* Empty range entries have no effect. */
6519 if (start == end)
6520 continue;
6521
6522 record_block_range (block,
6523 baseaddr + base + start,
6524 baseaddr + base + end - 1);
6525 }
6526 }
6527 }
6528 }
6529
6530 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6531 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6532 during 4.6.0 experimental. */
6533
6534 static int
6535 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6536 {
6537 const char *cs;
6538 int major, minor, release;
6539
6540 if (cu->producer == NULL)
6541 {
6542 /* For unknown compilers expect their behavior is DWARF version
6543 compliant.
6544
6545 GCC started to support .debug_types sections by -gdwarf-4 since
6546 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6547 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6548 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6549 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6550
6551 return 0;
6552 }
6553
6554 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6555
6556 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6557 {
6558 /* For non-GCC compilers expect their behavior is DWARF version
6559 compliant. */
6560
6561 return 0;
6562 }
6563 cs = &cu->producer[strlen ("GNU ")];
6564 while (*cs && !isdigit (*cs))
6565 cs++;
6566 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6567 {
6568 /* Not recognized as GCC. */
6569
6570 return 0;
6571 }
6572
6573 return major < 4 || (major == 4 && minor < 6);
6574 }
6575
6576 /* Return the default accessibility type if it is not overriden by
6577 DW_AT_accessibility. */
6578
6579 static enum dwarf_access_attribute
6580 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6581 {
6582 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6583 {
6584 /* The default DWARF 2 accessibility for members is public, the default
6585 accessibility for inheritance is private. */
6586
6587 if (die->tag != DW_TAG_inheritance)
6588 return DW_ACCESS_public;
6589 else
6590 return DW_ACCESS_private;
6591 }
6592 else
6593 {
6594 /* DWARF 3+ defines the default accessibility a different way. The same
6595 rules apply now for DW_TAG_inheritance as for the members and it only
6596 depends on the container kind. */
6597
6598 if (die->parent->tag == DW_TAG_class_type)
6599 return DW_ACCESS_private;
6600 else
6601 return DW_ACCESS_public;
6602 }
6603 }
6604
6605 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6606 offset. If the attribute was not found return 0, otherwise return
6607 1. If it was found but could not properly be handled, set *OFFSET
6608 to 0. */
6609
6610 static int
6611 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6612 LONGEST *offset)
6613 {
6614 struct attribute *attr;
6615
6616 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6617 if (attr != NULL)
6618 {
6619 *offset = 0;
6620
6621 /* Note that we do not check for a section offset first here.
6622 This is because DW_AT_data_member_location is new in DWARF 4,
6623 so if we see it, we can assume that a constant form is really
6624 a constant and not a section offset. */
6625 if (attr_form_is_constant (attr))
6626 *offset = dwarf2_get_attr_constant_value (attr, 0);
6627 else if (attr_form_is_section_offset (attr))
6628 dwarf2_complex_location_expr_complaint ();
6629 else if (attr_form_is_block (attr))
6630 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6631 else
6632 dwarf2_complex_location_expr_complaint ();
6633
6634 return 1;
6635 }
6636
6637 return 0;
6638 }
6639
6640 /* Add an aggregate field to the field list. */
6641
6642 static void
6643 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6644 struct dwarf2_cu *cu)
6645 {
6646 struct objfile *objfile = cu->objfile;
6647 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6648 struct nextfield *new_field;
6649 struct attribute *attr;
6650 struct field *fp;
6651 char *fieldname = "";
6652
6653 /* Allocate a new field list entry and link it in. */
6654 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6655 make_cleanup (xfree, new_field);
6656 memset (new_field, 0, sizeof (struct nextfield));
6657
6658 if (die->tag == DW_TAG_inheritance)
6659 {
6660 new_field->next = fip->baseclasses;
6661 fip->baseclasses = new_field;
6662 }
6663 else
6664 {
6665 new_field->next = fip->fields;
6666 fip->fields = new_field;
6667 }
6668 fip->nfields++;
6669
6670 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6671 if (attr)
6672 new_field->accessibility = DW_UNSND (attr);
6673 else
6674 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6675 if (new_field->accessibility != DW_ACCESS_public)
6676 fip->non_public_fields = 1;
6677
6678 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6679 if (attr)
6680 new_field->virtuality = DW_UNSND (attr);
6681 else
6682 new_field->virtuality = DW_VIRTUALITY_none;
6683
6684 fp = &new_field->field;
6685
6686 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6687 {
6688 LONGEST offset;
6689
6690 /* Data member other than a C++ static data member. */
6691
6692 /* Get type of field. */
6693 fp->type = die_type (die, cu);
6694
6695 SET_FIELD_BITPOS (*fp, 0);
6696
6697 /* Get bit size of field (zero if none). */
6698 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6699 if (attr)
6700 {
6701 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6702 }
6703 else
6704 {
6705 FIELD_BITSIZE (*fp) = 0;
6706 }
6707
6708 /* Get bit offset of field. */
6709 if (handle_data_member_location (die, cu, &offset))
6710 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6711 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6712 if (attr)
6713 {
6714 if (gdbarch_bits_big_endian (gdbarch))
6715 {
6716 /* For big endian bits, the DW_AT_bit_offset gives the
6717 additional bit offset from the MSB of the containing
6718 anonymous object to the MSB of the field. We don't
6719 have to do anything special since we don't need to
6720 know the size of the anonymous object. */
6721 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6722 }
6723 else
6724 {
6725 /* For little endian bits, compute the bit offset to the
6726 MSB of the anonymous object, subtract off the number of
6727 bits from the MSB of the field to the MSB of the
6728 object, and then subtract off the number of bits of
6729 the field itself. The result is the bit offset of
6730 the LSB of the field. */
6731 int anonymous_size;
6732 int bit_offset = DW_UNSND (attr);
6733
6734 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6735 if (attr)
6736 {
6737 /* The size of the anonymous object containing
6738 the bit field is explicit, so use the
6739 indicated size (in bytes). */
6740 anonymous_size = DW_UNSND (attr);
6741 }
6742 else
6743 {
6744 /* The size of the anonymous object containing
6745 the bit field must be inferred from the type
6746 attribute of the data member containing the
6747 bit field. */
6748 anonymous_size = TYPE_LENGTH (fp->type);
6749 }
6750 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6751 - bit_offset - FIELD_BITSIZE (*fp);
6752 }
6753 }
6754
6755 /* Get name of field. */
6756 fieldname = dwarf2_name (die, cu);
6757 if (fieldname == NULL)
6758 fieldname = "";
6759
6760 /* The name is already allocated along with this objfile, so we don't
6761 need to duplicate it for the type. */
6762 fp->name = fieldname;
6763
6764 /* Change accessibility for artificial fields (e.g. virtual table
6765 pointer or virtual base class pointer) to private. */
6766 if (dwarf2_attr (die, DW_AT_artificial, cu))
6767 {
6768 FIELD_ARTIFICIAL (*fp) = 1;
6769 new_field->accessibility = DW_ACCESS_private;
6770 fip->non_public_fields = 1;
6771 }
6772 }
6773 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6774 {
6775 /* C++ static member. */
6776
6777 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6778 is a declaration, but all versions of G++ as of this writing
6779 (so through at least 3.2.1) incorrectly generate
6780 DW_TAG_variable tags. */
6781
6782 const char *physname;
6783
6784 /* Get name of field. */
6785 fieldname = dwarf2_name (die, cu);
6786 if (fieldname == NULL)
6787 return;
6788
6789 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6790 if (attr
6791 /* Only create a symbol if this is an external value.
6792 new_symbol checks this and puts the value in the global symbol
6793 table, which we want. If it is not external, new_symbol
6794 will try to put the value in cu->list_in_scope which is wrong. */
6795 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6796 {
6797 /* A static const member, not much different than an enum as far as
6798 we're concerned, except that we can support more types. */
6799 new_symbol (die, NULL, cu);
6800 }
6801
6802 /* Get physical name. */
6803 physname = dwarf2_physname (fieldname, die, cu);
6804
6805 /* The name is already allocated along with this objfile, so we don't
6806 need to duplicate it for the type. */
6807 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6808 FIELD_TYPE (*fp) = die_type (die, cu);
6809 FIELD_NAME (*fp) = fieldname;
6810 }
6811 else if (die->tag == DW_TAG_inheritance)
6812 {
6813 LONGEST offset;
6814
6815 /* C++ base class field. */
6816 if (handle_data_member_location (die, cu, &offset))
6817 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6818 FIELD_BITSIZE (*fp) = 0;
6819 FIELD_TYPE (*fp) = die_type (die, cu);
6820 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6821 fip->nbaseclasses++;
6822 }
6823 }
6824
6825 /* Add a typedef defined in the scope of the FIP's class. */
6826
6827 static void
6828 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6829 struct dwarf2_cu *cu)
6830 {
6831 struct objfile *objfile = cu->objfile;
6832 struct typedef_field_list *new_field;
6833 struct attribute *attr;
6834 struct typedef_field *fp;
6835 char *fieldname = "";
6836
6837 /* Allocate a new field list entry and link it in. */
6838 new_field = xzalloc (sizeof (*new_field));
6839 make_cleanup (xfree, new_field);
6840
6841 gdb_assert (die->tag == DW_TAG_typedef);
6842
6843 fp = &new_field->field;
6844
6845 /* Get name of field. */
6846 fp->name = dwarf2_name (die, cu);
6847 if (fp->name == NULL)
6848 return;
6849
6850 fp->type = read_type_die (die, cu);
6851
6852 new_field->next = fip->typedef_field_list;
6853 fip->typedef_field_list = new_field;
6854 fip->typedef_field_list_count++;
6855 }
6856
6857 /* Create the vector of fields, and attach it to the type. */
6858
6859 static void
6860 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6861 struct dwarf2_cu *cu)
6862 {
6863 int nfields = fip->nfields;
6864
6865 /* Record the field count, allocate space for the array of fields,
6866 and create blank accessibility bitfields if necessary. */
6867 TYPE_NFIELDS (type) = nfields;
6868 TYPE_FIELDS (type) = (struct field *)
6869 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6870 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6871
6872 if (fip->non_public_fields && cu->language != language_ada)
6873 {
6874 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6875
6876 TYPE_FIELD_PRIVATE_BITS (type) =
6877 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6878 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6879
6880 TYPE_FIELD_PROTECTED_BITS (type) =
6881 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6882 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6883
6884 TYPE_FIELD_IGNORE_BITS (type) =
6885 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6886 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6887 }
6888
6889 /* If the type has baseclasses, allocate and clear a bit vector for
6890 TYPE_FIELD_VIRTUAL_BITS. */
6891 if (fip->nbaseclasses && cu->language != language_ada)
6892 {
6893 int num_bytes = B_BYTES (fip->nbaseclasses);
6894 unsigned char *pointer;
6895
6896 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6897 pointer = TYPE_ALLOC (type, num_bytes);
6898 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6899 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6900 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6901 }
6902
6903 /* Copy the saved-up fields into the field vector. Start from the head of
6904 the list, adding to the tail of the field array, so that they end up in
6905 the same order in the array in which they were added to the list. */
6906 while (nfields-- > 0)
6907 {
6908 struct nextfield *fieldp;
6909
6910 if (fip->fields)
6911 {
6912 fieldp = fip->fields;
6913 fip->fields = fieldp->next;
6914 }
6915 else
6916 {
6917 fieldp = fip->baseclasses;
6918 fip->baseclasses = fieldp->next;
6919 }
6920
6921 TYPE_FIELD (type, nfields) = fieldp->field;
6922 switch (fieldp->accessibility)
6923 {
6924 case DW_ACCESS_private:
6925 if (cu->language != language_ada)
6926 SET_TYPE_FIELD_PRIVATE (type, nfields);
6927 break;
6928
6929 case DW_ACCESS_protected:
6930 if (cu->language != language_ada)
6931 SET_TYPE_FIELD_PROTECTED (type, nfields);
6932 break;
6933
6934 case DW_ACCESS_public:
6935 break;
6936
6937 default:
6938 /* Unknown accessibility. Complain and treat it as public. */
6939 {
6940 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6941 fieldp->accessibility);
6942 }
6943 break;
6944 }
6945 if (nfields < fip->nbaseclasses)
6946 {
6947 switch (fieldp->virtuality)
6948 {
6949 case DW_VIRTUALITY_virtual:
6950 case DW_VIRTUALITY_pure_virtual:
6951 if (cu->language == language_ada)
6952 error (_("unexpected virtuality in component of Ada type"));
6953 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6954 break;
6955 }
6956 }
6957 }
6958 }
6959
6960 /* Add a member function to the proper fieldlist. */
6961
6962 static void
6963 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6964 struct type *type, struct dwarf2_cu *cu)
6965 {
6966 struct objfile *objfile = cu->objfile;
6967 struct attribute *attr;
6968 struct fnfieldlist *flp;
6969 int i;
6970 struct fn_field *fnp;
6971 char *fieldname;
6972 struct nextfnfield *new_fnfield;
6973 struct type *this_type;
6974 enum dwarf_access_attribute accessibility;
6975
6976 if (cu->language == language_ada)
6977 error (_("unexpected member function in Ada type"));
6978
6979 /* Get name of member function. */
6980 fieldname = dwarf2_name (die, cu);
6981 if (fieldname == NULL)
6982 return;
6983
6984 /* Look up member function name in fieldlist. */
6985 for (i = 0; i < fip->nfnfields; i++)
6986 {
6987 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6988 break;
6989 }
6990
6991 /* Create new list element if necessary. */
6992 if (i < fip->nfnfields)
6993 flp = &fip->fnfieldlists[i];
6994 else
6995 {
6996 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6997 {
6998 fip->fnfieldlists = (struct fnfieldlist *)
6999 xrealloc (fip->fnfieldlists,
7000 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7001 * sizeof (struct fnfieldlist));
7002 if (fip->nfnfields == 0)
7003 make_cleanup (free_current_contents, &fip->fnfieldlists);
7004 }
7005 flp = &fip->fnfieldlists[fip->nfnfields];
7006 flp->name = fieldname;
7007 flp->length = 0;
7008 flp->head = NULL;
7009 i = fip->nfnfields++;
7010 }
7011
7012 /* Create a new member function field and chain it to the field list
7013 entry. */
7014 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7015 make_cleanup (xfree, new_fnfield);
7016 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7017 new_fnfield->next = flp->head;
7018 flp->head = new_fnfield;
7019 flp->length++;
7020
7021 /* Fill in the member function field info. */
7022 fnp = &new_fnfield->fnfield;
7023
7024 /* Delay processing of the physname until later. */
7025 if (cu->language == language_cplus || cu->language == language_java)
7026 {
7027 add_to_method_list (type, i, flp->length - 1, fieldname,
7028 die, cu);
7029 }
7030 else
7031 {
7032 const char *physname = dwarf2_physname (fieldname, die, cu);
7033 fnp->physname = physname ? physname : "";
7034 }
7035
7036 fnp->type = alloc_type (objfile);
7037 this_type = read_type_die (die, cu);
7038 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7039 {
7040 int nparams = TYPE_NFIELDS (this_type);
7041
7042 /* TYPE is the domain of this method, and THIS_TYPE is the type
7043 of the method itself (TYPE_CODE_METHOD). */
7044 smash_to_method_type (fnp->type, type,
7045 TYPE_TARGET_TYPE (this_type),
7046 TYPE_FIELDS (this_type),
7047 TYPE_NFIELDS (this_type),
7048 TYPE_VARARGS (this_type));
7049
7050 /* Handle static member functions.
7051 Dwarf2 has no clean way to discern C++ static and non-static
7052 member functions. G++ helps GDB by marking the first
7053 parameter for non-static member functions (which is the this
7054 pointer) as artificial. We obtain this information from
7055 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7056 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7057 fnp->voffset = VOFFSET_STATIC;
7058 }
7059 else
7060 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7061 dwarf2_full_name (fieldname, die, cu));
7062
7063 /* Get fcontext from DW_AT_containing_type if present. */
7064 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7065 fnp->fcontext = die_containing_type (die, cu);
7066
7067 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7068 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7069
7070 /* Get accessibility. */
7071 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7072 if (attr)
7073 accessibility = DW_UNSND (attr);
7074 else
7075 accessibility = dwarf2_default_access_attribute (die, cu);
7076 switch (accessibility)
7077 {
7078 case DW_ACCESS_private:
7079 fnp->is_private = 1;
7080 break;
7081 case DW_ACCESS_protected:
7082 fnp->is_protected = 1;
7083 break;
7084 }
7085
7086 /* Check for artificial methods. */
7087 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7088 if (attr && DW_UNSND (attr) != 0)
7089 fnp->is_artificial = 1;
7090
7091 /* Get index in virtual function table if it is a virtual member
7092 function. For older versions of GCC, this is an offset in the
7093 appropriate virtual table, as specified by DW_AT_containing_type.
7094 For everyone else, it is an expression to be evaluated relative
7095 to the object address. */
7096
7097 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7098 if (attr)
7099 {
7100 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7101 {
7102 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7103 {
7104 /* Old-style GCC. */
7105 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7106 }
7107 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7108 || (DW_BLOCK (attr)->size > 1
7109 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7110 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7111 {
7112 struct dwarf_block blk;
7113 int offset;
7114
7115 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7116 ? 1 : 2);
7117 blk.size = DW_BLOCK (attr)->size - offset;
7118 blk.data = DW_BLOCK (attr)->data + offset;
7119 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7120 if ((fnp->voffset % cu->header.addr_size) != 0)
7121 dwarf2_complex_location_expr_complaint ();
7122 else
7123 fnp->voffset /= cu->header.addr_size;
7124 fnp->voffset += 2;
7125 }
7126 else
7127 dwarf2_complex_location_expr_complaint ();
7128
7129 if (!fnp->fcontext)
7130 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7131 }
7132 else if (attr_form_is_section_offset (attr))
7133 {
7134 dwarf2_complex_location_expr_complaint ();
7135 }
7136 else
7137 {
7138 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7139 fieldname);
7140 }
7141 }
7142 else
7143 {
7144 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7145 if (attr && DW_UNSND (attr))
7146 {
7147 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7148 complaint (&symfile_complaints,
7149 _("Member function \"%s\" (offset %d) is virtual "
7150 "but the vtable offset is not specified"),
7151 fieldname, die->offset);
7152 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7153 TYPE_CPLUS_DYNAMIC (type) = 1;
7154 }
7155 }
7156 }
7157
7158 /* Create the vector of member function fields, and attach it to the type. */
7159
7160 static void
7161 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7162 struct dwarf2_cu *cu)
7163 {
7164 struct fnfieldlist *flp;
7165 int total_length = 0;
7166 int i;
7167
7168 if (cu->language == language_ada)
7169 error (_("unexpected member functions in Ada type"));
7170
7171 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7172 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7173 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7174
7175 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7176 {
7177 struct nextfnfield *nfp = flp->head;
7178 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7179 int k;
7180
7181 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7182 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7183 fn_flp->fn_fields = (struct fn_field *)
7184 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7185 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7186 fn_flp->fn_fields[k] = nfp->fnfield;
7187
7188 total_length += flp->length;
7189 }
7190
7191 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7192 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7193 }
7194
7195 /* Returns non-zero if NAME is the name of a vtable member in CU's
7196 language, zero otherwise. */
7197 static int
7198 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7199 {
7200 static const char vptr[] = "_vptr";
7201 static const char vtable[] = "vtable";
7202
7203 /* Look for the C++ and Java forms of the vtable. */
7204 if ((cu->language == language_java
7205 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7206 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7207 && is_cplus_marker (name[sizeof (vptr) - 1])))
7208 return 1;
7209
7210 return 0;
7211 }
7212
7213 /* GCC outputs unnamed structures that are really pointers to member
7214 functions, with the ABI-specified layout. If TYPE describes
7215 such a structure, smash it into a member function type.
7216
7217 GCC shouldn't do this; it should just output pointer to member DIEs.
7218 This is GCC PR debug/28767. */
7219
7220 static void
7221 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7222 {
7223 struct type *pfn_type, *domain_type, *new_type;
7224
7225 /* Check for a structure with no name and two children. */
7226 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7227 return;
7228
7229 /* Check for __pfn and __delta members. */
7230 if (TYPE_FIELD_NAME (type, 0) == NULL
7231 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7232 || TYPE_FIELD_NAME (type, 1) == NULL
7233 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7234 return;
7235
7236 /* Find the type of the method. */
7237 pfn_type = TYPE_FIELD_TYPE (type, 0);
7238 if (pfn_type == NULL
7239 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7240 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7241 return;
7242
7243 /* Look for the "this" argument. */
7244 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7245 if (TYPE_NFIELDS (pfn_type) == 0
7246 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7247 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7248 return;
7249
7250 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7251 new_type = alloc_type (objfile);
7252 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7253 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7254 TYPE_VARARGS (pfn_type));
7255 smash_to_methodptr_type (type, new_type);
7256 }
7257
7258 /* Called when we find the DIE that starts a structure or union scope
7259 (definition) to create a type for the structure or union. Fill in
7260 the type's name and general properties; the members will not be
7261 processed until process_structure_type.
7262
7263 NOTE: we need to call these functions regardless of whether or not the
7264 DIE has a DW_AT_name attribute, since it might be an anonymous
7265 structure or union. This gets the type entered into our set of
7266 user defined types.
7267
7268 However, if the structure is incomplete (an opaque struct/union)
7269 then suppress creating a symbol table entry for it since gdb only
7270 wants to find the one with the complete definition. Note that if
7271 it is complete, we just call new_symbol, which does it's own
7272 checking about whether the struct/union is anonymous or not (and
7273 suppresses creating a symbol table entry itself). */
7274
7275 static struct type *
7276 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7277 {
7278 struct objfile *objfile = cu->objfile;
7279 struct type *type;
7280 struct attribute *attr;
7281 char *name;
7282
7283 /* If the definition of this type lives in .debug_types, read that type.
7284 Don't follow DW_AT_specification though, that will take us back up
7285 the chain and we want to go down. */
7286 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7287 if (attr)
7288 {
7289 struct dwarf2_cu *type_cu = cu;
7290 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7291
7292 /* We could just recurse on read_structure_type, but we need to call
7293 get_die_type to ensure only one type for this DIE is created.
7294 This is important, for example, because for c++ classes we need
7295 TYPE_NAME set which is only done by new_symbol. Blech. */
7296 type = read_type_die (type_die, type_cu);
7297
7298 /* TYPE_CU may not be the same as CU.
7299 Ensure TYPE is recorded in CU's type_hash table. */
7300 return set_die_type (die, type, cu);
7301 }
7302
7303 type = alloc_type (objfile);
7304 INIT_CPLUS_SPECIFIC (type);
7305
7306 name = dwarf2_name (die, cu);
7307 if (name != NULL)
7308 {
7309 if (cu->language == language_cplus
7310 || cu->language == language_java)
7311 {
7312 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7313
7314 /* dwarf2_full_name might have already finished building the DIE's
7315 type. If so, there is no need to continue. */
7316 if (get_die_type (die, cu) != NULL)
7317 return get_die_type (die, cu);
7318
7319 TYPE_TAG_NAME (type) = full_name;
7320 if (die->tag == DW_TAG_structure_type
7321 || die->tag == DW_TAG_class_type)
7322 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7323 }
7324 else
7325 {
7326 /* The name is already allocated along with this objfile, so
7327 we don't need to duplicate it for the type. */
7328 TYPE_TAG_NAME (type) = (char *) name;
7329 if (die->tag == DW_TAG_class_type)
7330 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7331 }
7332 }
7333
7334 if (die->tag == DW_TAG_structure_type)
7335 {
7336 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7337 }
7338 else if (die->tag == DW_TAG_union_type)
7339 {
7340 TYPE_CODE (type) = TYPE_CODE_UNION;
7341 }
7342 else
7343 {
7344 TYPE_CODE (type) = TYPE_CODE_CLASS;
7345 }
7346
7347 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7348 TYPE_DECLARED_CLASS (type) = 1;
7349
7350 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7351 if (attr)
7352 {
7353 TYPE_LENGTH (type) = DW_UNSND (attr);
7354 }
7355 else
7356 {
7357 TYPE_LENGTH (type) = 0;
7358 }
7359
7360 TYPE_STUB_SUPPORTED (type) = 1;
7361 if (die_is_declaration (die, cu))
7362 TYPE_STUB (type) = 1;
7363 else if (attr == NULL && die->child == NULL
7364 && producer_is_realview (cu->producer))
7365 /* RealView does not output the required DW_AT_declaration
7366 on incomplete types. */
7367 TYPE_STUB (type) = 1;
7368
7369 /* We need to add the type field to the die immediately so we don't
7370 infinitely recurse when dealing with pointers to the structure
7371 type within the structure itself. */
7372 set_die_type (die, type, cu);
7373
7374 /* set_die_type should be already done. */
7375 set_descriptive_type (type, die, cu);
7376
7377 return type;
7378 }
7379
7380 /* Finish creating a structure or union type, including filling in
7381 its members and creating a symbol for it. */
7382
7383 static void
7384 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7385 {
7386 struct objfile *objfile = cu->objfile;
7387 struct die_info *child_die = die->child;
7388 struct type *type;
7389
7390 type = get_die_type (die, cu);
7391 if (type == NULL)
7392 type = read_structure_type (die, cu);
7393
7394 if (die->child != NULL && ! die_is_declaration (die, cu))
7395 {
7396 struct field_info fi;
7397 struct die_info *child_die;
7398 VEC (symbolp) *template_args = NULL;
7399 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7400
7401 memset (&fi, 0, sizeof (struct field_info));
7402
7403 child_die = die->child;
7404
7405 while (child_die && child_die->tag)
7406 {
7407 if (child_die->tag == DW_TAG_member
7408 || child_die->tag == DW_TAG_variable)
7409 {
7410 /* NOTE: carlton/2002-11-05: A C++ static data member
7411 should be a DW_TAG_member that is a declaration, but
7412 all versions of G++ as of this writing (so through at
7413 least 3.2.1) incorrectly generate DW_TAG_variable
7414 tags for them instead. */
7415 dwarf2_add_field (&fi, child_die, cu);
7416 }
7417 else if (child_die->tag == DW_TAG_subprogram)
7418 {
7419 /* C++ member function. */
7420 dwarf2_add_member_fn (&fi, child_die, type, cu);
7421 }
7422 else if (child_die->tag == DW_TAG_inheritance)
7423 {
7424 /* C++ base class field. */
7425 dwarf2_add_field (&fi, child_die, cu);
7426 }
7427 else if (child_die->tag == DW_TAG_typedef)
7428 dwarf2_add_typedef (&fi, child_die, cu);
7429 else if (child_die->tag == DW_TAG_template_type_param
7430 || child_die->tag == DW_TAG_template_value_param)
7431 {
7432 struct symbol *arg = new_symbol (child_die, NULL, cu);
7433
7434 if (arg != NULL)
7435 VEC_safe_push (symbolp, template_args, arg);
7436 }
7437
7438 child_die = sibling_die (child_die);
7439 }
7440
7441 /* Attach template arguments to type. */
7442 if (! VEC_empty (symbolp, template_args))
7443 {
7444 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7445 TYPE_N_TEMPLATE_ARGUMENTS (type)
7446 = VEC_length (symbolp, template_args);
7447 TYPE_TEMPLATE_ARGUMENTS (type)
7448 = obstack_alloc (&objfile->objfile_obstack,
7449 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7450 * sizeof (struct symbol *)));
7451 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7452 VEC_address (symbolp, template_args),
7453 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7454 * sizeof (struct symbol *)));
7455 VEC_free (symbolp, template_args);
7456 }
7457
7458 /* Attach fields and member functions to the type. */
7459 if (fi.nfields)
7460 dwarf2_attach_fields_to_type (&fi, type, cu);
7461 if (fi.nfnfields)
7462 {
7463 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7464
7465 /* Get the type which refers to the base class (possibly this
7466 class itself) which contains the vtable pointer for the current
7467 class from the DW_AT_containing_type attribute. This use of
7468 DW_AT_containing_type is a GNU extension. */
7469
7470 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7471 {
7472 struct type *t = die_containing_type (die, cu);
7473
7474 TYPE_VPTR_BASETYPE (type) = t;
7475 if (type == t)
7476 {
7477 int i;
7478
7479 /* Our own class provides vtbl ptr. */
7480 for (i = TYPE_NFIELDS (t) - 1;
7481 i >= TYPE_N_BASECLASSES (t);
7482 --i)
7483 {
7484 char *fieldname = TYPE_FIELD_NAME (t, i);
7485
7486 if (is_vtable_name (fieldname, cu))
7487 {
7488 TYPE_VPTR_FIELDNO (type) = i;
7489 break;
7490 }
7491 }
7492
7493 /* Complain if virtual function table field not found. */
7494 if (i < TYPE_N_BASECLASSES (t))
7495 complaint (&symfile_complaints,
7496 _("virtual function table pointer "
7497 "not found when defining class '%s'"),
7498 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7499 "");
7500 }
7501 else
7502 {
7503 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7504 }
7505 }
7506 else if (cu->producer
7507 && strncmp (cu->producer,
7508 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7509 {
7510 /* The IBM XLC compiler does not provide direct indication
7511 of the containing type, but the vtable pointer is
7512 always named __vfp. */
7513
7514 int i;
7515
7516 for (i = TYPE_NFIELDS (type) - 1;
7517 i >= TYPE_N_BASECLASSES (type);
7518 --i)
7519 {
7520 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7521 {
7522 TYPE_VPTR_FIELDNO (type) = i;
7523 TYPE_VPTR_BASETYPE (type) = type;
7524 break;
7525 }
7526 }
7527 }
7528 }
7529
7530 /* Copy fi.typedef_field_list linked list elements content into the
7531 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7532 if (fi.typedef_field_list)
7533 {
7534 int i = fi.typedef_field_list_count;
7535
7536 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7537 TYPE_TYPEDEF_FIELD_ARRAY (type)
7538 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7539 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7540
7541 /* Reverse the list order to keep the debug info elements order. */
7542 while (--i >= 0)
7543 {
7544 struct typedef_field *dest, *src;
7545
7546 dest = &TYPE_TYPEDEF_FIELD (type, i);
7547 src = &fi.typedef_field_list->field;
7548 fi.typedef_field_list = fi.typedef_field_list->next;
7549 *dest = *src;
7550 }
7551 }
7552
7553 do_cleanups (back_to);
7554
7555 if (HAVE_CPLUS_STRUCT (type))
7556 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7557 }
7558
7559 quirk_gcc_member_function_pointer (type, cu->objfile);
7560
7561 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7562 snapshots) has been known to create a die giving a declaration
7563 for a class that has, as a child, a die giving a definition for a
7564 nested class. So we have to process our children even if the
7565 current die is a declaration. Normally, of course, a declaration
7566 won't have any children at all. */
7567
7568 while (child_die != NULL && child_die->tag)
7569 {
7570 if (child_die->tag == DW_TAG_member
7571 || child_die->tag == DW_TAG_variable
7572 || child_die->tag == DW_TAG_inheritance
7573 || child_die->tag == DW_TAG_template_value_param
7574 || child_die->tag == DW_TAG_template_type_param)
7575 {
7576 /* Do nothing. */
7577 }
7578 else
7579 process_die (child_die, cu);
7580
7581 child_die = sibling_die (child_die);
7582 }
7583
7584 /* Do not consider external references. According to the DWARF standard,
7585 these DIEs are identified by the fact that they have no byte_size
7586 attribute, and a declaration attribute. */
7587 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7588 || !die_is_declaration (die, cu))
7589 new_symbol (die, type, cu);
7590 }
7591
7592 /* Given a DW_AT_enumeration_type die, set its type. We do not
7593 complete the type's fields yet, or create any symbols. */
7594
7595 static struct type *
7596 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7597 {
7598 struct objfile *objfile = cu->objfile;
7599 struct type *type;
7600 struct attribute *attr;
7601 const char *name;
7602
7603 /* If the definition of this type lives in .debug_types, read that type.
7604 Don't follow DW_AT_specification though, that will take us back up
7605 the chain and we want to go down. */
7606 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7607 if (attr)
7608 {
7609 struct dwarf2_cu *type_cu = cu;
7610 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7611
7612 type = read_type_die (type_die, type_cu);
7613
7614 /* TYPE_CU may not be the same as CU.
7615 Ensure TYPE is recorded in CU's type_hash table. */
7616 return set_die_type (die, type, cu);
7617 }
7618
7619 type = alloc_type (objfile);
7620
7621 TYPE_CODE (type) = TYPE_CODE_ENUM;
7622 name = dwarf2_full_name (NULL, die, cu);
7623 if (name != NULL)
7624 TYPE_TAG_NAME (type) = (char *) name;
7625
7626 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7627 if (attr)
7628 {
7629 TYPE_LENGTH (type) = DW_UNSND (attr);
7630 }
7631 else
7632 {
7633 TYPE_LENGTH (type) = 0;
7634 }
7635
7636 /* The enumeration DIE can be incomplete. In Ada, any type can be
7637 declared as private in the package spec, and then defined only
7638 inside the package body. Such types are known as Taft Amendment
7639 Types. When another package uses such a type, an incomplete DIE
7640 may be generated by the compiler. */
7641 if (die_is_declaration (die, cu))
7642 TYPE_STUB (type) = 1;
7643
7644 return set_die_type (die, type, cu);
7645 }
7646
7647 /* Given a pointer to a die which begins an enumeration, process all
7648 the dies that define the members of the enumeration, and create the
7649 symbol for the enumeration type.
7650
7651 NOTE: We reverse the order of the element list. */
7652
7653 static void
7654 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7655 {
7656 struct type *this_type;
7657
7658 this_type = get_die_type (die, cu);
7659 if (this_type == NULL)
7660 this_type = read_enumeration_type (die, cu);
7661
7662 if (die->child != NULL)
7663 {
7664 struct die_info *child_die;
7665 struct symbol *sym;
7666 struct field *fields = NULL;
7667 int num_fields = 0;
7668 int unsigned_enum = 1;
7669 char *name;
7670
7671 child_die = die->child;
7672 while (child_die && child_die->tag)
7673 {
7674 if (child_die->tag != DW_TAG_enumerator)
7675 {
7676 process_die (child_die, cu);
7677 }
7678 else
7679 {
7680 name = dwarf2_name (child_die, cu);
7681 if (name)
7682 {
7683 sym = new_symbol (child_die, this_type, cu);
7684 if (SYMBOL_VALUE (sym) < 0)
7685 unsigned_enum = 0;
7686
7687 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7688 {
7689 fields = (struct field *)
7690 xrealloc (fields,
7691 (num_fields + DW_FIELD_ALLOC_CHUNK)
7692 * sizeof (struct field));
7693 }
7694
7695 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7696 FIELD_TYPE (fields[num_fields]) = NULL;
7697 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7698 FIELD_BITSIZE (fields[num_fields]) = 0;
7699
7700 num_fields++;
7701 }
7702 }
7703
7704 child_die = sibling_die (child_die);
7705 }
7706
7707 if (num_fields)
7708 {
7709 TYPE_NFIELDS (this_type) = num_fields;
7710 TYPE_FIELDS (this_type) = (struct field *)
7711 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7712 memcpy (TYPE_FIELDS (this_type), fields,
7713 sizeof (struct field) * num_fields);
7714 xfree (fields);
7715 }
7716 if (unsigned_enum)
7717 TYPE_UNSIGNED (this_type) = 1;
7718 }
7719
7720 /* If we are reading an enum from a .debug_types unit, and the enum
7721 is a declaration, and the enum is not the signatured type in the
7722 unit, then we do not want to add a symbol for it. Adding a
7723 symbol would in some cases obscure the true definition of the
7724 enum, giving users an incomplete type when the definition is
7725 actually available. Note that we do not want to do this for all
7726 enums which are just declarations, because C++0x allows forward
7727 enum declarations. */
7728 if (cu->per_cu->debug_type_section
7729 && die_is_declaration (die, cu))
7730 {
7731 struct signatured_type *type_sig;
7732
7733 type_sig
7734 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7735 cu->per_cu->debug_type_section,
7736 cu->per_cu->offset);
7737 if (type_sig->type_offset != die->offset)
7738 return;
7739 }
7740
7741 new_symbol (die, this_type, cu);
7742 }
7743
7744 /* Extract all information from a DW_TAG_array_type DIE and put it in
7745 the DIE's type field. For now, this only handles one dimensional
7746 arrays. */
7747
7748 static struct type *
7749 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7750 {
7751 struct objfile *objfile = cu->objfile;
7752 struct die_info *child_die;
7753 struct type *type;
7754 struct type *element_type, *range_type, *index_type;
7755 struct type **range_types = NULL;
7756 struct attribute *attr;
7757 int ndim = 0;
7758 struct cleanup *back_to;
7759 char *name;
7760
7761 element_type = die_type (die, cu);
7762
7763 /* The die_type call above may have already set the type for this DIE. */
7764 type = get_die_type (die, cu);
7765 if (type)
7766 return type;
7767
7768 /* Irix 6.2 native cc creates array types without children for
7769 arrays with unspecified length. */
7770 if (die->child == NULL)
7771 {
7772 index_type = objfile_type (objfile)->builtin_int;
7773 range_type = create_range_type (NULL, index_type, 0, -1);
7774 type = create_array_type (NULL, element_type, range_type);
7775 return set_die_type (die, type, cu);
7776 }
7777
7778 back_to = make_cleanup (null_cleanup, NULL);
7779 child_die = die->child;
7780 while (child_die && child_die->tag)
7781 {
7782 if (child_die->tag == DW_TAG_subrange_type)
7783 {
7784 struct type *child_type = read_type_die (child_die, cu);
7785
7786 if (child_type != NULL)
7787 {
7788 /* The range type was succesfully read. Save it for the
7789 array type creation. */
7790 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7791 {
7792 range_types = (struct type **)
7793 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7794 * sizeof (struct type *));
7795 if (ndim == 0)
7796 make_cleanup (free_current_contents, &range_types);
7797 }
7798 range_types[ndim++] = child_type;
7799 }
7800 }
7801 child_die = sibling_die (child_die);
7802 }
7803
7804 /* Dwarf2 dimensions are output from left to right, create the
7805 necessary array types in backwards order. */
7806
7807 type = element_type;
7808
7809 if (read_array_order (die, cu) == DW_ORD_col_major)
7810 {
7811 int i = 0;
7812
7813 while (i < ndim)
7814 type = create_array_type (NULL, type, range_types[i++]);
7815 }
7816 else
7817 {
7818 while (ndim-- > 0)
7819 type = create_array_type (NULL, type, range_types[ndim]);
7820 }
7821
7822 /* Understand Dwarf2 support for vector types (like they occur on
7823 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7824 array type. This is not part of the Dwarf2/3 standard yet, but a
7825 custom vendor extension. The main difference between a regular
7826 array and the vector variant is that vectors are passed by value
7827 to functions. */
7828 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7829 if (attr)
7830 make_vector_type (type);
7831
7832 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7833 implementation may choose to implement triple vectors using this
7834 attribute. */
7835 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7836 if (attr)
7837 {
7838 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7839 TYPE_LENGTH (type) = DW_UNSND (attr);
7840 else
7841 complaint (&symfile_complaints,
7842 _("DW_AT_byte_size for array type smaller "
7843 "than the total size of elements"));
7844 }
7845
7846 name = dwarf2_name (die, cu);
7847 if (name)
7848 TYPE_NAME (type) = name;
7849
7850 /* Install the type in the die. */
7851 set_die_type (die, type, cu);
7852
7853 /* set_die_type should be already done. */
7854 set_descriptive_type (type, die, cu);
7855
7856 do_cleanups (back_to);
7857
7858 return type;
7859 }
7860
7861 static enum dwarf_array_dim_ordering
7862 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7863 {
7864 struct attribute *attr;
7865
7866 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7867
7868 if (attr) return DW_SND (attr);
7869
7870 /* GNU F77 is a special case, as at 08/2004 array type info is the
7871 opposite order to the dwarf2 specification, but data is still
7872 laid out as per normal fortran.
7873
7874 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7875 version checking. */
7876
7877 if (cu->language == language_fortran
7878 && cu->producer && strstr (cu->producer, "GNU F77"))
7879 {
7880 return DW_ORD_row_major;
7881 }
7882
7883 switch (cu->language_defn->la_array_ordering)
7884 {
7885 case array_column_major:
7886 return DW_ORD_col_major;
7887 case array_row_major:
7888 default:
7889 return DW_ORD_row_major;
7890 };
7891 }
7892
7893 /* Extract all information from a DW_TAG_set_type DIE and put it in
7894 the DIE's type field. */
7895
7896 static struct type *
7897 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7898 {
7899 struct type *domain_type, *set_type;
7900 struct attribute *attr;
7901
7902 domain_type = die_type (die, cu);
7903
7904 /* The die_type call above may have already set the type for this DIE. */
7905 set_type = get_die_type (die, cu);
7906 if (set_type)
7907 return set_type;
7908
7909 set_type = create_set_type (NULL, domain_type);
7910
7911 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7912 if (attr)
7913 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7914
7915 return set_die_type (die, set_type, cu);
7916 }
7917
7918 /* First cut: install each common block member as a global variable. */
7919
7920 static void
7921 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7922 {
7923 struct die_info *child_die;
7924 struct attribute *attr;
7925 struct symbol *sym;
7926 CORE_ADDR base = (CORE_ADDR) 0;
7927
7928 attr = dwarf2_attr (die, DW_AT_location, cu);
7929 if (attr)
7930 {
7931 /* Support the .debug_loc offsets. */
7932 if (attr_form_is_block (attr))
7933 {
7934 base = decode_locdesc (DW_BLOCK (attr), cu);
7935 }
7936 else if (attr_form_is_section_offset (attr))
7937 {
7938 dwarf2_complex_location_expr_complaint ();
7939 }
7940 else
7941 {
7942 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7943 "common block member");
7944 }
7945 }
7946 if (die->child != NULL)
7947 {
7948 child_die = die->child;
7949 while (child_die && child_die->tag)
7950 {
7951 LONGEST offset;
7952
7953 sym = new_symbol (child_die, NULL, cu);
7954 if (sym != NULL
7955 && handle_data_member_location (child_die, cu, &offset))
7956 {
7957 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
7958 add_symbol_to_list (sym, &global_symbols);
7959 }
7960 child_die = sibling_die (child_die);
7961 }
7962 }
7963 }
7964
7965 /* Create a type for a C++ namespace. */
7966
7967 static struct type *
7968 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7969 {
7970 struct objfile *objfile = cu->objfile;
7971 const char *previous_prefix, *name;
7972 int is_anonymous;
7973 struct type *type;
7974
7975 /* For extensions, reuse the type of the original namespace. */
7976 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7977 {
7978 struct die_info *ext_die;
7979 struct dwarf2_cu *ext_cu = cu;
7980
7981 ext_die = dwarf2_extension (die, &ext_cu);
7982 type = read_type_die (ext_die, ext_cu);
7983
7984 /* EXT_CU may not be the same as CU.
7985 Ensure TYPE is recorded in CU's type_hash table. */
7986 return set_die_type (die, type, cu);
7987 }
7988
7989 name = namespace_name (die, &is_anonymous, cu);
7990
7991 /* Now build the name of the current namespace. */
7992
7993 previous_prefix = determine_prefix (die, cu);
7994 if (previous_prefix[0] != '\0')
7995 name = typename_concat (&objfile->objfile_obstack,
7996 previous_prefix, name, 0, cu);
7997
7998 /* Create the type. */
7999 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8000 objfile);
8001 TYPE_NAME (type) = (char *) name;
8002 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8003
8004 return set_die_type (die, type, cu);
8005 }
8006
8007 /* Read a C++ namespace. */
8008
8009 static void
8010 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8011 {
8012 struct objfile *objfile = cu->objfile;
8013 int is_anonymous;
8014
8015 /* Add a symbol associated to this if we haven't seen the namespace
8016 before. Also, add a using directive if it's an anonymous
8017 namespace. */
8018
8019 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8020 {
8021 struct type *type;
8022
8023 type = read_type_die (die, cu);
8024 new_symbol (die, type, cu);
8025
8026 namespace_name (die, &is_anonymous, cu);
8027 if (is_anonymous)
8028 {
8029 const char *previous_prefix = determine_prefix (die, cu);
8030
8031 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8032 NULL, NULL, &objfile->objfile_obstack);
8033 }
8034 }
8035
8036 if (die->child != NULL)
8037 {
8038 struct die_info *child_die = die->child;
8039
8040 while (child_die && child_die->tag)
8041 {
8042 process_die (child_die, cu);
8043 child_die = sibling_die (child_die);
8044 }
8045 }
8046 }
8047
8048 /* Read a Fortran module as type. This DIE can be only a declaration used for
8049 imported module. Still we need that type as local Fortran "use ... only"
8050 declaration imports depend on the created type in determine_prefix. */
8051
8052 static struct type *
8053 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8054 {
8055 struct objfile *objfile = cu->objfile;
8056 char *module_name;
8057 struct type *type;
8058
8059 module_name = dwarf2_name (die, cu);
8060 if (!module_name)
8061 complaint (&symfile_complaints,
8062 _("DW_TAG_module has no name, offset 0x%x"),
8063 die->offset);
8064 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8065
8066 /* determine_prefix uses TYPE_TAG_NAME. */
8067 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8068
8069 return set_die_type (die, type, cu);
8070 }
8071
8072 /* Read a Fortran module. */
8073
8074 static void
8075 read_module (struct die_info *die, struct dwarf2_cu *cu)
8076 {
8077 struct die_info *child_die = die->child;
8078
8079 while (child_die && child_die->tag)
8080 {
8081 process_die (child_die, cu);
8082 child_die = sibling_die (child_die);
8083 }
8084 }
8085
8086 /* Return the name of the namespace represented by DIE. Set
8087 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8088 namespace. */
8089
8090 static const char *
8091 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8092 {
8093 struct die_info *current_die;
8094 const char *name = NULL;
8095
8096 /* Loop through the extensions until we find a name. */
8097
8098 for (current_die = die;
8099 current_die != NULL;
8100 current_die = dwarf2_extension (die, &cu))
8101 {
8102 name = dwarf2_name (current_die, cu);
8103 if (name != NULL)
8104 break;
8105 }
8106
8107 /* Is it an anonymous namespace? */
8108
8109 *is_anonymous = (name == NULL);
8110 if (*is_anonymous)
8111 name = CP_ANONYMOUS_NAMESPACE_STR;
8112
8113 return name;
8114 }
8115
8116 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8117 the user defined type vector. */
8118
8119 static struct type *
8120 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8121 {
8122 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8123 struct comp_unit_head *cu_header = &cu->header;
8124 struct type *type;
8125 struct attribute *attr_byte_size;
8126 struct attribute *attr_address_class;
8127 int byte_size, addr_class;
8128 struct type *target_type;
8129
8130 target_type = die_type (die, cu);
8131
8132 /* The die_type call above may have already set the type for this DIE. */
8133 type = get_die_type (die, cu);
8134 if (type)
8135 return type;
8136
8137 type = lookup_pointer_type (target_type);
8138
8139 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8140 if (attr_byte_size)
8141 byte_size = DW_UNSND (attr_byte_size);
8142 else
8143 byte_size = cu_header->addr_size;
8144
8145 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8146 if (attr_address_class)
8147 addr_class = DW_UNSND (attr_address_class);
8148 else
8149 addr_class = DW_ADDR_none;
8150
8151 /* If the pointer size or address class is different than the
8152 default, create a type variant marked as such and set the
8153 length accordingly. */
8154 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8155 {
8156 if (gdbarch_address_class_type_flags_p (gdbarch))
8157 {
8158 int type_flags;
8159
8160 type_flags = gdbarch_address_class_type_flags
8161 (gdbarch, byte_size, addr_class);
8162 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8163 == 0);
8164 type = make_type_with_address_space (type, type_flags);
8165 }
8166 else if (TYPE_LENGTH (type) != byte_size)
8167 {
8168 complaint (&symfile_complaints,
8169 _("invalid pointer size %d"), byte_size);
8170 }
8171 else
8172 {
8173 /* Should we also complain about unhandled address classes? */
8174 }
8175 }
8176
8177 TYPE_LENGTH (type) = byte_size;
8178 return set_die_type (die, type, cu);
8179 }
8180
8181 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8182 the user defined type vector. */
8183
8184 static struct type *
8185 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8186 {
8187 struct type *type;
8188 struct type *to_type;
8189 struct type *domain;
8190
8191 to_type = die_type (die, cu);
8192 domain = die_containing_type (die, cu);
8193
8194 /* The calls above may have already set the type for this DIE. */
8195 type = get_die_type (die, cu);
8196 if (type)
8197 return type;
8198
8199 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8200 type = lookup_methodptr_type (to_type);
8201 else
8202 type = lookup_memberptr_type (to_type, domain);
8203
8204 return set_die_type (die, type, cu);
8205 }
8206
8207 /* Extract all information from a DW_TAG_reference_type DIE and add to
8208 the user defined type vector. */
8209
8210 static struct type *
8211 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8212 {
8213 struct comp_unit_head *cu_header = &cu->header;
8214 struct type *type, *target_type;
8215 struct attribute *attr;
8216
8217 target_type = die_type (die, cu);
8218
8219 /* The die_type call above may have already set the type for this DIE. */
8220 type = get_die_type (die, cu);
8221 if (type)
8222 return type;
8223
8224 type = lookup_reference_type (target_type);
8225 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8226 if (attr)
8227 {
8228 TYPE_LENGTH (type) = DW_UNSND (attr);
8229 }
8230 else
8231 {
8232 TYPE_LENGTH (type) = cu_header->addr_size;
8233 }
8234 return set_die_type (die, type, cu);
8235 }
8236
8237 static struct type *
8238 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8239 {
8240 struct type *base_type, *cv_type;
8241
8242 base_type = die_type (die, cu);
8243
8244 /* The die_type call above may have already set the type for this DIE. */
8245 cv_type = get_die_type (die, cu);
8246 if (cv_type)
8247 return cv_type;
8248
8249 /* In case the const qualifier is applied to an array type, the element type
8250 is so qualified, not the array type (section 6.7.3 of C99). */
8251 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8252 {
8253 struct type *el_type, *inner_array;
8254
8255 base_type = copy_type (base_type);
8256 inner_array = base_type;
8257
8258 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8259 {
8260 TYPE_TARGET_TYPE (inner_array) =
8261 copy_type (TYPE_TARGET_TYPE (inner_array));
8262 inner_array = TYPE_TARGET_TYPE (inner_array);
8263 }
8264
8265 el_type = TYPE_TARGET_TYPE (inner_array);
8266 TYPE_TARGET_TYPE (inner_array) =
8267 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8268
8269 return set_die_type (die, base_type, cu);
8270 }
8271
8272 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8273 return set_die_type (die, cv_type, cu);
8274 }
8275
8276 static struct type *
8277 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8278 {
8279 struct type *base_type, *cv_type;
8280
8281 base_type = die_type (die, cu);
8282
8283 /* The die_type call above may have already set the type for this DIE. */
8284 cv_type = get_die_type (die, cu);
8285 if (cv_type)
8286 return cv_type;
8287
8288 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8289 return set_die_type (die, cv_type, cu);
8290 }
8291
8292 /* Extract all information from a DW_TAG_string_type DIE and add to
8293 the user defined type vector. It isn't really a user defined type,
8294 but it behaves like one, with other DIE's using an AT_user_def_type
8295 attribute to reference it. */
8296
8297 static struct type *
8298 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8299 {
8300 struct objfile *objfile = cu->objfile;
8301 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8302 struct type *type, *range_type, *index_type, *char_type;
8303 struct attribute *attr;
8304 unsigned int length;
8305
8306 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8307 if (attr)
8308 {
8309 length = DW_UNSND (attr);
8310 }
8311 else
8312 {
8313 /* Check for the DW_AT_byte_size attribute. */
8314 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8315 if (attr)
8316 {
8317 length = DW_UNSND (attr);
8318 }
8319 else
8320 {
8321 length = 1;
8322 }
8323 }
8324
8325 index_type = objfile_type (objfile)->builtin_int;
8326 range_type = create_range_type (NULL, index_type, 1, length);
8327 char_type = language_string_char_type (cu->language_defn, gdbarch);
8328 type = create_string_type (NULL, char_type, range_type);
8329
8330 return set_die_type (die, type, cu);
8331 }
8332
8333 /* Handle DIES due to C code like:
8334
8335 struct foo
8336 {
8337 int (*funcp)(int a, long l);
8338 int b;
8339 };
8340
8341 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8342
8343 static struct type *
8344 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8345 {
8346 struct type *type; /* Type that this function returns. */
8347 struct type *ftype; /* Function that returns above type. */
8348 struct attribute *attr;
8349
8350 type = die_type (die, cu);
8351
8352 /* The die_type call above may have already set the type for this DIE. */
8353 ftype = get_die_type (die, cu);
8354 if (ftype)
8355 return ftype;
8356
8357 ftype = lookup_function_type (type);
8358
8359 /* All functions in C++, Pascal and Java have prototypes. */
8360 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8361 if ((attr && (DW_UNSND (attr) != 0))
8362 || cu->language == language_cplus
8363 || cu->language == language_java
8364 || cu->language == language_pascal)
8365 TYPE_PROTOTYPED (ftype) = 1;
8366 else if (producer_is_realview (cu->producer))
8367 /* RealView does not emit DW_AT_prototyped. We can not
8368 distinguish prototyped and unprototyped functions; default to
8369 prototyped, since that is more common in modern code (and
8370 RealView warns about unprototyped functions). */
8371 TYPE_PROTOTYPED (ftype) = 1;
8372
8373 /* Store the calling convention in the type if it's available in
8374 the subroutine die. Otherwise set the calling convention to
8375 the default value DW_CC_normal. */
8376 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8377 if (attr)
8378 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8379 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8380 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8381 else
8382 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8383
8384 /* We need to add the subroutine type to the die immediately so
8385 we don't infinitely recurse when dealing with parameters
8386 declared as the same subroutine type. */
8387 set_die_type (die, ftype, cu);
8388
8389 if (die->child != NULL)
8390 {
8391 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8392 struct die_info *child_die;
8393 int nparams, iparams;
8394
8395 /* Count the number of parameters.
8396 FIXME: GDB currently ignores vararg functions, but knows about
8397 vararg member functions. */
8398 nparams = 0;
8399 child_die = die->child;
8400 while (child_die && child_die->tag)
8401 {
8402 if (child_die->tag == DW_TAG_formal_parameter)
8403 nparams++;
8404 else if (child_die->tag == DW_TAG_unspecified_parameters)
8405 TYPE_VARARGS (ftype) = 1;
8406 child_die = sibling_die (child_die);
8407 }
8408
8409 /* Allocate storage for parameters and fill them in. */
8410 TYPE_NFIELDS (ftype) = nparams;
8411 TYPE_FIELDS (ftype) = (struct field *)
8412 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8413
8414 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8415 even if we error out during the parameters reading below. */
8416 for (iparams = 0; iparams < nparams; iparams++)
8417 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8418
8419 iparams = 0;
8420 child_die = die->child;
8421 while (child_die && child_die->tag)
8422 {
8423 if (child_die->tag == DW_TAG_formal_parameter)
8424 {
8425 struct type *arg_type;
8426
8427 /* DWARF version 2 has no clean way to discern C++
8428 static and non-static member functions. G++ helps
8429 GDB by marking the first parameter for non-static
8430 member functions (which is the this pointer) as
8431 artificial. We pass this information to
8432 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8433
8434 DWARF version 3 added DW_AT_object_pointer, which GCC
8435 4.5 does not yet generate. */
8436 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8437 if (attr)
8438 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8439 else
8440 {
8441 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8442
8443 /* GCC/43521: In java, the formal parameter
8444 "this" is sometimes not marked with DW_AT_artificial. */
8445 if (cu->language == language_java)
8446 {
8447 const char *name = dwarf2_name (child_die, cu);
8448
8449 if (name && !strcmp (name, "this"))
8450 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8451 }
8452 }
8453 arg_type = die_type (child_die, cu);
8454
8455 /* RealView does not mark THIS as const, which the testsuite
8456 expects. GCC marks THIS as const in method definitions,
8457 but not in the class specifications (GCC PR 43053). */
8458 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8459 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8460 {
8461 int is_this = 0;
8462 struct dwarf2_cu *arg_cu = cu;
8463 const char *name = dwarf2_name (child_die, cu);
8464
8465 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8466 if (attr)
8467 {
8468 /* If the compiler emits this, use it. */
8469 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8470 is_this = 1;
8471 }
8472 else if (name && strcmp (name, "this") == 0)
8473 /* Function definitions will have the argument names. */
8474 is_this = 1;
8475 else if (name == NULL && iparams == 0)
8476 /* Declarations may not have the names, so like
8477 elsewhere in GDB, assume an artificial first
8478 argument is "this". */
8479 is_this = 1;
8480
8481 if (is_this)
8482 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8483 arg_type, 0);
8484 }
8485
8486 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8487 iparams++;
8488 }
8489 child_die = sibling_die (child_die);
8490 }
8491 }
8492
8493 return ftype;
8494 }
8495
8496 static struct type *
8497 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8498 {
8499 struct objfile *objfile = cu->objfile;
8500 const char *name = NULL;
8501 struct type *this_type;
8502
8503 name = dwarf2_full_name (NULL, die, cu);
8504 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8505 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8506 TYPE_NAME (this_type) = (char *) name;
8507 set_die_type (die, this_type, cu);
8508 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8509 return this_type;
8510 }
8511
8512 /* Find a representation of a given base type and install
8513 it in the TYPE field of the die. */
8514
8515 static struct type *
8516 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8517 {
8518 struct objfile *objfile = cu->objfile;
8519 struct type *type;
8520 struct attribute *attr;
8521 int encoding = 0, size = 0;
8522 char *name;
8523 enum type_code code = TYPE_CODE_INT;
8524 int type_flags = 0;
8525 struct type *target_type = NULL;
8526
8527 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8528 if (attr)
8529 {
8530 encoding = DW_UNSND (attr);
8531 }
8532 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8533 if (attr)
8534 {
8535 size = DW_UNSND (attr);
8536 }
8537 name = dwarf2_name (die, cu);
8538 if (!name)
8539 {
8540 complaint (&symfile_complaints,
8541 _("DW_AT_name missing from DW_TAG_base_type"));
8542 }
8543
8544 switch (encoding)
8545 {
8546 case DW_ATE_address:
8547 /* Turn DW_ATE_address into a void * pointer. */
8548 code = TYPE_CODE_PTR;
8549 type_flags |= TYPE_FLAG_UNSIGNED;
8550 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8551 break;
8552 case DW_ATE_boolean:
8553 code = TYPE_CODE_BOOL;
8554 type_flags |= TYPE_FLAG_UNSIGNED;
8555 break;
8556 case DW_ATE_complex_float:
8557 code = TYPE_CODE_COMPLEX;
8558 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8559 break;
8560 case DW_ATE_decimal_float:
8561 code = TYPE_CODE_DECFLOAT;
8562 break;
8563 case DW_ATE_float:
8564 code = TYPE_CODE_FLT;
8565 break;
8566 case DW_ATE_signed:
8567 break;
8568 case DW_ATE_unsigned:
8569 type_flags |= TYPE_FLAG_UNSIGNED;
8570 if (cu->language == language_fortran
8571 && name
8572 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8573 code = TYPE_CODE_CHAR;
8574 break;
8575 case DW_ATE_signed_char:
8576 if (cu->language == language_ada || cu->language == language_m2
8577 || cu->language == language_pascal
8578 || cu->language == language_fortran)
8579 code = TYPE_CODE_CHAR;
8580 break;
8581 case DW_ATE_unsigned_char:
8582 if (cu->language == language_ada || cu->language == language_m2
8583 || cu->language == language_pascal
8584 || cu->language == language_fortran)
8585 code = TYPE_CODE_CHAR;
8586 type_flags |= TYPE_FLAG_UNSIGNED;
8587 break;
8588 case DW_ATE_UTF:
8589 /* We just treat this as an integer and then recognize the
8590 type by name elsewhere. */
8591 break;
8592
8593 default:
8594 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8595 dwarf_type_encoding_name (encoding));
8596 break;
8597 }
8598
8599 type = init_type (code, size, type_flags, NULL, objfile);
8600 TYPE_NAME (type) = name;
8601 TYPE_TARGET_TYPE (type) = target_type;
8602
8603 if (name && strcmp (name, "char") == 0)
8604 TYPE_NOSIGN (type) = 1;
8605
8606 return set_die_type (die, type, cu);
8607 }
8608
8609 /* Read the given DW_AT_subrange DIE. */
8610
8611 static struct type *
8612 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8613 {
8614 struct type *base_type;
8615 struct type *range_type;
8616 struct attribute *attr;
8617 LONGEST low = 0;
8618 LONGEST high = -1;
8619 char *name;
8620 LONGEST negative_mask;
8621
8622 base_type = die_type (die, cu);
8623 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8624 check_typedef (base_type);
8625
8626 /* The die_type call above may have already set the type for this DIE. */
8627 range_type = get_die_type (die, cu);
8628 if (range_type)
8629 return range_type;
8630
8631 if (cu->language == language_fortran)
8632 {
8633 /* FORTRAN implies a lower bound of 1, if not given. */
8634 low = 1;
8635 }
8636
8637 /* FIXME: For variable sized arrays either of these could be
8638 a variable rather than a constant value. We'll allow it,
8639 but we don't know how to handle it. */
8640 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8641 if (attr)
8642 low = dwarf2_get_attr_constant_value (attr, 0);
8643
8644 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8645 if (attr)
8646 {
8647 if (attr_form_is_block (attr) || is_ref_attr (attr))
8648 {
8649 /* GCC encodes arrays with unspecified or dynamic length
8650 with a DW_FORM_block1 attribute or a reference attribute.
8651 FIXME: GDB does not yet know how to handle dynamic
8652 arrays properly, treat them as arrays with unspecified
8653 length for now.
8654
8655 FIXME: jimb/2003-09-22: GDB does not really know
8656 how to handle arrays of unspecified length
8657 either; we just represent them as zero-length
8658 arrays. Choose an appropriate upper bound given
8659 the lower bound we've computed above. */
8660 high = low - 1;
8661 }
8662 else
8663 high = dwarf2_get_attr_constant_value (attr, 1);
8664 }
8665 else
8666 {
8667 attr = dwarf2_attr (die, DW_AT_count, cu);
8668 if (attr)
8669 {
8670 int count = dwarf2_get_attr_constant_value (attr, 1);
8671 high = low + count - 1;
8672 }
8673 else
8674 {
8675 /* Unspecified array length. */
8676 high = low - 1;
8677 }
8678 }
8679
8680 /* Dwarf-2 specifications explicitly allows to create subrange types
8681 without specifying a base type.
8682 In that case, the base type must be set to the type of
8683 the lower bound, upper bound or count, in that order, if any of these
8684 three attributes references an object that has a type.
8685 If no base type is found, the Dwarf-2 specifications say that
8686 a signed integer type of size equal to the size of an address should
8687 be used.
8688 For the following C code: `extern char gdb_int [];'
8689 GCC produces an empty range DIE.
8690 FIXME: muller/2010-05-28: Possible references to object for low bound,
8691 high bound or count are not yet handled by this code. */
8692 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8693 {
8694 struct objfile *objfile = cu->objfile;
8695 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8696 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8697 struct type *int_type = objfile_type (objfile)->builtin_int;
8698
8699 /* Test "int", "long int", and "long long int" objfile types,
8700 and select the first one having a size above or equal to the
8701 architecture address size. */
8702 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8703 base_type = int_type;
8704 else
8705 {
8706 int_type = objfile_type (objfile)->builtin_long;
8707 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8708 base_type = int_type;
8709 else
8710 {
8711 int_type = objfile_type (objfile)->builtin_long_long;
8712 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8713 base_type = int_type;
8714 }
8715 }
8716 }
8717
8718 negative_mask =
8719 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8720 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8721 low |= negative_mask;
8722 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8723 high |= negative_mask;
8724
8725 range_type = create_range_type (NULL, base_type, low, high);
8726
8727 /* Mark arrays with dynamic length at least as an array of unspecified
8728 length. GDB could check the boundary but before it gets implemented at
8729 least allow accessing the array elements. */
8730 if (attr && attr_form_is_block (attr))
8731 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8732
8733 /* Ada expects an empty array on no boundary attributes. */
8734 if (attr == NULL && cu->language != language_ada)
8735 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8736
8737 name = dwarf2_name (die, cu);
8738 if (name)
8739 TYPE_NAME (range_type) = name;
8740
8741 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8742 if (attr)
8743 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8744
8745 set_die_type (die, range_type, cu);
8746
8747 /* set_die_type should be already done. */
8748 set_descriptive_type (range_type, die, cu);
8749
8750 return range_type;
8751 }
8752
8753 static struct type *
8754 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8755 {
8756 struct type *type;
8757
8758 /* For now, we only support the C meaning of an unspecified type: void. */
8759
8760 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8761 TYPE_NAME (type) = dwarf2_name (die, cu);
8762
8763 return set_die_type (die, type, cu);
8764 }
8765
8766 /* Trivial hash function for die_info: the hash value of a DIE
8767 is its offset in .debug_info for this objfile. */
8768
8769 static hashval_t
8770 die_hash (const void *item)
8771 {
8772 const struct die_info *die = item;
8773
8774 return die->offset;
8775 }
8776
8777 /* Trivial comparison function for die_info structures: two DIEs
8778 are equal if they have the same offset. */
8779
8780 static int
8781 die_eq (const void *item_lhs, const void *item_rhs)
8782 {
8783 const struct die_info *die_lhs = item_lhs;
8784 const struct die_info *die_rhs = item_rhs;
8785
8786 return die_lhs->offset == die_rhs->offset;
8787 }
8788
8789 /* Read a whole compilation unit into a linked list of dies. */
8790
8791 static struct die_info *
8792 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8793 {
8794 struct die_reader_specs reader_specs;
8795 int read_abbrevs = 0;
8796 struct cleanup *back_to = NULL;
8797 struct die_info *die;
8798
8799 if (cu->dwarf2_abbrevs == NULL)
8800 {
8801 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8802 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8803 read_abbrevs = 1;
8804 }
8805
8806 gdb_assert (cu->die_hash == NULL);
8807 cu->die_hash
8808 = htab_create_alloc_ex (cu->header.length / 12,
8809 die_hash,
8810 die_eq,
8811 NULL,
8812 &cu->comp_unit_obstack,
8813 hashtab_obstack_allocate,
8814 dummy_obstack_deallocate);
8815
8816 init_cu_die_reader (&reader_specs, cu);
8817
8818 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8819
8820 if (read_abbrevs)
8821 do_cleanups (back_to);
8822
8823 return die;
8824 }
8825
8826 /* Main entry point for reading a DIE and all children.
8827 Read the DIE and dump it if requested. */
8828
8829 static struct die_info *
8830 read_die_and_children (const struct die_reader_specs *reader,
8831 gdb_byte *info_ptr,
8832 gdb_byte **new_info_ptr,
8833 struct die_info *parent)
8834 {
8835 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8836 new_info_ptr, parent);
8837
8838 if (dwarf2_die_debug)
8839 {
8840 fprintf_unfiltered (gdb_stdlog,
8841 "\nRead die from %s of %s:\n",
8842 (reader->cu->per_cu->debug_type_section
8843 ? ".debug_types"
8844 : ".debug_info"),
8845 reader->abfd->filename);
8846 dump_die (result, dwarf2_die_debug);
8847 }
8848
8849 return result;
8850 }
8851
8852 /* Read a single die and all its descendents. Set the die's sibling
8853 field to NULL; set other fields in the die correctly, and set all
8854 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8855 location of the info_ptr after reading all of those dies. PARENT
8856 is the parent of the die in question. */
8857
8858 static struct die_info *
8859 read_die_and_children_1 (const struct die_reader_specs *reader,
8860 gdb_byte *info_ptr,
8861 gdb_byte **new_info_ptr,
8862 struct die_info *parent)
8863 {
8864 struct die_info *die;
8865 gdb_byte *cur_ptr;
8866 int has_children;
8867
8868 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8869 if (die == NULL)
8870 {
8871 *new_info_ptr = cur_ptr;
8872 return NULL;
8873 }
8874 store_in_ref_table (die, reader->cu);
8875
8876 if (has_children)
8877 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8878 else
8879 {
8880 die->child = NULL;
8881 *new_info_ptr = cur_ptr;
8882 }
8883
8884 die->sibling = NULL;
8885 die->parent = parent;
8886 return die;
8887 }
8888
8889 /* Read a die, all of its descendents, and all of its siblings; set
8890 all of the fields of all of the dies correctly. Arguments are as
8891 in read_die_and_children. */
8892
8893 static struct die_info *
8894 read_die_and_siblings (const struct die_reader_specs *reader,
8895 gdb_byte *info_ptr,
8896 gdb_byte **new_info_ptr,
8897 struct die_info *parent)
8898 {
8899 struct die_info *first_die, *last_sibling;
8900 gdb_byte *cur_ptr;
8901
8902 cur_ptr = info_ptr;
8903 first_die = last_sibling = NULL;
8904
8905 while (1)
8906 {
8907 struct die_info *die
8908 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8909
8910 if (die == NULL)
8911 {
8912 *new_info_ptr = cur_ptr;
8913 return first_die;
8914 }
8915
8916 if (!first_die)
8917 first_die = die;
8918 else
8919 last_sibling->sibling = die;
8920
8921 last_sibling = die;
8922 }
8923 }
8924
8925 /* Read the die from the .debug_info section buffer. Set DIEP to
8926 point to a newly allocated die with its information, except for its
8927 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8928 whether the die has children or not. */
8929
8930 static gdb_byte *
8931 read_full_die (const struct die_reader_specs *reader,
8932 struct die_info **diep, gdb_byte *info_ptr,
8933 int *has_children)
8934 {
8935 unsigned int abbrev_number, bytes_read, i, offset;
8936 struct abbrev_info *abbrev;
8937 struct die_info *die;
8938 struct dwarf2_cu *cu = reader->cu;
8939 bfd *abfd = reader->abfd;
8940
8941 offset = info_ptr - reader->buffer;
8942 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8943 info_ptr += bytes_read;
8944 if (!abbrev_number)
8945 {
8946 *diep = NULL;
8947 *has_children = 0;
8948 return info_ptr;
8949 }
8950
8951 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8952 if (!abbrev)
8953 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8954 abbrev_number,
8955 bfd_get_filename (abfd));
8956
8957 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8958 die->offset = offset;
8959 die->tag = abbrev->tag;
8960 die->abbrev = abbrev_number;
8961
8962 die->num_attrs = abbrev->num_attrs;
8963
8964 for (i = 0; i < abbrev->num_attrs; ++i)
8965 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8966 abfd, info_ptr, cu);
8967
8968 *diep = die;
8969 *has_children = abbrev->has_children;
8970 return info_ptr;
8971 }
8972
8973 /* In DWARF version 2, the description of the debugging information is
8974 stored in a separate .debug_abbrev section. Before we read any
8975 dies from a section we read in all abbreviations and install them
8976 in a hash table. This function also sets flags in CU describing
8977 the data found in the abbrev table. */
8978
8979 static void
8980 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8981 {
8982 struct comp_unit_head *cu_header = &cu->header;
8983 gdb_byte *abbrev_ptr;
8984 struct abbrev_info *cur_abbrev;
8985 unsigned int abbrev_number, bytes_read, abbrev_name;
8986 unsigned int abbrev_form, hash_number;
8987 struct attr_abbrev *cur_attrs;
8988 unsigned int allocated_attrs;
8989
8990 /* Initialize dwarf2 abbrevs. */
8991 obstack_init (&cu->abbrev_obstack);
8992 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8993 (ABBREV_HASH_SIZE
8994 * sizeof (struct abbrev_info *)));
8995 memset (cu->dwarf2_abbrevs, 0,
8996 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8997
8998 dwarf2_read_section (dwarf2_per_objfile->objfile,
8999 &dwarf2_per_objfile->abbrev);
9000 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9001 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9002 abbrev_ptr += bytes_read;
9003
9004 allocated_attrs = ATTR_ALLOC_CHUNK;
9005 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9006
9007 /* Loop until we reach an abbrev number of 0. */
9008 while (abbrev_number)
9009 {
9010 cur_abbrev = dwarf_alloc_abbrev (cu);
9011
9012 /* read in abbrev header */
9013 cur_abbrev->number = abbrev_number;
9014 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9015 abbrev_ptr += bytes_read;
9016 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9017 abbrev_ptr += 1;
9018
9019 if (cur_abbrev->tag == DW_TAG_namespace)
9020 cu->has_namespace_info = 1;
9021
9022 /* now read in declarations */
9023 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9024 abbrev_ptr += bytes_read;
9025 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9026 abbrev_ptr += bytes_read;
9027 while (abbrev_name)
9028 {
9029 if (cur_abbrev->num_attrs == allocated_attrs)
9030 {
9031 allocated_attrs += ATTR_ALLOC_CHUNK;
9032 cur_attrs
9033 = xrealloc (cur_attrs, (allocated_attrs
9034 * sizeof (struct attr_abbrev)));
9035 }
9036
9037 /* Record whether this compilation unit might have
9038 inter-compilation-unit references. If we don't know what form
9039 this attribute will have, then it might potentially be a
9040 DW_FORM_ref_addr, so we conservatively expect inter-CU
9041 references. */
9042
9043 if (abbrev_form == DW_FORM_ref_addr
9044 || abbrev_form == DW_FORM_indirect)
9045 cu->has_form_ref_addr = 1;
9046
9047 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9048 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9049 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9050 abbrev_ptr += bytes_read;
9051 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9052 abbrev_ptr += bytes_read;
9053 }
9054
9055 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9056 (cur_abbrev->num_attrs
9057 * sizeof (struct attr_abbrev)));
9058 memcpy (cur_abbrev->attrs, cur_attrs,
9059 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9060
9061 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9062 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9063 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9064
9065 /* Get next abbreviation.
9066 Under Irix6 the abbreviations for a compilation unit are not
9067 always properly terminated with an abbrev number of 0.
9068 Exit loop if we encounter an abbreviation which we have
9069 already read (which means we are about to read the abbreviations
9070 for the next compile unit) or if the end of the abbreviation
9071 table is reached. */
9072 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9073 >= dwarf2_per_objfile->abbrev.size)
9074 break;
9075 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9076 abbrev_ptr += bytes_read;
9077 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9078 break;
9079 }
9080
9081 xfree (cur_attrs);
9082 }
9083
9084 /* Release the memory used by the abbrev table for a compilation unit. */
9085
9086 static void
9087 dwarf2_free_abbrev_table (void *ptr_to_cu)
9088 {
9089 struct dwarf2_cu *cu = ptr_to_cu;
9090
9091 obstack_free (&cu->abbrev_obstack, NULL);
9092 cu->dwarf2_abbrevs = NULL;
9093 }
9094
9095 /* Lookup an abbrev_info structure in the abbrev hash table. */
9096
9097 static struct abbrev_info *
9098 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9099 {
9100 unsigned int hash_number;
9101 struct abbrev_info *abbrev;
9102
9103 hash_number = number % ABBREV_HASH_SIZE;
9104 abbrev = cu->dwarf2_abbrevs[hash_number];
9105
9106 while (abbrev)
9107 {
9108 if (abbrev->number == number)
9109 return abbrev;
9110 else
9111 abbrev = abbrev->next;
9112 }
9113 return NULL;
9114 }
9115
9116 /* Returns nonzero if TAG represents a type that we might generate a partial
9117 symbol for. */
9118
9119 static int
9120 is_type_tag_for_partial (int tag)
9121 {
9122 switch (tag)
9123 {
9124 #if 0
9125 /* Some types that would be reasonable to generate partial symbols for,
9126 that we don't at present. */
9127 case DW_TAG_array_type:
9128 case DW_TAG_file_type:
9129 case DW_TAG_ptr_to_member_type:
9130 case DW_TAG_set_type:
9131 case DW_TAG_string_type:
9132 case DW_TAG_subroutine_type:
9133 #endif
9134 case DW_TAG_base_type:
9135 case DW_TAG_class_type:
9136 case DW_TAG_interface_type:
9137 case DW_TAG_enumeration_type:
9138 case DW_TAG_structure_type:
9139 case DW_TAG_subrange_type:
9140 case DW_TAG_typedef:
9141 case DW_TAG_union_type:
9142 return 1;
9143 default:
9144 return 0;
9145 }
9146 }
9147
9148 /* Load all DIEs that are interesting for partial symbols into memory. */
9149
9150 static struct partial_die_info *
9151 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9152 int building_psymtab, struct dwarf2_cu *cu)
9153 {
9154 struct partial_die_info *part_die;
9155 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9156 struct abbrev_info *abbrev;
9157 unsigned int bytes_read;
9158 unsigned int load_all = 0;
9159
9160 int nesting_level = 1;
9161
9162 parent_die = NULL;
9163 last_die = NULL;
9164
9165 if (cu->per_cu && cu->per_cu->load_all_dies)
9166 load_all = 1;
9167
9168 cu->partial_dies
9169 = htab_create_alloc_ex (cu->header.length / 12,
9170 partial_die_hash,
9171 partial_die_eq,
9172 NULL,
9173 &cu->comp_unit_obstack,
9174 hashtab_obstack_allocate,
9175 dummy_obstack_deallocate);
9176
9177 part_die = obstack_alloc (&cu->comp_unit_obstack,
9178 sizeof (struct partial_die_info));
9179
9180 while (1)
9181 {
9182 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9183
9184 /* A NULL abbrev means the end of a series of children. */
9185 if (abbrev == NULL)
9186 {
9187 if (--nesting_level == 0)
9188 {
9189 /* PART_DIE was probably the last thing allocated on the
9190 comp_unit_obstack, so we could call obstack_free
9191 here. We don't do that because the waste is small,
9192 and will be cleaned up when we're done with this
9193 compilation unit. This way, we're also more robust
9194 against other users of the comp_unit_obstack. */
9195 return first_die;
9196 }
9197 info_ptr += bytes_read;
9198 last_die = parent_die;
9199 parent_die = parent_die->die_parent;
9200 continue;
9201 }
9202
9203 /* Check for template arguments. We never save these; if
9204 they're seen, we just mark the parent, and go on our way. */
9205 if (parent_die != NULL
9206 && cu->language == language_cplus
9207 && (abbrev->tag == DW_TAG_template_type_param
9208 || abbrev->tag == DW_TAG_template_value_param))
9209 {
9210 parent_die->has_template_arguments = 1;
9211
9212 if (!load_all)
9213 {
9214 /* We don't need a partial DIE for the template argument. */
9215 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9216 cu);
9217 continue;
9218 }
9219 }
9220
9221 /* We only recurse into subprograms looking for template arguments.
9222 Skip their other children. */
9223 if (!load_all
9224 && cu->language == language_cplus
9225 && parent_die != NULL
9226 && parent_die->tag == DW_TAG_subprogram)
9227 {
9228 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9229 continue;
9230 }
9231
9232 /* Check whether this DIE is interesting enough to save. Normally
9233 we would not be interested in members here, but there may be
9234 later variables referencing them via DW_AT_specification (for
9235 static members). */
9236 if (!load_all
9237 && !is_type_tag_for_partial (abbrev->tag)
9238 && abbrev->tag != DW_TAG_constant
9239 && abbrev->tag != DW_TAG_enumerator
9240 && abbrev->tag != DW_TAG_subprogram
9241 && abbrev->tag != DW_TAG_lexical_block
9242 && abbrev->tag != DW_TAG_variable
9243 && abbrev->tag != DW_TAG_namespace
9244 && abbrev->tag != DW_TAG_module
9245 && abbrev->tag != DW_TAG_member)
9246 {
9247 /* Otherwise we skip to the next sibling, if any. */
9248 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9249 continue;
9250 }
9251
9252 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9253 buffer, info_ptr, cu);
9254
9255 /* This two-pass algorithm for processing partial symbols has a
9256 high cost in cache pressure. Thus, handle some simple cases
9257 here which cover the majority of C partial symbols. DIEs
9258 which neither have specification tags in them, nor could have
9259 specification tags elsewhere pointing at them, can simply be
9260 processed and discarded.
9261
9262 This segment is also optional; scan_partial_symbols and
9263 add_partial_symbol will handle these DIEs if we chain
9264 them in normally. When compilers which do not emit large
9265 quantities of duplicate debug information are more common,
9266 this code can probably be removed. */
9267
9268 /* Any complete simple types at the top level (pretty much all
9269 of them, for a language without namespaces), can be processed
9270 directly. */
9271 if (parent_die == NULL
9272 && part_die->has_specification == 0
9273 && part_die->is_declaration == 0
9274 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9275 || part_die->tag == DW_TAG_base_type
9276 || part_die->tag == DW_TAG_subrange_type))
9277 {
9278 if (building_psymtab && part_die->name != NULL)
9279 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9280 VAR_DOMAIN, LOC_TYPEDEF,
9281 &cu->objfile->static_psymbols,
9282 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9283 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9284 continue;
9285 }
9286
9287 /* The exception for DW_TAG_typedef with has_children above is
9288 a workaround of GCC PR debug/47510. In the case of this complaint
9289 type_name_no_tag_or_error will error on such types later.
9290
9291 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9292 it could not find the child DIEs referenced later, this is checked
9293 above. In correct DWARF DW_TAG_typedef should have no children. */
9294
9295 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9296 complaint (&symfile_complaints,
9297 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9298 "- DIE at 0x%x [in module %s]"),
9299 part_die->offset, cu->objfile->name);
9300
9301 /* If we're at the second level, and we're an enumerator, and
9302 our parent has no specification (meaning possibly lives in a
9303 namespace elsewhere), then we can add the partial symbol now
9304 instead of queueing it. */
9305 if (part_die->tag == DW_TAG_enumerator
9306 && parent_die != NULL
9307 && parent_die->die_parent == NULL
9308 && parent_die->tag == DW_TAG_enumeration_type
9309 && parent_die->has_specification == 0)
9310 {
9311 if (part_die->name == NULL)
9312 complaint (&symfile_complaints,
9313 _("malformed enumerator DIE ignored"));
9314 else if (building_psymtab)
9315 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9316 VAR_DOMAIN, LOC_CONST,
9317 (cu->language == language_cplus
9318 || cu->language == language_java)
9319 ? &cu->objfile->global_psymbols
9320 : &cu->objfile->static_psymbols,
9321 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9322
9323 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9324 continue;
9325 }
9326
9327 /* We'll save this DIE so link it in. */
9328 part_die->die_parent = parent_die;
9329 part_die->die_sibling = NULL;
9330 part_die->die_child = NULL;
9331
9332 if (last_die && last_die == parent_die)
9333 last_die->die_child = part_die;
9334 else if (last_die)
9335 last_die->die_sibling = part_die;
9336
9337 last_die = part_die;
9338
9339 if (first_die == NULL)
9340 first_die = part_die;
9341
9342 /* Maybe add the DIE to the hash table. Not all DIEs that we
9343 find interesting need to be in the hash table, because we
9344 also have the parent/sibling/child chains; only those that we
9345 might refer to by offset later during partial symbol reading.
9346
9347 For now this means things that might have be the target of a
9348 DW_AT_specification, DW_AT_abstract_origin, or
9349 DW_AT_extension. DW_AT_extension will refer only to
9350 namespaces; DW_AT_abstract_origin refers to functions (and
9351 many things under the function DIE, but we do not recurse
9352 into function DIEs during partial symbol reading) and
9353 possibly variables as well; DW_AT_specification refers to
9354 declarations. Declarations ought to have the DW_AT_declaration
9355 flag. It happens that GCC forgets to put it in sometimes, but
9356 only for functions, not for types.
9357
9358 Adding more things than necessary to the hash table is harmless
9359 except for the performance cost. Adding too few will result in
9360 wasted time in find_partial_die, when we reread the compilation
9361 unit with load_all_dies set. */
9362
9363 if (load_all
9364 || abbrev->tag == DW_TAG_constant
9365 || abbrev->tag == DW_TAG_subprogram
9366 || abbrev->tag == DW_TAG_variable
9367 || abbrev->tag == DW_TAG_namespace
9368 || part_die->is_declaration)
9369 {
9370 void **slot;
9371
9372 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9373 part_die->offset, INSERT);
9374 *slot = part_die;
9375 }
9376
9377 part_die = obstack_alloc (&cu->comp_unit_obstack,
9378 sizeof (struct partial_die_info));
9379
9380 /* For some DIEs we want to follow their children (if any). For C
9381 we have no reason to follow the children of structures; for other
9382 languages we have to, so that we can get at method physnames
9383 to infer fully qualified class names, for DW_AT_specification,
9384 and for C++ template arguments. For C++, we also look one level
9385 inside functions to find template arguments (if the name of the
9386 function does not already contain the template arguments).
9387
9388 For Ada, we need to scan the children of subprograms and lexical
9389 blocks as well because Ada allows the definition of nested
9390 entities that could be interesting for the debugger, such as
9391 nested subprograms for instance. */
9392 if (last_die->has_children
9393 && (load_all
9394 || last_die->tag == DW_TAG_namespace
9395 || last_die->tag == DW_TAG_module
9396 || last_die->tag == DW_TAG_enumeration_type
9397 || (cu->language == language_cplus
9398 && last_die->tag == DW_TAG_subprogram
9399 && (last_die->name == NULL
9400 || strchr (last_die->name, '<') == NULL))
9401 || (cu->language != language_c
9402 && (last_die->tag == DW_TAG_class_type
9403 || last_die->tag == DW_TAG_interface_type
9404 || last_die->tag == DW_TAG_structure_type
9405 || last_die->tag == DW_TAG_union_type))
9406 || (cu->language == language_ada
9407 && (last_die->tag == DW_TAG_subprogram
9408 || last_die->tag == DW_TAG_lexical_block))))
9409 {
9410 nesting_level++;
9411 parent_die = last_die;
9412 continue;
9413 }
9414
9415 /* Otherwise we skip to the next sibling, if any. */
9416 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9417
9418 /* Back to the top, do it again. */
9419 }
9420 }
9421
9422 /* Read a minimal amount of information into the minimal die structure. */
9423
9424 static gdb_byte *
9425 read_partial_die (struct partial_die_info *part_die,
9426 struct abbrev_info *abbrev,
9427 unsigned int abbrev_len, bfd *abfd,
9428 gdb_byte *buffer, gdb_byte *info_ptr,
9429 struct dwarf2_cu *cu)
9430 {
9431 unsigned int i;
9432 struct attribute attr;
9433 int has_low_pc_attr = 0;
9434 int has_high_pc_attr = 0;
9435
9436 memset (part_die, 0, sizeof (struct partial_die_info));
9437
9438 part_die->offset = info_ptr - buffer;
9439
9440 info_ptr += abbrev_len;
9441
9442 if (abbrev == NULL)
9443 return info_ptr;
9444
9445 part_die->tag = abbrev->tag;
9446 part_die->has_children = abbrev->has_children;
9447
9448 for (i = 0; i < abbrev->num_attrs; ++i)
9449 {
9450 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9451
9452 /* Store the data if it is of an attribute we want to keep in a
9453 partial symbol table. */
9454 switch (attr.name)
9455 {
9456 case DW_AT_name:
9457 switch (part_die->tag)
9458 {
9459 case DW_TAG_compile_unit:
9460 case DW_TAG_type_unit:
9461 /* Compilation units have a DW_AT_name that is a filename, not
9462 a source language identifier. */
9463 case DW_TAG_enumeration_type:
9464 case DW_TAG_enumerator:
9465 /* These tags always have simple identifiers already; no need
9466 to canonicalize them. */
9467 part_die->name = DW_STRING (&attr);
9468 break;
9469 default:
9470 part_die->name
9471 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9472 &cu->objfile->objfile_obstack);
9473 break;
9474 }
9475 break;
9476 case DW_AT_linkage_name:
9477 case DW_AT_MIPS_linkage_name:
9478 /* Note that both forms of linkage name might appear. We
9479 assume they will be the same, and we only store the last
9480 one we see. */
9481 if (cu->language == language_ada)
9482 part_die->name = DW_STRING (&attr);
9483 part_die->linkage_name = DW_STRING (&attr);
9484 break;
9485 case DW_AT_low_pc:
9486 has_low_pc_attr = 1;
9487 part_die->lowpc = DW_ADDR (&attr);
9488 break;
9489 case DW_AT_high_pc:
9490 has_high_pc_attr = 1;
9491 part_die->highpc = DW_ADDR (&attr);
9492 break;
9493 case DW_AT_location:
9494 /* Support the .debug_loc offsets. */
9495 if (attr_form_is_block (&attr))
9496 {
9497 part_die->locdesc = DW_BLOCK (&attr);
9498 }
9499 else if (attr_form_is_section_offset (&attr))
9500 {
9501 dwarf2_complex_location_expr_complaint ();
9502 }
9503 else
9504 {
9505 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9506 "partial symbol information");
9507 }
9508 break;
9509 case DW_AT_external:
9510 part_die->is_external = DW_UNSND (&attr);
9511 break;
9512 case DW_AT_declaration:
9513 part_die->is_declaration = DW_UNSND (&attr);
9514 break;
9515 case DW_AT_type:
9516 part_die->has_type = 1;
9517 break;
9518 case DW_AT_abstract_origin:
9519 case DW_AT_specification:
9520 case DW_AT_extension:
9521 part_die->has_specification = 1;
9522 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9523 break;
9524 case DW_AT_sibling:
9525 /* Ignore absolute siblings, they might point outside of
9526 the current compile unit. */
9527 if (attr.form == DW_FORM_ref_addr)
9528 complaint (&symfile_complaints,
9529 _("ignoring absolute DW_AT_sibling"));
9530 else
9531 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9532 break;
9533 case DW_AT_byte_size:
9534 part_die->has_byte_size = 1;
9535 break;
9536 case DW_AT_calling_convention:
9537 /* DWARF doesn't provide a way to identify a program's source-level
9538 entry point. DW_AT_calling_convention attributes are only meant
9539 to describe functions' calling conventions.
9540
9541 However, because it's a necessary piece of information in
9542 Fortran, and because DW_CC_program is the only piece of debugging
9543 information whose definition refers to a 'main program' at all,
9544 several compilers have begun marking Fortran main programs with
9545 DW_CC_program --- even when those functions use the standard
9546 calling conventions.
9547
9548 So until DWARF specifies a way to provide this information and
9549 compilers pick up the new representation, we'll support this
9550 practice. */
9551 if (DW_UNSND (&attr) == DW_CC_program
9552 && cu->language == language_fortran)
9553 {
9554 set_main_name (part_die->name);
9555
9556 /* As this DIE has a static linkage the name would be difficult
9557 to look up later. */
9558 language_of_main = language_fortran;
9559 }
9560 break;
9561 default:
9562 break;
9563 }
9564 }
9565
9566 if (has_low_pc_attr && has_high_pc_attr)
9567 {
9568 /* When using the GNU linker, .gnu.linkonce. sections are used to
9569 eliminate duplicate copies of functions and vtables and such.
9570 The linker will arbitrarily choose one and discard the others.
9571 The AT_*_pc values for such functions refer to local labels in
9572 these sections. If the section from that file was discarded, the
9573 labels are not in the output, so the relocs get a value of 0.
9574 If this is a discarded function, mark the pc bounds as invalid,
9575 so that GDB will ignore it. */
9576 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9577 {
9578 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9579
9580 complaint (&symfile_complaints,
9581 _("DW_AT_low_pc %s is zero "
9582 "for DIE at 0x%x [in module %s]"),
9583 paddress (gdbarch, part_die->lowpc),
9584 part_die->offset, cu->objfile->name);
9585 }
9586 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9587 else if (part_die->lowpc >= part_die->highpc)
9588 {
9589 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9590
9591 complaint (&symfile_complaints,
9592 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9593 "for DIE at 0x%x [in module %s]"),
9594 paddress (gdbarch, part_die->lowpc),
9595 paddress (gdbarch, part_die->highpc),
9596 part_die->offset, cu->objfile->name);
9597 }
9598 else
9599 part_die->has_pc_info = 1;
9600 }
9601
9602 return info_ptr;
9603 }
9604
9605 /* Find a cached partial DIE at OFFSET in CU. */
9606
9607 static struct partial_die_info *
9608 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9609 {
9610 struct partial_die_info *lookup_die = NULL;
9611 struct partial_die_info part_die;
9612
9613 part_die.offset = offset;
9614 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9615
9616 return lookup_die;
9617 }
9618
9619 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9620 except in the case of .debug_types DIEs which do not reference
9621 outside their CU (they do however referencing other types via
9622 DW_FORM_ref_sig8). */
9623
9624 static struct partial_die_info *
9625 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9626 {
9627 struct dwarf2_per_cu_data *per_cu = NULL;
9628 struct partial_die_info *pd = NULL;
9629
9630 if (cu->per_cu->debug_type_section)
9631 {
9632 pd = find_partial_die_in_comp_unit (offset, cu);
9633 if (pd != NULL)
9634 return pd;
9635 goto not_found;
9636 }
9637
9638 if (offset_in_cu_p (&cu->header, offset))
9639 {
9640 pd = find_partial_die_in_comp_unit (offset, cu);
9641 if (pd != NULL)
9642 return pd;
9643 }
9644
9645 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9646
9647 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9648 load_partial_comp_unit (per_cu, cu->objfile);
9649
9650 per_cu->cu->last_used = 0;
9651 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9652
9653 if (pd == NULL && per_cu->load_all_dies == 0)
9654 {
9655 struct cleanup *back_to;
9656 struct partial_die_info comp_unit_die;
9657 struct abbrev_info *abbrev;
9658 unsigned int bytes_read;
9659 char *info_ptr;
9660
9661 per_cu->load_all_dies = 1;
9662
9663 /* Re-read the DIEs. */
9664 back_to = make_cleanup (null_cleanup, 0);
9665 if (per_cu->cu->dwarf2_abbrevs == NULL)
9666 {
9667 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9668 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9669 }
9670 info_ptr = (dwarf2_per_objfile->info.buffer
9671 + per_cu->cu->header.offset
9672 + per_cu->cu->header.first_die_offset);
9673 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9674 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9675 per_cu->cu->objfile->obfd,
9676 dwarf2_per_objfile->info.buffer, info_ptr,
9677 per_cu->cu);
9678 if (comp_unit_die.has_children)
9679 load_partial_dies (per_cu->cu->objfile->obfd,
9680 dwarf2_per_objfile->info.buffer, info_ptr,
9681 0, per_cu->cu);
9682 do_cleanups (back_to);
9683
9684 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9685 }
9686
9687 not_found:
9688
9689 if (pd == NULL)
9690 internal_error (__FILE__, __LINE__,
9691 _("could not find partial DIE 0x%x "
9692 "in cache [from module %s]\n"),
9693 offset, bfd_get_filename (cu->objfile->obfd));
9694 return pd;
9695 }
9696
9697 /* See if we can figure out if the class lives in a namespace. We do
9698 this by looking for a member function; its demangled name will
9699 contain namespace info, if there is any. */
9700
9701 static void
9702 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9703 struct dwarf2_cu *cu)
9704 {
9705 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9706 what template types look like, because the demangler
9707 frequently doesn't give the same name as the debug info. We
9708 could fix this by only using the demangled name to get the
9709 prefix (but see comment in read_structure_type). */
9710
9711 struct partial_die_info *real_pdi;
9712 struct partial_die_info *child_pdi;
9713
9714 /* If this DIE (this DIE's specification, if any) has a parent, then
9715 we should not do this. We'll prepend the parent's fully qualified
9716 name when we create the partial symbol. */
9717
9718 real_pdi = struct_pdi;
9719 while (real_pdi->has_specification)
9720 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9721
9722 if (real_pdi->die_parent != NULL)
9723 return;
9724
9725 for (child_pdi = struct_pdi->die_child;
9726 child_pdi != NULL;
9727 child_pdi = child_pdi->die_sibling)
9728 {
9729 if (child_pdi->tag == DW_TAG_subprogram
9730 && child_pdi->linkage_name != NULL)
9731 {
9732 char *actual_class_name
9733 = language_class_name_from_physname (cu->language_defn,
9734 child_pdi->linkage_name);
9735 if (actual_class_name != NULL)
9736 {
9737 struct_pdi->name
9738 = obsavestring (actual_class_name,
9739 strlen (actual_class_name),
9740 &cu->objfile->objfile_obstack);
9741 xfree (actual_class_name);
9742 }
9743 break;
9744 }
9745 }
9746 }
9747
9748 /* Adjust PART_DIE before generating a symbol for it. This function
9749 may set the is_external flag or change the DIE's name. */
9750
9751 static void
9752 fixup_partial_die (struct partial_die_info *part_die,
9753 struct dwarf2_cu *cu)
9754 {
9755 /* Once we've fixed up a die, there's no point in doing so again.
9756 This also avoids a memory leak if we were to call
9757 guess_partial_die_structure_name multiple times. */
9758 if (part_die->fixup_called)
9759 return;
9760
9761 /* If we found a reference attribute and the DIE has no name, try
9762 to find a name in the referred to DIE. */
9763
9764 if (part_die->name == NULL && part_die->has_specification)
9765 {
9766 struct partial_die_info *spec_die;
9767
9768 spec_die = find_partial_die (part_die->spec_offset, cu);
9769
9770 fixup_partial_die (spec_die, cu);
9771
9772 if (spec_die->name)
9773 {
9774 part_die->name = spec_die->name;
9775
9776 /* Copy DW_AT_external attribute if it is set. */
9777 if (spec_die->is_external)
9778 part_die->is_external = spec_die->is_external;
9779 }
9780 }
9781
9782 /* Set default names for some unnamed DIEs. */
9783
9784 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9785 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
9786
9787 /* If there is no parent die to provide a namespace, and there are
9788 children, see if we can determine the namespace from their linkage
9789 name.
9790 NOTE: We need to do this even if cu->has_namespace_info != 0.
9791 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9792 if (cu->language == language_cplus
9793 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
9794 && part_die->die_parent == NULL
9795 && part_die->has_children
9796 && (part_die->tag == DW_TAG_class_type
9797 || part_die->tag == DW_TAG_structure_type
9798 || part_die->tag == DW_TAG_union_type))
9799 guess_partial_die_structure_name (part_die, cu);
9800
9801 /* GCC might emit a nameless struct or union that has a linkage
9802 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9803 if (part_die->name == NULL
9804 && (part_die->tag == DW_TAG_structure_type
9805 || part_die->tag == DW_TAG_union_type
9806 || part_die->tag == DW_TAG_class_type)
9807 && part_die->linkage_name != NULL)
9808 {
9809 char *demangled;
9810
9811 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9812 if (demangled)
9813 {
9814 part_die->name = obsavestring (demangled, strlen (demangled),
9815 &cu->objfile->objfile_obstack);
9816 xfree (demangled);
9817 }
9818 }
9819
9820 part_die->fixup_called = 1;
9821 }
9822
9823 /* Read an attribute value described by an attribute form. */
9824
9825 static gdb_byte *
9826 read_attribute_value (struct attribute *attr, unsigned form,
9827 bfd *abfd, gdb_byte *info_ptr,
9828 struct dwarf2_cu *cu)
9829 {
9830 struct comp_unit_head *cu_header = &cu->header;
9831 unsigned int bytes_read;
9832 struct dwarf_block *blk;
9833
9834 attr->form = form;
9835 switch (form)
9836 {
9837 case DW_FORM_ref_addr:
9838 if (cu->header.version == 2)
9839 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9840 else
9841 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9842 &cu->header, &bytes_read);
9843 info_ptr += bytes_read;
9844 break;
9845 case DW_FORM_addr:
9846 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9847 info_ptr += bytes_read;
9848 break;
9849 case DW_FORM_block2:
9850 blk = dwarf_alloc_block (cu);
9851 blk->size = read_2_bytes (abfd, info_ptr);
9852 info_ptr += 2;
9853 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9854 info_ptr += blk->size;
9855 DW_BLOCK (attr) = blk;
9856 break;
9857 case DW_FORM_block4:
9858 blk = dwarf_alloc_block (cu);
9859 blk->size = read_4_bytes (abfd, info_ptr);
9860 info_ptr += 4;
9861 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9862 info_ptr += blk->size;
9863 DW_BLOCK (attr) = blk;
9864 break;
9865 case DW_FORM_data2:
9866 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9867 info_ptr += 2;
9868 break;
9869 case DW_FORM_data4:
9870 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9871 info_ptr += 4;
9872 break;
9873 case DW_FORM_data8:
9874 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9875 info_ptr += 8;
9876 break;
9877 case DW_FORM_sec_offset:
9878 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9879 info_ptr += bytes_read;
9880 break;
9881 case DW_FORM_string:
9882 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9883 DW_STRING_IS_CANONICAL (attr) = 0;
9884 info_ptr += bytes_read;
9885 break;
9886 case DW_FORM_strp:
9887 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9888 &bytes_read);
9889 DW_STRING_IS_CANONICAL (attr) = 0;
9890 info_ptr += bytes_read;
9891 break;
9892 case DW_FORM_exprloc:
9893 case DW_FORM_block:
9894 blk = dwarf_alloc_block (cu);
9895 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9896 info_ptr += bytes_read;
9897 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9898 info_ptr += blk->size;
9899 DW_BLOCK (attr) = blk;
9900 break;
9901 case DW_FORM_block1:
9902 blk = dwarf_alloc_block (cu);
9903 blk->size = read_1_byte (abfd, info_ptr);
9904 info_ptr += 1;
9905 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9906 info_ptr += blk->size;
9907 DW_BLOCK (attr) = blk;
9908 break;
9909 case DW_FORM_data1:
9910 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9911 info_ptr += 1;
9912 break;
9913 case DW_FORM_flag:
9914 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9915 info_ptr += 1;
9916 break;
9917 case DW_FORM_flag_present:
9918 DW_UNSND (attr) = 1;
9919 break;
9920 case DW_FORM_sdata:
9921 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9922 info_ptr += bytes_read;
9923 break;
9924 case DW_FORM_udata:
9925 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9926 info_ptr += bytes_read;
9927 break;
9928 case DW_FORM_ref1:
9929 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9930 info_ptr += 1;
9931 break;
9932 case DW_FORM_ref2:
9933 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9934 info_ptr += 2;
9935 break;
9936 case DW_FORM_ref4:
9937 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9938 info_ptr += 4;
9939 break;
9940 case DW_FORM_ref8:
9941 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9942 info_ptr += 8;
9943 break;
9944 case DW_FORM_ref_sig8:
9945 /* Convert the signature to something we can record in DW_UNSND
9946 for later lookup.
9947 NOTE: This is NULL if the type wasn't found. */
9948 DW_SIGNATURED_TYPE (attr) =
9949 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9950 info_ptr += 8;
9951 break;
9952 case DW_FORM_ref_udata:
9953 DW_ADDR (attr) = (cu->header.offset
9954 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9955 info_ptr += bytes_read;
9956 break;
9957 case DW_FORM_indirect:
9958 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9959 info_ptr += bytes_read;
9960 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9961 break;
9962 default:
9963 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9964 dwarf_form_name (form),
9965 bfd_get_filename (abfd));
9966 }
9967
9968 /* We have seen instances where the compiler tried to emit a byte
9969 size attribute of -1 which ended up being encoded as an unsigned
9970 0xffffffff. Although 0xffffffff is technically a valid size value,
9971 an object of this size seems pretty unlikely so we can relatively
9972 safely treat these cases as if the size attribute was invalid and
9973 treat them as zero by default. */
9974 if (attr->name == DW_AT_byte_size
9975 && form == DW_FORM_data4
9976 && DW_UNSND (attr) >= 0xffffffff)
9977 {
9978 complaint
9979 (&symfile_complaints,
9980 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9981 hex_string (DW_UNSND (attr)));
9982 DW_UNSND (attr) = 0;
9983 }
9984
9985 return info_ptr;
9986 }
9987
9988 /* Read an attribute described by an abbreviated attribute. */
9989
9990 static gdb_byte *
9991 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9992 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9993 {
9994 attr->name = abbrev->name;
9995 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9996 }
9997
9998 /* Read dwarf information from a buffer. */
9999
10000 static unsigned int
10001 read_1_byte (bfd *abfd, gdb_byte *buf)
10002 {
10003 return bfd_get_8 (abfd, buf);
10004 }
10005
10006 static int
10007 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10008 {
10009 return bfd_get_signed_8 (abfd, buf);
10010 }
10011
10012 static unsigned int
10013 read_2_bytes (bfd *abfd, gdb_byte *buf)
10014 {
10015 return bfd_get_16 (abfd, buf);
10016 }
10017
10018 static unsigned int
10019 read_4_bytes (bfd *abfd, gdb_byte *buf)
10020 {
10021 return bfd_get_32 (abfd, buf);
10022 }
10023
10024 static ULONGEST
10025 read_8_bytes (bfd *abfd, gdb_byte *buf)
10026 {
10027 return bfd_get_64 (abfd, buf);
10028 }
10029
10030 static CORE_ADDR
10031 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10032 unsigned int *bytes_read)
10033 {
10034 struct comp_unit_head *cu_header = &cu->header;
10035 CORE_ADDR retval = 0;
10036
10037 if (cu_header->signed_addr_p)
10038 {
10039 switch (cu_header->addr_size)
10040 {
10041 case 2:
10042 retval = bfd_get_signed_16 (abfd, buf);
10043 break;
10044 case 4:
10045 retval = bfd_get_signed_32 (abfd, buf);
10046 break;
10047 case 8:
10048 retval = bfd_get_signed_64 (abfd, buf);
10049 break;
10050 default:
10051 internal_error (__FILE__, __LINE__,
10052 _("read_address: bad switch, signed [in module %s]"),
10053 bfd_get_filename (abfd));
10054 }
10055 }
10056 else
10057 {
10058 switch (cu_header->addr_size)
10059 {
10060 case 2:
10061 retval = bfd_get_16 (abfd, buf);
10062 break;
10063 case 4:
10064 retval = bfd_get_32 (abfd, buf);
10065 break;
10066 case 8:
10067 retval = bfd_get_64 (abfd, buf);
10068 break;
10069 default:
10070 internal_error (__FILE__, __LINE__,
10071 _("read_address: bad switch, "
10072 "unsigned [in module %s]"),
10073 bfd_get_filename (abfd));
10074 }
10075 }
10076
10077 *bytes_read = cu_header->addr_size;
10078 return retval;
10079 }
10080
10081 /* Read the initial length from a section. The (draft) DWARF 3
10082 specification allows the initial length to take up either 4 bytes
10083 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10084 bytes describe the length and all offsets will be 8 bytes in length
10085 instead of 4.
10086
10087 An older, non-standard 64-bit format is also handled by this
10088 function. The older format in question stores the initial length
10089 as an 8-byte quantity without an escape value. Lengths greater
10090 than 2^32 aren't very common which means that the initial 4 bytes
10091 is almost always zero. Since a length value of zero doesn't make
10092 sense for the 32-bit format, this initial zero can be considered to
10093 be an escape value which indicates the presence of the older 64-bit
10094 format. As written, the code can't detect (old format) lengths
10095 greater than 4GB. If it becomes necessary to handle lengths
10096 somewhat larger than 4GB, we could allow other small values (such
10097 as the non-sensical values of 1, 2, and 3) to also be used as
10098 escape values indicating the presence of the old format.
10099
10100 The value returned via bytes_read should be used to increment the
10101 relevant pointer after calling read_initial_length().
10102
10103 [ Note: read_initial_length() and read_offset() are based on the
10104 document entitled "DWARF Debugging Information Format", revision
10105 3, draft 8, dated November 19, 2001. This document was obtained
10106 from:
10107
10108 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10109
10110 This document is only a draft and is subject to change. (So beware.)
10111
10112 Details regarding the older, non-standard 64-bit format were
10113 determined empirically by examining 64-bit ELF files produced by
10114 the SGI toolchain on an IRIX 6.5 machine.
10115
10116 - Kevin, July 16, 2002
10117 ] */
10118
10119 static LONGEST
10120 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10121 {
10122 LONGEST length = bfd_get_32 (abfd, buf);
10123
10124 if (length == 0xffffffff)
10125 {
10126 length = bfd_get_64 (abfd, buf + 4);
10127 *bytes_read = 12;
10128 }
10129 else if (length == 0)
10130 {
10131 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10132 length = bfd_get_64 (abfd, buf);
10133 *bytes_read = 8;
10134 }
10135 else
10136 {
10137 *bytes_read = 4;
10138 }
10139
10140 return length;
10141 }
10142
10143 /* Cover function for read_initial_length.
10144 Returns the length of the object at BUF, and stores the size of the
10145 initial length in *BYTES_READ and stores the size that offsets will be in
10146 *OFFSET_SIZE.
10147 If the initial length size is not equivalent to that specified in
10148 CU_HEADER then issue a complaint.
10149 This is useful when reading non-comp-unit headers. */
10150
10151 static LONGEST
10152 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10153 const struct comp_unit_head *cu_header,
10154 unsigned int *bytes_read,
10155 unsigned int *offset_size)
10156 {
10157 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10158
10159 gdb_assert (cu_header->initial_length_size == 4
10160 || cu_header->initial_length_size == 8
10161 || cu_header->initial_length_size == 12);
10162
10163 if (cu_header->initial_length_size != *bytes_read)
10164 complaint (&symfile_complaints,
10165 _("intermixed 32-bit and 64-bit DWARF sections"));
10166
10167 *offset_size = (*bytes_read == 4) ? 4 : 8;
10168 return length;
10169 }
10170
10171 /* Read an offset from the data stream. The size of the offset is
10172 given by cu_header->offset_size. */
10173
10174 static LONGEST
10175 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10176 unsigned int *bytes_read)
10177 {
10178 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10179
10180 *bytes_read = cu_header->offset_size;
10181 return offset;
10182 }
10183
10184 /* Read an offset from the data stream. */
10185
10186 static LONGEST
10187 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10188 {
10189 LONGEST retval = 0;
10190
10191 switch (offset_size)
10192 {
10193 case 4:
10194 retval = bfd_get_32 (abfd, buf);
10195 break;
10196 case 8:
10197 retval = bfd_get_64 (abfd, buf);
10198 break;
10199 default:
10200 internal_error (__FILE__, __LINE__,
10201 _("read_offset_1: bad switch [in module %s]"),
10202 bfd_get_filename (abfd));
10203 }
10204
10205 return retval;
10206 }
10207
10208 static gdb_byte *
10209 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10210 {
10211 /* If the size of a host char is 8 bits, we can return a pointer
10212 to the buffer, otherwise we have to copy the data to a buffer
10213 allocated on the temporary obstack. */
10214 gdb_assert (HOST_CHAR_BIT == 8);
10215 return buf;
10216 }
10217
10218 static char *
10219 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10220 {
10221 /* If the size of a host char is 8 bits, we can return a pointer
10222 to the string, otherwise we have to copy the string to a buffer
10223 allocated on the temporary obstack. */
10224 gdb_assert (HOST_CHAR_BIT == 8);
10225 if (*buf == '\0')
10226 {
10227 *bytes_read_ptr = 1;
10228 return NULL;
10229 }
10230 *bytes_read_ptr = strlen ((char *) buf) + 1;
10231 return (char *) buf;
10232 }
10233
10234 static char *
10235 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10236 {
10237 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10238 if (dwarf2_per_objfile->str.buffer == NULL)
10239 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10240 bfd_get_filename (abfd));
10241 if (str_offset >= dwarf2_per_objfile->str.size)
10242 error (_("DW_FORM_strp pointing outside of "
10243 ".debug_str section [in module %s]"),
10244 bfd_get_filename (abfd));
10245 gdb_assert (HOST_CHAR_BIT == 8);
10246 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10247 return NULL;
10248 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10249 }
10250
10251 static char *
10252 read_indirect_string (bfd *abfd, gdb_byte *buf,
10253 const struct comp_unit_head *cu_header,
10254 unsigned int *bytes_read_ptr)
10255 {
10256 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10257
10258 return read_indirect_string_at_offset (abfd, str_offset);
10259 }
10260
10261 static unsigned long
10262 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10263 {
10264 unsigned long result;
10265 unsigned int num_read;
10266 int i, shift;
10267 unsigned char byte;
10268
10269 result = 0;
10270 shift = 0;
10271 num_read = 0;
10272 i = 0;
10273 while (1)
10274 {
10275 byte = bfd_get_8 (abfd, buf);
10276 buf++;
10277 num_read++;
10278 result |= ((unsigned long)(byte & 127) << shift);
10279 if ((byte & 128) == 0)
10280 {
10281 break;
10282 }
10283 shift += 7;
10284 }
10285 *bytes_read_ptr = num_read;
10286 return result;
10287 }
10288
10289 static long
10290 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10291 {
10292 long result;
10293 int i, shift, num_read;
10294 unsigned char byte;
10295
10296 result = 0;
10297 shift = 0;
10298 num_read = 0;
10299 i = 0;
10300 while (1)
10301 {
10302 byte = bfd_get_8 (abfd, buf);
10303 buf++;
10304 num_read++;
10305 result |= ((long)(byte & 127) << shift);
10306 shift += 7;
10307 if ((byte & 128) == 0)
10308 {
10309 break;
10310 }
10311 }
10312 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10313 result |= -(((long)1) << shift);
10314 *bytes_read_ptr = num_read;
10315 return result;
10316 }
10317
10318 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10319
10320 static gdb_byte *
10321 skip_leb128 (bfd *abfd, gdb_byte *buf)
10322 {
10323 int byte;
10324
10325 while (1)
10326 {
10327 byte = bfd_get_8 (abfd, buf);
10328 buf++;
10329 if ((byte & 128) == 0)
10330 return buf;
10331 }
10332 }
10333
10334 static void
10335 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10336 {
10337 switch (lang)
10338 {
10339 case DW_LANG_C89:
10340 case DW_LANG_C99:
10341 case DW_LANG_C:
10342 cu->language = language_c;
10343 break;
10344 case DW_LANG_C_plus_plus:
10345 cu->language = language_cplus;
10346 break;
10347 case DW_LANG_D:
10348 cu->language = language_d;
10349 break;
10350 case DW_LANG_Fortran77:
10351 case DW_LANG_Fortran90:
10352 case DW_LANG_Fortran95:
10353 cu->language = language_fortran;
10354 break;
10355 case DW_LANG_Mips_Assembler:
10356 cu->language = language_asm;
10357 break;
10358 case DW_LANG_Java:
10359 cu->language = language_java;
10360 break;
10361 case DW_LANG_Ada83:
10362 case DW_LANG_Ada95:
10363 cu->language = language_ada;
10364 break;
10365 case DW_LANG_Modula2:
10366 cu->language = language_m2;
10367 break;
10368 case DW_LANG_Pascal83:
10369 cu->language = language_pascal;
10370 break;
10371 case DW_LANG_ObjC:
10372 cu->language = language_objc;
10373 break;
10374 case DW_LANG_Cobol74:
10375 case DW_LANG_Cobol85:
10376 default:
10377 cu->language = language_minimal;
10378 break;
10379 }
10380 cu->language_defn = language_def (cu->language);
10381 }
10382
10383 /* Return the named attribute or NULL if not there. */
10384
10385 static struct attribute *
10386 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10387 {
10388 unsigned int i;
10389 struct attribute *spec = NULL;
10390
10391 for (i = 0; i < die->num_attrs; ++i)
10392 {
10393 if (die->attrs[i].name == name)
10394 return &die->attrs[i];
10395 if (die->attrs[i].name == DW_AT_specification
10396 || die->attrs[i].name == DW_AT_abstract_origin)
10397 spec = &die->attrs[i];
10398 }
10399
10400 if (spec)
10401 {
10402 die = follow_die_ref (die, spec, &cu);
10403 return dwarf2_attr (die, name, cu);
10404 }
10405
10406 return NULL;
10407 }
10408
10409 /* Return the named attribute or NULL if not there,
10410 but do not follow DW_AT_specification, etc.
10411 This is for use in contexts where we're reading .debug_types dies.
10412 Following DW_AT_specification, DW_AT_abstract_origin will take us
10413 back up the chain, and we want to go down. */
10414
10415 static struct attribute *
10416 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10417 struct dwarf2_cu *cu)
10418 {
10419 unsigned int i;
10420
10421 for (i = 0; i < die->num_attrs; ++i)
10422 if (die->attrs[i].name == name)
10423 return &die->attrs[i];
10424
10425 return NULL;
10426 }
10427
10428 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10429 and holds a non-zero value. This function should only be used for
10430 DW_FORM_flag or DW_FORM_flag_present attributes. */
10431
10432 static int
10433 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10434 {
10435 struct attribute *attr = dwarf2_attr (die, name, cu);
10436
10437 return (attr && DW_UNSND (attr));
10438 }
10439
10440 static int
10441 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10442 {
10443 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10444 which value is non-zero. However, we have to be careful with
10445 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10446 (via dwarf2_flag_true_p) follows this attribute. So we may
10447 end up accidently finding a declaration attribute that belongs
10448 to a different DIE referenced by the specification attribute,
10449 even though the given DIE does not have a declaration attribute. */
10450 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10451 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10452 }
10453
10454 /* Return the die giving the specification for DIE, if there is
10455 one. *SPEC_CU is the CU containing DIE on input, and the CU
10456 containing the return value on output. If there is no
10457 specification, but there is an abstract origin, that is
10458 returned. */
10459
10460 static struct die_info *
10461 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10462 {
10463 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10464 *spec_cu);
10465
10466 if (spec_attr == NULL)
10467 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10468
10469 if (spec_attr == NULL)
10470 return NULL;
10471 else
10472 return follow_die_ref (die, spec_attr, spec_cu);
10473 }
10474
10475 /* Free the line_header structure *LH, and any arrays and strings it
10476 refers to.
10477 NOTE: This is also used as a "cleanup" function. */
10478
10479 static void
10480 free_line_header (struct line_header *lh)
10481 {
10482 if (lh->standard_opcode_lengths)
10483 xfree (lh->standard_opcode_lengths);
10484
10485 /* Remember that all the lh->file_names[i].name pointers are
10486 pointers into debug_line_buffer, and don't need to be freed. */
10487 if (lh->file_names)
10488 xfree (lh->file_names);
10489
10490 /* Similarly for the include directory names. */
10491 if (lh->include_dirs)
10492 xfree (lh->include_dirs);
10493
10494 xfree (lh);
10495 }
10496
10497 /* Add an entry to LH's include directory table. */
10498
10499 static void
10500 add_include_dir (struct line_header *lh, char *include_dir)
10501 {
10502 /* Grow the array if necessary. */
10503 if (lh->include_dirs_size == 0)
10504 {
10505 lh->include_dirs_size = 1; /* for testing */
10506 lh->include_dirs = xmalloc (lh->include_dirs_size
10507 * sizeof (*lh->include_dirs));
10508 }
10509 else if (lh->num_include_dirs >= lh->include_dirs_size)
10510 {
10511 lh->include_dirs_size *= 2;
10512 lh->include_dirs = xrealloc (lh->include_dirs,
10513 (lh->include_dirs_size
10514 * sizeof (*lh->include_dirs)));
10515 }
10516
10517 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10518 }
10519
10520 /* Add an entry to LH's file name table. */
10521
10522 static void
10523 add_file_name (struct line_header *lh,
10524 char *name,
10525 unsigned int dir_index,
10526 unsigned int mod_time,
10527 unsigned int length)
10528 {
10529 struct file_entry *fe;
10530
10531 /* Grow the array if necessary. */
10532 if (lh->file_names_size == 0)
10533 {
10534 lh->file_names_size = 1; /* for testing */
10535 lh->file_names = xmalloc (lh->file_names_size
10536 * sizeof (*lh->file_names));
10537 }
10538 else if (lh->num_file_names >= lh->file_names_size)
10539 {
10540 lh->file_names_size *= 2;
10541 lh->file_names = xrealloc (lh->file_names,
10542 (lh->file_names_size
10543 * sizeof (*lh->file_names)));
10544 }
10545
10546 fe = &lh->file_names[lh->num_file_names++];
10547 fe->name = name;
10548 fe->dir_index = dir_index;
10549 fe->mod_time = mod_time;
10550 fe->length = length;
10551 fe->included_p = 0;
10552 fe->symtab = NULL;
10553 }
10554
10555 /* Read the statement program header starting at OFFSET in
10556 .debug_line, according to the endianness of ABFD. Return a pointer
10557 to a struct line_header, allocated using xmalloc.
10558
10559 NOTE: the strings in the include directory and file name tables of
10560 the returned object point into debug_line_buffer, and must not be
10561 freed. */
10562
10563 static struct line_header *
10564 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10565 struct dwarf2_cu *cu)
10566 {
10567 struct cleanup *back_to;
10568 struct line_header *lh;
10569 gdb_byte *line_ptr;
10570 unsigned int bytes_read, offset_size;
10571 int i;
10572 char *cur_dir, *cur_file;
10573
10574 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10575 if (dwarf2_per_objfile->line.buffer == NULL)
10576 {
10577 complaint (&symfile_complaints, _("missing .debug_line section"));
10578 return 0;
10579 }
10580
10581 /* Make sure that at least there's room for the total_length field.
10582 That could be 12 bytes long, but we're just going to fudge that. */
10583 if (offset + 4 >= dwarf2_per_objfile->line.size)
10584 {
10585 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10586 return 0;
10587 }
10588
10589 lh = xmalloc (sizeof (*lh));
10590 memset (lh, 0, sizeof (*lh));
10591 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10592 (void *) lh);
10593
10594 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10595
10596 /* Read in the header. */
10597 lh->total_length =
10598 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10599 &bytes_read, &offset_size);
10600 line_ptr += bytes_read;
10601 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10602 + dwarf2_per_objfile->line.size))
10603 {
10604 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10605 return 0;
10606 }
10607 lh->statement_program_end = line_ptr + lh->total_length;
10608 lh->version = read_2_bytes (abfd, line_ptr);
10609 line_ptr += 2;
10610 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10611 line_ptr += offset_size;
10612 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10613 line_ptr += 1;
10614 if (lh->version >= 4)
10615 {
10616 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10617 line_ptr += 1;
10618 }
10619 else
10620 lh->maximum_ops_per_instruction = 1;
10621
10622 if (lh->maximum_ops_per_instruction == 0)
10623 {
10624 lh->maximum_ops_per_instruction = 1;
10625 complaint (&symfile_complaints,
10626 _("invalid maximum_ops_per_instruction "
10627 "in `.debug_line' section"));
10628 }
10629
10630 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10631 line_ptr += 1;
10632 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10633 line_ptr += 1;
10634 lh->line_range = read_1_byte (abfd, line_ptr);
10635 line_ptr += 1;
10636 lh->opcode_base = read_1_byte (abfd, line_ptr);
10637 line_ptr += 1;
10638 lh->standard_opcode_lengths
10639 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10640
10641 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10642 for (i = 1; i < lh->opcode_base; ++i)
10643 {
10644 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10645 line_ptr += 1;
10646 }
10647
10648 /* Read directory table. */
10649 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10650 {
10651 line_ptr += bytes_read;
10652 add_include_dir (lh, cur_dir);
10653 }
10654 line_ptr += bytes_read;
10655
10656 /* Read file name table. */
10657 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10658 {
10659 unsigned int dir_index, mod_time, length;
10660
10661 line_ptr += bytes_read;
10662 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10663 line_ptr += bytes_read;
10664 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10665 line_ptr += bytes_read;
10666 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10667 line_ptr += bytes_read;
10668
10669 add_file_name (lh, cur_file, dir_index, mod_time, length);
10670 }
10671 line_ptr += bytes_read;
10672 lh->statement_program_start = line_ptr;
10673
10674 if (line_ptr > (dwarf2_per_objfile->line.buffer
10675 + dwarf2_per_objfile->line.size))
10676 complaint (&symfile_complaints,
10677 _("line number info header doesn't "
10678 "fit in `.debug_line' section"));
10679
10680 discard_cleanups (back_to);
10681 return lh;
10682 }
10683
10684 /* This function exists to work around a bug in certain compilers
10685 (particularly GCC 2.95), in which the first line number marker of a
10686 function does not show up until after the prologue, right before
10687 the second line number marker. This function shifts ADDRESS down
10688 to the beginning of the function if necessary, and is called on
10689 addresses passed to record_line. */
10690
10691 static CORE_ADDR
10692 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10693 {
10694 struct function_range *fn;
10695
10696 /* Find the function_range containing address. */
10697 if (!cu->first_fn)
10698 return address;
10699
10700 if (!cu->cached_fn)
10701 cu->cached_fn = cu->first_fn;
10702
10703 fn = cu->cached_fn;
10704 while (fn)
10705 if (fn->lowpc <= address && fn->highpc > address)
10706 goto found;
10707 else
10708 fn = fn->next;
10709
10710 fn = cu->first_fn;
10711 while (fn && fn != cu->cached_fn)
10712 if (fn->lowpc <= address && fn->highpc > address)
10713 goto found;
10714 else
10715 fn = fn->next;
10716
10717 return address;
10718
10719 found:
10720 if (fn->seen_line)
10721 return address;
10722 if (address != fn->lowpc)
10723 complaint (&symfile_complaints,
10724 _("misplaced first line number at 0x%lx for '%s'"),
10725 (unsigned long) address, fn->name);
10726 fn->seen_line = 1;
10727 return fn->lowpc;
10728 }
10729
10730 /* Subroutine of dwarf_decode_lines to simplify it.
10731 Return the file name of the psymtab for included file FILE_INDEX
10732 in line header LH of PST.
10733 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10734 If space for the result is malloc'd, it will be freed by a cleanup.
10735 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10736
10737 static char *
10738 psymtab_include_file_name (const struct line_header *lh, int file_index,
10739 const struct partial_symtab *pst,
10740 const char *comp_dir)
10741 {
10742 const struct file_entry fe = lh->file_names [file_index];
10743 char *include_name = fe.name;
10744 char *include_name_to_compare = include_name;
10745 char *dir_name = NULL;
10746 const char *pst_filename;
10747 char *copied_name = NULL;
10748 int file_is_pst;
10749
10750 if (fe.dir_index)
10751 dir_name = lh->include_dirs[fe.dir_index - 1];
10752
10753 if (!IS_ABSOLUTE_PATH (include_name)
10754 && (dir_name != NULL || comp_dir != NULL))
10755 {
10756 /* Avoid creating a duplicate psymtab for PST.
10757 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10758 Before we do the comparison, however, we need to account
10759 for DIR_NAME and COMP_DIR.
10760 First prepend dir_name (if non-NULL). If we still don't
10761 have an absolute path prepend comp_dir (if non-NULL).
10762 However, the directory we record in the include-file's
10763 psymtab does not contain COMP_DIR (to match the
10764 corresponding symtab(s)).
10765
10766 Example:
10767
10768 bash$ cd /tmp
10769 bash$ gcc -g ./hello.c
10770 include_name = "hello.c"
10771 dir_name = "."
10772 DW_AT_comp_dir = comp_dir = "/tmp"
10773 DW_AT_name = "./hello.c" */
10774
10775 if (dir_name != NULL)
10776 {
10777 include_name = concat (dir_name, SLASH_STRING,
10778 include_name, (char *)NULL);
10779 include_name_to_compare = include_name;
10780 make_cleanup (xfree, include_name);
10781 }
10782 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10783 {
10784 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10785 include_name, (char *)NULL);
10786 }
10787 }
10788
10789 pst_filename = pst->filename;
10790 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10791 {
10792 copied_name = concat (pst->dirname, SLASH_STRING,
10793 pst_filename, (char *)NULL);
10794 pst_filename = copied_name;
10795 }
10796
10797 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10798
10799 if (include_name_to_compare != include_name)
10800 xfree (include_name_to_compare);
10801 if (copied_name != NULL)
10802 xfree (copied_name);
10803
10804 if (file_is_pst)
10805 return NULL;
10806 return include_name;
10807 }
10808
10809 /* Ignore this record_line request. */
10810
10811 static void
10812 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10813 {
10814 return;
10815 }
10816
10817 /* Decode the Line Number Program (LNP) for the given line_header
10818 structure and CU. The actual information extracted and the type
10819 of structures created from the LNP depends on the value of PST.
10820
10821 1. If PST is NULL, then this procedure uses the data from the program
10822 to create all necessary symbol tables, and their linetables.
10823
10824 2. If PST is not NULL, this procedure reads the program to determine
10825 the list of files included by the unit represented by PST, and
10826 builds all the associated partial symbol tables.
10827
10828 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10829 It is used for relative paths in the line table.
10830 NOTE: When processing partial symtabs (pst != NULL),
10831 comp_dir == pst->dirname.
10832
10833 NOTE: It is important that psymtabs have the same file name (via strcmp)
10834 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10835 symtab we don't use it in the name of the psymtabs we create.
10836 E.g. expand_line_sal requires this when finding psymtabs to expand.
10837 A good testcase for this is mb-inline.exp. */
10838
10839 static void
10840 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10841 struct dwarf2_cu *cu, struct partial_symtab *pst)
10842 {
10843 gdb_byte *line_ptr, *extended_end;
10844 gdb_byte *line_end;
10845 unsigned int bytes_read, extended_len;
10846 unsigned char op_code, extended_op, adj_opcode;
10847 CORE_ADDR baseaddr;
10848 struct objfile *objfile = cu->objfile;
10849 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10850 const int decode_for_pst_p = (pst != NULL);
10851 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10852 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10853 = record_line;
10854
10855 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10856
10857 line_ptr = lh->statement_program_start;
10858 line_end = lh->statement_program_end;
10859
10860 /* Read the statement sequences until there's nothing left. */
10861 while (line_ptr < line_end)
10862 {
10863 /* state machine registers */
10864 CORE_ADDR address = 0;
10865 unsigned int file = 1;
10866 unsigned int line = 1;
10867 unsigned int column = 0;
10868 int is_stmt = lh->default_is_stmt;
10869 int basic_block = 0;
10870 int end_sequence = 0;
10871 CORE_ADDR addr;
10872 unsigned char op_index = 0;
10873
10874 if (!decode_for_pst_p && lh->num_file_names >= file)
10875 {
10876 /* Start a subfile for the current file of the state machine. */
10877 /* lh->include_dirs and lh->file_names are 0-based, but the
10878 directory and file name numbers in the statement program
10879 are 1-based. */
10880 struct file_entry *fe = &lh->file_names[file - 1];
10881 char *dir = NULL;
10882
10883 if (fe->dir_index)
10884 dir = lh->include_dirs[fe->dir_index - 1];
10885
10886 dwarf2_start_subfile (fe->name, dir, comp_dir);
10887 }
10888
10889 /* Decode the table. */
10890 while (!end_sequence)
10891 {
10892 op_code = read_1_byte (abfd, line_ptr);
10893 line_ptr += 1;
10894 if (line_ptr > line_end)
10895 {
10896 dwarf2_debug_line_missing_end_sequence_complaint ();
10897 break;
10898 }
10899
10900 if (op_code >= lh->opcode_base)
10901 {
10902 /* Special operand. */
10903 adj_opcode = op_code - lh->opcode_base;
10904 address += (((op_index + (adj_opcode / lh->line_range))
10905 / lh->maximum_ops_per_instruction)
10906 * lh->minimum_instruction_length);
10907 op_index = ((op_index + (adj_opcode / lh->line_range))
10908 % lh->maximum_ops_per_instruction);
10909 line += lh->line_base + (adj_opcode % lh->line_range);
10910 if (lh->num_file_names < file || file == 0)
10911 dwarf2_debug_line_missing_file_complaint ();
10912 /* For now we ignore lines not starting on an
10913 instruction boundary. */
10914 else if (op_index == 0)
10915 {
10916 lh->file_names[file - 1].included_p = 1;
10917 if (!decode_for_pst_p && is_stmt)
10918 {
10919 if (last_subfile != current_subfile)
10920 {
10921 addr = gdbarch_addr_bits_remove (gdbarch, address);
10922 if (last_subfile)
10923 (*p_record_line) (last_subfile, 0, addr);
10924 last_subfile = current_subfile;
10925 }
10926 /* Append row to matrix using current values. */
10927 addr = check_cu_functions (address, cu);
10928 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10929 (*p_record_line) (current_subfile, line, addr);
10930 }
10931 }
10932 basic_block = 0;
10933 }
10934 else switch (op_code)
10935 {
10936 case DW_LNS_extended_op:
10937 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10938 &bytes_read);
10939 line_ptr += bytes_read;
10940 extended_end = line_ptr + extended_len;
10941 extended_op = read_1_byte (abfd, line_ptr);
10942 line_ptr += 1;
10943 switch (extended_op)
10944 {
10945 case DW_LNE_end_sequence:
10946 p_record_line = record_line;
10947 end_sequence = 1;
10948 break;
10949 case DW_LNE_set_address:
10950 address = read_address (abfd, line_ptr, cu, &bytes_read);
10951
10952 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10953 {
10954 /* This line table is for a function which has been
10955 GCd by the linker. Ignore it. PR gdb/12528 */
10956
10957 long line_offset
10958 = line_ptr - dwarf2_per_objfile->line.buffer;
10959
10960 complaint (&symfile_complaints,
10961 _(".debug_line address at offset 0x%lx is 0 "
10962 "[in module %s]"),
10963 line_offset, cu->objfile->name);
10964 p_record_line = noop_record_line;
10965 }
10966
10967 op_index = 0;
10968 line_ptr += bytes_read;
10969 address += baseaddr;
10970 break;
10971 case DW_LNE_define_file:
10972 {
10973 char *cur_file;
10974 unsigned int dir_index, mod_time, length;
10975
10976 cur_file = read_direct_string (abfd, line_ptr,
10977 &bytes_read);
10978 line_ptr += bytes_read;
10979 dir_index =
10980 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10981 line_ptr += bytes_read;
10982 mod_time =
10983 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10984 line_ptr += bytes_read;
10985 length =
10986 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10987 line_ptr += bytes_read;
10988 add_file_name (lh, cur_file, dir_index, mod_time, length);
10989 }
10990 break;
10991 case DW_LNE_set_discriminator:
10992 /* The discriminator is not interesting to the debugger;
10993 just ignore it. */
10994 line_ptr = extended_end;
10995 break;
10996 default:
10997 complaint (&symfile_complaints,
10998 _("mangled .debug_line section"));
10999 return;
11000 }
11001 /* Make sure that we parsed the extended op correctly. If e.g.
11002 we expected a different address size than the producer used,
11003 we may have read the wrong number of bytes. */
11004 if (line_ptr != extended_end)
11005 {
11006 complaint (&symfile_complaints,
11007 _("mangled .debug_line section"));
11008 return;
11009 }
11010 break;
11011 case DW_LNS_copy:
11012 if (lh->num_file_names < file || file == 0)
11013 dwarf2_debug_line_missing_file_complaint ();
11014 else
11015 {
11016 lh->file_names[file - 1].included_p = 1;
11017 if (!decode_for_pst_p && is_stmt)
11018 {
11019 if (last_subfile != current_subfile)
11020 {
11021 addr = gdbarch_addr_bits_remove (gdbarch, address);
11022 if (last_subfile)
11023 (*p_record_line) (last_subfile, 0, addr);
11024 last_subfile = current_subfile;
11025 }
11026 addr = check_cu_functions (address, cu);
11027 addr = gdbarch_addr_bits_remove (gdbarch, addr);
11028 (*p_record_line) (current_subfile, line, addr);
11029 }
11030 }
11031 basic_block = 0;
11032 break;
11033 case DW_LNS_advance_pc:
11034 {
11035 CORE_ADDR adjust
11036 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11037
11038 address += (((op_index + adjust)
11039 / lh->maximum_ops_per_instruction)
11040 * lh->minimum_instruction_length);
11041 op_index = ((op_index + adjust)
11042 % lh->maximum_ops_per_instruction);
11043 line_ptr += bytes_read;
11044 }
11045 break;
11046 case DW_LNS_advance_line:
11047 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11048 line_ptr += bytes_read;
11049 break;
11050 case DW_LNS_set_file:
11051 {
11052 /* The arrays lh->include_dirs and lh->file_names are
11053 0-based, but the directory and file name numbers in
11054 the statement program are 1-based. */
11055 struct file_entry *fe;
11056 char *dir = NULL;
11057
11058 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11059 line_ptr += bytes_read;
11060 if (lh->num_file_names < file || file == 0)
11061 dwarf2_debug_line_missing_file_complaint ();
11062 else
11063 {
11064 fe = &lh->file_names[file - 1];
11065 if (fe->dir_index)
11066 dir = lh->include_dirs[fe->dir_index - 1];
11067 if (!decode_for_pst_p)
11068 {
11069 last_subfile = current_subfile;
11070 dwarf2_start_subfile (fe->name, dir, comp_dir);
11071 }
11072 }
11073 }
11074 break;
11075 case DW_LNS_set_column:
11076 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11077 line_ptr += bytes_read;
11078 break;
11079 case DW_LNS_negate_stmt:
11080 is_stmt = (!is_stmt);
11081 break;
11082 case DW_LNS_set_basic_block:
11083 basic_block = 1;
11084 break;
11085 /* Add to the address register of the state machine the
11086 address increment value corresponding to special opcode
11087 255. I.e., this value is scaled by the minimum
11088 instruction length since special opcode 255 would have
11089 scaled the increment. */
11090 case DW_LNS_const_add_pc:
11091 {
11092 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11093
11094 address += (((op_index + adjust)
11095 / lh->maximum_ops_per_instruction)
11096 * lh->minimum_instruction_length);
11097 op_index = ((op_index + adjust)
11098 % lh->maximum_ops_per_instruction);
11099 }
11100 break;
11101 case DW_LNS_fixed_advance_pc:
11102 address += read_2_bytes (abfd, line_ptr);
11103 op_index = 0;
11104 line_ptr += 2;
11105 break;
11106 default:
11107 {
11108 /* Unknown standard opcode, ignore it. */
11109 int i;
11110
11111 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11112 {
11113 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11114 line_ptr += bytes_read;
11115 }
11116 }
11117 }
11118 }
11119 if (lh->num_file_names < file || file == 0)
11120 dwarf2_debug_line_missing_file_complaint ();
11121 else
11122 {
11123 lh->file_names[file - 1].included_p = 1;
11124 if (!decode_for_pst_p)
11125 {
11126 addr = gdbarch_addr_bits_remove (gdbarch, address);
11127 (*p_record_line) (current_subfile, 0, addr);
11128 }
11129 }
11130 }
11131
11132 if (decode_for_pst_p)
11133 {
11134 int file_index;
11135
11136 /* Now that we're done scanning the Line Header Program, we can
11137 create the psymtab of each included file. */
11138 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11139 if (lh->file_names[file_index].included_p == 1)
11140 {
11141 char *include_name =
11142 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11143 if (include_name != NULL)
11144 dwarf2_create_include_psymtab (include_name, pst, objfile);
11145 }
11146 }
11147 else
11148 {
11149 /* Make sure a symtab is created for every file, even files
11150 which contain only variables (i.e. no code with associated
11151 line numbers). */
11152
11153 int i;
11154 struct file_entry *fe;
11155
11156 for (i = 0; i < lh->num_file_names; i++)
11157 {
11158 char *dir = NULL;
11159
11160 fe = &lh->file_names[i];
11161 if (fe->dir_index)
11162 dir = lh->include_dirs[fe->dir_index - 1];
11163 dwarf2_start_subfile (fe->name, dir, comp_dir);
11164
11165 /* Skip the main file; we don't need it, and it must be
11166 allocated last, so that it will show up before the
11167 non-primary symtabs in the objfile's symtab list. */
11168 if (current_subfile == first_subfile)
11169 continue;
11170
11171 if (current_subfile->symtab == NULL)
11172 current_subfile->symtab = allocate_symtab (current_subfile->name,
11173 cu->objfile);
11174 fe->symtab = current_subfile->symtab;
11175 }
11176 }
11177 }
11178
11179 /* Start a subfile for DWARF. FILENAME is the name of the file and
11180 DIRNAME the name of the source directory which contains FILENAME
11181 or NULL if not known. COMP_DIR is the compilation directory for the
11182 linetable's compilation unit or NULL if not known.
11183 This routine tries to keep line numbers from identical absolute and
11184 relative file names in a common subfile.
11185
11186 Using the `list' example from the GDB testsuite, which resides in
11187 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11188 of /srcdir/list0.c yields the following debugging information for list0.c:
11189
11190 DW_AT_name: /srcdir/list0.c
11191 DW_AT_comp_dir: /compdir
11192 files.files[0].name: list0.h
11193 files.files[0].dir: /srcdir
11194 files.files[1].name: list0.c
11195 files.files[1].dir: /srcdir
11196
11197 The line number information for list0.c has to end up in a single
11198 subfile, so that `break /srcdir/list0.c:1' works as expected.
11199 start_subfile will ensure that this happens provided that we pass the
11200 concatenation of files.files[1].dir and files.files[1].name as the
11201 subfile's name. */
11202
11203 static void
11204 dwarf2_start_subfile (char *filename, const char *dirname,
11205 const char *comp_dir)
11206 {
11207 char *fullname;
11208
11209 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11210 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11211 second argument to start_subfile. To be consistent, we do the
11212 same here. In order not to lose the line information directory,
11213 we concatenate it to the filename when it makes sense.
11214 Note that the Dwarf3 standard says (speaking of filenames in line
11215 information): ``The directory index is ignored for file names
11216 that represent full path names''. Thus ignoring dirname in the
11217 `else' branch below isn't an issue. */
11218
11219 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11220 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11221 else
11222 fullname = filename;
11223
11224 start_subfile (fullname, comp_dir);
11225
11226 if (fullname != filename)
11227 xfree (fullname);
11228 }
11229
11230 static void
11231 var_decode_location (struct attribute *attr, struct symbol *sym,
11232 struct dwarf2_cu *cu)
11233 {
11234 struct objfile *objfile = cu->objfile;
11235 struct comp_unit_head *cu_header = &cu->header;
11236
11237 /* NOTE drow/2003-01-30: There used to be a comment and some special
11238 code here to turn a symbol with DW_AT_external and a
11239 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11240 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11241 with some versions of binutils) where shared libraries could have
11242 relocations against symbols in their debug information - the
11243 minimal symbol would have the right address, but the debug info
11244 would not. It's no longer necessary, because we will explicitly
11245 apply relocations when we read in the debug information now. */
11246
11247 /* A DW_AT_location attribute with no contents indicates that a
11248 variable has been optimized away. */
11249 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11250 {
11251 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11252 return;
11253 }
11254
11255 /* Handle one degenerate form of location expression specially, to
11256 preserve GDB's previous behavior when section offsets are
11257 specified. If this is just a DW_OP_addr then mark this symbol
11258 as LOC_STATIC. */
11259
11260 if (attr_form_is_block (attr)
11261 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11262 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11263 {
11264 unsigned int dummy;
11265
11266 SYMBOL_VALUE_ADDRESS (sym) =
11267 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11268 SYMBOL_CLASS (sym) = LOC_STATIC;
11269 fixup_symbol_section (sym, objfile);
11270 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11271 SYMBOL_SECTION (sym));
11272 return;
11273 }
11274
11275 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11276 expression evaluator, and use LOC_COMPUTED only when necessary
11277 (i.e. when the value of a register or memory location is
11278 referenced, or a thread-local block, etc.). Then again, it might
11279 not be worthwhile. I'm assuming that it isn't unless performance
11280 or memory numbers show me otherwise. */
11281
11282 dwarf2_symbol_mark_computed (attr, sym, cu);
11283 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11284
11285 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11286 cu->has_loclist = 1;
11287 }
11288
11289 /* Given a pointer to a DWARF information entry, figure out if we need
11290 to make a symbol table entry for it, and if so, create a new entry
11291 and return a pointer to it.
11292 If TYPE is NULL, determine symbol type from the die, otherwise
11293 used the passed type.
11294 If SPACE is not NULL, use it to hold the new symbol. If it is
11295 NULL, allocate a new symbol on the objfile's obstack. */
11296
11297 static struct symbol *
11298 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11299 struct symbol *space)
11300 {
11301 struct objfile *objfile = cu->objfile;
11302 struct symbol *sym = NULL;
11303 char *name;
11304 struct attribute *attr = NULL;
11305 struct attribute *attr2 = NULL;
11306 CORE_ADDR baseaddr;
11307 struct pending **list_to_add = NULL;
11308
11309 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11310
11311 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11312
11313 name = dwarf2_name (die, cu);
11314 if (name)
11315 {
11316 const char *linkagename;
11317 int suppress_add = 0;
11318
11319 if (space)
11320 sym = space;
11321 else
11322 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11323 OBJSTAT (objfile, n_syms++);
11324
11325 /* Cache this symbol's name and the name's demangled form (if any). */
11326 SYMBOL_SET_LANGUAGE (sym, cu->language);
11327 linkagename = dwarf2_physname (name, die, cu);
11328 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11329
11330 /* Fortran does not have mangling standard and the mangling does differ
11331 between gfortran, iFort etc. */
11332 if (cu->language == language_fortran
11333 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11334 symbol_set_demangled_name (&(sym->ginfo),
11335 (char *) dwarf2_full_name (name, die, cu),
11336 NULL);
11337
11338 /* Default assumptions.
11339 Use the passed type or decode it from the die. */
11340 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11341 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11342 if (type != NULL)
11343 SYMBOL_TYPE (sym) = type;
11344 else
11345 SYMBOL_TYPE (sym) = die_type (die, cu);
11346 attr = dwarf2_attr (die,
11347 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11348 cu);
11349 if (attr)
11350 {
11351 SYMBOL_LINE (sym) = DW_UNSND (attr);
11352 }
11353
11354 attr = dwarf2_attr (die,
11355 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11356 cu);
11357 if (attr)
11358 {
11359 int file_index = DW_UNSND (attr);
11360
11361 if (cu->line_header == NULL
11362 || file_index > cu->line_header->num_file_names)
11363 complaint (&symfile_complaints,
11364 _("file index out of range"));
11365 else if (file_index > 0)
11366 {
11367 struct file_entry *fe;
11368
11369 fe = &cu->line_header->file_names[file_index - 1];
11370 SYMBOL_SYMTAB (sym) = fe->symtab;
11371 }
11372 }
11373
11374 switch (die->tag)
11375 {
11376 case DW_TAG_label:
11377 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11378 if (attr)
11379 {
11380 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11381 }
11382 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11383 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11384 SYMBOL_CLASS (sym) = LOC_LABEL;
11385 add_symbol_to_list (sym, cu->list_in_scope);
11386 break;
11387 case DW_TAG_subprogram:
11388 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11389 finish_block. */
11390 SYMBOL_CLASS (sym) = LOC_BLOCK;
11391 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11392 if ((attr2 && (DW_UNSND (attr2) != 0))
11393 || cu->language == language_ada)
11394 {
11395 /* Subprograms marked external are stored as a global symbol.
11396 Ada subprograms, whether marked external or not, are always
11397 stored as a global symbol, because we want to be able to
11398 access them globally. For instance, we want to be able
11399 to break on a nested subprogram without having to
11400 specify the context. */
11401 list_to_add = &global_symbols;
11402 }
11403 else
11404 {
11405 list_to_add = cu->list_in_scope;
11406 }
11407 break;
11408 case DW_TAG_inlined_subroutine:
11409 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11410 finish_block. */
11411 SYMBOL_CLASS (sym) = LOC_BLOCK;
11412 SYMBOL_INLINED (sym) = 1;
11413 /* Do not add the symbol to any lists. It will be found via
11414 BLOCK_FUNCTION from the blockvector. */
11415 break;
11416 case DW_TAG_template_value_param:
11417 suppress_add = 1;
11418 /* Fall through. */
11419 case DW_TAG_constant:
11420 case DW_TAG_variable:
11421 case DW_TAG_member:
11422 /* Compilation with minimal debug info may result in
11423 variables with missing type entries. Change the
11424 misleading `void' type to something sensible. */
11425 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11426 SYMBOL_TYPE (sym)
11427 = objfile_type (objfile)->nodebug_data_symbol;
11428
11429 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11430 /* In the case of DW_TAG_member, we should only be called for
11431 static const members. */
11432 if (die->tag == DW_TAG_member)
11433 {
11434 /* dwarf2_add_field uses die_is_declaration,
11435 so we do the same. */
11436 gdb_assert (die_is_declaration (die, cu));
11437 gdb_assert (attr);
11438 }
11439 if (attr)
11440 {
11441 dwarf2_const_value (attr, sym, cu);
11442 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11443 if (!suppress_add)
11444 {
11445 if (attr2 && (DW_UNSND (attr2) != 0))
11446 list_to_add = &global_symbols;
11447 else
11448 list_to_add = cu->list_in_scope;
11449 }
11450 break;
11451 }
11452 attr = dwarf2_attr (die, DW_AT_location, cu);
11453 if (attr)
11454 {
11455 var_decode_location (attr, sym, cu);
11456 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11457 if (SYMBOL_CLASS (sym) == LOC_STATIC
11458 && SYMBOL_VALUE_ADDRESS (sym) == 0
11459 && !dwarf2_per_objfile->has_section_at_zero)
11460 {
11461 /* When a static variable is eliminated by the linker,
11462 the corresponding debug information is not stripped
11463 out, but the variable address is set to null;
11464 do not add such variables into symbol table. */
11465 }
11466 else if (attr2 && (DW_UNSND (attr2) != 0))
11467 {
11468 /* Workaround gfortran PR debug/40040 - it uses
11469 DW_AT_location for variables in -fPIC libraries which may
11470 get overriden by other libraries/executable and get
11471 a different address. Resolve it by the minimal symbol
11472 which may come from inferior's executable using copy
11473 relocation. Make this workaround only for gfortran as for
11474 other compilers GDB cannot guess the minimal symbol
11475 Fortran mangling kind. */
11476 if (cu->language == language_fortran && die->parent
11477 && die->parent->tag == DW_TAG_module
11478 && cu->producer
11479 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11480 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11481
11482 /* A variable with DW_AT_external is never static,
11483 but it may be block-scoped. */
11484 list_to_add = (cu->list_in_scope == &file_symbols
11485 ? &global_symbols : cu->list_in_scope);
11486 }
11487 else
11488 list_to_add = cu->list_in_scope;
11489 }
11490 else
11491 {
11492 /* We do not know the address of this symbol.
11493 If it is an external symbol and we have type information
11494 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11495 The address of the variable will then be determined from
11496 the minimal symbol table whenever the variable is
11497 referenced. */
11498 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11499 if (attr2 && (DW_UNSND (attr2) != 0)
11500 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11501 {
11502 /* A variable with DW_AT_external is never static, but it
11503 may be block-scoped. */
11504 list_to_add = (cu->list_in_scope == &file_symbols
11505 ? &global_symbols : cu->list_in_scope);
11506
11507 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11508 }
11509 else if (!die_is_declaration (die, cu))
11510 {
11511 /* Use the default LOC_OPTIMIZED_OUT class. */
11512 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11513 if (!suppress_add)
11514 list_to_add = cu->list_in_scope;
11515 }
11516 }
11517 break;
11518 case DW_TAG_formal_parameter:
11519 /* If we are inside a function, mark this as an argument. If
11520 not, we might be looking at an argument to an inlined function
11521 when we do not have enough information to show inlined frames;
11522 pretend it's a local variable in that case so that the user can
11523 still see it. */
11524 if (context_stack_depth > 0
11525 && context_stack[context_stack_depth - 1].name != NULL)
11526 SYMBOL_IS_ARGUMENT (sym) = 1;
11527 attr = dwarf2_attr (die, DW_AT_location, cu);
11528 if (attr)
11529 {
11530 var_decode_location (attr, sym, cu);
11531 }
11532 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11533 if (attr)
11534 {
11535 dwarf2_const_value (attr, sym, cu);
11536 }
11537
11538 list_to_add = cu->list_in_scope;
11539 break;
11540 case DW_TAG_unspecified_parameters:
11541 /* From varargs functions; gdb doesn't seem to have any
11542 interest in this information, so just ignore it for now.
11543 (FIXME?) */
11544 break;
11545 case DW_TAG_template_type_param:
11546 suppress_add = 1;
11547 /* Fall through. */
11548 case DW_TAG_class_type:
11549 case DW_TAG_interface_type:
11550 case DW_TAG_structure_type:
11551 case DW_TAG_union_type:
11552 case DW_TAG_set_type:
11553 case DW_TAG_enumeration_type:
11554 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11555 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11556
11557 {
11558 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11559 really ever be static objects: otherwise, if you try
11560 to, say, break of a class's method and you're in a file
11561 which doesn't mention that class, it won't work unless
11562 the check for all static symbols in lookup_symbol_aux
11563 saves you. See the OtherFileClass tests in
11564 gdb.c++/namespace.exp. */
11565
11566 if (!suppress_add)
11567 {
11568 list_to_add = (cu->list_in_scope == &file_symbols
11569 && (cu->language == language_cplus
11570 || cu->language == language_java)
11571 ? &global_symbols : cu->list_in_scope);
11572
11573 /* The semantics of C++ state that "struct foo {
11574 ... }" also defines a typedef for "foo". A Java
11575 class declaration also defines a typedef for the
11576 class. */
11577 if (cu->language == language_cplus
11578 || cu->language == language_java
11579 || cu->language == language_ada)
11580 {
11581 /* The symbol's name is already allocated along
11582 with this objfile, so we don't need to
11583 duplicate it for the type. */
11584 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11585 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11586 }
11587 }
11588 }
11589 break;
11590 case DW_TAG_typedef:
11591 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11592 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11593 list_to_add = cu->list_in_scope;
11594 break;
11595 case DW_TAG_base_type:
11596 case DW_TAG_subrange_type:
11597 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11598 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11599 list_to_add = cu->list_in_scope;
11600 break;
11601 case DW_TAG_enumerator:
11602 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11603 if (attr)
11604 {
11605 dwarf2_const_value (attr, sym, cu);
11606 }
11607 {
11608 /* NOTE: carlton/2003-11-10: See comment above in the
11609 DW_TAG_class_type, etc. block. */
11610
11611 list_to_add = (cu->list_in_scope == &file_symbols
11612 && (cu->language == language_cplus
11613 || cu->language == language_java)
11614 ? &global_symbols : cu->list_in_scope);
11615 }
11616 break;
11617 case DW_TAG_namespace:
11618 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11619 list_to_add = &global_symbols;
11620 break;
11621 default:
11622 /* Not a tag we recognize. Hopefully we aren't processing
11623 trash data, but since we must specifically ignore things
11624 we don't recognize, there is nothing else we should do at
11625 this point. */
11626 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11627 dwarf_tag_name (die->tag));
11628 break;
11629 }
11630
11631 if (suppress_add)
11632 {
11633 sym->hash_next = objfile->template_symbols;
11634 objfile->template_symbols = sym;
11635 list_to_add = NULL;
11636 }
11637
11638 if (list_to_add != NULL)
11639 add_symbol_to_list (sym, list_to_add);
11640
11641 /* For the benefit of old versions of GCC, check for anonymous
11642 namespaces based on the demangled name. */
11643 if (!processing_has_namespace_info
11644 && cu->language == language_cplus)
11645 cp_scan_for_anonymous_namespaces (sym);
11646 }
11647 return (sym);
11648 }
11649
11650 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11651
11652 static struct symbol *
11653 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11654 {
11655 return new_symbol_full (die, type, cu, NULL);
11656 }
11657
11658 /* Given an attr with a DW_FORM_dataN value in host byte order,
11659 zero-extend it as appropriate for the symbol's type. The DWARF
11660 standard (v4) is not entirely clear about the meaning of using
11661 DW_FORM_dataN for a constant with a signed type, where the type is
11662 wider than the data. The conclusion of a discussion on the DWARF
11663 list was that this is unspecified. We choose to always zero-extend
11664 because that is the interpretation long in use by GCC. */
11665
11666 static gdb_byte *
11667 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11668 const char *name, struct obstack *obstack,
11669 struct dwarf2_cu *cu, long *value, int bits)
11670 {
11671 struct objfile *objfile = cu->objfile;
11672 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11673 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11674 LONGEST l = DW_UNSND (attr);
11675
11676 if (bits < sizeof (*value) * 8)
11677 {
11678 l &= ((LONGEST) 1 << bits) - 1;
11679 *value = l;
11680 }
11681 else if (bits == sizeof (*value) * 8)
11682 *value = l;
11683 else
11684 {
11685 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11686 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11687 return bytes;
11688 }
11689
11690 return NULL;
11691 }
11692
11693 /* Read a constant value from an attribute. Either set *VALUE, or if
11694 the value does not fit in *VALUE, set *BYTES - either already
11695 allocated on the objfile obstack, or newly allocated on OBSTACK,
11696 or, set *BATON, if we translated the constant to a location
11697 expression. */
11698
11699 static void
11700 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11701 const char *name, struct obstack *obstack,
11702 struct dwarf2_cu *cu,
11703 long *value, gdb_byte **bytes,
11704 struct dwarf2_locexpr_baton **baton)
11705 {
11706 struct objfile *objfile = cu->objfile;
11707 struct comp_unit_head *cu_header = &cu->header;
11708 struct dwarf_block *blk;
11709 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11710 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11711
11712 *value = 0;
11713 *bytes = NULL;
11714 *baton = NULL;
11715
11716 switch (attr->form)
11717 {
11718 case DW_FORM_addr:
11719 {
11720 gdb_byte *data;
11721
11722 if (TYPE_LENGTH (type) != cu_header->addr_size)
11723 dwarf2_const_value_length_mismatch_complaint (name,
11724 cu_header->addr_size,
11725 TYPE_LENGTH (type));
11726 /* Symbols of this form are reasonably rare, so we just
11727 piggyback on the existing location code rather than writing
11728 a new implementation of symbol_computed_ops. */
11729 *baton = obstack_alloc (&objfile->objfile_obstack,
11730 sizeof (struct dwarf2_locexpr_baton));
11731 (*baton)->per_cu = cu->per_cu;
11732 gdb_assert ((*baton)->per_cu);
11733
11734 (*baton)->size = 2 + cu_header->addr_size;
11735 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11736 (*baton)->data = data;
11737
11738 data[0] = DW_OP_addr;
11739 store_unsigned_integer (&data[1], cu_header->addr_size,
11740 byte_order, DW_ADDR (attr));
11741 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11742 }
11743 break;
11744 case DW_FORM_string:
11745 case DW_FORM_strp:
11746 /* DW_STRING is already allocated on the objfile obstack, point
11747 directly to it. */
11748 *bytes = (gdb_byte *) DW_STRING (attr);
11749 break;
11750 case DW_FORM_block1:
11751 case DW_FORM_block2:
11752 case DW_FORM_block4:
11753 case DW_FORM_block:
11754 case DW_FORM_exprloc:
11755 blk = DW_BLOCK (attr);
11756 if (TYPE_LENGTH (type) != blk->size)
11757 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11758 TYPE_LENGTH (type));
11759 *bytes = blk->data;
11760 break;
11761
11762 /* The DW_AT_const_value attributes are supposed to carry the
11763 symbol's value "represented as it would be on the target
11764 architecture." By the time we get here, it's already been
11765 converted to host endianness, so we just need to sign- or
11766 zero-extend it as appropriate. */
11767 case DW_FORM_data1:
11768 *bytes = dwarf2_const_value_data (attr, type, name,
11769 obstack, cu, value, 8);
11770 break;
11771 case DW_FORM_data2:
11772 *bytes = dwarf2_const_value_data (attr, type, name,
11773 obstack, cu, value, 16);
11774 break;
11775 case DW_FORM_data4:
11776 *bytes = dwarf2_const_value_data (attr, type, name,
11777 obstack, cu, value, 32);
11778 break;
11779 case DW_FORM_data8:
11780 *bytes = dwarf2_const_value_data (attr, type, name,
11781 obstack, cu, value, 64);
11782 break;
11783
11784 case DW_FORM_sdata:
11785 *value = DW_SND (attr);
11786 break;
11787
11788 case DW_FORM_udata:
11789 *value = DW_UNSND (attr);
11790 break;
11791
11792 default:
11793 complaint (&symfile_complaints,
11794 _("unsupported const value attribute form: '%s'"),
11795 dwarf_form_name (attr->form));
11796 *value = 0;
11797 break;
11798 }
11799 }
11800
11801
11802 /* Copy constant value from an attribute to a symbol. */
11803
11804 static void
11805 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11806 struct dwarf2_cu *cu)
11807 {
11808 struct objfile *objfile = cu->objfile;
11809 struct comp_unit_head *cu_header = &cu->header;
11810 long value;
11811 gdb_byte *bytes;
11812 struct dwarf2_locexpr_baton *baton;
11813
11814 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11815 SYMBOL_PRINT_NAME (sym),
11816 &objfile->objfile_obstack, cu,
11817 &value, &bytes, &baton);
11818
11819 if (baton != NULL)
11820 {
11821 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11822 SYMBOL_LOCATION_BATON (sym) = baton;
11823 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11824 }
11825 else if (bytes != NULL)
11826 {
11827 SYMBOL_VALUE_BYTES (sym) = bytes;
11828 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11829 }
11830 else
11831 {
11832 SYMBOL_VALUE (sym) = value;
11833 SYMBOL_CLASS (sym) = LOC_CONST;
11834 }
11835 }
11836
11837 /* Return the type of the die in question using its DW_AT_type attribute. */
11838
11839 static struct type *
11840 die_type (struct die_info *die, struct dwarf2_cu *cu)
11841 {
11842 struct attribute *type_attr;
11843
11844 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11845 if (!type_attr)
11846 {
11847 /* A missing DW_AT_type represents a void type. */
11848 return objfile_type (cu->objfile)->builtin_void;
11849 }
11850
11851 return lookup_die_type (die, type_attr, cu);
11852 }
11853
11854 /* True iff CU's producer generates GNAT Ada auxiliary information
11855 that allows to find parallel types through that information instead
11856 of having to do expensive parallel lookups by type name. */
11857
11858 static int
11859 need_gnat_info (struct dwarf2_cu *cu)
11860 {
11861 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11862 of GNAT produces this auxiliary information, without any indication
11863 that it is produced. Part of enhancing the FSF version of GNAT
11864 to produce that information will be to put in place an indicator
11865 that we can use in order to determine whether the descriptive type
11866 info is available or not. One suggestion that has been made is
11867 to use a new attribute, attached to the CU die. For now, assume
11868 that the descriptive type info is not available. */
11869 return 0;
11870 }
11871
11872 /* Return the auxiliary type of the die in question using its
11873 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11874 attribute is not present. */
11875
11876 static struct type *
11877 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11878 {
11879 struct attribute *type_attr;
11880
11881 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11882 if (!type_attr)
11883 return NULL;
11884
11885 return lookup_die_type (die, type_attr, cu);
11886 }
11887
11888 /* If DIE has a descriptive_type attribute, then set the TYPE's
11889 descriptive type accordingly. */
11890
11891 static void
11892 set_descriptive_type (struct type *type, struct die_info *die,
11893 struct dwarf2_cu *cu)
11894 {
11895 struct type *descriptive_type = die_descriptive_type (die, cu);
11896
11897 if (descriptive_type)
11898 {
11899 ALLOCATE_GNAT_AUX_TYPE (type);
11900 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11901 }
11902 }
11903
11904 /* Return the containing type of the die in question using its
11905 DW_AT_containing_type attribute. */
11906
11907 static struct type *
11908 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11909 {
11910 struct attribute *type_attr;
11911
11912 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11913 if (!type_attr)
11914 error (_("Dwarf Error: Problem turning containing type into gdb type "
11915 "[in module %s]"), cu->objfile->name);
11916
11917 return lookup_die_type (die, type_attr, cu);
11918 }
11919
11920 /* Look up the type of DIE in CU using its type attribute ATTR.
11921 If there is no type substitute an error marker. */
11922
11923 static struct type *
11924 lookup_die_type (struct die_info *die, struct attribute *attr,
11925 struct dwarf2_cu *cu)
11926 {
11927 struct type *this_type;
11928
11929 /* First see if we have it cached. */
11930
11931 if (is_ref_attr (attr))
11932 {
11933 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11934
11935 this_type = get_die_type_at_offset (offset, cu->per_cu);
11936 }
11937 else if (attr->form == DW_FORM_ref_sig8)
11938 {
11939 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11940 struct dwarf2_cu *sig_cu;
11941 unsigned int offset;
11942
11943 /* sig_type will be NULL if the signatured type is missing from
11944 the debug info. */
11945 if (sig_type == NULL)
11946 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11947 "at 0x%x [in module %s]"),
11948 die->offset, cu->objfile->name);
11949
11950 gdb_assert (sig_type->per_cu.debug_type_section);
11951 offset = sig_type->per_cu.offset + sig_type->type_offset;
11952 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11953 }
11954 else
11955 {
11956 dump_die_for_error (die);
11957 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11958 dwarf_attr_name (attr->name), cu->objfile->name);
11959 }
11960
11961 /* If not cached we need to read it in. */
11962
11963 if (this_type == NULL)
11964 {
11965 struct die_info *type_die;
11966 struct dwarf2_cu *type_cu = cu;
11967
11968 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11969 /* If the type is cached, we should have found it above. */
11970 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11971 this_type = read_type_die_1 (type_die, type_cu);
11972 }
11973
11974 /* If we still don't have a type use an error marker. */
11975
11976 if (this_type == NULL)
11977 {
11978 char *message, *saved;
11979
11980 /* read_type_die already issued a complaint. */
11981 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11982 cu->objfile->name,
11983 cu->header.offset,
11984 die->offset);
11985 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11986 message, strlen (message));
11987 xfree (message);
11988
11989 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11990 }
11991
11992 return this_type;
11993 }
11994
11995 /* Return the type in DIE, CU.
11996 Returns NULL for invalid types.
11997
11998 This first does a lookup in the appropriate type_hash table,
11999 and only reads the die in if necessary.
12000
12001 NOTE: This can be called when reading in partial or full symbols. */
12002
12003 static struct type *
12004 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12005 {
12006 struct type *this_type;
12007
12008 this_type = get_die_type (die, cu);
12009 if (this_type)
12010 return this_type;
12011
12012 return read_type_die_1 (die, cu);
12013 }
12014
12015 /* Read the type in DIE, CU.
12016 Returns NULL for invalid types. */
12017
12018 static struct type *
12019 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12020 {
12021 struct type *this_type = NULL;
12022
12023 switch (die->tag)
12024 {
12025 case DW_TAG_class_type:
12026 case DW_TAG_interface_type:
12027 case DW_TAG_structure_type:
12028 case DW_TAG_union_type:
12029 this_type = read_structure_type (die, cu);
12030 break;
12031 case DW_TAG_enumeration_type:
12032 this_type = read_enumeration_type (die, cu);
12033 break;
12034 case DW_TAG_subprogram:
12035 case DW_TAG_subroutine_type:
12036 case DW_TAG_inlined_subroutine:
12037 this_type = read_subroutine_type (die, cu);
12038 break;
12039 case DW_TAG_array_type:
12040 this_type = read_array_type (die, cu);
12041 break;
12042 case DW_TAG_set_type:
12043 this_type = read_set_type (die, cu);
12044 break;
12045 case DW_TAG_pointer_type:
12046 this_type = read_tag_pointer_type (die, cu);
12047 break;
12048 case DW_TAG_ptr_to_member_type:
12049 this_type = read_tag_ptr_to_member_type (die, cu);
12050 break;
12051 case DW_TAG_reference_type:
12052 this_type = read_tag_reference_type (die, cu);
12053 break;
12054 case DW_TAG_const_type:
12055 this_type = read_tag_const_type (die, cu);
12056 break;
12057 case DW_TAG_volatile_type:
12058 this_type = read_tag_volatile_type (die, cu);
12059 break;
12060 case DW_TAG_string_type:
12061 this_type = read_tag_string_type (die, cu);
12062 break;
12063 case DW_TAG_typedef:
12064 this_type = read_typedef (die, cu);
12065 break;
12066 case DW_TAG_subrange_type:
12067 this_type = read_subrange_type (die, cu);
12068 break;
12069 case DW_TAG_base_type:
12070 this_type = read_base_type (die, cu);
12071 break;
12072 case DW_TAG_unspecified_type:
12073 this_type = read_unspecified_type (die, cu);
12074 break;
12075 case DW_TAG_namespace:
12076 this_type = read_namespace_type (die, cu);
12077 break;
12078 case DW_TAG_module:
12079 this_type = read_module_type (die, cu);
12080 break;
12081 default:
12082 complaint (&symfile_complaints,
12083 _("unexpected tag in read_type_die: '%s'"),
12084 dwarf_tag_name (die->tag));
12085 break;
12086 }
12087
12088 return this_type;
12089 }
12090
12091 /* See if we can figure out if the class lives in a namespace. We do
12092 this by looking for a member function; its demangled name will
12093 contain namespace info, if there is any.
12094 Return the computed name or NULL.
12095 Space for the result is allocated on the objfile's obstack.
12096 This is the full-die version of guess_partial_die_structure_name.
12097 In this case we know DIE has no useful parent. */
12098
12099 static char *
12100 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12101 {
12102 struct die_info *spec_die;
12103 struct dwarf2_cu *spec_cu;
12104 struct die_info *child;
12105
12106 spec_cu = cu;
12107 spec_die = die_specification (die, &spec_cu);
12108 if (spec_die != NULL)
12109 {
12110 die = spec_die;
12111 cu = spec_cu;
12112 }
12113
12114 for (child = die->child;
12115 child != NULL;
12116 child = child->sibling)
12117 {
12118 if (child->tag == DW_TAG_subprogram)
12119 {
12120 struct attribute *attr;
12121
12122 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12123 if (attr == NULL)
12124 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12125 if (attr != NULL)
12126 {
12127 char *actual_name
12128 = language_class_name_from_physname (cu->language_defn,
12129 DW_STRING (attr));
12130 char *name = NULL;
12131
12132 if (actual_name != NULL)
12133 {
12134 char *die_name = dwarf2_name (die, cu);
12135
12136 if (die_name != NULL
12137 && strcmp (die_name, actual_name) != 0)
12138 {
12139 /* Strip off the class name from the full name.
12140 We want the prefix. */
12141 int die_name_len = strlen (die_name);
12142 int actual_name_len = strlen (actual_name);
12143
12144 /* Test for '::' as a sanity check. */
12145 if (actual_name_len > die_name_len + 2
12146 && actual_name[actual_name_len
12147 - die_name_len - 1] == ':')
12148 name =
12149 obsavestring (actual_name,
12150 actual_name_len - die_name_len - 2,
12151 &cu->objfile->objfile_obstack);
12152 }
12153 }
12154 xfree (actual_name);
12155 return name;
12156 }
12157 }
12158 }
12159
12160 return NULL;
12161 }
12162
12163 /* Return the name of the namespace/class that DIE is defined within,
12164 or "" if we can't tell. The caller should not xfree the result.
12165
12166 For example, if we're within the method foo() in the following
12167 code:
12168
12169 namespace N {
12170 class C {
12171 void foo () {
12172 }
12173 };
12174 }
12175
12176 then determine_prefix on foo's die will return "N::C". */
12177
12178 static char *
12179 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12180 {
12181 struct die_info *parent, *spec_die;
12182 struct dwarf2_cu *spec_cu;
12183 struct type *parent_type;
12184
12185 if (cu->language != language_cplus && cu->language != language_java
12186 && cu->language != language_fortran)
12187 return "";
12188
12189 /* We have to be careful in the presence of DW_AT_specification.
12190 For example, with GCC 3.4, given the code
12191
12192 namespace N {
12193 void foo() {
12194 // Definition of N::foo.
12195 }
12196 }
12197
12198 then we'll have a tree of DIEs like this:
12199
12200 1: DW_TAG_compile_unit
12201 2: DW_TAG_namespace // N
12202 3: DW_TAG_subprogram // declaration of N::foo
12203 4: DW_TAG_subprogram // definition of N::foo
12204 DW_AT_specification // refers to die #3
12205
12206 Thus, when processing die #4, we have to pretend that we're in
12207 the context of its DW_AT_specification, namely the contex of die
12208 #3. */
12209 spec_cu = cu;
12210 spec_die = die_specification (die, &spec_cu);
12211 if (spec_die == NULL)
12212 parent = die->parent;
12213 else
12214 {
12215 parent = spec_die->parent;
12216 cu = spec_cu;
12217 }
12218
12219 if (parent == NULL)
12220 return "";
12221 else if (parent->building_fullname)
12222 {
12223 const char *name;
12224 const char *parent_name;
12225
12226 /* It has been seen on RealView 2.2 built binaries,
12227 DW_TAG_template_type_param types actually _defined_ as
12228 children of the parent class:
12229
12230 enum E {};
12231 template class <class Enum> Class{};
12232 Class<enum E> class_e;
12233
12234 1: DW_TAG_class_type (Class)
12235 2: DW_TAG_enumeration_type (E)
12236 3: DW_TAG_enumerator (enum1:0)
12237 3: DW_TAG_enumerator (enum2:1)
12238 ...
12239 2: DW_TAG_template_type_param
12240 DW_AT_type DW_FORM_ref_udata (E)
12241
12242 Besides being broken debug info, it can put GDB into an
12243 infinite loop. Consider:
12244
12245 When we're building the full name for Class<E>, we'll start
12246 at Class, and go look over its template type parameters,
12247 finding E. We'll then try to build the full name of E, and
12248 reach here. We're now trying to build the full name of E,
12249 and look over the parent DIE for containing scope. In the
12250 broken case, if we followed the parent DIE of E, we'd again
12251 find Class, and once again go look at its template type
12252 arguments, etc., etc. Simply don't consider such parent die
12253 as source-level parent of this die (it can't be, the language
12254 doesn't allow it), and break the loop here. */
12255 name = dwarf2_name (die, cu);
12256 parent_name = dwarf2_name (parent, cu);
12257 complaint (&symfile_complaints,
12258 _("template param type '%s' defined within parent '%s'"),
12259 name ? name : "<unknown>",
12260 parent_name ? parent_name : "<unknown>");
12261 return "";
12262 }
12263 else
12264 switch (parent->tag)
12265 {
12266 case DW_TAG_namespace:
12267 parent_type = read_type_die (parent, cu);
12268 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12269 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12270 Work around this problem here. */
12271 if (cu->language == language_cplus
12272 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12273 return "";
12274 /* We give a name to even anonymous namespaces. */
12275 return TYPE_TAG_NAME (parent_type);
12276 case DW_TAG_class_type:
12277 case DW_TAG_interface_type:
12278 case DW_TAG_structure_type:
12279 case DW_TAG_union_type:
12280 case DW_TAG_module:
12281 parent_type = read_type_die (parent, cu);
12282 if (TYPE_TAG_NAME (parent_type) != NULL)
12283 return TYPE_TAG_NAME (parent_type);
12284 else
12285 /* An anonymous structure is only allowed non-static data
12286 members; no typedefs, no member functions, et cetera.
12287 So it does not need a prefix. */
12288 return "";
12289 case DW_TAG_compile_unit:
12290 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12291 if (cu->language == language_cplus
12292 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12293 && die->child != NULL
12294 && (die->tag == DW_TAG_class_type
12295 || die->tag == DW_TAG_structure_type
12296 || die->tag == DW_TAG_union_type))
12297 {
12298 char *name = guess_full_die_structure_name (die, cu);
12299 if (name != NULL)
12300 return name;
12301 }
12302 return "";
12303 default:
12304 return determine_prefix (parent, cu);
12305 }
12306 }
12307
12308 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12309 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12310 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12311 an obconcat, otherwise allocate storage for the result. The CU argument is
12312 used to determine the language and hence, the appropriate separator. */
12313
12314 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12315
12316 static char *
12317 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12318 int physname, struct dwarf2_cu *cu)
12319 {
12320 const char *lead = "";
12321 const char *sep;
12322
12323 if (suffix == NULL || suffix[0] == '\0'
12324 || prefix == NULL || prefix[0] == '\0')
12325 sep = "";
12326 else if (cu->language == language_java)
12327 sep = ".";
12328 else if (cu->language == language_fortran && physname)
12329 {
12330 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12331 DW_AT_MIPS_linkage_name is preferred and used instead. */
12332
12333 lead = "__";
12334 sep = "_MOD_";
12335 }
12336 else
12337 sep = "::";
12338
12339 if (prefix == NULL)
12340 prefix = "";
12341 if (suffix == NULL)
12342 suffix = "";
12343
12344 if (obs == NULL)
12345 {
12346 char *retval
12347 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12348
12349 strcpy (retval, lead);
12350 strcat (retval, prefix);
12351 strcat (retval, sep);
12352 strcat (retval, suffix);
12353 return retval;
12354 }
12355 else
12356 {
12357 /* We have an obstack. */
12358 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12359 }
12360 }
12361
12362 /* Return sibling of die, NULL if no sibling. */
12363
12364 static struct die_info *
12365 sibling_die (struct die_info *die)
12366 {
12367 return die->sibling;
12368 }
12369
12370 /* Get name of a die, return NULL if not found. */
12371
12372 static char *
12373 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12374 struct obstack *obstack)
12375 {
12376 if (name && cu->language == language_cplus)
12377 {
12378 char *canon_name = cp_canonicalize_string (name);
12379
12380 if (canon_name != NULL)
12381 {
12382 if (strcmp (canon_name, name) != 0)
12383 name = obsavestring (canon_name, strlen (canon_name),
12384 obstack);
12385 xfree (canon_name);
12386 }
12387 }
12388
12389 return name;
12390 }
12391
12392 /* Get name of a die, return NULL if not found. */
12393
12394 static char *
12395 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12396 {
12397 struct attribute *attr;
12398
12399 attr = dwarf2_attr (die, DW_AT_name, cu);
12400 if ((!attr || !DW_STRING (attr))
12401 && die->tag != DW_TAG_class_type
12402 && die->tag != DW_TAG_interface_type
12403 && die->tag != DW_TAG_structure_type
12404 && die->tag != DW_TAG_union_type)
12405 return NULL;
12406
12407 switch (die->tag)
12408 {
12409 case DW_TAG_compile_unit:
12410 /* Compilation units have a DW_AT_name that is a filename, not
12411 a source language identifier. */
12412 case DW_TAG_enumeration_type:
12413 case DW_TAG_enumerator:
12414 /* These tags always have simple identifiers already; no need
12415 to canonicalize them. */
12416 return DW_STRING (attr);
12417
12418 case DW_TAG_subprogram:
12419 /* Java constructors will all be named "<init>", so return
12420 the class name when we see this special case. */
12421 if (cu->language == language_java
12422 && DW_STRING (attr) != NULL
12423 && strcmp (DW_STRING (attr), "<init>") == 0)
12424 {
12425 struct dwarf2_cu *spec_cu = cu;
12426 struct die_info *spec_die;
12427
12428 /* GCJ will output '<init>' for Java constructor names.
12429 For this special case, return the name of the parent class. */
12430
12431 /* GCJ may output suprogram DIEs with AT_specification set.
12432 If so, use the name of the specified DIE. */
12433 spec_die = die_specification (die, &spec_cu);
12434 if (spec_die != NULL)
12435 return dwarf2_name (spec_die, spec_cu);
12436
12437 do
12438 {
12439 die = die->parent;
12440 if (die->tag == DW_TAG_class_type)
12441 return dwarf2_name (die, cu);
12442 }
12443 while (die->tag != DW_TAG_compile_unit);
12444 }
12445 break;
12446
12447 case DW_TAG_class_type:
12448 case DW_TAG_interface_type:
12449 case DW_TAG_structure_type:
12450 case DW_TAG_union_type:
12451 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12452 structures or unions. These were of the form "._%d" in GCC 4.1,
12453 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12454 and GCC 4.4. We work around this problem by ignoring these. */
12455 if (attr && DW_STRING (attr)
12456 && (strncmp (DW_STRING (attr), "._", 2) == 0
12457 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12458 return NULL;
12459
12460 /* GCC might emit a nameless typedef that has a linkage name. See
12461 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12462 if (!attr || DW_STRING (attr) == NULL)
12463 {
12464 char *demangled = NULL;
12465
12466 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12467 if (attr == NULL)
12468 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12469
12470 if (attr == NULL || DW_STRING (attr) == NULL)
12471 return NULL;
12472
12473 /* Avoid demangling DW_STRING (attr) the second time on a second
12474 call for the same DIE. */
12475 if (!DW_STRING_IS_CANONICAL (attr))
12476 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12477
12478 if (demangled)
12479 {
12480 /* FIXME: we already did this for the partial symbol... */
12481 DW_STRING (attr)
12482 = obsavestring (demangled, strlen (demangled),
12483 &cu->objfile->objfile_obstack);
12484 DW_STRING_IS_CANONICAL (attr) = 1;
12485 xfree (demangled);
12486 }
12487 }
12488 break;
12489
12490 default:
12491 break;
12492 }
12493
12494 if (!DW_STRING_IS_CANONICAL (attr))
12495 {
12496 DW_STRING (attr)
12497 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12498 &cu->objfile->objfile_obstack);
12499 DW_STRING_IS_CANONICAL (attr) = 1;
12500 }
12501 return DW_STRING (attr);
12502 }
12503
12504 /* Return the die that this die in an extension of, or NULL if there
12505 is none. *EXT_CU is the CU containing DIE on input, and the CU
12506 containing the return value on output. */
12507
12508 static struct die_info *
12509 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12510 {
12511 struct attribute *attr;
12512
12513 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12514 if (attr == NULL)
12515 return NULL;
12516
12517 return follow_die_ref (die, attr, ext_cu);
12518 }
12519
12520 /* Convert a DIE tag into its string name. */
12521
12522 static char *
12523 dwarf_tag_name (unsigned tag)
12524 {
12525 switch (tag)
12526 {
12527 case DW_TAG_padding:
12528 return "DW_TAG_padding";
12529 case DW_TAG_array_type:
12530 return "DW_TAG_array_type";
12531 case DW_TAG_class_type:
12532 return "DW_TAG_class_type";
12533 case DW_TAG_entry_point:
12534 return "DW_TAG_entry_point";
12535 case DW_TAG_enumeration_type:
12536 return "DW_TAG_enumeration_type";
12537 case DW_TAG_formal_parameter:
12538 return "DW_TAG_formal_parameter";
12539 case DW_TAG_imported_declaration:
12540 return "DW_TAG_imported_declaration";
12541 case DW_TAG_label:
12542 return "DW_TAG_label";
12543 case DW_TAG_lexical_block:
12544 return "DW_TAG_lexical_block";
12545 case DW_TAG_member:
12546 return "DW_TAG_member";
12547 case DW_TAG_pointer_type:
12548 return "DW_TAG_pointer_type";
12549 case DW_TAG_reference_type:
12550 return "DW_TAG_reference_type";
12551 case DW_TAG_compile_unit:
12552 return "DW_TAG_compile_unit";
12553 case DW_TAG_string_type:
12554 return "DW_TAG_string_type";
12555 case DW_TAG_structure_type:
12556 return "DW_TAG_structure_type";
12557 case DW_TAG_subroutine_type:
12558 return "DW_TAG_subroutine_type";
12559 case DW_TAG_typedef:
12560 return "DW_TAG_typedef";
12561 case DW_TAG_union_type:
12562 return "DW_TAG_union_type";
12563 case DW_TAG_unspecified_parameters:
12564 return "DW_TAG_unspecified_parameters";
12565 case DW_TAG_variant:
12566 return "DW_TAG_variant";
12567 case DW_TAG_common_block:
12568 return "DW_TAG_common_block";
12569 case DW_TAG_common_inclusion:
12570 return "DW_TAG_common_inclusion";
12571 case DW_TAG_inheritance:
12572 return "DW_TAG_inheritance";
12573 case DW_TAG_inlined_subroutine:
12574 return "DW_TAG_inlined_subroutine";
12575 case DW_TAG_module:
12576 return "DW_TAG_module";
12577 case DW_TAG_ptr_to_member_type:
12578 return "DW_TAG_ptr_to_member_type";
12579 case DW_TAG_set_type:
12580 return "DW_TAG_set_type";
12581 case DW_TAG_subrange_type:
12582 return "DW_TAG_subrange_type";
12583 case DW_TAG_with_stmt:
12584 return "DW_TAG_with_stmt";
12585 case DW_TAG_access_declaration:
12586 return "DW_TAG_access_declaration";
12587 case DW_TAG_base_type:
12588 return "DW_TAG_base_type";
12589 case DW_TAG_catch_block:
12590 return "DW_TAG_catch_block";
12591 case DW_TAG_const_type:
12592 return "DW_TAG_const_type";
12593 case DW_TAG_constant:
12594 return "DW_TAG_constant";
12595 case DW_TAG_enumerator:
12596 return "DW_TAG_enumerator";
12597 case DW_TAG_file_type:
12598 return "DW_TAG_file_type";
12599 case DW_TAG_friend:
12600 return "DW_TAG_friend";
12601 case DW_TAG_namelist:
12602 return "DW_TAG_namelist";
12603 case DW_TAG_namelist_item:
12604 return "DW_TAG_namelist_item";
12605 case DW_TAG_packed_type:
12606 return "DW_TAG_packed_type";
12607 case DW_TAG_subprogram:
12608 return "DW_TAG_subprogram";
12609 case DW_TAG_template_type_param:
12610 return "DW_TAG_template_type_param";
12611 case DW_TAG_template_value_param:
12612 return "DW_TAG_template_value_param";
12613 case DW_TAG_thrown_type:
12614 return "DW_TAG_thrown_type";
12615 case DW_TAG_try_block:
12616 return "DW_TAG_try_block";
12617 case DW_TAG_variant_part:
12618 return "DW_TAG_variant_part";
12619 case DW_TAG_variable:
12620 return "DW_TAG_variable";
12621 case DW_TAG_volatile_type:
12622 return "DW_TAG_volatile_type";
12623 case DW_TAG_dwarf_procedure:
12624 return "DW_TAG_dwarf_procedure";
12625 case DW_TAG_restrict_type:
12626 return "DW_TAG_restrict_type";
12627 case DW_TAG_interface_type:
12628 return "DW_TAG_interface_type";
12629 case DW_TAG_namespace:
12630 return "DW_TAG_namespace";
12631 case DW_TAG_imported_module:
12632 return "DW_TAG_imported_module";
12633 case DW_TAG_unspecified_type:
12634 return "DW_TAG_unspecified_type";
12635 case DW_TAG_partial_unit:
12636 return "DW_TAG_partial_unit";
12637 case DW_TAG_imported_unit:
12638 return "DW_TAG_imported_unit";
12639 case DW_TAG_condition:
12640 return "DW_TAG_condition";
12641 case DW_TAG_shared_type:
12642 return "DW_TAG_shared_type";
12643 case DW_TAG_type_unit:
12644 return "DW_TAG_type_unit";
12645 case DW_TAG_MIPS_loop:
12646 return "DW_TAG_MIPS_loop";
12647 case DW_TAG_HP_array_descriptor:
12648 return "DW_TAG_HP_array_descriptor";
12649 case DW_TAG_format_label:
12650 return "DW_TAG_format_label";
12651 case DW_TAG_function_template:
12652 return "DW_TAG_function_template";
12653 case DW_TAG_class_template:
12654 return "DW_TAG_class_template";
12655 case DW_TAG_GNU_BINCL:
12656 return "DW_TAG_GNU_BINCL";
12657 case DW_TAG_GNU_EINCL:
12658 return "DW_TAG_GNU_EINCL";
12659 case DW_TAG_upc_shared_type:
12660 return "DW_TAG_upc_shared_type";
12661 case DW_TAG_upc_strict_type:
12662 return "DW_TAG_upc_strict_type";
12663 case DW_TAG_upc_relaxed_type:
12664 return "DW_TAG_upc_relaxed_type";
12665 case DW_TAG_PGI_kanji_type:
12666 return "DW_TAG_PGI_kanji_type";
12667 case DW_TAG_PGI_interface_block:
12668 return "DW_TAG_PGI_interface_block";
12669 default:
12670 return "DW_TAG_<unknown>";
12671 }
12672 }
12673
12674 /* Convert a DWARF attribute code into its string name. */
12675
12676 static char *
12677 dwarf_attr_name (unsigned attr)
12678 {
12679 switch (attr)
12680 {
12681 case DW_AT_sibling:
12682 return "DW_AT_sibling";
12683 case DW_AT_location:
12684 return "DW_AT_location";
12685 case DW_AT_name:
12686 return "DW_AT_name";
12687 case DW_AT_ordering:
12688 return "DW_AT_ordering";
12689 case DW_AT_subscr_data:
12690 return "DW_AT_subscr_data";
12691 case DW_AT_byte_size:
12692 return "DW_AT_byte_size";
12693 case DW_AT_bit_offset:
12694 return "DW_AT_bit_offset";
12695 case DW_AT_bit_size:
12696 return "DW_AT_bit_size";
12697 case DW_AT_element_list:
12698 return "DW_AT_element_list";
12699 case DW_AT_stmt_list:
12700 return "DW_AT_stmt_list";
12701 case DW_AT_low_pc:
12702 return "DW_AT_low_pc";
12703 case DW_AT_high_pc:
12704 return "DW_AT_high_pc";
12705 case DW_AT_language:
12706 return "DW_AT_language";
12707 case DW_AT_member:
12708 return "DW_AT_member";
12709 case DW_AT_discr:
12710 return "DW_AT_discr";
12711 case DW_AT_discr_value:
12712 return "DW_AT_discr_value";
12713 case DW_AT_visibility:
12714 return "DW_AT_visibility";
12715 case DW_AT_import:
12716 return "DW_AT_import";
12717 case DW_AT_string_length:
12718 return "DW_AT_string_length";
12719 case DW_AT_common_reference:
12720 return "DW_AT_common_reference";
12721 case DW_AT_comp_dir:
12722 return "DW_AT_comp_dir";
12723 case DW_AT_const_value:
12724 return "DW_AT_const_value";
12725 case DW_AT_containing_type:
12726 return "DW_AT_containing_type";
12727 case DW_AT_default_value:
12728 return "DW_AT_default_value";
12729 case DW_AT_inline:
12730 return "DW_AT_inline";
12731 case DW_AT_is_optional:
12732 return "DW_AT_is_optional";
12733 case DW_AT_lower_bound:
12734 return "DW_AT_lower_bound";
12735 case DW_AT_producer:
12736 return "DW_AT_producer";
12737 case DW_AT_prototyped:
12738 return "DW_AT_prototyped";
12739 case DW_AT_return_addr:
12740 return "DW_AT_return_addr";
12741 case DW_AT_start_scope:
12742 return "DW_AT_start_scope";
12743 case DW_AT_bit_stride:
12744 return "DW_AT_bit_stride";
12745 case DW_AT_upper_bound:
12746 return "DW_AT_upper_bound";
12747 case DW_AT_abstract_origin:
12748 return "DW_AT_abstract_origin";
12749 case DW_AT_accessibility:
12750 return "DW_AT_accessibility";
12751 case DW_AT_address_class:
12752 return "DW_AT_address_class";
12753 case DW_AT_artificial:
12754 return "DW_AT_artificial";
12755 case DW_AT_base_types:
12756 return "DW_AT_base_types";
12757 case DW_AT_calling_convention:
12758 return "DW_AT_calling_convention";
12759 case DW_AT_count:
12760 return "DW_AT_count";
12761 case DW_AT_data_member_location:
12762 return "DW_AT_data_member_location";
12763 case DW_AT_decl_column:
12764 return "DW_AT_decl_column";
12765 case DW_AT_decl_file:
12766 return "DW_AT_decl_file";
12767 case DW_AT_decl_line:
12768 return "DW_AT_decl_line";
12769 case DW_AT_declaration:
12770 return "DW_AT_declaration";
12771 case DW_AT_discr_list:
12772 return "DW_AT_discr_list";
12773 case DW_AT_encoding:
12774 return "DW_AT_encoding";
12775 case DW_AT_external:
12776 return "DW_AT_external";
12777 case DW_AT_frame_base:
12778 return "DW_AT_frame_base";
12779 case DW_AT_friend:
12780 return "DW_AT_friend";
12781 case DW_AT_identifier_case:
12782 return "DW_AT_identifier_case";
12783 case DW_AT_macro_info:
12784 return "DW_AT_macro_info";
12785 case DW_AT_namelist_items:
12786 return "DW_AT_namelist_items";
12787 case DW_AT_priority:
12788 return "DW_AT_priority";
12789 case DW_AT_segment:
12790 return "DW_AT_segment";
12791 case DW_AT_specification:
12792 return "DW_AT_specification";
12793 case DW_AT_static_link:
12794 return "DW_AT_static_link";
12795 case DW_AT_type:
12796 return "DW_AT_type";
12797 case DW_AT_use_location:
12798 return "DW_AT_use_location";
12799 case DW_AT_variable_parameter:
12800 return "DW_AT_variable_parameter";
12801 case DW_AT_virtuality:
12802 return "DW_AT_virtuality";
12803 case DW_AT_vtable_elem_location:
12804 return "DW_AT_vtable_elem_location";
12805 /* DWARF 3 values. */
12806 case DW_AT_allocated:
12807 return "DW_AT_allocated";
12808 case DW_AT_associated:
12809 return "DW_AT_associated";
12810 case DW_AT_data_location:
12811 return "DW_AT_data_location";
12812 case DW_AT_byte_stride:
12813 return "DW_AT_byte_stride";
12814 case DW_AT_entry_pc:
12815 return "DW_AT_entry_pc";
12816 case DW_AT_use_UTF8:
12817 return "DW_AT_use_UTF8";
12818 case DW_AT_extension:
12819 return "DW_AT_extension";
12820 case DW_AT_ranges:
12821 return "DW_AT_ranges";
12822 case DW_AT_trampoline:
12823 return "DW_AT_trampoline";
12824 case DW_AT_call_column:
12825 return "DW_AT_call_column";
12826 case DW_AT_call_file:
12827 return "DW_AT_call_file";
12828 case DW_AT_call_line:
12829 return "DW_AT_call_line";
12830 case DW_AT_description:
12831 return "DW_AT_description";
12832 case DW_AT_binary_scale:
12833 return "DW_AT_binary_scale";
12834 case DW_AT_decimal_scale:
12835 return "DW_AT_decimal_scale";
12836 case DW_AT_small:
12837 return "DW_AT_small";
12838 case DW_AT_decimal_sign:
12839 return "DW_AT_decimal_sign";
12840 case DW_AT_digit_count:
12841 return "DW_AT_digit_count";
12842 case DW_AT_picture_string:
12843 return "DW_AT_picture_string";
12844 case DW_AT_mutable:
12845 return "DW_AT_mutable";
12846 case DW_AT_threads_scaled:
12847 return "DW_AT_threads_scaled";
12848 case DW_AT_explicit:
12849 return "DW_AT_explicit";
12850 case DW_AT_object_pointer:
12851 return "DW_AT_object_pointer";
12852 case DW_AT_endianity:
12853 return "DW_AT_endianity";
12854 case DW_AT_elemental:
12855 return "DW_AT_elemental";
12856 case DW_AT_pure:
12857 return "DW_AT_pure";
12858 case DW_AT_recursive:
12859 return "DW_AT_recursive";
12860 /* DWARF 4 values. */
12861 case DW_AT_signature:
12862 return "DW_AT_signature";
12863 case DW_AT_linkage_name:
12864 return "DW_AT_linkage_name";
12865 /* SGI/MIPS extensions. */
12866 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12867 case DW_AT_MIPS_fde:
12868 return "DW_AT_MIPS_fde";
12869 #endif
12870 case DW_AT_MIPS_loop_begin:
12871 return "DW_AT_MIPS_loop_begin";
12872 case DW_AT_MIPS_tail_loop_begin:
12873 return "DW_AT_MIPS_tail_loop_begin";
12874 case DW_AT_MIPS_epilog_begin:
12875 return "DW_AT_MIPS_epilog_begin";
12876 case DW_AT_MIPS_loop_unroll_factor:
12877 return "DW_AT_MIPS_loop_unroll_factor";
12878 case DW_AT_MIPS_software_pipeline_depth:
12879 return "DW_AT_MIPS_software_pipeline_depth";
12880 case DW_AT_MIPS_linkage_name:
12881 return "DW_AT_MIPS_linkage_name";
12882 case DW_AT_MIPS_stride:
12883 return "DW_AT_MIPS_stride";
12884 case DW_AT_MIPS_abstract_name:
12885 return "DW_AT_MIPS_abstract_name";
12886 case DW_AT_MIPS_clone_origin:
12887 return "DW_AT_MIPS_clone_origin";
12888 case DW_AT_MIPS_has_inlines:
12889 return "DW_AT_MIPS_has_inlines";
12890 /* HP extensions. */
12891 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12892 case DW_AT_HP_block_index:
12893 return "DW_AT_HP_block_index";
12894 #endif
12895 case DW_AT_HP_unmodifiable:
12896 return "DW_AT_HP_unmodifiable";
12897 case DW_AT_HP_actuals_stmt_list:
12898 return "DW_AT_HP_actuals_stmt_list";
12899 case DW_AT_HP_proc_per_section:
12900 return "DW_AT_HP_proc_per_section";
12901 case DW_AT_HP_raw_data_ptr:
12902 return "DW_AT_HP_raw_data_ptr";
12903 case DW_AT_HP_pass_by_reference:
12904 return "DW_AT_HP_pass_by_reference";
12905 case DW_AT_HP_opt_level:
12906 return "DW_AT_HP_opt_level";
12907 case DW_AT_HP_prof_version_id:
12908 return "DW_AT_HP_prof_version_id";
12909 case DW_AT_HP_opt_flags:
12910 return "DW_AT_HP_opt_flags";
12911 case DW_AT_HP_cold_region_low_pc:
12912 return "DW_AT_HP_cold_region_low_pc";
12913 case DW_AT_HP_cold_region_high_pc:
12914 return "DW_AT_HP_cold_region_high_pc";
12915 case DW_AT_HP_all_variables_modifiable:
12916 return "DW_AT_HP_all_variables_modifiable";
12917 case DW_AT_HP_linkage_name:
12918 return "DW_AT_HP_linkage_name";
12919 case DW_AT_HP_prof_flags:
12920 return "DW_AT_HP_prof_flags";
12921 /* GNU extensions. */
12922 case DW_AT_sf_names:
12923 return "DW_AT_sf_names";
12924 case DW_AT_src_info:
12925 return "DW_AT_src_info";
12926 case DW_AT_mac_info:
12927 return "DW_AT_mac_info";
12928 case DW_AT_src_coords:
12929 return "DW_AT_src_coords";
12930 case DW_AT_body_begin:
12931 return "DW_AT_body_begin";
12932 case DW_AT_body_end:
12933 return "DW_AT_body_end";
12934 case DW_AT_GNU_vector:
12935 return "DW_AT_GNU_vector";
12936 case DW_AT_GNU_odr_signature:
12937 return "DW_AT_GNU_odr_signature";
12938 /* VMS extensions. */
12939 case DW_AT_VMS_rtnbeg_pd_address:
12940 return "DW_AT_VMS_rtnbeg_pd_address";
12941 /* UPC extension. */
12942 case DW_AT_upc_threads_scaled:
12943 return "DW_AT_upc_threads_scaled";
12944 /* PGI (STMicroelectronics) extensions. */
12945 case DW_AT_PGI_lbase:
12946 return "DW_AT_PGI_lbase";
12947 case DW_AT_PGI_soffset:
12948 return "DW_AT_PGI_soffset";
12949 case DW_AT_PGI_lstride:
12950 return "DW_AT_PGI_lstride";
12951 default:
12952 return "DW_AT_<unknown>";
12953 }
12954 }
12955
12956 /* Convert a DWARF value form code into its string name. */
12957
12958 static char *
12959 dwarf_form_name (unsigned form)
12960 {
12961 switch (form)
12962 {
12963 case DW_FORM_addr:
12964 return "DW_FORM_addr";
12965 case DW_FORM_block2:
12966 return "DW_FORM_block2";
12967 case DW_FORM_block4:
12968 return "DW_FORM_block4";
12969 case DW_FORM_data2:
12970 return "DW_FORM_data2";
12971 case DW_FORM_data4:
12972 return "DW_FORM_data4";
12973 case DW_FORM_data8:
12974 return "DW_FORM_data8";
12975 case DW_FORM_string:
12976 return "DW_FORM_string";
12977 case DW_FORM_block:
12978 return "DW_FORM_block";
12979 case DW_FORM_block1:
12980 return "DW_FORM_block1";
12981 case DW_FORM_data1:
12982 return "DW_FORM_data1";
12983 case DW_FORM_flag:
12984 return "DW_FORM_flag";
12985 case DW_FORM_sdata:
12986 return "DW_FORM_sdata";
12987 case DW_FORM_strp:
12988 return "DW_FORM_strp";
12989 case DW_FORM_udata:
12990 return "DW_FORM_udata";
12991 case DW_FORM_ref_addr:
12992 return "DW_FORM_ref_addr";
12993 case DW_FORM_ref1:
12994 return "DW_FORM_ref1";
12995 case DW_FORM_ref2:
12996 return "DW_FORM_ref2";
12997 case DW_FORM_ref4:
12998 return "DW_FORM_ref4";
12999 case DW_FORM_ref8:
13000 return "DW_FORM_ref8";
13001 case DW_FORM_ref_udata:
13002 return "DW_FORM_ref_udata";
13003 case DW_FORM_indirect:
13004 return "DW_FORM_indirect";
13005 case DW_FORM_sec_offset:
13006 return "DW_FORM_sec_offset";
13007 case DW_FORM_exprloc:
13008 return "DW_FORM_exprloc";
13009 case DW_FORM_flag_present:
13010 return "DW_FORM_flag_present";
13011 case DW_FORM_ref_sig8:
13012 return "DW_FORM_ref_sig8";
13013 default:
13014 return "DW_FORM_<unknown>";
13015 }
13016 }
13017
13018 /* Convert a DWARF stack opcode into its string name. */
13019
13020 const char *
13021 dwarf_stack_op_name (unsigned op)
13022 {
13023 switch (op)
13024 {
13025 case DW_OP_addr:
13026 return "DW_OP_addr";
13027 case DW_OP_deref:
13028 return "DW_OP_deref";
13029 case DW_OP_const1u:
13030 return "DW_OP_const1u";
13031 case DW_OP_const1s:
13032 return "DW_OP_const1s";
13033 case DW_OP_const2u:
13034 return "DW_OP_const2u";
13035 case DW_OP_const2s:
13036 return "DW_OP_const2s";
13037 case DW_OP_const4u:
13038 return "DW_OP_const4u";
13039 case DW_OP_const4s:
13040 return "DW_OP_const4s";
13041 case DW_OP_const8u:
13042 return "DW_OP_const8u";
13043 case DW_OP_const8s:
13044 return "DW_OP_const8s";
13045 case DW_OP_constu:
13046 return "DW_OP_constu";
13047 case DW_OP_consts:
13048 return "DW_OP_consts";
13049 case DW_OP_dup:
13050 return "DW_OP_dup";
13051 case DW_OP_drop:
13052 return "DW_OP_drop";
13053 case DW_OP_over:
13054 return "DW_OP_over";
13055 case DW_OP_pick:
13056 return "DW_OP_pick";
13057 case DW_OP_swap:
13058 return "DW_OP_swap";
13059 case DW_OP_rot:
13060 return "DW_OP_rot";
13061 case DW_OP_xderef:
13062 return "DW_OP_xderef";
13063 case DW_OP_abs:
13064 return "DW_OP_abs";
13065 case DW_OP_and:
13066 return "DW_OP_and";
13067 case DW_OP_div:
13068 return "DW_OP_div";
13069 case DW_OP_minus:
13070 return "DW_OP_minus";
13071 case DW_OP_mod:
13072 return "DW_OP_mod";
13073 case DW_OP_mul:
13074 return "DW_OP_mul";
13075 case DW_OP_neg:
13076 return "DW_OP_neg";
13077 case DW_OP_not:
13078 return "DW_OP_not";
13079 case DW_OP_or:
13080 return "DW_OP_or";
13081 case DW_OP_plus:
13082 return "DW_OP_plus";
13083 case DW_OP_plus_uconst:
13084 return "DW_OP_plus_uconst";
13085 case DW_OP_shl:
13086 return "DW_OP_shl";
13087 case DW_OP_shr:
13088 return "DW_OP_shr";
13089 case DW_OP_shra:
13090 return "DW_OP_shra";
13091 case DW_OP_xor:
13092 return "DW_OP_xor";
13093 case DW_OP_bra:
13094 return "DW_OP_bra";
13095 case DW_OP_eq:
13096 return "DW_OP_eq";
13097 case DW_OP_ge:
13098 return "DW_OP_ge";
13099 case DW_OP_gt:
13100 return "DW_OP_gt";
13101 case DW_OP_le:
13102 return "DW_OP_le";
13103 case DW_OP_lt:
13104 return "DW_OP_lt";
13105 case DW_OP_ne:
13106 return "DW_OP_ne";
13107 case DW_OP_skip:
13108 return "DW_OP_skip";
13109 case DW_OP_lit0:
13110 return "DW_OP_lit0";
13111 case DW_OP_lit1:
13112 return "DW_OP_lit1";
13113 case DW_OP_lit2:
13114 return "DW_OP_lit2";
13115 case DW_OP_lit3:
13116 return "DW_OP_lit3";
13117 case DW_OP_lit4:
13118 return "DW_OP_lit4";
13119 case DW_OP_lit5:
13120 return "DW_OP_lit5";
13121 case DW_OP_lit6:
13122 return "DW_OP_lit6";
13123 case DW_OP_lit7:
13124 return "DW_OP_lit7";
13125 case DW_OP_lit8:
13126 return "DW_OP_lit8";
13127 case DW_OP_lit9:
13128 return "DW_OP_lit9";
13129 case DW_OP_lit10:
13130 return "DW_OP_lit10";
13131 case DW_OP_lit11:
13132 return "DW_OP_lit11";
13133 case DW_OP_lit12:
13134 return "DW_OP_lit12";
13135 case DW_OP_lit13:
13136 return "DW_OP_lit13";
13137 case DW_OP_lit14:
13138 return "DW_OP_lit14";
13139 case DW_OP_lit15:
13140 return "DW_OP_lit15";
13141 case DW_OP_lit16:
13142 return "DW_OP_lit16";
13143 case DW_OP_lit17:
13144 return "DW_OP_lit17";
13145 case DW_OP_lit18:
13146 return "DW_OP_lit18";
13147 case DW_OP_lit19:
13148 return "DW_OP_lit19";
13149 case DW_OP_lit20:
13150 return "DW_OP_lit20";
13151 case DW_OP_lit21:
13152 return "DW_OP_lit21";
13153 case DW_OP_lit22:
13154 return "DW_OP_lit22";
13155 case DW_OP_lit23:
13156 return "DW_OP_lit23";
13157 case DW_OP_lit24:
13158 return "DW_OP_lit24";
13159 case DW_OP_lit25:
13160 return "DW_OP_lit25";
13161 case DW_OP_lit26:
13162 return "DW_OP_lit26";
13163 case DW_OP_lit27:
13164 return "DW_OP_lit27";
13165 case DW_OP_lit28:
13166 return "DW_OP_lit28";
13167 case DW_OP_lit29:
13168 return "DW_OP_lit29";
13169 case DW_OP_lit30:
13170 return "DW_OP_lit30";
13171 case DW_OP_lit31:
13172 return "DW_OP_lit31";
13173 case DW_OP_reg0:
13174 return "DW_OP_reg0";
13175 case DW_OP_reg1:
13176 return "DW_OP_reg1";
13177 case DW_OP_reg2:
13178 return "DW_OP_reg2";
13179 case DW_OP_reg3:
13180 return "DW_OP_reg3";
13181 case DW_OP_reg4:
13182 return "DW_OP_reg4";
13183 case DW_OP_reg5:
13184 return "DW_OP_reg5";
13185 case DW_OP_reg6:
13186 return "DW_OP_reg6";
13187 case DW_OP_reg7:
13188 return "DW_OP_reg7";
13189 case DW_OP_reg8:
13190 return "DW_OP_reg8";
13191 case DW_OP_reg9:
13192 return "DW_OP_reg9";
13193 case DW_OP_reg10:
13194 return "DW_OP_reg10";
13195 case DW_OP_reg11:
13196 return "DW_OP_reg11";
13197 case DW_OP_reg12:
13198 return "DW_OP_reg12";
13199 case DW_OP_reg13:
13200 return "DW_OP_reg13";
13201 case DW_OP_reg14:
13202 return "DW_OP_reg14";
13203 case DW_OP_reg15:
13204 return "DW_OP_reg15";
13205 case DW_OP_reg16:
13206 return "DW_OP_reg16";
13207 case DW_OP_reg17:
13208 return "DW_OP_reg17";
13209 case DW_OP_reg18:
13210 return "DW_OP_reg18";
13211 case DW_OP_reg19:
13212 return "DW_OP_reg19";
13213 case DW_OP_reg20:
13214 return "DW_OP_reg20";
13215 case DW_OP_reg21:
13216 return "DW_OP_reg21";
13217 case DW_OP_reg22:
13218 return "DW_OP_reg22";
13219 case DW_OP_reg23:
13220 return "DW_OP_reg23";
13221 case DW_OP_reg24:
13222 return "DW_OP_reg24";
13223 case DW_OP_reg25:
13224 return "DW_OP_reg25";
13225 case DW_OP_reg26:
13226 return "DW_OP_reg26";
13227 case DW_OP_reg27:
13228 return "DW_OP_reg27";
13229 case DW_OP_reg28:
13230 return "DW_OP_reg28";
13231 case DW_OP_reg29:
13232 return "DW_OP_reg29";
13233 case DW_OP_reg30:
13234 return "DW_OP_reg30";
13235 case DW_OP_reg31:
13236 return "DW_OP_reg31";
13237 case DW_OP_breg0:
13238 return "DW_OP_breg0";
13239 case DW_OP_breg1:
13240 return "DW_OP_breg1";
13241 case DW_OP_breg2:
13242 return "DW_OP_breg2";
13243 case DW_OP_breg3:
13244 return "DW_OP_breg3";
13245 case DW_OP_breg4:
13246 return "DW_OP_breg4";
13247 case DW_OP_breg5:
13248 return "DW_OP_breg5";
13249 case DW_OP_breg6:
13250 return "DW_OP_breg6";
13251 case DW_OP_breg7:
13252 return "DW_OP_breg7";
13253 case DW_OP_breg8:
13254 return "DW_OP_breg8";
13255 case DW_OP_breg9:
13256 return "DW_OP_breg9";
13257 case DW_OP_breg10:
13258 return "DW_OP_breg10";
13259 case DW_OP_breg11:
13260 return "DW_OP_breg11";
13261 case DW_OP_breg12:
13262 return "DW_OP_breg12";
13263 case DW_OP_breg13:
13264 return "DW_OP_breg13";
13265 case DW_OP_breg14:
13266 return "DW_OP_breg14";
13267 case DW_OP_breg15:
13268 return "DW_OP_breg15";
13269 case DW_OP_breg16:
13270 return "DW_OP_breg16";
13271 case DW_OP_breg17:
13272 return "DW_OP_breg17";
13273 case DW_OP_breg18:
13274 return "DW_OP_breg18";
13275 case DW_OP_breg19:
13276 return "DW_OP_breg19";
13277 case DW_OP_breg20:
13278 return "DW_OP_breg20";
13279 case DW_OP_breg21:
13280 return "DW_OP_breg21";
13281 case DW_OP_breg22:
13282 return "DW_OP_breg22";
13283 case DW_OP_breg23:
13284 return "DW_OP_breg23";
13285 case DW_OP_breg24:
13286 return "DW_OP_breg24";
13287 case DW_OP_breg25:
13288 return "DW_OP_breg25";
13289 case DW_OP_breg26:
13290 return "DW_OP_breg26";
13291 case DW_OP_breg27:
13292 return "DW_OP_breg27";
13293 case DW_OP_breg28:
13294 return "DW_OP_breg28";
13295 case DW_OP_breg29:
13296 return "DW_OP_breg29";
13297 case DW_OP_breg30:
13298 return "DW_OP_breg30";
13299 case DW_OP_breg31:
13300 return "DW_OP_breg31";
13301 case DW_OP_regx:
13302 return "DW_OP_regx";
13303 case DW_OP_fbreg:
13304 return "DW_OP_fbreg";
13305 case DW_OP_bregx:
13306 return "DW_OP_bregx";
13307 case DW_OP_piece:
13308 return "DW_OP_piece";
13309 case DW_OP_deref_size:
13310 return "DW_OP_deref_size";
13311 case DW_OP_xderef_size:
13312 return "DW_OP_xderef_size";
13313 case DW_OP_nop:
13314 return "DW_OP_nop";
13315 /* DWARF 3 extensions. */
13316 case DW_OP_push_object_address:
13317 return "DW_OP_push_object_address";
13318 case DW_OP_call2:
13319 return "DW_OP_call2";
13320 case DW_OP_call4:
13321 return "DW_OP_call4";
13322 case DW_OP_call_ref:
13323 return "DW_OP_call_ref";
13324 case DW_OP_form_tls_address:
13325 return "DW_OP_form_tls_address";
13326 case DW_OP_call_frame_cfa:
13327 return "DW_OP_call_frame_cfa";
13328 case DW_OP_bit_piece:
13329 return "DW_OP_bit_piece";
13330 /* DWARF 4 extensions. */
13331 case DW_OP_implicit_value:
13332 return "DW_OP_implicit_value";
13333 case DW_OP_stack_value:
13334 return "DW_OP_stack_value";
13335 /* GNU extensions. */
13336 case DW_OP_GNU_push_tls_address:
13337 return "DW_OP_GNU_push_tls_address";
13338 case DW_OP_GNU_uninit:
13339 return "DW_OP_GNU_uninit";
13340 case DW_OP_GNU_implicit_pointer:
13341 return "DW_OP_GNU_implicit_pointer";
13342 case DW_OP_GNU_entry_value:
13343 return "DW_OP_GNU_entry_value";
13344 case DW_OP_GNU_const_type:
13345 return "DW_OP_GNU_const_type";
13346 case DW_OP_GNU_regval_type:
13347 return "DW_OP_GNU_regval_type";
13348 case DW_OP_GNU_deref_type:
13349 return "DW_OP_GNU_deref_type";
13350 case DW_OP_GNU_convert:
13351 return "DW_OP_GNU_convert";
13352 case DW_OP_GNU_reinterpret:
13353 return "DW_OP_GNU_reinterpret";
13354 default:
13355 return NULL;
13356 }
13357 }
13358
13359 static char *
13360 dwarf_bool_name (unsigned mybool)
13361 {
13362 if (mybool)
13363 return "TRUE";
13364 else
13365 return "FALSE";
13366 }
13367
13368 /* Convert a DWARF type code into its string name. */
13369
13370 static char *
13371 dwarf_type_encoding_name (unsigned enc)
13372 {
13373 switch (enc)
13374 {
13375 case DW_ATE_void:
13376 return "DW_ATE_void";
13377 case DW_ATE_address:
13378 return "DW_ATE_address";
13379 case DW_ATE_boolean:
13380 return "DW_ATE_boolean";
13381 case DW_ATE_complex_float:
13382 return "DW_ATE_complex_float";
13383 case DW_ATE_float:
13384 return "DW_ATE_float";
13385 case DW_ATE_signed:
13386 return "DW_ATE_signed";
13387 case DW_ATE_signed_char:
13388 return "DW_ATE_signed_char";
13389 case DW_ATE_unsigned:
13390 return "DW_ATE_unsigned";
13391 case DW_ATE_unsigned_char:
13392 return "DW_ATE_unsigned_char";
13393 /* DWARF 3. */
13394 case DW_ATE_imaginary_float:
13395 return "DW_ATE_imaginary_float";
13396 case DW_ATE_packed_decimal:
13397 return "DW_ATE_packed_decimal";
13398 case DW_ATE_numeric_string:
13399 return "DW_ATE_numeric_string";
13400 case DW_ATE_edited:
13401 return "DW_ATE_edited";
13402 case DW_ATE_signed_fixed:
13403 return "DW_ATE_signed_fixed";
13404 case DW_ATE_unsigned_fixed:
13405 return "DW_ATE_unsigned_fixed";
13406 case DW_ATE_decimal_float:
13407 return "DW_ATE_decimal_float";
13408 /* DWARF 4. */
13409 case DW_ATE_UTF:
13410 return "DW_ATE_UTF";
13411 /* HP extensions. */
13412 case DW_ATE_HP_float80:
13413 return "DW_ATE_HP_float80";
13414 case DW_ATE_HP_complex_float80:
13415 return "DW_ATE_HP_complex_float80";
13416 case DW_ATE_HP_float128:
13417 return "DW_ATE_HP_float128";
13418 case DW_ATE_HP_complex_float128:
13419 return "DW_ATE_HP_complex_float128";
13420 case DW_ATE_HP_floathpintel:
13421 return "DW_ATE_HP_floathpintel";
13422 case DW_ATE_HP_imaginary_float80:
13423 return "DW_ATE_HP_imaginary_float80";
13424 case DW_ATE_HP_imaginary_float128:
13425 return "DW_ATE_HP_imaginary_float128";
13426 default:
13427 return "DW_ATE_<unknown>";
13428 }
13429 }
13430
13431 /* Convert a DWARF call frame info operation to its string name. */
13432
13433 #if 0
13434 static char *
13435 dwarf_cfi_name (unsigned cfi_opc)
13436 {
13437 switch (cfi_opc)
13438 {
13439 case DW_CFA_advance_loc:
13440 return "DW_CFA_advance_loc";
13441 case DW_CFA_offset:
13442 return "DW_CFA_offset";
13443 case DW_CFA_restore:
13444 return "DW_CFA_restore";
13445 case DW_CFA_nop:
13446 return "DW_CFA_nop";
13447 case DW_CFA_set_loc:
13448 return "DW_CFA_set_loc";
13449 case DW_CFA_advance_loc1:
13450 return "DW_CFA_advance_loc1";
13451 case DW_CFA_advance_loc2:
13452 return "DW_CFA_advance_loc2";
13453 case DW_CFA_advance_loc4:
13454 return "DW_CFA_advance_loc4";
13455 case DW_CFA_offset_extended:
13456 return "DW_CFA_offset_extended";
13457 case DW_CFA_restore_extended:
13458 return "DW_CFA_restore_extended";
13459 case DW_CFA_undefined:
13460 return "DW_CFA_undefined";
13461 case DW_CFA_same_value:
13462 return "DW_CFA_same_value";
13463 case DW_CFA_register:
13464 return "DW_CFA_register";
13465 case DW_CFA_remember_state:
13466 return "DW_CFA_remember_state";
13467 case DW_CFA_restore_state:
13468 return "DW_CFA_restore_state";
13469 case DW_CFA_def_cfa:
13470 return "DW_CFA_def_cfa";
13471 case DW_CFA_def_cfa_register:
13472 return "DW_CFA_def_cfa_register";
13473 case DW_CFA_def_cfa_offset:
13474 return "DW_CFA_def_cfa_offset";
13475 /* DWARF 3. */
13476 case DW_CFA_def_cfa_expression:
13477 return "DW_CFA_def_cfa_expression";
13478 case DW_CFA_expression:
13479 return "DW_CFA_expression";
13480 case DW_CFA_offset_extended_sf:
13481 return "DW_CFA_offset_extended_sf";
13482 case DW_CFA_def_cfa_sf:
13483 return "DW_CFA_def_cfa_sf";
13484 case DW_CFA_def_cfa_offset_sf:
13485 return "DW_CFA_def_cfa_offset_sf";
13486 case DW_CFA_val_offset:
13487 return "DW_CFA_val_offset";
13488 case DW_CFA_val_offset_sf:
13489 return "DW_CFA_val_offset_sf";
13490 case DW_CFA_val_expression:
13491 return "DW_CFA_val_expression";
13492 /* SGI/MIPS specific. */
13493 case DW_CFA_MIPS_advance_loc8:
13494 return "DW_CFA_MIPS_advance_loc8";
13495 /* GNU extensions. */
13496 case DW_CFA_GNU_window_save:
13497 return "DW_CFA_GNU_window_save";
13498 case DW_CFA_GNU_args_size:
13499 return "DW_CFA_GNU_args_size";
13500 case DW_CFA_GNU_negative_offset_extended:
13501 return "DW_CFA_GNU_negative_offset_extended";
13502 default:
13503 return "DW_CFA_<unknown>";
13504 }
13505 }
13506 #endif
13507
13508 static void
13509 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13510 {
13511 unsigned int i;
13512
13513 print_spaces (indent, f);
13514 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13515 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13516
13517 if (die->parent != NULL)
13518 {
13519 print_spaces (indent, f);
13520 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13521 die->parent->offset);
13522 }
13523
13524 print_spaces (indent, f);
13525 fprintf_unfiltered (f, " has children: %s\n",
13526 dwarf_bool_name (die->child != NULL));
13527
13528 print_spaces (indent, f);
13529 fprintf_unfiltered (f, " attributes:\n");
13530
13531 for (i = 0; i < die->num_attrs; ++i)
13532 {
13533 print_spaces (indent, f);
13534 fprintf_unfiltered (f, " %s (%s) ",
13535 dwarf_attr_name (die->attrs[i].name),
13536 dwarf_form_name (die->attrs[i].form));
13537
13538 switch (die->attrs[i].form)
13539 {
13540 case DW_FORM_ref_addr:
13541 case DW_FORM_addr:
13542 fprintf_unfiltered (f, "address: ");
13543 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13544 break;
13545 case DW_FORM_block2:
13546 case DW_FORM_block4:
13547 case DW_FORM_block:
13548 case DW_FORM_block1:
13549 fprintf_unfiltered (f, "block: size %d",
13550 DW_BLOCK (&die->attrs[i])->size);
13551 break;
13552 case DW_FORM_exprloc:
13553 fprintf_unfiltered (f, "expression: size %u",
13554 DW_BLOCK (&die->attrs[i])->size);
13555 break;
13556 case DW_FORM_ref1:
13557 case DW_FORM_ref2:
13558 case DW_FORM_ref4:
13559 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13560 (long) (DW_ADDR (&die->attrs[i])));
13561 break;
13562 case DW_FORM_data1:
13563 case DW_FORM_data2:
13564 case DW_FORM_data4:
13565 case DW_FORM_data8:
13566 case DW_FORM_udata:
13567 case DW_FORM_sdata:
13568 fprintf_unfiltered (f, "constant: %s",
13569 pulongest (DW_UNSND (&die->attrs[i])));
13570 break;
13571 case DW_FORM_sec_offset:
13572 fprintf_unfiltered (f, "section offset: %s",
13573 pulongest (DW_UNSND (&die->attrs[i])));
13574 break;
13575 case DW_FORM_ref_sig8:
13576 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13577 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13578 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13579 else
13580 fprintf_unfiltered (f, "signatured type, offset: unknown");
13581 break;
13582 case DW_FORM_string:
13583 case DW_FORM_strp:
13584 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13585 DW_STRING (&die->attrs[i])
13586 ? DW_STRING (&die->attrs[i]) : "",
13587 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13588 break;
13589 case DW_FORM_flag:
13590 if (DW_UNSND (&die->attrs[i]))
13591 fprintf_unfiltered (f, "flag: TRUE");
13592 else
13593 fprintf_unfiltered (f, "flag: FALSE");
13594 break;
13595 case DW_FORM_flag_present:
13596 fprintf_unfiltered (f, "flag: TRUE");
13597 break;
13598 case DW_FORM_indirect:
13599 /* The reader will have reduced the indirect form to
13600 the "base form" so this form should not occur. */
13601 fprintf_unfiltered (f,
13602 "unexpected attribute form: DW_FORM_indirect");
13603 break;
13604 default:
13605 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13606 die->attrs[i].form);
13607 break;
13608 }
13609 fprintf_unfiltered (f, "\n");
13610 }
13611 }
13612
13613 static void
13614 dump_die_for_error (struct die_info *die)
13615 {
13616 dump_die_shallow (gdb_stderr, 0, die);
13617 }
13618
13619 static void
13620 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13621 {
13622 int indent = level * 4;
13623
13624 gdb_assert (die != NULL);
13625
13626 if (level >= max_level)
13627 return;
13628
13629 dump_die_shallow (f, indent, die);
13630
13631 if (die->child != NULL)
13632 {
13633 print_spaces (indent, f);
13634 fprintf_unfiltered (f, " Children:");
13635 if (level + 1 < max_level)
13636 {
13637 fprintf_unfiltered (f, "\n");
13638 dump_die_1 (f, level + 1, max_level, die->child);
13639 }
13640 else
13641 {
13642 fprintf_unfiltered (f,
13643 " [not printed, max nesting level reached]\n");
13644 }
13645 }
13646
13647 if (die->sibling != NULL && level > 0)
13648 {
13649 dump_die_1 (f, level, max_level, die->sibling);
13650 }
13651 }
13652
13653 /* This is called from the pdie macro in gdbinit.in.
13654 It's not static so gcc will keep a copy callable from gdb. */
13655
13656 void
13657 dump_die (struct die_info *die, int max_level)
13658 {
13659 dump_die_1 (gdb_stdlog, 0, max_level, die);
13660 }
13661
13662 static void
13663 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13664 {
13665 void **slot;
13666
13667 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13668
13669 *slot = die;
13670 }
13671
13672 static int
13673 is_ref_attr (struct attribute *attr)
13674 {
13675 switch (attr->form)
13676 {
13677 case DW_FORM_ref_addr:
13678 case DW_FORM_ref1:
13679 case DW_FORM_ref2:
13680 case DW_FORM_ref4:
13681 case DW_FORM_ref8:
13682 case DW_FORM_ref_udata:
13683 return 1;
13684 default:
13685 return 0;
13686 }
13687 }
13688
13689 static unsigned int
13690 dwarf2_get_ref_die_offset (struct attribute *attr)
13691 {
13692 if (is_ref_attr (attr))
13693 return DW_ADDR (attr);
13694
13695 complaint (&symfile_complaints,
13696 _("unsupported die ref attribute form: '%s'"),
13697 dwarf_form_name (attr->form));
13698 return 0;
13699 }
13700
13701 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13702 * the value held by the attribute is not constant. */
13703
13704 static LONGEST
13705 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13706 {
13707 if (attr->form == DW_FORM_sdata)
13708 return DW_SND (attr);
13709 else if (attr->form == DW_FORM_udata
13710 || attr->form == DW_FORM_data1
13711 || attr->form == DW_FORM_data2
13712 || attr->form == DW_FORM_data4
13713 || attr->form == DW_FORM_data8)
13714 return DW_UNSND (attr);
13715 else
13716 {
13717 complaint (&symfile_complaints,
13718 _("Attribute value is not a constant (%s)"),
13719 dwarf_form_name (attr->form));
13720 return default_value;
13721 }
13722 }
13723
13724 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
13725 unit and add it to our queue.
13726 The result is non-zero if PER_CU was queued, otherwise the result is zero
13727 meaning either PER_CU is already queued or it is already loaded. */
13728
13729 static int
13730 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13731 struct dwarf2_per_cu_data *per_cu)
13732 {
13733 /* We may arrive here during partial symbol reading, if we need full
13734 DIEs to process an unusual case (e.g. template arguments). Do
13735 not queue PER_CU, just tell our caller to load its DIEs. */
13736 if (dwarf2_per_objfile->reading_partial_symbols)
13737 {
13738 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13739 return 1;
13740 return 0;
13741 }
13742
13743 /* Mark the dependence relation so that we don't flush PER_CU
13744 too early. */
13745 dwarf2_add_dependence (this_cu, per_cu);
13746
13747 /* If it's already on the queue, we have nothing to do. */
13748 if (per_cu->queued)
13749 return 0;
13750
13751 /* If the compilation unit is already loaded, just mark it as
13752 used. */
13753 if (per_cu->cu != NULL)
13754 {
13755 per_cu->cu->last_used = 0;
13756 return 0;
13757 }
13758
13759 /* Add it to the queue. */
13760 queue_comp_unit (per_cu, this_cu->objfile);
13761
13762 return 1;
13763 }
13764
13765 /* Follow reference or signature attribute ATTR of SRC_DIE.
13766 On entry *REF_CU is the CU of SRC_DIE.
13767 On exit *REF_CU is the CU of the result. */
13768
13769 static struct die_info *
13770 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13771 struct dwarf2_cu **ref_cu)
13772 {
13773 struct die_info *die;
13774
13775 if (is_ref_attr (attr))
13776 die = follow_die_ref (src_die, attr, ref_cu);
13777 else if (attr->form == DW_FORM_ref_sig8)
13778 die = follow_die_sig (src_die, attr, ref_cu);
13779 else
13780 {
13781 dump_die_for_error (src_die);
13782 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13783 (*ref_cu)->objfile->name);
13784 }
13785
13786 return die;
13787 }
13788
13789 /* Follow reference OFFSET.
13790 On entry *REF_CU is the CU of the source die referencing OFFSET.
13791 On exit *REF_CU is the CU of the result.
13792 Returns NULL if OFFSET is invalid. */
13793
13794 static struct die_info *
13795 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13796 {
13797 struct die_info temp_die;
13798 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13799
13800 gdb_assert (cu->per_cu != NULL);
13801
13802 target_cu = cu;
13803
13804 if (cu->per_cu->debug_type_section)
13805 {
13806 /* .debug_types CUs cannot reference anything outside their CU.
13807 If they need to, they have to reference a signatured type via
13808 DW_FORM_ref_sig8. */
13809 if (! offset_in_cu_p (&cu->header, offset))
13810 return NULL;
13811 }
13812 else if (! offset_in_cu_p (&cu->header, offset))
13813 {
13814 struct dwarf2_per_cu_data *per_cu;
13815
13816 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13817
13818 /* If necessary, add it to the queue and load its DIEs. */
13819 if (maybe_queue_comp_unit (cu, per_cu))
13820 load_full_comp_unit (per_cu, cu->objfile);
13821
13822 target_cu = per_cu->cu;
13823 }
13824 else if (cu->dies == NULL)
13825 {
13826 /* We're loading full DIEs during partial symbol reading. */
13827 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13828 load_full_comp_unit (cu->per_cu, cu->objfile);
13829 }
13830
13831 *ref_cu = target_cu;
13832 temp_die.offset = offset;
13833 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13834 }
13835
13836 /* Follow reference attribute ATTR of SRC_DIE.
13837 On entry *REF_CU is the CU of SRC_DIE.
13838 On exit *REF_CU is the CU of the result. */
13839
13840 static struct die_info *
13841 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13842 struct dwarf2_cu **ref_cu)
13843 {
13844 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13845 struct dwarf2_cu *cu = *ref_cu;
13846 struct die_info *die;
13847
13848 die = follow_die_offset (offset, ref_cu);
13849 if (!die)
13850 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13851 "at 0x%x [in module %s]"),
13852 offset, src_die->offset, cu->objfile->name);
13853
13854 return die;
13855 }
13856
13857 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
13858 Returned value is intended for DW_OP_call*. Returned
13859 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
13860
13861 struct dwarf2_locexpr_baton
13862 dwarf2_fetch_die_location_block (unsigned int offset,
13863 struct dwarf2_per_cu_data *per_cu,
13864 CORE_ADDR (*get_frame_pc) (void *baton),
13865 void *baton)
13866 {
13867 struct dwarf2_cu *cu;
13868 struct die_info *die;
13869 struct attribute *attr;
13870 struct dwarf2_locexpr_baton retval;
13871
13872 dw2_setup (per_cu->objfile);
13873
13874 if (per_cu->cu == NULL)
13875 load_cu (per_cu);
13876 cu = per_cu->cu;
13877
13878 die = follow_die_offset (offset, &cu);
13879 if (!die)
13880 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13881 offset, per_cu->cu->objfile->name);
13882
13883 attr = dwarf2_attr (die, DW_AT_location, cu);
13884 if (!attr)
13885 {
13886 /* DWARF: "If there is no such attribute, then there is no effect.". */
13887
13888 retval.data = NULL;
13889 retval.size = 0;
13890 }
13891 else if (attr_form_is_section_offset (attr))
13892 {
13893 struct dwarf2_loclist_baton loclist_baton;
13894 CORE_ADDR pc = (*get_frame_pc) (baton);
13895 size_t size;
13896
13897 fill_in_loclist_baton (cu, &loclist_baton, attr);
13898
13899 retval.data = dwarf2_find_location_expression (&loclist_baton,
13900 &size, pc);
13901 retval.size = size;
13902 }
13903 else
13904 {
13905 if (!attr_form_is_block (attr))
13906 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13907 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13908 offset, per_cu->cu->objfile->name);
13909
13910 retval.data = DW_BLOCK (attr)->data;
13911 retval.size = DW_BLOCK (attr)->size;
13912 }
13913 retval.per_cu = cu->per_cu;
13914
13915 age_cached_comp_units ();
13916
13917 return retval;
13918 }
13919
13920 /* Return the type of the DIE at DIE_OFFSET in the CU named by
13921 PER_CU. */
13922
13923 struct type *
13924 dwarf2_get_die_type (unsigned int die_offset,
13925 struct dwarf2_per_cu_data *per_cu)
13926 {
13927 dw2_setup (per_cu->objfile);
13928 return get_die_type_at_offset (die_offset, per_cu);
13929 }
13930
13931 /* Follow the signature attribute ATTR in SRC_DIE.
13932 On entry *REF_CU is the CU of SRC_DIE.
13933 On exit *REF_CU is the CU of the result. */
13934
13935 static struct die_info *
13936 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13937 struct dwarf2_cu **ref_cu)
13938 {
13939 struct objfile *objfile = (*ref_cu)->objfile;
13940 struct die_info temp_die;
13941 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13942 struct dwarf2_cu *sig_cu;
13943 struct die_info *die;
13944
13945 /* sig_type will be NULL if the signatured type is missing from
13946 the debug info. */
13947 if (sig_type == NULL)
13948 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13949 "at 0x%x [in module %s]"),
13950 src_die->offset, objfile->name);
13951
13952 /* If necessary, add it to the queue and load its DIEs. */
13953
13954 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13955 read_signatured_type (objfile, sig_type);
13956
13957 gdb_assert (sig_type->per_cu.cu != NULL);
13958
13959 sig_cu = sig_type->per_cu.cu;
13960 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13961 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13962 if (die)
13963 {
13964 *ref_cu = sig_cu;
13965 return die;
13966 }
13967
13968 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13969 "from DIE at 0x%x [in module %s]"),
13970 sig_type->type_offset, src_die->offset, objfile->name);
13971 }
13972
13973 /* Given an offset of a signatured type, return its signatured_type. */
13974
13975 static struct signatured_type *
13976 lookup_signatured_type_at_offset (struct objfile *objfile,
13977 struct dwarf2_section_info *section,
13978 unsigned int offset)
13979 {
13980 gdb_byte *info_ptr = section->buffer + offset;
13981 unsigned int length, initial_length_size;
13982 unsigned int sig_offset;
13983 struct signatured_type find_entry, *type_sig;
13984
13985 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13986 sig_offset = (initial_length_size
13987 + 2 /*version*/
13988 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13989 + 1 /*address_size*/);
13990 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13991 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13992
13993 /* This is only used to lookup previously recorded types.
13994 If we didn't find it, it's our bug. */
13995 gdb_assert (type_sig != NULL);
13996 gdb_assert (offset == type_sig->per_cu.offset);
13997
13998 return type_sig;
13999 }
14000
14001 /* Read in signatured type at OFFSET and build its CU and die(s). */
14002
14003 static void
14004 read_signatured_type_at_offset (struct objfile *objfile,
14005 struct dwarf2_section_info *sect,
14006 unsigned int offset)
14007 {
14008 struct signatured_type *type_sig;
14009
14010 dwarf2_read_section (objfile, sect);
14011
14012 /* We have the section offset, but we need the signature to do the
14013 hash table lookup. */
14014 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14015
14016 gdb_assert (type_sig->per_cu.cu == NULL);
14017
14018 read_signatured_type (objfile, type_sig);
14019
14020 gdb_assert (type_sig->per_cu.cu != NULL);
14021 }
14022
14023 /* Read in a signatured type and build its CU and DIEs. */
14024
14025 static void
14026 read_signatured_type (struct objfile *objfile,
14027 struct signatured_type *type_sig)
14028 {
14029 gdb_byte *types_ptr;
14030 struct die_reader_specs reader_specs;
14031 struct dwarf2_cu *cu;
14032 ULONGEST signature;
14033 struct cleanup *back_to, *free_cu_cleanup;
14034 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
14035
14036 dwarf2_read_section (objfile, section);
14037 types_ptr = section->buffer + type_sig->per_cu.offset;
14038
14039 gdb_assert (type_sig->per_cu.cu == NULL);
14040
14041 cu = xmalloc (sizeof (*cu));
14042 init_one_comp_unit (cu, objfile);
14043
14044 type_sig->per_cu.cu = cu;
14045 cu->per_cu = &type_sig->per_cu;
14046
14047 /* If an error occurs while loading, release our storage. */
14048 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14049
14050 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14051 types_ptr, objfile->obfd);
14052 gdb_assert (signature == type_sig->signature);
14053
14054 cu->die_hash
14055 = htab_create_alloc_ex (cu->header.length / 12,
14056 die_hash,
14057 die_eq,
14058 NULL,
14059 &cu->comp_unit_obstack,
14060 hashtab_obstack_allocate,
14061 dummy_obstack_deallocate);
14062
14063 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14064 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14065
14066 init_cu_die_reader (&reader_specs, cu);
14067
14068 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14069 NULL /*parent*/);
14070
14071 /* We try not to read any attributes in this function, because not
14072 all objfiles needed for references have been loaded yet, and symbol
14073 table processing isn't initialized. But we have to set the CU language,
14074 or we won't be able to build types correctly. */
14075 prepare_one_comp_unit (cu, cu->dies);
14076
14077 do_cleanups (back_to);
14078
14079 /* We've successfully allocated this compilation unit. Let our caller
14080 clean it up when finished with it. */
14081 discard_cleanups (free_cu_cleanup);
14082
14083 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14084 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14085 }
14086
14087 /* Workaround as dwarf_expr_context_funcs.read_mem implementation before
14088 a proper runtime DWARF expressions evaluator gets implemented.
14089 Otherwise gnuv3_baseclass_offset would error by:
14090 Expected a negative vbase offset (old compiler?) */
14091
14092 static void
14093 decode_locdesc_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr,
14094 size_t length)
14095 {
14096 struct dwarf_expr_context *ctx = baton;
14097 struct gdbarch *gdbarch = ctx->gdbarch;
14098 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
14099
14100 memset (buf, 0, length);
14101
14102 if (TYPE_LENGTH (ptr_type) == length)
14103 store_typed_address (buf, ptr_type, addr);
14104 }
14105
14106 static const struct dwarf_expr_context_funcs decode_locdesc_ctx_funcs =
14107 {
14108 ctx_no_read_reg,
14109 decode_locdesc_read_mem,
14110 ctx_no_get_frame_base,
14111 ctx_no_get_frame_cfa,
14112 ctx_no_get_frame_pc,
14113 ctx_no_get_tls_address,
14114 ctx_no_dwarf_call,
14115 ctx_no_get_base_type
14116 };
14117
14118 /* Decode simple location descriptions.
14119 Given a pointer to a dwarf block that defines a location, compute
14120 the location and return the value.
14121
14122 NOTE drow/2003-11-18: This function is called in two situations
14123 now: for the address of static or global variables (partial symbols
14124 only) and for offsets into structures which are expected to be
14125 (more or less) constant. The partial symbol case should go away,
14126 and only the constant case should remain. That will let this
14127 function complain more accurately. A few special modes are allowed
14128 without complaint for global variables (for instance, global
14129 register values and thread-local values).
14130
14131 A location description containing no operations indicates that the
14132 object is optimized out. The return value is 0 for that case.
14133 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14134 callers will only want a very basic result and this can become a
14135 complaint. */
14136
14137 static CORE_ADDR
14138 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14139 {
14140 struct objfile *objfile = cu->objfile;
14141 struct dwarf_expr_context *ctx;
14142 struct cleanup *old_chain;
14143 volatile struct gdb_exception ex;
14144
14145 ctx = new_dwarf_expr_context ();
14146 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
14147 make_cleanup_value_free_to_mark (value_mark ());
14148
14149 ctx->gdbarch = get_objfile_arch (objfile);
14150 ctx->addr_size = cu->header.addr_size;
14151 ctx->offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14152 ctx->baton = ctx;
14153 ctx->funcs = &decode_locdesc_ctx_funcs;
14154
14155 /* DW_AT_data_member_location expects the structure address to be pushed on
14156 the stack. Simulate the offset by address 0. */
14157 dwarf_expr_push_address (ctx, 0, 0);
14158
14159 TRY_CATCH (ex, RETURN_MASK_ERROR)
14160 {
14161 dwarf_expr_eval (ctx, blk->data, blk->size);
14162 }
14163 if (ex.reason < 0)
14164 {
14165 if (ex.message)
14166 complaint (&symfile_complaints, "%s", ex.message);
14167 }
14168 else if (ctx->num_pieces == 0)
14169 switch (ctx->location)
14170 {
14171 /* The returned number will be bogus, just do not complain for locations
14172 in global registers - it is here only a partial symbol address. */
14173 case DWARF_VALUE_REGISTER:
14174
14175 case DWARF_VALUE_MEMORY:
14176 case DWARF_VALUE_STACK:
14177 {
14178 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
14179
14180 do_cleanups (old_chain);
14181 return address;
14182 }
14183 }
14184
14185 do_cleanups (old_chain);
14186 dwarf2_complex_location_expr_complaint ();
14187 return 0;
14188 }
14189
14190 /* memory allocation interface */
14191
14192 static struct dwarf_block *
14193 dwarf_alloc_block (struct dwarf2_cu *cu)
14194 {
14195 struct dwarf_block *blk;
14196
14197 blk = (struct dwarf_block *)
14198 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14199 return (blk);
14200 }
14201
14202 static struct abbrev_info *
14203 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14204 {
14205 struct abbrev_info *abbrev;
14206
14207 abbrev = (struct abbrev_info *)
14208 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14209 memset (abbrev, 0, sizeof (struct abbrev_info));
14210 return (abbrev);
14211 }
14212
14213 static struct die_info *
14214 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14215 {
14216 struct die_info *die;
14217 size_t size = sizeof (struct die_info);
14218
14219 if (num_attrs > 1)
14220 size += (num_attrs - 1) * sizeof (struct attribute);
14221
14222 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14223 memset (die, 0, sizeof (struct die_info));
14224 return (die);
14225 }
14226
14227 \f
14228 /* Macro support. */
14229
14230 /* Return the full name of file number I in *LH's file name table.
14231 Use COMP_DIR as the name of the current directory of the
14232 compilation. The result is allocated using xmalloc; the caller is
14233 responsible for freeing it. */
14234 static char *
14235 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14236 {
14237 /* Is the file number a valid index into the line header's file name
14238 table? Remember that file numbers start with one, not zero. */
14239 if (1 <= file && file <= lh->num_file_names)
14240 {
14241 struct file_entry *fe = &lh->file_names[file - 1];
14242
14243 if (IS_ABSOLUTE_PATH (fe->name))
14244 return xstrdup (fe->name);
14245 else
14246 {
14247 const char *dir;
14248 int dir_len;
14249 char *full_name;
14250
14251 if (fe->dir_index)
14252 dir = lh->include_dirs[fe->dir_index - 1];
14253 else
14254 dir = comp_dir;
14255
14256 if (dir)
14257 {
14258 dir_len = strlen (dir);
14259 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14260 strcpy (full_name, dir);
14261 full_name[dir_len] = '/';
14262 strcpy (full_name + dir_len + 1, fe->name);
14263 return full_name;
14264 }
14265 else
14266 return xstrdup (fe->name);
14267 }
14268 }
14269 else
14270 {
14271 /* The compiler produced a bogus file number. We can at least
14272 record the macro definitions made in the file, even if we
14273 won't be able to find the file by name. */
14274 char fake_name[80];
14275
14276 sprintf (fake_name, "<bad macro file number %d>", file);
14277
14278 complaint (&symfile_complaints,
14279 _("bad file number in macro information (%d)"),
14280 file);
14281
14282 return xstrdup (fake_name);
14283 }
14284 }
14285
14286
14287 static struct macro_source_file *
14288 macro_start_file (int file, int line,
14289 struct macro_source_file *current_file,
14290 const char *comp_dir,
14291 struct line_header *lh, struct objfile *objfile)
14292 {
14293 /* The full name of this source file. */
14294 char *full_name = file_full_name (file, lh, comp_dir);
14295
14296 /* We don't create a macro table for this compilation unit
14297 at all until we actually get a filename. */
14298 if (! pending_macros)
14299 pending_macros = new_macro_table (&objfile->objfile_obstack,
14300 objfile->macro_cache);
14301
14302 if (! current_file)
14303 /* If we have no current file, then this must be the start_file
14304 directive for the compilation unit's main source file. */
14305 current_file = macro_set_main (pending_macros, full_name);
14306 else
14307 current_file = macro_include (current_file, line, full_name);
14308
14309 xfree (full_name);
14310
14311 return current_file;
14312 }
14313
14314
14315 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14316 followed by a null byte. */
14317 static char *
14318 copy_string (const char *buf, int len)
14319 {
14320 char *s = xmalloc (len + 1);
14321
14322 memcpy (s, buf, len);
14323 s[len] = '\0';
14324 return s;
14325 }
14326
14327
14328 static const char *
14329 consume_improper_spaces (const char *p, const char *body)
14330 {
14331 if (*p == ' ')
14332 {
14333 complaint (&symfile_complaints,
14334 _("macro definition contains spaces "
14335 "in formal argument list:\n`%s'"),
14336 body);
14337
14338 while (*p == ' ')
14339 p++;
14340 }
14341
14342 return p;
14343 }
14344
14345
14346 static void
14347 parse_macro_definition (struct macro_source_file *file, int line,
14348 const char *body)
14349 {
14350 const char *p;
14351
14352 /* The body string takes one of two forms. For object-like macro
14353 definitions, it should be:
14354
14355 <macro name> " " <definition>
14356
14357 For function-like macro definitions, it should be:
14358
14359 <macro name> "() " <definition>
14360 or
14361 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14362
14363 Spaces may appear only where explicitly indicated, and in the
14364 <definition>.
14365
14366 The Dwarf 2 spec says that an object-like macro's name is always
14367 followed by a space, but versions of GCC around March 2002 omit
14368 the space when the macro's definition is the empty string.
14369
14370 The Dwarf 2 spec says that there should be no spaces between the
14371 formal arguments in a function-like macro's formal argument list,
14372 but versions of GCC around March 2002 include spaces after the
14373 commas. */
14374
14375
14376 /* Find the extent of the macro name. The macro name is terminated
14377 by either a space or null character (for an object-like macro) or
14378 an opening paren (for a function-like macro). */
14379 for (p = body; *p; p++)
14380 if (*p == ' ' || *p == '(')
14381 break;
14382
14383 if (*p == ' ' || *p == '\0')
14384 {
14385 /* It's an object-like macro. */
14386 int name_len = p - body;
14387 char *name = copy_string (body, name_len);
14388 const char *replacement;
14389
14390 if (*p == ' ')
14391 replacement = body + name_len + 1;
14392 else
14393 {
14394 dwarf2_macro_malformed_definition_complaint (body);
14395 replacement = body + name_len;
14396 }
14397
14398 macro_define_object (file, line, name, replacement);
14399
14400 xfree (name);
14401 }
14402 else if (*p == '(')
14403 {
14404 /* It's a function-like macro. */
14405 char *name = copy_string (body, p - body);
14406 int argc = 0;
14407 int argv_size = 1;
14408 char **argv = xmalloc (argv_size * sizeof (*argv));
14409
14410 p++;
14411
14412 p = consume_improper_spaces (p, body);
14413
14414 /* Parse the formal argument list. */
14415 while (*p && *p != ')')
14416 {
14417 /* Find the extent of the current argument name. */
14418 const char *arg_start = p;
14419
14420 while (*p && *p != ',' && *p != ')' && *p != ' ')
14421 p++;
14422
14423 if (! *p || p == arg_start)
14424 dwarf2_macro_malformed_definition_complaint (body);
14425 else
14426 {
14427 /* Make sure argv has room for the new argument. */
14428 if (argc >= argv_size)
14429 {
14430 argv_size *= 2;
14431 argv = xrealloc (argv, argv_size * sizeof (*argv));
14432 }
14433
14434 argv[argc++] = copy_string (arg_start, p - arg_start);
14435 }
14436
14437 p = consume_improper_spaces (p, body);
14438
14439 /* Consume the comma, if present. */
14440 if (*p == ',')
14441 {
14442 p++;
14443
14444 p = consume_improper_spaces (p, body);
14445 }
14446 }
14447
14448 if (*p == ')')
14449 {
14450 p++;
14451
14452 if (*p == ' ')
14453 /* Perfectly formed definition, no complaints. */
14454 macro_define_function (file, line, name,
14455 argc, (const char **) argv,
14456 p + 1);
14457 else if (*p == '\0')
14458 {
14459 /* Complain, but do define it. */
14460 dwarf2_macro_malformed_definition_complaint (body);
14461 macro_define_function (file, line, name,
14462 argc, (const char **) argv,
14463 p);
14464 }
14465 else
14466 /* Just complain. */
14467 dwarf2_macro_malformed_definition_complaint (body);
14468 }
14469 else
14470 /* Just complain. */
14471 dwarf2_macro_malformed_definition_complaint (body);
14472
14473 xfree (name);
14474 {
14475 int i;
14476
14477 for (i = 0; i < argc; i++)
14478 xfree (argv[i]);
14479 }
14480 xfree (argv);
14481 }
14482 else
14483 dwarf2_macro_malformed_definition_complaint (body);
14484 }
14485
14486 /* Skip some bytes from BYTES according to the form given in FORM.
14487 Returns the new pointer. */
14488
14489 static gdb_byte *
14490 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14491 enum dwarf_form form,
14492 unsigned int offset_size,
14493 struct dwarf2_section_info *section)
14494 {
14495 unsigned int bytes_read;
14496
14497 switch (form)
14498 {
14499 case DW_FORM_data1:
14500 case DW_FORM_flag:
14501 ++bytes;
14502 break;
14503
14504 case DW_FORM_data2:
14505 bytes += 2;
14506 break;
14507
14508 case DW_FORM_data4:
14509 bytes += 4;
14510 break;
14511
14512 case DW_FORM_data8:
14513 bytes += 8;
14514 break;
14515
14516 case DW_FORM_string:
14517 read_direct_string (abfd, bytes, &bytes_read);
14518 bytes += bytes_read;
14519 break;
14520
14521 case DW_FORM_sec_offset:
14522 case DW_FORM_strp:
14523 bytes += offset_size;
14524 break;
14525
14526 case DW_FORM_block:
14527 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14528 bytes += bytes_read;
14529 break;
14530
14531 case DW_FORM_block1:
14532 bytes += 1 + read_1_byte (abfd, bytes);
14533 break;
14534 case DW_FORM_block2:
14535 bytes += 2 + read_2_bytes (abfd, bytes);
14536 break;
14537 case DW_FORM_block4:
14538 bytes += 4 + read_4_bytes (abfd, bytes);
14539 break;
14540
14541 case DW_FORM_sdata:
14542 case DW_FORM_udata:
14543 bytes = skip_leb128 (abfd, bytes);
14544 break;
14545
14546 default:
14547 {
14548 complain:
14549 complaint (&symfile_complaints,
14550 _("invalid form 0x%x in `%s'"),
14551 form,
14552 section->asection->name);
14553 return NULL;
14554 }
14555 }
14556
14557 return bytes;
14558 }
14559
14560 /* A helper for dwarf_decode_macros that handles skipping an unknown
14561 opcode. Returns an updated pointer to the macro data buffer; or,
14562 on error, issues a complaint and returns NULL. */
14563
14564 static gdb_byte *
14565 skip_unknown_opcode (unsigned int opcode,
14566 gdb_byte **opcode_definitions,
14567 gdb_byte *mac_ptr,
14568 bfd *abfd,
14569 unsigned int offset_size,
14570 struct dwarf2_section_info *section)
14571 {
14572 unsigned int bytes_read, i;
14573 unsigned long arg;
14574 gdb_byte *defn;
14575
14576 if (opcode_definitions[opcode] == NULL)
14577 {
14578 complaint (&symfile_complaints,
14579 _("unrecognized DW_MACFINO opcode 0x%x"),
14580 opcode);
14581 return NULL;
14582 }
14583
14584 defn = opcode_definitions[opcode];
14585 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
14586 defn += bytes_read;
14587
14588 for (i = 0; i < arg; ++i)
14589 {
14590 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
14591 if (mac_ptr == NULL)
14592 {
14593 /* skip_form_bytes already issued the complaint. */
14594 return NULL;
14595 }
14596 }
14597
14598 return mac_ptr;
14599 }
14600
14601 /* A helper function which parses the header of a macro section.
14602 If the macro section is the extended (for now called "GNU") type,
14603 then this updates *OFFSET_SIZE. Returns a pointer to just after
14604 the header, or issues a complaint and returns NULL on error. */
14605
14606 static gdb_byte *
14607 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
14608 bfd *abfd,
14609 gdb_byte *mac_ptr,
14610 unsigned int *offset_size,
14611 int section_is_gnu)
14612 {
14613 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
14614
14615 if (section_is_gnu)
14616 {
14617 unsigned int version, flags;
14618
14619 version = read_2_bytes (abfd, mac_ptr);
14620 if (version != 4)
14621 {
14622 complaint (&symfile_complaints,
14623 _("unrecognized version `%d' in .debug_macro section"),
14624 version);
14625 return NULL;
14626 }
14627 mac_ptr += 2;
14628
14629 flags = read_1_byte (abfd, mac_ptr);
14630 ++mac_ptr;
14631 *offset_size = (flags & 1) ? 8 : 4;
14632
14633 if ((flags & 2) != 0)
14634 /* We don't need the line table offset. */
14635 mac_ptr += *offset_size;
14636
14637 /* Vendor opcode descriptions. */
14638 if ((flags & 4) != 0)
14639 {
14640 unsigned int i, count;
14641
14642 count = read_1_byte (abfd, mac_ptr);
14643 ++mac_ptr;
14644 for (i = 0; i < count; ++i)
14645 {
14646 unsigned int opcode, bytes_read;
14647 unsigned long arg;
14648
14649 opcode = read_1_byte (abfd, mac_ptr);
14650 ++mac_ptr;
14651 opcode_definitions[opcode] = mac_ptr;
14652 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14653 mac_ptr += bytes_read;
14654 mac_ptr += arg;
14655 }
14656 }
14657 }
14658
14659 return mac_ptr;
14660 }
14661
14662 /* A helper for dwarf_decode_macros that handles the GNU extensions,
14663 including DW_GNU_MACINFO_transparent_include. */
14664
14665 static void
14666 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
14667 struct macro_source_file *current_file,
14668 struct line_header *lh, char *comp_dir,
14669 struct dwarf2_section_info *section,
14670 int section_is_gnu,
14671 unsigned int offset_size,
14672 struct objfile *objfile)
14673 {
14674 enum dwarf_macro_record_type macinfo_type;
14675 int at_commandline;
14676 gdb_byte *opcode_definitions[256];
14677
14678 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
14679 &offset_size, section_is_gnu);
14680 if (mac_ptr == NULL)
14681 {
14682 /* We already issued a complaint. */
14683 return;
14684 }
14685
14686 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14687 GDB is still reading the definitions from command line. First
14688 DW_MACINFO_start_file will need to be ignored as it was already executed
14689 to create CURRENT_FILE for the main source holding also the command line
14690 definitions. On first met DW_MACINFO_start_file this flag is reset to
14691 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14692
14693 at_commandline = 1;
14694
14695 do
14696 {
14697 /* Do we at least have room for a macinfo type byte? */
14698 if (mac_ptr >= mac_end)
14699 {
14700 dwarf2_macros_too_long_complaint (section);
14701 break;
14702 }
14703
14704 macinfo_type = read_1_byte (abfd, mac_ptr);
14705 mac_ptr++;
14706
14707 /* Note that we rely on the fact that the corresponding GNU and
14708 DWARF constants are the same. */
14709 switch (macinfo_type)
14710 {
14711 /* A zero macinfo type indicates the end of the macro
14712 information. */
14713 case 0:
14714 break;
14715
14716 case DW_MACRO_GNU_define:
14717 case DW_MACRO_GNU_undef:
14718 case DW_MACRO_GNU_define_indirect:
14719 case DW_MACRO_GNU_undef_indirect:
14720 {
14721 unsigned int bytes_read;
14722 int line;
14723 char *body;
14724 int is_define;
14725
14726 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14727 mac_ptr += bytes_read;
14728
14729 if (macinfo_type == DW_MACRO_GNU_define
14730 || macinfo_type == DW_MACRO_GNU_undef)
14731 {
14732 body = read_direct_string (abfd, mac_ptr, &bytes_read);
14733 mac_ptr += bytes_read;
14734 }
14735 else
14736 {
14737 LONGEST str_offset;
14738
14739 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
14740 mac_ptr += offset_size;
14741
14742 body = read_indirect_string_at_offset (abfd, str_offset);
14743 }
14744
14745 is_define = (macinfo_type == DW_MACRO_GNU_define
14746 || macinfo_type == DW_MACRO_GNU_define_indirect);
14747 if (! current_file)
14748 {
14749 /* DWARF violation as no main source is present. */
14750 complaint (&symfile_complaints,
14751 _("debug info with no main source gives macro %s "
14752 "on line %d: %s"),
14753 is_define ? _("definition") : _("undefinition"),
14754 line, body);
14755 break;
14756 }
14757 if ((line == 0 && !at_commandline)
14758 || (line != 0 && at_commandline))
14759 complaint (&symfile_complaints,
14760 _("debug info gives %s macro %s with %s line %d: %s"),
14761 at_commandline ? _("command-line") : _("in-file"),
14762 is_define ? _("definition") : _("undefinition"),
14763 line == 0 ? _("zero") : _("non-zero"), line, body);
14764
14765 if (is_define)
14766 parse_macro_definition (current_file, line, body);
14767 else
14768 {
14769 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
14770 || macinfo_type == DW_MACRO_GNU_undef_indirect);
14771 macro_undef (current_file, line, body);
14772 }
14773 }
14774 break;
14775
14776 case DW_MACRO_GNU_start_file:
14777 {
14778 unsigned int bytes_read;
14779 int line, file;
14780
14781 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14782 mac_ptr += bytes_read;
14783 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14784 mac_ptr += bytes_read;
14785
14786 if ((line == 0 && !at_commandline)
14787 || (line != 0 && at_commandline))
14788 complaint (&symfile_complaints,
14789 _("debug info gives source %d included "
14790 "from %s at %s line %d"),
14791 file, at_commandline ? _("command-line") : _("file"),
14792 line == 0 ? _("zero") : _("non-zero"), line);
14793
14794 if (at_commandline)
14795 {
14796 /* This DW_MACRO_GNU_start_file was executed in the
14797 pass one. */
14798 at_commandline = 0;
14799 }
14800 else
14801 current_file = macro_start_file (file, line,
14802 current_file, comp_dir,
14803 lh, objfile);
14804 }
14805 break;
14806
14807 case DW_MACRO_GNU_end_file:
14808 if (! current_file)
14809 complaint (&symfile_complaints,
14810 _("macro debug info has an unmatched "
14811 "`close_file' directive"));
14812 else
14813 {
14814 current_file = current_file->included_by;
14815 if (! current_file)
14816 {
14817 enum dwarf_macro_record_type next_type;
14818
14819 /* GCC circa March 2002 doesn't produce the zero
14820 type byte marking the end of the compilation
14821 unit. Complain if it's not there, but exit no
14822 matter what. */
14823
14824 /* Do we at least have room for a macinfo type byte? */
14825 if (mac_ptr >= mac_end)
14826 {
14827 dwarf2_macros_too_long_complaint (section);
14828 return;
14829 }
14830
14831 /* We don't increment mac_ptr here, so this is just
14832 a look-ahead. */
14833 next_type = read_1_byte (abfd, mac_ptr);
14834 if (next_type != 0)
14835 complaint (&symfile_complaints,
14836 _("no terminating 0-type entry for "
14837 "macros in `.debug_macinfo' section"));
14838
14839 return;
14840 }
14841 }
14842 break;
14843
14844 case DW_MACRO_GNU_transparent_include:
14845 {
14846 LONGEST offset;
14847
14848 offset = read_offset_1 (abfd, mac_ptr, offset_size);
14849 mac_ptr += offset_size;
14850
14851 dwarf_decode_macro_bytes (abfd,
14852 section->buffer + offset,
14853 mac_end, current_file,
14854 lh, comp_dir,
14855 section, section_is_gnu,
14856 offset_size, objfile);
14857 }
14858 break;
14859
14860 case DW_MACINFO_vendor_ext:
14861 if (!section_is_gnu)
14862 {
14863 unsigned int bytes_read;
14864 int constant;
14865
14866 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14867 mac_ptr += bytes_read;
14868 read_direct_string (abfd, mac_ptr, &bytes_read);
14869 mac_ptr += bytes_read;
14870
14871 /* We don't recognize any vendor extensions. */
14872 break;
14873 }
14874 /* FALLTHROUGH */
14875
14876 default:
14877 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
14878 mac_ptr, abfd, offset_size,
14879 section);
14880 if (mac_ptr == NULL)
14881 return;
14882 break;
14883 }
14884 } while (macinfo_type != 0);
14885 }
14886
14887 static void
14888 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14889 char *comp_dir, bfd *abfd,
14890 struct dwarf2_cu *cu,
14891 struct dwarf2_section_info *section,
14892 int section_is_gnu)
14893 {
14894 gdb_byte *mac_ptr, *mac_end;
14895 struct macro_source_file *current_file = 0;
14896 enum dwarf_macro_record_type macinfo_type;
14897 unsigned int offset_size = cu->header.offset_size;
14898 gdb_byte *opcode_definitions[256];
14899
14900 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14901 if (section->buffer == NULL)
14902 {
14903 complaint (&symfile_complaints, _("missing %s section"),
14904 section->asection->name);
14905 return;
14906 }
14907
14908 /* First pass: Find the name of the base filename.
14909 This filename is needed in order to process all macros whose definition
14910 (or undefinition) comes from the command line. These macros are defined
14911 before the first DW_MACINFO_start_file entry, and yet still need to be
14912 associated to the base file.
14913
14914 To determine the base file name, we scan the macro definitions until we
14915 reach the first DW_MACINFO_start_file entry. We then initialize
14916 CURRENT_FILE accordingly so that any macro definition found before the
14917 first DW_MACINFO_start_file can still be associated to the base file. */
14918
14919 mac_ptr = section->buffer + offset;
14920 mac_end = section->buffer + section->size;
14921
14922 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
14923 &offset_size, section_is_gnu);
14924 if (mac_ptr == NULL)
14925 {
14926 /* We already issued a complaint. */
14927 return;
14928 }
14929
14930 do
14931 {
14932 /* Do we at least have room for a macinfo type byte? */
14933 if (mac_ptr >= mac_end)
14934 {
14935 /* Complaint is printed during the second pass as GDB will probably
14936 stop the first pass earlier upon finding
14937 DW_MACINFO_start_file. */
14938 break;
14939 }
14940
14941 macinfo_type = read_1_byte (abfd, mac_ptr);
14942 mac_ptr++;
14943
14944 /* Note that we rely on the fact that the corresponding GNU and
14945 DWARF constants are the same. */
14946 switch (macinfo_type)
14947 {
14948 /* A zero macinfo type indicates the end of the macro
14949 information. */
14950 case 0:
14951 break;
14952
14953 case DW_MACRO_GNU_define:
14954 case DW_MACRO_GNU_undef:
14955 /* Only skip the data by MAC_PTR. */
14956 {
14957 unsigned int bytes_read;
14958
14959 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14960 mac_ptr += bytes_read;
14961 read_direct_string (abfd, mac_ptr, &bytes_read);
14962 mac_ptr += bytes_read;
14963 }
14964 break;
14965
14966 case DW_MACRO_GNU_start_file:
14967 {
14968 unsigned int bytes_read;
14969 int line, file;
14970
14971 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14972 mac_ptr += bytes_read;
14973 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14974 mac_ptr += bytes_read;
14975
14976 current_file = macro_start_file (file, line, current_file,
14977 comp_dir, lh, cu->objfile);
14978 }
14979 break;
14980
14981 case DW_MACRO_GNU_end_file:
14982 /* No data to skip by MAC_PTR. */
14983 break;
14984
14985 case DW_MACRO_GNU_define_indirect:
14986 case DW_MACRO_GNU_undef_indirect:
14987 {
14988 unsigned int bytes_read;
14989
14990 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14991 mac_ptr += bytes_read;
14992 mac_ptr += offset_size;
14993 }
14994 break;
14995
14996 case DW_MACRO_GNU_transparent_include:
14997 /* Note that, according to the spec, a transparent include
14998 chain cannot call DW_MACRO_GNU_start_file. So, we can just
14999 skip this opcode. */
15000 mac_ptr += offset_size;
15001 break;
15002
15003 case DW_MACINFO_vendor_ext:
15004 /* Only skip the data by MAC_PTR. */
15005 if (!section_is_gnu)
15006 {
15007 unsigned int bytes_read;
15008
15009 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15010 mac_ptr += bytes_read;
15011 read_direct_string (abfd, mac_ptr, &bytes_read);
15012 mac_ptr += bytes_read;
15013 }
15014 /* FALLTHROUGH */
15015
15016 default:
15017 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15018 mac_ptr, abfd, offset_size,
15019 section);
15020 if (mac_ptr == NULL)
15021 return;
15022 break;
15023 }
15024 } while (macinfo_type != 0 && current_file == NULL);
15025
15026 /* Second pass: Process all entries.
15027
15028 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15029 command-line macro definitions/undefinitions. This flag is unset when we
15030 reach the first DW_MACINFO_start_file entry. */
15031
15032 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15033 current_file, lh, comp_dir, section, section_is_gnu,
15034 offset_size, cu->objfile);
15035 }
15036
15037 /* Check if the attribute's form is a DW_FORM_block*
15038 if so return true else false. */
15039 static int
15040 attr_form_is_block (struct attribute *attr)
15041 {
15042 return (attr == NULL ? 0 :
15043 attr->form == DW_FORM_block1
15044 || attr->form == DW_FORM_block2
15045 || attr->form == DW_FORM_block4
15046 || attr->form == DW_FORM_block
15047 || attr->form == DW_FORM_exprloc);
15048 }
15049
15050 /* Return non-zero if ATTR's value is a section offset --- classes
15051 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15052 You may use DW_UNSND (attr) to retrieve such offsets.
15053
15054 Section 7.5.4, "Attribute Encodings", explains that no attribute
15055 may have a value that belongs to more than one of these classes; it
15056 would be ambiguous if we did, because we use the same forms for all
15057 of them. */
15058 static int
15059 attr_form_is_section_offset (struct attribute *attr)
15060 {
15061 return (attr->form == DW_FORM_data4
15062 || attr->form == DW_FORM_data8
15063 || attr->form == DW_FORM_sec_offset);
15064 }
15065
15066
15067 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15068 zero otherwise. When this function returns true, you can apply
15069 dwarf2_get_attr_constant_value to it.
15070
15071 However, note that for some attributes you must check
15072 attr_form_is_section_offset before using this test. DW_FORM_data4
15073 and DW_FORM_data8 are members of both the constant class, and of
15074 the classes that contain offsets into other debug sections
15075 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15076 that, if an attribute's can be either a constant or one of the
15077 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15078 taken as section offsets, not constants. */
15079 static int
15080 attr_form_is_constant (struct attribute *attr)
15081 {
15082 switch (attr->form)
15083 {
15084 case DW_FORM_sdata:
15085 case DW_FORM_udata:
15086 case DW_FORM_data1:
15087 case DW_FORM_data2:
15088 case DW_FORM_data4:
15089 case DW_FORM_data8:
15090 return 1;
15091 default:
15092 return 0;
15093 }
15094 }
15095
15096 /* A helper function that fills in a dwarf2_loclist_baton. */
15097
15098 static void
15099 fill_in_loclist_baton (struct dwarf2_cu *cu,
15100 struct dwarf2_loclist_baton *baton,
15101 struct attribute *attr)
15102 {
15103 dwarf2_read_section (dwarf2_per_objfile->objfile,
15104 &dwarf2_per_objfile->loc);
15105
15106 baton->per_cu = cu->per_cu;
15107 gdb_assert (baton->per_cu);
15108 /* We don't know how long the location list is, but make sure we
15109 don't run off the edge of the section. */
15110 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15111 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15112 baton->base_address = cu->base_address;
15113 }
15114
15115 static void
15116 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15117 struct dwarf2_cu *cu)
15118 {
15119 if (attr_form_is_section_offset (attr)
15120 /* ".debug_loc" may not exist at all, or the offset may be outside
15121 the section. If so, fall through to the complaint in the
15122 other branch. */
15123 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15124 &dwarf2_per_objfile->loc))
15125 {
15126 struct dwarf2_loclist_baton *baton;
15127
15128 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15129 sizeof (struct dwarf2_loclist_baton));
15130
15131 fill_in_loclist_baton (cu, baton, attr);
15132
15133 if (cu->base_known == 0)
15134 complaint (&symfile_complaints,
15135 _("Location list used without "
15136 "specifying the CU base address."));
15137
15138 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15139 SYMBOL_LOCATION_BATON (sym) = baton;
15140 }
15141 else
15142 {
15143 struct dwarf2_locexpr_baton *baton;
15144
15145 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15146 sizeof (struct dwarf2_locexpr_baton));
15147 baton->per_cu = cu->per_cu;
15148 gdb_assert (baton->per_cu);
15149
15150 if (attr_form_is_block (attr))
15151 {
15152 /* Note that we're just copying the block's data pointer
15153 here, not the actual data. We're still pointing into the
15154 info_buffer for SYM's objfile; right now we never release
15155 that buffer, but when we do clean up properly this may
15156 need to change. */
15157 baton->size = DW_BLOCK (attr)->size;
15158 baton->data = DW_BLOCK (attr)->data;
15159 }
15160 else
15161 {
15162 dwarf2_invalid_attrib_class_complaint ("location description",
15163 SYMBOL_NATURAL_NAME (sym));
15164 baton->size = 0;
15165 baton->data = NULL;
15166 }
15167
15168 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15169 SYMBOL_LOCATION_BATON (sym) = baton;
15170 }
15171 }
15172
15173 /* Return the OBJFILE associated with the compilation unit CU. If CU
15174 came from a separate debuginfo file, then the master objfile is
15175 returned. */
15176
15177 struct objfile *
15178 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15179 {
15180 struct objfile *objfile = per_cu->objfile;
15181
15182 /* Return the master objfile, so that we can report and look up the
15183 correct file containing this variable. */
15184 if (objfile->separate_debug_objfile_backlink)
15185 objfile = objfile->separate_debug_objfile_backlink;
15186
15187 return objfile;
15188 }
15189
15190 /* Return the address size given in the compilation unit header for CU. */
15191
15192 CORE_ADDR
15193 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15194 {
15195 if (per_cu->cu)
15196 return per_cu->cu->header.addr_size;
15197 else
15198 {
15199 /* If the CU is not currently read in, we re-read its header. */
15200 struct objfile *objfile = per_cu->objfile;
15201 struct dwarf2_per_objfile *per_objfile
15202 = objfile_data (objfile, dwarf2_objfile_data_key);
15203 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15204 struct comp_unit_head cu_header;
15205
15206 memset (&cu_header, 0, sizeof cu_header);
15207 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15208 return cu_header.addr_size;
15209 }
15210 }
15211
15212 /* Return the offset size given in the compilation unit header for CU. */
15213
15214 int
15215 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15216 {
15217 if (per_cu->cu)
15218 return per_cu->cu->header.offset_size;
15219 else
15220 {
15221 /* If the CU is not currently read in, we re-read its header. */
15222 struct objfile *objfile = per_cu->objfile;
15223 struct dwarf2_per_objfile *per_objfile
15224 = objfile_data (objfile, dwarf2_objfile_data_key);
15225 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15226 struct comp_unit_head cu_header;
15227
15228 memset (&cu_header, 0, sizeof cu_header);
15229 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15230 return cu_header.offset_size;
15231 }
15232 }
15233
15234 /* Return the text offset of the CU. The returned offset comes from
15235 this CU's objfile. If this objfile came from a separate debuginfo
15236 file, then the offset may be different from the corresponding
15237 offset in the parent objfile. */
15238
15239 CORE_ADDR
15240 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15241 {
15242 struct objfile *objfile = per_cu->objfile;
15243
15244 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15245 }
15246
15247 /* Locate the .debug_info compilation unit from CU's objfile which contains
15248 the DIE at OFFSET. Raises an error on failure. */
15249
15250 static struct dwarf2_per_cu_data *
15251 dwarf2_find_containing_comp_unit (unsigned int offset,
15252 struct objfile *objfile)
15253 {
15254 struct dwarf2_per_cu_data *this_cu;
15255 int low, high;
15256
15257 low = 0;
15258 high = dwarf2_per_objfile->n_comp_units - 1;
15259 while (high > low)
15260 {
15261 int mid = low + (high - low) / 2;
15262
15263 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15264 high = mid;
15265 else
15266 low = mid + 1;
15267 }
15268 gdb_assert (low == high);
15269 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15270 {
15271 if (low == 0)
15272 error (_("Dwarf Error: could not find partial DIE containing "
15273 "offset 0x%lx [in module %s]"),
15274 (long) offset, bfd_get_filename (objfile->obfd));
15275
15276 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15277 return dwarf2_per_objfile->all_comp_units[low-1];
15278 }
15279 else
15280 {
15281 this_cu = dwarf2_per_objfile->all_comp_units[low];
15282 if (low == dwarf2_per_objfile->n_comp_units - 1
15283 && offset >= this_cu->offset + this_cu->length)
15284 error (_("invalid dwarf2 offset %u"), offset);
15285 gdb_assert (offset < this_cu->offset + this_cu->length);
15286 return this_cu;
15287 }
15288 }
15289
15290 /* Locate the compilation unit from OBJFILE which is located at exactly
15291 OFFSET. Raises an error on failure. */
15292
15293 static struct dwarf2_per_cu_data *
15294 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15295 {
15296 struct dwarf2_per_cu_data *this_cu;
15297
15298 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15299 if (this_cu->offset != offset)
15300 error (_("no compilation unit with offset %u."), offset);
15301 return this_cu;
15302 }
15303
15304 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
15305
15306 static void
15307 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15308 {
15309 memset (cu, 0, sizeof (*cu));
15310 cu->objfile = objfile;
15311 obstack_init (&cu->comp_unit_obstack);
15312 }
15313
15314 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15315
15316 static void
15317 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15318 {
15319 struct attribute *attr;
15320
15321 /* Set the language we're debugging. */
15322 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15323 if (attr)
15324 set_cu_language (DW_UNSND (attr), cu);
15325 else
15326 {
15327 cu->language = language_minimal;
15328 cu->language_defn = language_def (cu->language);
15329 }
15330 }
15331
15332 /* Release one cached compilation unit, CU. We unlink it from the tree
15333 of compilation units, but we don't remove it from the read_in_chain;
15334 the caller is responsible for that.
15335 NOTE: DATA is a void * because this function is also used as a
15336 cleanup routine. */
15337
15338 static void
15339 free_one_comp_unit (void *data)
15340 {
15341 struct dwarf2_cu *cu = data;
15342
15343 if (cu->per_cu != NULL)
15344 cu->per_cu->cu = NULL;
15345 cu->per_cu = NULL;
15346
15347 obstack_free (&cu->comp_unit_obstack, NULL);
15348
15349 xfree (cu);
15350 }
15351
15352 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15353 when we're finished with it. We can't free the pointer itself, but be
15354 sure to unlink it from the cache. Also release any associated storage
15355 and perform cache maintenance.
15356
15357 Only used during partial symbol parsing. */
15358
15359 static void
15360 free_stack_comp_unit (void *data)
15361 {
15362 struct dwarf2_cu *cu = data;
15363
15364 obstack_free (&cu->comp_unit_obstack, NULL);
15365 cu->partial_dies = NULL;
15366
15367 if (cu->per_cu != NULL)
15368 {
15369 /* This compilation unit is on the stack in our caller, so we
15370 should not xfree it. Just unlink it. */
15371 cu->per_cu->cu = NULL;
15372 cu->per_cu = NULL;
15373
15374 /* If we had a per-cu pointer, then we may have other compilation
15375 units loaded, so age them now. */
15376 age_cached_comp_units ();
15377 }
15378 }
15379
15380 /* Free all cached compilation units. */
15381
15382 static void
15383 free_cached_comp_units (void *data)
15384 {
15385 struct dwarf2_per_cu_data *per_cu, **last_chain;
15386
15387 per_cu = dwarf2_per_objfile->read_in_chain;
15388 last_chain = &dwarf2_per_objfile->read_in_chain;
15389 while (per_cu != NULL)
15390 {
15391 struct dwarf2_per_cu_data *next_cu;
15392
15393 next_cu = per_cu->cu->read_in_chain;
15394
15395 free_one_comp_unit (per_cu->cu);
15396 *last_chain = next_cu;
15397
15398 per_cu = next_cu;
15399 }
15400 }
15401
15402 /* Increase the age counter on each cached compilation unit, and free
15403 any that are too old. */
15404
15405 static void
15406 age_cached_comp_units (void)
15407 {
15408 struct dwarf2_per_cu_data *per_cu, **last_chain;
15409
15410 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15411 per_cu = dwarf2_per_objfile->read_in_chain;
15412 while (per_cu != NULL)
15413 {
15414 per_cu->cu->last_used ++;
15415 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15416 dwarf2_mark (per_cu->cu);
15417 per_cu = per_cu->cu->read_in_chain;
15418 }
15419
15420 per_cu = dwarf2_per_objfile->read_in_chain;
15421 last_chain = &dwarf2_per_objfile->read_in_chain;
15422 while (per_cu != NULL)
15423 {
15424 struct dwarf2_per_cu_data *next_cu;
15425
15426 next_cu = per_cu->cu->read_in_chain;
15427
15428 if (!per_cu->cu->mark)
15429 {
15430 free_one_comp_unit (per_cu->cu);
15431 *last_chain = next_cu;
15432 }
15433 else
15434 last_chain = &per_cu->cu->read_in_chain;
15435
15436 per_cu = next_cu;
15437 }
15438 }
15439
15440 /* Remove a single compilation unit from the cache. */
15441
15442 static void
15443 free_one_cached_comp_unit (void *target_cu)
15444 {
15445 struct dwarf2_per_cu_data *per_cu, **last_chain;
15446
15447 per_cu = dwarf2_per_objfile->read_in_chain;
15448 last_chain = &dwarf2_per_objfile->read_in_chain;
15449 while (per_cu != NULL)
15450 {
15451 struct dwarf2_per_cu_data *next_cu;
15452
15453 next_cu = per_cu->cu->read_in_chain;
15454
15455 if (per_cu->cu == target_cu)
15456 {
15457 free_one_comp_unit (per_cu->cu);
15458 *last_chain = next_cu;
15459 break;
15460 }
15461 else
15462 last_chain = &per_cu->cu->read_in_chain;
15463
15464 per_cu = next_cu;
15465 }
15466 }
15467
15468 /* Release all extra memory associated with OBJFILE. */
15469
15470 void
15471 dwarf2_free_objfile (struct objfile *objfile)
15472 {
15473 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15474
15475 if (dwarf2_per_objfile == NULL)
15476 return;
15477
15478 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15479 free_cached_comp_units (NULL);
15480
15481 if (dwarf2_per_objfile->quick_file_names_table)
15482 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15483
15484 /* Everything else should be on the objfile obstack. */
15485 }
15486
15487 /* A pair of DIE offset and GDB type pointer. We store these
15488 in a hash table separate from the DIEs, and preserve them
15489 when the DIEs are flushed out of cache. */
15490
15491 struct dwarf2_offset_and_type
15492 {
15493 unsigned int offset;
15494 struct type *type;
15495 };
15496
15497 /* Hash function for a dwarf2_offset_and_type. */
15498
15499 static hashval_t
15500 offset_and_type_hash (const void *item)
15501 {
15502 const struct dwarf2_offset_and_type *ofs = item;
15503
15504 return ofs->offset;
15505 }
15506
15507 /* Equality function for a dwarf2_offset_and_type. */
15508
15509 static int
15510 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15511 {
15512 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15513 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15514
15515 return ofs_lhs->offset == ofs_rhs->offset;
15516 }
15517
15518 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15519 table if necessary. For convenience, return TYPE.
15520
15521 The DIEs reading must have careful ordering to:
15522 * Not cause infite loops trying to read in DIEs as a prerequisite for
15523 reading current DIE.
15524 * Not trying to dereference contents of still incompletely read in types
15525 while reading in other DIEs.
15526 * Enable referencing still incompletely read in types just by a pointer to
15527 the type without accessing its fields.
15528
15529 Therefore caller should follow these rules:
15530 * Try to fetch any prerequisite types we may need to build this DIE type
15531 before building the type and calling set_die_type.
15532 * After building type call set_die_type for current DIE as soon as
15533 possible before fetching more types to complete the current type.
15534 * Make the type as complete as possible before fetching more types. */
15535
15536 static struct type *
15537 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15538 {
15539 struct dwarf2_offset_and_type **slot, ofs;
15540 struct objfile *objfile = cu->objfile;
15541 htab_t *type_hash_ptr;
15542
15543 /* For Ada types, make sure that the gnat-specific data is always
15544 initialized (if not already set). There are a few types where
15545 we should not be doing so, because the type-specific area is
15546 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15547 where the type-specific area is used to store the floatformat).
15548 But this is not a problem, because the gnat-specific information
15549 is actually not needed for these types. */
15550 if (need_gnat_info (cu)
15551 && TYPE_CODE (type) != TYPE_CODE_FUNC
15552 && TYPE_CODE (type) != TYPE_CODE_FLT
15553 && !HAVE_GNAT_AUX_INFO (type))
15554 INIT_GNAT_SPECIFIC (type);
15555
15556 if (cu->per_cu->debug_type_section)
15557 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15558 else
15559 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15560
15561 if (*type_hash_ptr == NULL)
15562 {
15563 *type_hash_ptr
15564 = htab_create_alloc_ex (127,
15565 offset_and_type_hash,
15566 offset_and_type_eq,
15567 NULL,
15568 &objfile->objfile_obstack,
15569 hashtab_obstack_allocate,
15570 dummy_obstack_deallocate);
15571 }
15572
15573 ofs.offset = die->offset;
15574 ofs.type = type;
15575 slot = (struct dwarf2_offset_and_type **)
15576 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15577 if (*slot)
15578 complaint (&symfile_complaints,
15579 _("A problem internal to GDB: DIE 0x%x has type already set"),
15580 die->offset);
15581 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15582 **slot = ofs;
15583 return type;
15584 }
15585
15586 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15587 table, or return NULL if the die does not have a saved type. */
15588
15589 static struct type *
15590 get_die_type_at_offset (unsigned int offset,
15591 struct dwarf2_per_cu_data *per_cu)
15592 {
15593 struct dwarf2_offset_and_type *slot, ofs;
15594 htab_t type_hash;
15595
15596 if (per_cu->debug_type_section)
15597 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15598 else
15599 type_hash = dwarf2_per_objfile->debug_info_type_hash;
15600 if (type_hash == NULL)
15601 return NULL;
15602
15603 ofs.offset = offset;
15604 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15605 if (slot)
15606 return slot->type;
15607 else
15608 return NULL;
15609 }
15610
15611 /* Look up the type for DIE in the appropriate type_hash table,
15612 or return NULL if DIE does not have a saved type. */
15613
15614 static struct type *
15615 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15616 {
15617 return get_die_type_at_offset (die->offset, cu->per_cu);
15618 }
15619
15620 /* Add a dependence relationship from CU to REF_PER_CU. */
15621
15622 static void
15623 dwarf2_add_dependence (struct dwarf2_cu *cu,
15624 struct dwarf2_per_cu_data *ref_per_cu)
15625 {
15626 void **slot;
15627
15628 if (cu->dependencies == NULL)
15629 cu->dependencies
15630 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15631 NULL, &cu->comp_unit_obstack,
15632 hashtab_obstack_allocate,
15633 dummy_obstack_deallocate);
15634
15635 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15636 if (*slot == NULL)
15637 *slot = ref_per_cu;
15638 }
15639
15640 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15641 Set the mark field in every compilation unit in the
15642 cache that we must keep because we are keeping CU. */
15643
15644 static int
15645 dwarf2_mark_helper (void **slot, void *data)
15646 {
15647 struct dwarf2_per_cu_data *per_cu;
15648
15649 per_cu = (struct dwarf2_per_cu_data *) *slot;
15650
15651 /* cu->dependencies references may not yet have been ever read if QUIT aborts
15652 reading of the chain. As such dependencies remain valid it is not much
15653 useful to track and undo them during QUIT cleanups. */
15654 if (per_cu->cu == NULL)
15655 return 1;
15656
15657 if (per_cu->cu->mark)
15658 return 1;
15659 per_cu->cu->mark = 1;
15660
15661 if (per_cu->cu->dependencies != NULL)
15662 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15663
15664 return 1;
15665 }
15666
15667 /* Set the mark field in CU and in every other compilation unit in the
15668 cache that we must keep because we are keeping CU. */
15669
15670 static void
15671 dwarf2_mark (struct dwarf2_cu *cu)
15672 {
15673 if (cu->mark)
15674 return;
15675 cu->mark = 1;
15676 if (cu->dependencies != NULL)
15677 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15678 }
15679
15680 static void
15681 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15682 {
15683 while (per_cu)
15684 {
15685 per_cu->cu->mark = 0;
15686 per_cu = per_cu->cu->read_in_chain;
15687 }
15688 }
15689
15690 /* Trivial hash function for partial_die_info: the hash value of a DIE
15691 is its offset in .debug_info for this objfile. */
15692
15693 static hashval_t
15694 partial_die_hash (const void *item)
15695 {
15696 const struct partial_die_info *part_die = item;
15697
15698 return part_die->offset;
15699 }
15700
15701 /* Trivial comparison function for partial_die_info structures: two DIEs
15702 are equal if they have the same offset. */
15703
15704 static int
15705 partial_die_eq (const void *item_lhs, const void *item_rhs)
15706 {
15707 const struct partial_die_info *part_die_lhs = item_lhs;
15708 const struct partial_die_info *part_die_rhs = item_rhs;
15709
15710 return part_die_lhs->offset == part_die_rhs->offset;
15711 }
15712
15713 static struct cmd_list_element *set_dwarf2_cmdlist;
15714 static struct cmd_list_element *show_dwarf2_cmdlist;
15715
15716 static void
15717 set_dwarf2_cmd (char *args, int from_tty)
15718 {
15719 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15720 }
15721
15722 static void
15723 show_dwarf2_cmd (char *args, int from_tty)
15724 {
15725 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15726 }
15727
15728 /* If section described by INFO was mmapped, munmap it now. */
15729
15730 static void
15731 munmap_section_buffer (struct dwarf2_section_info *info)
15732 {
15733 if (info->map_addr != NULL)
15734 {
15735 #ifdef HAVE_MMAP
15736 int res;
15737
15738 res = munmap (info->map_addr, info->map_len);
15739 gdb_assert (res == 0);
15740 #else
15741 /* Without HAVE_MMAP, we should never be here to begin with. */
15742 gdb_assert_not_reached ("no mmap support");
15743 #endif
15744 }
15745 }
15746
15747 /* munmap debug sections for OBJFILE, if necessary. */
15748
15749 static void
15750 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15751 {
15752 struct dwarf2_per_objfile *data = d;
15753 int ix;
15754 struct dwarf2_section_info *section;
15755
15756 /* This is sorted according to the order they're defined in to make it easier
15757 to keep in sync. */
15758 munmap_section_buffer (&data->info);
15759 munmap_section_buffer (&data->abbrev);
15760 munmap_section_buffer (&data->line);
15761 munmap_section_buffer (&data->loc);
15762 munmap_section_buffer (&data->macinfo);
15763 munmap_section_buffer (&data->macro);
15764 munmap_section_buffer (&data->str);
15765 munmap_section_buffer (&data->ranges);
15766 munmap_section_buffer (&data->frame);
15767 munmap_section_buffer (&data->eh_frame);
15768 munmap_section_buffer (&data->gdb_index);
15769
15770 for (ix = 0;
15771 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
15772 ++ix)
15773 munmap_section_buffer (section);
15774
15775 VEC_free (dwarf2_section_info_def, data->types);
15776 }
15777
15778 \f
15779 /* The "save gdb-index" command. */
15780
15781 /* The contents of the hash table we create when building the string
15782 table. */
15783 struct strtab_entry
15784 {
15785 offset_type offset;
15786 const char *str;
15787 };
15788
15789 /* Hash function for a strtab_entry.
15790
15791 Function is used only during write_hash_table so no index format backward
15792 compatibility is needed. */
15793
15794 static hashval_t
15795 hash_strtab_entry (const void *e)
15796 {
15797 const struct strtab_entry *entry = e;
15798 return mapped_index_string_hash (INT_MAX, entry->str);
15799 }
15800
15801 /* Equality function for a strtab_entry. */
15802
15803 static int
15804 eq_strtab_entry (const void *a, const void *b)
15805 {
15806 const struct strtab_entry *ea = a;
15807 const struct strtab_entry *eb = b;
15808 return !strcmp (ea->str, eb->str);
15809 }
15810
15811 /* Create a strtab_entry hash table. */
15812
15813 static htab_t
15814 create_strtab (void)
15815 {
15816 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15817 xfree, xcalloc, xfree);
15818 }
15819
15820 /* Add a string to the constant pool. Return the string's offset in
15821 host order. */
15822
15823 static offset_type
15824 add_string (htab_t table, struct obstack *cpool, const char *str)
15825 {
15826 void **slot;
15827 struct strtab_entry entry;
15828 struct strtab_entry *result;
15829
15830 entry.str = str;
15831 slot = htab_find_slot (table, &entry, INSERT);
15832 if (*slot)
15833 result = *slot;
15834 else
15835 {
15836 result = XNEW (struct strtab_entry);
15837 result->offset = obstack_object_size (cpool);
15838 result->str = str;
15839 obstack_grow_str0 (cpool, str);
15840 *slot = result;
15841 }
15842 return result->offset;
15843 }
15844
15845 /* An entry in the symbol table. */
15846 struct symtab_index_entry
15847 {
15848 /* The name of the symbol. */
15849 const char *name;
15850 /* The offset of the name in the constant pool. */
15851 offset_type index_offset;
15852 /* A sorted vector of the indices of all the CUs that hold an object
15853 of this name. */
15854 VEC (offset_type) *cu_indices;
15855 };
15856
15857 /* The symbol table. This is a power-of-2-sized hash table. */
15858 struct mapped_symtab
15859 {
15860 offset_type n_elements;
15861 offset_type size;
15862 struct symtab_index_entry **data;
15863 };
15864
15865 /* Hash function for a symtab_index_entry. */
15866
15867 static hashval_t
15868 hash_symtab_entry (const void *e)
15869 {
15870 const struct symtab_index_entry *entry = e;
15871 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15872 sizeof (offset_type) * VEC_length (offset_type,
15873 entry->cu_indices),
15874 0);
15875 }
15876
15877 /* Equality function for a symtab_index_entry. */
15878
15879 static int
15880 eq_symtab_entry (const void *a, const void *b)
15881 {
15882 const struct symtab_index_entry *ea = a;
15883 const struct symtab_index_entry *eb = b;
15884 int len = VEC_length (offset_type, ea->cu_indices);
15885 if (len != VEC_length (offset_type, eb->cu_indices))
15886 return 0;
15887 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15888 VEC_address (offset_type, eb->cu_indices),
15889 sizeof (offset_type) * len);
15890 }
15891
15892 /* Destroy a symtab_index_entry. */
15893
15894 static void
15895 delete_symtab_entry (void *p)
15896 {
15897 struct symtab_index_entry *entry = p;
15898 VEC_free (offset_type, entry->cu_indices);
15899 xfree (entry);
15900 }
15901
15902 /* Create a hash table holding symtab_index_entry objects. */
15903
15904 static htab_t
15905 create_symbol_hash_table (void)
15906 {
15907 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15908 delete_symtab_entry, xcalloc, xfree);
15909 }
15910
15911 /* Create a new mapped symtab object. */
15912
15913 static struct mapped_symtab *
15914 create_mapped_symtab (void)
15915 {
15916 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15917 symtab->n_elements = 0;
15918 symtab->size = 1024;
15919 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15920 return symtab;
15921 }
15922
15923 /* Destroy a mapped_symtab. */
15924
15925 static void
15926 cleanup_mapped_symtab (void *p)
15927 {
15928 struct mapped_symtab *symtab = p;
15929 /* The contents of the array are freed when the other hash table is
15930 destroyed. */
15931 xfree (symtab->data);
15932 xfree (symtab);
15933 }
15934
15935 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15936 the slot.
15937
15938 Function is used only during write_hash_table so no index format backward
15939 compatibility is needed. */
15940
15941 static struct symtab_index_entry **
15942 find_slot (struct mapped_symtab *symtab, const char *name)
15943 {
15944 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15945
15946 index = hash & (symtab->size - 1);
15947 step = ((hash * 17) & (symtab->size - 1)) | 1;
15948
15949 for (;;)
15950 {
15951 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15952 return &symtab->data[index];
15953 index = (index + step) & (symtab->size - 1);
15954 }
15955 }
15956
15957 /* Expand SYMTAB's hash table. */
15958
15959 static void
15960 hash_expand (struct mapped_symtab *symtab)
15961 {
15962 offset_type old_size = symtab->size;
15963 offset_type i;
15964 struct symtab_index_entry **old_entries = symtab->data;
15965
15966 symtab->size *= 2;
15967 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15968
15969 for (i = 0; i < old_size; ++i)
15970 {
15971 if (old_entries[i])
15972 {
15973 struct symtab_index_entry **slot = find_slot (symtab,
15974 old_entries[i]->name);
15975 *slot = old_entries[i];
15976 }
15977 }
15978
15979 xfree (old_entries);
15980 }
15981
15982 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15983 is the index of the CU in which the symbol appears. */
15984
15985 static void
15986 add_index_entry (struct mapped_symtab *symtab, const char *name,
15987 offset_type cu_index)
15988 {
15989 struct symtab_index_entry **slot;
15990
15991 ++symtab->n_elements;
15992 if (4 * symtab->n_elements / 3 >= symtab->size)
15993 hash_expand (symtab);
15994
15995 slot = find_slot (symtab, name);
15996 if (!*slot)
15997 {
15998 *slot = XNEW (struct symtab_index_entry);
15999 (*slot)->name = name;
16000 (*slot)->cu_indices = NULL;
16001 }
16002 /* Don't push an index twice. Due to how we add entries we only
16003 have to check the last one. */
16004 if (VEC_empty (offset_type, (*slot)->cu_indices)
16005 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16006 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16007 }
16008
16009 /* Add a vector of indices to the constant pool. */
16010
16011 static offset_type
16012 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16013 struct symtab_index_entry *entry)
16014 {
16015 void **slot;
16016
16017 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16018 if (!*slot)
16019 {
16020 offset_type len = VEC_length (offset_type, entry->cu_indices);
16021 offset_type val = MAYBE_SWAP (len);
16022 offset_type iter;
16023 int i;
16024
16025 *slot = entry;
16026 entry->index_offset = obstack_object_size (cpool);
16027
16028 obstack_grow (cpool, &val, sizeof (val));
16029 for (i = 0;
16030 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16031 ++i)
16032 {
16033 val = MAYBE_SWAP (iter);
16034 obstack_grow (cpool, &val, sizeof (val));
16035 }
16036 }
16037 else
16038 {
16039 struct symtab_index_entry *old_entry = *slot;
16040 entry->index_offset = old_entry->index_offset;
16041 entry = old_entry;
16042 }
16043 return entry->index_offset;
16044 }
16045
16046 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16047 constant pool entries going into the obstack CPOOL. */
16048
16049 static void
16050 write_hash_table (struct mapped_symtab *symtab,
16051 struct obstack *output, struct obstack *cpool)
16052 {
16053 offset_type i;
16054 htab_t symbol_hash_table;
16055 htab_t str_table;
16056
16057 symbol_hash_table = create_symbol_hash_table ();
16058 str_table = create_strtab ();
16059
16060 /* We add all the index vectors to the constant pool first, to
16061 ensure alignment is ok. */
16062 for (i = 0; i < symtab->size; ++i)
16063 {
16064 if (symtab->data[i])
16065 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16066 }
16067
16068 /* Now write out the hash table. */
16069 for (i = 0; i < symtab->size; ++i)
16070 {
16071 offset_type str_off, vec_off;
16072
16073 if (symtab->data[i])
16074 {
16075 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16076 vec_off = symtab->data[i]->index_offset;
16077 }
16078 else
16079 {
16080 /* While 0 is a valid constant pool index, it is not valid
16081 to have 0 for both offsets. */
16082 str_off = 0;
16083 vec_off = 0;
16084 }
16085
16086 str_off = MAYBE_SWAP (str_off);
16087 vec_off = MAYBE_SWAP (vec_off);
16088
16089 obstack_grow (output, &str_off, sizeof (str_off));
16090 obstack_grow (output, &vec_off, sizeof (vec_off));
16091 }
16092
16093 htab_delete (str_table);
16094 htab_delete (symbol_hash_table);
16095 }
16096
16097 /* Struct to map psymtab to CU index in the index file. */
16098 struct psymtab_cu_index_map
16099 {
16100 struct partial_symtab *psymtab;
16101 unsigned int cu_index;
16102 };
16103
16104 static hashval_t
16105 hash_psymtab_cu_index (const void *item)
16106 {
16107 const struct psymtab_cu_index_map *map = item;
16108
16109 return htab_hash_pointer (map->psymtab);
16110 }
16111
16112 static int
16113 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16114 {
16115 const struct psymtab_cu_index_map *lhs = item_lhs;
16116 const struct psymtab_cu_index_map *rhs = item_rhs;
16117
16118 return lhs->psymtab == rhs->psymtab;
16119 }
16120
16121 /* Helper struct for building the address table. */
16122 struct addrmap_index_data
16123 {
16124 struct objfile *objfile;
16125 struct obstack *addr_obstack;
16126 htab_t cu_index_htab;
16127
16128 /* Non-zero if the previous_* fields are valid.
16129 We can't write an entry until we see the next entry (since it is only then
16130 that we know the end of the entry). */
16131 int previous_valid;
16132 /* Index of the CU in the table of all CUs in the index file. */
16133 unsigned int previous_cu_index;
16134 /* Start address of the CU. */
16135 CORE_ADDR previous_cu_start;
16136 };
16137
16138 /* Write an address entry to OBSTACK. */
16139
16140 static void
16141 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16142 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16143 {
16144 offset_type cu_index_to_write;
16145 char addr[8];
16146 CORE_ADDR baseaddr;
16147
16148 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16149
16150 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16151 obstack_grow (obstack, addr, 8);
16152 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16153 obstack_grow (obstack, addr, 8);
16154 cu_index_to_write = MAYBE_SWAP (cu_index);
16155 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16156 }
16157
16158 /* Worker function for traversing an addrmap to build the address table. */
16159
16160 static int
16161 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16162 {
16163 struct addrmap_index_data *data = datap;
16164 struct partial_symtab *pst = obj;
16165 offset_type cu_index;
16166 void **slot;
16167
16168 if (data->previous_valid)
16169 add_address_entry (data->objfile, data->addr_obstack,
16170 data->previous_cu_start, start_addr,
16171 data->previous_cu_index);
16172
16173 data->previous_cu_start = start_addr;
16174 if (pst != NULL)
16175 {
16176 struct psymtab_cu_index_map find_map, *map;
16177 find_map.psymtab = pst;
16178 map = htab_find (data->cu_index_htab, &find_map);
16179 gdb_assert (map != NULL);
16180 data->previous_cu_index = map->cu_index;
16181 data->previous_valid = 1;
16182 }
16183 else
16184 data->previous_valid = 0;
16185
16186 return 0;
16187 }
16188
16189 /* Write OBJFILE's address map to OBSTACK.
16190 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16191 in the index file. */
16192
16193 static void
16194 write_address_map (struct objfile *objfile, struct obstack *obstack,
16195 htab_t cu_index_htab)
16196 {
16197 struct addrmap_index_data addrmap_index_data;
16198
16199 /* When writing the address table, we have to cope with the fact that
16200 the addrmap iterator only provides the start of a region; we have to
16201 wait until the next invocation to get the start of the next region. */
16202
16203 addrmap_index_data.objfile = objfile;
16204 addrmap_index_data.addr_obstack = obstack;
16205 addrmap_index_data.cu_index_htab = cu_index_htab;
16206 addrmap_index_data.previous_valid = 0;
16207
16208 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16209 &addrmap_index_data);
16210
16211 /* It's highly unlikely the last entry (end address = 0xff...ff)
16212 is valid, but we should still handle it.
16213 The end address is recorded as the start of the next region, but that
16214 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16215 anyway. */
16216 if (addrmap_index_data.previous_valid)
16217 add_address_entry (objfile, obstack,
16218 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16219 addrmap_index_data.previous_cu_index);
16220 }
16221
16222 /* Add a list of partial symbols to SYMTAB. */
16223
16224 static void
16225 write_psymbols (struct mapped_symtab *symtab,
16226 htab_t psyms_seen,
16227 struct partial_symbol **psymp,
16228 int count,
16229 offset_type cu_index,
16230 int is_static)
16231 {
16232 for (; count-- > 0; ++psymp)
16233 {
16234 void **slot, *lookup;
16235
16236 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16237 error (_("Ada is not currently supported by the index"));
16238
16239 /* We only want to add a given psymbol once. However, we also
16240 want to account for whether it is global or static. So, we
16241 may add it twice, using slightly different values. */
16242 if (is_static)
16243 {
16244 uintptr_t val = 1 | (uintptr_t) *psymp;
16245
16246 lookup = (void *) val;
16247 }
16248 else
16249 lookup = *psymp;
16250
16251 /* Only add a given psymbol once. */
16252 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16253 if (!*slot)
16254 {
16255 *slot = lookup;
16256 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16257 }
16258 }
16259 }
16260
16261 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16262 exception if there is an error. */
16263
16264 static void
16265 write_obstack (FILE *file, struct obstack *obstack)
16266 {
16267 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16268 file)
16269 != obstack_object_size (obstack))
16270 error (_("couldn't data write to file"));
16271 }
16272
16273 /* Unlink a file if the argument is not NULL. */
16274
16275 static void
16276 unlink_if_set (void *p)
16277 {
16278 char **filename = p;
16279 if (*filename)
16280 unlink (*filename);
16281 }
16282
16283 /* A helper struct used when iterating over debug_types. */
16284 struct signatured_type_index_data
16285 {
16286 struct objfile *objfile;
16287 struct mapped_symtab *symtab;
16288 struct obstack *types_list;
16289 htab_t psyms_seen;
16290 int cu_index;
16291 };
16292
16293 /* A helper function that writes a single signatured_type to an
16294 obstack. */
16295
16296 static int
16297 write_one_signatured_type (void **slot, void *d)
16298 {
16299 struct signatured_type_index_data *info = d;
16300 struct signatured_type *entry = (struct signatured_type *) *slot;
16301 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16302 struct partial_symtab *psymtab = per_cu->v.psymtab;
16303 gdb_byte val[8];
16304
16305 write_psymbols (info->symtab,
16306 info->psyms_seen,
16307 info->objfile->global_psymbols.list
16308 + psymtab->globals_offset,
16309 psymtab->n_global_syms, info->cu_index,
16310 0);
16311 write_psymbols (info->symtab,
16312 info->psyms_seen,
16313 info->objfile->static_psymbols.list
16314 + psymtab->statics_offset,
16315 psymtab->n_static_syms, info->cu_index,
16316 1);
16317
16318 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16319 obstack_grow (info->types_list, val, 8);
16320 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16321 obstack_grow (info->types_list, val, 8);
16322 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16323 obstack_grow (info->types_list, val, 8);
16324
16325 ++info->cu_index;
16326
16327 return 1;
16328 }
16329
16330 /* A cleanup function for an htab_t. */
16331
16332 static void
16333 cleanup_htab (void *arg)
16334 {
16335 htab_delete (arg);
16336 }
16337
16338 /* Create an index file for OBJFILE in the directory DIR. */
16339
16340 static void
16341 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16342 {
16343 struct cleanup *cleanup;
16344 char *filename, *cleanup_filename;
16345 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16346 struct obstack cu_list, types_cu_list;
16347 int i;
16348 FILE *out_file;
16349 struct mapped_symtab *symtab;
16350 offset_type val, size_of_contents, total_len;
16351 struct stat st;
16352 char buf[8];
16353 htab_t psyms_seen;
16354 htab_t cu_index_htab;
16355 struct psymtab_cu_index_map *psymtab_cu_index_map;
16356
16357 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16358 return;
16359
16360 if (dwarf2_per_objfile->using_index)
16361 error (_("Cannot use an index to create the index"));
16362
16363 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16364 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16365
16366 if (stat (objfile->name, &st) < 0)
16367 perror_with_name (objfile->name);
16368
16369 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16370 INDEX_SUFFIX, (char *) NULL);
16371 cleanup = make_cleanup (xfree, filename);
16372
16373 out_file = fopen (filename, "wb");
16374 if (!out_file)
16375 error (_("Can't open `%s' for writing"), filename);
16376
16377 cleanup_filename = filename;
16378 make_cleanup (unlink_if_set, &cleanup_filename);
16379
16380 symtab = create_mapped_symtab ();
16381 make_cleanup (cleanup_mapped_symtab, symtab);
16382
16383 obstack_init (&addr_obstack);
16384 make_cleanup_obstack_free (&addr_obstack);
16385
16386 obstack_init (&cu_list);
16387 make_cleanup_obstack_free (&cu_list);
16388
16389 obstack_init (&types_cu_list);
16390 make_cleanup_obstack_free (&types_cu_list);
16391
16392 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16393 NULL, xcalloc, xfree);
16394 make_cleanup (cleanup_htab, psyms_seen);
16395
16396 /* While we're scanning CU's create a table that maps a psymtab pointer
16397 (which is what addrmap records) to its index (which is what is recorded
16398 in the index file). This will later be needed to write the address
16399 table. */
16400 cu_index_htab = htab_create_alloc (100,
16401 hash_psymtab_cu_index,
16402 eq_psymtab_cu_index,
16403 NULL, xcalloc, xfree);
16404 make_cleanup (cleanup_htab, cu_index_htab);
16405 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16406 xmalloc (sizeof (struct psymtab_cu_index_map)
16407 * dwarf2_per_objfile->n_comp_units);
16408 make_cleanup (xfree, psymtab_cu_index_map);
16409
16410 /* The CU list is already sorted, so we don't need to do additional
16411 work here. Also, the debug_types entries do not appear in
16412 all_comp_units, but only in their own hash table. */
16413 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16414 {
16415 struct dwarf2_per_cu_data *per_cu
16416 = dwarf2_per_objfile->all_comp_units[i];
16417 struct partial_symtab *psymtab = per_cu->v.psymtab;
16418 gdb_byte val[8];
16419 struct psymtab_cu_index_map *map;
16420 void **slot;
16421
16422 write_psymbols (symtab,
16423 psyms_seen,
16424 objfile->global_psymbols.list + psymtab->globals_offset,
16425 psymtab->n_global_syms, i,
16426 0);
16427 write_psymbols (symtab,
16428 psyms_seen,
16429 objfile->static_psymbols.list + psymtab->statics_offset,
16430 psymtab->n_static_syms, i,
16431 1);
16432
16433 map = &psymtab_cu_index_map[i];
16434 map->psymtab = psymtab;
16435 map->cu_index = i;
16436 slot = htab_find_slot (cu_index_htab, map, INSERT);
16437 gdb_assert (slot != NULL);
16438 gdb_assert (*slot == NULL);
16439 *slot = map;
16440
16441 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16442 obstack_grow (&cu_list, val, 8);
16443 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16444 obstack_grow (&cu_list, val, 8);
16445 }
16446
16447 /* Dump the address map. */
16448 write_address_map (objfile, &addr_obstack, cu_index_htab);
16449
16450 /* Write out the .debug_type entries, if any. */
16451 if (dwarf2_per_objfile->signatured_types)
16452 {
16453 struct signatured_type_index_data sig_data;
16454
16455 sig_data.objfile = objfile;
16456 sig_data.symtab = symtab;
16457 sig_data.types_list = &types_cu_list;
16458 sig_data.psyms_seen = psyms_seen;
16459 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16460 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16461 write_one_signatured_type, &sig_data);
16462 }
16463
16464 obstack_init (&constant_pool);
16465 make_cleanup_obstack_free (&constant_pool);
16466 obstack_init (&symtab_obstack);
16467 make_cleanup_obstack_free (&symtab_obstack);
16468 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16469
16470 obstack_init (&contents);
16471 make_cleanup_obstack_free (&contents);
16472 size_of_contents = 6 * sizeof (offset_type);
16473 total_len = size_of_contents;
16474
16475 /* The version number. */
16476 val = MAYBE_SWAP (5);
16477 obstack_grow (&contents, &val, sizeof (val));
16478
16479 /* The offset of the CU list from the start of the file. */
16480 val = MAYBE_SWAP (total_len);
16481 obstack_grow (&contents, &val, sizeof (val));
16482 total_len += obstack_object_size (&cu_list);
16483
16484 /* The offset of the types CU list from the start of the file. */
16485 val = MAYBE_SWAP (total_len);
16486 obstack_grow (&contents, &val, sizeof (val));
16487 total_len += obstack_object_size (&types_cu_list);
16488
16489 /* The offset of the address table from the start of the file. */
16490 val = MAYBE_SWAP (total_len);
16491 obstack_grow (&contents, &val, sizeof (val));
16492 total_len += obstack_object_size (&addr_obstack);
16493
16494 /* The offset of the symbol table from the start of the file. */
16495 val = MAYBE_SWAP (total_len);
16496 obstack_grow (&contents, &val, sizeof (val));
16497 total_len += obstack_object_size (&symtab_obstack);
16498
16499 /* The offset of the constant pool from the start of the file. */
16500 val = MAYBE_SWAP (total_len);
16501 obstack_grow (&contents, &val, sizeof (val));
16502 total_len += obstack_object_size (&constant_pool);
16503
16504 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16505
16506 write_obstack (out_file, &contents);
16507 write_obstack (out_file, &cu_list);
16508 write_obstack (out_file, &types_cu_list);
16509 write_obstack (out_file, &addr_obstack);
16510 write_obstack (out_file, &symtab_obstack);
16511 write_obstack (out_file, &constant_pool);
16512
16513 fclose (out_file);
16514
16515 /* We want to keep the file, so we set cleanup_filename to NULL
16516 here. See unlink_if_set. */
16517 cleanup_filename = NULL;
16518
16519 do_cleanups (cleanup);
16520 }
16521
16522 /* Implementation of the `save gdb-index' command.
16523
16524 Note that the file format used by this command is documented in the
16525 GDB manual. Any changes here must be documented there. */
16526
16527 static void
16528 save_gdb_index_command (char *arg, int from_tty)
16529 {
16530 struct objfile *objfile;
16531
16532 if (!arg || !*arg)
16533 error (_("usage: save gdb-index DIRECTORY"));
16534
16535 ALL_OBJFILES (objfile)
16536 {
16537 struct stat st;
16538
16539 /* If the objfile does not correspond to an actual file, skip it. */
16540 if (stat (objfile->name, &st) < 0)
16541 continue;
16542
16543 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16544 if (dwarf2_per_objfile)
16545 {
16546 volatile struct gdb_exception except;
16547
16548 TRY_CATCH (except, RETURN_MASK_ERROR)
16549 {
16550 write_psymtabs_to_index (objfile, arg);
16551 }
16552 if (except.reason < 0)
16553 exception_fprintf (gdb_stderr, except,
16554 _("Error while writing index for `%s': "),
16555 objfile->name);
16556 }
16557 }
16558 }
16559
16560 \f
16561
16562 int dwarf2_always_disassemble;
16563
16564 static void
16565 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16566 struct cmd_list_element *c, const char *value)
16567 {
16568 fprintf_filtered (file,
16569 _("Whether to always disassemble "
16570 "DWARF expressions is %s.\n"),
16571 value);
16572 }
16573
16574 static void
16575 show_check_physname (struct ui_file *file, int from_tty,
16576 struct cmd_list_element *c, const char *value)
16577 {
16578 fprintf_filtered (file,
16579 _("Whether to check \"physname\" is %s.\n"),
16580 value);
16581 }
16582
16583 void _initialize_dwarf2_read (void);
16584
16585 void
16586 _initialize_dwarf2_read (void)
16587 {
16588 struct cmd_list_element *c;
16589
16590 dwarf2_objfile_data_key
16591 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16592
16593 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16594 Set DWARF 2 specific variables.\n\
16595 Configure DWARF 2 variables such as the cache size"),
16596 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16597 0/*allow-unknown*/, &maintenance_set_cmdlist);
16598
16599 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16600 Show DWARF 2 specific variables\n\
16601 Show DWARF 2 variables such as the cache size"),
16602 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16603 0/*allow-unknown*/, &maintenance_show_cmdlist);
16604
16605 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16606 &dwarf2_max_cache_age, _("\
16607 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16608 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16609 A higher limit means that cached compilation units will be stored\n\
16610 in memory longer, and more total memory will be used. Zero disables\n\
16611 caching, which can slow down startup."),
16612 NULL,
16613 show_dwarf2_max_cache_age,
16614 &set_dwarf2_cmdlist,
16615 &show_dwarf2_cmdlist);
16616
16617 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16618 &dwarf2_always_disassemble, _("\
16619 Set whether `info address' always disassembles DWARF expressions."), _("\
16620 Show whether `info address' always disassembles DWARF expressions."), _("\
16621 When enabled, DWARF expressions are always printed in an assembly-like\n\
16622 syntax. When disabled, expressions will be printed in a more\n\
16623 conversational style, when possible."),
16624 NULL,
16625 show_dwarf2_always_disassemble,
16626 &set_dwarf2_cmdlist,
16627 &show_dwarf2_cmdlist);
16628
16629 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16630 Set debugging of the dwarf2 DIE reader."), _("\
16631 Show debugging of the dwarf2 DIE reader."), _("\
16632 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16633 The value is the maximum depth to print."),
16634 NULL,
16635 NULL,
16636 &setdebuglist, &showdebuglist);
16637
16638 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16639 Set cross-checking of \"physname\" code against demangler."), _("\
16640 Show cross-checking of \"physname\" code against demangler."), _("\
16641 When enabled, GDB's internal \"physname\" code is checked against\n\
16642 the demangler."),
16643 NULL, show_check_physname,
16644 &setdebuglist, &showdebuglist);
16645
16646 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16647 _("\
16648 Save a gdb-index file.\n\
16649 Usage: save gdb-index DIRECTORY"),
16650 &save_cmdlist);
16651 set_cmd_completer (c, filename_completer);
16652 }
This page took 0.545 seconds and 4 git commands to generate.