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