* dwarf.h (enum DW_CFA): Define.
[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 /* All offsets in the index are of this type. It must be
149 architecture-independent. */
150 typedef uint32_t offset_type;
151
152 DEF_VEC_I (offset_type);
153
154 /* A description of the mapped index. The file format is described in
155 a comment by the code that writes the index. */
156 struct mapped_index
157 {
158 /* Index data format version. */
159 int version;
160
161 /* The total length of the buffer. */
162 off_t total_size;
163
164 /* A pointer to the address table data. */
165 const gdb_byte *address_table;
166
167 /* Size of the address table data in bytes. */
168 offset_type address_table_size;
169
170 /* The symbol table, implemented as a hash table. */
171 const offset_type *symbol_table;
172
173 /* Size in slots, each slot is 2 offset_types. */
174 offset_type symbol_table_slots;
175
176 /* A pointer to the constant pool. */
177 const char *constant_pool;
178 };
179
180 struct dwarf2_per_objfile
181 {
182 struct dwarf2_section_info info;
183 struct dwarf2_section_info abbrev;
184 struct dwarf2_section_info line;
185 struct dwarf2_section_info loc;
186 struct dwarf2_section_info macinfo;
187 struct dwarf2_section_info str;
188 struct dwarf2_section_info ranges;
189 struct dwarf2_section_info types;
190 struct dwarf2_section_info frame;
191 struct dwarf2_section_info eh_frame;
192 struct dwarf2_section_info gdb_index;
193
194 /* Back link. */
195 struct objfile *objfile;
196
197 /* A list of all the compilation units. This is used to locate
198 the target compilation unit of a particular reference. */
199 struct dwarf2_per_cu_data **all_comp_units;
200
201 /* The number of compilation units in ALL_COMP_UNITS. */
202 int n_comp_units;
203
204 /* The number of .debug_types-related CUs. */
205 int n_type_comp_units;
206
207 /* The .debug_types-related CUs. */
208 struct dwarf2_per_cu_data **type_comp_units;
209
210 /* A chain of compilation units that are currently read in, so that
211 they can be freed later. */
212 struct dwarf2_per_cu_data *read_in_chain;
213
214 /* A table mapping .debug_types signatures to its signatured_type entry.
215 This is NULL if the .debug_types section hasn't been read in yet. */
216 htab_t signatured_types;
217
218 /* A flag indicating wether this objfile has a section loaded at a
219 VMA of 0. */
220 int has_section_at_zero;
221
222 /* True if we are using the mapped index,
223 or we are faking it for OBJF_READNOW's sake. */
224 unsigned char using_index;
225
226 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
227 struct mapped_index *index_table;
228
229 /* When using index_table, this keeps track of all quick_file_names entries.
230 TUs can share line table entries with CUs or other TUs, and there can be
231 a lot more TUs than unique line tables, so we maintain a separate table
232 of all line table entries to support the sharing. */
233 htab_t quick_file_names_table;
234
235 /* Set during partial symbol reading, to prevent queueing of full
236 symbols. */
237 int reading_partial_symbols;
238
239 /* Table mapping type .debug_info DIE offsets to types.
240 This is NULL if not allocated yet.
241 It (currently) makes sense to allocate debug_types_type_hash lazily.
242 To keep things simple we allocate both lazily. */
243 htab_t debug_info_type_hash;
244
245 /* Table mapping type .debug_types DIE offsets to types.
246 This is NULL if not allocated yet. */
247 htab_t debug_types_type_hash;
248 };
249
250 static struct dwarf2_per_objfile *dwarf2_per_objfile;
251
252 /* Default names of the debugging sections. */
253
254 /* Note that if the debugging section has been compressed, it might
255 have a name like .zdebug_info. */
256
257 static const struct dwarf2_debug_sections dwarf2_elf_names = {
258 { ".debug_info", ".zdebug_info" },
259 { ".debug_abbrev", ".zdebug_abbrev" },
260 { ".debug_line", ".zdebug_line" },
261 { ".debug_loc", ".zdebug_loc" },
262 { ".debug_macinfo", ".zdebug_macinfo" },
263 { ".debug_str", ".zdebug_str" },
264 { ".debug_ranges", ".zdebug_ranges" },
265 { ".debug_types", ".zdebug_types" },
266 { ".debug_frame", ".zdebug_frame" },
267 { ".eh_frame", NULL },
268 { ".gdb_index", ".zgdb_index" }
269 };
270
271 /* local data types */
272
273 /* We hold several abbreviation tables in memory at the same time. */
274 #ifndef ABBREV_HASH_SIZE
275 #define ABBREV_HASH_SIZE 121
276 #endif
277
278 /* The data in a compilation unit header, after target2host
279 translation, looks like this. */
280 struct comp_unit_head
281 {
282 unsigned int length;
283 short version;
284 unsigned char addr_size;
285 unsigned char signed_addr_p;
286 unsigned int abbrev_offset;
287
288 /* Size of file offsets; either 4 or 8. */
289 unsigned int offset_size;
290
291 /* Size of the length field; either 4 or 12. */
292 unsigned int initial_length_size;
293
294 /* Offset to the first byte of this compilation unit header in the
295 .debug_info section, for resolving relative reference dies. */
296 unsigned int offset;
297
298 /* Offset to first die in this cu from the start of the cu.
299 This will be the first byte following the compilation unit header. */
300 unsigned int first_die_offset;
301 };
302
303 /* Type used for delaying computation of method physnames.
304 See comments for compute_delayed_physnames. */
305 struct delayed_method_info
306 {
307 /* The type to which the method is attached, i.e., its parent class. */
308 struct type *type;
309
310 /* The index of the method in the type's function fieldlists. */
311 int fnfield_index;
312
313 /* The index of the method in the fieldlist. */
314 int index;
315
316 /* The name of the DIE. */
317 const char *name;
318
319 /* The DIE associated with this method. */
320 struct die_info *die;
321 };
322
323 typedef struct delayed_method_info delayed_method_info;
324 DEF_VEC_O (delayed_method_info);
325
326 /* Internal state when decoding a particular compilation unit. */
327 struct dwarf2_cu
328 {
329 /* The objfile containing this compilation unit. */
330 struct objfile *objfile;
331
332 /* The header of the compilation unit. */
333 struct comp_unit_head header;
334
335 /* Base address of this compilation unit. */
336 CORE_ADDR base_address;
337
338 /* Non-zero if base_address has been set. */
339 int base_known;
340
341 struct function_range *first_fn, *last_fn, *cached_fn;
342
343 /* The language we are debugging. */
344 enum language language;
345 const struct language_defn *language_defn;
346
347 const char *producer;
348
349 /* The generic symbol table building routines have separate lists for
350 file scope symbols and all all other scopes (local scopes). So
351 we need to select the right one to pass to add_symbol_to_list().
352 We do it by keeping a pointer to the correct list in list_in_scope.
353
354 FIXME: The original dwarf code just treated the file scope as the
355 first local scope, and all other local scopes as nested local
356 scopes, and worked fine. Check to see if we really need to
357 distinguish these in buildsym.c. */
358 struct pending **list_in_scope;
359
360 /* DWARF abbreviation table associated with this compilation unit. */
361 struct abbrev_info **dwarf2_abbrevs;
362
363 /* Storage for the abbrev table. */
364 struct obstack abbrev_obstack;
365
366 /* Hash table holding all the loaded partial DIEs. */
367 htab_t partial_dies;
368
369 /* Storage for things with the same lifetime as this read-in compilation
370 unit, including partial DIEs. */
371 struct obstack comp_unit_obstack;
372
373 /* When multiple dwarf2_cu structures are living in memory, this field
374 chains them all together, so that they can be released efficiently.
375 We will probably also want a generation counter so that most-recently-used
376 compilation units are cached... */
377 struct dwarf2_per_cu_data *read_in_chain;
378
379 /* Backchain to our per_cu entry if the tree has been built. */
380 struct dwarf2_per_cu_data *per_cu;
381
382 /* How many compilation units ago was this CU last referenced? */
383 int last_used;
384
385 /* A hash table of die offsets for following references. */
386 htab_t die_hash;
387
388 /* Full DIEs if read in. */
389 struct die_info *dies;
390
391 /* A set of pointers to dwarf2_per_cu_data objects for compilation
392 units referenced by this one. Only set during full symbol processing;
393 partial symbol tables do not have dependencies. */
394 htab_t dependencies;
395
396 /* Header data from the line table, during full symbol processing. */
397 struct line_header *line_header;
398
399 /* A list of methods which need to have physnames computed
400 after all type information has been read. */
401 VEC (delayed_method_info) *method_list;
402
403 /* Mark used when releasing cached dies. */
404 unsigned int mark : 1;
405
406 /* This flag will be set if this compilation unit might include
407 inter-compilation-unit references. */
408 unsigned int has_form_ref_addr : 1;
409
410 /* This flag will be set if this compilation unit includes any
411 DW_TAG_namespace DIEs. If we know that there are explicit
412 DIEs for namespaces, we don't need to try to infer them
413 from mangled names. */
414 unsigned int has_namespace_info : 1;
415
416 /* This CU references .debug_loc. See the symtab->locations_valid field.
417 This test is imperfect as there may exist optimized debug code not using
418 any location list and still facing inlining issues if handled as
419 unoptimized code. For a future better test see GCC PR other/32998. */
420
421 unsigned int has_loclist : 1;
422 };
423
424 /* Persistent data held for a compilation unit, even when not
425 processing it. We put a pointer to this structure in the
426 read_symtab_private field of the psymtab. If we encounter
427 inter-compilation-unit references, we also maintain a sorted
428 list of all compilation units. */
429
430 struct dwarf2_per_cu_data
431 {
432 /* The start offset and length of this compilation unit. 2**29-1
433 bytes should suffice to store the length of any compilation unit
434 - if it doesn't, GDB will fall over anyway.
435 NOTE: Unlike comp_unit_head.length, this length includes
436 initial_length_size. */
437 unsigned int offset;
438 unsigned int length : 29;
439
440 /* Flag indicating this compilation unit will be read in before
441 any of the current compilation units are processed. */
442 unsigned int queued : 1;
443
444 /* This flag will be set if we need to load absolutely all DIEs
445 for this compilation unit, instead of just the ones we think
446 are interesting. It gets set if we look for a DIE in the
447 hash table and don't find it. */
448 unsigned int load_all_dies : 1;
449
450 /* Non-zero if this CU is from .debug_types.
451 Otherwise it's from .debug_info. */
452 unsigned int from_debug_types : 1;
453
454 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
455 of the CU cache it gets reset to NULL again. */
456 struct dwarf2_cu *cu;
457
458 /* The corresponding objfile. */
459 struct objfile *objfile;
460
461 /* When using partial symbol tables, the 'psymtab' field is active.
462 Otherwise the 'quick' field is active. */
463 union
464 {
465 /* The partial symbol table associated with this compilation unit,
466 or NULL for partial units (which do not have an associated
467 symtab). */
468 struct partial_symtab *psymtab;
469
470 /* Data needed by the "quick" functions. */
471 struct dwarf2_per_cu_quick_data *quick;
472 } v;
473 };
474
475 /* Entry in the signatured_types hash table. */
476
477 struct signatured_type
478 {
479 ULONGEST signature;
480
481 /* Offset in .debug_types of the type defined by this TU. */
482 unsigned int type_offset;
483
484 /* The CU(/TU) of this type. */
485 struct dwarf2_per_cu_data per_cu;
486 };
487
488 /* Struct used to pass misc. parameters to read_die_and_children, et
489 al. which are used for both .debug_info and .debug_types dies.
490 All parameters here are unchanging for the life of the call. This
491 struct exists to abstract away the constant parameters of die
492 reading. */
493
494 struct die_reader_specs
495 {
496 /* The bfd of this objfile. */
497 bfd* abfd;
498
499 /* The CU of the DIE we are parsing. */
500 struct dwarf2_cu *cu;
501
502 /* Pointer to start of section buffer.
503 This is either the start of .debug_info or .debug_types. */
504 const gdb_byte *buffer;
505 };
506
507 /* The line number information for a compilation unit (found in the
508 .debug_line section) begins with a "statement program header",
509 which contains the following information. */
510 struct line_header
511 {
512 unsigned int total_length;
513 unsigned short version;
514 unsigned int header_length;
515 unsigned char minimum_instruction_length;
516 unsigned char maximum_ops_per_instruction;
517 unsigned char default_is_stmt;
518 int line_base;
519 unsigned char line_range;
520 unsigned char opcode_base;
521
522 /* standard_opcode_lengths[i] is the number of operands for the
523 standard opcode whose value is i. This means that
524 standard_opcode_lengths[0] is unused, and the last meaningful
525 element is standard_opcode_lengths[opcode_base - 1]. */
526 unsigned char *standard_opcode_lengths;
527
528 /* The include_directories table. NOTE! These strings are not
529 allocated with xmalloc; instead, they are pointers into
530 debug_line_buffer. If you try to free them, `free' will get
531 indigestion. */
532 unsigned int num_include_dirs, include_dirs_size;
533 char **include_dirs;
534
535 /* The file_names table. NOTE! These strings are not allocated
536 with xmalloc; instead, they are pointers into debug_line_buffer.
537 Don't try to free them directly. */
538 unsigned int num_file_names, file_names_size;
539 struct file_entry
540 {
541 char *name;
542 unsigned int dir_index;
543 unsigned int mod_time;
544 unsigned int length;
545 int included_p; /* Non-zero if referenced by the Line Number Program. */
546 struct symtab *symtab; /* The associated symbol table, if any. */
547 } *file_names;
548
549 /* The start and end of the statement program following this
550 header. These point into dwarf2_per_objfile->line_buffer. */
551 gdb_byte *statement_program_start, *statement_program_end;
552 };
553
554 /* When we construct a partial symbol table entry we only
555 need this much information. */
556 struct partial_die_info
557 {
558 /* Offset of this DIE. */
559 unsigned int offset;
560
561 /* DWARF-2 tag for this DIE. */
562 ENUM_BITFIELD(dwarf_tag) tag : 16;
563
564 /* Assorted flags describing the data found in this DIE. */
565 unsigned int has_children : 1;
566 unsigned int is_external : 1;
567 unsigned int is_declaration : 1;
568 unsigned int has_type : 1;
569 unsigned int has_specification : 1;
570 unsigned int has_pc_info : 1;
571
572 /* Flag set if the SCOPE field of this structure has been
573 computed. */
574 unsigned int scope_set : 1;
575
576 /* Flag set if the DIE has a byte_size attribute. */
577 unsigned int has_byte_size : 1;
578
579 /* Flag set if any of the DIE's children are template arguments. */
580 unsigned int has_template_arguments : 1;
581
582 /* Flag set if fixup_partial_die has been called on this die. */
583 unsigned int fixup_called : 1;
584
585 /* The name of this DIE. Normally the value of DW_AT_name, but
586 sometimes a default name for unnamed DIEs. */
587 char *name;
588
589 /* The linkage name, if present. */
590 const char *linkage_name;
591
592 /* The scope to prepend to our children. This is generally
593 allocated on the comp_unit_obstack, so will disappear
594 when this compilation unit leaves the cache. */
595 char *scope;
596
597 /* The location description associated with this DIE, if any. */
598 struct dwarf_block *locdesc;
599
600 /* If HAS_PC_INFO, the PC range associated with this DIE. */
601 CORE_ADDR lowpc;
602 CORE_ADDR highpc;
603
604 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
605 DW_AT_sibling, if any. */
606 /* NOTE: This member isn't strictly necessary, read_partial_die could
607 return DW_AT_sibling values to its caller load_partial_dies. */
608 gdb_byte *sibling;
609
610 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
611 DW_AT_specification (or DW_AT_abstract_origin or
612 DW_AT_extension). */
613 unsigned int spec_offset;
614
615 /* Pointers to this DIE's parent, first child, and next sibling,
616 if any. */
617 struct partial_die_info *die_parent, *die_child, *die_sibling;
618 };
619
620 /* This data structure holds the information of an abbrev. */
621 struct abbrev_info
622 {
623 unsigned int number; /* number identifying abbrev */
624 enum dwarf_tag tag; /* dwarf tag */
625 unsigned short has_children; /* boolean */
626 unsigned short num_attrs; /* number of attributes */
627 struct attr_abbrev *attrs; /* an array of attribute descriptions */
628 struct abbrev_info *next; /* next in chain */
629 };
630
631 struct attr_abbrev
632 {
633 ENUM_BITFIELD(dwarf_attribute) name : 16;
634 ENUM_BITFIELD(dwarf_form) form : 16;
635 };
636
637 /* Attributes have a name and a value. */
638 struct attribute
639 {
640 ENUM_BITFIELD(dwarf_attribute) name : 16;
641 ENUM_BITFIELD(dwarf_form) form : 15;
642
643 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
644 field should be in u.str (existing only for DW_STRING) but it is kept
645 here for better struct attribute alignment. */
646 unsigned int string_is_canonical : 1;
647
648 union
649 {
650 char *str;
651 struct dwarf_block *blk;
652 ULONGEST unsnd;
653 LONGEST snd;
654 CORE_ADDR addr;
655 struct signatured_type *signatured_type;
656 }
657 u;
658 };
659
660 /* This data structure holds a complete die structure. */
661 struct die_info
662 {
663 /* DWARF-2 tag for this DIE. */
664 ENUM_BITFIELD(dwarf_tag) tag : 16;
665
666 /* Number of attributes */
667 unsigned char num_attrs;
668
669 /* True if we're presently building the full type name for the
670 type derived from this DIE. */
671 unsigned char building_fullname : 1;
672
673 /* Abbrev number */
674 unsigned int abbrev;
675
676 /* Offset in .debug_info or .debug_types section. */
677 unsigned int offset;
678
679 /* The dies in a compilation unit form an n-ary tree. PARENT
680 points to this die's parent; CHILD points to the first child of
681 this node; and all the children of a given node are chained
682 together via their SIBLING fields. */
683 struct die_info *child; /* Its first child, if any. */
684 struct die_info *sibling; /* Its next sibling, if any. */
685 struct die_info *parent; /* Its parent, if any. */
686
687 /* An array of attributes, with NUM_ATTRS elements. There may be
688 zero, but it's not common and zero-sized arrays are not
689 sufficiently portable C. */
690 struct attribute attrs[1];
691 };
692
693 struct function_range
694 {
695 const char *name;
696 CORE_ADDR lowpc, highpc;
697 int seen_line;
698 struct function_range *next;
699 };
700
701 /* Get at parts of an attribute structure. */
702
703 #define DW_STRING(attr) ((attr)->u.str)
704 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
705 #define DW_UNSND(attr) ((attr)->u.unsnd)
706 #define DW_BLOCK(attr) ((attr)->u.blk)
707 #define DW_SND(attr) ((attr)->u.snd)
708 #define DW_ADDR(attr) ((attr)->u.addr)
709 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
710
711 /* Blocks are a bunch of untyped bytes. */
712 struct dwarf_block
713 {
714 unsigned int size;
715 gdb_byte *data;
716 };
717
718 #ifndef ATTR_ALLOC_CHUNK
719 #define ATTR_ALLOC_CHUNK 4
720 #endif
721
722 /* Allocate fields for structs, unions and enums in this size. */
723 #ifndef DW_FIELD_ALLOC_CHUNK
724 #define DW_FIELD_ALLOC_CHUNK 4
725 #endif
726
727 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
728 but this would require a corresponding change in unpack_field_as_long
729 and friends. */
730 static int bits_per_byte = 8;
731
732 /* The routines that read and process dies for a C struct or C++ class
733 pass lists of data member fields and lists of member function fields
734 in an instance of a field_info structure, as defined below. */
735 struct field_info
736 {
737 /* List of data member and baseclasses fields. */
738 struct nextfield
739 {
740 struct nextfield *next;
741 int accessibility;
742 int virtuality;
743 struct field field;
744 }
745 *fields, *baseclasses;
746
747 /* Number of fields (including baseclasses). */
748 int nfields;
749
750 /* Number of baseclasses. */
751 int nbaseclasses;
752
753 /* Set if the accesibility of one of the fields is not public. */
754 int non_public_fields;
755
756 /* Member function fields array, entries are allocated in the order they
757 are encountered in the object file. */
758 struct nextfnfield
759 {
760 struct nextfnfield *next;
761 struct fn_field fnfield;
762 }
763 *fnfields;
764
765 /* Member function fieldlist array, contains name of possibly overloaded
766 member function, number of overloaded member functions and a pointer
767 to the head of the member function field chain. */
768 struct fnfieldlist
769 {
770 char *name;
771 int length;
772 struct nextfnfield *head;
773 }
774 *fnfieldlists;
775
776 /* Number of entries in the fnfieldlists array. */
777 int nfnfields;
778
779 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
780 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
781 struct typedef_field_list
782 {
783 struct typedef_field field;
784 struct typedef_field_list *next;
785 }
786 *typedef_field_list;
787 unsigned typedef_field_list_count;
788 };
789
790 /* One item on the queue of compilation units to read in full symbols
791 for. */
792 struct dwarf2_queue_item
793 {
794 struct dwarf2_per_cu_data *per_cu;
795 struct dwarf2_queue_item *next;
796 };
797
798 /* The current queue. */
799 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
800
801 /* Loaded secondary compilation units are kept in memory until they
802 have not been referenced for the processing of this many
803 compilation units. Set this to zero to disable caching. Cache
804 sizes of up to at least twenty will improve startup time for
805 typical inter-CU-reference binaries, at an obvious memory cost. */
806 static int dwarf2_max_cache_age = 5;
807 static void
808 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
809 struct cmd_list_element *c, const char *value)
810 {
811 fprintf_filtered (file, _("The upper bound on the age of cached "
812 "dwarf2 compilation units is %s.\n"),
813 value);
814 }
815
816
817 /* Various complaints about symbol reading that don't abort the process. */
818
819 static void
820 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
821 {
822 complaint (&symfile_complaints,
823 _("statement list doesn't fit in .debug_line section"));
824 }
825
826 static void
827 dwarf2_debug_line_missing_file_complaint (void)
828 {
829 complaint (&symfile_complaints,
830 _(".debug_line section has line data without a file"));
831 }
832
833 static void
834 dwarf2_debug_line_missing_end_sequence_complaint (void)
835 {
836 complaint (&symfile_complaints,
837 _(".debug_line section has line "
838 "program sequence without an end"));
839 }
840
841 static void
842 dwarf2_complex_location_expr_complaint (void)
843 {
844 complaint (&symfile_complaints, _("location expression too complex"));
845 }
846
847 static void
848 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
849 int arg3)
850 {
851 complaint (&symfile_complaints,
852 _("const value length mismatch for '%s', got %d, expected %d"),
853 arg1, arg2, arg3);
854 }
855
856 static void
857 dwarf2_macros_too_long_complaint (void)
858 {
859 complaint (&symfile_complaints,
860 _("macro info runs off end of `.debug_macinfo' section"));
861 }
862
863 static void
864 dwarf2_macro_malformed_definition_complaint (const char *arg1)
865 {
866 complaint (&symfile_complaints,
867 _("macro debug info contains a "
868 "malformed macro definition:\n`%s'"),
869 arg1);
870 }
871
872 static void
873 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
874 {
875 complaint (&symfile_complaints,
876 _("invalid attribute class or form for '%s' in '%s'"),
877 arg1, arg2);
878 }
879
880 /* local function prototypes */
881
882 static void dwarf2_locate_sections (bfd *, asection *, void *);
883
884 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
885 struct objfile *);
886
887 static void dwarf2_build_psymtabs_hard (struct objfile *);
888
889 static void scan_partial_symbols (struct partial_die_info *,
890 CORE_ADDR *, CORE_ADDR *,
891 int, struct dwarf2_cu *);
892
893 static void add_partial_symbol (struct partial_die_info *,
894 struct dwarf2_cu *);
895
896 static void add_partial_namespace (struct partial_die_info *pdi,
897 CORE_ADDR *lowpc, CORE_ADDR *highpc,
898 int need_pc, struct dwarf2_cu *cu);
899
900 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
901 CORE_ADDR *highpc, int need_pc,
902 struct dwarf2_cu *cu);
903
904 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
905 struct dwarf2_cu *cu);
906
907 static void add_partial_subprogram (struct partial_die_info *pdi,
908 CORE_ADDR *lowpc, CORE_ADDR *highpc,
909 int need_pc, struct dwarf2_cu *cu);
910
911 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
912 gdb_byte *buffer, gdb_byte *info_ptr,
913 bfd *abfd, struct dwarf2_cu *cu);
914
915 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
916
917 static void psymtab_to_symtab_1 (struct partial_symtab *);
918
919 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
920
921 static void dwarf2_free_abbrev_table (void *);
922
923 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
924 struct dwarf2_cu *);
925
926 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
927 struct dwarf2_cu *);
928
929 static struct partial_die_info *load_partial_dies (bfd *,
930 gdb_byte *, gdb_byte *,
931 int, struct dwarf2_cu *);
932
933 static gdb_byte *read_partial_die (struct partial_die_info *,
934 struct abbrev_info *abbrev,
935 unsigned int, bfd *,
936 gdb_byte *, gdb_byte *,
937 struct dwarf2_cu *);
938
939 static struct partial_die_info *find_partial_die (unsigned int,
940 struct dwarf2_cu *);
941
942 static void fixup_partial_die (struct partial_die_info *,
943 struct dwarf2_cu *);
944
945 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
946 bfd *, gdb_byte *, struct dwarf2_cu *);
947
948 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
949 bfd *, gdb_byte *, struct dwarf2_cu *);
950
951 static unsigned int read_1_byte (bfd *, gdb_byte *);
952
953 static int read_1_signed_byte (bfd *, gdb_byte *);
954
955 static unsigned int read_2_bytes (bfd *, gdb_byte *);
956
957 static unsigned int read_4_bytes (bfd *, gdb_byte *);
958
959 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
960
961 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
962 unsigned int *);
963
964 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
965
966 static LONGEST read_checked_initial_length_and_offset
967 (bfd *, gdb_byte *, const struct comp_unit_head *,
968 unsigned int *, unsigned int *);
969
970 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
971 unsigned int *);
972
973 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
974
975 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
976
977 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
978
979 static char *read_indirect_string (bfd *, gdb_byte *,
980 const struct comp_unit_head *,
981 unsigned int *);
982
983 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
984
985 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
986
987 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
988
989 static void set_cu_language (unsigned int, struct dwarf2_cu *);
990
991 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
992 struct dwarf2_cu *);
993
994 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
995 unsigned int,
996 struct dwarf2_cu *);
997
998 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
999 struct dwarf2_cu *cu);
1000
1001 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
1002
1003 static struct die_info *die_specification (struct die_info *die,
1004 struct dwarf2_cu **);
1005
1006 static void free_line_header (struct line_header *lh);
1007
1008 static void add_file_name (struct line_header *, char *, unsigned int,
1009 unsigned int, unsigned int);
1010
1011 static struct line_header *(dwarf_decode_line_header
1012 (unsigned int offset,
1013 bfd *abfd, struct dwarf2_cu *cu));
1014
1015 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1016 struct dwarf2_cu *, struct partial_symtab *);
1017
1018 static void dwarf2_start_subfile (char *, const char *, const char *);
1019
1020 static struct symbol *new_symbol (struct die_info *, struct type *,
1021 struct dwarf2_cu *);
1022
1023 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1024 struct dwarf2_cu *, struct symbol *);
1025
1026 static void dwarf2_const_value (struct attribute *, struct symbol *,
1027 struct dwarf2_cu *);
1028
1029 static void dwarf2_const_value_attr (struct attribute *attr,
1030 struct type *type,
1031 const char *name,
1032 struct obstack *obstack,
1033 struct dwarf2_cu *cu, long *value,
1034 gdb_byte **bytes,
1035 struct dwarf2_locexpr_baton **baton);
1036
1037 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1038
1039 static int need_gnat_info (struct dwarf2_cu *);
1040
1041 static struct type *die_descriptive_type (struct die_info *,
1042 struct dwarf2_cu *);
1043
1044 static void set_descriptive_type (struct type *, struct die_info *,
1045 struct dwarf2_cu *);
1046
1047 static struct type *die_containing_type (struct die_info *,
1048 struct dwarf2_cu *);
1049
1050 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1051 struct dwarf2_cu *);
1052
1053 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1054
1055 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1056
1057 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1058
1059 static char *typename_concat (struct obstack *obs, const char *prefix,
1060 const char *suffix, int physname,
1061 struct dwarf2_cu *cu);
1062
1063 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1064
1065 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1066
1067 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1068
1069 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1070
1071 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1072 struct dwarf2_cu *, struct partial_symtab *);
1073
1074 static int dwarf2_get_pc_bounds (struct die_info *,
1075 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1076 struct partial_symtab *);
1077
1078 static void get_scope_pc_bounds (struct die_info *,
1079 CORE_ADDR *, CORE_ADDR *,
1080 struct dwarf2_cu *);
1081
1082 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1083 CORE_ADDR, struct dwarf2_cu *);
1084
1085 static void dwarf2_add_field (struct field_info *, struct die_info *,
1086 struct dwarf2_cu *);
1087
1088 static void dwarf2_attach_fields_to_type (struct field_info *,
1089 struct type *, struct dwarf2_cu *);
1090
1091 static void dwarf2_add_member_fn (struct field_info *,
1092 struct die_info *, struct type *,
1093 struct dwarf2_cu *);
1094
1095 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1096 struct type *,
1097 struct dwarf2_cu *);
1098
1099 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1100
1101 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1102
1103 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1104
1105 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1106
1107 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1108
1109 static struct type *read_module_type (struct die_info *die,
1110 struct dwarf2_cu *cu);
1111
1112 static const char *namespace_name (struct die_info *die,
1113 int *is_anonymous, struct dwarf2_cu *);
1114
1115 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1116
1117 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1118
1119 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1120 struct dwarf2_cu *);
1121
1122 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1123
1124 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1125 gdb_byte *info_ptr,
1126 gdb_byte **new_info_ptr,
1127 struct die_info *parent);
1128
1129 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1130 gdb_byte *info_ptr,
1131 gdb_byte **new_info_ptr,
1132 struct die_info *parent);
1133
1134 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1135 gdb_byte *info_ptr,
1136 gdb_byte **new_info_ptr,
1137 struct die_info *parent);
1138
1139 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1140 struct die_info **, gdb_byte *,
1141 int *);
1142
1143 static void process_die (struct die_info *, struct dwarf2_cu *);
1144
1145 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1146 struct obstack *);
1147
1148 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1149
1150 static const char *dwarf2_full_name (char *name,
1151 struct die_info *die,
1152 struct dwarf2_cu *cu);
1153
1154 static struct die_info *dwarf2_extension (struct die_info *die,
1155 struct dwarf2_cu **);
1156
1157 static char *dwarf_tag_name (unsigned int);
1158
1159 static char *dwarf_attr_name (unsigned int);
1160
1161 static char *dwarf_form_name (unsigned int);
1162
1163 static char *dwarf_bool_name (unsigned int);
1164
1165 static char *dwarf_type_encoding_name (unsigned int);
1166
1167 #if 0
1168 static char *dwarf_cfi_name (unsigned int);
1169 #endif
1170
1171 static struct die_info *sibling_die (struct die_info *);
1172
1173 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1174
1175 static void dump_die_for_error (struct die_info *);
1176
1177 static void dump_die_1 (struct ui_file *, int level, int max_level,
1178 struct die_info *);
1179
1180 /*static*/ void dump_die (struct die_info *, int max_level);
1181
1182 static void store_in_ref_table (struct die_info *,
1183 struct dwarf2_cu *);
1184
1185 static int is_ref_attr (struct attribute *);
1186
1187 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1188
1189 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1190
1191 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1192 struct attribute *,
1193 struct dwarf2_cu **);
1194
1195 static struct die_info *follow_die_ref (struct die_info *,
1196 struct attribute *,
1197 struct dwarf2_cu **);
1198
1199 static struct die_info *follow_die_sig (struct die_info *,
1200 struct attribute *,
1201 struct dwarf2_cu **);
1202
1203 static void read_signatured_type_at_offset (struct objfile *objfile,
1204 unsigned int offset);
1205
1206 static void read_signatured_type (struct objfile *,
1207 struct signatured_type *type_sig);
1208
1209 /* memory allocation interface */
1210
1211 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1212
1213 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1214
1215 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1216
1217 static void initialize_cu_func_list (struct dwarf2_cu *);
1218
1219 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1220 struct dwarf2_cu *);
1221
1222 static void dwarf_decode_macros (struct line_header *, unsigned int,
1223 char *, bfd *, struct dwarf2_cu *);
1224
1225 static int attr_form_is_block (struct attribute *);
1226
1227 static int attr_form_is_section_offset (struct attribute *);
1228
1229 static int attr_form_is_constant (struct attribute *);
1230
1231 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1232 struct dwarf2_loclist_baton *baton,
1233 struct attribute *attr);
1234
1235 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1236 struct symbol *sym,
1237 struct dwarf2_cu *cu);
1238
1239 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1240 struct abbrev_info *abbrev,
1241 struct dwarf2_cu *cu);
1242
1243 static void free_stack_comp_unit (void *);
1244
1245 static hashval_t partial_die_hash (const void *item);
1246
1247 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1248
1249 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1250 (unsigned int offset, struct objfile *objfile);
1251
1252 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1253 (unsigned int offset, struct objfile *objfile);
1254
1255 static void init_one_comp_unit (struct dwarf2_cu *cu,
1256 struct objfile *objfile);
1257
1258 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1259 struct die_info *comp_unit_die);
1260
1261 static void free_one_comp_unit (void *);
1262
1263 static void free_cached_comp_units (void *);
1264
1265 static void age_cached_comp_units (void);
1266
1267 static void free_one_cached_comp_unit (void *);
1268
1269 static struct type *set_die_type (struct die_info *, struct type *,
1270 struct dwarf2_cu *);
1271
1272 static void create_all_comp_units (struct objfile *);
1273
1274 static int create_debug_types_hash_table (struct objfile *objfile);
1275
1276 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1277 struct objfile *);
1278
1279 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1280
1281 static void dwarf2_add_dependence (struct dwarf2_cu *,
1282 struct dwarf2_per_cu_data *);
1283
1284 static void dwarf2_mark (struct dwarf2_cu *);
1285
1286 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1287
1288 static struct type *get_die_type_at_offset (unsigned int,
1289 struct dwarf2_per_cu_data *per_cu);
1290
1291 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1292
1293 static void dwarf2_release_queue (void *dummy);
1294
1295 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1296 struct objfile *objfile);
1297
1298 static void process_queue (struct objfile *objfile);
1299
1300 static void find_file_and_directory (struct die_info *die,
1301 struct dwarf2_cu *cu,
1302 char **name, char **comp_dir);
1303
1304 static char *file_full_name (int file, struct line_header *lh,
1305 const char *comp_dir);
1306
1307 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1308 gdb_byte *info_ptr,
1309 gdb_byte *buffer,
1310 unsigned int buffer_size,
1311 bfd *abfd);
1312
1313 static void init_cu_die_reader (struct die_reader_specs *reader,
1314 struct dwarf2_cu *cu);
1315
1316 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1317
1318 #if WORDS_BIGENDIAN
1319
1320 /* Convert VALUE between big- and little-endian. */
1321 static offset_type
1322 byte_swap (offset_type value)
1323 {
1324 offset_type result;
1325
1326 result = (value & 0xff) << 24;
1327 result |= (value & 0xff00) << 8;
1328 result |= (value & 0xff0000) >> 8;
1329 result |= (value & 0xff000000) >> 24;
1330 return result;
1331 }
1332
1333 #define MAYBE_SWAP(V) byte_swap (V)
1334
1335 #else
1336 #define MAYBE_SWAP(V) (V)
1337 #endif /* WORDS_BIGENDIAN */
1338
1339 /* The suffix for an index file. */
1340 #define INDEX_SUFFIX ".gdb-index"
1341
1342 static const char *dwarf2_physname (char *name, struct die_info *die,
1343 struct dwarf2_cu *cu);
1344
1345 /* Try to locate the sections we need for DWARF 2 debugging
1346 information and return true if we have enough to do something.
1347 NAMES points to the dwarf2 section names, or is NULL if the standard
1348 ELF names are used. */
1349
1350 int
1351 dwarf2_has_info (struct objfile *objfile,
1352 const struct dwarf2_debug_sections *names)
1353 {
1354 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1355 if (!dwarf2_per_objfile)
1356 {
1357 /* Initialize per-objfile state. */
1358 struct dwarf2_per_objfile *data
1359 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1360
1361 memset (data, 0, sizeof (*data));
1362 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1363 dwarf2_per_objfile = data;
1364
1365 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1366 (void *) names);
1367 dwarf2_per_objfile->objfile = objfile;
1368 }
1369 return (dwarf2_per_objfile->info.asection != NULL
1370 && dwarf2_per_objfile->abbrev.asection != NULL);
1371 }
1372
1373 /* When loading sections, we look either for uncompressed section or for
1374 compressed section names. */
1375
1376 static int
1377 section_is_p (const char *section_name,
1378 const struct dwarf2_section_names *names)
1379 {
1380 if (names->normal != NULL
1381 && strcmp (section_name, names->normal) == 0)
1382 return 1;
1383 if (names->compressed != NULL
1384 && strcmp (section_name, names->compressed) == 0)
1385 return 1;
1386 return 0;
1387 }
1388
1389 /* This function is mapped across the sections and remembers the
1390 offset and size of each of the debugging sections we are interested
1391 in. */
1392
1393 static void
1394 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1395 {
1396 const struct dwarf2_debug_sections *names;
1397
1398 if (vnames == NULL)
1399 names = &dwarf2_elf_names;
1400 else
1401 names = (const struct dwarf2_debug_sections *) vnames;
1402
1403 if (section_is_p (sectp->name, &names->info))
1404 {
1405 dwarf2_per_objfile->info.asection = sectp;
1406 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1407 }
1408 else if (section_is_p (sectp->name, &names->abbrev))
1409 {
1410 dwarf2_per_objfile->abbrev.asection = sectp;
1411 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1412 }
1413 else if (section_is_p (sectp->name, &names->line))
1414 {
1415 dwarf2_per_objfile->line.asection = sectp;
1416 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1417 }
1418 else if (section_is_p (sectp->name, &names->loc))
1419 {
1420 dwarf2_per_objfile->loc.asection = sectp;
1421 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1422 }
1423 else if (section_is_p (sectp->name, &names->macinfo))
1424 {
1425 dwarf2_per_objfile->macinfo.asection = sectp;
1426 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1427 }
1428 else if (section_is_p (sectp->name, &names->str))
1429 {
1430 dwarf2_per_objfile->str.asection = sectp;
1431 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1432 }
1433 else if (section_is_p (sectp->name, &names->frame))
1434 {
1435 dwarf2_per_objfile->frame.asection = sectp;
1436 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1437 }
1438 else if (section_is_p (sectp->name, &names->eh_frame))
1439 {
1440 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1441
1442 if (aflag & SEC_HAS_CONTENTS)
1443 {
1444 dwarf2_per_objfile->eh_frame.asection = sectp;
1445 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1446 }
1447 }
1448 else if (section_is_p (sectp->name, &names->ranges))
1449 {
1450 dwarf2_per_objfile->ranges.asection = sectp;
1451 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1452 }
1453 else if (section_is_p (sectp->name, &names->types))
1454 {
1455 dwarf2_per_objfile->types.asection = sectp;
1456 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1457 }
1458 else if (section_is_p (sectp->name, &names->gdb_index))
1459 {
1460 dwarf2_per_objfile->gdb_index.asection = sectp;
1461 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1462 }
1463
1464 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1465 && bfd_section_vma (abfd, sectp) == 0)
1466 dwarf2_per_objfile->has_section_at_zero = 1;
1467 }
1468
1469 /* Decompress a section that was compressed using zlib. Store the
1470 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1471
1472 static void
1473 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1474 gdb_byte **outbuf, bfd_size_type *outsize)
1475 {
1476 bfd *abfd = objfile->obfd;
1477 #ifndef HAVE_ZLIB_H
1478 error (_("Support for zlib-compressed DWARF data (from '%s') "
1479 "is disabled in this copy of GDB"),
1480 bfd_get_filename (abfd));
1481 #else
1482 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1483 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1484 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1485 bfd_size_type uncompressed_size;
1486 gdb_byte *uncompressed_buffer;
1487 z_stream strm;
1488 int rc;
1489 int header_size = 12;
1490
1491 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1492 || bfd_bread (compressed_buffer,
1493 compressed_size, abfd) != compressed_size)
1494 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1495 bfd_get_filename (abfd));
1496
1497 /* Read the zlib header. In this case, it should be "ZLIB" followed
1498 by the uncompressed section size, 8 bytes in big-endian order. */
1499 if (compressed_size < header_size
1500 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1501 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1502 bfd_get_filename (abfd));
1503 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1504 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1505 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1506 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1507 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1508 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1509 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1510 uncompressed_size += compressed_buffer[11];
1511
1512 /* It is possible the section consists of several compressed
1513 buffers concatenated together, so we uncompress in a loop. */
1514 strm.zalloc = NULL;
1515 strm.zfree = NULL;
1516 strm.opaque = NULL;
1517 strm.avail_in = compressed_size - header_size;
1518 strm.next_in = (Bytef*) compressed_buffer + header_size;
1519 strm.avail_out = uncompressed_size;
1520 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1521 uncompressed_size);
1522 rc = inflateInit (&strm);
1523 while (strm.avail_in > 0)
1524 {
1525 if (rc != Z_OK)
1526 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1527 bfd_get_filename (abfd), rc);
1528 strm.next_out = ((Bytef*) uncompressed_buffer
1529 + (uncompressed_size - strm.avail_out));
1530 rc = inflate (&strm, Z_FINISH);
1531 if (rc != Z_STREAM_END)
1532 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1533 bfd_get_filename (abfd), rc);
1534 rc = inflateReset (&strm);
1535 }
1536 rc = inflateEnd (&strm);
1537 if (rc != Z_OK
1538 || strm.avail_out != 0)
1539 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1540 bfd_get_filename (abfd), rc);
1541
1542 do_cleanups (cleanup);
1543 *outbuf = uncompressed_buffer;
1544 *outsize = uncompressed_size;
1545 #endif
1546 }
1547
1548 /* A helper function that decides whether a section is empty. */
1549
1550 static int
1551 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1552 {
1553 return info->asection == NULL || info->size == 0;
1554 }
1555
1556 /* Read the contents of the section SECTP from object file specified by
1557 OBJFILE, store info about the section into INFO.
1558 If the section is compressed, uncompress it before returning. */
1559
1560 static void
1561 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1562 {
1563 bfd *abfd = objfile->obfd;
1564 asection *sectp = info->asection;
1565 gdb_byte *buf, *retbuf;
1566 unsigned char header[4];
1567
1568 if (info->readin)
1569 return;
1570 info->buffer = NULL;
1571 info->map_addr = NULL;
1572 info->readin = 1;
1573
1574 if (dwarf2_section_empty_p (info))
1575 return;
1576
1577 /* Check if the file has a 4-byte header indicating compression. */
1578 if (info->size > sizeof (header)
1579 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1580 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1581 {
1582 /* Upon decompression, update the buffer and its size. */
1583 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1584 {
1585 zlib_decompress_section (objfile, sectp, &info->buffer,
1586 &info->size);
1587 return;
1588 }
1589 }
1590
1591 #ifdef HAVE_MMAP
1592 if (pagesize == 0)
1593 pagesize = getpagesize ();
1594
1595 /* Only try to mmap sections which are large enough: we don't want to
1596 waste space due to fragmentation. Also, only try mmap for sections
1597 without relocations. */
1598
1599 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1600 {
1601 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1602 MAP_PRIVATE, sectp->filepos,
1603 &info->map_addr, &info->map_len);
1604
1605 if ((caddr_t)info->buffer != MAP_FAILED)
1606 {
1607 #if HAVE_POSIX_MADVISE
1608 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1609 #endif
1610 return;
1611 }
1612 }
1613 #endif
1614
1615 /* If we get here, we are a normal, not-compressed section. */
1616 info->buffer = buf
1617 = obstack_alloc (&objfile->objfile_obstack, info->size);
1618
1619 /* When debugging .o files, we may need to apply relocations; see
1620 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1621 We never compress sections in .o files, so we only need to
1622 try this when the section is not compressed. */
1623 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1624 if (retbuf != NULL)
1625 {
1626 info->buffer = retbuf;
1627 return;
1628 }
1629
1630 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1631 || bfd_bread (buf, info->size, abfd) != info->size)
1632 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1633 bfd_get_filename (abfd));
1634 }
1635
1636 /* A helper function that returns the size of a section in a safe way.
1637 If you are positive that the section has been read before using the
1638 size, then it is safe to refer to the dwarf2_section_info object's
1639 "size" field directly. In other cases, you must call this
1640 function, because for compressed sections the size field is not set
1641 correctly until the section has been read. */
1642
1643 static bfd_size_type
1644 dwarf2_section_size (struct objfile *objfile,
1645 struct dwarf2_section_info *info)
1646 {
1647 if (!info->readin)
1648 dwarf2_read_section (objfile, info);
1649 return info->size;
1650 }
1651
1652 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1653 SECTION_NAME. */
1654
1655 void
1656 dwarf2_get_section_info (struct objfile *objfile,
1657 enum dwarf2_section_enum sect,
1658 asection **sectp, gdb_byte **bufp,
1659 bfd_size_type *sizep)
1660 {
1661 struct dwarf2_per_objfile *data
1662 = objfile_data (objfile, dwarf2_objfile_data_key);
1663 struct dwarf2_section_info *info;
1664
1665 /* We may see an objfile without any DWARF, in which case we just
1666 return nothing. */
1667 if (data == NULL)
1668 {
1669 *sectp = NULL;
1670 *bufp = NULL;
1671 *sizep = 0;
1672 return;
1673 }
1674 switch (sect)
1675 {
1676 case DWARF2_DEBUG_FRAME:
1677 info = &data->frame;
1678 break;
1679 case DWARF2_EH_FRAME:
1680 info = &data->eh_frame;
1681 break;
1682 default:
1683 gdb_assert_not_reached ("unexpected section");
1684 }
1685
1686 dwarf2_read_section (objfile, info);
1687
1688 *sectp = info->asection;
1689 *bufp = info->buffer;
1690 *sizep = info->size;
1691 }
1692
1693 \f
1694 /* DWARF quick_symbols_functions support. */
1695
1696 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1697 unique line tables, so we maintain a separate table of all .debug_line
1698 derived entries to support the sharing.
1699 All the quick functions need is the list of file names. We discard the
1700 line_header when we're done and don't need to record it here. */
1701 struct quick_file_names
1702 {
1703 /* The offset in .debug_line of the line table. We hash on this. */
1704 unsigned int offset;
1705
1706 /* The number of entries in file_names, real_names. */
1707 unsigned int num_file_names;
1708
1709 /* The file names from the line table, after being run through
1710 file_full_name. */
1711 const char **file_names;
1712
1713 /* The file names from the line table after being run through
1714 gdb_realpath. These are computed lazily. */
1715 const char **real_names;
1716 };
1717
1718 /* When using the index (and thus not using psymtabs), each CU has an
1719 object of this type. This is used to hold information needed by
1720 the various "quick" methods. */
1721 struct dwarf2_per_cu_quick_data
1722 {
1723 /* The file table. This can be NULL if there was no file table
1724 or it's currently not read in.
1725 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1726 struct quick_file_names *file_names;
1727
1728 /* The corresponding symbol table. This is NULL if symbols for this
1729 CU have not yet been read. */
1730 struct symtab *symtab;
1731
1732 /* A temporary mark bit used when iterating over all CUs in
1733 expand_symtabs_matching. */
1734 unsigned int mark : 1;
1735
1736 /* True if we've tried to read the file table and found there isn't one.
1737 There will be no point in trying to read it again next time. */
1738 unsigned int no_file_data : 1;
1739 };
1740
1741 /* Hash function for a quick_file_names. */
1742
1743 static hashval_t
1744 hash_file_name_entry (const void *e)
1745 {
1746 const struct quick_file_names *file_data = e;
1747
1748 return file_data->offset;
1749 }
1750
1751 /* Equality function for a quick_file_names. */
1752
1753 static int
1754 eq_file_name_entry (const void *a, const void *b)
1755 {
1756 const struct quick_file_names *ea = a;
1757 const struct quick_file_names *eb = b;
1758
1759 return ea->offset == eb->offset;
1760 }
1761
1762 /* Delete function for a quick_file_names. */
1763
1764 static void
1765 delete_file_name_entry (void *e)
1766 {
1767 struct quick_file_names *file_data = e;
1768 int i;
1769
1770 for (i = 0; i < file_data->num_file_names; ++i)
1771 {
1772 xfree ((void*) file_data->file_names[i]);
1773 if (file_data->real_names)
1774 xfree ((void*) file_data->real_names[i]);
1775 }
1776
1777 /* The space for the struct itself lives on objfile_obstack,
1778 so we don't free it here. */
1779 }
1780
1781 /* Create a quick_file_names hash table. */
1782
1783 static htab_t
1784 create_quick_file_names_table (unsigned int nr_initial_entries)
1785 {
1786 return htab_create_alloc (nr_initial_entries,
1787 hash_file_name_entry, eq_file_name_entry,
1788 delete_file_name_entry, xcalloc, xfree);
1789 }
1790
1791 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1792 this CU came. */
1793
1794 static void
1795 dw2_do_instantiate_symtab (struct objfile *objfile,
1796 struct dwarf2_per_cu_data *per_cu)
1797 {
1798 struct cleanup *back_to;
1799
1800 back_to = make_cleanup (dwarf2_release_queue, NULL);
1801
1802 queue_comp_unit (per_cu, objfile);
1803
1804 if (per_cu->from_debug_types)
1805 read_signatured_type_at_offset (objfile, per_cu->offset);
1806 else
1807 load_full_comp_unit (per_cu, objfile);
1808
1809 process_queue (objfile);
1810
1811 /* Age the cache, releasing compilation units that have not
1812 been used recently. */
1813 age_cached_comp_units ();
1814
1815 do_cleanups (back_to);
1816 }
1817
1818 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1819 the objfile from which this CU came. Returns the resulting symbol
1820 table. */
1821
1822 static struct symtab *
1823 dw2_instantiate_symtab (struct objfile *objfile,
1824 struct dwarf2_per_cu_data *per_cu)
1825 {
1826 if (!per_cu->v.quick->symtab)
1827 {
1828 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1829 increment_reading_symtab ();
1830 dw2_do_instantiate_symtab (objfile, per_cu);
1831 do_cleanups (back_to);
1832 }
1833 return per_cu->v.quick->symtab;
1834 }
1835
1836 /* Return the CU given its index. */
1837
1838 static struct dwarf2_per_cu_data *
1839 dw2_get_cu (int index)
1840 {
1841 if (index >= dwarf2_per_objfile->n_comp_units)
1842 {
1843 index -= dwarf2_per_objfile->n_comp_units;
1844 return dwarf2_per_objfile->type_comp_units[index];
1845 }
1846 return dwarf2_per_objfile->all_comp_units[index];
1847 }
1848
1849 /* A helper function that knows how to read a 64-bit value in a way
1850 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1851 otherwise. */
1852
1853 static int
1854 extract_cu_value (const char *bytes, ULONGEST *result)
1855 {
1856 if (sizeof (ULONGEST) < 8)
1857 {
1858 int i;
1859
1860 /* Ignore the upper 4 bytes if they are all zero. */
1861 for (i = 0; i < 4; ++i)
1862 if (bytes[i + 4] != 0)
1863 return 0;
1864
1865 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1866 }
1867 else
1868 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1869 return 1;
1870 }
1871
1872 /* Read the CU list from the mapped index, and use it to create all
1873 the CU objects for this objfile. Return 0 if something went wrong,
1874 1 if everything went ok. */
1875
1876 static int
1877 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1878 offset_type cu_list_elements)
1879 {
1880 offset_type i;
1881
1882 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1883 dwarf2_per_objfile->all_comp_units
1884 = obstack_alloc (&objfile->objfile_obstack,
1885 dwarf2_per_objfile->n_comp_units
1886 * sizeof (struct dwarf2_per_cu_data *));
1887
1888 for (i = 0; i < cu_list_elements; i += 2)
1889 {
1890 struct dwarf2_per_cu_data *the_cu;
1891 ULONGEST offset, length;
1892
1893 if (!extract_cu_value (cu_list, &offset)
1894 || !extract_cu_value (cu_list + 8, &length))
1895 return 0;
1896 cu_list += 2 * 8;
1897
1898 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1899 struct dwarf2_per_cu_data);
1900 the_cu->offset = offset;
1901 the_cu->length = length;
1902 the_cu->objfile = objfile;
1903 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1904 struct dwarf2_per_cu_quick_data);
1905 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1906 }
1907
1908 return 1;
1909 }
1910
1911 /* Create the signatured type hash table from the index. */
1912
1913 static int
1914 create_signatured_type_table_from_index (struct objfile *objfile,
1915 const gdb_byte *bytes,
1916 offset_type elements)
1917 {
1918 offset_type i;
1919 htab_t sig_types_hash;
1920
1921 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1922 dwarf2_per_objfile->type_comp_units
1923 = obstack_alloc (&objfile->objfile_obstack,
1924 dwarf2_per_objfile->n_type_comp_units
1925 * sizeof (struct dwarf2_per_cu_data *));
1926
1927 sig_types_hash = allocate_signatured_type_table (objfile);
1928
1929 for (i = 0; i < elements; i += 3)
1930 {
1931 struct signatured_type *type_sig;
1932 ULONGEST offset, type_offset, signature;
1933 void **slot;
1934
1935 if (!extract_cu_value (bytes, &offset)
1936 || !extract_cu_value (bytes + 8, &type_offset))
1937 return 0;
1938 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1939 bytes += 3 * 8;
1940
1941 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1942 struct signatured_type);
1943 type_sig->signature = signature;
1944 type_sig->type_offset = type_offset;
1945 type_sig->per_cu.from_debug_types = 1;
1946 type_sig->per_cu.offset = offset;
1947 type_sig->per_cu.objfile = objfile;
1948 type_sig->per_cu.v.quick
1949 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1950 struct dwarf2_per_cu_quick_data);
1951
1952 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1953 *slot = type_sig;
1954
1955 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1956 }
1957
1958 dwarf2_per_objfile->signatured_types = sig_types_hash;
1959
1960 return 1;
1961 }
1962
1963 /* Read the address map data from the mapped index, and use it to
1964 populate the objfile's psymtabs_addrmap. */
1965
1966 static void
1967 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1968 {
1969 const gdb_byte *iter, *end;
1970 struct obstack temp_obstack;
1971 struct addrmap *mutable_map;
1972 struct cleanup *cleanup;
1973 CORE_ADDR baseaddr;
1974
1975 obstack_init (&temp_obstack);
1976 cleanup = make_cleanup_obstack_free (&temp_obstack);
1977 mutable_map = addrmap_create_mutable (&temp_obstack);
1978
1979 iter = index->address_table;
1980 end = iter + index->address_table_size;
1981
1982 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1983
1984 while (iter < end)
1985 {
1986 ULONGEST hi, lo, cu_index;
1987 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1988 iter += 8;
1989 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1990 iter += 8;
1991 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1992 iter += 4;
1993
1994 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1995 dw2_get_cu (cu_index));
1996 }
1997
1998 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1999 &objfile->objfile_obstack);
2000 do_cleanups (cleanup);
2001 }
2002
2003 /* The hash function for strings in the mapped index. This is the same as
2004 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2005 implementation. This is necessary because the hash function is tied to the
2006 format of the mapped index file. The hash values do not have to match with
2007 SYMBOL_HASH_NEXT.
2008
2009 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2010
2011 static hashval_t
2012 mapped_index_string_hash (int index_version, const void *p)
2013 {
2014 const unsigned char *str = (const unsigned char *) p;
2015 hashval_t r = 0;
2016 unsigned char c;
2017
2018 while ((c = *str++) != 0)
2019 {
2020 if (index_version >= 5)
2021 c = tolower (c);
2022 r = r * 67 + c - 113;
2023 }
2024
2025 return r;
2026 }
2027
2028 /* Find a slot in the mapped index INDEX for the object named NAME.
2029 If NAME is found, set *VEC_OUT to point to the CU vector in the
2030 constant pool and return 1. If NAME cannot be found, return 0. */
2031
2032 static int
2033 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2034 offset_type **vec_out)
2035 {
2036 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2037 offset_type hash;
2038 offset_type slot, step;
2039 int (*cmp) (const char *, const char *);
2040
2041 if (current_language->la_language == language_cplus
2042 || current_language->la_language == language_java
2043 || current_language->la_language == language_fortran)
2044 {
2045 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2046 not contain any. */
2047 const char *paren = strchr (name, '(');
2048
2049 if (paren)
2050 {
2051 char *dup;
2052
2053 dup = xmalloc (paren - name + 1);
2054 memcpy (dup, name, paren - name);
2055 dup[paren - name] = 0;
2056
2057 make_cleanup (xfree, dup);
2058 name = dup;
2059 }
2060 }
2061
2062 /* Index version 4 did not support case insensitive searches. But the
2063 indexes for case insensitive languages are built in lowercase, therefore
2064 simulate our NAME being searched is also lowercased. */
2065 hash = mapped_index_string_hash ((index->version == 4
2066 && case_sensitivity == case_sensitive_off
2067 ? 5 : index->version),
2068 name);
2069
2070 slot = hash & (index->symbol_table_slots - 1);
2071 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2072 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2073
2074 for (;;)
2075 {
2076 /* Convert a slot number to an offset into the table. */
2077 offset_type i = 2 * slot;
2078 const char *str;
2079 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2080 {
2081 do_cleanups (back_to);
2082 return 0;
2083 }
2084
2085 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2086 if (!cmp (name, str))
2087 {
2088 *vec_out = (offset_type *) (index->constant_pool
2089 + MAYBE_SWAP (index->symbol_table[i + 1]));
2090 do_cleanups (back_to);
2091 return 1;
2092 }
2093
2094 slot = (slot + step) & (index->symbol_table_slots - 1);
2095 }
2096 }
2097
2098 /* Read the index file. If everything went ok, initialize the "quick"
2099 elements of all the CUs and return 1. Otherwise, return 0. */
2100
2101 static int
2102 dwarf2_read_index (struct objfile *objfile)
2103 {
2104 char *addr;
2105 struct mapped_index *map;
2106 offset_type *metadata;
2107 const gdb_byte *cu_list;
2108 const gdb_byte *types_list = NULL;
2109 offset_type version, cu_list_elements;
2110 offset_type types_list_elements = 0;
2111 int i;
2112
2113 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2114 return 0;
2115
2116 /* Older elfutils strip versions could keep the section in the main
2117 executable while splitting it for the separate debug info file. */
2118 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2119 & SEC_HAS_CONTENTS) == 0)
2120 return 0;
2121
2122 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2123
2124 addr = dwarf2_per_objfile->gdb_index.buffer;
2125 /* Version check. */
2126 version = MAYBE_SWAP (*(offset_type *) addr);
2127 /* Versions earlier than 3 emitted every copy of a psymbol. This
2128 causes the index to behave very poorly for certain requests. Version 3
2129 contained incomplete addrmap. So, it seems better to just ignore such
2130 indices. Index version 4 uses a different hash function than index
2131 version 5 and later. */
2132 if (version < 4)
2133 return 0;
2134 /* Indexes with higher version than the one supported by GDB may be no
2135 longer backward compatible. */
2136 if (version > 5)
2137 return 0;
2138
2139 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2140 map->version = version;
2141 map->total_size = dwarf2_per_objfile->gdb_index.size;
2142
2143 metadata = (offset_type *) (addr + sizeof (offset_type));
2144
2145 i = 0;
2146 cu_list = addr + MAYBE_SWAP (metadata[i]);
2147 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2148 / 8);
2149 ++i;
2150
2151 types_list = addr + MAYBE_SWAP (metadata[i]);
2152 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2153 - MAYBE_SWAP (metadata[i]))
2154 / 8);
2155 ++i;
2156
2157 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2158 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2159 - MAYBE_SWAP (metadata[i]));
2160 ++i;
2161
2162 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2163 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2164 - MAYBE_SWAP (metadata[i]))
2165 / (2 * sizeof (offset_type)));
2166 ++i;
2167
2168 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2169
2170 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2171 return 0;
2172
2173 if (types_list_elements
2174 && !create_signatured_type_table_from_index (objfile, types_list,
2175 types_list_elements))
2176 return 0;
2177
2178 create_addrmap_from_index (objfile, map);
2179
2180 dwarf2_per_objfile->index_table = map;
2181 dwarf2_per_objfile->using_index = 1;
2182 dwarf2_per_objfile->quick_file_names_table =
2183 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2184
2185 return 1;
2186 }
2187
2188 /* A helper for the "quick" functions which sets the global
2189 dwarf2_per_objfile according to OBJFILE. */
2190
2191 static void
2192 dw2_setup (struct objfile *objfile)
2193 {
2194 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2195 gdb_assert (dwarf2_per_objfile);
2196 }
2197
2198 /* A helper for the "quick" functions which attempts to read the line
2199 table for THIS_CU. */
2200
2201 static struct quick_file_names *
2202 dw2_get_file_names (struct objfile *objfile,
2203 struct dwarf2_per_cu_data *this_cu)
2204 {
2205 bfd *abfd = objfile->obfd;
2206 struct line_header *lh;
2207 struct attribute *attr;
2208 struct cleanup *cleanups;
2209 struct die_info *comp_unit_die;
2210 struct dwarf2_section_info* sec;
2211 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2212 int has_children, i;
2213 struct dwarf2_cu cu;
2214 unsigned int bytes_read, buffer_size;
2215 struct die_reader_specs reader_specs;
2216 char *name, *comp_dir;
2217 void **slot;
2218 struct quick_file_names *qfn;
2219 unsigned int line_offset;
2220
2221 if (this_cu->v.quick->file_names != NULL)
2222 return this_cu->v.quick->file_names;
2223 /* If we know there is no line data, no point in looking again. */
2224 if (this_cu->v.quick->no_file_data)
2225 return NULL;
2226
2227 init_one_comp_unit (&cu, objfile);
2228 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2229
2230 if (this_cu->from_debug_types)
2231 sec = &dwarf2_per_objfile->types;
2232 else
2233 sec = &dwarf2_per_objfile->info;
2234 dwarf2_read_section (objfile, sec);
2235 buffer_size = sec->size;
2236 buffer = sec->buffer;
2237 info_ptr = buffer + this_cu->offset;
2238 beg_of_comp_unit = info_ptr;
2239
2240 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2241 buffer, buffer_size,
2242 abfd);
2243
2244 /* Complete the cu_header. */
2245 cu.header.offset = beg_of_comp_unit - buffer;
2246 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2247
2248 this_cu->cu = &cu;
2249 cu.per_cu = this_cu;
2250
2251 dwarf2_read_abbrevs (abfd, &cu);
2252 make_cleanup (dwarf2_free_abbrev_table, &cu);
2253
2254 if (this_cu->from_debug_types)
2255 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2256 init_cu_die_reader (&reader_specs, &cu);
2257 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2258 &has_children);
2259
2260 lh = NULL;
2261 slot = NULL;
2262 line_offset = 0;
2263 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2264 if (attr)
2265 {
2266 struct quick_file_names find_entry;
2267
2268 line_offset = DW_UNSND (attr);
2269
2270 /* We may have already read in this line header (TU line header sharing).
2271 If we have we're done. */
2272 find_entry.offset = line_offset;
2273 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2274 &find_entry, INSERT);
2275 if (*slot != NULL)
2276 {
2277 do_cleanups (cleanups);
2278 this_cu->v.quick->file_names = *slot;
2279 return *slot;
2280 }
2281
2282 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2283 }
2284 if (lh == NULL)
2285 {
2286 do_cleanups (cleanups);
2287 this_cu->v.quick->no_file_data = 1;
2288 return NULL;
2289 }
2290
2291 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2292 qfn->offset = line_offset;
2293 gdb_assert (slot != NULL);
2294 *slot = qfn;
2295
2296 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2297
2298 qfn->num_file_names = lh->num_file_names;
2299 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2300 lh->num_file_names * sizeof (char *));
2301 for (i = 0; i < lh->num_file_names; ++i)
2302 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2303 qfn->real_names = NULL;
2304
2305 free_line_header (lh);
2306 do_cleanups (cleanups);
2307
2308 this_cu->v.quick->file_names = qfn;
2309 return qfn;
2310 }
2311
2312 /* A helper for the "quick" functions which computes and caches the
2313 real path for a given file name from the line table. */
2314
2315 static const char *
2316 dw2_get_real_path (struct objfile *objfile,
2317 struct quick_file_names *qfn, int index)
2318 {
2319 if (qfn->real_names == NULL)
2320 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2321 qfn->num_file_names, sizeof (char *));
2322
2323 if (qfn->real_names[index] == NULL)
2324 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2325
2326 return qfn->real_names[index];
2327 }
2328
2329 static struct symtab *
2330 dw2_find_last_source_symtab (struct objfile *objfile)
2331 {
2332 int index;
2333
2334 dw2_setup (objfile);
2335 index = dwarf2_per_objfile->n_comp_units - 1;
2336 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2337 }
2338
2339 /* Traversal function for dw2_forget_cached_source_info. */
2340
2341 static int
2342 dw2_free_cached_file_names (void **slot, void *info)
2343 {
2344 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2345
2346 if (file_data->real_names)
2347 {
2348 int i;
2349
2350 for (i = 0; i < file_data->num_file_names; ++i)
2351 {
2352 xfree ((void*) file_data->real_names[i]);
2353 file_data->real_names[i] = NULL;
2354 }
2355 }
2356
2357 return 1;
2358 }
2359
2360 static void
2361 dw2_forget_cached_source_info (struct objfile *objfile)
2362 {
2363 dw2_setup (objfile);
2364
2365 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2366 dw2_free_cached_file_names, NULL);
2367 }
2368
2369 static int
2370 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2371 const char *full_path, const char *real_path,
2372 struct symtab **result)
2373 {
2374 int i;
2375 int check_basename = lbasename (name) == name;
2376 struct dwarf2_per_cu_data *base_cu = NULL;
2377
2378 dw2_setup (objfile);
2379
2380 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2381 + dwarf2_per_objfile->n_type_comp_units); ++i)
2382 {
2383 int j;
2384 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2385 struct quick_file_names *file_data;
2386
2387 if (per_cu->v.quick->symtab)
2388 continue;
2389
2390 file_data = dw2_get_file_names (objfile, per_cu);
2391 if (file_data == NULL)
2392 continue;
2393
2394 for (j = 0; j < file_data->num_file_names; ++j)
2395 {
2396 const char *this_name = file_data->file_names[j];
2397
2398 if (FILENAME_CMP (name, this_name) == 0)
2399 {
2400 *result = dw2_instantiate_symtab (objfile, per_cu);
2401 return 1;
2402 }
2403
2404 if (check_basename && ! base_cu
2405 && FILENAME_CMP (lbasename (this_name), name) == 0)
2406 base_cu = per_cu;
2407
2408 if (full_path != NULL)
2409 {
2410 const char *this_real_name = dw2_get_real_path (objfile,
2411 file_data, j);
2412
2413 if (this_real_name != NULL
2414 && FILENAME_CMP (full_path, this_real_name) == 0)
2415 {
2416 *result = dw2_instantiate_symtab (objfile, per_cu);
2417 return 1;
2418 }
2419 }
2420
2421 if (real_path != NULL)
2422 {
2423 const char *this_real_name = dw2_get_real_path (objfile,
2424 file_data, j);
2425
2426 if (this_real_name != NULL
2427 && FILENAME_CMP (real_path, this_real_name) == 0)
2428 {
2429 *result = dw2_instantiate_symtab (objfile, per_cu);
2430 return 1;
2431 }
2432 }
2433 }
2434 }
2435
2436 if (base_cu)
2437 {
2438 *result = dw2_instantiate_symtab (objfile, base_cu);
2439 return 1;
2440 }
2441
2442 return 0;
2443 }
2444
2445 static struct symtab *
2446 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2447 const char *name, domain_enum domain)
2448 {
2449 /* We do all the work in the pre_expand_symtabs_matching hook
2450 instead. */
2451 return NULL;
2452 }
2453
2454 /* A helper function that expands all symtabs that hold an object
2455 named NAME. */
2456
2457 static void
2458 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2459 {
2460 dw2_setup (objfile);
2461
2462 /* index_table is NULL if OBJF_READNOW. */
2463 if (dwarf2_per_objfile->index_table)
2464 {
2465 offset_type *vec;
2466
2467 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2468 name, &vec))
2469 {
2470 offset_type i, len = MAYBE_SWAP (*vec);
2471 for (i = 0; i < len; ++i)
2472 {
2473 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2474 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2475
2476 dw2_instantiate_symtab (objfile, per_cu);
2477 }
2478 }
2479 }
2480 }
2481
2482 static void
2483 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2484 enum block_enum block_kind, const char *name,
2485 domain_enum domain)
2486 {
2487 dw2_do_expand_symtabs_matching (objfile, name);
2488 }
2489
2490 static void
2491 dw2_print_stats (struct objfile *objfile)
2492 {
2493 int i, count;
2494
2495 dw2_setup (objfile);
2496 count = 0;
2497 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2498 + dwarf2_per_objfile->n_type_comp_units); ++i)
2499 {
2500 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2501
2502 if (!per_cu->v.quick->symtab)
2503 ++count;
2504 }
2505 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2506 }
2507
2508 static void
2509 dw2_dump (struct objfile *objfile)
2510 {
2511 /* Nothing worth printing. */
2512 }
2513
2514 static void
2515 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2516 struct section_offsets *delta)
2517 {
2518 /* There's nothing to relocate here. */
2519 }
2520
2521 static void
2522 dw2_expand_symtabs_for_function (struct objfile *objfile,
2523 const char *func_name)
2524 {
2525 dw2_do_expand_symtabs_matching (objfile, func_name);
2526 }
2527
2528 static void
2529 dw2_expand_all_symtabs (struct objfile *objfile)
2530 {
2531 int i;
2532
2533 dw2_setup (objfile);
2534
2535 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2536 + dwarf2_per_objfile->n_type_comp_units); ++i)
2537 {
2538 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2539
2540 dw2_instantiate_symtab (objfile, per_cu);
2541 }
2542 }
2543
2544 static void
2545 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2546 const char *filename)
2547 {
2548 int i;
2549
2550 dw2_setup (objfile);
2551
2552 /* We don't need to consider type units here.
2553 This is only called for examining code, e.g. expand_line_sal.
2554 There can be an order of magnitude (or more) more type units
2555 than comp units, and we avoid them if we can. */
2556
2557 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2558 {
2559 int j;
2560 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2561 struct quick_file_names *file_data;
2562
2563 if (per_cu->v.quick->symtab)
2564 continue;
2565
2566 file_data = dw2_get_file_names (objfile, per_cu);
2567 if (file_data == NULL)
2568 continue;
2569
2570 for (j = 0; j < file_data->num_file_names; ++j)
2571 {
2572 const char *this_name = file_data->file_names[j];
2573 if (FILENAME_CMP (this_name, filename) == 0)
2574 {
2575 dw2_instantiate_symtab (objfile, per_cu);
2576 break;
2577 }
2578 }
2579 }
2580 }
2581
2582 static const char *
2583 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2584 {
2585 struct dwarf2_per_cu_data *per_cu;
2586 offset_type *vec;
2587 struct quick_file_names *file_data;
2588
2589 dw2_setup (objfile);
2590
2591 /* index_table is NULL if OBJF_READNOW. */
2592 if (!dwarf2_per_objfile->index_table)
2593 return NULL;
2594
2595 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2596 name, &vec))
2597 return NULL;
2598
2599 /* Note that this just looks at the very first one named NAME -- but
2600 actually we are looking for a function. find_main_filename
2601 should be rewritten so that it doesn't require a custom hook. It
2602 could just use the ordinary symbol tables. */
2603 /* vec[0] is the length, which must always be >0. */
2604 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2605
2606 file_data = dw2_get_file_names (objfile, per_cu);
2607 if (file_data == NULL)
2608 return NULL;
2609
2610 return file_data->file_names[file_data->num_file_names - 1];
2611 }
2612
2613 static void
2614 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2615 struct objfile *objfile, int global,
2616 int (*callback) (struct block *,
2617 struct symbol *, void *),
2618 void *data, symbol_compare_ftype *match,
2619 symbol_compare_ftype *ordered_compare)
2620 {
2621 /* Currently unimplemented; used for Ada. The function can be called if the
2622 current language is Ada for a non-Ada objfile using GNU index. As Ada
2623 does not look for non-Ada symbols this function should just return. */
2624 }
2625
2626 static void
2627 dw2_expand_symtabs_matching (struct objfile *objfile,
2628 int (*file_matcher) (const char *, void *),
2629 int (*name_matcher) (const char *, void *),
2630 enum search_domain kind,
2631 void *data)
2632 {
2633 int i;
2634 offset_type iter;
2635 struct mapped_index *index;
2636
2637 dw2_setup (objfile);
2638
2639 /* index_table is NULL if OBJF_READNOW. */
2640 if (!dwarf2_per_objfile->index_table)
2641 return;
2642 index = dwarf2_per_objfile->index_table;
2643
2644 if (file_matcher != NULL)
2645 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2646 + dwarf2_per_objfile->n_type_comp_units); ++i)
2647 {
2648 int j;
2649 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2650 struct quick_file_names *file_data;
2651
2652 per_cu->v.quick->mark = 0;
2653 if (per_cu->v.quick->symtab)
2654 continue;
2655
2656 file_data = dw2_get_file_names (objfile, per_cu);
2657 if (file_data == NULL)
2658 continue;
2659
2660 for (j = 0; j < file_data->num_file_names; ++j)
2661 {
2662 if (file_matcher (file_data->file_names[j], data))
2663 {
2664 per_cu->v.quick->mark = 1;
2665 break;
2666 }
2667 }
2668 }
2669
2670 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2671 {
2672 offset_type idx = 2 * iter;
2673 const char *name;
2674 offset_type *vec, vec_len, vec_idx;
2675
2676 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2677 continue;
2678
2679 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2680
2681 if (! (*name_matcher) (name, data))
2682 continue;
2683
2684 /* The name was matched, now expand corresponding CUs that were
2685 marked. */
2686 vec = (offset_type *) (index->constant_pool
2687 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2688 vec_len = MAYBE_SWAP (vec[0]);
2689 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2690 {
2691 struct dwarf2_per_cu_data *per_cu;
2692
2693 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2694 if (file_matcher == NULL || per_cu->v.quick->mark)
2695 dw2_instantiate_symtab (objfile, per_cu);
2696 }
2697 }
2698 }
2699
2700 static struct symtab *
2701 dw2_find_pc_sect_symtab (struct objfile *objfile,
2702 struct minimal_symbol *msymbol,
2703 CORE_ADDR pc,
2704 struct obj_section *section,
2705 int warn_if_readin)
2706 {
2707 struct dwarf2_per_cu_data *data;
2708
2709 dw2_setup (objfile);
2710
2711 if (!objfile->psymtabs_addrmap)
2712 return NULL;
2713
2714 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2715 if (!data)
2716 return NULL;
2717
2718 if (warn_if_readin && data->v.quick->symtab)
2719 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2720 paddress (get_objfile_arch (objfile), pc));
2721
2722 return dw2_instantiate_symtab (objfile, data);
2723 }
2724
2725 static void
2726 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2727 void *data)
2728 {
2729 int i;
2730
2731 dw2_setup (objfile);
2732
2733 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2734 + dwarf2_per_objfile->n_type_comp_units); ++i)
2735 {
2736 int j;
2737 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2738 struct quick_file_names *file_data;
2739
2740 if (per_cu->v.quick->symtab)
2741 continue;
2742
2743 file_data = dw2_get_file_names (objfile, per_cu);
2744 if (file_data == NULL)
2745 continue;
2746
2747 for (j = 0; j < file_data->num_file_names; ++j)
2748 {
2749 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2750 j);
2751 (*fun) (file_data->file_names[j], this_real_name, data);
2752 }
2753 }
2754 }
2755
2756 static int
2757 dw2_has_symbols (struct objfile *objfile)
2758 {
2759 return 1;
2760 }
2761
2762 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2763 {
2764 dw2_has_symbols,
2765 dw2_find_last_source_symtab,
2766 dw2_forget_cached_source_info,
2767 dw2_lookup_symtab,
2768 dw2_lookup_symbol,
2769 dw2_pre_expand_symtabs_matching,
2770 dw2_print_stats,
2771 dw2_dump,
2772 dw2_relocate,
2773 dw2_expand_symtabs_for_function,
2774 dw2_expand_all_symtabs,
2775 dw2_expand_symtabs_with_filename,
2776 dw2_find_symbol_file,
2777 dw2_map_matching_symbols,
2778 dw2_expand_symtabs_matching,
2779 dw2_find_pc_sect_symtab,
2780 dw2_map_symbol_filenames
2781 };
2782
2783 /* Initialize for reading DWARF for this objfile. Return 0 if this
2784 file will use psymtabs, or 1 if using the GNU index. */
2785
2786 int
2787 dwarf2_initialize_objfile (struct objfile *objfile)
2788 {
2789 /* If we're about to read full symbols, don't bother with the
2790 indices. In this case we also don't care if some other debug
2791 format is making psymtabs, because they are all about to be
2792 expanded anyway. */
2793 if ((objfile->flags & OBJF_READNOW))
2794 {
2795 int i;
2796
2797 dwarf2_per_objfile->using_index = 1;
2798 create_all_comp_units (objfile);
2799 create_debug_types_hash_table (objfile);
2800 dwarf2_per_objfile->quick_file_names_table =
2801 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2802
2803 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2804 + dwarf2_per_objfile->n_type_comp_units); ++i)
2805 {
2806 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2807
2808 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2809 struct dwarf2_per_cu_quick_data);
2810 }
2811
2812 /* Return 1 so that gdb sees the "quick" functions. However,
2813 these functions will be no-ops because we will have expanded
2814 all symtabs. */
2815 return 1;
2816 }
2817
2818 if (dwarf2_read_index (objfile))
2819 return 1;
2820
2821 return 0;
2822 }
2823
2824 \f
2825
2826 /* Build a partial symbol table. */
2827
2828 void
2829 dwarf2_build_psymtabs (struct objfile *objfile)
2830 {
2831 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2832 {
2833 init_psymbol_list (objfile, 1024);
2834 }
2835
2836 dwarf2_build_psymtabs_hard (objfile);
2837 }
2838
2839 /* Return TRUE if OFFSET is within CU_HEADER. */
2840
2841 static inline int
2842 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2843 {
2844 unsigned int bottom = cu_header->offset;
2845 unsigned int top = (cu_header->offset
2846 + cu_header->length
2847 + cu_header->initial_length_size);
2848
2849 return (offset >= bottom && offset < top);
2850 }
2851
2852 /* Read in the comp unit header information from the debug_info at info_ptr.
2853 NOTE: This leaves members offset, first_die_offset to be filled in
2854 by the caller. */
2855
2856 static gdb_byte *
2857 read_comp_unit_head (struct comp_unit_head *cu_header,
2858 gdb_byte *info_ptr, bfd *abfd)
2859 {
2860 int signed_addr;
2861 unsigned int bytes_read;
2862
2863 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2864 cu_header->initial_length_size = bytes_read;
2865 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2866 info_ptr += bytes_read;
2867 cu_header->version = read_2_bytes (abfd, info_ptr);
2868 info_ptr += 2;
2869 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2870 &bytes_read);
2871 info_ptr += bytes_read;
2872 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2873 info_ptr += 1;
2874 signed_addr = bfd_get_sign_extend_vma (abfd);
2875 if (signed_addr < 0)
2876 internal_error (__FILE__, __LINE__,
2877 _("read_comp_unit_head: dwarf from non elf file"));
2878 cu_header->signed_addr_p = signed_addr;
2879
2880 return info_ptr;
2881 }
2882
2883 static gdb_byte *
2884 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2885 gdb_byte *buffer, unsigned int buffer_size,
2886 bfd *abfd)
2887 {
2888 gdb_byte *beg_of_comp_unit = info_ptr;
2889
2890 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2891
2892 if (header->version != 2 && header->version != 3 && header->version != 4)
2893 error (_("Dwarf Error: wrong version in compilation unit header "
2894 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2895 bfd_get_filename (abfd));
2896
2897 if (header->abbrev_offset
2898 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2899 &dwarf2_per_objfile->abbrev))
2900 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2901 "(offset 0x%lx + 6) [in module %s]"),
2902 (long) header->abbrev_offset,
2903 (long) (beg_of_comp_unit - buffer),
2904 bfd_get_filename (abfd));
2905
2906 if (beg_of_comp_unit + header->length + header->initial_length_size
2907 > buffer + buffer_size)
2908 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2909 "(offset 0x%lx + 0) [in module %s]"),
2910 (long) header->length,
2911 (long) (beg_of_comp_unit - buffer),
2912 bfd_get_filename (abfd));
2913
2914 return info_ptr;
2915 }
2916
2917 /* Read in the types comp unit header information from .debug_types entry at
2918 types_ptr. The result is a pointer to one past the end of the header. */
2919
2920 static gdb_byte *
2921 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2922 ULONGEST *signature,
2923 gdb_byte *types_ptr, bfd *abfd)
2924 {
2925 gdb_byte *initial_types_ptr = types_ptr;
2926
2927 dwarf2_read_section (dwarf2_per_objfile->objfile,
2928 &dwarf2_per_objfile->types);
2929 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2930
2931 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2932
2933 *signature = read_8_bytes (abfd, types_ptr);
2934 types_ptr += 8;
2935 types_ptr += cu_header->offset_size;
2936 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2937
2938 return types_ptr;
2939 }
2940
2941 /* Allocate a new partial symtab for file named NAME and mark this new
2942 partial symtab as being an include of PST. */
2943
2944 static void
2945 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2946 struct objfile *objfile)
2947 {
2948 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2949
2950 subpst->section_offsets = pst->section_offsets;
2951 subpst->textlow = 0;
2952 subpst->texthigh = 0;
2953
2954 subpst->dependencies = (struct partial_symtab **)
2955 obstack_alloc (&objfile->objfile_obstack,
2956 sizeof (struct partial_symtab *));
2957 subpst->dependencies[0] = pst;
2958 subpst->number_of_dependencies = 1;
2959
2960 subpst->globals_offset = 0;
2961 subpst->n_global_syms = 0;
2962 subpst->statics_offset = 0;
2963 subpst->n_static_syms = 0;
2964 subpst->symtab = NULL;
2965 subpst->read_symtab = pst->read_symtab;
2966 subpst->readin = 0;
2967
2968 /* No private part is necessary for include psymtabs. This property
2969 can be used to differentiate between such include psymtabs and
2970 the regular ones. */
2971 subpst->read_symtab_private = NULL;
2972 }
2973
2974 /* Read the Line Number Program data and extract the list of files
2975 included by the source file represented by PST. Build an include
2976 partial symtab for each of these included files. */
2977
2978 static void
2979 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2980 struct die_info *die,
2981 struct partial_symtab *pst)
2982 {
2983 struct objfile *objfile = cu->objfile;
2984 bfd *abfd = objfile->obfd;
2985 struct line_header *lh = NULL;
2986 struct attribute *attr;
2987
2988 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2989 if (attr)
2990 {
2991 unsigned int line_offset = DW_UNSND (attr);
2992
2993 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2994 }
2995 if (lh == NULL)
2996 return; /* No linetable, so no includes. */
2997
2998 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2999 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
3000
3001 free_line_header (lh);
3002 }
3003
3004 static hashval_t
3005 hash_type_signature (const void *item)
3006 {
3007 const struct signatured_type *type_sig = item;
3008
3009 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3010 return type_sig->signature;
3011 }
3012
3013 static int
3014 eq_type_signature (const void *item_lhs, const void *item_rhs)
3015 {
3016 const struct signatured_type *lhs = item_lhs;
3017 const struct signatured_type *rhs = item_rhs;
3018
3019 return lhs->signature == rhs->signature;
3020 }
3021
3022 /* Allocate a hash table for signatured types. */
3023
3024 static htab_t
3025 allocate_signatured_type_table (struct objfile *objfile)
3026 {
3027 return htab_create_alloc_ex (41,
3028 hash_type_signature,
3029 eq_type_signature,
3030 NULL,
3031 &objfile->objfile_obstack,
3032 hashtab_obstack_allocate,
3033 dummy_obstack_deallocate);
3034 }
3035
3036 /* A helper function to add a signatured type CU to a list. */
3037
3038 static int
3039 add_signatured_type_cu_to_list (void **slot, void *datum)
3040 {
3041 struct signatured_type *sigt = *slot;
3042 struct dwarf2_per_cu_data ***datap = datum;
3043
3044 **datap = &sigt->per_cu;
3045 ++*datap;
3046
3047 return 1;
3048 }
3049
3050 /* Create the hash table of all entries in the .debug_types section.
3051 The result is zero if there is an error (e.g. missing .debug_types section),
3052 otherwise non-zero. */
3053
3054 static int
3055 create_debug_types_hash_table (struct objfile *objfile)
3056 {
3057 gdb_byte *info_ptr;
3058 htab_t types_htab;
3059 struct dwarf2_per_cu_data **iter;
3060
3061 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3062 info_ptr = dwarf2_per_objfile->types.buffer;
3063
3064 if (info_ptr == NULL)
3065 {
3066 dwarf2_per_objfile->signatured_types = NULL;
3067 return 0;
3068 }
3069
3070 types_htab = allocate_signatured_type_table (objfile);
3071
3072 if (dwarf2_die_debug)
3073 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3074
3075 while (info_ptr < dwarf2_per_objfile->types.buffer
3076 + dwarf2_per_objfile->types.size)
3077 {
3078 unsigned int offset;
3079 unsigned int offset_size;
3080 unsigned int type_offset;
3081 unsigned int length, initial_length_size;
3082 unsigned short version;
3083 ULONGEST signature;
3084 struct signatured_type *type_sig;
3085 void **slot;
3086 gdb_byte *ptr = info_ptr;
3087
3088 offset = ptr - dwarf2_per_objfile->types.buffer;
3089
3090 /* We need to read the type's signature in order to build the hash
3091 table, but we don't need to read anything else just yet. */
3092
3093 /* Sanity check to ensure entire cu is present. */
3094 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3095 if (ptr + length + initial_length_size
3096 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3097 {
3098 complaint (&symfile_complaints,
3099 _("debug type entry runs off end "
3100 "of `.debug_types' section, ignored"));
3101 break;
3102 }
3103
3104 offset_size = initial_length_size == 4 ? 4 : 8;
3105 ptr += initial_length_size;
3106 version = bfd_get_16 (objfile->obfd, ptr);
3107 ptr += 2;
3108 ptr += offset_size; /* abbrev offset */
3109 ptr += 1; /* address size */
3110 signature = bfd_get_64 (objfile->obfd, ptr);
3111 ptr += 8;
3112 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3113
3114 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3115 memset (type_sig, 0, sizeof (*type_sig));
3116 type_sig->signature = signature;
3117 type_sig->type_offset = type_offset;
3118 type_sig->per_cu.objfile = objfile;
3119 type_sig->per_cu.from_debug_types = 1;
3120 type_sig->per_cu.offset = offset;
3121
3122 slot = htab_find_slot (types_htab, type_sig, INSERT);
3123 gdb_assert (slot != NULL);
3124 if (*slot != NULL)
3125 {
3126 const struct signatured_type *dup_sig = *slot;
3127
3128 complaint (&symfile_complaints,
3129 _("debug type entry at offset 0x%x is duplicate to the "
3130 "entry at offset 0x%x, signature 0x%s"),
3131 offset, dup_sig->per_cu.offset,
3132 phex (signature, sizeof (signature)));
3133 gdb_assert (signature == dup_sig->signature);
3134 }
3135 *slot = type_sig;
3136
3137 if (dwarf2_die_debug)
3138 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3139 offset, phex (signature, sizeof (signature)));
3140
3141 info_ptr = info_ptr + initial_length_size + length;
3142 }
3143
3144 dwarf2_per_objfile->signatured_types = types_htab;
3145
3146 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3147 dwarf2_per_objfile->type_comp_units
3148 = obstack_alloc (&objfile->objfile_obstack,
3149 dwarf2_per_objfile->n_type_comp_units
3150 * sizeof (struct dwarf2_per_cu_data *));
3151 iter = &dwarf2_per_objfile->type_comp_units[0];
3152 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3153 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3154 == dwarf2_per_objfile->n_type_comp_units);
3155
3156 return 1;
3157 }
3158
3159 /* Lookup a signature based type.
3160 Returns NULL if SIG is not present in the table. */
3161
3162 static struct signatured_type *
3163 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3164 {
3165 struct signatured_type find_entry, *entry;
3166
3167 if (dwarf2_per_objfile->signatured_types == NULL)
3168 {
3169 complaint (&symfile_complaints,
3170 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3171 return 0;
3172 }
3173
3174 find_entry.signature = sig;
3175 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3176 return entry;
3177 }
3178
3179 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3180
3181 static void
3182 init_cu_die_reader (struct die_reader_specs *reader,
3183 struct dwarf2_cu *cu)
3184 {
3185 reader->abfd = cu->objfile->obfd;
3186 reader->cu = cu;
3187 if (cu->per_cu->from_debug_types)
3188 {
3189 gdb_assert (dwarf2_per_objfile->types.readin);
3190 reader->buffer = dwarf2_per_objfile->types.buffer;
3191 }
3192 else
3193 {
3194 gdb_assert (dwarf2_per_objfile->info.readin);
3195 reader->buffer = dwarf2_per_objfile->info.buffer;
3196 }
3197 }
3198
3199 /* Find the base address of the compilation unit for range lists and
3200 location lists. It will normally be specified by DW_AT_low_pc.
3201 In DWARF-3 draft 4, the base address could be overridden by
3202 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3203 compilation units with discontinuous ranges. */
3204
3205 static void
3206 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3207 {
3208 struct attribute *attr;
3209
3210 cu->base_known = 0;
3211 cu->base_address = 0;
3212
3213 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3214 if (attr)
3215 {
3216 cu->base_address = DW_ADDR (attr);
3217 cu->base_known = 1;
3218 }
3219 else
3220 {
3221 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3222 if (attr)
3223 {
3224 cu->base_address = DW_ADDR (attr);
3225 cu->base_known = 1;
3226 }
3227 }
3228 }
3229
3230 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3231 to combine the common parts.
3232 Process a compilation unit for a psymtab.
3233 BUFFER is a pointer to the beginning of the dwarf section buffer,
3234 either .debug_info or debug_types.
3235 INFO_PTR is a pointer to the start of the CU.
3236 Returns a pointer to the next CU. */
3237
3238 static gdb_byte *
3239 process_psymtab_comp_unit (struct objfile *objfile,
3240 struct dwarf2_per_cu_data *this_cu,
3241 gdb_byte *buffer, gdb_byte *info_ptr,
3242 unsigned int buffer_size)
3243 {
3244 bfd *abfd = objfile->obfd;
3245 gdb_byte *beg_of_comp_unit = info_ptr;
3246 struct die_info *comp_unit_die;
3247 struct partial_symtab *pst;
3248 CORE_ADDR baseaddr;
3249 struct cleanup *back_to_inner;
3250 struct dwarf2_cu cu;
3251 int has_children, has_pc_info;
3252 struct attribute *attr;
3253 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3254 struct die_reader_specs reader_specs;
3255 const char *filename;
3256
3257 init_one_comp_unit (&cu, objfile);
3258 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3259
3260 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3261 buffer, buffer_size,
3262 abfd);
3263
3264 /* Complete the cu_header. */
3265 cu.header.offset = beg_of_comp_unit - buffer;
3266 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3267
3268 cu.list_in_scope = &file_symbols;
3269
3270 /* If this compilation unit was already read in, free the
3271 cached copy in order to read it in again. This is
3272 necessary because we skipped some symbols when we first
3273 read in the compilation unit (see load_partial_dies).
3274 This problem could be avoided, but the benefit is
3275 unclear. */
3276 if (this_cu->cu != NULL)
3277 free_one_cached_comp_unit (this_cu->cu);
3278
3279 /* Note that this is a pointer to our stack frame, being
3280 added to a global data structure. It will be cleaned up
3281 in free_stack_comp_unit when we finish with this
3282 compilation unit. */
3283 this_cu->cu = &cu;
3284 cu.per_cu = this_cu;
3285
3286 /* Read the abbrevs for this compilation unit into a table. */
3287 dwarf2_read_abbrevs (abfd, &cu);
3288 make_cleanup (dwarf2_free_abbrev_table, &cu);
3289
3290 /* Read the compilation unit die. */
3291 if (this_cu->from_debug_types)
3292 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3293 init_cu_die_reader (&reader_specs, &cu);
3294 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3295 &has_children);
3296
3297 if (this_cu->from_debug_types)
3298 {
3299 /* LENGTH has not been set yet for type units. */
3300 gdb_assert (this_cu->offset == cu.header.offset);
3301 this_cu->length = cu.header.length + cu.header.initial_length_size;
3302 }
3303 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3304 {
3305 info_ptr = (beg_of_comp_unit + cu.header.length
3306 + cu.header.initial_length_size);
3307 do_cleanups (back_to_inner);
3308 return info_ptr;
3309 }
3310
3311 prepare_one_comp_unit (&cu, comp_unit_die);
3312
3313 /* Allocate a new partial symbol table structure. */
3314 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3315 if (attr == NULL || !DW_STRING (attr))
3316 filename = "";
3317 else
3318 filename = DW_STRING (attr);
3319 pst = start_psymtab_common (objfile, objfile->section_offsets,
3320 filename,
3321 /* TEXTLOW and TEXTHIGH are set below. */
3322 0,
3323 objfile->global_psymbols.next,
3324 objfile->static_psymbols.next);
3325
3326 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3327 if (attr != NULL)
3328 pst->dirname = DW_STRING (attr);
3329
3330 pst->read_symtab_private = this_cu;
3331
3332 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3333
3334 /* Store the function that reads in the rest of the symbol table. */
3335 pst->read_symtab = dwarf2_psymtab_to_symtab;
3336
3337 this_cu->v.psymtab = pst;
3338
3339 dwarf2_find_base_address (comp_unit_die, &cu);
3340
3341 /* Possibly set the default values of LOWPC and HIGHPC from
3342 `DW_AT_ranges'. */
3343 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3344 &best_highpc, &cu, pst);
3345 if (has_pc_info == 1 && best_lowpc < best_highpc)
3346 /* Store the contiguous range if it is not empty; it can be empty for
3347 CUs with no code. */
3348 addrmap_set_empty (objfile->psymtabs_addrmap,
3349 best_lowpc + baseaddr,
3350 best_highpc + baseaddr - 1, pst);
3351
3352 /* Check if comp unit has_children.
3353 If so, read the rest of the partial symbols from this comp unit.
3354 If not, there's no more debug_info for this comp unit. */
3355 if (has_children)
3356 {
3357 struct partial_die_info *first_die;
3358 CORE_ADDR lowpc, highpc;
3359
3360 lowpc = ((CORE_ADDR) -1);
3361 highpc = ((CORE_ADDR) 0);
3362
3363 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3364
3365 scan_partial_symbols (first_die, &lowpc, &highpc,
3366 ! has_pc_info, &cu);
3367
3368 /* If we didn't find a lowpc, set it to highpc to avoid
3369 complaints from `maint check'. */
3370 if (lowpc == ((CORE_ADDR) -1))
3371 lowpc = highpc;
3372
3373 /* If the compilation unit didn't have an explicit address range,
3374 then use the information extracted from its child dies. */
3375 if (! has_pc_info)
3376 {
3377 best_lowpc = lowpc;
3378 best_highpc = highpc;
3379 }
3380 }
3381 pst->textlow = best_lowpc + baseaddr;
3382 pst->texthigh = best_highpc + baseaddr;
3383
3384 pst->n_global_syms = objfile->global_psymbols.next -
3385 (objfile->global_psymbols.list + pst->globals_offset);
3386 pst->n_static_syms = objfile->static_psymbols.next -
3387 (objfile->static_psymbols.list + pst->statics_offset);
3388 sort_pst_symbols (pst);
3389
3390 info_ptr = (beg_of_comp_unit + cu.header.length
3391 + cu.header.initial_length_size);
3392
3393 if (this_cu->from_debug_types)
3394 {
3395 /* It's not clear we want to do anything with stmt lists here.
3396 Waiting to see what gcc ultimately does. */
3397 }
3398 else
3399 {
3400 /* Get the list of files included in the current compilation unit,
3401 and build a psymtab for each of them. */
3402 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3403 }
3404
3405 do_cleanups (back_to_inner);
3406
3407 return info_ptr;
3408 }
3409
3410 /* Traversal function for htab_traverse_noresize.
3411 Process one .debug_types comp-unit. */
3412
3413 static int
3414 process_type_comp_unit (void **slot, void *info)
3415 {
3416 struct signatured_type *entry = (struct signatured_type *) *slot;
3417 struct objfile *objfile = (struct objfile *) info;
3418 struct dwarf2_per_cu_data *this_cu;
3419
3420 this_cu = &entry->per_cu;
3421
3422 gdb_assert (dwarf2_per_objfile->types.readin);
3423 process_psymtab_comp_unit (objfile, this_cu,
3424 dwarf2_per_objfile->types.buffer,
3425 dwarf2_per_objfile->types.buffer + this_cu->offset,
3426 dwarf2_per_objfile->types.size);
3427
3428 return 1;
3429 }
3430
3431 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3432 Build partial symbol tables for the .debug_types comp-units. */
3433
3434 static void
3435 build_type_psymtabs (struct objfile *objfile)
3436 {
3437 if (! create_debug_types_hash_table (objfile))
3438 return;
3439
3440 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3441 process_type_comp_unit, objfile);
3442 }
3443
3444 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3445
3446 static void
3447 psymtabs_addrmap_cleanup (void *o)
3448 {
3449 struct objfile *objfile = o;
3450
3451 objfile->psymtabs_addrmap = NULL;
3452 }
3453
3454 /* Build the partial symbol table by doing a quick pass through the
3455 .debug_info and .debug_abbrev sections. */
3456
3457 static void
3458 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3459 {
3460 gdb_byte *info_ptr;
3461 struct cleanup *back_to, *addrmap_cleanup;
3462 struct obstack temp_obstack;
3463
3464 dwarf2_per_objfile->reading_partial_symbols = 1;
3465
3466 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3467 info_ptr = dwarf2_per_objfile->info.buffer;
3468
3469 /* Any cached compilation units will be linked by the per-objfile
3470 read_in_chain. Make sure to free them when we're done. */
3471 back_to = make_cleanup (free_cached_comp_units, NULL);
3472
3473 build_type_psymtabs (objfile);
3474
3475 create_all_comp_units (objfile);
3476
3477 /* Create a temporary address map on a temporary obstack. We later
3478 copy this to the final obstack. */
3479 obstack_init (&temp_obstack);
3480 make_cleanup_obstack_free (&temp_obstack);
3481 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3482 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3483
3484 /* Since the objects we're extracting from .debug_info vary in
3485 length, only the individual functions to extract them (like
3486 read_comp_unit_head and load_partial_die) can really know whether
3487 the buffer is large enough to hold another complete object.
3488
3489 At the moment, they don't actually check that. If .debug_info
3490 holds just one extra byte after the last compilation unit's dies,
3491 then read_comp_unit_head will happily read off the end of the
3492 buffer. read_partial_die is similarly casual. Those functions
3493 should be fixed.
3494
3495 For this loop condition, simply checking whether there's any data
3496 left at all should be sufficient. */
3497
3498 while (info_ptr < (dwarf2_per_objfile->info.buffer
3499 + dwarf2_per_objfile->info.size))
3500 {
3501 struct dwarf2_per_cu_data *this_cu;
3502
3503 this_cu = dwarf2_find_comp_unit (info_ptr
3504 - dwarf2_per_objfile->info.buffer,
3505 objfile);
3506
3507 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3508 dwarf2_per_objfile->info.buffer,
3509 info_ptr,
3510 dwarf2_per_objfile->info.size);
3511 }
3512
3513 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3514 &objfile->objfile_obstack);
3515 discard_cleanups (addrmap_cleanup);
3516
3517 do_cleanups (back_to);
3518 }
3519
3520 /* Load the partial DIEs for a secondary CU into memory. */
3521
3522 static void
3523 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3524 struct objfile *objfile)
3525 {
3526 bfd *abfd = objfile->obfd;
3527 gdb_byte *info_ptr, *beg_of_comp_unit;
3528 struct die_info *comp_unit_die;
3529 struct dwarf2_cu *cu;
3530 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3531 int has_children;
3532 struct die_reader_specs reader_specs;
3533 int read_cu = 0;
3534
3535 gdb_assert (! this_cu->from_debug_types);
3536
3537 gdb_assert (dwarf2_per_objfile->info.readin);
3538 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3539 beg_of_comp_unit = info_ptr;
3540
3541 if (this_cu->cu == NULL)
3542 {
3543 cu = xmalloc (sizeof (*cu));
3544 init_one_comp_unit (cu, objfile);
3545
3546 read_cu = 1;
3547
3548 /* If an error occurs while loading, release our storage. */
3549 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3550
3551 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3552 dwarf2_per_objfile->info.buffer,
3553 dwarf2_per_objfile->info.size,
3554 abfd);
3555
3556 /* Complete the cu_header. */
3557 cu->header.offset = this_cu->offset;
3558 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3559
3560 /* Link this compilation unit into the compilation unit tree. */
3561 this_cu->cu = cu;
3562 cu->per_cu = this_cu;
3563
3564 /* Link this CU into read_in_chain. */
3565 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3566 dwarf2_per_objfile->read_in_chain = this_cu;
3567 }
3568 else
3569 {
3570 cu = this_cu->cu;
3571 info_ptr += cu->header.first_die_offset;
3572 }
3573
3574 /* Read the abbrevs for this compilation unit into a table. */
3575 gdb_assert (cu->dwarf2_abbrevs == NULL);
3576 dwarf2_read_abbrevs (abfd, cu);
3577 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3578
3579 /* Read the compilation unit die. */
3580 init_cu_die_reader (&reader_specs, cu);
3581 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3582 &has_children);
3583
3584 prepare_one_comp_unit (cu, comp_unit_die);
3585
3586 /* Check if comp unit has_children.
3587 If so, read the rest of the partial symbols from this comp unit.
3588 If not, there's no more debug_info for this comp unit. */
3589 if (has_children)
3590 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3591
3592 do_cleanups (free_abbrevs_cleanup);
3593
3594 if (read_cu)
3595 {
3596 /* We've successfully allocated this compilation unit. Let our
3597 caller clean it up when finished with it. */
3598 discard_cleanups (free_cu_cleanup);
3599 }
3600 }
3601
3602 /* Create a list of all compilation units in OBJFILE. We do this only
3603 if an inter-comp-unit reference is found; presumably if there is one,
3604 there will be many, and one will occur early in the .debug_info section.
3605 So there's no point in building this list incrementally. */
3606
3607 static void
3608 create_all_comp_units (struct objfile *objfile)
3609 {
3610 int n_allocated;
3611 int n_comp_units;
3612 struct dwarf2_per_cu_data **all_comp_units;
3613 gdb_byte *info_ptr;
3614
3615 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3616 info_ptr = dwarf2_per_objfile->info.buffer;
3617
3618 n_comp_units = 0;
3619 n_allocated = 10;
3620 all_comp_units = xmalloc (n_allocated
3621 * sizeof (struct dwarf2_per_cu_data *));
3622
3623 while (info_ptr < dwarf2_per_objfile->info.buffer
3624 + dwarf2_per_objfile->info.size)
3625 {
3626 unsigned int length, initial_length_size;
3627 struct dwarf2_per_cu_data *this_cu;
3628 unsigned int offset;
3629
3630 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3631
3632 /* Read just enough information to find out where the next
3633 compilation unit is. */
3634 length = read_initial_length (objfile->obfd, info_ptr,
3635 &initial_length_size);
3636
3637 /* Save the compilation unit for later lookup. */
3638 this_cu = obstack_alloc (&objfile->objfile_obstack,
3639 sizeof (struct dwarf2_per_cu_data));
3640 memset (this_cu, 0, sizeof (*this_cu));
3641 this_cu->offset = offset;
3642 this_cu->length = length + initial_length_size;
3643 this_cu->objfile = objfile;
3644
3645 if (n_comp_units == n_allocated)
3646 {
3647 n_allocated *= 2;
3648 all_comp_units = xrealloc (all_comp_units,
3649 n_allocated
3650 * sizeof (struct dwarf2_per_cu_data *));
3651 }
3652 all_comp_units[n_comp_units++] = this_cu;
3653
3654 info_ptr = info_ptr + this_cu->length;
3655 }
3656
3657 dwarf2_per_objfile->all_comp_units
3658 = obstack_alloc (&objfile->objfile_obstack,
3659 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3660 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3661 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3662 xfree (all_comp_units);
3663 dwarf2_per_objfile->n_comp_units = n_comp_units;
3664 }
3665
3666 /* Process all loaded DIEs for compilation unit CU, starting at
3667 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3668 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3669 DW_AT_ranges). If NEED_PC is set, then this function will set
3670 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3671 and record the covered ranges in the addrmap. */
3672
3673 static void
3674 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3675 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3676 {
3677 struct partial_die_info *pdi;
3678
3679 /* Now, march along the PDI's, descending into ones which have
3680 interesting children but skipping the children of the other ones,
3681 until we reach the end of the compilation unit. */
3682
3683 pdi = first_die;
3684
3685 while (pdi != NULL)
3686 {
3687 fixup_partial_die (pdi, cu);
3688
3689 /* Anonymous namespaces or modules have no name but have interesting
3690 children, so we need to look at them. Ditto for anonymous
3691 enums. */
3692
3693 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3694 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3695 {
3696 switch (pdi->tag)
3697 {
3698 case DW_TAG_subprogram:
3699 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3700 break;
3701 case DW_TAG_constant:
3702 case DW_TAG_variable:
3703 case DW_TAG_typedef:
3704 case DW_TAG_union_type:
3705 if (!pdi->is_declaration)
3706 {
3707 add_partial_symbol (pdi, cu);
3708 }
3709 break;
3710 case DW_TAG_class_type:
3711 case DW_TAG_interface_type:
3712 case DW_TAG_structure_type:
3713 if (!pdi->is_declaration)
3714 {
3715 add_partial_symbol (pdi, cu);
3716 }
3717 break;
3718 case DW_TAG_enumeration_type:
3719 if (!pdi->is_declaration)
3720 add_partial_enumeration (pdi, cu);
3721 break;
3722 case DW_TAG_base_type:
3723 case DW_TAG_subrange_type:
3724 /* File scope base type definitions are added to the partial
3725 symbol table. */
3726 add_partial_symbol (pdi, cu);
3727 break;
3728 case DW_TAG_namespace:
3729 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3730 break;
3731 case DW_TAG_module:
3732 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3733 break;
3734 default:
3735 break;
3736 }
3737 }
3738
3739 /* If the die has a sibling, skip to the sibling. */
3740
3741 pdi = pdi->die_sibling;
3742 }
3743 }
3744
3745 /* Functions used to compute the fully scoped name of a partial DIE.
3746
3747 Normally, this is simple. For C++, the parent DIE's fully scoped
3748 name is concatenated with "::" and the partial DIE's name. For
3749 Java, the same thing occurs except that "." is used instead of "::".
3750 Enumerators are an exception; they use the scope of their parent
3751 enumeration type, i.e. the name of the enumeration type is not
3752 prepended to the enumerator.
3753
3754 There are two complexities. One is DW_AT_specification; in this
3755 case "parent" means the parent of the target of the specification,
3756 instead of the direct parent of the DIE. The other is compilers
3757 which do not emit DW_TAG_namespace; in this case we try to guess
3758 the fully qualified name of structure types from their members'
3759 linkage names. This must be done using the DIE's children rather
3760 than the children of any DW_AT_specification target. We only need
3761 to do this for structures at the top level, i.e. if the target of
3762 any DW_AT_specification (if any; otherwise the DIE itself) does not
3763 have a parent. */
3764
3765 /* Compute the scope prefix associated with PDI's parent, in
3766 compilation unit CU. The result will be allocated on CU's
3767 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3768 field. NULL is returned if no prefix is necessary. */
3769 static char *
3770 partial_die_parent_scope (struct partial_die_info *pdi,
3771 struct dwarf2_cu *cu)
3772 {
3773 char *grandparent_scope;
3774 struct partial_die_info *parent, *real_pdi;
3775
3776 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3777 then this means the parent of the specification DIE. */
3778
3779 real_pdi = pdi;
3780 while (real_pdi->has_specification)
3781 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3782
3783 parent = real_pdi->die_parent;
3784 if (parent == NULL)
3785 return NULL;
3786
3787 if (parent->scope_set)
3788 return parent->scope;
3789
3790 fixup_partial_die (parent, cu);
3791
3792 grandparent_scope = partial_die_parent_scope (parent, cu);
3793
3794 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3795 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3796 Work around this problem here. */
3797 if (cu->language == language_cplus
3798 && parent->tag == DW_TAG_namespace
3799 && strcmp (parent->name, "::") == 0
3800 && grandparent_scope == NULL)
3801 {
3802 parent->scope = NULL;
3803 parent->scope_set = 1;
3804 return NULL;
3805 }
3806
3807 if (parent->tag == DW_TAG_namespace
3808 || parent->tag == DW_TAG_module
3809 || parent->tag == DW_TAG_structure_type
3810 || parent->tag == DW_TAG_class_type
3811 || parent->tag == DW_TAG_interface_type
3812 || parent->tag == DW_TAG_union_type
3813 || parent->tag == DW_TAG_enumeration_type)
3814 {
3815 if (grandparent_scope == NULL)
3816 parent->scope = parent->name;
3817 else
3818 parent->scope = typename_concat (&cu->comp_unit_obstack,
3819 grandparent_scope,
3820 parent->name, 0, cu);
3821 }
3822 else if (parent->tag == DW_TAG_enumerator)
3823 /* Enumerators should not get the name of the enumeration as a prefix. */
3824 parent->scope = grandparent_scope;
3825 else
3826 {
3827 /* FIXME drow/2004-04-01: What should we be doing with
3828 function-local names? For partial symbols, we should probably be
3829 ignoring them. */
3830 complaint (&symfile_complaints,
3831 _("unhandled containing DIE tag %d for DIE at %d"),
3832 parent->tag, pdi->offset);
3833 parent->scope = grandparent_scope;
3834 }
3835
3836 parent->scope_set = 1;
3837 return parent->scope;
3838 }
3839
3840 /* Return the fully scoped name associated with PDI, from compilation unit
3841 CU. The result will be allocated with malloc. */
3842 static char *
3843 partial_die_full_name (struct partial_die_info *pdi,
3844 struct dwarf2_cu *cu)
3845 {
3846 char *parent_scope;
3847
3848 /* If this is a template instantiation, we can not work out the
3849 template arguments from partial DIEs. So, unfortunately, we have
3850 to go through the full DIEs. At least any work we do building
3851 types here will be reused if full symbols are loaded later. */
3852 if (pdi->has_template_arguments)
3853 {
3854 fixup_partial_die (pdi, cu);
3855
3856 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3857 {
3858 struct die_info *die;
3859 struct attribute attr;
3860 struct dwarf2_cu *ref_cu = cu;
3861
3862 attr.name = 0;
3863 attr.form = DW_FORM_ref_addr;
3864 attr.u.addr = pdi->offset;
3865 die = follow_die_ref (NULL, &attr, &ref_cu);
3866
3867 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3868 }
3869 }
3870
3871 parent_scope = partial_die_parent_scope (pdi, cu);
3872 if (parent_scope == NULL)
3873 return NULL;
3874 else
3875 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3876 }
3877
3878 static void
3879 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3880 {
3881 struct objfile *objfile = cu->objfile;
3882 CORE_ADDR addr = 0;
3883 char *actual_name = NULL;
3884 const struct partial_symbol *psym = NULL;
3885 CORE_ADDR baseaddr;
3886 int built_actual_name = 0;
3887
3888 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3889
3890 actual_name = partial_die_full_name (pdi, cu);
3891 if (actual_name)
3892 built_actual_name = 1;
3893
3894 if (actual_name == NULL)
3895 actual_name = pdi->name;
3896
3897 switch (pdi->tag)
3898 {
3899 case DW_TAG_subprogram:
3900 if (pdi->is_external || cu->language == language_ada)
3901 {
3902 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3903 of the global scope. But in Ada, we want to be able to access
3904 nested procedures globally. So all Ada subprograms are stored
3905 in the global scope. */
3906 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3907 mst_text, objfile); */
3908 add_psymbol_to_list (actual_name, strlen (actual_name),
3909 built_actual_name,
3910 VAR_DOMAIN, LOC_BLOCK,
3911 &objfile->global_psymbols,
3912 0, pdi->lowpc + baseaddr,
3913 cu->language, objfile);
3914 }
3915 else
3916 {
3917 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3918 mst_file_text, objfile); */
3919 add_psymbol_to_list (actual_name, strlen (actual_name),
3920 built_actual_name,
3921 VAR_DOMAIN, LOC_BLOCK,
3922 &objfile->static_psymbols,
3923 0, pdi->lowpc + baseaddr,
3924 cu->language, objfile);
3925 }
3926 break;
3927 case DW_TAG_constant:
3928 {
3929 struct psymbol_allocation_list *list;
3930
3931 if (pdi->is_external)
3932 list = &objfile->global_psymbols;
3933 else
3934 list = &objfile->static_psymbols;
3935 add_psymbol_to_list (actual_name, strlen (actual_name),
3936 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3937 list, 0, 0, cu->language, objfile);
3938 }
3939 break;
3940 case DW_TAG_variable:
3941 if (pdi->locdesc)
3942 addr = decode_locdesc (pdi->locdesc, cu);
3943
3944 if (pdi->locdesc
3945 && addr == 0
3946 && !dwarf2_per_objfile->has_section_at_zero)
3947 {
3948 /* A global or static variable may also have been stripped
3949 out by the linker if unused, in which case its address
3950 will be nullified; do not add such variables into partial
3951 symbol table then. */
3952 }
3953 else if (pdi->is_external)
3954 {
3955 /* Global Variable.
3956 Don't enter into the minimal symbol tables as there is
3957 a minimal symbol table entry from the ELF symbols already.
3958 Enter into partial symbol table if it has a location
3959 descriptor or a type.
3960 If the location descriptor is missing, new_symbol will create
3961 a LOC_UNRESOLVED symbol, the address of the variable will then
3962 be determined from the minimal symbol table whenever the variable
3963 is referenced.
3964 The address for the partial symbol table entry is not
3965 used by GDB, but it comes in handy for debugging partial symbol
3966 table building. */
3967
3968 if (pdi->locdesc || pdi->has_type)
3969 add_psymbol_to_list (actual_name, strlen (actual_name),
3970 built_actual_name,
3971 VAR_DOMAIN, LOC_STATIC,
3972 &objfile->global_psymbols,
3973 0, addr + baseaddr,
3974 cu->language, objfile);
3975 }
3976 else
3977 {
3978 /* Static Variable. Skip symbols without location descriptors. */
3979 if (pdi->locdesc == NULL)
3980 {
3981 if (built_actual_name)
3982 xfree (actual_name);
3983 return;
3984 }
3985 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3986 mst_file_data, objfile); */
3987 add_psymbol_to_list (actual_name, strlen (actual_name),
3988 built_actual_name,
3989 VAR_DOMAIN, LOC_STATIC,
3990 &objfile->static_psymbols,
3991 0, addr + baseaddr,
3992 cu->language, objfile);
3993 }
3994 break;
3995 case DW_TAG_typedef:
3996 case DW_TAG_base_type:
3997 case DW_TAG_subrange_type:
3998 add_psymbol_to_list (actual_name, strlen (actual_name),
3999 built_actual_name,
4000 VAR_DOMAIN, LOC_TYPEDEF,
4001 &objfile->static_psymbols,
4002 0, (CORE_ADDR) 0, cu->language, objfile);
4003 break;
4004 case DW_TAG_namespace:
4005 add_psymbol_to_list (actual_name, strlen (actual_name),
4006 built_actual_name,
4007 VAR_DOMAIN, LOC_TYPEDEF,
4008 &objfile->global_psymbols,
4009 0, (CORE_ADDR) 0, cu->language, objfile);
4010 break;
4011 case DW_TAG_class_type:
4012 case DW_TAG_interface_type:
4013 case DW_TAG_structure_type:
4014 case DW_TAG_union_type:
4015 case DW_TAG_enumeration_type:
4016 /* Skip external references. The DWARF standard says in the section
4017 about "Structure, Union, and Class Type Entries": "An incomplete
4018 structure, union or class type is represented by a structure,
4019 union or class entry that does not have a byte size attribute
4020 and that has a DW_AT_declaration attribute." */
4021 if (!pdi->has_byte_size && pdi->is_declaration)
4022 {
4023 if (built_actual_name)
4024 xfree (actual_name);
4025 return;
4026 }
4027
4028 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4029 static vs. global. */
4030 add_psymbol_to_list (actual_name, strlen (actual_name),
4031 built_actual_name,
4032 STRUCT_DOMAIN, LOC_TYPEDEF,
4033 (cu->language == language_cplus
4034 || cu->language == language_java)
4035 ? &objfile->global_psymbols
4036 : &objfile->static_psymbols,
4037 0, (CORE_ADDR) 0, cu->language, objfile);
4038
4039 break;
4040 case DW_TAG_enumerator:
4041 add_psymbol_to_list (actual_name, strlen (actual_name),
4042 built_actual_name,
4043 VAR_DOMAIN, LOC_CONST,
4044 (cu->language == language_cplus
4045 || cu->language == language_java)
4046 ? &objfile->global_psymbols
4047 : &objfile->static_psymbols,
4048 0, (CORE_ADDR) 0, cu->language, objfile);
4049 break;
4050 default:
4051 break;
4052 }
4053
4054 if (built_actual_name)
4055 xfree (actual_name);
4056 }
4057
4058 /* Read a partial die corresponding to a namespace; also, add a symbol
4059 corresponding to that namespace to the symbol table. NAMESPACE is
4060 the name of the enclosing namespace. */
4061
4062 static void
4063 add_partial_namespace (struct partial_die_info *pdi,
4064 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4065 int need_pc, struct dwarf2_cu *cu)
4066 {
4067 /* Add a symbol for the namespace. */
4068
4069 add_partial_symbol (pdi, cu);
4070
4071 /* Now scan partial symbols in that namespace. */
4072
4073 if (pdi->has_children)
4074 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4075 }
4076
4077 /* Read a partial die corresponding to a Fortran module. */
4078
4079 static void
4080 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4081 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4082 {
4083 /* Now scan partial symbols in that module. */
4084
4085 if (pdi->has_children)
4086 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4087 }
4088
4089 /* Read a partial die corresponding to a subprogram and create a partial
4090 symbol for that subprogram. When the CU language allows it, this
4091 routine also defines a partial symbol for each nested subprogram
4092 that this subprogram contains.
4093
4094 DIE my also be a lexical block, in which case we simply search
4095 recursively for suprograms defined inside that lexical block.
4096 Again, this is only performed when the CU language allows this
4097 type of definitions. */
4098
4099 static void
4100 add_partial_subprogram (struct partial_die_info *pdi,
4101 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4102 int need_pc, struct dwarf2_cu *cu)
4103 {
4104 if (pdi->tag == DW_TAG_subprogram)
4105 {
4106 if (pdi->has_pc_info)
4107 {
4108 if (pdi->lowpc < *lowpc)
4109 *lowpc = pdi->lowpc;
4110 if (pdi->highpc > *highpc)
4111 *highpc = pdi->highpc;
4112 if (need_pc)
4113 {
4114 CORE_ADDR baseaddr;
4115 struct objfile *objfile = cu->objfile;
4116
4117 baseaddr = ANOFFSET (objfile->section_offsets,
4118 SECT_OFF_TEXT (objfile));
4119 addrmap_set_empty (objfile->psymtabs_addrmap,
4120 pdi->lowpc + baseaddr,
4121 pdi->highpc - 1 + baseaddr,
4122 cu->per_cu->v.psymtab);
4123 }
4124 if (!pdi->is_declaration)
4125 /* Ignore subprogram DIEs that do not have a name, they are
4126 illegal. Do not emit a complaint at this point, we will
4127 do so when we convert this psymtab into a symtab. */
4128 if (pdi->name)
4129 add_partial_symbol (pdi, cu);
4130 }
4131 }
4132
4133 if (! pdi->has_children)
4134 return;
4135
4136 if (cu->language == language_ada)
4137 {
4138 pdi = pdi->die_child;
4139 while (pdi != NULL)
4140 {
4141 fixup_partial_die (pdi, cu);
4142 if (pdi->tag == DW_TAG_subprogram
4143 || pdi->tag == DW_TAG_lexical_block)
4144 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4145 pdi = pdi->die_sibling;
4146 }
4147 }
4148 }
4149
4150 /* Read a partial die corresponding to an enumeration type. */
4151
4152 static void
4153 add_partial_enumeration (struct partial_die_info *enum_pdi,
4154 struct dwarf2_cu *cu)
4155 {
4156 struct partial_die_info *pdi;
4157
4158 if (enum_pdi->name != NULL)
4159 add_partial_symbol (enum_pdi, cu);
4160
4161 pdi = enum_pdi->die_child;
4162 while (pdi)
4163 {
4164 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4165 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4166 else
4167 add_partial_symbol (pdi, cu);
4168 pdi = pdi->die_sibling;
4169 }
4170 }
4171
4172 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4173 Return the corresponding abbrev, or NULL if the number is zero (indicating
4174 an empty DIE). In either case *BYTES_READ will be set to the length of
4175 the initial number. */
4176
4177 static struct abbrev_info *
4178 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4179 struct dwarf2_cu *cu)
4180 {
4181 bfd *abfd = cu->objfile->obfd;
4182 unsigned int abbrev_number;
4183 struct abbrev_info *abbrev;
4184
4185 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4186
4187 if (abbrev_number == 0)
4188 return NULL;
4189
4190 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4191 if (!abbrev)
4192 {
4193 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4194 abbrev_number, bfd_get_filename (abfd));
4195 }
4196
4197 return abbrev;
4198 }
4199
4200 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4201 Returns a pointer to the end of a series of DIEs, terminated by an empty
4202 DIE. Any children of the skipped DIEs will also be skipped. */
4203
4204 static gdb_byte *
4205 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4206 {
4207 struct abbrev_info *abbrev;
4208 unsigned int bytes_read;
4209
4210 while (1)
4211 {
4212 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4213 if (abbrev == NULL)
4214 return info_ptr + bytes_read;
4215 else
4216 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4217 }
4218 }
4219
4220 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4221 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4222 abbrev corresponding to that skipped uleb128 should be passed in
4223 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4224 children. */
4225
4226 static gdb_byte *
4227 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4228 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4229 {
4230 unsigned int bytes_read;
4231 struct attribute attr;
4232 bfd *abfd = cu->objfile->obfd;
4233 unsigned int form, i;
4234
4235 for (i = 0; i < abbrev->num_attrs; i++)
4236 {
4237 /* The only abbrev we care about is DW_AT_sibling. */
4238 if (abbrev->attrs[i].name == DW_AT_sibling)
4239 {
4240 read_attribute (&attr, &abbrev->attrs[i],
4241 abfd, info_ptr, cu);
4242 if (attr.form == DW_FORM_ref_addr)
4243 complaint (&symfile_complaints,
4244 _("ignoring absolute DW_AT_sibling"));
4245 else
4246 return buffer + dwarf2_get_ref_die_offset (&attr);
4247 }
4248
4249 /* If it isn't DW_AT_sibling, skip this attribute. */
4250 form = abbrev->attrs[i].form;
4251 skip_attribute:
4252 switch (form)
4253 {
4254 case DW_FORM_ref_addr:
4255 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4256 and later it is offset sized. */
4257 if (cu->header.version == 2)
4258 info_ptr += cu->header.addr_size;
4259 else
4260 info_ptr += cu->header.offset_size;
4261 break;
4262 case DW_FORM_addr:
4263 info_ptr += cu->header.addr_size;
4264 break;
4265 case DW_FORM_data1:
4266 case DW_FORM_ref1:
4267 case DW_FORM_flag:
4268 info_ptr += 1;
4269 break;
4270 case DW_FORM_flag_present:
4271 break;
4272 case DW_FORM_data2:
4273 case DW_FORM_ref2:
4274 info_ptr += 2;
4275 break;
4276 case DW_FORM_data4:
4277 case DW_FORM_ref4:
4278 info_ptr += 4;
4279 break;
4280 case DW_FORM_data8:
4281 case DW_FORM_ref8:
4282 case DW_FORM_ref_sig8:
4283 info_ptr += 8;
4284 break;
4285 case DW_FORM_string:
4286 read_direct_string (abfd, info_ptr, &bytes_read);
4287 info_ptr += bytes_read;
4288 break;
4289 case DW_FORM_sec_offset:
4290 case DW_FORM_strp:
4291 info_ptr += cu->header.offset_size;
4292 break;
4293 case DW_FORM_exprloc:
4294 case DW_FORM_block:
4295 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4296 info_ptr += bytes_read;
4297 break;
4298 case DW_FORM_block1:
4299 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4300 break;
4301 case DW_FORM_block2:
4302 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4303 break;
4304 case DW_FORM_block4:
4305 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4306 break;
4307 case DW_FORM_sdata:
4308 case DW_FORM_udata:
4309 case DW_FORM_ref_udata:
4310 info_ptr = skip_leb128 (abfd, info_ptr);
4311 break;
4312 case DW_FORM_indirect:
4313 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4314 info_ptr += bytes_read;
4315 /* We need to continue parsing from here, so just go back to
4316 the top. */
4317 goto skip_attribute;
4318
4319 default:
4320 error (_("Dwarf Error: Cannot handle %s "
4321 "in DWARF reader [in module %s]"),
4322 dwarf_form_name (form),
4323 bfd_get_filename (abfd));
4324 }
4325 }
4326
4327 if (abbrev->has_children)
4328 return skip_children (buffer, info_ptr, cu);
4329 else
4330 return info_ptr;
4331 }
4332
4333 /* Locate ORIG_PDI's sibling.
4334 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4335 in BUFFER. */
4336
4337 static gdb_byte *
4338 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4339 gdb_byte *buffer, gdb_byte *info_ptr,
4340 bfd *abfd, struct dwarf2_cu *cu)
4341 {
4342 /* Do we know the sibling already? */
4343
4344 if (orig_pdi->sibling)
4345 return orig_pdi->sibling;
4346
4347 /* Are there any children to deal with? */
4348
4349 if (!orig_pdi->has_children)
4350 return info_ptr;
4351
4352 /* Skip the children the long way. */
4353
4354 return skip_children (buffer, info_ptr, cu);
4355 }
4356
4357 /* Expand this partial symbol table into a full symbol table. */
4358
4359 static void
4360 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4361 {
4362 if (pst != NULL)
4363 {
4364 if (pst->readin)
4365 {
4366 warning (_("bug: psymtab for %s is already read in."),
4367 pst->filename);
4368 }
4369 else
4370 {
4371 if (info_verbose)
4372 {
4373 printf_filtered (_("Reading in symbols for %s..."),
4374 pst->filename);
4375 gdb_flush (gdb_stdout);
4376 }
4377
4378 /* Restore our global data. */
4379 dwarf2_per_objfile = objfile_data (pst->objfile,
4380 dwarf2_objfile_data_key);
4381
4382 /* If this psymtab is constructed from a debug-only objfile, the
4383 has_section_at_zero flag will not necessarily be correct. We
4384 can get the correct value for this flag by looking at the data
4385 associated with the (presumably stripped) associated objfile. */
4386 if (pst->objfile->separate_debug_objfile_backlink)
4387 {
4388 struct dwarf2_per_objfile *dpo_backlink
4389 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4390 dwarf2_objfile_data_key);
4391
4392 dwarf2_per_objfile->has_section_at_zero
4393 = dpo_backlink->has_section_at_zero;
4394 }
4395
4396 dwarf2_per_objfile->reading_partial_symbols = 0;
4397
4398 psymtab_to_symtab_1 (pst);
4399
4400 /* Finish up the debug error message. */
4401 if (info_verbose)
4402 printf_filtered (_("done.\n"));
4403 }
4404 }
4405 }
4406
4407 /* Add PER_CU to the queue. */
4408
4409 static void
4410 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4411 {
4412 struct dwarf2_queue_item *item;
4413
4414 per_cu->queued = 1;
4415 item = xmalloc (sizeof (*item));
4416 item->per_cu = per_cu;
4417 item->next = NULL;
4418
4419 if (dwarf2_queue == NULL)
4420 dwarf2_queue = item;
4421 else
4422 dwarf2_queue_tail->next = item;
4423
4424 dwarf2_queue_tail = item;
4425 }
4426
4427 /* Process the queue. */
4428
4429 static void
4430 process_queue (struct objfile *objfile)
4431 {
4432 struct dwarf2_queue_item *item, *next_item;
4433
4434 /* The queue starts out with one item, but following a DIE reference
4435 may load a new CU, adding it to the end of the queue. */
4436 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4437 {
4438 if (dwarf2_per_objfile->using_index
4439 ? !item->per_cu->v.quick->symtab
4440 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4441 process_full_comp_unit (item->per_cu);
4442
4443 item->per_cu->queued = 0;
4444 next_item = item->next;
4445 xfree (item);
4446 }
4447
4448 dwarf2_queue_tail = NULL;
4449 }
4450
4451 /* Free all allocated queue entries. This function only releases anything if
4452 an error was thrown; if the queue was processed then it would have been
4453 freed as we went along. */
4454
4455 static void
4456 dwarf2_release_queue (void *dummy)
4457 {
4458 struct dwarf2_queue_item *item, *last;
4459
4460 item = dwarf2_queue;
4461 while (item)
4462 {
4463 /* Anything still marked queued is likely to be in an
4464 inconsistent state, so discard it. */
4465 if (item->per_cu->queued)
4466 {
4467 if (item->per_cu->cu != NULL)
4468 free_one_cached_comp_unit (item->per_cu->cu);
4469 item->per_cu->queued = 0;
4470 }
4471
4472 last = item;
4473 item = item->next;
4474 xfree (last);
4475 }
4476
4477 dwarf2_queue = dwarf2_queue_tail = NULL;
4478 }
4479
4480 /* Read in full symbols for PST, and anything it depends on. */
4481
4482 static void
4483 psymtab_to_symtab_1 (struct partial_symtab *pst)
4484 {
4485 struct dwarf2_per_cu_data *per_cu;
4486 struct cleanup *back_to;
4487 int i;
4488
4489 for (i = 0; i < pst->number_of_dependencies; i++)
4490 if (!pst->dependencies[i]->readin)
4491 {
4492 /* Inform about additional files that need to be read in. */
4493 if (info_verbose)
4494 {
4495 /* FIXME: i18n: Need to make this a single string. */
4496 fputs_filtered (" ", gdb_stdout);
4497 wrap_here ("");
4498 fputs_filtered ("and ", gdb_stdout);
4499 wrap_here ("");
4500 printf_filtered ("%s...", pst->dependencies[i]->filename);
4501 wrap_here (""); /* Flush output. */
4502 gdb_flush (gdb_stdout);
4503 }
4504 psymtab_to_symtab_1 (pst->dependencies[i]);
4505 }
4506
4507 per_cu = pst->read_symtab_private;
4508
4509 if (per_cu == NULL)
4510 {
4511 /* It's an include file, no symbols to read for it.
4512 Everything is in the parent symtab. */
4513 pst->readin = 1;
4514 return;
4515 }
4516
4517 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4518 }
4519
4520 /* Load the DIEs associated with PER_CU into memory. */
4521
4522 static void
4523 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4524 struct objfile *objfile)
4525 {
4526 bfd *abfd = objfile->obfd;
4527 struct dwarf2_cu *cu;
4528 unsigned int offset;
4529 gdb_byte *info_ptr, *beg_of_comp_unit;
4530 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4531 struct attribute *attr;
4532 int read_cu = 0;
4533
4534 gdb_assert (! per_cu->from_debug_types);
4535
4536 /* Set local variables from the partial symbol table info. */
4537 offset = per_cu->offset;
4538
4539 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4540 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4541 beg_of_comp_unit = info_ptr;
4542
4543 if (per_cu->cu == NULL)
4544 {
4545 cu = xmalloc (sizeof (*cu));
4546 init_one_comp_unit (cu, objfile);
4547
4548 read_cu = 1;
4549
4550 /* If an error occurs while loading, release our storage. */
4551 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4552
4553 /* Read in the comp_unit header. */
4554 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4555
4556 /* Complete the cu_header. */
4557 cu->header.offset = offset;
4558 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4559
4560 /* Read the abbrevs for this compilation unit. */
4561 dwarf2_read_abbrevs (abfd, cu);
4562 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4563
4564 /* Link this compilation unit into the compilation unit tree. */
4565 per_cu->cu = cu;
4566 cu->per_cu = per_cu;
4567
4568 /* Link this CU into read_in_chain. */
4569 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4570 dwarf2_per_objfile->read_in_chain = per_cu;
4571 }
4572 else
4573 {
4574 cu = per_cu->cu;
4575 info_ptr += cu->header.first_die_offset;
4576 }
4577
4578 cu->dies = read_comp_unit (info_ptr, cu);
4579
4580 /* We try not to read any attributes in this function, because not
4581 all objfiles needed for references have been loaded yet, and symbol
4582 table processing isn't initialized. But we have to set the CU language,
4583 or we won't be able to build types correctly. */
4584 prepare_one_comp_unit (cu, cu->dies);
4585
4586 /* Similarly, if we do not read the producer, we can not apply
4587 producer-specific interpretation. */
4588 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4589 if (attr)
4590 cu->producer = DW_STRING (attr);
4591
4592 if (read_cu)
4593 {
4594 do_cleanups (free_abbrevs_cleanup);
4595
4596 /* We've successfully allocated this compilation unit. Let our
4597 caller clean it up when finished with it. */
4598 discard_cleanups (free_cu_cleanup);
4599 }
4600 }
4601
4602 /* Add a DIE to the delayed physname list. */
4603
4604 static void
4605 add_to_method_list (struct type *type, int fnfield_index, int index,
4606 const char *name, struct die_info *die,
4607 struct dwarf2_cu *cu)
4608 {
4609 struct delayed_method_info mi;
4610 mi.type = type;
4611 mi.fnfield_index = fnfield_index;
4612 mi.index = index;
4613 mi.name = name;
4614 mi.die = die;
4615 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4616 }
4617
4618 /* A cleanup for freeing the delayed method list. */
4619
4620 static void
4621 free_delayed_list (void *ptr)
4622 {
4623 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4624 if (cu->method_list != NULL)
4625 {
4626 VEC_free (delayed_method_info, cu->method_list);
4627 cu->method_list = NULL;
4628 }
4629 }
4630
4631 /* Compute the physnames of any methods on the CU's method list.
4632
4633 The computation of method physnames is delayed in order to avoid the
4634 (bad) condition that one of the method's formal parameters is of an as yet
4635 incomplete type. */
4636
4637 static void
4638 compute_delayed_physnames (struct dwarf2_cu *cu)
4639 {
4640 int i;
4641 struct delayed_method_info *mi;
4642 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4643 {
4644 const char *physname;
4645 struct fn_fieldlist *fn_flp
4646 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4647 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4648 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4649 }
4650 }
4651
4652 /* Check for GCC >= 4.x. Return minor version (x) of 4.x in such case. If it
4653 is not GCC or it is GCC older than 4.x return -1. If it is GCC 5.x or
4654 higher return INT_MAX. */
4655
4656 static int
4657 producer_is_gcc_ge_4 (struct dwarf2_cu *cu)
4658 {
4659 const char *cs;
4660 int major, minor;
4661
4662 if (cu->producer == NULL)
4663 {
4664 /* For unknown compilers expect their behavior is not compliant. For GCC
4665 this case can also happen for -gdwarf-4 type units supported since
4666 gcc-4.5. */
4667
4668 return -1;
4669 }
4670
4671 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4672
4673 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4674 {
4675 /* For non-GCC compilers expect their behavior is not compliant. */
4676
4677 return -1;
4678 }
4679 cs = &cu->producer[strlen ("GNU ")];
4680 while (*cs && !isdigit (*cs))
4681 cs++;
4682 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4683 {
4684 /* Not recognized as GCC. */
4685
4686 return -1;
4687 }
4688
4689 if (major < 4)
4690 return -1;
4691 if (major > 4)
4692 return INT_MAX;
4693 return minor;
4694 }
4695
4696 /* Generate full symbol information for PST and CU, whose DIEs have
4697 already been loaded into memory. */
4698
4699 static void
4700 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4701 {
4702 struct dwarf2_cu *cu = per_cu->cu;
4703 struct objfile *objfile = per_cu->objfile;
4704 CORE_ADDR lowpc, highpc;
4705 struct symtab *symtab;
4706 struct cleanup *back_to, *delayed_list_cleanup;
4707 CORE_ADDR baseaddr;
4708
4709 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4710
4711 buildsym_init ();
4712 back_to = make_cleanup (really_free_pendings, NULL);
4713 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4714
4715 cu->list_in_scope = &file_symbols;
4716
4717 dwarf2_find_base_address (cu->dies, cu);
4718
4719 /* Do line number decoding in read_file_scope () */
4720 process_die (cu->dies, cu);
4721
4722 /* Now that we have processed all the DIEs in the CU, all the types
4723 should be complete, and it should now be safe to compute all of the
4724 physnames. */
4725 compute_delayed_physnames (cu);
4726 do_cleanups (delayed_list_cleanup);
4727
4728 /* Some compilers don't define a DW_AT_high_pc attribute for the
4729 compilation unit. If the DW_AT_high_pc is missing, synthesize
4730 it, by scanning the DIE's below the compilation unit. */
4731 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4732
4733 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4734
4735 if (symtab != NULL)
4736 {
4737 int gcc_4_minor = producer_is_gcc_ge_4 (cu);
4738
4739 /* Set symtab language to language from DW_AT_language. If the
4740 compilation is from a C file generated by language preprocessors, do
4741 not set the language if it was already deduced by start_subfile. */
4742 if (!(cu->language == language_c && symtab->language != language_c))
4743 symtab->language = cu->language;
4744
4745 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4746 produce DW_AT_location with location lists but it can be possibly
4747 invalid without -fvar-tracking.
4748
4749 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4750 needed, it would be wrong due to missing DW_AT_producer there.
4751
4752 Still one can confuse GDB by using non-standard GCC compilation
4753 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4754 */
4755 if (cu->has_loclist && gcc_4_minor >= 0)
4756 symtab->locations_valid = 1;
4757
4758 if (gcc_4_minor >= 5)
4759 symtab->epilogue_unwind_valid = 1;
4760 }
4761
4762 if (dwarf2_per_objfile->using_index)
4763 per_cu->v.quick->symtab = symtab;
4764 else
4765 {
4766 struct partial_symtab *pst = per_cu->v.psymtab;
4767 pst->symtab = symtab;
4768 pst->readin = 1;
4769 }
4770
4771 do_cleanups (back_to);
4772 }
4773
4774 /* Process a die and its children. */
4775
4776 static void
4777 process_die (struct die_info *die, struct dwarf2_cu *cu)
4778 {
4779 switch (die->tag)
4780 {
4781 case DW_TAG_padding:
4782 break;
4783 case DW_TAG_compile_unit:
4784 read_file_scope (die, cu);
4785 break;
4786 case DW_TAG_type_unit:
4787 read_type_unit_scope (die, cu);
4788 break;
4789 case DW_TAG_subprogram:
4790 case DW_TAG_inlined_subroutine:
4791 read_func_scope (die, cu);
4792 break;
4793 case DW_TAG_lexical_block:
4794 case DW_TAG_try_block:
4795 case DW_TAG_catch_block:
4796 read_lexical_block_scope (die, cu);
4797 break;
4798 case DW_TAG_class_type:
4799 case DW_TAG_interface_type:
4800 case DW_TAG_structure_type:
4801 case DW_TAG_union_type:
4802 process_structure_scope (die, cu);
4803 break;
4804 case DW_TAG_enumeration_type:
4805 process_enumeration_scope (die, cu);
4806 break;
4807
4808 /* These dies have a type, but processing them does not create
4809 a symbol or recurse to process the children. Therefore we can
4810 read them on-demand through read_type_die. */
4811 case DW_TAG_subroutine_type:
4812 case DW_TAG_set_type:
4813 case DW_TAG_array_type:
4814 case DW_TAG_pointer_type:
4815 case DW_TAG_ptr_to_member_type:
4816 case DW_TAG_reference_type:
4817 case DW_TAG_string_type:
4818 break;
4819
4820 case DW_TAG_base_type:
4821 case DW_TAG_subrange_type:
4822 case DW_TAG_typedef:
4823 /* Add a typedef symbol for the type definition, if it has a
4824 DW_AT_name. */
4825 new_symbol (die, read_type_die (die, cu), cu);
4826 break;
4827 case DW_TAG_common_block:
4828 read_common_block (die, cu);
4829 break;
4830 case DW_TAG_common_inclusion:
4831 break;
4832 case DW_TAG_namespace:
4833 processing_has_namespace_info = 1;
4834 read_namespace (die, cu);
4835 break;
4836 case DW_TAG_module:
4837 processing_has_namespace_info = 1;
4838 read_module (die, cu);
4839 break;
4840 case DW_TAG_imported_declaration:
4841 case DW_TAG_imported_module:
4842 processing_has_namespace_info = 1;
4843 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4844 || cu->language != language_fortran))
4845 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4846 dwarf_tag_name (die->tag));
4847 read_import_statement (die, cu);
4848 break;
4849 default:
4850 new_symbol (die, NULL, cu);
4851 break;
4852 }
4853 }
4854
4855 /* A helper function for dwarf2_compute_name which determines whether DIE
4856 needs to have the name of the scope prepended to the name listed in the
4857 die. */
4858
4859 static int
4860 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4861 {
4862 struct attribute *attr;
4863
4864 switch (die->tag)
4865 {
4866 case DW_TAG_namespace:
4867 case DW_TAG_typedef:
4868 case DW_TAG_class_type:
4869 case DW_TAG_interface_type:
4870 case DW_TAG_structure_type:
4871 case DW_TAG_union_type:
4872 case DW_TAG_enumeration_type:
4873 case DW_TAG_enumerator:
4874 case DW_TAG_subprogram:
4875 case DW_TAG_member:
4876 return 1;
4877
4878 case DW_TAG_variable:
4879 case DW_TAG_constant:
4880 /* We only need to prefix "globally" visible variables. These include
4881 any variable marked with DW_AT_external or any variable that
4882 lives in a namespace. [Variables in anonymous namespaces
4883 require prefixing, but they are not DW_AT_external.] */
4884
4885 if (dwarf2_attr (die, DW_AT_specification, cu))
4886 {
4887 struct dwarf2_cu *spec_cu = cu;
4888
4889 return die_needs_namespace (die_specification (die, &spec_cu),
4890 spec_cu);
4891 }
4892
4893 attr = dwarf2_attr (die, DW_AT_external, cu);
4894 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4895 && die->parent->tag != DW_TAG_module)
4896 return 0;
4897 /* A variable in a lexical block of some kind does not need a
4898 namespace, even though in C++ such variables may be external
4899 and have a mangled name. */
4900 if (die->parent->tag == DW_TAG_lexical_block
4901 || die->parent->tag == DW_TAG_try_block
4902 || die->parent->tag == DW_TAG_catch_block
4903 || die->parent->tag == DW_TAG_subprogram)
4904 return 0;
4905 return 1;
4906
4907 default:
4908 return 0;
4909 }
4910 }
4911
4912 /* Retrieve the last character from a mem_file. */
4913
4914 static void
4915 do_ui_file_peek_last (void *object, const char *buffer, long length)
4916 {
4917 char *last_char_p = (char *) object;
4918
4919 if (length > 0)
4920 *last_char_p = buffer[length - 1];
4921 }
4922
4923 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4924 compute the physname for the object, which include a method's
4925 formal parameters (C++/Java) and return type (Java).
4926
4927 For Ada, return the DIE's linkage name rather than the fully qualified
4928 name. PHYSNAME is ignored..
4929
4930 The result is allocated on the objfile_obstack and canonicalized. */
4931
4932 static const char *
4933 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4934 int physname)
4935 {
4936 if (name == NULL)
4937 name = dwarf2_name (die, cu);
4938
4939 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4940 compute it by typename_concat inside GDB. */
4941 if (cu->language == language_ada
4942 || (cu->language == language_fortran && physname))
4943 {
4944 /* For Ada unit, we prefer the linkage name over the name, as
4945 the former contains the exported name, which the user expects
4946 to be able to reference. Ideally, we want the user to be able
4947 to reference this entity using either natural or linkage name,
4948 but we haven't started looking at this enhancement yet. */
4949 struct attribute *attr;
4950
4951 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4952 if (attr == NULL)
4953 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4954 if (attr && DW_STRING (attr))
4955 return DW_STRING (attr);
4956 }
4957
4958 /* These are the only languages we know how to qualify names in. */
4959 if (name != NULL
4960 && (cu->language == language_cplus || cu->language == language_java
4961 || cu->language == language_fortran))
4962 {
4963 if (die_needs_namespace (die, cu))
4964 {
4965 long length;
4966 char *prefix;
4967 struct ui_file *buf;
4968
4969 prefix = determine_prefix (die, cu);
4970 buf = mem_fileopen ();
4971 if (*prefix != '\0')
4972 {
4973 char *prefixed_name = typename_concat (NULL, prefix, name,
4974 physname, cu);
4975
4976 fputs_unfiltered (prefixed_name, buf);
4977 xfree (prefixed_name);
4978 }
4979 else
4980 fputs_unfiltered (name, buf);
4981
4982 /* Template parameters may be specified in the DIE's DW_AT_name, or
4983 as children with DW_TAG_template_type_param or
4984 DW_TAG_value_type_param. If the latter, add them to the name
4985 here. If the name already has template parameters, then
4986 skip this step; some versions of GCC emit both, and
4987 it is more efficient to use the pre-computed name.
4988
4989 Something to keep in mind about this process: it is very
4990 unlikely, or in some cases downright impossible, to produce
4991 something that will match the mangled name of a function.
4992 If the definition of the function has the same debug info,
4993 we should be able to match up with it anyway. But fallbacks
4994 using the minimal symbol, for instance to find a method
4995 implemented in a stripped copy of libstdc++, will not work.
4996 If we do not have debug info for the definition, we will have to
4997 match them up some other way.
4998
4999 When we do name matching there is a related problem with function
5000 templates; two instantiated function templates are allowed to
5001 differ only by their return types, which we do not add here. */
5002
5003 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5004 {
5005 struct attribute *attr;
5006 struct die_info *child;
5007 int first = 1;
5008
5009 die->building_fullname = 1;
5010
5011 for (child = die->child; child != NULL; child = child->sibling)
5012 {
5013 struct type *type;
5014 long value;
5015 gdb_byte *bytes;
5016 struct dwarf2_locexpr_baton *baton;
5017 struct value *v;
5018
5019 if (child->tag != DW_TAG_template_type_param
5020 && child->tag != DW_TAG_template_value_param)
5021 continue;
5022
5023 if (first)
5024 {
5025 fputs_unfiltered ("<", buf);
5026 first = 0;
5027 }
5028 else
5029 fputs_unfiltered (", ", buf);
5030
5031 attr = dwarf2_attr (child, DW_AT_type, cu);
5032 if (attr == NULL)
5033 {
5034 complaint (&symfile_complaints,
5035 _("template parameter missing DW_AT_type"));
5036 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5037 continue;
5038 }
5039 type = die_type (child, cu);
5040
5041 if (child->tag == DW_TAG_template_type_param)
5042 {
5043 c_print_type (type, "", buf, -1, 0);
5044 continue;
5045 }
5046
5047 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5048 if (attr == NULL)
5049 {
5050 complaint (&symfile_complaints,
5051 _("template parameter missing "
5052 "DW_AT_const_value"));
5053 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5054 continue;
5055 }
5056
5057 dwarf2_const_value_attr (attr, type, name,
5058 &cu->comp_unit_obstack, cu,
5059 &value, &bytes, &baton);
5060
5061 if (TYPE_NOSIGN (type))
5062 /* GDB prints characters as NUMBER 'CHAR'. If that's
5063 changed, this can use value_print instead. */
5064 c_printchar (value, type, buf);
5065 else
5066 {
5067 struct value_print_options opts;
5068
5069 if (baton != NULL)
5070 v = dwarf2_evaluate_loc_desc (type, NULL,
5071 baton->data,
5072 baton->size,
5073 baton->per_cu);
5074 else if (bytes != NULL)
5075 {
5076 v = allocate_value (type);
5077 memcpy (value_contents_writeable (v), bytes,
5078 TYPE_LENGTH (type));
5079 }
5080 else
5081 v = value_from_longest (type, value);
5082
5083 /* Specify decimal so that we do not depend on
5084 the radix. */
5085 get_formatted_print_options (&opts, 'd');
5086 opts.raw = 1;
5087 value_print (v, buf, &opts);
5088 release_value (v);
5089 value_free (v);
5090 }
5091 }
5092
5093 die->building_fullname = 0;
5094
5095 if (!first)
5096 {
5097 /* Close the argument list, with a space if necessary
5098 (nested templates). */
5099 char last_char = '\0';
5100 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5101 if (last_char == '>')
5102 fputs_unfiltered (" >", buf);
5103 else
5104 fputs_unfiltered (">", buf);
5105 }
5106 }
5107
5108 /* For Java and C++ methods, append formal parameter type
5109 information, if PHYSNAME. */
5110
5111 if (physname && die->tag == DW_TAG_subprogram
5112 && (cu->language == language_cplus
5113 || cu->language == language_java))
5114 {
5115 struct type *type = read_type_die (die, cu);
5116
5117 c_type_print_args (type, buf, 1, cu->language);
5118
5119 if (cu->language == language_java)
5120 {
5121 /* For java, we must append the return type to method
5122 names. */
5123 if (die->tag == DW_TAG_subprogram)
5124 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5125 0, 0);
5126 }
5127 else if (cu->language == language_cplus)
5128 {
5129 /* Assume that an artificial first parameter is
5130 "this", but do not crash if it is not. RealView
5131 marks unnamed (and thus unused) parameters as
5132 artificial; there is no way to differentiate
5133 the two cases. */
5134 if (TYPE_NFIELDS (type) > 0
5135 && TYPE_FIELD_ARTIFICIAL (type, 0)
5136 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5137 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5138 0))))
5139 fputs_unfiltered (" const", buf);
5140 }
5141 }
5142
5143 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5144 &length);
5145 ui_file_delete (buf);
5146
5147 if (cu->language == language_cplus)
5148 {
5149 char *cname
5150 = dwarf2_canonicalize_name (name, cu,
5151 &cu->objfile->objfile_obstack);
5152
5153 if (cname != NULL)
5154 name = cname;
5155 }
5156 }
5157 }
5158
5159 return name;
5160 }
5161
5162 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5163 If scope qualifiers are appropriate they will be added. The result
5164 will be allocated on the objfile_obstack, or NULL if the DIE does
5165 not have a name. NAME may either be from a previous call to
5166 dwarf2_name or NULL.
5167
5168 The output string will be canonicalized (if C++/Java). */
5169
5170 static const char *
5171 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5172 {
5173 return dwarf2_compute_name (name, die, cu, 0);
5174 }
5175
5176 /* Construct a physname for the given DIE in CU. NAME may either be
5177 from a previous call to dwarf2_name or NULL. The result will be
5178 allocated on the objfile_objstack or NULL if the DIE does not have a
5179 name.
5180
5181 The output string will be canonicalized (if C++/Java). */
5182
5183 static const char *
5184 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5185 {
5186 struct attribute *attr;
5187 const char *retval, *mangled = NULL, *canon = NULL;
5188 struct cleanup *back_to;
5189 int need_copy = 1;
5190
5191 /* In this case dwarf2_compute_name is just a shortcut not building anything
5192 on its own. */
5193 if (!die_needs_namespace (die, cu))
5194 return dwarf2_compute_name (name, die, cu, 1);
5195
5196 back_to = make_cleanup (null_cleanup, NULL);
5197
5198 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5199 if (!attr)
5200 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5201
5202 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5203 has computed. */
5204 if (attr && DW_STRING (attr))
5205 {
5206 char *demangled;
5207
5208 mangled = DW_STRING (attr);
5209
5210 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5211 type. It is easier for GDB users to search for such functions as
5212 `name(params)' than `long name(params)'. In such case the minimal
5213 symbol names do not match the full symbol names but for template
5214 functions there is never a need to look up their definition from their
5215 declaration so the only disadvantage remains the minimal symbol
5216 variant `long name(params)' does not have the proper inferior type.
5217 */
5218
5219 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5220 | (cu->language == language_java
5221 ? DMGL_JAVA | DMGL_RET_POSTFIX
5222 : DMGL_RET_DROP)));
5223 if (demangled)
5224 {
5225 make_cleanup (xfree, demangled);
5226 canon = demangled;
5227 }
5228 else
5229 {
5230 canon = mangled;
5231 need_copy = 0;
5232 }
5233 }
5234
5235 if (canon == NULL || check_physname)
5236 {
5237 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5238
5239 if (canon != NULL && strcmp (physname, canon) != 0)
5240 {
5241 /* It may not mean a bug in GDB. The compiler could also
5242 compute DW_AT_linkage_name incorrectly. But in such case
5243 GDB would need to be bug-to-bug compatible. */
5244
5245 complaint (&symfile_complaints,
5246 _("Computed physname <%s> does not match demangled <%s> "
5247 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5248 physname, canon, mangled, die->offset, cu->objfile->name);
5249
5250 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5251 is available here - over computed PHYSNAME. It is safer
5252 against both buggy GDB and buggy compilers. */
5253
5254 retval = canon;
5255 }
5256 else
5257 {
5258 retval = physname;
5259 need_copy = 0;
5260 }
5261 }
5262 else
5263 retval = canon;
5264
5265 if (need_copy)
5266 retval = obsavestring (retval, strlen (retval),
5267 &cu->objfile->objfile_obstack);
5268
5269 do_cleanups (back_to);
5270 return retval;
5271 }
5272
5273 /* Read the import statement specified by the given die and record it. */
5274
5275 static void
5276 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5277 {
5278 struct attribute *import_attr;
5279 struct die_info *imported_die, *child_die;
5280 struct dwarf2_cu *imported_cu;
5281 const char *imported_name;
5282 const char *imported_name_prefix;
5283 const char *canonical_name;
5284 const char *import_alias;
5285 const char *imported_declaration = NULL;
5286 const char *import_prefix;
5287 VEC (const_char_ptr) *excludes = NULL;
5288 struct cleanup *cleanups;
5289
5290 char *temp;
5291
5292 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5293 if (import_attr == NULL)
5294 {
5295 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5296 dwarf_tag_name (die->tag));
5297 return;
5298 }
5299
5300 imported_cu = cu;
5301 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5302 imported_name = dwarf2_name (imported_die, imported_cu);
5303 if (imported_name == NULL)
5304 {
5305 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5306
5307 The import in the following code:
5308 namespace A
5309 {
5310 typedef int B;
5311 }
5312
5313 int main ()
5314 {
5315 using A::B;
5316 B b;
5317 return b;
5318 }
5319
5320 ...
5321 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5322 <52> DW_AT_decl_file : 1
5323 <53> DW_AT_decl_line : 6
5324 <54> DW_AT_import : <0x75>
5325 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5326 <59> DW_AT_name : B
5327 <5b> DW_AT_decl_file : 1
5328 <5c> DW_AT_decl_line : 2
5329 <5d> DW_AT_type : <0x6e>
5330 ...
5331 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5332 <76> DW_AT_byte_size : 4
5333 <77> DW_AT_encoding : 5 (signed)
5334
5335 imports the wrong die ( 0x75 instead of 0x58 ).
5336 This case will be ignored until the gcc bug is fixed. */
5337 return;
5338 }
5339
5340 /* Figure out the local name after import. */
5341 import_alias = dwarf2_name (die, cu);
5342
5343 /* Figure out where the statement is being imported to. */
5344 import_prefix = determine_prefix (die, cu);
5345
5346 /* Figure out what the scope of the imported die is and prepend it
5347 to the name of the imported die. */
5348 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5349
5350 if (imported_die->tag != DW_TAG_namespace
5351 && imported_die->tag != DW_TAG_module)
5352 {
5353 imported_declaration = imported_name;
5354 canonical_name = imported_name_prefix;
5355 }
5356 else if (strlen (imported_name_prefix) > 0)
5357 {
5358 temp = alloca (strlen (imported_name_prefix)
5359 + 2 + strlen (imported_name) + 1);
5360 strcpy (temp, imported_name_prefix);
5361 strcat (temp, "::");
5362 strcat (temp, imported_name);
5363 canonical_name = temp;
5364 }
5365 else
5366 canonical_name = imported_name;
5367
5368 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5369
5370 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5371 for (child_die = die->child; child_die && child_die->tag;
5372 child_die = sibling_die (child_die))
5373 {
5374 /* DWARF-4: A Fortran use statement with a “rename list” may be
5375 represented by an imported module entry with an import attribute
5376 referring to the module and owned entries corresponding to those
5377 entities that are renamed as part of being imported. */
5378
5379 if (child_die->tag != DW_TAG_imported_declaration)
5380 {
5381 complaint (&symfile_complaints,
5382 _("child DW_TAG_imported_declaration expected "
5383 "- DIE at 0x%x [in module %s]"),
5384 child_die->offset, cu->objfile->name);
5385 continue;
5386 }
5387
5388 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5389 if (import_attr == NULL)
5390 {
5391 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5392 dwarf_tag_name (child_die->tag));
5393 continue;
5394 }
5395
5396 imported_cu = cu;
5397 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5398 &imported_cu);
5399 imported_name = dwarf2_name (imported_die, imported_cu);
5400 if (imported_name == NULL)
5401 {
5402 complaint (&symfile_complaints,
5403 _("child DW_TAG_imported_declaration has unknown "
5404 "imported name - DIE at 0x%x [in module %s]"),
5405 child_die->offset, cu->objfile->name);
5406 continue;
5407 }
5408
5409 VEC_safe_push (const_char_ptr, excludes, imported_name);
5410
5411 process_die (child_die, cu);
5412 }
5413
5414 cp_add_using_directive (import_prefix,
5415 canonical_name,
5416 import_alias,
5417 imported_declaration,
5418 excludes,
5419 &cu->objfile->objfile_obstack);
5420
5421 do_cleanups (cleanups);
5422 }
5423
5424 static void
5425 initialize_cu_func_list (struct dwarf2_cu *cu)
5426 {
5427 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5428 }
5429
5430 /* Cleanup function for read_file_scope. */
5431
5432 static void
5433 free_cu_line_header (void *arg)
5434 {
5435 struct dwarf2_cu *cu = arg;
5436
5437 free_line_header (cu->line_header);
5438 cu->line_header = NULL;
5439 }
5440
5441 static void
5442 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5443 char **name, char **comp_dir)
5444 {
5445 struct attribute *attr;
5446
5447 *name = NULL;
5448 *comp_dir = NULL;
5449
5450 /* Find the filename. Do not use dwarf2_name here, since the filename
5451 is not a source language identifier. */
5452 attr = dwarf2_attr (die, DW_AT_name, cu);
5453 if (attr)
5454 {
5455 *name = DW_STRING (attr);
5456 }
5457
5458 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5459 if (attr)
5460 *comp_dir = DW_STRING (attr);
5461 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5462 {
5463 *comp_dir = ldirname (*name);
5464 if (*comp_dir != NULL)
5465 make_cleanup (xfree, *comp_dir);
5466 }
5467 if (*comp_dir != NULL)
5468 {
5469 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5470 directory, get rid of it. */
5471 char *cp = strchr (*comp_dir, ':');
5472
5473 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5474 *comp_dir = cp + 1;
5475 }
5476
5477 if (*name == NULL)
5478 *name = "<unknown>";
5479 }
5480
5481 /* Process DW_TAG_compile_unit. */
5482
5483 static void
5484 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5485 {
5486 struct objfile *objfile = cu->objfile;
5487 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5488 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5489 CORE_ADDR highpc = ((CORE_ADDR) 0);
5490 struct attribute *attr;
5491 char *name = NULL;
5492 char *comp_dir = NULL;
5493 struct die_info *child_die;
5494 bfd *abfd = objfile->obfd;
5495 struct line_header *line_header = 0;
5496 CORE_ADDR baseaddr;
5497
5498 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5499
5500 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5501
5502 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5503 from finish_block. */
5504 if (lowpc == ((CORE_ADDR) -1))
5505 lowpc = highpc;
5506 lowpc += baseaddr;
5507 highpc += baseaddr;
5508
5509 find_file_and_directory (die, cu, &name, &comp_dir);
5510
5511 attr = dwarf2_attr (die, DW_AT_language, cu);
5512 if (attr)
5513 {
5514 set_cu_language (DW_UNSND (attr), cu);
5515 }
5516
5517 attr = dwarf2_attr (die, DW_AT_producer, cu);
5518 if (attr)
5519 cu->producer = DW_STRING (attr);
5520
5521 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5522 standardised yet. As a workaround for the language detection we fall
5523 back to the DW_AT_producer string. */
5524 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5525 cu->language = language_opencl;
5526
5527 /* We assume that we're processing GCC output. */
5528 processing_gcc_compilation = 2;
5529
5530 processing_has_namespace_info = 0;
5531
5532 start_symtab (name, comp_dir, lowpc);
5533 record_debugformat ("DWARF 2");
5534 record_producer (cu->producer);
5535
5536 initialize_cu_func_list (cu);
5537
5538 /* Decode line number information if present. We do this before
5539 processing child DIEs, so that the line header table is available
5540 for DW_AT_decl_file. */
5541 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5542 if (attr)
5543 {
5544 unsigned int line_offset = DW_UNSND (attr);
5545 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5546 if (line_header)
5547 {
5548 cu->line_header = line_header;
5549 make_cleanup (free_cu_line_header, cu);
5550 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5551 }
5552 }
5553
5554 /* Process all dies in compilation unit. */
5555 if (die->child != NULL)
5556 {
5557 child_die = die->child;
5558 while (child_die && child_die->tag)
5559 {
5560 process_die (child_die, cu);
5561 child_die = sibling_die (child_die);
5562 }
5563 }
5564
5565 /* Decode macro information, if present. Dwarf 2 macro information
5566 refers to information in the line number info statement program
5567 header, so we can only read it if we've read the header
5568 successfully. */
5569 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5570 if (attr && line_header)
5571 {
5572 unsigned int macro_offset = DW_UNSND (attr);
5573
5574 dwarf_decode_macros (line_header, macro_offset,
5575 comp_dir, abfd, cu);
5576 }
5577 do_cleanups (back_to);
5578 }
5579
5580 /* Process DW_TAG_type_unit.
5581 For TUs we want to skip the first top level sibling if it's not the
5582 actual type being defined by this TU. In this case the first top
5583 level sibling is there to provide context only. */
5584
5585 static void
5586 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5587 {
5588 struct objfile *objfile = cu->objfile;
5589 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5590 CORE_ADDR lowpc;
5591 struct attribute *attr;
5592 char *name = NULL;
5593 char *comp_dir = NULL;
5594 struct die_info *child_die;
5595 bfd *abfd = objfile->obfd;
5596
5597 /* start_symtab needs a low pc, but we don't really have one.
5598 Do what read_file_scope would do in the absence of such info. */
5599 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5600
5601 /* Find the filename. Do not use dwarf2_name here, since the filename
5602 is not a source language identifier. */
5603 attr = dwarf2_attr (die, DW_AT_name, cu);
5604 if (attr)
5605 name = DW_STRING (attr);
5606
5607 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5608 if (attr)
5609 comp_dir = DW_STRING (attr);
5610 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5611 {
5612 comp_dir = ldirname (name);
5613 if (comp_dir != NULL)
5614 make_cleanup (xfree, comp_dir);
5615 }
5616
5617 if (name == NULL)
5618 name = "<unknown>";
5619
5620 attr = dwarf2_attr (die, DW_AT_language, cu);
5621 if (attr)
5622 set_cu_language (DW_UNSND (attr), cu);
5623
5624 /* This isn't technically needed today. It is done for symmetry
5625 with read_file_scope. */
5626 attr = dwarf2_attr (die, DW_AT_producer, cu);
5627 if (attr)
5628 cu->producer = DW_STRING (attr);
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 /* Process the dies in the type unit. */
5640 if (die->child == NULL)
5641 {
5642 dump_die_for_error (die);
5643 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5644 bfd_get_filename (abfd));
5645 }
5646
5647 child_die = die->child;
5648
5649 while (child_die && child_die->tag)
5650 {
5651 process_die (child_die, cu);
5652
5653 child_die = sibling_die (child_die);
5654 }
5655
5656 do_cleanups (back_to);
5657 }
5658
5659 static void
5660 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5661 struct dwarf2_cu *cu)
5662 {
5663 struct function_range *thisfn;
5664
5665 thisfn = (struct function_range *)
5666 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5667 thisfn->name = name;
5668 thisfn->lowpc = lowpc;
5669 thisfn->highpc = highpc;
5670 thisfn->seen_line = 0;
5671 thisfn->next = NULL;
5672
5673 if (cu->last_fn == NULL)
5674 cu->first_fn = thisfn;
5675 else
5676 cu->last_fn->next = thisfn;
5677
5678 cu->last_fn = thisfn;
5679 }
5680
5681 /* qsort helper for inherit_abstract_dies. */
5682
5683 static int
5684 unsigned_int_compar (const void *ap, const void *bp)
5685 {
5686 unsigned int a = *(unsigned int *) ap;
5687 unsigned int b = *(unsigned int *) bp;
5688
5689 return (a > b) - (b > a);
5690 }
5691
5692 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5693 Inherit only the children of the DW_AT_abstract_origin DIE not being
5694 already referenced by DW_AT_abstract_origin from the children of the
5695 current DIE. */
5696
5697 static void
5698 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5699 {
5700 struct die_info *child_die;
5701 unsigned die_children_count;
5702 /* CU offsets which were referenced by children of the current DIE. */
5703 unsigned *offsets;
5704 unsigned *offsets_end, *offsetp;
5705 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5706 struct die_info *origin_die;
5707 /* Iterator of the ORIGIN_DIE children. */
5708 struct die_info *origin_child_die;
5709 struct cleanup *cleanups;
5710 struct attribute *attr;
5711 struct dwarf2_cu *origin_cu;
5712 struct pending **origin_previous_list_in_scope;
5713
5714 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5715 if (!attr)
5716 return;
5717
5718 /* Note that following die references may follow to a die in a
5719 different cu. */
5720
5721 origin_cu = cu;
5722 origin_die = follow_die_ref (die, attr, &origin_cu);
5723
5724 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5725 symbols in. */
5726 origin_previous_list_in_scope = origin_cu->list_in_scope;
5727 origin_cu->list_in_scope = cu->list_in_scope;
5728
5729 if (die->tag != origin_die->tag
5730 && !(die->tag == DW_TAG_inlined_subroutine
5731 && origin_die->tag == DW_TAG_subprogram))
5732 complaint (&symfile_complaints,
5733 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5734 die->offset, origin_die->offset);
5735
5736 child_die = die->child;
5737 die_children_count = 0;
5738 while (child_die && child_die->tag)
5739 {
5740 child_die = sibling_die (child_die);
5741 die_children_count++;
5742 }
5743 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5744 cleanups = make_cleanup (xfree, offsets);
5745
5746 offsets_end = offsets;
5747 child_die = die->child;
5748 while (child_die && child_die->tag)
5749 {
5750 /* For each CHILD_DIE, find the corresponding child of
5751 ORIGIN_DIE. If there is more than one layer of
5752 DW_AT_abstract_origin, follow them all; there shouldn't be,
5753 but GCC versions at least through 4.4 generate this (GCC PR
5754 40573). */
5755 struct die_info *child_origin_die = child_die;
5756 struct dwarf2_cu *child_origin_cu = cu;
5757
5758 while (1)
5759 {
5760 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5761 child_origin_cu);
5762 if (attr == NULL)
5763 break;
5764 child_origin_die = follow_die_ref (child_origin_die, attr,
5765 &child_origin_cu);
5766 }
5767
5768 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5769 counterpart may exist. */
5770 if (child_origin_die != child_die)
5771 {
5772 if (child_die->tag != child_origin_die->tag
5773 && !(child_die->tag == DW_TAG_inlined_subroutine
5774 && child_origin_die->tag == DW_TAG_subprogram))
5775 complaint (&symfile_complaints,
5776 _("Child DIE 0x%x and its abstract origin 0x%x have "
5777 "different tags"), child_die->offset,
5778 child_origin_die->offset);
5779 if (child_origin_die->parent != origin_die)
5780 complaint (&symfile_complaints,
5781 _("Child DIE 0x%x and its abstract origin 0x%x have "
5782 "different parents"), child_die->offset,
5783 child_origin_die->offset);
5784 else
5785 *offsets_end++ = child_origin_die->offset;
5786 }
5787 child_die = sibling_die (child_die);
5788 }
5789 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5790 unsigned_int_compar);
5791 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5792 if (offsetp[-1] == *offsetp)
5793 complaint (&symfile_complaints,
5794 _("Multiple children of DIE 0x%x refer "
5795 "to DIE 0x%x as their abstract origin"),
5796 die->offset, *offsetp);
5797
5798 offsetp = offsets;
5799 origin_child_die = origin_die->child;
5800 while (origin_child_die && origin_child_die->tag)
5801 {
5802 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5803 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5804 offsetp++;
5805 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5806 {
5807 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5808 process_die (origin_child_die, origin_cu);
5809 }
5810 origin_child_die = sibling_die (origin_child_die);
5811 }
5812 origin_cu->list_in_scope = origin_previous_list_in_scope;
5813
5814 do_cleanups (cleanups);
5815 }
5816
5817 static void
5818 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5819 {
5820 struct objfile *objfile = cu->objfile;
5821 struct context_stack *new;
5822 CORE_ADDR lowpc;
5823 CORE_ADDR highpc;
5824 struct die_info *child_die;
5825 struct attribute *attr, *call_line, *call_file;
5826 char *name;
5827 CORE_ADDR baseaddr;
5828 struct block *block;
5829 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5830 VEC (symbolp) *template_args = NULL;
5831 struct template_symbol *templ_func = NULL;
5832
5833 if (inlined_func)
5834 {
5835 /* If we do not have call site information, we can't show the
5836 caller of this inlined function. That's too confusing, so
5837 only use the scope for local variables. */
5838 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5839 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5840 if (call_line == NULL || call_file == NULL)
5841 {
5842 read_lexical_block_scope (die, cu);
5843 return;
5844 }
5845 }
5846
5847 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5848
5849 name = dwarf2_name (die, cu);
5850
5851 /* Ignore functions with missing or empty names. These are actually
5852 illegal according to the DWARF standard. */
5853 if (name == NULL)
5854 {
5855 complaint (&symfile_complaints,
5856 _("missing name for subprogram DIE at %d"), die->offset);
5857 return;
5858 }
5859
5860 /* Ignore functions with missing or invalid low and high pc attributes. */
5861 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5862 {
5863 attr = dwarf2_attr (die, DW_AT_external, cu);
5864 if (!attr || !DW_UNSND (attr))
5865 complaint (&symfile_complaints,
5866 _("cannot get low and high bounds "
5867 "for subprogram DIE at %d"),
5868 die->offset);
5869 return;
5870 }
5871
5872 lowpc += baseaddr;
5873 highpc += baseaddr;
5874
5875 /* Record the function range for dwarf_decode_lines. */
5876 add_to_cu_func_list (name, lowpc, highpc, cu);
5877
5878 /* If we have any template arguments, then we must allocate a
5879 different sort of symbol. */
5880 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5881 {
5882 if (child_die->tag == DW_TAG_template_type_param
5883 || child_die->tag == DW_TAG_template_value_param)
5884 {
5885 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5886 struct template_symbol);
5887 templ_func->base.is_cplus_template_function = 1;
5888 break;
5889 }
5890 }
5891
5892 new = push_context (0, lowpc);
5893 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5894 (struct symbol *) templ_func);
5895
5896 /* If there is a location expression for DW_AT_frame_base, record
5897 it. */
5898 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5899 if (attr)
5900 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5901 expression is being recorded directly in the function's symbol
5902 and not in a separate frame-base object. I guess this hack is
5903 to avoid adding some sort of frame-base adjunct/annex to the
5904 function's symbol :-(. The problem with doing this is that it
5905 results in a function symbol with a location expression that
5906 has nothing to do with the location of the function, ouch! The
5907 relationship should be: a function's symbol has-a frame base; a
5908 frame-base has-a location expression. */
5909 dwarf2_symbol_mark_computed (attr, new->name, cu);
5910
5911 cu->list_in_scope = &local_symbols;
5912
5913 if (die->child != NULL)
5914 {
5915 child_die = die->child;
5916 while (child_die && child_die->tag)
5917 {
5918 if (child_die->tag == DW_TAG_template_type_param
5919 || child_die->tag == DW_TAG_template_value_param)
5920 {
5921 struct symbol *arg = new_symbol (child_die, NULL, cu);
5922
5923 if (arg != NULL)
5924 VEC_safe_push (symbolp, template_args, arg);
5925 }
5926 else
5927 process_die (child_die, cu);
5928 child_die = sibling_die (child_die);
5929 }
5930 }
5931
5932 inherit_abstract_dies (die, cu);
5933
5934 /* If we have a DW_AT_specification, we might need to import using
5935 directives from the context of the specification DIE. See the
5936 comment in determine_prefix. */
5937 if (cu->language == language_cplus
5938 && dwarf2_attr (die, DW_AT_specification, cu))
5939 {
5940 struct dwarf2_cu *spec_cu = cu;
5941 struct die_info *spec_die = die_specification (die, &spec_cu);
5942
5943 while (spec_die)
5944 {
5945 child_die = spec_die->child;
5946 while (child_die && child_die->tag)
5947 {
5948 if (child_die->tag == DW_TAG_imported_module)
5949 process_die (child_die, spec_cu);
5950 child_die = sibling_die (child_die);
5951 }
5952
5953 /* In some cases, GCC generates specification DIEs that
5954 themselves contain DW_AT_specification attributes. */
5955 spec_die = die_specification (spec_die, &spec_cu);
5956 }
5957 }
5958
5959 new = pop_context ();
5960 /* Make a block for the local symbols within. */
5961 block = finish_block (new->name, &local_symbols, new->old_blocks,
5962 lowpc, highpc, objfile);
5963
5964 /* For C++, set the block's scope. */
5965 if (cu->language == language_cplus || cu->language == language_fortran)
5966 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5967 determine_prefix (die, cu),
5968 processing_has_namespace_info);
5969
5970 /* If we have address ranges, record them. */
5971 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5972
5973 /* Attach template arguments to function. */
5974 if (! VEC_empty (symbolp, template_args))
5975 {
5976 gdb_assert (templ_func != NULL);
5977
5978 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5979 templ_func->template_arguments
5980 = obstack_alloc (&objfile->objfile_obstack,
5981 (templ_func->n_template_arguments
5982 * sizeof (struct symbol *)));
5983 memcpy (templ_func->template_arguments,
5984 VEC_address (symbolp, template_args),
5985 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5986 VEC_free (symbolp, template_args);
5987 }
5988
5989 /* In C++, we can have functions nested inside functions (e.g., when
5990 a function declares a class that has methods). This means that
5991 when we finish processing a function scope, we may need to go
5992 back to building a containing block's symbol lists. */
5993 local_symbols = new->locals;
5994 param_symbols = new->params;
5995 using_directives = new->using_directives;
5996
5997 /* If we've finished processing a top-level function, subsequent
5998 symbols go in the file symbol list. */
5999 if (outermost_context_p ())
6000 cu->list_in_scope = &file_symbols;
6001 }
6002
6003 /* Process all the DIES contained within a lexical block scope. Start
6004 a new scope, process the dies, and then close the scope. */
6005
6006 static void
6007 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6008 {
6009 struct objfile *objfile = cu->objfile;
6010 struct context_stack *new;
6011 CORE_ADDR lowpc, highpc;
6012 struct die_info *child_die;
6013 CORE_ADDR baseaddr;
6014
6015 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6016
6017 /* Ignore blocks with missing or invalid low and high pc attributes. */
6018 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6019 as multiple lexical blocks? Handling children in a sane way would
6020 be nasty. Might be easier to properly extend generic blocks to
6021 describe ranges. */
6022 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6023 return;
6024 lowpc += baseaddr;
6025 highpc += baseaddr;
6026
6027 push_context (0, lowpc);
6028 if (die->child != NULL)
6029 {
6030 child_die = die->child;
6031 while (child_die && child_die->tag)
6032 {
6033 process_die (child_die, cu);
6034 child_die = sibling_die (child_die);
6035 }
6036 }
6037 new = pop_context ();
6038
6039 if (local_symbols != NULL || using_directives != NULL)
6040 {
6041 struct block *block
6042 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6043 highpc, objfile);
6044
6045 /* Note that recording ranges after traversing children, as we
6046 do here, means that recording a parent's ranges entails
6047 walking across all its children's ranges as they appear in
6048 the address map, which is quadratic behavior.
6049
6050 It would be nicer to record the parent's ranges before
6051 traversing its children, simply overriding whatever you find
6052 there. But since we don't even decide whether to create a
6053 block until after we've traversed its children, that's hard
6054 to do. */
6055 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6056 }
6057 local_symbols = new->locals;
6058 using_directives = new->using_directives;
6059 }
6060
6061 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6062 Return 1 if the attributes are present and valid, otherwise, return 0.
6063 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6064
6065 static int
6066 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6067 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6068 struct partial_symtab *ranges_pst)
6069 {
6070 struct objfile *objfile = cu->objfile;
6071 struct comp_unit_head *cu_header = &cu->header;
6072 bfd *obfd = objfile->obfd;
6073 unsigned int addr_size = cu_header->addr_size;
6074 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6075 /* Base address selection entry. */
6076 CORE_ADDR base;
6077 int found_base;
6078 unsigned int dummy;
6079 gdb_byte *buffer;
6080 CORE_ADDR marker;
6081 int low_set;
6082 CORE_ADDR low = 0;
6083 CORE_ADDR high = 0;
6084 CORE_ADDR baseaddr;
6085
6086 found_base = cu->base_known;
6087 base = cu->base_address;
6088
6089 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6090 if (offset >= dwarf2_per_objfile->ranges.size)
6091 {
6092 complaint (&symfile_complaints,
6093 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6094 offset);
6095 return 0;
6096 }
6097 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6098
6099 /* Read in the largest possible address. */
6100 marker = read_address (obfd, buffer, cu, &dummy);
6101 if ((marker & mask) == mask)
6102 {
6103 /* If we found the largest possible address, then
6104 read the base address. */
6105 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6106 buffer += 2 * addr_size;
6107 offset += 2 * addr_size;
6108 found_base = 1;
6109 }
6110
6111 low_set = 0;
6112
6113 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6114
6115 while (1)
6116 {
6117 CORE_ADDR range_beginning, range_end;
6118
6119 range_beginning = read_address (obfd, buffer, cu, &dummy);
6120 buffer += addr_size;
6121 range_end = read_address (obfd, buffer, cu, &dummy);
6122 buffer += addr_size;
6123 offset += 2 * addr_size;
6124
6125 /* An end of list marker is a pair of zero addresses. */
6126 if (range_beginning == 0 && range_end == 0)
6127 /* Found the end of list entry. */
6128 break;
6129
6130 /* Each base address selection entry is a pair of 2 values.
6131 The first is the largest possible address, the second is
6132 the base address. Check for a base address here. */
6133 if ((range_beginning & mask) == mask)
6134 {
6135 /* If we found the largest possible address, then
6136 read the base address. */
6137 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6138 found_base = 1;
6139 continue;
6140 }
6141
6142 if (!found_base)
6143 {
6144 /* We have no valid base address for the ranges
6145 data. */
6146 complaint (&symfile_complaints,
6147 _("Invalid .debug_ranges data (no base address)"));
6148 return 0;
6149 }
6150
6151 if (range_beginning > range_end)
6152 {
6153 /* Inverted range entries are invalid. */
6154 complaint (&symfile_complaints,
6155 _("Invalid .debug_ranges data (inverted range)"));
6156 return 0;
6157 }
6158
6159 /* Empty range entries have no effect. */
6160 if (range_beginning == range_end)
6161 continue;
6162
6163 range_beginning += base;
6164 range_end += base;
6165
6166 if (ranges_pst != NULL)
6167 addrmap_set_empty (objfile->psymtabs_addrmap,
6168 range_beginning + baseaddr,
6169 range_end - 1 + baseaddr,
6170 ranges_pst);
6171
6172 /* FIXME: This is recording everything as a low-high
6173 segment of consecutive addresses. We should have a
6174 data structure for discontiguous block ranges
6175 instead. */
6176 if (! low_set)
6177 {
6178 low = range_beginning;
6179 high = range_end;
6180 low_set = 1;
6181 }
6182 else
6183 {
6184 if (range_beginning < low)
6185 low = range_beginning;
6186 if (range_end > high)
6187 high = range_end;
6188 }
6189 }
6190
6191 if (! low_set)
6192 /* If the first entry is an end-of-list marker, the range
6193 describes an empty scope, i.e. no instructions. */
6194 return 0;
6195
6196 if (low_return)
6197 *low_return = low;
6198 if (high_return)
6199 *high_return = high;
6200 return 1;
6201 }
6202
6203 /* Get low and high pc attributes from a die. Return 1 if the attributes
6204 are present and valid, otherwise, return 0. Return -1 if the range is
6205 discontinuous, i.e. derived from DW_AT_ranges information. */
6206 static int
6207 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6208 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6209 struct partial_symtab *pst)
6210 {
6211 struct attribute *attr;
6212 CORE_ADDR low = 0;
6213 CORE_ADDR high = 0;
6214 int ret = 0;
6215
6216 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6217 if (attr)
6218 {
6219 high = DW_ADDR (attr);
6220 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6221 if (attr)
6222 low = DW_ADDR (attr);
6223 else
6224 /* Found high w/o low attribute. */
6225 return 0;
6226
6227 /* Found consecutive range of addresses. */
6228 ret = 1;
6229 }
6230 else
6231 {
6232 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6233 if (attr != NULL)
6234 {
6235 /* Value of the DW_AT_ranges attribute is the offset in the
6236 .debug_ranges section. */
6237 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6238 return 0;
6239 /* Found discontinuous range of addresses. */
6240 ret = -1;
6241 }
6242 }
6243
6244 /* read_partial_die has also the strict LOW < HIGH requirement. */
6245 if (high <= low)
6246 return 0;
6247
6248 /* When using the GNU linker, .gnu.linkonce. sections are used to
6249 eliminate duplicate copies of functions and vtables and such.
6250 The linker will arbitrarily choose one and discard the others.
6251 The AT_*_pc values for such functions refer to local labels in
6252 these sections. If the section from that file was discarded, the
6253 labels are not in the output, so the relocs get a value of 0.
6254 If this is a discarded function, mark the pc bounds as invalid,
6255 so that GDB will ignore it. */
6256 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6257 return 0;
6258
6259 *lowpc = low;
6260 *highpc = high;
6261 return ret;
6262 }
6263
6264 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6265 its low and high PC addresses. Do nothing if these addresses could not
6266 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6267 and HIGHPC to the high address if greater than HIGHPC. */
6268
6269 static void
6270 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6271 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6272 struct dwarf2_cu *cu)
6273 {
6274 CORE_ADDR low, high;
6275 struct die_info *child = die->child;
6276
6277 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6278 {
6279 *lowpc = min (*lowpc, low);
6280 *highpc = max (*highpc, high);
6281 }
6282
6283 /* If the language does not allow nested subprograms (either inside
6284 subprograms or lexical blocks), we're done. */
6285 if (cu->language != language_ada)
6286 return;
6287
6288 /* Check all the children of the given DIE. If it contains nested
6289 subprograms, then check their pc bounds. Likewise, we need to
6290 check lexical blocks as well, as they may also contain subprogram
6291 definitions. */
6292 while (child && child->tag)
6293 {
6294 if (child->tag == DW_TAG_subprogram
6295 || child->tag == DW_TAG_lexical_block)
6296 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6297 child = sibling_die (child);
6298 }
6299 }
6300
6301 /* Get the low and high pc's represented by the scope DIE, and store
6302 them in *LOWPC and *HIGHPC. If the correct values can't be
6303 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6304
6305 static void
6306 get_scope_pc_bounds (struct die_info *die,
6307 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6308 struct dwarf2_cu *cu)
6309 {
6310 CORE_ADDR best_low = (CORE_ADDR) -1;
6311 CORE_ADDR best_high = (CORE_ADDR) 0;
6312 CORE_ADDR current_low, current_high;
6313
6314 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6315 {
6316 best_low = current_low;
6317 best_high = current_high;
6318 }
6319 else
6320 {
6321 struct die_info *child = die->child;
6322
6323 while (child && child->tag)
6324 {
6325 switch (child->tag) {
6326 case DW_TAG_subprogram:
6327 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6328 break;
6329 case DW_TAG_namespace:
6330 case DW_TAG_module:
6331 /* FIXME: carlton/2004-01-16: Should we do this for
6332 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6333 that current GCC's always emit the DIEs corresponding
6334 to definitions of methods of classes as children of a
6335 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6336 the DIEs giving the declarations, which could be
6337 anywhere). But I don't see any reason why the
6338 standards says that they have to be there. */
6339 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6340
6341 if (current_low != ((CORE_ADDR) -1))
6342 {
6343 best_low = min (best_low, current_low);
6344 best_high = max (best_high, current_high);
6345 }
6346 break;
6347 default:
6348 /* Ignore. */
6349 break;
6350 }
6351
6352 child = sibling_die (child);
6353 }
6354 }
6355
6356 *lowpc = best_low;
6357 *highpc = best_high;
6358 }
6359
6360 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6361 in DIE. */
6362 static void
6363 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6364 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6365 {
6366 struct attribute *attr;
6367
6368 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6369 if (attr)
6370 {
6371 CORE_ADDR high = DW_ADDR (attr);
6372
6373 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6374 if (attr)
6375 {
6376 CORE_ADDR low = DW_ADDR (attr);
6377
6378 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6379 }
6380 }
6381
6382 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6383 if (attr)
6384 {
6385 bfd *obfd = cu->objfile->obfd;
6386
6387 /* The value of the DW_AT_ranges attribute is the offset of the
6388 address range list in the .debug_ranges section. */
6389 unsigned long offset = DW_UNSND (attr);
6390 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6391
6392 /* For some target architectures, but not others, the
6393 read_address function sign-extends the addresses it returns.
6394 To recognize base address selection entries, we need a
6395 mask. */
6396 unsigned int addr_size = cu->header.addr_size;
6397 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6398
6399 /* The base address, to which the next pair is relative. Note
6400 that this 'base' is a DWARF concept: most entries in a range
6401 list are relative, to reduce the number of relocs against the
6402 debugging information. This is separate from this function's
6403 'baseaddr' argument, which GDB uses to relocate debugging
6404 information from a shared library based on the address at
6405 which the library was loaded. */
6406 CORE_ADDR base = cu->base_address;
6407 int base_known = cu->base_known;
6408
6409 gdb_assert (dwarf2_per_objfile->ranges.readin);
6410 if (offset >= dwarf2_per_objfile->ranges.size)
6411 {
6412 complaint (&symfile_complaints,
6413 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6414 offset);
6415 return;
6416 }
6417
6418 for (;;)
6419 {
6420 unsigned int bytes_read;
6421 CORE_ADDR start, end;
6422
6423 start = read_address (obfd, buffer, cu, &bytes_read);
6424 buffer += bytes_read;
6425 end = read_address (obfd, buffer, cu, &bytes_read);
6426 buffer += bytes_read;
6427
6428 /* Did we find the end of the range list? */
6429 if (start == 0 && end == 0)
6430 break;
6431
6432 /* Did we find a base address selection entry? */
6433 else if ((start & base_select_mask) == base_select_mask)
6434 {
6435 base = end;
6436 base_known = 1;
6437 }
6438
6439 /* We found an ordinary address range. */
6440 else
6441 {
6442 if (!base_known)
6443 {
6444 complaint (&symfile_complaints,
6445 _("Invalid .debug_ranges data "
6446 "(no base address)"));
6447 return;
6448 }
6449
6450 if (start > end)
6451 {
6452 /* Inverted range entries are invalid. */
6453 complaint (&symfile_complaints,
6454 _("Invalid .debug_ranges data "
6455 "(inverted range)"));
6456 return;
6457 }
6458
6459 /* Empty range entries have no effect. */
6460 if (start == end)
6461 continue;
6462
6463 record_block_range (block,
6464 baseaddr + base + start,
6465 baseaddr + base + end - 1);
6466 }
6467 }
6468 }
6469 }
6470
6471 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6472 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6473 during 4.6.0 experimental. */
6474
6475 static int
6476 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6477 {
6478 const char *cs;
6479 int major, minor, release;
6480
6481 if (cu->producer == NULL)
6482 {
6483 /* For unknown compilers expect their behavior is DWARF version
6484 compliant.
6485
6486 GCC started to support .debug_types sections by -gdwarf-4 since
6487 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6488 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6489 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6490 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6491
6492 return 0;
6493 }
6494
6495 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6496
6497 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6498 {
6499 /* For non-GCC compilers expect their behavior is DWARF version
6500 compliant. */
6501
6502 return 0;
6503 }
6504 cs = &cu->producer[strlen ("GNU ")];
6505 while (*cs && !isdigit (*cs))
6506 cs++;
6507 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6508 {
6509 /* Not recognized as GCC. */
6510
6511 return 0;
6512 }
6513
6514 return major < 4 || (major == 4 && minor < 6);
6515 }
6516
6517 /* Return the default accessibility type if it is not overriden by
6518 DW_AT_accessibility. */
6519
6520 static enum dwarf_access_attribute
6521 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6522 {
6523 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6524 {
6525 /* The default DWARF 2 accessibility for members is public, the default
6526 accessibility for inheritance is private. */
6527
6528 if (die->tag != DW_TAG_inheritance)
6529 return DW_ACCESS_public;
6530 else
6531 return DW_ACCESS_private;
6532 }
6533 else
6534 {
6535 /* DWARF 3+ defines the default accessibility a different way. The same
6536 rules apply now for DW_TAG_inheritance as for the members and it only
6537 depends on the container kind. */
6538
6539 if (die->parent->tag == DW_TAG_class_type)
6540 return DW_ACCESS_private;
6541 else
6542 return DW_ACCESS_public;
6543 }
6544 }
6545
6546 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6547 offset. If the attribute was not found return 0, otherwise return
6548 1. If it was found but could not properly be handled, set *OFFSET
6549 to 0. */
6550
6551 static int
6552 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6553 LONGEST *offset)
6554 {
6555 struct attribute *attr;
6556
6557 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6558 if (attr != NULL)
6559 {
6560 *offset = 0;
6561
6562 /* Note that we do not check for a section offset first here.
6563 This is because DW_AT_data_member_location is new in DWARF 4,
6564 so if we see it, we can assume that a constant form is really
6565 a constant and not a section offset. */
6566 if (attr_form_is_constant (attr))
6567 *offset = dwarf2_get_attr_constant_value (attr, 0);
6568 else if (attr_form_is_section_offset (attr))
6569 dwarf2_complex_location_expr_complaint ();
6570 else if (attr_form_is_block (attr))
6571 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6572 else
6573 dwarf2_complex_location_expr_complaint ();
6574
6575 return 1;
6576 }
6577
6578 return 0;
6579 }
6580
6581 /* Add an aggregate field to the field list. */
6582
6583 static void
6584 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6585 struct dwarf2_cu *cu)
6586 {
6587 struct objfile *objfile = cu->objfile;
6588 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6589 struct nextfield *new_field;
6590 struct attribute *attr;
6591 struct field *fp;
6592 char *fieldname = "";
6593
6594 /* Allocate a new field list entry and link it in. */
6595 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6596 make_cleanup (xfree, new_field);
6597 memset (new_field, 0, sizeof (struct nextfield));
6598
6599 if (die->tag == DW_TAG_inheritance)
6600 {
6601 new_field->next = fip->baseclasses;
6602 fip->baseclasses = new_field;
6603 }
6604 else
6605 {
6606 new_field->next = fip->fields;
6607 fip->fields = new_field;
6608 }
6609 fip->nfields++;
6610
6611 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6612 if (attr)
6613 new_field->accessibility = DW_UNSND (attr);
6614 else
6615 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6616 if (new_field->accessibility != DW_ACCESS_public)
6617 fip->non_public_fields = 1;
6618
6619 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6620 if (attr)
6621 new_field->virtuality = DW_UNSND (attr);
6622 else
6623 new_field->virtuality = DW_VIRTUALITY_none;
6624
6625 fp = &new_field->field;
6626
6627 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6628 {
6629 LONGEST offset;
6630
6631 /* Data member other than a C++ static data member. */
6632
6633 /* Get type of field. */
6634 fp->type = die_type (die, cu);
6635
6636 SET_FIELD_BITPOS (*fp, 0);
6637
6638 /* Get bit size of field (zero if none). */
6639 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6640 if (attr)
6641 {
6642 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6643 }
6644 else
6645 {
6646 FIELD_BITSIZE (*fp) = 0;
6647 }
6648
6649 /* Get bit offset of field. */
6650 if (handle_data_member_location (die, cu, &offset))
6651 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6652 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6653 if (attr)
6654 {
6655 if (gdbarch_bits_big_endian (gdbarch))
6656 {
6657 /* For big endian bits, the DW_AT_bit_offset gives the
6658 additional bit offset from the MSB of the containing
6659 anonymous object to the MSB of the field. We don't
6660 have to do anything special since we don't need to
6661 know the size of the anonymous object. */
6662 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6663 }
6664 else
6665 {
6666 /* For little endian bits, compute the bit offset to the
6667 MSB of the anonymous object, subtract off the number of
6668 bits from the MSB of the field to the MSB of the
6669 object, and then subtract off the number of bits of
6670 the field itself. The result is the bit offset of
6671 the LSB of the field. */
6672 int anonymous_size;
6673 int bit_offset = DW_UNSND (attr);
6674
6675 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6676 if (attr)
6677 {
6678 /* The size of the anonymous object containing
6679 the bit field is explicit, so use the
6680 indicated size (in bytes). */
6681 anonymous_size = DW_UNSND (attr);
6682 }
6683 else
6684 {
6685 /* The size of the anonymous object containing
6686 the bit field must be inferred from the type
6687 attribute of the data member containing the
6688 bit field. */
6689 anonymous_size = TYPE_LENGTH (fp->type);
6690 }
6691 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6692 - bit_offset - FIELD_BITSIZE (*fp);
6693 }
6694 }
6695
6696 /* Get name of field. */
6697 fieldname = dwarf2_name (die, cu);
6698 if (fieldname == NULL)
6699 fieldname = "";
6700
6701 /* The name is already allocated along with this objfile, so we don't
6702 need to duplicate it for the type. */
6703 fp->name = fieldname;
6704
6705 /* Change accessibility for artificial fields (e.g. virtual table
6706 pointer or virtual base class pointer) to private. */
6707 if (dwarf2_attr (die, DW_AT_artificial, cu))
6708 {
6709 FIELD_ARTIFICIAL (*fp) = 1;
6710 new_field->accessibility = DW_ACCESS_private;
6711 fip->non_public_fields = 1;
6712 }
6713 }
6714 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6715 {
6716 /* C++ static member. */
6717
6718 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6719 is a declaration, but all versions of G++ as of this writing
6720 (so through at least 3.2.1) incorrectly generate
6721 DW_TAG_variable tags. */
6722
6723 const char *physname;
6724
6725 /* Get name of field. */
6726 fieldname = dwarf2_name (die, cu);
6727 if (fieldname == NULL)
6728 return;
6729
6730 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6731 if (attr
6732 /* Only create a symbol if this is an external value.
6733 new_symbol checks this and puts the value in the global symbol
6734 table, which we want. If it is not external, new_symbol
6735 will try to put the value in cu->list_in_scope which is wrong. */
6736 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6737 {
6738 /* A static const member, not much different than an enum as far as
6739 we're concerned, except that we can support more types. */
6740 new_symbol (die, NULL, cu);
6741 }
6742
6743 /* Get physical name. */
6744 physname = dwarf2_physname (fieldname, die, cu);
6745
6746 /* The name is already allocated along with this objfile, so we don't
6747 need to duplicate it for the type. */
6748 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6749 FIELD_TYPE (*fp) = die_type (die, cu);
6750 FIELD_NAME (*fp) = fieldname;
6751 }
6752 else if (die->tag == DW_TAG_inheritance)
6753 {
6754 LONGEST offset;
6755
6756 /* C++ base class field. */
6757 if (handle_data_member_location (die, cu, &offset))
6758 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6759 FIELD_BITSIZE (*fp) = 0;
6760 FIELD_TYPE (*fp) = die_type (die, cu);
6761 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6762 fip->nbaseclasses++;
6763 }
6764 }
6765
6766 /* Add a typedef defined in the scope of the FIP's class. */
6767
6768 static void
6769 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6770 struct dwarf2_cu *cu)
6771 {
6772 struct objfile *objfile = cu->objfile;
6773 struct typedef_field_list *new_field;
6774 struct attribute *attr;
6775 struct typedef_field *fp;
6776 char *fieldname = "";
6777
6778 /* Allocate a new field list entry and link it in. */
6779 new_field = xzalloc (sizeof (*new_field));
6780 make_cleanup (xfree, new_field);
6781
6782 gdb_assert (die->tag == DW_TAG_typedef);
6783
6784 fp = &new_field->field;
6785
6786 /* Get name of field. */
6787 fp->name = dwarf2_name (die, cu);
6788 if (fp->name == NULL)
6789 return;
6790
6791 fp->type = read_type_die (die, cu);
6792
6793 new_field->next = fip->typedef_field_list;
6794 fip->typedef_field_list = new_field;
6795 fip->typedef_field_list_count++;
6796 }
6797
6798 /* Create the vector of fields, and attach it to the type. */
6799
6800 static void
6801 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6802 struct dwarf2_cu *cu)
6803 {
6804 int nfields = fip->nfields;
6805
6806 /* Record the field count, allocate space for the array of fields,
6807 and create blank accessibility bitfields if necessary. */
6808 TYPE_NFIELDS (type) = nfields;
6809 TYPE_FIELDS (type) = (struct field *)
6810 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6811 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6812
6813 if (fip->non_public_fields && cu->language != language_ada)
6814 {
6815 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6816
6817 TYPE_FIELD_PRIVATE_BITS (type) =
6818 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6819 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6820
6821 TYPE_FIELD_PROTECTED_BITS (type) =
6822 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6823 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6824
6825 TYPE_FIELD_IGNORE_BITS (type) =
6826 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6827 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6828 }
6829
6830 /* If the type has baseclasses, allocate and clear a bit vector for
6831 TYPE_FIELD_VIRTUAL_BITS. */
6832 if (fip->nbaseclasses && cu->language != language_ada)
6833 {
6834 int num_bytes = B_BYTES (fip->nbaseclasses);
6835 unsigned char *pointer;
6836
6837 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6838 pointer = TYPE_ALLOC (type, num_bytes);
6839 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6840 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6841 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6842 }
6843
6844 /* Copy the saved-up fields into the field vector. Start from the head of
6845 the list, adding to the tail of the field array, so that they end up in
6846 the same order in the array in which they were added to the list. */
6847 while (nfields-- > 0)
6848 {
6849 struct nextfield *fieldp;
6850
6851 if (fip->fields)
6852 {
6853 fieldp = fip->fields;
6854 fip->fields = fieldp->next;
6855 }
6856 else
6857 {
6858 fieldp = fip->baseclasses;
6859 fip->baseclasses = fieldp->next;
6860 }
6861
6862 TYPE_FIELD (type, nfields) = fieldp->field;
6863 switch (fieldp->accessibility)
6864 {
6865 case DW_ACCESS_private:
6866 if (cu->language != language_ada)
6867 SET_TYPE_FIELD_PRIVATE (type, nfields);
6868 break;
6869
6870 case DW_ACCESS_protected:
6871 if (cu->language != language_ada)
6872 SET_TYPE_FIELD_PROTECTED (type, nfields);
6873 break;
6874
6875 case DW_ACCESS_public:
6876 break;
6877
6878 default:
6879 /* Unknown accessibility. Complain and treat it as public. */
6880 {
6881 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6882 fieldp->accessibility);
6883 }
6884 break;
6885 }
6886 if (nfields < fip->nbaseclasses)
6887 {
6888 switch (fieldp->virtuality)
6889 {
6890 case DW_VIRTUALITY_virtual:
6891 case DW_VIRTUALITY_pure_virtual:
6892 if (cu->language == language_ada)
6893 error (_("unexpected virtuality in component of Ada type"));
6894 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6895 break;
6896 }
6897 }
6898 }
6899 }
6900
6901 /* Add a member function to the proper fieldlist. */
6902
6903 static void
6904 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6905 struct type *type, struct dwarf2_cu *cu)
6906 {
6907 struct objfile *objfile = cu->objfile;
6908 struct attribute *attr;
6909 struct fnfieldlist *flp;
6910 int i;
6911 struct fn_field *fnp;
6912 char *fieldname;
6913 struct nextfnfield *new_fnfield;
6914 struct type *this_type;
6915 enum dwarf_access_attribute accessibility;
6916
6917 if (cu->language == language_ada)
6918 error (_("unexpected member function in Ada type"));
6919
6920 /* Get name of member function. */
6921 fieldname = dwarf2_name (die, cu);
6922 if (fieldname == NULL)
6923 return;
6924
6925 /* Look up member function name in fieldlist. */
6926 for (i = 0; i < fip->nfnfields; i++)
6927 {
6928 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6929 break;
6930 }
6931
6932 /* Create new list element if necessary. */
6933 if (i < fip->nfnfields)
6934 flp = &fip->fnfieldlists[i];
6935 else
6936 {
6937 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6938 {
6939 fip->fnfieldlists = (struct fnfieldlist *)
6940 xrealloc (fip->fnfieldlists,
6941 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6942 * sizeof (struct fnfieldlist));
6943 if (fip->nfnfields == 0)
6944 make_cleanup (free_current_contents, &fip->fnfieldlists);
6945 }
6946 flp = &fip->fnfieldlists[fip->nfnfields];
6947 flp->name = fieldname;
6948 flp->length = 0;
6949 flp->head = NULL;
6950 i = fip->nfnfields++;
6951 }
6952
6953 /* Create a new member function field and chain it to the field list
6954 entry. */
6955 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6956 make_cleanup (xfree, new_fnfield);
6957 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6958 new_fnfield->next = flp->head;
6959 flp->head = new_fnfield;
6960 flp->length++;
6961
6962 /* Fill in the member function field info. */
6963 fnp = &new_fnfield->fnfield;
6964
6965 /* Delay processing of the physname until later. */
6966 if (cu->language == language_cplus || cu->language == language_java)
6967 {
6968 add_to_method_list (type, i, flp->length - 1, fieldname,
6969 die, cu);
6970 }
6971 else
6972 {
6973 const char *physname = dwarf2_physname (fieldname, die, cu);
6974 fnp->physname = physname ? physname : "";
6975 }
6976
6977 fnp->type = alloc_type (objfile);
6978 this_type = read_type_die (die, cu);
6979 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6980 {
6981 int nparams = TYPE_NFIELDS (this_type);
6982
6983 /* TYPE is the domain of this method, and THIS_TYPE is the type
6984 of the method itself (TYPE_CODE_METHOD). */
6985 smash_to_method_type (fnp->type, type,
6986 TYPE_TARGET_TYPE (this_type),
6987 TYPE_FIELDS (this_type),
6988 TYPE_NFIELDS (this_type),
6989 TYPE_VARARGS (this_type));
6990
6991 /* Handle static member functions.
6992 Dwarf2 has no clean way to discern C++ static and non-static
6993 member functions. G++ helps GDB by marking the first
6994 parameter for non-static member functions (which is the this
6995 pointer) as artificial. We obtain this information from
6996 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
6997 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6998 fnp->voffset = VOFFSET_STATIC;
6999 }
7000 else
7001 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7002 dwarf2_full_name (fieldname, die, cu));
7003
7004 /* Get fcontext from DW_AT_containing_type if present. */
7005 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7006 fnp->fcontext = die_containing_type (die, cu);
7007
7008 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7009 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7010
7011 /* Get accessibility. */
7012 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7013 if (attr)
7014 accessibility = DW_UNSND (attr);
7015 else
7016 accessibility = dwarf2_default_access_attribute (die, cu);
7017 switch (accessibility)
7018 {
7019 case DW_ACCESS_private:
7020 fnp->is_private = 1;
7021 break;
7022 case DW_ACCESS_protected:
7023 fnp->is_protected = 1;
7024 break;
7025 }
7026
7027 /* Check for artificial methods. */
7028 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7029 if (attr && DW_UNSND (attr) != 0)
7030 fnp->is_artificial = 1;
7031
7032 /* Get index in virtual function table if it is a virtual member
7033 function. For older versions of GCC, this is an offset in the
7034 appropriate virtual table, as specified by DW_AT_containing_type.
7035 For everyone else, it is an expression to be evaluated relative
7036 to the object address. */
7037
7038 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7039 if (attr)
7040 {
7041 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7042 {
7043 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7044 {
7045 /* Old-style GCC. */
7046 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7047 }
7048 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7049 || (DW_BLOCK (attr)->size > 1
7050 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7051 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7052 {
7053 struct dwarf_block blk;
7054 int offset;
7055
7056 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7057 ? 1 : 2);
7058 blk.size = DW_BLOCK (attr)->size - offset;
7059 blk.data = DW_BLOCK (attr)->data + offset;
7060 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7061 if ((fnp->voffset % cu->header.addr_size) != 0)
7062 dwarf2_complex_location_expr_complaint ();
7063 else
7064 fnp->voffset /= cu->header.addr_size;
7065 fnp->voffset += 2;
7066 }
7067 else
7068 dwarf2_complex_location_expr_complaint ();
7069
7070 if (!fnp->fcontext)
7071 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7072 }
7073 else if (attr_form_is_section_offset (attr))
7074 {
7075 dwarf2_complex_location_expr_complaint ();
7076 }
7077 else
7078 {
7079 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7080 fieldname);
7081 }
7082 }
7083 else
7084 {
7085 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7086 if (attr && DW_UNSND (attr))
7087 {
7088 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7089 complaint (&symfile_complaints,
7090 _("Member function \"%s\" (offset %d) is virtual "
7091 "but the vtable offset is not specified"),
7092 fieldname, die->offset);
7093 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7094 TYPE_CPLUS_DYNAMIC (type) = 1;
7095 }
7096 }
7097 }
7098
7099 /* Create the vector of member function fields, and attach it to the type. */
7100
7101 static void
7102 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7103 struct dwarf2_cu *cu)
7104 {
7105 struct fnfieldlist *flp;
7106 int total_length = 0;
7107 int i;
7108
7109 if (cu->language == language_ada)
7110 error (_("unexpected member functions in Ada type"));
7111
7112 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7113 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7114 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7115
7116 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7117 {
7118 struct nextfnfield *nfp = flp->head;
7119 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7120 int k;
7121
7122 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7123 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7124 fn_flp->fn_fields = (struct fn_field *)
7125 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7126 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7127 fn_flp->fn_fields[k] = nfp->fnfield;
7128
7129 total_length += flp->length;
7130 }
7131
7132 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7133 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7134 }
7135
7136 /* Returns non-zero if NAME is the name of a vtable member in CU's
7137 language, zero otherwise. */
7138 static int
7139 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7140 {
7141 static const char vptr[] = "_vptr";
7142 static const char vtable[] = "vtable";
7143
7144 /* Look for the C++ and Java forms of the vtable. */
7145 if ((cu->language == language_java
7146 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7147 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7148 && is_cplus_marker (name[sizeof (vptr) - 1])))
7149 return 1;
7150
7151 return 0;
7152 }
7153
7154 /* GCC outputs unnamed structures that are really pointers to member
7155 functions, with the ABI-specified layout. If TYPE describes
7156 such a structure, smash it into a member function type.
7157
7158 GCC shouldn't do this; it should just output pointer to member DIEs.
7159 This is GCC PR debug/28767. */
7160
7161 static void
7162 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7163 {
7164 struct type *pfn_type, *domain_type, *new_type;
7165
7166 /* Check for a structure with no name and two children. */
7167 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7168 return;
7169
7170 /* Check for __pfn and __delta members. */
7171 if (TYPE_FIELD_NAME (type, 0) == NULL
7172 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7173 || TYPE_FIELD_NAME (type, 1) == NULL
7174 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7175 return;
7176
7177 /* Find the type of the method. */
7178 pfn_type = TYPE_FIELD_TYPE (type, 0);
7179 if (pfn_type == NULL
7180 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7181 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7182 return;
7183
7184 /* Look for the "this" argument. */
7185 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7186 if (TYPE_NFIELDS (pfn_type) == 0
7187 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7188 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7189 return;
7190
7191 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7192 new_type = alloc_type (objfile);
7193 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7194 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7195 TYPE_VARARGS (pfn_type));
7196 smash_to_methodptr_type (type, new_type);
7197 }
7198
7199 /* Called when we find the DIE that starts a structure or union scope
7200 (definition) to create a type for the structure or union. Fill in
7201 the type's name and general properties; the members will not be
7202 processed until process_structure_type.
7203
7204 NOTE: we need to call these functions regardless of whether or not the
7205 DIE has a DW_AT_name attribute, since it might be an anonymous
7206 structure or union. This gets the type entered into our set of
7207 user defined types.
7208
7209 However, if the structure is incomplete (an opaque struct/union)
7210 then suppress creating a symbol table entry for it since gdb only
7211 wants to find the one with the complete definition. Note that if
7212 it is complete, we just call new_symbol, which does it's own
7213 checking about whether the struct/union is anonymous or not (and
7214 suppresses creating a symbol table entry itself). */
7215
7216 static struct type *
7217 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7218 {
7219 struct objfile *objfile = cu->objfile;
7220 struct type *type;
7221 struct attribute *attr;
7222 char *name;
7223
7224 /* If the definition of this type lives in .debug_types, read that type.
7225 Don't follow DW_AT_specification though, that will take us back up
7226 the chain and we want to go down. */
7227 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7228 if (attr)
7229 {
7230 struct dwarf2_cu *type_cu = cu;
7231 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7232
7233 /* We could just recurse on read_structure_type, but we need to call
7234 get_die_type to ensure only one type for this DIE is created.
7235 This is important, for example, because for c++ classes we need
7236 TYPE_NAME set which is only done by new_symbol. Blech. */
7237 type = read_type_die (type_die, type_cu);
7238
7239 /* TYPE_CU may not be the same as CU.
7240 Ensure TYPE is recorded in CU's type_hash table. */
7241 return set_die_type (die, type, cu);
7242 }
7243
7244 type = alloc_type (objfile);
7245 INIT_CPLUS_SPECIFIC (type);
7246
7247 name = dwarf2_name (die, cu);
7248 if (name != NULL)
7249 {
7250 if (cu->language == language_cplus
7251 || cu->language == language_java)
7252 {
7253 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7254
7255 /* dwarf2_full_name might have already finished building the DIE's
7256 type. If so, there is no need to continue. */
7257 if (get_die_type (die, cu) != NULL)
7258 return get_die_type (die, cu);
7259
7260 TYPE_TAG_NAME (type) = full_name;
7261 if (die->tag == DW_TAG_structure_type
7262 || die->tag == DW_TAG_class_type)
7263 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7264 }
7265 else
7266 {
7267 /* The name is already allocated along with this objfile, so
7268 we don't need to duplicate it for the type. */
7269 TYPE_TAG_NAME (type) = (char *) name;
7270 if (die->tag == DW_TAG_class_type)
7271 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7272 }
7273 }
7274
7275 if (die->tag == DW_TAG_structure_type)
7276 {
7277 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7278 }
7279 else if (die->tag == DW_TAG_union_type)
7280 {
7281 TYPE_CODE (type) = TYPE_CODE_UNION;
7282 }
7283 else
7284 {
7285 TYPE_CODE (type) = TYPE_CODE_CLASS;
7286 }
7287
7288 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7289 TYPE_DECLARED_CLASS (type) = 1;
7290
7291 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7292 if (attr)
7293 {
7294 TYPE_LENGTH (type) = DW_UNSND (attr);
7295 }
7296 else
7297 {
7298 TYPE_LENGTH (type) = 0;
7299 }
7300
7301 TYPE_STUB_SUPPORTED (type) = 1;
7302 if (die_is_declaration (die, cu))
7303 TYPE_STUB (type) = 1;
7304 else if (attr == NULL && die->child == NULL
7305 && producer_is_realview (cu->producer))
7306 /* RealView does not output the required DW_AT_declaration
7307 on incomplete types. */
7308 TYPE_STUB (type) = 1;
7309
7310 /* We need to add the type field to the die immediately so we don't
7311 infinitely recurse when dealing with pointers to the structure
7312 type within the structure itself. */
7313 set_die_type (die, type, cu);
7314
7315 /* set_die_type should be already done. */
7316 set_descriptive_type (type, die, cu);
7317
7318 return type;
7319 }
7320
7321 /* Finish creating a structure or union type, including filling in
7322 its members and creating a symbol for it. */
7323
7324 static void
7325 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7326 {
7327 struct objfile *objfile = cu->objfile;
7328 struct die_info *child_die = die->child;
7329 struct type *type;
7330
7331 type = get_die_type (die, cu);
7332 if (type == NULL)
7333 type = read_structure_type (die, cu);
7334
7335 if (die->child != NULL && ! die_is_declaration (die, cu))
7336 {
7337 struct field_info fi;
7338 struct die_info *child_die;
7339 VEC (symbolp) *template_args = NULL;
7340 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7341
7342 memset (&fi, 0, sizeof (struct field_info));
7343
7344 child_die = die->child;
7345
7346 while (child_die && child_die->tag)
7347 {
7348 if (child_die->tag == DW_TAG_member
7349 || child_die->tag == DW_TAG_variable)
7350 {
7351 /* NOTE: carlton/2002-11-05: A C++ static data member
7352 should be a DW_TAG_member that is a declaration, but
7353 all versions of G++ as of this writing (so through at
7354 least 3.2.1) incorrectly generate DW_TAG_variable
7355 tags for them instead. */
7356 dwarf2_add_field (&fi, child_die, cu);
7357 }
7358 else if (child_die->tag == DW_TAG_subprogram)
7359 {
7360 /* C++ member function. */
7361 dwarf2_add_member_fn (&fi, child_die, type, cu);
7362 }
7363 else if (child_die->tag == DW_TAG_inheritance)
7364 {
7365 /* C++ base class field. */
7366 dwarf2_add_field (&fi, child_die, cu);
7367 }
7368 else if (child_die->tag == DW_TAG_typedef)
7369 dwarf2_add_typedef (&fi, child_die, cu);
7370 else if (child_die->tag == DW_TAG_template_type_param
7371 || child_die->tag == DW_TAG_template_value_param)
7372 {
7373 struct symbol *arg = new_symbol (child_die, NULL, cu);
7374
7375 if (arg != NULL)
7376 VEC_safe_push (symbolp, template_args, arg);
7377 }
7378
7379 child_die = sibling_die (child_die);
7380 }
7381
7382 /* Attach template arguments to type. */
7383 if (! VEC_empty (symbolp, template_args))
7384 {
7385 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7386 TYPE_N_TEMPLATE_ARGUMENTS (type)
7387 = VEC_length (symbolp, template_args);
7388 TYPE_TEMPLATE_ARGUMENTS (type)
7389 = obstack_alloc (&objfile->objfile_obstack,
7390 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7391 * sizeof (struct symbol *)));
7392 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7393 VEC_address (symbolp, template_args),
7394 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7395 * sizeof (struct symbol *)));
7396 VEC_free (symbolp, template_args);
7397 }
7398
7399 /* Attach fields and member functions to the type. */
7400 if (fi.nfields)
7401 dwarf2_attach_fields_to_type (&fi, type, cu);
7402 if (fi.nfnfields)
7403 {
7404 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7405
7406 /* Get the type which refers to the base class (possibly this
7407 class itself) which contains the vtable pointer for the current
7408 class from the DW_AT_containing_type attribute. This use of
7409 DW_AT_containing_type is a GNU extension. */
7410
7411 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7412 {
7413 struct type *t = die_containing_type (die, cu);
7414
7415 TYPE_VPTR_BASETYPE (type) = t;
7416 if (type == t)
7417 {
7418 int i;
7419
7420 /* Our own class provides vtbl ptr. */
7421 for (i = TYPE_NFIELDS (t) - 1;
7422 i >= TYPE_N_BASECLASSES (t);
7423 --i)
7424 {
7425 char *fieldname = TYPE_FIELD_NAME (t, i);
7426
7427 if (is_vtable_name (fieldname, cu))
7428 {
7429 TYPE_VPTR_FIELDNO (type) = i;
7430 break;
7431 }
7432 }
7433
7434 /* Complain if virtual function table field not found. */
7435 if (i < TYPE_N_BASECLASSES (t))
7436 complaint (&symfile_complaints,
7437 _("virtual function table pointer "
7438 "not found when defining class '%s'"),
7439 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7440 "");
7441 }
7442 else
7443 {
7444 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7445 }
7446 }
7447 else if (cu->producer
7448 && strncmp (cu->producer,
7449 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7450 {
7451 /* The IBM XLC compiler does not provide direct indication
7452 of the containing type, but the vtable pointer is
7453 always named __vfp. */
7454
7455 int i;
7456
7457 for (i = TYPE_NFIELDS (type) - 1;
7458 i >= TYPE_N_BASECLASSES (type);
7459 --i)
7460 {
7461 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7462 {
7463 TYPE_VPTR_FIELDNO (type) = i;
7464 TYPE_VPTR_BASETYPE (type) = type;
7465 break;
7466 }
7467 }
7468 }
7469 }
7470
7471 /* Copy fi.typedef_field_list linked list elements content into the
7472 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7473 if (fi.typedef_field_list)
7474 {
7475 int i = fi.typedef_field_list_count;
7476
7477 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7478 TYPE_TYPEDEF_FIELD_ARRAY (type)
7479 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7480 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7481
7482 /* Reverse the list order to keep the debug info elements order. */
7483 while (--i >= 0)
7484 {
7485 struct typedef_field *dest, *src;
7486
7487 dest = &TYPE_TYPEDEF_FIELD (type, i);
7488 src = &fi.typedef_field_list->field;
7489 fi.typedef_field_list = fi.typedef_field_list->next;
7490 *dest = *src;
7491 }
7492 }
7493
7494 do_cleanups (back_to);
7495 }
7496
7497 quirk_gcc_member_function_pointer (type, cu->objfile);
7498
7499 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7500 snapshots) has been known to create a die giving a declaration
7501 for a class that has, as a child, a die giving a definition for a
7502 nested class. So we have to process our children even if the
7503 current die is a declaration. Normally, of course, a declaration
7504 won't have any children at all. */
7505
7506 while (child_die != NULL && child_die->tag)
7507 {
7508 if (child_die->tag == DW_TAG_member
7509 || child_die->tag == DW_TAG_variable
7510 || child_die->tag == DW_TAG_inheritance
7511 || child_die->tag == DW_TAG_template_value_param
7512 || child_die->tag == DW_TAG_template_type_param)
7513 {
7514 /* Do nothing. */
7515 }
7516 else
7517 process_die (child_die, cu);
7518
7519 child_die = sibling_die (child_die);
7520 }
7521
7522 /* Do not consider external references. According to the DWARF standard,
7523 these DIEs are identified by the fact that they have no byte_size
7524 attribute, and a declaration attribute. */
7525 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7526 || !die_is_declaration (die, cu))
7527 new_symbol (die, type, cu);
7528 }
7529
7530 /* Given a DW_AT_enumeration_type die, set its type. We do not
7531 complete the type's fields yet, or create any symbols. */
7532
7533 static struct type *
7534 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7535 {
7536 struct objfile *objfile = cu->objfile;
7537 struct type *type;
7538 struct attribute *attr;
7539 const char *name;
7540
7541 /* If the definition of this type lives in .debug_types, read that type.
7542 Don't follow DW_AT_specification though, that will take us back up
7543 the chain and we want to go down. */
7544 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7545 if (attr)
7546 {
7547 struct dwarf2_cu *type_cu = cu;
7548 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7549
7550 type = read_type_die (type_die, type_cu);
7551
7552 /* TYPE_CU may not be the same as CU.
7553 Ensure TYPE is recorded in CU's type_hash table. */
7554 return set_die_type (die, type, cu);
7555 }
7556
7557 type = alloc_type (objfile);
7558
7559 TYPE_CODE (type) = TYPE_CODE_ENUM;
7560 name = dwarf2_full_name (NULL, die, cu);
7561 if (name != NULL)
7562 TYPE_TAG_NAME (type) = (char *) name;
7563
7564 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7565 if (attr)
7566 {
7567 TYPE_LENGTH (type) = DW_UNSND (attr);
7568 }
7569 else
7570 {
7571 TYPE_LENGTH (type) = 0;
7572 }
7573
7574 /* The enumeration DIE can be incomplete. In Ada, any type can be
7575 declared as private in the package spec, and then defined only
7576 inside the package body. Such types are known as Taft Amendment
7577 Types. When another package uses such a type, an incomplete DIE
7578 may be generated by the compiler. */
7579 if (die_is_declaration (die, cu))
7580 TYPE_STUB (type) = 1;
7581
7582 return set_die_type (die, type, cu);
7583 }
7584
7585 /* Given a pointer to a die which begins an enumeration, process all
7586 the dies that define the members of the enumeration, and create the
7587 symbol for the enumeration type.
7588
7589 NOTE: We reverse the order of the element list. */
7590
7591 static void
7592 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7593 {
7594 struct type *this_type;
7595
7596 this_type = get_die_type (die, cu);
7597 if (this_type == NULL)
7598 this_type = read_enumeration_type (die, cu);
7599
7600 if (die->child != NULL)
7601 {
7602 struct die_info *child_die;
7603 struct symbol *sym;
7604 struct field *fields = NULL;
7605 int num_fields = 0;
7606 int unsigned_enum = 1;
7607 char *name;
7608
7609 child_die = die->child;
7610 while (child_die && child_die->tag)
7611 {
7612 if (child_die->tag != DW_TAG_enumerator)
7613 {
7614 process_die (child_die, cu);
7615 }
7616 else
7617 {
7618 name = dwarf2_name (child_die, cu);
7619 if (name)
7620 {
7621 sym = new_symbol (child_die, this_type, cu);
7622 if (SYMBOL_VALUE (sym) < 0)
7623 unsigned_enum = 0;
7624
7625 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7626 {
7627 fields = (struct field *)
7628 xrealloc (fields,
7629 (num_fields + DW_FIELD_ALLOC_CHUNK)
7630 * sizeof (struct field));
7631 }
7632
7633 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7634 FIELD_TYPE (fields[num_fields]) = NULL;
7635 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7636 FIELD_BITSIZE (fields[num_fields]) = 0;
7637
7638 num_fields++;
7639 }
7640 }
7641
7642 child_die = sibling_die (child_die);
7643 }
7644
7645 if (num_fields)
7646 {
7647 TYPE_NFIELDS (this_type) = num_fields;
7648 TYPE_FIELDS (this_type) = (struct field *)
7649 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7650 memcpy (TYPE_FIELDS (this_type), fields,
7651 sizeof (struct field) * num_fields);
7652 xfree (fields);
7653 }
7654 if (unsigned_enum)
7655 TYPE_UNSIGNED (this_type) = 1;
7656 }
7657
7658 new_symbol (die, this_type, cu);
7659 }
7660
7661 /* Extract all information from a DW_TAG_array_type DIE and put it in
7662 the DIE's type field. For now, this only handles one dimensional
7663 arrays. */
7664
7665 static struct type *
7666 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7667 {
7668 struct objfile *objfile = cu->objfile;
7669 struct die_info *child_die;
7670 struct type *type;
7671 struct type *element_type, *range_type, *index_type;
7672 struct type **range_types = NULL;
7673 struct attribute *attr;
7674 int ndim = 0;
7675 struct cleanup *back_to;
7676 char *name;
7677
7678 element_type = die_type (die, cu);
7679
7680 /* The die_type call above may have already set the type for this DIE. */
7681 type = get_die_type (die, cu);
7682 if (type)
7683 return type;
7684
7685 /* Irix 6.2 native cc creates array types without children for
7686 arrays with unspecified length. */
7687 if (die->child == NULL)
7688 {
7689 index_type = objfile_type (objfile)->builtin_int;
7690 range_type = create_range_type (NULL, index_type, 0, -1);
7691 type = create_array_type (NULL, element_type, range_type);
7692 return set_die_type (die, type, cu);
7693 }
7694
7695 back_to = make_cleanup (null_cleanup, NULL);
7696 child_die = die->child;
7697 while (child_die && child_die->tag)
7698 {
7699 if (child_die->tag == DW_TAG_subrange_type)
7700 {
7701 struct type *child_type = read_type_die (child_die, cu);
7702
7703 if (child_type != NULL)
7704 {
7705 /* The range type was succesfully read. Save it for the
7706 array type creation. */
7707 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7708 {
7709 range_types = (struct type **)
7710 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7711 * sizeof (struct type *));
7712 if (ndim == 0)
7713 make_cleanup (free_current_contents, &range_types);
7714 }
7715 range_types[ndim++] = child_type;
7716 }
7717 }
7718 child_die = sibling_die (child_die);
7719 }
7720
7721 /* Dwarf2 dimensions are output from left to right, create the
7722 necessary array types in backwards order. */
7723
7724 type = element_type;
7725
7726 if (read_array_order (die, cu) == DW_ORD_col_major)
7727 {
7728 int i = 0;
7729
7730 while (i < ndim)
7731 type = create_array_type (NULL, type, range_types[i++]);
7732 }
7733 else
7734 {
7735 while (ndim-- > 0)
7736 type = create_array_type (NULL, type, range_types[ndim]);
7737 }
7738
7739 /* Understand Dwarf2 support for vector types (like they occur on
7740 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7741 array type. This is not part of the Dwarf2/3 standard yet, but a
7742 custom vendor extension. The main difference between a regular
7743 array and the vector variant is that vectors are passed by value
7744 to functions. */
7745 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7746 if (attr)
7747 make_vector_type (type);
7748
7749 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7750 implementation may choose to implement triple vectors using this
7751 attribute. */
7752 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7753 if (attr)
7754 {
7755 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7756 TYPE_LENGTH (type) = DW_UNSND (attr);
7757 else
7758 complaint (&symfile_complaints,
7759 _("DW_AT_byte_size for array type smaller "
7760 "than the total size of elements"));
7761 }
7762
7763 name = dwarf2_name (die, cu);
7764 if (name)
7765 TYPE_NAME (type) = name;
7766
7767 /* Install the type in the die. */
7768 set_die_type (die, type, cu);
7769
7770 /* set_die_type should be already done. */
7771 set_descriptive_type (type, die, cu);
7772
7773 do_cleanups (back_to);
7774
7775 return type;
7776 }
7777
7778 static enum dwarf_array_dim_ordering
7779 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7780 {
7781 struct attribute *attr;
7782
7783 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7784
7785 if (attr) return DW_SND (attr);
7786
7787 /* GNU F77 is a special case, as at 08/2004 array type info is the
7788 opposite order to the dwarf2 specification, but data is still
7789 laid out as per normal fortran.
7790
7791 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7792 version checking. */
7793
7794 if (cu->language == language_fortran
7795 && cu->producer && strstr (cu->producer, "GNU F77"))
7796 {
7797 return DW_ORD_row_major;
7798 }
7799
7800 switch (cu->language_defn->la_array_ordering)
7801 {
7802 case array_column_major:
7803 return DW_ORD_col_major;
7804 case array_row_major:
7805 default:
7806 return DW_ORD_row_major;
7807 };
7808 }
7809
7810 /* Extract all information from a DW_TAG_set_type DIE and put it in
7811 the DIE's type field. */
7812
7813 static struct type *
7814 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7815 {
7816 struct type *domain_type, *set_type;
7817 struct attribute *attr;
7818
7819 domain_type = die_type (die, cu);
7820
7821 /* The die_type call above may have already set the type for this DIE. */
7822 set_type = get_die_type (die, cu);
7823 if (set_type)
7824 return set_type;
7825
7826 set_type = create_set_type (NULL, domain_type);
7827
7828 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7829 if (attr)
7830 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7831
7832 return set_die_type (die, set_type, cu);
7833 }
7834
7835 /* First cut: install each common block member as a global variable. */
7836
7837 static void
7838 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7839 {
7840 struct die_info *child_die;
7841 struct attribute *attr;
7842 struct symbol *sym;
7843 CORE_ADDR base = (CORE_ADDR) 0;
7844
7845 attr = dwarf2_attr (die, DW_AT_location, cu);
7846 if (attr)
7847 {
7848 /* Support the .debug_loc offsets. */
7849 if (attr_form_is_block (attr))
7850 {
7851 base = decode_locdesc (DW_BLOCK (attr), cu);
7852 }
7853 else if (attr_form_is_section_offset (attr))
7854 {
7855 dwarf2_complex_location_expr_complaint ();
7856 }
7857 else
7858 {
7859 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7860 "common block member");
7861 }
7862 }
7863 if (die->child != NULL)
7864 {
7865 child_die = die->child;
7866 while (child_die && child_die->tag)
7867 {
7868 LONGEST offset;
7869
7870 sym = new_symbol (child_die, NULL, cu);
7871 if (sym != NULL
7872 && handle_data_member_location (child_die, cu, &offset))
7873 {
7874 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
7875 add_symbol_to_list (sym, &global_symbols);
7876 }
7877 child_die = sibling_die (child_die);
7878 }
7879 }
7880 }
7881
7882 /* Create a type for a C++ namespace. */
7883
7884 static struct type *
7885 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7886 {
7887 struct objfile *objfile = cu->objfile;
7888 const char *previous_prefix, *name;
7889 int is_anonymous;
7890 struct type *type;
7891
7892 /* For extensions, reuse the type of the original namespace. */
7893 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7894 {
7895 struct die_info *ext_die;
7896 struct dwarf2_cu *ext_cu = cu;
7897
7898 ext_die = dwarf2_extension (die, &ext_cu);
7899 type = read_type_die (ext_die, ext_cu);
7900
7901 /* EXT_CU may not be the same as CU.
7902 Ensure TYPE is recorded in CU's type_hash table. */
7903 return set_die_type (die, type, cu);
7904 }
7905
7906 name = namespace_name (die, &is_anonymous, cu);
7907
7908 /* Now build the name of the current namespace. */
7909
7910 previous_prefix = determine_prefix (die, cu);
7911 if (previous_prefix[0] != '\0')
7912 name = typename_concat (&objfile->objfile_obstack,
7913 previous_prefix, name, 0, cu);
7914
7915 /* Create the type. */
7916 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7917 objfile);
7918 TYPE_NAME (type) = (char *) name;
7919 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7920
7921 return set_die_type (die, type, cu);
7922 }
7923
7924 /* Read a C++ namespace. */
7925
7926 static void
7927 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7928 {
7929 struct objfile *objfile = cu->objfile;
7930 int is_anonymous;
7931
7932 /* Add a symbol associated to this if we haven't seen the namespace
7933 before. Also, add a using directive if it's an anonymous
7934 namespace. */
7935
7936 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7937 {
7938 struct type *type;
7939
7940 type = read_type_die (die, cu);
7941 new_symbol (die, type, cu);
7942
7943 namespace_name (die, &is_anonymous, cu);
7944 if (is_anonymous)
7945 {
7946 const char *previous_prefix = determine_prefix (die, cu);
7947
7948 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7949 NULL, NULL, &objfile->objfile_obstack);
7950 }
7951 }
7952
7953 if (die->child != NULL)
7954 {
7955 struct die_info *child_die = die->child;
7956
7957 while (child_die && child_die->tag)
7958 {
7959 process_die (child_die, cu);
7960 child_die = sibling_die (child_die);
7961 }
7962 }
7963 }
7964
7965 /* Read a Fortran module as type. This DIE can be only a declaration used for
7966 imported module. Still we need that type as local Fortran "use ... only"
7967 declaration imports depend on the created type in determine_prefix. */
7968
7969 static struct type *
7970 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7971 {
7972 struct objfile *objfile = cu->objfile;
7973 char *module_name;
7974 struct type *type;
7975
7976 module_name = dwarf2_name (die, cu);
7977 if (!module_name)
7978 complaint (&symfile_complaints,
7979 _("DW_TAG_module has no name, offset 0x%x"),
7980 die->offset);
7981 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7982
7983 /* determine_prefix uses TYPE_TAG_NAME. */
7984 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7985
7986 return set_die_type (die, type, cu);
7987 }
7988
7989 /* Read a Fortran module. */
7990
7991 static void
7992 read_module (struct die_info *die, struct dwarf2_cu *cu)
7993 {
7994 struct die_info *child_die = die->child;
7995
7996 while (child_die && child_die->tag)
7997 {
7998 process_die (child_die, cu);
7999 child_die = sibling_die (child_die);
8000 }
8001 }
8002
8003 /* Return the name of the namespace represented by DIE. Set
8004 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8005 namespace. */
8006
8007 static const char *
8008 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8009 {
8010 struct die_info *current_die;
8011 const char *name = NULL;
8012
8013 /* Loop through the extensions until we find a name. */
8014
8015 for (current_die = die;
8016 current_die != NULL;
8017 current_die = dwarf2_extension (die, &cu))
8018 {
8019 name = dwarf2_name (current_die, cu);
8020 if (name != NULL)
8021 break;
8022 }
8023
8024 /* Is it an anonymous namespace? */
8025
8026 *is_anonymous = (name == NULL);
8027 if (*is_anonymous)
8028 name = CP_ANONYMOUS_NAMESPACE_STR;
8029
8030 return name;
8031 }
8032
8033 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8034 the user defined type vector. */
8035
8036 static struct type *
8037 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8038 {
8039 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8040 struct comp_unit_head *cu_header = &cu->header;
8041 struct type *type;
8042 struct attribute *attr_byte_size;
8043 struct attribute *attr_address_class;
8044 int byte_size, addr_class;
8045 struct type *target_type;
8046
8047 target_type = die_type (die, cu);
8048
8049 /* The die_type call above may have already set the type for this DIE. */
8050 type = get_die_type (die, cu);
8051 if (type)
8052 return type;
8053
8054 type = lookup_pointer_type (target_type);
8055
8056 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8057 if (attr_byte_size)
8058 byte_size = DW_UNSND (attr_byte_size);
8059 else
8060 byte_size = cu_header->addr_size;
8061
8062 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8063 if (attr_address_class)
8064 addr_class = DW_UNSND (attr_address_class);
8065 else
8066 addr_class = DW_ADDR_none;
8067
8068 /* If the pointer size or address class is different than the
8069 default, create a type variant marked as such and set the
8070 length accordingly. */
8071 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8072 {
8073 if (gdbarch_address_class_type_flags_p (gdbarch))
8074 {
8075 int type_flags;
8076
8077 type_flags = gdbarch_address_class_type_flags
8078 (gdbarch, byte_size, addr_class);
8079 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8080 == 0);
8081 type = make_type_with_address_space (type, type_flags);
8082 }
8083 else if (TYPE_LENGTH (type) != byte_size)
8084 {
8085 complaint (&symfile_complaints,
8086 _("invalid pointer size %d"), byte_size);
8087 }
8088 else
8089 {
8090 /* Should we also complain about unhandled address classes? */
8091 }
8092 }
8093
8094 TYPE_LENGTH (type) = byte_size;
8095 return set_die_type (die, type, cu);
8096 }
8097
8098 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8099 the user defined type vector. */
8100
8101 static struct type *
8102 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8103 {
8104 struct type *type;
8105 struct type *to_type;
8106 struct type *domain;
8107
8108 to_type = die_type (die, cu);
8109 domain = die_containing_type (die, cu);
8110
8111 /* The calls above may have already set the type for this DIE. */
8112 type = get_die_type (die, cu);
8113 if (type)
8114 return type;
8115
8116 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8117 type = lookup_methodptr_type (to_type);
8118 else
8119 type = lookup_memberptr_type (to_type, domain);
8120
8121 return set_die_type (die, type, cu);
8122 }
8123
8124 /* Extract all information from a DW_TAG_reference_type DIE and add to
8125 the user defined type vector. */
8126
8127 static struct type *
8128 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8129 {
8130 struct comp_unit_head *cu_header = &cu->header;
8131 struct type *type, *target_type;
8132 struct attribute *attr;
8133
8134 target_type = die_type (die, cu);
8135
8136 /* The die_type call above may have already set the type for this DIE. */
8137 type = get_die_type (die, cu);
8138 if (type)
8139 return type;
8140
8141 type = lookup_reference_type (target_type);
8142 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8143 if (attr)
8144 {
8145 TYPE_LENGTH (type) = DW_UNSND (attr);
8146 }
8147 else
8148 {
8149 TYPE_LENGTH (type) = cu_header->addr_size;
8150 }
8151 return set_die_type (die, type, cu);
8152 }
8153
8154 static struct type *
8155 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8156 {
8157 struct type *base_type, *cv_type;
8158
8159 base_type = die_type (die, cu);
8160
8161 /* The die_type call above may have already set the type for this DIE. */
8162 cv_type = get_die_type (die, cu);
8163 if (cv_type)
8164 return cv_type;
8165
8166 /* In case the const qualifier is applied to an array type, the element type
8167 is so qualified, not the array type (section 6.7.3 of C99). */
8168 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8169 {
8170 struct type *el_type, *inner_array;
8171
8172 base_type = copy_type (base_type);
8173 inner_array = base_type;
8174
8175 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8176 {
8177 TYPE_TARGET_TYPE (inner_array) =
8178 copy_type (TYPE_TARGET_TYPE (inner_array));
8179 inner_array = TYPE_TARGET_TYPE (inner_array);
8180 }
8181
8182 el_type = TYPE_TARGET_TYPE (inner_array);
8183 TYPE_TARGET_TYPE (inner_array) =
8184 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8185
8186 return set_die_type (die, base_type, cu);
8187 }
8188
8189 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8190 return set_die_type (die, cv_type, cu);
8191 }
8192
8193 static struct type *
8194 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8195 {
8196 struct type *base_type, *cv_type;
8197
8198 base_type = die_type (die, cu);
8199
8200 /* The die_type call above may have already set the type for this DIE. */
8201 cv_type = get_die_type (die, cu);
8202 if (cv_type)
8203 return cv_type;
8204
8205 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8206 return set_die_type (die, cv_type, cu);
8207 }
8208
8209 /* Extract all information from a DW_TAG_string_type DIE and add to
8210 the user defined type vector. It isn't really a user defined type,
8211 but it behaves like one, with other DIE's using an AT_user_def_type
8212 attribute to reference it. */
8213
8214 static struct type *
8215 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8216 {
8217 struct objfile *objfile = cu->objfile;
8218 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8219 struct type *type, *range_type, *index_type, *char_type;
8220 struct attribute *attr;
8221 unsigned int length;
8222
8223 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8224 if (attr)
8225 {
8226 length = DW_UNSND (attr);
8227 }
8228 else
8229 {
8230 /* Check for the DW_AT_byte_size attribute. */
8231 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8232 if (attr)
8233 {
8234 length = DW_UNSND (attr);
8235 }
8236 else
8237 {
8238 length = 1;
8239 }
8240 }
8241
8242 index_type = objfile_type (objfile)->builtin_int;
8243 range_type = create_range_type (NULL, index_type, 1, length);
8244 char_type = language_string_char_type (cu->language_defn, gdbarch);
8245 type = create_string_type (NULL, char_type, range_type);
8246
8247 return set_die_type (die, type, cu);
8248 }
8249
8250 /* Handle DIES due to C code like:
8251
8252 struct foo
8253 {
8254 int (*funcp)(int a, long l);
8255 int b;
8256 };
8257
8258 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8259
8260 static struct type *
8261 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8262 {
8263 struct type *type; /* Type that this function returns. */
8264 struct type *ftype; /* Function that returns above type. */
8265 struct attribute *attr;
8266
8267 type = die_type (die, cu);
8268
8269 /* The die_type call above may have already set the type for this DIE. */
8270 ftype = get_die_type (die, cu);
8271 if (ftype)
8272 return ftype;
8273
8274 ftype = lookup_function_type (type);
8275
8276 /* All functions in C++, Pascal and Java have prototypes. */
8277 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8278 if ((attr && (DW_UNSND (attr) != 0))
8279 || cu->language == language_cplus
8280 || cu->language == language_java
8281 || cu->language == language_pascal)
8282 TYPE_PROTOTYPED (ftype) = 1;
8283 else if (producer_is_realview (cu->producer))
8284 /* RealView does not emit DW_AT_prototyped. We can not
8285 distinguish prototyped and unprototyped functions; default to
8286 prototyped, since that is more common in modern code (and
8287 RealView warns about unprototyped functions). */
8288 TYPE_PROTOTYPED (ftype) = 1;
8289
8290 /* Store the calling convention in the type if it's available in
8291 the subroutine die. Otherwise set the calling convention to
8292 the default value DW_CC_normal. */
8293 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8294 if (attr)
8295 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8296 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8297 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8298 else
8299 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8300
8301 /* We need to add the subroutine type to the die immediately so
8302 we don't infinitely recurse when dealing with parameters
8303 declared as the same subroutine type. */
8304 set_die_type (die, ftype, cu);
8305
8306 if (die->child != NULL)
8307 {
8308 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8309 struct die_info *child_die;
8310 int nparams, iparams;
8311
8312 /* Count the number of parameters.
8313 FIXME: GDB currently ignores vararg functions, but knows about
8314 vararg member functions. */
8315 nparams = 0;
8316 child_die = die->child;
8317 while (child_die && child_die->tag)
8318 {
8319 if (child_die->tag == DW_TAG_formal_parameter)
8320 nparams++;
8321 else if (child_die->tag == DW_TAG_unspecified_parameters)
8322 TYPE_VARARGS (ftype) = 1;
8323 child_die = sibling_die (child_die);
8324 }
8325
8326 /* Allocate storage for parameters and fill them in. */
8327 TYPE_NFIELDS (ftype) = nparams;
8328 TYPE_FIELDS (ftype) = (struct field *)
8329 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8330
8331 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8332 even if we error out during the parameters reading below. */
8333 for (iparams = 0; iparams < nparams; iparams++)
8334 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8335
8336 iparams = 0;
8337 child_die = die->child;
8338 while (child_die && child_die->tag)
8339 {
8340 if (child_die->tag == DW_TAG_formal_parameter)
8341 {
8342 struct type *arg_type;
8343
8344 /* DWARF version 2 has no clean way to discern C++
8345 static and non-static member functions. G++ helps
8346 GDB by marking the first parameter for non-static
8347 member functions (which is the this pointer) as
8348 artificial. We pass this information to
8349 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8350
8351 DWARF version 3 added DW_AT_object_pointer, which GCC
8352 4.5 does not yet generate. */
8353 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8354 if (attr)
8355 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8356 else
8357 {
8358 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8359
8360 /* GCC/43521: In java, the formal parameter
8361 "this" is sometimes not marked with DW_AT_artificial. */
8362 if (cu->language == language_java)
8363 {
8364 const char *name = dwarf2_name (child_die, cu);
8365
8366 if (name && !strcmp (name, "this"))
8367 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8368 }
8369 }
8370 arg_type = die_type (child_die, cu);
8371
8372 /* RealView does not mark THIS as const, which the testsuite
8373 expects. GCC marks THIS as const in method definitions,
8374 but not in the class specifications (GCC PR 43053). */
8375 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8376 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8377 {
8378 int is_this = 0;
8379 struct dwarf2_cu *arg_cu = cu;
8380 const char *name = dwarf2_name (child_die, cu);
8381
8382 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8383 if (attr)
8384 {
8385 /* If the compiler emits this, use it. */
8386 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8387 is_this = 1;
8388 }
8389 else if (name && strcmp (name, "this") == 0)
8390 /* Function definitions will have the argument names. */
8391 is_this = 1;
8392 else if (name == NULL && iparams == 0)
8393 /* Declarations may not have the names, so like
8394 elsewhere in GDB, assume an artificial first
8395 argument is "this". */
8396 is_this = 1;
8397
8398 if (is_this)
8399 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8400 arg_type, 0);
8401 }
8402
8403 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8404 iparams++;
8405 }
8406 child_die = sibling_die (child_die);
8407 }
8408 }
8409
8410 return ftype;
8411 }
8412
8413 static struct type *
8414 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8415 {
8416 struct objfile *objfile = cu->objfile;
8417 const char *name = NULL;
8418 struct type *this_type;
8419
8420 name = dwarf2_full_name (NULL, die, cu);
8421 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8422 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8423 TYPE_NAME (this_type) = (char *) name;
8424 set_die_type (die, this_type, cu);
8425 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8426 return this_type;
8427 }
8428
8429 /* Find a representation of a given base type and install
8430 it in the TYPE field of the die. */
8431
8432 static struct type *
8433 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8434 {
8435 struct objfile *objfile = cu->objfile;
8436 struct type *type;
8437 struct attribute *attr;
8438 int encoding = 0, size = 0;
8439 char *name;
8440 enum type_code code = TYPE_CODE_INT;
8441 int type_flags = 0;
8442 struct type *target_type = NULL;
8443
8444 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8445 if (attr)
8446 {
8447 encoding = DW_UNSND (attr);
8448 }
8449 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8450 if (attr)
8451 {
8452 size = DW_UNSND (attr);
8453 }
8454 name = dwarf2_name (die, cu);
8455 if (!name)
8456 {
8457 complaint (&symfile_complaints,
8458 _("DW_AT_name missing from DW_TAG_base_type"));
8459 }
8460
8461 switch (encoding)
8462 {
8463 case DW_ATE_address:
8464 /* Turn DW_ATE_address into a void * pointer. */
8465 code = TYPE_CODE_PTR;
8466 type_flags |= TYPE_FLAG_UNSIGNED;
8467 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8468 break;
8469 case DW_ATE_boolean:
8470 code = TYPE_CODE_BOOL;
8471 type_flags |= TYPE_FLAG_UNSIGNED;
8472 break;
8473 case DW_ATE_complex_float:
8474 code = TYPE_CODE_COMPLEX;
8475 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8476 break;
8477 case DW_ATE_decimal_float:
8478 code = TYPE_CODE_DECFLOAT;
8479 break;
8480 case DW_ATE_float:
8481 code = TYPE_CODE_FLT;
8482 break;
8483 case DW_ATE_signed:
8484 break;
8485 case DW_ATE_unsigned:
8486 type_flags |= TYPE_FLAG_UNSIGNED;
8487 if (cu->language == language_fortran
8488 && name
8489 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8490 code = TYPE_CODE_CHAR;
8491 break;
8492 case DW_ATE_signed_char:
8493 if (cu->language == language_ada || cu->language == language_m2
8494 || cu->language == language_pascal
8495 || cu->language == language_fortran)
8496 code = TYPE_CODE_CHAR;
8497 break;
8498 case DW_ATE_unsigned_char:
8499 if (cu->language == language_ada || cu->language == language_m2
8500 || cu->language == language_pascal
8501 || cu->language == language_fortran)
8502 code = TYPE_CODE_CHAR;
8503 type_flags |= TYPE_FLAG_UNSIGNED;
8504 break;
8505 case DW_ATE_UTF:
8506 /* We just treat this as an integer and then recognize the
8507 type by name elsewhere. */
8508 break;
8509
8510 default:
8511 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8512 dwarf_type_encoding_name (encoding));
8513 break;
8514 }
8515
8516 type = init_type (code, size, type_flags, NULL, objfile);
8517 TYPE_NAME (type) = name;
8518 TYPE_TARGET_TYPE (type) = target_type;
8519
8520 if (name && strcmp (name, "char") == 0)
8521 TYPE_NOSIGN (type) = 1;
8522
8523 return set_die_type (die, type, cu);
8524 }
8525
8526 /* Read the given DW_AT_subrange DIE. */
8527
8528 static struct type *
8529 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8530 {
8531 struct type *base_type;
8532 struct type *range_type;
8533 struct attribute *attr;
8534 LONGEST low = 0;
8535 LONGEST high = -1;
8536 char *name;
8537 LONGEST negative_mask;
8538
8539 base_type = die_type (die, cu);
8540 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8541 check_typedef (base_type);
8542
8543 /* The die_type call above may have already set the type for this DIE. */
8544 range_type = get_die_type (die, cu);
8545 if (range_type)
8546 return range_type;
8547
8548 if (cu->language == language_fortran)
8549 {
8550 /* FORTRAN implies a lower bound of 1, if not given. */
8551 low = 1;
8552 }
8553
8554 /* FIXME: For variable sized arrays either of these could be
8555 a variable rather than a constant value. We'll allow it,
8556 but we don't know how to handle it. */
8557 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8558 if (attr)
8559 low = dwarf2_get_attr_constant_value (attr, 0);
8560
8561 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8562 if (attr)
8563 {
8564 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8565 {
8566 /* GCC encodes arrays with unspecified or dynamic length
8567 with a DW_FORM_block1 attribute or a reference attribute.
8568 FIXME: GDB does not yet know how to handle dynamic
8569 arrays properly, treat them as arrays with unspecified
8570 length for now.
8571
8572 FIXME: jimb/2003-09-22: GDB does not really know
8573 how to handle arrays of unspecified length
8574 either; we just represent them as zero-length
8575 arrays. Choose an appropriate upper bound given
8576 the lower bound we've computed above. */
8577 high = low - 1;
8578 }
8579 else
8580 high = dwarf2_get_attr_constant_value (attr, 1);
8581 }
8582 else
8583 {
8584 attr = dwarf2_attr (die, DW_AT_count, cu);
8585 if (attr)
8586 {
8587 int count = dwarf2_get_attr_constant_value (attr, 1);
8588 high = low + count - 1;
8589 }
8590 else
8591 {
8592 /* Unspecified array length. */
8593 high = low - 1;
8594 }
8595 }
8596
8597 /* Dwarf-2 specifications explicitly allows to create subrange types
8598 without specifying a base type.
8599 In that case, the base type must be set to the type of
8600 the lower bound, upper bound or count, in that order, if any of these
8601 three attributes references an object that has a type.
8602 If no base type is found, the Dwarf-2 specifications say that
8603 a signed integer type of size equal to the size of an address should
8604 be used.
8605 For the following C code: `extern char gdb_int [];'
8606 GCC produces an empty range DIE.
8607 FIXME: muller/2010-05-28: Possible references to object for low bound,
8608 high bound or count are not yet handled by this code. */
8609 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8610 {
8611 struct objfile *objfile = cu->objfile;
8612 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8613 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8614 struct type *int_type = objfile_type (objfile)->builtin_int;
8615
8616 /* Test "int", "long int", and "long long int" objfile types,
8617 and select the first one having a size above or equal to the
8618 architecture address size. */
8619 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8620 base_type = int_type;
8621 else
8622 {
8623 int_type = objfile_type (objfile)->builtin_long;
8624 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8625 base_type = int_type;
8626 else
8627 {
8628 int_type = objfile_type (objfile)->builtin_long_long;
8629 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8630 base_type = int_type;
8631 }
8632 }
8633 }
8634
8635 negative_mask =
8636 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8637 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8638 low |= negative_mask;
8639 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8640 high |= negative_mask;
8641
8642 range_type = create_range_type (NULL, base_type, low, high);
8643
8644 /* Mark arrays with dynamic length at least as an array of unspecified
8645 length. GDB could check the boundary but before it gets implemented at
8646 least allow accessing the array elements. */
8647 if (attr && attr->form == DW_FORM_block1)
8648 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8649
8650 /* Ada expects an empty array on no boundary attributes. */
8651 if (attr == NULL && cu->language != language_ada)
8652 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8653
8654 name = dwarf2_name (die, cu);
8655 if (name)
8656 TYPE_NAME (range_type) = name;
8657
8658 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8659 if (attr)
8660 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8661
8662 set_die_type (die, range_type, cu);
8663
8664 /* set_die_type should be already done. */
8665 set_descriptive_type (range_type, die, cu);
8666
8667 return range_type;
8668 }
8669
8670 static struct type *
8671 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8672 {
8673 struct type *type;
8674
8675 /* For now, we only support the C meaning of an unspecified type: void. */
8676
8677 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8678 TYPE_NAME (type) = dwarf2_name (die, cu);
8679
8680 return set_die_type (die, type, cu);
8681 }
8682
8683 /* Trivial hash function for die_info: the hash value of a DIE
8684 is its offset in .debug_info for this objfile. */
8685
8686 static hashval_t
8687 die_hash (const void *item)
8688 {
8689 const struct die_info *die = item;
8690
8691 return die->offset;
8692 }
8693
8694 /* Trivial comparison function for die_info structures: two DIEs
8695 are equal if they have the same offset. */
8696
8697 static int
8698 die_eq (const void *item_lhs, const void *item_rhs)
8699 {
8700 const struct die_info *die_lhs = item_lhs;
8701 const struct die_info *die_rhs = item_rhs;
8702
8703 return die_lhs->offset == die_rhs->offset;
8704 }
8705
8706 /* Read a whole compilation unit into a linked list of dies. */
8707
8708 static struct die_info *
8709 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8710 {
8711 struct die_reader_specs reader_specs;
8712 int read_abbrevs = 0;
8713 struct cleanup *back_to = NULL;
8714 struct die_info *die;
8715
8716 if (cu->dwarf2_abbrevs == NULL)
8717 {
8718 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8719 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8720 read_abbrevs = 1;
8721 }
8722
8723 gdb_assert (cu->die_hash == NULL);
8724 cu->die_hash
8725 = htab_create_alloc_ex (cu->header.length / 12,
8726 die_hash,
8727 die_eq,
8728 NULL,
8729 &cu->comp_unit_obstack,
8730 hashtab_obstack_allocate,
8731 dummy_obstack_deallocate);
8732
8733 init_cu_die_reader (&reader_specs, cu);
8734
8735 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8736
8737 if (read_abbrevs)
8738 do_cleanups (back_to);
8739
8740 return die;
8741 }
8742
8743 /* Main entry point for reading a DIE and all children.
8744 Read the DIE and dump it if requested. */
8745
8746 static struct die_info *
8747 read_die_and_children (const struct die_reader_specs *reader,
8748 gdb_byte *info_ptr,
8749 gdb_byte **new_info_ptr,
8750 struct die_info *parent)
8751 {
8752 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8753 new_info_ptr, parent);
8754
8755 if (dwarf2_die_debug)
8756 {
8757 fprintf_unfiltered (gdb_stdlog,
8758 "\nRead die from %s of %s:\n",
8759 reader->buffer == dwarf2_per_objfile->info.buffer
8760 ? ".debug_info"
8761 : reader->buffer == dwarf2_per_objfile->types.buffer
8762 ? ".debug_types"
8763 : "unknown section",
8764 reader->abfd->filename);
8765 dump_die (result, dwarf2_die_debug);
8766 }
8767
8768 return result;
8769 }
8770
8771 /* Read a single die and all its descendents. Set the die's sibling
8772 field to NULL; set other fields in the die correctly, and set all
8773 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8774 location of the info_ptr after reading all of those dies. PARENT
8775 is the parent of the die in question. */
8776
8777 static struct die_info *
8778 read_die_and_children_1 (const struct die_reader_specs *reader,
8779 gdb_byte *info_ptr,
8780 gdb_byte **new_info_ptr,
8781 struct die_info *parent)
8782 {
8783 struct die_info *die;
8784 gdb_byte *cur_ptr;
8785 int has_children;
8786
8787 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8788 if (die == NULL)
8789 {
8790 *new_info_ptr = cur_ptr;
8791 return NULL;
8792 }
8793 store_in_ref_table (die, reader->cu);
8794
8795 if (has_children)
8796 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8797 else
8798 {
8799 die->child = NULL;
8800 *new_info_ptr = cur_ptr;
8801 }
8802
8803 die->sibling = NULL;
8804 die->parent = parent;
8805 return die;
8806 }
8807
8808 /* Read a die, all of its descendents, and all of its siblings; set
8809 all of the fields of all of the dies correctly. Arguments are as
8810 in read_die_and_children. */
8811
8812 static struct die_info *
8813 read_die_and_siblings (const struct die_reader_specs *reader,
8814 gdb_byte *info_ptr,
8815 gdb_byte **new_info_ptr,
8816 struct die_info *parent)
8817 {
8818 struct die_info *first_die, *last_sibling;
8819 gdb_byte *cur_ptr;
8820
8821 cur_ptr = info_ptr;
8822 first_die = last_sibling = NULL;
8823
8824 while (1)
8825 {
8826 struct die_info *die
8827 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8828
8829 if (die == NULL)
8830 {
8831 *new_info_ptr = cur_ptr;
8832 return first_die;
8833 }
8834
8835 if (!first_die)
8836 first_die = die;
8837 else
8838 last_sibling->sibling = die;
8839
8840 last_sibling = die;
8841 }
8842 }
8843
8844 /* Read the die from the .debug_info section buffer. Set DIEP to
8845 point to a newly allocated die with its information, except for its
8846 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8847 whether the die has children or not. */
8848
8849 static gdb_byte *
8850 read_full_die (const struct die_reader_specs *reader,
8851 struct die_info **diep, gdb_byte *info_ptr,
8852 int *has_children)
8853 {
8854 unsigned int abbrev_number, bytes_read, i, offset;
8855 struct abbrev_info *abbrev;
8856 struct die_info *die;
8857 struct dwarf2_cu *cu = reader->cu;
8858 bfd *abfd = reader->abfd;
8859
8860 offset = info_ptr - reader->buffer;
8861 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8862 info_ptr += bytes_read;
8863 if (!abbrev_number)
8864 {
8865 *diep = NULL;
8866 *has_children = 0;
8867 return info_ptr;
8868 }
8869
8870 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8871 if (!abbrev)
8872 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8873 abbrev_number,
8874 bfd_get_filename (abfd));
8875
8876 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8877 die->offset = offset;
8878 die->tag = abbrev->tag;
8879 die->abbrev = abbrev_number;
8880
8881 die->num_attrs = abbrev->num_attrs;
8882
8883 for (i = 0; i < abbrev->num_attrs; ++i)
8884 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8885 abfd, info_ptr, cu);
8886
8887 *diep = die;
8888 *has_children = abbrev->has_children;
8889 return info_ptr;
8890 }
8891
8892 /* In DWARF version 2, the description of the debugging information is
8893 stored in a separate .debug_abbrev section. Before we read any
8894 dies from a section we read in all abbreviations and install them
8895 in a hash table. This function also sets flags in CU describing
8896 the data found in the abbrev table. */
8897
8898 static void
8899 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8900 {
8901 struct comp_unit_head *cu_header = &cu->header;
8902 gdb_byte *abbrev_ptr;
8903 struct abbrev_info *cur_abbrev;
8904 unsigned int abbrev_number, bytes_read, abbrev_name;
8905 unsigned int abbrev_form, hash_number;
8906 struct attr_abbrev *cur_attrs;
8907 unsigned int allocated_attrs;
8908
8909 /* Initialize dwarf2 abbrevs. */
8910 obstack_init (&cu->abbrev_obstack);
8911 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8912 (ABBREV_HASH_SIZE
8913 * sizeof (struct abbrev_info *)));
8914 memset (cu->dwarf2_abbrevs, 0,
8915 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8916
8917 dwarf2_read_section (dwarf2_per_objfile->objfile,
8918 &dwarf2_per_objfile->abbrev);
8919 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8920 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8921 abbrev_ptr += bytes_read;
8922
8923 allocated_attrs = ATTR_ALLOC_CHUNK;
8924 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8925
8926 /* Loop until we reach an abbrev number of 0. */
8927 while (abbrev_number)
8928 {
8929 cur_abbrev = dwarf_alloc_abbrev (cu);
8930
8931 /* read in abbrev header */
8932 cur_abbrev->number = abbrev_number;
8933 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8934 abbrev_ptr += bytes_read;
8935 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8936 abbrev_ptr += 1;
8937
8938 if (cur_abbrev->tag == DW_TAG_namespace)
8939 cu->has_namespace_info = 1;
8940
8941 /* now read in declarations */
8942 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8943 abbrev_ptr += bytes_read;
8944 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8945 abbrev_ptr += bytes_read;
8946 while (abbrev_name)
8947 {
8948 if (cur_abbrev->num_attrs == allocated_attrs)
8949 {
8950 allocated_attrs += ATTR_ALLOC_CHUNK;
8951 cur_attrs
8952 = xrealloc (cur_attrs, (allocated_attrs
8953 * sizeof (struct attr_abbrev)));
8954 }
8955
8956 /* Record whether this compilation unit might have
8957 inter-compilation-unit references. If we don't know what form
8958 this attribute will have, then it might potentially be a
8959 DW_FORM_ref_addr, so we conservatively expect inter-CU
8960 references. */
8961
8962 if (abbrev_form == DW_FORM_ref_addr
8963 || abbrev_form == DW_FORM_indirect)
8964 cu->has_form_ref_addr = 1;
8965
8966 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8967 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8968 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8969 abbrev_ptr += bytes_read;
8970 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8971 abbrev_ptr += bytes_read;
8972 }
8973
8974 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8975 (cur_abbrev->num_attrs
8976 * sizeof (struct attr_abbrev)));
8977 memcpy (cur_abbrev->attrs, cur_attrs,
8978 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8979
8980 hash_number = abbrev_number % ABBREV_HASH_SIZE;
8981 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8982 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8983
8984 /* Get next abbreviation.
8985 Under Irix6 the abbreviations for a compilation unit are not
8986 always properly terminated with an abbrev number of 0.
8987 Exit loop if we encounter an abbreviation which we have
8988 already read (which means we are about to read the abbreviations
8989 for the next compile unit) or if the end of the abbreviation
8990 table is reached. */
8991 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8992 >= dwarf2_per_objfile->abbrev.size)
8993 break;
8994 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8995 abbrev_ptr += bytes_read;
8996 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8997 break;
8998 }
8999
9000 xfree (cur_attrs);
9001 }
9002
9003 /* Release the memory used by the abbrev table for a compilation unit. */
9004
9005 static void
9006 dwarf2_free_abbrev_table (void *ptr_to_cu)
9007 {
9008 struct dwarf2_cu *cu = ptr_to_cu;
9009
9010 obstack_free (&cu->abbrev_obstack, NULL);
9011 cu->dwarf2_abbrevs = NULL;
9012 }
9013
9014 /* Lookup an abbrev_info structure in the abbrev hash table. */
9015
9016 static struct abbrev_info *
9017 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9018 {
9019 unsigned int hash_number;
9020 struct abbrev_info *abbrev;
9021
9022 hash_number = number % ABBREV_HASH_SIZE;
9023 abbrev = cu->dwarf2_abbrevs[hash_number];
9024
9025 while (abbrev)
9026 {
9027 if (abbrev->number == number)
9028 return abbrev;
9029 else
9030 abbrev = abbrev->next;
9031 }
9032 return NULL;
9033 }
9034
9035 /* Returns nonzero if TAG represents a type that we might generate a partial
9036 symbol for. */
9037
9038 static int
9039 is_type_tag_for_partial (int tag)
9040 {
9041 switch (tag)
9042 {
9043 #if 0
9044 /* Some types that would be reasonable to generate partial symbols for,
9045 that we don't at present. */
9046 case DW_TAG_array_type:
9047 case DW_TAG_file_type:
9048 case DW_TAG_ptr_to_member_type:
9049 case DW_TAG_set_type:
9050 case DW_TAG_string_type:
9051 case DW_TAG_subroutine_type:
9052 #endif
9053 case DW_TAG_base_type:
9054 case DW_TAG_class_type:
9055 case DW_TAG_interface_type:
9056 case DW_TAG_enumeration_type:
9057 case DW_TAG_structure_type:
9058 case DW_TAG_subrange_type:
9059 case DW_TAG_typedef:
9060 case DW_TAG_union_type:
9061 return 1;
9062 default:
9063 return 0;
9064 }
9065 }
9066
9067 /* Load all DIEs that are interesting for partial symbols into memory. */
9068
9069 static struct partial_die_info *
9070 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9071 int building_psymtab, struct dwarf2_cu *cu)
9072 {
9073 struct partial_die_info *part_die;
9074 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9075 struct abbrev_info *abbrev;
9076 unsigned int bytes_read;
9077 unsigned int load_all = 0;
9078
9079 int nesting_level = 1;
9080
9081 parent_die = NULL;
9082 last_die = NULL;
9083
9084 if (cu->per_cu && cu->per_cu->load_all_dies)
9085 load_all = 1;
9086
9087 cu->partial_dies
9088 = htab_create_alloc_ex (cu->header.length / 12,
9089 partial_die_hash,
9090 partial_die_eq,
9091 NULL,
9092 &cu->comp_unit_obstack,
9093 hashtab_obstack_allocate,
9094 dummy_obstack_deallocate);
9095
9096 part_die = obstack_alloc (&cu->comp_unit_obstack,
9097 sizeof (struct partial_die_info));
9098
9099 while (1)
9100 {
9101 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9102
9103 /* A NULL abbrev means the end of a series of children. */
9104 if (abbrev == NULL)
9105 {
9106 if (--nesting_level == 0)
9107 {
9108 /* PART_DIE was probably the last thing allocated on the
9109 comp_unit_obstack, so we could call obstack_free
9110 here. We don't do that because the waste is small,
9111 and will be cleaned up when we're done with this
9112 compilation unit. This way, we're also more robust
9113 against other users of the comp_unit_obstack. */
9114 return first_die;
9115 }
9116 info_ptr += bytes_read;
9117 last_die = parent_die;
9118 parent_die = parent_die->die_parent;
9119 continue;
9120 }
9121
9122 /* Check for template arguments. We never save these; if
9123 they're seen, we just mark the parent, and go on our way. */
9124 if (parent_die != NULL
9125 && cu->language == language_cplus
9126 && (abbrev->tag == DW_TAG_template_type_param
9127 || abbrev->tag == DW_TAG_template_value_param))
9128 {
9129 parent_die->has_template_arguments = 1;
9130
9131 if (!load_all)
9132 {
9133 /* We don't need a partial DIE for the template argument. */
9134 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9135 cu);
9136 continue;
9137 }
9138 }
9139
9140 /* We only recurse into subprograms looking for template arguments.
9141 Skip their other children. */
9142 if (!load_all
9143 && cu->language == language_cplus
9144 && parent_die != NULL
9145 && parent_die->tag == DW_TAG_subprogram)
9146 {
9147 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9148 continue;
9149 }
9150
9151 /* Check whether this DIE is interesting enough to save. Normally
9152 we would not be interested in members here, but there may be
9153 later variables referencing them via DW_AT_specification (for
9154 static members). */
9155 if (!load_all
9156 && !is_type_tag_for_partial (abbrev->tag)
9157 && abbrev->tag != DW_TAG_constant
9158 && abbrev->tag != DW_TAG_enumerator
9159 && abbrev->tag != DW_TAG_subprogram
9160 && abbrev->tag != DW_TAG_lexical_block
9161 && abbrev->tag != DW_TAG_variable
9162 && abbrev->tag != DW_TAG_namespace
9163 && abbrev->tag != DW_TAG_module
9164 && abbrev->tag != DW_TAG_member)
9165 {
9166 /* Otherwise we skip to the next sibling, if any. */
9167 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9168 continue;
9169 }
9170
9171 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9172 buffer, info_ptr, cu);
9173
9174 /* This two-pass algorithm for processing partial symbols has a
9175 high cost in cache pressure. Thus, handle some simple cases
9176 here which cover the majority of C partial symbols. DIEs
9177 which neither have specification tags in them, nor could have
9178 specification tags elsewhere pointing at them, can simply be
9179 processed and discarded.
9180
9181 This segment is also optional; scan_partial_symbols and
9182 add_partial_symbol will handle these DIEs if we chain
9183 them in normally. When compilers which do not emit large
9184 quantities of duplicate debug information are more common,
9185 this code can probably be removed. */
9186
9187 /* Any complete simple types at the top level (pretty much all
9188 of them, for a language without namespaces), can be processed
9189 directly. */
9190 if (parent_die == NULL
9191 && part_die->has_specification == 0
9192 && part_die->is_declaration == 0
9193 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9194 || part_die->tag == DW_TAG_base_type
9195 || part_die->tag == DW_TAG_subrange_type))
9196 {
9197 if (building_psymtab && part_die->name != NULL)
9198 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9199 VAR_DOMAIN, LOC_TYPEDEF,
9200 &cu->objfile->static_psymbols,
9201 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9202 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9203 continue;
9204 }
9205
9206 /* The exception for DW_TAG_typedef with has_children above is
9207 a workaround of GCC PR debug/47510. In the case of this complaint
9208 type_name_no_tag_or_error will error on such types later.
9209
9210 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9211 it could not find the child DIEs referenced later, this is checked
9212 above. In correct DWARF DW_TAG_typedef should have no children. */
9213
9214 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9215 complaint (&symfile_complaints,
9216 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9217 "- DIE at 0x%x [in module %s]"),
9218 part_die->offset, cu->objfile->name);
9219
9220 /* If we're at the second level, and we're an enumerator, and
9221 our parent has no specification (meaning possibly lives in a
9222 namespace elsewhere), then we can add the partial symbol now
9223 instead of queueing it. */
9224 if (part_die->tag == DW_TAG_enumerator
9225 && parent_die != NULL
9226 && parent_die->die_parent == NULL
9227 && parent_die->tag == DW_TAG_enumeration_type
9228 && parent_die->has_specification == 0)
9229 {
9230 if (part_die->name == NULL)
9231 complaint (&symfile_complaints,
9232 _("malformed enumerator DIE ignored"));
9233 else if (building_psymtab)
9234 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9235 VAR_DOMAIN, LOC_CONST,
9236 (cu->language == language_cplus
9237 || cu->language == language_java)
9238 ? &cu->objfile->global_psymbols
9239 : &cu->objfile->static_psymbols,
9240 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9241
9242 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9243 continue;
9244 }
9245
9246 /* We'll save this DIE so link it in. */
9247 part_die->die_parent = parent_die;
9248 part_die->die_sibling = NULL;
9249 part_die->die_child = NULL;
9250
9251 if (last_die && last_die == parent_die)
9252 last_die->die_child = part_die;
9253 else if (last_die)
9254 last_die->die_sibling = part_die;
9255
9256 last_die = part_die;
9257
9258 if (first_die == NULL)
9259 first_die = part_die;
9260
9261 /* Maybe add the DIE to the hash table. Not all DIEs that we
9262 find interesting need to be in the hash table, because we
9263 also have the parent/sibling/child chains; only those that we
9264 might refer to by offset later during partial symbol reading.
9265
9266 For now this means things that might have be the target of a
9267 DW_AT_specification, DW_AT_abstract_origin, or
9268 DW_AT_extension. DW_AT_extension will refer only to
9269 namespaces; DW_AT_abstract_origin refers to functions (and
9270 many things under the function DIE, but we do not recurse
9271 into function DIEs during partial symbol reading) and
9272 possibly variables as well; DW_AT_specification refers to
9273 declarations. Declarations ought to have the DW_AT_declaration
9274 flag. It happens that GCC forgets to put it in sometimes, but
9275 only for functions, not for types.
9276
9277 Adding more things than necessary to the hash table is harmless
9278 except for the performance cost. Adding too few will result in
9279 wasted time in find_partial_die, when we reread the compilation
9280 unit with load_all_dies set. */
9281
9282 if (load_all
9283 || abbrev->tag == DW_TAG_constant
9284 || abbrev->tag == DW_TAG_subprogram
9285 || abbrev->tag == DW_TAG_variable
9286 || abbrev->tag == DW_TAG_namespace
9287 || part_die->is_declaration)
9288 {
9289 void **slot;
9290
9291 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9292 part_die->offset, INSERT);
9293 *slot = part_die;
9294 }
9295
9296 part_die = obstack_alloc (&cu->comp_unit_obstack,
9297 sizeof (struct partial_die_info));
9298
9299 /* For some DIEs we want to follow their children (if any). For C
9300 we have no reason to follow the children of structures; for other
9301 languages we have to, so that we can get at method physnames
9302 to infer fully qualified class names, for DW_AT_specification,
9303 and for C++ template arguments. For C++, we also look one level
9304 inside functions to find template arguments (if the name of the
9305 function does not already contain the template arguments).
9306
9307 For Ada, we need to scan the children of subprograms and lexical
9308 blocks as well because Ada allows the definition of nested
9309 entities that could be interesting for the debugger, such as
9310 nested subprograms for instance. */
9311 if (last_die->has_children
9312 && (load_all
9313 || last_die->tag == DW_TAG_namespace
9314 || last_die->tag == DW_TAG_module
9315 || last_die->tag == DW_TAG_enumeration_type
9316 || (cu->language == language_cplus
9317 && last_die->tag == DW_TAG_subprogram
9318 && (last_die->name == NULL
9319 || strchr (last_die->name, '<') == NULL))
9320 || (cu->language != language_c
9321 && (last_die->tag == DW_TAG_class_type
9322 || last_die->tag == DW_TAG_interface_type
9323 || last_die->tag == DW_TAG_structure_type
9324 || last_die->tag == DW_TAG_union_type))
9325 || (cu->language == language_ada
9326 && (last_die->tag == DW_TAG_subprogram
9327 || last_die->tag == DW_TAG_lexical_block))))
9328 {
9329 nesting_level++;
9330 parent_die = last_die;
9331 continue;
9332 }
9333
9334 /* Otherwise we skip to the next sibling, if any. */
9335 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9336
9337 /* Back to the top, do it again. */
9338 }
9339 }
9340
9341 /* Read a minimal amount of information into the minimal die structure. */
9342
9343 static gdb_byte *
9344 read_partial_die (struct partial_die_info *part_die,
9345 struct abbrev_info *abbrev,
9346 unsigned int abbrev_len, bfd *abfd,
9347 gdb_byte *buffer, gdb_byte *info_ptr,
9348 struct dwarf2_cu *cu)
9349 {
9350 unsigned int i;
9351 struct attribute attr;
9352 int has_low_pc_attr = 0;
9353 int has_high_pc_attr = 0;
9354
9355 memset (part_die, 0, sizeof (struct partial_die_info));
9356
9357 part_die->offset = info_ptr - buffer;
9358
9359 info_ptr += abbrev_len;
9360
9361 if (abbrev == NULL)
9362 return info_ptr;
9363
9364 part_die->tag = abbrev->tag;
9365 part_die->has_children = abbrev->has_children;
9366
9367 for (i = 0; i < abbrev->num_attrs; ++i)
9368 {
9369 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9370
9371 /* Store the data if it is of an attribute we want to keep in a
9372 partial symbol table. */
9373 switch (attr.name)
9374 {
9375 case DW_AT_name:
9376 switch (part_die->tag)
9377 {
9378 case DW_TAG_compile_unit:
9379 case DW_TAG_type_unit:
9380 /* Compilation units have a DW_AT_name that is a filename, not
9381 a source language identifier. */
9382 case DW_TAG_enumeration_type:
9383 case DW_TAG_enumerator:
9384 /* These tags always have simple identifiers already; no need
9385 to canonicalize them. */
9386 part_die->name = DW_STRING (&attr);
9387 break;
9388 default:
9389 part_die->name
9390 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9391 &cu->objfile->objfile_obstack);
9392 break;
9393 }
9394 break;
9395 case DW_AT_linkage_name:
9396 case DW_AT_MIPS_linkage_name:
9397 /* Note that both forms of linkage name might appear. We
9398 assume they will be the same, and we only store the last
9399 one we see. */
9400 if (cu->language == language_ada)
9401 part_die->name = DW_STRING (&attr);
9402 part_die->linkage_name = DW_STRING (&attr);
9403 break;
9404 case DW_AT_low_pc:
9405 has_low_pc_attr = 1;
9406 part_die->lowpc = DW_ADDR (&attr);
9407 break;
9408 case DW_AT_high_pc:
9409 has_high_pc_attr = 1;
9410 part_die->highpc = DW_ADDR (&attr);
9411 break;
9412 case DW_AT_location:
9413 /* Support the .debug_loc offsets. */
9414 if (attr_form_is_block (&attr))
9415 {
9416 part_die->locdesc = DW_BLOCK (&attr);
9417 }
9418 else if (attr_form_is_section_offset (&attr))
9419 {
9420 dwarf2_complex_location_expr_complaint ();
9421 }
9422 else
9423 {
9424 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9425 "partial symbol information");
9426 }
9427 break;
9428 case DW_AT_external:
9429 part_die->is_external = DW_UNSND (&attr);
9430 break;
9431 case DW_AT_declaration:
9432 part_die->is_declaration = DW_UNSND (&attr);
9433 break;
9434 case DW_AT_type:
9435 part_die->has_type = 1;
9436 break;
9437 case DW_AT_abstract_origin:
9438 case DW_AT_specification:
9439 case DW_AT_extension:
9440 part_die->has_specification = 1;
9441 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9442 break;
9443 case DW_AT_sibling:
9444 /* Ignore absolute siblings, they might point outside of
9445 the current compile unit. */
9446 if (attr.form == DW_FORM_ref_addr)
9447 complaint (&symfile_complaints,
9448 _("ignoring absolute DW_AT_sibling"));
9449 else
9450 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9451 break;
9452 case DW_AT_byte_size:
9453 part_die->has_byte_size = 1;
9454 break;
9455 case DW_AT_calling_convention:
9456 /* DWARF doesn't provide a way to identify a program's source-level
9457 entry point. DW_AT_calling_convention attributes are only meant
9458 to describe functions' calling conventions.
9459
9460 However, because it's a necessary piece of information in
9461 Fortran, and because DW_CC_program is the only piece of debugging
9462 information whose definition refers to a 'main program' at all,
9463 several compilers have begun marking Fortran main programs with
9464 DW_CC_program --- even when those functions use the standard
9465 calling conventions.
9466
9467 So until DWARF specifies a way to provide this information and
9468 compilers pick up the new representation, we'll support this
9469 practice. */
9470 if (DW_UNSND (&attr) == DW_CC_program
9471 && cu->language == language_fortran)
9472 {
9473 set_main_name (part_die->name);
9474
9475 /* As this DIE has a static linkage the name would be difficult
9476 to look up later. */
9477 language_of_main = language_fortran;
9478 }
9479 break;
9480 default:
9481 break;
9482 }
9483 }
9484
9485 if (has_low_pc_attr && has_high_pc_attr)
9486 {
9487 /* When using the GNU linker, .gnu.linkonce. sections are used to
9488 eliminate duplicate copies of functions and vtables and such.
9489 The linker will arbitrarily choose one and discard the others.
9490 The AT_*_pc values for such functions refer to local labels in
9491 these sections. If the section from that file was discarded, the
9492 labels are not in the output, so the relocs get a value of 0.
9493 If this is a discarded function, mark the pc bounds as invalid,
9494 so that GDB will ignore it. */
9495 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9496 {
9497 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9498
9499 complaint (&symfile_complaints,
9500 _("DW_AT_low_pc %s is zero "
9501 "for DIE at 0x%x [in module %s]"),
9502 paddress (gdbarch, part_die->lowpc),
9503 part_die->offset, cu->objfile->name);
9504 }
9505 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9506 else if (part_die->lowpc >= part_die->highpc)
9507 {
9508 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9509
9510 complaint (&symfile_complaints,
9511 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9512 "for DIE at 0x%x [in module %s]"),
9513 paddress (gdbarch, part_die->lowpc),
9514 paddress (gdbarch, part_die->highpc),
9515 part_die->offset, cu->objfile->name);
9516 }
9517 else
9518 part_die->has_pc_info = 1;
9519 }
9520
9521 return info_ptr;
9522 }
9523
9524 /* Find a cached partial DIE at OFFSET in CU. */
9525
9526 static struct partial_die_info *
9527 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9528 {
9529 struct partial_die_info *lookup_die = NULL;
9530 struct partial_die_info part_die;
9531
9532 part_die.offset = offset;
9533 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9534
9535 return lookup_die;
9536 }
9537
9538 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9539 except in the case of .debug_types DIEs which do not reference
9540 outside their CU (they do however referencing other types via
9541 DW_FORM_ref_sig8). */
9542
9543 static struct partial_die_info *
9544 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9545 {
9546 struct dwarf2_per_cu_data *per_cu = NULL;
9547 struct partial_die_info *pd = NULL;
9548
9549 if (cu->per_cu->from_debug_types)
9550 {
9551 pd = find_partial_die_in_comp_unit (offset, cu);
9552 if (pd != NULL)
9553 return pd;
9554 goto not_found;
9555 }
9556
9557 if (offset_in_cu_p (&cu->header, offset))
9558 {
9559 pd = find_partial_die_in_comp_unit (offset, cu);
9560 if (pd != NULL)
9561 return pd;
9562 }
9563
9564 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9565
9566 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9567 load_partial_comp_unit (per_cu, cu->objfile);
9568
9569 per_cu->cu->last_used = 0;
9570 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9571
9572 if (pd == NULL && per_cu->load_all_dies == 0)
9573 {
9574 struct cleanup *back_to;
9575 struct partial_die_info comp_unit_die;
9576 struct abbrev_info *abbrev;
9577 unsigned int bytes_read;
9578 char *info_ptr;
9579
9580 per_cu->load_all_dies = 1;
9581
9582 /* Re-read the DIEs. */
9583 back_to = make_cleanup (null_cleanup, 0);
9584 if (per_cu->cu->dwarf2_abbrevs == NULL)
9585 {
9586 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9587 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9588 }
9589 info_ptr = (dwarf2_per_objfile->info.buffer
9590 + per_cu->cu->header.offset
9591 + per_cu->cu->header.first_die_offset);
9592 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9593 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9594 per_cu->cu->objfile->obfd,
9595 dwarf2_per_objfile->info.buffer, info_ptr,
9596 per_cu->cu);
9597 if (comp_unit_die.has_children)
9598 load_partial_dies (per_cu->cu->objfile->obfd,
9599 dwarf2_per_objfile->info.buffer, info_ptr,
9600 0, per_cu->cu);
9601 do_cleanups (back_to);
9602
9603 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9604 }
9605
9606 not_found:
9607
9608 if (pd == NULL)
9609 internal_error (__FILE__, __LINE__,
9610 _("could not find partial DIE 0x%x "
9611 "in cache [from module %s]\n"),
9612 offset, bfd_get_filename (cu->objfile->obfd));
9613 return pd;
9614 }
9615
9616 /* See if we can figure out if the class lives in a namespace. We do
9617 this by looking for a member function; its demangled name will
9618 contain namespace info, if there is any. */
9619
9620 static void
9621 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9622 struct dwarf2_cu *cu)
9623 {
9624 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9625 what template types look like, because the demangler
9626 frequently doesn't give the same name as the debug info. We
9627 could fix this by only using the demangled name to get the
9628 prefix (but see comment in read_structure_type). */
9629
9630 struct partial_die_info *real_pdi;
9631 struct partial_die_info *child_pdi;
9632
9633 /* If this DIE (this DIE's specification, if any) has a parent, then
9634 we should not do this. We'll prepend the parent's fully qualified
9635 name when we create the partial symbol. */
9636
9637 real_pdi = struct_pdi;
9638 while (real_pdi->has_specification)
9639 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9640
9641 if (real_pdi->die_parent != NULL)
9642 return;
9643
9644 for (child_pdi = struct_pdi->die_child;
9645 child_pdi != NULL;
9646 child_pdi = child_pdi->die_sibling)
9647 {
9648 if (child_pdi->tag == DW_TAG_subprogram
9649 && child_pdi->linkage_name != NULL)
9650 {
9651 char *actual_class_name
9652 = language_class_name_from_physname (cu->language_defn,
9653 child_pdi->linkage_name);
9654 if (actual_class_name != NULL)
9655 {
9656 struct_pdi->name
9657 = obsavestring (actual_class_name,
9658 strlen (actual_class_name),
9659 &cu->objfile->objfile_obstack);
9660 xfree (actual_class_name);
9661 }
9662 break;
9663 }
9664 }
9665 }
9666
9667 /* Adjust PART_DIE before generating a symbol for it. This function
9668 may set the is_external flag or change the DIE's name. */
9669
9670 static void
9671 fixup_partial_die (struct partial_die_info *part_die,
9672 struct dwarf2_cu *cu)
9673 {
9674 /* Once we've fixed up a die, there's no point in doing so again.
9675 This also avoids a memory leak if we were to call
9676 guess_partial_die_structure_name multiple times. */
9677 if (part_die->fixup_called)
9678 return;
9679
9680 /* If we found a reference attribute and the DIE has no name, try
9681 to find a name in the referred to DIE. */
9682
9683 if (part_die->name == NULL && part_die->has_specification)
9684 {
9685 struct partial_die_info *spec_die;
9686
9687 spec_die = find_partial_die (part_die->spec_offset, cu);
9688
9689 fixup_partial_die (spec_die, cu);
9690
9691 if (spec_die->name)
9692 {
9693 part_die->name = spec_die->name;
9694
9695 /* Copy DW_AT_external attribute if it is set. */
9696 if (spec_die->is_external)
9697 part_die->is_external = spec_die->is_external;
9698 }
9699 }
9700
9701 /* Set default names for some unnamed DIEs. */
9702
9703 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9704 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
9705
9706 /* If there is no parent die to provide a namespace, and there are
9707 children, see if we can determine the namespace from their linkage
9708 name.
9709 NOTE: We need to do this even if cu->has_namespace_info != 0.
9710 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9711 if (cu->language == language_cplus
9712 && dwarf2_per_objfile->types.asection != NULL
9713 && part_die->die_parent == NULL
9714 && part_die->has_children
9715 && (part_die->tag == DW_TAG_class_type
9716 || part_die->tag == DW_TAG_structure_type
9717 || part_die->tag == DW_TAG_union_type))
9718 guess_partial_die_structure_name (part_die, cu);
9719
9720 /* GCC might emit a nameless struct or union that has a linkage
9721 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9722 if (part_die->name == NULL
9723 && (part_die->tag == DW_TAG_structure_type
9724 || part_die->tag == DW_TAG_union_type
9725 || part_die->tag == DW_TAG_class_type)
9726 && part_die->linkage_name != NULL)
9727 {
9728 char *demangled;
9729
9730 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9731 if (demangled)
9732 {
9733 part_die->name = obsavestring (demangled, strlen (demangled),
9734 &cu->objfile->objfile_obstack);
9735 xfree (demangled);
9736 }
9737 }
9738
9739 part_die->fixup_called = 1;
9740 }
9741
9742 /* Read an attribute value described by an attribute form. */
9743
9744 static gdb_byte *
9745 read_attribute_value (struct attribute *attr, unsigned form,
9746 bfd *abfd, gdb_byte *info_ptr,
9747 struct dwarf2_cu *cu)
9748 {
9749 struct comp_unit_head *cu_header = &cu->header;
9750 unsigned int bytes_read;
9751 struct dwarf_block *blk;
9752
9753 attr->form = form;
9754 switch (form)
9755 {
9756 case DW_FORM_ref_addr:
9757 if (cu->header.version == 2)
9758 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9759 else
9760 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9761 &cu->header, &bytes_read);
9762 info_ptr += bytes_read;
9763 break;
9764 case DW_FORM_addr:
9765 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9766 info_ptr += bytes_read;
9767 break;
9768 case DW_FORM_block2:
9769 blk = dwarf_alloc_block (cu);
9770 blk->size = read_2_bytes (abfd, info_ptr);
9771 info_ptr += 2;
9772 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9773 info_ptr += blk->size;
9774 DW_BLOCK (attr) = blk;
9775 break;
9776 case DW_FORM_block4:
9777 blk = dwarf_alloc_block (cu);
9778 blk->size = read_4_bytes (abfd, info_ptr);
9779 info_ptr += 4;
9780 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9781 info_ptr += blk->size;
9782 DW_BLOCK (attr) = blk;
9783 break;
9784 case DW_FORM_data2:
9785 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9786 info_ptr += 2;
9787 break;
9788 case DW_FORM_data4:
9789 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9790 info_ptr += 4;
9791 break;
9792 case DW_FORM_data8:
9793 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9794 info_ptr += 8;
9795 break;
9796 case DW_FORM_sec_offset:
9797 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9798 info_ptr += bytes_read;
9799 break;
9800 case DW_FORM_string:
9801 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9802 DW_STRING_IS_CANONICAL (attr) = 0;
9803 info_ptr += bytes_read;
9804 break;
9805 case DW_FORM_strp:
9806 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9807 &bytes_read);
9808 DW_STRING_IS_CANONICAL (attr) = 0;
9809 info_ptr += bytes_read;
9810 break;
9811 case DW_FORM_exprloc:
9812 case DW_FORM_block:
9813 blk = dwarf_alloc_block (cu);
9814 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9815 info_ptr += bytes_read;
9816 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9817 info_ptr += blk->size;
9818 DW_BLOCK (attr) = blk;
9819 break;
9820 case DW_FORM_block1:
9821 blk = dwarf_alloc_block (cu);
9822 blk->size = read_1_byte (abfd, info_ptr);
9823 info_ptr += 1;
9824 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9825 info_ptr += blk->size;
9826 DW_BLOCK (attr) = blk;
9827 break;
9828 case DW_FORM_data1:
9829 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9830 info_ptr += 1;
9831 break;
9832 case DW_FORM_flag:
9833 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9834 info_ptr += 1;
9835 break;
9836 case DW_FORM_flag_present:
9837 DW_UNSND (attr) = 1;
9838 break;
9839 case DW_FORM_sdata:
9840 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9841 info_ptr += bytes_read;
9842 break;
9843 case DW_FORM_udata:
9844 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9845 info_ptr += bytes_read;
9846 break;
9847 case DW_FORM_ref1:
9848 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9849 info_ptr += 1;
9850 break;
9851 case DW_FORM_ref2:
9852 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9853 info_ptr += 2;
9854 break;
9855 case DW_FORM_ref4:
9856 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9857 info_ptr += 4;
9858 break;
9859 case DW_FORM_ref8:
9860 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9861 info_ptr += 8;
9862 break;
9863 case DW_FORM_ref_sig8:
9864 /* Convert the signature to something we can record in DW_UNSND
9865 for later lookup.
9866 NOTE: This is NULL if the type wasn't found. */
9867 DW_SIGNATURED_TYPE (attr) =
9868 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9869 info_ptr += 8;
9870 break;
9871 case DW_FORM_ref_udata:
9872 DW_ADDR (attr) = (cu->header.offset
9873 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9874 info_ptr += bytes_read;
9875 break;
9876 case DW_FORM_indirect:
9877 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9878 info_ptr += bytes_read;
9879 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9880 break;
9881 default:
9882 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9883 dwarf_form_name (form),
9884 bfd_get_filename (abfd));
9885 }
9886
9887 /* We have seen instances where the compiler tried to emit a byte
9888 size attribute of -1 which ended up being encoded as an unsigned
9889 0xffffffff. Although 0xffffffff is technically a valid size value,
9890 an object of this size seems pretty unlikely so we can relatively
9891 safely treat these cases as if the size attribute was invalid and
9892 treat them as zero by default. */
9893 if (attr->name == DW_AT_byte_size
9894 && form == DW_FORM_data4
9895 && DW_UNSND (attr) >= 0xffffffff)
9896 {
9897 complaint
9898 (&symfile_complaints,
9899 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9900 hex_string (DW_UNSND (attr)));
9901 DW_UNSND (attr) = 0;
9902 }
9903
9904 return info_ptr;
9905 }
9906
9907 /* Read an attribute described by an abbreviated attribute. */
9908
9909 static gdb_byte *
9910 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9911 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9912 {
9913 attr->name = abbrev->name;
9914 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9915 }
9916
9917 /* Read dwarf information from a buffer. */
9918
9919 static unsigned int
9920 read_1_byte (bfd *abfd, gdb_byte *buf)
9921 {
9922 return bfd_get_8 (abfd, buf);
9923 }
9924
9925 static int
9926 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9927 {
9928 return bfd_get_signed_8 (abfd, buf);
9929 }
9930
9931 static unsigned int
9932 read_2_bytes (bfd *abfd, gdb_byte *buf)
9933 {
9934 return bfd_get_16 (abfd, buf);
9935 }
9936
9937 static int
9938 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9939 {
9940 return bfd_get_signed_16 (abfd, buf);
9941 }
9942
9943 static unsigned int
9944 read_4_bytes (bfd *abfd, gdb_byte *buf)
9945 {
9946 return bfd_get_32 (abfd, buf);
9947 }
9948
9949 static int
9950 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9951 {
9952 return bfd_get_signed_32 (abfd, buf);
9953 }
9954
9955 static ULONGEST
9956 read_8_bytes (bfd *abfd, gdb_byte *buf)
9957 {
9958 return bfd_get_64 (abfd, buf);
9959 }
9960
9961 static CORE_ADDR
9962 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9963 unsigned int *bytes_read)
9964 {
9965 struct comp_unit_head *cu_header = &cu->header;
9966 CORE_ADDR retval = 0;
9967
9968 if (cu_header->signed_addr_p)
9969 {
9970 switch (cu_header->addr_size)
9971 {
9972 case 2:
9973 retval = bfd_get_signed_16 (abfd, buf);
9974 break;
9975 case 4:
9976 retval = bfd_get_signed_32 (abfd, buf);
9977 break;
9978 case 8:
9979 retval = bfd_get_signed_64 (abfd, buf);
9980 break;
9981 default:
9982 internal_error (__FILE__, __LINE__,
9983 _("read_address: bad switch, signed [in module %s]"),
9984 bfd_get_filename (abfd));
9985 }
9986 }
9987 else
9988 {
9989 switch (cu_header->addr_size)
9990 {
9991 case 2:
9992 retval = bfd_get_16 (abfd, buf);
9993 break;
9994 case 4:
9995 retval = bfd_get_32 (abfd, buf);
9996 break;
9997 case 8:
9998 retval = bfd_get_64 (abfd, buf);
9999 break;
10000 default:
10001 internal_error (__FILE__, __LINE__,
10002 _("read_address: bad switch, "
10003 "unsigned [in module %s]"),
10004 bfd_get_filename (abfd));
10005 }
10006 }
10007
10008 *bytes_read = cu_header->addr_size;
10009 return retval;
10010 }
10011
10012 /* Read the initial length from a section. The (draft) DWARF 3
10013 specification allows the initial length to take up either 4 bytes
10014 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10015 bytes describe the length and all offsets will be 8 bytes in length
10016 instead of 4.
10017
10018 An older, non-standard 64-bit format is also handled by this
10019 function. The older format in question stores the initial length
10020 as an 8-byte quantity without an escape value. Lengths greater
10021 than 2^32 aren't very common which means that the initial 4 bytes
10022 is almost always zero. Since a length value of zero doesn't make
10023 sense for the 32-bit format, this initial zero can be considered to
10024 be an escape value which indicates the presence of the older 64-bit
10025 format. As written, the code can't detect (old format) lengths
10026 greater than 4GB. If it becomes necessary to handle lengths
10027 somewhat larger than 4GB, we could allow other small values (such
10028 as the non-sensical values of 1, 2, and 3) to also be used as
10029 escape values indicating the presence of the old format.
10030
10031 The value returned via bytes_read should be used to increment the
10032 relevant pointer after calling read_initial_length().
10033
10034 [ Note: read_initial_length() and read_offset() are based on the
10035 document entitled "DWARF Debugging Information Format", revision
10036 3, draft 8, dated November 19, 2001. This document was obtained
10037 from:
10038
10039 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10040
10041 This document is only a draft and is subject to change. (So beware.)
10042
10043 Details regarding the older, non-standard 64-bit format were
10044 determined empirically by examining 64-bit ELF files produced by
10045 the SGI toolchain on an IRIX 6.5 machine.
10046
10047 - Kevin, July 16, 2002
10048 ] */
10049
10050 static LONGEST
10051 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10052 {
10053 LONGEST length = bfd_get_32 (abfd, buf);
10054
10055 if (length == 0xffffffff)
10056 {
10057 length = bfd_get_64 (abfd, buf + 4);
10058 *bytes_read = 12;
10059 }
10060 else if (length == 0)
10061 {
10062 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10063 length = bfd_get_64 (abfd, buf);
10064 *bytes_read = 8;
10065 }
10066 else
10067 {
10068 *bytes_read = 4;
10069 }
10070
10071 return length;
10072 }
10073
10074 /* Cover function for read_initial_length.
10075 Returns the length of the object at BUF, and stores the size of the
10076 initial length in *BYTES_READ and stores the size that offsets will be in
10077 *OFFSET_SIZE.
10078 If the initial length size is not equivalent to that specified in
10079 CU_HEADER then issue a complaint.
10080 This is useful when reading non-comp-unit headers. */
10081
10082 static LONGEST
10083 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10084 const struct comp_unit_head *cu_header,
10085 unsigned int *bytes_read,
10086 unsigned int *offset_size)
10087 {
10088 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10089
10090 gdb_assert (cu_header->initial_length_size == 4
10091 || cu_header->initial_length_size == 8
10092 || cu_header->initial_length_size == 12);
10093
10094 if (cu_header->initial_length_size != *bytes_read)
10095 complaint (&symfile_complaints,
10096 _("intermixed 32-bit and 64-bit DWARF sections"));
10097
10098 *offset_size = (*bytes_read == 4) ? 4 : 8;
10099 return length;
10100 }
10101
10102 /* Read an offset from the data stream. The size of the offset is
10103 given by cu_header->offset_size. */
10104
10105 static LONGEST
10106 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10107 unsigned int *bytes_read)
10108 {
10109 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10110
10111 *bytes_read = cu_header->offset_size;
10112 return offset;
10113 }
10114
10115 /* Read an offset from the data stream. */
10116
10117 static LONGEST
10118 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10119 {
10120 LONGEST retval = 0;
10121
10122 switch (offset_size)
10123 {
10124 case 4:
10125 retval = bfd_get_32 (abfd, buf);
10126 break;
10127 case 8:
10128 retval = bfd_get_64 (abfd, buf);
10129 break;
10130 default:
10131 internal_error (__FILE__, __LINE__,
10132 _("read_offset_1: bad switch [in module %s]"),
10133 bfd_get_filename (abfd));
10134 }
10135
10136 return retval;
10137 }
10138
10139 static gdb_byte *
10140 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10141 {
10142 /* If the size of a host char is 8 bits, we can return a pointer
10143 to the buffer, otherwise we have to copy the data to a buffer
10144 allocated on the temporary obstack. */
10145 gdb_assert (HOST_CHAR_BIT == 8);
10146 return buf;
10147 }
10148
10149 static char *
10150 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10151 {
10152 /* If the size of a host char is 8 bits, we can return a pointer
10153 to the string, otherwise we have to copy the string to a buffer
10154 allocated on the temporary obstack. */
10155 gdb_assert (HOST_CHAR_BIT == 8);
10156 if (*buf == '\0')
10157 {
10158 *bytes_read_ptr = 1;
10159 return NULL;
10160 }
10161 *bytes_read_ptr = strlen ((char *) buf) + 1;
10162 return (char *) buf;
10163 }
10164
10165 static char *
10166 read_indirect_string (bfd *abfd, gdb_byte *buf,
10167 const struct comp_unit_head *cu_header,
10168 unsigned int *bytes_read_ptr)
10169 {
10170 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10171
10172 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10173 if (dwarf2_per_objfile->str.buffer == NULL)
10174 {
10175 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10176 bfd_get_filename (abfd));
10177 return NULL;
10178 }
10179 if (str_offset >= dwarf2_per_objfile->str.size)
10180 {
10181 error (_("DW_FORM_strp pointing outside of "
10182 ".debug_str section [in module %s]"),
10183 bfd_get_filename (abfd));
10184 return NULL;
10185 }
10186 gdb_assert (HOST_CHAR_BIT == 8);
10187 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10188 return NULL;
10189 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10190 }
10191
10192 static unsigned long
10193 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10194 {
10195 unsigned long result;
10196 unsigned int num_read;
10197 int i, shift;
10198 unsigned char byte;
10199
10200 result = 0;
10201 shift = 0;
10202 num_read = 0;
10203 i = 0;
10204 while (1)
10205 {
10206 byte = bfd_get_8 (abfd, buf);
10207 buf++;
10208 num_read++;
10209 result |= ((unsigned long)(byte & 127) << shift);
10210 if ((byte & 128) == 0)
10211 {
10212 break;
10213 }
10214 shift += 7;
10215 }
10216 *bytes_read_ptr = num_read;
10217 return result;
10218 }
10219
10220 static long
10221 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10222 {
10223 long result;
10224 int i, shift, num_read;
10225 unsigned char byte;
10226
10227 result = 0;
10228 shift = 0;
10229 num_read = 0;
10230 i = 0;
10231 while (1)
10232 {
10233 byte = bfd_get_8 (abfd, buf);
10234 buf++;
10235 num_read++;
10236 result |= ((long)(byte & 127) << shift);
10237 shift += 7;
10238 if ((byte & 128) == 0)
10239 {
10240 break;
10241 }
10242 }
10243 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10244 result |= -(((long)1) << shift);
10245 *bytes_read_ptr = num_read;
10246 return result;
10247 }
10248
10249 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10250
10251 static gdb_byte *
10252 skip_leb128 (bfd *abfd, gdb_byte *buf)
10253 {
10254 int byte;
10255
10256 while (1)
10257 {
10258 byte = bfd_get_8 (abfd, buf);
10259 buf++;
10260 if ((byte & 128) == 0)
10261 return buf;
10262 }
10263 }
10264
10265 static void
10266 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10267 {
10268 switch (lang)
10269 {
10270 case DW_LANG_C89:
10271 case DW_LANG_C99:
10272 case DW_LANG_C:
10273 cu->language = language_c;
10274 break;
10275 case DW_LANG_C_plus_plus:
10276 cu->language = language_cplus;
10277 break;
10278 case DW_LANG_D:
10279 cu->language = language_d;
10280 break;
10281 case DW_LANG_Fortran77:
10282 case DW_LANG_Fortran90:
10283 case DW_LANG_Fortran95:
10284 cu->language = language_fortran;
10285 break;
10286 case DW_LANG_Mips_Assembler:
10287 cu->language = language_asm;
10288 break;
10289 case DW_LANG_Java:
10290 cu->language = language_java;
10291 break;
10292 case DW_LANG_Ada83:
10293 case DW_LANG_Ada95:
10294 cu->language = language_ada;
10295 break;
10296 case DW_LANG_Modula2:
10297 cu->language = language_m2;
10298 break;
10299 case DW_LANG_Pascal83:
10300 cu->language = language_pascal;
10301 break;
10302 case DW_LANG_ObjC:
10303 cu->language = language_objc;
10304 break;
10305 case DW_LANG_Cobol74:
10306 case DW_LANG_Cobol85:
10307 default:
10308 cu->language = language_minimal;
10309 break;
10310 }
10311 cu->language_defn = language_def (cu->language);
10312 }
10313
10314 /* Return the named attribute or NULL if not there. */
10315
10316 static struct attribute *
10317 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10318 {
10319 unsigned int i;
10320 struct attribute *spec = NULL;
10321
10322 for (i = 0; i < die->num_attrs; ++i)
10323 {
10324 if (die->attrs[i].name == name)
10325 return &die->attrs[i];
10326 if (die->attrs[i].name == DW_AT_specification
10327 || die->attrs[i].name == DW_AT_abstract_origin)
10328 spec = &die->attrs[i];
10329 }
10330
10331 if (spec)
10332 {
10333 die = follow_die_ref (die, spec, &cu);
10334 return dwarf2_attr (die, name, cu);
10335 }
10336
10337 return NULL;
10338 }
10339
10340 /* Return the named attribute or NULL if not there,
10341 but do not follow DW_AT_specification, etc.
10342 This is for use in contexts where we're reading .debug_types dies.
10343 Following DW_AT_specification, DW_AT_abstract_origin will take us
10344 back up the chain, and we want to go down. */
10345
10346 static struct attribute *
10347 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10348 struct dwarf2_cu *cu)
10349 {
10350 unsigned int i;
10351
10352 for (i = 0; i < die->num_attrs; ++i)
10353 if (die->attrs[i].name == name)
10354 return &die->attrs[i];
10355
10356 return NULL;
10357 }
10358
10359 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10360 and holds a non-zero value. This function should only be used for
10361 DW_FORM_flag or DW_FORM_flag_present attributes. */
10362
10363 static int
10364 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10365 {
10366 struct attribute *attr = dwarf2_attr (die, name, cu);
10367
10368 return (attr && DW_UNSND (attr));
10369 }
10370
10371 static int
10372 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10373 {
10374 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10375 which value is non-zero. However, we have to be careful with
10376 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10377 (via dwarf2_flag_true_p) follows this attribute. So we may
10378 end up accidently finding a declaration attribute that belongs
10379 to a different DIE referenced by the specification attribute,
10380 even though the given DIE does not have a declaration attribute. */
10381 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10382 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10383 }
10384
10385 /* Return the die giving the specification for DIE, if there is
10386 one. *SPEC_CU is the CU containing DIE on input, and the CU
10387 containing the return value on output. If there is no
10388 specification, but there is an abstract origin, that is
10389 returned. */
10390
10391 static struct die_info *
10392 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10393 {
10394 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10395 *spec_cu);
10396
10397 if (spec_attr == NULL)
10398 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10399
10400 if (spec_attr == NULL)
10401 return NULL;
10402 else
10403 return follow_die_ref (die, spec_attr, spec_cu);
10404 }
10405
10406 /* Free the line_header structure *LH, and any arrays and strings it
10407 refers to.
10408 NOTE: This is also used as a "cleanup" function. */
10409
10410 static void
10411 free_line_header (struct line_header *lh)
10412 {
10413 if (lh->standard_opcode_lengths)
10414 xfree (lh->standard_opcode_lengths);
10415
10416 /* Remember that all the lh->file_names[i].name pointers are
10417 pointers into debug_line_buffer, and don't need to be freed. */
10418 if (lh->file_names)
10419 xfree (lh->file_names);
10420
10421 /* Similarly for the include directory names. */
10422 if (lh->include_dirs)
10423 xfree (lh->include_dirs);
10424
10425 xfree (lh);
10426 }
10427
10428 /* Add an entry to LH's include directory table. */
10429
10430 static void
10431 add_include_dir (struct line_header *lh, char *include_dir)
10432 {
10433 /* Grow the array if necessary. */
10434 if (lh->include_dirs_size == 0)
10435 {
10436 lh->include_dirs_size = 1; /* for testing */
10437 lh->include_dirs = xmalloc (lh->include_dirs_size
10438 * sizeof (*lh->include_dirs));
10439 }
10440 else if (lh->num_include_dirs >= lh->include_dirs_size)
10441 {
10442 lh->include_dirs_size *= 2;
10443 lh->include_dirs = xrealloc (lh->include_dirs,
10444 (lh->include_dirs_size
10445 * sizeof (*lh->include_dirs)));
10446 }
10447
10448 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10449 }
10450
10451 /* Add an entry to LH's file name table. */
10452
10453 static void
10454 add_file_name (struct line_header *lh,
10455 char *name,
10456 unsigned int dir_index,
10457 unsigned int mod_time,
10458 unsigned int length)
10459 {
10460 struct file_entry *fe;
10461
10462 /* Grow the array if necessary. */
10463 if (lh->file_names_size == 0)
10464 {
10465 lh->file_names_size = 1; /* for testing */
10466 lh->file_names = xmalloc (lh->file_names_size
10467 * sizeof (*lh->file_names));
10468 }
10469 else if (lh->num_file_names >= lh->file_names_size)
10470 {
10471 lh->file_names_size *= 2;
10472 lh->file_names = xrealloc (lh->file_names,
10473 (lh->file_names_size
10474 * sizeof (*lh->file_names)));
10475 }
10476
10477 fe = &lh->file_names[lh->num_file_names++];
10478 fe->name = name;
10479 fe->dir_index = dir_index;
10480 fe->mod_time = mod_time;
10481 fe->length = length;
10482 fe->included_p = 0;
10483 fe->symtab = NULL;
10484 }
10485
10486 /* Read the statement program header starting at OFFSET in
10487 .debug_line, according to the endianness of ABFD. Return a pointer
10488 to a struct line_header, allocated using xmalloc.
10489
10490 NOTE: the strings in the include directory and file name tables of
10491 the returned object point into debug_line_buffer, and must not be
10492 freed. */
10493
10494 static struct line_header *
10495 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10496 struct dwarf2_cu *cu)
10497 {
10498 struct cleanup *back_to;
10499 struct line_header *lh;
10500 gdb_byte *line_ptr;
10501 unsigned int bytes_read, offset_size;
10502 int i;
10503 char *cur_dir, *cur_file;
10504
10505 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10506 if (dwarf2_per_objfile->line.buffer == NULL)
10507 {
10508 complaint (&symfile_complaints, _("missing .debug_line section"));
10509 return 0;
10510 }
10511
10512 /* Make sure that at least there's room for the total_length field.
10513 That could be 12 bytes long, but we're just going to fudge that. */
10514 if (offset + 4 >= dwarf2_per_objfile->line.size)
10515 {
10516 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10517 return 0;
10518 }
10519
10520 lh = xmalloc (sizeof (*lh));
10521 memset (lh, 0, sizeof (*lh));
10522 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10523 (void *) lh);
10524
10525 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10526
10527 /* Read in the header. */
10528 lh->total_length =
10529 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10530 &bytes_read, &offset_size);
10531 line_ptr += bytes_read;
10532 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10533 + dwarf2_per_objfile->line.size))
10534 {
10535 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10536 return 0;
10537 }
10538 lh->statement_program_end = line_ptr + lh->total_length;
10539 lh->version = read_2_bytes (abfd, line_ptr);
10540 line_ptr += 2;
10541 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10542 line_ptr += offset_size;
10543 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10544 line_ptr += 1;
10545 if (lh->version >= 4)
10546 {
10547 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10548 line_ptr += 1;
10549 }
10550 else
10551 lh->maximum_ops_per_instruction = 1;
10552
10553 if (lh->maximum_ops_per_instruction == 0)
10554 {
10555 lh->maximum_ops_per_instruction = 1;
10556 complaint (&symfile_complaints,
10557 _("invalid maximum_ops_per_instruction "
10558 "in `.debug_line' section"));
10559 }
10560
10561 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10562 line_ptr += 1;
10563 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10564 line_ptr += 1;
10565 lh->line_range = read_1_byte (abfd, line_ptr);
10566 line_ptr += 1;
10567 lh->opcode_base = read_1_byte (abfd, line_ptr);
10568 line_ptr += 1;
10569 lh->standard_opcode_lengths
10570 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10571
10572 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10573 for (i = 1; i < lh->opcode_base; ++i)
10574 {
10575 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10576 line_ptr += 1;
10577 }
10578
10579 /* Read directory table. */
10580 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10581 {
10582 line_ptr += bytes_read;
10583 add_include_dir (lh, cur_dir);
10584 }
10585 line_ptr += bytes_read;
10586
10587 /* Read file name table. */
10588 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10589 {
10590 unsigned int dir_index, mod_time, length;
10591
10592 line_ptr += bytes_read;
10593 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10594 line_ptr += bytes_read;
10595 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10596 line_ptr += bytes_read;
10597 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10598 line_ptr += bytes_read;
10599
10600 add_file_name (lh, cur_file, dir_index, mod_time, length);
10601 }
10602 line_ptr += bytes_read;
10603 lh->statement_program_start = line_ptr;
10604
10605 if (line_ptr > (dwarf2_per_objfile->line.buffer
10606 + dwarf2_per_objfile->line.size))
10607 complaint (&symfile_complaints,
10608 _("line number info header doesn't "
10609 "fit in `.debug_line' section"));
10610
10611 discard_cleanups (back_to);
10612 return lh;
10613 }
10614
10615 /* This function exists to work around a bug in certain compilers
10616 (particularly GCC 2.95), in which the first line number marker of a
10617 function does not show up until after the prologue, right before
10618 the second line number marker. This function shifts ADDRESS down
10619 to the beginning of the function if necessary, and is called on
10620 addresses passed to record_line. */
10621
10622 static CORE_ADDR
10623 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10624 {
10625 struct function_range *fn;
10626
10627 /* Find the function_range containing address. */
10628 if (!cu->first_fn)
10629 return address;
10630
10631 if (!cu->cached_fn)
10632 cu->cached_fn = cu->first_fn;
10633
10634 fn = cu->cached_fn;
10635 while (fn)
10636 if (fn->lowpc <= address && fn->highpc > address)
10637 goto found;
10638 else
10639 fn = fn->next;
10640
10641 fn = cu->first_fn;
10642 while (fn && fn != cu->cached_fn)
10643 if (fn->lowpc <= address && fn->highpc > address)
10644 goto found;
10645 else
10646 fn = fn->next;
10647
10648 return address;
10649
10650 found:
10651 if (fn->seen_line)
10652 return address;
10653 if (address != fn->lowpc)
10654 complaint (&symfile_complaints,
10655 _("misplaced first line number at 0x%lx for '%s'"),
10656 (unsigned long) address, fn->name);
10657 fn->seen_line = 1;
10658 return fn->lowpc;
10659 }
10660
10661 /* Subroutine of dwarf_decode_lines to simplify it.
10662 Return the file name of the psymtab for included file FILE_INDEX
10663 in line header LH of PST.
10664 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10665 If space for the result is malloc'd, it will be freed by a cleanup.
10666 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10667
10668 static char *
10669 psymtab_include_file_name (const struct line_header *lh, int file_index,
10670 const struct partial_symtab *pst,
10671 const char *comp_dir)
10672 {
10673 const struct file_entry fe = lh->file_names [file_index];
10674 char *include_name = fe.name;
10675 char *include_name_to_compare = include_name;
10676 char *dir_name = NULL;
10677 const char *pst_filename;
10678 char *copied_name = NULL;
10679 int file_is_pst;
10680
10681 if (fe.dir_index)
10682 dir_name = lh->include_dirs[fe.dir_index - 1];
10683
10684 if (!IS_ABSOLUTE_PATH (include_name)
10685 && (dir_name != NULL || comp_dir != NULL))
10686 {
10687 /* Avoid creating a duplicate psymtab for PST.
10688 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10689 Before we do the comparison, however, we need to account
10690 for DIR_NAME and COMP_DIR.
10691 First prepend dir_name (if non-NULL). If we still don't
10692 have an absolute path prepend comp_dir (if non-NULL).
10693 However, the directory we record in the include-file's
10694 psymtab does not contain COMP_DIR (to match the
10695 corresponding symtab(s)).
10696
10697 Example:
10698
10699 bash$ cd /tmp
10700 bash$ gcc -g ./hello.c
10701 include_name = "hello.c"
10702 dir_name = "."
10703 DW_AT_comp_dir = comp_dir = "/tmp"
10704 DW_AT_name = "./hello.c" */
10705
10706 if (dir_name != NULL)
10707 {
10708 include_name = concat (dir_name, SLASH_STRING,
10709 include_name, (char *)NULL);
10710 include_name_to_compare = include_name;
10711 make_cleanup (xfree, include_name);
10712 }
10713 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10714 {
10715 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10716 include_name, (char *)NULL);
10717 }
10718 }
10719
10720 pst_filename = pst->filename;
10721 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10722 {
10723 copied_name = concat (pst->dirname, SLASH_STRING,
10724 pst_filename, (char *)NULL);
10725 pst_filename = copied_name;
10726 }
10727
10728 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10729
10730 if (include_name_to_compare != include_name)
10731 xfree (include_name_to_compare);
10732 if (copied_name != NULL)
10733 xfree (copied_name);
10734
10735 if (file_is_pst)
10736 return NULL;
10737 return include_name;
10738 }
10739
10740 /* Ignore this record_line request. */
10741
10742 static void
10743 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10744 {
10745 return;
10746 }
10747
10748 /* Decode the Line Number Program (LNP) for the given line_header
10749 structure and CU. The actual information extracted and the type
10750 of structures created from the LNP depends on the value of PST.
10751
10752 1. If PST is NULL, then this procedure uses the data from the program
10753 to create all necessary symbol tables, and their linetables.
10754
10755 2. If PST is not NULL, this procedure reads the program to determine
10756 the list of files included by the unit represented by PST, and
10757 builds all the associated partial symbol tables.
10758
10759 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10760 It is used for relative paths in the line table.
10761 NOTE: When processing partial symtabs (pst != NULL),
10762 comp_dir == pst->dirname.
10763
10764 NOTE: It is important that psymtabs have the same file name (via strcmp)
10765 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10766 symtab we don't use it in the name of the psymtabs we create.
10767 E.g. expand_line_sal requires this when finding psymtabs to expand.
10768 A good testcase for this is mb-inline.exp. */
10769
10770 static void
10771 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10772 struct dwarf2_cu *cu, struct partial_symtab *pst)
10773 {
10774 gdb_byte *line_ptr, *extended_end;
10775 gdb_byte *line_end;
10776 unsigned int bytes_read, extended_len;
10777 unsigned char op_code, extended_op, adj_opcode;
10778 CORE_ADDR baseaddr;
10779 struct objfile *objfile = cu->objfile;
10780 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10781 const int decode_for_pst_p = (pst != NULL);
10782 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10783 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10784 = record_line;
10785
10786 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10787
10788 line_ptr = lh->statement_program_start;
10789 line_end = lh->statement_program_end;
10790
10791 /* Read the statement sequences until there's nothing left. */
10792 while (line_ptr < line_end)
10793 {
10794 /* state machine registers */
10795 CORE_ADDR address = 0;
10796 unsigned int file = 1;
10797 unsigned int line = 1;
10798 unsigned int column = 0;
10799 int is_stmt = lh->default_is_stmt;
10800 int basic_block = 0;
10801 int end_sequence = 0;
10802 CORE_ADDR addr;
10803 unsigned char op_index = 0;
10804
10805 if (!decode_for_pst_p && lh->num_file_names >= file)
10806 {
10807 /* Start a subfile for the current file of the state machine. */
10808 /* lh->include_dirs and lh->file_names are 0-based, but the
10809 directory and file name numbers in the statement program
10810 are 1-based. */
10811 struct file_entry *fe = &lh->file_names[file - 1];
10812 char *dir = NULL;
10813
10814 if (fe->dir_index)
10815 dir = lh->include_dirs[fe->dir_index - 1];
10816
10817 dwarf2_start_subfile (fe->name, dir, comp_dir);
10818 }
10819
10820 /* Decode the table. */
10821 while (!end_sequence)
10822 {
10823 op_code = read_1_byte (abfd, line_ptr);
10824 line_ptr += 1;
10825 if (line_ptr > line_end)
10826 {
10827 dwarf2_debug_line_missing_end_sequence_complaint ();
10828 break;
10829 }
10830
10831 if (op_code >= lh->opcode_base)
10832 {
10833 /* Special operand. */
10834 adj_opcode = op_code - lh->opcode_base;
10835 address += (((op_index + (adj_opcode / lh->line_range))
10836 / lh->maximum_ops_per_instruction)
10837 * lh->minimum_instruction_length);
10838 op_index = ((op_index + (adj_opcode / lh->line_range))
10839 % lh->maximum_ops_per_instruction);
10840 line += lh->line_base + (adj_opcode % lh->line_range);
10841 if (lh->num_file_names < file || file == 0)
10842 dwarf2_debug_line_missing_file_complaint ();
10843 /* For now we ignore lines not starting on an
10844 instruction boundary. */
10845 else if (op_index == 0)
10846 {
10847 lh->file_names[file - 1].included_p = 1;
10848 if (!decode_for_pst_p && is_stmt)
10849 {
10850 if (last_subfile != current_subfile)
10851 {
10852 addr = gdbarch_addr_bits_remove (gdbarch, address);
10853 if (last_subfile)
10854 (*p_record_line) (last_subfile, 0, addr);
10855 last_subfile = current_subfile;
10856 }
10857 /* Append row to matrix using current values. */
10858 addr = check_cu_functions (address, cu);
10859 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10860 (*p_record_line) (current_subfile, line, addr);
10861 }
10862 }
10863 basic_block = 0;
10864 }
10865 else switch (op_code)
10866 {
10867 case DW_LNS_extended_op:
10868 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10869 &bytes_read);
10870 line_ptr += bytes_read;
10871 extended_end = line_ptr + extended_len;
10872 extended_op = read_1_byte (abfd, line_ptr);
10873 line_ptr += 1;
10874 switch (extended_op)
10875 {
10876 case DW_LNE_end_sequence:
10877 p_record_line = record_line;
10878 end_sequence = 1;
10879 break;
10880 case DW_LNE_set_address:
10881 address = read_address (abfd, line_ptr, cu, &bytes_read);
10882
10883 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10884 {
10885 /* This line table is for a function which has been
10886 GCd by the linker. Ignore it. PR gdb/12528 */
10887
10888 long line_offset
10889 = line_ptr - dwarf2_per_objfile->line.buffer;
10890
10891 complaint (&symfile_complaints,
10892 _(".debug_line address at offset 0x%lx is 0 "
10893 "[in module %s]"),
10894 line_offset, cu->objfile->name);
10895 p_record_line = noop_record_line;
10896 }
10897
10898 op_index = 0;
10899 line_ptr += bytes_read;
10900 address += baseaddr;
10901 break;
10902 case DW_LNE_define_file:
10903 {
10904 char *cur_file;
10905 unsigned int dir_index, mod_time, length;
10906
10907 cur_file = read_direct_string (abfd, line_ptr,
10908 &bytes_read);
10909 line_ptr += bytes_read;
10910 dir_index =
10911 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10912 line_ptr += bytes_read;
10913 mod_time =
10914 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10915 line_ptr += bytes_read;
10916 length =
10917 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10918 line_ptr += bytes_read;
10919 add_file_name (lh, cur_file, dir_index, mod_time, length);
10920 }
10921 break;
10922 case DW_LNE_set_discriminator:
10923 /* The discriminator is not interesting to the debugger;
10924 just ignore it. */
10925 line_ptr = extended_end;
10926 break;
10927 default:
10928 complaint (&symfile_complaints,
10929 _("mangled .debug_line section"));
10930 return;
10931 }
10932 /* Make sure that we parsed the extended op correctly. If e.g.
10933 we expected a different address size than the producer used,
10934 we may have read the wrong number of bytes. */
10935 if (line_ptr != extended_end)
10936 {
10937 complaint (&symfile_complaints,
10938 _("mangled .debug_line section"));
10939 return;
10940 }
10941 break;
10942 case DW_LNS_copy:
10943 if (lh->num_file_names < file || file == 0)
10944 dwarf2_debug_line_missing_file_complaint ();
10945 else
10946 {
10947 lh->file_names[file - 1].included_p = 1;
10948 if (!decode_for_pst_p && is_stmt)
10949 {
10950 if (last_subfile != current_subfile)
10951 {
10952 addr = gdbarch_addr_bits_remove (gdbarch, address);
10953 if (last_subfile)
10954 (*p_record_line) (last_subfile, 0, addr);
10955 last_subfile = current_subfile;
10956 }
10957 addr = check_cu_functions (address, cu);
10958 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10959 (*p_record_line) (current_subfile, line, addr);
10960 }
10961 }
10962 basic_block = 0;
10963 break;
10964 case DW_LNS_advance_pc:
10965 {
10966 CORE_ADDR adjust
10967 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10968
10969 address += (((op_index + adjust)
10970 / lh->maximum_ops_per_instruction)
10971 * lh->minimum_instruction_length);
10972 op_index = ((op_index + adjust)
10973 % lh->maximum_ops_per_instruction);
10974 line_ptr += bytes_read;
10975 }
10976 break;
10977 case DW_LNS_advance_line:
10978 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10979 line_ptr += bytes_read;
10980 break;
10981 case DW_LNS_set_file:
10982 {
10983 /* The arrays lh->include_dirs and lh->file_names are
10984 0-based, but the directory and file name numbers in
10985 the statement program are 1-based. */
10986 struct file_entry *fe;
10987 char *dir = NULL;
10988
10989 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10990 line_ptr += bytes_read;
10991 if (lh->num_file_names < file || file == 0)
10992 dwarf2_debug_line_missing_file_complaint ();
10993 else
10994 {
10995 fe = &lh->file_names[file - 1];
10996 if (fe->dir_index)
10997 dir = lh->include_dirs[fe->dir_index - 1];
10998 if (!decode_for_pst_p)
10999 {
11000 last_subfile = current_subfile;
11001 dwarf2_start_subfile (fe->name, dir, comp_dir);
11002 }
11003 }
11004 }
11005 break;
11006 case DW_LNS_set_column:
11007 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11008 line_ptr += bytes_read;
11009 break;
11010 case DW_LNS_negate_stmt:
11011 is_stmt = (!is_stmt);
11012 break;
11013 case DW_LNS_set_basic_block:
11014 basic_block = 1;
11015 break;
11016 /* Add to the address register of the state machine the
11017 address increment value corresponding to special opcode
11018 255. I.e., this value is scaled by the minimum
11019 instruction length since special opcode 255 would have
11020 scaled the increment. */
11021 case DW_LNS_const_add_pc:
11022 {
11023 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11024
11025 address += (((op_index + adjust)
11026 / lh->maximum_ops_per_instruction)
11027 * lh->minimum_instruction_length);
11028 op_index = ((op_index + adjust)
11029 % lh->maximum_ops_per_instruction);
11030 }
11031 break;
11032 case DW_LNS_fixed_advance_pc:
11033 address += read_2_bytes (abfd, line_ptr);
11034 op_index = 0;
11035 line_ptr += 2;
11036 break;
11037 default:
11038 {
11039 /* Unknown standard opcode, ignore it. */
11040 int i;
11041
11042 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11043 {
11044 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11045 line_ptr += bytes_read;
11046 }
11047 }
11048 }
11049 }
11050 if (lh->num_file_names < file || file == 0)
11051 dwarf2_debug_line_missing_file_complaint ();
11052 else
11053 {
11054 lh->file_names[file - 1].included_p = 1;
11055 if (!decode_for_pst_p)
11056 {
11057 addr = gdbarch_addr_bits_remove (gdbarch, address);
11058 (*p_record_line) (current_subfile, 0, addr);
11059 }
11060 }
11061 }
11062
11063 if (decode_for_pst_p)
11064 {
11065 int file_index;
11066
11067 /* Now that we're done scanning the Line Header Program, we can
11068 create the psymtab of each included file. */
11069 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11070 if (lh->file_names[file_index].included_p == 1)
11071 {
11072 char *include_name =
11073 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11074 if (include_name != NULL)
11075 dwarf2_create_include_psymtab (include_name, pst, objfile);
11076 }
11077 }
11078 else
11079 {
11080 /* Make sure a symtab is created for every file, even files
11081 which contain only variables (i.e. no code with associated
11082 line numbers). */
11083
11084 int i;
11085 struct file_entry *fe;
11086
11087 for (i = 0; i < lh->num_file_names; i++)
11088 {
11089 char *dir = NULL;
11090
11091 fe = &lh->file_names[i];
11092 if (fe->dir_index)
11093 dir = lh->include_dirs[fe->dir_index - 1];
11094 dwarf2_start_subfile (fe->name, dir, comp_dir);
11095
11096 /* Skip the main file; we don't need it, and it must be
11097 allocated last, so that it will show up before the
11098 non-primary symtabs in the objfile's symtab list. */
11099 if (current_subfile == first_subfile)
11100 continue;
11101
11102 if (current_subfile->symtab == NULL)
11103 current_subfile->symtab = allocate_symtab (current_subfile->name,
11104 cu->objfile);
11105 fe->symtab = current_subfile->symtab;
11106 }
11107 }
11108 }
11109
11110 /* Start a subfile for DWARF. FILENAME is the name of the file and
11111 DIRNAME the name of the source directory which contains FILENAME
11112 or NULL if not known. COMP_DIR is the compilation directory for the
11113 linetable's compilation unit or NULL if not known.
11114 This routine tries to keep line numbers from identical absolute and
11115 relative file names in a common subfile.
11116
11117 Using the `list' example from the GDB testsuite, which resides in
11118 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11119 of /srcdir/list0.c yields the following debugging information for list0.c:
11120
11121 DW_AT_name: /srcdir/list0.c
11122 DW_AT_comp_dir: /compdir
11123 files.files[0].name: list0.h
11124 files.files[0].dir: /srcdir
11125 files.files[1].name: list0.c
11126 files.files[1].dir: /srcdir
11127
11128 The line number information for list0.c has to end up in a single
11129 subfile, so that `break /srcdir/list0.c:1' works as expected.
11130 start_subfile will ensure that this happens provided that we pass the
11131 concatenation of files.files[1].dir and files.files[1].name as the
11132 subfile's name. */
11133
11134 static void
11135 dwarf2_start_subfile (char *filename, const char *dirname,
11136 const char *comp_dir)
11137 {
11138 char *fullname;
11139
11140 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11141 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11142 second argument to start_subfile. To be consistent, we do the
11143 same here. In order not to lose the line information directory,
11144 we concatenate it to the filename when it makes sense.
11145 Note that the Dwarf3 standard says (speaking of filenames in line
11146 information): ``The directory index is ignored for file names
11147 that represent full path names''. Thus ignoring dirname in the
11148 `else' branch below isn't an issue. */
11149
11150 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11151 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11152 else
11153 fullname = filename;
11154
11155 start_subfile (fullname, comp_dir);
11156
11157 if (fullname != filename)
11158 xfree (fullname);
11159 }
11160
11161 static void
11162 var_decode_location (struct attribute *attr, struct symbol *sym,
11163 struct dwarf2_cu *cu)
11164 {
11165 struct objfile *objfile = cu->objfile;
11166 struct comp_unit_head *cu_header = &cu->header;
11167
11168 /* NOTE drow/2003-01-30: There used to be a comment and some special
11169 code here to turn a symbol with DW_AT_external and a
11170 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11171 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11172 with some versions of binutils) where shared libraries could have
11173 relocations against symbols in their debug information - the
11174 minimal symbol would have the right address, but the debug info
11175 would not. It's no longer necessary, because we will explicitly
11176 apply relocations when we read in the debug information now. */
11177
11178 /* A DW_AT_location attribute with no contents indicates that a
11179 variable has been optimized away. */
11180 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11181 {
11182 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11183 return;
11184 }
11185
11186 /* Handle one degenerate form of location expression specially, to
11187 preserve GDB's previous behavior when section offsets are
11188 specified. If this is just a DW_OP_addr then mark this symbol
11189 as LOC_STATIC. */
11190
11191 if (attr_form_is_block (attr)
11192 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11193 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11194 {
11195 unsigned int dummy;
11196
11197 SYMBOL_VALUE_ADDRESS (sym) =
11198 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11199 SYMBOL_CLASS (sym) = LOC_STATIC;
11200 fixup_symbol_section (sym, objfile);
11201 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11202 SYMBOL_SECTION (sym));
11203 return;
11204 }
11205
11206 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11207 expression evaluator, and use LOC_COMPUTED only when necessary
11208 (i.e. when the value of a register or memory location is
11209 referenced, or a thread-local block, etc.). Then again, it might
11210 not be worthwhile. I'm assuming that it isn't unless performance
11211 or memory numbers show me otherwise. */
11212
11213 dwarf2_symbol_mark_computed (attr, sym, cu);
11214 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11215
11216 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11217 cu->has_loclist = 1;
11218 }
11219
11220 /* Given a pointer to a DWARF information entry, figure out if we need
11221 to make a symbol table entry for it, and if so, create a new entry
11222 and return a pointer to it.
11223 If TYPE is NULL, determine symbol type from the die, otherwise
11224 used the passed type.
11225 If SPACE is not NULL, use it to hold the new symbol. If it is
11226 NULL, allocate a new symbol on the objfile's obstack. */
11227
11228 static struct symbol *
11229 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11230 struct symbol *space)
11231 {
11232 struct objfile *objfile = cu->objfile;
11233 struct symbol *sym = NULL;
11234 char *name;
11235 struct attribute *attr = NULL;
11236 struct attribute *attr2 = NULL;
11237 CORE_ADDR baseaddr;
11238 struct pending **list_to_add = NULL;
11239
11240 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11241
11242 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11243
11244 name = dwarf2_name (die, cu);
11245 if (name)
11246 {
11247 const char *linkagename;
11248 int suppress_add = 0;
11249
11250 if (space)
11251 sym = space;
11252 else
11253 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11254 OBJSTAT (objfile, n_syms++);
11255
11256 /* Cache this symbol's name and the name's demangled form (if any). */
11257 SYMBOL_SET_LANGUAGE (sym, cu->language);
11258 linkagename = dwarf2_physname (name, die, cu);
11259 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11260
11261 /* Fortran does not have mangling standard and the mangling does differ
11262 between gfortran, iFort etc. */
11263 if (cu->language == language_fortran
11264 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11265 symbol_set_demangled_name (&(sym->ginfo),
11266 (char *) dwarf2_full_name (name, die, cu),
11267 NULL);
11268
11269 /* Default assumptions.
11270 Use the passed type or decode it from the die. */
11271 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11272 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11273 if (type != NULL)
11274 SYMBOL_TYPE (sym) = type;
11275 else
11276 SYMBOL_TYPE (sym) = die_type (die, cu);
11277 attr = dwarf2_attr (die,
11278 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11279 cu);
11280 if (attr)
11281 {
11282 SYMBOL_LINE (sym) = DW_UNSND (attr);
11283 }
11284
11285 attr = dwarf2_attr (die,
11286 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11287 cu);
11288 if (attr)
11289 {
11290 int file_index = DW_UNSND (attr);
11291
11292 if (cu->line_header == NULL
11293 || file_index > cu->line_header->num_file_names)
11294 complaint (&symfile_complaints,
11295 _("file index out of range"));
11296 else if (file_index > 0)
11297 {
11298 struct file_entry *fe;
11299
11300 fe = &cu->line_header->file_names[file_index - 1];
11301 SYMBOL_SYMTAB (sym) = fe->symtab;
11302 }
11303 }
11304
11305 switch (die->tag)
11306 {
11307 case DW_TAG_label:
11308 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11309 if (attr)
11310 {
11311 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11312 }
11313 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11314 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11315 SYMBOL_CLASS (sym) = LOC_LABEL;
11316 add_symbol_to_list (sym, cu->list_in_scope);
11317 break;
11318 case DW_TAG_subprogram:
11319 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11320 finish_block. */
11321 SYMBOL_CLASS (sym) = LOC_BLOCK;
11322 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11323 if ((attr2 && (DW_UNSND (attr2) != 0))
11324 || cu->language == language_ada)
11325 {
11326 /* Subprograms marked external are stored as a global symbol.
11327 Ada subprograms, whether marked external or not, are always
11328 stored as a global symbol, because we want to be able to
11329 access them globally. For instance, we want to be able
11330 to break on a nested subprogram without having to
11331 specify the context. */
11332 list_to_add = &global_symbols;
11333 }
11334 else
11335 {
11336 list_to_add = cu->list_in_scope;
11337 }
11338 break;
11339 case DW_TAG_inlined_subroutine:
11340 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11341 finish_block. */
11342 SYMBOL_CLASS (sym) = LOC_BLOCK;
11343 SYMBOL_INLINED (sym) = 1;
11344 /* Do not add the symbol to any lists. It will be found via
11345 BLOCK_FUNCTION from the blockvector. */
11346 break;
11347 case DW_TAG_template_value_param:
11348 suppress_add = 1;
11349 /* Fall through. */
11350 case DW_TAG_constant:
11351 case DW_TAG_variable:
11352 case DW_TAG_member:
11353 /* Compilation with minimal debug info may result in
11354 variables with missing type entries. Change the
11355 misleading `void' type to something sensible. */
11356 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11357 SYMBOL_TYPE (sym)
11358 = objfile_type (objfile)->nodebug_data_symbol;
11359
11360 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11361 /* In the case of DW_TAG_member, we should only be called for
11362 static const members. */
11363 if (die->tag == DW_TAG_member)
11364 {
11365 /* dwarf2_add_field uses die_is_declaration,
11366 so we do the same. */
11367 gdb_assert (die_is_declaration (die, cu));
11368 gdb_assert (attr);
11369 }
11370 if (attr)
11371 {
11372 dwarf2_const_value (attr, sym, cu);
11373 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11374 if (!suppress_add)
11375 {
11376 if (attr2 && (DW_UNSND (attr2) != 0))
11377 list_to_add = &global_symbols;
11378 else
11379 list_to_add = cu->list_in_scope;
11380 }
11381 break;
11382 }
11383 attr = dwarf2_attr (die, DW_AT_location, cu);
11384 if (attr)
11385 {
11386 var_decode_location (attr, sym, cu);
11387 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11388 if (SYMBOL_CLASS (sym) == LOC_STATIC
11389 && SYMBOL_VALUE_ADDRESS (sym) == 0
11390 && !dwarf2_per_objfile->has_section_at_zero)
11391 {
11392 /* When a static variable is eliminated by the linker,
11393 the corresponding debug information is not stripped
11394 out, but the variable address is set to null;
11395 do not add such variables into symbol table. */
11396 }
11397 else if (attr2 && (DW_UNSND (attr2) != 0))
11398 {
11399 /* Workaround gfortran PR debug/40040 - it uses
11400 DW_AT_location for variables in -fPIC libraries which may
11401 get overriden by other libraries/executable and get
11402 a different address. Resolve it by the minimal symbol
11403 which may come from inferior's executable using copy
11404 relocation. Make this workaround only for gfortran as for
11405 other compilers GDB cannot guess the minimal symbol
11406 Fortran mangling kind. */
11407 if (cu->language == language_fortran && die->parent
11408 && die->parent->tag == DW_TAG_module
11409 && cu->producer
11410 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11411 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11412
11413 /* A variable with DW_AT_external is never static,
11414 but it may be block-scoped. */
11415 list_to_add = (cu->list_in_scope == &file_symbols
11416 ? &global_symbols : cu->list_in_scope);
11417 }
11418 else
11419 list_to_add = cu->list_in_scope;
11420 }
11421 else
11422 {
11423 /* We do not know the address of this symbol.
11424 If it is an external symbol and we have type information
11425 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11426 The address of the variable will then be determined from
11427 the minimal symbol table whenever the variable is
11428 referenced. */
11429 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11430 if (attr2 && (DW_UNSND (attr2) != 0)
11431 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11432 {
11433 /* A variable with DW_AT_external is never static, but it
11434 may be block-scoped. */
11435 list_to_add = (cu->list_in_scope == &file_symbols
11436 ? &global_symbols : cu->list_in_scope);
11437
11438 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11439 }
11440 else if (!die_is_declaration (die, cu))
11441 {
11442 /* Use the default LOC_OPTIMIZED_OUT class. */
11443 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11444 if (!suppress_add)
11445 list_to_add = cu->list_in_scope;
11446 }
11447 }
11448 break;
11449 case DW_TAG_formal_parameter:
11450 /* If we are inside a function, mark this as an argument. If
11451 not, we might be looking at an argument to an inlined function
11452 when we do not have enough information to show inlined frames;
11453 pretend it's a local variable in that case so that the user can
11454 still see it. */
11455 if (context_stack_depth > 0
11456 && context_stack[context_stack_depth - 1].name != NULL)
11457 SYMBOL_IS_ARGUMENT (sym) = 1;
11458 attr = dwarf2_attr (die, DW_AT_location, cu);
11459 if (attr)
11460 {
11461 var_decode_location (attr, sym, cu);
11462 }
11463 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11464 if (attr)
11465 {
11466 dwarf2_const_value (attr, sym, cu);
11467 }
11468 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11469 if (attr && DW_UNSND (attr))
11470 {
11471 struct type *ref_type;
11472
11473 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11474 SYMBOL_TYPE (sym) = ref_type;
11475 }
11476
11477 list_to_add = cu->list_in_scope;
11478 break;
11479 case DW_TAG_unspecified_parameters:
11480 /* From varargs functions; gdb doesn't seem to have any
11481 interest in this information, so just ignore it for now.
11482 (FIXME?) */
11483 break;
11484 case DW_TAG_template_type_param:
11485 suppress_add = 1;
11486 /* Fall through. */
11487 case DW_TAG_class_type:
11488 case DW_TAG_interface_type:
11489 case DW_TAG_structure_type:
11490 case DW_TAG_union_type:
11491 case DW_TAG_set_type:
11492 case DW_TAG_enumeration_type:
11493 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11494 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11495
11496 {
11497 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11498 really ever be static objects: otherwise, if you try
11499 to, say, break of a class's method and you're in a file
11500 which doesn't mention that class, it won't work unless
11501 the check for all static symbols in lookup_symbol_aux
11502 saves you. See the OtherFileClass tests in
11503 gdb.c++/namespace.exp. */
11504
11505 if (!suppress_add)
11506 {
11507 list_to_add = (cu->list_in_scope == &file_symbols
11508 && (cu->language == language_cplus
11509 || cu->language == language_java)
11510 ? &global_symbols : cu->list_in_scope);
11511
11512 /* The semantics of C++ state that "struct foo {
11513 ... }" also defines a typedef for "foo". A Java
11514 class declaration also defines a typedef for the
11515 class. */
11516 if (cu->language == language_cplus
11517 || cu->language == language_java
11518 || cu->language == language_ada)
11519 {
11520 /* The symbol's name is already allocated along
11521 with this objfile, so we don't need to
11522 duplicate it for the type. */
11523 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11524 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11525 }
11526 }
11527 }
11528 break;
11529 case DW_TAG_typedef:
11530 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11531 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11532 list_to_add = cu->list_in_scope;
11533 break;
11534 case DW_TAG_base_type:
11535 case DW_TAG_subrange_type:
11536 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11537 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11538 list_to_add = cu->list_in_scope;
11539 break;
11540 case DW_TAG_enumerator:
11541 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11542 if (attr)
11543 {
11544 dwarf2_const_value (attr, sym, cu);
11545 }
11546 {
11547 /* NOTE: carlton/2003-11-10: See comment above in the
11548 DW_TAG_class_type, etc. block. */
11549
11550 list_to_add = (cu->list_in_scope == &file_symbols
11551 && (cu->language == language_cplus
11552 || cu->language == language_java)
11553 ? &global_symbols : cu->list_in_scope);
11554 }
11555 break;
11556 case DW_TAG_namespace:
11557 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11558 list_to_add = &global_symbols;
11559 break;
11560 default:
11561 /* Not a tag we recognize. Hopefully we aren't processing
11562 trash data, but since we must specifically ignore things
11563 we don't recognize, there is nothing else we should do at
11564 this point. */
11565 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11566 dwarf_tag_name (die->tag));
11567 break;
11568 }
11569
11570 if (suppress_add)
11571 {
11572 sym->hash_next = objfile->template_symbols;
11573 objfile->template_symbols = sym;
11574 list_to_add = NULL;
11575 }
11576
11577 if (list_to_add != NULL)
11578 add_symbol_to_list (sym, list_to_add);
11579
11580 /* For the benefit of old versions of GCC, check for anonymous
11581 namespaces based on the demangled name. */
11582 if (!processing_has_namespace_info
11583 && cu->language == language_cplus)
11584 cp_scan_for_anonymous_namespaces (sym);
11585 }
11586 return (sym);
11587 }
11588
11589 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11590
11591 static struct symbol *
11592 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11593 {
11594 return new_symbol_full (die, type, cu, NULL);
11595 }
11596
11597 /* Given an attr with a DW_FORM_dataN value in host byte order,
11598 zero-extend it as appropriate for the symbol's type. The DWARF
11599 standard (v4) is not entirely clear about the meaning of using
11600 DW_FORM_dataN for a constant with a signed type, where the type is
11601 wider than the data. The conclusion of a discussion on the DWARF
11602 list was that this is unspecified. We choose to always zero-extend
11603 because that is the interpretation long in use by GCC. */
11604
11605 static gdb_byte *
11606 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11607 const char *name, struct obstack *obstack,
11608 struct dwarf2_cu *cu, long *value, int bits)
11609 {
11610 struct objfile *objfile = cu->objfile;
11611 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11612 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11613 LONGEST l = DW_UNSND (attr);
11614
11615 if (bits < sizeof (*value) * 8)
11616 {
11617 l &= ((LONGEST) 1 << bits) - 1;
11618 *value = l;
11619 }
11620 else if (bits == sizeof (*value) * 8)
11621 *value = l;
11622 else
11623 {
11624 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11625 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11626 return bytes;
11627 }
11628
11629 return NULL;
11630 }
11631
11632 /* Read a constant value from an attribute. Either set *VALUE, or if
11633 the value does not fit in *VALUE, set *BYTES - either already
11634 allocated on the objfile obstack, or newly allocated on OBSTACK,
11635 or, set *BATON, if we translated the constant to a location
11636 expression. */
11637
11638 static void
11639 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11640 const char *name, struct obstack *obstack,
11641 struct dwarf2_cu *cu,
11642 long *value, gdb_byte **bytes,
11643 struct dwarf2_locexpr_baton **baton)
11644 {
11645 struct objfile *objfile = cu->objfile;
11646 struct comp_unit_head *cu_header = &cu->header;
11647 struct dwarf_block *blk;
11648 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11649 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11650
11651 *value = 0;
11652 *bytes = NULL;
11653 *baton = NULL;
11654
11655 switch (attr->form)
11656 {
11657 case DW_FORM_addr:
11658 {
11659 gdb_byte *data;
11660
11661 if (TYPE_LENGTH (type) != cu_header->addr_size)
11662 dwarf2_const_value_length_mismatch_complaint (name,
11663 cu_header->addr_size,
11664 TYPE_LENGTH (type));
11665 /* Symbols of this form are reasonably rare, so we just
11666 piggyback on the existing location code rather than writing
11667 a new implementation of symbol_computed_ops. */
11668 *baton = obstack_alloc (&objfile->objfile_obstack,
11669 sizeof (struct dwarf2_locexpr_baton));
11670 (*baton)->per_cu = cu->per_cu;
11671 gdb_assert ((*baton)->per_cu);
11672
11673 (*baton)->size = 2 + cu_header->addr_size;
11674 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11675 (*baton)->data = data;
11676
11677 data[0] = DW_OP_addr;
11678 store_unsigned_integer (&data[1], cu_header->addr_size,
11679 byte_order, DW_ADDR (attr));
11680 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11681 }
11682 break;
11683 case DW_FORM_string:
11684 case DW_FORM_strp:
11685 /* DW_STRING is already allocated on the objfile obstack, point
11686 directly to it. */
11687 *bytes = (gdb_byte *) DW_STRING (attr);
11688 break;
11689 case DW_FORM_block1:
11690 case DW_FORM_block2:
11691 case DW_FORM_block4:
11692 case DW_FORM_block:
11693 case DW_FORM_exprloc:
11694 blk = DW_BLOCK (attr);
11695 if (TYPE_LENGTH (type) != blk->size)
11696 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11697 TYPE_LENGTH (type));
11698 *bytes = blk->data;
11699 break;
11700
11701 /* The DW_AT_const_value attributes are supposed to carry the
11702 symbol's value "represented as it would be on the target
11703 architecture." By the time we get here, it's already been
11704 converted to host endianness, so we just need to sign- or
11705 zero-extend it as appropriate. */
11706 case DW_FORM_data1:
11707 *bytes = dwarf2_const_value_data (attr, type, name,
11708 obstack, cu, value, 8);
11709 break;
11710 case DW_FORM_data2:
11711 *bytes = dwarf2_const_value_data (attr, type, name,
11712 obstack, cu, value, 16);
11713 break;
11714 case DW_FORM_data4:
11715 *bytes = dwarf2_const_value_data (attr, type, name,
11716 obstack, cu, value, 32);
11717 break;
11718 case DW_FORM_data8:
11719 *bytes = dwarf2_const_value_data (attr, type, name,
11720 obstack, cu, value, 64);
11721 break;
11722
11723 case DW_FORM_sdata:
11724 *value = DW_SND (attr);
11725 break;
11726
11727 case DW_FORM_udata:
11728 *value = DW_UNSND (attr);
11729 break;
11730
11731 default:
11732 complaint (&symfile_complaints,
11733 _("unsupported const value attribute form: '%s'"),
11734 dwarf_form_name (attr->form));
11735 *value = 0;
11736 break;
11737 }
11738 }
11739
11740
11741 /* Copy constant value from an attribute to a symbol. */
11742
11743 static void
11744 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11745 struct dwarf2_cu *cu)
11746 {
11747 struct objfile *objfile = cu->objfile;
11748 struct comp_unit_head *cu_header = &cu->header;
11749 long value;
11750 gdb_byte *bytes;
11751 struct dwarf2_locexpr_baton *baton;
11752
11753 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11754 SYMBOL_PRINT_NAME (sym),
11755 &objfile->objfile_obstack, cu,
11756 &value, &bytes, &baton);
11757
11758 if (baton != NULL)
11759 {
11760 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11761 SYMBOL_LOCATION_BATON (sym) = baton;
11762 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11763 }
11764 else if (bytes != NULL)
11765 {
11766 SYMBOL_VALUE_BYTES (sym) = bytes;
11767 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11768 }
11769 else
11770 {
11771 SYMBOL_VALUE (sym) = value;
11772 SYMBOL_CLASS (sym) = LOC_CONST;
11773 }
11774 }
11775
11776 /* Return the type of the die in question using its DW_AT_type attribute. */
11777
11778 static struct type *
11779 die_type (struct die_info *die, struct dwarf2_cu *cu)
11780 {
11781 struct attribute *type_attr;
11782
11783 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11784 if (!type_attr)
11785 {
11786 /* A missing DW_AT_type represents a void type. */
11787 return objfile_type (cu->objfile)->builtin_void;
11788 }
11789
11790 return lookup_die_type (die, type_attr, cu);
11791 }
11792
11793 /* True iff CU's producer generates GNAT Ada auxiliary information
11794 that allows to find parallel types through that information instead
11795 of having to do expensive parallel lookups by type name. */
11796
11797 static int
11798 need_gnat_info (struct dwarf2_cu *cu)
11799 {
11800 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11801 of GNAT produces this auxiliary information, without any indication
11802 that it is produced. Part of enhancing the FSF version of GNAT
11803 to produce that information will be to put in place an indicator
11804 that we can use in order to determine whether the descriptive type
11805 info is available or not. One suggestion that has been made is
11806 to use a new attribute, attached to the CU die. For now, assume
11807 that the descriptive type info is not available. */
11808 return 0;
11809 }
11810
11811 /* Return the auxiliary type of the die in question using its
11812 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11813 attribute is not present. */
11814
11815 static struct type *
11816 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11817 {
11818 struct attribute *type_attr;
11819
11820 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11821 if (!type_attr)
11822 return NULL;
11823
11824 return lookup_die_type (die, type_attr, cu);
11825 }
11826
11827 /* If DIE has a descriptive_type attribute, then set the TYPE's
11828 descriptive type accordingly. */
11829
11830 static void
11831 set_descriptive_type (struct type *type, struct die_info *die,
11832 struct dwarf2_cu *cu)
11833 {
11834 struct type *descriptive_type = die_descriptive_type (die, cu);
11835
11836 if (descriptive_type)
11837 {
11838 ALLOCATE_GNAT_AUX_TYPE (type);
11839 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11840 }
11841 }
11842
11843 /* Return the containing type of the die in question using its
11844 DW_AT_containing_type attribute. */
11845
11846 static struct type *
11847 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11848 {
11849 struct attribute *type_attr;
11850
11851 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11852 if (!type_attr)
11853 error (_("Dwarf Error: Problem turning containing type into gdb type "
11854 "[in module %s]"), cu->objfile->name);
11855
11856 return lookup_die_type (die, type_attr, cu);
11857 }
11858
11859 /* Look up the type of DIE in CU using its type attribute ATTR.
11860 If there is no type substitute an error marker. */
11861
11862 static struct type *
11863 lookup_die_type (struct die_info *die, struct attribute *attr,
11864 struct dwarf2_cu *cu)
11865 {
11866 struct type *this_type;
11867
11868 /* First see if we have it cached. */
11869
11870 if (is_ref_attr (attr))
11871 {
11872 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11873
11874 this_type = get_die_type_at_offset (offset, cu->per_cu);
11875 }
11876 else if (attr->form == DW_FORM_ref_sig8)
11877 {
11878 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11879 struct dwarf2_cu *sig_cu;
11880 unsigned int offset;
11881
11882 /* sig_type will be NULL if the signatured type is missing from
11883 the debug info. */
11884 if (sig_type == NULL)
11885 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11886 "at 0x%x [in module %s]"),
11887 die->offset, cu->objfile->name);
11888
11889 gdb_assert (sig_type->per_cu.from_debug_types);
11890 offset = sig_type->per_cu.offset + sig_type->type_offset;
11891 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11892 }
11893 else
11894 {
11895 dump_die_for_error (die);
11896 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11897 dwarf_attr_name (attr->name), cu->objfile->name);
11898 }
11899
11900 /* If not cached we need to read it in. */
11901
11902 if (this_type == NULL)
11903 {
11904 struct die_info *type_die;
11905 struct dwarf2_cu *type_cu = cu;
11906
11907 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11908 /* If the type is cached, we should have found it above. */
11909 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11910 this_type = read_type_die_1 (type_die, type_cu);
11911 }
11912
11913 /* If we still don't have a type use an error marker. */
11914
11915 if (this_type == NULL)
11916 {
11917 char *message, *saved;
11918
11919 /* read_type_die already issued a complaint. */
11920 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11921 cu->objfile->name,
11922 cu->header.offset,
11923 die->offset);
11924 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11925 message, strlen (message));
11926 xfree (message);
11927
11928 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11929 }
11930
11931 return this_type;
11932 }
11933
11934 /* Return the type in DIE, CU.
11935 Returns NULL for invalid types.
11936
11937 This first does a lookup in the appropriate type_hash table,
11938 and only reads the die in if necessary.
11939
11940 NOTE: This can be called when reading in partial or full symbols. */
11941
11942 static struct type *
11943 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11944 {
11945 struct type *this_type;
11946
11947 this_type = get_die_type (die, cu);
11948 if (this_type)
11949 return this_type;
11950
11951 return read_type_die_1 (die, cu);
11952 }
11953
11954 /* Read the type in DIE, CU.
11955 Returns NULL for invalid types. */
11956
11957 static struct type *
11958 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11959 {
11960 struct type *this_type = NULL;
11961
11962 switch (die->tag)
11963 {
11964 case DW_TAG_class_type:
11965 case DW_TAG_interface_type:
11966 case DW_TAG_structure_type:
11967 case DW_TAG_union_type:
11968 this_type = read_structure_type (die, cu);
11969 break;
11970 case DW_TAG_enumeration_type:
11971 this_type = read_enumeration_type (die, cu);
11972 break;
11973 case DW_TAG_subprogram:
11974 case DW_TAG_subroutine_type:
11975 case DW_TAG_inlined_subroutine:
11976 this_type = read_subroutine_type (die, cu);
11977 break;
11978 case DW_TAG_array_type:
11979 this_type = read_array_type (die, cu);
11980 break;
11981 case DW_TAG_set_type:
11982 this_type = read_set_type (die, cu);
11983 break;
11984 case DW_TAG_pointer_type:
11985 this_type = read_tag_pointer_type (die, cu);
11986 break;
11987 case DW_TAG_ptr_to_member_type:
11988 this_type = read_tag_ptr_to_member_type (die, cu);
11989 break;
11990 case DW_TAG_reference_type:
11991 this_type = read_tag_reference_type (die, cu);
11992 break;
11993 case DW_TAG_const_type:
11994 this_type = read_tag_const_type (die, cu);
11995 break;
11996 case DW_TAG_volatile_type:
11997 this_type = read_tag_volatile_type (die, cu);
11998 break;
11999 case DW_TAG_string_type:
12000 this_type = read_tag_string_type (die, cu);
12001 break;
12002 case DW_TAG_typedef:
12003 this_type = read_typedef (die, cu);
12004 break;
12005 case DW_TAG_subrange_type:
12006 this_type = read_subrange_type (die, cu);
12007 break;
12008 case DW_TAG_base_type:
12009 this_type = read_base_type (die, cu);
12010 break;
12011 case DW_TAG_unspecified_type:
12012 this_type = read_unspecified_type (die, cu);
12013 break;
12014 case DW_TAG_namespace:
12015 this_type = read_namespace_type (die, cu);
12016 break;
12017 case DW_TAG_module:
12018 this_type = read_module_type (die, cu);
12019 break;
12020 default:
12021 complaint (&symfile_complaints,
12022 _("unexpected tag in read_type_die: '%s'"),
12023 dwarf_tag_name (die->tag));
12024 break;
12025 }
12026
12027 return this_type;
12028 }
12029
12030 /* See if we can figure out if the class lives in a namespace. We do
12031 this by looking for a member function; its demangled name will
12032 contain namespace info, if there is any.
12033 Return the computed name or NULL.
12034 Space for the result is allocated on the objfile's obstack.
12035 This is the full-die version of guess_partial_die_structure_name.
12036 In this case we know DIE has no useful parent. */
12037
12038 static char *
12039 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12040 {
12041 struct die_info *spec_die;
12042 struct dwarf2_cu *spec_cu;
12043 struct die_info *child;
12044
12045 spec_cu = cu;
12046 spec_die = die_specification (die, &spec_cu);
12047 if (spec_die != NULL)
12048 {
12049 die = spec_die;
12050 cu = spec_cu;
12051 }
12052
12053 for (child = die->child;
12054 child != NULL;
12055 child = child->sibling)
12056 {
12057 if (child->tag == DW_TAG_subprogram)
12058 {
12059 struct attribute *attr;
12060
12061 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12062 if (attr == NULL)
12063 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12064 if (attr != NULL)
12065 {
12066 char *actual_name
12067 = language_class_name_from_physname (cu->language_defn,
12068 DW_STRING (attr));
12069 char *name = NULL;
12070
12071 if (actual_name != NULL)
12072 {
12073 char *die_name = dwarf2_name (die, cu);
12074
12075 if (die_name != NULL
12076 && strcmp (die_name, actual_name) != 0)
12077 {
12078 /* Strip off the class name from the full name.
12079 We want the prefix. */
12080 int die_name_len = strlen (die_name);
12081 int actual_name_len = strlen (actual_name);
12082
12083 /* Test for '::' as a sanity check. */
12084 if (actual_name_len > die_name_len + 2
12085 && actual_name[actual_name_len
12086 - die_name_len - 1] == ':')
12087 name =
12088 obsavestring (actual_name,
12089 actual_name_len - die_name_len - 2,
12090 &cu->objfile->objfile_obstack);
12091 }
12092 }
12093 xfree (actual_name);
12094 return name;
12095 }
12096 }
12097 }
12098
12099 return NULL;
12100 }
12101
12102 /* Return the name of the namespace/class that DIE is defined within,
12103 or "" if we can't tell. The caller should not xfree the result.
12104
12105 For example, if we're within the method foo() in the following
12106 code:
12107
12108 namespace N {
12109 class C {
12110 void foo () {
12111 }
12112 };
12113 }
12114
12115 then determine_prefix on foo's die will return "N::C". */
12116
12117 static char *
12118 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12119 {
12120 struct die_info *parent, *spec_die;
12121 struct dwarf2_cu *spec_cu;
12122 struct type *parent_type;
12123
12124 if (cu->language != language_cplus && cu->language != language_java
12125 && cu->language != language_fortran)
12126 return "";
12127
12128 /* We have to be careful in the presence of DW_AT_specification.
12129 For example, with GCC 3.4, given the code
12130
12131 namespace N {
12132 void foo() {
12133 // Definition of N::foo.
12134 }
12135 }
12136
12137 then we'll have a tree of DIEs like this:
12138
12139 1: DW_TAG_compile_unit
12140 2: DW_TAG_namespace // N
12141 3: DW_TAG_subprogram // declaration of N::foo
12142 4: DW_TAG_subprogram // definition of N::foo
12143 DW_AT_specification // refers to die #3
12144
12145 Thus, when processing die #4, we have to pretend that we're in
12146 the context of its DW_AT_specification, namely the contex of die
12147 #3. */
12148 spec_cu = cu;
12149 spec_die = die_specification (die, &spec_cu);
12150 if (spec_die == NULL)
12151 parent = die->parent;
12152 else
12153 {
12154 parent = spec_die->parent;
12155 cu = spec_cu;
12156 }
12157
12158 if (parent == NULL)
12159 return "";
12160 else if (parent->building_fullname)
12161 {
12162 const char *name;
12163 const char *parent_name;
12164
12165 /* It has been seen on RealView 2.2 built binaries,
12166 DW_TAG_template_type_param types actually _defined_ as
12167 children of the parent class:
12168
12169 enum E {};
12170 template class <class Enum> Class{};
12171 Class<enum E> class_e;
12172
12173 1: DW_TAG_class_type (Class)
12174 2: DW_TAG_enumeration_type (E)
12175 3: DW_TAG_enumerator (enum1:0)
12176 3: DW_TAG_enumerator (enum2:1)
12177 ...
12178 2: DW_TAG_template_type_param
12179 DW_AT_type DW_FORM_ref_udata (E)
12180
12181 Besides being broken debug info, it can put GDB into an
12182 infinite loop. Consider:
12183
12184 When we're building the full name for Class<E>, we'll start
12185 at Class, and go look over its template type parameters,
12186 finding E. We'll then try to build the full name of E, and
12187 reach here. We're now trying to build the full name of E,
12188 and look over the parent DIE for containing scope. In the
12189 broken case, if we followed the parent DIE of E, we'd again
12190 find Class, and once again go look at its template type
12191 arguments, etc., etc. Simply don't consider such parent die
12192 as source-level parent of this die (it can't be, the language
12193 doesn't allow it), and break the loop here. */
12194 name = dwarf2_name (die, cu);
12195 parent_name = dwarf2_name (parent, cu);
12196 complaint (&symfile_complaints,
12197 _("template param type '%s' defined within parent '%s'"),
12198 name ? name : "<unknown>",
12199 parent_name ? parent_name : "<unknown>");
12200 return "";
12201 }
12202 else
12203 switch (parent->tag)
12204 {
12205 case DW_TAG_namespace:
12206 parent_type = read_type_die (parent, cu);
12207 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12208 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12209 Work around this problem here. */
12210 if (cu->language == language_cplus
12211 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12212 return "";
12213 /* We give a name to even anonymous namespaces. */
12214 return TYPE_TAG_NAME (parent_type);
12215 case DW_TAG_class_type:
12216 case DW_TAG_interface_type:
12217 case DW_TAG_structure_type:
12218 case DW_TAG_union_type:
12219 case DW_TAG_module:
12220 parent_type = read_type_die (parent, cu);
12221 if (TYPE_TAG_NAME (parent_type) != NULL)
12222 return TYPE_TAG_NAME (parent_type);
12223 else
12224 /* An anonymous structure is only allowed non-static data
12225 members; no typedefs, no member functions, et cetera.
12226 So it does not need a prefix. */
12227 return "";
12228 case DW_TAG_compile_unit:
12229 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12230 if (cu->language == language_cplus
12231 && dwarf2_per_objfile->types.asection != NULL
12232 && die->child != NULL
12233 && (die->tag == DW_TAG_class_type
12234 || die->tag == DW_TAG_structure_type
12235 || die->tag == DW_TAG_union_type))
12236 {
12237 char *name = guess_full_die_structure_name (die, cu);
12238 if (name != NULL)
12239 return name;
12240 }
12241 return "";
12242 default:
12243 return determine_prefix (parent, cu);
12244 }
12245 }
12246
12247 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12248 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12249 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12250 an obconcat, otherwise allocate storage for the result. The CU argument is
12251 used to determine the language and hence, the appropriate separator. */
12252
12253 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12254
12255 static char *
12256 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12257 int physname, struct dwarf2_cu *cu)
12258 {
12259 const char *lead = "";
12260 const char *sep;
12261
12262 if (suffix == NULL || suffix[0] == '\0'
12263 || prefix == NULL || prefix[0] == '\0')
12264 sep = "";
12265 else if (cu->language == language_java)
12266 sep = ".";
12267 else if (cu->language == language_fortran && physname)
12268 {
12269 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12270 DW_AT_MIPS_linkage_name is preferred and used instead. */
12271
12272 lead = "__";
12273 sep = "_MOD_";
12274 }
12275 else
12276 sep = "::";
12277
12278 if (prefix == NULL)
12279 prefix = "";
12280 if (suffix == NULL)
12281 suffix = "";
12282
12283 if (obs == NULL)
12284 {
12285 char *retval
12286 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12287
12288 strcpy (retval, lead);
12289 strcat (retval, prefix);
12290 strcat (retval, sep);
12291 strcat (retval, suffix);
12292 return retval;
12293 }
12294 else
12295 {
12296 /* We have an obstack. */
12297 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12298 }
12299 }
12300
12301 /* Return sibling of die, NULL if no sibling. */
12302
12303 static struct die_info *
12304 sibling_die (struct die_info *die)
12305 {
12306 return die->sibling;
12307 }
12308
12309 /* Get name of a die, return NULL if not found. */
12310
12311 static char *
12312 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12313 struct obstack *obstack)
12314 {
12315 if (name && cu->language == language_cplus)
12316 {
12317 char *canon_name = cp_canonicalize_string (name);
12318
12319 if (canon_name != NULL)
12320 {
12321 if (strcmp (canon_name, name) != 0)
12322 name = obsavestring (canon_name, strlen (canon_name),
12323 obstack);
12324 xfree (canon_name);
12325 }
12326 }
12327
12328 return name;
12329 }
12330
12331 /* Get name of a die, return NULL if not found. */
12332
12333 static char *
12334 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12335 {
12336 struct attribute *attr;
12337
12338 attr = dwarf2_attr (die, DW_AT_name, cu);
12339 if ((!attr || !DW_STRING (attr))
12340 && die->tag != DW_TAG_class_type
12341 && die->tag != DW_TAG_interface_type
12342 && die->tag != DW_TAG_structure_type
12343 && die->tag != DW_TAG_union_type)
12344 return NULL;
12345
12346 switch (die->tag)
12347 {
12348 case DW_TAG_compile_unit:
12349 /* Compilation units have a DW_AT_name that is a filename, not
12350 a source language identifier. */
12351 case DW_TAG_enumeration_type:
12352 case DW_TAG_enumerator:
12353 /* These tags always have simple identifiers already; no need
12354 to canonicalize them. */
12355 return DW_STRING (attr);
12356
12357 case DW_TAG_subprogram:
12358 /* Java constructors will all be named "<init>", so return
12359 the class name when we see this special case. */
12360 if (cu->language == language_java
12361 && DW_STRING (attr) != NULL
12362 && strcmp (DW_STRING (attr), "<init>") == 0)
12363 {
12364 struct dwarf2_cu *spec_cu = cu;
12365 struct die_info *spec_die;
12366
12367 /* GCJ will output '<init>' for Java constructor names.
12368 For this special case, return the name of the parent class. */
12369
12370 /* GCJ may output suprogram DIEs with AT_specification set.
12371 If so, use the name of the specified DIE. */
12372 spec_die = die_specification (die, &spec_cu);
12373 if (spec_die != NULL)
12374 return dwarf2_name (spec_die, spec_cu);
12375
12376 do
12377 {
12378 die = die->parent;
12379 if (die->tag == DW_TAG_class_type)
12380 return dwarf2_name (die, cu);
12381 }
12382 while (die->tag != DW_TAG_compile_unit);
12383 }
12384 break;
12385
12386 case DW_TAG_class_type:
12387 case DW_TAG_interface_type:
12388 case DW_TAG_structure_type:
12389 case DW_TAG_union_type:
12390 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12391 structures or unions. These were of the form "._%d" in GCC 4.1,
12392 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12393 and GCC 4.4. We work around this problem by ignoring these. */
12394 if (attr && DW_STRING (attr)
12395 && (strncmp (DW_STRING (attr), "._", 2) == 0
12396 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12397 return NULL;
12398
12399 /* GCC might emit a nameless typedef that has a linkage name. See
12400 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12401 if (!attr || DW_STRING (attr) == NULL)
12402 {
12403 char *demangled = NULL;
12404
12405 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12406 if (attr == NULL)
12407 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12408
12409 if (attr == NULL || DW_STRING (attr) == NULL)
12410 return NULL;
12411
12412 /* Avoid demangling DW_STRING (attr) the second time on a second
12413 call for the same DIE. */
12414 if (!DW_STRING_IS_CANONICAL (attr))
12415 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12416
12417 if (demangled)
12418 {
12419 /* FIXME: we already did this for the partial symbol... */
12420 DW_STRING (attr)
12421 = obsavestring (demangled, strlen (demangled),
12422 &cu->objfile->objfile_obstack);
12423 DW_STRING_IS_CANONICAL (attr) = 1;
12424 xfree (demangled);
12425 }
12426 }
12427 break;
12428
12429 default:
12430 break;
12431 }
12432
12433 if (!DW_STRING_IS_CANONICAL (attr))
12434 {
12435 DW_STRING (attr)
12436 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12437 &cu->objfile->objfile_obstack);
12438 DW_STRING_IS_CANONICAL (attr) = 1;
12439 }
12440 return DW_STRING (attr);
12441 }
12442
12443 /* Return the die that this die in an extension of, or NULL if there
12444 is none. *EXT_CU is the CU containing DIE on input, and the CU
12445 containing the return value on output. */
12446
12447 static struct die_info *
12448 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12449 {
12450 struct attribute *attr;
12451
12452 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12453 if (attr == NULL)
12454 return NULL;
12455
12456 return follow_die_ref (die, attr, ext_cu);
12457 }
12458
12459 /* Convert a DIE tag into its string name. */
12460
12461 static char *
12462 dwarf_tag_name (unsigned tag)
12463 {
12464 switch (tag)
12465 {
12466 case DW_TAG_padding:
12467 return "DW_TAG_padding";
12468 case DW_TAG_array_type:
12469 return "DW_TAG_array_type";
12470 case DW_TAG_class_type:
12471 return "DW_TAG_class_type";
12472 case DW_TAG_entry_point:
12473 return "DW_TAG_entry_point";
12474 case DW_TAG_enumeration_type:
12475 return "DW_TAG_enumeration_type";
12476 case DW_TAG_formal_parameter:
12477 return "DW_TAG_formal_parameter";
12478 case DW_TAG_imported_declaration:
12479 return "DW_TAG_imported_declaration";
12480 case DW_TAG_label:
12481 return "DW_TAG_label";
12482 case DW_TAG_lexical_block:
12483 return "DW_TAG_lexical_block";
12484 case DW_TAG_member:
12485 return "DW_TAG_member";
12486 case DW_TAG_pointer_type:
12487 return "DW_TAG_pointer_type";
12488 case DW_TAG_reference_type:
12489 return "DW_TAG_reference_type";
12490 case DW_TAG_compile_unit:
12491 return "DW_TAG_compile_unit";
12492 case DW_TAG_string_type:
12493 return "DW_TAG_string_type";
12494 case DW_TAG_structure_type:
12495 return "DW_TAG_structure_type";
12496 case DW_TAG_subroutine_type:
12497 return "DW_TAG_subroutine_type";
12498 case DW_TAG_typedef:
12499 return "DW_TAG_typedef";
12500 case DW_TAG_union_type:
12501 return "DW_TAG_union_type";
12502 case DW_TAG_unspecified_parameters:
12503 return "DW_TAG_unspecified_parameters";
12504 case DW_TAG_variant:
12505 return "DW_TAG_variant";
12506 case DW_TAG_common_block:
12507 return "DW_TAG_common_block";
12508 case DW_TAG_common_inclusion:
12509 return "DW_TAG_common_inclusion";
12510 case DW_TAG_inheritance:
12511 return "DW_TAG_inheritance";
12512 case DW_TAG_inlined_subroutine:
12513 return "DW_TAG_inlined_subroutine";
12514 case DW_TAG_module:
12515 return "DW_TAG_module";
12516 case DW_TAG_ptr_to_member_type:
12517 return "DW_TAG_ptr_to_member_type";
12518 case DW_TAG_set_type:
12519 return "DW_TAG_set_type";
12520 case DW_TAG_subrange_type:
12521 return "DW_TAG_subrange_type";
12522 case DW_TAG_with_stmt:
12523 return "DW_TAG_with_stmt";
12524 case DW_TAG_access_declaration:
12525 return "DW_TAG_access_declaration";
12526 case DW_TAG_base_type:
12527 return "DW_TAG_base_type";
12528 case DW_TAG_catch_block:
12529 return "DW_TAG_catch_block";
12530 case DW_TAG_const_type:
12531 return "DW_TAG_const_type";
12532 case DW_TAG_constant:
12533 return "DW_TAG_constant";
12534 case DW_TAG_enumerator:
12535 return "DW_TAG_enumerator";
12536 case DW_TAG_file_type:
12537 return "DW_TAG_file_type";
12538 case DW_TAG_friend:
12539 return "DW_TAG_friend";
12540 case DW_TAG_namelist:
12541 return "DW_TAG_namelist";
12542 case DW_TAG_namelist_item:
12543 return "DW_TAG_namelist_item";
12544 case DW_TAG_packed_type:
12545 return "DW_TAG_packed_type";
12546 case DW_TAG_subprogram:
12547 return "DW_TAG_subprogram";
12548 case DW_TAG_template_type_param:
12549 return "DW_TAG_template_type_param";
12550 case DW_TAG_template_value_param:
12551 return "DW_TAG_template_value_param";
12552 case DW_TAG_thrown_type:
12553 return "DW_TAG_thrown_type";
12554 case DW_TAG_try_block:
12555 return "DW_TAG_try_block";
12556 case DW_TAG_variant_part:
12557 return "DW_TAG_variant_part";
12558 case DW_TAG_variable:
12559 return "DW_TAG_variable";
12560 case DW_TAG_volatile_type:
12561 return "DW_TAG_volatile_type";
12562 case DW_TAG_dwarf_procedure:
12563 return "DW_TAG_dwarf_procedure";
12564 case DW_TAG_restrict_type:
12565 return "DW_TAG_restrict_type";
12566 case DW_TAG_interface_type:
12567 return "DW_TAG_interface_type";
12568 case DW_TAG_namespace:
12569 return "DW_TAG_namespace";
12570 case DW_TAG_imported_module:
12571 return "DW_TAG_imported_module";
12572 case DW_TAG_unspecified_type:
12573 return "DW_TAG_unspecified_type";
12574 case DW_TAG_partial_unit:
12575 return "DW_TAG_partial_unit";
12576 case DW_TAG_imported_unit:
12577 return "DW_TAG_imported_unit";
12578 case DW_TAG_condition:
12579 return "DW_TAG_condition";
12580 case DW_TAG_shared_type:
12581 return "DW_TAG_shared_type";
12582 case DW_TAG_type_unit:
12583 return "DW_TAG_type_unit";
12584 case DW_TAG_MIPS_loop:
12585 return "DW_TAG_MIPS_loop";
12586 case DW_TAG_HP_array_descriptor:
12587 return "DW_TAG_HP_array_descriptor";
12588 case DW_TAG_format_label:
12589 return "DW_TAG_format_label";
12590 case DW_TAG_function_template:
12591 return "DW_TAG_function_template";
12592 case DW_TAG_class_template:
12593 return "DW_TAG_class_template";
12594 case DW_TAG_GNU_BINCL:
12595 return "DW_TAG_GNU_BINCL";
12596 case DW_TAG_GNU_EINCL:
12597 return "DW_TAG_GNU_EINCL";
12598 case DW_TAG_upc_shared_type:
12599 return "DW_TAG_upc_shared_type";
12600 case DW_TAG_upc_strict_type:
12601 return "DW_TAG_upc_strict_type";
12602 case DW_TAG_upc_relaxed_type:
12603 return "DW_TAG_upc_relaxed_type";
12604 case DW_TAG_PGI_kanji_type:
12605 return "DW_TAG_PGI_kanji_type";
12606 case DW_TAG_PGI_interface_block:
12607 return "DW_TAG_PGI_interface_block";
12608 default:
12609 return "DW_TAG_<unknown>";
12610 }
12611 }
12612
12613 /* Convert a DWARF attribute code into its string name. */
12614
12615 static char *
12616 dwarf_attr_name (unsigned attr)
12617 {
12618 switch (attr)
12619 {
12620 case DW_AT_sibling:
12621 return "DW_AT_sibling";
12622 case DW_AT_location:
12623 return "DW_AT_location";
12624 case DW_AT_name:
12625 return "DW_AT_name";
12626 case DW_AT_ordering:
12627 return "DW_AT_ordering";
12628 case DW_AT_subscr_data:
12629 return "DW_AT_subscr_data";
12630 case DW_AT_byte_size:
12631 return "DW_AT_byte_size";
12632 case DW_AT_bit_offset:
12633 return "DW_AT_bit_offset";
12634 case DW_AT_bit_size:
12635 return "DW_AT_bit_size";
12636 case DW_AT_element_list:
12637 return "DW_AT_element_list";
12638 case DW_AT_stmt_list:
12639 return "DW_AT_stmt_list";
12640 case DW_AT_low_pc:
12641 return "DW_AT_low_pc";
12642 case DW_AT_high_pc:
12643 return "DW_AT_high_pc";
12644 case DW_AT_language:
12645 return "DW_AT_language";
12646 case DW_AT_member:
12647 return "DW_AT_member";
12648 case DW_AT_discr:
12649 return "DW_AT_discr";
12650 case DW_AT_discr_value:
12651 return "DW_AT_discr_value";
12652 case DW_AT_visibility:
12653 return "DW_AT_visibility";
12654 case DW_AT_import:
12655 return "DW_AT_import";
12656 case DW_AT_string_length:
12657 return "DW_AT_string_length";
12658 case DW_AT_common_reference:
12659 return "DW_AT_common_reference";
12660 case DW_AT_comp_dir:
12661 return "DW_AT_comp_dir";
12662 case DW_AT_const_value:
12663 return "DW_AT_const_value";
12664 case DW_AT_containing_type:
12665 return "DW_AT_containing_type";
12666 case DW_AT_default_value:
12667 return "DW_AT_default_value";
12668 case DW_AT_inline:
12669 return "DW_AT_inline";
12670 case DW_AT_is_optional:
12671 return "DW_AT_is_optional";
12672 case DW_AT_lower_bound:
12673 return "DW_AT_lower_bound";
12674 case DW_AT_producer:
12675 return "DW_AT_producer";
12676 case DW_AT_prototyped:
12677 return "DW_AT_prototyped";
12678 case DW_AT_return_addr:
12679 return "DW_AT_return_addr";
12680 case DW_AT_start_scope:
12681 return "DW_AT_start_scope";
12682 case DW_AT_bit_stride:
12683 return "DW_AT_bit_stride";
12684 case DW_AT_upper_bound:
12685 return "DW_AT_upper_bound";
12686 case DW_AT_abstract_origin:
12687 return "DW_AT_abstract_origin";
12688 case DW_AT_accessibility:
12689 return "DW_AT_accessibility";
12690 case DW_AT_address_class:
12691 return "DW_AT_address_class";
12692 case DW_AT_artificial:
12693 return "DW_AT_artificial";
12694 case DW_AT_base_types:
12695 return "DW_AT_base_types";
12696 case DW_AT_calling_convention:
12697 return "DW_AT_calling_convention";
12698 case DW_AT_count:
12699 return "DW_AT_count";
12700 case DW_AT_data_member_location:
12701 return "DW_AT_data_member_location";
12702 case DW_AT_decl_column:
12703 return "DW_AT_decl_column";
12704 case DW_AT_decl_file:
12705 return "DW_AT_decl_file";
12706 case DW_AT_decl_line:
12707 return "DW_AT_decl_line";
12708 case DW_AT_declaration:
12709 return "DW_AT_declaration";
12710 case DW_AT_discr_list:
12711 return "DW_AT_discr_list";
12712 case DW_AT_encoding:
12713 return "DW_AT_encoding";
12714 case DW_AT_external:
12715 return "DW_AT_external";
12716 case DW_AT_frame_base:
12717 return "DW_AT_frame_base";
12718 case DW_AT_friend:
12719 return "DW_AT_friend";
12720 case DW_AT_identifier_case:
12721 return "DW_AT_identifier_case";
12722 case DW_AT_macro_info:
12723 return "DW_AT_macro_info";
12724 case DW_AT_namelist_items:
12725 return "DW_AT_namelist_items";
12726 case DW_AT_priority:
12727 return "DW_AT_priority";
12728 case DW_AT_segment:
12729 return "DW_AT_segment";
12730 case DW_AT_specification:
12731 return "DW_AT_specification";
12732 case DW_AT_static_link:
12733 return "DW_AT_static_link";
12734 case DW_AT_type:
12735 return "DW_AT_type";
12736 case DW_AT_use_location:
12737 return "DW_AT_use_location";
12738 case DW_AT_variable_parameter:
12739 return "DW_AT_variable_parameter";
12740 case DW_AT_virtuality:
12741 return "DW_AT_virtuality";
12742 case DW_AT_vtable_elem_location:
12743 return "DW_AT_vtable_elem_location";
12744 /* DWARF 3 values. */
12745 case DW_AT_allocated:
12746 return "DW_AT_allocated";
12747 case DW_AT_associated:
12748 return "DW_AT_associated";
12749 case DW_AT_data_location:
12750 return "DW_AT_data_location";
12751 case DW_AT_byte_stride:
12752 return "DW_AT_byte_stride";
12753 case DW_AT_entry_pc:
12754 return "DW_AT_entry_pc";
12755 case DW_AT_use_UTF8:
12756 return "DW_AT_use_UTF8";
12757 case DW_AT_extension:
12758 return "DW_AT_extension";
12759 case DW_AT_ranges:
12760 return "DW_AT_ranges";
12761 case DW_AT_trampoline:
12762 return "DW_AT_trampoline";
12763 case DW_AT_call_column:
12764 return "DW_AT_call_column";
12765 case DW_AT_call_file:
12766 return "DW_AT_call_file";
12767 case DW_AT_call_line:
12768 return "DW_AT_call_line";
12769 case DW_AT_description:
12770 return "DW_AT_description";
12771 case DW_AT_binary_scale:
12772 return "DW_AT_binary_scale";
12773 case DW_AT_decimal_scale:
12774 return "DW_AT_decimal_scale";
12775 case DW_AT_small:
12776 return "DW_AT_small";
12777 case DW_AT_decimal_sign:
12778 return "DW_AT_decimal_sign";
12779 case DW_AT_digit_count:
12780 return "DW_AT_digit_count";
12781 case DW_AT_picture_string:
12782 return "DW_AT_picture_string";
12783 case DW_AT_mutable:
12784 return "DW_AT_mutable";
12785 case DW_AT_threads_scaled:
12786 return "DW_AT_threads_scaled";
12787 case DW_AT_explicit:
12788 return "DW_AT_explicit";
12789 case DW_AT_object_pointer:
12790 return "DW_AT_object_pointer";
12791 case DW_AT_endianity:
12792 return "DW_AT_endianity";
12793 case DW_AT_elemental:
12794 return "DW_AT_elemental";
12795 case DW_AT_pure:
12796 return "DW_AT_pure";
12797 case DW_AT_recursive:
12798 return "DW_AT_recursive";
12799 /* DWARF 4 values. */
12800 case DW_AT_signature:
12801 return "DW_AT_signature";
12802 case DW_AT_linkage_name:
12803 return "DW_AT_linkage_name";
12804 /* SGI/MIPS extensions. */
12805 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12806 case DW_AT_MIPS_fde:
12807 return "DW_AT_MIPS_fde";
12808 #endif
12809 case DW_AT_MIPS_loop_begin:
12810 return "DW_AT_MIPS_loop_begin";
12811 case DW_AT_MIPS_tail_loop_begin:
12812 return "DW_AT_MIPS_tail_loop_begin";
12813 case DW_AT_MIPS_epilog_begin:
12814 return "DW_AT_MIPS_epilog_begin";
12815 case DW_AT_MIPS_loop_unroll_factor:
12816 return "DW_AT_MIPS_loop_unroll_factor";
12817 case DW_AT_MIPS_software_pipeline_depth:
12818 return "DW_AT_MIPS_software_pipeline_depth";
12819 case DW_AT_MIPS_linkage_name:
12820 return "DW_AT_MIPS_linkage_name";
12821 case DW_AT_MIPS_stride:
12822 return "DW_AT_MIPS_stride";
12823 case DW_AT_MIPS_abstract_name:
12824 return "DW_AT_MIPS_abstract_name";
12825 case DW_AT_MIPS_clone_origin:
12826 return "DW_AT_MIPS_clone_origin";
12827 case DW_AT_MIPS_has_inlines:
12828 return "DW_AT_MIPS_has_inlines";
12829 /* HP extensions. */
12830 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12831 case DW_AT_HP_block_index:
12832 return "DW_AT_HP_block_index";
12833 #endif
12834 case DW_AT_HP_unmodifiable:
12835 return "DW_AT_HP_unmodifiable";
12836 case DW_AT_HP_actuals_stmt_list:
12837 return "DW_AT_HP_actuals_stmt_list";
12838 case DW_AT_HP_proc_per_section:
12839 return "DW_AT_HP_proc_per_section";
12840 case DW_AT_HP_raw_data_ptr:
12841 return "DW_AT_HP_raw_data_ptr";
12842 case DW_AT_HP_pass_by_reference:
12843 return "DW_AT_HP_pass_by_reference";
12844 case DW_AT_HP_opt_level:
12845 return "DW_AT_HP_opt_level";
12846 case DW_AT_HP_prof_version_id:
12847 return "DW_AT_HP_prof_version_id";
12848 case DW_AT_HP_opt_flags:
12849 return "DW_AT_HP_opt_flags";
12850 case DW_AT_HP_cold_region_low_pc:
12851 return "DW_AT_HP_cold_region_low_pc";
12852 case DW_AT_HP_cold_region_high_pc:
12853 return "DW_AT_HP_cold_region_high_pc";
12854 case DW_AT_HP_all_variables_modifiable:
12855 return "DW_AT_HP_all_variables_modifiable";
12856 case DW_AT_HP_linkage_name:
12857 return "DW_AT_HP_linkage_name";
12858 case DW_AT_HP_prof_flags:
12859 return "DW_AT_HP_prof_flags";
12860 /* GNU extensions. */
12861 case DW_AT_sf_names:
12862 return "DW_AT_sf_names";
12863 case DW_AT_src_info:
12864 return "DW_AT_src_info";
12865 case DW_AT_mac_info:
12866 return "DW_AT_mac_info";
12867 case DW_AT_src_coords:
12868 return "DW_AT_src_coords";
12869 case DW_AT_body_begin:
12870 return "DW_AT_body_begin";
12871 case DW_AT_body_end:
12872 return "DW_AT_body_end";
12873 case DW_AT_GNU_vector:
12874 return "DW_AT_GNU_vector";
12875 case DW_AT_GNU_odr_signature:
12876 return "DW_AT_GNU_odr_signature";
12877 /* VMS extensions. */
12878 case DW_AT_VMS_rtnbeg_pd_address:
12879 return "DW_AT_VMS_rtnbeg_pd_address";
12880 /* UPC extension. */
12881 case DW_AT_upc_threads_scaled:
12882 return "DW_AT_upc_threads_scaled";
12883 /* PGI (STMicroelectronics) extensions. */
12884 case DW_AT_PGI_lbase:
12885 return "DW_AT_PGI_lbase";
12886 case DW_AT_PGI_soffset:
12887 return "DW_AT_PGI_soffset";
12888 case DW_AT_PGI_lstride:
12889 return "DW_AT_PGI_lstride";
12890 default:
12891 return "DW_AT_<unknown>";
12892 }
12893 }
12894
12895 /* Convert a DWARF value form code into its string name. */
12896
12897 static char *
12898 dwarf_form_name (unsigned form)
12899 {
12900 switch (form)
12901 {
12902 case DW_FORM_addr:
12903 return "DW_FORM_addr";
12904 case DW_FORM_block2:
12905 return "DW_FORM_block2";
12906 case DW_FORM_block4:
12907 return "DW_FORM_block4";
12908 case DW_FORM_data2:
12909 return "DW_FORM_data2";
12910 case DW_FORM_data4:
12911 return "DW_FORM_data4";
12912 case DW_FORM_data8:
12913 return "DW_FORM_data8";
12914 case DW_FORM_string:
12915 return "DW_FORM_string";
12916 case DW_FORM_block:
12917 return "DW_FORM_block";
12918 case DW_FORM_block1:
12919 return "DW_FORM_block1";
12920 case DW_FORM_data1:
12921 return "DW_FORM_data1";
12922 case DW_FORM_flag:
12923 return "DW_FORM_flag";
12924 case DW_FORM_sdata:
12925 return "DW_FORM_sdata";
12926 case DW_FORM_strp:
12927 return "DW_FORM_strp";
12928 case DW_FORM_udata:
12929 return "DW_FORM_udata";
12930 case DW_FORM_ref_addr:
12931 return "DW_FORM_ref_addr";
12932 case DW_FORM_ref1:
12933 return "DW_FORM_ref1";
12934 case DW_FORM_ref2:
12935 return "DW_FORM_ref2";
12936 case DW_FORM_ref4:
12937 return "DW_FORM_ref4";
12938 case DW_FORM_ref8:
12939 return "DW_FORM_ref8";
12940 case DW_FORM_ref_udata:
12941 return "DW_FORM_ref_udata";
12942 case DW_FORM_indirect:
12943 return "DW_FORM_indirect";
12944 case DW_FORM_sec_offset:
12945 return "DW_FORM_sec_offset";
12946 case DW_FORM_exprloc:
12947 return "DW_FORM_exprloc";
12948 case DW_FORM_flag_present:
12949 return "DW_FORM_flag_present";
12950 case DW_FORM_ref_sig8:
12951 return "DW_FORM_ref_sig8";
12952 default:
12953 return "DW_FORM_<unknown>";
12954 }
12955 }
12956
12957 /* Convert a DWARF stack opcode into its string name. */
12958
12959 const char *
12960 dwarf_stack_op_name (unsigned op)
12961 {
12962 switch (op)
12963 {
12964 case DW_OP_addr:
12965 return "DW_OP_addr";
12966 case DW_OP_deref:
12967 return "DW_OP_deref";
12968 case DW_OP_const1u:
12969 return "DW_OP_const1u";
12970 case DW_OP_const1s:
12971 return "DW_OP_const1s";
12972 case DW_OP_const2u:
12973 return "DW_OP_const2u";
12974 case DW_OP_const2s:
12975 return "DW_OP_const2s";
12976 case DW_OP_const4u:
12977 return "DW_OP_const4u";
12978 case DW_OP_const4s:
12979 return "DW_OP_const4s";
12980 case DW_OP_const8u:
12981 return "DW_OP_const8u";
12982 case DW_OP_const8s:
12983 return "DW_OP_const8s";
12984 case DW_OP_constu:
12985 return "DW_OP_constu";
12986 case DW_OP_consts:
12987 return "DW_OP_consts";
12988 case DW_OP_dup:
12989 return "DW_OP_dup";
12990 case DW_OP_drop:
12991 return "DW_OP_drop";
12992 case DW_OP_over:
12993 return "DW_OP_over";
12994 case DW_OP_pick:
12995 return "DW_OP_pick";
12996 case DW_OP_swap:
12997 return "DW_OP_swap";
12998 case DW_OP_rot:
12999 return "DW_OP_rot";
13000 case DW_OP_xderef:
13001 return "DW_OP_xderef";
13002 case DW_OP_abs:
13003 return "DW_OP_abs";
13004 case DW_OP_and:
13005 return "DW_OP_and";
13006 case DW_OP_div:
13007 return "DW_OP_div";
13008 case DW_OP_minus:
13009 return "DW_OP_minus";
13010 case DW_OP_mod:
13011 return "DW_OP_mod";
13012 case DW_OP_mul:
13013 return "DW_OP_mul";
13014 case DW_OP_neg:
13015 return "DW_OP_neg";
13016 case DW_OP_not:
13017 return "DW_OP_not";
13018 case DW_OP_or:
13019 return "DW_OP_or";
13020 case DW_OP_plus:
13021 return "DW_OP_plus";
13022 case DW_OP_plus_uconst:
13023 return "DW_OP_plus_uconst";
13024 case DW_OP_shl:
13025 return "DW_OP_shl";
13026 case DW_OP_shr:
13027 return "DW_OP_shr";
13028 case DW_OP_shra:
13029 return "DW_OP_shra";
13030 case DW_OP_xor:
13031 return "DW_OP_xor";
13032 case DW_OP_bra:
13033 return "DW_OP_bra";
13034 case DW_OP_eq:
13035 return "DW_OP_eq";
13036 case DW_OP_ge:
13037 return "DW_OP_ge";
13038 case DW_OP_gt:
13039 return "DW_OP_gt";
13040 case DW_OP_le:
13041 return "DW_OP_le";
13042 case DW_OP_lt:
13043 return "DW_OP_lt";
13044 case DW_OP_ne:
13045 return "DW_OP_ne";
13046 case DW_OP_skip:
13047 return "DW_OP_skip";
13048 case DW_OP_lit0:
13049 return "DW_OP_lit0";
13050 case DW_OP_lit1:
13051 return "DW_OP_lit1";
13052 case DW_OP_lit2:
13053 return "DW_OP_lit2";
13054 case DW_OP_lit3:
13055 return "DW_OP_lit3";
13056 case DW_OP_lit4:
13057 return "DW_OP_lit4";
13058 case DW_OP_lit5:
13059 return "DW_OP_lit5";
13060 case DW_OP_lit6:
13061 return "DW_OP_lit6";
13062 case DW_OP_lit7:
13063 return "DW_OP_lit7";
13064 case DW_OP_lit8:
13065 return "DW_OP_lit8";
13066 case DW_OP_lit9:
13067 return "DW_OP_lit9";
13068 case DW_OP_lit10:
13069 return "DW_OP_lit10";
13070 case DW_OP_lit11:
13071 return "DW_OP_lit11";
13072 case DW_OP_lit12:
13073 return "DW_OP_lit12";
13074 case DW_OP_lit13:
13075 return "DW_OP_lit13";
13076 case DW_OP_lit14:
13077 return "DW_OP_lit14";
13078 case DW_OP_lit15:
13079 return "DW_OP_lit15";
13080 case DW_OP_lit16:
13081 return "DW_OP_lit16";
13082 case DW_OP_lit17:
13083 return "DW_OP_lit17";
13084 case DW_OP_lit18:
13085 return "DW_OP_lit18";
13086 case DW_OP_lit19:
13087 return "DW_OP_lit19";
13088 case DW_OP_lit20:
13089 return "DW_OP_lit20";
13090 case DW_OP_lit21:
13091 return "DW_OP_lit21";
13092 case DW_OP_lit22:
13093 return "DW_OP_lit22";
13094 case DW_OP_lit23:
13095 return "DW_OP_lit23";
13096 case DW_OP_lit24:
13097 return "DW_OP_lit24";
13098 case DW_OP_lit25:
13099 return "DW_OP_lit25";
13100 case DW_OP_lit26:
13101 return "DW_OP_lit26";
13102 case DW_OP_lit27:
13103 return "DW_OP_lit27";
13104 case DW_OP_lit28:
13105 return "DW_OP_lit28";
13106 case DW_OP_lit29:
13107 return "DW_OP_lit29";
13108 case DW_OP_lit30:
13109 return "DW_OP_lit30";
13110 case DW_OP_lit31:
13111 return "DW_OP_lit31";
13112 case DW_OP_reg0:
13113 return "DW_OP_reg0";
13114 case DW_OP_reg1:
13115 return "DW_OP_reg1";
13116 case DW_OP_reg2:
13117 return "DW_OP_reg2";
13118 case DW_OP_reg3:
13119 return "DW_OP_reg3";
13120 case DW_OP_reg4:
13121 return "DW_OP_reg4";
13122 case DW_OP_reg5:
13123 return "DW_OP_reg5";
13124 case DW_OP_reg6:
13125 return "DW_OP_reg6";
13126 case DW_OP_reg7:
13127 return "DW_OP_reg7";
13128 case DW_OP_reg8:
13129 return "DW_OP_reg8";
13130 case DW_OP_reg9:
13131 return "DW_OP_reg9";
13132 case DW_OP_reg10:
13133 return "DW_OP_reg10";
13134 case DW_OP_reg11:
13135 return "DW_OP_reg11";
13136 case DW_OP_reg12:
13137 return "DW_OP_reg12";
13138 case DW_OP_reg13:
13139 return "DW_OP_reg13";
13140 case DW_OP_reg14:
13141 return "DW_OP_reg14";
13142 case DW_OP_reg15:
13143 return "DW_OP_reg15";
13144 case DW_OP_reg16:
13145 return "DW_OP_reg16";
13146 case DW_OP_reg17:
13147 return "DW_OP_reg17";
13148 case DW_OP_reg18:
13149 return "DW_OP_reg18";
13150 case DW_OP_reg19:
13151 return "DW_OP_reg19";
13152 case DW_OP_reg20:
13153 return "DW_OP_reg20";
13154 case DW_OP_reg21:
13155 return "DW_OP_reg21";
13156 case DW_OP_reg22:
13157 return "DW_OP_reg22";
13158 case DW_OP_reg23:
13159 return "DW_OP_reg23";
13160 case DW_OP_reg24:
13161 return "DW_OP_reg24";
13162 case DW_OP_reg25:
13163 return "DW_OP_reg25";
13164 case DW_OP_reg26:
13165 return "DW_OP_reg26";
13166 case DW_OP_reg27:
13167 return "DW_OP_reg27";
13168 case DW_OP_reg28:
13169 return "DW_OP_reg28";
13170 case DW_OP_reg29:
13171 return "DW_OP_reg29";
13172 case DW_OP_reg30:
13173 return "DW_OP_reg30";
13174 case DW_OP_reg31:
13175 return "DW_OP_reg31";
13176 case DW_OP_breg0:
13177 return "DW_OP_breg0";
13178 case DW_OP_breg1:
13179 return "DW_OP_breg1";
13180 case DW_OP_breg2:
13181 return "DW_OP_breg2";
13182 case DW_OP_breg3:
13183 return "DW_OP_breg3";
13184 case DW_OP_breg4:
13185 return "DW_OP_breg4";
13186 case DW_OP_breg5:
13187 return "DW_OP_breg5";
13188 case DW_OP_breg6:
13189 return "DW_OP_breg6";
13190 case DW_OP_breg7:
13191 return "DW_OP_breg7";
13192 case DW_OP_breg8:
13193 return "DW_OP_breg8";
13194 case DW_OP_breg9:
13195 return "DW_OP_breg9";
13196 case DW_OP_breg10:
13197 return "DW_OP_breg10";
13198 case DW_OP_breg11:
13199 return "DW_OP_breg11";
13200 case DW_OP_breg12:
13201 return "DW_OP_breg12";
13202 case DW_OP_breg13:
13203 return "DW_OP_breg13";
13204 case DW_OP_breg14:
13205 return "DW_OP_breg14";
13206 case DW_OP_breg15:
13207 return "DW_OP_breg15";
13208 case DW_OP_breg16:
13209 return "DW_OP_breg16";
13210 case DW_OP_breg17:
13211 return "DW_OP_breg17";
13212 case DW_OP_breg18:
13213 return "DW_OP_breg18";
13214 case DW_OP_breg19:
13215 return "DW_OP_breg19";
13216 case DW_OP_breg20:
13217 return "DW_OP_breg20";
13218 case DW_OP_breg21:
13219 return "DW_OP_breg21";
13220 case DW_OP_breg22:
13221 return "DW_OP_breg22";
13222 case DW_OP_breg23:
13223 return "DW_OP_breg23";
13224 case DW_OP_breg24:
13225 return "DW_OP_breg24";
13226 case DW_OP_breg25:
13227 return "DW_OP_breg25";
13228 case DW_OP_breg26:
13229 return "DW_OP_breg26";
13230 case DW_OP_breg27:
13231 return "DW_OP_breg27";
13232 case DW_OP_breg28:
13233 return "DW_OP_breg28";
13234 case DW_OP_breg29:
13235 return "DW_OP_breg29";
13236 case DW_OP_breg30:
13237 return "DW_OP_breg30";
13238 case DW_OP_breg31:
13239 return "DW_OP_breg31";
13240 case DW_OP_regx:
13241 return "DW_OP_regx";
13242 case DW_OP_fbreg:
13243 return "DW_OP_fbreg";
13244 case DW_OP_bregx:
13245 return "DW_OP_bregx";
13246 case DW_OP_piece:
13247 return "DW_OP_piece";
13248 case DW_OP_deref_size:
13249 return "DW_OP_deref_size";
13250 case DW_OP_xderef_size:
13251 return "DW_OP_xderef_size";
13252 case DW_OP_nop:
13253 return "DW_OP_nop";
13254 /* DWARF 3 extensions. */
13255 case DW_OP_push_object_address:
13256 return "DW_OP_push_object_address";
13257 case DW_OP_call2:
13258 return "DW_OP_call2";
13259 case DW_OP_call4:
13260 return "DW_OP_call4";
13261 case DW_OP_call_ref:
13262 return "DW_OP_call_ref";
13263 case DW_OP_form_tls_address:
13264 return "DW_OP_form_tls_address";
13265 case DW_OP_call_frame_cfa:
13266 return "DW_OP_call_frame_cfa";
13267 case DW_OP_bit_piece:
13268 return "DW_OP_bit_piece";
13269 /* DWARF 4 extensions. */
13270 case DW_OP_implicit_value:
13271 return "DW_OP_implicit_value";
13272 case DW_OP_stack_value:
13273 return "DW_OP_stack_value";
13274 /* GNU extensions. */
13275 case DW_OP_GNU_push_tls_address:
13276 return "DW_OP_GNU_push_tls_address";
13277 case DW_OP_GNU_uninit:
13278 return "DW_OP_GNU_uninit";
13279 case DW_OP_GNU_implicit_pointer:
13280 return "DW_OP_GNU_implicit_pointer";
13281 case DW_OP_GNU_entry_value:
13282 return "DW_OP_GNU_entry_value";
13283 case DW_OP_GNU_const_type:
13284 return "DW_OP_GNU_const_type";
13285 case DW_OP_GNU_regval_type:
13286 return "DW_OP_GNU_regval_type";
13287 case DW_OP_GNU_deref_type:
13288 return "DW_OP_GNU_deref_type";
13289 case DW_OP_GNU_convert:
13290 return "DW_OP_GNU_convert";
13291 case DW_OP_GNU_reinterpret:
13292 return "DW_OP_GNU_reinterpret";
13293 default:
13294 return NULL;
13295 }
13296 }
13297
13298 static char *
13299 dwarf_bool_name (unsigned mybool)
13300 {
13301 if (mybool)
13302 return "TRUE";
13303 else
13304 return "FALSE";
13305 }
13306
13307 /* Convert a DWARF type code into its string name. */
13308
13309 static char *
13310 dwarf_type_encoding_name (unsigned enc)
13311 {
13312 switch (enc)
13313 {
13314 case DW_ATE_void:
13315 return "DW_ATE_void";
13316 case DW_ATE_address:
13317 return "DW_ATE_address";
13318 case DW_ATE_boolean:
13319 return "DW_ATE_boolean";
13320 case DW_ATE_complex_float:
13321 return "DW_ATE_complex_float";
13322 case DW_ATE_float:
13323 return "DW_ATE_float";
13324 case DW_ATE_signed:
13325 return "DW_ATE_signed";
13326 case DW_ATE_signed_char:
13327 return "DW_ATE_signed_char";
13328 case DW_ATE_unsigned:
13329 return "DW_ATE_unsigned";
13330 case DW_ATE_unsigned_char:
13331 return "DW_ATE_unsigned_char";
13332 /* DWARF 3. */
13333 case DW_ATE_imaginary_float:
13334 return "DW_ATE_imaginary_float";
13335 case DW_ATE_packed_decimal:
13336 return "DW_ATE_packed_decimal";
13337 case DW_ATE_numeric_string:
13338 return "DW_ATE_numeric_string";
13339 case DW_ATE_edited:
13340 return "DW_ATE_edited";
13341 case DW_ATE_signed_fixed:
13342 return "DW_ATE_signed_fixed";
13343 case DW_ATE_unsigned_fixed:
13344 return "DW_ATE_unsigned_fixed";
13345 case DW_ATE_decimal_float:
13346 return "DW_ATE_decimal_float";
13347 /* DWARF 4. */
13348 case DW_ATE_UTF:
13349 return "DW_ATE_UTF";
13350 /* HP extensions. */
13351 case DW_ATE_HP_float80:
13352 return "DW_ATE_HP_float80";
13353 case DW_ATE_HP_complex_float80:
13354 return "DW_ATE_HP_complex_float80";
13355 case DW_ATE_HP_float128:
13356 return "DW_ATE_HP_float128";
13357 case DW_ATE_HP_complex_float128:
13358 return "DW_ATE_HP_complex_float128";
13359 case DW_ATE_HP_floathpintel:
13360 return "DW_ATE_HP_floathpintel";
13361 case DW_ATE_HP_imaginary_float80:
13362 return "DW_ATE_HP_imaginary_float80";
13363 case DW_ATE_HP_imaginary_float128:
13364 return "DW_ATE_HP_imaginary_float128";
13365 default:
13366 return "DW_ATE_<unknown>";
13367 }
13368 }
13369
13370 /* Convert a DWARF call frame info operation to its string name. */
13371
13372 #if 0
13373 static char *
13374 dwarf_cfi_name (unsigned cfi_opc)
13375 {
13376 switch (cfi_opc)
13377 {
13378 case DW_CFA_advance_loc:
13379 return "DW_CFA_advance_loc";
13380 case DW_CFA_offset:
13381 return "DW_CFA_offset";
13382 case DW_CFA_restore:
13383 return "DW_CFA_restore";
13384 case DW_CFA_nop:
13385 return "DW_CFA_nop";
13386 case DW_CFA_set_loc:
13387 return "DW_CFA_set_loc";
13388 case DW_CFA_advance_loc1:
13389 return "DW_CFA_advance_loc1";
13390 case DW_CFA_advance_loc2:
13391 return "DW_CFA_advance_loc2";
13392 case DW_CFA_advance_loc4:
13393 return "DW_CFA_advance_loc4";
13394 case DW_CFA_offset_extended:
13395 return "DW_CFA_offset_extended";
13396 case DW_CFA_restore_extended:
13397 return "DW_CFA_restore_extended";
13398 case DW_CFA_undefined:
13399 return "DW_CFA_undefined";
13400 case DW_CFA_same_value:
13401 return "DW_CFA_same_value";
13402 case DW_CFA_register:
13403 return "DW_CFA_register";
13404 case DW_CFA_remember_state:
13405 return "DW_CFA_remember_state";
13406 case DW_CFA_restore_state:
13407 return "DW_CFA_restore_state";
13408 case DW_CFA_def_cfa:
13409 return "DW_CFA_def_cfa";
13410 case DW_CFA_def_cfa_register:
13411 return "DW_CFA_def_cfa_register";
13412 case DW_CFA_def_cfa_offset:
13413 return "DW_CFA_def_cfa_offset";
13414 /* DWARF 3. */
13415 case DW_CFA_def_cfa_expression:
13416 return "DW_CFA_def_cfa_expression";
13417 case DW_CFA_expression:
13418 return "DW_CFA_expression";
13419 case DW_CFA_offset_extended_sf:
13420 return "DW_CFA_offset_extended_sf";
13421 case DW_CFA_def_cfa_sf:
13422 return "DW_CFA_def_cfa_sf";
13423 case DW_CFA_def_cfa_offset_sf:
13424 return "DW_CFA_def_cfa_offset_sf";
13425 case DW_CFA_val_offset:
13426 return "DW_CFA_val_offset";
13427 case DW_CFA_val_offset_sf:
13428 return "DW_CFA_val_offset_sf";
13429 case DW_CFA_val_expression:
13430 return "DW_CFA_val_expression";
13431 /* SGI/MIPS specific. */
13432 case DW_CFA_MIPS_advance_loc8:
13433 return "DW_CFA_MIPS_advance_loc8";
13434 /* GNU extensions. */
13435 case DW_CFA_GNU_window_save:
13436 return "DW_CFA_GNU_window_save";
13437 case DW_CFA_GNU_args_size:
13438 return "DW_CFA_GNU_args_size";
13439 case DW_CFA_GNU_negative_offset_extended:
13440 return "DW_CFA_GNU_negative_offset_extended";
13441 default:
13442 return "DW_CFA_<unknown>";
13443 }
13444 }
13445 #endif
13446
13447 static void
13448 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13449 {
13450 unsigned int i;
13451
13452 print_spaces (indent, f);
13453 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13454 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13455
13456 if (die->parent != NULL)
13457 {
13458 print_spaces (indent, f);
13459 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13460 die->parent->offset);
13461 }
13462
13463 print_spaces (indent, f);
13464 fprintf_unfiltered (f, " has children: %s\n",
13465 dwarf_bool_name (die->child != NULL));
13466
13467 print_spaces (indent, f);
13468 fprintf_unfiltered (f, " attributes:\n");
13469
13470 for (i = 0; i < die->num_attrs; ++i)
13471 {
13472 print_spaces (indent, f);
13473 fprintf_unfiltered (f, " %s (%s) ",
13474 dwarf_attr_name (die->attrs[i].name),
13475 dwarf_form_name (die->attrs[i].form));
13476
13477 switch (die->attrs[i].form)
13478 {
13479 case DW_FORM_ref_addr:
13480 case DW_FORM_addr:
13481 fprintf_unfiltered (f, "address: ");
13482 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13483 break;
13484 case DW_FORM_block2:
13485 case DW_FORM_block4:
13486 case DW_FORM_block:
13487 case DW_FORM_block1:
13488 fprintf_unfiltered (f, "block: size %d",
13489 DW_BLOCK (&die->attrs[i])->size);
13490 break;
13491 case DW_FORM_exprloc:
13492 fprintf_unfiltered (f, "expression: size %u",
13493 DW_BLOCK (&die->attrs[i])->size);
13494 break;
13495 case DW_FORM_ref1:
13496 case DW_FORM_ref2:
13497 case DW_FORM_ref4:
13498 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13499 (long) (DW_ADDR (&die->attrs[i])));
13500 break;
13501 case DW_FORM_data1:
13502 case DW_FORM_data2:
13503 case DW_FORM_data4:
13504 case DW_FORM_data8:
13505 case DW_FORM_udata:
13506 case DW_FORM_sdata:
13507 fprintf_unfiltered (f, "constant: %s",
13508 pulongest (DW_UNSND (&die->attrs[i])));
13509 break;
13510 case DW_FORM_sec_offset:
13511 fprintf_unfiltered (f, "section offset: %s",
13512 pulongest (DW_UNSND (&die->attrs[i])));
13513 break;
13514 case DW_FORM_ref_sig8:
13515 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13516 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13517 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13518 else
13519 fprintf_unfiltered (f, "signatured type, offset: unknown");
13520 break;
13521 case DW_FORM_string:
13522 case DW_FORM_strp:
13523 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13524 DW_STRING (&die->attrs[i])
13525 ? DW_STRING (&die->attrs[i]) : "",
13526 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13527 break;
13528 case DW_FORM_flag:
13529 if (DW_UNSND (&die->attrs[i]))
13530 fprintf_unfiltered (f, "flag: TRUE");
13531 else
13532 fprintf_unfiltered (f, "flag: FALSE");
13533 break;
13534 case DW_FORM_flag_present:
13535 fprintf_unfiltered (f, "flag: TRUE");
13536 break;
13537 case DW_FORM_indirect:
13538 /* The reader will have reduced the indirect form to
13539 the "base form" so this form should not occur. */
13540 fprintf_unfiltered (f,
13541 "unexpected attribute form: DW_FORM_indirect");
13542 break;
13543 default:
13544 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13545 die->attrs[i].form);
13546 break;
13547 }
13548 fprintf_unfiltered (f, "\n");
13549 }
13550 }
13551
13552 static void
13553 dump_die_for_error (struct die_info *die)
13554 {
13555 dump_die_shallow (gdb_stderr, 0, die);
13556 }
13557
13558 static void
13559 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13560 {
13561 int indent = level * 4;
13562
13563 gdb_assert (die != NULL);
13564
13565 if (level >= max_level)
13566 return;
13567
13568 dump_die_shallow (f, indent, die);
13569
13570 if (die->child != NULL)
13571 {
13572 print_spaces (indent, f);
13573 fprintf_unfiltered (f, " Children:");
13574 if (level + 1 < max_level)
13575 {
13576 fprintf_unfiltered (f, "\n");
13577 dump_die_1 (f, level + 1, max_level, die->child);
13578 }
13579 else
13580 {
13581 fprintf_unfiltered (f,
13582 " [not printed, max nesting level reached]\n");
13583 }
13584 }
13585
13586 if (die->sibling != NULL && level > 0)
13587 {
13588 dump_die_1 (f, level, max_level, die->sibling);
13589 }
13590 }
13591
13592 /* This is called from the pdie macro in gdbinit.in.
13593 It's not static so gcc will keep a copy callable from gdb. */
13594
13595 void
13596 dump_die (struct die_info *die, int max_level)
13597 {
13598 dump_die_1 (gdb_stdlog, 0, max_level, die);
13599 }
13600
13601 static void
13602 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13603 {
13604 void **slot;
13605
13606 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13607
13608 *slot = die;
13609 }
13610
13611 static int
13612 is_ref_attr (struct attribute *attr)
13613 {
13614 switch (attr->form)
13615 {
13616 case DW_FORM_ref_addr:
13617 case DW_FORM_ref1:
13618 case DW_FORM_ref2:
13619 case DW_FORM_ref4:
13620 case DW_FORM_ref8:
13621 case DW_FORM_ref_udata:
13622 return 1;
13623 default:
13624 return 0;
13625 }
13626 }
13627
13628 static unsigned int
13629 dwarf2_get_ref_die_offset (struct attribute *attr)
13630 {
13631 if (is_ref_attr (attr))
13632 return DW_ADDR (attr);
13633
13634 complaint (&symfile_complaints,
13635 _("unsupported die ref attribute form: '%s'"),
13636 dwarf_form_name (attr->form));
13637 return 0;
13638 }
13639
13640 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13641 * the value held by the attribute is not constant. */
13642
13643 static LONGEST
13644 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13645 {
13646 if (attr->form == DW_FORM_sdata)
13647 return DW_SND (attr);
13648 else if (attr->form == DW_FORM_udata
13649 || attr->form == DW_FORM_data1
13650 || attr->form == DW_FORM_data2
13651 || attr->form == DW_FORM_data4
13652 || attr->form == DW_FORM_data8)
13653 return DW_UNSND (attr);
13654 else
13655 {
13656 complaint (&symfile_complaints,
13657 _("Attribute value is not a constant (%s)"),
13658 dwarf_form_name (attr->form));
13659 return default_value;
13660 }
13661 }
13662
13663 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
13664 unit and add it to our queue.
13665 The result is non-zero if PER_CU was queued, otherwise the result is zero
13666 meaning either PER_CU is already queued or it is already loaded. */
13667
13668 static int
13669 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13670 struct dwarf2_per_cu_data *per_cu)
13671 {
13672 /* We may arrive here during partial symbol reading, if we need full
13673 DIEs to process an unusual case (e.g. template arguments). Do
13674 not queue PER_CU, just tell our caller to load its DIEs. */
13675 if (dwarf2_per_objfile->reading_partial_symbols)
13676 {
13677 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13678 return 1;
13679 return 0;
13680 }
13681
13682 /* Mark the dependence relation so that we don't flush PER_CU
13683 too early. */
13684 dwarf2_add_dependence (this_cu, per_cu);
13685
13686 /* If it's already on the queue, we have nothing to do. */
13687 if (per_cu->queued)
13688 return 0;
13689
13690 /* If the compilation unit is already loaded, just mark it as
13691 used. */
13692 if (per_cu->cu != NULL)
13693 {
13694 per_cu->cu->last_used = 0;
13695 return 0;
13696 }
13697
13698 /* Add it to the queue. */
13699 queue_comp_unit (per_cu, this_cu->objfile);
13700
13701 return 1;
13702 }
13703
13704 /* Follow reference or signature attribute ATTR of SRC_DIE.
13705 On entry *REF_CU is the CU of SRC_DIE.
13706 On exit *REF_CU is the CU of the result. */
13707
13708 static struct die_info *
13709 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13710 struct dwarf2_cu **ref_cu)
13711 {
13712 struct die_info *die;
13713
13714 if (is_ref_attr (attr))
13715 die = follow_die_ref (src_die, attr, ref_cu);
13716 else if (attr->form == DW_FORM_ref_sig8)
13717 die = follow_die_sig (src_die, attr, ref_cu);
13718 else
13719 {
13720 dump_die_for_error (src_die);
13721 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13722 (*ref_cu)->objfile->name);
13723 }
13724
13725 return die;
13726 }
13727
13728 /* Follow reference OFFSET.
13729 On entry *REF_CU is the CU of the source die referencing OFFSET.
13730 On exit *REF_CU is the CU of the result.
13731 Returns NULL if OFFSET is invalid. */
13732
13733 static struct die_info *
13734 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13735 {
13736 struct die_info temp_die;
13737 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13738
13739 gdb_assert (cu->per_cu != NULL);
13740
13741 target_cu = cu;
13742
13743 if (cu->per_cu->from_debug_types)
13744 {
13745 /* .debug_types CUs cannot reference anything outside their CU.
13746 If they need to, they have to reference a signatured type via
13747 DW_FORM_ref_sig8. */
13748 if (! offset_in_cu_p (&cu->header, offset))
13749 return NULL;
13750 }
13751 else if (! offset_in_cu_p (&cu->header, offset))
13752 {
13753 struct dwarf2_per_cu_data *per_cu;
13754
13755 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13756
13757 /* If necessary, add it to the queue and load its DIEs. */
13758 if (maybe_queue_comp_unit (cu, per_cu))
13759 load_full_comp_unit (per_cu, cu->objfile);
13760
13761 target_cu = per_cu->cu;
13762 }
13763 else if (cu->dies == NULL)
13764 {
13765 /* We're loading full DIEs during partial symbol reading. */
13766 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13767 load_full_comp_unit (cu->per_cu, cu->objfile);
13768 }
13769
13770 *ref_cu = target_cu;
13771 temp_die.offset = offset;
13772 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13773 }
13774
13775 /* Follow reference attribute ATTR of SRC_DIE.
13776 On entry *REF_CU is the CU of SRC_DIE.
13777 On exit *REF_CU is the CU of the result. */
13778
13779 static struct die_info *
13780 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13781 struct dwarf2_cu **ref_cu)
13782 {
13783 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13784 struct dwarf2_cu *cu = *ref_cu;
13785 struct die_info *die;
13786
13787 die = follow_die_offset (offset, ref_cu);
13788 if (!die)
13789 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13790 "at 0x%x [in module %s]"),
13791 offset, src_die->offset, cu->objfile->name);
13792
13793 return die;
13794 }
13795
13796 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13797 value is intended for DW_OP_call*. */
13798
13799 struct dwarf2_locexpr_baton
13800 dwarf2_fetch_die_location_block (unsigned int offset,
13801 struct dwarf2_per_cu_data *per_cu,
13802 CORE_ADDR (*get_frame_pc) (void *baton),
13803 void *baton)
13804 {
13805 struct dwarf2_cu *cu = per_cu->cu;
13806 struct die_info *die;
13807 struct attribute *attr;
13808 struct dwarf2_locexpr_baton retval;
13809
13810 dw2_setup (per_cu->objfile);
13811
13812 die = follow_die_offset (offset, &cu);
13813 if (!die)
13814 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13815 offset, per_cu->cu->objfile->name);
13816
13817 attr = dwarf2_attr (die, DW_AT_location, cu);
13818 if (!attr)
13819 {
13820 /* DWARF: "If there is no such attribute, then there is no effect.". */
13821
13822 retval.data = NULL;
13823 retval.size = 0;
13824 }
13825 else if (attr_form_is_section_offset (attr))
13826 {
13827 struct dwarf2_loclist_baton loclist_baton;
13828 CORE_ADDR pc = (*get_frame_pc) (baton);
13829 size_t size;
13830
13831 fill_in_loclist_baton (cu, &loclist_baton, attr);
13832
13833 retval.data = dwarf2_find_location_expression (&loclist_baton,
13834 &size, pc);
13835 retval.size = size;
13836 }
13837 else
13838 {
13839 if (!attr_form_is_block (attr))
13840 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13841 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13842 offset, per_cu->cu->objfile->name);
13843
13844 retval.data = DW_BLOCK (attr)->data;
13845 retval.size = DW_BLOCK (attr)->size;
13846 }
13847 retval.per_cu = cu->per_cu;
13848 return retval;
13849 }
13850
13851 /* Return the type of the DIE at DIE_OFFSET in the CU named by
13852 PER_CU. */
13853
13854 struct type *
13855 dwarf2_get_die_type (unsigned int die_offset,
13856 struct dwarf2_per_cu_data *per_cu)
13857 {
13858 dw2_setup (per_cu->objfile);
13859 return get_die_type_at_offset (die_offset, per_cu);
13860 }
13861
13862 /* Follow the signature attribute ATTR in SRC_DIE.
13863 On entry *REF_CU is the CU of SRC_DIE.
13864 On exit *REF_CU is the CU of the result. */
13865
13866 static struct die_info *
13867 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13868 struct dwarf2_cu **ref_cu)
13869 {
13870 struct objfile *objfile = (*ref_cu)->objfile;
13871 struct die_info temp_die;
13872 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13873 struct dwarf2_cu *sig_cu;
13874 struct die_info *die;
13875
13876 /* sig_type will be NULL if the signatured type is missing from
13877 the debug info. */
13878 if (sig_type == NULL)
13879 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13880 "at 0x%x [in module %s]"),
13881 src_die->offset, objfile->name);
13882
13883 /* If necessary, add it to the queue and load its DIEs. */
13884
13885 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13886 read_signatured_type (objfile, sig_type);
13887
13888 gdb_assert (sig_type->per_cu.cu != NULL);
13889
13890 sig_cu = sig_type->per_cu.cu;
13891 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13892 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13893 if (die)
13894 {
13895 *ref_cu = sig_cu;
13896 return die;
13897 }
13898
13899 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13900 "from DIE at 0x%x [in module %s]"),
13901 sig_type->type_offset, src_die->offset, objfile->name);
13902 }
13903
13904 /* Given an offset of a signatured type, return its signatured_type. */
13905
13906 static struct signatured_type *
13907 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13908 {
13909 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13910 unsigned int length, initial_length_size;
13911 unsigned int sig_offset;
13912 struct signatured_type find_entry, *type_sig;
13913
13914 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13915 sig_offset = (initial_length_size
13916 + 2 /*version*/
13917 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13918 + 1 /*address_size*/);
13919 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13920 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13921
13922 /* This is only used to lookup previously recorded types.
13923 If we didn't find it, it's our bug. */
13924 gdb_assert (type_sig != NULL);
13925 gdb_assert (offset == type_sig->per_cu.offset);
13926
13927 return type_sig;
13928 }
13929
13930 /* Read in signatured type at OFFSET and build its CU and die(s). */
13931
13932 static void
13933 read_signatured_type_at_offset (struct objfile *objfile,
13934 unsigned int offset)
13935 {
13936 struct signatured_type *type_sig;
13937
13938 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13939
13940 /* We have the section offset, but we need the signature to do the
13941 hash table lookup. */
13942 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13943
13944 gdb_assert (type_sig->per_cu.cu == NULL);
13945
13946 read_signatured_type (objfile, type_sig);
13947
13948 gdb_assert (type_sig->per_cu.cu != NULL);
13949 }
13950
13951 /* Read in a signatured type and build its CU and DIEs. */
13952
13953 static void
13954 read_signatured_type (struct objfile *objfile,
13955 struct signatured_type *type_sig)
13956 {
13957 gdb_byte *types_ptr;
13958 struct die_reader_specs reader_specs;
13959 struct dwarf2_cu *cu;
13960 ULONGEST signature;
13961 struct cleanup *back_to, *free_cu_cleanup;
13962
13963 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13964 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->per_cu.offset;
13965
13966 gdb_assert (type_sig->per_cu.cu == NULL);
13967
13968 cu = xmalloc (sizeof (*cu));
13969 init_one_comp_unit (cu, objfile);
13970
13971 type_sig->per_cu.cu = cu;
13972 cu->per_cu = &type_sig->per_cu;
13973
13974 /* If an error occurs while loading, release our storage. */
13975 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13976
13977 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13978 types_ptr, objfile->obfd);
13979 gdb_assert (signature == type_sig->signature);
13980
13981 cu->die_hash
13982 = htab_create_alloc_ex (cu->header.length / 12,
13983 die_hash,
13984 die_eq,
13985 NULL,
13986 &cu->comp_unit_obstack,
13987 hashtab_obstack_allocate,
13988 dummy_obstack_deallocate);
13989
13990 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13991 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13992
13993 init_cu_die_reader (&reader_specs, cu);
13994
13995 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13996 NULL /*parent*/);
13997
13998 /* We try not to read any attributes in this function, because not
13999 all objfiles needed for references have been loaded yet, and symbol
14000 table processing isn't initialized. But we have to set the CU language,
14001 or we won't be able to build types correctly. */
14002 prepare_one_comp_unit (cu, cu->dies);
14003
14004 do_cleanups (back_to);
14005
14006 /* We've successfully allocated this compilation unit. Let our caller
14007 clean it up when finished with it. */
14008 discard_cleanups (free_cu_cleanup);
14009
14010 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14011 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14012 }
14013
14014 /* Decode simple location descriptions.
14015 Given a pointer to a dwarf block that defines a location, compute
14016 the location and return the value.
14017
14018 NOTE drow/2003-11-18: This function is called in two situations
14019 now: for the address of static or global variables (partial symbols
14020 only) and for offsets into structures which are expected to be
14021 (more or less) constant. The partial symbol case should go away,
14022 and only the constant case should remain. That will let this
14023 function complain more accurately. A few special modes are allowed
14024 without complaint for global variables (for instance, global
14025 register values and thread-local values).
14026
14027 A location description containing no operations indicates that the
14028 object is optimized out. The return value is 0 for that case.
14029 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14030 callers will only want a very basic result and this can become a
14031 complaint.
14032
14033 Note that stack[0] is unused except as a default error return. */
14034
14035 static CORE_ADDR
14036 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14037 {
14038 struct objfile *objfile = cu->objfile;
14039 int i;
14040 int size = blk->size;
14041 gdb_byte *data = blk->data;
14042 CORE_ADDR stack[64];
14043 int stacki;
14044 unsigned int bytes_read, unsnd;
14045 gdb_byte op;
14046
14047 i = 0;
14048 stacki = 0;
14049 stack[stacki] = 0;
14050 stack[++stacki] = 0;
14051
14052 while (i < size)
14053 {
14054 op = data[i++];
14055 switch (op)
14056 {
14057 case DW_OP_lit0:
14058 case DW_OP_lit1:
14059 case DW_OP_lit2:
14060 case DW_OP_lit3:
14061 case DW_OP_lit4:
14062 case DW_OP_lit5:
14063 case DW_OP_lit6:
14064 case DW_OP_lit7:
14065 case DW_OP_lit8:
14066 case DW_OP_lit9:
14067 case DW_OP_lit10:
14068 case DW_OP_lit11:
14069 case DW_OP_lit12:
14070 case DW_OP_lit13:
14071 case DW_OP_lit14:
14072 case DW_OP_lit15:
14073 case DW_OP_lit16:
14074 case DW_OP_lit17:
14075 case DW_OP_lit18:
14076 case DW_OP_lit19:
14077 case DW_OP_lit20:
14078 case DW_OP_lit21:
14079 case DW_OP_lit22:
14080 case DW_OP_lit23:
14081 case DW_OP_lit24:
14082 case DW_OP_lit25:
14083 case DW_OP_lit26:
14084 case DW_OP_lit27:
14085 case DW_OP_lit28:
14086 case DW_OP_lit29:
14087 case DW_OP_lit30:
14088 case DW_OP_lit31:
14089 stack[++stacki] = op - DW_OP_lit0;
14090 break;
14091
14092 case DW_OP_reg0:
14093 case DW_OP_reg1:
14094 case DW_OP_reg2:
14095 case DW_OP_reg3:
14096 case DW_OP_reg4:
14097 case DW_OP_reg5:
14098 case DW_OP_reg6:
14099 case DW_OP_reg7:
14100 case DW_OP_reg8:
14101 case DW_OP_reg9:
14102 case DW_OP_reg10:
14103 case DW_OP_reg11:
14104 case DW_OP_reg12:
14105 case DW_OP_reg13:
14106 case DW_OP_reg14:
14107 case DW_OP_reg15:
14108 case DW_OP_reg16:
14109 case DW_OP_reg17:
14110 case DW_OP_reg18:
14111 case DW_OP_reg19:
14112 case DW_OP_reg20:
14113 case DW_OP_reg21:
14114 case DW_OP_reg22:
14115 case DW_OP_reg23:
14116 case DW_OP_reg24:
14117 case DW_OP_reg25:
14118 case DW_OP_reg26:
14119 case DW_OP_reg27:
14120 case DW_OP_reg28:
14121 case DW_OP_reg29:
14122 case DW_OP_reg30:
14123 case DW_OP_reg31:
14124 stack[++stacki] = op - DW_OP_reg0;
14125 if (i < size)
14126 dwarf2_complex_location_expr_complaint ();
14127 break;
14128
14129 case DW_OP_regx:
14130 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14131 i += bytes_read;
14132 stack[++stacki] = unsnd;
14133 if (i < size)
14134 dwarf2_complex_location_expr_complaint ();
14135 break;
14136
14137 case DW_OP_addr:
14138 stack[++stacki] = read_address (objfile->obfd, &data[i],
14139 cu, &bytes_read);
14140 i += bytes_read;
14141 break;
14142
14143 case DW_OP_const1u:
14144 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14145 i += 1;
14146 break;
14147
14148 case DW_OP_const1s:
14149 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14150 i += 1;
14151 break;
14152
14153 case DW_OP_const2u:
14154 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14155 i += 2;
14156 break;
14157
14158 case DW_OP_const2s:
14159 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14160 i += 2;
14161 break;
14162
14163 case DW_OP_const4u:
14164 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14165 i += 4;
14166 break;
14167
14168 case DW_OP_const4s:
14169 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14170 i += 4;
14171 break;
14172
14173 case DW_OP_constu:
14174 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14175 &bytes_read);
14176 i += bytes_read;
14177 break;
14178
14179 case DW_OP_consts:
14180 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14181 i += bytes_read;
14182 break;
14183
14184 case DW_OP_dup:
14185 stack[stacki + 1] = stack[stacki];
14186 stacki++;
14187 break;
14188
14189 case DW_OP_plus:
14190 stack[stacki - 1] += stack[stacki];
14191 stacki--;
14192 break;
14193
14194 case DW_OP_plus_uconst:
14195 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14196 &bytes_read);
14197 i += bytes_read;
14198 break;
14199
14200 case DW_OP_minus:
14201 stack[stacki - 1] -= stack[stacki];
14202 stacki--;
14203 break;
14204
14205 case DW_OP_deref:
14206 /* If we're not the last op, then we definitely can't encode
14207 this using GDB's address_class enum. This is valid for partial
14208 global symbols, although the variable's address will be bogus
14209 in the psymtab. */
14210 if (i < size)
14211 dwarf2_complex_location_expr_complaint ();
14212 break;
14213
14214 case DW_OP_GNU_push_tls_address:
14215 /* The top of the stack has the offset from the beginning
14216 of the thread control block at which the variable is located. */
14217 /* Nothing should follow this operator, so the top of stack would
14218 be returned. */
14219 /* This is valid for partial global symbols, but the variable's
14220 address will be bogus in the psymtab. */
14221 if (i < size)
14222 dwarf2_complex_location_expr_complaint ();
14223 break;
14224
14225 case DW_OP_GNU_uninit:
14226 break;
14227
14228 default:
14229 {
14230 const char *name = dwarf_stack_op_name (op);
14231
14232 if (name)
14233 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14234 name);
14235 else
14236 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14237 op);
14238 }
14239
14240 return (stack[stacki]);
14241 }
14242
14243 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14244 outside of the allocated space. Also enforce minimum>0. */
14245 if (stacki >= ARRAY_SIZE (stack) - 1)
14246 {
14247 complaint (&symfile_complaints,
14248 _("location description stack overflow"));
14249 return 0;
14250 }
14251
14252 if (stacki <= 0)
14253 {
14254 complaint (&symfile_complaints,
14255 _("location description stack underflow"));
14256 return 0;
14257 }
14258 }
14259 return (stack[stacki]);
14260 }
14261
14262 /* memory allocation interface */
14263
14264 static struct dwarf_block *
14265 dwarf_alloc_block (struct dwarf2_cu *cu)
14266 {
14267 struct dwarf_block *blk;
14268
14269 blk = (struct dwarf_block *)
14270 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14271 return (blk);
14272 }
14273
14274 static struct abbrev_info *
14275 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14276 {
14277 struct abbrev_info *abbrev;
14278
14279 abbrev = (struct abbrev_info *)
14280 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14281 memset (abbrev, 0, sizeof (struct abbrev_info));
14282 return (abbrev);
14283 }
14284
14285 static struct die_info *
14286 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14287 {
14288 struct die_info *die;
14289 size_t size = sizeof (struct die_info);
14290
14291 if (num_attrs > 1)
14292 size += (num_attrs - 1) * sizeof (struct attribute);
14293
14294 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14295 memset (die, 0, sizeof (struct die_info));
14296 return (die);
14297 }
14298
14299 \f
14300 /* Macro support. */
14301
14302 /* Return the full name of file number I in *LH's file name table.
14303 Use COMP_DIR as the name of the current directory of the
14304 compilation. The result is allocated using xmalloc; the caller is
14305 responsible for freeing it. */
14306 static char *
14307 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14308 {
14309 /* Is the file number a valid index into the line header's file name
14310 table? Remember that file numbers start with one, not zero. */
14311 if (1 <= file && file <= lh->num_file_names)
14312 {
14313 struct file_entry *fe = &lh->file_names[file - 1];
14314
14315 if (IS_ABSOLUTE_PATH (fe->name))
14316 return xstrdup (fe->name);
14317 else
14318 {
14319 const char *dir;
14320 int dir_len;
14321 char *full_name;
14322
14323 if (fe->dir_index)
14324 dir = lh->include_dirs[fe->dir_index - 1];
14325 else
14326 dir = comp_dir;
14327
14328 if (dir)
14329 {
14330 dir_len = strlen (dir);
14331 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14332 strcpy (full_name, dir);
14333 full_name[dir_len] = '/';
14334 strcpy (full_name + dir_len + 1, fe->name);
14335 return full_name;
14336 }
14337 else
14338 return xstrdup (fe->name);
14339 }
14340 }
14341 else
14342 {
14343 /* The compiler produced a bogus file number. We can at least
14344 record the macro definitions made in the file, even if we
14345 won't be able to find the file by name. */
14346 char fake_name[80];
14347
14348 sprintf (fake_name, "<bad macro file number %d>", file);
14349
14350 complaint (&symfile_complaints,
14351 _("bad file number in macro information (%d)"),
14352 file);
14353
14354 return xstrdup (fake_name);
14355 }
14356 }
14357
14358
14359 static struct macro_source_file *
14360 macro_start_file (int file, int line,
14361 struct macro_source_file *current_file,
14362 const char *comp_dir,
14363 struct line_header *lh, struct objfile *objfile)
14364 {
14365 /* The full name of this source file. */
14366 char *full_name = file_full_name (file, lh, comp_dir);
14367
14368 /* We don't create a macro table for this compilation unit
14369 at all until we actually get a filename. */
14370 if (! pending_macros)
14371 pending_macros = new_macro_table (&objfile->objfile_obstack,
14372 objfile->macro_cache);
14373
14374 if (! current_file)
14375 /* If we have no current file, then this must be the start_file
14376 directive for the compilation unit's main source file. */
14377 current_file = macro_set_main (pending_macros, full_name);
14378 else
14379 current_file = macro_include (current_file, line, full_name);
14380
14381 xfree (full_name);
14382
14383 return current_file;
14384 }
14385
14386
14387 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14388 followed by a null byte. */
14389 static char *
14390 copy_string (const char *buf, int len)
14391 {
14392 char *s = xmalloc (len + 1);
14393
14394 memcpy (s, buf, len);
14395 s[len] = '\0';
14396 return s;
14397 }
14398
14399
14400 static const char *
14401 consume_improper_spaces (const char *p, const char *body)
14402 {
14403 if (*p == ' ')
14404 {
14405 complaint (&symfile_complaints,
14406 _("macro definition contains spaces "
14407 "in formal argument list:\n`%s'"),
14408 body);
14409
14410 while (*p == ' ')
14411 p++;
14412 }
14413
14414 return p;
14415 }
14416
14417
14418 static void
14419 parse_macro_definition (struct macro_source_file *file, int line,
14420 const char *body)
14421 {
14422 const char *p;
14423
14424 /* The body string takes one of two forms. For object-like macro
14425 definitions, it should be:
14426
14427 <macro name> " " <definition>
14428
14429 For function-like macro definitions, it should be:
14430
14431 <macro name> "() " <definition>
14432 or
14433 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14434
14435 Spaces may appear only where explicitly indicated, and in the
14436 <definition>.
14437
14438 The Dwarf 2 spec says that an object-like macro's name is always
14439 followed by a space, but versions of GCC around March 2002 omit
14440 the space when the macro's definition is the empty string.
14441
14442 The Dwarf 2 spec says that there should be no spaces between the
14443 formal arguments in a function-like macro's formal argument list,
14444 but versions of GCC around March 2002 include spaces after the
14445 commas. */
14446
14447
14448 /* Find the extent of the macro name. The macro name is terminated
14449 by either a space or null character (for an object-like macro) or
14450 an opening paren (for a function-like macro). */
14451 for (p = body; *p; p++)
14452 if (*p == ' ' || *p == '(')
14453 break;
14454
14455 if (*p == ' ' || *p == '\0')
14456 {
14457 /* It's an object-like macro. */
14458 int name_len = p - body;
14459 char *name = copy_string (body, name_len);
14460 const char *replacement;
14461
14462 if (*p == ' ')
14463 replacement = body + name_len + 1;
14464 else
14465 {
14466 dwarf2_macro_malformed_definition_complaint (body);
14467 replacement = body + name_len;
14468 }
14469
14470 macro_define_object (file, line, name, replacement);
14471
14472 xfree (name);
14473 }
14474 else if (*p == '(')
14475 {
14476 /* It's a function-like macro. */
14477 char *name = copy_string (body, p - body);
14478 int argc = 0;
14479 int argv_size = 1;
14480 char **argv = xmalloc (argv_size * sizeof (*argv));
14481
14482 p++;
14483
14484 p = consume_improper_spaces (p, body);
14485
14486 /* Parse the formal argument list. */
14487 while (*p && *p != ')')
14488 {
14489 /* Find the extent of the current argument name. */
14490 const char *arg_start = p;
14491
14492 while (*p && *p != ',' && *p != ')' && *p != ' ')
14493 p++;
14494
14495 if (! *p || p == arg_start)
14496 dwarf2_macro_malformed_definition_complaint (body);
14497 else
14498 {
14499 /* Make sure argv has room for the new argument. */
14500 if (argc >= argv_size)
14501 {
14502 argv_size *= 2;
14503 argv = xrealloc (argv, argv_size * sizeof (*argv));
14504 }
14505
14506 argv[argc++] = copy_string (arg_start, p - arg_start);
14507 }
14508
14509 p = consume_improper_spaces (p, body);
14510
14511 /* Consume the comma, if present. */
14512 if (*p == ',')
14513 {
14514 p++;
14515
14516 p = consume_improper_spaces (p, body);
14517 }
14518 }
14519
14520 if (*p == ')')
14521 {
14522 p++;
14523
14524 if (*p == ' ')
14525 /* Perfectly formed definition, no complaints. */
14526 macro_define_function (file, line, name,
14527 argc, (const char **) argv,
14528 p + 1);
14529 else if (*p == '\0')
14530 {
14531 /* Complain, but do define it. */
14532 dwarf2_macro_malformed_definition_complaint (body);
14533 macro_define_function (file, line, name,
14534 argc, (const char **) argv,
14535 p);
14536 }
14537 else
14538 /* Just complain. */
14539 dwarf2_macro_malformed_definition_complaint (body);
14540 }
14541 else
14542 /* Just complain. */
14543 dwarf2_macro_malformed_definition_complaint (body);
14544
14545 xfree (name);
14546 {
14547 int i;
14548
14549 for (i = 0; i < argc; i++)
14550 xfree (argv[i]);
14551 }
14552 xfree (argv);
14553 }
14554 else
14555 dwarf2_macro_malformed_definition_complaint (body);
14556 }
14557
14558
14559 static void
14560 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14561 char *comp_dir, bfd *abfd,
14562 struct dwarf2_cu *cu)
14563 {
14564 gdb_byte *mac_ptr, *mac_end;
14565 struct macro_source_file *current_file = 0;
14566 enum dwarf_macinfo_record_type macinfo_type;
14567 int at_commandline;
14568
14569 dwarf2_read_section (dwarf2_per_objfile->objfile,
14570 &dwarf2_per_objfile->macinfo);
14571 if (dwarf2_per_objfile->macinfo.buffer == NULL)
14572 {
14573 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14574 return;
14575 }
14576
14577 /* First pass: Find the name of the base filename.
14578 This filename is needed in order to process all macros whose definition
14579 (or undefinition) comes from the command line. These macros are defined
14580 before the first DW_MACINFO_start_file entry, and yet still need to be
14581 associated to the base file.
14582
14583 To determine the base file name, we scan the macro definitions until we
14584 reach the first DW_MACINFO_start_file entry. We then initialize
14585 CURRENT_FILE accordingly so that any macro definition found before the
14586 first DW_MACINFO_start_file can still be associated to the base file. */
14587
14588 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14589 mac_end = dwarf2_per_objfile->macinfo.buffer
14590 + dwarf2_per_objfile->macinfo.size;
14591
14592 do
14593 {
14594 /* Do we at least have room for a macinfo type byte? */
14595 if (mac_ptr >= mac_end)
14596 {
14597 /* Complaint is printed during the second pass as GDB will probably
14598 stop the first pass earlier upon finding
14599 DW_MACINFO_start_file. */
14600 break;
14601 }
14602
14603 macinfo_type = read_1_byte (abfd, mac_ptr);
14604 mac_ptr++;
14605
14606 switch (macinfo_type)
14607 {
14608 /* A zero macinfo type indicates the end of the macro
14609 information. */
14610 case 0:
14611 break;
14612
14613 case DW_MACINFO_define:
14614 case DW_MACINFO_undef:
14615 /* Only skip the data by MAC_PTR. */
14616 {
14617 unsigned int bytes_read;
14618
14619 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14620 mac_ptr += bytes_read;
14621 read_direct_string (abfd, mac_ptr, &bytes_read);
14622 mac_ptr += bytes_read;
14623 }
14624 break;
14625
14626 case DW_MACINFO_start_file:
14627 {
14628 unsigned int bytes_read;
14629 int line, file;
14630
14631 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14632 mac_ptr += bytes_read;
14633 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14634 mac_ptr += bytes_read;
14635
14636 current_file = macro_start_file (file, line, current_file,
14637 comp_dir, lh, cu->objfile);
14638 }
14639 break;
14640
14641 case DW_MACINFO_end_file:
14642 /* No data to skip by MAC_PTR. */
14643 break;
14644
14645 case DW_MACINFO_vendor_ext:
14646 /* Only skip the data by MAC_PTR. */
14647 {
14648 unsigned int bytes_read;
14649
14650 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14651 mac_ptr += bytes_read;
14652 read_direct_string (abfd, mac_ptr, &bytes_read);
14653 mac_ptr += bytes_read;
14654 }
14655 break;
14656
14657 default:
14658 break;
14659 }
14660 } while (macinfo_type != 0 && current_file == NULL);
14661
14662 /* Second pass: Process all entries.
14663
14664 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14665 command-line macro definitions/undefinitions. This flag is unset when we
14666 reach the first DW_MACINFO_start_file entry. */
14667
14668 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14669
14670 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14671 GDB is still reading the definitions from command line. First
14672 DW_MACINFO_start_file will need to be ignored as it was already executed
14673 to create CURRENT_FILE for the main source holding also the command line
14674 definitions. On first met DW_MACINFO_start_file this flag is reset to
14675 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14676
14677 at_commandline = 1;
14678
14679 do
14680 {
14681 /* Do we at least have room for a macinfo type byte? */
14682 if (mac_ptr >= mac_end)
14683 {
14684 dwarf2_macros_too_long_complaint ();
14685 break;
14686 }
14687
14688 macinfo_type = read_1_byte (abfd, mac_ptr);
14689 mac_ptr++;
14690
14691 switch (macinfo_type)
14692 {
14693 /* A zero macinfo type indicates the end of the macro
14694 information. */
14695 case 0:
14696 break;
14697
14698 case DW_MACINFO_define:
14699 case DW_MACINFO_undef:
14700 {
14701 unsigned int bytes_read;
14702 int line;
14703 char *body;
14704
14705 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14706 mac_ptr += bytes_read;
14707 body = read_direct_string (abfd, mac_ptr, &bytes_read);
14708 mac_ptr += bytes_read;
14709
14710 if (! current_file)
14711 {
14712 /* DWARF violation as no main source is present. */
14713 complaint (&symfile_complaints,
14714 _("debug info with no main source gives macro %s "
14715 "on line %d: %s"),
14716 macinfo_type == DW_MACINFO_define ?
14717 _("definition") :
14718 macinfo_type == DW_MACINFO_undef ?
14719 _("undefinition") :
14720 _("something-or-other"), line, body);
14721 break;
14722 }
14723 if ((line == 0 && !at_commandline)
14724 || (line != 0 && at_commandline))
14725 complaint (&symfile_complaints,
14726 _("debug info gives %s macro %s with %s line %d: %s"),
14727 at_commandline ? _("command-line") : _("in-file"),
14728 macinfo_type == DW_MACINFO_define ?
14729 _("definition") :
14730 macinfo_type == DW_MACINFO_undef ?
14731 _("undefinition") :
14732 _("something-or-other"),
14733 line == 0 ? _("zero") : _("non-zero"), line, body);
14734
14735 if (macinfo_type == DW_MACINFO_define)
14736 parse_macro_definition (current_file, line, body);
14737 else if (macinfo_type == DW_MACINFO_undef)
14738 macro_undef (current_file, line, body);
14739 }
14740 break;
14741
14742 case DW_MACINFO_start_file:
14743 {
14744 unsigned int bytes_read;
14745 int line, file;
14746
14747 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14748 mac_ptr += bytes_read;
14749 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14750 mac_ptr += bytes_read;
14751
14752 if ((line == 0 && !at_commandline)
14753 || (line != 0 && at_commandline))
14754 complaint (&symfile_complaints,
14755 _("debug info gives source %d included "
14756 "from %s at %s line %d"),
14757 file, at_commandline ? _("command-line") : _("file"),
14758 line == 0 ? _("zero") : _("non-zero"), line);
14759
14760 if (at_commandline)
14761 {
14762 /* This DW_MACINFO_start_file was executed in the pass one. */
14763 at_commandline = 0;
14764 }
14765 else
14766 current_file = macro_start_file (file, line,
14767 current_file, comp_dir,
14768 lh, cu->objfile);
14769 }
14770 break;
14771
14772 case DW_MACINFO_end_file:
14773 if (! current_file)
14774 complaint (&symfile_complaints,
14775 _("macro debug info has an unmatched "
14776 "`close_file' directive"));
14777 else
14778 {
14779 current_file = current_file->included_by;
14780 if (! current_file)
14781 {
14782 enum dwarf_macinfo_record_type next_type;
14783
14784 /* GCC circa March 2002 doesn't produce the zero
14785 type byte marking the end of the compilation
14786 unit. Complain if it's not there, but exit no
14787 matter what. */
14788
14789 /* Do we at least have room for a macinfo type byte? */
14790 if (mac_ptr >= mac_end)
14791 {
14792 dwarf2_macros_too_long_complaint ();
14793 return;
14794 }
14795
14796 /* We don't increment mac_ptr here, so this is just
14797 a look-ahead. */
14798 next_type = read_1_byte (abfd, mac_ptr);
14799 if (next_type != 0)
14800 complaint (&symfile_complaints,
14801 _("no terminating 0-type entry for "
14802 "macros in `.debug_macinfo' section"));
14803
14804 return;
14805 }
14806 }
14807 break;
14808
14809 case DW_MACINFO_vendor_ext:
14810 {
14811 unsigned int bytes_read;
14812 int constant;
14813
14814 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14815 mac_ptr += bytes_read;
14816 read_direct_string (abfd, mac_ptr, &bytes_read);
14817 mac_ptr += bytes_read;
14818
14819 /* We don't recognize any vendor extensions. */
14820 }
14821 break;
14822 }
14823 } while (macinfo_type != 0);
14824 }
14825
14826 /* Check if the attribute's form is a DW_FORM_block*
14827 if so return true else false. */
14828 static int
14829 attr_form_is_block (struct attribute *attr)
14830 {
14831 return (attr == NULL ? 0 :
14832 attr->form == DW_FORM_block1
14833 || attr->form == DW_FORM_block2
14834 || attr->form == DW_FORM_block4
14835 || attr->form == DW_FORM_block
14836 || attr->form == DW_FORM_exprloc);
14837 }
14838
14839 /* Return non-zero if ATTR's value is a section offset --- classes
14840 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14841 You may use DW_UNSND (attr) to retrieve such offsets.
14842
14843 Section 7.5.4, "Attribute Encodings", explains that no attribute
14844 may have a value that belongs to more than one of these classes; it
14845 would be ambiguous if we did, because we use the same forms for all
14846 of them. */
14847 static int
14848 attr_form_is_section_offset (struct attribute *attr)
14849 {
14850 return (attr->form == DW_FORM_data4
14851 || attr->form == DW_FORM_data8
14852 || attr->form == DW_FORM_sec_offset);
14853 }
14854
14855
14856 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14857 zero otherwise. When this function returns true, you can apply
14858 dwarf2_get_attr_constant_value to it.
14859
14860 However, note that for some attributes you must check
14861 attr_form_is_section_offset before using this test. DW_FORM_data4
14862 and DW_FORM_data8 are members of both the constant class, and of
14863 the classes that contain offsets into other debug sections
14864 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14865 that, if an attribute's can be either a constant or one of the
14866 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14867 taken as section offsets, not constants. */
14868 static int
14869 attr_form_is_constant (struct attribute *attr)
14870 {
14871 switch (attr->form)
14872 {
14873 case DW_FORM_sdata:
14874 case DW_FORM_udata:
14875 case DW_FORM_data1:
14876 case DW_FORM_data2:
14877 case DW_FORM_data4:
14878 case DW_FORM_data8:
14879 return 1;
14880 default:
14881 return 0;
14882 }
14883 }
14884
14885 /* A helper function that fills in a dwarf2_loclist_baton. */
14886
14887 static void
14888 fill_in_loclist_baton (struct dwarf2_cu *cu,
14889 struct dwarf2_loclist_baton *baton,
14890 struct attribute *attr)
14891 {
14892 dwarf2_read_section (dwarf2_per_objfile->objfile,
14893 &dwarf2_per_objfile->loc);
14894
14895 baton->per_cu = cu->per_cu;
14896 gdb_assert (baton->per_cu);
14897 /* We don't know how long the location list is, but make sure we
14898 don't run off the edge of the section. */
14899 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14900 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14901 baton->base_address = cu->base_address;
14902 }
14903
14904 static void
14905 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14906 struct dwarf2_cu *cu)
14907 {
14908 if (attr_form_is_section_offset (attr)
14909 /* ".debug_loc" may not exist at all, or the offset may be outside
14910 the section. If so, fall through to the complaint in the
14911 other branch. */
14912 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14913 &dwarf2_per_objfile->loc))
14914 {
14915 struct dwarf2_loclist_baton *baton;
14916
14917 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14918 sizeof (struct dwarf2_loclist_baton));
14919
14920 fill_in_loclist_baton (cu, baton, attr);
14921
14922 if (cu->base_known == 0)
14923 complaint (&symfile_complaints,
14924 _("Location list used without "
14925 "specifying the CU base address."));
14926
14927 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14928 SYMBOL_LOCATION_BATON (sym) = baton;
14929 }
14930 else
14931 {
14932 struct dwarf2_locexpr_baton *baton;
14933
14934 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14935 sizeof (struct dwarf2_locexpr_baton));
14936 baton->per_cu = cu->per_cu;
14937 gdb_assert (baton->per_cu);
14938
14939 if (attr_form_is_block (attr))
14940 {
14941 /* Note that we're just copying the block's data pointer
14942 here, not the actual data. We're still pointing into the
14943 info_buffer for SYM's objfile; right now we never release
14944 that buffer, but when we do clean up properly this may
14945 need to change. */
14946 baton->size = DW_BLOCK (attr)->size;
14947 baton->data = DW_BLOCK (attr)->data;
14948 }
14949 else
14950 {
14951 dwarf2_invalid_attrib_class_complaint ("location description",
14952 SYMBOL_NATURAL_NAME (sym));
14953 baton->size = 0;
14954 baton->data = NULL;
14955 }
14956
14957 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14958 SYMBOL_LOCATION_BATON (sym) = baton;
14959 }
14960 }
14961
14962 /* Return the OBJFILE associated with the compilation unit CU. If CU
14963 came from a separate debuginfo file, then the master objfile is
14964 returned. */
14965
14966 struct objfile *
14967 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14968 {
14969 struct objfile *objfile = per_cu->objfile;
14970
14971 /* Return the master objfile, so that we can report and look up the
14972 correct file containing this variable. */
14973 if (objfile->separate_debug_objfile_backlink)
14974 objfile = objfile->separate_debug_objfile_backlink;
14975
14976 return objfile;
14977 }
14978
14979 /* Return the address size given in the compilation unit header for CU. */
14980
14981 CORE_ADDR
14982 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14983 {
14984 if (per_cu->cu)
14985 return per_cu->cu->header.addr_size;
14986 else
14987 {
14988 /* If the CU is not currently read in, we re-read its header. */
14989 struct objfile *objfile = per_cu->objfile;
14990 struct dwarf2_per_objfile *per_objfile
14991 = objfile_data (objfile, dwarf2_objfile_data_key);
14992 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14993 struct comp_unit_head cu_header;
14994
14995 memset (&cu_header, 0, sizeof cu_header);
14996 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14997 return cu_header.addr_size;
14998 }
14999 }
15000
15001 /* Return the offset size given in the compilation unit header for CU. */
15002
15003 int
15004 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15005 {
15006 if (per_cu->cu)
15007 return per_cu->cu->header.offset_size;
15008 else
15009 {
15010 /* If the CU is not currently read in, we re-read its header. */
15011 struct objfile *objfile = per_cu->objfile;
15012 struct dwarf2_per_objfile *per_objfile
15013 = objfile_data (objfile, dwarf2_objfile_data_key);
15014 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
15015 struct comp_unit_head cu_header;
15016
15017 memset (&cu_header, 0, sizeof cu_header);
15018 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
15019 return cu_header.offset_size;
15020 }
15021 }
15022
15023 /* Return the text offset of the CU. The returned offset comes from
15024 this CU's objfile. If this objfile came from a separate debuginfo
15025 file, then the offset may be different from the corresponding
15026 offset in the parent objfile. */
15027
15028 CORE_ADDR
15029 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15030 {
15031 struct objfile *objfile = per_cu->objfile;
15032
15033 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15034 }
15035
15036 /* Locate the .debug_info compilation unit from CU's objfile which contains
15037 the DIE at OFFSET. Raises an error on failure. */
15038
15039 static struct dwarf2_per_cu_data *
15040 dwarf2_find_containing_comp_unit (unsigned int offset,
15041 struct objfile *objfile)
15042 {
15043 struct dwarf2_per_cu_data *this_cu;
15044 int low, high;
15045
15046 low = 0;
15047 high = dwarf2_per_objfile->n_comp_units - 1;
15048 while (high > low)
15049 {
15050 int mid = low + (high - low) / 2;
15051
15052 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15053 high = mid;
15054 else
15055 low = mid + 1;
15056 }
15057 gdb_assert (low == high);
15058 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15059 {
15060 if (low == 0)
15061 error (_("Dwarf Error: could not find partial DIE containing "
15062 "offset 0x%lx [in module %s]"),
15063 (long) offset, bfd_get_filename (objfile->obfd));
15064
15065 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15066 return dwarf2_per_objfile->all_comp_units[low-1];
15067 }
15068 else
15069 {
15070 this_cu = dwarf2_per_objfile->all_comp_units[low];
15071 if (low == dwarf2_per_objfile->n_comp_units - 1
15072 && offset >= this_cu->offset + this_cu->length)
15073 error (_("invalid dwarf2 offset %u"), offset);
15074 gdb_assert (offset < this_cu->offset + this_cu->length);
15075 return this_cu;
15076 }
15077 }
15078
15079 /* Locate the compilation unit from OBJFILE which is located at exactly
15080 OFFSET. Raises an error on failure. */
15081
15082 static struct dwarf2_per_cu_data *
15083 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
15084 {
15085 struct dwarf2_per_cu_data *this_cu;
15086
15087 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15088 if (this_cu->offset != offset)
15089 error (_("no compilation unit with offset %u."), offset);
15090 return this_cu;
15091 }
15092
15093 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
15094
15095 static void
15096 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15097 {
15098 memset (cu, 0, sizeof (*cu));
15099 cu->objfile = objfile;
15100 obstack_init (&cu->comp_unit_obstack);
15101 }
15102
15103 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15104
15105 static void
15106 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15107 {
15108 struct attribute *attr;
15109
15110 /* Set the language we're debugging. */
15111 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15112 if (attr)
15113 set_cu_language (DW_UNSND (attr), cu);
15114 else
15115 {
15116 cu->language = language_minimal;
15117 cu->language_defn = language_def (cu->language);
15118 }
15119 }
15120
15121 /* Release one cached compilation unit, CU. We unlink it from the tree
15122 of compilation units, but we don't remove it from the read_in_chain;
15123 the caller is responsible for that.
15124 NOTE: DATA is a void * because this function is also used as a
15125 cleanup routine. */
15126
15127 static void
15128 free_one_comp_unit (void *data)
15129 {
15130 struct dwarf2_cu *cu = data;
15131
15132 if (cu->per_cu != NULL)
15133 cu->per_cu->cu = NULL;
15134 cu->per_cu = NULL;
15135
15136 obstack_free (&cu->comp_unit_obstack, NULL);
15137
15138 xfree (cu);
15139 }
15140
15141 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15142 when we're finished with it. We can't free the pointer itself, but be
15143 sure to unlink it from the cache. Also release any associated storage
15144 and perform cache maintenance.
15145
15146 Only used during partial symbol parsing. */
15147
15148 static void
15149 free_stack_comp_unit (void *data)
15150 {
15151 struct dwarf2_cu *cu = data;
15152
15153 obstack_free (&cu->comp_unit_obstack, NULL);
15154 cu->partial_dies = NULL;
15155
15156 if (cu->per_cu != NULL)
15157 {
15158 /* This compilation unit is on the stack in our caller, so we
15159 should not xfree it. Just unlink it. */
15160 cu->per_cu->cu = NULL;
15161 cu->per_cu = NULL;
15162
15163 /* If we had a per-cu pointer, then we may have other compilation
15164 units loaded, so age them now. */
15165 age_cached_comp_units ();
15166 }
15167 }
15168
15169 /* Free all cached compilation units. */
15170
15171 static void
15172 free_cached_comp_units (void *data)
15173 {
15174 struct dwarf2_per_cu_data *per_cu, **last_chain;
15175
15176 per_cu = dwarf2_per_objfile->read_in_chain;
15177 last_chain = &dwarf2_per_objfile->read_in_chain;
15178 while (per_cu != NULL)
15179 {
15180 struct dwarf2_per_cu_data *next_cu;
15181
15182 next_cu = per_cu->cu->read_in_chain;
15183
15184 free_one_comp_unit (per_cu->cu);
15185 *last_chain = next_cu;
15186
15187 per_cu = next_cu;
15188 }
15189 }
15190
15191 /* Increase the age counter on each cached compilation unit, and free
15192 any that are too old. */
15193
15194 static void
15195 age_cached_comp_units (void)
15196 {
15197 struct dwarf2_per_cu_data *per_cu, **last_chain;
15198
15199 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15200 per_cu = dwarf2_per_objfile->read_in_chain;
15201 while (per_cu != NULL)
15202 {
15203 per_cu->cu->last_used ++;
15204 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15205 dwarf2_mark (per_cu->cu);
15206 per_cu = per_cu->cu->read_in_chain;
15207 }
15208
15209 per_cu = dwarf2_per_objfile->read_in_chain;
15210 last_chain = &dwarf2_per_objfile->read_in_chain;
15211 while (per_cu != NULL)
15212 {
15213 struct dwarf2_per_cu_data *next_cu;
15214
15215 next_cu = per_cu->cu->read_in_chain;
15216
15217 if (!per_cu->cu->mark)
15218 {
15219 free_one_comp_unit (per_cu->cu);
15220 *last_chain = next_cu;
15221 }
15222 else
15223 last_chain = &per_cu->cu->read_in_chain;
15224
15225 per_cu = next_cu;
15226 }
15227 }
15228
15229 /* Remove a single compilation unit from the cache. */
15230
15231 static void
15232 free_one_cached_comp_unit (void *target_cu)
15233 {
15234 struct dwarf2_per_cu_data *per_cu, **last_chain;
15235
15236 per_cu = dwarf2_per_objfile->read_in_chain;
15237 last_chain = &dwarf2_per_objfile->read_in_chain;
15238 while (per_cu != NULL)
15239 {
15240 struct dwarf2_per_cu_data *next_cu;
15241
15242 next_cu = per_cu->cu->read_in_chain;
15243
15244 if (per_cu->cu == target_cu)
15245 {
15246 free_one_comp_unit (per_cu->cu);
15247 *last_chain = next_cu;
15248 break;
15249 }
15250 else
15251 last_chain = &per_cu->cu->read_in_chain;
15252
15253 per_cu = next_cu;
15254 }
15255 }
15256
15257 /* Release all extra memory associated with OBJFILE. */
15258
15259 void
15260 dwarf2_free_objfile (struct objfile *objfile)
15261 {
15262 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15263
15264 if (dwarf2_per_objfile == NULL)
15265 return;
15266
15267 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15268 free_cached_comp_units (NULL);
15269
15270 if (dwarf2_per_objfile->quick_file_names_table)
15271 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15272
15273 /* Everything else should be on the objfile obstack. */
15274 }
15275
15276 /* A pair of DIE offset and GDB type pointer. We store these
15277 in a hash table separate from the DIEs, and preserve them
15278 when the DIEs are flushed out of cache. */
15279
15280 struct dwarf2_offset_and_type
15281 {
15282 unsigned int offset;
15283 struct type *type;
15284 };
15285
15286 /* Hash function for a dwarf2_offset_and_type. */
15287
15288 static hashval_t
15289 offset_and_type_hash (const void *item)
15290 {
15291 const struct dwarf2_offset_and_type *ofs = item;
15292
15293 return ofs->offset;
15294 }
15295
15296 /* Equality function for a dwarf2_offset_and_type. */
15297
15298 static int
15299 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15300 {
15301 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15302 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15303
15304 return ofs_lhs->offset == ofs_rhs->offset;
15305 }
15306
15307 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15308 table if necessary. For convenience, return TYPE.
15309
15310 The DIEs reading must have careful ordering to:
15311 * Not cause infite loops trying to read in DIEs as a prerequisite for
15312 reading current DIE.
15313 * Not trying to dereference contents of still incompletely read in types
15314 while reading in other DIEs.
15315 * Enable referencing still incompletely read in types just by a pointer to
15316 the type without accessing its fields.
15317
15318 Therefore caller should follow these rules:
15319 * Try to fetch any prerequisite types we may need to build this DIE type
15320 before building the type and calling set_die_type.
15321 * After building type call set_die_type for current DIE as soon as
15322 possible before fetching more types to complete the current type.
15323 * Make the type as complete as possible before fetching more types. */
15324
15325 static struct type *
15326 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15327 {
15328 struct dwarf2_offset_and_type **slot, ofs;
15329 struct objfile *objfile = cu->objfile;
15330 htab_t *type_hash_ptr;
15331
15332 /* For Ada types, make sure that the gnat-specific data is always
15333 initialized (if not already set). There are a few types where
15334 we should not be doing so, because the type-specific area is
15335 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15336 where the type-specific area is used to store the floatformat).
15337 But this is not a problem, because the gnat-specific information
15338 is actually not needed for these types. */
15339 if (need_gnat_info (cu)
15340 && TYPE_CODE (type) != TYPE_CODE_FUNC
15341 && TYPE_CODE (type) != TYPE_CODE_FLT
15342 && !HAVE_GNAT_AUX_INFO (type))
15343 INIT_GNAT_SPECIFIC (type);
15344
15345 if (cu->per_cu->from_debug_types)
15346 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15347 else
15348 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15349
15350 if (*type_hash_ptr == NULL)
15351 {
15352 *type_hash_ptr
15353 = htab_create_alloc_ex (127,
15354 offset_and_type_hash,
15355 offset_and_type_eq,
15356 NULL,
15357 &objfile->objfile_obstack,
15358 hashtab_obstack_allocate,
15359 dummy_obstack_deallocate);
15360 }
15361
15362 ofs.offset = die->offset;
15363 ofs.type = type;
15364 slot = (struct dwarf2_offset_and_type **)
15365 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15366 if (*slot)
15367 complaint (&symfile_complaints,
15368 _("A problem internal to GDB: DIE 0x%x has type already set"),
15369 die->offset);
15370 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15371 **slot = ofs;
15372 return type;
15373 }
15374
15375 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15376 table, or return NULL if the die does not have a saved type. */
15377
15378 static struct type *
15379 get_die_type_at_offset (unsigned int offset,
15380 struct dwarf2_per_cu_data *per_cu)
15381 {
15382 struct dwarf2_offset_and_type *slot, ofs;
15383 htab_t type_hash;
15384
15385 if (per_cu->from_debug_types)
15386 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15387 else
15388 type_hash = dwarf2_per_objfile->debug_info_type_hash;
15389 if (type_hash == NULL)
15390 return NULL;
15391
15392 ofs.offset = offset;
15393 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15394 if (slot)
15395 return slot->type;
15396 else
15397 return NULL;
15398 }
15399
15400 /* Look up the type for DIE in the appropriate type_hash table,
15401 or return NULL if DIE does not have a saved type. */
15402
15403 static struct type *
15404 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15405 {
15406 return get_die_type_at_offset (die->offset, cu->per_cu);
15407 }
15408
15409 /* Add a dependence relationship from CU to REF_PER_CU. */
15410
15411 static void
15412 dwarf2_add_dependence (struct dwarf2_cu *cu,
15413 struct dwarf2_per_cu_data *ref_per_cu)
15414 {
15415 void **slot;
15416
15417 if (cu->dependencies == NULL)
15418 cu->dependencies
15419 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15420 NULL, &cu->comp_unit_obstack,
15421 hashtab_obstack_allocate,
15422 dummy_obstack_deallocate);
15423
15424 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15425 if (*slot == NULL)
15426 *slot = ref_per_cu;
15427 }
15428
15429 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15430 Set the mark field in every compilation unit in the
15431 cache that we must keep because we are keeping CU. */
15432
15433 static int
15434 dwarf2_mark_helper (void **slot, void *data)
15435 {
15436 struct dwarf2_per_cu_data *per_cu;
15437
15438 per_cu = (struct dwarf2_per_cu_data *) *slot;
15439 if (per_cu->cu->mark)
15440 return 1;
15441 per_cu->cu->mark = 1;
15442
15443 if (per_cu->cu->dependencies != NULL)
15444 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15445
15446 return 1;
15447 }
15448
15449 /* Set the mark field in CU and in every other compilation unit in the
15450 cache that we must keep because we are keeping CU. */
15451
15452 static void
15453 dwarf2_mark (struct dwarf2_cu *cu)
15454 {
15455 if (cu->mark)
15456 return;
15457 cu->mark = 1;
15458 if (cu->dependencies != NULL)
15459 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15460 }
15461
15462 static void
15463 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15464 {
15465 while (per_cu)
15466 {
15467 per_cu->cu->mark = 0;
15468 per_cu = per_cu->cu->read_in_chain;
15469 }
15470 }
15471
15472 /* Trivial hash function for partial_die_info: the hash value of a DIE
15473 is its offset in .debug_info for this objfile. */
15474
15475 static hashval_t
15476 partial_die_hash (const void *item)
15477 {
15478 const struct partial_die_info *part_die = item;
15479
15480 return part_die->offset;
15481 }
15482
15483 /* Trivial comparison function for partial_die_info structures: two DIEs
15484 are equal if they have the same offset. */
15485
15486 static int
15487 partial_die_eq (const void *item_lhs, const void *item_rhs)
15488 {
15489 const struct partial_die_info *part_die_lhs = item_lhs;
15490 const struct partial_die_info *part_die_rhs = item_rhs;
15491
15492 return part_die_lhs->offset == part_die_rhs->offset;
15493 }
15494
15495 static struct cmd_list_element *set_dwarf2_cmdlist;
15496 static struct cmd_list_element *show_dwarf2_cmdlist;
15497
15498 static void
15499 set_dwarf2_cmd (char *args, int from_tty)
15500 {
15501 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15502 }
15503
15504 static void
15505 show_dwarf2_cmd (char *args, int from_tty)
15506 {
15507 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15508 }
15509
15510 /* If section described by INFO was mmapped, munmap it now. */
15511
15512 static void
15513 munmap_section_buffer (struct dwarf2_section_info *info)
15514 {
15515 if (info->map_addr != NULL)
15516 {
15517 #ifdef HAVE_MMAP
15518 int res;
15519
15520 res = munmap (info->map_addr, info->map_len);
15521 gdb_assert (res == 0);
15522 #else
15523 /* Without HAVE_MMAP, we should never be here to begin with. */
15524 gdb_assert_not_reached ("no mmap support");
15525 #endif
15526 }
15527 }
15528
15529 /* munmap debug sections for OBJFILE, if necessary. */
15530
15531 static void
15532 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15533 {
15534 struct dwarf2_per_objfile *data = d;
15535
15536 /* This is sorted according to the order they're defined in to make it easier
15537 to keep in sync. */
15538 munmap_section_buffer (&data->info);
15539 munmap_section_buffer (&data->abbrev);
15540 munmap_section_buffer (&data->line);
15541 munmap_section_buffer (&data->loc);
15542 munmap_section_buffer (&data->macinfo);
15543 munmap_section_buffer (&data->str);
15544 munmap_section_buffer (&data->ranges);
15545 munmap_section_buffer (&data->types);
15546 munmap_section_buffer (&data->frame);
15547 munmap_section_buffer (&data->eh_frame);
15548 munmap_section_buffer (&data->gdb_index);
15549 }
15550
15551 \f
15552 /* The "save gdb-index" command. */
15553
15554 /* The contents of the hash table we create when building the string
15555 table. */
15556 struct strtab_entry
15557 {
15558 offset_type offset;
15559 const char *str;
15560 };
15561
15562 /* Hash function for a strtab_entry.
15563
15564 Function is used only during write_hash_table so no index format backward
15565 compatibility is needed. */
15566
15567 static hashval_t
15568 hash_strtab_entry (const void *e)
15569 {
15570 const struct strtab_entry *entry = e;
15571 return mapped_index_string_hash (INT_MAX, entry->str);
15572 }
15573
15574 /* Equality function for a strtab_entry. */
15575
15576 static int
15577 eq_strtab_entry (const void *a, const void *b)
15578 {
15579 const struct strtab_entry *ea = a;
15580 const struct strtab_entry *eb = b;
15581 return !strcmp (ea->str, eb->str);
15582 }
15583
15584 /* Create a strtab_entry hash table. */
15585
15586 static htab_t
15587 create_strtab (void)
15588 {
15589 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15590 xfree, xcalloc, xfree);
15591 }
15592
15593 /* Add a string to the constant pool. Return the string's offset in
15594 host order. */
15595
15596 static offset_type
15597 add_string (htab_t table, struct obstack *cpool, const char *str)
15598 {
15599 void **slot;
15600 struct strtab_entry entry;
15601 struct strtab_entry *result;
15602
15603 entry.str = str;
15604 slot = htab_find_slot (table, &entry, INSERT);
15605 if (*slot)
15606 result = *slot;
15607 else
15608 {
15609 result = XNEW (struct strtab_entry);
15610 result->offset = obstack_object_size (cpool);
15611 result->str = str;
15612 obstack_grow_str0 (cpool, str);
15613 *slot = result;
15614 }
15615 return result->offset;
15616 }
15617
15618 /* An entry in the symbol table. */
15619 struct symtab_index_entry
15620 {
15621 /* The name of the symbol. */
15622 const char *name;
15623 /* The offset of the name in the constant pool. */
15624 offset_type index_offset;
15625 /* A sorted vector of the indices of all the CUs that hold an object
15626 of this name. */
15627 VEC (offset_type) *cu_indices;
15628 };
15629
15630 /* The symbol table. This is a power-of-2-sized hash table. */
15631 struct mapped_symtab
15632 {
15633 offset_type n_elements;
15634 offset_type size;
15635 struct symtab_index_entry **data;
15636 };
15637
15638 /* Hash function for a symtab_index_entry. */
15639
15640 static hashval_t
15641 hash_symtab_entry (const void *e)
15642 {
15643 const struct symtab_index_entry *entry = e;
15644 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15645 sizeof (offset_type) * VEC_length (offset_type,
15646 entry->cu_indices),
15647 0);
15648 }
15649
15650 /* Equality function for a symtab_index_entry. */
15651
15652 static int
15653 eq_symtab_entry (const void *a, const void *b)
15654 {
15655 const struct symtab_index_entry *ea = a;
15656 const struct symtab_index_entry *eb = b;
15657 int len = VEC_length (offset_type, ea->cu_indices);
15658 if (len != VEC_length (offset_type, eb->cu_indices))
15659 return 0;
15660 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15661 VEC_address (offset_type, eb->cu_indices),
15662 sizeof (offset_type) * len);
15663 }
15664
15665 /* Destroy a symtab_index_entry. */
15666
15667 static void
15668 delete_symtab_entry (void *p)
15669 {
15670 struct symtab_index_entry *entry = p;
15671 VEC_free (offset_type, entry->cu_indices);
15672 xfree (entry);
15673 }
15674
15675 /* Create a hash table holding symtab_index_entry objects. */
15676
15677 static htab_t
15678 create_symbol_hash_table (void)
15679 {
15680 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15681 delete_symtab_entry, xcalloc, xfree);
15682 }
15683
15684 /* Create a new mapped symtab object. */
15685
15686 static struct mapped_symtab *
15687 create_mapped_symtab (void)
15688 {
15689 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15690 symtab->n_elements = 0;
15691 symtab->size = 1024;
15692 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15693 return symtab;
15694 }
15695
15696 /* Destroy a mapped_symtab. */
15697
15698 static void
15699 cleanup_mapped_symtab (void *p)
15700 {
15701 struct mapped_symtab *symtab = p;
15702 /* The contents of the array are freed when the other hash table is
15703 destroyed. */
15704 xfree (symtab->data);
15705 xfree (symtab);
15706 }
15707
15708 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15709 the slot.
15710
15711 Function is used only during write_hash_table so no index format backward
15712 compatibility is needed. */
15713
15714 static struct symtab_index_entry **
15715 find_slot (struct mapped_symtab *symtab, const char *name)
15716 {
15717 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15718
15719 index = hash & (symtab->size - 1);
15720 step = ((hash * 17) & (symtab->size - 1)) | 1;
15721
15722 for (;;)
15723 {
15724 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15725 return &symtab->data[index];
15726 index = (index + step) & (symtab->size - 1);
15727 }
15728 }
15729
15730 /* Expand SYMTAB's hash table. */
15731
15732 static void
15733 hash_expand (struct mapped_symtab *symtab)
15734 {
15735 offset_type old_size = symtab->size;
15736 offset_type i;
15737 struct symtab_index_entry **old_entries = symtab->data;
15738
15739 symtab->size *= 2;
15740 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15741
15742 for (i = 0; i < old_size; ++i)
15743 {
15744 if (old_entries[i])
15745 {
15746 struct symtab_index_entry **slot = find_slot (symtab,
15747 old_entries[i]->name);
15748 *slot = old_entries[i];
15749 }
15750 }
15751
15752 xfree (old_entries);
15753 }
15754
15755 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15756 is the index of the CU in which the symbol appears. */
15757
15758 static void
15759 add_index_entry (struct mapped_symtab *symtab, const char *name,
15760 offset_type cu_index)
15761 {
15762 struct symtab_index_entry **slot;
15763
15764 ++symtab->n_elements;
15765 if (4 * symtab->n_elements / 3 >= symtab->size)
15766 hash_expand (symtab);
15767
15768 slot = find_slot (symtab, name);
15769 if (!*slot)
15770 {
15771 *slot = XNEW (struct symtab_index_entry);
15772 (*slot)->name = name;
15773 (*slot)->cu_indices = NULL;
15774 }
15775 /* Don't push an index twice. Due to how we add entries we only
15776 have to check the last one. */
15777 if (VEC_empty (offset_type, (*slot)->cu_indices)
15778 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
15779 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15780 }
15781
15782 /* Add a vector of indices to the constant pool. */
15783
15784 static offset_type
15785 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15786 struct symtab_index_entry *entry)
15787 {
15788 void **slot;
15789
15790 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15791 if (!*slot)
15792 {
15793 offset_type len = VEC_length (offset_type, entry->cu_indices);
15794 offset_type val = MAYBE_SWAP (len);
15795 offset_type iter;
15796 int i;
15797
15798 *slot = entry;
15799 entry->index_offset = obstack_object_size (cpool);
15800
15801 obstack_grow (cpool, &val, sizeof (val));
15802 for (i = 0;
15803 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15804 ++i)
15805 {
15806 val = MAYBE_SWAP (iter);
15807 obstack_grow (cpool, &val, sizeof (val));
15808 }
15809 }
15810 else
15811 {
15812 struct symtab_index_entry *old_entry = *slot;
15813 entry->index_offset = old_entry->index_offset;
15814 entry = old_entry;
15815 }
15816 return entry->index_offset;
15817 }
15818
15819 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15820 constant pool entries going into the obstack CPOOL. */
15821
15822 static void
15823 write_hash_table (struct mapped_symtab *symtab,
15824 struct obstack *output, struct obstack *cpool)
15825 {
15826 offset_type i;
15827 htab_t symbol_hash_table;
15828 htab_t str_table;
15829
15830 symbol_hash_table = create_symbol_hash_table ();
15831 str_table = create_strtab ();
15832
15833 /* We add all the index vectors to the constant pool first, to
15834 ensure alignment is ok. */
15835 for (i = 0; i < symtab->size; ++i)
15836 {
15837 if (symtab->data[i])
15838 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15839 }
15840
15841 /* Now write out the hash table. */
15842 for (i = 0; i < symtab->size; ++i)
15843 {
15844 offset_type str_off, vec_off;
15845
15846 if (symtab->data[i])
15847 {
15848 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15849 vec_off = symtab->data[i]->index_offset;
15850 }
15851 else
15852 {
15853 /* While 0 is a valid constant pool index, it is not valid
15854 to have 0 for both offsets. */
15855 str_off = 0;
15856 vec_off = 0;
15857 }
15858
15859 str_off = MAYBE_SWAP (str_off);
15860 vec_off = MAYBE_SWAP (vec_off);
15861
15862 obstack_grow (output, &str_off, sizeof (str_off));
15863 obstack_grow (output, &vec_off, sizeof (vec_off));
15864 }
15865
15866 htab_delete (str_table);
15867 htab_delete (symbol_hash_table);
15868 }
15869
15870 /* Struct to map psymtab to CU index in the index file. */
15871 struct psymtab_cu_index_map
15872 {
15873 struct partial_symtab *psymtab;
15874 unsigned int cu_index;
15875 };
15876
15877 static hashval_t
15878 hash_psymtab_cu_index (const void *item)
15879 {
15880 const struct psymtab_cu_index_map *map = item;
15881
15882 return htab_hash_pointer (map->psymtab);
15883 }
15884
15885 static int
15886 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15887 {
15888 const struct psymtab_cu_index_map *lhs = item_lhs;
15889 const struct psymtab_cu_index_map *rhs = item_rhs;
15890
15891 return lhs->psymtab == rhs->psymtab;
15892 }
15893
15894 /* Helper struct for building the address table. */
15895 struct addrmap_index_data
15896 {
15897 struct objfile *objfile;
15898 struct obstack *addr_obstack;
15899 htab_t cu_index_htab;
15900
15901 /* Non-zero if the previous_* fields are valid.
15902 We can't write an entry until we see the next entry (since it is only then
15903 that we know the end of the entry). */
15904 int previous_valid;
15905 /* Index of the CU in the table of all CUs in the index file. */
15906 unsigned int previous_cu_index;
15907 /* Start address of the CU. */
15908 CORE_ADDR previous_cu_start;
15909 };
15910
15911 /* Write an address entry to OBSTACK. */
15912
15913 static void
15914 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15915 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15916 {
15917 offset_type cu_index_to_write;
15918 char addr[8];
15919 CORE_ADDR baseaddr;
15920
15921 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15922
15923 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15924 obstack_grow (obstack, addr, 8);
15925 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15926 obstack_grow (obstack, addr, 8);
15927 cu_index_to_write = MAYBE_SWAP (cu_index);
15928 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15929 }
15930
15931 /* Worker function for traversing an addrmap to build the address table. */
15932
15933 static int
15934 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15935 {
15936 struct addrmap_index_data *data = datap;
15937 struct partial_symtab *pst = obj;
15938 offset_type cu_index;
15939 void **slot;
15940
15941 if (data->previous_valid)
15942 add_address_entry (data->objfile, data->addr_obstack,
15943 data->previous_cu_start, start_addr,
15944 data->previous_cu_index);
15945
15946 data->previous_cu_start = start_addr;
15947 if (pst != NULL)
15948 {
15949 struct psymtab_cu_index_map find_map, *map;
15950 find_map.psymtab = pst;
15951 map = htab_find (data->cu_index_htab, &find_map);
15952 gdb_assert (map != NULL);
15953 data->previous_cu_index = map->cu_index;
15954 data->previous_valid = 1;
15955 }
15956 else
15957 data->previous_valid = 0;
15958
15959 return 0;
15960 }
15961
15962 /* Write OBJFILE's address map to OBSTACK.
15963 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15964 in the index file. */
15965
15966 static void
15967 write_address_map (struct objfile *objfile, struct obstack *obstack,
15968 htab_t cu_index_htab)
15969 {
15970 struct addrmap_index_data addrmap_index_data;
15971
15972 /* When writing the address table, we have to cope with the fact that
15973 the addrmap iterator only provides the start of a region; we have to
15974 wait until the next invocation to get the start of the next region. */
15975
15976 addrmap_index_data.objfile = objfile;
15977 addrmap_index_data.addr_obstack = obstack;
15978 addrmap_index_data.cu_index_htab = cu_index_htab;
15979 addrmap_index_data.previous_valid = 0;
15980
15981 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15982 &addrmap_index_data);
15983
15984 /* It's highly unlikely the last entry (end address = 0xff...ff)
15985 is valid, but we should still handle it.
15986 The end address is recorded as the start of the next region, but that
15987 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15988 anyway. */
15989 if (addrmap_index_data.previous_valid)
15990 add_address_entry (objfile, obstack,
15991 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15992 addrmap_index_data.previous_cu_index);
15993 }
15994
15995 /* Add a list of partial symbols to SYMTAB. */
15996
15997 static void
15998 write_psymbols (struct mapped_symtab *symtab,
15999 htab_t psyms_seen,
16000 struct partial_symbol **psymp,
16001 int count,
16002 offset_type cu_index,
16003 int is_static)
16004 {
16005 for (; count-- > 0; ++psymp)
16006 {
16007 void **slot, *lookup;
16008
16009 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16010 error (_("Ada is not currently supported by the index"));
16011
16012 /* We only want to add a given psymbol once. However, we also
16013 want to account for whether it is global or static. So, we
16014 may add it twice, using slightly different values. */
16015 if (is_static)
16016 {
16017 uintptr_t val = 1 | (uintptr_t) *psymp;
16018
16019 lookup = (void *) val;
16020 }
16021 else
16022 lookup = *psymp;
16023
16024 /* Only add a given psymbol once. */
16025 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16026 if (!*slot)
16027 {
16028 *slot = lookup;
16029 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
16030 }
16031 }
16032 }
16033
16034 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16035 exception if there is an error. */
16036
16037 static void
16038 write_obstack (FILE *file, struct obstack *obstack)
16039 {
16040 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16041 file)
16042 != obstack_object_size (obstack))
16043 error (_("couldn't data write to file"));
16044 }
16045
16046 /* Unlink a file if the argument is not NULL. */
16047
16048 static void
16049 unlink_if_set (void *p)
16050 {
16051 char **filename = p;
16052 if (*filename)
16053 unlink (*filename);
16054 }
16055
16056 /* A helper struct used when iterating over debug_types. */
16057 struct signatured_type_index_data
16058 {
16059 struct objfile *objfile;
16060 struct mapped_symtab *symtab;
16061 struct obstack *types_list;
16062 htab_t psyms_seen;
16063 int cu_index;
16064 };
16065
16066 /* A helper function that writes a single signatured_type to an
16067 obstack. */
16068
16069 static int
16070 write_one_signatured_type (void **slot, void *d)
16071 {
16072 struct signatured_type_index_data *info = d;
16073 struct signatured_type *entry = (struct signatured_type *) *slot;
16074 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16075 struct partial_symtab *psymtab = per_cu->v.psymtab;
16076 gdb_byte val[8];
16077
16078 write_psymbols (info->symtab,
16079 info->psyms_seen,
16080 info->objfile->global_psymbols.list
16081 + psymtab->globals_offset,
16082 psymtab->n_global_syms, info->cu_index,
16083 0);
16084 write_psymbols (info->symtab,
16085 info->psyms_seen,
16086 info->objfile->static_psymbols.list
16087 + psymtab->statics_offset,
16088 psymtab->n_static_syms, info->cu_index,
16089 1);
16090
16091 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16092 obstack_grow (info->types_list, val, 8);
16093 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16094 obstack_grow (info->types_list, val, 8);
16095 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16096 obstack_grow (info->types_list, val, 8);
16097
16098 ++info->cu_index;
16099
16100 return 1;
16101 }
16102
16103 /* A cleanup function for an htab_t. */
16104
16105 static void
16106 cleanup_htab (void *arg)
16107 {
16108 htab_delete (arg);
16109 }
16110
16111 /* Create an index file for OBJFILE in the directory DIR. */
16112
16113 static void
16114 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16115 {
16116 struct cleanup *cleanup;
16117 char *filename, *cleanup_filename;
16118 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16119 struct obstack cu_list, types_cu_list;
16120 int i;
16121 FILE *out_file;
16122 struct mapped_symtab *symtab;
16123 offset_type val, size_of_contents, total_len;
16124 struct stat st;
16125 char buf[8];
16126 htab_t psyms_seen;
16127 htab_t cu_index_htab;
16128 struct psymtab_cu_index_map *psymtab_cu_index_map;
16129
16130 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16131 return;
16132
16133 if (dwarf2_per_objfile->using_index)
16134 error (_("Cannot use an index to create the index"));
16135
16136 if (stat (objfile->name, &st) < 0)
16137 perror_with_name (objfile->name);
16138
16139 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16140 INDEX_SUFFIX, (char *) NULL);
16141 cleanup = make_cleanup (xfree, filename);
16142
16143 out_file = fopen (filename, "wb");
16144 if (!out_file)
16145 error (_("Can't open `%s' for writing"), filename);
16146
16147 cleanup_filename = filename;
16148 make_cleanup (unlink_if_set, &cleanup_filename);
16149
16150 symtab = create_mapped_symtab ();
16151 make_cleanup (cleanup_mapped_symtab, symtab);
16152
16153 obstack_init (&addr_obstack);
16154 make_cleanup_obstack_free (&addr_obstack);
16155
16156 obstack_init (&cu_list);
16157 make_cleanup_obstack_free (&cu_list);
16158
16159 obstack_init (&types_cu_list);
16160 make_cleanup_obstack_free (&types_cu_list);
16161
16162 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16163 NULL, xcalloc, xfree);
16164 make_cleanup (cleanup_htab, psyms_seen);
16165
16166 /* While we're scanning CU's create a table that maps a psymtab pointer
16167 (which is what addrmap records) to its index (which is what is recorded
16168 in the index file). This will later be needed to write the address
16169 table. */
16170 cu_index_htab = htab_create_alloc (100,
16171 hash_psymtab_cu_index,
16172 eq_psymtab_cu_index,
16173 NULL, xcalloc, xfree);
16174 make_cleanup (cleanup_htab, cu_index_htab);
16175 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16176 xmalloc (sizeof (struct psymtab_cu_index_map)
16177 * dwarf2_per_objfile->n_comp_units);
16178 make_cleanup (xfree, psymtab_cu_index_map);
16179
16180 /* The CU list is already sorted, so we don't need to do additional
16181 work here. Also, the debug_types entries do not appear in
16182 all_comp_units, but only in their own hash table. */
16183 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16184 {
16185 struct dwarf2_per_cu_data *per_cu
16186 = dwarf2_per_objfile->all_comp_units[i];
16187 struct partial_symtab *psymtab = per_cu->v.psymtab;
16188 gdb_byte val[8];
16189 struct psymtab_cu_index_map *map;
16190 void **slot;
16191
16192 write_psymbols (symtab,
16193 psyms_seen,
16194 objfile->global_psymbols.list + psymtab->globals_offset,
16195 psymtab->n_global_syms, i,
16196 0);
16197 write_psymbols (symtab,
16198 psyms_seen,
16199 objfile->static_psymbols.list + psymtab->statics_offset,
16200 psymtab->n_static_syms, i,
16201 1);
16202
16203 map = &psymtab_cu_index_map[i];
16204 map->psymtab = psymtab;
16205 map->cu_index = i;
16206 slot = htab_find_slot (cu_index_htab, map, INSERT);
16207 gdb_assert (slot != NULL);
16208 gdb_assert (*slot == NULL);
16209 *slot = map;
16210
16211 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16212 obstack_grow (&cu_list, val, 8);
16213 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16214 obstack_grow (&cu_list, val, 8);
16215 }
16216
16217 /* Dump the address map. */
16218 write_address_map (objfile, &addr_obstack, cu_index_htab);
16219
16220 /* Write out the .debug_type entries, if any. */
16221 if (dwarf2_per_objfile->signatured_types)
16222 {
16223 struct signatured_type_index_data sig_data;
16224
16225 sig_data.objfile = objfile;
16226 sig_data.symtab = symtab;
16227 sig_data.types_list = &types_cu_list;
16228 sig_data.psyms_seen = psyms_seen;
16229 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16230 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16231 write_one_signatured_type, &sig_data);
16232 }
16233
16234 obstack_init (&constant_pool);
16235 make_cleanup_obstack_free (&constant_pool);
16236 obstack_init (&symtab_obstack);
16237 make_cleanup_obstack_free (&symtab_obstack);
16238 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16239
16240 obstack_init (&contents);
16241 make_cleanup_obstack_free (&contents);
16242 size_of_contents = 6 * sizeof (offset_type);
16243 total_len = size_of_contents;
16244
16245 /* The version number. */
16246 val = MAYBE_SWAP (5);
16247 obstack_grow (&contents, &val, sizeof (val));
16248
16249 /* The offset of the CU list from the start of the file. */
16250 val = MAYBE_SWAP (total_len);
16251 obstack_grow (&contents, &val, sizeof (val));
16252 total_len += obstack_object_size (&cu_list);
16253
16254 /* The offset of the types CU list from the start of the file. */
16255 val = MAYBE_SWAP (total_len);
16256 obstack_grow (&contents, &val, sizeof (val));
16257 total_len += obstack_object_size (&types_cu_list);
16258
16259 /* The offset of the address table from the start of the file. */
16260 val = MAYBE_SWAP (total_len);
16261 obstack_grow (&contents, &val, sizeof (val));
16262 total_len += obstack_object_size (&addr_obstack);
16263
16264 /* The offset of the symbol table from the start of the file. */
16265 val = MAYBE_SWAP (total_len);
16266 obstack_grow (&contents, &val, sizeof (val));
16267 total_len += obstack_object_size (&symtab_obstack);
16268
16269 /* The offset of the constant pool from the start of the file. */
16270 val = MAYBE_SWAP (total_len);
16271 obstack_grow (&contents, &val, sizeof (val));
16272 total_len += obstack_object_size (&constant_pool);
16273
16274 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16275
16276 write_obstack (out_file, &contents);
16277 write_obstack (out_file, &cu_list);
16278 write_obstack (out_file, &types_cu_list);
16279 write_obstack (out_file, &addr_obstack);
16280 write_obstack (out_file, &symtab_obstack);
16281 write_obstack (out_file, &constant_pool);
16282
16283 fclose (out_file);
16284
16285 /* We want to keep the file, so we set cleanup_filename to NULL
16286 here. See unlink_if_set. */
16287 cleanup_filename = NULL;
16288
16289 do_cleanups (cleanup);
16290 }
16291
16292 /* Implementation of the `save gdb-index' command.
16293
16294 Note that the file format used by this command is documented in the
16295 GDB manual. Any changes here must be documented there. */
16296
16297 static void
16298 save_gdb_index_command (char *arg, int from_tty)
16299 {
16300 struct objfile *objfile;
16301
16302 if (!arg || !*arg)
16303 error (_("usage: save gdb-index DIRECTORY"));
16304
16305 ALL_OBJFILES (objfile)
16306 {
16307 struct stat st;
16308
16309 /* If the objfile does not correspond to an actual file, skip it. */
16310 if (stat (objfile->name, &st) < 0)
16311 continue;
16312
16313 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16314 if (dwarf2_per_objfile)
16315 {
16316 volatile struct gdb_exception except;
16317
16318 TRY_CATCH (except, RETURN_MASK_ERROR)
16319 {
16320 write_psymtabs_to_index (objfile, arg);
16321 }
16322 if (except.reason < 0)
16323 exception_fprintf (gdb_stderr, except,
16324 _("Error while writing index for `%s': "),
16325 objfile->name);
16326 }
16327 }
16328 }
16329
16330 \f
16331
16332 int dwarf2_always_disassemble;
16333
16334 static void
16335 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16336 struct cmd_list_element *c, const char *value)
16337 {
16338 fprintf_filtered (file,
16339 _("Whether to always disassemble "
16340 "DWARF expressions is %s.\n"),
16341 value);
16342 }
16343
16344 static void
16345 show_check_physname (struct ui_file *file, int from_tty,
16346 struct cmd_list_element *c, const char *value)
16347 {
16348 fprintf_filtered (file,
16349 _("Whether to check \"physname\" is %s.\n"),
16350 value);
16351 }
16352
16353 void _initialize_dwarf2_read (void);
16354
16355 void
16356 _initialize_dwarf2_read (void)
16357 {
16358 struct cmd_list_element *c;
16359
16360 dwarf2_objfile_data_key
16361 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16362
16363 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16364 Set DWARF 2 specific variables.\n\
16365 Configure DWARF 2 variables such as the cache size"),
16366 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16367 0/*allow-unknown*/, &maintenance_set_cmdlist);
16368
16369 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16370 Show DWARF 2 specific variables\n\
16371 Show DWARF 2 variables such as the cache size"),
16372 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16373 0/*allow-unknown*/, &maintenance_show_cmdlist);
16374
16375 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16376 &dwarf2_max_cache_age, _("\
16377 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16378 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16379 A higher limit means that cached compilation units will be stored\n\
16380 in memory longer, and more total memory will be used. Zero disables\n\
16381 caching, which can slow down startup."),
16382 NULL,
16383 show_dwarf2_max_cache_age,
16384 &set_dwarf2_cmdlist,
16385 &show_dwarf2_cmdlist);
16386
16387 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16388 &dwarf2_always_disassemble, _("\
16389 Set whether `info address' always disassembles DWARF expressions."), _("\
16390 Show whether `info address' always disassembles DWARF expressions."), _("\
16391 When enabled, DWARF expressions are always printed in an assembly-like\n\
16392 syntax. When disabled, expressions will be printed in a more\n\
16393 conversational style, when possible."),
16394 NULL,
16395 show_dwarf2_always_disassemble,
16396 &set_dwarf2_cmdlist,
16397 &show_dwarf2_cmdlist);
16398
16399 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16400 Set debugging of the dwarf2 DIE reader."), _("\
16401 Show debugging of the dwarf2 DIE reader."), _("\
16402 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16403 The value is the maximum depth to print."),
16404 NULL,
16405 NULL,
16406 &setdebuglist, &showdebuglist);
16407
16408 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
16409 Set cross-checking of \"physname\" code against demangler."), _("\
16410 Show cross-checking of \"physname\" code against demangler."), _("\
16411 When enabled, GDB's internal \"physname\" code is checked against\n\
16412 the demangler."),
16413 NULL, show_check_physname,
16414 &setdebuglist, &showdebuglist);
16415
16416 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16417 _("\
16418 Save a gdb-index file.\n\
16419 Usage: save gdb-index DIRECTORY"),
16420 &save_cmdlist);
16421 set_cmd_completer (c, filename_completer);
16422 }