gdb/
[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 (parent->tag == DW_TAG_namespace
3882 || parent->tag == DW_TAG_module
3883 || parent->tag == DW_TAG_structure_type
3884 || parent->tag == DW_TAG_class_type
3885 || parent->tag == DW_TAG_interface_type
3886 || parent->tag == DW_TAG_union_type
3887 || parent->tag == DW_TAG_enumeration_type)
3888 {
3889 if (grandparent_scope == NULL)
3890 parent->scope = parent->name;
3891 else
3892 parent->scope = typename_concat (&cu->comp_unit_obstack,
3893 grandparent_scope,
3894 parent->name, 0, cu);
3895 }
3896 else if (parent->tag == DW_TAG_enumerator)
3897 /* Enumerators should not get the name of the enumeration as a prefix. */
3898 parent->scope = grandparent_scope;
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 /* Check for GCC >= 4.x. Return minor version (x) of 4.x in such case. If it
4727 is not GCC or it is GCC older than 4.x return -1. If it is GCC 5.x or
4728 higher return INT_MAX. */
4729
4730 static int
4731 producer_is_gcc_ge_4 (struct dwarf2_cu *cu)
4732 {
4733 const char *cs;
4734 int major, minor;
4735
4736 if (cu->producer == NULL)
4737 {
4738 /* For unknown compilers expect their behavior is not compliant. For GCC
4739 this case can also happen for -gdwarf-4 type units supported since
4740 gcc-4.5. */
4741
4742 return -1;
4743 }
4744
4745 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4746
4747 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4748 {
4749 /* For non-GCC compilers expect their behavior is not compliant. */
4750
4751 return -1;
4752 }
4753 cs = &cu->producer[strlen ("GNU ")];
4754 while (*cs && !isdigit (*cs))
4755 cs++;
4756 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4757 {
4758 /* Not recognized as GCC. */
4759
4760 return -1;
4761 }
4762
4763 if (major < 4)
4764 return -1;
4765 if (major > 4)
4766 return INT_MAX;
4767 return minor;
4768 }
4769
4770 /* Generate full symbol information for PST and CU, whose DIEs have
4771 already been loaded into memory. */
4772
4773 static void
4774 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4775 {
4776 struct dwarf2_cu *cu = per_cu->cu;
4777 struct objfile *objfile = per_cu->objfile;
4778 CORE_ADDR lowpc, highpc;
4779 struct symtab *symtab;
4780 struct cleanup *back_to, *delayed_list_cleanup;
4781 CORE_ADDR baseaddr;
4782
4783 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4784
4785 buildsym_init ();
4786 back_to = make_cleanup (really_free_pendings, NULL);
4787 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4788
4789 cu->list_in_scope = &file_symbols;
4790
4791 /* Do line number decoding in read_file_scope () */
4792 process_die (cu->dies, cu);
4793
4794 /* Now that we have processed all the DIEs in the CU, all the types
4795 should be complete, and it should now be safe to compute all of the
4796 physnames. */
4797 compute_delayed_physnames (cu);
4798 do_cleanups (delayed_list_cleanup);
4799
4800 /* Some compilers don't define a DW_AT_high_pc attribute for the
4801 compilation unit. If the DW_AT_high_pc is missing, synthesize
4802 it, by scanning the DIE's below the compilation unit. */
4803 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4804
4805 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4806
4807 if (symtab != NULL)
4808 {
4809 int gcc_4_minor = producer_is_gcc_ge_4 (cu);
4810
4811 /* Set symtab language to language from DW_AT_language. If the
4812 compilation is from a C file generated by language preprocessors, do
4813 not set the language if it was already deduced by start_subfile. */
4814 if (!(cu->language == language_c && symtab->language != language_c))
4815 symtab->language = cu->language;
4816
4817 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4818 produce DW_AT_location with location lists but it can be possibly
4819 invalid without -fvar-tracking.
4820
4821 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4822 needed, it would be wrong due to missing DW_AT_producer there.
4823
4824 Still one can confuse GDB by using non-standard GCC compilation
4825 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4826 */
4827 if (cu->has_loclist && gcc_4_minor >= 0)
4828 symtab->locations_valid = 1;
4829
4830 if (gcc_4_minor >= 5)
4831 symtab->epilogue_unwind_valid = 1;
4832
4833 if (gcc_4_minor >= 6)
4834 symtab->amd64_prologue_line_bug = 1;
4835 }
4836
4837 if (dwarf2_per_objfile->using_index)
4838 per_cu->v.quick->symtab = symtab;
4839 else
4840 {
4841 struct partial_symtab *pst = per_cu->v.psymtab;
4842 pst->symtab = symtab;
4843 pst->readin = 1;
4844 }
4845
4846 do_cleanups (back_to);
4847 }
4848
4849 /* Process a die and its children. */
4850
4851 static void
4852 process_die (struct die_info *die, struct dwarf2_cu *cu)
4853 {
4854 switch (die->tag)
4855 {
4856 case DW_TAG_padding:
4857 break;
4858 case DW_TAG_compile_unit:
4859 read_file_scope (die, cu);
4860 break;
4861 case DW_TAG_type_unit:
4862 read_type_unit_scope (die, cu);
4863 break;
4864 case DW_TAG_subprogram:
4865 case DW_TAG_inlined_subroutine:
4866 read_func_scope (die, cu);
4867 break;
4868 case DW_TAG_lexical_block:
4869 case DW_TAG_try_block:
4870 case DW_TAG_catch_block:
4871 read_lexical_block_scope (die, cu);
4872 break;
4873 case DW_TAG_class_type:
4874 case DW_TAG_interface_type:
4875 case DW_TAG_structure_type:
4876 case DW_TAG_union_type:
4877 process_structure_scope (die, cu);
4878 break;
4879 case DW_TAG_enumeration_type:
4880 process_enumeration_scope (die, cu);
4881 break;
4882
4883 /* These dies have a type, but processing them does not create
4884 a symbol or recurse to process the children. Therefore we can
4885 read them on-demand through read_type_die. */
4886 case DW_TAG_subroutine_type:
4887 case DW_TAG_set_type:
4888 case DW_TAG_array_type:
4889 case DW_TAG_pointer_type:
4890 case DW_TAG_ptr_to_member_type:
4891 case DW_TAG_reference_type:
4892 case DW_TAG_string_type:
4893 break;
4894
4895 case DW_TAG_base_type:
4896 case DW_TAG_subrange_type:
4897 case DW_TAG_typedef:
4898 /* Add a typedef symbol for the type definition, if it has a
4899 DW_AT_name. */
4900 new_symbol (die, read_type_die (die, cu), cu);
4901 break;
4902 case DW_TAG_common_block:
4903 read_common_block (die, cu);
4904 break;
4905 case DW_TAG_common_inclusion:
4906 break;
4907 case DW_TAG_namespace:
4908 processing_has_namespace_info = 1;
4909 read_namespace (die, cu);
4910 break;
4911 case DW_TAG_module:
4912 processing_has_namespace_info = 1;
4913 read_module (die, cu);
4914 break;
4915 case DW_TAG_imported_declaration:
4916 case DW_TAG_imported_module:
4917 processing_has_namespace_info = 1;
4918 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4919 || cu->language != language_fortran))
4920 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4921 dwarf_tag_name (die->tag));
4922 read_import_statement (die, cu);
4923 break;
4924 default:
4925 new_symbol (die, NULL, cu);
4926 break;
4927 }
4928 }
4929
4930 /* A helper function for dwarf2_compute_name which determines whether DIE
4931 needs to have the name of the scope prepended to the name listed in the
4932 die. */
4933
4934 static int
4935 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4936 {
4937 struct attribute *attr;
4938
4939 switch (die->tag)
4940 {
4941 case DW_TAG_namespace:
4942 case DW_TAG_typedef:
4943 case DW_TAG_class_type:
4944 case DW_TAG_interface_type:
4945 case DW_TAG_structure_type:
4946 case DW_TAG_union_type:
4947 case DW_TAG_enumeration_type:
4948 case DW_TAG_enumerator:
4949 case DW_TAG_subprogram:
4950 case DW_TAG_member:
4951 return 1;
4952
4953 case DW_TAG_variable:
4954 case DW_TAG_constant:
4955 /* We only need to prefix "globally" visible variables. These include
4956 any variable marked with DW_AT_external or any variable that
4957 lives in a namespace. [Variables in anonymous namespaces
4958 require prefixing, but they are not DW_AT_external.] */
4959
4960 if (dwarf2_attr (die, DW_AT_specification, cu))
4961 {
4962 struct dwarf2_cu *spec_cu = cu;
4963
4964 return die_needs_namespace (die_specification (die, &spec_cu),
4965 spec_cu);
4966 }
4967
4968 attr = dwarf2_attr (die, DW_AT_external, cu);
4969 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4970 && die->parent->tag != DW_TAG_module)
4971 return 0;
4972 /* A variable in a lexical block of some kind does not need a
4973 namespace, even though in C++ such variables may be external
4974 and have a mangled name. */
4975 if (die->parent->tag == DW_TAG_lexical_block
4976 || die->parent->tag == DW_TAG_try_block
4977 || die->parent->tag == DW_TAG_catch_block
4978 || die->parent->tag == DW_TAG_subprogram)
4979 return 0;
4980 return 1;
4981
4982 default:
4983 return 0;
4984 }
4985 }
4986
4987 /* Retrieve the last character from a mem_file. */
4988
4989 static void
4990 do_ui_file_peek_last (void *object, const char *buffer, long length)
4991 {
4992 char *last_char_p = (char *) object;
4993
4994 if (length > 0)
4995 *last_char_p = buffer[length - 1];
4996 }
4997
4998 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4999 compute the physname for the object, which include a method's
5000 formal parameters (C++/Java) and return type (Java).
5001
5002 For Ada, return the DIE's linkage name rather than the fully qualified
5003 name. PHYSNAME is ignored..
5004
5005 The result is allocated on the objfile_obstack and canonicalized. */
5006
5007 static const char *
5008 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5009 int physname)
5010 {
5011 if (name == NULL)
5012 name = dwarf2_name (die, cu);
5013
5014 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5015 compute it by typename_concat inside GDB. */
5016 if (cu->language == language_ada
5017 || (cu->language == language_fortran && physname))
5018 {
5019 /* For Ada unit, we prefer the linkage name over the name, as
5020 the former contains the exported name, which the user expects
5021 to be able to reference. Ideally, we want the user to be able
5022 to reference this entity using either natural or linkage name,
5023 but we haven't started looking at this enhancement yet. */
5024 struct attribute *attr;
5025
5026 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5027 if (attr == NULL)
5028 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5029 if (attr && DW_STRING (attr))
5030 return DW_STRING (attr);
5031 }
5032
5033 /* These are the only languages we know how to qualify names in. */
5034 if (name != NULL
5035 && (cu->language == language_cplus || cu->language == language_java
5036 || cu->language == language_fortran))
5037 {
5038 if (die_needs_namespace (die, cu))
5039 {
5040 long length;
5041 char *prefix;
5042 struct ui_file *buf;
5043
5044 prefix = determine_prefix (die, cu);
5045 buf = mem_fileopen ();
5046 if (*prefix != '\0')
5047 {
5048 char *prefixed_name = typename_concat (NULL, prefix, name,
5049 physname, cu);
5050
5051 fputs_unfiltered (prefixed_name, buf);
5052 xfree (prefixed_name);
5053 }
5054 else
5055 fputs_unfiltered (name, buf);
5056
5057 /* Template parameters may be specified in the DIE's DW_AT_name, or
5058 as children with DW_TAG_template_type_param or
5059 DW_TAG_value_type_param. If the latter, add them to the name
5060 here. If the name already has template parameters, then
5061 skip this step; some versions of GCC emit both, and
5062 it is more efficient to use the pre-computed name.
5063
5064 Something to keep in mind about this process: it is very
5065 unlikely, or in some cases downright impossible, to produce
5066 something that will match the mangled name of a function.
5067 If the definition of the function has the same debug info,
5068 we should be able to match up with it anyway. But fallbacks
5069 using the minimal symbol, for instance to find a method
5070 implemented in a stripped copy of libstdc++, will not work.
5071 If we do not have debug info for the definition, we will have to
5072 match them up some other way.
5073
5074 When we do name matching there is a related problem with function
5075 templates; two instantiated function templates are allowed to
5076 differ only by their return types, which we do not add here. */
5077
5078 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5079 {
5080 struct attribute *attr;
5081 struct die_info *child;
5082 int first = 1;
5083
5084 die->building_fullname = 1;
5085
5086 for (child = die->child; child != NULL; child = child->sibling)
5087 {
5088 struct type *type;
5089 long value;
5090 gdb_byte *bytes;
5091 struct dwarf2_locexpr_baton *baton;
5092 struct value *v;
5093
5094 if (child->tag != DW_TAG_template_type_param
5095 && child->tag != DW_TAG_template_value_param)
5096 continue;
5097
5098 if (first)
5099 {
5100 fputs_unfiltered ("<", buf);
5101 first = 0;
5102 }
5103 else
5104 fputs_unfiltered (", ", buf);
5105
5106 attr = dwarf2_attr (child, DW_AT_type, cu);
5107 if (attr == NULL)
5108 {
5109 complaint (&symfile_complaints,
5110 _("template parameter missing DW_AT_type"));
5111 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5112 continue;
5113 }
5114 type = die_type (child, cu);
5115
5116 if (child->tag == DW_TAG_template_type_param)
5117 {
5118 c_print_type (type, "", buf, -1, 0);
5119 continue;
5120 }
5121
5122 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5123 if (attr == NULL)
5124 {
5125 complaint (&symfile_complaints,
5126 _("template parameter missing "
5127 "DW_AT_const_value"));
5128 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5129 continue;
5130 }
5131
5132 dwarf2_const_value_attr (attr, type, name,
5133 &cu->comp_unit_obstack, cu,
5134 &value, &bytes, &baton);
5135
5136 if (TYPE_NOSIGN (type))
5137 /* GDB prints characters as NUMBER 'CHAR'. If that's
5138 changed, this can use value_print instead. */
5139 c_printchar (value, type, buf);
5140 else
5141 {
5142 struct value_print_options opts;
5143
5144 if (baton != NULL)
5145 v = dwarf2_evaluate_loc_desc (type, NULL,
5146 baton->data,
5147 baton->size,
5148 baton->per_cu);
5149 else if (bytes != NULL)
5150 {
5151 v = allocate_value (type);
5152 memcpy (value_contents_writeable (v), bytes,
5153 TYPE_LENGTH (type));
5154 }
5155 else
5156 v = value_from_longest (type, value);
5157
5158 /* Specify decimal so that we do not depend on
5159 the radix. */
5160 get_formatted_print_options (&opts, 'd');
5161 opts.raw = 1;
5162 value_print (v, buf, &opts);
5163 release_value (v);
5164 value_free (v);
5165 }
5166 }
5167
5168 die->building_fullname = 0;
5169
5170 if (!first)
5171 {
5172 /* Close the argument list, with a space if necessary
5173 (nested templates). */
5174 char last_char = '\0';
5175 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5176 if (last_char == '>')
5177 fputs_unfiltered (" >", buf);
5178 else
5179 fputs_unfiltered (">", buf);
5180 }
5181 }
5182
5183 /* For Java and C++ methods, append formal parameter type
5184 information, if PHYSNAME. */
5185
5186 if (physname && die->tag == DW_TAG_subprogram
5187 && (cu->language == language_cplus
5188 || cu->language == language_java))
5189 {
5190 struct type *type = read_type_die (die, cu);
5191
5192 c_type_print_args (type, buf, 1, cu->language);
5193
5194 if (cu->language == language_java)
5195 {
5196 /* For java, we must append the return type to method
5197 names. */
5198 if (die->tag == DW_TAG_subprogram)
5199 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5200 0, 0);
5201 }
5202 else if (cu->language == language_cplus)
5203 {
5204 /* Assume that an artificial first parameter is
5205 "this", but do not crash if it is not. RealView
5206 marks unnamed (and thus unused) parameters as
5207 artificial; there is no way to differentiate
5208 the two cases. */
5209 if (TYPE_NFIELDS (type) > 0
5210 && TYPE_FIELD_ARTIFICIAL (type, 0)
5211 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5212 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5213 0))))
5214 fputs_unfiltered (" const", buf);
5215 }
5216 }
5217
5218 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5219 &length);
5220 ui_file_delete (buf);
5221
5222 if (cu->language == language_cplus)
5223 {
5224 char *cname
5225 = dwarf2_canonicalize_name (name, cu,
5226 &cu->objfile->objfile_obstack);
5227
5228 if (cname != NULL)
5229 name = cname;
5230 }
5231 }
5232 }
5233
5234 return name;
5235 }
5236
5237 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5238 If scope qualifiers are appropriate they will be added. The result
5239 will be allocated on the objfile_obstack, or NULL if the DIE does
5240 not have a name. NAME may either be from a previous call to
5241 dwarf2_name or NULL.
5242
5243 The output string will be canonicalized (if C++/Java). */
5244
5245 static const char *
5246 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5247 {
5248 return dwarf2_compute_name (name, die, cu, 0);
5249 }
5250
5251 /* Construct a physname for the given DIE in CU. NAME may either be
5252 from a previous call to dwarf2_name or NULL. The result will be
5253 allocated on the objfile_objstack or NULL if the DIE does not have a
5254 name.
5255
5256 The output string will be canonicalized (if C++/Java). */
5257
5258 static const char *
5259 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5260 {
5261 struct attribute *attr;
5262 const char *retval, *mangled = NULL, *canon = NULL;
5263 struct cleanup *back_to;
5264 int need_copy = 1;
5265
5266 /* In this case dwarf2_compute_name is just a shortcut not building anything
5267 on its own. */
5268 if (!die_needs_namespace (die, cu))
5269 return dwarf2_compute_name (name, die, cu, 1);
5270
5271 back_to = make_cleanup (null_cleanup, NULL);
5272
5273 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5274 if (!attr)
5275 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5276
5277 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5278 has computed. */
5279 if (attr && DW_STRING (attr))
5280 {
5281 char *demangled;
5282
5283 mangled = DW_STRING (attr);
5284
5285 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5286 type. It is easier for GDB users to search for such functions as
5287 `name(params)' than `long name(params)'. In such case the minimal
5288 symbol names do not match the full symbol names but for template
5289 functions there is never a need to look up their definition from their
5290 declaration so the only disadvantage remains the minimal symbol
5291 variant `long name(params)' does not have the proper inferior type.
5292 */
5293
5294 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5295 | (cu->language == language_java
5296 ? DMGL_JAVA | DMGL_RET_POSTFIX
5297 : DMGL_RET_DROP)));
5298 if (demangled)
5299 {
5300 make_cleanup (xfree, demangled);
5301 canon = demangled;
5302 }
5303 else
5304 {
5305 canon = mangled;
5306 need_copy = 0;
5307 }
5308 }
5309
5310 if (canon == NULL || check_physname)
5311 {
5312 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5313
5314 if (canon != NULL && strcmp (physname, canon) != 0)
5315 {
5316 /* It may not mean a bug in GDB. The compiler could also
5317 compute DW_AT_linkage_name incorrectly. But in such case
5318 GDB would need to be bug-to-bug compatible. */
5319
5320 complaint (&symfile_complaints,
5321 _("Computed physname <%s> does not match demangled <%s> "
5322 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5323 physname, canon, mangled, die->offset, cu->objfile->name);
5324
5325 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5326 is available here - over computed PHYSNAME. It is safer
5327 against both buggy GDB and buggy compilers. */
5328
5329 retval = canon;
5330 }
5331 else
5332 {
5333 retval = physname;
5334 need_copy = 0;
5335 }
5336 }
5337 else
5338 retval = canon;
5339
5340 if (need_copy)
5341 retval = obsavestring (retval, strlen (retval),
5342 &cu->objfile->objfile_obstack);
5343
5344 do_cleanups (back_to);
5345 return retval;
5346 }
5347
5348 /* Read the import statement specified by the given die and record it. */
5349
5350 static void
5351 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5352 {
5353 struct attribute *import_attr;
5354 struct die_info *imported_die, *child_die;
5355 struct dwarf2_cu *imported_cu;
5356 const char *imported_name;
5357 const char *imported_name_prefix;
5358 const char *canonical_name;
5359 const char *import_alias;
5360 const char *imported_declaration = NULL;
5361 const char *import_prefix;
5362 VEC (const_char_ptr) *excludes = NULL;
5363 struct cleanup *cleanups;
5364
5365 char *temp;
5366
5367 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5368 if (import_attr == NULL)
5369 {
5370 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5371 dwarf_tag_name (die->tag));
5372 return;
5373 }
5374
5375 imported_cu = cu;
5376 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5377 imported_name = dwarf2_name (imported_die, imported_cu);
5378 if (imported_name == NULL)
5379 {
5380 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5381
5382 The import in the following code:
5383 namespace A
5384 {
5385 typedef int B;
5386 }
5387
5388 int main ()
5389 {
5390 using A::B;
5391 B b;
5392 return b;
5393 }
5394
5395 ...
5396 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5397 <52> DW_AT_decl_file : 1
5398 <53> DW_AT_decl_line : 6
5399 <54> DW_AT_import : <0x75>
5400 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5401 <59> DW_AT_name : B
5402 <5b> DW_AT_decl_file : 1
5403 <5c> DW_AT_decl_line : 2
5404 <5d> DW_AT_type : <0x6e>
5405 ...
5406 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5407 <76> DW_AT_byte_size : 4
5408 <77> DW_AT_encoding : 5 (signed)
5409
5410 imports the wrong die ( 0x75 instead of 0x58 ).
5411 This case will be ignored until the gcc bug is fixed. */
5412 return;
5413 }
5414
5415 /* Figure out the local name after import. */
5416 import_alias = dwarf2_name (die, cu);
5417
5418 /* Figure out where the statement is being imported to. */
5419 import_prefix = determine_prefix (die, cu);
5420
5421 /* Figure out what the scope of the imported die is and prepend it
5422 to the name of the imported die. */
5423 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5424
5425 if (imported_die->tag != DW_TAG_namespace
5426 && imported_die->tag != DW_TAG_module)
5427 {
5428 imported_declaration = imported_name;
5429 canonical_name = imported_name_prefix;
5430 }
5431 else if (strlen (imported_name_prefix) > 0)
5432 {
5433 temp = alloca (strlen (imported_name_prefix)
5434 + 2 + strlen (imported_name) + 1);
5435 strcpy (temp, imported_name_prefix);
5436 strcat (temp, "::");
5437 strcat (temp, imported_name);
5438 canonical_name = temp;
5439 }
5440 else
5441 canonical_name = imported_name;
5442
5443 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5444
5445 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5446 for (child_die = die->child; child_die && child_die->tag;
5447 child_die = sibling_die (child_die))
5448 {
5449 /* DWARF-4: A Fortran use statement with a “rename list” may be
5450 represented by an imported module entry with an import attribute
5451 referring to the module and owned entries corresponding to those
5452 entities that are renamed as part of being imported. */
5453
5454 if (child_die->tag != DW_TAG_imported_declaration)
5455 {
5456 complaint (&symfile_complaints,
5457 _("child DW_TAG_imported_declaration expected "
5458 "- DIE at 0x%x [in module %s]"),
5459 child_die->offset, cu->objfile->name);
5460 continue;
5461 }
5462
5463 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5464 if (import_attr == NULL)
5465 {
5466 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5467 dwarf_tag_name (child_die->tag));
5468 continue;
5469 }
5470
5471 imported_cu = cu;
5472 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5473 &imported_cu);
5474 imported_name = dwarf2_name (imported_die, imported_cu);
5475 if (imported_name == NULL)
5476 {
5477 complaint (&symfile_complaints,
5478 _("child DW_TAG_imported_declaration has unknown "
5479 "imported name - DIE at 0x%x [in module %s]"),
5480 child_die->offset, cu->objfile->name);
5481 continue;
5482 }
5483
5484 VEC_safe_push (const_char_ptr, excludes, imported_name);
5485
5486 process_die (child_die, cu);
5487 }
5488
5489 cp_add_using_directive (import_prefix,
5490 canonical_name,
5491 import_alias,
5492 imported_declaration,
5493 excludes,
5494 &cu->objfile->objfile_obstack);
5495
5496 do_cleanups (cleanups);
5497 }
5498
5499 static void
5500 initialize_cu_func_list (struct dwarf2_cu *cu)
5501 {
5502 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5503 }
5504
5505 /* Cleanup function for read_file_scope. */
5506
5507 static void
5508 free_cu_line_header (void *arg)
5509 {
5510 struct dwarf2_cu *cu = arg;
5511
5512 free_line_header (cu->line_header);
5513 cu->line_header = NULL;
5514 }
5515
5516 static void
5517 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5518 char **name, char **comp_dir)
5519 {
5520 struct attribute *attr;
5521
5522 *name = NULL;
5523 *comp_dir = NULL;
5524
5525 /* Find the filename. Do not use dwarf2_name here, since the filename
5526 is not a source language identifier. */
5527 attr = dwarf2_attr (die, DW_AT_name, cu);
5528 if (attr)
5529 {
5530 *name = DW_STRING (attr);
5531 }
5532
5533 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5534 if (attr)
5535 *comp_dir = DW_STRING (attr);
5536 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5537 {
5538 *comp_dir = ldirname (*name);
5539 if (*comp_dir != NULL)
5540 make_cleanup (xfree, *comp_dir);
5541 }
5542 if (*comp_dir != NULL)
5543 {
5544 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5545 directory, get rid of it. */
5546 char *cp = strchr (*comp_dir, ':');
5547
5548 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5549 *comp_dir = cp + 1;
5550 }
5551
5552 if (*name == NULL)
5553 *name = "<unknown>";
5554 }
5555
5556 /* Handle DW_AT_stmt_list for a compilation unit. */
5557
5558 static void
5559 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5560 const char *comp_dir)
5561 {
5562 struct attribute *attr;
5563 struct objfile *objfile = cu->objfile;
5564 bfd *abfd = objfile->obfd;
5565
5566 /* Decode line number information if present. We do this before
5567 processing child DIEs, so that the line header table is available
5568 for DW_AT_decl_file. */
5569 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5570 if (attr)
5571 {
5572 unsigned int line_offset = DW_UNSND (attr);
5573 struct line_header *line_header
5574 = dwarf_decode_line_header (line_offset, abfd, cu);
5575
5576 if (line_header)
5577 {
5578 cu->line_header = line_header;
5579 make_cleanup (free_cu_line_header, cu);
5580 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5581 }
5582 }
5583 }
5584
5585 /* Process DW_TAG_compile_unit. */
5586
5587 static void
5588 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5589 {
5590 struct objfile *objfile = cu->objfile;
5591 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5592 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5593 CORE_ADDR highpc = ((CORE_ADDR) 0);
5594 struct attribute *attr;
5595 char *name = NULL;
5596 char *comp_dir = NULL;
5597 struct die_info *child_die;
5598 bfd *abfd = objfile->obfd;
5599 CORE_ADDR baseaddr;
5600
5601 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5602
5603 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5604
5605 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5606 from finish_block. */
5607 if (lowpc == ((CORE_ADDR) -1))
5608 lowpc = highpc;
5609 lowpc += baseaddr;
5610 highpc += baseaddr;
5611
5612 find_file_and_directory (die, cu, &name, &comp_dir);
5613
5614 attr = dwarf2_attr (die, DW_AT_language, cu);
5615 if (attr)
5616 {
5617 set_cu_language (DW_UNSND (attr), cu);
5618 }
5619
5620 attr = dwarf2_attr (die, DW_AT_producer, cu);
5621 if (attr)
5622 cu->producer = DW_STRING (attr);
5623
5624 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5625 standardised yet. As a workaround for the language detection we fall
5626 back to the DW_AT_producer string. */
5627 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5628 cu->language = language_opencl;
5629
5630 /* We assume that we're processing GCC output. */
5631 processing_gcc_compilation = 2;
5632
5633 processing_has_namespace_info = 0;
5634
5635 start_symtab (name, comp_dir, lowpc);
5636 record_debugformat ("DWARF 2");
5637 record_producer (cu->producer);
5638
5639 initialize_cu_func_list (cu);
5640
5641 handle_DW_AT_stmt_list (die, cu, comp_dir);
5642
5643 /* Process all dies in compilation unit. */
5644 if (die->child != NULL)
5645 {
5646 child_die = die->child;
5647 while (child_die && child_die->tag)
5648 {
5649 process_die (child_die, cu);
5650 child_die = sibling_die (child_die);
5651 }
5652 }
5653
5654 /* Decode macro information, if present. Dwarf 2 macro information
5655 refers to information in the line number info statement program
5656 header, so we can only read it if we've read the header
5657 successfully. */
5658 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5659 if (attr && cu->line_header)
5660 {
5661 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5662 complaint (&symfile_complaints,
5663 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5664
5665 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5666 comp_dir, abfd, cu,
5667 &dwarf2_per_objfile->macro, 1);
5668 }
5669 else
5670 {
5671 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5672 if (attr && cu->line_header)
5673 {
5674 unsigned int macro_offset = DW_UNSND (attr);
5675
5676 dwarf_decode_macros (cu->line_header, macro_offset,
5677 comp_dir, abfd, cu,
5678 &dwarf2_per_objfile->macinfo, 0);
5679 }
5680 }
5681 do_cleanups (back_to);
5682 }
5683
5684 /* Process DW_TAG_type_unit.
5685 For TUs we want to skip the first top level sibling if it's not the
5686 actual type being defined by this TU. In this case the first top
5687 level sibling is there to provide context only. */
5688
5689 static void
5690 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5691 {
5692 struct objfile *objfile = cu->objfile;
5693 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5694 CORE_ADDR lowpc;
5695 struct attribute *attr;
5696 char *name = NULL;
5697 char *comp_dir = NULL;
5698 struct die_info *child_die;
5699 bfd *abfd = objfile->obfd;
5700
5701 /* start_symtab needs a low pc, but we don't really have one.
5702 Do what read_file_scope would do in the absence of such info. */
5703 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5704
5705 /* Find the filename. Do not use dwarf2_name here, since the filename
5706 is not a source language identifier. */
5707 attr = dwarf2_attr (die, DW_AT_name, cu);
5708 if (attr)
5709 name = DW_STRING (attr);
5710
5711 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5712 if (attr)
5713 comp_dir = DW_STRING (attr);
5714 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5715 {
5716 comp_dir = ldirname (name);
5717 if (comp_dir != NULL)
5718 make_cleanup (xfree, comp_dir);
5719 }
5720
5721 if (name == NULL)
5722 name = "<unknown>";
5723
5724 attr = dwarf2_attr (die, DW_AT_language, cu);
5725 if (attr)
5726 set_cu_language (DW_UNSND (attr), cu);
5727
5728 /* This isn't technically needed today. It is done for symmetry
5729 with read_file_scope. */
5730 attr = dwarf2_attr (die, DW_AT_producer, cu);
5731 if (attr)
5732 cu->producer = DW_STRING (attr);
5733
5734 /* We assume that we're processing GCC output. */
5735 processing_gcc_compilation = 2;
5736
5737 processing_has_namespace_info = 0;
5738
5739 start_symtab (name, comp_dir, lowpc);
5740 record_debugformat ("DWARF 2");
5741 record_producer (cu->producer);
5742
5743 handle_DW_AT_stmt_list (die, cu, comp_dir);
5744
5745 /* Process the dies in the type unit. */
5746 if (die->child == NULL)
5747 {
5748 dump_die_for_error (die);
5749 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5750 bfd_get_filename (abfd));
5751 }
5752
5753 child_die = die->child;
5754
5755 while (child_die && child_die->tag)
5756 {
5757 process_die (child_die, cu);
5758
5759 child_die = sibling_die (child_die);
5760 }
5761
5762 do_cleanups (back_to);
5763 }
5764
5765 static void
5766 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5767 struct dwarf2_cu *cu)
5768 {
5769 struct function_range *thisfn;
5770
5771 thisfn = (struct function_range *)
5772 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5773 thisfn->name = name;
5774 thisfn->lowpc = lowpc;
5775 thisfn->highpc = highpc;
5776 thisfn->seen_line = 0;
5777 thisfn->next = NULL;
5778
5779 if (cu->last_fn == NULL)
5780 cu->first_fn = thisfn;
5781 else
5782 cu->last_fn->next = thisfn;
5783
5784 cu->last_fn = thisfn;
5785 }
5786
5787 /* qsort helper for inherit_abstract_dies. */
5788
5789 static int
5790 unsigned_int_compar (const void *ap, const void *bp)
5791 {
5792 unsigned int a = *(unsigned int *) ap;
5793 unsigned int b = *(unsigned int *) bp;
5794
5795 return (a > b) - (b > a);
5796 }
5797
5798 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5799 Inherit only the children of the DW_AT_abstract_origin DIE not being
5800 already referenced by DW_AT_abstract_origin from the children of the
5801 current DIE. */
5802
5803 static void
5804 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5805 {
5806 struct die_info *child_die;
5807 unsigned die_children_count;
5808 /* CU offsets which were referenced by children of the current DIE. */
5809 unsigned *offsets;
5810 unsigned *offsets_end, *offsetp;
5811 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5812 struct die_info *origin_die;
5813 /* Iterator of the ORIGIN_DIE children. */
5814 struct die_info *origin_child_die;
5815 struct cleanup *cleanups;
5816 struct attribute *attr;
5817 struct dwarf2_cu *origin_cu;
5818 struct pending **origin_previous_list_in_scope;
5819
5820 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5821 if (!attr)
5822 return;
5823
5824 /* Note that following die references may follow to a die in a
5825 different cu. */
5826
5827 origin_cu = cu;
5828 origin_die = follow_die_ref (die, attr, &origin_cu);
5829
5830 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5831 symbols in. */
5832 origin_previous_list_in_scope = origin_cu->list_in_scope;
5833 origin_cu->list_in_scope = cu->list_in_scope;
5834
5835 if (die->tag != origin_die->tag
5836 && !(die->tag == DW_TAG_inlined_subroutine
5837 && origin_die->tag == DW_TAG_subprogram))
5838 complaint (&symfile_complaints,
5839 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5840 die->offset, origin_die->offset);
5841
5842 child_die = die->child;
5843 die_children_count = 0;
5844 while (child_die && child_die->tag)
5845 {
5846 child_die = sibling_die (child_die);
5847 die_children_count++;
5848 }
5849 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5850 cleanups = make_cleanup (xfree, offsets);
5851
5852 offsets_end = offsets;
5853 child_die = die->child;
5854 while (child_die && child_die->tag)
5855 {
5856 /* For each CHILD_DIE, find the corresponding child of
5857 ORIGIN_DIE. If there is more than one layer of
5858 DW_AT_abstract_origin, follow them all; there shouldn't be,
5859 but GCC versions at least through 4.4 generate this (GCC PR
5860 40573). */
5861 struct die_info *child_origin_die = child_die;
5862 struct dwarf2_cu *child_origin_cu = cu;
5863
5864 while (1)
5865 {
5866 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5867 child_origin_cu);
5868 if (attr == NULL)
5869 break;
5870 child_origin_die = follow_die_ref (child_origin_die, attr,
5871 &child_origin_cu);
5872 }
5873
5874 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5875 counterpart may exist. */
5876 if (child_origin_die != child_die)
5877 {
5878 if (child_die->tag != child_origin_die->tag
5879 && !(child_die->tag == DW_TAG_inlined_subroutine
5880 && child_origin_die->tag == DW_TAG_subprogram))
5881 complaint (&symfile_complaints,
5882 _("Child DIE 0x%x and its abstract origin 0x%x have "
5883 "different tags"), child_die->offset,
5884 child_origin_die->offset);
5885 if (child_origin_die->parent != origin_die)
5886 complaint (&symfile_complaints,
5887 _("Child DIE 0x%x and its abstract origin 0x%x have "
5888 "different parents"), child_die->offset,
5889 child_origin_die->offset);
5890 else
5891 *offsets_end++ = child_origin_die->offset;
5892 }
5893 child_die = sibling_die (child_die);
5894 }
5895 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5896 unsigned_int_compar);
5897 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5898 if (offsetp[-1] == *offsetp)
5899 complaint (&symfile_complaints,
5900 _("Multiple children of DIE 0x%x refer "
5901 "to DIE 0x%x as their abstract origin"),
5902 die->offset, *offsetp);
5903
5904 offsetp = offsets;
5905 origin_child_die = origin_die->child;
5906 while (origin_child_die && origin_child_die->tag)
5907 {
5908 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5909 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5910 offsetp++;
5911 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5912 {
5913 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5914 process_die (origin_child_die, origin_cu);
5915 }
5916 origin_child_die = sibling_die (origin_child_die);
5917 }
5918 origin_cu->list_in_scope = origin_previous_list_in_scope;
5919
5920 do_cleanups (cleanups);
5921 }
5922
5923 static void
5924 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5925 {
5926 struct objfile *objfile = cu->objfile;
5927 struct context_stack *new;
5928 CORE_ADDR lowpc;
5929 CORE_ADDR highpc;
5930 struct die_info *child_die;
5931 struct attribute *attr, *call_line, *call_file;
5932 char *name;
5933 CORE_ADDR baseaddr;
5934 struct block *block;
5935 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5936 VEC (symbolp) *template_args = NULL;
5937 struct template_symbol *templ_func = NULL;
5938
5939 if (inlined_func)
5940 {
5941 /* If we do not have call site information, we can't show the
5942 caller of this inlined function. That's too confusing, so
5943 only use the scope for local variables. */
5944 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5945 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5946 if (call_line == NULL || call_file == NULL)
5947 {
5948 read_lexical_block_scope (die, cu);
5949 return;
5950 }
5951 }
5952
5953 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5954
5955 name = dwarf2_name (die, cu);
5956
5957 /* Ignore functions with missing or empty names. These are actually
5958 illegal according to the DWARF standard. */
5959 if (name == NULL)
5960 {
5961 complaint (&symfile_complaints,
5962 _("missing name for subprogram DIE at %d"), die->offset);
5963 return;
5964 }
5965
5966 /* Ignore functions with missing or invalid low and high pc attributes. */
5967 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5968 {
5969 attr = dwarf2_attr (die, DW_AT_external, cu);
5970 if (!attr || !DW_UNSND (attr))
5971 complaint (&symfile_complaints,
5972 _("cannot get low and high bounds "
5973 "for subprogram DIE at %d"),
5974 die->offset);
5975 return;
5976 }
5977
5978 lowpc += baseaddr;
5979 highpc += baseaddr;
5980
5981 /* Record the function range for dwarf_decode_lines. */
5982 add_to_cu_func_list (name, lowpc, highpc, cu);
5983
5984 /* If we have any template arguments, then we must allocate a
5985 different sort of symbol. */
5986 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5987 {
5988 if (child_die->tag == DW_TAG_template_type_param
5989 || child_die->tag == DW_TAG_template_value_param)
5990 {
5991 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5992 struct template_symbol);
5993 templ_func->base.is_cplus_template_function = 1;
5994 break;
5995 }
5996 }
5997
5998 new = push_context (0, lowpc);
5999 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6000 (struct symbol *) templ_func);
6001
6002 /* If there is a location expression for DW_AT_frame_base, record
6003 it. */
6004 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6005 if (attr)
6006 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6007 expression is being recorded directly in the function's symbol
6008 and not in a separate frame-base object. I guess this hack is
6009 to avoid adding some sort of frame-base adjunct/annex to the
6010 function's symbol :-(. The problem with doing this is that it
6011 results in a function symbol with a location expression that
6012 has nothing to do with the location of the function, ouch! The
6013 relationship should be: a function's symbol has-a frame base; a
6014 frame-base has-a location expression. */
6015 dwarf2_symbol_mark_computed (attr, new->name, cu);
6016
6017 cu->list_in_scope = &local_symbols;
6018
6019 if (die->child != NULL)
6020 {
6021 child_die = die->child;
6022 while (child_die && child_die->tag)
6023 {
6024 if (child_die->tag == DW_TAG_template_type_param
6025 || child_die->tag == DW_TAG_template_value_param)
6026 {
6027 struct symbol *arg = new_symbol (child_die, NULL, cu);
6028
6029 if (arg != NULL)
6030 VEC_safe_push (symbolp, template_args, arg);
6031 }
6032 else
6033 process_die (child_die, cu);
6034 child_die = sibling_die (child_die);
6035 }
6036 }
6037
6038 inherit_abstract_dies (die, cu);
6039
6040 /* If we have a DW_AT_specification, we might need to import using
6041 directives from the context of the specification DIE. See the
6042 comment in determine_prefix. */
6043 if (cu->language == language_cplus
6044 && dwarf2_attr (die, DW_AT_specification, cu))
6045 {
6046 struct dwarf2_cu *spec_cu = cu;
6047 struct die_info *spec_die = die_specification (die, &spec_cu);
6048
6049 while (spec_die)
6050 {
6051 child_die = spec_die->child;
6052 while (child_die && child_die->tag)
6053 {
6054 if (child_die->tag == DW_TAG_imported_module)
6055 process_die (child_die, spec_cu);
6056 child_die = sibling_die (child_die);
6057 }
6058
6059 /* In some cases, GCC generates specification DIEs that
6060 themselves contain DW_AT_specification attributes. */
6061 spec_die = die_specification (spec_die, &spec_cu);
6062 }
6063 }
6064
6065 new = pop_context ();
6066 /* Make a block for the local symbols within. */
6067 block = finish_block (new->name, &local_symbols, new->old_blocks,
6068 lowpc, highpc, objfile);
6069
6070 /* For C++, set the block's scope. */
6071 if (cu->language == language_cplus || cu->language == language_fortran)
6072 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6073 determine_prefix (die, cu),
6074 processing_has_namespace_info);
6075
6076 /* If we have address ranges, record them. */
6077 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6078
6079 /* Attach template arguments to function. */
6080 if (! VEC_empty (symbolp, template_args))
6081 {
6082 gdb_assert (templ_func != NULL);
6083
6084 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6085 templ_func->template_arguments
6086 = obstack_alloc (&objfile->objfile_obstack,
6087 (templ_func->n_template_arguments
6088 * sizeof (struct symbol *)));
6089 memcpy (templ_func->template_arguments,
6090 VEC_address (symbolp, template_args),
6091 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6092 VEC_free (symbolp, template_args);
6093 }
6094
6095 /* In C++, we can have functions nested inside functions (e.g., when
6096 a function declares a class that has methods). This means that
6097 when we finish processing a function scope, we may need to go
6098 back to building a containing block's symbol lists. */
6099 local_symbols = new->locals;
6100 param_symbols = new->params;
6101 using_directives = new->using_directives;
6102
6103 /* If we've finished processing a top-level function, subsequent
6104 symbols go in the file symbol list. */
6105 if (outermost_context_p ())
6106 cu->list_in_scope = &file_symbols;
6107 }
6108
6109 /* Process all the DIES contained within a lexical block scope. Start
6110 a new scope, process the dies, and then close the scope. */
6111
6112 static void
6113 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6114 {
6115 struct objfile *objfile = cu->objfile;
6116 struct context_stack *new;
6117 CORE_ADDR lowpc, highpc;
6118 struct die_info *child_die;
6119 CORE_ADDR baseaddr;
6120
6121 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6122
6123 /* Ignore blocks with missing or invalid low and high pc attributes. */
6124 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6125 as multiple lexical blocks? Handling children in a sane way would
6126 be nasty. Might be easier to properly extend generic blocks to
6127 describe ranges. */
6128 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6129 return;
6130 lowpc += baseaddr;
6131 highpc += baseaddr;
6132
6133 push_context (0, lowpc);
6134 if (die->child != NULL)
6135 {
6136 child_die = die->child;
6137 while (child_die && child_die->tag)
6138 {
6139 process_die (child_die, cu);
6140 child_die = sibling_die (child_die);
6141 }
6142 }
6143 new = pop_context ();
6144
6145 if (local_symbols != NULL || using_directives != NULL)
6146 {
6147 struct block *block
6148 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6149 highpc, objfile);
6150
6151 /* Note that recording ranges after traversing children, as we
6152 do here, means that recording a parent's ranges entails
6153 walking across all its children's ranges as they appear in
6154 the address map, which is quadratic behavior.
6155
6156 It would be nicer to record the parent's ranges before
6157 traversing its children, simply overriding whatever you find
6158 there. But since we don't even decide whether to create a
6159 block until after we've traversed its children, that's hard
6160 to do. */
6161 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6162 }
6163 local_symbols = new->locals;
6164 using_directives = new->using_directives;
6165 }
6166
6167 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6168 Return 1 if the attributes are present and valid, otherwise, return 0.
6169 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6170
6171 static int
6172 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6173 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6174 struct partial_symtab *ranges_pst)
6175 {
6176 struct objfile *objfile = cu->objfile;
6177 struct comp_unit_head *cu_header = &cu->header;
6178 bfd *obfd = objfile->obfd;
6179 unsigned int addr_size = cu_header->addr_size;
6180 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6181 /* Base address selection entry. */
6182 CORE_ADDR base;
6183 int found_base;
6184 unsigned int dummy;
6185 gdb_byte *buffer;
6186 CORE_ADDR marker;
6187 int low_set;
6188 CORE_ADDR low = 0;
6189 CORE_ADDR high = 0;
6190 CORE_ADDR baseaddr;
6191
6192 found_base = cu->base_known;
6193 base = cu->base_address;
6194
6195 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6196 if (offset >= dwarf2_per_objfile->ranges.size)
6197 {
6198 complaint (&symfile_complaints,
6199 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6200 offset);
6201 return 0;
6202 }
6203 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6204
6205 /* Read in the largest possible address. */
6206 marker = read_address (obfd, buffer, cu, &dummy);
6207 if ((marker & mask) == mask)
6208 {
6209 /* If we found the largest possible address, then
6210 read the base address. */
6211 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6212 buffer += 2 * addr_size;
6213 offset += 2 * addr_size;
6214 found_base = 1;
6215 }
6216
6217 low_set = 0;
6218
6219 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6220
6221 while (1)
6222 {
6223 CORE_ADDR range_beginning, range_end;
6224
6225 range_beginning = read_address (obfd, buffer, cu, &dummy);
6226 buffer += addr_size;
6227 range_end = read_address (obfd, buffer, cu, &dummy);
6228 buffer += addr_size;
6229 offset += 2 * addr_size;
6230
6231 /* An end of list marker is a pair of zero addresses. */
6232 if (range_beginning == 0 && range_end == 0)
6233 /* Found the end of list entry. */
6234 break;
6235
6236 /* Each base address selection entry is a pair of 2 values.
6237 The first is the largest possible address, the second is
6238 the base address. Check for a base address here. */
6239 if ((range_beginning & mask) == mask)
6240 {
6241 /* If we found the largest possible address, then
6242 read the base address. */
6243 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6244 found_base = 1;
6245 continue;
6246 }
6247
6248 if (!found_base)
6249 {
6250 /* We have no valid base address for the ranges
6251 data. */
6252 complaint (&symfile_complaints,
6253 _("Invalid .debug_ranges data (no base address)"));
6254 return 0;
6255 }
6256
6257 if (range_beginning > range_end)
6258 {
6259 /* Inverted range entries are invalid. */
6260 complaint (&symfile_complaints,
6261 _("Invalid .debug_ranges data (inverted range)"));
6262 return 0;
6263 }
6264
6265 /* Empty range entries have no effect. */
6266 if (range_beginning == range_end)
6267 continue;
6268
6269 range_beginning += base;
6270 range_end += base;
6271
6272 if (ranges_pst != NULL)
6273 addrmap_set_empty (objfile->psymtabs_addrmap,
6274 range_beginning + baseaddr,
6275 range_end - 1 + baseaddr,
6276 ranges_pst);
6277
6278 /* FIXME: This is recording everything as a low-high
6279 segment of consecutive addresses. We should have a
6280 data structure for discontiguous block ranges
6281 instead. */
6282 if (! low_set)
6283 {
6284 low = range_beginning;
6285 high = range_end;
6286 low_set = 1;
6287 }
6288 else
6289 {
6290 if (range_beginning < low)
6291 low = range_beginning;
6292 if (range_end > high)
6293 high = range_end;
6294 }
6295 }
6296
6297 if (! low_set)
6298 /* If the first entry is an end-of-list marker, the range
6299 describes an empty scope, i.e. no instructions. */
6300 return 0;
6301
6302 if (low_return)
6303 *low_return = low;
6304 if (high_return)
6305 *high_return = high;
6306 return 1;
6307 }
6308
6309 /* Get low and high pc attributes from a die. Return 1 if the attributes
6310 are present and valid, otherwise, return 0. Return -1 if the range is
6311 discontinuous, i.e. derived from DW_AT_ranges information. */
6312 static int
6313 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6314 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6315 struct partial_symtab *pst)
6316 {
6317 struct attribute *attr;
6318 CORE_ADDR low = 0;
6319 CORE_ADDR high = 0;
6320 int ret = 0;
6321
6322 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6323 if (attr)
6324 {
6325 high = DW_ADDR (attr);
6326 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6327 if (attr)
6328 low = DW_ADDR (attr);
6329 else
6330 /* Found high w/o low attribute. */
6331 return 0;
6332
6333 /* Found consecutive range of addresses. */
6334 ret = 1;
6335 }
6336 else
6337 {
6338 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6339 if (attr != NULL)
6340 {
6341 /* Value of the DW_AT_ranges attribute is the offset in the
6342 .debug_ranges section. */
6343 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6344 return 0;
6345 /* Found discontinuous range of addresses. */
6346 ret = -1;
6347 }
6348 }
6349
6350 /* read_partial_die has also the strict LOW < HIGH requirement. */
6351 if (high <= low)
6352 return 0;
6353
6354 /* When using the GNU linker, .gnu.linkonce. sections are used to
6355 eliminate duplicate copies of functions and vtables and such.
6356 The linker will arbitrarily choose one and discard the others.
6357 The AT_*_pc values for such functions refer to local labels in
6358 these sections. If the section from that file was discarded, the
6359 labels are not in the output, so the relocs get a value of 0.
6360 If this is a discarded function, mark the pc bounds as invalid,
6361 so that GDB will ignore it. */
6362 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6363 return 0;
6364
6365 *lowpc = low;
6366 *highpc = high;
6367 return ret;
6368 }
6369
6370 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6371 its low and high PC addresses. Do nothing if these addresses could not
6372 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6373 and HIGHPC to the high address if greater than HIGHPC. */
6374
6375 static void
6376 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6377 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6378 struct dwarf2_cu *cu)
6379 {
6380 CORE_ADDR low, high;
6381 struct die_info *child = die->child;
6382
6383 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6384 {
6385 *lowpc = min (*lowpc, low);
6386 *highpc = max (*highpc, high);
6387 }
6388
6389 /* If the language does not allow nested subprograms (either inside
6390 subprograms or lexical blocks), we're done. */
6391 if (cu->language != language_ada)
6392 return;
6393
6394 /* Check all the children of the given DIE. If it contains nested
6395 subprograms, then check their pc bounds. Likewise, we need to
6396 check lexical blocks as well, as they may also contain subprogram
6397 definitions. */
6398 while (child && child->tag)
6399 {
6400 if (child->tag == DW_TAG_subprogram
6401 || child->tag == DW_TAG_lexical_block)
6402 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6403 child = sibling_die (child);
6404 }
6405 }
6406
6407 /* Get the low and high pc's represented by the scope DIE, and store
6408 them in *LOWPC and *HIGHPC. If the correct values can't be
6409 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6410
6411 static void
6412 get_scope_pc_bounds (struct die_info *die,
6413 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6414 struct dwarf2_cu *cu)
6415 {
6416 CORE_ADDR best_low = (CORE_ADDR) -1;
6417 CORE_ADDR best_high = (CORE_ADDR) 0;
6418 CORE_ADDR current_low, current_high;
6419
6420 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6421 {
6422 best_low = current_low;
6423 best_high = current_high;
6424 }
6425 else
6426 {
6427 struct die_info *child = die->child;
6428
6429 while (child && child->tag)
6430 {
6431 switch (child->tag) {
6432 case DW_TAG_subprogram:
6433 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6434 break;
6435 case DW_TAG_namespace:
6436 case DW_TAG_module:
6437 /* FIXME: carlton/2004-01-16: Should we do this for
6438 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6439 that current GCC's always emit the DIEs corresponding
6440 to definitions of methods of classes as children of a
6441 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6442 the DIEs giving the declarations, which could be
6443 anywhere). But I don't see any reason why the
6444 standards says that they have to be there. */
6445 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6446
6447 if (current_low != ((CORE_ADDR) -1))
6448 {
6449 best_low = min (best_low, current_low);
6450 best_high = max (best_high, current_high);
6451 }
6452 break;
6453 default:
6454 /* Ignore. */
6455 break;
6456 }
6457
6458 child = sibling_die (child);
6459 }
6460 }
6461
6462 *lowpc = best_low;
6463 *highpc = best_high;
6464 }
6465
6466 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6467 in DIE. */
6468 static void
6469 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6470 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6471 {
6472 struct attribute *attr;
6473
6474 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6475 if (attr)
6476 {
6477 CORE_ADDR high = DW_ADDR (attr);
6478
6479 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6480 if (attr)
6481 {
6482 CORE_ADDR low = DW_ADDR (attr);
6483
6484 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6485 }
6486 }
6487
6488 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6489 if (attr)
6490 {
6491 bfd *obfd = cu->objfile->obfd;
6492
6493 /* The value of the DW_AT_ranges attribute is the offset of the
6494 address range list in the .debug_ranges section. */
6495 unsigned long offset = DW_UNSND (attr);
6496 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6497
6498 /* For some target architectures, but not others, the
6499 read_address function sign-extends the addresses it returns.
6500 To recognize base address selection entries, we need a
6501 mask. */
6502 unsigned int addr_size = cu->header.addr_size;
6503 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6504
6505 /* The base address, to which the next pair is relative. Note
6506 that this 'base' is a DWARF concept: most entries in a range
6507 list are relative, to reduce the number of relocs against the
6508 debugging information. This is separate from this function's
6509 'baseaddr' argument, which GDB uses to relocate debugging
6510 information from a shared library based on the address at
6511 which the library was loaded. */
6512 CORE_ADDR base = cu->base_address;
6513 int base_known = cu->base_known;
6514
6515 gdb_assert (dwarf2_per_objfile->ranges.readin);
6516 if (offset >= dwarf2_per_objfile->ranges.size)
6517 {
6518 complaint (&symfile_complaints,
6519 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6520 offset);
6521 return;
6522 }
6523
6524 for (;;)
6525 {
6526 unsigned int bytes_read;
6527 CORE_ADDR start, end;
6528
6529 start = read_address (obfd, buffer, cu, &bytes_read);
6530 buffer += bytes_read;
6531 end = read_address (obfd, buffer, cu, &bytes_read);
6532 buffer += bytes_read;
6533
6534 /* Did we find the end of the range list? */
6535 if (start == 0 && end == 0)
6536 break;
6537
6538 /* Did we find a base address selection entry? */
6539 else if ((start & base_select_mask) == base_select_mask)
6540 {
6541 base = end;
6542 base_known = 1;
6543 }
6544
6545 /* We found an ordinary address range. */
6546 else
6547 {
6548 if (!base_known)
6549 {
6550 complaint (&symfile_complaints,
6551 _("Invalid .debug_ranges data "
6552 "(no base address)"));
6553 return;
6554 }
6555
6556 if (start > end)
6557 {
6558 /* Inverted range entries are invalid. */
6559 complaint (&symfile_complaints,
6560 _("Invalid .debug_ranges data "
6561 "(inverted range)"));
6562 return;
6563 }
6564
6565 /* Empty range entries have no effect. */
6566 if (start == end)
6567 continue;
6568
6569 record_block_range (block,
6570 baseaddr + base + start,
6571 baseaddr + base + end - 1);
6572 }
6573 }
6574 }
6575 }
6576
6577 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6578 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6579 during 4.6.0 experimental. */
6580
6581 static int
6582 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6583 {
6584 const char *cs;
6585 int major, minor, release;
6586
6587 if (cu->producer == NULL)
6588 {
6589 /* For unknown compilers expect their behavior is DWARF version
6590 compliant.
6591
6592 GCC started to support .debug_types sections by -gdwarf-4 since
6593 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6594 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6595 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6596 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6597
6598 return 0;
6599 }
6600
6601 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6602
6603 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6604 {
6605 /* For non-GCC compilers expect their behavior is DWARF version
6606 compliant. */
6607
6608 return 0;
6609 }
6610 cs = &cu->producer[strlen ("GNU ")];
6611 while (*cs && !isdigit (*cs))
6612 cs++;
6613 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6614 {
6615 /* Not recognized as GCC. */
6616
6617 return 0;
6618 }
6619
6620 return major < 4 || (major == 4 && minor < 6);
6621 }
6622
6623 /* Return the default accessibility type if it is not overriden by
6624 DW_AT_accessibility. */
6625
6626 static enum dwarf_access_attribute
6627 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6628 {
6629 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6630 {
6631 /* The default DWARF 2 accessibility for members is public, the default
6632 accessibility for inheritance is private. */
6633
6634 if (die->tag != DW_TAG_inheritance)
6635 return DW_ACCESS_public;
6636 else
6637 return DW_ACCESS_private;
6638 }
6639 else
6640 {
6641 /* DWARF 3+ defines the default accessibility a different way. The same
6642 rules apply now for DW_TAG_inheritance as for the members and it only
6643 depends on the container kind. */
6644
6645 if (die->parent->tag == DW_TAG_class_type)
6646 return DW_ACCESS_private;
6647 else
6648 return DW_ACCESS_public;
6649 }
6650 }
6651
6652 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6653 offset. If the attribute was not found return 0, otherwise return
6654 1. If it was found but could not properly be handled, set *OFFSET
6655 to 0. */
6656
6657 static int
6658 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6659 LONGEST *offset)
6660 {
6661 struct attribute *attr;
6662
6663 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6664 if (attr != NULL)
6665 {
6666 *offset = 0;
6667
6668 /* Note that we do not check for a section offset first here.
6669 This is because DW_AT_data_member_location is new in DWARF 4,
6670 so if we see it, we can assume that a constant form is really
6671 a constant and not a section offset. */
6672 if (attr_form_is_constant (attr))
6673 *offset = dwarf2_get_attr_constant_value (attr, 0);
6674 else if (attr_form_is_section_offset (attr))
6675 dwarf2_complex_location_expr_complaint ();
6676 else if (attr_form_is_block (attr))
6677 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6678 else
6679 dwarf2_complex_location_expr_complaint ();
6680
6681 return 1;
6682 }
6683
6684 return 0;
6685 }
6686
6687 /* Add an aggregate field to the field list. */
6688
6689 static void
6690 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6691 struct dwarf2_cu *cu)
6692 {
6693 struct objfile *objfile = cu->objfile;
6694 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6695 struct nextfield *new_field;
6696 struct attribute *attr;
6697 struct field *fp;
6698 char *fieldname = "";
6699
6700 /* Allocate a new field list entry and link it in. */
6701 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6702 make_cleanup (xfree, new_field);
6703 memset (new_field, 0, sizeof (struct nextfield));
6704
6705 if (die->tag == DW_TAG_inheritance)
6706 {
6707 new_field->next = fip->baseclasses;
6708 fip->baseclasses = new_field;
6709 }
6710 else
6711 {
6712 new_field->next = fip->fields;
6713 fip->fields = new_field;
6714 }
6715 fip->nfields++;
6716
6717 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6718 if (attr)
6719 new_field->accessibility = DW_UNSND (attr);
6720 else
6721 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6722 if (new_field->accessibility != DW_ACCESS_public)
6723 fip->non_public_fields = 1;
6724
6725 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6726 if (attr)
6727 new_field->virtuality = DW_UNSND (attr);
6728 else
6729 new_field->virtuality = DW_VIRTUALITY_none;
6730
6731 fp = &new_field->field;
6732
6733 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6734 {
6735 LONGEST offset;
6736
6737 /* Data member other than a C++ static data member. */
6738
6739 /* Get type of field. */
6740 fp->type = die_type (die, cu);
6741
6742 SET_FIELD_BITPOS (*fp, 0);
6743
6744 /* Get bit size of field (zero if none). */
6745 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6746 if (attr)
6747 {
6748 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6749 }
6750 else
6751 {
6752 FIELD_BITSIZE (*fp) = 0;
6753 }
6754
6755 /* Get bit offset of field. */
6756 if (handle_data_member_location (die, cu, &offset))
6757 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6758 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6759 if (attr)
6760 {
6761 if (gdbarch_bits_big_endian (gdbarch))
6762 {
6763 /* For big endian bits, the DW_AT_bit_offset gives the
6764 additional bit offset from the MSB of the containing
6765 anonymous object to the MSB of the field. We don't
6766 have to do anything special since we don't need to
6767 know the size of the anonymous object. */
6768 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6769 }
6770 else
6771 {
6772 /* For little endian bits, compute the bit offset to the
6773 MSB of the anonymous object, subtract off the number of
6774 bits from the MSB of the field to the MSB of the
6775 object, and then subtract off the number of bits of
6776 the field itself. The result is the bit offset of
6777 the LSB of the field. */
6778 int anonymous_size;
6779 int bit_offset = DW_UNSND (attr);
6780
6781 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6782 if (attr)
6783 {
6784 /* The size of the anonymous object containing
6785 the bit field is explicit, so use the
6786 indicated size (in bytes). */
6787 anonymous_size = DW_UNSND (attr);
6788 }
6789 else
6790 {
6791 /* The size of the anonymous object containing
6792 the bit field must be inferred from the type
6793 attribute of the data member containing the
6794 bit field. */
6795 anonymous_size = TYPE_LENGTH (fp->type);
6796 }
6797 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6798 - bit_offset - FIELD_BITSIZE (*fp);
6799 }
6800 }
6801
6802 /* Get name of field. */
6803 fieldname = dwarf2_name (die, cu);
6804 if (fieldname == NULL)
6805 fieldname = "";
6806
6807 /* The name is already allocated along with this objfile, so we don't
6808 need to duplicate it for the type. */
6809 fp->name = fieldname;
6810
6811 /* Change accessibility for artificial fields (e.g. virtual table
6812 pointer or virtual base class pointer) to private. */
6813 if (dwarf2_attr (die, DW_AT_artificial, cu))
6814 {
6815 FIELD_ARTIFICIAL (*fp) = 1;
6816 new_field->accessibility = DW_ACCESS_private;
6817 fip->non_public_fields = 1;
6818 }
6819 }
6820 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6821 {
6822 /* C++ static member. */
6823
6824 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6825 is a declaration, but all versions of G++ as of this writing
6826 (so through at least 3.2.1) incorrectly generate
6827 DW_TAG_variable tags. */
6828
6829 const char *physname;
6830
6831 /* Get name of field. */
6832 fieldname = dwarf2_name (die, cu);
6833 if (fieldname == NULL)
6834 return;
6835
6836 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6837 if (attr
6838 /* Only create a symbol if this is an external value.
6839 new_symbol checks this and puts the value in the global symbol
6840 table, which we want. If it is not external, new_symbol
6841 will try to put the value in cu->list_in_scope which is wrong. */
6842 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6843 {
6844 /* A static const member, not much different than an enum as far as
6845 we're concerned, except that we can support more types. */
6846 new_symbol (die, NULL, cu);
6847 }
6848
6849 /* Get physical name. */
6850 physname = dwarf2_physname (fieldname, die, cu);
6851
6852 /* The name is already allocated along with this objfile, so we don't
6853 need to duplicate it for the type. */
6854 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6855 FIELD_TYPE (*fp) = die_type (die, cu);
6856 FIELD_NAME (*fp) = fieldname;
6857 }
6858 else if (die->tag == DW_TAG_inheritance)
6859 {
6860 LONGEST offset;
6861
6862 /* C++ base class field. */
6863 if (handle_data_member_location (die, cu, &offset))
6864 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6865 FIELD_BITSIZE (*fp) = 0;
6866 FIELD_TYPE (*fp) = die_type (die, cu);
6867 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6868 fip->nbaseclasses++;
6869 }
6870 }
6871
6872 /* Add a typedef defined in the scope of the FIP's class. */
6873
6874 static void
6875 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6876 struct dwarf2_cu *cu)
6877 {
6878 struct objfile *objfile = cu->objfile;
6879 struct typedef_field_list *new_field;
6880 struct attribute *attr;
6881 struct typedef_field *fp;
6882 char *fieldname = "";
6883
6884 /* Allocate a new field list entry and link it in. */
6885 new_field = xzalloc (sizeof (*new_field));
6886 make_cleanup (xfree, new_field);
6887
6888 gdb_assert (die->tag == DW_TAG_typedef);
6889
6890 fp = &new_field->field;
6891
6892 /* Get name of field. */
6893 fp->name = dwarf2_name (die, cu);
6894 if (fp->name == NULL)
6895 return;
6896
6897 fp->type = read_type_die (die, cu);
6898
6899 new_field->next = fip->typedef_field_list;
6900 fip->typedef_field_list = new_field;
6901 fip->typedef_field_list_count++;
6902 }
6903
6904 /* Create the vector of fields, and attach it to the type. */
6905
6906 static void
6907 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6908 struct dwarf2_cu *cu)
6909 {
6910 int nfields = fip->nfields;
6911
6912 /* Record the field count, allocate space for the array of fields,
6913 and create blank accessibility bitfields if necessary. */
6914 TYPE_NFIELDS (type) = nfields;
6915 TYPE_FIELDS (type) = (struct field *)
6916 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6917 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6918
6919 if (fip->non_public_fields && cu->language != language_ada)
6920 {
6921 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6922
6923 TYPE_FIELD_PRIVATE_BITS (type) =
6924 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6925 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6926
6927 TYPE_FIELD_PROTECTED_BITS (type) =
6928 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6929 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6930
6931 TYPE_FIELD_IGNORE_BITS (type) =
6932 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6933 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6934 }
6935
6936 /* If the type has baseclasses, allocate and clear a bit vector for
6937 TYPE_FIELD_VIRTUAL_BITS. */
6938 if (fip->nbaseclasses && cu->language != language_ada)
6939 {
6940 int num_bytes = B_BYTES (fip->nbaseclasses);
6941 unsigned char *pointer;
6942
6943 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6944 pointer = TYPE_ALLOC (type, num_bytes);
6945 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6946 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6947 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6948 }
6949
6950 /* Copy the saved-up fields into the field vector. Start from the head of
6951 the list, adding to the tail of the field array, so that they end up in
6952 the same order in the array in which they were added to the list. */
6953 while (nfields-- > 0)
6954 {
6955 struct nextfield *fieldp;
6956
6957 if (fip->fields)
6958 {
6959 fieldp = fip->fields;
6960 fip->fields = fieldp->next;
6961 }
6962 else
6963 {
6964 fieldp = fip->baseclasses;
6965 fip->baseclasses = fieldp->next;
6966 }
6967
6968 TYPE_FIELD (type, nfields) = fieldp->field;
6969 switch (fieldp->accessibility)
6970 {
6971 case DW_ACCESS_private:
6972 if (cu->language != language_ada)
6973 SET_TYPE_FIELD_PRIVATE (type, nfields);
6974 break;
6975
6976 case DW_ACCESS_protected:
6977 if (cu->language != language_ada)
6978 SET_TYPE_FIELD_PROTECTED (type, nfields);
6979 break;
6980
6981 case DW_ACCESS_public:
6982 break;
6983
6984 default:
6985 /* Unknown accessibility. Complain and treat it as public. */
6986 {
6987 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6988 fieldp->accessibility);
6989 }
6990 break;
6991 }
6992 if (nfields < fip->nbaseclasses)
6993 {
6994 switch (fieldp->virtuality)
6995 {
6996 case DW_VIRTUALITY_virtual:
6997 case DW_VIRTUALITY_pure_virtual:
6998 if (cu->language == language_ada)
6999 error (_("unexpected virtuality in component of Ada type"));
7000 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7001 break;
7002 }
7003 }
7004 }
7005 }
7006
7007 /* Add a member function to the proper fieldlist. */
7008
7009 static void
7010 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7011 struct type *type, struct dwarf2_cu *cu)
7012 {
7013 struct objfile *objfile = cu->objfile;
7014 struct attribute *attr;
7015 struct fnfieldlist *flp;
7016 int i;
7017 struct fn_field *fnp;
7018 char *fieldname;
7019 struct nextfnfield *new_fnfield;
7020 struct type *this_type;
7021 enum dwarf_access_attribute accessibility;
7022
7023 if (cu->language == language_ada)
7024 error (_("unexpected member function in Ada type"));
7025
7026 /* Get name of member function. */
7027 fieldname = dwarf2_name (die, cu);
7028 if (fieldname == NULL)
7029 return;
7030
7031 /* Look up member function name in fieldlist. */
7032 for (i = 0; i < fip->nfnfields; i++)
7033 {
7034 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7035 break;
7036 }
7037
7038 /* Create new list element if necessary. */
7039 if (i < fip->nfnfields)
7040 flp = &fip->fnfieldlists[i];
7041 else
7042 {
7043 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7044 {
7045 fip->fnfieldlists = (struct fnfieldlist *)
7046 xrealloc (fip->fnfieldlists,
7047 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7048 * sizeof (struct fnfieldlist));
7049 if (fip->nfnfields == 0)
7050 make_cleanup (free_current_contents, &fip->fnfieldlists);
7051 }
7052 flp = &fip->fnfieldlists[fip->nfnfields];
7053 flp->name = fieldname;
7054 flp->length = 0;
7055 flp->head = NULL;
7056 i = fip->nfnfields++;
7057 }
7058
7059 /* Create a new member function field and chain it to the field list
7060 entry. */
7061 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7062 make_cleanup (xfree, new_fnfield);
7063 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7064 new_fnfield->next = flp->head;
7065 flp->head = new_fnfield;
7066 flp->length++;
7067
7068 /* Fill in the member function field info. */
7069 fnp = &new_fnfield->fnfield;
7070
7071 /* Delay processing of the physname until later. */
7072 if (cu->language == language_cplus || cu->language == language_java)
7073 {
7074 add_to_method_list (type, i, flp->length - 1, fieldname,
7075 die, cu);
7076 }
7077 else
7078 {
7079 const char *physname = dwarf2_physname (fieldname, die, cu);
7080 fnp->physname = physname ? physname : "";
7081 }
7082
7083 fnp->type = alloc_type (objfile);
7084 this_type = read_type_die (die, cu);
7085 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7086 {
7087 int nparams = TYPE_NFIELDS (this_type);
7088
7089 /* TYPE is the domain of this method, and THIS_TYPE is the type
7090 of the method itself (TYPE_CODE_METHOD). */
7091 smash_to_method_type (fnp->type, type,
7092 TYPE_TARGET_TYPE (this_type),
7093 TYPE_FIELDS (this_type),
7094 TYPE_NFIELDS (this_type),
7095 TYPE_VARARGS (this_type));
7096
7097 /* Handle static member functions.
7098 Dwarf2 has no clean way to discern C++ static and non-static
7099 member functions. G++ helps GDB by marking the first
7100 parameter for non-static member functions (which is the this
7101 pointer) as artificial. We obtain this information from
7102 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7103 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7104 fnp->voffset = VOFFSET_STATIC;
7105 }
7106 else
7107 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7108 dwarf2_full_name (fieldname, die, cu));
7109
7110 /* Get fcontext from DW_AT_containing_type if present. */
7111 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7112 fnp->fcontext = die_containing_type (die, cu);
7113
7114 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7115 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7116
7117 /* Get accessibility. */
7118 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7119 if (attr)
7120 accessibility = DW_UNSND (attr);
7121 else
7122 accessibility = dwarf2_default_access_attribute (die, cu);
7123 switch (accessibility)
7124 {
7125 case DW_ACCESS_private:
7126 fnp->is_private = 1;
7127 break;
7128 case DW_ACCESS_protected:
7129 fnp->is_protected = 1;
7130 break;
7131 }
7132
7133 /* Check for artificial methods. */
7134 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7135 if (attr && DW_UNSND (attr) != 0)
7136 fnp->is_artificial = 1;
7137
7138 /* Get index in virtual function table if it is a virtual member
7139 function. For older versions of GCC, this is an offset in the
7140 appropriate virtual table, as specified by DW_AT_containing_type.
7141 For everyone else, it is an expression to be evaluated relative
7142 to the object address. */
7143
7144 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7145 if (attr)
7146 {
7147 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7148 {
7149 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7150 {
7151 /* Old-style GCC. */
7152 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7153 }
7154 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7155 || (DW_BLOCK (attr)->size > 1
7156 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7157 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7158 {
7159 struct dwarf_block blk;
7160 int offset;
7161
7162 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7163 ? 1 : 2);
7164 blk.size = DW_BLOCK (attr)->size - offset;
7165 blk.data = DW_BLOCK (attr)->data + offset;
7166 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7167 if ((fnp->voffset % cu->header.addr_size) != 0)
7168 dwarf2_complex_location_expr_complaint ();
7169 else
7170 fnp->voffset /= cu->header.addr_size;
7171 fnp->voffset += 2;
7172 }
7173 else
7174 dwarf2_complex_location_expr_complaint ();
7175
7176 if (!fnp->fcontext)
7177 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7178 }
7179 else if (attr_form_is_section_offset (attr))
7180 {
7181 dwarf2_complex_location_expr_complaint ();
7182 }
7183 else
7184 {
7185 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7186 fieldname);
7187 }
7188 }
7189 else
7190 {
7191 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7192 if (attr && DW_UNSND (attr))
7193 {
7194 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7195 complaint (&symfile_complaints,
7196 _("Member function \"%s\" (offset %d) is virtual "
7197 "but the vtable offset is not specified"),
7198 fieldname, die->offset);
7199 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7200 TYPE_CPLUS_DYNAMIC (type) = 1;
7201 }
7202 }
7203 }
7204
7205 /* Create the vector of member function fields, and attach it to the type. */
7206
7207 static void
7208 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7209 struct dwarf2_cu *cu)
7210 {
7211 struct fnfieldlist *flp;
7212 int total_length = 0;
7213 int i;
7214
7215 if (cu->language == language_ada)
7216 error (_("unexpected member functions in Ada type"));
7217
7218 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7219 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7220 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7221
7222 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7223 {
7224 struct nextfnfield *nfp = flp->head;
7225 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7226 int k;
7227
7228 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7229 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7230 fn_flp->fn_fields = (struct fn_field *)
7231 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7232 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7233 fn_flp->fn_fields[k] = nfp->fnfield;
7234
7235 total_length += flp->length;
7236 }
7237
7238 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7239 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7240 }
7241
7242 /* Returns non-zero if NAME is the name of a vtable member in CU's
7243 language, zero otherwise. */
7244 static int
7245 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7246 {
7247 static const char vptr[] = "_vptr";
7248 static const char vtable[] = "vtable";
7249
7250 /* Look for the C++ and Java forms of the vtable. */
7251 if ((cu->language == language_java
7252 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7253 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7254 && is_cplus_marker (name[sizeof (vptr) - 1])))
7255 return 1;
7256
7257 return 0;
7258 }
7259
7260 /* GCC outputs unnamed structures that are really pointers to member
7261 functions, with the ABI-specified layout. If TYPE describes
7262 such a structure, smash it into a member function type.
7263
7264 GCC shouldn't do this; it should just output pointer to member DIEs.
7265 This is GCC PR debug/28767. */
7266
7267 static void
7268 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7269 {
7270 struct type *pfn_type, *domain_type, *new_type;
7271
7272 /* Check for a structure with no name and two children. */
7273 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7274 return;
7275
7276 /* Check for __pfn and __delta members. */
7277 if (TYPE_FIELD_NAME (type, 0) == NULL
7278 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7279 || TYPE_FIELD_NAME (type, 1) == NULL
7280 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7281 return;
7282
7283 /* Find the type of the method. */
7284 pfn_type = TYPE_FIELD_TYPE (type, 0);
7285 if (pfn_type == NULL
7286 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7287 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7288 return;
7289
7290 /* Look for the "this" argument. */
7291 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7292 if (TYPE_NFIELDS (pfn_type) == 0
7293 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7294 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7295 return;
7296
7297 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7298 new_type = alloc_type (objfile);
7299 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7300 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7301 TYPE_VARARGS (pfn_type));
7302 smash_to_methodptr_type (type, new_type);
7303 }
7304
7305 /* Called when we find the DIE that starts a structure or union scope
7306 (definition) to create a type for the structure or union. Fill in
7307 the type's name and general properties; the members will not be
7308 processed until process_structure_type.
7309
7310 NOTE: we need to call these functions regardless of whether or not the
7311 DIE has a DW_AT_name attribute, since it might be an anonymous
7312 structure or union. This gets the type entered into our set of
7313 user defined types.
7314
7315 However, if the structure is incomplete (an opaque struct/union)
7316 then suppress creating a symbol table entry for it since gdb only
7317 wants to find the one with the complete definition. Note that if
7318 it is complete, we just call new_symbol, which does it's own
7319 checking about whether the struct/union is anonymous or not (and
7320 suppresses creating a symbol table entry itself). */
7321
7322 static struct type *
7323 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7324 {
7325 struct objfile *objfile = cu->objfile;
7326 struct type *type;
7327 struct attribute *attr;
7328 char *name;
7329
7330 /* If the definition of this type lives in .debug_types, read that type.
7331 Don't follow DW_AT_specification though, that will take us back up
7332 the chain and we want to go down. */
7333 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7334 if (attr)
7335 {
7336 struct dwarf2_cu *type_cu = cu;
7337 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7338
7339 /* We could just recurse on read_structure_type, but we need to call
7340 get_die_type to ensure only one type for this DIE is created.
7341 This is important, for example, because for c++ classes we need
7342 TYPE_NAME set which is only done by new_symbol. Blech. */
7343 type = read_type_die (type_die, type_cu);
7344
7345 /* TYPE_CU may not be the same as CU.
7346 Ensure TYPE is recorded in CU's type_hash table. */
7347 return set_die_type (die, type, cu);
7348 }
7349
7350 type = alloc_type (objfile);
7351 INIT_CPLUS_SPECIFIC (type);
7352
7353 name = dwarf2_name (die, cu);
7354 if (name != NULL)
7355 {
7356 if (cu->language == language_cplus
7357 || cu->language == language_java)
7358 {
7359 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7360
7361 /* dwarf2_full_name might have already finished building the DIE's
7362 type. If so, there is no need to continue. */
7363 if (get_die_type (die, cu) != NULL)
7364 return get_die_type (die, cu);
7365
7366 TYPE_TAG_NAME (type) = full_name;
7367 if (die->tag == DW_TAG_structure_type
7368 || die->tag == DW_TAG_class_type)
7369 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7370 }
7371 else
7372 {
7373 /* The name is already allocated along with this objfile, so
7374 we don't need to duplicate it for the type. */
7375 TYPE_TAG_NAME (type) = (char *) name;
7376 if (die->tag == DW_TAG_class_type)
7377 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7378 }
7379 }
7380
7381 if (die->tag == DW_TAG_structure_type)
7382 {
7383 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7384 }
7385 else if (die->tag == DW_TAG_union_type)
7386 {
7387 TYPE_CODE (type) = TYPE_CODE_UNION;
7388 }
7389 else
7390 {
7391 TYPE_CODE (type) = TYPE_CODE_CLASS;
7392 }
7393
7394 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7395 TYPE_DECLARED_CLASS (type) = 1;
7396
7397 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7398 if (attr)
7399 {
7400 TYPE_LENGTH (type) = DW_UNSND (attr);
7401 }
7402 else
7403 {
7404 TYPE_LENGTH (type) = 0;
7405 }
7406
7407 TYPE_STUB_SUPPORTED (type) = 1;
7408 if (die_is_declaration (die, cu))
7409 TYPE_STUB (type) = 1;
7410 else if (attr == NULL && die->child == NULL
7411 && producer_is_realview (cu->producer))
7412 /* RealView does not output the required DW_AT_declaration
7413 on incomplete types. */
7414 TYPE_STUB (type) = 1;
7415
7416 /* We need to add the type field to the die immediately so we don't
7417 infinitely recurse when dealing with pointers to the structure
7418 type within the structure itself. */
7419 set_die_type (die, type, cu);
7420
7421 /* set_die_type should be already done. */
7422 set_descriptive_type (type, die, cu);
7423
7424 return type;
7425 }
7426
7427 /* Finish creating a structure or union type, including filling in
7428 its members and creating a symbol for it. */
7429
7430 static void
7431 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7432 {
7433 struct objfile *objfile = cu->objfile;
7434 struct die_info *child_die = die->child;
7435 struct type *type;
7436
7437 type = get_die_type (die, cu);
7438 if (type == NULL)
7439 type = read_structure_type (die, cu);
7440
7441 if (die->child != NULL && ! die_is_declaration (die, cu))
7442 {
7443 struct field_info fi;
7444 struct die_info *child_die;
7445 VEC (symbolp) *template_args = NULL;
7446 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7447
7448 memset (&fi, 0, sizeof (struct field_info));
7449
7450 child_die = die->child;
7451
7452 while (child_die && child_die->tag)
7453 {
7454 if (child_die->tag == DW_TAG_member
7455 || child_die->tag == DW_TAG_variable)
7456 {
7457 /* NOTE: carlton/2002-11-05: A C++ static data member
7458 should be a DW_TAG_member that is a declaration, but
7459 all versions of G++ as of this writing (so through at
7460 least 3.2.1) incorrectly generate DW_TAG_variable
7461 tags for them instead. */
7462 dwarf2_add_field (&fi, child_die, cu);
7463 }
7464 else if (child_die->tag == DW_TAG_subprogram)
7465 {
7466 /* C++ member function. */
7467 dwarf2_add_member_fn (&fi, child_die, type, cu);
7468 }
7469 else if (child_die->tag == DW_TAG_inheritance)
7470 {
7471 /* C++ base class field. */
7472 dwarf2_add_field (&fi, child_die, cu);
7473 }
7474 else if (child_die->tag == DW_TAG_typedef)
7475 dwarf2_add_typedef (&fi, child_die, cu);
7476 else if (child_die->tag == DW_TAG_template_type_param
7477 || child_die->tag == DW_TAG_template_value_param)
7478 {
7479 struct symbol *arg = new_symbol (child_die, NULL, cu);
7480
7481 if (arg != NULL)
7482 VEC_safe_push (symbolp, template_args, arg);
7483 }
7484
7485 child_die = sibling_die (child_die);
7486 }
7487
7488 /* Attach template arguments to type. */
7489 if (! VEC_empty (symbolp, template_args))
7490 {
7491 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7492 TYPE_N_TEMPLATE_ARGUMENTS (type)
7493 = VEC_length (symbolp, template_args);
7494 TYPE_TEMPLATE_ARGUMENTS (type)
7495 = obstack_alloc (&objfile->objfile_obstack,
7496 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7497 * sizeof (struct symbol *)));
7498 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7499 VEC_address (symbolp, template_args),
7500 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7501 * sizeof (struct symbol *)));
7502 VEC_free (symbolp, template_args);
7503 }
7504
7505 /* Attach fields and member functions to the type. */
7506 if (fi.nfields)
7507 dwarf2_attach_fields_to_type (&fi, type, cu);
7508 if (fi.nfnfields)
7509 {
7510 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7511
7512 /* Get the type which refers to the base class (possibly this
7513 class itself) which contains the vtable pointer for the current
7514 class from the DW_AT_containing_type attribute. This use of
7515 DW_AT_containing_type is a GNU extension. */
7516
7517 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7518 {
7519 struct type *t = die_containing_type (die, cu);
7520
7521 TYPE_VPTR_BASETYPE (type) = t;
7522 if (type == t)
7523 {
7524 int i;
7525
7526 /* Our own class provides vtbl ptr. */
7527 for (i = TYPE_NFIELDS (t) - 1;
7528 i >= TYPE_N_BASECLASSES (t);
7529 --i)
7530 {
7531 char *fieldname = TYPE_FIELD_NAME (t, i);
7532
7533 if (is_vtable_name (fieldname, cu))
7534 {
7535 TYPE_VPTR_FIELDNO (type) = i;
7536 break;
7537 }
7538 }
7539
7540 /* Complain if virtual function table field not found. */
7541 if (i < TYPE_N_BASECLASSES (t))
7542 complaint (&symfile_complaints,
7543 _("virtual function table pointer "
7544 "not found when defining class '%s'"),
7545 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7546 "");
7547 }
7548 else
7549 {
7550 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7551 }
7552 }
7553 else if (cu->producer
7554 && strncmp (cu->producer,
7555 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7556 {
7557 /* The IBM XLC compiler does not provide direct indication
7558 of the containing type, but the vtable pointer is
7559 always named __vfp. */
7560
7561 int i;
7562
7563 for (i = TYPE_NFIELDS (type) - 1;
7564 i >= TYPE_N_BASECLASSES (type);
7565 --i)
7566 {
7567 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7568 {
7569 TYPE_VPTR_FIELDNO (type) = i;
7570 TYPE_VPTR_BASETYPE (type) = type;
7571 break;
7572 }
7573 }
7574 }
7575 }
7576
7577 /* Copy fi.typedef_field_list linked list elements content into the
7578 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7579 if (fi.typedef_field_list)
7580 {
7581 int i = fi.typedef_field_list_count;
7582
7583 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7584 TYPE_TYPEDEF_FIELD_ARRAY (type)
7585 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7586 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7587
7588 /* Reverse the list order to keep the debug info elements order. */
7589 while (--i >= 0)
7590 {
7591 struct typedef_field *dest, *src;
7592
7593 dest = &TYPE_TYPEDEF_FIELD (type, i);
7594 src = &fi.typedef_field_list->field;
7595 fi.typedef_field_list = fi.typedef_field_list->next;
7596 *dest = *src;
7597 }
7598 }
7599
7600 do_cleanups (back_to);
7601
7602 if (HAVE_CPLUS_STRUCT (type))
7603 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7604 }
7605
7606 quirk_gcc_member_function_pointer (type, cu->objfile);
7607
7608 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7609 snapshots) has been known to create a die giving a declaration
7610 for a class that has, as a child, a die giving a definition for a
7611 nested class. So we have to process our children even if the
7612 current die is a declaration. Normally, of course, a declaration
7613 won't have any children at all. */
7614
7615 while (child_die != NULL && child_die->tag)
7616 {
7617 if (child_die->tag == DW_TAG_member
7618 || child_die->tag == DW_TAG_variable
7619 || child_die->tag == DW_TAG_inheritance
7620 || child_die->tag == DW_TAG_template_value_param
7621 || child_die->tag == DW_TAG_template_type_param)
7622 {
7623 /* Do nothing. */
7624 }
7625 else
7626 process_die (child_die, cu);
7627
7628 child_die = sibling_die (child_die);
7629 }
7630
7631 /* Do not consider external references. According to the DWARF standard,
7632 these DIEs are identified by the fact that they have no byte_size
7633 attribute, and a declaration attribute. */
7634 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7635 || !die_is_declaration (die, cu))
7636 new_symbol (die, type, cu);
7637 }
7638
7639 /* Given a DW_AT_enumeration_type die, set its type. We do not
7640 complete the type's fields yet, or create any symbols. */
7641
7642 static struct type *
7643 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7644 {
7645 struct objfile *objfile = cu->objfile;
7646 struct type *type;
7647 struct attribute *attr;
7648 const char *name;
7649
7650 /* If the definition of this type lives in .debug_types, read that type.
7651 Don't follow DW_AT_specification though, that will take us back up
7652 the chain and we want to go down. */
7653 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7654 if (attr)
7655 {
7656 struct dwarf2_cu *type_cu = cu;
7657 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7658
7659 type = read_type_die (type_die, type_cu);
7660
7661 /* TYPE_CU may not be the same as CU.
7662 Ensure TYPE is recorded in CU's type_hash table. */
7663 return set_die_type (die, type, cu);
7664 }
7665
7666 type = alloc_type (objfile);
7667
7668 TYPE_CODE (type) = TYPE_CODE_ENUM;
7669 name = dwarf2_full_name (NULL, die, cu);
7670 if (name != NULL)
7671 TYPE_TAG_NAME (type) = (char *) name;
7672
7673 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7674 if (attr)
7675 {
7676 TYPE_LENGTH (type) = DW_UNSND (attr);
7677 }
7678 else
7679 {
7680 TYPE_LENGTH (type) = 0;
7681 }
7682
7683 /* The enumeration DIE can be incomplete. In Ada, any type can be
7684 declared as private in the package spec, and then defined only
7685 inside the package body. Such types are known as Taft Amendment
7686 Types. When another package uses such a type, an incomplete DIE
7687 may be generated by the compiler. */
7688 if (die_is_declaration (die, cu))
7689 TYPE_STUB (type) = 1;
7690
7691 return set_die_type (die, type, cu);
7692 }
7693
7694 /* Given a pointer to a die which begins an enumeration, process all
7695 the dies that define the members of the enumeration, and create the
7696 symbol for the enumeration type.
7697
7698 NOTE: We reverse the order of the element list. */
7699
7700 static void
7701 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7702 {
7703 struct type *this_type;
7704
7705 this_type = get_die_type (die, cu);
7706 if (this_type == NULL)
7707 this_type = read_enumeration_type (die, cu);
7708
7709 if (die->child != NULL)
7710 {
7711 struct die_info *child_die;
7712 struct symbol *sym;
7713 struct field *fields = NULL;
7714 int num_fields = 0;
7715 int unsigned_enum = 1;
7716 char *name;
7717
7718 child_die = die->child;
7719 while (child_die && child_die->tag)
7720 {
7721 if (child_die->tag != DW_TAG_enumerator)
7722 {
7723 process_die (child_die, cu);
7724 }
7725 else
7726 {
7727 name = dwarf2_name (child_die, cu);
7728 if (name)
7729 {
7730 sym = new_symbol (child_die, this_type, cu);
7731 if (SYMBOL_VALUE (sym) < 0)
7732 unsigned_enum = 0;
7733
7734 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7735 {
7736 fields = (struct field *)
7737 xrealloc (fields,
7738 (num_fields + DW_FIELD_ALLOC_CHUNK)
7739 * sizeof (struct field));
7740 }
7741
7742 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7743 FIELD_TYPE (fields[num_fields]) = NULL;
7744 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7745 FIELD_BITSIZE (fields[num_fields]) = 0;
7746
7747 num_fields++;
7748 }
7749 }
7750
7751 child_die = sibling_die (child_die);
7752 }
7753
7754 if (num_fields)
7755 {
7756 TYPE_NFIELDS (this_type) = num_fields;
7757 TYPE_FIELDS (this_type) = (struct field *)
7758 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7759 memcpy (TYPE_FIELDS (this_type), fields,
7760 sizeof (struct field) * num_fields);
7761 xfree (fields);
7762 }
7763 if (unsigned_enum)
7764 TYPE_UNSIGNED (this_type) = 1;
7765 }
7766
7767 /* If we are reading an enum from a .debug_types unit, and the enum
7768 is a declaration, and the enum is not the signatured type in the
7769 unit, then we do not want to add a symbol for it. Adding a
7770 symbol would in some cases obscure the true definition of the
7771 enum, giving users an incomplete type when the definition is
7772 actually available. Note that we do not want to do this for all
7773 enums which are just declarations, because C++0x allows forward
7774 enum declarations. */
7775 if (cu->per_cu->debug_type_section
7776 && die_is_declaration (die, cu))
7777 {
7778 struct signatured_type *type_sig;
7779
7780 type_sig
7781 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
7782 cu->per_cu->debug_type_section,
7783 cu->per_cu->offset);
7784 if (type_sig->type_offset != die->offset)
7785 return;
7786 }
7787
7788 new_symbol (die, this_type, cu);
7789 }
7790
7791 /* Extract all information from a DW_TAG_array_type DIE and put it in
7792 the DIE's type field. For now, this only handles one dimensional
7793 arrays. */
7794
7795 static struct type *
7796 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7797 {
7798 struct objfile *objfile = cu->objfile;
7799 struct die_info *child_die;
7800 struct type *type;
7801 struct type *element_type, *range_type, *index_type;
7802 struct type **range_types = NULL;
7803 struct attribute *attr;
7804 int ndim = 0;
7805 struct cleanup *back_to;
7806 char *name;
7807
7808 element_type = die_type (die, cu);
7809
7810 /* The die_type call above may have already set the type for this DIE. */
7811 type = get_die_type (die, cu);
7812 if (type)
7813 return type;
7814
7815 /* Irix 6.2 native cc creates array types without children for
7816 arrays with unspecified length. */
7817 if (die->child == NULL)
7818 {
7819 index_type = objfile_type (objfile)->builtin_int;
7820 range_type = create_range_type (NULL, index_type, 0, -1);
7821 type = create_array_type (NULL, element_type, range_type);
7822 return set_die_type (die, type, cu);
7823 }
7824
7825 back_to = make_cleanup (null_cleanup, NULL);
7826 child_die = die->child;
7827 while (child_die && child_die->tag)
7828 {
7829 if (child_die->tag == DW_TAG_subrange_type)
7830 {
7831 struct type *child_type = read_type_die (child_die, cu);
7832
7833 if (child_type != NULL)
7834 {
7835 /* The range type was succesfully read. Save it for the
7836 array type creation. */
7837 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7838 {
7839 range_types = (struct type **)
7840 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7841 * sizeof (struct type *));
7842 if (ndim == 0)
7843 make_cleanup (free_current_contents, &range_types);
7844 }
7845 range_types[ndim++] = child_type;
7846 }
7847 }
7848 child_die = sibling_die (child_die);
7849 }
7850
7851 /* Dwarf2 dimensions are output from left to right, create the
7852 necessary array types in backwards order. */
7853
7854 type = element_type;
7855
7856 if (read_array_order (die, cu) == DW_ORD_col_major)
7857 {
7858 int i = 0;
7859
7860 while (i < ndim)
7861 type = create_array_type (NULL, type, range_types[i++]);
7862 }
7863 else
7864 {
7865 while (ndim-- > 0)
7866 type = create_array_type (NULL, type, range_types[ndim]);
7867 }
7868
7869 /* Understand Dwarf2 support for vector types (like they occur on
7870 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7871 array type. This is not part of the Dwarf2/3 standard yet, but a
7872 custom vendor extension. The main difference between a regular
7873 array and the vector variant is that vectors are passed by value
7874 to functions. */
7875 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7876 if (attr)
7877 make_vector_type (type);
7878
7879 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7880 implementation may choose to implement triple vectors using this
7881 attribute. */
7882 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7883 if (attr)
7884 {
7885 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7886 TYPE_LENGTH (type) = DW_UNSND (attr);
7887 else
7888 complaint (&symfile_complaints,
7889 _("DW_AT_byte_size for array type smaller "
7890 "than the total size of elements"));
7891 }
7892
7893 name = dwarf2_name (die, cu);
7894 if (name)
7895 TYPE_NAME (type) = name;
7896
7897 /* Install the type in the die. */
7898 set_die_type (die, type, cu);
7899
7900 /* set_die_type should be already done. */
7901 set_descriptive_type (type, die, cu);
7902
7903 do_cleanups (back_to);
7904
7905 return type;
7906 }
7907
7908 static enum dwarf_array_dim_ordering
7909 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7910 {
7911 struct attribute *attr;
7912
7913 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7914
7915 if (attr) return DW_SND (attr);
7916
7917 /* GNU F77 is a special case, as at 08/2004 array type info is the
7918 opposite order to the dwarf2 specification, but data is still
7919 laid out as per normal fortran.
7920
7921 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7922 version checking. */
7923
7924 if (cu->language == language_fortran
7925 && cu->producer && strstr (cu->producer, "GNU F77"))
7926 {
7927 return DW_ORD_row_major;
7928 }
7929
7930 switch (cu->language_defn->la_array_ordering)
7931 {
7932 case array_column_major:
7933 return DW_ORD_col_major;
7934 case array_row_major:
7935 default:
7936 return DW_ORD_row_major;
7937 };
7938 }
7939
7940 /* Extract all information from a DW_TAG_set_type DIE and put it in
7941 the DIE's type field. */
7942
7943 static struct type *
7944 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7945 {
7946 struct type *domain_type, *set_type;
7947 struct attribute *attr;
7948
7949 domain_type = die_type (die, cu);
7950
7951 /* The die_type call above may have already set the type for this DIE. */
7952 set_type = get_die_type (die, cu);
7953 if (set_type)
7954 return set_type;
7955
7956 set_type = create_set_type (NULL, domain_type);
7957
7958 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7959 if (attr)
7960 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7961
7962 return set_die_type (die, set_type, cu);
7963 }
7964
7965 /* First cut: install each common block member as a global variable. */
7966
7967 static void
7968 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7969 {
7970 struct die_info *child_die;
7971 struct attribute *attr;
7972 struct symbol *sym;
7973 CORE_ADDR base = (CORE_ADDR) 0;
7974
7975 attr = dwarf2_attr (die, DW_AT_location, cu);
7976 if (attr)
7977 {
7978 /* Support the .debug_loc offsets. */
7979 if (attr_form_is_block (attr))
7980 {
7981 base = decode_locdesc (DW_BLOCK (attr), cu);
7982 }
7983 else if (attr_form_is_section_offset (attr))
7984 {
7985 dwarf2_complex_location_expr_complaint ();
7986 }
7987 else
7988 {
7989 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7990 "common block member");
7991 }
7992 }
7993 if (die->child != NULL)
7994 {
7995 child_die = die->child;
7996 while (child_die && child_die->tag)
7997 {
7998 LONGEST offset;
7999
8000 sym = new_symbol (child_die, NULL, cu);
8001 if (sym != NULL
8002 && handle_data_member_location (child_die, cu, &offset))
8003 {
8004 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8005 add_symbol_to_list (sym, &global_symbols);
8006 }
8007 child_die = sibling_die (child_die);
8008 }
8009 }
8010 }
8011
8012 /* Create a type for a C++ namespace. */
8013
8014 static struct type *
8015 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8016 {
8017 struct objfile *objfile = cu->objfile;
8018 const char *previous_prefix, *name;
8019 int is_anonymous;
8020 struct type *type;
8021
8022 /* For extensions, reuse the type of the original namespace. */
8023 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8024 {
8025 struct die_info *ext_die;
8026 struct dwarf2_cu *ext_cu = cu;
8027
8028 ext_die = dwarf2_extension (die, &ext_cu);
8029 type = read_type_die (ext_die, ext_cu);
8030
8031 /* EXT_CU may not be the same as CU.
8032 Ensure TYPE is recorded in CU's type_hash table. */
8033 return set_die_type (die, type, cu);
8034 }
8035
8036 name = namespace_name (die, &is_anonymous, cu);
8037
8038 /* Now build the name of the current namespace. */
8039
8040 previous_prefix = determine_prefix (die, cu);
8041 if (previous_prefix[0] != '\0')
8042 name = typename_concat (&objfile->objfile_obstack,
8043 previous_prefix, name, 0, cu);
8044
8045 /* Create the type. */
8046 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8047 objfile);
8048 TYPE_NAME (type) = (char *) name;
8049 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8050
8051 return set_die_type (die, type, cu);
8052 }
8053
8054 /* Read a C++ namespace. */
8055
8056 static void
8057 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8058 {
8059 struct objfile *objfile = cu->objfile;
8060 int is_anonymous;
8061
8062 /* Add a symbol associated to this if we haven't seen the namespace
8063 before. Also, add a using directive if it's an anonymous
8064 namespace. */
8065
8066 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8067 {
8068 struct type *type;
8069
8070 type = read_type_die (die, cu);
8071 new_symbol (die, type, cu);
8072
8073 namespace_name (die, &is_anonymous, cu);
8074 if (is_anonymous)
8075 {
8076 const char *previous_prefix = determine_prefix (die, cu);
8077
8078 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8079 NULL, NULL, &objfile->objfile_obstack);
8080 }
8081 }
8082
8083 if (die->child != NULL)
8084 {
8085 struct die_info *child_die = die->child;
8086
8087 while (child_die && child_die->tag)
8088 {
8089 process_die (child_die, cu);
8090 child_die = sibling_die (child_die);
8091 }
8092 }
8093 }
8094
8095 /* Read a Fortran module as type. This DIE can be only a declaration used for
8096 imported module. Still we need that type as local Fortran "use ... only"
8097 declaration imports depend on the created type in determine_prefix. */
8098
8099 static struct type *
8100 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8101 {
8102 struct objfile *objfile = cu->objfile;
8103 char *module_name;
8104 struct type *type;
8105
8106 module_name = dwarf2_name (die, cu);
8107 if (!module_name)
8108 complaint (&symfile_complaints,
8109 _("DW_TAG_module has no name, offset 0x%x"),
8110 die->offset);
8111 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8112
8113 /* determine_prefix uses TYPE_TAG_NAME. */
8114 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8115
8116 return set_die_type (die, type, cu);
8117 }
8118
8119 /* Read a Fortran module. */
8120
8121 static void
8122 read_module (struct die_info *die, struct dwarf2_cu *cu)
8123 {
8124 struct die_info *child_die = die->child;
8125
8126 while (child_die && child_die->tag)
8127 {
8128 process_die (child_die, cu);
8129 child_die = sibling_die (child_die);
8130 }
8131 }
8132
8133 /* Return the name of the namespace represented by DIE. Set
8134 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8135 namespace. */
8136
8137 static const char *
8138 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8139 {
8140 struct die_info *current_die;
8141 const char *name = NULL;
8142
8143 /* Loop through the extensions until we find a name. */
8144
8145 for (current_die = die;
8146 current_die != NULL;
8147 current_die = dwarf2_extension (die, &cu))
8148 {
8149 name = dwarf2_name (current_die, cu);
8150 if (name != NULL)
8151 break;
8152 }
8153
8154 /* Is it an anonymous namespace? */
8155
8156 *is_anonymous = (name == NULL);
8157 if (*is_anonymous)
8158 name = CP_ANONYMOUS_NAMESPACE_STR;
8159
8160 return name;
8161 }
8162
8163 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8164 the user defined type vector. */
8165
8166 static struct type *
8167 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8168 {
8169 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8170 struct comp_unit_head *cu_header = &cu->header;
8171 struct type *type;
8172 struct attribute *attr_byte_size;
8173 struct attribute *attr_address_class;
8174 int byte_size, addr_class;
8175 struct type *target_type;
8176
8177 target_type = die_type (die, cu);
8178
8179 /* The die_type call above may have already set the type for this DIE. */
8180 type = get_die_type (die, cu);
8181 if (type)
8182 return type;
8183
8184 type = lookup_pointer_type (target_type);
8185
8186 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8187 if (attr_byte_size)
8188 byte_size = DW_UNSND (attr_byte_size);
8189 else
8190 byte_size = cu_header->addr_size;
8191
8192 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8193 if (attr_address_class)
8194 addr_class = DW_UNSND (attr_address_class);
8195 else
8196 addr_class = DW_ADDR_none;
8197
8198 /* If the pointer size or address class is different than the
8199 default, create a type variant marked as such and set the
8200 length accordingly. */
8201 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8202 {
8203 if (gdbarch_address_class_type_flags_p (gdbarch))
8204 {
8205 int type_flags;
8206
8207 type_flags = gdbarch_address_class_type_flags
8208 (gdbarch, byte_size, addr_class);
8209 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8210 == 0);
8211 type = make_type_with_address_space (type, type_flags);
8212 }
8213 else if (TYPE_LENGTH (type) != byte_size)
8214 {
8215 complaint (&symfile_complaints,
8216 _("invalid pointer size %d"), byte_size);
8217 }
8218 else
8219 {
8220 /* Should we also complain about unhandled address classes? */
8221 }
8222 }
8223
8224 TYPE_LENGTH (type) = byte_size;
8225 return set_die_type (die, type, cu);
8226 }
8227
8228 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8229 the user defined type vector. */
8230
8231 static struct type *
8232 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8233 {
8234 struct type *type;
8235 struct type *to_type;
8236 struct type *domain;
8237
8238 to_type = die_type (die, cu);
8239 domain = die_containing_type (die, cu);
8240
8241 /* The calls above may have already set the type for this DIE. */
8242 type = get_die_type (die, cu);
8243 if (type)
8244 return type;
8245
8246 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8247 type = lookup_methodptr_type (to_type);
8248 else
8249 type = lookup_memberptr_type (to_type, domain);
8250
8251 return set_die_type (die, type, cu);
8252 }
8253
8254 /* Extract all information from a DW_TAG_reference_type DIE and add to
8255 the user defined type vector. */
8256
8257 static struct type *
8258 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8259 {
8260 struct comp_unit_head *cu_header = &cu->header;
8261 struct type *type, *target_type;
8262 struct attribute *attr;
8263
8264 target_type = die_type (die, cu);
8265
8266 /* The die_type call above may have already set the type for this DIE. */
8267 type = get_die_type (die, cu);
8268 if (type)
8269 return type;
8270
8271 type = lookup_reference_type (target_type);
8272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8273 if (attr)
8274 {
8275 TYPE_LENGTH (type) = DW_UNSND (attr);
8276 }
8277 else
8278 {
8279 TYPE_LENGTH (type) = cu_header->addr_size;
8280 }
8281 return set_die_type (die, type, cu);
8282 }
8283
8284 static struct type *
8285 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8286 {
8287 struct type *base_type, *cv_type;
8288
8289 base_type = die_type (die, cu);
8290
8291 /* The die_type call above may have already set the type for this DIE. */
8292 cv_type = get_die_type (die, cu);
8293 if (cv_type)
8294 return cv_type;
8295
8296 /* In case the const qualifier is applied to an array type, the element type
8297 is so qualified, not the array type (section 6.7.3 of C99). */
8298 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8299 {
8300 struct type *el_type, *inner_array;
8301
8302 base_type = copy_type (base_type);
8303 inner_array = base_type;
8304
8305 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8306 {
8307 TYPE_TARGET_TYPE (inner_array) =
8308 copy_type (TYPE_TARGET_TYPE (inner_array));
8309 inner_array = TYPE_TARGET_TYPE (inner_array);
8310 }
8311
8312 el_type = TYPE_TARGET_TYPE (inner_array);
8313 TYPE_TARGET_TYPE (inner_array) =
8314 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8315
8316 return set_die_type (die, base_type, cu);
8317 }
8318
8319 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8320 return set_die_type (die, cv_type, cu);
8321 }
8322
8323 static struct type *
8324 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8325 {
8326 struct type *base_type, *cv_type;
8327
8328 base_type = die_type (die, cu);
8329
8330 /* The die_type call above may have already set the type for this DIE. */
8331 cv_type = get_die_type (die, cu);
8332 if (cv_type)
8333 return cv_type;
8334
8335 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8336 return set_die_type (die, cv_type, cu);
8337 }
8338
8339 /* Extract all information from a DW_TAG_string_type DIE and add to
8340 the user defined type vector. It isn't really a user defined type,
8341 but it behaves like one, with other DIE's using an AT_user_def_type
8342 attribute to reference it. */
8343
8344 static struct type *
8345 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8346 {
8347 struct objfile *objfile = cu->objfile;
8348 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8349 struct type *type, *range_type, *index_type, *char_type;
8350 struct attribute *attr;
8351 unsigned int length;
8352
8353 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8354 if (attr)
8355 {
8356 length = DW_UNSND (attr);
8357 }
8358 else
8359 {
8360 /* Check for the DW_AT_byte_size attribute. */
8361 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8362 if (attr)
8363 {
8364 length = DW_UNSND (attr);
8365 }
8366 else
8367 {
8368 length = 1;
8369 }
8370 }
8371
8372 index_type = objfile_type (objfile)->builtin_int;
8373 range_type = create_range_type (NULL, index_type, 1, length);
8374 char_type = language_string_char_type (cu->language_defn, gdbarch);
8375 type = create_string_type (NULL, char_type, range_type);
8376
8377 return set_die_type (die, type, cu);
8378 }
8379
8380 /* Handle DIES due to C code like:
8381
8382 struct foo
8383 {
8384 int (*funcp)(int a, long l);
8385 int b;
8386 };
8387
8388 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8389
8390 static struct type *
8391 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8392 {
8393 struct type *type; /* Type that this function returns. */
8394 struct type *ftype; /* Function that returns above type. */
8395 struct attribute *attr;
8396
8397 type = die_type (die, cu);
8398
8399 /* The die_type call above may have already set the type for this DIE. */
8400 ftype = get_die_type (die, cu);
8401 if (ftype)
8402 return ftype;
8403
8404 ftype = lookup_function_type (type);
8405
8406 /* All functions in C++, Pascal and Java have prototypes. */
8407 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8408 if ((attr && (DW_UNSND (attr) != 0))
8409 || cu->language == language_cplus
8410 || cu->language == language_java
8411 || cu->language == language_pascal)
8412 TYPE_PROTOTYPED (ftype) = 1;
8413 else if (producer_is_realview (cu->producer))
8414 /* RealView does not emit DW_AT_prototyped. We can not
8415 distinguish prototyped and unprototyped functions; default to
8416 prototyped, since that is more common in modern code (and
8417 RealView warns about unprototyped functions). */
8418 TYPE_PROTOTYPED (ftype) = 1;
8419
8420 /* Store the calling convention in the type if it's available in
8421 the subroutine die. Otherwise set the calling convention to
8422 the default value DW_CC_normal. */
8423 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8424 if (attr)
8425 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8426 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8427 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8428 else
8429 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8430
8431 /* We need to add the subroutine type to the die immediately so
8432 we don't infinitely recurse when dealing with parameters
8433 declared as the same subroutine type. */
8434 set_die_type (die, ftype, cu);
8435
8436 if (die->child != NULL)
8437 {
8438 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8439 struct die_info *child_die;
8440 int nparams, iparams;
8441
8442 /* Count the number of parameters.
8443 FIXME: GDB currently ignores vararg functions, but knows about
8444 vararg member functions. */
8445 nparams = 0;
8446 child_die = die->child;
8447 while (child_die && child_die->tag)
8448 {
8449 if (child_die->tag == DW_TAG_formal_parameter)
8450 nparams++;
8451 else if (child_die->tag == DW_TAG_unspecified_parameters)
8452 TYPE_VARARGS (ftype) = 1;
8453 child_die = sibling_die (child_die);
8454 }
8455
8456 /* Allocate storage for parameters and fill them in. */
8457 TYPE_NFIELDS (ftype) = nparams;
8458 TYPE_FIELDS (ftype) = (struct field *)
8459 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8460
8461 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8462 even if we error out during the parameters reading below. */
8463 for (iparams = 0; iparams < nparams; iparams++)
8464 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8465
8466 iparams = 0;
8467 child_die = die->child;
8468 while (child_die && child_die->tag)
8469 {
8470 if (child_die->tag == DW_TAG_formal_parameter)
8471 {
8472 struct type *arg_type;
8473
8474 /* DWARF version 2 has no clean way to discern C++
8475 static and non-static member functions. G++ helps
8476 GDB by marking the first parameter for non-static
8477 member functions (which is the this pointer) as
8478 artificial. We pass this information to
8479 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8480
8481 DWARF version 3 added DW_AT_object_pointer, which GCC
8482 4.5 does not yet generate. */
8483 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8484 if (attr)
8485 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8486 else
8487 {
8488 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8489
8490 /* GCC/43521: In java, the formal parameter
8491 "this" is sometimes not marked with DW_AT_artificial. */
8492 if (cu->language == language_java)
8493 {
8494 const char *name = dwarf2_name (child_die, cu);
8495
8496 if (name && !strcmp (name, "this"))
8497 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8498 }
8499 }
8500 arg_type = die_type (child_die, cu);
8501
8502 /* RealView does not mark THIS as const, which the testsuite
8503 expects. GCC marks THIS as const in method definitions,
8504 but not in the class specifications (GCC PR 43053). */
8505 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8506 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8507 {
8508 int is_this = 0;
8509 struct dwarf2_cu *arg_cu = cu;
8510 const char *name = dwarf2_name (child_die, cu);
8511
8512 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8513 if (attr)
8514 {
8515 /* If the compiler emits this, use it. */
8516 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8517 is_this = 1;
8518 }
8519 else if (name && strcmp (name, "this") == 0)
8520 /* Function definitions will have the argument names. */
8521 is_this = 1;
8522 else if (name == NULL && iparams == 0)
8523 /* Declarations may not have the names, so like
8524 elsewhere in GDB, assume an artificial first
8525 argument is "this". */
8526 is_this = 1;
8527
8528 if (is_this)
8529 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8530 arg_type, 0);
8531 }
8532
8533 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8534 iparams++;
8535 }
8536 child_die = sibling_die (child_die);
8537 }
8538 }
8539
8540 return ftype;
8541 }
8542
8543 static struct type *
8544 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8545 {
8546 struct objfile *objfile = cu->objfile;
8547 const char *name = NULL;
8548 struct type *this_type;
8549
8550 name = dwarf2_full_name (NULL, die, cu);
8551 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8552 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8553 TYPE_NAME (this_type) = (char *) name;
8554 set_die_type (die, this_type, cu);
8555 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8556 return this_type;
8557 }
8558
8559 /* Find a representation of a given base type and install
8560 it in the TYPE field of the die. */
8561
8562 static struct type *
8563 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8564 {
8565 struct objfile *objfile = cu->objfile;
8566 struct type *type;
8567 struct attribute *attr;
8568 int encoding = 0, size = 0;
8569 char *name;
8570 enum type_code code = TYPE_CODE_INT;
8571 int type_flags = 0;
8572 struct type *target_type = NULL;
8573
8574 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8575 if (attr)
8576 {
8577 encoding = DW_UNSND (attr);
8578 }
8579 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8580 if (attr)
8581 {
8582 size = DW_UNSND (attr);
8583 }
8584 name = dwarf2_name (die, cu);
8585 if (!name)
8586 {
8587 complaint (&symfile_complaints,
8588 _("DW_AT_name missing from DW_TAG_base_type"));
8589 }
8590
8591 switch (encoding)
8592 {
8593 case DW_ATE_address:
8594 /* Turn DW_ATE_address into a void * pointer. */
8595 code = TYPE_CODE_PTR;
8596 type_flags |= TYPE_FLAG_UNSIGNED;
8597 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8598 break;
8599 case DW_ATE_boolean:
8600 code = TYPE_CODE_BOOL;
8601 type_flags |= TYPE_FLAG_UNSIGNED;
8602 break;
8603 case DW_ATE_complex_float:
8604 code = TYPE_CODE_COMPLEX;
8605 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8606 break;
8607 case DW_ATE_decimal_float:
8608 code = TYPE_CODE_DECFLOAT;
8609 break;
8610 case DW_ATE_float:
8611 code = TYPE_CODE_FLT;
8612 break;
8613 case DW_ATE_signed:
8614 break;
8615 case DW_ATE_unsigned:
8616 type_flags |= TYPE_FLAG_UNSIGNED;
8617 if (cu->language == language_fortran
8618 && name
8619 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8620 code = TYPE_CODE_CHAR;
8621 break;
8622 case DW_ATE_signed_char:
8623 if (cu->language == language_ada || cu->language == language_m2
8624 || cu->language == language_pascal
8625 || cu->language == language_fortran)
8626 code = TYPE_CODE_CHAR;
8627 break;
8628 case DW_ATE_unsigned_char:
8629 if (cu->language == language_ada || cu->language == language_m2
8630 || cu->language == language_pascal
8631 || cu->language == language_fortran)
8632 code = TYPE_CODE_CHAR;
8633 type_flags |= TYPE_FLAG_UNSIGNED;
8634 break;
8635 case DW_ATE_UTF:
8636 /* We just treat this as an integer and then recognize the
8637 type by name elsewhere. */
8638 break;
8639
8640 default:
8641 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8642 dwarf_type_encoding_name (encoding));
8643 break;
8644 }
8645
8646 type = init_type (code, size, type_flags, NULL, objfile);
8647 TYPE_NAME (type) = name;
8648 TYPE_TARGET_TYPE (type) = target_type;
8649
8650 if (name && strcmp (name, "char") == 0)
8651 TYPE_NOSIGN (type) = 1;
8652
8653 return set_die_type (die, type, cu);
8654 }
8655
8656 /* Read the given DW_AT_subrange DIE. */
8657
8658 static struct type *
8659 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8660 {
8661 struct type *base_type;
8662 struct type *range_type;
8663 struct attribute *attr;
8664 LONGEST low = 0;
8665 LONGEST high = -1;
8666 char *name;
8667 LONGEST negative_mask;
8668
8669 base_type = die_type (die, cu);
8670 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8671 check_typedef (base_type);
8672
8673 /* The die_type call above may have already set the type for this DIE. */
8674 range_type = get_die_type (die, cu);
8675 if (range_type)
8676 return range_type;
8677
8678 if (cu->language == language_fortran)
8679 {
8680 /* FORTRAN implies a lower bound of 1, if not given. */
8681 low = 1;
8682 }
8683
8684 /* FIXME: For variable sized arrays either of these could be
8685 a variable rather than a constant value. We'll allow it,
8686 but we don't know how to handle it. */
8687 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8688 if (attr)
8689 low = dwarf2_get_attr_constant_value (attr, 0);
8690
8691 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8692 if (attr)
8693 {
8694 if (attr_form_is_block (attr) || is_ref_attr (attr))
8695 {
8696 /* GCC encodes arrays with unspecified or dynamic length
8697 with a DW_FORM_block1 attribute or a reference attribute.
8698 FIXME: GDB does not yet know how to handle dynamic
8699 arrays properly, treat them as arrays with unspecified
8700 length for now.
8701
8702 FIXME: jimb/2003-09-22: GDB does not really know
8703 how to handle arrays of unspecified length
8704 either; we just represent them as zero-length
8705 arrays. Choose an appropriate upper bound given
8706 the lower bound we've computed above. */
8707 high = low - 1;
8708 }
8709 else
8710 high = dwarf2_get_attr_constant_value (attr, 1);
8711 }
8712 else
8713 {
8714 attr = dwarf2_attr (die, DW_AT_count, cu);
8715 if (attr)
8716 {
8717 int count = dwarf2_get_attr_constant_value (attr, 1);
8718 high = low + count - 1;
8719 }
8720 else
8721 {
8722 /* Unspecified array length. */
8723 high = low - 1;
8724 }
8725 }
8726
8727 /* Dwarf-2 specifications explicitly allows to create subrange types
8728 without specifying a base type.
8729 In that case, the base type must be set to the type of
8730 the lower bound, upper bound or count, in that order, if any of these
8731 three attributes references an object that has a type.
8732 If no base type is found, the Dwarf-2 specifications say that
8733 a signed integer type of size equal to the size of an address should
8734 be used.
8735 For the following C code: `extern char gdb_int [];'
8736 GCC produces an empty range DIE.
8737 FIXME: muller/2010-05-28: Possible references to object for low bound,
8738 high bound or count are not yet handled by this code. */
8739 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8740 {
8741 struct objfile *objfile = cu->objfile;
8742 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8743 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8744 struct type *int_type = objfile_type (objfile)->builtin_int;
8745
8746 /* Test "int", "long int", and "long long int" objfile types,
8747 and select the first one having a size above or equal to the
8748 architecture address size. */
8749 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8750 base_type = int_type;
8751 else
8752 {
8753 int_type = objfile_type (objfile)->builtin_long;
8754 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8755 base_type = int_type;
8756 else
8757 {
8758 int_type = objfile_type (objfile)->builtin_long_long;
8759 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8760 base_type = int_type;
8761 }
8762 }
8763 }
8764
8765 negative_mask =
8766 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8767 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8768 low |= negative_mask;
8769 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8770 high |= negative_mask;
8771
8772 range_type = create_range_type (NULL, base_type, low, high);
8773
8774 /* Mark arrays with dynamic length at least as an array of unspecified
8775 length. GDB could check the boundary but before it gets implemented at
8776 least allow accessing the array elements. */
8777 if (attr && attr_form_is_block (attr))
8778 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8779
8780 /* Ada expects an empty array on no boundary attributes. */
8781 if (attr == NULL && cu->language != language_ada)
8782 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8783
8784 name = dwarf2_name (die, cu);
8785 if (name)
8786 TYPE_NAME (range_type) = name;
8787
8788 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8789 if (attr)
8790 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8791
8792 set_die_type (die, range_type, cu);
8793
8794 /* set_die_type should be already done. */
8795 set_descriptive_type (range_type, die, cu);
8796
8797 return range_type;
8798 }
8799
8800 static struct type *
8801 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8802 {
8803 struct type *type;
8804
8805 /* For now, we only support the C meaning of an unspecified type: void. */
8806
8807 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8808 TYPE_NAME (type) = dwarf2_name (die, cu);
8809
8810 return set_die_type (die, type, cu);
8811 }
8812
8813 /* Trivial hash function for die_info: the hash value of a DIE
8814 is its offset in .debug_info for this objfile. */
8815
8816 static hashval_t
8817 die_hash (const void *item)
8818 {
8819 const struct die_info *die = item;
8820
8821 return die->offset;
8822 }
8823
8824 /* Trivial comparison function for die_info structures: two DIEs
8825 are equal if they have the same offset. */
8826
8827 static int
8828 die_eq (const void *item_lhs, const void *item_rhs)
8829 {
8830 const struct die_info *die_lhs = item_lhs;
8831 const struct die_info *die_rhs = item_rhs;
8832
8833 return die_lhs->offset == die_rhs->offset;
8834 }
8835
8836 /* Read a whole compilation unit into a linked list of dies. */
8837
8838 static struct die_info *
8839 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8840 {
8841 struct die_reader_specs reader_specs;
8842 int read_abbrevs = 0;
8843 struct cleanup *back_to = NULL;
8844 struct die_info *die;
8845
8846 if (cu->dwarf2_abbrevs == NULL)
8847 {
8848 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8849 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8850 read_abbrevs = 1;
8851 }
8852
8853 gdb_assert (cu->die_hash == NULL);
8854 cu->die_hash
8855 = htab_create_alloc_ex (cu->header.length / 12,
8856 die_hash,
8857 die_eq,
8858 NULL,
8859 &cu->comp_unit_obstack,
8860 hashtab_obstack_allocate,
8861 dummy_obstack_deallocate);
8862
8863 init_cu_die_reader (&reader_specs, cu);
8864
8865 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8866
8867 if (read_abbrevs)
8868 do_cleanups (back_to);
8869
8870 return die;
8871 }
8872
8873 /* Main entry point for reading a DIE and all children.
8874 Read the DIE and dump it if requested. */
8875
8876 static struct die_info *
8877 read_die_and_children (const struct die_reader_specs *reader,
8878 gdb_byte *info_ptr,
8879 gdb_byte **new_info_ptr,
8880 struct die_info *parent)
8881 {
8882 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8883 new_info_ptr, parent);
8884
8885 if (dwarf2_die_debug)
8886 {
8887 fprintf_unfiltered (gdb_stdlog,
8888 "\nRead die from %s of %s:\n",
8889 (reader->cu->per_cu->debug_type_section
8890 ? ".debug_types"
8891 : ".debug_info"),
8892 reader->abfd->filename);
8893 dump_die (result, dwarf2_die_debug);
8894 }
8895
8896 return result;
8897 }
8898
8899 /* Read a single die and all its descendents. Set the die's sibling
8900 field to NULL; set other fields in the die correctly, and set all
8901 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8902 location of the info_ptr after reading all of those dies. PARENT
8903 is the parent of the die in question. */
8904
8905 static struct die_info *
8906 read_die_and_children_1 (const struct die_reader_specs *reader,
8907 gdb_byte *info_ptr,
8908 gdb_byte **new_info_ptr,
8909 struct die_info *parent)
8910 {
8911 struct die_info *die;
8912 gdb_byte *cur_ptr;
8913 int has_children;
8914
8915 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8916 if (die == NULL)
8917 {
8918 *new_info_ptr = cur_ptr;
8919 return NULL;
8920 }
8921 store_in_ref_table (die, reader->cu);
8922
8923 if (has_children)
8924 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8925 else
8926 {
8927 die->child = NULL;
8928 *new_info_ptr = cur_ptr;
8929 }
8930
8931 die->sibling = NULL;
8932 die->parent = parent;
8933 return die;
8934 }
8935
8936 /* Read a die, all of its descendents, and all of its siblings; set
8937 all of the fields of all of the dies correctly. Arguments are as
8938 in read_die_and_children. */
8939
8940 static struct die_info *
8941 read_die_and_siblings (const struct die_reader_specs *reader,
8942 gdb_byte *info_ptr,
8943 gdb_byte **new_info_ptr,
8944 struct die_info *parent)
8945 {
8946 struct die_info *first_die, *last_sibling;
8947 gdb_byte *cur_ptr;
8948
8949 cur_ptr = info_ptr;
8950 first_die = last_sibling = NULL;
8951
8952 while (1)
8953 {
8954 struct die_info *die
8955 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8956
8957 if (die == NULL)
8958 {
8959 *new_info_ptr = cur_ptr;
8960 return first_die;
8961 }
8962
8963 if (!first_die)
8964 first_die = die;
8965 else
8966 last_sibling->sibling = die;
8967
8968 last_sibling = die;
8969 }
8970 }
8971
8972 /* Read the die from the .debug_info section buffer. Set DIEP to
8973 point to a newly allocated die with its information, except for its
8974 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8975 whether the die has children or not. */
8976
8977 static gdb_byte *
8978 read_full_die (const struct die_reader_specs *reader,
8979 struct die_info **diep, gdb_byte *info_ptr,
8980 int *has_children)
8981 {
8982 unsigned int abbrev_number, bytes_read, i, offset;
8983 struct abbrev_info *abbrev;
8984 struct die_info *die;
8985 struct dwarf2_cu *cu = reader->cu;
8986 bfd *abfd = reader->abfd;
8987
8988 offset = info_ptr - reader->buffer;
8989 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8990 info_ptr += bytes_read;
8991 if (!abbrev_number)
8992 {
8993 *diep = NULL;
8994 *has_children = 0;
8995 return info_ptr;
8996 }
8997
8998 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8999 if (!abbrev)
9000 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9001 abbrev_number,
9002 bfd_get_filename (abfd));
9003
9004 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9005 die->offset = offset;
9006 die->tag = abbrev->tag;
9007 die->abbrev = abbrev_number;
9008
9009 die->num_attrs = abbrev->num_attrs;
9010
9011 for (i = 0; i < abbrev->num_attrs; ++i)
9012 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9013 abfd, info_ptr, cu);
9014
9015 *diep = die;
9016 *has_children = abbrev->has_children;
9017 return info_ptr;
9018 }
9019
9020 /* In DWARF version 2, the description of the debugging information is
9021 stored in a separate .debug_abbrev section. Before we read any
9022 dies from a section we read in all abbreviations and install them
9023 in a hash table. This function also sets flags in CU describing
9024 the data found in the abbrev table. */
9025
9026 static void
9027 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
9028 {
9029 struct comp_unit_head *cu_header = &cu->header;
9030 gdb_byte *abbrev_ptr;
9031 struct abbrev_info *cur_abbrev;
9032 unsigned int abbrev_number, bytes_read, abbrev_name;
9033 unsigned int abbrev_form, hash_number;
9034 struct attr_abbrev *cur_attrs;
9035 unsigned int allocated_attrs;
9036
9037 /* Initialize dwarf2 abbrevs. */
9038 obstack_init (&cu->abbrev_obstack);
9039 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9040 (ABBREV_HASH_SIZE
9041 * sizeof (struct abbrev_info *)));
9042 memset (cu->dwarf2_abbrevs, 0,
9043 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9044
9045 dwarf2_read_section (dwarf2_per_objfile->objfile,
9046 &dwarf2_per_objfile->abbrev);
9047 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9048 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9049 abbrev_ptr += bytes_read;
9050
9051 allocated_attrs = ATTR_ALLOC_CHUNK;
9052 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9053
9054 /* Loop until we reach an abbrev number of 0. */
9055 while (abbrev_number)
9056 {
9057 cur_abbrev = dwarf_alloc_abbrev (cu);
9058
9059 /* read in abbrev header */
9060 cur_abbrev->number = abbrev_number;
9061 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9062 abbrev_ptr += bytes_read;
9063 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9064 abbrev_ptr += 1;
9065
9066 if (cur_abbrev->tag == DW_TAG_namespace)
9067 cu->has_namespace_info = 1;
9068
9069 /* now read in declarations */
9070 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9071 abbrev_ptr += bytes_read;
9072 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9073 abbrev_ptr += bytes_read;
9074 while (abbrev_name)
9075 {
9076 if (cur_abbrev->num_attrs == allocated_attrs)
9077 {
9078 allocated_attrs += ATTR_ALLOC_CHUNK;
9079 cur_attrs
9080 = xrealloc (cur_attrs, (allocated_attrs
9081 * sizeof (struct attr_abbrev)));
9082 }
9083
9084 /* Record whether this compilation unit might have
9085 inter-compilation-unit references. If we don't know what form
9086 this attribute will have, then it might potentially be a
9087 DW_FORM_ref_addr, so we conservatively expect inter-CU
9088 references. */
9089
9090 if (abbrev_form == DW_FORM_ref_addr
9091 || abbrev_form == DW_FORM_indirect)
9092 cu->has_form_ref_addr = 1;
9093
9094 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9095 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9096 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9097 abbrev_ptr += bytes_read;
9098 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9099 abbrev_ptr += bytes_read;
9100 }
9101
9102 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9103 (cur_abbrev->num_attrs
9104 * sizeof (struct attr_abbrev)));
9105 memcpy (cur_abbrev->attrs, cur_attrs,
9106 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9107
9108 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9109 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9110 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9111
9112 /* Get next abbreviation.
9113 Under Irix6 the abbreviations for a compilation unit are not
9114 always properly terminated with an abbrev number of 0.
9115 Exit loop if we encounter an abbreviation which we have
9116 already read (which means we are about to read the abbreviations
9117 for the next compile unit) or if the end of the abbreviation
9118 table is reached. */
9119 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9120 >= dwarf2_per_objfile->abbrev.size)
9121 break;
9122 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9123 abbrev_ptr += bytes_read;
9124 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9125 break;
9126 }
9127
9128 xfree (cur_attrs);
9129 }
9130
9131 /* Release the memory used by the abbrev table for a compilation unit. */
9132
9133 static void
9134 dwarf2_free_abbrev_table (void *ptr_to_cu)
9135 {
9136 struct dwarf2_cu *cu = ptr_to_cu;
9137
9138 obstack_free (&cu->abbrev_obstack, NULL);
9139 cu->dwarf2_abbrevs = NULL;
9140 }
9141
9142 /* Lookup an abbrev_info structure in the abbrev hash table. */
9143
9144 static struct abbrev_info *
9145 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9146 {
9147 unsigned int hash_number;
9148 struct abbrev_info *abbrev;
9149
9150 hash_number = number % ABBREV_HASH_SIZE;
9151 abbrev = cu->dwarf2_abbrevs[hash_number];
9152
9153 while (abbrev)
9154 {
9155 if (abbrev->number == number)
9156 return abbrev;
9157 else
9158 abbrev = abbrev->next;
9159 }
9160 return NULL;
9161 }
9162
9163 /* Returns nonzero if TAG represents a type that we might generate a partial
9164 symbol for. */
9165
9166 static int
9167 is_type_tag_for_partial (int tag)
9168 {
9169 switch (tag)
9170 {
9171 #if 0
9172 /* Some types that would be reasonable to generate partial symbols for,
9173 that we don't at present. */
9174 case DW_TAG_array_type:
9175 case DW_TAG_file_type:
9176 case DW_TAG_ptr_to_member_type:
9177 case DW_TAG_set_type:
9178 case DW_TAG_string_type:
9179 case DW_TAG_subroutine_type:
9180 #endif
9181 case DW_TAG_base_type:
9182 case DW_TAG_class_type:
9183 case DW_TAG_interface_type:
9184 case DW_TAG_enumeration_type:
9185 case DW_TAG_structure_type:
9186 case DW_TAG_subrange_type:
9187 case DW_TAG_typedef:
9188 case DW_TAG_union_type:
9189 return 1;
9190 default:
9191 return 0;
9192 }
9193 }
9194
9195 /* Load all DIEs that are interesting for partial symbols into memory. */
9196
9197 static struct partial_die_info *
9198 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9199 int building_psymtab, struct dwarf2_cu *cu)
9200 {
9201 struct partial_die_info *part_die;
9202 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9203 struct abbrev_info *abbrev;
9204 unsigned int bytes_read;
9205 unsigned int load_all = 0;
9206
9207 int nesting_level = 1;
9208
9209 parent_die = NULL;
9210 last_die = NULL;
9211
9212 if (cu->per_cu && cu->per_cu->load_all_dies)
9213 load_all = 1;
9214
9215 cu->partial_dies
9216 = htab_create_alloc_ex (cu->header.length / 12,
9217 partial_die_hash,
9218 partial_die_eq,
9219 NULL,
9220 &cu->comp_unit_obstack,
9221 hashtab_obstack_allocate,
9222 dummy_obstack_deallocate);
9223
9224 part_die = obstack_alloc (&cu->comp_unit_obstack,
9225 sizeof (struct partial_die_info));
9226
9227 while (1)
9228 {
9229 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9230
9231 /* A NULL abbrev means the end of a series of children. */
9232 if (abbrev == NULL)
9233 {
9234 if (--nesting_level == 0)
9235 {
9236 /* PART_DIE was probably the last thing allocated on the
9237 comp_unit_obstack, so we could call obstack_free
9238 here. We don't do that because the waste is small,
9239 and will be cleaned up when we're done with this
9240 compilation unit. This way, we're also more robust
9241 against other users of the comp_unit_obstack. */
9242 return first_die;
9243 }
9244 info_ptr += bytes_read;
9245 last_die = parent_die;
9246 parent_die = parent_die->die_parent;
9247 continue;
9248 }
9249
9250 /* Check for template arguments. We never save these; if
9251 they're seen, we just mark the parent, and go on our way. */
9252 if (parent_die != NULL
9253 && cu->language == language_cplus
9254 && (abbrev->tag == DW_TAG_template_type_param
9255 || abbrev->tag == DW_TAG_template_value_param))
9256 {
9257 parent_die->has_template_arguments = 1;
9258
9259 if (!load_all)
9260 {
9261 /* We don't need a partial DIE for the template argument. */
9262 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9263 cu);
9264 continue;
9265 }
9266 }
9267
9268 /* We only recurse into subprograms looking for template arguments.
9269 Skip their other children. */
9270 if (!load_all
9271 && cu->language == language_cplus
9272 && parent_die != NULL
9273 && parent_die->tag == DW_TAG_subprogram)
9274 {
9275 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9276 continue;
9277 }
9278
9279 /* Check whether this DIE is interesting enough to save. Normally
9280 we would not be interested in members here, but there may be
9281 later variables referencing them via DW_AT_specification (for
9282 static members). */
9283 if (!load_all
9284 && !is_type_tag_for_partial (abbrev->tag)
9285 && abbrev->tag != DW_TAG_constant
9286 && abbrev->tag != DW_TAG_enumerator
9287 && abbrev->tag != DW_TAG_subprogram
9288 && abbrev->tag != DW_TAG_lexical_block
9289 && abbrev->tag != DW_TAG_variable
9290 && abbrev->tag != DW_TAG_namespace
9291 && abbrev->tag != DW_TAG_module
9292 && abbrev->tag != DW_TAG_member)
9293 {
9294 /* Otherwise we skip to the next sibling, if any. */
9295 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9296 continue;
9297 }
9298
9299 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9300 buffer, info_ptr, cu);
9301
9302 /* This two-pass algorithm for processing partial symbols has a
9303 high cost in cache pressure. Thus, handle some simple cases
9304 here which cover the majority of C partial symbols. DIEs
9305 which neither have specification tags in them, nor could have
9306 specification tags elsewhere pointing at them, can simply be
9307 processed and discarded.
9308
9309 This segment is also optional; scan_partial_symbols and
9310 add_partial_symbol will handle these DIEs if we chain
9311 them in normally. When compilers which do not emit large
9312 quantities of duplicate debug information are more common,
9313 this code can probably be removed. */
9314
9315 /* Any complete simple types at the top level (pretty much all
9316 of them, for a language without namespaces), can be processed
9317 directly. */
9318 if (parent_die == NULL
9319 && part_die->has_specification == 0
9320 && part_die->is_declaration == 0
9321 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9322 || part_die->tag == DW_TAG_base_type
9323 || part_die->tag == DW_TAG_subrange_type))
9324 {
9325 if (building_psymtab && part_die->name != NULL)
9326 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9327 VAR_DOMAIN, LOC_TYPEDEF,
9328 &cu->objfile->static_psymbols,
9329 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9330 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9331 continue;
9332 }
9333
9334 /* The exception for DW_TAG_typedef with has_children above is
9335 a workaround of GCC PR debug/47510. In the case of this complaint
9336 type_name_no_tag_or_error will error on such types later.
9337
9338 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9339 it could not find the child DIEs referenced later, this is checked
9340 above. In correct DWARF DW_TAG_typedef should have no children. */
9341
9342 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9343 complaint (&symfile_complaints,
9344 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9345 "- DIE at 0x%x [in module %s]"),
9346 part_die->offset, cu->objfile->name);
9347
9348 /* If we're at the second level, and we're an enumerator, and
9349 our parent has no specification (meaning possibly lives in a
9350 namespace elsewhere), then we can add the partial symbol now
9351 instead of queueing it. */
9352 if (part_die->tag == DW_TAG_enumerator
9353 && parent_die != NULL
9354 && parent_die->die_parent == NULL
9355 && parent_die->tag == DW_TAG_enumeration_type
9356 && parent_die->has_specification == 0)
9357 {
9358 if (part_die->name == NULL)
9359 complaint (&symfile_complaints,
9360 _("malformed enumerator DIE ignored"));
9361 else if (building_psymtab)
9362 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9363 VAR_DOMAIN, LOC_CONST,
9364 (cu->language == language_cplus
9365 || cu->language == language_java)
9366 ? &cu->objfile->global_psymbols
9367 : &cu->objfile->static_psymbols,
9368 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9369
9370 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9371 continue;
9372 }
9373
9374 /* We'll save this DIE so link it in. */
9375 part_die->die_parent = parent_die;
9376 part_die->die_sibling = NULL;
9377 part_die->die_child = NULL;
9378
9379 if (last_die && last_die == parent_die)
9380 last_die->die_child = part_die;
9381 else if (last_die)
9382 last_die->die_sibling = part_die;
9383
9384 last_die = part_die;
9385
9386 if (first_die == NULL)
9387 first_die = part_die;
9388
9389 /* Maybe add the DIE to the hash table. Not all DIEs that we
9390 find interesting need to be in the hash table, because we
9391 also have the parent/sibling/child chains; only those that we
9392 might refer to by offset later during partial symbol reading.
9393
9394 For now this means things that might have be the target of a
9395 DW_AT_specification, DW_AT_abstract_origin, or
9396 DW_AT_extension. DW_AT_extension will refer only to
9397 namespaces; DW_AT_abstract_origin refers to functions (and
9398 many things under the function DIE, but we do not recurse
9399 into function DIEs during partial symbol reading) and
9400 possibly variables as well; DW_AT_specification refers to
9401 declarations. Declarations ought to have the DW_AT_declaration
9402 flag. It happens that GCC forgets to put it in sometimes, but
9403 only for functions, not for types.
9404
9405 Adding more things than necessary to the hash table is harmless
9406 except for the performance cost. Adding too few will result in
9407 wasted time in find_partial_die, when we reread the compilation
9408 unit with load_all_dies set. */
9409
9410 if (load_all
9411 || abbrev->tag == DW_TAG_constant
9412 || abbrev->tag == DW_TAG_subprogram
9413 || abbrev->tag == DW_TAG_variable
9414 || abbrev->tag == DW_TAG_namespace
9415 || part_die->is_declaration)
9416 {
9417 void **slot;
9418
9419 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9420 part_die->offset, INSERT);
9421 *slot = part_die;
9422 }
9423
9424 part_die = obstack_alloc (&cu->comp_unit_obstack,
9425 sizeof (struct partial_die_info));
9426
9427 /* For some DIEs we want to follow their children (if any). For C
9428 we have no reason to follow the children of structures; for other
9429 languages we have to, so that we can get at method physnames
9430 to infer fully qualified class names, for DW_AT_specification,
9431 and for C++ template arguments. For C++, we also look one level
9432 inside functions to find template arguments (if the name of the
9433 function does not already contain the template arguments).
9434
9435 For Ada, we need to scan the children of subprograms and lexical
9436 blocks as well because Ada allows the definition of nested
9437 entities that could be interesting for the debugger, such as
9438 nested subprograms for instance. */
9439 if (last_die->has_children
9440 && (load_all
9441 || last_die->tag == DW_TAG_namespace
9442 || last_die->tag == DW_TAG_module
9443 || last_die->tag == DW_TAG_enumeration_type
9444 || (cu->language == language_cplus
9445 && last_die->tag == DW_TAG_subprogram
9446 && (last_die->name == NULL
9447 || strchr (last_die->name, '<') == NULL))
9448 || (cu->language != language_c
9449 && (last_die->tag == DW_TAG_class_type
9450 || last_die->tag == DW_TAG_interface_type
9451 || last_die->tag == DW_TAG_structure_type
9452 || last_die->tag == DW_TAG_union_type))
9453 || (cu->language == language_ada
9454 && (last_die->tag == DW_TAG_subprogram
9455 || last_die->tag == DW_TAG_lexical_block))))
9456 {
9457 nesting_level++;
9458 parent_die = last_die;
9459 continue;
9460 }
9461
9462 /* Otherwise we skip to the next sibling, if any. */
9463 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9464
9465 /* Back to the top, do it again. */
9466 }
9467 }
9468
9469 /* Read a minimal amount of information into the minimal die structure. */
9470
9471 static gdb_byte *
9472 read_partial_die (struct partial_die_info *part_die,
9473 struct abbrev_info *abbrev,
9474 unsigned int abbrev_len, bfd *abfd,
9475 gdb_byte *buffer, gdb_byte *info_ptr,
9476 struct dwarf2_cu *cu)
9477 {
9478 unsigned int i;
9479 struct attribute attr;
9480 int has_low_pc_attr = 0;
9481 int has_high_pc_attr = 0;
9482
9483 memset (part_die, 0, sizeof (struct partial_die_info));
9484
9485 part_die->offset = info_ptr - buffer;
9486
9487 info_ptr += abbrev_len;
9488
9489 if (abbrev == NULL)
9490 return info_ptr;
9491
9492 part_die->tag = abbrev->tag;
9493 part_die->has_children = abbrev->has_children;
9494
9495 for (i = 0; i < abbrev->num_attrs; ++i)
9496 {
9497 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9498
9499 /* Store the data if it is of an attribute we want to keep in a
9500 partial symbol table. */
9501 switch (attr.name)
9502 {
9503 case DW_AT_name:
9504 switch (part_die->tag)
9505 {
9506 case DW_TAG_compile_unit:
9507 case DW_TAG_type_unit:
9508 /* Compilation units have a DW_AT_name that is a filename, not
9509 a source language identifier. */
9510 case DW_TAG_enumeration_type:
9511 case DW_TAG_enumerator:
9512 /* These tags always have simple identifiers already; no need
9513 to canonicalize them. */
9514 part_die->name = DW_STRING (&attr);
9515 break;
9516 default:
9517 part_die->name
9518 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9519 &cu->objfile->objfile_obstack);
9520 break;
9521 }
9522 break;
9523 case DW_AT_linkage_name:
9524 case DW_AT_MIPS_linkage_name:
9525 /* Note that both forms of linkage name might appear. We
9526 assume they will be the same, and we only store the last
9527 one we see. */
9528 if (cu->language == language_ada)
9529 part_die->name = DW_STRING (&attr);
9530 part_die->linkage_name = DW_STRING (&attr);
9531 break;
9532 case DW_AT_low_pc:
9533 has_low_pc_attr = 1;
9534 part_die->lowpc = DW_ADDR (&attr);
9535 break;
9536 case DW_AT_high_pc:
9537 has_high_pc_attr = 1;
9538 part_die->highpc = DW_ADDR (&attr);
9539 break;
9540 case DW_AT_location:
9541 /* Support the .debug_loc offsets. */
9542 if (attr_form_is_block (&attr))
9543 {
9544 part_die->locdesc = DW_BLOCK (&attr);
9545 }
9546 else if (attr_form_is_section_offset (&attr))
9547 {
9548 dwarf2_complex_location_expr_complaint ();
9549 }
9550 else
9551 {
9552 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9553 "partial symbol information");
9554 }
9555 break;
9556 case DW_AT_external:
9557 part_die->is_external = DW_UNSND (&attr);
9558 break;
9559 case DW_AT_declaration:
9560 part_die->is_declaration = DW_UNSND (&attr);
9561 break;
9562 case DW_AT_type:
9563 part_die->has_type = 1;
9564 break;
9565 case DW_AT_abstract_origin:
9566 case DW_AT_specification:
9567 case DW_AT_extension:
9568 part_die->has_specification = 1;
9569 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9570 break;
9571 case DW_AT_sibling:
9572 /* Ignore absolute siblings, they might point outside of
9573 the current compile unit. */
9574 if (attr.form == DW_FORM_ref_addr)
9575 complaint (&symfile_complaints,
9576 _("ignoring absolute DW_AT_sibling"));
9577 else
9578 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9579 break;
9580 case DW_AT_byte_size:
9581 part_die->has_byte_size = 1;
9582 break;
9583 case DW_AT_calling_convention:
9584 /* DWARF doesn't provide a way to identify a program's source-level
9585 entry point. DW_AT_calling_convention attributes are only meant
9586 to describe functions' calling conventions.
9587
9588 However, because it's a necessary piece of information in
9589 Fortran, and because DW_CC_program is the only piece of debugging
9590 information whose definition refers to a 'main program' at all,
9591 several compilers have begun marking Fortran main programs with
9592 DW_CC_program --- even when those functions use the standard
9593 calling conventions.
9594
9595 So until DWARF specifies a way to provide this information and
9596 compilers pick up the new representation, we'll support this
9597 practice. */
9598 if (DW_UNSND (&attr) == DW_CC_program
9599 && cu->language == language_fortran)
9600 {
9601 set_main_name (part_die->name);
9602
9603 /* As this DIE has a static linkage the name would be difficult
9604 to look up later. */
9605 language_of_main = language_fortran;
9606 }
9607 break;
9608 default:
9609 break;
9610 }
9611 }
9612
9613 if (has_low_pc_attr && has_high_pc_attr)
9614 {
9615 /* When using the GNU linker, .gnu.linkonce. sections are used to
9616 eliminate duplicate copies of functions and vtables and such.
9617 The linker will arbitrarily choose one and discard the others.
9618 The AT_*_pc values for such functions refer to local labels in
9619 these sections. If the section from that file was discarded, the
9620 labels are not in the output, so the relocs get a value of 0.
9621 If this is a discarded function, mark the pc bounds as invalid,
9622 so that GDB will ignore it. */
9623 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9624 {
9625 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9626
9627 complaint (&symfile_complaints,
9628 _("DW_AT_low_pc %s is zero "
9629 "for DIE at 0x%x [in module %s]"),
9630 paddress (gdbarch, part_die->lowpc),
9631 part_die->offset, cu->objfile->name);
9632 }
9633 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9634 else if (part_die->lowpc >= part_die->highpc)
9635 {
9636 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9637
9638 complaint (&symfile_complaints,
9639 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9640 "for DIE at 0x%x [in module %s]"),
9641 paddress (gdbarch, part_die->lowpc),
9642 paddress (gdbarch, part_die->highpc),
9643 part_die->offset, cu->objfile->name);
9644 }
9645 else
9646 part_die->has_pc_info = 1;
9647 }
9648
9649 return info_ptr;
9650 }
9651
9652 /* Find a cached partial DIE at OFFSET in CU. */
9653
9654 static struct partial_die_info *
9655 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9656 {
9657 struct partial_die_info *lookup_die = NULL;
9658 struct partial_die_info part_die;
9659
9660 part_die.offset = offset;
9661 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9662
9663 return lookup_die;
9664 }
9665
9666 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9667 except in the case of .debug_types DIEs which do not reference
9668 outside their CU (they do however referencing other types via
9669 DW_FORM_ref_sig8). */
9670
9671 static struct partial_die_info *
9672 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9673 {
9674 struct dwarf2_per_cu_data *per_cu = NULL;
9675 struct partial_die_info *pd = NULL;
9676
9677 if (cu->per_cu->debug_type_section)
9678 {
9679 pd = find_partial_die_in_comp_unit (offset, cu);
9680 if (pd != NULL)
9681 return pd;
9682 goto not_found;
9683 }
9684
9685 if (offset_in_cu_p (&cu->header, offset))
9686 {
9687 pd = find_partial_die_in_comp_unit (offset, cu);
9688 if (pd != NULL)
9689 return pd;
9690 }
9691
9692 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9693
9694 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9695 load_partial_comp_unit (per_cu, cu->objfile);
9696
9697 per_cu->cu->last_used = 0;
9698 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9699
9700 if (pd == NULL && per_cu->load_all_dies == 0)
9701 {
9702 struct cleanup *back_to;
9703 struct partial_die_info comp_unit_die;
9704 struct abbrev_info *abbrev;
9705 unsigned int bytes_read;
9706 char *info_ptr;
9707
9708 per_cu->load_all_dies = 1;
9709
9710 /* Re-read the DIEs. */
9711 back_to = make_cleanup (null_cleanup, 0);
9712 if (per_cu->cu->dwarf2_abbrevs == NULL)
9713 {
9714 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9715 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9716 }
9717 info_ptr = (dwarf2_per_objfile->info.buffer
9718 + per_cu->cu->header.offset
9719 + per_cu->cu->header.first_die_offset);
9720 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9721 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9722 per_cu->cu->objfile->obfd,
9723 dwarf2_per_objfile->info.buffer, info_ptr,
9724 per_cu->cu);
9725 if (comp_unit_die.has_children)
9726 load_partial_dies (per_cu->cu->objfile->obfd,
9727 dwarf2_per_objfile->info.buffer, info_ptr,
9728 0, per_cu->cu);
9729 do_cleanups (back_to);
9730
9731 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9732 }
9733
9734 not_found:
9735
9736 if (pd == NULL)
9737 internal_error (__FILE__, __LINE__,
9738 _("could not find partial DIE 0x%x "
9739 "in cache [from module %s]\n"),
9740 offset, bfd_get_filename (cu->objfile->obfd));
9741 return pd;
9742 }
9743
9744 /* See if we can figure out if the class lives in a namespace. We do
9745 this by looking for a member function; its demangled name will
9746 contain namespace info, if there is any. */
9747
9748 static void
9749 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9750 struct dwarf2_cu *cu)
9751 {
9752 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9753 what template types look like, because the demangler
9754 frequently doesn't give the same name as the debug info. We
9755 could fix this by only using the demangled name to get the
9756 prefix (but see comment in read_structure_type). */
9757
9758 struct partial_die_info *real_pdi;
9759 struct partial_die_info *child_pdi;
9760
9761 /* If this DIE (this DIE's specification, if any) has a parent, then
9762 we should not do this. We'll prepend the parent's fully qualified
9763 name when we create the partial symbol. */
9764
9765 real_pdi = struct_pdi;
9766 while (real_pdi->has_specification)
9767 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9768
9769 if (real_pdi->die_parent != NULL)
9770 return;
9771
9772 for (child_pdi = struct_pdi->die_child;
9773 child_pdi != NULL;
9774 child_pdi = child_pdi->die_sibling)
9775 {
9776 if (child_pdi->tag == DW_TAG_subprogram
9777 && child_pdi->linkage_name != NULL)
9778 {
9779 char *actual_class_name
9780 = language_class_name_from_physname (cu->language_defn,
9781 child_pdi->linkage_name);
9782 if (actual_class_name != NULL)
9783 {
9784 struct_pdi->name
9785 = obsavestring (actual_class_name,
9786 strlen (actual_class_name),
9787 &cu->objfile->objfile_obstack);
9788 xfree (actual_class_name);
9789 }
9790 break;
9791 }
9792 }
9793 }
9794
9795 /* Adjust PART_DIE before generating a symbol for it. This function
9796 may set the is_external flag or change the DIE's name. */
9797
9798 static void
9799 fixup_partial_die (struct partial_die_info *part_die,
9800 struct dwarf2_cu *cu)
9801 {
9802 /* Once we've fixed up a die, there's no point in doing so again.
9803 This also avoids a memory leak if we were to call
9804 guess_partial_die_structure_name multiple times. */
9805 if (part_die->fixup_called)
9806 return;
9807
9808 /* If we found a reference attribute and the DIE has no name, try
9809 to find a name in the referred to DIE. */
9810
9811 if (part_die->name == NULL && part_die->has_specification)
9812 {
9813 struct partial_die_info *spec_die;
9814
9815 spec_die = find_partial_die (part_die->spec_offset, cu);
9816
9817 fixup_partial_die (spec_die, cu);
9818
9819 if (spec_die->name)
9820 {
9821 part_die->name = spec_die->name;
9822
9823 /* Copy DW_AT_external attribute if it is set. */
9824 if (spec_die->is_external)
9825 part_die->is_external = spec_die->is_external;
9826 }
9827 }
9828
9829 /* Set default names for some unnamed DIEs. */
9830
9831 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9832 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
9833
9834 /* If there is no parent die to provide a namespace, and there are
9835 children, see if we can determine the namespace from their linkage
9836 name.
9837 NOTE: We need to do this even if cu->has_namespace_info != 0.
9838 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9839 if (cu->language == language_cplus
9840 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
9841 && part_die->die_parent == NULL
9842 && part_die->has_children
9843 && (part_die->tag == DW_TAG_class_type
9844 || part_die->tag == DW_TAG_structure_type
9845 || part_die->tag == DW_TAG_union_type))
9846 guess_partial_die_structure_name (part_die, cu);
9847
9848 /* GCC might emit a nameless struct or union that has a linkage
9849 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9850 if (part_die->name == NULL
9851 && (part_die->tag == DW_TAG_structure_type
9852 || part_die->tag == DW_TAG_union_type
9853 || part_die->tag == DW_TAG_class_type)
9854 && part_die->linkage_name != NULL)
9855 {
9856 char *demangled;
9857
9858 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9859 if (demangled)
9860 {
9861 part_die->name = obsavestring (demangled, strlen (demangled),
9862 &cu->objfile->objfile_obstack);
9863 xfree (demangled);
9864 }
9865 }
9866
9867 part_die->fixup_called = 1;
9868 }
9869
9870 /* Read an attribute value described by an attribute form. */
9871
9872 static gdb_byte *
9873 read_attribute_value (struct attribute *attr, unsigned form,
9874 bfd *abfd, gdb_byte *info_ptr,
9875 struct dwarf2_cu *cu)
9876 {
9877 struct comp_unit_head *cu_header = &cu->header;
9878 unsigned int bytes_read;
9879 struct dwarf_block *blk;
9880
9881 attr->form = form;
9882 switch (form)
9883 {
9884 case DW_FORM_ref_addr:
9885 if (cu->header.version == 2)
9886 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9887 else
9888 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9889 &cu->header, &bytes_read);
9890 info_ptr += bytes_read;
9891 break;
9892 case DW_FORM_addr:
9893 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9894 info_ptr += bytes_read;
9895 break;
9896 case DW_FORM_block2:
9897 blk = dwarf_alloc_block (cu);
9898 blk->size = read_2_bytes (abfd, info_ptr);
9899 info_ptr += 2;
9900 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9901 info_ptr += blk->size;
9902 DW_BLOCK (attr) = blk;
9903 break;
9904 case DW_FORM_block4:
9905 blk = dwarf_alloc_block (cu);
9906 blk->size = read_4_bytes (abfd, info_ptr);
9907 info_ptr += 4;
9908 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9909 info_ptr += blk->size;
9910 DW_BLOCK (attr) = blk;
9911 break;
9912 case DW_FORM_data2:
9913 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9914 info_ptr += 2;
9915 break;
9916 case DW_FORM_data4:
9917 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9918 info_ptr += 4;
9919 break;
9920 case DW_FORM_data8:
9921 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9922 info_ptr += 8;
9923 break;
9924 case DW_FORM_sec_offset:
9925 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9926 info_ptr += bytes_read;
9927 break;
9928 case DW_FORM_string:
9929 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9930 DW_STRING_IS_CANONICAL (attr) = 0;
9931 info_ptr += bytes_read;
9932 break;
9933 case DW_FORM_strp:
9934 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9935 &bytes_read);
9936 DW_STRING_IS_CANONICAL (attr) = 0;
9937 info_ptr += bytes_read;
9938 break;
9939 case DW_FORM_exprloc:
9940 case DW_FORM_block:
9941 blk = dwarf_alloc_block (cu);
9942 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9943 info_ptr += bytes_read;
9944 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9945 info_ptr += blk->size;
9946 DW_BLOCK (attr) = blk;
9947 break;
9948 case DW_FORM_block1:
9949 blk = dwarf_alloc_block (cu);
9950 blk->size = read_1_byte (abfd, info_ptr);
9951 info_ptr += 1;
9952 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9953 info_ptr += blk->size;
9954 DW_BLOCK (attr) = blk;
9955 break;
9956 case DW_FORM_data1:
9957 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9958 info_ptr += 1;
9959 break;
9960 case DW_FORM_flag:
9961 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9962 info_ptr += 1;
9963 break;
9964 case DW_FORM_flag_present:
9965 DW_UNSND (attr) = 1;
9966 break;
9967 case DW_FORM_sdata:
9968 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9969 info_ptr += bytes_read;
9970 break;
9971 case DW_FORM_udata:
9972 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9973 info_ptr += bytes_read;
9974 break;
9975 case DW_FORM_ref1:
9976 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9977 info_ptr += 1;
9978 break;
9979 case DW_FORM_ref2:
9980 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9981 info_ptr += 2;
9982 break;
9983 case DW_FORM_ref4:
9984 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9985 info_ptr += 4;
9986 break;
9987 case DW_FORM_ref8:
9988 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9989 info_ptr += 8;
9990 break;
9991 case DW_FORM_ref_sig8:
9992 /* Convert the signature to something we can record in DW_UNSND
9993 for later lookup.
9994 NOTE: This is NULL if the type wasn't found. */
9995 DW_SIGNATURED_TYPE (attr) =
9996 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9997 info_ptr += 8;
9998 break;
9999 case DW_FORM_ref_udata:
10000 DW_ADDR (attr) = (cu->header.offset
10001 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10002 info_ptr += bytes_read;
10003 break;
10004 case DW_FORM_indirect:
10005 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10006 info_ptr += bytes_read;
10007 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10008 break;
10009 default:
10010 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10011 dwarf_form_name (form),
10012 bfd_get_filename (abfd));
10013 }
10014
10015 /* We have seen instances where the compiler tried to emit a byte
10016 size attribute of -1 which ended up being encoded as an unsigned
10017 0xffffffff. Although 0xffffffff is technically a valid size value,
10018 an object of this size seems pretty unlikely so we can relatively
10019 safely treat these cases as if the size attribute was invalid and
10020 treat them as zero by default. */
10021 if (attr->name == DW_AT_byte_size
10022 && form == DW_FORM_data4
10023 && DW_UNSND (attr) >= 0xffffffff)
10024 {
10025 complaint
10026 (&symfile_complaints,
10027 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10028 hex_string (DW_UNSND (attr)));
10029 DW_UNSND (attr) = 0;
10030 }
10031
10032 return info_ptr;
10033 }
10034
10035 /* Read an attribute described by an abbreviated attribute. */
10036
10037 static gdb_byte *
10038 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10039 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10040 {
10041 attr->name = abbrev->name;
10042 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10043 }
10044
10045 /* Read dwarf information from a buffer. */
10046
10047 static unsigned int
10048 read_1_byte (bfd *abfd, gdb_byte *buf)
10049 {
10050 return bfd_get_8 (abfd, buf);
10051 }
10052
10053 static int
10054 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10055 {
10056 return bfd_get_signed_8 (abfd, buf);
10057 }
10058
10059 static unsigned int
10060 read_2_bytes (bfd *abfd, gdb_byte *buf)
10061 {
10062 return bfd_get_16 (abfd, buf);
10063 }
10064
10065 static unsigned int
10066 read_4_bytes (bfd *abfd, gdb_byte *buf)
10067 {
10068 return bfd_get_32 (abfd, buf);
10069 }
10070
10071 static ULONGEST
10072 read_8_bytes (bfd *abfd, gdb_byte *buf)
10073 {
10074 return bfd_get_64 (abfd, buf);
10075 }
10076
10077 static CORE_ADDR
10078 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10079 unsigned int *bytes_read)
10080 {
10081 struct comp_unit_head *cu_header = &cu->header;
10082 CORE_ADDR retval = 0;
10083
10084 if (cu_header->signed_addr_p)
10085 {
10086 switch (cu_header->addr_size)
10087 {
10088 case 2:
10089 retval = bfd_get_signed_16 (abfd, buf);
10090 break;
10091 case 4:
10092 retval = bfd_get_signed_32 (abfd, buf);
10093 break;
10094 case 8:
10095 retval = bfd_get_signed_64 (abfd, buf);
10096 break;
10097 default:
10098 internal_error (__FILE__, __LINE__,
10099 _("read_address: bad switch, signed [in module %s]"),
10100 bfd_get_filename (abfd));
10101 }
10102 }
10103 else
10104 {
10105 switch (cu_header->addr_size)
10106 {
10107 case 2:
10108 retval = bfd_get_16 (abfd, buf);
10109 break;
10110 case 4:
10111 retval = bfd_get_32 (abfd, buf);
10112 break;
10113 case 8:
10114 retval = bfd_get_64 (abfd, buf);
10115 break;
10116 default:
10117 internal_error (__FILE__, __LINE__,
10118 _("read_address: bad switch, "
10119 "unsigned [in module %s]"),
10120 bfd_get_filename (abfd));
10121 }
10122 }
10123
10124 *bytes_read = cu_header->addr_size;
10125 return retval;
10126 }
10127
10128 /* Read the initial length from a section. The (draft) DWARF 3
10129 specification allows the initial length to take up either 4 bytes
10130 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10131 bytes describe the length and all offsets will be 8 bytes in length
10132 instead of 4.
10133
10134 An older, non-standard 64-bit format is also handled by this
10135 function. The older format in question stores the initial length
10136 as an 8-byte quantity without an escape value. Lengths greater
10137 than 2^32 aren't very common which means that the initial 4 bytes
10138 is almost always zero. Since a length value of zero doesn't make
10139 sense for the 32-bit format, this initial zero can be considered to
10140 be an escape value which indicates the presence of the older 64-bit
10141 format. As written, the code can't detect (old format) lengths
10142 greater than 4GB. If it becomes necessary to handle lengths
10143 somewhat larger than 4GB, we could allow other small values (such
10144 as the non-sensical values of 1, 2, and 3) to also be used as
10145 escape values indicating the presence of the old format.
10146
10147 The value returned via bytes_read should be used to increment the
10148 relevant pointer after calling read_initial_length().
10149
10150 [ Note: read_initial_length() and read_offset() are based on the
10151 document entitled "DWARF Debugging Information Format", revision
10152 3, draft 8, dated November 19, 2001. This document was obtained
10153 from:
10154
10155 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10156
10157 This document is only a draft and is subject to change. (So beware.)
10158
10159 Details regarding the older, non-standard 64-bit format were
10160 determined empirically by examining 64-bit ELF files produced by
10161 the SGI toolchain on an IRIX 6.5 machine.
10162
10163 - Kevin, July 16, 2002
10164 ] */
10165
10166 static LONGEST
10167 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10168 {
10169 LONGEST length = bfd_get_32 (abfd, buf);
10170
10171 if (length == 0xffffffff)
10172 {
10173 length = bfd_get_64 (abfd, buf + 4);
10174 *bytes_read = 12;
10175 }
10176 else if (length == 0)
10177 {
10178 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10179 length = bfd_get_64 (abfd, buf);
10180 *bytes_read = 8;
10181 }
10182 else
10183 {
10184 *bytes_read = 4;
10185 }
10186
10187 return length;
10188 }
10189
10190 /* Cover function for read_initial_length.
10191 Returns the length of the object at BUF, and stores the size of the
10192 initial length in *BYTES_READ and stores the size that offsets will be in
10193 *OFFSET_SIZE.
10194 If the initial length size is not equivalent to that specified in
10195 CU_HEADER then issue a complaint.
10196 This is useful when reading non-comp-unit headers. */
10197
10198 static LONGEST
10199 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10200 const struct comp_unit_head *cu_header,
10201 unsigned int *bytes_read,
10202 unsigned int *offset_size)
10203 {
10204 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10205
10206 gdb_assert (cu_header->initial_length_size == 4
10207 || cu_header->initial_length_size == 8
10208 || cu_header->initial_length_size == 12);
10209
10210 if (cu_header->initial_length_size != *bytes_read)
10211 complaint (&symfile_complaints,
10212 _("intermixed 32-bit and 64-bit DWARF sections"));
10213
10214 *offset_size = (*bytes_read == 4) ? 4 : 8;
10215 return length;
10216 }
10217
10218 /* Read an offset from the data stream. The size of the offset is
10219 given by cu_header->offset_size. */
10220
10221 static LONGEST
10222 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10223 unsigned int *bytes_read)
10224 {
10225 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10226
10227 *bytes_read = cu_header->offset_size;
10228 return offset;
10229 }
10230
10231 /* Read an offset from the data stream. */
10232
10233 static LONGEST
10234 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10235 {
10236 LONGEST retval = 0;
10237
10238 switch (offset_size)
10239 {
10240 case 4:
10241 retval = bfd_get_32 (abfd, buf);
10242 break;
10243 case 8:
10244 retval = bfd_get_64 (abfd, buf);
10245 break;
10246 default:
10247 internal_error (__FILE__, __LINE__,
10248 _("read_offset_1: bad switch [in module %s]"),
10249 bfd_get_filename (abfd));
10250 }
10251
10252 return retval;
10253 }
10254
10255 static gdb_byte *
10256 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10257 {
10258 /* If the size of a host char is 8 bits, we can return a pointer
10259 to the buffer, otherwise we have to copy the data to a buffer
10260 allocated on the temporary obstack. */
10261 gdb_assert (HOST_CHAR_BIT == 8);
10262 return buf;
10263 }
10264
10265 static char *
10266 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10267 {
10268 /* If the size of a host char is 8 bits, we can return a pointer
10269 to the string, otherwise we have to copy the string to a buffer
10270 allocated on the temporary obstack. */
10271 gdb_assert (HOST_CHAR_BIT == 8);
10272 if (*buf == '\0')
10273 {
10274 *bytes_read_ptr = 1;
10275 return NULL;
10276 }
10277 *bytes_read_ptr = strlen ((char *) buf) + 1;
10278 return (char *) buf;
10279 }
10280
10281 static char *
10282 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10283 {
10284 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10285 if (dwarf2_per_objfile->str.buffer == NULL)
10286 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10287 bfd_get_filename (abfd));
10288 if (str_offset >= dwarf2_per_objfile->str.size)
10289 error (_("DW_FORM_strp pointing outside of "
10290 ".debug_str section [in module %s]"),
10291 bfd_get_filename (abfd));
10292 gdb_assert (HOST_CHAR_BIT == 8);
10293 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10294 return NULL;
10295 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10296 }
10297
10298 static char *
10299 read_indirect_string (bfd *abfd, gdb_byte *buf,
10300 const struct comp_unit_head *cu_header,
10301 unsigned int *bytes_read_ptr)
10302 {
10303 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10304
10305 return read_indirect_string_at_offset (abfd, str_offset);
10306 }
10307
10308 static unsigned long
10309 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10310 {
10311 unsigned long result;
10312 unsigned int num_read;
10313 int i, shift;
10314 unsigned char byte;
10315
10316 result = 0;
10317 shift = 0;
10318 num_read = 0;
10319 i = 0;
10320 while (1)
10321 {
10322 byte = bfd_get_8 (abfd, buf);
10323 buf++;
10324 num_read++;
10325 result |= ((unsigned long)(byte & 127) << shift);
10326 if ((byte & 128) == 0)
10327 {
10328 break;
10329 }
10330 shift += 7;
10331 }
10332 *bytes_read_ptr = num_read;
10333 return result;
10334 }
10335
10336 static long
10337 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10338 {
10339 long result;
10340 int i, shift, num_read;
10341 unsigned char byte;
10342
10343 result = 0;
10344 shift = 0;
10345 num_read = 0;
10346 i = 0;
10347 while (1)
10348 {
10349 byte = bfd_get_8 (abfd, buf);
10350 buf++;
10351 num_read++;
10352 result |= ((long)(byte & 127) << shift);
10353 shift += 7;
10354 if ((byte & 128) == 0)
10355 {
10356 break;
10357 }
10358 }
10359 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10360 result |= -(((long)1) << shift);
10361 *bytes_read_ptr = num_read;
10362 return result;
10363 }
10364
10365 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10366
10367 static gdb_byte *
10368 skip_leb128 (bfd *abfd, gdb_byte *buf)
10369 {
10370 int byte;
10371
10372 while (1)
10373 {
10374 byte = bfd_get_8 (abfd, buf);
10375 buf++;
10376 if ((byte & 128) == 0)
10377 return buf;
10378 }
10379 }
10380
10381 static void
10382 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10383 {
10384 switch (lang)
10385 {
10386 case DW_LANG_C89:
10387 case DW_LANG_C99:
10388 case DW_LANG_C:
10389 cu->language = language_c;
10390 break;
10391 case DW_LANG_C_plus_plus:
10392 cu->language = language_cplus;
10393 break;
10394 case DW_LANG_D:
10395 cu->language = language_d;
10396 break;
10397 case DW_LANG_Fortran77:
10398 case DW_LANG_Fortran90:
10399 case DW_LANG_Fortran95:
10400 cu->language = language_fortran;
10401 break;
10402 case DW_LANG_Mips_Assembler:
10403 cu->language = language_asm;
10404 break;
10405 case DW_LANG_Java:
10406 cu->language = language_java;
10407 break;
10408 case DW_LANG_Ada83:
10409 case DW_LANG_Ada95:
10410 cu->language = language_ada;
10411 break;
10412 case DW_LANG_Modula2:
10413 cu->language = language_m2;
10414 break;
10415 case DW_LANG_Pascal83:
10416 cu->language = language_pascal;
10417 break;
10418 case DW_LANG_ObjC:
10419 cu->language = language_objc;
10420 break;
10421 case DW_LANG_Cobol74:
10422 case DW_LANG_Cobol85:
10423 default:
10424 cu->language = language_minimal;
10425 break;
10426 }
10427 cu->language_defn = language_def (cu->language);
10428 }
10429
10430 /* Return the named attribute or NULL if not there. */
10431
10432 static struct attribute *
10433 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10434 {
10435 unsigned int i;
10436 struct attribute *spec = NULL;
10437
10438 for (i = 0; i < die->num_attrs; ++i)
10439 {
10440 if (die->attrs[i].name == name)
10441 return &die->attrs[i];
10442 if (die->attrs[i].name == DW_AT_specification
10443 || die->attrs[i].name == DW_AT_abstract_origin)
10444 spec = &die->attrs[i];
10445 }
10446
10447 if (spec)
10448 {
10449 die = follow_die_ref (die, spec, &cu);
10450 return dwarf2_attr (die, name, cu);
10451 }
10452
10453 return NULL;
10454 }
10455
10456 /* Return the named attribute or NULL if not there,
10457 but do not follow DW_AT_specification, etc.
10458 This is for use in contexts where we're reading .debug_types dies.
10459 Following DW_AT_specification, DW_AT_abstract_origin will take us
10460 back up the chain, and we want to go down. */
10461
10462 static struct attribute *
10463 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10464 struct dwarf2_cu *cu)
10465 {
10466 unsigned int i;
10467
10468 for (i = 0; i < die->num_attrs; ++i)
10469 if (die->attrs[i].name == name)
10470 return &die->attrs[i];
10471
10472 return NULL;
10473 }
10474
10475 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10476 and holds a non-zero value. This function should only be used for
10477 DW_FORM_flag or DW_FORM_flag_present attributes. */
10478
10479 static int
10480 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10481 {
10482 struct attribute *attr = dwarf2_attr (die, name, cu);
10483
10484 return (attr && DW_UNSND (attr));
10485 }
10486
10487 static int
10488 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10489 {
10490 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10491 which value is non-zero. However, we have to be careful with
10492 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10493 (via dwarf2_flag_true_p) follows this attribute. So we may
10494 end up accidently finding a declaration attribute that belongs
10495 to a different DIE referenced by the specification attribute,
10496 even though the given DIE does not have a declaration attribute. */
10497 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10498 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10499 }
10500
10501 /* Return the die giving the specification for DIE, if there is
10502 one. *SPEC_CU is the CU containing DIE on input, and the CU
10503 containing the return value on output. If there is no
10504 specification, but there is an abstract origin, that is
10505 returned. */
10506
10507 static struct die_info *
10508 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10509 {
10510 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10511 *spec_cu);
10512
10513 if (spec_attr == NULL)
10514 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10515
10516 if (spec_attr == NULL)
10517 return NULL;
10518 else
10519 return follow_die_ref (die, spec_attr, spec_cu);
10520 }
10521
10522 /* Free the line_header structure *LH, and any arrays and strings it
10523 refers to.
10524 NOTE: This is also used as a "cleanup" function. */
10525
10526 static void
10527 free_line_header (struct line_header *lh)
10528 {
10529 if (lh->standard_opcode_lengths)
10530 xfree (lh->standard_opcode_lengths);
10531
10532 /* Remember that all the lh->file_names[i].name pointers are
10533 pointers into debug_line_buffer, and don't need to be freed. */
10534 if (lh->file_names)
10535 xfree (lh->file_names);
10536
10537 /* Similarly for the include directory names. */
10538 if (lh->include_dirs)
10539 xfree (lh->include_dirs);
10540
10541 xfree (lh);
10542 }
10543
10544 /* Add an entry to LH's include directory table. */
10545
10546 static void
10547 add_include_dir (struct line_header *lh, char *include_dir)
10548 {
10549 /* Grow the array if necessary. */
10550 if (lh->include_dirs_size == 0)
10551 {
10552 lh->include_dirs_size = 1; /* for testing */
10553 lh->include_dirs = xmalloc (lh->include_dirs_size
10554 * sizeof (*lh->include_dirs));
10555 }
10556 else if (lh->num_include_dirs >= lh->include_dirs_size)
10557 {
10558 lh->include_dirs_size *= 2;
10559 lh->include_dirs = xrealloc (lh->include_dirs,
10560 (lh->include_dirs_size
10561 * sizeof (*lh->include_dirs)));
10562 }
10563
10564 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10565 }
10566
10567 /* Add an entry to LH's file name table. */
10568
10569 static void
10570 add_file_name (struct line_header *lh,
10571 char *name,
10572 unsigned int dir_index,
10573 unsigned int mod_time,
10574 unsigned int length)
10575 {
10576 struct file_entry *fe;
10577
10578 /* Grow the array if necessary. */
10579 if (lh->file_names_size == 0)
10580 {
10581 lh->file_names_size = 1; /* for testing */
10582 lh->file_names = xmalloc (lh->file_names_size
10583 * sizeof (*lh->file_names));
10584 }
10585 else if (lh->num_file_names >= lh->file_names_size)
10586 {
10587 lh->file_names_size *= 2;
10588 lh->file_names = xrealloc (lh->file_names,
10589 (lh->file_names_size
10590 * sizeof (*lh->file_names)));
10591 }
10592
10593 fe = &lh->file_names[lh->num_file_names++];
10594 fe->name = name;
10595 fe->dir_index = dir_index;
10596 fe->mod_time = mod_time;
10597 fe->length = length;
10598 fe->included_p = 0;
10599 fe->symtab = NULL;
10600 }
10601
10602 /* Read the statement program header starting at OFFSET in
10603 .debug_line, according to the endianness of ABFD. Return a pointer
10604 to a struct line_header, allocated using xmalloc.
10605
10606 NOTE: the strings in the include directory and file name tables of
10607 the returned object point into debug_line_buffer, and must not be
10608 freed. */
10609
10610 static struct line_header *
10611 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10612 struct dwarf2_cu *cu)
10613 {
10614 struct cleanup *back_to;
10615 struct line_header *lh;
10616 gdb_byte *line_ptr;
10617 unsigned int bytes_read, offset_size;
10618 int i;
10619 char *cur_dir, *cur_file;
10620
10621 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10622 if (dwarf2_per_objfile->line.buffer == NULL)
10623 {
10624 complaint (&symfile_complaints, _("missing .debug_line section"));
10625 return 0;
10626 }
10627
10628 /* Make sure that at least there's room for the total_length field.
10629 That could be 12 bytes long, but we're just going to fudge that. */
10630 if (offset + 4 >= dwarf2_per_objfile->line.size)
10631 {
10632 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10633 return 0;
10634 }
10635
10636 lh = xmalloc (sizeof (*lh));
10637 memset (lh, 0, sizeof (*lh));
10638 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10639 (void *) lh);
10640
10641 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10642
10643 /* Read in the header. */
10644 lh->total_length =
10645 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10646 &bytes_read, &offset_size);
10647 line_ptr += bytes_read;
10648 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10649 + dwarf2_per_objfile->line.size))
10650 {
10651 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10652 return 0;
10653 }
10654 lh->statement_program_end = line_ptr + lh->total_length;
10655 lh->version = read_2_bytes (abfd, line_ptr);
10656 line_ptr += 2;
10657 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10658 line_ptr += offset_size;
10659 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10660 line_ptr += 1;
10661 if (lh->version >= 4)
10662 {
10663 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10664 line_ptr += 1;
10665 }
10666 else
10667 lh->maximum_ops_per_instruction = 1;
10668
10669 if (lh->maximum_ops_per_instruction == 0)
10670 {
10671 lh->maximum_ops_per_instruction = 1;
10672 complaint (&symfile_complaints,
10673 _("invalid maximum_ops_per_instruction "
10674 "in `.debug_line' section"));
10675 }
10676
10677 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10678 line_ptr += 1;
10679 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10680 line_ptr += 1;
10681 lh->line_range = read_1_byte (abfd, line_ptr);
10682 line_ptr += 1;
10683 lh->opcode_base = read_1_byte (abfd, line_ptr);
10684 line_ptr += 1;
10685 lh->standard_opcode_lengths
10686 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10687
10688 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10689 for (i = 1; i < lh->opcode_base; ++i)
10690 {
10691 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10692 line_ptr += 1;
10693 }
10694
10695 /* Read directory table. */
10696 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10697 {
10698 line_ptr += bytes_read;
10699 add_include_dir (lh, cur_dir);
10700 }
10701 line_ptr += bytes_read;
10702
10703 /* Read file name table. */
10704 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10705 {
10706 unsigned int dir_index, mod_time, length;
10707
10708 line_ptr += bytes_read;
10709 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10710 line_ptr += bytes_read;
10711 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10712 line_ptr += bytes_read;
10713 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10714 line_ptr += bytes_read;
10715
10716 add_file_name (lh, cur_file, dir_index, mod_time, length);
10717 }
10718 line_ptr += bytes_read;
10719 lh->statement_program_start = line_ptr;
10720
10721 if (line_ptr > (dwarf2_per_objfile->line.buffer
10722 + dwarf2_per_objfile->line.size))
10723 complaint (&symfile_complaints,
10724 _("line number info header doesn't "
10725 "fit in `.debug_line' section"));
10726
10727 discard_cleanups (back_to);
10728 return lh;
10729 }
10730
10731 /* This function exists to work around a bug in certain compilers
10732 (particularly GCC 2.95), in which the first line number marker of a
10733 function does not show up until after the prologue, right before
10734 the second line number marker. This function shifts ADDRESS down
10735 to the beginning of the function if necessary, and is called on
10736 addresses passed to record_line. */
10737
10738 static CORE_ADDR
10739 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10740 {
10741 struct function_range *fn;
10742
10743 /* Find the function_range containing address. */
10744 if (!cu->first_fn)
10745 return address;
10746
10747 if (!cu->cached_fn)
10748 cu->cached_fn = cu->first_fn;
10749
10750 fn = cu->cached_fn;
10751 while (fn)
10752 if (fn->lowpc <= address && fn->highpc > address)
10753 goto found;
10754 else
10755 fn = fn->next;
10756
10757 fn = cu->first_fn;
10758 while (fn && fn != cu->cached_fn)
10759 if (fn->lowpc <= address && fn->highpc > address)
10760 goto found;
10761 else
10762 fn = fn->next;
10763
10764 return address;
10765
10766 found:
10767 if (fn->seen_line)
10768 return address;
10769 if (address != fn->lowpc)
10770 complaint (&symfile_complaints,
10771 _("misplaced first line number at 0x%lx for '%s'"),
10772 (unsigned long) address, fn->name);
10773 fn->seen_line = 1;
10774 return fn->lowpc;
10775 }
10776
10777 /* Subroutine of dwarf_decode_lines to simplify it.
10778 Return the file name of the psymtab for included file FILE_INDEX
10779 in line header LH of PST.
10780 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10781 If space for the result is malloc'd, it will be freed by a cleanup.
10782 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10783
10784 static char *
10785 psymtab_include_file_name (const struct line_header *lh, int file_index,
10786 const struct partial_symtab *pst,
10787 const char *comp_dir)
10788 {
10789 const struct file_entry fe = lh->file_names [file_index];
10790 char *include_name = fe.name;
10791 char *include_name_to_compare = include_name;
10792 char *dir_name = NULL;
10793 const char *pst_filename;
10794 char *copied_name = NULL;
10795 int file_is_pst;
10796
10797 if (fe.dir_index)
10798 dir_name = lh->include_dirs[fe.dir_index - 1];
10799
10800 if (!IS_ABSOLUTE_PATH (include_name)
10801 && (dir_name != NULL || comp_dir != NULL))
10802 {
10803 /* Avoid creating a duplicate psymtab for PST.
10804 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10805 Before we do the comparison, however, we need to account
10806 for DIR_NAME and COMP_DIR.
10807 First prepend dir_name (if non-NULL). If we still don't
10808 have an absolute path prepend comp_dir (if non-NULL).
10809 However, the directory we record in the include-file's
10810 psymtab does not contain COMP_DIR (to match the
10811 corresponding symtab(s)).
10812
10813 Example:
10814
10815 bash$ cd /tmp
10816 bash$ gcc -g ./hello.c
10817 include_name = "hello.c"
10818 dir_name = "."
10819 DW_AT_comp_dir = comp_dir = "/tmp"
10820 DW_AT_name = "./hello.c" */
10821
10822 if (dir_name != NULL)
10823 {
10824 include_name = concat (dir_name, SLASH_STRING,
10825 include_name, (char *)NULL);
10826 include_name_to_compare = include_name;
10827 make_cleanup (xfree, include_name);
10828 }
10829 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10830 {
10831 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10832 include_name, (char *)NULL);
10833 }
10834 }
10835
10836 pst_filename = pst->filename;
10837 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10838 {
10839 copied_name = concat (pst->dirname, SLASH_STRING,
10840 pst_filename, (char *)NULL);
10841 pst_filename = copied_name;
10842 }
10843
10844 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10845
10846 if (include_name_to_compare != include_name)
10847 xfree (include_name_to_compare);
10848 if (copied_name != NULL)
10849 xfree (copied_name);
10850
10851 if (file_is_pst)
10852 return NULL;
10853 return include_name;
10854 }
10855
10856 /* Ignore this record_line request. */
10857
10858 static void
10859 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10860 {
10861 return;
10862 }
10863
10864 /* Decode the Line Number Program (LNP) for the given line_header
10865 structure and CU. The actual information extracted and the type
10866 of structures created from the LNP depends on the value of PST.
10867
10868 1. If PST is NULL, then this procedure uses the data from the program
10869 to create all necessary symbol tables, and their linetables.
10870
10871 2. If PST is not NULL, this procedure reads the program to determine
10872 the list of files included by the unit represented by PST, and
10873 builds all the associated partial symbol tables.
10874
10875 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10876 It is used for relative paths in the line table.
10877 NOTE: When processing partial symtabs (pst != NULL),
10878 comp_dir == pst->dirname.
10879
10880 NOTE: It is important that psymtabs have the same file name (via strcmp)
10881 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10882 symtab we don't use it in the name of the psymtabs we create.
10883 E.g. expand_line_sal requires this when finding psymtabs to expand.
10884 A good testcase for this is mb-inline.exp. */
10885
10886 static void
10887 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10888 struct dwarf2_cu *cu, struct partial_symtab *pst)
10889 {
10890 gdb_byte *line_ptr, *extended_end;
10891 gdb_byte *line_end;
10892 unsigned int bytes_read, extended_len;
10893 unsigned char op_code, extended_op, adj_opcode;
10894 CORE_ADDR baseaddr;
10895 struct objfile *objfile = cu->objfile;
10896 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10897 const int decode_for_pst_p = (pst != NULL);
10898 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10899 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10900 = record_line;
10901
10902 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10903
10904 line_ptr = lh->statement_program_start;
10905 line_end = lh->statement_program_end;
10906
10907 /* Read the statement sequences until there's nothing left. */
10908 while (line_ptr < line_end)
10909 {
10910 /* state machine registers */
10911 CORE_ADDR address = 0;
10912 unsigned int file = 1;
10913 unsigned int line = 1;
10914 unsigned int column = 0;
10915 int is_stmt = lh->default_is_stmt;
10916 int basic_block = 0;
10917 int end_sequence = 0;
10918 CORE_ADDR addr;
10919 unsigned char op_index = 0;
10920
10921 if (!decode_for_pst_p && lh->num_file_names >= file)
10922 {
10923 /* Start a subfile for the current file of the state machine. */
10924 /* lh->include_dirs and lh->file_names are 0-based, but the
10925 directory and file name numbers in the statement program
10926 are 1-based. */
10927 struct file_entry *fe = &lh->file_names[file - 1];
10928 char *dir = NULL;
10929
10930 if (fe->dir_index)
10931 dir = lh->include_dirs[fe->dir_index - 1];
10932
10933 dwarf2_start_subfile (fe->name, dir, comp_dir);
10934 }
10935
10936 /* Decode the table. */
10937 while (!end_sequence)
10938 {
10939 op_code = read_1_byte (abfd, line_ptr);
10940 line_ptr += 1;
10941 if (line_ptr > line_end)
10942 {
10943 dwarf2_debug_line_missing_end_sequence_complaint ();
10944 break;
10945 }
10946
10947 if (op_code >= lh->opcode_base)
10948 {
10949 /* Special operand. */
10950 adj_opcode = op_code - lh->opcode_base;
10951 address += (((op_index + (adj_opcode / lh->line_range))
10952 / lh->maximum_ops_per_instruction)
10953 * lh->minimum_instruction_length);
10954 op_index = ((op_index + (adj_opcode / lh->line_range))
10955 % lh->maximum_ops_per_instruction);
10956 line += lh->line_base + (adj_opcode % lh->line_range);
10957 if (lh->num_file_names < file || file == 0)
10958 dwarf2_debug_line_missing_file_complaint ();
10959 /* For now we ignore lines not starting on an
10960 instruction boundary. */
10961 else if (op_index == 0)
10962 {
10963 lh->file_names[file - 1].included_p = 1;
10964 if (!decode_for_pst_p && is_stmt)
10965 {
10966 if (last_subfile != current_subfile)
10967 {
10968 addr = gdbarch_addr_bits_remove (gdbarch, address);
10969 if (last_subfile)
10970 (*p_record_line) (last_subfile, 0, addr);
10971 last_subfile = current_subfile;
10972 }
10973 /* Append row to matrix using current values. */
10974 addr = check_cu_functions (address, cu);
10975 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10976 (*p_record_line) (current_subfile, line, addr);
10977 }
10978 }
10979 basic_block = 0;
10980 }
10981 else switch (op_code)
10982 {
10983 case DW_LNS_extended_op:
10984 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10985 &bytes_read);
10986 line_ptr += bytes_read;
10987 extended_end = line_ptr + extended_len;
10988 extended_op = read_1_byte (abfd, line_ptr);
10989 line_ptr += 1;
10990 switch (extended_op)
10991 {
10992 case DW_LNE_end_sequence:
10993 p_record_line = record_line;
10994 end_sequence = 1;
10995 break;
10996 case DW_LNE_set_address:
10997 address = read_address (abfd, line_ptr, cu, &bytes_read);
10998
10999 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11000 {
11001 /* This line table is for a function which has been
11002 GCd by the linker. Ignore it. PR gdb/12528 */
11003
11004 long line_offset
11005 = line_ptr - dwarf2_per_objfile->line.buffer;
11006
11007 complaint (&symfile_complaints,
11008 _(".debug_line address at offset 0x%lx is 0 "
11009 "[in module %s]"),
11010 line_offset, cu->objfile->name);
11011 p_record_line = noop_record_line;
11012 }
11013
11014 op_index = 0;
11015 line_ptr += bytes_read;
11016 address += baseaddr;
11017 break;
11018 case DW_LNE_define_file:
11019 {
11020 char *cur_file;
11021 unsigned int dir_index, mod_time, length;
11022
11023 cur_file = read_direct_string (abfd, line_ptr,
11024 &bytes_read);
11025 line_ptr += bytes_read;
11026 dir_index =
11027 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11028 line_ptr += bytes_read;
11029 mod_time =
11030 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11031 line_ptr += bytes_read;
11032 length =
11033 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11034 line_ptr += bytes_read;
11035 add_file_name (lh, cur_file, dir_index, mod_time, length);
11036 }
11037 break;
11038 case DW_LNE_set_discriminator:
11039 /* The discriminator is not interesting to the debugger;
11040 just ignore it. */
11041 line_ptr = extended_end;
11042 break;
11043 default:
11044 complaint (&symfile_complaints,
11045 _("mangled .debug_line section"));
11046 return;
11047 }
11048 /* Make sure that we parsed the extended op correctly. If e.g.
11049 we expected a different address size than the producer used,
11050 we may have read the wrong number of bytes. */
11051 if (line_ptr != extended_end)
11052 {
11053 complaint (&symfile_complaints,
11054 _("mangled .debug_line section"));
11055 return;
11056 }
11057 break;
11058 case DW_LNS_copy:
11059 if (lh->num_file_names < file || file == 0)
11060 dwarf2_debug_line_missing_file_complaint ();
11061 else
11062 {
11063 lh->file_names[file - 1].included_p = 1;
11064 if (!decode_for_pst_p && is_stmt)
11065 {
11066 if (last_subfile != current_subfile)
11067 {
11068 addr = gdbarch_addr_bits_remove (gdbarch, address);
11069 if (last_subfile)
11070 (*p_record_line) (last_subfile, 0, addr);
11071 last_subfile = current_subfile;
11072 }
11073 addr = check_cu_functions (address, cu);
11074 addr = gdbarch_addr_bits_remove (gdbarch, addr);
11075 (*p_record_line) (current_subfile, line, addr);
11076 }
11077 }
11078 basic_block = 0;
11079 break;
11080 case DW_LNS_advance_pc:
11081 {
11082 CORE_ADDR adjust
11083 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11084
11085 address += (((op_index + adjust)
11086 / lh->maximum_ops_per_instruction)
11087 * lh->minimum_instruction_length);
11088 op_index = ((op_index + adjust)
11089 % lh->maximum_ops_per_instruction);
11090 line_ptr += bytes_read;
11091 }
11092 break;
11093 case DW_LNS_advance_line:
11094 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11095 line_ptr += bytes_read;
11096 break;
11097 case DW_LNS_set_file:
11098 {
11099 /* The arrays lh->include_dirs and lh->file_names are
11100 0-based, but the directory and file name numbers in
11101 the statement program are 1-based. */
11102 struct file_entry *fe;
11103 char *dir = NULL;
11104
11105 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11106 line_ptr += bytes_read;
11107 if (lh->num_file_names < file || file == 0)
11108 dwarf2_debug_line_missing_file_complaint ();
11109 else
11110 {
11111 fe = &lh->file_names[file - 1];
11112 if (fe->dir_index)
11113 dir = lh->include_dirs[fe->dir_index - 1];
11114 if (!decode_for_pst_p)
11115 {
11116 last_subfile = current_subfile;
11117 dwarf2_start_subfile (fe->name, dir, comp_dir);
11118 }
11119 }
11120 }
11121 break;
11122 case DW_LNS_set_column:
11123 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11124 line_ptr += bytes_read;
11125 break;
11126 case DW_LNS_negate_stmt:
11127 is_stmt = (!is_stmt);
11128 break;
11129 case DW_LNS_set_basic_block:
11130 basic_block = 1;
11131 break;
11132 /* Add to the address register of the state machine the
11133 address increment value corresponding to special opcode
11134 255. I.e., this value is scaled by the minimum
11135 instruction length since special opcode 255 would have
11136 scaled the increment. */
11137 case DW_LNS_const_add_pc:
11138 {
11139 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11140
11141 address += (((op_index + adjust)
11142 / lh->maximum_ops_per_instruction)
11143 * lh->minimum_instruction_length);
11144 op_index = ((op_index + adjust)
11145 % lh->maximum_ops_per_instruction);
11146 }
11147 break;
11148 case DW_LNS_fixed_advance_pc:
11149 address += read_2_bytes (abfd, line_ptr);
11150 op_index = 0;
11151 line_ptr += 2;
11152 break;
11153 default:
11154 {
11155 /* Unknown standard opcode, ignore it. */
11156 int i;
11157
11158 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11159 {
11160 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11161 line_ptr += bytes_read;
11162 }
11163 }
11164 }
11165 }
11166 if (lh->num_file_names < file || file == 0)
11167 dwarf2_debug_line_missing_file_complaint ();
11168 else
11169 {
11170 lh->file_names[file - 1].included_p = 1;
11171 if (!decode_for_pst_p)
11172 {
11173 addr = gdbarch_addr_bits_remove (gdbarch, address);
11174 (*p_record_line) (current_subfile, 0, addr);
11175 }
11176 }
11177 }
11178
11179 if (decode_for_pst_p)
11180 {
11181 int file_index;
11182
11183 /* Now that we're done scanning the Line Header Program, we can
11184 create the psymtab of each included file. */
11185 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11186 if (lh->file_names[file_index].included_p == 1)
11187 {
11188 char *include_name =
11189 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11190 if (include_name != NULL)
11191 dwarf2_create_include_psymtab (include_name, pst, objfile);
11192 }
11193 }
11194 else
11195 {
11196 /* Make sure a symtab is created for every file, even files
11197 which contain only variables (i.e. no code with associated
11198 line numbers). */
11199
11200 int i;
11201 struct file_entry *fe;
11202
11203 for (i = 0; i < lh->num_file_names; i++)
11204 {
11205 char *dir = NULL;
11206
11207 fe = &lh->file_names[i];
11208 if (fe->dir_index)
11209 dir = lh->include_dirs[fe->dir_index - 1];
11210 dwarf2_start_subfile (fe->name, dir, comp_dir);
11211
11212 /* Skip the main file; we don't need it, and it must be
11213 allocated last, so that it will show up before the
11214 non-primary symtabs in the objfile's symtab list. */
11215 if (current_subfile == first_subfile)
11216 continue;
11217
11218 if (current_subfile->symtab == NULL)
11219 current_subfile->symtab = allocate_symtab (current_subfile->name,
11220 cu->objfile);
11221 fe->symtab = current_subfile->symtab;
11222 }
11223 }
11224 }
11225
11226 /* Start a subfile for DWARF. FILENAME is the name of the file and
11227 DIRNAME the name of the source directory which contains FILENAME
11228 or NULL if not known. COMP_DIR is the compilation directory for the
11229 linetable's compilation unit or NULL if not known.
11230 This routine tries to keep line numbers from identical absolute and
11231 relative file names in a common subfile.
11232
11233 Using the `list' example from the GDB testsuite, which resides in
11234 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11235 of /srcdir/list0.c yields the following debugging information for list0.c:
11236
11237 DW_AT_name: /srcdir/list0.c
11238 DW_AT_comp_dir: /compdir
11239 files.files[0].name: list0.h
11240 files.files[0].dir: /srcdir
11241 files.files[1].name: list0.c
11242 files.files[1].dir: /srcdir
11243
11244 The line number information for list0.c has to end up in a single
11245 subfile, so that `break /srcdir/list0.c:1' works as expected.
11246 start_subfile will ensure that this happens provided that we pass the
11247 concatenation of files.files[1].dir and files.files[1].name as the
11248 subfile's name. */
11249
11250 static void
11251 dwarf2_start_subfile (char *filename, const char *dirname,
11252 const char *comp_dir)
11253 {
11254 char *fullname;
11255
11256 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11257 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11258 second argument to start_subfile. To be consistent, we do the
11259 same here. In order not to lose the line information directory,
11260 we concatenate it to the filename when it makes sense.
11261 Note that the Dwarf3 standard says (speaking of filenames in line
11262 information): ``The directory index is ignored for file names
11263 that represent full path names''. Thus ignoring dirname in the
11264 `else' branch below isn't an issue. */
11265
11266 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11267 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11268 else
11269 fullname = filename;
11270
11271 start_subfile (fullname, comp_dir);
11272
11273 if (fullname != filename)
11274 xfree (fullname);
11275 }
11276
11277 static void
11278 var_decode_location (struct attribute *attr, struct symbol *sym,
11279 struct dwarf2_cu *cu)
11280 {
11281 struct objfile *objfile = cu->objfile;
11282 struct comp_unit_head *cu_header = &cu->header;
11283
11284 /* NOTE drow/2003-01-30: There used to be a comment and some special
11285 code here to turn a symbol with DW_AT_external and a
11286 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11287 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11288 with some versions of binutils) where shared libraries could have
11289 relocations against symbols in their debug information - the
11290 minimal symbol would have the right address, but the debug info
11291 would not. It's no longer necessary, because we will explicitly
11292 apply relocations when we read in the debug information now. */
11293
11294 /* A DW_AT_location attribute with no contents indicates that a
11295 variable has been optimized away. */
11296 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11297 {
11298 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11299 return;
11300 }
11301
11302 /* Handle one degenerate form of location expression specially, to
11303 preserve GDB's previous behavior when section offsets are
11304 specified. If this is just a DW_OP_addr then mark this symbol
11305 as LOC_STATIC. */
11306
11307 if (attr_form_is_block (attr)
11308 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11309 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11310 {
11311 unsigned int dummy;
11312
11313 SYMBOL_VALUE_ADDRESS (sym) =
11314 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11315 SYMBOL_CLASS (sym) = LOC_STATIC;
11316 fixup_symbol_section (sym, objfile);
11317 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11318 SYMBOL_SECTION (sym));
11319 return;
11320 }
11321
11322 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11323 expression evaluator, and use LOC_COMPUTED only when necessary
11324 (i.e. when the value of a register or memory location is
11325 referenced, or a thread-local block, etc.). Then again, it might
11326 not be worthwhile. I'm assuming that it isn't unless performance
11327 or memory numbers show me otherwise. */
11328
11329 dwarf2_symbol_mark_computed (attr, sym, cu);
11330 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11331
11332 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11333 cu->has_loclist = 1;
11334 }
11335
11336 /* Given a pointer to a DWARF information entry, figure out if we need
11337 to make a symbol table entry for it, and if so, create a new entry
11338 and return a pointer to it.
11339 If TYPE is NULL, determine symbol type from the die, otherwise
11340 used the passed type.
11341 If SPACE is not NULL, use it to hold the new symbol. If it is
11342 NULL, allocate a new symbol on the objfile's obstack. */
11343
11344 static struct symbol *
11345 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11346 struct symbol *space)
11347 {
11348 struct objfile *objfile = cu->objfile;
11349 struct symbol *sym = NULL;
11350 char *name;
11351 struct attribute *attr = NULL;
11352 struct attribute *attr2 = NULL;
11353 CORE_ADDR baseaddr;
11354 struct pending **list_to_add = NULL;
11355
11356 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11357
11358 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11359
11360 name = dwarf2_name (die, cu);
11361 if (name)
11362 {
11363 const char *linkagename;
11364 int suppress_add = 0;
11365
11366 if (space)
11367 sym = space;
11368 else
11369 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11370 OBJSTAT (objfile, n_syms++);
11371
11372 /* Cache this symbol's name and the name's demangled form (if any). */
11373 SYMBOL_SET_LANGUAGE (sym, cu->language);
11374 linkagename = dwarf2_physname (name, die, cu);
11375 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11376
11377 /* Fortran does not have mangling standard and the mangling does differ
11378 between gfortran, iFort etc. */
11379 if (cu->language == language_fortran
11380 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11381 symbol_set_demangled_name (&(sym->ginfo),
11382 (char *) dwarf2_full_name (name, die, cu),
11383 NULL);
11384
11385 /* Default assumptions.
11386 Use the passed type or decode it from the die. */
11387 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11388 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11389 if (type != NULL)
11390 SYMBOL_TYPE (sym) = type;
11391 else
11392 SYMBOL_TYPE (sym) = die_type (die, cu);
11393 attr = dwarf2_attr (die,
11394 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11395 cu);
11396 if (attr)
11397 {
11398 SYMBOL_LINE (sym) = DW_UNSND (attr);
11399 }
11400
11401 attr = dwarf2_attr (die,
11402 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11403 cu);
11404 if (attr)
11405 {
11406 int file_index = DW_UNSND (attr);
11407
11408 if (cu->line_header == NULL
11409 || file_index > cu->line_header->num_file_names)
11410 complaint (&symfile_complaints,
11411 _("file index out of range"));
11412 else if (file_index > 0)
11413 {
11414 struct file_entry *fe;
11415
11416 fe = &cu->line_header->file_names[file_index - 1];
11417 SYMBOL_SYMTAB (sym) = fe->symtab;
11418 }
11419 }
11420
11421 switch (die->tag)
11422 {
11423 case DW_TAG_label:
11424 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11425 if (attr)
11426 {
11427 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11428 }
11429 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11430 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11431 SYMBOL_CLASS (sym) = LOC_LABEL;
11432 add_symbol_to_list (sym, cu->list_in_scope);
11433 break;
11434 case DW_TAG_subprogram:
11435 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11436 finish_block. */
11437 SYMBOL_CLASS (sym) = LOC_BLOCK;
11438 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11439 if ((attr2 && (DW_UNSND (attr2) != 0))
11440 || cu->language == language_ada)
11441 {
11442 /* Subprograms marked external are stored as a global symbol.
11443 Ada subprograms, whether marked external or not, are always
11444 stored as a global symbol, because we want to be able to
11445 access them globally. For instance, we want to be able
11446 to break on a nested subprogram without having to
11447 specify the context. */
11448 list_to_add = &global_symbols;
11449 }
11450 else
11451 {
11452 list_to_add = cu->list_in_scope;
11453 }
11454 break;
11455 case DW_TAG_inlined_subroutine:
11456 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11457 finish_block. */
11458 SYMBOL_CLASS (sym) = LOC_BLOCK;
11459 SYMBOL_INLINED (sym) = 1;
11460 /* Do not add the symbol to any lists. It will be found via
11461 BLOCK_FUNCTION from the blockvector. */
11462 break;
11463 case DW_TAG_template_value_param:
11464 suppress_add = 1;
11465 /* Fall through. */
11466 case DW_TAG_constant:
11467 case DW_TAG_variable:
11468 case DW_TAG_member:
11469 /* Compilation with minimal debug info may result in
11470 variables with missing type entries. Change the
11471 misleading `void' type to something sensible. */
11472 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11473 SYMBOL_TYPE (sym)
11474 = objfile_type (objfile)->nodebug_data_symbol;
11475
11476 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11477 /* In the case of DW_TAG_member, we should only be called for
11478 static const members. */
11479 if (die->tag == DW_TAG_member)
11480 {
11481 /* dwarf2_add_field uses die_is_declaration,
11482 so we do the same. */
11483 gdb_assert (die_is_declaration (die, cu));
11484 gdb_assert (attr);
11485 }
11486 if (attr)
11487 {
11488 dwarf2_const_value (attr, sym, cu);
11489 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11490 if (!suppress_add)
11491 {
11492 if (attr2 && (DW_UNSND (attr2) != 0))
11493 list_to_add = &global_symbols;
11494 else
11495 list_to_add = cu->list_in_scope;
11496 }
11497 break;
11498 }
11499 attr = dwarf2_attr (die, DW_AT_location, cu);
11500 if (attr)
11501 {
11502 var_decode_location (attr, sym, cu);
11503 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11504 if (SYMBOL_CLASS (sym) == LOC_STATIC
11505 && SYMBOL_VALUE_ADDRESS (sym) == 0
11506 && !dwarf2_per_objfile->has_section_at_zero)
11507 {
11508 /* When a static variable is eliminated by the linker,
11509 the corresponding debug information is not stripped
11510 out, but the variable address is set to null;
11511 do not add such variables into symbol table. */
11512 }
11513 else if (attr2 && (DW_UNSND (attr2) != 0))
11514 {
11515 /* Workaround gfortran PR debug/40040 - it uses
11516 DW_AT_location for variables in -fPIC libraries which may
11517 get overriden by other libraries/executable and get
11518 a different address. Resolve it by the minimal symbol
11519 which may come from inferior's executable using copy
11520 relocation. Make this workaround only for gfortran as for
11521 other compilers GDB cannot guess the minimal symbol
11522 Fortran mangling kind. */
11523 if (cu->language == language_fortran && die->parent
11524 && die->parent->tag == DW_TAG_module
11525 && cu->producer
11526 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11527 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11528
11529 /* A variable with DW_AT_external is never static,
11530 but it may be block-scoped. */
11531 list_to_add = (cu->list_in_scope == &file_symbols
11532 ? &global_symbols : cu->list_in_scope);
11533 }
11534 else
11535 list_to_add = cu->list_in_scope;
11536 }
11537 else
11538 {
11539 /* We do not know the address of this symbol.
11540 If it is an external symbol and we have type information
11541 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11542 The address of the variable will then be determined from
11543 the minimal symbol table whenever the variable is
11544 referenced. */
11545 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11546 if (attr2 && (DW_UNSND (attr2) != 0)
11547 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11548 {
11549 /* A variable with DW_AT_external is never static, but it
11550 may be block-scoped. */
11551 list_to_add = (cu->list_in_scope == &file_symbols
11552 ? &global_symbols : cu->list_in_scope);
11553
11554 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11555 }
11556 else if (!die_is_declaration (die, cu))
11557 {
11558 /* Use the default LOC_OPTIMIZED_OUT class. */
11559 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11560 if (!suppress_add)
11561 list_to_add = cu->list_in_scope;
11562 }
11563 }
11564 break;
11565 case DW_TAG_formal_parameter:
11566 /* If we are inside a function, mark this as an argument. If
11567 not, we might be looking at an argument to an inlined function
11568 when we do not have enough information to show inlined frames;
11569 pretend it's a local variable in that case so that the user can
11570 still see it. */
11571 if (context_stack_depth > 0
11572 && context_stack[context_stack_depth - 1].name != NULL)
11573 SYMBOL_IS_ARGUMENT (sym) = 1;
11574 attr = dwarf2_attr (die, DW_AT_location, cu);
11575 if (attr)
11576 {
11577 var_decode_location (attr, sym, cu);
11578 }
11579 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11580 if (attr)
11581 {
11582 dwarf2_const_value (attr, sym, cu);
11583 }
11584
11585 list_to_add = cu->list_in_scope;
11586 break;
11587 case DW_TAG_unspecified_parameters:
11588 /* From varargs functions; gdb doesn't seem to have any
11589 interest in this information, so just ignore it for now.
11590 (FIXME?) */
11591 break;
11592 case DW_TAG_template_type_param:
11593 suppress_add = 1;
11594 /* Fall through. */
11595 case DW_TAG_class_type:
11596 case DW_TAG_interface_type:
11597 case DW_TAG_structure_type:
11598 case DW_TAG_union_type:
11599 case DW_TAG_set_type:
11600 case DW_TAG_enumeration_type:
11601 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11602 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11603
11604 {
11605 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11606 really ever be static objects: otherwise, if you try
11607 to, say, break of a class's method and you're in a file
11608 which doesn't mention that class, it won't work unless
11609 the check for all static symbols in lookup_symbol_aux
11610 saves you. See the OtherFileClass tests in
11611 gdb.c++/namespace.exp. */
11612
11613 if (!suppress_add)
11614 {
11615 list_to_add = (cu->list_in_scope == &file_symbols
11616 && (cu->language == language_cplus
11617 || cu->language == language_java)
11618 ? &global_symbols : cu->list_in_scope);
11619
11620 /* The semantics of C++ state that "struct foo {
11621 ... }" also defines a typedef for "foo". A Java
11622 class declaration also defines a typedef for the
11623 class. */
11624 if (cu->language == language_cplus
11625 || cu->language == language_java
11626 || cu->language == language_ada)
11627 {
11628 /* The symbol's name is already allocated along
11629 with this objfile, so we don't need to
11630 duplicate it for the type. */
11631 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11632 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11633 }
11634 }
11635 }
11636 break;
11637 case DW_TAG_typedef:
11638 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11639 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11640 list_to_add = cu->list_in_scope;
11641 break;
11642 case DW_TAG_base_type:
11643 case DW_TAG_subrange_type:
11644 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11645 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11646 list_to_add = cu->list_in_scope;
11647 break;
11648 case DW_TAG_enumerator:
11649 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11650 if (attr)
11651 {
11652 dwarf2_const_value (attr, sym, cu);
11653 }
11654 {
11655 /* NOTE: carlton/2003-11-10: See comment above in the
11656 DW_TAG_class_type, etc. block. */
11657
11658 list_to_add = (cu->list_in_scope == &file_symbols
11659 && (cu->language == language_cplus
11660 || cu->language == language_java)
11661 ? &global_symbols : cu->list_in_scope);
11662 }
11663 break;
11664 case DW_TAG_namespace:
11665 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11666 list_to_add = &global_symbols;
11667 break;
11668 default:
11669 /* Not a tag we recognize. Hopefully we aren't processing
11670 trash data, but since we must specifically ignore things
11671 we don't recognize, there is nothing else we should do at
11672 this point. */
11673 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11674 dwarf_tag_name (die->tag));
11675 break;
11676 }
11677
11678 if (suppress_add)
11679 {
11680 sym->hash_next = objfile->template_symbols;
11681 objfile->template_symbols = sym;
11682 list_to_add = NULL;
11683 }
11684
11685 if (list_to_add != NULL)
11686 add_symbol_to_list (sym, list_to_add);
11687
11688 /* For the benefit of old versions of GCC, check for anonymous
11689 namespaces based on the demangled name. */
11690 if (!processing_has_namespace_info
11691 && cu->language == language_cplus)
11692 cp_scan_for_anonymous_namespaces (sym);
11693 }
11694 return (sym);
11695 }
11696
11697 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11698
11699 static struct symbol *
11700 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11701 {
11702 return new_symbol_full (die, type, cu, NULL);
11703 }
11704
11705 /* Given an attr with a DW_FORM_dataN value in host byte order,
11706 zero-extend it as appropriate for the symbol's type. The DWARF
11707 standard (v4) is not entirely clear about the meaning of using
11708 DW_FORM_dataN for a constant with a signed type, where the type is
11709 wider than the data. The conclusion of a discussion on the DWARF
11710 list was that this is unspecified. We choose to always zero-extend
11711 because that is the interpretation long in use by GCC. */
11712
11713 static gdb_byte *
11714 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11715 const char *name, struct obstack *obstack,
11716 struct dwarf2_cu *cu, long *value, int bits)
11717 {
11718 struct objfile *objfile = cu->objfile;
11719 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11720 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11721 LONGEST l = DW_UNSND (attr);
11722
11723 if (bits < sizeof (*value) * 8)
11724 {
11725 l &= ((LONGEST) 1 << bits) - 1;
11726 *value = l;
11727 }
11728 else if (bits == sizeof (*value) * 8)
11729 *value = l;
11730 else
11731 {
11732 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11733 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11734 return bytes;
11735 }
11736
11737 return NULL;
11738 }
11739
11740 /* Read a constant value from an attribute. Either set *VALUE, or if
11741 the value does not fit in *VALUE, set *BYTES - either already
11742 allocated on the objfile obstack, or newly allocated on OBSTACK,
11743 or, set *BATON, if we translated the constant to a location
11744 expression. */
11745
11746 static void
11747 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11748 const char *name, struct obstack *obstack,
11749 struct dwarf2_cu *cu,
11750 long *value, gdb_byte **bytes,
11751 struct dwarf2_locexpr_baton **baton)
11752 {
11753 struct objfile *objfile = cu->objfile;
11754 struct comp_unit_head *cu_header = &cu->header;
11755 struct dwarf_block *blk;
11756 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11757 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11758
11759 *value = 0;
11760 *bytes = NULL;
11761 *baton = NULL;
11762
11763 switch (attr->form)
11764 {
11765 case DW_FORM_addr:
11766 {
11767 gdb_byte *data;
11768
11769 if (TYPE_LENGTH (type) != cu_header->addr_size)
11770 dwarf2_const_value_length_mismatch_complaint (name,
11771 cu_header->addr_size,
11772 TYPE_LENGTH (type));
11773 /* Symbols of this form are reasonably rare, so we just
11774 piggyback on the existing location code rather than writing
11775 a new implementation of symbol_computed_ops. */
11776 *baton = obstack_alloc (&objfile->objfile_obstack,
11777 sizeof (struct dwarf2_locexpr_baton));
11778 (*baton)->per_cu = cu->per_cu;
11779 gdb_assert ((*baton)->per_cu);
11780
11781 (*baton)->size = 2 + cu_header->addr_size;
11782 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11783 (*baton)->data = data;
11784
11785 data[0] = DW_OP_addr;
11786 store_unsigned_integer (&data[1], cu_header->addr_size,
11787 byte_order, DW_ADDR (attr));
11788 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11789 }
11790 break;
11791 case DW_FORM_string:
11792 case DW_FORM_strp:
11793 /* DW_STRING is already allocated on the objfile obstack, point
11794 directly to it. */
11795 *bytes = (gdb_byte *) DW_STRING (attr);
11796 break;
11797 case DW_FORM_block1:
11798 case DW_FORM_block2:
11799 case DW_FORM_block4:
11800 case DW_FORM_block:
11801 case DW_FORM_exprloc:
11802 blk = DW_BLOCK (attr);
11803 if (TYPE_LENGTH (type) != blk->size)
11804 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11805 TYPE_LENGTH (type));
11806 *bytes = blk->data;
11807 break;
11808
11809 /* The DW_AT_const_value attributes are supposed to carry the
11810 symbol's value "represented as it would be on the target
11811 architecture." By the time we get here, it's already been
11812 converted to host endianness, so we just need to sign- or
11813 zero-extend it as appropriate. */
11814 case DW_FORM_data1:
11815 *bytes = dwarf2_const_value_data (attr, type, name,
11816 obstack, cu, value, 8);
11817 break;
11818 case DW_FORM_data2:
11819 *bytes = dwarf2_const_value_data (attr, type, name,
11820 obstack, cu, value, 16);
11821 break;
11822 case DW_FORM_data4:
11823 *bytes = dwarf2_const_value_data (attr, type, name,
11824 obstack, cu, value, 32);
11825 break;
11826 case DW_FORM_data8:
11827 *bytes = dwarf2_const_value_data (attr, type, name,
11828 obstack, cu, value, 64);
11829 break;
11830
11831 case DW_FORM_sdata:
11832 *value = DW_SND (attr);
11833 break;
11834
11835 case DW_FORM_udata:
11836 *value = DW_UNSND (attr);
11837 break;
11838
11839 default:
11840 complaint (&symfile_complaints,
11841 _("unsupported const value attribute form: '%s'"),
11842 dwarf_form_name (attr->form));
11843 *value = 0;
11844 break;
11845 }
11846 }
11847
11848
11849 /* Copy constant value from an attribute to a symbol. */
11850
11851 static void
11852 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11853 struct dwarf2_cu *cu)
11854 {
11855 struct objfile *objfile = cu->objfile;
11856 struct comp_unit_head *cu_header = &cu->header;
11857 long value;
11858 gdb_byte *bytes;
11859 struct dwarf2_locexpr_baton *baton;
11860
11861 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11862 SYMBOL_PRINT_NAME (sym),
11863 &objfile->objfile_obstack, cu,
11864 &value, &bytes, &baton);
11865
11866 if (baton != NULL)
11867 {
11868 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11869 SYMBOL_LOCATION_BATON (sym) = baton;
11870 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11871 }
11872 else if (bytes != NULL)
11873 {
11874 SYMBOL_VALUE_BYTES (sym) = bytes;
11875 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11876 }
11877 else
11878 {
11879 SYMBOL_VALUE (sym) = value;
11880 SYMBOL_CLASS (sym) = LOC_CONST;
11881 }
11882 }
11883
11884 /* Return the type of the die in question using its DW_AT_type attribute. */
11885
11886 static struct type *
11887 die_type (struct die_info *die, struct dwarf2_cu *cu)
11888 {
11889 struct attribute *type_attr;
11890
11891 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11892 if (!type_attr)
11893 {
11894 /* A missing DW_AT_type represents a void type. */
11895 return objfile_type (cu->objfile)->builtin_void;
11896 }
11897
11898 return lookup_die_type (die, type_attr, cu);
11899 }
11900
11901 /* True iff CU's producer generates GNAT Ada auxiliary information
11902 that allows to find parallel types through that information instead
11903 of having to do expensive parallel lookups by type name. */
11904
11905 static int
11906 need_gnat_info (struct dwarf2_cu *cu)
11907 {
11908 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11909 of GNAT produces this auxiliary information, without any indication
11910 that it is produced. Part of enhancing the FSF version of GNAT
11911 to produce that information will be to put in place an indicator
11912 that we can use in order to determine whether the descriptive type
11913 info is available or not. One suggestion that has been made is
11914 to use a new attribute, attached to the CU die. For now, assume
11915 that the descriptive type info is not available. */
11916 return 0;
11917 }
11918
11919 /* Return the auxiliary type of the die in question using its
11920 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11921 attribute is not present. */
11922
11923 static struct type *
11924 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11925 {
11926 struct attribute *type_attr;
11927
11928 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11929 if (!type_attr)
11930 return NULL;
11931
11932 return lookup_die_type (die, type_attr, cu);
11933 }
11934
11935 /* If DIE has a descriptive_type attribute, then set the TYPE's
11936 descriptive type accordingly. */
11937
11938 static void
11939 set_descriptive_type (struct type *type, struct die_info *die,
11940 struct dwarf2_cu *cu)
11941 {
11942 struct type *descriptive_type = die_descriptive_type (die, cu);
11943
11944 if (descriptive_type)
11945 {
11946 ALLOCATE_GNAT_AUX_TYPE (type);
11947 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11948 }
11949 }
11950
11951 /* Return the containing type of the die in question using its
11952 DW_AT_containing_type attribute. */
11953
11954 static struct type *
11955 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11956 {
11957 struct attribute *type_attr;
11958
11959 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11960 if (!type_attr)
11961 error (_("Dwarf Error: Problem turning containing type into gdb type "
11962 "[in module %s]"), cu->objfile->name);
11963
11964 return lookup_die_type (die, type_attr, cu);
11965 }
11966
11967 /* Look up the type of DIE in CU using its type attribute ATTR.
11968 If there is no type substitute an error marker. */
11969
11970 static struct type *
11971 lookup_die_type (struct die_info *die, struct attribute *attr,
11972 struct dwarf2_cu *cu)
11973 {
11974 struct type *this_type;
11975
11976 /* First see if we have it cached. */
11977
11978 if (is_ref_attr (attr))
11979 {
11980 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11981
11982 this_type = get_die_type_at_offset (offset, cu->per_cu);
11983 }
11984 else if (attr->form == DW_FORM_ref_sig8)
11985 {
11986 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11987 struct dwarf2_cu *sig_cu;
11988 unsigned int offset;
11989
11990 /* sig_type will be NULL if the signatured type is missing from
11991 the debug info. */
11992 if (sig_type == NULL)
11993 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11994 "at 0x%x [in module %s]"),
11995 die->offset, cu->objfile->name);
11996
11997 gdb_assert (sig_type->per_cu.debug_type_section);
11998 offset = sig_type->per_cu.offset + sig_type->type_offset;
11999 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12000 }
12001 else
12002 {
12003 dump_die_for_error (die);
12004 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12005 dwarf_attr_name (attr->name), cu->objfile->name);
12006 }
12007
12008 /* If not cached we need to read it in. */
12009
12010 if (this_type == NULL)
12011 {
12012 struct die_info *type_die;
12013 struct dwarf2_cu *type_cu = cu;
12014
12015 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12016 /* If the type is cached, we should have found it above. */
12017 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12018 this_type = read_type_die_1 (type_die, type_cu);
12019 }
12020
12021 /* If we still don't have a type use an error marker. */
12022
12023 if (this_type == NULL)
12024 {
12025 char *message, *saved;
12026
12027 /* read_type_die already issued a complaint. */
12028 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12029 cu->objfile->name,
12030 cu->header.offset,
12031 die->offset);
12032 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
12033 message, strlen (message));
12034 xfree (message);
12035
12036 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
12037 }
12038
12039 return this_type;
12040 }
12041
12042 /* Return the type in DIE, CU.
12043 Returns NULL for invalid types.
12044
12045 This first does a lookup in the appropriate type_hash table,
12046 and only reads the die in if necessary.
12047
12048 NOTE: This can be called when reading in partial or full symbols. */
12049
12050 static struct type *
12051 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12052 {
12053 struct type *this_type;
12054
12055 this_type = get_die_type (die, cu);
12056 if (this_type)
12057 return this_type;
12058
12059 return read_type_die_1 (die, cu);
12060 }
12061
12062 /* Read the type in DIE, CU.
12063 Returns NULL for invalid types. */
12064
12065 static struct type *
12066 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12067 {
12068 struct type *this_type = NULL;
12069
12070 switch (die->tag)
12071 {
12072 case DW_TAG_class_type:
12073 case DW_TAG_interface_type:
12074 case DW_TAG_structure_type:
12075 case DW_TAG_union_type:
12076 this_type = read_structure_type (die, cu);
12077 break;
12078 case DW_TAG_enumeration_type:
12079 this_type = read_enumeration_type (die, cu);
12080 break;
12081 case DW_TAG_subprogram:
12082 case DW_TAG_subroutine_type:
12083 case DW_TAG_inlined_subroutine:
12084 this_type = read_subroutine_type (die, cu);
12085 break;
12086 case DW_TAG_array_type:
12087 this_type = read_array_type (die, cu);
12088 break;
12089 case DW_TAG_set_type:
12090 this_type = read_set_type (die, cu);
12091 break;
12092 case DW_TAG_pointer_type:
12093 this_type = read_tag_pointer_type (die, cu);
12094 break;
12095 case DW_TAG_ptr_to_member_type:
12096 this_type = read_tag_ptr_to_member_type (die, cu);
12097 break;
12098 case DW_TAG_reference_type:
12099 this_type = read_tag_reference_type (die, cu);
12100 break;
12101 case DW_TAG_const_type:
12102 this_type = read_tag_const_type (die, cu);
12103 break;
12104 case DW_TAG_volatile_type:
12105 this_type = read_tag_volatile_type (die, cu);
12106 break;
12107 case DW_TAG_string_type:
12108 this_type = read_tag_string_type (die, cu);
12109 break;
12110 case DW_TAG_typedef:
12111 this_type = read_typedef (die, cu);
12112 break;
12113 case DW_TAG_subrange_type:
12114 this_type = read_subrange_type (die, cu);
12115 break;
12116 case DW_TAG_base_type:
12117 this_type = read_base_type (die, cu);
12118 break;
12119 case DW_TAG_unspecified_type:
12120 this_type = read_unspecified_type (die, cu);
12121 break;
12122 case DW_TAG_namespace:
12123 this_type = read_namespace_type (die, cu);
12124 break;
12125 case DW_TAG_module:
12126 this_type = read_module_type (die, cu);
12127 break;
12128 default:
12129 complaint (&symfile_complaints,
12130 _("unexpected tag in read_type_die: '%s'"),
12131 dwarf_tag_name (die->tag));
12132 break;
12133 }
12134
12135 return this_type;
12136 }
12137
12138 /* See if we can figure out if the class lives in a namespace. We do
12139 this by looking for a member function; its demangled name will
12140 contain namespace info, if there is any.
12141 Return the computed name or NULL.
12142 Space for the result is allocated on the objfile's obstack.
12143 This is the full-die version of guess_partial_die_structure_name.
12144 In this case we know DIE has no useful parent. */
12145
12146 static char *
12147 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12148 {
12149 struct die_info *spec_die;
12150 struct dwarf2_cu *spec_cu;
12151 struct die_info *child;
12152
12153 spec_cu = cu;
12154 spec_die = die_specification (die, &spec_cu);
12155 if (spec_die != NULL)
12156 {
12157 die = spec_die;
12158 cu = spec_cu;
12159 }
12160
12161 for (child = die->child;
12162 child != NULL;
12163 child = child->sibling)
12164 {
12165 if (child->tag == DW_TAG_subprogram)
12166 {
12167 struct attribute *attr;
12168
12169 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12170 if (attr == NULL)
12171 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12172 if (attr != NULL)
12173 {
12174 char *actual_name
12175 = language_class_name_from_physname (cu->language_defn,
12176 DW_STRING (attr));
12177 char *name = NULL;
12178
12179 if (actual_name != NULL)
12180 {
12181 char *die_name = dwarf2_name (die, cu);
12182
12183 if (die_name != NULL
12184 && strcmp (die_name, actual_name) != 0)
12185 {
12186 /* Strip off the class name from the full name.
12187 We want the prefix. */
12188 int die_name_len = strlen (die_name);
12189 int actual_name_len = strlen (actual_name);
12190
12191 /* Test for '::' as a sanity check. */
12192 if (actual_name_len > die_name_len + 2
12193 && actual_name[actual_name_len
12194 - die_name_len - 1] == ':')
12195 name =
12196 obsavestring (actual_name,
12197 actual_name_len - die_name_len - 2,
12198 &cu->objfile->objfile_obstack);
12199 }
12200 }
12201 xfree (actual_name);
12202 return name;
12203 }
12204 }
12205 }
12206
12207 return NULL;
12208 }
12209
12210 /* Return the name of the namespace/class that DIE is defined within,
12211 or "" if we can't tell. The caller should not xfree the result.
12212
12213 For example, if we're within the method foo() in the following
12214 code:
12215
12216 namespace N {
12217 class C {
12218 void foo () {
12219 }
12220 };
12221 }
12222
12223 then determine_prefix on foo's die will return "N::C". */
12224
12225 static char *
12226 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12227 {
12228 struct die_info *parent, *spec_die;
12229 struct dwarf2_cu *spec_cu;
12230 struct type *parent_type;
12231
12232 if (cu->language != language_cplus && cu->language != language_java
12233 && cu->language != language_fortran)
12234 return "";
12235
12236 /* We have to be careful in the presence of DW_AT_specification.
12237 For example, with GCC 3.4, given the code
12238
12239 namespace N {
12240 void foo() {
12241 // Definition of N::foo.
12242 }
12243 }
12244
12245 then we'll have a tree of DIEs like this:
12246
12247 1: DW_TAG_compile_unit
12248 2: DW_TAG_namespace // N
12249 3: DW_TAG_subprogram // declaration of N::foo
12250 4: DW_TAG_subprogram // definition of N::foo
12251 DW_AT_specification // refers to die #3
12252
12253 Thus, when processing die #4, we have to pretend that we're in
12254 the context of its DW_AT_specification, namely the contex of die
12255 #3. */
12256 spec_cu = cu;
12257 spec_die = die_specification (die, &spec_cu);
12258 if (spec_die == NULL)
12259 parent = die->parent;
12260 else
12261 {
12262 parent = spec_die->parent;
12263 cu = spec_cu;
12264 }
12265
12266 if (parent == NULL)
12267 return "";
12268 else if (parent->building_fullname)
12269 {
12270 const char *name;
12271 const char *parent_name;
12272
12273 /* It has been seen on RealView 2.2 built binaries,
12274 DW_TAG_template_type_param types actually _defined_ as
12275 children of the parent class:
12276
12277 enum E {};
12278 template class <class Enum> Class{};
12279 Class<enum E> class_e;
12280
12281 1: DW_TAG_class_type (Class)
12282 2: DW_TAG_enumeration_type (E)
12283 3: DW_TAG_enumerator (enum1:0)
12284 3: DW_TAG_enumerator (enum2:1)
12285 ...
12286 2: DW_TAG_template_type_param
12287 DW_AT_type DW_FORM_ref_udata (E)
12288
12289 Besides being broken debug info, it can put GDB into an
12290 infinite loop. Consider:
12291
12292 When we're building the full name for Class<E>, we'll start
12293 at Class, and go look over its template type parameters,
12294 finding E. We'll then try to build the full name of E, and
12295 reach here. We're now trying to build the full name of E,
12296 and look over the parent DIE for containing scope. In the
12297 broken case, if we followed the parent DIE of E, we'd again
12298 find Class, and once again go look at its template type
12299 arguments, etc., etc. Simply don't consider such parent die
12300 as source-level parent of this die (it can't be, the language
12301 doesn't allow it), and break the loop here. */
12302 name = dwarf2_name (die, cu);
12303 parent_name = dwarf2_name (parent, cu);
12304 complaint (&symfile_complaints,
12305 _("template param type '%s' defined within parent '%s'"),
12306 name ? name : "<unknown>",
12307 parent_name ? parent_name : "<unknown>");
12308 return "";
12309 }
12310 else
12311 switch (parent->tag)
12312 {
12313 case DW_TAG_namespace:
12314 parent_type = read_type_die (parent, cu);
12315 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12316 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12317 Work around this problem here. */
12318 if (cu->language == language_cplus
12319 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12320 return "";
12321 /* We give a name to even anonymous namespaces. */
12322 return TYPE_TAG_NAME (parent_type);
12323 case DW_TAG_class_type:
12324 case DW_TAG_interface_type:
12325 case DW_TAG_structure_type:
12326 case DW_TAG_union_type:
12327 case DW_TAG_module:
12328 parent_type = read_type_die (parent, cu);
12329 if (TYPE_TAG_NAME (parent_type) != NULL)
12330 return TYPE_TAG_NAME (parent_type);
12331 else
12332 /* An anonymous structure is only allowed non-static data
12333 members; no typedefs, no member functions, et cetera.
12334 So it does not need a prefix. */
12335 return "";
12336 case DW_TAG_compile_unit:
12337 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12338 if (cu->language == language_cplus
12339 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12340 && die->child != NULL
12341 && (die->tag == DW_TAG_class_type
12342 || die->tag == DW_TAG_structure_type
12343 || die->tag == DW_TAG_union_type))
12344 {
12345 char *name = guess_full_die_structure_name (die, cu);
12346 if (name != NULL)
12347 return name;
12348 }
12349 return "";
12350 default:
12351 return determine_prefix (parent, cu);
12352 }
12353 }
12354
12355 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12356 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12357 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12358 an obconcat, otherwise allocate storage for the result. The CU argument is
12359 used to determine the language and hence, the appropriate separator. */
12360
12361 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12362
12363 static char *
12364 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12365 int physname, struct dwarf2_cu *cu)
12366 {
12367 const char *lead = "";
12368 const char *sep;
12369
12370 if (suffix == NULL || suffix[0] == '\0'
12371 || prefix == NULL || prefix[0] == '\0')
12372 sep = "";
12373 else if (cu->language == language_java)
12374 sep = ".";
12375 else if (cu->language == language_fortran && physname)
12376 {
12377 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12378 DW_AT_MIPS_linkage_name is preferred and used instead. */
12379
12380 lead = "__";
12381 sep = "_MOD_";
12382 }
12383 else
12384 sep = "::";
12385
12386 if (prefix == NULL)
12387 prefix = "";
12388 if (suffix == NULL)
12389 suffix = "";
12390
12391 if (obs == NULL)
12392 {
12393 char *retval
12394 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12395
12396 strcpy (retval, lead);
12397 strcat (retval, prefix);
12398 strcat (retval, sep);
12399 strcat (retval, suffix);
12400 return retval;
12401 }
12402 else
12403 {
12404 /* We have an obstack. */
12405 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12406 }
12407 }
12408
12409 /* Return sibling of die, NULL if no sibling. */
12410
12411 static struct die_info *
12412 sibling_die (struct die_info *die)
12413 {
12414 return die->sibling;
12415 }
12416
12417 /* Get name of a die, return NULL if not found. */
12418
12419 static char *
12420 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12421 struct obstack *obstack)
12422 {
12423 if (name && cu->language == language_cplus)
12424 {
12425 char *canon_name = cp_canonicalize_string (name);
12426
12427 if (canon_name != NULL)
12428 {
12429 if (strcmp (canon_name, name) != 0)
12430 name = obsavestring (canon_name, strlen (canon_name),
12431 obstack);
12432 xfree (canon_name);
12433 }
12434 }
12435
12436 return name;
12437 }
12438
12439 /* Get name of a die, return NULL if not found. */
12440
12441 static char *
12442 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12443 {
12444 struct attribute *attr;
12445
12446 attr = dwarf2_attr (die, DW_AT_name, cu);
12447 if ((!attr || !DW_STRING (attr))
12448 && die->tag != DW_TAG_class_type
12449 && die->tag != DW_TAG_interface_type
12450 && die->tag != DW_TAG_structure_type
12451 && die->tag != DW_TAG_union_type)
12452 return NULL;
12453
12454 switch (die->tag)
12455 {
12456 case DW_TAG_compile_unit:
12457 /* Compilation units have a DW_AT_name that is a filename, not
12458 a source language identifier. */
12459 case DW_TAG_enumeration_type:
12460 case DW_TAG_enumerator:
12461 /* These tags always have simple identifiers already; no need
12462 to canonicalize them. */
12463 return DW_STRING (attr);
12464
12465 case DW_TAG_subprogram:
12466 /* Java constructors will all be named "<init>", so return
12467 the class name when we see this special case. */
12468 if (cu->language == language_java
12469 && DW_STRING (attr) != NULL
12470 && strcmp (DW_STRING (attr), "<init>") == 0)
12471 {
12472 struct dwarf2_cu *spec_cu = cu;
12473 struct die_info *spec_die;
12474
12475 /* GCJ will output '<init>' for Java constructor names.
12476 For this special case, return the name of the parent class. */
12477
12478 /* GCJ may output suprogram DIEs with AT_specification set.
12479 If so, use the name of the specified DIE. */
12480 spec_die = die_specification (die, &spec_cu);
12481 if (spec_die != NULL)
12482 return dwarf2_name (spec_die, spec_cu);
12483
12484 do
12485 {
12486 die = die->parent;
12487 if (die->tag == DW_TAG_class_type)
12488 return dwarf2_name (die, cu);
12489 }
12490 while (die->tag != DW_TAG_compile_unit);
12491 }
12492 break;
12493
12494 case DW_TAG_class_type:
12495 case DW_TAG_interface_type:
12496 case DW_TAG_structure_type:
12497 case DW_TAG_union_type:
12498 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12499 structures or unions. These were of the form "._%d" in GCC 4.1,
12500 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12501 and GCC 4.4. We work around this problem by ignoring these. */
12502 if (attr && DW_STRING (attr)
12503 && (strncmp (DW_STRING (attr), "._", 2) == 0
12504 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12505 return NULL;
12506
12507 /* GCC might emit a nameless typedef that has a linkage name. See
12508 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12509 if (!attr || DW_STRING (attr) == NULL)
12510 {
12511 char *demangled = NULL;
12512
12513 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12514 if (attr == NULL)
12515 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12516
12517 if (attr == NULL || DW_STRING (attr) == NULL)
12518 return NULL;
12519
12520 /* Avoid demangling DW_STRING (attr) the second time on a second
12521 call for the same DIE. */
12522 if (!DW_STRING_IS_CANONICAL (attr))
12523 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12524
12525 if (demangled)
12526 {
12527 /* FIXME: we already did this for the partial symbol... */
12528 DW_STRING (attr)
12529 = obsavestring (demangled, strlen (demangled),
12530 &cu->objfile->objfile_obstack);
12531 DW_STRING_IS_CANONICAL (attr) = 1;
12532 xfree (demangled);
12533 }
12534 }
12535 break;
12536
12537 default:
12538 break;
12539 }
12540
12541 if (!DW_STRING_IS_CANONICAL (attr))
12542 {
12543 DW_STRING (attr)
12544 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12545 &cu->objfile->objfile_obstack);
12546 DW_STRING_IS_CANONICAL (attr) = 1;
12547 }
12548 return DW_STRING (attr);
12549 }
12550
12551 /* Return the die that this die in an extension of, or NULL if there
12552 is none. *EXT_CU is the CU containing DIE on input, and the CU
12553 containing the return value on output. */
12554
12555 static struct die_info *
12556 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12557 {
12558 struct attribute *attr;
12559
12560 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12561 if (attr == NULL)
12562 return NULL;
12563
12564 return follow_die_ref (die, attr, ext_cu);
12565 }
12566
12567 /* Convert a DIE tag into its string name. */
12568
12569 static char *
12570 dwarf_tag_name (unsigned tag)
12571 {
12572 switch (tag)
12573 {
12574 case DW_TAG_padding:
12575 return "DW_TAG_padding";
12576 case DW_TAG_array_type:
12577 return "DW_TAG_array_type";
12578 case DW_TAG_class_type:
12579 return "DW_TAG_class_type";
12580 case DW_TAG_entry_point:
12581 return "DW_TAG_entry_point";
12582 case DW_TAG_enumeration_type:
12583 return "DW_TAG_enumeration_type";
12584 case DW_TAG_formal_parameter:
12585 return "DW_TAG_formal_parameter";
12586 case DW_TAG_imported_declaration:
12587 return "DW_TAG_imported_declaration";
12588 case DW_TAG_label:
12589 return "DW_TAG_label";
12590 case DW_TAG_lexical_block:
12591 return "DW_TAG_lexical_block";
12592 case DW_TAG_member:
12593 return "DW_TAG_member";
12594 case DW_TAG_pointer_type:
12595 return "DW_TAG_pointer_type";
12596 case DW_TAG_reference_type:
12597 return "DW_TAG_reference_type";
12598 case DW_TAG_compile_unit:
12599 return "DW_TAG_compile_unit";
12600 case DW_TAG_string_type:
12601 return "DW_TAG_string_type";
12602 case DW_TAG_structure_type:
12603 return "DW_TAG_structure_type";
12604 case DW_TAG_subroutine_type:
12605 return "DW_TAG_subroutine_type";
12606 case DW_TAG_typedef:
12607 return "DW_TAG_typedef";
12608 case DW_TAG_union_type:
12609 return "DW_TAG_union_type";
12610 case DW_TAG_unspecified_parameters:
12611 return "DW_TAG_unspecified_parameters";
12612 case DW_TAG_variant:
12613 return "DW_TAG_variant";
12614 case DW_TAG_common_block:
12615 return "DW_TAG_common_block";
12616 case DW_TAG_common_inclusion:
12617 return "DW_TAG_common_inclusion";
12618 case DW_TAG_inheritance:
12619 return "DW_TAG_inheritance";
12620 case DW_TAG_inlined_subroutine:
12621 return "DW_TAG_inlined_subroutine";
12622 case DW_TAG_module:
12623 return "DW_TAG_module";
12624 case DW_TAG_ptr_to_member_type:
12625 return "DW_TAG_ptr_to_member_type";
12626 case DW_TAG_set_type:
12627 return "DW_TAG_set_type";
12628 case DW_TAG_subrange_type:
12629 return "DW_TAG_subrange_type";
12630 case DW_TAG_with_stmt:
12631 return "DW_TAG_with_stmt";
12632 case DW_TAG_access_declaration:
12633 return "DW_TAG_access_declaration";
12634 case DW_TAG_base_type:
12635 return "DW_TAG_base_type";
12636 case DW_TAG_catch_block:
12637 return "DW_TAG_catch_block";
12638 case DW_TAG_const_type:
12639 return "DW_TAG_const_type";
12640 case DW_TAG_constant:
12641 return "DW_TAG_constant";
12642 case DW_TAG_enumerator:
12643 return "DW_TAG_enumerator";
12644 case DW_TAG_file_type:
12645 return "DW_TAG_file_type";
12646 case DW_TAG_friend:
12647 return "DW_TAG_friend";
12648 case DW_TAG_namelist:
12649 return "DW_TAG_namelist";
12650 case DW_TAG_namelist_item:
12651 return "DW_TAG_namelist_item";
12652 case DW_TAG_packed_type:
12653 return "DW_TAG_packed_type";
12654 case DW_TAG_subprogram:
12655 return "DW_TAG_subprogram";
12656 case DW_TAG_template_type_param:
12657 return "DW_TAG_template_type_param";
12658 case DW_TAG_template_value_param:
12659 return "DW_TAG_template_value_param";
12660 case DW_TAG_thrown_type:
12661 return "DW_TAG_thrown_type";
12662 case DW_TAG_try_block:
12663 return "DW_TAG_try_block";
12664 case DW_TAG_variant_part:
12665 return "DW_TAG_variant_part";
12666 case DW_TAG_variable:
12667 return "DW_TAG_variable";
12668 case DW_TAG_volatile_type:
12669 return "DW_TAG_volatile_type";
12670 case DW_TAG_dwarf_procedure:
12671 return "DW_TAG_dwarf_procedure";
12672 case DW_TAG_restrict_type:
12673 return "DW_TAG_restrict_type";
12674 case DW_TAG_interface_type:
12675 return "DW_TAG_interface_type";
12676 case DW_TAG_namespace:
12677 return "DW_TAG_namespace";
12678 case DW_TAG_imported_module:
12679 return "DW_TAG_imported_module";
12680 case DW_TAG_unspecified_type:
12681 return "DW_TAG_unspecified_type";
12682 case DW_TAG_partial_unit:
12683 return "DW_TAG_partial_unit";
12684 case DW_TAG_imported_unit:
12685 return "DW_TAG_imported_unit";
12686 case DW_TAG_condition:
12687 return "DW_TAG_condition";
12688 case DW_TAG_shared_type:
12689 return "DW_TAG_shared_type";
12690 case DW_TAG_type_unit:
12691 return "DW_TAG_type_unit";
12692 case DW_TAG_MIPS_loop:
12693 return "DW_TAG_MIPS_loop";
12694 case DW_TAG_HP_array_descriptor:
12695 return "DW_TAG_HP_array_descriptor";
12696 case DW_TAG_format_label:
12697 return "DW_TAG_format_label";
12698 case DW_TAG_function_template:
12699 return "DW_TAG_function_template";
12700 case DW_TAG_class_template:
12701 return "DW_TAG_class_template";
12702 case DW_TAG_GNU_BINCL:
12703 return "DW_TAG_GNU_BINCL";
12704 case DW_TAG_GNU_EINCL:
12705 return "DW_TAG_GNU_EINCL";
12706 case DW_TAG_upc_shared_type:
12707 return "DW_TAG_upc_shared_type";
12708 case DW_TAG_upc_strict_type:
12709 return "DW_TAG_upc_strict_type";
12710 case DW_TAG_upc_relaxed_type:
12711 return "DW_TAG_upc_relaxed_type";
12712 case DW_TAG_PGI_kanji_type:
12713 return "DW_TAG_PGI_kanji_type";
12714 case DW_TAG_PGI_interface_block:
12715 return "DW_TAG_PGI_interface_block";
12716 default:
12717 return "DW_TAG_<unknown>";
12718 }
12719 }
12720
12721 /* Convert a DWARF attribute code into its string name. */
12722
12723 static char *
12724 dwarf_attr_name (unsigned attr)
12725 {
12726 switch (attr)
12727 {
12728 case DW_AT_sibling:
12729 return "DW_AT_sibling";
12730 case DW_AT_location:
12731 return "DW_AT_location";
12732 case DW_AT_name:
12733 return "DW_AT_name";
12734 case DW_AT_ordering:
12735 return "DW_AT_ordering";
12736 case DW_AT_subscr_data:
12737 return "DW_AT_subscr_data";
12738 case DW_AT_byte_size:
12739 return "DW_AT_byte_size";
12740 case DW_AT_bit_offset:
12741 return "DW_AT_bit_offset";
12742 case DW_AT_bit_size:
12743 return "DW_AT_bit_size";
12744 case DW_AT_element_list:
12745 return "DW_AT_element_list";
12746 case DW_AT_stmt_list:
12747 return "DW_AT_stmt_list";
12748 case DW_AT_low_pc:
12749 return "DW_AT_low_pc";
12750 case DW_AT_high_pc:
12751 return "DW_AT_high_pc";
12752 case DW_AT_language:
12753 return "DW_AT_language";
12754 case DW_AT_member:
12755 return "DW_AT_member";
12756 case DW_AT_discr:
12757 return "DW_AT_discr";
12758 case DW_AT_discr_value:
12759 return "DW_AT_discr_value";
12760 case DW_AT_visibility:
12761 return "DW_AT_visibility";
12762 case DW_AT_import:
12763 return "DW_AT_import";
12764 case DW_AT_string_length:
12765 return "DW_AT_string_length";
12766 case DW_AT_common_reference:
12767 return "DW_AT_common_reference";
12768 case DW_AT_comp_dir:
12769 return "DW_AT_comp_dir";
12770 case DW_AT_const_value:
12771 return "DW_AT_const_value";
12772 case DW_AT_containing_type:
12773 return "DW_AT_containing_type";
12774 case DW_AT_default_value:
12775 return "DW_AT_default_value";
12776 case DW_AT_inline:
12777 return "DW_AT_inline";
12778 case DW_AT_is_optional:
12779 return "DW_AT_is_optional";
12780 case DW_AT_lower_bound:
12781 return "DW_AT_lower_bound";
12782 case DW_AT_producer:
12783 return "DW_AT_producer";
12784 case DW_AT_prototyped:
12785 return "DW_AT_prototyped";
12786 case DW_AT_return_addr:
12787 return "DW_AT_return_addr";
12788 case DW_AT_start_scope:
12789 return "DW_AT_start_scope";
12790 case DW_AT_bit_stride:
12791 return "DW_AT_bit_stride";
12792 case DW_AT_upper_bound:
12793 return "DW_AT_upper_bound";
12794 case DW_AT_abstract_origin:
12795 return "DW_AT_abstract_origin";
12796 case DW_AT_accessibility:
12797 return "DW_AT_accessibility";
12798 case DW_AT_address_class:
12799 return "DW_AT_address_class";
12800 case DW_AT_artificial:
12801 return "DW_AT_artificial";
12802 case DW_AT_base_types:
12803 return "DW_AT_base_types";
12804 case DW_AT_calling_convention:
12805 return "DW_AT_calling_convention";
12806 case DW_AT_count:
12807 return "DW_AT_count";
12808 case DW_AT_data_member_location:
12809 return "DW_AT_data_member_location";
12810 case DW_AT_decl_column:
12811 return "DW_AT_decl_column";
12812 case DW_AT_decl_file:
12813 return "DW_AT_decl_file";
12814 case DW_AT_decl_line:
12815 return "DW_AT_decl_line";
12816 case DW_AT_declaration:
12817 return "DW_AT_declaration";
12818 case DW_AT_discr_list:
12819 return "DW_AT_discr_list";
12820 case DW_AT_encoding:
12821 return "DW_AT_encoding";
12822 case DW_AT_external:
12823 return "DW_AT_external";
12824 case DW_AT_frame_base:
12825 return "DW_AT_frame_base";
12826 case DW_AT_friend:
12827 return "DW_AT_friend";
12828 case DW_AT_identifier_case:
12829 return "DW_AT_identifier_case";
12830 case DW_AT_macro_info:
12831 return "DW_AT_macro_info";
12832 case DW_AT_namelist_items:
12833 return "DW_AT_namelist_items";
12834 case DW_AT_priority:
12835 return "DW_AT_priority";
12836 case DW_AT_segment:
12837 return "DW_AT_segment";
12838 case DW_AT_specification:
12839 return "DW_AT_specification";
12840 case DW_AT_static_link:
12841 return "DW_AT_static_link";
12842 case DW_AT_type:
12843 return "DW_AT_type";
12844 case DW_AT_use_location:
12845 return "DW_AT_use_location";
12846 case DW_AT_variable_parameter:
12847 return "DW_AT_variable_parameter";
12848 case DW_AT_virtuality:
12849 return "DW_AT_virtuality";
12850 case DW_AT_vtable_elem_location:
12851 return "DW_AT_vtable_elem_location";
12852 /* DWARF 3 values. */
12853 case DW_AT_allocated:
12854 return "DW_AT_allocated";
12855 case DW_AT_associated:
12856 return "DW_AT_associated";
12857 case DW_AT_data_location:
12858 return "DW_AT_data_location";
12859 case DW_AT_byte_stride:
12860 return "DW_AT_byte_stride";
12861 case DW_AT_entry_pc:
12862 return "DW_AT_entry_pc";
12863 case DW_AT_use_UTF8:
12864 return "DW_AT_use_UTF8";
12865 case DW_AT_extension:
12866 return "DW_AT_extension";
12867 case DW_AT_ranges:
12868 return "DW_AT_ranges";
12869 case DW_AT_trampoline:
12870 return "DW_AT_trampoline";
12871 case DW_AT_call_column:
12872 return "DW_AT_call_column";
12873 case DW_AT_call_file:
12874 return "DW_AT_call_file";
12875 case DW_AT_call_line:
12876 return "DW_AT_call_line";
12877 case DW_AT_description:
12878 return "DW_AT_description";
12879 case DW_AT_binary_scale:
12880 return "DW_AT_binary_scale";
12881 case DW_AT_decimal_scale:
12882 return "DW_AT_decimal_scale";
12883 case DW_AT_small:
12884 return "DW_AT_small";
12885 case DW_AT_decimal_sign:
12886 return "DW_AT_decimal_sign";
12887 case DW_AT_digit_count:
12888 return "DW_AT_digit_count";
12889 case DW_AT_picture_string:
12890 return "DW_AT_picture_string";
12891 case DW_AT_mutable:
12892 return "DW_AT_mutable";
12893 case DW_AT_threads_scaled:
12894 return "DW_AT_threads_scaled";
12895 case DW_AT_explicit:
12896 return "DW_AT_explicit";
12897 case DW_AT_object_pointer:
12898 return "DW_AT_object_pointer";
12899 case DW_AT_endianity:
12900 return "DW_AT_endianity";
12901 case DW_AT_elemental:
12902 return "DW_AT_elemental";
12903 case DW_AT_pure:
12904 return "DW_AT_pure";
12905 case DW_AT_recursive:
12906 return "DW_AT_recursive";
12907 /* DWARF 4 values. */
12908 case DW_AT_signature:
12909 return "DW_AT_signature";
12910 case DW_AT_linkage_name:
12911 return "DW_AT_linkage_name";
12912 /* SGI/MIPS extensions. */
12913 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12914 case DW_AT_MIPS_fde:
12915 return "DW_AT_MIPS_fde";
12916 #endif
12917 case DW_AT_MIPS_loop_begin:
12918 return "DW_AT_MIPS_loop_begin";
12919 case DW_AT_MIPS_tail_loop_begin:
12920 return "DW_AT_MIPS_tail_loop_begin";
12921 case DW_AT_MIPS_epilog_begin:
12922 return "DW_AT_MIPS_epilog_begin";
12923 case DW_AT_MIPS_loop_unroll_factor:
12924 return "DW_AT_MIPS_loop_unroll_factor";
12925 case DW_AT_MIPS_software_pipeline_depth:
12926 return "DW_AT_MIPS_software_pipeline_depth";
12927 case DW_AT_MIPS_linkage_name:
12928 return "DW_AT_MIPS_linkage_name";
12929 case DW_AT_MIPS_stride:
12930 return "DW_AT_MIPS_stride";
12931 case DW_AT_MIPS_abstract_name:
12932 return "DW_AT_MIPS_abstract_name";
12933 case DW_AT_MIPS_clone_origin:
12934 return "DW_AT_MIPS_clone_origin";
12935 case DW_AT_MIPS_has_inlines:
12936 return "DW_AT_MIPS_has_inlines";
12937 /* HP extensions. */
12938 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12939 case DW_AT_HP_block_index:
12940 return "DW_AT_HP_block_index";
12941 #endif
12942 case DW_AT_HP_unmodifiable:
12943 return "DW_AT_HP_unmodifiable";
12944 case DW_AT_HP_actuals_stmt_list:
12945 return "DW_AT_HP_actuals_stmt_list";
12946 case DW_AT_HP_proc_per_section:
12947 return "DW_AT_HP_proc_per_section";
12948 case DW_AT_HP_raw_data_ptr:
12949 return "DW_AT_HP_raw_data_ptr";
12950 case DW_AT_HP_pass_by_reference:
12951 return "DW_AT_HP_pass_by_reference";
12952 case DW_AT_HP_opt_level:
12953 return "DW_AT_HP_opt_level";
12954 case DW_AT_HP_prof_version_id:
12955 return "DW_AT_HP_prof_version_id";
12956 case DW_AT_HP_opt_flags:
12957 return "DW_AT_HP_opt_flags";
12958 case DW_AT_HP_cold_region_low_pc:
12959 return "DW_AT_HP_cold_region_low_pc";
12960 case DW_AT_HP_cold_region_high_pc:
12961 return "DW_AT_HP_cold_region_high_pc";
12962 case DW_AT_HP_all_variables_modifiable:
12963 return "DW_AT_HP_all_variables_modifiable";
12964 case DW_AT_HP_linkage_name:
12965 return "DW_AT_HP_linkage_name";
12966 case DW_AT_HP_prof_flags:
12967 return "DW_AT_HP_prof_flags";
12968 /* GNU extensions. */
12969 case DW_AT_sf_names:
12970 return "DW_AT_sf_names";
12971 case DW_AT_src_info:
12972 return "DW_AT_src_info";
12973 case DW_AT_mac_info:
12974 return "DW_AT_mac_info";
12975 case DW_AT_src_coords:
12976 return "DW_AT_src_coords";
12977 case DW_AT_body_begin:
12978 return "DW_AT_body_begin";
12979 case DW_AT_body_end:
12980 return "DW_AT_body_end";
12981 case DW_AT_GNU_vector:
12982 return "DW_AT_GNU_vector";
12983 case DW_AT_GNU_odr_signature:
12984 return "DW_AT_GNU_odr_signature";
12985 /* VMS extensions. */
12986 case DW_AT_VMS_rtnbeg_pd_address:
12987 return "DW_AT_VMS_rtnbeg_pd_address";
12988 /* UPC extension. */
12989 case DW_AT_upc_threads_scaled:
12990 return "DW_AT_upc_threads_scaled";
12991 /* PGI (STMicroelectronics) extensions. */
12992 case DW_AT_PGI_lbase:
12993 return "DW_AT_PGI_lbase";
12994 case DW_AT_PGI_soffset:
12995 return "DW_AT_PGI_soffset";
12996 case DW_AT_PGI_lstride:
12997 return "DW_AT_PGI_lstride";
12998 default:
12999 return "DW_AT_<unknown>";
13000 }
13001 }
13002
13003 /* Convert a DWARF value form code into its string name. */
13004
13005 static char *
13006 dwarf_form_name (unsigned form)
13007 {
13008 switch (form)
13009 {
13010 case DW_FORM_addr:
13011 return "DW_FORM_addr";
13012 case DW_FORM_block2:
13013 return "DW_FORM_block2";
13014 case DW_FORM_block4:
13015 return "DW_FORM_block4";
13016 case DW_FORM_data2:
13017 return "DW_FORM_data2";
13018 case DW_FORM_data4:
13019 return "DW_FORM_data4";
13020 case DW_FORM_data8:
13021 return "DW_FORM_data8";
13022 case DW_FORM_string:
13023 return "DW_FORM_string";
13024 case DW_FORM_block:
13025 return "DW_FORM_block";
13026 case DW_FORM_block1:
13027 return "DW_FORM_block1";
13028 case DW_FORM_data1:
13029 return "DW_FORM_data1";
13030 case DW_FORM_flag:
13031 return "DW_FORM_flag";
13032 case DW_FORM_sdata:
13033 return "DW_FORM_sdata";
13034 case DW_FORM_strp:
13035 return "DW_FORM_strp";
13036 case DW_FORM_udata:
13037 return "DW_FORM_udata";
13038 case DW_FORM_ref_addr:
13039 return "DW_FORM_ref_addr";
13040 case DW_FORM_ref1:
13041 return "DW_FORM_ref1";
13042 case DW_FORM_ref2:
13043 return "DW_FORM_ref2";
13044 case DW_FORM_ref4:
13045 return "DW_FORM_ref4";
13046 case DW_FORM_ref8:
13047 return "DW_FORM_ref8";
13048 case DW_FORM_ref_udata:
13049 return "DW_FORM_ref_udata";
13050 case DW_FORM_indirect:
13051 return "DW_FORM_indirect";
13052 case DW_FORM_sec_offset:
13053 return "DW_FORM_sec_offset";
13054 case DW_FORM_exprloc:
13055 return "DW_FORM_exprloc";
13056 case DW_FORM_flag_present:
13057 return "DW_FORM_flag_present";
13058 case DW_FORM_ref_sig8:
13059 return "DW_FORM_ref_sig8";
13060 default:
13061 return "DW_FORM_<unknown>";
13062 }
13063 }
13064
13065 /* Convert a DWARF stack opcode into its string name. */
13066
13067 const char *
13068 dwarf_stack_op_name (unsigned op)
13069 {
13070 switch (op)
13071 {
13072 case DW_OP_addr:
13073 return "DW_OP_addr";
13074 case DW_OP_deref:
13075 return "DW_OP_deref";
13076 case DW_OP_const1u:
13077 return "DW_OP_const1u";
13078 case DW_OP_const1s:
13079 return "DW_OP_const1s";
13080 case DW_OP_const2u:
13081 return "DW_OP_const2u";
13082 case DW_OP_const2s:
13083 return "DW_OP_const2s";
13084 case DW_OP_const4u:
13085 return "DW_OP_const4u";
13086 case DW_OP_const4s:
13087 return "DW_OP_const4s";
13088 case DW_OP_const8u:
13089 return "DW_OP_const8u";
13090 case DW_OP_const8s:
13091 return "DW_OP_const8s";
13092 case DW_OP_constu:
13093 return "DW_OP_constu";
13094 case DW_OP_consts:
13095 return "DW_OP_consts";
13096 case DW_OP_dup:
13097 return "DW_OP_dup";
13098 case DW_OP_drop:
13099 return "DW_OP_drop";
13100 case DW_OP_over:
13101 return "DW_OP_over";
13102 case DW_OP_pick:
13103 return "DW_OP_pick";
13104 case DW_OP_swap:
13105 return "DW_OP_swap";
13106 case DW_OP_rot:
13107 return "DW_OP_rot";
13108 case DW_OP_xderef:
13109 return "DW_OP_xderef";
13110 case DW_OP_abs:
13111 return "DW_OP_abs";
13112 case DW_OP_and:
13113 return "DW_OP_and";
13114 case DW_OP_div:
13115 return "DW_OP_div";
13116 case DW_OP_minus:
13117 return "DW_OP_minus";
13118 case DW_OP_mod:
13119 return "DW_OP_mod";
13120 case DW_OP_mul:
13121 return "DW_OP_mul";
13122 case DW_OP_neg:
13123 return "DW_OP_neg";
13124 case DW_OP_not:
13125 return "DW_OP_not";
13126 case DW_OP_or:
13127 return "DW_OP_or";
13128 case DW_OP_plus:
13129 return "DW_OP_plus";
13130 case DW_OP_plus_uconst:
13131 return "DW_OP_plus_uconst";
13132 case DW_OP_shl:
13133 return "DW_OP_shl";
13134 case DW_OP_shr:
13135 return "DW_OP_shr";
13136 case DW_OP_shra:
13137 return "DW_OP_shra";
13138 case DW_OP_xor:
13139 return "DW_OP_xor";
13140 case DW_OP_bra:
13141 return "DW_OP_bra";
13142 case DW_OP_eq:
13143 return "DW_OP_eq";
13144 case DW_OP_ge:
13145 return "DW_OP_ge";
13146 case DW_OP_gt:
13147 return "DW_OP_gt";
13148 case DW_OP_le:
13149 return "DW_OP_le";
13150 case DW_OP_lt:
13151 return "DW_OP_lt";
13152 case DW_OP_ne:
13153 return "DW_OP_ne";
13154 case DW_OP_skip:
13155 return "DW_OP_skip";
13156 case DW_OP_lit0:
13157 return "DW_OP_lit0";
13158 case DW_OP_lit1:
13159 return "DW_OP_lit1";
13160 case DW_OP_lit2:
13161 return "DW_OP_lit2";
13162 case DW_OP_lit3:
13163 return "DW_OP_lit3";
13164 case DW_OP_lit4:
13165 return "DW_OP_lit4";
13166 case DW_OP_lit5:
13167 return "DW_OP_lit5";
13168 case DW_OP_lit6:
13169 return "DW_OP_lit6";
13170 case DW_OP_lit7:
13171 return "DW_OP_lit7";
13172 case DW_OP_lit8:
13173 return "DW_OP_lit8";
13174 case DW_OP_lit9:
13175 return "DW_OP_lit9";
13176 case DW_OP_lit10:
13177 return "DW_OP_lit10";
13178 case DW_OP_lit11:
13179 return "DW_OP_lit11";
13180 case DW_OP_lit12:
13181 return "DW_OP_lit12";
13182 case DW_OP_lit13:
13183 return "DW_OP_lit13";
13184 case DW_OP_lit14:
13185 return "DW_OP_lit14";
13186 case DW_OP_lit15:
13187 return "DW_OP_lit15";
13188 case DW_OP_lit16:
13189 return "DW_OP_lit16";
13190 case DW_OP_lit17:
13191 return "DW_OP_lit17";
13192 case DW_OP_lit18:
13193 return "DW_OP_lit18";
13194 case DW_OP_lit19:
13195 return "DW_OP_lit19";
13196 case DW_OP_lit20:
13197 return "DW_OP_lit20";
13198 case DW_OP_lit21:
13199 return "DW_OP_lit21";
13200 case DW_OP_lit22:
13201 return "DW_OP_lit22";
13202 case DW_OP_lit23:
13203 return "DW_OP_lit23";
13204 case DW_OP_lit24:
13205 return "DW_OP_lit24";
13206 case DW_OP_lit25:
13207 return "DW_OP_lit25";
13208 case DW_OP_lit26:
13209 return "DW_OP_lit26";
13210 case DW_OP_lit27:
13211 return "DW_OP_lit27";
13212 case DW_OP_lit28:
13213 return "DW_OP_lit28";
13214 case DW_OP_lit29:
13215 return "DW_OP_lit29";
13216 case DW_OP_lit30:
13217 return "DW_OP_lit30";
13218 case DW_OP_lit31:
13219 return "DW_OP_lit31";
13220 case DW_OP_reg0:
13221 return "DW_OP_reg0";
13222 case DW_OP_reg1:
13223 return "DW_OP_reg1";
13224 case DW_OP_reg2:
13225 return "DW_OP_reg2";
13226 case DW_OP_reg3:
13227 return "DW_OP_reg3";
13228 case DW_OP_reg4:
13229 return "DW_OP_reg4";
13230 case DW_OP_reg5:
13231 return "DW_OP_reg5";
13232 case DW_OP_reg6:
13233 return "DW_OP_reg6";
13234 case DW_OP_reg7:
13235 return "DW_OP_reg7";
13236 case DW_OP_reg8:
13237 return "DW_OP_reg8";
13238 case DW_OP_reg9:
13239 return "DW_OP_reg9";
13240 case DW_OP_reg10:
13241 return "DW_OP_reg10";
13242 case DW_OP_reg11:
13243 return "DW_OP_reg11";
13244 case DW_OP_reg12:
13245 return "DW_OP_reg12";
13246 case DW_OP_reg13:
13247 return "DW_OP_reg13";
13248 case DW_OP_reg14:
13249 return "DW_OP_reg14";
13250 case DW_OP_reg15:
13251 return "DW_OP_reg15";
13252 case DW_OP_reg16:
13253 return "DW_OP_reg16";
13254 case DW_OP_reg17:
13255 return "DW_OP_reg17";
13256 case DW_OP_reg18:
13257 return "DW_OP_reg18";
13258 case DW_OP_reg19:
13259 return "DW_OP_reg19";
13260 case DW_OP_reg20:
13261 return "DW_OP_reg20";
13262 case DW_OP_reg21:
13263 return "DW_OP_reg21";
13264 case DW_OP_reg22:
13265 return "DW_OP_reg22";
13266 case DW_OP_reg23:
13267 return "DW_OP_reg23";
13268 case DW_OP_reg24:
13269 return "DW_OP_reg24";
13270 case DW_OP_reg25:
13271 return "DW_OP_reg25";
13272 case DW_OP_reg26:
13273 return "DW_OP_reg26";
13274 case DW_OP_reg27:
13275 return "DW_OP_reg27";
13276 case DW_OP_reg28:
13277 return "DW_OP_reg28";
13278 case DW_OP_reg29:
13279 return "DW_OP_reg29";
13280 case DW_OP_reg30:
13281 return "DW_OP_reg30";
13282 case DW_OP_reg31:
13283 return "DW_OP_reg31";
13284 case DW_OP_breg0:
13285 return "DW_OP_breg0";
13286 case DW_OP_breg1:
13287 return "DW_OP_breg1";
13288 case DW_OP_breg2:
13289 return "DW_OP_breg2";
13290 case DW_OP_breg3:
13291 return "DW_OP_breg3";
13292 case DW_OP_breg4:
13293 return "DW_OP_breg4";
13294 case DW_OP_breg5:
13295 return "DW_OP_breg5";
13296 case DW_OP_breg6:
13297 return "DW_OP_breg6";
13298 case DW_OP_breg7:
13299 return "DW_OP_breg7";
13300 case DW_OP_breg8:
13301 return "DW_OP_breg8";
13302 case DW_OP_breg9:
13303 return "DW_OP_breg9";
13304 case DW_OP_breg10:
13305 return "DW_OP_breg10";
13306 case DW_OP_breg11:
13307 return "DW_OP_breg11";
13308 case DW_OP_breg12:
13309 return "DW_OP_breg12";
13310 case DW_OP_breg13:
13311 return "DW_OP_breg13";
13312 case DW_OP_breg14:
13313 return "DW_OP_breg14";
13314 case DW_OP_breg15:
13315 return "DW_OP_breg15";
13316 case DW_OP_breg16:
13317 return "DW_OP_breg16";
13318 case DW_OP_breg17:
13319 return "DW_OP_breg17";
13320 case DW_OP_breg18:
13321 return "DW_OP_breg18";
13322 case DW_OP_breg19:
13323 return "DW_OP_breg19";
13324 case DW_OP_breg20:
13325 return "DW_OP_breg20";
13326 case DW_OP_breg21:
13327 return "DW_OP_breg21";
13328 case DW_OP_breg22:
13329 return "DW_OP_breg22";
13330 case DW_OP_breg23:
13331 return "DW_OP_breg23";
13332 case DW_OP_breg24:
13333 return "DW_OP_breg24";
13334 case DW_OP_breg25:
13335 return "DW_OP_breg25";
13336 case DW_OP_breg26:
13337 return "DW_OP_breg26";
13338 case DW_OP_breg27:
13339 return "DW_OP_breg27";
13340 case DW_OP_breg28:
13341 return "DW_OP_breg28";
13342 case DW_OP_breg29:
13343 return "DW_OP_breg29";
13344 case DW_OP_breg30:
13345 return "DW_OP_breg30";
13346 case DW_OP_breg31:
13347 return "DW_OP_breg31";
13348 case DW_OP_regx:
13349 return "DW_OP_regx";
13350 case DW_OP_fbreg:
13351 return "DW_OP_fbreg";
13352 case DW_OP_bregx:
13353 return "DW_OP_bregx";
13354 case DW_OP_piece:
13355 return "DW_OP_piece";
13356 case DW_OP_deref_size:
13357 return "DW_OP_deref_size";
13358 case DW_OP_xderef_size:
13359 return "DW_OP_xderef_size";
13360 case DW_OP_nop:
13361 return "DW_OP_nop";
13362 /* DWARF 3 extensions. */
13363 case DW_OP_push_object_address:
13364 return "DW_OP_push_object_address";
13365 case DW_OP_call2:
13366 return "DW_OP_call2";
13367 case DW_OP_call4:
13368 return "DW_OP_call4";
13369 case DW_OP_call_ref:
13370 return "DW_OP_call_ref";
13371 case DW_OP_form_tls_address:
13372 return "DW_OP_form_tls_address";
13373 case DW_OP_call_frame_cfa:
13374 return "DW_OP_call_frame_cfa";
13375 case DW_OP_bit_piece:
13376 return "DW_OP_bit_piece";
13377 /* DWARF 4 extensions. */
13378 case DW_OP_implicit_value:
13379 return "DW_OP_implicit_value";
13380 case DW_OP_stack_value:
13381 return "DW_OP_stack_value";
13382 /* GNU extensions. */
13383 case DW_OP_GNU_push_tls_address:
13384 return "DW_OP_GNU_push_tls_address";
13385 case DW_OP_GNU_uninit:
13386 return "DW_OP_GNU_uninit";
13387 case DW_OP_GNU_implicit_pointer:
13388 return "DW_OP_GNU_implicit_pointer";
13389 case DW_OP_GNU_entry_value:
13390 return "DW_OP_GNU_entry_value";
13391 case DW_OP_GNU_const_type:
13392 return "DW_OP_GNU_const_type";
13393 case DW_OP_GNU_regval_type:
13394 return "DW_OP_GNU_regval_type";
13395 case DW_OP_GNU_deref_type:
13396 return "DW_OP_GNU_deref_type";
13397 case DW_OP_GNU_convert:
13398 return "DW_OP_GNU_convert";
13399 case DW_OP_GNU_reinterpret:
13400 return "DW_OP_GNU_reinterpret";
13401 default:
13402 return NULL;
13403 }
13404 }
13405
13406 static char *
13407 dwarf_bool_name (unsigned mybool)
13408 {
13409 if (mybool)
13410 return "TRUE";
13411 else
13412 return "FALSE";
13413 }
13414
13415 /* Convert a DWARF type code into its string name. */
13416
13417 static char *
13418 dwarf_type_encoding_name (unsigned enc)
13419 {
13420 switch (enc)
13421 {
13422 case DW_ATE_void:
13423 return "DW_ATE_void";
13424 case DW_ATE_address:
13425 return "DW_ATE_address";
13426 case DW_ATE_boolean:
13427 return "DW_ATE_boolean";
13428 case DW_ATE_complex_float:
13429 return "DW_ATE_complex_float";
13430 case DW_ATE_float:
13431 return "DW_ATE_float";
13432 case DW_ATE_signed:
13433 return "DW_ATE_signed";
13434 case DW_ATE_signed_char:
13435 return "DW_ATE_signed_char";
13436 case DW_ATE_unsigned:
13437 return "DW_ATE_unsigned";
13438 case DW_ATE_unsigned_char:
13439 return "DW_ATE_unsigned_char";
13440 /* DWARF 3. */
13441 case DW_ATE_imaginary_float:
13442 return "DW_ATE_imaginary_float";
13443 case DW_ATE_packed_decimal:
13444 return "DW_ATE_packed_decimal";
13445 case DW_ATE_numeric_string:
13446 return "DW_ATE_numeric_string";
13447 case DW_ATE_edited:
13448 return "DW_ATE_edited";
13449 case DW_ATE_signed_fixed:
13450 return "DW_ATE_signed_fixed";
13451 case DW_ATE_unsigned_fixed:
13452 return "DW_ATE_unsigned_fixed";
13453 case DW_ATE_decimal_float:
13454 return "DW_ATE_decimal_float";
13455 /* DWARF 4. */
13456 case DW_ATE_UTF:
13457 return "DW_ATE_UTF";
13458 /* HP extensions. */
13459 case DW_ATE_HP_float80:
13460 return "DW_ATE_HP_float80";
13461 case DW_ATE_HP_complex_float80:
13462 return "DW_ATE_HP_complex_float80";
13463 case DW_ATE_HP_float128:
13464 return "DW_ATE_HP_float128";
13465 case DW_ATE_HP_complex_float128:
13466 return "DW_ATE_HP_complex_float128";
13467 case DW_ATE_HP_floathpintel:
13468 return "DW_ATE_HP_floathpintel";
13469 case DW_ATE_HP_imaginary_float80:
13470 return "DW_ATE_HP_imaginary_float80";
13471 case DW_ATE_HP_imaginary_float128:
13472 return "DW_ATE_HP_imaginary_float128";
13473 default:
13474 return "DW_ATE_<unknown>";
13475 }
13476 }
13477
13478 /* Convert a DWARF call frame info operation to its string name. */
13479
13480 #if 0
13481 static char *
13482 dwarf_cfi_name (unsigned cfi_opc)
13483 {
13484 switch (cfi_opc)
13485 {
13486 case DW_CFA_advance_loc:
13487 return "DW_CFA_advance_loc";
13488 case DW_CFA_offset:
13489 return "DW_CFA_offset";
13490 case DW_CFA_restore:
13491 return "DW_CFA_restore";
13492 case DW_CFA_nop:
13493 return "DW_CFA_nop";
13494 case DW_CFA_set_loc:
13495 return "DW_CFA_set_loc";
13496 case DW_CFA_advance_loc1:
13497 return "DW_CFA_advance_loc1";
13498 case DW_CFA_advance_loc2:
13499 return "DW_CFA_advance_loc2";
13500 case DW_CFA_advance_loc4:
13501 return "DW_CFA_advance_loc4";
13502 case DW_CFA_offset_extended:
13503 return "DW_CFA_offset_extended";
13504 case DW_CFA_restore_extended:
13505 return "DW_CFA_restore_extended";
13506 case DW_CFA_undefined:
13507 return "DW_CFA_undefined";
13508 case DW_CFA_same_value:
13509 return "DW_CFA_same_value";
13510 case DW_CFA_register:
13511 return "DW_CFA_register";
13512 case DW_CFA_remember_state:
13513 return "DW_CFA_remember_state";
13514 case DW_CFA_restore_state:
13515 return "DW_CFA_restore_state";
13516 case DW_CFA_def_cfa:
13517 return "DW_CFA_def_cfa";
13518 case DW_CFA_def_cfa_register:
13519 return "DW_CFA_def_cfa_register";
13520 case DW_CFA_def_cfa_offset:
13521 return "DW_CFA_def_cfa_offset";
13522 /* DWARF 3. */
13523 case DW_CFA_def_cfa_expression:
13524 return "DW_CFA_def_cfa_expression";
13525 case DW_CFA_expression:
13526 return "DW_CFA_expression";
13527 case DW_CFA_offset_extended_sf:
13528 return "DW_CFA_offset_extended_sf";
13529 case DW_CFA_def_cfa_sf:
13530 return "DW_CFA_def_cfa_sf";
13531 case DW_CFA_def_cfa_offset_sf:
13532 return "DW_CFA_def_cfa_offset_sf";
13533 case DW_CFA_val_offset:
13534 return "DW_CFA_val_offset";
13535 case DW_CFA_val_offset_sf:
13536 return "DW_CFA_val_offset_sf";
13537 case DW_CFA_val_expression:
13538 return "DW_CFA_val_expression";
13539 /* SGI/MIPS specific. */
13540 case DW_CFA_MIPS_advance_loc8:
13541 return "DW_CFA_MIPS_advance_loc8";
13542 /* GNU extensions. */
13543 case DW_CFA_GNU_window_save:
13544 return "DW_CFA_GNU_window_save";
13545 case DW_CFA_GNU_args_size:
13546 return "DW_CFA_GNU_args_size";
13547 case DW_CFA_GNU_negative_offset_extended:
13548 return "DW_CFA_GNU_negative_offset_extended";
13549 default:
13550 return "DW_CFA_<unknown>";
13551 }
13552 }
13553 #endif
13554
13555 static void
13556 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13557 {
13558 unsigned int i;
13559
13560 print_spaces (indent, f);
13561 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13562 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13563
13564 if (die->parent != NULL)
13565 {
13566 print_spaces (indent, f);
13567 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13568 die->parent->offset);
13569 }
13570
13571 print_spaces (indent, f);
13572 fprintf_unfiltered (f, " has children: %s\n",
13573 dwarf_bool_name (die->child != NULL));
13574
13575 print_spaces (indent, f);
13576 fprintf_unfiltered (f, " attributes:\n");
13577
13578 for (i = 0; i < die->num_attrs; ++i)
13579 {
13580 print_spaces (indent, f);
13581 fprintf_unfiltered (f, " %s (%s) ",
13582 dwarf_attr_name (die->attrs[i].name),
13583 dwarf_form_name (die->attrs[i].form));
13584
13585 switch (die->attrs[i].form)
13586 {
13587 case DW_FORM_ref_addr:
13588 case DW_FORM_addr:
13589 fprintf_unfiltered (f, "address: ");
13590 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13591 break;
13592 case DW_FORM_block2:
13593 case DW_FORM_block4:
13594 case DW_FORM_block:
13595 case DW_FORM_block1:
13596 fprintf_unfiltered (f, "block: size %d",
13597 DW_BLOCK (&die->attrs[i])->size);
13598 break;
13599 case DW_FORM_exprloc:
13600 fprintf_unfiltered (f, "expression: size %u",
13601 DW_BLOCK (&die->attrs[i])->size);
13602 break;
13603 case DW_FORM_ref1:
13604 case DW_FORM_ref2:
13605 case DW_FORM_ref4:
13606 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13607 (long) (DW_ADDR (&die->attrs[i])));
13608 break;
13609 case DW_FORM_data1:
13610 case DW_FORM_data2:
13611 case DW_FORM_data4:
13612 case DW_FORM_data8:
13613 case DW_FORM_udata:
13614 case DW_FORM_sdata:
13615 fprintf_unfiltered (f, "constant: %s",
13616 pulongest (DW_UNSND (&die->attrs[i])));
13617 break;
13618 case DW_FORM_sec_offset:
13619 fprintf_unfiltered (f, "section offset: %s",
13620 pulongest (DW_UNSND (&die->attrs[i])));
13621 break;
13622 case DW_FORM_ref_sig8:
13623 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13624 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13625 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13626 else
13627 fprintf_unfiltered (f, "signatured type, offset: unknown");
13628 break;
13629 case DW_FORM_string:
13630 case DW_FORM_strp:
13631 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13632 DW_STRING (&die->attrs[i])
13633 ? DW_STRING (&die->attrs[i]) : "",
13634 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13635 break;
13636 case DW_FORM_flag:
13637 if (DW_UNSND (&die->attrs[i]))
13638 fprintf_unfiltered (f, "flag: TRUE");
13639 else
13640 fprintf_unfiltered (f, "flag: FALSE");
13641 break;
13642 case DW_FORM_flag_present:
13643 fprintf_unfiltered (f, "flag: TRUE");
13644 break;
13645 case DW_FORM_indirect:
13646 /* The reader will have reduced the indirect form to
13647 the "base form" so this form should not occur. */
13648 fprintf_unfiltered (f,
13649 "unexpected attribute form: DW_FORM_indirect");
13650 break;
13651 default:
13652 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13653 die->attrs[i].form);
13654 break;
13655 }
13656 fprintf_unfiltered (f, "\n");
13657 }
13658 }
13659
13660 static void
13661 dump_die_for_error (struct die_info *die)
13662 {
13663 dump_die_shallow (gdb_stderr, 0, die);
13664 }
13665
13666 static void
13667 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13668 {
13669 int indent = level * 4;
13670
13671 gdb_assert (die != NULL);
13672
13673 if (level >= max_level)
13674 return;
13675
13676 dump_die_shallow (f, indent, die);
13677
13678 if (die->child != NULL)
13679 {
13680 print_spaces (indent, f);
13681 fprintf_unfiltered (f, " Children:");
13682 if (level + 1 < max_level)
13683 {
13684 fprintf_unfiltered (f, "\n");
13685 dump_die_1 (f, level + 1, max_level, die->child);
13686 }
13687 else
13688 {
13689 fprintf_unfiltered (f,
13690 " [not printed, max nesting level reached]\n");
13691 }
13692 }
13693
13694 if (die->sibling != NULL && level > 0)
13695 {
13696 dump_die_1 (f, level, max_level, die->sibling);
13697 }
13698 }
13699
13700 /* This is called from the pdie macro in gdbinit.in.
13701 It's not static so gcc will keep a copy callable from gdb. */
13702
13703 void
13704 dump_die (struct die_info *die, int max_level)
13705 {
13706 dump_die_1 (gdb_stdlog, 0, max_level, die);
13707 }
13708
13709 static void
13710 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13711 {
13712 void **slot;
13713
13714 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13715
13716 *slot = die;
13717 }
13718
13719 static int
13720 is_ref_attr (struct attribute *attr)
13721 {
13722 switch (attr->form)
13723 {
13724 case DW_FORM_ref_addr:
13725 case DW_FORM_ref1:
13726 case DW_FORM_ref2:
13727 case DW_FORM_ref4:
13728 case DW_FORM_ref8:
13729 case DW_FORM_ref_udata:
13730 return 1;
13731 default:
13732 return 0;
13733 }
13734 }
13735
13736 static unsigned int
13737 dwarf2_get_ref_die_offset (struct attribute *attr)
13738 {
13739 if (is_ref_attr (attr))
13740 return DW_ADDR (attr);
13741
13742 complaint (&symfile_complaints,
13743 _("unsupported die ref attribute form: '%s'"),
13744 dwarf_form_name (attr->form));
13745 return 0;
13746 }
13747
13748 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13749 * the value held by the attribute is not constant. */
13750
13751 static LONGEST
13752 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13753 {
13754 if (attr->form == DW_FORM_sdata)
13755 return DW_SND (attr);
13756 else if (attr->form == DW_FORM_udata
13757 || attr->form == DW_FORM_data1
13758 || attr->form == DW_FORM_data2
13759 || attr->form == DW_FORM_data4
13760 || attr->form == DW_FORM_data8)
13761 return DW_UNSND (attr);
13762 else
13763 {
13764 complaint (&symfile_complaints,
13765 _("Attribute value is not a constant (%s)"),
13766 dwarf_form_name (attr->form));
13767 return default_value;
13768 }
13769 }
13770
13771 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
13772 unit and add it to our queue.
13773 The result is non-zero if PER_CU was queued, otherwise the result is zero
13774 meaning either PER_CU is already queued or it is already loaded. */
13775
13776 static int
13777 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13778 struct dwarf2_per_cu_data *per_cu)
13779 {
13780 /* We may arrive here during partial symbol reading, if we need full
13781 DIEs to process an unusual case (e.g. template arguments). Do
13782 not queue PER_CU, just tell our caller to load its DIEs. */
13783 if (dwarf2_per_objfile->reading_partial_symbols)
13784 {
13785 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13786 return 1;
13787 return 0;
13788 }
13789
13790 /* Mark the dependence relation so that we don't flush PER_CU
13791 too early. */
13792 dwarf2_add_dependence (this_cu, per_cu);
13793
13794 /* If it's already on the queue, we have nothing to do. */
13795 if (per_cu->queued)
13796 return 0;
13797
13798 /* If the compilation unit is already loaded, just mark it as
13799 used. */
13800 if (per_cu->cu != NULL)
13801 {
13802 per_cu->cu->last_used = 0;
13803 return 0;
13804 }
13805
13806 /* Add it to the queue. */
13807 queue_comp_unit (per_cu, this_cu->objfile);
13808
13809 return 1;
13810 }
13811
13812 /* Follow reference or signature attribute ATTR of SRC_DIE.
13813 On entry *REF_CU is the CU of SRC_DIE.
13814 On exit *REF_CU is the CU of the result. */
13815
13816 static struct die_info *
13817 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13818 struct dwarf2_cu **ref_cu)
13819 {
13820 struct die_info *die;
13821
13822 if (is_ref_attr (attr))
13823 die = follow_die_ref (src_die, attr, ref_cu);
13824 else if (attr->form == DW_FORM_ref_sig8)
13825 die = follow_die_sig (src_die, attr, ref_cu);
13826 else
13827 {
13828 dump_die_for_error (src_die);
13829 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13830 (*ref_cu)->objfile->name);
13831 }
13832
13833 return die;
13834 }
13835
13836 /* Follow reference OFFSET.
13837 On entry *REF_CU is the CU of the source die referencing OFFSET.
13838 On exit *REF_CU is the CU of the result.
13839 Returns NULL if OFFSET is invalid. */
13840
13841 static struct die_info *
13842 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13843 {
13844 struct die_info temp_die;
13845 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13846
13847 gdb_assert (cu->per_cu != NULL);
13848
13849 target_cu = cu;
13850
13851 if (cu->per_cu->debug_type_section)
13852 {
13853 /* .debug_types CUs cannot reference anything outside their CU.
13854 If they need to, they have to reference a signatured type via
13855 DW_FORM_ref_sig8. */
13856 if (! offset_in_cu_p (&cu->header, offset))
13857 return NULL;
13858 }
13859 else if (! offset_in_cu_p (&cu->header, offset))
13860 {
13861 struct dwarf2_per_cu_data *per_cu;
13862
13863 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13864
13865 /* If necessary, add it to the queue and load its DIEs. */
13866 if (maybe_queue_comp_unit (cu, per_cu))
13867 load_full_comp_unit (per_cu, cu->objfile);
13868
13869 target_cu = per_cu->cu;
13870 }
13871 else if (cu->dies == NULL)
13872 {
13873 /* We're loading full DIEs during partial symbol reading. */
13874 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13875 load_full_comp_unit (cu->per_cu, cu->objfile);
13876 }
13877
13878 *ref_cu = target_cu;
13879 temp_die.offset = offset;
13880 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13881 }
13882
13883 /* Follow reference attribute ATTR of SRC_DIE.
13884 On entry *REF_CU is the CU of SRC_DIE.
13885 On exit *REF_CU is the CU of the result. */
13886
13887 static struct die_info *
13888 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13889 struct dwarf2_cu **ref_cu)
13890 {
13891 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13892 struct dwarf2_cu *cu = *ref_cu;
13893 struct die_info *die;
13894
13895 die = follow_die_offset (offset, ref_cu);
13896 if (!die)
13897 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13898 "at 0x%x [in module %s]"),
13899 offset, src_die->offset, cu->objfile->name);
13900
13901 return die;
13902 }
13903
13904 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13905 value is intended for DW_OP_call*. You must call xfree on returned
13906 dwarf2_locexpr_baton->data. */
13907
13908 struct dwarf2_locexpr_baton
13909 dwarf2_fetch_die_location_block (unsigned int offset,
13910 struct dwarf2_per_cu_data *per_cu,
13911 CORE_ADDR (*get_frame_pc) (void *baton),
13912 void *baton)
13913 {
13914 struct dwarf2_cu *cu;
13915 struct die_info *die;
13916 struct attribute *attr;
13917 struct dwarf2_locexpr_baton retval;
13918
13919 dw2_setup (per_cu->objfile);
13920
13921 if (per_cu->cu == NULL)
13922 load_cu (per_cu);
13923 cu = per_cu->cu;
13924
13925 die = follow_die_offset (offset, &cu);
13926 if (!die)
13927 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13928 offset, per_cu->cu->objfile->name);
13929
13930 attr = dwarf2_attr (die, DW_AT_location, cu);
13931 if (!attr)
13932 {
13933 /* DWARF: "If there is no such attribute, then there is no effect.". */
13934
13935 retval.data = NULL;
13936 retval.size = 0;
13937 }
13938 else if (attr_form_is_section_offset (attr))
13939 {
13940 struct dwarf2_loclist_baton loclist_baton;
13941 CORE_ADDR pc = (*get_frame_pc) (baton);
13942 size_t size;
13943
13944 fill_in_loclist_baton (cu, &loclist_baton, attr);
13945
13946 retval.data = dwarf2_find_location_expression (&loclist_baton,
13947 &size, pc);
13948 retval.size = size;
13949 }
13950 else
13951 {
13952 if (!attr_form_is_block (attr))
13953 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13954 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13955 offset, per_cu->cu->objfile->name);
13956
13957 retval.data = DW_BLOCK (attr)->data;
13958 retval.size = DW_BLOCK (attr)->size;
13959 }
13960 retval.per_cu = cu->per_cu;
13961
13962 if (retval.data)
13963 retval.data = xmemdup (retval.data, retval.size, retval.size);
13964
13965 age_cached_comp_units ();
13966
13967 return retval;
13968 }
13969
13970 /* Return the type of the DIE at DIE_OFFSET in the CU named by
13971 PER_CU. */
13972
13973 struct type *
13974 dwarf2_get_die_type (unsigned int die_offset,
13975 struct dwarf2_per_cu_data *per_cu)
13976 {
13977 dw2_setup (per_cu->objfile);
13978 return get_die_type_at_offset (die_offset, per_cu);
13979 }
13980
13981 /* Follow the signature attribute ATTR in SRC_DIE.
13982 On entry *REF_CU is the CU of SRC_DIE.
13983 On exit *REF_CU is the CU of the result. */
13984
13985 static struct die_info *
13986 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13987 struct dwarf2_cu **ref_cu)
13988 {
13989 struct objfile *objfile = (*ref_cu)->objfile;
13990 struct die_info temp_die;
13991 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13992 struct dwarf2_cu *sig_cu;
13993 struct die_info *die;
13994
13995 /* sig_type will be NULL if the signatured type is missing from
13996 the debug info. */
13997 if (sig_type == NULL)
13998 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13999 "at 0x%x [in module %s]"),
14000 src_die->offset, objfile->name);
14001
14002 /* If necessary, add it to the queue and load its DIEs. */
14003
14004 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14005 read_signatured_type (objfile, sig_type);
14006
14007 gdb_assert (sig_type->per_cu.cu != NULL);
14008
14009 sig_cu = sig_type->per_cu.cu;
14010 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14011 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14012 if (die)
14013 {
14014 *ref_cu = sig_cu;
14015 return die;
14016 }
14017
14018 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14019 "from DIE at 0x%x [in module %s]"),
14020 sig_type->type_offset, src_die->offset, objfile->name);
14021 }
14022
14023 /* Given an offset of a signatured type, return its signatured_type. */
14024
14025 static struct signatured_type *
14026 lookup_signatured_type_at_offset (struct objfile *objfile,
14027 struct dwarf2_section_info *section,
14028 unsigned int offset)
14029 {
14030 gdb_byte *info_ptr = section->buffer + offset;
14031 unsigned int length, initial_length_size;
14032 unsigned int sig_offset;
14033 struct signatured_type find_entry, *type_sig;
14034
14035 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14036 sig_offset = (initial_length_size
14037 + 2 /*version*/
14038 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14039 + 1 /*address_size*/);
14040 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14041 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14042
14043 /* This is only used to lookup previously recorded types.
14044 If we didn't find it, it's our bug. */
14045 gdb_assert (type_sig != NULL);
14046 gdb_assert (offset == type_sig->per_cu.offset);
14047
14048 return type_sig;
14049 }
14050
14051 /* Read in signatured type at OFFSET and build its CU and die(s). */
14052
14053 static void
14054 read_signatured_type_at_offset (struct objfile *objfile,
14055 struct dwarf2_section_info *sect,
14056 unsigned int offset)
14057 {
14058 struct signatured_type *type_sig;
14059
14060 dwarf2_read_section (objfile, sect);
14061
14062 /* We have the section offset, but we need the signature to do the
14063 hash table lookup. */
14064 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14065
14066 gdb_assert (type_sig->per_cu.cu == NULL);
14067
14068 read_signatured_type (objfile, type_sig);
14069
14070 gdb_assert (type_sig->per_cu.cu != NULL);
14071 }
14072
14073 /* Read in a signatured type and build its CU and DIEs. */
14074
14075 static void
14076 read_signatured_type (struct objfile *objfile,
14077 struct signatured_type *type_sig)
14078 {
14079 gdb_byte *types_ptr;
14080 struct die_reader_specs reader_specs;
14081 struct dwarf2_cu *cu;
14082 ULONGEST signature;
14083 struct cleanup *back_to, *free_cu_cleanup;
14084 struct dwarf2_section_info *section = type_sig->per_cu.debug_type_section;
14085
14086 dwarf2_read_section (objfile, section);
14087 types_ptr = section->buffer + type_sig->per_cu.offset;
14088
14089 gdb_assert (type_sig->per_cu.cu == NULL);
14090
14091 cu = xmalloc (sizeof (*cu));
14092 init_one_comp_unit (cu, objfile);
14093
14094 type_sig->per_cu.cu = cu;
14095 cu->per_cu = &type_sig->per_cu;
14096
14097 /* If an error occurs while loading, release our storage. */
14098 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
14099
14100 types_ptr = read_type_comp_unit_head (&cu->header, section, &signature,
14101 types_ptr, objfile->obfd);
14102 gdb_assert (signature == type_sig->signature);
14103
14104 cu->die_hash
14105 = htab_create_alloc_ex (cu->header.length / 12,
14106 die_hash,
14107 die_eq,
14108 NULL,
14109 &cu->comp_unit_obstack,
14110 hashtab_obstack_allocate,
14111 dummy_obstack_deallocate);
14112
14113 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
14114 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14115
14116 init_cu_die_reader (&reader_specs, cu);
14117
14118 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14119 NULL /*parent*/);
14120
14121 /* We try not to read any attributes in this function, because not
14122 all objfiles needed for references have been loaded yet, and symbol
14123 table processing isn't initialized. But we have to set the CU language,
14124 or we won't be able to build types correctly. */
14125 prepare_one_comp_unit (cu, cu->dies);
14126
14127 do_cleanups (back_to);
14128
14129 /* We've successfully allocated this compilation unit. Let our caller
14130 clean it up when finished with it. */
14131 discard_cleanups (free_cu_cleanup);
14132
14133 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14134 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14135 }
14136
14137 /* Workaround as dwarf_expr_context_funcs.read_mem implementation before
14138 a proper runtime DWARF expressions evaluator gets implemented.
14139 Otherwise gnuv3_baseclass_offset would error by:
14140 Expected a negative vbase offset (old compiler?) */
14141
14142 static void
14143 decode_locdesc_read_mem (void *baton, gdb_byte *buf, CORE_ADDR addr,
14144 size_t length)
14145 {
14146 struct dwarf_expr_context *ctx = baton;
14147 struct gdbarch *gdbarch = ctx->gdbarch;
14148 struct type *ptr_type = builtin_type (gdbarch)->builtin_data_ptr;
14149
14150 memset (buf, 0, length);
14151
14152 if (TYPE_LENGTH (ptr_type) == length)
14153 store_typed_address (buf, ptr_type, addr);
14154 }
14155
14156 static const struct dwarf_expr_context_funcs decode_locdesc_ctx_funcs =
14157 {
14158 ctx_no_read_reg,
14159 decode_locdesc_read_mem,
14160 ctx_no_get_frame_base,
14161 ctx_no_get_frame_cfa,
14162 ctx_no_get_frame_pc,
14163 ctx_no_get_tls_address,
14164 ctx_no_dwarf_call,
14165 ctx_no_get_base_type
14166 };
14167
14168 /* Decode simple location descriptions.
14169 Given a pointer to a dwarf block that defines a location, compute
14170 the location and return the value.
14171
14172 NOTE drow/2003-11-18: This function is called in two situations
14173 now: for the address of static or global variables (partial symbols
14174 only) and for offsets into structures which are expected to be
14175 (more or less) constant. The partial symbol case should go away,
14176 and only the constant case should remain. That will let this
14177 function complain more accurately. A few special modes are allowed
14178 without complaint for global variables (for instance, global
14179 register values and thread-local values).
14180
14181 A location description containing no operations indicates that the
14182 object is optimized out. The return value is 0 for that case.
14183 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14184 callers will only want a very basic result and this can become a
14185 complaint. */
14186
14187 static CORE_ADDR
14188 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14189 {
14190 struct objfile *objfile = cu->objfile;
14191 struct dwarf_expr_context *ctx;
14192 struct cleanup *old_chain;
14193 volatile struct gdb_exception ex;
14194
14195 ctx = new_dwarf_expr_context ();
14196 old_chain = make_cleanup_free_dwarf_expr_context (ctx);
14197 make_cleanup_value_free_to_mark (value_mark ());
14198
14199 ctx->gdbarch = get_objfile_arch (objfile);
14200 ctx->addr_size = cu->header.addr_size;
14201 ctx->offset = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14202 ctx->baton = ctx;
14203 ctx->funcs = &decode_locdesc_ctx_funcs;
14204
14205 /* DW_AT_data_member_location expects the structure address to be pushed on
14206 the stack. Simulate the offset by address 0. */
14207 dwarf_expr_push_address (ctx, 0, 0);
14208
14209 TRY_CATCH (ex, RETURN_MASK_ERROR)
14210 {
14211 dwarf_expr_eval (ctx, blk->data, blk->size);
14212 }
14213 if (ex.reason < 0)
14214 {
14215 if (ex.message)
14216 complaint (&symfile_complaints, "%s", ex.message);
14217 }
14218 else if (ctx->num_pieces == 0)
14219 switch (ctx->location)
14220 {
14221 /* The returned number will be bogus, just do not complain for locations
14222 in global registers - it is here only a partial symbol address. */
14223 case DWARF_VALUE_REGISTER:
14224
14225 case DWARF_VALUE_MEMORY:
14226 case DWARF_VALUE_STACK:
14227 {
14228 CORE_ADDR address = dwarf_expr_fetch_address (ctx, 0);
14229
14230 do_cleanups (old_chain);
14231 return address;
14232 }
14233 }
14234
14235 do_cleanups (old_chain);
14236 dwarf2_complex_location_expr_complaint ();
14237 return 0;
14238 }
14239
14240 /* memory allocation interface */
14241
14242 static struct dwarf_block *
14243 dwarf_alloc_block (struct dwarf2_cu *cu)
14244 {
14245 struct dwarf_block *blk;
14246
14247 blk = (struct dwarf_block *)
14248 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14249 return (blk);
14250 }
14251
14252 static struct abbrev_info *
14253 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14254 {
14255 struct abbrev_info *abbrev;
14256
14257 abbrev = (struct abbrev_info *)
14258 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14259 memset (abbrev, 0, sizeof (struct abbrev_info));
14260 return (abbrev);
14261 }
14262
14263 static struct die_info *
14264 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14265 {
14266 struct die_info *die;
14267 size_t size = sizeof (struct die_info);
14268
14269 if (num_attrs > 1)
14270 size += (num_attrs - 1) * sizeof (struct attribute);
14271
14272 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14273 memset (die, 0, sizeof (struct die_info));
14274 return (die);
14275 }
14276
14277 \f
14278 /* Macro support. */
14279
14280 /* Return the full name of file number I in *LH's file name table.
14281 Use COMP_DIR as the name of the current directory of the
14282 compilation. The result is allocated using xmalloc; the caller is
14283 responsible for freeing it. */
14284 static char *
14285 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14286 {
14287 /* Is the file number a valid index into the line header's file name
14288 table? Remember that file numbers start with one, not zero. */
14289 if (1 <= file && file <= lh->num_file_names)
14290 {
14291 struct file_entry *fe = &lh->file_names[file - 1];
14292
14293 if (IS_ABSOLUTE_PATH (fe->name))
14294 return xstrdup (fe->name);
14295 else
14296 {
14297 const char *dir;
14298 int dir_len;
14299 char *full_name;
14300
14301 if (fe->dir_index)
14302 dir = lh->include_dirs[fe->dir_index - 1];
14303 else
14304 dir = comp_dir;
14305
14306 if (dir)
14307 {
14308 dir_len = strlen (dir);
14309 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14310 strcpy (full_name, dir);
14311 full_name[dir_len] = '/';
14312 strcpy (full_name + dir_len + 1, fe->name);
14313 return full_name;
14314 }
14315 else
14316 return xstrdup (fe->name);
14317 }
14318 }
14319 else
14320 {
14321 /* The compiler produced a bogus file number. We can at least
14322 record the macro definitions made in the file, even if we
14323 won't be able to find the file by name. */
14324 char fake_name[80];
14325
14326 sprintf (fake_name, "<bad macro file number %d>", file);
14327
14328 complaint (&symfile_complaints,
14329 _("bad file number in macro information (%d)"),
14330 file);
14331
14332 return xstrdup (fake_name);
14333 }
14334 }
14335
14336
14337 static struct macro_source_file *
14338 macro_start_file (int file, int line,
14339 struct macro_source_file *current_file,
14340 const char *comp_dir,
14341 struct line_header *lh, struct objfile *objfile)
14342 {
14343 /* The full name of this source file. */
14344 char *full_name = file_full_name (file, lh, comp_dir);
14345
14346 /* We don't create a macro table for this compilation unit
14347 at all until we actually get a filename. */
14348 if (! pending_macros)
14349 pending_macros = new_macro_table (&objfile->objfile_obstack,
14350 objfile->macro_cache);
14351
14352 if (! current_file)
14353 /* If we have no current file, then this must be the start_file
14354 directive for the compilation unit's main source file. */
14355 current_file = macro_set_main (pending_macros, full_name);
14356 else
14357 current_file = macro_include (current_file, line, full_name);
14358
14359 xfree (full_name);
14360
14361 return current_file;
14362 }
14363
14364
14365 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14366 followed by a null byte. */
14367 static char *
14368 copy_string (const char *buf, int len)
14369 {
14370 char *s = xmalloc (len + 1);
14371
14372 memcpy (s, buf, len);
14373 s[len] = '\0';
14374 return s;
14375 }
14376
14377
14378 static const char *
14379 consume_improper_spaces (const char *p, const char *body)
14380 {
14381 if (*p == ' ')
14382 {
14383 complaint (&symfile_complaints,
14384 _("macro definition contains spaces "
14385 "in formal argument list:\n`%s'"),
14386 body);
14387
14388 while (*p == ' ')
14389 p++;
14390 }
14391
14392 return p;
14393 }
14394
14395
14396 static void
14397 parse_macro_definition (struct macro_source_file *file, int line,
14398 const char *body)
14399 {
14400 const char *p;
14401
14402 /* The body string takes one of two forms. For object-like macro
14403 definitions, it should be:
14404
14405 <macro name> " " <definition>
14406
14407 For function-like macro definitions, it should be:
14408
14409 <macro name> "() " <definition>
14410 or
14411 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14412
14413 Spaces may appear only where explicitly indicated, and in the
14414 <definition>.
14415
14416 The Dwarf 2 spec says that an object-like macro's name is always
14417 followed by a space, but versions of GCC around March 2002 omit
14418 the space when the macro's definition is the empty string.
14419
14420 The Dwarf 2 spec says that there should be no spaces between the
14421 formal arguments in a function-like macro's formal argument list,
14422 but versions of GCC around March 2002 include spaces after the
14423 commas. */
14424
14425
14426 /* Find the extent of the macro name. The macro name is terminated
14427 by either a space or null character (for an object-like macro) or
14428 an opening paren (for a function-like macro). */
14429 for (p = body; *p; p++)
14430 if (*p == ' ' || *p == '(')
14431 break;
14432
14433 if (*p == ' ' || *p == '\0')
14434 {
14435 /* It's an object-like macro. */
14436 int name_len = p - body;
14437 char *name = copy_string (body, name_len);
14438 const char *replacement;
14439
14440 if (*p == ' ')
14441 replacement = body + name_len + 1;
14442 else
14443 {
14444 dwarf2_macro_malformed_definition_complaint (body);
14445 replacement = body + name_len;
14446 }
14447
14448 macro_define_object (file, line, name, replacement);
14449
14450 xfree (name);
14451 }
14452 else if (*p == '(')
14453 {
14454 /* It's a function-like macro. */
14455 char *name = copy_string (body, p - body);
14456 int argc = 0;
14457 int argv_size = 1;
14458 char **argv = xmalloc (argv_size * sizeof (*argv));
14459
14460 p++;
14461
14462 p = consume_improper_spaces (p, body);
14463
14464 /* Parse the formal argument list. */
14465 while (*p && *p != ')')
14466 {
14467 /* Find the extent of the current argument name. */
14468 const char *arg_start = p;
14469
14470 while (*p && *p != ',' && *p != ')' && *p != ' ')
14471 p++;
14472
14473 if (! *p || p == arg_start)
14474 dwarf2_macro_malformed_definition_complaint (body);
14475 else
14476 {
14477 /* Make sure argv has room for the new argument. */
14478 if (argc >= argv_size)
14479 {
14480 argv_size *= 2;
14481 argv = xrealloc (argv, argv_size * sizeof (*argv));
14482 }
14483
14484 argv[argc++] = copy_string (arg_start, p - arg_start);
14485 }
14486
14487 p = consume_improper_spaces (p, body);
14488
14489 /* Consume the comma, if present. */
14490 if (*p == ',')
14491 {
14492 p++;
14493
14494 p = consume_improper_spaces (p, body);
14495 }
14496 }
14497
14498 if (*p == ')')
14499 {
14500 p++;
14501
14502 if (*p == ' ')
14503 /* Perfectly formed definition, no complaints. */
14504 macro_define_function (file, line, name,
14505 argc, (const char **) argv,
14506 p + 1);
14507 else if (*p == '\0')
14508 {
14509 /* Complain, but do define it. */
14510 dwarf2_macro_malformed_definition_complaint (body);
14511 macro_define_function (file, line, name,
14512 argc, (const char **) argv,
14513 p);
14514 }
14515 else
14516 /* Just complain. */
14517 dwarf2_macro_malformed_definition_complaint (body);
14518 }
14519 else
14520 /* Just complain. */
14521 dwarf2_macro_malformed_definition_complaint (body);
14522
14523 xfree (name);
14524 {
14525 int i;
14526
14527 for (i = 0; i < argc; i++)
14528 xfree (argv[i]);
14529 }
14530 xfree (argv);
14531 }
14532 else
14533 dwarf2_macro_malformed_definition_complaint (body);
14534 }
14535
14536 /* Skip some bytes from BYTES according to the form given in FORM.
14537 Returns the new pointer. */
14538
14539 static gdb_byte *
14540 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
14541 enum dwarf_form form,
14542 unsigned int offset_size,
14543 struct dwarf2_section_info *section)
14544 {
14545 unsigned int bytes_read;
14546
14547 switch (form)
14548 {
14549 case DW_FORM_data1:
14550 case DW_FORM_flag:
14551 ++bytes;
14552 break;
14553
14554 case DW_FORM_data2:
14555 bytes += 2;
14556 break;
14557
14558 case DW_FORM_data4:
14559 bytes += 4;
14560 break;
14561
14562 case DW_FORM_data8:
14563 bytes += 8;
14564 break;
14565
14566 case DW_FORM_string:
14567 read_direct_string (abfd, bytes, &bytes_read);
14568 bytes += bytes_read;
14569 break;
14570
14571 case DW_FORM_sec_offset:
14572 case DW_FORM_strp:
14573 bytes += offset_size;
14574 break;
14575
14576 case DW_FORM_block:
14577 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
14578 bytes += bytes_read;
14579 break;
14580
14581 case DW_FORM_block1:
14582 bytes += 1 + read_1_byte (abfd, bytes);
14583 break;
14584 case DW_FORM_block2:
14585 bytes += 2 + read_2_bytes (abfd, bytes);
14586 break;
14587 case DW_FORM_block4:
14588 bytes += 4 + read_4_bytes (abfd, bytes);
14589 break;
14590
14591 case DW_FORM_sdata:
14592 case DW_FORM_udata:
14593 bytes = skip_leb128 (abfd, bytes);
14594 break;
14595
14596 default:
14597 {
14598 complain:
14599 complaint (&symfile_complaints,
14600 _("invalid form 0x%x in `%s'"),
14601 form,
14602 section->asection->name);
14603 return NULL;
14604 }
14605 }
14606
14607 return bytes;
14608 }
14609
14610 /* A helper for dwarf_decode_macros that handles skipping an unknown
14611 opcode. Returns an updated pointer to the macro data buffer; or,
14612 on error, issues a complaint and returns NULL. */
14613
14614 static gdb_byte *
14615 skip_unknown_opcode (unsigned int opcode,
14616 gdb_byte **opcode_definitions,
14617 gdb_byte *mac_ptr,
14618 bfd *abfd,
14619 unsigned int offset_size,
14620 struct dwarf2_section_info *section)
14621 {
14622 unsigned int bytes_read, i;
14623 unsigned long arg;
14624 gdb_byte *defn;
14625
14626 if (opcode_definitions[opcode] == NULL)
14627 {
14628 complaint (&symfile_complaints,
14629 _("unrecognized DW_MACFINO opcode 0x%x"),
14630 opcode);
14631 return NULL;
14632 }
14633
14634 defn = opcode_definitions[opcode];
14635 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
14636 defn += bytes_read;
14637
14638 for (i = 0; i < arg; ++i)
14639 {
14640 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
14641 if (mac_ptr == NULL)
14642 {
14643 /* skip_form_bytes already issued the complaint. */
14644 return NULL;
14645 }
14646 }
14647
14648 return mac_ptr;
14649 }
14650
14651 /* A helper function which parses the header of a macro section.
14652 If the macro section is the extended (for now called "GNU") type,
14653 then this updates *OFFSET_SIZE. Returns a pointer to just after
14654 the header, or issues a complaint and returns NULL on error. */
14655
14656 static gdb_byte *
14657 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
14658 bfd *abfd,
14659 gdb_byte *mac_ptr,
14660 unsigned int *offset_size,
14661 int section_is_gnu)
14662 {
14663 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
14664
14665 if (section_is_gnu)
14666 {
14667 unsigned int version, flags;
14668
14669 version = read_2_bytes (abfd, mac_ptr);
14670 if (version != 4)
14671 {
14672 complaint (&symfile_complaints,
14673 _("unrecognized version `%d' in .debug_macro section"),
14674 version);
14675 return NULL;
14676 }
14677 mac_ptr += 2;
14678
14679 flags = read_1_byte (abfd, mac_ptr);
14680 ++mac_ptr;
14681 *offset_size = (flags & 1) ? 8 : 4;
14682
14683 if ((flags & 2) != 0)
14684 /* We don't need the line table offset. */
14685 mac_ptr += *offset_size;
14686
14687 /* Vendor opcode descriptions. */
14688 if ((flags & 4) != 0)
14689 {
14690 unsigned int i, count;
14691
14692 count = read_1_byte (abfd, mac_ptr);
14693 ++mac_ptr;
14694 for (i = 0; i < count; ++i)
14695 {
14696 unsigned int opcode, bytes_read;
14697 unsigned long arg;
14698
14699 opcode = read_1_byte (abfd, mac_ptr);
14700 ++mac_ptr;
14701 opcode_definitions[opcode] = mac_ptr;
14702 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14703 mac_ptr += bytes_read;
14704 mac_ptr += arg;
14705 }
14706 }
14707 }
14708
14709 return mac_ptr;
14710 }
14711
14712 /* A helper for dwarf_decode_macros that handles the GNU extensions,
14713 including DW_GNU_MACINFO_transparent_include. */
14714
14715 static void
14716 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
14717 struct macro_source_file *current_file,
14718 struct line_header *lh, char *comp_dir,
14719 struct dwarf2_section_info *section,
14720 int section_is_gnu,
14721 unsigned int offset_size,
14722 struct objfile *objfile)
14723 {
14724 enum dwarf_macro_record_type macinfo_type;
14725 int at_commandline;
14726 gdb_byte *opcode_definitions[256];
14727
14728 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
14729 &offset_size, section_is_gnu);
14730 if (mac_ptr == NULL)
14731 {
14732 /* We already issued a complaint. */
14733 return;
14734 }
14735
14736 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14737 GDB is still reading the definitions from command line. First
14738 DW_MACINFO_start_file will need to be ignored as it was already executed
14739 to create CURRENT_FILE for the main source holding also the command line
14740 definitions. On first met DW_MACINFO_start_file this flag is reset to
14741 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14742
14743 at_commandline = 1;
14744
14745 do
14746 {
14747 /* Do we at least have room for a macinfo type byte? */
14748 if (mac_ptr >= mac_end)
14749 {
14750 dwarf2_macros_too_long_complaint (section);
14751 break;
14752 }
14753
14754 macinfo_type = read_1_byte (abfd, mac_ptr);
14755 mac_ptr++;
14756
14757 /* Note that we rely on the fact that the corresponding GNU and
14758 DWARF constants are the same. */
14759 switch (macinfo_type)
14760 {
14761 /* A zero macinfo type indicates the end of the macro
14762 information. */
14763 case 0:
14764 break;
14765
14766 case DW_MACRO_GNU_define:
14767 case DW_MACRO_GNU_undef:
14768 case DW_MACRO_GNU_define_indirect:
14769 case DW_MACRO_GNU_undef_indirect:
14770 {
14771 unsigned int bytes_read;
14772 int line;
14773 char *body;
14774 int is_define;
14775
14776 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14777 mac_ptr += bytes_read;
14778
14779 if (macinfo_type == DW_MACRO_GNU_define
14780 || macinfo_type == DW_MACRO_GNU_undef)
14781 {
14782 body = read_direct_string (abfd, mac_ptr, &bytes_read);
14783 mac_ptr += bytes_read;
14784 }
14785 else
14786 {
14787 LONGEST str_offset;
14788
14789 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
14790 mac_ptr += offset_size;
14791
14792 body = read_indirect_string_at_offset (abfd, str_offset);
14793 }
14794
14795 is_define = (macinfo_type == DW_MACRO_GNU_define
14796 || macinfo_type == DW_MACRO_GNU_define_indirect);
14797 if (! current_file)
14798 {
14799 /* DWARF violation as no main source is present. */
14800 complaint (&symfile_complaints,
14801 _("debug info with no main source gives macro %s "
14802 "on line %d: %s"),
14803 is_define ? _("definition") : _("undefinition"),
14804 line, body);
14805 break;
14806 }
14807 if ((line == 0 && !at_commandline)
14808 || (line != 0 && at_commandline))
14809 complaint (&symfile_complaints,
14810 _("debug info gives %s macro %s with %s line %d: %s"),
14811 at_commandline ? _("command-line") : _("in-file"),
14812 is_define ? _("definition") : _("undefinition"),
14813 line == 0 ? _("zero") : _("non-zero"), line, body);
14814
14815 if (is_define)
14816 parse_macro_definition (current_file, line, body);
14817 else
14818 {
14819 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
14820 || macinfo_type == DW_MACRO_GNU_undef_indirect);
14821 macro_undef (current_file, line, body);
14822 }
14823 }
14824 break;
14825
14826 case DW_MACRO_GNU_start_file:
14827 {
14828 unsigned int bytes_read;
14829 int line, file;
14830
14831 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14832 mac_ptr += bytes_read;
14833 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14834 mac_ptr += bytes_read;
14835
14836 if ((line == 0 && !at_commandline)
14837 || (line != 0 && at_commandline))
14838 complaint (&symfile_complaints,
14839 _("debug info gives source %d included "
14840 "from %s at %s line %d"),
14841 file, at_commandline ? _("command-line") : _("file"),
14842 line == 0 ? _("zero") : _("non-zero"), line);
14843
14844 if (at_commandline)
14845 {
14846 /* This DW_MACRO_GNU_start_file was executed in the
14847 pass one. */
14848 at_commandline = 0;
14849 }
14850 else
14851 current_file = macro_start_file (file, line,
14852 current_file, comp_dir,
14853 lh, objfile);
14854 }
14855 break;
14856
14857 case DW_MACRO_GNU_end_file:
14858 if (! current_file)
14859 complaint (&symfile_complaints,
14860 _("macro debug info has an unmatched "
14861 "`close_file' directive"));
14862 else
14863 {
14864 current_file = current_file->included_by;
14865 if (! current_file)
14866 {
14867 enum dwarf_macro_record_type next_type;
14868
14869 /* GCC circa March 2002 doesn't produce the zero
14870 type byte marking the end of the compilation
14871 unit. Complain if it's not there, but exit no
14872 matter what. */
14873
14874 /* Do we at least have room for a macinfo type byte? */
14875 if (mac_ptr >= mac_end)
14876 {
14877 dwarf2_macros_too_long_complaint (section);
14878 return;
14879 }
14880
14881 /* We don't increment mac_ptr here, so this is just
14882 a look-ahead. */
14883 next_type = read_1_byte (abfd, mac_ptr);
14884 if (next_type != 0)
14885 complaint (&symfile_complaints,
14886 _("no terminating 0-type entry for "
14887 "macros in `.debug_macinfo' section"));
14888
14889 return;
14890 }
14891 }
14892 break;
14893
14894 case DW_MACRO_GNU_transparent_include:
14895 {
14896 LONGEST offset;
14897
14898 offset = read_offset_1 (abfd, mac_ptr, offset_size);
14899 mac_ptr += offset_size;
14900
14901 dwarf_decode_macro_bytes (abfd,
14902 section->buffer + offset,
14903 mac_end, current_file,
14904 lh, comp_dir,
14905 section, section_is_gnu,
14906 offset_size, objfile);
14907 }
14908 break;
14909
14910 case DW_MACINFO_vendor_ext:
14911 if (!section_is_gnu)
14912 {
14913 unsigned int bytes_read;
14914 int constant;
14915
14916 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14917 mac_ptr += bytes_read;
14918 read_direct_string (abfd, mac_ptr, &bytes_read);
14919 mac_ptr += bytes_read;
14920
14921 /* We don't recognize any vendor extensions. */
14922 break;
14923 }
14924 /* FALLTHROUGH */
14925
14926 default:
14927 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
14928 mac_ptr, abfd, offset_size,
14929 section);
14930 if (mac_ptr == NULL)
14931 return;
14932 break;
14933 }
14934 } while (macinfo_type != 0);
14935 }
14936
14937 static void
14938 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14939 char *comp_dir, bfd *abfd,
14940 struct dwarf2_cu *cu,
14941 struct dwarf2_section_info *section,
14942 int section_is_gnu)
14943 {
14944 gdb_byte *mac_ptr, *mac_end;
14945 struct macro_source_file *current_file = 0;
14946 enum dwarf_macro_record_type macinfo_type;
14947 unsigned int offset_size = cu->header.offset_size;
14948 gdb_byte *opcode_definitions[256];
14949
14950 dwarf2_read_section (dwarf2_per_objfile->objfile, section);
14951 if (section->buffer == NULL)
14952 {
14953 complaint (&symfile_complaints, _("missing %s section"),
14954 section->asection->name);
14955 return;
14956 }
14957
14958 /* First pass: Find the name of the base filename.
14959 This filename is needed in order to process all macros whose definition
14960 (or undefinition) comes from the command line. These macros are defined
14961 before the first DW_MACINFO_start_file entry, and yet still need to be
14962 associated to the base file.
14963
14964 To determine the base file name, we scan the macro definitions until we
14965 reach the first DW_MACINFO_start_file entry. We then initialize
14966 CURRENT_FILE accordingly so that any macro definition found before the
14967 first DW_MACINFO_start_file can still be associated to the base file. */
14968
14969 mac_ptr = section->buffer + offset;
14970 mac_end = section->buffer + section->size;
14971
14972 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
14973 &offset_size, section_is_gnu);
14974 if (mac_ptr == NULL)
14975 {
14976 /* We already issued a complaint. */
14977 return;
14978 }
14979
14980 do
14981 {
14982 /* Do we at least have room for a macinfo type byte? */
14983 if (mac_ptr >= mac_end)
14984 {
14985 /* Complaint is printed during the second pass as GDB will probably
14986 stop the first pass earlier upon finding
14987 DW_MACINFO_start_file. */
14988 break;
14989 }
14990
14991 macinfo_type = read_1_byte (abfd, mac_ptr);
14992 mac_ptr++;
14993
14994 /* Note that we rely on the fact that the corresponding GNU and
14995 DWARF constants are the same. */
14996 switch (macinfo_type)
14997 {
14998 /* A zero macinfo type indicates the end of the macro
14999 information. */
15000 case 0:
15001 break;
15002
15003 case DW_MACRO_GNU_define:
15004 case DW_MACRO_GNU_undef:
15005 /* Only skip the data by MAC_PTR. */
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 break;
15015
15016 case DW_MACRO_GNU_start_file:
15017 {
15018 unsigned int bytes_read;
15019 int line, file;
15020
15021 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15022 mac_ptr += bytes_read;
15023 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15024 mac_ptr += bytes_read;
15025
15026 current_file = macro_start_file (file, line, current_file,
15027 comp_dir, lh, cu->objfile);
15028 }
15029 break;
15030
15031 case DW_MACRO_GNU_end_file:
15032 /* No data to skip by MAC_PTR. */
15033 break;
15034
15035 case DW_MACRO_GNU_define_indirect:
15036 case DW_MACRO_GNU_undef_indirect:
15037 {
15038 unsigned int bytes_read;
15039
15040 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15041 mac_ptr += bytes_read;
15042 mac_ptr += offset_size;
15043 }
15044 break;
15045
15046 case DW_MACRO_GNU_transparent_include:
15047 /* Note that, according to the spec, a transparent include
15048 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15049 skip this opcode. */
15050 mac_ptr += offset_size;
15051 break;
15052
15053 case DW_MACINFO_vendor_ext:
15054 /* Only skip the data by MAC_PTR. */
15055 if (!section_is_gnu)
15056 {
15057 unsigned int bytes_read;
15058
15059 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15060 mac_ptr += bytes_read;
15061 read_direct_string (abfd, mac_ptr, &bytes_read);
15062 mac_ptr += bytes_read;
15063 }
15064 /* FALLTHROUGH */
15065
15066 default:
15067 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15068 mac_ptr, abfd, offset_size,
15069 section);
15070 if (mac_ptr == NULL)
15071 return;
15072 break;
15073 }
15074 } while (macinfo_type != 0 && current_file == NULL);
15075
15076 /* Second pass: Process all entries.
15077
15078 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15079 command-line macro definitions/undefinitions. This flag is unset when we
15080 reach the first DW_MACINFO_start_file entry. */
15081
15082 dwarf_decode_macro_bytes (abfd, section->buffer + offset, mac_end,
15083 current_file, lh, comp_dir, section, section_is_gnu,
15084 offset_size, cu->objfile);
15085 }
15086
15087 /* Check if the attribute's form is a DW_FORM_block*
15088 if so return true else false. */
15089 static int
15090 attr_form_is_block (struct attribute *attr)
15091 {
15092 return (attr == NULL ? 0 :
15093 attr->form == DW_FORM_block1
15094 || attr->form == DW_FORM_block2
15095 || attr->form == DW_FORM_block4
15096 || attr->form == DW_FORM_block
15097 || attr->form == DW_FORM_exprloc);
15098 }
15099
15100 /* Return non-zero if ATTR's value is a section offset --- classes
15101 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15102 You may use DW_UNSND (attr) to retrieve such offsets.
15103
15104 Section 7.5.4, "Attribute Encodings", explains that no attribute
15105 may have a value that belongs to more than one of these classes; it
15106 would be ambiguous if we did, because we use the same forms for all
15107 of them. */
15108 static int
15109 attr_form_is_section_offset (struct attribute *attr)
15110 {
15111 return (attr->form == DW_FORM_data4
15112 || attr->form == DW_FORM_data8
15113 || attr->form == DW_FORM_sec_offset);
15114 }
15115
15116
15117 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15118 zero otherwise. When this function returns true, you can apply
15119 dwarf2_get_attr_constant_value to it.
15120
15121 However, note that for some attributes you must check
15122 attr_form_is_section_offset before using this test. DW_FORM_data4
15123 and DW_FORM_data8 are members of both the constant class, and of
15124 the classes that contain offsets into other debug sections
15125 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15126 that, if an attribute's can be either a constant or one of the
15127 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15128 taken as section offsets, not constants. */
15129 static int
15130 attr_form_is_constant (struct attribute *attr)
15131 {
15132 switch (attr->form)
15133 {
15134 case DW_FORM_sdata:
15135 case DW_FORM_udata:
15136 case DW_FORM_data1:
15137 case DW_FORM_data2:
15138 case DW_FORM_data4:
15139 case DW_FORM_data8:
15140 return 1;
15141 default:
15142 return 0;
15143 }
15144 }
15145
15146 /* A helper function that fills in a dwarf2_loclist_baton. */
15147
15148 static void
15149 fill_in_loclist_baton (struct dwarf2_cu *cu,
15150 struct dwarf2_loclist_baton *baton,
15151 struct attribute *attr)
15152 {
15153 dwarf2_read_section (dwarf2_per_objfile->objfile,
15154 &dwarf2_per_objfile->loc);
15155
15156 baton->per_cu = cu->per_cu;
15157 gdb_assert (baton->per_cu);
15158 /* We don't know how long the location list is, but make sure we
15159 don't run off the edge of the section. */
15160 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15161 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15162 baton->base_address = cu->base_address;
15163 }
15164
15165 static void
15166 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15167 struct dwarf2_cu *cu)
15168 {
15169 if (attr_form_is_section_offset (attr)
15170 /* ".debug_loc" may not exist at all, or the offset may be outside
15171 the section. If so, fall through to the complaint in the
15172 other branch. */
15173 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
15174 &dwarf2_per_objfile->loc))
15175 {
15176 struct dwarf2_loclist_baton *baton;
15177
15178 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15179 sizeof (struct dwarf2_loclist_baton));
15180
15181 fill_in_loclist_baton (cu, baton, attr);
15182
15183 if (cu->base_known == 0)
15184 complaint (&symfile_complaints,
15185 _("Location list used without "
15186 "specifying the CU base address."));
15187
15188 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15189 SYMBOL_LOCATION_BATON (sym) = baton;
15190 }
15191 else
15192 {
15193 struct dwarf2_locexpr_baton *baton;
15194
15195 baton = obstack_alloc (&cu->objfile->objfile_obstack,
15196 sizeof (struct dwarf2_locexpr_baton));
15197 baton->per_cu = cu->per_cu;
15198 gdb_assert (baton->per_cu);
15199
15200 if (attr_form_is_block (attr))
15201 {
15202 /* Note that we're just copying the block's data pointer
15203 here, not the actual data. We're still pointing into the
15204 info_buffer for SYM's objfile; right now we never release
15205 that buffer, but when we do clean up properly this may
15206 need to change. */
15207 baton->size = DW_BLOCK (attr)->size;
15208 baton->data = DW_BLOCK (attr)->data;
15209 }
15210 else
15211 {
15212 dwarf2_invalid_attrib_class_complaint ("location description",
15213 SYMBOL_NATURAL_NAME (sym));
15214 baton->size = 0;
15215 baton->data = NULL;
15216 }
15217
15218 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15219 SYMBOL_LOCATION_BATON (sym) = baton;
15220 }
15221 }
15222
15223 /* Return the OBJFILE associated with the compilation unit CU. If CU
15224 came from a separate debuginfo file, then the master objfile is
15225 returned. */
15226
15227 struct objfile *
15228 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15229 {
15230 struct objfile *objfile = per_cu->objfile;
15231
15232 /* Return the master objfile, so that we can report and look up the
15233 correct file containing this variable. */
15234 if (objfile->separate_debug_objfile_backlink)
15235 objfile = objfile->separate_debug_objfile_backlink;
15236
15237 return objfile;
15238 }
15239
15240 /* Return the address size given in the compilation unit header for CU. */
15241
15242 CORE_ADDR
15243 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15244 {
15245 if (per_cu->cu)
15246 return per_cu->cu->header.addr_size;
15247 else
15248 {
15249 /* If the CU is not currently read in, we re-read its header. */
15250 struct objfile *objfile = per_cu->objfile;
15251 struct dwarf2_per_objfile *per_objfile
15252 = objfile_data (objfile, dwarf2_objfile_data_key);
15253 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15254 struct comp_unit_head cu_header;
15255
15256 memset (&cu_header, 0, sizeof cu_header);
15257 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15258 return cu_header.addr_size;
15259 }
15260 }
15261
15262 /* Return the offset size given in the compilation unit header for CU. */
15263
15264 int
15265 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15266 {
15267 if (per_cu->cu)
15268 return per_cu->cu->header.offset_size;
15269 else
15270 {
15271 /* If the CU is not currently read in, we re-read its header. */
15272 struct objfile *objfile = per_cu->objfile;
15273 struct dwarf2_per_objfile *per_objfile
15274 = objfile_data (objfile, dwarf2_objfile_data_key);
15275 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15276 struct comp_unit_head cu_header;
15277
15278 memset (&cu_header, 0, sizeof cu_header);
15279 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15280 return cu_header.offset_size;
15281 }
15282 }
15283
15284 /* Return the text offset of the CU. The returned offset comes from
15285 this CU's objfile. If this objfile came from a separate debuginfo
15286 file, then the offset may be different from the corresponding
15287 offset in the parent objfile. */
15288
15289 CORE_ADDR
15290 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15291 {
15292 struct objfile *objfile = per_cu->objfile;
15293
15294 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15295 }
15296
15297 /* Locate the .debug_info compilation unit from CU's objfile which contains
15298 the DIE at OFFSET. Raises an error on failure. */
15299
15300 static struct dwarf2_per_cu_data *
15301 dwarf2_find_containing_comp_unit (unsigned int offset,
15302 struct objfile *objfile)
15303 {
15304 struct dwarf2_per_cu_data *this_cu;
15305 int low, high;
15306
15307 low = 0;
15308 high = dwarf2_per_objfile->n_comp_units - 1;
15309 while (high > low)
15310 {
15311 int mid = low + (high - low) / 2;
15312
15313 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15314 high = mid;
15315 else
15316 low = mid + 1;
15317 }
15318 gdb_assert (low == high);
15319 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15320 {
15321 if (low == 0)
15322 error (_("Dwarf Error: could not find partial DIE containing "
15323 "offset 0x%lx [in module %s]"),
15324 (long) offset, bfd_get_filename (objfile->obfd));
15325
15326 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15327 return dwarf2_per_objfile->all_comp_units[low-1];
15328 }
15329 else
15330 {
15331 this_cu = dwarf2_per_objfile->all_comp_units[low];
15332 if (low == dwarf2_per_objfile->n_comp_units - 1
15333 && offset >= this_cu->offset + this_cu->length)
15334 error (_("invalid dwarf2 offset %u"), offset);
15335 gdb_assert (offset < this_cu->offset + this_cu->length);
15336 return this_cu;
15337 }
15338 }
15339
15340 /* Locate the compilation unit from OBJFILE which is located at exactly
15341 OFFSET. Raises an error on failure. */
15342
15343 static struct dwarf2_per_cu_data *
15344 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15345 {
15346 struct dwarf2_per_cu_data *this_cu;
15347
15348 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15349 if (this_cu->offset != offset)
15350 error (_("no compilation unit with offset %u."), offset);
15351 return this_cu;
15352 }
15353
15354 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
15355
15356 static void
15357 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15358 {
15359 memset (cu, 0, sizeof (*cu));
15360 cu->objfile = objfile;
15361 obstack_init (&cu->comp_unit_obstack);
15362 }
15363
15364 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15365
15366 static void
15367 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15368 {
15369 struct attribute *attr;
15370
15371 /* Set the language we're debugging. */
15372 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15373 if (attr)
15374 set_cu_language (DW_UNSND (attr), cu);
15375 else
15376 {
15377 cu->language = language_minimal;
15378 cu->language_defn = language_def (cu->language);
15379 }
15380 }
15381
15382 /* Release one cached compilation unit, CU. We unlink it from the tree
15383 of compilation units, but we don't remove it from the read_in_chain;
15384 the caller is responsible for that.
15385 NOTE: DATA is a void * because this function is also used as a
15386 cleanup routine. */
15387
15388 static void
15389 free_one_comp_unit (void *data)
15390 {
15391 struct dwarf2_cu *cu = data;
15392
15393 if (cu->per_cu != NULL)
15394 cu->per_cu->cu = NULL;
15395 cu->per_cu = NULL;
15396
15397 obstack_free (&cu->comp_unit_obstack, NULL);
15398
15399 xfree (cu);
15400 }
15401
15402 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15403 when we're finished with it. We can't free the pointer itself, but be
15404 sure to unlink it from the cache. Also release any associated storage
15405 and perform cache maintenance.
15406
15407 Only used during partial symbol parsing. */
15408
15409 static void
15410 free_stack_comp_unit (void *data)
15411 {
15412 struct dwarf2_cu *cu = data;
15413
15414 obstack_free (&cu->comp_unit_obstack, NULL);
15415 cu->partial_dies = NULL;
15416
15417 if (cu->per_cu != NULL)
15418 {
15419 /* This compilation unit is on the stack in our caller, so we
15420 should not xfree it. Just unlink it. */
15421 cu->per_cu->cu = NULL;
15422 cu->per_cu = NULL;
15423
15424 /* If we had a per-cu pointer, then we may have other compilation
15425 units loaded, so age them now. */
15426 age_cached_comp_units ();
15427 }
15428 }
15429
15430 /* Free all cached compilation units. */
15431
15432 static void
15433 free_cached_comp_units (void *data)
15434 {
15435 struct dwarf2_per_cu_data *per_cu, **last_chain;
15436
15437 per_cu = dwarf2_per_objfile->read_in_chain;
15438 last_chain = &dwarf2_per_objfile->read_in_chain;
15439 while (per_cu != NULL)
15440 {
15441 struct dwarf2_per_cu_data *next_cu;
15442
15443 next_cu = per_cu->cu->read_in_chain;
15444
15445 free_one_comp_unit (per_cu->cu);
15446 *last_chain = next_cu;
15447
15448 per_cu = next_cu;
15449 }
15450 }
15451
15452 /* Increase the age counter on each cached compilation unit, and free
15453 any that are too old. */
15454
15455 static void
15456 age_cached_comp_units (void)
15457 {
15458 struct dwarf2_per_cu_data *per_cu, **last_chain;
15459
15460 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15461 per_cu = dwarf2_per_objfile->read_in_chain;
15462 while (per_cu != NULL)
15463 {
15464 per_cu->cu->last_used ++;
15465 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15466 dwarf2_mark (per_cu->cu);
15467 per_cu = per_cu->cu->read_in_chain;
15468 }
15469
15470 per_cu = dwarf2_per_objfile->read_in_chain;
15471 last_chain = &dwarf2_per_objfile->read_in_chain;
15472 while (per_cu != NULL)
15473 {
15474 struct dwarf2_per_cu_data *next_cu;
15475
15476 next_cu = per_cu->cu->read_in_chain;
15477
15478 if (!per_cu->cu->mark)
15479 {
15480 free_one_comp_unit (per_cu->cu);
15481 *last_chain = next_cu;
15482 }
15483 else
15484 last_chain = &per_cu->cu->read_in_chain;
15485
15486 per_cu = next_cu;
15487 }
15488 }
15489
15490 /* Remove a single compilation unit from the cache. */
15491
15492 static void
15493 free_one_cached_comp_unit (void *target_cu)
15494 {
15495 struct dwarf2_per_cu_data *per_cu, **last_chain;
15496
15497 per_cu = dwarf2_per_objfile->read_in_chain;
15498 last_chain = &dwarf2_per_objfile->read_in_chain;
15499 while (per_cu != NULL)
15500 {
15501 struct dwarf2_per_cu_data *next_cu;
15502
15503 next_cu = per_cu->cu->read_in_chain;
15504
15505 if (per_cu->cu == target_cu)
15506 {
15507 free_one_comp_unit (per_cu->cu);
15508 *last_chain = next_cu;
15509 break;
15510 }
15511 else
15512 last_chain = &per_cu->cu->read_in_chain;
15513
15514 per_cu = next_cu;
15515 }
15516 }
15517
15518 /* Release all extra memory associated with OBJFILE. */
15519
15520 void
15521 dwarf2_free_objfile (struct objfile *objfile)
15522 {
15523 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15524
15525 if (dwarf2_per_objfile == NULL)
15526 return;
15527
15528 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15529 free_cached_comp_units (NULL);
15530
15531 if (dwarf2_per_objfile->quick_file_names_table)
15532 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15533
15534 /* Everything else should be on the objfile obstack. */
15535 }
15536
15537 /* A pair of DIE offset and GDB type pointer. We store these
15538 in a hash table separate from the DIEs, and preserve them
15539 when the DIEs are flushed out of cache. */
15540
15541 struct dwarf2_offset_and_type
15542 {
15543 unsigned int offset;
15544 struct type *type;
15545 };
15546
15547 /* Hash function for a dwarf2_offset_and_type. */
15548
15549 static hashval_t
15550 offset_and_type_hash (const void *item)
15551 {
15552 const struct dwarf2_offset_and_type *ofs = item;
15553
15554 return ofs->offset;
15555 }
15556
15557 /* Equality function for a dwarf2_offset_and_type. */
15558
15559 static int
15560 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15561 {
15562 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15563 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15564
15565 return ofs_lhs->offset == ofs_rhs->offset;
15566 }
15567
15568 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15569 table if necessary. For convenience, return TYPE.
15570
15571 The DIEs reading must have careful ordering to:
15572 * Not cause infite loops trying to read in DIEs as a prerequisite for
15573 reading current DIE.
15574 * Not trying to dereference contents of still incompletely read in types
15575 while reading in other DIEs.
15576 * Enable referencing still incompletely read in types just by a pointer to
15577 the type without accessing its fields.
15578
15579 Therefore caller should follow these rules:
15580 * Try to fetch any prerequisite types we may need to build this DIE type
15581 before building the type and calling set_die_type.
15582 * After building type call set_die_type for current DIE as soon as
15583 possible before fetching more types to complete the current type.
15584 * Make the type as complete as possible before fetching more types. */
15585
15586 static struct type *
15587 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15588 {
15589 struct dwarf2_offset_and_type **slot, ofs;
15590 struct objfile *objfile = cu->objfile;
15591 htab_t *type_hash_ptr;
15592
15593 /* For Ada types, make sure that the gnat-specific data is always
15594 initialized (if not already set). There are a few types where
15595 we should not be doing so, because the type-specific area is
15596 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15597 where the type-specific area is used to store the floatformat).
15598 But this is not a problem, because the gnat-specific information
15599 is actually not needed for these types. */
15600 if (need_gnat_info (cu)
15601 && TYPE_CODE (type) != TYPE_CODE_FUNC
15602 && TYPE_CODE (type) != TYPE_CODE_FLT
15603 && !HAVE_GNAT_AUX_INFO (type))
15604 INIT_GNAT_SPECIFIC (type);
15605
15606 if (cu->per_cu->debug_type_section)
15607 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15608 else
15609 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15610
15611 if (*type_hash_ptr == NULL)
15612 {
15613 *type_hash_ptr
15614 = htab_create_alloc_ex (127,
15615 offset_and_type_hash,
15616 offset_and_type_eq,
15617 NULL,
15618 &objfile->objfile_obstack,
15619 hashtab_obstack_allocate,
15620 dummy_obstack_deallocate);
15621 }
15622
15623 ofs.offset = die->offset;
15624 ofs.type = type;
15625 slot = (struct dwarf2_offset_and_type **)
15626 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15627 if (*slot)
15628 complaint (&symfile_complaints,
15629 _("A problem internal to GDB: DIE 0x%x has type already set"),
15630 die->offset);
15631 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15632 **slot = ofs;
15633 return type;
15634 }
15635
15636 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15637 table, or return NULL if the die does not have a saved type. */
15638
15639 static struct type *
15640 get_die_type_at_offset (unsigned int offset,
15641 struct dwarf2_per_cu_data *per_cu)
15642 {
15643 struct dwarf2_offset_and_type *slot, ofs;
15644 htab_t type_hash;
15645
15646 if (per_cu->debug_type_section)
15647 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15648 else
15649 type_hash = dwarf2_per_objfile->debug_info_type_hash;
15650 if (type_hash == NULL)
15651 return NULL;
15652
15653 ofs.offset = offset;
15654 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15655 if (slot)
15656 return slot->type;
15657 else
15658 return NULL;
15659 }
15660
15661 /* Look up the type for DIE in the appropriate type_hash table,
15662 or return NULL if DIE does not have a saved type. */
15663
15664 static struct type *
15665 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15666 {
15667 return get_die_type_at_offset (die->offset, cu->per_cu);
15668 }
15669
15670 /* Add a dependence relationship from CU to REF_PER_CU. */
15671
15672 static void
15673 dwarf2_add_dependence (struct dwarf2_cu *cu,
15674 struct dwarf2_per_cu_data *ref_per_cu)
15675 {
15676 void **slot;
15677
15678 if (cu->dependencies == NULL)
15679 cu->dependencies
15680 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15681 NULL, &cu->comp_unit_obstack,
15682 hashtab_obstack_allocate,
15683 dummy_obstack_deallocate);
15684
15685 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15686 if (*slot == NULL)
15687 *slot = ref_per_cu;
15688 }
15689
15690 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15691 Set the mark field in every compilation unit in the
15692 cache that we must keep because we are keeping CU. */
15693
15694 static int
15695 dwarf2_mark_helper (void **slot, void *data)
15696 {
15697 struct dwarf2_per_cu_data *per_cu;
15698
15699 per_cu = (struct dwarf2_per_cu_data *) *slot;
15700
15701 /* cu->dependencies references may not yet have been ever read if QUIT aborts
15702 reading of the chain. As such dependencies remain valid it is not much
15703 useful to track and undo them during QUIT cleanups. */
15704 if (per_cu->cu == NULL)
15705 return 1;
15706
15707 if (per_cu->cu->mark)
15708 return 1;
15709 per_cu->cu->mark = 1;
15710
15711 if (per_cu->cu->dependencies != NULL)
15712 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15713
15714 return 1;
15715 }
15716
15717 /* Set the mark field in CU and in every other compilation unit in the
15718 cache that we must keep because we are keeping CU. */
15719
15720 static void
15721 dwarf2_mark (struct dwarf2_cu *cu)
15722 {
15723 if (cu->mark)
15724 return;
15725 cu->mark = 1;
15726 if (cu->dependencies != NULL)
15727 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15728 }
15729
15730 static void
15731 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15732 {
15733 while (per_cu)
15734 {
15735 per_cu->cu->mark = 0;
15736 per_cu = per_cu->cu->read_in_chain;
15737 }
15738 }
15739
15740 /* Trivial hash function for partial_die_info: the hash value of a DIE
15741 is its offset in .debug_info for this objfile. */
15742
15743 static hashval_t
15744 partial_die_hash (const void *item)
15745 {
15746 const struct partial_die_info *part_die = item;
15747
15748 return part_die->offset;
15749 }
15750
15751 /* Trivial comparison function for partial_die_info structures: two DIEs
15752 are equal if they have the same offset. */
15753
15754 static int
15755 partial_die_eq (const void *item_lhs, const void *item_rhs)
15756 {
15757 const struct partial_die_info *part_die_lhs = item_lhs;
15758 const struct partial_die_info *part_die_rhs = item_rhs;
15759
15760 return part_die_lhs->offset == part_die_rhs->offset;
15761 }
15762
15763 static struct cmd_list_element *set_dwarf2_cmdlist;
15764 static struct cmd_list_element *show_dwarf2_cmdlist;
15765
15766 static void
15767 set_dwarf2_cmd (char *args, int from_tty)
15768 {
15769 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15770 }
15771
15772 static void
15773 show_dwarf2_cmd (char *args, int from_tty)
15774 {
15775 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15776 }
15777
15778 /* If section described by INFO was mmapped, munmap it now. */
15779
15780 static void
15781 munmap_section_buffer (struct dwarf2_section_info *info)
15782 {
15783 if (info->map_addr != NULL)
15784 {
15785 #ifdef HAVE_MMAP
15786 int res;
15787
15788 res = munmap (info->map_addr, info->map_len);
15789 gdb_assert (res == 0);
15790 #else
15791 /* Without HAVE_MMAP, we should never be here to begin with. */
15792 gdb_assert_not_reached ("no mmap support");
15793 #endif
15794 }
15795 }
15796
15797 /* munmap debug sections for OBJFILE, if necessary. */
15798
15799 static void
15800 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15801 {
15802 struct dwarf2_per_objfile *data = d;
15803 int ix;
15804 struct dwarf2_section_info *section;
15805
15806 /* This is sorted according to the order they're defined in to make it easier
15807 to keep in sync. */
15808 munmap_section_buffer (&data->info);
15809 munmap_section_buffer (&data->abbrev);
15810 munmap_section_buffer (&data->line);
15811 munmap_section_buffer (&data->loc);
15812 munmap_section_buffer (&data->macinfo);
15813 munmap_section_buffer (&data->macro);
15814 munmap_section_buffer (&data->str);
15815 munmap_section_buffer (&data->ranges);
15816 munmap_section_buffer (&data->frame);
15817 munmap_section_buffer (&data->eh_frame);
15818 munmap_section_buffer (&data->gdb_index);
15819
15820 for (ix = 0;
15821 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
15822 ++ix)
15823 munmap_section_buffer (section);
15824
15825 VEC_free (dwarf2_section_info_def, data->types);
15826 }
15827
15828 \f
15829 /* The "save gdb-index" command. */
15830
15831 /* The contents of the hash table we create when building the string
15832 table. */
15833 struct strtab_entry
15834 {
15835 offset_type offset;
15836 const char *str;
15837 };
15838
15839 /* Hash function for a strtab_entry.
15840
15841 Function is used only during write_hash_table so no index format backward
15842 compatibility is needed. */
15843
15844 static hashval_t
15845 hash_strtab_entry (const void *e)
15846 {
15847 const struct strtab_entry *entry = e;
15848 return mapped_index_string_hash (INT_MAX, entry->str);
15849 }
15850
15851 /* Equality function for a strtab_entry. */
15852
15853 static int
15854 eq_strtab_entry (const void *a, const void *b)
15855 {
15856 const struct strtab_entry *ea = a;
15857 const struct strtab_entry *eb = b;
15858 return !strcmp (ea->str, eb->str);
15859 }
15860
15861 /* Create a strtab_entry hash table. */
15862
15863 static htab_t
15864 create_strtab (void)
15865 {
15866 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15867 xfree, xcalloc, xfree);
15868 }
15869
15870 /* Add a string to the constant pool. Return the string's offset in
15871 host order. */
15872
15873 static offset_type
15874 add_string (htab_t table, struct obstack *cpool, const char *str)
15875 {
15876 void **slot;
15877 struct strtab_entry entry;
15878 struct strtab_entry *result;
15879
15880 entry.str = str;
15881 slot = htab_find_slot (table, &entry, INSERT);
15882 if (*slot)
15883 result = *slot;
15884 else
15885 {
15886 result = XNEW (struct strtab_entry);
15887 result->offset = obstack_object_size (cpool);
15888 result->str = str;
15889 obstack_grow_str0 (cpool, str);
15890 *slot = result;
15891 }
15892 return result->offset;
15893 }
15894
15895 /* An entry in the symbol table. */
15896 struct symtab_index_entry
15897 {
15898 /* The name of the symbol. */
15899 const char *name;
15900 /* The offset of the name in the constant pool. */
15901 offset_type index_offset;
15902 /* A sorted vector of the indices of all the CUs that hold an object
15903 of this name. */
15904 VEC (offset_type) *cu_indices;
15905 };
15906
15907 /* The symbol table. This is a power-of-2-sized hash table. */
15908 struct mapped_symtab
15909 {
15910 offset_type n_elements;
15911 offset_type size;
15912 struct symtab_index_entry **data;
15913 };
15914
15915 /* Hash function for a symtab_index_entry. */
15916
15917 static hashval_t
15918 hash_symtab_entry (const void *e)
15919 {
15920 const struct symtab_index_entry *entry = e;
15921 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15922 sizeof (offset_type) * VEC_length (offset_type,
15923 entry->cu_indices),
15924 0);
15925 }
15926
15927 /* Equality function for a symtab_index_entry. */
15928
15929 static int
15930 eq_symtab_entry (const void *a, const void *b)
15931 {
15932 const struct symtab_index_entry *ea = a;
15933 const struct symtab_index_entry *eb = b;
15934 int len = VEC_length (offset_type, ea->cu_indices);
15935 if (len != VEC_length (offset_type, eb->cu_indices))
15936 return 0;
15937 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15938 VEC_address (offset_type, eb->cu_indices),
15939 sizeof (offset_type) * len);
15940 }
15941
15942 /* Destroy a symtab_index_entry. */
15943
15944 static void
15945 delete_symtab_entry (void *p)
15946 {
15947 struct symtab_index_entry *entry = p;
15948 VEC_free (offset_type, entry->cu_indices);
15949 xfree (entry);
15950 }
15951
15952 /* Create a hash table holding symtab_index_entry objects. */
15953
15954 static htab_t
15955 create_symbol_hash_table (void)
15956 {
15957 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15958 delete_symtab_entry, xcalloc, xfree);
15959 }
15960
15961 /* Create a new mapped symtab object. */
15962
15963 static struct mapped_symtab *
15964 create_mapped_symtab (void)
15965 {
15966 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15967 symtab->n_elements = 0;
15968 symtab->size = 1024;
15969 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15970 return symtab;
15971 }
15972
15973 /* Destroy a mapped_symtab. */
15974
15975 static void
15976 cleanup_mapped_symtab (void *p)
15977 {
15978 struct mapped_symtab *symtab = p;
15979 /* The contents of the array are freed when the other hash table is
15980 destroyed. */
15981 xfree (symtab->data);
15982 xfree (symtab);
15983 }
15984
15985 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15986 the slot.
15987
15988 Function is used only during write_hash_table so no index format backward
15989 compatibility is needed. */
15990
15991 static struct symtab_index_entry **
15992 find_slot (struct mapped_symtab *symtab, const char *name)
15993 {
15994 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15995
15996 index = hash & (symtab->size - 1);
15997 step = ((hash * 17) & (symtab->size - 1)) | 1;
15998
15999 for (;;)
16000 {
16001 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16002 return &symtab->data[index];
16003 index = (index + step) & (symtab->size - 1);
16004 }
16005 }
16006
16007 /* Expand SYMTAB's hash table. */
16008
16009 static void
16010 hash_expand (struct mapped_symtab *symtab)
16011 {
16012 offset_type old_size = symtab->size;
16013 offset_type i;
16014 struct symtab_index_entry **old_entries = symtab->data;
16015
16016 symtab->size *= 2;
16017 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16018
16019 for (i = 0; i < old_size; ++i)
16020 {
16021 if (old_entries[i])
16022 {
16023 struct symtab_index_entry **slot = find_slot (symtab,
16024 old_entries[i]->name);
16025 *slot = old_entries[i];
16026 }
16027 }
16028
16029 xfree (old_entries);
16030 }
16031
16032 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16033 is the index of the CU in which the symbol appears. */
16034
16035 static void
16036 add_index_entry (struct mapped_symtab *symtab, const char *name,
16037 offset_type cu_index)
16038 {
16039 struct symtab_index_entry **slot;
16040
16041 ++symtab->n_elements;
16042 if (4 * symtab->n_elements / 3 >= symtab->size)
16043 hash_expand (symtab);
16044
16045 slot = find_slot (symtab, name);
16046 if (!*slot)
16047 {
16048 *slot = XNEW (struct symtab_index_entry);
16049 (*slot)->name = name;
16050 (*slot)->cu_indices = NULL;
16051 }
16052 /* Don't push an index twice. Due to how we add entries we only
16053 have to check the last one. */
16054 if (VEC_empty (offset_type, (*slot)->cu_indices)
16055 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16056 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16057 }
16058
16059 /* Add a vector of indices to the constant pool. */
16060
16061 static offset_type
16062 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16063 struct symtab_index_entry *entry)
16064 {
16065 void **slot;
16066
16067 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16068 if (!*slot)
16069 {
16070 offset_type len = VEC_length (offset_type, entry->cu_indices);
16071 offset_type val = MAYBE_SWAP (len);
16072 offset_type iter;
16073 int i;
16074
16075 *slot = entry;
16076 entry->index_offset = obstack_object_size (cpool);
16077
16078 obstack_grow (cpool, &val, sizeof (val));
16079 for (i = 0;
16080 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16081 ++i)
16082 {
16083 val = MAYBE_SWAP (iter);
16084 obstack_grow (cpool, &val, sizeof (val));
16085 }
16086 }
16087 else
16088 {
16089 struct symtab_index_entry *old_entry = *slot;
16090 entry->index_offset = old_entry->index_offset;
16091 entry = old_entry;
16092 }
16093 return entry->index_offset;
16094 }
16095
16096 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16097 constant pool entries going into the obstack CPOOL. */
16098
16099 static void
16100 write_hash_table (struct mapped_symtab *symtab,
16101 struct obstack *output, struct obstack *cpool)
16102 {
16103 offset_type i;
16104 htab_t symbol_hash_table;
16105 htab_t str_table;
16106
16107 symbol_hash_table = create_symbol_hash_table ();
16108 str_table = create_strtab ();
16109
16110 /* We add all the index vectors to the constant pool first, to
16111 ensure alignment is ok. */
16112 for (i = 0; i < symtab->size; ++i)
16113 {
16114 if (symtab->data[i])
16115 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16116 }
16117
16118 /* Now write out the hash table. */
16119 for (i = 0; i < symtab->size; ++i)
16120 {
16121 offset_type str_off, vec_off;
16122
16123 if (symtab->data[i])
16124 {
16125 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16126 vec_off = symtab->data[i]->index_offset;
16127 }
16128 else
16129 {
16130 /* While 0 is a valid constant pool index, it is not valid
16131 to have 0 for both offsets. */
16132 str_off = 0;
16133 vec_off = 0;
16134 }
16135
16136 str_off = MAYBE_SWAP (str_off);
16137 vec_off = MAYBE_SWAP (vec_off);
16138
16139 obstack_grow (output, &str_off, sizeof (str_off));
16140 obstack_grow (output, &vec_off, sizeof (vec_off));
16141 }
16142
16143 htab_delete (str_table);
16144 htab_delete (symbol_hash_table);
16145 }
16146
16147 /* Struct to map psymtab to CU index in the index file. */
16148 struct psymtab_cu_index_map
16149 {
16150 struct partial_symtab *psymtab;
16151 unsigned int cu_index;
16152 };
16153
16154 static hashval_t
16155 hash_psymtab_cu_index (const void *item)
16156 {
16157 const struct psymtab_cu_index_map *map = item;
16158
16159 return htab_hash_pointer (map->psymtab);
16160 }
16161
16162 static int
16163 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16164 {
16165 const struct psymtab_cu_index_map *lhs = item_lhs;
16166 const struct psymtab_cu_index_map *rhs = item_rhs;
16167
16168 return lhs->psymtab == rhs->psymtab;
16169 }
16170
16171 /* Helper struct for building the address table. */
16172 struct addrmap_index_data
16173 {
16174 struct objfile *objfile;
16175 struct obstack *addr_obstack;
16176 htab_t cu_index_htab;
16177
16178 /* Non-zero if the previous_* fields are valid.
16179 We can't write an entry until we see the next entry (since it is only then
16180 that we know the end of the entry). */
16181 int previous_valid;
16182 /* Index of the CU in the table of all CUs in the index file. */
16183 unsigned int previous_cu_index;
16184 /* Start address of the CU. */
16185 CORE_ADDR previous_cu_start;
16186 };
16187
16188 /* Write an address entry to OBSTACK. */
16189
16190 static void
16191 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16192 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16193 {
16194 offset_type cu_index_to_write;
16195 char addr[8];
16196 CORE_ADDR baseaddr;
16197
16198 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16199
16200 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16201 obstack_grow (obstack, addr, 8);
16202 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16203 obstack_grow (obstack, addr, 8);
16204 cu_index_to_write = MAYBE_SWAP (cu_index);
16205 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16206 }
16207
16208 /* Worker function for traversing an addrmap to build the address table. */
16209
16210 static int
16211 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16212 {
16213 struct addrmap_index_data *data = datap;
16214 struct partial_symtab *pst = obj;
16215 offset_type cu_index;
16216 void **slot;
16217
16218 if (data->previous_valid)
16219 add_address_entry (data->objfile, data->addr_obstack,
16220 data->previous_cu_start, start_addr,
16221 data->previous_cu_index);
16222
16223 data->previous_cu_start = start_addr;
16224 if (pst != NULL)
16225 {
16226 struct psymtab_cu_index_map find_map, *map;
16227 find_map.psymtab = pst;
16228 map = htab_find (data->cu_index_htab, &find_map);
16229 gdb_assert (map != NULL);
16230 data->previous_cu_index = map->cu_index;
16231 data->previous_valid = 1;
16232 }
16233 else
16234 data->previous_valid = 0;
16235
16236 return 0;
16237 }
16238
16239 /* Write OBJFILE's address map to OBSTACK.
16240 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16241 in the index file. */
16242
16243 static void
16244 write_address_map (struct objfile *objfile, struct obstack *obstack,
16245 htab_t cu_index_htab)
16246 {
16247 struct addrmap_index_data addrmap_index_data;
16248
16249 /* When writing the address table, we have to cope with the fact that
16250 the addrmap iterator only provides the start of a region; we have to
16251 wait until the next invocation to get the start of the next region. */
16252
16253 addrmap_index_data.objfile = objfile;
16254 addrmap_index_data.addr_obstack = obstack;
16255 addrmap_index_data.cu_index_htab = cu_index_htab;
16256 addrmap_index_data.previous_valid = 0;
16257
16258 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16259 &addrmap_index_data);
16260
16261 /* It's highly unlikely the last entry (end address = 0xff...ff)
16262 is valid, but we should still handle it.
16263 The end address is recorded as the start of the next region, but that
16264 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16265 anyway. */
16266 if (addrmap_index_data.previous_valid)
16267 add_address_entry (objfile, obstack,
16268 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16269 addrmap_index_data.previous_cu_index);
16270 }
16271
16272 /* Add a list of partial symbols to SYMTAB. */
16273
16274 static void
16275 write_psymbols (struct mapped_symtab *symtab,
16276 htab_t psyms_seen,
16277 struct partial_symbol **psymp,
16278 int count,
16279 offset_type cu_index,
16280 int is_static)
16281 {
16282 for (; count-- > 0; ++psymp)
16283 {
16284 void **slot, *lookup;
16285
16286 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16287 error (_("Ada is not currently supported by the index"));
16288
16289 /* We only want to add a given psymbol once. However, we also
16290 want to account for whether it is global or static. So, we
16291 may add it twice, using slightly different values. */
16292 if (is_static)
16293 {
16294 uintptr_t val = 1 | (uintptr_t) *psymp;
16295
16296 lookup = (void *) val;
16297 }
16298 else
16299 lookup = *psymp;
16300
16301 /* Only add a given psymbol once. */
16302 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16303 if (!*slot)
16304 {
16305 *slot = lookup;
16306 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16307 }
16308 }
16309 }
16310
16311 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16312 exception if there is an error. */
16313
16314 static void
16315 write_obstack (FILE *file, struct obstack *obstack)
16316 {
16317 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16318 file)
16319 != obstack_object_size (obstack))
16320 error (_("couldn't data write to file"));
16321 }
16322
16323 /* Unlink a file if the argument is not NULL. */
16324
16325 static void
16326 unlink_if_set (void *p)
16327 {
16328 char **filename = p;
16329 if (*filename)
16330 unlink (*filename);
16331 }
16332
16333 /* A helper struct used when iterating over debug_types. */
16334 struct signatured_type_index_data
16335 {
16336 struct objfile *objfile;
16337 struct mapped_symtab *symtab;
16338 struct obstack *types_list;
16339 htab_t psyms_seen;
16340 int cu_index;
16341 };
16342
16343 /* A helper function that writes a single signatured_type to an
16344 obstack. */
16345
16346 static int
16347 write_one_signatured_type (void **slot, void *d)
16348 {
16349 struct signatured_type_index_data *info = d;
16350 struct signatured_type *entry = (struct signatured_type *) *slot;
16351 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16352 struct partial_symtab *psymtab = per_cu->v.psymtab;
16353 gdb_byte val[8];
16354
16355 write_psymbols (info->symtab,
16356 info->psyms_seen,
16357 info->objfile->global_psymbols.list
16358 + psymtab->globals_offset,
16359 psymtab->n_global_syms, info->cu_index,
16360 0);
16361 write_psymbols (info->symtab,
16362 info->psyms_seen,
16363 info->objfile->static_psymbols.list
16364 + psymtab->statics_offset,
16365 psymtab->n_static_syms, info->cu_index,
16366 1);
16367
16368 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16369 obstack_grow (info->types_list, val, 8);
16370 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16371 obstack_grow (info->types_list, val, 8);
16372 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16373 obstack_grow (info->types_list, val, 8);
16374
16375 ++info->cu_index;
16376
16377 return 1;
16378 }
16379
16380 /* A cleanup function for an htab_t. */
16381
16382 static void
16383 cleanup_htab (void *arg)
16384 {
16385 htab_delete (arg);
16386 }
16387
16388 /* Create an index file for OBJFILE in the directory DIR. */
16389
16390 static void
16391 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16392 {
16393 struct cleanup *cleanup;
16394 char *filename, *cleanup_filename;
16395 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16396 struct obstack cu_list, types_cu_list;
16397 int i;
16398 FILE *out_file;
16399 struct mapped_symtab *symtab;
16400 offset_type val, size_of_contents, total_len;
16401 struct stat st;
16402 char buf[8];
16403 htab_t psyms_seen;
16404 htab_t cu_index_htab;
16405 struct psymtab_cu_index_map *psymtab_cu_index_map;
16406
16407 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16408 return;
16409
16410 if (dwarf2_per_objfile->using_index)
16411 error (_("Cannot use an index to create the index"));
16412
16413 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16414 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16415
16416 if (stat (objfile->name, &st) < 0)
16417 perror_with_name (objfile->name);
16418
16419 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16420 INDEX_SUFFIX, (char *) NULL);
16421 cleanup = make_cleanup (xfree, filename);
16422
16423 out_file = fopen (filename, "wb");
16424 if (!out_file)
16425 error (_("Can't open `%s' for writing"), filename);
16426
16427 cleanup_filename = filename;
16428 make_cleanup (unlink_if_set, &cleanup_filename);
16429
16430 symtab = create_mapped_symtab ();
16431 make_cleanup (cleanup_mapped_symtab, symtab);
16432
16433 obstack_init (&addr_obstack);
16434 make_cleanup_obstack_free (&addr_obstack);
16435
16436 obstack_init (&cu_list);
16437 make_cleanup_obstack_free (&cu_list);
16438
16439 obstack_init (&types_cu_list);
16440 make_cleanup_obstack_free (&types_cu_list);
16441
16442 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16443 NULL, xcalloc, xfree);
16444 make_cleanup (cleanup_htab, psyms_seen);
16445
16446 /* While we're scanning CU's create a table that maps a psymtab pointer
16447 (which is what addrmap records) to its index (which is what is recorded
16448 in the index file). This will later be needed to write the address
16449 table. */
16450 cu_index_htab = htab_create_alloc (100,
16451 hash_psymtab_cu_index,
16452 eq_psymtab_cu_index,
16453 NULL, xcalloc, xfree);
16454 make_cleanup (cleanup_htab, cu_index_htab);
16455 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16456 xmalloc (sizeof (struct psymtab_cu_index_map)
16457 * dwarf2_per_objfile->n_comp_units);
16458 make_cleanup (xfree, psymtab_cu_index_map);
16459
16460 /* The CU list is already sorted, so we don't need to do additional
16461 work here. Also, the debug_types entries do not appear in
16462 all_comp_units, but only in their own hash table. */
16463 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16464 {
16465 struct dwarf2_per_cu_data *per_cu
16466 = dwarf2_per_objfile->all_comp_units[i];
16467 struct partial_symtab *psymtab = per_cu->v.psymtab;
16468 gdb_byte val[8];
16469 struct psymtab_cu_index_map *map;
16470 void **slot;
16471
16472 write_psymbols (symtab,
16473 psyms_seen,
16474 objfile->global_psymbols.list + psymtab->globals_offset,
16475 psymtab->n_global_syms, i,
16476 0);
16477 write_psymbols (symtab,
16478 psyms_seen,
16479 objfile->static_psymbols.list + psymtab->statics_offset,
16480 psymtab->n_static_syms, i,
16481 1);
16482
16483 map = &psymtab_cu_index_map[i];
16484 map->psymtab = psymtab;
16485 map->cu_index = i;
16486 slot = htab_find_slot (cu_index_htab, map, INSERT);
16487 gdb_assert (slot != NULL);
16488 gdb_assert (*slot == NULL);
16489 *slot = map;
16490
16491 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16492 obstack_grow (&cu_list, val, 8);
16493 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16494 obstack_grow (&cu_list, val, 8);
16495 }
16496
16497 /* Dump the address map. */
16498 write_address_map (objfile, &addr_obstack, cu_index_htab);
16499
16500 /* Write out the .debug_type entries, if any. */
16501 if (dwarf2_per_objfile->signatured_types)
16502 {
16503 struct signatured_type_index_data sig_data;
16504
16505 sig_data.objfile = objfile;
16506 sig_data.symtab = symtab;
16507 sig_data.types_list = &types_cu_list;
16508 sig_data.psyms_seen = psyms_seen;
16509 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16510 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16511 write_one_signatured_type, &sig_data);
16512 }
16513
16514 obstack_init (&constant_pool);
16515 make_cleanup_obstack_free (&constant_pool);
16516 obstack_init (&symtab_obstack);
16517 make_cleanup_obstack_free (&symtab_obstack);
16518 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16519
16520 obstack_init (&contents);
16521 make_cleanup_obstack_free (&contents);
16522 size_of_contents = 6 * sizeof (offset_type);
16523 total_len = size_of_contents;
16524
16525 /* The version number. */
16526 val = MAYBE_SWAP (5);
16527 obstack_grow (&contents, &val, sizeof (val));
16528
16529 /* The offset of the CU list from the start of the file. */
16530 val = MAYBE_SWAP (total_len);
16531 obstack_grow (&contents, &val, sizeof (val));
16532 total_len += obstack_object_size (&cu_list);
16533
16534 /* The offset of the types CU list from the start of the file. */
16535 val = MAYBE_SWAP (total_len);
16536 obstack_grow (&contents, &val, sizeof (val));
16537 total_len += obstack_object_size (&types_cu_list);
16538
16539 /* The offset of the address table from the start of the file. */
16540 val = MAYBE_SWAP (total_len);
16541 obstack_grow (&contents, &val, sizeof (val));
16542 total_len += obstack_object_size (&addr_obstack);
16543
16544 /* The offset of the symbol table from the start of the file. */
16545 val = MAYBE_SWAP (total_len);
16546 obstack_grow (&contents, &val, sizeof (val));
16547 total_len += obstack_object_size (&symtab_obstack);
16548
16549 /* The offset of the constant pool from the start of the file. */
16550 val = MAYBE_SWAP (total_len);
16551 obstack_grow (&contents, &val, sizeof (val));
16552 total_len += obstack_object_size (&constant_pool);
16553
16554 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16555
16556 write_obstack (out_file, &contents);
16557 write_obstack (out_file, &cu_list);
16558 write_obstack (out_file, &types_cu_list);
16559 write_obstack (out_file, &addr_obstack);
16560 write_obstack (out_file, &symtab_obstack);
16561 write_obstack (out_file, &constant_pool);
16562
16563 fclose (out_file);
16564
16565 /* We want to keep the file, so we set cleanup_filename to NULL
16566 here. See unlink_if_set. */
16567 cleanup_filename = NULL;
16568
16569 do_cleanups (cleanup);
16570 }
16571
16572 /* Implementation of the `save gdb-index' command.
16573
16574 Note that the file format used by this command is documented in the
16575 GDB manual. Any changes here must be documented there. */
16576
16577 static void
16578 save_gdb_index_command (char *arg, int from_tty)
16579 {
16580 struct objfile *objfile;
16581
16582 if (!arg || !*arg)
16583 error (_("usage: save gdb-index DIRECTORY"));
16584
16585 ALL_OBJFILES (objfile)
16586 {
16587 struct stat st;
16588
16589 /* If the objfile does not correspond to an actual file, skip it. */
16590 if (stat (objfile->name, &st) < 0)
16591 continue;
16592
16593 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16594 if (dwarf2_per_objfile)
16595 {
16596 volatile struct gdb_exception except;
16597
16598 TRY_CATCH (except, RETURN_MASK_ERROR)
16599 {
16600 write_psymtabs_to_index (objfile, arg);
16601 }
16602 if (except.reason < 0)
16603 exception_fprintf (gdb_stderr, except,
16604 _("Error while writing index for `%s': "),
16605 objfile->name);
16606 }
16607 }
16608 }
16609
16610 \f
16611
16612 int dwarf2_always_disassemble;
16613
16614 static void
16615 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16616 struct cmd_list_element *c, const char *value)
16617 {
16618 fprintf_filtered (file,
16619 _("Whether to always disassemble "
16620 "DWARF expressions is %s.\n"),
16621 value);
16622 }
16623
16624 static void
16625 show_check_physname (struct ui_file *file, int from_tty,
16626 struct cmd_list_element *c, const char *value)
16627 {
16628 fprintf_filtered (file,
16629 _("Whether to check \"physname\" is %s.\n"),
16630 value);
16631 }
16632
16633 void _initialize_dwarf2_read (void);
16634
16635 void
16636 _initialize_dwarf2_read (void)
16637 {
16638 struct cmd_list_element *c;
16639
16640 dwarf2_objfile_data_key
16641 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16642
16643 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16644 Set DWARF 2 specific variables.\n\
16645 Configure DWARF 2 variables such as the cache size"),
16646 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16647 0/*allow-unknown*/, &maintenance_set_cmdlist);
16648
16649 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16650 Show DWARF 2 specific variables\n\
16651 Show DWARF 2 variables such as the cache size"),
16652 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16653 0/*allow-unknown*/, &maintenance_show_cmdlist);
16654
16655 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16656 &dwarf2_max_cache_age, _("\
16657 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16658 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16659 A higher limit means that cached compilation units will be stored\n\
16660 in memory longer, and more total memory will be used. Zero disables\n\
16661 caching, which can slow down startup."),
16662 NULL,
16663 show_dwarf2_max_cache_age,
16664 &set_dwarf2_cmdlist,
16665 &show_dwarf2_cmdlist);
16666
16667 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16668 &dwarf2_always_disassemble, _("\
16669 Set whether `info address' always disassembles DWARF expressions."), _("\
16670 Show whether `info address' always disassembles DWARF expressions."), _("\
16671 When enabled, DWARF expressions are always printed in an assembly-like\n\
16672 syntax. When disabled, expressions will be printed in a more\n\
16673 conversational style, when possible."),
16674 NULL,
16675 show_dwarf2_always_disassemble,
16676 &set_dwarf2_cmdlist,
16677 &show_dwarf2_cmdlist);
16678
16679 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16680 Set debugging of the dwarf2 DIE reader."), _("\
16681 Show debugging of the dwarf2 DIE reader."), _("\
16682 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16683 The value is the maximum depth to print."),
16684 NULL,
16685 NULL,
16686 &setdebuglist, &showdebuglist);
16687
16688 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16689 Set cross-checking of \"physname\" code against demangler."), _("\
16690 Show cross-checking of \"physname\" code against demangler."), _("\
16691 When enabled, GDB's internal \"physname\" code is checked against\n\
16692 the demangler."),
16693 NULL, show_check_physname,
16694 &setdebuglist, &showdebuglist);
16695
16696 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16697 _("\
16698 Save a gdb-index file.\n\
16699 Usage: save gdb-index DIRECTORY"),
16700 &save_cmdlist);
16701 set_cmd_completer (c, filename_completer);
16702 }
This page took 0.419448 seconds and 4 git commands to generate.