../commit.txt~
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011
5 Free Software Foundation, Inc.
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 support.
13
14 This file is part of GDB.
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h" /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60 #include <ctype.h>
61
62 #include <fcntl.h>
63 #include "gdb_string.h"
64 #include "gdb_assert.h"
65 #include <sys/types.h>
66 #ifdef HAVE_ZLIB_H
67 #include <zlib.h>
68 #endif
69 #ifdef HAVE_MMAP
70 #include <sys/mman.h>
71 #ifndef MAP_FAILED
72 #define MAP_FAILED ((void *) -1)
73 #endif
74 #endif
75
76 typedef struct symbol *symbolp;
77 DEF_VEC_P (symbolp);
78
79 #if 0
80 /* .debug_info header for a compilation unit
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83 typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92 _COMP_UNIT_HEADER;
93 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
94 #endif
95
96 /* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99 typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117 _STATEMENT_PROLOGUE;
118
119 /* When non-zero, dump DIEs after they are read in. */
120 static int dwarf2_die_debug = 0;
121
122 static int pagesize;
123
124 /* When set, the file that we're processing is known to have debugging
125 info for C++ namespaces. GCC 3.3.x did not produce this information,
126 but later versions do. */
127
128 static int processing_has_namespace_info;
129
130 static const struct objfile_data *dwarf2_objfile_data_key;
131
132 struct dwarf2_section_info
133 {
134 asection *asection;
135 gdb_byte *buffer;
136 bfd_size_type size;
137 int was_mmapped;
138 /* True if we have tried to read this section. */
139 int readin;
140 };
141
142 /* All offsets in the index are of this type. It must be
143 architecture-independent. */
144 typedef uint32_t offset_type;
145
146 DEF_VEC_I (offset_type);
147
148 /* A description of the mapped index. The file format is described in
149 a comment by the code that writes the index. */
150 struct mapped_index
151 {
152 /* Index data format version. */
153 int version;
154
155 /* The total length of the buffer. */
156 off_t total_size;
157
158 /* A pointer to the address table data. */
159 const gdb_byte *address_table;
160
161 /* Size of the address table data in bytes. */
162 offset_type address_table_size;
163
164 /* The symbol table, implemented as a hash table. */
165 const offset_type *symbol_table;
166
167 /* Size in slots, each slot is 2 offset_types. */
168 offset_type symbol_table_slots;
169
170 /* A pointer to the constant pool. */
171 const char *constant_pool;
172 };
173
174 struct dwarf2_per_objfile
175 {
176 struct dwarf2_section_info info;
177 struct dwarf2_section_info abbrev;
178 struct dwarf2_section_info line;
179 struct dwarf2_section_info loc;
180 struct dwarf2_section_info macinfo;
181 struct dwarf2_section_info str;
182 struct dwarf2_section_info ranges;
183 struct dwarf2_section_info types;
184 struct dwarf2_section_info frame;
185 struct dwarf2_section_info eh_frame;
186 struct dwarf2_section_info gdb_index;
187
188 /* Back link. */
189 struct objfile *objfile;
190
191 /* A list of all the compilation units. This is used to locate
192 the target compilation unit of a particular reference. */
193 struct dwarf2_per_cu_data **all_comp_units;
194
195 /* The number of compilation units in ALL_COMP_UNITS. */
196 int n_comp_units;
197
198 /* The number of .debug_types-related CUs. */
199 int n_type_comp_units;
200
201 /* The .debug_types-related CUs. */
202 struct dwarf2_per_cu_data **type_comp_units;
203
204 /* A chain of compilation units that are currently read in, so that
205 they can be freed later. */
206 struct dwarf2_per_cu_data *read_in_chain;
207
208 /* A table mapping .debug_types signatures to its signatured_type entry.
209 This is NULL if the .debug_types section hasn't been read in yet. */
210 htab_t signatured_types;
211
212 /* A flag indicating wether this objfile has a section loaded at a
213 VMA of 0. */
214 int has_section_at_zero;
215
216 /* True if we are using the mapped index,
217 or we are faking it for OBJF_READNOW's sake. */
218 unsigned char using_index;
219
220 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
221 struct mapped_index *index_table;
222
223 /* When using index_table, this keeps track of all quick_file_names entries.
224 TUs can share line table entries with CUs or other TUs, and there can be
225 a lot more TUs than unique line tables, so we maintain a separate table
226 of all line table entries to support the sharing. */
227 htab_t quick_file_names_table;
228
229 /* Set during partial symbol reading, to prevent queueing of full
230 symbols. */
231 int reading_partial_symbols;
232
233 /* Table mapping type .debug_info DIE offsets to types.
234 This is NULL if not allocated yet.
235 It (currently) makes sense to allocate debug_types_type_hash lazily.
236 To keep things simple we allocate both lazily. */
237 htab_t debug_info_type_hash;
238
239 /* Table mapping type .debug_types DIE offsets to types.
240 This is NULL if not allocated yet. */
241 htab_t debug_types_type_hash;
242 };
243
244 static struct dwarf2_per_objfile *dwarf2_per_objfile;
245
246 /* names of the debugging sections */
247
248 /* Note that if the debugging section has been compressed, it might
249 have a name like .zdebug_info. */
250
251 #define INFO_SECTION "debug_info"
252 #define ABBREV_SECTION "debug_abbrev"
253 #define LINE_SECTION "debug_line"
254 #define LOC_SECTION "debug_loc"
255 #define MACINFO_SECTION "debug_macinfo"
256 #define STR_SECTION "debug_str"
257 #define RANGES_SECTION "debug_ranges"
258 #define TYPES_SECTION "debug_types"
259 #define FRAME_SECTION "debug_frame"
260 #define EH_FRAME_SECTION "eh_frame"
261 #define GDB_INDEX_SECTION "gdb_index"
262
263 /* local data types */
264
265 /* We hold several abbreviation tables in memory at the same time. */
266 #ifndef ABBREV_HASH_SIZE
267 #define ABBREV_HASH_SIZE 121
268 #endif
269
270 /* The data in a compilation unit header, after target2host
271 translation, looks like this. */
272 struct comp_unit_head
273 {
274 unsigned int length;
275 short version;
276 unsigned char addr_size;
277 unsigned char signed_addr_p;
278 unsigned int abbrev_offset;
279
280 /* Size of file offsets; either 4 or 8. */
281 unsigned int offset_size;
282
283 /* Size of the length field; either 4 or 12. */
284 unsigned int initial_length_size;
285
286 /* Offset to the first byte of this compilation unit header in the
287 .debug_info section, for resolving relative reference dies. */
288 unsigned int offset;
289
290 /* Offset to first die in this cu from the start of the cu.
291 This will be the first byte following the compilation unit header. */
292 unsigned int first_die_offset;
293 };
294
295 /* Type used for delaying computation of method physnames.
296 See comments for compute_delayed_physnames. */
297 struct delayed_method_info
298 {
299 /* The type to which the method is attached, i.e., its parent class. */
300 struct type *type;
301
302 /* The index of the method in the type's function fieldlists. */
303 int fnfield_index;
304
305 /* The index of the method in the fieldlist. */
306 int index;
307
308 /* The name of the DIE. */
309 const char *name;
310
311 /* The DIE associated with this method. */
312 struct die_info *die;
313 };
314
315 typedef struct delayed_method_info delayed_method_info;
316 DEF_VEC_O (delayed_method_info);
317
318 /* Internal state when decoding a particular compilation unit. */
319 struct dwarf2_cu
320 {
321 /* The objfile containing this compilation unit. */
322 struct objfile *objfile;
323
324 /* The header of the compilation unit. */
325 struct comp_unit_head header;
326
327 /* Base address of this compilation unit. */
328 CORE_ADDR base_address;
329
330 /* Non-zero if base_address has been set. */
331 int base_known;
332
333 struct function_range *first_fn, *last_fn, *cached_fn;
334
335 /* The language we are debugging. */
336 enum language language;
337 const struct language_defn *language_defn;
338
339 const char *producer;
340
341 /* The generic symbol table building routines have separate lists for
342 file scope symbols and all all other scopes (local scopes). So
343 we need to select the right one to pass to add_symbol_to_list().
344 We do it by keeping a pointer to the correct list in list_in_scope.
345
346 FIXME: The original dwarf code just treated the file scope as the
347 first local scope, and all other local scopes as nested local
348 scopes, and worked fine. Check to see if we really need to
349 distinguish these in buildsym.c. */
350 struct pending **list_in_scope;
351
352 /* DWARF abbreviation table associated with this compilation unit. */
353 struct abbrev_info **dwarf2_abbrevs;
354
355 /* Storage for the abbrev table. */
356 struct obstack abbrev_obstack;
357
358 /* Hash table holding all the loaded partial DIEs. */
359 htab_t partial_dies;
360
361 /* Storage for things with the same lifetime as this read-in compilation
362 unit, including partial DIEs. */
363 struct obstack comp_unit_obstack;
364
365 /* When multiple dwarf2_cu structures are living in memory, this field
366 chains them all together, so that they can be released efficiently.
367 We will probably also want a generation counter so that most-recently-used
368 compilation units are cached... */
369 struct dwarf2_per_cu_data *read_in_chain;
370
371 /* Backchain to our per_cu entry if the tree has been built. */
372 struct dwarf2_per_cu_data *per_cu;
373
374 /* How many compilation units ago was this CU last referenced? */
375 int last_used;
376
377 /* A hash table of die offsets for following references. */
378 htab_t die_hash;
379
380 /* Full DIEs if read in. */
381 struct die_info *dies;
382
383 /* A set of pointers to dwarf2_per_cu_data objects for compilation
384 units referenced by this one. Only set during full symbol processing;
385 partial symbol tables do not have dependencies. */
386 htab_t dependencies;
387
388 /* Header data from the line table, during full symbol processing. */
389 struct line_header *line_header;
390
391 /* A list of methods which need to have physnames computed
392 after all type information has been read. */
393 VEC (delayed_method_info) *method_list;
394
395 /* Mark used when releasing cached dies. */
396 unsigned int mark : 1;
397
398 /* This flag will be set if this compilation unit might include
399 inter-compilation-unit references. */
400 unsigned int has_form_ref_addr : 1;
401
402 /* This flag will be set if this compilation unit includes any
403 DW_TAG_namespace DIEs. If we know that there are explicit
404 DIEs for namespaces, we don't need to try to infer them
405 from mangled names. */
406 unsigned int has_namespace_info : 1;
407
408 /* This CU references .debug_loc. See the symtab->locations_valid field.
409 This test is imperfect as there may exist optimized debug code not using
410 any location list and still facing inlining issues if handled as
411 unoptimized code. For a future better test see GCC PR other/32998. */
412
413 unsigned int has_loclist : 1;
414 };
415
416 /* Persistent data held for a compilation unit, even when not
417 processing it. We put a pointer to this structure in the
418 read_symtab_private field of the psymtab. If we encounter
419 inter-compilation-unit references, we also maintain a sorted
420 list of all compilation units. */
421
422 struct dwarf2_per_cu_data
423 {
424 /* The start offset and length of this compilation unit. 2**29-1
425 bytes should suffice to store the length of any compilation unit
426 - if it doesn't, GDB will fall over anyway.
427 NOTE: Unlike comp_unit_head.length, this length includes
428 initial_length_size. */
429 unsigned int offset;
430 unsigned int length : 29;
431
432 /* Flag indicating this compilation unit will be read in before
433 any of the current compilation units are processed. */
434 unsigned int queued : 1;
435
436 /* This flag will be set if we need to load absolutely all DIEs
437 for this compilation unit, instead of just the ones we think
438 are interesting. It gets set if we look for a DIE in the
439 hash table and don't find it. */
440 unsigned int load_all_dies : 1;
441
442 /* Non-zero if this CU is from .debug_types.
443 Otherwise it's from .debug_info. */
444 unsigned int from_debug_types : 1;
445
446 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
447 of the CU cache it gets reset to NULL again. */
448 struct dwarf2_cu *cu;
449
450 /* The corresponding objfile. */
451 struct objfile *objfile;
452
453 /* When using partial symbol tables, the 'psymtab' field is active.
454 Otherwise the 'quick' field is active. */
455 union
456 {
457 /* The partial symbol table associated with this compilation unit,
458 or NULL for partial units (which do not have an associated
459 symtab). */
460 struct partial_symtab *psymtab;
461
462 /* Data needed by the "quick" functions. */
463 struct dwarf2_per_cu_quick_data *quick;
464 } v;
465 };
466
467 /* Entry in the signatured_types hash table. */
468
469 struct signatured_type
470 {
471 ULONGEST signature;
472
473 /* Offset in .debug_types of the type defined by this TU. */
474 unsigned int type_offset;
475
476 /* The CU(/TU) of this type. */
477 struct dwarf2_per_cu_data per_cu;
478 };
479
480 /* Struct used to pass misc. parameters to read_die_and_children, et
481 al. which are used for both .debug_info and .debug_types dies.
482 All parameters here are unchanging for the life of the call. This
483 struct exists to abstract away the constant parameters of die
484 reading. */
485
486 struct die_reader_specs
487 {
488 /* The bfd of this objfile. */
489 bfd* abfd;
490
491 /* The CU of the DIE we are parsing. */
492 struct dwarf2_cu *cu;
493
494 /* Pointer to start of section buffer.
495 This is either the start of .debug_info or .debug_types. */
496 const gdb_byte *buffer;
497 };
498
499 /* The line number information for a compilation unit (found in the
500 .debug_line section) begins with a "statement program header",
501 which contains the following information. */
502 struct line_header
503 {
504 unsigned int total_length;
505 unsigned short version;
506 unsigned int header_length;
507 unsigned char minimum_instruction_length;
508 unsigned char maximum_ops_per_instruction;
509 unsigned char default_is_stmt;
510 int line_base;
511 unsigned char line_range;
512 unsigned char opcode_base;
513
514 /* standard_opcode_lengths[i] is the number of operands for the
515 standard opcode whose value is i. This means that
516 standard_opcode_lengths[0] is unused, and the last meaningful
517 element is standard_opcode_lengths[opcode_base - 1]. */
518 unsigned char *standard_opcode_lengths;
519
520 /* The include_directories table. NOTE! These strings are not
521 allocated with xmalloc; instead, they are pointers into
522 debug_line_buffer. If you try to free them, `free' will get
523 indigestion. */
524 unsigned int num_include_dirs, include_dirs_size;
525 char **include_dirs;
526
527 /* The file_names table. NOTE! These strings are not allocated
528 with xmalloc; instead, they are pointers into debug_line_buffer.
529 Don't try to free them directly. */
530 unsigned int num_file_names, file_names_size;
531 struct file_entry
532 {
533 char *name;
534 unsigned int dir_index;
535 unsigned int mod_time;
536 unsigned int length;
537 int included_p; /* Non-zero if referenced by the Line Number Program. */
538 struct symtab *symtab; /* The associated symbol table, if any. */
539 } *file_names;
540
541 /* The start and end of the statement program following this
542 header. These point into dwarf2_per_objfile->line_buffer. */
543 gdb_byte *statement_program_start, *statement_program_end;
544 };
545
546 /* When we construct a partial symbol table entry we only
547 need this much information. */
548 struct partial_die_info
549 {
550 /* Offset of this DIE. */
551 unsigned int offset;
552
553 /* DWARF-2 tag for this DIE. */
554 ENUM_BITFIELD(dwarf_tag) tag : 16;
555
556 /* Assorted flags describing the data found in this DIE. */
557 unsigned int has_children : 1;
558 unsigned int is_external : 1;
559 unsigned int is_declaration : 1;
560 unsigned int has_type : 1;
561 unsigned int has_specification : 1;
562 unsigned int has_pc_info : 1;
563
564 /* Flag set if the SCOPE field of this structure has been
565 computed. */
566 unsigned int scope_set : 1;
567
568 /* Flag set if the DIE has a byte_size attribute. */
569 unsigned int has_byte_size : 1;
570
571 /* Flag set if any of the DIE's children are template arguments. */
572 unsigned int has_template_arguments : 1;
573
574 /* Flag set if fixup_partial_die has been called on this die. */
575 unsigned int fixup_called : 1;
576
577 /* The name of this DIE. Normally the value of DW_AT_name, but
578 sometimes a default name for unnamed DIEs. */
579 char *name;
580
581 /* The linkage name, if present. */
582 const char *linkage_name;
583
584 /* The scope to prepend to our children. This is generally
585 allocated on the comp_unit_obstack, so will disappear
586 when this compilation unit leaves the cache. */
587 char *scope;
588
589 /* The location description associated with this DIE, if any. */
590 struct dwarf_block *locdesc;
591
592 /* If HAS_PC_INFO, the PC range associated with this DIE. */
593 CORE_ADDR lowpc;
594 CORE_ADDR highpc;
595
596 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
597 DW_AT_sibling, if any. */
598 /* NOTE: This member isn't strictly necessary, read_partial_die could
599 return DW_AT_sibling values to its caller load_partial_dies. */
600 gdb_byte *sibling;
601
602 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
603 DW_AT_specification (or DW_AT_abstract_origin or
604 DW_AT_extension). */
605 unsigned int spec_offset;
606
607 /* Pointers to this DIE's parent, first child, and next sibling,
608 if any. */
609 struct partial_die_info *die_parent, *die_child, *die_sibling;
610 };
611
612 /* This data structure holds the information of an abbrev. */
613 struct abbrev_info
614 {
615 unsigned int number; /* number identifying abbrev */
616 enum dwarf_tag tag; /* dwarf tag */
617 unsigned short has_children; /* boolean */
618 unsigned short num_attrs; /* number of attributes */
619 struct attr_abbrev *attrs; /* an array of attribute descriptions */
620 struct abbrev_info *next; /* next in chain */
621 };
622
623 struct attr_abbrev
624 {
625 ENUM_BITFIELD(dwarf_attribute) name : 16;
626 ENUM_BITFIELD(dwarf_form) form : 16;
627 };
628
629 /* Attributes have a name and a value. */
630 struct attribute
631 {
632 ENUM_BITFIELD(dwarf_attribute) name : 16;
633 ENUM_BITFIELD(dwarf_form) form : 15;
634
635 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
636 field should be in u.str (existing only for DW_STRING) but it is kept
637 here for better struct attribute alignment. */
638 unsigned int string_is_canonical : 1;
639
640 union
641 {
642 char *str;
643 struct dwarf_block *blk;
644 ULONGEST unsnd;
645 LONGEST snd;
646 CORE_ADDR addr;
647 struct signatured_type *signatured_type;
648 }
649 u;
650 };
651
652 /* This data structure holds a complete die structure. */
653 struct die_info
654 {
655 /* DWARF-2 tag for this DIE. */
656 ENUM_BITFIELD(dwarf_tag) tag : 16;
657
658 /* Number of attributes */
659 unsigned char num_attrs;
660
661 /* True if we're presently building the full type name for the
662 type derived from this DIE. */
663 unsigned char building_fullname : 1;
664
665 /* Abbrev number */
666 unsigned int abbrev;
667
668 /* Offset in .debug_info or .debug_types section. */
669 unsigned int offset;
670
671 /* The dies in a compilation unit form an n-ary tree. PARENT
672 points to this die's parent; CHILD points to the first child of
673 this node; and all the children of a given node are chained
674 together via their SIBLING fields. */
675 struct die_info *child; /* Its first child, if any. */
676 struct die_info *sibling; /* Its next sibling, if any. */
677 struct die_info *parent; /* Its parent, if any. */
678
679 /* An array of attributes, with NUM_ATTRS elements. There may be
680 zero, but it's not common and zero-sized arrays are not
681 sufficiently portable C. */
682 struct attribute attrs[1];
683 };
684
685 struct function_range
686 {
687 const char *name;
688 CORE_ADDR lowpc, highpc;
689 int seen_line;
690 struct function_range *next;
691 };
692
693 /* Get at parts of an attribute structure. */
694
695 #define DW_STRING(attr) ((attr)->u.str)
696 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
697 #define DW_UNSND(attr) ((attr)->u.unsnd)
698 #define DW_BLOCK(attr) ((attr)->u.blk)
699 #define DW_SND(attr) ((attr)->u.snd)
700 #define DW_ADDR(attr) ((attr)->u.addr)
701 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
702
703 /* Blocks are a bunch of untyped bytes. */
704 struct dwarf_block
705 {
706 unsigned int size;
707 gdb_byte *data;
708 };
709
710 #ifndef ATTR_ALLOC_CHUNK
711 #define ATTR_ALLOC_CHUNK 4
712 #endif
713
714 /* Allocate fields for structs, unions and enums in this size. */
715 #ifndef DW_FIELD_ALLOC_CHUNK
716 #define DW_FIELD_ALLOC_CHUNK 4
717 #endif
718
719 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
720 but this would require a corresponding change in unpack_field_as_long
721 and friends. */
722 static int bits_per_byte = 8;
723
724 /* The routines that read and process dies for a C struct or C++ class
725 pass lists of data member fields and lists of member function fields
726 in an instance of a field_info structure, as defined below. */
727 struct field_info
728 {
729 /* List of data member and baseclasses fields. */
730 struct nextfield
731 {
732 struct nextfield *next;
733 int accessibility;
734 int virtuality;
735 struct field field;
736 }
737 *fields, *baseclasses;
738
739 /* Number of fields (including baseclasses). */
740 int nfields;
741
742 /* Number of baseclasses. */
743 int nbaseclasses;
744
745 /* Set if the accesibility of one of the fields is not public. */
746 int non_public_fields;
747
748 /* Member function fields array, entries are allocated in the order they
749 are encountered in the object file. */
750 struct nextfnfield
751 {
752 struct nextfnfield *next;
753 struct fn_field fnfield;
754 }
755 *fnfields;
756
757 /* Member function fieldlist array, contains name of possibly overloaded
758 member function, number of overloaded member functions and a pointer
759 to the head of the member function field chain. */
760 struct fnfieldlist
761 {
762 char *name;
763 int length;
764 struct nextfnfield *head;
765 }
766 *fnfieldlists;
767
768 /* Number of entries in the fnfieldlists array. */
769 int nfnfields;
770
771 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
772 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
773 struct typedef_field_list
774 {
775 struct typedef_field field;
776 struct typedef_field_list *next;
777 }
778 *typedef_field_list;
779 unsigned typedef_field_list_count;
780 };
781
782 /* One item on the queue of compilation units to read in full symbols
783 for. */
784 struct dwarf2_queue_item
785 {
786 struct dwarf2_per_cu_data *per_cu;
787 struct dwarf2_queue_item *next;
788 };
789
790 /* The current queue. */
791 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
792
793 /* Loaded secondary compilation units are kept in memory until they
794 have not been referenced for the processing of this many
795 compilation units. Set this to zero to disable caching. Cache
796 sizes of up to at least twenty will improve startup time for
797 typical inter-CU-reference binaries, at an obvious memory cost. */
798 static int dwarf2_max_cache_age = 5;
799 static void
800 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
801 struct cmd_list_element *c, const char *value)
802 {
803 fprintf_filtered (file, _("The upper bound on the age of cached "
804 "dwarf2 compilation units is %s.\n"),
805 value);
806 }
807
808
809 /* Various complaints about symbol reading that don't abort the process. */
810
811 static void
812 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
813 {
814 complaint (&symfile_complaints,
815 _("statement list doesn't fit in .debug_line section"));
816 }
817
818 static void
819 dwarf2_debug_line_missing_file_complaint (void)
820 {
821 complaint (&symfile_complaints,
822 _(".debug_line section has line data without a file"));
823 }
824
825 static void
826 dwarf2_debug_line_missing_end_sequence_complaint (void)
827 {
828 complaint (&symfile_complaints,
829 _(".debug_line section has line "
830 "program sequence without an end"));
831 }
832
833 static void
834 dwarf2_complex_location_expr_complaint (void)
835 {
836 complaint (&symfile_complaints, _("location expression too complex"));
837 }
838
839 static void
840 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
841 int arg3)
842 {
843 complaint (&symfile_complaints,
844 _("const value length mismatch for '%s', got %d, expected %d"),
845 arg1, arg2, arg3);
846 }
847
848 static void
849 dwarf2_macros_too_long_complaint (void)
850 {
851 complaint (&symfile_complaints,
852 _("macro info runs off end of `.debug_macinfo' section"));
853 }
854
855 static void
856 dwarf2_macro_malformed_definition_complaint (const char *arg1)
857 {
858 complaint (&symfile_complaints,
859 _("macro debug info contains a "
860 "malformed macro definition:\n`%s'"),
861 arg1);
862 }
863
864 static void
865 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
866 {
867 complaint (&symfile_complaints,
868 _("invalid attribute class or form for '%s' in '%s'"),
869 arg1, arg2);
870 }
871
872 /* local function prototypes */
873
874 static void dwarf2_locate_sections (bfd *, asection *, void *);
875
876 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
877 struct objfile *);
878
879 static void dwarf2_build_psymtabs_hard (struct objfile *);
880
881 static void scan_partial_symbols (struct partial_die_info *,
882 CORE_ADDR *, CORE_ADDR *,
883 int, struct dwarf2_cu *);
884
885 static void add_partial_symbol (struct partial_die_info *,
886 struct dwarf2_cu *);
887
888 static void add_partial_namespace (struct partial_die_info *pdi,
889 CORE_ADDR *lowpc, CORE_ADDR *highpc,
890 int need_pc, struct dwarf2_cu *cu);
891
892 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
893 CORE_ADDR *highpc, int need_pc,
894 struct dwarf2_cu *cu);
895
896 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
897 struct dwarf2_cu *cu);
898
899 static void add_partial_subprogram (struct partial_die_info *pdi,
900 CORE_ADDR *lowpc, CORE_ADDR *highpc,
901 int need_pc, struct dwarf2_cu *cu);
902
903 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
904 gdb_byte *buffer, gdb_byte *info_ptr,
905 bfd *abfd, struct dwarf2_cu *cu);
906
907 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
908
909 static void psymtab_to_symtab_1 (struct partial_symtab *);
910
911 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
912
913 static void dwarf2_free_abbrev_table (void *);
914
915 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
916 struct dwarf2_cu *);
917
918 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
919 struct dwarf2_cu *);
920
921 static struct partial_die_info *load_partial_dies (bfd *,
922 gdb_byte *, gdb_byte *,
923 int, struct dwarf2_cu *);
924
925 static gdb_byte *read_partial_die (struct partial_die_info *,
926 struct abbrev_info *abbrev,
927 unsigned int, bfd *,
928 gdb_byte *, gdb_byte *,
929 struct dwarf2_cu *);
930
931 static struct partial_die_info *find_partial_die (unsigned int,
932 struct dwarf2_cu *);
933
934 static void fixup_partial_die (struct partial_die_info *,
935 struct dwarf2_cu *);
936
937 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
938 bfd *, gdb_byte *, struct dwarf2_cu *);
939
940 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
941 bfd *, gdb_byte *, struct dwarf2_cu *);
942
943 static unsigned int read_1_byte (bfd *, gdb_byte *);
944
945 static int read_1_signed_byte (bfd *, gdb_byte *);
946
947 static unsigned int read_2_bytes (bfd *, gdb_byte *);
948
949 static unsigned int read_4_bytes (bfd *, gdb_byte *);
950
951 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
952
953 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
954 unsigned int *);
955
956 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
957
958 static LONGEST read_checked_initial_length_and_offset
959 (bfd *, gdb_byte *, const struct comp_unit_head *,
960 unsigned int *, unsigned int *);
961
962 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
963 unsigned int *);
964
965 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
966
967 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
968
969 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
970
971 static char *read_indirect_string (bfd *, gdb_byte *,
972 const struct comp_unit_head *,
973 unsigned int *);
974
975 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
976
977 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
978
979 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
980
981 static void set_cu_language (unsigned int, struct dwarf2_cu *);
982
983 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
984 struct dwarf2_cu *);
985
986 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
987 unsigned int,
988 struct dwarf2_cu *);
989
990 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
991 struct dwarf2_cu *cu);
992
993 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
994
995 static struct die_info *die_specification (struct die_info *die,
996 struct dwarf2_cu **);
997
998 static void free_line_header (struct line_header *lh);
999
1000 static void add_file_name (struct line_header *, char *, unsigned int,
1001 unsigned int, unsigned int);
1002
1003 static struct line_header *(dwarf_decode_line_header
1004 (unsigned int offset,
1005 bfd *abfd, struct dwarf2_cu *cu));
1006
1007 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1008 struct dwarf2_cu *, struct partial_symtab *);
1009
1010 static void dwarf2_start_subfile (char *, const char *, const char *);
1011
1012 static struct symbol *new_symbol (struct die_info *, struct type *,
1013 struct dwarf2_cu *);
1014
1015 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1016 struct dwarf2_cu *, struct symbol *);
1017
1018 static void dwarf2_const_value (struct attribute *, struct symbol *,
1019 struct dwarf2_cu *);
1020
1021 static void dwarf2_const_value_attr (struct attribute *attr,
1022 struct type *type,
1023 const char *name,
1024 struct obstack *obstack,
1025 struct dwarf2_cu *cu, long *value,
1026 gdb_byte **bytes,
1027 struct dwarf2_locexpr_baton **baton);
1028
1029 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1030
1031 static int need_gnat_info (struct dwarf2_cu *);
1032
1033 static struct type *die_descriptive_type (struct die_info *,
1034 struct dwarf2_cu *);
1035
1036 static void set_descriptive_type (struct type *, struct die_info *,
1037 struct dwarf2_cu *);
1038
1039 static struct type *die_containing_type (struct die_info *,
1040 struct dwarf2_cu *);
1041
1042 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1043 struct dwarf2_cu *);
1044
1045 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1046
1047 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1048
1049 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1050
1051 static char *typename_concat (struct obstack *obs, const char *prefix,
1052 const char *suffix, int physname,
1053 struct dwarf2_cu *cu);
1054
1055 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1056
1057 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1058
1059 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1060
1061 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1062
1063 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1064 struct dwarf2_cu *, struct partial_symtab *);
1065
1066 static int dwarf2_get_pc_bounds (struct die_info *,
1067 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1068 struct partial_symtab *);
1069
1070 static void get_scope_pc_bounds (struct die_info *,
1071 CORE_ADDR *, CORE_ADDR *,
1072 struct dwarf2_cu *);
1073
1074 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1075 CORE_ADDR, struct dwarf2_cu *);
1076
1077 static void dwarf2_add_field (struct field_info *, struct die_info *,
1078 struct dwarf2_cu *);
1079
1080 static void dwarf2_attach_fields_to_type (struct field_info *,
1081 struct type *, struct dwarf2_cu *);
1082
1083 static void dwarf2_add_member_fn (struct field_info *,
1084 struct die_info *, struct type *,
1085 struct dwarf2_cu *);
1086
1087 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1088 struct type *,
1089 struct dwarf2_cu *);
1090
1091 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1092
1093 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1094
1095 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1096
1097 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1098
1099 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1100
1101 static struct type *read_module_type (struct die_info *die,
1102 struct dwarf2_cu *cu);
1103
1104 static const char *namespace_name (struct die_info *die,
1105 int *is_anonymous, struct dwarf2_cu *);
1106
1107 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1108
1109 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1110
1111 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1112 struct dwarf2_cu *);
1113
1114 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1115
1116 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1117 gdb_byte *info_ptr,
1118 gdb_byte **new_info_ptr,
1119 struct die_info *parent);
1120
1121 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1122 gdb_byte *info_ptr,
1123 gdb_byte **new_info_ptr,
1124 struct die_info *parent);
1125
1126 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1127 gdb_byte *info_ptr,
1128 gdb_byte **new_info_ptr,
1129 struct die_info *parent);
1130
1131 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1132 struct die_info **, gdb_byte *,
1133 int *);
1134
1135 static void process_die (struct die_info *, struct dwarf2_cu *);
1136
1137 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1138 struct obstack *);
1139
1140 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1141
1142 static const char *dwarf2_full_name (char *name,
1143 struct die_info *die,
1144 struct dwarf2_cu *cu);
1145
1146 static struct die_info *dwarf2_extension (struct die_info *die,
1147 struct dwarf2_cu **);
1148
1149 static char *dwarf_tag_name (unsigned int);
1150
1151 static char *dwarf_attr_name (unsigned int);
1152
1153 static char *dwarf_form_name (unsigned int);
1154
1155 static char *dwarf_bool_name (unsigned int);
1156
1157 static char *dwarf_type_encoding_name (unsigned int);
1158
1159 #if 0
1160 static char *dwarf_cfi_name (unsigned int);
1161 #endif
1162
1163 static struct die_info *sibling_die (struct die_info *);
1164
1165 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1166
1167 static void dump_die_for_error (struct die_info *);
1168
1169 static void dump_die_1 (struct ui_file *, int level, int max_level,
1170 struct die_info *);
1171
1172 /*static*/ void dump_die (struct die_info *, int max_level);
1173
1174 static void store_in_ref_table (struct die_info *,
1175 struct dwarf2_cu *);
1176
1177 static int is_ref_attr (struct attribute *);
1178
1179 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1180
1181 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1182
1183 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1184 struct attribute *,
1185 struct dwarf2_cu **);
1186
1187 static struct die_info *follow_die_ref (struct die_info *,
1188 struct attribute *,
1189 struct dwarf2_cu **);
1190
1191 static struct die_info *follow_die_sig (struct die_info *,
1192 struct attribute *,
1193 struct dwarf2_cu **);
1194
1195 static void read_signatured_type_at_offset (struct objfile *objfile,
1196 unsigned int offset);
1197
1198 static void read_signatured_type (struct objfile *,
1199 struct signatured_type *type_sig);
1200
1201 /* memory allocation interface */
1202
1203 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1204
1205 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1206
1207 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1208
1209 static void initialize_cu_func_list (struct dwarf2_cu *);
1210
1211 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1212 struct dwarf2_cu *);
1213
1214 static void dwarf_decode_macros (struct line_header *, unsigned int,
1215 char *, bfd *, struct dwarf2_cu *);
1216
1217 static int attr_form_is_block (struct attribute *);
1218
1219 static int attr_form_is_section_offset (struct attribute *);
1220
1221 static int attr_form_is_constant (struct attribute *);
1222
1223 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1224 struct dwarf2_loclist_baton *baton,
1225 struct attribute *attr);
1226
1227 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1228 struct symbol *sym,
1229 struct dwarf2_cu *cu);
1230
1231 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1232 struct abbrev_info *abbrev,
1233 struct dwarf2_cu *cu);
1234
1235 static void free_stack_comp_unit (void *);
1236
1237 static hashval_t partial_die_hash (const void *item);
1238
1239 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1240
1241 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1242 (unsigned int offset, struct objfile *objfile);
1243
1244 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1245 (unsigned int offset, struct objfile *objfile);
1246
1247 static void init_one_comp_unit (struct dwarf2_cu *cu,
1248 struct objfile *objfile);
1249
1250 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1251 struct die_info *comp_unit_die);
1252
1253 static void free_one_comp_unit (void *);
1254
1255 static void free_cached_comp_units (void *);
1256
1257 static void age_cached_comp_units (void);
1258
1259 static void free_one_cached_comp_unit (void *);
1260
1261 static struct type *set_die_type (struct die_info *, struct type *,
1262 struct dwarf2_cu *);
1263
1264 static void create_all_comp_units (struct objfile *);
1265
1266 static int create_debug_types_hash_table (struct objfile *objfile);
1267
1268 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1269 struct objfile *);
1270
1271 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1272
1273 static void dwarf2_add_dependence (struct dwarf2_cu *,
1274 struct dwarf2_per_cu_data *);
1275
1276 static void dwarf2_mark (struct dwarf2_cu *);
1277
1278 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1279
1280 static struct type *get_die_type_at_offset (unsigned int,
1281 struct dwarf2_per_cu_data *per_cu);
1282
1283 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1284
1285 static void dwarf2_release_queue (void *dummy);
1286
1287 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1288 struct objfile *objfile);
1289
1290 static void process_queue (struct objfile *objfile);
1291
1292 static void find_file_and_directory (struct die_info *die,
1293 struct dwarf2_cu *cu,
1294 char **name, char **comp_dir);
1295
1296 static char *file_full_name (int file, struct line_header *lh,
1297 const char *comp_dir);
1298
1299 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1300 gdb_byte *info_ptr,
1301 gdb_byte *buffer,
1302 unsigned int buffer_size,
1303 bfd *abfd);
1304
1305 static void init_cu_die_reader (struct die_reader_specs *reader,
1306 struct dwarf2_cu *cu);
1307
1308 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1309
1310 #if WORDS_BIGENDIAN
1311
1312 /* Convert VALUE between big- and little-endian. */
1313 static offset_type
1314 byte_swap (offset_type value)
1315 {
1316 offset_type result;
1317
1318 result = (value & 0xff) << 24;
1319 result |= (value & 0xff00) << 8;
1320 result |= (value & 0xff0000) >> 8;
1321 result |= (value & 0xff000000) >> 24;
1322 return result;
1323 }
1324
1325 #define MAYBE_SWAP(V) byte_swap (V)
1326
1327 #else
1328 #define MAYBE_SWAP(V) (V)
1329 #endif /* WORDS_BIGENDIAN */
1330
1331 /* The suffix for an index file. */
1332 #define INDEX_SUFFIX ".gdb-index"
1333
1334 static const char *dwarf2_physname (char *name, struct die_info *die,
1335 struct dwarf2_cu *cu);
1336
1337 /* Try to locate the sections we need for DWARF 2 debugging
1338 information and return true if we have enough to do something. */
1339
1340 int
1341 dwarf2_has_info (struct objfile *objfile)
1342 {
1343 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1344 if (!dwarf2_per_objfile)
1345 {
1346 /* Initialize per-objfile state. */
1347 struct dwarf2_per_objfile *data
1348 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1349
1350 memset (data, 0, sizeof (*data));
1351 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1352 dwarf2_per_objfile = data;
1353
1354 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1355 dwarf2_per_objfile->objfile = objfile;
1356 }
1357 return (dwarf2_per_objfile->info.asection != NULL
1358 && dwarf2_per_objfile->abbrev.asection != NULL);
1359 }
1360
1361 /* When loading sections, we can either look for ".<name>", or for
1362 * ".z<name>", which indicates a compressed section. */
1363
1364 static int
1365 section_is_p (const char *section_name, const char *name)
1366 {
1367 return (section_name[0] == '.'
1368 && (strcmp (section_name + 1, name) == 0
1369 || (section_name[1] == 'z'
1370 && strcmp (section_name + 2, name) == 0)));
1371 }
1372
1373 /* This function is mapped across the sections and remembers the
1374 offset and size of each of the debugging sections we are interested
1375 in. */
1376
1377 static void
1378 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1379 {
1380 if (section_is_p (sectp->name, INFO_SECTION))
1381 {
1382 dwarf2_per_objfile->info.asection = sectp;
1383 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1384 }
1385 else if (section_is_p (sectp->name, ABBREV_SECTION))
1386 {
1387 dwarf2_per_objfile->abbrev.asection = sectp;
1388 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1389 }
1390 else if (section_is_p (sectp->name, LINE_SECTION))
1391 {
1392 dwarf2_per_objfile->line.asection = sectp;
1393 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1394 }
1395 else if (section_is_p (sectp->name, LOC_SECTION))
1396 {
1397 dwarf2_per_objfile->loc.asection = sectp;
1398 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1399 }
1400 else if (section_is_p (sectp->name, MACINFO_SECTION))
1401 {
1402 dwarf2_per_objfile->macinfo.asection = sectp;
1403 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1404 }
1405 else if (section_is_p (sectp->name, STR_SECTION))
1406 {
1407 dwarf2_per_objfile->str.asection = sectp;
1408 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1409 }
1410 else if (section_is_p (sectp->name, FRAME_SECTION))
1411 {
1412 dwarf2_per_objfile->frame.asection = sectp;
1413 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1414 }
1415 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1416 {
1417 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1418
1419 if (aflag & SEC_HAS_CONTENTS)
1420 {
1421 dwarf2_per_objfile->eh_frame.asection = sectp;
1422 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1423 }
1424 }
1425 else if (section_is_p (sectp->name, RANGES_SECTION))
1426 {
1427 dwarf2_per_objfile->ranges.asection = sectp;
1428 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1429 }
1430 else if (section_is_p (sectp->name, TYPES_SECTION))
1431 {
1432 dwarf2_per_objfile->types.asection = sectp;
1433 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1434 }
1435 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1436 {
1437 dwarf2_per_objfile->gdb_index.asection = sectp;
1438 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1439 }
1440
1441 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1442 && bfd_section_vma (abfd, sectp) == 0)
1443 dwarf2_per_objfile->has_section_at_zero = 1;
1444 }
1445
1446 /* Decompress a section that was compressed using zlib. Store the
1447 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1448
1449 static void
1450 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1451 gdb_byte **outbuf, bfd_size_type *outsize)
1452 {
1453 bfd *abfd = objfile->obfd;
1454 #ifndef HAVE_ZLIB_H
1455 error (_("Support for zlib-compressed DWARF data (from '%s') "
1456 "is disabled in this copy of GDB"),
1457 bfd_get_filename (abfd));
1458 #else
1459 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1460 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1461 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1462 bfd_size_type uncompressed_size;
1463 gdb_byte *uncompressed_buffer;
1464 z_stream strm;
1465 int rc;
1466 int header_size = 12;
1467
1468 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1469 || bfd_bread (compressed_buffer,
1470 compressed_size, abfd) != compressed_size)
1471 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1472 bfd_get_filename (abfd));
1473
1474 /* Read the zlib header. In this case, it should be "ZLIB" followed
1475 by the uncompressed section size, 8 bytes in big-endian order. */
1476 if (compressed_size < header_size
1477 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1478 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1479 bfd_get_filename (abfd));
1480 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1481 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1482 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1483 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1484 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1485 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[11];
1488
1489 /* It is possible the section consists of several compressed
1490 buffers concatenated together, so we uncompress in a loop. */
1491 strm.zalloc = NULL;
1492 strm.zfree = NULL;
1493 strm.opaque = NULL;
1494 strm.avail_in = compressed_size - header_size;
1495 strm.next_in = (Bytef*) compressed_buffer + header_size;
1496 strm.avail_out = uncompressed_size;
1497 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1498 uncompressed_size);
1499 rc = inflateInit (&strm);
1500 while (strm.avail_in > 0)
1501 {
1502 if (rc != Z_OK)
1503 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1504 bfd_get_filename (abfd), rc);
1505 strm.next_out = ((Bytef*) uncompressed_buffer
1506 + (uncompressed_size - strm.avail_out));
1507 rc = inflate (&strm, Z_FINISH);
1508 if (rc != Z_STREAM_END)
1509 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1510 bfd_get_filename (abfd), rc);
1511 rc = inflateReset (&strm);
1512 }
1513 rc = inflateEnd (&strm);
1514 if (rc != Z_OK
1515 || strm.avail_out != 0)
1516 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1517 bfd_get_filename (abfd), rc);
1518
1519 do_cleanups (cleanup);
1520 *outbuf = uncompressed_buffer;
1521 *outsize = uncompressed_size;
1522 #endif
1523 }
1524
1525 /* A helper function that decides whether a section is empty. */
1526
1527 static int
1528 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1529 {
1530 return info->asection == NULL || info->size == 0;
1531 }
1532
1533 /* Read the contents of the section SECTP from object file specified by
1534 OBJFILE, store info about the section into INFO.
1535 If the section is compressed, uncompress it before returning. */
1536
1537 static void
1538 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1539 {
1540 bfd *abfd = objfile->obfd;
1541 asection *sectp = info->asection;
1542 gdb_byte *buf, *retbuf;
1543 unsigned char header[4];
1544
1545 if (info->readin)
1546 return;
1547 info->buffer = NULL;
1548 info->was_mmapped = 0;
1549 info->readin = 1;
1550
1551 if (dwarf2_section_empty_p (info))
1552 return;
1553
1554 /* Check if the file has a 4-byte header indicating compression. */
1555 if (info->size > sizeof (header)
1556 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1557 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1558 {
1559 /* Upon decompression, update the buffer and its size. */
1560 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1561 {
1562 zlib_decompress_section (objfile, sectp, &info->buffer,
1563 &info->size);
1564 return;
1565 }
1566 }
1567
1568 #ifdef HAVE_MMAP
1569 if (pagesize == 0)
1570 pagesize = getpagesize ();
1571
1572 /* Only try to mmap sections which are large enough: we don't want to
1573 waste space due to fragmentation. Also, only try mmap for sections
1574 without relocations. */
1575
1576 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1577 {
1578 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1579 size_t map_length = info->size + sectp->filepos - pg_offset;
1580 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1581 MAP_PRIVATE, pg_offset);
1582
1583 if (retbuf != MAP_FAILED)
1584 {
1585 info->was_mmapped = 1;
1586 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1587 #if HAVE_POSIX_MADVISE
1588 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1589 #endif
1590 return;
1591 }
1592 }
1593 #endif
1594
1595 /* If we get here, we are a normal, not-compressed section. */
1596 info->buffer = buf
1597 = obstack_alloc (&objfile->objfile_obstack, info->size);
1598
1599 /* When debugging .o files, we may need to apply relocations; see
1600 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1601 We never compress sections in .o files, so we only need to
1602 try this when the section is not compressed. */
1603 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1604 if (retbuf != NULL)
1605 {
1606 info->buffer = retbuf;
1607 return;
1608 }
1609
1610 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1611 || bfd_bread (buf, info->size, abfd) != info->size)
1612 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1613 bfd_get_filename (abfd));
1614 }
1615
1616 /* A helper function that returns the size of a section in a safe way.
1617 If you are positive that the section has been read before using the
1618 size, then it is safe to refer to the dwarf2_section_info object's
1619 "size" field directly. In other cases, you must call this
1620 function, because for compressed sections the size field is not set
1621 correctly until the section has been read. */
1622
1623 static bfd_size_type
1624 dwarf2_section_size (struct objfile *objfile,
1625 struct dwarf2_section_info *info)
1626 {
1627 if (!info->readin)
1628 dwarf2_read_section (objfile, info);
1629 return info->size;
1630 }
1631
1632 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1633 SECTION_NAME. */
1634
1635 void
1636 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1637 asection **sectp, gdb_byte **bufp,
1638 bfd_size_type *sizep)
1639 {
1640 struct dwarf2_per_objfile *data
1641 = objfile_data (objfile, dwarf2_objfile_data_key);
1642 struct dwarf2_section_info *info;
1643
1644 /* We may see an objfile without any DWARF, in which case we just
1645 return nothing. */
1646 if (data == NULL)
1647 {
1648 *sectp = NULL;
1649 *bufp = NULL;
1650 *sizep = 0;
1651 return;
1652 }
1653 if (section_is_p (section_name, EH_FRAME_SECTION))
1654 info = &data->eh_frame;
1655 else if (section_is_p (section_name, FRAME_SECTION))
1656 info = &data->frame;
1657 else
1658 gdb_assert_not_reached ("unexpected section");
1659
1660 dwarf2_read_section (objfile, info);
1661
1662 *sectp = info->asection;
1663 *bufp = info->buffer;
1664 *sizep = info->size;
1665 }
1666
1667 \f
1668 /* DWARF quick_symbols_functions support. */
1669
1670 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1671 unique line tables, so we maintain a separate table of all .debug_line
1672 derived entries to support the sharing.
1673 All the quick functions need is the list of file names. We discard the
1674 line_header when we're done and don't need to record it here. */
1675 struct quick_file_names
1676 {
1677 /* The offset in .debug_line of the line table. We hash on this. */
1678 unsigned int offset;
1679
1680 /* The number of entries in file_names, real_names. */
1681 unsigned int num_file_names;
1682
1683 /* The file names from the line table, after being run through
1684 file_full_name. */
1685 const char **file_names;
1686
1687 /* The file names from the line table after being run through
1688 gdb_realpath. These are computed lazily. */
1689 const char **real_names;
1690 };
1691
1692 /* When using the index (and thus not using psymtabs), each CU has an
1693 object of this type. This is used to hold information needed by
1694 the various "quick" methods. */
1695 struct dwarf2_per_cu_quick_data
1696 {
1697 /* The file table. This can be NULL if there was no file table
1698 or it's currently not read in.
1699 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1700 struct quick_file_names *file_names;
1701
1702 /* The corresponding symbol table. This is NULL if symbols for this
1703 CU have not yet been read. */
1704 struct symtab *symtab;
1705
1706 /* A temporary mark bit used when iterating over all CUs in
1707 expand_symtabs_matching. */
1708 unsigned int mark : 1;
1709
1710 /* True if we've tried to read the file table and found there isn't one.
1711 There will be no point in trying to read it again next time. */
1712 unsigned int no_file_data : 1;
1713 };
1714
1715 /* Hash function for a quick_file_names. */
1716
1717 static hashval_t
1718 hash_file_name_entry (const void *e)
1719 {
1720 const struct quick_file_names *file_data = e;
1721
1722 return file_data->offset;
1723 }
1724
1725 /* Equality function for a quick_file_names. */
1726
1727 static int
1728 eq_file_name_entry (const void *a, const void *b)
1729 {
1730 const struct quick_file_names *ea = a;
1731 const struct quick_file_names *eb = b;
1732
1733 return ea->offset == eb->offset;
1734 }
1735
1736 /* Delete function for a quick_file_names. */
1737
1738 static void
1739 delete_file_name_entry (void *e)
1740 {
1741 struct quick_file_names *file_data = e;
1742 int i;
1743
1744 for (i = 0; i < file_data->num_file_names; ++i)
1745 {
1746 xfree ((void*) file_data->file_names[i]);
1747 if (file_data->real_names)
1748 xfree ((void*) file_data->real_names[i]);
1749 }
1750
1751 /* The space for the struct itself lives on objfile_obstack,
1752 so we don't free it here. */
1753 }
1754
1755 /* Create a quick_file_names hash table. */
1756
1757 static htab_t
1758 create_quick_file_names_table (unsigned int nr_initial_entries)
1759 {
1760 return htab_create_alloc (nr_initial_entries,
1761 hash_file_name_entry, eq_file_name_entry,
1762 delete_file_name_entry, xcalloc, xfree);
1763 }
1764
1765 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1766 this CU came. */
1767
1768 static void
1769 dw2_do_instantiate_symtab (struct objfile *objfile,
1770 struct dwarf2_per_cu_data *per_cu)
1771 {
1772 struct cleanup *back_to;
1773
1774 back_to = make_cleanup (dwarf2_release_queue, NULL);
1775
1776 queue_comp_unit (per_cu, objfile);
1777
1778 if (per_cu->from_debug_types)
1779 read_signatured_type_at_offset (objfile, per_cu->offset);
1780 else
1781 load_full_comp_unit (per_cu, objfile);
1782
1783 process_queue (objfile);
1784
1785 /* Age the cache, releasing compilation units that have not
1786 been used recently. */
1787 age_cached_comp_units ();
1788
1789 do_cleanups (back_to);
1790 }
1791
1792 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1793 the objfile from which this CU came. Returns the resulting symbol
1794 table. */
1795
1796 static struct symtab *
1797 dw2_instantiate_symtab (struct objfile *objfile,
1798 struct dwarf2_per_cu_data *per_cu)
1799 {
1800 if (!per_cu->v.quick->symtab)
1801 {
1802 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1803 increment_reading_symtab ();
1804 dw2_do_instantiate_symtab (objfile, per_cu);
1805 do_cleanups (back_to);
1806 }
1807 return per_cu->v.quick->symtab;
1808 }
1809
1810 /* Return the CU given its index. */
1811
1812 static struct dwarf2_per_cu_data *
1813 dw2_get_cu (int index)
1814 {
1815 if (index >= dwarf2_per_objfile->n_comp_units)
1816 {
1817 index -= dwarf2_per_objfile->n_comp_units;
1818 return dwarf2_per_objfile->type_comp_units[index];
1819 }
1820 return dwarf2_per_objfile->all_comp_units[index];
1821 }
1822
1823 /* A helper function that knows how to read a 64-bit value in a way
1824 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1825 otherwise. */
1826
1827 static int
1828 extract_cu_value (const char *bytes, ULONGEST *result)
1829 {
1830 if (sizeof (ULONGEST) < 8)
1831 {
1832 int i;
1833
1834 /* Ignore the upper 4 bytes if they are all zero. */
1835 for (i = 0; i < 4; ++i)
1836 if (bytes[i + 4] != 0)
1837 return 0;
1838
1839 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1840 }
1841 else
1842 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1843 return 1;
1844 }
1845
1846 /* Read the CU list from the mapped index, and use it to create all
1847 the CU objects for this objfile. Return 0 if something went wrong,
1848 1 if everything went ok. */
1849
1850 static int
1851 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1852 offset_type cu_list_elements)
1853 {
1854 offset_type i;
1855
1856 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1857 dwarf2_per_objfile->all_comp_units
1858 = obstack_alloc (&objfile->objfile_obstack,
1859 dwarf2_per_objfile->n_comp_units
1860 * sizeof (struct dwarf2_per_cu_data *));
1861
1862 for (i = 0; i < cu_list_elements; i += 2)
1863 {
1864 struct dwarf2_per_cu_data *the_cu;
1865 ULONGEST offset, length;
1866
1867 if (!extract_cu_value (cu_list, &offset)
1868 || !extract_cu_value (cu_list + 8, &length))
1869 return 0;
1870 cu_list += 2 * 8;
1871
1872 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1873 struct dwarf2_per_cu_data);
1874 the_cu->offset = offset;
1875 the_cu->length = length;
1876 the_cu->objfile = objfile;
1877 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1878 struct dwarf2_per_cu_quick_data);
1879 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1880 }
1881
1882 return 1;
1883 }
1884
1885 /* Create the signatured type hash table from the index. */
1886
1887 static int
1888 create_signatured_type_table_from_index (struct objfile *objfile,
1889 const gdb_byte *bytes,
1890 offset_type elements)
1891 {
1892 offset_type i;
1893 htab_t sig_types_hash;
1894
1895 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1896 dwarf2_per_objfile->type_comp_units
1897 = obstack_alloc (&objfile->objfile_obstack,
1898 dwarf2_per_objfile->n_type_comp_units
1899 * sizeof (struct dwarf2_per_cu_data *));
1900
1901 sig_types_hash = allocate_signatured_type_table (objfile);
1902
1903 for (i = 0; i < elements; i += 3)
1904 {
1905 struct signatured_type *type_sig;
1906 ULONGEST offset, type_offset, signature;
1907 void **slot;
1908
1909 if (!extract_cu_value (bytes, &offset)
1910 || !extract_cu_value (bytes + 8, &type_offset))
1911 return 0;
1912 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1913 bytes += 3 * 8;
1914
1915 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1916 struct signatured_type);
1917 type_sig->signature = signature;
1918 type_sig->type_offset = type_offset;
1919 type_sig->per_cu.from_debug_types = 1;
1920 type_sig->per_cu.offset = offset;
1921 type_sig->per_cu.objfile = objfile;
1922 type_sig->per_cu.v.quick
1923 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1924 struct dwarf2_per_cu_quick_data);
1925
1926 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1927 *slot = type_sig;
1928
1929 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1930 }
1931
1932 dwarf2_per_objfile->signatured_types = sig_types_hash;
1933
1934 return 1;
1935 }
1936
1937 /* Read the address map data from the mapped index, and use it to
1938 populate the objfile's psymtabs_addrmap. */
1939
1940 static void
1941 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1942 {
1943 const gdb_byte *iter, *end;
1944 struct obstack temp_obstack;
1945 struct addrmap *mutable_map;
1946 struct cleanup *cleanup;
1947 CORE_ADDR baseaddr;
1948
1949 obstack_init (&temp_obstack);
1950 cleanup = make_cleanup_obstack_free (&temp_obstack);
1951 mutable_map = addrmap_create_mutable (&temp_obstack);
1952
1953 iter = index->address_table;
1954 end = iter + index->address_table_size;
1955
1956 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1957
1958 while (iter < end)
1959 {
1960 ULONGEST hi, lo, cu_index;
1961 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1962 iter += 8;
1963 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1964 iter += 8;
1965 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1966 iter += 4;
1967
1968 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1969 dw2_get_cu (cu_index));
1970 }
1971
1972 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1973 &objfile->objfile_obstack);
1974 do_cleanups (cleanup);
1975 }
1976
1977 /* The hash function for strings in the mapped index. This is the same as
1978 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1979 implementation. This is necessary because the hash function is tied to the
1980 format of the mapped index file. The hash values do not have to match with
1981 SYMBOL_HASH_NEXT.
1982
1983 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
1984
1985 static hashval_t
1986 mapped_index_string_hash (int index_version, const void *p)
1987 {
1988 const unsigned char *str = (const unsigned char *) p;
1989 hashval_t r = 0;
1990 unsigned char c;
1991
1992 while ((c = *str++) != 0)
1993 {
1994 if (index_version >= 5)
1995 c = tolower (c);
1996 r = r * 67 + c - 113;
1997 }
1998
1999 return r;
2000 }
2001
2002 /* Find a slot in the mapped index INDEX for the object named NAME.
2003 If NAME is found, set *VEC_OUT to point to the CU vector in the
2004 constant pool and return 1. If NAME cannot be found, return 0. */
2005
2006 static int
2007 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2008 offset_type **vec_out)
2009 {
2010 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2011 offset_type hash;
2012 offset_type slot, step;
2013 int (*cmp) (const char *, const char *);
2014
2015 if (current_language->la_language == language_cplus
2016 || current_language->la_language == language_java
2017 || current_language->la_language == language_fortran)
2018 {
2019 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2020 not contain any. */
2021 const char *paren = strchr (name, '(');
2022
2023 if (paren)
2024 {
2025 char *dup;
2026
2027 dup = xmalloc (paren - name + 1);
2028 memcpy (dup, name, paren - name);
2029 dup[paren - name] = 0;
2030
2031 make_cleanup (xfree, dup);
2032 name = dup;
2033 }
2034 }
2035
2036 /* Index version 4 did not support case insensitive searches. But the
2037 indexes for case insensitive languages are built in lowercase, therefore
2038 simulate our NAME being searched is also lowercased. */
2039 hash = mapped_index_string_hash ((index->version == 4
2040 && case_sensitivity == case_sensitive_off
2041 ? 5 : index->version),
2042 name);
2043
2044 slot = hash & (index->symbol_table_slots - 1);
2045 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2046 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2047
2048 for (;;)
2049 {
2050 /* Convert a slot number to an offset into the table. */
2051 offset_type i = 2 * slot;
2052 const char *str;
2053 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2054 {
2055 do_cleanups (back_to);
2056 return 0;
2057 }
2058
2059 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2060 if (!cmp (name, str))
2061 {
2062 *vec_out = (offset_type *) (index->constant_pool
2063 + MAYBE_SWAP (index->symbol_table[i + 1]));
2064 do_cleanups (back_to);
2065 return 1;
2066 }
2067
2068 slot = (slot + step) & (index->symbol_table_slots - 1);
2069 }
2070 }
2071
2072 /* Read the index file. If everything went ok, initialize the "quick"
2073 elements of all the CUs and return 1. Otherwise, return 0. */
2074
2075 static int
2076 dwarf2_read_index (struct objfile *objfile)
2077 {
2078 char *addr;
2079 struct mapped_index *map;
2080 offset_type *metadata;
2081 const gdb_byte *cu_list;
2082 const gdb_byte *types_list = NULL;
2083 offset_type version, cu_list_elements;
2084 offset_type types_list_elements = 0;
2085 int i;
2086
2087 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2088 return 0;
2089
2090 /* Older elfutils strip versions could keep the section in the main
2091 executable while splitting it for the separate debug info file. */
2092 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2093 & SEC_HAS_CONTENTS) == 0)
2094 return 0;
2095
2096 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2097
2098 addr = dwarf2_per_objfile->gdb_index.buffer;
2099 /* Version check. */
2100 version = MAYBE_SWAP (*(offset_type *) addr);
2101 /* Versions earlier than 3 emitted every copy of a psymbol. This
2102 causes the index to behave very poorly for certain requests. Version 3
2103 contained incomplete addrmap. So, it seems better to just ignore such
2104 indices. Index version 4 uses a different hash function than index
2105 version 5 and later. */
2106 if (version < 4)
2107 return 0;
2108 /* Indexes with higher version than the one supported by GDB may be no
2109 longer backward compatible. */
2110 if (version > 5)
2111 return 0;
2112
2113 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2114 map->version = version;
2115 map->total_size = dwarf2_per_objfile->gdb_index.size;
2116
2117 metadata = (offset_type *) (addr + sizeof (offset_type));
2118
2119 i = 0;
2120 cu_list = addr + MAYBE_SWAP (metadata[i]);
2121 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2122 / 8);
2123 ++i;
2124
2125 types_list = addr + MAYBE_SWAP (metadata[i]);
2126 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2127 - MAYBE_SWAP (metadata[i]))
2128 / 8);
2129 ++i;
2130
2131 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2132 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2133 - MAYBE_SWAP (metadata[i]));
2134 ++i;
2135
2136 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2137 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2138 - MAYBE_SWAP (metadata[i]))
2139 / (2 * sizeof (offset_type)));
2140 ++i;
2141
2142 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2143
2144 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2145 return 0;
2146
2147 if (types_list_elements
2148 && !create_signatured_type_table_from_index (objfile, types_list,
2149 types_list_elements))
2150 return 0;
2151
2152 create_addrmap_from_index (objfile, map);
2153
2154 dwarf2_per_objfile->index_table = map;
2155 dwarf2_per_objfile->using_index = 1;
2156 dwarf2_per_objfile->quick_file_names_table =
2157 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2158
2159 return 1;
2160 }
2161
2162 /* A helper for the "quick" functions which sets the global
2163 dwarf2_per_objfile according to OBJFILE. */
2164
2165 static void
2166 dw2_setup (struct objfile *objfile)
2167 {
2168 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2169 gdb_assert (dwarf2_per_objfile);
2170 }
2171
2172 /* A helper for the "quick" functions which attempts to read the line
2173 table for THIS_CU. */
2174
2175 static struct quick_file_names *
2176 dw2_get_file_names (struct objfile *objfile,
2177 struct dwarf2_per_cu_data *this_cu)
2178 {
2179 bfd *abfd = objfile->obfd;
2180 struct line_header *lh;
2181 struct attribute *attr;
2182 struct cleanup *cleanups;
2183 struct die_info *comp_unit_die;
2184 struct dwarf2_section_info* sec;
2185 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2186 int has_children, i;
2187 struct dwarf2_cu cu;
2188 unsigned int bytes_read, buffer_size;
2189 struct die_reader_specs reader_specs;
2190 char *name, *comp_dir;
2191 void **slot;
2192 struct quick_file_names *qfn;
2193 unsigned int line_offset;
2194
2195 if (this_cu->v.quick->file_names != NULL)
2196 return this_cu->v.quick->file_names;
2197 /* If we know there is no line data, no point in looking again. */
2198 if (this_cu->v.quick->no_file_data)
2199 return NULL;
2200
2201 init_one_comp_unit (&cu, objfile);
2202 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2203
2204 if (this_cu->from_debug_types)
2205 sec = &dwarf2_per_objfile->types;
2206 else
2207 sec = &dwarf2_per_objfile->info;
2208 dwarf2_read_section (objfile, sec);
2209 buffer_size = sec->size;
2210 buffer = sec->buffer;
2211 info_ptr = buffer + this_cu->offset;
2212 beg_of_comp_unit = info_ptr;
2213
2214 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2215 buffer, buffer_size,
2216 abfd);
2217
2218 /* Complete the cu_header. */
2219 cu.header.offset = beg_of_comp_unit - buffer;
2220 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2221
2222 this_cu->cu = &cu;
2223 cu.per_cu = this_cu;
2224
2225 dwarf2_read_abbrevs (abfd, &cu);
2226 make_cleanup (dwarf2_free_abbrev_table, &cu);
2227
2228 if (this_cu->from_debug_types)
2229 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2230 init_cu_die_reader (&reader_specs, &cu);
2231 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2232 &has_children);
2233
2234 lh = NULL;
2235 slot = NULL;
2236 line_offset = 0;
2237 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2238 if (attr)
2239 {
2240 struct quick_file_names find_entry;
2241
2242 line_offset = DW_UNSND (attr);
2243
2244 /* We may have already read in this line header (TU line header sharing).
2245 If we have we're done. */
2246 find_entry.offset = line_offset;
2247 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2248 &find_entry, INSERT);
2249 if (*slot != NULL)
2250 {
2251 do_cleanups (cleanups);
2252 this_cu->v.quick->file_names = *slot;
2253 return *slot;
2254 }
2255
2256 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2257 }
2258 if (lh == NULL)
2259 {
2260 do_cleanups (cleanups);
2261 this_cu->v.quick->no_file_data = 1;
2262 return NULL;
2263 }
2264
2265 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2266 qfn->offset = line_offset;
2267 gdb_assert (slot != NULL);
2268 *slot = qfn;
2269
2270 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2271
2272 qfn->num_file_names = lh->num_file_names;
2273 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2274 lh->num_file_names * sizeof (char *));
2275 for (i = 0; i < lh->num_file_names; ++i)
2276 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2277 qfn->real_names = NULL;
2278
2279 free_line_header (lh);
2280 do_cleanups (cleanups);
2281
2282 this_cu->v.quick->file_names = qfn;
2283 return qfn;
2284 }
2285
2286 /* A helper for the "quick" functions which computes and caches the
2287 real path for a given file name from the line table. */
2288
2289 static const char *
2290 dw2_get_real_path (struct objfile *objfile,
2291 struct quick_file_names *qfn, int index)
2292 {
2293 if (qfn->real_names == NULL)
2294 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2295 qfn->num_file_names, sizeof (char *));
2296
2297 if (qfn->real_names[index] == NULL)
2298 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2299
2300 return qfn->real_names[index];
2301 }
2302
2303 static struct symtab *
2304 dw2_find_last_source_symtab (struct objfile *objfile)
2305 {
2306 int index;
2307
2308 dw2_setup (objfile);
2309 index = dwarf2_per_objfile->n_comp_units - 1;
2310 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2311 }
2312
2313 /* Traversal function for dw2_forget_cached_source_info. */
2314
2315 static int
2316 dw2_free_cached_file_names (void **slot, void *info)
2317 {
2318 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2319
2320 if (file_data->real_names)
2321 {
2322 int i;
2323
2324 for (i = 0; i < file_data->num_file_names; ++i)
2325 {
2326 xfree ((void*) file_data->real_names[i]);
2327 file_data->real_names[i] = NULL;
2328 }
2329 }
2330
2331 return 1;
2332 }
2333
2334 static void
2335 dw2_forget_cached_source_info (struct objfile *objfile)
2336 {
2337 dw2_setup (objfile);
2338
2339 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2340 dw2_free_cached_file_names, NULL);
2341 }
2342
2343 static int
2344 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2345 const char *full_path, const char *real_path,
2346 struct symtab **result)
2347 {
2348 int i;
2349 int check_basename = lbasename (name) == name;
2350 struct dwarf2_per_cu_data *base_cu = NULL;
2351
2352 dw2_setup (objfile);
2353
2354 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2355 + dwarf2_per_objfile->n_type_comp_units); ++i)
2356 {
2357 int j;
2358 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2359 struct quick_file_names *file_data;
2360
2361 if (per_cu->v.quick->symtab)
2362 continue;
2363
2364 file_data = dw2_get_file_names (objfile, per_cu);
2365 if (file_data == NULL)
2366 continue;
2367
2368 for (j = 0; j < file_data->num_file_names; ++j)
2369 {
2370 const char *this_name = file_data->file_names[j];
2371
2372 if (FILENAME_CMP (name, this_name) == 0)
2373 {
2374 *result = dw2_instantiate_symtab (objfile, per_cu);
2375 return 1;
2376 }
2377
2378 if (check_basename && ! base_cu
2379 && FILENAME_CMP (lbasename (this_name), name) == 0)
2380 base_cu = per_cu;
2381
2382 if (full_path != NULL)
2383 {
2384 const char *this_real_name = dw2_get_real_path (objfile,
2385 file_data, j);
2386
2387 if (this_real_name != NULL
2388 && FILENAME_CMP (full_path, this_real_name) == 0)
2389 {
2390 *result = dw2_instantiate_symtab (objfile, per_cu);
2391 return 1;
2392 }
2393 }
2394
2395 if (real_path != NULL)
2396 {
2397 const char *this_real_name = dw2_get_real_path (objfile,
2398 file_data, j);
2399
2400 if (this_real_name != NULL
2401 && FILENAME_CMP (real_path, this_real_name) == 0)
2402 {
2403 *result = dw2_instantiate_symtab (objfile, per_cu);
2404 return 1;
2405 }
2406 }
2407 }
2408 }
2409
2410 if (base_cu)
2411 {
2412 *result = dw2_instantiate_symtab (objfile, base_cu);
2413 return 1;
2414 }
2415
2416 return 0;
2417 }
2418
2419 static struct symtab *
2420 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2421 const char *name, domain_enum domain)
2422 {
2423 /* We do all the work in the pre_expand_symtabs_matching hook
2424 instead. */
2425 return NULL;
2426 }
2427
2428 /* A helper function that expands all symtabs that hold an object
2429 named NAME. */
2430
2431 static void
2432 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2433 {
2434 dw2_setup (objfile);
2435
2436 /* index_table is NULL if OBJF_READNOW. */
2437 if (dwarf2_per_objfile->index_table)
2438 {
2439 offset_type *vec;
2440
2441 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2442 name, &vec))
2443 {
2444 offset_type i, len = MAYBE_SWAP (*vec);
2445 for (i = 0; i < len; ++i)
2446 {
2447 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2448 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2449
2450 dw2_instantiate_symtab (objfile, per_cu);
2451 }
2452 }
2453 }
2454 }
2455
2456 static void
2457 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2458 enum block_enum block_kind, const char *name,
2459 domain_enum domain)
2460 {
2461 dw2_do_expand_symtabs_matching (objfile, name);
2462 }
2463
2464 static void
2465 dw2_print_stats (struct objfile *objfile)
2466 {
2467 int i, count;
2468
2469 dw2_setup (objfile);
2470 count = 0;
2471 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2472 + dwarf2_per_objfile->n_type_comp_units); ++i)
2473 {
2474 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2475
2476 if (!per_cu->v.quick->symtab)
2477 ++count;
2478 }
2479 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2480 }
2481
2482 static void
2483 dw2_dump (struct objfile *objfile)
2484 {
2485 /* Nothing worth printing. */
2486 }
2487
2488 static void
2489 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2490 struct section_offsets *delta)
2491 {
2492 /* There's nothing to relocate here. */
2493 }
2494
2495 static void
2496 dw2_expand_symtabs_for_function (struct objfile *objfile,
2497 const char *func_name)
2498 {
2499 dw2_do_expand_symtabs_matching (objfile, func_name);
2500 }
2501
2502 static void
2503 dw2_expand_all_symtabs (struct objfile *objfile)
2504 {
2505 int i;
2506
2507 dw2_setup (objfile);
2508
2509 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2510 + dwarf2_per_objfile->n_type_comp_units); ++i)
2511 {
2512 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2513
2514 dw2_instantiate_symtab (objfile, per_cu);
2515 }
2516 }
2517
2518 static void
2519 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2520 const char *filename)
2521 {
2522 int i;
2523
2524 dw2_setup (objfile);
2525
2526 /* We don't need to consider type units here.
2527 This is only called for examining code, e.g. expand_line_sal.
2528 There can be an order of magnitude (or more) more type units
2529 than comp units, and we avoid them if we can. */
2530
2531 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2532 {
2533 int j;
2534 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2535 struct quick_file_names *file_data;
2536
2537 if (per_cu->v.quick->symtab)
2538 continue;
2539
2540 file_data = dw2_get_file_names (objfile, per_cu);
2541 if (file_data == NULL)
2542 continue;
2543
2544 for (j = 0; j < file_data->num_file_names; ++j)
2545 {
2546 const char *this_name = file_data->file_names[j];
2547 if (FILENAME_CMP (this_name, filename) == 0)
2548 {
2549 dw2_instantiate_symtab (objfile, per_cu);
2550 break;
2551 }
2552 }
2553 }
2554 }
2555
2556 static const char *
2557 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2558 {
2559 struct dwarf2_per_cu_data *per_cu;
2560 offset_type *vec;
2561 struct quick_file_names *file_data;
2562
2563 dw2_setup (objfile);
2564
2565 /* index_table is NULL if OBJF_READNOW. */
2566 if (!dwarf2_per_objfile->index_table)
2567 return NULL;
2568
2569 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2570 name, &vec))
2571 return NULL;
2572
2573 /* Note that this just looks at the very first one named NAME -- but
2574 actually we are looking for a function. find_main_filename
2575 should be rewritten so that it doesn't require a custom hook. It
2576 could just use the ordinary symbol tables. */
2577 /* vec[0] is the length, which must always be >0. */
2578 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2579
2580 file_data = dw2_get_file_names (objfile, per_cu);
2581 if (file_data == NULL)
2582 return NULL;
2583
2584 return file_data->file_names[file_data->num_file_names - 1];
2585 }
2586
2587 static void
2588 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2589 struct objfile *objfile, int global,
2590 int (*callback) (struct block *,
2591 struct symbol *, void *),
2592 void *data, symbol_compare_ftype *match,
2593 symbol_compare_ftype *ordered_compare)
2594 {
2595 /* Currently unimplemented; used for Ada. The function can be called if the
2596 current language is Ada for a non-Ada objfile using GNU index. As Ada
2597 does not look for non-Ada symbols this function should just return. */
2598 }
2599
2600 static void
2601 dw2_expand_symtabs_matching (struct objfile *objfile,
2602 int (*file_matcher) (const char *, void *),
2603 int (*name_matcher) (const char *, void *),
2604 enum search_domain kind,
2605 void *data)
2606 {
2607 int i;
2608 offset_type iter;
2609 struct mapped_index *index;
2610
2611 dw2_setup (objfile);
2612
2613 /* index_table is NULL if OBJF_READNOW. */
2614 if (!dwarf2_per_objfile->index_table)
2615 return;
2616 index = dwarf2_per_objfile->index_table;
2617
2618 if (file_matcher != NULL)
2619 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2620 + dwarf2_per_objfile->n_type_comp_units); ++i)
2621 {
2622 int j;
2623 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2624 struct quick_file_names *file_data;
2625
2626 per_cu->v.quick->mark = 0;
2627 if (per_cu->v.quick->symtab)
2628 continue;
2629
2630 file_data = dw2_get_file_names (objfile, per_cu);
2631 if (file_data == NULL)
2632 continue;
2633
2634 for (j = 0; j < file_data->num_file_names; ++j)
2635 {
2636 if (file_matcher (file_data->file_names[j], data))
2637 {
2638 per_cu->v.quick->mark = 1;
2639 break;
2640 }
2641 }
2642 }
2643
2644 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2645 {
2646 offset_type idx = 2 * iter;
2647 const char *name;
2648 offset_type *vec, vec_len, vec_idx;
2649
2650 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2651 continue;
2652
2653 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2654
2655 if (! (*name_matcher) (name, data))
2656 continue;
2657
2658 /* The name was matched, now expand corresponding CUs that were
2659 marked. */
2660 vec = (offset_type *) (index->constant_pool
2661 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2662 vec_len = MAYBE_SWAP (vec[0]);
2663 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2664 {
2665 struct dwarf2_per_cu_data *per_cu;
2666
2667 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2668 if (file_matcher == NULL || per_cu->v.quick->mark)
2669 dw2_instantiate_symtab (objfile, per_cu);
2670 }
2671 }
2672 }
2673
2674 static struct symtab *
2675 dw2_find_pc_sect_symtab (struct objfile *objfile,
2676 struct minimal_symbol *msymbol,
2677 CORE_ADDR pc,
2678 struct obj_section *section,
2679 int warn_if_readin)
2680 {
2681 struct dwarf2_per_cu_data *data;
2682
2683 dw2_setup (objfile);
2684
2685 if (!objfile->psymtabs_addrmap)
2686 return NULL;
2687
2688 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2689 if (!data)
2690 return NULL;
2691
2692 if (warn_if_readin && data->v.quick->symtab)
2693 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2694 paddress (get_objfile_arch (objfile), pc));
2695
2696 return dw2_instantiate_symtab (objfile, data);
2697 }
2698
2699 static void
2700 dw2_map_symbol_filenames (struct objfile *objfile,
2701 void (*fun) (const char *, const char *, void *),
2702 void *data)
2703 {
2704 int i;
2705
2706 dw2_setup (objfile);
2707
2708 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2709 + dwarf2_per_objfile->n_type_comp_units); ++i)
2710 {
2711 int j;
2712 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2713 struct quick_file_names *file_data;
2714
2715 if (per_cu->v.quick->symtab)
2716 continue;
2717
2718 file_data = dw2_get_file_names (objfile, per_cu);
2719 if (file_data == NULL)
2720 continue;
2721
2722 for (j = 0; j < file_data->num_file_names; ++j)
2723 {
2724 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2725 j);
2726 (*fun) (file_data->file_names[j], this_real_name, data);
2727 }
2728 }
2729 }
2730
2731 static int
2732 dw2_has_symbols (struct objfile *objfile)
2733 {
2734 return 1;
2735 }
2736
2737 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2738 {
2739 dw2_has_symbols,
2740 dw2_find_last_source_symtab,
2741 dw2_forget_cached_source_info,
2742 dw2_lookup_symtab,
2743 dw2_lookup_symbol,
2744 dw2_pre_expand_symtabs_matching,
2745 dw2_print_stats,
2746 dw2_dump,
2747 dw2_relocate,
2748 dw2_expand_symtabs_for_function,
2749 dw2_expand_all_symtabs,
2750 dw2_expand_symtabs_with_filename,
2751 dw2_find_symbol_file,
2752 dw2_map_matching_symbols,
2753 dw2_expand_symtabs_matching,
2754 dw2_find_pc_sect_symtab,
2755 dw2_map_symbol_filenames
2756 };
2757
2758 /* Initialize for reading DWARF for this objfile. Return 0 if this
2759 file will use psymtabs, or 1 if using the GNU index. */
2760
2761 int
2762 dwarf2_initialize_objfile (struct objfile *objfile)
2763 {
2764 /* If we're about to read full symbols, don't bother with the
2765 indices. In this case we also don't care if some other debug
2766 format is making psymtabs, because they are all about to be
2767 expanded anyway. */
2768 if ((objfile->flags & OBJF_READNOW))
2769 {
2770 int i;
2771
2772 dwarf2_per_objfile->using_index = 1;
2773 create_all_comp_units (objfile);
2774 create_debug_types_hash_table (objfile);
2775 dwarf2_per_objfile->quick_file_names_table =
2776 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2777
2778 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2779 + dwarf2_per_objfile->n_type_comp_units); ++i)
2780 {
2781 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2782
2783 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2784 struct dwarf2_per_cu_quick_data);
2785 }
2786
2787 /* Return 1 so that gdb sees the "quick" functions. However,
2788 these functions will be no-ops because we will have expanded
2789 all symtabs. */
2790 return 1;
2791 }
2792
2793 if (dwarf2_read_index (objfile))
2794 return 1;
2795
2796 return 0;
2797 }
2798
2799 \f
2800
2801 /* Build a partial symbol table. */
2802
2803 void
2804 dwarf2_build_psymtabs (struct objfile *objfile)
2805 {
2806 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2807 {
2808 init_psymbol_list (objfile, 1024);
2809 }
2810
2811 dwarf2_build_psymtabs_hard (objfile);
2812 }
2813
2814 /* Return TRUE if OFFSET is within CU_HEADER. */
2815
2816 static inline int
2817 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2818 {
2819 unsigned int bottom = cu_header->offset;
2820 unsigned int top = (cu_header->offset
2821 + cu_header->length
2822 + cu_header->initial_length_size);
2823
2824 return (offset >= bottom && offset < top);
2825 }
2826
2827 /* Read in the comp unit header information from the debug_info at info_ptr.
2828 NOTE: This leaves members offset, first_die_offset to be filled in
2829 by the caller. */
2830
2831 static gdb_byte *
2832 read_comp_unit_head (struct comp_unit_head *cu_header,
2833 gdb_byte *info_ptr, bfd *abfd)
2834 {
2835 int signed_addr;
2836 unsigned int bytes_read;
2837
2838 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2839 cu_header->initial_length_size = bytes_read;
2840 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2841 info_ptr += bytes_read;
2842 cu_header->version = read_2_bytes (abfd, info_ptr);
2843 info_ptr += 2;
2844 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2845 &bytes_read);
2846 info_ptr += bytes_read;
2847 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2848 info_ptr += 1;
2849 signed_addr = bfd_get_sign_extend_vma (abfd);
2850 if (signed_addr < 0)
2851 internal_error (__FILE__, __LINE__,
2852 _("read_comp_unit_head: dwarf from non elf file"));
2853 cu_header->signed_addr_p = signed_addr;
2854
2855 return info_ptr;
2856 }
2857
2858 static gdb_byte *
2859 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2860 gdb_byte *buffer, unsigned int buffer_size,
2861 bfd *abfd)
2862 {
2863 gdb_byte *beg_of_comp_unit = info_ptr;
2864
2865 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2866
2867 if (header->version != 2 && header->version != 3 && header->version != 4)
2868 error (_("Dwarf Error: wrong version in compilation unit header "
2869 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2870 bfd_get_filename (abfd));
2871
2872 if (header->abbrev_offset
2873 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2874 &dwarf2_per_objfile->abbrev))
2875 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2876 "(offset 0x%lx + 6) [in module %s]"),
2877 (long) header->abbrev_offset,
2878 (long) (beg_of_comp_unit - buffer),
2879 bfd_get_filename (abfd));
2880
2881 if (beg_of_comp_unit + header->length + header->initial_length_size
2882 > buffer + buffer_size)
2883 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2884 "(offset 0x%lx + 0) [in module %s]"),
2885 (long) header->length,
2886 (long) (beg_of_comp_unit - buffer),
2887 bfd_get_filename (abfd));
2888
2889 return info_ptr;
2890 }
2891
2892 /* Read in the types comp unit header information from .debug_types entry at
2893 types_ptr. The result is a pointer to one past the end of the header. */
2894
2895 static gdb_byte *
2896 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2897 ULONGEST *signature,
2898 gdb_byte *types_ptr, bfd *abfd)
2899 {
2900 gdb_byte *initial_types_ptr = types_ptr;
2901
2902 dwarf2_read_section (dwarf2_per_objfile->objfile,
2903 &dwarf2_per_objfile->types);
2904 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2905
2906 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2907
2908 *signature = read_8_bytes (abfd, types_ptr);
2909 types_ptr += 8;
2910 types_ptr += cu_header->offset_size;
2911 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2912
2913 return types_ptr;
2914 }
2915
2916 /* Allocate a new partial symtab for file named NAME and mark this new
2917 partial symtab as being an include of PST. */
2918
2919 static void
2920 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2921 struct objfile *objfile)
2922 {
2923 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2924
2925 subpst->section_offsets = pst->section_offsets;
2926 subpst->textlow = 0;
2927 subpst->texthigh = 0;
2928
2929 subpst->dependencies = (struct partial_symtab **)
2930 obstack_alloc (&objfile->objfile_obstack,
2931 sizeof (struct partial_symtab *));
2932 subpst->dependencies[0] = pst;
2933 subpst->number_of_dependencies = 1;
2934
2935 subpst->globals_offset = 0;
2936 subpst->n_global_syms = 0;
2937 subpst->statics_offset = 0;
2938 subpst->n_static_syms = 0;
2939 subpst->symtab = NULL;
2940 subpst->read_symtab = pst->read_symtab;
2941 subpst->readin = 0;
2942
2943 /* No private part is necessary for include psymtabs. This property
2944 can be used to differentiate between such include psymtabs and
2945 the regular ones. */
2946 subpst->read_symtab_private = NULL;
2947 }
2948
2949 /* Read the Line Number Program data and extract the list of files
2950 included by the source file represented by PST. Build an include
2951 partial symtab for each of these included files. */
2952
2953 static void
2954 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2955 struct die_info *die,
2956 struct partial_symtab *pst)
2957 {
2958 struct objfile *objfile = cu->objfile;
2959 bfd *abfd = objfile->obfd;
2960 struct line_header *lh = NULL;
2961 struct attribute *attr;
2962
2963 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2964 if (attr)
2965 {
2966 unsigned int line_offset = DW_UNSND (attr);
2967
2968 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2969 }
2970 if (lh == NULL)
2971 return; /* No linetable, so no includes. */
2972
2973 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2974 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2975
2976 free_line_header (lh);
2977 }
2978
2979 static hashval_t
2980 hash_type_signature (const void *item)
2981 {
2982 const struct signatured_type *type_sig = item;
2983
2984 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2985 return type_sig->signature;
2986 }
2987
2988 static int
2989 eq_type_signature (const void *item_lhs, const void *item_rhs)
2990 {
2991 const struct signatured_type *lhs = item_lhs;
2992 const struct signatured_type *rhs = item_rhs;
2993
2994 return lhs->signature == rhs->signature;
2995 }
2996
2997 /* Allocate a hash table for signatured types. */
2998
2999 static htab_t
3000 allocate_signatured_type_table (struct objfile *objfile)
3001 {
3002 return htab_create_alloc_ex (41,
3003 hash_type_signature,
3004 eq_type_signature,
3005 NULL,
3006 &objfile->objfile_obstack,
3007 hashtab_obstack_allocate,
3008 dummy_obstack_deallocate);
3009 }
3010
3011 /* A helper function to add a signatured type CU to a list. */
3012
3013 static int
3014 add_signatured_type_cu_to_list (void **slot, void *datum)
3015 {
3016 struct signatured_type *sigt = *slot;
3017 struct dwarf2_per_cu_data ***datap = datum;
3018
3019 **datap = &sigt->per_cu;
3020 ++*datap;
3021
3022 return 1;
3023 }
3024
3025 /* Create the hash table of all entries in the .debug_types section.
3026 The result is zero if there is an error (e.g. missing .debug_types section),
3027 otherwise non-zero. */
3028
3029 static int
3030 create_debug_types_hash_table (struct objfile *objfile)
3031 {
3032 gdb_byte *info_ptr;
3033 htab_t types_htab;
3034 struct dwarf2_per_cu_data **iter;
3035
3036 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3037 info_ptr = dwarf2_per_objfile->types.buffer;
3038
3039 if (info_ptr == NULL)
3040 {
3041 dwarf2_per_objfile->signatured_types = NULL;
3042 return 0;
3043 }
3044
3045 types_htab = allocate_signatured_type_table (objfile);
3046
3047 if (dwarf2_die_debug)
3048 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3049
3050 while (info_ptr < dwarf2_per_objfile->types.buffer
3051 + dwarf2_per_objfile->types.size)
3052 {
3053 unsigned int offset;
3054 unsigned int offset_size;
3055 unsigned int type_offset;
3056 unsigned int length, initial_length_size;
3057 unsigned short version;
3058 ULONGEST signature;
3059 struct signatured_type *type_sig;
3060 void **slot;
3061 gdb_byte *ptr = info_ptr;
3062
3063 offset = ptr - dwarf2_per_objfile->types.buffer;
3064
3065 /* We need to read the type's signature in order to build the hash
3066 table, but we don't need to read anything else just yet. */
3067
3068 /* Sanity check to ensure entire cu is present. */
3069 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3070 if (ptr + length + initial_length_size
3071 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3072 {
3073 complaint (&symfile_complaints,
3074 _("debug type entry runs off end "
3075 "of `.debug_types' section, ignored"));
3076 break;
3077 }
3078
3079 offset_size = initial_length_size == 4 ? 4 : 8;
3080 ptr += initial_length_size;
3081 version = bfd_get_16 (objfile->obfd, ptr);
3082 ptr += 2;
3083 ptr += offset_size; /* abbrev offset */
3084 ptr += 1; /* address size */
3085 signature = bfd_get_64 (objfile->obfd, ptr);
3086 ptr += 8;
3087 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3088
3089 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3090 memset (type_sig, 0, sizeof (*type_sig));
3091 type_sig->signature = signature;
3092 type_sig->type_offset = type_offset;
3093 type_sig->per_cu.objfile = objfile;
3094 type_sig->per_cu.from_debug_types = 1;
3095 type_sig->per_cu.offset = offset;
3096
3097 slot = htab_find_slot (types_htab, type_sig, INSERT);
3098 gdb_assert (slot != NULL);
3099 if (*slot != NULL)
3100 {
3101 const struct signatured_type *dup_sig = *slot;
3102
3103 complaint (&symfile_complaints,
3104 _("debug type entry at offset 0x%x is duplicate to the "
3105 "entry at offset 0x%x, signature 0x%s"),
3106 offset, dup_sig->per_cu.offset,
3107 phex (signature, sizeof (signature)));
3108 gdb_assert (signature == dup_sig->signature);
3109 }
3110 *slot = type_sig;
3111
3112 if (dwarf2_die_debug)
3113 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3114 offset, phex (signature, sizeof (signature)));
3115
3116 info_ptr = info_ptr + initial_length_size + length;
3117 }
3118
3119 dwarf2_per_objfile->signatured_types = types_htab;
3120
3121 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3122 dwarf2_per_objfile->type_comp_units
3123 = obstack_alloc (&objfile->objfile_obstack,
3124 dwarf2_per_objfile->n_type_comp_units
3125 * sizeof (struct dwarf2_per_cu_data *));
3126 iter = &dwarf2_per_objfile->type_comp_units[0];
3127 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3128 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3129 == dwarf2_per_objfile->n_type_comp_units);
3130
3131 return 1;
3132 }
3133
3134 /* Lookup a signature based type.
3135 Returns NULL if SIG is not present in the table. */
3136
3137 static struct signatured_type *
3138 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3139 {
3140 struct signatured_type find_entry, *entry;
3141
3142 if (dwarf2_per_objfile->signatured_types == NULL)
3143 {
3144 complaint (&symfile_complaints,
3145 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3146 return 0;
3147 }
3148
3149 find_entry.signature = sig;
3150 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3151 return entry;
3152 }
3153
3154 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3155
3156 static void
3157 init_cu_die_reader (struct die_reader_specs *reader,
3158 struct dwarf2_cu *cu)
3159 {
3160 reader->abfd = cu->objfile->obfd;
3161 reader->cu = cu;
3162 if (cu->per_cu->from_debug_types)
3163 {
3164 gdb_assert (dwarf2_per_objfile->types.readin);
3165 reader->buffer = dwarf2_per_objfile->types.buffer;
3166 }
3167 else
3168 {
3169 gdb_assert (dwarf2_per_objfile->info.readin);
3170 reader->buffer = dwarf2_per_objfile->info.buffer;
3171 }
3172 }
3173
3174 /* Find the base address of the compilation unit for range lists and
3175 location lists. It will normally be specified by DW_AT_low_pc.
3176 In DWARF-3 draft 4, the base address could be overridden by
3177 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3178 compilation units with discontinuous ranges. */
3179
3180 static void
3181 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3182 {
3183 struct attribute *attr;
3184
3185 cu->base_known = 0;
3186 cu->base_address = 0;
3187
3188 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3189 if (attr)
3190 {
3191 cu->base_address = DW_ADDR (attr);
3192 cu->base_known = 1;
3193 }
3194 else
3195 {
3196 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3197 if (attr)
3198 {
3199 cu->base_address = DW_ADDR (attr);
3200 cu->base_known = 1;
3201 }
3202 }
3203 }
3204
3205 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3206 to combine the common parts.
3207 Process a compilation unit for a psymtab.
3208 BUFFER is a pointer to the beginning of the dwarf section buffer,
3209 either .debug_info or debug_types.
3210 INFO_PTR is a pointer to the start of the CU.
3211 Returns a pointer to the next CU. */
3212
3213 static gdb_byte *
3214 process_psymtab_comp_unit (struct objfile *objfile,
3215 struct dwarf2_per_cu_data *this_cu,
3216 gdb_byte *buffer, gdb_byte *info_ptr,
3217 unsigned int buffer_size)
3218 {
3219 bfd *abfd = objfile->obfd;
3220 gdb_byte *beg_of_comp_unit = info_ptr;
3221 struct die_info *comp_unit_die;
3222 struct partial_symtab *pst;
3223 CORE_ADDR baseaddr;
3224 struct cleanup *back_to_inner;
3225 struct dwarf2_cu cu;
3226 int has_children, has_pc_info;
3227 struct attribute *attr;
3228 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3229 struct die_reader_specs reader_specs;
3230 const char *filename;
3231
3232 init_one_comp_unit (&cu, objfile);
3233 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3234
3235 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3236 buffer, buffer_size,
3237 abfd);
3238
3239 /* Complete the cu_header. */
3240 cu.header.offset = beg_of_comp_unit - buffer;
3241 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3242
3243 cu.list_in_scope = &file_symbols;
3244
3245 /* If this compilation unit was already read in, free the
3246 cached copy in order to read it in again. This is
3247 necessary because we skipped some symbols when we first
3248 read in the compilation unit (see load_partial_dies).
3249 This problem could be avoided, but the benefit is
3250 unclear. */
3251 if (this_cu->cu != NULL)
3252 free_one_cached_comp_unit (this_cu->cu);
3253
3254 /* Note that this is a pointer to our stack frame, being
3255 added to a global data structure. It will be cleaned up
3256 in free_stack_comp_unit when we finish with this
3257 compilation unit. */
3258 this_cu->cu = &cu;
3259 cu.per_cu = this_cu;
3260
3261 /* Read the abbrevs for this compilation unit into a table. */
3262 dwarf2_read_abbrevs (abfd, &cu);
3263 make_cleanup (dwarf2_free_abbrev_table, &cu);
3264
3265 /* Read the compilation unit die. */
3266 if (this_cu->from_debug_types)
3267 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3268 init_cu_die_reader (&reader_specs, &cu);
3269 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3270 &has_children);
3271
3272 if (this_cu->from_debug_types)
3273 {
3274 /* LENGTH has not been set yet for type units. */
3275 gdb_assert (this_cu->offset == cu.header.offset);
3276 this_cu->length = cu.header.length + cu.header.initial_length_size;
3277 }
3278 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3279 {
3280 info_ptr = (beg_of_comp_unit + cu.header.length
3281 + cu.header.initial_length_size);
3282 do_cleanups (back_to_inner);
3283 return info_ptr;
3284 }
3285
3286 prepare_one_comp_unit (&cu, comp_unit_die);
3287
3288 /* Allocate a new partial symbol table structure. */
3289 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3290 if (attr == NULL || !DW_STRING (attr))
3291 filename = "";
3292 else
3293 filename = DW_STRING (attr);
3294 pst = start_psymtab_common (objfile, objfile->section_offsets,
3295 filename,
3296 /* TEXTLOW and TEXTHIGH are set below. */
3297 0,
3298 objfile->global_psymbols.next,
3299 objfile->static_psymbols.next);
3300
3301 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3302 if (attr != NULL)
3303 pst->dirname = DW_STRING (attr);
3304
3305 pst->read_symtab_private = this_cu;
3306
3307 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3308
3309 /* Store the function that reads in the rest of the symbol table. */
3310 pst->read_symtab = dwarf2_psymtab_to_symtab;
3311
3312 this_cu->v.psymtab = pst;
3313
3314 dwarf2_find_base_address (comp_unit_die, &cu);
3315
3316 /* Possibly set the default values of LOWPC and HIGHPC from
3317 `DW_AT_ranges'. */
3318 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3319 &best_highpc, &cu, pst);
3320 if (has_pc_info == 1 && best_lowpc < best_highpc)
3321 /* Store the contiguous range if it is not empty; it can be empty for
3322 CUs with no code. */
3323 addrmap_set_empty (objfile->psymtabs_addrmap,
3324 best_lowpc + baseaddr,
3325 best_highpc + baseaddr - 1, pst);
3326
3327 /* Check if comp unit has_children.
3328 If so, read the rest of the partial symbols from this comp unit.
3329 If not, there's no more debug_info for this comp unit. */
3330 if (has_children)
3331 {
3332 struct partial_die_info *first_die;
3333 CORE_ADDR lowpc, highpc;
3334
3335 lowpc = ((CORE_ADDR) -1);
3336 highpc = ((CORE_ADDR) 0);
3337
3338 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3339
3340 scan_partial_symbols (first_die, &lowpc, &highpc,
3341 ! has_pc_info, &cu);
3342
3343 /* If we didn't find a lowpc, set it to highpc to avoid
3344 complaints from `maint check'. */
3345 if (lowpc == ((CORE_ADDR) -1))
3346 lowpc = highpc;
3347
3348 /* If the compilation unit didn't have an explicit address range,
3349 then use the information extracted from its child dies. */
3350 if (! has_pc_info)
3351 {
3352 best_lowpc = lowpc;
3353 best_highpc = highpc;
3354 }
3355 }
3356 pst->textlow = best_lowpc + baseaddr;
3357 pst->texthigh = best_highpc + baseaddr;
3358
3359 pst->n_global_syms = objfile->global_psymbols.next -
3360 (objfile->global_psymbols.list + pst->globals_offset);
3361 pst->n_static_syms = objfile->static_psymbols.next -
3362 (objfile->static_psymbols.list + pst->statics_offset);
3363 sort_pst_symbols (pst);
3364
3365 info_ptr = (beg_of_comp_unit + cu.header.length
3366 + cu.header.initial_length_size);
3367
3368 if (this_cu->from_debug_types)
3369 {
3370 /* It's not clear we want to do anything with stmt lists here.
3371 Waiting to see what gcc ultimately does. */
3372 }
3373 else
3374 {
3375 /* Get the list of files included in the current compilation unit,
3376 and build a psymtab for each of them. */
3377 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3378 }
3379
3380 do_cleanups (back_to_inner);
3381
3382 return info_ptr;
3383 }
3384
3385 /* Traversal function for htab_traverse_noresize.
3386 Process one .debug_types comp-unit. */
3387
3388 static int
3389 process_type_comp_unit (void **slot, void *info)
3390 {
3391 struct signatured_type *entry = (struct signatured_type *) *slot;
3392 struct objfile *objfile = (struct objfile *) info;
3393 struct dwarf2_per_cu_data *this_cu;
3394
3395 this_cu = &entry->per_cu;
3396
3397 gdb_assert (dwarf2_per_objfile->types.readin);
3398 process_psymtab_comp_unit (objfile, this_cu,
3399 dwarf2_per_objfile->types.buffer,
3400 dwarf2_per_objfile->types.buffer + this_cu->offset,
3401 dwarf2_per_objfile->types.size);
3402
3403 return 1;
3404 }
3405
3406 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3407 Build partial symbol tables for the .debug_types comp-units. */
3408
3409 static void
3410 build_type_psymtabs (struct objfile *objfile)
3411 {
3412 if (! create_debug_types_hash_table (objfile))
3413 return;
3414
3415 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3416 process_type_comp_unit, objfile);
3417 }
3418
3419 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3420
3421 static void
3422 psymtabs_addrmap_cleanup (void *o)
3423 {
3424 struct objfile *objfile = o;
3425
3426 objfile->psymtabs_addrmap = NULL;
3427 }
3428
3429 /* Build the partial symbol table by doing a quick pass through the
3430 .debug_info and .debug_abbrev sections. */
3431
3432 static void
3433 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3434 {
3435 gdb_byte *info_ptr;
3436 struct cleanup *back_to, *addrmap_cleanup;
3437 struct obstack temp_obstack;
3438
3439 dwarf2_per_objfile->reading_partial_symbols = 1;
3440
3441 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3442 info_ptr = dwarf2_per_objfile->info.buffer;
3443
3444 /* Any cached compilation units will be linked by the per-objfile
3445 read_in_chain. Make sure to free them when we're done. */
3446 back_to = make_cleanup (free_cached_comp_units, NULL);
3447
3448 build_type_psymtabs (objfile);
3449
3450 create_all_comp_units (objfile);
3451
3452 /* Create a temporary address map on a temporary obstack. We later
3453 copy this to the final obstack. */
3454 obstack_init (&temp_obstack);
3455 make_cleanup_obstack_free (&temp_obstack);
3456 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3457 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3458
3459 /* Since the objects we're extracting from .debug_info vary in
3460 length, only the individual functions to extract them (like
3461 read_comp_unit_head and load_partial_die) can really know whether
3462 the buffer is large enough to hold another complete object.
3463
3464 At the moment, they don't actually check that. If .debug_info
3465 holds just one extra byte after the last compilation unit's dies,
3466 then read_comp_unit_head will happily read off the end of the
3467 buffer. read_partial_die is similarly casual. Those functions
3468 should be fixed.
3469
3470 For this loop condition, simply checking whether there's any data
3471 left at all should be sufficient. */
3472
3473 while (info_ptr < (dwarf2_per_objfile->info.buffer
3474 + dwarf2_per_objfile->info.size))
3475 {
3476 struct dwarf2_per_cu_data *this_cu;
3477
3478 this_cu = dwarf2_find_comp_unit (info_ptr
3479 - dwarf2_per_objfile->info.buffer,
3480 objfile);
3481
3482 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3483 dwarf2_per_objfile->info.buffer,
3484 info_ptr,
3485 dwarf2_per_objfile->info.size);
3486 }
3487
3488 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3489 &objfile->objfile_obstack);
3490 discard_cleanups (addrmap_cleanup);
3491
3492 do_cleanups (back_to);
3493 }
3494
3495 /* Load the partial DIEs for a secondary CU into memory. */
3496
3497 static void
3498 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3499 struct objfile *objfile)
3500 {
3501 bfd *abfd = objfile->obfd;
3502 gdb_byte *info_ptr, *beg_of_comp_unit;
3503 struct die_info *comp_unit_die;
3504 struct dwarf2_cu *cu;
3505 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3506 int has_children;
3507 struct die_reader_specs reader_specs;
3508 int read_cu = 0;
3509
3510 gdb_assert (! this_cu->from_debug_types);
3511
3512 gdb_assert (dwarf2_per_objfile->info.readin);
3513 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3514 beg_of_comp_unit = info_ptr;
3515
3516 if (this_cu->cu == NULL)
3517 {
3518 cu = xmalloc (sizeof (*cu));
3519 init_one_comp_unit (cu, objfile);
3520
3521 read_cu = 1;
3522
3523 /* If an error occurs while loading, release our storage. */
3524 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3525
3526 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3527 dwarf2_per_objfile->info.buffer,
3528 dwarf2_per_objfile->info.size,
3529 abfd);
3530
3531 /* Complete the cu_header. */
3532 cu->header.offset = this_cu->offset;
3533 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3534
3535 /* Link this compilation unit into the compilation unit tree. */
3536 this_cu->cu = cu;
3537 cu->per_cu = this_cu;
3538
3539 /* Link this CU into read_in_chain. */
3540 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3541 dwarf2_per_objfile->read_in_chain = this_cu;
3542 }
3543 else
3544 {
3545 cu = this_cu->cu;
3546 info_ptr += cu->header.first_die_offset;
3547 }
3548
3549 /* Read the abbrevs for this compilation unit into a table. */
3550 gdb_assert (cu->dwarf2_abbrevs == NULL);
3551 dwarf2_read_abbrevs (abfd, cu);
3552 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3553
3554 /* Read the compilation unit die. */
3555 init_cu_die_reader (&reader_specs, cu);
3556 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3557 &has_children);
3558
3559 prepare_one_comp_unit (cu, comp_unit_die);
3560
3561 /* Check if comp unit has_children.
3562 If so, read the rest of the partial symbols from this comp unit.
3563 If not, there's no more debug_info for this comp unit. */
3564 if (has_children)
3565 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3566
3567 do_cleanups (free_abbrevs_cleanup);
3568
3569 if (read_cu)
3570 {
3571 /* We've successfully allocated this compilation unit. Let our
3572 caller clean it up when finished with it. */
3573 discard_cleanups (free_cu_cleanup);
3574 }
3575 }
3576
3577 /* Create a list of all compilation units in OBJFILE. We do this only
3578 if an inter-comp-unit reference is found; presumably if there is one,
3579 there will be many, and one will occur early in the .debug_info section.
3580 So there's no point in building this list incrementally. */
3581
3582 static void
3583 create_all_comp_units (struct objfile *objfile)
3584 {
3585 int n_allocated;
3586 int n_comp_units;
3587 struct dwarf2_per_cu_data **all_comp_units;
3588 gdb_byte *info_ptr;
3589
3590 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3591 info_ptr = dwarf2_per_objfile->info.buffer;
3592
3593 n_comp_units = 0;
3594 n_allocated = 10;
3595 all_comp_units = xmalloc (n_allocated
3596 * sizeof (struct dwarf2_per_cu_data *));
3597
3598 while (info_ptr < dwarf2_per_objfile->info.buffer
3599 + dwarf2_per_objfile->info.size)
3600 {
3601 unsigned int length, initial_length_size;
3602 struct dwarf2_per_cu_data *this_cu;
3603 unsigned int offset;
3604
3605 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3606
3607 /* Read just enough information to find out where the next
3608 compilation unit is. */
3609 length = read_initial_length (objfile->obfd, info_ptr,
3610 &initial_length_size);
3611
3612 /* Save the compilation unit for later lookup. */
3613 this_cu = obstack_alloc (&objfile->objfile_obstack,
3614 sizeof (struct dwarf2_per_cu_data));
3615 memset (this_cu, 0, sizeof (*this_cu));
3616 this_cu->offset = offset;
3617 this_cu->length = length + initial_length_size;
3618 this_cu->objfile = objfile;
3619
3620 if (n_comp_units == n_allocated)
3621 {
3622 n_allocated *= 2;
3623 all_comp_units = xrealloc (all_comp_units,
3624 n_allocated
3625 * sizeof (struct dwarf2_per_cu_data *));
3626 }
3627 all_comp_units[n_comp_units++] = this_cu;
3628
3629 info_ptr = info_ptr + this_cu->length;
3630 }
3631
3632 dwarf2_per_objfile->all_comp_units
3633 = obstack_alloc (&objfile->objfile_obstack,
3634 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3635 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3636 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3637 xfree (all_comp_units);
3638 dwarf2_per_objfile->n_comp_units = n_comp_units;
3639 }
3640
3641 /* Process all loaded DIEs for compilation unit CU, starting at
3642 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3643 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3644 DW_AT_ranges). If NEED_PC is set, then this function will set
3645 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3646 and record the covered ranges in the addrmap. */
3647
3648 static void
3649 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3650 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3651 {
3652 struct partial_die_info *pdi;
3653
3654 /* Now, march along the PDI's, descending into ones which have
3655 interesting children but skipping the children of the other ones,
3656 until we reach the end of the compilation unit. */
3657
3658 pdi = first_die;
3659
3660 while (pdi != NULL)
3661 {
3662 fixup_partial_die (pdi, cu);
3663
3664 /* Anonymous namespaces or modules have no name but have interesting
3665 children, so we need to look at them. Ditto for anonymous
3666 enums. */
3667
3668 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3669 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3670 {
3671 switch (pdi->tag)
3672 {
3673 case DW_TAG_subprogram:
3674 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3675 break;
3676 case DW_TAG_constant:
3677 case DW_TAG_variable:
3678 case DW_TAG_typedef:
3679 case DW_TAG_union_type:
3680 if (!pdi->is_declaration)
3681 {
3682 add_partial_symbol (pdi, cu);
3683 }
3684 break;
3685 case DW_TAG_class_type:
3686 case DW_TAG_interface_type:
3687 case DW_TAG_structure_type:
3688 if (!pdi->is_declaration)
3689 {
3690 add_partial_symbol (pdi, cu);
3691 }
3692 break;
3693 case DW_TAG_enumeration_type:
3694 if (!pdi->is_declaration)
3695 add_partial_enumeration (pdi, cu);
3696 break;
3697 case DW_TAG_base_type:
3698 case DW_TAG_subrange_type:
3699 /* File scope base type definitions are added to the partial
3700 symbol table. */
3701 add_partial_symbol (pdi, cu);
3702 break;
3703 case DW_TAG_namespace:
3704 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3705 break;
3706 case DW_TAG_module:
3707 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3708 break;
3709 default:
3710 break;
3711 }
3712 }
3713
3714 /* If the die has a sibling, skip to the sibling. */
3715
3716 pdi = pdi->die_sibling;
3717 }
3718 }
3719
3720 /* Functions used to compute the fully scoped name of a partial DIE.
3721
3722 Normally, this is simple. For C++, the parent DIE's fully scoped
3723 name is concatenated with "::" and the partial DIE's name. For
3724 Java, the same thing occurs except that "." is used instead of "::".
3725 Enumerators are an exception; they use the scope of their parent
3726 enumeration type, i.e. the name of the enumeration type is not
3727 prepended to the enumerator.
3728
3729 There are two complexities. One is DW_AT_specification; in this
3730 case "parent" means the parent of the target of the specification,
3731 instead of the direct parent of the DIE. The other is compilers
3732 which do not emit DW_TAG_namespace; in this case we try to guess
3733 the fully qualified name of structure types from their members'
3734 linkage names. This must be done using the DIE's children rather
3735 than the children of any DW_AT_specification target. We only need
3736 to do this for structures at the top level, i.e. if the target of
3737 any DW_AT_specification (if any; otherwise the DIE itself) does not
3738 have a parent. */
3739
3740 /* Compute the scope prefix associated with PDI's parent, in
3741 compilation unit CU. The result will be allocated on CU's
3742 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3743 field. NULL is returned if no prefix is necessary. */
3744 static char *
3745 partial_die_parent_scope (struct partial_die_info *pdi,
3746 struct dwarf2_cu *cu)
3747 {
3748 char *grandparent_scope;
3749 struct partial_die_info *parent, *real_pdi;
3750
3751 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3752 then this means the parent of the specification DIE. */
3753
3754 real_pdi = pdi;
3755 while (real_pdi->has_specification)
3756 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3757
3758 parent = real_pdi->die_parent;
3759 if (parent == NULL)
3760 return NULL;
3761
3762 if (parent->scope_set)
3763 return parent->scope;
3764
3765 fixup_partial_die (parent, cu);
3766
3767 grandparent_scope = partial_die_parent_scope (parent, cu);
3768
3769 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3770 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3771 Work around this problem here. */
3772 if (cu->language == language_cplus
3773 && parent->tag == DW_TAG_namespace
3774 && strcmp (parent->name, "::") == 0
3775 && grandparent_scope == NULL)
3776 {
3777 parent->scope = NULL;
3778 parent->scope_set = 1;
3779 return NULL;
3780 }
3781
3782 if (parent->tag == DW_TAG_namespace
3783 || parent->tag == DW_TAG_module
3784 || parent->tag == DW_TAG_structure_type
3785 || parent->tag == DW_TAG_class_type
3786 || parent->tag == DW_TAG_interface_type
3787 || parent->tag == DW_TAG_union_type
3788 || parent->tag == DW_TAG_enumeration_type)
3789 {
3790 if (grandparent_scope == NULL)
3791 parent->scope = parent->name;
3792 else
3793 parent->scope = typename_concat (&cu->comp_unit_obstack,
3794 grandparent_scope,
3795 parent->name, 0, cu);
3796 }
3797 else if (parent->tag == DW_TAG_enumerator)
3798 /* Enumerators should not get the name of the enumeration as a prefix. */
3799 parent->scope = grandparent_scope;
3800 else
3801 {
3802 /* FIXME drow/2004-04-01: What should we be doing with
3803 function-local names? For partial symbols, we should probably be
3804 ignoring them. */
3805 complaint (&symfile_complaints,
3806 _("unhandled containing DIE tag %d for DIE at %d"),
3807 parent->tag, pdi->offset);
3808 parent->scope = grandparent_scope;
3809 }
3810
3811 parent->scope_set = 1;
3812 return parent->scope;
3813 }
3814
3815 /* Return the fully scoped name associated with PDI, from compilation unit
3816 CU. The result will be allocated with malloc. */
3817 static char *
3818 partial_die_full_name (struct partial_die_info *pdi,
3819 struct dwarf2_cu *cu)
3820 {
3821 char *parent_scope;
3822
3823 /* If this is a template instantiation, we can not work out the
3824 template arguments from partial DIEs. So, unfortunately, we have
3825 to go through the full DIEs. At least any work we do building
3826 types here will be reused if full symbols are loaded later. */
3827 if (pdi->has_template_arguments)
3828 {
3829 fixup_partial_die (pdi, cu);
3830
3831 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3832 {
3833 struct die_info *die;
3834 struct attribute attr;
3835 struct dwarf2_cu *ref_cu = cu;
3836
3837 attr.name = 0;
3838 attr.form = DW_FORM_ref_addr;
3839 attr.u.addr = pdi->offset;
3840 die = follow_die_ref (NULL, &attr, &ref_cu);
3841
3842 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3843 }
3844 }
3845
3846 parent_scope = partial_die_parent_scope (pdi, cu);
3847 if (parent_scope == NULL)
3848 return NULL;
3849 else
3850 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3851 }
3852
3853 static void
3854 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3855 {
3856 struct objfile *objfile = cu->objfile;
3857 CORE_ADDR addr = 0;
3858 char *actual_name = NULL;
3859 const struct partial_symbol *psym = NULL;
3860 CORE_ADDR baseaddr;
3861 int built_actual_name = 0;
3862
3863 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3864
3865 actual_name = partial_die_full_name (pdi, cu);
3866 if (actual_name)
3867 built_actual_name = 1;
3868
3869 if (actual_name == NULL)
3870 actual_name = pdi->name;
3871
3872 switch (pdi->tag)
3873 {
3874 case DW_TAG_subprogram:
3875 if (pdi->is_external || cu->language == language_ada)
3876 {
3877 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3878 of the global scope. But in Ada, we want to be able to access
3879 nested procedures globally. So all Ada subprograms are stored
3880 in the global scope. */
3881 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3882 mst_text, objfile); */
3883 add_psymbol_to_list (actual_name, strlen (actual_name),
3884 built_actual_name,
3885 VAR_DOMAIN, LOC_BLOCK,
3886 &objfile->global_psymbols,
3887 0, pdi->lowpc + baseaddr,
3888 cu->language, objfile);
3889 }
3890 else
3891 {
3892 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3893 mst_file_text, objfile); */
3894 add_psymbol_to_list (actual_name, strlen (actual_name),
3895 built_actual_name,
3896 VAR_DOMAIN, LOC_BLOCK,
3897 &objfile->static_psymbols,
3898 0, pdi->lowpc + baseaddr,
3899 cu->language, objfile);
3900 }
3901 break;
3902 case DW_TAG_constant:
3903 {
3904 struct psymbol_allocation_list *list;
3905
3906 if (pdi->is_external)
3907 list = &objfile->global_psymbols;
3908 else
3909 list = &objfile->static_psymbols;
3910 add_psymbol_to_list (actual_name, strlen (actual_name),
3911 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3912 list, 0, 0, cu->language, objfile);
3913 }
3914 break;
3915 case DW_TAG_variable:
3916 if (pdi->locdesc)
3917 addr = decode_locdesc (pdi->locdesc, cu);
3918
3919 if (pdi->locdesc
3920 && addr == 0
3921 && !dwarf2_per_objfile->has_section_at_zero)
3922 {
3923 /* A global or static variable may also have been stripped
3924 out by the linker if unused, in which case its address
3925 will be nullified; do not add such variables into partial
3926 symbol table then. */
3927 }
3928 else if (pdi->is_external)
3929 {
3930 /* Global Variable.
3931 Don't enter into the minimal symbol tables as there is
3932 a minimal symbol table entry from the ELF symbols already.
3933 Enter into partial symbol table if it has a location
3934 descriptor or a type.
3935 If the location descriptor is missing, new_symbol will create
3936 a LOC_UNRESOLVED symbol, the address of the variable will then
3937 be determined from the minimal symbol table whenever the variable
3938 is referenced.
3939 The address for the partial symbol table entry is not
3940 used by GDB, but it comes in handy for debugging partial symbol
3941 table building. */
3942
3943 if (pdi->locdesc || pdi->has_type)
3944 add_psymbol_to_list (actual_name, strlen (actual_name),
3945 built_actual_name,
3946 VAR_DOMAIN, LOC_STATIC,
3947 &objfile->global_psymbols,
3948 0, addr + baseaddr,
3949 cu->language, objfile);
3950 }
3951 else
3952 {
3953 /* Static Variable. Skip symbols without location descriptors. */
3954 if (pdi->locdesc == NULL)
3955 {
3956 if (built_actual_name)
3957 xfree (actual_name);
3958 return;
3959 }
3960 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3961 mst_file_data, objfile); */
3962 add_psymbol_to_list (actual_name, strlen (actual_name),
3963 built_actual_name,
3964 VAR_DOMAIN, LOC_STATIC,
3965 &objfile->static_psymbols,
3966 0, addr + baseaddr,
3967 cu->language, objfile);
3968 }
3969 break;
3970 case DW_TAG_typedef:
3971 case DW_TAG_base_type:
3972 case DW_TAG_subrange_type:
3973 add_psymbol_to_list (actual_name, strlen (actual_name),
3974 built_actual_name,
3975 VAR_DOMAIN, LOC_TYPEDEF,
3976 &objfile->static_psymbols,
3977 0, (CORE_ADDR) 0, cu->language, objfile);
3978 break;
3979 case DW_TAG_namespace:
3980 add_psymbol_to_list (actual_name, strlen (actual_name),
3981 built_actual_name,
3982 VAR_DOMAIN, LOC_TYPEDEF,
3983 &objfile->global_psymbols,
3984 0, (CORE_ADDR) 0, cu->language, objfile);
3985 break;
3986 case DW_TAG_class_type:
3987 case DW_TAG_interface_type:
3988 case DW_TAG_structure_type:
3989 case DW_TAG_union_type:
3990 case DW_TAG_enumeration_type:
3991 /* Skip external references. The DWARF standard says in the section
3992 about "Structure, Union, and Class Type Entries": "An incomplete
3993 structure, union or class type is represented by a structure,
3994 union or class entry that does not have a byte size attribute
3995 and that has a DW_AT_declaration attribute." */
3996 if (!pdi->has_byte_size && pdi->is_declaration)
3997 {
3998 if (built_actual_name)
3999 xfree (actual_name);
4000 return;
4001 }
4002
4003 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4004 static vs. global. */
4005 add_psymbol_to_list (actual_name, strlen (actual_name),
4006 built_actual_name,
4007 STRUCT_DOMAIN, LOC_TYPEDEF,
4008 (cu->language == language_cplus
4009 || cu->language == language_java)
4010 ? &objfile->global_psymbols
4011 : &objfile->static_psymbols,
4012 0, (CORE_ADDR) 0, cu->language, objfile);
4013
4014 break;
4015 case DW_TAG_enumerator:
4016 add_psymbol_to_list (actual_name, strlen (actual_name),
4017 built_actual_name,
4018 VAR_DOMAIN, LOC_CONST,
4019 (cu->language == language_cplus
4020 || cu->language == language_java)
4021 ? &objfile->global_psymbols
4022 : &objfile->static_psymbols,
4023 0, (CORE_ADDR) 0, cu->language, objfile);
4024 break;
4025 default:
4026 break;
4027 }
4028
4029 if (built_actual_name)
4030 xfree (actual_name);
4031 }
4032
4033 /* Read a partial die corresponding to a namespace; also, add a symbol
4034 corresponding to that namespace to the symbol table. NAMESPACE is
4035 the name of the enclosing namespace. */
4036
4037 static void
4038 add_partial_namespace (struct partial_die_info *pdi,
4039 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4040 int need_pc, struct dwarf2_cu *cu)
4041 {
4042 /* Add a symbol for the namespace. */
4043
4044 add_partial_symbol (pdi, cu);
4045
4046 /* Now scan partial symbols in that namespace. */
4047
4048 if (pdi->has_children)
4049 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4050 }
4051
4052 /* Read a partial die corresponding to a Fortran module. */
4053
4054 static void
4055 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4056 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4057 {
4058 /* Now scan partial symbols in that module. */
4059
4060 if (pdi->has_children)
4061 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4062 }
4063
4064 /* Read a partial die corresponding to a subprogram and create a partial
4065 symbol for that subprogram. When the CU language allows it, this
4066 routine also defines a partial symbol for each nested subprogram
4067 that this subprogram contains.
4068
4069 DIE my also be a lexical block, in which case we simply search
4070 recursively for suprograms defined inside that lexical block.
4071 Again, this is only performed when the CU language allows this
4072 type of definitions. */
4073
4074 static void
4075 add_partial_subprogram (struct partial_die_info *pdi,
4076 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4077 int need_pc, struct dwarf2_cu *cu)
4078 {
4079 if (pdi->tag == DW_TAG_subprogram)
4080 {
4081 if (pdi->has_pc_info)
4082 {
4083 if (pdi->lowpc < *lowpc)
4084 *lowpc = pdi->lowpc;
4085 if (pdi->highpc > *highpc)
4086 *highpc = pdi->highpc;
4087 if (need_pc)
4088 {
4089 CORE_ADDR baseaddr;
4090 struct objfile *objfile = cu->objfile;
4091
4092 baseaddr = ANOFFSET (objfile->section_offsets,
4093 SECT_OFF_TEXT (objfile));
4094 addrmap_set_empty (objfile->psymtabs_addrmap,
4095 pdi->lowpc + baseaddr,
4096 pdi->highpc - 1 + baseaddr,
4097 cu->per_cu->v.psymtab);
4098 }
4099 if (!pdi->is_declaration)
4100 /* Ignore subprogram DIEs that do not have a name, they are
4101 illegal. Do not emit a complaint at this point, we will
4102 do so when we convert this psymtab into a symtab. */
4103 if (pdi->name)
4104 add_partial_symbol (pdi, cu);
4105 }
4106 }
4107
4108 if (! pdi->has_children)
4109 return;
4110
4111 if (cu->language == language_ada)
4112 {
4113 pdi = pdi->die_child;
4114 while (pdi != NULL)
4115 {
4116 fixup_partial_die (pdi, cu);
4117 if (pdi->tag == DW_TAG_subprogram
4118 || pdi->tag == DW_TAG_lexical_block)
4119 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4120 pdi = pdi->die_sibling;
4121 }
4122 }
4123 }
4124
4125 /* Read a partial die corresponding to an enumeration type. */
4126
4127 static void
4128 add_partial_enumeration (struct partial_die_info *enum_pdi,
4129 struct dwarf2_cu *cu)
4130 {
4131 struct partial_die_info *pdi;
4132
4133 if (enum_pdi->name != NULL)
4134 add_partial_symbol (enum_pdi, cu);
4135
4136 pdi = enum_pdi->die_child;
4137 while (pdi)
4138 {
4139 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4140 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4141 else
4142 add_partial_symbol (pdi, cu);
4143 pdi = pdi->die_sibling;
4144 }
4145 }
4146
4147 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4148 Return the corresponding abbrev, or NULL if the number is zero (indicating
4149 an empty DIE). In either case *BYTES_READ will be set to the length of
4150 the initial number. */
4151
4152 static struct abbrev_info *
4153 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4154 struct dwarf2_cu *cu)
4155 {
4156 bfd *abfd = cu->objfile->obfd;
4157 unsigned int abbrev_number;
4158 struct abbrev_info *abbrev;
4159
4160 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4161
4162 if (abbrev_number == 0)
4163 return NULL;
4164
4165 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4166 if (!abbrev)
4167 {
4168 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4169 abbrev_number, bfd_get_filename (abfd));
4170 }
4171
4172 return abbrev;
4173 }
4174
4175 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4176 Returns a pointer to the end of a series of DIEs, terminated by an empty
4177 DIE. Any children of the skipped DIEs will also be skipped. */
4178
4179 static gdb_byte *
4180 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4181 {
4182 struct abbrev_info *abbrev;
4183 unsigned int bytes_read;
4184
4185 while (1)
4186 {
4187 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4188 if (abbrev == NULL)
4189 return info_ptr + bytes_read;
4190 else
4191 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4192 }
4193 }
4194
4195 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4196 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4197 abbrev corresponding to that skipped uleb128 should be passed in
4198 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4199 children. */
4200
4201 static gdb_byte *
4202 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4203 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4204 {
4205 unsigned int bytes_read;
4206 struct attribute attr;
4207 bfd *abfd = cu->objfile->obfd;
4208 unsigned int form, i;
4209
4210 for (i = 0; i < abbrev->num_attrs; i++)
4211 {
4212 /* The only abbrev we care about is DW_AT_sibling. */
4213 if (abbrev->attrs[i].name == DW_AT_sibling)
4214 {
4215 read_attribute (&attr, &abbrev->attrs[i],
4216 abfd, info_ptr, cu);
4217 if (attr.form == DW_FORM_ref_addr)
4218 complaint (&symfile_complaints,
4219 _("ignoring absolute DW_AT_sibling"));
4220 else
4221 return buffer + dwarf2_get_ref_die_offset (&attr);
4222 }
4223
4224 /* If it isn't DW_AT_sibling, skip this attribute. */
4225 form = abbrev->attrs[i].form;
4226 skip_attribute:
4227 switch (form)
4228 {
4229 case DW_FORM_ref_addr:
4230 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4231 and later it is offset sized. */
4232 if (cu->header.version == 2)
4233 info_ptr += cu->header.addr_size;
4234 else
4235 info_ptr += cu->header.offset_size;
4236 break;
4237 case DW_FORM_addr:
4238 info_ptr += cu->header.addr_size;
4239 break;
4240 case DW_FORM_data1:
4241 case DW_FORM_ref1:
4242 case DW_FORM_flag:
4243 info_ptr += 1;
4244 break;
4245 case DW_FORM_flag_present:
4246 break;
4247 case DW_FORM_data2:
4248 case DW_FORM_ref2:
4249 info_ptr += 2;
4250 break;
4251 case DW_FORM_data4:
4252 case DW_FORM_ref4:
4253 info_ptr += 4;
4254 break;
4255 case DW_FORM_data8:
4256 case DW_FORM_ref8:
4257 case DW_FORM_ref_sig8:
4258 info_ptr += 8;
4259 break;
4260 case DW_FORM_string:
4261 read_direct_string (abfd, info_ptr, &bytes_read);
4262 info_ptr += bytes_read;
4263 break;
4264 case DW_FORM_sec_offset:
4265 case DW_FORM_strp:
4266 info_ptr += cu->header.offset_size;
4267 break;
4268 case DW_FORM_exprloc:
4269 case DW_FORM_block:
4270 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4271 info_ptr += bytes_read;
4272 break;
4273 case DW_FORM_block1:
4274 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4275 break;
4276 case DW_FORM_block2:
4277 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4278 break;
4279 case DW_FORM_block4:
4280 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4281 break;
4282 case DW_FORM_sdata:
4283 case DW_FORM_udata:
4284 case DW_FORM_ref_udata:
4285 info_ptr = skip_leb128 (abfd, info_ptr);
4286 break;
4287 case DW_FORM_indirect:
4288 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4289 info_ptr += bytes_read;
4290 /* We need to continue parsing from here, so just go back to
4291 the top. */
4292 goto skip_attribute;
4293
4294 default:
4295 error (_("Dwarf Error: Cannot handle %s "
4296 "in DWARF reader [in module %s]"),
4297 dwarf_form_name (form),
4298 bfd_get_filename (abfd));
4299 }
4300 }
4301
4302 if (abbrev->has_children)
4303 return skip_children (buffer, info_ptr, cu);
4304 else
4305 return info_ptr;
4306 }
4307
4308 /* Locate ORIG_PDI's sibling.
4309 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4310 in BUFFER. */
4311
4312 static gdb_byte *
4313 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4314 gdb_byte *buffer, gdb_byte *info_ptr,
4315 bfd *abfd, struct dwarf2_cu *cu)
4316 {
4317 /* Do we know the sibling already? */
4318
4319 if (orig_pdi->sibling)
4320 return orig_pdi->sibling;
4321
4322 /* Are there any children to deal with? */
4323
4324 if (!orig_pdi->has_children)
4325 return info_ptr;
4326
4327 /* Skip the children the long way. */
4328
4329 return skip_children (buffer, info_ptr, cu);
4330 }
4331
4332 /* Expand this partial symbol table into a full symbol table. */
4333
4334 static void
4335 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4336 {
4337 if (pst != NULL)
4338 {
4339 if (pst->readin)
4340 {
4341 warning (_("bug: psymtab for %s is already read in."),
4342 pst->filename);
4343 }
4344 else
4345 {
4346 if (info_verbose)
4347 {
4348 printf_filtered (_("Reading in symbols for %s..."),
4349 pst->filename);
4350 gdb_flush (gdb_stdout);
4351 }
4352
4353 /* Restore our global data. */
4354 dwarf2_per_objfile = objfile_data (pst->objfile,
4355 dwarf2_objfile_data_key);
4356
4357 /* If this psymtab is constructed from a debug-only objfile, the
4358 has_section_at_zero flag will not necessarily be correct. We
4359 can get the correct value for this flag by looking at the data
4360 associated with the (presumably stripped) associated objfile. */
4361 if (pst->objfile->separate_debug_objfile_backlink)
4362 {
4363 struct dwarf2_per_objfile *dpo_backlink
4364 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4365 dwarf2_objfile_data_key);
4366
4367 dwarf2_per_objfile->has_section_at_zero
4368 = dpo_backlink->has_section_at_zero;
4369 }
4370
4371 dwarf2_per_objfile->reading_partial_symbols = 0;
4372
4373 psymtab_to_symtab_1 (pst);
4374
4375 /* Finish up the debug error message. */
4376 if (info_verbose)
4377 printf_filtered (_("done.\n"));
4378 }
4379 }
4380 }
4381
4382 /* Add PER_CU to the queue. */
4383
4384 static void
4385 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4386 {
4387 struct dwarf2_queue_item *item;
4388
4389 per_cu->queued = 1;
4390 item = xmalloc (sizeof (*item));
4391 item->per_cu = per_cu;
4392 item->next = NULL;
4393
4394 if (dwarf2_queue == NULL)
4395 dwarf2_queue = item;
4396 else
4397 dwarf2_queue_tail->next = item;
4398
4399 dwarf2_queue_tail = item;
4400 }
4401
4402 /* Process the queue. */
4403
4404 static void
4405 process_queue (struct objfile *objfile)
4406 {
4407 struct dwarf2_queue_item *item, *next_item;
4408
4409 /* The queue starts out with one item, but following a DIE reference
4410 may load a new CU, adding it to the end of the queue. */
4411 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4412 {
4413 if (dwarf2_per_objfile->using_index
4414 ? !item->per_cu->v.quick->symtab
4415 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4416 process_full_comp_unit (item->per_cu);
4417
4418 item->per_cu->queued = 0;
4419 next_item = item->next;
4420 xfree (item);
4421 }
4422
4423 dwarf2_queue_tail = NULL;
4424 }
4425
4426 /* Free all allocated queue entries. This function only releases anything if
4427 an error was thrown; if the queue was processed then it would have been
4428 freed as we went along. */
4429
4430 static void
4431 dwarf2_release_queue (void *dummy)
4432 {
4433 struct dwarf2_queue_item *item, *last;
4434
4435 item = dwarf2_queue;
4436 while (item)
4437 {
4438 /* Anything still marked queued is likely to be in an
4439 inconsistent state, so discard it. */
4440 if (item->per_cu->queued)
4441 {
4442 if (item->per_cu->cu != NULL)
4443 free_one_cached_comp_unit (item->per_cu->cu);
4444 item->per_cu->queued = 0;
4445 }
4446
4447 last = item;
4448 item = item->next;
4449 xfree (last);
4450 }
4451
4452 dwarf2_queue = dwarf2_queue_tail = NULL;
4453 }
4454
4455 /* Read in full symbols for PST, and anything it depends on. */
4456
4457 static void
4458 psymtab_to_symtab_1 (struct partial_symtab *pst)
4459 {
4460 struct dwarf2_per_cu_data *per_cu;
4461 struct cleanup *back_to;
4462 int i;
4463
4464 for (i = 0; i < pst->number_of_dependencies; i++)
4465 if (!pst->dependencies[i]->readin)
4466 {
4467 /* Inform about additional files that need to be read in. */
4468 if (info_verbose)
4469 {
4470 /* FIXME: i18n: Need to make this a single string. */
4471 fputs_filtered (" ", gdb_stdout);
4472 wrap_here ("");
4473 fputs_filtered ("and ", gdb_stdout);
4474 wrap_here ("");
4475 printf_filtered ("%s...", pst->dependencies[i]->filename);
4476 wrap_here (""); /* Flush output. */
4477 gdb_flush (gdb_stdout);
4478 }
4479 psymtab_to_symtab_1 (pst->dependencies[i]);
4480 }
4481
4482 per_cu = pst->read_symtab_private;
4483
4484 if (per_cu == NULL)
4485 {
4486 /* It's an include file, no symbols to read for it.
4487 Everything is in the parent symtab. */
4488 pst->readin = 1;
4489 return;
4490 }
4491
4492 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4493 }
4494
4495 /* Load the DIEs associated with PER_CU into memory. */
4496
4497 static void
4498 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4499 struct objfile *objfile)
4500 {
4501 bfd *abfd = objfile->obfd;
4502 struct dwarf2_cu *cu;
4503 unsigned int offset;
4504 gdb_byte *info_ptr, *beg_of_comp_unit;
4505 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4506 struct attribute *attr;
4507 int read_cu = 0;
4508
4509 gdb_assert (! per_cu->from_debug_types);
4510
4511 /* Set local variables from the partial symbol table info. */
4512 offset = per_cu->offset;
4513
4514 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4515 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4516 beg_of_comp_unit = info_ptr;
4517
4518 if (per_cu->cu == NULL)
4519 {
4520 cu = xmalloc (sizeof (*cu));
4521 init_one_comp_unit (cu, objfile);
4522
4523 read_cu = 1;
4524
4525 /* If an error occurs while loading, release our storage. */
4526 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4527
4528 /* Read in the comp_unit header. */
4529 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4530
4531 /* Complete the cu_header. */
4532 cu->header.offset = offset;
4533 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4534
4535 /* Read the abbrevs for this compilation unit. */
4536 dwarf2_read_abbrevs (abfd, cu);
4537 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4538
4539 /* Link this compilation unit into the compilation unit tree. */
4540 per_cu->cu = cu;
4541 cu->per_cu = per_cu;
4542
4543 /* Link this CU into read_in_chain. */
4544 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4545 dwarf2_per_objfile->read_in_chain = per_cu;
4546 }
4547 else
4548 {
4549 cu = per_cu->cu;
4550 info_ptr += cu->header.first_die_offset;
4551 }
4552
4553 cu->dies = read_comp_unit (info_ptr, cu);
4554
4555 /* We try not to read any attributes in this function, because not
4556 all objfiles needed for references have been loaded yet, and symbol
4557 table processing isn't initialized. But we have to set the CU language,
4558 or we won't be able to build types correctly. */
4559 prepare_one_comp_unit (cu, cu->dies);
4560
4561 /* Similarly, if we do not read the producer, we can not apply
4562 producer-specific interpretation. */
4563 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4564 if (attr)
4565 cu->producer = DW_STRING (attr);
4566
4567 if (read_cu)
4568 {
4569 do_cleanups (free_abbrevs_cleanup);
4570
4571 /* We've successfully allocated this compilation unit. Let our
4572 caller clean it up when finished with it. */
4573 discard_cleanups (free_cu_cleanup);
4574 }
4575 }
4576
4577 /* Add a DIE to the delayed physname list. */
4578
4579 static void
4580 add_to_method_list (struct type *type, int fnfield_index, int index,
4581 const char *name, struct die_info *die,
4582 struct dwarf2_cu *cu)
4583 {
4584 struct delayed_method_info mi;
4585 mi.type = type;
4586 mi.fnfield_index = fnfield_index;
4587 mi.index = index;
4588 mi.name = name;
4589 mi.die = die;
4590 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4591 }
4592
4593 /* A cleanup for freeing the delayed method list. */
4594
4595 static void
4596 free_delayed_list (void *ptr)
4597 {
4598 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4599 if (cu->method_list != NULL)
4600 {
4601 VEC_free (delayed_method_info, cu->method_list);
4602 cu->method_list = NULL;
4603 }
4604 }
4605
4606 /* Compute the physnames of any methods on the CU's method list.
4607
4608 The computation of method physnames is delayed in order to avoid the
4609 (bad) condition that one of the method's formal parameters is of an as yet
4610 incomplete type. */
4611
4612 static void
4613 compute_delayed_physnames (struct dwarf2_cu *cu)
4614 {
4615 int i;
4616 struct delayed_method_info *mi;
4617 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4618 {
4619 const char *physname;
4620 struct fn_fieldlist *fn_flp
4621 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4622 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4623 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4624 }
4625 }
4626
4627 /* Check for GCC >= 4.0. */
4628
4629 static int
4630 producer_is_gcc_ge_4_0 (struct dwarf2_cu *cu)
4631 {
4632 const char *cs;
4633 int major, minor;
4634
4635 if (cu->producer == NULL)
4636 {
4637 /* For unknown compilers expect their behavior is not compliant. For GCC
4638 this case can also happen for -gdwarf-4 type units supported since
4639 gcc-4.5. */
4640
4641 return 0;
4642 }
4643
4644 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4645
4646 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4647 {
4648 /* For non-GCC compilers expect their behavior is not compliant. */
4649
4650 return 0;
4651 }
4652 cs = &cu->producer[strlen ("GNU ")];
4653 while (*cs && !isdigit (*cs))
4654 cs++;
4655 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4656 {
4657 /* Not recognized as GCC. */
4658
4659 return 0;
4660 }
4661
4662 return major >= 4;
4663 }
4664
4665 /* Generate full symbol information for PST and CU, whose DIEs have
4666 already been loaded into memory. */
4667
4668 static void
4669 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4670 {
4671 struct dwarf2_cu *cu = per_cu->cu;
4672 struct objfile *objfile = per_cu->objfile;
4673 CORE_ADDR lowpc, highpc;
4674 struct symtab *symtab;
4675 struct cleanup *back_to, *delayed_list_cleanup;
4676 CORE_ADDR baseaddr;
4677
4678 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4679
4680 buildsym_init ();
4681 back_to = make_cleanup (really_free_pendings, NULL);
4682 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4683
4684 cu->list_in_scope = &file_symbols;
4685
4686 dwarf2_find_base_address (cu->dies, cu);
4687
4688 /* Do line number decoding in read_file_scope () */
4689 process_die (cu->dies, cu);
4690
4691 /* Now that we have processed all the DIEs in the CU, all the types
4692 should be complete, and it should now be safe to compute all of the
4693 physnames. */
4694 compute_delayed_physnames (cu);
4695 do_cleanups (delayed_list_cleanup);
4696
4697 /* Some compilers don't define a DW_AT_high_pc attribute for the
4698 compilation unit. If the DW_AT_high_pc is missing, synthesize
4699 it, by scanning the DIE's below the compilation unit. */
4700 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4701
4702 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4703
4704 if (symtab != NULL)
4705 {
4706 /* Set symtab language to language from DW_AT_language. If the
4707 compilation is from a C file generated by language preprocessors, do
4708 not set the language if it was already deduced by start_subfile. */
4709 if (!(cu->language == language_c && symtab->language != language_c))
4710 symtab->language = cu->language;
4711
4712 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4713 produce DW_AT_location with location lists but it can be possibly
4714 invalid without -fvar-tracking.
4715
4716 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4717 needed, it would be wrong due to missing DW_AT_producer there.
4718
4719 Still one can confuse GDB by using non-standard GCC compilation
4720 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4721 */
4722 if (cu->has_loclist && producer_is_gcc_ge_4_0 (cu))
4723 symtab->locations_valid = 1;
4724 }
4725
4726 if (dwarf2_per_objfile->using_index)
4727 per_cu->v.quick->symtab = symtab;
4728 else
4729 {
4730 struct partial_symtab *pst = per_cu->v.psymtab;
4731 pst->symtab = symtab;
4732 pst->readin = 1;
4733 }
4734
4735 do_cleanups (back_to);
4736 }
4737
4738 /* Process a die and its children. */
4739
4740 static void
4741 process_die (struct die_info *die, struct dwarf2_cu *cu)
4742 {
4743 switch (die->tag)
4744 {
4745 case DW_TAG_padding:
4746 break;
4747 case DW_TAG_compile_unit:
4748 read_file_scope (die, cu);
4749 break;
4750 case DW_TAG_type_unit:
4751 read_type_unit_scope (die, cu);
4752 break;
4753 case DW_TAG_subprogram:
4754 case DW_TAG_inlined_subroutine:
4755 read_func_scope (die, cu);
4756 break;
4757 case DW_TAG_lexical_block:
4758 case DW_TAG_try_block:
4759 case DW_TAG_catch_block:
4760 read_lexical_block_scope (die, cu);
4761 break;
4762 case DW_TAG_class_type:
4763 case DW_TAG_interface_type:
4764 case DW_TAG_structure_type:
4765 case DW_TAG_union_type:
4766 process_structure_scope (die, cu);
4767 break;
4768 case DW_TAG_enumeration_type:
4769 process_enumeration_scope (die, cu);
4770 break;
4771
4772 /* These dies have a type, but processing them does not create
4773 a symbol or recurse to process the children. Therefore we can
4774 read them on-demand through read_type_die. */
4775 case DW_TAG_subroutine_type:
4776 case DW_TAG_set_type:
4777 case DW_TAG_array_type:
4778 case DW_TAG_pointer_type:
4779 case DW_TAG_ptr_to_member_type:
4780 case DW_TAG_reference_type:
4781 case DW_TAG_string_type:
4782 break;
4783
4784 case DW_TAG_base_type:
4785 case DW_TAG_subrange_type:
4786 case DW_TAG_typedef:
4787 /* Add a typedef symbol for the type definition, if it has a
4788 DW_AT_name. */
4789 new_symbol (die, read_type_die (die, cu), cu);
4790 break;
4791 case DW_TAG_common_block:
4792 read_common_block (die, cu);
4793 break;
4794 case DW_TAG_common_inclusion:
4795 break;
4796 case DW_TAG_namespace:
4797 processing_has_namespace_info = 1;
4798 read_namespace (die, cu);
4799 break;
4800 case DW_TAG_module:
4801 processing_has_namespace_info = 1;
4802 read_module (die, cu);
4803 break;
4804 case DW_TAG_imported_declaration:
4805 case DW_TAG_imported_module:
4806 processing_has_namespace_info = 1;
4807 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4808 || cu->language != language_fortran))
4809 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4810 dwarf_tag_name (die->tag));
4811 read_import_statement (die, cu);
4812 break;
4813 default:
4814 new_symbol (die, NULL, cu);
4815 break;
4816 }
4817 }
4818
4819 /* A helper function for dwarf2_compute_name which determines whether DIE
4820 needs to have the name of the scope prepended to the name listed in the
4821 die. */
4822
4823 static int
4824 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4825 {
4826 struct attribute *attr;
4827
4828 switch (die->tag)
4829 {
4830 case DW_TAG_namespace:
4831 case DW_TAG_typedef:
4832 case DW_TAG_class_type:
4833 case DW_TAG_interface_type:
4834 case DW_TAG_structure_type:
4835 case DW_TAG_union_type:
4836 case DW_TAG_enumeration_type:
4837 case DW_TAG_enumerator:
4838 case DW_TAG_subprogram:
4839 case DW_TAG_member:
4840 return 1;
4841
4842 case DW_TAG_variable:
4843 case DW_TAG_constant:
4844 /* We only need to prefix "globally" visible variables. These include
4845 any variable marked with DW_AT_external or any variable that
4846 lives in a namespace. [Variables in anonymous namespaces
4847 require prefixing, but they are not DW_AT_external.] */
4848
4849 if (dwarf2_attr (die, DW_AT_specification, cu))
4850 {
4851 struct dwarf2_cu *spec_cu = cu;
4852
4853 return die_needs_namespace (die_specification (die, &spec_cu),
4854 spec_cu);
4855 }
4856
4857 attr = dwarf2_attr (die, DW_AT_external, cu);
4858 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4859 && die->parent->tag != DW_TAG_module)
4860 return 0;
4861 /* A variable in a lexical block of some kind does not need a
4862 namespace, even though in C++ such variables may be external
4863 and have a mangled name. */
4864 if (die->parent->tag == DW_TAG_lexical_block
4865 || die->parent->tag == DW_TAG_try_block
4866 || die->parent->tag == DW_TAG_catch_block
4867 || die->parent->tag == DW_TAG_subprogram)
4868 return 0;
4869 return 1;
4870
4871 default:
4872 return 0;
4873 }
4874 }
4875
4876 /* Retrieve the last character from a mem_file. */
4877
4878 static void
4879 do_ui_file_peek_last (void *object, const char *buffer, long length)
4880 {
4881 char *last_char_p = (char *) object;
4882
4883 if (length > 0)
4884 *last_char_p = buffer[length - 1];
4885 }
4886
4887 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4888 compute the physname for the object, which include a method's
4889 formal parameters (C++/Java) and return type (Java).
4890
4891 For Ada, return the DIE's linkage name rather than the fully qualified
4892 name. PHYSNAME is ignored..
4893
4894 The result is allocated on the objfile_obstack and canonicalized. */
4895
4896 static const char *
4897 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4898 int physname)
4899 {
4900 if (name == NULL)
4901 name = dwarf2_name (die, cu);
4902
4903 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4904 compute it by typename_concat inside GDB. */
4905 if (cu->language == language_ada
4906 || (cu->language == language_fortran && physname))
4907 {
4908 /* For Ada unit, we prefer the linkage name over the name, as
4909 the former contains the exported name, which the user expects
4910 to be able to reference. Ideally, we want the user to be able
4911 to reference this entity using either natural or linkage name,
4912 but we haven't started looking at this enhancement yet. */
4913 struct attribute *attr;
4914
4915 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4916 if (attr == NULL)
4917 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4918 if (attr && DW_STRING (attr))
4919 return DW_STRING (attr);
4920 }
4921
4922 /* These are the only languages we know how to qualify names in. */
4923 if (name != NULL
4924 && (cu->language == language_cplus || cu->language == language_java
4925 || cu->language == language_fortran))
4926 {
4927 if (die_needs_namespace (die, cu))
4928 {
4929 long length;
4930 char *prefix;
4931 struct ui_file *buf;
4932
4933 prefix = determine_prefix (die, cu);
4934 buf = mem_fileopen ();
4935 if (*prefix != '\0')
4936 {
4937 char *prefixed_name = typename_concat (NULL, prefix, name,
4938 physname, cu);
4939
4940 fputs_unfiltered (prefixed_name, buf);
4941 xfree (prefixed_name);
4942 }
4943 else
4944 fputs_unfiltered (name, buf);
4945
4946 /* Template parameters may be specified in the DIE's DW_AT_name, or
4947 as children with DW_TAG_template_type_param or
4948 DW_TAG_value_type_param. If the latter, add them to the name
4949 here. If the name already has template parameters, then
4950 skip this step; some versions of GCC emit both, and
4951 it is more efficient to use the pre-computed name.
4952
4953 Something to keep in mind about this process: it is very
4954 unlikely, or in some cases downright impossible, to produce
4955 something that will match the mangled name of a function.
4956 If the definition of the function has the same debug info,
4957 we should be able to match up with it anyway. But fallbacks
4958 using the minimal symbol, for instance to find a method
4959 implemented in a stripped copy of libstdc++, will not work.
4960 If we do not have debug info for the definition, we will have to
4961 match them up some other way.
4962
4963 When we do name matching there is a related problem with function
4964 templates; two instantiated function templates are allowed to
4965 differ only by their return types, which we do not add here. */
4966
4967 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4968 {
4969 struct attribute *attr;
4970 struct die_info *child;
4971 int first = 1;
4972
4973 die->building_fullname = 1;
4974
4975 for (child = die->child; child != NULL; child = child->sibling)
4976 {
4977 struct type *type;
4978 long value;
4979 gdb_byte *bytes;
4980 struct dwarf2_locexpr_baton *baton;
4981 struct value *v;
4982
4983 if (child->tag != DW_TAG_template_type_param
4984 && child->tag != DW_TAG_template_value_param)
4985 continue;
4986
4987 if (first)
4988 {
4989 fputs_unfiltered ("<", buf);
4990 first = 0;
4991 }
4992 else
4993 fputs_unfiltered (", ", buf);
4994
4995 attr = dwarf2_attr (child, DW_AT_type, cu);
4996 if (attr == NULL)
4997 {
4998 complaint (&symfile_complaints,
4999 _("template parameter missing DW_AT_type"));
5000 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5001 continue;
5002 }
5003 type = die_type (child, cu);
5004
5005 if (child->tag == DW_TAG_template_type_param)
5006 {
5007 c_print_type (type, "", buf, -1, 0);
5008 continue;
5009 }
5010
5011 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5012 if (attr == NULL)
5013 {
5014 complaint (&symfile_complaints,
5015 _("template parameter missing "
5016 "DW_AT_const_value"));
5017 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5018 continue;
5019 }
5020
5021 dwarf2_const_value_attr (attr, type, name,
5022 &cu->comp_unit_obstack, cu,
5023 &value, &bytes, &baton);
5024
5025 if (TYPE_NOSIGN (type))
5026 /* GDB prints characters as NUMBER 'CHAR'. If that's
5027 changed, this can use value_print instead. */
5028 c_printchar (value, type, buf);
5029 else
5030 {
5031 struct value_print_options opts;
5032
5033 if (baton != NULL)
5034 v = dwarf2_evaluate_loc_desc (type, NULL,
5035 baton->data,
5036 baton->size,
5037 baton->per_cu);
5038 else if (bytes != NULL)
5039 {
5040 v = allocate_value (type);
5041 memcpy (value_contents_writeable (v), bytes,
5042 TYPE_LENGTH (type));
5043 }
5044 else
5045 v = value_from_longest (type, value);
5046
5047 /* Specify decimal so that we do not depend on
5048 the radix. */
5049 get_formatted_print_options (&opts, 'd');
5050 opts.raw = 1;
5051 value_print (v, buf, &opts);
5052 release_value (v);
5053 value_free (v);
5054 }
5055 }
5056
5057 die->building_fullname = 0;
5058
5059 if (!first)
5060 {
5061 /* Close the argument list, with a space if necessary
5062 (nested templates). */
5063 char last_char = '\0';
5064 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5065 if (last_char == '>')
5066 fputs_unfiltered (" >", buf);
5067 else
5068 fputs_unfiltered (">", buf);
5069 }
5070 }
5071
5072 /* For Java and C++ methods, append formal parameter type
5073 information, if PHYSNAME. */
5074
5075 if (physname && die->tag == DW_TAG_subprogram
5076 && (cu->language == language_cplus
5077 || cu->language == language_java))
5078 {
5079 struct type *type = read_type_die (die, cu);
5080
5081 c_type_print_args (type, buf, 1, cu->language);
5082
5083 if (cu->language == language_java)
5084 {
5085 /* For java, we must append the return type to method
5086 names. */
5087 if (die->tag == DW_TAG_subprogram)
5088 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5089 0, 0);
5090 }
5091 else if (cu->language == language_cplus)
5092 {
5093 /* Assume that an artificial first parameter is
5094 "this", but do not crash if it is not. RealView
5095 marks unnamed (and thus unused) parameters as
5096 artificial; there is no way to differentiate
5097 the two cases. */
5098 if (TYPE_NFIELDS (type) > 0
5099 && TYPE_FIELD_ARTIFICIAL (type, 0)
5100 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5101 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5102 0))))
5103 fputs_unfiltered (" const", buf);
5104 }
5105 }
5106
5107 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5108 &length);
5109 ui_file_delete (buf);
5110
5111 if (cu->language == language_cplus)
5112 {
5113 char *cname
5114 = dwarf2_canonicalize_name (name, cu,
5115 &cu->objfile->objfile_obstack);
5116
5117 if (cname != NULL)
5118 name = cname;
5119 }
5120 }
5121 }
5122
5123 return name;
5124 }
5125
5126 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5127 If scope qualifiers are appropriate they will be added. The result
5128 will be allocated on the objfile_obstack, or NULL if the DIE does
5129 not have a name. NAME may either be from a previous call to
5130 dwarf2_name or NULL.
5131
5132 The output string will be canonicalized (if C++/Java). */
5133
5134 static const char *
5135 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5136 {
5137 return dwarf2_compute_name (name, die, cu, 0);
5138 }
5139
5140 /* Construct a physname for the given DIE in CU. NAME may either be
5141 from a previous call to dwarf2_name or NULL. The result will be
5142 allocated on the objfile_objstack or NULL if the DIE does not have a
5143 name.
5144
5145 The output string will be canonicalized (if C++/Java). */
5146
5147 static const char *
5148 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5149 {
5150 return dwarf2_compute_name (name, die, cu, 1);
5151 }
5152
5153 /* Read the import statement specified by the given die and record it. */
5154
5155 static void
5156 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5157 {
5158 struct attribute *import_attr;
5159 struct die_info *imported_die;
5160 struct dwarf2_cu *imported_cu;
5161 const char *imported_name;
5162 const char *imported_name_prefix;
5163 const char *canonical_name;
5164 const char *import_alias;
5165 const char *imported_declaration = NULL;
5166 const char *import_prefix;
5167
5168 char *temp;
5169
5170 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5171 if (import_attr == NULL)
5172 {
5173 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5174 dwarf_tag_name (die->tag));
5175 return;
5176 }
5177
5178 imported_cu = cu;
5179 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5180 imported_name = dwarf2_name (imported_die, imported_cu);
5181 if (imported_name == NULL)
5182 {
5183 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5184
5185 The import in the following code:
5186 namespace A
5187 {
5188 typedef int B;
5189 }
5190
5191 int main ()
5192 {
5193 using A::B;
5194 B b;
5195 return b;
5196 }
5197
5198 ...
5199 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5200 <52> DW_AT_decl_file : 1
5201 <53> DW_AT_decl_line : 6
5202 <54> DW_AT_import : <0x75>
5203 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5204 <59> DW_AT_name : B
5205 <5b> DW_AT_decl_file : 1
5206 <5c> DW_AT_decl_line : 2
5207 <5d> DW_AT_type : <0x6e>
5208 ...
5209 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5210 <76> DW_AT_byte_size : 4
5211 <77> DW_AT_encoding : 5 (signed)
5212
5213 imports the wrong die ( 0x75 instead of 0x58 ).
5214 This case will be ignored until the gcc bug is fixed. */
5215 return;
5216 }
5217
5218 /* Figure out the local name after import. */
5219 import_alias = dwarf2_name (die, cu);
5220
5221 /* Figure out where the statement is being imported to. */
5222 import_prefix = determine_prefix (die, cu);
5223
5224 /* Figure out what the scope of the imported die is and prepend it
5225 to the name of the imported die. */
5226 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5227
5228 if (imported_die->tag != DW_TAG_namespace
5229 && imported_die->tag != DW_TAG_module)
5230 {
5231 imported_declaration = imported_name;
5232 canonical_name = imported_name_prefix;
5233 }
5234 else if (strlen (imported_name_prefix) > 0)
5235 {
5236 temp = alloca (strlen (imported_name_prefix)
5237 + 2 + strlen (imported_name) + 1);
5238 strcpy (temp, imported_name_prefix);
5239 strcat (temp, "::");
5240 strcat (temp, imported_name);
5241 canonical_name = temp;
5242 }
5243 else
5244 canonical_name = imported_name;
5245
5246 cp_add_using_directive (import_prefix,
5247 canonical_name,
5248 import_alias,
5249 imported_declaration,
5250 &cu->objfile->objfile_obstack);
5251 }
5252
5253 static void
5254 initialize_cu_func_list (struct dwarf2_cu *cu)
5255 {
5256 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5257 }
5258
5259 /* Cleanup function for read_file_scope. */
5260
5261 static void
5262 free_cu_line_header (void *arg)
5263 {
5264 struct dwarf2_cu *cu = arg;
5265
5266 free_line_header (cu->line_header);
5267 cu->line_header = NULL;
5268 }
5269
5270 static void
5271 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5272 char **name, char **comp_dir)
5273 {
5274 struct attribute *attr;
5275
5276 *name = NULL;
5277 *comp_dir = NULL;
5278
5279 /* Find the filename. Do not use dwarf2_name here, since the filename
5280 is not a source language identifier. */
5281 attr = dwarf2_attr (die, DW_AT_name, cu);
5282 if (attr)
5283 {
5284 *name = DW_STRING (attr);
5285 }
5286
5287 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5288 if (attr)
5289 *comp_dir = DW_STRING (attr);
5290 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5291 {
5292 *comp_dir = ldirname (*name);
5293 if (*comp_dir != NULL)
5294 make_cleanup (xfree, *comp_dir);
5295 }
5296 if (*comp_dir != NULL)
5297 {
5298 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5299 directory, get rid of it. */
5300 char *cp = strchr (*comp_dir, ':');
5301
5302 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5303 *comp_dir = cp + 1;
5304 }
5305
5306 if (*name == NULL)
5307 *name = "<unknown>";
5308 }
5309
5310 /* Process DW_TAG_compile_unit. */
5311
5312 static void
5313 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5314 {
5315 struct objfile *objfile = cu->objfile;
5316 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5317 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5318 CORE_ADDR highpc = ((CORE_ADDR) 0);
5319 struct attribute *attr;
5320 char *name = NULL;
5321 char *comp_dir = NULL;
5322 struct die_info *child_die;
5323 bfd *abfd = objfile->obfd;
5324 struct line_header *line_header = 0;
5325 CORE_ADDR baseaddr;
5326
5327 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5328
5329 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5330
5331 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5332 from finish_block. */
5333 if (lowpc == ((CORE_ADDR) -1))
5334 lowpc = highpc;
5335 lowpc += baseaddr;
5336 highpc += baseaddr;
5337
5338 find_file_and_directory (die, cu, &name, &comp_dir);
5339
5340 attr = dwarf2_attr (die, DW_AT_language, cu);
5341 if (attr)
5342 {
5343 set_cu_language (DW_UNSND (attr), cu);
5344 }
5345
5346 attr = dwarf2_attr (die, DW_AT_producer, cu);
5347 if (attr)
5348 cu->producer = DW_STRING (attr);
5349
5350 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5351 standardised yet. As a workaround for the language detection we fall
5352 back to the DW_AT_producer string. */
5353 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5354 cu->language = language_opencl;
5355
5356 /* We assume that we're processing GCC output. */
5357 processing_gcc_compilation = 2;
5358
5359 processing_has_namespace_info = 0;
5360
5361 start_symtab (name, comp_dir, lowpc);
5362 record_debugformat ("DWARF 2");
5363 record_producer (cu->producer);
5364
5365 initialize_cu_func_list (cu);
5366
5367 /* Decode line number information if present. We do this before
5368 processing child DIEs, so that the line header table is available
5369 for DW_AT_decl_file. */
5370 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5371 if (attr)
5372 {
5373 unsigned int line_offset = DW_UNSND (attr);
5374 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5375 if (line_header)
5376 {
5377 cu->line_header = line_header;
5378 make_cleanup (free_cu_line_header, cu);
5379 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5380 }
5381 }
5382
5383 /* Process all dies in compilation unit. */
5384 if (die->child != NULL)
5385 {
5386 child_die = die->child;
5387 while (child_die && child_die->tag)
5388 {
5389 process_die (child_die, cu);
5390 child_die = sibling_die (child_die);
5391 }
5392 }
5393
5394 /* Decode macro information, if present. Dwarf 2 macro information
5395 refers to information in the line number info statement program
5396 header, so we can only read it if we've read the header
5397 successfully. */
5398 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5399 if (attr && line_header)
5400 {
5401 unsigned int macro_offset = DW_UNSND (attr);
5402
5403 dwarf_decode_macros (line_header, macro_offset,
5404 comp_dir, abfd, cu);
5405 }
5406 do_cleanups (back_to);
5407 }
5408
5409 /* Process DW_TAG_type_unit.
5410 For TUs we want to skip the first top level sibling if it's not the
5411 actual type being defined by this TU. In this case the first top
5412 level sibling is there to provide context only. */
5413
5414 static void
5415 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5416 {
5417 struct objfile *objfile = cu->objfile;
5418 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5419 CORE_ADDR lowpc;
5420 struct attribute *attr;
5421 char *name = NULL;
5422 char *comp_dir = NULL;
5423 struct die_info *child_die;
5424 bfd *abfd = objfile->obfd;
5425
5426 /* start_symtab needs a low pc, but we don't really have one.
5427 Do what read_file_scope would do in the absence of such info. */
5428 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5429
5430 /* Find the filename. Do not use dwarf2_name here, since the filename
5431 is not a source language identifier. */
5432 attr = dwarf2_attr (die, DW_AT_name, cu);
5433 if (attr)
5434 name = DW_STRING (attr);
5435
5436 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5437 if (attr)
5438 comp_dir = DW_STRING (attr);
5439 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5440 {
5441 comp_dir = ldirname (name);
5442 if (comp_dir != NULL)
5443 make_cleanup (xfree, comp_dir);
5444 }
5445
5446 if (name == NULL)
5447 name = "<unknown>";
5448
5449 attr = dwarf2_attr (die, DW_AT_language, cu);
5450 if (attr)
5451 set_cu_language (DW_UNSND (attr), cu);
5452
5453 /* This isn't technically needed today. It is done for symmetry
5454 with read_file_scope. */
5455 attr = dwarf2_attr (die, DW_AT_producer, cu);
5456 if (attr)
5457 cu->producer = DW_STRING (attr);
5458
5459 /* We assume that we're processing GCC output. */
5460 processing_gcc_compilation = 2;
5461
5462 processing_has_namespace_info = 0;
5463
5464 start_symtab (name, comp_dir, lowpc);
5465 record_debugformat ("DWARF 2");
5466 record_producer (cu->producer);
5467
5468 /* Process the dies in the type unit. */
5469 if (die->child == NULL)
5470 {
5471 dump_die_for_error (die);
5472 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5473 bfd_get_filename (abfd));
5474 }
5475
5476 child_die = die->child;
5477
5478 while (child_die && child_die->tag)
5479 {
5480 process_die (child_die, cu);
5481
5482 child_die = sibling_die (child_die);
5483 }
5484
5485 do_cleanups (back_to);
5486 }
5487
5488 static void
5489 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5490 struct dwarf2_cu *cu)
5491 {
5492 struct function_range *thisfn;
5493
5494 thisfn = (struct function_range *)
5495 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5496 thisfn->name = name;
5497 thisfn->lowpc = lowpc;
5498 thisfn->highpc = highpc;
5499 thisfn->seen_line = 0;
5500 thisfn->next = NULL;
5501
5502 if (cu->last_fn == NULL)
5503 cu->first_fn = thisfn;
5504 else
5505 cu->last_fn->next = thisfn;
5506
5507 cu->last_fn = thisfn;
5508 }
5509
5510 /* qsort helper for inherit_abstract_dies. */
5511
5512 static int
5513 unsigned_int_compar (const void *ap, const void *bp)
5514 {
5515 unsigned int a = *(unsigned int *) ap;
5516 unsigned int b = *(unsigned int *) bp;
5517
5518 return (a > b) - (b > a);
5519 }
5520
5521 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5522 Inherit only the children of the DW_AT_abstract_origin DIE not being
5523 already referenced by DW_AT_abstract_origin from the children of the
5524 current DIE. */
5525
5526 static void
5527 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5528 {
5529 struct die_info *child_die;
5530 unsigned die_children_count;
5531 /* CU offsets which were referenced by children of the current DIE. */
5532 unsigned *offsets;
5533 unsigned *offsets_end, *offsetp;
5534 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5535 struct die_info *origin_die;
5536 /* Iterator of the ORIGIN_DIE children. */
5537 struct die_info *origin_child_die;
5538 struct cleanup *cleanups;
5539 struct attribute *attr;
5540 struct dwarf2_cu *origin_cu;
5541 struct pending **origin_previous_list_in_scope;
5542
5543 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5544 if (!attr)
5545 return;
5546
5547 /* Note that following die references may follow to a die in a
5548 different cu. */
5549
5550 origin_cu = cu;
5551 origin_die = follow_die_ref (die, attr, &origin_cu);
5552
5553 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5554 symbols in. */
5555 origin_previous_list_in_scope = origin_cu->list_in_scope;
5556 origin_cu->list_in_scope = cu->list_in_scope;
5557
5558 if (die->tag != origin_die->tag
5559 && !(die->tag == DW_TAG_inlined_subroutine
5560 && origin_die->tag == DW_TAG_subprogram))
5561 complaint (&symfile_complaints,
5562 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5563 die->offset, origin_die->offset);
5564
5565 child_die = die->child;
5566 die_children_count = 0;
5567 while (child_die && child_die->tag)
5568 {
5569 child_die = sibling_die (child_die);
5570 die_children_count++;
5571 }
5572 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5573 cleanups = make_cleanup (xfree, offsets);
5574
5575 offsets_end = offsets;
5576 child_die = die->child;
5577 while (child_die && child_die->tag)
5578 {
5579 /* For each CHILD_DIE, find the corresponding child of
5580 ORIGIN_DIE. If there is more than one layer of
5581 DW_AT_abstract_origin, follow them all; there shouldn't be,
5582 but GCC versions at least through 4.4 generate this (GCC PR
5583 40573). */
5584 struct die_info *child_origin_die = child_die;
5585 struct dwarf2_cu *child_origin_cu = cu;
5586
5587 while (1)
5588 {
5589 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5590 child_origin_cu);
5591 if (attr == NULL)
5592 break;
5593 child_origin_die = follow_die_ref (child_origin_die, attr,
5594 &child_origin_cu);
5595 }
5596
5597 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5598 counterpart may exist. */
5599 if (child_origin_die != child_die)
5600 {
5601 if (child_die->tag != child_origin_die->tag
5602 && !(child_die->tag == DW_TAG_inlined_subroutine
5603 && child_origin_die->tag == DW_TAG_subprogram))
5604 complaint (&symfile_complaints,
5605 _("Child DIE 0x%x and its abstract origin 0x%x have "
5606 "different tags"), child_die->offset,
5607 child_origin_die->offset);
5608 if (child_origin_die->parent != origin_die)
5609 complaint (&symfile_complaints,
5610 _("Child DIE 0x%x and its abstract origin 0x%x have "
5611 "different parents"), child_die->offset,
5612 child_origin_die->offset);
5613 else
5614 *offsets_end++ = child_origin_die->offset;
5615 }
5616 child_die = sibling_die (child_die);
5617 }
5618 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5619 unsigned_int_compar);
5620 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5621 if (offsetp[-1] == *offsetp)
5622 complaint (&symfile_complaints,
5623 _("Multiple children of DIE 0x%x refer "
5624 "to DIE 0x%x as their abstract origin"),
5625 die->offset, *offsetp);
5626
5627 offsetp = offsets;
5628 origin_child_die = origin_die->child;
5629 while (origin_child_die && origin_child_die->tag)
5630 {
5631 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5632 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5633 offsetp++;
5634 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5635 {
5636 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5637 process_die (origin_child_die, origin_cu);
5638 }
5639 origin_child_die = sibling_die (origin_child_die);
5640 }
5641 origin_cu->list_in_scope = origin_previous_list_in_scope;
5642
5643 do_cleanups (cleanups);
5644 }
5645
5646 static void
5647 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5648 {
5649 struct objfile *objfile = cu->objfile;
5650 struct context_stack *new;
5651 CORE_ADDR lowpc;
5652 CORE_ADDR highpc;
5653 struct die_info *child_die;
5654 struct attribute *attr, *call_line, *call_file;
5655 char *name;
5656 CORE_ADDR baseaddr;
5657 struct block *block;
5658 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5659 VEC (symbolp) *template_args = NULL;
5660 struct template_symbol *templ_func = NULL;
5661
5662 if (inlined_func)
5663 {
5664 /* If we do not have call site information, we can't show the
5665 caller of this inlined function. That's too confusing, so
5666 only use the scope for local variables. */
5667 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5668 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5669 if (call_line == NULL || call_file == NULL)
5670 {
5671 read_lexical_block_scope (die, cu);
5672 return;
5673 }
5674 }
5675
5676 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5677
5678 name = dwarf2_name (die, cu);
5679
5680 /* Ignore functions with missing or empty names. These are actually
5681 illegal according to the DWARF standard. */
5682 if (name == NULL)
5683 {
5684 complaint (&symfile_complaints,
5685 _("missing name for subprogram DIE at %d"), die->offset);
5686 return;
5687 }
5688
5689 /* Ignore functions with missing or invalid low and high pc attributes. */
5690 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5691 {
5692 attr = dwarf2_attr (die, DW_AT_external, cu);
5693 if (!attr || !DW_UNSND (attr))
5694 complaint (&symfile_complaints,
5695 _("cannot get low and high bounds "
5696 "for subprogram DIE at %d"),
5697 die->offset);
5698 return;
5699 }
5700
5701 lowpc += baseaddr;
5702 highpc += baseaddr;
5703
5704 /* Record the function range for dwarf_decode_lines. */
5705 add_to_cu_func_list (name, lowpc, highpc, cu);
5706
5707 /* If we have any template arguments, then we must allocate a
5708 different sort of symbol. */
5709 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5710 {
5711 if (child_die->tag == DW_TAG_template_type_param
5712 || child_die->tag == DW_TAG_template_value_param)
5713 {
5714 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5715 struct template_symbol);
5716 templ_func->base.is_cplus_template_function = 1;
5717 break;
5718 }
5719 }
5720
5721 new = push_context (0, lowpc);
5722 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5723 (struct symbol *) templ_func);
5724
5725 /* If there is a location expression for DW_AT_frame_base, record
5726 it. */
5727 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5728 if (attr)
5729 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5730 expression is being recorded directly in the function's symbol
5731 and not in a separate frame-base object. I guess this hack is
5732 to avoid adding some sort of frame-base adjunct/annex to the
5733 function's symbol :-(. The problem with doing this is that it
5734 results in a function symbol with a location expression that
5735 has nothing to do with the location of the function, ouch! The
5736 relationship should be: a function's symbol has-a frame base; a
5737 frame-base has-a location expression. */
5738 dwarf2_symbol_mark_computed (attr, new->name, cu);
5739
5740 cu->list_in_scope = &local_symbols;
5741
5742 if (die->child != NULL)
5743 {
5744 child_die = die->child;
5745 while (child_die && child_die->tag)
5746 {
5747 if (child_die->tag == DW_TAG_template_type_param
5748 || child_die->tag == DW_TAG_template_value_param)
5749 {
5750 struct symbol *arg = new_symbol (child_die, NULL, cu);
5751
5752 if (arg != NULL)
5753 VEC_safe_push (symbolp, template_args, arg);
5754 }
5755 else
5756 process_die (child_die, cu);
5757 child_die = sibling_die (child_die);
5758 }
5759 }
5760
5761 inherit_abstract_dies (die, cu);
5762
5763 /* If we have a DW_AT_specification, we might need to import using
5764 directives from the context of the specification DIE. See the
5765 comment in determine_prefix. */
5766 if (cu->language == language_cplus
5767 && dwarf2_attr (die, DW_AT_specification, cu))
5768 {
5769 struct dwarf2_cu *spec_cu = cu;
5770 struct die_info *spec_die = die_specification (die, &spec_cu);
5771
5772 while (spec_die)
5773 {
5774 child_die = spec_die->child;
5775 while (child_die && child_die->tag)
5776 {
5777 if (child_die->tag == DW_TAG_imported_module)
5778 process_die (child_die, spec_cu);
5779 child_die = sibling_die (child_die);
5780 }
5781
5782 /* In some cases, GCC generates specification DIEs that
5783 themselves contain DW_AT_specification attributes. */
5784 spec_die = die_specification (spec_die, &spec_cu);
5785 }
5786 }
5787
5788 new = pop_context ();
5789 /* Make a block for the local symbols within. */
5790 block = finish_block (new->name, &local_symbols, new->old_blocks,
5791 lowpc, highpc, objfile);
5792
5793 /* For C++, set the block's scope. */
5794 if (cu->language == language_cplus || cu->language == language_fortran)
5795 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5796 determine_prefix (die, cu),
5797 processing_has_namespace_info);
5798
5799 /* If we have address ranges, record them. */
5800 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5801
5802 /* Attach template arguments to function. */
5803 if (! VEC_empty (symbolp, template_args))
5804 {
5805 gdb_assert (templ_func != NULL);
5806
5807 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5808 templ_func->template_arguments
5809 = obstack_alloc (&objfile->objfile_obstack,
5810 (templ_func->n_template_arguments
5811 * sizeof (struct symbol *)));
5812 memcpy (templ_func->template_arguments,
5813 VEC_address (symbolp, template_args),
5814 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5815 VEC_free (symbolp, template_args);
5816 }
5817
5818 /* In C++, we can have functions nested inside functions (e.g., when
5819 a function declares a class that has methods). This means that
5820 when we finish processing a function scope, we may need to go
5821 back to building a containing block's symbol lists. */
5822 local_symbols = new->locals;
5823 param_symbols = new->params;
5824 using_directives = new->using_directives;
5825
5826 /* If we've finished processing a top-level function, subsequent
5827 symbols go in the file symbol list. */
5828 if (outermost_context_p ())
5829 cu->list_in_scope = &file_symbols;
5830 }
5831
5832 /* Process all the DIES contained within a lexical block scope. Start
5833 a new scope, process the dies, and then close the scope. */
5834
5835 static void
5836 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5837 {
5838 struct objfile *objfile = cu->objfile;
5839 struct context_stack *new;
5840 CORE_ADDR lowpc, highpc;
5841 struct die_info *child_die;
5842 CORE_ADDR baseaddr;
5843
5844 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5845
5846 /* Ignore blocks with missing or invalid low and high pc attributes. */
5847 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5848 as multiple lexical blocks? Handling children in a sane way would
5849 be nasty. Might be easier to properly extend generic blocks to
5850 describe ranges. */
5851 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5852 return;
5853 lowpc += baseaddr;
5854 highpc += baseaddr;
5855
5856 push_context (0, lowpc);
5857 if (die->child != NULL)
5858 {
5859 child_die = die->child;
5860 while (child_die && child_die->tag)
5861 {
5862 process_die (child_die, cu);
5863 child_die = sibling_die (child_die);
5864 }
5865 }
5866 new = pop_context ();
5867
5868 if (local_symbols != NULL || using_directives != NULL)
5869 {
5870 struct block *block
5871 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5872 highpc, objfile);
5873
5874 /* Note that recording ranges after traversing children, as we
5875 do here, means that recording a parent's ranges entails
5876 walking across all its children's ranges as they appear in
5877 the address map, which is quadratic behavior.
5878
5879 It would be nicer to record the parent's ranges before
5880 traversing its children, simply overriding whatever you find
5881 there. But since we don't even decide whether to create a
5882 block until after we've traversed its children, that's hard
5883 to do. */
5884 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5885 }
5886 local_symbols = new->locals;
5887 using_directives = new->using_directives;
5888 }
5889
5890 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5891 Return 1 if the attributes are present and valid, otherwise, return 0.
5892 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
5893
5894 static int
5895 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5896 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5897 struct partial_symtab *ranges_pst)
5898 {
5899 struct objfile *objfile = cu->objfile;
5900 struct comp_unit_head *cu_header = &cu->header;
5901 bfd *obfd = objfile->obfd;
5902 unsigned int addr_size = cu_header->addr_size;
5903 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5904 /* Base address selection entry. */
5905 CORE_ADDR base;
5906 int found_base;
5907 unsigned int dummy;
5908 gdb_byte *buffer;
5909 CORE_ADDR marker;
5910 int low_set;
5911 CORE_ADDR low = 0;
5912 CORE_ADDR high = 0;
5913 CORE_ADDR baseaddr;
5914
5915 found_base = cu->base_known;
5916 base = cu->base_address;
5917
5918 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5919 if (offset >= dwarf2_per_objfile->ranges.size)
5920 {
5921 complaint (&symfile_complaints,
5922 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5923 offset);
5924 return 0;
5925 }
5926 buffer = dwarf2_per_objfile->ranges.buffer + offset;
5927
5928 /* Read in the largest possible address. */
5929 marker = read_address (obfd, buffer, cu, &dummy);
5930 if ((marker & mask) == mask)
5931 {
5932 /* If we found the largest possible address, then
5933 read the base address. */
5934 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5935 buffer += 2 * addr_size;
5936 offset += 2 * addr_size;
5937 found_base = 1;
5938 }
5939
5940 low_set = 0;
5941
5942 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5943
5944 while (1)
5945 {
5946 CORE_ADDR range_beginning, range_end;
5947
5948 range_beginning = read_address (obfd, buffer, cu, &dummy);
5949 buffer += addr_size;
5950 range_end = read_address (obfd, buffer, cu, &dummy);
5951 buffer += addr_size;
5952 offset += 2 * addr_size;
5953
5954 /* An end of list marker is a pair of zero addresses. */
5955 if (range_beginning == 0 && range_end == 0)
5956 /* Found the end of list entry. */
5957 break;
5958
5959 /* Each base address selection entry is a pair of 2 values.
5960 The first is the largest possible address, the second is
5961 the base address. Check for a base address here. */
5962 if ((range_beginning & mask) == mask)
5963 {
5964 /* If we found the largest possible address, then
5965 read the base address. */
5966 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5967 found_base = 1;
5968 continue;
5969 }
5970
5971 if (!found_base)
5972 {
5973 /* We have no valid base address for the ranges
5974 data. */
5975 complaint (&symfile_complaints,
5976 _("Invalid .debug_ranges data (no base address)"));
5977 return 0;
5978 }
5979
5980 if (range_beginning > range_end)
5981 {
5982 /* Inverted range entries are invalid. */
5983 complaint (&symfile_complaints,
5984 _("Invalid .debug_ranges data (inverted range)"));
5985 return 0;
5986 }
5987
5988 /* Empty range entries have no effect. */
5989 if (range_beginning == range_end)
5990 continue;
5991
5992 range_beginning += base;
5993 range_end += base;
5994
5995 if (ranges_pst != NULL)
5996 addrmap_set_empty (objfile->psymtabs_addrmap,
5997 range_beginning + baseaddr,
5998 range_end - 1 + baseaddr,
5999 ranges_pst);
6000
6001 /* FIXME: This is recording everything as a low-high
6002 segment of consecutive addresses. We should have a
6003 data structure for discontiguous block ranges
6004 instead. */
6005 if (! low_set)
6006 {
6007 low = range_beginning;
6008 high = range_end;
6009 low_set = 1;
6010 }
6011 else
6012 {
6013 if (range_beginning < low)
6014 low = range_beginning;
6015 if (range_end > high)
6016 high = range_end;
6017 }
6018 }
6019
6020 if (! low_set)
6021 /* If the first entry is an end-of-list marker, the range
6022 describes an empty scope, i.e. no instructions. */
6023 return 0;
6024
6025 if (low_return)
6026 *low_return = low;
6027 if (high_return)
6028 *high_return = high;
6029 return 1;
6030 }
6031
6032 /* Get low and high pc attributes from a die. Return 1 if the attributes
6033 are present and valid, otherwise, return 0. Return -1 if the range is
6034 discontinuous, i.e. derived from DW_AT_ranges information. */
6035 static int
6036 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6037 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6038 struct partial_symtab *pst)
6039 {
6040 struct attribute *attr;
6041 CORE_ADDR low = 0;
6042 CORE_ADDR high = 0;
6043 int ret = 0;
6044
6045 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6046 if (attr)
6047 {
6048 high = DW_ADDR (attr);
6049 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6050 if (attr)
6051 low = DW_ADDR (attr);
6052 else
6053 /* Found high w/o low attribute. */
6054 return 0;
6055
6056 /* Found consecutive range of addresses. */
6057 ret = 1;
6058 }
6059 else
6060 {
6061 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6062 if (attr != NULL)
6063 {
6064 /* Value of the DW_AT_ranges attribute is the offset in the
6065 .debug_ranges section. */
6066 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6067 return 0;
6068 /* Found discontinuous range of addresses. */
6069 ret = -1;
6070 }
6071 }
6072
6073 /* read_partial_die has also the strict LOW < HIGH requirement. */
6074 if (high <= low)
6075 return 0;
6076
6077 /* When using the GNU linker, .gnu.linkonce. sections are used to
6078 eliminate duplicate copies of functions and vtables and such.
6079 The linker will arbitrarily choose one and discard the others.
6080 The AT_*_pc values for such functions refer to local labels in
6081 these sections. If the section from that file was discarded, the
6082 labels are not in the output, so the relocs get a value of 0.
6083 If this is a discarded function, mark the pc bounds as invalid,
6084 so that GDB will ignore it. */
6085 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6086 return 0;
6087
6088 *lowpc = low;
6089 *highpc = high;
6090 return ret;
6091 }
6092
6093 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6094 its low and high PC addresses. Do nothing if these addresses could not
6095 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6096 and HIGHPC to the high address if greater than HIGHPC. */
6097
6098 static void
6099 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6100 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6101 struct dwarf2_cu *cu)
6102 {
6103 CORE_ADDR low, high;
6104 struct die_info *child = die->child;
6105
6106 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6107 {
6108 *lowpc = min (*lowpc, low);
6109 *highpc = max (*highpc, high);
6110 }
6111
6112 /* If the language does not allow nested subprograms (either inside
6113 subprograms or lexical blocks), we're done. */
6114 if (cu->language != language_ada)
6115 return;
6116
6117 /* Check all the children of the given DIE. If it contains nested
6118 subprograms, then check their pc bounds. Likewise, we need to
6119 check lexical blocks as well, as they may also contain subprogram
6120 definitions. */
6121 while (child && child->tag)
6122 {
6123 if (child->tag == DW_TAG_subprogram
6124 || child->tag == DW_TAG_lexical_block)
6125 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6126 child = sibling_die (child);
6127 }
6128 }
6129
6130 /* Get the low and high pc's represented by the scope DIE, and store
6131 them in *LOWPC and *HIGHPC. If the correct values can't be
6132 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6133
6134 static void
6135 get_scope_pc_bounds (struct die_info *die,
6136 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6137 struct dwarf2_cu *cu)
6138 {
6139 CORE_ADDR best_low = (CORE_ADDR) -1;
6140 CORE_ADDR best_high = (CORE_ADDR) 0;
6141 CORE_ADDR current_low, current_high;
6142
6143 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6144 {
6145 best_low = current_low;
6146 best_high = current_high;
6147 }
6148 else
6149 {
6150 struct die_info *child = die->child;
6151
6152 while (child && child->tag)
6153 {
6154 switch (child->tag) {
6155 case DW_TAG_subprogram:
6156 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6157 break;
6158 case DW_TAG_namespace:
6159 case DW_TAG_module:
6160 /* FIXME: carlton/2004-01-16: Should we do this for
6161 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6162 that current GCC's always emit the DIEs corresponding
6163 to definitions of methods of classes as children of a
6164 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6165 the DIEs giving the declarations, which could be
6166 anywhere). But I don't see any reason why the
6167 standards says that they have to be there. */
6168 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6169
6170 if (current_low != ((CORE_ADDR) -1))
6171 {
6172 best_low = min (best_low, current_low);
6173 best_high = max (best_high, current_high);
6174 }
6175 break;
6176 default:
6177 /* Ignore. */
6178 break;
6179 }
6180
6181 child = sibling_die (child);
6182 }
6183 }
6184
6185 *lowpc = best_low;
6186 *highpc = best_high;
6187 }
6188
6189 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6190 in DIE. */
6191 static void
6192 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6193 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6194 {
6195 struct attribute *attr;
6196
6197 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6198 if (attr)
6199 {
6200 CORE_ADDR high = DW_ADDR (attr);
6201
6202 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6203 if (attr)
6204 {
6205 CORE_ADDR low = DW_ADDR (attr);
6206
6207 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6208 }
6209 }
6210
6211 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6212 if (attr)
6213 {
6214 bfd *obfd = cu->objfile->obfd;
6215
6216 /* The value of the DW_AT_ranges attribute is the offset of the
6217 address range list in the .debug_ranges section. */
6218 unsigned long offset = DW_UNSND (attr);
6219 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6220
6221 /* For some target architectures, but not others, the
6222 read_address function sign-extends the addresses it returns.
6223 To recognize base address selection entries, we need a
6224 mask. */
6225 unsigned int addr_size = cu->header.addr_size;
6226 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6227
6228 /* The base address, to which the next pair is relative. Note
6229 that this 'base' is a DWARF concept: most entries in a range
6230 list are relative, to reduce the number of relocs against the
6231 debugging information. This is separate from this function's
6232 'baseaddr' argument, which GDB uses to relocate debugging
6233 information from a shared library based on the address at
6234 which the library was loaded. */
6235 CORE_ADDR base = cu->base_address;
6236 int base_known = cu->base_known;
6237
6238 gdb_assert (dwarf2_per_objfile->ranges.readin);
6239 if (offset >= dwarf2_per_objfile->ranges.size)
6240 {
6241 complaint (&symfile_complaints,
6242 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6243 offset);
6244 return;
6245 }
6246
6247 for (;;)
6248 {
6249 unsigned int bytes_read;
6250 CORE_ADDR start, end;
6251
6252 start = read_address (obfd, buffer, cu, &bytes_read);
6253 buffer += bytes_read;
6254 end = read_address (obfd, buffer, cu, &bytes_read);
6255 buffer += bytes_read;
6256
6257 /* Did we find the end of the range list? */
6258 if (start == 0 && end == 0)
6259 break;
6260
6261 /* Did we find a base address selection entry? */
6262 else if ((start & base_select_mask) == base_select_mask)
6263 {
6264 base = end;
6265 base_known = 1;
6266 }
6267
6268 /* We found an ordinary address range. */
6269 else
6270 {
6271 if (!base_known)
6272 {
6273 complaint (&symfile_complaints,
6274 _("Invalid .debug_ranges data "
6275 "(no base address)"));
6276 return;
6277 }
6278
6279 if (start > end)
6280 {
6281 /* Inverted range entries are invalid. */
6282 complaint (&symfile_complaints,
6283 _("Invalid .debug_ranges data "
6284 "(inverted range)"));
6285 return;
6286 }
6287
6288 /* Empty range entries have no effect. */
6289 if (start == end)
6290 continue;
6291
6292 record_block_range (block,
6293 baseaddr + base + start,
6294 baseaddr + base + end - 1);
6295 }
6296 }
6297 }
6298 }
6299
6300 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6301 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6302 during 4.6.0 experimental. */
6303
6304 static int
6305 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6306 {
6307 const char *cs;
6308 int major, minor, release;
6309
6310 if (cu->producer == NULL)
6311 {
6312 /* For unknown compilers expect their behavior is DWARF version
6313 compliant.
6314
6315 GCC started to support .debug_types sections by -gdwarf-4 since
6316 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6317 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6318 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6319 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6320
6321 return 0;
6322 }
6323
6324 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6325
6326 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6327 {
6328 /* For non-GCC compilers expect their behavior is DWARF version
6329 compliant. */
6330
6331 return 0;
6332 }
6333 cs = &cu->producer[strlen ("GNU ")];
6334 while (*cs && !isdigit (*cs))
6335 cs++;
6336 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6337 {
6338 /* Not recognized as GCC. */
6339
6340 return 0;
6341 }
6342
6343 return major < 4 || (major == 4 && minor < 6);
6344 }
6345
6346 /* Return the default accessibility type if it is not overriden by
6347 DW_AT_accessibility. */
6348
6349 static enum dwarf_access_attribute
6350 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6351 {
6352 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6353 {
6354 /* The default DWARF 2 accessibility for members is public, the default
6355 accessibility for inheritance is private. */
6356
6357 if (die->tag != DW_TAG_inheritance)
6358 return DW_ACCESS_public;
6359 else
6360 return DW_ACCESS_private;
6361 }
6362 else
6363 {
6364 /* DWARF 3+ defines the default accessibility a different way. The same
6365 rules apply now for DW_TAG_inheritance as for the members and it only
6366 depends on the container kind. */
6367
6368 if (die->parent->tag == DW_TAG_class_type)
6369 return DW_ACCESS_private;
6370 else
6371 return DW_ACCESS_public;
6372 }
6373 }
6374
6375 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6376 offset. If the attribute was not found return 0, otherwise return
6377 1. If it was found but could not properly be handled, set *OFFSET
6378 to 0. */
6379
6380 static int
6381 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6382 LONGEST *offset)
6383 {
6384 struct attribute *attr;
6385
6386 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6387 if (attr != NULL)
6388 {
6389 *offset = 0;
6390
6391 /* Note that we do not check for a section offset first here.
6392 This is because DW_AT_data_member_location is new in DWARF 4,
6393 so if we see it, we can assume that a constant form is really
6394 a constant and not a section offset. */
6395 if (attr_form_is_constant (attr))
6396 *offset = dwarf2_get_attr_constant_value (attr, 0);
6397 else if (attr_form_is_section_offset (attr))
6398 dwarf2_complex_location_expr_complaint ();
6399 else if (attr_form_is_block (attr))
6400 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6401 else
6402 dwarf2_complex_location_expr_complaint ();
6403
6404 return 1;
6405 }
6406
6407 return 0;
6408 }
6409
6410 /* Add an aggregate field to the field list. */
6411
6412 static void
6413 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6414 struct dwarf2_cu *cu)
6415 {
6416 struct objfile *objfile = cu->objfile;
6417 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6418 struct nextfield *new_field;
6419 struct attribute *attr;
6420 struct field *fp;
6421 char *fieldname = "";
6422
6423 /* Allocate a new field list entry and link it in. */
6424 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6425 make_cleanup (xfree, new_field);
6426 memset (new_field, 0, sizeof (struct nextfield));
6427
6428 if (die->tag == DW_TAG_inheritance)
6429 {
6430 new_field->next = fip->baseclasses;
6431 fip->baseclasses = new_field;
6432 }
6433 else
6434 {
6435 new_field->next = fip->fields;
6436 fip->fields = new_field;
6437 }
6438 fip->nfields++;
6439
6440 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6441 if (attr)
6442 new_field->accessibility = DW_UNSND (attr);
6443 else
6444 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6445 if (new_field->accessibility != DW_ACCESS_public)
6446 fip->non_public_fields = 1;
6447
6448 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6449 if (attr)
6450 new_field->virtuality = DW_UNSND (attr);
6451 else
6452 new_field->virtuality = DW_VIRTUALITY_none;
6453
6454 fp = &new_field->field;
6455
6456 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6457 {
6458 LONGEST offset;
6459
6460 /* Data member other than a C++ static data member. */
6461
6462 /* Get type of field. */
6463 fp->type = die_type (die, cu);
6464
6465 SET_FIELD_BITPOS (*fp, 0);
6466
6467 /* Get bit size of field (zero if none). */
6468 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6469 if (attr)
6470 {
6471 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6472 }
6473 else
6474 {
6475 FIELD_BITSIZE (*fp) = 0;
6476 }
6477
6478 /* Get bit offset of field. */
6479 if (handle_data_member_location (die, cu, &offset))
6480 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6481 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6482 if (attr)
6483 {
6484 if (gdbarch_bits_big_endian (gdbarch))
6485 {
6486 /* For big endian bits, the DW_AT_bit_offset gives the
6487 additional bit offset from the MSB of the containing
6488 anonymous object to the MSB of the field. We don't
6489 have to do anything special since we don't need to
6490 know the size of the anonymous object. */
6491 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6492 }
6493 else
6494 {
6495 /* For little endian bits, compute the bit offset to the
6496 MSB of the anonymous object, subtract off the number of
6497 bits from the MSB of the field to the MSB of the
6498 object, and then subtract off the number of bits of
6499 the field itself. The result is the bit offset of
6500 the LSB of the field. */
6501 int anonymous_size;
6502 int bit_offset = DW_UNSND (attr);
6503
6504 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6505 if (attr)
6506 {
6507 /* The size of the anonymous object containing
6508 the bit field is explicit, so use the
6509 indicated size (in bytes). */
6510 anonymous_size = DW_UNSND (attr);
6511 }
6512 else
6513 {
6514 /* The size of the anonymous object containing
6515 the bit field must be inferred from the type
6516 attribute of the data member containing the
6517 bit field. */
6518 anonymous_size = TYPE_LENGTH (fp->type);
6519 }
6520 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6521 - bit_offset - FIELD_BITSIZE (*fp);
6522 }
6523 }
6524
6525 /* Get name of field. */
6526 fieldname = dwarf2_name (die, cu);
6527 if (fieldname == NULL)
6528 fieldname = "";
6529
6530 /* The name is already allocated along with this objfile, so we don't
6531 need to duplicate it for the type. */
6532 fp->name = fieldname;
6533
6534 /* Change accessibility for artificial fields (e.g. virtual table
6535 pointer or virtual base class pointer) to private. */
6536 if (dwarf2_attr (die, DW_AT_artificial, cu))
6537 {
6538 FIELD_ARTIFICIAL (*fp) = 1;
6539 new_field->accessibility = DW_ACCESS_private;
6540 fip->non_public_fields = 1;
6541 }
6542 }
6543 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6544 {
6545 /* C++ static member. */
6546
6547 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6548 is a declaration, but all versions of G++ as of this writing
6549 (so through at least 3.2.1) incorrectly generate
6550 DW_TAG_variable tags. */
6551
6552 const char *physname;
6553
6554 /* Get name of field. */
6555 fieldname = dwarf2_name (die, cu);
6556 if (fieldname == NULL)
6557 return;
6558
6559 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6560 if (attr
6561 /* Only create a symbol if this is an external value.
6562 new_symbol checks this and puts the value in the global symbol
6563 table, which we want. If it is not external, new_symbol
6564 will try to put the value in cu->list_in_scope which is wrong. */
6565 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6566 {
6567 /* A static const member, not much different than an enum as far as
6568 we're concerned, except that we can support more types. */
6569 new_symbol (die, NULL, cu);
6570 }
6571
6572 /* Get physical name. */
6573 physname = dwarf2_physname (fieldname, die, cu);
6574
6575 /* The name is already allocated along with this objfile, so we don't
6576 need to duplicate it for the type. */
6577 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6578 FIELD_TYPE (*fp) = die_type (die, cu);
6579 FIELD_NAME (*fp) = fieldname;
6580 }
6581 else if (die->tag == DW_TAG_inheritance)
6582 {
6583 LONGEST offset;
6584
6585 /* C++ base class field. */
6586 if (handle_data_member_location (die, cu, &offset))
6587 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6588 FIELD_BITSIZE (*fp) = 0;
6589 FIELD_TYPE (*fp) = die_type (die, cu);
6590 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6591 fip->nbaseclasses++;
6592 }
6593 }
6594
6595 /* Add a typedef defined in the scope of the FIP's class. */
6596
6597 static void
6598 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6599 struct dwarf2_cu *cu)
6600 {
6601 struct objfile *objfile = cu->objfile;
6602 struct typedef_field_list *new_field;
6603 struct attribute *attr;
6604 struct typedef_field *fp;
6605 char *fieldname = "";
6606
6607 /* Allocate a new field list entry and link it in. */
6608 new_field = xzalloc (sizeof (*new_field));
6609 make_cleanup (xfree, new_field);
6610
6611 gdb_assert (die->tag == DW_TAG_typedef);
6612
6613 fp = &new_field->field;
6614
6615 /* Get name of field. */
6616 fp->name = dwarf2_name (die, cu);
6617 if (fp->name == NULL)
6618 return;
6619
6620 fp->type = read_type_die (die, cu);
6621
6622 new_field->next = fip->typedef_field_list;
6623 fip->typedef_field_list = new_field;
6624 fip->typedef_field_list_count++;
6625 }
6626
6627 /* Create the vector of fields, and attach it to the type. */
6628
6629 static void
6630 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6631 struct dwarf2_cu *cu)
6632 {
6633 int nfields = fip->nfields;
6634
6635 /* Record the field count, allocate space for the array of fields,
6636 and create blank accessibility bitfields if necessary. */
6637 TYPE_NFIELDS (type) = nfields;
6638 TYPE_FIELDS (type) = (struct field *)
6639 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6640 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6641
6642 if (fip->non_public_fields && cu->language != language_ada)
6643 {
6644 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6645
6646 TYPE_FIELD_PRIVATE_BITS (type) =
6647 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6648 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6649
6650 TYPE_FIELD_PROTECTED_BITS (type) =
6651 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6652 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6653
6654 TYPE_FIELD_IGNORE_BITS (type) =
6655 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6656 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6657 }
6658
6659 /* If the type has baseclasses, allocate and clear a bit vector for
6660 TYPE_FIELD_VIRTUAL_BITS. */
6661 if (fip->nbaseclasses && cu->language != language_ada)
6662 {
6663 int num_bytes = B_BYTES (fip->nbaseclasses);
6664 unsigned char *pointer;
6665
6666 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6667 pointer = TYPE_ALLOC (type, num_bytes);
6668 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6669 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6670 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6671 }
6672
6673 /* Copy the saved-up fields into the field vector. Start from the head of
6674 the list, adding to the tail of the field array, so that they end up in
6675 the same order in the array in which they were added to the list. */
6676 while (nfields-- > 0)
6677 {
6678 struct nextfield *fieldp;
6679
6680 if (fip->fields)
6681 {
6682 fieldp = fip->fields;
6683 fip->fields = fieldp->next;
6684 }
6685 else
6686 {
6687 fieldp = fip->baseclasses;
6688 fip->baseclasses = fieldp->next;
6689 }
6690
6691 TYPE_FIELD (type, nfields) = fieldp->field;
6692 switch (fieldp->accessibility)
6693 {
6694 case DW_ACCESS_private:
6695 if (cu->language != language_ada)
6696 SET_TYPE_FIELD_PRIVATE (type, nfields);
6697 break;
6698
6699 case DW_ACCESS_protected:
6700 if (cu->language != language_ada)
6701 SET_TYPE_FIELD_PROTECTED (type, nfields);
6702 break;
6703
6704 case DW_ACCESS_public:
6705 break;
6706
6707 default:
6708 /* Unknown accessibility. Complain and treat it as public. */
6709 {
6710 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6711 fieldp->accessibility);
6712 }
6713 break;
6714 }
6715 if (nfields < fip->nbaseclasses)
6716 {
6717 switch (fieldp->virtuality)
6718 {
6719 case DW_VIRTUALITY_virtual:
6720 case DW_VIRTUALITY_pure_virtual:
6721 if (cu->language == language_ada)
6722 error (_("unexpected virtuality in component of Ada type"));
6723 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6724 break;
6725 }
6726 }
6727 }
6728 }
6729
6730 /* Add a member function to the proper fieldlist. */
6731
6732 static void
6733 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6734 struct type *type, struct dwarf2_cu *cu)
6735 {
6736 struct objfile *objfile = cu->objfile;
6737 struct attribute *attr;
6738 struct fnfieldlist *flp;
6739 int i;
6740 struct fn_field *fnp;
6741 char *fieldname;
6742 struct nextfnfield *new_fnfield;
6743 struct type *this_type;
6744 enum dwarf_access_attribute accessibility;
6745
6746 if (cu->language == language_ada)
6747 error (_("unexpected member function in Ada type"));
6748
6749 /* Get name of member function. */
6750 fieldname = dwarf2_name (die, cu);
6751 if (fieldname == NULL)
6752 return;
6753
6754 /* Look up member function name in fieldlist. */
6755 for (i = 0; i < fip->nfnfields; i++)
6756 {
6757 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6758 break;
6759 }
6760
6761 /* Create new list element if necessary. */
6762 if (i < fip->nfnfields)
6763 flp = &fip->fnfieldlists[i];
6764 else
6765 {
6766 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6767 {
6768 fip->fnfieldlists = (struct fnfieldlist *)
6769 xrealloc (fip->fnfieldlists,
6770 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6771 * sizeof (struct fnfieldlist));
6772 if (fip->nfnfields == 0)
6773 make_cleanup (free_current_contents, &fip->fnfieldlists);
6774 }
6775 flp = &fip->fnfieldlists[fip->nfnfields];
6776 flp->name = fieldname;
6777 flp->length = 0;
6778 flp->head = NULL;
6779 i = fip->nfnfields++;
6780 }
6781
6782 /* Create a new member function field and chain it to the field list
6783 entry. */
6784 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6785 make_cleanup (xfree, new_fnfield);
6786 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6787 new_fnfield->next = flp->head;
6788 flp->head = new_fnfield;
6789 flp->length++;
6790
6791 /* Fill in the member function field info. */
6792 fnp = &new_fnfield->fnfield;
6793
6794 /* Delay processing of the physname until later. */
6795 if (cu->language == language_cplus || cu->language == language_java)
6796 {
6797 add_to_method_list (type, i, flp->length - 1, fieldname,
6798 die, cu);
6799 }
6800 else
6801 {
6802 const char *physname = dwarf2_physname (fieldname, die, cu);
6803 fnp->physname = physname ? physname : "";
6804 }
6805
6806 fnp->type = alloc_type (objfile);
6807 this_type = read_type_die (die, cu);
6808 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6809 {
6810 int nparams = TYPE_NFIELDS (this_type);
6811
6812 /* TYPE is the domain of this method, and THIS_TYPE is the type
6813 of the method itself (TYPE_CODE_METHOD). */
6814 smash_to_method_type (fnp->type, type,
6815 TYPE_TARGET_TYPE (this_type),
6816 TYPE_FIELDS (this_type),
6817 TYPE_NFIELDS (this_type),
6818 TYPE_VARARGS (this_type));
6819
6820 /* Handle static member functions.
6821 Dwarf2 has no clean way to discern C++ static and non-static
6822 member functions. G++ helps GDB by marking the first
6823 parameter for non-static member functions (which is the this
6824 pointer) as artificial. We obtain this information from
6825 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
6826 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6827 fnp->voffset = VOFFSET_STATIC;
6828 }
6829 else
6830 complaint (&symfile_complaints, _("member function type missing for '%s'"),
6831 dwarf2_full_name (fieldname, die, cu));
6832
6833 /* Get fcontext from DW_AT_containing_type if present. */
6834 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6835 fnp->fcontext = die_containing_type (die, cu);
6836
6837 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6838 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6839
6840 /* Get accessibility. */
6841 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6842 if (attr)
6843 accessibility = DW_UNSND (attr);
6844 else
6845 accessibility = dwarf2_default_access_attribute (die, cu);
6846 switch (accessibility)
6847 {
6848 case DW_ACCESS_private:
6849 fnp->is_private = 1;
6850 break;
6851 case DW_ACCESS_protected:
6852 fnp->is_protected = 1;
6853 break;
6854 }
6855
6856 /* Check for artificial methods. */
6857 attr = dwarf2_attr (die, DW_AT_artificial, cu);
6858 if (attr && DW_UNSND (attr) != 0)
6859 fnp->is_artificial = 1;
6860
6861 /* Get index in virtual function table if it is a virtual member
6862 function. For older versions of GCC, this is an offset in the
6863 appropriate virtual table, as specified by DW_AT_containing_type.
6864 For everyone else, it is an expression to be evaluated relative
6865 to the object address. */
6866
6867 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6868 if (attr)
6869 {
6870 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6871 {
6872 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6873 {
6874 /* Old-style GCC. */
6875 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6876 }
6877 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6878 || (DW_BLOCK (attr)->size > 1
6879 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6880 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6881 {
6882 struct dwarf_block blk;
6883 int offset;
6884
6885 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6886 ? 1 : 2);
6887 blk.size = DW_BLOCK (attr)->size - offset;
6888 blk.data = DW_BLOCK (attr)->data + offset;
6889 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6890 if ((fnp->voffset % cu->header.addr_size) != 0)
6891 dwarf2_complex_location_expr_complaint ();
6892 else
6893 fnp->voffset /= cu->header.addr_size;
6894 fnp->voffset += 2;
6895 }
6896 else
6897 dwarf2_complex_location_expr_complaint ();
6898
6899 if (!fnp->fcontext)
6900 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6901 }
6902 else if (attr_form_is_section_offset (attr))
6903 {
6904 dwarf2_complex_location_expr_complaint ();
6905 }
6906 else
6907 {
6908 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6909 fieldname);
6910 }
6911 }
6912 else
6913 {
6914 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6915 if (attr && DW_UNSND (attr))
6916 {
6917 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6918 complaint (&symfile_complaints,
6919 _("Member function \"%s\" (offset %d) is virtual "
6920 "but the vtable offset is not specified"),
6921 fieldname, die->offset);
6922 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6923 TYPE_CPLUS_DYNAMIC (type) = 1;
6924 }
6925 }
6926 }
6927
6928 /* Create the vector of member function fields, and attach it to the type. */
6929
6930 static void
6931 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6932 struct dwarf2_cu *cu)
6933 {
6934 struct fnfieldlist *flp;
6935 int total_length = 0;
6936 int i;
6937
6938 if (cu->language == language_ada)
6939 error (_("unexpected member functions in Ada type"));
6940
6941 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6942 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6943 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6944
6945 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6946 {
6947 struct nextfnfield *nfp = flp->head;
6948 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6949 int k;
6950
6951 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6952 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6953 fn_flp->fn_fields = (struct fn_field *)
6954 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6955 for (k = flp->length; (k--, nfp); nfp = nfp->next)
6956 fn_flp->fn_fields[k] = nfp->fnfield;
6957
6958 total_length += flp->length;
6959 }
6960
6961 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6962 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6963 }
6964
6965 /* Returns non-zero if NAME is the name of a vtable member in CU's
6966 language, zero otherwise. */
6967 static int
6968 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6969 {
6970 static const char vptr[] = "_vptr";
6971 static const char vtable[] = "vtable";
6972
6973 /* Look for the C++ and Java forms of the vtable. */
6974 if ((cu->language == language_java
6975 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6976 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6977 && is_cplus_marker (name[sizeof (vptr) - 1])))
6978 return 1;
6979
6980 return 0;
6981 }
6982
6983 /* GCC outputs unnamed structures that are really pointers to member
6984 functions, with the ABI-specified layout. If TYPE describes
6985 such a structure, smash it into a member function type.
6986
6987 GCC shouldn't do this; it should just output pointer to member DIEs.
6988 This is GCC PR debug/28767. */
6989
6990 static void
6991 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6992 {
6993 struct type *pfn_type, *domain_type, *new_type;
6994
6995 /* Check for a structure with no name and two children. */
6996 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6997 return;
6998
6999 /* Check for __pfn and __delta members. */
7000 if (TYPE_FIELD_NAME (type, 0) == NULL
7001 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7002 || TYPE_FIELD_NAME (type, 1) == NULL
7003 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7004 return;
7005
7006 /* Find the type of the method. */
7007 pfn_type = TYPE_FIELD_TYPE (type, 0);
7008 if (pfn_type == NULL
7009 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7010 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7011 return;
7012
7013 /* Look for the "this" argument. */
7014 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7015 if (TYPE_NFIELDS (pfn_type) == 0
7016 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7017 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7018 return;
7019
7020 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7021 new_type = alloc_type (objfile);
7022 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7023 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7024 TYPE_VARARGS (pfn_type));
7025 smash_to_methodptr_type (type, new_type);
7026 }
7027
7028 /* Called when we find the DIE that starts a structure or union scope
7029 (definition) to create a type for the structure or union. Fill in
7030 the type's name and general properties; the members will not be
7031 processed until process_structure_type.
7032
7033 NOTE: we need to call these functions regardless of whether or not the
7034 DIE has a DW_AT_name attribute, since it might be an anonymous
7035 structure or union. This gets the type entered into our set of
7036 user defined types.
7037
7038 However, if the structure is incomplete (an opaque struct/union)
7039 then suppress creating a symbol table entry for it since gdb only
7040 wants to find the one with the complete definition. Note that if
7041 it is complete, we just call new_symbol, which does it's own
7042 checking about whether the struct/union is anonymous or not (and
7043 suppresses creating a symbol table entry itself). */
7044
7045 static struct type *
7046 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7047 {
7048 struct objfile *objfile = cu->objfile;
7049 struct type *type;
7050 struct attribute *attr;
7051 char *name;
7052
7053 /* If the definition of this type lives in .debug_types, read that type.
7054 Don't follow DW_AT_specification though, that will take us back up
7055 the chain and we want to go down. */
7056 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7057 if (attr)
7058 {
7059 struct dwarf2_cu *type_cu = cu;
7060 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7061
7062 /* We could just recurse on read_structure_type, but we need to call
7063 get_die_type to ensure only one type for this DIE is created.
7064 This is important, for example, because for c++ classes we need
7065 TYPE_NAME set which is only done by new_symbol. Blech. */
7066 type = read_type_die (type_die, type_cu);
7067
7068 /* TYPE_CU may not be the same as CU.
7069 Ensure TYPE is recorded in CU's type_hash table. */
7070 return set_die_type (die, type, cu);
7071 }
7072
7073 type = alloc_type (objfile);
7074 INIT_CPLUS_SPECIFIC (type);
7075
7076 name = dwarf2_name (die, cu);
7077 if (name != NULL)
7078 {
7079 if (cu->language == language_cplus
7080 || cu->language == language_java)
7081 {
7082 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7083
7084 /* dwarf2_full_name might have already finished building the DIE's
7085 type. If so, there is no need to continue. */
7086 if (get_die_type (die, cu) != NULL)
7087 return get_die_type (die, cu);
7088
7089 TYPE_TAG_NAME (type) = full_name;
7090 if (die->tag == DW_TAG_structure_type
7091 || die->tag == DW_TAG_class_type)
7092 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7093 }
7094 else
7095 {
7096 /* The name is already allocated along with this objfile, so
7097 we don't need to duplicate it for the type. */
7098 TYPE_TAG_NAME (type) = (char *) name;
7099 if (die->tag == DW_TAG_class_type)
7100 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7101 }
7102 }
7103
7104 if (die->tag == DW_TAG_structure_type)
7105 {
7106 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7107 }
7108 else if (die->tag == DW_TAG_union_type)
7109 {
7110 TYPE_CODE (type) = TYPE_CODE_UNION;
7111 }
7112 else
7113 {
7114 TYPE_CODE (type) = TYPE_CODE_CLASS;
7115 }
7116
7117 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7118 TYPE_DECLARED_CLASS (type) = 1;
7119
7120 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7121 if (attr)
7122 {
7123 TYPE_LENGTH (type) = DW_UNSND (attr);
7124 }
7125 else
7126 {
7127 TYPE_LENGTH (type) = 0;
7128 }
7129
7130 TYPE_STUB_SUPPORTED (type) = 1;
7131 if (die_is_declaration (die, cu))
7132 TYPE_STUB (type) = 1;
7133 else if (attr == NULL && die->child == NULL
7134 && producer_is_realview (cu->producer))
7135 /* RealView does not output the required DW_AT_declaration
7136 on incomplete types. */
7137 TYPE_STUB (type) = 1;
7138
7139 /* We need to add the type field to the die immediately so we don't
7140 infinitely recurse when dealing with pointers to the structure
7141 type within the structure itself. */
7142 set_die_type (die, type, cu);
7143
7144 /* set_die_type should be already done. */
7145 set_descriptive_type (type, die, cu);
7146
7147 return type;
7148 }
7149
7150 /* Finish creating a structure or union type, including filling in
7151 its members and creating a symbol for it. */
7152
7153 static void
7154 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7155 {
7156 struct objfile *objfile = cu->objfile;
7157 struct die_info *child_die = die->child;
7158 struct type *type;
7159
7160 type = get_die_type (die, cu);
7161 if (type == NULL)
7162 type = read_structure_type (die, cu);
7163
7164 if (die->child != NULL && ! die_is_declaration (die, cu))
7165 {
7166 struct field_info fi;
7167 struct die_info *child_die;
7168 VEC (symbolp) *template_args = NULL;
7169 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7170
7171 memset (&fi, 0, sizeof (struct field_info));
7172
7173 child_die = die->child;
7174
7175 while (child_die && child_die->tag)
7176 {
7177 if (child_die->tag == DW_TAG_member
7178 || child_die->tag == DW_TAG_variable)
7179 {
7180 /* NOTE: carlton/2002-11-05: A C++ static data member
7181 should be a DW_TAG_member that is a declaration, but
7182 all versions of G++ as of this writing (so through at
7183 least 3.2.1) incorrectly generate DW_TAG_variable
7184 tags for them instead. */
7185 dwarf2_add_field (&fi, child_die, cu);
7186 }
7187 else if (child_die->tag == DW_TAG_subprogram)
7188 {
7189 /* C++ member function. */
7190 dwarf2_add_member_fn (&fi, child_die, type, cu);
7191 }
7192 else if (child_die->tag == DW_TAG_inheritance)
7193 {
7194 /* C++ base class field. */
7195 dwarf2_add_field (&fi, child_die, cu);
7196 }
7197 else if (child_die->tag == DW_TAG_typedef)
7198 dwarf2_add_typedef (&fi, child_die, cu);
7199 else if (child_die->tag == DW_TAG_template_type_param
7200 || child_die->tag == DW_TAG_template_value_param)
7201 {
7202 struct symbol *arg = new_symbol (child_die, NULL, cu);
7203
7204 if (arg != NULL)
7205 VEC_safe_push (symbolp, template_args, arg);
7206 }
7207
7208 child_die = sibling_die (child_die);
7209 }
7210
7211 /* Attach template arguments to type. */
7212 if (! VEC_empty (symbolp, template_args))
7213 {
7214 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7215 TYPE_N_TEMPLATE_ARGUMENTS (type)
7216 = VEC_length (symbolp, template_args);
7217 TYPE_TEMPLATE_ARGUMENTS (type)
7218 = obstack_alloc (&objfile->objfile_obstack,
7219 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7220 * sizeof (struct symbol *)));
7221 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7222 VEC_address (symbolp, template_args),
7223 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7224 * sizeof (struct symbol *)));
7225 VEC_free (symbolp, template_args);
7226 }
7227
7228 /* Attach fields and member functions to the type. */
7229 if (fi.nfields)
7230 dwarf2_attach_fields_to_type (&fi, type, cu);
7231 if (fi.nfnfields)
7232 {
7233 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7234
7235 /* Get the type which refers to the base class (possibly this
7236 class itself) which contains the vtable pointer for the current
7237 class from the DW_AT_containing_type attribute. This use of
7238 DW_AT_containing_type is a GNU extension. */
7239
7240 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7241 {
7242 struct type *t = die_containing_type (die, cu);
7243
7244 TYPE_VPTR_BASETYPE (type) = t;
7245 if (type == t)
7246 {
7247 int i;
7248
7249 /* Our own class provides vtbl ptr. */
7250 for (i = TYPE_NFIELDS (t) - 1;
7251 i >= TYPE_N_BASECLASSES (t);
7252 --i)
7253 {
7254 char *fieldname = TYPE_FIELD_NAME (t, i);
7255
7256 if (is_vtable_name (fieldname, cu))
7257 {
7258 TYPE_VPTR_FIELDNO (type) = i;
7259 break;
7260 }
7261 }
7262
7263 /* Complain if virtual function table field not found. */
7264 if (i < TYPE_N_BASECLASSES (t))
7265 complaint (&symfile_complaints,
7266 _("virtual function table pointer "
7267 "not found when defining class '%s'"),
7268 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7269 "");
7270 }
7271 else
7272 {
7273 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7274 }
7275 }
7276 else if (cu->producer
7277 && strncmp (cu->producer,
7278 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7279 {
7280 /* The IBM XLC compiler does not provide direct indication
7281 of the containing type, but the vtable pointer is
7282 always named __vfp. */
7283
7284 int i;
7285
7286 for (i = TYPE_NFIELDS (type) - 1;
7287 i >= TYPE_N_BASECLASSES (type);
7288 --i)
7289 {
7290 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7291 {
7292 TYPE_VPTR_FIELDNO (type) = i;
7293 TYPE_VPTR_BASETYPE (type) = type;
7294 break;
7295 }
7296 }
7297 }
7298 }
7299
7300 /* Copy fi.typedef_field_list linked list elements content into the
7301 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7302 if (fi.typedef_field_list)
7303 {
7304 int i = fi.typedef_field_list_count;
7305
7306 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7307 TYPE_TYPEDEF_FIELD_ARRAY (type)
7308 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7309 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7310
7311 /* Reverse the list order to keep the debug info elements order. */
7312 while (--i >= 0)
7313 {
7314 struct typedef_field *dest, *src;
7315
7316 dest = &TYPE_TYPEDEF_FIELD (type, i);
7317 src = &fi.typedef_field_list->field;
7318 fi.typedef_field_list = fi.typedef_field_list->next;
7319 *dest = *src;
7320 }
7321 }
7322
7323 do_cleanups (back_to);
7324 }
7325
7326 quirk_gcc_member_function_pointer (type, cu->objfile);
7327
7328 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7329 snapshots) has been known to create a die giving a declaration
7330 for a class that has, as a child, a die giving a definition for a
7331 nested class. So we have to process our children even if the
7332 current die is a declaration. Normally, of course, a declaration
7333 won't have any children at all. */
7334
7335 while (child_die != NULL && child_die->tag)
7336 {
7337 if (child_die->tag == DW_TAG_member
7338 || child_die->tag == DW_TAG_variable
7339 || child_die->tag == DW_TAG_inheritance
7340 || child_die->tag == DW_TAG_template_value_param
7341 || child_die->tag == DW_TAG_template_type_param)
7342 {
7343 /* Do nothing. */
7344 }
7345 else
7346 process_die (child_die, cu);
7347
7348 child_die = sibling_die (child_die);
7349 }
7350
7351 /* Do not consider external references. According to the DWARF standard,
7352 these DIEs are identified by the fact that they have no byte_size
7353 attribute, and a declaration attribute. */
7354 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7355 || !die_is_declaration (die, cu))
7356 new_symbol (die, type, cu);
7357 }
7358
7359 /* Given a DW_AT_enumeration_type die, set its type. We do not
7360 complete the type's fields yet, or create any symbols. */
7361
7362 static struct type *
7363 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7364 {
7365 struct objfile *objfile = cu->objfile;
7366 struct type *type;
7367 struct attribute *attr;
7368 const char *name;
7369
7370 /* If the definition of this type lives in .debug_types, read that type.
7371 Don't follow DW_AT_specification though, that will take us back up
7372 the chain and we want to go down. */
7373 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7374 if (attr)
7375 {
7376 struct dwarf2_cu *type_cu = cu;
7377 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7378
7379 type = read_type_die (type_die, type_cu);
7380
7381 /* TYPE_CU may not be the same as CU.
7382 Ensure TYPE is recorded in CU's type_hash table. */
7383 return set_die_type (die, type, cu);
7384 }
7385
7386 type = alloc_type (objfile);
7387
7388 TYPE_CODE (type) = TYPE_CODE_ENUM;
7389 name = dwarf2_full_name (NULL, die, cu);
7390 if (name != NULL)
7391 TYPE_TAG_NAME (type) = (char *) name;
7392
7393 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7394 if (attr)
7395 {
7396 TYPE_LENGTH (type) = DW_UNSND (attr);
7397 }
7398 else
7399 {
7400 TYPE_LENGTH (type) = 0;
7401 }
7402
7403 /* The enumeration DIE can be incomplete. In Ada, any type can be
7404 declared as private in the package spec, and then defined only
7405 inside the package body. Such types are known as Taft Amendment
7406 Types. When another package uses such a type, an incomplete DIE
7407 may be generated by the compiler. */
7408 if (die_is_declaration (die, cu))
7409 TYPE_STUB (type) = 1;
7410
7411 return set_die_type (die, type, cu);
7412 }
7413
7414 /* Given a pointer to a die which begins an enumeration, process all
7415 the dies that define the members of the enumeration, and create the
7416 symbol for the enumeration type.
7417
7418 NOTE: We reverse the order of the element list. */
7419
7420 static void
7421 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7422 {
7423 struct type *this_type;
7424
7425 this_type = get_die_type (die, cu);
7426 if (this_type == NULL)
7427 this_type = read_enumeration_type (die, cu);
7428
7429 if (die->child != NULL)
7430 {
7431 struct die_info *child_die;
7432 struct symbol *sym;
7433 struct field *fields = NULL;
7434 int num_fields = 0;
7435 int unsigned_enum = 1;
7436 char *name;
7437
7438 child_die = die->child;
7439 while (child_die && child_die->tag)
7440 {
7441 if (child_die->tag != DW_TAG_enumerator)
7442 {
7443 process_die (child_die, cu);
7444 }
7445 else
7446 {
7447 name = dwarf2_name (child_die, cu);
7448 if (name)
7449 {
7450 sym = new_symbol (child_die, this_type, cu);
7451 if (SYMBOL_VALUE (sym) < 0)
7452 unsigned_enum = 0;
7453
7454 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7455 {
7456 fields = (struct field *)
7457 xrealloc (fields,
7458 (num_fields + DW_FIELD_ALLOC_CHUNK)
7459 * sizeof (struct field));
7460 }
7461
7462 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7463 FIELD_TYPE (fields[num_fields]) = NULL;
7464 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7465 FIELD_BITSIZE (fields[num_fields]) = 0;
7466
7467 num_fields++;
7468 }
7469 }
7470
7471 child_die = sibling_die (child_die);
7472 }
7473
7474 if (num_fields)
7475 {
7476 TYPE_NFIELDS (this_type) = num_fields;
7477 TYPE_FIELDS (this_type) = (struct field *)
7478 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7479 memcpy (TYPE_FIELDS (this_type), fields,
7480 sizeof (struct field) * num_fields);
7481 xfree (fields);
7482 }
7483 if (unsigned_enum)
7484 TYPE_UNSIGNED (this_type) = 1;
7485 }
7486
7487 new_symbol (die, this_type, cu);
7488 }
7489
7490 /* Extract all information from a DW_TAG_array_type DIE and put it in
7491 the DIE's type field. For now, this only handles one dimensional
7492 arrays. */
7493
7494 static struct type *
7495 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7496 {
7497 struct objfile *objfile = cu->objfile;
7498 struct die_info *child_die;
7499 struct type *type;
7500 struct type *element_type, *range_type, *index_type;
7501 struct type **range_types = NULL;
7502 struct attribute *attr;
7503 int ndim = 0;
7504 struct cleanup *back_to;
7505 char *name;
7506
7507 element_type = die_type (die, cu);
7508
7509 /* The die_type call above may have already set the type for this DIE. */
7510 type = get_die_type (die, cu);
7511 if (type)
7512 return type;
7513
7514 /* Irix 6.2 native cc creates array types without children for
7515 arrays with unspecified length. */
7516 if (die->child == NULL)
7517 {
7518 index_type = objfile_type (objfile)->builtin_int;
7519 range_type = create_range_type (NULL, index_type, 0, -1);
7520 type = create_array_type (NULL, element_type, range_type);
7521 return set_die_type (die, type, cu);
7522 }
7523
7524 back_to = make_cleanup (null_cleanup, NULL);
7525 child_die = die->child;
7526 while (child_die && child_die->tag)
7527 {
7528 if (child_die->tag == DW_TAG_subrange_type)
7529 {
7530 struct type *child_type = read_type_die (child_die, cu);
7531
7532 if (child_type != NULL)
7533 {
7534 /* The range type was succesfully read. Save it for the
7535 array type creation. */
7536 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7537 {
7538 range_types = (struct type **)
7539 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7540 * sizeof (struct type *));
7541 if (ndim == 0)
7542 make_cleanup (free_current_contents, &range_types);
7543 }
7544 range_types[ndim++] = child_type;
7545 }
7546 }
7547 child_die = sibling_die (child_die);
7548 }
7549
7550 /* Dwarf2 dimensions are output from left to right, create the
7551 necessary array types in backwards order. */
7552
7553 type = element_type;
7554
7555 if (read_array_order (die, cu) == DW_ORD_col_major)
7556 {
7557 int i = 0;
7558
7559 while (i < ndim)
7560 type = create_array_type (NULL, type, range_types[i++]);
7561 }
7562 else
7563 {
7564 while (ndim-- > 0)
7565 type = create_array_type (NULL, type, range_types[ndim]);
7566 }
7567
7568 /* Understand Dwarf2 support for vector types (like they occur on
7569 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7570 array type. This is not part of the Dwarf2/3 standard yet, but a
7571 custom vendor extension. The main difference between a regular
7572 array and the vector variant is that vectors are passed by value
7573 to functions. */
7574 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7575 if (attr)
7576 make_vector_type (type);
7577
7578 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7579 implementation may choose to implement triple vectors using this
7580 attribute. */
7581 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7582 if (attr)
7583 {
7584 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7585 TYPE_LENGTH (type) = DW_UNSND (attr);
7586 else
7587 complaint (&symfile_complaints,
7588 _("DW_AT_byte_size for array type smaller "
7589 "than the total size of elements"));
7590 }
7591
7592 name = dwarf2_name (die, cu);
7593 if (name)
7594 TYPE_NAME (type) = name;
7595
7596 /* Install the type in the die. */
7597 set_die_type (die, type, cu);
7598
7599 /* set_die_type should be already done. */
7600 set_descriptive_type (type, die, cu);
7601
7602 do_cleanups (back_to);
7603
7604 return type;
7605 }
7606
7607 static enum dwarf_array_dim_ordering
7608 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7609 {
7610 struct attribute *attr;
7611
7612 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7613
7614 if (attr) return DW_SND (attr);
7615
7616 /* GNU F77 is a special case, as at 08/2004 array type info is the
7617 opposite order to the dwarf2 specification, but data is still
7618 laid out as per normal fortran.
7619
7620 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7621 version checking. */
7622
7623 if (cu->language == language_fortran
7624 && cu->producer && strstr (cu->producer, "GNU F77"))
7625 {
7626 return DW_ORD_row_major;
7627 }
7628
7629 switch (cu->language_defn->la_array_ordering)
7630 {
7631 case array_column_major:
7632 return DW_ORD_col_major;
7633 case array_row_major:
7634 default:
7635 return DW_ORD_row_major;
7636 };
7637 }
7638
7639 /* Extract all information from a DW_TAG_set_type DIE and put it in
7640 the DIE's type field. */
7641
7642 static struct type *
7643 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7644 {
7645 struct type *domain_type, *set_type;
7646 struct attribute *attr;
7647
7648 domain_type = die_type (die, cu);
7649
7650 /* The die_type call above may have already set the type for this DIE. */
7651 set_type = get_die_type (die, cu);
7652 if (set_type)
7653 return set_type;
7654
7655 set_type = create_set_type (NULL, domain_type);
7656
7657 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7658 if (attr)
7659 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7660
7661 return set_die_type (die, set_type, cu);
7662 }
7663
7664 /* First cut: install each common block member as a global variable. */
7665
7666 static void
7667 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7668 {
7669 struct die_info *child_die;
7670 struct attribute *attr;
7671 struct symbol *sym;
7672 CORE_ADDR base = (CORE_ADDR) 0;
7673
7674 attr = dwarf2_attr (die, DW_AT_location, cu);
7675 if (attr)
7676 {
7677 /* Support the .debug_loc offsets. */
7678 if (attr_form_is_block (attr))
7679 {
7680 base = decode_locdesc (DW_BLOCK (attr), cu);
7681 }
7682 else if (attr_form_is_section_offset (attr))
7683 {
7684 dwarf2_complex_location_expr_complaint ();
7685 }
7686 else
7687 {
7688 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7689 "common block member");
7690 }
7691 }
7692 if (die->child != NULL)
7693 {
7694 child_die = die->child;
7695 while (child_die && child_die->tag)
7696 {
7697 LONGEST offset;
7698
7699 sym = new_symbol (child_die, NULL, cu);
7700 if (sym != NULL
7701 && handle_data_member_location (child_die, cu, &offset))
7702 {
7703 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
7704 add_symbol_to_list (sym, &global_symbols);
7705 }
7706 child_die = sibling_die (child_die);
7707 }
7708 }
7709 }
7710
7711 /* Create a type for a C++ namespace. */
7712
7713 static struct type *
7714 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7715 {
7716 struct objfile *objfile = cu->objfile;
7717 const char *previous_prefix, *name;
7718 int is_anonymous;
7719 struct type *type;
7720
7721 /* For extensions, reuse the type of the original namespace. */
7722 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7723 {
7724 struct die_info *ext_die;
7725 struct dwarf2_cu *ext_cu = cu;
7726
7727 ext_die = dwarf2_extension (die, &ext_cu);
7728 type = read_type_die (ext_die, ext_cu);
7729
7730 /* EXT_CU may not be the same as CU.
7731 Ensure TYPE is recorded in CU's type_hash table. */
7732 return set_die_type (die, type, cu);
7733 }
7734
7735 name = namespace_name (die, &is_anonymous, cu);
7736
7737 /* Now build the name of the current namespace. */
7738
7739 previous_prefix = determine_prefix (die, cu);
7740 if (previous_prefix[0] != '\0')
7741 name = typename_concat (&objfile->objfile_obstack,
7742 previous_prefix, name, 0, cu);
7743
7744 /* Create the type. */
7745 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7746 objfile);
7747 TYPE_NAME (type) = (char *) name;
7748 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7749
7750 return set_die_type (die, type, cu);
7751 }
7752
7753 /* Read a C++ namespace. */
7754
7755 static void
7756 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7757 {
7758 struct objfile *objfile = cu->objfile;
7759 int is_anonymous;
7760
7761 /* Add a symbol associated to this if we haven't seen the namespace
7762 before. Also, add a using directive if it's an anonymous
7763 namespace. */
7764
7765 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7766 {
7767 struct type *type;
7768
7769 type = read_type_die (die, cu);
7770 new_symbol (die, type, cu);
7771
7772 namespace_name (die, &is_anonymous, cu);
7773 if (is_anonymous)
7774 {
7775 const char *previous_prefix = determine_prefix (die, cu);
7776
7777 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7778 NULL, &objfile->objfile_obstack);
7779 }
7780 }
7781
7782 if (die->child != NULL)
7783 {
7784 struct die_info *child_die = die->child;
7785
7786 while (child_die && child_die->tag)
7787 {
7788 process_die (child_die, cu);
7789 child_die = sibling_die (child_die);
7790 }
7791 }
7792 }
7793
7794 /* Read a Fortran module as type. This DIE can be only a declaration used for
7795 imported module. Still we need that type as local Fortran "use ... only"
7796 declaration imports depend on the created type in determine_prefix. */
7797
7798 static struct type *
7799 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7800 {
7801 struct objfile *objfile = cu->objfile;
7802 char *module_name;
7803 struct type *type;
7804
7805 module_name = dwarf2_name (die, cu);
7806 if (!module_name)
7807 complaint (&symfile_complaints,
7808 _("DW_TAG_module has no name, offset 0x%x"),
7809 die->offset);
7810 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7811
7812 /* determine_prefix uses TYPE_TAG_NAME. */
7813 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7814
7815 return set_die_type (die, type, cu);
7816 }
7817
7818 /* Read a Fortran module. */
7819
7820 static void
7821 read_module (struct die_info *die, struct dwarf2_cu *cu)
7822 {
7823 struct die_info *child_die = die->child;
7824
7825 while (child_die && child_die->tag)
7826 {
7827 process_die (child_die, cu);
7828 child_die = sibling_die (child_die);
7829 }
7830 }
7831
7832 /* Return the name of the namespace represented by DIE. Set
7833 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7834 namespace. */
7835
7836 static const char *
7837 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7838 {
7839 struct die_info *current_die;
7840 const char *name = NULL;
7841
7842 /* Loop through the extensions until we find a name. */
7843
7844 for (current_die = die;
7845 current_die != NULL;
7846 current_die = dwarf2_extension (die, &cu))
7847 {
7848 name = dwarf2_name (current_die, cu);
7849 if (name != NULL)
7850 break;
7851 }
7852
7853 /* Is it an anonymous namespace? */
7854
7855 *is_anonymous = (name == NULL);
7856 if (*is_anonymous)
7857 name = "(anonymous namespace)";
7858
7859 return name;
7860 }
7861
7862 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7863 the user defined type vector. */
7864
7865 static struct type *
7866 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7867 {
7868 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7869 struct comp_unit_head *cu_header = &cu->header;
7870 struct type *type;
7871 struct attribute *attr_byte_size;
7872 struct attribute *attr_address_class;
7873 int byte_size, addr_class;
7874 struct type *target_type;
7875
7876 target_type = die_type (die, cu);
7877
7878 /* The die_type call above may have already set the type for this DIE. */
7879 type = get_die_type (die, cu);
7880 if (type)
7881 return type;
7882
7883 type = lookup_pointer_type (target_type);
7884
7885 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7886 if (attr_byte_size)
7887 byte_size = DW_UNSND (attr_byte_size);
7888 else
7889 byte_size = cu_header->addr_size;
7890
7891 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7892 if (attr_address_class)
7893 addr_class = DW_UNSND (attr_address_class);
7894 else
7895 addr_class = DW_ADDR_none;
7896
7897 /* If the pointer size or address class is different than the
7898 default, create a type variant marked as such and set the
7899 length accordingly. */
7900 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7901 {
7902 if (gdbarch_address_class_type_flags_p (gdbarch))
7903 {
7904 int type_flags;
7905
7906 type_flags = gdbarch_address_class_type_flags
7907 (gdbarch, byte_size, addr_class);
7908 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7909 == 0);
7910 type = make_type_with_address_space (type, type_flags);
7911 }
7912 else if (TYPE_LENGTH (type) != byte_size)
7913 {
7914 complaint (&symfile_complaints,
7915 _("invalid pointer size %d"), byte_size);
7916 }
7917 else
7918 {
7919 /* Should we also complain about unhandled address classes? */
7920 }
7921 }
7922
7923 TYPE_LENGTH (type) = byte_size;
7924 return set_die_type (die, type, cu);
7925 }
7926
7927 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7928 the user defined type vector. */
7929
7930 static struct type *
7931 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7932 {
7933 struct type *type;
7934 struct type *to_type;
7935 struct type *domain;
7936
7937 to_type = die_type (die, cu);
7938 domain = die_containing_type (die, cu);
7939
7940 /* The calls above may have already set the type for this DIE. */
7941 type = get_die_type (die, cu);
7942 if (type)
7943 return type;
7944
7945 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7946 type = lookup_methodptr_type (to_type);
7947 else
7948 type = lookup_memberptr_type (to_type, domain);
7949
7950 return set_die_type (die, type, cu);
7951 }
7952
7953 /* Extract all information from a DW_TAG_reference_type DIE and add to
7954 the user defined type vector. */
7955
7956 static struct type *
7957 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7958 {
7959 struct comp_unit_head *cu_header = &cu->header;
7960 struct type *type, *target_type;
7961 struct attribute *attr;
7962
7963 target_type = die_type (die, cu);
7964
7965 /* The die_type call above may have already set the type for this DIE. */
7966 type = get_die_type (die, cu);
7967 if (type)
7968 return type;
7969
7970 type = lookup_reference_type (target_type);
7971 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7972 if (attr)
7973 {
7974 TYPE_LENGTH (type) = DW_UNSND (attr);
7975 }
7976 else
7977 {
7978 TYPE_LENGTH (type) = cu_header->addr_size;
7979 }
7980 return set_die_type (die, type, cu);
7981 }
7982
7983 static struct type *
7984 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7985 {
7986 struct type *base_type, *cv_type;
7987
7988 base_type = die_type (die, cu);
7989
7990 /* The die_type call above may have already set the type for this DIE. */
7991 cv_type = get_die_type (die, cu);
7992 if (cv_type)
7993 return cv_type;
7994
7995 /* In case the const qualifier is applied to an array type, the element type
7996 is so qualified, not the array type (section 6.7.3 of C99). */
7997 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7998 {
7999 struct type *el_type, *inner_array;
8000
8001 base_type = copy_type (base_type);
8002 inner_array = base_type;
8003
8004 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8005 {
8006 TYPE_TARGET_TYPE (inner_array) =
8007 copy_type (TYPE_TARGET_TYPE (inner_array));
8008 inner_array = TYPE_TARGET_TYPE (inner_array);
8009 }
8010
8011 el_type = TYPE_TARGET_TYPE (inner_array);
8012 TYPE_TARGET_TYPE (inner_array) =
8013 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8014
8015 return set_die_type (die, base_type, cu);
8016 }
8017
8018 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8019 return set_die_type (die, cv_type, cu);
8020 }
8021
8022 static struct type *
8023 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8024 {
8025 struct type *base_type, *cv_type;
8026
8027 base_type = die_type (die, cu);
8028
8029 /* The die_type call above may have already set the type for this DIE. */
8030 cv_type = get_die_type (die, cu);
8031 if (cv_type)
8032 return cv_type;
8033
8034 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8035 return set_die_type (die, cv_type, cu);
8036 }
8037
8038 /* Extract all information from a DW_TAG_string_type DIE and add to
8039 the user defined type vector. It isn't really a user defined type,
8040 but it behaves like one, with other DIE's using an AT_user_def_type
8041 attribute to reference it. */
8042
8043 static struct type *
8044 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8045 {
8046 struct objfile *objfile = cu->objfile;
8047 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8048 struct type *type, *range_type, *index_type, *char_type;
8049 struct attribute *attr;
8050 unsigned int length;
8051
8052 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8053 if (attr)
8054 {
8055 length = DW_UNSND (attr);
8056 }
8057 else
8058 {
8059 /* Check for the DW_AT_byte_size attribute. */
8060 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8061 if (attr)
8062 {
8063 length = DW_UNSND (attr);
8064 }
8065 else
8066 {
8067 length = 1;
8068 }
8069 }
8070
8071 index_type = objfile_type (objfile)->builtin_int;
8072 range_type = create_range_type (NULL, index_type, 1, length);
8073 char_type = language_string_char_type (cu->language_defn, gdbarch);
8074 type = create_string_type (NULL, char_type, range_type);
8075
8076 return set_die_type (die, type, cu);
8077 }
8078
8079 /* Handle DIES due to C code like:
8080
8081 struct foo
8082 {
8083 int (*funcp)(int a, long l);
8084 int b;
8085 };
8086
8087 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8088
8089 static struct type *
8090 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8091 {
8092 struct type *type; /* Type that this function returns. */
8093 struct type *ftype; /* Function that returns above type. */
8094 struct attribute *attr;
8095
8096 type = die_type (die, cu);
8097
8098 /* The die_type call above may have already set the type for this DIE. */
8099 ftype = get_die_type (die, cu);
8100 if (ftype)
8101 return ftype;
8102
8103 ftype = lookup_function_type (type);
8104
8105 /* All functions in C++, Pascal and Java have prototypes. */
8106 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8107 if ((attr && (DW_UNSND (attr) != 0))
8108 || cu->language == language_cplus
8109 || cu->language == language_java
8110 || cu->language == language_pascal)
8111 TYPE_PROTOTYPED (ftype) = 1;
8112 else if (producer_is_realview (cu->producer))
8113 /* RealView does not emit DW_AT_prototyped. We can not
8114 distinguish prototyped and unprototyped functions; default to
8115 prototyped, since that is more common in modern code (and
8116 RealView warns about unprototyped functions). */
8117 TYPE_PROTOTYPED (ftype) = 1;
8118
8119 /* Store the calling convention in the type if it's available in
8120 the subroutine die. Otherwise set the calling convention to
8121 the default value DW_CC_normal. */
8122 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8123 if (attr)
8124 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8125 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8126 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8127 else
8128 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8129
8130 /* We need to add the subroutine type to the die immediately so
8131 we don't infinitely recurse when dealing with parameters
8132 declared as the same subroutine type. */
8133 set_die_type (die, ftype, cu);
8134
8135 if (die->child != NULL)
8136 {
8137 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8138 struct die_info *child_die;
8139 int nparams, iparams;
8140
8141 /* Count the number of parameters.
8142 FIXME: GDB currently ignores vararg functions, but knows about
8143 vararg member functions. */
8144 nparams = 0;
8145 child_die = die->child;
8146 while (child_die && child_die->tag)
8147 {
8148 if (child_die->tag == DW_TAG_formal_parameter)
8149 nparams++;
8150 else if (child_die->tag == DW_TAG_unspecified_parameters)
8151 TYPE_VARARGS (ftype) = 1;
8152 child_die = sibling_die (child_die);
8153 }
8154
8155 /* Allocate storage for parameters and fill them in. */
8156 TYPE_NFIELDS (ftype) = nparams;
8157 TYPE_FIELDS (ftype) = (struct field *)
8158 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8159
8160 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8161 even if we error out during the parameters reading below. */
8162 for (iparams = 0; iparams < nparams; iparams++)
8163 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8164
8165 iparams = 0;
8166 child_die = die->child;
8167 while (child_die && child_die->tag)
8168 {
8169 if (child_die->tag == DW_TAG_formal_parameter)
8170 {
8171 struct type *arg_type;
8172
8173 /* DWARF version 2 has no clean way to discern C++
8174 static and non-static member functions. G++ helps
8175 GDB by marking the first parameter for non-static
8176 member functions (which is the this pointer) as
8177 artificial. We pass this information to
8178 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8179
8180 DWARF version 3 added DW_AT_object_pointer, which GCC
8181 4.5 does not yet generate. */
8182 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8183 if (attr)
8184 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8185 else
8186 {
8187 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8188
8189 /* GCC/43521: In java, the formal parameter
8190 "this" is sometimes not marked with DW_AT_artificial. */
8191 if (cu->language == language_java)
8192 {
8193 const char *name = dwarf2_name (child_die, cu);
8194
8195 if (name && !strcmp (name, "this"))
8196 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8197 }
8198 }
8199 arg_type = die_type (child_die, cu);
8200
8201 /* RealView does not mark THIS as const, which the testsuite
8202 expects. GCC marks THIS as const in method definitions,
8203 but not in the class specifications (GCC PR 43053). */
8204 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8205 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8206 {
8207 int is_this = 0;
8208 struct dwarf2_cu *arg_cu = cu;
8209 const char *name = dwarf2_name (child_die, cu);
8210
8211 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8212 if (attr)
8213 {
8214 /* If the compiler emits this, use it. */
8215 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8216 is_this = 1;
8217 }
8218 else if (name && strcmp (name, "this") == 0)
8219 /* Function definitions will have the argument names. */
8220 is_this = 1;
8221 else if (name == NULL && iparams == 0)
8222 /* Declarations may not have the names, so like
8223 elsewhere in GDB, assume an artificial first
8224 argument is "this". */
8225 is_this = 1;
8226
8227 if (is_this)
8228 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8229 arg_type, 0);
8230 }
8231
8232 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8233 iparams++;
8234 }
8235 child_die = sibling_die (child_die);
8236 }
8237 }
8238
8239 return ftype;
8240 }
8241
8242 static struct type *
8243 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8244 {
8245 struct objfile *objfile = cu->objfile;
8246 const char *name = NULL;
8247 struct type *this_type;
8248
8249 name = dwarf2_full_name (NULL, die, cu);
8250 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8251 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8252 TYPE_NAME (this_type) = (char *) name;
8253 set_die_type (die, this_type, cu);
8254 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8255 return this_type;
8256 }
8257
8258 /* Find a representation of a given base type and install
8259 it in the TYPE field of the die. */
8260
8261 static struct type *
8262 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8263 {
8264 struct objfile *objfile = cu->objfile;
8265 struct type *type;
8266 struct attribute *attr;
8267 int encoding = 0, size = 0;
8268 char *name;
8269 enum type_code code = TYPE_CODE_INT;
8270 int type_flags = 0;
8271 struct type *target_type = NULL;
8272
8273 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8274 if (attr)
8275 {
8276 encoding = DW_UNSND (attr);
8277 }
8278 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8279 if (attr)
8280 {
8281 size = DW_UNSND (attr);
8282 }
8283 name = dwarf2_name (die, cu);
8284 if (!name)
8285 {
8286 complaint (&symfile_complaints,
8287 _("DW_AT_name missing from DW_TAG_base_type"));
8288 }
8289
8290 switch (encoding)
8291 {
8292 case DW_ATE_address:
8293 /* Turn DW_ATE_address into a void * pointer. */
8294 code = TYPE_CODE_PTR;
8295 type_flags |= TYPE_FLAG_UNSIGNED;
8296 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8297 break;
8298 case DW_ATE_boolean:
8299 code = TYPE_CODE_BOOL;
8300 type_flags |= TYPE_FLAG_UNSIGNED;
8301 break;
8302 case DW_ATE_complex_float:
8303 code = TYPE_CODE_COMPLEX;
8304 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8305 break;
8306 case DW_ATE_decimal_float:
8307 code = TYPE_CODE_DECFLOAT;
8308 break;
8309 case DW_ATE_float:
8310 code = TYPE_CODE_FLT;
8311 break;
8312 case DW_ATE_signed:
8313 break;
8314 case DW_ATE_unsigned:
8315 type_flags |= TYPE_FLAG_UNSIGNED;
8316 break;
8317 case DW_ATE_signed_char:
8318 if (cu->language == language_ada || cu->language == language_m2
8319 || cu->language == language_pascal)
8320 code = TYPE_CODE_CHAR;
8321 break;
8322 case DW_ATE_unsigned_char:
8323 if (cu->language == language_ada || cu->language == language_m2
8324 || cu->language == language_pascal)
8325 code = TYPE_CODE_CHAR;
8326 type_flags |= TYPE_FLAG_UNSIGNED;
8327 break;
8328 case DW_ATE_UTF:
8329 /* We just treat this as an integer and then recognize the
8330 type by name elsewhere. */
8331 break;
8332
8333 default:
8334 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8335 dwarf_type_encoding_name (encoding));
8336 break;
8337 }
8338
8339 type = init_type (code, size, type_flags, NULL, objfile);
8340 TYPE_NAME (type) = name;
8341 TYPE_TARGET_TYPE (type) = target_type;
8342
8343 if (name && strcmp (name, "char") == 0)
8344 TYPE_NOSIGN (type) = 1;
8345
8346 return set_die_type (die, type, cu);
8347 }
8348
8349 /* Read the given DW_AT_subrange DIE. */
8350
8351 static struct type *
8352 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8353 {
8354 struct type *base_type;
8355 struct type *range_type;
8356 struct attribute *attr;
8357 LONGEST low = 0;
8358 LONGEST high = -1;
8359 char *name;
8360 LONGEST negative_mask;
8361
8362 base_type = die_type (die, cu);
8363 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8364 check_typedef (base_type);
8365
8366 /* The die_type call above may have already set the type for this DIE. */
8367 range_type = get_die_type (die, cu);
8368 if (range_type)
8369 return range_type;
8370
8371 if (cu->language == language_fortran)
8372 {
8373 /* FORTRAN implies a lower bound of 1, if not given. */
8374 low = 1;
8375 }
8376
8377 /* FIXME: For variable sized arrays either of these could be
8378 a variable rather than a constant value. We'll allow it,
8379 but we don't know how to handle it. */
8380 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8381 if (attr)
8382 low = dwarf2_get_attr_constant_value (attr, 0);
8383
8384 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8385 if (attr)
8386 {
8387 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8388 {
8389 /* GCC encodes arrays with unspecified or dynamic length
8390 with a DW_FORM_block1 attribute or a reference attribute.
8391 FIXME: GDB does not yet know how to handle dynamic
8392 arrays properly, treat them as arrays with unspecified
8393 length for now.
8394
8395 FIXME: jimb/2003-09-22: GDB does not really know
8396 how to handle arrays of unspecified length
8397 either; we just represent them as zero-length
8398 arrays. Choose an appropriate upper bound given
8399 the lower bound we've computed above. */
8400 high = low - 1;
8401 }
8402 else
8403 high = dwarf2_get_attr_constant_value (attr, 1);
8404 }
8405 else
8406 {
8407 attr = dwarf2_attr (die, DW_AT_count, cu);
8408 if (attr)
8409 {
8410 int count = dwarf2_get_attr_constant_value (attr, 1);
8411 high = low + count - 1;
8412 }
8413 else
8414 {
8415 /* Unspecified array length. */
8416 high = low - 1;
8417 }
8418 }
8419
8420 /* Dwarf-2 specifications explicitly allows to create subrange types
8421 without specifying a base type.
8422 In that case, the base type must be set to the type of
8423 the lower bound, upper bound or count, in that order, if any of these
8424 three attributes references an object that has a type.
8425 If no base type is found, the Dwarf-2 specifications say that
8426 a signed integer type of size equal to the size of an address should
8427 be used.
8428 For the following C code: `extern char gdb_int [];'
8429 GCC produces an empty range DIE.
8430 FIXME: muller/2010-05-28: Possible references to object for low bound,
8431 high bound or count are not yet handled by this code. */
8432 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8433 {
8434 struct objfile *objfile = cu->objfile;
8435 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8436 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8437 struct type *int_type = objfile_type (objfile)->builtin_int;
8438
8439 /* Test "int", "long int", and "long long int" objfile types,
8440 and select the first one having a size above or equal to the
8441 architecture address size. */
8442 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8443 base_type = int_type;
8444 else
8445 {
8446 int_type = objfile_type (objfile)->builtin_long;
8447 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8448 base_type = int_type;
8449 else
8450 {
8451 int_type = objfile_type (objfile)->builtin_long_long;
8452 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8453 base_type = int_type;
8454 }
8455 }
8456 }
8457
8458 negative_mask =
8459 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8460 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8461 low |= negative_mask;
8462 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8463 high |= negative_mask;
8464
8465 range_type = create_range_type (NULL, base_type, low, high);
8466
8467 /* Mark arrays with dynamic length at least as an array of unspecified
8468 length. GDB could check the boundary but before it gets implemented at
8469 least allow accessing the array elements. */
8470 if (attr && attr->form == DW_FORM_block1)
8471 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8472
8473 /* Ada expects an empty array on no boundary attributes. */
8474 if (attr == NULL && cu->language != language_ada)
8475 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8476
8477 name = dwarf2_name (die, cu);
8478 if (name)
8479 TYPE_NAME (range_type) = name;
8480
8481 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8482 if (attr)
8483 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8484
8485 set_die_type (die, range_type, cu);
8486
8487 /* set_die_type should be already done. */
8488 set_descriptive_type (range_type, die, cu);
8489
8490 return range_type;
8491 }
8492
8493 static struct type *
8494 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8495 {
8496 struct type *type;
8497
8498 /* For now, we only support the C meaning of an unspecified type: void. */
8499
8500 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8501 TYPE_NAME (type) = dwarf2_name (die, cu);
8502
8503 return set_die_type (die, type, cu);
8504 }
8505
8506 /* Trivial hash function for die_info: the hash value of a DIE
8507 is its offset in .debug_info for this objfile. */
8508
8509 static hashval_t
8510 die_hash (const void *item)
8511 {
8512 const struct die_info *die = item;
8513
8514 return die->offset;
8515 }
8516
8517 /* Trivial comparison function for die_info structures: two DIEs
8518 are equal if they have the same offset. */
8519
8520 static int
8521 die_eq (const void *item_lhs, const void *item_rhs)
8522 {
8523 const struct die_info *die_lhs = item_lhs;
8524 const struct die_info *die_rhs = item_rhs;
8525
8526 return die_lhs->offset == die_rhs->offset;
8527 }
8528
8529 /* Read a whole compilation unit into a linked list of dies. */
8530
8531 static struct die_info *
8532 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8533 {
8534 struct die_reader_specs reader_specs;
8535 int read_abbrevs = 0;
8536 struct cleanup *back_to = NULL;
8537 struct die_info *die;
8538
8539 if (cu->dwarf2_abbrevs == NULL)
8540 {
8541 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8542 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8543 read_abbrevs = 1;
8544 }
8545
8546 gdb_assert (cu->die_hash == NULL);
8547 cu->die_hash
8548 = htab_create_alloc_ex (cu->header.length / 12,
8549 die_hash,
8550 die_eq,
8551 NULL,
8552 &cu->comp_unit_obstack,
8553 hashtab_obstack_allocate,
8554 dummy_obstack_deallocate);
8555
8556 init_cu_die_reader (&reader_specs, cu);
8557
8558 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8559
8560 if (read_abbrevs)
8561 do_cleanups (back_to);
8562
8563 return die;
8564 }
8565
8566 /* Main entry point for reading a DIE and all children.
8567 Read the DIE and dump it if requested. */
8568
8569 static struct die_info *
8570 read_die_and_children (const struct die_reader_specs *reader,
8571 gdb_byte *info_ptr,
8572 gdb_byte **new_info_ptr,
8573 struct die_info *parent)
8574 {
8575 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8576 new_info_ptr, parent);
8577
8578 if (dwarf2_die_debug)
8579 {
8580 fprintf_unfiltered (gdb_stdlog,
8581 "\nRead die from %s of %s:\n",
8582 reader->buffer == dwarf2_per_objfile->info.buffer
8583 ? ".debug_info"
8584 : reader->buffer == dwarf2_per_objfile->types.buffer
8585 ? ".debug_types"
8586 : "unknown section",
8587 reader->abfd->filename);
8588 dump_die (result, dwarf2_die_debug);
8589 }
8590
8591 return result;
8592 }
8593
8594 /* Read a single die and all its descendents. Set the die's sibling
8595 field to NULL; set other fields in the die correctly, and set all
8596 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8597 location of the info_ptr after reading all of those dies. PARENT
8598 is the parent of the die in question. */
8599
8600 static struct die_info *
8601 read_die_and_children_1 (const struct die_reader_specs *reader,
8602 gdb_byte *info_ptr,
8603 gdb_byte **new_info_ptr,
8604 struct die_info *parent)
8605 {
8606 struct die_info *die;
8607 gdb_byte *cur_ptr;
8608 int has_children;
8609
8610 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8611 if (die == NULL)
8612 {
8613 *new_info_ptr = cur_ptr;
8614 return NULL;
8615 }
8616 store_in_ref_table (die, reader->cu);
8617
8618 if (has_children)
8619 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8620 else
8621 {
8622 die->child = NULL;
8623 *new_info_ptr = cur_ptr;
8624 }
8625
8626 die->sibling = NULL;
8627 die->parent = parent;
8628 return die;
8629 }
8630
8631 /* Read a die, all of its descendents, and all of its siblings; set
8632 all of the fields of all of the dies correctly. Arguments are as
8633 in read_die_and_children. */
8634
8635 static struct die_info *
8636 read_die_and_siblings (const struct die_reader_specs *reader,
8637 gdb_byte *info_ptr,
8638 gdb_byte **new_info_ptr,
8639 struct die_info *parent)
8640 {
8641 struct die_info *first_die, *last_sibling;
8642 gdb_byte *cur_ptr;
8643
8644 cur_ptr = info_ptr;
8645 first_die = last_sibling = NULL;
8646
8647 while (1)
8648 {
8649 struct die_info *die
8650 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8651
8652 if (die == NULL)
8653 {
8654 *new_info_ptr = cur_ptr;
8655 return first_die;
8656 }
8657
8658 if (!first_die)
8659 first_die = die;
8660 else
8661 last_sibling->sibling = die;
8662
8663 last_sibling = die;
8664 }
8665 }
8666
8667 /* Read the die from the .debug_info section buffer. Set DIEP to
8668 point to a newly allocated die with its information, except for its
8669 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8670 whether the die has children or not. */
8671
8672 static gdb_byte *
8673 read_full_die (const struct die_reader_specs *reader,
8674 struct die_info **diep, gdb_byte *info_ptr,
8675 int *has_children)
8676 {
8677 unsigned int abbrev_number, bytes_read, i, offset;
8678 struct abbrev_info *abbrev;
8679 struct die_info *die;
8680 struct dwarf2_cu *cu = reader->cu;
8681 bfd *abfd = reader->abfd;
8682
8683 offset = info_ptr - reader->buffer;
8684 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8685 info_ptr += bytes_read;
8686 if (!abbrev_number)
8687 {
8688 *diep = NULL;
8689 *has_children = 0;
8690 return info_ptr;
8691 }
8692
8693 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8694 if (!abbrev)
8695 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8696 abbrev_number,
8697 bfd_get_filename (abfd));
8698
8699 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8700 die->offset = offset;
8701 die->tag = abbrev->tag;
8702 die->abbrev = abbrev_number;
8703
8704 die->num_attrs = abbrev->num_attrs;
8705
8706 for (i = 0; i < abbrev->num_attrs; ++i)
8707 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8708 abfd, info_ptr, cu);
8709
8710 *diep = die;
8711 *has_children = abbrev->has_children;
8712 return info_ptr;
8713 }
8714
8715 /* In DWARF version 2, the description of the debugging information is
8716 stored in a separate .debug_abbrev section. Before we read any
8717 dies from a section we read in all abbreviations and install them
8718 in a hash table. This function also sets flags in CU describing
8719 the data found in the abbrev table. */
8720
8721 static void
8722 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8723 {
8724 struct comp_unit_head *cu_header = &cu->header;
8725 gdb_byte *abbrev_ptr;
8726 struct abbrev_info *cur_abbrev;
8727 unsigned int abbrev_number, bytes_read, abbrev_name;
8728 unsigned int abbrev_form, hash_number;
8729 struct attr_abbrev *cur_attrs;
8730 unsigned int allocated_attrs;
8731
8732 /* Initialize dwarf2 abbrevs. */
8733 obstack_init (&cu->abbrev_obstack);
8734 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8735 (ABBREV_HASH_SIZE
8736 * sizeof (struct abbrev_info *)));
8737 memset (cu->dwarf2_abbrevs, 0,
8738 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8739
8740 dwarf2_read_section (dwarf2_per_objfile->objfile,
8741 &dwarf2_per_objfile->abbrev);
8742 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8743 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8744 abbrev_ptr += bytes_read;
8745
8746 allocated_attrs = ATTR_ALLOC_CHUNK;
8747 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8748
8749 /* Loop until we reach an abbrev number of 0. */
8750 while (abbrev_number)
8751 {
8752 cur_abbrev = dwarf_alloc_abbrev (cu);
8753
8754 /* read in abbrev header */
8755 cur_abbrev->number = abbrev_number;
8756 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8757 abbrev_ptr += bytes_read;
8758 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8759 abbrev_ptr += 1;
8760
8761 if (cur_abbrev->tag == DW_TAG_namespace)
8762 cu->has_namespace_info = 1;
8763
8764 /* now read in declarations */
8765 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8766 abbrev_ptr += bytes_read;
8767 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8768 abbrev_ptr += bytes_read;
8769 while (abbrev_name)
8770 {
8771 if (cur_abbrev->num_attrs == allocated_attrs)
8772 {
8773 allocated_attrs += ATTR_ALLOC_CHUNK;
8774 cur_attrs
8775 = xrealloc (cur_attrs, (allocated_attrs
8776 * sizeof (struct attr_abbrev)));
8777 }
8778
8779 /* Record whether this compilation unit might have
8780 inter-compilation-unit references. If we don't know what form
8781 this attribute will have, then it might potentially be a
8782 DW_FORM_ref_addr, so we conservatively expect inter-CU
8783 references. */
8784
8785 if (abbrev_form == DW_FORM_ref_addr
8786 || abbrev_form == DW_FORM_indirect)
8787 cu->has_form_ref_addr = 1;
8788
8789 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8790 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8791 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8792 abbrev_ptr += bytes_read;
8793 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8794 abbrev_ptr += bytes_read;
8795 }
8796
8797 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8798 (cur_abbrev->num_attrs
8799 * sizeof (struct attr_abbrev)));
8800 memcpy (cur_abbrev->attrs, cur_attrs,
8801 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8802
8803 hash_number = abbrev_number % ABBREV_HASH_SIZE;
8804 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8805 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8806
8807 /* Get next abbreviation.
8808 Under Irix6 the abbreviations for a compilation unit are not
8809 always properly terminated with an abbrev number of 0.
8810 Exit loop if we encounter an abbreviation which we have
8811 already read (which means we are about to read the abbreviations
8812 for the next compile unit) or if the end of the abbreviation
8813 table is reached. */
8814 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8815 >= dwarf2_per_objfile->abbrev.size)
8816 break;
8817 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8818 abbrev_ptr += bytes_read;
8819 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8820 break;
8821 }
8822
8823 xfree (cur_attrs);
8824 }
8825
8826 /* Release the memory used by the abbrev table for a compilation unit. */
8827
8828 static void
8829 dwarf2_free_abbrev_table (void *ptr_to_cu)
8830 {
8831 struct dwarf2_cu *cu = ptr_to_cu;
8832
8833 obstack_free (&cu->abbrev_obstack, NULL);
8834 cu->dwarf2_abbrevs = NULL;
8835 }
8836
8837 /* Lookup an abbrev_info structure in the abbrev hash table. */
8838
8839 static struct abbrev_info *
8840 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8841 {
8842 unsigned int hash_number;
8843 struct abbrev_info *abbrev;
8844
8845 hash_number = number % ABBREV_HASH_SIZE;
8846 abbrev = cu->dwarf2_abbrevs[hash_number];
8847
8848 while (abbrev)
8849 {
8850 if (abbrev->number == number)
8851 return abbrev;
8852 else
8853 abbrev = abbrev->next;
8854 }
8855 return NULL;
8856 }
8857
8858 /* Returns nonzero if TAG represents a type that we might generate a partial
8859 symbol for. */
8860
8861 static int
8862 is_type_tag_for_partial (int tag)
8863 {
8864 switch (tag)
8865 {
8866 #if 0
8867 /* Some types that would be reasonable to generate partial symbols for,
8868 that we don't at present. */
8869 case DW_TAG_array_type:
8870 case DW_TAG_file_type:
8871 case DW_TAG_ptr_to_member_type:
8872 case DW_TAG_set_type:
8873 case DW_TAG_string_type:
8874 case DW_TAG_subroutine_type:
8875 #endif
8876 case DW_TAG_base_type:
8877 case DW_TAG_class_type:
8878 case DW_TAG_interface_type:
8879 case DW_TAG_enumeration_type:
8880 case DW_TAG_structure_type:
8881 case DW_TAG_subrange_type:
8882 case DW_TAG_typedef:
8883 case DW_TAG_union_type:
8884 return 1;
8885 default:
8886 return 0;
8887 }
8888 }
8889
8890 /* Load all DIEs that are interesting for partial symbols into memory. */
8891
8892 static struct partial_die_info *
8893 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8894 int building_psymtab, struct dwarf2_cu *cu)
8895 {
8896 struct partial_die_info *part_die;
8897 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8898 struct abbrev_info *abbrev;
8899 unsigned int bytes_read;
8900 unsigned int load_all = 0;
8901
8902 int nesting_level = 1;
8903
8904 parent_die = NULL;
8905 last_die = NULL;
8906
8907 if (cu->per_cu && cu->per_cu->load_all_dies)
8908 load_all = 1;
8909
8910 cu->partial_dies
8911 = htab_create_alloc_ex (cu->header.length / 12,
8912 partial_die_hash,
8913 partial_die_eq,
8914 NULL,
8915 &cu->comp_unit_obstack,
8916 hashtab_obstack_allocate,
8917 dummy_obstack_deallocate);
8918
8919 part_die = obstack_alloc (&cu->comp_unit_obstack,
8920 sizeof (struct partial_die_info));
8921
8922 while (1)
8923 {
8924 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8925
8926 /* A NULL abbrev means the end of a series of children. */
8927 if (abbrev == NULL)
8928 {
8929 if (--nesting_level == 0)
8930 {
8931 /* PART_DIE was probably the last thing allocated on the
8932 comp_unit_obstack, so we could call obstack_free
8933 here. We don't do that because the waste is small,
8934 and will be cleaned up when we're done with this
8935 compilation unit. This way, we're also more robust
8936 against other users of the comp_unit_obstack. */
8937 return first_die;
8938 }
8939 info_ptr += bytes_read;
8940 last_die = parent_die;
8941 parent_die = parent_die->die_parent;
8942 continue;
8943 }
8944
8945 /* Check for template arguments. We never save these; if
8946 they're seen, we just mark the parent, and go on our way. */
8947 if (parent_die != NULL
8948 && cu->language == language_cplus
8949 && (abbrev->tag == DW_TAG_template_type_param
8950 || abbrev->tag == DW_TAG_template_value_param))
8951 {
8952 parent_die->has_template_arguments = 1;
8953
8954 if (!load_all)
8955 {
8956 /* We don't need a partial DIE for the template argument. */
8957 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8958 cu);
8959 continue;
8960 }
8961 }
8962
8963 /* We only recurse into subprograms looking for template arguments.
8964 Skip their other children. */
8965 if (!load_all
8966 && cu->language == language_cplus
8967 && parent_die != NULL
8968 && parent_die->tag == DW_TAG_subprogram)
8969 {
8970 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8971 continue;
8972 }
8973
8974 /* Check whether this DIE is interesting enough to save. Normally
8975 we would not be interested in members here, but there may be
8976 later variables referencing them via DW_AT_specification (for
8977 static members). */
8978 if (!load_all
8979 && !is_type_tag_for_partial (abbrev->tag)
8980 && abbrev->tag != DW_TAG_constant
8981 && abbrev->tag != DW_TAG_enumerator
8982 && abbrev->tag != DW_TAG_subprogram
8983 && abbrev->tag != DW_TAG_lexical_block
8984 && abbrev->tag != DW_TAG_variable
8985 && abbrev->tag != DW_TAG_namespace
8986 && abbrev->tag != DW_TAG_module
8987 && abbrev->tag != DW_TAG_member)
8988 {
8989 /* Otherwise we skip to the next sibling, if any. */
8990 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8991 continue;
8992 }
8993
8994 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8995 buffer, info_ptr, cu);
8996
8997 /* This two-pass algorithm for processing partial symbols has a
8998 high cost in cache pressure. Thus, handle some simple cases
8999 here which cover the majority of C partial symbols. DIEs
9000 which neither have specification tags in them, nor could have
9001 specification tags elsewhere pointing at them, can simply be
9002 processed and discarded.
9003
9004 This segment is also optional; scan_partial_symbols and
9005 add_partial_symbol will handle these DIEs if we chain
9006 them in normally. When compilers which do not emit large
9007 quantities of duplicate debug information are more common,
9008 this code can probably be removed. */
9009
9010 /* Any complete simple types at the top level (pretty much all
9011 of them, for a language without namespaces), can be processed
9012 directly. */
9013 if (parent_die == NULL
9014 && part_die->has_specification == 0
9015 && part_die->is_declaration == 0
9016 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9017 || part_die->tag == DW_TAG_base_type
9018 || part_die->tag == DW_TAG_subrange_type))
9019 {
9020 if (building_psymtab && part_die->name != NULL)
9021 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9022 VAR_DOMAIN, LOC_TYPEDEF,
9023 &cu->objfile->static_psymbols,
9024 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9025 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9026 continue;
9027 }
9028
9029 /* The exception for DW_TAG_typedef with has_children above is
9030 a workaround of GCC PR debug/47510. In the case of this complaint
9031 type_name_no_tag_or_error will error on such types later.
9032
9033 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9034 it could not find the child DIEs referenced later, this is checked
9035 above. In correct DWARF DW_TAG_typedef should have no children. */
9036
9037 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9038 complaint (&symfile_complaints,
9039 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9040 "- DIE at 0x%x [in module %s]"),
9041 part_die->offset, cu->objfile->name);
9042
9043 /* If we're at the second level, and we're an enumerator, and
9044 our parent has no specification (meaning possibly lives in a
9045 namespace elsewhere), then we can add the partial symbol now
9046 instead of queueing it. */
9047 if (part_die->tag == DW_TAG_enumerator
9048 && parent_die != NULL
9049 && parent_die->die_parent == NULL
9050 && parent_die->tag == DW_TAG_enumeration_type
9051 && parent_die->has_specification == 0)
9052 {
9053 if (part_die->name == NULL)
9054 complaint (&symfile_complaints,
9055 _("malformed enumerator DIE ignored"));
9056 else if (building_psymtab)
9057 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9058 VAR_DOMAIN, LOC_CONST,
9059 (cu->language == language_cplus
9060 || cu->language == language_java)
9061 ? &cu->objfile->global_psymbols
9062 : &cu->objfile->static_psymbols,
9063 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9064
9065 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9066 continue;
9067 }
9068
9069 /* We'll save this DIE so link it in. */
9070 part_die->die_parent = parent_die;
9071 part_die->die_sibling = NULL;
9072 part_die->die_child = NULL;
9073
9074 if (last_die && last_die == parent_die)
9075 last_die->die_child = part_die;
9076 else if (last_die)
9077 last_die->die_sibling = part_die;
9078
9079 last_die = part_die;
9080
9081 if (first_die == NULL)
9082 first_die = part_die;
9083
9084 /* Maybe add the DIE to the hash table. Not all DIEs that we
9085 find interesting need to be in the hash table, because we
9086 also have the parent/sibling/child chains; only those that we
9087 might refer to by offset later during partial symbol reading.
9088
9089 For now this means things that might have be the target of a
9090 DW_AT_specification, DW_AT_abstract_origin, or
9091 DW_AT_extension. DW_AT_extension will refer only to
9092 namespaces; DW_AT_abstract_origin refers to functions (and
9093 many things under the function DIE, but we do not recurse
9094 into function DIEs during partial symbol reading) and
9095 possibly variables as well; DW_AT_specification refers to
9096 declarations. Declarations ought to have the DW_AT_declaration
9097 flag. It happens that GCC forgets to put it in sometimes, but
9098 only for functions, not for types.
9099
9100 Adding more things than necessary to the hash table is harmless
9101 except for the performance cost. Adding too few will result in
9102 wasted time in find_partial_die, when we reread the compilation
9103 unit with load_all_dies set. */
9104
9105 if (load_all
9106 || abbrev->tag == DW_TAG_constant
9107 || abbrev->tag == DW_TAG_subprogram
9108 || abbrev->tag == DW_TAG_variable
9109 || abbrev->tag == DW_TAG_namespace
9110 || part_die->is_declaration)
9111 {
9112 void **slot;
9113
9114 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9115 part_die->offset, INSERT);
9116 *slot = part_die;
9117 }
9118
9119 part_die = obstack_alloc (&cu->comp_unit_obstack,
9120 sizeof (struct partial_die_info));
9121
9122 /* For some DIEs we want to follow their children (if any). For C
9123 we have no reason to follow the children of structures; for other
9124 languages we have to, so that we can get at method physnames
9125 to infer fully qualified class names, for DW_AT_specification,
9126 and for C++ template arguments. For C++, we also look one level
9127 inside functions to find template arguments (if the name of the
9128 function does not already contain the template arguments).
9129
9130 For Ada, we need to scan the children of subprograms and lexical
9131 blocks as well because Ada allows the definition of nested
9132 entities that could be interesting for the debugger, such as
9133 nested subprograms for instance. */
9134 if (last_die->has_children
9135 && (load_all
9136 || last_die->tag == DW_TAG_namespace
9137 || last_die->tag == DW_TAG_module
9138 || last_die->tag == DW_TAG_enumeration_type
9139 || (cu->language == language_cplus
9140 && last_die->tag == DW_TAG_subprogram
9141 && (last_die->name == NULL
9142 || strchr (last_die->name, '<') == NULL))
9143 || (cu->language != language_c
9144 && (last_die->tag == DW_TAG_class_type
9145 || last_die->tag == DW_TAG_interface_type
9146 || last_die->tag == DW_TAG_structure_type
9147 || last_die->tag == DW_TAG_union_type))
9148 || (cu->language == language_ada
9149 && (last_die->tag == DW_TAG_subprogram
9150 || last_die->tag == DW_TAG_lexical_block))))
9151 {
9152 nesting_level++;
9153 parent_die = last_die;
9154 continue;
9155 }
9156
9157 /* Otherwise we skip to the next sibling, if any. */
9158 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9159
9160 /* Back to the top, do it again. */
9161 }
9162 }
9163
9164 /* Read a minimal amount of information into the minimal die structure. */
9165
9166 static gdb_byte *
9167 read_partial_die (struct partial_die_info *part_die,
9168 struct abbrev_info *abbrev,
9169 unsigned int abbrev_len, bfd *abfd,
9170 gdb_byte *buffer, gdb_byte *info_ptr,
9171 struct dwarf2_cu *cu)
9172 {
9173 unsigned int i;
9174 struct attribute attr;
9175 int has_low_pc_attr = 0;
9176 int has_high_pc_attr = 0;
9177
9178 memset (part_die, 0, sizeof (struct partial_die_info));
9179
9180 part_die->offset = info_ptr - buffer;
9181
9182 info_ptr += abbrev_len;
9183
9184 if (abbrev == NULL)
9185 return info_ptr;
9186
9187 part_die->tag = abbrev->tag;
9188 part_die->has_children = abbrev->has_children;
9189
9190 for (i = 0; i < abbrev->num_attrs; ++i)
9191 {
9192 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9193
9194 /* Store the data if it is of an attribute we want to keep in a
9195 partial symbol table. */
9196 switch (attr.name)
9197 {
9198 case DW_AT_name:
9199 switch (part_die->tag)
9200 {
9201 case DW_TAG_compile_unit:
9202 case DW_TAG_type_unit:
9203 /* Compilation units have a DW_AT_name that is a filename, not
9204 a source language identifier. */
9205 case DW_TAG_enumeration_type:
9206 case DW_TAG_enumerator:
9207 /* These tags always have simple identifiers already; no need
9208 to canonicalize them. */
9209 part_die->name = DW_STRING (&attr);
9210 break;
9211 default:
9212 part_die->name
9213 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9214 &cu->objfile->objfile_obstack);
9215 break;
9216 }
9217 break;
9218 case DW_AT_linkage_name:
9219 case DW_AT_MIPS_linkage_name:
9220 /* Note that both forms of linkage name might appear. We
9221 assume they will be the same, and we only store the last
9222 one we see. */
9223 if (cu->language == language_ada)
9224 part_die->name = DW_STRING (&attr);
9225 part_die->linkage_name = DW_STRING (&attr);
9226 break;
9227 case DW_AT_low_pc:
9228 has_low_pc_attr = 1;
9229 part_die->lowpc = DW_ADDR (&attr);
9230 break;
9231 case DW_AT_high_pc:
9232 has_high_pc_attr = 1;
9233 part_die->highpc = DW_ADDR (&attr);
9234 break;
9235 case DW_AT_location:
9236 /* Support the .debug_loc offsets. */
9237 if (attr_form_is_block (&attr))
9238 {
9239 part_die->locdesc = DW_BLOCK (&attr);
9240 }
9241 else if (attr_form_is_section_offset (&attr))
9242 {
9243 dwarf2_complex_location_expr_complaint ();
9244 }
9245 else
9246 {
9247 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9248 "partial symbol information");
9249 }
9250 break;
9251 case DW_AT_external:
9252 part_die->is_external = DW_UNSND (&attr);
9253 break;
9254 case DW_AT_declaration:
9255 part_die->is_declaration = DW_UNSND (&attr);
9256 break;
9257 case DW_AT_type:
9258 part_die->has_type = 1;
9259 break;
9260 case DW_AT_abstract_origin:
9261 case DW_AT_specification:
9262 case DW_AT_extension:
9263 part_die->has_specification = 1;
9264 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9265 break;
9266 case DW_AT_sibling:
9267 /* Ignore absolute siblings, they might point outside of
9268 the current compile unit. */
9269 if (attr.form == DW_FORM_ref_addr)
9270 complaint (&symfile_complaints,
9271 _("ignoring absolute DW_AT_sibling"));
9272 else
9273 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9274 break;
9275 case DW_AT_byte_size:
9276 part_die->has_byte_size = 1;
9277 break;
9278 case DW_AT_calling_convention:
9279 /* DWARF doesn't provide a way to identify a program's source-level
9280 entry point. DW_AT_calling_convention attributes are only meant
9281 to describe functions' calling conventions.
9282
9283 However, because it's a necessary piece of information in
9284 Fortran, and because DW_CC_program is the only piece of debugging
9285 information whose definition refers to a 'main program' at all,
9286 several compilers have begun marking Fortran main programs with
9287 DW_CC_program --- even when those functions use the standard
9288 calling conventions.
9289
9290 So until DWARF specifies a way to provide this information and
9291 compilers pick up the new representation, we'll support this
9292 practice. */
9293 if (DW_UNSND (&attr) == DW_CC_program
9294 && cu->language == language_fortran)
9295 {
9296 set_main_name (part_die->name);
9297
9298 /* As this DIE has a static linkage the name would be difficult
9299 to look up later. */
9300 language_of_main = language_fortran;
9301 }
9302 break;
9303 default:
9304 break;
9305 }
9306 }
9307
9308 if (has_low_pc_attr && has_high_pc_attr)
9309 {
9310 /* When using the GNU linker, .gnu.linkonce. sections are used to
9311 eliminate duplicate copies of functions and vtables and such.
9312 The linker will arbitrarily choose one and discard the others.
9313 The AT_*_pc values for such functions refer to local labels in
9314 these sections. If the section from that file was discarded, the
9315 labels are not in the output, so the relocs get a value of 0.
9316 If this is a discarded function, mark the pc bounds as invalid,
9317 so that GDB will ignore it. */
9318 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9319 {
9320 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9321
9322 complaint (&symfile_complaints,
9323 _("DW_AT_low_pc %s is zero "
9324 "for DIE at 0x%x [in module %s]"),
9325 paddress (gdbarch, part_die->lowpc),
9326 part_die->offset, cu->objfile->name);
9327 }
9328 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9329 else if (part_die->lowpc >= part_die->highpc)
9330 {
9331 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9332
9333 complaint (&symfile_complaints,
9334 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9335 "for DIE at 0x%x [in module %s]"),
9336 paddress (gdbarch, part_die->lowpc),
9337 paddress (gdbarch, part_die->highpc),
9338 part_die->offset, cu->objfile->name);
9339 }
9340 else
9341 part_die->has_pc_info = 1;
9342 }
9343
9344 return info_ptr;
9345 }
9346
9347 /* Find a cached partial DIE at OFFSET in CU. */
9348
9349 static struct partial_die_info *
9350 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9351 {
9352 struct partial_die_info *lookup_die = NULL;
9353 struct partial_die_info part_die;
9354
9355 part_die.offset = offset;
9356 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9357
9358 return lookup_die;
9359 }
9360
9361 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9362 except in the case of .debug_types DIEs which do not reference
9363 outside their CU (they do however referencing other types via
9364 DW_FORM_ref_sig8). */
9365
9366 static struct partial_die_info *
9367 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9368 {
9369 struct dwarf2_per_cu_data *per_cu = NULL;
9370 struct partial_die_info *pd = NULL;
9371
9372 if (cu->per_cu->from_debug_types)
9373 {
9374 pd = find_partial_die_in_comp_unit (offset, cu);
9375 if (pd != NULL)
9376 return pd;
9377 goto not_found;
9378 }
9379
9380 if (offset_in_cu_p (&cu->header, offset))
9381 {
9382 pd = find_partial_die_in_comp_unit (offset, cu);
9383 if (pd != NULL)
9384 return pd;
9385 }
9386
9387 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9388
9389 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9390 load_partial_comp_unit (per_cu, cu->objfile);
9391
9392 per_cu->cu->last_used = 0;
9393 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9394
9395 if (pd == NULL && per_cu->load_all_dies == 0)
9396 {
9397 struct cleanup *back_to;
9398 struct partial_die_info comp_unit_die;
9399 struct abbrev_info *abbrev;
9400 unsigned int bytes_read;
9401 char *info_ptr;
9402
9403 per_cu->load_all_dies = 1;
9404
9405 /* Re-read the DIEs. */
9406 back_to = make_cleanup (null_cleanup, 0);
9407 if (per_cu->cu->dwarf2_abbrevs == NULL)
9408 {
9409 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9410 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9411 }
9412 info_ptr = (dwarf2_per_objfile->info.buffer
9413 + per_cu->cu->header.offset
9414 + per_cu->cu->header.first_die_offset);
9415 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9416 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9417 per_cu->cu->objfile->obfd,
9418 dwarf2_per_objfile->info.buffer, info_ptr,
9419 per_cu->cu);
9420 if (comp_unit_die.has_children)
9421 load_partial_dies (per_cu->cu->objfile->obfd,
9422 dwarf2_per_objfile->info.buffer, info_ptr,
9423 0, per_cu->cu);
9424 do_cleanups (back_to);
9425
9426 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9427 }
9428
9429 not_found:
9430
9431 if (pd == NULL)
9432 internal_error (__FILE__, __LINE__,
9433 _("could not find partial DIE 0x%x "
9434 "in cache [from module %s]\n"),
9435 offset, bfd_get_filename (cu->objfile->obfd));
9436 return pd;
9437 }
9438
9439 /* See if we can figure out if the class lives in a namespace. We do
9440 this by looking for a member function; its demangled name will
9441 contain namespace info, if there is any. */
9442
9443 static void
9444 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9445 struct dwarf2_cu *cu)
9446 {
9447 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9448 what template types look like, because the demangler
9449 frequently doesn't give the same name as the debug info. We
9450 could fix this by only using the demangled name to get the
9451 prefix (but see comment in read_structure_type). */
9452
9453 struct partial_die_info *real_pdi;
9454 struct partial_die_info *child_pdi;
9455
9456 /* If this DIE (this DIE's specification, if any) has a parent, then
9457 we should not do this. We'll prepend the parent's fully qualified
9458 name when we create the partial symbol. */
9459
9460 real_pdi = struct_pdi;
9461 while (real_pdi->has_specification)
9462 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9463
9464 if (real_pdi->die_parent != NULL)
9465 return;
9466
9467 for (child_pdi = struct_pdi->die_child;
9468 child_pdi != NULL;
9469 child_pdi = child_pdi->die_sibling)
9470 {
9471 if (child_pdi->tag == DW_TAG_subprogram
9472 && child_pdi->linkage_name != NULL)
9473 {
9474 char *actual_class_name
9475 = language_class_name_from_physname (cu->language_defn,
9476 child_pdi->linkage_name);
9477 if (actual_class_name != NULL)
9478 {
9479 struct_pdi->name
9480 = obsavestring (actual_class_name,
9481 strlen (actual_class_name),
9482 &cu->objfile->objfile_obstack);
9483 xfree (actual_class_name);
9484 }
9485 break;
9486 }
9487 }
9488 }
9489
9490 /* Adjust PART_DIE before generating a symbol for it. This function
9491 may set the is_external flag or change the DIE's name. */
9492
9493 static void
9494 fixup_partial_die (struct partial_die_info *part_die,
9495 struct dwarf2_cu *cu)
9496 {
9497 /* Once we've fixed up a die, there's no point in doing so again.
9498 This also avoids a memory leak if we were to call
9499 guess_partial_die_structure_name multiple times. */
9500 if (part_die->fixup_called)
9501 return;
9502
9503 /* If we found a reference attribute and the DIE has no name, try
9504 to find a name in the referred to DIE. */
9505
9506 if (part_die->name == NULL && part_die->has_specification)
9507 {
9508 struct partial_die_info *spec_die;
9509
9510 spec_die = find_partial_die (part_die->spec_offset, cu);
9511
9512 fixup_partial_die (spec_die, cu);
9513
9514 if (spec_die->name)
9515 {
9516 part_die->name = spec_die->name;
9517
9518 /* Copy DW_AT_external attribute if it is set. */
9519 if (spec_die->is_external)
9520 part_die->is_external = spec_die->is_external;
9521 }
9522 }
9523
9524 /* Set default names for some unnamed DIEs. */
9525
9526 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9527 part_die->name = "(anonymous namespace)";
9528
9529 /* If there is no parent die to provide a namespace, and there are
9530 children, see if we can determine the namespace from their linkage
9531 name.
9532 NOTE: We need to do this even if cu->has_namespace_info != 0.
9533 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9534 if (cu->language == language_cplus
9535 && dwarf2_per_objfile->types.asection != NULL
9536 && part_die->die_parent == NULL
9537 && part_die->has_children
9538 && (part_die->tag == DW_TAG_class_type
9539 || part_die->tag == DW_TAG_structure_type
9540 || part_die->tag == DW_TAG_union_type))
9541 guess_partial_die_structure_name (part_die, cu);
9542
9543 /* GCC might emit a nameless struct or union that has a linkage
9544 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9545 if (part_die->name == NULL
9546 && (part_die->tag == DW_TAG_structure_type
9547 || part_die->tag == DW_TAG_union_type
9548 || part_die->tag == DW_TAG_class_type)
9549 && part_die->linkage_name != NULL)
9550 {
9551 char *demangled;
9552
9553 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9554 if (demangled)
9555 {
9556 part_die->name = obsavestring (demangled, strlen (demangled),
9557 &cu->objfile->objfile_obstack);
9558 xfree (demangled);
9559 }
9560 }
9561
9562 part_die->fixup_called = 1;
9563 }
9564
9565 /* Read an attribute value described by an attribute form. */
9566
9567 static gdb_byte *
9568 read_attribute_value (struct attribute *attr, unsigned form,
9569 bfd *abfd, gdb_byte *info_ptr,
9570 struct dwarf2_cu *cu)
9571 {
9572 struct comp_unit_head *cu_header = &cu->header;
9573 unsigned int bytes_read;
9574 struct dwarf_block *blk;
9575
9576 attr->form = form;
9577 switch (form)
9578 {
9579 case DW_FORM_ref_addr:
9580 if (cu->header.version == 2)
9581 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9582 else
9583 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9584 &cu->header, &bytes_read);
9585 info_ptr += bytes_read;
9586 break;
9587 case DW_FORM_addr:
9588 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9589 info_ptr += bytes_read;
9590 break;
9591 case DW_FORM_block2:
9592 blk = dwarf_alloc_block (cu);
9593 blk->size = read_2_bytes (abfd, info_ptr);
9594 info_ptr += 2;
9595 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9596 info_ptr += blk->size;
9597 DW_BLOCK (attr) = blk;
9598 break;
9599 case DW_FORM_block4:
9600 blk = dwarf_alloc_block (cu);
9601 blk->size = read_4_bytes (abfd, info_ptr);
9602 info_ptr += 4;
9603 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9604 info_ptr += blk->size;
9605 DW_BLOCK (attr) = blk;
9606 break;
9607 case DW_FORM_data2:
9608 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9609 info_ptr += 2;
9610 break;
9611 case DW_FORM_data4:
9612 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9613 info_ptr += 4;
9614 break;
9615 case DW_FORM_data8:
9616 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9617 info_ptr += 8;
9618 break;
9619 case DW_FORM_sec_offset:
9620 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9621 info_ptr += bytes_read;
9622 break;
9623 case DW_FORM_string:
9624 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9625 DW_STRING_IS_CANONICAL (attr) = 0;
9626 info_ptr += bytes_read;
9627 break;
9628 case DW_FORM_strp:
9629 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9630 &bytes_read);
9631 DW_STRING_IS_CANONICAL (attr) = 0;
9632 info_ptr += bytes_read;
9633 break;
9634 case DW_FORM_exprloc:
9635 case DW_FORM_block:
9636 blk = dwarf_alloc_block (cu);
9637 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9638 info_ptr += bytes_read;
9639 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9640 info_ptr += blk->size;
9641 DW_BLOCK (attr) = blk;
9642 break;
9643 case DW_FORM_block1:
9644 blk = dwarf_alloc_block (cu);
9645 blk->size = read_1_byte (abfd, info_ptr);
9646 info_ptr += 1;
9647 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9648 info_ptr += blk->size;
9649 DW_BLOCK (attr) = blk;
9650 break;
9651 case DW_FORM_data1:
9652 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9653 info_ptr += 1;
9654 break;
9655 case DW_FORM_flag:
9656 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9657 info_ptr += 1;
9658 break;
9659 case DW_FORM_flag_present:
9660 DW_UNSND (attr) = 1;
9661 break;
9662 case DW_FORM_sdata:
9663 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9664 info_ptr += bytes_read;
9665 break;
9666 case DW_FORM_udata:
9667 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9668 info_ptr += bytes_read;
9669 break;
9670 case DW_FORM_ref1:
9671 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9672 info_ptr += 1;
9673 break;
9674 case DW_FORM_ref2:
9675 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9676 info_ptr += 2;
9677 break;
9678 case DW_FORM_ref4:
9679 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9680 info_ptr += 4;
9681 break;
9682 case DW_FORM_ref8:
9683 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9684 info_ptr += 8;
9685 break;
9686 case DW_FORM_ref_sig8:
9687 /* Convert the signature to something we can record in DW_UNSND
9688 for later lookup.
9689 NOTE: This is NULL if the type wasn't found. */
9690 DW_SIGNATURED_TYPE (attr) =
9691 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9692 info_ptr += 8;
9693 break;
9694 case DW_FORM_ref_udata:
9695 DW_ADDR (attr) = (cu->header.offset
9696 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9697 info_ptr += bytes_read;
9698 break;
9699 case DW_FORM_indirect:
9700 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9701 info_ptr += bytes_read;
9702 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9703 break;
9704 default:
9705 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9706 dwarf_form_name (form),
9707 bfd_get_filename (abfd));
9708 }
9709
9710 /* We have seen instances where the compiler tried to emit a byte
9711 size attribute of -1 which ended up being encoded as an unsigned
9712 0xffffffff. Although 0xffffffff is technically a valid size value,
9713 an object of this size seems pretty unlikely so we can relatively
9714 safely treat these cases as if the size attribute was invalid and
9715 treat them as zero by default. */
9716 if (attr->name == DW_AT_byte_size
9717 && form == DW_FORM_data4
9718 && DW_UNSND (attr) >= 0xffffffff)
9719 {
9720 complaint
9721 (&symfile_complaints,
9722 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9723 hex_string (DW_UNSND (attr)));
9724 DW_UNSND (attr) = 0;
9725 }
9726
9727 return info_ptr;
9728 }
9729
9730 /* Read an attribute described by an abbreviated attribute. */
9731
9732 static gdb_byte *
9733 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9734 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9735 {
9736 attr->name = abbrev->name;
9737 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9738 }
9739
9740 /* Read dwarf information from a buffer. */
9741
9742 static unsigned int
9743 read_1_byte (bfd *abfd, gdb_byte *buf)
9744 {
9745 return bfd_get_8 (abfd, buf);
9746 }
9747
9748 static int
9749 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9750 {
9751 return bfd_get_signed_8 (abfd, buf);
9752 }
9753
9754 static unsigned int
9755 read_2_bytes (bfd *abfd, gdb_byte *buf)
9756 {
9757 return bfd_get_16 (abfd, buf);
9758 }
9759
9760 static int
9761 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9762 {
9763 return bfd_get_signed_16 (abfd, buf);
9764 }
9765
9766 static unsigned int
9767 read_4_bytes (bfd *abfd, gdb_byte *buf)
9768 {
9769 return bfd_get_32 (abfd, buf);
9770 }
9771
9772 static int
9773 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9774 {
9775 return bfd_get_signed_32 (abfd, buf);
9776 }
9777
9778 static ULONGEST
9779 read_8_bytes (bfd *abfd, gdb_byte *buf)
9780 {
9781 return bfd_get_64 (abfd, buf);
9782 }
9783
9784 static CORE_ADDR
9785 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9786 unsigned int *bytes_read)
9787 {
9788 struct comp_unit_head *cu_header = &cu->header;
9789 CORE_ADDR retval = 0;
9790
9791 if (cu_header->signed_addr_p)
9792 {
9793 switch (cu_header->addr_size)
9794 {
9795 case 2:
9796 retval = bfd_get_signed_16 (abfd, buf);
9797 break;
9798 case 4:
9799 retval = bfd_get_signed_32 (abfd, buf);
9800 break;
9801 case 8:
9802 retval = bfd_get_signed_64 (abfd, buf);
9803 break;
9804 default:
9805 internal_error (__FILE__, __LINE__,
9806 _("read_address: bad switch, signed [in module %s]"),
9807 bfd_get_filename (abfd));
9808 }
9809 }
9810 else
9811 {
9812 switch (cu_header->addr_size)
9813 {
9814 case 2:
9815 retval = bfd_get_16 (abfd, buf);
9816 break;
9817 case 4:
9818 retval = bfd_get_32 (abfd, buf);
9819 break;
9820 case 8:
9821 retval = bfd_get_64 (abfd, buf);
9822 break;
9823 default:
9824 internal_error (__FILE__, __LINE__,
9825 _("read_address: bad switch, "
9826 "unsigned [in module %s]"),
9827 bfd_get_filename (abfd));
9828 }
9829 }
9830
9831 *bytes_read = cu_header->addr_size;
9832 return retval;
9833 }
9834
9835 /* Read the initial length from a section. The (draft) DWARF 3
9836 specification allows the initial length to take up either 4 bytes
9837 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9838 bytes describe the length and all offsets will be 8 bytes in length
9839 instead of 4.
9840
9841 An older, non-standard 64-bit format is also handled by this
9842 function. The older format in question stores the initial length
9843 as an 8-byte quantity without an escape value. Lengths greater
9844 than 2^32 aren't very common which means that the initial 4 bytes
9845 is almost always zero. Since a length value of zero doesn't make
9846 sense for the 32-bit format, this initial zero can be considered to
9847 be an escape value which indicates the presence of the older 64-bit
9848 format. As written, the code can't detect (old format) lengths
9849 greater than 4GB. If it becomes necessary to handle lengths
9850 somewhat larger than 4GB, we could allow other small values (such
9851 as the non-sensical values of 1, 2, and 3) to also be used as
9852 escape values indicating the presence of the old format.
9853
9854 The value returned via bytes_read should be used to increment the
9855 relevant pointer after calling read_initial_length().
9856
9857 [ Note: read_initial_length() and read_offset() are based on the
9858 document entitled "DWARF Debugging Information Format", revision
9859 3, draft 8, dated November 19, 2001. This document was obtained
9860 from:
9861
9862 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9863
9864 This document is only a draft and is subject to change. (So beware.)
9865
9866 Details regarding the older, non-standard 64-bit format were
9867 determined empirically by examining 64-bit ELF files produced by
9868 the SGI toolchain on an IRIX 6.5 machine.
9869
9870 - Kevin, July 16, 2002
9871 ] */
9872
9873 static LONGEST
9874 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9875 {
9876 LONGEST length = bfd_get_32 (abfd, buf);
9877
9878 if (length == 0xffffffff)
9879 {
9880 length = bfd_get_64 (abfd, buf + 4);
9881 *bytes_read = 12;
9882 }
9883 else if (length == 0)
9884 {
9885 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
9886 length = bfd_get_64 (abfd, buf);
9887 *bytes_read = 8;
9888 }
9889 else
9890 {
9891 *bytes_read = 4;
9892 }
9893
9894 return length;
9895 }
9896
9897 /* Cover function for read_initial_length.
9898 Returns the length of the object at BUF, and stores the size of the
9899 initial length in *BYTES_READ and stores the size that offsets will be in
9900 *OFFSET_SIZE.
9901 If the initial length size is not equivalent to that specified in
9902 CU_HEADER then issue a complaint.
9903 This is useful when reading non-comp-unit headers. */
9904
9905 static LONGEST
9906 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9907 const struct comp_unit_head *cu_header,
9908 unsigned int *bytes_read,
9909 unsigned int *offset_size)
9910 {
9911 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9912
9913 gdb_assert (cu_header->initial_length_size == 4
9914 || cu_header->initial_length_size == 8
9915 || cu_header->initial_length_size == 12);
9916
9917 if (cu_header->initial_length_size != *bytes_read)
9918 complaint (&symfile_complaints,
9919 _("intermixed 32-bit and 64-bit DWARF sections"));
9920
9921 *offset_size = (*bytes_read == 4) ? 4 : 8;
9922 return length;
9923 }
9924
9925 /* Read an offset from the data stream. The size of the offset is
9926 given by cu_header->offset_size. */
9927
9928 static LONGEST
9929 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9930 unsigned int *bytes_read)
9931 {
9932 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9933
9934 *bytes_read = cu_header->offset_size;
9935 return offset;
9936 }
9937
9938 /* Read an offset from the data stream. */
9939
9940 static LONGEST
9941 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9942 {
9943 LONGEST retval = 0;
9944
9945 switch (offset_size)
9946 {
9947 case 4:
9948 retval = bfd_get_32 (abfd, buf);
9949 break;
9950 case 8:
9951 retval = bfd_get_64 (abfd, buf);
9952 break;
9953 default:
9954 internal_error (__FILE__, __LINE__,
9955 _("read_offset_1: bad switch [in module %s]"),
9956 bfd_get_filename (abfd));
9957 }
9958
9959 return retval;
9960 }
9961
9962 static gdb_byte *
9963 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9964 {
9965 /* If the size of a host char is 8 bits, we can return a pointer
9966 to the buffer, otherwise we have to copy the data to a buffer
9967 allocated on the temporary obstack. */
9968 gdb_assert (HOST_CHAR_BIT == 8);
9969 return buf;
9970 }
9971
9972 static char *
9973 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9974 {
9975 /* If the size of a host char is 8 bits, we can return a pointer
9976 to the string, otherwise we have to copy the string to a buffer
9977 allocated on the temporary obstack. */
9978 gdb_assert (HOST_CHAR_BIT == 8);
9979 if (*buf == '\0')
9980 {
9981 *bytes_read_ptr = 1;
9982 return NULL;
9983 }
9984 *bytes_read_ptr = strlen ((char *) buf) + 1;
9985 return (char *) buf;
9986 }
9987
9988 static char *
9989 read_indirect_string (bfd *abfd, gdb_byte *buf,
9990 const struct comp_unit_head *cu_header,
9991 unsigned int *bytes_read_ptr)
9992 {
9993 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9994
9995 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9996 if (dwarf2_per_objfile->str.buffer == NULL)
9997 {
9998 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9999 bfd_get_filename (abfd));
10000 return NULL;
10001 }
10002 if (str_offset >= dwarf2_per_objfile->str.size)
10003 {
10004 error (_("DW_FORM_strp pointing outside of "
10005 ".debug_str section [in module %s]"),
10006 bfd_get_filename (abfd));
10007 return NULL;
10008 }
10009 gdb_assert (HOST_CHAR_BIT == 8);
10010 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10011 return NULL;
10012 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10013 }
10014
10015 static unsigned long
10016 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10017 {
10018 unsigned long result;
10019 unsigned int num_read;
10020 int i, shift;
10021 unsigned char byte;
10022
10023 result = 0;
10024 shift = 0;
10025 num_read = 0;
10026 i = 0;
10027 while (1)
10028 {
10029 byte = bfd_get_8 (abfd, buf);
10030 buf++;
10031 num_read++;
10032 result |= ((unsigned long)(byte & 127) << shift);
10033 if ((byte & 128) == 0)
10034 {
10035 break;
10036 }
10037 shift += 7;
10038 }
10039 *bytes_read_ptr = num_read;
10040 return result;
10041 }
10042
10043 static long
10044 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10045 {
10046 long result;
10047 int i, shift, num_read;
10048 unsigned char byte;
10049
10050 result = 0;
10051 shift = 0;
10052 num_read = 0;
10053 i = 0;
10054 while (1)
10055 {
10056 byte = bfd_get_8 (abfd, buf);
10057 buf++;
10058 num_read++;
10059 result |= ((long)(byte & 127) << shift);
10060 shift += 7;
10061 if ((byte & 128) == 0)
10062 {
10063 break;
10064 }
10065 }
10066 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10067 result |= -(((long)1) << shift);
10068 *bytes_read_ptr = num_read;
10069 return result;
10070 }
10071
10072 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10073
10074 static gdb_byte *
10075 skip_leb128 (bfd *abfd, gdb_byte *buf)
10076 {
10077 int byte;
10078
10079 while (1)
10080 {
10081 byte = bfd_get_8 (abfd, buf);
10082 buf++;
10083 if ((byte & 128) == 0)
10084 return buf;
10085 }
10086 }
10087
10088 static void
10089 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10090 {
10091 switch (lang)
10092 {
10093 case DW_LANG_C89:
10094 case DW_LANG_C99:
10095 case DW_LANG_C:
10096 cu->language = language_c;
10097 break;
10098 case DW_LANG_C_plus_plus:
10099 cu->language = language_cplus;
10100 break;
10101 case DW_LANG_D:
10102 cu->language = language_d;
10103 break;
10104 case DW_LANG_Fortran77:
10105 case DW_LANG_Fortran90:
10106 case DW_LANG_Fortran95:
10107 cu->language = language_fortran;
10108 break;
10109 case DW_LANG_Mips_Assembler:
10110 cu->language = language_asm;
10111 break;
10112 case DW_LANG_Java:
10113 cu->language = language_java;
10114 break;
10115 case DW_LANG_Ada83:
10116 case DW_LANG_Ada95:
10117 cu->language = language_ada;
10118 break;
10119 case DW_LANG_Modula2:
10120 cu->language = language_m2;
10121 break;
10122 case DW_LANG_Pascal83:
10123 cu->language = language_pascal;
10124 break;
10125 case DW_LANG_ObjC:
10126 cu->language = language_objc;
10127 break;
10128 case DW_LANG_Cobol74:
10129 case DW_LANG_Cobol85:
10130 default:
10131 cu->language = language_minimal;
10132 break;
10133 }
10134 cu->language_defn = language_def (cu->language);
10135 }
10136
10137 /* Return the named attribute or NULL if not there. */
10138
10139 static struct attribute *
10140 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10141 {
10142 unsigned int i;
10143 struct attribute *spec = NULL;
10144
10145 for (i = 0; i < die->num_attrs; ++i)
10146 {
10147 if (die->attrs[i].name == name)
10148 return &die->attrs[i];
10149 if (die->attrs[i].name == DW_AT_specification
10150 || die->attrs[i].name == DW_AT_abstract_origin)
10151 spec = &die->attrs[i];
10152 }
10153
10154 if (spec)
10155 {
10156 die = follow_die_ref (die, spec, &cu);
10157 return dwarf2_attr (die, name, cu);
10158 }
10159
10160 return NULL;
10161 }
10162
10163 /* Return the named attribute or NULL if not there,
10164 but do not follow DW_AT_specification, etc.
10165 This is for use in contexts where we're reading .debug_types dies.
10166 Following DW_AT_specification, DW_AT_abstract_origin will take us
10167 back up the chain, and we want to go down. */
10168
10169 static struct attribute *
10170 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10171 struct dwarf2_cu *cu)
10172 {
10173 unsigned int i;
10174
10175 for (i = 0; i < die->num_attrs; ++i)
10176 if (die->attrs[i].name == name)
10177 return &die->attrs[i];
10178
10179 return NULL;
10180 }
10181
10182 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10183 and holds a non-zero value. This function should only be used for
10184 DW_FORM_flag or DW_FORM_flag_present attributes. */
10185
10186 static int
10187 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10188 {
10189 struct attribute *attr = dwarf2_attr (die, name, cu);
10190
10191 return (attr && DW_UNSND (attr));
10192 }
10193
10194 static int
10195 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10196 {
10197 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10198 which value is non-zero. However, we have to be careful with
10199 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10200 (via dwarf2_flag_true_p) follows this attribute. So we may
10201 end up accidently finding a declaration attribute that belongs
10202 to a different DIE referenced by the specification attribute,
10203 even though the given DIE does not have a declaration attribute. */
10204 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10205 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10206 }
10207
10208 /* Return the die giving the specification for DIE, if there is
10209 one. *SPEC_CU is the CU containing DIE on input, and the CU
10210 containing the return value on output. If there is no
10211 specification, but there is an abstract origin, that is
10212 returned. */
10213
10214 static struct die_info *
10215 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10216 {
10217 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10218 *spec_cu);
10219
10220 if (spec_attr == NULL)
10221 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10222
10223 if (spec_attr == NULL)
10224 return NULL;
10225 else
10226 return follow_die_ref (die, spec_attr, spec_cu);
10227 }
10228
10229 /* Free the line_header structure *LH, and any arrays and strings it
10230 refers to.
10231 NOTE: This is also used as a "cleanup" function. */
10232
10233 static void
10234 free_line_header (struct line_header *lh)
10235 {
10236 if (lh->standard_opcode_lengths)
10237 xfree (lh->standard_opcode_lengths);
10238
10239 /* Remember that all the lh->file_names[i].name pointers are
10240 pointers into debug_line_buffer, and don't need to be freed. */
10241 if (lh->file_names)
10242 xfree (lh->file_names);
10243
10244 /* Similarly for the include directory names. */
10245 if (lh->include_dirs)
10246 xfree (lh->include_dirs);
10247
10248 xfree (lh);
10249 }
10250
10251 /* Add an entry to LH's include directory table. */
10252
10253 static void
10254 add_include_dir (struct line_header *lh, char *include_dir)
10255 {
10256 /* Grow the array if necessary. */
10257 if (lh->include_dirs_size == 0)
10258 {
10259 lh->include_dirs_size = 1; /* for testing */
10260 lh->include_dirs = xmalloc (lh->include_dirs_size
10261 * sizeof (*lh->include_dirs));
10262 }
10263 else if (lh->num_include_dirs >= lh->include_dirs_size)
10264 {
10265 lh->include_dirs_size *= 2;
10266 lh->include_dirs = xrealloc (lh->include_dirs,
10267 (lh->include_dirs_size
10268 * sizeof (*lh->include_dirs)));
10269 }
10270
10271 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10272 }
10273
10274 /* Add an entry to LH's file name table. */
10275
10276 static void
10277 add_file_name (struct line_header *lh,
10278 char *name,
10279 unsigned int dir_index,
10280 unsigned int mod_time,
10281 unsigned int length)
10282 {
10283 struct file_entry *fe;
10284
10285 /* Grow the array if necessary. */
10286 if (lh->file_names_size == 0)
10287 {
10288 lh->file_names_size = 1; /* for testing */
10289 lh->file_names = xmalloc (lh->file_names_size
10290 * sizeof (*lh->file_names));
10291 }
10292 else if (lh->num_file_names >= lh->file_names_size)
10293 {
10294 lh->file_names_size *= 2;
10295 lh->file_names = xrealloc (lh->file_names,
10296 (lh->file_names_size
10297 * sizeof (*lh->file_names)));
10298 }
10299
10300 fe = &lh->file_names[lh->num_file_names++];
10301 fe->name = name;
10302 fe->dir_index = dir_index;
10303 fe->mod_time = mod_time;
10304 fe->length = length;
10305 fe->included_p = 0;
10306 fe->symtab = NULL;
10307 }
10308
10309 /* Read the statement program header starting at OFFSET in
10310 .debug_line, according to the endianness of ABFD. Return a pointer
10311 to a struct line_header, allocated using xmalloc.
10312
10313 NOTE: the strings in the include directory and file name tables of
10314 the returned object point into debug_line_buffer, and must not be
10315 freed. */
10316
10317 static struct line_header *
10318 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10319 struct dwarf2_cu *cu)
10320 {
10321 struct cleanup *back_to;
10322 struct line_header *lh;
10323 gdb_byte *line_ptr;
10324 unsigned int bytes_read, offset_size;
10325 int i;
10326 char *cur_dir, *cur_file;
10327
10328 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10329 if (dwarf2_per_objfile->line.buffer == NULL)
10330 {
10331 complaint (&symfile_complaints, _("missing .debug_line section"));
10332 return 0;
10333 }
10334
10335 /* Make sure that at least there's room for the total_length field.
10336 That could be 12 bytes long, but we're just going to fudge that. */
10337 if (offset + 4 >= dwarf2_per_objfile->line.size)
10338 {
10339 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10340 return 0;
10341 }
10342
10343 lh = xmalloc (sizeof (*lh));
10344 memset (lh, 0, sizeof (*lh));
10345 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10346 (void *) lh);
10347
10348 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10349
10350 /* Read in the header. */
10351 lh->total_length =
10352 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10353 &bytes_read, &offset_size);
10354 line_ptr += bytes_read;
10355 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10356 + dwarf2_per_objfile->line.size))
10357 {
10358 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10359 return 0;
10360 }
10361 lh->statement_program_end = line_ptr + lh->total_length;
10362 lh->version = read_2_bytes (abfd, line_ptr);
10363 line_ptr += 2;
10364 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10365 line_ptr += offset_size;
10366 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10367 line_ptr += 1;
10368 if (lh->version >= 4)
10369 {
10370 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10371 line_ptr += 1;
10372 }
10373 else
10374 lh->maximum_ops_per_instruction = 1;
10375
10376 if (lh->maximum_ops_per_instruction == 0)
10377 {
10378 lh->maximum_ops_per_instruction = 1;
10379 complaint (&symfile_complaints,
10380 _("invalid maximum_ops_per_instruction "
10381 "in `.debug_line' section"));
10382 }
10383
10384 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10385 line_ptr += 1;
10386 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10387 line_ptr += 1;
10388 lh->line_range = read_1_byte (abfd, line_ptr);
10389 line_ptr += 1;
10390 lh->opcode_base = read_1_byte (abfd, line_ptr);
10391 line_ptr += 1;
10392 lh->standard_opcode_lengths
10393 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10394
10395 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10396 for (i = 1; i < lh->opcode_base; ++i)
10397 {
10398 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10399 line_ptr += 1;
10400 }
10401
10402 /* Read directory table. */
10403 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10404 {
10405 line_ptr += bytes_read;
10406 add_include_dir (lh, cur_dir);
10407 }
10408 line_ptr += bytes_read;
10409
10410 /* Read file name table. */
10411 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10412 {
10413 unsigned int dir_index, mod_time, length;
10414
10415 line_ptr += bytes_read;
10416 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10417 line_ptr += bytes_read;
10418 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10419 line_ptr += bytes_read;
10420 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10421 line_ptr += bytes_read;
10422
10423 add_file_name (lh, cur_file, dir_index, mod_time, length);
10424 }
10425 line_ptr += bytes_read;
10426 lh->statement_program_start = line_ptr;
10427
10428 if (line_ptr > (dwarf2_per_objfile->line.buffer
10429 + dwarf2_per_objfile->line.size))
10430 complaint (&symfile_complaints,
10431 _("line number info header doesn't "
10432 "fit in `.debug_line' section"));
10433
10434 discard_cleanups (back_to);
10435 return lh;
10436 }
10437
10438 /* This function exists to work around a bug in certain compilers
10439 (particularly GCC 2.95), in which the first line number marker of a
10440 function does not show up until after the prologue, right before
10441 the second line number marker. This function shifts ADDRESS down
10442 to the beginning of the function if necessary, and is called on
10443 addresses passed to record_line. */
10444
10445 static CORE_ADDR
10446 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10447 {
10448 struct function_range *fn;
10449
10450 /* Find the function_range containing address. */
10451 if (!cu->first_fn)
10452 return address;
10453
10454 if (!cu->cached_fn)
10455 cu->cached_fn = cu->first_fn;
10456
10457 fn = cu->cached_fn;
10458 while (fn)
10459 if (fn->lowpc <= address && fn->highpc > address)
10460 goto found;
10461 else
10462 fn = fn->next;
10463
10464 fn = cu->first_fn;
10465 while (fn && fn != cu->cached_fn)
10466 if (fn->lowpc <= address && fn->highpc > address)
10467 goto found;
10468 else
10469 fn = fn->next;
10470
10471 return address;
10472
10473 found:
10474 if (fn->seen_line)
10475 return address;
10476 if (address != fn->lowpc)
10477 complaint (&symfile_complaints,
10478 _("misplaced first line number at 0x%lx for '%s'"),
10479 (unsigned long) address, fn->name);
10480 fn->seen_line = 1;
10481 return fn->lowpc;
10482 }
10483
10484 /* Subroutine of dwarf_decode_lines to simplify it.
10485 Return the file name of the psymtab for included file FILE_INDEX
10486 in line header LH of PST.
10487 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10488 If space for the result is malloc'd, it will be freed by a cleanup.
10489 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10490
10491 static char *
10492 psymtab_include_file_name (const struct line_header *lh, int file_index,
10493 const struct partial_symtab *pst,
10494 const char *comp_dir)
10495 {
10496 const struct file_entry fe = lh->file_names [file_index];
10497 char *include_name = fe.name;
10498 char *include_name_to_compare = include_name;
10499 char *dir_name = NULL;
10500 const char *pst_filename;
10501 char *copied_name = NULL;
10502 int file_is_pst;
10503
10504 if (fe.dir_index)
10505 dir_name = lh->include_dirs[fe.dir_index - 1];
10506
10507 if (!IS_ABSOLUTE_PATH (include_name)
10508 && (dir_name != NULL || comp_dir != NULL))
10509 {
10510 /* Avoid creating a duplicate psymtab for PST.
10511 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10512 Before we do the comparison, however, we need to account
10513 for DIR_NAME and COMP_DIR.
10514 First prepend dir_name (if non-NULL). If we still don't
10515 have an absolute path prepend comp_dir (if non-NULL).
10516 However, the directory we record in the include-file's
10517 psymtab does not contain COMP_DIR (to match the
10518 corresponding symtab(s)).
10519
10520 Example:
10521
10522 bash$ cd /tmp
10523 bash$ gcc -g ./hello.c
10524 include_name = "hello.c"
10525 dir_name = "."
10526 DW_AT_comp_dir = comp_dir = "/tmp"
10527 DW_AT_name = "./hello.c" */
10528
10529 if (dir_name != NULL)
10530 {
10531 include_name = concat (dir_name, SLASH_STRING,
10532 include_name, (char *)NULL);
10533 include_name_to_compare = include_name;
10534 make_cleanup (xfree, include_name);
10535 }
10536 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10537 {
10538 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10539 include_name, (char *)NULL);
10540 }
10541 }
10542
10543 pst_filename = pst->filename;
10544 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10545 {
10546 copied_name = concat (pst->dirname, SLASH_STRING,
10547 pst_filename, (char *)NULL);
10548 pst_filename = copied_name;
10549 }
10550
10551 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10552
10553 if (include_name_to_compare != include_name)
10554 xfree (include_name_to_compare);
10555 if (copied_name != NULL)
10556 xfree (copied_name);
10557
10558 if (file_is_pst)
10559 return NULL;
10560 return include_name;
10561 }
10562
10563 /* Ignore this record_line request. */
10564
10565 static void
10566 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10567 {
10568 return;
10569 }
10570
10571 /* Decode the Line Number Program (LNP) for the given line_header
10572 structure and CU. The actual information extracted and the type
10573 of structures created from the LNP depends on the value of PST.
10574
10575 1. If PST is NULL, then this procedure uses the data from the program
10576 to create all necessary symbol tables, and their linetables.
10577
10578 2. If PST is not NULL, this procedure reads the program to determine
10579 the list of files included by the unit represented by PST, and
10580 builds all the associated partial symbol tables.
10581
10582 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10583 It is used for relative paths in the line table.
10584 NOTE: When processing partial symtabs (pst != NULL),
10585 comp_dir == pst->dirname.
10586
10587 NOTE: It is important that psymtabs have the same file name (via strcmp)
10588 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10589 symtab we don't use it in the name of the psymtabs we create.
10590 E.g. expand_line_sal requires this when finding psymtabs to expand.
10591 A good testcase for this is mb-inline.exp. */
10592
10593 static void
10594 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10595 struct dwarf2_cu *cu, struct partial_symtab *pst)
10596 {
10597 gdb_byte *line_ptr, *extended_end;
10598 gdb_byte *line_end;
10599 unsigned int bytes_read, extended_len;
10600 unsigned char op_code, extended_op, adj_opcode;
10601 CORE_ADDR baseaddr;
10602 struct objfile *objfile = cu->objfile;
10603 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10604 const int decode_for_pst_p = (pst != NULL);
10605 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10606 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10607 = record_line;
10608
10609 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10610
10611 line_ptr = lh->statement_program_start;
10612 line_end = lh->statement_program_end;
10613
10614 /* Read the statement sequences until there's nothing left. */
10615 while (line_ptr < line_end)
10616 {
10617 /* state machine registers */
10618 CORE_ADDR address = 0;
10619 unsigned int file = 1;
10620 unsigned int line = 1;
10621 unsigned int column = 0;
10622 int is_stmt = lh->default_is_stmt;
10623 int basic_block = 0;
10624 int end_sequence = 0;
10625 CORE_ADDR addr;
10626 unsigned char op_index = 0;
10627
10628 if (!decode_for_pst_p && lh->num_file_names >= file)
10629 {
10630 /* Start a subfile for the current file of the state machine. */
10631 /* lh->include_dirs and lh->file_names are 0-based, but the
10632 directory and file name numbers in the statement program
10633 are 1-based. */
10634 struct file_entry *fe = &lh->file_names[file - 1];
10635 char *dir = NULL;
10636
10637 if (fe->dir_index)
10638 dir = lh->include_dirs[fe->dir_index - 1];
10639
10640 dwarf2_start_subfile (fe->name, dir, comp_dir);
10641 }
10642
10643 /* Decode the table. */
10644 while (!end_sequence)
10645 {
10646 op_code = read_1_byte (abfd, line_ptr);
10647 line_ptr += 1;
10648 if (line_ptr > line_end)
10649 {
10650 dwarf2_debug_line_missing_end_sequence_complaint ();
10651 break;
10652 }
10653
10654 if (op_code >= lh->opcode_base)
10655 {
10656 /* Special operand. */
10657 adj_opcode = op_code - lh->opcode_base;
10658 address += (((op_index + (adj_opcode / lh->line_range))
10659 / lh->maximum_ops_per_instruction)
10660 * lh->minimum_instruction_length);
10661 op_index = ((op_index + (adj_opcode / lh->line_range))
10662 % lh->maximum_ops_per_instruction);
10663 line += lh->line_base + (adj_opcode % lh->line_range);
10664 if (lh->num_file_names < file || file == 0)
10665 dwarf2_debug_line_missing_file_complaint ();
10666 /* For now we ignore lines not starting on an
10667 instruction boundary. */
10668 else if (op_index == 0)
10669 {
10670 lh->file_names[file - 1].included_p = 1;
10671 if (!decode_for_pst_p && is_stmt)
10672 {
10673 if (last_subfile != current_subfile)
10674 {
10675 addr = gdbarch_addr_bits_remove (gdbarch, address);
10676 if (last_subfile)
10677 (*p_record_line) (last_subfile, 0, addr);
10678 last_subfile = current_subfile;
10679 }
10680 /* Append row to matrix using current values. */
10681 addr = check_cu_functions (address, cu);
10682 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10683 (*p_record_line) (current_subfile, line, addr);
10684 }
10685 }
10686 basic_block = 0;
10687 }
10688 else switch (op_code)
10689 {
10690 case DW_LNS_extended_op:
10691 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10692 &bytes_read);
10693 line_ptr += bytes_read;
10694 extended_end = line_ptr + extended_len;
10695 extended_op = read_1_byte (abfd, line_ptr);
10696 line_ptr += 1;
10697 switch (extended_op)
10698 {
10699 case DW_LNE_end_sequence:
10700 p_record_line = record_line;
10701 end_sequence = 1;
10702 break;
10703 case DW_LNE_set_address:
10704 address = read_address (abfd, line_ptr, cu, &bytes_read);
10705
10706 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10707 {
10708 /* This line table is for a function which has been
10709 GCd by the linker. Ignore it. PR gdb/12528 */
10710
10711 long line_offset
10712 = line_ptr - dwarf2_per_objfile->line.buffer;
10713
10714 complaint (&symfile_complaints,
10715 _(".debug_line address at offset 0x%lx is 0 "
10716 "[in module %s]"),
10717 line_offset, cu->objfile->name);
10718 p_record_line = noop_record_line;
10719 }
10720
10721 op_index = 0;
10722 line_ptr += bytes_read;
10723 address += baseaddr;
10724 break;
10725 case DW_LNE_define_file:
10726 {
10727 char *cur_file;
10728 unsigned int dir_index, mod_time, length;
10729
10730 cur_file = read_direct_string (abfd, line_ptr,
10731 &bytes_read);
10732 line_ptr += bytes_read;
10733 dir_index =
10734 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10735 line_ptr += bytes_read;
10736 mod_time =
10737 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10738 line_ptr += bytes_read;
10739 length =
10740 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10741 line_ptr += bytes_read;
10742 add_file_name (lh, cur_file, dir_index, mod_time, length);
10743 }
10744 break;
10745 case DW_LNE_set_discriminator:
10746 /* The discriminator is not interesting to the debugger;
10747 just ignore it. */
10748 line_ptr = extended_end;
10749 break;
10750 default:
10751 complaint (&symfile_complaints,
10752 _("mangled .debug_line section"));
10753 return;
10754 }
10755 /* Make sure that we parsed the extended op correctly. If e.g.
10756 we expected a different address size than the producer used,
10757 we may have read the wrong number of bytes. */
10758 if (line_ptr != extended_end)
10759 {
10760 complaint (&symfile_complaints,
10761 _("mangled .debug_line section"));
10762 return;
10763 }
10764 break;
10765 case DW_LNS_copy:
10766 if (lh->num_file_names < file || file == 0)
10767 dwarf2_debug_line_missing_file_complaint ();
10768 else
10769 {
10770 lh->file_names[file - 1].included_p = 1;
10771 if (!decode_for_pst_p && is_stmt)
10772 {
10773 if (last_subfile != current_subfile)
10774 {
10775 addr = gdbarch_addr_bits_remove (gdbarch, address);
10776 if (last_subfile)
10777 (*p_record_line) (last_subfile, 0, addr);
10778 last_subfile = current_subfile;
10779 }
10780 addr = check_cu_functions (address, cu);
10781 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10782 (*p_record_line) (current_subfile, line, addr);
10783 }
10784 }
10785 basic_block = 0;
10786 break;
10787 case DW_LNS_advance_pc:
10788 {
10789 CORE_ADDR adjust
10790 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10791
10792 address += (((op_index + adjust)
10793 / lh->maximum_ops_per_instruction)
10794 * lh->minimum_instruction_length);
10795 op_index = ((op_index + adjust)
10796 % lh->maximum_ops_per_instruction);
10797 line_ptr += bytes_read;
10798 }
10799 break;
10800 case DW_LNS_advance_line:
10801 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10802 line_ptr += bytes_read;
10803 break;
10804 case DW_LNS_set_file:
10805 {
10806 /* The arrays lh->include_dirs and lh->file_names are
10807 0-based, but the directory and file name numbers in
10808 the statement program are 1-based. */
10809 struct file_entry *fe;
10810 char *dir = NULL;
10811
10812 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10813 line_ptr += bytes_read;
10814 if (lh->num_file_names < file || file == 0)
10815 dwarf2_debug_line_missing_file_complaint ();
10816 else
10817 {
10818 fe = &lh->file_names[file - 1];
10819 if (fe->dir_index)
10820 dir = lh->include_dirs[fe->dir_index - 1];
10821 if (!decode_for_pst_p)
10822 {
10823 last_subfile = current_subfile;
10824 dwarf2_start_subfile (fe->name, dir, comp_dir);
10825 }
10826 }
10827 }
10828 break;
10829 case DW_LNS_set_column:
10830 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10831 line_ptr += bytes_read;
10832 break;
10833 case DW_LNS_negate_stmt:
10834 is_stmt = (!is_stmt);
10835 break;
10836 case DW_LNS_set_basic_block:
10837 basic_block = 1;
10838 break;
10839 /* Add to the address register of the state machine the
10840 address increment value corresponding to special opcode
10841 255. I.e., this value is scaled by the minimum
10842 instruction length since special opcode 255 would have
10843 scaled the increment. */
10844 case DW_LNS_const_add_pc:
10845 {
10846 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10847
10848 address += (((op_index + adjust)
10849 / lh->maximum_ops_per_instruction)
10850 * lh->minimum_instruction_length);
10851 op_index = ((op_index + adjust)
10852 % lh->maximum_ops_per_instruction);
10853 }
10854 break;
10855 case DW_LNS_fixed_advance_pc:
10856 address += read_2_bytes (abfd, line_ptr);
10857 op_index = 0;
10858 line_ptr += 2;
10859 break;
10860 default:
10861 {
10862 /* Unknown standard opcode, ignore it. */
10863 int i;
10864
10865 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10866 {
10867 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10868 line_ptr += bytes_read;
10869 }
10870 }
10871 }
10872 }
10873 if (lh->num_file_names < file || file == 0)
10874 dwarf2_debug_line_missing_file_complaint ();
10875 else
10876 {
10877 lh->file_names[file - 1].included_p = 1;
10878 if (!decode_for_pst_p)
10879 {
10880 addr = gdbarch_addr_bits_remove (gdbarch, address);
10881 (*p_record_line) (current_subfile, 0, addr);
10882 }
10883 }
10884 }
10885
10886 if (decode_for_pst_p)
10887 {
10888 int file_index;
10889
10890 /* Now that we're done scanning the Line Header Program, we can
10891 create the psymtab of each included file. */
10892 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10893 if (lh->file_names[file_index].included_p == 1)
10894 {
10895 char *include_name =
10896 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10897 if (include_name != NULL)
10898 dwarf2_create_include_psymtab (include_name, pst, objfile);
10899 }
10900 }
10901 else
10902 {
10903 /* Make sure a symtab is created for every file, even files
10904 which contain only variables (i.e. no code with associated
10905 line numbers). */
10906
10907 int i;
10908 struct file_entry *fe;
10909
10910 for (i = 0; i < lh->num_file_names; i++)
10911 {
10912 char *dir = NULL;
10913
10914 fe = &lh->file_names[i];
10915 if (fe->dir_index)
10916 dir = lh->include_dirs[fe->dir_index - 1];
10917 dwarf2_start_subfile (fe->name, dir, comp_dir);
10918
10919 /* Skip the main file; we don't need it, and it must be
10920 allocated last, so that it will show up before the
10921 non-primary symtabs in the objfile's symtab list. */
10922 if (current_subfile == first_subfile)
10923 continue;
10924
10925 if (current_subfile->symtab == NULL)
10926 current_subfile->symtab = allocate_symtab (current_subfile->name,
10927 cu->objfile);
10928 fe->symtab = current_subfile->symtab;
10929 }
10930 }
10931 }
10932
10933 /* Start a subfile for DWARF. FILENAME is the name of the file and
10934 DIRNAME the name of the source directory which contains FILENAME
10935 or NULL if not known. COMP_DIR is the compilation directory for the
10936 linetable's compilation unit or NULL if not known.
10937 This routine tries to keep line numbers from identical absolute and
10938 relative file names in a common subfile.
10939
10940 Using the `list' example from the GDB testsuite, which resides in
10941 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10942 of /srcdir/list0.c yields the following debugging information for list0.c:
10943
10944 DW_AT_name: /srcdir/list0.c
10945 DW_AT_comp_dir: /compdir
10946 files.files[0].name: list0.h
10947 files.files[0].dir: /srcdir
10948 files.files[1].name: list0.c
10949 files.files[1].dir: /srcdir
10950
10951 The line number information for list0.c has to end up in a single
10952 subfile, so that `break /srcdir/list0.c:1' works as expected.
10953 start_subfile will ensure that this happens provided that we pass the
10954 concatenation of files.files[1].dir and files.files[1].name as the
10955 subfile's name. */
10956
10957 static void
10958 dwarf2_start_subfile (char *filename, const char *dirname,
10959 const char *comp_dir)
10960 {
10961 char *fullname;
10962
10963 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10964 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10965 second argument to start_subfile. To be consistent, we do the
10966 same here. In order not to lose the line information directory,
10967 we concatenate it to the filename when it makes sense.
10968 Note that the Dwarf3 standard says (speaking of filenames in line
10969 information): ``The directory index is ignored for file names
10970 that represent full path names''. Thus ignoring dirname in the
10971 `else' branch below isn't an issue. */
10972
10973 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10974 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10975 else
10976 fullname = filename;
10977
10978 start_subfile (fullname, comp_dir);
10979
10980 if (fullname != filename)
10981 xfree (fullname);
10982 }
10983
10984 static void
10985 var_decode_location (struct attribute *attr, struct symbol *sym,
10986 struct dwarf2_cu *cu)
10987 {
10988 struct objfile *objfile = cu->objfile;
10989 struct comp_unit_head *cu_header = &cu->header;
10990
10991 /* NOTE drow/2003-01-30: There used to be a comment and some special
10992 code here to turn a symbol with DW_AT_external and a
10993 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10994 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10995 with some versions of binutils) where shared libraries could have
10996 relocations against symbols in their debug information - the
10997 minimal symbol would have the right address, but the debug info
10998 would not. It's no longer necessary, because we will explicitly
10999 apply relocations when we read in the debug information now. */
11000
11001 /* A DW_AT_location attribute with no contents indicates that a
11002 variable has been optimized away. */
11003 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11004 {
11005 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11006 return;
11007 }
11008
11009 /* Handle one degenerate form of location expression specially, to
11010 preserve GDB's previous behavior when section offsets are
11011 specified. If this is just a DW_OP_addr then mark this symbol
11012 as LOC_STATIC. */
11013
11014 if (attr_form_is_block (attr)
11015 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11016 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11017 {
11018 unsigned int dummy;
11019
11020 SYMBOL_VALUE_ADDRESS (sym) =
11021 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11022 SYMBOL_CLASS (sym) = LOC_STATIC;
11023 fixup_symbol_section (sym, objfile);
11024 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11025 SYMBOL_SECTION (sym));
11026 return;
11027 }
11028
11029 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11030 expression evaluator, and use LOC_COMPUTED only when necessary
11031 (i.e. when the value of a register or memory location is
11032 referenced, or a thread-local block, etc.). Then again, it might
11033 not be worthwhile. I'm assuming that it isn't unless performance
11034 or memory numbers show me otherwise. */
11035
11036 dwarf2_symbol_mark_computed (attr, sym, cu);
11037 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11038
11039 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11040 cu->has_loclist = 1;
11041 }
11042
11043 /* Given a pointer to a DWARF information entry, figure out if we need
11044 to make a symbol table entry for it, and if so, create a new entry
11045 and return a pointer to it.
11046 If TYPE is NULL, determine symbol type from the die, otherwise
11047 used the passed type.
11048 If SPACE is not NULL, use it to hold the new symbol. If it is
11049 NULL, allocate a new symbol on the objfile's obstack. */
11050
11051 static struct symbol *
11052 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11053 struct symbol *space)
11054 {
11055 struct objfile *objfile = cu->objfile;
11056 struct symbol *sym = NULL;
11057 char *name;
11058 struct attribute *attr = NULL;
11059 struct attribute *attr2 = NULL;
11060 CORE_ADDR baseaddr;
11061 struct pending **list_to_add = NULL;
11062
11063 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11064
11065 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11066
11067 name = dwarf2_name (die, cu);
11068 if (name)
11069 {
11070 const char *linkagename;
11071 int suppress_add = 0;
11072
11073 if (space)
11074 sym = space;
11075 else
11076 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11077 OBJSTAT (objfile, n_syms++);
11078
11079 /* Cache this symbol's name and the name's demangled form (if any). */
11080 SYMBOL_SET_LANGUAGE (sym, cu->language);
11081 linkagename = dwarf2_physname (name, die, cu);
11082 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11083
11084 /* Fortran does not have mangling standard and the mangling does differ
11085 between gfortran, iFort etc. */
11086 if (cu->language == language_fortran
11087 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11088 symbol_set_demangled_name (&(sym->ginfo),
11089 (char *) dwarf2_full_name (name, die, cu),
11090 NULL);
11091
11092 /* Default assumptions.
11093 Use the passed type or decode it from the die. */
11094 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11095 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11096 if (type != NULL)
11097 SYMBOL_TYPE (sym) = type;
11098 else
11099 SYMBOL_TYPE (sym) = die_type (die, cu);
11100 attr = dwarf2_attr (die,
11101 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11102 cu);
11103 if (attr)
11104 {
11105 SYMBOL_LINE (sym) = DW_UNSND (attr);
11106 }
11107
11108 attr = dwarf2_attr (die,
11109 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11110 cu);
11111 if (attr)
11112 {
11113 int file_index = DW_UNSND (attr);
11114
11115 if (cu->line_header == NULL
11116 || file_index > cu->line_header->num_file_names)
11117 complaint (&symfile_complaints,
11118 _("file index out of range"));
11119 else if (file_index > 0)
11120 {
11121 struct file_entry *fe;
11122
11123 fe = &cu->line_header->file_names[file_index - 1];
11124 SYMBOL_SYMTAB (sym) = fe->symtab;
11125 }
11126 }
11127
11128 switch (die->tag)
11129 {
11130 case DW_TAG_label:
11131 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11132 if (attr)
11133 {
11134 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11135 }
11136 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11137 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11138 SYMBOL_CLASS (sym) = LOC_LABEL;
11139 add_symbol_to_list (sym, cu->list_in_scope);
11140 break;
11141 case DW_TAG_subprogram:
11142 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11143 finish_block. */
11144 SYMBOL_CLASS (sym) = LOC_BLOCK;
11145 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11146 if ((attr2 && (DW_UNSND (attr2) != 0))
11147 || cu->language == language_ada)
11148 {
11149 /* Subprograms marked external are stored as a global symbol.
11150 Ada subprograms, whether marked external or not, are always
11151 stored as a global symbol, because we want to be able to
11152 access them globally. For instance, we want to be able
11153 to break on a nested subprogram without having to
11154 specify the context. */
11155 list_to_add = &global_symbols;
11156 }
11157 else
11158 {
11159 list_to_add = cu->list_in_scope;
11160 }
11161 break;
11162 case DW_TAG_inlined_subroutine:
11163 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11164 finish_block. */
11165 SYMBOL_CLASS (sym) = LOC_BLOCK;
11166 SYMBOL_INLINED (sym) = 1;
11167 /* Do not add the symbol to any lists. It will be found via
11168 BLOCK_FUNCTION from the blockvector. */
11169 break;
11170 case DW_TAG_template_value_param:
11171 suppress_add = 1;
11172 /* Fall through. */
11173 case DW_TAG_constant:
11174 case DW_TAG_variable:
11175 case DW_TAG_member:
11176 /* Compilation with minimal debug info may result in
11177 variables with missing type entries. Change the
11178 misleading `void' type to something sensible. */
11179 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11180 SYMBOL_TYPE (sym)
11181 = objfile_type (objfile)->nodebug_data_symbol;
11182
11183 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11184 /* In the case of DW_TAG_member, we should only be called for
11185 static const members. */
11186 if (die->tag == DW_TAG_member)
11187 {
11188 /* dwarf2_add_field uses die_is_declaration,
11189 so we do the same. */
11190 gdb_assert (die_is_declaration (die, cu));
11191 gdb_assert (attr);
11192 }
11193 if (attr)
11194 {
11195 dwarf2_const_value (attr, sym, cu);
11196 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11197 if (!suppress_add)
11198 {
11199 if (attr2 && (DW_UNSND (attr2) != 0))
11200 list_to_add = &global_symbols;
11201 else
11202 list_to_add = cu->list_in_scope;
11203 }
11204 break;
11205 }
11206 attr = dwarf2_attr (die, DW_AT_location, cu);
11207 if (attr)
11208 {
11209 var_decode_location (attr, sym, cu);
11210 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11211 if (SYMBOL_CLASS (sym) == LOC_STATIC
11212 && SYMBOL_VALUE_ADDRESS (sym) == 0
11213 && !dwarf2_per_objfile->has_section_at_zero)
11214 {
11215 /* When a static variable is eliminated by the linker,
11216 the corresponding debug information is not stripped
11217 out, but the variable address is set to null;
11218 do not add such variables into symbol table. */
11219 }
11220 else if (attr2 && (DW_UNSND (attr2) != 0))
11221 {
11222 /* Workaround gfortran PR debug/40040 - it uses
11223 DW_AT_location for variables in -fPIC libraries which may
11224 get overriden by other libraries/executable and get
11225 a different address. Resolve it by the minimal symbol
11226 which may come from inferior's executable using copy
11227 relocation. Make this workaround only for gfortran as for
11228 other compilers GDB cannot guess the minimal symbol
11229 Fortran mangling kind. */
11230 if (cu->language == language_fortran && die->parent
11231 && die->parent->tag == DW_TAG_module
11232 && cu->producer
11233 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11234 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11235
11236 /* A variable with DW_AT_external is never static,
11237 but it may be block-scoped. */
11238 list_to_add = (cu->list_in_scope == &file_symbols
11239 ? &global_symbols : cu->list_in_scope);
11240 }
11241 else
11242 list_to_add = cu->list_in_scope;
11243 }
11244 else
11245 {
11246 /* We do not know the address of this symbol.
11247 If it is an external symbol and we have type information
11248 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11249 The address of the variable will then be determined from
11250 the minimal symbol table whenever the variable is
11251 referenced. */
11252 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11253 if (attr2 && (DW_UNSND (attr2) != 0)
11254 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11255 {
11256 /* A variable with DW_AT_external is never static, but it
11257 may be block-scoped. */
11258 list_to_add = (cu->list_in_scope == &file_symbols
11259 ? &global_symbols : cu->list_in_scope);
11260
11261 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11262 }
11263 else if (!die_is_declaration (die, cu))
11264 {
11265 /* Use the default LOC_OPTIMIZED_OUT class. */
11266 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11267 if (!suppress_add)
11268 list_to_add = cu->list_in_scope;
11269 }
11270 }
11271 break;
11272 case DW_TAG_formal_parameter:
11273 /* If we are inside a function, mark this as an argument. If
11274 not, we might be looking at an argument to an inlined function
11275 when we do not have enough information to show inlined frames;
11276 pretend it's a local variable in that case so that the user can
11277 still see it. */
11278 if (context_stack_depth > 0
11279 && context_stack[context_stack_depth - 1].name != NULL)
11280 SYMBOL_IS_ARGUMENT (sym) = 1;
11281 attr = dwarf2_attr (die, DW_AT_location, cu);
11282 if (attr)
11283 {
11284 var_decode_location (attr, sym, cu);
11285 }
11286 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11287 if (attr)
11288 {
11289 dwarf2_const_value (attr, sym, cu);
11290 }
11291 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11292 if (attr && DW_UNSND (attr))
11293 {
11294 struct type *ref_type;
11295
11296 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11297 SYMBOL_TYPE (sym) = ref_type;
11298 }
11299
11300 list_to_add = cu->list_in_scope;
11301 break;
11302 case DW_TAG_unspecified_parameters:
11303 /* From varargs functions; gdb doesn't seem to have any
11304 interest in this information, so just ignore it for now.
11305 (FIXME?) */
11306 break;
11307 case DW_TAG_template_type_param:
11308 suppress_add = 1;
11309 /* Fall through. */
11310 case DW_TAG_class_type:
11311 case DW_TAG_interface_type:
11312 case DW_TAG_structure_type:
11313 case DW_TAG_union_type:
11314 case DW_TAG_set_type:
11315 case DW_TAG_enumeration_type:
11316 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11317 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11318
11319 {
11320 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11321 really ever be static objects: otherwise, if you try
11322 to, say, break of a class's method and you're in a file
11323 which doesn't mention that class, it won't work unless
11324 the check for all static symbols in lookup_symbol_aux
11325 saves you. See the OtherFileClass tests in
11326 gdb.c++/namespace.exp. */
11327
11328 if (!suppress_add)
11329 {
11330 list_to_add = (cu->list_in_scope == &file_symbols
11331 && (cu->language == language_cplus
11332 || cu->language == language_java)
11333 ? &global_symbols : cu->list_in_scope);
11334
11335 /* The semantics of C++ state that "struct foo {
11336 ... }" also defines a typedef for "foo". A Java
11337 class declaration also defines a typedef for the
11338 class. */
11339 if (cu->language == language_cplus
11340 || cu->language == language_java
11341 || cu->language == language_ada)
11342 {
11343 /* The symbol's name is already allocated along
11344 with this objfile, so we don't need to
11345 duplicate it for the type. */
11346 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11347 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11348 }
11349 }
11350 }
11351 break;
11352 case DW_TAG_typedef:
11353 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11354 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11355 list_to_add = cu->list_in_scope;
11356 break;
11357 case DW_TAG_base_type:
11358 case DW_TAG_subrange_type:
11359 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11360 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11361 list_to_add = cu->list_in_scope;
11362 break;
11363 case DW_TAG_enumerator:
11364 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11365 if (attr)
11366 {
11367 dwarf2_const_value (attr, sym, cu);
11368 }
11369 {
11370 /* NOTE: carlton/2003-11-10: See comment above in the
11371 DW_TAG_class_type, etc. block. */
11372
11373 list_to_add = (cu->list_in_scope == &file_symbols
11374 && (cu->language == language_cplus
11375 || cu->language == language_java)
11376 ? &global_symbols : cu->list_in_scope);
11377 }
11378 break;
11379 case DW_TAG_namespace:
11380 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11381 list_to_add = &global_symbols;
11382 break;
11383 default:
11384 /* Not a tag we recognize. Hopefully we aren't processing
11385 trash data, but since we must specifically ignore things
11386 we don't recognize, there is nothing else we should do at
11387 this point. */
11388 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11389 dwarf_tag_name (die->tag));
11390 break;
11391 }
11392
11393 if (suppress_add)
11394 {
11395 sym->hash_next = objfile->template_symbols;
11396 objfile->template_symbols = sym;
11397 list_to_add = NULL;
11398 }
11399
11400 if (list_to_add != NULL)
11401 add_symbol_to_list (sym, list_to_add);
11402
11403 /* For the benefit of old versions of GCC, check for anonymous
11404 namespaces based on the demangled name. */
11405 if (!processing_has_namespace_info
11406 && cu->language == language_cplus)
11407 cp_scan_for_anonymous_namespaces (sym);
11408 }
11409 return (sym);
11410 }
11411
11412 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11413
11414 static struct symbol *
11415 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11416 {
11417 return new_symbol_full (die, type, cu, NULL);
11418 }
11419
11420 /* Given an attr with a DW_FORM_dataN value in host byte order,
11421 zero-extend it as appropriate for the symbol's type. The DWARF
11422 standard (v4) is not entirely clear about the meaning of using
11423 DW_FORM_dataN for a constant with a signed type, where the type is
11424 wider than the data. The conclusion of a discussion on the DWARF
11425 list was that this is unspecified. We choose to always zero-extend
11426 because that is the interpretation long in use by GCC. */
11427
11428 static gdb_byte *
11429 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11430 const char *name, struct obstack *obstack,
11431 struct dwarf2_cu *cu, long *value, int bits)
11432 {
11433 struct objfile *objfile = cu->objfile;
11434 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11435 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11436 LONGEST l = DW_UNSND (attr);
11437
11438 if (bits < sizeof (*value) * 8)
11439 {
11440 l &= ((LONGEST) 1 << bits) - 1;
11441 *value = l;
11442 }
11443 else if (bits == sizeof (*value) * 8)
11444 *value = l;
11445 else
11446 {
11447 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11448 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11449 return bytes;
11450 }
11451
11452 return NULL;
11453 }
11454
11455 /* Read a constant value from an attribute. Either set *VALUE, or if
11456 the value does not fit in *VALUE, set *BYTES - either already
11457 allocated on the objfile obstack, or newly allocated on OBSTACK,
11458 or, set *BATON, if we translated the constant to a location
11459 expression. */
11460
11461 static void
11462 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11463 const char *name, struct obstack *obstack,
11464 struct dwarf2_cu *cu,
11465 long *value, gdb_byte **bytes,
11466 struct dwarf2_locexpr_baton **baton)
11467 {
11468 struct objfile *objfile = cu->objfile;
11469 struct comp_unit_head *cu_header = &cu->header;
11470 struct dwarf_block *blk;
11471 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11472 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11473
11474 *value = 0;
11475 *bytes = NULL;
11476 *baton = NULL;
11477
11478 switch (attr->form)
11479 {
11480 case DW_FORM_addr:
11481 {
11482 gdb_byte *data;
11483
11484 if (TYPE_LENGTH (type) != cu_header->addr_size)
11485 dwarf2_const_value_length_mismatch_complaint (name,
11486 cu_header->addr_size,
11487 TYPE_LENGTH (type));
11488 /* Symbols of this form are reasonably rare, so we just
11489 piggyback on the existing location code rather than writing
11490 a new implementation of symbol_computed_ops. */
11491 *baton = obstack_alloc (&objfile->objfile_obstack,
11492 sizeof (struct dwarf2_locexpr_baton));
11493 (*baton)->per_cu = cu->per_cu;
11494 gdb_assert ((*baton)->per_cu);
11495
11496 (*baton)->size = 2 + cu_header->addr_size;
11497 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11498 (*baton)->data = data;
11499
11500 data[0] = DW_OP_addr;
11501 store_unsigned_integer (&data[1], cu_header->addr_size,
11502 byte_order, DW_ADDR (attr));
11503 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11504 }
11505 break;
11506 case DW_FORM_string:
11507 case DW_FORM_strp:
11508 /* DW_STRING is already allocated on the objfile obstack, point
11509 directly to it. */
11510 *bytes = (gdb_byte *) DW_STRING (attr);
11511 break;
11512 case DW_FORM_block1:
11513 case DW_FORM_block2:
11514 case DW_FORM_block4:
11515 case DW_FORM_block:
11516 case DW_FORM_exprloc:
11517 blk = DW_BLOCK (attr);
11518 if (TYPE_LENGTH (type) != blk->size)
11519 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11520 TYPE_LENGTH (type));
11521 *bytes = blk->data;
11522 break;
11523
11524 /* The DW_AT_const_value attributes are supposed to carry the
11525 symbol's value "represented as it would be on the target
11526 architecture." By the time we get here, it's already been
11527 converted to host endianness, so we just need to sign- or
11528 zero-extend it as appropriate. */
11529 case DW_FORM_data1:
11530 *bytes = dwarf2_const_value_data (attr, type, name,
11531 obstack, cu, value, 8);
11532 break;
11533 case DW_FORM_data2:
11534 *bytes = dwarf2_const_value_data (attr, type, name,
11535 obstack, cu, value, 16);
11536 break;
11537 case DW_FORM_data4:
11538 *bytes = dwarf2_const_value_data (attr, type, name,
11539 obstack, cu, value, 32);
11540 break;
11541 case DW_FORM_data8:
11542 *bytes = dwarf2_const_value_data (attr, type, name,
11543 obstack, cu, value, 64);
11544 break;
11545
11546 case DW_FORM_sdata:
11547 *value = DW_SND (attr);
11548 break;
11549
11550 case DW_FORM_udata:
11551 *value = DW_UNSND (attr);
11552 break;
11553
11554 default:
11555 complaint (&symfile_complaints,
11556 _("unsupported const value attribute form: '%s'"),
11557 dwarf_form_name (attr->form));
11558 *value = 0;
11559 break;
11560 }
11561 }
11562
11563
11564 /* Copy constant value from an attribute to a symbol. */
11565
11566 static void
11567 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11568 struct dwarf2_cu *cu)
11569 {
11570 struct objfile *objfile = cu->objfile;
11571 struct comp_unit_head *cu_header = &cu->header;
11572 long value;
11573 gdb_byte *bytes;
11574 struct dwarf2_locexpr_baton *baton;
11575
11576 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11577 SYMBOL_PRINT_NAME (sym),
11578 &objfile->objfile_obstack, cu,
11579 &value, &bytes, &baton);
11580
11581 if (baton != NULL)
11582 {
11583 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11584 SYMBOL_LOCATION_BATON (sym) = baton;
11585 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11586 }
11587 else if (bytes != NULL)
11588 {
11589 SYMBOL_VALUE_BYTES (sym) = bytes;
11590 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11591 }
11592 else
11593 {
11594 SYMBOL_VALUE (sym) = value;
11595 SYMBOL_CLASS (sym) = LOC_CONST;
11596 }
11597 }
11598
11599 /* Return the type of the die in question using its DW_AT_type attribute. */
11600
11601 static struct type *
11602 die_type (struct die_info *die, struct dwarf2_cu *cu)
11603 {
11604 struct attribute *type_attr;
11605
11606 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11607 if (!type_attr)
11608 {
11609 /* A missing DW_AT_type represents a void type. */
11610 return objfile_type (cu->objfile)->builtin_void;
11611 }
11612
11613 return lookup_die_type (die, type_attr, cu);
11614 }
11615
11616 /* True iff CU's producer generates GNAT Ada auxiliary information
11617 that allows to find parallel types through that information instead
11618 of having to do expensive parallel lookups by type name. */
11619
11620 static int
11621 need_gnat_info (struct dwarf2_cu *cu)
11622 {
11623 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11624 of GNAT produces this auxiliary information, without any indication
11625 that it is produced. Part of enhancing the FSF version of GNAT
11626 to produce that information will be to put in place an indicator
11627 that we can use in order to determine whether the descriptive type
11628 info is available or not. One suggestion that has been made is
11629 to use a new attribute, attached to the CU die. For now, assume
11630 that the descriptive type info is not available. */
11631 return 0;
11632 }
11633
11634 /* Return the auxiliary type of the die in question using its
11635 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11636 attribute is not present. */
11637
11638 static struct type *
11639 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11640 {
11641 struct attribute *type_attr;
11642
11643 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11644 if (!type_attr)
11645 return NULL;
11646
11647 return lookup_die_type (die, type_attr, cu);
11648 }
11649
11650 /* If DIE has a descriptive_type attribute, then set the TYPE's
11651 descriptive type accordingly. */
11652
11653 static void
11654 set_descriptive_type (struct type *type, struct die_info *die,
11655 struct dwarf2_cu *cu)
11656 {
11657 struct type *descriptive_type = die_descriptive_type (die, cu);
11658
11659 if (descriptive_type)
11660 {
11661 ALLOCATE_GNAT_AUX_TYPE (type);
11662 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11663 }
11664 }
11665
11666 /* Return the containing type of the die in question using its
11667 DW_AT_containing_type attribute. */
11668
11669 static struct type *
11670 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11671 {
11672 struct attribute *type_attr;
11673
11674 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11675 if (!type_attr)
11676 error (_("Dwarf Error: Problem turning containing type into gdb type "
11677 "[in module %s]"), cu->objfile->name);
11678
11679 return lookup_die_type (die, type_attr, cu);
11680 }
11681
11682 /* Look up the type of DIE in CU using its type attribute ATTR.
11683 If there is no type substitute an error marker. */
11684
11685 static struct type *
11686 lookup_die_type (struct die_info *die, struct attribute *attr,
11687 struct dwarf2_cu *cu)
11688 {
11689 struct type *this_type;
11690
11691 /* First see if we have it cached. */
11692
11693 if (is_ref_attr (attr))
11694 {
11695 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11696
11697 this_type = get_die_type_at_offset (offset, cu->per_cu);
11698 }
11699 else if (attr->form == DW_FORM_ref_sig8)
11700 {
11701 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11702 struct dwarf2_cu *sig_cu;
11703 unsigned int offset;
11704
11705 /* sig_type will be NULL if the signatured type is missing from
11706 the debug info. */
11707 if (sig_type == NULL)
11708 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11709 "at 0x%x [in module %s]"),
11710 die->offset, cu->objfile->name);
11711
11712 gdb_assert (sig_type->per_cu.from_debug_types);
11713 offset = sig_type->per_cu.offset + sig_type->type_offset;
11714 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11715 }
11716 else
11717 {
11718 dump_die_for_error (die);
11719 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11720 dwarf_attr_name (attr->name), cu->objfile->name);
11721 }
11722
11723 /* If not cached we need to read it in. */
11724
11725 if (this_type == NULL)
11726 {
11727 struct die_info *type_die;
11728 struct dwarf2_cu *type_cu = cu;
11729
11730 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11731 /* If the type is cached, we should have found it above. */
11732 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11733 this_type = read_type_die_1 (type_die, type_cu);
11734 }
11735
11736 /* If we still don't have a type use an error marker. */
11737
11738 if (this_type == NULL)
11739 {
11740 char *message, *saved;
11741
11742 /* read_type_die already issued a complaint. */
11743 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11744 cu->objfile->name,
11745 cu->header.offset,
11746 die->offset);
11747 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11748 message, strlen (message));
11749 xfree (message);
11750
11751 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11752 }
11753
11754 return this_type;
11755 }
11756
11757 /* Return the type in DIE, CU.
11758 Returns NULL for invalid types.
11759
11760 This first does a lookup in the appropriate type_hash table,
11761 and only reads the die in if necessary.
11762
11763 NOTE: This can be called when reading in partial or full symbols. */
11764
11765 static struct type *
11766 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11767 {
11768 struct type *this_type;
11769
11770 this_type = get_die_type (die, cu);
11771 if (this_type)
11772 return this_type;
11773
11774 return read_type_die_1 (die, cu);
11775 }
11776
11777 /* Read the type in DIE, CU.
11778 Returns NULL for invalid types. */
11779
11780 static struct type *
11781 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11782 {
11783 struct type *this_type = NULL;
11784
11785 switch (die->tag)
11786 {
11787 case DW_TAG_class_type:
11788 case DW_TAG_interface_type:
11789 case DW_TAG_structure_type:
11790 case DW_TAG_union_type:
11791 this_type = read_structure_type (die, cu);
11792 break;
11793 case DW_TAG_enumeration_type:
11794 this_type = read_enumeration_type (die, cu);
11795 break;
11796 case DW_TAG_subprogram:
11797 case DW_TAG_subroutine_type:
11798 case DW_TAG_inlined_subroutine:
11799 this_type = read_subroutine_type (die, cu);
11800 break;
11801 case DW_TAG_array_type:
11802 this_type = read_array_type (die, cu);
11803 break;
11804 case DW_TAG_set_type:
11805 this_type = read_set_type (die, cu);
11806 break;
11807 case DW_TAG_pointer_type:
11808 this_type = read_tag_pointer_type (die, cu);
11809 break;
11810 case DW_TAG_ptr_to_member_type:
11811 this_type = read_tag_ptr_to_member_type (die, cu);
11812 break;
11813 case DW_TAG_reference_type:
11814 this_type = read_tag_reference_type (die, cu);
11815 break;
11816 case DW_TAG_const_type:
11817 this_type = read_tag_const_type (die, cu);
11818 break;
11819 case DW_TAG_volatile_type:
11820 this_type = read_tag_volatile_type (die, cu);
11821 break;
11822 case DW_TAG_string_type:
11823 this_type = read_tag_string_type (die, cu);
11824 break;
11825 case DW_TAG_typedef:
11826 this_type = read_typedef (die, cu);
11827 break;
11828 case DW_TAG_subrange_type:
11829 this_type = read_subrange_type (die, cu);
11830 break;
11831 case DW_TAG_base_type:
11832 this_type = read_base_type (die, cu);
11833 break;
11834 case DW_TAG_unspecified_type:
11835 this_type = read_unspecified_type (die, cu);
11836 break;
11837 case DW_TAG_namespace:
11838 this_type = read_namespace_type (die, cu);
11839 break;
11840 case DW_TAG_module:
11841 this_type = read_module_type (die, cu);
11842 break;
11843 default:
11844 complaint (&symfile_complaints,
11845 _("unexpected tag in read_type_die: '%s'"),
11846 dwarf_tag_name (die->tag));
11847 break;
11848 }
11849
11850 return this_type;
11851 }
11852
11853 /* See if we can figure out if the class lives in a namespace. We do
11854 this by looking for a member function; its demangled name will
11855 contain namespace info, if there is any.
11856 Return the computed name or NULL.
11857 Space for the result is allocated on the objfile's obstack.
11858 This is the full-die version of guess_partial_die_structure_name.
11859 In this case we know DIE has no useful parent. */
11860
11861 static char *
11862 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11863 {
11864 struct die_info *spec_die;
11865 struct dwarf2_cu *spec_cu;
11866 struct die_info *child;
11867
11868 spec_cu = cu;
11869 spec_die = die_specification (die, &spec_cu);
11870 if (spec_die != NULL)
11871 {
11872 die = spec_die;
11873 cu = spec_cu;
11874 }
11875
11876 for (child = die->child;
11877 child != NULL;
11878 child = child->sibling)
11879 {
11880 if (child->tag == DW_TAG_subprogram)
11881 {
11882 struct attribute *attr;
11883
11884 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11885 if (attr == NULL)
11886 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11887 if (attr != NULL)
11888 {
11889 char *actual_name
11890 = language_class_name_from_physname (cu->language_defn,
11891 DW_STRING (attr));
11892 char *name = NULL;
11893
11894 if (actual_name != NULL)
11895 {
11896 char *die_name = dwarf2_name (die, cu);
11897
11898 if (die_name != NULL
11899 && strcmp (die_name, actual_name) != 0)
11900 {
11901 /* Strip off the class name from the full name.
11902 We want the prefix. */
11903 int die_name_len = strlen (die_name);
11904 int actual_name_len = strlen (actual_name);
11905
11906 /* Test for '::' as a sanity check. */
11907 if (actual_name_len > die_name_len + 2
11908 && actual_name[actual_name_len
11909 - die_name_len - 1] == ':')
11910 name =
11911 obsavestring (actual_name,
11912 actual_name_len - die_name_len - 2,
11913 &cu->objfile->objfile_obstack);
11914 }
11915 }
11916 xfree (actual_name);
11917 return name;
11918 }
11919 }
11920 }
11921
11922 return NULL;
11923 }
11924
11925 /* Return the name of the namespace/class that DIE is defined within,
11926 or "" if we can't tell. The caller should not xfree the result.
11927
11928 For example, if we're within the method foo() in the following
11929 code:
11930
11931 namespace N {
11932 class C {
11933 void foo () {
11934 }
11935 };
11936 }
11937
11938 then determine_prefix on foo's die will return "N::C". */
11939
11940 static char *
11941 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11942 {
11943 struct die_info *parent, *spec_die;
11944 struct dwarf2_cu *spec_cu;
11945 struct type *parent_type;
11946
11947 if (cu->language != language_cplus && cu->language != language_java
11948 && cu->language != language_fortran)
11949 return "";
11950
11951 /* We have to be careful in the presence of DW_AT_specification.
11952 For example, with GCC 3.4, given the code
11953
11954 namespace N {
11955 void foo() {
11956 // Definition of N::foo.
11957 }
11958 }
11959
11960 then we'll have a tree of DIEs like this:
11961
11962 1: DW_TAG_compile_unit
11963 2: DW_TAG_namespace // N
11964 3: DW_TAG_subprogram // declaration of N::foo
11965 4: DW_TAG_subprogram // definition of N::foo
11966 DW_AT_specification // refers to die #3
11967
11968 Thus, when processing die #4, we have to pretend that we're in
11969 the context of its DW_AT_specification, namely the contex of die
11970 #3. */
11971 spec_cu = cu;
11972 spec_die = die_specification (die, &spec_cu);
11973 if (spec_die == NULL)
11974 parent = die->parent;
11975 else
11976 {
11977 parent = spec_die->parent;
11978 cu = spec_cu;
11979 }
11980
11981 if (parent == NULL)
11982 return "";
11983 else if (parent->building_fullname)
11984 {
11985 const char *name;
11986 const char *parent_name;
11987
11988 /* It has been seen on RealView 2.2 built binaries,
11989 DW_TAG_template_type_param types actually _defined_ as
11990 children of the parent class:
11991
11992 enum E {};
11993 template class <class Enum> Class{};
11994 Class<enum E> class_e;
11995
11996 1: DW_TAG_class_type (Class)
11997 2: DW_TAG_enumeration_type (E)
11998 3: DW_TAG_enumerator (enum1:0)
11999 3: DW_TAG_enumerator (enum2:1)
12000 ...
12001 2: DW_TAG_template_type_param
12002 DW_AT_type DW_FORM_ref_udata (E)
12003
12004 Besides being broken debug info, it can put GDB into an
12005 infinite loop. Consider:
12006
12007 When we're building the full name for Class<E>, we'll start
12008 at Class, and go look over its template type parameters,
12009 finding E. We'll then try to build the full name of E, and
12010 reach here. We're now trying to build the full name of E,
12011 and look over the parent DIE for containing scope. In the
12012 broken case, if we followed the parent DIE of E, we'd again
12013 find Class, and once again go look at its template type
12014 arguments, etc., etc. Simply don't consider such parent die
12015 as source-level parent of this die (it can't be, the language
12016 doesn't allow it), and break the loop here. */
12017 name = dwarf2_name (die, cu);
12018 parent_name = dwarf2_name (parent, cu);
12019 complaint (&symfile_complaints,
12020 _("template param type '%s' defined within parent '%s'"),
12021 name ? name : "<unknown>",
12022 parent_name ? parent_name : "<unknown>");
12023 return "";
12024 }
12025 else
12026 switch (parent->tag)
12027 {
12028 case DW_TAG_namespace:
12029 parent_type = read_type_die (parent, cu);
12030 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12031 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12032 Work around this problem here. */
12033 if (cu->language == language_cplus
12034 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12035 return "";
12036 /* We give a name to even anonymous namespaces. */
12037 return TYPE_TAG_NAME (parent_type);
12038 case DW_TAG_class_type:
12039 case DW_TAG_interface_type:
12040 case DW_TAG_structure_type:
12041 case DW_TAG_union_type:
12042 case DW_TAG_module:
12043 parent_type = read_type_die (parent, cu);
12044 if (TYPE_TAG_NAME (parent_type) != NULL)
12045 return TYPE_TAG_NAME (parent_type);
12046 else
12047 /* An anonymous structure is only allowed non-static data
12048 members; no typedefs, no member functions, et cetera.
12049 So it does not need a prefix. */
12050 return "";
12051 case DW_TAG_compile_unit:
12052 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12053 if (cu->language == language_cplus
12054 && dwarf2_per_objfile->types.asection != NULL
12055 && die->child != NULL
12056 && (die->tag == DW_TAG_class_type
12057 || die->tag == DW_TAG_structure_type
12058 || die->tag == DW_TAG_union_type))
12059 {
12060 char *name = guess_full_die_structure_name (die, cu);
12061 if (name != NULL)
12062 return name;
12063 }
12064 return "";
12065 default:
12066 return determine_prefix (parent, cu);
12067 }
12068 }
12069
12070 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12071 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12072 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12073 an obconcat, otherwise allocate storage for the result. The CU argument is
12074 used to determine the language and hence, the appropriate separator. */
12075
12076 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12077
12078 static char *
12079 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12080 int physname, struct dwarf2_cu *cu)
12081 {
12082 const char *lead = "";
12083 const char *sep;
12084
12085 if (suffix == NULL || suffix[0] == '\0'
12086 || prefix == NULL || prefix[0] == '\0')
12087 sep = "";
12088 else if (cu->language == language_java)
12089 sep = ".";
12090 else if (cu->language == language_fortran && physname)
12091 {
12092 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12093 DW_AT_MIPS_linkage_name is preferred and used instead. */
12094
12095 lead = "__";
12096 sep = "_MOD_";
12097 }
12098 else
12099 sep = "::";
12100
12101 if (prefix == NULL)
12102 prefix = "";
12103 if (suffix == NULL)
12104 suffix = "";
12105
12106 if (obs == NULL)
12107 {
12108 char *retval
12109 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12110
12111 strcpy (retval, lead);
12112 strcat (retval, prefix);
12113 strcat (retval, sep);
12114 strcat (retval, suffix);
12115 return retval;
12116 }
12117 else
12118 {
12119 /* We have an obstack. */
12120 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12121 }
12122 }
12123
12124 /* Return sibling of die, NULL if no sibling. */
12125
12126 static struct die_info *
12127 sibling_die (struct die_info *die)
12128 {
12129 return die->sibling;
12130 }
12131
12132 /* Get name of a die, return NULL if not found. */
12133
12134 static char *
12135 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12136 struct obstack *obstack)
12137 {
12138 if (name && cu->language == language_cplus)
12139 {
12140 char *canon_name = cp_canonicalize_string (name);
12141
12142 if (canon_name != NULL)
12143 {
12144 if (strcmp (canon_name, name) != 0)
12145 name = obsavestring (canon_name, strlen (canon_name),
12146 obstack);
12147 xfree (canon_name);
12148 }
12149 }
12150
12151 return name;
12152 }
12153
12154 /* Get name of a die, return NULL if not found. */
12155
12156 static char *
12157 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12158 {
12159 struct attribute *attr;
12160
12161 attr = dwarf2_attr (die, DW_AT_name, cu);
12162 if ((!attr || !DW_STRING (attr))
12163 && die->tag != DW_TAG_class_type
12164 && die->tag != DW_TAG_interface_type
12165 && die->tag != DW_TAG_structure_type
12166 && die->tag != DW_TAG_union_type)
12167 return NULL;
12168
12169 switch (die->tag)
12170 {
12171 case DW_TAG_compile_unit:
12172 /* Compilation units have a DW_AT_name that is a filename, not
12173 a source language identifier. */
12174 case DW_TAG_enumeration_type:
12175 case DW_TAG_enumerator:
12176 /* These tags always have simple identifiers already; no need
12177 to canonicalize them. */
12178 return DW_STRING (attr);
12179
12180 case DW_TAG_subprogram:
12181 /* Java constructors will all be named "<init>", so return
12182 the class name when we see this special case. */
12183 if (cu->language == language_java
12184 && DW_STRING (attr) != NULL
12185 && strcmp (DW_STRING (attr), "<init>") == 0)
12186 {
12187 struct dwarf2_cu *spec_cu = cu;
12188 struct die_info *spec_die;
12189
12190 /* GCJ will output '<init>' for Java constructor names.
12191 For this special case, return the name of the parent class. */
12192
12193 /* GCJ may output suprogram DIEs with AT_specification set.
12194 If so, use the name of the specified DIE. */
12195 spec_die = die_specification (die, &spec_cu);
12196 if (spec_die != NULL)
12197 return dwarf2_name (spec_die, spec_cu);
12198
12199 do
12200 {
12201 die = die->parent;
12202 if (die->tag == DW_TAG_class_type)
12203 return dwarf2_name (die, cu);
12204 }
12205 while (die->tag != DW_TAG_compile_unit);
12206 }
12207 break;
12208
12209 case DW_TAG_class_type:
12210 case DW_TAG_interface_type:
12211 case DW_TAG_structure_type:
12212 case DW_TAG_union_type:
12213 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12214 structures or unions. These were of the form "._%d" in GCC 4.1,
12215 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12216 and GCC 4.4. We work around this problem by ignoring these. */
12217 if (attr && DW_STRING (attr)
12218 && (strncmp (DW_STRING (attr), "._", 2) == 0
12219 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12220 return NULL;
12221
12222 /* GCC might emit a nameless typedef that has a linkage name. See
12223 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12224 if (!attr || DW_STRING (attr) == NULL)
12225 {
12226 char *demangled = NULL;
12227
12228 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12229 if (attr == NULL)
12230 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12231
12232 if (attr == NULL || DW_STRING (attr) == NULL)
12233 return NULL;
12234
12235 /* Avoid demangling DW_STRING (attr) the second time on a second
12236 call for the same DIE. */
12237 if (!DW_STRING_IS_CANONICAL (attr))
12238 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12239
12240 if (demangled)
12241 {
12242 /* FIXME: we already did this for the partial symbol... */
12243 DW_STRING (attr)
12244 = obsavestring (demangled, strlen (demangled),
12245 &cu->objfile->objfile_obstack);
12246 DW_STRING_IS_CANONICAL (attr) = 1;
12247 xfree (demangled);
12248 }
12249 }
12250 break;
12251
12252 default:
12253 break;
12254 }
12255
12256 if (!DW_STRING_IS_CANONICAL (attr))
12257 {
12258 DW_STRING (attr)
12259 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12260 &cu->objfile->objfile_obstack);
12261 DW_STRING_IS_CANONICAL (attr) = 1;
12262 }
12263 return DW_STRING (attr);
12264 }
12265
12266 /* Return the die that this die in an extension of, or NULL if there
12267 is none. *EXT_CU is the CU containing DIE on input, and the CU
12268 containing the return value on output. */
12269
12270 static struct die_info *
12271 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12272 {
12273 struct attribute *attr;
12274
12275 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12276 if (attr == NULL)
12277 return NULL;
12278
12279 return follow_die_ref (die, attr, ext_cu);
12280 }
12281
12282 /* Convert a DIE tag into its string name. */
12283
12284 static char *
12285 dwarf_tag_name (unsigned tag)
12286 {
12287 switch (tag)
12288 {
12289 case DW_TAG_padding:
12290 return "DW_TAG_padding";
12291 case DW_TAG_array_type:
12292 return "DW_TAG_array_type";
12293 case DW_TAG_class_type:
12294 return "DW_TAG_class_type";
12295 case DW_TAG_entry_point:
12296 return "DW_TAG_entry_point";
12297 case DW_TAG_enumeration_type:
12298 return "DW_TAG_enumeration_type";
12299 case DW_TAG_formal_parameter:
12300 return "DW_TAG_formal_parameter";
12301 case DW_TAG_imported_declaration:
12302 return "DW_TAG_imported_declaration";
12303 case DW_TAG_label:
12304 return "DW_TAG_label";
12305 case DW_TAG_lexical_block:
12306 return "DW_TAG_lexical_block";
12307 case DW_TAG_member:
12308 return "DW_TAG_member";
12309 case DW_TAG_pointer_type:
12310 return "DW_TAG_pointer_type";
12311 case DW_TAG_reference_type:
12312 return "DW_TAG_reference_type";
12313 case DW_TAG_compile_unit:
12314 return "DW_TAG_compile_unit";
12315 case DW_TAG_string_type:
12316 return "DW_TAG_string_type";
12317 case DW_TAG_structure_type:
12318 return "DW_TAG_structure_type";
12319 case DW_TAG_subroutine_type:
12320 return "DW_TAG_subroutine_type";
12321 case DW_TAG_typedef:
12322 return "DW_TAG_typedef";
12323 case DW_TAG_union_type:
12324 return "DW_TAG_union_type";
12325 case DW_TAG_unspecified_parameters:
12326 return "DW_TAG_unspecified_parameters";
12327 case DW_TAG_variant:
12328 return "DW_TAG_variant";
12329 case DW_TAG_common_block:
12330 return "DW_TAG_common_block";
12331 case DW_TAG_common_inclusion:
12332 return "DW_TAG_common_inclusion";
12333 case DW_TAG_inheritance:
12334 return "DW_TAG_inheritance";
12335 case DW_TAG_inlined_subroutine:
12336 return "DW_TAG_inlined_subroutine";
12337 case DW_TAG_module:
12338 return "DW_TAG_module";
12339 case DW_TAG_ptr_to_member_type:
12340 return "DW_TAG_ptr_to_member_type";
12341 case DW_TAG_set_type:
12342 return "DW_TAG_set_type";
12343 case DW_TAG_subrange_type:
12344 return "DW_TAG_subrange_type";
12345 case DW_TAG_with_stmt:
12346 return "DW_TAG_with_stmt";
12347 case DW_TAG_access_declaration:
12348 return "DW_TAG_access_declaration";
12349 case DW_TAG_base_type:
12350 return "DW_TAG_base_type";
12351 case DW_TAG_catch_block:
12352 return "DW_TAG_catch_block";
12353 case DW_TAG_const_type:
12354 return "DW_TAG_const_type";
12355 case DW_TAG_constant:
12356 return "DW_TAG_constant";
12357 case DW_TAG_enumerator:
12358 return "DW_TAG_enumerator";
12359 case DW_TAG_file_type:
12360 return "DW_TAG_file_type";
12361 case DW_TAG_friend:
12362 return "DW_TAG_friend";
12363 case DW_TAG_namelist:
12364 return "DW_TAG_namelist";
12365 case DW_TAG_namelist_item:
12366 return "DW_TAG_namelist_item";
12367 case DW_TAG_packed_type:
12368 return "DW_TAG_packed_type";
12369 case DW_TAG_subprogram:
12370 return "DW_TAG_subprogram";
12371 case DW_TAG_template_type_param:
12372 return "DW_TAG_template_type_param";
12373 case DW_TAG_template_value_param:
12374 return "DW_TAG_template_value_param";
12375 case DW_TAG_thrown_type:
12376 return "DW_TAG_thrown_type";
12377 case DW_TAG_try_block:
12378 return "DW_TAG_try_block";
12379 case DW_TAG_variant_part:
12380 return "DW_TAG_variant_part";
12381 case DW_TAG_variable:
12382 return "DW_TAG_variable";
12383 case DW_TAG_volatile_type:
12384 return "DW_TAG_volatile_type";
12385 case DW_TAG_dwarf_procedure:
12386 return "DW_TAG_dwarf_procedure";
12387 case DW_TAG_restrict_type:
12388 return "DW_TAG_restrict_type";
12389 case DW_TAG_interface_type:
12390 return "DW_TAG_interface_type";
12391 case DW_TAG_namespace:
12392 return "DW_TAG_namespace";
12393 case DW_TAG_imported_module:
12394 return "DW_TAG_imported_module";
12395 case DW_TAG_unspecified_type:
12396 return "DW_TAG_unspecified_type";
12397 case DW_TAG_partial_unit:
12398 return "DW_TAG_partial_unit";
12399 case DW_TAG_imported_unit:
12400 return "DW_TAG_imported_unit";
12401 case DW_TAG_condition:
12402 return "DW_TAG_condition";
12403 case DW_TAG_shared_type:
12404 return "DW_TAG_shared_type";
12405 case DW_TAG_type_unit:
12406 return "DW_TAG_type_unit";
12407 case DW_TAG_MIPS_loop:
12408 return "DW_TAG_MIPS_loop";
12409 case DW_TAG_HP_array_descriptor:
12410 return "DW_TAG_HP_array_descriptor";
12411 case DW_TAG_format_label:
12412 return "DW_TAG_format_label";
12413 case DW_TAG_function_template:
12414 return "DW_TAG_function_template";
12415 case DW_TAG_class_template:
12416 return "DW_TAG_class_template";
12417 case DW_TAG_GNU_BINCL:
12418 return "DW_TAG_GNU_BINCL";
12419 case DW_TAG_GNU_EINCL:
12420 return "DW_TAG_GNU_EINCL";
12421 case DW_TAG_upc_shared_type:
12422 return "DW_TAG_upc_shared_type";
12423 case DW_TAG_upc_strict_type:
12424 return "DW_TAG_upc_strict_type";
12425 case DW_TAG_upc_relaxed_type:
12426 return "DW_TAG_upc_relaxed_type";
12427 case DW_TAG_PGI_kanji_type:
12428 return "DW_TAG_PGI_kanji_type";
12429 case DW_TAG_PGI_interface_block:
12430 return "DW_TAG_PGI_interface_block";
12431 default:
12432 return "DW_TAG_<unknown>";
12433 }
12434 }
12435
12436 /* Convert a DWARF attribute code into its string name. */
12437
12438 static char *
12439 dwarf_attr_name (unsigned attr)
12440 {
12441 switch (attr)
12442 {
12443 case DW_AT_sibling:
12444 return "DW_AT_sibling";
12445 case DW_AT_location:
12446 return "DW_AT_location";
12447 case DW_AT_name:
12448 return "DW_AT_name";
12449 case DW_AT_ordering:
12450 return "DW_AT_ordering";
12451 case DW_AT_subscr_data:
12452 return "DW_AT_subscr_data";
12453 case DW_AT_byte_size:
12454 return "DW_AT_byte_size";
12455 case DW_AT_bit_offset:
12456 return "DW_AT_bit_offset";
12457 case DW_AT_bit_size:
12458 return "DW_AT_bit_size";
12459 case DW_AT_element_list:
12460 return "DW_AT_element_list";
12461 case DW_AT_stmt_list:
12462 return "DW_AT_stmt_list";
12463 case DW_AT_low_pc:
12464 return "DW_AT_low_pc";
12465 case DW_AT_high_pc:
12466 return "DW_AT_high_pc";
12467 case DW_AT_language:
12468 return "DW_AT_language";
12469 case DW_AT_member:
12470 return "DW_AT_member";
12471 case DW_AT_discr:
12472 return "DW_AT_discr";
12473 case DW_AT_discr_value:
12474 return "DW_AT_discr_value";
12475 case DW_AT_visibility:
12476 return "DW_AT_visibility";
12477 case DW_AT_import:
12478 return "DW_AT_import";
12479 case DW_AT_string_length:
12480 return "DW_AT_string_length";
12481 case DW_AT_common_reference:
12482 return "DW_AT_common_reference";
12483 case DW_AT_comp_dir:
12484 return "DW_AT_comp_dir";
12485 case DW_AT_const_value:
12486 return "DW_AT_const_value";
12487 case DW_AT_containing_type:
12488 return "DW_AT_containing_type";
12489 case DW_AT_default_value:
12490 return "DW_AT_default_value";
12491 case DW_AT_inline:
12492 return "DW_AT_inline";
12493 case DW_AT_is_optional:
12494 return "DW_AT_is_optional";
12495 case DW_AT_lower_bound:
12496 return "DW_AT_lower_bound";
12497 case DW_AT_producer:
12498 return "DW_AT_producer";
12499 case DW_AT_prototyped:
12500 return "DW_AT_prototyped";
12501 case DW_AT_return_addr:
12502 return "DW_AT_return_addr";
12503 case DW_AT_start_scope:
12504 return "DW_AT_start_scope";
12505 case DW_AT_bit_stride:
12506 return "DW_AT_bit_stride";
12507 case DW_AT_upper_bound:
12508 return "DW_AT_upper_bound";
12509 case DW_AT_abstract_origin:
12510 return "DW_AT_abstract_origin";
12511 case DW_AT_accessibility:
12512 return "DW_AT_accessibility";
12513 case DW_AT_address_class:
12514 return "DW_AT_address_class";
12515 case DW_AT_artificial:
12516 return "DW_AT_artificial";
12517 case DW_AT_base_types:
12518 return "DW_AT_base_types";
12519 case DW_AT_calling_convention:
12520 return "DW_AT_calling_convention";
12521 case DW_AT_count:
12522 return "DW_AT_count";
12523 case DW_AT_data_member_location:
12524 return "DW_AT_data_member_location";
12525 case DW_AT_decl_column:
12526 return "DW_AT_decl_column";
12527 case DW_AT_decl_file:
12528 return "DW_AT_decl_file";
12529 case DW_AT_decl_line:
12530 return "DW_AT_decl_line";
12531 case DW_AT_declaration:
12532 return "DW_AT_declaration";
12533 case DW_AT_discr_list:
12534 return "DW_AT_discr_list";
12535 case DW_AT_encoding:
12536 return "DW_AT_encoding";
12537 case DW_AT_external:
12538 return "DW_AT_external";
12539 case DW_AT_frame_base:
12540 return "DW_AT_frame_base";
12541 case DW_AT_friend:
12542 return "DW_AT_friend";
12543 case DW_AT_identifier_case:
12544 return "DW_AT_identifier_case";
12545 case DW_AT_macro_info:
12546 return "DW_AT_macro_info";
12547 case DW_AT_namelist_items:
12548 return "DW_AT_namelist_items";
12549 case DW_AT_priority:
12550 return "DW_AT_priority";
12551 case DW_AT_segment:
12552 return "DW_AT_segment";
12553 case DW_AT_specification:
12554 return "DW_AT_specification";
12555 case DW_AT_static_link:
12556 return "DW_AT_static_link";
12557 case DW_AT_type:
12558 return "DW_AT_type";
12559 case DW_AT_use_location:
12560 return "DW_AT_use_location";
12561 case DW_AT_variable_parameter:
12562 return "DW_AT_variable_parameter";
12563 case DW_AT_virtuality:
12564 return "DW_AT_virtuality";
12565 case DW_AT_vtable_elem_location:
12566 return "DW_AT_vtable_elem_location";
12567 /* DWARF 3 values. */
12568 case DW_AT_allocated:
12569 return "DW_AT_allocated";
12570 case DW_AT_associated:
12571 return "DW_AT_associated";
12572 case DW_AT_data_location:
12573 return "DW_AT_data_location";
12574 case DW_AT_byte_stride:
12575 return "DW_AT_byte_stride";
12576 case DW_AT_entry_pc:
12577 return "DW_AT_entry_pc";
12578 case DW_AT_use_UTF8:
12579 return "DW_AT_use_UTF8";
12580 case DW_AT_extension:
12581 return "DW_AT_extension";
12582 case DW_AT_ranges:
12583 return "DW_AT_ranges";
12584 case DW_AT_trampoline:
12585 return "DW_AT_trampoline";
12586 case DW_AT_call_column:
12587 return "DW_AT_call_column";
12588 case DW_AT_call_file:
12589 return "DW_AT_call_file";
12590 case DW_AT_call_line:
12591 return "DW_AT_call_line";
12592 case DW_AT_description:
12593 return "DW_AT_description";
12594 case DW_AT_binary_scale:
12595 return "DW_AT_binary_scale";
12596 case DW_AT_decimal_scale:
12597 return "DW_AT_decimal_scale";
12598 case DW_AT_small:
12599 return "DW_AT_small";
12600 case DW_AT_decimal_sign:
12601 return "DW_AT_decimal_sign";
12602 case DW_AT_digit_count:
12603 return "DW_AT_digit_count";
12604 case DW_AT_picture_string:
12605 return "DW_AT_picture_string";
12606 case DW_AT_mutable:
12607 return "DW_AT_mutable";
12608 case DW_AT_threads_scaled:
12609 return "DW_AT_threads_scaled";
12610 case DW_AT_explicit:
12611 return "DW_AT_explicit";
12612 case DW_AT_object_pointer:
12613 return "DW_AT_object_pointer";
12614 case DW_AT_endianity:
12615 return "DW_AT_endianity";
12616 case DW_AT_elemental:
12617 return "DW_AT_elemental";
12618 case DW_AT_pure:
12619 return "DW_AT_pure";
12620 case DW_AT_recursive:
12621 return "DW_AT_recursive";
12622 /* DWARF 4 values. */
12623 case DW_AT_signature:
12624 return "DW_AT_signature";
12625 case DW_AT_linkage_name:
12626 return "DW_AT_linkage_name";
12627 /* SGI/MIPS extensions. */
12628 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12629 case DW_AT_MIPS_fde:
12630 return "DW_AT_MIPS_fde";
12631 #endif
12632 case DW_AT_MIPS_loop_begin:
12633 return "DW_AT_MIPS_loop_begin";
12634 case DW_AT_MIPS_tail_loop_begin:
12635 return "DW_AT_MIPS_tail_loop_begin";
12636 case DW_AT_MIPS_epilog_begin:
12637 return "DW_AT_MIPS_epilog_begin";
12638 case DW_AT_MIPS_loop_unroll_factor:
12639 return "DW_AT_MIPS_loop_unroll_factor";
12640 case DW_AT_MIPS_software_pipeline_depth:
12641 return "DW_AT_MIPS_software_pipeline_depth";
12642 case DW_AT_MIPS_linkage_name:
12643 return "DW_AT_MIPS_linkage_name";
12644 case DW_AT_MIPS_stride:
12645 return "DW_AT_MIPS_stride";
12646 case DW_AT_MIPS_abstract_name:
12647 return "DW_AT_MIPS_abstract_name";
12648 case DW_AT_MIPS_clone_origin:
12649 return "DW_AT_MIPS_clone_origin";
12650 case DW_AT_MIPS_has_inlines:
12651 return "DW_AT_MIPS_has_inlines";
12652 /* HP extensions. */
12653 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12654 case DW_AT_HP_block_index:
12655 return "DW_AT_HP_block_index";
12656 #endif
12657 case DW_AT_HP_unmodifiable:
12658 return "DW_AT_HP_unmodifiable";
12659 case DW_AT_HP_actuals_stmt_list:
12660 return "DW_AT_HP_actuals_stmt_list";
12661 case DW_AT_HP_proc_per_section:
12662 return "DW_AT_HP_proc_per_section";
12663 case DW_AT_HP_raw_data_ptr:
12664 return "DW_AT_HP_raw_data_ptr";
12665 case DW_AT_HP_pass_by_reference:
12666 return "DW_AT_HP_pass_by_reference";
12667 case DW_AT_HP_opt_level:
12668 return "DW_AT_HP_opt_level";
12669 case DW_AT_HP_prof_version_id:
12670 return "DW_AT_HP_prof_version_id";
12671 case DW_AT_HP_opt_flags:
12672 return "DW_AT_HP_opt_flags";
12673 case DW_AT_HP_cold_region_low_pc:
12674 return "DW_AT_HP_cold_region_low_pc";
12675 case DW_AT_HP_cold_region_high_pc:
12676 return "DW_AT_HP_cold_region_high_pc";
12677 case DW_AT_HP_all_variables_modifiable:
12678 return "DW_AT_HP_all_variables_modifiable";
12679 case DW_AT_HP_linkage_name:
12680 return "DW_AT_HP_linkage_name";
12681 case DW_AT_HP_prof_flags:
12682 return "DW_AT_HP_prof_flags";
12683 /* GNU extensions. */
12684 case DW_AT_sf_names:
12685 return "DW_AT_sf_names";
12686 case DW_AT_src_info:
12687 return "DW_AT_src_info";
12688 case DW_AT_mac_info:
12689 return "DW_AT_mac_info";
12690 case DW_AT_src_coords:
12691 return "DW_AT_src_coords";
12692 case DW_AT_body_begin:
12693 return "DW_AT_body_begin";
12694 case DW_AT_body_end:
12695 return "DW_AT_body_end";
12696 case DW_AT_GNU_vector:
12697 return "DW_AT_GNU_vector";
12698 case DW_AT_GNU_odr_signature:
12699 return "DW_AT_GNU_odr_signature";
12700 /* VMS extensions. */
12701 case DW_AT_VMS_rtnbeg_pd_address:
12702 return "DW_AT_VMS_rtnbeg_pd_address";
12703 /* UPC extension. */
12704 case DW_AT_upc_threads_scaled:
12705 return "DW_AT_upc_threads_scaled";
12706 /* PGI (STMicroelectronics) extensions. */
12707 case DW_AT_PGI_lbase:
12708 return "DW_AT_PGI_lbase";
12709 case DW_AT_PGI_soffset:
12710 return "DW_AT_PGI_soffset";
12711 case DW_AT_PGI_lstride:
12712 return "DW_AT_PGI_lstride";
12713 default:
12714 return "DW_AT_<unknown>";
12715 }
12716 }
12717
12718 /* Convert a DWARF value form code into its string name. */
12719
12720 static char *
12721 dwarf_form_name (unsigned form)
12722 {
12723 switch (form)
12724 {
12725 case DW_FORM_addr:
12726 return "DW_FORM_addr";
12727 case DW_FORM_block2:
12728 return "DW_FORM_block2";
12729 case DW_FORM_block4:
12730 return "DW_FORM_block4";
12731 case DW_FORM_data2:
12732 return "DW_FORM_data2";
12733 case DW_FORM_data4:
12734 return "DW_FORM_data4";
12735 case DW_FORM_data8:
12736 return "DW_FORM_data8";
12737 case DW_FORM_string:
12738 return "DW_FORM_string";
12739 case DW_FORM_block:
12740 return "DW_FORM_block";
12741 case DW_FORM_block1:
12742 return "DW_FORM_block1";
12743 case DW_FORM_data1:
12744 return "DW_FORM_data1";
12745 case DW_FORM_flag:
12746 return "DW_FORM_flag";
12747 case DW_FORM_sdata:
12748 return "DW_FORM_sdata";
12749 case DW_FORM_strp:
12750 return "DW_FORM_strp";
12751 case DW_FORM_udata:
12752 return "DW_FORM_udata";
12753 case DW_FORM_ref_addr:
12754 return "DW_FORM_ref_addr";
12755 case DW_FORM_ref1:
12756 return "DW_FORM_ref1";
12757 case DW_FORM_ref2:
12758 return "DW_FORM_ref2";
12759 case DW_FORM_ref4:
12760 return "DW_FORM_ref4";
12761 case DW_FORM_ref8:
12762 return "DW_FORM_ref8";
12763 case DW_FORM_ref_udata:
12764 return "DW_FORM_ref_udata";
12765 case DW_FORM_indirect:
12766 return "DW_FORM_indirect";
12767 case DW_FORM_sec_offset:
12768 return "DW_FORM_sec_offset";
12769 case DW_FORM_exprloc:
12770 return "DW_FORM_exprloc";
12771 case DW_FORM_flag_present:
12772 return "DW_FORM_flag_present";
12773 case DW_FORM_ref_sig8:
12774 return "DW_FORM_ref_sig8";
12775 default:
12776 return "DW_FORM_<unknown>";
12777 }
12778 }
12779
12780 /* Convert a DWARF stack opcode into its string name. */
12781
12782 const char *
12783 dwarf_stack_op_name (unsigned op)
12784 {
12785 switch (op)
12786 {
12787 case DW_OP_addr:
12788 return "DW_OP_addr";
12789 case DW_OP_deref:
12790 return "DW_OP_deref";
12791 case DW_OP_const1u:
12792 return "DW_OP_const1u";
12793 case DW_OP_const1s:
12794 return "DW_OP_const1s";
12795 case DW_OP_const2u:
12796 return "DW_OP_const2u";
12797 case DW_OP_const2s:
12798 return "DW_OP_const2s";
12799 case DW_OP_const4u:
12800 return "DW_OP_const4u";
12801 case DW_OP_const4s:
12802 return "DW_OP_const4s";
12803 case DW_OP_const8u:
12804 return "DW_OP_const8u";
12805 case DW_OP_const8s:
12806 return "DW_OP_const8s";
12807 case DW_OP_constu:
12808 return "DW_OP_constu";
12809 case DW_OP_consts:
12810 return "DW_OP_consts";
12811 case DW_OP_dup:
12812 return "DW_OP_dup";
12813 case DW_OP_drop:
12814 return "DW_OP_drop";
12815 case DW_OP_over:
12816 return "DW_OP_over";
12817 case DW_OP_pick:
12818 return "DW_OP_pick";
12819 case DW_OP_swap:
12820 return "DW_OP_swap";
12821 case DW_OP_rot:
12822 return "DW_OP_rot";
12823 case DW_OP_xderef:
12824 return "DW_OP_xderef";
12825 case DW_OP_abs:
12826 return "DW_OP_abs";
12827 case DW_OP_and:
12828 return "DW_OP_and";
12829 case DW_OP_div:
12830 return "DW_OP_div";
12831 case DW_OP_minus:
12832 return "DW_OP_minus";
12833 case DW_OP_mod:
12834 return "DW_OP_mod";
12835 case DW_OP_mul:
12836 return "DW_OP_mul";
12837 case DW_OP_neg:
12838 return "DW_OP_neg";
12839 case DW_OP_not:
12840 return "DW_OP_not";
12841 case DW_OP_or:
12842 return "DW_OP_or";
12843 case DW_OP_plus:
12844 return "DW_OP_plus";
12845 case DW_OP_plus_uconst:
12846 return "DW_OP_plus_uconst";
12847 case DW_OP_shl:
12848 return "DW_OP_shl";
12849 case DW_OP_shr:
12850 return "DW_OP_shr";
12851 case DW_OP_shra:
12852 return "DW_OP_shra";
12853 case DW_OP_xor:
12854 return "DW_OP_xor";
12855 case DW_OP_bra:
12856 return "DW_OP_bra";
12857 case DW_OP_eq:
12858 return "DW_OP_eq";
12859 case DW_OP_ge:
12860 return "DW_OP_ge";
12861 case DW_OP_gt:
12862 return "DW_OP_gt";
12863 case DW_OP_le:
12864 return "DW_OP_le";
12865 case DW_OP_lt:
12866 return "DW_OP_lt";
12867 case DW_OP_ne:
12868 return "DW_OP_ne";
12869 case DW_OP_skip:
12870 return "DW_OP_skip";
12871 case DW_OP_lit0:
12872 return "DW_OP_lit0";
12873 case DW_OP_lit1:
12874 return "DW_OP_lit1";
12875 case DW_OP_lit2:
12876 return "DW_OP_lit2";
12877 case DW_OP_lit3:
12878 return "DW_OP_lit3";
12879 case DW_OP_lit4:
12880 return "DW_OP_lit4";
12881 case DW_OP_lit5:
12882 return "DW_OP_lit5";
12883 case DW_OP_lit6:
12884 return "DW_OP_lit6";
12885 case DW_OP_lit7:
12886 return "DW_OP_lit7";
12887 case DW_OP_lit8:
12888 return "DW_OP_lit8";
12889 case DW_OP_lit9:
12890 return "DW_OP_lit9";
12891 case DW_OP_lit10:
12892 return "DW_OP_lit10";
12893 case DW_OP_lit11:
12894 return "DW_OP_lit11";
12895 case DW_OP_lit12:
12896 return "DW_OP_lit12";
12897 case DW_OP_lit13:
12898 return "DW_OP_lit13";
12899 case DW_OP_lit14:
12900 return "DW_OP_lit14";
12901 case DW_OP_lit15:
12902 return "DW_OP_lit15";
12903 case DW_OP_lit16:
12904 return "DW_OP_lit16";
12905 case DW_OP_lit17:
12906 return "DW_OP_lit17";
12907 case DW_OP_lit18:
12908 return "DW_OP_lit18";
12909 case DW_OP_lit19:
12910 return "DW_OP_lit19";
12911 case DW_OP_lit20:
12912 return "DW_OP_lit20";
12913 case DW_OP_lit21:
12914 return "DW_OP_lit21";
12915 case DW_OP_lit22:
12916 return "DW_OP_lit22";
12917 case DW_OP_lit23:
12918 return "DW_OP_lit23";
12919 case DW_OP_lit24:
12920 return "DW_OP_lit24";
12921 case DW_OP_lit25:
12922 return "DW_OP_lit25";
12923 case DW_OP_lit26:
12924 return "DW_OP_lit26";
12925 case DW_OP_lit27:
12926 return "DW_OP_lit27";
12927 case DW_OP_lit28:
12928 return "DW_OP_lit28";
12929 case DW_OP_lit29:
12930 return "DW_OP_lit29";
12931 case DW_OP_lit30:
12932 return "DW_OP_lit30";
12933 case DW_OP_lit31:
12934 return "DW_OP_lit31";
12935 case DW_OP_reg0:
12936 return "DW_OP_reg0";
12937 case DW_OP_reg1:
12938 return "DW_OP_reg1";
12939 case DW_OP_reg2:
12940 return "DW_OP_reg2";
12941 case DW_OP_reg3:
12942 return "DW_OP_reg3";
12943 case DW_OP_reg4:
12944 return "DW_OP_reg4";
12945 case DW_OP_reg5:
12946 return "DW_OP_reg5";
12947 case DW_OP_reg6:
12948 return "DW_OP_reg6";
12949 case DW_OP_reg7:
12950 return "DW_OP_reg7";
12951 case DW_OP_reg8:
12952 return "DW_OP_reg8";
12953 case DW_OP_reg9:
12954 return "DW_OP_reg9";
12955 case DW_OP_reg10:
12956 return "DW_OP_reg10";
12957 case DW_OP_reg11:
12958 return "DW_OP_reg11";
12959 case DW_OP_reg12:
12960 return "DW_OP_reg12";
12961 case DW_OP_reg13:
12962 return "DW_OP_reg13";
12963 case DW_OP_reg14:
12964 return "DW_OP_reg14";
12965 case DW_OP_reg15:
12966 return "DW_OP_reg15";
12967 case DW_OP_reg16:
12968 return "DW_OP_reg16";
12969 case DW_OP_reg17:
12970 return "DW_OP_reg17";
12971 case DW_OP_reg18:
12972 return "DW_OP_reg18";
12973 case DW_OP_reg19:
12974 return "DW_OP_reg19";
12975 case DW_OP_reg20:
12976 return "DW_OP_reg20";
12977 case DW_OP_reg21:
12978 return "DW_OP_reg21";
12979 case DW_OP_reg22:
12980 return "DW_OP_reg22";
12981 case DW_OP_reg23:
12982 return "DW_OP_reg23";
12983 case DW_OP_reg24:
12984 return "DW_OP_reg24";
12985 case DW_OP_reg25:
12986 return "DW_OP_reg25";
12987 case DW_OP_reg26:
12988 return "DW_OP_reg26";
12989 case DW_OP_reg27:
12990 return "DW_OP_reg27";
12991 case DW_OP_reg28:
12992 return "DW_OP_reg28";
12993 case DW_OP_reg29:
12994 return "DW_OP_reg29";
12995 case DW_OP_reg30:
12996 return "DW_OP_reg30";
12997 case DW_OP_reg31:
12998 return "DW_OP_reg31";
12999 case DW_OP_breg0:
13000 return "DW_OP_breg0";
13001 case DW_OP_breg1:
13002 return "DW_OP_breg1";
13003 case DW_OP_breg2:
13004 return "DW_OP_breg2";
13005 case DW_OP_breg3:
13006 return "DW_OP_breg3";
13007 case DW_OP_breg4:
13008 return "DW_OP_breg4";
13009 case DW_OP_breg5:
13010 return "DW_OP_breg5";
13011 case DW_OP_breg6:
13012 return "DW_OP_breg6";
13013 case DW_OP_breg7:
13014 return "DW_OP_breg7";
13015 case DW_OP_breg8:
13016 return "DW_OP_breg8";
13017 case DW_OP_breg9:
13018 return "DW_OP_breg9";
13019 case DW_OP_breg10:
13020 return "DW_OP_breg10";
13021 case DW_OP_breg11:
13022 return "DW_OP_breg11";
13023 case DW_OP_breg12:
13024 return "DW_OP_breg12";
13025 case DW_OP_breg13:
13026 return "DW_OP_breg13";
13027 case DW_OP_breg14:
13028 return "DW_OP_breg14";
13029 case DW_OP_breg15:
13030 return "DW_OP_breg15";
13031 case DW_OP_breg16:
13032 return "DW_OP_breg16";
13033 case DW_OP_breg17:
13034 return "DW_OP_breg17";
13035 case DW_OP_breg18:
13036 return "DW_OP_breg18";
13037 case DW_OP_breg19:
13038 return "DW_OP_breg19";
13039 case DW_OP_breg20:
13040 return "DW_OP_breg20";
13041 case DW_OP_breg21:
13042 return "DW_OP_breg21";
13043 case DW_OP_breg22:
13044 return "DW_OP_breg22";
13045 case DW_OP_breg23:
13046 return "DW_OP_breg23";
13047 case DW_OP_breg24:
13048 return "DW_OP_breg24";
13049 case DW_OP_breg25:
13050 return "DW_OP_breg25";
13051 case DW_OP_breg26:
13052 return "DW_OP_breg26";
13053 case DW_OP_breg27:
13054 return "DW_OP_breg27";
13055 case DW_OP_breg28:
13056 return "DW_OP_breg28";
13057 case DW_OP_breg29:
13058 return "DW_OP_breg29";
13059 case DW_OP_breg30:
13060 return "DW_OP_breg30";
13061 case DW_OP_breg31:
13062 return "DW_OP_breg31";
13063 case DW_OP_regx:
13064 return "DW_OP_regx";
13065 case DW_OP_fbreg:
13066 return "DW_OP_fbreg";
13067 case DW_OP_bregx:
13068 return "DW_OP_bregx";
13069 case DW_OP_piece:
13070 return "DW_OP_piece";
13071 case DW_OP_deref_size:
13072 return "DW_OP_deref_size";
13073 case DW_OP_xderef_size:
13074 return "DW_OP_xderef_size";
13075 case DW_OP_nop:
13076 return "DW_OP_nop";
13077 /* DWARF 3 extensions. */
13078 case DW_OP_push_object_address:
13079 return "DW_OP_push_object_address";
13080 case DW_OP_call2:
13081 return "DW_OP_call2";
13082 case DW_OP_call4:
13083 return "DW_OP_call4";
13084 case DW_OP_call_ref:
13085 return "DW_OP_call_ref";
13086 case DW_OP_form_tls_address:
13087 return "DW_OP_form_tls_address";
13088 case DW_OP_call_frame_cfa:
13089 return "DW_OP_call_frame_cfa";
13090 case DW_OP_bit_piece:
13091 return "DW_OP_bit_piece";
13092 /* DWARF 4 extensions. */
13093 case DW_OP_implicit_value:
13094 return "DW_OP_implicit_value";
13095 case DW_OP_stack_value:
13096 return "DW_OP_stack_value";
13097 /* GNU extensions. */
13098 case DW_OP_GNU_push_tls_address:
13099 return "DW_OP_GNU_push_tls_address";
13100 case DW_OP_GNU_uninit:
13101 return "DW_OP_GNU_uninit";
13102 case DW_OP_GNU_implicit_pointer:
13103 return "DW_OP_GNU_implicit_pointer";
13104 case DW_OP_GNU_entry_value:
13105 return "DW_OP_GNU_entry_value";
13106 case DW_OP_GNU_const_type:
13107 return "DW_OP_GNU_const_type";
13108 case DW_OP_GNU_regval_type:
13109 return "DW_OP_GNU_regval_type";
13110 case DW_OP_GNU_deref_type:
13111 return "DW_OP_GNU_deref_type";
13112 case DW_OP_GNU_convert:
13113 return "DW_OP_GNU_convert";
13114 case DW_OP_GNU_reinterpret:
13115 return "DW_OP_GNU_reinterpret";
13116 default:
13117 return NULL;
13118 }
13119 }
13120
13121 static char *
13122 dwarf_bool_name (unsigned mybool)
13123 {
13124 if (mybool)
13125 return "TRUE";
13126 else
13127 return "FALSE";
13128 }
13129
13130 /* Convert a DWARF type code into its string name. */
13131
13132 static char *
13133 dwarf_type_encoding_name (unsigned enc)
13134 {
13135 switch (enc)
13136 {
13137 case DW_ATE_void:
13138 return "DW_ATE_void";
13139 case DW_ATE_address:
13140 return "DW_ATE_address";
13141 case DW_ATE_boolean:
13142 return "DW_ATE_boolean";
13143 case DW_ATE_complex_float:
13144 return "DW_ATE_complex_float";
13145 case DW_ATE_float:
13146 return "DW_ATE_float";
13147 case DW_ATE_signed:
13148 return "DW_ATE_signed";
13149 case DW_ATE_signed_char:
13150 return "DW_ATE_signed_char";
13151 case DW_ATE_unsigned:
13152 return "DW_ATE_unsigned";
13153 case DW_ATE_unsigned_char:
13154 return "DW_ATE_unsigned_char";
13155 /* DWARF 3. */
13156 case DW_ATE_imaginary_float:
13157 return "DW_ATE_imaginary_float";
13158 case DW_ATE_packed_decimal:
13159 return "DW_ATE_packed_decimal";
13160 case DW_ATE_numeric_string:
13161 return "DW_ATE_numeric_string";
13162 case DW_ATE_edited:
13163 return "DW_ATE_edited";
13164 case DW_ATE_signed_fixed:
13165 return "DW_ATE_signed_fixed";
13166 case DW_ATE_unsigned_fixed:
13167 return "DW_ATE_unsigned_fixed";
13168 case DW_ATE_decimal_float:
13169 return "DW_ATE_decimal_float";
13170 /* DWARF 4. */
13171 case DW_ATE_UTF:
13172 return "DW_ATE_UTF";
13173 /* HP extensions. */
13174 case DW_ATE_HP_float80:
13175 return "DW_ATE_HP_float80";
13176 case DW_ATE_HP_complex_float80:
13177 return "DW_ATE_HP_complex_float80";
13178 case DW_ATE_HP_float128:
13179 return "DW_ATE_HP_float128";
13180 case DW_ATE_HP_complex_float128:
13181 return "DW_ATE_HP_complex_float128";
13182 case DW_ATE_HP_floathpintel:
13183 return "DW_ATE_HP_floathpintel";
13184 case DW_ATE_HP_imaginary_float80:
13185 return "DW_ATE_HP_imaginary_float80";
13186 case DW_ATE_HP_imaginary_float128:
13187 return "DW_ATE_HP_imaginary_float128";
13188 default:
13189 return "DW_ATE_<unknown>";
13190 }
13191 }
13192
13193 /* Convert a DWARF call frame info operation to its string name. */
13194
13195 #if 0
13196 static char *
13197 dwarf_cfi_name (unsigned cfi_opc)
13198 {
13199 switch (cfi_opc)
13200 {
13201 case DW_CFA_advance_loc:
13202 return "DW_CFA_advance_loc";
13203 case DW_CFA_offset:
13204 return "DW_CFA_offset";
13205 case DW_CFA_restore:
13206 return "DW_CFA_restore";
13207 case DW_CFA_nop:
13208 return "DW_CFA_nop";
13209 case DW_CFA_set_loc:
13210 return "DW_CFA_set_loc";
13211 case DW_CFA_advance_loc1:
13212 return "DW_CFA_advance_loc1";
13213 case DW_CFA_advance_loc2:
13214 return "DW_CFA_advance_loc2";
13215 case DW_CFA_advance_loc4:
13216 return "DW_CFA_advance_loc4";
13217 case DW_CFA_offset_extended:
13218 return "DW_CFA_offset_extended";
13219 case DW_CFA_restore_extended:
13220 return "DW_CFA_restore_extended";
13221 case DW_CFA_undefined:
13222 return "DW_CFA_undefined";
13223 case DW_CFA_same_value:
13224 return "DW_CFA_same_value";
13225 case DW_CFA_register:
13226 return "DW_CFA_register";
13227 case DW_CFA_remember_state:
13228 return "DW_CFA_remember_state";
13229 case DW_CFA_restore_state:
13230 return "DW_CFA_restore_state";
13231 case DW_CFA_def_cfa:
13232 return "DW_CFA_def_cfa";
13233 case DW_CFA_def_cfa_register:
13234 return "DW_CFA_def_cfa_register";
13235 case DW_CFA_def_cfa_offset:
13236 return "DW_CFA_def_cfa_offset";
13237 /* DWARF 3. */
13238 case DW_CFA_def_cfa_expression:
13239 return "DW_CFA_def_cfa_expression";
13240 case DW_CFA_expression:
13241 return "DW_CFA_expression";
13242 case DW_CFA_offset_extended_sf:
13243 return "DW_CFA_offset_extended_sf";
13244 case DW_CFA_def_cfa_sf:
13245 return "DW_CFA_def_cfa_sf";
13246 case DW_CFA_def_cfa_offset_sf:
13247 return "DW_CFA_def_cfa_offset_sf";
13248 case DW_CFA_val_offset:
13249 return "DW_CFA_val_offset";
13250 case DW_CFA_val_offset_sf:
13251 return "DW_CFA_val_offset_sf";
13252 case DW_CFA_val_expression:
13253 return "DW_CFA_val_expression";
13254 /* SGI/MIPS specific. */
13255 case DW_CFA_MIPS_advance_loc8:
13256 return "DW_CFA_MIPS_advance_loc8";
13257 /* GNU extensions. */
13258 case DW_CFA_GNU_window_save:
13259 return "DW_CFA_GNU_window_save";
13260 case DW_CFA_GNU_args_size:
13261 return "DW_CFA_GNU_args_size";
13262 case DW_CFA_GNU_negative_offset_extended:
13263 return "DW_CFA_GNU_negative_offset_extended";
13264 default:
13265 return "DW_CFA_<unknown>";
13266 }
13267 }
13268 #endif
13269
13270 static void
13271 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13272 {
13273 unsigned int i;
13274
13275 print_spaces (indent, f);
13276 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13277 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13278
13279 if (die->parent != NULL)
13280 {
13281 print_spaces (indent, f);
13282 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13283 die->parent->offset);
13284 }
13285
13286 print_spaces (indent, f);
13287 fprintf_unfiltered (f, " has children: %s\n",
13288 dwarf_bool_name (die->child != NULL));
13289
13290 print_spaces (indent, f);
13291 fprintf_unfiltered (f, " attributes:\n");
13292
13293 for (i = 0; i < die->num_attrs; ++i)
13294 {
13295 print_spaces (indent, f);
13296 fprintf_unfiltered (f, " %s (%s) ",
13297 dwarf_attr_name (die->attrs[i].name),
13298 dwarf_form_name (die->attrs[i].form));
13299
13300 switch (die->attrs[i].form)
13301 {
13302 case DW_FORM_ref_addr:
13303 case DW_FORM_addr:
13304 fprintf_unfiltered (f, "address: ");
13305 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13306 break;
13307 case DW_FORM_block2:
13308 case DW_FORM_block4:
13309 case DW_FORM_block:
13310 case DW_FORM_block1:
13311 fprintf_unfiltered (f, "block: size %d",
13312 DW_BLOCK (&die->attrs[i])->size);
13313 break;
13314 case DW_FORM_exprloc:
13315 fprintf_unfiltered (f, "expression: size %u",
13316 DW_BLOCK (&die->attrs[i])->size);
13317 break;
13318 case DW_FORM_ref1:
13319 case DW_FORM_ref2:
13320 case DW_FORM_ref4:
13321 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13322 (long) (DW_ADDR (&die->attrs[i])));
13323 break;
13324 case DW_FORM_data1:
13325 case DW_FORM_data2:
13326 case DW_FORM_data4:
13327 case DW_FORM_data8:
13328 case DW_FORM_udata:
13329 case DW_FORM_sdata:
13330 fprintf_unfiltered (f, "constant: %s",
13331 pulongest (DW_UNSND (&die->attrs[i])));
13332 break;
13333 case DW_FORM_sec_offset:
13334 fprintf_unfiltered (f, "section offset: %s",
13335 pulongest (DW_UNSND (&die->attrs[i])));
13336 break;
13337 case DW_FORM_ref_sig8:
13338 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13339 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13340 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13341 else
13342 fprintf_unfiltered (f, "signatured type, offset: unknown");
13343 break;
13344 case DW_FORM_string:
13345 case DW_FORM_strp:
13346 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13347 DW_STRING (&die->attrs[i])
13348 ? DW_STRING (&die->attrs[i]) : "",
13349 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13350 break;
13351 case DW_FORM_flag:
13352 if (DW_UNSND (&die->attrs[i]))
13353 fprintf_unfiltered (f, "flag: TRUE");
13354 else
13355 fprintf_unfiltered (f, "flag: FALSE");
13356 break;
13357 case DW_FORM_flag_present:
13358 fprintf_unfiltered (f, "flag: TRUE");
13359 break;
13360 case DW_FORM_indirect:
13361 /* The reader will have reduced the indirect form to
13362 the "base form" so this form should not occur. */
13363 fprintf_unfiltered (f,
13364 "unexpected attribute form: DW_FORM_indirect");
13365 break;
13366 default:
13367 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13368 die->attrs[i].form);
13369 break;
13370 }
13371 fprintf_unfiltered (f, "\n");
13372 }
13373 }
13374
13375 static void
13376 dump_die_for_error (struct die_info *die)
13377 {
13378 dump_die_shallow (gdb_stderr, 0, die);
13379 }
13380
13381 static void
13382 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13383 {
13384 int indent = level * 4;
13385
13386 gdb_assert (die != NULL);
13387
13388 if (level >= max_level)
13389 return;
13390
13391 dump_die_shallow (f, indent, die);
13392
13393 if (die->child != NULL)
13394 {
13395 print_spaces (indent, f);
13396 fprintf_unfiltered (f, " Children:");
13397 if (level + 1 < max_level)
13398 {
13399 fprintf_unfiltered (f, "\n");
13400 dump_die_1 (f, level + 1, max_level, die->child);
13401 }
13402 else
13403 {
13404 fprintf_unfiltered (f,
13405 " [not printed, max nesting level reached]\n");
13406 }
13407 }
13408
13409 if (die->sibling != NULL && level > 0)
13410 {
13411 dump_die_1 (f, level, max_level, die->sibling);
13412 }
13413 }
13414
13415 /* This is called from the pdie macro in gdbinit.in.
13416 It's not static so gcc will keep a copy callable from gdb. */
13417
13418 void
13419 dump_die (struct die_info *die, int max_level)
13420 {
13421 dump_die_1 (gdb_stdlog, 0, max_level, die);
13422 }
13423
13424 static void
13425 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13426 {
13427 void **slot;
13428
13429 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13430
13431 *slot = die;
13432 }
13433
13434 static int
13435 is_ref_attr (struct attribute *attr)
13436 {
13437 switch (attr->form)
13438 {
13439 case DW_FORM_ref_addr:
13440 case DW_FORM_ref1:
13441 case DW_FORM_ref2:
13442 case DW_FORM_ref4:
13443 case DW_FORM_ref8:
13444 case DW_FORM_ref_udata:
13445 return 1;
13446 default:
13447 return 0;
13448 }
13449 }
13450
13451 static unsigned int
13452 dwarf2_get_ref_die_offset (struct attribute *attr)
13453 {
13454 if (is_ref_attr (attr))
13455 return DW_ADDR (attr);
13456
13457 complaint (&symfile_complaints,
13458 _("unsupported die ref attribute form: '%s'"),
13459 dwarf_form_name (attr->form));
13460 return 0;
13461 }
13462
13463 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13464 * the value held by the attribute is not constant. */
13465
13466 static LONGEST
13467 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13468 {
13469 if (attr->form == DW_FORM_sdata)
13470 return DW_SND (attr);
13471 else if (attr->form == DW_FORM_udata
13472 || attr->form == DW_FORM_data1
13473 || attr->form == DW_FORM_data2
13474 || attr->form == DW_FORM_data4
13475 || attr->form == DW_FORM_data8)
13476 return DW_UNSND (attr);
13477 else
13478 {
13479 complaint (&symfile_complaints,
13480 _("Attribute value is not a constant (%s)"),
13481 dwarf_form_name (attr->form));
13482 return default_value;
13483 }
13484 }
13485
13486 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
13487 unit and add it to our queue.
13488 The result is non-zero if PER_CU was queued, otherwise the result is zero
13489 meaning either PER_CU is already queued or it is already loaded. */
13490
13491 static int
13492 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13493 struct dwarf2_per_cu_data *per_cu)
13494 {
13495 /* We may arrive here during partial symbol reading, if we need full
13496 DIEs to process an unusual case (e.g. template arguments). Do
13497 not queue PER_CU, just tell our caller to load its DIEs. */
13498 if (dwarf2_per_objfile->reading_partial_symbols)
13499 {
13500 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13501 return 1;
13502 return 0;
13503 }
13504
13505 /* Mark the dependence relation so that we don't flush PER_CU
13506 too early. */
13507 dwarf2_add_dependence (this_cu, per_cu);
13508
13509 /* If it's already on the queue, we have nothing to do. */
13510 if (per_cu->queued)
13511 return 0;
13512
13513 /* If the compilation unit is already loaded, just mark it as
13514 used. */
13515 if (per_cu->cu != NULL)
13516 {
13517 per_cu->cu->last_used = 0;
13518 return 0;
13519 }
13520
13521 /* Add it to the queue. */
13522 queue_comp_unit (per_cu, this_cu->objfile);
13523
13524 return 1;
13525 }
13526
13527 /* Follow reference or signature attribute ATTR of SRC_DIE.
13528 On entry *REF_CU is the CU of SRC_DIE.
13529 On exit *REF_CU is the CU of the result. */
13530
13531 static struct die_info *
13532 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13533 struct dwarf2_cu **ref_cu)
13534 {
13535 struct die_info *die;
13536
13537 if (is_ref_attr (attr))
13538 die = follow_die_ref (src_die, attr, ref_cu);
13539 else if (attr->form == DW_FORM_ref_sig8)
13540 die = follow_die_sig (src_die, attr, ref_cu);
13541 else
13542 {
13543 dump_die_for_error (src_die);
13544 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13545 (*ref_cu)->objfile->name);
13546 }
13547
13548 return die;
13549 }
13550
13551 /* Follow reference OFFSET.
13552 On entry *REF_CU is the CU of the source die referencing OFFSET.
13553 On exit *REF_CU is the CU of the result.
13554 Returns NULL if OFFSET is invalid. */
13555
13556 static struct die_info *
13557 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13558 {
13559 struct die_info temp_die;
13560 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13561
13562 gdb_assert (cu->per_cu != NULL);
13563
13564 target_cu = cu;
13565
13566 if (cu->per_cu->from_debug_types)
13567 {
13568 /* .debug_types CUs cannot reference anything outside their CU.
13569 If they need to, they have to reference a signatured type via
13570 DW_FORM_ref_sig8. */
13571 if (! offset_in_cu_p (&cu->header, offset))
13572 return NULL;
13573 }
13574 else if (! offset_in_cu_p (&cu->header, offset))
13575 {
13576 struct dwarf2_per_cu_data *per_cu;
13577
13578 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13579
13580 /* If necessary, add it to the queue and load its DIEs. */
13581 if (maybe_queue_comp_unit (cu, per_cu))
13582 load_full_comp_unit (per_cu, cu->objfile);
13583
13584 target_cu = per_cu->cu;
13585 }
13586 else if (cu->dies == NULL)
13587 {
13588 /* We're loading full DIEs during partial symbol reading. */
13589 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13590 load_full_comp_unit (cu->per_cu, cu->objfile);
13591 }
13592
13593 *ref_cu = target_cu;
13594 temp_die.offset = offset;
13595 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13596 }
13597
13598 /* Follow reference attribute ATTR of SRC_DIE.
13599 On entry *REF_CU is the CU of SRC_DIE.
13600 On exit *REF_CU is the CU of the result. */
13601
13602 static struct die_info *
13603 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13604 struct dwarf2_cu **ref_cu)
13605 {
13606 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13607 struct dwarf2_cu *cu = *ref_cu;
13608 struct die_info *die;
13609
13610 die = follow_die_offset (offset, ref_cu);
13611 if (!die)
13612 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13613 "at 0x%x [in module %s]"),
13614 offset, src_die->offset, cu->objfile->name);
13615
13616 return die;
13617 }
13618
13619 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13620 value is intended for DW_OP_call*. */
13621
13622 struct dwarf2_locexpr_baton
13623 dwarf2_fetch_die_location_block (unsigned int offset,
13624 struct dwarf2_per_cu_data *per_cu,
13625 CORE_ADDR (*get_frame_pc) (void *baton),
13626 void *baton)
13627 {
13628 struct dwarf2_cu *cu = per_cu->cu;
13629 struct die_info *die;
13630 struct attribute *attr;
13631 struct dwarf2_locexpr_baton retval;
13632
13633 dw2_setup (per_cu->objfile);
13634
13635 die = follow_die_offset (offset, &cu);
13636 if (!die)
13637 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13638 offset, per_cu->cu->objfile->name);
13639
13640 attr = dwarf2_attr (die, DW_AT_location, cu);
13641 if (!attr)
13642 {
13643 /* DWARF: "If there is no such attribute, then there is no effect.". */
13644
13645 retval.data = NULL;
13646 retval.size = 0;
13647 }
13648 else if (attr_form_is_section_offset (attr))
13649 {
13650 struct dwarf2_loclist_baton loclist_baton;
13651 CORE_ADDR pc = (*get_frame_pc) (baton);
13652 size_t size;
13653
13654 fill_in_loclist_baton (cu, &loclist_baton, attr);
13655
13656 retval.data = dwarf2_find_location_expression (&loclist_baton,
13657 &size, pc);
13658 retval.size = size;
13659 }
13660 else
13661 {
13662 if (!attr_form_is_block (attr))
13663 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13664 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13665 offset, per_cu->cu->objfile->name);
13666
13667 retval.data = DW_BLOCK (attr)->data;
13668 retval.size = DW_BLOCK (attr)->size;
13669 }
13670 retval.per_cu = cu->per_cu;
13671 return retval;
13672 }
13673
13674 /* Return the type of the DIE at DIE_OFFSET in the CU named by
13675 PER_CU. */
13676
13677 struct type *
13678 dwarf2_get_die_type (unsigned int die_offset,
13679 struct dwarf2_per_cu_data *per_cu)
13680 {
13681 dw2_setup (per_cu->objfile);
13682 return get_die_type_at_offset (die_offset, per_cu);
13683 }
13684
13685 /* Follow the signature attribute ATTR in SRC_DIE.
13686 On entry *REF_CU is the CU of SRC_DIE.
13687 On exit *REF_CU is the CU of the result. */
13688
13689 static struct die_info *
13690 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13691 struct dwarf2_cu **ref_cu)
13692 {
13693 struct objfile *objfile = (*ref_cu)->objfile;
13694 struct die_info temp_die;
13695 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13696 struct dwarf2_cu *sig_cu;
13697 struct die_info *die;
13698
13699 /* sig_type will be NULL if the signatured type is missing from
13700 the debug info. */
13701 if (sig_type == NULL)
13702 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13703 "at 0x%x [in module %s]"),
13704 src_die->offset, objfile->name);
13705
13706 /* If necessary, add it to the queue and load its DIEs. */
13707
13708 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13709 read_signatured_type (objfile, sig_type);
13710
13711 gdb_assert (sig_type->per_cu.cu != NULL);
13712
13713 sig_cu = sig_type->per_cu.cu;
13714 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13715 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13716 if (die)
13717 {
13718 *ref_cu = sig_cu;
13719 return die;
13720 }
13721
13722 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13723 "from DIE at 0x%x [in module %s]"),
13724 sig_type->type_offset, src_die->offset, objfile->name);
13725 }
13726
13727 /* Given an offset of a signatured type, return its signatured_type. */
13728
13729 static struct signatured_type *
13730 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13731 {
13732 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13733 unsigned int length, initial_length_size;
13734 unsigned int sig_offset;
13735 struct signatured_type find_entry, *type_sig;
13736
13737 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13738 sig_offset = (initial_length_size
13739 + 2 /*version*/
13740 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13741 + 1 /*address_size*/);
13742 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13743 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13744
13745 /* This is only used to lookup previously recorded types.
13746 If we didn't find it, it's our bug. */
13747 gdb_assert (type_sig != NULL);
13748 gdb_assert (offset == type_sig->per_cu.offset);
13749
13750 return type_sig;
13751 }
13752
13753 /* Read in signatured type at OFFSET and build its CU and die(s). */
13754
13755 static void
13756 read_signatured_type_at_offset (struct objfile *objfile,
13757 unsigned int offset)
13758 {
13759 struct signatured_type *type_sig;
13760
13761 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13762
13763 /* We have the section offset, but we need the signature to do the
13764 hash table lookup. */
13765 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13766
13767 gdb_assert (type_sig->per_cu.cu == NULL);
13768
13769 read_signatured_type (objfile, type_sig);
13770
13771 gdb_assert (type_sig->per_cu.cu != NULL);
13772 }
13773
13774 /* Read in a signatured type and build its CU and DIEs. */
13775
13776 static void
13777 read_signatured_type (struct objfile *objfile,
13778 struct signatured_type *type_sig)
13779 {
13780 gdb_byte *types_ptr;
13781 struct die_reader_specs reader_specs;
13782 struct dwarf2_cu *cu;
13783 ULONGEST signature;
13784 struct cleanup *back_to, *free_cu_cleanup;
13785
13786 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13787 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->per_cu.offset;
13788
13789 gdb_assert (type_sig->per_cu.cu == NULL);
13790
13791 cu = xmalloc (sizeof (*cu));
13792 init_one_comp_unit (cu, objfile);
13793
13794 type_sig->per_cu.cu = cu;
13795 cu->per_cu = &type_sig->per_cu;
13796
13797 /* If an error occurs while loading, release our storage. */
13798 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13799
13800 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13801 types_ptr, objfile->obfd);
13802 gdb_assert (signature == type_sig->signature);
13803
13804 cu->die_hash
13805 = htab_create_alloc_ex (cu->header.length / 12,
13806 die_hash,
13807 die_eq,
13808 NULL,
13809 &cu->comp_unit_obstack,
13810 hashtab_obstack_allocate,
13811 dummy_obstack_deallocate);
13812
13813 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13814 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13815
13816 init_cu_die_reader (&reader_specs, cu);
13817
13818 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13819 NULL /*parent*/);
13820
13821 /* We try not to read any attributes in this function, because not
13822 all objfiles needed for references have been loaded yet, and symbol
13823 table processing isn't initialized. But we have to set the CU language,
13824 or we won't be able to build types correctly. */
13825 prepare_one_comp_unit (cu, cu->dies);
13826
13827 do_cleanups (back_to);
13828
13829 /* We've successfully allocated this compilation unit. Let our caller
13830 clean it up when finished with it. */
13831 discard_cleanups (free_cu_cleanup);
13832
13833 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13834 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13835 }
13836
13837 /* Decode simple location descriptions.
13838 Given a pointer to a dwarf block that defines a location, compute
13839 the location and return the value.
13840
13841 NOTE drow/2003-11-18: This function is called in two situations
13842 now: for the address of static or global variables (partial symbols
13843 only) and for offsets into structures which are expected to be
13844 (more or less) constant. The partial symbol case should go away,
13845 and only the constant case should remain. That will let this
13846 function complain more accurately. A few special modes are allowed
13847 without complaint for global variables (for instance, global
13848 register values and thread-local values).
13849
13850 A location description containing no operations indicates that the
13851 object is optimized out. The return value is 0 for that case.
13852 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13853 callers will only want a very basic result and this can become a
13854 complaint.
13855
13856 Note that stack[0] is unused except as a default error return. */
13857
13858 static CORE_ADDR
13859 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13860 {
13861 struct objfile *objfile = cu->objfile;
13862 int i;
13863 int size = blk->size;
13864 gdb_byte *data = blk->data;
13865 CORE_ADDR stack[64];
13866 int stacki;
13867 unsigned int bytes_read, unsnd;
13868 gdb_byte op;
13869
13870 i = 0;
13871 stacki = 0;
13872 stack[stacki] = 0;
13873 stack[++stacki] = 0;
13874
13875 while (i < size)
13876 {
13877 op = data[i++];
13878 switch (op)
13879 {
13880 case DW_OP_lit0:
13881 case DW_OP_lit1:
13882 case DW_OP_lit2:
13883 case DW_OP_lit3:
13884 case DW_OP_lit4:
13885 case DW_OP_lit5:
13886 case DW_OP_lit6:
13887 case DW_OP_lit7:
13888 case DW_OP_lit8:
13889 case DW_OP_lit9:
13890 case DW_OP_lit10:
13891 case DW_OP_lit11:
13892 case DW_OP_lit12:
13893 case DW_OP_lit13:
13894 case DW_OP_lit14:
13895 case DW_OP_lit15:
13896 case DW_OP_lit16:
13897 case DW_OP_lit17:
13898 case DW_OP_lit18:
13899 case DW_OP_lit19:
13900 case DW_OP_lit20:
13901 case DW_OP_lit21:
13902 case DW_OP_lit22:
13903 case DW_OP_lit23:
13904 case DW_OP_lit24:
13905 case DW_OP_lit25:
13906 case DW_OP_lit26:
13907 case DW_OP_lit27:
13908 case DW_OP_lit28:
13909 case DW_OP_lit29:
13910 case DW_OP_lit30:
13911 case DW_OP_lit31:
13912 stack[++stacki] = op - DW_OP_lit0;
13913 break;
13914
13915 case DW_OP_reg0:
13916 case DW_OP_reg1:
13917 case DW_OP_reg2:
13918 case DW_OP_reg3:
13919 case DW_OP_reg4:
13920 case DW_OP_reg5:
13921 case DW_OP_reg6:
13922 case DW_OP_reg7:
13923 case DW_OP_reg8:
13924 case DW_OP_reg9:
13925 case DW_OP_reg10:
13926 case DW_OP_reg11:
13927 case DW_OP_reg12:
13928 case DW_OP_reg13:
13929 case DW_OP_reg14:
13930 case DW_OP_reg15:
13931 case DW_OP_reg16:
13932 case DW_OP_reg17:
13933 case DW_OP_reg18:
13934 case DW_OP_reg19:
13935 case DW_OP_reg20:
13936 case DW_OP_reg21:
13937 case DW_OP_reg22:
13938 case DW_OP_reg23:
13939 case DW_OP_reg24:
13940 case DW_OP_reg25:
13941 case DW_OP_reg26:
13942 case DW_OP_reg27:
13943 case DW_OP_reg28:
13944 case DW_OP_reg29:
13945 case DW_OP_reg30:
13946 case DW_OP_reg31:
13947 stack[++stacki] = op - DW_OP_reg0;
13948 if (i < size)
13949 dwarf2_complex_location_expr_complaint ();
13950 break;
13951
13952 case DW_OP_regx:
13953 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13954 i += bytes_read;
13955 stack[++stacki] = unsnd;
13956 if (i < size)
13957 dwarf2_complex_location_expr_complaint ();
13958 break;
13959
13960 case DW_OP_addr:
13961 stack[++stacki] = read_address (objfile->obfd, &data[i],
13962 cu, &bytes_read);
13963 i += bytes_read;
13964 break;
13965
13966 case DW_OP_const1u:
13967 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13968 i += 1;
13969 break;
13970
13971 case DW_OP_const1s:
13972 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13973 i += 1;
13974 break;
13975
13976 case DW_OP_const2u:
13977 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13978 i += 2;
13979 break;
13980
13981 case DW_OP_const2s:
13982 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13983 i += 2;
13984 break;
13985
13986 case DW_OP_const4u:
13987 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13988 i += 4;
13989 break;
13990
13991 case DW_OP_const4s:
13992 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13993 i += 4;
13994 break;
13995
13996 case DW_OP_constu:
13997 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13998 &bytes_read);
13999 i += bytes_read;
14000 break;
14001
14002 case DW_OP_consts:
14003 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14004 i += bytes_read;
14005 break;
14006
14007 case DW_OP_dup:
14008 stack[stacki + 1] = stack[stacki];
14009 stacki++;
14010 break;
14011
14012 case DW_OP_plus:
14013 stack[stacki - 1] += stack[stacki];
14014 stacki--;
14015 break;
14016
14017 case DW_OP_plus_uconst:
14018 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14019 &bytes_read);
14020 i += bytes_read;
14021 break;
14022
14023 case DW_OP_minus:
14024 stack[stacki - 1] -= stack[stacki];
14025 stacki--;
14026 break;
14027
14028 case DW_OP_deref:
14029 /* If we're not the last op, then we definitely can't encode
14030 this using GDB's address_class enum. This is valid for partial
14031 global symbols, although the variable's address will be bogus
14032 in the psymtab. */
14033 if (i < size)
14034 dwarf2_complex_location_expr_complaint ();
14035 break;
14036
14037 case DW_OP_GNU_push_tls_address:
14038 /* The top of the stack has the offset from the beginning
14039 of the thread control block at which the variable is located. */
14040 /* Nothing should follow this operator, so the top of stack would
14041 be returned. */
14042 /* This is valid for partial global symbols, but the variable's
14043 address will be bogus in the psymtab. */
14044 if (i < size)
14045 dwarf2_complex_location_expr_complaint ();
14046 break;
14047
14048 case DW_OP_GNU_uninit:
14049 break;
14050
14051 default:
14052 {
14053 const char *name = dwarf_stack_op_name (op);
14054
14055 if (name)
14056 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14057 name);
14058 else
14059 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14060 op);
14061 }
14062
14063 return (stack[stacki]);
14064 }
14065
14066 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14067 outside of the allocated space. Also enforce minimum>0. */
14068 if (stacki >= ARRAY_SIZE (stack) - 1)
14069 {
14070 complaint (&symfile_complaints,
14071 _("location description stack overflow"));
14072 return 0;
14073 }
14074
14075 if (stacki <= 0)
14076 {
14077 complaint (&symfile_complaints,
14078 _("location description stack underflow"));
14079 return 0;
14080 }
14081 }
14082 return (stack[stacki]);
14083 }
14084
14085 /* memory allocation interface */
14086
14087 static struct dwarf_block *
14088 dwarf_alloc_block (struct dwarf2_cu *cu)
14089 {
14090 struct dwarf_block *blk;
14091
14092 blk = (struct dwarf_block *)
14093 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14094 return (blk);
14095 }
14096
14097 static struct abbrev_info *
14098 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14099 {
14100 struct abbrev_info *abbrev;
14101
14102 abbrev = (struct abbrev_info *)
14103 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14104 memset (abbrev, 0, sizeof (struct abbrev_info));
14105 return (abbrev);
14106 }
14107
14108 static struct die_info *
14109 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14110 {
14111 struct die_info *die;
14112 size_t size = sizeof (struct die_info);
14113
14114 if (num_attrs > 1)
14115 size += (num_attrs - 1) * sizeof (struct attribute);
14116
14117 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14118 memset (die, 0, sizeof (struct die_info));
14119 return (die);
14120 }
14121
14122 \f
14123 /* Macro support. */
14124
14125 /* Return the full name of file number I in *LH's file name table.
14126 Use COMP_DIR as the name of the current directory of the
14127 compilation. The result is allocated using xmalloc; the caller is
14128 responsible for freeing it. */
14129 static char *
14130 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14131 {
14132 /* Is the file number a valid index into the line header's file name
14133 table? Remember that file numbers start with one, not zero. */
14134 if (1 <= file && file <= lh->num_file_names)
14135 {
14136 struct file_entry *fe = &lh->file_names[file - 1];
14137
14138 if (IS_ABSOLUTE_PATH (fe->name))
14139 return xstrdup (fe->name);
14140 else
14141 {
14142 const char *dir;
14143 int dir_len;
14144 char *full_name;
14145
14146 if (fe->dir_index)
14147 dir = lh->include_dirs[fe->dir_index - 1];
14148 else
14149 dir = comp_dir;
14150
14151 if (dir)
14152 {
14153 dir_len = strlen (dir);
14154 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14155 strcpy (full_name, dir);
14156 full_name[dir_len] = '/';
14157 strcpy (full_name + dir_len + 1, fe->name);
14158 return full_name;
14159 }
14160 else
14161 return xstrdup (fe->name);
14162 }
14163 }
14164 else
14165 {
14166 /* The compiler produced a bogus file number. We can at least
14167 record the macro definitions made in the file, even if we
14168 won't be able to find the file by name. */
14169 char fake_name[80];
14170
14171 sprintf (fake_name, "<bad macro file number %d>", file);
14172
14173 complaint (&symfile_complaints,
14174 _("bad file number in macro information (%d)"),
14175 file);
14176
14177 return xstrdup (fake_name);
14178 }
14179 }
14180
14181
14182 static struct macro_source_file *
14183 macro_start_file (int file, int line,
14184 struct macro_source_file *current_file,
14185 const char *comp_dir,
14186 struct line_header *lh, struct objfile *objfile)
14187 {
14188 /* The full name of this source file. */
14189 char *full_name = file_full_name (file, lh, comp_dir);
14190
14191 /* We don't create a macro table for this compilation unit
14192 at all until we actually get a filename. */
14193 if (! pending_macros)
14194 pending_macros = new_macro_table (&objfile->objfile_obstack,
14195 objfile->macro_cache);
14196
14197 if (! current_file)
14198 /* If we have no current file, then this must be the start_file
14199 directive for the compilation unit's main source file. */
14200 current_file = macro_set_main (pending_macros, full_name);
14201 else
14202 current_file = macro_include (current_file, line, full_name);
14203
14204 xfree (full_name);
14205
14206 return current_file;
14207 }
14208
14209
14210 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14211 followed by a null byte. */
14212 static char *
14213 copy_string (const char *buf, int len)
14214 {
14215 char *s = xmalloc (len + 1);
14216
14217 memcpy (s, buf, len);
14218 s[len] = '\0';
14219 return s;
14220 }
14221
14222
14223 static const char *
14224 consume_improper_spaces (const char *p, const char *body)
14225 {
14226 if (*p == ' ')
14227 {
14228 complaint (&symfile_complaints,
14229 _("macro definition contains spaces "
14230 "in formal argument list:\n`%s'"),
14231 body);
14232
14233 while (*p == ' ')
14234 p++;
14235 }
14236
14237 return p;
14238 }
14239
14240
14241 static void
14242 parse_macro_definition (struct macro_source_file *file, int line,
14243 const char *body)
14244 {
14245 const char *p;
14246
14247 /* The body string takes one of two forms. For object-like macro
14248 definitions, it should be:
14249
14250 <macro name> " " <definition>
14251
14252 For function-like macro definitions, it should be:
14253
14254 <macro name> "() " <definition>
14255 or
14256 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14257
14258 Spaces may appear only where explicitly indicated, and in the
14259 <definition>.
14260
14261 The Dwarf 2 spec says that an object-like macro's name is always
14262 followed by a space, but versions of GCC around March 2002 omit
14263 the space when the macro's definition is the empty string.
14264
14265 The Dwarf 2 spec says that there should be no spaces between the
14266 formal arguments in a function-like macro's formal argument list,
14267 but versions of GCC around March 2002 include spaces after the
14268 commas. */
14269
14270
14271 /* Find the extent of the macro name. The macro name is terminated
14272 by either a space or null character (for an object-like macro) or
14273 an opening paren (for a function-like macro). */
14274 for (p = body; *p; p++)
14275 if (*p == ' ' || *p == '(')
14276 break;
14277
14278 if (*p == ' ' || *p == '\0')
14279 {
14280 /* It's an object-like macro. */
14281 int name_len = p - body;
14282 char *name = copy_string (body, name_len);
14283 const char *replacement;
14284
14285 if (*p == ' ')
14286 replacement = body + name_len + 1;
14287 else
14288 {
14289 dwarf2_macro_malformed_definition_complaint (body);
14290 replacement = body + name_len;
14291 }
14292
14293 macro_define_object (file, line, name, replacement);
14294
14295 xfree (name);
14296 }
14297 else if (*p == '(')
14298 {
14299 /* It's a function-like macro. */
14300 char *name = copy_string (body, p - body);
14301 int argc = 0;
14302 int argv_size = 1;
14303 char **argv = xmalloc (argv_size * sizeof (*argv));
14304
14305 p++;
14306
14307 p = consume_improper_spaces (p, body);
14308
14309 /* Parse the formal argument list. */
14310 while (*p && *p != ')')
14311 {
14312 /* Find the extent of the current argument name. */
14313 const char *arg_start = p;
14314
14315 while (*p && *p != ',' && *p != ')' && *p != ' ')
14316 p++;
14317
14318 if (! *p || p == arg_start)
14319 dwarf2_macro_malformed_definition_complaint (body);
14320 else
14321 {
14322 /* Make sure argv has room for the new argument. */
14323 if (argc >= argv_size)
14324 {
14325 argv_size *= 2;
14326 argv = xrealloc (argv, argv_size * sizeof (*argv));
14327 }
14328
14329 argv[argc++] = copy_string (arg_start, p - arg_start);
14330 }
14331
14332 p = consume_improper_spaces (p, body);
14333
14334 /* Consume the comma, if present. */
14335 if (*p == ',')
14336 {
14337 p++;
14338
14339 p = consume_improper_spaces (p, body);
14340 }
14341 }
14342
14343 if (*p == ')')
14344 {
14345 p++;
14346
14347 if (*p == ' ')
14348 /* Perfectly formed definition, no complaints. */
14349 macro_define_function (file, line, name,
14350 argc, (const char **) argv,
14351 p + 1);
14352 else if (*p == '\0')
14353 {
14354 /* Complain, but do define it. */
14355 dwarf2_macro_malformed_definition_complaint (body);
14356 macro_define_function (file, line, name,
14357 argc, (const char **) argv,
14358 p);
14359 }
14360 else
14361 /* Just complain. */
14362 dwarf2_macro_malformed_definition_complaint (body);
14363 }
14364 else
14365 /* Just complain. */
14366 dwarf2_macro_malformed_definition_complaint (body);
14367
14368 xfree (name);
14369 {
14370 int i;
14371
14372 for (i = 0; i < argc; i++)
14373 xfree (argv[i]);
14374 }
14375 xfree (argv);
14376 }
14377 else
14378 dwarf2_macro_malformed_definition_complaint (body);
14379 }
14380
14381
14382 static void
14383 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14384 char *comp_dir, bfd *abfd,
14385 struct dwarf2_cu *cu)
14386 {
14387 gdb_byte *mac_ptr, *mac_end;
14388 struct macro_source_file *current_file = 0;
14389 enum dwarf_macinfo_record_type macinfo_type;
14390 int at_commandline;
14391
14392 dwarf2_read_section (dwarf2_per_objfile->objfile,
14393 &dwarf2_per_objfile->macinfo);
14394 if (dwarf2_per_objfile->macinfo.buffer == NULL)
14395 {
14396 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14397 return;
14398 }
14399
14400 /* First pass: Find the name of the base filename.
14401 This filename is needed in order to process all macros whose definition
14402 (or undefinition) comes from the command line. These macros are defined
14403 before the first DW_MACINFO_start_file entry, and yet still need to be
14404 associated to the base file.
14405
14406 To determine the base file name, we scan the macro definitions until we
14407 reach the first DW_MACINFO_start_file entry. We then initialize
14408 CURRENT_FILE accordingly so that any macro definition found before the
14409 first DW_MACINFO_start_file can still be associated to the base file. */
14410
14411 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14412 mac_end = dwarf2_per_objfile->macinfo.buffer
14413 + dwarf2_per_objfile->macinfo.size;
14414
14415 do
14416 {
14417 /* Do we at least have room for a macinfo type byte? */
14418 if (mac_ptr >= mac_end)
14419 {
14420 /* Complaint is printed during the second pass as GDB will probably
14421 stop the first pass earlier upon finding
14422 DW_MACINFO_start_file. */
14423 break;
14424 }
14425
14426 macinfo_type = read_1_byte (abfd, mac_ptr);
14427 mac_ptr++;
14428
14429 switch (macinfo_type)
14430 {
14431 /* A zero macinfo type indicates the end of the macro
14432 information. */
14433 case 0:
14434 break;
14435
14436 case DW_MACINFO_define:
14437 case DW_MACINFO_undef:
14438 /* Only skip the data by MAC_PTR. */
14439 {
14440 unsigned int bytes_read;
14441
14442 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14443 mac_ptr += bytes_read;
14444 read_direct_string (abfd, mac_ptr, &bytes_read);
14445 mac_ptr += bytes_read;
14446 }
14447 break;
14448
14449 case DW_MACINFO_start_file:
14450 {
14451 unsigned int bytes_read;
14452 int line, file;
14453
14454 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14455 mac_ptr += bytes_read;
14456 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14457 mac_ptr += bytes_read;
14458
14459 current_file = macro_start_file (file, line, current_file,
14460 comp_dir, lh, cu->objfile);
14461 }
14462 break;
14463
14464 case DW_MACINFO_end_file:
14465 /* No data to skip by MAC_PTR. */
14466 break;
14467
14468 case DW_MACINFO_vendor_ext:
14469 /* Only skip the data by MAC_PTR. */
14470 {
14471 unsigned int bytes_read;
14472
14473 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14474 mac_ptr += bytes_read;
14475 read_direct_string (abfd, mac_ptr, &bytes_read);
14476 mac_ptr += bytes_read;
14477 }
14478 break;
14479
14480 default:
14481 break;
14482 }
14483 } while (macinfo_type != 0 && current_file == NULL);
14484
14485 /* Second pass: Process all entries.
14486
14487 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14488 command-line macro definitions/undefinitions. This flag is unset when we
14489 reach the first DW_MACINFO_start_file entry. */
14490
14491 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14492
14493 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14494 GDB is still reading the definitions from command line. First
14495 DW_MACINFO_start_file will need to be ignored as it was already executed
14496 to create CURRENT_FILE for the main source holding also the command line
14497 definitions. On first met DW_MACINFO_start_file this flag is reset to
14498 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14499
14500 at_commandline = 1;
14501
14502 do
14503 {
14504 /* Do we at least have room for a macinfo type byte? */
14505 if (mac_ptr >= mac_end)
14506 {
14507 dwarf2_macros_too_long_complaint ();
14508 break;
14509 }
14510
14511 macinfo_type = read_1_byte (abfd, mac_ptr);
14512 mac_ptr++;
14513
14514 switch (macinfo_type)
14515 {
14516 /* A zero macinfo type indicates the end of the macro
14517 information. */
14518 case 0:
14519 break;
14520
14521 case DW_MACINFO_define:
14522 case DW_MACINFO_undef:
14523 {
14524 unsigned int bytes_read;
14525 int line;
14526 char *body;
14527
14528 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14529 mac_ptr += bytes_read;
14530 body = read_direct_string (abfd, mac_ptr, &bytes_read);
14531 mac_ptr += bytes_read;
14532
14533 if (! current_file)
14534 {
14535 /* DWARF violation as no main source is present. */
14536 complaint (&symfile_complaints,
14537 _("debug info with no main source gives macro %s "
14538 "on line %d: %s"),
14539 macinfo_type == DW_MACINFO_define ?
14540 _("definition") :
14541 macinfo_type == DW_MACINFO_undef ?
14542 _("undefinition") :
14543 _("something-or-other"), line, body);
14544 break;
14545 }
14546 if ((line == 0 && !at_commandline)
14547 || (line != 0 && at_commandline))
14548 complaint (&symfile_complaints,
14549 _("debug info gives %s macro %s with %s line %d: %s"),
14550 at_commandline ? _("command-line") : _("in-file"),
14551 macinfo_type == DW_MACINFO_define ?
14552 _("definition") :
14553 macinfo_type == DW_MACINFO_undef ?
14554 _("undefinition") :
14555 _("something-or-other"),
14556 line == 0 ? _("zero") : _("non-zero"), line, body);
14557
14558 if (macinfo_type == DW_MACINFO_define)
14559 parse_macro_definition (current_file, line, body);
14560 else if (macinfo_type == DW_MACINFO_undef)
14561 macro_undef (current_file, line, body);
14562 }
14563 break;
14564
14565 case DW_MACINFO_start_file:
14566 {
14567 unsigned int bytes_read;
14568 int line, file;
14569
14570 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14571 mac_ptr += bytes_read;
14572 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14573 mac_ptr += bytes_read;
14574
14575 if ((line == 0 && !at_commandline)
14576 || (line != 0 && at_commandline))
14577 complaint (&symfile_complaints,
14578 _("debug info gives source %d included "
14579 "from %s at %s line %d"),
14580 file, at_commandline ? _("command-line") : _("file"),
14581 line == 0 ? _("zero") : _("non-zero"), line);
14582
14583 if (at_commandline)
14584 {
14585 /* This DW_MACINFO_start_file was executed in the pass one. */
14586 at_commandline = 0;
14587 }
14588 else
14589 current_file = macro_start_file (file, line,
14590 current_file, comp_dir,
14591 lh, cu->objfile);
14592 }
14593 break;
14594
14595 case DW_MACINFO_end_file:
14596 if (! current_file)
14597 complaint (&symfile_complaints,
14598 _("macro debug info has an unmatched "
14599 "`close_file' directive"));
14600 else
14601 {
14602 current_file = current_file->included_by;
14603 if (! current_file)
14604 {
14605 enum dwarf_macinfo_record_type next_type;
14606
14607 /* GCC circa March 2002 doesn't produce the zero
14608 type byte marking the end of the compilation
14609 unit. Complain if it's not there, but exit no
14610 matter what. */
14611
14612 /* Do we at least have room for a macinfo type byte? */
14613 if (mac_ptr >= mac_end)
14614 {
14615 dwarf2_macros_too_long_complaint ();
14616 return;
14617 }
14618
14619 /* We don't increment mac_ptr here, so this is just
14620 a look-ahead. */
14621 next_type = read_1_byte (abfd, mac_ptr);
14622 if (next_type != 0)
14623 complaint (&symfile_complaints,
14624 _("no terminating 0-type entry for "
14625 "macros in `.debug_macinfo' section"));
14626
14627 return;
14628 }
14629 }
14630 break;
14631
14632 case DW_MACINFO_vendor_ext:
14633 {
14634 unsigned int bytes_read;
14635 int constant;
14636
14637 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14638 mac_ptr += bytes_read;
14639 read_direct_string (abfd, mac_ptr, &bytes_read);
14640 mac_ptr += bytes_read;
14641
14642 /* We don't recognize any vendor extensions. */
14643 }
14644 break;
14645 }
14646 } while (macinfo_type != 0);
14647 }
14648
14649 /* Check if the attribute's form is a DW_FORM_block*
14650 if so return true else false. */
14651 static int
14652 attr_form_is_block (struct attribute *attr)
14653 {
14654 return (attr == NULL ? 0 :
14655 attr->form == DW_FORM_block1
14656 || attr->form == DW_FORM_block2
14657 || attr->form == DW_FORM_block4
14658 || attr->form == DW_FORM_block
14659 || attr->form == DW_FORM_exprloc);
14660 }
14661
14662 /* Return non-zero if ATTR's value is a section offset --- classes
14663 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14664 You may use DW_UNSND (attr) to retrieve such offsets.
14665
14666 Section 7.5.4, "Attribute Encodings", explains that no attribute
14667 may have a value that belongs to more than one of these classes; it
14668 would be ambiguous if we did, because we use the same forms for all
14669 of them. */
14670 static int
14671 attr_form_is_section_offset (struct attribute *attr)
14672 {
14673 return (attr->form == DW_FORM_data4
14674 || attr->form == DW_FORM_data8
14675 || attr->form == DW_FORM_sec_offset);
14676 }
14677
14678
14679 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14680 zero otherwise. When this function returns true, you can apply
14681 dwarf2_get_attr_constant_value to it.
14682
14683 However, note that for some attributes you must check
14684 attr_form_is_section_offset before using this test. DW_FORM_data4
14685 and DW_FORM_data8 are members of both the constant class, and of
14686 the classes that contain offsets into other debug sections
14687 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14688 that, if an attribute's can be either a constant or one of the
14689 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14690 taken as section offsets, not constants. */
14691 static int
14692 attr_form_is_constant (struct attribute *attr)
14693 {
14694 switch (attr->form)
14695 {
14696 case DW_FORM_sdata:
14697 case DW_FORM_udata:
14698 case DW_FORM_data1:
14699 case DW_FORM_data2:
14700 case DW_FORM_data4:
14701 case DW_FORM_data8:
14702 return 1;
14703 default:
14704 return 0;
14705 }
14706 }
14707
14708 /* A helper function that fills in a dwarf2_loclist_baton. */
14709
14710 static void
14711 fill_in_loclist_baton (struct dwarf2_cu *cu,
14712 struct dwarf2_loclist_baton *baton,
14713 struct attribute *attr)
14714 {
14715 dwarf2_read_section (dwarf2_per_objfile->objfile,
14716 &dwarf2_per_objfile->loc);
14717
14718 baton->per_cu = cu->per_cu;
14719 gdb_assert (baton->per_cu);
14720 /* We don't know how long the location list is, but make sure we
14721 don't run off the edge of the section. */
14722 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14723 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14724 baton->base_address = cu->base_address;
14725 }
14726
14727 static void
14728 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14729 struct dwarf2_cu *cu)
14730 {
14731 if (attr_form_is_section_offset (attr)
14732 /* ".debug_loc" may not exist at all, or the offset may be outside
14733 the section. If so, fall through to the complaint in the
14734 other branch. */
14735 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14736 &dwarf2_per_objfile->loc))
14737 {
14738 struct dwarf2_loclist_baton *baton;
14739
14740 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14741 sizeof (struct dwarf2_loclist_baton));
14742
14743 fill_in_loclist_baton (cu, baton, attr);
14744
14745 if (cu->base_known == 0)
14746 complaint (&symfile_complaints,
14747 _("Location list used without "
14748 "specifying the CU base address."));
14749
14750 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14751 SYMBOL_LOCATION_BATON (sym) = baton;
14752 }
14753 else
14754 {
14755 struct dwarf2_locexpr_baton *baton;
14756
14757 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14758 sizeof (struct dwarf2_locexpr_baton));
14759 baton->per_cu = cu->per_cu;
14760 gdb_assert (baton->per_cu);
14761
14762 if (attr_form_is_block (attr))
14763 {
14764 /* Note that we're just copying the block's data pointer
14765 here, not the actual data. We're still pointing into the
14766 info_buffer for SYM's objfile; right now we never release
14767 that buffer, but when we do clean up properly this may
14768 need to change. */
14769 baton->size = DW_BLOCK (attr)->size;
14770 baton->data = DW_BLOCK (attr)->data;
14771 }
14772 else
14773 {
14774 dwarf2_invalid_attrib_class_complaint ("location description",
14775 SYMBOL_NATURAL_NAME (sym));
14776 baton->size = 0;
14777 baton->data = NULL;
14778 }
14779
14780 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14781 SYMBOL_LOCATION_BATON (sym) = baton;
14782 }
14783 }
14784
14785 /* Return the OBJFILE associated with the compilation unit CU. If CU
14786 came from a separate debuginfo file, then the master objfile is
14787 returned. */
14788
14789 struct objfile *
14790 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14791 {
14792 struct objfile *objfile = per_cu->objfile;
14793
14794 /* Return the master objfile, so that we can report and look up the
14795 correct file containing this variable. */
14796 if (objfile->separate_debug_objfile_backlink)
14797 objfile = objfile->separate_debug_objfile_backlink;
14798
14799 return objfile;
14800 }
14801
14802 /* Return the address size given in the compilation unit header for CU. */
14803
14804 CORE_ADDR
14805 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14806 {
14807 if (per_cu->cu)
14808 return per_cu->cu->header.addr_size;
14809 else
14810 {
14811 /* If the CU is not currently read in, we re-read its header. */
14812 struct objfile *objfile = per_cu->objfile;
14813 struct dwarf2_per_objfile *per_objfile
14814 = objfile_data (objfile, dwarf2_objfile_data_key);
14815 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14816 struct comp_unit_head cu_header;
14817
14818 memset (&cu_header, 0, sizeof cu_header);
14819 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14820 return cu_header.addr_size;
14821 }
14822 }
14823
14824 /* Return the offset size given in the compilation unit header for CU. */
14825
14826 int
14827 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14828 {
14829 if (per_cu->cu)
14830 return per_cu->cu->header.offset_size;
14831 else
14832 {
14833 /* If the CU is not currently read in, we re-read its header. */
14834 struct objfile *objfile = per_cu->objfile;
14835 struct dwarf2_per_objfile *per_objfile
14836 = objfile_data (objfile, dwarf2_objfile_data_key);
14837 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14838 struct comp_unit_head cu_header;
14839
14840 memset (&cu_header, 0, sizeof cu_header);
14841 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14842 return cu_header.offset_size;
14843 }
14844 }
14845
14846 /* Return the text offset of the CU. The returned offset comes from
14847 this CU's objfile. If this objfile came from a separate debuginfo
14848 file, then the offset may be different from the corresponding
14849 offset in the parent objfile. */
14850
14851 CORE_ADDR
14852 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14853 {
14854 struct objfile *objfile = per_cu->objfile;
14855
14856 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14857 }
14858
14859 /* Locate the .debug_info compilation unit from CU's objfile which contains
14860 the DIE at OFFSET. Raises an error on failure. */
14861
14862 static struct dwarf2_per_cu_data *
14863 dwarf2_find_containing_comp_unit (unsigned int offset,
14864 struct objfile *objfile)
14865 {
14866 struct dwarf2_per_cu_data *this_cu;
14867 int low, high;
14868
14869 low = 0;
14870 high = dwarf2_per_objfile->n_comp_units - 1;
14871 while (high > low)
14872 {
14873 int mid = low + (high - low) / 2;
14874
14875 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14876 high = mid;
14877 else
14878 low = mid + 1;
14879 }
14880 gdb_assert (low == high);
14881 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14882 {
14883 if (low == 0)
14884 error (_("Dwarf Error: could not find partial DIE containing "
14885 "offset 0x%lx [in module %s]"),
14886 (long) offset, bfd_get_filename (objfile->obfd));
14887
14888 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14889 return dwarf2_per_objfile->all_comp_units[low-1];
14890 }
14891 else
14892 {
14893 this_cu = dwarf2_per_objfile->all_comp_units[low];
14894 if (low == dwarf2_per_objfile->n_comp_units - 1
14895 && offset >= this_cu->offset + this_cu->length)
14896 error (_("invalid dwarf2 offset %u"), offset);
14897 gdb_assert (offset < this_cu->offset + this_cu->length);
14898 return this_cu;
14899 }
14900 }
14901
14902 /* Locate the compilation unit from OBJFILE which is located at exactly
14903 OFFSET. Raises an error on failure. */
14904
14905 static struct dwarf2_per_cu_data *
14906 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14907 {
14908 struct dwarf2_per_cu_data *this_cu;
14909
14910 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14911 if (this_cu->offset != offset)
14912 error (_("no compilation unit with offset %u."), offset);
14913 return this_cu;
14914 }
14915
14916 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
14917
14918 static void
14919 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14920 {
14921 memset (cu, 0, sizeof (*cu));
14922 cu->objfile = objfile;
14923 obstack_init (&cu->comp_unit_obstack);
14924 }
14925
14926 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14927
14928 static void
14929 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14930 {
14931 struct attribute *attr;
14932
14933 /* Set the language we're debugging. */
14934 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14935 if (attr)
14936 set_cu_language (DW_UNSND (attr), cu);
14937 else
14938 {
14939 cu->language = language_minimal;
14940 cu->language_defn = language_def (cu->language);
14941 }
14942 }
14943
14944 /* Release one cached compilation unit, CU. We unlink it from the tree
14945 of compilation units, but we don't remove it from the read_in_chain;
14946 the caller is responsible for that.
14947 NOTE: DATA is a void * because this function is also used as a
14948 cleanup routine. */
14949
14950 static void
14951 free_one_comp_unit (void *data)
14952 {
14953 struct dwarf2_cu *cu = data;
14954
14955 if (cu->per_cu != NULL)
14956 cu->per_cu->cu = NULL;
14957 cu->per_cu = NULL;
14958
14959 obstack_free (&cu->comp_unit_obstack, NULL);
14960
14961 xfree (cu);
14962 }
14963
14964 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14965 when we're finished with it. We can't free the pointer itself, but be
14966 sure to unlink it from the cache. Also release any associated storage
14967 and perform cache maintenance.
14968
14969 Only used during partial symbol parsing. */
14970
14971 static void
14972 free_stack_comp_unit (void *data)
14973 {
14974 struct dwarf2_cu *cu = data;
14975
14976 obstack_free (&cu->comp_unit_obstack, NULL);
14977 cu->partial_dies = NULL;
14978
14979 if (cu->per_cu != NULL)
14980 {
14981 /* This compilation unit is on the stack in our caller, so we
14982 should not xfree it. Just unlink it. */
14983 cu->per_cu->cu = NULL;
14984 cu->per_cu = NULL;
14985
14986 /* If we had a per-cu pointer, then we may have other compilation
14987 units loaded, so age them now. */
14988 age_cached_comp_units ();
14989 }
14990 }
14991
14992 /* Free all cached compilation units. */
14993
14994 static void
14995 free_cached_comp_units (void *data)
14996 {
14997 struct dwarf2_per_cu_data *per_cu, **last_chain;
14998
14999 per_cu = dwarf2_per_objfile->read_in_chain;
15000 last_chain = &dwarf2_per_objfile->read_in_chain;
15001 while (per_cu != NULL)
15002 {
15003 struct dwarf2_per_cu_data *next_cu;
15004
15005 next_cu = per_cu->cu->read_in_chain;
15006
15007 free_one_comp_unit (per_cu->cu);
15008 *last_chain = next_cu;
15009
15010 per_cu = next_cu;
15011 }
15012 }
15013
15014 /* Increase the age counter on each cached compilation unit, and free
15015 any that are too old. */
15016
15017 static void
15018 age_cached_comp_units (void)
15019 {
15020 struct dwarf2_per_cu_data *per_cu, **last_chain;
15021
15022 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15023 per_cu = dwarf2_per_objfile->read_in_chain;
15024 while (per_cu != NULL)
15025 {
15026 per_cu->cu->last_used ++;
15027 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15028 dwarf2_mark (per_cu->cu);
15029 per_cu = per_cu->cu->read_in_chain;
15030 }
15031
15032 per_cu = dwarf2_per_objfile->read_in_chain;
15033 last_chain = &dwarf2_per_objfile->read_in_chain;
15034 while (per_cu != NULL)
15035 {
15036 struct dwarf2_per_cu_data *next_cu;
15037
15038 next_cu = per_cu->cu->read_in_chain;
15039
15040 if (!per_cu->cu->mark)
15041 {
15042 free_one_comp_unit (per_cu->cu);
15043 *last_chain = next_cu;
15044 }
15045 else
15046 last_chain = &per_cu->cu->read_in_chain;
15047
15048 per_cu = next_cu;
15049 }
15050 }
15051
15052 /* Remove a single compilation unit from the cache. */
15053
15054 static void
15055 free_one_cached_comp_unit (void *target_cu)
15056 {
15057 struct dwarf2_per_cu_data *per_cu, **last_chain;
15058
15059 per_cu = dwarf2_per_objfile->read_in_chain;
15060 last_chain = &dwarf2_per_objfile->read_in_chain;
15061 while (per_cu != NULL)
15062 {
15063 struct dwarf2_per_cu_data *next_cu;
15064
15065 next_cu = per_cu->cu->read_in_chain;
15066
15067 if (per_cu->cu == target_cu)
15068 {
15069 free_one_comp_unit (per_cu->cu);
15070 *last_chain = next_cu;
15071 break;
15072 }
15073 else
15074 last_chain = &per_cu->cu->read_in_chain;
15075
15076 per_cu = next_cu;
15077 }
15078 }
15079
15080 /* Release all extra memory associated with OBJFILE. */
15081
15082 void
15083 dwarf2_free_objfile (struct objfile *objfile)
15084 {
15085 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15086
15087 if (dwarf2_per_objfile == NULL)
15088 return;
15089
15090 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15091 free_cached_comp_units (NULL);
15092
15093 if (dwarf2_per_objfile->quick_file_names_table)
15094 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15095
15096 /* Everything else should be on the objfile obstack. */
15097 }
15098
15099 /* A pair of DIE offset and GDB type pointer. We store these
15100 in a hash table separate from the DIEs, and preserve them
15101 when the DIEs are flushed out of cache. */
15102
15103 struct dwarf2_offset_and_type
15104 {
15105 unsigned int offset;
15106 struct type *type;
15107 };
15108
15109 /* Hash function for a dwarf2_offset_and_type. */
15110
15111 static hashval_t
15112 offset_and_type_hash (const void *item)
15113 {
15114 const struct dwarf2_offset_and_type *ofs = item;
15115
15116 return ofs->offset;
15117 }
15118
15119 /* Equality function for a dwarf2_offset_and_type. */
15120
15121 static int
15122 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15123 {
15124 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15125 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15126
15127 return ofs_lhs->offset == ofs_rhs->offset;
15128 }
15129
15130 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15131 table if necessary. For convenience, return TYPE.
15132
15133 The DIEs reading must have careful ordering to:
15134 * Not cause infite loops trying to read in DIEs as a prerequisite for
15135 reading current DIE.
15136 * Not trying to dereference contents of still incompletely read in types
15137 while reading in other DIEs.
15138 * Enable referencing still incompletely read in types just by a pointer to
15139 the type without accessing its fields.
15140
15141 Therefore caller should follow these rules:
15142 * Try to fetch any prerequisite types we may need to build this DIE type
15143 before building the type and calling set_die_type.
15144 * After building type call set_die_type for current DIE as soon as
15145 possible before fetching more types to complete the current type.
15146 * Make the type as complete as possible before fetching more types. */
15147
15148 static struct type *
15149 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15150 {
15151 struct dwarf2_offset_and_type **slot, ofs;
15152 struct objfile *objfile = cu->objfile;
15153 htab_t *type_hash_ptr;
15154
15155 /* For Ada types, make sure that the gnat-specific data is always
15156 initialized (if not already set). There are a few types where
15157 we should not be doing so, because the type-specific area is
15158 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15159 where the type-specific area is used to store the floatformat).
15160 But this is not a problem, because the gnat-specific information
15161 is actually not needed for these types. */
15162 if (need_gnat_info (cu)
15163 && TYPE_CODE (type) != TYPE_CODE_FUNC
15164 && TYPE_CODE (type) != TYPE_CODE_FLT
15165 && !HAVE_GNAT_AUX_INFO (type))
15166 INIT_GNAT_SPECIFIC (type);
15167
15168 if (cu->per_cu->from_debug_types)
15169 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15170 else
15171 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15172
15173 if (*type_hash_ptr == NULL)
15174 {
15175 *type_hash_ptr
15176 = htab_create_alloc_ex (127,
15177 offset_and_type_hash,
15178 offset_and_type_eq,
15179 NULL,
15180 &objfile->objfile_obstack,
15181 hashtab_obstack_allocate,
15182 dummy_obstack_deallocate);
15183 }
15184
15185 ofs.offset = die->offset;
15186 ofs.type = type;
15187 slot = (struct dwarf2_offset_and_type **)
15188 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15189 if (*slot)
15190 complaint (&symfile_complaints,
15191 _("A problem internal to GDB: DIE 0x%x has type already set"),
15192 die->offset);
15193 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15194 **slot = ofs;
15195 return type;
15196 }
15197
15198 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15199 table, or return NULL if the die does not have a saved type. */
15200
15201 static struct type *
15202 get_die_type_at_offset (unsigned int offset,
15203 struct dwarf2_per_cu_data *per_cu)
15204 {
15205 struct dwarf2_offset_and_type *slot, ofs;
15206 htab_t type_hash;
15207
15208 if (per_cu->from_debug_types)
15209 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15210 else
15211 type_hash = dwarf2_per_objfile->debug_info_type_hash;
15212 if (type_hash == NULL)
15213 return NULL;
15214
15215 ofs.offset = offset;
15216 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15217 if (slot)
15218 return slot->type;
15219 else
15220 return NULL;
15221 }
15222
15223 /* Look up the type for DIE in the appropriate type_hash table,
15224 or return NULL if DIE does not have a saved type. */
15225
15226 static struct type *
15227 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15228 {
15229 return get_die_type_at_offset (die->offset, cu->per_cu);
15230 }
15231
15232 /* Add a dependence relationship from CU to REF_PER_CU. */
15233
15234 static void
15235 dwarf2_add_dependence (struct dwarf2_cu *cu,
15236 struct dwarf2_per_cu_data *ref_per_cu)
15237 {
15238 void **slot;
15239
15240 if (cu->dependencies == NULL)
15241 cu->dependencies
15242 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15243 NULL, &cu->comp_unit_obstack,
15244 hashtab_obstack_allocate,
15245 dummy_obstack_deallocate);
15246
15247 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15248 if (*slot == NULL)
15249 *slot = ref_per_cu;
15250 }
15251
15252 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15253 Set the mark field in every compilation unit in the
15254 cache that we must keep because we are keeping CU. */
15255
15256 static int
15257 dwarf2_mark_helper (void **slot, void *data)
15258 {
15259 struct dwarf2_per_cu_data *per_cu;
15260
15261 per_cu = (struct dwarf2_per_cu_data *) *slot;
15262 if (per_cu->cu->mark)
15263 return 1;
15264 per_cu->cu->mark = 1;
15265
15266 if (per_cu->cu->dependencies != NULL)
15267 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15268
15269 return 1;
15270 }
15271
15272 /* Set the mark field in CU and in every other compilation unit in the
15273 cache that we must keep because we are keeping CU. */
15274
15275 static void
15276 dwarf2_mark (struct dwarf2_cu *cu)
15277 {
15278 if (cu->mark)
15279 return;
15280 cu->mark = 1;
15281 if (cu->dependencies != NULL)
15282 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15283 }
15284
15285 static void
15286 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15287 {
15288 while (per_cu)
15289 {
15290 per_cu->cu->mark = 0;
15291 per_cu = per_cu->cu->read_in_chain;
15292 }
15293 }
15294
15295 /* Trivial hash function for partial_die_info: the hash value of a DIE
15296 is its offset in .debug_info for this objfile. */
15297
15298 static hashval_t
15299 partial_die_hash (const void *item)
15300 {
15301 const struct partial_die_info *part_die = item;
15302
15303 return part_die->offset;
15304 }
15305
15306 /* Trivial comparison function for partial_die_info structures: two DIEs
15307 are equal if they have the same offset. */
15308
15309 static int
15310 partial_die_eq (const void *item_lhs, const void *item_rhs)
15311 {
15312 const struct partial_die_info *part_die_lhs = item_lhs;
15313 const struct partial_die_info *part_die_rhs = item_rhs;
15314
15315 return part_die_lhs->offset == part_die_rhs->offset;
15316 }
15317
15318 static struct cmd_list_element *set_dwarf2_cmdlist;
15319 static struct cmd_list_element *show_dwarf2_cmdlist;
15320
15321 static void
15322 set_dwarf2_cmd (char *args, int from_tty)
15323 {
15324 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15325 }
15326
15327 static void
15328 show_dwarf2_cmd (char *args, int from_tty)
15329 {
15330 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15331 }
15332
15333 /* If section described by INFO was mmapped, munmap it now. */
15334
15335 static void
15336 munmap_section_buffer (struct dwarf2_section_info *info)
15337 {
15338 if (info->was_mmapped)
15339 {
15340 #ifdef HAVE_MMAP
15341 intptr_t begin = (intptr_t) info->buffer;
15342 intptr_t map_begin = begin & ~(pagesize - 1);
15343 size_t map_length = info->size + begin - map_begin;
15344
15345 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15346 #else
15347 /* Without HAVE_MMAP, we should never be here to begin with. */
15348 gdb_assert_not_reached ("no mmap support");
15349 #endif
15350 }
15351 }
15352
15353 /* munmap debug sections for OBJFILE, if necessary. */
15354
15355 static void
15356 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15357 {
15358 struct dwarf2_per_objfile *data = d;
15359
15360 /* This is sorted according to the order they're defined in to make it easier
15361 to keep in sync. */
15362 munmap_section_buffer (&data->info);
15363 munmap_section_buffer (&data->abbrev);
15364 munmap_section_buffer (&data->line);
15365 munmap_section_buffer (&data->loc);
15366 munmap_section_buffer (&data->macinfo);
15367 munmap_section_buffer (&data->str);
15368 munmap_section_buffer (&data->ranges);
15369 munmap_section_buffer (&data->types);
15370 munmap_section_buffer (&data->frame);
15371 munmap_section_buffer (&data->eh_frame);
15372 munmap_section_buffer (&data->gdb_index);
15373 }
15374
15375 \f
15376 /* The "save gdb-index" command. */
15377
15378 /* The contents of the hash table we create when building the string
15379 table. */
15380 struct strtab_entry
15381 {
15382 offset_type offset;
15383 const char *str;
15384 };
15385
15386 /* Hash function for a strtab_entry.
15387
15388 Function is used only during write_hash_table so no index format backward
15389 compatibility is needed. */
15390
15391 static hashval_t
15392 hash_strtab_entry (const void *e)
15393 {
15394 const struct strtab_entry *entry = e;
15395 return mapped_index_string_hash (INT_MAX, entry->str);
15396 }
15397
15398 /* Equality function for a strtab_entry. */
15399
15400 static int
15401 eq_strtab_entry (const void *a, const void *b)
15402 {
15403 const struct strtab_entry *ea = a;
15404 const struct strtab_entry *eb = b;
15405 return !strcmp (ea->str, eb->str);
15406 }
15407
15408 /* Create a strtab_entry hash table. */
15409
15410 static htab_t
15411 create_strtab (void)
15412 {
15413 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15414 xfree, xcalloc, xfree);
15415 }
15416
15417 /* Add a string to the constant pool. Return the string's offset in
15418 host order. */
15419
15420 static offset_type
15421 add_string (htab_t table, struct obstack *cpool, const char *str)
15422 {
15423 void **slot;
15424 struct strtab_entry entry;
15425 struct strtab_entry *result;
15426
15427 entry.str = str;
15428 slot = htab_find_slot (table, &entry, INSERT);
15429 if (*slot)
15430 result = *slot;
15431 else
15432 {
15433 result = XNEW (struct strtab_entry);
15434 result->offset = obstack_object_size (cpool);
15435 result->str = str;
15436 obstack_grow_str0 (cpool, str);
15437 *slot = result;
15438 }
15439 return result->offset;
15440 }
15441
15442 /* An entry in the symbol table. */
15443 struct symtab_index_entry
15444 {
15445 /* The name of the symbol. */
15446 const char *name;
15447 /* The offset of the name in the constant pool. */
15448 offset_type index_offset;
15449 /* A sorted vector of the indices of all the CUs that hold an object
15450 of this name. */
15451 VEC (offset_type) *cu_indices;
15452 };
15453
15454 /* The symbol table. This is a power-of-2-sized hash table. */
15455 struct mapped_symtab
15456 {
15457 offset_type n_elements;
15458 offset_type size;
15459 struct symtab_index_entry **data;
15460 };
15461
15462 /* Hash function for a symtab_index_entry. */
15463
15464 static hashval_t
15465 hash_symtab_entry (const void *e)
15466 {
15467 const struct symtab_index_entry *entry = e;
15468 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15469 sizeof (offset_type) * VEC_length (offset_type,
15470 entry->cu_indices),
15471 0);
15472 }
15473
15474 /* Equality function for a symtab_index_entry. */
15475
15476 static int
15477 eq_symtab_entry (const void *a, const void *b)
15478 {
15479 const struct symtab_index_entry *ea = a;
15480 const struct symtab_index_entry *eb = b;
15481 int len = VEC_length (offset_type, ea->cu_indices);
15482 if (len != VEC_length (offset_type, eb->cu_indices))
15483 return 0;
15484 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15485 VEC_address (offset_type, eb->cu_indices),
15486 sizeof (offset_type) * len);
15487 }
15488
15489 /* Destroy a symtab_index_entry. */
15490
15491 static void
15492 delete_symtab_entry (void *p)
15493 {
15494 struct symtab_index_entry *entry = p;
15495 VEC_free (offset_type, entry->cu_indices);
15496 xfree (entry);
15497 }
15498
15499 /* Create a hash table holding symtab_index_entry objects. */
15500
15501 static htab_t
15502 create_symbol_hash_table (void)
15503 {
15504 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15505 delete_symtab_entry, xcalloc, xfree);
15506 }
15507
15508 /* Create a new mapped symtab object. */
15509
15510 static struct mapped_symtab *
15511 create_mapped_symtab (void)
15512 {
15513 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15514 symtab->n_elements = 0;
15515 symtab->size = 1024;
15516 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15517 return symtab;
15518 }
15519
15520 /* Destroy a mapped_symtab. */
15521
15522 static void
15523 cleanup_mapped_symtab (void *p)
15524 {
15525 struct mapped_symtab *symtab = p;
15526 /* The contents of the array are freed when the other hash table is
15527 destroyed. */
15528 xfree (symtab->data);
15529 xfree (symtab);
15530 }
15531
15532 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15533 the slot.
15534
15535 Function is used only during write_hash_table so no index format backward
15536 compatibility is needed. */
15537
15538 static struct symtab_index_entry **
15539 find_slot (struct mapped_symtab *symtab, const char *name)
15540 {
15541 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15542
15543 index = hash & (symtab->size - 1);
15544 step = ((hash * 17) & (symtab->size - 1)) | 1;
15545
15546 for (;;)
15547 {
15548 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15549 return &symtab->data[index];
15550 index = (index + step) & (symtab->size - 1);
15551 }
15552 }
15553
15554 /* Expand SYMTAB's hash table. */
15555
15556 static void
15557 hash_expand (struct mapped_symtab *symtab)
15558 {
15559 offset_type old_size = symtab->size;
15560 offset_type i;
15561 struct symtab_index_entry **old_entries = symtab->data;
15562
15563 symtab->size *= 2;
15564 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15565
15566 for (i = 0; i < old_size; ++i)
15567 {
15568 if (old_entries[i])
15569 {
15570 struct symtab_index_entry **slot = find_slot (symtab,
15571 old_entries[i]->name);
15572 *slot = old_entries[i];
15573 }
15574 }
15575
15576 xfree (old_entries);
15577 }
15578
15579 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15580 is the index of the CU in which the symbol appears. */
15581
15582 static void
15583 add_index_entry (struct mapped_symtab *symtab, const char *name,
15584 offset_type cu_index)
15585 {
15586 struct symtab_index_entry **slot;
15587
15588 ++symtab->n_elements;
15589 if (4 * symtab->n_elements / 3 >= symtab->size)
15590 hash_expand (symtab);
15591
15592 slot = find_slot (symtab, name);
15593 if (!*slot)
15594 {
15595 *slot = XNEW (struct symtab_index_entry);
15596 (*slot)->name = name;
15597 (*slot)->cu_indices = NULL;
15598 }
15599 /* Don't push an index twice. Due to how we add entries we only
15600 have to check the last one. */
15601 if (VEC_empty (offset_type, (*slot)->cu_indices)
15602 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
15603 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15604 }
15605
15606 /* Add a vector of indices to the constant pool. */
15607
15608 static offset_type
15609 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15610 struct symtab_index_entry *entry)
15611 {
15612 void **slot;
15613
15614 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15615 if (!*slot)
15616 {
15617 offset_type len = VEC_length (offset_type, entry->cu_indices);
15618 offset_type val = MAYBE_SWAP (len);
15619 offset_type iter;
15620 int i;
15621
15622 *slot = entry;
15623 entry->index_offset = obstack_object_size (cpool);
15624
15625 obstack_grow (cpool, &val, sizeof (val));
15626 for (i = 0;
15627 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15628 ++i)
15629 {
15630 val = MAYBE_SWAP (iter);
15631 obstack_grow (cpool, &val, sizeof (val));
15632 }
15633 }
15634 else
15635 {
15636 struct symtab_index_entry *old_entry = *slot;
15637 entry->index_offset = old_entry->index_offset;
15638 entry = old_entry;
15639 }
15640 return entry->index_offset;
15641 }
15642
15643 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15644 constant pool entries going into the obstack CPOOL. */
15645
15646 static void
15647 write_hash_table (struct mapped_symtab *symtab,
15648 struct obstack *output, struct obstack *cpool)
15649 {
15650 offset_type i;
15651 htab_t symbol_hash_table;
15652 htab_t str_table;
15653
15654 symbol_hash_table = create_symbol_hash_table ();
15655 str_table = create_strtab ();
15656
15657 /* We add all the index vectors to the constant pool first, to
15658 ensure alignment is ok. */
15659 for (i = 0; i < symtab->size; ++i)
15660 {
15661 if (symtab->data[i])
15662 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15663 }
15664
15665 /* Now write out the hash table. */
15666 for (i = 0; i < symtab->size; ++i)
15667 {
15668 offset_type str_off, vec_off;
15669
15670 if (symtab->data[i])
15671 {
15672 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15673 vec_off = symtab->data[i]->index_offset;
15674 }
15675 else
15676 {
15677 /* While 0 is a valid constant pool index, it is not valid
15678 to have 0 for both offsets. */
15679 str_off = 0;
15680 vec_off = 0;
15681 }
15682
15683 str_off = MAYBE_SWAP (str_off);
15684 vec_off = MAYBE_SWAP (vec_off);
15685
15686 obstack_grow (output, &str_off, sizeof (str_off));
15687 obstack_grow (output, &vec_off, sizeof (vec_off));
15688 }
15689
15690 htab_delete (str_table);
15691 htab_delete (symbol_hash_table);
15692 }
15693
15694 /* Struct to map psymtab to CU index in the index file. */
15695 struct psymtab_cu_index_map
15696 {
15697 struct partial_symtab *psymtab;
15698 unsigned int cu_index;
15699 };
15700
15701 static hashval_t
15702 hash_psymtab_cu_index (const void *item)
15703 {
15704 const struct psymtab_cu_index_map *map = item;
15705
15706 return htab_hash_pointer (map->psymtab);
15707 }
15708
15709 static int
15710 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15711 {
15712 const struct psymtab_cu_index_map *lhs = item_lhs;
15713 const struct psymtab_cu_index_map *rhs = item_rhs;
15714
15715 return lhs->psymtab == rhs->psymtab;
15716 }
15717
15718 /* Helper struct for building the address table. */
15719 struct addrmap_index_data
15720 {
15721 struct objfile *objfile;
15722 struct obstack *addr_obstack;
15723 htab_t cu_index_htab;
15724
15725 /* Non-zero if the previous_* fields are valid.
15726 We can't write an entry until we see the next entry (since it is only then
15727 that we know the end of the entry). */
15728 int previous_valid;
15729 /* Index of the CU in the table of all CUs in the index file. */
15730 unsigned int previous_cu_index;
15731 /* Start address of the CU. */
15732 CORE_ADDR previous_cu_start;
15733 };
15734
15735 /* Write an address entry to OBSTACK. */
15736
15737 static void
15738 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15739 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15740 {
15741 offset_type cu_index_to_write;
15742 char addr[8];
15743 CORE_ADDR baseaddr;
15744
15745 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15746
15747 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15748 obstack_grow (obstack, addr, 8);
15749 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15750 obstack_grow (obstack, addr, 8);
15751 cu_index_to_write = MAYBE_SWAP (cu_index);
15752 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15753 }
15754
15755 /* Worker function for traversing an addrmap to build the address table. */
15756
15757 static int
15758 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15759 {
15760 struct addrmap_index_data *data = datap;
15761 struct partial_symtab *pst = obj;
15762 offset_type cu_index;
15763 void **slot;
15764
15765 if (data->previous_valid)
15766 add_address_entry (data->objfile, data->addr_obstack,
15767 data->previous_cu_start, start_addr,
15768 data->previous_cu_index);
15769
15770 data->previous_cu_start = start_addr;
15771 if (pst != NULL)
15772 {
15773 struct psymtab_cu_index_map find_map, *map;
15774 find_map.psymtab = pst;
15775 map = htab_find (data->cu_index_htab, &find_map);
15776 gdb_assert (map != NULL);
15777 data->previous_cu_index = map->cu_index;
15778 data->previous_valid = 1;
15779 }
15780 else
15781 data->previous_valid = 0;
15782
15783 return 0;
15784 }
15785
15786 /* Write OBJFILE's address map to OBSTACK.
15787 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15788 in the index file. */
15789
15790 static void
15791 write_address_map (struct objfile *objfile, struct obstack *obstack,
15792 htab_t cu_index_htab)
15793 {
15794 struct addrmap_index_data addrmap_index_data;
15795
15796 /* When writing the address table, we have to cope with the fact that
15797 the addrmap iterator only provides the start of a region; we have to
15798 wait until the next invocation to get the start of the next region. */
15799
15800 addrmap_index_data.objfile = objfile;
15801 addrmap_index_data.addr_obstack = obstack;
15802 addrmap_index_data.cu_index_htab = cu_index_htab;
15803 addrmap_index_data.previous_valid = 0;
15804
15805 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15806 &addrmap_index_data);
15807
15808 /* It's highly unlikely the last entry (end address = 0xff...ff)
15809 is valid, but we should still handle it.
15810 The end address is recorded as the start of the next region, but that
15811 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15812 anyway. */
15813 if (addrmap_index_data.previous_valid)
15814 add_address_entry (objfile, obstack,
15815 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15816 addrmap_index_data.previous_cu_index);
15817 }
15818
15819 /* Add a list of partial symbols to SYMTAB. */
15820
15821 static void
15822 write_psymbols (struct mapped_symtab *symtab,
15823 htab_t psyms_seen,
15824 struct partial_symbol **psymp,
15825 int count,
15826 offset_type cu_index,
15827 int is_static)
15828 {
15829 for (; count-- > 0; ++psymp)
15830 {
15831 void **slot, *lookup;
15832
15833 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15834 error (_("Ada is not currently supported by the index"));
15835
15836 /* We only want to add a given psymbol once. However, we also
15837 want to account for whether it is global or static. So, we
15838 may add it twice, using slightly different values. */
15839 if (is_static)
15840 {
15841 uintptr_t val = 1 | (uintptr_t) *psymp;
15842
15843 lookup = (void *) val;
15844 }
15845 else
15846 lookup = *psymp;
15847
15848 /* Only add a given psymbol once. */
15849 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15850 if (!*slot)
15851 {
15852 *slot = lookup;
15853 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15854 }
15855 }
15856 }
15857
15858 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
15859 exception if there is an error. */
15860
15861 static void
15862 write_obstack (FILE *file, struct obstack *obstack)
15863 {
15864 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15865 file)
15866 != obstack_object_size (obstack))
15867 error (_("couldn't data write to file"));
15868 }
15869
15870 /* Unlink a file if the argument is not NULL. */
15871
15872 static void
15873 unlink_if_set (void *p)
15874 {
15875 char **filename = p;
15876 if (*filename)
15877 unlink (*filename);
15878 }
15879
15880 /* A helper struct used when iterating over debug_types. */
15881 struct signatured_type_index_data
15882 {
15883 struct objfile *objfile;
15884 struct mapped_symtab *symtab;
15885 struct obstack *types_list;
15886 htab_t psyms_seen;
15887 int cu_index;
15888 };
15889
15890 /* A helper function that writes a single signatured_type to an
15891 obstack. */
15892
15893 static int
15894 write_one_signatured_type (void **slot, void *d)
15895 {
15896 struct signatured_type_index_data *info = d;
15897 struct signatured_type *entry = (struct signatured_type *) *slot;
15898 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15899 struct partial_symtab *psymtab = per_cu->v.psymtab;
15900 gdb_byte val[8];
15901
15902 write_psymbols (info->symtab,
15903 info->psyms_seen,
15904 info->objfile->global_psymbols.list
15905 + psymtab->globals_offset,
15906 psymtab->n_global_syms, info->cu_index,
15907 0);
15908 write_psymbols (info->symtab,
15909 info->psyms_seen,
15910 info->objfile->static_psymbols.list
15911 + psymtab->statics_offset,
15912 psymtab->n_static_syms, info->cu_index,
15913 1);
15914
15915 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
15916 obstack_grow (info->types_list, val, 8);
15917 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15918 obstack_grow (info->types_list, val, 8);
15919 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15920 obstack_grow (info->types_list, val, 8);
15921
15922 ++info->cu_index;
15923
15924 return 1;
15925 }
15926
15927 /* A cleanup function for an htab_t. */
15928
15929 static void
15930 cleanup_htab (void *arg)
15931 {
15932 htab_delete (arg);
15933 }
15934
15935 /* Create an index file for OBJFILE in the directory DIR. */
15936
15937 static void
15938 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15939 {
15940 struct cleanup *cleanup;
15941 char *filename, *cleanup_filename;
15942 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15943 struct obstack cu_list, types_cu_list;
15944 int i;
15945 FILE *out_file;
15946 struct mapped_symtab *symtab;
15947 offset_type val, size_of_contents, total_len;
15948 struct stat st;
15949 char buf[8];
15950 htab_t psyms_seen;
15951 htab_t cu_index_htab;
15952 struct psymtab_cu_index_map *psymtab_cu_index_map;
15953
15954 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
15955 return;
15956
15957 if (dwarf2_per_objfile->using_index)
15958 error (_("Cannot use an index to create the index"));
15959
15960 if (stat (objfile->name, &st) < 0)
15961 perror_with_name (objfile->name);
15962
15963 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15964 INDEX_SUFFIX, (char *) NULL);
15965 cleanup = make_cleanup (xfree, filename);
15966
15967 out_file = fopen (filename, "wb");
15968 if (!out_file)
15969 error (_("Can't open `%s' for writing"), filename);
15970
15971 cleanup_filename = filename;
15972 make_cleanup (unlink_if_set, &cleanup_filename);
15973
15974 symtab = create_mapped_symtab ();
15975 make_cleanup (cleanup_mapped_symtab, symtab);
15976
15977 obstack_init (&addr_obstack);
15978 make_cleanup_obstack_free (&addr_obstack);
15979
15980 obstack_init (&cu_list);
15981 make_cleanup_obstack_free (&cu_list);
15982
15983 obstack_init (&types_cu_list);
15984 make_cleanup_obstack_free (&types_cu_list);
15985
15986 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15987 NULL, xcalloc, xfree);
15988 make_cleanup (cleanup_htab, psyms_seen);
15989
15990 /* While we're scanning CU's create a table that maps a psymtab pointer
15991 (which is what addrmap records) to its index (which is what is recorded
15992 in the index file). This will later be needed to write the address
15993 table. */
15994 cu_index_htab = htab_create_alloc (100,
15995 hash_psymtab_cu_index,
15996 eq_psymtab_cu_index,
15997 NULL, xcalloc, xfree);
15998 make_cleanup (cleanup_htab, cu_index_htab);
15999 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16000 xmalloc (sizeof (struct psymtab_cu_index_map)
16001 * dwarf2_per_objfile->n_comp_units);
16002 make_cleanup (xfree, psymtab_cu_index_map);
16003
16004 /* The CU list is already sorted, so we don't need to do additional
16005 work here. Also, the debug_types entries do not appear in
16006 all_comp_units, but only in their own hash table. */
16007 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16008 {
16009 struct dwarf2_per_cu_data *per_cu
16010 = dwarf2_per_objfile->all_comp_units[i];
16011 struct partial_symtab *psymtab = per_cu->v.psymtab;
16012 gdb_byte val[8];
16013 struct psymtab_cu_index_map *map;
16014 void **slot;
16015
16016 write_psymbols (symtab,
16017 psyms_seen,
16018 objfile->global_psymbols.list + psymtab->globals_offset,
16019 psymtab->n_global_syms, i,
16020 0);
16021 write_psymbols (symtab,
16022 psyms_seen,
16023 objfile->static_psymbols.list + psymtab->statics_offset,
16024 psymtab->n_static_syms, i,
16025 1);
16026
16027 map = &psymtab_cu_index_map[i];
16028 map->psymtab = psymtab;
16029 map->cu_index = i;
16030 slot = htab_find_slot (cu_index_htab, map, INSERT);
16031 gdb_assert (slot != NULL);
16032 gdb_assert (*slot == NULL);
16033 *slot = map;
16034
16035 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16036 obstack_grow (&cu_list, val, 8);
16037 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16038 obstack_grow (&cu_list, val, 8);
16039 }
16040
16041 /* Dump the address map. */
16042 write_address_map (objfile, &addr_obstack, cu_index_htab);
16043
16044 /* Write out the .debug_type entries, if any. */
16045 if (dwarf2_per_objfile->signatured_types)
16046 {
16047 struct signatured_type_index_data sig_data;
16048
16049 sig_data.objfile = objfile;
16050 sig_data.symtab = symtab;
16051 sig_data.types_list = &types_cu_list;
16052 sig_data.psyms_seen = psyms_seen;
16053 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16054 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16055 write_one_signatured_type, &sig_data);
16056 }
16057
16058 obstack_init (&constant_pool);
16059 make_cleanup_obstack_free (&constant_pool);
16060 obstack_init (&symtab_obstack);
16061 make_cleanup_obstack_free (&symtab_obstack);
16062 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16063
16064 obstack_init (&contents);
16065 make_cleanup_obstack_free (&contents);
16066 size_of_contents = 6 * sizeof (offset_type);
16067 total_len = size_of_contents;
16068
16069 /* The version number. */
16070 val = MAYBE_SWAP (5);
16071 obstack_grow (&contents, &val, sizeof (val));
16072
16073 /* The offset of the CU list from the start of the file. */
16074 val = MAYBE_SWAP (total_len);
16075 obstack_grow (&contents, &val, sizeof (val));
16076 total_len += obstack_object_size (&cu_list);
16077
16078 /* The offset of the types CU list from the start of the file. */
16079 val = MAYBE_SWAP (total_len);
16080 obstack_grow (&contents, &val, sizeof (val));
16081 total_len += obstack_object_size (&types_cu_list);
16082
16083 /* The offset of the address table from the start of the file. */
16084 val = MAYBE_SWAP (total_len);
16085 obstack_grow (&contents, &val, sizeof (val));
16086 total_len += obstack_object_size (&addr_obstack);
16087
16088 /* The offset of the symbol table from the start of the file. */
16089 val = MAYBE_SWAP (total_len);
16090 obstack_grow (&contents, &val, sizeof (val));
16091 total_len += obstack_object_size (&symtab_obstack);
16092
16093 /* The offset of the constant pool from the start of the file. */
16094 val = MAYBE_SWAP (total_len);
16095 obstack_grow (&contents, &val, sizeof (val));
16096 total_len += obstack_object_size (&constant_pool);
16097
16098 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16099
16100 write_obstack (out_file, &contents);
16101 write_obstack (out_file, &cu_list);
16102 write_obstack (out_file, &types_cu_list);
16103 write_obstack (out_file, &addr_obstack);
16104 write_obstack (out_file, &symtab_obstack);
16105 write_obstack (out_file, &constant_pool);
16106
16107 fclose (out_file);
16108
16109 /* We want to keep the file, so we set cleanup_filename to NULL
16110 here. See unlink_if_set. */
16111 cleanup_filename = NULL;
16112
16113 do_cleanups (cleanup);
16114 }
16115
16116 /* Implementation of the `save gdb-index' command.
16117
16118 Note that the file format used by this command is documented in the
16119 GDB manual. Any changes here must be documented there. */
16120
16121 static void
16122 save_gdb_index_command (char *arg, int from_tty)
16123 {
16124 struct objfile *objfile;
16125
16126 if (!arg || !*arg)
16127 error (_("usage: save gdb-index DIRECTORY"));
16128
16129 ALL_OBJFILES (objfile)
16130 {
16131 struct stat st;
16132
16133 /* If the objfile does not correspond to an actual file, skip it. */
16134 if (stat (objfile->name, &st) < 0)
16135 continue;
16136
16137 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16138 if (dwarf2_per_objfile)
16139 {
16140 volatile struct gdb_exception except;
16141
16142 TRY_CATCH (except, RETURN_MASK_ERROR)
16143 {
16144 write_psymtabs_to_index (objfile, arg);
16145 }
16146 if (except.reason < 0)
16147 exception_fprintf (gdb_stderr, except,
16148 _("Error while writing index for `%s': "),
16149 objfile->name);
16150 }
16151 }
16152 }
16153
16154 \f
16155
16156 int dwarf2_always_disassemble;
16157
16158 static void
16159 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16160 struct cmd_list_element *c, const char *value)
16161 {
16162 fprintf_filtered (file,
16163 _("Whether to always disassemble "
16164 "DWARF expressions is %s.\n"),
16165 value);
16166 }
16167
16168 void _initialize_dwarf2_read (void);
16169
16170 void
16171 _initialize_dwarf2_read (void)
16172 {
16173 struct cmd_list_element *c;
16174
16175 dwarf2_objfile_data_key
16176 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16177
16178 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16179 Set DWARF 2 specific variables.\n\
16180 Configure DWARF 2 variables such as the cache size"),
16181 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16182 0/*allow-unknown*/, &maintenance_set_cmdlist);
16183
16184 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16185 Show DWARF 2 specific variables\n\
16186 Show DWARF 2 variables such as the cache size"),
16187 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16188 0/*allow-unknown*/, &maintenance_show_cmdlist);
16189
16190 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16191 &dwarf2_max_cache_age, _("\
16192 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16193 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16194 A higher limit means that cached compilation units will be stored\n\
16195 in memory longer, and more total memory will be used. Zero disables\n\
16196 caching, which can slow down startup."),
16197 NULL,
16198 show_dwarf2_max_cache_age,
16199 &set_dwarf2_cmdlist,
16200 &show_dwarf2_cmdlist);
16201
16202 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16203 &dwarf2_always_disassemble, _("\
16204 Set whether `info address' always disassembles DWARF expressions."), _("\
16205 Show whether `info address' always disassembles DWARF expressions."), _("\
16206 When enabled, DWARF expressions are always printed in an assembly-like\n\
16207 syntax. When disabled, expressions will be printed in a more\n\
16208 conversational style, when possible."),
16209 NULL,
16210 show_dwarf2_always_disassemble,
16211 &set_dwarf2_cmdlist,
16212 &show_dwarf2_cmdlist);
16213
16214 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16215 Set debugging of the dwarf2 DIE reader."), _("\
16216 Show debugging of the dwarf2 DIE reader."), _("\
16217 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16218 The value is the maximum depth to print."),
16219 NULL,
16220 NULL,
16221 &setdebuglist, &showdebuglist);
16222
16223 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16224 _("\
16225 Save a gdb-index file.\n\
16226 Usage: save gdb-index DIRECTORY"),
16227 &save_cmdlist);
16228 set_cmd_completer (c, filename_completer);
16229 }
This page took 0.379211 seconds and 4 git commands to generate.