* dwarf2read.c (add_index_entry): Use VEC_last, not VEC_length.
[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 /* The total length of the buffer. */
153 off_t total_size;
154 /* A pointer to the address table data. */
155 const gdb_byte *address_table;
156 /* Size of the address table data in bytes. */
157 offset_type address_table_size;
158 /* The symbol table, implemented as a hash table. */
159 const offset_type *symbol_table;
160 /* Size in slots, each slot is 2 offset_types. */
161 offset_type symbol_table_slots;
162 /* A pointer to the constant pool. */
163 const char *constant_pool;
164 };
165
166 struct dwarf2_per_objfile
167 {
168 struct dwarf2_section_info info;
169 struct dwarf2_section_info abbrev;
170 struct dwarf2_section_info line;
171 struct dwarf2_section_info loc;
172 struct dwarf2_section_info macinfo;
173 struct dwarf2_section_info str;
174 struct dwarf2_section_info ranges;
175 struct dwarf2_section_info types;
176 struct dwarf2_section_info frame;
177 struct dwarf2_section_info eh_frame;
178 struct dwarf2_section_info gdb_index;
179
180 /* Back link. */
181 struct objfile *objfile;
182
183 /* A list of all the compilation units. This is used to locate
184 the target compilation unit of a particular reference. */
185 struct dwarf2_per_cu_data **all_comp_units;
186
187 /* The number of compilation units in ALL_COMP_UNITS. */
188 int n_comp_units;
189
190 /* The number of .debug_types-related CUs. */
191 int n_type_comp_units;
192
193 /* The .debug_types-related CUs. */
194 struct dwarf2_per_cu_data **type_comp_units;
195
196 /* A chain of compilation units that are currently read in, so that
197 they can be freed later. */
198 struct dwarf2_per_cu_data *read_in_chain;
199
200 /* A table mapping .debug_types signatures to its signatured_type entry.
201 This is NULL if the .debug_types section hasn't been read in yet. */
202 htab_t signatured_types;
203
204 /* A flag indicating wether this objfile has a section loaded at a
205 VMA of 0. */
206 int has_section_at_zero;
207
208 /* True if we are using the mapped index,
209 or we are faking it for OBJF_READNOW's sake. */
210 unsigned char using_index;
211
212 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
213 struct mapped_index *index_table;
214
215 /* When using index_table, this keeps track of all quick_file_names entries.
216 TUs can share line table entries with CUs or other TUs, and there can be
217 a lot more TUs than unique line tables, so we maintain a separate table
218 of all line table entries to support the sharing. */
219 htab_t quick_file_names_table;
220
221 /* Set during partial symbol reading, to prevent queueing of full
222 symbols. */
223 int reading_partial_symbols;
224
225 /* Table mapping type .debug_info DIE offsets to types.
226 This is NULL if not allocated yet.
227 It (currently) makes sense to allocate debug_types_type_hash lazily.
228 To keep things simple we allocate both lazily. */
229 htab_t debug_info_type_hash;
230
231 /* Table mapping type .debug_types DIE offsets to types.
232 This is NULL if not allocated yet. */
233 htab_t debug_types_type_hash;
234 };
235
236 static struct dwarf2_per_objfile *dwarf2_per_objfile;
237
238 /* names of the debugging sections */
239
240 /* Note that if the debugging section has been compressed, it might
241 have a name like .zdebug_info. */
242
243 #define INFO_SECTION "debug_info"
244 #define ABBREV_SECTION "debug_abbrev"
245 #define LINE_SECTION "debug_line"
246 #define LOC_SECTION "debug_loc"
247 #define MACINFO_SECTION "debug_macinfo"
248 #define STR_SECTION "debug_str"
249 #define RANGES_SECTION "debug_ranges"
250 #define TYPES_SECTION "debug_types"
251 #define FRAME_SECTION "debug_frame"
252 #define EH_FRAME_SECTION "eh_frame"
253 #define GDB_INDEX_SECTION "gdb_index"
254
255 /* local data types */
256
257 /* We hold several abbreviation tables in memory at the same time. */
258 #ifndef ABBREV_HASH_SIZE
259 #define ABBREV_HASH_SIZE 121
260 #endif
261
262 /* The data in a compilation unit header, after target2host
263 translation, looks like this. */
264 struct comp_unit_head
265 {
266 unsigned int length;
267 short version;
268 unsigned char addr_size;
269 unsigned char signed_addr_p;
270 unsigned int abbrev_offset;
271
272 /* Size of file offsets; either 4 or 8. */
273 unsigned int offset_size;
274
275 /* Size of the length field; either 4 or 12. */
276 unsigned int initial_length_size;
277
278 /* Offset to the first byte of this compilation unit header in the
279 .debug_info section, for resolving relative reference dies. */
280 unsigned int offset;
281
282 /* Offset to first die in this cu from the start of the cu.
283 This will be the first byte following the compilation unit header. */
284 unsigned int first_die_offset;
285 };
286
287 /* Type used for delaying computation of method physnames.
288 See comments for compute_delayed_physnames. */
289 struct delayed_method_info
290 {
291 /* The type to which the method is attached, i.e., its parent class. */
292 struct type *type;
293
294 /* The index of the method in the type's function fieldlists. */
295 int fnfield_index;
296
297 /* The index of the method in the fieldlist. */
298 int index;
299
300 /* The name of the DIE. */
301 const char *name;
302
303 /* The DIE associated with this method. */
304 struct die_info *die;
305 };
306
307 typedef struct delayed_method_info delayed_method_info;
308 DEF_VEC_O (delayed_method_info);
309
310 /* Internal state when decoding a particular compilation unit. */
311 struct dwarf2_cu
312 {
313 /* The objfile containing this compilation unit. */
314 struct objfile *objfile;
315
316 /* The header of the compilation unit. */
317 struct comp_unit_head header;
318
319 /* Base address of this compilation unit. */
320 CORE_ADDR base_address;
321
322 /* Non-zero if base_address has been set. */
323 int base_known;
324
325 struct function_range *first_fn, *last_fn, *cached_fn;
326
327 /* The language we are debugging. */
328 enum language language;
329 const struct language_defn *language_defn;
330
331 const char *producer;
332
333 /* The generic symbol table building routines have separate lists for
334 file scope symbols and all all other scopes (local scopes). So
335 we need to select the right one to pass to add_symbol_to_list().
336 We do it by keeping a pointer to the correct list in list_in_scope.
337
338 FIXME: The original dwarf code just treated the file scope as the
339 first local scope, and all other local scopes as nested local
340 scopes, and worked fine. Check to see if we really need to
341 distinguish these in buildsym.c. */
342 struct pending **list_in_scope;
343
344 /* DWARF abbreviation table associated with this compilation unit. */
345 struct abbrev_info **dwarf2_abbrevs;
346
347 /* Storage for the abbrev table. */
348 struct obstack abbrev_obstack;
349
350 /* Hash table holding all the loaded partial DIEs. */
351 htab_t partial_dies;
352
353 /* Storage for things with the same lifetime as this read-in compilation
354 unit, including partial DIEs. */
355 struct obstack comp_unit_obstack;
356
357 /* When multiple dwarf2_cu structures are living in memory, this field
358 chains them all together, so that they can be released efficiently.
359 We will probably also want a generation counter so that most-recently-used
360 compilation units are cached... */
361 struct dwarf2_per_cu_data *read_in_chain;
362
363 /* Backchain to our per_cu entry if the tree has been built. */
364 struct dwarf2_per_cu_data *per_cu;
365
366 /* How many compilation units ago was this CU last referenced? */
367 int last_used;
368
369 /* A hash table of die offsets for following references. */
370 htab_t die_hash;
371
372 /* Full DIEs if read in. */
373 struct die_info *dies;
374
375 /* A set of pointers to dwarf2_per_cu_data objects for compilation
376 units referenced by this one. Only set during full symbol processing;
377 partial symbol tables do not have dependencies. */
378 htab_t dependencies;
379
380 /* Header data from the line table, during full symbol processing. */
381 struct line_header *line_header;
382
383 /* A list of methods which need to have physnames computed
384 after all type information has been read. */
385 VEC (delayed_method_info) *method_list;
386
387 /* Mark used when releasing cached dies. */
388 unsigned int mark : 1;
389
390 /* This flag will be set if this compilation unit might include
391 inter-compilation-unit references. */
392 unsigned int has_form_ref_addr : 1;
393
394 /* This flag will be set if this compilation unit includes any
395 DW_TAG_namespace DIEs. If we know that there are explicit
396 DIEs for namespaces, we don't need to try to infer them
397 from mangled names. */
398 unsigned int has_namespace_info : 1;
399 };
400
401 /* Persistent data held for a compilation unit, even when not
402 processing it. We put a pointer to this structure in the
403 read_symtab_private field of the psymtab. If we encounter
404 inter-compilation-unit references, we also maintain a sorted
405 list of all compilation units. */
406
407 struct dwarf2_per_cu_data
408 {
409 /* The start offset and length of this compilation unit. 2**29-1
410 bytes should suffice to store the length of any compilation unit
411 - if it doesn't, GDB will fall over anyway.
412 NOTE: Unlike comp_unit_head.length, this length includes
413 initial_length_size. */
414 unsigned int offset;
415 unsigned int length : 29;
416
417 /* Flag indicating this compilation unit will be read in before
418 any of the current compilation units are processed. */
419 unsigned int queued : 1;
420
421 /* This flag will be set if we need to load absolutely all DIEs
422 for this compilation unit, instead of just the ones we think
423 are interesting. It gets set if we look for a DIE in the
424 hash table and don't find it. */
425 unsigned int load_all_dies : 1;
426
427 /* Non-zero if this CU is from .debug_types.
428 Otherwise it's from .debug_info. */
429 unsigned int from_debug_types : 1;
430
431 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
432 of the CU cache it gets reset to NULL again. */
433 struct dwarf2_cu *cu;
434
435 /* The corresponding objfile. */
436 struct objfile *objfile;
437
438 /* When using partial symbol tables, the 'psymtab' field is active.
439 Otherwise the 'quick' field is active. */
440 union
441 {
442 /* The partial symbol table associated with this compilation unit,
443 or NULL for partial units (which do not have an associated
444 symtab). */
445 struct partial_symtab *psymtab;
446
447 /* Data needed by the "quick" functions. */
448 struct dwarf2_per_cu_quick_data *quick;
449 } v;
450 };
451
452 /* Entry in the signatured_types hash table. */
453
454 struct signatured_type
455 {
456 ULONGEST signature;
457
458 /* Offset in .debug_types of the TU (type_unit) for this type. */
459 unsigned int offset;
460
461 /* Offset in .debug_types of the type defined by this TU. */
462 unsigned int type_offset;
463
464 /* The CU(/TU) of this type. */
465 struct dwarf2_per_cu_data per_cu;
466 };
467
468 /* Struct used to pass misc. parameters to read_die_and_children, et
469 al. which are used for both .debug_info and .debug_types dies.
470 All parameters here are unchanging for the life of the call. This
471 struct exists to abstract away the constant parameters of die
472 reading. */
473
474 struct die_reader_specs
475 {
476 /* The bfd of this objfile. */
477 bfd* abfd;
478
479 /* The CU of the DIE we are parsing. */
480 struct dwarf2_cu *cu;
481
482 /* Pointer to start of section buffer.
483 This is either the start of .debug_info or .debug_types. */
484 const gdb_byte *buffer;
485 };
486
487 /* The line number information for a compilation unit (found in the
488 .debug_line section) begins with a "statement program header",
489 which contains the following information. */
490 struct line_header
491 {
492 unsigned int total_length;
493 unsigned short version;
494 unsigned int header_length;
495 unsigned char minimum_instruction_length;
496 unsigned char maximum_ops_per_instruction;
497 unsigned char default_is_stmt;
498 int line_base;
499 unsigned char line_range;
500 unsigned char opcode_base;
501
502 /* standard_opcode_lengths[i] is the number of operands for the
503 standard opcode whose value is i. This means that
504 standard_opcode_lengths[0] is unused, and the last meaningful
505 element is standard_opcode_lengths[opcode_base - 1]. */
506 unsigned char *standard_opcode_lengths;
507
508 /* The include_directories table. NOTE! These strings are not
509 allocated with xmalloc; instead, they are pointers into
510 debug_line_buffer. If you try to free them, `free' will get
511 indigestion. */
512 unsigned int num_include_dirs, include_dirs_size;
513 char **include_dirs;
514
515 /* The file_names table. NOTE! These strings are not allocated
516 with xmalloc; instead, they are pointers into debug_line_buffer.
517 Don't try to free them directly. */
518 unsigned int num_file_names, file_names_size;
519 struct file_entry
520 {
521 char *name;
522 unsigned int dir_index;
523 unsigned int mod_time;
524 unsigned int length;
525 int included_p; /* Non-zero if referenced by the Line Number Program. */
526 struct symtab *symtab; /* The associated symbol table, if any. */
527 } *file_names;
528
529 /* The start and end of the statement program following this
530 header. These point into dwarf2_per_objfile->line_buffer. */
531 gdb_byte *statement_program_start, *statement_program_end;
532 };
533
534 /* When we construct a partial symbol table entry we only
535 need this much information. */
536 struct partial_die_info
537 {
538 /* Offset of this DIE. */
539 unsigned int offset;
540
541 /* DWARF-2 tag for this DIE. */
542 ENUM_BITFIELD(dwarf_tag) tag : 16;
543
544 /* Assorted flags describing the data found in this DIE. */
545 unsigned int has_children : 1;
546 unsigned int is_external : 1;
547 unsigned int is_declaration : 1;
548 unsigned int has_type : 1;
549 unsigned int has_specification : 1;
550 unsigned int has_pc_info : 1;
551
552 /* Flag set if the SCOPE field of this structure has been
553 computed. */
554 unsigned int scope_set : 1;
555
556 /* Flag set if the DIE has a byte_size attribute. */
557 unsigned int has_byte_size : 1;
558
559 /* Flag set if any of the DIE's children are template arguments. */
560 unsigned int has_template_arguments : 1;
561
562 /* Flag set if fixup_partial_die has been called on this die. */
563 unsigned int fixup_called : 1;
564
565 /* The name of this DIE. Normally the value of DW_AT_name, but
566 sometimes a default name for unnamed DIEs. */
567 char *name;
568
569 /* The linkage name, if present. */
570 const char *linkage_name;
571
572 /* The scope to prepend to our children. This is generally
573 allocated on the comp_unit_obstack, so will disappear
574 when this compilation unit leaves the cache. */
575 char *scope;
576
577 /* The location description associated with this DIE, if any. */
578 struct dwarf_block *locdesc;
579
580 /* If HAS_PC_INFO, the PC range associated with this DIE. */
581 CORE_ADDR lowpc;
582 CORE_ADDR highpc;
583
584 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
585 DW_AT_sibling, if any. */
586 /* NOTE: This member isn't strictly necessary, read_partial_die could
587 return DW_AT_sibling values to its caller load_partial_dies. */
588 gdb_byte *sibling;
589
590 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
591 DW_AT_specification (or DW_AT_abstract_origin or
592 DW_AT_extension). */
593 unsigned int spec_offset;
594
595 /* Pointers to this DIE's parent, first child, and next sibling,
596 if any. */
597 struct partial_die_info *die_parent, *die_child, *die_sibling;
598 };
599
600 /* This data structure holds the information of an abbrev. */
601 struct abbrev_info
602 {
603 unsigned int number; /* number identifying abbrev */
604 enum dwarf_tag tag; /* dwarf tag */
605 unsigned short has_children; /* boolean */
606 unsigned short num_attrs; /* number of attributes */
607 struct attr_abbrev *attrs; /* an array of attribute descriptions */
608 struct abbrev_info *next; /* next in chain */
609 };
610
611 struct attr_abbrev
612 {
613 ENUM_BITFIELD(dwarf_attribute) name : 16;
614 ENUM_BITFIELD(dwarf_form) form : 16;
615 };
616
617 /* Attributes have a name and a value. */
618 struct attribute
619 {
620 ENUM_BITFIELD(dwarf_attribute) name : 16;
621 ENUM_BITFIELD(dwarf_form) form : 15;
622
623 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
624 field should be in u.str (existing only for DW_STRING) but it is kept
625 here for better struct attribute alignment. */
626 unsigned int string_is_canonical : 1;
627
628 union
629 {
630 char *str;
631 struct dwarf_block *blk;
632 ULONGEST unsnd;
633 LONGEST snd;
634 CORE_ADDR addr;
635 struct signatured_type *signatured_type;
636 }
637 u;
638 };
639
640 /* This data structure holds a complete die structure. */
641 struct die_info
642 {
643 /* DWARF-2 tag for this DIE. */
644 ENUM_BITFIELD(dwarf_tag) tag : 16;
645
646 /* Number of attributes */
647 unsigned char num_attrs;
648
649 /* True if we're presently building the full type name for the
650 type derived from this DIE. */
651 unsigned char building_fullname : 1;
652
653 /* Abbrev number */
654 unsigned int abbrev;
655
656 /* Offset in .debug_info or .debug_types section. */
657 unsigned int offset;
658
659 /* The dies in a compilation unit form an n-ary tree. PARENT
660 points to this die's parent; CHILD points to the first child of
661 this node; and all the children of a given node are chained
662 together via their SIBLING fields. */
663 struct die_info *child; /* Its first child, if any. */
664 struct die_info *sibling; /* Its next sibling, if any. */
665 struct die_info *parent; /* Its parent, if any. */
666
667 /* An array of attributes, with NUM_ATTRS elements. There may be
668 zero, but it's not common and zero-sized arrays are not
669 sufficiently portable C. */
670 struct attribute attrs[1];
671 };
672
673 struct function_range
674 {
675 const char *name;
676 CORE_ADDR lowpc, highpc;
677 int seen_line;
678 struct function_range *next;
679 };
680
681 /* Get at parts of an attribute structure. */
682
683 #define DW_STRING(attr) ((attr)->u.str)
684 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
685 #define DW_UNSND(attr) ((attr)->u.unsnd)
686 #define DW_BLOCK(attr) ((attr)->u.blk)
687 #define DW_SND(attr) ((attr)->u.snd)
688 #define DW_ADDR(attr) ((attr)->u.addr)
689 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
690
691 /* Blocks are a bunch of untyped bytes. */
692 struct dwarf_block
693 {
694 unsigned int size;
695 gdb_byte *data;
696 };
697
698 #ifndef ATTR_ALLOC_CHUNK
699 #define ATTR_ALLOC_CHUNK 4
700 #endif
701
702 /* Allocate fields for structs, unions and enums in this size. */
703 #ifndef DW_FIELD_ALLOC_CHUNK
704 #define DW_FIELD_ALLOC_CHUNK 4
705 #endif
706
707 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
708 but this would require a corresponding change in unpack_field_as_long
709 and friends. */
710 static int bits_per_byte = 8;
711
712 /* The routines that read and process dies for a C struct or C++ class
713 pass lists of data member fields and lists of member function fields
714 in an instance of a field_info structure, as defined below. */
715 struct field_info
716 {
717 /* List of data member and baseclasses fields. */
718 struct nextfield
719 {
720 struct nextfield *next;
721 int accessibility;
722 int virtuality;
723 struct field field;
724 }
725 *fields, *baseclasses;
726
727 /* Number of fields (including baseclasses). */
728 int nfields;
729
730 /* Number of baseclasses. */
731 int nbaseclasses;
732
733 /* Set if the accesibility of one of the fields is not public. */
734 int non_public_fields;
735
736 /* Member function fields array, entries are allocated in the order they
737 are encountered in the object file. */
738 struct nextfnfield
739 {
740 struct nextfnfield *next;
741 struct fn_field fnfield;
742 }
743 *fnfields;
744
745 /* Member function fieldlist array, contains name of possibly overloaded
746 member function, number of overloaded member functions and a pointer
747 to the head of the member function field chain. */
748 struct fnfieldlist
749 {
750 char *name;
751 int length;
752 struct nextfnfield *head;
753 }
754 *fnfieldlists;
755
756 /* Number of entries in the fnfieldlists array. */
757 int nfnfields;
758
759 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
760 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
761 struct typedef_field_list
762 {
763 struct typedef_field field;
764 struct typedef_field_list *next;
765 }
766 *typedef_field_list;
767 unsigned typedef_field_list_count;
768 };
769
770 /* One item on the queue of compilation units to read in full symbols
771 for. */
772 struct dwarf2_queue_item
773 {
774 struct dwarf2_per_cu_data *per_cu;
775 struct dwarf2_queue_item *next;
776 };
777
778 /* The current queue. */
779 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
780
781 /* Loaded secondary compilation units are kept in memory until they
782 have not been referenced for the processing of this many
783 compilation units. Set this to zero to disable caching. Cache
784 sizes of up to at least twenty will improve startup time for
785 typical inter-CU-reference binaries, at an obvious memory cost. */
786 static int dwarf2_max_cache_age = 5;
787 static void
788 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
789 struct cmd_list_element *c, const char *value)
790 {
791 fprintf_filtered (file, _("The upper bound on the age of cached "
792 "dwarf2 compilation units is %s.\n"),
793 value);
794 }
795
796
797 /* Various complaints about symbol reading that don't abort the process. */
798
799 static void
800 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
801 {
802 complaint (&symfile_complaints,
803 _("statement list doesn't fit in .debug_line section"));
804 }
805
806 static void
807 dwarf2_debug_line_missing_file_complaint (void)
808 {
809 complaint (&symfile_complaints,
810 _(".debug_line section has line data without a file"));
811 }
812
813 static void
814 dwarf2_debug_line_missing_end_sequence_complaint (void)
815 {
816 complaint (&symfile_complaints,
817 _(".debug_line section has line "
818 "program sequence without an end"));
819 }
820
821 static void
822 dwarf2_complex_location_expr_complaint (void)
823 {
824 complaint (&symfile_complaints, _("location expression too complex"));
825 }
826
827 static void
828 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
829 int arg3)
830 {
831 complaint (&symfile_complaints,
832 _("const value length mismatch for '%s', got %d, expected %d"),
833 arg1, arg2, arg3);
834 }
835
836 static void
837 dwarf2_macros_too_long_complaint (void)
838 {
839 complaint (&symfile_complaints,
840 _("macro info runs off end of `.debug_macinfo' section"));
841 }
842
843 static void
844 dwarf2_macro_malformed_definition_complaint (const char *arg1)
845 {
846 complaint (&symfile_complaints,
847 _("macro debug info contains a "
848 "malformed macro definition:\n`%s'"),
849 arg1);
850 }
851
852 static void
853 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
854 {
855 complaint (&symfile_complaints,
856 _("invalid attribute class or form for '%s' in '%s'"),
857 arg1, arg2);
858 }
859
860 /* local function prototypes */
861
862 static void dwarf2_locate_sections (bfd *, asection *, void *);
863
864 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
865 struct objfile *);
866
867 static void dwarf2_build_psymtabs_hard (struct objfile *);
868
869 static void scan_partial_symbols (struct partial_die_info *,
870 CORE_ADDR *, CORE_ADDR *,
871 int, struct dwarf2_cu *);
872
873 static void add_partial_symbol (struct partial_die_info *,
874 struct dwarf2_cu *);
875
876 static void add_partial_namespace (struct partial_die_info *pdi,
877 CORE_ADDR *lowpc, CORE_ADDR *highpc,
878 int need_pc, struct dwarf2_cu *cu);
879
880 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
881 CORE_ADDR *highpc, int need_pc,
882 struct dwarf2_cu *cu);
883
884 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
885 struct dwarf2_cu *cu);
886
887 static void add_partial_subprogram (struct partial_die_info *pdi,
888 CORE_ADDR *lowpc, CORE_ADDR *highpc,
889 int need_pc, struct dwarf2_cu *cu);
890
891 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
892 gdb_byte *buffer, gdb_byte *info_ptr,
893 bfd *abfd, struct dwarf2_cu *cu);
894
895 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
896
897 static void psymtab_to_symtab_1 (struct partial_symtab *);
898
899 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
900
901 static void dwarf2_free_abbrev_table (void *);
902
903 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
904 struct dwarf2_cu *);
905
906 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
907 struct dwarf2_cu *);
908
909 static struct partial_die_info *load_partial_dies (bfd *,
910 gdb_byte *, gdb_byte *,
911 int, struct dwarf2_cu *);
912
913 static gdb_byte *read_partial_die (struct partial_die_info *,
914 struct abbrev_info *abbrev,
915 unsigned int, bfd *,
916 gdb_byte *, gdb_byte *,
917 struct dwarf2_cu *);
918
919 static struct partial_die_info *find_partial_die (unsigned int,
920 struct dwarf2_cu *);
921
922 static void fixup_partial_die (struct partial_die_info *,
923 struct dwarf2_cu *);
924
925 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
926 bfd *, gdb_byte *, struct dwarf2_cu *);
927
928 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
929 bfd *, gdb_byte *, struct dwarf2_cu *);
930
931 static unsigned int read_1_byte (bfd *, gdb_byte *);
932
933 static int read_1_signed_byte (bfd *, gdb_byte *);
934
935 static unsigned int read_2_bytes (bfd *, gdb_byte *);
936
937 static unsigned int read_4_bytes (bfd *, gdb_byte *);
938
939 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
940
941 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
942 unsigned int *);
943
944 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
945
946 static LONGEST read_checked_initial_length_and_offset
947 (bfd *, gdb_byte *, const struct comp_unit_head *,
948 unsigned int *, unsigned int *);
949
950 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
951 unsigned int *);
952
953 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
954
955 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
956
957 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
958
959 static char *read_indirect_string (bfd *, gdb_byte *,
960 const struct comp_unit_head *,
961 unsigned int *);
962
963 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
964
965 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
966
967 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
968
969 static void set_cu_language (unsigned int, struct dwarf2_cu *);
970
971 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
972 struct dwarf2_cu *);
973
974 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
975 unsigned int,
976 struct dwarf2_cu *);
977
978 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
979 struct dwarf2_cu *cu);
980
981 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
982
983 static struct die_info *die_specification (struct die_info *die,
984 struct dwarf2_cu **);
985
986 static void free_line_header (struct line_header *lh);
987
988 static void add_file_name (struct line_header *, char *, unsigned int,
989 unsigned int, unsigned int);
990
991 static struct line_header *(dwarf_decode_line_header
992 (unsigned int offset,
993 bfd *abfd, struct dwarf2_cu *cu));
994
995 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
996 struct dwarf2_cu *, struct partial_symtab *);
997
998 static void dwarf2_start_subfile (char *, const char *, const char *);
999
1000 static struct symbol *new_symbol (struct die_info *, struct type *,
1001 struct dwarf2_cu *);
1002
1003 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1004 struct dwarf2_cu *, struct symbol *);
1005
1006 static void dwarf2_const_value (struct attribute *, struct symbol *,
1007 struct dwarf2_cu *);
1008
1009 static void dwarf2_const_value_attr (struct attribute *attr,
1010 struct type *type,
1011 const char *name,
1012 struct obstack *obstack,
1013 struct dwarf2_cu *cu, long *value,
1014 gdb_byte **bytes,
1015 struct dwarf2_locexpr_baton **baton);
1016
1017 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1018
1019 static int need_gnat_info (struct dwarf2_cu *);
1020
1021 static struct type *die_descriptive_type (struct die_info *,
1022 struct dwarf2_cu *);
1023
1024 static void set_descriptive_type (struct type *, struct die_info *,
1025 struct dwarf2_cu *);
1026
1027 static struct type *die_containing_type (struct die_info *,
1028 struct dwarf2_cu *);
1029
1030 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1031 struct dwarf2_cu *);
1032
1033 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1034
1035 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1036
1037 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1038
1039 static char *typename_concat (struct obstack *obs, const char *prefix,
1040 const char *suffix, int physname,
1041 struct dwarf2_cu *cu);
1042
1043 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1044
1045 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1046
1047 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1048
1049 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1050
1051 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1052 struct dwarf2_cu *, struct partial_symtab *);
1053
1054 static int dwarf2_get_pc_bounds (struct die_info *,
1055 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1056 struct partial_symtab *);
1057
1058 static void get_scope_pc_bounds (struct die_info *,
1059 CORE_ADDR *, CORE_ADDR *,
1060 struct dwarf2_cu *);
1061
1062 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1063 CORE_ADDR, struct dwarf2_cu *);
1064
1065 static void dwarf2_add_field (struct field_info *, struct die_info *,
1066 struct dwarf2_cu *);
1067
1068 static void dwarf2_attach_fields_to_type (struct field_info *,
1069 struct type *, struct dwarf2_cu *);
1070
1071 static void dwarf2_add_member_fn (struct field_info *,
1072 struct die_info *, struct type *,
1073 struct dwarf2_cu *);
1074
1075 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1076 struct type *,
1077 struct dwarf2_cu *);
1078
1079 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1080
1081 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1082
1083 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1084
1085 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1086
1087 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1088
1089 static struct type *read_module_type (struct die_info *die,
1090 struct dwarf2_cu *cu);
1091
1092 static const char *namespace_name (struct die_info *die,
1093 int *is_anonymous, struct dwarf2_cu *);
1094
1095 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1096
1097 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1098
1099 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1100 struct dwarf2_cu *);
1101
1102 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1103
1104 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1105 gdb_byte *info_ptr,
1106 gdb_byte **new_info_ptr,
1107 struct die_info *parent);
1108
1109 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1110 gdb_byte *info_ptr,
1111 gdb_byte **new_info_ptr,
1112 struct die_info *parent);
1113
1114 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1115 gdb_byte *info_ptr,
1116 gdb_byte **new_info_ptr,
1117 struct die_info *parent);
1118
1119 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1120 struct die_info **, gdb_byte *,
1121 int *);
1122
1123 static void process_die (struct die_info *, struct dwarf2_cu *);
1124
1125 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1126 struct obstack *);
1127
1128 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1129
1130 static const char *dwarf2_full_name (char *name,
1131 struct die_info *die,
1132 struct dwarf2_cu *cu);
1133
1134 static struct die_info *dwarf2_extension (struct die_info *die,
1135 struct dwarf2_cu **);
1136
1137 static char *dwarf_tag_name (unsigned int);
1138
1139 static char *dwarf_attr_name (unsigned int);
1140
1141 static char *dwarf_form_name (unsigned int);
1142
1143 static char *dwarf_bool_name (unsigned int);
1144
1145 static char *dwarf_type_encoding_name (unsigned int);
1146
1147 #if 0
1148 static char *dwarf_cfi_name (unsigned int);
1149 #endif
1150
1151 static struct die_info *sibling_die (struct die_info *);
1152
1153 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1154
1155 static void dump_die_for_error (struct die_info *);
1156
1157 static void dump_die_1 (struct ui_file *, int level, int max_level,
1158 struct die_info *);
1159
1160 /*static*/ void dump_die (struct die_info *, int max_level);
1161
1162 static void store_in_ref_table (struct die_info *,
1163 struct dwarf2_cu *);
1164
1165 static int is_ref_attr (struct attribute *);
1166
1167 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1168
1169 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1170
1171 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1172 struct attribute *,
1173 struct dwarf2_cu **);
1174
1175 static struct die_info *follow_die_ref (struct die_info *,
1176 struct attribute *,
1177 struct dwarf2_cu **);
1178
1179 static struct die_info *follow_die_sig (struct die_info *,
1180 struct attribute *,
1181 struct dwarf2_cu **);
1182
1183 static void read_signatured_type_at_offset (struct objfile *objfile,
1184 unsigned int offset);
1185
1186 static void read_signatured_type (struct objfile *,
1187 struct signatured_type *type_sig);
1188
1189 /* memory allocation interface */
1190
1191 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1192
1193 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1194
1195 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1196
1197 static void initialize_cu_func_list (struct dwarf2_cu *);
1198
1199 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1200 struct dwarf2_cu *);
1201
1202 static void dwarf_decode_macros (struct line_header *, unsigned int,
1203 char *, bfd *, struct dwarf2_cu *);
1204
1205 static int attr_form_is_block (struct attribute *);
1206
1207 static int attr_form_is_section_offset (struct attribute *);
1208
1209 static int attr_form_is_constant (struct attribute *);
1210
1211 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1212 struct dwarf2_loclist_baton *baton,
1213 struct attribute *attr);
1214
1215 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1216 struct symbol *sym,
1217 struct dwarf2_cu *cu);
1218
1219 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1220 struct abbrev_info *abbrev,
1221 struct dwarf2_cu *cu);
1222
1223 static void free_stack_comp_unit (void *);
1224
1225 static hashval_t partial_die_hash (const void *item);
1226
1227 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1228
1229 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1230 (unsigned int offset, struct objfile *objfile);
1231
1232 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1233 (unsigned int offset, struct objfile *objfile);
1234
1235 static void init_one_comp_unit (struct dwarf2_cu *cu,
1236 struct objfile *objfile);
1237
1238 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1239 struct die_info *comp_unit_die);
1240
1241 static void free_one_comp_unit (void *);
1242
1243 static void free_cached_comp_units (void *);
1244
1245 static void age_cached_comp_units (void);
1246
1247 static void free_one_cached_comp_unit (void *);
1248
1249 static struct type *set_die_type (struct die_info *, struct type *,
1250 struct dwarf2_cu *);
1251
1252 static void create_all_comp_units (struct objfile *);
1253
1254 static int create_debug_types_hash_table (struct objfile *objfile);
1255
1256 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1257 struct objfile *);
1258
1259 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1260
1261 static void dwarf2_add_dependence (struct dwarf2_cu *,
1262 struct dwarf2_per_cu_data *);
1263
1264 static void dwarf2_mark (struct dwarf2_cu *);
1265
1266 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1267
1268 static struct type *get_die_type_at_offset (unsigned int,
1269 struct dwarf2_per_cu_data *per_cu);
1270
1271 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1272
1273 static void dwarf2_release_queue (void *dummy);
1274
1275 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1276 struct objfile *objfile);
1277
1278 static void process_queue (struct objfile *objfile);
1279
1280 static void find_file_and_directory (struct die_info *die,
1281 struct dwarf2_cu *cu,
1282 char **name, char **comp_dir);
1283
1284 static char *file_full_name (int file, struct line_header *lh,
1285 const char *comp_dir);
1286
1287 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1288 gdb_byte *info_ptr,
1289 gdb_byte *buffer,
1290 unsigned int buffer_size,
1291 bfd *abfd);
1292
1293 static void init_cu_die_reader (struct die_reader_specs *reader,
1294 struct dwarf2_cu *cu);
1295
1296 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1297
1298 #if WORDS_BIGENDIAN
1299
1300 /* Convert VALUE between big- and little-endian. */
1301 static offset_type
1302 byte_swap (offset_type value)
1303 {
1304 offset_type result;
1305
1306 result = (value & 0xff) << 24;
1307 result |= (value & 0xff00) << 8;
1308 result |= (value & 0xff0000) >> 8;
1309 result |= (value & 0xff000000) >> 24;
1310 return result;
1311 }
1312
1313 #define MAYBE_SWAP(V) byte_swap (V)
1314
1315 #else
1316 #define MAYBE_SWAP(V) (V)
1317 #endif /* WORDS_BIGENDIAN */
1318
1319 /* The suffix for an index file. */
1320 #define INDEX_SUFFIX ".gdb-index"
1321
1322 static const char *dwarf2_physname (char *name, struct die_info *die,
1323 struct dwarf2_cu *cu);
1324
1325 /* Try to locate the sections we need for DWARF 2 debugging
1326 information and return true if we have enough to do something. */
1327
1328 int
1329 dwarf2_has_info (struct objfile *objfile)
1330 {
1331 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1332 if (!dwarf2_per_objfile)
1333 {
1334 /* Initialize per-objfile state. */
1335 struct dwarf2_per_objfile *data
1336 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1337
1338 memset (data, 0, sizeof (*data));
1339 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1340 dwarf2_per_objfile = data;
1341
1342 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1343 dwarf2_per_objfile->objfile = objfile;
1344 }
1345 return (dwarf2_per_objfile->info.asection != NULL
1346 && dwarf2_per_objfile->abbrev.asection != NULL);
1347 }
1348
1349 /* When loading sections, we can either look for ".<name>", or for
1350 * ".z<name>", which indicates a compressed section. */
1351
1352 static int
1353 section_is_p (const char *section_name, const char *name)
1354 {
1355 return (section_name[0] == '.'
1356 && (strcmp (section_name + 1, name) == 0
1357 || (section_name[1] == 'z'
1358 && strcmp (section_name + 2, name) == 0)));
1359 }
1360
1361 /* This function is mapped across the sections and remembers the
1362 offset and size of each of the debugging sections we are interested
1363 in. */
1364
1365 static void
1366 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1367 {
1368 if (section_is_p (sectp->name, INFO_SECTION))
1369 {
1370 dwarf2_per_objfile->info.asection = sectp;
1371 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1372 }
1373 else if (section_is_p (sectp->name, ABBREV_SECTION))
1374 {
1375 dwarf2_per_objfile->abbrev.asection = sectp;
1376 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1377 }
1378 else if (section_is_p (sectp->name, LINE_SECTION))
1379 {
1380 dwarf2_per_objfile->line.asection = sectp;
1381 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1382 }
1383 else if (section_is_p (sectp->name, LOC_SECTION))
1384 {
1385 dwarf2_per_objfile->loc.asection = sectp;
1386 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1387 }
1388 else if (section_is_p (sectp->name, MACINFO_SECTION))
1389 {
1390 dwarf2_per_objfile->macinfo.asection = sectp;
1391 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1392 }
1393 else if (section_is_p (sectp->name, STR_SECTION))
1394 {
1395 dwarf2_per_objfile->str.asection = sectp;
1396 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1397 }
1398 else if (section_is_p (sectp->name, FRAME_SECTION))
1399 {
1400 dwarf2_per_objfile->frame.asection = sectp;
1401 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1402 }
1403 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1404 {
1405 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1406
1407 if (aflag & SEC_HAS_CONTENTS)
1408 {
1409 dwarf2_per_objfile->eh_frame.asection = sectp;
1410 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1411 }
1412 }
1413 else if (section_is_p (sectp->name, RANGES_SECTION))
1414 {
1415 dwarf2_per_objfile->ranges.asection = sectp;
1416 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1417 }
1418 else if (section_is_p (sectp->name, TYPES_SECTION))
1419 {
1420 dwarf2_per_objfile->types.asection = sectp;
1421 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1422 }
1423 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1424 {
1425 dwarf2_per_objfile->gdb_index.asection = sectp;
1426 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1427 }
1428
1429 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1430 && bfd_section_vma (abfd, sectp) == 0)
1431 dwarf2_per_objfile->has_section_at_zero = 1;
1432 }
1433
1434 /* Decompress a section that was compressed using zlib. Store the
1435 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1436
1437 static void
1438 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1439 gdb_byte **outbuf, bfd_size_type *outsize)
1440 {
1441 bfd *abfd = objfile->obfd;
1442 #ifndef HAVE_ZLIB_H
1443 error (_("Support for zlib-compressed DWARF data (from '%s') "
1444 "is disabled in this copy of GDB"),
1445 bfd_get_filename (abfd));
1446 #else
1447 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1448 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1449 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1450 bfd_size_type uncompressed_size;
1451 gdb_byte *uncompressed_buffer;
1452 z_stream strm;
1453 int rc;
1454 int header_size = 12;
1455
1456 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1457 || bfd_bread (compressed_buffer,
1458 compressed_size, abfd) != compressed_size)
1459 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1460 bfd_get_filename (abfd));
1461
1462 /* Read the zlib header. In this case, it should be "ZLIB" followed
1463 by the uncompressed section size, 8 bytes in big-endian order. */
1464 if (compressed_size < header_size
1465 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1466 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1467 bfd_get_filename (abfd));
1468 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1472 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1473 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1474 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1475 uncompressed_size += compressed_buffer[11];
1476
1477 /* It is possible the section consists of several compressed
1478 buffers concatenated together, so we uncompress in a loop. */
1479 strm.zalloc = NULL;
1480 strm.zfree = NULL;
1481 strm.opaque = NULL;
1482 strm.avail_in = compressed_size - header_size;
1483 strm.next_in = (Bytef*) compressed_buffer + header_size;
1484 strm.avail_out = uncompressed_size;
1485 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1486 uncompressed_size);
1487 rc = inflateInit (&strm);
1488 while (strm.avail_in > 0)
1489 {
1490 if (rc != Z_OK)
1491 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1492 bfd_get_filename (abfd), rc);
1493 strm.next_out = ((Bytef*) uncompressed_buffer
1494 + (uncompressed_size - strm.avail_out));
1495 rc = inflate (&strm, Z_FINISH);
1496 if (rc != Z_STREAM_END)
1497 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1498 bfd_get_filename (abfd), rc);
1499 rc = inflateReset (&strm);
1500 }
1501 rc = inflateEnd (&strm);
1502 if (rc != Z_OK
1503 || strm.avail_out != 0)
1504 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1505 bfd_get_filename (abfd), rc);
1506
1507 do_cleanups (cleanup);
1508 *outbuf = uncompressed_buffer;
1509 *outsize = uncompressed_size;
1510 #endif
1511 }
1512
1513 /* A helper function that decides whether a section is empty. */
1514
1515 static int
1516 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1517 {
1518 return info->asection == NULL || info->size == 0;
1519 }
1520
1521 /* Read the contents of the section SECTP from object file specified by
1522 OBJFILE, store info about the section into INFO.
1523 If the section is compressed, uncompress it before returning. */
1524
1525 static void
1526 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1527 {
1528 bfd *abfd = objfile->obfd;
1529 asection *sectp = info->asection;
1530 gdb_byte *buf, *retbuf;
1531 unsigned char header[4];
1532
1533 if (info->readin)
1534 return;
1535 info->buffer = NULL;
1536 info->was_mmapped = 0;
1537 info->readin = 1;
1538
1539 if (dwarf2_section_empty_p (info))
1540 return;
1541
1542 /* Check if the file has a 4-byte header indicating compression. */
1543 if (info->size > sizeof (header)
1544 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1545 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1546 {
1547 /* Upon decompression, update the buffer and its size. */
1548 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1549 {
1550 zlib_decompress_section (objfile, sectp, &info->buffer,
1551 &info->size);
1552 return;
1553 }
1554 }
1555
1556 #ifdef HAVE_MMAP
1557 if (pagesize == 0)
1558 pagesize = getpagesize ();
1559
1560 /* Only try to mmap sections which are large enough: we don't want to
1561 waste space due to fragmentation. Also, only try mmap for sections
1562 without relocations. */
1563
1564 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1565 {
1566 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1567 size_t map_length = info->size + sectp->filepos - pg_offset;
1568 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1569 MAP_PRIVATE, pg_offset);
1570
1571 if (retbuf != MAP_FAILED)
1572 {
1573 info->was_mmapped = 1;
1574 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1575 #if HAVE_POSIX_MADVISE
1576 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1577 #endif
1578 return;
1579 }
1580 }
1581 #endif
1582
1583 /* If we get here, we are a normal, not-compressed section. */
1584 info->buffer = buf
1585 = obstack_alloc (&objfile->objfile_obstack, info->size);
1586
1587 /* When debugging .o files, we may need to apply relocations; see
1588 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1589 We never compress sections in .o files, so we only need to
1590 try this when the section is not compressed. */
1591 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1592 if (retbuf != NULL)
1593 {
1594 info->buffer = retbuf;
1595 return;
1596 }
1597
1598 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1599 || bfd_bread (buf, info->size, abfd) != info->size)
1600 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1601 bfd_get_filename (abfd));
1602 }
1603
1604 /* A helper function that returns the size of a section in a safe way.
1605 If you are positive that the section has been read before using the
1606 size, then it is safe to refer to the dwarf2_section_info object's
1607 "size" field directly. In other cases, you must call this
1608 function, because for compressed sections the size field is not set
1609 correctly until the section has been read. */
1610
1611 static bfd_size_type
1612 dwarf2_section_size (struct objfile *objfile,
1613 struct dwarf2_section_info *info)
1614 {
1615 if (!info->readin)
1616 dwarf2_read_section (objfile, info);
1617 return info->size;
1618 }
1619
1620 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1621 SECTION_NAME. */
1622
1623 void
1624 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1625 asection **sectp, gdb_byte **bufp,
1626 bfd_size_type *sizep)
1627 {
1628 struct dwarf2_per_objfile *data
1629 = objfile_data (objfile, dwarf2_objfile_data_key);
1630 struct dwarf2_section_info *info;
1631
1632 /* We may see an objfile without any DWARF, in which case we just
1633 return nothing. */
1634 if (data == NULL)
1635 {
1636 *sectp = NULL;
1637 *bufp = NULL;
1638 *sizep = 0;
1639 return;
1640 }
1641 if (section_is_p (section_name, EH_FRAME_SECTION))
1642 info = &data->eh_frame;
1643 else if (section_is_p (section_name, FRAME_SECTION))
1644 info = &data->frame;
1645 else
1646 gdb_assert_not_reached ("unexpected section");
1647
1648 dwarf2_read_section (objfile, info);
1649
1650 *sectp = info->asection;
1651 *bufp = info->buffer;
1652 *sizep = info->size;
1653 }
1654
1655 \f
1656 /* DWARF quick_symbols_functions support. */
1657
1658 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1659 unique line tables, so we maintain a separate table of all .debug_line
1660 derived entries to support the sharing.
1661 All the quick functions need is the list of file names. We discard the
1662 line_header when we're done and don't need to record it here. */
1663 struct quick_file_names
1664 {
1665 /* The offset in .debug_line of the line table. We hash on this. */
1666 unsigned int offset;
1667
1668 /* The number of entries in file_names, real_names. */
1669 unsigned int num_file_names;
1670
1671 /* The file names from the line table, after being run through
1672 file_full_name. */
1673 const char **file_names;
1674
1675 /* The file names from the line table after being run through
1676 gdb_realpath. These are computed lazily. */
1677 const char **real_names;
1678 };
1679
1680 /* When using the index (and thus not using psymtabs), each CU has an
1681 object of this type. This is used to hold information needed by
1682 the various "quick" methods. */
1683 struct dwarf2_per_cu_quick_data
1684 {
1685 /* The file table. This can be NULL if there was no file table
1686 or it's currently not read in.
1687 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1688 struct quick_file_names *file_names;
1689
1690 /* The corresponding symbol table. This is NULL if symbols for this
1691 CU have not yet been read. */
1692 struct symtab *symtab;
1693
1694 /* A temporary mark bit used when iterating over all CUs in
1695 expand_symtabs_matching. */
1696 unsigned int mark : 1;
1697
1698 /* True if we've tried to read the file table and found there isn't one.
1699 There will be no point in trying to read it again next time. */
1700 unsigned int no_file_data : 1;
1701 };
1702
1703 /* Hash function for a quick_file_names. */
1704
1705 static hashval_t
1706 hash_file_name_entry (const void *e)
1707 {
1708 const struct quick_file_names *file_data = e;
1709
1710 return file_data->offset;
1711 }
1712
1713 /* Equality function for a quick_file_names. */
1714
1715 static int
1716 eq_file_name_entry (const void *a, const void *b)
1717 {
1718 const struct quick_file_names *ea = a;
1719 const struct quick_file_names *eb = b;
1720
1721 return ea->offset == eb->offset;
1722 }
1723
1724 /* Delete function for a quick_file_names. */
1725
1726 static void
1727 delete_file_name_entry (void *e)
1728 {
1729 struct quick_file_names *file_data = e;
1730 int i;
1731
1732 for (i = 0; i < file_data->num_file_names; ++i)
1733 {
1734 xfree ((void*) file_data->file_names[i]);
1735 if (file_data->real_names)
1736 xfree ((void*) file_data->real_names[i]);
1737 }
1738
1739 /* The space for the struct itself lives on objfile_obstack,
1740 so we don't free it here. */
1741 }
1742
1743 /* Create a quick_file_names hash table. */
1744
1745 static htab_t
1746 create_quick_file_names_table (unsigned int nr_initial_entries)
1747 {
1748 return htab_create_alloc (nr_initial_entries,
1749 hash_file_name_entry, eq_file_name_entry,
1750 delete_file_name_entry, xcalloc, xfree);
1751 }
1752
1753 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1754 this CU came. */
1755
1756 static void
1757 dw2_do_instantiate_symtab (struct objfile *objfile,
1758 struct dwarf2_per_cu_data *per_cu)
1759 {
1760 struct cleanup *back_to;
1761
1762 back_to = make_cleanup (dwarf2_release_queue, NULL);
1763
1764 queue_comp_unit (per_cu, objfile);
1765
1766 if (per_cu->from_debug_types)
1767 read_signatured_type_at_offset (objfile, per_cu->offset);
1768 else
1769 load_full_comp_unit (per_cu, objfile);
1770
1771 process_queue (objfile);
1772
1773 /* Age the cache, releasing compilation units that have not
1774 been used recently. */
1775 age_cached_comp_units ();
1776
1777 do_cleanups (back_to);
1778 }
1779
1780 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1781 the objfile from which this CU came. Returns the resulting symbol
1782 table. */
1783
1784 static struct symtab *
1785 dw2_instantiate_symtab (struct objfile *objfile,
1786 struct dwarf2_per_cu_data *per_cu)
1787 {
1788 if (!per_cu->v.quick->symtab)
1789 {
1790 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1791 increment_reading_symtab ();
1792 dw2_do_instantiate_symtab (objfile, per_cu);
1793 do_cleanups (back_to);
1794 }
1795 return per_cu->v.quick->symtab;
1796 }
1797
1798 /* Return the CU given its index. */
1799
1800 static struct dwarf2_per_cu_data *
1801 dw2_get_cu (int index)
1802 {
1803 if (index >= dwarf2_per_objfile->n_comp_units)
1804 {
1805 index -= dwarf2_per_objfile->n_comp_units;
1806 return dwarf2_per_objfile->type_comp_units[index];
1807 }
1808 return dwarf2_per_objfile->all_comp_units[index];
1809 }
1810
1811 /* A helper function that knows how to read a 64-bit value in a way
1812 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1813 otherwise. */
1814
1815 static int
1816 extract_cu_value (const char *bytes, ULONGEST *result)
1817 {
1818 if (sizeof (ULONGEST) < 8)
1819 {
1820 int i;
1821
1822 /* Ignore the upper 4 bytes if they are all zero. */
1823 for (i = 0; i < 4; ++i)
1824 if (bytes[i + 4] != 0)
1825 return 0;
1826
1827 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1828 }
1829 else
1830 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1831 return 1;
1832 }
1833
1834 /* Read the CU list from the mapped index, and use it to create all
1835 the CU objects for this objfile. Return 0 if something went wrong,
1836 1 if everything went ok. */
1837
1838 static int
1839 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1840 offset_type cu_list_elements)
1841 {
1842 offset_type i;
1843
1844 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1845 dwarf2_per_objfile->all_comp_units
1846 = obstack_alloc (&objfile->objfile_obstack,
1847 dwarf2_per_objfile->n_comp_units
1848 * sizeof (struct dwarf2_per_cu_data *));
1849
1850 for (i = 0; i < cu_list_elements; i += 2)
1851 {
1852 struct dwarf2_per_cu_data *the_cu;
1853 ULONGEST offset, length;
1854
1855 if (!extract_cu_value (cu_list, &offset)
1856 || !extract_cu_value (cu_list + 8, &length))
1857 return 0;
1858 cu_list += 2 * 8;
1859
1860 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1861 struct dwarf2_per_cu_data);
1862 the_cu->offset = offset;
1863 the_cu->length = length;
1864 the_cu->objfile = objfile;
1865 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1866 struct dwarf2_per_cu_quick_data);
1867 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1868 }
1869
1870 return 1;
1871 }
1872
1873 /* Create the signatured type hash table from the index. */
1874
1875 static int
1876 create_signatured_type_table_from_index (struct objfile *objfile,
1877 const gdb_byte *bytes,
1878 offset_type elements)
1879 {
1880 offset_type i;
1881 htab_t sig_types_hash;
1882
1883 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1884 dwarf2_per_objfile->type_comp_units
1885 = obstack_alloc (&objfile->objfile_obstack,
1886 dwarf2_per_objfile->n_type_comp_units
1887 * sizeof (struct dwarf2_per_cu_data *));
1888
1889 sig_types_hash = allocate_signatured_type_table (objfile);
1890
1891 for (i = 0; i < elements; i += 3)
1892 {
1893 struct signatured_type *type_sig;
1894 ULONGEST offset, type_offset, signature;
1895 void **slot;
1896
1897 if (!extract_cu_value (bytes, &offset)
1898 || !extract_cu_value (bytes + 8, &type_offset))
1899 return 0;
1900 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1901 bytes += 3 * 8;
1902
1903 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1904 struct signatured_type);
1905 type_sig->signature = signature;
1906 type_sig->offset = offset;
1907 type_sig->type_offset = type_offset;
1908 type_sig->per_cu.from_debug_types = 1;
1909 type_sig->per_cu.offset = offset;
1910 type_sig->per_cu.objfile = objfile;
1911 type_sig->per_cu.v.quick
1912 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1913 struct dwarf2_per_cu_quick_data);
1914
1915 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1916 *slot = type_sig;
1917
1918 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1919 }
1920
1921 dwarf2_per_objfile->signatured_types = sig_types_hash;
1922
1923 return 1;
1924 }
1925
1926 /* Read the address map data from the mapped index, and use it to
1927 populate the objfile's psymtabs_addrmap. */
1928
1929 static void
1930 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1931 {
1932 const gdb_byte *iter, *end;
1933 struct obstack temp_obstack;
1934 struct addrmap *mutable_map;
1935 struct cleanup *cleanup;
1936 CORE_ADDR baseaddr;
1937
1938 obstack_init (&temp_obstack);
1939 cleanup = make_cleanup_obstack_free (&temp_obstack);
1940 mutable_map = addrmap_create_mutable (&temp_obstack);
1941
1942 iter = index->address_table;
1943 end = iter + index->address_table_size;
1944
1945 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1946
1947 while (iter < end)
1948 {
1949 ULONGEST hi, lo, cu_index;
1950 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1951 iter += 8;
1952 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1953 iter += 8;
1954 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1955 iter += 4;
1956
1957 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1958 dw2_get_cu (cu_index));
1959 }
1960
1961 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1962 &objfile->objfile_obstack);
1963 do_cleanups (cleanup);
1964 }
1965
1966 /* The hash function for strings in the mapped index. This is the same as
1967 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
1968 implementation. This is necessary because the hash function is tied to the
1969 format of the mapped index file. The hash values do not have to match with
1970 SYMBOL_HASH_NEXT. */
1971
1972 static hashval_t
1973 mapped_index_string_hash (const void *p)
1974 {
1975 const unsigned char *str = (const unsigned char *) p;
1976 hashval_t r = 0;
1977 unsigned char c;
1978
1979 while ((c = *str++) != 0)
1980 r = r * 67 + c - 113;
1981
1982 return r;
1983 }
1984
1985 /* Find a slot in the mapped index INDEX for the object named NAME.
1986 If NAME is found, set *VEC_OUT to point to the CU vector in the
1987 constant pool and return 1. If NAME cannot be found, return 0. */
1988
1989 static int
1990 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1991 offset_type **vec_out)
1992 {
1993 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
1994 offset_type hash;
1995 offset_type slot, step;
1996
1997 if (current_language->la_language == language_cplus
1998 || current_language->la_language == language_java
1999 || current_language->la_language == language_fortran)
2000 {
2001 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2002 not contain any. */
2003 const char *paren = strchr (name, '(');
2004
2005 if (paren)
2006 {
2007 char *dup;
2008
2009 dup = xmalloc (paren - name + 1);
2010 memcpy (dup, name, paren - name);
2011 dup[paren - name] = 0;
2012
2013 make_cleanup (xfree, dup);
2014 name = dup;
2015 }
2016 }
2017
2018 hash = mapped_index_string_hash (name);
2019 slot = hash & (index->symbol_table_slots - 1);
2020 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2021
2022 for (;;)
2023 {
2024 /* Convert a slot number to an offset into the table. */
2025 offset_type i = 2 * slot;
2026 const char *str;
2027 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2028 {
2029 do_cleanups (back_to);
2030 return 0;
2031 }
2032
2033 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2034 if (!strcmp (name, str))
2035 {
2036 *vec_out = (offset_type *) (index->constant_pool
2037 + MAYBE_SWAP (index->symbol_table[i + 1]));
2038 do_cleanups (back_to);
2039 return 1;
2040 }
2041
2042 slot = (slot + step) & (index->symbol_table_slots - 1);
2043 }
2044 }
2045
2046 /* Read the index file. If everything went ok, initialize the "quick"
2047 elements of all the CUs and return 1. Otherwise, return 0. */
2048
2049 static int
2050 dwarf2_read_index (struct objfile *objfile)
2051 {
2052 char *addr;
2053 struct mapped_index *map;
2054 offset_type *metadata;
2055 const gdb_byte *cu_list;
2056 const gdb_byte *types_list = NULL;
2057 offset_type version, cu_list_elements;
2058 offset_type types_list_elements = 0;
2059 int i;
2060
2061 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2062 return 0;
2063
2064 /* Older elfutils strip versions could keep the section in the main
2065 executable while splitting it for the separate debug info file. */
2066 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2067 & SEC_HAS_CONTENTS) == 0)
2068 return 0;
2069
2070 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2071
2072 addr = dwarf2_per_objfile->gdb_index.buffer;
2073 /* Version check. */
2074 version = MAYBE_SWAP (*(offset_type *) addr);
2075 /* Versions earlier than 3 emitted every copy of a psymbol. This
2076 causes the index to behave very poorly for certain requests. Version 3
2077 contained incomplete addrmap. So, it seems better to just ignore such
2078 indices. */
2079 if (version < 4)
2080 return 0;
2081 /* Indexes with higher version than the one supported by GDB may be no
2082 longer backward compatible. */
2083 if (version > 4)
2084 return 0;
2085
2086 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2087 map->total_size = dwarf2_per_objfile->gdb_index.size;
2088
2089 metadata = (offset_type *) (addr + sizeof (offset_type));
2090
2091 i = 0;
2092 cu_list = addr + MAYBE_SWAP (metadata[i]);
2093 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2094 / 8);
2095 ++i;
2096
2097 types_list = addr + MAYBE_SWAP (metadata[i]);
2098 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2099 - MAYBE_SWAP (metadata[i]))
2100 / 8);
2101 ++i;
2102
2103 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2104 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2105 - MAYBE_SWAP (metadata[i]));
2106 ++i;
2107
2108 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2109 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2110 - MAYBE_SWAP (metadata[i]))
2111 / (2 * sizeof (offset_type)));
2112 ++i;
2113
2114 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2115
2116 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2117 return 0;
2118
2119 if (types_list_elements
2120 && !create_signatured_type_table_from_index (objfile, types_list,
2121 types_list_elements))
2122 return 0;
2123
2124 create_addrmap_from_index (objfile, map);
2125
2126 dwarf2_per_objfile->index_table = map;
2127 dwarf2_per_objfile->using_index = 1;
2128 dwarf2_per_objfile->quick_file_names_table =
2129 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2130
2131 return 1;
2132 }
2133
2134 /* A helper for the "quick" functions which sets the global
2135 dwarf2_per_objfile according to OBJFILE. */
2136
2137 static void
2138 dw2_setup (struct objfile *objfile)
2139 {
2140 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2141 gdb_assert (dwarf2_per_objfile);
2142 }
2143
2144 /* A helper for the "quick" functions which attempts to read the line
2145 table for THIS_CU. */
2146
2147 static struct quick_file_names *
2148 dw2_get_file_names (struct objfile *objfile,
2149 struct dwarf2_per_cu_data *this_cu)
2150 {
2151 bfd *abfd = objfile->obfd;
2152 struct line_header *lh;
2153 struct attribute *attr;
2154 struct cleanup *cleanups;
2155 struct die_info *comp_unit_die;
2156 struct dwarf2_section_info* sec;
2157 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2158 int has_children, i;
2159 struct dwarf2_cu cu;
2160 unsigned int bytes_read, buffer_size;
2161 struct die_reader_specs reader_specs;
2162 char *name, *comp_dir;
2163 void **slot;
2164 struct quick_file_names *qfn;
2165 unsigned int line_offset;
2166
2167 if (this_cu->v.quick->file_names != NULL)
2168 return this_cu->v.quick->file_names;
2169 /* If we know there is no line data, no point in looking again. */
2170 if (this_cu->v.quick->no_file_data)
2171 return NULL;
2172
2173 init_one_comp_unit (&cu, objfile);
2174 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2175
2176 if (this_cu->from_debug_types)
2177 sec = &dwarf2_per_objfile->types;
2178 else
2179 sec = &dwarf2_per_objfile->info;
2180 dwarf2_read_section (objfile, sec);
2181 buffer_size = sec->size;
2182 buffer = sec->buffer;
2183 info_ptr = buffer + this_cu->offset;
2184 beg_of_comp_unit = info_ptr;
2185
2186 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2187 buffer, buffer_size,
2188 abfd);
2189
2190 /* Complete the cu_header. */
2191 cu.header.offset = beg_of_comp_unit - buffer;
2192 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2193
2194 this_cu->cu = &cu;
2195 cu.per_cu = this_cu;
2196
2197 dwarf2_read_abbrevs (abfd, &cu);
2198 make_cleanup (dwarf2_free_abbrev_table, &cu);
2199
2200 if (this_cu->from_debug_types)
2201 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2202 init_cu_die_reader (&reader_specs, &cu);
2203 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2204 &has_children);
2205
2206 lh = NULL;
2207 slot = NULL;
2208 line_offset = 0;
2209 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2210 if (attr)
2211 {
2212 struct quick_file_names find_entry;
2213
2214 line_offset = DW_UNSND (attr);
2215
2216 /* We may have already read in this line header (TU line header sharing).
2217 If we have we're done. */
2218 find_entry.offset = line_offset;
2219 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2220 &find_entry, INSERT);
2221 if (*slot != NULL)
2222 {
2223 do_cleanups (cleanups);
2224 this_cu->v.quick->file_names = *slot;
2225 return *slot;
2226 }
2227
2228 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2229 }
2230 if (lh == NULL)
2231 {
2232 do_cleanups (cleanups);
2233 this_cu->v.quick->no_file_data = 1;
2234 return NULL;
2235 }
2236
2237 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2238 qfn->offset = line_offset;
2239 gdb_assert (slot != NULL);
2240 *slot = qfn;
2241
2242 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2243
2244 qfn->num_file_names = lh->num_file_names;
2245 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2246 lh->num_file_names * sizeof (char *));
2247 for (i = 0; i < lh->num_file_names; ++i)
2248 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2249 qfn->real_names = NULL;
2250
2251 free_line_header (lh);
2252 do_cleanups (cleanups);
2253
2254 this_cu->v.quick->file_names = qfn;
2255 return qfn;
2256 }
2257
2258 /* A helper for the "quick" functions which computes and caches the
2259 real path for a given file name from the line table. */
2260
2261 static const char *
2262 dw2_get_real_path (struct objfile *objfile,
2263 struct quick_file_names *qfn, int index)
2264 {
2265 if (qfn->real_names == NULL)
2266 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2267 qfn->num_file_names, sizeof (char *));
2268
2269 if (qfn->real_names[index] == NULL)
2270 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2271
2272 return qfn->real_names[index];
2273 }
2274
2275 static struct symtab *
2276 dw2_find_last_source_symtab (struct objfile *objfile)
2277 {
2278 int index;
2279
2280 dw2_setup (objfile);
2281 index = dwarf2_per_objfile->n_comp_units - 1;
2282 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2283 }
2284
2285 /* Traversal function for dw2_forget_cached_source_info. */
2286
2287 static int
2288 dw2_free_cached_file_names (void **slot, void *info)
2289 {
2290 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2291
2292 if (file_data->real_names)
2293 {
2294 int i;
2295
2296 for (i = 0; i < file_data->num_file_names; ++i)
2297 {
2298 xfree ((void*) file_data->real_names[i]);
2299 file_data->real_names[i] = NULL;
2300 }
2301 }
2302
2303 return 1;
2304 }
2305
2306 static void
2307 dw2_forget_cached_source_info (struct objfile *objfile)
2308 {
2309 dw2_setup (objfile);
2310
2311 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2312 dw2_free_cached_file_names, NULL);
2313 }
2314
2315 static int
2316 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2317 const char *full_path, const char *real_path,
2318 struct symtab **result)
2319 {
2320 int i;
2321 int check_basename = lbasename (name) == name;
2322 struct dwarf2_per_cu_data *base_cu = NULL;
2323
2324 dw2_setup (objfile);
2325
2326 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2327 + dwarf2_per_objfile->n_type_comp_units); ++i)
2328 {
2329 int j;
2330 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2331 struct quick_file_names *file_data;
2332
2333 if (per_cu->v.quick->symtab)
2334 continue;
2335
2336 file_data = dw2_get_file_names (objfile, per_cu);
2337 if (file_data == NULL)
2338 continue;
2339
2340 for (j = 0; j < file_data->num_file_names; ++j)
2341 {
2342 const char *this_name = file_data->file_names[j];
2343
2344 if (FILENAME_CMP (name, this_name) == 0)
2345 {
2346 *result = dw2_instantiate_symtab (objfile, per_cu);
2347 return 1;
2348 }
2349
2350 if (check_basename && ! base_cu
2351 && FILENAME_CMP (lbasename (this_name), name) == 0)
2352 base_cu = per_cu;
2353
2354 if (full_path != NULL)
2355 {
2356 const char *this_real_name = dw2_get_real_path (objfile,
2357 file_data, j);
2358
2359 if (this_real_name != NULL
2360 && FILENAME_CMP (full_path, this_real_name) == 0)
2361 {
2362 *result = dw2_instantiate_symtab (objfile, per_cu);
2363 return 1;
2364 }
2365 }
2366
2367 if (real_path != NULL)
2368 {
2369 const char *this_real_name = dw2_get_real_path (objfile,
2370 file_data, j);
2371
2372 if (this_real_name != NULL
2373 && FILENAME_CMP (real_path, this_real_name) == 0)
2374 {
2375 *result = dw2_instantiate_symtab (objfile, per_cu);
2376 return 1;
2377 }
2378 }
2379 }
2380 }
2381
2382 if (base_cu)
2383 {
2384 *result = dw2_instantiate_symtab (objfile, base_cu);
2385 return 1;
2386 }
2387
2388 return 0;
2389 }
2390
2391 static struct symtab *
2392 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2393 const char *name, domain_enum domain)
2394 {
2395 /* We do all the work in the pre_expand_symtabs_matching hook
2396 instead. */
2397 return NULL;
2398 }
2399
2400 /* A helper function that expands all symtabs that hold an object
2401 named NAME. */
2402
2403 static void
2404 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2405 {
2406 dw2_setup (objfile);
2407
2408 /* index_table is NULL if OBJF_READNOW. */
2409 if (dwarf2_per_objfile->index_table)
2410 {
2411 offset_type *vec;
2412
2413 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2414 name, &vec))
2415 {
2416 offset_type i, len = MAYBE_SWAP (*vec);
2417 for (i = 0; i < len; ++i)
2418 {
2419 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2420 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2421
2422 dw2_instantiate_symtab (objfile, per_cu);
2423 }
2424 }
2425 }
2426 }
2427
2428 static void
2429 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2430 enum block_enum block_kind, const char *name,
2431 domain_enum domain)
2432 {
2433 dw2_do_expand_symtabs_matching (objfile, name);
2434 }
2435
2436 static void
2437 dw2_print_stats (struct objfile *objfile)
2438 {
2439 int i, count;
2440
2441 dw2_setup (objfile);
2442 count = 0;
2443 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2444 + dwarf2_per_objfile->n_type_comp_units); ++i)
2445 {
2446 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2447
2448 if (!per_cu->v.quick->symtab)
2449 ++count;
2450 }
2451 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2452 }
2453
2454 static void
2455 dw2_dump (struct objfile *objfile)
2456 {
2457 /* Nothing worth printing. */
2458 }
2459
2460 static void
2461 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2462 struct section_offsets *delta)
2463 {
2464 /* There's nothing to relocate here. */
2465 }
2466
2467 static void
2468 dw2_expand_symtabs_for_function (struct objfile *objfile,
2469 const char *func_name)
2470 {
2471 dw2_do_expand_symtabs_matching (objfile, func_name);
2472 }
2473
2474 static void
2475 dw2_expand_all_symtabs (struct objfile *objfile)
2476 {
2477 int i;
2478
2479 dw2_setup (objfile);
2480
2481 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2482 + dwarf2_per_objfile->n_type_comp_units); ++i)
2483 {
2484 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2485
2486 dw2_instantiate_symtab (objfile, per_cu);
2487 }
2488 }
2489
2490 static void
2491 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2492 const char *filename)
2493 {
2494 int i;
2495
2496 dw2_setup (objfile);
2497
2498 /* We don't need to consider type units here.
2499 This is only called for examining code, e.g. expand_line_sal.
2500 There can be an order of magnitude (or more) more type units
2501 than comp units, and we avoid them if we can. */
2502
2503 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2504 {
2505 int j;
2506 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2507 struct quick_file_names *file_data;
2508
2509 if (per_cu->v.quick->symtab)
2510 continue;
2511
2512 file_data = dw2_get_file_names (objfile, per_cu);
2513 if (file_data == NULL)
2514 continue;
2515
2516 for (j = 0; j < file_data->num_file_names; ++j)
2517 {
2518 const char *this_name = file_data->file_names[j];
2519 if (FILENAME_CMP (this_name, filename) == 0)
2520 {
2521 dw2_instantiate_symtab (objfile, per_cu);
2522 break;
2523 }
2524 }
2525 }
2526 }
2527
2528 static const char *
2529 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2530 {
2531 struct dwarf2_per_cu_data *per_cu;
2532 offset_type *vec;
2533 struct quick_file_names *file_data;
2534
2535 dw2_setup (objfile);
2536
2537 /* index_table is NULL if OBJF_READNOW. */
2538 if (!dwarf2_per_objfile->index_table)
2539 return NULL;
2540
2541 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2542 name, &vec))
2543 return NULL;
2544
2545 /* Note that this just looks at the very first one named NAME -- but
2546 actually we are looking for a function. find_main_filename
2547 should be rewritten so that it doesn't require a custom hook. It
2548 could just use the ordinary symbol tables. */
2549 /* vec[0] is the length, which must always be >0. */
2550 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2551
2552 file_data = dw2_get_file_names (objfile, per_cu);
2553 if (file_data == NULL)
2554 return NULL;
2555
2556 return file_data->file_names[file_data->num_file_names - 1];
2557 }
2558
2559 static void
2560 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2561 struct objfile *objfile, int global,
2562 int (*callback) (struct block *,
2563 struct symbol *, void *),
2564 void *data, symbol_compare_ftype *match,
2565 symbol_compare_ftype *ordered_compare)
2566 {
2567 /* Currently unimplemented; used for Ada. The function can be called if the
2568 current language is Ada for a non-Ada objfile using GNU index. As Ada
2569 does not look for non-Ada symbols this function should just return. */
2570 }
2571
2572 static void
2573 dw2_expand_symtabs_matching (struct objfile *objfile,
2574 int (*file_matcher) (const char *, void *),
2575 int (*name_matcher) (const char *, void *),
2576 enum search_domain kind,
2577 void *data)
2578 {
2579 int i;
2580 offset_type iter;
2581 struct mapped_index *index;
2582
2583 dw2_setup (objfile);
2584
2585 /* index_table is NULL if OBJF_READNOW. */
2586 if (!dwarf2_per_objfile->index_table)
2587 return;
2588 index = dwarf2_per_objfile->index_table;
2589
2590 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2591 + dwarf2_per_objfile->n_type_comp_units); ++i)
2592 {
2593 int j;
2594 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2595 struct quick_file_names *file_data;
2596
2597 per_cu->v.quick->mark = 0;
2598 if (per_cu->v.quick->symtab)
2599 continue;
2600
2601 file_data = dw2_get_file_names (objfile, per_cu);
2602 if (file_data == NULL)
2603 continue;
2604
2605 for (j = 0; j < file_data->num_file_names; ++j)
2606 {
2607 if (file_matcher (file_data->file_names[j], data))
2608 {
2609 per_cu->v.quick->mark = 1;
2610 break;
2611 }
2612 }
2613 }
2614
2615 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2616 {
2617 offset_type idx = 2 * iter;
2618 const char *name;
2619 offset_type *vec, vec_len, vec_idx;
2620
2621 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2622 continue;
2623
2624 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2625
2626 if (! (*name_matcher) (name, data))
2627 continue;
2628
2629 /* The name was matched, now expand corresponding CUs that were
2630 marked. */
2631 vec = (offset_type *) (index->constant_pool
2632 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2633 vec_len = MAYBE_SWAP (vec[0]);
2634 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2635 {
2636 struct dwarf2_per_cu_data *per_cu;
2637
2638 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2639 if (per_cu->v.quick->mark)
2640 dw2_instantiate_symtab (objfile, per_cu);
2641 }
2642 }
2643 }
2644
2645 static struct symtab *
2646 dw2_find_pc_sect_symtab (struct objfile *objfile,
2647 struct minimal_symbol *msymbol,
2648 CORE_ADDR pc,
2649 struct obj_section *section,
2650 int warn_if_readin)
2651 {
2652 struct dwarf2_per_cu_data *data;
2653
2654 dw2_setup (objfile);
2655
2656 if (!objfile->psymtabs_addrmap)
2657 return NULL;
2658
2659 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2660 if (!data)
2661 return NULL;
2662
2663 if (warn_if_readin && data->v.quick->symtab)
2664 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2665 paddress (get_objfile_arch (objfile), pc));
2666
2667 return dw2_instantiate_symtab (objfile, data);
2668 }
2669
2670 static void
2671 dw2_map_symbol_names (struct objfile *objfile,
2672 void (*fun) (const char *, void *),
2673 void *data)
2674 {
2675 offset_type iter;
2676 struct mapped_index *index;
2677
2678 dw2_setup (objfile);
2679
2680 /* index_table is NULL if OBJF_READNOW. */
2681 if (!dwarf2_per_objfile->index_table)
2682 return;
2683 index = dwarf2_per_objfile->index_table;
2684
2685 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2686 {
2687 offset_type idx = 2 * iter;
2688 const char *name;
2689 offset_type *vec, vec_len, vec_idx;
2690
2691 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2692 continue;
2693
2694 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
2695
2696 (*fun) (name, data);
2697 }
2698 }
2699
2700 static void
2701 dw2_map_symbol_filenames (struct objfile *objfile,
2702 void (*fun) (const char *, const char *, void *),
2703 void *data)
2704 {
2705 int i;
2706
2707 dw2_setup (objfile);
2708
2709 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2710 + dwarf2_per_objfile->n_type_comp_units); ++i)
2711 {
2712 int j;
2713 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2714 struct quick_file_names *file_data;
2715
2716 if (per_cu->v.quick->symtab)
2717 continue;
2718
2719 file_data = dw2_get_file_names (objfile, per_cu);
2720 if (file_data == NULL)
2721 continue;
2722
2723 for (j = 0; j < file_data->num_file_names; ++j)
2724 {
2725 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2726 j);
2727 (*fun) (file_data->file_names[j], this_real_name, data);
2728 }
2729 }
2730 }
2731
2732 static int
2733 dw2_has_symbols (struct objfile *objfile)
2734 {
2735 return 1;
2736 }
2737
2738 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2739 {
2740 dw2_has_symbols,
2741 dw2_find_last_source_symtab,
2742 dw2_forget_cached_source_info,
2743 dw2_lookup_symtab,
2744 dw2_lookup_symbol,
2745 dw2_pre_expand_symtabs_matching,
2746 dw2_print_stats,
2747 dw2_dump,
2748 dw2_relocate,
2749 dw2_expand_symtabs_for_function,
2750 dw2_expand_all_symtabs,
2751 dw2_expand_symtabs_with_filename,
2752 dw2_find_symbol_file,
2753 dw2_map_matching_symbols,
2754 dw2_expand_symtabs_matching,
2755 dw2_find_pc_sect_symtab,
2756 dw2_map_symbol_names,
2757 dw2_map_symbol_filenames
2758 };
2759
2760 /* Initialize for reading DWARF for this objfile. Return 0 if this
2761 file will use psymtabs, or 1 if using the GNU index. */
2762
2763 int
2764 dwarf2_initialize_objfile (struct objfile *objfile)
2765 {
2766 /* If we're about to read full symbols, don't bother with the
2767 indices. In this case we also don't care if some other debug
2768 format is making psymtabs, because they are all about to be
2769 expanded anyway. */
2770 if ((objfile->flags & OBJF_READNOW))
2771 {
2772 int i;
2773
2774 dwarf2_per_objfile->using_index = 1;
2775 create_all_comp_units (objfile);
2776 create_debug_types_hash_table (objfile);
2777 dwarf2_per_objfile->quick_file_names_table =
2778 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2779
2780 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2781 + dwarf2_per_objfile->n_type_comp_units); ++i)
2782 {
2783 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2784
2785 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2786 struct dwarf2_per_cu_quick_data);
2787 }
2788
2789 /* Return 1 so that gdb sees the "quick" functions. However,
2790 these functions will be no-ops because we will have expanded
2791 all symtabs. */
2792 return 1;
2793 }
2794
2795 if (dwarf2_read_index (objfile))
2796 return 1;
2797
2798 return 0;
2799 }
2800
2801 \f
2802
2803 /* Build a partial symbol table. */
2804
2805 void
2806 dwarf2_build_psymtabs (struct objfile *objfile)
2807 {
2808 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2809 {
2810 init_psymbol_list (objfile, 1024);
2811 }
2812
2813 dwarf2_build_psymtabs_hard (objfile);
2814 }
2815
2816 /* Return TRUE if OFFSET is within CU_HEADER. */
2817
2818 static inline int
2819 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2820 {
2821 unsigned int bottom = cu_header->offset;
2822 unsigned int top = (cu_header->offset
2823 + cu_header->length
2824 + cu_header->initial_length_size);
2825
2826 return (offset >= bottom && offset < top);
2827 }
2828
2829 /* Read in the comp unit header information from the debug_info at info_ptr.
2830 NOTE: This leaves members offset, first_die_offset to be filled in
2831 by the caller. */
2832
2833 static gdb_byte *
2834 read_comp_unit_head (struct comp_unit_head *cu_header,
2835 gdb_byte *info_ptr, bfd *abfd)
2836 {
2837 int signed_addr;
2838 unsigned int bytes_read;
2839
2840 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2841 cu_header->initial_length_size = bytes_read;
2842 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2843 info_ptr += bytes_read;
2844 cu_header->version = read_2_bytes (abfd, info_ptr);
2845 info_ptr += 2;
2846 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2847 &bytes_read);
2848 info_ptr += bytes_read;
2849 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2850 info_ptr += 1;
2851 signed_addr = bfd_get_sign_extend_vma (abfd);
2852 if (signed_addr < 0)
2853 internal_error (__FILE__, __LINE__,
2854 _("read_comp_unit_head: dwarf from non elf file"));
2855 cu_header->signed_addr_p = signed_addr;
2856
2857 return info_ptr;
2858 }
2859
2860 static gdb_byte *
2861 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2862 gdb_byte *buffer, unsigned int buffer_size,
2863 bfd *abfd)
2864 {
2865 gdb_byte *beg_of_comp_unit = info_ptr;
2866
2867 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2868
2869 if (header->version != 2 && header->version != 3 && header->version != 4)
2870 error (_("Dwarf Error: wrong version in compilation unit header "
2871 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2872 bfd_get_filename (abfd));
2873
2874 if (header->abbrev_offset
2875 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2876 &dwarf2_per_objfile->abbrev))
2877 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2878 "(offset 0x%lx + 6) [in module %s]"),
2879 (long) header->abbrev_offset,
2880 (long) (beg_of_comp_unit - buffer),
2881 bfd_get_filename (abfd));
2882
2883 if (beg_of_comp_unit + header->length + header->initial_length_size
2884 > buffer + buffer_size)
2885 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2886 "(offset 0x%lx + 0) [in module %s]"),
2887 (long) header->length,
2888 (long) (beg_of_comp_unit - buffer),
2889 bfd_get_filename (abfd));
2890
2891 return info_ptr;
2892 }
2893
2894 /* Read in the types comp unit header information from .debug_types entry at
2895 types_ptr. The result is a pointer to one past the end of the header. */
2896
2897 static gdb_byte *
2898 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2899 ULONGEST *signature,
2900 gdb_byte *types_ptr, bfd *abfd)
2901 {
2902 gdb_byte *initial_types_ptr = types_ptr;
2903
2904 dwarf2_read_section (dwarf2_per_objfile->objfile,
2905 &dwarf2_per_objfile->types);
2906 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2907
2908 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2909
2910 *signature = read_8_bytes (abfd, types_ptr);
2911 types_ptr += 8;
2912 types_ptr += cu_header->offset_size;
2913 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2914
2915 return types_ptr;
2916 }
2917
2918 /* Allocate a new partial symtab for file named NAME and mark this new
2919 partial symtab as being an include of PST. */
2920
2921 static void
2922 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2923 struct objfile *objfile)
2924 {
2925 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2926
2927 subpst->section_offsets = pst->section_offsets;
2928 subpst->textlow = 0;
2929 subpst->texthigh = 0;
2930
2931 subpst->dependencies = (struct partial_symtab **)
2932 obstack_alloc (&objfile->objfile_obstack,
2933 sizeof (struct partial_symtab *));
2934 subpst->dependencies[0] = pst;
2935 subpst->number_of_dependencies = 1;
2936
2937 subpst->globals_offset = 0;
2938 subpst->n_global_syms = 0;
2939 subpst->statics_offset = 0;
2940 subpst->n_static_syms = 0;
2941 subpst->symtab = NULL;
2942 subpst->read_symtab = pst->read_symtab;
2943 subpst->readin = 0;
2944
2945 /* No private part is necessary for include psymtabs. This property
2946 can be used to differentiate between such include psymtabs and
2947 the regular ones. */
2948 subpst->read_symtab_private = NULL;
2949 }
2950
2951 /* Read the Line Number Program data and extract the list of files
2952 included by the source file represented by PST. Build an include
2953 partial symtab for each of these included files. */
2954
2955 static void
2956 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2957 struct die_info *die,
2958 struct partial_symtab *pst)
2959 {
2960 struct objfile *objfile = cu->objfile;
2961 bfd *abfd = objfile->obfd;
2962 struct line_header *lh = NULL;
2963 struct attribute *attr;
2964
2965 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2966 if (attr)
2967 {
2968 unsigned int line_offset = DW_UNSND (attr);
2969
2970 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2971 }
2972 if (lh == NULL)
2973 return; /* No linetable, so no includes. */
2974
2975 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2976 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2977
2978 free_line_header (lh);
2979 }
2980
2981 static hashval_t
2982 hash_type_signature (const void *item)
2983 {
2984 const struct signatured_type *type_sig = item;
2985
2986 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2987 return type_sig->signature;
2988 }
2989
2990 static int
2991 eq_type_signature (const void *item_lhs, const void *item_rhs)
2992 {
2993 const struct signatured_type *lhs = item_lhs;
2994 const struct signatured_type *rhs = item_rhs;
2995
2996 return lhs->signature == rhs->signature;
2997 }
2998
2999 /* Allocate a hash table for signatured types. */
3000
3001 static htab_t
3002 allocate_signatured_type_table (struct objfile *objfile)
3003 {
3004 return htab_create_alloc_ex (41,
3005 hash_type_signature,
3006 eq_type_signature,
3007 NULL,
3008 &objfile->objfile_obstack,
3009 hashtab_obstack_allocate,
3010 dummy_obstack_deallocate);
3011 }
3012
3013 /* A helper function to add a signatured type CU to a list. */
3014
3015 static int
3016 add_signatured_type_cu_to_list (void **slot, void *datum)
3017 {
3018 struct signatured_type *sigt = *slot;
3019 struct dwarf2_per_cu_data ***datap = datum;
3020
3021 **datap = &sigt->per_cu;
3022 ++*datap;
3023
3024 return 1;
3025 }
3026
3027 /* Create the hash table of all entries in the .debug_types section.
3028 The result is zero if there is an error (e.g. missing .debug_types section),
3029 otherwise non-zero. */
3030
3031 static int
3032 create_debug_types_hash_table (struct objfile *objfile)
3033 {
3034 gdb_byte *info_ptr;
3035 htab_t types_htab;
3036 struct dwarf2_per_cu_data **iter;
3037
3038 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3039 info_ptr = dwarf2_per_objfile->types.buffer;
3040
3041 if (info_ptr == NULL)
3042 {
3043 dwarf2_per_objfile->signatured_types = NULL;
3044 return 0;
3045 }
3046
3047 types_htab = allocate_signatured_type_table (objfile);
3048
3049 if (dwarf2_die_debug)
3050 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3051
3052 while (info_ptr < dwarf2_per_objfile->types.buffer
3053 + dwarf2_per_objfile->types.size)
3054 {
3055 unsigned int offset;
3056 unsigned int offset_size;
3057 unsigned int type_offset;
3058 unsigned int length, initial_length_size;
3059 unsigned short version;
3060 ULONGEST signature;
3061 struct signatured_type *type_sig;
3062 void **slot;
3063 gdb_byte *ptr = info_ptr;
3064
3065 offset = ptr - dwarf2_per_objfile->types.buffer;
3066
3067 /* We need to read the type's signature in order to build the hash
3068 table, but we don't need to read anything else just yet. */
3069
3070 /* Sanity check to ensure entire cu is present. */
3071 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3072 if (ptr + length + initial_length_size
3073 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3074 {
3075 complaint (&symfile_complaints,
3076 _("debug type entry runs off end "
3077 "of `.debug_types' section, ignored"));
3078 break;
3079 }
3080
3081 offset_size = initial_length_size == 4 ? 4 : 8;
3082 ptr += initial_length_size;
3083 version = bfd_get_16 (objfile->obfd, ptr);
3084 ptr += 2;
3085 ptr += offset_size; /* abbrev offset */
3086 ptr += 1; /* address size */
3087 signature = bfd_get_64 (objfile->obfd, ptr);
3088 ptr += 8;
3089 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3090
3091 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3092 memset (type_sig, 0, sizeof (*type_sig));
3093 type_sig->signature = signature;
3094 type_sig->offset = offset;
3095 type_sig->type_offset = type_offset;
3096 type_sig->per_cu.objfile = objfile;
3097 type_sig->per_cu.from_debug_types = 1;
3098
3099 slot = htab_find_slot (types_htab, type_sig, INSERT);
3100 gdb_assert (slot != NULL);
3101 *slot = type_sig;
3102
3103 if (dwarf2_die_debug)
3104 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3105 offset, phex (signature, sizeof (signature)));
3106
3107 info_ptr = info_ptr + initial_length_size + length;
3108 }
3109
3110 dwarf2_per_objfile->signatured_types = types_htab;
3111
3112 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3113 dwarf2_per_objfile->type_comp_units
3114 = obstack_alloc (&objfile->objfile_obstack,
3115 dwarf2_per_objfile->n_type_comp_units
3116 * sizeof (struct dwarf2_per_cu_data *));
3117 iter = &dwarf2_per_objfile->type_comp_units[0];
3118 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3119 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3120 == dwarf2_per_objfile->n_type_comp_units);
3121
3122 return 1;
3123 }
3124
3125 /* Lookup a signature based type.
3126 Returns NULL if SIG is not present in the table. */
3127
3128 static struct signatured_type *
3129 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3130 {
3131 struct signatured_type find_entry, *entry;
3132
3133 if (dwarf2_per_objfile->signatured_types == NULL)
3134 {
3135 complaint (&symfile_complaints,
3136 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3137 return 0;
3138 }
3139
3140 find_entry.signature = sig;
3141 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3142 return entry;
3143 }
3144
3145 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3146
3147 static void
3148 init_cu_die_reader (struct die_reader_specs *reader,
3149 struct dwarf2_cu *cu)
3150 {
3151 reader->abfd = cu->objfile->obfd;
3152 reader->cu = cu;
3153 if (cu->per_cu->from_debug_types)
3154 {
3155 gdb_assert (dwarf2_per_objfile->types.readin);
3156 reader->buffer = dwarf2_per_objfile->types.buffer;
3157 }
3158 else
3159 {
3160 gdb_assert (dwarf2_per_objfile->info.readin);
3161 reader->buffer = dwarf2_per_objfile->info.buffer;
3162 }
3163 }
3164
3165 /* Find the base address of the compilation unit for range lists and
3166 location lists. It will normally be specified by DW_AT_low_pc.
3167 In DWARF-3 draft 4, the base address could be overridden by
3168 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3169 compilation units with discontinuous ranges. */
3170
3171 static void
3172 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3173 {
3174 struct attribute *attr;
3175
3176 cu->base_known = 0;
3177 cu->base_address = 0;
3178
3179 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3180 if (attr)
3181 {
3182 cu->base_address = DW_ADDR (attr);
3183 cu->base_known = 1;
3184 }
3185 else
3186 {
3187 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3188 if (attr)
3189 {
3190 cu->base_address = DW_ADDR (attr);
3191 cu->base_known = 1;
3192 }
3193 }
3194 }
3195
3196 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3197 to combine the common parts.
3198 Process a compilation unit for a psymtab.
3199 BUFFER is a pointer to the beginning of the dwarf section buffer,
3200 either .debug_info or debug_types.
3201 INFO_PTR is a pointer to the start of the CU.
3202 Returns a pointer to the next CU. */
3203
3204 static gdb_byte *
3205 process_psymtab_comp_unit (struct objfile *objfile,
3206 struct dwarf2_per_cu_data *this_cu,
3207 gdb_byte *buffer, gdb_byte *info_ptr,
3208 unsigned int buffer_size)
3209 {
3210 bfd *abfd = objfile->obfd;
3211 gdb_byte *beg_of_comp_unit = info_ptr;
3212 struct die_info *comp_unit_die;
3213 struct partial_symtab *pst;
3214 CORE_ADDR baseaddr;
3215 struct cleanup *back_to_inner;
3216 struct dwarf2_cu cu;
3217 int has_children, has_pc_info;
3218 struct attribute *attr;
3219 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3220 struct die_reader_specs reader_specs;
3221 const char *filename;
3222
3223 init_one_comp_unit (&cu, objfile);
3224 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3225
3226 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3227 buffer, buffer_size,
3228 abfd);
3229
3230 /* Complete the cu_header. */
3231 cu.header.offset = beg_of_comp_unit - buffer;
3232 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3233
3234 cu.list_in_scope = &file_symbols;
3235
3236 /* If this compilation unit was already read in, free the
3237 cached copy in order to read it in again. This is
3238 necessary because we skipped some symbols when we first
3239 read in the compilation unit (see load_partial_dies).
3240 This problem could be avoided, but the benefit is
3241 unclear. */
3242 if (this_cu->cu != NULL)
3243 free_one_cached_comp_unit (this_cu->cu);
3244
3245 /* Note that this is a pointer to our stack frame, being
3246 added to a global data structure. It will be cleaned up
3247 in free_stack_comp_unit when we finish with this
3248 compilation unit. */
3249 this_cu->cu = &cu;
3250 cu.per_cu = this_cu;
3251
3252 /* Read the abbrevs for this compilation unit into a table. */
3253 dwarf2_read_abbrevs (abfd, &cu);
3254 make_cleanup (dwarf2_free_abbrev_table, &cu);
3255
3256 /* Read the compilation unit die. */
3257 if (this_cu->from_debug_types)
3258 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3259 init_cu_die_reader (&reader_specs, &cu);
3260 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3261 &has_children);
3262
3263 if (this_cu->from_debug_types)
3264 {
3265 /* offset,length haven't been set yet for type units. */
3266 this_cu->offset = cu.header.offset;
3267 this_cu->length = cu.header.length + cu.header.initial_length_size;
3268 }
3269 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3270 {
3271 info_ptr = (beg_of_comp_unit + cu.header.length
3272 + cu.header.initial_length_size);
3273 do_cleanups (back_to_inner);
3274 return info_ptr;
3275 }
3276
3277 prepare_one_comp_unit (&cu, comp_unit_die);
3278
3279 /* Allocate a new partial symbol table structure. */
3280 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3281 if (attr == NULL || !DW_STRING (attr))
3282 filename = "";
3283 else
3284 filename = DW_STRING (attr);
3285 pst = start_psymtab_common (objfile, objfile->section_offsets,
3286 filename,
3287 /* TEXTLOW and TEXTHIGH are set below. */
3288 0,
3289 objfile->global_psymbols.next,
3290 objfile->static_psymbols.next);
3291
3292 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3293 if (attr != NULL)
3294 pst->dirname = DW_STRING (attr);
3295
3296 pst->read_symtab_private = this_cu;
3297
3298 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3299
3300 /* Store the function that reads in the rest of the symbol table. */
3301 pst->read_symtab = dwarf2_psymtab_to_symtab;
3302
3303 this_cu->v.psymtab = pst;
3304
3305 dwarf2_find_base_address (comp_unit_die, &cu);
3306
3307 /* Possibly set the default values of LOWPC and HIGHPC from
3308 `DW_AT_ranges'. */
3309 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3310 &best_highpc, &cu, pst);
3311 if (has_pc_info == 1 && best_lowpc < best_highpc)
3312 /* Store the contiguous range if it is not empty; it can be empty for
3313 CUs with no code. */
3314 addrmap_set_empty (objfile->psymtabs_addrmap,
3315 best_lowpc + baseaddr,
3316 best_highpc + baseaddr - 1, pst);
3317
3318 /* Check if comp unit has_children.
3319 If so, read the rest of the partial symbols from this comp unit.
3320 If not, there's no more debug_info for this comp unit. */
3321 if (has_children)
3322 {
3323 struct partial_die_info *first_die;
3324 CORE_ADDR lowpc, highpc;
3325
3326 lowpc = ((CORE_ADDR) -1);
3327 highpc = ((CORE_ADDR) 0);
3328
3329 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3330
3331 scan_partial_symbols (first_die, &lowpc, &highpc,
3332 ! has_pc_info, &cu);
3333
3334 /* If we didn't find a lowpc, set it to highpc to avoid
3335 complaints from `maint check'. */
3336 if (lowpc == ((CORE_ADDR) -1))
3337 lowpc = highpc;
3338
3339 /* If the compilation unit didn't have an explicit address range,
3340 then use the information extracted from its child dies. */
3341 if (! has_pc_info)
3342 {
3343 best_lowpc = lowpc;
3344 best_highpc = highpc;
3345 }
3346 }
3347 pst->textlow = best_lowpc + baseaddr;
3348 pst->texthigh = best_highpc + baseaddr;
3349
3350 pst->n_global_syms = objfile->global_psymbols.next -
3351 (objfile->global_psymbols.list + pst->globals_offset);
3352 pst->n_static_syms = objfile->static_psymbols.next -
3353 (objfile->static_psymbols.list + pst->statics_offset);
3354 sort_pst_symbols (pst);
3355
3356 info_ptr = (beg_of_comp_unit + cu.header.length
3357 + cu.header.initial_length_size);
3358
3359 if (this_cu->from_debug_types)
3360 {
3361 /* It's not clear we want to do anything with stmt lists here.
3362 Waiting to see what gcc ultimately does. */
3363 }
3364 else
3365 {
3366 /* Get the list of files included in the current compilation unit,
3367 and build a psymtab for each of them. */
3368 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3369 }
3370
3371 do_cleanups (back_to_inner);
3372
3373 return info_ptr;
3374 }
3375
3376 /* Traversal function for htab_traverse_noresize.
3377 Process one .debug_types comp-unit. */
3378
3379 static int
3380 process_type_comp_unit (void **slot, void *info)
3381 {
3382 struct signatured_type *entry = (struct signatured_type *) *slot;
3383 struct objfile *objfile = (struct objfile *) info;
3384 struct dwarf2_per_cu_data *this_cu;
3385
3386 this_cu = &entry->per_cu;
3387
3388 gdb_assert (dwarf2_per_objfile->types.readin);
3389 process_psymtab_comp_unit (objfile, this_cu,
3390 dwarf2_per_objfile->types.buffer,
3391 dwarf2_per_objfile->types.buffer + entry->offset,
3392 dwarf2_per_objfile->types.size);
3393
3394 return 1;
3395 }
3396
3397 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3398 Build partial symbol tables for the .debug_types comp-units. */
3399
3400 static void
3401 build_type_psymtabs (struct objfile *objfile)
3402 {
3403 if (! create_debug_types_hash_table (objfile))
3404 return;
3405
3406 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3407 process_type_comp_unit, objfile);
3408 }
3409
3410 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3411
3412 static void
3413 psymtabs_addrmap_cleanup (void *o)
3414 {
3415 struct objfile *objfile = o;
3416
3417 objfile->psymtabs_addrmap = NULL;
3418 }
3419
3420 /* Build the partial symbol table by doing a quick pass through the
3421 .debug_info and .debug_abbrev sections. */
3422
3423 static void
3424 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3425 {
3426 gdb_byte *info_ptr;
3427 struct cleanup *back_to, *addrmap_cleanup;
3428 struct obstack temp_obstack;
3429
3430 dwarf2_per_objfile->reading_partial_symbols = 1;
3431
3432 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3433 info_ptr = dwarf2_per_objfile->info.buffer;
3434
3435 /* Any cached compilation units will be linked by the per-objfile
3436 read_in_chain. Make sure to free them when we're done. */
3437 back_to = make_cleanup (free_cached_comp_units, NULL);
3438
3439 build_type_psymtabs (objfile);
3440
3441 create_all_comp_units (objfile);
3442
3443 /* Create a temporary address map on a temporary obstack. We later
3444 copy this to the final obstack. */
3445 obstack_init (&temp_obstack);
3446 make_cleanup_obstack_free (&temp_obstack);
3447 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3448 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3449
3450 /* Since the objects we're extracting from .debug_info vary in
3451 length, only the individual functions to extract them (like
3452 read_comp_unit_head and load_partial_die) can really know whether
3453 the buffer is large enough to hold another complete object.
3454
3455 At the moment, they don't actually check that. If .debug_info
3456 holds just one extra byte after the last compilation unit's dies,
3457 then read_comp_unit_head will happily read off the end of the
3458 buffer. read_partial_die is similarly casual. Those functions
3459 should be fixed.
3460
3461 For this loop condition, simply checking whether there's any data
3462 left at all should be sufficient. */
3463
3464 while (info_ptr < (dwarf2_per_objfile->info.buffer
3465 + dwarf2_per_objfile->info.size))
3466 {
3467 struct dwarf2_per_cu_data *this_cu;
3468
3469 this_cu = dwarf2_find_comp_unit (info_ptr
3470 - dwarf2_per_objfile->info.buffer,
3471 objfile);
3472
3473 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3474 dwarf2_per_objfile->info.buffer,
3475 info_ptr,
3476 dwarf2_per_objfile->info.size);
3477 }
3478
3479 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3480 &objfile->objfile_obstack);
3481 discard_cleanups (addrmap_cleanup);
3482
3483 do_cleanups (back_to);
3484 }
3485
3486 /* Load the partial DIEs for a secondary CU into memory. */
3487
3488 static void
3489 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3490 struct objfile *objfile)
3491 {
3492 bfd *abfd = objfile->obfd;
3493 gdb_byte *info_ptr, *beg_of_comp_unit;
3494 struct die_info *comp_unit_die;
3495 struct dwarf2_cu *cu;
3496 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3497 int has_children;
3498 struct die_reader_specs reader_specs;
3499 int read_cu = 0;
3500
3501 gdb_assert (! this_cu->from_debug_types);
3502
3503 gdb_assert (dwarf2_per_objfile->info.readin);
3504 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3505 beg_of_comp_unit = info_ptr;
3506
3507 if (this_cu->cu == NULL)
3508 {
3509 cu = xmalloc (sizeof (*cu));
3510 init_one_comp_unit (cu, objfile);
3511
3512 read_cu = 1;
3513
3514 /* If an error occurs while loading, release our storage. */
3515 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3516
3517 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3518 dwarf2_per_objfile->info.buffer,
3519 dwarf2_per_objfile->info.size,
3520 abfd);
3521
3522 /* Complete the cu_header. */
3523 cu->header.offset = this_cu->offset;
3524 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3525
3526 /* Link this compilation unit into the compilation unit tree. */
3527 this_cu->cu = cu;
3528 cu->per_cu = this_cu;
3529
3530 /* Link this CU into read_in_chain. */
3531 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3532 dwarf2_per_objfile->read_in_chain = this_cu;
3533 }
3534 else
3535 {
3536 cu = this_cu->cu;
3537 info_ptr += cu->header.first_die_offset;
3538 }
3539
3540 /* Read the abbrevs for this compilation unit into a table. */
3541 gdb_assert (cu->dwarf2_abbrevs == NULL);
3542 dwarf2_read_abbrevs (abfd, cu);
3543 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3544
3545 /* Read the compilation unit die. */
3546 init_cu_die_reader (&reader_specs, cu);
3547 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3548 &has_children);
3549
3550 prepare_one_comp_unit (cu, comp_unit_die);
3551
3552 /* Check if comp unit has_children.
3553 If so, read the rest of the partial symbols from this comp unit.
3554 If not, there's no more debug_info for this comp unit. */
3555 if (has_children)
3556 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3557
3558 do_cleanups (free_abbrevs_cleanup);
3559
3560 if (read_cu)
3561 {
3562 /* We've successfully allocated this compilation unit. Let our
3563 caller clean it up when finished with it. */
3564 discard_cleanups (free_cu_cleanup);
3565 }
3566 }
3567
3568 /* Create a list of all compilation units in OBJFILE. We do this only
3569 if an inter-comp-unit reference is found; presumably if there is one,
3570 there will be many, and one will occur early in the .debug_info section.
3571 So there's no point in building this list incrementally. */
3572
3573 static void
3574 create_all_comp_units (struct objfile *objfile)
3575 {
3576 int n_allocated;
3577 int n_comp_units;
3578 struct dwarf2_per_cu_data **all_comp_units;
3579 gdb_byte *info_ptr;
3580
3581 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3582 info_ptr = dwarf2_per_objfile->info.buffer;
3583
3584 n_comp_units = 0;
3585 n_allocated = 10;
3586 all_comp_units = xmalloc (n_allocated
3587 * sizeof (struct dwarf2_per_cu_data *));
3588
3589 while (info_ptr < dwarf2_per_objfile->info.buffer
3590 + dwarf2_per_objfile->info.size)
3591 {
3592 unsigned int length, initial_length_size;
3593 struct dwarf2_per_cu_data *this_cu;
3594 unsigned int offset;
3595
3596 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3597
3598 /* Read just enough information to find out where the next
3599 compilation unit is. */
3600 length = read_initial_length (objfile->obfd, info_ptr,
3601 &initial_length_size);
3602
3603 /* Save the compilation unit for later lookup. */
3604 this_cu = obstack_alloc (&objfile->objfile_obstack,
3605 sizeof (struct dwarf2_per_cu_data));
3606 memset (this_cu, 0, sizeof (*this_cu));
3607 this_cu->offset = offset;
3608 this_cu->length = length + initial_length_size;
3609 this_cu->objfile = objfile;
3610
3611 if (n_comp_units == n_allocated)
3612 {
3613 n_allocated *= 2;
3614 all_comp_units = xrealloc (all_comp_units,
3615 n_allocated
3616 * sizeof (struct dwarf2_per_cu_data *));
3617 }
3618 all_comp_units[n_comp_units++] = this_cu;
3619
3620 info_ptr = info_ptr + this_cu->length;
3621 }
3622
3623 dwarf2_per_objfile->all_comp_units
3624 = obstack_alloc (&objfile->objfile_obstack,
3625 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3626 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3627 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3628 xfree (all_comp_units);
3629 dwarf2_per_objfile->n_comp_units = n_comp_units;
3630 }
3631
3632 /* Process all loaded DIEs for compilation unit CU, starting at
3633 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3634 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3635 DW_AT_ranges). If NEED_PC is set, then this function will set
3636 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3637 and record the covered ranges in the addrmap. */
3638
3639 static void
3640 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3641 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3642 {
3643 struct partial_die_info *pdi;
3644
3645 /* Now, march along the PDI's, descending into ones which have
3646 interesting children but skipping the children of the other ones,
3647 until we reach the end of the compilation unit. */
3648
3649 pdi = first_die;
3650
3651 while (pdi != NULL)
3652 {
3653 fixup_partial_die (pdi, cu);
3654
3655 /* Anonymous namespaces or modules have no name but have interesting
3656 children, so we need to look at them. Ditto for anonymous
3657 enums. */
3658
3659 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3660 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3661 {
3662 switch (pdi->tag)
3663 {
3664 case DW_TAG_subprogram:
3665 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3666 break;
3667 case DW_TAG_constant:
3668 case DW_TAG_variable:
3669 case DW_TAG_typedef:
3670 case DW_TAG_union_type:
3671 if (!pdi->is_declaration)
3672 {
3673 add_partial_symbol (pdi, cu);
3674 }
3675 break;
3676 case DW_TAG_class_type:
3677 case DW_TAG_interface_type:
3678 case DW_TAG_structure_type:
3679 if (!pdi->is_declaration)
3680 {
3681 add_partial_symbol (pdi, cu);
3682 }
3683 break;
3684 case DW_TAG_enumeration_type:
3685 if (!pdi->is_declaration)
3686 add_partial_enumeration (pdi, cu);
3687 break;
3688 case DW_TAG_base_type:
3689 case DW_TAG_subrange_type:
3690 /* File scope base type definitions are added to the partial
3691 symbol table. */
3692 add_partial_symbol (pdi, cu);
3693 break;
3694 case DW_TAG_namespace:
3695 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3696 break;
3697 case DW_TAG_module:
3698 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3699 break;
3700 default:
3701 break;
3702 }
3703 }
3704
3705 /* If the die has a sibling, skip to the sibling. */
3706
3707 pdi = pdi->die_sibling;
3708 }
3709 }
3710
3711 /* Functions used to compute the fully scoped name of a partial DIE.
3712
3713 Normally, this is simple. For C++, the parent DIE's fully scoped
3714 name is concatenated with "::" and the partial DIE's name. For
3715 Java, the same thing occurs except that "." is used instead of "::".
3716 Enumerators are an exception; they use the scope of their parent
3717 enumeration type, i.e. the name of the enumeration type is not
3718 prepended to the enumerator.
3719
3720 There are two complexities. One is DW_AT_specification; in this
3721 case "parent" means the parent of the target of the specification,
3722 instead of the direct parent of the DIE. The other is compilers
3723 which do not emit DW_TAG_namespace; in this case we try to guess
3724 the fully qualified name of structure types from their members'
3725 linkage names. This must be done using the DIE's children rather
3726 than the children of any DW_AT_specification target. We only need
3727 to do this for structures at the top level, i.e. if the target of
3728 any DW_AT_specification (if any; otherwise the DIE itself) does not
3729 have a parent. */
3730
3731 /* Compute the scope prefix associated with PDI's parent, in
3732 compilation unit CU. The result will be allocated on CU's
3733 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3734 field. NULL is returned if no prefix is necessary. */
3735 static char *
3736 partial_die_parent_scope (struct partial_die_info *pdi,
3737 struct dwarf2_cu *cu)
3738 {
3739 char *grandparent_scope;
3740 struct partial_die_info *parent, *real_pdi;
3741
3742 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3743 then this means the parent of the specification DIE. */
3744
3745 real_pdi = pdi;
3746 while (real_pdi->has_specification)
3747 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3748
3749 parent = real_pdi->die_parent;
3750 if (parent == NULL)
3751 return NULL;
3752
3753 if (parent->scope_set)
3754 return parent->scope;
3755
3756 fixup_partial_die (parent, cu);
3757
3758 grandparent_scope = partial_die_parent_scope (parent, cu);
3759
3760 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3761 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3762 Work around this problem here. */
3763 if (cu->language == language_cplus
3764 && parent->tag == DW_TAG_namespace
3765 && strcmp (parent->name, "::") == 0
3766 && grandparent_scope == NULL)
3767 {
3768 parent->scope = NULL;
3769 parent->scope_set = 1;
3770 return NULL;
3771 }
3772
3773 if (parent->tag == DW_TAG_namespace
3774 || parent->tag == DW_TAG_module
3775 || parent->tag == DW_TAG_structure_type
3776 || parent->tag == DW_TAG_class_type
3777 || parent->tag == DW_TAG_interface_type
3778 || parent->tag == DW_TAG_union_type
3779 || parent->tag == DW_TAG_enumeration_type)
3780 {
3781 if (grandparent_scope == NULL)
3782 parent->scope = parent->name;
3783 else
3784 parent->scope = typename_concat (&cu->comp_unit_obstack,
3785 grandparent_scope,
3786 parent->name, 0, cu);
3787 }
3788 else if (parent->tag == DW_TAG_enumerator)
3789 /* Enumerators should not get the name of the enumeration as a prefix. */
3790 parent->scope = grandparent_scope;
3791 else
3792 {
3793 /* FIXME drow/2004-04-01: What should we be doing with
3794 function-local names? For partial symbols, we should probably be
3795 ignoring them. */
3796 complaint (&symfile_complaints,
3797 _("unhandled containing DIE tag %d for DIE at %d"),
3798 parent->tag, pdi->offset);
3799 parent->scope = grandparent_scope;
3800 }
3801
3802 parent->scope_set = 1;
3803 return parent->scope;
3804 }
3805
3806 /* Return the fully scoped name associated with PDI, from compilation unit
3807 CU. The result will be allocated with malloc. */
3808 static char *
3809 partial_die_full_name (struct partial_die_info *pdi,
3810 struct dwarf2_cu *cu)
3811 {
3812 char *parent_scope;
3813
3814 /* If this is a template instantiation, we can not work out the
3815 template arguments from partial DIEs. So, unfortunately, we have
3816 to go through the full DIEs. At least any work we do building
3817 types here will be reused if full symbols are loaded later. */
3818 if (pdi->has_template_arguments)
3819 {
3820 fixup_partial_die (pdi, cu);
3821
3822 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3823 {
3824 struct die_info *die;
3825 struct attribute attr;
3826 struct dwarf2_cu *ref_cu = cu;
3827
3828 attr.name = 0;
3829 attr.form = DW_FORM_ref_addr;
3830 attr.u.addr = pdi->offset;
3831 die = follow_die_ref (NULL, &attr, &ref_cu);
3832
3833 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3834 }
3835 }
3836
3837 parent_scope = partial_die_parent_scope (pdi, cu);
3838 if (parent_scope == NULL)
3839 return NULL;
3840 else
3841 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3842 }
3843
3844 static void
3845 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3846 {
3847 struct objfile *objfile = cu->objfile;
3848 CORE_ADDR addr = 0;
3849 char *actual_name = NULL;
3850 const struct partial_symbol *psym = NULL;
3851 CORE_ADDR baseaddr;
3852 int built_actual_name = 0;
3853
3854 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3855
3856 actual_name = partial_die_full_name (pdi, cu);
3857 if (actual_name)
3858 built_actual_name = 1;
3859
3860 if (actual_name == NULL)
3861 actual_name = pdi->name;
3862
3863 switch (pdi->tag)
3864 {
3865 case DW_TAG_subprogram:
3866 if (pdi->is_external || cu->language == language_ada)
3867 {
3868 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3869 of the global scope. But in Ada, we want to be able to access
3870 nested procedures globally. So all Ada subprograms are stored
3871 in the global scope. */
3872 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3873 mst_text, objfile); */
3874 add_psymbol_to_list (actual_name, strlen (actual_name),
3875 built_actual_name,
3876 VAR_DOMAIN, LOC_BLOCK,
3877 &objfile->global_psymbols,
3878 0, pdi->lowpc + baseaddr,
3879 cu->language, objfile);
3880 }
3881 else
3882 {
3883 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3884 mst_file_text, objfile); */
3885 add_psymbol_to_list (actual_name, strlen (actual_name),
3886 built_actual_name,
3887 VAR_DOMAIN, LOC_BLOCK,
3888 &objfile->static_psymbols,
3889 0, pdi->lowpc + baseaddr,
3890 cu->language, objfile);
3891 }
3892 break;
3893 case DW_TAG_constant:
3894 {
3895 struct psymbol_allocation_list *list;
3896
3897 if (pdi->is_external)
3898 list = &objfile->global_psymbols;
3899 else
3900 list = &objfile->static_psymbols;
3901 add_psymbol_to_list (actual_name, strlen (actual_name),
3902 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3903 list, 0, 0, cu->language, objfile);
3904 }
3905 break;
3906 case DW_TAG_variable:
3907 if (pdi->locdesc)
3908 addr = decode_locdesc (pdi->locdesc, cu);
3909
3910 if (pdi->locdesc
3911 && addr == 0
3912 && !dwarf2_per_objfile->has_section_at_zero)
3913 {
3914 /* A global or static variable may also have been stripped
3915 out by the linker if unused, in which case its address
3916 will be nullified; do not add such variables into partial
3917 symbol table then. */
3918 }
3919 else if (pdi->is_external)
3920 {
3921 /* Global Variable.
3922 Don't enter into the minimal symbol tables as there is
3923 a minimal symbol table entry from the ELF symbols already.
3924 Enter into partial symbol table if it has a location
3925 descriptor or a type.
3926 If the location descriptor is missing, new_symbol will create
3927 a LOC_UNRESOLVED symbol, the address of the variable will then
3928 be determined from the minimal symbol table whenever the variable
3929 is referenced.
3930 The address for the partial symbol table entry is not
3931 used by GDB, but it comes in handy for debugging partial symbol
3932 table building. */
3933
3934 if (pdi->locdesc || pdi->has_type)
3935 add_psymbol_to_list (actual_name, strlen (actual_name),
3936 built_actual_name,
3937 VAR_DOMAIN, LOC_STATIC,
3938 &objfile->global_psymbols,
3939 0, addr + baseaddr,
3940 cu->language, objfile);
3941 }
3942 else
3943 {
3944 /* Static Variable. Skip symbols without location descriptors. */
3945 if (pdi->locdesc == NULL)
3946 {
3947 if (built_actual_name)
3948 xfree (actual_name);
3949 return;
3950 }
3951 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3952 mst_file_data, objfile); */
3953 add_psymbol_to_list (actual_name, strlen (actual_name),
3954 built_actual_name,
3955 VAR_DOMAIN, LOC_STATIC,
3956 &objfile->static_psymbols,
3957 0, addr + baseaddr,
3958 cu->language, objfile);
3959 }
3960 break;
3961 case DW_TAG_typedef:
3962 case DW_TAG_base_type:
3963 case DW_TAG_subrange_type:
3964 add_psymbol_to_list (actual_name, strlen (actual_name),
3965 built_actual_name,
3966 VAR_DOMAIN, LOC_TYPEDEF,
3967 &objfile->static_psymbols,
3968 0, (CORE_ADDR) 0, cu->language, objfile);
3969 break;
3970 case DW_TAG_namespace:
3971 add_psymbol_to_list (actual_name, strlen (actual_name),
3972 built_actual_name,
3973 VAR_DOMAIN, LOC_TYPEDEF,
3974 &objfile->global_psymbols,
3975 0, (CORE_ADDR) 0, cu->language, objfile);
3976 break;
3977 case DW_TAG_class_type:
3978 case DW_TAG_interface_type:
3979 case DW_TAG_structure_type:
3980 case DW_TAG_union_type:
3981 case DW_TAG_enumeration_type:
3982 /* Skip external references. The DWARF standard says in the section
3983 about "Structure, Union, and Class Type Entries": "An incomplete
3984 structure, union or class type is represented by a structure,
3985 union or class entry that does not have a byte size attribute
3986 and that has a DW_AT_declaration attribute." */
3987 if (!pdi->has_byte_size && pdi->is_declaration)
3988 {
3989 if (built_actual_name)
3990 xfree (actual_name);
3991 return;
3992 }
3993
3994 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3995 static vs. global. */
3996 add_psymbol_to_list (actual_name, strlen (actual_name),
3997 built_actual_name,
3998 STRUCT_DOMAIN, LOC_TYPEDEF,
3999 (cu->language == language_cplus
4000 || cu->language == language_java)
4001 ? &objfile->global_psymbols
4002 : &objfile->static_psymbols,
4003 0, (CORE_ADDR) 0, cu->language, objfile);
4004
4005 break;
4006 case DW_TAG_enumerator:
4007 add_psymbol_to_list (actual_name, strlen (actual_name),
4008 built_actual_name,
4009 VAR_DOMAIN, LOC_CONST,
4010 (cu->language == language_cplus
4011 || cu->language == language_java)
4012 ? &objfile->global_psymbols
4013 : &objfile->static_psymbols,
4014 0, (CORE_ADDR) 0, cu->language, objfile);
4015 break;
4016 default:
4017 break;
4018 }
4019
4020 if (built_actual_name)
4021 xfree (actual_name);
4022 }
4023
4024 /* Read a partial die corresponding to a namespace; also, add a symbol
4025 corresponding to that namespace to the symbol table. NAMESPACE is
4026 the name of the enclosing namespace. */
4027
4028 static void
4029 add_partial_namespace (struct partial_die_info *pdi,
4030 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4031 int need_pc, struct dwarf2_cu *cu)
4032 {
4033 /* Add a symbol for the namespace. */
4034
4035 add_partial_symbol (pdi, cu);
4036
4037 /* Now scan partial symbols in that namespace. */
4038
4039 if (pdi->has_children)
4040 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4041 }
4042
4043 /* Read a partial die corresponding to a Fortran module. */
4044
4045 static void
4046 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4047 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4048 {
4049 /* Now scan partial symbols in that module. */
4050
4051 if (pdi->has_children)
4052 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4053 }
4054
4055 /* Read a partial die corresponding to a subprogram and create a partial
4056 symbol for that subprogram. When the CU language allows it, this
4057 routine also defines a partial symbol for each nested subprogram
4058 that this subprogram contains.
4059
4060 DIE my also be a lexical block, in which case we simply search
4061 recursively for suprograms defined inside that lexical block.
4062 Again, this is only performed when the CU language allows this
4063 type of definitions. */
4064
4065 static void
4066 add_partial_subprogram (struct partial_die_info *pdi,
4067 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4068 int need_pc, struct dwarf2_cu *cu)
4069 {
4070 if (pdi->tag == DW_TAG_subprogram)
4071 {
4072 if (pdi->has_pc_info)
4073 {
4074 if (pdi->lowpc < *lowpc)
4075 *lowpc = pdi->lowpc;
4076 if (pdi->highpc > *highpc)
4077 *highpc = pdi->highpc;
4078 if (need_pc)
4079 {
4080 CORE_ADDR baseaddr;
4081 struct objfile *objfile = cu->objfile;
4082
4083 baseaddr = ANOFFSET (objfile->section_offsets,
4084 SECT_OFF_TEXT (objfile));
4085 addrmap_set_empty (objfile->psymtabs_addrmap,
4086 pdi->lowpc + baseaddr,
4087 pdi->highpc - 1 + baseaddr,
4088 cu->per_cu->v.psymtab);
4089 }
4090 if (!pdi->is_declaration)
4091 /* Ignore subprogram DIEs that do not have a name, they are
4092 illegal. Do not emit a complaint at this point, we will
4093 do so when we convert this psymtab into a symtab. */
4094 if (pdi->name)
4095 add_partial_symbol (pdi, cu);
4096 }
4097 }
4098
4099 if (! pdi->has_children)
4100 return;
4101
4102 if (cu->language == language_ada)
4103 {
4104 pdi = pdi->die_child;
4105 while (pdi != NULL)
4106 {
4107 fixup_partial_die (pdi, cu);
4108 if (pdi->tag == DW_TAG_subprogram
4109 || pdi->tag == DW_TAG_lexical_block)
4110 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4111 pdi = pdi->die_sibling;
4112 }
4113 }
4114 }
4115
4116 /* Read a partial die corresponding to an enumeration type. */
4117
4118 static void
4119 add_partial_enumeration (struct partial_die_info *enum_pdi,
4120 struct dwarf2_cu *cu)
4121 {
4122 struct partial_die_info *pdi;
4123
4124 if (enum_pdi->name != NULL)
4125 add_partial_symbol (enum_pdi, cu);
4126
4127 pdi = enum_pdi->die_child;
4128 while (pdi)
4129 {
4130 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4131 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4132 else
4133 add_partial_symbol (pdi, cu);
4134 pdi = pdi->die_sibling;
4135 }
4136 }
4137
4138 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4139 Return the corresponding abbrev, or NULL if the number is zero (indicating
4140 an empty DIE). In either case *BYTES_READ will be set to the length of
4141 the initial number. */
4142
4143 static struct abbrev_info *
4144 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4145 struct dwarf2_cu *cu)
4146 {
4147 bfd *abfd = cu->objfile->obfd;
4148 unsigned int abbrev_number;
4149 struct abbrev_info *abbrev;
4150
4151 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4152
4153 if (abbrev_number == 0)
4154 return NULL;
4155
4156 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4157 if (!abbrev)
4158 {
4159 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4160 abbrev_number, bfd_get_filename (abfd));
4161 }
4162
4163 return abbrev;
4164 }
4165
4166 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4167 Returns a pointer to the end of a series of DIEs, terminated by an empty
4168 DIE. Any children of the skipped DIEs will also be skipped. */
4169
4170 static gdb_byte *
4171 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4172 {
4173 struct abbrev_info *abbrev;
4174 unsigned int bytes_read;
4175
4176 while (1)
4177 {
4178 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4179 if (abbrev == NULL)
4180 return info_ptr + bytes_read;
4181 else
4182 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4183 }
4184 }
4185
4186 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4187 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4188 abbrev corresponding to that skipped uleb128 should be passed in
4189 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4190 children. */
4191
4192 static gdb_byte *
4193 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4194 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4195 {
4196 unsigned int bytes_read;
4197 struct attribute attr;
4198 bfd *abfd = cu->objfile->obfd;
4199 unsigned int form, i;
4200
4201 for (i = 0; i < abbrev->num_attrs; i++)
4202 {
4203 /* The only abbrev we care about is DW_AT_sibling. */
4204 if (abbrev->attrs[i].name == DW_AT_sibling)
4205 {
4206 read_attribute (&attr, &abbrev->attrs[i],
4207 abfd, info_ptr, cu);
4208 if (attr.form == DW_FORM_ref_addr)
4209 complaint (&symfile_complaints,
4210 _("ignoring absolute DW_AT_sibling"));
4211 else
4212 return buffer + dwarf2_get_ref_die_offset (&attr);
4213 }
4214
4215 /* If it isn't DW_AT_sibling, skip this attribute. */
4216 form = abbrev->attrs[i].form;
4217 skip_attribute:
4218 switch (form)
4219 {
4220 case DW_FORM_ref_addr:
4221 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4222 and later it is offset sized. */
4223 if (cu->header.version == 2)
4224 info_ptr += cu->header.addr_size;
4225 else
4226 info_ptr += cu->header.offset_size;
4227 break;
4228 case DW_FORM_addr:
4229 info_ptr += cu->header.addr_size;
4230 break;
4231 case DW_FORM_data1:
4232 case DW_FORM_ref1:
4233 case DW_FORM_flag:
4234 info_ptr += 1;
4235 break;
4236 case DW_FORM_flag_present:
4237 break;
4238 case DW_FORM_data2:
4239 case DW_FORM_ref2:
4240 info_ptr += 2;
4241 break;
4242 case DW_FORM_data4:
4243 case DW_FORM_ref4:
4244 info_ptr += 4;
4245 break;
4246 case DW_FORM_data8:
4247 case DW_FORM_ref8:
4248 case DW_FORM_ref_sig8:
4249 info_ptr += 8;
4250 break;
4251 case DW_FORM_string:
4252 read_direct_string (abfd, info_ptr, &bytes_read);
4253 info_ptr += bytes_read;
4254 break;
4255 case DW_FORM_sec_offset:
4256 case DW_FORM_strp:
4257 info_ptr += cu->header.offset_size;
4258 break;
4259 case DW_FORM_exprloc:
4260 case DW_FORM_block:
4261 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4262 info_ptr += bytes_read;
4263 break;
4264 case DW_FORM_block1:
4265 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4266 break;
4267 case DW_FORM_block2:
4268 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4269 break;
4270 case DW_FORM_block4:
4271 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4272 break;
4273 case DW_FORM_sdata:
4274 case DW_FORM_udata:
4275 case DW_FORM_ref_udata:
4276 info_ptr = skip_leb128 (abfd, info_ptr);
4277 break;
4278 case DW_FORM_indirect:
4279 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4280 info_ptr += bytes_read;
4281 /* We need to continue parsing from here, so just go back to
4282 the top. */
4283 goto skip_attribute;
4284
4285 default:
4286 error (_("Dwarf Error: Cannot handle %s "
4287 "in DWARF reader [in module %s]"),
4288 dwarf_form_name (form),
4289 bfd_get_filename (abfd));
4290 }
4291 }
4292
4293 if (abbrev->has_children)
4294 return skip_children (buffer, info_ptr, cu);
4295 else
4296 return info_ptr;
4297 }
4298
4299 /* Locate ORIG_PDI's sibling.
4300 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4301 in BUFFER. */
4302
4303 static gdb_byte *
4304 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4305 gdb_byte *buffer, gdb_byte *info_ptr,
4306 bfd *abfd, struct dwarf2_cu *cu)
4307 {
4308 /* Do we know the sibling already? */
4309
4310 if (orig_pdi->sibling)
4311 return orig_pdi->sibling;
4312
4313 /* Are there any children to deal with? */
4314
4315 if (!orig_pdi->has_children)
4316 return info_ptr;
4317
4318 /* Skip the children the long way. */
4319
4320 return skip_children (buffer, info_ptr, cu);
4321 }
4322
4323 /* Expand this partial symbol table into a full symbol table. */
4324
4325 static void
4326 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4327 {
4328 if (pst != NULL)
4329 {
4330 if (pst->readin)
4331 {
4332 warning (_("bug: psymtab for %s is already read in."),
4333 pst->filename);
4334 }
4335 else
4336 {
4337 if (info_verbose)
4338 {
4339 printf_filtered (_("Reading in symbols for %s..."),
4340 pst->filename);
4341 gdb_flush (gdb_stdout);
4342 }
4343
4344 /* Restore our global data. */
4345 dwarf2_per_objfile = objfile_data (pst->objfile,
4346 dwarf2_objfile_data_key);
4347
4348 /* If this psymtab is constructed from a debug-only objfile, the
4349 has_section_at_zero flag will not necessarily be correct. We
4350 can get the correct value for this flag by looking at the data
4351 associated with the (presumably stripped) associated objfile. */
4352 if (pst->objfile->separate_debug_objfile_backlink)
4353 {
4354 struct dwarf2_per_objfile *dpo_backlink
4355 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4356 dwarf2_objfile_data_key);
4357
4358 dwarf2_per_objfile->has_section_at_zero
4359 = dpo_backlink->has_section_at_zero;
4360 }
4361
4362 dwarf2_per_objfile->reading_partial_symbols = 0;
4363
4364 psymtab_to_symtab_1 (pst);
4365
4366 /* Finish up the debug error message. */
4367 if (info_verbose)
4368 printf_filtered (_("done.\n"));
4369 }
4370 }
4371 }
4372
4373 /* Add PER_CU to the queue. */
4374
4375 static void
4376 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4377 {
4378 struct dwarf2_queue_item *item;
4379
4380 per_cu->queued = 1;
4381 item = xmalloc (sizeof (*item));
4382 item->per_cu = per_cu;
4383 item->next = NULL;
4384
4385 if (dwarf2_queue == NULL)
4386 dwarf2_queue = item;
4387 else
4388 dwarf2_queue_tail->next = item;
4389
4390 dwarf2_queue_tail = item;
4391 }
4392
4393 /* Process the queue. */
4394
4395 static void
4396 process_queue (struct objfile *objfile)
4397 {
4398 struct dwarf2_queue_item *item, *next_item;
4399
4400 /* The queue starts out with one item, but following a DIE reference
4401 may load a new CU, adding it to the end of the queue. */
4402 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4403 {
4404 if (dwarf2_per_objfile->using_index
4405 ? !item->per_cu->v.quick->symtab
4406 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4407 process_full_comp_unit (item->per_cu);
4408
4409 item->per_cu->queued = 0;
4410 next_item = item->next;
4411 xfree (item);
4412 }
4413
4414 dwarf2_queue_tail = NULL;
4415 }
4416
4417 /* Free all allocated queue entries. This function only releases anything if
4418 an error was thrown; if the queue was processed then it would have been
4419 freed as we went along. */
4420
4421 static void
4422 dwarf2_release_queue (void *dummy)
4423 {
4424 struct dwarf2_queue_item *item, *last;
4425
4426 item = dwarf2_queue;
4427 while (item)
4428 {
4429 /* Anything still marked queued is likely to be in an
4430 inconsistent state, so discard it. */
4431 if (item->per_cu->queued)
4432 {
4433 if (item->per_cu->cu != NULL)
4434 free_one_cached_comp_unit (item->per_cu->cu);
4435 item->per_cu->queued = 0;
4436 }
4437
4438 last = item;
4439 item = item->next;
4440 xfree (last);
4441 }
4442
4443 dwarf2_queue = dwarf2_queue_tail = NULL;
4444 }
4445
4446 /* Read in full symbols for PST, and anything it depends on. */
4447
4448 static void
4449 psymtab_to_symtab_1 (struct partial_symtab *pst)
4450 {
4451 struct dwarf2_per_cu_data *per_cu;
4452 struct cleanup *back_to;
4453 int i;
4454
4455 for (i = 0; i < pst->number_of_dependencies; i++)
4456 if (!pst->dependencies[i]->readin)
4457 {
4458 /* Inform about additional files that need to be read in. */
4459 if (info_verbose)
4460 {
4461 /* FIXME: i18n: Need to make this a single string. */
4462 fputs_filtered (" ", gdb_stdout);
4463 wrap_here ("");
4464 fputs_filtered ("and ", gdb_stdout);
4465 wrap_here ("");
4466 printf_filtered ("%s...", pst->dependencies[i]->filename);
4467 wrap_here (""); /* Flush output. */
4468 gdb_flush (gdb_stdout);
4469 }
4470 psymtab_to_symtab_1 (pst->dependencies[i]);
4471 }
4472
4473 per_cu = pst->read_symtab_private;
4474
4475 if (per_cu == NULL)
4476 {
4477 /* It's an include file, no symbols to read for it.
4478 Everything is in the parent symtab. */
4479 pst->readin = 1;
4480 return;
4481 }
4482
4483 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4484 }
4485
4486 /* Load the DIEs associated with PER_CU into memory. */
4487
4488 static void
4489 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4490 struct objfile *objfile)
4491 {
4492 bfd *abfd = objfile->obfd;
4493 struct dwarf2_cu *cu;
4494 unsigned int offset;
4495 gdb_byte *info_ptr, *beg_of_comp_unit;
4496 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4497 struct attribute *attr;
4498 int read_cu = 0;
4499
4500 gdb_assert (! per_cu->from_debug_types);
4501
4502 /* Set local variables from the partial symbol table info. */
4503 offset = per_cu->offset;
4504
4505 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4506 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4507 beg_of_comp_unit = info_ptr;
4508
4509 if (per_cu->cu == NULL)
4510 {
4511 cu = xmalloc (sizeof (*cu));
4512 init_one_comp_unit (cu, objfile);
4513
4514 read_cu = 1;
4515
4516 /* If an error occurs while loading, release our storage. */
4517 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4518
4519 /* Read in the comp_unit header. */
4520 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4521
4522 /* Complete the cu_header. */
4523 cu->header.offset = offset;
4524 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4525
4526 /* Read the abbrevs for this compilation unit. */
4527 dwarf2_read_abbrevs (abfd, cu);
4528 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4529
4530 /* Link this compilation unit into the compilation unit tree. */
4531 per_cu->cu = cu;
4532 cu->per_cu = per_cu;
4533
4534 /* Link this CU into read_in_chain. */
4535 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4536 dwarf2_per_objfile->read_in_chain = per_cu;
4537 }
4538 else
4539 {
4540 cu = per_cu->cu;
4541 info_ptr += cu->header.first_die_offset;
4542 }
4543
4544 cu->dies = read_comp_unit (info_ptr, cu);
4545
4546 /* We try not to read any attributes in this function, because not
4547 all objfiles needed for references have been loaded yet, and symbol
4548 table processing isn't initialized. But we have to set the CU language,
4549 or we won't be able to build types correctly. */
4550 prepare_one_comp_unit (cu, cu->dies);
4551
4552 /* Similarly, if we do not read the producer, we can not apply
4553 producer-specific interpretation. */
4554 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4555 if (attr)
4556 cu->producer = DW_STRING (attr);
4557
4558 if (read_cu)
4559 {
4560 do_cleanups (free_abbrevs_cleanup);
4561
4562 /* We've successfully allocated this compilation unit. Let our
4563 caller clean it up when finished with it. */
4564 discard_cleanups (free_cu_cleanup);
4565 }
4566 }
4567
4568 /* Add a DIE to the delayed physname list. */
4569
4570 static void
4571 add_to_method_list (struct type *type, int fnfield_index, int index,
4572 const char *name, struct die_info *die,
4573 struct dwarf2_cu *cu)
4574 {
4575 struct delayed_method_info mi;
4576 mi.type = type;
4577 mi.fnfield_index = fnfield_index;
4578 mi.index = index;
4579 mi.name = name;
4580 mi.die = die;
4581 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4582 }
4583
4584 /* A cleanup for freeing the delayed method list. */
4585
4586 static void
4587 free_delayed_list (void *ptr)
4588 {
4589 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4590 if (cu->method_list != NULL)
4591 {
4592 VEC_free (delayed_method_info, cu->method_list);
4593 cu->method_list = NULL;
4594 }
4595 }
4596
4597 /* Compute the physnames of any methods on the CU's method list.
4598
4599 The computation of method physnames is delayed in order to avoid the
4600 (bad) condition that one of the method's formal parameters is of an as yet
4601 incomplete type. */
4602
4603 static void
4604 compute_delayed_physnames (struct dwarf2_cu *cu)
4605 {
4606 int i;
4607 struct delayed_method_info *mi;
4608 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4609 {
4610 char *physname;
4611 struct fn_fieldlist *fn_flp
4612 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4613 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4614 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4615 }
4616 }
4617
4618 /* Generate full symbol information for PST and CU, whose DIEs have
4619 already been loaded into memory. */
4620
4621 static void
4622 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4623 {
4624 struct dwarf2_cu *cu = per_cu->cu;
4625 struct objfile *objfile = per_cu->objfile;
4626 CORE_ADDR lowpc, highpc;
4627 struct symtab *symtab;
4628 struct cleanup *back_to, *delayed_list_cleanup;
4629 CORE_ADDR baseaddr;
4630
4631 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4632
4633 buildsym_init ();
4634 back_to = make_cleanup (really_free_pendings, NULL);
4635 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4636
4637 cu->list_in_scope = &file_symbols;
4638
4639 dwarf2_find_base_address (cu->dies, cu);
4640
4641 /* Do line number decoding in read_file_scope () */
4642 process_die (cu->dies, cu);
4643
4644 /* Now that we have processed all the DIEs in the CU, all the types
4645 should be complete, and it should now be safe to compute all of the
4646 physnames. */
4647 compute_delayed_physnames (cu);
4648 do_cleanups (delayed_list_cleanup);
4649
4650 /* Some compilers don't define a DW_AT_high_pc attribute for the
4651 compilation unit. If the DW_AT_high_pc is missing, synthesize
4652 it, by scanning the DIE's below the compilation unit. */
4653 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4654
4655 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4656
4657 /* Set symtab language to language from DW_AT_language.
4658 If the compilation is from a C file generated by language preprocessors,
4659 do not set the language if it was already deduced by start_subfile. */
4660 if (symtab != NULL
4661 && !(cu->language == language_c && symtab->language != language_c))
4662 {
4663 symtab->language = cu->language;
4664 }
4665
4666 if (dwarf2_per_objfile->using_index)
4667 per_cu->v.quick->symtab = symtab;
4668 else
4669 {
4670 struct partial_symtab *pst = per_cu->v.psymtab;
4671 pst->symtab = symtab;
4672 pst->readin = 1;
4673 }
4674
4675 do_cleanups (back_to);
4676 }
4677
4678 /* Process a die and its children. */
4679
4680 static void
4681 process_die (struct die_info *die, struct dwarf2_cu *cu)
4682 {
4683 switch (die->tag)
4684 {
4685 case DW_TAG_padding:
4686 break;
4687 case DW_TAG_compile_unit:
4688 read_file_scope (die, cu);
4689 break;
4690 case DW_TAG_type_unit:
4691 read_type_unit_scope (die, cu);
4692 break;
4693 case DW_TAG_subprogram:
4694 case DW_TAG_inlined_subroutine:
4695 read_func_scope (die, cu);
4696 break;
4697 case DW_TAG_lexical_block:
4698 case DW_TAG_try_block:
4699 case DW_TAG_catch_block:
4700 read_lexical_block_scope (die, cu);
4701 break;
4702 case DW_TAG_class_type:
4703 case DW_TAG_interface_type:
4704 case DW_TAG_structure_type:
4705 case DW_TAG_union_type:
4706 process_structure_scope (die, cu);
4707 break;
4708 case DW_TAG_enumeration_type:
4709 process_enumeration_scope (die, cu);
4710 break;
4711
4712 /* These dies have a type, but processing them does not create
4713 a symbol or recurse to process the children. Therefore we can
4714 read them on-demand through read_type_die. */
4715 case DW_TAG_subroutine_type:
4716 case DW_TAG_set_type:
4717 case DW_TAG_array_type:
4718 case DW_TAG_pointer_type:
4719 case DW_TAG_ptr_to_member_type:
4720 case DW_TAG_reference_type:
4721 case DW_TAG_string_type:
4722 break;
4723
4724 case DW_TAG_base_type:
4725 case DW_TAG_subrange_type:
4726 case DW_TAG_typedef:
4727 /* Add a typedef symbol for the type definition, if it has a
4728 DW_AT_name. */
4729 new_symbol (die, read_type_die (die, cu), cu);
4730 break;
4731 case DW_TAG_common_block:
4732 read_common_block (die, cu);
4733 break;
4734 case DW_TAG_common_inclusion:
4735 break;
4736 case DW_TAG_namespace:
4737 processing_has_namespace_info = 1;
4738 read_namespace (die, cu);
4739 break;
4740 case DW_TAG_module:
4741 processing_has_namespace_info = 1;
4742 read_module (die, cu);
4743 break;
4744 case DW_TAG_imported_declaration:
4745 case DW_TAG_imported_module:
4746 processing_has_namespace_info = 1;
4747 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4748 || cu->language != language_fortran))
4749 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4750 dwarf_tag_name (die->tag));
4751 read_import_statement (die, cu);
4752 break;
4753 default:
4754 new_symbol (die, NULL, cu);
4755 break;
4756 }
4757 }
4758
4759 /* A helper function for dwarf2_compute_name which determines whether DIE
4760 needs to have the name of the scope prepended to the name listed in the
4761 die. */
4762
4763 static int
4764 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4765 {
4766 struct attribute *attr;
4767
4768 switch (die->tag)
4769 {
4770 case DW_TAG_namespace:
4771 case DW_TAG_typedef:
4772 case DW_TAG_class_type:
4773 case DW_TAG_interface_type:
4774 case DW_TAG_structure_type:
4775 case DW_TAG_union_type:
4776 case DW_TAG_enumeration_type:
4777 case DW_TAG_enumerator:
4778 case DW_TAG_subprogram:
4779 case DW_TAG_member:
4780 return 1;
4781
4782 case DW_TAG_variable:
4783 case DW_TAG_constant:
4784 /* We only need to prefix "globally" visible variables. These include
4785 any variable marked with DW_AT_external or any variable that
4786 lives in a namespace. [Variables in anonymous namespaces
4787 require prefixing, but they are not DW_AT_external.] */
4788
4789 if (dwarf2_attr (die, DW_AT_specification, cu))
4790 {
4791 struct dwarf2_cu *spec_cu = cu;
4792
4793 return die_needs_namespace (die_specification (die, &spec_cu),
4794 spec_cu);
4795 }
4796
4797 attr = dwarf2_attr (die, DW_AT_external, cu);
4798 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4799 && die->parent->tag != DW_TAG_module)
4800 return 0;
4801 /* A variable in a lexical block of some kind does not need a
4802 namespace, even though in C++ such variables may be external
4803 and have a mangled name. */
4804 if (die->parent->tag == DW_TAG_lexical_block
4805 || die->parent->tag == DW_TAG_try_block
4806 || die->parent->tag == DW_TAG_catch_block
4807 || die->parent->tag == DW_TAG_subprogram)
4808 return 0;
4809 return 1;
4810
4811 default:
4812 return 0;
4813 }
4814 }
4815
4816 /* Retrieve the last character from a mem_file. */
4817
4818 static void
4819 do_ui_file_peek_last (void *object, const char *buffer, long length)
4820 {
4821 char *last_char_p = (char *) object;
4822
4823 if (length > 0)
4824 *last_char_p = buffer[length - 1];
4825 }
4826
4827 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4828 compute the physname for the object, which include a method's
4829 formal parameters (C++/Java) and return type (Java).
4830
4831 For Ada, return the DIE's linkage name rather than the fully qualified
4832 name. PHYSNAME is ignored..
4833
4834 The result is allocated on the objfile_obstack and canonicalized. */
4835
4836 static const char *
4837 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4838 int physname)
4839 {
4840 if (name == NULL)
4841 name = dwarf2_name (die, cu);
4842
4843 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4844 compute it by typename_concat inside GDB. */
4845 if (cu->language == language_ada
4846 || (cu->language == language_fortran && physname))
4847 {
4848 /* For Ada unit, we prefer the linkage name over the name, as
4849 the former contains the exported name, which the user expects
4850 to be able to reference. Ideally, we want the user to be able
4851 to reference this entity using either natural or linkage name,
4852 but we haven't started looking at this enhancement yet. */
4853 struct attribute *attr;
4854
4855 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4856 if (attr == NULL)
4857 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4858 if (attr && DW_STRING (attr))
4859 return DW_STRING (attr);
4860 }
4861
4862 /* These are the only languages we know how to qualify names in. */
4863 if (name != NULL
4864 && (cu->language == language_cplus || cu->language == language_java
4865 || cu->language == language_fortran))
4866 {
4867 if (die_needs_namespace (die, cu))
4868 {
4869 long length;
4870 char *prefix;
4871 struct ui_file *buf;
4872
4873 prefix = determine_prefix (die, cu);
4874 buf = mem_fileopen ();
4875 if (*prefix != '\0')
4876 {
4877 char *prefixed_name = typename_concat (NULL, prefix, name,
4878 physname, cu);
4879
4880 fputs_unfiltered (prefixed_name, buf);
4881 xfree (prefixed_name);
4882 }
4883 else
4884 fputs_unfiltered (name, buf);
4885
4886 /* Template parameters may be specified in the DIE's DW_AT_name, or
4887 as children with DW_TAG_template_type_param or
4888 DW_TAG_value_type_param. If the latter, add them to the name
4889 here. If the name already has template parameters, then
4890 skip this step; some versions of GCC emit both, and
4891 it is more efficient to use the pre-computed name.
4892
4893 Something to keep in mind about this process: it is very
4894 unlikely, or in some cases downright impossible, to produce
4895 something that will match the mangled name of a function.
4896 If the definition of the function has the same debug info,
4897 we should be able to match up with it anyway. But fallbacks
4898 using the minimal symbol, for instance to find a method
4899 implemented in a stripped copy of libstdc++, will not work.
4900 If we do not have debug info for the definition, we will have to
4901 match them up some other way.
4902
4903 When we do name matching there is a related problem with function
4904 templates; two instantiated function templates are allowed to
4905 differ only by their return types, which we do not add here. */
4906
4907 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4908 {
4909 struct attribute *attr;
4910 struct die_info *child;
4911 int first = 1;
4912
4913 die->building_fullname = 1;
4914
4915 for (child = die->child; child != NULL; child = child->sibling)
4916 {
4917 struct type *type;
4918 long value;
4919 gdb_byte *bytes;
4920 struct dwarf2_locexpr_baton *baton;
4921 struct value *v;
4922
4923 if (child->tag != DW_TAG_template_type_param
4924 && child->tag != DW_TAG_template_value_param)
4925 continue;
4926
4927 if (first)
4928 {
4929 fputs_unfiltered ("<", buf);
4930 first = 0;
4931 }
4932 else
4933 fputs_unfiltered (", ", buf);
4934
4935 attr = dwarf2_attr (child, DW_AT_type, cu);
4936 if (attr == NULL)
4937 {
4938 complaint (&symfile_complaints,
4939 _("template parameter missing DW_AT_type"));
4940 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4941 continue;
4942 }
4943 type = die_type (child, cu);
4944
4945 if (child->tag == DW_TAG_template_type_param)
4946 {
4947 c_print_type (type, "", buf, -1, 0);
4948 continue;
4949 }
4950
4951 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4952 if (attr == NULL)
4953 {
4954 complaint (&symfile_complaints,
4955 _("template parameter missing "
4956 "DW_AT_const_value"));
4957 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4958 continue;
4959 }
4960
4961 dwarf2_const_value_attr (attr, type, name,
4962 &cu->comp_unit_obstack, cu,
4963 &value, &bytes, &baton);
4964
4965 if (TYPE_NOSIGN (type))
4966 /* GDB prints characters as NUMBER 'CHAR'. If that's
4967 changed, this can use value_print instead. */
4968 c_printchar (value, type, buf);
4969 else
4970 {
4971 struct value_print_options opts;
4972
4973 if (baton != NULL)
4974 v = dwarf2_evaluate_loc_desc (type, NULL,
4975 baton->data,
4976 baton->size,
4977 baton->per_cu);
4978 else if (bytes != NULL)
4979 {
4980 v = allocate_value (type);
4981 memcpy (value_contents_writeable (v), bytes,
4982 TYPE_LENGTH (type));
4983 }
4984 else
4985 v = value_from_longest (type, value);
4986
4987 /* Specify decimal so that we do not depend on
4988 the radix. */
4989 get_formatted_print_options (&opts, 'd');
4990 opts.raw = 1;
4991 value_print (v, buf, &opts);
4992 release_value (v);
4993 value_free (v);
4994 }
4995 }
4996
4997 die->building_fullname = 0;
4998
4999 if (!first)
5000 {
5001 /* Close the argument list, with a space if necessary
5002 (nested templates). */
5003 char last_char = '\0';
5004 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5005 if (last_char == '>')
5006 fputs_unfiltered (" >", buf);
5007 else
5008 fputs_unfiltered (">", buf);
5009 }
5010 }
5011
5012 /* For Java and C++ methods, append formal parameter type
5013 information, if PHYSNAME. */
5014
5015 if (physname && die->tag == DW_TAG_subprogram
5016 && (cu->language == language_cplus
5017 || cu->language == language_java))
5018 {
5019 struct type *type = read_type_die (die, cu);
5020
5021 c_type_print_args (type, buf, 1, cu->language);
5022
5023 if (cu->language == language_java)
5024 {
5025 /* For java, we must append the return type to method
5026 names. */
5027 if (die->tag == DW_TAG_subprogram)
5028 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5029 0, 0);
5030 }
5031 else if (cu->language == language_cplus)
5032 {
5033 /* Assume that an artificial first parameter is
5034 "this", but do not crash if it is not. RealView
5035 marks unnamed (and thus unused) parameters as
5036 artificial; there is no way to differentiate
5037 the two cases. */
5038 if (TYPE_NFIELDS (type) > 0
5039 && TYPE_FIELD_ARTIFICIAL (type, 0)
5040 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5041 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5042 0))))
5043 fputs_unfiltered (" const", buf);
5044 }
5045 }
5046
5047 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5048 &length);
5049 ui_file_delete (buf);
5050
5051 if (cu->language == language_cplus)
5052 {
5053 char *cname
5054 = dwarf2_canonicalize_name (name, cu,
5055 &cu->objfile->objfile_obstack);
5056
5057 if (cname != NULL)
5058 name = cname;
5059 }
5060 }
5061 }
5062
5063 return name;
5064 }
5065
5066 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5067 If scope qualifiers are appropriate they will be added. The result
5068 will be allocated on the objfile_obstack, or NULL if the DIE does
5069 not have a name. NAME may either be from a previous call to
5070 dwarf2_name or NULL.
5071
5072 The output string will be canonicalized (if C++/Java). */
5073
5074 static const char *
5075 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5076 {
5077 return dwarf2_compute_name (name, die, cu, 0);
5078 }
5079
5080 /* Construct a physname for the given DIE in CU. NAME may either be
5081 from a previous call to dwarf2_name or NULL. The result will be
5082 allocated on the objfile_objstack or NULL if the DIE does not have a
5083 name.
5084
5085 The output string will be canonicalized (if C++/Java). */
5086
5087 static const char *
5088 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5089 {
5090 return dwarf2_compute_name (name, die, cu, 1);
5091 }
5092
5093 /* Read the import statement specified by the given die and record it. */
5094
5095 static void
5096 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5097 {
5098 struct attribute *import_attr;
5099 struct die_info *imported_die;
5100 struct dwarf2_cu *imported_cu;
5101 const char *imported_name;
5102 const char *imported_name_prefix;
5103 const char *canonical_name;
5104 const char *import_alias;
5105 const char *imported_declaration = NULL;
5106 const char *import_prefix;
5107
5108 char *temp;
5109
5110 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5111 if (import_attr == NULL)
5112 {
5113 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5114 dwarf_tag_name (die->tag));
5115 return;
5116 }
5117
5118 imported_cu = cu;
5119 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5120 imported_name = dwarf2_name (imported_die, imported_cu);
5121 if (imported_name == NULL)
5122 {
5123 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5124
5125 The import in the following code:
5126 namespace A
5127 {
5128 typedef int B;
5129 }
5130
5131 int main ()
5132 {
5133 using A::B;
5134 B b;
5135 return b;
5136 }
5137
5138 ...
5139 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5140 <52> DW_AT_decl_file : 1
5141 <53> DW_AT_decl_line : 6
5142 <54> DW_AT_import : <0x75>
5143 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5144 <59> DW_AT_name : B
5145 <5b> DW_AT_decl_file : 1
5146 <5c> DW_AT_decl_line : 2
5147 <5d> DW_AT_type : <0x6e>
5148 ...
5149 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5150 <76> DW_AT_byte_size : 4
5151 <77> DW_AT_encoding : 5 (signed)
5152
5153 imports the wrong die ( 0x75 instead of 0x58 ).
5154 This case will be ignored until the gcc bug is fixed. */
5155 return;
5156 }
5157
5158 /* Figure out the local name after import. */
5159 import_alias = dwarf2_name (die, cu);
5160
5161 /* Figure out where the statement is being imported to. */
5162 import_prefix = determine_prefix (die, cu);
5163
5164 /* Figure out what the scope of the imported die is and prepend it
5165 to the name of the imported die. */
5166 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5167
5168 if (imported_die->tag != DW_TAG_namespace
5169 && imported_die->tag != DW_TAG_module)
5170 {
5171 imported_declaration = imported_name;
5172 canonical_name = imported_name_prefix;
5173 }
5174 else if (strlen (imported_name_prefix) > 0)
5175 {
5176 temp = alloca (strlen (imported_name_prefix)
5177 + 2 + strlen (imported_name) + 1);
5178 strcpy (temp, imported_name_prefix);
5179 strcat (temp, "::");
5180 strcat (temp, imported_name);
5181 canonical_name = temp;
5182 }
5183 else
5184 canonical_name = imported_name;
5185
5186 cp_add_using_directive (import_prefix,
5187 canonical_name,
5188 import_alias,
5189 imported_declaration,
5190 &cu->objfile->objfile_obstack);
5191 }
5192
5193 static void
5194 initialize_cu_func_list (struct dwarf2_cu *cu)
5195 {
5196 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5197 }
5198
5199 /* Cleanup function for read_file_scope. */
5200
5201 static void
5202 free_cu_line_header (void *arg)
5203 {
5204 struct dwarf2_cu *cu = arg;
5205
5206 free_line_header (cu->line_header);
5207 cu->line_header = NULL;
5208 }
5209
5210 static void
5211 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5212 char **name, char **comp_dir)
5213 {
5214 struct attribute *attr;
5215
5216 *name = NULL;
5217 *comp_dir = NULL;
5218
5219 /* Find the filename. Do not use dwarf2_name here, since the filename
5220 is not a source language identifier. */
5221 attr = dwarf2_attr (die, DW_AT_name, cu);
5222 if (attr)
5223 {
5224 *name = DW_STRING (attr);
5225 }
5226
5227 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5228 if (attr)
5229 *comp_dir = DW_STRING (attr);
5230 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5231 {
5232 *comp_dir = ldirname (*name);
5233 if (*comp_dir != NULL)
5234 make_cleanup (xfree, *comp_dir);
5235 }
5236 if (*comp_dir != NULL)
5237 {
5238 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5239 directory, get rid of it. */
5240 char *cp = strchr (*comp_dir, ':');
5241
5242 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5243 *comp_dir = cp + 1;
5244 }
5245
5246 if (*name == NULL)
5247 *name = "<unknown>";
5248 }
5249
5250 /* Process DW_TAG_compile_unit. */
5251
5252 static void
5253 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5254 {
5255 struct objfile *objfile = cu->objfile;
5256 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5257 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5258 CORE_ADDR highpc = ((CORE_ADDR) 0);
5259 struct attribute *attr;
5260 char *name = NULL;
5261 char *comp_dir = NULL;
5262 struct die_info *child_die;
5263 bfd *abfd = objfile->obfd;
5264 struct line_header *line_header = 0;
5265 CORE_ADDR baseaddr;
5266
5267 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5268
5269 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5270
5271 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5272 from finish_block. */
5273 if (lowpc == ((CORE_ADDR) -1))
5274 lowpc = highpc;
5275 lowpc += baseaddr;
5276 highpc += baseaddr;
5277
5278 find_file_and_directory (die, cu, &name, &comp_dir);
5279
5280 attr = dwarf2_attr (die, DW_AT_language, cu);
5281 if (attr)
5282 {
5283 set_cu_language (DW_UNSND (attr), cu);
5284 }
5285
5286 attr = dwarf2_attr (die, DW_AT_producer, cu);
5287 if (attr)
5288 cu->producer = DW_STRING (attr);
5289
5290 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5291 standardised yet. As a workaround for the language detection we fall
5292 back to the DW_AT_producer string. */
5293 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5294 cu->language = language_opencl;
5295
5296 /* We assume that we're processing GCC output. */
5297 processing_gcc_compilation = 2;
5298
5299 processing_has_namespace_info = 0;
5300
5301 start_symtab (name, comp_dir, lowpc);
5302 record_debugformat ("DWARF 2");
5303 record_producer (cu->producer);
5304
5305 initialize_cu_func_list (cu);
5306
5307 /* Decode line number information if present. We do this before
5308 processing child DIEs, so that the line header table is available
5309 for DW_AT_decl_file. */
5310 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5311 if (attr)
5312 {
5313 unsigned int line_offset = DW_UNSND (attr);
5314 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5315 if (line_header)
5316 {
5317 cu->line_header = line_header;
5318 make_cleanup (free_cu_line_header, cu);
5319 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5320 }
5321 }
5322
5323 /* Process all dies in compilation unit. */
5324 if (die->child != NULL)
5325 {
5326 child_die = die->child;
5327 while (child_die && child_die->tag)
5328 {
5329 process_die (child_die, cu);
5330 child_die = sibling_die (child_die);
5331 }
5332 }
5333
5334 /* Decode macro information, if present. Dwarf 2 macro information
5335 refers to information in the line number info statement program
5336 header, so we can only read it if we've read the header
5337 successfully. */
5338 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5339 if (attr && line_header)
5340 {
5341 unsigned int macro_offset = DW_UNSND (attr);
5342
5343 dwarf_decode_macros (line_header, macro_offset,
5344 comp_dir, abfd, cu);
5345 }
5346 do_cleanups (back_to);
5347 }
5348
5349 /* Process DW_TAG_type_unit.
5350 For TUs we want to skip the first top level sibling if it's not the
5351 actual type being defined by this TU. In this case the first top
5352 level sibling is there to provide context only. */
5353
5354 static void
5355 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5356 {
5357 struct objfile *objfile = cu->objfile;
5358 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5359 CORE_ADDR lowpc;
5360 struct attribute *attr;
5361 char *name = NULL;
5362 char *comp_dir = NULL;
5363 struct die_info *child_die;
5364 bfd *abfd = objfile->obfd;
5365
5366 /* start_symtab needs a low pc, but we don't really have one.
5367 Do what read_file_scope would do in the absence of such info. */
5368 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5369
5370 /* Find the filename. Do not use dwarf2_name here, since the filename
5371 is not a source language identifier. */
5372 attr = dwarf2_attr (die, DW_AT_name, cu);
5373 if (attr)
5374 name = DW_STRING (attr);
5375
5376 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5377 if (attr)
5378 comp_dir = DW_STRING (attr);
5379 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5380 {
5381 comp_dir = ldirname (name);
5382 if (comp_dir != NULL)
5383 make_cleanup (xfree, comp_dir);
5384 }
5385
5386 if (name == NULL)
5387 name = "<unknown>";
5388
5389 attr = dwarf2_attr (die, DW_AT_language, cu);
5390 if (attr)
5391 set_cu_language (DW_UNSND (attr), cu);
5392
5393 /* This isn't technically needed today. It is done for symmetry
5394 with read_file_scope. */
5395 attr = dwarf2_attr (die, DW_AT_producer, cu);
5396 if (attr)
5397 cu->producer = DW_STRING (attr);
5398
5399 /* We assume that we're processing GCC output. */
5400 processing_gcc_compilation = 2;
5401
5402 processing_has_namespace_info = 0;
5403
5404 start_symtab (name, comp_dir, lowpc);
5405 record_debugformat ("DWARF 2");
5406 record_producer (cu->producer);
5407
5408 /* Process the dies in the type unit. */
5409 if (die->child == NULL)
5410 {
5411 dump_die_for_error (die);
5412 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5413 bfd_get_filename (abfd));
5414 }
5415
5416 child_die = die->child;
5417
5418 while (child_die && child_die->tag)
5419 {
5420 process_die (child_die, cu);
5421
5422 child_die = sibling_die (child_die);
5423 }
5424
5425 do_cleanups (back_to);
5426 }
5427
5428 static void
5429 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5430 struct dwarf2_cu *cu)
5431 {
5432 struct function_range *thisfn;
5433
5434 thisfn = (struct function_range *)
5435 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5436 thisfn->name = name;
5437 thisfn->lowpc = lowpc;
5438 thisfn->highpc = highpc;
5439 thisfn->seen_line = 0;
5440 thisfn->next = NULL;
5441
5442 if (cu->last_fn == NULL)
5443 cu->first_fn = thisfn;
5444 else
5445 cu->last_fn->next = thisfn;
5446
5447 cu->last_fn = thisfn;
5448 }
5449
5450 /* qsort helper for inherit_abstract_dies. */
5451
5452 static int
5453 unsigned_int_compar (const void *ap, const void *bp)
5454 {
5455 unsigned int a = *(unsigned int *) ap;
5456 unsigned int b = *(unsigned int *) bp;
5457
5458 return (a > b) - (b > a);
5459 }
5460
5461 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5462 Inherit only the children of the DW_AT_abstract_origin DIE not being
5463 already referenced by DW_AT_abstract_origin from the children of the
5464 current DIE. */
5465
5466 static void
5467 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5468 {
5469 struct die_info *child_die;
5470 unsigned die_children_count;
5471 /* CU offsets which were referenced by children of the current DIE. */
5472 unsigned *offsets;
5473 unsigned *offsets_end, *offsetp;
5474 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5475 struct die_info *origin_die;
5476 /* Iterator of the ORIGIN_DIE children. */
5477 struct die_info *origin_child_die;
5478 struct cleanup *cleanups;
5479 struct attribute *attr;
5480 struct dwarf2_cu *origin_cu;
5481 struct pending **origin_previous_list_in_scope;
5482
5483 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5484 if (!attr)
5485 return;
5486
5487 /* Note that following die references may follow to a die in a
5488 different cu. */
5489
5490 origin_cu = cu;
5491 origin_die = follow_die_ref (die, attr, &origin_cu);
5492
5493 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5494 symbols in. */
5495 origin_previous_list_in_scope = origin_cu->list_in_scope;
5496 origin_cu->list_in_scope = cu->list_in_scope;
5497
5498 if (die->tag != origin_die->tag
5499 && !(die->tag == DW_TAG_inlined_subroutine
5500 && origin_die->tag == DW_TAG_subprogram))
5501 complaint (&symfile_complaints,
5502 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5503 die->offset, origin_die->offset);
5504
5505 child_die = die->child;
5506 die_children_count = 0;
5507 while (child_die && child_die->tag)
5508 {
5509 child_die = sibling_die (child_die);
5510 die_children_count++;
5511 }
5512 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5513 cleanups = make_cleanup (xfree, offsets);
5514
5515 offsets_end = offsets;
5516 child_die = die->child;
5517 while (child_die && child_die->tag)
5518 {
5519 /* For each CHILD_DIE, find the corresponding child of
5520 ORIGIN_DIE. If there is more than one layer of
5521 DW_AT_abstract_origin, follow them all; there shouldn't be,
5522 but GCC versions at least through 4.4 generate this (GCC PR
5523 40573). */
5524 struct die_info *child_origin_die = child_die;
5525 struct dwarf2_cu *child_origin_cu = cu;
5526
5527 while (1)
5528 {
5529 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5530 child_origin_cu);
5531 if (attr == NULL)
5532 break;
5533 child_origin_die = follow_die_ref (child_origin_die, attr,
5534 &child_origin_cu);
5535 }
5536
5537 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5538 counterpart may exist. */
5539 if (child_origin_die != child_die)
5540 {
5541 if (child_die->tag != child_origin_die->tag
5542 && !(child_die->tag == DW_TAG_inlined_subroutine
5543 && child_origin_die->tag == DW_TAG_subprogram))
5544 complaint (&symfile_complaints,
5545 _("Child DIE 0x%x and its abstract origin 0x%x have "
5546 "different tags"), child_die->offset,
5547 child_origin_die->offset);
5548 if (child_origin_die->parent != origin_die)
5549 complaint (&symfile_complaints,
5550 _("Child DIE 0x%x and its abstract origin 0x%x have "
5551 "different parents"), child_die->offset,
5552 child_origin_die->offset);
5553 else
5554 *offsets_end++ = child_origin_die->offset;
5555 }
5556 child_die = sibling_die (child_die);
5557 }
5558 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5559 unsigned_int_compar);
5560 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5561 if (offsetp[-1] == *offsetp)
5562 complaint (&symfile_complaints,
5563 _("Multiple children of DIE 0x%x refer "
5564 "to DIE 0x%x as their abstract origin"),
5565 die->offset, *offsetp);
5566
5567 offsetp = offsets;
5568 origin_child_die = origin_die->child;
5569 while (origin_child_die && origin_child_die->tag)
5570 {
5571 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5572 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5573 offsetp++;
5574 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5575 {
5576 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5577 process_die (origin_child_die, origin_cu);
5578 }
5579 origin_child_die = sibling_die (origin_child_die);
5580 }
5581 origin_cu->list_in_scope = origin_previous_list_in_scope;
5582
5583 do_cleanups (cleanups);
5584 }
5585
5586 static void
5587 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5588 {
5589 struct objfile *objfile = cu->objfile;
5590 struct context_stack *new;
5591 CORE_ADDR lowpc;
5592 CORE_ADDR highpc;
5593 struct die_info *child_die;
5594 struct attribute *attr, *call_line, *call_file;
5595 char *name;
5596 CORE_ADDR baseaddr;
5597 struct block *block;
5598 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5599 VEC (symbolp) *template_args = NULL;
5600 struct template_symbol *templ_func = NULL;
5601
5602 if (inlined_func)
5603 {
5604 /* If we do not have call site information, we can't show the
5605 caller of this inlined function. That's too confusing, so
5606 only use the scope for local variables. */
5607 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5608 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5609 if (call_line == NULL || call_file == NULL)
5610 {
5611 read_lexical_block_scope (die, cu);
5612 return;
5613 }
5614 }
5615
5616 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5617
5618 name = dwarf2_name (die, cu);
5619
5620 /* Ignore functions with missing or empty names. These are actually
5621 illegal according to the DWARF standard. */
5622 if (name == NULL)
5623 {
5624 complaint (&symfile_complaints,
5625 _("missing name for subprogram DIE at %d"), die->offset);
5626 return;
5627 }
5628
5629 /* Ignore functions with missing or invalid low and high pc attributes. */
5630 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5631 {
5632 attr = dwarf2_attr (die, DW_AT_external, cu);
5633 if (!attr || !DW_UNSND (attr))
5634 complaint (&symfile_complaints,
5635 _("cannot get low and high bounds "
5636 "for subprogram DIE at %d"),
5637 die->offset);
5638 return;
5639 }
5640
5641 lowpc += baseaddr;
5642 highpc += baseaddr;
5643
5644 /* Record the function range for dwarf_decode_lines. */
5645 add_to_cu_func_list (name, lowpc, highpc, cu);
5646
5647 /* If we have any template arguments, then we must allocate a
5648 different sort of symbol. */
5649 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5650 {
5651 if (child_die->tag == DW_TAG_template_type_param
5652 || child_die->tag == DW_TAG_template_value_param)
5653 {
5654 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5655 struct template_symbol);
5656 templ_func->base.is_cplus_template_function = 1;
5657 break;
5658 }
5659 }
5660
5661 new = push_context (0, lowpc);
5662 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5663 (struct symbol *) templ_func);
5664
5665 /* If there is a location expression for DW_AT_frame_base, record
5666 it. */
5667 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5668 if (attr)
5669 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5670 expression is being recorded directly in the function's symbol
5671 and not in a separate frame-base object. I guess this hack is
5672 to avoid adding some sort of frame-base adjunct/annex to the
5673 function's symbol :-(. The problem with doing this is that it
5674 results in a function symbol with a location expression that
5675 has nothing to do with the location of the function, ouch! The
5676 relationship should be: a function's symbol has-a frame base; a
5677 frame-base has-a location expression. */
5678 dwarf2_symbol_mark_computed (attr, new->name, cu);
5679
5680 cu->list_in_scope = &local_symbols;
5681
5682 if (die->child != NULL)
5683 {
5684 child_die = die->child;
5685 while (child_die && child_die->tag)
5686 {
5687 if (child_die->tag == DW_TAG_template_type_param
5688 || child_die->tag == DW_TAG_template_value_param)
5689 {
5690 struct symbol *arg = new_symbol (child_die, NULL, cu);
5691
5692 if (arg != NULL)
5693 VEC_safe_push (symbolp, template_args, arg);
5694 }
5695 else
5696 process_die (child_die, cu);
5697 child_die = sibling_die (child_die);
5698 }
5699 }
5700
5701 inherit_abstract_dies (die, cu);
5702
5703 /* If we have a DW_AT_specification, we might need to import using
5704 directives from the context of the specification DIE. See the
5705 comment in determine_prefix. */
5706 if (cu->language == language_cplus
5707 && dwarf2_attr (die, DW_AT_specification, cu))
5708 {
5709 struct dwarf2_cu *spec_cu = cu;
5710 struct die_info *spec_die = die_specification (die, &spec_cu);
5711
5712 while (spec_die)
5713 {
5714 child_die = spec_die->child;
5715 while (child_die && child_die->tag)
5716 {
5717 if (child_die->tag == DW_TAG_imported_module)
5718 process_die (child_die, spec_cu);
5719 child_die = sibling_die (child_die);
5720 }
5721
5722 /* In some cases, GCC generates specification DIEs that
5723 themselves contain DW_AT_specification attributes. */
5724 spec_die = die_specification (spec_die, &spec_cu);
5725 }
5726 }
5727
5728 new = pop_context ();
5729 /* Make a block for the local symbols within. */
5730 block = finish_block (new->name, &local_symbols, new->old_blocks,
5731 lowpc, highpc, objfile);
5732
5733 /* For C++, set the block's scope. */
5734 if (cu->language == language_cplus || cu->language == language_fortran)
5735 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5736 determine_prefix (die, cu),
5737 processing_has_namespace_info);
5738
5739 /* If we have address ranges, record them. */
5740 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5741
5742 /* Attach template arguments to function. */
5743 if (! VEC_empty (symbolp, template_args))
5744 {
5745 gdb_assert (templ_func != NULL);
5746
5747 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5748 templ_func->template_arguments
5749 = obstack_alloc (&objfile->objfile_obstack,
5750 (templ_func->n_template_arguments
5751 * sizeof (struct symbol *)));
5752 memcpy (templ_func->template_arguments,
5753 VEC_address (symbolp, template_args),
5754 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5755 VEC_free (symbolp, template_args);
5756 }
5757
5758 /* In C++, we can have functions nested inside functions (e.g., when
5759 a function declares a class that has methods). This means that
5760 when we finish processing a function scope, we may need to go
5761 back to building a containing block's symbol lists. */
5762 local_symbols = new->locals;
5763 param_symbols = new->params;
5764 using_directives = new->using_directives;
5765
5766 /* If we've finished processing a top-level function, subsequent
5767 symbols go in the file symbol list. */
5768 if (outermost_context_p ())
5769 cu->list_in_scope = &file_symbols;
5770 }
5771
5772 /* Process all the DIES contained within a lexical block scope. Start
5773 a new scope, process the dies, and then close the scope. */
5774
5775 static void
5776 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5777 {
5778 struct objfile *objfile = cu->objfile;
5779 struct context_stack *new;
5780 CORE_ADDR lowpc, highpc;
5781 struct die_info *child_die;
5782 CORE_ADDR baseaddr;
5783
5784 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5785
5786 /* Ignore blocks with missing or invalid low and high pc attributes. */
5787 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5788 as multiple lexical blocks? Handling children in a sane way would
5789 be nasty. Might be easier to properly extend generic blocks to
5790 describe ranges. */
5791 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5792 return;
5793 lowpc += baseaddr;
5794 highpc += baseaddr;
5795
5796 push_context (0, lowpc);
5797 if (die->child != NULL)
5798 {
5799 child_die = die->child;
5800 while (child_die && child_die->tag)
5801 {
5802 process_die (child_die, cu);
5803 child_die = sibling_die (child_die);
5804 }
5805 }
5806 new = pop_context ();
5807
5808 if (local_symbols != NULL || using_directives != NULL)
5809 {
5810 struct block *block
5811 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5812 highpc, objfile);
5813
5814 /* Note that recording ranges after traversing children, as we
5815 do here, means that recording a parent's ranges entails
5816 walking across all its children's ranges as they appear in
5817 the address map, which is quadratic behavior.
5818
5819 It would be nicer to record the parent's ranges before
5820 traversing its children, simply overriding whatever you find
5821 there. But since we don't even decide whether to create a
5822 block until after we've traversed its children, that's hard
5823 to do. */
5824 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5825 }
5826 local_symbols = new->locals;
5827 using_directives = new->using_directives;
5828 }
5829
5830 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5831 Return 1 if the attributes are present and valid, otherwise, return 0.
5832 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
5833
5834 static int
5835 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5836 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5837 struct partial_symtab *ranges_pst)
5838 {
5839 struct objfile *objfile = cu->objfile;
5840 struct comp_unit_head *cu_header = &cu->header;
5841 bfd *obfd = objfile->obfd;
5842 unsigned int addr_size = cu_header->addr_size;
5843 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5844 /* Base address selection entry. */
5845 CORE_ADDR base;
5846 int found_base;
5847 unsigned int dummy;
5848 gdb_byte *buffer;
5849 CORE_ADDR marker;
5850 int low_set;
5851 CORE_ADDR low = 0;
5852 CORE_ADDR high = 0;
5853 CORE_ADDR baseaddr;
5854
5855 found_base = cu->base_known;
5856 base = cu->base_address;
5857
5858 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5859 if (offset >= dwarf2_per_objfile->ranges.size)
5860 {
5861 complaint (&symfile_complaints,
5862 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5863 offset);
5864 return 0;
5865 }
5866 buffer = dwarf2_per_objfile->ranges.buffer + offset;
5867
5868 /* Read in the largest possible address. */
5869 marker = read_address (obfd, buffer, cu, &dummy);
5870 if ((marker & mask) == mask)
5871 {
5872 /* If we found the largest possible address, then
5873 read the base address. */
5874 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5875 buffer += 2 * addr_size;
5876 offset += 2 * addr_size;
5877 found_base = 1;
5878 }
5879
5880 low_set = 0;
5881
5882 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5883
5884 while (1)
5885 {
5886 CORE_ADDR range_beginning, range_end;
5887
5888 range_beginning = read_address (obfd, buffer, cu, &dummy);
5889 buffer += addr_size;
5890 range_end = read_address (obfd, buffer, cu, &dummy);
5891 buffer += addr_size;
5892 offset += 2 * addr_size;
5893
5894 /* An end of list marker is a pair of zero addresses. */
5895 if (range_beginning == 0 && range_end == 0)
5896 /* Found the end of list entry. */
5897 break;
5898
5899 /* Each base address selection entry is a pair of 2 values.
5900 The first is the largest possible address, the second is
5901 the base address. Check for a base address here. */
5902 if ((range_beginning & mask) == mask)
5903 {
5904 /* If we found the largest possible address, then
5905 read the base address. */
5906 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5907 found_base = 1;
5908 continue;
5909 }
5910
5911 if (!found_base)
5912 {
5913 /* We have no valid base address for the ranges
5914 data. */
5915 complaint (&symfile_complaints,
5916 _("Invalid .debug_ranges data (no base address)"));
5917 return 0;
5918 }
5919
5920 if (range_beginning > range_end)
5921 {
5922 /* Inverted range entries are invalid. */
5923 complaint (&symfile_complaints,
5924 _("Invalid .debug_ranges data (inverted range)"));
5925 return 0;
5926 }
5927
5928 /* Empty range entries have no effect. */
5929 if (range_beginning == range_end)
5930 continue;
5931
5932 range_beginning += base;
5933 range_end += base;
5934
5935 if (ranges_pst != NULL)
5936 addrmap_set_empty (objfile->psymtabs_addrmap,
5937 range_beginning + baseaddr,
5938 range_end - 1 + baseaddr,
5939 ranges_pst);
5940
5941 /* FIXME: This is recording everything as a low-high
5942 segment of consecutive addresses. We should have a
5943 data structure for discontiguous block ranges
5944 instead. */
5945 if (! low_set)
5946 {
5947 low = range_beginning;
5948 high = range_end;
5949 low_set = 1;
5950 }
5951 else
5952 {
5953 if (range_beginning < low)
5954 low = range_beginning;
5955 if (range_end > high)
5956 high = range_end;
5957 }
5958 }
5959
5960 if (! low_set)
5961 /* If the first entry is an end-of-list marker, the range
5962 describes an empty scope, i.e. no instructions. */
5963 return 0;
5964
5965 if (low_return)
5966 *low_return = low;
5967 if (high_return)
5968 *high_return = high;
5969 return 1;
5970 }
5971
5972 /* Get low and high pc attributes from a die. Return 1 if the attributes
5973 are present and valid, otherwise, return 0. Return -1 if the range is
5974 discontinuous, i.e. derived from DW_AT_ranges information. */
5975 static int
5976 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5977 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5978 struct partial_symtab *pst)
5979 {
5980 struct attribute *attr;
5981 CORE_ADDR low = 0;
5982 CORE_ADDR high = 0;
5983 int ret = 0;
5984
5985 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5986 if (attr)
5987 {
5988 high = DW_ADDR (attr);
5989 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5990 if (attr)
5991 low = DW_ADDR (attr);
5992 else
5993 /* Found high w/o low attribute. */
5994 return 0;
5995
5996 /* Found consecutive range of addresses. */
5997 ret = 1;
5998 }
5999 else
6000 {
6001 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6002 if (attr != NULL)
6003 {
6004 /* Value of the DW_AT_ranges attribute is the offset in the
6005 .debug_ranges section. */
6006 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6007 return 0;
6008 /* Found discontinuous range of addresses. */
6009 ret = -1;
6010 }
6011 }
6012
6013 /* read_partial_die has also the strict LOW < HIGH requirement. */
6014 if (high <= low)
6015 return 0;
6016
6017 /* When using the GNU linker, .gnu.linkonce. sections are used to
6018 eliminate duplicate copies of functions and vtables and such.
6019 The linker will arbitrarily choose one and discard the others.
6020 The AT_*_pc values for such functions refer to local labels in
6021 these sections. If the section from that file was discarded, the
6022 labels are not in the output, so the relocs get a value of 0.
6023 If this is a discarded function, mark the pc bounds as invalid,
6024 so that GDB will ignore it. */
6025 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6026 return 0;
6027
6028 *lowpc = low;
6029 *highpc = high;
6030 return ret;
6031 }
6032
6033 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6034 its low and high PC addresses. Do nothing if these addresses could not
6035 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6036 and HIGHPC to the high address if greater than HIGHPC. */
6037
6038 static void
6039 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6040 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6041 struct dwarf2_cu *cu)
6042 {
6043 CORE_ADDR low, high;
6044 struct die_info *child = die->child;
6045
6046 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6047 {
6048 *lowpc = min (*lowpc, low);
6049 *highpc = max (*highpc, high);
6050 }
6051
6052 /* If the language does not allow nested subprograms (either inside
6053 subprograms or lexical blocks), we're done. */
6054 if (cu->language != language_ada)
6055 return;
6056
6057 /* Check all the children of the given DIE. If it contains nested
6058 subprograms, then check their pc bounds. Likewise, we need to
6059 check lexical blocks as well, as they may also contain subprogram
6060 definitions. */
6061 while (child && child->tag)
6062 {
6063 if (child->tag == DW_TAG_subprogram
6064 || child->tag == DW_TAG_lexical_block)
6065 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6066 child = sibling_die (child);
6067 }
6068 }
6069
6070 /* Get the low and high pc's represented by the scope DIE, and store
6071 them in *LOWPC and *HIGHPC. If the correct values can't be
6072 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6073
6074 static void
6075 get_scope_pc_bounds (struct die_info *die,
6076 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6077 struct dwarf2_cu *cu)
6078 {
6079 CORE_ADDR best_low = (CORE_ADDR) -1;
6080 CORE_ADDR best_high = (CORE_ADDR) 0;
6081 CORE_ADDR current_low, current_high;
6082
6083 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6084 {
6085 best_low = current_low;
6086 best_high = current_high;
6087 }
6088 else
6089 {
6090 struct die_info *child = die->child;
6091
6092 while (child && child->tag)
6093 {
6094 switch (child->tag) {
6095 case DW_TAG_subprogram:
6096 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6097 break;
6098 case DW_TAG_namespace:
6099 case DW_TAG_module:
6100 /* FIXME: carlton/2004-01-16: Should we do this for
6101 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6102 that current GCC's always emit the DIEs corresponding
6103 to definitions of methods of classes as children of a
6104 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6105 the DIEs giving the declarations, which could be
6106 anywhere). But I don't see any reason why the
6107 standards says that they have to be there. */
6108 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6109
6110 if (current_low != ((CORE_ADDR) -1))
6111 {
6112 best_low = min (best_low, current_low);
6113 best_high = max (best_high, current_high);
6114 }
6115 break;
6116 default:
6117 /* Ignore. */
6118 break;
6119 }
6120
6121 child = sibling_die (child);
6122 }
6123 }
6124
6125 *lowpc = best_low;
6126 *highpc = best_high;
6127 }
6128
6129 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6130 in DIE. */
6131 static void
6132 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6133 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6134 {
6135 struct attribute *attr;
6136
6137 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6138 if (attr)
6139 {
6140 CORE_ADDR high = DW_ADDR (attr);
6141
6142 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6143 if (attr)
6144 {
6145 CORE_ADDR low = DW_ADDR (attr);
6146
6147 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6148 }
6149 }
6150
6151 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6152 if (attr)
6153 {
6154 bfd *obfd = cu->objfile->obfd;
6155
6156 /* The value of the DW_AT_ranges attribute is the offset of the
6157 address range list in the .debug_ranges section. */
6158 unsigned long offset = DW_UNSND (attr);
6159 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6160
6161 /* For some target architectures, but not others, the
6162 read_address function sign-extends the addresses it returns.
6163 To recognize base address selection entries, we need a
6164 mask. */
6165 unsigned int addr_size = cu->header.addr_size;
6166 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6167
6168 /* The base address, to which the next pair is relative. Note
6169 that this 'base' is a DWARF concept: most entries in a range
6170 list are relative, to reduce the number of relocs against the
6171 debugging information. This is separate from this function's
6172 'baseaddr' argument, which GDB uses to relocate debugging
6173 information from a shared library based on the address at
6174 which the library was loaded. */
6175 CORE_ADDR base = cu->base_address;
6176 int base_known = cu->base_known;
6177
6178 gdb_assert (dwarf2_per_objfile->ranges.readin);
6179 if (offset >= dwarf2_per_objfile->ranges.size)
6180 {
6181 complaint (&symfile_complaints,
6182 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6183 offset);
6184 return;
6185 }
6186
6187 for (;;)
6188 {
6189 unsigned int bytes_read;
6190 CORE_ADDR start, end;
6191
6192 start = read_address (obfd, buffer, cu, &bytes_read);
6193 buffer += bytes_read;
6194 end = read_address (obfd, buffer, cu, &bytes_read);
6195 buffer += bytes_read;
6196
6197 /* Did we find the end of the range list? */
6198 if (start == 0 && end == 0)
6199 break;
6200
6201 /* Did we find a base address selection entry? */
6202 else if ((start & base_select_mask) == base_select_mask)
6203 {
6204 base = end;
6205 base_known = 1;
6206 }
6207
6208 /* We found an ordinary address range. */
6209 else
6210 {
6211 if (!base_known)
6212 {
6213 complaint (&symfile_complaints,
6214 _("Invalid .debug_ranges data "
6215 "(no base address)"));
6216 return;
6217 }
6218
6219 if (start > end)
6220 {
6221 /* Inverted range entries are invalid. */
6222 complaint (&symfile_complaints,
6223 _("Invalid .debug_ranges data "
6224 "(inverted range)"));
6225 return;
6226 }
6227
6228 /* Empty range entries have no effect. */
6229 if (start == end)
6230 continue;
6231
6232 record_block_range (block,
6233 baseaddr + base + start,
6234 baseaddr + base + end - 1);
6235 }
6236 }
6237 }
6238 }
6239
6240 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6241 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6242 during 4.6.0 experimental. */
6243
6244 static int
6245 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6246 {
6247 const char *cs;
6248 int major, minor, release;
6249
6250 if (cu->producer == NULL)
6251 {
6252 /* For unknown compilers expect their behavior is DWARF version
6253 compliant.
6254
6255 GCC started to support .debug_types sections by -gdwarf-4 since
6256 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6257 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6258 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6259 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6260
6261 return 0;
6262 }
6263
6264 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6265
6266 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6267 {
6268 /* For non-GCC compilers expect their behavior is DWARF version
6269 compliant. */
6270
6271 return 0;
6272 }
6273 cs = &cu->producer[strlen ("GNU ")];
6274 while (*cs && !isdigit (*cs))
6275 cs++;
6276 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6277 {
6278 /* Not recognized as GCC. */
6279
6280 return 0;
6281 }
6282
6283 return major < 4 || (major == 4 && minor < 6);
6284 }
6285
6286 /* Return the default accessibility type if it is not overriden by
6287 DW_AT_accessibility. */
6288
6289 static enum dwarf_access_attribute
6290 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6291 {
6292 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6293 {
6294 /* The default DWARF 2 accessibility for members is public, the default
6295 accessibility for inheritance is private. */
6296
6297 if (die->tag != DW_TAG_inheritance)
6298 return DW_ACCESS_public;
6299 else
6300 return DW_ACCESS_private;
6301 }
6302 else
6303 {
6304 /* DWARF 3+ defines the default accessibility a different way. The same
6305 rules apply now for DW_TAG_inheritance as for the members and it only
6306 depends on the container kind. */
6307
6308 if (die->parent->tag == DW_TAG_class_type)
6309 return DW_ACCESS_private;
6310 else
6311 return DW_ACCESS_public;
6312 }
6313 }
6314
6315 /* Add an aggregate field to the field list. */
6316
6317 static void
6318 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6319 struct dwarf2_cu *cu)
6320 {
6321 struct objfile *objfile = cu->objfile;
6322 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6323 struct nextfield *new_field;
6324 struct attribute *attr;
6325 struct field *fp;
6326 char *fieldname = "";
6327
6328 /* Allocate a new field list entry and link it in. */
6329 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6330 make_cleanup (xfree, new_field);
6331 memset (new_field, 0, sizeof (struct nextfield));
6332
6333 if (die->tag == DW_TAG_inheritance)
6334 {
6335 new_field->next = fip->baseclasses;
6336 fip->baseclasses = new_field;
6337 }
6338 else
6339 {
6340 new_field->next = fip->fields;
6341 fip->fields = new_field;
6342 }
6343 fip->nfields++;
6344
6345 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6346 if (attr)
6347 new_field->accessibility = DW_UNSND (attr);
6348 else
6349 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6350 if (new_field->accessibility != DW_ACCESS_public)
6351 fip->non_public_fields = 1;
6352
6353 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6354 if (attr)
6355 new_field->virtuality = DW_UNSND (attr);
6356 else
6357 new_field->virtuality = DW_VIRTUALITY_none;
6358
6359 fp = &new_field->field;
6360
6361 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6362 {
6363 /* Data member other than a C++ static data member. */
6364
6365 /* Get type of field. */
6366 fp->type = die_type (die, cu);
6367
6368 SET_FIELD_BITPOS (*fp, 0);
6369
6370 /* Get bit size of field (zero if none). */
6371 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6372 if (attr)
6373 {
6374 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6375 }
6376 else
6377 {
6378 FIELD_BITSIZE (*fp) = 0;
6379 }
6380
6381 /* Get bit offset of field. */
6382 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6383 if (attr)
6384 {
6385 int byte_offset = 0;
6386
6387 if (attr_form_is_section_offset (attr))
6388 dwarf2_complex_location_expr_complaint ();
6389 else if (attr_form_is_constant (attr))
6390 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6391 else if (attr_form_is_block (attr))
6392 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6393 else
6394 dwarf2_complex_location_expr_complaint ();
6395
6396 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6397 }
6398 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6399 if (attr)
6400 {
6401 if (gdbarch_bits_big_endian (gdbarch))
6402 {
6403 /* For big endian bits, the DW_AT_bit_offset gives the
6404 additional bit offset from the MSB of the containing
6405 anonymous object to the MSB of the field. We don't
6406 have to do anything special since we don't need to
6407 know the size of the anonymous object. */
6408 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6409 }
6410 else
6411 {
6412 /* For little endian bits, compute the bit offset to the
6413 MSB of the anonymous object, subtract off the number of
6414 bits from the MSB of the field to the MSB of the
6415 object, and then subtract off the number of bits of
6416 the field itself. The result is the bit offset of
6417 the LSB of the field. */
6418 int anonymous_size;
6419 int bit_offset = DW_UNSND (attr);
6420
6421 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6422 if (attr)
6423 {
6424 /* The size of the anonymous object containing
6425 the bit field is explicit, so use the
6426 indicated size (in bytes). */
6427 anonymous_size = DW_UNSND (attr);
6428 }
6429 else
6430 {
6431 /* The size of the anonymous object containing
6432 the bit field must be inferred from the type
6433 attribute of the data member containing the
6434 bit field. */
6435 anonymous_size = TYPE_LENGTH (fp->type);
6436 }
6437 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6438 - bit_offset - FIELD_BITSIZE (*fp);
6439 }
6440 }
6441
6442 /* Get name of field. */
6443 fieldname = dwarf2_name (die, cu);
6444 if (fieldname == NULL)
6445 fieldname = "";
6446
6447 /* The name is already allocated along with this objfile, so we don't
6448 need to duplicate it for the type. */
6449 fp->name = fieldname;
6450
6451 /* Change accessibility for artificial fields (e.g. virtual table
6452 pointer or virtual base class pointer) to private. */
6453 if (dwarf2_attr (die, DW_AT_artificial, cu))
6454 {
6455 FIELD_ARTIFICIAL (*fp) = 1;
6456 new_field->accessibility = DW_ACCESS_private;
6457 fip->non_public_fields = 1;
6458 }
6459 }
6460 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6461 {
6462 /* C++ static member. */
6463
6464 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6465 is a declaration, but all versions of G++ as of this writing
6466 (so through at least 3.2.1) incorrectly generate
6467 DW_TAG_variable tags. */
6468
6469 char *physname;
6470
6471 /* Get name of field. */
6472 fieldname = dwarf2_name (die, cu);
6473 if (fieldname == NULL)
6474 return;
6475
6476 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6477 if (attr
6478 /* Only create a symbol if this is an external value.
6479 new_symbol checks this and puts the value in the global symbol
6480 table, which we want. If it is not external, new_symbol
6481 will try to put the value in cu->list_in_scope which is wrong. */
6482 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6483 {
6484 /* A static const member, not much different than an enum as far as
6485 we're concerned, except that we can support more types. */
6486 new_symbol (die, NULL, cu);
6487 }
6488
6489 /* Get physical name. */
6490 physname = (char *) dwarf2_physname (fieldname, die, cu);
6491
6492 /* The name is already allocated along with this objfile, so we don't
6493 need to duplicate it for the type. */
6494 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6495 FIELD_TYPE (*fp) = die_type (die, cu);
6496 FIELD_NAME (*fp) = fieldname;
6497 }
6498 else if (die->tag == DW_TAG_inheritance)
6499 {
6500 /* C++ base class field. */
6501 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6502 if (attr)
6503 {
6504 int byte_offset = 0;
6505
6506 if (attr_form_is_section_offset (attr))
6507 dwarf2_complex_location_expr_complaint ();
6508 else if (attr_form_is_constant (attr))
6509 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6510 else if (attr_form_is_block (attr))
6511 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6512 else
6513 dwarf2_complex_location_expr_complaint ();
6514
6515 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6516 }
6517 FIELD_BITSIZE (*fp) = 0;
6518 FIELD_TYPE (*fp) = die_type (die, cu);
6519 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6520 fip->nbaseclasses++;
6521 }
6522 }
6523
6524 /* Add a typedef defined in the scope of the FIP's class. */
6525
6526 static void
6527 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6528 struct dwarf2_cu *cu)
6529 {
6530 struct objfile *objfile = cu->objfile;
6531 struct typedef_field_list *new_field;
6532 struct attribute *attr;
6533 struct typedef_field *fp;
6534 char *fieldname = "";
6535
6536 /* Allocate a new field list entry and link it in. */
6537 new_field = xzalloc (sizeof (*new_field));
6538 make_cleanup (xfree, new_field);
6539
6540 gdb_assert (die->tag == DW_TAG_typedef);
6541
6542 fp = &new_field->field;
6543
6544 /* Get name of field. */
6545 fp->name = dwarf2_name (die, cu);
6546 if (fp->name == NULL)
6547 return;
6548
6549 fp->type = read_type_die (die, cu);
6550
6551 new_field->next = fip->typedef_field_list;
6552 fip->typedef_field_list = new_field;
6553 fip->typedef_field_list_count++;
6554 }
6555
6556 /* Create the vector of fields, and attach it to the type. */
6557
6558 static void
6559 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6560 struct dwarf2_cu *cu)
6561 {
6562 int nfields = fip->nfields;
6563
6564 /* Record the field count, allocate space for the array of fields,
6565 and create blank accessibility bitfields if necessary. */
6566 TYPE_NFIELDS (type) = nfields;
6567 TYPE_FIELDS (type) = (struct field *)
6568 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6569 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6570
6571 if (fip->non_public_fields && cu->language != language_ada)
6572 {
6573 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6574
6575 TYPE_FIELD_PRIVATE_BITS (type) =
6576 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6577 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6578
6579 TYPE_FIELD_PROTECTED_BITS (type) =
6580 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6581 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6582
6583 TYPE_FIELD_IGNORE_BITS (type) =
6584 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6585 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6586 }
6587
6588 /* If the type has baseclasses, allocate and clear a bit vector for
6589 TYPE_FIELD_VIRTUAL_BITS. */
6590 if (fip->nbaseclasses && cu->language != language_ada)
6591 {
6592 int num_bytes = B_BYTES (fip->nbaseclasses);
6593 unsigned char *pointer;
6594
6595 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6596 pointer = TYPE_ALLOC (type, num_bytes);
6597 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6598 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6599 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6600 }
6601
6602 /* Copy the saved-up fields into the field vector. Start from the head of
6603 the list, adding to the tail of the field array, so that they end up in
6604 the same order in the array in which they were added to the list. */
6605 while (nfields-- > 0)
6606 {
6607 struct nextfield *fieldp;
6608
6609 if (fip->fields)
6610 {
6611 fieldp = fip->fields;
6612 fip->fields = fieldp->next;
6613 }
6614 else
6615 {
6616 fieldp = fip->baseclasses;
6617 fip->baseclasses = fieldp->next;
6618 }
6619
6620 TYPE_FIELD (type, nfields) = fieldp->field;
6621 switch (fieldp->accessibility)
6622 {
6623 case DW_ACCESS_private:
6624 if (cu->language != language_ada)
6625 SET_TYPE_FIELD_PRIVATE (type, nfields);
6626 break;
6627
6628 case DW_ACCESS_protected:
6629 if (cu->language != language_ada)
6630 SET_TYPE_FIELD_PROTECTED (type, nfields);
6631 break;
6632
6633 case DW_ACCESS_public:
6634 break;
6635
6636 default:
6637 /* Unknown accessibility. Complain and treat it as public. */
6638 {
6639 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6640 fieldp->accessibility);
6641 }
6642 break;
6643 }
6644 if (nfields < fip->nbaseclasses)
6645 {
6646 switch (fieldp->virtuality)
6647 {
6648 case DW_VIRTUALITY_virtual:
6649 case DW_VIRTUALITY_pure_virtual:
6650 if (cu->language == language_ada)
6651 error (_("unexpected virtuality in component of Ada type"));
6652 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6653 break;
6654 }
6655 }
6656 }
6657 }
6658
6659 /* Add a member function to the proper fieldlist. */
6660
6661 static void
6662 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6663 struct type *type, struct dwarf2_cu *cu)
6664 {
6665 struct objfile *objfile = cu->objfile;
6666 struct attribute *attr;
6667 struct fnfieldlist *flp;
6668 int i;
6669 struct fn_field *fnp;
6670 char *fieldname;
6671 struct nextfnfield *new_fnfield;
6672 struct type *this_type;
6673 enum dwarf_access_attribute accessibility;
6674
6675 if (cu->language == language_ada)
6676 error (_("unexpected member function in Ada type"));
6677
6678 /* Get name of member function. */
6679 fieldname = dwarf2_name (die, cu);
6680 if (fieldname == NULL)
6681 return;
6682
6683 /* Look up member function name in fieldlist. */
6684 for (i = 0; i < fip->nfnfields; i++)
6685 {
6686 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6687 break;
6688 }
6689
6690 /* Create new list element if necessary. */
6691 if (i < fip->nfnfields)
6692 flp = &fip->fnfieldlists[i];
6693 else
6694 {
6695 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6696 {
6697 fip->fnfieldlists = (struct fnfieldlist *)
6698 xrealloc (fip->fnfieldlists,
6699 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6700 * sizeof (struct fnfieldlist));
6701 if (fip->nfnfields == 0)
6702 make_cleanup (free_current_contents, &fip->fnfieldlists);
6703 }
6704 flp = &fip->fnfieldlists[fip->nfnfields];
6705 flp->name = fieldname;
6706 flp->length = 0;
6707 flp->head = NULL;
6708 i = fip->nfnfields++;
6709 }
6710
6711 /* Create a new member function field and chain it to the field list
6712 entry. */
6713 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6714 make_cleanup (xfree, new_fnfield);
6715 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6716 new_fnfield->next = flp->head;
6717 flp->head = new_fnfield;
6718 flp->length++;
6719
6720 /* Fill in the member function field info. */
6721 fnp = &new_fnfield->fnfield;
6722
6723 /* Delay processing of the physname until later. */
6724 if (cu->language == language_cplus || cu->language == language_java)
6725 {
6726 add_to_method_list (type, i, flp->length - 1, fieldname,
6727 die, cu);
6728 }
6729 else
6730 {
6731 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6732 fnp->physname = physname ? physname : "";
6733 }
6734
6735 fnp->type = alloc_type (objfile);
6736 this_type = read_type_die (die, cu);
6737 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6738 {
6739 int nparams = TYPE_NFIELDS (this_type);
6740
6741 /* TYPE is the domain of this method, and THIS_TYPE is the type
6742 of the method itself (TYPE_CODE_METHOD). */
6743 smash_to_method_type (fnp->type, type,
6744 TYPE_TARGET_TYPE (this_type),
6745 TYPE_FIELDS (this_type),
6746 TYPE_NFIELDS (this_type),
6747 TYPE_VARARGS (this_type));
6748
6749 /* Handle static member functions.
6750 Dwarf2 has no clean way to discern C++ static and non-static
6751 member functions. G++ helps GDB by marking the first
6752 parameter for non-static member functions (which is the this
6753 pointer) as artificial. We obtain this information from
6754 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
6755 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6756 fnp->voffset = VOFFSET_STATIC;
6757 }
6758 else
6759 complaint (&symfile_complaints, _("member function type missing for '%s'"),
6760 dwarf2_full_name (fieldname, die, cu));
6761
6762 /* Get fcontext from DW_AT_containing_type if present. */
6763 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6764 fnp->fcontext = die_containing_type (die, cu);
6765
6766 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6767 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6768
6769 /* Get accessibility. */
6770 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6771 if (attr)
6772 accessibility = DW_UNSND (attr);
6773 else
6774 accessibility = dwarf2_default_access_attribute (die, cu);
6775 switch (accessibility)
6776 {
6777 case DW_ACCESS_private:
6778 fnp->is_private = 1;
6779 break;
6780 case DW_ACCESS_protected:
6781 fnp->is_protected = 1;
6782 break;
6783 }
6784
6785 /* Check for artificial methods. */
6786 attr = dwarf2_attr (die, DW_AT_artificial, cu);
6787 if (attr && DW_UNSND (attr) != 0)
6788 fnp->is_artificial = 1;
6789
6790 /* Get index in virtual function table if it is a virtual member
6791 function. For older versions of GCC, this is an offset in the
6792 appropriate virtual table, as specified by DW_AT_containing_type.
6793 For everyone else, it is an expression to be evaluated relative
6794 to the object address. */
6795
6796 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6797 if (attr)
6798 {
6799 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6800 {
6801 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6802 {
6803 /* Old-style GCC. */
6804 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6805 }
6806 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6807 || (DW_BLOCK (attr)->size > 1
6808 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6809 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6810 {
6811 struct dwarf_block blk;
6812 int offset;
6813
6814 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6815 ? 1 : 2);
6816 blk.size = DW_BLOCK (attr)->size - offset;
6817 blk.data = DW_BLOCK (attr)->data + offset;
6818 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6819 if ((fnp->voffset % cu->header.addr_size) != 0)
6820 dwarf2_complex_location_expr_complaint ();
6821 else
6822 fnp->voffset /= cu->header.addr_size;
6823 fnp->voffset += 2;
6824 }
6825 else
6826 dwarf2_complex_location_expr_complaint ();
6827
6828 if (!fnp->fcontext)
6829 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6830 }
6831 else if (attr_form_is_section_offset (attr))
6832 {
6833 dwarf2_complex_location_expr_complaint ();
6834 }
6835 else
6836 {
6837 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6838 fieldname);
6839 }
6840 }
6841 else
6842 {
6843 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6844 if (attr && DW_UNSND (attr))
6845 {
6846 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6847 complaint (&symfile_complaints,
6848 _("Member function \"%s\" (offset %d) is virtual "
6849 "but the vtable offset is not specified"),
6850 fieldname, die->offset);
6851 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6852 TYPE_CPLUS_DYNAMIC (type) = 1;
6853 }
6854 }
6855 }
6856
6857 /* Create the vector of member function fields, and attach it to the type. */
6858
6859 static void
6860 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6861 struct dwarf2_cu *cu)
6862 {
6863 struct fnfieldlist *flp;
6864 int total_length = 0;
6865 int i;
6866
6867 if (cu->language == language_ada)
6868 error (_("unexpected member functions in Ada type"));
6869
6870 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6871 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6872 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6873
6874 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6875 {
6876 struct nextfnfield *nfp = flp->head;
6877 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6878 int k;
6879
6880 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6881 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6882 fn_flp->fn_fields = (struct fn_field *)
6883 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6884 for (k = flp->length; (k--, nfp); nfp = nfp->next)
6885 fn_flp->fn_fields[k] = nfp->fnfield;
6886
6887 total_length += flp->length;
6888 }
6889
6890 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6891 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6892 }
6893
6894 /* Returns non-zero if NAME is the name of a vtable member in CU's
6895 language, zero otherwise. */
6896 static int
6897 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6898 {
6899 static const char vptr[] = "_vptr";
6900 static const char vtable[] = "vtable";
6901
6902 /* Look for the C++ and Java forms of the vtable. */
6903 if ((cu->language == language_java
6904 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6905 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6906 && is_cplus_marker (name[sizeof (vptr) - 1])))
6907 return 1;
6908
6909 return 0;
6910 }
6911
6912 /* GCC outputs unnamed structures that are really pointers to member
6913 functions, with the ABI-specified layout. If TYPE describes
6914 such a structure, smash it into a member function type.
6915
6916 GCC shouldn't do this; it should just output pointer to member DIEs.
6917 This is GCC PR debug/28767. */
6918
6919 static void
6920 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6921 {
6922 struct type *pfn_type, *domain_type, *new_type;
6923
6924 /* Check for a structure with no name and two children. */
6925 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6926 return;
6927
6928 /* Check for __pfn and __delta members. */
6929 if (TYPE_FIELD_NAME (type, 0) == NULL
6930 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6931 || TYPE_FIELD_NAME (type, 1) == NULL
6932 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6933 return;
6934
6935 /* Find the type of the method. */
6936 pfn_type = TYPE_FIELD_TYPE (type, 0);
6937 if (pfn_type == NULL
6938 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6939 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6940 return;
6941
6942 /* Look for the "this" argument. */
6943 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6944 if (TYPE_NFIELDS (pfn_type) == 0
6945 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6946 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6947 return;
6948
6949 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6950 new_type = alloc_type (objfile);
6951 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6952 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6953 TYPE_VARARGS (pfn_type));
6954 smash_to_methodptr_type (type, new_type);
6955 }
6956
6957 /* Called when we find the DIE that starts a structure or union scope
6958 (definition) to create a type for the structure or union. Fill in
6959 the type's name and general properties; the members will not be
6960 processed until process_structure_type.
6961
6962 NOTE: we need to call these functions regardless of whether or not the
6963 DIE has a DW_AT_name attribute, since it might be an anonymous
6964 structure or union. This gets the type entered into our set of
6965 user defined types.
6966
6967 However, if the structure is incomplete (an opaque struct/union)
6968 then suppress creating a symbol table entry for it since gdb only
6969 wants to find the one with the complete definition. Note that if
6970 it is complete, we just call new_symbol, which does it's own
6971 checking about whether the struct/union is anonymous or not (and
6972 suppresses creating a symbol table entry itself). */
6973
6974 static struct type *
6975 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6976 {
6977 struct objfile *objfile = cu->objfile;
6978 struct type *type;
6979 struct attribute *attr;
6980 char *name;
6981
6982 /* If the definition of this type lives in .debug_types, read that type.
6983 Don't follow DW_AT_specification though, that will take us back up
6984 the chain and we want to go down. */
6985 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6986 if (attr)
6987 {
6988 struct dwarf2_cu *type_cu = cu;
6989 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6990
6991 /* We could just recurse on read_structure_type, but we need to call
6992 get_die_type to ensure only one type for this DIE is created.
6993 This is important, for example, because for c++ classes we need
6994 TYPE_NAME set which is only done by new_symbol. Blech. */
6995 type = read_type_die (type_die, type_cu);
6996
6997 /* TYPE_CU may not be the same as CU.
6998 Ensure TYPE is recorded in CU's type_hash table. */
6999 return set_die_type (die, type, cu);
7000 }
7001
7002 type = alloc_type (objfile);
7003 INIT_CPLUS_SPECIFIC (type);
7004
7005 name = dwarf2_name (die, cu);
7006 if (name != NULL)
7007 {
7008 if (cu->language == language_cplus
7009 || cu->language == language_java)
7010 {
7011 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7012
7013 /* dwarf2_full_name might have already finished building the DIE's
7014 type. If so, there is no need to continue. */
7015 if (get_die_type (die, cu) != NULL)
7016 return get_die_type (die, cu);
7017
7018 TYPE_TAG_NAME (type) = full_name;
7019 if (die->tag == DW_TAG_structure_type
7020 || die->tag == DW_TAG_class_type)
7021 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7022 }
7023 else
7024 {
7025 /* The name is already allocated along with this objfile, so
7026 we don't need to duplicate it for the type. */
7027 TYPE_TAG_NAME (type) = (char *) name;
7028 if (die->tag == DW_TAG_class_type)
7029 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7030 }
7031 }
7032
7033 if (die->tag == DW_TAG_structure_type)
7034 {
7035 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7036 }
7037 else if (die->tag == DW_TAG_union_type)
7038 {
7039 TYPE_CODE (type) = TYPE_CODE_UNION;
7040 }
7041 else
7042 {
7043 TYPE_CODE (type) = TYPE_CODE_CLASS;
7044 }
7045
7046 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7047 TYPE_DECLARED_CLASS (type) = 1;
7048
7049 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7050 if (attr)
7051 {
7052 TYPE_LENGTH (type) = DW_UNSND (attr);
7053 }
7054 else
7055 {
7056 TYPE_LENGTH (type) = 0;
7057 }
7058
7059 TYPE_STUB_SUPPORTED (type) = 1;
7060 if (die_is_declaration (die, cu))
7061 TYPE_STUB (type) = 1;
7062 else if (attr == NULL && die->child == NULL
7063 && producer_is_realview (cu->producer))
7064 /* RealView does not output the required DW_AT_declaration
7065 on incomplete types. */
7066 TYPE_STUB (type) = 1;
7067
7068 /* We need to add the type field to the die immediately so we don't
7069 infinitely recurse when dealing with pointers to the structure
7070 type within the structure itself. */
7071 set_die_type (die, type, cu);
7072
7073 /* set_die_type should be already done. */
7074 set_descriptive_type (type, die, cu);
7075
7076 return type;
7077 }
7078
7079 /* Finish creating a structure or union type, including filling in
7080 its members and creating a symbol for it. */
7081
7082 static void
7083 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7084 {
7085 struct objfile *objfile = cu->objfile;
7086 struct die_info *child_die = die->child;
7087 struct type *type;
7088
7089 type = get_die_type (die, cu);
7090 if (type == NULL)
7091 type = read_structure_type (die, cu);
7092
7093 if (die->child != NULL && ! die_is_declaration (die, cu))
7094 {
7095 struct field_info fi;
7096 struct die_info *child_die;
7097 VEC (symbolp) *template_args = NULL;
7098 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7099
7100 memset (&fi, 0, sizeof (struct field_info));
7101
7102 child_die = die->child;
7103
7104 while (child_die && child_die->tag)
7105 {
7106 if (child_die->tag == DW_TAG_member
7107 || child_die->tag == DW_TAG_variable)
7108 {
7109 /* NOTE: carlton/2002-11-05: A C++ static data member
7110 should be a DW_TAG_member that is a declaration, but
7111 all versions of G++ as of this writing (so through at
7112 least 3.2.1) incorrectly generate DW_TAG_variable
7113 tags for them instead. */
7114 dwarf2_add_field (&fi, child_die, cu);
7115 }
7116 else if (child_die->tag == DW_TAG_subprogram)
7117 {
7118 /* C++ member function. */
7119 dwarf2_add_member_fn (&fi, child_die, type, cu);
7120 }
7121 else if (child_die->tag == DW_TAG_inheritance)
7122 {
7123 /* C++ base class field. */
7124 dwarf2_add_field (&fi, child_die, cu);
7125 }
7126 else if (child_die->tag == DW_TAG_typedef)
7127 dwarf2_add_typedef (&fi, child_die, cu);
7128 else if (child_die->tag == DW_TAG_template_type_param
7129 || child_die->tag == DW_TAG_template_value_param)
7130 {
7131 struct symbol *arg = new_symbol (child_die, NULL, cu);
7132
7133 if (arg != NULL)
7134 VEC_safe_push (symbolp, template_args, arg);
7135 }
7136
7137 child_die = sibling_die (child_die);
7138 }
7139
7140 /* Attach template arguments to type. */
7141 if (! VEC_empty (symbolp, template_args))
7142 {
7143 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7144 TYPE_N_TEMPLATE_ARGUMENTS (type)
7145 = VEC_length (symbolp, template_args);
7146 TYPE_TEMPLATE_ARGUMENTS (type)
7147 = obstack_alloc (&objfile->objfile_obstack,
7148 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7149 * sizeof (struct symbol *)));
7150 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7151 VEC_address (symbolp, template_args),
7152 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7153 * sizeof (struct symbol *)));
7154 VEC_free (symbolp, template_args);
7155 }
7156
7157 /* Attach fields and member functions to the type. */
7158 if (fi.nfields)
7159 dwarf2_attach_fields_to_type (&fi, type, cu);
7160 if (fi.nfnfields)
7161 {
7162 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7163
7164 /* Get the type which refers to the base class (possibly this
7165 class itself) which contains the vtable pointer for the current
7166 class from the DW_AT_containing_type attribute. This use of
7167 DW_AT_containing_type is a GNU extension. */
7168
7169 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7170 {
7171 struct type *t = die_containing_type (die, cu);
7172
7173 TYPE_VPTR_BASETYPE (type) = t;
7174 if (type == t)
7175 {
7176 int i;
7177
7178 /* Our own class provides vtbl ptr. */
7179 for (i = TYPE_NFIELDS (t) - 1;
7180 i >= TYPE_N_BASECLASSES (t);
7181 --i)
7182 {
7183 char *fieldname = TYPE_FIELD_NAME (t, i);
7184
7185 if (is_vtable_name (fieldname, cu))
7186 {
7187 TYPE_VPTR_FIELDNO (type) = i;
7188 break;
7189 }
7190 }
7191
7192 /* Complain if virtual function table field not found. */
7193 if (i < TYPE_N_BASECLASSES (t))
7194 complaint (&symfile_complaints,
7195 _("virtual function table pointer "
7196 "not found when defining class '%s'"),
7197 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7198 "");
7199 }
7200 else
7201 {
7202 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7203 }
7204 }
7205 else if (cu->producer
7206 && strncmp (cu->producer,
7207 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7208 {
7209 /* The IBM XLC compiler does not provide direct indication
7210 of the containing type, but the vtable pointer is
7211 always named __vfp. */
7212
7213 int i;
7214
7215 for (i = TYPE_NFIELDS (type) - 1;
7216 i >= TYPE_N_BASECLASSES (type);
7217 --i)
7218 {
7219 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7220 {
7221 TYPE_VPTR_FIELDNO (type) = i;
7222 TYPE_VPTR_BASETYPE (type) = type;
7223 break;
7224 }
7225 }
7226 }
7227 }
7228
7229 /* Copy fi.typedef_field_list linked list elements content into the
7230 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7231 if (fi.typedef_field_list)
7232 {
7233 int i = fi.typedef_field_list_count;
7234
7235 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7236 TYPE_TYPEDEF_FIELD_ARRAY (type)
7237 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7238 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7239
7240 /* Reverse the list order to keep the debug info elements order. */
7241 while (--i >= 0)
7242 {
7243 struct typedef_field *dest, *src;
7244
7245 dest = &TYPE_TYPEDEF_FIELD (type, i);
7246 src = &fi.typedef_field_list->field;
7247 fi.typedef_field_list = fi.typedef_field_list->next;
7248 *dest = *src;
7249 }
7250 }
7251
7252 do_cleanups (back_to);
7253 }
7254
7255 quirk_gcc_member_function_pointer (type, cu->objfile);
7256
7257 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7258 snapshots) has been known to create a die giving a declaration
7259 for a class that has, as a child, a die giving a definition for a
7260 nested class. So we have to process our children even if the
7261 current die is a declaration. Normally, of course, a declaration
7262 won't have any children at all. */
7263
7264 while (child_die != NULL && child_die->tag)
7265 {
7266 if (child_die->tag == DW_TAG_member
7267 || child_die->tag == DW_TAG_variable
7268 || child_die->tag == DW_TAG_inheritance
7269 || child_die->tag == DW_TAG_template_value_param
7270 || child_die->tag == DW_TAG_template_type_param)
7271 {
7272 /* Do nothing. */
7273 }
7274 else
7275 process_die (child_die, cu);
7276
7277 child_die = sibling_die (child_die);
7278 }
7279
7280 /* Do not consider external references. According to the DWARF standard,
7281 these DIEs are identified by the fact that they have no byte_size
7282 attribute, and a declaration attribute. */
7283 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7284 || !die_is_declaration (die, cu))
7285 new_symbol (die, type, cu);
7286 }
7287
7288 /* Given a DW_AT_enumeration_type die, set its type. We do not
7289 complete the type's fields yet, or create any symbols. */
7290
7291 static struct type *
7292 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7293 {
7294 struct objfile *objfile = cu->objfile;
7295 struct type *type;
7296 struct attribute *attr;
7297 const char *name;
7298
7299 /* If the definition of this type lives in .debug_types, read that type.
7300 Don't follow DW_AT_specification though, that will take us back up
7301 the chain and we want to go down. */
7302 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7303 if (attr)
7304 {
7305 struct dwarf2_cu *type_cu = cu;
7306 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7307
7308 type = read_type_die (type_die, type_cu);
7309
7310 /* TYPE_CU may not be the same as CU.
7311 Ensure TYPE is recorded in CU's type_hash table. */
7312 return set_die_type (die, type, cu);
7313 }
7314
7315 type = alloc_type (objfile);
7316
7317 TYPE_CODE (type) = TYPE_CODE_ENUM;
7318 name = dwarf2_full_name (NULL, die, cu);
7319 if (name != NULL)
7320 TYPE_TAG_NAME (type) = (char *) name;
7321
7322 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7323 if (attr)
7324 {
7325 TYPE_LENGTH (type) = DW_UNSND (attr);
7326 }
7327 else
7328 {
7329 TYPE_LENGTH (type) = 0;
7330 }
7331
7332 /* The enumeration DIE can be incomplete. In Ada, any type can be
7333 declared as private in the package spec, and then defined only
7334 inside the package body. Such types are known as Taft Amendment
7335 Types. When another package uses such a type, an incomplete DIE
7336 may be generated by the compiler. */
7337 if (die_is_declaration (die, cu))
7338 TYPE_STUB (type) = 1;
7339
7340 return set_die_type (die, type, cu);
7341 }
7342
7343 /* Given a pointer to a die which begins an enumeration, process all
7344 the dies that define the members of the enumeration, and create the
7345 symbol for the enumeration type.
7346
7347 NOTE: We reverse the order of the element list. */
7348
7349 static void
7350 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7351 {
7352 struct type *this_type;
7353
7354 this_type = get_die_type (die, cu);
7355 if (this_type == NULL)
7356 this_type = read_enumeration_type (die, cu);
7357
7358 if (die->child != NULL)
7359 {
7360 struct die_info *child_die;
7361 struct symbol *sym;
7362 struct field *fields = NULL;
7363 int num_fields = 0;
7364 int unsigned_enum = 1;
7365 char *name;
7366
7367 child_die = die->child;
7368 while (child_die && child_die->tag)
7369 {
7370 if (child_die->tag != DW_TAG_enumerator)
7371 {
7372 process_die (child_die, cu);
7373 }
7374 else
7375 {
7376 name = dwarf2_name (child_die, cu);
7377 if (name)
7378 {
7379 sym = new_symbol (child_die, this_type, cu);
7380 if (SYMBOL_VALUE (sym) < 0)
7381 unsigned_enum = 0;
7382
7383 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7384 {
7385 fields = (struct field *)
7386 xrealloc (fields,
7387 (num_fields + DW_FIELD_ALLOC_CHUNK)
7388 * sizeof (struct field));
7389 }
7390
7391 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7392 FIELD_TYPE (fields[num_fields]) = NULL;
7393 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7394 FIELD_BITSIZE (fields[num_fields]) = 0;
7395
7396 num_fields++;
7397 }
7398 }
7399
7400 child_die = sibling_die (child_die);
7401 }
7402
7403 if (num_fields)
7404 {
7405 TYPE_NFIELDS (this_type) = num_fields;
7406 TYPE_FIELDS (this_type) = (struct field *)
7407 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7408 memcpy (TYPE_FIELDS (this_type), fields,
7409 sizeof (struct field) * num_fields);
7410 xfree (fields);
7411 }
7412 if (unsigned_enum)
7413 TYPE_UNSIGNED (this_type) = 1;
7414 }
7415
7416 new_symbol (die, this_type, cu);
7417 }
7418
7419 /* Extract all information from a DW_TAG_array_type DIE and put it in
7420 the DIE's type field. For now, this only handles one dimensional
7421 arrays. */
7422
7423 static struct type *
7424 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7425 {
7426 struct objfile *objfile = cu->objfile;
7427 struct die_info *child_die;
7428 struct type *type;
7429 struct type *element_type, *range_type, *index_type;
7430 struct type **range_types = NULL;
7431 struct attribute *attr;
7432 int ndim = 0;
7433 struct cleanup *back_to;
7434 char *name;
7435
7436 element_type = die_type (die, cu);
7437
7438 /* The die_type call above may have already set the type for this DIE. */
7439 type = get_die_type (die, cu);
7440 if (type)
7441 return type;
7442
7443 /* Irix 6.2 native cc creates array types without children for
7444 arrays with unspecified length. */
7445 if (die->child == NULL)
7446 {
7447 index_type = objfile_type (objfile)->builtin_int;
7448 range_type = create_range_type (NULL, index_type, 0, -1);
7449 type = create_array_type (NULL, element_type, range_type);
7450 return set_die_type (die, type, cu);
7451 }
7452
7453 back_to = make_cleanup (null_cleanup, NULL);
7454 child_die = die->child;
7455 while (child_die && child_die->tag)
7456 {
7457 if (child_die->tag == DW_TAG_subrange_type)
7458 {
7459 struct type *child_type = read_type_die (child_die, cu);
7460
7461 if (child_type != NULL)
7462 {
7463 /* The range type was succesfully read. Save it for the
7464 array type creation. */
7465 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7466 {
7467 range_types = (struct type **)
7468 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7469 * sizeof (struct type *));
7470 if (ndim == 0)
7471 make_cleanup (free_current_contents, &range_types);
7472 }
7473 range_types[ndim++] = child_type;
7474 }
7475 }
7476 child_die = sibling_die (child_die);
7477 }
7478
7479 /* Dwarf2 dimensions are output from left to right, create the
7480 necessary array types in backwards order. */
7481
7482 type = element_type;
7483
7484 if (read_array_order (die, cu) == DW_ORD_col_major)
7485 {
7486 int i = 0;
7487
7488 while (i < ndim)
7489 type = create_array_type (NULL, type, range_types[i++]);
7490 }
7491 else
7492 {
7493 while (ndim-- > 0)
7494 type = create_array_type (NULL, type, range_types[ndim]);
7495 }
7496
7497 /* Understand Dwarf2 support for vector types (like they occur on
7498 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7499 array type. This is not part of the Dwarf2/3 standard yet, but a
7500 custom vendor extension. The main difference between a regular
7501 array and the vector variant is that vectors are passed by value
7502 to functions. */
7503 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7504 if (attr)
7505 make_vector_type (type);
7506
7507 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7508 implementation may choose to implement triple vectors using this
7509 attribute. */
7510 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7511 if (attr)
7512 {
7513 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7514 TYPE_LENGTH (type) = DW_UNSND (attr);
7515 else
7516 complaint (&symfile_complaints,
7517 _("DW_AT_byte_size for array type smaller "
7518 "than the total size of elements"));
7519 }
7520
7521 name = dwarf2_name (die, cu);
7522 if (name)
7523 TYPE_NAME (type) = name;
7524
7525 /* Install the type in the die. */
7526 set_die_type (die, type, cu);
7527
7528 /* set_die_type should be already done. */
7529 set_descriptive_type (type, die, cu);
7530
7531 do_cleanups (back_to);
7532
7533 return type;
7534 }
7535
7536 static enum dwarf_array_dim_ordering
7537 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7538 {
7539 struct attribute *attr;
7540
7541 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7542
7543 if (attr) return DW_SND (attr);
7544
7545 /* GNU F77 is a special case, as at 08/2004 array type info is the
7546 opposite order to the dwarf2 specification, but data is still
7547 laid out as per normal fortran.
7548
7549 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7550 version checking. */
7551
7552 if (cu->language == language_fortran
7553 && cu->producer && strstr (cu->producer, "GNU F77"))
7554 {
7555 return DW_ORD_row_major;
7556 }
7557
7558 switch (cu->language_defn->la_array_ordering)
7559 {
7560 case array_column_major:
7561 return DW_ORD_col_major;
7562 case array_row_major:
7563 default:
7564 return DW_ORD_row_major;
7565 };
7566 }
7567
7568 /* Extract all information from a DW_TAG_set_type DIE and put it in
7569 the DIE's type field. */
7570
7571 static struct type *
7572 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7573 {
7574 struct type *domain_type, *set_type;
7575 struct attribute *attr;
7576
7577 domain_type = die_type (die, cu);
7578
7579 /* The die_type call above may have already set the type for this DIE. */
7580 set_type = get_die_type (die, cu);
7581 if (set_type)
7582 return set_type;
7583
7584 set_type = create_set_type (NULL, domain_type);
7585
7586 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7587 if (attr)
7588 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7589
7590 return set_die_type (die, set_type, cu);
7591 }
7592
7593 /* First cut: install each common block member as a global variable. */
7594
7595 static void
7596 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7597 {
7598 struct die_info *child_die;
7599 struct attribute *attr;
7600 struct symbol *sym;
7601 CORE_ADDR base = (CORE_ADDR) 0;
7602
7603 attr = dwarf2_attr (die, DW_AT_location, cu);
7604 if (attr)
7605 {
7606 /* Support the .debug_loc offsets. */
7607 if (attr_form_is_block (attr))
7608 {
7609 base = decode_locdesc (DW_BLOCK (attr), cu);
7610 }
7611 else if (attr_form_is_section_offset (attr))
7612 {
7613 dwarf2_complex_location_expr_complaint ();
7614 }
7615 else
7616 {
7617 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7618 "common block member");
7619 }
7620 }
7621 if (die->child != NULL)
7622 {
7623 child_die = die->child;
7624 while (child_die && child_die->tag)
7625 {
7626 sym = new_symbol (child_die, NULL, cu);
7627 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7628 if (sym != NULL && attr != NULL)
7629 {
7630 CORE_ADDR byte_offset = 0;
7631
7632 if (attr_form_is_section_offset (attr))
7633 dwarf2_complex_location_expr_complaint ();
7634 else if (attr_form_is_constant (attr))
7635 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7636 else if (attr_form_is_block (attr))
7637 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7638 else
7639 dwarf2_complex_location_expr_complaint ();
7640
7641 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7642 add_symbol_to_list (sym, &global_symbols);
7643 }
7644 child_die = sibling_die (child_die);
7645 }
7646 }
7647 }
7648
7649 /* Create a type for a C++ namespace. */
7650
7651 static struct type *
7652 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7653 {
7654 struct objfile *objfile = cu->objfile;
7655 const char *previous_prefix, *name;
7656 int is_anonymous;
7657 struct type *type;
7658
7659 /* For extensions, reuse the type of the original namespace. */
7660 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7661 {
7662 struct die_info *ext_die;
7663 struct dwarf2_cu *ext_cu = cu;
7664
7665 ext_die = dwarf2_extension (die, &ext_cu);
7666 type = read_type_die (ext_die, ext_cu);
7667
7668 /* EXT_CU may not be the same as CU.
7669 Ensure TYPE is recorded in CU's type_hash table. */
7670 return set_die_type (die, type, cu);
7671 }
7672
7673 name = namespace_name (die, &is_anonymous, cu);
7674
7675 /* Now build the name of the current namespace. */
7676
7677 previous_prefix = determine_prefix (die, cu);
7678 if (previous_prefix[0] != '\0')
7679 name = typename_concat (&objfile->objfile_obstack,
7680 previous_prefix, name, 0, cu);
7681
7682 /* Create the type. */
7683 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7684 objfile);
7685 TYPE_NAME (type) = (char *) name;
7686 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7687
7688 return set_die_type (die, type, cu);
7689 }
7690
7691 /* Read a C++ namespace. */
7692
7693 static void
7694 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7695 {
7696 struct objfile *objfile = cu->objfile;
7697 int is_anonymous;
7698
7699 /* Add a symbol associated to this if we haven't seen the namespace
7700 before. Also, add a using directive if it's an anonymous
7701 namespace. */
7702
7703 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7704 {
7705 struct type *type;
7706
7707 type = read_type_die (die, cu);
7708 new_symbol (die, type, cu);
7709
7710 namespace_name (die, &is_anonymous, cu);
7711 if (is_anonymous)
7712 {
7713 const char *previous_prefix = determine_prefix (die, cu);
7714
7715 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7716 NULL, &objfile->objfile_obstack);
7717 }
7718 }
7719
7720 if (die->child != NULL)
7721 {
7722 struct die_info *child_die = die->child;
7723
7724 while (child_die && child_die->tag)
7725 {
7726 process_die (child_die, cu);
7727 child_die = sibling_die (child_die);
7728 }
7729 }
7730 }
7731
7732 /* Read a Fortran module as type. This DIE can be only a declaration used for
7733 imported module. Still we need that type as local Fortran "use ... only"
7734 declaration imports depend on the created type in determine_prefix. */
7735
7736 static struct type *
7737 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7738 {
7739 struct objfile *objfile = cu->objfile;
7740 char *module_name;
7741 struct type *type;
7742
7743 module_name = dwarf2_name (die, cu);
7744 if (!module_name)
7745 complaint (&symfile_complaints,
7746 _("DW_TAG_module has no name, offset 0x%x"),
7747 die->offset);
7748 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7749
7750 /* determine_prefix uses TYPE_TAG_NAME. */
7751 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7752
7753 return set_die_type (die, type, cu);
7754 }
7755
7756 /* Read a Fortran module. */
7757
7758 static void
7759 read_module (struct die_info *die, struct dwarf2_cu *cu)
7760 {
7761 struct die_info *child_die = die->child;
7762
7763 while (child_die && child_die->tag)
7764 {
7765 process_die (child_die, cu);
7766 child_die = sibling_die (child_die);
7767 }
7768 }
7769
7770 /* Return the name of the namespace represented by DIE. Set
7771 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7772 namespace. */
7773
7774 static const char *
7775 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7776 {
7777 struct die_info *current_die;
7778 const char *name = NULL;
7779
7780 /* Loop through the extensions until we find a name. */
7781
7782 for (current_die = die;
7783 current_die != NULL;
7784 current_die = dwarf2_extension (die, &cu))
7785 {
7786 name = dwarf2_name (current_die, cu);
7787 if (name != NULL)
7788 break;
7789 }
7790
7791 /* Is it an anonymous namespace? */
7792
7793 *is_anonymous = (name == NULL);
7794 if (*is_anonymous)
7795 name = "(anonymous namespace)";
7796
7797 return name;
7798 }
7799
7800 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7801 the user defined type vector. */
7802
7803 static struct type *
7804 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7805 {
7806 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7807 struct comp_unit_head *cu_header = &cu->header;
7808 struct type *type;
7809 struct attribute *attr_byte_size;
7810 struct attribute *attr_address_class;
7811 int byte_size, addr_class;
7812 struct type *target_type;
7813
7814 target_type = die_type (die, cu);
7815
7816 /* The die_type call above may have already set the type for this DIE. */
7817 type = get_die_type (die, cu);
7818 if (type)
7819 return type;
7820
7821 type = lookup_pointer_type (target_type);
7822
7823 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7824 if (attr_byte_size)
7825 byte_size = DW_UNSND (attr_byte_size);
7826 else
7827 byte_size = cu_header->addr_size;
7828
7829 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7830 if (attr_address_class)
7831 addr_class = DW_UNSND (attr_address_class);
7832 else
7833 addr_class = DW_ADDR_none;
7834
7835 /* If the pointer size or address class is different than the
7836 default, create a type variant marked as such and set the
7837 length accordingly. */
7838 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7839 {
7840 if (gdbarch_address_class_type_flags_p (gdbarch))
7841 {
7842 int type_flags;
7843
7844 type_flags = gdbarch_address_class_type_flags
7845 (gdbarch, byte_size, addr_class);
7846 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7847 == 0);
7848 type = make_type_with_address_space (type, type_flags);
7849 }
7850 else if (TYPE_LENGTH (type) != byte_size)
7851 {
7852 complaint (&symfile_complaints,
7853 _("invalid pointer size %d"), byte_size);
7854 }
7855 else
7856 {
7857 /* Should we also complain about unhandled address classes? */
7858 }
7859 }
7860
7861 TYPE_LENGTH (type) = byte_size;
7862 return set_die_type (die, type, cu);
7863 }
7864
7865 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7866 the user defined type vector. */
7867
7868 static struct type *
7869 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7870 {
7871 struct type *type;
7872 struct type *to_type;
7873 struct type *domain;
7874
7875 to_type = die_type (die, cu);
7876 domain = die_containing_type (die, cu);
7877
7878 /* The calls 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 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7884 type = lookup_methodptr_type (to_type);
7885 else
7886 type = lookup_memberptr_type (to_type, domain);
7887
7888 return set_die_type (die, type, cu);
7889 }
7890
7891 /* Extract all information from a DW_TAG_reference_type DIE and add to
7892 the user defined type vector. */
7893
7894 static struct type *
7895 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7896 {
7897 struct comp_unit_head *cu_header = &cu->header;
7898 struct type *type, *target_type;
7899 struct attribute *attr;
7900
7901 target_type = die_type (die, cu);
7902
7903 /* The die_type call above may have already set the type for this DIE. */
7904 type = get_die_type (die, cu);
7905 if (type)
7906 return type;
7907
7908 type = lookup_reference_type (target_type);
7909 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7910 if (attr)
7911 {
7912 TYPE_LENGTH (type) = DW_UNSND (attr);
7913 }
7914 else
7915 {
7916 TYPE_LENGTH (type) = cu_header->addr_size;
7917 }
7918 return set_die_type (die, type, cu);
7919 }
7920
7921 static struct type *
7922 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7923 {
7924 struct type *base_type, *cv_type;
7925
7926 base_type = die_type (die, cu);
7927
7928 /* The die_type call above may have already set the type for this DIE. */
7929 cv_type = get_die_type (die, cu);
7930 if (cv_type)
7931 return cv_type;
7932
7933 /* In case the const qualifier is applied to an array type, the element type
7934 is so qualified, not the array type (section 6.7.3 of C99). */
7935 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7936 {
7937 struct type *el_type, *inner_array;
7938
7939 base_type = copy_type (base_type);
7940 inner_array = base_type;
7941
7942 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7943 {
7944 TYPE_TARGET_TYPE (inner_array) =
7945 copy_type (TYPE_TARGET_TYPE (inner_array));
7946 inner_array = TYPE_TARGET_TYPE (inner_array);
7947 }
7948
7949 el_type = TYPE_TARGET_TYPE (inner_array);
7950 TYPE_TARGET_TYPE (inner_array) =
7951 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7952
7953 return set_die_type (die, base_type, cu);
7954 }
7955
7956 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7957 return set_die_type (die, cv_type, cu);
7958 }
7959
7960 static struct type *
7961 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7962 {
7963 struct type *base_type, *cv_type;
7964
7965 base_type = die_type (die, cu);
7966
7967 /* The die_type call above may have already set the type for this DIE. */
7968 cv_type = get_die_type (die, cu);
7969 if (cv_type)
7970 return cv_type;
7971
7972 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7973 return set_die_type (die, cv_type, cu);
7974 }
7975
7976 /* Extract all information from a DW_TAG_string_type DIE and add to
7977 the user defined type vector. It isn't really a user defined type,
7978 but it behaves like one, with other DIE's using an AT_user_def_type
7979 attribute to reference it. */
7980
7981 static struct type *
7982 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7983 {
7984 struct objfile *objfile = cu->objfile;
7985 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7986 struct type *type, *range_type, *index_type, *char_type;
7987 struct attribute *attr;
7988 unsigned int length;
7989
7990 attr = dwarf2_attr (die, DW_AT_string_length, cu);
7991 if (attr)
7992 {
7993 length = DW_UNSND (attr);
7994 }
7995 else
7996 {
7997 /* Check for the DW_AT_byte_size attribute. */
7998 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7999 if (attr)
8000 {
8001 length = DW_UNSND (attr);
8002 }
8003 else
8004 {
8005 length = 1;
8006 }
8007 }
8008
8009 index_type = objfile_type (objfile)->builtin_int;
8010 range_type = create_range_type (NULL, index_type, 1, length);
8011 char_type = language_string_char_type (cu->language_defn, gdbarch);
8012 type = create_string_type (NULL, char_type, range_type);
8013
8014 return set_die_type (die, type, cu);
8015 }
8016
8017 /* Handle DIES due to C code like:
8018
8019 struct foo
8020 {
8021 int (*funcp)(int a, long l);
8022 int b;
8023 };
8024
8025 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8026
8027 static struct type *
8028 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8029 {
8030 struct type *type; /* Type that this function returns. */
8031 struct type *ftype; /* Function that returns above type. */
8032 struct attribute *attr;
8033
8034 type = die_type (die, cu);
8035
8036 /* The die_type call above may have already set the type for this DIE. */
8037 ftype = get_die_type (die, cu);
8038 if (ftype)
8039 return ftype;
8040
8041 ftype = lookup_function_type (type);
8042
8043 /* All functions in C++, Pascal and Java have prototypes. */
8044 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8045 if ((attr && (DW_UNSND (attr) != 0))
8046 || cu->language == language_cplus
8047 || cu->language == language_java
8048 || cu->language == language_pascal)
8049 TYPE_PROTOTYPED (ftype) = 1;
8050 else if (producer_is_realview (cu->producer))
8051 /* RealView does not emit DW_AT_prototyped. We can not
8052 distinguish prototyped and unprototyped functions; default to
8053 prototyped, since that is more common in modern code (and
8054 RealView warns about unprototyped functions). */
8055 TYPE_PROTOTYPED (ftype) = 1;
8056
8057 /* Store the calling convention in the type if it's available in
8058 the subroutine die. Otherwise set the calling convention to
8059 the default value DW_CC_normal. */
8060 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8061 if (attr)
8062 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8063 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8064 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8065 else
8066 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8067
8068 /* We need to add the subroutine type to the die immediately so
8069 we don't infinitely recurse when dealing with parameters
8070 declared as the same subroutine type. */
8071 set_die_type (die, ftype, cu);
8072
8073 if (die->child != NULL)
8074 {
8075 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8076 struct die_info *child_die;
8077 int nparams, iparams;
8078
8079 /* Count the number of parameters.
8080 FIXME: GDB currently ignores vararg functions, but knows about
8081 vararg member functions. */
8082 nparams = 0;
8083 child_die = die->child;
8084 while (child_die && child_die->tag)
8085 {
8086 if (child_die->tag == DW_TAG_formal_parameter)
8087 nparams++;
8088 else if (child_die->tag == DW_TAG_unspecified_parameters)
8089 TYPE_VARARGS (ftype) = 1;
8090 child_die = sibling_die (child_die);
8091 }
8092
8093 /* Allocate storage for parameters and fill them in. */
8094 TYPE_NFIELDS (ftype) = nparams;
8095 TYPE_FIELDS (ftype) = (struct field *)
8096 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8097
8098 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8099 even if we error out during the parameters reading below. */
8100 for (iparams = 0; iparams < nparams; iparams++)
8101 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8102
8103 iparams = 0;
8104 child_die = die->child;
8105 while (child_die && child_die->tag)
8106 {
8107 if (child_die->tag == DW_TAG_formal_parameter)
8108 {
8109 struct type *arg_type;
8110
8111 /* DWARF version 2 has no clean way to discern C++
8112 static and non-static member functions. G++ helps
8113 GDB by marking the first parameter for non-static
8114 member functions (which is the this pointer) as
8115 artificial. We pass this information to
8116 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8117
8118 DWARF version 3 added DW_AT_object_pointer, which GCC
8119 4.5 does not yet generate. */
8120 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8121 if (attr)
8122 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8123 else
8124 {
8125 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8126
8127 /* GCC/43521: In java, the formal parameter
8128 "this" is sometimes not marked with DW_AT_artificial. */
8129 if (cu->language == language_java)
8130 {
8131 const char *name = dwarf2_name (child_die, cu);
8132
8133 if (name && !strcmp (name, "this"))
8134 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8135 }
8136 }
8137 arg_type = die_type (child_die, cu);
8138
8139 /* RealView does not mark THIS as const, which the testsuite
8140 expects. GCC marks THIS as const in method definitions,
8141 but not in the class specifications (GCC PR 43053). */
8142 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8143 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8144 {
8145 int is_this = 0;
8146 struct dwarf2_cu *arg_cu = cu;
8147 const char *name = dwarf2_name (child_die, cu);
8148
8149 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8150 if (attr)
8151 {
8152 /* If the compiler emits this, use it. */
8153 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8154 is_this = 1;
8155 }
8156 else if (name && strcmp (name, "this") == 0)
8157 /* Function definitions will have the argument names. */
8158 is_this = 1;
8159 else if (name == NULL && iparams == 0)
8160 /* Declarations may not have the names, so like
8161 elsewhere in GDB, assume an artificial first
8162 argument is "this". */
8163 is_this = 1;
8164
8165 if (is_this)
8166 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8167 arg_type, 0);
8168 }
8169
8170 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8171 iparams++;
8172 }
8173 child_die = sibling_die (child_die);
8174 }
8175 }
8176
8177 return ftype;
8178 }
8179
8180 static struct type *
8181 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8182 {
8183 struct objfile *objfile = cu->objfile;
8184 const char *name = NULL;
8185 struct type *this_type;
8186
8187 name = dwarf2_full_name (NULL, die, cu);
8188 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8189 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8190 TYPE_NAME (this_type) = (char *) name;
8191 set_die_type (die, this_type, cu);
8192 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8193 return this_type;
8194 }
8195
8196 /* Find a representation of a given base type and install
8197 it in the TYPE field of the die. */
8198
8199 static struct type *
8200 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8201 {
8202 struct objfile *objfile = cu->objfile;
8203 struct type *type;
8204 struct attribute *attr;
8205 int encoding = 0, size = 0;
8206 char *name;
8207 enum type_code code = TYPE_CODE_INT;
8208 int type_flags = 0;
8209 struct type *target_type = NULL;
8210
8211 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8212 if (attr)
8213 {
8214 encoding = DW_UNSND (attr);
8215 }
8216 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8217 if (attr)
8218 {
8219 size = DW_UNSND (attr);
8220 }
8221 name = dwarf2_name (die, cu);
8222 if (!name)
8223 {
8224 complaint (&symfile_complaints,
8225 _("DW_AT_name missing from DW_TAG_base_type"));
8226 }
8227
8228 switch (encoding)
8229 {
8230 case DW_ATE_address:
8231 /* Turn DW_ATE_address into a void * pointer. */
8232 code = TYPE_CODE_PTR;
8233 type_flags |= TYPE_FLAG_UNSIGNED;
8234 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8235 break;
8236 case DW_ATE_boolean:
8237 code = TYPE_CODE_BOOL;
8238 type_flags |= TYPE_FLAG_UNSIGNED;
8239 break;
8240 case DW_ATE_complex_float:
8241 code = TYPE_CODE_COMPLEX;
8242 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8243 break;
8244 case DW_ATE_decimal_float:
8245 code = TYPE_CODE_DECFLOAT;
8246 break;
8247 case DW_ATE_float:
8248 code = TYPE_CODE_FLT;
8249 break;
8250 case DW_ATE_signed:
8251 break;
8252 case DW_ATE_unsigned:
8253 type_flags |= TYPE_FLAG_UNSIGNED;
8254 break;
8255 case DW_ATE_signed_char:
8256 if (cu->language == language_ada || cu->language == language_m2
8257 || cu->language == language_pascal)
8258 code = TYPE_CODE_CHAR;
8259 break;
8260 case DW_ATE_unsigned_char:
8261 if (cu->language == language_ada || cu->language == language_m2
8262 || cu->language == language_pascal)
8263 code = TYPE_CODE_CHAR;
8264 type_flags |= TYPE_FLAG_UNSIGNED;
8265 break;
8266 case DW_ATE_UTF:
8267 /* We just treat this as an integer and then recognize the
8268 type by name elsewhere. */
8269 break;
8270
8271 default:
8272 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8273 dwarf_type_encoding_name (encoding));
8274 break;
8275 }
8276
8277 type = init_type (code, size, type_flags, NULL, objfile);
8278 TYPE_NAME (type) = name;
8279 TYPE_TARGET_TYPE (type) = target_type;
8280
8281 if (name && strcmp (name, "char") == 0)
8282 TYPE_NOSIGN (type) = 1;
8283
8284 return set_die_type (die, type, cu);
8285 }
8286
8287 /* Read the given DW_AT_subrange DIE. */
8288
8289 static struct type *
8290 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8291 {
8292 struct type *base_type;
8293 struct type *range_type;
8294 struct attribute *attr;
8295 LONGEST low = 0;
8296 LONGEST high = -1;
8297 char *name;
8298 LONGEST negative_mask;
8299
8300 base_type = die_type (die, cu);
8301 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8302 check_typedef (base_type);
8303
8304 /* The die_type call above may have already set the type for this DIE. */
8305 range_type = get_die_type (die, cu);
8306 if (range_type)
8307 return range_type;
8308
8309 if (cu->language == language_fortran)
8310 {
8311 /* FORTRAN implies a lower bound of 1, if not given. */
8312 low = 1;
8313 }
8314
8315 /* FIXME: For variable sized arrays either of these could be
8316 a variable rather than a constant value. We'll allow it,
8317 but we don't know how to handle it. */
8318 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8319 if (attr)
8320 low = dwarf2_get_attr_constant_value (attr, 0);
8321
8322 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8323 if (attr)
8324 {
8325 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8326 {
8327 /* GCC encodes arrays with unspecified or dynamic length
8328 with a DW_FORM_block1 attribute or a reference attribute.
8329 FIXME: GDB does not yet know how to handle dynamic
8330 arrays properly, treat them as arrays with unspecified
8331 length for now.
8332
8333 FIXME: jimb/2003-09-22: GDB does not really know
8334 how to handle arrays of unspecified length
8335 either; we just represent them as zero-length
8336 arrays. Choose an appropriate upper bound given
8337 the lower bound we've computed above. */
8338 high = low - 1;
8339 }
8340 else
8341 high = dwarf2_get_attr_constant_value (attr, 1);
8342 }
8343 else
8344 {
8345 attr = dwarf2_attr (die, DW_AT_count, cu);
8346 if (attr)
8347 {
8348 int count = dwarf2_get_attr_constant_value (attr, 1);
8349 high = low + count - 1;
8350 }
8351 else
8352 {
8353 /* Unspecified array length. */
8354 high = low - 1;
8355 }
8356 }
8357
8358 /* Dwarf-2 specifications explicitly allows to create subrange types
8359 without specifying a base type.
8360 In that case, the base type must be set to the type of
8361 the lower bound, upper bound or count, in that order, if any of these
8362 three attributes references an object that has a type.
8363 If no base type is found, the Dwarf-2 specifications say that
8364 a signed integer type of size equal to the size of an address should
8365 be used.
8366 For the following C code: `extern char gdb_int [];'
8367 GCC produces an empty range DIE.
8368 FIXME: muller/2010-05-28: Possible references to object for low bound,
8369 high bound or count are not yet handled by this code. */
8370 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8371 {
8372 struct objfile *objfile = cu->objfile;
8373 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8374 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8375 struct type *int_type = objfile_type (objfile)->builtin_int;
8376
8377 /* Test "int", "long int", and "long long int" objfile types,
8378 and select the first one having a size above or equal to the
8379 architecture address size. */
8380 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8381 base_type = int_type;
8382 else
8383 {
8384 int_type = objfile_type (objfile)->builtin_long;
8385 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8386 base_type = int_type;
8387 else
8388 {
8389 int_type = objfile_type (objfile)->builtin_long_long;
8390 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8391 base_type = int_type;
8392 }
8393 }
8394 }
8395
8396 negative_mask =
8397 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8398 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8399 low |= negative_mask;
8400 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8401 high |= negative_mask;
8402
8403 range_type = create_range_type (NULL, base_type, low, high);
8404
8405 /* Mark arrays with dynamic length at least as an array of unspecified
8406 length. GDB could check the boundary but before it gets implemented at
8407 least allow accessing the array elements. */
8408 if (attr && attr->form == DW_FORM_block1)
8409 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8410
8411 /* Ada expects an empty array on no boundary attributes. */
8412 if (attr == NULL && cu->language != language_ada)
8413 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8414
8415 name = dwarf2_name (die, cu);
8416 if (name)
8417 TYPE_NAME (range_type) = name;
8418
8419 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8420 if (attr)
8421 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8422
8423 set_die_type (die, range_type, cu);
8424
8425 /* set_die_type should be already done. */
8426 set_descriptive_type (range_type, die, cu);
8427
8428 return range_type;
8429 }
8430
8431 static struct type *
8432 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8433 {
8434 struct type *type;
8435
8436 /* For now, we only support the C meaning of an unspecified type: void. */
8437
8438 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8439 TYPE_NAME (type) = dwarf2_name (die, cu);
8440
8441 return set_die_type (die, type, cu);
8442 }
8443
8444 /* Trivial hash function for die_info: the hash value of a DIE
8445 is its offset in .debug_info for this objfile. */
8446
8447 static hashval_t
8448 die_hash (const void *item)
8449 {
8450 const struct die_info *die = item;
8451
8452 return die->offset;
8453 }
8454
8455 /* Trivial comparison function for die_info structures: two DIEs
8456 are equal if they have the same offset. */
8457
8458 static int
8459 die_eq (const void *item_lhs, const void *item_rhs)
8460 {
8461 const struct die_info *die_lhs = item_lhs;
8462 const struct die_info *die_rhs = item_rhs;
8463
8464 return die_lhs->offset == die_rhs->offset;
8465 }
8466
8467 /* Read a whole compilation unit into a linked list of dies. */
8468
8469 static struct die_info *
8470 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8471 {
8472 struct die_reader_specs reader_specs;
8473 int read_abbrevs = 0;
8474 struct cleanup *back_to = NULL;
8475 struct die_info *die;
8476
8477 if (cu->dwarf2_abbrevs == NULL)
8478 {
8479 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8480 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8481 read_abbrevs = 1;
8482 }
8483
8484 gdb_assert (cu->die_hash == NULL);
8485 cu->die_hash
8486 = htab_create_alloc_ex (cu->header.length / 12,
8487 die_hash,
8488 die_eq,
8489 NULL,
8490 &cu->comp_unit_obstack,
8491 hashtab_obstack_allocate,
8492 dummy_obstack_deallocate);
8493
8494 init_cu_die_reader (&reader_specs, cu);
8495
8496 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8497
8498 if (read_abbrevs)
8499 do_cleanups (back_to);
8500
8501 return die;
8502 }
8503
8504 /* Main entry point for reading a DIE and all children.
8505 Read the DIE and dump it if requested. */
8506
8507 static struct die_info *
8508 read_die_and_children (const struct die_reader_specs *reader,
8509 gdb_byte *info_ptr,
8510 gdb_byte **new_info_ptr,
8511 struct die_info *parent)
8512 {
8513 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8514 new_info_ptr, parent);
8515
8516 if (dwarf2_die_debug)
8517 {
8518 fprintf_unfiltered (gdb_stdlog,
8519 "\nRead die from %s of %s:\n",
8520 reader->buffer == dwarf2_per_objfile->info.buffer
8521 ? ".debug_info"
8522 : reader->buffer == dwarf2_per_objfile->types.buffer
8523 ? ".debug_types"
8524 : "unknown section",
8525 reader->abfd->filename);
8526 dump_die (result, dwarf2_die_debug);
8527 }
8528
8529 return result;
8530 }
8531
8532 /* Read a single die and all its descendents. Set the die's sibling
8533 field to NULL; set other fields in the die correctly, and set all
8534 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8535 location of the info_ptr after reading all of those dies. PARENT
8536 is the parent of the die in question. */
8537
8538 static struct die_info *
8539 read_die_and_children_1 (const struct die_reader_specs *reader,
8540 gdb_byte *info_ptr,
8541 gdb_byte **new_info_ptr,
8542 struct die_info *parent)
8543 {
8544 struct die_info *die;
8545 gdb_byte *cur_ptr;
8546 int has_children;
8547
8548 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8549 if (die == NULL)
8550 {
8551 *new_info_ptr = cur_ptr;
8552 return NULL;
8553 }
8554 store_in_ref_table (die, reader->cu);
8555
8556 if (has_children)
8557 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8558 else
8559 {
8560 die->child = NULL;
8561 *new_info_ptr = cur_ptr;
8562 }
8563
8564 die->sibling = NULL;
8565 die->parent = parent;
8566 return die;
8567 }
8568
8569 /* Read a die, all of its descendents, and all of its siblings; set
8570 all of the fields of all of the dies correctly. Arguments are as
8571 in read_die_and_children. */
8572
8573 static struct die_info *
8574 read_die_and_siblings (const struct die_reader_specs *reader,
8575 gdb_byte *info_ptr,
8576 gdb_byte **new_info_ptr,
8577 struct die_info *parent)
8578 {
8579 struct die_info *first_die, *last_sibling;
8580 gdb_byte *cur_ptr;
8581
8582 cur_ptr = info_ptr;
8583 first_die = last_sibling = NULL;
8584
8585 while (1)
8586 {
8587 struct die_info *die
8588 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8589
8590 if (die == NULL)
8591 {
8592 *new_info_ptr = cur_ptr;
8593 return first_die;
8594 }
8595
8596 if (!first_die)
8597 first_die = die;
8598 else
8599 last_sibling->sibling = die;
8600
8601 last_sibling = die;
8602 }
8603 }
8604
8605 /* Read the die from the .debug_info section buffer. Set DIEP to
8606 point to a newly allocated die with its information, except for its
8607 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8608 whether the die has children or not. */
8609
8610 static gdb_byte *
8611 read_full_die (const struct die_reader_specs *reader,
8612 struct die_info **diep, gdb_byte *info_ptr,
8613 int *has_children)
8614 {
8615 unsigned int abbrev_number, bytes_read, i, offset;
8616 struct abbrev_info *abbrev;
8617 struct die_info *die;
8618 struct dwarf2_cu *cu = reader->cu;
8619 bfd *abfd = reader->abfd;
8620
8621 offset = info_ptr - reader->buffer;
8622 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8623 info_ptr += bytes_read;
8624 if (!abbrev_number)
8625 {
8626 *diep = NULL;
8627 *has_children = 0;
8628 return info_ptr;
8629 }
8630
8631 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8632 if (!abbrev)
8633 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8634 abbrev_number,
8635 bfd_get_filename (abfd));
8636
8637 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8638 die->offset = offset;
8639 die->tag = abbrev->tag;
8640 die->abbrev = abbrev_number;
8641
8642 die->num_attrs = abbrev->num_attrs;
8643
8644 for (i = 0; i < abbrev->num_attrs; ++i)
8645 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8646 abfd, info_ptr, cu);
8647
8648 *diep = die;
8649 *has_children = abbrev->has_children;
8650 return info_ptr;
8651 }
8652
8653 /* In DWARF version 2, the description of the debugging information is
8654 stored in a separate .debug_abbrev section. Before we read any
8655 dies from a section we read in all abbreviations and install them
8656 in a hash table. This function also sets flags in CU describing
8657 the data found in the abbrev table. */
8658
8659 static void
8660 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8661 {
8662 struct comp_unit_head *cu_header = &cu->header;
8663 gdb_byte *abbrev_ptr;
8664 struct abbrev_info *cur_abbrev;
8665 unsigned int abbrev_number, bytes_read, abbrev_name;
8666 unsigned int abbrev_form, hash_number;
8667 struct attr_abbrev *cur_attrs;
8668 unsigned int allocated_attrs;
8669
8670 /* Initialize dwarf2 abbrevs. */
8671 obstack_init (&cu->abbrev_obstack);
8672 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8673 (ABBREV_HASH_SIZE
8674 * sizeof (struct abbrev_info *)));
8675 memset (cu->dwarf2_abbrevs, 0,
8676 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8677
8678 dwarf2_read_section (dwarf2_per_objfile->objfile,
8679 &dwarf2_per_objfile->abbrev);
8680 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8681 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8682 abbrev_ptr += bytes_read;
8683
8684 allocated_attrs = ATTR_ALLOC_CHUNK;
8685 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8686
8687 /* Loop until we reach an abbrev number of 0. */
8688 while (abbrev_number)
8689 {
8690 cur_abbrev = dwarf_alloc_abbrev (cu);
8691
8692 /* read in abbrev header */
8693 cur_abbrev->number = abbrev_number;
8694 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8695 abbrev_ptr += bytes_read;
8696 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8697 abbrev_ptr += 1;
8698
8699 if (cur_abbrev->tag == DW_TAG_namespace)
8700 cu->has_namespace_info = 1;
8701
8702 /* now read in declarations */
8703 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8704 abbrev_ptr += bytes_read;
8705 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8706 abbrev_ptr += bytes_read;
8707 while (abbrev_name)
8708 {
8709 if (cur_abbrev->num_attrs == allocated_attrs)
8710 {
8711 allocated_attrs += ATTR_ALLOC_CHUNK;
8712 cur_attrs
8713 = xrealloc (cur_attrs, (allocated_attrs
8714 * sizeof (struct attr_abbrev)));
8715 }
8716
8717 /* Record whether this compilation unit might have
8718 inter-compilation-unit references. If we don't know what form
8719 this attribute will have, then it might potentially be a
8720 DW_FORM_ref_addr, so we conservatively expect inter-CU
8721 references. */
8722
8723 if (abbrev_form == DW_FORM_ref_addr
8724 || abbrev_form == DW_FORM_indirect)
8725 cu->has_form_ref_addr = 1;
8726
8727 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8728 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8729 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8730 abbrev_ptr += bytes_read;
8731 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8732 abbrev_ptr += bytes_read;
8733 }
8734
8735 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8736 (cur_abbrev->num_attrs
8737 * sizeof (struct attr_abbrev)));
8738 memcpy (cur_abbrev->attrs, cur_attrs,
8739 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8740
8741 hash_number = abbrev_number % ABBREV_HASH_SIZE;
8742 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8743 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8744
8745 /* Get next abbreviation.
8746 Under Irix6 the abbreviations for a compilation unit are not
8747 always properly terminated with an abbrev number of 0.
8748 Exit loop if we encounter an abbreviation which we have
8749 already read (which means we are about to read the abbreviations
8750 for the next compile unit) or if the end of the abbreviation
8751 table is reached. */
8752 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8753 >= dwarf2_per_objfile->abbrev.size)
8754 break;
8755 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8756 abbrev_ptr += bytes_read;
8757 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8758 break;
8759 }
8760
8761 xfree (cur_attrs);
8762 }
8763
8764 /* Release the memory used by the abbrev table for a compilation unit. */
8765
8766 static void
8767 dwarf2_free_abbrev_table (void *ptr_to_cu)
8768 {
8769 struct dwarf2_cu *cu = ptr_to_cu;
8770
8771 obstack_free (&cu->abbrev_obstack, NULL);
8772 cu->dwarf2_abbrevs = NULL;
8773 }
8774
8775 /* Lookup an abbrev_info structure in the abbrev hash table. */
8776
8777 static struct abbrev_info *
8778 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8779 {
8780 unsigned int hash_number;
8781 struct abbrev_info *abbrev;
8782
8783 hash_number = number % ABBREV_HASH_SIZE;
8784 abbrev = cu->dwarf2_abbrevs[hash_number];
8785
8786 while (abbrev)
8787 {
8788 if (abbrev->number == number)
8789 return abbrev;
8790 else
8791 abbrev = abbrev->next;
8792 }
8793 return NULL;
8794 }
8795
8796 /* Returns nonzero if TAG represents a type that we might generate a partial
8797 symbol for. */
8798
8799 static int
8800 is_type_tag_for_partial (int tag)
8801 {
8802 switch (tag)
8803 {
8804 #if 0
8805 /* Some types that would be reasonable to generate partial symbols for,
8806 that we don't at present. */
8807 case DW_TAG_array_type:
8808 case DW_TAG_file_type:
8809 case DW_TAG_ptr_to_member_type:
8810 case DW_TAG_set_type:
8811 case DW_TAG_string_type:
8812 case DW_TAG_subroutine_type:
8813 #endif
8814 case DW_TAG_base_type:
8815 case DW_TAG_class_type:
8816 case DW_TAG_interface_type:
8817 case DW_TAG_enumeration_type:
8818 case DW_TAG_structure_type:
8819 case DW_TAG_subrange_type:
8820 case DW_TAG_typedef:
8821 case DW_TAG_union_type:
8822 return 1;
8823 default:
8824 return 0;
8825 }
8826 }
8827
8828 /* Load all DIEs that are interesting for partial symbols into memory. */
8829
8830 static struct partial_die_info *
8831 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8832 int building_psymtab, struct dwarf2_cu *cu)
8833 {
8834 struct partial_die_info *part_die;
8835 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8836 struct abbrev_info *abbrev;
8837 unsigned int bytes_read;
8838 unsigned int load_all = 0;
8839
8840 int nesting_level = 1;
8841
8842 parent_die = NULL;
8843 last_die = NULL;
8844
8845 if (cu->per_cu && cu->per_cu->load_all_dies)
8846 load_all = 1;
8847
8848 cu->partial_dies
8849 = htab_create_alloc_ex (cu->header.length / 12,
8850 partial_die_hash,
8851 partial_die_eq,
8852 NULL,
8853 &cu->comp_unit_obstack,
8854 hashtab_obstack_allocate,
8855 dummy_obstack_deallocate);
8856
8857 part_die = obstack_alloc (&cu->comp_unit_obstack,
8858 sizeof (struct partial_die_info));
8859
8860 while (1)
8861 {
8862 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8863
8864 /* A NULL abbrev means the end of a series of children. */
8865 if (abbrev == NULL)
8866 {
8867 if (--nesting_level == 0)
8868 {
8869 /* PART_DIE was probably the last thing allocated on the
8870 comp_unit_obstack, so we could call obstack_free
8871 here. We don't do that because the waste is small,
8872 and will be cleaned up when we're done with this
8873 compilation unit. This way, we're also more robust
8874 against other users of the comp_unit_obstack. */
8875 return first_die;
8876 }
8877 info_ptr += bytes_read;
8878 last_die = parent_die;
8879 parent_die = parent_die->die_parent;
8880 continue;
8881 }
8882
8883 /* Check for template arguments. We never save these; if
8884 they're seen, we just mark the parent, and go on our way. */
8885 if (parent_die != NULL
8886 && cu->language == language_cplus
8887 && (abbrev->tag == DW_TAG_template_type_param
8888 || abbrev->tag == DW_TAG_template_value_param))
8889 {
8890 parent_die->has_template_arguments = 1;
8891
8892 if (!load_all)
8893 {
8894 /* We don't need a partial DIE for the template argument. */
8895 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8896 cu);
8897 continue;
8898 }
8899 }
8900
8901 /* We only recurse into subprograms looking for template arguments.
8902 Skip their other children. */
8903 if (!load_all
8904 && cu->language == language_cplus
8905 && parent_die != NULL
8906 && parent_die->tag == DW_TAG_subprogram)
8907 {
8908 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8909 continue;
8910 }
8911
8912 /* Check whether this DIE is interesting enough to save. Normally
8913 we would not be interested in members here, but there may be
8914 later variables referencing them via DW_AT_specification (for
8915 static members). */
8916 if (!load_all
8917 && !is_type_tag_for_partial (abbrev->tag)
8918 && abbrev->tag != DW_TAG_constant
8919 && abbrev->tag != DW_TAG_enumerator
8920 && abbrev->tag != DW_TAG_subprogram
8921 && abbrev->tag != DW_TAG_lexical_block
8922 && abbrev->tag != DW_TAG_variable
8923 && abbrev->tag != DW_TAG_namespace
8924 && abbrev->tag != DW_TAG_module
8925 && abbrev->tag != DW_TAG_member)
8926 {
8927 /* Otherwise we skip to the next sibling, if any. */
8928 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8929 continue;
8930 }
8931
8932 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8933 buffer, info_ptr, cu);
8934
8935 /* This two-pass algorithm for processing partial symbols has a
8936 high cost in cache pressure. Thus, handle some simple cases
8937 here which cover the majority of C partial symbols. DIEs
8938 which neither have specification tags in them, nor could have
8939 specification tags elsewhere pointing at them, can simply be
8940 processed and discarded.
8941
8942 This segment is also optional; scan_partial_symbols and
8943 add_partial_symbol will handle these DIEs if we chain
8944 them in normally. When compilers which do not emit large
8945 quantities of duplicate debug information are more common,
8946 this code can probably be removed. */
8947
8948 /* Any complete simple types at the top level (pretty much all
8949 of them, for a language without namespaces), can be processed
8950 directly. */
8951 if (parent_die == NULL
8952 && part_die->has_specification == 0
8953 && part_die->is_declaration == 0
8954 && (part_die->tag == DW_TAG_typedef
8955 || part_die->tag == DW_TAG_base_type
8956 || part_die->tag == DW_TAG_subrange_type))
8957 {
8958 if (building_psymtab && part_die->name != NULL)
8959 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8960 VAR_DOMAIN, LOC_TYPEDEF,
8961 &cu->objfile->static_psymbols,
8962 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8963 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8964 continue;
8965 }
8966
8967 /* If we're at the second level, and we're an enumerator, and
8968 our parent has no specification (meaning possibly lives in a
8969 namespace elsewhere), then we can add the partial symbol now
8970 instead of queueing it. */
8971 if (part_die->tag == DW_TAG_enumerator
8972 && parent_die != NULL
8973 && parent_die->die_parent == NULL
8974 && parent_die->tag == DW_TAG_enumeration_type
8975 && parent_die->has_specification == 0)
8976 {
8977 if (part_die->name == NULL)
8978 complaint (&symfile_complaints,
8979 _("malformed enumerator DIE ignored"));
8980 else if (building_psymtab)
8981 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8982 VAR_DOMAIN, LOC_CONST,
8983 (cu->language == language_cplus
8984 || cu->language == language_java)
8985 ? &cu->objfile->global_psymbols
8986 : &cu->objfile->static_psymbols,
8987 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8988
8989 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8990 continue;
8991 }
8992
8993 /* We'll save this DIE so link it in. */
8994 part_die->die_parent = parent_die;
8995 part_die->die_sibling = NULL;
8996 part_die->die_child = NULL;
8997
8998 if (last_die && last_die == parent_die)
8999 last_die->die_child = part_die;
9000 else if (last_die)
9001 last_die->die_sibling = part_die;
9002
9003 last_die = part_die;
9004
9005 if (first_die == NULL)
9006 first_die = part_die;
9007
9008 /* Maybe add the DIE to the hash table. Not all DIEs that we
9009 find interesting need to be in the hash table, because we
9010 also have the parent/sibling/child chains; only those that we
9011 might refer to by offset later during partial symbol reading.
9012
9013 For now this means things that might have be the target of a
9014 DW_AT_specification, DW_AT_abstract_origin, or
9015 DW_AT_extension. DW_AT_extension will refer only to
9016 namespaces; DW_AT_abstract_origin refers to functions (and
9017 many things under the function DIE, but we do not recurse
9018 into function DIEs during partial symbol reading) and
9019 possibly variables as well; DW_AT_specification refers to
9020 declarations. Declarations ought to have the DW_AT_declaration
9021 flag. It happens that GCC forgets to put it in sometimes, but
9022 only for functions, not for types.
9023
9024 Adding more things than necessary to the hash table is harmless
9025 except for the performance cost. Adding too few will result in
9026 wasted time in find_partial_die, when we reread the compilation
9027 unit with load_all_dies set. */
9028
9029 if (load_all
9030 || abbrev->tag == DW_TAG_constant
9031 || abbrev->tag == DW_TAG_subprogram
9032 || abbrev->tag == DW_TAG_variable
9033 || abbrev->tag == DW_TAG_namespace
9034 || part_die->is_declaration)
9035 {
9036 void **slot;
9037
9038 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9039 part_die->offset, INSERT);
9040 *slot = part_die;
9041 }
9042
9043 part_die = obstack_alloc (&cu->comp_unit_obstack,
9044 sizeof (struct partial_die_info));
9045
9046 /* For some DIEs we want to follow their children (if any). For C
9047 we have no reason to follow the children of structures; for other
9048 languages we have to, so that we can get at method physnames
9049 to infer fully qualified class names, for DW_AT_specification,
9050 and for C++ template arguments. For C++, we also look one level
9051 inside functions to find template arguments (if the name of the
9052 function does not already contain the template arguments).
9053
9054 For Ada, we need to scan the children of subprograms and lexical
9055 blocks as well because Ada allows the definition of nested
9056 entities that could be interesting for the debugger, such as
9057 nested subprograms for instance. */
9058 if (last_die->has_children
9059 && (load_all
9060 || last_die->tag == DW_TAG_namespace
9061 || last_die->tag == DW_TAG_module
9062 || last_die->tag == DW_TAG_enumeration_type
9063 || (cu->language == language_cplus
9064 && last_die->tag == DW_TAG_subprogram
9065 && (last_die->name == NULL
9066 || strchr (last_die->name, '<') == NULL))
9067 || (cu->language != language_c
9068 && (last_die->tag == DW_TAG_class_type
9069 || last_die->tag == DW_TAG_interface_type
9070 || last_die->tag == DW_TAG_structure_type
9071 || last_die->tag == DW_TAG_union_type))
9072 || (cu->language == language_ada
9073 && (last_die->tag == DW_TAG_subprogram
9074 || last_die->tag == DW_TAG_lexical_block))))
9075 {
9076 nesting_level++;
9077 parent_die = last_die;
9078 continue;
9079 }
9080
9081 /* Otherwise we skip to the next sibling, if any. */
9082 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9083
9084 /* Back to the top, do it again. */
9085 }
9086 }
9087
9088 /* Read a minimal amount of information into the minimal die structure. */
9089
9090 static gdb_byte *
9091 read_partial_die (struct partial_die_info *part_die,
9092 struct abbrev_info *abbrev,
9093 unsigned int abbrev_len, bfd *abfd,
9094 gdb_byte *buffer, gdb_byte *info_ptr,
9095 struct dwarf2_cu *cu)
9096 {
9097 unsigned int i;
9098 struct attribute attr;
9099 int has_low_pc_attr = 0;
9100 int has_high_pc_attr = 0;
9101
9102 memset (part_die, 0, sizeof (struct partial_die_info));
9103
9104 part_die->offset = info_ptr - buffer;
9105
9106 info_ptr += abbrev_len;
9107
9108 if (abbrev == NULL)
9109 return info_ptr;
9110
9111 part_die->tag = abbrev->tag;
9112 part_die->has_children = abbrev->has_children;
9113
9114 for (i = 0; i < abbrev->num_attrs; ++i)
9115 {
9116 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9117
9118 /* Store the data if it is of an attribute we want to keep in a
9119 partial symbol table. */
9120 switch (attr.name)
9121 {
9122 case DW_AT_name:
9123 switch (part_die->tag)
9124 {
9125 case DW_TAG_compile_unit:
9126 case DW_TAG_type_unit:
9127 /* Compilation units have a DW_AT_name that is a filename, not
9128 a source language identifier. */
9129 case DW_TAG_enumeration_type:
9130 case DW_TAG_enumerator:
9131 /* These tags always have simple identifiers already; no need
9132 to canonicalize them. */
9133 part_die->name = DW_STRING (&attr);
9134 break;
9135 default:
9136 part_die->name
9137 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9138 &cu->objfile->objfile_obstack);
9139 break;
9140 }
9141 break;
9142 case DW_AT_linkage_name:
9143 case DW_AT_MIPS_linkage_name:
9144 /* Note that both forms of linkage name might appear. We
9145 assume they will be the same, and we only store the last
9146 one we see. */
9147 if (cu->language == language_ada)
9148 part_die->name = DW_STRING (&attr);
9149 part_die->linkage_name = DW_STRING (&attr);
9150 break;
9151 case DW_AT_low_pc:
9152 has_low_pc_attr = 1;
9153 part_die->lowpc = DW_ADDR (&attr);
9154 break;
9155 case DW_AT_high_pc:
9156 has_high_pc_attr = 1;
9157 part_die->highpc = DW_ADDR (&attr);
9158 break;
9159 case DW_AT_location:
9160 /* Support the .debug_loc offsets. */
9161 if (attr_form_is_block (&attr))
9162 {
9163 part_die->locdesc = DW_BLOCK (&attr);
9164 }
9165 else if (attr_form_is_section_offset (&attr))
9166 {
9167 dwarf2_complex_location_expr_complaint ();
9168 }
9169 else
9170 {
9171 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9172 "partial symbol information");
9173 }
9174 break;
9175 case DW_AT_external:
9176 part_die->is_external = DW_UNSND (&attr);
9177 break;
9178 case DW_AT_declaration:
9179 part_die->is_declaration = DW_UNSND (&attr);
9180 break;
9181 case DW_AT_type:
9182 part_die->has_type = 1;
9183 break;
9184 case DW_AT_abstract_origin:
9185 case DW_AT_specification:
9186 case DW_AT_extension:
9187 part_die->has_specification = 1;
9188 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9189 break;
9190 case DW_AT_sibling:
9191 /* Ignore absolute siblings, they might point outside of
9192 the current compile unit. */
9193 if (attr.form == DW_FORM_ref_addr)
9194 complaint (&symfile_complaints,
9195 _("ignoring absolute DW_AT_sibling"));
9196 else
9197 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9198 break;
9199 case DW_AT_byte_size:
9200 part_die->has_byte_size = 1;
9201 break;
9202 case DW_AT_calling_convention:
9203 /* DWARF doesn't provide a way to identify a program's source-level
9204 entry point. DW_AT_calling_convention attributes are only meant
9205 to describe functions' calling conventions.
9206
9207 However, because it's a necessary piece of information in
9208 Fortran, and because DW_CC_program is the only piece of debugging
9209 information whose definition refers to a 'main program' at all,
9210 several compilers have begun marking Fortran main programs with
9211 DW_CC_program --- even when those functions use the standard
9212 calling conventions.
9213
9214 So until DWARF specifies a way to provide this information and
9215 compilers pick up the new representation, we'll support this
9216 practice. */
9217 if (DW_UNSND (&attr) == DW_CC_program
9218 && cu->language == language_fortran)
9219 {
9220 set_main_name (part_die->name);
9221
9222 /* As this DIE has a static linkage the name would be difficult
9223 to look up later. */
9224 language_of_main = language_fortran;
9225 }
9226 break;
9227 default:
9228 break;
9229 }
9230 }
9231
9232 if (has_low_pc_attr && has_high_pc_attr)
9233 {
9234 /* When using the GNU linker, .gnu.linkonce. sections are used to
9235 eliminate duplicate copies of functions and vtables and such.
9236 The linker will arbitrarily choose one and discard the others.
9237 The AT_*_pc values for such functions refer to local labels in
9238 these sections. If the section from that file was discarded, the
9239 labels are not in the output, so the relocs get a value of 0.
9240 If this is a discarded function, mark the pc bounds as invalid,
9241 so that GDB will ignore it. */
9242 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9243 {
9244 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9245
9246 complaint (&symfile_complaints,
9247 _("DW_AT_low_pc %s is zero "
9248 "for DIE at 0x%x [in module %s]"),
9249 paddress (gdbarch, part_die->lowpc),
9250 part_die->offset, cu->objfile->name);
9251 }
9252 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9253 else if (part_die->lowpc >= part_die->highpc)
9254 {
9255 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9256
9257 complaint (&symfile_complaints,
9258 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9259 "for DIE at 0x%x [in module %s]"),
9260 paddress (gdbarch, part_die->lowpc),
9261 paddress (gdbarch, part_die->highpc),
9262 part_die->offset, cu->objfile->name);
9263 }
9264 else
9265 part_die->has_pc_info = 1;
9266 }
9267
9268 return info_ptr;
9269 }
9270
9271 /* Find a cached partial DIE at OFFSET in CU. */
9272
9273 static struct partial_die_info *
9274 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9275 {
9276 struct partial_die_info *lookup_die = NULL;
9277 struct partial_die_info part_die;
9278
9279 part_die.offset = offset;
9280 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9281
9282 return lookup_die;
9283 }
9284
9285 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9286 except in the case of .debug_types DIEs which do not reference
9287 outside their CU (they do however referencing other types via
9288 DW_FORM_ref_sig8). */
9289
9290 static struct partial_die_info *
9291 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9292 {
9293 struct dwarf2_per_cu_data *per_cu = NULL;
9294 struct partial_die_info *pd = NULL;
9295
9296 if (cu->per_cu->from_debug_types)
9297 {
9298 pd = find_partial_die_in_comp_unit (offset, cu);
9299 if (pd != NULL)
9300 return pd;
9301 goto not_found;
9302 }
9303
9304 if (offset_in_cu_p (&cu->header, offset))
9305 {
9306 pd = find_partial_die_in_comp_unit (offset, cu);
9307 if (pd != NULL)
9308 return pd;
9309 }
9310
9311 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9312
9313 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9314 load_partial_comp_unit (per_cu, cu->objfile);
9315
9316 per_cu->cu->last_used = 0;
9317 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9318
9319 if (pd == NULL && per_cu->load_all_dies == 0)
9320 {
9321 struct cleanup *back_to;
9322 struct partial_die_info comp_unit_die;
9323 struct abbrev_info *abbrev;
9324 unsigned int bytes_read;
9325 char *info_ptr;
9326
9327 per_cu->load_all_dies = 1;
9328
9329 /* Re-read the DIEs. */
9330 back_to = make_cleanup (null_cleanup, 0);
9331 if (per_cu->cu->dwarf2_abbrevs == NULL)
9332 {
9333 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9334 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9335 }
9336 info_ptr = (dwarf2_per_objfile->info.buffer
9337 + per_cu->cu->header.offset
9338 + per_cu->cu->header.first_die_offset);
9339 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9340 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9341 per_cu->cu->objfile->obfd,
9342 dwarf2_per_objfile->info.buffer, info_ptr,
9343 per_cu->cu);
9344 if (comp_unit_die.has_children)
9345 load_partial_dies (per_cu->cu->objfile->obfd,
9346 dwarf2_per_objfile->info.buffer, info_ptr,
9347 0, per_cu->cu);
9348 do_cleanups (back_to);
9349
9350 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9351 }
9352
9353 not_found:
9354
9355 if (pd == NULL)
9356 internal_error (__FILE__, __LINE__,
9357 _("could not find partial DIE 0x%x "
9358 "in cache [from module %s]\n"),
9359 offset, bfd_get_filename (cu->objfile->obfd));
9360 return pd;
9361 }
9362
9363 /* See if we can figure out if the class lives in a namespace. We do
9364 this by looking for a member function; its demangled name will
9365 contain namespace info, if there is any. */
9366
9367 static void
9368 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9369 struct dwarf2_cu *cu)
9370 {
9371 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9372 what template types look like, because the demangler
9373 frequently doesn't give the same name as the debug info. We
9374 could fix this by only using the demangled name to get the
9375 prefix (but see comment in read_structure_type). */
9376
9377 struct partial_die_info *real_pdi;
9378 struct partial_die_info *child_pdi;
9379
9380 /* If this DIE (this DIE's specification, if any) has a parent, then
9381 we should not do this. We'll prepend the parent's fully qualified
9382 name when we create the partial symbol. */
9383
9384 real_pdi = struct_pdi;
9385 while (real_pdi->has_specification)
9386 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9387
9388 if (real_pdi->die_parent != NULL)
9389 return;
9390
9391 for (child_pdi = struct_pdi->die_child;
9392 child_pdi != NULL;
9393 child_pdi = child_pdi->die_sibling)
9394 {
9395 if (child_pdi->tag == DW_TAG_subprogram
9396 && child_pdi->linkage_name != NULL)
9397 {
9398 char *actual_class_name
9399 = language_class_name_from_physname (cu->language_defn,
9400 child_pdi->linkage_name);
9401 if (actual_class_name != NULL)
9402 {
9403 struct_pdi->name
9404 = obsavestring (actual_class_name,
9405 strlen (actual_class_name),
9406 &cu->objfile->objfile_obstack);
9407 xfree (actual_class_name);
9408 }
9409 break;
9410 }
9411 }
9412 }
9413
9414 /* Adjust PART_DIE before generating a symbol for it. This function
9415 may set the is_external flag or change the DIE's name. */
9416
9417 static void
9418 fixup_partial_die (struct partial_die_info *part_die,
9419 struct dwarf2_cu *cu)
9420 {
9421 /* Once we've fixed up a die, there's no point in doing so again.
9422 This also avoids a memory leak if we were to call
9423 guess_partial_die_structure_name multiple times. */
9424 if (part_die->fixup_called)
9425 return;
9426
9427 /* If we found a reference attribute and the DIE has no name, try
9428 to find a name in the referred to DIE. */
9429
9430 if (part_die->name == NULL && part_die->has_specification)
9431 {
9432 struct partial_die_info *spec_die;
9433
9434 spec_die = find_partial_die (part_die->spec_offset, cu);
9435
9436 fixup_partial_die (spec_die, cu);
9437
9438 if (spec_die->name)
9439 {
9440 part_die->name = spec_die->name;
9441
9442 /* Copy DW_AT_external attribute if it is set. */
9443 if (spec_die->is_external)
9444 part_die->is_external = spec_die->is_external;
9445 }
9446 }
9447
9448 /* Set default names for some unnamed DIEs. */
9449
9450 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9451 part_die->name = "(anonymous namespace)";
9452
9453 /* If there is no parent die to provide a namespace, and there are
9454 children, see if we can determine the namespace from their linkage
9455 name.
9456 NOTE: We need to do this even if cu->has_namespace_info != 0.
9457 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9458 if (cu->language == language_cplus
9459 && dwarf2_per_objfile->types.asection != NULL
9460 && part_die->die_parent == NULL
9461 && part_die->has_children
9462 && (part_die->tag == DW_TAG_class_type
9463 || part_die->tag == DW_TAG_structure_type
9464 || part_die->tag == DW_TAG_union_type))
9465 guess_partial_die_structure_name (part_die, cu);
9466
9467 /* GCC might emit a nameless struct or union that has a linkage
9468 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9469 if (part_die->name == NULL
9470 && (part_die->tag == DW_TAG_structure_type
9471 || part_die->tag == DW_TAG_union_type
9472 || part_die->tag == DW_TAG_class_type)
9473 && part_die->linkage_name != NULL)
9474 {
9475 char *demangled;
9476
9477 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9478 if (demangled)
9479 {
9480 part_die->name = obsavestring (demangled, strlen (demangled),
9481 &cu->objfile->objfile_obstack);
9482 xfree (demangled);
9483 }
9484 }
9485
9486 part_die->fixup_called = 1;
9487 }
9488
9489 /* Read an attribute value described by an attribute form. */
9490
9491 static gdb_byte *
9492 read_attribute_value (struct attribute *attr, unsigned form,
9493 bfd *abfd, gdb_byte *info_ptr,
9494 struct dwarf2_cu *cu)
9495 {
9496 struct comp_unit_head *cu_header = &cu->header;
9497 unsigned int bytes_read;
9498 struct dwarf_block *blk;
9499
9500 attr->form = form;
9501 switch (form)
9502 {
9503 case DW_FORM_ref_addr:
9504 if (cu->header.version == 2)
9505 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9506 else
9507 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9508 &cu->header, &bytes_read);
9509 info_ptr += bytes_read;
9510 break;
9511 case DW_FORM_addr:
9512 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9513 info_ptr += bytes_read;
9514 break;
9515 case DW_FORM_block2:
9516 blk = dwarf_alloc_block (cu);
9517 blk->size = read_2_bytes (abfd, info_ptr);
9518 info_ptr += 2;
9519 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9520 info_ptr += blk->size;
9521 DW_BLOCK (attr) = blk;
9522 break;
9523 case DW_FORM_block4:
9524 blk = dwarf_alloc_block (cu);
9525 blk->size = read_4_bytes (abfd, info_ptr);
9526 info_ptr += 4;
9527 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9528 info_ptr += blk->size;
9529 DW_BLOCK (attr) = blk;
9530 break;
9531 case DW_FORM_data2:
9532 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9533 info_ptr += 2;
9534 break;
9535 case DW_FORM_data4:
9536 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9537 info_ptr += 4;
9538 break;
9539 case DW_FORM_data8:
9540 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9541 info_ptr += 8;
9542 break;
9543 case DW_FORM_sec_offset:
9544 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9545 info_ptr += bytes_read;
9546 break;
9547 case DW_FORM_string:
9548 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9549 DW_STRING_IS_CANONICAL (attr) = 0;
9550 info_ptr += bytes_read;
9551 break;
9552 case DW_FORM_strp:
9553 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9554 &bytes_read);
9555 DW_STRING_IS_CANONICAL (attr) = 0;
9556 info_ptr += bytes_read;
9557 break;
9558 case DW_FORM_exprloc:
9559 case DW_FORM_block:
9560 blk = dwarf_alloc_block (cu);
9561 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9562 info_ptr += bytes_read;
9563 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9564 info_ptr += blk->size;
9565 DW_BLOCK (attr) = blk;
9566 break;
9567 case DW_FORM_block1:
9568 blk = dwarf_alloc_block (cu);
9569 blk->size = read_1_byte (abfd, info_ptr);
9570 info_ptr += 1;
9571 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9572 info_ptr += blk->size;
9573 DW_BLOCK (attr) = blk;
9574 break;
9575 case DW_FORM_data1:
9576 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9577 info_ptr += 1;
9578 break;
9579 case DW_FORM_flag:
9580 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9581 info_ptr += 1;
9582 break;
9583 case DW_FORM_flag_present:
9584 DW_UNSND (attr) = 1;
9585 break;
9586 case DW_FORM_sdata:
9587 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9588 info_ptr += bytes_read;
9589 break;
9590 case DW_FORM_udata:
9591 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9592 info_ptr += bytes_read;
9593 break;
9594 case DW_FORM_ref1:
9595 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9596 info_ptr += 1;
9597 break;
9598 case DW_FORM_ref2:
9599 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9600 info_ptr += 2;
9601 break;
9602 case DW_FORM_ref4:
9603 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9604 info_ptr += 4;
9605 break;
9606 case DW_FORM_ref8:
9607 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9608 info_ptr += 8;
9609 break;
9610 case DW_FORM_ref_sig8:
9611 /* Convert the signature to something we can record in DW_UNSND
9612 for later lookup.
9613 NOTE: This is NULL if the type wasn't found. */
9614 DW_SIGNATURED_TYPE (attr) =
9615 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9616 info_ptr += 8;
9617 break;
9618 case DW_FORM_ref_udata:
9619 DW_ADDR (attr) = (cu->header.offset
9620 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9621 info_ptr += bytes_read;
9622 break;
9623 case DW_FORM_indirect:
9624 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9625 info_ptr += bytes_read;
9626 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9627 break;
9628 default:
9629 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9630 dwarf_form_name (form),
9631 bfd_get_filename (abfd));
9632 }
9633
9634 /* We have seen instances where the compiler tried to emit a byte
9635 size attribute of -1 which ended up being encoded as an unsigned
9636 0xffffffff. Although 0xffffffff is technically a valid size value,
9637 an object of this size seems pretty unlikely so we can relatively
9638 safely treat these cases as if the size attribute was invalid and
9639 treat them as zero by default. */
9640 if (attr->name == DW_AT_byte_size
9641 && form == DW_FORM_data4
9642 && DW_UNSND (attr) >= 0xffffffff)
9643 {
9644 complaint
9645 (&symfile_complaints,
9646 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9647 hex_string (DW_UNSND (attr)));
9648 DW_UNSND (attr) = 0;
9649 }
9650
9651 return info_ptr;
9652 }
9653
9654 /* Read an attribute described by an abbreviated attribute. */
9655
9656 static gdb_byte *
9657 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9658 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9659 {
9660 attr->name = abbrev->name;
9661 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9662 }
9663
9664 /* Read dwarf information from a buffer. */
9665
9666 static unsigned int
9667 read_1_byte (bfd *abfd, gdb_byte *buf)
9668 {
9669 return bfd_get_8 (abfd, buf);
9670 }
9671
9672 static int
9673 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9674 {
9675 return bfd_get_signed_8 (abfd, buf);
9676 }
9677
9678 static unsigned int
9679 read_2_bytes (bfd *abfd, gdb_byte *buf)
9680 {
9681 return bfd_get_16 (abfd, buf);
9682 }
9683
9684 static int
9685 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9686 {
9687 return bfd_get_signed_16 (abfd, buf);
9688 }
9689
9690 static unsigned int
9691 read_4_bytes (bfd *abfd, gdb_byte *buf)
9692 {
9693 return bfd_get_32 (abfd, buf);
9694 }
9695
9696 static int
9697 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9698 {
9699 return bfd_get_signed_32 (abfd, buf);
9700 }
9701
9702 static ULONGEST
9703 read_8_bytes (bfd *abfd, gdb_byte *buf)
9704 {
9705 return bfd_get_64 (abfd, buf);
9706 }
9707
9708 static CORE_ADDR
9709 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9710 unsigned int *bytes_read)
9711 {
9712 struct comp_unit_head *cu_header = &cu->header;
9713 CORE_ADDR retval = 0;
9714
9715 if (cu_header->signed_addr_p)
9716 {
9717 switch (cu_header->addr_size)
9718 {
9719 case 2:
9720 retval = bfd_get_signed_16 (abfd, buf);
9721 break;
9722 case 4:
9723 retval = bfd_get_signed_32 (abfd, buf);
9724 break;
9725 case 8:
9726 retval = bfd_get_signed_64 (abfd, buf);
9727 break;
9728 default:
9729 internal_error (__FILE__, __LINE__,
9730 _("read_address: bad switch, signed [in module %s]"),
9731 bfd_get_filename (abfd));
9732 }
9733 }
9734 else
9735 {
9736 switch (cu_header->addr_size)
9737 {
9738 case 2:
9739 retval = bfd_get_16 (abfd, buf);
9740 break;
9741 case 4:
9742 retval = bfd_get_32 (abfd, buf);
9743 break;
9744 case 8:
9745 retval = bfd_get_64 (abfd, buf);
9746 break;
9747 default:
9748 internal_error (__FILE__, __LINE__,
9749 _("read_address: bad switch, "
9750 "unsigned [in module %s]"),
9751 bfd_get_filename (abfd));
9752 }
9753 }
9754
9755 *bytes_read = cu_header->addr_size;
9756 return retval;
9757 }
9758
9759 /* Read the initial length from a section. The (draft) DWARF 3
9760 specification allows the initial length to take up either 4 bytes
9761 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9762 bytes describe the length and all offsets will be 8 bytes in length
9763 instead of 4.
9764
9765 An older, non-standard 64-bit format is also handled by this
9766 function. The older format in question stores the initial length
9767 as an 8-byte quantity without an escape value. Lengths greater
9768 than 2^32 aren't very common which means that the initial 4 bytes
9769 is almost always zero. Since a length value of zero doesn't make
9770 sense for the 32-bit format, this initial zero can be considered to
9771 be an escape value which indicates the presence of the older 64-bit
9772 format. As written, the code can't detect (old format) lengths
9773 greater than 4GB. If it becomes necessary to handle lengths
9774 somewhat larger than 4GB, we could allow other small values (such
9775 as the non-sensical values of 1, 2, and 3) to also be used as
9776 escape values indicating the presence of the old format.
9777
9778 The value returned via bytes_read should be used to increment the
9779 relevant pointer after calling read_initial_length().
9780
9781 [ Note: read_initial_length() and read_offset() are based on the
9782 document entitled "DWARF Debugging Information Format", revision
9783 3, draft 8, dated November 19, 2001. This document was obtained
9784 from:
9785
9786 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9787
9788 This document is only a draft and is subject to change. (So beware.)
9789
9790 Details regarding the older, non-standard 64-bit format were
9791 determined empirically by examining 64-bit ELF files produced by
9792 the SGI toolchain on an IRIX 6.5 machine.
9793
9794 - Kevin, July 16, 2002
9795 ] */
9796
9797 static LONGEST
9798 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9799 {
9800 LONGEST length = bfd_get_32 (abfd, buf);
9801
9802 if (length == 0xffffffff)
9803 {
9804 length = bfd_get_64 (abfd, buf + 4);
9805 *bytes_read = 12;
9806 }
9807 else if (length == 0)
9808 {
9809 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
9810 length = bfd_get_64 (abfd, buf);
9811 *bytes_read = 8;
9812 }
9813 else
9814 {
9815 *bytes_read = 4;
9816 }
9817
9818 return length;
9819 }
9820
9821 /* Cover function for read_initial_length.
9822 Returns the length of the object at BUF, and stores the size of the
9823 initial length in *BYTES_READ and stores the size that offsets will be in
9824 *OFFSET_SIZE.
9825 If the initial length size is not equivalent to that specified in
9826 CU_HEADER then issue a complaint.
9827 This is useful when reading non-comp-unit headers. */
9828
9829 static LONGEST
9830 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9831 const struct comp_unit_head *cu_header,
9832 unsigned int *bytes_read,
9833 unsigned int *offset_size)
9834 {
9835 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9836
9837 gdb_assert (cu_header->initial_length_size == 4
9838 || cu_header->initial_length_size == 8
9839 || cu_header->initial_length_size == 12);
9840
9841 if (cu_header->initial_length_size != *bytes_read)
9842 complaint (&symfile_complaints,
9843 _("intermixed 32-bit and 64-bit DWARF sections"));
9844
9845 *offset_size = (*bytes_read == 4) ? 4 : 8;
9846 return length;
9847 }
9848
9849 /* Read an offset from the data stream. The size of the offset is
9850 given by cu_header->offset_size. */
9851
9852 static LONGEST
9853 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9854 unsigned int *bytes_read)
9855 {
9856 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9857
9858 *bytes_read = cu_header->offset_size;
9859 return offset;
9860 }
9861
9862 /* Read an offset from the data stream. */
9863
9864 static LONGEST
9865 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9866 {
9867 LONGEST retval = 0;
9868
9869 switch (offset_size)
9870 {
9871 case 4:
9872 retval = bfd_get_32 (abfd, buf);
9873 break;
9874 case 8:
9875 retval = bfd_get_64 (abfd, buf);
9876 break;
9877 default:
9878 internal_error (__FILE__, __LINE__,
9879 _("read_offset_1: bad switch [in module %s]"),
9880 bfd_get_filename (abfd));
9881 }
9882
9883 return retval;
9884 }
9885
9886 static gdb_byte *
9887 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9888 {
9889 /* If the size of a host char is 8 bits, we can return a pointer
9890 to the buffer, otherwise we have to copy the data to a buffer
9891 allocated on the temporary obstack. */
9892 gdb_assert (HOST_CHAR_BIT == 8);
9893 return buf;
9894 }
9895
9896 static char *
9897 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9898 {
9899 /* If the size of a host char is 8 bits, we can return a pointer
9900 to the string, otherwise we have to copy the string to a buffer
9901 allocated on the temporary obstack. */
9902 gdb_assert (HOST_CHAR_BIT == 8);
9903 if (*buf == '\0')
9904 {
9905 *bytes_read_ptr = 1;
9906 return NULL;
9907 }
9908 *bytes_read_ptr = strlen ((char *) buf) + 1;
9909 return (char *) buf;
9910 }
9911
9912 static char *
9913 read_indirect_string (bfd *abfd, gdb_byte *buf,
9914 const struct comp_unit_head *cu_header,
9915 unsigned int *bytes_read_ptr)
9916 {
9917 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9918
9919 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9920 if (dwarf2_per_objfile->str.buffer == NULL)
9921 {
9922 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9923 bfd_get_filename (abfd));
9924 return NULL;
9925 }
9926 if (str_offset >= dwarf2_per_objfile->str.size)
9927 {
9928 error (_("DW_FORM_strp pointing outside of "
9929 ".debug_str section [in module %s]"),
9930 bfd_get_filename (abfd));
9931 return NULL;
9932 }
9933 gdb_assert (HOST_CHAR_BIT == 8);
9934 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9935 return NULL;
9936 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9937 }
9938
9939 static unsigned long
9940 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9941 {
9942 unsigned long result;
9943 unsigned int num_read;
9944 int i, shift;
9945 unsigned char byte;
9946
9947 result = 0;
9948 shift = 0;
9949 num_read = 0;
9950 i = 0;
9951 while (1)
9952 {
9953 byte = bfd_get_8 (abfd, buf);
9954 buf++;
9955 num_read++;
9956 result |= ((unsigned long)(byte & 127) << shift);
9957 if ((byte & 128) == 0)
9958 {
9959 break;
9960 }
9961 shift += 7;
9962 }
9963 *bytes_read_ptr = num_read;
9964 return result;
9965 }
9966
9967 static long
9968 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9969 {
9970 long result;
9971 int i, shift, num_read;
9972 unsigned char byte;
9973
9974 result = 0;
9975 shift = 0;
9976 num_read = 0;
9977 i = 0;
9978 while (1)
9979 {
9980 byte = bfd_get_8 (abfd, buf);
9981 buf++;
9982 num_read++;
9983 result |= ((long)(byte & 127) << shift);
9984 shift += 7;
9985 if ((byte & 128) == 0)
9986 {
9987 break;
9988 }
9989 }
9990 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9991 result |= -(((long)1) << shift);
9992 *bytes_read_ptr = num_read;
9993 return result;
9994 }
9995
9996 /* Return a pointer to just past the end of an LEB128 number in BUF. */
9997
9998 static gdb_byte *
9999 skip_leb128 (bfd *abfd, gdb_byte *buf)
10000 {
10001 int byte;
10002
10003 while (1)
10004 {
10005 byte = bfd_get_8 (abfd, buf);
10006 buf++;
10007 if ((byte & 128) == 0)
10008 return buf;
10009 }
10010 }
10011
10012 static void
10013 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10014 {
10015 switch (lang)
10016 {
10017 case DW_LANG_C89:
10018 case DW_LANG_C99:
10019 case DW_LANG_C:
10020 cu->language = language_c;
10021 break;
10022 case DW_LANG_C_plus_plus:
10023 cu->language = language_cplus;
10024 break;
10025 case DW_LANG_D:
10026 cu->language = language_d;
10027 break;
10028 case DW_LANG_Fortran77:
10029 case DW_LANG_Fortran90:
10030 case DW_LANG_Fortran95:
10031 cu->language = language_fortran;
10032 break;
10033 case DW_LANG_Mips_Assembler:
10034 cu->language = language_asm;
10035 break;
10036 case DW_LANG_Java:
10037 cu->language = language_java;
10038 break;
10039 case DW_LANG_Ada83:
10040 case DW_LANG_Ada95:
10041 cu->language = language_ada;
10042 break;
10043 case DW_LANG_Modula2:
10044 cu->language = language_m2;
10045 break;
10046 case DW_LANG_Pascal83:
10047 cu->language = language_pascal;
10048 break;
10049 case DW_LANG_ObjC:
10050 cu->language = language_objc;
10051 break;
10052 case DW_LANG_Cobol74:
10053 case DW_LANG_Cobol85:
10054 default:
10055 cu->language = language_minimal;
10056 break;
10057 }
10058 cu->language_defn = language_def (cu->language);
10059 }
10060
10061 /* Return the named attribute or NULL if not there. */
10062
10063 static struct attribute *
10064 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10065 {
10066 unsigned int i;
10067 struct attribute *spec = NULL;
10068
10069 for (i = 0; i < die->num_attrs; ++i)
10070 {
10071 if (die->attrs[i].name == name)
10072 return &die->attrs[i];
10073 if (die->attrs[i].name == DW_AT_specification
10074 || die->attrs[i].name == DW_AT_abstract_origin)
10075 spec = &die->attrs[i];
10076 }
10077
10078 if (spec)
10079 {
10080 die = follow_die_ref (die, spec, &cu);
10081 return dwarf2_attr (die, name, cu);
10082 }
10083
10084 return NULL;
10085 }
10086
10087 /* Return the named attribute or NULL if not there,
10088 but do not follow DW_AT_specification, etc.
10089 This is for use in contexts where we're reading .debug_types dies.
10090 Following DW_AT_specification, DW_AT_abstract_origin will take us
10091 back up the chain, and we want to go down. */
10092
10093 static struct attribute *
10094 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10095 struct dwarf2_cu *cu)
10096 {
10097 unsigned int i;
10098
10099 for (i = 0; i < die->num_attrs; ++i)
10100 if (die->attrs[i].name == name)
10101 return &die->attrs[i];
10102
10103 return NULL;
10104 }
10105
10106 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10107 and holds a non-zero value. This function should only be used for
10108 DW_FORM_flag or DW_FORM_flag_present attributes. */
10109
10110 static int
10111 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10112 {
10113 struct attribute *attr = dwarf2_attr (die, name, cu);
10114
10115 return (attr && DW_UNSND (attr));
10116 }
10117
10118 static int
10119 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10120 {
10121 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10122 which value is non-zero. However, we have to be careful with
10123 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10124 (via dwarf2_flag_true_p) follows this attribute. So we may
10125 end up accidently finding a declaration attribute that belongs
10126 to a different DIE referenced by the specification attribute,
10127 even though the given DIE does not have a declaration attribute. */
10128 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10129 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10130 }
10131
10132 /* Return the die giving the specification for DIE, if there is
10133 one. *SPEC_CU is the CU containing DIE on input, and the CU
10134 containing the return value on output. If there is no
10135 specification, but there is an abstract origin, that is
10136 returned. */
10137
10138 static struct die_info *
10139 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10140 {
10141 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10142 *spec_cu);
10143
10144 if (spec_attr == NULL)
10145 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10146
10147 if (spec_attr == NULL)
10148 return NULL;
10149 else
10150 return follow_die_ref (die, spec_attr, spec_cu);
10151 }
10152
10153 /* Free the line_header structure *LH, and any arrays and strings it
10154 refers to.
10155 NOTE: This is also used as a "cleanup" function. */
10156
10157 static void
10158 free_line_header (struct line_header *lh)
10159 {
10160 if (lh->standard_opcode_lengths)
10161 xfree (lh->standard_opcode_lengths);
10162
10163 /* Remember that all the lh->file_names[i].name pointers are
10164 pointers into debug_line_buffer, and don't need to be freed. */
10165 if (lh->file_names)
10166 xfree (lh->file_names);
10167
10168 /* Similarly for the include directory names. */
10169 if (lh->include_dirs)
10170 xfree (lh->include_dirs);
10171
10172 xfree (lh);
10173 }
10174
10175 /* Add an entry to LH's include directory table. */
10176
10177 static void
10178 add_include_dir (struct line_header *lh, char *include_dir)
10179 {
10180 /* Grow the array if necessary. */
10181 if (lh->include_dirs_size == 0)
10182 {
10183 lh->include_dirs_size = 1; /* for testing */
10184 lh->include_dirs = xmalloc (lh->include_dirs_size
10185 * sizeof (*lh->include_dirs));
10186 }
10187 else if (lh->num_include_dirs >= lh->include_dirs_size)
10188 {
10189 lh->include_dirs_size *= 2;
10190 lh->include_dirs = xrealloc (lh->include_dirs,
10191 (lh->include_dirs_size
10192 * sizeof (*lh->include_dirs)));
10193 }
10194
10195 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10196 }
10197
10198 /* Add an entry to LH's file name table. */
10199
10200 static void
10201 add_file_name (struct line_header *lh,
10202 char *name,
10203 unsigned int dir_index,
10204 unsigned int mod_time,
10205 unsigned int length)
10206 {
10207 struct file_entry *fe;
10208
10209 /* Grow the array if necessary. */
10210 if (lh->file_names_size == 0)
10211 {
10212 lh->file_names_size = 1; /* for testing */
10213 lh->file_names = xmalloc (lh->file_names_size
10214 * sizeof (*lh->file_names));
10215 }
10216 else if (lh->num_file_names >= lh->file_names_size)
10217 {
10218 lh->file_names_size *= 2;
10219 lh->file_names = xrealloc (lh->file_names,
10220 (lh->file_names_size
10221 * sizeof (*lh->file_names)));
10222 }
10223
10224 fe = &lh->file_names[lh->num_file_names++];
10225 fe->name = name;
10226 fe->dir_index = dir_index;
10227 fe->mod_time = mod_time;
10228 fe->length = length;
10229 fe->included_p = 0;
10230 fe->symtab = NULL;
10231 }
10232
10233 /* Read the statement program header starting at OFFSET in
10234 .debug_line, according to the endianness of ABFD. Return a pointer
10235 to a struct line_header, allocated using xmalloc.
10236
10237 NOTE: the strings in the include directory and file name tables of
10238 the returned object point into debug_line_buffer, and must not be
10239 freed. */
10240
10241 static struct line_header *
10242 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10243 struct dwarf2_cu *cu)
10244 {
10245 struct cleanup *back_to;
10246 struct line_header *lh;
10247 gdb_byte *line_ptr;
10248 unsigned int bytes_read, offset_size;
10249 int i;
10250 char *cur_dir, *cur_file;
10251
10252 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10253 if (dwarf2_per_objfile->line.buffer == NULL)
10254 {
10255 complaint (&symfile_complaints, _("missing .debug_line section"));
10256 return 0;
10257 }
10258
10259 /* Make sure that at least there's room for the total_length field.
10260 That could be 12 bytes long, but we're just going to fudge that. */
10261 if (offset + 4 >= dwarf2_per_objfile->line.size)
10262 {
10263 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10264 return 0;
10265 }
10266
10267 lh = xmalloc (sizeof (*lh));
10268 memset (lh, 0, sizeof (*lh));
10269 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10270 (void *) lh);
10271
10272 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10273
10274 /* Read in the header. */
10275 lh->total_length =
10276 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10277 &bytes_read, &offset_size);
10278 line_ptr += bytes_read;
10279 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10280 + dwarf2_per_objfile->line.size))
10281 {
10282 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10283 return 0;
10284 }
10285 lh->statement_program_end = line_ptr + lh->total_length;
10286 lh->version = read_2_bytes (abfd, line_ptr);
10287 line_ptr += 2;
10288 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10289 line_ptr += offset_size;
10290 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10291 line_ptr += 1;
10292 if (lh->version >= 4)
10293 {
10294 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10295 line_ptr += 1;
10296 }
10297 else
10298 lh->maximum_ops_per_instruction = 1;
10299
10300 if (lh->maximum_ops_per_instruction == 0)
10301 {
10302 lh->maximum_ops_per_instruction = 1;
10303 complaint (&symfile_complaints,
10304 _("invalid maximum_ops_per_instruction "
10305 "in `.debug_line' section"));
10306 }
10307
10308 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10309 line_ptr += 1;
10310 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10311 line_ptr += 1;
10312 lh->line_range = read_1_byte (abfd, line_ptr);
10313 line_ptr += 1;
10314 lh->opcode_base = read_1_byte (abfd, line_ptr);
10315 line_ptr += 1;
10316 lh->standard_opcode_lengths
10317 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10318
10319 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10320 for (i = 1; i < lh->opcode_base; ++i)
10321 {
10322 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10323 line_ptr += 1;
10324 }
10325
10326 /* Read directory table. */
10327 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10328 {
10329 line_ptr += bytes_read;
10330 add_include_dir (lh, cur_dir);
10331 }
10332 line_ptr += bytes_read;
10333
10334 /* Read file name table. */
10335 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10336 {
10337 unsigned int dir_index, mod_time, length;
10338
10339 line_ptr += bytes_read;
10340 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10341 line_ptr += bytes_read;
10342 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10343 line_ptr += bytes_read;
10344 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10345 line_ptr += bytes_read;
10346
10347 add_file_name (lh, cur_file, dir_index, mod_time, length);
10348 }
10349 line_ptr += bytes_read;
10350 lh->statement_program_start = line_ptr;
10351
10352 if (line_ptr > (dwarf2_per_objfile->line.buffer
10353 + dwarf2_per_objfile->line.size))
10354 complaint (&symfile_complaints,
10355 _("line number info header doesn't "
10356 "fit in `.debug_line' section"));
10357
10358 discard_cleanups (back_to);
10359 return lh;
10360 }
10361
10362 /* This function exists to work around a bug in certain compilers
10363 (particularly GCC 2.95), in which the first line number marker of a
10364 function does not show up until after the prologue, right before
10365 the second line number marker. This function shifts ADDRESS down
10366 to the beginning of the function if necessary, and is called on
10367 addresses passed to record_line. */
10368
10369 static CORE_ADDR
10370 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10371 {
10372 struct function_range *fn;
10373
10374 /* Find the function_range containing address. */
10375 if (!cu->first_fn)
10376 return address;
10377
10378 if (!cu->cached_fn)
10379 cu->cached_fn = cu->first_fn;
10380
10381 fn = cu->cached_fn;
10382 while (fn)
10383 if (fn->lowpc <= address && fn->highpc > address)
10384 goto found;
10385 else
10386 fn = fn->next;
10387
10388 fn = cu->first_fn;
10389 while (fn && fn != cu->cached_fn)
10390 if (fn->lowpc <= address && fn->highpc > address)
10391 goto found;
10392 else
10393 fn = fn->next;
10394
10395 return address;
10396
10397 found:
10398 if (fn->seen_line)
10399 return address;
10400 if (address != fn->lowpc)
10401 complaint (&symfile_complaints,
10402 _("misplaced first line number at 0x%lx for '%s'"),
10403 (unsigned long) address, fn->name);
10404 fn->seen_line = 1;
10405 return fn->lowpc;
10406 }
10407
10408 /* Subroutine of dwarf_decode_lines to simplify it.
10409 Return the file name of the psymtab for included file FILE_INDEX
10410 in line header LH of PST.
10411 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10412 If space for the result is malloc'd, it will be freed by a cleanup.
10413 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10414
10415 static char *
10416 psymtab_include_file_name (const struct line_header *lh, int file_index,
10417 const struct partial_symtab *pst,
10418 const char *comp_dir)
10419 {
10420 const struct file_entry fe = lh->file_names [file_index];
10421 char *include_name = fe.name;
10422 char *include_name_to_compare = include_name;
10423 char *dir_name = NULL;
10424 const char *pst_filename;
10425 char *copied_name = NULL;
10426 int file_is_pst;
10427
10428 if (fe.dir_index)
10429 dir_name = lh->include_dirs[fe.dir_index - 1];
10430
10431 if (!IS_ABSOLUTE_PATH (include_name)
10432 && (dir_name != NULL || comp_dir != NULL))
10433 {
10434 /* Avoid creating a duplicate psymtab for PST.
10435 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10436 Before we do the comparison, however, we need to account
10437 for DIR_NAME and COMP_DIR.
10438 First prepend dir_name (if non-NULL). If we still don't
10439 have an absolute path prepend comp_dir (if non-NULL).
10440 However, the directory we record in the include-file's
10441 psymtab does not contain COMP_DIR (to match the
10442 corresponding symtab(s)).
10443
10444 Example:
10445
10446 bash$ cd /tmp
10447 bash$ gcc -g ./hello.c
10448 include_name = "hello.c"
10449 dir_name = "."
10450 DW_AT_comp_dir = comp_dir = "/tmp"
10451 DW_AT_name = "./hello.c" */
10452
10453 if (dir_name != NULL)
10454 {
10455 include_name = concat (dir_name, SLASH_STRING,
10456 include_name, (char *)NULL);
10457 include_name_to_compare = include_name;
10458 make_cleanup (xfree, include_name);
10459 }
10460 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10461 {
10462 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10463 include_name, (char *)NULL);
10464 }
10465 }
10466
10467 pst_filename = pst->filename;
10468 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10469 {
10470 copied_name = concat (pst->dirname, SLASH_STRING,
10471 pst_filename, (char *)NULL);
10472 pst_filename = copied_name;
10473 }
10474
10475 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10476
10477 if (include_name_to_compare != include_name)
10478 xfree (include_name_to_compare);
10479 if (copied_name != NULL)
10480 xfree (copied_name);
10481
10482 if (file_is_pst)
10483 return NULL;
10484 return include_name;
10485 }
10486
10487 /* Ignore this record_line request. */
10488
10489 static void
10490 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10491 {
10492 return;
10493 }
10494
10495 /* Decode the Line Number Program (LNP) for the given line_header
10496 structure and CU. The actual information extracted and the type
10497 of structures created from the LNP depends on the value of PST.
10498
10499 1. If PST is NULL, then this procedure uses the data from the program
10500 to create all necessary symbol tables, and their linetables.
10501
10502 2. If PST is not NULL, this procedure reads the program to determine
10503 the list of files included by the unit represented by PST, and
10504 builds all the associated partial symbol tables.
10505
10506 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10507 It is used for relative paths in the line table.
10508 NOTE: When processing partial symtabs (pst != NULL),
10509 comp_dir == pst->dirname.
10510
10511 NOTE: It is important that psymtabs have the same file name (via strcmp)
10512 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10513 symtab we don't use it in the name of the psymtabs we create.
10514 E.g. expand_line_sal requires this when finding psymtabs to expand.
10515 A good testcase for this is mb-inline.exp. */
10516
10517 static void
10518 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10519 struct dwarf2_cu *cu, struct partial_symtab *pst)
10520 {
10521 gdb_byte *line_ptr, *extended_end;
10522 gdb_byte *line_end;
10523 unsigned int bytes_read, extended_len;
10524 unsigned char op_code, extended_op, adj_opcode;
10525 CORE_ADDR baseaddr;
10526 struct objfile *objfile = cu->objfile;
10527 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10528 const int decode_for_pst_p = (pst != NULL);
10529 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10530 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10531 = record_line;
10532
10533 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10534
10535 line_ptr = lh->statement_program_start;
10536 line_end = lh->statement_program_end;
10537
10538 /* Read the statement sequences until there's nothing left. */
10539 while (line_ptr < line_end)
10540 {
10541 /* state machine registers */
10542 CORE_ADDR address = 0;
10543 unsigned int file = 1;
10544 unsigned int line = 1;
10545 unsigned int column = 0;
10546 int is_stmt = lh->default_is_stmt;
10547 int basic_block = 0;
10548 int end_sequence = 0;
10549 CORE_ADDR addr;
10550 unsigned char op_index = 0;
10551
10552 if (!decode_for_pst_p && lh->num_file_names >= file)
10553 {
10554 /* Start a subfile for the current file of the state machine. */
10555 /* lh->include_dirs and lh->file_names are 0-based, but the
10556 directory and file name numbers in the statement program
10557 are 1-based. */
10558 struct file_entry *fe = &lh->file_names[file - 1];
10559 char *dir = NULL;
10560
10561 if (fe->dir_index)
10562 dir = lh->include_dirs[fe->dir_index - 1];
10563
10564 dwarf2_start_subfile (fe->name, dir, comp_dir);
10565 }
10566
10567 /* Decode the table. */
10568 while (!end_sequence)
10569 {
10570 op_code = read_1_byte (abfd, line_ptr);
10571 line_ptr += 1;
10572 if (line_ptr > line_end)
10573 {
10574 dwarf2_debug_line_missing_end_sequence_complaint ();
10575 break;
10576 }
10577
10578 if (op_code >= lh->opcode_base)
10579 {
10580 /* Special operand. */
10581 adj_opcode = op_code - lh->opcode_base;
10582 address += (((op_index + (adj_opcode / lh->line_range))
10583 / lh->maximum_ops_per_instruction)
10584 * lh->minimum_instruction_length);
10585 op_index = ((op_index + (adj_opcode / lh->line_range))
10586 % lh->maximum_ops_per_instruction);
10587 line += lh->line_base + (adj_opcode % lh->line_range);
10588 if (lh->num_file_names < file || file == 0)
10589 dwarf2_debug_line_missing_file_complaint ();
10590 /* For now we ignore lines not starting on an
10591 instruction boundary. */
10592 else if (op_index == 0)
10593 {
10594 lh->file_names[file - 1].included_p = 1;
10595 if (!decode_for_pst_p && is_stmt)
10596 {
10597 if (last_subfile != current_subfile)
10598 {
10599 addr = gdbarch_addr_bits_remove (gdbarch, address);
10600 if (last_subfile)
10601 (*p_record_line) (last_subfile, 0, addr);
10602 last_subfile = current_subfile;
10603 }
10604 /* Append row to matrix using current values. */
10605 addr = check_cu_functions (address, cu);
10606 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10607 (*p_record_line) (current_subfile, line, addr);
10608 }
10609 }
10610 basic_block = 0;
10611 }
10612 else switch (op_code)
10613 {
10614 case DW_LNS_extended_op:
10615 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10616 &bytes_read);
10617 line_ptr += bytes_read;
10618 extended_end = line_ptr + extended_len;
10619 extended_op = read_1_byte (abfd, line_ptr);
10620 line_ptr += 1;
10621 switch (extended_op)
10622 {
10623 case DW_LNE_end_sequence:
10624 p_record_line = record_line;
10625 end_sequence = 1;
10626 break;
10627 case DW_LNE_set_address:
10628 address = read_address (abfd, line_ptr, cu, &bytes_read);
10629
10630 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10631 {
10632 /* This line table is for a function which has been
10633 GCd by the linker. Ignore it. PR gdb/12528 */
10634
10635 long line_offset
10636 = line_ptr - dwarf2_per_objfile->line.buffer;
10637
10638 complaint (&symfile_complaints,
10639 _(".debug_line address at offset 0x%lx is 0 "
10640 "[in module %s]"),
10641 line_offset, cu->objfile->name);
10642 p_record_line = noop_record_line;
10643 }
10644
10645 op_index = 0;
10646 line_ptr += bytes_read;
10647 address += baseaddr;
10648 break;
10649 case DW_LNE_define_file:
10650 {
10651 char *cur_file;
10652 unsigned int dir_index, mod_time, length;
10653
10654 cur_file = read_direct_string (abfd, line_ptr,
10655 &bytes_read);
10656 line_ptr += bytes_read;
10657 dir_index =
10658 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10659 line_ptr += bytes_read;
10660 mod_time =
10661 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10662 line_ptr += bytes_read;
10663 length =
10664 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10665 line_ptr += bytes_read;
10666 add_file_name (lh, cur_file, dir_index, mod_time, length);
10667 }
10668 break;
10669 case DW_LNE_set_discriminator:
10670 /* The discriminator is not interesting to the debugger;
10671 just ignore it. */
10672 line_ptr = extended_end;
10673 break;
10674 default:
10675 complaint (&symfile_complaints,
10676 _("mangled .debug_line section"));
10677 return;
10678 }
10679 /* Make sure that we parsed the extended op correctly. If e.g.
10680 we expected a different address size than the producer used,
10681 we may have read the wrong number of bytes. */
10682 if (line_ptr != extended_end)
10683 {
10684 complaint (&symfile_complaints,
10685 _("mangled .debug_line section"));
10686 return;
10687 }
10688 break;
10689 case DW_LNS_copy:
10690 if (lh->num_file_names < file || file == 0)
10691 dwarf2_debug_line_missing_file_complaint ();
10692 else
10693 {
10694 lh->file_names[file - 1].included_p = 1;
10695 if (!decode_for_pst_p && is_stmt)
10696 {
10697 if (last_subfile != current_subfile)
10698 {
10699 addr = gdbarch_addr_bits_remove (gdbarch, address);
10700 if (last_subfile)
10701 (*p_record_line) (last_subfile, 0, addr);
10702 last_subfile = current_subfile;
10703 }
10704 addr = check_cu_functions (address, cu);
10705 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10706 (*p_record_line) (current_subfile, line, addr);
10707 }
10708 }
10709 basic_block = 0;
10710 break;
10711 case DW_LNS_advance_pc:
10712 {
10713 CORE_ADDR adjust
10714 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10715
10716 address += (((op_index + adjust)
10717 / lh->maximum_ops_per_instruction)
10718 * lh->minimum_instruction_length);
10719 op_index = ((op_index + adjust)
10720 % lh->maximum_ops_per_instruction);
10721 line_ptr += bytes_read;
10722 }
10723 break;
10724 case DW_LNS_advance_line:
10725 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10726 line_ptr += bytes_read;
10727 break;
10728 case DW_LNS_set_file:
10729 {
10730 /* The arrays lh->include_dirs and lh->file_names are
10731 0-based, but the directory and file name numbers in
10732 the statement program are 1-based. */
10733 struct file_entry *fe;
10734 char *dir = NULL;
10735
10736 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10737 line_ptr += bytes_read;
10738 if (lh->num_file_names < file || file == 0)
10739 dwarf2_debug_line_missing_file_complaint ();
10740 else
10741 {
10742 fe = &lh->file_names[file - 1];
10743 if (fe->dir_index)
10744 dir = lh->include_dirs[fe->dir_index - 1];
10745 if (!decode_for_pst_p)
10746 {
10747 last_subfile = current_subfile;
10748 dwarf2_start_subfile (fe->name, dir, comp_dir);
10749 }
10750 }
10751 }
10752 break;
10753 case DW_LNS_set_column:
10754 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10755 line_ptr += bytes_read;
10756 break;
10757 case DW_LNS_negate_stmt:
10758 is_stmt = (!is_stmt);
10759 break;
10760 case DW_LNS_set_basic_block:
10761 basic_block = 1;
10762 break;
10763 /* Add to the address register of the state machine the
10764 address increment value corresponding to special opcode
10765 255. I.e., this value is scaled by the minimum
10766 instruction length since special opcode 255 would have
10767 scaled the increment. */
10768 case DW_LNS_const_add_pc:
10769 {
10770 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10771
10772 address += (((op_index + adjust)
10773 / lh->maximum_ops_per_instruction)
10774 * lh->minimum_instruction_length);
10775 op_index = ((op_index + adjust)
10776 % lh->maximum_ops_per_instruction);
10777 }
10778 break;
10779 case DW_LNS_fixed_advance_pc:
10780 address += read_2_bytes (abfd, line_ptr);
10781 op_index = 0;
10782 line_ptr += 2;
10783 break;
10784 default:
10785 {
10786 /* Unknown standard opcode, ignore it. */
10787 int i;
10788
10789 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10790 {
10791 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10792 line_ptr += bytes_read;
10793 }
10794 }
10795 }
10796 }
10797 if (lh->num_file_names < file || file == 0)
10798 dwarf2_debug_line_missing_file_complaint ();
10799 else
10800 {
10801 lh->file_names[file - 1].included_p = 1;
10802 if (!decode_for_pst_p)
10803 {
10804 addr = gdbarch_addr_bits_remove (gdbarch, address);
10805 (*p_record_line) (current_subfile, 0, addr);
10806 }
10807 }
10808 }
10809
10810 if (decode_for_pst_p)
10811 {
10812 int file_index;
10813
10814 /* Now that we're done scanning the Line Header Program, we can
10815 create the psymtab of each included file. */
10816 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10817 if (lh->file_names[file_index].included_p == 1)
10818 {
10819 char *include_name =
10820 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10821 if (include_name != NULL)
10822 dwarf2_create_include_psymtab (include_name, pst, objfile);
10823 }
10824 }
10825 else
10826 {
10827 /* Make sure a symtab is created for every file, even files
10828 which contain only variables (i.e. no code with associated
10829 line numbers). */
10830
10831 int i;
10832 struct file_entry *fe;
10833
10834 for (i = 0; i < lh->num_file_names; i++)
10835 {
10836 char *dir = NULL;
10837
10838 fe = &lh->file_names[i];
10839 if (fe->dir_index)
10840 dir = lh->include_dirs[fe->dir_index - 1];
10841 dwarf2_start_subfile (fe->name, dir, comp_dir);
10842
10843 /* Skip the main file; we don't need it, and it must be
10844 allocated last, so that it will show up before the
10845 non-primary symtabs in the objfile's symtab list. */
10846 if (current_subfile == first_subfile)
10847 continue;
10848
10849 if (current_subfile->symtab == NULL)
10850 current_subfile->symtab = allocate_symtab (current_subfile->name,
10851 cu->objfile);
10852 fe->symtab = current_subfile->symtab;
10853 }
10854 }
10855 }
10856
10857 /* Start a subfile for DWARF. FILENAME is the name of the file and
10858 DIRNAME the name of the source directory which contains FILENAME
10859 or NULL if not known. COMP_DIR is the compilation directory for the
10860 linetable's compilation unit or NULL if not known.
10861 This routine tries to keep line numbers from identical absolute and
10862 relative file names in a common subfile.
10863
10864 Using the `list' example from the GDB testsuite, which resides in
10865 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10866 of /srcdir/list0.c yields the following debugging information for list0.c:
10867
10868 DW_AT_name: /srcdir/list0.c
10869 DW_AT_comp_dir: /compdir
10870 files.files[0].name: list0.h
10871 files.files[0].dir: /srcdir
10872 files.files[1].name: list0.c
10873 files.files[1].dir: /srcdir
10874
10875 The line number information for list0.c has to end up in a single
10876 subfile, so that `break /srcdir/list0.c:1' works as expected.
10877 start_subfile will ensure that this happens provided that we pass the
10878 concatenation of files.files[1].dir and files.files[1].name as the
10879 subfile's name. */
10880
10881 static void
10882 dwarf2_start_subfile (char *filename, const char *dirname,
10883 const char *comp_dir)
10884 {
10885 char *fullname;
10886
10887 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10888 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10889 second argument to start_subfile. To be consistent, we do the
10890 same here. In order not to lose the line information directory,
10891 we concatenate it to the filename when it makes sense.
10892 Note that the Dwarf3 standard says (speaking of filenames in line
10893 information): ``The directory index is ignored for file names
10894 that represent full path names''. Thus ignoring dirname in the
10895 `else' branch below isn't an issue. */
10896
10897 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10898 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10899 else
10900 fullname = filename;
10901
10902 start_subfile (fullname, comp_dir);
10903
10904 if (fullname != filename)
10905 xfree (fullname);
10906 }
10907
10908 static void
10909 var_decode_location (struct attribute *attr, struct symbol *sym,
10910 struct dwarf2_cu *cu)
10911 {
10912 struct objfile *objfile = cu->objfile;
10913 struct comp_unit_head *cu_header = &cu->header;
10914
10915 /* NOTE drow/2003-01-30: There used to be a comment and some special
10916 code here to turn a symbol with DW_AT_external and a
10917 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10918 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10919 with some versions of binutils) where shared libraries could have
10920 relocations against symbols in their debug information - the
10921 minimal symbol would have the right address, but the debug info
10922 would not. It's no longer necessary, because we will explicitly
10923 apply relocations when we read in the debug information now. */
10924
10925 /* A DW_AT_location attribute with no contents indicates that a
10926 variable has been optimized away. */
10927 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10928 {
10929 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10930 return;
10931 }
10932
10933 /* Handle one degenerate form of location expression specially, to
10934 preserve GDB's previous behavior when section offsets are
10935 specified. If this is just a DW_OP_addr then mark this symbol
10936 as LOC_STATIC. */
10937
10938 if (attr_form_is_block (attr)
10939 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10940 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10941 {
10942 unsigned int dummy;
10943
10944 SYMBOL_VALUE_ADDRESS (sym) =
10945 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10946 SYMBOL_CLASS (sym) = LOC_STATIC;
10947 fixup_symbol_section (sym, objfile);
10948 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10949 SYMBOL_SECTION (sym));
10950 return;
10951 }
10952
10953 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10954 expression evaluator, and use LOC_COMPUTED only when necessary
10955 (i.e. when the value of a register or memory location is
10956 referenced, or a thread-local block, etc.). Then again, it might
10957 not be worthwhile. I'm assuming that it isn't unless performance
10958 or memory numbers show me otherwise. */
10959
10960 dwarf2_symbol_mark_computed (attr, sym, cu);
10961 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10962 }
10963
10964 /* Given a pointer to a DWARF information entry, figure out if we need
10965 to make a symbol table entry for it, and if so, create a new entry
10966 and return a pointer to it.
10967 If TYPE is NULL, determine symbol type from the die, otherwise
10968 used the passed type.
10969 If SPACE is not NULL, use it to hold the new symbol. If it is
10970 NULL, allocate a new symbol on the objfile's obstack. */
10971
10972 static struct symbol *
10973 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10974 struct symbol *space)
10975 {
10976 struct objfile *objfile = cu->objfile;
10977 struct symbol *sym = NULL;
10978 char *name;
10979 struct attribute *attr = NULL;
10980 struct attribute *attr2 = NULL;
10981 CORE_ADDR baseaddr;
10982 struct pending **list_to_add = NULL;
10983
10984 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10985
10986 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10987
10988 name = dwarf2_name (die, cu);
10989 if (name)
10990 {
10991 const char *linkagename;
10992 int suppress_add = 0;
10993
10994 if (space)
10995 sym = space;
10996 else
10997 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10998 OBJSTAT (objfile, n_syms++);
10999
11000 /* Cache this symbol's name and the name's demangled form (if any). */
11001 SYMBOL_SET_LANGUAGE (sym, cu->language);
11002 linkagename = dwarf2_physname (name, die, cu);
11003 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11004
11005 /* Fortran does not have mangling standard and the mangling does differ
11006 between gfortran, iFort etc. */
11007 if (cu->language == language_fortran
11008 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11009 symbol_set_demangled_name (&(sym->ginfo),
11010 (char *) dwarf2_full_name (name, die, cu),
11011 NULL);
11012
11013 /* Default assumptions.
11014 Use the passed type or decode it from the die. */
11015 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11016 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11017 if (type != NULL)
11018 SYMBOL_TYPE (sym) = type;
11019 else
11020 SYMBOL_TYPE (sym) = die_type (die, cu);
11021 attr = dwarf2_attr (die,
11022 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11023 cu);
11024 if (attr)
11025 {
11026 SYMBOL_LINE (sym) = DW_UNSND (attr);
11027 }
11028
11029 attr = dwarf2_attr (die,
11030 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11031 cu);
11032 if (attr)
11033 {
11034 int file_index = DW_UNSND (attr);
11035
11036 if (cu->line_header == NULL
11037 || file_index > cu->line_header->num_file_names)
11038 complaint (&symfile_complaints,
11039 _("file index out of range"));
11040 else if (file_index > 0)
11041 {
11042 struct file_entry *fe;
11043
11044 fe = &cu->line_header->file_names[file_index - 1];
11045 SYMBOL_SYMTAB (sym) = fe->symtab;
11046 }
11047 }
11048
11049 switch (die->tag)
11050 {
11051 case DW_TAG_label:
11052 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11053 if (attr)
11054 {
11055 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11056 }
11057 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11058 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11059 SYMBOL_CLASS (sym) = LOC_LABEL;
11060 add_symbol_to_list (sym, cu->list_in_scope);
11061 break;
11062 case DW_TAG_subprogram:
11063 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11064 finish_block. */
11065 SYMBOL_CLASS (sym) = LOC_BLOCK;
11066 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11067 if ((attr2 && (DW_UNSND (attr2) != 0))
11068 || cu->language == language_ada)
11069 {
11070 /* Subprograms marked external are stored as a global symbol.
11071 Ada subprograms, whether marked external or not, are always
11072 stored as a global symbol, because we want to be able to
11073 access them globally. For instance, we want to be able
11074 to break on a nested subprogram without having to
11075 specify the context. */
11076 list_to_add = &global_symbols;
11077 }
11078 else
11079 {
11080 list_to_add = cu->list_in_scope;
11081 }
11082 break;
11083 case DW_TAG_inlined_subroutine:
11084 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11085 finish_block. */
11086 SYMBOL_CLASS (sym) = LOC_BLOCK;
11087 SYMBOL_INLINED (sym) = 1;
11088 /* Do not add the symbol to any lists. It will be found via
11089 BLOCK_FUNCTION from the blockvector. */
11090 break;
11091 case DW_TAG_template_value_param:
11092 suppress_add = 1;
11093 /* Fall through. */
11094 case DW_TAG_constant:
11095 case DW_TAG_variable:
11096 case DW_TAG_member:
11097 /* Compilation with minimal debug info may result in
11098 variables with missing type entries. Change the
11099 misleading `void' type to something sensible. */
11100 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11101 SYMBOL_TYPE (sym)
11102 = objfile_type (objfile)->nodebug_data_symbol;
11103
11104 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11105 /* In the case of DW_TAG_member, we should only be called for
11106 static const members. */
11107 if (die->tag == DW_TAG_member)
11108 {
11109 /* dwarf2_add_field uses die_is_declaration,
11110 so we do the same. */
11111 gdb_assert (die_is_declaration (die, cu));
11112 gdb_assert (attr);
11113 }
11114 if (attr)
11115 {
11116 dwarf2_const_value (attr, sym, cu);
11117 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11118 if (!suppress_add)
11119 {
11120 if (attr2 && (DW_UNSND (attr2) != 0))
11121 list_to_add = &global_symbols;
11122 else
11123 list_to_add = cu->list_in_scope;
11124 }
11125 break;
11126 }
11127 attr = dwarf2_attr (die, DW_AT_location, cu);
11128 if (attr)
11129 {
11130 var_decode_location (attr, sym, cu);
11131 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11132 if (SYMBOL_CLASS (sym) == LOC_STATIC
11133 && SYMBOL_VALUE_ADDRESS (sym) == 0
11134 && !dwarf2_per_objfile->has_section_at_zero)
11135 {
11136 /* When a static variable is eliminated by the linker,
11137 the corresponding debug information is not stripped
11138 out, but the variable address is set to null;
11139 do not add such variables into symbol table. */
11140 }
11141 else if (attr2 && (DW_UNSND (attr2) != 0))
11142 {
11143 /* Workaround gfortran PR debug/40040 - it uses
11144 DW_AT_location for variables in -fPIC libraries which may
11145 get overriden by other libraries/executable and get
11146 a different address. Resolve it by the minimal symbol
11147 which may come from inferior's executable using copy
11148 relocation. Make this workaround only for gfortran as for
11149 other compilers GDB cannot guess the minimal symbol
11150 Fortran mangling kind. */
11151 if (cu->language == language_fortran && die->parent
11152 && die->parent->tag == DW_TAG_module
11153 && cu->producer
11154 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11155 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11156
11157 /* A variable with DW_AT_external is never static,
11158 but it may be block-scoped. */
11159 list_to_add = (cu->list_in_scope == &file_symbols
11160 ? &global_symbols : cu->list_in_scope);
11161 }
11162 else
11163 list_to_add = cu->list_in_scope;
11164 }
11165 else
11166 {
11167 /* We do not know the address of this symbol.
11168 If it is an external symbol and we have type information
11169 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11170 The address of the variable will then be determined from
11171 the minimal symbol table whenever the variable is
11172 referenced. */
11173 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11174 if (attr2 && (DW_UNSND (attr2) != 0)
11175 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11176 {
11177 /* A variable with DW_AT_external is never static, but it
11178 may be block-scoped. */
11179 list_to_add = (cu->list_in_scope == &file_symbols
11180 ? &global_symbols : cu->list_in_scope);
11181
11182 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11183 }
11184 else if (!die_is_declaration (die, cu))
11185 {
11186 /* Use the default LOC_OPTIMIZED_OUT class. */
11187 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11188 if (!suppress_add)
11189 list_to_add = cu->list_in_scope;
11190 }
11191 }
11192 break;
11193 case DW_TAG_formal_parameter:
11194 /* If we are inside a function, mark this as an argument. If
11195 not, we might be looking at an argument to an inlined function
11196 when we do not have enough information to show inlined frames;
11197 pretend it's a local variable in that case so that the user can
11198 still see it. */
11199 if (context_stack_depth > 0
11200 && context_stack[context_stack_depth - 1].name != NULL)
11201 SYMBOL_IS_ARGUMENT (sym) = 1;
11202 attr = dwarf2_attr (die, DW_AT_location, cu);
11203 if (attr)
11204 {
11205 var_decode_location (attr, sym, cu);
11206 }
11207 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11208 if (attr)
11209 {
11210 dwarf2_const_value (attr, sym, cu);
11211 }
11212 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11213 if (attr && DW_UNSND (attr))
11214 {
11215 struct type *ref_type;
11216
11217 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11218 SYMBOL_TYPE (sym) = ref_type;
11219 }
11220
11221 list_to_add = cu->list_in_scope;
11222 break;
11223 case DW_TAG_unspecified_parameters:
11224 /* From varargs functions; gdb doesn't seem to have any
11225 interest in this information, so just ignore it for now.
11226 (FIXME?) */
11227 break;
11228 case DW_TAG_template_type_param:
11229 suppress_add = 1;
11230 /* Fall through. */
11231 case DW_TAG_class_type:
11232 case DW_TAG_interface_type:
11233 case DW_TAG_structure_type:
11234 case DW_TAG_union_type:
11235 case DW_TAG_set_type:
11236 case DW_TAG_enumeration_type:
11237 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11238 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11239
11240 {
11241 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11242 really ever be static objects: otherwise, if you try
11243 to, say, break of a class's method and you're in a file
11244 which doesn't mention that class, it won't work unless
11245 the check for all static symbols in lookup_symbol_aux
11246 saves you. See the OtherFileClass tests in
11247 gdb.c++/namespace.exp. */
11248
11249 if (!suppress_add)
11250 {
11251 list_to_add = (cu->list_in_scope == &file_symbols
11252 && (cu->language == language_cplus
11253 || cu->language == language_java)
11254 ? &global_symbols : cu->list_in_scope);
11255
11256 /* The semantics of C++ state that "struct foo {
11257 ... }" also defines a typedef for "foo". A Java
11258 class declaration also defines a typedef for the
11259 class. */
11260 if (cu->language == language_cplus
11261 || cu->language == language_java
11262 || cu->language == language_ada)
11263 {
11264 /* The symbol's name is already allocated along
11265 with this objfile, so we don't need to
11266 duplicate it for the type. */
11267 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11268 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11269 }
11270 }
11271 }
11272 break;
11273 case DW_TAG_typedef:
11274 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11275 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11276 list_to_add = cu->list_in_scope;
11277 break;
11278 case DW_TAG_base_type:
11279 case DW_TAG_subrange_type:
11280 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11281 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11282 list_to_add = cu->list_in_scope;
11283 break;
11284 case DW_TAG_enumerator:
11285 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11286 if (attr)
11287 {
11288 dwarf2_const_value (attr, sym, cu);
11289 }
11290 {
11291 /* NOTE: carlton/2003-11-10: See comment above in the
11292 DW_TAG_class_type, etc. block. */
11293
11294 list_to_add = (cu->list_in_scope == &file_symbols
11295 && (cu->language == language_cplus
11296 || cu->language == language_java)
11297 ? &global_symbols : cu->list_in_scope);
11298 }
11299 break;
11300 case DW_TAG_namespace:
11301 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11302 list_to_add = &global_symbols;
11303 break;
11304 default:
11305 /* Not a tag we recognize. Hopefully we aren't processing
11306 trash data, but since we must specifically ignore things
11307 we don't recognize, there is nothing else we should do at
11308 this point. */
11309 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11310 dwarf_tag_name (die->tag));
11311 break;
11312 }
11313
11314 if (suppress_add)
11315 {
11316 sym->hash_next = objfile->template_symbols;
11317 objfile->template_symbols = sym;
11318 list_to_add = NULL;
11319 }
11320
11321 if (list_to_add != NULL)
11322 add_symbol_to_list (sym, list_to_add);
11323
11324 /* For the benefit of old versions of GCC, check for anonymous
11325 namespaces based on the demangled name. */
11326 if (!processing_has_namespace_info
11327 && cu->language == language_cplus)
11328 cp_scan_for_anonymous_namespaces (sym);
11329 }
11330 return (sym);
11331 }
11332
11333 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11334
11335 static struct symbol *
11336 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11337 {
11338 return new_symbol_full (die, type, cu, NULL);
11339 }
11340
11341 /* Given an attr with a DW_FORM_dataN value in host byte order,
11342 zero-extend it as appropriate for the symbol's type. The DWARF
11343 standard (v4) is not entirely clear about the meaning of using
11344 DW_FORM_dataN for a constant with a signed type, where the type is
11345 wider than the data. The conclusion of a discussion on the DWARF
11346 list was that this is unspecified. We choose to always zero-extend
11347 because that is the interpretation long in use by GCC. */
11348
11349 static gdb_byte *
11350 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11351 const char *name, struct obstack *obstack,
11352 struct dwarf2_cu *cu, long *value, int bits)
11353 {
11354 struct objfile *objfile = cu->objfile;
11355 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11356 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11357 LONGEST l = DW_UNSND (attr);
11358
11359 if (bits < sizeof (*value) * 8)
11360 {
11361 l &= ((LONGEST) 1 << bits) - 1;
11362 *value = l;
11363 }
11364 else if (bits == sizeof (*value) * 8)
11365 *value = l;
11366 else
11367 {
11368 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11369 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11370 return bytes;
11371 }
11372
11373 return NULL;
11374 }
11375
11376 /* Read a constant value from an attribute. Either set *VALUE, or if
11377 the value does not fit in *VALUE, set *BYTES - either already
11378 allocated on the objfile obstack, or newly allocated on OBSTACK,
11379 or, set *BATON, if we translated the constant to a location
11380 expression. */
11381
11382 static void
11383 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11384 const char *name, struct obstack *obstack,
11385 struct dwarf2_cu *cu,
11386 long *value, gdb_byte **bytes,
11387 struct dwarf2_locexpr_baton **baton)
11388 {
11389 struct objfile *objfile = cu->objfile;
11390 struct comp_unit_head *cu_header = &cu->header;
11391 struct dwarf_block *blk;
11392 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11393 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11394
11395 *value = 0;
11396 *bytes = NULL;
11397 *baton = NULL;
11398
11399 switch (attr->form)
11400 {
11401 case DW_FORM_addr:
11402 {
11403 gdb_byte *data;
11404
11405 if (TYPE_LENGTH (type) != cu_header->addr_size)
11406 dwarf2_const_value_length_mismatch_complaint (name,
11407 cu_header->addr_size,
11408 TYPE_LENGTH (type));
11409 /* Symbols of this form are reasonably rare, so we just
11410 piggyback on the existing location code rather than writing
11411 a new implementation of symbol_computed_ops. */
11412 *baton = obstack_alloc (&objfile->objfile_obstack,
11413 sizeof (struct dwarf2_locexpr_baton));
11414 (*baton)->per_cu = cu->per_cu;
11415 gdb_assert ((*baton)->per_cu);
11416
11417 (*baton)->size = 2 + cu_header->addr_size;
11418 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11419 (*baton)->data = data;
11420
11421 data[0] = DW_OP_addr;
11422 store_unsigned_integer (&data[1], cu_header->addr_size,
11423 byte_order, DW_ADDR (attr));
11424 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11425 }
11426 break;
11427 case DW_FORM_string:
11428 case DW_FORM_strp:
11429 /* DW_STRING is already allocated on the objfile obstack, point
11430 directly to it. */
11431 *bytes = (gdb_byte *) DW_STRING (attr);
11432 break;
11433 case DW_FORM_block1:
11434 case DW_FORM_block2:
11435 case DW_FORM_block4:
11436 case DW_FORM_block:
11437 case DW_FORM_exprloc:
11438 blk = DW_BLOCK (attr);
11439 if (TYPE_LENGTH (type) != blk->size)
11440 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11441 TYPE_LENGTH (type));
11442 *bytes = blk->data;
11443 break;
11444
11445 /* The DW_AT_const_value attributes are supposed to carry the
11446 symbol's value "represented as it would be on the target
11447 architecture." By the time we get here, it's already been
11448 converted to host endianness, so we just need to sign- or
11449 zero-extend it as appropriate. */
11450 case DW_FORM_data1:
11451 *bytes = dwarf2_const_value_data (attr, type, name,
11452 obstack, cu, value, 8);
11453 break;
11454 case DW_FORM_data2:
11455 *bytes = dwarf2_const_value_data (attr, type, name,
11456 obstack, cu, value, 16);
11457 break;
11458 case DW_FORM_data4:
11459 *bytes = dwarf2_const_value_data (attr, type, name,
11460 obstack, cu, value, 32);
11461 break;
11462 case DW_FORM_data8:
11463 *bytes = dwarf2_const_value_data (attr, type, name,
11464 obstack, cu, value, 64);
11465 break;
11466
11467 case DW_FORM_sdata:
11468 *value = DW_SND (attr);
11469 break;
11470
11471 case DW_FORM_udata:
11472 *value = DW_UNSND (attr);
11473 break;
11474
11475 default:
11476 complaint (&symfile_complaints,
11477 _("unsupported const value attribute form: '%s'"),
11478 dwarf_form_name (attr->form));
11479 *value = 0;
11480 break;
11481 }
11482 }
11483
11484
11485 /* Copy constant value from an attribute to a symbol. */
11486
11487 static void
11488 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11489 struct dwarf2_cu *cu)
11490 {
11491 struct objfile *objfile = cu->objfile;
11492 struct comp_unit_head *cu_header = &cu->header;
11493 long value;
11494 gdb_byte *bytes;
11495 struct dwarf2_locexpr_baton *baton;
11496
11497 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11498 SYMBOL_PRINT_NAME (sym),
11499 &objfile->objfile_obstack, cu,
11500 &value, &bytes, &baton);
11501
11502 if (baton != NULL)
11503 {
11504 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11505 SYMBOL_LOCATION_BATON (sym) = baton;
11506 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11507 }
11508 else if (bytes != NULL)
11509 {
11510 SYMBOL_VALUE_BYTES (sym) = bytes;
11511 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11512 }
11513 else
11514 {
11515 SYMBOL_VALUE (sym) = value;
11516 SYMBOL_CLASS (sym) = LOC_CONST;
11517 }
11518 }
11519
11520 /* Return the type of the die in question using its DW_AT_type attribute. */
11521
11522 static struct type *
11523 die_type (struct die_info *die, struct dwarf2_cu *cu)
11524 {
11525 struct attribute *type_attr;
11526
11527 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11528 if (!type_attr)
11529 {
11530 /* A missing DW_AT_type represents a void type. */
11531 return objfile_type (cu->objfile)->builtin_void;
11532 }
11533
11534 return lookup_die_type (die, type_attr, cu);
11535 }
11536
11537 /* True iff CU's producer generates GNAT Ada auxiliary information
11538 that allows to find parallel types through that information instead
11539 of having to do expensive parallel lookups by type name. */
11540
11541 static int
11542 need_gnat_info (struct dwarf2_cu *cu)
11543 {
11544 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11545 of GNAT produces this auxiliary information, without any indication
11546 that it is produced. Part of enhancing the FSF version of GNAT
11547 to produce that information will be to put in place an indicator
11548 that we can use in order to determine whether the descriptive type
11549 info is available or not. One suggestion that has been made is
11550 to use a new attribute, attached to the CU die. For now, assume
11551 that the descriptive type info is not available. */
11552 return 0;
11553 }
11554
11555 /* Return the auxiliary type of the die in question using its
11556 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11557 attribute is not present. */
11558
11559 static struct type *
11560 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11561 {
11562 struct attribute *type_attr;
11563
11564 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11565 if (!type_attr)
11566 return NULL;
11567
11568 return lookup_die_type (die, type_attr, cu);
11569 }
11570
11571 /* If DIE has a descriptive_type attribute, then set the TYPE's
11572 descriptive type accordingly. */
11573
11574 static void
11575 set_descriptive_type (struct type *type, struct die_info *die,
11576 struct dwarf2_cu *cu)
11577 {
11578 struct type *descriptive_type = die_descriptive_type (die, cu);
11579
11580 if (descriptive_type)
11581 {
11582 ALLOCATE_GNAT_AUX_TYPE (type);
11583 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11584 }
11585 }
11586
11587 /* Return the containing type of the die in question using its
11588 DW_AT_containing_type attribute. */
11589
11590 static struct type *
11591 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11592 {
11593 struct attribute *type_attr;
11594
11595 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11596 if (!type_attr)
11597 error (_("Dwarf Error: Problem turning containing type into gdb type "
11598 "[in module %s]"), cu->objfile->name);
11599
11600 return lookup_die_type (die, type_attr, cu);
11601 }
11602
11603 /* Look up the type of DIE in CU using its type attribute ATTR.
11604 If there is no type substitute an error marker. */
11605
11606 static struct type *
11607 lookup_die_type (struct die_info *die, struct attribute *attr,
11608 struct dwarf2_cu *cu)
11609 {
11610 struct type *this_type;
11611
11612 /* First see if we have it cached. */
11613
11614 if (is_ref_attr (attr))
11615 {
11616 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11617
11618 this_type = get_die_type_at_offset (offset, cu->per_cu);
11619 }
11620 else if (attr->form == DW_FORM_ref_sig8)
11621 {
11622 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11623 struct dwarf2_cu *sig_cu;
11624 unsigned int offset;
11625
11626 /* sig_type will be NULL if the signatured type is missing from
11627 the debug info. */
11628 if (sig_type == NULL)
11629 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11630 "at 0x%x [in module %s]"),
11631 die->offset, cu->objfile->name);
11632
11633 gdb_assert (sig_type->per_cu.from_debug_types);
11634 offset = sig_type->offset + sig_type->type_offset;
11635 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11636 }
11637 else
11638 {
11639 dump_die_for_error (die);
11640 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11641 dwarf_attr_name (attr->name), cu->objfile->name);
11642 }
11643
11644 /* If not cached we need to read it in. */
11645
11646 if (this_type == NULL)
11647 {
11648 struct die_info *type_die;
11649 struct dwarf2_cu *type_cu = cu;
11650
11651 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11652 /* If the type is cached, we should have found it above. */
11653 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11654 this_type = read_type_die_1 (type_die, type_cu);
11655 }
11656
11657 /* If we still don't have a type use an error marker. */
11658
11659 if (this_type == NULL)
11660 {
11661 char *message, *saved;
11662
11663 /* read_type_die already issued a complaint. */
11664 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11665 cu->objfile->name,
11666 cu->header.offset,
11667 die->offset);
11668 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11669 message, strlen (message));
11670 xfree (message);
11671
11672 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11673 }
11674
11675 return this_type;
11676 }
11677
11678 /* Return the type in DIE, CU.
11679 Returns NULL for invalid types.
11680
11681 This first does a lookup in the appropriate type_hash table,
11682 and only reads the die in if necessary.
11683
11684 NOTE: This can be called when reading in partial or full symbols. */
11685
11686 static struct type *
11687 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11688 {
11689 struct type *this_type;
11690
11691 this_type = get_die_type (die, cu);
11692 if (this_type)
11693 return this_type;
11694
11695 return read_type_die_1 (die, cu);
11696 }
11697
11698 /* Read the type in DIE, CU.
11699 Returns NULL for invalid types. */
11700
11701 static struct type *
11702 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11703 {
11704 struct type *this_type = NULL;
11705
11706 switch (die->tag)
11707 {
11708 case DW_TAG_class_type:
11709 case DW_TAG_interface_type:
11710 case DW_TAG_structure_type:
11711 case DW_TAG_union_type:
11712 this_type = read_structure_type (die, cu);
11713 break;
11714 case DW_TAG_enumeration_type:
11715 this_type = read_enumeration_type (die, cu);
11716 break;
11717 case DW_TAG_subprogram:
11718 case DW_TAG_subroutine_type:
11719 case DW_TAG_inlined_subroutine:
11720 this_type = read_subroutine_type (die, cu);
11721 break;
11722 case DW_TAG_array_type:
11723 this_type = read_array_type (die, cu);
11724 break;
11725 case DW_TAG_set_type:
11726 this_type = read_set_type (die, cu);
11727 break;
11728 case DW_TAG_pointer_type:
11729 this_type = read_tag_pointer_type (die, cu);
11730 break;
11731 case DW_TAG_ptr_to_member_type:
11732 this_type = read_tag_ptr_to_member_type (die, cu);
11733 break;
11734 case DW_TAG_reference_type:
11735 this_type = read_tag_reference_type (die, cu);
11736 break;
11737 case DW_TAG_const_type:
11738 this_type = read_tag_const_type (die, cu);
11739 break;
11740 case DW_TAG_volatile_type:
11741 this_type = read_tag_volatile_type (die, cu);
11742 break;
11743 case DW_TAG_string_type:
11744 this_type = read_tag_string_type (die, cu);
11745 break;
11746 case DW_TAG_typedef:
11747 this_type = read_typedef (die, cu);
11748 break;
11749 case DW_TAG_subrange_type:
11750 this_type = read_subrange_type (die, cu);
11751 break;
11752 case DW_TAG_base_type:
11753 this_type = read_base_type (die, cu);
11754 break;
11755 case DW_TAG_unspecified_type:
11756 this_type = read_unspecified_type (die, cu);
11757 break;
11758 case DW_TAG_namespace:
11759 this_type = read_namespace_type (die, cu);
11760 break;
11761 case DW_TAG_module:
11762 this_type = read_module_type (die, cu);
11763 break;
11764 default:
11765 complaint (&symfile_complaints,
11766 _("unexpected tag in read_type_die: '%s'"),
11767 dwarf_tag_name (die->tag));
11768 break;
11769 }
11770
11771 return this_type;
11772 }
11773
11774 /* See if we can figure out if the class lives in a namespace. We do
11775 this by looking for a member function; its demangled name will
11776 contain namespace info, if there is any.
11777 Return the computed name or NULL.
11778 Space for the result is allocated on the objfile's obstack.
11779 This is the full-die version of guess_partial_die_structure_name.
11780 In this case we know DIE has no useful parent. */
11781
11782 static char *
11783 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11784 {
11785 struct die_info *spec_die;
11786 struct dwarf2_cu *spec_cu;
11787 struct die_info *child;
11788
11789 spec_cu = cu;
11790 spec_die = die_specification (die, &spec_cu);
11791 if (spec_die != NULL)
11792 {
11793 die = spec_die;
11794 cu = spec_cu;
11795 }
11796
11797 for (child = die->child;
11798 child != NULL;
11799 child = child->sibling)
11800 {
11801 if (child->tag == DW_TAG_subprogram)
11802 {
11803 struct attribute *attr;
11804
11805 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11806 if (attr == NULL)
11807 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11808 if (attr != NULL)
11809 {
11810 char *actual_name
11811 = language_class_name_from_physname (cu->language_defn,
11812 DW_STRING (attr));
11813 char *name = NULL;
11814
11815 if (actual_name != NULL)
11816 {
11817 char *die_name = dwarf2_name (die, cu);
11818
11819 if (die_name != NULL
11820 && strcmp (die_name, actual_name) != 0)
11821 {
11822 /* Strip off the class name from the full name.
11823 We want the prefix. */
11824 int die_name_len = strlen (die_name);
11825 int actual_name_len = strlen (actual_name);
11826
11827 /* Test for '::' as a sanity check. */
11828 if (actual_name_len > die_name_len + 2
11829 && actual_name[actual_name_len
11830 - die_name_len - 1] == ':')
11831 name =
11832 obsavestring (actual_name,
11833 actual_name_len - die_name_len - 2,
11834 &cu->objfile->objfile_obstack);
11835 }
11836 }
11837 xfree (actual_name);
11838 return name;
11839 }
11840 }
11841 }
11842
11843 return NULL;
11844 }
11845
11846 /* Return the name of the namespace/class that DIE is defined within,
11847 or "" if we can't tell. The caller should not xfree the result.
11848
11849 For example, if we're within the method foo() in the following
11850 code:
11851
11852 namespace N {
11853 class C {
11854 void foo () {
11855 }
11856 };
11857 }
11858
11859 then determine_prefix on foo's die will return "N::C". */
11860
11861 static char *
11862 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11863 {
11864 struct die_info *parent, *spec_die;
11865 struct dwarf2_cu *spec_cu;
11866 struct type *parent_type;
11867
11868 if (cu->language != language_cplus && cu->language != language_java
11869 && cu->language != language_fortran)
11870 return "";
11871
11872 /* We have to be careful in the presence of DW_AT_specification.
11873 For example, with GCC 3.4, given the code
11874
11875 namespace N {
11876 void foo() {
11877 // Definition of N::foo.
11878 }
11879 }
11880
11881 then we'll have a tree of DIEs like this:
11882
11883 1: DW_TAG_compile_unit
11884 2: DW_TAG_namespace // N
11885 3: DW_TAG_subprogram // declaration of N::foo
11886 4: DW_TAG_subprogram // definition of N::foo
11887 DW_AT_specification // refers to die #3
11888
11889 Thus, when processing die #4, we have to pretend that we're in
11890 the context of its DW_AT_specification, namely the contex of die
11891 #3. */
11892 spec_cu = cu;
11893 spec_die = die_specification (die, &spec_cu);
11894 if (spec_die == NULL)
11895 parent = die->parent;
11896 else
11897 {
11898 parent = spec_die->parent;
11899 cu = spec_cu;
11900 }
11901
11902 if (parent == NULL)
11903 return "";
11904 else if (parent->building_fullname)
11905 {
11906 const char *name;
11907 const char *parent_name;
11908
11909 /* It has been seen on RealView 2.2 built binaries,
11910 DW_TAG_template_type_param types actually _defined_ as
11911 children of the parent class:
11912
11913 enum E {};
11914 template class <class Enum> Class{};
11915 Class<enum E> class_e;
11916
11917 1: DW_TAG_class_type (Class)
11918 2: DW_TAG_enumeration_type (E)
11919 3: DW_TAG_enumerator (enum1:0)
11920 3: DW_TAG_enumerator (enum2:1)
11921 ...
11922 2: DW_TAG_template_type_param
11923 DW_AT_type DW_FORM_ref_udata (E)
11924
11925 Besides being broken debug info, it can put GDB into an
11926 infinite loop. Consider:
11927
11928 When we're building the full name for Class<E>, we'll start
11929 at Class, and go look over its template type parameters,
11930 finding E. We'll then try to build the full name of E, and
11931 reach here. We're now trying to build the full name of E,
11932 and look over the parent DIE for containing scope. In the
11933 broken case, if we followed the parent DIE of E, we'd again
11934 find Class, and once again go look at its template type
11935 arguments, etc., etc. Simply don't consider such parent die
11936 as source-level parent of this die (it can't be, the language
11937 doesn't allow it), and break the loop here. */
11938 name = dwarf2_name (die, cu);
11939 parent_name = dwarf2_name (parent, cu);
11940 complaint (&symfile_complaints,
11941 _("template param type '%s' defined within parent '%s'"),
11942 name ? name : "<unknown>",
11943 parent_name ? parent_name : "<unknown>");
11944 return "";
11945 }
11946 else
11947 switch (parent->tag)
11948 {
11949 case DW_TAG_namespace:
11950 parent_type = read_type_die (parent, cu);
11951 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11952 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11953 Work around this problem here. */
11954 if (cu->language == language_cplus
11955 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11956 return "";
11957 /* We give a name to even anonymous namespaces. */
11958 return TYPE_TAG_NAME (parent_type);
11959 case DW_TAG_class_type:
11960 case DW_TAG_interface_type:
11961 case DW_TAG_structure_type:
11962 case DW_TAG_union_type:
11963 case DW_TAG_module:
11964 parent_type = read_type_die (parent, cu);
11965 if (TYPE_TAG_NAME (parent_type) != NULL)
11966 return TYPE_TAG_NAME (parent_type);
11967 else
11968 /* An anonymous structure is only allowed non-static data
11969 members; no typedefs, no member functions, et cetera.
11970 So it does not need a prefix. */
11971 return "";
11972 case DW_TAG_compile_unit:
11973 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11974 if (cu->language == language_cplus
11975 && dwarf2_per_objfile->types.asection != NULL
11976 && die->child != NULL
11977 && (die->tag == DW_TAG_class_type
11978 || die->tag == DW_TAG_structure_type
11979 || die->tag == DW_TAG_union_type))
11980 {
11981 char *name = guess_full_die_structure_name (die, cu);
11982 if (name != NULL)
11983 return name;
11984 }
11985 return "";
11986 default:
11987 return determine_prefix (parent, cu);
11988 }
11989 }
11990
11991 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
11992 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11993 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
11994 an obconcat, otherwise allocate storage for the result. The CU argument is
11995 used to determine the language and hence, the appropriate separator. */
11996
11997 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
11998
11999 static char *
12000 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12001 int physname, struct dwarf2_cu *cu)
12002 {
12003 const char *lead = "";
12004 const char *sep;
12005
12006 if (suffix == NULL || suffix[0] == '\0'
12007 || prefix == NULL || prefix[0] == '\0')
12008 sep = "";
12009 else if (cu->language == language_java)
12010 sep = ".";
12011 else if (cu->language == language_fortran && physname)
12012 {
12013 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12014 DW_AT_MIPS_linkage_name is preferred and used instead. */
12015
12016 lead = "__";
12017 sep = "_MOD_";
12018 }
12019 else
12020 sep = "::";
12021
12022 if (prefix == NULL)
12023 prefix = "";
12024 if (suffix == NULL)
12025 suffix = "";
12026
12027 if (obs == NULL)
12028 {
12029 char *retval
12030 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12031
12032 strcpy (retval, lead);
12033 strcat (retval, prefix);
12034 strcat (retval, sep);
12035 strcat (retval, suffix);
12036 return retval;
12037 }
12038 else
12039 {
12040 /* We have an obstack. */
12041 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12042 }
12043 }
12044
12045 /* Return sibling of die, NULL if no sibling. */
12046
12047 static struct die_info *
12048 sibling_die (struct die_info *die)
12049 {
12050 return die->sibling;
12051 }
12052
12053 /* Get name of a die, return NULL if not found. */
12054
12055 static char *
12056 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12057 struct obstack *obstack)
12058 {
12059 if (name && cu->language == language_cplus)
12060 {
12061 char *canon_name = cp_canonicalize_string (name);
12062
12063 if (canon_name != NULL)
12064 {
12065 if (strcmp (canon_name, name) != 0)
12066 name = obsavestring (canon_name, strlen (canon_name),
12067 obstack);
12068 xfree (canon_name);
12069 }
12070 }
12071
12072 return name;
12073 }
12074
12075 /* Get name of a die, return NULL if not found. */
12076
12077 static char *
12078 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12079 {
12080 struct attribute *attr;
12081
12082 attr = dwarf2_attr (die, DW_AT_name, cu);
12083 if ((!attr || !DW_STRING (attr))
12084 && die->tag != DW_TAG_class_type
12085 && die->tag != DW_TAG_interface_type
12086 && die->tag != DW_TAG_structure_type
12087 && die->tag != DW_TAG_union_type)
12088 return NULL;
12089
12090 switch (die->tag)
12091 {
12092 case DW_TAG_compile_unit:
12093 /* Compilation units have a DW_AT_name that is a filename, not
12094 a source language identifier. */
12095 case DW_TAG_enumeration_type:
12096 case DW_TAG_enumerator:
12097 /* These tags always have simple identifiers already; no need
12098 to canonicalize them. */
12099 return DW_STRING (attr);
12100
12101 case DW_TAG_subprogram:
12102 /* Java constructors will all be named "<init>", so return
12103 the class name when we see this special case. */
12104 if (cu->language == language_java
12105 && DW_STRING (attr) != NULL
12106 && strcmp (DW_STRING (attr), "<init>") == 0)
12107 {
12108 struct dwarf2_cu *spec_cu = cu;
12109 struct die_info *spec_die;
12110
12111 /* GCJ will output '<init>' for Java constructor names.
12112 For this special case, return the name of the parent class. */
12113
12114 /* GCJ may output suprogram DIEs with AT_specification set.
12115 If so, use the name of the specified DIE. */
12116 spec_die = die_specification (die, &spec_cu);
12117 if (spec_die != NULL)
12118 return dwarf2_name (spec_die, spec_cu);
12119
12120 do
12121 {
12122 die = die->parent;
12123 if (die->tag == DW_TAG_class_type)
12124 return dwarf2_name (die, cu);
12125 }
12126 while (die->tag != DW_TAG_compile_unit);
12127 }
12128 break;
12129
12130 case DW_TAG_class_type:
12131 case DW_TAG_interface_type:
12132 case DW_TAG_structure_type:
12133 case DW_TAG_union_type:
12134 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12135 structures or unions. These were of the form "._%d" in GCC 4.1,
12136 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12137 and GCC 4.4. We work around this problem by ignoring these. */
12138 if (attr && DW_STRING (attr)
12139 && (strncmp (DW_STRING (attr), "._", 2) == 0
12140 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12141 return NULL;
12142
12143 /* GCC might emit a nameless typedef that has a linkage name. See
12144 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12145 if (!attr || DW_STRING (attr) == NULL)
12146 {
12147 char *demangled = NULL;
12148
12149 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12150 if (attr == NULL)
12151 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12152
12153 if (attr == NULL || DW_STRING (attr) == NULL)
12154 return NULL;
12155
12156 /* Avoid demangling DW_STRING (attr) the second time on a second
12157 call for the same DIE. */
12158 if (!DW_STRING_IS_CANONICAL (attr))
12159 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12160
12161 if (demangled)
12162 {
12163 /* FIXME: we already did this for the partial symbol... */
12164 DW_STRING (attr)
12165 = obsavestring (demangled, strlen (demangled),
12166 &cu->objfile->objfile_obstack);
12167 DW_STRING_IS_CANONICAL (attr) = 1;
12168 xfree (demangled);
12169 }
12170 }
12171 break;
12172
12173 default:
12174 break;
12175 }
12176
12177 if (!DW_STRING_IS_CANONICAL (attr))
12178 {
12179 DW_STRING (attr)
12180 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12181 &cu->objfile->objfile_obstack);
12182 DW_STRING_IS_CANONICAL (attr) = 1;
12183 }
12184 return DW_STRING (attr);
12185 }
12186
12187 /* Return the die that this die in an extension of, or NULL if there
12188 is none. *EXT_CU is the CU containing DIE on input, and the CU
12189 containing the return value on output. */
12190
12191 static struct die_info *
12192 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12193 {
12194 struct attribute *attr;
12195
12196 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12197 if (attr == NULL)
12198 return NULL;
12199
12200 return follow_die_ref (die, attr, ext_cu);
12201 }
12202
12203 /* Convert a DIE tag into its string name. */
12204
12205 static char *
12206 dwarf_tag_name (unsigned tag)
12207 {
12208 switch (tag)
12209 {
12210 case DW_TAG_padding:
12211 return "DW_TAG_padding";
12212 case DW_TAG_array_type:
12213 return "DW_TAG_array_type";
12214 case DW_TAG_class_type:
12215 return "DW_TAG_class_type";
12216 case DW_TAG_entry_point:
12217 return "DW_TAG_entry_point";
12218 case DW_TAG_enumeration_type:
12219 return "DW_TAG_enumeration_type";
12220 case DW_TAG_formal_parameter:
12221 return "DW_TAG_formal_parameter";
12222 case DW_TAG_imported_declaration:
12223 return "DW_TAG_imported_declaration";
12224 case DW_TAG_label:
12225 return "DW_TAG_label";
12226 case DW_TAG_lexical_block:
12227 return "DW_TAG_lexical_block";
12228 case DW_TAG_member:
12229 return "DW_TAG_member";
12230 case DW_TAG_pointer_type:
12231 return "DW_TAG_pointer_type";
12232 case DW_TAG_reference_type:
12233 return "DW_TAG_reference_type";
12234 case DW_TAG_compile_unit:
12235 return "DW_TAG_compile_unit";
12236 case DW_TAG_string_type:
12237 return "DW_TAG_string_type";
12238 case DW_TAG_structure_type:
12239 return "DW_TAG_structure_type";
12240 case DW_TAG_subroutine_type:
12241 return "DW_TAG_subroutine_type";
12242 case DW_TAG_typedef:
12243 return "DW_TAG_typedef";
12244 case DW_TAG_union_type:
12245 return "DW_TAG_union_type";
12246 case DW_TAG_unspecified_parameters:
12247 return "DW_TAG_unspecified_parameters";
12248 case DW_TAG_variant:
12249 return "DW_TAG_variant";
12250 case DW_TAG_common_block:
12251 return "DW_TAG_common_block";
12252 case DW_TAG_common_inclusion:
12253 return "DW_TAG_common_inclusion";
12254 case DW_TAG_inheritance:
12255 return "DW_TAG_inheritance";
12256 case DW_TAG_inlined_subroutine:
12257 return "DW_TAG_inlined_subroutine";
12258 case DW_TAG_module:
12259 return "DW_TAG_module";
12260 case DW_TAG_ptr_to_member_type:
12261 return "DW_TAG_ptr_to_member_type";
12262 case DW_TAG_set_type:
12263 return "DW_TAG_set_type";
12264 case DW_TAG_subrange_type:
12265 return "DW_TAG_subrange_type";
12266 case DW_TAG_with_stmt:
12267 return "DW_TAG_with_stmt";
12268 case DW_TAG_access_declaration:
12269 return "DW_TAG_access_declaration";
12270 case DW_TAG_base_type:
12271 return "DW_TAG_base_type";
12272 case DW_TAG_catch_block:
12273 return "DW_TAG_catch_block";
12274 case DW_TAG_const_type:
12275 return "DW_TAG_const_type";
12276 case DW_TAG_constant:
12277 return "DW_TAG_constant";
12278 case DW_TAG_enumerator:
12279 return "DW_TAG_enumerator";
12280 case DW_TAG_file_type:
12281 return "DW_TAG_file_type";
12282 case DW_TAG_friend:
12283 return "DW_TAG_friend";
12284 case DW_TAG_namelist:
12285 return "DW_TAG_namelist";
12286 case DW_TAG_namelist_item:
12287 return "DW_TAG_namelist_item";
12288 case DW_TAG_packed_type:
12289 return "DW_TAG_packed_type";
12290 case DW_TAG_subprogram:
12291 return "DW_TAG_subprogram";
12292 case DW_TAG_template_type_param:
12293 return "DW_TAG_template_type_param";
12294 case DW_TAG_template_value_param:
12295 return "DW_TAG_template_value_param";
12296 case DW_TAG_thrown_type:
12297 return "DW_TAG_thrown_type";
12298 case DW_TAG_try_block:
12299 return "DW_TAG_try_block";
12300 case DW_TAG_variant_part:
12301 return "DW_TAG_variant_part";
12302 case DW_TAG_variable:
12303 return "DW_TAG_variable";
12304 case DW_TAG_volatile_type:
12305 return "DW_TAG_volatile_type";
12306 case DW_TAG_dwarf_procedure:
12307 return "DW_TAG_dwarf_procedure";
12308 case DW_TAG_restrict_type:
12309 return "DW_TAG_restrict_type";
12310 case DW_TAG_interface_type:
12311 return "DW_TAG_interface_type";
12312 case DW_TAG_namespace:
12313 return "DW_TAG_namespace";
12314 case DW_TAG_imported_module:
12315 return "DW_TAG_imported_module";
12316 case DW_TAG_unspecified_type:
12317 return "DW_TAG_unspecified_type";
12318 case DW_TAG_partial_unit:
12319 return "DW_TAG_partial_unit";
12320 case DW_TAG_imported_unit:
12321 return "DW_TAG_imported_unit";
12322 case DW_TAG_condition:
12323 return "DW_TAG_condition";
12324 case DW_TAG_shared_type:
12325 return "DW_TAG_shared_type";
12326 case DW_TAG_type_unit:
12327 return "DW_TAG_type_unit";
12328 case DW_TAG_MIPS_loop:
12329 return "DW_TAG_MIPS_loop";
12330 case DW_TAG_HP_array_descriptor:
12331 return "DW_TAG_HP_array_descriptor";
12332 case DW_TAG_format_label:
12333 return "DW_TAG_format_label";
12334 case DW_TAG_function_template:
12335 return "DW_TAG_function_template";
12336 case DW_TAG_class_template:
12337 return "DW_TAG_class_template";
12338 case DW_TAG_GNU_BINCL:
12339 return "DW_TAG_GNU_BINCL";
12340 case DW_TAG_GNU_EINCL:
12341 return "DW_TAG_GNU_EINCL";
12342 case DW_TAG_upc_shared_type:
12343 return "DW_TAG_upc_shared_type";
12344 case DW_TAG_upc_strict_type:
12345 return "DW_TAG_upc_strict_type";
12346 case DW_TAG_upc_relaxed_type:
12347 return "DW_TAG_upc_relaxed_type";
12348 case DW_TAG_PGI_kanji_type:
12349 return "DW_TAG_PGI_kanji_type";
12350 case DW_TAG_PGI_interface_block:
12351 return "DW_TAG_PGI_interface_block";
12352 default:
12353 return "DW_TAG_<unknown>";
12354 }
12355 }
12356
12357 /* Convert a DWARF attribute code into its string name. */
12358
12359 static char *
12360 dwarf_attr_name (unsigned attr)
12361 {
12362 switch (attr)
12363 {
12364 case DW_AT_sibling:
12365 return "DW_AT_sibling";
12366 case DW_AT_location:
12367 return "DW_AT_location";
12368 case DW_AT_name:
12369 return "DW_AT_name";
12370 case DW_AT_ordering:
12371 return "DW_AT_ordering";
12372 case DW_AT_subscr_data:
12373 return "DW_AT_subscr_data";
12374 case DW_AT_byte_size:
12375 return "DW_AT_byte_size";
12376 case DW_AT_bit_offset:
12377 return "DW_AT_bit_offset";
12378 case DW_AT_bit_size:
12379 return "DW_AT_bit_size";
12380 case DW_AT_element_list:
12381 return "DW_AT_element_list";
12382 case DW_AT_stmt_list:
12383 return "DW_AT_stmt_list";
12384 case DW_AT_low_pc:
12385 return "DW_AT_low_pc";
12386 case DW_AT_high_pc:
12387 return "DW_AT_high_pc";
12388 case DW_AT_language:
12389 return "DW_AT_language";
12390 case DW_AT_member:
12391 return "DW_AT_member";
12392 case DW_AT_discr:
12393 return "DW_AT_discr";
12394 case DW_AT_discr_value:
12395 return "DW_AT_discr_value";
12396 case DW_AT_visibility:
12397 return "DW_AT_visibility";
12398 case DW_AT_import:
12399 return "DW_AT_import";
12400 case DW_AT_string_length:
12401 return "DW_AT_string_length";
12402 case DW_AT_common_reference:
12403 return "DW_AT_common_reference";
12404 case DW_AT_comp_dir:
12405 return "DW_AT_comp_dir";
12406 case DW_AT_const_value:
12407 return "DW_AT_const_value";
12408 case DW_AT_containing_type:
12409 return "DW_AT_containing_type";
12410 case DW_AT_default_value:
12411 return "DW_AT_default_value";
12412 case DW_AT_inline:
12413 return "DW_AT_inline";
12414 case DW_AT_is_optional:
12415 return "DW_AT_is_optional";
12416 case DW_AT_lower_bound:
12417 return "DW_AT_lower_bound";
12418 case DW_AT_producer:
12419 return "DW_AT_producer";
12420 case DW_AT_prototyped:
12421 return "DW_AT_prototyped";
12422 case DW_AT_return_addr:
12423 return "DW_AT_return_addr";
12424 case DW_AT_start_scope:
12425 return "DW_AT_start_scope";
12426 case DW_AT_bit_stride:
12427 return "DW_AT_bit_stride";
12428 case DW_AT_upper_bound:
12429 return "DW_AT_upper_bound";
12430 case DW_AT_abstract_origin:
12431 return "DW_AT_abstract_origin";
12432 case DW_AT_accessibility:
12433 return "DW_AT_accessibility";
12434 case DW_AT_address_class:
12435 return "DW_AT_address_class";
12436 case DW_AT_artificial:
12437 return "DW_AT_artificial";
12438 case DW_AT_base_types:
12439 return "DW_AT_base_types";
12440 case DW_AT_calling_convention:
12441 return "DW_AT_calling_convention";
12442 case DW_AT_count:
12443 return "DW_AT_count";
12444 case DW_AT_data_member_location:
12445 return "DW_AT_data_member_location";
12446 case DW_AT_decl_column:
12447 return "DW_AT_decl_column";
12448 case DW_AT_decl_file:
12449 return "DW_AT_decl_file";
12450 case DW_AT_decl_line:
12451 return "DW_AT_decl_line";
12452 case DW_AT_declaration:
12453 return "DW_AT_declaration";
12454 case DW_AT_discr_list:
12455 return "DW_AT_discr_list";
12456 case DW_AT_encoding:
12457 return "DW_AT_encoding";
12458 case DW_AT_external:
12459 return "DW_AT_external";
12460 case DW_AT_frame_base:
12461 return "DW_AT_frame_base";
12462 case DW_AT_friend:
12463 return "DW_AT_friend";
12464 case DW_AT_identifier_case:
12465 return "DW_AT_identifier_case";
12466 case DW_AT_macro_info:
12467 return "DW_AT_macro_info";
12468 case DW_AT_namelist_items:
12469 return "DW_AT_namelist_items";
12470 case DW_AT_priority:
12471 return "DW_AT_priority";
12472 case DW_AT_segment:
12473 return "DW_AT_segment";
12474 case DW_AT_specification:
12475 return "DW_AT_specification";
12476 case DW_AT_static_link:
12477 return "DW_AT_static_link";
12478 case DW_AT_type:
12479 return "DW_AT_type";
12480 case DW_AT_use_location:
12481 return "DW_AT_use_location";
12482 case DW_AT_variable_parameter:
12483 return "DW_AT_variable_parameter";
12484 case DW_AT_virtuality:
12485 return "DW_AT_virtuality";
12486 case DW_AT_vtable_elem_location:
12487 return "DW_AT_vtable_elem_location";
12488 /* DWARF 3 values. */
12489 case DW_AT_allocated:
12490 return "DW_AT_allocated";
12491 case DW_AT_associated:
12492 return "DW_AT_associated";
12493 case DW_AT_data_location:
12494 return "DW_AT_data_location";
12495 case DW_AT_byte_stride:
12496 return "DW_AT_byte_stride";
12497 case DW_AT_entry_pc:
12498 return "DW_AT_entry_pc";
12499 case DW_AT_use_UTF8:
12500 return "DW_AT_use_UTF8";
12501 case DW_AT_extension:
12502 return "DW_AT_extension";
12503 case DW_AT_ranges:
12504 return "DW_AT_ranges";
12505 case DW_AT_trampoline:
12506 return "DW_AT_trampoline";
12507 case DW_AT_call_column:
12508 return "DW_AT_call_column";
12509 case DW_AT_call_file:
12510 return "DW_AT_call_file";
12511 case DW_AT_call_line:
12512 return "DW_AT_call_line";
12513 case DW_AT_description:
12514 return "DW_AT_description";
12515 case DW_AT_binary_scale:
12516 return "DW_AT_binary_scale";
12517 case DW_AT_decimal_scale:
12518 return "DW_AT_decimal_scale";
12519 case DW_AT_small:
12520 return "DW_AT_small";
12521 case DW_AT_decimal_sign:
12522 return "DW_AT_decimal_sign";
12523 case DW_AT_digit_count:
12524 return "DW_AT_digit_count";
12525 case DW_AT_picture_string:
12526 return "DW_AT_picture_string";
12527 case DW_AT_mutable:
12528 return "DW_AT_mutable";
12529 case DW_AT_threads_scaled:
12530 return "DW_AT_threads_scaled";
12531 case DW_AT_explicit:
12532 return "DW_AT_explicit";
12533 case DW_AT_object_pointer:
12534 return "DW_AT_object_pointer";
12535 case DW_AT_endianity:
12536 return "DW_AT_endianity";
12537 case DW_AT_elemental:
12538 return "DW_AT_elemental";
12539 case DW_AT_pure:
12540 return "DW_AT_pure";
12541 case DW_AT_recursive:
12542 return "DW_AT_recursive";
12543 /* DWARF 4 values. */
12544 case DW_AT_signature:
12545 return "DW_AT_signature";
12546 case DW_AT_linkage_name:
12547 return "DW_AT_linkage_name";
12548 /* SGI/MIPS extensions. */
12549 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12550 case DW_AT_MIPS_fde:
12551 return "DW_AT_MIPS_fde";
12552 #endif
12553 case DW_AT_MIPS_loop_begin:
12554 return "DW_AT_MIPS_loop_begin";
12555 case DW_AT_MIPS_tail_loop_begin:
12556 return "DW_AT_MIPS_tail_loop_begin";
12557 case DW_AT_MIPS_epilog_begin:
12558 return "DW_AT_MIPS_epilog_begin";
12559 case DW_AT_MIPS_loop_unroll_factor:
12560 return "DW_AT_MIPS_loop_unroll_factor";
12561 case DW_AT_MIPS_software_pipeline_depth:
12562 return "DW_AT_MIPS_software_pipeline_depth";
12563 case DW_AT_MIPS_linkage_name:
12564 return "DW_AT_MIPS_linkage_name";
12565 case DW_AT_MIPS_stride:
12566 return "DW_AT_MIPS_stride";
12567 case DW_AT_MIPS_abstract_name:
12568 return "DW_AT_MIPS_abstract_name";
12569 case DW_AT_MIPS_clone_origin:
12570 return "DW_AT_MIPS_clone_origin";
12571 case DW_AT_MIPS_has_inlines:
12572 return "DW_AT_MIPS_has_inlines";
12573 /* HP extensions. */
12574 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12575 case DW_AT_HP_block_index:
12576 return "DW_AT_HP_block_index";
12577 #endif
12578 case DW_AT_HP_unmodifiable:
12579 return "DW_AT_HP_unmodifiable";
12580 case DW_AT_HP_actuals_stmt_list:
12581 return "DW_AT_HP_actuals_stmt_list";
12582 case DW_AT_HP_proc_per_section:
12583 return "DW_AT_HP_proc_per_section";
12584 case DW_AT_HP_raw_data_ptr:
12585 return "DW_AT_HP_raw_data_ptr";
12586 case DW_AT_HP_pass_by_reference:
12587 return "DW_AT_HP_pass_by_reference";
12588 case DW_AT_HP_opt_level:
12589 return "DW_AT_HP_opt_level";
12590 case DW_AT_HP_prof_version_id:
12591 return "DW_AT_HP_prof_version_id";
12592 case DW_AT_HP_opt_flags:
12593 return "DW_AT_HP_opt_flags";
12594 case DW_AT_HP_cold_region_low_pc:
12595 return "DW_AT_HP_cold_region_low_pc";
12596 case DW_AT_HP_cold_region_high_pc:
12597 return "DW_AT_HP_cold_region_high_pc";
12598 case DW_AT_HP_all_variables_modifiable:
12599 return "DW_AT_HP_all_variables_modifiable";
12600 case DW_AT_HP_linkage_name:
12601 return "DW_AT_HP_linkage_name";
12602 case DW_AT_HP_prof_flags:
12603 return "DW_AT_HP_prof_flags";
12604 /* GNU extensions. */
12605 case DW_AT_sf_names:
12606 return "DW_AT_sf_names";
12607 case DW_AT_src_info:
12608 return "DW_AT_src_info";
12609 case DW_AT_mac_info:
12610 return "DW_AT_mac_info";
12611 case DW_AT_src_coords:
12612 return "DW_AT_src_coords";
12613 case DW_AT_body_begin:
12614 return "DW_AT_body_begin";
12615 case DW_AT_body_end:
12616 return "DW_AT_body_end";
12617 case DW_AT_GNU_vector:
12618 return "DW_AT_GNU_vector";
12619 case DW_AT_GNU_odr_signature:
12620 return "DW_AT_GNU_odr_signature";
12621 /* VMS extensions. */
12622 case DW_AT_VMS_rtnbeg_pd_address:
12623 return "DW_AT_VMS_rtnbeg_pd_address";
12624 /* UPC extension. */
12625 case DW_AT_upc_threads_scaled:
12626 return "DW_AT_upc_threads_scaled";
12627 /* PGI (STMicroelectronics) extensions. */
12628 case DW_AT_PGI_lbase:
12629 return "DW_AT_PGI_lbase";
12630 case DW_AT_PGI_soffset:
12631 return "DW_AT_PGI_soffset";
12632 case DW_AT_PGI_lstride:
12633 return "DW_AT_PGI_lstride";
12634 default:
12635 return "DW_AT_<unknown>";
12636 }
12637 }
12638
12639 /* Convert a DWARF value form code into its string name. */
12640
12641 static char *
12642 dwarf_form_name (unsigned form)
12643 {
12644 switch (form)
12645 {
12646 case DW_FORM_addr:
12647 return "DW_FORM_addr";
12648 case DW_FORM_block2:
12649 return "DW_FORM_block2";
12650 case DW_FORM_block4:
12651 return "DW_FORM_block4";
12652 case DW_FORM_data2:
12653 return "DW_FORM_data2";
12654 case DW_FORM_data4:
12655 return "DW_FORM_data4";
12656 case DW_FORM_data8:
12657 return "DW_FORM_data8";
12658 case DW_FORM_string:
12659 return "DW_FORM_string";
12660 case DW_FORM_block:
12661 return "DW_FORM_block";
12662 case DW_FORM_block1:
12663 return "DW_FORM_block1";
12664 case DW_FORM_data1:
12665 return "DW_FORM_data1";
12666 case DW_FORM_flag:
12667 return "DW_FORM_flag";
12668 case DW_FORM_sdata:
12669 return "DW_FORM_sdata";
12670 case DW_FORM_strp:
12671 return "DW_FORM_strp";
12672 case DW_FORM_udata:
12673 return "DW_FORM_udata";
12674 case DW_FORM_ref_addr:
12675 return "DW_FORM_ref_addr";
12676 case DW_FORM_ref1:
12677 return "DW_FORM_ref1";
12678 case DW_FORM_ref2:
12679 return "DW_FORM_ref2";
12680 case DW_FORM_ref4:
12681 return "DW_FORM_ref4";
12682 case DW_FORM_ref8:
12683 return "DW_FORM_ref8";
12684 case DW_FORM_ref_udata:
12685 return "DW_FORM_ref_udata";
12686 case DW_FORM_indirect:
12687 return "DW_FORM_indirect";
12688 case DW_FORM_sec_offset:
12689 return "DW_FORM_sec_offset";
12690 case DW_FORM_exprloc:
12691 return "DW_FORM_exprloc";
12692 case DW_FORM_flag_present:
12693 return "DW_FORM_flag_present";
12694 case DW_FORM_ref_sig8:
12695 return "DW_FORM_ref_sig8";
12696 default:
12697 return "DW_FORM_<unknown>";
12698 }
12699 }
12700
12701 /* Convert a DWARF stack opcode into its string name. */
12702
12703 const char *
12704 dwarf_stack_op_name (unsigned op)
12705 {
12706 switch (op)
12707 {
12708 case DW_OP_addr:
12709 return "DW_OP_addr";
12710 case DW_OP_deref:
12711 return "DW_OP_deref";
12712 case DW_OP_const1u:
12713 return "DW_OP_const1u";
12714 case DW_OP_const1s:
12715 return "DW_OP_const1s";
12716 case DW_OP_const2u:
12717 return "DW_OP_const2u";
12718 case DW_OP_const2s:
12719 return "DW_OP_const2s";
12720 case DW_OP_const4u:
12721 return "DW_OP_const4u";
12722 case DW_OP_const4s:
12723 return "DW_OP_const4s";
12724 case DW_OP_const8u:
12725 return "DW_OP_const8u";
12726 case DW_OP_const8s:
12727 return "DW_OP_const8s";
12728 case DW_OP_constu:
12729 return "DW_OP_constu";
12730 case DW_OP_consts:
12731 return "DW_OP_consts";
12732 case DW_OP_dup:
12733 return "DW_OP_dup";
12734 case DW_OP_drop:
12735 return "DW_OP_drop";
12736 case DW_OP_over:
12737 return "DW_OP_over";
12738 case DW_OP_pick:
12739 return "DW_OP_pick";
12740 case DW_OP_swap:
12741 return "DW_OP_swap";
12742 case DW_OP_rot:
12743 return "DW_OP_rot";
12744 case DW_OP_xderef:
12745 return "DW_OP_xderef";
12746 case DW_OP_abs:
12747 return "DW_OP_abs";
12748 case DW_OP_and:
12749 return "DW_OP_and";
12750 case DW_OP_div:
12751 return "DW_OP_div";
12752 case DW_OP_minus:
12753 return "DW_OP_minus";
12754 case DW_OP_mod:
12755 return "DW_OP_mod";
12756 case DW_OP_mul:
12757 return "DW_OP_mul";
12758 case DW_OP_neg:
12759 return "DW_OP_neg";
12760 case DW_OP_not:
12761 return "DW_OP_not";
12762 case DW_OP_or:
12763 return "DW_OP_or";
12764 case DW_OP_plus:
12765 return "DW_OP_plus";
12766 case DW_OP_plus_uconst:
12767 return "DW_OP_plus_uconst";
12768 case DW_OP_shl:
12769 return "DW_OP_shl";
12770 case DW_OP_shr:
12771 return "DW_OP_shr";
12772 case DW_OP_shra:
12773 return "DW_OP_shra";
12774 case DW_OP_xor:
12775 return "DW_OP_xor";
12776 case DW_OP_bra:
12777 return "DW_OP_bra";
12778 case DW_OP_eq:
12779 return "DW_OP_eq";
12780 case DW_OP_ge:
12781 return "DW_OP_ge";
12782 case DW_OP_gt:
12783 return "DW_OP_gt";
12784 case DW_OP_le:
12785 return "DW_OP_le";
12786 case DW_OP_lt:
12787 return "DW_OP_lt";
12788 case DW_OP_ne:
12789 return "DW_OP_ne";
12790 case DW_OP_skip:
12791 return "DW_OP_skip";
12792 case DW_OP_lit0:
12793 return "DW_OP_lit0";
12794 case DW_OP_lit1:
12795 return "DW_OP_lit1";
12796 case DW_OP_lit2:
12797 return "DW_OP_lit2";
12798 case DW_OP_lit3:
12799 return "DW_OP_lit3";
12800 case DW_OP_lit4:
12801 return "DW_OP_lit4";
12802 case DW_OP_lit5:
12803 return "DW_OP_lit5";
12804 case DW_OP_lit6:
12805 return "DW_OP_lit6";
12806 case DW_OP_lit7:
12807 return "DW_OP_lit7";
12808 case DW_OP_lit8:
12809 return "DW_OP_lit8";
12810 case DW_OP_lit9:
12811 return "DW_OP_lit9";
12812 case DW_OP_lit10:
12813 return "DW_OP_lit10";
12814 case DW_OP_lit11:
12815 return "DW_OP_lit11";
12816 case DW_OP_lit12:
12817 return "DW_OP_lit12";
12818 case DW_OP_lit13:
12819 return "DW_OP_lit13";
12820 case DW_OP_lit14:
12821 return "DW_OP_lit14";
12822 case DW_OP_lit15:
12823 return "DW_OP_lit15";
12824 case DW_OP_lit16:
12825 return "DW_OP_lit16";
12826 case DW_OP_lit17:
12827 return "DW_OP_lit17";
12828 case DW_OP_lit18:
12829 return "DW_OP_lit18";
12830 case DW_OP_lit19:
12831 return "DW_OP_lit19";
12832 case DW_OP_lit20:
12833 return "DW_OP_lit20";
12834 case DW_OP_lit21:
12835 return "DW_OP_lit21";
12836 case DW_OP_lit22:
12837 return "DW_OP_lit22";
12838 case DW_OP_lit23:
12839 return "DW_OP_lit23";
12840 case DW_OP_lit24:
12841 return "DW_OP_lit24";
12842 case DW_OP_lit25:
12843 return "DW_OP_lit25";
12844 case DW_OP_lit26:
12845 return "DW_OP_lit26";
12846 case DW_OP_lit27:
12847 return "DW_OP_lit27";
12848 case DW_OP_lit28:
12849 return "DW_OP_lit28";
12850 case DW_OP_lit29:
12851 return "DW_OP_lit29";
12852 case DW_OP_lit30:
12853 return "DW_OP_lit30";
12854 case DW_OP_lit31:
12855 return "DW_OP_lit31";
12856 case DW_OP_reg0:
12857 return "DW_OP_reg0";
12858 case DW_OP_reg1:
12859 return "DW_OP_reg1";
12860 case DW_OP_reg2:
12861 return "DW_OP_reg2";
12862 case DW_OP_reg3:
12863 return "DW_OP_reg3";
12864 case DW_OP_reg4:
12865 return "DW_OP_reg4";
12866 case DW_OP_reg5:
12867 return "DW_OP_reg5";
12868 case DW_OP_reg6:
12869 return "DW_OP_reg6";
12870 case DW_OP_reg7:
12871 return "DW_OP_reg7";
12872 case DW_OP_reg8:
12873 return "DW_OP_reg8";
12874 case DW_OP_reg9:
12875 return "DW_OP_reg9";
12876 case DW_OP_reg10:
12877 return "DW_OP_reg10";
12878 case DW_OP_reg11:
12879 return "DW_OP_reg11";
12880 case DW_OP_reg12:
12881 return "DW_OP_reg12";
12882 case DW_OP_reg13:
12883 return "DW_OP_reg13";
12884 case DW_OP_reg14:
12885 return "DW_OP_reg14";
12886 case DW_OP_reg15:
12887 return "DW_OP_reg15";
12888 case DW_OP_reg16:
12889 return "DW_OP_reg16";
12890 case DW_OP_reg17:
12891 return "DW_OP_reg17";
12892 case DW_OP_reg18:
12893 return "DW_OP_reg18";
12894 case DW_OP_reg19:
12895 return "DW_OP_reg19";
12896 case DW_OP_reg20:
12897 return "DW_OP_reg20";
12898 case DW_OP_reg21:
12899 return "DW_OP_reg21";
12900 case DW_OP_reg22:
12901 return "DW_OP_reg22";
12902 case DW_OP_reg23:
12903 return "DW_OP_reg23";
12904 case DW_OP_reg24:
12905 return "DW_OP_reg24";
12906 case DW_OP_reg25:
12907 return "DW_OP_reg25";
12908 case DW_OP_reg26:
12909 return "DW_OP_reg26";
12910 case DW_OP_reg27:
12911 return "DW_OP_reg27";
12912 case DW_OP_reg28:
12913 return "DW_OP_reg28";
12914 case DW_OP_reg29:
12915 return "DW_OP_reg29";
12916 case DW_OP_reg30:
12917 return "DW_OP_reg30";
12918 case DW_OP_reg31:
12919 return "DW_OP_reg31";
12920 case DW_OP_breg0:
12921 return "DW_OP_breg0";
12922 case DW_OP_breg1:
12923 return "DW_OP_breg1";
12924 case DW_OP_breg2:
12925 return "DW_OP_breg2";
12926 case DW_OP_breg3:
12927 return "DW_OP_breg3";
12928 case DW_OP_breg4:
12929 return "DW_OP_breg4";
12930 case DW_OP_breg5:
12931 return "DW_OP_breg5";
12932 case DW_OP_breg6:
12933 return "DW_OP_breg6";
12934 case DW_OP_breg7:
12935 return "DW_OP_breg7";
12936 case DW_OP_breg8:
12937 return "DW_OP_breg8";
12938 case DW_OP_breg9:
12939 return "DW_OP_breg9";
12940 case DW_OP_breg10:
12941 return "DW_OP_breg10";
12942 case DW_OP_breg11:
12943 return "DW_OP_breg11";
12944 case DW_OP_breg12:
12945 return "DW_OP_breg12";
12946 case DW_OP_breg13:
12947 return "DW_OP_breg13";
12948 case DW_OP_breg14:
12949 return "DW_OP_breg14";
12950 case DW_OP_breg15:
12951 return "DW_OP_breg15";
12952 case DW_OP_breg16:
12953 return "DW_OP_breg16";
12954 case DW_OP_breg17:
12955 return "DW_OP_breg17";
12956 case DW_OP_breg18:
12957 return "DW_OP_breg18";
12958 case DW_OP_breg19:
12959 return "DW_OP_breg19";
12960 case DW_OP_breg20:
12961 return "DW_OP_breg20";
12962 case DW_OP_breg21:
12963 return "DW_OP_breg21";
12964 case DW_OP_breg22:
12965 return "DW_OP_breg22";
12966 case DW_OP_breg23:
12967 return "DW_OP_breg23";
12968 case DW_OP_breg24:
12969 return "DW_OP_breg24";
12970 case DW_OP_breg25:
12971 return "DW_OP_breg25";
12972 case DW_OP_breg26:
12973 return "DW_OP_breg26";
12974 case DW_OP_breg27:
12975 return "DW_OP_breg27";
12976 case DW_OP_breg28:
12977 return "DW_OP_breg28";
12978 case DW_OP_breg29:
12979 return "DW_OP_breg29";
12980 case DW_OP_breg30:
12981 return "DW_OP_breg30";
12982 case DW_OP_breg31:
12983 return "DW_OP_breg31";
12984 case DW_OP_regx:
12985 return "DW_OP_regx";
12986 case DW_OP_fbreg:
12987 return "DW_OP_fbreg";
12988 case DW_OP_bregx:
12989 return "DW_OP_bregx";
12990 case DW_OP_piece:
12991 return "DW_OP_piece";
12992 case DW_OP_deref_size:
12993 return "DW_OP_deref_size";
12994 case DW_OP_xderef_size:
12995 return "DW_OP_xderef_size";
12996 case DW_OP_nop:
12997 return "DW_OP_nop";
12998 /* DWARF 3 extensions. */
12999 case DW_OP_push_object_address:
13000 return "DW_OP_push_object_address";
13001 case DW_OP_call2:
13002 return "DW_OP_call2";
13003 case DW_OP_call4:
13004 return "DW_OP_call4";
13005 case DW_OP_call_ref:
13006 return "DW_OP_call_ref";
13007 case DW_OP_form_tls_address:
13008 return "DW_OP_form_tls_address";
13009 case DW_OP_call_frame_cfa:
13010 return "DW_OP_call_frame_cfa";
13011 case DW_OP_bit_piece:
13012 return "DW_OP_bit_piece";
13013 /* DWARF 4 extensions. */
13014 case DW_OP_implicit_value:
13015 return "DW_OP_implicit_value";
13016 case DW_OP_stack_value:
13017 return "DW_OP_stack_value";
13018 /* GNU extensions. */
13019 case DW_OP_GNU_push_tls_address:
13020 return "DW_OP_GNU_push_tls_address";
13021 case DW_OP_GNU_uninit:
13022 return "DW_OP_GNU_uninit";
13023 case DW_OP_GNU_implicit_pointer:
13024 return "DW_OP_GNU_implicit_pointer";
13025 default:
13026 return NULL;
13027 }
13028 }
13029
13030 static char *
13031 dwarf_bool_name (unsigned mybool)
13032 {
13033 if (mybool)
13034 return "TRUE";
13035 else
13036 return "FALSE";
13037 }
13038
13039 /* Convert a DWARF type code into its string name. */
13040
13041 static char *
13042 dwarf_type_encoding_name (unsigned enc)
13043 {
13044 switch (enc)
13045 {
13046 case DW_ATE_void:
13047 return "DW_ATE_void";
13048 case DW_ATE_address:
13049 return "DW_ATE_address";
13050 case DW_ATE_boolean:
13051 return "DW_ATE_boolean";
13052 case DW_ATE_complex_float:
13053 return "DW_ATE_complex_float";
13054 case DW_ATE_float:
13055 return "DW_ATE_float";
13056 case DW_ATE_signed:
13057 return "DW_ATE_signed";
13058 case DW_ATE_signed_char:
13059 return "DW_ATE_signed_char";
13060 case DW_ATE_unsigned:
13061 return "DW_ATE_unsigned";
13062 case DW_ATE_unsigned_char:
13063 return "DW_ATE_unsigned_char";
13064 /* DWARF 3. */
13065 case DW_ATE_imaginary_float:
13066 return "DW_ATE_imaginary_float";
13067 case DW_ATE_packed_decimal:
13068 return "DW_ATE_packed_decimal";
13069 case DW_ATE_numeric_string:
13070 return "DW_ATE_numeric_string";
13071 case DW_ATE_edited:
13072 return "DW_ATE_edited";
13073 case DW_ATE_signed_fixed:
13074 return "DW_ATE_signed_fixed";
13075 case DW_ATE_unsigned_fixed:
13076 return "DW_ATE_unsigned_fixed";
13077 case DW_ATE_decimal_float:
13078 return "DW_ATE_decimal_float";
13079 /* DWARF 4. */
13080 case DW_ATE_UTF:
13081 return "DW_ATE_UTF";
13082 /* HP extensions. */
13083 case DW_ATE_HP_float80:
13084 return "DW_ATE_HP_float80";
13085 case DW_ATE_HP_complex_float80:
13086 return "DW_ATE_HP_complex_float80";
13087 case DW_ATE_HP_float128:
13088 return "DW_ATE_HP_float128";
13089 case DW_ATE_HP_complex_float128:
13090 return "DW_ATE_HP_complex_float128";
13091 case DW_ATE_HP_floathpintel:
13092 return "DW_ATE_HP_floathpintel";
13093 case DW_ATE_HP_imaginary_float80:
13094 return "DW_ATE_HP_imaginary_float80";
13095 case DW_ATE_HP_imaginary_float128:
13096 return "DW_ATE_HP_imaginary_float128";
13097 default:
13098 return "DW_ATE_<unknown>";
13099 }
13100 }
13101
13102 /* Convert a DWARF call frame info operation to its string name. */
13103
13104 #if 0
13105 static char *
13106 dwarf_cfi_name (unsigned cfi_opc)
13107 {
13108 switch (cfi_opc)
13109 {
13110 case DW_CFA_advance_loc:
13111 return "DW_CFA_advance_loc";
13112 case DW_CFA_offset:
13113 return "DW_CFA_offset";
13114 case DW_CFA_restore:
13115 return "DW_CFA_restore";
13116 case DW_CFA_nop:
13117 return "DW_CFA_nop";
13118 case DW_CFA_set_loc:
13119 return "DW_CFA_set_loc";
13120 case DW_CFA_advance_loc1:
13121 return "DW_CFA_advance_loc1";
13122 case DW_CFA_advance_loc2:
13123 return "DW_CFA_advance_loc2";
13124 case DW_CFA_advance_loc4:
13125 return "DW_CFA_advance_loc4";
13126 case DW_CFA_offset_extended:
13127 return "DW_CFA_offset_extended";
13128 case DW_CFA_restore_extended:
13129 return "DW_CFA_restore_extended";
13130 case DW_CFA_undefined:
13131 return "DW_CFA_undefined";
13132 case DW_CFA_same_value:
13133 return "DW_CFA_same_value";
13134 case DW_CFA_register:
13135 return "DW_CFA_register";
13136 case DW_CFA_remember_state:
13137 return "DW_CFA_remember_state";
13138 case DW_CFA_restore_state:
13139 return "DW_CFA_restore_state";
13140 case DW_CFA_def_cfa:
13141 return "DW_CFA_def_cfa";
13142 case DW_CFA_def_cfa_register:
13143 return "DW_CFA_def_cfa_register";
13144 case DW_CFA_def_cfa_offset:
13145 return "DW_CFA_def_cfa_offset";
13146 /* DWARF 3. */
13147 case DW_CFA_def_cfa_expression:
13148 return "DW_CFA_def_cfa_expression";
13149 case DW_CFA_expression:
13150 return "DW_CFA_expression";
13151 case DW_CFA_offset_extended_sf:
13152 return "DW_CFA_offset_extended_sf";
13153 case DW_CFA_def_cfa_sf:
13154 return "DW_CFA_def_cfa_sf";
13155 case DW_CFA_def_cfa_offset_sf:
13156 return "DW_CFA_def_cfa_offset_sf";
13157 case DW_CFA_val_offset:
13158 return "DW_CFA_val_offset";
13159 case DW_CFA_val_offset_sf:
13160 return "DW_CFA_val_offset_sf";
13161 case DW_CFA_val_expression:
13162 return "DW_CFA_val_expression";
13163 /* SGI/MIPS specific. */
13164 case DW_CFA_MIPS_advance_loc8:
13165 return "DW_CFA_MIPS_advance_loc8";
13166 /* GNU extensions. */
13167 case DW_CFA_GNU_window_save:
13168 return "DW_CFA_GNU_window_save";
13169 case DW_CFA_GNU_args_size:
13170 return "DW_CFA_GNU_args_size";
13171 case DW_CFA_GNU_negative_offset_extended:
13172 return "DW_CFA_GNU_negative_offset_extended";
13173 default:
13174 return "DW_CFA_<unknown>";
13175 }
13176 }
13177 #endif
13178
13179 static void
13180 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13181 {
13182 unsigned int i;
13183
13184 print_spaces (indent, f);
13185 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13186 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13187
13188 if (die->parent != NULL)
13189 {
13190 print_spaces (indent, f);
13191 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13192 die->parent->offset);
13193 }
13194
13195 print_spaces (indent, f);
13196 fprintf_unfiltered (f, " has children: %s\n",
13197 dwarf_bool_name (die->child != NULL));
13198
13199 print_spaces (indent, f);
13200 fprintf_unfiltered (f, " attributes:\n");
13201
13202 for (i = 0; i < die->num_attrs; ++i)
13203 {
13204 print_spaces (indent, f);
13205 fprintf_unfiltered (f, " %s (%s) ",
13206 dwarf_attr_name (die->attrs[i].name),
13207 dwarf_form_name (die->attrs[i].form));
13208
13209 switch (die->attrs[i].form)
13210 {
13211 case DW_FORM_ref_addr:
13212 case DW_FORM_addr:
13213 fprintf_unfiltered (f, "address: ");
13214 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13215 break;
13216 case DW_FORM_block2:
13217 case DW_FORM_block4:
13218 case DW_FORM_block:
13219 case DW_FORM_block1:
13220 fprintf_unfiltered (f, "block: size %d",
13221 DW_BLOCK (&die->attrs[i])->size);
13222 break;
13223 case DW_FORM_exprloc:
13224 fprintf_unfiltered (f, "expression: size %u",
13225 DW_BLOCK (&die->attrs[i])->size);
13226 break;
13227 case DW_FORM_ref1:
13228 case DW_FORM_ref2:
13229 case DW_FORM_ref4:
13230 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13231 (long) (DW_ADDR (&die->attrs[i])));
13232 break;
13233 case DW_FORM_data1:
13234 case DW_FORM_data2:
13235 case DW_FORM_data4:
13236 case DW_FORM_data8:
13237 case DW_FORM_udata:
13238 case DW_FORM_sdata:
13239 fprintf_unfiltered (f, "constant: %s",
13240 pulongest (DW_UNSND (&die->attrs[i])));
13241 break;
13242 case DW_FORM_sec_offset:
13243 fprintf_unfiltered (f, "section offset: %s",
13244 pulongest (DW_UNSND (&die->attrs[i])));
13245 break;
13246 case DW_FORM_ref_sig8:
13247 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13248 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13249 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
13250 else
13251 fprintf_unfiltered (f, "signatured type, offset: unknown");
13252 break;
13253 case DW_FORM_string:
13254 case DW_FORM_strp:
13255 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13256 DW_STRING (&die->attrs[i])
13257 ? DW_STRING (&die->attrs[i]) : "",
13258 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13259 break;
13260 case DW_FORM_flag:
13261 if (DW_UNSND (&die->attrs[i]))
13262 fprintf_unfiltered (f, "flag: TRUE");
13263 else
13264 fprintf_unfiltered (f, "flag: FALSE");
13265 break;
13266 case DW_FORM_flag_present:
13267 fprintf_unfiltered (f, "flag: TRUE");
13268 break;
13269 case DW_FORM_indirect:
13270 /* The reader will have reduced the indirect form to
13271 the "base form" so this form should not occur. */
13272 fprintf_unfiltered (f,
13273 "unexpected attribute form: DW_FORM_indirect");
13274 break;
13275 default:
13276 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13277 die->attrs[i].form);
13278 break;
13279 }
13280 fprintf_unfiltered (f, "\n");
13281 }
13282 }
13283
13284 static void
13285 dump_die_for_error (struct die_info *die)
13286 {
13287 dump_die_shallow (gdb_stderr, 0, die);
13288 }
13289
13290 static void
13291 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13292 {
13293 int indent = level * 4;
13294
13295 gdb_assert (die != NULL);
13296
13297 if (level >= max_level)
13298 return;
13299
13300 dump_die_shallow (f, indent, die);
13301
13302 if (die->child != NULL)
13303 {
13304 print_spaces (indent, f);
13305 fprintf_unfiltered (f, " Children:");
13306 if (level + 1 < max_level)
13307 {
13308 fprintf_unfiltered (f, "\n");
13309 dump_die_1 (f, level + 1, max_level, die->child);
13310 }
13311 else
13312 {
13313 fprintf_unfiltered (f,
13314 " [not printed, max nesting level reached]\n");
13315 }
13316 }
13317
13318 if (die->sibling != NULL && level > 0)
13319 {
13320 dump_die_1 (f, level, max_level, die->sibling);
13321 }
13322 }
13323
13324 /* This is called from the pdie macro in gdbinit.in.
13325 It's not static so gcc will keep a copy callable from gdb. */
13326
13327 void
13328 dump_die (struct die_info *die, int max_level)
13329 {
13330 dump_die_1 (gdb_stdlog, 0, max_level, die);
13331 }
13332
13333 static void
13334 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13335 {
13336 void **slot;
13337
13338 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13339
13340 *slot = die;
13341 }
13342
13343 static int
13344 is_ref_attr (struct attribute *attr)
13345 {
13346 switch (attr->form)
13347 {
13348 case DW_FORM_ref_addr:
13349 case DW_FORM_ref1:
13350 case DW_FORM_ref2:
13351 case DW_FORM_ref4:
13352 case DW_FORM_ref8:
13353 case DW_FORM_ref_udata:
13354 return 1;
13355 default:
13356 return 0;
13357 }
13358 }
13359
13360 static unsigned int
13361 dwarf2_get_ref_die_offset (struct attribute *attr)
13362 {
13363 if (is_ref_attr (attr))
13364 return DW_ADDR (attr);
13365
13366 complaint (&symfile_complaints,
13367 _("unsupported die ref attribute form: '%s'"),
13368 dwarf_form_name (attr->form));
13369 return 0;
13370 }
13371
13372 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13373 * the value held by the attribute is not constant. */
13374
13375 static LONGEST
13376 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13377 {
13378 if (attr->form == DW_FORM_sdata)
13379 return DW_SND (attr);
13380 else if (attr->form == DW_FORM_udata
13381 || attr->form == DW_FORM_data1
13382 || attr->form == DW_FORM_data2
13383 || attr->form == DW_FORM_data4
13384 || attr->form == DW_FORM_data8)
13385 return DW_UNSND (attr);
13386 else
13387 {
13388 complaint (&symfile_complaints,
13389 _("Attribute value is not a constant (%s)"),
13390 dwarf_form_name (attr->form));
13391 return default_value;
13392 }
13393 }
13394
13395 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
13396 unit and add it to our queue.
13397 The result is non-zero if PER_CU was queued, otherwise the result is zero
13398 meaning either PER_CU is already queued or it is already loaded. */
13399
13400 static int
13401 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13402 struct dwarf2_per_cu_data *per_cu)
13403 {
13404 /* We may arrive here during partial symbol reading, if we need full
13405 DIEs to process an unusual case (e.g. template arguments). Do
13406 not queue PER_CU, just tell our caller to load its DIEs. */
13407 if (dwarf2_per_objfile->reading_partial_symbols)
13408 {
13409 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13410 return 1;
13411 return 0;
13412 }
13413
13414 /* Mark the dependence relation so that we don't flush PER_CU
13415 too early. */
13416 dwarf2_add_dependence (this_cu, per_cu);
13417
13418 /* If it's already on the queue, we have nothing to do. */
13419 if (per_cu->queued)
13420 return 0;
13421
13422 /* If the compilation unit is already loaded, just mark it as
13423 used. */
13424 if (per_cu->cu != NULL)
13425 {
13426 per_cu->cu->last_used = 0;
13427 return 0;
13428 }
13429
13430 /* Add it to the queue. */
13431 queue_comp_unit (per_cu, this_cu->objfile);
13432
13433 return 1;
13434 }
13435
13436 /* Follow reference or signature attribute ATTR of SRC_DIE.
13437 On entry *REF_CU is the CU of SRC_DIE.
13438 On exit *REF_CU is the CU of the result. */
13439
13440 static struct die_info *
13441 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13442 struct dwarf2_cu **ref_cu)
13443 {
13444 struct die_info *die;
13445
13446 if (is_ref_attr (attr))
13447 die = follow_die_ref (src_die, attr, ref_cu);
13448 else if (attr->form == DW_FORM_ref_sig8)
13449 die = follow_die_sig (src_die, attr, ref_cu);
13450 else
13451 {
13452 dump_die_for_error (src_die);
13453 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13454 (*ref_cu)->objfile->name);
13455 }
13456
13457 return die;
13458 }
13459
13460 /* Follow reference OFFSET.
13461 On entry *REF_CU is the CU of the source die referencing OFFSET.
13462 On exit *REF_CU is the CU of the result.
13463 Returns NULL if OFFSET is invalid. */
13464
13465 static struct die_info *
13466 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13467 {
13468 struct die_info temp_die;
13469 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13470
13471 gdb_assert (cu->per_cu != NULL);
13472
13473 target_cu = cu;
13474
13475 if (cu->per_cu->from_debug_types)
13476 {
13477 /* .debug_types CUs cannot reference anything outside their CU.
13478 If they need to, they have to reference a signatured type via
13479 DW_FORM_ref_sig8. */
13480 if (! offset_in_cu_p (&cu->header, offset))
13481 return NULL;
13482 }
13483 else if (! offset_in_cu_p (&cu->header, offset))
13484 {
13485 struct dwarf2_per_cu_data *per_cu;
13486
13487 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13488
13489 /* If necessary, add it to the queue and load its DIEs. */
13490 if (maybe_queue_comp_unit (cu, per_cu))
13491 load_full_comp_unit (per_cu, cu->objfile);
13492
13493 target_cu = per_cu->cu;
13494 }
13495 else if (cu->dies == NULL)
13496 {
13497 /* We're loading full DIEs during partial symbol reading. */
13498 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13499 load_full_comp_unit (cu->per_cu, cu->objfile);
13500 }
13501
13502 *ref_cu = target_cu;
13503 temp_die.offset = offset;
13504 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13505 }
13506
13507 /* Follow reference attribute ATTR of SRC_DIE.
13508 On entry *REF_CU is the CU of SRC_DIE.
13509 On exit *REF_CU is the CU of the result. */
13510
13511 static struct die_info *
13512 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13513 struct dwarf2_cu **ref_cu)
13514 {
13515 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13516 struct dwarf2_cu *cu = *ref_cu;
13517 struct die_info *die;
13518
13519 die = follow_die_offset (offset, ref_cu);
13520 if (!die)
13521 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13522 "at 0x%x [in module %s]"),
13523 offset, src_die->offset, cu->objfile->name);
13524
13525 return die;
13526 }
13527
13528 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13529 value is intended for DW_OP_call*. */
13530
13531 struct dwarf2_locexpr_baton
13532 dwarf2_fetch_die_location_block (unsigned int offset,
13533 struct dwarf2_per_cu_data *per_cu,
13534 CORE_ADDR (*get_frame_pc) (void *baton),
13535 void *baton)
13536 {
13537 struct dwarf2_cu *cu = per_cu->cu;
13538 struct die_info *die;
13539 struct attribute *attr;
13540 struct dwarf2_locexpr_baton retval;
13541
13542 dw2_setup (per_cu->objfile);
13543
13544 die = follow_die_offset (offset, &cu);
13545 if (!die)
13546 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13547 offset, per_cu->cu->objfile->name);
13548
13549 attr = dwarf2_attr (die, DW_AT_location, cu);
13550 if (!attr)
13551 {
13552 /* DWARF: "If there is no such attribute, then there is no effect.". */
13553
13554 retval.data = NULL;
13555 retval.size = 0;
13556 }
13557 else if (attr_form_is_section_offset (attr))
13558 {
13559 struct dwarf2_loclist_baton loclist_baton;
13560 CORE_ADDR pc = (*get_frame_pc) (baton);
13561 size_t size;
13562
13563 fill_in_loclist_baton (cu, &loclist_baton, attr);
13564
13565 retval.data = dwarf2_find_location_expression (&loclist_baton,
13566 &size, pc);
13567 retval.size = size;
13568 }
13569 else
13570 {
13571 if (!attr_form_is_block (attr))
13572 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13573 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13574 offset, per_cu->cu->objfile->name);
13575
13576 retval.data = DW_BLOCK (attr)->data;
13577 retval.size = DW_BLOCK (attr)->size;
13578 }
13579 retval.per_cu = cu->per_cu;
13580 return retval;
13581 }
13582
13583 /* Follow the signature attribute ATTR in SRC_DIE.
13584 On entry *REF_CU is the CU of SRC_DIE.
13585 On exit *REF_CU is the CU of the result. */
13586
13587 static struct die_info *
13588 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13589 struct dwarf2_cu **ref_cu)
13590 {
13591 struct objfile *objfile = (*ref_cu)->objfile;
13592 struct die_info temp_die;
13593 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13594 struct dwarf2_cu *sig_cu;
13595 struct die_info *die;
13596
13597 /* sig_type will be NULL if the signatured type is missing from
13598 the debug info. */
13599 if (sig_type == NULL)
13600 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13601 "at 0x%x [in module %s]"),
13602 src_die->offset, objfile->name);
13603
13604 /* If necessary, add it to the queue and load its DIEs. */
13605
13606 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13607 read_signatured_type (objfile, sig_type);
13608
13609 gdb_assert (sig_type->per_cu.cu != NULL);
13610
13611 sig_cu = sig_type->per_cu.cu;
13612 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13613 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13614 if (die)
13615 {
13616 *ref_cu = sig_cu;
13617 return die;
13618 }
13619
13620 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13621 "from DIE at 0x%x [in module %s]"),
13622 sig_type->type_offset, src_die->offset, objfile->name);
13623 }
13624
13625 /* Given an offset of a signatured type, return its signatured_type. */
13626
13627 static struct signatured_type *
13628 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13629 {
13630 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13631 unsigned int length, initial_length_size;
13632 unsigned int sig_offset;
13633 struct signatured_type find_entry, *type_sig;
13634
13635 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13636 sig_offset = (initial_length_size
13637 + 2 /*version*/
13638 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13639 + 1 /*address_size*/);
13640 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13641 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13642
13643 /* This is only used to lookup previously recorded types.
13644 If we didn't find it, it's our bug. */
13645 gdb_assert (type_sig != NULL);
13646 gdb_assert (offset == type_sig->offset);
13647
13648 return type_sig;
13649 }
13650
13651 /* Read in signatured type at OFFSET and build its CU and die(s). */
13652
13653 static void
13654 read_signatured_type_at_offset (struct objfile *objfile,
13655 unsigned int offset)
13656 {
13657 struct signatured_type *type_sig;
13658
13659 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13660
13661 /* We have the section offset, but we need the signature to do the
13662 hash table lookup. */
13663 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13664
13665 gdb_assert (type_sig->per_cu.cu == NULL);
13666
13667 read_signatured_type (objfile, type_sig);
13668
13669 gdb_assert (type_sig->per_cu.cu != NULL);
13670 }
13671
13672 /* Read in a signatured type and build its CU and DIEs. */
13673
13674 static void
13675 read_signatured_type (struct objfile *objfile,
13676 struct signatured_type *type_sig)
13677 {
13678 gdb_byte *types_ptr;
13679 struct die_reader_specs reader_specs;
13680 struct dwarf2_cu *cu;
13681 ULONGEST signature;
13682 struct cleanup *back_to, *free_cu_cleanup;
13683
13684 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13685 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13686
13687 gdb_assert (type_sig->per_cu.cu == NULL);
13688
13689 cu = xmalloc (sizeof (*cu));
13690 init_one_comp_unit (cu, objfile);
13691
13692 type_sig->per_cu.cu = cu;
13693 cu->per_cu = &type_sig->per_cu;
13694
13695 /* If an error occurs while loading, release our storage. */
13696 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13697
13698 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13699 types_ptr, objfile->obfd);
13700 gdb_assert (signature == type_sig->signature);
13701
13702 cu->die_hash
13703 = htab_create_alloc_ex (cu->header.length / 12,
13704 die_hash,
13705 die_eq,
13706 NULL,
13707 &cu->comp_unit_obstack,
13708 hashtab_obstack_allocate,
13709 dummy_obstack_deallocate);
13710
13711 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13712 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13713
13714 init_cu_die_reader (&reader_specs, cu);
13715
13716 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13717 NULL /*parent*/);
13718
13719 /* We try not to read any attributes in this function, because not
13720 all objfiles needed for references have been loaded yet, and symbol
13721 table processing isn't initialized. But we have to set the CU language,
13722 or we won't be able to build types correctly. */
13723 prepare_one_comp_unit (cu, cu->dies);
13724
13725 do_cleanups (back_to);
13726
13727 /* We've successfully allocated this compilation unit. Let our caller
13728 clean it up when finished with it. */
13729 discard_cleanups (free_cu_cleanup);
13730
13731 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13732 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13733 }
13734
13735 /* Decode simple location descriptions.
13736 Given a pointer to a dwarf block that defines a location, compute
13737 the location and return the value.
13738
13739 NOTE drow/2003-11-18: This function is called in two situations
13740 now: for the address of static or global variables (partial symbols
13741 only) and for offsets into structures which are expected to be
13742 (more or less) constant. The partial symbol case should go away,
13743 and only the constant case should remain. That will let this
13744 function complain more accurately. A few special modes are allowed
13745 without complaint for global variables (for instance, global
13746 register values and thread-local values).
13747
13748 A location description containing no operations indicates that the
13749 object is optimized out. The return value is 0 for that case.
13750 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13751 callers will only want a very basic result and this can become a
13752 complaint.
13753
13754 Note that stack[0] is unused except as a default error return. */
13755
13756 static CORE_ADDR
13757 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13758 {
13759 struct objfile *objfile = cu->objfile;
13760 int i;
13761 int size = blk->size;
13762 gdb_byte *data = blk->data;
13763 CORE_ADDR stack[64];
13764 int stacki;
13765 unsigned int bytes_read, unsnd;
13766 gdb_byte op;
13767
13768 i = 0;
13769 stacki = 0;
13770 stack[stacki] = 0;
13771 stack[++stacki] = 0;
13772
13773 while (i < size)
13774 {
13775 op = data[i++];
13776 switch (op)
13777 {
13778 case DW_OP_lit0:
13779 case DW_OP_lit1:
13780 case DW_OP_lit2:
13781 case DW_OP_lit3:
13782 case DW_OP_lit4:
13783 case DW_OP_lit5:
13784 case DW_OP_lit6:
13785 case DW_OP_lit7:
13786 case DW_OP_lit8:
13787 case DW_OP_lit9:
13788 case DW_OP_lit10:
13789 case DW_OP_lit11:
13790 case DW_OP_lit12:
13791 case DW_OP_lit13:
13792 case DW_OP_lit14:
13793 case DW_OP_lit15:
13794 case DW_OP_lit16:
13795 case DW_OP_lit17:
13796 case DW_OP_lit18:
13797 case DW_OP_lit19:
13798 case DW_OP_lit20:
13799 case DW_OP_lit21:
13800 case DW_OP_lit22:
13801 case DW_OP_lit23:
13802 case DW_OP_lit24:
13803 case DW_OP_lit25:
13804 case DW_OP_lit26:
13805 case DW_OP_lit27:
13806 case DW_OP_lit28:
13807 case DW_OP_lit29:
13808 case DW_OP_lit30:
13809 case DW_OP_lit31:
13810 stack[++stacki] = op - DW_OP_lit0;
13811 break;
13812
13813 case DW_OP_reg0:
13814 case DW_OP_reg1:
13815 case DW_OP_reg2:
13816 case DW_OP_reg3:
13817 case DW_OP_reg4:
13818 case DW_OP_reg5:
13819 case DW_OP_reg6:
13820 case DW_OP_reg7:
13821 case DW_OP_reg8:
13822 case DW_OP_reg9:
13823 case DW_OP_reg10:
13824 case DW_OP_reg11:
13825 case DW_OP_reg12:
13826 case DW_OP_reg13:
13827 case DW_OP_reg14:
13828 case DW_OP_reg15:
13829 case DW_OP_reg16:
13830 case DW_OP_reg17:
13831 case DW_OP_reg18:
13832 case DW_OP_reg19:
13833 case DW_OP_reg20:
13834 case DW_OP_reg21:
13835 case DW_OP_reg22:
13836 case DW_OP_reg23:
13837 case DW_OP_reg24:
13838 case DW_OP_reg25:
13839 case DW_OP_reg26:
13840 case DW_OP_reg27:
13841 case DW_OP_reg28:
13842 case DW_OP_reg29:
13843 case DW_OP_reg30:
13844 case DW_OP_reg31:
13845 stack[++stacki] = op - DW_OP_reg0;
13846 if (i < size)
13847 dwarf2_complex_location_expr_complaint ();
13848 break;
13849
13850 case DW_OP_regx:
13851 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13852 i += bytes_read;
13853 stack[++stacki] = unsnd;
13854 if (i < size)
13855 dwarf2_complex_location_expr_complaint ();
13856 break;
13857
13858 case DW_OP_addr:
13859 stack[++stacki] = read_address (objfile->obfd, &data[i],
13860 cu, &bytes_read);
13861 i += bytes_read;
13862 break;
13863
13864 case DW_OP_const1u:
13865 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13866 i += 1;
13867 break;
13868
13869 case DW_OP_const1s:
13870 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13871 i += 1;
13872 break;
13873
13874 case DW_OP_const2u:
13875 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13876 i += 2;
13877 break;
13878
13879 case DW_OP_const2s:
13880 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13881 i += 2;
13882 break;
13883
13884 case DW_OP_const4u:
13885 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13886 i += 4;
13887 break;
13888
13889 case DW_OP_const4s:
13890 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13891 i += 4;
13892 break;
13893
13894 case DW_OP_constu:
13895 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13896 &bytes_read);
13897 i += bytes_read;
13898 break;
13899
13900 case DW_OP_consts:
13901 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13902 i += bytes_read;
13903 break;
13904
13905 case DW_OP_dup:
13906 stack[stacki + 1] = stack[stacki];
13907 stacki++;
13908 break;
13909
13910 case DW_OP_plus:
13911 stack[stacki - 1] += stack[stacki];
13912 stacki--;
13913 break;
13914
13915 case DW_OP_plus_uconst:
13916 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
13917 &bytes_read);
13918 i += bytes_read;
13919 break;
13920
13921 case DW_OP_minus:
13922 stack[stacki - 1] -= stack[stacki];
13923 stacki--;
13924 break;
13925
13926 case DW_OP_deref:
13927 /* If we're not the last op, then we definitely can't encode
13928 this using GDB's address_class enum. This is valid for partial
13929 global symbols, although the variable's address will be bogus
13930 in the psymtab. */
13931 if (i < size)
13932 dwarf2_complex_location_expr_complaint ();
13933 break;
13934
13935 case DW_OP_GNU_push_tls_address:
13936 /* The top of the stack has the offset from the beginning
13937 of the thread control block at which the variable is located. */
13938 /* Nothing should follow this operator, so the top of stack would
13939 be returned. */
13940 /* This is valid for partial global symbols, but the variable's
13941 address will be bogus in the psymtab. */
13942 if (i < size)
13943 dwarf2_complex_location_expr_complaint ();
13944 break;
13945
13946 case DW_OP_GNU_uninit:
13947 break;
13948
13949 default:
13950 {
13951 const char *name = dwarf_stack_op_name (op);
13952
13953 if (name)
13954 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13955 name);
13956 else
13957 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
13958 op);
13959 }
13960
13961 return (stack[stacki]);
13962 }
13963
13964 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13965 outside of the allocated space. Also enforce minimum>0. */
13966 if (stacki >= ARRAY_SIZE (stack) - 1)
13967 {
13968 complaint (&symfile_complaints,
13969 _("location description stack overflow"));
13970 return 0;
13971 }
13972
13973 if (stacki <= 0)
13974 {
13975 complaint (&symfile_complaints,
13976 _("location description stack underflow"));
13977 return 0;
13978 }
13979 }
13980 return (stack[stacki]);
13981 }
13982
13983 /* memory allocation interface */
13984
13985 static struct dwarf_block *
13986 dwarf_alloc_block (struct dwarf2_cu *cu)
13987 {
13988 struct dwarf_block *blk;
13989
13990 blk = (struct dwarf_block *)
13991 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13992 return (blk);
13993 }
13994
13995 static struct abbrev_info *
13996 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13997 {
13998 struct abbrev_info *abbrev;
13999
14000 abbrev = (struct abbrev_info *)
14001 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14002 memset (abbrev, 0, sizeof (struct abbrev_info));
14003 return (abbrev);
14004 }
14005
14006 static struct die_info *
14007 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14008 {
14009 struct die_info *die;
14010 size_t size = sizeof (struct die_info);
14011
14012 if (num_attrs > 1)
14013 size += (num_attrs - 1) * sizeof (struct attribute);
14014
14015 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14016 memset (die, 0, sizeof (struct die_info));
14017 return (die);
14018 }
14019
14020 \f
14021 /* Macro support. */
14022
14023 /* Return the full name of file number I in *LH's file name table.
14024 Use COMP_DIR as the name of the current directory of the
14025 compilation. The result is allocated using xmalloc; the caller is
14026 responsible for freeing it. */
14027 static char *
14028 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14029 {
14030 /* Is the file number a valid index into the line header's file name
14031 table? Remember that file numbers start with one, not zero. */
14032 if (1 <= file && file <= lh->num_file_names)
14033 {
14034 struct file_entry *fe = &lh->file_names[file - 1];
14035
14036 if (IS_ABSOLUTE_PATH (fe->name))
14037 return xstrdup (fe->name);
14038 else
14039 {
14040 const char *dir;
14041 int dir_len;
14042 char *full_name;
14043
14044 if (fe->dir_index)
14045 dir = lh->include_dirs[fe->dir_index - 1];
14046 else
14047 dir = comp_dir;
14048
14049 if (dir)
14050 {
14051 dir_len = strlen (dir);
14052 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14053 strcpy (full_name, dir);
14054 full_name[dir_len] = '/';
14055 strcpy (full_name + dir_len + 1, fe->name);
14056 return full_name;
14057 }
14058 else
14059 return xstrdup (fe->name);
14060 }
14061 }
14062 else
14063 {
14064 /* The compiler produced a bogus file number. We can at least
14065 record the macro definitions made in the file, even if we
14066 won't be able to find the file by name. */
14067 char fake_name[80];
14068
14069 sprintf (fake_name, "<bad macro file number %d>", file);
14070
14071 complaint (&symfile_complaints,
14072 _("bad file number in macro information (%d)"),
14073 file);
14074
14075 return xstrdup (fake_name);
14076 }
14077 }
14078
14079
14080 static struct macro_source_file *
14081 macro_start_file (int file, int line,
14082 struct macro_source_file *current_file,
14083 const char *comp_dir,
14084 struct line_header *lh, struct objfile *objfile)
14085 {
14086 /* The full name of this source file. */
14087 char *full_name = file_full_name (file, lh, comp_dir);
14088
14089 /* We don't create a macro table for this compilation unit
14090 at all until we actually get a filename. */
14091 if (! pending_macros)
14092 pending_macros = new_macro_table (&objfile->objfile_obstack,
14093 objfile->macro_cache);
14094
14095 if (! current_file)
14096 /* If we have no current file, then this must be the start_file
14097 directive for the compilation unit's main source file. */
14098 current_file = macro_set_main (pending_macros, full_name);
14099 else
14100 current_file = macro_include (current_file, line, full_name);
14101
14102 xfree (full_name);
14103
14104 return current_file;
14105 }
14106
14107
14108 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14109 followed by a null byte. */
14110 static char *
14111 copy_string (const char *buf, int len)
14112 {
14113 char *s = xmalloc (len + 1);
14114
14115 memcpy (s, buf, len);
14116 s[len] = '\0';
14117 return s;
14118 }
14119
14120
14121 static const char *
14122 consume_improper_spaces (const char *p, const char *body)
14123 {
14124 if (*p == ' ')
14125 {
14126 complaint (&symfile_complaints,
14127 _("macro definition contains spaces "
14128 "in formal argument list:\n`%s'"),
14129 body);
14130
14131 while (*p == ' ')
14132 p++;
14133 }
14134
14135 return p;
14136 }
14137
14138
14139 static void
14140 parse_macro_definition (struct macro_source_file *file, int line,
14141 const char *body)
14142 {
14143 const char *p;
14144
14145 /* The body string takes one of two forms. For object-like macro
14146 definitions, it should be:
14147
14148 <macro name> " " <definition>
14149
14150 For function-like macro definitions, it should be:
14151
14152 <macro name> "() " <definition>
14153 or
14154 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14155
14156 Spaces may appear only where explicitly indicated, and in the
14157 <definition>.
14158
14159 The Dwarf 2 spec says that an object-like macro's name is always
14160 followed by a space, but versions of GCC around March 2002 omit
14161 the space when the macro's definition is the empty string.
14162
14163 The Dwarf 2 spec says that there should be no spaces between the
14164 formal arguments in a function-like macro's formal argument list,
14165 but versions of GCC around March 2002 include spaces after the
14166 commas. */
14167
14168
14169 /* Find the extent of the macro name. The macro name is terminated
14170 by either a space or null character (for an object-like macro) or
14171 an opening paren (for a function-like macro). */
14172 for (p = body; *p; p++)
14173 if (*p == ' ' || *p == '(')
14174 break;
14175
14176 if (*p == ' ' || *p == '\0')
14177 {
14178 /* It's an object-like macro. */
14179 int name_len = p - body;
14180 char *name = copy_string (body, name_len);
14181 const char *replacement;
14182
14183 if (*p == ' ')
14184 replacement = body + name_len + 1;
14185 else
14186 {
14187 dwarf2_macro_malformed_definition_complaint (body);
14188 replacement = body + name_len;
14189 }
14190
14191 macro_define_object (file, line, name, replacement);
14192
14193 xfree (name);
14194 }
14195 else if (*p == '(')
14196 {
14197 /* It's a function-like macro. */
14198 char *name = copy_string (body, p - body);
14199 int argc = 0;
14200 int argv_size = 1;
14201 char **argv = xmalloc (argv_size * sizeof (*argv));
14202
14203 p++;
14204
14205 p = consume_improper_spaces (p, body);
14206
14207 /* Parse the formal argument list. */
14208 while (*p && *p != ')')
14209 {
14210 /* Find the extent of the current argument name. */
14211 const char *arg_start = p;
14212
14213 while (*p && *p != ',' && *p != ')' && *p != ' ')
14214 p++;
14215
14216 if (! *p || p == arg_start)
14217 dwarf2_macro_malformed_definition_complaint (body);
14218 else
14219 {
14220 /* Make sure argv has room for the new argument. */
14221 if (argc >= argv_size)
14222 {
14223 argv_size *= 2;
14224 argv = xrealloc (argv, argv_size * sizeof (*argv));
14225 }
14226
14227 argv[argc++] = copy_string (arg_start, p - arg_start);
14228 }
14229
14230 p = consume_improper_spaces (p, body);
14231
14232 /* Consume the comma, if present. */
14233 if (*p == ',')
14234 {
14235 p++;
14236
14237 p = consume_improper_spaces (p, body);
14238 }
14239 }
14240
14241 if (*p == ')')
14242 {
14243 p++;
14244
14245 if (*p == ' ')
14246 /* Perfectly formed definition, no complaints. */
14247 macro_define_function (file, line, name,
14248 argc, (const char **) argv,
14249 p + 1);
14250 else if (*p == '\0')
14251 {
14252 /* Complain, but do define it. */
14253 dwarf2_macro_malformed_definition_complaint (body);
14254 macro_define_function (file, line, name,
14255 argc, (const char **) argv,
14256 p);
14257 }
14258 else
14259 /* Just complain. */
14260 dwarf2_macro_malformed_definition_complaint (body);
14261 }
14262 else
14263 /* Just complain. */
14264 dwarf2_macro_malformed_definition_complaint (body);
14265
14266 xfree (name);
14267 {
14268 int i;
14269
14270 for (i = 0; i < argc; i++)
14271 xfree (argv[i]);
14272 }
14273 xfree (argv);
14274 }
14275 else
14276 dwarf2_macro_malformed_definition_complaint (body);
14277 }
14278
14279
14280 static void
14281 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14282 char *comp_dir, bfd *abfd,
14283 struct dwarf2_cu *cu)
14284 {
14285 gdb_byte *mac_ptr, *mac_end;
14286 struct macro_source_file *current_file = 0;
14287 enum dwarf_macinfo_record_type macinfo_type;
14288 int at_commandline;
14289
14290 dwarf2_read_section (dwarf2_per_objfile->objfile,
14291 &dwarf2_per_objfile->macinfo);
14292 if (dwarf2_per_objfile->macinfo.buffer == NULL)
14293 {
14294 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14295 return;
14296 }
14297
14298 /* First pass: Find the name of the base filename.
14299 This filename is needed in order to process all macros whose definition
14300 (or undefinition) comes from the command line. These macros are defined
14301 before the first DW_MACINFO_start_file entry, and yet still need to be
14302 associated to the base file.
14303
14304 To determine the base file name, we scan the macro definitions until we
14305 reach the first DW_MACINFO_start_file entry. We then initialize
14306 CURRENT_FILE accordingly so that any macro definition found before the
14307 first DW_MACINFO_start_file can still be associated to the base file. */
14308
14309 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14310 mac_end = dwarf2_per_objfile->macinfo.buffer
14311 + dwarf2_per_objfile->macinfo.size;
14312
14313 do
14314 {
14315 /* Do we at least have room for a macinfo type byte? */
14316 if (mac_ptr >= mac_end)
14317 {
14318 /* Complaint is printed during the second pass as GDB will probably
14319 stop the first pass earlier upon finding
14320 DW_MACINFO_start_file. */
14321 break;
14322 }
14323
14324 macinfo_type = read_1_byte (abfd, mac_ptr);
14325 mac_ptr++;
14326
14327 switch (macinfo_type)
14328 {
14329 /* A zero macinfo type indicates the end of the macro
14330 information. */
14331 case 0:
14332 break;
14333
14334 case DW_MACINFO_define:
14335 case DW_MACINFO_undef:
14336 /* Only skip the data by MAC_PTR. */
14337 {
14338 unsigned int bytes_read;
14339
14340 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14341 mac_ptr += bytes_read;
14342 read_direct_string (abfd, mac_ptr, &bytes_read);
14343 mac_ptr += bytes_read;
14344 }
14345 break;
14346
14347 case DW_MACINFO_start_file:
14348 {
14349 unsigned int bytes_read;
14350 int line, file;
14351
14352 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14353 mac_ptr += bytes_read;
14354 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14355 mac_ptr += bytes_read;
14356
14357 current_file = macro_start_file (file, line, current_file,
14358 comp_dir, lh, cu->objfile);
14359 }
14360 break;
14361
14362 case DW_MACINFO_end_file:
14363 /* No data to skip by MAC_PTR. */
14364 break;
14365
14366 case DW_MACINFO_vendor_ext:
14367 /* Only skip the data by MAC_PTR. */
14368 {
14369 unsigned int bytes_read;
14370
14371 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14372 mac_ptr += bytes_read;
14373 read_direct_string (abfd, mac_ptr, &bytes_read);
14374 mac_ptr += bytes_read;
14375 }
14376 break;
14377
14378 default:
14379 break;
14380 }
14381 } while (macinfo_type != 0 && current_file == NULL);
14382
14383 /* Second pass: Process all entries.
14384
14385 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14386 command-line macro definitions/undefinitions. This flag is unset when we
14387 reach the first DW_MACINFO_start_file entry. */
14388
14389 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14390
14391 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14392 GDB is still reading the definitions from command line. First
14393 DW_MACINFO_start_file will need to be ignored as it was already executed
14394 to create CURRENT_FILE for the main source holding also the command line
14395 definitions. On first met DW_MACINFO_start_file this flag is reset to
14396 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14397
14398 at_commandline = 1;
14399
14400 do
14401 {
14402 /* Do we at least have room for a macinfo type byte? */
14403 if (mac_ptr >= mac_end)
14404 {
14405 dwarf2_macros_too_long_complaint ();
14406 break;
14407 }
14408
14409 macinfo_type = read_1_byte (abfd, mac_ptr);
14410 mac_ptr++;
14411
14412 switch (macinfo_type)
14413 {
14414 /* A zero macinfo type indicates the end of the macro
14415 information. */
14416 case 0:
14417 break;
14418
14419 case DW_MACINFO_define:
14420 case DW_MACINFO_undef:
14421 {
14422 unsigned int bytes_read;
14423 int line;
14424 char *body;
14425
14426 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14427 mac_ptr += bytes_read;
14428 body = read_direct_string (abfd, mac_ptr, &bytes_read);
14429 mac_ptr += bytes_read;
14430
14431 if (! current_file)
14432 {
14433 /* DWARF violation as no main source is present. */
14434 complaint (&symfile_complaints,
14435 _("debug info with no main source gives macro %s "
14436 "on line %d: %s"),
14437 macinfo_type == DW_MACINFO_define ?
14438 _("definition") :
14439 macinfo_type == DW_MACINFO_undef ?
14440 _("undefinition") :
14441 _("something-or-other"), line, body);
14442 break;
14443 }
14444 if ((line == 0 && !at_commandline)
14445 || (line != 0 && at_commandline))
14446 complaint (&symfile_complaints,
14447 _("debug info gives %s macro %s with %s line %d: %s"),
14448 at_commandline ? _("command-line") : _("in-file"),
14449 macinfo_type == DW_MACINFO_define ?
14450 _("definition") :
14451 macinfo_type == DW_MACINFO_undef ?
14452 _("undefinition") :
14453 _("something-or-other"),
14454 line == 0 ? _("zero") : _("non-zero"), line, body);
14455
14456 if (macinfo_type == DW_MACINFO_define)
14457 parse_macro_definition (current_file, line, body);
14458 else if (macinfo_type == DW_MACINFO_undef)
14459 macro_undef (current_file, line, body);
14460 }
14461 break;
14462
14463 case DW_MACINFO_start_file:
14464 {
14465 unsigned int bytes_read;
14466 int line, file;
14467
14468 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14469 mac_ptr += bytes_read;
14470 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14471 mac_ptr += bytes_read;
14472
14473 if ((line == 0 && !at_commandline)
14474 || (line != 0 && at_commandline))
14475 complaint (&symfile_complaints,
14476 _("debug info gives source %d included "
14477 "from %s at %s line %d"),
14478 file, at_commandline ? _("command-line") : _("file"),
14479 line == 0 ? _("zero") : _("non-zero"), line);
14480
14481 if (at_commandline)
14482 {
14483 /* This DW_MACINFO_start_file was executed in the pass one. */
14484 at_commandline = 0;
14485 }
14486 else
14487 current_file = macro_start_file (file, line,
14488 current_file, comp_dir,
14489 lh, cu->objfile);
14490 }
14491 break;
14492
14493 case DW_MACINFO_end_file:
14494 if (! current_file)
14495 complaint (&symfile_complaints,
14496 _("macro debug info has an unmatched "
14497 "`close_file' directive"));
14498 else
14499 {
14500 current_file = current_file->included_by;
14501 if (! current_file)
14502 {
14503 enum dwarf_macinfo_record_type next_type;
14504
14505 /* GCC circa March 2002 doesn't produce the zero
14506 type byte marking the end of the compilation
14507 unit. Complain if it's not there, but exit no
14508 matter what. */
14509
14510 /* Do we at least have room for a macinfo type byte? */
14511 if (mac_ptr >= mac_end)
14512 {
14513 dwarf2_macros_too_long_complaint ();
14514 return;
14515 }
14516
14517 /* We don't increment mac_ptr here, so this is just
14518 a look-ahead. */
14519 next_type = read_1_byte (abfd, mac_ptr);
14520 if (next_type != 0)
14521 complaint (&symfile_complaints,
14522 _("no terminating 0-type entry for "
14523 "macros in `.debug_macinfo' section"));
14524
14525 return;
14526 }
14527 }
14528 break;
14529
14530 case DW_MACINFO_vendor_ext:
14531 {
14532 unsigned int bytes_read;
14533 int constant;
14534
14535 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14536 mac_ptr += bytes_read;
14537 read_direct_string (abfd, mac_ptr, &bytes_read);
14538 mac_ptr += bytes_read;
14539
14540 /* We don't recognize any vendor extensions. */
14541 }
14542 break;
14543 }
14544 } while (macinfo_type != 0);
14545 }
14546
14547 /* Check if the attribute's form is a DW_FORM_block*
14548 if so return true else false. */
14549 static int
14550 attr_form_is_block (struct attribute *attr)
14551 {
14552 return (attr == NULL ? 0 :
14553 attr->form == DW_FORM_block1
14554 || attr->form == DW_FORM_block2
14555 || attr->form == DW_FORM_block4
14556 || attr->form == DW_FORM_block
14557 || attr->form == DW_FORM_exprloc);
14558 }
14559
14560 /* Return non-zero if ATTR's value is a section offset --- classes
14561 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14562 You may use DW_UNSND (attr) to retrieve such offsets.
14563
14564 Section 7.5.4, "Attribute Encodings", explains that no attribute
14565 may have a value that belongs to more than one of these classes; it
14566 would be ambiguous if we did, because we use the same forms for all
14567 of them. */
14568 static int
14569 attr_form_is_section_offset (struct attribute *attr)
14570 {
14571 return (attr->form == DW_FORM_data4
14572 || attr->form == DW_FORM_data8
14573 || attr->form == DW_FORM_sec_offset);
14574 }
14575
14576
14577 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14578 zero otherwise. When this function returns true, you can apply
14579 dwarf2_get_attr_constant_value to it.
14580
14581 However, note that for some attributes you must check
14582 attr_form_is_section_offset before using this test. DW_FORM_data4
14583 and DW_FORM_data8 are members of both the constant class, and of
14584 the classes that contain offsets into other debug sections
14585 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14586 that, if an attribute's can be either a constant or one of the
14587 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14588 taken as section offsets, not constants. */
14589 static int
14590 attr_form_is_constant (struct attribute *attr)
14591 {
14592 switch (attr->form)
14593 {
14594 case DW_FORM_sdata:
14595 case DW_FORM_udata:
14596 case DW_FORM_data1:
14597 case DW_FORM_data2:
14598 case DW_FORM_data4:
14599 case DW_FORM_data8:
14600 return 1;
14601 default:
14602 return 0;
14603 }
14604 }
14605
14606 /* A helper function that fills in a dwarf2_loclist_baton. */
14607
14608 static void
14609 fill_in_loclist_baton (struct dwarf2_cu *cu,
14610 struct dwarf2_loclist_baton *baton,
14611 struct attribute *attr)
14612 {
14613 dwarf2_read_section (dwarf2_per_objfile->objfile,
14614 &dwarf2_per_objfile->loc);
14615
14616 baton->per_cu = cu->per_cu;
14617 gdb_assert (baton->per_cu);
14618 /* We don't know how long the location list is, but make sure we
14619 don't run off the edge of the section. */
14620 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14621 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14622 baton->base_address = cu->base_address;
14623 }
14624
14625 static void
14626 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14627 struct dwarf2_cu *cu)
14628 {
14629 if (attr_form_is_section_offset (attr)
14630 /* ".debug_loc" may not exist at all, or the offset may be outside
14631 the section. If so, fall through to the complaint in the
14632 other branch. */
14633 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14634 &dwarf2_per_objfile->loc))
14635 {
14636 struct dwarf2_loclist_baton *baton;
14637
14638 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14639 sizeof (struct dwarf2_loclist_baton));
14640
14641 fill_in_loclist_baton (cu, baton, attr);
14642
14643 if (cu->base_known == 0)
14644 complaint (&symfile_complaints,
14645 _("Location list used without "
14646 "specifying the CU base address."));
14647
14648 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14649 SYMBOL_LOCATION_BATON (sym) = baton;
14650 }
14651 else
14652 {
14653 struct dwarf2_locexpr_baton *baton;
14654
14655 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14656 sizeof (struct dwarf2_locexpr_baton));
14657 baton->per_cu = cu->per_cu;
14658 gdb_assert (baton->per_cu);
14659
14660 if (attr_form_is_block (attr))
14661 {
14662 /* Note that we're just copying the block's data pointer
14663 here, not the actual data. We're still pointing into the
14664 info_buffer for SYM's objfile; right now we never release
14665 that buffer, but when we do clean up properly this may
14666 need to change. */
14667 baton->size = DW_BLOCK (attr)->size;
14668 baton->data = DW_BLOCK (attr)->data;
14669 }
14670 else
14671 {
14672 dwarf2_invalid_attrib_class_complaint ("location description",
14673 SYMBOL_NATURAL_NAME (sym));
14674 baton->size = 0;
14675 baton->data = NULL;
14676 }
14677
14678 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14679 SYMBOL_LOCATION_BATON (sym) = baton;
14680 }
14681 }
14682
14683 /* Return the OBJFILE associated with the compilation unit CU. If CU
14684 came from a separate debuginfo file, then the master objfile is
14685 returned. */
14686
14687 struct objfile *
14688 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14689 {
14690 struct objfile *objfile = per_cu->objfile;
14691
14692 /* Return the master objfile, so that we can report and look up the
14693 correct file containing this variable. */
14694 if (objfile->separate_debug_objfile_backlink)
14695 objfile = objfile->separate_debug_objfile_backlink;
14696
14697 return objfile;
14698 }
14699
14700 /* Return the address size given in the compilation unit header for CU. */
14701
14702 CORE_ADDR
14703 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14704 {
14705 if (per_cu->cu)
14706 return per_cu->cu->header.addr_size;
14707 else
14708 {
14709 /* If the CU is not currently read in, we re-read its header. */
14710 struct objfile *objfile = per_cu->objfile;
14711 struct dwarf2_per_objfile *per_objfile
14712 = objfile_data (objfile, dwarf2_objfile_data_key);
14713 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14714 struct comp_unit_head cu_header;
14715
14716 memset (&cu_header, 0, sizeof cu_header);
14717 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14718 return cu_header.addr_size;
14719 }
14720 }
14721
14722 /* Return the offset size given in the compilation unit header for CU. */
14723
14724 int
14725 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14726 {
14727 if (per_cu->cu)
14728 return per_cu->cu->header.offset_size;
14729 else
14730 {
14731 /* If the CU is not currently read in, we re-read its header. */
14732 struct objfile *objfile = per_cu->objfile;
14733 struct dwarf2_per_objfile *per_objfile
14734 = objfile_data (objfile, dwarf2_objfile_data_key);
14735 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14736 struct comp_unit_head cu_header;
14737
14738 memset (&cu_header, 0, sizeof cu_header);
14739 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14740 return cu_header.offset_size;
14741 }
14742 }
14743
14744 /* Return the text offset of the CU. The returned offset comes from
14745 this CU's objfile. If this objfile came from a separate debuginfo
14746 file, then the offset may be different from the corresponding
14747 offset in the parent objfile. */
14748
14749 CORE_ADDR
14750 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14751 {
14752 struct objfile *objfile = per_cu->objfile;
14753
14754 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14755 }
14756
14757 /* Locate the .debug_info compilation unit from CU's objfile which contains
14758 the DIE at OFFSET. Raises an error on failure. */
14759
14760 static struct dwarf2_per_cu_data *
14761 dwarf2_find_containing_comp_unit (unsigned int offset,
14762 struct objfile *objfile)
14763 {
14764 struct dwarf2_per_cu_data *this_cu;
14765 int low, high;
14766
14767 low = 0;
14768 high = dwarf2_per_objfile->n_comp_units - 1;
14769 while (high > low)
14770 {
14771 int mid = low + (high - low) / 2;
14772
14773 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14774 high = mid;
14775 else
14776 low = mid + 1;
14777 }
14778 gdb_assert (low == high);
14779 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14780 {
14781 if (low == 0)
14782 error (_("Dwarf Error: could not find partial DIE containing "
14783 "offset 0x%lx [in module %s]"),
14784 (long) offset, bfd_get_filename (objfile->obfd));
14785
14786 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14787 return dwarf2_per_objfile->all_comp_units[low-1];
14788 }
14789 else
14790 {
14791 this_cu = dwarf2_per_objfile->all_comp_units[low];
14792 if (low == dwarf2_per_objfile->n_comp_units - 1
14793 && offset >= this_cu->offset + this_cu->length)
14794 error (_("invalid dwarf2 offset %u"), offset);
14795 gdb_assert (offset < this_cu->offset + this_cu->length);
14796 return this_cu;
14797 }
14798 }
14799
14800 /* Locate the compilation unit from OBJFILE which is located at exactly
14801 OFFSET. Raises an error on failure. */
14802
14803 static struct dwarf2_per_cu_data *
14804 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14805 {
14806 struct dwarf2_per_cu_data *this_cu;
14807
14808 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14809 if (this_cu->offset != offset)
14810 error (_("no compilation unit with offset %u."), offset);
14811 return this_cu;
14812 }
14813
14814 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
14815
14816 static void
14817 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14818 {
14819 memset (cu, 0, sizeof (*cu));
14820 cu->objfile = objfile;
14821 obstack_init (&cu->comp_unit_obstack);
14822 }
14823
14824 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14825
14826 static void
14827 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14828 {
14829 struct attribute *attr;
14830
14831 /* Set the language we're debugging. */
14832 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14833 if (attr)
14834 set_cu_language (DW_UNSND (attr), cu);
14835 else
14836 {
14837 cu->language = language_minimal;
14838 cu->language_defn = language_def (cu->language);
14839 }
14840 }
14841
14842 /* Release one cached compilation unit, CU. We unlink it from the tree
14843 of compilation units, but we don't remove it from the read_in_chain;
14844 the caller is responsible for that.
14845 NOTE: DATA is a void * because this function is also used as a
14846 cleanup routine. */
14847
14848 static void
14849 free_one_comp_unit (void *data)
14850 {
14851 struct dwarf2_cu *cu = data;
14852
14853 if (cu->per_cu != NULL)
14854 cu->per_cu->cu = NULL;
14855 cu->per_cu = NULL;
14856
14857 obstack_free (&cu->comp_unit_obstack, NULL);
14858
14859 xfree (cu);
14860 }
14861
14862 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14863 when we're finished with it. We can't free the pointer itself, but be
14864 sure to unlink it from the cache. Also release any associated storage
14865 and perform cache maintenance.
14866
14867 Only used during partial symbol parsing. */
14868
14869 static void
14870 free_stack_comp_unit (void *data)
14871 {
14872 struct dwarf2_cu *cu = data;
14873
14874 obstack_free (&cu->comp_unit_obstack, NULL);
14875 cu->partial_dies = NULL;
14876
14877 if (cu->per_cu != NULL)
14878 {
14879 /* This compilation unit is on the stack in our caller, so we
14880 should not xfree it. Just unlink it. */
14881 cu->per_cu->cu = NULL;
14882 cu->per_cu = NULL;
14883
14884 /* If we had a per-cu pointer, then we may have other compilation
14885 units loaded, so age them now. */
14886 age_cached_comp_units ();
14887 }
14888 }
14889
14890 /* Free all cached compilation units. */
14891
14892 static void
14893 free_cached_comp_units (void *data)
14894 {
14895 struct dwarf2_per_cu_data *per_cu, **last_chain;
14896
14897 per_cu = dwarf2_per_objfile->read_in_chain;
14898 last_chain = &dwarf2_per_objfile->read_in_chain;
14899 while (per_cu != NULL)
14900 {
14901 struct dwarf2_per_cu_data *next_cu;
14902
14903 next_cu = per_cu->cu->read_in_chain;
14904
14905 free_one_comp_unit (per_cu->cu);
14906 *last_chain = next_cu;
14907
14908 per_cu = next_cu;
14909 }
14910 }
14911
14912 /* Increase the age counter on each cached compilation unit, and free
14913 any that are too old. */
14914
14915 static void
14916 age_cached_comp_units (void)
14917 {
14918 struct dwarf2_per_cu_data *per_cu, **last_chain;
14919
14920 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14921 per_cu = dwarf2_per_objfile->read_in_chain;
14922 while (per_cu != NULL)
14923 {
14924 per_cu->cu->last_used ++;
14925 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14926 dwarf2_mark (per_cu->cu);
14927 per_cu = per_cu->cu->read_in_chain;
14928 }
14929
14930 per_cu = dwarf2_per_objfile->read_in_chain;
14931 last_chain = &dwarf2_per_objfile->read_in_chain;
14932 while (per_cu != NULL)
14933 {
14934 struct dwarf2_per_cu_data *next_cu;
14935
14936 next_cu = per_cu->cu->read_in_chain;
14937
14938 if (!per_cu->cu->mark)
14939 {
14940 free_one_comp_unit (per_cu->cu);
14941 *last_chain = next_cu;
14942 }
14943 else
14944 last_chain = &per_cu->cu->read_in_chain;
14945
14946 per_cu = next_cu;
14947 }
14948 }
14949
14950 /* Remove a single compilation unit from the cache. */
14951
14952 static void
14953 free_one_cached_comp_unit (void *target_cu)
14954 {
14955 struct dwarf2_per_cu_data *per_cu, **last_chain;
14956
14957 per_cu = dwarf2_per_objfile->read_in_chain;
14958 last_chain = &dwarf2_per_objfile->read_in_chain;
14959 while (per_cu != NULL)
14960 {
14961 struct dwarf2_per_cu_data *next_cu;
14962
14963 next_cu = per_cu->cu->read_in_chain;
14964
14965 if (per_cu->cu == target_cu)
14966 {
14967 free_one_comp_unit (per_cu->cu);
14968 *last_chain = next_cu;
14969 break;
14970 }
14971 else
14972 last_chain = &per_cu->cu->read_in_chain;
14973
14974 per_cu = next_cu;
14975 }
14976 }
14977
14978 /* Release all extra memory associated with OBJFILE. */
14979
14980 void
14981 dwarf2_free_objfile (struct objfile *objfile)
14982 {
14983 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14984
14985 if (dwarf2_per_objfile == NULL)
14986 return;
14987
14988 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14989 free_cached_comp_units (NULL);
14990
14991 if (dwarf2_per_objfile->quick_file_names_table)
14992 htab_delete (dwarf2_per_objfile->quick_file_names_table);
14993
14994 /* Everything else should be on the objfile obstack. */
14995 }
14996
14997 /* A pair of DIE offset and GDB type pointer. We store these
14998 in a hash table separate from the DIEs, and preserve them
14999 when the DIEs are flushed out of cache. */
15000
15001 struct dwarf2_offset_and_type
15002 {
15003 unsigned int offset;
15004 struct type *type;
15005 };
15006
15007 /* Hash function for a dwarf2_offset_and_type. */
15008
15009 static hashval_t
15010 offset_and_type_hash (const void *item)
15011 {
15012 const struct dwarf2_offset_and_type *ofs = item;
15013
15014 return ofs->offset;
15015 }
15016
15017 /* Equality function for a dwarf2_offset_and_type. */
15018
15019 static int
15020 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15021 {
15022 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15023 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15024
15025 return ofs_lhs->offset == ofs_rhs->offset;
15026 }
15027
15028 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15029 table if necessary. For convenience, return TYPE.
15030
15031 The DIEs reading must have careful ordering to:
15032 * Not cause infite loops trying to read in DIEs as a prerequisite for
15033 reading current DIE.
15034 * Not trying to dereference contents of still incompletely read in types
15035 while reading in other DIEs.
15036 * Enable referencing still incompletely read in types just by a pointer to
15037 the type without accessing its fields.
15038
15039 Therefore caller should follow these rules:
15040 * Try to fetch any prerequisite types we may need to build this DIE type
15041 before building the type and calling set_die_type.
15042 * After building type call set_die_type for current DIE as soon as
15043 possible before fetching more types to complete the current type.
15044 * Make the type as complete as possible before fetching more types. */
15045
15046 static struct type *
15047 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15048 {
15049 struct dwarf2_offset_and_type **slot, ofs;
15050 struct objfile *objfile = cu->objfile;
15051 htab_t *type_hash_ptr;
15052
15053 /* For Ada types, make sure that the gnat-specific data is always
15054 initialized (if not already set). There are a few types where
15055 we should not be doing so, because the type-specific area is
15056 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15057 where the type-specific area is used to store the floatformat).
15058 But this is not a problem, because the gnat-specific information
15059 is actually not needed for these types. */
15060 if (need_gnat_info (cu)
15061 && TYPE_CODE (type) != TYPE_CODE_FUNC
15062 && TYPE_CODE (type) != TYPE_CODE_FLT
15063 && !HAVE_GNAT_AUX_INFO (type))
15064 INIT_GNAT_SPECIFIC (type);
15065
15066 if (cu->per_cu->from_debug_types)
15067 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15068 else
15069 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15070
15071 if (*type_hash_ptr == NULL)
15072 {
15073 *type_hash_ptr
15074 = htab_create_alloc_ex (127,
15075 offset_and_type_hash,
15076 offset_and_type_eq,
15077 NULL,
15078 &objfile->objfile_obstack,
15079 hashtab_obstack_allocate,
15080 dummy_obstack_deallocate);
15081 }
15082
15083 ofs.offset = die->offset;
15084 ofs.type = type;
15085 slot = (struct dwarf2_offset_and_type **)
15086 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15087 if (*slot)
15088 complaint (&symfile_complaints,
15089 _("A problem internal to GDB: DIE 0x%x has type already set"),
15090 die->offset);
15091 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15092 **slot = ofs;
15093 return type;
15094 }
15095
15096 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15097 table, or return NULL if the die does not have a saved type. */
15098
15099 static struct type *
15100 get_die_type_at_offset (unsigned int offset,
15101 struct dwarf2_per_cu_data *per_cu)
15102 {
15103 struct dwarf2_offset_and_type *slot, ofs;
15104 htab_t type_hash;
15105
15106 if (per_cu->from_debug_types)
15107 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15108 else
15109 type_hash = dwarf2_per_objfile->debug_info_type_hash;
15110 if (type_hash == NULL)
15111 return NULL;
15112
15113 ofs.offset = offset;
15114 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15115 if (slot)
15116 return slot->type;
15117 else
15118 return NULL;
15119 }
15120
15121 /* Look up the type for DIE in the appropriate type_hash table,
15122 or return NULL if DIE does not have a saved type. */
15123
15124 static struct type *
15125 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15126 {
15127 return get_die_type_at_offset (die->offset, cu->per_cu);
15128 }
15129
15130 /* Add a dependence relationship from CU to REF_PER_CU. */
15131
15132 static void
15133 dwarf2_add_dependence (struct dwarf2_cu *cu,
15134 struct dwarf2_per_cu_data *ref_per_cu)
15135 {
15136 void **slot;
15137
15138 if (cu->dependencies == NULL)
15139 cu->dependencies
15140 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15141 NULL, &cu->comp_unit_obstack,
15142 hashtab_obstack_allocate,
15143 dummy_obstack_deallocate);
15144
15145 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15146 if (*slot == NULL)
15147 *slot = ref_per_cu;
15148 }
15149
15150 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15151 Set the mark field in every compilation unit in the
15152 cache that we must keep because we are keeping CU. */
15153
15154 static int
15155 dwarf2_mark_helper (void **slot, void *data)
15156 {
15157 struct dwarf2_per_cu_data *per_cu;
15158
15159 per_cu = (struct dwarf2_per_cu_data *) *slot;
15160 if (per_cu->cu->mark)
15161 return 1;
15162 per_cu->cu->mark = 1;
15163
15164 if (per_cu->cu->dependencies != NULL)
15165 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15166
15167 return 1;
15168 }
15169
15170 /* Set the mark field in CU and in every other compilation unit in the
15171 cache that we must keep because we are keeping CU. */
15172
15173 static void
15174 dwarf2_mark (struct dwarf2_cu *cu)
15175 {
15176 if (cu->mark)
15177 return;
15178 cu->mark = 1;
15179 if (cu->dependencies != NULL)
15180 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15181 }
15182
15183 static void
15184 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15185 {
15186 while (per_cu)
15187 {
15188 per_cu->cu->mark = 0;
15189 per_cu = per_cu->cu->read_in_chain;
15190 }
15191 }
15192
15193 /* Trivial hash function for partial_die_info: the hash value of a DIE
15194 is its offset in .debug_info for this objfile. */
15195
15196 static hashval_t
15197 partial_die_hash (const void *item)
15198 {
15199 const struct partial_die_info *part_die = item;
15200
15201 return part_die->offset;
15202 }
15203
15204 /* Trivial comparison function for partial_die_info structures: two DIEs
15205 are equal if they have the same offset. */
15206
15207 static int
15208 partial_die_eq (const void *item_lhs, const void *item_rhs)
15209 {
15210 const struct partial_die_info *part_die_lhs = item_lhs;
15211 const struct partial_die_info *part_die_rhs = item_rhs;
15212
15213 return part_die_lhs->offset == part_die_rhs->offset;
15214 }
15215
15216 static struct cmd_list_element *set_dwarf2_cmdlist;
15217 static struct cmd_list_element *show_dwarf2_cmdlist;
15218
15219 static void
15220 set_dwarf2_cmd (char *args, int from_tty)
15221 {
15222 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15223 }
15224
15225 static void
15226 show_dwarf2_cmd (char *args, int from_tty)
15227 {
15228 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15229 }
15230
15231 /* If section described by INFO was mmapped, munmap it now. */
15232
15233 static void
15234 munmap_section_buffer (struct dwarf2_section_info *info)
15235 {
15236 if (info->was_mmapped)
15237 {
15238 #ifdef HAVE_MMAP
15239 intptr_t begin = (intptr_t) info->buffer;
15240 intptr_t map_begin = begin & ~(pagesize - 1);
15241 size_t map_length = info->size + begin - map_begin;
15242
15243 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
15244 #else
15245 /* Without HAVE_MMAP, we should never be here to begin with. */
15246 gdb_assert_not_reached ("no mmap support");
15247 #endif
15248 }
15249 }
15250
15251 /* munmap debug sections for OBJFILE, if necessary. */
15252
15253 static void
15254 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15255 {
15256 struct dwarf2_per_objfile *data = d;
15257
15258 /* This is sorted according to the order they're defined in to make it easier
15259 to keep in sync. */
15260 munmap_section_buffer (&data->info);
15261 munmap_section_buffer (&data->abbrev);
15262 munmap_section_buffer (&data->line);
15263 munmap_section_buffer (&data->loc);
15264 munmap_section_buffer (&data->macinfo);
15265 munmap_section_buffer (&data->str);
15266 munmap_section_buffer (&data->ranges);
15267 munmap_section_buffer (&data->types);
15268 munmap_section_buffer (&data->frame);
15269 munmap_section_buffer (&data->eh_frame);
15270 munmap_section_buffer (&data->gdb_index);
15271 }
15272
15273 \f
15274 /* The "save gdb-index" command. */
15275
15276 /* The contents of the hash table we create when building the string
15277 table. */
15278 struct strtab_entry
15279 {
15280 offset_type offset;
15281 const char *str;
15282 };
15283
15284 /* Hash function for a strtab_entry. */
15285
15286 static hashval_t
15287 hash_strtab_entry (const void *e)
15288 {
15289 const struct strtab_entry *entry = e;
15290 return mapped_index_string_hash (entry->str);
15291 }
15292
15293 /* Equality function for a strtab_entry. */
15294
15295 static int
15296 eq_strtab_entry (const void *a, const void *b)
15297 {
15298 const struct strtab_entry *ea = a;
15299 const struct strtab_entry *eb = b;
15300 return !strcmp (ea->str, eb->str);
15301 }
15302
15303 /* Create a strtab_entry hash table. */
15304
15305 static htab_t
15306 create_strtab (void)
15307 {
15308 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15309 xfree, xcalloc, xfree);
15310 }
15311
15312 /* Add a string to the constant pool. Return the string's offset in
15313 host order. */
15314
15315 static offset_type
15316 add_string (htab_t table, struct obstack *cpool, const char *str)
15317 {
15318 void **slot;
15319 struct strtab_entry entry;
15320 struct strtab_entry *result;
15321
15322 entry.str = str;
15323 slot = htab_find_slot (table, &entry, INSERT);
15324 if (*slot)
15325 result = *slot;
15326 else
15327 {
15328 result = XNEW (struct strtab_entry);
15329 result->offset = obstack_object_size (cpool);
15330 result->str = str;
15331 obstack_grow_str0 (cpool, str);
15332 *slot = result;
15333 }
15334 return result->offset;
15335 }
15336
15337 /* An entry in the symbol table. */
15338 struct symtab_index_entry
15339 {
15340 /* The name of the symbol. */
15341 const char *name;
15342 /* The offset of the name in the constant pool. */
15343 offset_type index_offset;
15344 /* A sorted vector of the indices of all the CUs that hold an object
15345 of this name. */
15346 VEC (offset_type) *cu_indices;
15347 };
15348
15349 /* The symbol table. This is a power-of-2-sized hash table. */
15350 struct mapped_symtab
15351 {
15352 offset_type n_elements;
15353 offset_type size;
15354 struct symtab_index_entry **data;
15355 };
15356
15357 /* Hash function for a symtab_index_entry. */
15358
15359 static hashval_t
15360 hash_symtab_entry (const void *e)
15361 {
15362 const struct symtab_index_entry *entry = e;
15363 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15364 sizeof (offset_type) * VEC_length (offset_type,
15365 entry->cu_indices),
15366 0);
15367 }
15368
15369 /* Equality function for a symtab_index_entry. */
15370
15371 static int
15372 eq_symtab_entry (const void *a, const void *b)
15373 {
15374 const struct symtab_index_entry *ea = a;
15375 const struct symtab_index_entry *eb = b;
15376 int len = VEC_length (offset_type, ea->cu_indices);
15377 if (len != VEC_length (offset_type, eb->cu_indices))
15378 return 0;
15379 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15380 VEC_address (offset_type, eb->cu_indices),
15381 sizeof (offset_type) * len);
15382 }
15383
15384 /* Destroy a symtab_index_entry. */
15385
15386 static void
15387 delete_symtab_entry (void *p)
15388 {
15389 struct symtab_index_entry *entry = p;
15390 VEC_free (offset_type, entry->cu_indices);
15391 xfree (entry);
15392 }
15393
15394 /* Create a hash table holding symtab_index_entry objects. */
15395
15396 static htab_t
15397 create_symbol_hash_table (void)
15398 {
15399 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15400 delete_symtab_entry, xcalloc, xfree);
15401 }
15402
15403 /* Create a new mapped symtab object. */
15404
15405 static struct mapped_symtab *
15406 create_mapped_symtab (void)
15407 {
15408 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15409 symtab->n_elements = 0;
15410 symtab->size = 1024;
15411 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15412 return symtab;
15413 }
15414
15415 /* Destroy a mapped_symtab. */
15416
15417 static void
15418 cleanup_mapped_symtab (void *p)
15419 {
15420 struct mapped_symtab *symtab = p;
15421 /* The contents of the array are freed when the other hash table is
15422 destroyed. */
15423 xfree (symtab->data);
15424 xfree (symtab);
15425 }
15426
15427 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15428 the slot. */
15429
15430 static struct symtab_index_entry **
15431 find_slot (struct mapped_symtab *symtab, const char *name)
15432 {
15433 offset_type index, step, hash = mapped_index_string_hash (name);
15434
15435 index = hash & (symtab->size - 1);
15436 step = ((hash * 17) & (symtab->size - 1)) | 1;
15437
15438 for (;;)
15439 {
15440 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15441 return &symtab->data[index];
15442 index = (index + step) & (symtab->size - 1);
15443 }
15444 }
15445
15446 /* Expand SYMTAB's hash table. */
15447
15448 static void
15449 hash_expand (struct mapped_symtab *symtab)
15450 {
15451 offset_type old_size = symtab->size;
15452 offset_type i;
15453 struct symtab_index_entry **old_entries = symtab->data;
15454
15455 symtab->size *= 2;
15456 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15457
15458 for (i = 0; i < old_size; ++i)
15459 {
15460 if (old_entries[i])
15461 {
15462 struct symtab_index_entry **slot = find_slot (symtab,
15463 old_entries[i]->name);
15464 *slot = old_entries[i];
15465 }
15466 }
15467
15468 xfree (old_entries);
15469 }
15470
15471 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15472 is the index of the CU in which the symbol appears. */
15473
15474 static void
15475 add_index_entry (struct mapped_symtab *symtab, const char *name,
15476 offset_type cu_index)
15477 {
15478 struct symtab_index_entry **slot;
15479
15480 ++symtab->n_elements;
15481 if (4 * symtab->n_elements / 3 >= symtab->size)
15482 hash_expand (symtab);
15483
15484 slot = find_slot (symtab, name);
15485 if (!*slot)
15486 {
15487 *slot = XNEW (struct symtab_index_entry);
15488 (*slot)->name = name;
15489 (*slot)->cu_indices = NULL;
15490 }
15491 /* Don't push an index twice. Due to how we add entries we only
15492 have to check the last one. */
15493 if (VEC_empty (offset_type, (*slot)->cu_indices)
15494 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
15495 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15496 }
15497
15498 /* Add a vector of indices to the constant pool. */
15499
15500 static offset_type
15501 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15502 struct symtab_index_entry *entry)
15503 {
15504 void **slot;
15505
15506 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15507 if (!*slot)
15508 {
15509 offset_type len = VEC_length (offset_type, entry->cu_indices);
15510 offset_type val = MAYBE_SWAP (len);
15511 offset_type iter;
15512 int i;
15513
15514 *slot = entry;
15515 entry->index_offset = obstack_object_size (cpool);
15516
15517 obstack_grow (cpool, &val, sizeof (val));
15518 for (i = 0;
15519 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15520 ++i)
15521 {
15522 val = MAYBE_SWAP (iter);
15523 obstack_grow (cpool, &val, sizeof (val));
15524 }
15525 }
15526 else
15527 {
15528 struct symtab_index_entry *old_entry = *slot;
15529 entry->index_offset = old_entry->index_offset;
15530 entry = old_entry;
15531 }
15532 return entry->index_offset;
15533 }
15534
15535 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15536 constant pool entries going into the obstack CPOOL. */
15537
15538 static void
15539 write_hash_table (struct mapped_symtab *symtab,
15540 struct obstack *output, struct obstack *cpool)
15541 {
15542 offset_type i;
15543 htab_t symbol_hash_table;
15544 htab_t str_table;
15545
15546 symbol_hash_table = create_symbol_hash_table ();
15547 str_table = create_strtab ();
15548
15549 /* We add all the index vectors to the constant pool first, to
15550 ensure alignment is ok. */
15551 for (i = 0; i < symtab->size; ++i)
15552 {
15553 if (symtab->data[i])
15554 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15555 }
15556
15557 /* Now write out the hash table. */
15558 for (i = 0; i < symtab->size; ++i)
15559 {
15560 offset_type str_off, vec_off;
15561
15562 if (symtab->data[i])
15563 {
15564 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15565 vec_off = symtab->data[i]->index_offset;
15566 }
15567 else
15568 {
15569 /* While 0 is a valid constant pool index, it is not valid
15570 to have 0 for both offsets. */
15571 str_off = 0;
15572 vec_off = 0;
15573 }
15574
15575 str_off = MAYBE_SWAP (str_off);
15576 vec_off = MAYBE_SWAP (vec_off);
15577
15578 obstack_grow (output, &str_off, sizeof (str_off));
15579 obstack_grow (output, &vec_off, sizeof (vec_off));
15580 }
15581
15582 htab_delete (str_table);
15583 htab_delete (symbol_hash_table);
15584 }
15585
15586 /* Struct to map psymtab to CU index in the index file. */
15587 struct psymtab_cu_index_map
15588 {
15589 struct partial_symtab *psymtab;
15590 unsigned int cu_index;
15591 };
15592
15593 static hashval_t
15594 hash_psymtab_cu_index (const void *item)
15595 {
15596 const struct psymtab_cu_index_map *map = item;
15597
15598 return htab_hash_pointer (map->psymtab);
15599 }
15600
15601 static int
15602 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15603 {
15604 const struct psymtab_cu_index_map *lhs = item_lhs;
15605 const struct psymtab_cu_index_map *rhs = item_rhs;
15606
15607 return lhs->psymtab == rhs->psymtab;
15608 }
15609
15610 /* Helper struct for building the address table. */
15611 struct addrmap_index_data
15612 {
15613 struct objfile *objfile;
15614 struct obstack *addr_obstack;
15615 htab_t cu_index_htab;
15616
15617 /* Non-zero if the previous_* fields are valid.
15618 We can't write an entry until we see the next entry (since it is only then
15619 that we know the end of the entry). */
15620 int previous_valid;
15621 /* Index of the CU in the table of all CUs in the index file. */
15622 unsigned int previous_cu_index;
15623 /* Start address of the CU. */
15624 CORE_ADDR previous_cu_start;
15625 };
15626
15627 /* Write an address entry to OBSTACK. */
15628
15629 static void
15630 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15631 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15632 {
15633 offset_type cu_index_to_write;
15634 char addr[8];
15635 CORE_ADDR baseaddr;
15636
15637 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15638
15639 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15640 obstack_grow (obstack, addr, 8);
15641 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15642 obstack_grow (obstack, addr, 8);
15643 cu_index_to_write = MAYBE_SWAP (cu_index);
15644 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15645 }
15646
15647 /* Worker function for traversing an addrmap to build the address table. */
15648
15649 static int
15650 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15651 {
15652 struct addrmap_index_data *data = datap;
15653 struct partial_symtab *pst = obj;
15654 offset_type cu_index;
15655 void **slot;
15656
15657 if (data->previous_valid)
15658 add_address_entry (data->objfile, data->addr_obstack,
15659 data->previous_cu_start, start_addr,
15660 data->previous_cu_index);
15661
15662 data->previous_cu_start = start_addr;
15663 if (pst != NULL)
15664 {
15665 struct psymtab_cu_index_map find_map, *map;
15666 find_map.psymtab = pst;
15667 map = htab_find (data->cu_index_htab, &find_map);
15668 gdb_assert (map != NULL);
15669 data->previous_cu_index = map->cu_index;
15670 data->previous_valid = 1;
15671 }
15672 else
15673 data->previous_valid = 0;
15674
15675 return 0;
15676 }
15677
15678 /* Write OBJFILE's address map to OBSTACK.
15679 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15680 in the index file. */
15681
15682 static void
15683 write_address_map (struct objfile *objfile, struct obstack *obstack,
15684 htab_t cu_index_htab)
15685 {
15686 struct addrmap_index_data addrmap_index_data;
15687
15688 /* When writing the address table, we have to cope with the fact that
15689 the addrmap iterator only provides the start of a region; we have to
15690 wait until the next invocation to get the start of the next region. */
15691
15692 addrmap_index_data.objfile = objfile;
15693 addrmap_index_data.addr_obstack = obstack;
15694 addrmap_index_data.cu_index_htab = cu_index_htab;
15695 addrmap_index_data.previous_valid = 0;
15696
15697 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15698 &addrmap_index_data);
15699
15700 /* It's highly unlikely the last entry (end address = 0xff...ff)
15701 is valid, but we should still handle it.
15702 The end address is recorded as the start of the next region, but that
15703 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15704 anyway. */
15705 if (addrmap_index_data.previous_valid)
15706 add_address_entry (objfile, obstack,
15707 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15708 addrmap_index_data.previous_cu_index);
15709 }
15710
15711 /* Add a list of partial symbols to SYMTAB. */
15712
15713 static void
15714 write_psymbols (struct mapped_symtab *symtab,
15715 htab_t psyms_seen,
15716 struct partial_symbol **psymp,
15717 int count,
15718 offset_type cu_index,
15719 int is_static)
15720 {
15721 for (; count-- > 0; ++psymp)
15722 {
15723 void **slot, *lookup;
15724
15725 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15726 error (_("Ada is not currently supported by the index"));
15727
15728 /* We only want to add a given psymbol once. However, we also
15729 want to account for whether it is global or static. So, we
15730 may add it twice, using slightly different values. */
15731 if (is_static)
15732 {
15733 uintptr_t val = 1 | (uintptr_t) *psymp;
15734
15735 lookup = (void *) val;
15736 }
15737 else
15738 lookup = *psymp;
15739
15740 /* Only add a given psymbol once. */
15741 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15742 if (!*slot)
15743 {
15744 *slot = lookup;
15745 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15746 }
15747 }
15748 }
15749
15750 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
15751 exception if there is an error. */
15752
15753 static void
15754 write_obstack (FILE *file, struct obstack *obstack)
15755 {
15756 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15757 file)
15758 != obstack_object_size (obstack))
15759 error (_("couldn't data write to file"));
15760 }
15761
15762 /* Unlink a file if the argument is not NULL. */
15763
15764 static void
15765 unlink_if_set (void *p)
15766 {
15767 char **filename = p;
15768 if (*filename)
15769 unlink (*filename);
15770 }
15771
15772 /* A helper struct used when iterating over debug_types. */
15773 struct signatured_type_index_data
15774 {
15775 struct objfile *objfile;
15776 struct mapped_symtab *symtab;
15777 struct obstack *types_list;
15778 htab_t psyms_seen;
15779 int cu_index;
15780 };
15781
15782 /* A helper function that writes a single signatured_type to an
15783 obstack. */
15784
15785 static int
15786 write_one_signatured_type (void **slot, void *d)
15787 {
15788 struct signatured_type_index_data *info = d;
15789 struct signatured_type *entry = (struct signatured_type *) *slot;
15790 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15791 struct partial_symtab *psymtab = per_cu->v.psymtab;
15792 gdb_byte val[8];
15793
15794 write_psymbols (info->symtab,
15795 info->psyms_seen,
15796 info->objfile->global_psymbols.list
15797 + psymtab->globals_offset,
15798 psymtab->n_global_syms, info->cu_index,
15799 0);
15800 write_psymbols (info->symtab,
15801 info->psyms_seen,
15802 info->objfile->static_psymbols.list
15803 + psymtab->statics_offset,
15804 psymtab->n_static_syms, info->cu_index,
15805 1);
15806
15807 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15808 obstack_grow (info->types_list, val, 8);
15809 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15810 obstack_grow (info->types_list, val, 8);
15811 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15812 obstack_grow (info->types_list, val, 8);
15813
15814 ++info->cu_index;
15815
15816 return 1;
15817 }
15818
15819 /* A cleanup function for an htab_t. */
15820
15821 static void
15822 cleanup_htab (void *arg)
15823 {
15824 htab_delete (arg);
15825 }
15826
15827 /* Create an index file for OBJFILE in the directory DIR. */
15828
15829 static void
15830 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15831 {
15832 struct cleanup *cleanup;
15833 char *filename, *cleanup_filename;
15834 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15835 struct obstack cu_list, types_cu_list;
15836 int i;
15837 FILE *out_file;
15838 struct mapped_symtab *symtab;
15839 offset_type val, size_of_contents, total_len;
15840 struct stat st;
15841 char buf[8];
15842 htab_t psyms_seen;
15843 htab_t cu_index_htab;
15844 struct psymtab_cu_index_map *psymtab_cu_index_map;
15845
15846 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
15847 return;
15848
15849 if (dwarf2_per_objfile->using_index)
15850 error (_("Cannot use an index to create the index"));
15851
15852 if (stat (objfile->name, &st) < 0)
15853 perror_with_name (objfile->name);
15854
15855 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15856 INDEX_SUFFIX, (char *) NULL);
15857 cleanup = make_cleanup (xfree, filename);
15858
15859 out_file = fopen (filename, "wb");
15860 if (!out_file)
15861 error (_("Can't open `%s' for writing"), filename);
15862
15863 cleanup_filename = filename;
15864 make_cleanup (unlink_if_set, &cleanup_filename);
15865
15866 symtab = create_mapped_symtab ();
15867 make_cleanup (cleanup_mapped_symtab, symtab);
15868
15869 obstack_init (&addr_obstack);
15870 make_cleanup_obstack_free (&addr_obstack);
15871
15872 obstack_init (&cu_list);
15873 make_cleanup_obstack_free (&cu_list);
15874
15875 obstack_init (&types_cu_list);
15876 make_cleanup_obstack_free (&types_cu_list);
15877
15878 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15879 NULL, xcalloc, xfree);
15880 make_cleanup (cleanup_htab, psyms_seen);
15881
15882 /* While we're scanning CU's create a table that maps a psymtab pointer
15883 (which is what addrmap records) to its index (which is what is recorded
15884 in the index file). This will later be needed to write the address
15885 table. */
15886 cu_index_htab = htab_create_alloc (100,
15887 hash_psymtab_cu_index,
15888 eq_psymtab_cu_index,
15889 NULL, xcalloc, xfree);
15890 make_cleanup (cleanup_htab, cu_index_htab);
15891 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15892 xmalloc (sizeof (struct psymtab_cu_index_map)
15893 * dwarf2_per_objfile->n_comp_units);
15894 make_cleanup (xfree, psymtab_cu_index_map);
15895
15896 /* The CU list is already sorted, so we don't need to do additional
15897 work here. Also, the debug_types entries do not appear in
15898 all_comp_units, but only in their own hash table. */
15899 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15900 {
15901 struct dwarf2_per_cu_data *per_cu
15902 = dwarf2_per_objfile->all_comp_units[i];
15903 struct partial_symtab *psymtab = per_cu->v.psymtab;
15904 gdb_byte val[8];
15905 struct psymtab_cu_index_map *map;
15906 void **slot;
15907
15908 write_psymbols (symtab,
15909 psyms_seen,
15910 objfile->global_psymbols.list + psymtab->globals_offset,
15911 psymtab->n_global_syms, i,
15912 0);
15913 write_psymbols (symtab,
15914 psyms_seen,
15915 objfile->static_psymbols.list + psymtab->statics_offset,
15916 psymtab->n_static_syms, i,
15917 1);
15918
15919 map = &psymtab_cu_index_map[i];
15920 map->psymtab = psymtab;
15921 map->cu_index = i;
15922 slot = htab_find_slot (cu_index_htab, map, INSERT);
15923 gdb_assert (slot != NULL);
15924 gdb_assert (*slot == NULL);
15925 *slot = map;
15926
15927 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15928 obstack_grow (&cu_list, val, 8);
15929 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15930 obstack_grow (&cu_list, val, 8);
15931 }
15932
15933 /* Dump the address map. */
15934 write_address_map (objfile, &addr_obstack, cu_index_htab);
15935
15936 /* Write out the .debug_type entries, if any. */
15937 if (dwarf2_per_objfile->signatured_types)
15938 {
15939 struct signatured_type_index_data sig_data;
15940
15941 sig_data.objfile = objfile;
15942 sig_data.symtab = symtab;
15943 sig_data.types_list = &types_cu_list;
15944 sig_data.psyms_seen = psyms_seen;
15945 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15946 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15947 write_one_signatured_type, &sig_data);
15948 }
15949
15950 obstack_init (&constant_pool);
15951 make_cleanup_obstack_free (&constant_pool);
15952 obstack_init (&symtab_obstack);
15953 make_cleanup_obstack_free (&symtab_obstack);
15954 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15955
15956 obstack_init (&contents);
15957 make_cleanup_obstack_free (&contents);
15958 size_of_contents = 6 * sizeof (offset_type);
15959 total_len = size_of_contents;
15960
15961 /* The version number. */
15962 val = MAYBE_SWAP (4);
15963 obstack_grow (&contents, &val, sizeof (val));
15964
15965 /* The offset of the CU list from the start of the file. */
15966 val = MAYBE_SWAP (total_len);
15967 obstack_grow (&contents, &val, sizeof (val));
15968 total_len += obstack_object_size (&cu_list);
15969
15970 /* The offset of the types CU list from the start of the file. */
15971 val = MAYBE_SWAP (total_len);
15972 obstack_grow (&contents, &val, sizeof (val));
15973 total_len += obstack_object_size (&types_cu_list);
15974
15975 /* The offset of the address table from the start of the file. */
15976 val = MAYBE_SWAP (total_len);
15977 obstack_grow (&contents, &val, sizeof (val));
15978 total_len += obstack_object_size (&addr_obstack);
15979
15980 /* The offset of the symbol table from the start of the file. */
15981 val = MAYBE_SWAP (total_len);
15982 obstack_grow (&contents, &val, sizeof (val));
15983 total_len += obstack_object_size (&symtab_obstack);
15984
15985 /* The offset of the constant pool from the start of the file. */
15986 val = MAYBE_SWAP (total_len);
15987 obstack_grow (&contents, &val, sizeof (val));
15988 total_len += obstack_object_size (&constant_pool);
15989
15990 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15991
15992 write_obstack (out_file, &contents);
15993 write_obstack (out_file, &cu_list);
15994 write_obstack (out_file, &types_cu_list);
15995 write_obstack (out_file, &addr_obstack);
15996 write_obstack (out_file, &symtab_obstack);
15997 write_obstack (out_file, &constant_pool);
15998
15999 fclose (out_file);
16000
16001 /* We want to keep the file, so we set cleanup_filename to NULL
16002 here. See unlink_if_set. */
16003 cleanup_filename = NULL;
16004
16005 do_cleanups (cleanup);
16006 }
16007
16008 /* The mapped index file format is designed to be directly mmap()able
16009 on any architecture. In most cases, a datum is represented using a
16010 little-endian 32-bit integer value, called an offset_type. Big
16011 endian machines must byte-swap the values before using them.
16012 Exceptions to this rule are noted. The data is laid out such that
16013 alignment is always respected.
16014
16015 A mapped index consists of several sections.
16016
16017 1. The file header. This is a sequence of values, of offset_type
16018 unless otherwise noted:
16019
16020 [0] The version number, currently 4. Versions 1, 2 and 3 are
16021 obsolete.
16022 [1] The offset, from the start of the file, of the CU list.
16023 [2] The offset, from the start of the file, of the types CU list.
16024 Note that this section can be empty, in which case this offset will
16025 be equal to the next offset.
16026 [3] The offset, from the start of the file, of the address section.
16027 [4] The offset, from the start of the file, of the symbol table.
16028 [5] The offset, from the start of the file, of the constant pool.
16029
16030 2. The CU list. This is a sequence of pairs of 64-bit
16031 little-endian values, sorted by the CU offset. The first element
16032 in each pair is the offset of a CU in the .debug_info section. The
16033 second element in each pair is the length of that CU. References
16034 to a CU elsewhere in the map are done using a CU index, which is
16035 just the 0-based index into this table. Note that if there are
16036 type CUs, then conceptually CUs and type CUs form a single list for
16037 the purposes of CU indices.
16038
16039 3. The types CU list. This is a sequence of triplets of 64-bit
16040 little-endian values. In a triplet, the first value is the CU
16041 offset, the second value is the type offset in the CU, and the
16042 third value is the type signature. The types CU list is not
16043 sorted.
16044
16045 4. The address section. The address section consists of a sequence
16046 of address entries. Each address entry has three elements.
16047 [0] The low address. This is a 64-bit little-endian value.
16048 [1] The high address. This is a 64-bit little-endian value.
16049 Like DW_AT_high_pc, the value is one byte beyond the end.
16050 [2] The CU index. This is an offset_type value.
16051
16052 5. The symbol table. This is a hash table. The size of the hash
16053 table is always a power of 2. The initial hash and the step are
16054 currently defined by the `find_slot' function.
16055
16056 Each slot in the hash table consists of a pair of offset_type
16057 values. The first value is the offset of the symbol's name in the
16058 constant pool. The second value is the offset of the CU vector in
16059 the constant pool.
16060
16061 If both values are 0, then this slot in the hash table is empty.
16062 This is ok because while 0 is a valid constant pool index, it
16063 cannot be a valid index for both a string and a CU vector.
16064
16065 A string in the constant pool is stored as a \0-terminated string,
16066 as you'd expect.
16067
16068 A CU vector in the constant pool is a sequence of offset_type
16069 values. The first value is the number of CU indices in the vector.
16070 Each subsequent value is the index of a CU in the CU list. This
16071 element in the hash table is used to indicate which CUs define the
16072 symbol.
16073
16074 6. The constant pool. This is simply a bunch of bytes. It is
16075 organized so that alignment is correct: CU vectors are stored
16076 first, followed by strings. */
16077
16078 static void
16079 save_gdb_index_command (char *arg, int from_tty)
16080 {
16081 struct objfile *objfile;
16082
16083 if (!arg || !*arg)
16084 error (_("usage: save gdb-index DIRECTORY"));
16085
16086 ALL_OBJFILES (objfile)
16087 {
16088 struct stat st;
16089
16090 /* If the objfile does not correspond to an actual file, skip it. */
16091 if (stat (objfile->name, &st) < 0)
16092 continue;
16093
16094 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16095 if (dwarf2_per_objfile)
16096 {
16097 volatile struct gdb_exception except;
16098
16099 TRY_CATCH (except, RETURN_MASK_ERROR)
16100 {
16101 write_psymtabs_to_index (objfile, arg);
16102 }
16103 if (except.reason < 0)
16104 exception_fprintf (gdb_stderr, except,
16105 _("Error while writing index for `%s': "),
16106 objfile->name);
16107 }
16108 }
16109 }
16110
16111 \f
16112
16113 int dwarf2_always_disassemble;
16114
16115 static void
16116 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16117 struct cmd_list_element *c, const char *value)
16118 {
16119 fprintf_filtered (file,
16120 _("Whether to always disassemble "
16121 "DWARF expressions is %s.\n"),
16122 value);
16123 }
16124
16125 void _initialize_dwarf2_read (void);
16126
16127 void
16128 _initialize_dwarf2_read (void)
16129 {
16130 struct cmd_list_element *c;
16131
16132 dwarf2_objfile_data_key
16133 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16134
16135 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16136 Set DWARF 2 specific variables.\n\
16137 Configure DWARF 2 variables such as the cache size"),
16138 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16139 0/*allow-unknown*/, &maintenance_set_cmdlist);
16140
16141 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16142 Show DWARF 2 specific variables\n\
16143 Show DWARF 2 variables such as the cache size"),
16144 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16145 0/*allow-unknown*/, &maintenance_show_cmdlist);
16146
16147 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16148 &dwarf2_max_cache_age, _("\
16149 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16150 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16151 A higher limit means that cached compilation units will be stored\n\
16152 in memory longer, and more total memory will be used. Zero disables\n\
16153 caching, which can slow down startup."),
16154 NULL,
16155 show_dwarf2_max_cache_age,
16156 &set_dwarf2_cmdlist,
16157 &show_dwarf2_cmdlist);
16158
16159 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16160 &dwarf2_always_disassemble, _("\
16161 Set whether `info address' always disassembles DWARF expressions."), _("\
16162 Show whether `info address' always disassembles DWARF expressions."), _("\
16163 When enabled, DWARF expressions are always printed in an assembly-like\n\
16164 syntax. When disabled, expressions will be printed in a more\n\
16165 conversational style, when possible."),
16166 NULL,
16167 show_dwarf2_always_disassemble,
16168 &set_dwarf2_cmdlist,
16169 &show_dwarf2_cmdlist);
16170
16171 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16172 Set debugging of the dwarf2 DIE reader."), _("\
16173 Show debugging of the dwarf2 DIE reader."), _("\
16174 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16175 The value is the maximum depth to print."),
16176 NULL,
16177 NULL,
16178 &setdebuglist, &showdebuglist);
16179
16180 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16181 _("\
16182 Save a gdb-index file.\n\
16183 Usage: save gdb-index DIRECTORY"),
16184 &save_cmdlist);
16185 set_cmd_completer (c, filename_completer);
16186 }