gdb/
[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
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
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82 typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98 typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in. */
119 static int dwarf2_die_debug = 0;
120
121 static int pagesize;
122
123 /* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127 static int processing_has_namespace_info;
128
129 static const struct objfile_data *dwarf2_objfile_data_key;
130
131 struct dwarf2_section_info
132 {
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
137 /* True if we have tried to read this section. */
138 int readin;
139 };
140
141 /* All offsets in the index are of this type. It must be
142 architecture-independent. */
143 typedef uint32_t offset_type;
144
145 DEF_VEC_I (offset_type);
146
147 /* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149 struct mapped_index
150 {
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157 /* The hash table. */
158 const offset_type *index_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type index_table_slots;
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163 };
164
165 struct dwarf2_per_objfile
166 {
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
174 struct dwarf2_section_info types;
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
177 struct dwarf2_section_info gdb_index;
178
179 /* Back link. */
180 struct objfile *objfile;
181
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
198
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
216
217 /* Table mapping type .debug_info DIE offsets to types.
218 This is NULL if not allocated yet.
219 It (currently) makes sense to allocate debug_types_type_hash lazily.
220 To keep things simple we allocate both lazily. */
221 htab_t debug_info_type_hash;
222
223 /* Table mapping type .debug_types DIE offsets to types.
224 This is NULL if not allocated yet. */
225 htab_t debug_types_type_hash;
226 };
227
228 static struct dwarf2_per_objfile *dwarf2_per_objfile;
229
230 /* names of the debugging sections */
231
232 /* Note that if the debugging section has been compressed, it might
233 have a name like .zdebug_info. */
234
235 #define INFO_SECTION "debug_info"
236 #define ABBREV_SECTION "debug_abbrev"
237 #define LINE_SECTION "debug_line"
238 #define LOC_SECTION "debug_loc"
239 #define MACINFO_SECTION "debug_macinfo"
240 #define STR_SECTION "debug_str"
241 #define RANGES_SECTION "debug_ranges"
242 #define TYPES_SECTION "debug_types"
243 #define FRAME_SECTION "debug_frame"
244 #define EH_FRAME_SECTION "eh_frame"
245 #define GDB_INDEX_SECTION "gdb_index"
246
247 /* local data types */
248
249 /* We hold several abbreviation tables in memory at the same time. */
250 #ifndef ABBREV_HASH_SIZE
251 #define ABBREV_HASH_SIZE 121
252 #endif
253
254 /* The data in a compilation unit header, after target2host
255 translation, looks like this. */
256 struct comp_unit_head
257 {
258 unsigned int length;
259 short version;
260 unsigned char addr_size;
261 unsigned char signed_addr_p;
262 unsigned int abbrev_offset;
263
264 /* Size of file offsets; either 4 or 8. */
265 unsigned int offset_size;
266
267 /* Size of the length field; either 4 or 12. */
268 unsigned int initial_length_size;
269
270 /* Offset to the first byte of this compilation unit header in the
271 .debug_info section, for resolving relative reference dies. */
272 unsigned int offset;
273
274 /* Offset to first die in this cu from the start of the cu.
275 This will be the first byte following the compilation unit header. */
276 unsigned int first_die_offset;
277 };
278
279 /* Type used for delaying computation of method physnames.
280 See comments for compute_delayed_physnames. */
281 struct delayed_method_info
282 {
283 /* The type to which the method is attached, i.e., its parent class. */
284 struct type *type;
285
286 /* The index of the method in the type's function fieldlists. */
287 int fnfield_index;
288
289 /* The index of the method in the fieldlist. */
290 int index;
291
292 /* The name of the DIE. */
293 const char *name;
294
295 /* The DIE associated with this method. */
296 struct die_info *die;
297 };
298
299 typedef struct delayed_method_info delayed_method_info;
300 DEF_VEC_O (delayed_method_info);
301
302 /* Internal state when decoding a particular compilation unit. */
303 struct dwarf2_cu
304 {
305 /* The objfile containing this compilation unit. */
306 struct objfile *objfile;
307
308 /* The header of the compilation unit. */
309 struct comp_unit_head header;
310
311 /* Base address of this compilation unit. */
312 CORE_ADDR base_address;
313
314 /* Non-zero if base_address has been set. */
315 int base_known;
316
317 struct function_range *first_fn, *last_fn, *cached_fn;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
323 const char *producer;
324
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
341
342 /* Hash table holding all the loaded partial DIEs. */
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
361 /* A hash table of die offsets for following references. */
362 htab_t die_hash;
363
364 /* Full DIEs if read in. */
365 struct die_info *dies;
366
367 /* A set of pointers to dwarf2_per_cu_data objects for compilation
368 units referenced by this one. Only set during full symbol processing;
369 partial symbol tables do not have dependencies. */
370 htab_t dependencies;
371
372 /* Header data from the line table, during full symbol processing. */
373 struct line_header *line_header;
374
375 /* A list of methods which need to have physnames computed
376 after all type information has been read. */
377 VEC (delayed_method_info) *method_list;
378
379 /* Mark used when releasing cached dies. */
380 unsigned int mark : 1;
381
382 /* This flag will be set if this compilation unit might include
383 inter-compilation-unit references. */
384 unsigned int has_form_ref_addr : 1;
385
386 /* This flag will be set if this compilation unit includes any
387 DW_TAG_namespace DIEs. If we know that there are explicit
388 DIEs for namespaces, we don't need to try to infer them
389 from mangled names. */
390 unsigned int has_namespace_info : 1;
391 };
392
393 /* When using the index (and thus not using psymtabs), each CU has an
394 object of this type. This is used to hold information needed by
395 the various "quick" methods. */
396 struct dwarf2_per_cu_quick_data
397 {
398 /* The line table. This can be NULL if there was no line table. */
399 struct line_header *lines;
400
401 /* The file names from the line table. */
402 const char **file_names;
403 /* The file names from the line table after being run through
404 gdb_realpath. */
405 const char **full_names;
406
407 /* The corresponding symbol table. This is NULL if symbols for this
408 CU have not yet been read. */
409 struct symtab *symtab;
410
411 /* A temporary mark bit used when iterating over all CUs in
412 expand_symtabs_matching. */
413 unsigned int mark : 1;
414
415 /* True if we've tried to read the line table. */
416 unsigned int read_lines : 1;
417 };
418
419 /* Persistent data held for a compilation unit, even when not
420 processing it. We put a pointer to this structure in the
421 read_symtab_private field of the psymtab. If we encounter
422 inter-compilation-unit references, we also maintain a sorted
423 list of all compilation units. */
424
425 struct dwarf2_per_cu_data
426 {
427 /* The start offset and length of this compilation unit. 2**29-1
428 bytes should suffice to store the length of any compilation unit
429 - if it doesn't, GDB will fall over anyway.
430 NOTE: Unlike comp_unit_head.length, this length includes
431 initial_length_size. */
432 unsigned int offset;
433 unsigned int length : 29;
434
435 /* Flag indicating this compilation unit will be read in before
436 any of the current compilation units are processed. */
437 unsigned int queued : 1;
438
439 /* This flag will be set if we need to load absolutely all DIEs
440 for this compilation unit, instead of just the ones we think
441 are interesting. It gets set if we look for a DIE in the
442 hash table and don't find it. */
443 unsigned int load_all_dies : 1;
444
445 /* Non-zero if this CU is from .debug_types.
446 Otherwise it's from .debug_info. */
447 unsigned int from_debug_types : 1;
448
449 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
450 of the CU cache it gets reset to NULL again. */
451 struct dwarf2_cu *cu;
452
453 /* The corresponding objfile. */
454 struct objfile *objfile;
455
456 /* When using partial symbol tables, the 'psymtab' field is active.
457 Otherwise the 'quick' field is active. */
458 union
459 {
460 /* The partial symbol table associated with this compilation unit,
461 or NULL for partial units (which do not have an associated
462 symtab). */
463 struct partial_symtab *psymtab;
464
465 /* Data needed by the "quick" functions. */
466 struct dwarf2_per_cu_quick_data *quick;
467 } v;
468 };
469
470 /* Entry in the signatured_types hash table. */
471
472 struct signatured_type
473 {
474 ULONGEST signature;
475
476 /* Offset in .debug_types of the TU (type_unit) for this type. */
477 unsigned int offset;
478
479 /* Offset in .debug_types of the type defined by this TU. */
480 unsigned int type_offset;
481
482 /* The CU(/TU) of this type. */
483 struct dwarf2_per_cu_data per_cu;
484 };
485
486 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
487 which are used for both .debug_info and .debug_types dies.
488 All parameters here are unchanging for the life of the call.
489 This struct exists to abstract away the constant parameters of
490 die reading. */
491
492 struct die_reader_specs
493 {
494 /* The bfd of this objfile. */
495 bfd* abfd;
496
497 /* The CU of the DIE we are parsing. */
498 struct dwarf2_cu *cu;
499
500 /* Pointer to start of section buffer.
501 This is either the start of .debug_info or .debug_types. */
502 const gdb_byte *buffer;
503 };
504
505 /* The line number information for a compilation unit (found in the
506 .debug_line section) begins with a "statement program header",
507 which contains the following information. */
508 struct line_header
509 {
510 unsigned int total_length;
511 unsigned short version;
512 unsigned int header_length;
513 unsigned char minimum_instruction_length;
514 unsigned char maximum_ops_per_instruction;
515 unsigned char default_is_stmt;
516 int line_base;
517 unsigned char line_range;
518 unsigned char opcode_base;
519
520 /* standard_opcode_lengths[i] is the number of operands for the
521 standard opcode whose value is i. This means that
522 standard_opcode_lengths[0] is unused, and the last meaningful
523 element is standard_opcode_lengths[opcode_base - 1]. */
524 unsigned char *standard_opcode_lengths;
525
526 /* The include_directories table. NOTE! These strings are not
527 allocated with xmalloc; instead, they are pointers into
528 debug_line_buffer. If you try to free them, `free' will get
529 indigestion. */
530 unsigned int num_include_dirs, include_dirs_size;
531 char **include_dirs;
532
533 /* The file_names table. NOTE! These strings are not allocated
534 with xmalloc; instead, they are pointers into debug_line_buffer.
535 Don't try to free them directly. */
536 unsigned int num_file_names, file_names_size;
537 struct file_entry
538 {
539 char *name;
540 unsigned int dir_index;
541 unsigned int mod_time;
542 unsigned int length;
543 int included_p; /* Non-zero if referenced by the Line Number Program. */
544 struct symtab *symtab; /* The associated symbol table, if any. */
545 } *file_names;
546
547 /* The start and end of the statement program following this
548 header. These point into dwarf2_per_objfile->line_buffer. */
549 gdb_byte *statement_program_start, *statement_program_end;
550 };
551
552 /* When we construct a partial symbol table entry we only
553 need this much information. */
554 struct partial_die_info
555 {
556 /* Offset of this DIE. */
557 unsigned int offset;
558
559 /* DWARF-2 tag for this DIE. */
560 ENUM_BITFIELD(dwarf_tag) tag : 16;
561
562 /* Assorted flags describing the data found in this DIE. */
563 unsigned int has_children : 1;
564 unsigned int is_external : 1;
565 unsigned int is_declaration : 1;
566 unsigned int has_type : 1;
567 unsigned int has_specification : 1;
568 unsigned int has_pc_info : 1;
569
570 /* Flag set if the SCOPE field of this structure has been
571 computed. */
572 unsigned int scope_set : 1;
573
574 /* Flag set if the DIE has a byte_size attribute. */
575 unsigned int has_byte_size : 1;
576
577 /* Flag set if any of the DIE's children are template arguments. */
578 unsigned int has_template_arguments : 1;
579
580 /* The name of this DIE. Normally the value of DW_AT_name, but
581 sometimes a default name for unnamed DIEs. */
582 char *name;
583
584 /* The scope to prepend to our children. This is generally
585 allocated on the comp_unit_obstack, so will disappear
586 when this compilation unit leaves the cache. */
587 char *scope;
588
589 /* The location description associated with this DIE, if any. */
590 struct dwarf_block *locdesc;
591
592 /* If HAS_PC_INFO, the PC range associated with this DIE. */
593 CORE_ADDR lowpc;
594 CORE_ADDR highpc;
595
596 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
597 DW_AT_sibling, if any. */
598 gdb_byte *sibling;
599
600 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
601 DW_AT_specification (or DW_AT_abstract_origin or
602 DW_AT_extension). */
603 unsigned int spec_offset;
604
605 /* Pointers to this DIE's parent, first child, and next sibling,
606 if any. */
607 struct partial_die_info *die_parent, *die_child, *die_sibling;
608 };
609
610 /* This data structure holds the information of an abbrev. */
611 struct abbrev_info
612 {
613 unsigned int number; /* number identifying abbrev */
614 enum dwarf_tag tag; /* dwarf tag */
615 unsigned short has_children; /* boolean */
616 unsigned short num_attrs; /* number of attributes */
617 struct attr_abbrev *attrs; /* an array of attribute descriptions */
618 struct abbrev_info *next; /* next in chain */
619 };
620
621 struct attr_abbrev
622 {
623 ENUM_BITFIELD(dwarf_attribute) name : 16;
624 ENUM_BITFIELD(dwarf_form) form : 16;
625 };
626
627 /* Attributes have a name and a value */
628 struct attribute
629 {
630 ENUM_BITFIELD(dwarf_attribute) name : 16;
631 ENUM_BITFIELD(dwarf_form) form : 15;
632
633 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
634 field should be in u.str (existing only for DW_STRING) but it is kept
635 here for better struct attribute alignment. */
636 unsigned int string_is_canonical : 1;
637
638 union
639 {
640 char *str;
641 struct dwarf_block *blk;
642 ULONGEST unsnd;
643 LONGEST snd;
644 CORE_ADDR addr;
645 struct signatured_type *signatured_type;
646 }
647 u;
648 };
649
650 /* This data structure holds a complete die structure. */
651 struct die_info
652 {
653 /* DWARF-2 tag for this DIE. */
654 ENUM_BITFIELD(dwarf_tag) tag : 16;
655
656 /* Number of attributes */
657 unsigned char num_attrs;
658
659 /* True if we're presently building the full type name for the
660 type derived from this DIE. */
661 unsigned char building_fullname : 1;
662
663 /* Abbrev number */
664 unsigned int abbrev;
665
666 /* Offset in .debug_info or .debug_types section. */
667 unsigned int offset;
668
669 /* The dies in a compilation unit form an n-ary tree. PARENT
670 points to this die's parent; CHILD points to the first child of
671 this node; and all the children of a given node are chained
672 together via their SIBLING fields, terminated by a die whose
673 tag is zero. */
674 struct die_info *child; /* Its first child, if any. */
675 struct die_info *sibling; /* Its next sibling, if any. */
676 struct die_info *parent; /* Its parent, if any. */
677
678 /* An array of attributes, with NUM_ATTRS elements. There may be
679 zero, but it's not common and zero-sized arrays are not
680 sufficiently portable C. */
681 struct attribute attrs[1];
682 };
683
684 struct function_range
685 {
686 const char *name;
687 CORE_ADDR lowpc, highpc;
688 int seen_line;
689 struct function_range *next;
690 };
691
692 /* Get at parts of an attribute structure */
693
694 #define DW_STRING(attr) ((attr)->u.str)
695 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
696 #define DW_UNSND(attr) ((attr)->u.unsnd)
697 #define DW_BLOCK(attr) ((attr)->u.blk)
698 #define DW_SND(attr) ((attr)->u.snd)
699 #define DW_ADDR(attr) ((attr)->u.addr)
700 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
701
702 /* Blocks are a bunch of untyped bytes. */
703 struct dwarf_block
704 {
705 unsigned int size;
706 gdb_byte *data;
707 };
708
709 #ifndef ATTR_ALLOC_CHUNK
710 #define ATTR_ALLOC_CHUNK 4
711 #endif
712
713 /* Allocate fields for structs, unions and enums in this size. */
714 #ifndef DW_FIELD_ALLOC_CHUNK
715 #define DW_FIELD_ALLOC_CHUNK 4
716 #endif
717
718 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
719 but this would require a corresponding change in unpack_field_as_long
720 and friends. */
721 static int bits_per_byte = 8;
722
723 /* The routines that read and process dies for a C struct or C++ class
724 pass lists of data member fields and lists of member function fields
725 in an instance of a field_info structure, as defined below. */
726 struct field_info
727 {
728 /* List of data member and baseclasses fields. */
729 struct nextfield
730 {
731 struct nextfield *next;
732 int accessibility;
733 int virtuality;
734 struct field field;
735 }
736 *fields, *baseclasses;
737
738 /* Number of fields (including baseclasses). */
739 int nfields;
740
741 /* Number of baseclasses. */
742 int nbaseclasses;
743
744 /* Set if the accesibility of one of the fields is not public. */
745 int non_public_fields;
746
747 /* Member function fields array, entries are allocated in the order they
748 are encountered in the object file. */
749 struct nextfnfield
750 {
751 struct nextfnfield *next;
752 struct fn_field fnfield;
753 }
754 *fnfields;
755
756 /* Member function fieldlist array, contains name of possibly overloaded
757 member function, number of overloaded member functions and a pointer
758 to the head of the member function field chain. */
759 struct fnfieldlist
760 {
761 char *name;
762 int length;
763 struct nextfnfield *head;
764 }
765 *fnfieldlists;
766
767 /* Number of entries in the fnfieldlists array. */
768 int nfnfields;
769
770 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
771 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
772 struct typedef_field_list
773 {
774 struct typedef_field field;
775 struct typedef_field_list *next;
776 }
777 *typedef_field_list;
778 unsigned typedef_field_list_count;
779 };
780
781 /* One item on the queue of compilation units to read in full symbols
782 for. */
783 struct dwarf2_queue_item
784 {
785 struct dwarf2_per_cu_data *per_cu;
786 struct dwarf2_queue_item *next;
787 };
788
789 /* The current queue. */
790 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
791
792 /* Loaded secondary compilation units are kept in memory until they
793 have not been referenced for the processing of this many
794 compilation units. Set this to zero to disable caching. Cache
795 sizes of up to at least twenty will improve startup time for
796 typical inter-CU-reference binaries, at an obvious memory cost. */
797 static int dwarf2_max_cache_age = 5;
798 static void
799 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
800 struct cmd_list_element *c, const char *value)
801 {
802 fprintf_filtered (file, _("\
803 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
804 value);
805 }
806
807
808 /* Various complaints about symbol reading that don't abort the process */
809
810 static void
811 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
812 {
813 complaint (&symfile_complaints,
814 _("statement list doesn't fit in .debug_line section"));
815 }
816
817 static void
818 dwarf2_debug_line_missing_file_complaint (void)
819 {
820 complaint (&symfile_complaints,
821 _(".debug_line section has line data without a file"));
822 }
823
824 static void
825 dwarf2_debug_line_missing_end_sequence_complaint (void)
826 {
827 complaint (&symfile_complaints,
828 _(".debug_line section has line program sequence without an end"));
829 }
830
831 static void
832 dwarf2_complex_location_expr_complaint (void)
833 {
834 complaint (&symfile_complaints, _("location expression too complex"));
835 }
836
837 static void
838 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
839 int arg3)
840 {
841 complaint (&symfile_complaints,
842 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
843 arg2, arg3);
844 }
845
846 static void
847 dwarf2_macros_too_long_complaint (void)
848 {
849 complaint (&symfile_complaints,
850 _("macro info runs off end of `.debug_macinfo' section"));
851 }
852
853 static void
854 dwarf2_macro_malformed_definition_complaint (const char *arg1)
855 {
856 complaint (&symfile_complaints,
857 _("macro debug info contains a malformed macro definition:\n`%s'"),
858 arg1);
859 }
860
861 static void
862 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
863 {
864 complaint (&symfile_complaints,
865 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
866 }
867
868 /* local function prototypes */
869
870 static void dwarf2_locate_sections (bfd *, asection *, void *);
871
872 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
873 struct objfile *);
874
875 static void dwarf2_build_psymtabs_hard (struct objfile *);
876
877 static void scan_partial_symbols (struct partial_die_info *,
878 CORE_ADDR *, CORE_ADDR *,
879 int, struct dwarf2_cu *);
880
881 static void add_partial_symbol (struct partial_die_info *,
882 struct dwarf2_cu *);
883
884 static void add_partial_namespace (struct partial_die_info *pdi,
885 CORE_ADDR *lowpc, CORE_ADDR *highpc,
886 int need_pc, struct dwarf2_cu *cu);
887
888 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
889 CORE_ADDR *highpc, int need_pc,
890 struct dwarf2_cu *cu);
891
892 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
893 struct dwarf2_cu *cu);
894
895 static void add_partial_subprogram (struct partial_die_info *pdi,
896 CORE_ADDR *lowpc, CORE_ADDR *highpc,
897 int need_pc, struct dwarf2_cu *cu);
898
899 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
900 gdb_byte *buffer, gdb_byte *info_ptr,
901 bfd *abfd, struct dwarf2_cu *cu);
902
903 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
904
905 static void psymtab_to_symtab_1 (struct partial_symtab *);
906
907 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
908
909 static void dwarf2_free_abbrev_table (void *);
910
911 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
912 struct dwarf2_cu *);
913
914 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
915 struct dwarf2_cu *);
916
917 static struct partial_die_info *load_partial_dies (bfd *,
918 gdb_byte *, gdb_byte *,
919 int, struct dwarf2_cu *);
920
921 static gdb_byte *read_partial_die (struct partial_die_info *,
922 struct abbrev_info *abbrev,
923 unsigned int, bfd *,
924 gdb_byte *, gdb_byte *,
925 struct dwarf2_cu *);
926
927 static struct partial_die_info *find_partial_die (unsigned int,
928 struct dwarf2_cu *);
929
930 static void fixup_partial_die (struct partial_die_info *,
931 struct dwarf2_cu *);
932
933 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
934 bfd *, gdb_byte *, struct dwarf2_cu *);
935
936 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
937 bfd *, gdb_byte *, struct dwarf2_cu *);
938
939 static unsigned int read_1_byte (bfd *, gdb_byte *);
940
941 static int read_1_signed_byte (bfd *, gdb_byte *);
942
943 static unsigned int read_2_bytes (bfd *, gdb_byte *);
944
945 static unsigned int read_4_bytes (bfd *, gdb_byte *);
946
947 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
948
949 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
950 unsigned int *);
951
952 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
953
954 static LONGEST read_checked_initial_length_and_offset
955 (bfd *, gdb_byte *, const struct comp_unit_head *,
956 unsigned int *, unsigned int *);
957
958 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
959 unsigned int *);
960
961 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
962
963 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
964
965 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
966
967 static char *read_indirect_string (bfd *, gdb_byte *,
968 const struct comp_unit_head *,
969 unsigned int *);
970
971 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
972
973 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
974
975 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
976
977 static void set_cu_language (unsigned int, struct dwarf2_cu *);
978
979 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
980 struct dwarf2_cu *);
981
982 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
983 unsigned int,
984 struct dwarf2_cu *);
985
986 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
987 struct dwarf2_cu *cu);
988
989 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
990
991 static struct die_info *die_specification (struct die_info *die,
992 struct dwarf2_cu **);
993
994 static void free_line_header (struct line_header *lh);
995
996 static void add_file_name (struct line_header *, char *, unsigned int,
997 unsigned int, unsigned int);
998
999 static struct line_header *(dwarf_decode_line_header
1000 (unsigned int offset,
1001 bfd *abfd, struct dwarf2_cu *cu));
1002
1003 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
1004 struct dwarf2_cu *, struct partial_symtab *);
1005
1006 static void dwarf2_start_subfile (char *, char *, char *);
1007
1008 static struct symbol *new_symbol (struct die_info *, struct type *,
1009 struct dwarf2_cu *);
1010
1011 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1012 struct dwarf2_cu *, struct symbol *);
1013
1014 static void dwarf2_const_value (struct attribute *, struct symbol *,
1015 struct dwarf2_cu *);
1016
1017 static void dwarf2_const_value_attr (struct attribute *attr,
1018 struct type *type,
1019 const char *name,
1020 struct obstack *obstack,
1021 struct dwarf2_cu *cu, long *value,
1022 gdb_byte **bytes,
1023 struct dwarf2_locexpr_baton **baton);
1024
1025 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1026
1027 static int need_gnat_info (struct dwarf2_cu *);
1028
1029 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1030
1031 static void set_descriptive_type (struct type *, struct die_info *,
1032 struct dwarf2_cu *);
1033
1034 static struct type *die_containing_type (struct die_info *,
1035 struct dwarf2_cu *);
1036
1037 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1038 struct dwarf2_cu *);
1039
1040 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1041
1042 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1043
1044 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1045
1046 static char *typename_concat (struct obstack *obs, const char *prefix,
1047 const char *suffix, int physname,
1048 struct dwarf2_cu *cu);
1049
1050 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1051
1052 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1053
1054 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1055
1056 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1057
1058 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1059 struct dwarf2_cu *, struct partial_symtab *);
1060
1061 static int dwarf2_get_pc_bounds (struct die_info *,
1062 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1063 struct partial_symtab *);
1064
1065 static void get_scope_pc_bounds (struct die_info *,
1066 CORE_ADDR *, CORE_ADDR *,
1067 struct dwarf2_cu *);
1068
1069 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1070 CORE_ADDR, struct dwarf2_cu *);
1071
1072 static void dwarf2_add_field (struct field_info *, struct die_info *,
1073 struct dwarf2_cu *);
1074
1075 static void dwarf2_attach_fields_to_type (struct field_info *,
1076 struct type *, struct dwarf2_cu *);
1077
1078 static void dwarf2_add_member_fn (struct field_info *,
1079 struct die_info *, struct type *,
1080 struct dwarf2_cu *);
1081
1082 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1083 struct type *, struct dwarf2_cu *);
1084
1085 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1086
1087 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1088
1089 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1090
1091 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1092
1093 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1094
1095 static struct type *read_module_type (struct die_info *die,
1096 struct dwarf2_cu *cu);
1097
1098 static const char *namespace_name (struct die_info *die,
1099 int *is_anonymous, struct dwarf2_cu *);
1100
1101 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1102
1103 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1104
1105 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1106 struct dwarf2_cu *);
1107
1108 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1109
1110 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1111 gdb_byte *info_ptr,
1112 gdb_byte **new_info_ptr,
1113 struct die_info *parent);
1114
1115 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1116 gdb_byte *info_ptr,
1117 gdb_byte **new_info_ptr,
1118 struct die_info *parent);
1119
1120 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1121 gdb_byte *info_ptr,
1122 gdb_byte **new_info_ptr,
1123 struct die_info *parent);
1124
1125 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1126 struct die_info **, gdb_byte *,
1127 int *);
1128
1129 static void process_die (struct die_info *, struct dwarf2_cu *);
1130
1131 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1132 struct obstack *);
1133
1134 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1135
1136 static const char *dwarf2_full_name (char *name,
1137 struct die_info *die,
1138 struct dwarf2_cu *cu);
1139
1140 static struct die_info *dwarf2_extension (struct die_info *die,
1141 struct dwarf2_cu **);
1142
1143 static char *dwarf_tag_name (unsigned int);
1144
1145 static char *dwarf_attr_name (unsigned int);
1146
1147 static char *dwarf_form_name (unsigned int);
1148
1149 static char *dwarf_bool_name (unsigned int);
1150
1151 static char *dwarf_type_encoding_name (unsigned int);
1152
1153 #if 0
1154 static char *dwarf_cfi_name (unsigned int);
1155 #endif
1156
1157 static struct die_info *sibling_die (struct die_info *);
1158
1159 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1160
1161 static void dump_die_for_error (struct die_info *);
1162
1163 static void dump_die_1 (struct ui_file *, int level, int max_level,
1164 struct die_info *);
1165
1166 /*static*/ void dump_die (struct die_info *, int max_level);
1167
1168 static void store_in_ref_table (struct die_info *,
1169 struct dwarf2_cu *);
1170
1171 static int is_ref_attr (struct attribute *);
1172
1173 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1174
1175 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1176
1177 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1178 struct attribute *,
1179 struct dwarf2_cu **);
1180
1181 static struct die_info *follow_die_ref (struct die_info *,
1182 struct attribute *,
1183 struct dwarf2_cu **);
1184
1185 static struct die_info *follow_die_sig (struct die_info *,
1186 struct attribute *,
1187 struct dwarf2_cu **);
1188
1189 static void read_signatured_type_at_offset (struct objfile *objfile,
1190 unsigned int offset);
1191
1192 static void read_signatured_type (struct objfile *,
1193 struct signatured_type *type_sig);
1194
1195 /* memory allocation interface */
1196
1197 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1198
1199 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1200
1201 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1202
1203 static void initialize_cu_func_list (struct dwarf2_cu *);
1204
1205 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1206 struct dwarf2_cu *);
1207
1208 static void dwarf_decode_macros (struct line_header *, unsigned int,
1209 char *, bfd *, struct dwarf2_cu *);
1210
1211 static int attr_form_is_block (struct attribute *);
1212
1213 static int attr_form_is_section_offset (struct attribute *);
1214
1215 static int attr_form_is_constant (struct attribute *);
1216
1217 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1218 struct symbol *sym,
1219 struct dwarf2_cu *cu);
1220
1221 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1222 struct abbrev_info *abbrev,
1223 struct dwarf2_cu *cu);
1224
1225 static void free_stack_comp_unit (void *);
1226
1227 static hashval_t partial_die_hash (const void *item);
1228
1229 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1230
1231 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1232 (unsigned int offset, struct objfile *objfile);
1233
1234 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1235 (unsigned int offset, struct objfile *objfile);
1236
1237 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1238
1239 static void free_one_comp_unit (void *);
1240
1241 static void free_cached_comp_units (void *);
1242
1243 static void age_cached_comp_units (void);
1244
1245 static void free_one_cached_comp_unit (void *);
1246
1247 static struct type *set_die_type (struct die_info *, struct type *,
1248 struct dwarf2_cu *);
1249
1250 static void create_all_comp_units (struct objfile *);
1251
1252 static int create_debug_types_hash_table (struct objfile *objfile);
1253
1254 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1255 struct objfile *);
1256
1257 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1258
1259 static void dwarf2_add_dependence (struct dwarf2_cu *,
1260 struct dwarf2_per_cu_data *);
1261
1262 static void dwarf2_mark (struct dwarf2_cu *);
1263
1264 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1265
1266 static struct type *get_die_type_at_offset (unsigned int,
1267 struct dwarf2_per_cu_data *per_cu);
1268
1269 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1270
1271 static void dwarf2_release_queue (void *dummy);
1272
1273 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1274 struct objfile *objfile);
1275
1276 static void process_queue (struct objfile *objfile);
1277
1278 static void find_file_and_directory (struct die_info *die,
1279 struct dwarf2_cu *cu,
1280 char **name, char **comp_dir);
1281
1282 static char *file_full_name (int file, struct line_header *lh,
1283 const char *comp_dir);
1284
1285 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1286 gdb_byte *info_ptr,
1287 gdb_byte *buffer,
1288 unsigned int buffer_size,
1289 bfd *abfd);
1290
1291 static void init_cu_die_reader (struct die_reader_specs *reader,
1292 struct dwarf2_cu *cu);
1293
1294 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1295
1296 #if WORDS_BIGENDIAN
1297
1298 /* Convert VALUE between big- and little-endian. */
1299 static offset_type
1300 byte_swap (offset_type value)
1301 {
1302 offset_type result;
1303
1304 result = (value & 0xff) << 24;
1305 result |= (value & 0xff00) << 8;
1306 result |= (value & 0xff0000) >> 8;
1307 result |= (value & 0xff000000) >> 24;
1308 return result;
1309 }
1310
1311 #define MAYBE_SWAP(V) byte_swap (V)
1312
1313 #else
1314 #define MAYBE_SWAP(V) (V)
1315 #endif /* WORDS_BIGENDIAN */
1316
1317 /* The suffix for an index file. */
1318 #define INDEX_SUFFIX ".gdb-index"
1319
1320 static const char *dwarf2_physname (char *name, struct die_info *die,
1321 struct dwarf2_cu *cu);
1322
1323 /* Try to locate the sections we need for DWARF 2 debugging
1324 information and return true if we have enough to do something. */
1325
1326 int
1327 dwarf2_has_info (struct objfile *objfile)
1328 {
1329 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1330 if (!dwarf2_per_objfile)
1331 {
1332 /* Initialize per-objfile state. */
1333 struct dwarf2_per_objfile *data
1334 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1335
1336 memset (data, 0, sizeof (*data));
1337 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1338 dwarf2_per_objfile = data;
1339
1340 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1341 dwarf2_per_objfile->objfile = objfile;
1342 }
1343 return (dwarf2_per_objfile->info.asection != NULL
1344 && dwarf2_per_objfile->abbrev.asection != NULL);
1345 }
1346
1347 /* When loading sections, we can either look for ".<name>", or for
1348 * ".z<name>", which indicates a compressed section. */
1349
1350 static int
1351 section_is_p (const char *section_name, const char *name)
1352 {
1353 return (section_name[0] == '.'
1354 && (strcmp (section_name + 1, name) == 0
1355 || (section_name[1] == 'z'
1356 && strcmp (section_name + 2, name) == 0)));
1357 }
1358
1359 /* This function is mapped across the sections and remembers the
1360 offset and size of each of the debugging sections we are interested
1361 in. */
1362
1363 static void
1364 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1365 {
1366 if (section_is_p (sectp->name, INFO_SECTION))
1367 {
1368 dwarf2_per_objfile->info.asection = sectp;
1369 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1370 }
1371 else if (section_is_p (sectp->name, ABBREV_SECTION))
1372 {
1373 dwarf2_per_objfile->abbrev.asection = sectp;
1374 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1375 }
1376 else if (section_is_p (sectp->name, LINE_SECTION))
1377 {
1378 dwarf2_per_objfile->line.asection = sectp;
1379 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1380 }
1381 else if (section_is_p (sectp->name, LOC_SECTION))
1382 {
1383 dwarf2_per_objfile->loc.asection = sectp;
1384 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1385 }
1386 else if (section_is_p (sectp->name, MACINFO_SECTION))
1387 {
1388 dwarf2_per_objfile->macinfo.asection = sectp;
1389 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1390 }
1391 else if (section_is_p (sectp->name, STR_SECTION))
1392 {
1393 dwarf2_per_objfile->str.asection = sectp;
1394 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1395 }
1396 else if (section_is_p (sectp->name, FRAME_SECTION))
1397 {
1398 dwarf2_per_objfile->frame.asection = sectp;
1399 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1400 }
1401 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1402 {
1403 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1404
1405 if (aflag & SEC_HAS_CONTENTS)
1406 {
1407 dwarf2_per_objfile->eh_frame.asection = sectp;
1408 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1409 }
1410 }
1411 else if (section_is_p (sectp->name, RANGES_SECTION))
1412 {
1413 dwarf2_per_objfile->ranges.asection = sectp;
1414 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1415 }
1416 else if (section_is_p (sectp->name, TYPES_SECTION))
1417 {
1418 dwarf2_per_objfile->types.asection = sectp;
1419 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1420 }
1421 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1422 {
1423 dwarf2_per_objfile->gdb_index.asection = sectp;
1424 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1425 }
1426
1427 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1428 && bfd_section_vma (abfd, sectp) == 0)
1429 dwarf2_per_objfile->has_section_at_zero = 1;
1430 }
1431
1432 /* Decompress a section that was compressed using zlib. Store the
1433 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1434
1435 static void
1436 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1437 gdb_byte **outbuf, bfd_size_type *outsize)
1438 {
1439 bfd *abfd = objfile->obfd;
1440 #ifndef HAVE_ZLIB_H
1441 error (_("Support for zlib-compressed DWARF data (from '%s') "
1442 "is disabled in this copy of GDB"),
1443 bfd_get_filename (abfd));
1444 #else
1445 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1446 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1447 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1448 bfd_size_type uncompressed_size;
1449 gdb_byte *uncompressed_buffer;
1450 z_stream strm;
1451 int rc;
1452 int header_size = 12;
1453
1454 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1455 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1456 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1457 bfd_get_filename (abfd));
1458
1459 /* Read the zlib header. In this case, it should be "ZLIB" followed
1460 by the uncompressed section size, 8 bytes in big-endian order. */
1461 if (compressed_size < header_size
1462 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1463 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1464 bfd_get_filename (abfd));
1465 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1466 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1467 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1472 uncompressed_size += compressed_buffer[11];
1473
1474 /* It is possible the section consists of several compressed
1475 buffers concatenated together, so we uncompress in a loop. */
1476 strm.zalloc = NULL;
1477 strm.zfree = NULL;
1478 strm.opaque = NULL;
1479 strm.avail_in = compressed_size - header_size;
1480 strm.next_in = (Bytef*) compressed_buffer + header_size;
1481 strm.avail_out = uncompressed_size;
1482 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1483 uncompressed_size);
1484 rc = inflateInit (&strm);
1485 while (strm.avail_in > 0)
1486 {
1487 if (rc != Z_OK)
1488 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1489 bfd_get_filename (abfd), rc);
1490 strm.next_out = ((Bytef*) uncompressed_buffer
1491 + (uncompressed_size - strm.avail_out));
1492 rc = inflate (&strm, Z_FINISH);
1493 if (rc != Z_STREAM_END)
1494 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1495 bfd_get_filename (abfd), rc);
1496 rc = inflateReset (&strm);
1497 }
1498 rc = inflateEnd (&strm);
1499 if (rc != Z_OK
1500 || strm.avail_out != 0)
1501 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1502 bfd_get_filename (abfd), rc);
1503
1504 do_cleanups (cleanup);
1505 *outbuf = uncompressed_buffer;
1506 *outsize = uncompressed_size;
1507 #endif
1508 }
1509
1510 /* Read the contents of the section SECTP from object file specified by
1511 OBJFILE, store info about the section into INFO.
1512 If the section is compressed, uncompress it before returning. */
1513
1514 static void
1515 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1516 {
1517 bfd *abfd = objfile->obfd;
1518 asection *sectp = info->asection;
1519 gdb_byte *buf, *retbuf;
1520 unsigned char header[4];
1521
1522 if (info->readin)
1523 return;
1524 info->buffer = NULL;
1525 info->was_mmapped = 0;
1526 info->readin = 1;
1527
1528 if (info->asection == NULL || info->size == 0)
1529 return;
1530
1531 /* Check if the file has a 4-byte header indicating compression. */
1532 if (info->size > sizeof (header)
1533 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1534 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1535 {
1536 /* Upon decompression, update the buffer and its size. */
1537 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1538 {
1539 zlib_decompress_section (objfile, sectp, &info->buffer,
1540 &info->size);
1541 return;
1542 }
1543 }
1544
1545 #ifdef HAVE_MMAP
1546 if (pagesize == 0)
1547 pagesize = getpagesize ();
1548
1549 /* Only try to mmap sections which are large enough: we don't want to
1550 waste space due to fragmentation. Also, only try mmap for sections
1551 without relocations. */
1552
1553 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1554 {
1555 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1556 size_t map_length = info->size + sectp->filepos - pg_offset;
1557 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1558 MAP_PRIVATE, pg_offset);
1559
1560 if (retbuf != MAP_FAILED)
1561 {
1562 info->was_mmapped = 1;
1563 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1564 #if HAVE_POSIX_MADVISE
1565 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1566 #endif
1567 return;
1568 }
1569 }
1570 #endif
1571
1572 /* If we get here, we are a normal, not-compressed section. */
1573 info->buffer = buf
1574 = obstack_alloc (&objfile->objfile_obstack, info->size);
1575
1576 /* When debugging .o files, we may need to apply relocations; see
1577 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1578 We never compress sections in .o files, so we only need to
1579 try this when the section is not compressed. */
1580 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1581 if (retbuf != NULL)
1582 {
1583 info->buffer = retbuf;
1584 return;
1585 }
1586
1587 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1588 || bfd_bread (buf, info->size, abfd) != info->size)
1589 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1590 bfd_get_filename (abfd));
1591 }
1592
1593 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1594 SECTION_NAME. */
1595
1596 void
1597 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1598 asection **sectp, gdb_byte **bufp,
1599 bfd_size_type *sizep)
1600 {
1601 struct dwarf2_per_objfile *data
1602 = objfile_data (objfile, dwarf2_objfile_data_key);
1603 struct dwarf2_section_info *info;
1604
1605 /* We may see an objfile without any DWARF, in which case we just
1606 return nothing. */
1607 if (data == NULL)
1608 {
1609 *sectp = NULL;
1610 *bufp = NULL;
1611 *sizep = 0;
1612 return;
1613 }
1614 if (section_is_p (section_name, EH_FRAME_SECTION))
1615 info = &data->eh_frame;
1616 else if (section_is_p (section_name, FRAME_SECTION))
1617 info = &data->frame;
1618 else
1619 gdb_assert_not_reached ("unexpected section");
1620
1621 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1622 /* We haven't read this section in yet. Do it now. */
1623 dwarf2_read_section (objfile, info);
1624
1625 *sectp = info->asection;
1626 *bufp = info->buffer;
1627 *sizep = info->size;
1628 }
1629
1630 \f
1631
1632 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1633 this CU came. */
1634 static void
1635 dw2_do_instantiate_symtab (struct objfile *objfile,
1636 struct dwarf2_per_cu_data *per_cu)
1637 {
1638 struct cleanup *back_to;
1639
1640 back_to = make_cleanup (dwarf2_release_queue, NULL);
1641
1642 queue_comp_unit (per_cu, objfile);
1643
1644 if (per_cu->from_debug_types)
1645 read_signatured_type_at_offset (objfile, per_cu->offset);
1646 else
1647 load_full_comp_unit (per_cu, objfile);
1648
1649 process_queue (objfile);
1650
1651 /* Age the cache, releasing compilation units that have not
1652 been used recently. */
1653 age_cached_comp_units ();
1654
1655 do_cleanups (back_to);
1656 }
1657
1658 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1659 the objfile from which this CU came. Returns the resulting symbol
1660 table. */
1661 static struct symtab *
1662 dw2_instantiate_symtab (struct objfile *objfile,
1663 struct dwarf2_per_cu_data *per_cu)
1664 {
1665 if (!per_cu->v.quick->symtab)
1666 {
1667 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1668 increment_reading_symtab ();
1669 dw2_do_instantiate_symtab (objfile, per_cu);
1670 do_cleanups (back_to);
1671 }
1672 return per_cu->v.quick->symtab;
1673 }
1674
1675 /* Return the CU given its index. */
1676 static struct dwarf2_per_cu_data *
1677 dw2_get_cu (int index)
1678 {
1679 if (index >= dwarf2_per_objfile->n_comp_units)
1680 {
1681 index -= dwarf2_per_objfile->n_comp_units;
1682 return dwarf2_per_objfile->type_comp_units[index];
1683 }
1684 return dwarf2_per_objfile->all_comp_units[index];
1685 }
1686
1687 /* A helper function that knows how to read a 64-bit value in a way
1688 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1689 otherwise. */
1690 static int
1691 extract_cu_value (const char *bytes, ULONGEST *result)
1692 {
1693 if (sizeof (ULONGEST) < 8)
1694 {
1695 int i;
1696
1697 /* Ignore the upper 4 bytes if they are all zero. */
1698 for (i = 0; i < 4; ++i)
1699 if (bytes[i + 4] != 0)
1700 return 0;
1701
1702 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1703 }
1704 else
1705 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1706 return 1;
1707 }
1708
1709 /* Read the CU list from the mapped index, and use it to create all
1710 the CU objects for this objfile. Return 0 if something went wrong,
1711 1 if everything went ok. */
1712 static int
1713 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1714 offset_type cu_list_elements)
1715 {
1716 offset_type i;
1717
1718 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1719 dwarf2_per_objfile->all_comp_units
1720 = obstack_alloc (&objfile->objfile_obstack,
1721 dwarf2_per_objfile->n_comp_units
1722 * sizeof (struct dwarf2_per_cu_data *));
1723
1724 for (i = 0; i < cu_list_elements; i += 2)
1725 {
1726 struct dwarf2_per_cu_data *the_cu;
1727 ULONGEST offset, length;
1728
1729 if (!extract_cu_value (cu_list, &offset)
1730 || !extract_cu_value (cu_list + 8, &length))
1731 return 0;
1732 cu_list += 2 * 8;
1733
1734 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1735 struct dwarf2_per_cu_data);
1736 the_cu->offset = offset;
1737 the_cu->length = length;
1738 the_cu->objfile = objfile;
1739 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1740 struct dwarf2_per_cu_quick_data);
1741 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1742 }
1743
1744 return 1;
1745 }
1746
1747 /* Create the signatured type hash table from the index. */
1748
1749 static int
1750 create_signatured_type_table_from_index (struct objfile *objfile,
1751 const gdb_byte *bytes,
1752 offset_type elements)
1753 {
1754 offset_type i;
1755 htab_t sig_types_hash;
1756
1757 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1758 dwarf2_per_objfile->type_comp_units
1759 = obstack_alloc (&objfile->objfile_obstack,
1760 dwarf2_per_objfile->n_type_comp_units
1761 * sizeof (struct dwarf2_per_cu_data *));
1762
1763 sig_types_hash = allocate_signatured_type_table (objfile);
1764
1765 for (i = 0; i < elements; i += 3)
1766 {
1767 struct signatured_type *type_sig;
1768 ULONGEST offset, type_offset, signature;
1769 void **slot;
1770
1771 if (!extract_cu_value (bytes, &offset)
1772 || !extract_cu_value (bytes + 8, &type_offset))
1773 return 0;
1774 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1775 bytes += 3 * 8;
1776
1777 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1778 struct signatured_type);
1779 type_sig->signature = signature;
1780 type_sig->offset = offset;
1781 type_sig->type_offset = type_offset;
1782 type_sig->per_cu.from_debug_types = 1;
1783 type_sig->per_cu.offset = offset;
1784 type_sig->per_cu.objfile = objfile;
1785 type_sig->per_cu.v.quick
1786 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1787 struct dwarf2_per_cu_quick_data);
1788
1789 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1790 *slot = type_sig;
1791
1792 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1793 }
1794
1795 dwarf2_per_objfile->signatured_types = sig_types_hash;
1796
1797 return 1;
1798 }
1799
1800 /* Read the address map data from the mapped index, and use it to
1801 populate the objfile's psymtabs_addrmap. */
1802 static void
1803 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1804 {
1805 const gdb_byte *iter, *end;
1806 struct obstack temp_obstack;
1807 struct addrmap *mutable_map;
1808 struct cleanup *cleanup;
1809 CORE_ADDR baseaddr;
1810
1811 obstack_init (&temp_obstack);
1812 cleanup = make_cleanup_obstack_free (&temp_obstack);
1813 mutable_map = addrmap_create_mutable (&temp_obstack);
1814
1815 iter = index->address_table;
1816 end = iter + index->address_table_size;
1817
1818 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1819
1820 while (iter < end)
1821 {
1822 ULONGEST hi, lo, cu_index;
1823 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1824 iter += 8;
1825 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1826 iter += 8;
1827 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1828 iter += 4;
1829
1830 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1831 dw2_get_cu (cu_index));
1832 }
1833
1834 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1835 &objfile->objfile_obstack);
1836 do_cleanups (cleanup);
1837 }
1838
1839 /* The hash function for strings in the mapped index. This is the
1840 same as the hashtab.c hash function, but we keep a separate copy to
1841 maintain control over the implementation. This is necessary
1842 because the hash function is tied to the format of the mapped index
1843 file. */
1844 static hashval_t
1845 mapped_index_string_hash (const void *p)
1846 {
1847 const unsigned char *str = (const unsigned char *) p;
1848 hashval_t r = 0;
1849 unsigned char c;
1850
1851 while ((c = *str++) != 0)
1852 r = r * 67 + c - 113;
1853
1854 return r;
1855 }
1856
1857 /* Find a slot in the mapped index INDEX for the object named NAME.
1858 If NAME is found, set *VEC_OUT to point to the CU vector in the
1859 constant pool and return 1. If NAME cannot be found, return 0. */
1860 static int
1861 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1862 offset_type **vec_out)
1863 {
1864 offset_type hash = mapped_index_string_hash (name);
1865 offset_type slot, step;
1866
1867 slot = hash & (index->index_table_slots - 1);
1868 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1869
1870 for (;;)
1871 {
1872 /* Convert a slot number to an offset into the table. */
1873 offset_type i = 2 * slot;
1874 const char *str;
1875 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1876 return 0;
1877
1878 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1879 if (!strcmp (name, str))
1880 {
1881 *vec_out = (offset_type *) (index->constant_pool
1882 + MAYBE_SWAP (index->index_table[i + 1]));
1883 return 1;
1884 }
1885
1886 slot = (slot + step) & (index->index_table_slots - 1);
1887 }
1888 }
1889
1890 /* Read the index file. If everything went ok, initialize the "quick"
1891 elements of all the CUs and return 1. Otherwise, return 0. */
1892 static int
1893 dwarf2_read_index (struct objfile *objfile)
1894 {
1895 char *addr;
1896 struct mapped_index *map;
1897 offset_type *metadata;
1898 const gdb_byte *cu_list;
1899 const gdb_byte *types_list = NULL;
1900 offset_type version, cu_list_elements;
1901 offset_type types_list_elements = 0;
1902 int i;
1903
1904 if (dwarf2_per_objfile->gdb_index.asection == NULL
1905 || dwarf2_per_objfile->gdb_index.size == 0)
1906 return 0;
1907
1908 /* Older elfutils strip versions could keep the section in the main
1909 executable while splitting it for the separate debug info file. */
1910 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1911 & SEC_HAS_CONTENTS) == 0)
1912 return 0;
1913
1914 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1915
1916 addr = dwarf2_per_objfile->gdb_index.buffer;
1917 /* Version check. */
1918 version = MAYBE_SWAP (*(offset_type *) addr);
1919 if (version == 1)
1920 {
1921 /* Index version 1 neglected to account for .debug_types. So,
1922 if we see .debug_types, we cannot use this index. */
1923 if (dwarf2_per_objfile->types.asection != NULL
1924 && dwarf2_per_objfile->types.size != 0)
1925 return 0;
1926 }
1927 else if (version != 2)
1928 return 0;
1929
1930 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1931 map->total_size = dwarf2_per_objfile->gdb_index.size;
1932
1933 metadata = (offset_type *) (addr + sizeof (offset_type));
1934
1935 i = 0;
1936 cu_list = addr + MAYBE_SWAP (metadata[i]);
1937 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1938 / 8);
1939 ++i;
1940
1941 if (version == 2)
1942 {
1943 types_list = addr + MAYBE_SWAP (metadata[i]);
1944 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1945 - MAYBE_SWAP (metadata[i]))
1946 / 8);
1947 ++i;
1948 }
1949
1950 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1951 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1952 - MAYBE_SWAP (metadata[i]));
1953 ++i;
1954
1955 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1956 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1957 - MAYBE_SWAP (metadata[i]))
1958 / (2 * sizeof (offset_type)));
1959 ++i;
1960
1961 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1962
1963 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1964 return 0;
1965
1966 if (version == 2
1967 && types_list_elements
1968 && !create_signatured_type_table_from_index (objfile, types_list,
1969 types_list_elements))
1970 return 0;
1971
1972 create_addrmap_from_index (objfile, map);
1973
1974 dwarf2_per_objfile->index_table = map;
1975 dwarf2_per_objfile->using_index = 1;
1976
1977 return 1;
1978 }
1979
1980 /* A helper for the "quick" functions which sets the global
1981 dwarf2_per_objfile according to OBJFILE. */
1982 static void
1983 dw2_setup (struct objfile *objfile)
1984 {
1985 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1986 gdb_assert (dwarf2_per_objfile);
1987 }
1988
1989 /* A helper for the "quick" functions which attempts to read the line
1990 table for THIS_CU. */
1991 static void
1992 dw2_require_line_header (struct objfile *objfile,
1993 struct dwarf2_per_cu_data *this_cu)
1994 {
1995 bfd *abfd = objfile->obfd;
1996 struct line_header *lh = NULL;
1997 struct attribute *attr;
1998 struct cleanup *cleanups;
1999 struct die_info *comp_unit_die;
2000 struct dwarf2_section_info* sec;
2001 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2002 int has_children, i;
2003 struct dwarf2_cu cu;
2004 unsigned int bytes_read, buffer_size;
2005 struct die_reader_specs reader_specs;
2006 char *name, *comp_dir;
2007
2008 if (this_cu->v.quick->read_lines)
2009 return;
2010 this_cu->v.quick->read_lines = 1;
2011
2012 memset (&cu, 0, sizeof (cu));
2013 cu.objfile = objfile;
2014 obstack_init (&cu.comp_unit_obstack);
2015
2016 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2017
2018 if (this_cu->from_debug_types)
2019 sec = &dwarf2_per_objfile->types;
2020 else
2021 sec = &dwarf2_per_objfile->info;
2022 dwarf2_read_section (objfile, sec);
2023 buffer_size = sec->size;
2024 buffer = sec->buffer;
2025 info_ptr = buffer + this_cu->offset;
2026 beg_of_comp_unit = info_ptr;
2027
2028 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2029 buffer, buffer_size,
2030 abfd);
2031
2032 /* Complete the cu_header. */
2033 cu.header.offset = beg_of_comp_unit - buffer;
2034 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2035
2036 this_cu->cu = &cu;
2037 cu.per_cu = this_cu;
2038
2039 dwarf2_read_abbrevs (abfd, &cu);
2040 make_cleanup (dwarf2_free_abbrev_table, &cu);
2041
2042 if (this_cu->from_debug_types)
2043 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2044 init_cu_die_reader (&reader_specs, &cu);
2045 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2046 &has_children);
2047
2048 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2049 if (attr)
2050 {
2051 unsigned int line_offset = DW_UNSND (attr);
2052 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2053 }
2054 if (lh == NULL)
2055 {
2056 do_cleanups (cleanups);
2057 return;
2058 }
2059
2060 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2061
2062 this_cu->v.quick->lines = lh;
2063
2064 this_cu->v.quick->file_names
2065 = obstack_alloc (&objfile->objfile_obstack,
2066 lh->num_file_names * sizeof (char *));
2067 for (i = 0; i < lh->num_file_names; ++i)
2068 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2069
2070 do_cleanups (cleanups);
2071 }
2072
2073 /* A helper for the "quick" functions which computes and caches the
2074 real path for a given file name from the line table.
2075 dw2_require_line_header must have been called before this is
2076 invoked. */
2077 static const char *
2078 dw2_require_full_path (struct objfile *objfile,
2079 struct dwarf2_per_cu_data *per_cu,
2080 int index)
2081 {
2082 if (!per_cu->v.quick->full_names)
2083 per_cu->v.quick->full_names
2084 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2085 per_cu->v.quick->lines->num_file_names,
2086 sizeof (char *));
2087
2088 if (!per_cu->v.quick->full_names[index])
2089 per_cu->v.quick->full_names[index]
2090 = gdb_realpath (per_cu->v.quick->file_names[index]);
2091
2092 return per_cu->v.quick->full_names[index];
2093 }
2094
2095 static struct symtab *
2096 dw2_find_last_source_symtab (struct objfile *objfile)
2097 {
2098 int index;
2099 dw2_setup (objfile);
2100 index = dwarf2_per_objfile->n_comp_units - 1;
2101 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2102 }
2103
2104 static void
2105 dw2_forget_cached_source_info (struct objfile *objfile)
2106 {
2107 int i;
2108
2109 dw2_setup (objfile);
2110 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2111 + dwarf2_per_objfile->n_type_comp_units); ++i)
2112 {
2113 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2114
2115 if (per_cu->v.quick->full_names)
2116 {
2117 int j;
2118
2119 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2120 xfree ((void *) per_cu->v.quick->full_names[j]);
2121 }
2122 }
2123 }
2124
2125 static int
2126 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2127 const char *full_path, const char *real_path,
2128 struct symtab **result)
2129 {
2130 int i;
2131 int check_basename = lbasename (name) == name;
2132 struct dwarf2_per_cu_data *base_cu = NULL;
2133
2134 dw2_setup (objfile);
2135 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2136 + dwarf2_per_objfile->n_type_comp_units); ++i)
2137 {
2138 int j;
2139 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2140
2141 if (per_cu->v.quick->symtab)
2142 continue;
2143
2144 dw2_require_line_header (objfile, per_cu);
2145 if (!per_cu->v.quick->lines)
2146 continue;
2147
2148 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2149 {
2150 const char *this_name = per_cu->v.quick->file_names[j];
2151
2152 if (FILENAME_CMP (name, this_name) == 0)
2153 {
2154 *result = dw2_instantiate_symtab (objfile, per_cu);
2155 return 1;
2156 }
2157
2158 if (check_basename && ! base_cu
2159 && FILENAME_CMP (lbasename (this_name), name) == 0)
2160 base_cu = per_cu;
2161
2162 if (full_path != NULL)
2163 {
2164 const char *this_full_name = dw2_require_full_path (objfile,
2165 per_cu, j);
2166
2167 if (this_full_name
2168 && FILENAME_CMP (full_path, this_full_name) == 0)
2169 {
2170 *result = dw2_instantiate_symtab (objfile, per_cu);
2171 return 1;
2172 }
2173 }
2174
2175 if (real_path != NULL)
2176 {
2177 const char *this_full_name = dw2_require_full_path (objfile,
2178 per_cu, j);
2179
2180 if (this_full_name != NULL)
2181 {
2182 char *rp = gdb_realpath (this_full_name);
2183 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2184 {
2185 xfree (rp);
2186 *result = dw2_instantiate_symtab (objfile, per_cu);
2187 return 1;
2188 }
2189 xfree (rp);
2190 }
2191 }
2192 }
2193 }
2194
2195 if (base_cu)
2196 {
2197 *result = dw2_instantiate_symtab (objfile, base_cu);
2198 return 1;
2199 }
2200
2201 return 0;
2202 }
2203
2204 static struct symtab *
2205 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2206 const char *name, domain_enum domain)
2207 {
2208 /* We do all the work in the pre_expand_symtabs_matching hook
2209 instead. */
2210 return NULL;
2211 }
2212
2213 /* A helper function that expands all symtabs that hold an object
2214 named NAME. */
2215 static void
2216 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2217 {
2218 dw2_setup (objfile);
2219
2220 if (dwarf2_per_objfile->index_table)
2221 {
2222 offset_type *vec;
2223
2224 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2225 name, &vec))
2226 {
2227 offset_type i, len = MAYBE_SWAP (*vec);
2228 for (i = 0; i < len; ++i)
2229 {
2230 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2231 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2232
2233 dw2_instantiate_symtab (objfile, per_cu);
2234 }
2235 }
2236 }
2237 }
2238
2239 static void
2240 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2241 int kind, const char *name,
2242 domain_enum domain)
2243 {
2244 dw2_do_expand_symtabs_matching (objfile, name);
2245 }
2246
2247 static void
2248 dw2_print_stats (struct objfile *objfile)
2249 {
2250 int i, count;
2251
2252 dw2_setup (objfile);
2253 count = 0;
2254 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2255 + dwarf2_per_objfile->n_type_comp_units); ++i)
2256 {
2257 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2258
2259 if (!per_cu->v.quick->symtab)
2260 ++count;
2261 }
2262 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2263 }
2264
2265 static void
2266 dw2_dump (struct objfile *objfile)
2267 {
2268 /* Nothing worth printing. */
2269 }
2270
2271 static void
2272 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2273 struct section_offsets *delta)
2274 {
2275 /* There's nothing to relocate here. */
2276 }
2277
2278 static void
2279 dw2_expand_symtabs_for_function (struct objfile *objfile,
2280 const char *func_name)
2281 {
2282 dw2_do_expand_symtabs_matching (objfile, func_name);
2283 }
2284
2285 static void
2286 dw2_expand_all_symtabs (struct objfile *objfile)
2287 {
2288 int i;
2289
2290 dw2_setup (objfile);
2291
2292 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2293 + dwarf2_per_objfile->n_type_comp_units); ++i)
2294 {
2295 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2296
2297 dw2_instantiate_symtab (objfile, per_cu);
2298 }
2299 }
2300
2301 static void
2302 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2303 const char *filename)
2304 {
2305 int i;
2306
2307 dw2_setup (objfile);
2308 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2309 + dwarf2_per_objfile->n_type_comp_units); ++i)
2310 {
2311 int j;
2312 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2313
2314 if (per_cu->v.quick->symtab)
2315 continue;
2316
2317 dw2_require_line_header (objfile, per_cu);
2318 if (!per_cu->v.quick->lines)
2319 continue;
2320
2321 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2322 {
2323 const char *this_name = per_cu->v.quick->file_names[j];
2324 if (strcmp (this_name, filename) == 0)
2325 {
2326 dw2_instantiate_symtab (objfile, per_cu);
2327 break;
2328 }
2329 }
2330 }
2331 }
2332
2333 static const char *
2334 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2335 {
2336 struct dwarf2_per_cu_data *per_cu;
2337 offset_type *vec;
2338
2339 dw2_setup (objfile);
2340
2341 if (!dwarf2_per_objfile->index_table)
2342 return NULL;
2343
2344 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2345 name, &vec))
2346 return NULL;
2347
2348 /* Note that this just looks at the very first one named NAME -- but
2349 actually we are looking for a function. find_main_filename
2350 should be rewritten so that it doesn't require a custom hook. It
2351 could just use the ordinary symbol tables. */
2352 /* vec[0] is the length, which must always be >0. */
2353 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2354
2355 dw2_require_line_header (objfile, per_cu);
2356 if (!per_cu->v.quick->lines)
2357 return NULL;
2358
2359 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
2360 }
2361
2362 static void
2363 dw2_map_ada_symtabs (struct objfile *objfile,
2364 int (*wild_match) (const char *, int, const char *),
2365 int (*is_name_suffix) (const char *),
2366 void (*callback) (struct objfile *,
2367 struct symtab *, void *),
2368 const char *name, int global,
2369 domain_enum namespace, int wild,
2370 void *data)
2371 {
2372 /* For now, we don't support Ada. Still the function can be called if the
2373 current language is Ada for a non-Ada objfile using GNU index. As Ada
2374 does not look for non-Ada symbols this function should just return. */
2375 }
2376
2377 static void
2378 dw2_expand_symtabs_matching (struct objfile *objfile,
2379 int (*file_matcher) (const char *, void *),
2380 int (*name_matcher) (const char *, void *),
2381 domain_enum kind,
2382 void *data)
2383 {
2384 int i;
2385 offset_type iter;
2386
2387 dw2_setup (objfile);
2388 if (!dwarf2_per_objfile->index_table)
2389 return;
2390
2391 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2392 + dwarf2_per_objfile->n_type_comp_units); ++i)
2393 {
2394 int j;
2395 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2396
2397 per_cu->v.quick->mark = 0;
2398 if (per_cu->v.quick->symtab)
2399 continue;
2400
2401 dw2_require_line_header (objfile, per_cu);
2402 if (!per_cu->v.quick->lines)
2403 continue;
2404
2405 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2406 {
2407 if (file_matcher (per_cu->v.quick->file_names[j], data))
2408 {
2409 per_cu->v.quick->mark = 1;
2410 break;
2411 }
2412 }
2413 }
2414
2415 for (iter = 0;
2416 iter < dwarf2_per_objfile->index_table->index_table_slots;
2417 ++iter)
2418 {
2419 offset_type idx = 2 * iter;
2420 const char *name;
2421 offset_type *vec, vec_len, vec_idx;
2422
2423 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2424 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2425 continue;
2426
2427 name = (dwarf2_per_objfile->index_table->constant_pool
2428 + dwarf2_per_objfile->index_table->index_table[idx]);
2429
2430 if (! (*name_matcher) (name, data))
2431 continue;
2432
2433 /* The name was matched, now expand corresponding CUs that were
2434 marked. */
2435 vec = (offset_type *) (dwarf2_per_objfile->index_table->constant_pool
2436 + dwarf2_per_objfile->index_table->index_table[idx + 1]);
2437 vec_len = MAYBE_SWAP (vec[0]);
2438 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2439 {
2440 struct dwarf2_per_cu_data *per_cu;
2441
2442 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2443 if (per_cu->v.quick->mark)
2444 dw2_instantiate_symtab (objfile, per_cu);
2445 }
2446 }
2447 }
2448
2449 static struct symtab *
2450 dw2_find_pc_sect_symtab (struct objfile *objfile,
2451 struct minimal_symbol *msymbol,
2452 CORE_ADDR pc,
2453 struct obj_section *section,
2454 int warn_if_readin)
2455 {
2456 struct dwarf2_per_cu_data *data;
2457
2458 dw2_setup (objfile);
2459
2460 if (!objfile->psymtabs_addrmap)
2461 return NULL;
2462
2463 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2464 if (!data)
2465 return NULL;
2466
2467 if (warn_if_readin && data->v.quick->symtab)
2468 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2469 paddress (get_objfile_arch (objfile), pc));
2470
2471 return dw2_instantiate_symtab (objfile, data);
2472 }
2473
2474 static void
2475 dw2_map_symbol_names (struct objfile *objfile,
2476 void (*fun) (const char *, void *),
2477 void *data)
2478 {
2479 offset_type iter;
2480 dw2_setup (objfile);
2481
2482 if (!dwarf2_per_objfile->index_table)
2483 return;
2484
2485 for (iter = 0;
2486 iter < dwarf2_per_objfile->index_table->index_table_slots;
2487 ++iter)
2488 {
2489 offset_type idx = 2 * iter;
2490 const char *name;
2491 offset_type *vec, vec_len, vec_idx;
2492
2493 if (dwarf2_per_objfile->index_table->index_table[idx] == 0
2494 && dwarf2_per_objfile->index_table->index_table[idx + 1] == 0)
2495 continue;
2496
2497 name = (dwarf2_per_objfile->index_table->constant_pool
2498 + dwarf2_per_objfile->index_table->index_table[idx]);
2499
2500 (*fun) (name, data);
2501 }
2502 }
2503
2504 static void
2505 dw2_map_symbol_filenames (struct objfile *objfile,
2506 void (*fun) (const char *, const char *, void *),
2507 void *data)
2508 {
2509 int i;
2510
2511 dw2_setup (objfile);
2512 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2513 + dwarf2_per_objfile->n_type_comp_units); ++i)
2514 {
2515 int j;
2516 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2517
2518 if (per_cu->v.quick->symtab)
2519 continue;
2520
2521 dw2_require_line_header (objfile, per_cu);
2522 if (!per_cu->v.quick->lines)
2523 continue;
2524
2525 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2526 {
2527 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2528 j);
2529 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
2530 }
2531 }
2532 }
2533
2534 static int
2535 dw2_has_symbols (struct objfile *objfile)
2536 {
2537 return 1;
2538 }
2539
2540 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2541 {
2542 dw2_has_symbols,
2543 dw2_find_last_source_symtab,
2544 dw2_forget_cached_source_info,
2545 dw2_lookup_symtab,
2546 dw2_lookup_symbol,
2547 dw2_pre_expand_symtabs_matching,
2548 dw2_print_stats,
2549 dw2_dump,
2550 dw2_relocate,
2551 dw2_expand_symtabs_for_function,
2552 dw2_expand_all_symtabs,
2553 dw2_expand_symtabs_with_filename,
2554 dw2_find_symbol_file,
2555 dw2_map_ada_symtabs,
2556 dw2_expand_symtabs_matching,
2557 dw2_find_pc_sect_symtab,
2558 dw2_map_symbol_names,
2559 dw2_map_symbol_filenames
2560 };
2561
2562 /* Initialize for reading DWARF for this objfile. Return 0 if this
2563 file will use psymtabs, or 1 if using the GNU index. */
2564
2565 int
2566 dwarf2_initialize_objfile (struct objfile *objfile)
2567 {
2568 /* If we're about to read full symbols, don't bother with the
2569 indices. In this case we also don't care if some other debug
2570 format is making psymtabs, because they are all about to be
2571 expanded anyway. */
2572 if ((objfile->flags & OBJF_READNOW))
2573 {
2574 int i;
2575
2576 dwarf2_per_objfile->using_index = 1;
2577 create_all_comp_units (objfile);
2578 create_debug_types_hash_table (objfile);
2579
2580 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2581 + dwarf2_per_objfile->n_type_comp_units); ++i)
2582 {
2583 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2584
2585 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2586 struct dwarf2_per_cu_quick_data);
2587 }
2588
2589 /* Return 1 so that gdb sees the "quick" functions. However,
2590 these functions will be no-ops because we will have expanded
2591 all symtabs. */
2592 return 1;
2593 }
2594
2595 if (dwarf2_read_index (objfile))
2596 return 1;
2597
2598 dwarf2_build_psymtabs (objfile);
2599 return 0;
2600 }
2601
2602 \f
2603
2604 /* Build a partial symbol table. */
2605
2606 void
2607 dwarf2_build_psymtabs (struct objfile *objfile)
2608 {
2609 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2610 {
2611 init_psymbol_list (objfile, 1024);
2612 }
2613
2614 dwarf2_build_psymtabs_hard (objfile);
2615 }
2616
2617 /* Return TRUE if OFFSET is within CU_HEADER. */
2618
2619 static inline int
2620 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2621 {
2622 unsigned int bottom = cu_header->offset;
2623 unsigned int top = (cu_header->offset
2624 + cu_header->length
2625 + cu_header->initial_length_size);
2626
2627 return (offset >= bottom && offset < top);
2628 }
2629
2630 /* Read in the comp unit header information from the debug_info at info_ptr.
2631 NOTE: This leaves members offset, first_die_offset to be filled in
2632 by the caller. */
2633
2634 static gdb_byte *
2635 read_comp_unit_head (struct comp_unit_head *cu_header,
2636 gdb_byte *info_ptr, bfd *abfd)
2637 {
2638 int signed_addr;
2639 unsigned int bytes_read;
2640
2641 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2642 cu_header->initial_length_size = bytes_read;
2643 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2644 info_ptr += bytes_read;
2645 cu_header->version = read_2_bytes (abfd, info_ptr);
2646 info_ptr += 2;
2647 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2648 &bytes_read);
2649 info_ptr += bytes_read;
2650 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2651 info_ptr += 1;
2652 signed_addr = bfd_get_sign_extend_vma (abfd);
2653 if (signed_addr < 0)
2654 internal_error (__FILE__, __LINE__,
2655 _("read_comp_unit_head: dwarf from non elf file"));
2656 cu_header->signed_addr_p = signed_addr;
2657
2658 return info_ptr;
2659 }
2660
2661 static gdb_byte *
2662 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2663 gdb_byte *buffer, unsigned int buffer_size,
2664 bfd *abfd)
2665 {
2666 gdb_byte *beg_of_comp_unit = info_ptr;
2667
2668 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2669
2670 if (header->version != 2 && header->version != 3 && header->version != 4)
2671 error (_("Dwarf Error: wrong version in compilation unit header "
2672 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2673 bfd_get_filename (abfd));
2674
2675 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2676 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2677 "(offset 0x%lx + 6) [in module %s]"),
2678 (long) header->abbrev_offset,
2679 (long) (beg_of_comp_unit - buffer),
2680 bfd_get_filename (abfd));
2681
2682 if (beg_of_comp_unit + header->length + header->initial_length_size
2683 > buffer + buffer_size)
2684 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2685 "(offset 0x%lx + 0) [in module %s]"),
2686 (long) header->length,
2687 (long) (beg_of_comp_unit - buffer),
2688 bfd_get_filename (abfd));
2689
2690 return info_ptr;
2691 }
2692
2693 /* Read in the types comp unit header information from .debug_types entry at
2694 types_ptr. The result is a pointer to one past the end of the header. */
2695
2696 static gdb_byte *
2697 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2698 ULONGEST *signature,
2699 gdb_byte *types_ptr, bfd *abfd)
2700 {
2701 gdb_byte *initial_types_ptr = types_ptr;
2702
2703 dwarf2_read_section (dwarf2_per_objfile->objfile,
2704 &dwarf2_per_objfile->types);
2705 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2706
2707 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2708
2709 *signature = read_8_bytes (abfd, types_ptr);
2710 types_ptr += 8;
2711 types_ptr += cu_header->offset_size;
2712 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2713
2714 return types_ptr;
2715 }
2716
2717 /* Allocate a new partial symtab for file named NAME and mark this new
2718 partial symtab as being an include of PST. */
2719
2720 static void
2721 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2722 struct objfile *objfile)
2723 {
2724 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2725
2726 subpst->section_offsets = pst->section_offsets;
2727 subpst->textlow = 0;
2728 subpst->texthigh = 0;
2729
2730 subpst->dependencies = (struct partial_symtab **)
2731 obstack_alloc (&objfile->objfile_obstack,
2732 sizeof (struct partial_symtab *));
2733 subpst->dependencies[0] = pst;
2734 subpst->number_of_dependencies = 1;
2735
2736 subpst->globals_offset = 0;
2737 subpst->n_global_syms = 0;
2738 subpst->statics_offset = 0;
2739 subpst->n_static_syms = 0;
2740 subpst->symtab = NULL;
2741 subpst->read_symtab = pst->read_symtab;
2742 subpst->readin = 0;
2743
2744 /* No private part is necessary for include psymtabs. This property
2745 can be used to differentiate between such include psymtabs and
2746 the regular ones. */
2747 subpst->read_symtab_private = NULL;
2748 }
2749
2750 /* Read the Line Number Program data and extract the list of files
2751 included by the source file represented by PST. Build an include
2752 partial symtab for each of these included files. */
2753
2754 static void
2755 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2756 struct die_info *die,
2757 struct partial_symtab *pst)
2758 {
2759 struct objfile *objfile = cu->objfile;
2760 bfd *abfd = objfile->obfd;
2761 struct line_header *lh = NULL;
2762 struct attribute *attr;
2763
2764 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2765 if (attr)
2766 {
2767 unsigned int line_offset = DW_UNSND (attr);
2768
2769 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2770 }
2771 if (lh == NULL)
2772 return; /* No linetable, so no includes. */
2773
2774 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2775 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2776
2777 free_line_header (lh);
2778 }
2779
2780 static hashval_t
2781 hash_type_signature (const void *item)
2782 {
2783 const struct signatured_type *type_sig = item;
2784
2785 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2786 return type_sig->signature;
2787 }
2788
2789 static int
2790 eq_type_signature (const void *item_lhs, const void *item_rhs)
2791 {
2792 const struct signatured_type *lhs = item_lhs;
2793 const struct signatured_type *rhs = item_rhs;
2794
2795 return lhs->signature == rhs->signature;
2796 }
2797
2798 /* Allocate a hash table for signatured types. */
2799
2800 static htab_t
2801 allocate_signatured_type_table (struct objfile *objfile)
2802 {
2803 return htab_create_alloc_ex (41,
2804 hash_type_signature,
2805 eq_type_signature,
2806 NULL,
2807 &objfile->objfile_obstack,
2808 hashtab_obstack_allocate,
2809 dummy_obstack_deallocate);
2810 }
2811
2812 /* A helper function to add a signatured type CU to a list. */
2813
2814 static int
2815 add_signatured_type_cu_to_list (void **slot, void *datum)
2816 {
2817 struct signatured_type *sigt = *slot;
2818 struct dwarf2_per_cu_data ***datap = datum;
2819
2820 **datap = &sigt->per_cu;
2821 ++*datap;
2822
2823 return 1;
2824 }
2825
2826 /* Create the hash table of all entries in the .debug_types section.
2827 The result is zero if there is an error (e.g. missing .debug_types section),
2828 otherwise non-zero. */
2829
2830 static int
2831 create_debug_types_hash_table (struct objfile *objfile)
2832 {
2833 gdb_byte *info_ptr;
2834 htab_t types_htab;
2835 struct dwarf2_per_cu_data **iter;
2836
2837 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2838 info_ptr = dwarf2_per_objfile->types.buffer;
2839
2840 if (info_ptr == NULL)
2841 {
2842 dwarf2_per_objfile->signatured_types = NULL;
2843 return 0;
2844 }
2845
2846 types_htab = allocate_signatured_type_table (objfile);
2847
2848 if (dwarf2_die_debug)
2849 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2850
2851 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2852 {
2853 unsigned int offset;
2854 unsigned int offset_size;
2855 unsigned int type_offset;
2856 unsigned int length, initial_length_size;
2857 unsigned short version;
2858 ULONGEST signature;
2859 struct signatured_type *type_sig;
2860 void **slot;
2861 gdb_byte *ptr = info_ptr;
2862
2863 offset = ptr - dwarf2_per_objfile->types.buffer;
2864
2865 /* We need to read the type's signature in order to build the hash
2866 table, but we don't need to read anything else just yet. */
2867
2868 /* Sanity check to ensure entire cu is present. */
2869 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2870 if (ptr + length + initial_length_size
2871 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2872 {
2873 complaint (&symfile_complaints,
2874 _("debug type entry runs off end of `.debug_types' section, ignored"));
2875 break;
2876 }
2877
2878 offset_size = initial_length_size == 4 ? 4 : 8;
2879 ptr += initial_length_size;
2880 version = bfd_get_16 (objfile->obfd, ptr);
2881 ptr += 2;
2882 ptr += offset_size; /* abbrev offset */
2883 ptr += 1; /* address size */
2884 signature = bfd_get_64 (objfile->obfd, ptr);
2885 ptr += 8;
2886 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2887
2888 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2889 memset (type_sig, 0, sizeof (*type_sig));
2890 type_sig->signature = signature;
2891 type_sig->offset = offset;
2892 type_sig->type_offset = type_offset;
2893 type_sig->per_cu.objfile = objfile;
2894 type_sig->per_cu.from_debug_types = 1;
2895
2896 slot = htab_find_slot (types_htab, type_sig, INSERT);
2897 gdb_assert (slot != NULL);
2898 *slot = type_sig;
2899
2900 if (dwarf2_die_debug)
2901 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2902 offset, phex (signature, sizeof (signature)));
2903
2904 info_ptr = info_ptr + initial_length_size + length;
2905 }
2906
2907 dwarf2_per_objfile->signatured_types = types_htab;
2908
2909 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2910 dwarf2_per_objfile->type_comp_units
2911 = obstack_alloc (&objfile->objfile_obstack,
2912 dwarf2_per_objfile->n_type_comp_units
2913 * sizeof (struct dwarf2_per_cu_data *));
2914 iter = &dwarf2_per_objfile->type_comp_units[0];
2915 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2916 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2917 == dwarf2_per_objfile->n_type_comp_units);
2918
2919 return 1;
2920 }
2921
2922 /* Lookup a signature based type.
2923 Returns NULL if SIG is not present in the table. */
2924
2925 static struct signatured_type *
2926 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2927 {
2928 struct signatured_type find_entry, *entry;
2929
2930 if (dwarf2_per_objfile->signatured_types == NULL)
2931 {
2932 complaint (&symfile_complaints,
2933 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2934 return 0;
2935 }
2936
2937 find_entry.signature = sig;
2938 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2939 return entry;
2940 }
2941
2942 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2943
2944 static void
2945 init_cu_die_reader (struct die_reader_specs *reader,
2946 struct dwarf2_cu *cu)
2947 {
2948 reader->abfd = cu->objfile->obfd;
2949 reader->cu = cu;
2950 if (cu->per_cu->from_debug_types)
2951 {
2952 gdb_assert (dwarf2_per_objfile->types.readin);
2953 reader->buffer = dwarf2_per_objfile->types.buffer;
2954 }
2955 else
2956 {
2957 gdb_assert (dwarf2_per_objfile->info.readin);
2958 reader->buffer = dwarf2_per_objfile->info.buffer;
2959 }
2960 }
2961
2962 /* Find the base address of the compilation unit for range lists and
2963 location lists. It will normally be specified by DW_AT_low_pc.
2964 In DWARF-3 draft 4, the base address could be overridden by
2965 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2966 compilation units with discontinuous ranges. */
2967
2968 static void
2969 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2970 {
2971 struct attribute *attr;
2972
2973 cu->base_known = 0;
2974 cu->base_address = 0;
2975
2976 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2977 if (attr)
2978 {
2979 cu->base_address = DW_ADDR (attr);
2980 cu->base_known = 1;
2981 }
2982 else
2983 {
2984 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2985 if (attr)
2986 {
2987 cu->base_address = DW_ADDR (attr);
2988 cu->base_known = 1;
2989 }
2990 }
2991 }
2992
2993 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2994 to combine the common parts.
2995 Process a compilation unit for a psymtab.
2996 BUFFER is a pointer to the beginning of the dwarf section buffer,
2997 either .debug_info or debug_types.
2998 INFO_PTR is a pointer to the start of the CU.
2999 Returns a pointer to the next CU. */
3000
3001 static gdb_byte *
3002 process_psymtab_comp_unit (struct objfile *objfile,
3003 struct dwarf2_per_cu_data *this_cu,
3004 gdb_byte *buffer, gdb_byte *info_ptr,
3005 unsigned int buffer_size)
3006 {
3007 bfd *abfd = objfile->obfd;
3008 gdb_byte *beg_of_comp_unit = info_ptr;
3009 struct die_info *comp_unit_die;
3010 struct partial_symtab *pst;
3011 CORE_ADDR baseaddr;
3012 struct cleanup *back_to_inner;
3013 struct dwarf2_cu cu;
3014 int has_children, has_pc_info;
3015 struct attribute *attr;
3016 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3017 struct die_reader_specs reader_specs;
3018
3019 memset (&cu, 0, sizeof (cu));
3020 cu.objfile = objfile;
3021 obstack_init (&cu.comp_unit_obstack);
3022
3023 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3024
3025 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3026 buffer, buffer_size,
3027 abfd);
3028
3029 /* Complete the cu_header. */
3030 cu.header.offset = beg_of_comp_unit - buffer;
3031 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3032
3033 cu.list_in_scope = &file_symbols;
3034
3035 /* If this compilation unit was already read in, free the
3036 cached copy in order to read it in again. This is
3037 necessary because we skipped some symbols when we first
3038 read in the compilation unit (see load_partial_dies).
3039 This problem could be avoided, but the benefit is
3040 unclear. */
3041 if (this_cu->cu != NULL)
3042 free_one_cached_comp_unit (this_cu->cu);
3043
3044 /* Note that this is a pointer to our stack frame, being
3045 added to a global data structure. It will be cleaned up
3046 in free_stack_comp_unit when we finish with this
3047 compilation unit. */
3048 this_cu->cu = &cu;
3049 cu.per_cu = this_cu;
3050
3051 /* Read the abbrevs for this compilation unit into a table. */
3052 dwarf2_read_abbrevs (abfd, &cu);
3053 make_cleanup (dwarf2_free_abbrev_table, &cu);
3054
3055 /* Read the compilation unit die. */
3056 if (this_cu->from_debug_types)
3057 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3058 init_cu_die_reader (&reader_specs, &cu);
3059 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3060 &has_children);
3061
3062 if (this_cu->from_debug_types)
3063 {
3064 /* offset,length haven't been set yet for type units. */
3065 this_cu->offset = cu.header.offset;
3066 this_cu->length = cu.header.length + cu.header.initial_length_size;
3067 }
3068 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3069 {
3070 info_ptr = (beg_of_comp_unit + cu.header.length
3071 + cu.header.initial_length_size);
3072 do_cleanups (back_to_inner);
3073 return info_ptr;
3074 }
3075
3076 /* Set the language we're debugging. */
3077 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3078 if (attr)
3079 set_cu_language (DW_UNSND (attr), &cu);
3080 else
3081 set_cu_language (language_minimal, &cu);
3082
3083 /* Allocate a new partial symbol table structure. */
3084 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3085 pst = start_psymtab_common (objfile, objfile->section_offsets,
3086 (attr != NULL) ? DW_STRING (attr) : "",
3087 /* TEXTLOW and TEXTHIGH are set below. */
3088 0,
3089 objfile->global_psymbols.next,
3090 objfile->static_psymbols.next);
3091
3092 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3093 if (attr != NULL)
3094 pst->dirname = DW_STRING (attr);
3095
3096 pst->read_symtab_private = this_cu;
3097
3098 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3099
3100 /* Store the function that reads in the rest of the symbol table */
3101 pst->read_symtab = dwarf2_psymtab_to_symtab;
3102
3103 this_cu->v.psymtab = pst;
3104
3105 dwarf2_find_base_address (comp_unit_die, &cu);
3106
3107 /* Possibly set the default values of LOWPC and HIGHPC from
3108 `DW_AT_ranges'. */
3109 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3110 &best_highpc, &cu, pst);
3111 if (has_pc_info == 1 && best_lowpc < best_highpc)
3112 /* Store the contiguous range if it is not empty; it can be empty for
3113 CUs with no code. */
3114 addrmap_set_empty (objfile->psymtabs_addrmap,
3115 best_lowpc + baseaddr,
3116 best_highpc + baseaddr - 1, pst);
3117
3118 /* Check if comp unit has_children.
3119 If so, read the rest of the partial symbols from this comp unit.
3120 If not, there's no more debug_info for this comp unit. */
3121 if (has_children)
3122 {
3123 struct partial_die_info *first_die;
3124 CORE_ADDR lowpc, highpc;
3125
3126 lowpc = ((CORE_ADDR) -1);
3127 highpc = ((CORE_ADDR) 0);
3128
3129 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3130
3131 scan_partial_symbols (first_die, &lowpc, &highpc,
3132 ! has_pc_info, &cu);
3133
3134 /* If we didn't find a lowpc, set it to highpc to avoid
3135 complaints from `maint check'. */
3136 if (lowpc == ((CORE_ADDR) -1))
3137 lowpc = highpc;
3138
3139 /* If the compilation unit didn't have an explicit address range,
3140 then use the information extracted from its child dies. */
3141 if (! has_pc_info)
3142 {
3143 best_lowpc = lowpc;
3144 best_highpc = highpc;
3145 }
3146 }
3147 pst->textlow = best_lowpc + baseaddr;
3148 pst->texthigh = best_highpc + baseaddr;
3149
3150 pst->n_global_syms = objfile->global_psymbols.next -
3151 (objfile->global_psymbols.list + pst->globals_offset);
3152 pst->n_static_syms = objfile->static_psymbols.next -
3153 (objfile->static_psymbols.list + pst->statics_offset);
3154 sort_pst_symbols (pst);
3155
3156 info_ptr = (beg_of_comp_unit + cu.header.length
3157 + cu.header.initial_length_size);
3158
3159 if (this_cu->from_debug_types)
3160 {
3161 /* It's not clear we want to do anything with stmt lists here.
3162 Waiting to see what gcc ultimately does. */
3163 }
3164 else
3165 {
3166 /* Get the list of files included in the current compilation unit,
3167 and build a psymtab for each of them. */
3168 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3169 }
3170
3171 do_cleanups (back_to_inner);
3172
3173 return info_ptr;
3174 }
3175
3176 /* Traversal function for htab_traverse_noresize.
3177 Process one .debug_types comp-unit. */
3178
3179 static int
3180 process_type_comp_unit (void **slot, void *info)
3181 {
3182 struct signatured_type *entry = (struct signatured_type *) *slot;
3183 struct objfile *objfile = (struct objfile *) info;
3184 struct dwarf2_per_cu_data *this_cu;
3185
3186 this_cu = &entry->per_cu;
3187
3188 gdb_assert (dwarf2_per_objfile->types.readin);
3189 process_psymtab_comp_unit (objfile, this_cu,
3190 dwarf2_per_objfile->types.buffer,
3191 dwarf2_per_objfile->types.buffer + entry->offset,
3192 dwarf2_per_objfile->types.size);
3193
3194 return 1;
3195 }
3196
3197 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3198 Build partial symbol tables for the .debug_types comp-units. */
3199
3200 static void
3201 build_type_psymtabs (struct objfile *objfile)
3202 {
3203 if (! create_debug_types_hash_table (objfile))
3204 return;
3205
3206 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3207 process_type_comp_unit, objfile);
3208 }
3209
3210 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3211
3212 static void
3213 psymtabs_addrmap_cleanup (void *o)
3214 {
3215 struct objfile *objfile = o;
3216
3217 objfile->psymtabs_addrmap = NULL;
3218 }
3219
3220 /* Build the partial symbol table by doing a quick pass through the
3221 .debug_info and .debug_abbrev sections. */
3222
3223 static void
3224 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3225 {
3226 gdb_byte *info_ptr;
3227 struct cleanup *back_to, *addrmap_cleanup;
3228 struct obstack temp_obstack;
3229
3230 dwarf2_per_objfile->reading_partial_symbols = 1;
3231
3232 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3233 info_ptr = dwarf2_per_objfile->info.buffer;
3234
3235 /* Any cached compilation units will be linked by the per-objfile
3236 read_in_chain. Make sure to free them when we're done. */
3237 back_to = make_cleanup (free_cached_comp_units, NULL);
3238
3239 build_type_psymtabs (objfile);
3240
3241 create_all_comp_units (objfile);
3242
3243 /* Create a temporary address map on a temporary obstack. We later
3244 copy this to the final obstack. */
3245 obstack_init (&temp_obstack);
3246 make_cleanup_obstack_free (&temp_obstack);
3247 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3248 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3249
3250 /* Since the objects we're extracting from .debug_info vary in
3251 length, only the individual functions to extract them (like
3252 read_comp_unit_head and load_partial_die) can really know whether
3253 the buffer is large enough to hold another complete object.
3254
3255 At the moment, they don't actually check that. If .debug_info
3256 holds just one extra byte after the last compilation unit's dies,
3257 then read_comp_unit_head will happily read off the end of the
3258 buffer. read_partial_die is similarly casual. Those functions
3259 should be fixed.
3260
3261 For this loop condition, simply checking whether there's any data
3262 left at all should be sufficient. */
3263
3264 while (info_ptr < (dwarf2_per_objfile->info.buffer
3265 + dwarf2_per_objfile->info.size))
3266 {
3267 struct dwarf2_per_cu_data *this_cu;
3268
3269 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3270 objfile);
3271
3272 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3273 dwarf2_per_objfile->info.buffer,
3274 info_ptr,
3275 dwarf2_per_objfile->info.size);
3276 }
3277
3278 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3279 &objfile->objfile_obstack);
3280 discard_cleanups (addrmap_cleanup);
3281
3282 do_cleanups (back_to);
3283 }
3284
3285 /* Load the partial DIEs for a secondary CU into memory. */
3286
3287 static void
3288 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3289 struct objfile *objfile)
3290 {
3291 bfd *abfd = objfile->obfd;
3292 gdb_byte *info_ptr, *beg_of_comp_unit;
3293 struct die_info *comp_unit_die;
3294 struct dwarf2_cu *cu;
3295 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3296 struct attribute *attr;
3297 int has_children;
3298 struct die_reader_specs reader_specs;
3299 int read_cu = 0;
3300
3301 gdb_assert (! this_cu->from_debug_types);
3302
3303 gdb_assert (dwarf2_per_objfile->info.readin);
3304 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3305 beg_of_comp_unit = info_ptr;
3306
3307 if (this_cu->cu == NULL)
3308 {
3309 cu = alloc_one_comp_unit (objfile);
3310
3311 read_cu = 1;
3312
3313 /* If an error occurs while loading, release our storage. */
3314 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3315
3316 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3317 dwarf2_per_objfile->info.buffer,
3318 dwarf2_per_objfile->info.size,
3319 abfd);
3320
3321 /* Complete the cu_header. */
3322 cu->header.offset = this_cu->offset;
3323 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3324
3325 /* Link this compilation unit into the compilation unit tree. */
3326 this_cu->cu = cu;
3327 cu->per_cu = this_cu;
3328
3329 /* Link this CU into read_in_chain. */
3330 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3331 dwarf2_per_objfile->read_in_chain = this_cu;
3332 }
3333 else
3334 {
3335 cu = this_cu->cu;
3336 info_ptr += cu->header.first_die_offset;
3337 }
3338
3339 /* Read the abbrevs for this compilation unit into a table. */
3340 gdb_assert (cu->dwarf2_abbrevs == NULL);
3341 dwarf2_read_abbrevs (abfd, cu);
3342 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3343
3344 /* Read the compilation unit die. */
3345 init_cu_die_reader (&reader_specs, cu);
3346 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3347 &has_children);
3348
3349 /* Set the language we're debugging. */
3350 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3351 if (attr)
3352 set_cu_language (DW_UNSND (attr), cu);
3353 else
3354 set_cu_language (language_minimal, cu);
3355
3356 /* Check if comp unit has_children.
3357 If so, read the rest of the partial symbols from this comp unit.
3358 If not, there's no more debug_info for this comp unit. */
3359 if (has_children)
3360 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3361
3362 do_cleanups (free_abbrevs_cleanup);
3363
3364 if (read_cu)
3365 {
3366 /* We've successfully allocated this compilation unit. Let our
3367 caller clean it up when finished with it. */
3368 discard_cleanups (free_cu_cleanup);
3369 }
3370 }
3371
3372 /* Create a list of all compilation units in OBJFILE. We do this only
3373 if an inter-comp-unit reference is found; presumably if there is one,
3374 there will be many, and one will occur early in the .debug_info section.
3375 So there's no point in building this list incrementally. */
3376
3377 static void
3378 create_all_comp_units (struct objfile *objfile)
3379 {
3380 int n_allocated;
3381 int n_comp_units;
3382 struct dwarf2_per_cu_data **all_comp_units;
3383 gdb_byte *info_ptr;
3384
3385 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3386 info_ptr = dwarf2_per_objfile->info.buffer;
3387
3388 n_comp_units = 0;
3389 n_allocated = 10;
3390 all_comp_units = xmalloc (n_allocated
3391 * sizeof (struct dwarf2_per_cu_data *));
3392
3393 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3394 {
3395 unsigned int length, initial_length_size;
3396 struct dwarf2_per_cu_data *this_cu;
3397 unsigned int offset;
3398
3399 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3400
3401 /* Read just enough information to find out where the next
3402 compilation unit is. */
3403 length = read_initial_length (objfile->obfd, info_ptr,
3404 &initial_length_size);
3405
3406 /* Save the compilation unit for later lookup. */
3407 this_cu = obstack_alloc (&objfile->objfile_obstack,
3408 sizeof (struct dwarf2_per_cu_data));
3409 memset (this_cu, 0, sizeof (*this_cu));
3410 this_cu->offset = offset;
3411 this_cu->length = length + initial_length_size;
3412 this_cu->objfile = objfile;
3413
3414 if (n_comp_units == n_allocated)
3415 {
3416 n_allocated *= 2;
3417 all_comp_units = xrealloc (all_comp_units,
3418 n_allocated
3419 * sizeof (struct dwarf2_per_cu_data *));
3420 }
3421 all_comp_units[n_comp_units++] = this_cu;
3422
3423 info_ptr = info_ptr + this_cu->length;
3424 }
3425
3426 dwarf2_per_objfile->all_comp_units
3427 = obstack_alloc (&objfile->objfile_obstack,
3428 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3429 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3430 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3431 xfree (all_comp_units);
3432 dwarf2_per_objfile->n_comp_units = n_comp_units;
3433 }
3434
3435 /* Process all loaded DIEs for compilation unit CU, starting at
3436 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3437 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3438 DW_AT_ranges). If NEED_PC is set, then this function will set
3439 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3440 and record the covered ranges in the addrmap. */
3441
3442 static void
3443 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3444 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3445 {
3446 struct partial_die_info *pdi;
3447
3448 /* Now, march along the PDI's, descending into ones which have
3449 interesting children but skipping the children of the other ones,
3450 until we reach the end of the compilation unit. */
3451
3452 pdi = first_die;
3453
3454 while (pdi != NULL)
3455 {
3456 fixup_partial_die (pdi, cu);
3457
3458 /* Anonymous namespaces or modules have no name but have interesting
3459 children, so we need to look at them. Ditto for anonymous
3460 enums. */
3461
3462 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3463 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3464 {
3465 switch (pdi->tag)
3466 {
3467 case DW_TAG_subprogram:
3468 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3469 break;
3470 case DW_TAG_variable:
3471 case DW_TAG_typedef:
3472 case DW_TAG_union_type:
3473 if (!pdi->is_declaration)
3474 {
3475 add_partial_symbol (pdi, cu);
3476 }
3477 break;
3478 case DW_TAG_class_type:
3479 case DW_TAG_interface_type:
3480 case DW_TAG_structure_type:
3481 if (!pdi->is_declaration)
3482 {
3483 add_partial_symbol (pdi, cu);
3484 }
3485 break;
3486 case DW_TAG_enumeration_type:
3487 if (!pdi->is_declaration)
3488 add_partial_enumeration (pdi, cu);
3489 break;
3490 case DW_TAG_base_type:
3491 case DW_TAG_subrange_type:
3492 /* File scope base type definitions are added to the partial
3493 symbol table. */
3494 add_partial_symbol (pdi, cu);
3495 break;
3496 case DW_TAG_namespace:
3497 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3498 break;
3499 case DW_TAG_module:
3500 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3501 break;
3502 default:
3503 break;
3504 }
3505 }
3506
3507 /* If the die has a sibling, skip to the sibling. */
3508
3509 pdi = pdi->die_sibling;
3510 }
3511 }
3512
3513 /* Functions used to compute the fully scoped name of a partial DIE.
3514
3515 Normally, this is simple. For C++, the parent DIE's fully scoped
3516 name is concatenated with "::" and the partial DIE's name. For
3517 Java, the same thing occurs except that "." is used instead of "::".
3518 Enumerators are an exception; they use the scope of their parent
3519 enumeration type, i.e. the name of the enumeration type is not
3520 prepended to the enumerator.
3521
3522 There are two complexities. One is DW_AT_specification; in this
3523 case "parent" means the parent of the target of the specification,
3524 instead of the direct parent of the DIE. The other is compilers
3525 which do not emit DW_TAG_namespace; in this case we try to guess
3526 the fully qualified name of structure types from their members'
3527 linkage names. This must be done using the DIE's children rather
3528 than the children of any DW_AT_specification target. We only need
3529 to do this for structures at the top level, i.e. if the target of
3530 any DW_AT_specification (if any; otherwise the DIE itself) does not
3531 have a parent. */
3532
3533 /* Compute the scope prefix associated with PDI's parent, in
3534 compilation unit CU. The result will be allocated on CU's
3535 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3536 field. NULL is returned if no prefix is necessary. */
3537 static char *
3538 partial_die_parent_scope (struct partial_die_info *pdi,
3539 struct dwarf2_cu *cu)
3540 {
3541 char *grandparent_scope;
3542 struct partial_die_info *parent, *real_pdi;
3543
3544 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3545 then this means the parent of the specification DIE. */
3546
3547 real_pdi = pdi;
3548 while (real_pdi->has_specification)
3549 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3550
3551 parent = real_pdi->die_parent;
3552 if (parent == NULL)
3553 return NULL;
3554
3555 if (parent->scope_set)
3556 return parent->scope;
3557
3558 fixup_partial_die (parent, cu);
3559
3560 grandparent_scope = partial_die_parent_scope (parent, cu);
3561
3562 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3563 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3564 Work around this problem here. */
3565 if (cu->language == language_cplus
3566 && parent->tag == DW_TAG_namespace
3567 && strcmp (parent->name, "::") == 0
3568 && grandparent_scope == NULL)
3569 {
3570 parent->scope = NULL;
3571 parent->scope_set = 1;
3572 return NULL;
3573 }
3574
3575 if (parent->tag == DW_TAG_namespace
3576 || parent->tag == DW_TAG_module
3577 || parent->tag == DW_TAG_structure_type
3578 || parent->tag == DW_TAG_class_type
3579 || parent->tag == DW_TAG_interface_type
3580 || parent->tag == DW_TAG_union_type
3581 || parent->tag == DW_TAG_enumeration_type)
3582 {
3583 if (grandparent_scope == NULL)
3584 parent->scope = parent->name;
3585 else
3586 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3587 parent->name, 0, cu);
3588 }
3589 else if (parent->tag == DW_TAG_enumerator)
3590 /* Enumerators should not get the name of the enumeration as a prefix. */
3591 parent->scope = grandparent_scope;
3592 else
3593 {
3594 /* FIXME drow/2004-04-01: What should we be doing with
3595 function-local names? For partial symbols, we should probably be
3596 ignoring them. */
3597 complaint (&symfile_complaints,
3598 _("unhandled containing DIE tag %d for DIE at %d"),
3599 parent->tag, pdi->offset);
3600 parent->scope = grandparent_scope;
3601 }
3602
3603 parent->scope_set = 1;
3604 return parent->scope;
3605 }
3606
3607 /* Return the fully scoped name associated with PDI, from compilation unit
3608 CU. The result will be allocated with malloc. */
3609 static char *
3610 partial_die_full_name (struct partial_die_info *pdi,
3611 struct dwarf2_cu *cu)
3612 {
3613 char *parent_scope;
3614
3615 /* If this is a template instantiation, we can not work out the
3616 template arguments from partial DIEs. So, unfortunately, we have
3617 to go through the full DIEs. At least any work we do building
3618 types here will be reused if full symbols are loaded later. */
3619 if (pdi->has_template_arguments)
3620 {
3621 fixup_partial_die (pdi, cu);
3622
3623 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3624 {
3625 struct die_info *die;
3626 struct attribute attr;
3627 struct dwarf2_cu *ref_cu = cu;
3628
3629 attr.name = 0;
3630 attr.form = DW_FORM_ref_addr;
3631 attr.u.addr = pdi->offset;
3632 die = follow_die_ref (NULL, &attr, &ref_cu);
3633
3634 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3635 }
3636 }
3637
3638 parent_scope = partial_die_parent_scope (pdi, cu);
3639 if (parent_scope == NULL)
3640 return NULL;
3641 else
3642 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3643 }
3644
3645 static void
3646 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3647 {
3648 struct objfile *objfile = cu->objfile;
3649 CORE_ADDR addr = 0;
3650 char *actual_name = NULL;
3651 const struct partial_symbol *psym = NULL;
3652 CORE_ADDR baseaddr;
3653 int built_actual_name = 0;
3654
3655 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3656
3657 actual_name = partial_die_full_name (pdi, cu);
3658 if (actual_name)
3659 built_actual_name = 1;
3660
3661 if (actual_name == NULL)
3662 actual_name = pdi->name;
3663
3664 switch (pdi->tag)
3665 {
3666 case DW_TAG_subprogram:
3667 if (pdi->is_external || cu->language == language_ada)
3668 {
3669 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3670 of the global scope. But in Ada, we want to be able to access
3671 nested procedures globally. So all Ada subprograms are stored
3672 in the global scope. */
3673 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3674 mst_text, objfile); */
3675 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3676 built_actual_name,
3677 VAR_DOMAIN, LOC_BLOCK,
3678 &objfile->global_psymbols,
3679 0, pdi->lowpc + baseaddr,
3680 cu->language, objfile);
3681 }
3682 else
3683 {
3684 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3685 mst_file_text, objfile); */
3686 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3687 built_actual_name,
3688 VAR_DOMAIN, LOC_BLOCK,
3689 &objfile->static_psymbols,
3690 0, pdi->lowpc + baseaddr,
3691 cu->language, objfile);
3692 }
3693 break;
3694 case DW_TAG_variable:
3695 if (pdi->locdesc)
3696 addr = decode_locdesc (pdi->locdesc, cu);
3697
3698 if (pdi->locdesc
3699 && addr == 0
3700 && !dwarf2_per_objfile->has_section_at_zero)
3701 {
3702 /* A global or static variable may also have been stripped
3703 out by the linker if unused, in which case its address
3704 will be nullified; do not add such variables into partial
3705 symbol table then. */
3706 }
3707 else if (pdi->is_external)
3708 {
3709 /* Global Variable.
3710 Don't enter into the minimal symbol tables as there is
3711 a minimal symbol table entry from the ELF symbols already.
3712 Enter into partial symbol table if it has a location
3713 descriptor or a type.
3714 If the location descriptor is missing, new_symbol will create
3715 a LOC_UNRESOLVED symbol, the address of the variable will then
3716 be determined from the minimal symbol table whenever the variable
3717 is referenced.
3718 The address for the partial symbol table entry is not
3719 used by GDB, but it comes in handy for debugging partial symbol
3720 table building. */
3721
3722 if (pdi->locdesc || pdi->has_type)
3723 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3724 built_actual_name,
3725 VAR_DOMAIN, LOC_STATIC,
3726 &objfile->global_psymbols,
3727 0, addr + baseaddr,
3728 cu->language, objfile);
3729 }
3730 else
3731 {
3732 /* Static Variable. Skip symbols without location descriptors. */
3733 if (pdi->locdesc == NULL)
3734 {
3735 if (built_actual_name)
3736 xfree (actual_name);
3737 return;
3738 }
3739 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3740 mst_file_data, objfile); */
3741 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3742 built_actual_name,
3743 VAR_DOMAIN, LOC_STATIC,
3744 &objfile->static_psymbols,
3745 0, addr + baseaddr,
3746 cu->language, objfile);
3747 }
3748 break;
3749 case DW_TAG_typedef:
3750 case DW_TAG_base_type:
3751 case DW_TAG_subrange_type:
3752 add_psymbol_to_list (actual_name, strlen (actual_name),
3753 built_actual_name,
3754 VAR_DOMAIN, LOC_TYPEDEF,
3755 &objfile->static_psymbols,
3756 0, (CORE_ADDR) 0, cu->language, objfile);
3757 break;
3758 case DW_TAG_namespace:
3759 add_psymbol_to_list (actual_name, strlen (actual_name),
3760 built_actual_name,
3761 VAR_DOMAIN, LOC_TYPEDEF,
3762 &objfile->global_psymbols,
3763 0, (CORE_ADDR) 0, cu->language, objfile);
3764 break;
3765 case DW_TAG_class_type:
3766 case DW_TAG_interface_type:
3767 case DW_TAG_structure_type:
3768 case DW_TAG_union_type:
3769 case DW_TAG_enumeration_type:
3770 /* Skip external references. The DWARF standard says in the section
3771 about "Structure, Union, and Class Type Entries": "An incomplete
3772 structure, union or class type is represented by a structure,
3773 union or class entry that does not have a byte size attribute
3774 and that has a DW_AT_declaration attribute." */
3775 if (!pdi->has_byte_size && pdi->is_declaration)
3776 {
3777 if (built_actual_name)
3778 xfree (actual_name);
3779 return;
3780 }
3781
3782 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3783 static vs. global. */
3784 add_psymbol_to_list (actual_name, strlen (actual_name),
3785 built_actual_name,
3786 STRUCT_DOMAIN, LOC_TYPEDEF,
3787 (cu->language == language_cplus
3788 || cu->language == language_java)
3789 ? &objfile->global_psymbols
3790 : &objfile->static_psymbols,
3791 0, (CORE_ADDR) 0, cu->language, objfile);
3792
3793 break;
3794 case DW_TAG_enumerator:
3795 add_psymbol_to_list (actual_name, strlen (actual_name),
3796 built_actual_name,
3797 VAR_DOMAIN, LOC_CONST,
3798 (cu->language == language_cplus
3799 || cu->language == language_java)
3800 ? &objfile->global_psymbols
3801 : &objfile->static_psymbols,
3802 0, (CORE_ADDR) 0, cu->language, objfile);
3803 break;
3804 default:
3805 break;
3806 }
3807
3808 if (built_actual_name)
3809 xfree (actual_name);
3810 }
3811
3812 /* Read a partial die corresponding to a namespace; also, add a symbol
3813 corresponding to that namespace to the symbol table. NAMESPACE is
3814 the name of the enclosing namespace. */
3815
3816 static void
3817 add_partial_namespace (struct partial_die_info *pdi,
3818 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3819 int need_pc, struct dwarf2_cu *cu)
3820 {
3821 /* Add a symbol for the namespace. */
3822
3823 add_partial_symbol (pdi, cu);
3824
3825 /* Now scan partial symbols in that namespace. */
3826
3827 if (pdi->has_children)
3828 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3829 }
3830
3831 /* Read a partial die corresponding to a Fortran module. */
3832
3833 static void
3834 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3835 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3836 {
3837 /* Now scan partial symbols in that module. */
3838
3839 if (pdi->has_children)
3840 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3841 }
3842
3843 /* Read a partial die corresponding to a subprogram and create a partial
3844 symbol for that subprogram. When the CU language allows it, this
3845 routine also defines a partial symbol for each nested subprogram
3846 that this subprogram contains.
3847
3848 DIE my also be a lexical block, in which case we simply search
3849 recursively for suprograms defined inside that lexical block.
3850 Again, this is only performed when the CU language allows this
3851 type of definitions. */
3852
3853 static void
3854 add_partial_subprogram (struct partial_die_info *pdi,
3855 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3856 int need_pc, struct dwarf2_cu *cu)
3857 {
3858 if (pdi->tag == DW_TAG_subprogram)
3859 {
3860 if (pdi->has_pc_info)
3861 {
3862 if (pdi->lowpc < *lowpc)
3863 *lowpc = pdi->lowpc;
3864 if (pdi->highpc > *highpc)
3865 *highpc = pdi->highpc;
3866 if (need_pc)
3867 {
3868 CORE_ADDR baseaddr;
3869 struct objfile *objfile = cu->objfile;
3870
3871 baseaddr = ANOFFSET (objfile->section_offsets,
3872 SECT_OFF_TEXT (objfile));
3873 addrmap_set_empty (objfile->psymtabs_addrmap,
3874 pdi->lowpc + baseaddr,
3875 pdi->highpc - 1 + baseaddr,
3876 cu->per_cu->v.psymtab);
3877 }
3878 if (!pdi->is_declaration)
3879 /* Ignore subprogram DIEs that do not have a name, they are
3880 illegal. Do not emit a complaint at this point, we will
3881 do so when we convert this psymtab into a symtab. */
3882 if (pdi->name)
3883 add_partial_symbol (pdi, cu);
3884 }
3885 }
3886
3887 if (! pdi->has_children)
3888 return;
3889
3890 if (cu->language == language_ada)
3891 {
3892 pdi = pdi->die_child;
3893 while (pdi != NULL)
3894 {
3895 fixup_partial_die (pdi, cu);
3896 if (pdi->tag == DW_TAG_subprogram
3897 || pdi->tag == DW_TAG_lexical_block)
3898 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3899 pdi = pdi->die_sibling;
3900 }
3901 }
3902 }
3903
3904 /* See if we can figure out if the class lives in a namespace. We do
3905 this by looking for a member function; its demangled name will
3906 contain namespace info, if there is any. */
3907
3908 static void
3909 guess_structure_name (struct partial_die_info *struct_pdi,
3910 struct dwarf2_cu *cu)
3911 {
3912 if ((cu->language == language_cplus
3913 || cu->language == language_java)
3914 && cu->has_namespace_info == 0
3915 && struct_pdi->has_children)
3916 {
3917 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3918 what template types look like, because the demangler
3919 frequently doesn't give the same name as the debug info. We
3920 could fix this by only using the demangled name to get the
3921 prefix (but see comment in read_structure_type). */
3922
3923 struct partial_die_info *real_pdi;
3924
3925 /* If this DIE (this DIE's specification, if any) has a parent, then
3926 we should not do this. We'll prepend the parent's fully qualified
3927 name when we create the partial symbol. */
3928
3929 real_pdi = struct_pdi;
3930 while (real_pdi->has_specification)
3931 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3932
3933 if (real_pdi->die_parent != NULL)
3934 return;
3935 }
3936 }
3937
3938 /* Read a partial die corresponding to an enumeration type. */
3939
3940 static void
3941 add_partial_enumeration (struct partial_die_info *enum_pdi,
3942 struct dwarf2_cu *cu)
3943 {
3944 struct partial_die_info *pdi;
3945
3946 if (enum_pdi->name != NULL)
3947 add_partial_symbol (enum_pdi, cu);
3948
3949 pdi = enum_pdi->die_child;
3950 while (pdi)
3951 {
3952 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3953 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3954 else
3955 add_partial_symbol (pdi, cu);
3956 pdi = pdi->die_sibling;
3957 }
3958 }
3959
3960 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3961 Return the corresponding abbrev, or NULL if the number is zero (indicating
3962 an empty DIE). In either case *BYTES_READ will be set to the length of
3963 the initial number. */
3964
3965 static struct abbrev_info *
3966 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3967 struct dwarf2_cu *cu)
3968 {
3969 bfd *abfd = cu->objfile->obfd;
3970 unsigned int abbrev_number;
3971 struct abbrev_info *abbrev;
3972
3973 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3974
3975 if (abbrev_number == 0)
3976 return NULL;
3977
3978 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3979 if (!abbrev)
3980 {
3981 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3982 bfd_get_filename (abfd));
3983 }
3984
3985 return abbrev;
3986 }
3987
3988 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3989 Returns a pointer to the end of a series of DIEs, terminated by an empty
3990 DIE. Any children of the skipped DIEs will also be skipped. */
3991
3992 static gdb_byte *
3993 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3994 {
3995 struct abbrev_info *abbrev;
3996 unsigned int bytes_read;
3997
3998 while (1)
3999 {
4000 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4001 if (abbrev == NULL)
4002 return info_ptr + bytes_read;
4003 else
4004 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4005 }
4006 }
4007
4008 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4009 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4010 abbrev corresponding to that skipped uleb128 should be passed in
4011 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4012 children. */
4013
4014 static gdb_byte *
4015 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4016 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4017 {
4018 unsigned int bytes_read;
4019 struct attribute attr;
4020 bfd *abfd = cu->objfile->obfd;
4021 unsigned int form, i;
4022
4023 for (i = 0; i < abbrev->num_attrs; i++)
4024 {
4025 /* The only abbrev we care about is DW_AT_sibling. */
4026 if (abbrev->attrs[i].name == DW_AT_sibling)
4027 {
4028 read_attribute (&attr, &abbrev->attrs[i],
4029 abfd, info_ptr, cu);
4030 if (attr.form == DW_FORM_ref_addr)
4031 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4032 else
4033 return buffer + dwarf2_get_ref_die_offset (&attr);
4034 }
4035
4036 /* If it isn't DW_AT_sibling, skip this attribute. */
4037 form = abbrev->attrs[i].form;
4038 skip_attribute:
4039 switch (form)
4040 {
4041 case DW_FORM_ref_addr:
4042 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4043 and later it is offset sized. */
4044 if (cu->header.version == 2)
4045 info_ptr += cu->header.addr_size;
4046 else
4047 info_ptr += cu->header.offset_size;
4048 break;
4049 case DW_FORM_addr:
4050 info_ptr += cu->header.addr_size;
4051 break;
4052 case DW_FORM_data1:
4053 case DW_FORM_ref1:
4054 case DW_FORM_flag:
4055 info_ptr += 1;
4056 break;
4057 case DW_FORM_flag_present:
4058 break;
4059 case DW_FORM_data2:
4060 case DW_FORM_ref2:
4061 info_ptr += 2;
4062 break;
4063 case DW_FORM_data4:
4064 case DW_FORM_ref4:
4065 info_ptr += 4;
4066 break;
4067 case DW_FORM_data8:
4068 case DW_FORM_ref8:
4069 case DW_FORM_sig8:
4070 info_ptr += 8;
4071 break;
4072 case DW_FORM_string:
4073 read_direct_string (abfd, info_ptr, &bytes_read);
4074 info_ptr += bytes_read;
4075 break;
4076 case DW_FORM_sec_offset:
4077 case DW_FORM_strp:
4078 info_ptr += cu->header.offset_size;
4079 break;
4080 case DW_FORM_exprloc:
4081 case DW_FORM_block:
4082 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4083 info_ptr += bytes_read;
4084 break;
4085 case DW_FORM_block1:
4086 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4087 break;
4088 case DW_FORM_block2:
4089 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4090 break;
4091 case DW_FORM_block4:
4092 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4093 break;
4094 case DW_FORM_sdata:
4095 case DW_FORM_udata:
4096 case DW_FORM_ref_udata:
4097 info_ptr = skip_leb128 (abfd, info_ptr);
4098 break;
4099 case DW_FORM_indirect:
4100 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4101 info_ptr += bytes_read;
4102 /* We need to continue parsing from here, so just go back to
4103 the top. */
4104 goto skip_attribute;
4105
4106 default:
4107 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4108 dwarf_form_name (form),
4109 bfd_get_filename (abfd));
4110 }
4111 }
4112
4113 if (abbrev->has_children)
4114 return skip_children (buffer, info_ptr, cu);
4115 else
4116 return info_ptr;
4117 }
4118
4119 /* Locate ORIG_PDI's sibling.
4120 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4121 in BUFFER. */
4122
4123 static gdb_byte *
4124 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4125 gdb_byte *buffer, gdb_byte *info_ptr,
4126 bfd *abfd, struct dwarf2_cu *cu)
4127 {
4128 /* Do we know the sibling already? */
4129
4130 if (orig_pdi->sibling)
4131 return orig_pdi->sibling;
4132
4133 /* Are there any children to deal with? */
4134
4135 if (!orig_pdi->has_children)
4136 return info_ptr;
4137
4138 /* Skip the children the long way. */
4139
4140 return skip_children (buffer, info_ptr, cu);
4141 }
4142
4143 /* Expand this partial symbol table into a full symbol table. */
4144
4145 static void
4146 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4147 {
4148 if (pst != NULL)
4149 {
4150 if (pst->readin)
4151 {
4152 warning (_("bug: psymtab for %s is already read in."), pst->filename);
4153 }
4154 else
4155 {
4156 if (info_verbose)
4157 {
4158 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4159 gdb_flush (gdb_stdout);
4160 }
4161
4162 /* Restore our global data. */
4163 dwarf2_per_objfile = objfile_data (pst->objfile,
4164 dwarf2_objfile_data_key);
4165
4166 /* If this psymtab is constructed from a debug-only objfile, the
4167 has_section_at_zero flag will not necessarily be correct. We
4168 can get the correct value for this flag by looking at the data
4169 associated with the (presumably stripped) associated objfile. */
4170 if (pst->objfile->separate_debug_objfile_backlink)
4171 {
4172 struct dwarf2_per_objfile *dpo_backlink
4173 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4174 dwarf2_objfile_data_key);
4175
4176 dwarf2_per_objfile->has_section_at_zero
4177 = dpo_backlink->has_section_at_zero;
4178 }
4179
4180 dwarf2_per_objfile->reading_partial_symbols = 0;
4181
4182 psymtab_to_symtab_1 (pst);
4183
4184 /* Finish up the debug error message. */
4185 if (info_verbose)
4186 printf_filtered (_("done.\n"));
4187 }
4188 }
4189 }
4190
4191 /* Add PER_CU to the queue. */
4192
4193 static void
4194 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4195 {
4196 struct dwarf2_queue_item *item;
4197
4198 per_cu->queued = 1;
4199 item = xmalloc (sizeof (*item));
4200 item->per_cu = per_cu;
4201 item->next = NULL;
4202
4203 if (dwarf2_queue == NULL)
4204 dwarf2_queue = item;
4205 else
4206 dwarf2_queue_tail->next = item;
4207
4208 dwarf2_queue_tail = item;
4209 }
4210
4211 /* Process the queue. */
4212
4213 static void
4214 process_queue (struct objfile *objfile)
4215 {
4216 struct dwarf2_queue_item *item, *next_item;
4217
4218 /* The queue starts out with one item, but following a DIE reference
4219 may load a new CU, adding it to the end of the queue. */
4220 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4221 {
4222 if (dwarf2_per_objfile->using_index
4223 ? !item->per_cu->v.quick->symtab
4224 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4225 process_full_comp_unit (item->per_cu);
4226
4227 item->per_cu->queued = 0;
4228 next_item = item->next;
4229 xfree (item);
4230 }
4231
4232 dwarf2_queue_tail = NULL;
4233 }
4234
4235 /* Free all allocated queue entries. This function only releases anything if
4236 an error was thrown; if the queue was processed then it would have been
4237 freed as we went along. */
4238
4239 static void
4240 dwarf2_release_queue (void *dummy)
4241 {
4242 struct dwarf2_queue_item *item, *last;
4243
4244 item = dwarf2_queue;
4245 while (item)
4246 {
4247 /* Anything still marked queued is likely to be in an
4248 inconsistent state, so discard it. */
4249 if (item->per_cu->queued)
4250 {
4251 if (item->per_cu->cu != NULL)
4252 free_one_cached_comp_unit (item->per_cu->cu);
4253 item->per_cu->queued = 0;
4254 }
4255
4256 last = item;
4257 item = item->next;
4258 xfree (last);
4259 }
4260
4261 dwarf2_queue = dwarf2_queue_tail = NULL;
4262 }
4263
4264 /* Read in full symbols for PST, and anything it depends on. */
4265
4266 static void
4267 psymtab_to_symtab_1 (struct partial_symtab *pst)
4268 {
4269 struct dwarf2_per_cu_data *per_cu;
4270 struct cleanup *back_to;
4271 int i;
4272
4273 for (i = 0; i < pst->number_of_dependencies; i++)
4274 if (!pst->dependencies[i]->readin)
4275 {
4276 /* Inform about additional files that need to be read in. */
4277 if (info_verbose)
4278 {
4279 /* FIXME: i18n: Need to make this a single string. */
4280 fputs_filtered (" ", gdb_stdout);
4281 wrap_here ("");
4282 fputs_filtered ("and ", gdb_stdout);
4283 wrap_here ("");
4284 printf_filtered ("%s...", pst->dependencies[i]->filename);
4285 wrap_here (""); /* Flush output */
4286 gdb_flush (gdb_stdout);
4287 }
4288 psymtab_to_symtab_1 (pst->dependencies[i]);
4289 }
4290
4291 per_cu = pst->read_symtab_private;
4292
4293 if (per_cu == NULL)
4294 {
4295 /* It's an include file, no symbols to read for it.
4296 Everything is in the parent symtab. */
4297 pst->readin = 1;
4298 return;
4299 }
4300
4301 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4302 }
4303
4304 /* Load the DIEs associated with PER_CU into memory. */
4305
4306 static void
4307 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4308 {
4309 bfd *abfd = objfile->obfd;
4310 struct dwarf2_cu *cu;
4311 unsigned int offset;
4312 gdb_byte *info_ptr, *beg_of_comp_unit;
4313 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4314 struct attribute *attr;
4315 int read_cu = 0;
4316
4317 gdb_assert (! per_cu->from_debug_types);
4318
4319 /* Set local variables from the partial symbol table info. */
4320 offset = per_cu->offset;
4321
4322 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4323 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4324 beg_of_comp_unit = info_ptr;
4325
4326 if (per_cu->cu == NULL)
4327 {
4328 cu = alloc_one_comp_unit (objfile);
4329
4330 read_cu = 1;
4331
4332 /* If an error occurs while loading, release our storage. */
4333 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4334
4335 /* Read in the comp_unit header. */
4336 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4337
4338 /* Complete the cu_header. */
4339 cu->header.offset = offset;
4340 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4341
4342 /* Read the abbrevs for this compilation unit. */
4343 dwarf2_read_abbrevs (abfd, cu);
4344 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4345
4346 /* Link this compilation unit into the compilation unit tree. */
4347 per_cu->cu = cu;
4348 cu->per_cu = per_cu;
4349
4350 /* Link this CU into read_in_chain. */
4351 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4352 dwarf2_per_objfile->read_in_chain = per_cu;
4353 }
4354 else
4355 {
4356 cu = per_cu->cu;
4357 info_ptr += cu->header.first_die_offset;
4358 }
4359
4360 cu->dies = read_comp_unit (info_ptr, cu);
4361
4362 /* We try not to read any attributes in this function, because not
4363 all objfiles needed for references have been loaded yet, and symbol
4364 table processing isn't initialized. But we have to set the CU language,
4365 or we won't be able to build types correctly. */
4366 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4367 if (attr)
4368 set_cu_language (DW_UNSND (attr), cu);
4369 else
4370 set_cu_language (language_minimal, cu);
4371
4372 /* Similarly, if we do not read the producer, we can not apply
4373 producer-specific interpretation. */
4374 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4375 if (attr)
4376 cu->producer = DW_STRING (attr);
4377
4378 if (read_cu)
4379 {
4380 do_cleanups (free_abbrevs_cleanup);
4381
4382 /* We've successfully allocated this compilation unit. Let our
4383 caller clean it up when finished with it. */
4384 discard_cleanups (free_cu_cleanup);
4385 }
4386 }
4387
4388 /* Add a DIE to the delayed physname list. */
4389
4390 static void
4391 add_to_method_list (struct type *type, int fnfield_index, int index,
4392 const char *name, struct die_info *die,
4393 struct dwarf2_cu *cu)
4394 {
4395 struct delayed_method_info mi;
4396 mi.type = type;
4397 mi.fnfield_index = fnfield_index;
4398 mi.index = index;
4399 mi.name = name;
4400 mi.die = die;
4401 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4402 }
4403
4404 /* A cleanup for freeing the delayed method list. */
4405
4406 static void
4407 free_delayed_list (void *ptr)
4408 {
4409 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4410 if (cu->method_list != NULL)
4411 {
4412 VEC_free (delayed_method_info, cu->method_list);
4413 cu->method_list = NULL;
4414 }
4415 }
4416
4417 /* Compute the physnames of any methods on the CU's method list.
4418
4419 The computation of method physnames is delayed in order to avoid the
4420 (bad) condition that one of the method's formal parameters is of an as yet
4421 incomplete type. */
4422
4423 static void
4424 compute_delayed_physnames (struct dwarf2_cu *cu)
4425 {
4426 int i;
4427 struct delayed_method_info *mi;
4428 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4429 {
4430 char *physname;
4431 struct fn_fieldlist *fn_flp
4432 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4433 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4434 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4435 }
4436 }
4437
4438 /* Generate full symbol information for PST and CU, whose DIEs have
4439 already been loaded into memory. */
4440
4441 static void
4442 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4443 {
4444 struct dwarf2_cu *cu = per_cu->cu;
4445 struct objfile *objfile = per_cu->objfile;
4446 CORE_ADDR lowpc, highpc;
4447 struct symtab *symtab;
4448 struct cleanup *back_to, *delayed_list_cleanup;
4449 CORE_ADDR baseaddr;
4450
4451 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4452
4453 buildsym_init ();
4454 back_to = make_cleanup (really_free_pendings, NULL);
4455 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4456
4457 cu->list_in_scope = &file_symbols;
4458
4459 dwarf2_find_base_address (cu->dies, cu);
4460
4461 /* Do line number decoding in read_file_scope () */
4462 process_die (cu->dies, cu);
4463
4464 /* Now that we have processed all the DIEs in the CU, all the types
4465 should be complete, and it should now be safe to compute all of the
4466 physnames. */
4467 compute_delayed_physnames (cu);
4468 do_cleanups (delayed_list_cleanup);
4469
4470 /* Some compilers don't define a DW_AT_high_pc attribute for the
4471 compilation unit. If the DW_AT_high_pc is missing, synthesize
4472 it, by scanning the DIE's below the compilation unit. */
4473 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4474
4475 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4476
4477 /* Set symtab language to language from DW_AT_language.
4478 If the compilation is from a C file generated by language preprocessors,
4479 do not set the language if it was already deduced by start_subfile. */
4480 if (symtab != NULL
4481 && !(cu->language == language_c && symtab->language != language_c))
4482 {
4483 symtab->language = cu->language;
4484 }
4485
4486 if (dwarf2_per_objfile->using_index)
4487 per_cu->v.quick->symtab = symtab;
4488 else
4489 {
4490 struct partial_symtab *pst = per_cu->v.psymtab;
4491 pst->symtab = symtab;
4492 pst->readin = 1;
4493 }
4494
4495 do_cleanups (back_to);
4496 }
4497
4498 /* Process a die and its children. */
4499
4500 static void
4501 process_die (struct die_info *die, struct dwarf2_cu *cu)
4502 {
4503 switch (die->tag)
4504 {
4505 case DW_TAG_padding:
4506 break;
4507 case DW_TAG_compile_unit:
4508 read_file_scope (die, cu);
4509 break;
4510 case DW_TAG_type_unit:
4511 read_type_unit_scope (die, cu);
4512 break;
4513 case DW_TAG_subprogram:
4514 case DW_TAG_inlined_subroutine:
4515 read_func_scope (die, cu);
4516 break;
4517 case DW_TAG_lexical_block:
4518 case DW_TAG_try_block:
4519 case DW_TAG_catch_block:
4520 read_lexical_block_scope (die, cu);
4521 break;
4522 case DW_TAG_class_type:
4523 case DW_TAG_interface_type:
4524 case DW_TAG_structure_type:
4525 case DW_TAG_union_type:
4526 process_structure_scope (die, cu);
4527 break;
4528 case DW_TAG_enumeration_type:
4529 process_enumeration_scope (die, cu);
4530 break;
4531
4532 /* These dies have a type, but processing them does not create
4533 a symbol or recurse to process the children. Therefore we can
4534 read them on-demand through read_type_die. */
4535 case DW_TAG_subroutine_type:
4536 case DW_TAG_set_type:
4537 case DW_TAG_array_type:
4538 case DW_TAG_pointer_type:
4539 case DW_TAG_ptr_to_member_type:
4540 case DW_TAG_reference_type:
4541 case DW_TAG_string_type:
4542 break;
4543
4544 case DW_TAG_base_type:
4545 case DW_TAG_subrange_type:
4546 case DW_TAG_typedef:
4547 /* Add a typedef symbol for the type definition, if it has a
4548 DW_AT_name. */
4549 new_symbol (die, read_type_die (die, cu), cu);
4550 break;
4551 case DW_TAG_common_block:
4552 read_common_block (die, cu);
4553 break;
4554 case DW_TAG_common_inclusion:
4555 break;
4556 case DW_TAG_namespace:
4557 processing_has_namespace_info = 1;
4558 read_namespace (die, cu);
4559 break;
4560 case DW_TAG_module:
4561 processing_has_namespace_info = 1;
4562 read_module (die, cu);
4563 break;
4564 case DW_TAG_imported_declaration:
4565 case DW_TAG_imported_module:
4566 processing_has_namespace_info = 1;
4567 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4568 || cu->language != language_fortran))
4569 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4570 dwarf_tag_name (die->tag));
4571 read_import_statement (die, cu);
4572 break;
4573 default:
4574 new_symbol (die, NULL, cu);
4575 break;
4576 }
4577 }
4578
4579 /* A helper function for dwarf2_compute_name which determines whether DIE
4580 needs to have the name of the scope prepended to the name listed in the
4581 die. */
4582
4583 static int
4584 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4585 {
4586 struct attribute *attr;
4587
4588 switch (die->tag)
4589 {
4590 case DW_TAG_namespace:
4591 case DW_TAG_typedef:
4592 case DW_TAG_class_type:
4593 case DW_TAG_interface_type:
4594 case DW_TAG_structure_type:
4595 case DW_TAG_union_type:
4596 case DW_TAG_enumeration_type:
4597 case DW_TAG_enumerator:
4598 case DW_TAG_subprogram:
4599 case DW_TAG_member:
4600 return 1;
4601
4602 case DW_TAG_variable:
4603 /* We only need to prefix "globally" visible variables. These include
4604 any variable marked with DW_AT_external or any variable that
4605 lives in a namespace. [Variables in anonymous namespaces
4606 require prefixing, but they are not DW_AT_external.] */
4607
4608 if (dwarf2_attr (die, DW_AT_specification, cu))
4609 {
4610 struct dwarf2_cu *spec_cu = cu;
4611
4612 return die_needs_namespace (die_specification (die, &spec_cu),
4613 spec_cu);
4614 }
4615
4616 attr = dwarf2_attr (die, DW_AT_external, cu);
4617 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4618 && die->parent->tag != DW_TAG_module)
4619 return 0;
4620 /* A variable in a lexical block of some kind does not need a
4621 namespace, even though in C++ such variables may be external
4622 and have a mangled name. */
4623 if (die->parent->tag == DW_TAG_lexical_block
4624 || die->parent->tag == DW_TAG_try_block
4625 || die->parent->tag == DW_TAG_catch_block
4626 || die->parent->tag == DW_TAG_subprogram)
4627 return 0;
4628 return 1;
4629
4630 default:
4631 return 0;
4632 }
4633 }
4634
4635 /* Retrieve the last character from a mem_file. */
4636
4637 static void
4638 do_ui_file_peek_last (void *object, const char *buffer, long length)
4639 {
4640 char *last_char_p = (char *) object;
4641
4642 if (length > 0)
4643 *last_char_p = buffer[length - 1];
4644 }
4645
4646 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4647 compute the physname for the object, which include a method's
4648 formal parameters (C++/Java) and return type (Java).
4649
4650 For Ada, return the DIE's linkage name rather than the fully qualified
4651 name. PHYSNAME is ignored..
4652
4653 The result is allocated on the objfile_obstack and canonicalized. */
4654
4655 static const char *
4656 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4657 int physname)
4658 {
4659 if (name == NULL)
4660 name = dwarf2_name (die, cu);
4661
4662 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4663 compute it by typename_concat inside GDB. */
4664 if (cu->language == language_ada
4665 || (cu->language == language_fortran && physname))
4666 {
4667 /* For Ada unit, we prefer the linkage name over the name, as
4668 the former contains the exported name, which the user expects
4669 to be able to reference. Ideally, we want the user to be able
4670 to reference this entity using either natural or linkage name,
4671 but we haven't started looking at this enhancement yet. */
4672 struct attribute *attr;
4673
4674 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4675 if (attr == NULL)
4676 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4677 if (attr && DW_STRING (attr))
4678 return DW_STRING (attr);
4679 }
4680
4681 /* These are the only languages we know how to qualify names in. */
4682 if (name != NULL
4683 && (cu->language == language_cplus || cu->language == language_java
4684 || cu->language == language_fortran))
4685 {
4686 if (die_needs_namespace (die, cu))
4687 {
4688 long length;
4689 char *prefix;
4690 struct ui_file *buf;
4691
4692 prefix = determine_prefix (die, cu);
4693 buf = mem_fileopen ();
4694 if (*prefix != '\0')
4695 {
4696 char *prefixed_name = typename_concat (NULL, prefix, name,
4697 physname, cu);
4698
4699 fputs_unfiltered (prefixed_name, buf);
4700 xfree (prefixed_name);
4701 }
4702 else
4703 fputs_unfiltered (name ? name : "", buf);
4704
4705 /* Template parameters may be specified in the DIE's DW_AT_name, or
4706 as children with DW_TAG_template_type_param or
4707 DW_TAG_value_type_param. If the latter, add them to the name
4708 here. If the name already has template parameters, then
4709 skip this step; some versions of GCC emit both, and
4710 it is more efficient to use the pre-computed name.
4711
4712 Something to keep in mind about this process: it is very
4713 unlikely, or in some cases downright impossible, to produce
4714 something that will match the mangled name of a function.
4715 If the definition of the function has the same debug info,
4716 we should be able to match up with it anyway. But fallbacks
4717 using the minimal symbol, for instance to find a method
4718 implemented in a stripped copy of libstdc++, will not work.
4719 If we do not have debug info for the definition, we will have to
4720 match them up some other way.
4721
4722 When we do name matching there is a related problem with function
4723 templates; two instantiated function templates are allowed to
4724 differ only by their return types, which we do not add here. */
4725
4726 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4727 {
4728 struct attribute *attr;
4729 struct die_info *child;
4730 int first = 1;
4731
4732 die->building_fullname = 1;
4733
4734 for (child = die->child; child != NULL; child = child->sibling)
4735 {
4736 struct type *type;
4737 long value;
4738 gdb_byte *bytes;
4739 struct dwarf2_locexpr_baton *baton;
4740 struct value *v;
4741
4742 if (child->tag != DW_TAG_template_type_param
4743 && child->tag != DW_TAG_template_value_param)
4744 continue;
4745
4746 if (first)
4747 {
4748 fputs_unfiltered ("<", buf);
4749 first = 0;
4750 }
4751 else
4752 fputs_unfiltered (", ", buf);
4753
4754 attr = dwarf2_attr (child, DW_AT_type, cu);
4755 if (attr == NULL)
4756 {
4757 complaint (&symfile_complaints,
4758 _("template parameter missing DW_AT_type"));
4759 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4760 continue;
4761 }
4762 type = die_type (child, cu);
4763
4764 if (child->tag == DW_TAG_template_type_param)
4765 {
4766 c_print_type (type, "", buf, -1, 0);
4767 continue;
4768 }
4769
4770 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4771 if (attr == NULL)
4772 {
4773 complaint (&symfile_complaints,
4774 _("template parameter missing DW_AT_const_value"));
4775 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4776 continue;
4777 }
4778
4779 dwarf2_const_value_attr (attr, type, name,
4780 &cu->comp_unit_obstack, cu,
4781 &value, &bytes, &baton);
4782
4783 if (TYPE_NOSIGN (type))
4784 /* GDB prints characters as NUMBER 'CHAR'. If that's
4785 changed, this can use value_print instead. */
4786 c_printchar (value, type, buf);
4787 else
4788 {
4789 struct value_print_options opts;
4790
4791 if (baton != NULL)
4792 v = dwarf2_evaluate_loc_desc (type, NULL,
4793 baton->data,
4794 baton->size,
4795 baton->per_cu);
4796 else if (bytes != NULL)
4797 {
4798 v = allocate_value (type);
4799 memcpy (value_contents_writeable (v), bytes,
4800 TYPE_LENGTH (type));
4801 }
4802 else
4803 v = value_from_longest (type, value);
4804
4805 /* Specify decimal so that we do not depend on the radix. */
4806 get_formatted_print_options (&opts, 'd');
4807 opts.raw = 1;
4808 value_print (v, buf, &opts);
4809 release_value (v);
4810 value_free (v);
4811 }
4812 }
4813
4814 die->building_fullname = 0;
4815
4816 if (!first)
4817 {
4818 /* Close the argument list, with a space if necessary
4819 (nested templates). */
4820 char last_char = '\0';
4821 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4822 if (last_char == '>')
4823 fputs_unfiltered (" >", buf);
4824 else
4825 fputs_unfiltered (">", buf);
4826 }
4827 }
4828
4829 /* For Java and C++ methods, append formal parameter type
4830 information, if PHYSNAME. */
4831
4832 if (physname && die->tag == DW_TAG_subprogram
4833 && (cu->language == language_cplus
4834 || cu->language == language_java))
4835 {
4836 struct type *type = read_type_die (die, cu);
4837
4838 c_type_print_args (type, buf, 0, cu->language);
4839
4840 if (cu->language == language_java)
4841 {
4842 /* For java, we must append the return type to method
4843 names. */
4844 if (die->tag == DW_TAG_subprogram)
4845 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4846 0, 0);
4847 }
4848 else if (cu->language == language_cplus)
4849 {
4850 /* Assume that an artificial first parameter is
4851 "this", but do not crash if it is not. RealView
4852 marks unnamed (and thus unused) parameters as
4853 artificial; there is no way to differentiate
4854 the two cases. */
4855 if (TYPE_NFIELDS (type) > 0
4856 && TYPE_FIELD_ARTIFICIAL (type, 0)
4857 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
4858 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4859 fputs_unfiltered (" const", buf);
4860 }
4861 }
4862
4863 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4864 &length);
4865 ui_file_delete (buf);
4866
4867 if (cu->language == language_cplus)
4868 {
4869 char *cname
4870 = dwarf2_canonicalize_name (name, cu,
4871 &cu->objfile->objfile_obstack);
4872
4873 if (cname != NULL)
4874 name = cname;
4875 }
4876 }
4877 }
4878
4879 return name;
4880 }
4881
4882 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4883 If scope qualifiers are appropriate they will be added. The result
4884 will be allocated on the objfile_obstack, or NULL if the DIE does
4885 not have a name. NAME may either be from a previous call to
4886 dwarf2_name or NULL.
4887
4888 The output string will be canonicalized (if C++/Java). */
4889
4890 static const char *
4891 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4892 {
4893 return dwarf2_compute_name (name, die, cu, 0);
4894 }
4895
4896 /* Construct a physname for the given DIE in CU. NAME may either be
4897 from a previous call to dwarf2_name or NULL. The result will be
4898 allocated on the objfile_objstack or NULL if the DIE does not have a
4899 name.
4900
4901 The output string will be canonicalized (if C++/Java). */
4902
4903 static const char *
4904 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4905 {
4906 return dwarf2_compute_name (name, die, cu, 1);
4907 }
4908
4909 /* Read the import statement specified by the given die and record it. */
4910
4911 static void
4912 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4913 {
4914 struct attribute *import_attr;
4915 struct die_info *imported_die;
4916 struct dwarf2_cu *imported_cu;
4917 const char *imported_name;
4918 const char *imported_name_prefix;
4919 const char *canonical_name;
4920 const char *import_alias;
4921 const char *imported_declaration = NULL;
4922 const char *import_prefix;
4923
4924 char *temp;
4925
4926 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4927 if (import_attr == NULL)
4928 {
4929 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4930 dwarf_tag_name (die->tag));
4931 return;
4932 }
4933
4934 imported_cu = cu;
4935 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4936 imported_name = dwarf2_name (imported_die, imported_cu);
4937 if (imported_name == NULL)
4938 {
4939 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4940
4941 The import in the following code:
4942 namespace A
4943 {
4944 typedef int B;
4945 }
4946
4947 int main ()
4948 {
4949 using A::B;
4950 B b;
4951 return b;
4952 }
4953
4954 ...
4955 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4956 <52> DW_AT_decl_file : 1
4957 <53> DW_AT_decl_line : 6
4958 <54> DW_AT_import : <0x75>
4959 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4960 <59> DW_AT_name : B
4961 <5b> DW_AT_decl_file : 1
4962 <5c> DW_AT_decl_line : 2
4963 <5d> DW_AT_type : <0x6e>
4964 ...
4965 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4966 <76> DW_AT_byte_size : 4
4967 <77> DW_AT_encoding : 5 (signed)
4968
4969 imports the wrong die ( 0x75 instead of 0x58 ).
4970 This case will be ignored until the gcc bug is fixed. */
4971 return;
4972 }
4973
4974 /* Figure out the local name after import. */
4975 import_alias = dwarf2_name (die, cu);
4976
4977 /* Figure out where the statement is being imported to. */
4978 import_prefix = determine_prefix (die, cu);
4979
4980 /* Figure out what the scope of the imported die is and prepend it
4981 to the name of the imported die. */
4982 imported_name_prefix = determine_prefix (imported_die, imported_cu);
4983
4984 if (imported_die->tag != DW_TAG_namespace
4985 && imported_die->tag != DW_TAG_module)
4986 {
4987 imported_declaration = imported_name;
4988 canonical_name = imported_name_prefix;
4989 }
4990 else if (strlen (imported_name_prefix) > 0)
4991 {
4992 temp = alloca (strlen (imported_name_prefix)
4993 + 2 + strlen (imported_name) + 1);
4994 strcpy (temp, imported_name_prefix);
4995 strcat (temp, "::");
4996 strcat (temp, imported_name);
4997 canonical_name = temp;
4998 }
4999 else
5000 canonical_name = imported_name;
5001
5002 cp_add_using_directive (import_prefix,
5003 canonical_name,
5004 import_alias,
5005 imported_declaration,
5006 &cu->objfile->objfile_obstack);
5007 }
5008
5009 static void
5010 initialize_cu_func_list (struct dwarf2_cu *cu)
5011 {
5012 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5013 }
5014
5015 static void
5016 free_cu_line_header (void *arg)
5017 {
5018 struct dwarf2_cu *cu = arg;
5019
5020 free_line_header (cu->line_header);
5021 cu->line_header = NULL;
5022 }
5023
5024 static void
5025 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5026 char **name, char **comp_dir)
5027 {
5028 struct attribute *attr;
5029
5030 *name = NULL;
5031 *comp_dir = NULL;
5032
5033 /* Find the filename. Do not use dwarf2_name here, since the filename
5034 is not a source language identifier. */
5035 attr = dwarf2_attr (die, DW_AT_name, cu);
5036 if (attr)
5037 {
5038 *name = DW_STRING (attr);
5039 }
5040
5041 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5042 if (attr)
5043 *comp_dir = DW_STRING (attr);
5044 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5045 {
5046 *comp_dir = ldirname (*name);
5047 if (*comp_dir != NULL)
5048 make_cleanup (xfree, *comp_dir);
5049 }
5050 if (*comp_dir != NULL)
5051 {
5052 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5053 directory, get rid of it. */
5054 char *cp = strchr (*comp_dir, ':');
5055
5056 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5057 *comp_dir = cp + 1;
5058 }
5059
5060 if (*name == NULL)
5061 *name = "<unknown>";
5062 }
5063
5064 static void
5065 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5066 {
5067 struct objfile *objfile = cu->objfile;
5068 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5069 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5070 CORE_ADDR highpc = ((CORE_ADDR) 0);
5071 struct attribute *attr;
5072 char *name = NULL;
5073 char *comp_dir = NULL;
5074 struct die_info *child_die;
5075 bfd *abfd = objfile->obfd;
5076 struct line_header *line_header = 0;
5077 CORE_ADDR baseaddr;
5078
5079 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5080
5081 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5082
5083 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5084 from finish_block. */
5085 if (lowpc == ((CORE_ADDR) -1))
5086 lowpc = highpc;
5087 lowpc += baseaddr;
5088 highpc += baseaddr;
5089
5090 find_file_and_directory (die, cu, &name, &comp_dir);
5091
5092 attr = dwarf2_attr (die, DW_AT_language, cu);
5093 if (attr)
5094 {
5095 set_cu_language (DW_UNSND (attr), cu);
5096 }
5097
5098 attr = dwarf2_attr (die, DW_AT_producer, cu);
5099 if (attr)
5100 cu->producer = DW_STRING (attr);
5101
5102 /* We assume that we're processing GCC output. */
5103 processing_gcc_compilation = 2;
5104
5105 processing_has_namespace_info = 0;
5106
5107 start_symtab (name, comp_dir, lowpc);
5108 record_debugformat ("DWARF 2");
5109 record_producer (cu->producer);
5110
5111 initialize_cu_func_list (cu);
5112
5113 /* Decode line number information if present. We do this before
5114 processing child DIEs, so that the line header table is available
5115 for DW_AT_decl_file. */
5116 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5117 if (attr)
5118 {
5119 unsigned int line_offset = DW_UNSND (attr);
5120 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5121 if (line_header)
5122 {
5123 cu->line_header = line_header;
5124 make_cleanup (free_cu_line_header, cu);
5125 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5126 }
5127 }
5128
5129 /* Process all dies in compilation unit. */
5130 if (die->child != NULL)
5131 {
5132 child_die = die->child;
5133 while (child_die && child_die->tag)
5134 {
5135 process_die (child_die, cu);
5136 child_die = sibling_die (child_die);
5137 }
5138 }
5139
5140 /* Decode macro information, if present. Dwarf 2 macro information
5141 refers to information in the line number info statement program
5142 header, so we can only read it if we've read the header
5143 successfully. */
5144 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5145 if (attr && line_header)
5146 {
5147 unsigned int macro_offset = DW_UNSND (attr);
5148
5149 dwarf_decode_macros (line_header, macro_offset,
5150 comp_dir, abfd, cu);
5151 }
5152 do_cleanups (back_to);
5153 }
5154
5155 /* For TUs we want to skip the first top level sibling if it's not the
5156 actual type being defined by this TU. In this case the first top
5157 level sibling is there to provide context only. */
5158
5159 static void
5160 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5161 {
5162 struct objfile *objfile = cu->objfile;
5163 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5164 CORE_ADDR lowpc;
5165 struct attribute *attr;
5166 char *name = NULL;
5167 char *comp_dir = NULL;
5168 struct die_info *child_die;
5169 bfd *abfd = objfile->obfd;
5170
5171 /* start_symtab needs a low pc, but we don't really have one.
5172 Do what read_file_scope would do in the absence of such info. */
5173 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5174
5175 /* Find the filename. Do not use dwarf2_name here, since the filename
5176 is not a source language identifier. */
5177 attr = dwarf2_attr (die, DW_AT_name, cu);
5178 if (attr)
5179 name = DW_STRING (attr);
5180
5181 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5182 if (attr)
5183 comp_dir = DW_STRING (attr);
5184 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5185 {
5186 comp_dir = ldirname (name);
5187 if (comp_dir != NULL)
5188 make_cleanup (xfree, comp_dir);
5189 }
5190
5191 if (name == NULL)
5192 name = "<unknown>";
5193
5194 attr = dwarf2_attr (die, DW_AT_language, cu);
5195 if (attr)
5196 set_cu_language (DW_UNSND (attr), cu);
5197
5198 /* This isn't technically needed today. It is done for symmetry
5199 with read_file_scope. */
5200 attr = dwarf2_attr (die, DW_AT_producer, cu);
5201 if (attr)
5202 cu->producer = DW_STRING (attr);
5203
5204 /* We assume that we're processing GCC output. */
5205 processing_gcc_compilation = 2;
5206
5207 processing_has_namespace_info = 0;
5208
5209 start_symtab (name, comp_dir, lowpc);
5210 record_debugformat ("DWARF 2");
5211 record_producer (cu->producer);
5212
5213 /* Process the dies in the type unit. */
5214 if (die->child == NULL)
5215 {
5216 dump_die_for_error (die);
5217 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5218 bfd_get_filename (abfd));
5219 }
5220
5221 child_die = die->child;
5222
5223 while (child_die && child_die->tag)
5224 {
5225 process_die (child_die, cu);
5226
5227 child_die = sibling_die (child_die);
5228 }
5229
5230 do_cleanups (back_to);
5231 }
5232
5233 static void
5234 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5235 struct dwarf2_cu *cu)
5236 {
5237 struct function_range *thisfn;
5238
5239 thisfn = (struct function_range *)
5240 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5241 thisfn->name = name;
5242 thisfn->lowpc = lowpc;
5243 thisfn->highpc = highpc;
5244 thisfn->seen_line = 0;
5245 thisfn->next = NULL;
5246
5247 if (cu->last_fn == NULL)
5248 cu->first_fn = thisfn;
5249 else
5250 cu->last_fn->next = thisfn;
5251
5252 cu->last_fn = thisfn;
5253 }
5254
5255 /* qsort helper for inherit_abstract_dies. */
5256
5257 static int
5258 unsigned_int_compar (const void *ap, const void *bp)
5259 {
5260 unsigned int a = *(unsigned int *) ap;
5261 unsigned int b = *(unsigned int *) bp;
5262
5263 return (a > b) - (b > a);
5264 }
5265
5266 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5267 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5268 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5269
5270 static void
5271 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5272 {
5273 struct die_info *child_die;
5274 unsigned die_children_count;
5275 /* CU offsets which were referenced by children of the current DIE. */
5276 unsigned *offsets;
5277 unsigned *offsets_end, *offsetp;
5278 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5279 struct die_info *origin_die;
5280 /* Iterator of the ORIGIN_DIE children. */
5281 struct die_info *origin_child_die;
5282 struct cleanup *cleanups;
5283 struct attribute *attr;
5284 struct dwarf2_cu *origin_cu;
5285 struct pending **origin_previous_list_in_scope;
5286
5287 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5288 if (!attr)
5289 return;
5290
5291 /* Note that following die references may follow to a die in a
5292 different cu. */
5293
5294 origin_cu = cu;
5295 origin_die = follow_die_ref (die, attr, &origin_cu);
5296
5297 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5298 symbols in. */
5299 origin_previous_list_in_scope = origin_cu->list_in_scope;
5300 origin_cu->list_in_scope = cu->list_in_scope;
5301
5302 if (die->tag != origin_die->tag
5303 && !(die->tag == DW_TAG_inlined_subroutine
5304 && origin_die->tag == DW_TAG_subprogram))
5305 complaint (&symfile_complaints,
5306 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5307 die->offset, origin_die->offset);
5308
5309 child_die = die->child;
5310 die_children_count = 0;
5311 while (child_die && child_die->tag)
5312 {
5313 child_die = sibling_die (child_die);
5314 die_children_count++;
5315 }
5316 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5317 cleanups = make_cleanup (xfree, offsets);
5318
5319 offsets_end = offsets;
5320 child_die = die->child;
5321 while (child_die && child_die->tag)
5322 {
5323 /* For each CHILD_DIE, find the corresponding child of
5324 ORIGIN_DIE. If there is more than one layer of
5325 DW_AT_abstract_origin, follow them all; there shouldn't be,
5326 but GCC versions at least through 4.4 generate this (GCC PR
5327 40573). */
5328 struct die_info *child_origin_die = child_die;
5329 struct dwarf2_cu *child_origin_cu = cu;
5330
5331 while (1)
5332 {
5333 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5334 child_origin_cu);
5335 if (attr == NULL)
5336 break;
5337 child_origin_die = follow_die_ref (child_origin_die, attr,
5338 &child_origin_cu);
5339 }
5340
5341 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5342 counterpart may exist. */
5343 if (child_origin_die != child_die)
5344 {
5345 if (child_die->tag != child_origin_die->tag
5346 && !(child_die->tag == DW_TAG_inlined_subroutine
5347 && child_origin_die->tag == DW_TAG_subprogram))
5348 complaint (&symfile_complaints,
5349 _("Child DIE 0x%x and its abstract origin 0x%x have "
5350 "different tags"), child_die->offset,
5351 child_origin_die->offset);
5352 if (child_origin_die->parent != origin_die)
5353 complaint (&symfile_complaints,
5354 _("Child DIE 0x%x and its abstract origin 0x%x have "
5355 "different parents"), child_die->offset,
5356 child_origin_die->offset);
5357 else
5358 *offsets_end++ = child_origin_die->offset;
5359 }
5360 child_die = sibling_die (child_die);
5361 }
5362 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5363 unsigned_int_compar);
5364 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5365 if (offsetp[-1] == *offsetp)
5366 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5367 "to DIE 0x%x as their abstract origin"),
5368 die->offset, *offsetp);
5369
5370 offsetp = offsets;
5371 origin_child_die = origin_die->child;
5372 while (origin_child_die && origin_child_die->tag)
5373 {
5374 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5375 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5376 offsetp++;
5377 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5378 {
5379 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5380 process_die (origin_child_die, origin_cu);
5381 }
5382 origin_child_die = sibling_die (origin_child_die);
5383 }
5384 origin_cu->list_in_scope = origin_previous_list_in_scope;
5385
5386 do_cleanups (cleanups);
5387 }
5388
5389 static void
5390 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5391 {
5392 struct objfile *objfile = cu->objfile;
5393 struct context_stack *new;
5394 CORE_ADDR lowpc;
5395 CORE_ADDR highpc;
5396 struct die_info *child_die;
5397 struct attribute *attr, *call_line, *call_file;
5398 char *name;
5399 CORE_ADDR baseaddr;
5400 struct block *block;
5401 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5402 VEC (symbolp) *template_args = NULL;
5403 struct template_symbol *templ_func = NULL;
5404
5405 if (inlined_func)
5406 {
5407 /* If we do not have call site information, we can't show the
5408 caller of this inlined function. That's too confusing, so
5409 only use the scope for local variables. */
5410 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5411 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5412 if (call_line == NULL || call_file == NULL)
5413 {
5414 read_lexical_block_scope (die, cu);
5415 return;
5416 }
5417 }
5418
5419 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5420
5421 name = dwarf2_name (die, cu);
5422
5423 /* Ignore functions with missing or empty names. These are actually
5424 illegal according to the DWARF standard. */
5425 if (name == NULL)
5426 {
5427 complaint (&symfile_complaints,
5428 _("missing name for subprogram DIE at %d"), die->offset);
5429 return;
5430 }
5431
5432 /* Ignore functions with missing or invalid low and high pc attributes. */
5433 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5434 {
5435 attr = dwarf2_attr (die, DW_AT_external, cu);
5436 if (!attr || !DW_UNSND (attr))
5437 complaint (&symfile_complaints,
5438 _("cannot get low and high bounds for subprogram DIE at %d"),
5439 die->offset);
5440 return;
5441 }
5442
5443 lowpc += baseaddr;
5444 highpc += baseaddr;
5445
5446 /* Record the function range for dwarf_decode_lines. */
5447 add_to_cu_func_list (name, lowpc, highpc, cu);
5448
5449 /* If we have any template arguments, then we must allocate a
5450 different sort of symbol. */
5451 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5452 {
5453 if (child_die->tag == DW_TAG_template_type_param
5454 || child_die->tag == DW_TAG_template_value_param)
5455 {
5456 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5457 struct template_symbol);
5458 templ_func->base.is_cplus_template_function = 1;
5459 break;
5460 }
5461 }
5462
5463 new = push_context (0, lowpc);
5464 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5465 (struct symbol *) templ_func);
5466
5467 /* If there is a location expression for DW_AT_frame_base, record
5468 it. */
5469 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5470 if (attr)
5471 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5472 expression is being recorded directly in the function's symbol
5473 and not in a separate frame-base object. I guess this hack is
5474 to avoid adding some sort of frame-base adjunct/annex to the
5475 function's symbol :-(. The problem with doing this is that it
5476 results in a function symbol with a location expression that
5477 has nothing to do with the location of the function, ouch! The
5478 relationship should be: a function's symbol has-a frame base; a
5479 frame-base has-a location expression. */
5480 dwarf2_symbol_mark_computed (attr, new->name, cu);
5481
5482 cu->list_in_scope = &local_symbols;
5483
5484 if (die->child != NULL)
5485 {
5486 child_die = die->child;
5487 while (child_die && child_die->tag)
5488 {
5489 if (child_die->tag == DW_TAG_template_type_param
5490 || child_die->tag == DW_TAG_template_value_param)
5491 {
5492 struct symbol *arg = new_symbol (child_die, NULL, cu);
5493
5494 if (arg != NULL)
5495 VEC_safe_push (symbolp, template_args, arg);
5496 }
5497 else
5498 process_die (child_die, cu);
5499 child_die = sibling_die (child_die);
5500 }
5501 }
5502
5503 inherit_abstract_dies (die, cu);
5504
5505 /* If we have a DW_AT_specification, we might need to import using
5506 directives from the context of the specification DIE. See the
5507 comment in determine_prefix. */
5508 if (cu->language == language_cplus
5509 && dwarf2_attr (die, DW_AT_specification, cu))
5510 {
5511 struct dwarf2_cu *spec_cu = cu;
5512 struct die_info *spec_die = die_specification (die, &spec_cu);
5513
5514 while (spec_die)
5515 {
5516 child_die = spec_die->child;
5517 while (child_die && child_die->tag)
5518 {
5519 if (child_die->tag == DW_TAG_imported_module)
5520 process_die (child_die, spec_cu);
5521 child_die = sibling_die (child_die);
5522 }
5523
5524 /* In some cases, GCC generates specification DIEs that
5525 themselves contain DW_AT_specification attributes. */
5526 spec_die = die_specification (spec_die, &spec_cu);
5527 }
5528 }
5529
5530 new = pop_context ();
5531 /* Make a block for the local symbols within. */
5532 block = finish_block (new->name, &local_symbols, new->old_blocks,
5533 lowpc, highpc, objfile);
5534
5535 /* For C++, set the block's scope. */
5536 if (cu->language == language_cplus || cu->language == language_fortran)
5537 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5538 determine_prefix (die, cu),
5539 processing_has_namespace_info);
5540
5541 /* If we have address ranges, record them. */
5542 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5543
5544 /* Attach template arguments to function. */
5545 if (! VEC_empty (symbolp, template_args))
5546 {
5547 gdb_assert (templ_func != NULL);
5548
5549 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5550 templ_func->template_arguments
5551 = obstack_alloc (&objfile->objfile_obstack,
5552 (templ_func->n_template_arguments
5553 * sizeof (struct symbol *)));
5554 memcpy (templ_func->template_arguments,
5555 VEC_address (symbolp, template_args),
5556 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5557 VEC_free (symbolp, template_args);
5558 }
5559
5560 /* In C++, we can have functions nested inside functions (e.g., when
5561 a function declares a class that has methods). This means that
5562 when we finish processing a function scope, we may need to go
5563 back to building a containing block's symbol lists. */
5564 local_symbols = new->locals;
5565 param_symbols = new->params;
5566 using_directives = new->using_directives;
5567
5568 /* If we've finished processing a top-level function, subsequent
5569 symbols go in the file symbol list. */
5570 if (outermost_context_p ())
5571 cu->list_in_scope = &file_symbols;
5572 }
5573
5574 /* Process all the DIES contained within a lexical block scope. Start
5575 a new scope, process the dies, and then close the scope. */
5576
5577 static void
5578 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5579 {
5580 struct objfile *objfile = cu->objfile;
5581 struct context_stack *new;
5582 CORE_ADDR lowpc, highpc;
5583 struct die_info *child_die;
5584 CORE_ADDR baseaddr;
5585
5586 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5587
5588 /* Ignore blocks with missing or invalid low and high pc attributes. */
5589 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5590 as multiple lexical blocks? Handling children in a sane way would
5591 be nasty. Might be easier to properly extend generic blocks to
5592 describe ranges. */
5593 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5594 return;
5595 lowpc += baseaddr;
5596 highpc += baseaddr;
5597
5598 push_context (0, lowpc);
5599 if (die->child != NULL)
5600 {
5601 child_die = die->child;
5602 while (child_die && child_die->tag)
5603 {
5604 process_die (child_die, cu);
5605 child_die = sibling_die (child_die);
5606 }
5607 }
5608 new = pop_context ();
5609
5610 if (local_symbols != NULL || using_directives != NULL)
5611 {
5612 struct block *block
5613 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5614 highpc, objfile);
5615
5616 /* Note that recording ranges after traversing children, as we
5617 do here, means that recording a parent's ranges entails
5618 walking across all its children's ranges as they appear in
5619 the address map, which is quadratic behavior.
5620
5621 It would be nicer to record the parent's ranges before
5622 traversing its children, simply overriding whatever you find
5623 there. But since we don't even decide whether to create a
5624 block until after we've traversed its children, that's hard
5625 to do. */
5626 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5627 }
5628 local_symbols = new->locals;
5629 using_directives = new->using_directives;
5630 }
5631
5632 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5633 Return 1 if the attributes are present and valid, otherwise, return 0.
5634 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
5635
5636 static int
5637 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5638 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5639 struct partial_symtab *ranges_pst)
5640 {
5641 struct objfile *objfile = cu->objfile;
5642 struct comp_unit_head *cu_header = &cu->header;
5643 bfd *obfd = objfile->obfd;
5644 unsigned int addr_size = cu_header->addr_size;
5645 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5646 /* Base address selection entry. */
5647 CORE_ADDR base;
5648 int found_base;
5649 unsigned int dummy;
5650 gdb_byte *buffer;
5651 CORE_ADDR marker;
5652 int low_set;
5653 CORE_ADDR low = 0;
5654 CORE_ADDR high = 0;
5655 CORE_ADDR baseaddr;
5656
5657 found_base = cu->base_known;
5658 base = cu->base_address;
5659
5660 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5661 if (offset >= dwarf2_per_objfile->ranges.size)
5662 {
5663 complaint (&symfile_complaints,
5664 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5665 offset);
5666 return 0;
5667 }
5668 buffer = dwarf2_per_objfile->ranges.buffer + offset;
5669
5670 /* Read in the largest possible address. */
5671 marker = read_address (obfd, buffer, cu, &dummy);
5672 if ((marker & mask) == mask)
5673 {
5674 /* If we found the largest possible address, then
5675 read the base address. */
5676 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5677 buffer += 2 * addr_size;
5678 offset += 2 * addr_size;
5679 found_base = 1;
5680 }
5681
5682 low_set = 0;
5683
5684 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5685
5686 while (1)
5687 {
5688 CORE_ADDR range_beginning, range_end;
5689
5690 range_beginning = read_address (obfd, buffer, cu, &dummy);
5691 buffer += addr_size;
5692 range_end = read_address (obfd, buffer, cu, &dummy);
5693 buffer += addr_size;
5694 offset += 2 * addr_size;
5695
5696 /* An end of list marker is a pair of zero addresses. */
5697 if (range_beginning == 0 && range_end == 0)
5698 /* Found the end of list entry. */
5699 break;
5700
5701 /* Each base address selection entry is a pair of 2 values.
5702 The first is the largest possible address, the second is
5703 the base address. Check for a base address here. */
5704 if ((range_beginning & mask) == mask)
5705 {
5706 /* If we found the largest possible address, then
5707 read the base address. */
5708 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5709 found_base = 1;
5710 continue;
5711 }
5712
5713 if (!found_base)
5714 {
5715 /* We have no valid base address for the ranges
5716 data. */
5717 complaint (&symfile_complaints,
5718 _("Invalid .debug_ranges data (no base address)"));
5719 return 0;
5720 }
5721
5722 range_beginning += base;
5723 range_end += base;
5724
5725 if (ranges_pst != NULL && range_beginning < range_end)
5726 addrmap_set_empty (objfile->psymtabs_addrmap,
5727 range_beginning + baseaddr, range_end - 1 + baseaddr,
5728 ranges_pst);
5729
5730 /* FIXME: This is recording everything as a low-high
5731 segment of consecutive addresses. We should have a
5732 data structure for discontiguous block ranges
5733 instead. */
5734 if (! low_set)
5735 {
5736 low = range_beginning;
5737 high = range_end;
5738 low_set = 1;
5739 }
5740 else
5741 {
5742 if (range_beginning < low)
5743 low = range_beginning;
5744 if (range_end > high)
5745 high = range_end;
5746 }
5747 }
5748
5749 if (! low_set)
5750 /* If the first entry is an end-of-list marker, the range
5751 describes an empty scope, i.e. no instructions. */
5752 return 0;
5753
5754 if (low_return)
5755 *low_return = low;
5756 if (high_return)
5757 *high_return = high;
5758 return 1;
5759 }
5760
5761 /* Get low and high pc attributes from a die. Return 1 if the attributes
5762 are present and valid, otherwise, return 0. Return -1 if the range is
5763 discontinuous, i.e. derived from DW_AT_ranges information. */
5764 static int
5765 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5766 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5767 struct partial_symtab *pst)
5768 {
5769 struct attribute *attr;
5770 CORE_ADDR low = 0;
5771 CORE_ADDR high = 0;
5772 int ret = 0;
5773
5774 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5775 if (attr)
5776 {
5777 high = DW_ADDR (attr);
5778 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5779 if (attr)
5780 low = DW_ADDR (attr);
5781 else
5782 /* Found high w/o low attribute. */
5783 return 0;
5784
5785 /* Found consecutive range of addresses. */
5786 ret = 1;
5787 }
5788 else
5789 {
5790 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5791 if (attr != NULL)
5792 {
5793 /* Value of the DW_AT_ranges attribute is the offset in the
5794 .debug_ranges section. */
5795 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5796 return 0;
5797 /* Found discontinuous range of addresses. */
5798 ret = -1;
5799 }
5800 }
5801
5802 if (high < low)
5803 return 0;
5804
5805 /* When using the GNU linker, .gnu.linkonce. sections are used to
5806 eliminate duplicate copies of functions and vtables and such.
5807 The linker will arbitrarily choose one and discard the others.
5808 The AT_*_pc values for such functions refer to local labels in
5809 these sections. If the section from that file was discarded, the
5810 labels are not in the output, so the relocs get a value of 0.
5811 If this is a discarded function, mark the pc bounds as invalid,
5812 so that GDB will ignore it. */
5813 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5814 return 0;
5815
5816 *lowpc = low;
5817 *highpc = high;
5818 return ret;
5819 }
5820
5821 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5822 its low and high PC addresses. Do nothing if these addresses could not
5823 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5824 and HIGHPC to the high address if greater than HIGHPC. */
5825
5826 static void
5827 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5828 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5829 struct dwarf2_cu *cu)
5830 {
5831 CORE_ADDR low, high;
5832 struct die_info *child = die->child;
5833
5834 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5835 {
5836 *lowpc = min (*lowpc, low);
5837 *highpc = max (*highpc, high);
5838 }
5839
5840 /* If the language does not allow nested subprograms (either inside
5841 subprograms or lexical blocks), we're done. */
5842 if (cu->language != language_ada)
5843 return;
5844
5845 /* Check all the children of the given DIE. If it contains nested
5846 subprograms, then check their pc bounds. Likewise, we need to
5847 check lexical blocks as well, as they may also contain subprogram
5848 definitions. */
5849 while (child && child->tag)
5850 {
5851 if (child->tag == DW_TAG_subprogram
5852 || child->tag == DW_TAG_lexical_block)
5853 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5854 child = sibling_die (child);
5855 }
5856 }
5857
5858 /* Get the low and high pc's represented by the scope DIE, and store
5859 them in *LOWPC and *HIGHPC. If the correct values can't be
5860 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5861
5862 static void
5863 get_scope_pc_bounds (struct die_info *die,
5864 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5865 struct dwarf2_cu *cu)
5866 {
5867 CORE_ADDR best_low = (CORE_ADDR) -1;
5868 CORE_ADDR best_high = (CORE_ADDR) 0;
5869 CORE_ADDR current_low, current_high;
5870
5871 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5872 {
5873 best_low = current_low;
5874 best_high = current_high;
5875 }
5876 else
5877 {
5878 struct die_info *child = die->child;
5879
5880 while (child && child->tag)
5881 {
5882 switch (child->tag) {
5883 case DW_TAG_subprogram:
5884 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5885 break;
5886 case DW_TAG_namespace:
5887 case DW_TAG_module:
5888 /* FIXME: carlton/2004-01-16: Should we do this for
5889 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5890 that current GCC's always emit the DIEs corresponding
5891 to definitions of methods of classes as children of a
5892 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5893 the DIEs giving the declarations, which could be
5894 anywhere). But I don't see any reason why the
5895 standards says that they have to be there. */
5896 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5897
5898 if (current_low != ((CORE_ADDR) -1))
5899 {
5900 best_low = min (best_low, current_low);
5901 best_high = max (best_high, current_high);
5902 }
5903 break;
5904 default:
5905 /* Ignore. */
5906 break;
5907 }
5908
5909 child = sibling_die (child);
5910 }
5911 }
5912
5913 *lowpc = best_low;
5914 *highpc = best_high;
5915 }
5916
5917 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5918 in DIE. */
5919 static void
5920 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5921 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5922 {
5923 struct attribute *attr;
5924
5925 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5926 if (attr)
5927 {
5928 CORE_ADDR high = DW_ADDR (attr);
5929
5930 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5931 if (attr)
5932 {
5933 CORE_ADDR low = DW_ADDR (attr);
5934
5935 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5936 }
5937 }
5938
5939 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5940 if (attr)
5941 {
5942 bfd *obfd = cu->objfile->obfd;
5943
5944 /* The value of the DW_AT_ranges attribute is the offset of the
5945 address range list in the .debug_ranges section. */
5946 unsigned long offset = DW_UNSND (attr);
5947 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5948
5949 /* For some target architectures, but not others, the
5950 read_address function sign-extends the addresses it returns.
5951 To recognize base address selection entries, we need a
5952 mask. */
5953 unsigned int addr_size = cu->header.addr_size;
5954 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5955
5956 /* The base address, to which the next pair is relative. Note
5957 that this 'base' is a DWARF concept: most entries in a range
5958 list are relative, to reduce the number of relocs against the
5959 debugging information. This is separate from this function's
5960 'baseaddr' argument, which GDB uses to relocate debugging
5961 information from a shared library based on the address at
5962 which the library was loaded. */
5963 CORE_ADDR base = cu->base_address;
5964 int base_known = cu->base_known;
5965
5966 gdb_assert (dwarf2_per_objfile->ranges.readin);
5967 if (offset >= dwarf2_per_objfile->ranges.size)
5968 {
5969 complaint (&symfile_complaints,
5970 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5971 offset);
5972 return;
5973 }
5974
5975 for (;;)
5976 {
5977 unsigned int bytes_read;
5978 CORE_ADDR start, end;
5979
5980 start = read_address (obfd, buffer, cu, &bytes_read);
5981 buffer += bytes_read;
5982 end = read_address (obfd, buffer, cu, &bytes_read);
5983 buffer += bytes_read;
5984
5985 /* Did we find the end of the range list? */
5986 if (start == 0 && end == 0)
5987 break;
5988
5989 /* Did we find a base address selection entry? */
5990 else if ((start & base_select_mask) == base_select_mask)
5991 {
5992 base = end;
5993 base_known = 1;
5994 }
5995
5996 /* We found an ordinary address range. */
5997 else
5998 {
5999 if (!base_known)
6000 {
6001 complaint (&symfile_complaints,
6002 _("Invalid .debug_ranges data (no base address)"));
6003 return;
6004 }
6005
6006 record_block_range (block,
6007 baseaddr + base + start,
6008 baseaddr + base + end - 1);
6009 }
6010 }
6011 }
6012 }
6013
6014 /* Add an aggregate field to the field list. */
6015
6016 static void
6017 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6018 struct dwarf2_cu *cu)
6019 {
6020 struct objfile *objfile = cu->objfile;
6021 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6022 struct nextfield *new_field;
6023 struct attribute *attr;
6024 struct field *fp;
6025 char *fieldname = "";
6026
6027 /* Allocate a new field list entry and link it in. */
6028 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6029 make_cleanup (xfree, new_field);
6030 memset (new_field, 0, sizeof (struct nextfield));
6031
6032 if (die->tag == DW_TAG_inheritance)
6033 {
6034 new_field->next = fip->baseclasses;
6035 fip->baseclasses = new_field;
6036 }
6037 else
6038 {
6039 new_field->next = fip->fields;
6040 fip->fields = new_field;
6041 }
6042 fip->nfields++;
6043
6044 /* Handle accessibility and virtuality of field.
6045 The default accessibility for members is public, the default
6046 accessibility for inheritance is private. */
6047 if (die->tag != DW_TAG_inheritance)
6048 new_field->accessibility = DW_ACCESS_public;
6049 else
6050 new_field->accessibility = DW_ACCESS_private;
6051 new_field->virtuality = DW_VIRTUALITY_none;
6052
6053 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6054 if (attr)
6055 new_field->accessibility = DW_UNSND (attr);
6056 if (new_field->accessibility != DW_ACCESS_public)
6057 fip->non_public_fields = 1;
6058 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6059 if (attr)
6060 new_field->virtuality = DW_UNSND (attr);
6061
6062 fp = &new_field->field;
6063
6064 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6065 {
6066 /* Data member other than a C++ static data member. */
6067
6068 /* Get type of field. */
6069 fp->type = die_type (die, cu);
6070
6071 SET_FIELD_BITPOS (*fp, 0);
6072
6073 /* Get bit size of field (zero if none). */
6074 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6075 if (attr)
6076 {
6077 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6078 }
6079 else
6080 {
6081 FIELD_BITSIZE (*fp) = 0;
6082 }
6083
6084 /* Get bit offset of field. */
6085 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6086 if (attr)
6087 {
6088 int byte_offset = 0;
6089
6090 if (attr_form_is_section_offset (attr))
6091 dwarf2_complex_location_expr_complaint ();
6092 else if (attr_form_is_constant (attr))
6093 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6094 else if (attr_form_is_block (attr))
6095 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6096 else
6097 dwarf2_complex_location_expr_complaint ();
6098
6099 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6100 }
6101 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6102 if (attr)
6103 {
6104 if (gdbarch_bits_big_endian (gdbarch))
6105 {
6106 /* For big endian bits, the DW_AT_bit_offset gives the
6107 additional bit offset from the MSB of the containing
6108 anonymous object to the MSB of the field. We don't
6109 have to do anything special since we don't need to
6110 know the size of the anonymous object. */
6111 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6112 }
6113 else
6114 {
6115 /* For little endian bits, compute the bit offset to the
6116 MSB of the anonymous object, subtract off the number of
6117 bits from the MSB of the field to the MSB of the
6118 object, and then subtract off the number of bits of
6119 the field itself. The result is the bit offset of
6120 the LSB of the field. */
6121 int anonymous_size;
6122 int bit_offset = DW_UNSND (attr);
6123
6124 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6125 if (attr)
6126 {
6127 /* The size of the anonymous object containing
6128 the bit field is explicit, so use the
6129 indicated size (in bytes). */
6130 anonymous_size = DW_UNSND (attr);
6131 }
6132 else
6133 {
6134 /* The size of the anonymous object containing
6135 the bit field must be inferred from the type
6136 attribute of the data member containing the
6137 bit field. */
6138 anonymous_size = TYPE_LENGTH (fp->type);
6139 }
6140 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6141 - bit_offset - FIELD_BITSIZE (*fp);
6142 }
6143 }
6144
6145 /* Get name of field. */
6146 fieldname = dwarf2_name (die, cu);
6147 if (fieldname == NULL)
6148 fieldname = "";
6149
6150 /* The name is already allocated along with this objfile, so we don't
6151 need to duplicate it for the type. */
6152 fp->name = fieldname;
6153
6154 /* Change accessibility for artificial fields (e.g. virtual table
6155 pointer or virtual base class pointer) to private. */
6156 if (dwarf2_attr (die, DW_AT_artificial, cu))
6157 {
6158 FIELD_ARTIFICIAL (*fp) = 1;
6159 new_field->accessibility = DW_ACCESS_private;
6160 fip->non_public_fields = 1;
6161 }
6162 }
6163 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6164 {
6165 /* C++ static member. */
6166
6167 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6168 is a declaration, but all versions of G++ as of this writing
6169 (so through at least 3.2.1) incorrectly generate
6170 DW_TAG_variable tags. */
6171
6172 char *physname;
6173
6174 /* Get name of field. */
6175 fieldname = dwarf2_name (die, cu);
6176 if (fieldname == NULL)
6177 return;
6178
6179 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6180 if (attr
6181 /* Only create a symbol if this is an external value.
6182 new_symbol checks this and puts the value in the global symbol
6183 table, which we want. If it is not external, new_symbol
6184 will try to put the value in cu->list_in_scope which is wrong. */
6185 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6186 {
6187 /* A static const member, not much different than an enum as far as
6188 we're concerned, except that we can support more types. */
6189 new_symbol (die, NULL, cu);
6190 }
6191
6192 /* Get physical name. */
6193 physname = (char *) dwarf2_physname (fieldname, die, cu);
6194
6195 /* The name is already allocated along with this objfile, so we don't
6196 need to duplicate it for the type. */
6197 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6198 FIELD_TYPE (*fp) = die_type (die, cu);
6199 FIELD_NAME (*fp) = fieldname;
6200 }
6201 else if (die->tag == DW_TAG_inheritance)
6202 {
6203 /* C++ base class field. */
6204 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6205 if (attr)
6206 {
6207 int byte_offset = 0;
6208
6209 if (attr_form_is_section_offset (attr))
6210 dwarf2_complex_location_expr_complaint ();
6211 else if (attr_form_is_constant (attr))
6212 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6213 else if (attr_form_is_block (attr))
6214 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6215 else
6216 dwarf2_complex_location_expr_complaint ();
6217
6218 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6219 }
6220 FIELD_BITSIZE (*fp) = 0;
6221 FIELD_TYPE (*fp) = die_type (die, cu);
6222 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6223 fip->nbaseclasses++;
6224 }
6225 }
6226
6227 /* Add a typedef defined in the scope of the FIP's class. */
6228
6229 static void
6230 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6231 struct dwarf2_cu *cu)
6232 {
6233 struct objfile *objfile = cu->objfile;
6234 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6235 struct typedef_field_list *new_field;
6236 struct attribute *attr;
6237 struct typedef_field *fp;
6238 char *fieldname = "";
6239
6240 /* Allocate a new field list entry and link it in. */
6241 new_field = xzalloc (sizeof (*new_field));
6242 make_cleanup (xfree, new_field);
6243
6244 gdb_assert (die->tag == DW_TAG_typedef);
6245
6246 fp = &new_field->field;
6247
6248 /* Get name of field. */
6249 fp->name = dwarf2_name (die, cu);
6250 if (fp->name == NULL)
6251 return;
6252
6253 fp->type = read_type_die (die, cu);
6254
6255 new_field->next = fip->typedef_field_list;
6256 fip->typedef_field_list = new_field;
6257 fip->typedef_field_list_count++;
6258 }
6259
6260 /* Create the vector of fields, and attach it to the type. */
6261
6262 static void
6263 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6264 struct dwarf2_cu *cu)
6265 {
6266 int nfields = fip->nfields;
6267
6268 /* Record the field count, allocate space for the array of fields,
6269 and create blank accessibility bitfields if necessary. */
6270 TYPE_NFIELDS (type) = nfields;
6271 TYPE_FIELDS (type) = (struct field *)
6272 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6273 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6274
6275 if (fip->non_public_fields && cu->language != language_ada)
6276 {
6277 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6278
6279 TYPE_FIELD_PRIVATE_BITS (type) =
6280 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6281 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6282
6283 TYPE_FIELD_PROTECTED_BITS (type) =
6284 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6285 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6286
6287 TYPE_FIELD_IGNORE_BITS (type) =
6288 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6289 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6290 }
6291
6292 /* If the type has baseclasses, allocate and clear a bit vector for
6293 TYPE_FIELD_VIRTUAL_BITS. */
6294 if (fip->nbaseclasses && cu->language != language_ada)
6295 {
6296 int num_bytes = B_BYTES (fip->nbaseclasses);
6297 unsigned char *pointer;
6298
6299 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6300 pointer = TYPE_ALLOC (type, num_bytes);
6301 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6302 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6303 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6304 }
6305
6306 /* Copy the saved-up fields into the field vector. Start from the head
6307 of the list, adding to the tail of the field array, so that they end
6308 up in the same order in the array in which they were added to the list. */
6309 while (nfields-- > 0)
6310 {
6311 struct nextfield *fieldp;
6312
6313 if (fip->fields)
6314 {
6315 fieldp = fip->fields;
6316 fip->fields = fieldp->next;
6317 }
6318 else
6319 {
6320 fieldp = fip->baseclasses;
6321 fip->baseclasses = fieldp->next;
6322 }
6323
6324 TYPE_FIELD (type, nfields) = fieldp->field;
6325 switch (fieldp->accessibility)
6326 {
6327 case DW_ACCESS_private:
6328 if (cu->language != language_ada)
6329 SET_TYPE_FIELD_PRIVATE (type, nfields);
6330 break;
6331
6332 case DW_ACCESS_protected:
6333 if (cu->language != language_ada)
6334 SET_TYPE_FIELD_PROTECTED (type, nfields);
6335 break;
6336
6337 case DW_ACCESS_public:
6338 break;
6339
6340 default:
6341 /* Unknown accessibility. Complain and treat it as public. */
6342 {
6343 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6344 fieldp->accessibility);
6345 }
6346 break;
6347 }
6348 if (nfields < fip->nbaseclasses)
6349 {
6350 switch (fieldp->virtuality)
6351 {
6352 case DW_VIRTUALITY_virtual:
6353 case DW_VIRTUALITY_pure_virtual:
6354 if (cu->language == language_ada)
6355 error ("unexpected virtuality in component of Ada type");
6356 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6357 break;
6358 }
6359 }
6360 }
6361 }
6362
6363 /* Add a member function to the proper fieldlist. */
6364
6365 static void
6366 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6367 struct type *type, struct dwarf2_cu *cu)
6368 {
6369 struct objfile *objfile = cu->objfile;
6370 struct attribute *attr;
6371 struct fnfieldlist *flp;
6372 int i;
6373 struct fn_field *fnp;
6374 char *fieldname;
6375 struct nextfnfield *new_fnfield;
6376 struct type *this_type;
6377
6378 if (cu->language == language_ada)
6379 error ("unexpected member function in Ada type");
6380
6381 /* Get name of member function. */
6382 fieldname = dwarf2_name (die, cu);
6383 if (fieldname == NULL)
6384 return;
6385
6386 /* Look up member function name in fieldlist. */
6387 for (i = 0; i < fip->nfnfields; i++)
6388 {
6389 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6390 break;
6391 }
6392
6393 /* Create new list element if necessary. */
6394 if (i < fip->nfnfields)
6395 flp = &fip->fnfieldlists[i];
6396 else
6397 {
6398 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6399 {
6400 fip->fnfieldlists = (struct fnfieldlist *)
6401 xrealloc (fip->fnfieldlists,
6402 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6403 * sizeof (struct fnfieldlist));
6404 if (fip->nfnfields == 0)
6405 make_cleanup (free_current_contents, &fip->fnfieldlists);
6406 }
6407 flp = &fip->fnfieldlists[fip->nfnfields];
6408 flp->name = fieldname;
6409 flp->length = 0;
6410 flp->head = NULL;
6411 i = fip->nfnfields++;
6412 }
6413
6414 /* Create a new member function field and chain it to the field list
6415 entry. */
6416 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6417 make_cleanup (xfree, new_fnfield);
6418 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6419 new_fnfield->next = flp->head;
6420 flp->head = new_fnfield;
6421 flp->length++;
6422
6423 /* Fill in the member function field info. */
6424 fnp = &new_fnfield->fnfield;
6425
6426 /* Delay processing of the physname until later. */
6427 if (cu->language == language_cplus || cu->language == language_java)
6428 {
6429 add_to_method_list (type, i, flp->length - 1, fieldname,
6430 die, cu);
6431 }
6432 else
6433 {
6434 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6435 fnp->physname = physname ? physname : "";
6436 }
6437
6438 fnp->type = alloc_type (objfile);
6439 this_type = read_type_die (die, cu);
6440 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6441 {
6442 int nparams = TYPE_NFIELDS (this_type);
6443
6444 /* TYPE is the domain of this method, and THIS_TYPE is the type
6445 of the method itself (TYPE_CODE_METHOD). */
6446 smash_to_method_type (fnp->type, type,
6447 TYPE_TARGET_TYPE (this_type),
6448 TYPE_FIELDS (this_type),
6449 TYPE_NFIELDS (this_type),
6450 TYPE_VARARGS (this_type));
6451
6452 /* Handle static member functions.
6453 Dwarf2 has no clean way to discern C++ static and non-static
6454 member functions. G++ helps GDB by marking the first
6455 parameter for non-static member functions (which is the
6456 this pointer) as artificial. We obtain this information
6457 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
6458 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6459 fnp->voffset = VOFFSET_STATIC;
6460 }
6461 else
6462 complaint (&symfile_complaints, _("member function type missing for '%s'"),
6463 dwarf2_full_name (fieldname, die, cu));
6464
6465 /* Get fcontext from DW_AT_containing_type if present. */
6466 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6467 fnp->fcontext = die_containing_type (die, cu);
6468
6469 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6470 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6471
6472 /* Get accessibility. */
6473 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6474 if (attr)
6475 {
6476 switch (DW_UNSND (attr))
6477 {
6478 case DW_ACCESS_private:
6479 fnp->is_private = 1;
6480 break;
6481 case DW_ACCESS_protected:
6482 fnp->is_protected = 1;
6483 break;
6484 }
6485 }
6486
6487 /* Check for artificial methods. */
6488 attr = dwarf2_attr (die, DW_AT_artificial, cu);
6489 if (attr && DW_UNSND (attr) != 0)
6490 fnp->is_artificial = 1;
6491
6492 /* Get index in virtual function table if it is a virtual member
6493 function. For older versions of GCC, this is an offset in the
6494 appropriate virtual table, as specified by DW_AT_containing_type.
6495 For everyone else, it is an expression to be evaluated relative
6496 to the object address. */
6497
6498 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6499 if (attr)
6500 {
6501 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6502 {
6503 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6504 {
6505 /* Old-style GCC. */
6506 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6507 }
6508 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6509 || (DW_BLOCK (attr)->size > 1
6510 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6511 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6512 {
6513 struct dwarf_block blk;
6514 int offset;
6515
6516 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6517 ? 1 : 2);
6518 blk.size = DW_BLOCK (attr)->size - offset;
6519 blk.data = DW_BLOCK (attr)->data + offset;
6520 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6521 if ((fnp->voffset % cu->header.addr_size) != 0)
6522 dwarf2_complex_location_expr_complaint ();
6523 else
6524 fnp->voffset /= cu->header.addr_size;
6525 fnp->voffset += 2;
6526 }
6527 else
6528 dwarf2_complex_location_expr_complaint ();
6529
6530 if (!fnp->fcontext)
6531 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6532 }
6533 else if (attr_form_is_section_offset (attr))
6534 {
6535 dwarf2_complex_location_expr_complaint ();
6536 }
6537 else
6538 {
6539 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6540 fieldname);
6541 }
6542 }
6543 else
6544 {
6545 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6546 if (attr && DW_UNSND (attr))
6547 {
6548 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6549 complaint (&symfile_complaints,
6550 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6551 fieldname, die->offset);
6552 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6553 TYPE_CPLUS_DYNAMIC (type) = 1;
6554 }
6555 }
6556 }
6557
6558 /* Create the vector of member function fields, and attach it to the type. */
6559
6560 static void
6561 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6562 struct dwarf2_cu *cu)
6563 {
6564 struct fnfieldlist *flp;
6565 int total_length = 0;
6566 int i;
6567
6568 if (cu->language == language_ada)
6569 error ("unexpected member functions in Ada type");
6570
6571 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6572 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6573 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6574
6575 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6576 {
6577 struct nextfnfield *nfp = flp->head;
6578 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6579 int k;
6580
6581 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6582 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6583 fn_flp->fn_fields = (struct fn_field *)
6584 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6585 for (k = flp->length; (k--, nfp); nfp = nfp->next)
6586 fn_flp->fn_fields[k] = nfp->fnfield;
6587
6588 total_length += flp->length;
6589 }
6590
6591 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6592 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6593 }
6594
6595 /* Returns non-zero if NAME is the name of a vtable member in CU's
6596 language, zero otherwise. */
6597 static int
6598 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6599 {
6600 static const char vptr[] = "_vptr";
6601 static const char vtable[] = "vtable";
6602
6603 /* Look for the C++ and Java forms of the vtable. */
6604 if ((cu->language == language_java
6605 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6606 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6607 && is_cplus_marker (name[sizeof (vptr) - 1])))
6608 return 1;
6609
6610 return 0;
6611 }
6612
6613 /* GCC outputs unnamed structures that are really pointers to member
6614 functions, with the ABI-specified layout. If TYPE describes
6615 such a structure, smash it into a member function type.
6616
6617 GCC shouldn't do this; it should just output pointer to member DIEs.
6618 This is GCC PR debug/28767. */
6619
6620 static void
6621 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6622 {
6623 struct type *pfn_type, *domain_type, *new_type;
6624
6625 /* Check for a structure with no name and two children. */
6626 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6627 return;
6628
6629 /* Check for __pfn and __delta members. */
6630 if (TYPE_FIELD_NAME (type, 0) == NULL
6631 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6632 || TYPE_FIELD_NAME (type, 1) == NULL
6633 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6634 return;
6635
6636 /* Find the type of the method. */
6637 pfn_type = TYPE_FIELD_TYPE (type, 0);
6638 if (pfn_type == NULL
6639 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6640 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6641 return;
6642
6643 /* Look for the "this" argument. */
6644 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6645 if (TYPE_NFIELDS (pfn_type) == 0
6646 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6647 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6648 return;
6649
6650 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6651 new_type = alloc_type (objfile);
6652 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6653 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6654 TYPE_VARARGS (pfn_type));
6655 smash_to_methodptr_type (type, new_type);
6656 }
6657
6658 /* Called when we find the DIE that starts a structure or union scope
6659 (definition) to create a type for the structure or union. Fill in
6660 the type's name and general properties; the members will not be
6661 processed until process_structure_type.
6662
6663 NOTE: we need to call these functions regardless of whether or not the
6664 DIE has a DW_AT_name attribute, since it might be an anonymous
6665 structure or union. This gets the type entered into our set of
6666 user defined types.
6667
6668 However, if the structure is incomplete (an opaque struct/union)
6669 then suppress creating a symbol table entry for it since gdb only
6670 wants to find the one with the complete definition. Note that if
6671 it is complete, we just call new_symbol, which does it's own
6672 checking about whether the struct/union is anonymous or not (and
6673 suppresses creating a symbol table entry itself). */
6674
6675 static struct type *
6676 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6677 {
6678 struct objfile *objfile = cu->objfile;
6679 struct type *type;
6680 struct attribute *attr;
6681 char *name;
6682
6683 /* If the definition of this type lives in .debug_types, read that type.
6684 Don't follow DW_AT_specification though, that will take us back up
6685 the chain and we want to go down. */
6686 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6687 if (attr)
6688 {
6689 struct dwarf2_cu *type_cu = cu;
6690 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6691
6692 /* We could just recurse on read_structure_type, but we need to call
6693 get_die_type to ensure only one type for this DIE is created.
6694 This is important, for example, because for c++ classes we need
6695 TYPE_NAME set which is only done by new_symbol. Blech. */
6696 type = read_type_die (type_die, type_cu);
6697
6698 /* TYPE_CU may not be the same as CU.
6699 Ensure TYPE is recorded in CU's type_hash table. */
6700 return set_die_type (die, type, cu);
6701 }
6702
6703 type = alloc_type (objfile);
6704 INIT_CPLUS_SPECIFIC (type);
6705
6706 name = dwarf2_name (die, cu);
6707 if (name != NULL)
6708 {
6709 if (cu->language == language_cplus
6710 || cu->language == language_java)
6711 {
6712 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6713
6714 /* dwarf2_full_name might have already finished building the DIE's
6715 type. If so, there is no need to continue. */
6716 if (get_die_type (die, cu) != NULL)
6717 return get_die_type (die, cu);
6718
6719 TYPE_TAG_NAME (type) = full_name;
6720 if (die->tag == DW_TAG_structure_type
6721 || die->tag == DW_TAG_class_type)
6722 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6723 }
6724 else
6725 {
6726 /* The name is already allocated along with this objfile, so
6727 we don't need to duplicate it for the type. */
6728 TYPE_TAG_NAME (type) = (char *) name;
6729 if (die->tag == DW_TAG_class_type)
6730 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6731 }
6732 }
6733
6734 if (die->tag == DW_TAG_structure_type)
6735 {
6736 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6737 }
6738 else if (die->tag == DW_TAG_union_type)
6739 {
6740 TYPE_CODE (type) = TYPE_CODE_UNION;
6741 }
6742 else
6743 {
6744 TYPE_CODE (type) = TYPE_CODE_CLASS;
6745 }
6746
6747 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6748 TYPE_DECLARED_CLASS (type) = 1;
6749
6750 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6751 if (attr)
6752 {
6753 TYPE_LENGTH (type) = DW_UNSND (attr);
6754 }
6755 else
6756 {
6757 TYPE_LENGTH (type) = 0;
6758 }
6759
6760 TYPE_STUB_SUPPORTED (type) = 1;
6761 if (die_is_declaration (die, cu))
6762 TYPE_STUB (type) = 1;
6763 else if (attr == NULL && die->child == NULL
6764 && producer_is_realview (cu->producer))
6765 /* RealView does not output the required DW_AT_declaration
6766 on incomplete types. */
6767 TYPE_STUB (type) = 1;
6768
6769 /* We need to add the type field to the die immediately so we don't
6770 infinitely recurse when dealing with pointers to the structure
6771 type within the structure itself. */
6772 set_die_type (die, type, cu);
6773
6774 /* set_die_type should be already done. */
6775 set_descriptive_type (type, die, cu);
6776
6777 return type;
6778 }
6779
6780 /* Finish creating a structure or union type, including filling in
6781 its members and creating a symbol for it. */
6782
6783 static void
6784 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6785 {
6786 struct objfile *objfile = cu->objfile;
6787 struct die_info *child_die = die->child;
6788 struct type *type;
6789
6790 type = get_die_type (die, cu);
6791 if (type == NULL)
6792 type = read_structure_type (die, cu);
6793
6794 if (die->child != NULL && ! die_is_declaration (die, cu))
6795 {
6796 struct field_info fi;
6797 struct die_info *child_die;
6798 VEC (symbolp) *template_args = NULL;
6799 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6800
6801 memset (&fi, 0, sizeof (struct field_info));
6802
6803 child_die = die->child;
6804
6805 while (child_die && child_die->tag)
6806 {
6807 if (child_die->tag == DW_TAG_member
6808 || child_die->tag == DW_TAG_variable)
6809 {
6810 /* NOTE: carlton/2002-11-05: A C++ static data member
6811 should be a DW_TAG_member that is a declaration, but
6812 all versions of G++ as of this writing (so through at
6813 least 3.2.1) incorrectly generate DW_TAG_variable
6814 tags for them instead. */
6815 dwarf2_add_field (&fi, child_die, cu);
6816 }
6817 else if (child_die->tag == DW_TAG_subprogram)
6818 {
6819 /* C++ member function. */
6820 dwarf2_add_member_fn (&fi, child_die, type, cu);
6821 }
6822 else if (child_die->tag == DW_TAG_inheritance)
6823 {
6824 /* C++ base class field. */
6825 dwarf2_add_field (&fi, child_die, cu);
6826 }
6827 else if (child_die->tag == DW_TAG_typedef)
6828 dwarf2_add_typedef (&fi, child_die, cu);
6829 else if (child_die->tag == DW_TAG_template_type_param
6830 || child_die->tag == DW_TAG_template_value_param)
6831 {
6832 struct symbol *arg = new_symbol (child_die, NULL, cu);
6833
6834 if (arg != NULL)
6835 VEC_safe_push (symbolp, template_args, arg);
6836 }
6837
6838 child_die = sibling_die (child_die);
6839 }
6840
6841 /* Attach template arguments to type. */
6842 if (! VEC_empty (symbolp, template_args))
6843 {
6844 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6845 TYPE_N_TEMPLATE_ARGUMENTS (type)
6846 = VEC_length (symbolp, template_args);
6847 TYPE_TEMPLATE_ARGUMENTS (type)
6848 = obstack_alloc (&objfile->objfile_obstack,
6849 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6850 * sizeof (struct symbol *)));
6851 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6852 VEC_address (symbolp, template_args),
6853 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6854 * sizeof (struct symbol *)));
6855 VEC_free (symbolp, template_args);
6856 }
6857
6858 /* Attach fields and member functions to the type. */
6859 if (fi.nfields)
6860 dwarf2_attach_fields_to_type (&fi, type, cu);
6861 if (fi.nfnfields)
6862 {
6863 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6864
6865 /* Get the type which refers to the base class (possibly this
6866 class itself) which contains the vtable pointer for the current
6867 class from the DW_AT_containing_type attribute. This use of
6868 DW_AT_containing_type is a GNU extension. */
6869
6870 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6871 {
6872 struct type *t = die_containing_type (die, cu);
6873
6874 TYPE_VPTR_BASETYPE (type) = t;
6875 if (type == t)
6876 {
6877 int i;
6878
6879 /* Our own class provides vtbl ptr. */
6880 for (i = TYPE_NFIELDS (t) - 1;
6881 i >= TYPE_N_BASECLASSES (t);
6882 --i)
6883 {
6884 char *fieldname = TYPE_FIELD_NAME (t, i);
6885
6886 if (is_vtable_name (fieldname, cu))
6887 {
6888 TYPE_VPTR_FIELDNO (type) = i;
6889 break;
6890 }
6891 }
6892
6893 /* Complain if virtual function table field not found. */
6894 if (i < TYPE_N_BASECLASSES (t))
6895 complaint (&symfile_complaints,
6896 _("virtual function table pointer not found when defining class '%s'"),
6897 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6898 "");
6899 }
6900 else
6901 {
6902 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6903 }
6904 }
6905 else if (cu->producer
6906 && strncmp (cu->producer,
6907 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6908 {
6909 /* The IBM XLC compiler does not provide direct indication
6910 of the containing type, but the vtable pointer is
6911 always named __vfp. */
6912
6913 int i;
6914
6915 for (i = TYPE_NFIELDS (type) - 1;
6916 i >= TYPE_N_BASECLASSES (type);
6917 --i)
6918 {
6919 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6920 {
6921 TYPE_VPTR_FIELDNO (type) = i;
6922 TYPE_VPTR_BASETYPE (type) = type;
6923 break;
6924 }
6925 }
6926 }
6927 }
6928
6929 /* Copy fi.typedef_field_list linked list elements content into the
6930 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6931 if (fi.typedef_field_list)
6932 {
6933 int i = fi.typedef_field_list_count;
6934
6935 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6936 TYPE_TYPEDEF_FIELD_ARRAY (type)
6937 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6938 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6939
6940 /* Reverse the list order to keep the debug info elements order. */
6941 while (--i >= 0)
6942 {
6943 struct typedef_field *dest, *src;
6944
6945 dest = &TYPE_TYPEDEF_FIELD (type, i);
6946 src = &fi.typedef_field_list->field;
6947 fi.typedef_field_list = fi.typedef_field_list->next;
6948 *dest = *src;
6949 }
6950 }
6951
6952 do_cleanups (back_to);
6953 }
6954
6955 quirk_gcc_member_function_pointer (type, cu->objfile);
6956
6957 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6958 snapshots) has been known to create a die giving a declaration
6959 for a class that has, as a child, a die giving a definition for a
6960 nested class. So we have to process our children even if the
6961 current die is a declaration. Normally, of course, a declaration
6962 won't have any children at all. */
6963
6964 while (child_die != NULL && child_die->tag)
6965 {
6966 if (child_die->tag == DW_TAG_member
6967 || child_die->tag == DW_TAG_variable
6968 || child_die->tag == DW_TAG_inheritance
6969 || child_die->tag == DW_TAG_template_value_param
6970 || child_die->tag == DW_TAG_template_type_param)
6971 {
6972 /* Do nothing. */
6973 }
6974 else
6975 process_die (child_die, cu);
6976
6977 child_die = sibling_die (child_die);
6978 }
6979
6980 /* Do not consider external references. According to the DWARF standard,
6981 these DIEs are identified by the fact that they have no byte_size
6982 attribute, and a declaration attribute. */
6983 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6984 || !die_is_declaration (die, cu))
6985 new_symbol (die, type, cu);
6986 }
6987
6988 /* Given a DW_AT_enumeration_type die, set its type. We do not
6989 complete the type's fields yet, or create any symbols. */
6990
6991 static struct type *
6992 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6993 {
6994 struct objfile *objfile = cu->objfile;
6995 struct type *type;
6996 struct attribute *attr;
6997 const char *name;
6998
6999 /* If the definition of this type lives in .debug_types, read that type.
7000 Don't follow DW_AT_specification though, that will take us back up
7001 the chain and we want to go down. */
7002 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7003 if (attr)
7004 {
7005 struct dwarf2_cu *type_cu = cu;
7006 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7007
7008 type = read_type_die (type_die, type_cu);
7009
7010 /* TYPE_CU may not be the same as CU.
7011 Ensure TYPE is recorded in CU's type_hash table. */
7012 return set_die_type (die, type, cu);
7013 }
7014
7015 type = alloc_type (objfile);
7016
7017 TYPE_CODE (type) = TYPE_CODE_ENUM;
7018 name = dwarf2_full_name (NULL, die, cu);
7019 if (name != NULL)
7020 TYPE_TAG_NAME (type) = (char *) name;
7021
7022 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7023 if (attr)
7024 {
7025 TYPE_LENGTH (type) = DW_UNSND (attr);
7026 }
7027 else
7028 {
7029 TYPE_LENGTH (type) = 0;
7030 }
7031
7032 /* The enumeration DIE can be incomplete. In Ada, any type can be
7033 declared as private in the package spec, and then defined only
7034 inside the package body. Such types are known as Taft Amendment
7035 Types. When another package uses such a type, an incomplete DIE
7036 may be generated by the compiler. */
7037 if (die_is_declaration (die, cu))
7038 TYPE_STUB (type) = 1;
7039
7040 return set_die_type (die, type, cu);
7041 }
7042
7043 /* Given a pointer to a die which begins an enumeration, process all
7044 the dies that define the members of the enumeration, and create the
7045 symbol for the enumeration type.
7046
7047 NOTE: We reverse the order of the element list. */
7048
7049 static void
7050 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7051 {
7052 struct type *this_type;
7053
7054 this_type = get_die_type (die, cu);
7055 if (this_type == NULL)
7056 this_type = read_enumeration_type (die, cu);
7057
7058 if (die->child != NULL)
7059 {
7060 struct die_info *child_die;
7061 struct symbol *sym;
7062 struct field *fields = NULL;
7063 int num_fields = 0;
7064 int unsigned_enum = 1;
7065 char *name;
7066
7067 child_die = die->child;
7068 while (child_die && child_die->tag)
7069 {
7070 if (child_die->tag != DW_TAG_enumerator)
7071 {
7072 process_die (child_die, cu);
7073 }
7074 else
7075 {
7076 name = dwarf2_name (child_die, cu);
7077 if (name)
7078 {
7079 sym = new_symbol (child_die, this_type, cu);
7080 if (SYMBOL_VALUE (sym) < 0)
7081 unsigned_enum = 0;
7082
7083 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7084 {
7085 fields = (struct field *)
7086 xrealloc (fields,
7087 (num_fields + DW_FIELD_ALLOC_CHUNK)
7088 * sizeof (struct field));
7089 }
7090
7091 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7092 FIELD_TYPE (fields[num_fields]) = NULL;
7093 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7094 FIELD_BITSIZE (fields[num_fields]) = 0;
7095
7096 num_fields++;
7097 }
7098 }
7099
7100 child_die = sibling_die (child_die);
7101 }
7102
7103 if (num_fields)
7104 {
7105 TYPE_NFIELDS (this_type) = num_fields;
7106 TYPE_FIELDS (this_type) = (struct field *)
7107 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7108 memcpy (TYPE_FIELDS (this_type), fields,
7109 sizeof (struct field) * num_fields);
7110 xfree (fields);
7111 }
7112 if (unsigned_enum)
7113 TYPE_UNSIGNED (this_type) = 1;
7114 }
7115
7116 new_symbol (die, this_type, cu);
7117 }
7118
7119 /* Extract all information from a DW_TAG_array_type DIE and put it in
7120 the DIE's type field. For now, this only handles one dimensional
7121 arrays. */
7122
7123 static struct type *
7124 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7125 {
7126 struct objfile *objfile = cu->objfile;
7127 struct die_info *child_die;
7128 struct type *type;
7129 struct type *element_type, *range_type, *index_type;
7130 struct type **range_types = NULL;
7131 struct attribute *attr;
7132 int ndim = 0;
7133 struct cleanup *back_to;
7134 char *name;
7135
7136 element_type = die_type (die, cu);
7137
7138 /* The die_type call above may have already set the type for this DIE. */
7139 type = get_die_type (die, cu);
7140 if (type)
7141 return type;
7142
7143 /* Irix 6.2 native cc creates array types without children for
7144 arrays with unspecified length. */
7145 if (die->child == NULL)
7146 {
7147 index_type = objfile_type (objfile)->builtin_int;
7148 range_type = create_range_type (NULL, index_type, 0, -1);
7149 type = create_array_type (NULL, element_type, range_type);
7150 return set_die_type (die, type, cu);
7151 }
7152
7153 back_to = make_cleanup (null_cleanup, NULL);
7154 child_die = die->child;
7155 while (child_die && child_die->tag)
7156 {
7157 if (child_die->tag == DW_TAG_subrange_type)
7158 {
7159 struct type *child_type = read_type_die (child_die, cu);
7160
7161 if (child_type != NULL)
7162 {
7163 /* The range type was succesfully read. Save it for
7164 the array type creation. */
7165 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7166 {
7167 range_types = (struct type **)
7168 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7169 * sizeof (struct type *));
7170 if (ndim == 0)
7171 make_cleanup (free_current_contents, &range_types);
7172 }
7173 range_types[ndim++] = child_type;
7174 }
7175 }
7176 child_die = sibling_die (child_die);
7177 }
7178
7179 /* Dwarf2 dimensions are output from left to right, create the
7180 necessary array types in backwards order. */
7181
7182 type = element_type;
7183
7184 if (read_array_order (die, cu) == DW_ORD_col_major)
7185 {
7186 int i = 0;
7187
7188 while (i < ndim)
7189 type = create_array_type (NULL, type, range_types[i++]);
7190 }
7191 else
7192 {
7193 while (ndim-- > 0)
7194 type = create_array_type (NULL, type, range_types[ndim]);
7195 }
7196
7197 /* Understand Dwarf2 support for vector types (like they occur on
7198 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7199 array type. This is not part of the Dwarf2/3 standard yet, but a
7200 custom vendor extension. The main difference between a regular
7201 array and the vector variant is that vectors are passed by value
7202 to functions. */
7203 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7204 if (attr)
7205 make_vector_type (type);
7206
7207 name = dwarf2_name (die, cu);
7208 if (name)
7209 TYPE_NAME (type) = name;
7210
7211 /* Install the type in the die. */
7212 set_die_type (die, type, cu);
7213
7214 /* set_die_type should be already done. */
7215 set_descriptive_type (type, die, cu);
7216
7217 do_cleanups (back_to);
7218
7219 return type;
7220 }
7221
7222 static enum dwarf_array_dim_ordering
7223 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7224 {
7225 struct attribute *attr;
7226
7227 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7228
7229 if (attr) return DW_SND (attr);
7230
7231 /*
7232 GNU F77 is a special case, as at 08/2004 array type info is the
7233 opposite order to the dwarf2 specification, but data is still
7234 laid out as per normal fortran.
7235
7236 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7237 version checking.
7238 */
7239
7240 if (cu->language == language_fortran
7241 && cu->producer && strstr (cu->producer, "GNU F77"))
7242 {
7243 return DW_ORD_row_major;
7244 }
7245
7246 switch (cu->language_defn->la_array_ordering)
7247 {
7248 case array_column_major:
7249 return DW_ORD_col_major;
7250 case array_row_major:
7251 default:
7252 return DW_ORD_row_major;
7253 };
7254 }
7255
7256 /* Extract all information from a DW_TAG_set_type DIE and put it in
7257 the DIE's type field. */
7258
7259 static struct type *
7260 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7261 {
7262 struct type *domain_type, *set_type;
7263 struct attribute *attr;
7264
7265 domain_type = die_type (die, cu);
7266
7267 /* The die_type call above may have already set the type for this DIE. */
7268 set_type = get_die_type (die, cu);
7269 if (set_type)
7270 return set_type;
7271
7272 set_type = create_set_type (NULL, domain_type);
7273
7274 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7275 if (attr)
7276 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7277
7278 return set_die_type (die, set_type, cu);
7279 }
7280
7281 /* First cut: install each common block member as a global variable. */
7282
7283 static void
7284 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7285 {
7286 struct die_info *child_die;
7287 struct attribute *attr;
7288 struct symbol *sym;
7289 CORE_ADDR base = (CORE_ADDR) 0;
7290
7291 attr = dwarf2_attr (die, DW_AT_location, cu);
7292 if (attr)
7293 {
7294 /* Support the .debug_loc offsets */
7295 if (attr_form_is_block (attr))
7296 {
7297 base = decode_locdesc (DW_BLOCK (attr), cu);
7298 }
7299 else if (attr_form_is_section_offset (attr))
7300 {
7301 dwarf2_complex_location_expr_complaint ();
7302 }
7303 else
7304 {
7305 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7306 "common block member");
7307 }
7308 }
7309 if (die->child != NULL)
7310 {
7311 child_die = die->child;
7312 while (child_die && child_die->tag)
7313 {
7314 sym = new_symbol (child_die, NULL, cu);
7315 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7316 if (sym != NULL && attr != NULL)
7317 {
7318 CORE_ADDR byte_offset = 0;
7319
7320 if (attr_form_is_section_offset (attr))
7321 dwarf2_complex_location_expr_complaint ();
7322 else if (attr_form_is_constant (attr))
7323 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7324 else if (attr_form_is_block (attr))
7325 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7326 else
7327 dwarf2_complex_location_expr_complaint ();
7328
7329 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7330 add_symbol_to_list (sym, &global_symbols);
7331 }
7332 child_die = sibling_die (child_die);
7333 }
7334 }
7335 }
7336
7337 /* Create a type for a C++ namespace. */
7338
7339 static struct type *
7340 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7341 {
7342 struct objfile *objfile = cu->objfile;
7343 const char *previous_prefix, *name;
7344 int is_anonymous;
7345 struct type *type;
7346
7347 /* For extensions, reuse the type of the original namespace. */
7348 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7349 {
7350 struct die_info *ext_die;
7351 struct dwarf2_cu *ext_cu = cu;
7352
7353 ext_die = dwarf2_extension (die, &ext_cu);
7354 type = read_type_die (ext_die, ext_cu);
7355
7356 /* EXT_CU may not be the same as CU.
7357 Ensure TYPE is recorded in CU's type_hash table. */
7358 return set_die_type (die, type, cu);
7359 }
7360
7361 name = namespace_name (die, &is_anonymous, cu);
7362
7363 /* Now build the name of the current namespace. */
7364
7365 previous_prefix = determine_prefix (die, cu);
7366 if (previous_prefix[0] != '\0')
7367 name = typename_concat (&objfile->objfile_obstack,
7368 previous_prefix, name, 0, cu);
7369
7370 /* Create the type. */
7371 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7372 objfile);
7373 TYPE_NAME (type) = (char *) name;
7374 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7375
7376 return set_die_type (die, type, cu);
7377 }
7378
7379 /* Read a C++ namespace. */
7380
7381 static void
7382 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7383 {
7384 struct objfile *objfile = cu->objfile;
7385 const char *name;
7386 int is_anonymous;
7387
7388 /* Add a symbol associated to this if we haven't seen the namespace
7389 before. Also, add a using directive if it's an anonymous
7390 namespace. */
7391
7392 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7393 {
7394 struct type *type;
7395
7396 type = read_type_die (die, cu);
7397 new_symbol (die, type, cu);
7398
7399 name = namespace_name (die, &is_anonymous, cu);
7400 if (is_anonymous)
7401 {
7402 const char *previous_prefix = determine_prefix (die, cu);
7403
7404 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7405 NULL, &objfile->objfile_obstack);
7406 }
7407 }
7408
7409 if (die->child != NULL)
7410 {
7411 struct die_info *child_die = die->child;
7412
7413 while (child_die && child_die->tag)
7414 {
7415 process_die (child_die, cu);
7416 child_die = sibling_die (child_die);
7417 }
7418 }
7419 }
7420
7421 /* Read a Fortran module as type. This DIE can be only a declaration used for
7422 imported module. Still we need that type as local Fortran "use ... only"
7423 declaration imports depend on the created type in determine_prefix. */
7424
7425 static struct type *
7426 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7427 {
7428 struct objfile *objfile = cu->objfile;
7429 char *module_name;
7430 struct type *type;
7431
7432 module_name = dwarf2_name (die, cu);
7433 if (!module_name)
7434 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7435 die->offset);
7436 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7437
7438 /* determine_prefix uses TYPE_TAG_NAME. */
7439 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7440
7441 return set_die_type (die, type, cu);
7442 }
7443
7444 /* Read a Fortran module. */
7445
7446 static void
7447 read_module (struct die_info *die, struct dwarf2_cu *cu)
7448 {
7449 struct die_info *child_die = die->child;
7450
7451 while (child_die && child_die->tag)
7452 {
7453 process_die (child_die, cu);
7454 child_die = sibling_die (child_die);
7455 }
7456 }
7457
7458 /* Return the name of the namespace represented by DIE. Set
7459 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7460 namespace. */
7461
7462 static const char *
7463 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7464 {
7465 struct die_info *current_die;
7466 const char *name = NULL;
7467
7468 /* Loop through the extensions until we find a name. */
7469
7470 for (current_die = die;
7471 current_die != NULL;
7472 current_die = dwarf2_extension (die, &cu))
7473 {
7474 name = dwarf2_name (current_die, cu);
7475 if (name != NULL)
7476 break;
7477 }
7478
7479 /* Is it an anonymous namespace? */
7480
7481 *is_anonymous = (name == NULL);
7482 if (*is_anonymous)
7483 name = "(anonymous namespace)";
7484
7485 return name;
7486 }
7487
7488 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7489 the user defined type vector. */
7490
7491 static struct type *
7492 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7493 {
7494 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7495 struct comp_unit_head *cu_header = &cu->header;
7496 struct type *type;
7497 struct attribute *attr_byte_size;
7498 struct attribute *attr_address_class;
7499 int byte_size, addr_class;
7500 struct type *target_type;
7501
7502 target_type = die_type (die, cu);
7503
7504 /* The die_type call above may have already set the type for this DIE. */
7505 type = get_die_type (die, cu);
7506 if (type)
7507 return type;
7508
7509 type = lookup_pointer_type (target_type);
7510
7511 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7512 if (attr_byte_size)
7513 byte_size = DW_UNSND (attr_byte_size);
7514 else
7515 byte_size = cu_header->addr_size;
7516
7517 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7518 if (attr_address_class)
7519 addr_class = DW_UNSND (attr_address_class);
7520 else
7521 addr_class = DW_ADDR_none;
7522
7523 /* If the pointer size or address class is different than the
7524 default, create a type variant marked as such and set the
7525 length accordingly. */
7526 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7527 {
7528 if (gdbarch_address_class_type_flags_p (gdbarch))
7529 {
7530 int type_flags;
7531
7532 type_flags = gdbarch_address_class_type_flags
7533 (gdbarch, byte_size, addr_class);
7534 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7535 == 0);
7536 type = make_type_with_address_space (type, type_flags);
7537 }
7538 else if (TYPE_LENGTH (type) != byte_size)
7539 {
7540 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7541 }
7542 else
7543 {
7544 /* Should we also complain about unhandled address classes? */
7545 }
7546 }
7547
7548 TYPE_LENGTH (type) = byte_size;
7549 return set_die_type (die, type, cu);
7550 }
7551
7552 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7553 the user defined type vector. */
7554
7555 static struct type *
7556 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7557 {
7558 struct type *type;
7559 struct type *to_type;
7560 struct type *domain;
7561
7562 to_type = die_type (die, cu);
7563 domain = die_containing_type (die, cu);
7564
7565 /* The calls above may have already set the type for this DIE. */
7566 type = get_die_type (die, cu);
7567 if (type)
7568 return type;
7569
7570 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7571 type = lookup_methodptr_type (to_type);
7572 else
7573 type = lookup_memberptr_type (to_type, domain);
7574
7575 return set_die_type (die, type, cu);
7576 }
7577
7578 /* Extract all information from a DW_TAG_reference_type DIE and add to
7579 the user defined type vector. */
7580
7581 static struct type *
7582 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7583 {
7584 struct comp_unit_head *cu_header = &cu->header;
7585 struct type *type, *target_type;
7586 struct attribute *attr;
7587
7588 target_type = die_type (die, cu);
7589
7590 /* The die_type call above may have already set the type for this DIE. */
7591 type = get_die_type (die, cu);
7592 if (type)
7593 return type;
7594
7595 type = lookup_reference_type (target_type);
7596 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7597 if (attr)
7598 {
7599 TYPE_LENGTH (type) = DW_UNSND (attr);
7600 }
7601 else
7602 {
7603 TYPE_LENGTH (type) = cu_header->addr_size;
7604 }
7605 return set_die_type (die, type, cu);
7606 }
7607
7608 static struct type *
7609 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7610 {
7611 struct type *base_type, *cv_type;
7612
7613 base_type = die_type (die, cu);
7614
7615 /* The die_type call above may have already set the type for this DIE. */
7616 cv_type = get_die_type (die, cu);
7617 if (cv_type)
7618 return cv_type;
7619
7620 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7621 return set_die_type (die, cv_type, cu);
7622 }
7623
7624 static struct type *
7625 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7626 {
7627 struct type *base_type, *cv_type;
7628
7629 base_type = die_type (die, cu);
7630
7631 /* The die_type call above may have already set the type for this DIE. */
7632 cv_type = get_die_type (die, cu);
7633 if (cv_type)
7634 return cv_type;
7635
7636 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7637 return set_die_type (die, cv_type, cu);
7638 }
7639
7640 /* Extract all information from a DW_TAG_string_type DIE and add to
7641 the user defined type vector. It isn't really a user defined type,
7642 but it behaves like one, with other DIE's using an AT_user_def_type
7643 attribute to reference it. */
7644
7645 static struct type *
7646 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7647 {
7648 struct objfile *objfile = cu->objfile;
7649 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7650 struct type *type, *range_type, *index_type, *char_type;
7651 struct attribute *attr;
7652 unsigned int length;
7653
7654 attr = dwarf2_attr (die, DW_AT_string_length, cu);
7655 if (attr)
7656 {
7657 length = DW_UNSND (attr);
7658 }
7659 else
7660 {
7661 /* check for the DW_AT_byte_size attribute */
7662 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7663 if (attr)
7664 {
7665 length = DW_UNSND (attr);
7666 }
7667 else
7668 {
7669 length = 1;
7670 }
7671 }
7672
7673 index_type = objfile_type (objfile)->builtin_int;
7674 range_type = create_range_type (NULL, index_type, 1, length);
7675 char_type = language_string_char_type (cu->language_defn, gdbarch);
7676 type = create_string_type (NULL, char_type, range_type);
7677
7678 return set_die_type (die, type, cu);
7679 }
7680
7681 /* Handle DIES due to C code like:
7682
7683 struct foo
7684 {
7685 int (*funcp)(int a, long l);
7686 int b;
7687 };
7688
7689 ('funcp' generates a DW_TAG_subroutine_type DIE)
7690 */
7691
7692 static struct type *
7693 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7694 {
7695 struct type *type; /* Type that this function returns */
7696 struct type *ftype; /* Function that returns above type */
7697 struct attribute *attr;
7698
7699 type = die_type (die, cu);
7700
7701 /* The die_type call above may have already set the type for this DIE. */
7702 ftype = get_die_type (die, cu);
7703 if (ftype)
7704 return ftype;
7705
7706 ftype = lookup_function_type (type);
7707
7708 /* All functions in C++, Pascal and Java have prototypes. */
7709 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7710 if ((attr && (DW_UNSND (attr) != 0))
7711 || cu->language == language_cplus
7712 || cu->language == language_java
7713 || cu->language == language_pascal)
7714 TYPE_PROTOTYPED (ftype) = 1;
7715 else if (producer_is_realview (cu->producer))
7716 /* RealView does not emit DW_AT_prototyped. We can not
7717 distinguish prototyped and unprototyped functions; default to
7718 prototyped, since that is more common in modern code (and
7719 RealView warns about unprototyped functions). */
7720 TYPE_PROTOTYPED (ftype) = 1;
7721
7722 /* Store the calling convention in the type if it's available in
7723 the subroutine die. Otherwise set the calling convention to
7724 the default value DW_CC_normal. */
7725 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7726 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7727
7728 /* We need to add the subroutine type to the die immediately so
7729 we don't infinitely recurse when dealing with parameters
7730 declared as the same subroutine type. */
7731 set_die_type (die, ftype, cu);
7732
7733 if (die->child != NULL)
7734 {
7735 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7736 struct die_info *child_die;
7737 int nparams, iparams;
7738
7739 /* Count the number of parameters.
7740 FIXME: GDB currently ignores vararg functions, but knows about
7741 vararg member functions. */
7742 nparams = 0;
7743 child_die = die->child;
7744 while (child_die && child_die->tag)
7745 {
7746 if (child_die->tag == DW_TAG_formal_parameter)
7747 nparams++;
7748 else if (child_die->tag == DW_TAG_unspecified_parameters)
7749 TYPE_VARARGS (ftype) = 1;
7750 child_die = sibling_die (child_die);
7751 }
7752
7753 /* Allocate storage for parameters and fill them in. */
7754 TYPE_NFIELDS (ftype) = nparams;
7755 TYPE_FIELDS (ftype) = (struct field *)
7756 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7757
7758 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7759 even if we error out during the parameters reading below. */
7760 for (iparams = 0; iparams < nparams; iparams++)
7761 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7762
7763 iparams = 0;
7764 child_die = die->child;
7765 while (child_die && child_die->tag)
7766 {
7767 if (child_die->tag == DW_TAG_formal_parameter)
7768 {
7769 struct type *arg_type;
7770
7771 /* DWARF version 2 has no clean way to discern C++
7772 static and non-static member functions. G++ helps
7773 GDB by marking the first parameter for non-static
7774 member functions (which is the this pointer) as
7775 artificial. We pass this information to
7776 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7777
7778 DWARF version 3 added DW_AT_object_pointer, which GCC
7779 4.5 does not yet generate. */
7780 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7781 if (attr)
7782 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7783 else
7784 {
7785 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7786
7787 /* GCC/43521: In java, the formal parameter
7788 "this" is sometimes not marked with DW_AT_artificial. */
7789 if (cu->language == language_java)
7790 {
7791 const char *name = dwarf2_name (child_die, cu);
7792
7793 if (name && !strcmp (name, "this"))
7794 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7795 }
7796 }
7797 arg_type = die_type (child_die, cu);
7798
7799 /* RealView does not mark THIS as const, which the testsuite
7800 expects. GCC marks THIS as const in method definitions,
7801 but not in the class specifications (GCC PR 43053). */
7802 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7803 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7804 {
7805 int is_this = 0;
7806 struct dwarf2_cu *arg_cu = cu;
7807 const char *name = dwarf2_name (child_die, cu);
7808
7809 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7810 if (attr)
7811 {
7812 /* If the compiler emits this, use it. */
7813 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7814 is_this = 1;
7815 }
7816 else if (name && strcmp (name, "this") == 0)
7817 /* Function definitions will have the argument names. */
7818 is_this = 1;
7819 else if (name == NULL && iparams == 0)
7820 /* Declarations may not have the names, so like
7821 elsewhere in GDB, assume an artificial first
7822 argument is "this". */
7823 is_this = 1;
7824
7825 if (is_this)
7826 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7827 arg_type, 0);
7828 }
7829
7830 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7831 iparams++;
7832 }
7833 child_die = sibling_die (child_die);
7834 }
7835 }
7836
7837 return ftype;
7838 }
7839
7840 static struct type *
7841 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7842 {
7843 struct objfile *objfile = cu->objfile;
7844 const char *name = NULL;
7845 struct type *this_type;
7846
7847 name = dwarf2_full_name (NULL, die, cu);
7848 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7849 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7850 TYPE_NAME (this_type) = (char *) name;
7851 set_die_type (die, this_type, cu);
7852 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7853 return this_type;
7854 }
7855
7856 /* Find a representation of a given base type and install
7857 it in the TYPE field of the die. */
7858
7859 static struct type *
7860 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7861 {
7862 struct objfile *objfile = cu->objfile;
7863 struct type *type;
7864 struct attribute *attr;
7865 int encoding = 0, size = 0;
7866 char *name;
7867 enum type_code code = TYPE_CODE_INT;
7868 int type_flags = 0;
7869 struct type *target_type = NULL;
7870
7871 attr = dwarf2_attr (die, DW_AT_encoding, cu);
7872 if (attr)
7873 {
7874 encoding = DW_UNSND (attr);
7875 }
7876 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7877 if (attr)
7878 {
7879 size = DW_UNSND (attr);
7880 }
7881 name = dwarf2_name (die, cu);
7882 if (!name)
7883 {
7884 complaint (&symfile_complaints,
7885 _("DW_AT_name missing from DW_TAG_base_type"));
7886 }
7887
7888 switch (encoding)
7889 {
7890 case DW_ATE_address:
7891 /* Turn DW_ATE_address into a void * pointer. */
7892 code = TYPE_CODE_PTR;
7893 type_flags |= TYPE_FLAG_UNSIGNED;
7894 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7895 break;
7896 case DW_ATE_boolean:
7897 code = TYPE_CODE_BOOL;
7898 type_flags |= TYPE_FLAG_UNSIGNED;
7899 break;
7900 case DW_ATE_complex_float:
7901 code = TYPE_CODE_COMPLEX;
7902 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7903 break;
7904 case DW_ATE_decimal_float:
7905 code = TYPE_CODE_DECFLOAT;
7906 break;
7907 case DW_ATE_float:
7908 code = TYPE_CODE_FLT;
7909 break;
7910 case DW_ATE_signed:
7911 break;
7912 case DW_ATE_unsigned:
7913 type_flags |= TYPE_FLAG_UNSIGNED;
7914 break;
7915 case DW_ATE_signed_char:
7916 if (cu->language == language_ada || cu->language == language_m2
7917 || cu->language == language_pascal)
7918 code = TYPE_CODE_CHAR;
7919 break;
7920 case DW_ATE_unsigned_char:
7921 if (cu->language == language_ada || cu->language == language_m2
7922 || cu->language == language_pascal)
7923 code = TYPE_CODE_CHAR;
7924 type_flags |= TYPE_FLAG_UNSIGNED;
7925 break;
7926 case DW_ATE_UTF:
7927 /* We just treat this as an integer and then recognize the
7928 type by name elsewhere. */
7929 break;
7930
7931 default:
7932 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7933 dwarf_type_encoding_name (encoding));
7934 break;
7935 }
7936
7937 type = init_type (code, size, type_flags, NULL, objfile);
7938 TYPE_NAME (type) = name;
7939 TYPE_TARGET_TYPE (type) = target_type;
7940
7941 if (name && strcmp (name, "char") == 0)
7942 TYPE_NOSIGN (type) = 1;
7943
7944 return set_die_type (die, type, cu);
7945 }
7946
7947 /* Read the given DW_AT_subrange DIE. */
7948
7949 static struct type *
7950 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7951 {
7952 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7953 struct type *base_type;
7954 struct type *range_type;
7955 struct attribute *attr;
7956 LONGEST low = 0;
7957 LONGEST high = -1;
7958 char *name;
7959 LONGEST negative_mask;
7960
7961 base_type = die_type (die, cu);
7962 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7963 check_typedef (base_type);
7964
7965 /* The die_type call above may have already set the type for this DIE. */
7966 range_type = get_die_type (die, cu);
7967 if (range_type)
7968 return range_type;
7969
7970 if (cu->language == language_fortran)
7971 {
7972 /* FORTRAN implies a lower bound of 1, if not given. */
7973 low = 1;
7974 }
7975
7976 /* FIXME: For variable sized arrays either of these could be
7977 a variable rather than a constant value. We'll allow it,
7978 but we don't know how to handle it. */
7979 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7980 if (attr)
7981 low = dwarf2_get_attr_constant_value (attr, 0);
7982
7983 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7984 if (attr)
7985 {
7986 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7987 {
7988 /* GCC encodes arrays with unspecified or dynamic length
7989 with a DW_FORM_block1 attribute or a reference attribute.
7990 FIXME: GDB does not yet know how to handle dynamic
7991 arrays properly, treat them as arrays with unspecified
7992 length for now.
7993
7994 FIXME: jimb/2003-09-22: GDB does not really know
7995 how to handle arrays of unspecified length
7996 either; we just represent them as zero-length
7997 arrays. Choose an appropriate upper bound given
7998 the lower bound we've computed above. */
7999 high = low - 1;
8000 }
8001 else
8002 high = dwarf2_get_attr_constant_value (attr, 1);
8003 }
8004 else
8005 {
8006 attr = dwarf2_attr (die, DW_AT_count, cu);
8007 if (attr)
8008 {
8009 int count = dwarf2_get_attr_constant_value (attr, 1);
8010 high = low + count - 1;
8011 }
8012 }
8013
8014 /* Dwarf-2 specifications explicitly allows to create subrange types
8015 without specifying a base type.
8016 In that case, the base type must be set to the type of
8017 the lower bound, upper bound or count, in that order, if any of these
8018 three attributes references an object that has a type.
8019 If no base type is found, the Dwarf-2 specifications say that
8020 a signed integer type of size equal to the size of an address should
8021 be used.
8022 For the following C code: `extern char gdb_int [];'
8023 GCC produces an empty range DIE.
8024 FIXME: muller/2010-05-28: Possible references to object for low bound,
8025 high bound or count are not yet handled by this code.
8026 */
8027 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8028 {
8029 struct objfile *objfile = cu->objfile;
8030 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8031 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8032 struct type *int_type = objfile_type (objfile)->builtin_int;
8033
8034 /* Test "int", "long int", and "long long int" objfile types,
8035 and select the first one having a size above or equal to the
8036 architecture address size. */
8037 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8038 base_type = int_type;
8039 else
8040 {
8041 int_type = objfile_type (objfile)->builtin_long;
8042 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8043 base_type = int_type;
8044 else
8045 {
8046 int_type = objfile_type (objfile)->builtin_long_long;
8047 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8048 base_type = int_type;
8049 }
8050 }
8051 }
8052
8053 negative_mask =
8054 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8055 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8056 low |= negative_mask;
8057 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8058 high |= negative_mask;
8059
8060 range_type = create_range_type (NULL, base_type, low, high);
8061
8062 /* Mark arrays with dynamic length at least as an array of unspecified
8063 length. GDB could check the boundary but before it gets implemented at
8064 least allow accessing the array elements. */
8065 if (attr && attr->form == DW_FORM_block1)
8066 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8067
8068 name = dwarf2_name (die, cu);
8069 if (name)
8070 TYPE_NAME (range_type) = name;
8071
8072 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8073 if (attr)
8074 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8075
8076 set_die_type (die, range_type, cu);
8077
8078 /* set_die_type should be already done. */
8079 set_descriptive_type (range_type, die, cu);
8080
8081 return range_type;
8082 }
8083
8084 static struct type *
8085 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8086 {
8087 struct type *type;
8088
8089 /* For now, we only support the C meaning of an unspecified type: void. */
8090
8091 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8092 TYPE_NAME (type) = dwarf2_name (die, cu);
8093
8094 return set_die_type (die, type, cu);
8095 }
8096
8097 /* Trivial hash function for die_info: the hash value of a DIE
8098 is its offset in .debug_info for this objfile. */
8099
8100 static hashval_t
8101 die_hash (const void *item)
8102 {
8103 const struct die_info *die = item;
8104
8105 return die->offset;
8106 }
8107
8108 /* Trivial comparison function for die_info structures: two DIEs
8109 are equal if they have the same offset. */
8110
8111 static int
8112 die_eq (const void *item_lhs, const void *item_rhs)
8113 {
8114 const struct die_info *die_lhs = item_lhs;
8115 const struct die_info *die_rhs = item_rhs;
8116
8117 return die_lhs->offset == die_rhs->offset;
8118 }
8119
8120 /* Read a whole compilation unit into a linked list of dies. */
8121
8122 static struct die_info *
8123 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8124 {
8125 struct die_reader_specs reader_specs;
8126 int read_abbrevs = 0;
8127 struct cleanup *back_to = NULL;
8128 struct die_info *die;
8129
8130 if (cu->dwarf2_abbrevs == NULL)
8131 {
8132 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8133 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8134 read_abbrevs = 1;
8135 }
8136
8137 gdb_assert (cu->die_hash == NULL);
8138 cu->die_hash
8139 = htab_create_alloc_ex (cu->header.length / 12,
8140 die_hash,
8141 die_eq,
8142 NULL,
8143 &cu->comp_unit_obstack,
8144 hashtab_obstack_allocate,
8145 dummy_obstack_deallocate);
8146
8147 init_cu_die_reader (&reader_specs, cu);
8148
8149 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8150
8151 if (read_abbrevs)
8152 do_cleanups (back_to);
8153
8154 return die;
8155 }
8156
8157 /* Main entry point for reading a DIE and all children.
8158 Read the DIE and dump it if requested. */
8159
8160 static struct die_info *
8161 read_die_and_children (const struct die_reader_specs *reader,
8162 gdb_byte *info_ptr,
8163 gdb_byte **new_info_ptr,
8164 struct die_info *parent)
8165 {
8166 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8167 new_info_ptr, parent);
8168
8169 if (dwarf2_die_debug)
8170 {
8171 fprintf_unfiltered (gdb_stdlog,
8172 "\nRead die from %s of %s:\n",
8173 reader->buffer == dwarf2_per_objfile->info.buffer
8174 ? ".debug_info"
8175 : reader->buffer == dwarf2_per_objfile->types.buffer
8176 ? ".debug_types"
8177 : "unknown section",
8178 reader->abfd->filename);
8179 dump_die (result, dwarf2_die_debug);
8180 }
8181
8182 return result;
8183 }
8184
8185 /* Read a single die and all its descendents. Set the die's sibling
8186 field to NULL; set other fields in the die correctly, and set all
8187 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8188 location of the info_ptr after reading all of those dies. PARENT
8189 is the parent of the die in question. */
8190
8191 static struct die_info *
8192 read_die_and_children_1 (const struct die_reader_specs *reader,
8193 gdb_byte *info_ptr,
8194 gdb_byte **new_info_ptr,
8195 struct die_info *parent)
8196 {
8197 struct die_info *die;
8198 gdb_byte *cur_ptr;
8199 int has_children;
8200
8201 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8202 if (die == NULL)
8203 {
8204 *new_info_ptr = cur_ptr;
8205 return NULL;
8206 }
8207 store_in_ref_table (die, reader->cu);
8208
8209 if (has_children)
8210 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8211 else
8212 {
8213 die->child = NULL;
8214 *new_info_ptr = cur_ptr;
8215 }
8216
8217 die->sibling = NULL;
8218 die->parent = parent;
8219 return die;
8220 }
8221
8222 /* Read a die, all of its descendents, and all of its siblings; set
8223 all of the fields of all of the dies correctly. Arguments are as
8224 in read_die_and_children. */
8225
8226 static struct die_info *
8227 read_die_and_siblings (const struct die_reader_specs *reader,
8228 gdb_byte *info_ptr,
8229 gdb_byte **new_info_ptr,
8230 struct die_info *parent)
8231 {
8232 struct die_info *first_die, *last_sibling;
8233 gdb_byte *cur_ptr;
8234
8235 cur_ptr = info_ptr;
8236 first_die = last_sibling = NULL;
8237
8238 while (1)
8239 {
8240 struct die_info *die
8241 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8242
8243 if (die == NULL)
8244 {
8245 *new_info_ptr = cur_ptr;
8246 return first_die;
8247 }
8248
8249 if (!first_die)
8250 first_die = die;
8251 else
8252 last_sibling->sibling = die;
8253
8254 last_sibling = die;
8255 }
8256 }
8257
8258 /* Read the die from the .debug_info section buffer. Set DIEP to
8259 point to a newly allocated die with its information, except for its
8260 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8261 whether the die has children or not. */
8262
8263 static gdb_byte *
8264 read_full_die (const struct die_reader_specs *reader,
8265 struct die_info **diep, gdb_byte *info_ptr,
8266 int *has_children)
8267 {
8268 unsigned int abbrev_number, bytes_read, i, offset;
8269 struct abbrev_info *abbrev;
8270 struct die_info *die;
8271 struct dwarf2_cu *cu = reader->cu;
8272 bfd *abfd = reader->abfd;
8273
8274 offset = info_ptr - reader->buffer;
8275 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8276 info_ptr += bytes_read;
8277 if (!abbrev_number)
8278 {
8279 *diep = NULL;
8280 *has_children = 0;
8281 return info_ptr;
8282 }
8283
8284 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8285 if (!abbrev)
8286 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8287 abbrev_number,
8288 bfd_get_filename (abfd));
8289
8290 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8291 die->offset = offset;
8292 die->tag = abbrev->tag;
8293 die->abbrev = abbrev_number;
8294
8295 die->num_attrs = abbrev->num_attrs;
8296
8297 for (i = 0; i < abbrev->num_attrs; ++i)
8298 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8299 abfd, info_ptr, cu);
8300
8301 *diep = die;
8302 *has_children = abbrev->has_children;
8303 return info_ptr;
8304 }
8305
8306 /* In DWARF version 2, the description of the debugging information is
8307 stored in a separate .debug_abbrev section. Before we read any
8308 dies from a section we read in all abbreviations and install them
8309 in a hash table. This function also sets flags in CU describing
8310 the data found in the abbrev table. */
8311
8312 static void
8313 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8314 {
8315 struct comp_unit_head *cu_header = &cu->header;
8316 gdb_byte *abbrev_ptr;
8317 struct abbrev_info *cur_abbrev;
8318 unsigned int abbrev_number, bytes_read, abbrev_name;
8319 unsigned int abbrev_form, hash_number;
8320 struct attr_abbrev *cur_attrs;
8321 unsigned int allocated_attrs;
8322
8323 /* Initialize dwarf2 abbrevs */
8324 obstack_init (&cu->abbrev_obstack);
8325 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8326 (ABBREV_HASH_SIZE
8327 * sizeof (struct abbrev_info *)));
8328 memset (cu->dwarf2_abbrevs, 0,
8329 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8330
8331 dwarf2_read_section (dwarf2_per_objfile->objfile,
8332 &dwarf2_per_objfile->abbrev);
8333 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8334 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8335 abbrev_ptr += bytes_read;
8336
8337 allocated_attrs = ATTR_ALLOC_CHUNK;
8338 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8339
8340 /* loop until we reach an abbrev number of 0 */
8341 while (abbrev_number)
8342 {
8343 cur_abbrev = dwarf_alloc_abbrev (cu);
8344
8345 /* read in abbrev header */
8346 cur_abbrev->number = abbrev_number;
8347 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8348 abbrev_ptr += bytes_read;
8349 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8350 abbrev_ptr += 1;
8351
8352 if (cur_abbrev->tag == DW_TAG_namespace)
8353 cu->has_namespace_info = 1;
8354
8355 /* now read in declarations */
8356 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8357 abbrev_ptr += bytes_read;
8358 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8359 abbrev_ptr += bytes_read;
8360 while (abbrev_name)
8361 {
8362 if (cur_abbrev->num_attrs == allocated_attrs)
8363 {
8364 allocated_attrs += ATTR_ALLOC_CHUNK;
8365 cur_attrs
8366 = xrealloc (cur_attrs, (allocated_attrs
8367 * sizeof (struct attr_abbrev)));
8368 }
8369
8370 /* Record whether this compilation unit might have
8371 inter-compilation-unit references. If we don't know what form
8372 this attribute will have, then it might potentially be a
8373 DW_FORM_ref_addr, so we conservatively expect inter-CU
8374 references. */
8375
8376 if (abbrev_form == DW_FORM_ref_addr
8377 || abbrev_form == DW_FORM_indirect)
8378 cu->has_form_ref_addr = 1;
8379
8380 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8381 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8382 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8383 abbrev_ptr += bytes_read;
8384 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8385 abbrev_ptr += bytes_read;
8386 }
8387
8388 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8389 (cur_abbrev->num_attrs
8390 * sizeof (struct attr_abbrev)));
8391 memcpy (cur_abbrev->attrs, cur_attrs,
8392 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8393
8394 hash_number = abbrev_number % ABBREV_HASH_SIZE;
8395 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8396 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8397
8398 /* Get next abbreviation.
8399 Under Irix6 the abbreviations for a compilation unit are not
8400 always properly terminated with an abbrev number of 0.
8401 Exit loop if we encounter an abbreviation which we have
8402 already read (which means we are about to read the abbreviations
8403 for the next compile unit) or if the end of the abbreviation
8404 table is reached. */
8405 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8406 >= dwarf2_per_objfile->abbrev.size)
8407 break;
8408 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8409 abbrev_ptr += bytes_read;
8410 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8411 break;
8412 }
8413
8414 xfree (cur_attrs);
8415 }
8416
8417 /* Release the memory used by the abbrev table for a compilation unit. */
8418
8419 static void
8420 dwarf2_free_abbrev_table (void *ptr_to_cu)
8421 {
8422 struct dwarf2_cu *cu = ptr_to_cu;
8423
8424 obstack_free (&cu->abbrev_obstack, NULL);
8425 cu->dwarf2_abbrevs = NULL;
8426 }
8427
8428 /* Lookup an abbrev_info structure in the abbrev hash table. */
8429
8430 static struct abbrev_info *
8431 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8432 {
8433 unsigned int hash_number;
8434 struct abbrev_info *abbrev;
8435
8436 hash_number = number % ABBREV_HASH_SIZE;
8437 abbrev = cu->dwarf2_abbrevs[hash_number];
8438
8439 while (abbrev)
8440 {
8441 if (abbrev->number == number)
8442 return abbrev;
8443 else
8444 abbrev = abbrev->next;
8445 }
8446 return NULL;
8447 }
8448
8449 /* Returns nonzero if TAG represents a type that we might generate a partial
8450 symbol for. */
8451
8452 static int
8453 is_type_tag_for_partial (int tag)
8454 {
8455 switch (tag)
8456 {
8457 #if 0
8458 /* Some types that would be reasonable to generate partial symbols for,
8459 that we don't at present. */
8460 case DW_TAG_array_type:
8461 case DW_TAG_file_type:
8462 case DW_TAG_ptr_to_member_type:
8463 case DW_TAG_set_type:
8464 case DW_TAG_string_type:
8465 case DW_TAG_subroutine_type:
8466 #endif
8467 case DW_TAG_base_type:
8468 case DW_TAG_class_type:
8469 case DW_TAG_interface_type:
8470 case DW_TAG_enumeration_type:
8471 case DW_TAG_structure_type:
8472 case DW_TAG_subrange_type:
8473 case DW_TAG_typedef:
8474 case DW_TAG_union_type:
8475 return 1;
8476 default:
8477 return 0;
8478 }
8479 }
8480
8481 /* Load all DIEs that are interesting for partial symbols into memory. */
8482
8483 static struct partial_die_info *
8484 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8485 int building_psymtab, struct dwarf2_cu *cu)
8486 {
8487 struct partial_die_info *part_die;
8488 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8489 struct abbrev_info *abbrev;
8490 unsigned int bytes_read;
8491 unsigned int load_all = 0;
8492
8493 int nesting_level = 1;
8494
8495 parent_die = NULL;
8496 last_die = NULL;
8497
8498 if (cu->per_cu && cu->per_cu->load_all_dies)
8499 load_all = 1;
8500
8501 cu->partial_dies
8502 = htab_create_alloc_ex (cu->header.length / 12,
8503 partial_die_hash,
8504 partial_die_eq,
8505 NULL,
8506 &cu->comp_unit_obstack,
8507 hashtab_obstack_allocate,
8508 dummy_obstack_deallocate);
8509
8510 part_die = obstack_alloc (&cu->comp_unit_obstack,
8511 sizeof (struct partial_die_info));
8512
8513 while (1)
8514 {
8515 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8516
8517 /* A NULL abbrev means the end of a series of children. */
8518 if (abbrev == NULL)
8519 {
8520 if (--nesting_level == 0)
8521 {
8522 /* PART_DIE was probably the last thing allocated on the
8523 comp_unit_obstack, so we could call obstack_free
8524 here. We don't do that because the waste is small,
8525 and will be cleaned up when we're done with this
8526 compilation unit. This way, we're also more robust
8527 against other users of the comp_unit_obstack. */
8528 return first_die;
8529 }
8530 info_ptr += bytes_read;
8531 last_die = parent_die;
8532 parent_die = parent_die->die_parent;
8533 continue;
8534 }
8535
8536 /* Check for template arguments. We never save these; if
8537 they're seen, we just mark the parent, and go on our way. */
8538 if (parent_die != NULL
8539 && cu->language == language_cplus
8540 && (abbrev->tag == DW_TAG_template_type_param
8541 || abbrev->tag == DW_TAG_template_value_param))
8542 {
8543 parent_die->has_template_arguments = 1;
8544
8545 if (!load_all)
8546 {
8547 /* We don't need a partial DIE for the template argument. */
8548 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8549 cu);
8550 continue;
8551 }
8552 }
8553
8554 /* We only recurse into subprograms looking for template arguments.
8555 Skip their other children. */
8556 if (!load_all
8557 && cu->language == language_cplus
8558 && parent_die != NULL
8559 && parent_die->tag == DW_TAG_subprogram)
8560 {
8561 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8562 continue;
8563 }
8564
8565 /* Check whether this DIE is interesting enough to save. Normally
8566 we would not be interested in members here, but there may be
8567 later variables referencing them via DW_AT_specification (for
8568 static members). */
8569 if (!load_all
8570 && !is_type_tag_for_partial (abbrev->tag)
8571 && abbrev->tag != DW_TAG_enumerator
8572 && abbrev->tag != DW_TAG_subprogram
8573 && abbrev->tag != DW_TAG_lexical_block
8574 && abbrev->tag != DW_TAG_variable
8575 && abbrev->tag != DW_TAG_namespace
8576 && abbrev->tag != DW_TAG_module
8577 && abbrev->tag != DW_TAG_member)
8578 {
8579 /* Otherwise we skip to the next sibling, if any. */
8580 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8581 continue;
8582 }
8583
8584 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8585 buffer, info_ptr, cu);
8586
8587 /* This two-pass algorithm for processing partial symbols has a
8588 high cost in cache pressure. Thus, handle some simple cases
8589 here which cover the majority of C partial symbols. DIEs
8590 which neither have specification tags in them, nor could have
8591 specification tags elsewhere pointing at them, can simply be
8592 processed and discarded.
8593
8594 This segment is also optional; scan_partial_symbols and
8595 add_partial_symbol will handle these DIEs if we chain
8596 them in normally. When compilers which do not emit large
8597 quantities of duplicate debug information are more common,
8598 this code can probably be removed. */
8599
8600 /* Any complete simple types at the top level (pretty much all
8601 of them, for a language without namespaces), can be processed
8602 directly. */
8603 if (parent_die == NULL
8604 && part_die->has_specification == 0
8605 && part_die->is_declaration == 0
8606 && (part_die->tag == DW_TAG_typedef
8607 || part_die->tag == DW_TAG_base_type
8608 || part_die->tag == DW_TAG_subrange_type))
8609 {
8610 if (building_psymtab && part_die->name != NULL)
8611 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8612 VAR_DOMAIN, LOC_TYPEDEF,
8613 &cu->objfile->static_psymbols,
8614 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8615 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8616 continue;
8617 }
8618
8619 /* If we're at the second level, and we're an enumerator, and
8620 our parent has no specification (meaning possibly lives in a
8621 namespace elsewhere), then we can add the partial symbol now
8622 instead of queueing it. */
8623 if (part_die->tag == DW_TAG_enumerator
8624 && parent_die != NULL
8625 && parent_die->die_parent == NULL
8626 && parent_die->tag == DW_TAG_enumeration_type
8627 && parent_die->has_specification == 0)
8628 {
8629 if (part_die->name == NULL)
8630 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8631 else if (building_psymtab)
8632 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8633 VAR_DOMAIN, LOC_CONST,
8634 (cu->language == language_cplus
8635 || cu->language == language_java)
8636 ? &cu->objfile->global_psymbols
8637 : &cu->objfile->static_psymbols,
8638 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8639
8640 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8641 continue;
8642 }
8643
8644 /* We'll save this DIE so link it in. */
8645 part_die->die_parent = parent_die;
8646 part_die->die_sibling = NULL;
8647 part_die->die_child = NULL;
8648
8649 if (last_die && last_die == parent_die)
8650 last_die->die_child = part_die;
8651 else if (last_die)
8652 last_die->die_sibling = part_die;
8653
8654 last_die = part_die;
8655
8656 if (first_die == NULL)
8657 first_die = part_die;
8658
8659 /* Maybe add the DIE to the hash table. Not all DIEs that we
8660 find interesting need to be in the hash table, because we
8661 also have the parent/sibling/child chains; only those that we
8662 might refer to by offset later during partial symbol reading.
8663
8664 For now this means things that might have be the target of a
8665 DW_AT_specification, DW_AT_abstract_origin, or
8666 DW_AT_extension. DW_AT_extension will refer only to
8667 namespaces; DW_AT_abstract_origin refers to functions (and
8668 many things under the function DIE, but we do not recurse
8669 into function DIEs during partial symbol reading) and
8670 possibly variables as well; DW_AT_specification refers to
8671 declarations. Declarations ought to have the DW_AT_declaration
8672 flag. It happens that GCC forgets to put it in sometimes, but
8673 only for functions, not for types.
8674
8675 Adding more things than necessary to the hash table is harmless
8676 except for the performance cost. Adding too few will result in
8677 wasted time in find_partial_die, when we reread the compilation
8678 unit with load_all_dies set. */
8679
8680 if (load_all
8681 || abbrev->tag == DW_TAG_subprogram
8682 || abbrev->tag == DW_TAG_variable
8683 || abbrev->tag == DW_TAG_namespace
8684 || part_die->is_declaration)
8685 {
8686 void **slot;
8687
8688 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8689 part_die->offset, INSERT);
8690 *slot = part_die;
8691 }
8692
8693 part_die = obstack_alloc (&cu->comp_unit_obstack,
8694 sizeof (struct partial_die_info));
8695
8696 /* For some DIEs we want to follow their children (if any). For C
8697 we have no reason to follow the children of structures; for other
8698 languages we have to, so that we can get at method physnames
8699 to infer fully qualified class names, for DW_AT_specification,
8700 and for C++ template arguments. For C++, we also look one level
8701 inside functions to find template arguments (if the name of the
8702 function does not already contain the template arguments).
8703
8704 For Ada, we need to scan the children of subprograms and lexical
8705 blocks as well because Ada allows the definition of nested
8706 entities that could be interesting for the debugger, such as
8707 nested subprograms for instance. */
8708 if (last_die->has_children
8709 && (load_all
8710 || last_die->tag == DW_TAG_namespace
8711 || last_die->tag == DW_TAG_module
8712 || last_die->tag == DW_TAG_enumeration_type
8713 || (cu->language == language_cplus
8714 && last_die->tag == DW_TAG_subprogram
8715 && (last_die->name == NULL
8716 || strchr (last_die->name, '<') == NULL))
8717 || (cu->language != language_c
8718 && (last_die->tag == DW_TAG_class_type
8719 || last_die->tag == DW_TAG_interface_type
8720 || last_die->tag == DW_TAG_structure_type
8721 || last_die->tag == DW_TAG_union_type))
8722 || (cu->language == language_ada
8723 && (last_die->tag == DW_TAG_subprogram
8724 || last_die->tag == DW_TAG_lexical_block))))
8725 {
8726 nesting_level++;
8727 parent_die = last_die;
8728 continue;
8729 }
8730
8731 /* Otherwise we skip to the next sibling, if any. */
8732 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8733
8734 /* Back to the top, do it again. */
8735 }
8736 }
8737
8738 /* Read a minimal amount of information into the minimal die structure. */
8739
8740 static gdb_byte *
8741 read_partial_die (struct partial_die_info *part_die,
8742 struct abbrev_info *abbrev,
8743 unsigned int abbrev_len, bfd *abfd,
8744 gdb_byte *buffer, gdb_byte *info_ptr,
8745 struct dwarf2_cu *cu)
8746 {
8747 unsigned int i;
8748 struct attribute attr;
8749 int has_low_pc_attr = 0;
8750 int has_high_pc_attr = 0;
8751
8752 memset (part_die, 0, sizeof (struct partial_die_info));
8753
8754 part_die->offset = info_ptr - buffer;
8755
8756 info_ptr += abbrev_len;
8757
8758 if (abbrev == NULL)
8759 return info_ptr;
8760
8761 part_die->tag = abbrev->tag;
8762 part_die->has_children = abbrev->has_children;
8763
8764 for (i = 0; i < abbrev->num_attrs; ++i)
8765 {
8766 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8767
8768 /* Store the data if it is of an attribute we want to keep in a
8769 partial symbol table. */
8770 switch (attr.name)
8771 {
8772 case DW_AT_name:
8773 switch (part_die->tag)
8774 {
8775 case DW_TAG_compile_unit:
8776 case DW_TAG_type_unit:
8777 /* Compilation units have a DW_AT_name that is a filename, not
8778 a source language identifier. */
8779 case DW_TAG_enumeration_type:
8780 case DW_TAG_enumerator:
8781 /* These tags always have simple identifiers already; no need
8782 to canonicalize them. */
8783 part_die->name = DW_STRING (&attr);
8784 break;
8785 default:
8786 part_die->name
8787 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8788 &cu->objfile->objfile_obstack);
8789 break;
8790 }
8791 break;
8792 case DW_AT_linkage_name:
8793 case DW_AT_MIPS_linkage_name:
8794 /* Note that both forms of linkage name might appear. We
8795 assume they will be the same, and we only store the last
8796 one we see. */
8797 if (cu->language == language_ada)
8798 part_die->name = DW_STRING (&attr);
8799 break;
8800 case DW_AT_low_pc:
8801 has_low_pc_attr = 1;
8802 part_die->lowpc = DW_ADDR (&attr);
8803 break;
8804 case DW_AT_high_pc:
8805 has_high_pc_attr = 1;
8806 part_die->highpc = DW_ADDR (&attr);
8807 break;
8808 case DW_AT_location:
8809 /* Support the .debug_loc offsets */
8810 if (attr_form_is_block (&attr))
8811 {
8812 part_die->locdesc = DW_BLOCK (&attr);
8813 }
8814 else if (attr_form_is_section_offset (&attr))
8815 {
8816 dwarf2_complex_location_expr_complaint ();
8817 }
8818 else
8819 {
8820 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8821 "partial symbol information");
8822 }
8823 break;
8824 case DW_AT_external:
8825 part_die->is_external = DW_UNSND (&attr);
8826 break;
8827 case DW_AT_declaration:
8828 part_die->is_declaration = DW_UNSND (&attr);
8829 break;
8830 case DW_AT_type:
8831 part_die->has_type = 1;
8832 break;
8833 case DW_AT_abstract_origin:
8834 case DW_AT_specification:
8835 case DW_AT_extension:
8836 part_die->has_specification = 1;
8837 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8838 break;
8839 case DW_AT_sibling:
8840 /* Ignore absolute siblings, they might point outside of
8841 the current compile unit. */
8842 if (attr.form == DW_FORM_ref_addr)
8843 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8844 else
8845 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8846 break;
8847 case DW_AT_byte_size:
8848 part_die->has_byte_size = 1;
8849 break;
8850 case DW_AT_calling_convention:
8851 /* DWARF doesn't provide a way to identify a program's source-level
8852 entry point. DW_AT_calling_convention attributes are only meant
8853 to describe functions' calling conventions.
8854
8855 However, because it's a necessary piece of information in
8856 Fortran, and because DW_CC_program is the only piece of debugging
8857 information whose definition refers to a 'main program' at all,
8858 several compilers have begun marking Fortran main programs with
8859 DW_CC_program --- even when those functions use the standard
8860 calling conventions.
8861
8862 So until DWARF specifies a way to provide this information and
8863 compilers pick up the new representation, we'll support this
8864 practice. */
8865 if (DW_UNSND (&attr) == DW_CC_program
8866 && cu->language == language_fortran)
8867 set_main_name (part_die->name);
8868 break;
8869 default:
8870 break;
8871 }
8872 }
8873
8874 /* When using the GNU linker, .gnu.linkonce. sections are used to
8875 eliminate duplicate copies of functions and vtables and such.
8876 The linker will arbitrarily choose one and discard the others.
8877 The AT_*_pc values for such functions refer to local labels in
8878 these sections. If the section from that file was discarded, the
8879 labels are not in the output, so the relocs get a value of 0.
8880 If this is a discarded function, mark the pc bounds as invalid,
8881 so that GDB will ignore it. */
8882 if (has_low_pc_attr && has_high_pc_attr
8883 && part_die->lowpc < part_die->highpc
8884 && (part_die->lowpc != 0
8885 || dwarf2_per_objfile->has_section_at_zero))
8886 part_die->has_pc_info = 1;
8887
8888 return info_ptr;
8889 }
8890
8891 /* Find a cached partial DIE at OFFSET in CU. */
8892
8893 static struct partial_die_info *
8894 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8895 {
8896 struct partial_die_info *lookup_die = NULL;
8897 struct partial_die_info part_die;
8898
8899 part_die.offset = offset;
8900 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8901
8902 return lookup_die;
8903 }
8904
8905 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8906 except in the case of .debug_types DIEs which do not reference
8907 outside their CU (they do however referencing other types via
8908 DW_FORM_sig8). */
8909
8910 static struct partial_die_info *
8911 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8912 {
8913 struct dwarf2_per_cu_data *per_cu = NULL;
8914 struct partial_die_info *pd = NULL;
8915
8916 if (cu->per_cu->from_debug_types)
8917 {
8918 pd = find_partial_die_in_comp_unit (offset, cu);
8919 if (pd != NULL)
8920 return pd;
8921 goto not_found;
8922 }
8923
8924 if (offset_in_cu_p (&cu->header, offset))
8925 {
8926 pd = find_partial_die_in_comp_unit (offset, cu);
8927 if (pd != NULL)
8928 return pd;
8929 }
8930
8931 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8932
8933 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8934 load_partial_comp_unit (per_cu, cu->objfile);
8935
8936 per_cu->cu->last_used = 0;
8937 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8938
8939 if (pd == NULL && per_cu->load_all_dies == 0)
8940 {
8941 struct cleanup *back_to;
8942 struct partial_die_info comp_unit_die;
8943 struct abbrev_info *abbrev;
8944 unsigned int bytes_read;
8945 char *info_ptr;
8946
8947 per_cu->load_all_dies = 1;
8948
8949 /* Re-read the DIEs. */
8950 back_to = make_cleanup (null_cleanup, 0);
8951 if (per_cu->cu->dwarf2_abbrevs == NULL)
8952 {
8953 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8954 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8955 }
8956 info_ptr = (dwarf2_per_objfile->info.buffer
8957 + per_cu->cu->header.offset
8958 + per_cu->cu->header.first_die_offset);
8959 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8960 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8961 per_cu->cu->objfile->obfd,
8962 dwarf2_per_objfile->info.buffer, info_ptr,
8963 per_cu->cu);
8964 if (comp_unit_die.has_children)
8965 load_partial_dies (per_cu->cu->objfile->obfd,
8966 dwarf2_per_objfile->info.buffer, info_ptr,
8967 0, per_cu->cu);
8968 do_cleanups (back_to);
8969
8970 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8971 }
8972
8973 not_found:
8974
8975 if (pd == NULL)
8976 internal_error (__FILE__, __LINE__,
8977 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8978 offset, bfd_get_filename (cu->objfile->obfd));
8979 return pd;
8980 }
8981
8982 /* Adjust PART_DIE before generating a symbol for it. This function
8983 may set the is_external flag or change the DIE's name. */
8984
8985 static void
8986 fixup_partial_die (struct partial_die_info *part_die,
8987 struct dwarf2_cu *cu)
8988 {
8989 /* If we found a reference attribute and the DIE has no name, try
8990 to find a name in the referred to DIE. */
8991
8992 if (part_die->name == NULL && part_die->has_specification)
8993 {
8994 struct partial_die_info *spec_die;
8995
8996 spec_die = find_partial_die (part_die->spec_offset, cu);
8997
8998 fixup_partial_die (spec_die, cu);
8999
9000 if (spec_die->name)
9001 {
9002 part_die->name = spec_die->name;
9003
9004 /* Copy DW_AT_external attribute if it is set. */
9005 if (spec_die->is_external)
9006 part_die->is_external = spec_die->is_external;
9007 }
9008 }
9009
9010 /* Set default names for some unnamed DIEs. */
9011
9012 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9013 part_die->name = "(anonymous namespace)";
9014
9015 if (part_die->tag == DW_TAG_structure_type
9016 || part_die->tag == DW_TAG_class_type
9017 || part_die->tag == DW_TAG_union_type)
9018 guess_structure_name (part_die, cu);
9019 }
9020
9021 /* Read an attribute value described by an attribute form. */
9022
9023 static gdb_byte *
9024 read_attribute_value (struct attribute *attr, unsigned form,
9025 bfd *abfd, gdb_byte *info_ptr,
9026 struct dwarf2_cu *cu)
9027 {
9028 struct comp_unit_head *cu_header = &cu->header;
9029 unsigned int bytes_read;
9030 struct dwarf_block *blk;
9031
9032 attr->form = form;
9033 switch (form)
9034 {
9035 case DW_FORM_ref_addr:
9036 if (cu->header.version == 2)
9037 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9038 else
9039 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9040 info_ptr += bytes_read;
9041 break;
9042 case DW_FORM_addr:
9043 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9044 info_ptr += bytes_read;
9045 break;
9046 case DW_FORM_block2:
9047 blk = dwarf_alloc_block (cu);
9048 blk->size = read_2_bytes (abfd, info_ptr);
9049 info_ptr += 2;
9050 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9051 info_ptr += blk->size;
9052 DW_BLOCK (attr) = blk;
9053 break;
9054 case DW_FORM_block4:
9055 blk = dwarf_alloc_block (cu);
9056 blk->size = read_4_bytes (abfd, info_ptr);
9057 info_ptr += 4;
9058 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9059 info_ptr += blk->size;
9060 DW_BLOCK (attr) = blk;
9061 break;
9062 case DW_FORM_data2:
9063 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9064 info_ptr += 2;
9065 break;
9066 case DW_FORM_data4:
9067 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9068 info_ptr += 4;
9069 break;
9070 case DW_FORM_data8:
9071 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9072 info_ptr += 8;
9073 break;
9074 case DW_FORM_sec_offset:
9075 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9076 info_ptr += bytes_read;
9077 break;
9078 case DW_FORM_string:
9079 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9080 DW_STRING_IS_CANONICAL (attr) = 0;
9081 info_ptr += bytes_read;
9082 break;
9083 case DW_FORM_strp:
9084 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9085 &bytes_read);
9086 DW_STRING_IS_CANONICAL (attr) = 0;
9087 info_ptr += bytes_read;
9088 break;
9089 case DW_FORM_exprloc:
9090 case DW_FORM_block:
9091 blk = dwarf_alloc_block (cu);
9092 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9093 info_ptr += bytes_read;
9094 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9095 info_ptr += blk->size;
9096 DW_BLOCK (attr) = blk;
9097 break;
9098 case DW_FORM_block1:
9099 blk = dwarf_alloc_block (cu);
9100 blk->size = read_1_byte (abfd, info_ptr);
9101 info_ptr += 1;
9102 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9103 info_ptr += blk->size;
9104 DW_BLOCK (attr) = blk;
9105 break;
9106 case DW_FORM_data1:
9107 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9108 info_ptr += 1;
9109 break;
9110 case DW_FORM_flag:
9111 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9112 info_ptr += 1;
9113 break;
9114 case DW_FORM_flag_present:
9115 DW_UNSND (attr) = 1;
9116 break;
9117 case DW_FORM_sdata:
9118 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9119 info_ptr += bytes_read;
9120 break;
9121 case DW_FORM_udata:
9122 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9123 info_ptr += bytes_read;
9124 break;
9125 case DW_FORM_ref1:
9126 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9127 info_ptr += 1;
9128 break;
9129 case DW_FORM_ref2:
9130 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9131 info_ptr += 2;
9132 break;
9133 case DW_FORM_ref4:
9134 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9135 info_ptr += 4;
9136 break;
9137 case DW_FORM_ref8:
9138 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9139 info_ptr += 8;
9140 break;
9141 case DW_FORM_sig8:
9142 /* Convert the signature to something we can record in DW_UNSND
9143 for later lookup.
9144 NOTE: This is NULL if the type wasn't found. */
9145 DW_SIGNATURED_TYPE (attr) =
9146 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9147 info_ptr += 8;
9148 break;
9149 case DW_FORM_ref_udata:
9150 DW_ADDR (attr) = (cu->header.offset
9151 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9152 info_ptr += bytes_read;
9153 break;
9154 case DW_FORM_indirect:
9155 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9156 info_ptr += bytes_read;
9157 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9158 break;
9159 default:
9160 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9161 dwarf_form_name (form),
9162 bfd_get_filename (abfd));
9163 }
9164
9165 /* We have seen instances where the compiler tried to emit a byte
9166 size attribute of -1 which ended up being encoded as an unsigned
9167 0xffffffff. Although 0xffffffff is technically a valid size value,
9168 an object of this size seems pretty unlikely so we can relatively
9169 safely treat these cases as if the size attribute was invalid and
9170 treat them as zero by default. */
9171 if (attr->name == DW_AT_byte_size
9172 && form == DW_FORM_data4
9173 && DW_UNSND (attr) >= 0xffffffff)
9174 {
9175 complaint
9176 (&symfile_complaints,
9177 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9178 hex_string (DW_UNSND (attr)));
9179 DW_UNSND (attr) = 0;
9180 }
9181
9182 return info_ptr;
9183 }
9184
9185 /* Read an attribute described by an abbreviated attribute. */
9186
9187 static gdb_byte *
9188 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9189 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9190 {
9191 attr->name = abbrev->name;
9192 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9193 }
9194
9195 /* read dwarf information from a buffer */
9196
9197 static unsigned int
9198 read_1_byte (bfd *abfd, gdb_byte *buf)
9199 {
9200 return bfd_get_8 (abfd, buf);
9201 }
9202
9203 static int
9204 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9205 {
9206 return bfd_get_signed_8 (abfd, buf);
9207 }
9208
9209 static unsigned int
9210 read_2_bytes (bfd *abfd, gdb_byte *buf)
9211 {
9212 return bfd_get_16 (abfd, buf);
9213 }
9214
9215 static int
9216 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9217 {
9218 return bfd_get_signed_16 (abfd, buf);
9219 }
9220
9221 static unsigned int
9222 read_4_bytes (bfd *abfd, gdb_byte *buf)
9223 {
9224 return bfd_get_32 (abfd, buf);
9225 }
9226
9227 static int
9228 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9229 {
9230 return bfd_get_signed_32 (abfd, buf);
9231 }
9232
9233 static ULONGEST
9234 read_8_bytes (bfd *abfd, gdb_byte *buf)
9235 {
9236 return bfd_get_64 (abfd, buf);
9237 }
9238
9239 static CORE_ADDR
9240 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9241 unsigned int *bytes_read)
9242 {
9243 struct comp_unit_head *cu_header = &cu->header;
9244 CORE_ADDR retval = 0;
9245
9246 if (cu_header->signed_addr_p)
9247 {
9248 switch (cu_header->addr_size)
9249 {
9250 case 2:
9251 retval = bfd_get_signed_16 (abfd, buf);
9252 break;
9253 case 4:
9254 retval = bfd_get_signed_32 (abfd, buf);
9255 break;
9256 case 8:
9257 retval = bfd_get_signed_64 (abfd, buf);
9258 break;
9259 default:
9260 internal_error (__FILE__, __LINE__,
9261 _("read_address: bad switch, signed [in module %s]"),
9262 bfd_get_filename (abfd));
9263 }
9264 }
9265 else
9266 {
9267 switch (cu_header->addr_size)
9268 {
9269 case 2:
9270 retval = bfd_get_16 (abfd, buf);
9271 break;
9272 case 4:
9273 retval = bfd_get_32 (abfd, buf);
9274 break;
9275 case 8:
9276 retval = bfd_get_64 (abfd, buf);
9277 break;
9278 default:
9279 internal_error (__FILE__, __LINE__,
9280 _("read_address: bad switch, unsigned [in module %s]"),
9281 bfd_get_filename (abfd));
9282 }
9283 }
9284
9285 *bytes_read = cu_header->addr_size;
9286 return retval;
9287 }
9288
9289 /* Read the initial length from a section. The (draft) DWARF 3
9290 specification allows the initial length to take up either 4 bytes
9291 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9292 bytes describe the length and all offsets will be 8 bytes in length
9293 instead of 4.
9294
9295 An older, non-standard 64-bit format is also handled by this
9296 function. The older format in question stores the initial length
9297 as an 8-byte quantity without an escape value. Lengths greater
9298 than 2^32 aren't very common which means that the initial 4 bytes
9299 is almost always zero. Since a length value of zero doesn't make
9300 sense for the 32-bit format, this initial zero can be considered to
9301 be an escape value which indicates the presence of the older 64-bit
9302 format. As written, the code can't detect (old format) lengths
9303 greater than 4GB. If it becomes necessary to handle lengths
9304 somewhat larger than 4GB, we could allow other small values (such
9305 as the non-sensical values of 1, 2, and 3) to also be used as
9306 escape values indicating the presence of the old format.
9307
9308 The value returned via bytes_read should be used to increment the
9309 relevant pointer after calling read_initial_length().
9310
9311 [ Note: read_initial_length() and read_offset() are based on the
9312 document entitled "DWARF Debugging Information Format", revision
9313 3, draft 8, dated November 19, 2001. This document was obtained
9314 from:
9315
9316 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9317
9318 This document is only a draft and is subject to change. (So beware.)
9319
9320 Details regarding the older, non-standard 64-bit format were
9321 determined empirically by examining 64-bit ELF files produced by
9322 the SGI toolchain on an IRIX 6.5 machine.
9323
9324 - Kevin, July 16, 2002
9325 ] */
9326
9327 static LONGEST
9328 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9329 {
9330 LONGEST length = bfd_get_32 (abfd, buf);
9331
9332 if (length == 0xffffffff)
9333 {
9334 length = bfd_get_64 (abfd, buf + 4);
9335 *bytes_read = 12;
9336 }
9337 else if (length == 0)
9338 {
9339 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
9340 length = bfd_get_64 (abfd, buf);
9341 *bytes_read = 8;
9342 }
9343 else
9344 {
9345 *bytes_read = 4;
9346 }
9347
9348 return length;
9349 }
9350
9351 /* Cover function for read_initial_length.
9352 Returns the length of the object at BUF, and stores the size of the
9353 initial length in *BYTES_READ and stores the size that offsets will be in
9354 *OFFSET_SIZE.
9355 If the initial length size is not equivalent to that specified in
9356 CU_HEADER then issue a complaint.
9357 This is useful when reading non-comp-unit headers. */
9358
9359 static LONGEST
9360 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9361 const struct comp_unit_head *cu_header,
9362 unsigned int *bytes_read,
9363 unsigned int *offset_size)
9364 {
9365 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9366
9367 gdb_assert (cu_header->initial_length_size == 4
9368 || cu_header->initial_length_size == 8
9369 || cu_header->initial_length_size == 12);
9370
9371 if (cu_header->initial_length_size != *bytes_read)
9372 complaint (&symfile_complaints,
9373 _("intermixed 32-bit and 64-bit DWARF sections"));
9374
9375 *offset_size = (*bytes_read == 4) ? 4 : 8;
9376 return length;
9377 }
9378
9379 /* Read an offset from the data stream. The size of the offset is
9380 given by cu_header->offset_size. */
9381
9382 static LONGEST
9383 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9384 unsigned int *bytes_read)
9385 {
9386 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9387
9388 *bytes_read = cu_header->offset_size;
9389 return offset;
9390 }
9391
9392 /* Read an offset from the data stream. */
9393
9394 static LONGEST
9395 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9396 {
9397 LONGEST retval = 0;
9398
9399 switch (offset_size)
9400 {
9401 case 4:
9402 retval = bfd_get_32 (abfd, buf);
9403 break;
9404 case 8:
9405 retval = bfd_get_64 (abfd, buf);
9406 break;
9407 default:
9408 internal_error (__FILE__, __LINE__,
9409 _("read_offset_1: bad switch [in module %s]"),
9410 bfd_get_filename (abfd));
9411 }
9412
9413 return retval;
9414 }
9415
9416 static gdb_byte *
9417 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9418 {
9419 /* If the size of a host char is 8 bits, we can return a pointer
9420 to the buffer, otherwise we have to copy the data to a buffer
9421 allocated on the temporary obstack. */
9422 gdb_assert (HOST_CHAR_BIT == 8);
9423 return buf;
9424 }
9425
9426 static char *
9427 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9428 {
9429 /* If the size of a host char is 8 bits, we can return a pointer
9430 to the string, otherwise we have to copy the string to a buffer
9431 allocated on the temporary obstack. */
9432 gdb_assert (HOST_CHAR_BIT == 8);
9433 if (*buf == '\0')
9434 {
9435 *bytes_read_ptr = 1;
9436 return NULL;
9437 }
9438 *bytes_read_ptr = strlen ((char *) buf) + 1;
9439 return (char *) buf;
9440 }
9441
9442 static char *
9443 read_indirect_string (bfd *abfd, gdb_byte *buf,
9444 const struct comp_unit_head *cu_header,
9445 unsigned int *bytes_read_ptr)
9446 {
9447 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9448
9449 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9450 if (dwarf2_per_objfile->str.buffer == NULL)
9451 {
9452 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9453 bfd_get_filename (abfd));
9454 return NULL;
9455 }
9456 if (str_offset >= dwarf2_per_objfile->str.size)
9457 {
9458 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9459 bfd_get_filename (abfd));
9460 return NULL;
9461 }
9462 gdb_assert (HOST_CHAR_BIT == 8);
9463 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9464 return NULL;
9465 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9466 }
9467
9468 static unsigned long
9469 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9470 {
9471 unsigned long result;
9472 unsigned int num_read;
9473 int i, shift;
9474 unsigned char byte;
9475
9476 result = 0;
9477 shift = 0;
9478 num_read = 0;
9479 i = 0;
9480 while (1)
9481 {
9482 byte = bfd_get_8 (abfd, buf);
9483 buf++;
9484 num_read++;
9485 result |= ((unsigned long)(byte & 127) << shift);
9486 if ((byte & 128) == 0)
9487 {
9488 break;
9489 }
9490 shift += 7;
9491 }
9492 *bytes_read_ptr = num_read;
9493 return result;
9494 }
9495
9496 static long
9497 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9498 {
9499 long result;
9500 int i, shift, num_read;
9501 unsigned char byte;
9502
9503 result = 0;
9504 shift = 0;
9505 num_read = 0;
9506 i = 0;
9507 while (1)
9508 {
9509 byte = bfd_get_8 (abfd, buf);
9510 buf++;
9511 num_read++;
9512 result |= ((long)(byte & 127) << shift);
9513 shift += 7;
9514 if ((byte & 128) == 0)
9515 {
9516 break;
9517 }
9518 }
9519 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9520 result |= -(((long)1) << shift);
9521 *bytes_read_ptr = num_read;
9522 return result;
9523 }
9524
9525 /* Return a pointer to just past the end of an LEB128 number in BUF. */
9526
9527 static gdb_byte *
9528 skip_leb128 (bfd *abfd, gdb_byte *buf)
9529 {
9530 int byte;
9531
9532 while (1)
9533 {
9534 byte = bfd_get_8 (abfd, buf);
9535 buf++;
9536 if ((byte & 128) == 0)
9537 return buf;
9538 }
9539 }
9540
9541 static void
9542 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9543 {
9544 switch (lang)
9545 {
9546 case DW_LANG_C89:
9547 case DW_LANG_C99:
9548 case DW_LANG_C:
9549 cu->language = language_c;
9550 break;
9551 case DW_LANG_C_plus_plus:
9552 cu->language = language_cplus;
9553 break;
9554 case DW_LANG_D:
9555 cu->language = language_d;
9556 break;
9557 case DW_LANG_Fortran77:
9558 case DW_LANG_Fortran90:
9559 case DW_LANG_Fortran95:
9560 cu->language = language_fortran;
9561 break;
9562 case DW_LANG_Mips_Assembler:
9563 cu->language = language_asm;
9564 break;
9565 case DW_LANG_Java:
9566 cu->language = language_java;
9567 break;
9568 case DW_LANG_Ada83:
9569 case DW_LANG_Ada95:
9570 cu->language = language_ada;
9571 break;
9572 case DW_LANG_Modula2:
9573 cu->language = language_m2;
9574 break;
9575 case DW_LANG_Pascal83:
9576 cu->language = language_pascal;
9577 break;
9578 case DW_LANG_ObjC:
9579 cu->language = language_objc;
9580 break;
9581 case DW_LANG_Cobol74:
9582 case DW_LANG_Cobol85:
9583 default:
9584 cu->language = language_minimal;
9585 break;
9586 }
9587 cu->language_defn = language_def (cu->language);
9588 }
9589
9590 /* Return the named attribute or NULL if not there. */
9591
9592 static struct attribute *
9593 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9594 {
9595 unsigned int i;
9596 struct attribute *spec = NULL;
9597
9598 for (i = 0; i < die->num_attrs; ++i)
9599 {
9600 if (die->attrs[i].name == name)
9601 return &die->attrs[i];
9602 if (die->attrs[i].name == DW_AT_specification
9603 || die->attrs[i].name == DW_AT_abstract_origin)
9604 spec = &die->attrs[i];
9605 }
9606
9607 if (spec)
9608 {
9609 die = follow_die_ref (die, spec, &cu);
9610 return dwarf2_attr (die, name, cu);
9611 }
9612
9613 return NULL;
9614 }
9615
9616 /* Return the named attribute or NULL if not there,
9617 but do not follow DW_AT_specification, etc.
9618 This is for use in contexts where we're reading .debug_types dies.
9619 Following DW_AT_specification, DW_AT_abstract_origin will take us
9620 back up the chain, and we want to go down. */
9621
9622 static struct attribute *
9623 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9624 struct dwarf2_cu *cu)
9625 {
9626 unsigned int i;
9627
9628 for (i = 0; i < die->num_attrs; ++i)
9629 if (die->attrs[i].name == name)
9630 return &die->attrs[i];
9631
9632 return NULL;
9633 }
9634
9635 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9636 and holds a non-zero value. This function should only be used for
9637 DW_FORM_flag or DW_FORM_flag_present attributes. */
9638
9639 static int
9640 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9641 {
9642 struct attribute *attr = dwarf2_attr (die, name, cu);
9643
9644 return (attr && DW_UNSND (attr));
9645 }
9646
9647 static int
9648 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9649 {
9650 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9651 which value is non-zero. However, we have to be careful with
9652 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9653 (via dwarf2_flag_true_p) follows this attribute. So we may
9654 end up accidently finding a declaration attribute that belongs
9655 to a different DIE referenced by the specification attribute,
9656 even though the given DIE does not have a declaration attribute. */
9657 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9658 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9659 }
9660
9661 /* Return the die giving the specification for DIE, if there is
9662 one. *SPEC_CU is the CU containing DIE on input, and the CU
9663 containing the return value on output. If there is no
9664 specification, but there is an abstract origin, that is
9665 returned. */
9666
9667 static struct die_info *
9668 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9669 {
9670 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9671 *spec_cu);
9672
9673 if (spec_attr == NULL)
9674 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9675
9676 if (spec_attr == NULL)
9677 return NULL;
9678 else
9679 return follow_die_ref (die, spec_attr, spec_cu);
9680 }
9681
9682 /* Free the line_header structure *LH, and any arrays and strings it
9683 refers to. */
9684 static void
9685 free_line_header (struct line_header *lh)
9686 {
9687 if (lh->standard_opcode_lengths)
9688 xfree (lh->standard_opcode_lengths);
9689
9690 /* Remember that all the lh->file_names[i].name pointers are
9691 pointers into debug_line_buffer, and don't need to be freed. */
9692 if (lh->file_names)
9693 xfree (lh->file_names);
9694
9695 /* Similarly for the include directory names. */
9696 if (lh->include_dirs)
9697 xfree (lh->include_dirs);
9698
9699 xfree (lh);
9700 }
9701
9702
9703 /* Add an entry to LH's include directory table. */
9704 static void
9705 add_include_dir (struct line_header *lh, char *include_dir)
9706 {
9707 /* Grow the array if necessary. */
9708 if (lh->include_dirs_size == 0)
9709 {
9710 lh->include_dirs_size = 1; /* for testing */
9711 lh->include_dirs = xmalloc (lh->include_dirs_size
9712 * sizeof (*lh->include_dirs));
9713 }
9714 else if (lh->num_include_dirs >= lh->include_dirs_size)
9715 {
9716 lh->include_dirs_size *= 2;
9717 lh->include_dirs = xrealloc (lh->include_dirs,
9718 (lh->include_dirs_size
9719 * sizeof (*lh->include_dirs)));
9720 }
9721
9722 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9723 }
9724
9725
9726 /* Add an entry to LH's file name table. */
9727 static void
9728 add_file_name (struct line_header *lh,
9729 char *name,
9730 unsigned int dir_index,
9731 unsigned int mod_time,
9732 unsigned int length)
9733 {
9734 struct file_entry *fe;
9735
9736 /* Grow the array if necessary. */
9737 if (lh->file_names_size == 0)
9738 {
9739 lh->file_names_size = 1; /* for testing */
9740 lh->file_names = xmalloc (lh->file_names_size
9741 * sizeof (*lh->file_names));
9742 }
9743 else if (lh->num_file_names >= lh->file_names_size)
9744 {
9745 lh->file_names_size *= 2;
9746 lh->file_names = xrealloc (lh->file_names,
9747 (lh->file_names_size
9748 * sizeof (*lh->file_names)));
9749 }
9750
9751 fe = &lh->file_names[lh->num_file_names++];
9752 fe->name = name;
9753 fe->dir_index = dir_index;
9754 fe->mod_time = mod_time;
9755 fe->length = length;
9756 fe->included_p = 0;
9757 fe->symtab = NULL;
9758 }
9759
9760
9761 /* Read the statement program header starting at OFFSET in
9762 .debug_line, according to the endianness of ABFD. Return a pointer
9763 to a struct line_header, allocated using xmalloc.
9764
9765 NOTE: the strings in the include directory and file name tables of
9766 the returned object point into debug_line_buffer, and must not be
9767 freed. */
9768 static struct line_header *
9769 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9770 struct dwarf2_cu *cu)
9771 {
9772 struct cleanup *back_to;
9773 struct line_header *lh;
9774 gdb_byte *line_ptr;
9775 unsigned int bytes_read, offset_size;
9776 int i;
9777 char *cur_dir, *cur_file;
9778
9779 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9780 if (dwarf2_per_objfile->line.buffer == NULL)
9781 {
9782 complaint (&symfile_complaints, _("missing .debug_line section"));
9783 return 0;
9784 }
9785
9786 /* Make sure that at least there's room for the total_length field.
9787 That could be 12 bytes long, but we're just going to fudge that. */
9788 if (offset + 4 >= dwarf2_per_objfile->line.size)
9789 {
9790 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9791 return 0;
9792 }
9793
9794 lh = xmalloc (sizeof (*lh));
9795 memset (lh, 0, sizeof (*lh));
9796 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9797 (void *) lh);
9798
9799 line_ptr = dwarf2_per_objfile->line.buffer + offset;
9800
9801 /* Read in the header. */
9802 lh->total_length =
9803 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9804 &bytes_read, &offset_size);
9805 line_ptr += bytes_read;
9806 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9807 + dwarf2_per_objfile->line.size))
9808 {
9809 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9810 return 0;
9811 }
9812 lh->statement_program_end = line_ptr + lh->total_length;
9813 lh->version = read_2_bytes (abfd, line_ptr);
9814 line_ptr += 2;
9815 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9816 line_ptr += offset_size;
9817 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9818 line_ptr += 1;
9819 if (lh->version >= 4)
9820 {
9821 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9822 line_ptr += 1;
9823 }
9824 else
9825 lh->maximum_ops_per_instruction = 1;
9826
9827 if (lh->maximum_ops_per_instruction == 0)
9828 {
9829 lh->maximum_ops_per_instruction = 1;
9830 complaint (&symfile_complaints,
9831 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9832 }
9833
9834 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9835 line_ptr += 1;
9836 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9837 line_ptr += 1;
9838 lh->line_range = read_1_byte (abfd, line_ptr);
9839 line_ptr += 1;
9840 lh->opcode_base = read_1_byte (abfd, line_ptr);
9841 line_ptr += 1;
9842 lh->standard_opcode_lengths
9843 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9844
9845 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9846 for (i = 1; i < lh->opcode_base; ++i)
9847 {
9848 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9849 line_ptr += 1;
9850 }
9851
9852 /* Read directory table. */
9853 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9854 {
9855 line_ptr += bytes_read;
9856 add_include_dir (lh, cur_dir);
9857 }
9858 line_ptr += bytes_read;
9859
9860 /* Read file name table. */
9861 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9862 {
9863 unsigned int dir_index, mod_time, length;
9864
9865 line_ptr += bytes_read;
9866 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9867 line_ptr += bytes_read;
9868 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9869 line_ptr += bytes_read;
9870 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9871 line_ptr += bytes_read;
9872
9873 add_file_name (lh, cur_file, dir_index, mod_time, length);
9874 }
9875 line_ptr += bytes_read;
9876 lh->statement_program_start = line_ptr;
9877
9878 if (line_ptr > (dwarf2_per_objfile->line.buffer
9879 + dwarf2_per_objfile->line.size))
9880 complaint (&symfile_complaints,
9881 _("line number info header doesn't fit in `.debug_line' section"));
9882
9883 discard_cleanups (back_to);
9884 return lh;
9885 }
9886
9887 /* This function exists to work around a bug in certain compilers
9888 (particularly GCC 2.95), in which the first line number marker of a
9889 function does not show up until after the prologue, right before
9890 the second line number marker. This function shifts ADDRESS down
9891 to the beginning of the function if necessary, and is called on
9892 addresses passed to record_line. */
9893
9894 static CORE_ADDR
9895 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9896 {
9897 struct function_range *fn;
9898
9899 /* Find the function_range containing address. */
9900 if (!cu->first_fn)
9901 return address;
9902
9903 if (!cu->cached_fn)
9904 cu->cached_fn = cu->first_fn;
9905
9906 fn = cu->cached_fn;
9907 while (fn)
9908 if (fn->lowpc <= address && fn->highpc > address)
9909 goto found;
9910 else
9911 fn = fn->next;
9912
9913 fn = cu->first_fn;
9914 while (fn && fn != cu->cached_fn)
9915 if (fn->lowpc <= address && fn->highpc > address)
9916 goto found;
9917 else
9918 fn = fn->next;
9919
9920 return address;
9921
9922 found:
9923 if (fn->seen_line)
9924 return address;
9925 if (address != fn->lowpc)
9926 complaint (&symfile_complaints,
9927 _("misplaced first line number at 0x%lx for '%s'"),
9928 (unsigned long) address, fn->name);
9929 fn->seen_line = 1;
9930 return fn->lowpc;
9931 }
9932
9933 /* Subroutine of dwarf_decode_lines to simplify it.
9934 Return the file name of the psymtab for included file FILE_INDEX
9935 in line header LH of PST.
9936 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
9937 If space for the result is malloc'd, it will be freed by a cleanup.
9938 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
9939
9940 static char *
9941 psymtab_include_file_name (const struct line_header *lh, int file_index,
9942 const struct partial_symtab *pst,
9943 const char *comp_dir)
9944 {
9945 const struct file_entry fe = lh->file_names [file_index];
9946 char *include_name = fe.name;
9947 char *include_name_to_compare = include_name;
9948 char *dir_name = NULL;
9949 char *pst_filename;
9950 int file_is_pst;
9951
9952 if (fe.dir_index)
9953 dir_name = lh->include_dirs[fe.dir_index - 1];
9954
9955 if (!IS_ABSOLUTE_PATH (include_name)
9956 && (dir_name != NULL || comp_dir != NULL))
9957 {
9958 /* Avoid creating a duplicate psymtab for PST.
9959 We do this by comparing INCLUDE_NAME and PST_FILENAME.
9960 Before we do the comparison, however, we need to account
9961 for DIR_NAME and COMP_DIR.
9962 First prepend dir_name (if non-NULL). If we still don't
9963 have an absolute path prepend comp_dir (if non-NULL).
9964 However, the directory we record in the include-file's
9965 psymtab does not contain COMP_DIR (to match the
9966 corresponding symtab(s)).
9967
9968 Example:
9969
9970 bash$ cd /tmp
9971 bash$ gcc -g ./hello.c
9972 include_name = "hello.c"
9973 dir_name = "."
9974 DW_AT_comp_dir = comp_dir = "/tmp"
9975 DW_AT_name = "./hello.c" */
9976
9977 if (dir_name != NULL)
9978 {
9979 include_name = concat (dir_name, SLASH_STRING,
9980 include_name, (char *)NULL);
9981 include_name_to_compare = include_name;
9982 make_cleanup (xfree, include_name);
9983 }
9984 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
9985 {
9986 include_name_to_compare = concat (comp_dir, SLASH_STRING,
9987 include_name, (char *)NULL);
9988 }
9989 }
9990
9991 pst_filename = pst->filename;
9992 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9993 {
9994 pst_filename = concat (pst->dirname, SLASH_STRING,
9995 pst_filename, (char *)NULL);
9996 }
9997
9998 file_is_pst = strcmp (include_name_to_compare, pst_filename) == 0;
9999
10000 if (include_name_to_compare != include_name)
10001 xfree (include_name_to_compare);
10002 if (pst_filename != pst->filename)
10003 xfree (pst_filename);
10004
10005 if (file_is_pst)
10006 return NULL;
10007 return include_name;
10008 }
10009
10010 /* Decode the Line Number Program (LNP) for the given line_header
10011 structure and CU. The actual information extracted and the type
10012 of structures created from the LNP depends on the value of PST.
10013
10014 1. If PST is NULL, then this procedure uses the data from the program
10015 to create all necessary symbol tables, and their linetables.
10016
10017 2. If PST is not NULL, this procedure reads the program to determine
10018 the list of files included by the unit represented by PST, and
10019 builds all the associated partial symbol tables.
10020
10021 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10022 It is used for relative paths in the line table.
10023 NOTE: When processing partial symtabs (pst != NULL),
10024 comp_dir == pst->dirname.
10025
10026 NOTE: It is important that psymtabs have the same file name (via strcmp)
10027 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10028 symtab we don't use it in the name of the psymtabs we create.
10029 E.g. expand_line_sal requires this when finding psymtabs to expand.
10030 A good testcase for this is mb-inline.exp. */
10031
10032 static void
10033 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
10034 struct dwarf2_cu *cu, struct partial_symtab *pst)
10035 {
10036 gdb_byte *line_ptr, *extended_end;
10037 gdb_byte *line_end;
10038 unsigned int bytes_read, extended_len;
10039 unsigned char op_code, extended_op, adj_opcode;
10040 CORE_ADDR baseaddr;
10041 struct objfile *objfile = cu->objfile;
10042 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10043 const int decode_for_pst_p = (pst != NULL);
10044 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10045
10046 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10047
10048 line_ptr = lh->statement_program_start;
10049 line_end = lh->statement_program_end;
10050
10051 /* Read the statement sequences until there's nothing left. */
10052 while (line_ptr < line_end)
10053 {
10054 /* state machine registers */
10055 CORE_ADDR address = 0;
10056 unsigned int file = 1;
10057 unsigned int line = 1;
10058 unsigned int column = 0;
10059 int is_stmt = lh->default_is_stmt;
10060 int basic_block = 0;
10061 int end_sequence = 0;
10062 CORE_ADDR addr;
10063 unsigned char op_index = 0;
10064
10065 if (!decode_for_pst_p && lh->num_file_names >= file)
10066 {
10067 /* Start a subfile for the current file of the state machine. */
10068 /* lh->include_dirs and lh->file_names are 0-based, but the
10069 directory and file name numbers in the statement program
10070 are 1-based. */
10071 struct file_entry *fe = &lh->file_names[file - 1];
10072 char *dir = NULL;
10073
10074 if (fe->dir_index)
10075 dir = lh->include_dirs[fe->dir_index - 1];
10076
10077 dwarf2_start_subfile (fe->name, dir, comp_dir);
10078 }
10079
10080 /* Decode the table. */
10081 while (!end_sequence)
10082 {
10083 op_code = read_1_byte (abfd, line_ptr);
10084 line_ptr += 1;
10085 if (line_ptr > line_end)
10086 {
10087 dwarf2_debug_line_missing_end_sequence_complaint ();
10088 break;
10089 }
10090
10091 if (op_code >= lh->opcode_base)
10092 {
10093 /* Special operand. */
10094 adj_opcode = op_code - lh->opcode_base;
10095 address += (((op_index + (adj_opcode / lh->line_range))
10096 / lh->maximum_ops_per_instruction)
10097 * lh->minimum_instruction_length);
10098 op_index = ((op_index + (adj_opcode / lh->line_range))
10099 % lh->maximum_ops_per_instruction);
10100 line += lh->line_base + (adj_opcode % lh->line_range);
10101 if (lh->num_file_names < file || file == 0)
10102 dwarf2_debug_line_missing_file_complaint ();
10103 /* For now we ignore lines not starting on an
10104 instruction boundary. */
10105 else if (op_index == 0)
10106 {
10107 lh->file_names[file - 1].included_p = 1;
10108 if (!decode_for_pst_p && is_stmt)
10109 {
10110 if (last_subfile != current_subfile)
10111 {
10112 addr = gdbarch_addr_bits_remove (gdbarch, address);
10113 if (last_subfile)
10114 record_line (last_subfile, 0, addr);
10115 last_subfile = current_subfile;
10116 }
10117 /* Append row to matrix using current values. */
10118 addr = check_cu_functions (address, cu);
10119 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10120 record_line (current_subfile, line, addr);
10121 }
10122 }
10123 basic_block = 0;
10124 }
10125 else switch (op_code)
10126 {
10127 case DW_LNS_extended_op:
10128 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10129 line_ptr += bytes_read;
10130 extended_end = line_ptr + extended_len;
10131 extended_op = read_1_byte (abfd, line_ptr);
10132 line_ptr += 1;
10133 switch (extended_op)
10134 {
10135 case DW_LNE_end_sequence:
10136 end_sequence = 1;
10137 break;
10138 case DW_LNE_set_address:
10139 address = read_address (abfd, line_ptr, cu, &bytes_read);
10140 op_index = 0;
10141 line_ptr += bytes_read;
10142 address += baseaddr;
10143 break;
10144 case DW_LNE_define_file:
10145 {
10146 char *cur_file;
10147 unsigned int dir_index, mod_time, length;
10148
10149 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
10150 line_ptr += bytes_read;
10151 dir_index =
10152 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10153 line_ptr += bytes_read;
10154 mod_time =
10155 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10156 line_ptr += bytes_read;
10157 length =
10158 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10159 line_ptr += bytes_read;
10160 add_file_name (lh, cur_file, dir_index, mod_time, length);
10161 }
10162 break;
10163 case DW_LNE_set_discriminator:
10164 /* The discriminator is not interesting to the debugger;
10165 just ignore it. */
10166 line_ptr = extended_end;
10167 break;
10168 default:
10169 complaint (&symfile_complaints,
10170 _("mangled .debug_line section"));
10171 return;
10172 }
10173 /* Make sure that we parsed the extended op correctly. If e.g.
10174 we expected a different address size than the producer used,
10175 we may have read the wrong number of bytes. */
10176 if (line_ptr != extended_end)
10177 {
10178 complaint (&symfile_complaints,
10179 _("mangled .debug_line section"));
10180 return;
10181 }
10182 break;
10183 case DW_LNS_copy:
10184 if (lh->num_file_names < file || file == 0)
10185 dwarf2_debug_line_missing_file_complaint ();
10186 else
10187 {
10188 lh->file_names[file - 1].included_p = 1;
10189 if (!decode_for_pst_p && is_stmt)
10190 {
10191 if (last_subfile != current_subfile)
10192 {
10193 addr = gdbarch_addr_bits_remove (gdbarch, address);
10194 if (last_subfile)
10195 record_line (last_subfile, 0, addr);
10196 last_subfile = current_subfile;
10197 }
10198 addr = check_cu_functions (address, cu);
10199 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10200 record_line (current_subfile, line, addr);
10201 }
10202 }
10203 basic_block = 0;
10204 break;
10205 case DW_LNS_advance_pc:
10206 {
10207 CORE_ADDR adjust
10208 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10209
10210 address += (((op_index + adjust)
10211 / lh->maximum_ops_per_instruction)
10212 * lh->minimum_instruction_length);
10213 op_index = ((op_index + adjust)
10214 % lh->maximum_ops_per_instruction);
10215 line_ptr += bytes_read;
10216 }
10217 break;
10218 case DW_LNS_advance_line:
10219 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10220 line_ptr += bytes_read;
10221 break;
10222 case DW_LNS_set_file:
10223 {
10224 /* The arrays lh->include_dirs and lh->file_names are
10225 0-based, but the directory and file name numbers in
10226 the statement program are 1-based. */
10227 struct file_entry *fe;
10228 char *dir = NULL;
10229
10230 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10231 line_ptr += bytes_read;
10232 if (lh->num_file_names < file || file == 0)
10233 dwarf2_debug_line_missing_file_complaint ();
10234 else
10235 {
10236 fe = &lh->file_names[file - 1];
10237 if (fe->dir_index)
10238 dir = lh->include_dirs[fe->dir_index - 1];
10239 if (!decode_for_pst_p)
10240 {
10241 last_subfile = current_subfile;
10242 dwarf2_start_subfile (fe->name, dir, comp_dir);
10243 }
10244 }
10245 }
10246 break;
10247 case DW_LNS_set_column:
10248 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10249 line_ptr += bytes_read;
10250 break;
10251 case DW_LNS_negate_stmt:
10252 is_stmt = (!is_stmt);
10253 break;
10254 case DW_LNS_set_basic_block:
10255 basic_block = 1;
10256 break;
10257 /* Add to the address register of the state machine the
10258 address increment value corresponding to special opcode
10259 255. I.e., this value is scaled by the minimum
10260 instruction length since special opcode 255 would have
10261 scaled the the increment. */
10262 case DW_LNS_const_add_pc:
10263 {
10264 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10265
10266 address += (((op_index + adjust)
10267 / lh->maximum_ops_per_instruction)
10268 * lh->minimum_instruction_length);
10269 op_index = ((op_index + adjust)
10270 % lh->maximum_ops_per_instruction);
10271 }
10272 break;
10273 case DW_LNS_fixed_advance_pc:
10274 address += read_2_bytes (abfd, line_ptr);
10275 op_index = 0;
10276 line_ptr += 2;
10277 break;
10278 default:
10279 {
10280 /* Unknown standard opcode, ignore it. */
10281 int i;
10282
10283 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10284 {
10285 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10286 line_ptr += bytes_read;
10287 }
10288 }
10289 }
10290 }
10291 if (lh->num_file_names < file || file == 0)
10292 dwarf2_debug_line_missing_file_complaint ();
10293 else
10294 {
10295 lh->file_names[file - 1].included_p = 1;
10296 if (!decode_for_pst_p)
10297 {
10298 addr = gdbarch_addr_bits_remove (gdbarch, address);
10299 record_line (current_subfile, 0, addr);
10300 }
10301 }
10302 }
10303
10304 if (decode_for_pst_p)
10305 {
10306 int file_index;
10307
10308 /* Now that we're done scanning the Line Header Program, we can
10309 create the psymtab of each included file. */
10310 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10311 if (lh->file_names[file_index].included_p == 1)
10312 {
10313 char *include_name =
10314 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10315 if (include_name != NULL)
10316 dwarf2_create_include_psymtab (include_name, pst, objfile);
10317 }
10318 }
10319 else
10320 {
10321 /* Make sure a symtab is created for every file, even files
10322 which contain only variables (i.e. no code with associated
10323 line numbers). */
10324
10325 int i;
10326 struct file_entry *fe;
10327
10328 for (i = 0; i < lh->num_file_names; i++)
10329 {
10330 char *dir = NULL;
10331
10332 fe = &lh->file_names[i];
10333 if (fe->dir_index)
10334 dir = lh->include_dirs[fe->dir_index - 1];
10335 dwarf2_start_subfile (fe->name, dir, comp_dir);
10336
10337 /* Skip the main file; we don't need it, and it must be
10338 allocated last, so that it will show up before the
10339 non-primary symtabs in the objfile's symtab list. */
10340 if (current_subfile == first_subfile)
10341 continue;
10342
10343 if (current_subfile->symtab == NULL)
10344 current_subfile->symtab = allocate_symtab (current_subfile->name,
10345 cu->objfile);
10346 fe->symtab = current_subfile->symtab;
10347 }
10348 }
10349 }
10350
10351 /* Start a subfile for DWARF. FILENAME is the name of the file and
10352 DIRNAME the name of the source directory which contains FILENAME
10353 or NULL if not known. COMP_DIR is the compilation directory for the
10354 linetable's compilation unit or NULL if not known.
10355 This routine tries to keep line numbers from identical absolute and
10356 relative file names in a common subfile.
10357
10358 Using the `list' example from the GDB testsuite, which resides in
10359 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10360 of /srcdir/list0.c yields the following debugging information for list0.c:
10361
10362 DW_AT_name: /srcdir/list0.c
10363 DW_AT_comp_dir: /compdir
10364 files.files[0].name: list0.h
10365 files.files[0].dir: /srcdir
10366 files.files[1].name: list0.c
10367 files.files[1].dir: /srcdir
10368
10369 The line number information for list0.c has to end up in a single
10370 subfile, so that `break /srcdir/list0.c:1' works as expected.
10371 start_subfile will ensure that this happens provided that we pass the
10372 concatenation of files.files[1].dir and files.files[1].name as the
10373 subfile's name. */
10374
10375 static void
10376 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
10377 {
10378 char *fullname;
10379
10380 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10381 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10382 second argument to start_subfile. To be consistent, we do the
10383 same here. In order not to lose the line information directory,
10384 we concatenate it to the filename when it makes sense.
10385 Note that the Dwarf3 standard says (speaking of filenames in line
10386 information): ``The directory index is ignored for file names
10387 that represent full path names''. Thus ignoring dirname in the
10388 `else' branch below isn't an issue. */
10389
10390 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10391 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10392 else
10393 fullname = filename;
10394
10395 start_subfile (fullname, comp_dir);
10396
10397 if (fullname != filename)
10398 xfree (fullname);
10399 }
10400
10401 static void
10402 var_decode_location (struct attribute *attr, struct symbol *sym,
10403 struct dwarf2_cu *cu)
10404 {
10405 struct objfile *objfile = cu->objfile;
10406 struct comp_unit_head *cu_header = &cu->header;
10407
10408 /* NOTE drow/2003-01-30: There used to be a comment and some special
10409 code here to turn a symbol with DW_AT_external and a
10410 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10411 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10412 with some versions of binutils) where shared libraries could have
10413 relocations against symbols in their debug information - the
10414 minimal symbol would have the right address, but the debug info
10415 would not. It's no longer necessary, because we will explicitly
10416 apply relocations when we read in the debug information now. */
10417
10418 /* A DW_AT_location attribute with no contents indicates that a
10419 variable has been optimized away. */
10420 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10421 {
10422 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10423 return;
10424 }
10425
10426 /* Handle one degenerate form of location expression specially, to
10427 preserve GDB's previous behavior when section offsets are
10428 specified. If this is just a DW_OP_addr then mark this symbol
10429 as LOC_STATIC. */
10430
10431 if (attr_form_is_block (attr)
10432 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10433 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10434 {
10435 unsigned int dummy;
10436
10437 SYMBOL_VALUE_ADDRESS (sym) =
10438 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10439 SYMBOL_CLASS (sym) = LOC_STATIC;
10440 fixup_symbol_section (sym, objfile);
10441 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10442 SYMBOL_SECTION (sym));
10443 return;
10444 }
10445
10446 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10447 expression evaluator, and use LOC_COMPUTED only when necessary
10448 (i.e. when the value of a register or memory location is
10449 referenced, or a thread-local block, etc.). Then again, it might
10450 not be worthwhile. I'm assuming that it isn't unless performance
10451 or memory numbers show me otherwise. */
10452
10453 dwarf2_symbol_mark_computed (attr, sym, cu);
10454 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10455 }
10456
10457 /* Given a pointer to a DWARF information entry, figure out if we need
10458 to make a symbol table entry for it, and if so, create a new entry
10459 and return a pointer to it.
10460 If TYPE is NULL, determine symbol type from the die, otherwise
10461 used the passed type.
10462 If SPACE is not NULL, use it to hold the new symbol. If it is
10463 NULL, allocate a new symbol on the objfile's obstack. */
10464
10465 static struct symbol *
10466 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10467 struct symbol *space)
10468 {
10469 struct objfile *objfile = cu->objfile;
10470 struct symbol *sym = NULL;
10471 char *name;
10472 struct attribute *attr = NULL;
10473 struct attribute *attr2 = NULL;
10474 CORE_ADDR baseaddr;
10475 struct pending **list_to_add = NULL;
10476
10477 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10478
10479 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10480
10481 name = dwarf2_name (die, cu);
10482 if (name)
10483 {
10484 const char *linkagename;
10485 int suppress_add = 0;
10486
10487 if (space)
10488 sym = space;
10489 else
10490 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10491 OBJSTAT (objfile, n_syms++);
10492
10493 /* Cache this symbol's name and the name's demangled form (if any). */
10494 SYMBOL_SET_LANGUAGE (sym, cu->language);
10495 linkagename = dwarf2_physname (name, die, cu);
10496 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10497
10498 /* Fortran does not have mangling standard and the mangling does differ
10499 between gfortran, iFort etc. */
10500 if (cu->language == language_fortran
10501 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10502 symbol_set_demangled_name (&(sym->ginfo),
10503 (char *) dwarf2_full_name (name, die, cu),
10504 NULL);
10505
10506 /* Default assumptions.
10507 Use the passed type or decode it from the die. */
10508 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10509 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10510 if (type != NULL)
10511 SYMBOL_TYPE (sym) = type;
10512 else
10513 SYMBOL_TYPE (sym) = die_type (die, cu);
10514 attr = dwarf2_attr (die,
10515 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10516 cu);
10517 if (attr)
10518 {
10519 SYMBOL_LINE (sym) = DW_UNSND (attr);
10520 }
10521
10522 attr = dwarf2_attr (die,
10523 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10524 cu);
10525 if (attr)
10526 {
10527 int file_index = DW_UNSND (attr);
10528
10529 if (cu->line_header == NULL
10530 || file_index > cu->line_header->num_file_names)
10531 complaint (&symfile_complaints,
10532 _("file index out of range"));
10533 else if (file_index > 0)
10534 {
10535 struct file_entry *fe;
10536
10537 fe = &cu->line_header->file_names[file_index - 1];
10538 SYMBOL_SYMTAB (sym) = fe->symtab;
10539 }
10540 }
10541
10542 switch (die->tag)
10543 {
10544 case DW_TAG_label:
10545 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10546 if (attr)
10547 {
10548 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10549 }
10550 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10551 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10552 SYMBOL_CLASS (sym) = LOC_LABEL;
10553 add_symbol_to_list (sym, cu->list_in_scope);
10554 break;
10555 case DW_TAG_subprogram:
10556 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10557 finish_block. */
10558 SYMBOL_CLASS (sym) = LOC_BLOCK;
10559 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10560 if ((attr2 && (DW_UNSND (attr2) != 0))
10561 || cu->language == language_ada)
10562 {
10563 /* Subprograms marked external are stored as a global symbol.
10564 Ada subprograms, whether marked external or not, are always
10565 stored as a global symbol, because we want to be able to
10566 access them globally. For instance, we want to be able
10567 to break on a nested subprogram without having to
10568 specify the context. */
10569 list_to_add = &global_symbols;
10570 }
10571 else
10572 {
10573 list_to_add = cu->list_in_scope;
10574 }
10575 break;
10576 case DW_TAG_inlined_subroutine:
10577 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10578 finish_block. */
10579 SYMBOL_CLASS (sym) = LOC_BLOCK;
10580 SYMBOL_INLINED (sym) = 1;
10581 /* Do not add the symbol to any lists. It will be found via
10582 BLOCK_FUNCTION from the blockvector. */
10583 break;
10584 case DW_TAG_template_value_param:
10585 suppress_add = 1;
10586 /* Fall through. */
10587 case DW_TAG_variable:
10588 case DW_TAG_member:
10589 /* Compilation with minimal debug info may result in variables
10590 with missing type entries. Change the misleading `void' type
10591 to something sensible. */
10592 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10593 SYMBOL_TYPE (sym)
10594 = objfile_type (objfile)->nodebug_data_symbol;
10595
10596 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10597 /* In the case of DW_TAG_member, we should only be called for
10598 static const members. */
10599 if (die->tag == DW_TAG_member)
10600 {
10601 /* dwarf2_add_field uses die_is_declaration,
10602 so we do the same. */
10603 gdb_assert (die_is_declaration (die, cu));
10604 gdb_assert (attr);
10605 }
10606 if (attr)
10607 {
10608 dwarf2_const_value (attr, sym, cu);
10609 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10610 if (!suppress_add)
10611 {
10612 if (attr2 && (DW_UNSND (attr2) != 0))
10613 list_to_add = &global_symbols;
10614 else
10615 list_to_add = cu->list_in_scope;
10616 }
10617 break;
10618 }
10619 attr = dwarf2_attr (die, DW_AT_location, cu);
10620 if (attr)
10621 {
10622 var_decode_location (attr, sym, cu);
10623 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10624 if (SYMBOL_CLASS (sym) == LOC_STATIC
10625 && SYMBOL_VALUE_ADDRESS (sym) == 0
10626 && !dwarf2_per_objfile->has_section_at_zero)
10627 {
10628 /* When a static variable is eliminated by the linker,
10629 the corresponding debug information is not stripped
10630 out, but the variable address is set to null;
10631 do not add such variables into symbol table. */
10632 }
10633 else if (attr2 && (DW_UNSND (attr2) != 0))
10634 {
10635 /* Workaround gfortran PR debug/40040 - it uses
10636 DW_AT_location for variables in -fPIC libraries which may
10637 get overriden by other libraries/executable and get
10638 a different address. Resolve it by the minimal symbol
10639 which may come from inferior's executable using copy
10640 relocation. Make this workaround only for gfortran as for
10641 other compilers GDB cannot guess the minimal symbol
10642 Fortran mangling kind. */
10643 if (cu->language == language_fortran && die->parent
10644 && die->parent->tag == DW_TAG_module
10645 && cu->producer
10646 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10647 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10648
10649 /* A variable with DW_AT_external is never static,
10650 but it may be block-scoped. */
10651 list_to_add = (cu->list_in_scope == &file_symbols
10652 ? &global_symbols : cu->list_in_scope);
10653 }
10654 else
10655 list_to_add = cu->list_in_scope;
10656 }
10657 else
10658 {
10659 /* We do not know the address of this symbol.
10660 If it is an external symbol and we have type information
10661 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10662 The address of the variable will then be determined from
10663 the minimal symbol table whenever the variable is
10664 referenced. */
10665 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10666 if (attr2 && (DW_UNSND (attr2) != 0)
10667 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10668 {
10669 /* A variable with DW_AT_external is never static, but it
10670 may be block-scoped. */
10671 list_to_add = (cu->list_in_scope == &file_symbols
10672 ? &global_symbols : cu->list_in_scope);
10673
10674 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10675 }
10676 else if (!die_is_declaration (die, cu))
10677 {
10678 /* Use the default LOC_OPTIMIZED_OUT class. */
10679 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10680 if (!suppress_add)
10681 list_to_add = cu->list_in_scope;
10682 }
10683 }
10684 break;
10685 case DW_TAG_formal_parameter:
10686 /* If we are inside a function, mark this as an argument. If
10687 not, we might be looking at an argument to an inlined function
10688 when we do not have enough information to show inlined frames;
10689 pretend it's a local variable in that case so that the user can
10690 still see it. */
10691 if (context_stack_depth > 0
10692 && context_stack[context_stack_depth - 1].name != NULL)
10693 SYMBOL_IS_ARGUMENT (sym) = 1;
10694 attr = dwarf2_attr (die, DW_AT_location, cu);
10695 if (attr)
10696 {
10697 var_decode_location (attr, sym, cu);
10698 }
10699 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10700 if (attr)
10701 {
10702 dwarf2_const_value (attr, sym, cu);
10703 }
10704 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10705 if (attr && DW_UNSND (attr))
10706 {
10707 struct type *ref_type;
10708
10709 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10710 SYMBOL_TYPE (sym) = ref_type;
10711 }
10712
10713 list_to_add = cu->list_in_scope;
10714 break;
10715 case DW_TAG_unspecified_parameters:
10716 /* From varargs functions; gdb doesn't seem to have any
10717 interest in this information, so just ignore it for now.
10718 (FIXME?) */
10719 break;
10720 case DW_TAG_template_type_param:
10721 suppress_add = 1;
10722 /* Fall through. */
10723 case DW_TAG_class_type:
10724 case DW_TAG_interface_type:
10725 case DW_TAG_structure_type:
10726 case DW_TAG_union_type:
10727 case DW_TAG_set_type:
10728 case DW_TAG_enumeration_type:
10729 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10730 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10731
10732 {
10733 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10734 really ever be static objects: otherwise, if you try
10735 to, say, break of a class's method and you're in a file
10736 which doesn't mention that class, it won't work unless
10737 the check for all static symbols in lookup_symbol_aux
10738 saves you. See the OtherFileClass tests in
10739 gdb.c++/namespace.exp. */
10740
10741 if (!suppress_add)
10742 {
10743 list_to_add = (cu->list_in_scope == &file_symbols
10744 && (cu->language == language_cplus
10745 || cu->language == language_java)
10746 ? &global_symbols : cu->list_in_scope);
10747
10748 /* The semantics of C++ state that "struct foo {
10749 ... }" also defines a typedef for "foo". A Java
10750 class declaration also defines a typedef for the
10751 class. */
10752 if (cu->language == language_cplus
10753 || cu->language == language_java
10754 || cu->language == language_ada)
10755 {
10756 /* The symbol's name is already allocated along
10757 with this objfile, so we don't need to
10758 duplicate it for the type. */
10759 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10760 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10761 }
10762 }
10763 }
10764 break;
10765 case DW_TAG_typedef:
10766 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10767 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10768 list_to_add = cu->list_in_scope;
10769 break;
10770 case DW_TAG_base_type:
10771 case DW_TAG_subrange_type:
10772 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10773 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10774 list_to_add = cu->list_in_scope;
10775 break;
10776 case DW_TAG_enumerator:
10777 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10778 if (attr)
10779 {
10780 dwarf2_const_value (attr, sym, cu);
10781 }
10782 {
10783 /* NOTE: carlton/2003-11-10: See comment above in the
10784 DW_TAG_class_type, etc. block. */
10785
10786 list_to_add = (cu->list_in_scope == &file_symbols
10787 && (cu->language == language_cplus
10788 || cu->language == language_java)
10789 ? &global_symbols : cu->list_in_scope);
10790 }
10791 break;
10792 case DW_TAG_namespace:
10793 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10794 list_to_add = &global_symbols;
10795 break;
10796 default:
10797 /* Not a tag we recognize. Hopefully we aren't processing
10798 trash data, but since we must specifically ignore things
10799 we don't recognize, there is nothing else we should do at
10800 this point. */
10801 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10802 dwarf_tag_name (die->tag));
10803 break;
10804 }
10805
10806 if (suppress_add)
10807 {
10808 sym->hash_next = objfile->template_symbols;
10809 objfile->template_symbols = sym;
10810 list_to_add = NULL;
10811 }
10812
10813 if (list_to_add != NULL)
10814 add_symbol_to_list (sym, list_to_add);
10815
10816 /* For the benefit of old versions of GCC, check for anonymous
10817 namespaces based on the demangled name. */
10818 if (!processing_has_namespace_info
10819 && cu->language == language_cplus)
10820 cp_scan_for_anonymous_namespaces (sym);
10821 }
10822 return (sym);
10823 }
10824
10825 /* A wrapper for new_symbol_full that always allocates a new symbol. */
10826
10827 static struct symbol *
10828 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10829 {
10830 return new_symbol_full (die, type, cu, NULL);
10831 }
10832
10833 /* Given an attr with a DW_FORM_dataN value in host byte order,
10834 zero-extend it as appropriate for the symbol's type. The DWARF
10835 standard (v4) is not entirely clear about the meaning of using
10836 DW_FORM_dataN for a constant with a signed type, where the type is
10837 wider than the data. The conclusion of a discussion on the DWARF
10838 list was that this is unspecified. We choose to always zero-extend
10839 because that is the interpretation long in use by GCC. */
10840
10841 static gdb_byte *
10842 dwarf2_const_value_data (struct attribute *attr, struct type *type,
10843 const char *name, struct obstack *obstack,
10844 struct dwarf2_cu *cu, long *value, int bits)
10845 {
10846 struct objfile *objfile = cu->objfile;
10847 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10848 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10849 LONGEST l = DW_UNSND (attr);
10850
10851 if (bits < sizeof (*value) * 8)
10852 {
10853 l &= ((LONGEST) 1 << bits) - 1;
10854 *value = l;
10855 }
10856 else if (bits == sizeof (*value) * 8)
10857 *value = l;
10858 else
10859 {
10860 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10861 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10862 return bytes;
10863 }
10864
10865 return NULL;
10866 }
10867
10868 /* Read a constant value from an attribute. Either set *VALUE, or if
10869 the value does not fit in *VALUE, set *BYTES - either already
10870 allocated on the objfile obstack, or newly allocated on OBSTACK,
10871 or, set *BATON, if we translated the constant to a location
10872 expression. */
10873
10874 static void
10875 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10876 const char *name, struct obstack *obstack,
10877 struct dwarf2_cu *cu,
10878 long *value, gdb_byte **bytes,
10879 struct dwarf2_locexpr_baton **baton)
10880 {
10881 struct objfile *objfile = cu->objfile;
10882 struct comp_unit_head *cu_header = &cu->header;
10883 struct dwarf_block *blk;
10884 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10885 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10886
10887 *value = 0;
10888 *bytes = NULL;
10889 *baton = NULL;
10890
10891 switch (attr->form)
10892 {
10893 case DW_FORM_addr:
10894 {
10895 gdb_byte *data;
10896
10897 if (TYPE_LENGTH (type) != cu_header->addr_size)
10898 dwarf2_const_value_length_mismatch_complaint (name,
10899 cu_header->addr_size,
10900 TYPE_LENGTH (type));
10901 /* Symbols of this form are reasonably rare, so we just
10902 piggyback on the existing location code rather than writing
10903 a new implementation of symbol_computed_ops. */
10904 *baton = obstack_alloc (&objfile->objfile_obstack,
10905 sizeof (struct dwarf2_locexpr_baton));
10906 (*baton)->per_cu = cu->per_cu;
10907 gdb_assert ((*baton)->per_cu);
10908
10909 (*baton)->size = 2 + cu_header->addr_size;
10910 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10911 (*baton)->data = data;
10912
10913 data[0] = DW_OP_addr;
10914 store_unsigned_integer (&data[1], cu_header->addr_size,
10915 byte_order, DW_ADDR (attr));
10916 data[cu_header->addr_size + 1] = DW_OP_stack_value;
10917 }
10918 break;
10919 case DW_FORM_string:
10920 case DW_FORM_strp:
10921 /* DW_STRING is already allocated on the objfile obstack, point
10922 directly to it. */
10923 *bytes = (gdb_byte *) DW_STRING (attr);
10924 break;
10925 case DW_FORM_block1:
10926 case DW_FORM_block2:
10927 case DW_FORM_block4:
10928 case DW_FORM_block:
10929 case DW_FORM_exprloc:
10930 blk = DW_BLOCK (attr);
10931 if (TYPE_LENGTH (type) != blk->size)
10932 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10933 TYPE_LENGTH (type));
10934 *bytes = blk->data;
10935 break;
10936
10937 /* The DW_AT_const_value attributes are supposed to carry the
10938 symbol's value "represented as it would be on the target
10939 architecture." By the time we get here, it's already been
10940 converted to host endianness, so we just need to sign- or
10941 zero-extend it as appropriate. */
10942 case DW_FORM_data1:
10943 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
10944 break;
10945 case DW_FORM_data2:
10946 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
10947 break;
10948 case DW_FORM_data4:
10949 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
10950 break;
10951 case DW_FORM_data8:
10952 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
10953 break;
10954
10955 case DW_FORM_sdata:
10956 *value = DW_SND (attr);
10957 break;
10958
10959 case DW_FORM_udata:
10960 *value = DW_UNSND (attr);
10961 break;
10962
10963 default:
10964 complaint (&symfile_complaints,
10965 _("unsupported const value attribute form: '%s'"),
10966 dwarf_form_name (attr->form));
10967 *value = 0;
10968 break;
10969 }
10970 }
10971
10972
10973 /* Copy constant value from an attribute to a symbol. */
10974
10975 static void
10976 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10977 struct dwarf2_cu *cu)
10978 {
10979 struct objfile *objfile = cu->objfile;
10980 struct comp_unit_head *cu_header = &cu->header;
10981 long value;
10982 gdb_byte *bytes;
10983 struct dwarf2_locexpr_baton *baton;
10984
10985 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10986 SYMBOL_PRINT_NAME (sym),
10987 &objfile->objfile_obstack, cu,
10988 &value, &bytes, &baton);
10989
10990 if (baton != NULL)
10991 {
10992 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10993 SYMBOL_LOCATION_BATON (sym) = baton;
10994 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10995 }
10996 else if (bytes != NULL)
10997 {
10998 SYMBOL_VALUE_BYTES (sym) = bytes;
10999 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11000 }
11001 else
11002 {
11003 SYMBOL_VALUE (sym) = value;
11004 SYMBOL_CLASS (sym) = LOC_CONST;
11005 }
11006 }
11007
11008 /* Return the type of the die in question using its DW_AT_type attribute. */
11009
11010 static struct type *
11011 die_type (struct die_info *die, struct dwarf2_cu *cu)
11012 {
11013 struct attribute *type_attr;
11014
11015 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11016 if (!type_attr)
11017 {
11018 /* A missing DW_AT_type represents a void type. */
11019 return objfile_type (cu->objfile)->builtin_void;
11020 }
11021
11022 return lookup_die_type (die, type_attr, cu);
11023 }
11024
11025 /* True iff CU's producer generates GNAT Ada auxiliary information
11026 that allows to find parallel types through that information instead
11027 of having to do expensive parallel lookups by type name. */
11028
11029 static int
11030 need_gnat_info (struct dwarf2_cu *cu)
11031 {
11032 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11033 of GNAT produces this auxiliary information, without any indication
11034 that it is produced. Part of enhancing the FSF version of GNAT
11035 to produce that information will be to put in place an indicator
11036 that we can use in order to determine whether the descriptive type
11037 info is available or not. One suggestion that has been made is
11038 to use a new attribute, attached to the CU die. For now, assume
11039 that the descriptive type info is not available. */
11040 return 0;
11041 }
11042
11043 /* Return the auxiliary type of the die in question using its
11044 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11045 attribute is not present. */
11046
11047 static struct type *
11048 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11049 {
11050 struct attribute *type_attr;
11051
11052 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11053 if (!type_attr)
11054 return NULL;
11055
11056 return lookup_die_type (die, type_attr, cu);
11057 }
11058
11059 /* If DIE has a descriptive_type attribute, then set the TYPE's
11060 descriptive type accordingly. */
11061
11062 static void
11063 set_descriptive_type (struct type *type, struct die_info *die,
11064 struct dwarf2_cu *cu)
11065 {
11066 struct type *descriptive_type = die_descriptive_type (die, cu);
11067
11068 if (descriptive_type)
11069 {
11070 ALLOCATE_GNAT_AUX_TYPE (type);
11071 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11072 }
11073 }
11074
11075 /* Return the containing type of the die in question using its
11076 DW_AT_containing_type attribute. */
11077
11078 static struct type *
11079 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11080 {
11081 struct attribute *type_attr;
11082
11083 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11084 if (!type_attr)
11085 error (_("Dwarf Error: Problem turning containing type into gdb type "
11086 "[in module %s]"), cu->objfile->name);
11087
11088 return lookup_die_type (die, type_attr, cu);
11089 }
11090
11091 /* Look up the type of DIE in CU using its type attribute ATTR.
11092 If there is no type substitute an error marker. */
11093
11094 static struct type *
11095 lookup_die_type (struct die_info *die, struct attribute *attr,
11096 struct dwarf2_cu *cu)
11097 {
11098 struct type *this_type;
11099
11100 /* First see if we have it cached. */
11101
11102 if (is_ref_attr (attr))
11103 {
11104 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11105
11106 this_type = get_die_type_at_offset (offset, cu->per_cu);
11107 }
11108 else if (attr->form == DW_FORM_sig8)
11109 {
11110 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11111 struct dwarf2_cu *sig_cu;
11112 unsigned int offset;
11113
11114 /* sig_type will be NULL if the signatured type is missing from
11115 the debug info. */
11116 if (sig_type == NULL)
11117 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11118 "at 0x%x [in module %s]"),
11119 die->offset, cu->objfile->name);
11120
11121 gdb_assert (sig_type->per_cu.from_debug_types);
11122 offset = sig_type->offset + sig_type->type_offset;
11123 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11124 }
11125 else
11126 {
11127 dump_die_for_error (die);
11128 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11129 dwarf_attr_name (attr->name), cu->objfile->name);
11130 }
11131
11132 /* If not cached we need to read it in. */
11133
11134 if (this_type == NULL)
11135 {
11136 struct die_info *type_die;
11137 struct dwarf2_cu *type_cu = cu;
11138
11139 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11140 /* If the type is cached, we should have found it above. */
11141 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11142 this_type = read_type_die_1 (type_die, type_cu);
11143 }
11144
11145 /* If we still don't have a type use an error marker. */
11146
11147 if (this_type == NULL)
11148 {
11149 char *message, *saved;
11150
11151 /* read_type_die already issued a complaint. */
11152 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11153 cu->objfile->name,
11154 cu->header.offset,
11155 die->offset);
11156 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11157 message, strlen (message));
11158 xfree (message);
11159
11160 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11161 }
11162
11163 return this_type;
11164 }
11165
11166 /* Return the type in DIE, CU.
11167 Returns NULL for invalid types.
11168
11169 This first does a lookup in the appropriate type_hash table,
11170 and only reads the die in if necessary.
11171
11172 NOTE: This can be called when reading in partial or full symbols. */
11173
11174 static struct type *
11175 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11176 {
11177 struct type *this_type;
11178
11179 this_type = get_die_type (die, cu);
11180 if (this_type)
11181 return this_type;
11182
11183 return read_type_die_1 (die, cu);
11184 }
11185
11186 /* Read the type in DIE, CU.
11187 Returns NULL for invalid types. */
11188
11189 static struct type *
11190 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11191 {
11192 struct type *this_type = NULL;
11193
11194 switch (die->tag)
11195 {
11196 case DW_TAG_class_type:
11197 case DW_TAG_interface_type:
11198 case DW_TAG_structure_type:
11199 case DW_TAG_union_type:
11200 this_type = read_structure_type (die, cu);
11201 break;
11202 case DW_TAG_enumeration_type:
11203 this_type = read_enumeration_type (die, cu);
11204 break;
11205 case DW_TAG_subprogram:
11206 case DW_TAG_subroutine_type:
11207 case DW_TAG_inlined_subroutine:
11208 this_type = read_subroutine_type (die, cu);
11209 break;
11210 case DW_TAG_array_type:
11211 this_type = read_array_type (die, cu);
11212 break;
11213 case DW_TAG_set_type:
11214 this_type = read_set_type (die, cu);
11215 break;
11216 case DW_TAG_pointer_type:
11217 this_type = read_tag_pointer_type (die, cu);
11218 break;
11219 case DW_TAG_ptr_to_member_type:
11220 this_type = read_tag_ptr_to_member_type (die, cu);
11221 break;
11222 case DW_TAG_reference_type:
11223 this_type = read_tag_reference_type (die, cu);
11224 break;
11225 case DW_TAG_const_type:
11226 this_type = read_tag_const_type (die, cu);
11227 break;
11228 case DW_TAG_volatile_type:
11229 this_type = read_tag_volatile_type (die, cu);
11230 break;
11231 case DW_TAG_string_type:
11232 this_type = read_tag_string_type (die, cu);
11233 break;
11234 case DW_TAG_typedef:
11235 this_type = read_typedef (die, cu);
11236 break;
11237 case DW_TAG_subrange_type:
11238 this_type = read_subrange_type (die, cu);
11239 break;
11240 case DW_TAG_base_type:
11241 this_type = read_base_type (die, cu);
11242 break;
11243 case DW_TAG_unspecified_type:
11244 this_type = read_unspecified_type (die, cu);
11245 break;
11246 case DW_TAG_namespace:
11247 this_type = read_namespace_type (die, cu);
11248 break;
11249 case DW_TAG_module:
11250 this_type = read_module_type (die, cu);
11251 break;
11252 default:
11253 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
11254 dwarf_tag_name (die->tag));
11255 break;
11256 }
11257
11258 return this_type;
11259 }
11260
11261 /* Return the name of the namespace/class that DIE is defined within,
11262 or "" if we can't tell. The caller should not xfree the result.
11263
11264 For example, if we're within the method foo() in the following
11265 code:
11266
11267 namespace N {
11268 class C {
11269 void foo () {
11270 }
11271 };
11272 }
11273
11274 then determine_prefix on foo's die will return "N::C". */
11275
11276 static char *
11277 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11278 {
11279 struct die_info *parent, *spec_die;
11280 struct dwarf2_cu *spec_cu;
11281 struct type *parent_type;
11282
11283 if (cu->language != language_cplus && cu->language != language_java
11284 && cu->language != language_fortran)
11285 return "";
11286
11287 /* We have to be careful in the presence of DW_AT_specification.
11288 For example, with GCC 3.4, given the code
11289
11290 namespace N {
11291 void foo() {
11292 // Definition of N::foo.
11293 }
11294 }
11295
11296 then we'll have a tree of DIEs like this:
11297
11298 1: DW_TAG_compile_unit
11299 2: DW_TAG_namespace // N
11300 3: DW_TAG_subprogram // declaration of N::foo
11301 4: DW_TAG_subprogram // definition of N::foo
11302 DW_AT_specification // refers to die #3
11303
11304 Thus, when processing die #4, we have to pretend that we're in
11305 the context of its DW_AT_specification, namely the contex of die
11306 #3. */
11307 spec_cu = cu;
11308 spec_die = die_specification (die, &spec_cu);
11309 if (spec_die == NULL)
11310 parent = die->parent;
11311 else
11312 {
11313 parent = spec_die->parent;
11314 cu = spec_cu;
11315 }
11316
11317 if (parent == NULL)
11318 return "";
11319 else if (parent->building_fullname)
11320 {
11321 const char *name;
11322 const char *parent_name;
11323
11324 /* It has been seen on RealView 2.2 built binaries,
11325 DW_TAG_template_type_param types actually _defined_ as
11326 children of the parent class:
11327
11328 enum E {};
11329 template class <class Enum> Class{};
11330 Class<enum E> class_e;
11331
11332 1: DW_TAG_class_type (Class)
11333 2: DW_TAG_enumeration_type (E)
11334 3: DW_TAG_enumerator (enum1:0)
11335 3: DW_TAG_enumerator (enum2:1)
11336 ...
11337 2: DW_TAG_template_type_param
11338 DW_AT_type DW_FORM_ref_udata (E)
11339
11340 Besides being broken debug info, it can put GDB into an
11341 infinite loop. Consider:
11342
11343 When we're building the full name for Class<E>, we'll start
11344 at Class, and go look over its template type parameters,
11345 finding E. We'll then try to build the full name of E, and
11346 reach here. We're now trying to build the full name of E,
11347 and look over the parent DIE for containing scope. In the
11348 broken case, if we followed the parent DIE of E, we'd again
11349 find Class, and once again go look at its template type
11350 arguments, etc., etc. Simply don't consider such parent die
11351 as source-level parent of this die (it can't be, the language
11352 doesn't allow it), and break the loop here. */
11353 name = dwarf2_name (die, cu);
11354 parent_name = dwarf2_name (parent, cu);
11355 complaint (&symfile_complaints,
11356 _("template param type '%s' defined within parent '%s'"),
11357 name ? name : "<unknown>",
11358 parent_name ? parent_name : "<unknown>");
11359 return "";
11360 }
11361 else
11362 switch (parent->tag)
11363 {
11364 case DW_TAG_namespace:
11365 parent_type = read_type_die (parent, cu);
11366 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11367 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11368 Work around this problem here. */
11369 if (cu->language == language_cplus
11370 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11371 return "";
11372 /* We give a name to even anonymous namespaces. */
11373 return TYPE_TAG_NAME (parent_type);
11374 case DW_TAG_class_type:
11375 case DW_TAG_interface_type:
11376 case DW_TAG_structure_type:
11377 case DW_TAG_union_type:
11378 case DW_TAG_module:
11379 parent_type = read_type_die (parent, cu);
11380 if (TYPE_TAG_NAME (parent_type) != NULL)
11381 return TYPE_TAG_NAME (parent_type);
11382 else
11383 /* An anonymous structure is only allowed non-static data
11384 members; no typedefs, no member functions, et cetera.
11385 So it does not need a prefix. */
11386 return "";
11387 default:
11388 return determine_prefix (parent, cu);
11389 }
11390 }
11391
11392 /* Return a newly-allocated string formed by concatenating PREFIX and
11393 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11394 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11395 perform an obconcat, otherwise allocate storage for the result. The CU argument
11396 is used to determine the language and hence, the appropriate separator. */
11397
11398 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
11399
11400 static char *
11401 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11402 int physname, struct dwarf2_cu *cu)
11403 {
11404 const char *lead = "";
11405 const char *sep;
11406
11407 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11408 sep = "";
11409 else if (cu->language == language_java)
11410 sep = ".";
11411 else if (cu->language == language_fortran && physname)
11412 {
11413 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11414 DW_AT_MIPS_linkage_name is preferred and used instead. */
11415
11416 lead = "__";
11417 sep = "_MOD_";
11418 }
11419 else
11420 sep = "::";
11421
11422 if (prefix == NULL)
11423 prefix = "";
11424 if (suffix == NULL)
11425 suffix = "";
11426
11427 if (obs == NULL)
11428 {
11429 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11430
11431 strcpy (retval, lead);
11432 strcat (retval, prefix);
11433 strcat (retval, sep);
11434 strcat (retval, suffix);
11435 return retval;
11436 }
11437 else
11438 {
11439 /* We have an obstack. */
11440 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11441 }
11442 }
11443
11444 /* Return sibling of die, NULL if no sibling. */
11445
11446 static struct die_info *
11447 sibling_die (struct die_info *die)
11448 {
11449 return die->sibling;
11450 }
11451
11452 /* Get name of a die, return NULL if not found. */
11453
11454 static char *
11455 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11456 struct obstack *obstack)
11457 {
11458 if (name && cu->language == language_cplus)
11459 {
11460 char *canon_name = cp_canonicalize_string (name);
11461
11462 if (canon_name != NULL)
11463 {
11464 if (strcmp (canon_name, name) != 0)
11465 name = obsavestring (canon_name, strlen (canon_name),
11466 obstack);
11467 xfree (canon_name);
11468 }
11469 }
11470
11471 return name;
11472 }
11473
11474 /* Get name of a die, return NULL if not found. */
11475
11476 static char *
11477 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11478 {
11479 struct attribute *attr;
11480
11481 attr = dwarf2_attr (die, DW_AT_name, cu);
11482 if (!attr || !DW_STRING (attr))
11483 return NULL;
11484
11485 switch (die->tag)
11486 {
11487 case DW_TAG_compile_unit:
11488 /* Compilation units have a DW_AT_name that is a filename, not
11489 a source language identifier. */
11490 case DW_TAG_enumeration_type:
11491 case DW_TAG_enumerator:
11492 /* These tags always have simple identifiers already; no need
11493 to canonicalize them. */
11494 return DW_STRING (attr);
11495
11496 case DW_TAG_subprogram:
11497 /* Java constructors will all be named "<init>", so return
11498 the class name when we see this special case. */
11499 if (cu->language == language_java
11500 && DW_STRING (attr) != NULL
11501 && strcmp (DW_STRING (attr), "<init>") == 0)
11502 {
11503 struct dwarf2_cu *spec_cu = cu;
11504 struct die_info *spec_die;
11505
11506 /* GCJ will output '<init>' for Java constructor names.
11507 For this special case, return the name of the parent class. */
11508
11509 /* GCJ may output suprogram DIEs with AT_specification set.
11510 If so, use the name of the specified DIE. */
11511 spec_die = die_specification (die, &spec_cu);
11512 if (spec_die != NULL)
11513 return dwarf2_name (spec_die, spec_cu);
11514
11515 do
11516 {
11517 die = die->parent;
11518 if (die->tag == DW_TAG_class_type)
11519 return dwarf2_name (die, cu);
11520 }
11521 while (die->tag != DW_TAG_compile_unit);
11522 }
11523 break;
11524
11525 case DW_TAG_class_type:
11526 case DW_TAG_interface_type:
11527 case DW_TAG_structure_type:
11528 case DW_TAG_union_type:
11529 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11530 structures or unions. These were of the form "._%d" in GCC 4.1,
11531 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11532 and GCC 4.4. We work around this problem by ignoring these. */
11533 if (strncmp (DW_STRING (attr), "._", 2) == 0
11534 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11535 return NULL;
11536 break;
11537
11538 default:
11539 break;
11540 }
11541
11542 if (!DW_STRING_IS_CANONICAL (attr))
11543 {
11544 DW_STRING (attr)
11545 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11546 &cu->objfile->objfile_obstack);
11547 DW_STRING_IS_CANONICAL (attr) = 1;
11548 }
11549 return DW_STRING (attr);
11550 }
11551
11552 /* Return the die that this die in an extension of, or NULL if there
11553 is none. *EXT_CU is the CU containing DIE on input, and the CU
11554 containing the return value on output. */
11555
11556 static struct die_info *
11557 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11558 {
11559 struct attribute *attr;
11560
11561 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11562 if (attr == NULL)
11563 return NULL;
11564
11565 return follow_die_ref (die, attr, ext_cu);
11566 }
11567
11568 /* Convert a DIE tag into its string name. */
11569
11570 static char *
11571 dwarf_tag_name (unsigned tag)
11572 {
11573 switch (tag)
11574 {
11575 case DW_TAG_padding:
11576 return "DW_TAG_padding";
11577 case DW_TAG_array_type:
11578 return "DW_TAG_array_type";
11579 case DW_TAG_class_type:
11580 return "DW_TAG_class_type";
11581 case DW_TAG_entry_point:
11582 return "DW_TAG_entry_point";
11583 case DW_TAG_enumeration_type:
11584 return "DW_TAG_enumeration_type";
11585 case DW_TAG_formal_parameter:
11586 return "DW_TAG_formal_parameter";
11587 case DW_TAG_imported_declaration:
11588 return "DW_TAG_imported_declaration";
11589 case DW_TAG_label:
11590 return "DW_TAG_label";
11591 case DW_TAG_lexical_block:
11592 return "DW_TAG_lexical_block";
11593 case DW_TAG_member:
11594 return "DW_TAG_member";
11595 case DW_TAG_pointer_type:
11596 return "DW_TAG_pointer_type";
11597 case DW_TAG_reference_type:
11598 return "DW_TAG_reference_type";
11599 case DW_TAG_compile_unit:
11600 return "DW_TAG_compile_unit";
11601 case DW_TAG_string_type:
11602 return "DW_TAG_string_type";
11603 case DW_TAG_structure_type:
11604 return "DW_TAG_structure_type";
11605 case DW_TAG_subroutine_type:
11606 return "DW_TAG_subroutine_type";
11607 case DW_TAG_typedef:
11608 return "DW_TAG_typedef";
11609 case DW_TAG_union_type:
11610 return "DW_TAG_union_type";
11611 case DW_TAG_unspecified_parameters:
11612 return "DW_TAG_unspecified_parameters";
11613 case DW_TAG_variant:
11614 return "DW_TAG_variant";
11615 case DW_TAG_common_block:
11616 return "DW_TAG_common_block";
11617 case DW_TAG_common_inclusion:
11618 return "DW_TAG_common_inclusion";
11619 case DW_TAG_inheritance:
11620 return "DW_TAG_inheritance";
11621 case DW_TAG_inlined_subroutine:
11622 return "DW_TAG_inlined_subroutine";
11623 case DW_TAG_module:
11624 return "DW_TAG_module";
11625 case DW_TAG_ptr_to_member_type:
11626 return "DW_TAG_ptr_to_member_type";
11627 case DW_TAG_set_type:
11628 return "DW_TAG_set_type";
11629 case DW_TAG_subrange_type:
11630 return "DW_TAG_subrange_type";
11631 case DW_TAG_with_stmt:
11632 return "DW_TAG_with_stmt";
11633 case DW_TAG_access_declaration:
11634 return "DW_TAG_access_declaration";
11635 case DW_TAG_base_type:
11636 return "DW_TAG_base_type";
11637 case DW_TAG_catch_block:
11638 return "DW_TAG_catch_block";
11639 case DW_TAG_const_type:
11640 return "DW_TAG_const_type";
11641 case DW_TAG_constant:
11642 return "DW_TAG_constant";
11643 case DW_TAG_enumerator:
11644 return "DW_TAG_enumerator";
11645 case DW_TAG_file_type:
11646 return "DW_TAG_file_type";
11647 case DW_TAG_friend:
11648 return "DW_TAG_friend";
11649 case DW_TAG_namelist:
11650 return "DW_TAG_namelist";
11651 case DW_TAG_namelist_item:
11652 return "DW_TAG_namelist_item";
11653 case DW_TAG_packed_type:
11654 return "DW_TAG_packed_type";
11655 case DW_TAG_subprogram:
11656 return "DW_TAG_subprogram";
11657 case DW_TAG_template_type_param:
11658 return "DW_TAG_template_type_param";
11659 case DW_TAG_template_value_param:
11660 return "DW_TAG_template_value_param";
11661 case DW_TAG_thrown_type:
11662 return "DW_TAG_thrown_type";
11663 case DW_TAG_try_block:
11664 return "DW_TAG_try_block";
11665 case DW_TAG_variant_part:
11666 return "DW_TAG_variant_part";
11667 case DW_TAG_variable:
11668 return "DW_TAG_variable";
11669 case DW_TAG_volatile_type:
11670 return "DW_TAG_volatile_type";
11671 case DW_TAG_dwarf_procedure:
11672 return "DW_TAG_dwarf_procedure";
11673 case DW_TAG_restrict_type:
11674 return "DW_TAG_restrict_type";
11675 case DW_TAG_interface_type:
11676 return "DW_TAG_interface_type";
11677 case DW_TAG_namespace:
11678 return "DW_TAG_namespace";
11679 case DW_TAG_imported_module:
11680 return "DW_TAG_imported_module";
11681 case DW_TAG_unspecified_type:
11682 return "DW_TAG_unspecified_type";
11683 case DW_TAG_partial_unit:
11684 return "DW_TAG_partial_unit";
11685 case DW_TAG_imported_unit:
11686 return "DW_TAG_imported_unit";
11687 case DW_TAG_condition:
11688 return "DW_TAG_condition";
11689 case DW_TAG_shared_type:
11690 return "DW_TAG_shared_type";
11691 case DW_TAG_type_unit:
11692 return "DW_TAG_type_unit";
11693 case DW_TAG_MIPS_loop:
11694 return "DW_TAG_MIPS_loop";
11695 case DW_TAG_HP_array_descriptor:
11696 return "DW_TAG_HP_array_descriptor";
11697 case DW_TAG_format_label:
11698 return "DW_TAG_format_label";
11699 case DW_TAG_function_template:
11700 return "DW_TAG_function_template";
11701 case DW_TAG_class_template:
11702 return "DW_TAG_class_template";
11703 case DW_TAG_GNU_BINCL:
11704 return "DW_TAG_GNU_BINCL";
11705 case DW_TAG_GNU_EINCL:
11706 return "DW_TAG_GNU_EINCL";
11707 case DW_TAG_upc_shared_type:
11708 return "DW_TAG_upc_shared_type";
11709 case DW_TAG_upc_strict_type:
11710 return "DW_TAG_upc_strict_type";
11711 case DW_TAG_upc_relaxed_type:
11712 return "DW_TAG_upc_relaxed_type";
11713 case DW_TAG_PGI_kanji_type:
11714 return "DW_TAG_PGI_kanji_type";
11715 case DW_TAG_PGI_interface_block:
11716 return "DW_TAG_PGI_interface_block";
11717 default:
11718 return "DW_TAG_<unknown>";
11719 }
11720 }
11721
11722 /* Convert a DWARF attribute code into its string name. */
11723
11724 static char *
11725 dwarf_attr_name (unsigned attr)
11726 {
11727 switch (attr)
11728 {
11729 case DW_AT_sibling:
11730 return "DW_AT_sibling";
11731 case DW_AT_location:
11732 return "DW_AT_location";
11733 case DW_AT_name:
11734 return "DW_AT_name";
11735 case DW_AT_ordering:
11736 return "DW_AT_ordering";
11737 case DW_AT_subscr_data:
11738 return "DW_AT_subscr_data";
11739 case DW_AT_byte_size:
11740 return "DW_AT_byte_size";
11741 case DW_AT_bit_offset:
11742 return "DW_AT_bit_offset";
11743 case DW_AT_bit_size:
11744 return "DW_AT_bit_size";
11745 case DW_AT_element_list:
11746 return "DW_AT_element_list";
11747 case DW_AT_stmt_list:
11748 return "DW_AT_stmt_list";
11749 case DW_AT_low_pc:
11750 return "DW_AT_low_pc";
11751 case DW_AT_high_pc:
11752 return "DW_AT_high_pc";
11753 case DW_AT_language:
11754 return "DW_AT_language";
11755 case DW_AT_member:
11756 return "DW_AT_member";
11757 case DW_AT_discr:
11758 return "DW_AT_discr";
11759 case DW_AT_discr_value:
11760 return "DW_AT_discr_value";
11761 case DW_AT_visibility:
11762 return "DW_AT_visibility";
11763 case DW_AT_import:
11764 return "DW_AT_import";
11765 case DW_AT_string_length:
11766 return "DW_AT_string_length";
11767 case DW_AT_common_reference:
11768 return "DW_AT_common_reference";
11769 case DW_AT_comp_dir:
11770 return "DW_AT_comp_dir";
11771 case DW_AT_const_value:
11772 return "DW_AT_const_value";
11773 case DW_AT_containing_type:
11774 return "DW_AT_containing_type";
11775 case DW_AT_default_value:
11776 return "DW_AT_default_value";
11777 case DW_AT_inline:
11778 return "DW_AT_inline";
11779 case DW_AT_is_optional:
11780 return "DW_AT_is_optional";
11781 case DW_AT_lower_bound:
11782 return "DW_AT_lower_bound";
11783 case DW_AT_producer:
11784 return "DW_AT_producer";
11785 case DW_AT_prototyped:
11786 return "DW_AT_prototyped";
11787 case DW_AT_return_addr:
11788 return "DW_AT_return_addr";
11789 case DW_AT_start_scope:
11790 return "DW_AT_start_scope";
11791 case DW_AT_bit_stride:
11792 return "DW_AT_bit_stride";
11793 case DW_AT_upper_bound:
11794 return "DW_AT_upper_bound";
11795 case DW_AT_abstract_origin:
11796 return "DW_AT_abstract_origin";
11797 case DW_AT_accessibility:
11798 return "DW_AT_accessibility";
11799 case DW_AT_address_class:
11800 return "DW_AT_address_class";
11801 case DW_AT_artificial:
11802 return "DW_AT_artificial";
11803 case DW_AT_base_types:
11804 return "DW_AT_base_types";
11805 case DW_AT_calling_convention:
11806 return "DW_AT_calling_convention";
11807 case DW_AT_count:
11808 return "DW_AT_count";
11809 case DW_AT_data_member_location:
11810 return "DW_AT_data_member_location";
11811 case DW_AT_decl_column:
11812 return "DW_AT_decl_column";
11813 case DW_AT_decl_file:
11814 return "DW_AT_decl_file";
11815 case DW_AT_decl_line:
11816 return "DW_AT_decl_line";
11817 case DW_AT_declaration:
11818 return "DW_AT_declaration";
11819 case DW_AT_discr_list:
11820 return "DW_AT_discr_list";
11821 case DW_AT_encoding:
11822 return "DW_AT_encoding";
11823 case DW_AT_external:
11824 return "DW_AT_external";
11825 case DW_AT_frame_base:
11826 return "DW_AT_frame_base";
11827 case DW_AT_friend:
11828 return "DW_AT_friend";
11829 case DW_AT_identifier_case:
11830 return "DW_AT_identifier_case";
11831 case DW_AT_macro_info:
11832 return "DW_AT_macro_info";
11833 case DW_AT_namelist_items:
11834 return "DW_AT_namelist_items";
11835 case DW_AT_priority:
11836 return "DW_AT_priority";
11837 case DW_AT_segment:
11838 return "DW_AT_segment";
11839 case DW_AT_specification:
11840 return "DW_AT_specification";
11841 case DW_AT_static_link:
11842 return "DW_AT_static_link";
11843 case DW_AT_type:
11844 return "DW_AT_type";
11845 case DW_AT_use_location:
11846 return "DW_AT_use_location";
11847 case DW_AT_variable_parameter:
11848 return "DW_AT_variable_parameter";
11849 case DW_AT_virtuality:
11850 return "DW_AT_virtuality";
11851 case DW_AT_vtable_elem_location:
11852 return "DW_AT_vtable_elem_location";
11853 /* DWARF 3 values. */
11854 case DW_AT_allocated:
11855 return "DW_AT_allocated";
11856 case DW_AT_associated:
11857 return "DW_AT_associated";
11858 case DW_AT_data_location:
11859 return "DW_AT_data_location";
11860 case DW_AT_byte_stride:
11861 return "DW_AT_byte_stride";
11862 case DW_AT_entry_pc:
11863 return "DW_AT_entry_pc";
11864 case DW_AT_use_UTF8:
11865 return "DW_AT_use_UTF8";
11866 case DW_AT_extension:
11867 return "DW_AT_extension";
11868 case DW_AT_ranges:
11869 return "DW_AT_ranges";
11870 case DW_AT_trampoline:
11871 return "DW_AT_trampoline";
11872 case DW_AT_call_column:
11873 return "DW_AT_call_column";
11874 case DW_AT_call_file:
11875 return "DW_AT_call_file";
11876 case DW_AT_call_line:
11877 return "DW_AT_call_line";
11878 case DW_AT_description:
11879 return "DW_AT_description";
11880 case DW_AT_binary_scale:
11881 return "DW_AT_binary_scale";
11882 case DW_AT_decimal_scale:
11883 return "DW_AT_decimal_scale";
11884 case DW_AT_small:
11885 return "DW_AT_small";
11886 case DW_AT_decimal_sign:
11887 return "DW_AT_decimal_sign";
11888 case DW_AT_digit_count:
11889 return "DW_AT_digit_count";
11890 case DW_AT_picture_string:
11891 return "DW_AT_picture_string";
11892 case DW_AT_mutable:
11893 return "DW_AT_mutable";
11894 case DW_AT_threads_scaled:
11895 return "DW_AT_threads_scaled";
11896 case DW_AT_explicit:
11897 return "DW_AT_explicit";
11898 case DW_AT_object_pointer:
11899 return "DW_AT_object_pointer";
11900 case DW_AT_endianity:
11901 return "DW_AT_endianity";
11902 case DW_AT_elemental:
11903 return "DW_AT_elemental";
11904 case DW_AT_pure:
11905 return "DW_AT_pure";
11906 case DW_AT_recursive:
11907 return "DW_AT_recursive";
11908 /* DWARF 4 values. */
11909 case DW_AT_signature:
11910 return "DW_AT_signature";
11911 case DW_AT_linkage_name:
11912 return "DW_AT_linkage_name";
11913 /* SGI/MIPS extensions. */
11914 #ifdef MIPS /* collides with DW_AT_HP_block_index */
11915 case DW_AT_MIPS_fde:
11916 return "DW_AT_MIPS_fde";
11917 #endif
11918 case DW_AT_MIPS_loop_begin:
11919 return "DW_AT_MIPS_loop_begin";
11920 case DW_AT_MIPS_tail_loop_begin:
11921 return "DW_AT_MIPS_tail_loop_begin";
11922 case DW_AT_MIPS_epilog_begin:
11923 return "DW_AT_MIPS_epilog_begin";
11924 case DW_AT_MIPS_loop_unroll_factor:
11925 return "DW_AT_MIPS_loop_unroll_factor";
11926 case DW_AT_MIPS_software_pipeline_depth:
11927 return "DW_AT_MIPS_software_pipeline_depth";
11928 case DW_AT_MIPS_linkage_name:
11929 return "DW_AT_MIPS_linkage_name";
11930 case DW_AT_MIPS_stride:
11931 return "DW_AT_MIPS_stride";
11932 case DW_AT_MIPS_abstract_name:
11933 return "DW_AT_MIPS_abstract_name";
11934 case DW_AT_MIPS_clone_origin:
11935 return "DW_AT_MIPS_clone_origin";
11936 case DW_AT_MIPS_has_inlines:
11937 return "DW_AT_MIPS_has_inlines";
11938 /* HP extensions. */
11939 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
11940 case DW_AT_HP_block_index:
11941 return "DW_AT_HP_block_index";
11942 #endif
11943 case DW_AT_HP_unmodifiable:
11944 return "DW_AT_HP_unmodifiable";
11945 case DW_AT_HP_actuals_stmt_list:
11946 return "DW_AT_HP_actuals_stmt_list";
11947 case DW_AT_HP_proc_per_section:
11948 return "DW_AT_HP_proc_per_section";
11949 case DW_AT_HP_raw_data_ptr:
11950 return "DW_AT_HP_raw_data_ptr";
11951 case DW_AT_HP_pass_by_reference:
11952 return "DW_AT_HP_pass_by_reference";
11953 case DW_AT_HP_opt_level:
11954 return "DW_AT_HP_opt_level";
11955 case DW_AT_HP_prof_version_id:
11956 return "DW_AT_HP_prof_version_id";
11957 case DW_AT_HP_opt_flags:
11958 return "DW_AT_HP_opt_flags";
11959 case DW_AT_HP_cold_region_low_pc:
11960 return "DW_AT_HP_cold_region_low_pc";
11961 case DW_AT_HP_cold_region_high_pc:
11962 return "DW_AT_HP_cold_region_high_pc";
11963 case DW_AT_HP_all_variables_modifiable:
11964 return "DW_AT_HP_all_variables_modifiable";
11965 case DW_AT_HP_linkage_name:
11966 return "DW_AT_HP_linkage_name";
11967 case DW_AT_HP_prof_flags:
11968 return "DW_AT_HP_prof_flags";
11969 /* GNU extensions. */
11970 case DW_AT_sf_names:
11971 return "DW_AT_sf_names";
11972 case DW_AT_src_info:
11973 return "DW_AT_src_info";
11974 case DW_AT_mac_info:
11975 return "DW_AT_mac_info";
11976 case DW_AT_src_coords:
11977 return "DW_AT_src_coords";
11978 case DW_AT_body_begin:
11979 return "DW_AT_body_begin";
11980 case DW_AT_body_end:
11981 return "DW_AT_body_end";
11982 case DW_AT_GNU_vector:
11983 return "DW_AT_GNU_vector";
11984 case DW_AT_GNU_odr_signature:
11985 return "DW_AT_GNU_odr_signature";
11986 /* VMS extensions. */
11987 case DW_AT_VMS_rtnbeg_pd_address:
11988 return "DW_AT_VMS_rtnbeg_pd_address";
11989 /* UPC extension. */
11990 case DW_AT_upc_threads_scaled:
11991 return "DW_AT_upc_threads_scaled";
11992 /* PGI (STMicroelectronics) extensions. */
11993 case DW_AT_PGI_lbase:
11994 return "DW_AT_PGI_lbase";
11995 case DW_AT_PGI_soffset:
11996 return "DW_AT_PGI_soffset";
11997 case DW_AT_PGI_lstride:
11998 return "DW_AT_PGI_lstride";
11999 default:
12000 return "DW_AT_<unknown>";
12001 }
12002 }
12003
12004 /* Convert a DWARF value form code into its string name. */
12005
12006 static char *
12007 dwarf_form_name (unsigned form)
12008 {
12009 switch (form)
12010 {
12011 case DW_FORM_addr:
12012 return "DW_FORM_addr";
12013 case DW_FORM_block2:
12014 return "DW_FORM_block2";
12015 case DW_FORM_block4:
12016 return "DW_FORM_block4";
12017 case DW_FORM_data2:
12018 return "DW_FORM_data2";
12019 case DW_FORM_data4:
12020 return "DW_FORM_data4";
12021 case DW_FORM_data8:
12022 return "DW_FORM_data8";
12023 case DW_FORM_string:
12024 return "DW_FORM_string";
12025 case DW_FORM_block:
12026 return "DW_FORM_block";
12027 case DW_FORM_block1:
12028 return "DW_FORM_block1";
12029 case DW_FORM_data1:
12030 return "DW_FORM_data1";
12031 case DW_FORM_flag:
12032 return "DW_FORM_flag";
12033 case DW_FORM_sdata:
12034 return "DW_FORM_sdata";
12035 case DW_FORM_strp:
12036 return "DW_FORM_strp";
12037 case DW_FORM_udata:
12038 return "DW_FORM_udata";
12039 case DW_FORM_ref_addr:
12040 return "DW_FORM_ref_addr";
12041 case DW_FORM_ref1:
12042 return "DW_FORM_ref1";
12043 case DW_FORM_ref2:
12044 return "DW_FORM_ref2";
12045 case DW_FORM_ref4:
12046 return "DW_FORM_ref4";
12047 case DW_FORM_ref8:
12048 return "DW_FORM_ref8";
12049 case DW_FORM_ref_udata:
12050 return "DW_FORM_ref_udata";
12051 case DW_FORM_indirect:
12052 return "DW_FORM_indirect";
12053 case DW_FORM_sec_offset:
12054 return "DW_FORM_sec_offset";
12055 case DW_FORM_exprloc:
12056 return "DW_FORM_exprloc";
12057 case DW_FORM_flag_present:
12058 return "DW_FORM_flag_present";
12059 case DW_FORM_sig8:
12060 return "DW_FORM_sig8";
12061 default:
12062 return "DW_FORM_<unknown>";
12063 }
12064 }
12065
12066 /* Convert a DWARF stack opcode into its string name. */
12067
12068 const char *
12069 dwarf_stack_op_name (unsigned op, int def)
12070 {
12071 switch (op)
12072 {
12073 case DW_OP_addr:
12074 return "DW_OP_addr";
12075 case DW_OP_deref:
12076 return "DW_OP_deref";
12077 case DW_OP_const1u:
12078 return "DW_OP_const1u";
12079 case DW_OP_const1s:
12080 return "DW_OP_const1s";
12081 case DW_OP_const2u:
12082 return "DW_OP_const2u";
12083 case DW_OP_const2s:
12084 return "DW_OP_const2s";
12085 case DW_OP_const4u:
12086 return "DW_OP_const4u";
12087 case DW_OP_const4s:
12088 return "DW_OP_const4s";
12089 case DW_OP_const8u:
12090 return "DW_OP_const8u";
12091 case DW_OP_const8s:
12092 return "DW_OP_const8s";
12093 case DW_OP_constu:
12094 return "DW_OP_constu";
12095 case DW_OP_consts:
12096 return "DW_OP_consts";
12097 case DW_OP_dup:
12098 return "DW_OP_dup";
12099 case DW_OP_drop:
12100 return "DW_OP_drop";
12101 case DW_OP_over:
12102 return "DW_OP_over";
12103 case DW_OP_pick:
12104 return "DW_OP_pick";
12105 case DW_OP_swap:
12106 return "DW_OP_swap";
12107 case DW_OP_rot:
12108 return "DW_OP_rot";
12109 case DW_OP_xderef:
12110 return "DW_OP_xderef";
12111 case DW_OP_abs:
12112 return "DW_OP_abs";
12113 case DW_OP_and:
12114 return "DW_OP_and";
12115 case DW_OP_div:
12116 return "DW_OP_div";
12117 case DW_OP_minus:
12118 return "DW_OP_minus";
12119 case DW_OP_mod:
12120 return "DW_OP_mod";
12121 case DW_OP_mul:
12122 return "DW_OP_mul";
12123 case DW_OP_neg:
12124 return "DW_OP_neg";
12125 case DW_OP_not:
12126 return "DW_OP_not";
12127 case DW_OP_or:
12128 return "DW_OP_or";
12129 case DW_OP_plus:
12130 return "DW_OP_plus";
12131 case DW_OP_plus_uconst:
12132 return "DW_OP_plus_uconst";
12133 case DW_OP_shl:
12134 return "DW_OP_shl";
12135 case DW_OP_shr:
12136 return "DW_OP_shr";
12137 case DW_OP_shra:
12138 return "DW_OP_shra";
12139 case DW_OP_xor:
12140 return "DW_OP_xor";
12141 case DW_OP_bra:
12142 return "DW_OP_bra";
12143 case DW_OP_eq:
12144 return "DW_OP_eq";
12145 case DW_OP_ge:
12146 return "DW_OP_ge";
12147 case DW_OP_gt:
12148 return "DW_OP_gt";
12149 case DW_OP_le:
12150 return "DW_OP_le";
12151 case DW_OP_lt:
12152 return "DW_OP_lt";
12153 case DW_OP_ne:
12154 return "DW_OP_ne";
12155 case DW_OP_skip:
12156 return "DW_OP_skip";
12157 case DW_OP_lit0:
12158 return "DW_OP_lit0";
12159 case DW_OP_lit1:
12160 return "DW_OP_lit1";
12161 case DW_OP_lit2:
12162 return "DW_OP_lit2";
12163 case DW_OP_lit3:
12164 return "DW_OP_lit3";
12165 case DW_OP_lit4:
12166 return "DW_OP_lit4";
12167 case DW_OP_lit5:
12168 return "DW_OP_lit5";
12169 case DW_OP_lit6:
12170 return "DW_OP_lit6";
12171 case DW_OP_lit7:
12172 return "DW_OP_lit7";
12173 case DW_OP_lit8:
12174 return "DW_OP_lit8";
12175 case DW_OP_lit9:
12176 return "DW_OP_lit9";
12177 case DW_OP_lit10:
12178 return "DW_OP_lit10";
12179 case DW_OP_lit11:
12180 return "DW_OP_lit11";
12181 case DW_OP_lit12:
12182 return "DW_OP_lit12";
12183 case DW_OP_lit13:
12184 return "DW_OP_lit13";
12185 case DW_OP_lit14:
12186 return "DW_OP_lit14";
12187 case DW_OP_lit15:
12188 return "DW_OP_lit15";
12189 case DW_OP_lit16:
12190 return "DW_OP_lit16";
12191 case DW_OP_lit17:
12192 return "DW_OP_lit17";
12193 case DW_OP_lit18:
12194 return "DW_OP_lit18";
12195 case DW_OP_lit19:
12196 return "DW_OP_lit19";
12197 case DW_OP_lit20:
12198 return "DW_OP_lit20";
12199 case DW_OP_lit21:
12200 return "DW_OP_lit21";
12201 case DW_OP_lit22:
12202 return "DW_OP_lit22";
12203 case DW_OP_lit23:
12204 return "DW_OP_lit23";
12205 case DW_OP_lit24:
12206 return "DW_OP_lit24";
12207 case DW_OP_lit25:
12208 return "DW_OP_lit25";
12209 case DW_OP_lit26:
12210 return "DW_OP_lit26";
12211 case DW_OP_lit27:
12212 return "DW_OP_lit27";
12213 case DW_OP_lit28:
12214 return "DW_OP_lit28";
12215 case DW_OP_lit29:
12216 return "DW_OP_lit29";
12217 case DW_OP_lit30:
12218 return "DW_OP_lit30";
12219 case DW_OP_lit31:
12220 return "DW_OP_lit31";
12221 case DW_OP_reg0:
12222 return "DW_OP_reg0";
12223 case DW_OP_reg1:
12224 return "DW_OP_reg1";
12225 case DW_OP_reg2:
12226 return "DW_OP_reg2";
12227 case DW_OP_reg3:
12228 return "DW_OP_reg3";
12229 case DW_OP_reg4:
12230 return "DW_OP_reg4";
12231 case DW_OP_reg5:
12232 return "DW_OP_reg5";
12233 case DW_OP_reg6:
12234 return "DW_OP_reg6";
12235 case DW_OP_reg7:
12236 return "DW_OP_reg7";
12237 case DW_OP_reg8:
12238 return "DW_OP_reg8";
12239 case DW_OP_reg9:
12240 return "DW_OP_reg9";
12241 case DW_OP_reg10:
12242 return "DW_OP_reg10";
12243 case DW_OP_reg11:
12244 return "DW_OP_reg11";
12245 case DW_OP_reg12:
12246 return "DW_OP_reg12";
12247 case DW_OP_reg13:
12248 return "DW_OP_reg13";
12249 case DW_OP_reg14:
12250 return "DW_OP_reg14";
12251 case DW_OP_reg15:
12252 return "DW_OP_reg15";
12253 case DW_OP_reg16:
12254 return "DW_OP_reg16";
12255 case DW_OP_reg17:
12256 return "DW_OP_reg17";
12257 case DW_OP_reg18:
12258 return "DW_OP_reg18";
12259 case DW_OP_reg19:
12260 return "DW_OP_reg19";
12261 case DW_OP_reg20:
12262 return "DW_OP_reg20";
12263 case DW_OP_reg21:
12264 return "DW_OP_reg21";
12265 case DW_OP_reg22:
12266 return "DW_OP_reg22";
12267 case DW_OP_reg23:
12268 return "DW_OP_reg23";
12269 case DW_OP_reg24:
12270 return "DW_OP_reg24";
12271 case DW_OP_reg25:
12272 return "DW_OP_reg25";
12273 case DW_OP_reg26:
12274 return "DW_OP_reg26";
12275 case DW_OP_reg27:
12276 return "DW_OP_reg27";
12277 case DW_OP_reg28:
12278 return "DW_OP_reg28";
12279 case DW_OP_reg29:
12280 return "DW_OP_reg29";
12281 case DW_OP_reg30:
12282 return "DW_OP_reg30";
12283 case DW_OP_reg31:
12284 return "DW_OP_reg31";
12285 case DW_OP_breg0:
12286 return "DW_OP_breg0";
12287 case DW_OP_breg1:
12288 return "DW_OP_breg1";
12289 case DW_OP_breg2:
12290 return "DW_OP_breg2";
12291 case DW_OP_breg3:
12292 return "DW_OP_breg3";
12293 case DW_OP_breg4:
12294 return "DW_OP_breg4";
12295 case DW_OP_breg5:
12296 return "DW_OP_breg5";
12297 case DW_OP_breg6:
12298 return "DW_OP_breg6";
12299 case DW_OP_breg7:
12300 return "DW_OP_breg7";
12301 case DW_OP_breg8:
12302 return "DW_OP_breg8";
12303 case DW_OP_breg9:
12304 return "DW_OP_breg9";
12305 case DW_OP_breg10:
12306 return "DW_OP_breg10";
12307 case DW_OP_breg11:
12308 return "DW_OP_breg11";
12309 case DW_OP_breg12:
12310 return "DW_OP_breg12";
12311 case DW_OP_breg13:
12312 return "DW_OP_breg13";
12313 case DW_OP_breg14:
12314 return "DW_OP_breg14";
12315 case DW_OP_breg15:
12316 return "DW_OP_breg15";
12317 case DW_OP_breg16:
12318 return "DW_OP_breg16";
12319 case DW_OP_breg17:
12320 return "DW_OP_breg17";
12321 case DW_OP_breg18:
12322 return "DW_OP_breg18";
12323 case DW_OP_breg19:
12324 return "DW_OP_breg19";
12325 case DW_OP_breg20:
12326 return "DW_OP_breg20";
12327 case DW_OP_breg21:
12328 return "DW_OP_breg21";
12329 case DW_OP_breg22:
12330 return "DW_OP_breg22";
12331 case DW_OP_breg23:
12332 return "DW_OP_breg23";
12333 case DW_OP_breg24:
12334 return "DW_OP_breg24";
12335 case DW_OP_breg25:
12336 return "DW_OP_breg25";
12337 case DW_OP_breg26:
12338 return "DW_OP_breg26";
12339 case DW_OP_breg27:
12340 return "DW_OP_breg27";
12341 case DW_OP_breg28:
12342 return "DW_OP_breg28";
12343 case DW_OP_breg29:
12344 return "DW_OP_breg29";
12345 case DW_OP_breg30:
12346 return "DW_OP_breg30";
12347 case DW_OP_breg31:
12348 return "DW_OP_breg31";
12349 case DW_OP_regx:
12350 return "DW_OP_regx";
12351 case DW_OP_fbreg:
12352 return "DW_OP_fbreg";
12353 case DW_OP_bregx:
12354 return "DW_OP_bregx";
12355 case DW_OP_piece:
12356 return "DW_OP_piece";
12357 case DW_OP_deref_size:
12358 return "DW_OP_deref_size";
12359 case DW_OP_xderef_size:
12360 return "DW_OP_xderef_size";
12361 case DW_OP_nop:
12362 return "DW_OP_nop";
12363 /* DWARF 3 extensions. */
12364 case DW_OP_push_object_address:
12365 return "DW_OP_push_object_address";
12366 case DW_OP_call2:
12367 return "DW_OP_call2";
12368 case DW_OP_call4:
12369 return "DW_OP_call4";
12370 case DW_OP_call_ref:
12371 return "DW_OP_call_ref";
12372 case DW_OP_form_tls_address:
12373 return "DW_OP_form_tls_address";
12374 case DW_OP_call_frame_cfa:
12375 return "DW_OP_call_frame_cfa";
12376 case DW_OP_bit_piece:
12377 return "DW_OP_bit_piece";
12378 /* DWARF 4 extensions. */
12379 case DW_OP_implicit_value:
12380 return "DW_OP_implicit_value";
12381 case DW_OP_stack_value:
12382 return "DW_OP_stack_value";
12383 /* GNU extensions. */
12384 case DW_OP_GNU_push_tls_address:
12385 return "DW_OP_GNU_push_tls_address";
12386 case DW_OP_GNU_uninit:
12387 return "DW_OP_GNU_uninit";
12388 default:
12389 return def ? "OP_<unknown>" : NULL;
12390 }
12391 }
12392
12393 static char *
12394 dwarf_bool_name (unsigned mybool)
12395 {
12396 if (mybool)
12397 return "TRUE";
12398 else
12399 return "FALSE";
12400 }
12401
12402 /* Convert a DWARF type code into its string name. */
12403
12404 static char *
12405 dwarf_type_encoding_name (unsigned enc)
12406 {
12407 switch (enc)
12408 {
12409 case DW_ATE_void:
12410 return "DW_ATE_void";
12411 case DW_ATE_address:
12412 return "DW_ATE_address";
12413 case DW_ATE_boolean:
12414 return "DW_ATE_boolean";
12415 case DW_ATE_complex_float:
12416 return "DW_ATE_complex_float";
12417 case DW_ATE_float:
12418 return "DW_ATE_float";
12419 case DW_ATE_signed:
12420 return "DW_ATE_signed";
12421 case DW_ATE_signed_char:
12422 return "DW_ATE_signed_char";
12423 case DW_ATE_unsigned:
12424 return "DW_ATE_unsigned";
12425 case DW_ATE_unsigned_char:
12426 return "DW_ATE_unsigned_char";
12427 /* DWARF 3. */
12428 case DW_ATE_imaginary_float:
12429 return "DW_ATE_imaginary_float";
12430 case DW_ATE_packed_decimal:
12431 return "DW_ATE_packed_decimal";
12432 case DW_ATE_numeric_string:
12433 return "DW_ATE_numeric_string";
12434 case DW_ATE_edited:
12435 return "DW_ATE_edited";
12436 case DW_ATE_signed_fixed:
12437 return "DW_ATE_signed_fixed";
12438 case DW_ATE_unsigned_fixed:
12439 return "DW_ATE_unsigned_fixed";
12440 case DW_ATE_decimal_float:
12441 return "DW_ATE_decimal_float";
12442 /* DWARF 4. */
12443 case DW_ATE_UTF:
12444 return "DW_ATE_UTF";
12445 /* HP extensions. */
12446 case DW_ATE_HP_float80:
12447 return "DW_ATE_HP_float80";
12448 case DW_ATE_HP_complex_float80:
12449 return "DW_ATE_HP_complex_float80";
12450 case DW_ATE_HP_float128:
12451 return "DW_ATE_HP_float128";
12452 case DW_ATE_HP_complex_float128:
12453 return "DW_ATE_HP_complex_float128";
12454 case DW_ATE_HP_floathpintel:
12455 return "DW_ATE_HP_floathpintel";
12456 case DW_ATE_HP_imaginary_float80:
12457 return "DW_ATE_HP_imaginary_float80";
12458 case DW_ATE_HP_imaginary_float128:
12459 return "DW_ATE_HP_imaginary_float128";
12460 default:
12461 return "DW_ATE_<unknown>";
12462 }
12463 }
12464
12465 /* Convert a DWARF call frame info operation to its string name. */
12466
12467 #if 0
12468 static char *
12469 dwarf_cfi_name (unsigned cfi_opc)
12470 {
12471 switch (cfi_opc)
12472 {
12473 case DW_CFA_advance_loc:
12474 return "DW_CFA_advance_loc";
12475 case DW_CFA_offset:
12476 return "DW_CFA_offset";
12477 case DW_CFA_restore:
12478 return "DW_CFA_restore";
12479 case DW_CFA_nop:
12480 return "DW_CFA_nop";
12481 case DW_CFA_set_loc:
12482 return "DW_CFA_set_loc";
12483 case DW_CFA_advance_loc1:
12484 return "DW_CFA_advance_loc1";
12485 case DW_CFA_advance_loc2:
12486 return "DW_CFA_advance_loc2";
12487 case DW_CFA_advance_loc4:
12488 return "DW_CFA_advance_loc4";
12489 case DW_CFA_offset_extended:
12490 return "DW_CFA_offset_extended";
12491 case DW_CFA_restore_extended:
12492 return "DW_CFA_restore_extended";
12493 case DW_CFA_undefined:
12494 return "DW_CFA_undefined";
12495 case DW_CFA_same_value:
12496 return "DW_CFA_same_value";
12497 case DW_CFA_register:
12498 return "DW_CFA_register";
12499 case DW_CFA_remember_state:
12500 return "DW_CFA_remember_state";
12501 case DW_CFA_restore_state:
12502 return "DW_CFA_restore_state";
12503 case DW_CFA_def_cfa:
12504 return "DW_CFA_def_cfa";
12505 case DW_CFA_def_cfa_register:
12506 return "DW_CFA_def_cfa_register";
12507 case DW_CFA_def_cfa_offset:
12508 return "DW_CFA_def_cfa_offset";
12509 /* DWARF 3. */
12510 case DW_CFA_def_cfa_expression:
12511 return "DW_CFA_def_cfa_expression";
12512 case DW_CFA_expression:
12513 return "DW_CFA_expression";
12514 case DW_CFA_offset_extended_sf:
12515 return "DW_CFA_offset_extended_sf";
12516 case DW_CFA_def_cfa_sf:
12517 return "DW_CFA_def_cfa_sf";
12518 case DW_CFA_def_cfa_offset_sf:
12519 return "DW_CFA_def_cfa_offset_sf";
12520 case DW_CFA_val_offset:
12521 return "DW_CFA_val_offset";
12522 case DW_CFA_val_offset_sf:
12523 return "DW_CFA_val_offset_sf";
12524 case DW_CFA_val_expression:
12525 return "DW_CFA_val_expression";
12526 /* SGI/MIPS specific. */
12527 case DW_CFA_MIPS_advance_loc8:
12528 return "DW_CFA_MIPS_advance_loc8";
12529 /* GNU extensions. */
12530 case DW_CFA_GNU_window_save:
12531 return "DW_CFA_GNU_window_save";
12532 case DW_CFA_GNU_args_size:
12533 return "DW_CFA_GNU_args_size";
12534 case DW_CFA_GNU_negative_offset_extended:
12535 return "DW_CFA_GNU_negative_offset_extended";
12536 default:
12537 return "DW_CFA_<unknown>";
12538 }
12539 }
12540 #endif
12541
12542 static void
12543 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12544 {
12545 unsigned int i;
12546
12547 print_spaces (indent, f);
12548 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12549 dwarf_tag_name (die->tag), die->abbrev, die->offset);
12550
12551 if (die->parent != NULL)
12552 {
12553 print_spaces (indent, f);
12554 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12555 die->parent->offset);
12556 }
12557
12558 print_spaces (indent, f);
12559 fprintf_unfiltered (f, " has children: %s\n",
12560 dwarf_bool_name (die->child != NULL));
12561
12562 print_spaces (indent, f);
12563 fprintf_unfiltered (f, " attributes:\n");
12564
12565 for (i = 0; i < die->num_attrs; ++i)
12566 {
12567 print_spaces (indent, f);
12568 fprintf_unfiltered (f, " %s (%s) ",
12569 dwarf_attr_name (die->attrs[i].name),
12570 dwarf_form_name (die->attrs[i].form));
12571
12572 switch (die->attrs[i].form)
12573 {
12574 case DW_FORM_ref_addr:
12575 case DW_FORM_addr:
12576 fprintf_unfiltered (f, "address: ");
12577 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12578 break;
12579 case DW_FORM_block2:
12580 case DW_FORM_block4:
12581 case DW_FORM_block:
12582 case DW_FORM_block1:
12583 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12584 break;
12585 case DW_FORM_exprloc:
12586 fprintf_unfiltered (f, "expression: size %u",
12587 DW_BLOCK (&die->attrs[i])->size);
12588 break;
12589 case DW_FORM_ref1:
12590 case DW_FORM_ref2:
12591 case DW_FORM_ref4:
12592 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12593 (long) (DW_ADDR (&die->attrs[i])));
12594 break;
12595 case DW_FORM_data1:
12596 case DW_FORM_data2:
12597 case DW_FORM_data4:
12598 case DW_FORM_data8:
12599 case DW_FORM_udata:
12600 case DW_FORM_sdata:
12601 fprintf_unfiltered (f, "constant: %s",
12602 pulongest (DW_UNSND (&die->attrs[i])));
12603 break;
12604 case DW_FORM_sec_offset:
12605 fprintf_unfiltered (f, "section offset: %s",
12606 pulongest (DW_UNSND (&die->attrs[i])));
12607 break;
12608 case DW_FORM_sig8:
12609 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12610 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12611 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12612 else
12613 fprintf_unfiltered (f, "signatured type, offset: unknown");
12614 break;
12615 case DW_FORM_string:
12616 case DW_FORM_strp:
12617 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12618 DW_STRING (&die->attrs[i])
12619 ? DW_STRING (&die->attrs[i]) : "",
12620 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12621 break;
12622 case DW_FORM_flag:
12623 if (DW_UNSND (&die->attrs[i]))
12624 fprintf_unfiltered (f, "flag: TRUE");
12625 else
12626 fprintf_unfiltered (f, "flag: FALSE");
12627 break;
12628 case DW_FORM_flag_present:
12629 fprintf_unfiltered (f, "flag: TRUE");
12630 break;
12631 case DW_FORM_indirect:
12632 /* the reader will have reduced the indirect form to
12633 the "base form" so this form should not occur */
12634 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12635 break;
12636 default:
12637 fprintf_unfiltered (f, "unsupported attribute form: %d.",
12638 die->attrs[i].form);
12639 break;
12640 }
12641 fprintf_unfiltered (f, "\n");
12642 }
12643 }
12644
12645 static void
12646 dump_die_for_error (struct die_info *die)
12647 {
12648 dump_die_shallow (gdb_stderr, 0, die);
12649 }
12650
12651 static void
12652 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12653 {
12654 int indent = level * 4;
12655
12656 gdb_assert (die != NULL);
12657
12658 if (level >= max_level)
12659 return;
12660
12661 dump_die_shallow (f, indent, die);
12662
12663 if (die->child != NULL)
12664 {
12665 print_spaces (indent, f);
12666 fprintf_unfiltered (f, " Children:");
12667 if (level + 1 < max_level)
12668 {
12669 fprintf_unfiltered (f, "\n");
12670 dump_die_1 (f, level + 1, max_level, die->child);
12671 }
12672 else
12673 {
12674 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12675 }
12676 }
12677
12678 if (die->sibling != NULL && level > 0)
12679 {
12680 dump_die_1 (f, level, max_level, die->sibling);
12681 }
12682 }
12683
12684 /* This is called from the pdie macro in gdbinit.in.
12685 It's not static so gcc will keep a copy callable from gdb. */
12686
12687 void
12688 dump_die (struct die_info *die, int max_level)
12689 {
12690 dump_die_1 (gdb_stdlog, 0, max_level, die);
12691 }
12692
12693 static void
12694 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12695 {
12696 void **slot;
12697
12698 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12699
12700 *slot = die;
12701 }
12702
12703 static int
12704 is_ref_attr (struct attribute *attr)
12705 {
12706 switch (attr->form)
12707 {
12708 case DW_FORM_ref_addr:
12709 case DW_FORM_ref1:
12710 case DW_FORM_ref2:
12711 case DW_FORM_ref4:
12712 case DW_FORM_ref8:
12713 case DW_FORM_ref_udata:
12714 return 1;
12715 default:
12716 return 0;
12717 }
12718 }
12719
12720 static unsigned int
12721 dwarf2_get_ref_die_offset (struct attribute *attr)
12722 {
12723 if (is_ref_attr (attr))
12724 return DW_ADDR (attr);
12725
12726 complaint (&symfile_complaints,
12727 _("unsupported die ref attribute form: '%s'"),
12728 dwarf_form_name (attr->form));
12729 return 0;
12730 }
12731
12732 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12733 * the value held by the attribute is not constant. */
12734
12735 static LONGEST
12736 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12737 {
12738 if (attr->form == DW_FORM_sdata)
12739 return DW_SND (attr);
12740 else if (attr->form == DW_FORM_udata
12741 || attr->form == DW_FORM_data1
12742 || attr->form == DW_FORM_data2
12743 || attr->form == DW_FORM_data4
12744 || attr->form == DW_FORM_data8)
12745 return DW_UNSND (attr);
12746 else
12747 {
12748 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12749 dwarf_form_name (attr->form));
12750 return default_value;
12751 }
12752 }
12753
12754 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
12755 unit and add it to our queue.
12756 The result is non-zero if PER_CU was queued, otherwise the result is zero
12757 meaning either PER_CU is already queued or it is already loaded. */
12758
12759 static int
12760 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12761 struct dwarf2_per_cu_data *per_cu)
12762 {
12763 /* We may arrive here during partial symbol reading, if we need full
12764 DIEs to process an unusual case (e.g. template arguments). Do
12765 not queue PER_CU, just tell our caller to load its DIEs. */
12766 if (dwarf2_per_objfile->reading_partial_symbols)
12767 {
12768 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12769 return 1;
12770 return 0;
12771 }
12772
12773 /* Mark the dependence relation so that we don't flush PER_CU
12774 too early. */
12775 dwarf2_add_dependence (this_cu, per_cu);
12776
12777 /* If it's already on the queue, we have nothing to do. */
12778 if (per_cu->queued)
12779 return 0;
12780
12781 /* If the compilation unit is already loaded, just mark it as
12782 used. */
12783 if (per_cu->cu != NULL)
12784 {
12785 per_cu->cu->last_used = 0;
12786 return 0;
12787 }
12788
12789 /* Add it to the queue. */
12790 queue_comp_unit (per_cu, this_cu->objfile);
12791
12792 return 1;
12793 }
12794
12795 /* Follow reference or signature attribute ATTR of SRC_DIE.
12796 On entry *REF_CU is the CU of SRC_DIE.
12797 On exit *REF_CU is the CU of the result. */
12798
12799 static struct die_info *
12800 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12801 struct dwarf2_cu **ref_cu)
12802 {
12803 struct die_info *die;
12804
12805 if (is_ref_attr (attr))
12806 die = follow_die_ref (src_die, attr, ref_cu);
12807 else if (attr->form == DW_FORM_sig8)
12808 die = follow_die_sig (src_die, attr, ref_cu);
12809 else
12810 {
12811 dump_die_for_error (src_die);
12812 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12813 (*ref_cu)->objfile->name);
12814 }
12815
12816 return die;
12817 }
12818
12819 /* Follow reference OFFSET.
12820 On entry *REF_CU is the CU of the source die referencing OFFSET.
12821 On exit *REF_CU is the CU of the result.
12822 Returns NULL if OFFSET is invalid. */
12823
12824 static struct die_info *
12825 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
12826 {
12827 struct die_info temp_die;
12828 struct dwarf2_cu *target_cu, *cu = *ref_cu;
12829
12830 gdb_assert (cu->per_cu != NULL);
12831
12832 target_cu = cu;
12833
12834 if (cu->per_cu->from_debug_types)
12835 {
12836 /* .debug_types CUs cannot reference anything outside their CU.
12837 If they need to, they have to reference a signatured type via
12838 DW_FORM_sig8. */
12839 if (! offset_in_cu_p (&cu->header, offset))
12840 return NULL;
12841 }
12842 else if (! offset_in_cu_p (&cu->header, offset))
12843 {
12844 struct dwarf2_per_cu_data *per_cu;
12845
12846 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
12847
12848 /* If necessary, add it to the queue and load its DIEs. */
12849 if (maybe_queue_comp_unit (cu, per_cu))
12850 load_full_comp_unit (per_cu, cu->objfile);
12851
12852 target_cu = per_cu->cu;
12853 }
12854 else if (cu->dies == NULL)
12855 {
12856 /* We're loading full DIEs during partial symbol reading. */
12857 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12858 load_full_comp_unit (cu->per_cu, cu->objfile);
12859 }
12860
12861 *ref_cu = target_cu;
12862 temp_die.offset = offset;
12863 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12864 }
12865
12866 /* Follow reference attribute ATTR of SRC_DIE.
12867 On entry *REF_CU is the CU of SRC_DIE.
12868 On exit *REF_CU is the CU of the result. */
12869
12870 static struct die_info *
12871 follow_die_ref (struct die_info *src_die, struct attribute *attr,
12872 struct dwarf2_cu **ref_cu)
12873 {
12874 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12875 struct dwarf2_cu *cu = *ref_cu;
12876 struct die_info *die;
12877
12878 die = follow_die_offset (offset, ref_cu);
12879 if (!die)
12880 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12881 "at 0x%x [in module %s]"),
12882 offset, src_die->offset, cu->objfile->name);
12883
12884 return die;
12885 }
12886
12887 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12888 value is intended for DW_OP_call*. */
12889
12890 struct dwarf2_locexpr_baton
12891 dwarf2_fetch_die_location_block (unsigned int offset,
12892 struct dwarf2_per_cu_data *per_cu)
12893 {
12894 struct dwarf2_cu *cu = per_cu->cu;
12895 struct die_info *die;
12896 struct attribute *attr;
12897 struct dwarf2_locexpr_baton retval;
12898
12899 die = follow_die_offset (offset, &cu);
12900 if (!die)
12901 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12902 offset, per_cu->cu->objfile->name);
12903
12904 attr = dwarf2_attr (die, DW_AT_location, cu);
12905 if (!attr)
12906 {
12907 /* DWARF: "If there is no such attribute, then there is no effect.". */
12908
12909 retval.data = NULL;
12910 retval.size = 0;
12911 }
12912 else
12913 {
12914 if (!attr_form_is_block (attr))
12915 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12916 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12917 offset, per_cu->cu->objfile->name);
12918
12919 retval.data = DW_BLOCK (attr)->data;
12920 retval.size = DW_BLOCK (attr)->size;
12921 }
12922 retval.per_cu = cu->per_cu;
12923 return retval;
12924 }
12925
12926 /* Follow the signature attribute ATTR in SRC_DIE.
12927 On entry *REF_CU is the CU of SRC_DIE.
12928 On exit *REF_CU is the CU of the result. */
12929
12930 static struct die_info *
12931 follow_die_sig (struct die_info *src_die, struct attribute *attr,
12932 struct dwarf2_cu **ref_cu)
12933 {
12934 struct objfile *objfile = (*ref_cu)->objfile;
12935 struct die_info temp_die;
12936 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12937 struct dwarf2_cu *sig_cu;
12938 struct die_info *die;
12939
12940 /* sig_type will be NULL if the signatured type is missing from
12941 the debug info. */
12942 if (sig_type == NULL)
12943 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12944 "at 0x%x [in module %s]"),
12945 src_die->offset, objfile->name);
12946
12947 /* If necessary, add it to the queue and load its DIEs. */
12948
12949 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12950 read_signatured_type (objfile, sig_type);
12951
12952 gdb_assert (sig_type->per_cu.cu != NULL);
12953
12954 sig_cu = sig_type->per_cu.cu;
12955 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12956 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12957 if (die)
12958 {
12959 *ref_cu = sig_cu;
12960 return die;
12961 }
12962
12963 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12964 "at 0x%x [in module %s]"),
12965 sig_type->type_offset, src_die->offset, objfile->name);
12966 }
12967
12968 /* Given an offset of a signatured type, return its signatured_type. */
12969
12970 static struct signatured_type *
12971 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12972 {
12973 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12974 unsigned int length, initial_length_size;
12975 unsigned int sig_offset;
12976 struct signatured_type find_entry, *type_sig;
12977
12978 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12979 sig_offset = (initial_length_size
12980 + 2 /*version*/
12981 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12982 + 1 /*address_size*/);
12983 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12984 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12985
12986 /* This is only used to lookup previously recorded types.
12987 If we didn't find it, it's our bug. */
12988 gdb_assert (type_sig != NULL);
12989 gdb_assert (offset == type_sig->offset);
12990
12991 return type_sig;
12992 }
12993
12994 /* Read in signatured type at OFFSET and build its CU and die(s). */
12995
12996 static void
12997 read_signatured_type_at_offset (struct objfile *objfile,
12998 unsigned int offset)
12999 {
13000 struct signatured_type *type_sig;
13001
13002 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13003
13004 /* We have the section offset, but we need the signature to do the
13005 hash table lookup. */
13006 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13007
13008 gdb_assert (type_sig->per_cu.cu == NULL);
13009
13010 read_signatured_type (objfile, type_sig);
13011
13012 gdb_assert (type_sig->per_cu.cu != NULL);
13013 }
13014
13015 /* Read in a signatured type and build its CU and DIEs. */
13016
13017 static void
13018 read_signatured_type (struct objfile *objfile,
13019 struct signatured_type *type_sig)
13020 {
13021 gdb_byte *types_ptr;
13022 struct die_reader_specs reader_specs;
13023 struct dwarf2_cu *cu;
13024 ULONGEST signature;
13025 struct cleanup *back_to, *free_cu_cleanup;
13026 struct attribute *attr;
13027
13028 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13029 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13030
13031 gdb_assert (type_sig->per_cu.cu == NULL);
13032
13033 cu = xmalloc (sizeof (struct dwarf2_cu));
13034 memset (cu, 0, sizeof (struct dwarf2_cu));
13035 obstack_init (&cu->comp_unit_obstack);
13036 cu->objfile = objfile;
13037 type_sig->per_cu.cu = cu;
13038 cu->per_cu = &type_sig->per_cu;
13039
13040 /* If an error occurs while loading, release our storage. */
13041 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13042
13043 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13044 types_ptr, objfile->obfd);
13045 gdb_assert (signature == type_sig->signature);
13046
13047 cu->die_hash
13048 = htab_create_alloc_ex (cu->header.length / 12,
13049 die_hash,
13050 die_eq,
13051 NULL,
13052 &cu->comp_unit_obstack,
13053 hashtab_obstack_allocate,
13054 dummy_obstack_deallocate);
13055
13056 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13057 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13058
13059 init_cu_die_reader (&reader_specs, cu);
13060
13061 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13062 NULL /*parent*/);
13063
13064 /* We try not to read any attributes in this function, because not
13065 all objfiles needed for references have been loaded yet, and symbol
13066 table processing isn't initialized. But we have to set the CU language,
13067 or we won't be able to build types correctly. */
13068 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13069 if (attr)
13070 set_cu_language (DW_UNSND (attr), cu);
13071 else
13072 set_cu_language (language_minimal, cu);
13073
13074 do_cleanups (back_to);
13075
13076 /* We've successfully allocated this compilation unit. Let our caller
13077 clean it up when finished with it. */
13078 discard_cleanups (free_cu_cleanup);
13079
13080 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13081 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13082 }
13083
13084 /* Decode simple location descriptions.
13085 Given a pointer to a dwarf block that defines a location, compute
13086 the location and return the value.
13087
13088 NOTE drow/2003-11-18: This function is called in two situations
13089 now: for the address of static or global variables (partial symbols
13090 only) and for offsets into structures which are expected to be
13091 (more or less) constant. The partial symbol case should go away,
13092 and only the constant case should remain. That will let this
13093 function complain more accurately. A few special modes are allowed
13094 without complaint for global variables (for instance, global
13095 register values and thread-local values).
13096
13097 A location description containing no operations indicates that the
13098 object is optimized out. The return value is 0 for that case.
13099 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13100 callers will only want a very basic result and this can become a
13101 complaint.
13102
13103 Note that stack[0] is unused except as a default error return.
13104 Note that stack overflow is not yet handled. */
13105
13106 static CORE_ADDR
13107 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13108 {
13109 struct objfile *objfile = cu->objfile;
13110 int i;
13111 int size = blk->size;
13112 gdb_byte *data = blk->data;
13113 CORE_ADDR stack[64];
13114 int stacki;
13115 unsigned int bytes_read, unsnd;
13116 gdb_byte op;
13117
13118 i = 0;
13119 stacki = 0;
13120 stack[stacki] = 0;
13121
13122 while (i < size)
13123 {
13124 op = data[i++];
13125 switch (op)
13126 {
13127 case DW_OP_lit0:
13128 case DW_OP_lit1:
13129 case DW_OP_lit2:
13130 case DW_OP_lit3:
13131 case DW_OP_lit4:
13132 case DW_OP_lit5:
13133 case DW_OP_lit6:
13134 case DW_OP_lit7:
13135 case DW_OP_lit8:
13136 case DW_OP_lit9:
13137 case DW_OP_lit10:
13138 case DW_OP_lit11:
13139 case DW_OP_lit12:
13140 case DW_OP_lit13:
13141 case DW_OP_lit14:
13142 case DW_OP_lit15:
13143 case DW_OP_lit16:
13144 case DW_OP_lit17:
13145 case DW_OP_lit18:
13146 case DW_OP_lit19:
13147 case DW_OP_lit20:
13148 case DW_OP_lit21:
13149 case DW_OP_lit22:
13150 case DW_OP_lit23:
13151 case DW_OP_lit24:
13152 case DW_OP_lit25:
13153 case DW_OP_lit26:
13154 case DW_OP_lit27:
13155 case DW_OP_lit28:
13156 case DW_OP_lit29:
13157 case DW_OP_lit30:
13158 case DW_OP_lit31:
13159 stack[++stacki] = op - DW_OP_lit0;
13160 break;
13161
13162 case DW_OP_reg0:
13163 case DW_OP_reg1:
13164 case DW_OP_reg2:
13165 case DW_OP_reg3:
13166 case DW_OP_reg4:
13167 case DW_OP_reg5:
13168 case DW_OP_reg6:
13169 case DW_OP_reg7:
13170 case DW_OP_reg8:
13171 case DW_OP_reg9:
13172 case DW_OP_reg10:
13173 case DW_OP_reg11:
13174 case DW_OP_reg12:
13175 case DW_OP_reg13:
13176 case DW_OP_reg14:
13177 case DW_OP_reg15:
13178 case DW_OP_reg16:
13179 case DW_OP_reg17:
13180 case DW_OP_reg18:
13181 case DW_OP_reg19:
13182 case DW_OP_reg20:
13183 case DW_OP_reg21:
13184 case DW_OP_reg22:
13185 case DW_OP_reg23:
13186 case DW_OP_reg24:
13187 case DW_OP_reg25:
13188 case DW_OP_reg26:
13189 case DW_OP_reg27:
13190 case DW_OP_reg28:
13191 case DW_OP_reg29:
13192 case DW_OP_reg30:
13193 case DW_OP_reg31:
13194 stack[++stacki] = op - DW_OP_reg0;
13195 if (i < size)
13196 dwarf2_complex_location_expr_complaint ();
13197 break;
13198
13199 case DW_OP_regx:
13200 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13201 i += bytes_read;
13202 stack[++stacki] = unsnd;
13203 if (i < size)
13204 dwarf2_complex_location_expr_complaint ();
13205 break;
13206
13207 case DW_OP_addr:
13208 stack[++stacki] = read_address (objfile->obfd, &data[i],
13209 cu, &bytes_read);
13210 i += bytes_read;
13211 break;
13212
13213 case DW_OP_const1u:
13214 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13215 i += 1;
13216 break;
13217
13218 case DW_OP_const1s:
13219 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13220 i += 1;
13221 break;
13222
13223 case DW_OP_const2u:
13224 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13225 i += 2;
13226 break;
13227
13228 case DW_OP_const2s:
13229 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13230 i += 2;
13231 break;
13232
13233 case DW_OP_const4u:
13234 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13235 i += 4;
13236 break;
13237
13238 case DW_OP_const4s:
13239 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13240 i += 4;
13241 break;
13242
13243 case DW_OP_constu:
13244 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13245 &bytes_read);
13246 i += bytes_read;
13247 break;
13248
13249 case DW_OP_consts:
13250 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13251 i += bytes_read;
13252 break;
13253
13254 case DW_OP_dup:
13255 stack[stacki + 1] = stack[stacki];
13256 stacki++;
13257 break;
13258
13259 case DW_OP_plus:
13260 stack[stacki - 1] += stack[stacki];
13261 stacki--;
13262 break;
13263
13264 case DW_OP_plus_uconst:
13265 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13266 i += bytes_read;
13267 break;
13268
13269 case DW_OP_minus:
13270 stack[stacki - 1] -= stack[stacki];
13271 stacki--;
13272 break;
13273
13274 case DW_OP_deref:
13275 /* If we're not the last op, then we definitely can't encode
13276 this using GDB's address_class enum. This is valid for partial
13277 global symbols, although the variable's address will be bogus
13278 in the psymtab. */
13279 if (i < size)
13280 dwarf2_complex_location_expr_complaint ();
13281 break;
13282
13283 case DW_OP_GNU_push_tls_address:
13284 /* The top of the stack has the offset from the beginning
13285 of the thread control block at which the variable is located. */
13286 /* Nothing should follow this operator, so the top of stack would
13287 be returned. */
13288 /* This is valid for partial global symbols, but the variable's
13289 address will be bogus in the psymtab. */
13290 if (i < size)
13291 dwarf2_complex_location_expr_complaint ();
13292 break;
13293
13294 case DW_OP_GNU_uninit:
13295 break;
13296
13297 default:
13298 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13299 dwarf_stack_op_name (op, 1));
13300 return (stack[stacki]);
13301 }
13302 }
13303 return (stack[stacki]);
13304 }
13305
13306 /* memory allocation interface */
13307
13308 static struct dwarf_block *
13309 dwarf_alloc_block (struct dwarf2_cu *cu)
13310 {
13311 struct dwarf_block *blk;
13312
13313 blk = (struct dwarf_block *)
13314 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13315 return (blk);
13316 }
13317
13318 static struct abbrev_info *
13319 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13320 {
13321 struct abbrev_info *abbrev;
13322
13323 abbrev = (struct abbrev_info *)
13324 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13325 memset (abbrev, 0, sizeof (struct abbrev_info));
13326 return (abbrev);
13327 }
13328
13329 static struct die_info *
13330 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13331 {
13332 struct die_info *die;
13333 size_t size = sizeof (struct die_info);
13334
13335 if (num_attrs > 1)
13336 size += (num_attrs - 1) * sizeof (struct attribute);
13337
13338 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13339 memset (die, 0, sizeof (struct die_info));
13340 return (die);
13341 }
13342
13343 \f
13344 /* Macro support. */
13345
13346
13347 /* Return the full name of file number I in *LH's file name table.
13348 Use COMP_DIR as the name of the current directory of the
13349 compilation. The result is allocated using xmalloc; the caller is
13350 responsible for freeing it. */
13351 static char *
13352 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13353 {
13354 /* Is the file number a valid index into the line header's file name
13355 table? Remember that file numbers start with one, not zero. */
13356 if (1 <= file && file <= lh->num_file_names)
13357 {
13358 struct file_entry *fe = &lh->file_names[file - 1];
13359
13360 if (IS_ABSOLUTE_PATH (fe->name))
13361 return xstrdup (fe->name);
13362 else
13363 {
13364 const char *dir;
13365 int dir_len;
13366 char *full_name;
13367
13368 if (fe->dir_index)
13369 dir = lh->include_dirs[fe->dir_index - 1];
13370 else
13371 dir = comp_dir;
13372
13373 if (dir)
13374 {
13375 dir_len = strlen (dir);
13376 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13377 strcpy (full_name, dir);
13378 full_name[dir_len] = '/';
13379 strcpy (full_name + dir_len + 1, fe->name);
13380 return full_name;
13381 }
13382 else
13383 return xstrdup (fe->name);
13384 }
13385 }
13386 else
13387 {
13388 /* The compiler produced a bogus file number. We can at least
13389 record the macro definitions made in the file, even if we
13390 won't be able to find the file by name. */
13391 char fake_name[80];
13392
13393 sprintf (fake_name, "<bad macro file number %d>", file);
13394
13395 complaint (&symfile_complaints,
13396 _("bad file number in macro information (%d)"),
13397 file);
13398
13399 return xstrdup (fake_name);
13400 }
13401 }
13402
13403
13404 static struct macro_source_file *
13405 macro_start_file (int file, int line,
13406 struct macro_source_file *current_file,
13407 const char *comp_dir,
13408 struct line_header *lh, struct objfile *objfile)
13409 {
13410 /* The full name of this source file. */
13411 char *full_name = file_full_name (file, lh, comp_dir);
13412
13413 /* We don't create a macro table for this compilation unit
13414 at all until we actually get a filename. */
13415 if (! pending_macros)
13416 pending_macros = new_macro_table (&objfile->objfile_obstack,
13417 objfile->macro_cache);
13418
13419 if (! current_file)
13420 /* If we have no current file, then this must be the start_file
13421 directive for the compilation unit's main source file. */
13422 current_file = macro_set_main (pending_macros, full_name);
13423 else
13424 current_file = macro_include (current_file, line, full_name);
13425
13426 xfree (full_name);
13427
13428 return current_file;
13429 }
13430
13431
13432 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13433 followed by a null byte. */
13434 static char *
13435 copy_string (const char *buf, int len)
13436 {
13437 char *s = xmalloc (len + 1);
13438
13439 memcpy (s, buf, len);
13440 s[len] = '\0';
13441 return s;
13442 }
13443
13444
13445 static const char *
13446 consume_improper_spaces (const char *p, const char *body)
13447 {
13448 if (*p == ' ')
13449 {
13450 complaint (&symfile_complaints,
13451 _("macro definition contains spaces in formal argument list:\n`%s'"),
13452 body);
13453
13454 while (*p == ' ')
13455 p++;
13456 }
13457
13458 return p;
13459 }
13460
13461
13462 static void
13463 parse_macro_definition (struct macro_source_file *file, int line,
13464 const char *body)
13465 {
13466 const char *p;
13467
13468 /* The body string takes one of two forms. For object-like macro
13469 definitions, it should be:
13470
13471 <macro name> " " <definition>
13472
13473 For function-like macro definitions, it should be:
13474
13475 <macro name> "() " <definition>
13476 or
13477 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13478
13479 Spaces may appear only where explicitly indicated, and in the
13480 <definition>.
13481
13482 The Dwarf 2 spec says that an object-like macro's name is always
13483 followed by a space, but versions of GCC around March 2002 omit
13484 the space when the macro's definition is the empty string.
13485
13486 The Dwarf 2 spec says that there should be no spaces between the
13487 formal arguments in a function-like macro's formal argument list,
13488 but versions of GCC around March 2002 include spaces after the
13489 commas. */
13490
13491
13492 /* Find the extent of the macro name. The macro name is terminated
13493 by either a space or null character (for an object-like macro) or
13494 an opening paren (for a function-like macro). */
13495 for (p = body; *p; p++)
13496 if (*p == ' ' || *p == '(')
13497 break;
13498
13499 if (*p == ' ' || *p == '\0')
13500 {
13501 /* It's an object-like macro. */
13502 int name_len = p - body;
13503 char *name = copy_string (body, name_len);
13504 const char *replacement;
13505
13506 if (*p == ' ')
13507 replacement = body + name_len + 1;
13508 else
13509 {
13510 dwarf2_macro_malformed_definition_complaint (body);
13511 replacement = body + name_len;
13512 }
13513
13514 macro_define_object (file, line, name, replacement);
13515
13516 xfree (name);
13517 }
13518 else if (*p == '(')
13519 {
13520 /* It's a function-like macro. */
13521 char *name = copy_string (body, p - body);
13522 int argc = 0;
13523 int argv_size = 1;
13524 char **argv = xmalloc (argv_size * sizeof (*argv));
13525
13526 p++;
13527
13528 p = consume_improper_spaces (p, body);
13529
13530 /* Parse the formal argument list. */
13531 while (*p && *p != ')')
13532 {
13533 /* Find the extent of the current argument name. */
13534 const char *arg_start = p;
13535
13536 while (*p && *p != ',' && *p != ')' && *p != ' ')
13537 p++;
13538
13539 if (! *p || p == arg_start)
13540 dwarf2_macro_malformed_definition_complaint (body);
13541 else
13542 {
13543 /* Make sure argv has room for the new argument. */
13544 if (argc >= argv_size)
13545 {
13546 argv_size *= 2;
13547 argv = xrealloc (argv, argv_size * sizeof (*argv));
13548 }
13549
13550 argv[argc++] = copy_string (arg_start, p - arg_start);
13551 }
13552
13553 p = consume_improper_spaces (p, body);
13554
13555 /* Consume the comma, if present. */
13556 if (*p == ',')
13557 {
13558 p++;
13559
13560 p = consume_improper_spaces (p, body);
13561 }
13562 }
13563
13564 if (*p == ')')
13565 {
13566 p++;
13567
13568 if (*p == ' ')
13569 /* Perfectly formed definition, no complaints. */
13570 macro_define_function (file, line, name,
13571 argc, (const char **) argv,
13572 p + 1);
13573 else if (*p == '\0')
13574 {
13575 /* Complain, but do define it. */
13576 dwarf2_macro_malformed_definition_complaint (body);
13577 macro_define_function (file, line, name,
13578 argc, (const char **) argv,
13579 p);
13580 }
13581 else
13582 /* Just complain. */
13583 dwarf2_macro_malformed_definition_complaint (body);
13584 }
13585 else
13586 /* Just complain. */
13587 dwarf2_macro_malformed_definition_complaint (body);
13588
13589 xfree (name);
13590 {
13591 int i;
13592
13593 for (i = 0; i < argc; i++)
13594 xfree (argv[i]);
13595 }
13596 xfree (argv);
13597 }
13598 else
13599 dwarf2_macro_malformed_definition_complaint (body);
13600 }
13601
13602
13603 static void
13604 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13605 char *comp_dir, bfd *abfd,
13606 struct dwarf2_cu *cu)
13607 {
13608 gdb_byte *mac_ptr, *mac_end;
13609 struct macro_source_file *current_file = 0;
13610 enum dwarf_macinfo_record_type macinfo_type;
13611 int at_commandline;
13612
13613 dwarf2_read_section (dwarf2_per_objfile->objfile,
13614 &dwarf2_per_objfile->macinfo);
13615 if (dwarf2_per_objfile->macinfo.buffer == NULL)
13616 {
13617 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13618 return;
13619 }
13620
13621 /* First pass: Find the name of the base filename.
13622 This filename is needed in order to process all macros whose definition
13623 (or undefinition) comes from the command line. These macros are defined
13624 before the first DW_MACINFO_start_file entry, and yet still need to be
13625 associated to the base file.
13626
13627 To determine the base file name, we scan the macro definitions until we
13628 reach the first DW_MACINFO_start_file entry. We then initialize
13629 CURRENT_FILE accordingly so that any macro definition found before the
13630 first DW_MACINFO_start_file can still be associated to the base file. */
13631
13632 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13633 mac_end = dwarf2_per_objfile->macinfo.buffer
13634 + dwarf2_per_objfile->macinfo.size;
13635
13636 do
13637 {
13638 /* Do we at least have room for a macinfo type byte? */
13639 if (mac_ptr >= mac_end)
13640 {
13641 /* Complaint is printed during the second pass as GDB will probably
13642 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13643 break;
13644 }
13645
13646 macinfo_type = read_1_byte (abfd, mac_ptr);
13647 mac_ptr++;
13648
13649 switch (macinfo_type)
13650 {
13651 /* A zero macinfo type indicates the end of the macro
13652 information. */
13653 case 0:
13654 break;
13655
13656 case DW_MACINFO_define:
13657 case DW_MACINFO_undef:
13658 /* Only skip the data by MAC_PTR. */
13659 {
13660 unsigned int bytes_read;
13661
13662 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13663 mac_ptr += bytes_read;
13664 read_direct_string (abfd, mac_ptr, &bytes_read);
13665 mac_ptr += bytes_read;
13666 }
13667 break;
13668
13669 case DW_MACINFO_start_file:
13670 {
13671 unsigned int bytes_read;
13672 int line, file;
13673
13674 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13675 mac_ptr += bytes_read;
13676 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13677 mac_ptr += bytes_read;
13678
13679 current_file = macro_start_file (file, line, current_file, comp_dir,
13680 lh, cu->objfile);
13681 }
13682 break;
13683
13684 case DW_MACINFO_end_file:
13685 /* No data to skip by MAC_PTR. */
13686 break;
13687
13688 case DW_MACINFO_vendor_ext:
13689 /* Only skip the data by MAC_PTR. */
13690 {
13691 unsigned int bytes_read;
13692
13693 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13694 mac_ptr += bytes_read;
13695 read_direct_string (abfd, mac_ptr, &bytes_read);
13696 mac_ptr += bytes_read;
13697 }
13698 break;
13699
13700 default:
13701 break;
13702 }
13703 } while (macinfo_type != 0 && current_file == NULL);
13704
13705 /* Second pass: Process all entries.
13706
13707 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13708 command-line macro definitions/undefinitions. This flag is unset when we
13709 reach the first DW_MACINFO_start_file entry. */
13710
13711 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13712
13713 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13714 GDB is still reading the definitions from command line. First
13715 DW_MACINFO_start_file will need to be ignored as it was already executed
13716 to create CURRENT_FILE for the main source holding also the command line
13717 definitions. On first met DW_MACINFO_start_file this flag is reset to
13718 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13719
13720 at_commandline = 1;
13721
13722 do
13723 {
13724 /* Do we at least have room for a macinfo type byte? */
13725 if (mac_ptr >= mac_end)
13726 {
13727 dwarf2_macros_too_long_complaint ();
13728 break;
13729 }
13730
13731 macinfo_type = read_1_byte (abfd, mac_ptr);
13732 mac_ptr++;
13733
13734 switch (macinfo_type)
13735 {
13736 /* A zero macinfo type indicates the end of the macro
13737 information. */
13738 case 0:
13739 break;
13740
13741 case DW_MACINFO_define:
13742 case DW_MACINFO_undef:
13743 {
13744 unsigned int bytes_read;
13745 int line;
13746 char *body;
13747
13748 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13749 mac_ptr += bytes_read;
13750 body = read_direct_string (abfd, mac_ptr, &bytes_read);
13751 mac_ptr += bytes_read;
13752
13753 if (! current_file)
13754 {
13755 /* DWARF violation as no main source is present. */
13756 complaint (&symfile_complaints,
13757 _("debug info with no main source gives macro %s "
13758 "on line %d: %s"),
13759 macinfo_type == DW_MACINFO_define ?
13760 _("definition") :
13761 macinfo_type == DW_MACINFO_undef ?
13762 _("undefinition") :
13763 _("something-or-other"), line, body);
13764 break;
13765 }
13766 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13767 complaint (&symfile_complaints,
13768 _("debug info gives %s macro %s with %s line %d: %s"),
13769 at_commandline ? _("command-line") : _("in-file"),
13770 macinfo_type == DW_MACINFO_define ?
13771 _("definition") :
13772 macinfo_type == DW_MACINFO_undef ?
13773 _("undefinition") :
13774 _("something-or-other"),
13775 line == 0 ? _("zero") : _("non-zero"), line, body);
13776
13777 if (macinfo_type == DW_MACINFO_define)
13778 parse_macro_definition (current_file, line, body);
13779 else if (macinfo_type == DW_MACINFO_undef)
13780 macro_undef (current_file, line, body);
13781 }
13782 break;
13783
13784 case DW_MACINFO_start_file:
13785 {
13786 unsigned int bytes_read;
13787 int line, file;
13788
13789 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13790 mac_ptr += bytes_read;
13791 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13792 mac_ptr += bytes_read;
13793
13794 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13795 complaint (&symfile_complaints,
13796 _("debug info gives source %d included "
13797 "from %s at %s line %d"),
13798 file, at_commandline ? _("command-line") : _("file"),
13799 line == 0 ? _("zero") : _("non-zero"), line);
13800
13801 if (at_commandline)
13802 {
13803 /* This DW_MACINFO_start_file was executed in the pass one. */
13804 at_commandline = 0;
13805 }
13806 else
13807 current_file = macro_start_file (file, line,
13808 current_file, comp_dir,
13809 lh, cu->objfile);
13810 }
13811 break;
13812
13813 case DW_MACINFO_end_file:
13814 if (! current_file)
13815 complaint (&symfile_complaints,
13816 _("macro debug info has an unmatched `close_file' directive"));
13817 else
13818 {
13819 current_file = current_file->included_by;
13820 if (! current_file)
13821 {
13822 enum dwarf_macinfo_record_type next_type;
13823
13824 /* GCC circa March 2002 doesn't produce the zero
13825 type byte marking the end of the compilation
13826 unit. Complain if it's not there, but exit no
13827 matter what. */
13828
13829 /* Do we at least have room for a macinfo type byte? */
13830 if (mac_ptr >= mac_end)
13831 {
13832 dwarf2_macros_too_long_complaint ();
13833 return;
13834 }
13835
13836 /* We don't increment mac_ptr here, so this is just
13837 a look-ahead. */
13838 next_type = read_1_byte (abfd, mac_ptr);
13839 if (next_type != 0)
13840 complaint (&symfile_complaints,
13841 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
13842
13843 return;
13844 }
13845 }
13846 break;
13847
13848 case DW_MACINFO_vendor_ext:
13849 {
13850 unsigned int bytes_read;
13851 int constant;
13852 char *string;
13853
13854 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13855 mac_ptr += bytes_read;
13856 string = read_direct_string (abfd, mac_ptr, &bytes_read);
13857 mac_ptr += bytes_read;
13858
13859 /* We don't recognize any vendor extensions. */
13860 }
13861 break;
13862 }
13863 } while (macinfo_type != 0);
13864 }
13865
13866 /* Check if the attribute's form is a DW_FORM_block*
13867 if so return true else false. */
13868 static int
13869 attr_form_is_block (struct attribute *attr)
13870 {
13871 return (attr == NULL ? 0 :
13872 attr->form == DW_FORM_block1
13873 || attr->form == DW_FORM_block2
13874 || attr->form == DW_FORM_block4
13875 || attr->form == DW_FORM_block
13876 || attr->form == DW_FORM_exprloc);
13877 }
13878
13879 /* Return non-zero if ATTR's value is a section offset --- classes
13880 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13881 You may use DW_UNSND (attr) to retrieve such offsets.
13882
13883 Section 7.5.4, "Attribute Encodings", explains that no attribute
13884 may have a value that belongs to more than one of these classes; it
13885 would be ambiguous if we did, because we use the same forms for all
13886 of them. */
13887 static int
13888 attr_form_is_section_offset (struct attribute *attr)
13889 {
13890 return (attr->form == DW_FORM_data4
13891 || attr->form == DW_FORM_data8
13892 || attr->form == DW_FORM_sec_offset);
13893 }
13894
13895
13896 /* Return non-zero if ATTR's value falls in the 'constant' class, or
13897 zero otherwise. When this function returns true, you can apply
13898 dwarf2_get_attr_constant_value to it.
13899
13900 However, note that for some attributes you must check
13901 attr_form_is_section_offset before using this test. DW_FORM_data4
13902 and DW_FORM_data8 are members of both the constant class, and of
13903 the classes that contain offsets into other debug sections
13904 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13905 that, if an attribute's can be either a constant or one of the
13906 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13907 taken as section offsets, not constants. */
13908 static int
13909 attr_form_is_constant (struct attribute *attr)
13910 {
13911 switch (attr->form)
13912 {
13913 case DW_FORM_sdata:
13914 case DW_FORM_udata:
13915 case DW_FORM_data1:
13916 case DW_FORM_data2:
13917 case DW_FORM_data4:
13918 case DW_FORM_data8:
13919 return 1;
13920 default:
13921 return 0;
13922 }
13923 }
13924
13925 static void
13926 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
13927 struct dwarf2_cu *cu)
13928 {
13929 if (attr_form_is_section_offset (attr)
13930 /* ".debug_loc" may not exist at all, or the offset may be outside
13931 the section. If so, fall through to the complaint in the
13932 other branch. */
13933 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
13934 {
13935 struct dwarf2_loclist_baton *baton;
13936
13937 baton = obstack_alloc (&cu->objfile->objfile_obstack,
13938 sizeof (struct dwarf2_loclist_baton));
13939 baton->per_cu = cu->per_cu;
13940 gdb_assert (baton->per_cu);
13941
13942 dwarf2_read_section (dwarf2_per_objfile->objfile,
13943 &dwarf2_per_objfile->loc);
13944
13945 /* We don't know how long the location list is, but make sure we
13946 don't run off the edge of the section. */
13947 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13948 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
13949 baton->base_address = cu->base_address;
13950 if (cu->base_known == 0)
13951 complaint (&symfile_complaints,
13952 _("Location list used without specifying the CU base address."));
13953
13954 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
13955 SYMBOL_LOCATION_BATON (sym) = baton;
13956 }
13957 else
13958 {
13959 struct dwarf2_locexpr_baton *baton;
13960
13961 baton = obstack_alloc (&cu->objfile->objfile_obstack,
13962 sizeof (struct dwarf2_locexpr_baton));
13963 baton->per_cu = cu->per_cu;
13964 gdb_assert (baton->per_cu);
13965
13966 if (attr_form_is_block (attr))
13967 {
13968 /* Note that we're just copying the block's data pointer
13969 here, not the actual data. We're still pointing into the
13970 info_buffer for SYM's objfile; right now we never release
13971 that buffer, but when we do clean up properly this may
13972 need to change. */
13973 baton->size = DW_BLOCK (attr)->size;
13974 baton->data = DW_BLOCK (attr)->data;
13975 }
13976 else
13977 {
13978 dwarf2_invalid_attrib_class_complaint ("location description",
13979 SYMBOL_NATURAL_NAME (sym));
13980 baton->size = 0;
13981 baton->data = NULL;
13982 }
13983
13984 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13985 SYMBOL_LOCATION_BATON (sym) = baton;
13986 }
13987 }
13988
13989 /* Return the OBJFILE associated with the compilation unit CU. If CU
13990 came from a separate debuginfo file, then the master objfile is
13991 returned. */
13992
13993 struct objfile *
13994 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
13995 {
13996 struct objfile *objfile = per_cu->objfile;
13997
13998 /* Return the master objfile, so that we can report and look up the
13999 correct file containing this variable. */
14000 if (objfile->separate_debug_objfile_backlink)
14001 objfile = objfile->separate_debug_objfile_backlink;
14002
14003 return objfile;
14004 }
14005
14006 /* Return the address size given in the compilation unit header for CU. */
14007
14008 CORE_ADDR
14009 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14010 {
14011 if (per_cu->cu)
14012 return per_cu->cu->header.addr_size;
14013 else
14014 {
14015 /* If the CU is not currently read in, we re-read its header. */
14016 struct objfile *objfile = per_cu->objfile;
14017 struct dwarf2_per_objfile *per_objfile
14018 = objfile_data (objfile, dwarf2_objfile_data_key);
14019 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14020 struct comp_unit_head cu_header;
14021
14022 memset (&cu_header, 0, sizeof cu_header);
14023 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14024 return cu_header.addr_size;
14025 }
14026 }
14027
14028 /* Return the offset size given in the compilation unit header for CU. */
14029
14030 int
14031 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14032 {
14033 if (per_cu->cu)
14034 return per_cu->cu->header.offset_size;
14035 else
14036 {
14037 /* If the CU is not currently read in, we re-read its header. */
14038 struct objfile *objfile = per_cu->objfile;
14039 struct dwarf2_per_objfile *per_objfile
14040 = objfile_data (objfile, dwarf2_objfile_data_key);
14041 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14042 struct comp_unit_head cu_header;
14043
14044 memset (&cu_header, 0, sizeof cu_header);
14045 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14046 return cu_header.offset_size;
14047 }
14048 }
14049
14050 /* Return the text offset of the CU. The returned offset comes from
14051 this CU's objfile. If this objfile came from a separate debuginfo
14052 file, then the offset may be different from the corresponding
14053 offset in the parent objfile. */
14054
14055 CORE_ADDR
14056 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14057 {
14058 struct objfile *objfile = per_cu->objfile;
14059
14060 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14061 }
14062
14063 /* Locate the .debug_info compilation unit from CU's objfile which contains
14064 the DIE at OFFSET. Raises an error on failure. */
14065
14066 static struct dwarf2_per_cu_data *
14067 dwarf2_find_containing_comp_unit (unsigned int offset,
14068 struct objfile *objfile)
14069 {
14070 struct dwarf2_per_cu_data *this_cu;
14071 int low, high;
14072
14073 low = 0;
14074 high = dwarf2_per_objfile->n_comp_units - 1;
14075 while (high > low)
14076 {
14077 int mid = low + (high - low) / 2;
14078
14079 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14080 high = mid;
14081 else
14082 low = mid + 1;
14083 }
14084 gdb_assert (low == high);
14085 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14086 {
14087 if (low == 0)
14088 error (_("Dwarf Error: could not find partial DIE containing "
14089 "offset 0x%lx [in module %s]"),
14090 (long) offset, bfd_get_filename (objfile->obfd));
14091
14092 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14093 return dwarf2_per_objfile->all_comp_units[low-1];
14094 }
14095 else
14096 {
14097 this_cu = dwarf2_per_objfile->all_comp_units[low];
14098 if (low == dwarf2_per_objfile->n_comp_units - 1
14099 && offset >= this_cu->offset + this_cu->length)
14100 error (_("invalid dwarf2 offset %u"), offset);
14101 gdb_assert (offset < this_cu->offset + this_cu->length);
14102 return this_cu;
14103 }
14104 }
14105
14106 /* Locate the compilation unit from OBJFILE which is located at exactly
14107 OFFSET. Raises an error on failure. */
14108
14109 static struct dwarf2_per_cu_data *
14110 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14111 {
14112 struct dwarf2_per_cu_data *this_cu;
14113
14114 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14115 if (this_cu->offset != offset)
14116 error (_("no compilation unit with offset %u."), offset);
14117 return this_cu;
14118 }
14119
14120 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
14121
14122 static struct dwarf2_cu *
14123 alloc_one_comp_unit (struct objfile *objfile)
14124 {
14125 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14126 cu->objfile = objfile;
14127 obstack_init (&cu->comp_unit_obstack);
14128 return cu;
14129 }
14130
14131 /* Release one cached compilation unit, CU. We unlink it from the tree
14132 of compilation units, but we don't remove it from the read_in_chain;
14133 the caller is responsible for that.
14134 NOTE: DATA is a void * because this function is also used as a
14135 cleanup routine. */
14136
14137 static void
14138 free_one_comp_unit (void *data)
14139 {
14140 struct dwarf2_cu *cu = data;
14141
14142 if (cu->per_cu != NULL)
14143 cu->per_cu->cu = NULL;
14144 cu->per_cu = NULL;
14145
14146 obstack_free (&cu->comp_unit_obstack, NULL);
14147
14148 xfree (cu);
14149 }
14150
14151 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14152 when we're finished with it. We can't free the pointer itself, but be
14153 sure to unlink it from the cache. Also release any associated storage
14154 and perform cache maintenance.
14155
14156 Only used during partial symbol parsing. */
14157
14158 static void
14159 free_stack_comp_unit (void *data)
14160 {
14161 struct dwarf2_cu *cu = data;
14162
14163 obstack_free (&cu->comp_unit_obstack, NULL);
14164 cu->partial_dies = NULL;
14165
14166 if (cu->per_cu != NULL)
14167 {
14168 /* This compilation unit is on the stack in our caller, so we
14169 should not xfree it. Just unlink it. */
14170 cu->per_cu->cu = NULL;
14171 cu->per_cu = NULL;
14172
14173 /* If we had a per-cu pointer, then we may have other compilation
14174 units loaded, so age them now. */
14175 age_cached_comp_units ();
14176 }
14177 }
14178
14179 /* Free all cached compilation units. */
14180
14181 static void
14182 free_cached_comp_units (void *data)
14183 {
14184 struct dwarf2_per_cu_data *per_cu, **last_chain;
14185
14186 per_cu = dwarf2_per_objfile->read_in_chain;
14187 last_chain = &dwarf2_per_objfile->read_in_chain;
14188 while (per_cu != NULL)
14189 {
14190 struct dwarf2_per_cu_data *next_cu;
14191
14192 next_cu = per_cu->cu->read_in_chain;
14193
14194 free_one_comp_unit (per_cu->cu);
14195 *last_chain = next_cu;
14196
14197 per_cu = next_cu;
14198 }
14199 }
14200
14201 /* Increase the age counter on each cached compilation unit, and free
14202 any that are too old. */
14203
14204 static void
14205 age_cached_comp_units (void)
14206 {
14207 struct dwarf2_per_cu_data *per_cu, **last_chain;
14208
14209 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14210 per_cu = dwarf2_per_objfile->read_in_chain;
14211 while (per_cu != NULL)
14212 {
14213 per_cu->cu->last_used ++;
14214 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14215 dwarf2_mark (per_cu->cu);
14216 per_cu = per_cu->cu->read_in_chain;
14217 }
14218
14219 per_cu = dwarf2_per_objfile->read_in_chain;
14220 last_chain = &dwarf2_per_objfile->read_in_chain;
14221 while (per_cu != NULL)
14222 {
14223 struct dwarf2_per_cu_data *next_cu;
14224
14225 next_cu = per_cu->cu->read_in_chain;
14226
14227 if (!per_cu->cu->mark)
14228 {
14229 free_one_comp_unit (per_cu->cu);
14230 *last_chain = next_cu;
14231 }
14232 else
14233 last_chain = &per_cu->cu->read_in_chain;
14234
14235 per_cu = next_cu;
14236 }
14237 }
14238
14239 /* Remove a single compilation unit from the cache. */
14240
14241 static void
14242 free_one_cached_comp_unit (void *target_cu)
14243 {
14244 struct dwarf2_per_cu_data *per_cu, **last_chain;
14245
14246 per_cu = dwarf2_per_objfile->read_in_chain;
14247 last_chain = &dwarf2_per_objfile->read_in_chain;
14248 while (per_cu != NULL)
14249 {
14250 struct dwarf2_per_cu_data *next_cu;
14251
14252 next_cu = per_cu->cu->read_in_chain;
14253
14254 if (per_cu->cu == target_cu)
14255 {
14256 free_one_comp_unit (per_cu->cu);
14257 *last_chain = next_cu;
14258 break;
14259 }
14260 else
14261 last_chain = &per_cu->cu->read_in_chain;
14262
14263 per_cu = next_cu;
14264 }
14265 }
14266
14267 /* Release all extra memory associated with OBJFILE. */
14268
14269 void
14270 dwarf2_free_objfile (struct objfile *objfile)
14271 {
14272 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14273
14274 if (dwarf2_per_objfile == NULL)
14275 return;
14276
14277 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14278 free_cached_comp_units (NULL);
14279
14280 if (dwarf2_per_objfile->using_index)
14281 {
14282 int i;
14283
14284 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14285 {
14286 int j;
14287 struct dwarf2_per_cu_data *per_cu =
14288 dwarf2_per_objfile->all_comp_units[i];
14289
14290 if (!per_cu->v.quick->lines)
14291 continue;
14292
14293 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
14294 {
14295 if (per_cu->v.quick->file_names)
14296 xfree ((void *) per_cu->v.quick->file_names[j]);
14297 if (per_cu->v.quick->full_names)
14298 xfree ((void *) per_cu->v.quick->full_names[j]);
14299 }
14300
14301 free_line_header (per_cu->v.quick->lines);
14302 }
14303 }
14304
14305 /* Everything else should be on the objfile obstack. */
14306 }
14307
14308 /* A pair of DIE offset and GDB type pointer. We store these
14309 in a hash table separate from the DIEs, and preserve them
14310 when the DIEs are flushed out of cache. */
14311
14312 struct dwarf2_offset_and_type
14313 {
14314 unsigned int offset;
14315 struct type *type;
14316 };
14317
14318 /* Hash function for a dwarf2_offset_and_type. */
14319
14320 static hashval_t
14321 offset_and_type_hash (const void *item)
14322 {
14323 const struct dwarf2_offset_and_type *ofs = item;
14324
14325 return ofs->offset;
14326 }
14327
14328 /* Equality function for a dwarf2_offset_and_type. */
14329
14330 static int
14331 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14332 {
14333 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14334 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14335
14336 return ofs_lhs->offset == ofs_rhs->offset;
14337 }
14338
14339 /* Set the type associated with DIE to TYPE. Save it in CU's hash
14340 table if necessary. For convenience, return TYPE.
14341
14342 The DIEs reading must have careful ordering to:
14343 * Not cause infite loops trying to read in DIEs as a prerequisite for
14344 reading current DIE.
14345 * Not trying to dereference contents of still incompletely read in types
14346 while reading in other DIEs.
14347 * Enable referencing still incompletely read in types just by a pointer to
14348 the type without accessing its fields.
14349
14350 Therefore caller should follow these rules:
14351 * Try to fetch any prerequisite types we may need to build this DIE type
14352 before building the type and calling set_die_type.
14353 * After building type call set_die_type for current DIE as soon as
14354 possible before fetching more types to complete the current type.
14355 * Make the type as complete as possible before fetching more types. */
14356
14357 static struct type *
14358 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14359 {
14360 struct dwarf2_offset_and_type **slot, ofs;
14361 struct objfile *objfile = cu->objfile;
14362 htab_t *type_hash_ptr;
14363
14364 /* For Ada types, make sure that the gnat-specific data is always
14365 initialized (if not already set). There are a few types where
14366 we should not be doing so, because the type-specific area is
14367 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14368 where the type-specific area is used to store the floatformat).
14369 But this is not a problem, because the gnat-specific information
14370 is actually not needed for these types. */
14371 if (need_gnat_info (cu)
14372 && TYPE_CODE (type) != TYPE_CODE_FUNC
14373 && TYPE_CODE (type) != TYPE_CODE_FLT
14374 && !HAVE_GNAT_AUX_INFO (type))
14375 INIT_GNAT_SPECIFIC (type);
14376
14377 if (cu->per_cu->from_debug_types)
14378 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14379 else
14380 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14381
14382 if (*type_hash_ptr == NULL)
14383 {
14384 *type_hash_ptr
14385 = htab_create_alloc_ex (127,
14386 offset_and_type_hash,
14387 offset_and_type_eq,
14388 NULL,
14389 &objfile->objfile_obstack,
14390 hashtab_obstack_allocate,
14391 dummy_obstack_deallocate);
14392 }
14393
14394 ofs.offset = die->offset;
14395 ofs.type = type;
14396 slot = (struct dwarf2_offset_and_type **)
14397 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14398 if (*slot)
14399 complaint (&symfile_complaints,
14400 _("A problem internal to GDB: DIE 0x%x has type already set"),
14401 die->offset);
14402 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14403 **slot = ofs;
14404 return type;
14405 }
14406
14407 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14408 table, or return NULL if the die does not have a saved type. */
14409
14410 static struct type *
14411 get_die_type_at_offset (unsigned int offset,
14412 struct dwarf2_per_cu_data *per_cu)
14413 {
14414 struct dwarf2_offset_and_type *slot, ofs;
14415 htab_t type_hash;
14416
14417 if (per_cu->from_debug_types)
14418 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14419 else
14420 type_hash = dwarf2_per_objfile->debug_info_type_hash;
14421 if (type_hash == NULL)
14422 return NULL;
14423
14424 ofs.offset = offset;
14425 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14426 if (slot)
14427 return slot->type;
14428 else
14429 return NULL;
14430 }
14431
14432 /* Look up the type for DIE in the appropriate type_hash table,
14433 or return NULL if DIE does not have a saved type. */
14434
14435 static struct type *
14436 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14437 {
14438 return get_die_type_at_offset (die->offset, cu->per_cu);
14439 }
14440
14441 /* Add a dependence relationship from CU to REF_PER_CU. */
14442
14443 static void
14444 dwarf2_add_dependence (struct dwarf2_cu *cu,
14445 struct dwarf2_per_cu_data *ref_per_cu)
14446 {
14447 void **slot;
14448
14449 if (cu->dependencies == NULL)
14450 cu->dependencies
14451 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14452 NULL, &cu->comp_unit_obstack,
14453 hashtab_obstack_allocate,
14454 dummy_obstack_deallocate);
14455
14456 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14457 if (*slot == NULL)
14458 *slot = ref_per_cu;
14459 }
14460
14461 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14462 Set the mark field in every compilation unit in the
14463 cache that we must keep because we are keeping CU. */
14464
14465 static int
14466 dwarf2_mark_helper (void **slot, void *data)
14467 {
14468 struct dwarf2_per_cu_data *per_cu;
14469
14470 per_cu = (struct dwarf2_per_cu_data *) *slot;
14471 if (per_cu->cu->mark)
14472 return 1;
14473 per_cu->cu->mark = 1;
14474
14475 if (per_cu->cu->dependencies != NULL)
14476 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14477
14478 return 1;
14479 }
14480
14481 /* Set the mark field in CU and in every other compilation unit in the
14482 cache that we must keep because we are keeping CU. */
14483
14484 static void
14485 dwarf2_mark (struct dwarf2_cu *cu)
14486 {
14487 if (cu->mark)
14488 return;
14489 cu->mark = 1;
14490 if (cu->dependencies != NULL)
14491 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14492 }
14493
14494 static void
14495 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14496 {
14497 while (per_cu)
14498 {
14499 per_cu->cu->mark = 0;
14500 per_cu = per_cu->cu->read_in_chain;
14501 }
14502 }
14503
14504 /* Trivial hash function for partial_die_info: the hash value of a DIE
14505 is its offset in .debug_info for this objfile. */
14506
14507 static hashval_t
14508 partial_die_hash (const void *item)
14509 {
14510 const struct partial_die_info *part_die = item;
14511
14512 return part_die->offset;
14513 }
14514
14515 /* Trivial comparison function for partial_die_info structures: two DIEs
14516 are equal if they have the same offset. */
14517
14518 static int
14519 partial_die_eq (const void *item_lhs, const void *item_rhs)
14520 {
14521 const struct partial_die_info *part_die_lhs = item_lhs;
14522 const struct partial_die_info *part_die_rhs = item_rhs;
14523
14524 return part_die_lhs->offset == part_die_rhs->offset;
14525 }
14526
14527 static struct cmd_list_element *set_dwarf2_cmdlist;
14528 static struct cmd_list_element *show_dwarf2_cmdlist;
14529
14530 static void
14531 set_dwarf2_cmd (char *args, int from_tty)
14532 {
14533 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14534 }
14535
14536 static void
14537 show_dwarf2_cmd (char *args, int from_tty)
14538 {
14539 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14540 }
14541
14542 /* If section described by INFO was mmapped, munmap it now. */
14543
14544 static void
14545 munmap_section_buffer (struct dwarf2_section_info *info)
14546 {
14547 if (info->was_mmapped)
14548 {
14549 #ifdef HAVE_MMAP
14550 intptr_t begin = (intptr_t) info->buffer;
14551 intptr_t map_begin = begin & ~(pagesize - 1);
14552 size_t map_length = info->size + begin - map_begin;
14553
14554 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14555 #else
14556 /* Without HAVE_MMAP, we should never be here to begin with. */
14557 gdb_assert_not_reached ("no mmap support");
14558 #endif
14559 }
14560 }
14561
14562 /* munmap debug sections for OBJFILE, if necessary. */
14563
14564 static void
14565 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14566 {
14567 struct dwarf2_per_objfile *data = d;
14568
14569 /* This is sorted according to the order they're defined in to make it easier
14570 to keep in sync. */
14571 munmap_section_buffer (&data->info);
14572 munmap_section_buffer (&data->abbrev);
14573 munmap_section_buffer (&data->line);
14574 munmap_section_buffer (&data->loc);
14575 munmap_section_buffer (&data->macinfo);
14576 munmap_section_buffer (&data->str);
14577 munmap_section_buffer (&data->ranges);
14578 munmap_section_buffer (&data->types);
14579 munmap_section_buffer (&data->frame);
14580 munmap_section_buffer (&data->eh_frame);
14581 munmap_section_buffer (&data->gdb_index);
14582 }
14583
14584 \f
14585
14586 /* The contents of the hash table we create when building the string
14587 table. */
14588 struct strtab_entry
14589 {
14590 offset_type offset;
14591 const char *str;
14592 };
14593
14594 /* Hash function for a strtab_entry. */
14595 static hashval_t
14596 hash_strtab_entry (const void *e)
14597 {
14598 const struct strtab_entry *entry = e;
14599 return mapped_index_string_hash (entry->str);
14600 }
14601
14602 /* Equality function for a strtab_entry. */
14603 static int
14604 eq_strtab_entry (const void *a, const void *b)
14605 {
14606 const struct strtab_entry *ea = a;
14607 const struct strtab_entry *eb = b;
14608 return !strcmp (ea->str, eb->str);
14609 }
14610
14611 /* Create a strtab_entry hash table. */
14612 static htab_t
14613 create_strtab (void)
14614 {
14615 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14616 xfree, xcalloc, xfree);
14617 }
14618
14619 /* Add a string to the constant pool. Return the string's offset in
14620 host order. */
14621 static offset_type
14622 add_string (htab_t table, struct obstack *cpool, const char *str)
14623 {
14624 void **slot;
14625 struct strtab_entry entry;
14626 struct strtab_entry *result;
14627
14628 entry.str = str;
14629 slot = htab_find_slot (table, &entry, INSERT);
14630 if (*slot)
14631 result = *slot;
14632 else
14633 {
14634 result = XNEW (struct strtab_entry);
14635 result->offset = obstack_object_size (cpool);
14636 result->str = str;
14637 obstack_grow_str0 (cpool, str);
14638 *slot = result;
14639 }
14640 return result->offset;
14641 }
14642
14643 /* An entry in the symbol table. */
14644 struct symtab_index_entry
14645 {
14646 /* The name of the symbol. */
14647 const char *name;
14648 /* The offset of the name in the constant pool. */
14649 offset_type index_offset;
14650 /* A sorted vector of the indices of all the CUs that hold an object
14651 of this name. */
14652 VEC (offset_type) *cu_indices;
14653 };
14654
14655 /* The symbol table. This is a power-of-2-sized hash table. */
14656 struct mapped_symtab
14657 {
14658 offset_type n_elements;
14659 offset_type size;
14660 struct symtab_index_entry **data;
14661 };
14662
14663 /* Hash function for a symtab_index_entry. */
14664 static hashval_t
14665 hash_symtab_entry (const void *e)
14666 {
14667 const struct symtab_index_entry *entry = e;
14668 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14669 sizeof (offset_type) * VEC_length (offset_type,
14670 entry->cu_indices),
14671 0);
14672 }
14673
14674 /* Equality function for a symtab_index_entry. */
14675 static int
14676 eq_symtab_entry (const void *a, const void *b)
14677 {
14678 const struct symtab_index_entry *ea = a;
14679 const struct symtab_index_entry *eb = b;
14680 int len = VEC_length (offset_type, ea->cu_indices);
14681 if (len != VEC_length (offset_type, eb->cu_indices))
14682 return 0;
14683 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14684 VEC_address (offset_type, eb->cu_indices),
14685 sizeof (offset_type) * len);
14686 }
14687
14688 /* Destroy a symtab_index_entry. */
14689 static void
14690 delete_symtab_entry (void *p)
14691 {
14692 struct symtab_index_entry *entry = p;
14693 VEC_free (offset_type, entry->cu_indices);
14694 xfree (entry);
14695 }
14696
14697 /* Create a hash table holding symtab_index_entry objects. */
14698 static htab_t
14699 create_index_table (void)
14700 {
14701 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14702 delete_symtab_entry, xcalloc, xfree);
14703 }
14704
14705 /* Create a new mapped symtab object. */
14706 static struct mapped_symtab *
14707 create_mapped_symtab (void)
14708 {
14709 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14710 symtab->n_elements = 0;
14711 symtab->size = 1024;
14712 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14713 return symtab;
14714 }
14715
14716 /* Destroy a mapped_symtab. */
14717 static void
14718 cleanup_mapped_symtab (void *p)
14719 {
14720 struct mapped_symtab *symtab = p;
14721 /* The contents of the array are freed when the other hash table is
14722 destroyed. */
14723 xfree (symtab->data);
14724 xfree (symtab);
14725 }
14726
14727 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14728 the slot. */
14729 static struct symtab_index_entry **
14730 find_slot (struct mapped_symtab *symtab, const char *name)
14731 {
14732 offset_type index, step, hash = mapped_index_string_hash (name);
14733
14734 index = hash & (symtab->size - 1);
14735 step = ((hash * 17) & (symtab->size - 1)) | 1;
14736
14737 for (;;)
14738 {
14739 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14740 return &symtab->data[index];
14741 index = (index + step) & (symtab->size - 1);
14742 }
14743 }
14744
14745 /* Expand SYMTAB's hash table. */
14746 static void
14747 hash_expand (struct mapped_symtab *symtab)
14748 {
14749 offset_type old_size = symtab->size;
14750 offset_type i;
14751 struct symtab_index_entry **old_entries = symtab->data;
14752
14753 symtab->size *= 2;
14754 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14755
14756 for (i = 0; i < old_size; ++i)
14757 {
14758 if (old_entries[i])
14759 {
14760 struct symtab_index_entry **slot = find_slot (symtab,
14761 old_entries[i]->name);
14762 *slot = old_entries[i];
14763 }
14764 }
14765
14766 xfree (old_entries);
14767 }
14768
14769 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14770 is the index of the CU in which the symbol appears. */
14771 static void
14772 add_index_entry (struct mapped_symtab *symtab, const char *name,
14773 offset_type cu_index)
14774 {
14775 struct symtab_index_entry **slot;
14776
14777 ++symtab->n_elements;
14778 if (4 * symtab->n_elements / 3 >= symtab->size)
14779 hash_expand (symtab);
14780
14781 slot = find_slot (symtab, name);
14782 if (!*slot)
14783 {
14784 *slot = XNEW (struct symtab_index_entry);
14785 (*slot)->name = name;
14786 (*slot)->cu_indices = NULL;
14787 }
14788 /* Don't push an index twice. Due to how we add entries we only
14789 have to check the last one. */
14790 if (VEC_empty (offset_type, (*slot)->cu_indices)
14791 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14792 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14793 }
14794
14795 /* Add a vector of indices to the constant pool. */
14796 static offset_type
14797 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14798 struct symtab_index_entry *entry)
14799 {
14800 void **slot;
14801
14802 slot = htab_find_slot (index_table, entry, INSERT);
14803 if (!*slot)
14804 {
14805 offset_type len = VEC_length (offset_type, entry->cu_indices);
14806 offset_type val = MAYBE_SWAP (len);
14807 offset_type iter;
14808 int i;
14809
14810 *slot = entry;
14811 entry->index_offset = obstack_object_size (cpool);
14812
14813 obstack_grow (cpool, &val, sizeof (val));
14814 for (i = 0;
14815 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14816 ++i)
14817 {
14818 val = MAYBE_SWAP (iter);
14819 obstack_grow (cpool, &val, sizeof (val));
14820 }
14821 }
14822 else
14823 {
14824 struct symtab_index_entry *old_entry = *slot;
14825 entry->index_offset = old_entry->index_offset;
14826 entry = old_entry;
14827 }
14828 return entry->index_offset;
14829 }
14830
14831 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14832 constant pool entries going into the obstack CPOOL. */
14833 static void
14834 write_hash_table (struct mapped_symtab *symtab,
14835 struct obstack *output, struct obstack *cpool)
14836 {
14837 offset_type i;
14838 htab_t index_table;
14839 htab_t str_table;
14840
14841 index_table = create_index_table ();
14842 str_table = create_strtab ();
14843 /* We add all the index vectors to the constant pool first, to
14844 ensure alignment is ok. */
14845 for (i = 0; i < symtab->size; ++i)
14846 {
14847 if (symtab->data[i])
14848 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14849 }
14850
14851 /* Now write out the hash table. */
14852 for (i = 0; i < symtab->size; ++i)
14853 {
14854 offset_type str_off, vec_off;
14855
14856 if (symtab->data[i])
14857 {
14858 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14859 vec_off = symtab->data[i]->index_offset;
14860 }
14861 else
14862 {
14863 /* While 0 is a valid constant pool index, it is not valid
14864 to have 0 for both offsets. */
14865 str_off = 0;
14866 vec_off = 0;
14867 }
14868
14869 str_off = MAYBE_SWAP (str_off);
14870 vec_off = MAYBE_SWAP (vec_off);
14871
14872 obstack_grow (output, &str_off, sizeof (str_off));
14873 obstack_grow (output, &vec_off, sizeof (vec_off));
14874 }
14875
14876 htab_delete (str_table);
14877 htab_delete (index_table);
14878 }
14879
14880 /* Write an address entry to ADDR_OBSTACK. The addresses are taken
14881 from PST; CU_INDEX is the index of the CU in the vector of all
14882 CUs. */
14883 static void
14884 add_address_entry (struct objfile *objfile,
14885 struct obstack *addr_obstack, struct partial_symtab *pst,
14886 unsigned int cu_index)
14887 {
14888 offset_type offset;
14889 char addr[8];
14890 CORE_ADDR baseaddr;
14891
14892 /* Don't bother recording empty ranges. */
14893 if (pst->textlow == pst->texthigh)
14894 return;
14895
14896 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14897
14898 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14899 obstack_grow (addr_obstack, addr, 8);
14900 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14901 obstack_grow (addr_obstack, addr, 8);
14902 offset = MAYBE_SWAP (cu_index);
14903 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14904 }
14905
14906 /* Add a list of partial symbols to SYMTAB. */
14907 static void
14908 write_psymbols (struct mapped_symtab *symtab,
14909 struct partial_symbol **psymp,
14910 int count,
14911 offset_type cu_index)
14912 {
14913 for (; count-- > 0; ++psymp)
14914 {
14915 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14916 error (_("Ada is not currently supported by the index"));
14917 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14918 }
14919 }
14920
14921 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
14922 exception if there is an error. */
14923 static void
14924 write_obstack (FILE *file, struct obstack *obstack)
14925 {
14926 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14927 file)
14928 != obstack_object_size (obstack))
14929 error (_("couldn't data write to file"));
14930 }
14931
14932 /* Unlink a file if the argument is not NULL. */
14933 static void
14934 unlink_if_set (void *p)
14935 {
14936 char **filename = p;
14937 if (*filename)
14938 unlink (*filename);
14939 }
14940
14941 /* A helper struct used when iterating over debug_types. */
14942 struct signatured_type_index_data
14943 {
14944 struct objfile *objfile;
14945 struct mapped_symtab *symtab;
14946 struct obstack *types_list;
14947 int cu_index;
14948 };
14949
14950 /* A helper function that writes a single signatured_type to an
14951 obstack. */
14952 static int
14953 write_one_signatured_type (void **slot, void *d)
14954 {
14955 struct signatured_type_index_data *info = d;
14956 struct signatured_type *entry = (struct signatured_type *) *slot;
14957 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
14958 struct partial_symtab *psymtab = per_cu->v.psymtab;
14959 gdb_byte val[8];
14960
14961 write_psymbols (info->symtab,
14962 info->objfile->global_psymbols.list + psymtab->globals_offset,
14963 psymtab->n_global_syms, info->cu_index);
14964 write_psymbols (info->symtab,
14965 info->objfile->static_psymbols.list + psymtab->statics_offset,
14966 psymtab->n_static_syms, info->cu_index);
14967
14968 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
14969 obstack_grow (info->types_list, val, 8);
14970 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
14971 obstack_grow (info->types_list, val, 8);
14972 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
14973 obstack_grow (info->types_list, val, 8);
14974
14975 ++info->cu_index;
14976
14977 return 1;
14978 }
14979
14980 /* Create an index file for OBJFILE in the directory DIR. */
14981 static void
14982 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
14983 {
14984 struct cleanup *cleanup;
14985 char *filename, *cleanup_filename;
14986 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
14987 struct obstack cu_list, types_cu_list;
14988 int i;
14989 FILE *out_file;
14990 struct mapped_symtab *symtab;
14991 offset_type val, size_of_contents, total_len;
14992 struct stat st;
14993 char buf[8];
14994
14995 if (!objfile->psymtabs)
14996 return;
14997 if (dwarf2_per_objfile->using_index)
14998 error (_("Cannot use an index to create the index"));
14999
15000 if (stat (objfile->name, &st) < 0)
15001 perror_with_name (_("Could not stat"));
15002
15003 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15004 INDEX_SUFFIX, (char *) NULL);
15005 cleanup = make_cleanup (xfree, filename);
15006
15007 out_file = fopen (filename, "wb");
15008 if (!out_file)
15009 error (_("Can't open `%s' for writing"), filename);
15010
15011 cleanup_filename = filename;
15012 make_cleanup (unlink_if_set, &cleanup_filename);
15013
15014 symtab = create_mapped_symtab ();
15015 make_cleanup (cleanup_mapped_symtab, symtab);
15016
15017 obstack_init (&addr_obstack);
15018 make_cleanup_obstack_free (&addr_obstack);
15019
15020 obstack_init (&cu_list);
15021 make_cleanup_obstack_free (&cu_list);
15022
15023 obstack_init (&types_cu_list);
15024 make_cleanup_obstack_free (&types_cu_list);
15025
15026 /* The list is already sorted, so we don't need to do additional
15027 work here. Also, the debug_types entries do not appear in
15028 all_comp_units, but only in their own hash table. */
15029 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15030 {
15031 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15032 struct partial_symtab *psymtab = per_cu->v.psymtab;
15033 gdb_byte val[8];
15034
15035 write_psymbols (symtab,
15036 objfile->global_psymbols.list + psymtab->globals_offset,
15037 psymtab->n_global_syms, i);
15038 write_psymbols (symtab,
15039 objfile->static_psymbols.list + psymtab->statics_offset,
15040 psymtab->n_static_syms, i);
15041
15042 add_address_entry (objfile, &addr_obstack, psymtab, i);
15043
15044 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15045 obstack_grow (&cu_list, val, 8);
15046 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15047 obstack_grow (&cu_list, val, 8);
15048 }
15049
15050 /* Write out the .debug_type entries, if any. */
15051 if (dwarf2_per_objfile->signatured_types)
15052 {
15053 struct signatured_type_index_data sig_data;
15054
15055 sig_data.objfile = objfile;
15056 sig_data.symtab = symtab;
15057 sig_data.types_list = &types_cu_list;
15058 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15059 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15060 write_one_signatured_type, &sig_data);
15061 }
15062
15063 obstack_init (&constant_pool);
15064 make_cleanup_obstack_free (&constant_pool);
15065 obstack_init (&symtab_obstack);
15066 make_cleanup_obstack_free (&symtab_obstack);
15067 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15068
15069 obstack_init (&contents);
15070 make_cleanup_obstack_free (&contents);
15071 size_of_contents = 6 * sizeof (offset_type);
15072 total_len = size_of_contents;
15073
15074 /* The version number. */
15075 val = MAYBE_SWAP (2);
15076 obstack_grow (&contents, &val, sizeof (val));
15077
15078 /* The offset of the CU list from the start of the file. */
15079 val = MAYBE_SWAP (total_len);
15080 obstack_grow (&contents, &val, sizeof (val));
15081 total_len += obstack_object_size (&cu_list);
15082
15083 /* The offset of the types CU list from the start of the file. */
15084 val = MAYBE_SWAP (total_len);
15085 obstack_grow (&contents, &val, sizeof (val));
15086 total_len += obstack_object_size (&types_cu_list);
15087
15088 /* The offset of the address table from the start of the file. */
15089 val = MAYBE_SWAP (total_len);
15090 obstack_grow (&contents, &val, sizeof (val));
15091 total_len += obstack_object_size (&addr_obstack);
15092
15093 /* The offset of the symbol table from the start of the file. */
15094 val = MAYBE_SWAP (total_len);
15095 obstack_grow (&contents, &val, sizeof (val));
15096 total_len += obstack_object_size (&symtab_obstack);
15097
15098 /* The offset of the constant pool from the start of the file. */
15099 val = MAYBE_SWAP (total_len);
15100 obstack_grow (&contents, &val, sizeof (val));
15101 total_len += obstack_object_size (&constant_pool);
15102
15103 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15104
15105 write_obstack (out_file, &contents);
15106 write_obstack (out_file, &cu_list);
15107 write_obstack (out_file, &types_cu_list);
15108 write_obstack (out_file, &addr_obstack);
15109 write_obstack (out_file, &symtab_obstack);
15110 write_obstack (out_file, &constant_pool);
15111
15112 fclose (out_file);
15113
15114 /* We want to keep the file, so we set cleanup_filename to NULL
15115 here. See unlink_if_set. */
15116 cleanup_filename = NULL;
15117
15118 do_cleanups (cleanup);
15119 }
15120
15121 /* The mapped index file format is designed to be directly mmap()able
15122 on any architecture. In most cases, a datum is represented using a
15123 little-endian 32-bit integer value, called an offset_type. Big
15124 endian machines must byte-swap the values before using them.
15125 Exceptions to this rule are noted. The data is laid out such that
15126 alignment is always respected.
15127
15128 A mapped index consists of several sections.
15129
15130 1. The file header. This is a sequence of values, of offset_type
15131 unless otherwise noted:
15132 [0] The version number. Currently 1 or 2. The differences are
15133 noted below. Version 1 did not account for .debug_types sections;
15134 the presence of a .debug_types section invalidates any version 1
15135 index that may exist.
15136 [1] The offset, from the start of the file, of the CU list.
15137 [1.5] In version 2, the offset, from the start of the file, of the
15138 types CU list. This offset does not appear in version 1. Note
15139 that this can be empty, in which case this offset will be equal to
15140 the next offset.
15141 [2] The offset, from the start of the file, of the address section.
15142 [3] The offset, from the start of the file, of the symbol table.
15143 [4] The offset, from the start of the file, of the constant pool.
15144
15145 2. The CU list. This is a sequence of pairs of 64-bit
15146 little-endian values, sorted by the CU offset. The first element
15147 in each pair is the offset of a CU in the .debug_info section. The
15148 second element in each pair is the length of that CU. References
15149 to a CU elsewhere in the map are done using a CU index, which is
15150 just the 0-based index into this table. Note that if there are
15151 type CUs, then conceptually CUs and type CUs form a single list for
15152 the purposes of CU indices.
15153
15154 2.5 The types CU list. This does not appear in a version 1 index.
15155 This is a sequence of triplets of 64-bit little-endian values. In
15156 a triplet, the first value is the CU offset, the second value is
15157 the type offset in the CU, and the third value is the type
15158 signature. The types CU list is not sorted.
15159
15160 3. The address section. The address section consists of a sequence
15161 of address entries. Each address entry has three elements.
15162 [0] The low address. This is a 64-bit little-endian value.
15163 [1] The high address. This is a 64-bit little-endian value.
15164 [2] The CU index. This is an offset_type value.
15165
15166 4. The symbol table. This is a hash table. The size of the hash
15167 table is always a power of 2. The initial hash and the step are
15168 currently defined by the `find_slot' function.
15169
15170 Each slot in the hash table consists of a pair of offset_type
15171 values. The first value is the offset of the symbol's name in the
15172 constant pool. The second value is the offset of the CU vector in
15173 the constant pool.
15174
15175 If both values are 0, then this slot in the hash table is empty.
15176 This is ok because while 0 is a valid constant pool index, it
15177 cannot be a valid index for both a string and a CU vector.
15178
15179 A string in the constant pool is stored as a \0-terminated string,
15180 as you'd expect.
15181
15182 A CU vector in the constant pool is a sequence of offset_type
15183 values. The first value is the number of CU indices in the vector.
15184 Each subsequent value is the index of a CU in the CU list. This
15185 element in the hash table is used to indicate which CUs define the
15186 symbol.
15187
15188 5. The constant pool. This is simply a bunch of bytes. It is
15189 organized so that alignment is correct: CU vectors are stored
15190 first, followed by strings. */
15191 static void
15192 save_gdb_index_command (char *arg, int from_tty)
15193 {
15194 struct objfile *objfile;
15195
15196 if (!arg || !*arg)
15197 error (_("usage: save gdb-index DIRECTORY"));
15198
15199 ALL_OBJFILES (objfile)
15200 {
15201 struct stat st;
15202
15203 /* If the objfile does not correspond to an actual file, skip it. */
15204 if (stat (objfile->name, &st) < 0)
15205 continue;
15206
15207 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15208 if (dwarf2_per_objfile)
15209 {
15210 volatile struct gdb_exception except;
15211
15212 TRY_CATCH (except, RETURN_MASK_ERROR)
15213 {
15214 write_psymtabs_to_index (objfile, arg);
15215 }
15216 if (except.reason < 0)
15217 exception_fprintf (gdb_stderr, except,
15218 _("Error while writing index for `%s': "),
15219 objfile->name);
15220 }
15221 }
15222 }
15223
15224 \f
15225
15226 int dwarf2_always_disassemble;
15227
15228 static void
15229 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15230 struct cmd_list_element *c, const char *value)
15231 {
15232 fprintf_filtered (file, _("\
15233 Whether to always disassemble DWARF expressions is %s.\n"),
15234 value);
15235 }
15236
15237 void _initialize_dwarf2_read (void);
15238
15239 void
15240 _initialize_dwarf2_read (void)
15241 {
15242 struct cmd_list_element *c;
15243
15244 dwarf2_objfile_data_key
15245 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15246
15247 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15248 Set DWARF 2 specific variables.\n\
15249 Configure DWARF 2 variables such as the cache size"),
15250 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15251 0/*allow-unknown*/, &maintenance_set_cmdlist);
15252
15253 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15254 Show DWARF 2 specific variables\n\
15255 Show DWARF 2 variables such as the cache size"),
15256 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15257 0/*allow-unknown*/, &maintenance_show_cmdlist);
15258
15259 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15260 &dwarf2_max_cache_age, _("\
15261 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15262 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15263 A higher limit means that cached compilation units will be stored\n\
15264 in memory longer, and more total memory will be used. Zero disables\n\
15265 caching, which can slow down startup."),
15266 NULL,
15267 show_dwarf2_max_cache_age,
15268 &set_dwarf2_cmdlist,
15269 &show_dwarf2_cmdlist);
15270
15271 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15272 &dwarf2_always_disassemble, _("\
15273 Set whether `info address' always disassembles DWARF expressions."), _("\
15274 Show whether `info address' always disassembles DWARF expressions."), _("\
15275 When enabled, DWARF expressions are always printed in an assembly-like\n\
15276 syntax. When disabled, expressions will be printed in a more\n\
15277 conversational style, when possible."),
15278 NULL,
15279 show_dwarf2_always_disassemble,
15280 &set_dwarf2_cmdlist,
15281 &show_dwarf2_cmdlist);
15282
15283 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15284 Set debugging of the dwarf2 DIE reader."), _("\
15285 Show debugging of the dwarf2 DIE reader."), _("\
15286 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15287 The value is the maximum depth to print."),
15288 NULL,
15289 NULL,
15290 &setdebuglist, &showdebuglist);
15291
15292 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15293 _("Save a .gdb-index file"),
15294 &save_cmdlist);
15295 set_cmd_completer (c, filename_completer);
15296 }