* dwarf2read.c (dw2_lookup_symtab): Remove duplicate call to
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
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 symbol table, implemented as a hash table. */
158 const offset_type *symbol_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type symbol_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 /* Flag set if fixup_partial_die has been called on this die. */
581 unsigned int fixup_called : 1;
582
583 /* The name of this DIE. Normally the value of DW_AT_name, but
584 sometimes a default name for unnamed DIEs. */
585 char *name;
586
587 /* The linkage name, if present. */
588 const char *linkage_name;
589
590 /* The scope to prepend to our children. This is generally
591 allocated on the comp_unit_obstack, so will disappear
592 when this compilation unit leaves the cache. */
593 char *scope;
594
595 /* The location description associated with this DIE, if any. */
596 struct dwarf_block *locdesc;
597
598 /* If HAS_PC_INFO, the PC range associated with this DIE. */
599 CORE_ADDR lowpc;
600 CORE_ADDR highpc;
601
602 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
603 DW_AT_sibling, if any. */
604 /* NOTE: This member isn't strictly necessary, read_partial_die could
605 return DW_AT_sibling values to its caller load_partial_dies. */
606 gdb_byte *sibling;
607
608 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
609 DW_AT_specification (or DW_AT_abstract_origin or
610 DW_AT_extension). */
611 unsigned int spec_offset;
612
613 /* Pointers to this DIE's parent, first child, and next sibling,
614 if any. */
615 struct partial_die_info *die_parent, *die_child, *die_sibling;
616 };
617
618 /* This data structure holds the information of an abbrev. */
619 struct abbrev_info
620 {
621 unsigned int number; /* number identifying abbrev */
622 enum dwarf_tag tag; /* dwarf tag */
623 unsigned short has_children; /* boolean */
624 unsigned short num_attrs; /* number of attributes */
625 struct attr_abbrev *attrs; /* an array of attribute descriptions */
626 struct abbrev_info *next; /* next in chain */
627 };
628
629 struct attr_abbrev
630 {
631 ENUM_BITFIELD(dwarf_attribute) name : 16;
632 ENUM_BITFIELD(dwarf_form) form : 16;
633 };
634
635 /* Attributes have a name and a value */
636 struct attribute
637 {
638 ENUM_BITFIELD(dwarf_attribute) name : 16;
639 ENUM_BITFIELD(dwarf_form) form : 15;
640
641 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
642 field should be in u.str (existing only for DW_STRING) but it is kept
643 here for better struct attribute alignment. */
644 unsigned int string_is_canonical : 1;
645
646 union
647 {
648 char *str;
649 struct dwarf_block *blk;
650 ULONGEST unsnd;
651 LONGEST snd;
652 CORE_ADDR addr;
653 struct signatured_type *signatured_type;
654 }
655 u;
656 };
657
658 /* This data structure holds a complete die structure. */
659 struct die_info
660 {
661 /* DWARF-2 tag for this DIE. */
662 ENUM_BITFIELD(dwarf_tag) tag : 16;
663
664 /* Number of attributes */
665 unsigned char num_attrs;
666
667 /* True if we're presently building the full type name for the
668 type derived from this DIE. */
669 unsigned char building_fullname : 1;
670
671 /* Abbrev number */
672 unsigned int abbrev;
673
674 /* Offset in .debug_info or .debug_types section. */
675 unsigned int offset;
676
677 /* The dies in a compilation unit form an n-ary tree. PARENT
678 points to this die's parent; CHILD points to the first child of
679 this node; and all the children of a given node are chained
680 together via their SIBLING fields. */
681 struct die_info *child; /* Its first child, if any. */
682 struct die_info *sibling; /* Its next sibling, if any. */
683 struct die_info *parent; /* Its parent, if any. */
684
685 /* An array of attributes, with NUM_ATTRS elements. There may be
686 zero, but it's not common and zero-sized arrays are not
687 sufficiently portable C. */
688 struct attribute attrs[1];
689 };
690
691 struct function_range
692 {
693 const char *name;
694 CORE_ADDR lowpc, highpc;
695 int seen_line;
696 struct function_range *next;
697 };
698
699 /* Get at parts of an attribute structure */
700
701 #define DW_STRING(attr) ((attr)->u.str)
702 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
703 #define DW_UNSND(attr) ((attr)->u.unsnd)
704 #define DW_BLOCK(attr) ((attr)->u.blk)
705 #define DW_SND(attr) ((attr)->u.snd)
706 #define DW_ADDR(attr) ((attr)->u.addr)
707 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
708
709 /* Blocks are a bunch of untyped bytes. */
710 struct dwarf_block
711 {
712 unsigned int size;
713 gdb_byte *data;
714 };
715
716 #ifndef ATTR_ALLOC_CHUNK
717 #define ATTR_ALLOC_CHUNK 4
718 #endif
719
720 /* Allocate fields for structs, unions and enums in this size. */
721 #ifndef DW_FIELD_ALLOC_CHUNK
722 #define DW_FIELD_ALLOC_CHUNK 4
723 #endif
724
725 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
726 but this would require a corresponding change in unpack_field_as_long
727 and friends. */
728 static int bits_per_byte = 8;
729
730 /* The routines that read and process dies for a C struct or C++ class
731 pass lists of data member fields and lists of member function fields
732 in an instance of a field_info structure, as defined below. */
733 struct field_info
734 {
735 /* List of data member and baseclasses fields. */
736 struct nextfield
737 {
738 struct nextfield *next;
739 int accessibility;
740 int virtuality;
741 struct field field;
742 }
743 *fields, *baseclasses;
744
745 /* Number of fields (including baseclasses). */
746 int nfields;
747
748 /* Number of baseclasses. */
749 int nbaseclasses;
750
751 /* Set if the accesibility of one of the fields is not public. */
752 int non_public_fields;
753
754 /* Member function fields array, entries are allocated in the order they
755 are encountered in the object file. */
756 struct nextfnfield
757 {
758 struct nextfnfield *next;
759 struct fn_field fnfield;
760 }
761 *fnfields;
762
763 /* Member function fieldlist array, contains name of possibly overloaded
764 member function, number of overloaded member functions and a pointer
765 to the head of the member function field chain. */
766 struct fnfieldlist
767 {
768 char *name;
769 int length;
770 struct nextfnfield *head;
771 }
772 *fnfieldlists;
773
774 /* Number of entries in the fnfieldlists array. */
775 int nfnfields;
776
777 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
778 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
779 struct typedef_field_list
780 {
781 struct typedef_field field;
782 struct typedef_field_list *next;
783 }
784 *typedef_field_list;
785 unsigned typedef_field_list_count;
786 };
787
788 /* One item on the queue of compilation units to read in full symbols
789 for. */
790 struct dwarf2_queue_item
791 {
792 struct dwarf2_per_cu_data *per_cu;
793 struct dwarf2_queue_item *next;
794 };
795
796 /* The current queue. */
797 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
798
799 /* Loaded secondary compilation units are kept in memory until they
800 have not been referenced for the processing of this many
801 compilation units. Set this to zero to disable caching. Cache
802 sizes of up to at least twenty will improve startup time for
803 typical inter-CU-reference binaries, at an obvious memory cost. */
804 static int dwarf2_max_cache_age = 5;
805 static void
806 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
807 struct cmd_list_element *c, const char *value)
808 {
809 fprintf_filtered (file, _("\
810 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
811 value);
812 }
813
814
815 /* Various complaints about symbol reading that don't abort the process */
816
817 static void
818 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
819 {
820 complaint (&symfile_complaints,
821 _("statement list doesn't fit in .debug_line section"));
822 }
823
824 static void
825 dwarf2_debug_line_missing_file_complaint (void)
826 {
827 complaint (&symfile_complaints,
828 _(".debug_line section has line data without a file"));
829 }
830
831 static void
832 dwarf2_debug_line_missing_end_sequence_complaint (void)
833 {
834 complaint (&symfile_complaints,
835 _(".debug_line section has line program sequence without an end"));
836 }
837
838 static void
839 dwarf2_complex_location_expr_complaint (void)
840 {
841 complaint (&symfile_complaints, _("location expression too complex"));
842 }
843
844 static void
845 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
846 int arg3)
847 {
848 complaint (&symfile_complaints,
849 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
850 arg2, arg3);
851 }
852
853 static void
854 dwarf2_macros_too_long_complaint (void)
855 {
856 complaint (&symfile_complaints,
857 _("macro info runs off end of `.debug_macinfo' section"));
858 }
859
860 static void
861 dwarf2_macro_malformed_definition_complaint (const char *arg1)
862 {
863 complaint (&symfile_complaints,
864 _("macro debug info contains a malformed macro definition:\n`%s'"),
865 arg1);
866 }
867
868 static void
869 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
870 {
871 complaint (&symfile_complaints,
872 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
873 }
874
875 /* local function prototypes */
876
877 static void dwarf2_locate_sections (bfd *, asection *, void *);
878
879 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
880 struct objfile *);
881
882 static void dwarf2_build_psymtabs_hard (struct objfile *);
883
884 static void scan_partial_symbols (struct partial_die_info *,
885 CORE_ADDR *, CORE_ADDR *,
886 int, struct dwarf2_cu *);
887
888 static void add_partial_symbol (struct partial_die_info *,
889 struct dwarf2_cu *);
890
891 static void add_partial_namespace (struct partial_die_info *pdi,
892 CORE_ADDR *lowpc, CORE_ADDR *highpc,
893 int need_pc, struct dwarf2_cu *cu);
894
895 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
896 CORE_ADDR *highpc, int need_pc,
897 struct dwarf2_cu *cu);
898
899 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
900 struct dwarf2_cu *cu);
901
902 static void add_partial_subprogram (struct partial_die_info *pdi,
903 CORE_ADDR *lowpc, CORE_ADDR *highpc,
904 int need_pc, struct dwarf2_cu *cu);
905
906 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
907 gdb_byte *buffer, gdb_byte *info_ptr,
908 bfd *abfd, struct dwarf2_cu *cu);
909
910 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
911
912 static void psymtab_to_symtab_1 (struct partial_symtab *);
913
914 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
915
916 static void dwarf2_free_abbrev_table (void *);
917
918 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
919 struct dwarf2_cu *);
920
921 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
922 struct dwarf2_cu *);
923
924 static struct partial_die_info *load_partial_dies (bfd *,
925 gdb_byte *, gdb_byte *,
926 int, struct dwarf2_cu *);
927
928 static gdb_byte *read_partial_die (struct partial_die_info *,
929 struct abbrev_info *abbrev,
930 unsigned int, bfd *,
931 gdb_byte *, gdb_byte *,
932 struct dwarf2_cu *);
933
934 static struct partial_die_info *find_partial_die (unsigned int,
935 struct dwarf2_cu *);
936
937 static void fixup_partial_die (struct partial_die_info *,
938 struct dwarf2_cu *);
939
940 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
941 bfd *, gdb_byte *, struct dwarf2_cu *);
942
943 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
944 bfd *, gdb_byte *, struct dwarf2_cu *);
945
946 static unsigned int read_1_byte (bfd *, gdb_byte *);
947
948 static int read_1_signed_byte (bfd *, gdb_byte *);
949
950 static unsigned int read_2_bytes (bfd *, gdb_byte *);
951
952 static unsigned int read_4_bytes (bfd *, gdb_byte *);
953
954 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
955
956 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
957 unsigned int *);
958
959 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
960
961 static LONGEST read_checked_initial_length_and_offset
962 (bfd *, gdb_byte *, const struct comp_unit_head *,
963 unsigned int *, unsigned int *);
964
965 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
966 unsigned int *);
967
968 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
969
970 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
971
972 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
973
974 static char *read_indirect_string (bfd *, gdb_byte *,
975 const struct comp_unit_head *,
976 unsigned int *);
977
978 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
979
980 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
981
982 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
983
984 static void set_cu_language (unsigned int, struct dwarf2_cu *);
985
986 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
987 struct dwarf2_cu *);
988
989 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
990 unsigned int,
991 struct dwarf2_cu *);
992
993 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
994 struct dwarf2_cu *cu);
995
996 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
997
998 static struct die_info *die_specification (struct die_info *die,
999 struct dwarf2_cu **);
1000
1001 static void free_line_header (struct line_header *lh);
1002
1003 static void add_file_name (struct line_header *, char *, unsigned int,
1004 unsigned int, unsigned int);
1005
1006 static struct line_header *(dwarf_decode_line_header
1007 (unsigned int offset,
1008 bfd *abfd, struct dwarf2_cu *cu));
1009
1010 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1011 struct dwarf2_cu *, struct partial_symtab *);
1012
1013 static void dwarf2_start_subfile (char *, const char *, const char *);
1014
1015 static struct symbol *new_symbol (struct die_info *, struct type *,
1016 struct dwarf2_cu *);
1017
1018 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1019 struct dwarf2_cu *, struct symbol *);
1020
1021 static void dwarf2_const_value (struct attribute *, struct symbol *,
1022 struct dwarf2_cu *);
1023
1024 static void dwarf2_const_value_attr (struct attribute *attr,
1025 struct type *type,
1026 const char *name,
1027 struct obstack *obstack,
1028 struct dwarf2_cu *cu, long *value,
1029 gdb_byte **bytes,
1030 struct dwarf2_locexpr_baton **baton);
1031
1032 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1033
1034 static int need_gnat_info (struct dwarf2_cu *);
1035
1036 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1037
1038 static void set_descriptive_type (struct type *, struct die_info *,
1039 struct dwarf2_cu *);
1040
1041 static struct type *die_containing_type (struct die_info *,
1042 struct dwarf2_cu *);
1043
1044 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1045 struct dwarf2_cu *);
1046
1047 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1048
1049 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1050
1051 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1052
1053 static char *typename_concat (struct obstack *obs, const char *prefix,
1054 const char *suffix, int physname,
1055 struct dwarf2_cu *cu);
1056
1057 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1058
1059 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1060
1061 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1062
1063 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1064
1065 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1066 struct dwarf2_cu *, struct partial_symtab *);
1067
1068 static int dwarf2_get_pc_bounds (struct die_info *,
1069 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1070 struct partial_symtab *);
1071
1072 static void get_scope_pc_bounds (struct die_info *,
1073 CORE_ADDR *, CORE_ADDR *,
1074 struct dwarf2_cu *);
1075
1076 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1077 CORE_ADDR, struct dwarf2_cu *);
1078
1079 static void dwarf2_add_field (struct field_info *, struct die_info *,
1080 struct dwarf2_cu *);
1081
1082 static void dwarf2_attach_fields_to_type (struct field_info *,
1083 struct type *, struct dwarf2_cu *);
1084
1085 static void dwarf2_add_member_fn (struct field_info *,
1086 struct die_info *, struct type *,
1087 struct dwarf2_cu *);
1088
1089 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1090 struct type *, struct dwarf2_cu *);
1091
1092 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1093
1094 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1095
1096 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1097
1098 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1099
1100 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1101
1102 static struct type *read_module_type (struct die_info *die,
1103 struct dwarf2_cu *cu);
1104
1105 static const char *namespace_name (struct die_info *die,
1106 int *is_anonymous, struct dwarf2_cu *);
1107
1108 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1109
1110 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1111
1112 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1113 struct dwarf2_cu *);
1114
1115 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1116
1117 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1118 gdb_byte *info_ptr,
1119 gdb_byte **new_info_ptr,
1120 struct die_info *parent);
1121
1122 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1123 gdb_byte *info_ptr,
1124 gdb_byte **new_info_ptr,
1125 struct die_info *parent);
1126
1127 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1128 gdb_byte *info_ptr,
1129 gdb_byte **new_info_ptr,
1130 struct die_info *parent);
1131
1132 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1133 struct die_info **, gdb_byte *,
1134 int *);
1135
1136 static void process_die (struct die_info *, struct dwarf2_cu *);
1137
1138 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1139 struct obstack *);
1140
1141 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1142
1143 static const char *dwarf2_full_name (char *name,
1144 struct die_info *die,
1145 struct dwarf2_cu *cu);
1146
1147 static struct die_info *dwarf2_extension (struct die_info *die,
1148 struct dwarf2_cu **);
1149
1150 static char *dwarf_tag_name (unsigned int);
1151
1152 static char *dwarf_attr_name (unsigned int);
1153
1154 static char *dwarf_form_name (unsigned int);
1155
1156 static char *dwarf_bool_name (unsigned int);
1157
1158 static char *dwarf_type_encoding_name (unsigned int);
1159
1160 #if 0
1161 static char *dwarf_cfi_name (unsigned int);
1162 #endif
1163
1164 static struct die_info *sibling_die (struct die_info *);
1165
1166 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1167
1168 static void dump_die_for_error (struct die_info *);
1169
1170 static void dump_die_1 (struct ui_file *, int level, int max_level,
1171 struct die_info *);
1172
1173 /*static*/ void dump_die (struct die_info *, int max_level);
1174
1175 static void store_in_ref_table (struct die_info *,
1176 struct dwarf2_cu *);
1177
1178 static int is_ref_attr (struct attribute *);
1179
1180 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1181
1182 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1183
1184 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1185 struct attribute *,
1186 struct dwarf2_cu **);
1187
1188 static struct die_info *follow_die_ref (struct die_info *,
1189 struct attribute *,
1190 struct dwarf2_cu **);
1191
1192 static struct die_info *follow_die_sig (struct die_info *,
1193 struct attribute *,
1194 struct dwarf2_cu **);
1195
1196 static void read_signatured_type_at_offset (struct objfile *objfile,
1197 unsigned int offset);
1198
1199 static void read_signatured_type (struct objfile *,
1200 struct signatured_type *type_sig);
1201
1202 /* memory allocation interface */
1203
1204 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1205
1206 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1207
1208 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1209
1210 static void initialize_cu_func_list (struct dwarf2_cu *);
1211
1212 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1213 struct dwarf2_cu *);
1214
1215 static void dwarf_decode_macros (struct line_header *, unsigned int,
1216 char *, bfd *, struct dwarf2_cu *);
1217
1218 static int attr_form_is_block (struct attribute *);
1219
1220 static int attr_form_is_section_offset (struct attribute *);
1221
1222 static int attr_form_is_constant (struct attribute *);
1223
1224 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1225 struct dwarf2_loclist_baton *baton,
1226 struct attribute *attr);
1227
1228 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1229 struct symbol *sym,
1230 struct dwarf2_cu *cu);
1231
1232 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1233 struct abbrev_info *abbrev,
1234 struct dwarf2_cu *cu);
1235
1236 static void free_stack_comp_unit (void *);
1237
1238 static hashval_t partial_die_hash (const void *item);
1239
1240 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1241
1242 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1243 (unsigned int offset, struct objfile *objfile);
1244
1245 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1246 (unsigned int offset, struct objfile *objfile);
1247
1248 static void init_one_comp_unit (struct dwarf2_cu *cu,
1249 struct objfile *objfile);
1250
1251 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1252 struct die_info *comp_unit_die);
1253
1254 static void free_one_comp_unit (void *);
1255
1256 static void free_cached_comp_units (void *);
1257
1258 static void age_cached_comp_units (void);
1259
1260 static void free_one_cached_comp_unit (void *);
1261
1262 static struct type *set_die_type (struct die_info *, struct type *,
1263 struct dwarf2_cu *);
1264
1265 static void create_all_comp_units (struct objfile *);
1266
1267 static int create_debug_types_hash_table (struct objfile *objfile);
1268
1269 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1270 struct objfile *);
1271
1272 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1273
1274 static void dwarf2_add_dependence (struct dwarf2_cu *,
1275 struct dwarf2_per_cu_data *);
1276
1277 static void dwarf2_mark (struct dwarf2_cu *);
1278
1279 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1280
1281 static struct type *get_die_type_at_offset (unsigned int,
1282 struct dwarf2_per_cu_data *per_cu);
1283
1284 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1285
1286 static void dwarf2_release_queue (void *dummy);
1287
1288 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1289 struct objfile *objfile);
1290
1291 static void process_queue (struct objfile *objfile);
1292
1293 static void find_file_and_directory (struct die_info *die,
1294 struct dwarf2_cu *cu,
1295 char **name, char **comp_dir);
1296
1297 static char *file_full_name (int file, struct line_header *lh,
1298 const char *comp_dir);
1299
1300 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1301 gdb_byte *info_ptr,
1302 gdb_byte *buffer,
1303 unsigned int buffer_size,
1304 bfd *abfd);
1305
1306 static void init_cu_die_reader (struct die_reader_specs *reader,
1307 struct dwarf2_cu *cu);
1308
1309 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1310
1311 #if WORDS_BIGENDIAN
1312
1313 /* Convert VALUE between big- and little-endian. */
1314 static offset_type
1315 byte_swap (offset_type value)
1316 {
1317 offset_type result;
1318
1319 result = (value & 0xff) << 24;
1320 result |= (value & 0xff00) << 8;
1321 result |= (value & 0xff0000) >> 8;
1322 result |= (value & 0xff000000) >> 24;
1323 return result;
1324 }
1325
1326 #define MAYBE_SWAP(V) byte_swap (V)
1327
1328 #else
1329 #define MAYBE_SWAP(V) (V)
1330 #endif /* WORDS_BIGENDIAN */
1331
1332 /* The suffix for an index file. */
1333 #define INDEX_SUFFIX ".gdb-index"
1334
1335 static const char *dwarf2_physname (char *name, struct die_info *die,
1336 struct dwarf2_cu *cu);
1337
1338 /* Try to locate the sections we need for DWARF 2 debugging
1339 information and return true if we have enough to do something. */
1340
1341 int
1342 dwarf2_has_info (struct objfile *objfile)
1343 {
1344 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1345 if (!dwarf2_per_objfile)
1346 {
1347 /* Initialize per-objfile state. */
1348 struct dwarf2_per_objfile *data
1349 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1350
1351 memset (data, 0, sizeof (*data));
1352 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1353 dwarf2_per_objfile = data;
1354
1355 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1356 dwarf2_per_objfile->objfile = objfile;
1357 }
1358 return (dwarf2_per_objfile->info.asection != NULL
1359 && dwarf2_per_objfile->abbrev.asection != NULL);
1360 }
1361
1362 /* When loading sections, we can either look for ".<name>", or for
1363 * ".z<name>", which indicates a compressed section. */
1364
1365 static int
1366 section_is_p (const char *section_name, const char *name)
1367 {
1368 return (section_name[0] == '.'
1369 && (strcmp (section_name + 1, name) == 0
1370 || (section_name[1] == 'z'
1371 && strcmp (section_name + 2, name) == 0)));
1372 }
1373
1374 /* This function is mapped across the sections and remembers the
1375 offset and size of each of the debugging sections we are interested
1376 in. */
1377
1378 static void
1379 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1380 {
1381 if (section_is_p (sectp->name, INFO_SECTION))
1382 {
1383 dwarf2_per_objfile->info.asection = sectp;
1384 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1385 }
1386 else if (section_is_p (sectp->name, ABBREV_SECTION))
1387 {
1388 dwarf2_per_objfile->abbrev.asection = sectp;
1389 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1390 }
1391 else if (section_is_p (sectp->name, LINE_SECTION))
1392 {
1393 dwarf2_per_objfile->line.asection = sectp;
1394 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1395 }
1396 else if (section_is_p (sectp->name, LOC_SECTION))
1397 {
1398 dwarf2_per_objfile->loc.asection = sectp;
1399 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1400 }
1401 else if (section_is_p (sectp->name, MACINFO_SECTION))
1402 {
1403 dwarf2_per_objfile->macinfo.asection = sectp;
1404 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1405 }
1406 else if (section_is_p (sectp->name, STR_SECTION))
1407 {
1408 dwarf2_per_objfile->str.asection = sectp;
1409 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1410 }
1411 else if (section_is_p (sectp->name, FRAME_SECTION))
1412 {
1413 dwarf2_per_objfile->frame.asection = sectp;
1414 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1415 }
1416 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1417 {
1418 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1419
1420 if (aflag & SEC_HAS_CONTENTS)
1421 {
1422 dwarf2_per_objfile->eh_frame.asection = sectp;
1423 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1424 }
1425 }
1426 else if (section_is_p (sectp->name, RANGES_SECTION))
1427 {
1428 dwarf2_per_objfile->ranges.asection = sectp;
1429 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1430 }
1431 else if (section_is_p (sectp->name, TYPES_SECTION))
1432 {
1433 dwarf2_per_objfile->types.asection = sectp;
1434 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1435 }
1436 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1437 {
1438 dwarf2_per_objfile->gdb_index.asection = sectp;
1439 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1440 }
1441
1442 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1443 && bfd_section_vma (abfd, sectp) == 0)
1444 dwarf2_per_objfile->has_section_at_zero = 1;
1445 }
1446
1447 /* Decompress a section that was compressed using zlib. Store the
1448 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1449
1450 static void
1451 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1452 gdb_byte **outbuf, bfd_size_type *outsize)
1453 {
1454 bfd *abfd = objfile->obfd;
1455 #ifndef HAVE_ZLIB_H
1456 error (_("Support for zlib-compressed DWARF data (from '%s') "
1457 "is disabled in this copy of GDB"),
1458 bfd_get_filename (abfd));
1459 #else
1460 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1461 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1462 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1463 bfd_size_type uncompressed_size;
1464 gdb_byte *uncompressed_buffer;
1465 z_stream strm;
1466 int rc;
1467 int header_size = 12;
1468
1469 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1470 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1471 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1472 bfd_get_filename (abfd));
1473
1474 /* Read the zlib header. In this case, it should be "ZLIB" followed
1475 by the uncompressed section size, 8 bytes in big-endian order. */
1476 if (compressed_size < header_size
1477 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1478 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1479 bfd_get_filename (abfd));
1480 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1481 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1482 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1483 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1484 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1485 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[11];
1488
1489 /* It is possible the section consists of several compressed
1490 buffers concatenated together, so we uncompress in a loop. */
1491 strm.zalloc = NULL;
1492 strm.zfree = NULL;
1493 strm.opaque = NULL;
1494 strm.avail_in = compressed_size - header_size;
1495 strm.next_in = (Bytef*) compressed_buffer + header_size;
1496 strm.avail_out = uncompressed_size;
1497 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1498 uncompressed_size);
1499 rc = inflateInit (&strm);
1500 while (strm.avail_in > 0)
1501 {
1502 if (rc != Z_OK)
1503 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1504 bfd_get_filename (abfd), rc);
1505 strm.next_out = ((Bytef*) uncompressed_buffer
1506 + (uncompressed_size - strm.avail_out));
1507 rc = inflate (&strm, Z_FINISH);
1508 if (rc != Z_STREAM_END)
1509 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1510 bfd_get_filename (abfd), rc);
1511 rc = inflateReset (&strm);
1512 }
1513 rc = inflateEnd (&strm);
1514 if (rc != Z_OK
1515 || strm.avail_out != 0)
1516 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1517 bfd_get_filename (abfd), rc);
1518
1519 do_cleanups (cleanup);
1520 *outbuf = uncompressed_buffer;
1521 *outsize = uncompressed_size;
1522 #endif
1523 }
1524
1525 /* Read the contents of the section SECTP from object file specified by
1526 OBJFILE, store info about the section into INFO.
1527 If the section is compressed, uncompress it before returning. */
1528
1529 static void
1530 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1531 {
1532 bfd *abfd = objfile->obfd;
1533 asection *sectp = info->asection;
1534 gdb_byte *buf, *retbuf;
1535 unsigned char header[4];
1536
1537 if (info->readin)
1538 return;
1539 info->buffer = NULL;
1540 info->was_mmapped = 0;
1541 info->readin = 1;
1542
1543 if (info->asection == NULL || info->size == 0)
1544 return;
1545
1546 /* Check if the file has a 4-byte header indicating compression. */
1547 if (info->size > sizeof (header)
1548 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1549 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1550 {
1551 /* Upon decompression, update the buffer and its size. */
1552 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1553 {
1554 zlib_decompress_section (objfile, sectp, &info->buffer,
1555 &info->size);
1556 return;
1557 }
1558 }
1559
1560 #ifdef HAVE_MMAP
1561 if (pagesize == 0)
1562 pagesize = getpagesize ();
1563
1564 /* Only try to mmap sections which are large enough: we don't want to
1565 waste space due to fragmentation. Also, only try mmap for sections
1566 without relocations. */
1567
1568 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1569 {
1570 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1571 size_t map_length = info->size + sectp->filepos - pg_offset;
1572 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1573 MAP_PRIVATE, pg_offset);
1574
1575 if (retbuf != MAP_FAILED)
1576 {
1577 info->was_mmapped = 1;
1578 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1579 #if HAVE_POSIX_MADVISE
1580 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1581 #endif
1582 return;
1583 }
1584 }
1585 #endif
1586
1587 /* If we get here, we are a normal, not-compressed section. */
1588 info->buffer = buf
1589 = obstack_alloc (&objfile->objfile_obstack, info->size);
1590
1591 /* When debugging .o files, we may need to apply relocations; see
1592 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1593 We never compress sections in .o files, so we only need to
1594 try this when the section is not compressed. */
1595 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1596 if (retbuf != NULL)
1597 {
1598 info->buffer = retbuf;
1599 return;
1600 }
1601
1602 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1603 || bfd_bread (buf, info->size, abfd) != info->size)
1604 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1605 bfd_get_filename (abfd));
1606 }
1607
1608 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1609 SECTION_NAME. */
1610
1611 void
1612 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1613 asection **sectp, gdb_byte **bufp,
1614 bfd_size_type *sizep)
1615 {
1616 struct dwarf2_per_objfile *data
1617 = objfile_data (objfile, dwarf2_objfile_data_key);
1618 struct dwarf2_section_info *info;
1619
1620 /* We may see an objfile without any DWARF, in which case we just
1621 return nothing. */
1622 if (data == NULL)
1623 {
1624 *sectp = NULL;
1625 *bufp = NULL;
1626 *sizep = 0;
1627 return;
1628 }
1629 if (section_is_p (section_name, EH_FRAME_SECTION))
1630 info = &data->eh_frame;
1631 else if (section_is_p (section_name, FRAME_SECTION))
1632 info = &data->frame;
1633 else
1634 gdb_assert_not_reached ("unexpected section");
1635
1636 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1637 /* We haven't read this section in yet. Do it now. */
1638 dwarf2_read_section (objfile, info);
1639
1640 *sectp = info->asection;
1641 *bufp = info->buffer;
1642 *sizep = info->size;
1643 }
1644
1645 \f
1646
1647 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1648 this CU came. */
1649
1650 static void
1651 dw2_do_instantiate_symtab (struct objfile *objfile,
1652 struct dwarf2_per_cu_data *per_cu)
1653 {
1654 struct cleanup *back_to;
1655
1656 back_to = make_cleanup (dwarf2_release_queue, NULL);
1657
1658 queue_comp_unit (per_cu, objfile);
1659
1660 if (per_cu->from_debug_types)
1661 read_signatured_type_at_offset (objfile, per_cu->offset);
1662 else
1663 load_full_comp_unit (per_cu, objfile);
1664
1665 process_queue (objfile);
1666
1667 /* Age the cache, releasing compilation units that have not
1668 been used recently. */
1669 age_cached_comp_units ();
1670
1671 do_cleanups (back_to);
1672 }
1673
1674 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1675 the objfile from which this CU came. Returns the resulting symbol
1676 table. */
1677
1678 static struct symtab *
1679 dw2_instantiate_symtab (struct objfile *objfile,
1680 struct dwarf2_per_cu_data *per_cu)
1681 {
1682 if (!per_cu->v.quick->symtab)
1683 {
1684 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1685 increment_reading_symtab ();
1686 dw2_do_instantiate_symtab (objfile, per_cu);
1687 do_cleanups (back_to);
1688 }
1689 return per_cu->v.quick->symtab;
1690 }
1691
1692 /* Return the CU given its index. */
1693
1694 static struct dwarf2_per_cu_data *
1695 dw2_get_cu (int index)
1696 {
1697 if (index >= dwarf2_per_objfile->n_comp_units)
1698 {
1699 index -= dwarf2_per_objfile->n_comp_units;
1700 return dwarf2_per_objfile->type_comp_units[index];
1701 }
1702 return dwarf2_per_objfile->all_comp_units[index];
1703 }
1704
1705 /* A helper function that knows how to read a 64-bit value in a way
1706 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1707 otherwise. */
1708
1709 static int
1710 extract_cu_value (const char *bytes, ULONGEST *result)
1711 {
1712 if (sizeof (ULONGEST) < 8)
1713 {
1714 int i;
1715
1716 /* Ignore the upper 4 bytes if they are all zero. */
1717 for (i = 0; i < 4; ++i)
1718 if (bytes[i + 4] != 0)
1719 return 0;
1720
1721 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1722 }
1723 else
1724 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1725 return 1;
1726 }
1727
1728 /* Read the CU list from the mapped index, and use it to create all
1729 the CU objects for this objfile. Return 0 if something went wrong,
1730 1 if everything went ok. */
1731
1732 static int
1733 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1734 offset_type cu_list_elements)
1735 {
1736 offset_type i;
1737
1738 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1739 dwarf2_per_objfile->all_comp_units
1740 = obstack_alloc (&objfile->objfile_obstack,
1741 dwarf2_per_objfile->n_comp_units
1742 * sizeof (struct dwarf2_per_cu_data *));
1743
1744 for (i = 0; i < cu_list_elements; i += 2)
1745 {
1746 struct dwarf2_per_cu_data *the_cu;
1747 ULONGEST offset, length;
1748
1749 if (!extract_cu_value (cu_list, &offset)
1750 || !extract_cu_value (cu_list + 8, &length))
1751 return 0;
1752 cu_list += 2 * 8;
1753
1754 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1755 struct dwarf2_per_cu_data);
1756 the_cu->offset = offset;
1757 the_cu->length = length;
1758 the_cu->objfile = objfile;
1759 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1760 struct dwarf2_per_cu_quick_data);
1761 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1762 }
1763
1764 return 1;
1765 }
1766
1767 /* Create the signatured type hash table from the index. */
1768
1769 static int
1770 create_signatured_type_table_from_index (struct objfile *objfile,
1771 const gdb_byte *bytes,
1772 offset_type elements)
1773 {
1774 offset_type i;
1775 htab_t sig_types_hash;
1776
1777 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1778 dwarf2_per_objfile->type_comp_units
1779 = obstack_alloc (&objfile->objfile_obstack,
1780 dwarf2_per_objfile->n_type_comp_units
1781 * sizeof (struct dwarf2_per_cu_data *));
1782
1783 sig_types_hash = allocate_signatured_type_table (objfile);
1784
1785 for (i = 0; i < elements; i += 3)
1786 {
1787 struct signatured_type *type_sig;
1788 ULONGEST offset, type_offset, signature;
1789 void **slot;
1790
1791 if (!extract_cu_value (bytes, &offset)
1792 || !extract_cu_value (bytes + 8, &type_offset))
1793 return 0;
1794 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1795 bytes += 3 * 8;
1796
1797 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1798 struct signatured_type);
1799 type_sig->signature = signature;
1800 type_sig->offset = offset;
1801 type_sig->type_offset = type_offset;
1802 type_sig->per_cu.from_debug_types = 1;
1803 type_sig->per_cu.offset = offset;
1804 type_sig->per_cu.objfile = objfile;
1805 type_sig->per_cu.v.quick
1806 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1807 struct dwarf2_per_cu_quick_data);
1808
1809 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1810 *slot = type_sig;
1811
1812 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1813 }
1814
1815 dwarf2_per_objfile->signatured_types = sig_types_hash;
1816
1817 return 1;
1818 }
1819
1820 /* Read the address map data from the mapped index, and use it to
1821 populate the objfile's psymtabs_addrmap. */
1822
1823 static void
1824 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1825 {
1826 const gdb_byte *iter, *end;
1827 struct obstack temp_obstack;
1828 struct addrmap *mutable_map;
1829 struct cleanup *cleanup;
1830 CORE_ADDR baseaddr;
1831
1832 obstack_init (&temp_obstack);
1833 cleanup = make_cleanup_obstack_free (&temp_obstack);
1834 mutable_map = addrmap_create_mutable (&temp_obstack);
1835
1836 iter = index->address_table;
1837 end = iter + index->address_table_size;
1838
1839 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1840
1841 while (iter < end)
1842 {
1843 ULONGEST hi, lo, cu_index;
1844 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1845 iter += 8;
1846 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1847 iter += 8;
1848 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1849 iter += 4;
1850
1851 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1852 dw2_get_cu (cu_index));
1853 }
1854
1855 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1856 &objfile->objfile_obstack);
1857 do_cleanups (cleanup);
1858 }
1859
1860 /* The hash function for strings in the mapped index. This is the
1861 same as the hashtab.c hash function, but we keep a separate copy to
1862 maintain control over the implementation. This is necessary
1863 because the hash function is tied to the format of the mapped index
1864 file. */
1865
1866 static hashval_t
1867 mapped_index_string_hash (const void *p)
1868 {
1869 const unsigned char *str = (const unsigned char *) p;
1870 hashval_t r = 0;
1871 unsigned char c;
1872
1873 while ((c = *str++) != 0)
1874 r = r * 67 + c - 113;
1875
1876 return r;
1877 }
1878
1879 /* Find a slot in the mapped index INDEX for the object named NAME.
1880 If NAME is found, set *VEC_OUT to point to the CU vector in the
1881 constant pool and return 1. If NAME cannot be found, return 0. */
1882
1883 static int
1884 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1885 offset_type **vec_out)
1886 {
1887 offset_type hash = mapped_index_string_hash (name);
1888 offset_type slot, step;
1889
1890 slot = hash & (index->symbol_table_slots - 1);
1891 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
1892
1893 for (;;)
1894 {
1895 /* Convert a slot number to an offset into the table. */
1896 offset_type i = 2 * slot;
1897 const char *str;
1898 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
1899 return 0;
1900
1901 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
1902 if (!strcmp (name, str))
1903 {
1904 *vec_out = (offset_type *) (index->constant_pool
1905 + MAYBE_SWAP (index->symbol_table[i + 1]));
1906 return 1;
1907 }
1908
1909 slot = (slot + step) & (index->symbol_table_slots - 1);
1910 }
1911 }
1912
1913 /* Read the index file. If everything went ok, initialize the "quick"
1914 elements of all the CUs and return 1. Otherwise, return 0. */
1915
1916 static int
1917 dwarf2_read_index (struct objfile *objfile)
1918 {
1919 char *addr;
1920 struct mapped_index *map;
1921 offset_type *metadata;
1922 const gdb_byte *cu_list;
1923 const gdb_byte *types_list = NULL;
1924 offset_type version, cu_list_elements;
1925 offset_type types_list_elements = 0;
1926 int i;
1927
1928 if (dwarf2_per_objfile->gdb_index.asection == NULL
1929 || dwarf2_per_objfile->gdb_index.size == 0)
1930 return 0;
1931
1932 /* Older elfutils strip versions could keep the section in the main
1933 executable while splitting it for the separate debug info file. */
1934 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1935 & SEC_HAS_CONTENTS) == 0)
1936 return 0;
1937
1938 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1939
1940 addr = dwarf2_per_objfile->gdb_index.buffer;
1941 /* Version check. */
1942 version = MAYBE_SWAP (*(offset_type *) addr);
1943 /* Versions earlier than 3 emitted every copy of a psymbol. This
1944 causes the index to behave very poorly for certain requests. So,
1945 it seems better to just ignore such indices. */
1946 if (version < 3)
1947 return 0;
1948 /* Indexes with higher version than the one supported by GDB may be no
1949 longer backward compatible. */
1950 if (version > 3)
1951 return 0;
1952
1953 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1954 map->total_size = dwarf2_per_objfile->gdb_index.size;
1955
1956 metadata = (offset_type *) (addr + sizeof (offset_type));
1957
1958 i = 0;
1959 cu_list = addr + MAYBE_SWAP (metadata[i]);
1960 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1961 / 8);
1962 ++i;
1963
1964 types_list = addr + MAYBE_SWAP (metadata[i]);
1965 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1966 - MAYBE_SWAP (metadata[i]))
1967 / 8);
1968 ++i;
1969
1970 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1971 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1972 - MAYBE_SWAP (metadata[i]));
1973 ++i;
1974
1975 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1976 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1977 - MAYBE_SWAP (metadata[i]))
1978 / (2 * sizeof (offset_type)));
1979 ++i;
1980
1981 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1982
1983 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1984 return 0;
1985
1986 if (types_list_elements
1987 && !create_signatured_type_table_from_index (objfile, types_list,
1988 types_list_elements))
1989 return 0;
1990
1991 create_addrmap_from_index (objfile, map);
1992
1993 dwarf2_per_objfile->index_table = map;
1994 dwarf2_per_objfile->using_index = 1;
1995
1996 return 1;
1997 }
1998
1999 /* A helper for the "quick" functions which sets the global
2000 dwarf2_per_objfile according to OBJFILE. */
2001
2002 static void
2003 dw2_setup (struct objfile *objfile)
2004 {
2005 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2006 gdb_assert (dwarf2_per_objfile);
2007 }
2008
2009 /* A helper for the "quick" functions which attempts to read the line
2010 table for THIS_CU. */
2011
2012 static void
2013 dw2_require_line_header (struct objfile *objfile,
2014 struct dwarf2_per_cu_data *this_cu)
2015 {
2016 bfd *abfd = objfile->obfd;
2017 struct line_header *lh = NULL;
2018 struct attribute *attr;
2019 struct cleanup *cleanups;
2020 struct die_info *comp_unit_die;
2021 struct dwarf2_section_info* sec;
2022 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2023 int has_children, i;
2024 struct dwarf2_cu cu;
2025 unsigned int bytes_read, buffer_size;
2026 struct die_reader_specs reader_specs;
2027 char *name, *comp_dir;
2028
2029 if (this_cu->v.quick->read_lines)
2030 return;
2031 this_cu->v.quick->read_lines = 1;
2032
2033 init_one_comp_unit (&cu, objfile);
2034 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2035
2036 if (this_cu->from_debug_types)
2037 sec = &dwarf2_per_objfile->types;
2038 else
2039 sec = &dwarf2_per_objfile->info;
2040 dwarf2_read_section (objfile, sec);
2041 buffer_size = sec->size;
2042 buffer = sec->buffer;
2043 info_ptr = buffer + this_cu->offset;
2044 beg_of_comp_unit = info_ptr;
2045
2046 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2047 buffer, buffer_size,
2048 abfd);
2049
2050 /* Complete the cu_header. */
2051 cu.header.offset = beg_of_comp_unit - buffer;
2052 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2053
2054 this_cu->cu = &cu;
2055 cu.per_cu = this_cu;
2056
2057 dwarf2_read_abbrevs (abfd, &cu);
2058 make_cleanup (dwarf2_free_abbrev_table, &cu);
2059
2060 if (this_cu->from_debug_types)
2061 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2062 init_cu_die_reader (&reader_specs, &cu);
2063 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2064 &has_children);
2065
2066 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2067 if (attr)
2068 {
2069 unsigned int line_offset = DW_UNSND (attr);
2070 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2071 }
2072 if (lh == NULL)
2073 {
2074 do_cleanups (cleanups);
2075 return;
2076 }
2077
2078 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2079
2080 this_cu->v.quick->lines = lh;
2081
2082 this_cu->v.quick->file_names
2083 = obstack_alloc (&objfile->objfile_obstack,
2084 lh->num_file_names * sizeof (char *));
2085 for (i = 0; i < lh->num_file_names; ++i)
2086 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2087
2088 do_cleanups (cleanups);
2089 }
2090
2091 /* A helper for the "quick" functions which computes and caches the
2092 real path for a given file name from the line table.
2093 dw2_require_line_header must have been called before this is
2094 invoked. */
2095
2096 static const char *
2097 dw2_require_full_path (struct objfile *objfile,
2098 struct dwarf2_per_cu_data *per_cu,
2099 int index)
2100 {
2101 if (!per_cu->v.quick->full_names)
2102 per_cu->v.quick->full_names
2103 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2104 per_cu->v.quick->lines->num_file_names,
2105 sizeof (char *));
2106
2107 if (!per_cu->v.quick->full_names[index])
2108 per_cu->v.quick->full_names[index]
2109 = gdb_realpath (per_cu->v.quick->file_names[index]);
2110
2111 return per_cu->v.quick->full_names[index];
2112 }
2113
2114 static struct symtab *
2115 dw2_find_last_source_symtab (struct objfile *objfile)
2116 {
2117 int index;
2118 dw2_setup (objfile);
2119 index = dwarf2_per_objfile->n_comp_units - 1;
2120 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2121 }
2122
2123 static void
2124 dw2_forget_cached_source_info (struct objfile *objfile)
2125 {
2126 int i;
2127
2128 dw2_setup (objfile);
2129 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2130 + dwarf2_per_objfile->n_type_comp_units); ++i)
2131 {
2132 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2133
2134 if (per_cu->v.quick->full_names)
2135 {
2136 int j;
2137
2138 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2139 {
2140 xfree ((void *) per_cu->v.quick->full_names[j]);
2141 per_cu->v.quick->full_names[j] = NULL;
2142 }
2143 }
2144 }
2145 }
2146
2147 static int
2148 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2149 const char *full_path, const char *real_path,
2150 struct symtab **result)
2151 {
2152 int i;
2153 int check_basename = lbasename (name) == name;
2154 struct dwarf2_per_cu_data *base_cu = NULL;
2155
2156 dw2_setup (objfile);
2157 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2158 + dwarf2_per_objfile->n_type_comp_units); ++i)
2159 {
2160 int j;
2161 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2162
2163 if (per_cu->v.quick->symtab)
2164 continue;
2165
2166 dw2_require_line_header (objfile, per_cu);
2167 if (!per_cu->v.quick->lines)
2168 continue;
2169
2170 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2171 {
2172 const char *this_name = per_cu->v.quick->file_names[j];
2173
2174 if (FILENAME_CMP (name, this_name) == 0)
2175 {
2176 *result = dw2_instantiate_symtab (objfile, per_cu);
2177 return 1;
2178 }
2179
2180 if (check_basename && ! base_cu
2181 && FILENAME_CMP (lbasename (this_name), name) == 0)
2182 base_cu = per_cu;
2183
2184 if (full_path != NULL)
2185 {
2186 const char *this_full_name = dw2_require_full_path (objfile,
2187 per_cu, j);
2188
2189 if (this_full_name
2190 && FILENAME_CMP (full_path, this_full_name) == 0)
2191 {
2192 *result = dw2_instantiate_symtab (objfile, per_cu);
2193 return 1;
2194 }
2195 }
2196
2197 if (real_path != NULL)
2198 {
2199 const char *this_full_name = dw2_require_full_path (objfile,
2200 per_cu, j);
2201
2202 if (this_full_name != NULL
2203 && FILENAME_CMP (real_path, this_full_name) == 0)
2204 {
2205 *result = dw2_instantiate_symtab (objfile, per_cu);
2206 return 1;
2207 }
2208 }
2209 }
2210 }
2211
2212 if (base_cu)
2213 {
2214 *result = dw2_instantiate_symtab (objfile, base_cu);
2215 return 1;
2216 }
2217
2218 return 0;
2219 }
2220
2221 static struct symtab *
2222 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2223 const char *name, domain_enum domain)
2224 {
2225 /* We do all the work in the pre_expand_symtabs_matching hook
2226 instead. */
2227 return NULL;
2228 }
2229
2230 /* A helper function that expands all symtabs that hold an object
2231 named NAME. */
2232
2233 static void
2234 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2235 {
2236 dw2_setup (objfile);
2237
2238 if (dwarf2_per_objfile->index_table)
2239 {
2240 offset_type *vec;
2241
2242 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2243 name, &vec))
2244 {
2245 offset_type i, len = MAYBE_SWAP (*vec);
2246 for (i = 0; i < len; ++i)
2247 {
2248 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2249 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2250
2251 dw2_instantiate_symtab (objfile, per_cu);
2252 }
2253 }
2254 }
2255 }
2256
2257 static void
2258 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2259 int kind, const char *name,
2260 domain_enum domain)
2261 {
2262 dw2_do_expand_symtabs_matching (objfile, name);
2263 }
2264
2265 static void
2266 dw2_print_stats (struct objfile *objfile)
2267 {
2268 int i, count;
2269
2270 dw2_setup (objfile);
2271 count = 0;
2272 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2273 + dwarf2_per_objfile->n_type_comp_units); ++i)
2274 {
2275 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2276
2277 if (!per_cu->v.quick->symtab)
2278 ++count;
2279 }
2280 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2281 }
2282
2283 static void
2284 dw2_dump (struct objfile *objfile)
2285 {
2286 /* Nothing worth printing. */
2287 }
2288
2289 static void
2290 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2291 struct section_offsets *delta)
2292 {
2293 /* There's nothing to relocate here. */
2294 }
2295
2296 static void
2297 dw2_expand_symtabs_for_function (struct objfile *objfile,
2298 const char *func_name)
2299 {
2300 dw2_do_expand_symtabs_matching (objfile, func_name);
2301 }
2302
2303 static void
2304 dw2_expand_all_symtabs (struct objfile *objfile)
2305 {
2306 int i;
2307
2308 dw2_setup (objfile);
2309
2310 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2311 + dwarf2_per_objfile->n_type_comp_units); ++i)
2312 {
2313 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2314
2315 dw2_instantiate_symtab (objfile, per_cu);
2316 }
2317 }
2318
2319 static void
2320 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2321 const char *filename)
2322 {
2323 int i;
2324
2325 dw2_setup (objfile);
2326 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2327 + dwarf2_per_objfile->n_type_comp_units); ++i)
2328 {
2329 int j;
2330 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2331
2332 if (per_cu->v.quick->symtab)
2333 continue;
2334
2335 dw2_require_line_header (objfile, per_cu);
2336 if (!per_cu->v.quick->lines)
2337 continue;
2338
2339 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2340 {
2341 const char *this_name = per_cu->v.quick->file_names[j];
2342 if (strcmp (this_name, filename) == 0)
2343 {
2344 dw2_instantiate_symtab (objfile, per_cu);
2345 break;
2346 }
2347 }
2348 }
2349 }
2350
2351 static const char *
2352 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2353 {
2354 struct dwarf2_per_cu_data *per_cu;
2355 offset_type *vec;
2356
2357 dw2_setup (objfile);
2358
2359 if (!dwarf2_per_objfile->index_table)
2360 return NULL;
2361
2362 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2363 name, &vec))
2364 return NULL;
2365
2366 /* Note that this just looks at the very first one named NAME -- but
2367 actually we are looking for a function. find_main_filename
2368 should be rewritten so that it doesn't require a custom hook. It
2369 could just use the ordinary symbol tables. */
2370 /* vec[0] is the length, which must always be >0. */
2371 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2372
2373 dw2_require_line_header (objfile, per_cu);
2374 if (!per_cu->v.quick->lines)
2375 return NULL;
2376
2377 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
2378 }
2379
2380 static void
2381 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2382 struct objfile *objfile, int global,
2383 int (*callback) (struct block *,
2384 struct symbol *, void *),
2385 void *data, symbol_compare_ftype *match,
2386 symbol_compare_ftype *ordered_compare)
2387 {
2388 /* Currently unimplemented; used for Ada. The function can be called if the
2389 current language is Ada for a non-Ada objfile using GNU index. As Ada
2390 does not look for non-Ada symbols this function should just return. */
2391 }
2392
2393 static void
2394 dw2_expand_symtabs_matching (struct objfile *objfile,
2395 int (*file_matcher) (const char *, void *),
2396 int (*name_matcher) (const char *, void *),
2397 domain_enum kind,
2398 void *data)
2399 {
2400 int i;
2401 offset_type iter;
2402 struct mapped_index *index;
2403
2404 dw2_setup (objfile);
2405 if (!dwarf2_per_objfile->index_table)
2406 return;
2407 index = dwarf2_per_objfile->index_table;
2408
2409 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2410 + dwarf2_per_objfile->n_type_comp_units); ++i)
2411 {
2412 int j;
2413 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2414
2415 per_cu->v.quick->mark = 0;
2416 if (per_cu->v.quick->symtab)
2417 continue;
2418
2419 dw2_require_line_header (objfile, per_cu);
2420 if (!per_cu->v.quick->lines)
2421 continue;
2422
2423 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2424 {
2425 if (file_matcher (per_cu->v.quick->file_names[j], data))
2426 {
2427 per_cu->v.quick->mark = 1;
2428 break;
2429 }
2430 }
2431 }
2432
2433 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2434 {
2435 offset_type idx = 2 * iter;
2436 const char *name;
2437 offset_type *vec, vec_len, vec_idx;
2438
2439 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2440 continue;
2441
2442 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2443
2444 if (! (*name_matcher) (name, data))
2445 continue;
2446
2447 /* The name was matched, now expand corresponding CUs that were
2448 marked. */
2449 vec = (offset_type *) (index->constant_pool
2450 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2451 vec_len = MAYBE_SWAP (vec[0]);
2452 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2453 {
2454 struct dwarf2_per_cu_data *per_cu;
2455
2456 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2457 if (per_cu->v.quick->mark)
2458 dw2_instantiate_symtab (objfile, per_cu);
2459 }
2460 }
2461 }
2462
2463 static struct symtab *
2464 dw2_find_pc_sect_symtab (struct objfile *objfile,
2465 struct minimal_symbol *msymbol,
2466 CORE_ADDR pc,
2467 struct obj_section *section,
2468 int warn_if_readin)
2469 {
2470 struct dwarf2_per_cu_data *data;
2471
2472 dw2_setup (objfile);
2473
2474 if (!objfile->psymtabs_addrmap)
2475 return NULL;
2476
2477 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2478 if (!data)
2479 return NULL;
2480
2481 if (warn_if_readin && data->v.quick->symtab)
2482 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2483 paddress (get_objfile_arch (objfile), pc));
2484
2485 return dw2_instantiate_symtab (objfile, data);
2486 }
2487
2488 static void
2489 dw2_map_symbol_names (struct objfile *objfile,
2490 void (*fun) (const char *, void *),
2491 void *data)
2492 {
2493 offset_type iter;
2494 struct mapped_index *index;
2495
2496 dw2_setup (objfile);
2497
2498 if (!dwarf2_per_objfile->index_table)
2499 return;
2500 index = dwarf2_per_objfile->index_table;
2501
2502 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2503 {
2504 offset_type idx = 2 * iter;
2505 const char *name;
2506 offset_type *vec, vec_len, vec_idx;
2507
2508 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2509 continue;
2510
2511 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
2512
2513 (*fun) (name, data);
2514 }
2515 }
2516
2517 static void
2518 dw2_map_symbol_filenames (struct objfile *objfile,
2519 void (*fun) (const char *, const char *, void *),
2520 void *data)
2521 {
2522 int i;
2523
2524 dw2_setup (objfile);
2525 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2526 + dwarf2_per_objfile->n_type_comp_units); ++i)
2527 {
2528 int j;
2529 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2530
2531 if (per_cu->v.quick->symtab)
2532 continue;
2533
2534 dw2_require_line_header (objfile, per_cu);
2535 if (!per_cu->v.quick->lines)
2536 continue;
2537
2538 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2539 {
2540 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2541 j);
2542 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
2543 }
2544 }
2545 }
2546
2547 static int
2548 dw2_has_symbols (struct objfile *objfile)
2549 {
2550 return 1;
2551 }
2552
2553 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2554 {
2555 dw2_has_symbols,
2556 dw2_find_last_source_symtab,
2557 dw2_forget_cached_source_info,
2558 dw2_lookup_symtab,
2559 dw2_lookup_symbol,
2560 dw2_pre_expand_symtabs_matching,
2561 dw2_print_stats,
2562 dw2_dump,
2563 dw2_relocate,
2564 dw2_expand_symtabs_for_function,
2565 dw2_expand_all_symtabs,
2566 dw2_expand_symtabs_with_filename,
2567 dw2_find_symbol_file,
2568 dw2_map_matching_symbols,
2569 dw2_expand_symtabs_matching,
2570 dw2_find_pc_sect_symtab,
2571 dw2_map_symbol_names,
2572 dw2_map_symbol_filenames
2573 };
2574
2575 /* Initialize for reading DWARF for this objfile. Return 0 if this
2576 file will use psymtabs, or 1 if using the GNU index. */
2577
2578 int
2579 dwarf2_initialize_objfile (struct objfile *objfile)
2580 {
2581 /* If we're about to read full symbols, don't bother with the
2582 indices. In this case we also don't care if some other debug
2583 format is making psymtabs, because they are all about to be
2584 expanded anyway. */
2585 if ((objfile->flags & OBJF_READNOW))
2586 {
2587 int i;
2588
2589 dwarf2_per_objfile->using_index = 1;
2590 create_all_comp_units (objfile);
2591 create_debug_types_hash_table (objfile);
2592
2593 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2594 + dwarf2_per_objfile->n_type_comp_units); ++i)
2595 {
2596 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2597
2598 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2599 struct dwarf2_per_cu_quick_data);
2600 }
2601
2602 /* Return 1 so that gdb sees the "quick" functions. However,
2603 these functions will be no-ops because we will have expanded
2604 all symtabs. */
2605 return 1;
2606 }
2607
2608 if (dwarf2_read_index (objfile))
2609 return 1;
2610
2611 dwarf2_build_psymtabs (objfile);
2612 return 0;
2613 }
2614
2615 \f
2616
2617 /* Build a partial symbol table. */
2618
2619 void
2620 dwarf2_build_psymtabs (struct objfile *objfile)
2621 {
2622 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2623 {
2624 init_psymbol_list (objfile, 1024);
2625 }
2626
2627 dwarf2_build_psymtabs_hard (objfile);
2628 }
2629
2630 /* Return TRUE if OFFSET is within CU_HEADER. */
2631
2632 static inline int
2633 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2634 {
2635 unsigned int bottom = cu_header->offset;
2636 unsigned int top = (cu_header->offset
2637 + cu_header->length
2638 + cu_header->initial_length_size);
2639
2640 return (offset >= bottom && offset < top);
2641 }
2642
2643 /* Read in the comp unit header information from the debug_info at info_ptr.
2644 NOTE: This leaves members offset, first_die_offset to be filled in
2645 by the caller. */
2646
2647 static gdb_byte *
2648 read_comp_unit_head (struct comp_unit_head *cu_header,
2649 gdb_byte *info_ptr, bfd *abfd)
2650 {
2651 int signed_addr;
2652 unsigned int bytes_read;
2653
2654 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2655 cu_header->initial_length_size = bytes_read;
2656 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2657 info_ptr += bytes_read;
2658 cu_header->version = read_2_bytes (abfd, info_ptr);
2659 info_ptr += 2;
2660 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2661 &bytes_read);
2662 info_ptr += bytes_read;
2663 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2664 info_ptr += 1;
2665 signed_addr = bfd_get_sign_extend_vma (abfd);
2666 if (signed_addr < 0)
2667 internal_error (__FILE__, __LINE__,
2668 _("read_comp_unit_head: dwarf from non elf file"));
2669 cu_header->signed_addr_p = signed_addr;
2670
2671 return info_ptr;
2672 }
2673
2674 static gdb_byte *
2675 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2676 gdb_byte *buffer, unsigned int buffer_size,
2677 bfd *abfd)
2678 {
2679 gdb_byte *beg_of_comp_unit = info_ptr;
2680
2681 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2682
2683 if (header->version != 2 && header->version != 3 && header->version != 4)
2684 error (_("Dwarf Error: wrong version in compilation unit header "
2685 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2686 bfd_get_filename (abfd));
2687
2688 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2689 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2690 "(offset 0x%lx + 6) [in module %s]"),
2691 (long) header->abbrev_offset,
2692 (long) (beg_of_comp_unit - buffer),
2693 bfd_get_filename (abfd));
2694
2695 if (beg_of_comp_unit + header->length + header->initial_length_size
2696 > buffer + buffer_size)
2697 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2698 "(offset 0x%lx + 0) [in module %s]"),
2699 (long) header->length,
2700 (long) (beg_of_comp_unit - buffer),
2701 bfd_get_filename (abfd));
2702
2703 return info_ptr;
2704 }
2705
2706 /* Read in the types comp unit header information from .debug_types entry at
2707 types_ptr. The result is a pointer to one past the end of the header. */
2708
2709 static gdb_byte *
2710 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2711 ULONGEST *signature,
2712 gdb_byte *types_ptr, bfd *abfd)
2713 {
2714 gdb_byte *initial_types_ptr = types_ptr;
2715
2716 dwarf2_read_section (dwarf2_per_objfile->objfile,
2717 &dwarf2_per_objfile->types);
2718 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2719
2720 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2721
2722 *signature = read_8_bytes (abfd, types_ptr);
2723 types_ptr += 8;
2724 types_ptr += cu_header->offset_size;
2725 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2726
2727 return types_ptr;
2728 }
2729
2730 /* Allocate a new partial symtab for file named NAME and mark this new
2731 partial symtab as being an include of PST. */
2732
2733 static void
2734 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2735 struct objfile *objfile)
2736 {
2737 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2738
2739 subpst->section_offsets = pst->section_offsets;
2740 subpst->textlow = 0;
2741 subpst->texthigh = 0;
2742
2743 subpst->dependencies = (struct partial_symtab **)
2744 obstack_alloc (&objfile->objfile_obstack,
2745 sizeof (struct partial_symtab *));
2746 subpst->dependencies[0] = pst;
2747 subpst->number_of_dependencies = 1;
2748
2749 subpst->globals_offset = 0;
2750 subpst->n_global_syms = 0;
2751 subpst->statics_offset = 0;
2752 subpst->n_static_syms = 0;
2753 subpst->symtab = NULL;
2754 subpst->read_symtab = pst->read_symtab;
2755 subpst->readin = 0;
2756
2757 /* No private part is necessary for include psymtabs. This property
2758 can be used to differentiate between such include psymtabs and
2759 the regular ones. */
2760 subpst->read_symtab_private = NULL;
2761 }
2762
2763 /* Read the Line Number Program data and extract the list of files
2764 included by the source file represented by PST. Build an include
2765 partial symtab for each of these included files. */
2766
2767 static void
2768 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2769 struct die_info *die,
2770 struct partial_symtab *pst)
2771 {
2772 struct objfile *objfile = cu->objfile;
2773 bfd *abfd = objfile->obfd;
2774 struct line_header *lh = NULL;
2775 struct attribute *attr;
2776
2777 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2778 if (attr)
2779 {
2780 unsigned int line_offset = DW_UNSND (attr);
2781
2782 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2783 }
2784 if (lh == NULL)
2785 return; /* No linetable, so no includes. */
2786
2787 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2788 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2789
2790 free_line_header (lh);
2791 }
2792
2793 static hashval_t
2794 hash_type_signature (const void *item)
2795 {
2796 const struct signatured_type *type_sig = item;
2797
2798 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2799 return type_sig->signature;
2800 }
2801
2802 static int
2803 eq_type_signature (const void *item_lhs, const void *item_rhs)
2804 {
2805 const struct signatured_type *lhs = item_lhs;
2806 const struct signatured_type *rhs = item_rhs;
2807
2808 return lhs->signature == rhs->signature;
2809 }
2810
2811 /* Allocate a hash table for signatured types. */
2812
2813 static htab_t
2814 allocate_signatured_type_table (struct objfile *objfile)
2815 {
2816 return htab_create_alloc_ex (41,
2817 hash_type_signature,
2818 eq_type_signature,
2819 NULL,
2820 &objfile->objfile_obstack,
2821 hashtab_obstack_allocate,
2822 dummy_obstack_deallocate);
2823 }
2824
2825 /* A helper function to add a signatured type CU to a list. */
2826
2827 static int
2828 add_signatured_type_cu_to_list (void **slot, void *datum)
2829 {
2830 struct signatured_type *sigt = *slot;
2831 struct dwarf2_per_cu_data ***datap = datum;
2832
2833 **datap = &sigt->per_cu;
2834 ++*datap;
2835
2836 return 1;
2837 }
2838
2839 /* Create the hash table of all entries in the .debug_types section.
2840 The result is zero if there is an error (e.g. missing .debug_types section),
2841 otherwise non-zero. */
2842
2843 static int
2844 create_debug_types_hash_table (struct objfile *objfile)
2845 {
2846 gdb_byte *info_ptr;
2847 htab_t types_htab;
2848 struct dwarf2_per_cu_data **iter;
2849
2850 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2851 info_ptr = dwarf2_per_objfile->types.buffer;
2852
2853 if (info_ptr == NULL)
2854 {
2855 dwarf2_per_objfile->signatured_types = NULL;
2856 return 0;
2857 }
2858
2859 types_htab = allocate_signatured_type_table (objfile);
2860
2861 if (dwarf2_die_debug)
2862 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2863
2864 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2865 {
2866 unsigned int offset;
2867 unsigned int offset_size;
2868 unsigned int type_offset;
2869 unsigned int length, initial_length_size;
2870 unsigned short version;
2871 ULONGEST signature;
2872 struct signatured_type *type_sig;
2873 void **slot;
2874 gdb_byte *ptr = info_ptr;
2875
2876 offset = ptr - dwarf2_per_objfile->types.buffer;
2877
2878 /* We need to read the type's signature in order to build the hash
2879 table, but we don't need to read anything else just yet. */
2880
2881 /* Sanity check to ensure entire cu is present. */
2882 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2883 if (ptr + length + initial_length_size
2884 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2885 {
2886 complaint (&symfile_complaints,
2887 _("debug type entry runs off end of `.debug_types' section, ignored"));
2888 break;
2889 }
2890
2891 offset_size = initial_length_size == 4 ? 4 : 8;
2892 ptr += initial_length_size;
2893 version = bfd_get_16 (objfile->obfd, ptr);
2894 ptr += 2;
2895 ptr += offset_size; /* abbrev offset */
2896 ptr += 1; /* address size */
2897 signature = bfd_get_64 (objfile->obfd, ptr);
2898 ptr += 8;
2899 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2900
2901 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2902 memset (type_sig, 0, sizeof (*type_sig));
2903 type_sig->signature = signature;
2904 type_sig->offset = offset;
2905 type_sig->type_offset = type_offset;
2906 type_sig->per_cu.objfile = objfile;
2907 type_sig->per_cu.from_debug_types = 1;
2908
2909 slot = htab_find_slot (types_htab, type_sig, INSERT);
2910 gdb_assert (slot != NULL);
2911 *slot = type_sig;
2912
2913 if (dwarf2_die_debug)
2914 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2915 offset, phex (signature, sizeof (signature)));
2916
2917 info_ptr = info_ptr + initial_length_size + length;
2918 }
2919
2920 dwarf2_per_objfile->signatured_types = types_htab;
2921
2922 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2923 dwarf2_per_objfile->type_comp_units
2924 = obstack_alloc (&objfile->objfile_obstack,
2925 dwarf2_per_objfile->n_type_comp_units
2926 * sizeof (struct dwarf2_per_cu_data *));
2927 iter = &dwarf2_per_objfile->type_comp_units[0];
2928 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2929 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2930 == dwarf2_per_objfile->n_type_comp_units);
2931
2932 return 1;
2933 }
2934
2935 /* Lookup a signature based type.
2936 Returns NULL if SIG is not present in the table. */
2937
2938 static struct signatured_type *
2939 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2940 {
2941 struct signatured_type find_entry, *entry;
2942
2943 if (dwarf2_per_objfile->signatured_types == NULL)
2944 {
2945 complaint (&symfile_complaints,
2946 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2947 return 0;
2948 }
2949
2950 find_entry.signature = sig;
2951 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2952 return entry;
2953 }
2954
2955 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2956
2957 static void
2958 init_cu_die_reader (struct die_reader_specs *reader,
2959 struct dwarf2_cu *cu)
2960 {
2961 reader->abfd = cu->objfile->obfd;
2962 reader->cu = cu;
2963 if (cu->per_cu->from_debug_types)
2964 {
2965 gdb_assert (dwarf2_per_objfile->types.readin);
2966 reader->buffer = dwarf2_per_objfile->types.buffer;
2967 }
2968 else
2969 {
2970 gdb_assert (dwarf2_per_objfile->info.readin);
2971 reader->buffer = dwarf2_per_objfile->info.buffer;
2972 }
2973 }
2974
2975 /* Find the base address of the compilation unit for range lists and
2976 location lists. It will normally be specified by DW_AT_low_pc.
2977 In DWARF-3 draft 4, the base address could be overridden by
2978 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2979 compilation units with discontinuous ranges. */
2980
2981 static void
2982 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2983 {
2984 struct attribute *attr;
2985
2986 cu->base_known = 0;
2987 cu->base_address = 0;
2988
2989 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2990 if (attr)
2991 {
2992 cu->base_address = DW_ADDR (attr);
2993 cu->base_known = 1;
2994 }
2995 else
2996 {
2997 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2998 if (attr)
2999 {
3000 cu->base_address = DW_ADDR (attr);
3001 cu->base_known = 1;
3002 }
3003 }
3004 }
3005
3006 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3007 to combine the common parts.
3008 Process a compilation unit for a psymtab.
3009 BUFFER is a pointer to the beginning of the dwarf section buffer,
3010 either .debug_info or debug_types.
3011 INFO_PTR is a pointer to the start of the CU.
3012 Returns a pointer to the next CU. */
3013
3014 static gdb_byte *
3015 process_psymtab_comp_unit (struct objfile *objfile,
3016 struct dwarf2_per_cu_data *this_cu,
3017 gdb_byte *buffer, gdb_byte *info_ptr,
3018 unsigned int buffer_size)
3019 {
3020 bfd *abfd = objfile->obfd;
3021 gdb_byte *beg_of_comp_unit = info_ptr;
3022 struct die_info *comp_unit_die;
3023 struct partial_symtab *pst;
3024 CORE_ADDR baseaddr;
3025 struct cleanup *back_to_inner;
3026 struct dwarf2_cu cu;
3027 int has_children, has_pc_info;
3028 struct attribute *attr;
3029 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3030 struct die_reader_specs reader_specs;
3031
3032 init_one_comp_unit (&cu, objfile);
3033 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3034
3035 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3036 buffer, buffer_size,
3037 abfd);
3038
3039 /* Complete the cu_header. */
3040 cu.header.offset = beg_of_comp_unit - buffer;
3041 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3042
3043 cu.list_in_scope = &file_symbols;
3044
3045 /* If this compilation unit was already read in, free the
3046 cached copy in order to read it in again. This is
3047 necessary because we skipped some symbols when we first
3048 read in the compilation unit (see load_partial_dies).
3049 This problem could be avoided, but the benefit is
3050 unclear. */
3051 if (this_cu->cu != NULL)
3052 free_one_cached_comp_unit (this_cu->cu);
3053
3054 /* Note that this is a pointer to our stack frame, being
3055 added to a global data structure. It will be cleaned up
3056 in free_stack_comp_unit when we finish with this
3057 compilation unit. */
3058 this_cu->cu = &cu;
3059 cu.per_cu = this_cu;
3060
3061 /* Read the abbrevs for this compilation unit into a table. */
3062 dwarf2_read_abbrevs (abfd, &cu);
3063 make_cleanup (dwarf2_free_abbrev_table, &cu);
3064
3065 /* Read the compilation unit die. */
3066 if (this_cu->from_debug_types)
3067 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3068 init_cu_die_reader (&reader_specs, &cu);
3069 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3070 &has_children);
3071
3072 if (this_cu->from_debug_types)
3073 {
3074 /* offset,length haven't been set yet for type units. */
3075 this_cu->offset = cu.header.offset;
3076 this_cu->length = cu.header.length + cu.header.initial_length_size;
3077 }
3078 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3079 {
3080 info_ptr = (beg_of_comp_unit + cu.header.length
3081 + cu.header.initial_length_size);
3082 do_cleanups (back_to_inner);
3083 return info_ptr;
3084 }
3085
3086 prepare_one_comp_unit (&cu, comp_unit_die);
3087
3088 /* Allocate a new partial symbol table structure. */
3089 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3090 pst = start_psymtab_common (objfile, objfile->section_offsets,
3091 (attr != NULL) ? DW_STRING (attr) : "",
3092 /* TEXTLOW and TEXTHIGH are set below. */
3093 0,
3094 objfile->global_psymbols.next,
3095 objfile->static_psymbols.next);
3096
3097 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3098 if (attr != NULL)
3099 pst->dirname = DW_STRING (attr);
3100
3101 pst->read_symtab_private = this_cu;
3102
3103 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3104
3105 /* Store the function that reads in the rest of the symbol table */
3106 pst->read_symtab = dwarf2_psymtab_to_symtab;
3107
3108 this_cu->v.psymtab = pst;
3109
3110 dwarf2_find_base_address (comp_unit_die, &cu);
3111
3112 /* Possibly set the default values of LOWPC and HIGHPC from
3113 `DW_AT_ranges'. */
3114 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3115 &best_highpc, &cu, pst);
3116 if (has_pc_info == 1 && best_lowpc < best_highpc)
3117 /* Store the contiguous range if it is not empty; it can be empty for
3118 CUs with no code. */
3119 addrmap_set_empty (objfile->psymtabs_addrmap,
3120 best_lowpc + baseaddr,
3121 best_highpc + baseaddr - 1, pst);
3122
3123 /* Check if comp unit has_children.
3124 If so, read the rest of the partial symbols from this comp unit.
3125 If not, there's no more debug_info for this comp unit. */
3126 if (has_children)
3127 {
3128 struct partial_die_info *first_die;
3129 CORE_ADDR lowpc, highpc;
3130
3131 lowpc = ((CORE_ADDR) -1);
3132 highpc = ((CORE_ADDR) 0);
3133
3134 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3135
3136 scan_partial_symbols (first_die, &lowpc, &highpc,
3137 ! has_pc_info, &cu);
3138
3139 /* If we didn't find a lowpc, set it to highpc to avoid
3140 complaints from `maint check'. */
3141 if (lowpc == ((CORE_ADDR) -1))
3142 lowpc = highpc;
3143
3144 /* If the compilation unit didn't have an explicit address range,
3145 then use the information extracted from its child dies. */
3146 if (! has_pc_info)
3147 {
3148 best_lowpc = lowpc;
3149 best_highpc = highpc;
3150 }
3151 }
3152 pst->textlow = best_lowpc + baseaddr;
3153 pst->texthigh = best_highpc + baseaddr;
3154
3155 pst->n_global_syms = objfile->global_psymbols.next -
3156 (objfile->global_psymbols.list + pst->globals_offset);
3157 pst->n_static_syms = objfile->static_psymbols.next -
3158 (objfile->static_psymbols.list + pst->statics_offset);
3159 sort_pst_symbols (pst);
3160
3161 info_ptr = (beg_of_comp_unit + cu.header.length
3162 + cu.header.initial_length_size);
3163
3164 if (this_cu->from_debug_types)
3165 {
3166 /* It's not clear we want to do anything with stmt lists here.
3167 Waiting to see what gcc ultimately does. */
3168 }
3169 else
3170 {
3171 /* Get the list of files included in the current compilation unit,
3172 and build a psymtab for each of them. */
3173 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3174 }
3175
3176 do_cleanups (back_to_inner);
3177
3178 return info_ptr;
3179 }
3180
3181 /* Traversal function for htab_traverse_noresize.
3182 Process one .debug_types comp-unit. */
3183
3184 static int
3185 process_type_comp_unit (void **slot, void *info)
3186 {
3187 struct signatured_type *entry = (struct signatured_type *) *slot;
3188 struct objfile *objfile = (struct objfile *) info;
3189 struct dwarf2_per_cu_data *this_cu;
3190
3191 this_cu = &entry->per_cu;
3192
3193 gdb_assert (dwarf2_per_objfile->types.readin);
3194 process_psymtab_comp_unit (objfile, this_cu,
3195 dwarf2_per_objfile->types.buffer,
3196 dwarf2_per_objfile->types.buffer + entry->offset,
3197 dwarf2_per_objfile->types.size);
3198
3199 return 1;
3200 }
3201
3202 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3203 Build partial symbol tables for the .debug_types comp-units. */
3204
3205 static void
3206 build_type_psymtabs (struct objfile *objfile)
3207 {
3208 if (! create_debug_types_hash_table (objfile))
3209 return;
3210
3211 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3212 process_type_comp_unit, objfile);
3213 }
3214
3215 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3216
3217 static void
3218 psymtabs_addrmap_cleanup (void *o)
3219 {
3220 struct objfile *objfile = o;
3221
3222 objfile->psymtabs_addrmap = NULL;
3223 }
3224
3225 /* Build the partial symbol table by doing a quick pass through the
3226 .debug_info and .debug_abbrev sections. */
3227
3228 static void
3229 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3230 {
3231 gdb_byte *info_ptr;
3232 struct cleanup *back_to, *addrmap_cleanup;
3233 struct obstack temp_obstack;
3234
3235 dwarf2_per_objfile->reading_partial_symbols = 1;
3236
3237 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3238 info_ptr = dwarf2_per_objfile->info.buffer;
3239
3240 /* Any cached compilation units will be linked by the per-objfile
3241 read_in_chain. Make sure to free them when we're done. */
3242 back_to = make_cleanup (free_cached_comp_units, NULL);
3243
3244 build_type_psymtabs (objfile);
3245
3246 create_all_comp_units (objfile);
3247
3248 /* Create a temporary address map on a temporary obstack. We later
3249 copy this to the final obstack. */
3250 obstack_init (&temp_obstack);
3251 make_cleanup_obstack_free (&temp_obstack);
3252 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3253 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3254
3255 /* Since the objects we're extracting from .debug_info vary in
3256 length, only the individual functions to extract them (like
3257 read_comp_unit_head and load_partial_die) can really know whether
3258 the buffer is large enough to hold another complete object.
3259
3260 At the moment, they don't actually check that. If .debug_info
3261 holds just one extra byte after the last compilation unit's dies,
3262 then read_comp_unit_head will happily read off the end of the
3263 buffer. read_partial_die is similarly casual. Those functions
3264 should be fixed.
3265
3266 For this loop condition, simply checking whether there's any data
3267 left at all should be sufficient. */
3268
3269 while (info_ptr < (dwarf2_per_objfile->info.buffer
3270 + dwarf2_per_objfile->info.size))
3271 {
3272 struct dwarf2_per_cu_data *this_cu;
3273
3274 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3275 objfile);
3276
3277 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3278 dwarf2_per_objfile->info.buffer,
3279 info_ptr,
3280 dwarf2_per_objfile->info.size);
3281 }
3282
3283 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3284 &objfile->objfile_obstack);
3285 discard_cleanups (addrmap_cleanup);
3286
3287 do_cleanups (back_to);
3288 }
3289
3290 /* Load the partial DIEs for a secondary CU into memory. */
3291
3292 static void
3293 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3294 struct objfile *objfile)
3295 {
3296 bfd *abfd = objfile->obfd;
3297 gdb_byte *info_ptr, *beg_of_comp_unit;
3298 struct die_info *comp_unit_die;
3299 struct dwarf2_cu *cu;
3300 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3301 int has_children;
3302 struct die_reader_specs reader_specs;
3303 int read_cu = 0;
3304
3305 gdb_assert (! this_cu->from_debug_types);
3306
3307 gdb_assert (dwarf2_per_objfile->info.readin);
3308 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3309 beg_of_comp_unit = info_ptr;
3310
3311 if (this_cu->cu == NULL)
3312 {
3313 cu = xmalloc (sizeof (*cu));
3314 init_one_comp_unit (cu, objfile);
3315
3316 read_cu = 1;
3317
3318 /* If an error occurs while loading, release our storage. */
3319 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3320
3321 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3322 dwarf2_per_objfile->info.buffer,
3323 dwarf2_per_objfile->info.size,
3324 abfd);
3325
3326 /* Complete the cu_header. */
3327 cu->header.offset = this_cu->offset;
3328 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3329
3330 /* Link this compilation unit into the compilation unit tree. */
3331 this_cu->cu = cu;
3332 cu->per_cu = this_cu;
3333
3334 /* Link this CU into read_in_chain. */
3335 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3336 dwarf2_per_objfile->read_in_chain = this_cu;
3337 }
3338 else
3339 {
3340 cu = this_cu->cu;
3341 info_ptr += cu->header.first_die_offset;
3342 }
3343
3344 /* Read the abbrevs for this compilation unit into a table. */
3345 gdb_assert (cu->dwarf2_abbrevs == NULL);
3346 dwarf2_read_abbrevs (abfd, cu);
3347 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3348
3349 /* Read the compilation unit die. */
3350 init_cu_die_reader (&reader_specs, cu);
3351 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3352 &has_children);
3353
3354 prepare_one_comp_unit (cu, comp_unit_die);
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_constant:
3471 case DW_TAG_variable:
3472 case DW_TAG_typedef:
3473 case DW_TAG_union_type:
3474 if (!pdi->is_declaration)
3475 {
3476 add_partial_symbol (pdi, cu);
3477 }
3478 break;
3479 case DW_TAG_class_type:
3480 case DW_TAG_interface_type:
3481 case DW_TAG_structure_type:
3482 if (!pdi->is_declaration)
3483 {
3484 add_partial_symbol (pdi, cu);
3485 }
3486 break;
3487 case DW_TAG_enumeration_type:
3488 if (!pdi->is_declaration)
3489 add_partial_enumeration (pdi, cu);
3490 break;
3491 case DW_TAG_base_type:
3492 case DW_TAG_subrange_type:
3493 /* File scope base type definitions are added to the partial
3494 symbol table. */
3495 add_partial_symbol (pdi, cu);
3496 break;
3497 case DW_TAG_namespace:
3498 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3499 break;
3500 case DW_TAG_module:
3501 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3502 break;
3503 default:
3504 break;
3505 }
3506 }
3507
3508 /* If the die has a sibling, skip to the sibling. */
3509
3510 pdi = pdi->die_sibling;
3511 }
3512 }
3513
3514 /* Functions used to compute the fully scoped name of a partial DIE.
3515
3516 Normally, this is simple. For C++, the parent DIE's fully scoped
3517 name is concatenated with "::" and the partial DIE's name. For
3518 Java, the same thing occurs except that "." is used instead of "::".
3519 Enumerators are an exception; they use the scope of their parent
3520 enumeration type, i.e. the name of the enumeration type is not
3521 prepended to the enumerator.
3522
3523 There are two complexities. One is DW_AT_specification; in this
3524 case "parent" means the parent of the target of the specification,
3525 instead of the direct parent of the DIE. The other is compilers
3526 which do not emit DW_TAG_namespace; in this case we try to guess
3527 the fully qualified name of structure types from their members'
3528 linkage names. This must be done using the DIE's children rather
3529 than the children of any DW_AT_specification target. We only need
3530 to do this for structures at the top level, i.e. if the target of
3531 any DW_AT_specification (if any; otherwise the DIE itself) does not
3532 have a parent. */
3533
3534 /* Compute the scope prefix associated with PDI's parent, in
3535 compilation unit CU. The result will be allocated on CU's
3536 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3537 field. NULL is returned if no prefix is necessary. */
3538 static char *
3539 partial_die_parent_scope (struct partial_die_info *pdi,
3540 struct dwarf2_cu *cu)
3541 {
3542 char *grandparent_scope;
3543 struct partial_die_info *parent, *real_pdi;
3544
3545 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3546 then this means the parent of the specification DIE. */
3547
3548 real_pdi = pdi;
3549 while (real_pdi->has_specification)
3550 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3551
3552 parent = real_pdi->die_parent;
3553 if (parent == NULL)
3554 return NULL;
3555
3556 if (parent->scope_set)
3557 return parent->scope;
3558
3559 fixup_partial_die (parent, cu);
3560
3561 grandparent_scope = partial_die_parent_scope (parent, cu);
3562
3563 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3564 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3565 Work around this problem here. */
3566 if (cu->language == language_cplus
3567 && parent->tag == DW_TAG_namespace
3568 && strcmp (parent->name, "::") == 0
3569 && grandparent_scope == NULL)
3570 {
3571 parent->scope = NULL;
3572 parent->scope_set = 1;
3573 return NULL;
3574 }
3575
3576 if (parent->tag == DW_TAG_namespace
3577 || parent->tag == DW_TAG_module
3578 || parent->tag == DW_TAG_structure_type
3579 || parent->tag == DW_TAG_class_type
3580 || parent->tag == DW_TAG_interface_type
3581 || parent->tag == DW_TAG_union_type
3582 || parent->tag == DW_TAG_enumeration_type)
3583 {
3584 if (grandparent_scope == NULL)
3585 parent->scope = parent->name;
3586 else
3587 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3588 parent->name, 0, cu);
3589 }
3590 else if (parent->tag == DW_TAG_enumerator)
3591 /* Enumerators should not get the name of the enumeration as a prefix. */
3592 parent->scope = grandparent_scope;
3593 else
3594 {
3595 /* FIXME drow/2004-04-01: What should we be doing with
3596 function-local names? For partial symbols, we should probably be
3597 ignoring them. */
3598 complaint (&symfile_complaints,
3599 _("unhandled containing DIE tag %d for DIE at %d"),
3600 parent->tag, pdi->offset);
3601 parent->scope = grandparent_scope;
3602 }
3603
3604 parent->scope_set = 1;
3605 return parent->scope;
3606 }
3607
3608 /* Return the fully scoped name associated with PDI, from compilation unit
3609 CU. The result will be allocated with malloc. */
3610 static char *
3611 partial_die_full_name (struct partial_die_info *pdi,
3612 struct dwarf2_cu *cu)
3613 {
3614 char *parent_scope;
3615
3616 /* If this is a template instantiation, we can not work out the
3617 template arguments from partial DIEs. So, unfortunately, we have
3618 to go through the full DIEs. At least any work we do building
3619 types here will be reused if full symbols are loaded later. */
3620 if (pdi->has_template_arguments)
3621 {
3622 fixup_partial_die (pdi, cu);
3623
3624 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3625 {
3626 struct die_info *die;
3627 struct attribute attr;
3628 struct dwarf2_cu *ref_cu = cu;
3629
3630 attr.name = 0;
3631 attr.form = DW_FORM_ref_addr;
3632 attr.u.addr = pdi->offset;
3633 die = follow_die_ref (NULL, &attr, &ref_cu);
3634
3635 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3636 }
3637 }
3638
3639 parent_scope = partial_die_parent_scope (pdi, cu);
3640 if (parent_scope == NULL)
3641 return NULL;
3642 else
3643 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3644 }
3645
3646 static void
3647 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3648 {
3649 struct objfile *objfile = cu->objfile;
3650 CORE_ADDR addr = 0;
3651 char *actual_name = NULL;
3652 const struct partial_symbol *psym = NULL;
3653 CORE_ADDR baseaddr;
3654 int built_actual_name = 0;
3655
3656 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3657
3658 actual_name = partial_die_full_name (pdi, cu);
3659 if (actual_name)
3660 built_actual_name = 1;
3661
3662 if (actual_name == NULL)
3663 actual_name = pdi->name;
3664
3665 switch (pdi->tag)
3666 {
3667 case DW_TAG_subprogram:
3668 if (pdi->is_external || cu->language == language_ada)
3669 {
3670 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3671 of the global scope. But in Ada, we want to be able to access
3672 nested procedures globally. So all Ada subprograms are stored
3673 in the global scope. */
3674 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3675 mst_text, objfile); */
3676 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3677 built_actual_name,
3678 VAR_DOMAIN, LOC_BLOCK,
3679 &objfile->global_psymbols,
3680 0, pdi->lowpc + baseaddr,
3681 cu->language, objfile);
3682 }
3683 else
3684 {
3685 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3686 mst_file_text, objfile); */
3687 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3688 built_actual_name,
3689 VAR_DOMAIN, LOC_BLOCK,
3690 &objfile->static_psymbols,
3691 0, pdi->lowpc + baseaddr,
3692 cu->language, objfile);
3693 }
3694 break;
3695 case DW_TAG_constant:
3696 {
3697 struct psymbol_allocation_list *list;
3698
3699 if (pdi->is_external)
3700 list = &objfile->global_psymbols;
3701 else
3702 list = &objfile->static_psymbols;
3703 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3704 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3705 list, 0, 0, cu->language, objfile);
3706
3707 }
3708 break;
3709 case DW_TAG_variable:
3710 if (pdi->locdesc)
3711 addr = decode_locdesc (pdi->locdesc, cu);
3712
3713 if (pdi->locdesc
3714 && addr == 0
3715 && !dwarf2_per_objfile->has_section_at_zero)
3716 {
3717 /* A global or static variable may also have been stripped
3718 out by the linker if unused, in which case its address
3719 will be nullified; do not add such variables into partial
3720 symbol table then. */
3721 }
3722 else if (pdi->is_external)
3723 {
3724 /* Global Variable.
3725 Don't enter into the minimal symbol tables as there is
3726 a minimal symbol table entry from the ELF symbols already.
3727 Enter into partial symbol table if it has a location
3728 descriptor or a type.
3729 If the location descriptor is missing, new_symbol will create
3730 a LOC_UNRESOLVED symbol, the address of the variable will then
3731 be determined from the minimal symbol table whenever the variable
3732 is referenced.
3733 The address for the partial symbol table entry is not
3734 used by GDB, but it comes in handy for debugging partial symbol
3735 table building. */
3736
3737 if (pdi->locdesc || pdi->has_type)
3738 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3739 built_actual_name,
3740 VAR_DOMAIN, LOC_STATIC,
3741 &objfile->global_psymbols,
3742 0, addr + baseaddr,
3743 cu->language, objfile);
3744 }
3745 else
3746 {
3747 /* Static Variable. Skip symbols without location descriptors. */
3748 if (pdi->locdesc == NULL)
3749 {
3750 if (built_actual_name)
3751 xfree (actual_name);
3752 return;
3753 }
3754 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3755 mst_file_data, objfile); */
3756 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3757 built_actual_name,
3758 VAR_DOMAIN, LOC_STATIC,
3759 &objfile->static_psymbols,
3760 0, addr + baseaddr,
3761 cu->language, objfile);
3762 }
3763 break;
3764 case DW_TAG_typedef:
3765 case DW_TAG_base_type:
3766 case DW_TAG_subrange_type:
3767 add_psymbol_to_list (actual_name, strlen (actual_name),
3768 built_actual_name,
3769 VAR_DOMAIN, LOC_TYPEDEF,
3770 &objfile->static_psymbols,
3771 0, (CORE_ADDR) 0, cu->language, objfile);
3772 break;
3773 case DW_TAG_namespace:
3774 add_psymbol_to_list (actual_name, strlen (actual_name),
3775 built_actual_name,
3776 VAR_DOMAIN, LOC_TYPEDEF,
3777 &objfile->global_psymbols,
3778 0, (CORE_ADDR) 0, cu->language, objfile);
3779 break;
3780 case DW_TAG_class_type:
3781 case DW_TAG_interface_type:
3782 case DW_TAG_structure_type:
3783 case DW_TAG_union_type:
3784 case DW_TAG_enumeration_type:
3785 /* Skip external references. The DWARF standard says in the section
3786 about "Structure, Union, and Class Type Entries": "An incomplete
3787 structure, union or class type is represented by a structure,
3788 union or class entry that does not have a byte size attribute
3789 and that has a DW_AT_declaration attribute." */
3790 if (!pdi->has_byte_size && pdi->is_declaration)
3791 {
3792 if (built_actual_name)
3793 xfree (actual_name);
3794 return;
3795 }
3796
3797 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3798 static vs. global. */
3799 add_psymbol_to_list (actual_name, strlen (actual_name),
3800 built_actual_name,
3801 STRUCT_DOMAIN, LOC_TYPEDEF,
3802 (cu->language == language_cplus
3803 || cu->language == language_java)
3804 ? &objfile->global_psymbols
3805 : &objfile->static_psymbols,
3806 0, (CORE_ADDR) 0, cu->language, objfile);
3807
3808 break;
3809 case DW_TAG_enumerator:
3810 add_psymbol_to_list (actual_name, strlen (actual_name),
3811 built_actual_name,
3812 VAR_DOMAIN, LOC_CONST,
3813 (cu->language == language_cplus
3814 || cu->language == language_java)
3815 ? &objfile->global_psymbols
3816 : &objfile->static_psymbols,
3817 0, (CORE_ADDR) 0, cu->language, objfile);
3818 break;
3819 default:
3820 break;
3821 }
3822
3823 if (built_actual_name)
3824 xfree (actual_name);
3825 }
3826
3827 /* Read a partial die corresponding to a namespace; also, add a symbol
3828 corresponding to that namespace to the symbol table. NAMESPACE is
3829 the name of the enclosing namespace. */
3830
3831 static void
3832 add_partial_namespace (struct partial_die_info *pdi,
3833 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3834 int need_pc, struct dwarf2_cu *cu)
3835 {
3836 /* Add a symbol for the namespace. */
3837
3838 add_partial_symbol (pdi, cu);
3839
3840 /* Now scan partial symbols in that namespace. */
3841
3842 if (pdi->has_children)
3843 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3844 }
3845
3846 /* Read a partial die corresponding to a Fortran module. */
3847
3848 static void
3849 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3850 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3851 {
3852 /* Now scan partial symbols in that module. */
3853
3854 if (pdi->has_children)
3855 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3856 }
3857
3858 /* Read a partial die corresponding to a subprogram and create a partial
3859 symbol for that subprogram. When the CU language allows it, this
3860 routine also defines a partial symbol for each nested subprogram
3861 that this subprogram contains.
3862
3863 DIE my also be a lexical block, in which case we simply search
3864 recursively for suprograms defined inside that lexical block.
3865 Again, this is only performed when the CU language allows this
3866 type of definitions. */
3867
3868 static void
3869 add_partial_subprogram (struct partial_die_info *pdi,
3870 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3871 int need_pc, struct dwarf2_cu *cu)
3872 {
3873 if (pdi->tag == DW_TAG_subprogram)
3874 {
3875 if (pdi->has_pc_info)
3876 {
3877 if (pdi->lowpc < *lowpc)
3878 *lowpc = pdi->lowpc;
3879 if (pdi->highpc > *highpc)
3880 *highpc = pdi->highpc;
3881 if (need_pc)
3882 {
3883 CORE_ADDR baseaddr;
3884 struct objfile *objfile = cu->objfile;
3885
3886 baseaddr = ANOFFSET (objfile->section_offsets,
3887 SECT_OFF_TEXT (objfile));
3888 addrmap_set_empty (objfile->psymtabs_addrmap,
3889 pdi->lowpc + baseaddr,
3890 pdi->highpc - 1 + baseaddr,
3891 cu->per_cu->v.psymtab);
3892 }
3893 if (!pdi->is_declaration)
3894 /* Ignore subprogram DIEs that do not have a name, they are
3895 illegal. Do not emit a complaint at this point, we will
3896 do so when we convert this psymtab into a symtab. */
3897 if (pdi->name)
3898 add_partial_symbol (pdi, cu);
3899 }
3900 }
3901
3902 if (! pdi->has_children)
3903 return;
3904
3905 if (cu->language == language_ada)
3906 {
3907 pdi = pdi->die_child;
3908 while (pdi != NULL)
3909 {
3910 fixup_partial_die (pdi, cu);
3911 if (pdi->tag == DW_TAG_subprogram
3912 || pdi->tag == DW_TAG_lexical_block)
3913 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3914 pdi = pdi->die_sibling;
3915 }
3916 }
3917 }
3918
3919 /* Read a partial die corresponding to an enumeration type. */
3920
3921 static void
3922 add_partial_enumeration (struct partial_die_info *enum_pdi,
3923 struct dwarf2_cu *cu)
3924 {
3925 struct partial_die_info *pdi;
3926
3927 if (enum_pdi->name != NULL)
3928 add_partial_symbol (enum_pdi, cu);
3929
3930 pdi = enum_pdi->die_child;
3931 while (pdi)
3932 {
3933 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3934 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3935 else
3936 add_partial_symbol (pdi, cu);
3937 pdi = pdi->die_sibling;
3938 }
3939 }
3940
3941 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3942 Return the corresponding abbrev, or NULL if the number is zero (indicating
3943 an empty DIE). In either case *BYTES_READ will be set to the length of
3944 the initial number. */
3945
3946 static struct abbrev_info *
3947 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3948 struct dwarf2_cu *cu)
3949 {
3950 bfd *abfd = cu->objfile->obfd;
3951 unsigned int abbrev_number;
3952 struct abbrev_info *abbrev;
3953
3954 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3955
3956 if (abbrev_number == 0)
3957 return NULL;
3958
3959 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3960 if (!abbrev)
3961 {
3962 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3963 bfd_get_filename (abfd));
3964 }
3965
3966 return abbrev;
3967 }
3968
3969 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3970 Returns a pointer to the end of a series of DIEs, terminated by an empty
3971 DIE. Any children of the skipped DIEs will also be skipped. */
3972
3973 static gdb_byte *
3974 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3975 {
3976 struct abbrev_info *abbrev;
3977 unsigned int bytes_read;
3978
3979 while (1)
3980 {
3981 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3982 if (abbrev == NULL)
3983 return info_ptr + bytes_read;
3984 else
3985 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
3986 }
3987 }
3988
3989 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3990 INFO_PTR should point just after the initial uleb128 of a DIE, and the
3991 abbrev corresponding to that skipped uleb128 should be passed in
3992 ABBREV. Returns a pointer to this DIE's sibling, skipping any
3993 children. */
3994
3995 static gdb_byte *
3996 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
3997 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
3998 {
3999 unsigned int bytes_read;
4000 struct attribute attr;
4001 bfd *abfd = cu->objfile->obfd;
4002 unsigned int form, i;
4003
4004 for (i = 0; i < abbrev->num_attrs; i++)
4005 {
4006 /* The only abbrev we care about is DW_AT_sibling. */
4007 if (abbrev->attrs[i].name == DW_AT_sibling)
4008 {
4009 read_attribute (&attr, &abbrev->attrs[i],
4010 abfd, info_ptr, cu);
4011 if (attr.form == DW_FORM_ref_addr)
4012 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4013 else
4014 return buffer + dwarf2_get_ref_die_offset (&attr);
4015 }
4016
4017 /* If it isn't DW_AT_sibling, skip this attribute. */
4018 form = abbrev->attrs[i].form;
4019 skip_attribute:
4020 switch (form)
4021 {
4022 case DW_FORM_ref_addr:
4023 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4024 and later it is offset sized. */
4025 if (cu->header.version == 2)
4026 info_ptr += cu->header.addr_size;
4027 else
4028 info_ptr += cu->header.offset_size;
4029 break;
4030 case DW_FORM_addr:
4031 info_ptr += cu->header.addr_size;
4032 break;
4033 case DW_FORM_data1:
4034 case DW_FORM_ref1:
4035 case DW_FORM_flag:
4036 info_ptr += 1;
4037 break;
4038 case DW_FORM_flag_present:
4039 break;
4040 case DW_FORM_data2:
4041 case DW_FORM_ref2:
4042 info_ptr += 2;
4043 break;
4044 case DW_FORM_data4:
4045 case DW_FORM_ref4:
4046 info_ptr += 4;
4047 break;
4048 case DW_FORM_data8:
4049 case DW_FORM_ref8:
4050 case DW_FORM_sig8:
4051 info_ptr += 8;
4052 break;
4053 case DW_FORM_string:
4054 read_direct_string (abfd, info_ptr, &bytes_read);
4055 info_ptr += bytes_read;
4056 break;
4057 case DW_FORM_sec_offset:
4058 case DW_FORM_strp:
4059 info_ptr += cu->header.offset_size;
4060 break;
4061 case DW_FORM_exprloc:
4062 case DW_FORM_block:
4063 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4064 info_ptr += bytes_read;
4065 break;
4066 case DW_FORM_block1:
4067 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4068 break;
4069 case DW_FORM_block2:
4070 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4071 break;
4072 case DW_FORM_block4:
4073 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4074 break;
4075 case DW_FORM_sdata:
4076 case DW_FORM_udata:
4077 case DW_FORM_ref_udata:
4078 info_ptr = skip_leb128 (abfd, info_ptr);
4079 break;
4080 case DW_FORM_indirect:
4081 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4082 info_ptr += bytes_read;
4083 /* We need to continue parsing from here, so just go back to
4084 the top. */
4085 goto skip_attribute;
4086
4087 default:
4088 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4089 dwarf_form_name (form),
4090 bfd_get_filename (abfd));
4091 }
4092 }
4093
4094 if (abbrev->has_children)
4095 return skip_children (buffer, info_ptr, cu);
4096 else
4097 return info_ptr;
4098 }
4099
4100 /* Locate ORIG_PDI's sibling.
4101 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4102 in BUFFER. */
4103
4104 static gdb_byte *
4105 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4106 gdb_byte *buffer, gdb_byte *info_ptr,
4107 bfd *abfd, struct dwarf2_cu *cu)
4108 {
4109 /* Do we know the sibling already? */
4110
4111 if (orig_pdi->sibling)
4112 return orig_pdi->sibling;
4113
4114 /* Are there any children to deal with? */
4115
4116 if (!orig_pdi->has_children)
4117 return info_ptr;
4118
4119 /* Skip the children the long way. */
4120
4121 return skip_children (buffer, info_ptr, cu);
4122 }
4123
4124 /* Expand this partial symbol table into a full symbol table. */
4125
4126 static void
4127 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4128 {
4129 if (pst != NULL)
4130 {
4131 if (pst->readin)
4132 {
4133 warning (_("bug: psymtab for %s is already read in."), pst->filename);
4134 }
4135 else
4136 {
4137 if (info_verbose)
4138 {
4139 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4140 gdb_flush (gdb_stdout);
4141 }
4142
4143 /* Restore our global data. */
4144 dwarf2_per_objfile = objfile_data (pst->objfile,
4145 dwarf2_objfile_data_key);
4146
4147 /* If this psymtab is constructed from a debug-only objfile, the
4148 has_section_at_zero flag will not necessarily be correct. We
4149 can get the correct value for this flag by looking at the data
4150 associated with the (presumably stripped) associated objfile. */
4151 if (pst->objfile->separate_debug_objfile_backlink)
4152 {
4153 struct dwarf2_per_objfile *dpo_backlink
4154 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4155 dwarf2_objfile_data_key);
4156
4157 dwarf2_per_objfile->has_section_at_zero
4158 = dpo_backlink->has_section_at_zero;
4159 }
4160
4161 dwarf2_per_objfile->reading_partial_symbols = 0;
4162
4163 psymtab_to_symtab_1 (pst);
4164
4165 /* Finish up the debug error message. */
4166 if (info_verbose)
4167 printf_filtered (_("done.\n"));
4168 }
4169 }
4170 }
4171
4172 /* Add PER_CU to the queue. */
4173
4174 static void
4175 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4176 {
4177 struct dwarf2_queue_item *item;
4178
4179 per_cu->queued = 1;
4180 item = xmalloc (sizeof (*item));
4181 item->per_cu = per_cu;
4182 item->next = NULL;
4183
4184 if (dwarf2_queue == NULL)
4185 dwarf2_queue = item;
4186 else
4187 dwarf2_queue_tail->next = item;
4188
4189 dwarf2_queue_tail = item;
4190 }
4191
4192 /* Process the queue. */
4193
4194 static void
4195 process_queue (struct objfile *objfile)
4196 {
4197 struct dwarf2_queue_item *item, *next_item;
4198
4199 /* The queue starts out with one item, but following a DIE reference
4200 may load a new CU, adding it to the end of the queue. */
4201 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4202 {
4203 if (dwarf2_per_objfile->using_index
4204 ? !item->per_cu->v.quick->symtab
4205 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4206 process_full_comp_unit (item->per_cu);
4207
4208 item->per_cu->queued = 0;
4209 next_item = item->next;
4210 xfree (item);
4211 }
4212
4213 dwarf2_queue_tail = NULL;
4214 }
4215
4216 /* Free all allocated queue entries. This function only releases anything if
4217 an error was thrown; if the queue was processed then it would have been
4218 freed as we went along. */
4219
4220 static void
4221 dwarf2_release_queue (void *dummy)
4222 {
4223 struct dwarf2_queue_item *item, *last;
4224
4225 item = dwarf2_queue;
4226 while (item)
4227 {
4228 /* Anything still marked queued is likely to be in an
4229 inconsistent state, so discard it. */
4230 if (item->per_cu->queued)
4231 {
4232 if (item->per_cu->cu != NULL)
4233 free_one_cached_comp_unit (item->per_cu->cu);
4234 item->per_cu->queued = 0;
4235 }
4236
4237 last = item;
4238 item = item->next;
4239 xfree (last);
4240 }
4241
4242 dwarf2_queue = dwarf2_queue_tail = NULL;
4243 }
4244
4245 /* Read in full symbols for PST, and anything it depends on. */
4246
4247 static void
4248 psymtab_to_symtab_1 (struct partial_symtab *pst)
4249 {
4250 struct dwarf2_per_cu_data *per_cu;
4251 struct cleanup *back_to;
4252 int i;
4253
4254 for (i = 0; i < pst->number_of_dependencies; i++)
4255 if (!pst->dependencies[i]->readin)
4256 {
4257 /* Inform about additional files that need to be read in. */
4258 if (info_verbose)
4259 {
4260 /* FIXME: i18n: Need to make this a single string. */
4261 fputs_filtered (" ", gdb_stdout);
4262 wrap_here ("");
4263 fputs_filtered ("and ", gdb_stdout);
4264 wrap_here ("");
4265 printf_filtered ("%s...", pst->dependencies[i]->filename);
4266 wrap_here (""); /* Flush output */
4267 gdb_flush (gdb_stdout);
4268 }
4269 psymtab_to_symtab_1 (pst->dependencies[i]);
4270 }
4271
4272 per_cu = pst->read_symtab_private;
4273
4274 if (per_cu == NULL)
4275 {
4276 /* It's an include file, no symbols to read for it.
4277 Everything is in the parent symtab. */
4278 pst->readin = 1;
4279 return;
4280 }
4281
4282 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4283 }
4284
4285 /* Load the DIEs associated with PER_CU into memory. */
4286
4287 static void
4288 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4289 {
4290 bfd *abfd = objfile->obfd;
4291 struct dwarf2_cu *cu;
4292 unsigned int offset;
4293 gdb_byte *info_ptr, *beg_of_comp_unit;
4294 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4295 struct attribute *attr;
4296 int read_cu = 0;
4297
4298 gdb_assert (! per_cu->from_debug_types);
4299
4300 /* Set local variables from the partial symbol table info. */
4301 offset = per_cu->offset;
4302
4303 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4304 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4305 beg_of_comp_unit = info_ptr;
4306
4307 if (per_cu->cu == NULL)
4308 {
4309 cu = xmalloc (sizeof (*cu));
4310 init_one_comp_unit (cu, objfile);
4311
4312 read_cu = 1;
4313
4314 /* If an error occurs while loading, release our storage. */
4315 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4316
4317 /* Read in the comp_unit header. */
4318 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4319
4320 /* Complete the cu_header. */
4321 cu->header.offset = offset;
4322 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4323
4324 /* Read the abbrevs for this compilation unit. */
4325 dwarf2_read_abbrevs (abfd, cu);
4326 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4327
4328 /* Link this compilation unit into the compilation unit tree. */
4329 per_cu->cu = cu;
4330 cu->per_cu = per_cu;
4331
4332 /* Link this CU into read_in_chain. */
4333 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4334 dwarf2_per_objfile->read_in_chain = per_cu;
4335 }
4336 else
4337 {
4338 cu = per_cu->cu;
4339 info_ptr += cu->header.first_die_offset;
4340 }
4341
4342 cu->dies = read_comp_unit (info_ptr, cu);
4343
4344 /* We try not to read any attributes in this function, because not
4345 all objfiles needed for references have been loaded yet, and symbol
4346 table processing isn't initialized. But we have to set the CU language,
4347 or we won't be able to build types correctly. */
4348 prepare_one_comp_unit (cu, cu->dies);
4349
4350 /* Similarly, if we do not read the producer, we can not apply
4351 producer-specific interpretation. */
4352 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4353 if (attr)
4354 cu->producer = DW_STRING (attr);
4355
4356 if (read_cu)
4357 {
4358 do_cleanups (free_abbrevs_cleanup);
4359
4360 /* We've successfully allocated this compilation unit. Let our
4361 caller clean it up when finished with it. */
4362 discard_cleanups (free_cu_cleanup);
4363 }
4364 }
4365
4366 /* Add a DIE to the delayed physname list. */
4367
4368 static void
4369 add_to_method_list (struct type *type, int fnfield_index, int index,
4370 const char *name, struct die_info *die,
4371 struct dwarf2_cu *cu)
4372 {
4373 struct delayed_method_info mi;
4374 mi.type = type;
4375 mi.fnfield_index = fnfield_index;
4376 mi.index = index;
4377 mi.name = name;
4378 mi.die = die;
4379 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4380 }
4381
4382 /* A cleanup for freeing the delayed method list. */
4383
4384 static void
4385 free_delayed_list (void *ptr)
4386 {
4387 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4388 if (cu->method_list != NULL)
4389 {
4390 VEC_free (delayed_method_info, cu->method_list);
4391 cu->method_list = NULL;
4392 }
4393 }
4394
4395 /* Compute the physnames of any methods on the CU's method list.
4396
4397 The computation of method physnames is delayed in order to avoid the
4398 (bad) condition that one of the method's formal parameters is of an as yet
4399 incomplete type. */
4400
4401 static void
4402 compute_delayed_physnames (struct dwarf2_cu *cu)
4403 {
4404 int i;
4405 struct delayed_method_info *mi;
4406 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4407 {
4408 char *physname;
4409 struct fn_fieldlist *fn_flp
4410 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4411 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4412 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4413 }
4414 }
4415
4416 /* Generate full symbol information for PST and CU, whose DIEs have
4417 already been loaded into memory. */
4418
4419 static void
4420 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4421 {
4422 struct dwarf2_cu *cu = per_cu->cu;
4423 struct objfile *objfile = per_cu->objfile;
4424 CORE_ADDR lowpc, highpc;
4425 struct symtab *symtab;
4426 struct cleanup *back_to, *delayed_list_cleanup;
4427 CORE_ADDR baseaddr;
4428
4429 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4430
4431 buildsym_init ();
4432 back_to = make_cleanup (really_free_pendings, NULL);
4433 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4434
4435 cu->list_in_scope = &file_symbols;
4436
4437 dwarf2_find_base_address (cu->dies, cu);
4438
4439 /* Do line number decoding in read_file_scope () */
4440 process_die (cu->dies, cu);
4441
4442 /* Now that we have processed all the DIEs in the CU, all the types
4443 should be complete, and it should now be safe to compute all of the
4444 physnames. */
4445 compute_delayed_physnames (cu);
4446 do_cleanups (delayed_list_cleanup);
4447
4448 /* Some compilers don't define a DW_AT_high_pc attribute for the
4449 compilation unit. If the DW_AT_high_pc is missing, synthesize
4450 it, by scanning the DIE's below the compilation unit. */
4451 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4452
4453 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4454
4455 /* Set symtab language to language from DW_AT_language.
4456 If the compilation is from a C file generated by language preprocessors,
4457 do not set the language if it was already deduced by start_subfile. */
4458 if (symtab != NULL
4459 && !(cu->language == language_c && symtab->language != language_c))
4460 {
4461 symtab->language = cu->language;
4462 }
4463
4464 if (dwarf2_per_objfile->using_index)
4465 per_cu->v.quick->symtab = symtab;
4466 else
4467 {
4468 struct partial_symtab *pst = per_cu->v.psymtab;
4469 pst->symtab = symtab;
4470 pst->readin = 1;
4471 }
4472
4473 do_cleanups (back_to);
4474 }
4475
4476 /* Process a die and its children. */
4477
4478 static void
4479 process_die (struct die_info *die, struct dwarf2_cu *cu)
4480 {
4481 switch (die->tag)
4482 {
4483 case DW_TAG_padding:
4484 break;
4485 case DW_TAG_compile_unit:
4486 read_file_scope (die, cu);
4487 break;
4488 case DW_TAG_type_unit:
4489 read_type_unit_scope (die, cu);
4490 break;
4491 case DW_TAG_subprogram:
4492 case DW_TAG_inlined_subroutine:
4493 read_func_scope (die, cu);
4494 break;
4495 case DW_TAG_lexical_block:
4496 case DW_TAG_try_block:
4497 case DW_TAG_catch_block:
4498 read_lexical_block_scope (die, cu);
4499 break;
4500 case DW_TAG_class_type:
4501 case DW_TAG_interface_type:
4502 case DW_TAG_structure_type:
4503 case DW_TAG_union_type:
4504 process_structure_scope (die, cu);
4505 break;
4506 case DW_TAG_enumeration_type:
4507 process_enumeration_scope (die, cu);
4508 break;
4509
4510 /* These dies have a type, but processing them does not create
4511 a symbol or recurse to process the children. Therefore we can
4512 read them on-demand through read_type_die. */
4513 case DW_TAG_subroutine_type:
4514 case DW_TAG_set_type:
4515 case DW_TAG_array_type:
4516 case DW_TAG_pointer_type:
4517 case DW_TAG_ptr_to_member_type:
4518 case DW_TAG_reference_type:
4519 case DW_TAG_string_type:
4520 break;
4521
4522 case DW_TAG_base_type:
4523 case DW_TAG_subrange_type:
4524 case DW_TAG_typedef:
4525 /* Add a typedef symbol for the type definition, if it has a
4526 DW_AT_name. */
4527 new_symbol (die, read_type_die (die, cu), cu);
4528 break;
4529 case DW_TAG_common_block:
4530 read_common_block (die, cu);
4531 break;
4532 case DW_TAG_common_inclusion:
4533 break;
4534 case DW_TAG_namespace:
4535 processing_has_namespace_info = 1;
4536 read_namespace (die, cu);
4537 break;
4538 case DW_TAG_module:
4539 processing_has_namespace_info = 1;
4540 read_module (die, cu);
4541 break;
4542 case DW_TAG_imported_declaration:
4543 case DW_TAG_imported_module:
4544 processing_has_namespace_info = 1;
4545 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4546 || cu->language != language_fortran))
4547 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4548 dwarf_tag_name (die->tag));
4549 read_import_statement (die, cu);
4550 break;
4551 default:
4552 new_symbol (die, NULL, cu);
4553 break;
4554 }
4555 }
4556
4557 /* A helper function for dwarf2_compute_name which determines whether DIE
4558 needs to have the name of the scope prepended to the name listed in the
4559 die. */
4560
4561 static int
4562 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4563 {
4564 struct attribute *attr;
4565
4566 switch (die->tag)
4567 {
4568 case DW_TAG_namespace:
4569 case DW_TAG_typedef:
4570 case DW_TAG_class_type:
4571 case DW_TAG_interface_type:
4572 case DW_TAG_structure_type:
4573 case DW_TAG_union_type:
4574 case DW_TAG_enumeration_type:
4575 case DW_TAG_enumerator:
4576 case DW_TAG_subprogram:
4577 case DW_TAG_member:
4578 return 1;
4579
4580 case DW_TAG_variable:
4581 case DW_TAG_constant:
4582 /* We only need to prefix "globally" visible variables. These include
4583 any variable marked with DW_AT_external or any variable that
4584 lives in a namespace. [Variables in anonymous namespaces
4585 require prefixing, but they are not DW_AT_external.] */
4586
4587 if (dwarf2_attr (die, DW_AT_specification, cu))
4588 {
4589 struct dwarf2_cu *spec_cu = cu;
4590
4591 return die_needs_namespace (die_specification (die, &spec_cu),
4592 spec_cu);
4593 }
4594
4595 attr = dwarf2_attr (die, DW_AT_external, cu);
4596 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4597 && die->parent->tag != DW_TAG_module)
4598 return 0;
4599 /* A variable in a lexical block of some kind does not need a
4600 namespace, even though in C++ such variables may be external
4601 and have a mangled name. */
4602 if (die->parent->tag == DW_TAG_lexical_block
4603 || die->parent->tag == DW_TAG_try_block
4604 || die->parent->tag == DW_TAG_catch_block
4605 || die->parent->tag == DW_TAG_subprogram)
4606 return 0;
4607 return 1;
4608
4609 default:
4610 return 0;
4611 }
4612 }
4613
4614 /* Retrieve the last character from a mem_file. */
4615
4616 static void
4617 do_ui_file_peek_last (void *object, const char *buffer, long length)
4618 {
4619 char *last_char_p = (char *) object;
4620
4621 if (length > 0)
4622 *last_char_p = buffer[length - 1];
4623 }
4624
4625 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4626 compute the physname for the object, which include a method's
4627 formal parameters (C++/Java) and return type (Java).
4628
4629 For Ada, return the DIE's linkage name rather than the fully qualified
4630 name. PHYSNAME is ignored..
4631
4632 The result is allocated on the objfile_obstack and canonicalized. */
4633
4634 static const char *
4635 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4636 int physname)
4637 {
4638 if (name == NULL)
4639 name = dwarf2_name (die, cu);
4640
4641 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4642 compute it by typename_concat inside GDB. */
4643 if (cu->language == language_ada
4644 || (cu->language == language_fortran && physname))
4645 {
4646 /* For Ada unit, we prefer the linkage name over the name, as
4647 the former contains the exported name, which the user expects
4648 to be able to reference. Ideally, we want the user to be able
4649 to reference this entity using either natural or linkage name,
4650 but we haven't started looking at this enhancement yet. */
4651 struct attribute *attr;
4652
4653 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4654 if (attr == NULL)
4655 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4656 if (attr && DW_STRING (attr))
4657 return DW_STRING (attr);
4658 }
4659
4660 /* These are the only languages we know how to qualify names in. */
4661 if (name != NULL
4662 && (cu->language == language_cplus || cu->language == language_java
4663 || cu->language == language_fortran))
4664 {
4665 if (die_needs_namespace (die, cu))
4666 {
4667 long length;
4668 char *prefix;
4669 struct ui_file *buf;
4670
4671 prefix = determine_prefix (die, cu);
4672 buf = mem_fileopen ();
4673 if (*prefix != '\0')
4674 {
4675 char *prefixed_name = typename_concat (NULL, prefix, name,
4676 physname, cu);
4677
4678 fputs_unfiltered (prefixed_name, buf);
4679 xfree (prefixed_name);
4680 }
4681 else
4682 fputs_unfiltered (name ? name : "", buf);
4683
4684 /* Template parameters may be specified in the DIE's DW_AT_name, or
4685 as children with DW_TAG_template_type_param or
4686 DW_TAG_value_type_param. If the latter, add them to the name
4687 here. If the name already has template parameters, then
4688 skip this step; some versions of GCC emit both, and
4689 it is more efficient to use the pre-computed name.
4690
4691 Something to keep in mind about this process: it is very
4692 unlikely, or in some cases downright impossible, to produce
4693 something that will match the mangled name of a function.
4694 If the definition of the function has the same debug info,
4695 we should be able to match up with it anyway. But fallbacks
4696 using the minimal symbol, for instance to find a method
4697 implemented in a stripped copy of libstdc++, will not work.
4698 If we do not have debug info for the definition, we will have to
4699 match them up some other way.
4700
4701 When we do name matching there is a related problem with function
4702 templates; two instantiated function templates are allowed to
4703 differ only by their return types, which we do not add here. */
4704
4705 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4706 {
4707 struct attribute *attr;
4708 struct die_info *child;
4709 int first = 1;
4710
4711 die->building_fullname = 1;
4712
4713 for (child = die->child; child != NULL; child = child->sibling)
4714 {
4715 struct type *type;
4716 long value;
4717 gdb_byte *bytes;
4718 struct dwarf2_locexpr_baton *baton;
4719 struct value *v;
4720
4721 if (child->tag != DW_TAG_template_type_param
4722 && child->tag != DW_TAG_template_value_param)
4723 continue;
4724
4725 if (first)
4726 {
4727 fputs_unfiltered ("<", buf);
4728 first = 0;
4729 }
4730 else
4731 fputs_unfiltered (", ", buf);
4732
4733 attr = dwarf2_attr (child, DW_AT_type, cu);
4734 if (attr == NULL)
4735 {
4736 complaint (&symfile_complaints,
4737 _("template parameter missing DW_AT_type"));
4738 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4739 continue;
4740 }
4741 type = die_type (child, cu);
4742
4743 if (child->tag == DW_TAG_template_type_param)
4744 {
4745 c_print_type (type, "", buf, -1, 0);
4746 continue;
4747 }
4748
4749 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4750 if (attr == NULL)
4751 {
4752 complaint (&symfile_complaints,
4753 _("template parameter missing DW_AT_const_value"));
4754 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4755 continue;
4756 }
4757
4758 dwarf2_const_value_attr (attr, type, name,
4759 &cu->comp_unit_obstack, cu,
4760 &value, &bytes, &baton);
4761
4762 if (TYPE_NOSIGN (type))
4763 /* GDB prints characters as NUMBER 'CHAR'. If that's
4764 changed, this can use value_print instead. */
4765 c_printchar (value, type, buf);
4766 else
4767 {
4768 struct value_print_options opts;
4769
4770 if (baton != NULL)
4771 v = dwarf2_evaluate_loc_desc (type, NULL,
4772 baton->data,
4773 baton->size,
4774 baton->per_cu);
4775 else if (bytes != NULL)
4776 {
4777 v = allocate_value (type);
4778 memcpy (value_contents_writeable (v), bytes,
4779 TYPE_LENGTH (type));
4780 }
4781 else
4782 v = value_from_longest (type, value);
4783
4784 /* Specify decimal so that we do not depend on the radix. */
4785 get_formatted_print_options (&opts, 'd');
4786 opts.raw = 1;
4787 value_print (v, buf, &opts);
4788 release_value (v);
4789 value_free (v);
4790 }
4791 }
4792
4793 die->building_fullname = 0;
4794
4795 if (!first)
4796 {
4797 /* Close the argument list, with a space if necessary
4798 (nested templates). */
4799 char last_char = '\0';
4800 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4801 if (last_char == '>')
4802 fputs_unfiltered (" >", buf);
4803 else
4804 fputs_unfiltered (">", buf);
4805 }
4806 }
4807
4808 /* For Java and C++ methods, append formal parameter type
4809 information, if PHYSNAME. */
4810
4811 if (physname && die->tag == DW_TAG_subprogram
4812 && (cu->language == language_cplus
4813 || cu->language == language_java))
4814 {
4815 struct type *type = read_type_die (die, cu);
4816
4817 c_type_print_args (type, buf, 0, cu->language);
4818
4819 if (cu->language == language_java)
4820 {
4821 /* For java, we must append the return type to method
4822 names. */
4823 if (die->tag == DW_TAG_subprogram)
4824 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4825 0, 0);
4826 }
4827 else if (cu->language == language_cplus)
4828 {
4829 /* Assume that an artificial first parameter is
4830 "this", but do not crash if it is not. RealView
4831 marks unnamed (and thus unused) parameters as
4832 artificial; there is no way to differentiate
4833 the two cases. */
4834 if (TYPE_NFIELDS (type) > 0
4835 && TYPE_FIELD_ARTIFICIAL (type, 0)
4836 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
4837 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4838 fputs_unfiltered (" const", buf);
4839 }
4840 }
4841
4842 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4843 &length);
4844 ui_file_delete (buf);
4845
4846 if (cu->language == language_cplus)
4847 {
4848 char *cname
4849 = dwarf2_canonicalize_name (name, cu,
4850 &cu->objfile->objfile_obstack);
4851
4852 if (cname != NULL)
4853 name = cname;
4854 }
4855 }
4856 }
4857
4858 return name;
4859 }
4860
4861 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4862 If scope qualifiers are appropriate they will be added. The result
4863 will be allocated on the objfile_obstack, or NULL if the DIE does
4864 not have a name. NAME may either be from a previous call to
4865 dwarf2_name or NULL.
4866
4867 The output string will be canonicalized (if C++/Java). */
4868
4869 static const char *
4870 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4871 {
4872 return dwarf2_compute_name (name, die, cu, 0);
4873 }
4874
4875 /* Construct a physname for the given DIE in CU. NAME may either be
4876 from a previous call to dwarf2_name or NULL. The result will be
4877 allocated on the objfile_objstack or NULL if the DIE does not have a
4878 name.
4879
4880 The output string will be canonicalized (if C++/Java). */
4881
4882 static const char *
4883 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4884 {
4885 return dwarf2_compute_name (name, die, cu, 1);
4886 }
4887
4888 /* Read the import statement specified by the given die and record it. */
4889
4890 static void
4891 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4892 {
4893 struct attribute *import_attr;
4894 struct die_info *imported_die;
4895 struct dwarf2_cu *imported_cu;
4896 const char *imported_name;
4897 const char *imported_name_prefix;
4898 const char *canonical_name;
4899 const char *import_alias;
4900 const char *imported_declaration = NULL;
4901 const char *import_prefix;
4902
4903 char *temp;
4904
4905 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4906 if (import_attr == NULL)
4907 {
4908 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4909 dwarf_tag_name (die->tag));
4910 return;
4911 }
4912
4913 imported_cu = cu;
4914 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4915 imported_name = dwarf2_name (imported_die, imported_cu);
4916 if (imported_name == NULL)
4917 {
4918 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4919
4920 The import in the following code:
4921 namespace A
4922 {
4923 typedef int B;
4924 }
4925
4926 int main ()
4927 {
4928 using A::B;
4929 B b;
4930 return b;
4931 }
4932
4933 ...
4934 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4935 <52> DW_AT_decl_file : 1
4936 <53> DW_AT_decl_line : 6
4937 <54> DW_AT_import : <0x75>
4938 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4939 <59> DW_AT_name : B
4940 <5b> DW_AT_decl_file : 1
4941 <5c> DW_AT_decl_line : 2
4942 <5d> DW_AT_type : <0x6e>
4943 ...
4944 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4945 <76> DW_AT_byte_size : 4
4946 <77> DW_AT_encoding : 5 (signed)
4947
4948 imports the wrong die ( 0x75 instead of 0x58 ).
4949 This case will be ignored until the gcc bug is fixed. */
4950 return;
4951 }
4952
4953 /* Figure out the local name after import. */
4954 import_alias = dwarf2_name (die, cu);
4955
4956 /* Figure out where the statement is being imported to. */
4957 import_prefix = determine_prefix (die, cu);
4958
4959 /* Figure out what the scope of the imported die is and prepend it
4960 to the name of the imported die. */
4961 imported_name_prefix = determine_prefix (imported_die, imported_cu);
4962
4963 if (imported_die->tag != DW_TAG_namespace
4964 && imported_die->tag != DW_TAG_module)
4965 {
4966 imported_declaration = imported_name;
4967 canonical_name = imported_name_prefix;
4968 }
4969 else if (strlen (imported_name_prefix) > 0)
4970 {
4971 temp = alloca (strlen (imported_name_prefix)
4972 + 2 + strlen (imported_name) + 1);
4973 strcpy (temp, imported_name_prefix);
4974 strcat (temp, "::");
4975 strcat (temp, imported_name);
4976 canonical_name = temp;
4977 }
4978 else
4979 canonical_name = imported_name;
4980
4981 cp_add_using_directive (import_prefix,
4982 canonical_name,
4983 import_alias,
4984 imported_declaration,
4985 &cu->objfile->objfile_obstack);
4986 }
4987
4988 static void
4989 initialize_cu_func_list (struct dwarf2_cu *cu)
4990 {
4991 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
4992 }
4993
4994 static void
4995 free_cu_line_header (void *arg)
4996 {
4997 struct dwarf2_cu *cu = arg;
4998
4999 free_line_header (cu->line_header);
5000 cu->line_header = NULL;
5001 }
5002
5003 static void
5004 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5005 char **name, char **comp_dir)
5006 {
5007 struct attribute *attr;
5008
5009 *name = NULL;
5010 *comp_dir = NULL;
5011
5012 /* Find the filename. Do not use dwarf2_name here, since the filename
5013 is not a source language identifier. */
5014 attr = dwarf2_attr (die, DW_AT_name, cu);
5015 if (attr)
5016 {
5017 *name = DW_STRING (attr);
5018 }
5019
5020 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5021 if (attr)
5022 *comp_dir = DW_STRING (attr);
5023 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5024 {
5025 *comp_dir = ldirname (*name);
5026 if (*comp_dir != NULL)
5027 make_cleanup (xfree, *comp_dir);
5028 }
5029 if (*comp_dir != NULL)
5030 {
5031 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5032 directory, get rid of it. */
5033 char *cp = strchr (*comp_dir, ':');
5034
5035 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5036 *comp_dir = cp + 1;
5037 }
5038
5039 if (*name == NULL)
5040 *name = "<unknown>";
5041 }
5042
5043 static void
5044 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5045 {
5046 struct objfile *objfile = cu->objfile;
5047 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5048 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5049 CORE_ADDR highpc = ((CORE_ADDR) 0);
5050 struct attribute *attr;
5051 char *name = NULL;
5052 char *comp_dir = NULL;
5053 struct die_info *child_die;
5054 bfd *abfd = objfile->obfd;
5055 struct line_header *line_header = 0;
5056 CORE_ADDR baseaddr;
5057
5058 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5059
5060 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5061
5062 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5063 from finish_block. */
5064 if (lowpc == ((CORE_ADDR) -1))
5065 lowpc = highpc;
5066 lowpc += baseaddr;
5067 highpc += baseaddr;
5068
5069 find_file_and_directory (die, cu, &name, &comp_dir);
5070
5071 attr = dwarf2_attr (die, DW_AT_language, cu);
5072 if (attr)
5073 {
5074 set_cu_language (DW_UNSND (attr), cu);
5075 }
5076
5077 attr = dwarf2_attr (die, DW_AT_producer, cu);
5078 if (attr)
5079 cu->producer = DW_STRING (attr);
5080
5081 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5082 standardised yet. As a workaround for the language detection we fall
5083 back to the DW_AT_producer string. */
5084 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5085 cu->language = language_opencl;
5086
5087 /* We assume that we're processing GCC output. */
5088 processing_gcc_compilation = 2;
5089
5090 processing_has_namespace_info = 0;
5091
5092 start_symtab (name, comp_dir, lowpc);
5093 record_debugformat ("DWARF 2");
5094 record_producer (cu->producer);
5095
5096 initialize_cu_func_list (cu);
5097
5098 /* Decode line number information if present. We do this before
5099 processing child DIEs, so that the line header table is available
5100 for DW_AT_decl_file. */
5101 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5102 if (attr)
5103 {
5104 unsigned int line_offset = DW_UNSND (attr);
5105 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5106 if (line_header)
5107 {
5108 cu->line_header = line_header;
5109 make_cleanup (free_cu_line_header, cu);
5110 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5111 }
5112 }
5113
5114 /* Process all dies in compilation unit. */
5115 if (die->child != NULL)
5116 {
5117 child_die = die->child;
5118 while (child_die && child_die->tag)
5119 {
5120 process_die (child_die, cu);
5121 child_die = sibling_die (child_die);
5122 }
5123 }
5124
5125 /* Decode macro information, if present. Dwarf 2 macro information
5126 refers to information in the line number info statement program
5127 header, so we can only read it if we've read the header
5128 successfully. */
5129 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5130 if (attr && line_header)
5131 {
5132 unsigned int macro_offset = DW_UNSND (attr);
5133
5134 dwarf_decode_macros (line_header, macro_offset,
5135 comp_dir, abfd, cu);
5136 }
5137 do_cleanups (back_to);
5138 }
5139
5140 /* For TUs we want to skip the first top level sibling if it's not the
5141 actual type being defined by this TU. In this case the first top
5142 level sibling is there to provide context only. */
5143
5144 static void
5145 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5146 {
5147 struct objfile *objfile = cu->objfile;
5148 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5149 CORE_ADDR lowpc;
5150 struct attribute *attr;
5151 char *name = NULL;
5152 char *comp_dir = NULL;
5153 struct die_info *child_die;
5154 bfd *abfd = objfile->obfd;
5155
5156 /* start_symtab needs a low pc, but we don't really have one.
5157 Do what read_file_scope would do in the absence of such info. */
5158 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5159
5160 /* Find the filename. Do not use dwarf2_name here, since the filename
5161 is not a source language identifier. */
5162 attr = dwarf2_attr (die, DW_AT_name, cu);
5163 if (attr)
5164 name = DW_STRING (attr);
5165
5166 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5167 if (attr)
5168 comp_dir = DW_STRING (attr);
5169 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5170 {
5171 comp_dir = ldirname (name);
5172 if (comp_dir != NULL)
5173 make_cleanup (xfree, comp_dir);
5174 }
5175
5176 if (name == NULL)
5177 name = "<unknown>";
5178
5179 attr = dwarf2_attr (die, DW_AT_language, cu);
5180 if (attr)
5181 set_cu_language (DW_UNSND (attr), cu);
5182
5183 /* This isn't technically needed today. It is done for symmetry
5184 with read_file_scope. */
5185 attr = dwarf2_attr (die, DW_AT_producer, cu);
5186 if (attr)
5187 cu->producer = DW_STRING (attr);
5188
5189 /* We assume that we're processing GCC output. */
5190 processing_gcc_compilation = 2;
5191
5192 processing_has_namespace_info = 0;
5193
5194 start_symtab (name, comp_dir, lowpc);
5195 record_debugformat ("DWARF 2");
5196 record_producer (cu->producer);
5197
5198 /* Process the dies in the type unit. */
5199 if (die->child == NULL)
5200 {
5201 dump_die_for_error (die);
5202 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5203 bfd_get_filename (abfd));
5204 }
5205
5206 child_die = die->child;
5207
5208 while (child_die && child_die->tag)
5209 {
5210 process_die (child_die, cu);
5211
5212 child_die = sibling_die (child_die);
5213 }
5214
5215 do_cleanups (back_to);
5216 }
5217
5218 static void
5219 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5220 struct dwarf2_cu *cu)
5221 {
5222 struct function_range *thisfn;
5223
5224 thisfn = (struct function_range *)
5225 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5226 thisfn->name = name;
5227 thisfn->lowpc = lowpc;
5228 thisfn->highpc = highpc;
5229 thisfn->seen_line = 0;
5230 thisfn->next = NULL;
5231
5232 if (cu->last_fn == NULL)
5233 cu->first_fn = thisfn;
5234 else
5235 cu->last_fn->next = thisfn;
5236
5237 cu->last_fn = thisfn;
5238 }
5239
5240 /* qsort helper for inherit_abstract_dies. */
5241
5242 static int
5243 unsigned_int_compar (const void *ap, const void *bp)
5244 {
5245 unsigned int a = *(unsigned int *) ap;
5246 unsigned int b = *(unsigned int *) bp;
5247
5248 return (a > b) - (b > a);
5249 }
5250
5251 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5252 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5253 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5254
5255 static void
5256 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5257 {
5258 struct die_info *child_die;
5259 unsigned die_children_count;
5260 /* CU offsets which were referenced by children of the current DIE. */
5261 unsigned *offsets;
5262 unsigned *offsets_end, *offsetp;
5263 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5264 struct die_info *origin_die;
5265 /* Iterator of the ORIGIN_DIE children. */
5266 struct die_info *origin_child_die;
5267 struct cleanup *cleanups;
5268 struct attribute *attr;
5269 struct dwarf2_cu *origin_cu;
5270 struct pending **origin_previous_list_in_scope;
5271
5272 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5273 if (!attr)
5274 return;
5275
5276 /* Note that following die references may follow to a die in a
5277 different cu. */
5278
5279 origin_cu = cu;
5280 origin_die = follow_die_ref (die, attr, &origin_cu);
5281
5282 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5283 symbols in. */
5284 origin_previous_list_in_scope = origin_cu->list_in_scope;
5285 origin_cu->list_in_scope = cu->list_in_scope;
5286
5287 if (die->tag != origin_die->tag
5288 && !(die->tag == DW_TAG_inlined_subroutine
5289 && origin_die->tag == DW_TAG_subprogram))
5290 complaint (&symfile_complaints,
5291 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5292 die->offset, origin_die->offset);
5293
5294 child_die = die->child;
5295 die_children_count = 0;
5296 while (child_die && child_die->tag)
5297 {
5298 child_die = sibling_die (child_die);
5299 die_children_count++;
5300 }
5301 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5302 cleanups = make_cleanup (xfree, offsets);
5303
5304 offsets_end = offsets;
5305 child_die = die->child;
5306 while (child_die && child_die->tag)
5307 {
5308 /* For each CHILD_DIE, find the corresponding child of
5309 ORIGIN_DIE. If there is more than one layer of
5310 DW_AT_abstract_origin, follow them all; there shouldn't be,
5311 but GCC versions at least through 4.4 generate this (GCC PR
5312 40573). */
5313 struct die_info *child_origin_die = child_die;
5314 struct dwarf2_cu *child_origin_cu = cu;
5315
5316 while (1)
5317 {
5318 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5319 child_origin_cu);
5320 if (attr == NULL)
5321 break;
5322 child_origin_die = follow_die_ref (child_origin_die, attr,
5323 &child_origin_cu);
5324 }
5325
5326 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5327 counterpart may exist. */
5328 if (child_origin_die != child_die)
5329 {
5330 if (child_die->tag != child_origin_die->tag
5331 && !(child_die->tag == DW_TAG_inlined_subroutine
5332 && child_origin_die->tag == DW_TAG_subprogram))
5333 complaint (&symfile_complaints,
5334 _("Child DIE 0x%x and its abstract origin 0x%x have "
5335 "different tags"), child_die->offset,
5336 child_origin_die->offset);
5337 if (child_origin_die->parent != origin_die)
5338 complaint (&symfile_complaints,
5339 _("Child DIE 0x%x and its abstract origin 0x%x have "
5340 "different parents"), child_die->offset,
5341 child_origin_die->offset);
5342 else
5343 *offsets_end++ = child_origin_die->offset;
5344 }
5345 child_die = sibling_die (child_die);
5346 }
5347 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5348 unsigned_int_compar);
5349 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5350 if (offsetp[-1] == *offsetp)
5351 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5352 "to DIE 0x%x as their abstract origin"),
5353 die->offset, *offsetp);
5354
5355 offsetp = offsets;
5356 origin_child_die = origin_die->child;
5357 while (origin_child_die && origin_child_die->tag)
5358 {
5359 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5360 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5361 offsetp++;
5362 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5363 {
5364 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5365 process_die (origin_child_die, origin_cu);
5366 }
5367 origin_child_die = sibling_die (origin_child_die);
5368 }
5369 origin_cu->list_in_scope = origin_previous_list_in_scope;
5370
5371 do_cleanups (cleanups);
5372 }
5373
5374 static void
5375 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5376 {
5377 struct objfile *objfile = cu->objfile;
5378 struct context_stack *new;
5379 CORE_ADDR lowpc;
5380 CORE_ADDR highpc;
5381 struct die_info *child_die;
5382 struct attribute *attr, *call_line, *call_file;
5383 char *name;
5384 CORE_ADDR baseaddr;
5385 struct block *block;
5386 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5387 VEC (symbolp) *template_args = NULL;
5388 struct template_symbol *templ_func = NULL;
5389
5390 if (inlined_func)
5391 {
5392 /* If we do not have call site information, we can't show the
5393 caller of this inlined function. That's too confusing, so
5394 only use the scope for local variables. */
5395 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5396 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5397 if (call_line == NULL || call_file == NULL)
5398 {
5399 read_lexical_block_scope (die, cu);
5400 return;
5401 }
5402 }
5403
5404 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5405
5406 name = dwarf2_name (die, cu);
5407
5408 /* Ignore functions with missing or empty names. These are actually
5409 illegal according to the DWARF standard. */
5410 if (name == NULL)
5411 {
5412 complaint (&symfile_complaints,
5413 _("missing name for subprogram DIE at %d"), die->offset);
5414 return;
5415 }
5416
5417 /* Ignore functions with missing or invalid low and high pc attributes. */
5418 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5419 {
5420 attr = dwarf2_attr (die, DW_AT_external, cu);
5421 if (!attr || !DW_UNSND (attr))
5422 complaint (&symfile_complaints,
5423 _("cannot get low and high bounds for subprogram DIE at %d"),
5424 die->offset);
5425 return;
5426 }
5427
5428 lowpc += baseaddr;
5429 highpc += baseaddr;
5430
5431 /* Record the function range for dwarf_decode_lines. */
5432 add_to_cu_func_list (name, lowpc, highpc, cu);
5433
5434 /* If we have any template arguments, then we must allocate a
5435 different sort of symbol. */
5436 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5437 {
5438 if (child_die->tag == DW_TAG_template_type_param
5439 || child_die->tag == DW_TAG_template_value_param)
5440 {
5441 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5442 struct template_symbol);
5443 templ_func->base.is_cplus_template_function = 1;
5444 break;
5445 }
5446 }
5447
5448 new = push_context (0, lowpc);
5449 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5450 (struct symbol *) templ_func);
5451
5452 /* If there is a location expression for DW_AT_frame_base, record
5453 it. */
5454 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5455 if (attr)
5456 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5457 expression is being recorded directly in the function's symbol
5458 and not in a separate frame-base object. I guess this hack is
5459 to avoid adding some sort of frame-base adjunct/annex to the
5460 function's symbol :-(. The problem with doing this is that it
5461 results in a function symbol with a location expression that
5462 has nothing to do with the location of the function, ouch! The
5463 relationship should be: a function's symbol has-a frame base; a
5464 frame-base has-a location expression. */
5465 dwarf2_symbol_mark_computed (attr, new->name, cu);
5466
5467 cu->list_in_scope = &local_symbols;
5468
5469 if (die->child != NULL)
5470 {
5471 child_die = die->child;
5472 while (child_die && child_die->tag)
5473 {
5474 if (child_die->tag == DW_TAG_template_type_param
5475 || child_die->tag == DW_TAG_template_value_param)
5476 {
5477 struct symbol *arg = new_symbol (child_die, NULL, cu);
5478
5479 if (arg != NULL)
5480 VEC_safe_push (symbolp, template_args, arg);
5481 }
5482 else
5483 process_die (child_die, cu);
5484 child_die = sibling_die (child_die);
5485 }
5486 }
5487
5488 inherit_abstract_dies (die, cu);
5489
5490 /* If we have a DW_AT_specification, we might need to import using
5491 directives from the context of the specification DIE. See the
5492 comment in determine_prefix. */
5493 if (cu->language == language_cplus
5494 && dwarf2_attr (die, DW_AT_specification, cu))
5495 {
5496 struct dwarf2_cu *spec_cu = cu;
5497 struct die_info *spec_die = die_specification (die, &spec_cu);
5498
5499 while (spec_die)
5500 {
5501 child_die = spec_die->child;
5502 while (child_die && child_die->tag)
5503 {
5504 if (child_die->tag == DW_TAG_imported_module)
5505 process_die (child_die, spec_cu);
5506 child_die = sibling_die (child_die);
5507 }
5508
5509 /* In some cases, GCC generates specification DIEs that
5510 themselves contain DW_AT_specification attributes. */
5511 spec_die = die_specification (spec_die, &spec_cu);
5512 }
5513 }
5514
5515 new = pop_context ();
5516 /* Make a block for the local symbols within. */
5517 block = finish_block (new->name, &local_symbols, new->old_blocks,
5518 lowpc, highpc, objfile);
5519
5520 /* For C++, set the block's scope. */
5521 if (cu->language == language_cplus || cu->language == language_fortran)
5522 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5523 determine_prefix (die, cu),
5524 processing_has_namespace_info);
5525
5526 /* If we have address ranges, record them. */
5527 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5528
5529 /* Attach template arguments to function. */
5530 if (! VEC_empty (symbolp, template_args))
5531 {
5532 gdb_assert (templ_func != NULL);
5533
5534 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5535 templ_func->template_arguments
5536 = obstack_alloc (&objfile->objfile_obstack,
5537 (templ_func->n_template_arguments
5538 * sizeof (struct symbol *)));
5539 memcpy (templ_func->template_arguments,
5540 VEC_address (symbolp, template_args),
5541 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5542 VEC_free (symbolp, template_args);
5543 }
5544
5545 /* In C++, we can have functions nested inside functions (e.g., when
5546 a function declares a class that has methods). This means that
5547 when we finish processing a function scope, we may need to go
5548 back to building a containing block's symbol lists. */
5549 local_symbols = new->locals;
5550 param_symbols = new->params;
5551 using_directives = new->using_directives;
5552
5553 /* If we've finished processing a top-level function, subsequent
5554 symbols go in the file symbol list. */
5555 if (outermost_context_p ())
5556 cu->list_in_scope = &file_symbols;
5557 }
5558
5559 /* Process all the DIES contained within a lexical block scope. Start
5560 a new scope, process the dies, and then close the scope. */
5561
5562 static void
5563 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5564 {
5565 struct objfile *objfile = cu->objfile;
5566 struct context_stack *new;
5567 CORE_ADDR lowpc, highpc;
5568 struct die_info *child_die;
5569 CORE_ADDR baseaddr;
5570
5571 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5572
5573 /* Ignore blocks with missing or invalid low and high pc attributes. */
5574 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5575 as multiple lexical blocks? Handling children in a sane way would
5576 be nasty. Might be easier to properly extend generic blocks to
5577 describe ranges. */
5578 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5579 return;
5580 lowpc += baseaddr;
5581 highpc += baseaddr;
5582
5583 push_context (0, lowpc);
5584 if (die->child != NULL)
5585 {
5586 child_die = die->child;
5587 while (child_die && child_die->tag)
5588 {
5589 process_die (child_die, cu);
5590 child_die = sibling_die (child_die);
5591 }
5592 }
5593 new = pop_context ();
5594
5595 if (local_symbols != NULL || using_directives != NULL)
5596 {
5597 struct block *block
5598 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5599 highpc, objfile);
5600
5601 /* Note that recording ranges after traversing children, as we
5602 do here, means that recording a parent's ranges entails
5603 walking across all its children's ranges as they appear in
5604 the address map, which is quadratic behavior.
5605
5606 It would be nicer to record the parent's ranges before
5607 traversing its children, simply overriding whatever you find
5608 there. But since we don't even decide whether to create a
5609 block until after we've traversed its children, that's hard
5610 to do. */
5611 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5612 }
5613 local_symbols = new->locals;
5614 using_directives = new->using_directives;
5615 }
5616
5617 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5618 Return 1 if the attributes are present and valid, otherwise, return 0.
5619 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
5620
5621 static int
5622 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5623 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5624 struct partial_symtab *ranges_pst)
5625 {
5626 struct objfile *objfile = cu->objfile;
5627 struct comp_unit_head *cu_header = &cu->header;
5628 bfd *obfd = objfile->obfd;
5629 unsigned int addr_size = cu_header->addr_size;
5630 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5631 /* Base address selection entry. */
5632 CORE_ADDR base;
5633 int found_base;
5634 unsigned int dummy;
5635 gdb_byte *buffer;
5636 CORE_ADDR marker;
5637 int low_set;
5638 CORE_ADDR low = 0;
5639 CORE_ADDR high = 0;
5640 CORE_ADDR baseaddr;
5641
5642 found_base = cu->base_known;
5643 base = cu->base_address;
5644
5645 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5646 if (offset >= dwarf2_per_objfile->ranges.size)
5647 {
5648 complaint (&symfile_complaints,
5649 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5650 offset);
5651 return 0;
5652 }
5653 buffer = dwarf2_per_objfile->ranges.buffer + offset;
5654
5655 /* Read in the largest possible address. */
5656 marker = read_address (obfd, buffer, cu, &dummy);
5657 if ((marker & mask) == mask)
5658 {
5659 /* If we found the largest possible address, then
5660 read the base address. */
5661 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5662 buffer += 2 * addr_size;
5663 offset += 2 * addr_size;
5664 found_base = 1;
5665 }
5666
5667 low_set = 0;
5668
5669 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5670
5671 while (1)
5672 {
5673 CORE_ADDR range_beginning, range_end;
5674
5675 range_beginning = read_address (obfd, buffer, cu, &dummy);
5676 buffer += addr_size;
5677 range_end = read_address (obfd, buffer, cu, &dummy);
5678 buffer += addr_size;
5679 offset += 2 * addr_size;
5680
5681 /* An end of list marker is a pair of zero addresses. */
5682 if (range_beginning == 0 && range_end == 0)
5683 /* Found the end of list entry. */
5684 break;
5685
5686 /* Each base address selection entry is a pair of 2 values.
5687 The first is the largest possible address, the second is
5688 the base address. Check for a base address here. */
5689 if ((range_beginning & mask) == mask)
5690 {
5691 /* If we found the largest possible address, then
5692 read the base address. */
5693 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5694 found_base = 1;
5695 continue;
5696 }
5697
5698 if (!found_base)
5699 {
5700 /* We have no valid base address for the ranges
5701 data. */
5702 complaint (&symfile_complaints,
5703 _("Invalid .debug_ranges data (no base address)"));
5704 return 0;
5705 }
5706
5707 range_beginning += base;
5708 range_end += base;
5709
5710 if (ranges_pst != NULL && range_beginning < range_end)
5711 addrmap_set_empty (objfile->psymtabs_addrmap,
5712 range_beginning + baseaddr, range_end - 1 + baseaddr,
5713 ranges_pst);
5714
5715 /* FIXME: This is recording everything as a low-high
5716 segment of consecutive addresses. We should have a
5717 data structure for discontiguous block ranges
5718 instead. */
5719 if (! low_set)
5720 {
5721 low = range_beginning;
5722 high = range_end;
5723 low_set = 1;
5724 }
5725 else
5726 {
5727 if (range_beginning < low)
5728 low = range_beginning;
5729 if (range_end > high)
5730 high = range_end;
5731 }
5732 }
5733
5734 if (! low_set)
5735 /* If the first entry is an end-of-list marker, the range
5736 describes an empty scope, i.e. no instructions. */
5737 return 0;
5738
5739 if (low_return)
5740 *low_return = low;
5741 if (high_return)
5742 *high_return = high;
5743 return 1;
5744 }
5745
5746 /* Get low and high pc attributes from a die. Return 1 if the attributes
5747 are present and valid, otherwise, return 0. Return -1 if the range is
5748 discontinuous, i.e. derived from DW_AT_ranges information. */
5749 static int
5750 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5751 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5752 struct partial_symtab *pst)
5753 {
5754 struct attribute *attr;
5755 CORE_ADDR low = 0;
5756 CORE_ADDR high = 0;
5757 int ret = 0;
5758
5759 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5760 if (attr)
5761 {
5762 high = DW_ADDR (attr);
5763 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5764 if (attr)
5765 low = DW_ADDR (attr);
5766 else
5767 /* Found high w/o low attribute. */
5768 return 0;
5769
5770 /* Found consecutive range of addresses. */
5771 ret = 1;
5772 }
5773 else
5774 {
5775 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5776 if (attr != NULL)
5777 {
5778 /* Value of the DW_AT_ranges attribute is the offset in the
5779 .debug_ranges section. */
5780 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5781 return 0;
5782 /* Found discontinuous range of addresses. */
5783 ret = -1;
5784 }
5785 }
5786
5787 if (high < low)
5788 return 0;
5789
5790 /* When using the GNU linker, .gnu.linkonce. sections are used to
5791 eliminate duplicate copies of functions and vtables and such.
5792 The linker will arbitrarily choose one and discard the others.
5793 The AT_*_pc values for such functions refer to local labels in
5794 these sections. If the section from that file was discarded, the
5795 labels are not in the output, so the relocs get a value of 0.
5796 If this is a discarded function, mark the pc bounds as invalid,
5797 so that GDB will ignore it. */
5798 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5799 return 0;
5800
5801 *lowpc = low;
5802 *highpc = high;
5803 return ret;
5804 }
5805
5806 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5807 its low and high PC addresses. Do nothing if these addresses could not
5808 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5809 and HIGHPC to the high address if greater than HIGHPC. */
5810
5811 static void
5812 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5813 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5814 struct dwarf2_cu *cu)
5815 {
5816 CORE_ADDR low, high;
5817 struct die_info *child = die->child;
5818
5819 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5820 {
5821 *lowpc = min (*lowpc, low);
5822 *highpc = max (*highpc, high);
5823 }
5824
5825 /* If the language does not allow nested subprograms (either inside
5826 subprograms or lexical blocks), we're done. */
5827 if (cu->language != language_ada)
5828 return;
5829
5830 /* Check all the children of the given DIE. If it contains nested
5831 subprograms, then check their pc bounds. Likewise, we need to
5832 check lexical blocks as well, as they may also contain subprogram
5833 definitions. */
5834 while (child && child->tag)
5835 {
5836 if (child->tag == DW_TAG_subprogram
5837 || child->tag == DW_TAG_lexical_block)
5838 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5839 child = sibling_die (child);
5840 }
5841 }
5842
5843 /* Get the low and high pc's represented by the scope DIE, and store
5844 them in *LOWPC and *HIGHPC. If the correct values can't be
5845 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5846
5847 static void
5848 get_scope_pc_bounds (struct die_info *die,
5849 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5850 struct dwarf2_cu *cu)
5851 {
5852 CORE_ADDR best_low = (CORE_ADDR) -1;
5853 CORE_ADDR best_high = (CORE_ADDR) 0;
5854 CORE_ADDR current_low, current_high;
5855
5856 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5857 {
5858 best_low = current_low;
5859 best_high = current_high;
5860 }
5861 else
5862 {
5863 struct die_info *child = die->child;
5864
5865 while (child && child->tag)
5866 {
5867 switch (child->tag) {
5868 case DW_TAG_subprogram:
5869 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5870 break;
5871 case DW_TAG_namespace:
5872 case DW_TAG_module:
5873 /* FIXME: carlton/2004-01-16: Should we do this for
5874 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5875 that current GCC's always emit the DIEs corresponding
5876 to definitions of methods of classes as children of a
5877 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5878 the DIEs giving the declarations, which could be
5879 anywhere). But I don't see any reason why the
5880 standards says that they have to be there. */
5881 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5882
5883 if (current_low != ((CORE_ADDR) -1))
5884 {
5885 best_low = min (best_low, current_low);
5886 best_high = max (best_high, current_high);
5887 }
5888 break;
5889 default:
5890 /* Ignore. */
5891 break;
5892 }
5893
5894 child = sibling_die (child);
5895 }
5896 }
5897
5898 *lowpc = best_low;
5899 *highpc = best_high;
5900 }
5901
5902 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5903 in DIE. */
5904 static void
5905 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5906 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5907 {
5908 struct attribute *attr;
5909
5910 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5911 if (attr)
5912 {
5913 CORE_ADDR high = DW_ADDR (attr);
5914
5915 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5916 if (attr)
5917 {
5918 CORE_ADDR low = DW_ADDR (attr);
5919
5920 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5921 }
5922 }
5923
5924 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5925 if (attr)
5926 {
5927 bfd *obfd = cu->objfile->obfd;
5928
5929 /* The value of the DW_AT_ranges attribute is the offset of the
5930 address range list in the .debug_ranges section. */
5931 unsigned long offset = DW_UNSND (attr);
5932 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5933
5934 /* For some target architectures, but not others, the
5935 read_address function sign-extends the addresses it returns.
5936 To recognize base address selection entries, we need a
5937 mask. */
5938 unsigned int addr_size = cu->header.addr_size;
5939 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5940
5941 /* The base address, to which the next pair is relative. Note
5942 that this 'base' is a DWARF concept: most entries in a range
5943 list are relative, to reduce the number of relocs against the
5944 debugging information. This is separate from this function's
5945 'baseaddr' argument, which GDB uses to relocate debugging
5946 information from a shared library based on the address at
5947 which the library was loaded. */
5948 CORE_ADDR base = cu->base_address;
5949 int base_known = cu->base_known;
5950
5951 gdb_assert (dwarf2_per_objfile->ranges.readin);
5952 if (offset >= dwarf2_per_objfile->ranges.size)
5953 {
5954 complaint (&symfile_complaints,
5955 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5956 offset);
5957 return;
5958 }
5959
5960 for (;;)
5961 {
5962 unsigned int bytes_read;
5963 CORE_ADDR start, end;
5964
5965 start = read_address (obfd, buffer, cu, &bytes_read);
5966 buffer += bytes_read;
5967 end = read_address (obfd, buffer, cu, &bytes_read);
5968 buffer += bytes_read;
5969
5970 /* Did we find the end of the range list? */
5971 if (start == 0 && end == 0)
5972 break;
5973
5974 /* Did we find a base address selection entry? */
5975 else if ((start & base_select_mask) == base_select_mask)
5976 {
5977 base = end;
5978 base_known = 1;
5979 }
5980
5981 /* We found an ordinary address range. */
5982 else
5983 {
5984 if (!base_known)
5985 {
5986 complaint (&symfile_complaints,
5987 _("Invalid .debug_ranges data (no base address)"));
5988 return;
5989 }
5990
5991 record_block_range (block,
5992 baseaddr + base + start,
5993 baseaddr + base + end - 1);
5994 }
5995 }
5996 }
5997 }
5998
5999 /* Add an aggregate field to the field list. */
6000
6001 static void
6002 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6003 struct dwarf2_cu *cu)
6004 {
6005 struct objfile *objfile = cu->objfile;
6006 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6007 struct nextfield *new_field;
6008 struct attribute *attr;
6009 struct field *fp;
6010 char *fieldname = "";
6011
6012 /* Allocate a new field list entry and link it in. */
6013 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6014 make_cleanup (xfree, new_field);
6015 memset (new_field, 0, sizeof (struct nextfield));
6016
6017 if (die->tag == DW_TAG_inheritance)
6018 {
6019 new_field->next = fip->baseclasses;
6020 fip->baseclasses = new_field;
6021 }
6022 else
6023 {
6024 new_field->next = fip->fields;
6025 fip->fields = new_field;
6026 }
6027 fip->nfields++;
6028
6029 /* Handle accessibility and virtuality of field.
6030 The default accessibility for members is public, the default
6031 accessibility for inheritance is private. */
6032 if (die->tag != DW_TAG_inheritance)
6033 new_field->accessibility = DW_ACCESS_public;
6034 else
6035 new_field->accessibility = DW_ACCESS_private;
6036 new_field->virtuality = DW_VIRTUALITY_none;
6037
6038 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6039 if (attr)
6040 new_field->accessibility = DW_UNSND (attr);
6041 if (new_field->accessibility != DW_ACCESS_public)
6042 fip->non_public_fields = 1;
6043 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6044 if (attr)
6045 new_field->virtuality = DW_UNSND (attr);
6046
6047 fp = &new_field->field;
6048
6049 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6050 {
6051 /* Data member other than a C++ static data member. */
6052
6053 /* Get type of field. */
6054 fp->type = die_type (die, cu);
6055
6056 SET_FIELD_BITPOS (*fp, 0);
6057
6058 /* Get bit size of field (zero if none). */
6059 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6060 if (attr)
6061 {
6062 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6063 }
6064 else
6065 {
6066 FIELD_BITSIZE (*fp) = 0;
6067 }
6068
6069 /* Get bit offset of field. */
6070 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6071 if (attr)
6072 {
6073 int byte_offset = 0;
6074
6075 if (attr_form_is_section_offset (attr))
6076 dwarf2_complex_location_expr_complaint ();
6077 else if (attr_form_is_constant (attr))
6078 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6079 else if (attr_form_is_block (attr))
6080 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6081 else
6082 dwarf2_complex_location_expr_complaint ();
6083
6084 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6085 }
6086 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6087 if (attr)
6088 {
6089 if (gdbarch_bits_big_endian (gdbarch))
6090 {
6091 /* For big endian bits, the DW_AT_bit_offset gives the
6092 additional bit offset from the MSB of the containing
6093 anonymous object to the MSB of the field. We don't
6094 have to do anything special since we don't need to
6095 know the size of the anonymous object. */
6096 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6097 }
6098 else
6099 {
6100 /* For little endian bits, compute the bit offset to the
6101 MSB of the anonymous object, subtract off the number of
6102 bits from the MSB of the field to the MSB of the
6103 object, and then subtract off the number of bits of
6104 the field itself. The result is the bit offset of
6105 the LSB of the field. */
6106 int anonymous_size;
6107 int bit_offset = DW_UNSND (attr);
6108
6109 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6110 if (attr)
6111 {
6112 /* The size of the anonymous object containing
6113 the bit field is explicit, so use the
6114 indicated size (in bytes). */
6115 anonymous_size = DW_UNSND (attr);
6116 }
6117 else
6118 {
6119 /* The size of the anonymous object containing
6120 the bit field must be inferred from the type
6121 attribute of the data member containing the
6122 bit field. */
6123 anonymous_size = TYPE_LENGTH (fp->type);
6124 }
6125 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6126 - bit_offset - FIELD_BITSIZE (*fp);
6127 }
6128 }
6129
6130 /* Get name of field. */
6131 fieldname = dwarf2_name (die, cu);
6132 if (fieldname == NULL)
6133 fieldname = "";
6134
6135 /* The name is already allocated along with this objfile, so we don't
6136 need to duplicate it for the type. */
6137 fp->name = fieldname;
6138
6139 /* Change accessibility for artificial fields (e.g. virtual table
6140 pointer or virtual base class pointer) to private. */
6141 if (dwarf2_attr (die, DW_AT_artificial, cu))
6142 {
6143 FIELD_ARTIFICIAL (*fp) = 1;
6144 new_field->accessibility = DW_ACCESS_private;
6145 fip->non_public_fields = 1;
6146 }
6147 }
6148 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6149 {
6150 /* C++ static member. */
6151
6152 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6153 is a declaration, but all versions of G++ as of this writing
6154 (so through at least 3.2.1) incorrectly generate
6155 DW_TAG_variable tags. */
6156
6157 char *physname;
6158
6159 /* Get name of field. */
6160 fieldname = dwarf2_name (die, cu);
6161 if (fieldname == NULL)
6162 return;
6163
6164 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6165 if (attr
6166 /* Only create a symbol if this is an external value.
6167 new_symbol checks this and puts the value in the global symbol
6168 table, which we want. If it is not external, new_symbol
6169 will try to put the value in cu->list_in_scope which is wrong. */
6170 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6171 {
6172 /* A static const member, not much different than an enum as far as
6173 we're concerned, except that we can support more types. */
6174 new_symbol (die, NULL, cu);
6175 }
6176
6177 /* Get physical name. */
6178 physname = (char *) dwarf2_physname (fieldname, die, cu);
6179
6180 /* The name is already allocated along with this objfile, so we don't
6181 need to duplicate it for the type. */
6182 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6183 FIELD_TYPE (*fp) = die_type (die, cu);
6184 FIELD_NAME (*fp) = fieldname;
6185 }
6186 else if (die->tag == DW_TAG_inheritance)
6187 {
6188 /* C++ base class field. */
6189 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6190 if (attr)
6191 {
6192 int byte_offset = 0;
6193
6194 if (attr_form_is_section_offset (attr))
6195 dwarf2_complex_location_expr_complaint ();
6196 else if (attr_form_is_constant (attr))
6197 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6198 else if (attr_form_is_block (attr))
6199 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6200 else
6201 dwarf2_complex_location_expr_complaint ();
6202
6203 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6204 }
6205 FIELD_BITSIZE (*fp) = 0;
6206 FIELD_TYPE (*fp) = die_type (die, cu);
6207 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6208 fip->nbaseclasses++;
6209 }
6210 }
6211
6212 /* Add a typedef defined in the scope of the FIP's class. */
6213
6214 static void
6215 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6216 struct dwarf2_cu *cu)
6217 {
6218 struct objfile *objfile = cu->objfile;
6219 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6220 struct typedef_field_list *new_field;
6221 struct attribute *attr;
6222 struct typedef_field *fp;
6223 char *fieldname = "";
6224
6225 /* Allocate a new field list entry and link it in. */
6226 new_field = xzalloc (sizeof (*new_field));
6227 make_cleanup (xfree, new_field);
6228
6229 gdb_assert (die->tag == DW_TAG_typedef);
6230
6231 fp = &new_field->field;
6232
6233 /* Get name of field. */
6234 fp->name = dwarf2_name (die, cu);
6235 if (fp->name == NULL)
6236 return;
6237
6238 fp->type = read_type_die (die, cu);
6239
6240 new_field->next = fip->typedef_field_list;
6241 fip->typedef_field_list = new_field;
6242 fip->typedef_field_list_count++;
6243 }
6244
6245 /* Create the vector of fields, and attach it to the type. */
6246
6247 static void
6248 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6249 struct dwarf2_cu *cu)
6250 {
6251 int nfields = fip->nfields;
6252
6253 /* Record the field count, allocate space for the array of fields,
6254 and create blank accessibility bitfields if necessary. */
6255 TYPE_NFIELDS (type) = nfields;
6256 TYPE_FIELDS (type) = (struct field *)
6257 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6258 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6259
6260 if (fip->non_public_fields && cu->language != language_ada)
6261 {
6262 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6263
6264 TYPE_FIELD_PRIVATE_BITS (type) =
6265 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6266 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6267
6268 TYPE_FIELD_PROTECTED_BITS (type) =
6269 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6270 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6271
6272 TYPE_FIELD_IGNORE_BITS (type) =
6273 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6274 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6275 }
6276
6277 /* If the type has baseclasses, allocate and clear a bit vector for
6278 TYPE_FIELD_VIRTUAL_BITS. */
6279 if (fip->nbaseclasses && cu->language != language_ada)
6280 {
6281 int num_bytes = B_BYTES (fip->nbaseclasses);
6282 unsigned char *pointer;
6283
6284 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6285 pointer = TYPE_ALLOC (type, num_bytes);
6286 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6287 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6288 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6289 }
6290
6291 /* Copy the saved-up fields into the field vector. Start from the head
6292 of the list, adding to the tail of the field array, so that they end
6293 up in the same order in the array in which they were added to the list. */
6294 while (nfields-- > 0)
6295 {
6296 struct nextfield *fieldp;
6297
6298 if (fip->fields)
6299 {
6300 fieldp = fip->fields;
6301 fip->fields = fieldp->next;
6302 }
6303 else
6304 {
6305 fieldp = fip->baseclasses;
6306 fip->baseclasses = fieldp->next;
6307 }
6308
6309 TYPE_FIELD (type, nfields) = fieldp->field;
6310 switch (fieldp->accessibility)
6311 {
6312 case DW_ACCESS_private:
6313 if (cu->language != language_ada)
6314 SET_TYPE_FIELD_PRIVATE (type, nfields);
6315 break;
6316
6317 case DW_ACCESS_protected:
6318 if (cu->language != language_ada)
6319 SET_TYPE_FIELD_PROTECTED (type, nfields);
6320 break;
6321
6322 case DW_ACCESS_public:
6323 break;
6324
6325 default:
6326 /* Unknown accessibility. Complain and treat it as public. */
6327 {
6328 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6329 fieldp->accessibility);
6330 }
6331 break;
6332 }
6333 if (nfields < fip->nbaseclasses)
6334 {
6335 switch (fieldp->virtuality)
6336 {
6337 case DW_VIRTUALITY_virtual:
6338 case DW_VIRTUALITY_pure_virtual:
6339 if (cu->language == language_ada)
6340 error ("unexpected virtuality in component of Ada type");
6341 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6342 break;
6343 }
6344 }
6345 }
6346 }
6347
6348 /* Add a member function to the proper fieldlist. */
6349
6350 static void
6351 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6352 struct type *type, struct dwarf2_cu *cu)
6353 {
6354 struct objfile *objfile = cu->objfile;
6355 struct attribute *attr;
6356 struct fnfieldlist *flp;
6357 int i;
6358 struct fn_field *fnp;
6359 char *fieldname;
6360 struct nextfnfield *new_fnfield;
6361 struct type *this_type;
6362
6363 if (cu->language == language_ada)
6364 error ("unexpected member function in Ada type");
6365
6366 /* Get name of member function. */
6367 fieldname = dwarf2_name (die, cu);
6368 if (fieldname == NULL)
6369 return;
6370
6371 /* Look up member function name in fieldlist. */
6372 for (i = 0; i < fip->nfnfields; i++)
6373 {
6374 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6375 break;
6376 }
6377
6378 /* Create new list element if necessary. */
6379 if (i < fip->nfnfields)
6380 flp = &fip->fnfieldlists[i];
6381 else
6382 {
6383 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6384 {
6385 fip->fnfieldlists = (struct fnfieldlist *)
6386 xrealloc (fip->fnfieldlists,
6387 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6388 * sizeof (struct fnfieldlist));
6389 if (fip->nfnfields == 0)
6390 make_cleanup (free_current_contents, &fip->fnfieldlists);
6391 }
6392 flp = &fip->fnfieldlists[fip->nfnfields];
6393 flp->name = fieldname;
6394 flp->length = 0;
6395 flp->head = NULL;
6396 i = fip->nfnfields++;
6397 }
6398
6399 /* Create a new member function field and chain it to the field list
6400 entry. */
6401 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6402 make_cleanup (xfree, new_fnfield);
6403 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6404 new_fnfield->next = flp->head;
6405 flp->head = new_fnfield;
6406 flp->length++;
6407
6408 /* Fill in the member function field info. */
6409 fnp = &new_fnfield->fnfield;
6410
6411 /* Delay processing of the physname until later. */
6412 if (cu->language == language_cplus || cu->language == language_java)
6413 {
6414 add_to_method_list (type, i, flp->length - 1, fieldname,
6415 die, cu);
6416 }
6417 else
6418 {
6419 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6420 fnp->physname = physname ? physname : "";
6421 }
6422
6423 fnp->type = alloc_type (objfile);
6424 this_type = read_type_die (die, cu);
6425 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6426 {
6427 int nparams = TYPE_NFIELDS (this_type);
6428
6429 /* TYPE is the domain of this method, and THIS_TYPE is the type
6430 of the method itself (TYPE_CODE_METHOD). */
6431 smash_to_method_type (fnp->type, type,
6432 TYPE_TARGET_TYPE (this_type),
6433 TYPE_FIELDS (this_type),
6434 TYPE_NFIELDS (this_type),
6435 TYPE_VARARGS (this_type));
6436
6437 /* Handle static member functions.
6438 Dwarf2 has no clean way to discern C++ static and non-static
6439 member functions. G++ helps GDB by marking the first
6440 parameter for non-static member functions (which is the
6441 this pointer) as artificial. We obtain this information
6442 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
6443 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6444 fnp->voffset = VOFFSET_STATIC;
6445 }
6446 else
6447 complaint (&symfile_complaints, _("member function type missing for '%s'"),
6448 dwarf2_full_name (fieldname, die, cu));
6449
6450 /* Get fcontext from DW_AT_containing_type if present. */
6451 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6452 fnp->fcontext = die_containing_type (die, cu);
6453
6454 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6455 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6456
6457 /* Get accessibility. */
6458 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6459 if (attr)
6460 {
6461 switch (DW_UNSND (attr))
6462 {
6463 case DW_ACCESS_private:
6464 fnp->is_private = 1;
6465 break;
6466 case DW_ACCESS_protected:
6467 fnp->is_protected = 1;
6468 break;
6469 }
6470 }
6471
6472 /* Check for artificial methods. */
6473 attr = dwarf2_attr (die, DW_AT_artificial, cu);
6474 if (attr && DW_UNSND (attr) != 0)
6475 fnp->is_artificial = 1;
6476
6477 /* Get index in virtual function table if it is a virtual member
6478 function. For older versions of GCC, this is an offset in the
6479 appropriate virtual table, as specified by DW_AT_containing_type.
6480 For everyone else, it is an expression to be evaluated relative
6481 to the object address. */
6482
6483 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6484 if (attr)
6485 {
6486 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6487 {
6488 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6489 {
6490 /* Old-style GCC. */
6491 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6492 }
6493 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6494 || (DW_BLOCK (attr)->size > 1
6495 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6496 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6497 {
6498 struct dwarf_block blk;
6499 int offset;
6500
6501 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6502 ? 1 : 2);
6503 blk.size = DW_BLOCK (attr)->size - offset;
6504 blk.data = DW_BLOCK (attr)->data + offset;
6505 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6506 if ((fnp->voffset % cu->header.addr_size) != 0)
6507 dwarf2_complex_location_expr_complaint ();
6508 else
6509 fnp->voffset /= cu->header.addr_size;
6510 fnp->voffset += 2;
6511 }
6512 else
6513 dwarf2_complex_location_expr_complaint ();
6514
6515 if (!fnp->fcontext)
6516 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6517 }
6518 else if (attr_form_is_section_offset (attr))
6519 {
6520 dwarf2_complex_location_expr_complaint ();
6521 }
6522 else
6523 {
6524 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6525 fieldname);
6526 }
6527 }
6528 else
6529 {
6530 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6531 if (attr && DW_UNSND (attr))
6532 {
6533 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6534 complaint (&symfile_complaints,
6535 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6536 fieldname, die->offset);
6537 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6538 TYPE_CPLUS_DYNAMIC (type) = 1;
6539 }
6540 }
6541 }
6542
6543 /* Create the vector of member function fields, and attach it to the type. */
6544
6545 static void
6546 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6547 struct dwarf2_cu *cu)
6548 {
6549 struct fnfieldlist *flp;
6550 int total_length = 0;
6551 int i;
6552
6553 if (cu->language == language_ada)
6554 error ("unexpected member functions in Ada type");
6555
6556 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6557 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6558 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6559
6560 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6561 {
6562 struct nextfnfield *nfp = flp->head;
6563 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6564 int k;
6565
6566 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6567 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6568 fn_flp->fn_fields = (struct fn_field *)
6569 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6570 for (k = flp->length; (k--, nfp); nfp = nfp->next)
6571 fn_flp->fn_fields[k] = nfp->fnfield;
6572
6573 total_length += flp->length;
6574 }
6575
6576 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6577 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6578 }
6579
6580 /* Returns non-zero if NAME is the name of a vtable member in CU's
6581 language, zero otherwise. */
6582 static int
6583 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6584 {
6585 static const char vptr[] = "_vptr";
6586 static const char vtable[] = "vtable";
6587
6588 /* Look for the C++ and Java forms of the vtable. */
6589 if ((cu->language == language_java
6590 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6591 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6592 && is_cplus_marker (name[sizeof (vptr) - 1])))
6593 return 1;
6594
6595 return 0;
6596 }
6597
6598 /* GCC outputs unnamed structures that are really pointers to member
6599 functions, with the ABI-specified layout. If TYPE describes
6600 such a structure, smash it into a member function type.
6601
6602 GCC shouldn't do this; it should just output pointer to member DIEs.
6603 This is GCC PR debug/28767. */
6604
6605 static void
6606 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6607 {
6608 struct type *pfn_type, *domain_type, *new_type;
6609
6610 /* Check for a structure with no name and two children. */
6611 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6612 return;
6613
6614 /* Check for __pfn and __delta members. */
6615 if (TYPE_FIELD_NAME (type, 0) == NULL
6616 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6617 || TYPE_FIELD_NAME (type, 1) == NULL
6618 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6619 return;
6620
6621 /* Find the type of the method. */
6622 pfn_type = TYPE_FIELD_TYPE (type, 0);
6623 if (pfn_type == NULL
6624 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6625 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6626 return;
6627
6628 /* Look for the "this" argument. */
6629 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6630 if (TYPE_NFIELDS (pfn_type) == 0
6631 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6632 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6633 return;
6634
6635 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6636 new_type = alloc_type (objfile);
6637 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6638 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6639 TYPE_VARARGS (pfn_type));
6640 smash_to_methodptr_type (type, new_type);
6641 }
6642
6643 /* Called when we find the DIE that starts a structure or union scope
6644 (definition) to create a type for the structure or union. Fill in
6645 the type's name and general properties; the members will not be
6646 processed until process_structure_type.
6647
6648 NOTE: we need to call these functions regardless of whether or not the
6649 DIE has a DW_AT_name attribute, since it might be an anonymous
6650 structure or union. This gets the type entered into our set of
6651 user defined types.
6652
6653 However, if the structure is incomplete (an opaque struct/union)
6654 then suppress creating a symbol table entry for it since gdb only
6655 wants to find the one with the complete definition. Note that if
6656 it is complete, we just call new_symbol, which does it's own
6657 checking about whether the struct/union is anonymous or not (and
6658 suppresses creating a symbol table entry itself). */
6659
6660 static struct type *
6661 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6662 {
6663 struct objfile *objfile = cu->objfile;
6664 struct type *type;
6665 struct attribute *attr;
6666 char *name;
6667
6668 /* If the definition of this type lives in .debug_types, read that type.
6669 Don't follow DW_AT_specification though, that will take us back up
6670 the chain and we want to go down. */
6671 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6672 if (attr)
6673 {
6674 struct dwarf2_cu *type_cu = cu;
6675 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6676
6677 /* We could just recurse on read_structure_type, but we need to call
6678 get_die_type to ensure only one type for this DIE is created.
6679 This is important, for example, because for c++ classes we need
6680 TYPE_NAME set which is only done by new_symbol. Blech. */
6681 type = read_type_die (type_die, type_cu);
6682
6683 /* TYPE_CU may not be the same as CU.
6684 Ensure TYPE is recorded in CU's type_hash table. */
6685 return set_die_type (die, type, cu);
6686 }
6687
6688 type = alloc_type (objfile);
6689 INIT_CPLUS_SPECIFIC (type);
6690
6691 name = dwarf2_name (die, cu);
6692 if (name != NULL)
6693 {
6694 if (cu->language == language_cplus
6695 || cu->language == language_java)
6696 {
6697 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6698
6699 /* dwarf2_full_name might have already finished building the DIE's
6700 type. If so, there is no need to continue. */
6701 if (get_die_type (die, cu) != NULL)
6702 return get_die_type (die, cu);
6703
6704 TYPE_TAG_NAME (type) = full_name;
6705 if (die->tag == DW_TAG_structure_type
6706 || die->tag == DW_TAG_class_type)
6707 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6708 }
6709 else
6710 {
6711 /* The name is already allocated along with this objfile, so
6712 we don't need to duplicate it for the type. */
6713 TYPE_TAG_NAME (type) = (char *) name;
6714 if (die->tag == DW_TAG_class_type)
6715 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6716 }
6717 }
6718
6719 if (die->tag == DW_TAG_structure_type)
6720 {
6721 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6722 }
6723 else if (die->tag == DW_TAG_union_type)
6724 {
6725 TYPE_CODE (type) = TYPE_CODE_UNION;
6726 }
6727 else
6728 {
6729 TYPE_CODE (type) = TYPE_CODE_CLASS;
6730 }
6731
6732 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6733 TYPE_DECLARED_CLASS (type) = 1;
6734
6735 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6736 if (attr)
6737 {
6738 TYPE_LENGTH (type) = DW_UNSND (attr);
6739 }
6740 else
6741 {
6742 TYPE_LENGTH (type) = 0;
6743 }
6744
6745 TYPE_STUB_SUPPORTED (type) = 1;
6746 if (die_is_declaration (die, cu))
6747 TYPE_STUB (type) = 1;
6748 else if (attr == NULL && die->child == NULL
6749 && producer_is_realview (cu->producer))
6750 /* RealView does not output the required DW_AT_declaration
6751 on incomplete types. */
6752 TYPE_STUB (type) = 1;
6753
6754 /* We need to add the type field to the die immediately so we don't
6755 infinitely recurse when dealing with pointers to the structure
6756 type within the structure itself. */
6757 set_die_type (die, type, cu);
6758
6759 /* set_die_type should be already done. */
6760 set_descriptive_type (type, die, cu);
6761
6762 return type;
6763 }
6764
6765 /* Finish creating a structure or union type, including filling in
6766 its members and creating a symbol for it. */
6767
6768 static void
6769 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6770 {
6771 struct objfile *objfile = cu->objfile;
6772 struct die_info *child_die = die->child;
6773 struct type *type;
6774
6775 type = get_die_type (die, cu);
6776 if (type == NULL)
6777 type = read_structure_type (die, cu);
6778
6779 if (die->child != NULL && ! die_is_declaration (die, cu))
6780 {
6781 struct field_info fi;
6782 struct die_info *child_die;
6783 VEC (symbolp) *template_args = NULL;
6784 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6785
6786 memset (&fi, 0, sizeof (struct field_info));
6787
6788 child_die = die->child;
6789
6790 while (child_die && child_die->tag)
6791 {
6792 if (child_die->tag == DW_TAG_member
6793 || child_die->tag == DW_TAG_variable)
6794 {
6795 /* NOTE: carlton/2002-11-05: A C++ static data member
6796 should be a DW_TAG_member that is a declaration, but
6797 all versions of G++ as of this writing (so through at
6798 least 3.2.1) incorrectly generate DW_TAG_variable
6799 tags for them instead. */
6800 dwarf2_add_field (&fi, child_die, cu);
6801 }
6802 else if (child_die->tag == DW_TAG_subprogram)
6803 {
6804 /* C++ member function. */
6805 dwarf2_add_member_fn (&fi, child_die, type, cu);
6806 }
6807 else if (child_die->tag == DW_TAG_inheritance)
6808 {
6809 /* C++ base class field. */
6810 dwarf2_add_field (&fi, child_die, cu);
6811 }
6812 else if (child_die->tag == DW_TAG_typedef)
6813 dwarf2_add_typedef (&fi, child_die, cu);
6814 else if (child_die->tag == DW_TAG_template_type_param
6815 || child_die->tag == DW_TAG_template_value_param)
6816 {
6817 struct symbol *arg = new_symbol (child_die, NULL, cu);
6818
6819 if (arg != NULL)
6820 VEC_safe_push (symbolp, template_args, arg);
6821 }
6822
6823 child_die = sibling_die (child_die);
6824 }
6825
6826 /* Attach template arguments to type. */
6827 if (! VEC_empty (symbolp, template_args))
6828 {
6829 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6830 TYPE_N_TEMPLATE_ARGUMENTS (type)
6831 = VEC_length (symbolp, template_args);
6832 TYPE_TEMPLATE_ARGUMENTS (type)
6833 = obstack_alloc (&objfile->objfile_obstack,
6834 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6835 * sizeof (struct symbol *)));
6836 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6837 VEC_address (symbolp, template_args),
6838 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6839 * sizeof (struct symbol *)));
6840 VEC_free (symbolp, template_args);
6841 }
6842
6843 /* Attach fields and member functions to the type. */
6844 if (fi.nfields)
6845 dwarf2_attach_fields_to_type (&fi, type, cu);
6846 if (fi.nfnfields)
6847 {
6848 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6849
6850 /* Get the type which refers to the base class (possibly this
6851 class itself) which contains the vtable pointer for the current
6852 class from the DW_AT_containing_type attribute. This use of
6853 DW_AT_containing_type is a GNU extension. */
6854
6855 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6856 {
6857 struct type *t = die_containing_type (die, cu);
6858
6859 TYPE_VPTR_BASETYPE (type) = t;
6860 if (type == t)
6861 {
6862 int i;
6863
6864 /* Our own class provides vtbl ptr. */
6865 for (i = TYPE_NFIELDS (t) - 1;
6866 i >= TYPE_N_BASECLASSES (t);
6867 --i)
6868 {
6869 char *fieldname = TYPE_FIELD_NAME (t, i);
6870
6871 if (is_vtable_name (fieldname, cu))
6872 {
6873 TYPE_VPTR_FIELDNO (type) = i;
6874 break;
6875 }
6876 }
6877
6878 /* Complain if virtual function table field not found. */
6879 if (i < TYPE_N_BASECLASSES (t))
6880 complaint (&symfile_complaints,
6881 _("virtual function table pointer not found when defining class '%s'"),
6882 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6883 "");
6884 }
6885 else
6886 {
6887 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6888 }
6889 }
6890 else if (cu->producer
6891 && strncmp (cu->producer,
6892 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6893 {
6894 /* The IBM XLC compiler does not provide direct indication
6895 of the containing type, but the vtable pointer is
6896 always named __vfp. */
6897
6898 int i;
6899
6900 for (i = TYPE_NFIELDS (type) - 1;
6901 i >= TYPE_N_BASECLASSES (type);
6902 --i)
6903 {
6904 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6905 {
6906 TYPE_VPTR_FIELDNO (type) = i;
6907 TYPE_VPTR_BASETYPE (type) = type;
6908 break;
6909 }
6910 }
6911 }
6912 }
6913
6914 /* Copy fi.typedef_field_list linked list elements content into the
6915 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6916 if (fi.typedef_field_list)
6917 {
6918 int i = fi.typedef_field_list_count;
6919
6920 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6921 TYPE_TYPEDEF_FIELD_ARRAY (type)
6922 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6923 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6924
6925 /* Reverse the list order to keep the debug info elements order. */
6926 while (--i >= 0)
6927 {
6928 struct typedef_field *dest, *src;
6929
6930 dest = &TYPE_TYPEDEF_FIELD (type, i);
6931 src = &fi.typedef_field_list->field;
6932 fi.typedef_field_list = fi.typedef_field_list->next;
6933 *dest = *src;
6934 }
6935 }
6936
6937 do_cleanups (back_to);
6938 }
6939
6940 quirk_gcc_member_function_pointer (type, cu->objfile);
6941
6942 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6943 snapshots) has been known to create a die giving a declaration
6944 for a class that has, as a child, a die giving a definition for a
6945 nested class. So we have to process our children even if the
6946 current die is a declaration. Normally, of course, a declaration
6947 won't have any children at all. */
6948
6949 while (child_die != NULL && child_die->tag)
6950 {
6951 if (child_die->tag == DW_TAG_member
6952 || child_die->tag == DW_TAG_variable
6953 || child_die->tag == DW_TAG_inheritance
6954 || child_die->tag == DW_TAG_template_value_param
6955 || child_die->tag == DW_TAG_template_type_param)
6956 {
6957 /* Do nothing. */
6958 }
6959 else
6960 process_die (child_die, cu);
6961
6962 child_die = sibling_die (child_die);
6963 }
6964
6965 /* Do not consider external references. According to the DWARF standard,
6966 these DIEs are identified by the fact that they have no byte_size
6967 attribute, and a declaration attribute. */
6968 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6969 || !die_is_declaration (die, cu))
6970 new_symbol (die, type, cu);
6971 }
6972
6973 /* Given a DW_AT_enumeration_type die, set its type. We do not
6974 complete the type's fields yet, or create any symbols. */
6975
6976 static struct type *
6977 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6978 {
6979 struct objfile *objfile = cu->objfile;
6980 struct type *type;
6981 struct attribute *attr;
6982 const char *name;
6983
6984 /* If the definition of this type lives in .debug_types, read that type.
6985 Don't follow DW_AT_specification though, that will take us back up
6986 the chain and we want to go down. */
6987 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6988 if (attr)
6989 {
6990 struct dwarf2_cu *type_cu = cu;
6991 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6992
6993 type = read_type_die (type_die, type_cu);
6994
6995 /* TYPE_CU may not be the same as CU.
6996 Ensure TYPE is recorded in CU's type_hash table. */
6997 return set_die_type (die, type, cu);
6998 }
6999
7000 type = alloc_type (objfile);
7001
7002 TYPE_CODE (type) = TYPE_CODE_ENUM;
7003 name = dwarf2_full_name (NULL, die, cu);
7004 if (name != NULL)
7005 TYPE_TAG_NAME (type) = (char *) name;
7006
7007 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7008 if (attr)
7009 {
7010 TYPE_LENGTH (type) = DW_UNSND (attr);
7011 }
7012 else
7013 {
7014 TYPE_LENGTH (type) = 0;
7015 }
7016
7017 /* The enumeration DIE can be incomplete. In Ada, any type can be
7018 declared as private in the package spec, and then defined only
7019 inside the package body. Such types are known as Taft Amendment
7020 Types. When another package uses such a type, an incomplete DIE
7021 may be generated by the compiler. */
7022 if (die_is_declaration (die, cu))
7023 TYPE_STUB (type) = 1;
7024
7025 return set_die_type (die, type, cu);
7026 }
7027
7028 /* Given a pointer to a die which begins an enumeration, process all
7029 the dies that define the members of the enumeration, and create the
7030 symbol for the enumeration type.
7031
7032 NOTE: We reverse the order of the element list. */
7033
7034 static void
7035 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7036 {
7037 struct type *this_type;
7038
7039 this_type = get_die_type (die, cu);
7040 if (this_type == NULL)
7041 this_type = read_enumeration_type (die, cu);
7042
7043 if (die->child != NULL)
7044 {
7045 struct die_info *child_die;
7046 struct symbol *sym;
7047 struct field *fields = NULL;
7048 int num_fields = 0;
7049 int unsigned_enum = 1;
7050 char *name;
7051
7052 child_die = die->child;
7053 while (child_die && child_die->tag)
7054 {
7055 if (child_die->tag != DW_TAG_enumerator)
7056 {
7057 process_die (child_die, cu);
7058 }
7059 else
7060 {
7061 name = dwarf2_name (child_die, cu);
7062 if (name)
7063 {
7064 sym = new_symbol (child_die, this_type, cu);
7065 if (SYMBOL_VALUE (sym) < 0)
7066 unsigned_enum = 0;
7067
7068 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7069 {
7070 fields = (struct field *)
7071 xrealloc (fields,
7072 (num_fields + DW_FIELD_ALLOC_CHUNK)
7073 * sizeof (struct field));
7074 }
7075
7076 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7077 FIELD_TYPE (fields[num_fields]) = NULL;
7078 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7079 FIELD_BITSIZE (fields[num_fields]) = 0;
7080
7081 num_fields++;
7082 }
7083 }
7084
7085 child_die = sibling_die (child_die);
7086 }
7087
7088 if (num_fields)
7089 {
7090 TYPE_NFIELDS (this_type) = num_fields;
7091 TYPE_FIELDS (this_type) = (struct field *)
7092 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7093 memcpy (TYPE_FIELDS (this_type), fields,
7094 sizeof (struct field) * num_fields);
7095 xfree (fields);
7096 }
7097 if (unsigned_enum)
7098 TYPE_UNSIGNED (this_type) = 1;
7099 }
7100
7101 new_symbol (die, this_type, cu);
7102 }
7103
7104 /* Extract all information from a DW_TAG_array_type DIE and put it in
7105 the DIE's type field. For now, this only handles one dimensional
7106 arrays. */
7107
7108 static struct type *
7109 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7110 {
7111 struct objfile *objfile = cu->objfile;
7112 struct die_info *child_die;
7113 struct type *type;
7114 struct type *element_type, *range_type, *index_type;
7115 struct type **range_types = NULL;
7116 struct attribute *attr;
7117 int ndim = 0;
7118 struct cleanup *back_to;
7119 char *name;
7120
7121 element_type = die_type (die, cu);
7122
7123 /* The die_type call above may have already set the type for this DIE. */
7124 type = get_die_type (die, cu);
7125 if (type)
7126 return type;
7127
7128 /* Irix 6.2 native cc creates array types without children for
7129 arrays with unspecified length. */
7130 if (die->child == NULL)
7131 {
7132 index_type = objfile_type (objfile)->builtin_int;
7133 range_type = create_range_type (NULL, index_type, 0, -1);
7134 type = create_array_type (NULL, element_type, range_type);
7135 return set_die_type (die, type, cu);
7136 }
7137
7138 back_to = make_cleanup (null_cleanup, NULL);
7139 child_die = die->child;
7140 while (child_die && child_die->tag)
7141 {
7142 if (child_die->tag == DW_TAG_subrange_type)
7143 {
7144 struct type *child_type = read_type_die (child_die, cu);
7145
7146 if (child_type != NULL)
7147 {
7148 /* The range type was succesfully read. Save it for
7149 the array type creation. */
7150 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7151 {
7152 range_types = (struct type **)
7153 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7154 * sizeof (struct type *));
7155 if (ndim == 0)
7156 make_cleanup (free_current_contents, &range_types);
7157 }
7158 range_types[ndim++] = child_type;
7159 }
7160 }
7161 child_die = sibling_die (child_die);
7162 }
7163
7164 /* Dwarf2 dimensions are output from left to right, create the
7165 necessary array types in backwards order. */
7166
7167 type = element_type;
7168
7169 if (read_array_order (die, cu) == DW_ORD_col_major)
7170 {
7171 int i = 0;
7172
7173 while (i < ndim)
7174 type = create_array_type (NULL, type, range_types[i++]);
7175 }
7176 else
7177 {
7178 while (ndim-- > 0)
7179 type = create_array_type (NULL, type, range_types[ndim]);
7180 }
7181
7182 /* Understand Dwarf2 support for vector types (like they occur on
7183 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7184 array type. This is not part of the Dwarf2/3 standard yet, but a
7185 custom vendor extension. The main difference between a regular
7186 array and the vector variant is that vectors are passed by value
7187 to functions. */
7188 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7189 if (attr)
7190 make_vector_type (type);
7191
7192 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7193 implementation may choose to implement triple vectors using this
7194 attribute. */
7195 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7196 if (attr)
7197 {
7198 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7199 TYPE_LENGTH (type) = DW_UNSND (attr);
7200 else
7201 complaint (&symfile_complaints, _("\
7202 DW_AT_byte_size for array type smaller than the total size of elements"));
7203 }
7204
7205 name = dwarf2_name (die, cu);
7206 if (name)
7207 TYPE_NAME (type) = name;
7208
7209 /* Install the type in the die. */
7210 set_die_type (die, type, cu);
7211
7212 /* set_die_type should be already done. */
7213 set_descriptive_type (type, die, cu);
7214
7215 do_cleanups (back_to);
7216
7217 return type;
7218 }
7219
7220 static enum dwarf_array_dim_ordering
7221 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7222 {
7223 struct attribute *attr;
7224
7225 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7226
7227 if (attr) return DW_SND (attr);
7228
7229 /*
7230 GNU F77 is a special case, as at 08/2004 array type info is the
7231 opposite order to the dwarf2 specification, but data is still
7232 laid out as per normal fortran.
7233
7234 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7235 version checking.
7236 */
7237
7238 if (cu->language == language_fortran
7239 && cu->producer && strstr (cu->producer, "GNU F77"))
7240 {
7241 return DW_ORD_row_major;
7242 }
7243
7244 switch (cu->language_defn->la_array_ordering)
7245 {
7246 case array_column_major:
7247 return DW_ORD_col_major;
7248 case array_row_major:
7249 default:
7250 return DW_ORD_row_major;
7251 };
7252 }
7253
7254 /* Extract all information from a DW_TAG_set_type DIE and put it in
7255 the DIE's type field. */
7256
7257 static struct type *
7258 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7259 {
7260 struct type *domain_type, *set_type;
7261 struct attribute *attr;
7262
7263 domain_type = die_type (die, cu);
7264
7265 /* The die_type call above may have already set the type for this DIE. */
7266 set_type = get_die_type (die, cu);
7267 if (set_type)
7268 return set_type;
7269
7270 set_type = create_set_type (NULL, domain_type);
7271
7272 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7273 if (attr)
7274 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7275
7276 return set_die_type (die, set_type, cu);
7277 }
7278
7279 /* First cut: install each common block member as a global variable. */
7280
7281 static void
7282 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7283 {
7284 struct die_info *child_die;
7285 struct attribute *attr;
7286 struct symbol *sym;
7287 CORE_ADDR base = (CORE_ADDR) 0;
7288
7289 attr = dwarf2_attr (die, DW_AT_location, cu);
7290 if (attr)
7291 {
7292 /* Support the .debug_loc offsets */
7293 if (attr_form_is_block (attr))
7294 {
7295 base = decode_locdesc (DW_BLOCK (attr), cu);
7296 }
7297 else if (attr_form_is_section_offset (attr))
7298 {
7299 dwarf2_complex_location_expr_complaint ();
7300 }
7301 else
7302 {
7303 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7304 "common block member");
7305 }
7306 }
7307 if (die->child != NULL)
7308 {
7309 child_die = die->child;
7310 while (child_die && child_die->tag)
7311 {
7312 sym = new_symbol (child_die, NULL, cu);
7313 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7314 if (sym != NULL && attr != NULL)
7315 {
7316 CORE_ADDR byte_offset = 0;
7317
7318 if (attr_form_is_section_offset (attr))
7319 dwarf2_complex_location_expr_complaint ();
7320 else if (attr_form_is_constant (attr))
7321 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7322 else if (attr_form_is_block (attr))
7323 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7324 else
7325 dwarf2_complex_location_expr_complaint ();
7326
7327 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7328 add_symbol_to_list (sym, &global_symbols);
7329 }
7330 child_die = sibling_die (child_die);
7331 }
7332 }
7333 }
7334
7335 /* Create a type for a C++ namespace. */
7336
7337 static struct type *
7338 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7339 {
7340 struct objfile *objfile = cu->objfile;
7341 const char *previous_prefix, *name;
7342 int is_anonymous;
7343 struct type *type;
7344
7345 /* For extensions, reuse the type of the original namespace. */
7346 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7347 {
7348 struct die_info *ext_die;
7349 struct dwarf2_cu *ext_cu = cu;
7350
7351 ext_die = dwarf2_extension (die, &ext_cu);
7352 type = read_type_die (ext_die, ext_cu);
7353
7354 /* EXT_CU may not be the same as CU.
7355 Ensure TYPE is recorded in CU's type_hash table. */
7356 return set_die_type (die, type, cu);
7357 }
7358
7359 name = namespace_name (die, &is_anonymous, cu);
7360
7361 /* Now build the name of the current namespace. */
7362
7363 previous_prefix = determine_prefix (die, cu);
7364 if (previous_prefix[0] != '\0')
7365 name = typename_concat (&objfile->objfile_obstack,
7366 previous_prefix, name, 0, cu);
7367
7368 /* Create the type. */
7369 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7370 objfile);
7371 TYPE_NAME (type) = (char *) name;
7372 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7373
7374 return set_die_type (die, type, cu);
7375 }
7376
7377 /* Read a C++ namespace. */
7378
7379 static void
7380 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7381 {
7382 struct objfile *objfile = cu->objfile;
7383 const char *name;
7384 int is_anonymous;
7385
7386 /* Add a symbol associated to this if we haven't seen the namespace
7387 before. Also, add a using directive if it's an anonymous
7388 namespace. */
7389
7390 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7391 {
7392 struct type *type;
7393
7394 type = read_type_die (die, cu);
7395 new_symbol (die, type, cu);
7396
7397 name = namespace_name (die, &is_anonymous, cu);
7398 if (is_anonymous)
7399 {
7400 const char *previous_prefix = determine_prefix (die, cu);
7401
7402 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7403 NULL, &objfile->objfile_obstack);
7404 }
7405 }
7406
7407 if (die->child != NULL)
7408 {
7409 struct die_info *child_die = die->child;
7410
7411 while (child_die && child_die->tag)
7412 {
7413 process_die (child_die, cu);
7414 child_die = sibling_die (child_die);
7415 }
7416 }
7417 }
7418
7419 /* Read a Fortran module as type. This DIE can be only a declaration used for
7420 imported module. Still we need that type as local Fortran "use ... only"
7421 declaration imports depend on the created type in determine_prefix. */
7422
7423 static struct type *
7424 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7425 {
7426 struct objfile *objfile = cu->objfile;
7427 char *module_name;
7428 struct type *type;
7429
7430 module_name = dwarf2_name (die, cu);
7431 if (!module_name)
7432 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7433 die->offset);
7434 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7435
7436 /* determine_prefix uses TYPE_TAG_NAME. */
7437 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7438
7439 return set_die_type (die, type, cu);
7440 }
7441
7442 /* Read a Fortran module. */
7443
7444 static void
7445 read_module (struct die_info *die, struct dwarf2_cu *cu)
7446 {
7447 struct die_info *child_die = die->child;
7448
7449 while (child_die && child_die->tag)
7450 {
7451 process_die (child_die, cu);
7452 child_die = sibling_die (child_die);
7453 }
7454 }
7455
7456 /* Return the name of the namespace represented by DIE. Set
7457 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7458 namespace. */
7459
7460 static const char *
7461 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7462 {
7463 struct die_info *current_die;
7464 const char *name = NULL;
7465
7466 /* Loop through the extensions until we find a name. */
7467
7468 for (current_die = die;
7469 current_die != NULL;
7470 current_die = dwarf2_extension (die, &cu))
7471 {
7472 name = dwarf2_name (current_die, cu);
7473 if (name != NULL)
7474 break;
7475 }
7476
7477 /* Is it an anonymous namespace? */
7478
7479 *is_anonymous = (name == NULL);
7480 if (*is_anonymous)
7481 name = "(anonymous namespace)";
7482
7483 return name;
7484 }
7485
7486 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7487 the user defined type vector. */
7488
7489 static struct type *
7490 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7491 {
7492 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7493 struct comp_unit_head *cu_header = &cu->header;
7494 struct type *type;
7495 struct attribute *attr_byte_size;
7496 struct attribute *attr_address_class;
7497 int byte_size, addr_class;
7498 struct type *target_type;
7499
7500 target_type = die_type (die, cu);
7501
7502 /* The die_type call above may have already set the type for this DIE. */
7503 type = get_die_type (die, cu);
7504 if (type)
7505 return type;
7506
7507 type = lookup_pointer_type (target_type);
7508
7509 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7510 if (attr_byte_size)
7511 byte_size = DW_UNSND (attr_byte_size);
7512 else
7513 byte_size = cu_header->addr_size;
7514
7515 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7516 if (attr_address_class)
7517 addr_class = DW_UNSND (attr_address_class);
7518 else
7519 addr_class = DW_ADDR_none;
7520
7521 /* If the pointer size or address class is different than the
7522 default, create a type variant marked as such and set the
7523 length accordingly. */
7524 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7525 {
7526 if (gdbarch_address_class_type_flags_p (gdbarch))
7527 {
7528 int type_flags;
7529
7530 type_flags = gdbarch_address_class_type_flags
7531 (gdbarch, byte_size, addr_class);
7532 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7533 == 0);
7534 type = make_type_with_address_space (type, type_flags);
7535 }
7536 else if (TYPE_LENGTH (type) != byte_size)
7537 {
7538 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7539 }
7540 else
7541 {
7542 /* Should we also complain about unhandled address classes? */
7543 }
7544 }
7545
7546 TYPE_LENGTH (type) = byte_size;
7547 return set_die_type (die, type, cu);
7548 }
7549
7550 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7551 the user defined type vector. */
7552
7553 static struct type *
7554 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7555 {
7556 struct type *type;
7557 struct type *to_type;
7558 struct type *domain;
7559
7560 to_type = die_type (die, cu);
7561 domain = die_containing_type (die, cu);
7562
7563 /* The calls above may have already set the type for this DIE. */
7564 type = get_die_type (die, cu);
7565 if (type)
7566 return type;
7567
7568 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7569 type = lookup_methodptr_type (to_type);
7570 else
7571 type = lookup_memberptr_type (to_type, domain);
7572
7573 return set_die_type (die, type, cu);
7574 }
7575
7576 /* Extract all information from a DW_TAG_reference_type DIE and add to
7577 the user defined type vector. */
7578
7579 static struct type *
7580 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7581 {
7582 struct comp_unit_head *cu_header = &cu->header;
7583 struct type *type, *target_type;
7584 struct attribute *attr;
7585
7586 target_type = die_type (die, cu);
7587
7588 /* The die_type call above may have already set the type for this DIE. */
7589 type = get_die_type (die, cu);
7590 if (type)
7591 return type;
7592
7593 type = lookup_reference_type (target_type);
7594 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7595 if (attr)
7596 {
7597 TYPE_LENGTH (type) = DW_UNSND (attr);
7598 }
7599 else
7600 {
7601 TYPE_LENGTH (type) = cu_header->addr_size;
7602 }
7603 return set_die_type (die, type, cu);
7604 }
7605
7606 static struct type *
7607 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7608 {
7609 struct type *base_type, *cv_type;
7610
7611 base_type = die_type (die, cu);
7612
7613 /* The die_type call above may have already set the type for this DIE. */
7614 cv_type = get_die_type (die, cu);
7615 if (cv_type)
7616 return cv_type;
7617
7618 /* In case the const qualifier is applied to an array type, the element type
7619 is so qualified, not the array type (section 6.7.3 of C99). */
7620 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7621 {
7622 struct type *el_type, *inner_array;
7623
7624 base_type = copy_type (base_type);
7625 inner_array = base_type;
7626
7627 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7628 {
7629 TYPE_TARGET_TYPE (inner_array) =
7630 copy_type (TYPE_TARGET_TYPE (inner_array));
7631 inner_array = TYPE_TARGET_TYPE (inner_array);
7632 }
7633
7634 el_type = TYPE_TARGET_TYPE (inner_array);
7635 TYPE_TARGET_TYPE (inner_array) =
7636 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7637
7638 return set_die_type (die, base_type, cu);
7639 }
7640
7641 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7642 return set_die_type (die, cv_type, cu);
7643 }
7644
7645 static struct type *
7646 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7647 {
7648 struct type *base_type, *cv_type;
7649
7650 base_type = die_type (die, cu);
7651
7652 /* The die_type call above may have already set the type for this DIE. */
7653 cv_type = get_die_type (die, cu);
7654 if (cv_type)
7655 return cv_type;
7656
7657 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7658 return set_die_type (die, cv_type, cu);
7659 }
7660
7661 /* Extract all information from a DW_TAG_string_type DIE and add to
7662 the user defined type vector. It isn't really a user defined type,
7663 but it behaves like one, with other DIE's using an AT_user_def_type
7664 attribute to reference it. */
7665
7666 static struct type *
7667 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7668 {
7669 struct objfile *objfile = cu->objfile;
7670 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7671 struct type *type, *range_type, *index_type, *char_type;
7672 struct attribute *attr;
7673 unsigned int length;
7674
7675 attr = dwarf2_attr (die, DW_AT_string_length, cu);
7676 if (attr)
7677 {
7678 length = DW_UNSND (attr);
7679 }
7680 else
7681 {
7682 /* check for the DW_AT_byte_size attribute */
7683 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7684 if (attr)
7685 {
7686 length = DW_UNSND (attr);
7687 }
7688 else
7689 {
7690 length = 1;
7691 }
7692 }
7693
7694 index_type = objfile_type (objfile)->builtin_int;
7695 range_type = create_range_type (NULL, index_type, 1, length);
7696 char_type = language_string_char_type (cu->language_defn, gdbarch);
7697 type = create_string_type (NULL, char_type, range_type);
7698
7699 return set_die_type (die, type, cu);
7700 }
7701
7702 /* Handle DIES due to C code like:
7703
7704 struct foo
7705 {
7706 int (*funcp)(int a, long l);
7707 int b;
7708 };
7709
7710 ('funcp' generates a DW_TAG_subroutine_type DIE)
7711 */
7712
7713 static struct type *
7714 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7715 {
7716 struct type *type; /* Type that this function returns */
7717 struct type *ftype; /* Function that returns above type */
7718 struct attribute *attr;
7719
7720 type = die_type (die, cu);
7721
7722 /* The die_type call above may have already set the type for this DIE. */
7723 ftype = get_die_type (die, cu);
7724 if (ftype)
7725 return ftype;
7726
7727 ftype = lookup_function_type (type);
7728
7729 /* All functions in C++, Pascal and Java have prototypes. */
7730 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7731 if ((attr && (DW_UNSND (attr) != 0))
7732 || cu->language == language_cplus
7733 || cu->language == language_java
7734 || cu->language == language_pascal)
7735 TYPE_PROTOTYPED (ftype) = 1;
7736 else if (producer_is_realview (cu->producer))
7737 /* RealView does not emit DW_AT_prototyped. We can not
7738 distinguish prototyped and unprototyped functions; default to
7739 prototyped, since that is more common in modern code (and
7740 RealView warns about unprototyped functions). */
7741 TYPE_PROTOTYPED (ftype) = 1;
7742
7743 /* Store the calling convention in the type if it's available in
7744 the subroutine die. Otherwise set the calling convention to
7745 the default value DW_CC_normal. */
7746 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7747 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7748
7749 /* We need to add the subroutine type to the die immediately so
7750 we don't infinitely recurse when dealing with parameters
7751 declared as the same subroutine type. */
7752 set_die_type (die, ftype, cu);
7753
7754 if (die->child != NULL)
7755 {
7756 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7757 struct die_info *child_die;
7758 int nparams, iparams;
7759
7760 /* Count the number of parameters.
7761 FIXME: GDB currently ignores vararg functions, but knows about
7762 vararg member functions. */
7763 nparams = 0;
7764 child_die = die->child;
7765 while (child_die && child_die->tag)
7766 {
7767 if (child_die->tag == DW_TAG_formal_parameter)
7768 nparams++;
7769 else if (child_die->tag == DW_TAG_unspecified_parameters)
7770 TYPE_VARARGS (ftype) = 1;
7771 child_die = sibling_die (child_die);
7772 }
7773
7774 /* Allocate storage for parameters and fill them in. */
7775 TYPE_NFIELDS (ftype) = nparams;
7776 TYPE_FIELDS (ftype) = (struct field *)
7777 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7778
7779 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7780 even if we error out during the parameters reading below. */
7781 for (iparams = 0; iparams < nparams; iparams++)
7782 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7783
7784 iparams = 0;
7785 child_die = die->child;
7786 while (child_die && child_die->tag)
7787 {
7788 if (child_die->tag == DW_TAG_formal_parameter)
7789 {
7790 struct type *arg_type;
7791
7792 /* DWARF version 2 has no clean way to discern C++
7793 static and non-static member functions. G++ helps
7794 GDB by marking the first parameter for non-static
7795 member functions (which is the this pointer) as
7796 artificial. We pass this information to
7797 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7798
7799 DWARF version 3 added DW_AT_object_pointer, which GCC
7800 4.5 does not yet generate. */
7801 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7802 if (attr)
7803 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7804 else
7805 {
7806 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7807
7808 /* GCC/43521: In java, the formal parameter
7809 "this" is sometimes not marked with DW_AT_artificial. */
7810 if (cu->language == language_java)
7811 {
7812 const char *name = dwarf2_name (child_die, cu);
7813
7814 if (name && !strcmp (name, "this"))
7815 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7816 }
7817 }
7818 arg_type = die_type (child_die, cu);
7819
7820 /* RealView does not mark THIS as const, which the testsuite
7821 expects. GCC marks THIS as const in method definitions,
7822 but not in the class specifications (GCC PR 43053). */
7823 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7824 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7825 {
7826 int is_this = 0;
7827 struct dwarf2_cu *arg_cu = cu;
7828 const char *name = dwarf2_name (child_die, cu);
7829
7830 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7831 if (attr)
7832 {
7833 /* If the compiler emits this, use it. */
7834 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7835 is_this = 1;
7836 }
7837 else if (name && strcmp (name, "this") == 0)
7838 /* Function definitions will have the argument names. */
7839 is_this = 1;
7840 else if (name == NULL && iparams == 0)
7841 /* Declarations may not have the names, so like
7842 elsewhere in GDB, assume an artificial first
7843 argument is "this". */
7844 is_this = 1;
7845
7846 if (is_this)
7847 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7848 arg_type, 0);
7849 }
7850
7851 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7852 iparams++;
7853 }
7854 child_die = sibling_die (child_die);
7855 }
7856 }
7857
7858 return ftype;
7859 }
7860
7861 static struct type *
7862 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7863 {
7864 struct objfile *objfile = cu->objfile;
7865 const char *name = NULL;
7866 struct type *this_type;
7867
7868 name = dwarf2_full_name (NULL, die, cu);
7869 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7870 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7871 TYPE_NAME (this_type) = (char *) name;
7872 set_die_type (die, this_type, cu);
7873 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7874 return this_type;
7875 }
7876
7877 /* Find a representation of a given base type and install
7878 it in the TYPE field of the die. */
7879
7880 static struct type *
7881 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7882 {
7883 struct objfile *objfile = cu->objfile;
7884 struct type *type;
7885 struct attribute *attr;
7886 int encoding = 0, size = 0;
7887 char *name;
7888 enum type_code code = TYPE_CODE_INT;
7889 int type_flags = 0;
7890 struct type *target_type = NULL;
7891
7892 attr = dwarf2_attr (die, DW_AT_encoding, cu);
7893 if (attr)
7894 {
7895 encoding = DW_UNSND (attr);
7896 }
7897 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7898 if (attr)
7899 {
7900 size = DW_UNSND (attr);
7901 }
7902 name = dwarf2_name (die, cu);
7903 if (!name)
7904 {
7905 complaint (&symfile_complaints,
7906 _("DW_AT_name missing from DW_TAG_base_type"));
7907 }
7908
7909 switch (encoding)
7910 {
7911 case DW_ATE_address:
7912 /* Turn DW_ATE_address into a void * pointer. */
7913 code = TYPE_CODE_PTR;
7914 type_flags |= TYPE_FLAG_UNSIGNED;
7915 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7916 break;
7917 case DW_ATE_boolean:
7918 code = TYPE_CODE_BOOL;
7919 type_flags |= TYPE_FLAG_UNSIGNED;
7920 break;
7921 case DW_ATE_complex_float:
7922 code = TYPE_CODE_COMPLEX;
7923 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7924 break;
7925 case DW_ATE_decimal_float:
7926 code = TYPE_CODE_DECFLOAT;
7927 break;
7928 case DW_ATE_float:
7929 code = TYPE_CODE_FLT;
7930 break;
7931 case DW_ATE_signed:
7932 break;
7933 case DW_ATE_unsigned:
7934 type_flags |= TYPE_FLAG_UNSIGNED;
7935 break;
7936 case DW_ATE_signed_char:
7937 if (cu->language == language_ada || cu->language == language_m2
7938 || cu->language == language_pascal)
7939 code = TYPE_CODE_CHAR;
7940 break;
7941 case DW_ATE_unsigned_char:
7942 if (cu->language == language_ada || cu->language == language_m2
7943 || cu->language == language_pascal)
7944 code = TYPE_CODE_CHAR;
7945 type_flags |= TYPE_FLAG_UNSIGNED;
7946 break;
7947 case DW_ATE_UTF:
7948 /* We just treat this as an integer and then recognize the
7949 type by name elsewhere. */
7950 break;
7951
7952 default:
7953 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7954 dwarf_type_encoding_name (encoding));
7955 break;
7956 }
7957
7958 type = init_type (code, size, type_flags, NULL, objfile);
7959 TYPE_NAME (type) = name;
7960 TYPE_TARGET_TYPE (type) = target_type;
7961
7962 if (name && strcmp (name, "char") == 0)
7963 TYPE_NOSIGN (type) = 1;
7964
7965 return set_die_type (die, type, cu);
7966 }
7967
7968 /* Read the given DW_AT_subrange DIE. */
7969
7970 static struct type *
7971 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7972 {
7973 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7974 struct type *base_type;
7975 struct type *range_type;
7976 struct attribute *attr;
7977 LONGEST low = 0;
7978 LONGEST high = -1;
7979 char *name;
7980 LONGEST negative_mask;
7981
7982 base_type = die_type (die, cu);
7983 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7984 check_typedef (base_type);
7985
7986 /* The die_type call above may have already set the type for this DIE. */
7987 range_type = get_die_type (die, cu);
7988 if (range_type)
7989 return range_type;
7990
7991 if (cu->language == language_fortran)
7992 {
7993 /* FORTRAN implies a lower bound of 1, if not given. */
7994 low = 1;
7995 }
7996
7997 /* FIXME: For variable sized arrays either of these could be
7998 a variable rather than a constant value. We'll allow it,
7999 but we don't know how to handle it. */
8000 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8001 if (attr)
8002 low = dwarf2_get_attr_constant_value (attr, 0);
8003
8004 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8005 if (attr)
8006 {
8007 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8008 {
8009 /* GCC encodes arrays with unspecified or dynamic length
8010 with a DW_FORM_block1 attribute or a reference attribute.
8011 FIXME: GDB does not yet know how to handle dynamic
8012 arrays properly, treat them as arrays with unspecified
8013 length for now.
8014
8015 FIXME: jimb/2003-09-22: GDB does not really know
8016 how to handle arrays of unspecified length
8017 either; we just represent them as zero-length
8018 arrays. Choose an appropriate upper bound given
8019 the lower bound we've computed above. */
8020 high = low - 1;
8021 }
8022 else
8023 high = dwarf2_get_attr_constant_value (attr, 1);
8024 }
8025 else
8026 {
8027 attr = dwarf2_attr (die, DW_AT_count, cu);
8028 if (attr)
8029 {
8030 int count = dwarf2_get_attr_constant_value (attr, 1);
8031 high = low + count - 1;
8032 }
8033 }
8034
8035 /* Dwarf-2 specifications explicitly allows to create subrange types
8036 without specifying a base type.
8037 In that case, the base type must be set to the type of
8038 the lower bound, upper bound or count, in that order, if any of these
8039 three attributes references an object that has a type.
8040 If no base type is found, the Dwarf-2 specifications say that
8041 a signed integer type of size equal to the size of an address should
8042 be used.
8043 For the following C code: `extern char gdb_int [];'
8044 GCC produces an empty range DIE.
8045 FIXME: muller/2010-05-28: Possible references to object for low bound,
8046 high bound or count are not yet handled by this code.
8047 */
8048 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8049 {
8050 struct objfile *objfile = cu->objfile;
8051 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8052 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8053 struct type *int_type = objfile_type (objfile)->builtin_int;
8054
8055 /* Test "int", "long int", and "long long int" objfile types,
8056 and select the first one having a size above or equal to the
8057 architecture address size. */
8058 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8059 base_type = int_type;
8060 else
8061 {
8062 int_type = objfile_type (objfile)->builtin_long;
8063 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8064 base_type = int_type;
8065 else
8066 {
8067 int_type = objfile_type (objfile)->builtin_long_long;
8068 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8069 base_type = int_type;
8070 }
8071 }
8072 }
8073
8074 negative_mask =
8075 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8076 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8077 low |= negative_mask;
8078 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8079 high |= negative_mask;
8080
8081 range_type = create_range_type (NULL, base_type, low, high);
8082
8083 /* Mark arrays with dynamic length at least as an array of unspecified
8084 length. GDB could check the boundary but before it gets implemented at
8085 least allow accessing the array elements. */
8086 if (attr && attr->form == DW_FORM_block1)
8087 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8088
8089 name = dwarf2_name (die, cu);
8090 if (name)
8091 TYPE_NAME (range_type) = name;
8092
8093 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8094 if (attr)
8095 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8096
8097 set_die_type (die, range_type, cu);
8098
8099 /* set_die_type should be already done. */
8100 set_descriptive_type (range_type, die, cu);
8101
8102 return range_type;
8103 }
8104
8105 static struct type *
8106 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8107 {
8108 struct type *type;
8109
8110 /* For now, we only support the C meaning of an unspecified type: void. */
8111
8112 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8113 TYPE_NAME (type) = dwarf2_name (die, cu);
8114
8115 return set_die_type (die, type, cu);
8116 }
8117
8118 /* Trivial hash function for die_info: the hash value of a DIE
8119 is its offset in .debug_info for this objfile. */
8120
8121 static hashval_t
8122 die_hash (const void *item)
8123 {
8124 const struct die_info *die = item;
8125
8126 return die->offset;
8127 }
8128
8129 /* Trivial comparison function for die_info structures: two DIEs
8130 are equal if they have the same offset. */
8131
8132 static int
8133 die_eq (const void *item_lhs, const void *item_rhs)
8134 {
8135 const struct die_info *die_lhs = item_lhs;
8136 const struct die_info *die_rhs = item_rhs;
8137
8138 return die_lhs->offset == die_rhs->offset;
8139 }
8140
8141 /* Read a whole compilation unit into a linked list of dies. */
8142
8143 static struct die_info *
8144 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8145 {
8146 struct die_reader_specs reader_specs;
8147 int read_abbrevs = 0;
8148 struct cleanup *back_to = NULL;
8149 struct die_info *die;
8150
8151 if (cu->dwarf2_abbrevs == NULL)
8152 {
8153 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8154 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8155 read_abbrevs = 1;
8156 }
8157
8158 gdb_assert (cu->die_hash == NULL);
8159 cu->die_hash
8160 = htab_create_alloc_ex (cu->header.length / 12,
8161 die_hash,
8162 die_eq,
8163 NULL,
8164 &cu->comp_unit_obstack,
8165 hashtab_obstack_allocate,
8166 dummy_obstack_deallocate);
8167
8168 init_cu_die_reader (&reader_specs, cu);
8169
8170 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8171
8172 if (read_abbrevs)
8173 do_cleanups (back_to);
8174
8175 return die;
8176 }
8177
8178 /* Main entry point for reading a DIE and all children.
8179 Read the DIE and dump it if requested. */
8180
8181 static struct die_info *
8182 read_die_and_children (const struct die_reader_specs *reader,
8183 gdb_byte *info_ptr,
8184 gdb_byte **new_info_ptr,
8185 struct die_info *parent)
8186 {
8187 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8188 new_info_ptr, parent);
8189
8190 if (dwarf2_die_debug)
8191 {
8192 fprintf_unfiltered (gdb_stdlog,
8193 "\nRead die from %s of %s:\n",
8194 reader->buffer == dwarf2_per_objfile->info.buffer
8195 ? ".debug_info"
8196 : reader->buffer == dwarf2_per_objfile->types.buffer
8197 ? ".debug_types"
8198 : "unknown section",
8199 reader->abfd->filename);
8200 dump_die (result, dwarf2_die_debug);
8201 }
8202
8203 return result;
8204 }
8205
8206 /* Read a single die and all its descendents. Set the die's sibling
8207 field to NULL; set other fields in the die correctly, and set all
8208 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8209 location of the info_ptr after reading all of those dies. PARENT
8210 is the parent of the die in question. */
8211
8212 static struct die_info *
8213 read_die_and_children_1 (const struct die_reader_specs *reader,
8214 gdb_byte *info_ptr,
8215 gdb_byte **new_info_ptr,
8216 struct die_info *parent)
8217 {
8218 struct die_info *die;
8219 gdb_byte *cur_ptr;
8220 int has_children;
8221
8222 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8223 if (die == NULL)
8224 {
8225 *new_info_ptr = cur_ptr;
8226 return NULL;
8227 }
8228 store_in_ref_table (die, reader->cu);
8229
8230 if (has_children)
8231 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8232 else
8233 {
8234 die->child = NULL;
8235 *new_info_ptr = cur_ptr;
8236 }
8237
8238 die->sibling = NULL;
8239 die->parent = parent;
8240 return die;
8241 }
8242
8243 /* Read a die, all of its descendents, and all of its siblings; set
8244 all of the fields of all of the dies correctly. Arguments are as
8245 in read_die_and_children. */
8246
8247 static struct die_info *
8248 read_die_and_siblings (const struct die_reader_specs *reader,
8249 gdb_byte *info_ptr,
8250 gdb_byte **new_info_ptr,
8251 struct die_info *parent)
8252 {
8253 struct die_info *first_die, *last_sibling;
8254 gdb_byte *cur_ptr;
8255
8256 cur_ptr = info_ptr;
8257 first_die = last_sibling = NULL;
8258
8259 while (1)
8260 {
8261 struct die_info *die
8262 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8263
8264 if (die == NULL)
8265 {
8266 *new_info_ptr = cur_ptr;
8267 return first_die;
8268 }
8269
8270 if (!first_die)
8271 first_die = die;
8272 else
8273 last_sibling->sibling = die;
8274
8275 last_sibling = die;
8276 }
8277 }
8278
8279 /* Read the die from the .debug_info section buffer. Set DIEP to
8280 point to a newly allocated die with its information, except for its
8281 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8282 whether the die has children or not. */
8283
8284 static gdb_byte *
8285 read_full_die (const struct die_reader_specs *reader,
8286 struct die_info **diep, gdb_byte *info_ptr,
8287 int *has_children)
8288 {
8289 unsigned int abbrev_number, bytes_read, i, offset;
8290 struct abbrev_info *abbrev;
8291 struct die_info *die;
8292 struct dwarf2_cu *cu = reader->cu;
8293 bfd *abfd = reader->abfd;
8294
8295 offset = info_ptr - reader->buffer;
8296 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8297 info_ptr += bytes_read;
8298 if (!abbrev_number)
8299 {
8300 *diep = NULL;
8301 *has_children = 0;
8302 return info_ptr;
8303 }
8304
8305 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8306 if (!abbrev)
8307 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8308 abbrev_number,
8309 bfd_get_filename (abfd));
8310
8311 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8312 die->offset = offset;
8313 die->tag = abbrev->tag;
8314 die->abbrev = abbrev_number;
8315
8316 die->num_attrs = abbrev->num_attrs;
8317
8318 for (i = 0; i < abbrev->num_attrs; ++i)
8319 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8320 abfd, info_ptr, cu);
8321
8322 *diep = die;
8323 *has_children = abbrev->has_children;
8324 return info_ptr;
8325 }
8326
8327 /* In DWARF version 2, the description of the debugging information is
8328 stored in a separate .debug_abbrev section. Before we read any
8329 dies from a section we read in all abbreviations and install them
8330 in a hash table. This function also sets flags in CU describing
8331 the data found in the abbrev table. */
8332
8333 static void
8334 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8335 {
8336 struct comp_unit_head *cu_header = &cu->header;
8337 gdb_byte *abbrev_ptr;
8338 struct abbrev_info *cur_abbrev;
8339 unsigned int abbrev_number, bytes_read, abbrev_name;
8340 unsigned int abbrev_form, hash_number;
8341 struct attr_abbrev *cur_attrs;
8342 unsigned int allocated_attrs;
8343
8344 /* Initialize dwarf2 abbrevs */
8345 obstack_init (&cu->abbrev_obstack);
8346 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8347 (ABBREV_HASH_SIZE
8348 * sizeof (struct abbrev_info *)));
8349 memset (cu->dwarf2_abbrevs, 0,
8350 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8351
8352 dwarf2_read_section (dwarf2_per_objfile->objfile,
8353 &dwarf2_per_objfile->abbrev);
8354 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8355 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8356 abbrev_ptr += bytes_read;
8357
8358 allocated_attrs = ATTR_ALLOC_CHUNK;
8359 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8360
8361 /* loop until we reach an abbrev number of 0 */
8362 while (abbrev_number)
8363 {
8364 cur_abbrev = dwarf_alloc_abbrev (cu);
8365
8366 /* read in abbrev header */
8367 cur_abbrev->number = abbrev_number;
8368 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8369 abbrev_ptr += bytes_read;
8370 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8371 abbrev_ptr += 1;
8372
8373 if (cur_abbrev->tag == DW_TAG_namespace)
8374 cu->has_namespace_info = 1;
8375
8376 /* now read in declarations */
8377 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8378 abbrev_ptr += bytes_read;
8379 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8380 abbrev_ptr += bytes_read;
8381 while (abbrev_name)
8382 {
8383 if (cur_abbrev->num_attrs == allocated_attrs)
8384 {
8385 allocated_attrs += ATTR_ALLOC_CHUNK;
8386 cur_attrs
8387 = xrealloc (cur_attrs, (allocated_attrs
8388 * sizeof (struct attr_abbrev)));
8389 }
8390
8391 /* Record whether this compilation unit might have
8392 inter-compilation-unit references. If we don't know what form
8393 this attribute will have, then it might potentially be a
8394 DW_FORM_ref_addr, so we conservatively expect inter-CU
8395 references. */
8396
8397 if (abbrev_form == DW_FORM_ref_addr
8398 || abbrev_form == DW_FORM_indirect)
8399 cu->has_form_ref_addr = 1;
8400
8401 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8402 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8403 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8404 abbrev_ptr += bytes_read;
8405 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8406 abbrev_ptr += bytes_read;
8407 }
8408
8409 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8410 (cur_abbrev->num_attrs
8411 * sizeof (struct attr_abbrev)));
8412 memcpy (cur_abbrev->attrs, cur_attrs,
8413 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8414
8415 hash_number = abbrev_number % ABBREV_HASH_SIZE;
8416 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8417 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8418
8419 /* Get next abbreviation.
8420 Under Irix6 the abbreviations for a compilation unit are not
8421 always properly terminated with an abbrev number of 0.
8422 Exit loop if we encounter an abbreviation which we have
8423 already read (which means we are about to read the abbreviations
8424 for the next compile unit) or if the end of the abbreviation
8425 table is reached. */
8426 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8427 >= dwarf2_per_objfile->abbrev.size)
8428 break;
8429 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8430 abbrev_ptr += bytes_read;
8431 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8432 break;
8433 }
8434
8435 xfree (cur_attrs);
8436 }
8437
8438 /* Release the memory used by the abbrev table for a compilation unit. */
8439
8440 static void
8441 dwarf2_free_abbrev_table (void *ptr_to_cu)
8442 {
8443 struct dwarf2_cu *cu = ptr_to_cu;
8444
8445 obstack_free (&cu->abbrev_obstack, NULL);
8446 cu->dwarf2_abbrevs = NULL;
8447 }
8448
8449 /* Lookup an abbrev_info structure in the abbrev hash table. */
8450
8451 static struct abbrev_info *
8452 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8453 {
8454 unsigned int hash_number;
8455 struct abbrev_info *abbrev;
8456
8457 hash_number = number % ABBREV_HASH_SIZE;
8458 abbrev = cu->dwarf2_abbrevs[hash_number];
8459
8460 while (abbrev)
8461 {
8462 if (abbrev->number == number)
8463 return abbrev;
8464 else
8465 abbrev = abbrev->next;
8466 }
8467 return NULL;
8468 }
8469
8470 /* Returns nonzero if TAG represents a type that we might generate a partial
8471 symbol for. */
8472
8473 static int
8474 is_type_tag_for_partial (int tag)
8475 {
8476 switch (tag)
8477 {
8478 #if 0
8479 /* Some types that would be reasonable to generate partial symbols for,
8480 that we don't at present. */
8481 case DW_TAG_array_type:
8482 case DW_TAG_file_type:
8483 case DW_TAG_ptr_to_member_type:
8484 case DW_TAG_set_type:
8485 case DW_TAG_string_type:
8486 case DW_TAG_subroutine_type:
8487 #endif
8488 case DW_TAG_base_type:
8489 case DW_TAG_class_type:
8490 case DW_TAG_interface_type:
8491 case DW_TAG_enumeration_type:
8492 case DW_TAG_structure_type:
8493 case DW_TAG_subrange_type:
8494 case DW_TAG_typedef:
8495 case DW_TAG_union_type:
8496 return 1;
8497 default:
8498 return 0;
8499 }
8500 }
8501
8502 /* Load all DIEs that are interesting for partial symbols into memory. */
8503
8504 static struct partial_die_info *
8505 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8506 int building_psymtab, struct dwarf2_cu *cu)
8507 {
8508 struct partial_die_info *part_die;
8509 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8510 struct abbrev_info *abbrev;
8511 unsigned int bytes_read;
8512 unsigned int load_all = 0;
8513
8514 int nesting_level = 1;
8515
8516 parent_die = NULL;
8517 last_die = NULL;
8518
8519 if (cu->per_cu && cu->per_cu->load_all_dies)
8520 load_all = 1;
8521
8522 cu->partial_dies
8523 = htab_create_alloc_ex (cu->header.length / 12,
8524 partial_die_hash,
8525 partial_die_eq,
8526 NULL,
8527 &cu->comp_unit_obstack,
8528 hashtab_obstack_allocate,
8529 dummy_obstack_deallocate);
8530
8531 part_die = obstack_alloc (&cu->comp_unit_obstack,
8532 sizeof (struct partial_die_info));
8533
8534 while (1)
8535 {
8536 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8537
8538 /* A NULL abbrev means the end of a series of children. */
8539 if (abbrev == NULL)
8540 {
8541 if (--nesting_level == 0)
8542 {
8543 /* PART_DIE was probably the last thing allocated on the
8544 comp_unit_obstack, so we could call obstack_free
8545 here. We don't do that because the waste is small,
8546 and will be cleaned up when we're done with this
8547 compilation unit. This way, we're also more robust
8548 against other users of the comp_unit_obstack. */
8549 return first_die;
8550 }
8551 info_ptr += bytes_read;
8552 last_die = parent_die;
8553 parent_die = parent_die->die_parent;
8554 continue;
8555 }
8556
8557 /* Check for template arguments. We never save these; if
8558 they're seen, we just mark the parent, and go on our way. */
8559 if (parent_die != NULL
8560 && cu->language == language_cplus
8561 && (abbrev->tag == DW_TAG_template_type_param
8562 || abbrev->tag == DW_TAG_template_value_param))
8563 {
8564 parent_die->has_template_arguments = 1;
8565
8566 if (!load_all)
8567 {
8568 /* We don't need a partial DIE for the template argument. */
8569 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8570 cu);
8571 continue;
8572 }
8573 }
8574
8575 /* We only recurse into subprograms looking for template arguments.
8576 Skip their other children. */
8577 if (!load_all
8578 && cu->language == language_cplus
8579 && parent_die != NULL
8580 && parent_die->tag == DW_TAG_subprogram)
8581 {
8582 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8583 continue;
8584 }
8585
8586 /* Check whether this DIE is interesting enough to save. Normally
8587 we would not be interested in members here, but there may be
8588 later variables referencing them via DW_AT_specification (for
8589 static members). */
8590 if (!load_all
8591 && !is_type_tag_for_partial (abbrev->tag)
8592 && abbrev->tag != DW_TAG_constant
8593 && abbrev->tag != DW_TAG_enumerator
8594 && abbrev->tag != DW_TAG_subprogram
8595 && abbrev->tag != DW_TAG_lexical_block
8596 && abbrev->tag != DW_TAG_variable
8597 && abbrev->tag != DW_TAG_namespace
8598 && abbrev->tag != DW_TAG_module
8599 && abbrev->tag != DW_TAG_member)
8600 {
8601 /* Otherwise we skip to the next sibling, if any. */
8602 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8603 continue;
8604 }
8605
8606 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8607 buffer, info_ptr, cu);
8608
8609 /* This two-pass algorithm for processing partial symbols has a
8610 high cost in cache pressure. Thus, handle some simple cases
8611 here which cover the majority of C partial symbols. DIEs
8612 which neither have specification tags in them, nor could have
8613 specification tags elsewhere pointing at them, can simply be
8614 processed and discarded.
8615
8616 This segment is also optional; scan_partial_symbols and
8617 add_partial_symbol will handle these DIEs if we chain
8618 them in normally. When compilers which do not emit large
8619 quantities of duplicate debug information are more common,
8620 this code can probably be removed. */
8621
8622 /* Any complete simple types at the top level (pretty much all
8623 of them, for a language without namespaces), can be processed
8624 directly. */
8625 if (parent_die == NULL
8626 && part_die->has_specification == 0
8627 && part_die->is_declaration == 0
8628 && (part_die->tag == DW_TAG_typedef
8629 || part_die->tag == DW_TAG_base_type
8630 || part_die->tag == DW_TAG_subrange_type))
8631 {
8632 if (building_psymtab && part_die->name != NULL)
8633 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8634 VAR_DOMAIN, LOC_TYPEDEF,
8635 &cu->objfile->static_psymbols,
8636 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8637 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8638 continue;
8639 }
8640
8641 /* If we're at the second level, and we're an enumerator, and
8642 our parent has no specification (meaning possibly lives in a
8643 namespace elsewhere), then we can add the partial symbol now
8644 instead of queueing it. */
8645 if (part_die->tag == DW_TAG_enumerator
8646 && parent_die != NULL
8647 && parent_die->die_parent == NULL
8648 && parent_die->tag == DW_TAG_enumeration_type
8649 && parent_die->has_specification == 0)
8650 {
8651 if (part_die->name == NULL)
8652 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8653 else if (building_psymtab)
8654 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8655 VAR_DOMAIN, LOC_CONST,
8656 (cu->language == language_cplus
8657 || cu->language == language_java)
8658 ? &cu->objfile->global_psymbols
8659 : &cu->objfile->static_psymbols,
8660 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8661
8662 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8663 continue;
8664 }
8665
8666 /* We'll save this DIE so link it in. */
8667 part_die->die_parent = parent_die;
8668 part_die->die_sibling = NULL;
8669 part_die->die_child = NULL;
8670
8671 if (last_die && last_die == parent_die)
8672 last_die->die_child = part_die;
8673 else if (last_die)
8674 last_die->die_sibling = part_die;
8675
8676 last_die = part_die;
8677
8678 if (first_die == NULL)
8679 first_die = part_die;
8680
8681 /* Maybe add the DIE to the hash table. Not all DIEs that we
8682 find interesting need to be in the hash table, because we
8683 also have the parent/sibling/child chains; only those that we
8684 might refer to by offset later during partial symbol reading.
8685
8686 For now this means things that might have be the target of a
8687 DW_AT_specification, DW_AT_abstract_origin, or
8688 DW_AT_extension. DW_AT_extension will refer only to
8689 namespaces; DW_AT_abstract_origin refers to functions (and
8690 many things under the function DIE, but we do not recurse
8691 into function DIEs during partial symbol reading) and
8692 possibly variables as well; DW_AT_specification refers to
8693 declarations. Declarations ought to have the DW_AT_declaration
8694 flag. It happens that GCC forgets to put it in sometimes, but
8695 only for functions, not for types.
8696
8697 Adding more things than necessary to the hash table is harmless
8698 except for the performance cost. Adding too few will result in
8699 wasted time in find_partial_die, when we reread the compilation
8700 unit with load_all_dies set. */
8701
8702 if (load_all
8703 || abbrev->tag == DW_TAG_constant
8704 || abbrev->tag == DW_TAG_subprogram
8705 || abbrev->tag == DW_TAG_variable
8706 || abbrev->tag == DW_TAG_namespace
8707 || part_die->is_declaration)
8708 {
8709 void **slot;
8710
8711 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8712 part_die->offset, INSERT);
8713 *slot = part_die;
8714 }
8715
8716 part_die = obstack_alloc (&cu->comp_unit_obstack,
8717 sizeof (struct partial_die_info));
8718
8719 /* For some DIEs we want to follow their children (if any). For C
8720 we have no reason to follow the children of structures; for other
8721 languages we have to, so that we can get at method physnames
8722 to infer fully qualified class names, for DW_AT_specification,
8723 and for C++ template arguments. For C++, we also look one level
8724 inside functions to find template arguments (if the name of the
8725 function does not already contain the template arguments).
8726
8727 For Ada, we need to scan the children of subprograms and lexical
8728 blocks as well because Ada allows the definition of nested
8729 entities that could be interesting for the debugger, such as
8730 nested subprograms for instance. */
8731 if (last_die->has_children
8732 && (load_all
8733 || last_die->tag == DW_TAG_namespace
8734 || last_die->tag == DW_TAG_module
8735 || last_die->tag == DW_TAG_enumeration_type
8736 || (cu->language == language_cplus
8737 && last_die->tag == DW_TAG_subprogram
8738 && (last_die->name == NULL
8739 || strchr (last_die->name, '<') == NULL))
8740 || (cu->language != language_c
8741 && (last_die->tag == DW_TAG_class_type
8742 || last_die->tag == DW_TAG_interface_type
8743 || last_die->tag == DW_TAG_structure_type
8744 || last_die->tag == DW_TAG_union_type))
8745 || (cu->language == language_ada
8746 && (last_die->tag == DW_TAG_subprogram
8747 || last_die->tag == DW_TAG_lexical_block))))
8748 {
8749 nesting_level++;
8750 parent_die = last_die;
8751 continue;
8752 }
8753
8754 /* Otherwise we skip to the next sibling, if any. */
8755 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8756
8757 /* Back to the top, do it again. */
8758 }
8759 }
8760
8761 /* Read a minimal amount of information into the minimal die structure. */
8762
8763 static gdb_byte *
8764 read_partial_die (struct partial_die_info *part_die,
8765 struct abbrev_info *abbrev,
8766 unsigned int abbrev_len, bfd *abfd,
8767 gdb_byte *buffer, gdb_byte *info_ptr,
8768 struct dwarf2_cu *cu)
8769 {
8770 unsigned int i;
8771 struct attribute attr;
8772 int has_low_pc_attr = 0;
8773 int has_high_pc_attr = 0;
8774
8775 memset (part_die, 0, sizeof (struct partial_die_info));
8776
8777 part_die->offset = info_ptr - buffer;
8778
8779 info_ptr += abbrev_len;
8780
8781 if (abbrev == NULL)
8782 return info_ptr;
8783
8784 part_die->tag = abbrev->tag;
8785 part_die->has_children = abbrev->has_children;
8786
8787 for (i = 0; i < abbrev->num_attrs; ++i)
8788 {
8789 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8790
8791 /* Store the data if it is of an attribute we want to keep in a
8792 partial symbol table. */
8793 switch (attr.name)
8794 {
8795 case DW_AT_name:
8796 switch (part_die->tag)
8797 {
8798 case DW_TAG_compile_unit:
8799 case DW_TAG_type_unit:
8800 /* Compilation units have a DW_AT_name that is a filename, not
8801 a source language identifier. */
8802 case DW_TAG_enumeration_type:
8803 case DW_TAG_enumerator:
8804 /* These tags always have simple identifiers already; no need
8805 to canonicalize them. */
8806 part_die->name = DW_STRING (&attr);
8807 break;
8808 default:
8809 part_die->name
8810 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8811 &cu->objfile->objfile_obstack);
8812 break;
8813 }
8814 break;
8815 case DW_AT_linkage_name:
8816 case DW_AT_MIPS_linkage_name:
8817 /* Note that both forms of linkage name might appear. We
8818 assume they will be the same, and we only store the last
8819 one we see. */
8820 if (cu->language == language_ada)
8821 part_die->name = DW_STRING (&attr);
8822 part_die->linkage_name = DW_STRING (&attr);
8823 break;
8824 case DW_AT_low_pc:
8825 has_low_pc_attr = 1;
8826 part_die->lowpc = DW_ADDR (&attr);
8827 break;
8828 case DW_AT_high_pc:
8829 has_high_pc_attr = 1;
8830 part_die->highpc = DW_ADDR (&attr);
8831 break;
8832 case DW_AT_location:
8833 /* Support the .debug_loc offsets */
8834 if (attr_form_is_block (&attr))
8835 {
8836 part_die->locdesc = DW_BLOCK (&attr);
8837 }
8838 else if (attr_form_is_section_offset (&attr))
8839 {
8840 dwarf2_complex_location_expr_complaint ();
8841 }
8842 else
8843 {
8844 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8845 "partial symbol information");
8846 }
8847 break;
8848 case DW_AT_external:
8849 part_die->is_external = DW_UNSND (&attr);
8850 break;
8851 case DW_AT_declaration:
8852 part_die->is_declaration = DW_UNSND (&attr);
8853 break;
8854 case DW_AT_type:
8855 part_die->has_type = 1;
8856 break;
8857 case DW_AT_abstract_origin:
8858 case DW_AT_specification:
8859 case DW_AT_extension:
8860 part_die->has_specification = 1;
8861 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8862 break;
8863 case DW_AT_sibling:
8864 /* Ignore absolute siblings, they might point outside of
8865 the current compile unit. */
8866 if (attr.form == DW_FORM_ref_addr)
8867 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8868 else
8869 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8870 break;
8871 case DW_AT_byte_size:
8872 part_die->has_byte_size = 1;
8873 break;
8874 case DW_AT_calling_convention:
8875 /* DWARF doesn't provide a way to identify a program's source-level
8876 entry point. DW_AT_calling_convention attributes are only meant
8877 to describe functions' calling conventions.
8878
8879 However, because it's a necessary piece of information in
8880 Fortran, and because DW_CC_program is the only piece of debugging
8881 information whose definition refers to a 'main program' at all,
8882 several compilers have begun marking Fortran main programs with
8883 DW_CC_program --- even when those functions use the standard
8884 calling conventions.
8885
8886 So until DWARF specifies a way to provide this information and
8887 compilers pick up the new representation, we'll support this
8888 practice. */
8889 if (DW_UNSND (&attr) == DW_CC_program
8890 && cu->language == language_fortran)
8891 {
8892 set_main_name (part_die->name);
8893
8894 /* As this DIE has a static linkage the name would be difficult
8895 to look up later. */
8896 language_of_main = language_fortran;
8897 }
8898 break;
8899 default:
8900 break;
8901 }
8902 }
8903
8904 /* When using the GNU linker, .gnu.linkonce. sections are used to
8905 eliminate duplicate copies of functions and vtables and such.
8906 The linker will arbitrarily choose one and discard the others.
8907 The AT_*_pc values for such functions refer to local labels in
8908 these sections. If the section from that file was discarded, the
8909 labels are not in the output, so the relocs get a value of 0.
8910 If this is a discarded function, mark the pc bounds as invalid,
8911 so that GDB will ignore it. */
8912 if (has_low_pc_attr && has_high_pc_attr
8913 && part_die->lowpc < part_die->highpc
8914 && (part_die->lowpc != 0
8915 || dwarf2_per_objfile->has_section_at_zero))
8916 part_die->has_pc_info = 1;
8917
8918 return info_ptr;
8919 }
8920
8921 /* Find a cached partial DIE at OFFSET in CU. */
8922
8923 static struct partial_die_info *
8924 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8925 {
8926 struct partial_die_info *lookup_die = NULL;
8927 struct partial_die_info part_die;
8928
8929 part_die.offset = offset;
8930 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8931
8932 return lookup_die;
8933 }
8934
8935 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8936 except in the case of .debug_types DIEs which do not reference
8937 outside their CU (they do however referencing other types via
8938 DW_FORM_sig8). */
8939
8940 static struct partial_die_info *
8941 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8942 {
8943 struct dwarf2_per_cu_data *per_cu = NULL;
8944 struct partial_die_info *pd = NULL;
8945
8946 if (cu->per_cu->from_debug_types)
8947 {
8948 pd = find_partial_die_in_comp_unit (offset, cu);
8949 if (pd != NULL)
8950 return pd;
8951 goto not_found;
8952 }
8953
8954 if (offset_in_cu_p (&cu->header, offset))
8955 {
8956 pd = find_partial_die_in_comp_unit (offset, cu);
8957 if (pd != NULL)
8958 return pd;
8959 }
8960
8961 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8962
8963 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8964 load_partial_comp_unit (per_cu, cu->objfile);
8965
8966 per_cu->cu->last_used = 0;
8967 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8968
8969 if (pd == NULL && per_cu->load_all_dies == 0)
8970 {
8971 struct cleanup *back_to;
8972 struct partial_die_info comp_unit_die;
8973 struct abbrev_info *abbrev;
8974 unsigned int bytes_read;
8975 char *info_ptr;
8976
8977 per_cu->load_all_dies = 1;
8978
8979 /* Re-read the DIEs. */
8980 back_to = make_cleanup (null_cleanup, 0);
8981 if (per_cu->cu->dwarf2_abbrevs == NULL)
8982 {
8983 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8984 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8985 }
8986 info_ptr = (dwarf2_per_objfile->info.buffer
8987 + per_cu->cu->header.offset
8988 + per_cu->cu->header.first_die_offset);
8989 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8990 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8991 per_cu->cu->objfile->obfd,
8992 dwarf2_per_objfile->info.buffer, info_ptr,
8993 per_cu->cu);
8994 if (comp_unit_die.has_children)
8995 load_partial_dies (per_cu->cu->objfile->obfd,
8996 dwarf2_per_objfile->info.buffer, info_ptr,
8997 0, per_cu->cu);
8998 do_cleanups (back_to);
8999
9000 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9001 }
9002
9003 not_found:
9004
9005 if (pd == NULL)
9006 internal_error (__FILE__, __LINE__,
9007 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
9008 offset, bfd_get_filename (cu->objfile->obfd));
9009 return pd;
9010 }
9011
9012 /* See if we can figure out if the class lives in a namespace. We do
9013 this by looking for a member function; its demangled name will
9014 contain namespace info, if there is any. */
9015
9016 static void
9017 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9018 struct dwarf2_cu *cu)
9019 {
9020 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9021 what template types look like, because the demangler
9022 frequently doesn't give the same name as the debug info. We
9023 could fix this by only using the demangled name to get the
9024 prefix (but see comment in read_structure_type). */
9025
9026 struct partial_die_info *real_pdi;
9027 struct partial_die_info *child_pdi;
9028
9029 /* If this DIE (this DIE's specification, if any) has a parent, then
9030 we should not do this. We'll prepend the parent's fully qualified
9031 name when we create the partial symbol. */
9032
9033 real_pdi = struct_pdi;
9034 while (real_pdi->has_specification)
9035 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9036
9037 if (real_pdi->die_parent != NULL)
9038 return;
9039
9040 for (child_pdi = struct_pdi->die_child;
9041 child_pdi != NULL;
9042 child_pdi = child_pdi->die_sibling)
9043 {
9044 if (child_pdi->tag == DW_TAG_subprogram
9045 && child_pdi->linkage_name != NULL)
9046 {
9047 char *actual_class_name
9048 = language_class_name_from_physname (cu->language_defn,
9049 child_pdi->linkage_name);
9050 if (actual_class_name != NULL)
9051 {
9052 struct_pdi->name
9053 = obsavestring (actual_class_name,
9054 strlen (actual_class_name),
9055 &cu->objfile->objfile_obstack);
9056 xfree (actual_class_name);
9057 }
9058 break;
9059 }
9060 }
9061 }
9062
9063 /* Adjust PART_DIE before generating a symbol for it. This function
9064 may set the is_external flag or change the DIE's name. */
9065
9066 static void
9067 fixup_partial_die (struct partial_die_info *part_die,
9068 struct dwarf2_cu *cu)
9069 {
9070 /* Once we've fixed up a die, there's no point in doing so again.
9071 This also avoids a memory leak if we were to call
9072 guess_partial_die_structure_name multiple times. */
9073 if (part_die->fixup_called)
9074 return;
9075
9076 /* If we found a reference attribute and the DIE has no name, try
9077 to find a name in the referred to DIE. */
9078
9079 if (part_die->name == NULL && part_die->has_specification)
9080 {
9081 struct partial_die_info *spec_die;
9082
9083 spec_die = find_partial_die (part_die->spec_offset, cu);
9084
9085 fixup_partial_die (spec_die, cu);
9086
9087 if (spec_die->name)
9088 {
9089 part_die->name = spec_die->name;
9090
9091 /* Copy DW_AT_external attribute if it is set. */
9092 if (spec_die->is_external)
9093 part_die->is_external = spec_die->is_external;
9094 }
9095 }
9096
9097 /* Set default names for some unnamed DIEs. */
9098
9099 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9100 part_die->name = "(anonymous namespace)";
9101
9102 /* If there is no parent die to provide a namespace, and there are
9103 children, see if we can determine the namespace from their linkage
9104 name.
9105 NOTE: We need to do this even if cu->has_namespace_info != 0.
9106 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9107 if (cu->language == language_cplus
9108 && dwarf2_per_objfile->types.asection != NULL
9109 && part_die->die_parent == NULL
9110 && part_die->has_children
9111 && (part_die->tag == DW_TAG_class_type
9112 || part_die->tag == DW_TAG_structure_type
9113 || part_die->tag == DW_TAG_union_type))
9114 guess_partial_die_structure_name (part_die, cu);
9115
9116 part_die->fixup_called = 1;
9117 }
9118
9119 /* Read an attribute value described by an attribute form. */
9120
9121 static gdb_byte *
9122 read_attribute_value (struct attribute *attr, unsigned form,
9123 bfd *abfd, gdb_byte *info_ptr,
9124 struct dwarf2_cu *cu)
9125 {
9126 struct comp_unit_head *cu_header = &cu->header;
9127 unsigned int bytes_read;
9128 struct dwarf_block *blk;
9129
9130 attr->form = form;
9131 switch (form)
9132 {
9133 case DW_FORM_ref_addr:
9134 if (cu->header.version == 2)
9135 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9136 else
9137 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9138 info_ptr += bytes_read;
9139 break;
9140 case DW_FORM_addr:
9141 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9142 info_ptr += bytes_read;
9143 break;
9144 case DW_FORM_block2:
9145 blk = dwarf_alloc_block (cu);
9146 blk->size = read_2_bytes (abfd, info_ptr);
9147 info_ptr += 2;
9148 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9149 info_ptr += blk->size;
9150 DW_BLOCK (attr) = blk;
9151 break;
9152 case DW_FORM_block4:
9153 blk = dwarf_alloc_block (cu);
9154 blk->size = read_4_bytes (abfd, info_ptr);
9155 info_ptr += 4;
9156 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9157 info_ptr += blk->size;
9158 DW_BLOCK (attr) = blk;
9159 break;
9160 case DW_FORM_data2:
9161 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9162 info_ptr += 2;
9163 break;
9164 case DW_FORM_data4:
9165 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9166 info_ptr += 4;
9167 break;
9168 case DW_FORM_data8:
9169 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9170 info_ptr += 8;
9171 break;
9172 case DW_FORM_sec_offset:
9173 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9174 info_ptr += bytes_read;
9175 break;
9176 case DW_FORM_string:
9177 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9178 DW_STRING_IS_CANONICAL (attr) = 0;
9179 info_ptr += bytes_read;
9180 break;
9181 case DW_FORM_strp:
9182 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9183 &bytes_read);
9184 DW_STRING_IS_CANONICAL (attr) = 0;
9185 info_ptr += bytes_read;
9186 break;
9187 case DW_FORM_exprloc:
9188 case DW_FORM_block:
9189 blk = dwarf_alloc_block (cu);
9190 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9191 info_ptr += bytes_read;
9192 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9193 info_ptr += blk->size;
9194 DW_BLOCK (attr) = blk;
9195 break;
9196 case DW_FORM_block1:
9197 blk = dwarf_alloc_block (cu);
9198 blk->size = read_1_byte (abfd, info_ptr);
9199 info_ptr += 1;
9200 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9201 info_ptr += blk->size;
9202 DW_BLOCK (attr) = blk;
9203 break;
9204 case DW_FORM_data1:
9205 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9206 info_ptr += 1;
9207 break;
9208 case DW_FORM_flag:
9209 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9210 info_ptr += 1;
9211 break;
9212 case DW_FORM_flag_present:
9213 DW_UNSND (attr) = 1;
9214 break;
9215 case DW_FORM_sdata:
9216 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9217 info_ptr += bytes_read;
9218 break;
9219 case DW_FORM_udata:
9220 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9221 info_ptr += bytes_read;
9222 break;
9223 case DW_FORM_ref1:
9224 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9225 info_ptr += 1;
9226 break;
9227 case DW_FORM_ref2:
9228 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9229 info_ptr += 2;
9230 break;
9231 case DW_FORM_ref4:
9232 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9233 info_ptr += 4;
9234 break;
9235 case DW_FORM_ref8:
9236 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9237 info_ptr += 8;
9238 break;
9239 case DW_FORM_sig8:
9240 /* Convert the signature to something we can record in DW_UNSND
9241 for later lookup.
9242 NOTE: This is NULL if the type wasn't found. */
9243 DW_SIGNATURED_TYPE (attr) =
9244 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9245 info_ptr += 8;
9246 break;
9247 case DW_FORM_ref_udata:
9248 DW_ADDR (attr) = (cu->header.offset
9249 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9250 info_ptr += bytes_read;
9251 break;
9252 case DW_FORM_indirect:
9253 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9254 info_ptr += bytes_read;
9255 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9256 break;
9257 default:
9258 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9259 dwarf_form_name (form),
9260 bfd_get_filename (abfd));
9261 }
9262
9263 /* We have seen instances where the compiler tried to emit a byte
9264 size attribute of -1 which ended up being encoded as an unsigned
9265 0xffffffff. Although 0xffffffff is technically a valid size value,
9266 an object of this size seems pretty unlikely so we can relatively
9267 safely treat these cases as if the size attribute was invalid and
9268 treat them as zero by default. */
9269 if (attr->name == DW_AT_byte_size
9270 && form == DW_FORM_data4
9271 && DW_UNSND (attr) >= 0xffffffff)
9272 {
9273 complaint
9274 (&symfile_complaints,
9275 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9276 hex_string (DW_UNSND (attr)));
9277 DW_UNSND (attr) = 0;
9278 }
9279
9280 return info_ptr;
9281 }
9282
9283 /* Read an attribute described by an abbreviated attribute. */
9284
9285 static gdb_byte *
9286 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9287 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9288 {
9289 attr->name = abbrev->name;
9290 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9291 }
9292
9293 /* read dwarf information from a buffer */
9294
9295 static unsigned int
9296 read_1_byte (bfd *abfd, gdb_byte *buf)
9297 {
9298 return bfd_get_8 (abfd, buf);
9299 }
9300
9301 static int
9302 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9303 {
9304 return bfd_get_signed_8 (abfd, buf);
9305 }
9306
9307 static unsigned int
9308 read_2_bytes (bfd *abfd, gdb_byte *buf)
9309 {
9310 return bfd_get_16 (abfd, buf);
9311 }
9312
9313 static int
9314 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9315 {
9316 return bfd_get_signed_16 (abfd, buf);
9317 }
9318
9319 static unsigned int
9320 read_4_bytes (bfd *abfd, gdb_byte *buf)
9321 {
9322 return bfd_get_32 (abfd, buf);
9323 }
9324
9325 static int
9326 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9327 {
9328 return bfd_get_signed_32 (abfd, buf);
9329 }
9330
9331 static ULONGEST
9332 read_8_bytes (bfd *abfd, gdb_byte *buf)
9333 {
9334 return bfd_get_64 (abfd, buf);
9335 }
9336
9337 static CORE_ADDR
9338 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9339 unsigned int *bytes_read)
9340 {
9341 struct comp_unit_head *cu_header = &cu->header;
9342 CORE_ADDR retval = 0;
9343
9344 if (cu_header->signed_addr_p)
9345 {
9346 switch (cu_header->addr_size)
9347 {
9348 case 2:
9349 retval = bfd_get_signed_16 (abfd, buf);
9350 break;
9351 case 4:
9352 retval = bfd_get_signed_32 (abfd, buf);
9353 break;
9354 case 8:
9355 retval = bfd_get_signed_64 (abfd, buf);
9356 break;
9357 default:
9358 internal_error (__FILE__, __LINE__,
9359 _("read_address: bad switch, signed [in module %s]"),
9360 bfd_get_filename (abfd));
9361 }
9362 }
9363 else
9364 {
9365 switch (cu_header->addr_size)
9366 {
9367 case 2:
9368 retval = bfd_get_16 (abfd, buf);
9369 break;
9370 case 4:
9371 retval = bfd_get_32 (abfd, buf);
9372 break;
9373 case 8:
9374 retval = bfd_get_64 (abfd, buf);
9375 break;
9376 default:
9377 internal_error (__FILE__, __LINE__,
9378 _("read_address: bad switch, unsigned [in module %s]"),
9379 bfd_get_filename (abfd));
9380 }
9381 }
9382
9383 *bytes_read = cu_header->addr_size;
9384 return retval;
9385 }
9386
9387 /* Read the initial length from a section. The (draft) DWARF 3
9388 specification allows the initial length to take up either 4 bytes
9389 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9390 bytes describe the length and all offsets will be 8 bytes in length
9391 instead of 4.
9392
9393 An older, non-standard 64-bit format is also handled by this
9394 function. The older format in question stores the initial length
9395 as an 8-byte quantity without an escape value. Lengths greater
9396 than 2^32 aren't very common which means that the initial 4 bytes
9397 is almost always zero. Since a length value of zero doesn't make
9398 sense for the 32-bit format, this initial zero can be considered to
9399 be an escape value which indicates the presence of the older 64-bit
9400 format. As written, the code can't detect (old format) lengths
9401 greater than 4GB. If it becomes necessary to handle lengths
9402 somewhat larger than 4GB, we could allow other small values (such
9403 as the non-sensical values of 1, 2, and 3) to also be used as
9404 escape values indicating the presence of the old format.
9405
9406 The value returned via bytes_read should be used to increment the
9407 relevant pointer after calling read_initial_length().
9408
9409 [ Note: read_initial_length() and read_offset() are based on the
9410 document entitled "DWARF Debugging Information Format", revision
9411 3, draft 8, dated November 19, 2001. This document was obtained
9412 from:
9413
9414 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9415
9416 This document is only a draft and is subject to change. (So beware.)
9417
9418 Details regarding the older, non-standard 64-bit format were
9419 determined empirically by examining 64-bit ELF files produced by
9420 the SGI toolchain on an IRIX 6.5 machine.
9421
9422 - Kevin, July 16, 2002
9423 ] */
9424
9425 static LONGEST
9426 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9427 {
9428 LONGEST length = bfd_get_32 (abfd, buf);
9429
9430 if (length == 0xffffffff)
9431 {
9432 length = bfd_get_64 (abfd, buf + 4);
9433 *bytes_read = 12;
9434 }
9435 else if (length == 0)
9436 {
9437 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
9438 length = bfd_get_64 (abfd, buf);
9439 *bytes_read = 8;
9440 }
9441 else
9442 {
9443 *bytes_read = 4;
9444 }
9445
9446 return length;
9447 }
9448
9449 /* Cover function for read_initial_length.
9450 Returns the length of the object at BUF, and stores the size of the
9451 initial length in *BYTES_READ and stores the size that offsets will be in
9452 *OFFSET_SIZE.
9453 If the initial length size is not equivalent to that specified in
9454 CU_HEADER then issue a complaint.
9455 This is useful when reading non-comp-unit headers. */
9456
9457 static LONGEST
9458 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9459 const struct comp_unit_head *cu_header,
9460 unsigned int *bytes_read,
9461 unsigned int *offset_size)
9462 {
9463 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9464
9465 gdb_assert (cu_header->initial_length_size == 4
9466 || cu_header->initial_length_size == 8
9467 || cu_header->initial_length_size == 12);
9468
9469 if (cu_header->initial_length_size != *bytes_read)
9470 complaint (&symfile_complaints,
9471 _("intermixed 32-bit and 64-bit DWARF sections"));
9472
9473 *offset_size = (*bytes_read == 4) ? 4 : 8;
9474 return length;
9475 }
9476
9477 /* Read an offset from the data stream. The size of the offset is
9478 given by cu_header->offset_size. */
9479
9480 static LONGEST
9481 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9482 unsigned int *bytes_read)
9483 {
9484 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9485
9486 *bytes_read = cu_header->offset_size;
9487 return offset;
9488 }
9489
9490 /* Read an offset from the data stream. */
9491
9492 static LONGEST
9493 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9494 {
9495 LONGEST retval = 0;
9496
9497 switch (offset_size)
9498 {
9499 case 4:
9500 retval = bfd_get_32 (abfd, buf);
9501 break;
9502 case 8:
9503 retval = bfd_get_64 (abfd, buf);
9504 break;
9505 default:
9506 internal_error (__FILE__, __LINE__,
9507 _("read_offset_1: bad switch [in module %s]"),
9508 bfd_get_filename (abfd));
9509 }
9510
9511 return retval;
9512 }
9513
9514 static gdb_byte *
9515 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9516 {
9517 /* If the size of a host char is 8 bits, we can return a pointer
9518 to the buffer, otherwise we have to copy the data to a buffer
9519 allocated on the temporary obstack. */
9520 gdb_assert (HOST_CHAR_BIT == 8);
9521 return buf;
9522 }
9523
9524 static char *
9525 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9526 {
9527 /* If the size of a host char is 8 bits, we can return a pointer
9528 to the string, otherwise we have to copy the string to a buffer
9529 allocated on the temporary obstack. */
9530 gdb_assert (HOST_CHAR_BIT == 8);
9531 if (*buf == '\0')
9532 {
9533 *bytes_read_ptr = 1;
9534 return NULL;
9535 }
9536 *bytes_read_ptr = strlen ((char *) buf) + 1;
9537 return (char *) buf;
9538 }
9539
9540 static char *
9541 read_indirect_string (bfd *abfd, gdb_byte *buf,
9542 const struct comp_unit_head *cu_header,
9543 unsigned int *bytes_read_ptr)
9544 {
9545 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9546
9547 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9548 if (dwarf2_per_objfile->str.buffer == NULL)
9549 {
9550 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9551 bfd_get_filename (abfd));
9552 return NULL;
9553 }
9554 if (str_offset >= dwarf2_per_objfile->str.size)
9555 {
9556 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9557 bfd_get_filename (abfd));
9558 return NULL;
9559 }
9560 gdb_assert (HOST_CHAR_BIT == 8);
9561 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9562 return NULL;
9563 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9564 }
9565
9566 static unsigned long
9567 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9568 {
9569 unsigned long result;
9570 unsigned int num_read;
9571 int i, shift;
9572 unsigned char byte;
9573
9574 result = 0;
9575 shift = 0;
9576 num_read = 0;
9577 i = 0;
9578 while (1)
9579 {
9580 byte = bfd_get_8 (abfd, buf);
9581 buf++;
9582 num_read++;
9583 result |= ((unsigned long)(byte & 127) << shift);
9584 if ((byte & 128) == 0)
9585 {
9586 break;
9587 }
9588 shift += 7;
9589 }
9590 *bytes_read_ptr = num_read;
9591 return result;
9592 }
9593
9594 static long
9595 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9596 {
9597 long result;
9598 int i, shift, num_read;
9599 unsigned char byte;
9600
9601 result = 0;
9602 shift = 0;
9603 num_read = 0;
9604 i = 0;
9605 while (1)
9606 {
9607 byte = bfd_get_8 (abfd, buf);
9608 buf++;
9609 num_read++;
9610 result |= ((long)(byte & 127) << shift);
9611 shift += 7;
9612 if ((byte & 128) == 0)
9613 {
9614 break;
9615 }
9616 }
9617 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9618 result |= -(((long)1) << shift);
9619 *bytes_read_ptr = num_read;
9620 return result;
9621 }
9622
9623 /* Return a pointer to just past the end of an LEB128 number in BUF. */
9624
9625 static gdb_byte *
9626 skip_leb128 (bfd *abfd, gdb_byte *buf)
9627 {
9628 int byte;
9629
9630 while (1)
9631 {
9632 byte = bfd_get_8 (abfd, buf);
9633 buf++;
9634 if ((byte & 128) == 0)
9635 return buf;
9636 }
9637 }
9638
9639 static void
9640 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9641 {
9642 switch (lang)
9643 {
9644 case DW_LANG_C89:
9645 case DW_LANG_C99:
9646 case DW_LANG_C:
9647 cu->language = language_c;
9648 break;
9649 case DW_LANG_C_plus_plus:
9650 cu->language = language_cplus;
9651 break;
9652 case DW_LANG_D:
9653 cu->language = language_d;
9654 break;
9655 case DW_LANG_Fortran77:
9656 case DW_LANG_Fortran90:
9657 case DW_LANG_Fortran95:
9658 cu->language = language_fortran;
9659 break;
9660 case DW_LANG_Mips_Assembler:
9661 cu->language = language_asm;
9662 break;
9663 case DW_LANG_Java:
9664 cu->language = language_java;
9665 break;
9666 case DW_LANG_Ada83:
9667 case DW_LANG_Ada95:
9668 cu->language = language_ada;
9669 break;
9670 case DW_LANG_Modula2:
9671 cu->language = language_m2;
9672 break;
9673 case DW_LANG_Pascal83:
9674 cu->language = language_pascal;
9675 break;
9676 case DW_LANG_ObjC:
9677 cu->language = language_objc;
9678 break;
9679 case DW_LANG_Cobol74:
9680 case DW_LANG_Cobol85:
9681 default:
9682 cu->language = language_minimal;
9683 break;
9684 }
9685 cu->language_defn = language_def (cu->language);
9686 }
9687
9688 /* Return the named attribute or NULL if not there. */
9689
9690 static struct attribute *
9691 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9692 {
9693 unsigned int i;
9694 struct attribute *spec = NULL;
9695
9696 for (i = 0; i < die->num_attrs; ++i)
9697 {
9698 if (die->attrs[i].name == name)
9699 return &die->attrs[i];
9700 if (die->attrs[i].name == DW_AT_specification
9701 || die->attrs[i].name == DW_AT_abstract_origin)
9702 spec = &die->attrs[i];
9703 }
9704
9705 if (spec)
9706 {
9707 die = follow_die_ref (die, spec, &cu);
9708 return dwarf2_attr (die, name, cu);
9709 }
9710
9711 return NULL;
9712 }
9713
9714 /* Return the named attribute or NULL if not there,
9715 but do not follow DW_AT_specification, etc.
9716 This is for use in contexts where we're reading .debug_types dies.
9717 Following DW_AT_specification, DW_AT_abstract_origin will take us
9718 back up the chain, and we want to go down. */
9719
9720 static struct attribute *
9721 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9722 struct dwarf2_cu *cu)
9723 {
9724 unsigned int i;
9725
9726 for (i = 0; i < die->num_attrs; ++i)
9727 if (die->attrs[i].name == name)
9728 return &die->attrs[i];
9729
9730 return NULL;
9731 }
9732
9733 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9734 and holds a non-zero value. This function should only be used for
9735 DW_FORM_flag or DW_FORM_flag_present attributes. */
9736
9737 static int
9738 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9739 {
9740 struct attribute *attr = dwarf2_attr (die, name, cu);
9741
9742 return (attr && DW_UNSND (attr));
9743 }
9744
9745 static int
9746 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9747 {
9748 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9749 which value is non-zero. However, we have to be careful with
9750 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9751 (via dwarf2_flag_true_p) follows this attribute. So we may
9752 end up accidently finding a declaration attribute that belongs
9753 to a different DIE referenced by the specification attribute,
9754 even though the given DIE does not have a declaration attribute. */
9755 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9756 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9757 }
9758
9759 /* Return the die giving the specification for DIE, if there is
9760 one. *SPEC_CU is the CU containing DIE on input, and the CU
9761 containing the return value on output. If there is no
9762 specification, but there is an abstract origin, that is
9763 returned. */
9764
9765 static struct die_info *
9766 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9767 {
9768 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9769 *spec_cu);
9770
9771 if (spec_attr == NULL)
9772 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9773
9774 if (spec_attr == NULL)
9775 return NULL;
9776 else
9777 return follow_die_ref (die, spec_attr, spec_cu);
9778 }
9779
9780 /* Free the line_header structure *LH, and any arrays and strings it
9781 refers to. */
9782 static void
9783 free_line_header (struct line_header *lh)
9784 {
9785 if (lh->standard_opcode_lengths)
9786 xfree (lh->standard_opcode_lengths);
9787
9788 /* Remember that all the lh->file_names[i].name pointers are
9789 pointers into debug_line_buffer, and don't need to be freed. */
9790 if (lh->file_names)
9791 xfree (lh->file_names);
9792
9793 /* Similarly for the include directory names. */
9794 if (lh->include_dirs)
9795 xfree (lh->include_dirs);
9796
9797 xfree (lh);
9798 }
9799
9800
9801 /* Add an entry to LH's include directory table. */
9802 static void
9803 add_include_dir (struct line_header *lh, char *include_dir)
9804 {
9805 /* Grow the array if necessary. */
9806 if (lh->include_dirs_size == 0)
9807 {
9808 lh->include_dirs_size = 1; /* for testing */
9809 lh->include_dirs = xmalloc (lh->include_dirs_size
9810 * sizeof (*lh->include_dirs));
9811 }
9812 else if (lh->num_include_dirs >= lh->include_dirs_size)
9813 {
9814 lh->include_dirs_size *= 2;
9815 lh->include_dirs = xrealloc (lh->include_dirs,
9816 (lh->include_dirs_size
9817 * sizeof (*lh->include_dirs)));
9818 }
9819
9820 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9821 }
9822
9823
9824 /* Add an entry to LH's file name table. */
9825 static void
9826 add_file_name (struct line_header *lh,
9827 char *name,
9828 unsigned int dir_index,
9829 unsigned int mod_time,
9830 unsigned int length)
9831 {
9832 struct file_entry *fe;
9833
9834 /* Grow the array if necessary. */
9835 if (lh->file_names_size == 0)
9836 {
9837 lh->file_names_size = 1; /* for testing */
9838 lh->file_names = xmalloc (lh->file_names_size
9839 * sizeof (*lh->file_names));
9840 }
9841 else if (lh->num_file_names >= lh->file_names_size)
9842 {
9843 lh->file_names_size *= 2;
9844 lh->file_names = xrealloc (lh->file_names,
9845 (lh->file_names_size
9846 * sizeof (*lh->file_names)));
9847 }
9848
9849 fe = &lh->file_names[lh->num_file_names++];
9850 fe->name = name;
9851 fe->dir_index = dir_index;
9852 fe->mod_time = mod_time;
9853 fe->length = length;
9854 fe->included_p = 0;
9855 fe->symtab = NULL;
9856 }
9857
9858
9859 /* Read the statement program header starting at OFFSET in
9860 .debug_line, according to the endianness of ABFD. Return a pointer
9861 to a struct line_header, allocated using xmalloc.
9862
9863 NOTE: the strings in the include directory and file name tables of
9864 the returned object point into debug_line_buffer, and must not be
9865 freed. */
9866 static struct line_header *
9867 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9868 struct dwarf2_cu *cu)
9869 {
9870 struct cleanup *back_to;
9871 struct line_header *lh;
9872 gdb_byte *line_ptr;
9873 unsigned int bytes_read, offset_size;
9874 int i;
9875 char *cur_dir, *cur_file;
9876
9877 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9878 if (dwarf2_per_objfile->line.buffer == NULL)
9879 {
9880 complaint (&symfile_complaints, _("missing .debug_line section"));
9881 return 0;
9882 }
9883
9884 /* Make sure that at least there's room for the total_length field.
9885 That could be 12 bytes long, but we're just going to fudge that. */
9886 if (offset + 4 >= dwarf2_per_objfile->line.size)
9887 {
9888 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9889 return 0;
9890 }
9891
9892 lh = xmalloc (sizeof (*lh));
9893 memset (lh, 0, sizeof (*lh));
9894 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9895 (void *) lh);
9896
9897 line_ptr = dwarf2_per_objfile->line.buffer + offset;
9898
9899 /* Read in the header. */
9900 lh->total_length =
9901 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9902 &bytes_read, &offset_size);
9903 line_ptr += bytes_read;
9904 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9905 + dwarf2_per_objfile->line.size))
9906 {
9907 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9908 return 0;
9909 }
9910 lh->statement_program_end = line_ptr + lh->total_length;
9911 lh->version = read_2_bytes (abfd, line_ptr);
9912 line_ptr += 2;
9913 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9914 line_ptr += offset_size;
9915 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9916 line_ptr += 1;
9917 if (lh->version >= 4)
9918 {
9919 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9920 line_ptr += 1;
9921 }
9922 else
9923 lh->maximum_ops_per_instruction = 1;
9924
9925 if (lh->maximum_ops_per_instruction == 0)
9926 {
9927 lh->maximum_ops_per_instruction = 1;
9928 complaint (&symfile_complaints,
9929 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9930 }
9931
9932 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9933 line_ptr += 1;
9934 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9935 line_ptr += 1;
9936 lh->line_range = read_1_byte (abfd, line_ptr);
9937 line_ptr += 1;
9938 lh->opcode_base = read_1_byte (abfd, line_ptr);
9939 line_ptr += 1;
9940 lh->standard_opcode_lengths
9941 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9942
9943 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9944 for (i = 1; i < lh->opcode_base; ++i)
9945 {
9946 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9947 line_ptr += 1;
9948 }
9949
9950 /* Read directory table. */
9951 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9952 {
9953 line_ptr += bytes_read;
9954 add_include_dir (lh, cur_dir);
9955 }
9956 line_ptr += bytes_read;
9957
9958 /* Read file name table. */
9959 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9960 {
9961 unsigned int dir_index, mod_time, length;
9962
9963 line_ptr += bytes_read;
9964 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9965 line_ptr += bytes_read;
9966 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9967 line_ptr += bytes_read;
9968 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9969 line_ptr += bytes_read;
9970
9971 add_file_name (lh, cur_file, dir_index, mod_time, length);
9972 }
9973 line_ptr += bytes_read;
9974 lh->statement_program_start = line_ptr;
9975
9976 if (line_ptr > (dwarf2_per_objfile->line.buffer
9977 + dwarf2_per_objfile->line.size))
9978 complaint (&symfile_complaints,
9979 _("line number info header doesn't fit in `.debug_line' section"));
9980
9981 discard_cleanups (back_to);
9982 return lh;
9983 }
9984
9985 /* This function exists to work around a bug in certain compilers
9986 (particularly GCC 2.95), in which the first line number marker of a
9987 function does not show up until after the prologue, right before
9988 the second line number marker. This function shifts ADDRESS down
9989 to the beginning of the function if necessary, and is called on
9990 addresses passed to record_line. */
9991
9992 static CORE_ADDR
9993 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9994 {
9995 struct function_range *fn;
9996
9997 /* Find the function_range containing address. */
9998 if (!cu->first_fn)
9999 return address;
10000
10001 if (!cu->cached_fn)
10002 cu->cached_fn = cu->first_fn;
10003
10004 fn = cu->cached_fn;
10005 while (fn)
10006 if (fn->lowpc <= address && fn->highpc > address)
10007 goto found;
10008 else
10009 fn = fn->next;
10010
10011 fn = cu->first_fn;
10012 while (fn && fn != cu->cached_fn)
10013 if (fn->lowpc <= address && fn->highpc > address)
10014 goto found;
10015 else
10016 fn = fn->next;
10017
10018 return address;
10019
10020 found:
10021 if (fn->seen_line)
10022 return address;
10023 if (address != fn->lowpc)
10024 complaint (&symfile_complaints,
10025 _("misplaced first line number at 0x%lx for '%s'"),
10026 (unsigned long) address, fn->name);
10027 fn->seen_line = 1;
10028 return fn->lowpc;
10029 }
10030
10031 /* Subroutine of dwarf_decode_lines to simplify it.
10032 Return the file name of the psymtab for included file FILE_INDEX
10033 in line header LH of PST.
10034 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10035 If space for the result is malloc'd, it will be freed by a cleanup.
10036 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10037
10038 static char *
10039 psymtab_include_file_name (const struct line_header *lh, int file_index,
10040 const struct partial_symtab *pst,
10041 const char *comp_dir)
10042 {
10043 const struct file_entry fe = lh->file_names [file_index];
10044 char *include_name = fe.name;
10045 char *include_name_to_compare = include_name;
10046 char *dir_name = NULL;
10047 const char *pst_filename;
10048 char *copied_name = NULL;
10049 int file_is_pst;
10050
10051 if (fe.dir_index)
10052 dir_name = lh->include_dirs[fe.dir_index - 1];
10053
10054 if (!IS_ABSOLUTE_PATH (include_name)
10055 && (dir_name != NULL || comp_dir != NULL))
10056 {
10057 /* Avoid creating a duplicate psymtab for PST.
10058 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10059 Before we do the comparison, however, we need to account
10060 for DIR_NAME and COMP_DIR.
10061 First prepend dir_name (if non-NULL). If we still don't
10062 have an absolute path prepend comp_dir (if non-NULL).
10063 However, the directory we record in the include-file's
10064 psymtab does not contain COMP_DIR (to match the
10065 corresponding symtab(s)).
10066
10067 Example:
10068
10069 bash$ cd /tmp
10070 bash$ gcc -g ./hello.c
10071 include_name = "hello.c"
10072 dir_name = "."
10073 DW_AT_comp_dir = comp_dir = "/tmp"
10074 DW_AT_name = "./hello.c" */
10075
10076 if (dir_name != NULL)
10077 {
10078 include_name = concat (dir_name, SLASH_STRING,
10079 include_name, (char *)NULL);
10080 include_name_to_compare = include_name;
10081 make_cleanup (xfree, include_name);
10082 }
10083 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10084 {
10085 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10086 include_name, (char *)NULL);
10087 }
10088 }
10089
10090 pst_filename = pst->filename;
10091 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10092 {
10093 copied_name = concat (pst->dirname, SLASH_STRING,
10094 pst_filename, (char *)NULL);
10095 pst_filename = copied_name;
10096 }
10097
10098 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10099
10100 if (include_name_to_compare != include_name)
10101 xfree (include_name_to_compare);
10102 if (copied_name != NULL)
10103 xfree (copied_name);
10104
10105 if (file_is_pst)
10106 return NULL;
10107 return include_name;
10108 }
10109
10110 /* Decode the Line Number Program (LNP) for the given line_header
10111 structure and CU. The actual information extracted and the type
10112 of structures created from the LNP depends on the value of PST.
10113
10114 1. If PST is NULL, then this procedure uses the data from the program
10115 to create all necessary symbol tables, and their linetables.
10116
10117 2. If PST is not NULL, this procedure reads the program to determine
10118 the list of files included by the unit represented by PST, and
10119 builds all the associated partial symbol tables.
10120
10121 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10122 It is used for relative paths in the line table.
10123 NOTE: When processing partial symtabs (pst != NULL),
10124 comp_dir == pst->dirname.
10125
10126 NOTE: It is important that psymtabs have the same file name (via strcmp)
10127 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10128 symtab we don't use it in the name of the psymtabs we create.
10129 E.g. expand_line_sal requires this when finding psymtabs to expand.
10130 A good testcase for this is mb-inline.exp. */
10131
10132 static void
10133 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10134 struct dwarf2_cu *cu, struct partial_symtab *pst)
10135 {
10136 gdb_byte *line_ptr, *extended_end;
10137 gdb_byte *line_end;
10138 unsigned int bytes_read, extended_len;
10139 unsigned char op_code, extended_op, adj_opcode;
10140 CORE_ADDR baseaddr;
10141 struct objfile *objfile = cu->objfile;
10142 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10143 const int decode_for_pst_p = (pst != NULL);
10144 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10145
10146 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10147
10148 line_ptr = lh->statement_program_start;
10149 line_end = lh->statement_program_end;
10150
10151 /* Read the statement sequences until there's nothing left. */
10152 while (line_ptr < line_end)
10153 {
10154 /* state machine registers */
10155 CORE_ADDR address = 0;
10156 unsigned int file = 1;
10157 unsigned int line = 1;
10158 unsigned int column = 0;
10159 int is_stmt = lh->default_is_stmt;
10160 int basic_block = 0;
10161 int end_sequence = 0;
10162 CORE_ADDR addr;
10163 unsigned char op_index = 0;
10164
10165 if (!decode_for_pst_p && lh->num_file_names >= file)
10166 {
10167 /* Start a subfile for the current file of the state machine. */
10168 /* lh->include_dirs and lh->file_names are 0-based, but the
10169 directory and file name numbers in the statement program
10170 are 1-based. */
10171 struct file_entry *fe = &lh->file_names[file - 1];
10172 char *dir = NULL;
10173
10174 if (fe->dir_index)
10175 dir = lh->include_dirs[fe->dir_index - 1];
10176
10177 dwarf2_start_subfile (fe->name, dir, comp_dir);
10178 }
10179
10180 /* Decode the table. */
10181 while (!end_sequence)
10182 {
10183 op_code = read_1_byte (abfd, line_ptr);
10184 line_ptr += 1;
10185 if (line_ptr > line_end)
10186 {
10187 dwarf2_debug_line_missing_end_sequence_complaint ();
10188 break;
10189 }
10190
10191 if (op_code >= lh->opcode_base)
10192 {
10193 /* Special operand. */
10194 adj_opcode = op_code - lh->opcode_base;
10195 address += (((op_index + (adj_opcode / lh->line_range))
10196 / lh->maximum_ops_per_instruction)
10197 * lh->minimum_instruction_length);
10198 op_index = ((op_index + (adj_opcode / lh->line_range))
10199 % lh->maximum_ops_per_instruction);
10200 line += lh->line_base + (adj_opcode % lh->line_range);
10201 if (lh->num_file_names < file || file == 0)
10202 dwarf2_debug_line_missing_file_complaint ();
10203 /* For now we ignore lines not starting on an
10204 instruction boundary. */
10205 else if (op_index == 0)
10206 {
10207 lh->file_names[file - 1].included_p = 1;
10208 if (!decode_for_pst_p && is_stmt)
10209 {
10210 if (last_subfile != current_subfile)
10211 {
10212 addr = gdbarch_addr_bits_remove (gdbarch, address);
10213 if (last_subfile)
10214 record_line (last_subfile, 0, addr);
10215 last_subfile = current_subfile;
10216 }
10217 /* Append row to matrix using current values. */
10218 addr = check_cu_functions (address, cu);
10219 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10220 record_line (current_subfile, line, addr);
10221 }
10222 }
10223 basic_block = 0;
10224 }
10225 else switch (op_code)
10226 {
10227 case DW_LNS_extended_op:
10228 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10229 line_ptr += bytes_read;
10230 extended_end = line_ptr + extended_len;
10231 extended_op = read_1_byte (abfd, line_ptr);
10232 line_ptr += 1;
10233 switch (extended_op)
10234 {
10235 case DW_LNE_end_sequence:
10236 end_sequence = 1;
10237 break;
10238 case DW_LNE_set_address:
10239 address = read_address (abfd, line_ptr, cu, &bytes_read);
10240 op_index = 0;
10241 line_ptr += bytes_read;
10242 address += baseaddr;
10243 break;
10244 case DW_LNE_define_file:
10245 {
10246 char *cur_file;
10247 unsigned int dir_index, mod_time, length;
10248
10249 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
10250 line_ptr += bytes_read;
10251 dir_index =
10252 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10253 line_ptr += bytes_read;
10254 mod_time =
10255 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10256 line_ptr += bytes_read;
10257 length =
10258 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10259 line_ptr += bytes_read;
10260 add_file_name (lh, cur_file, dir_index, mod_time, length);
10261 }
10262 break;
10263 case DW_LNE_set_discriminator:
10264 /* The discriminator is not interesting to the debugger;
10265 just ignore it. */
10266 line_ptr = extended_end;
10267 break;
10268 default:
10269 complaint (&symfile_complaints,
10270 _("mangled .debug_line section"));
10271 return;
10272 }
10273 /* Make sure that we parsed the extended op correctly. If e.g.
10274 we expected a different address size than the producer used,
10275 we may have read the wrong number of bytes. */
10276 if (line_ptr != extended_end)
10277 {
10278 complaint (&symfile_complaints,
10279 _("mangled .debug_line section"));
10280 return;
10281 }
10282 break;
10283 case DW_LNS_copy:
10284 if (lh->num_file_names < file || file == 0)
10285 dwarf2_debug_line_missing_file_complaint ();
10286 else
10287 {
10288 lh->file_names[file - 1].included_p = 1;
10289 if (!decode_for_pst_p && is_stmt)
10290 {
10291 if (last_subfile != current_subfile)
10292 {
10293 addr = gdbarch_addr_bits_remove (gdbarch, address);
10294 if (last_subfile)
10295 record_line (last_subfile, 0, addr);
10296 last_subfile = current_subfile;
10297 }
10298 addr = check_cu_functions (address, cu);
10299 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10300 record_line (current_subfile, line, addr);
10301 }
10302 }
10303 basic_block = 0;
10304 break;
10305 case DW_LNS_advance_pc:
10306 {
10307 CORE_ADDR adjust
10308 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10309
10310 address += (((op_index + adjust)
10311 / lh->maximum_ops_per_instruction)
10312 * lh->minimum_instruction_length);
10313 op_index = ((op_index + adjust)
10314 % lh->maximum_ops_per_instruction);
10315 line_ptr += bytes_read;
10316 }
10317 break;
10318 case DW_LNS_advance_line:
10319 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10320 line_ptr += bytes_read;
10321 break;
10322 case DW_LNS_set_file:
10323 {
10324 /* The arrays lh->include_dirs and lh->file_names are
10325 0-based, but the directory and file name numbers in
10326 the statement program are 1-based. */
10327 struct file_entry *fe;
10328 char *dir = NULL;
10329
10330 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10331 line_ptr += bytes_read;
10332 if (lh->num_file_names < file || file == 0)
10333 dwarf2_debug_line_missing_file_complaint ();
10334 else
10335 {
10336 fe = &lh->file_names[file - 1];
10337 if (fe->dir_index)
10338 dir = lh->include_dirs[fe->dir_index - 1];
10339 if (!decode_for_pst_p)
10340 {
10341 last_subfile = current_subfile;
10342 dwarf2_start_subfile (fe->name, dir, comp_dir);
10343 }
10344 }
10345 }
10346 break;
10347 case DW_LNS_set_column:
10348 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10349 line_ptr += bytes_read;
10350 break;
10351 case DW_LNS_negate_stmt:
10352 is_stmt = (!is_stmt);
10353 break;
10354 case DW_LNS_set_basic_block:
10355 basic_block = 1;
10356 break;
10357 /* Add to the address register of the state machine the
10358 address increment value corresponding to special opcode
10359 255. I.e., this value is scaled by the minimum
10360 instruction length since special opcode 255 would have
10361 scaled the the increment. */
10362 case DW_LNS_const_add_pc:
10363 {
10364 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10365
10366 address += (((op_index + adjust)
10367 / lh->maximum_ops_per_instruction)
10368 * lh->minimum_instruction_length);
10369 op_index = ((op_index + adjust)
10370 % lh->maximum_ops_per_instruction);
10371 }
10372 break;
10373 case DW_LNS_fixed_advance_pc:
10374 address += read_2_bytes (abfd, line_ptr);
10375 op_index = 0;
10376 line_ptr += 2;
10377 break;
10378 default:
10379 {
10380 /* Unknown standard opcode, ignore it. */
10381 int i;
10382
10383 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10384 {
10385 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10386 line_ptr += bytes_read;
10387 }
10388 }
10389 }
10390 }
10391 if (lh->num_file_names < file || file == 0)
10392 dwarf2_debug_line_missing_file_complaint ();
10393 else
10394 {
10395 lh->file_names[file - 1].included_p = 1;
10396 if (!decode_for_pst_p)
10397 {
10398 addr = gdbarch_addr_bits_remove (gdbarch, address);
10399 record_line (current_subfile, 0, addr);
10400 }
10401 }
10402 }
10403
10404 if (decode_for_pst_p)
10405 {
10406 int file_index;
10407
10408 /* Now that we're done scanning the Line Header Program, we can
10409 create the psymtab of each included file. */
10410 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10411 if (lh->file_names[file_index].included_p == 1)
10412 {
10413 char *include_name =
10414 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10415 if (include_name != NULL)
10416 dwarf2_create_include_psymtab (include_name, pst, objfile);
10417 }
10418 }
10419 else
10420 {
10421 /* Make sure a symtab is created for every file, even files
10422 which contain only variables (i.e. no code with associated
10423 line numbers). */
10424
10425 int i;
10426 struct file_entry *fe;
10427
10428 for (i = 0; i < lh->num_file_names; i++)
10429 {
10430 char *dir = NULL;
10431
10432 fe = &lh->file_names[i];
10433 if (fe->dir_index)
10434 dir = lh->include_dirs[fe->dir_index - 1];
10435 dwarf2_start_subfile (fe->name, dir, comp_dir);
10436
10437 /* Skip the main file; we don't need it, and it must be
10438 allocated last, so that it will show up before the
10439 non-primary symtabs in the objfile's symtab list. */
10440 if (current_subfile == first_subfile)
10441 continue;
10442
10443 if (current_subfile->symtab == NULL)
10444 current_subfile->symtab = allocate_symtab (current_subfile->name,
10445 cu->objfile);
10446 fe->symtab = current_subfile->symtab;
10447 }
10448 }
10449 }
10450
10451 /* Start a subfile for DWARF. FILENAME is the name of the file and
10452 DIRNAME the name of the source directory which contains FILENAME
10453 or NULL if not known. COMP_DIR is the compilation directory for the
10454 linetable's compilation unit or NULL if not known.
10455 This routine tries to keep line numbers from identical absolute and
10456 relative file names in a common subfile.
10457
10458 Using the `list' example from the GDB testsuite, which resides in
10459 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10460 of /srcdir/list0.c yields the following debugging information for list0.c:
10461
10462 DW_AT_name: /srcdir/list0.c
10463 DW_AT_comp_dir: /compdir
10464 files.files[0].name: list0.h
10465 files.files[0].dir: /srcdir
10466 files.files[1].name: list0.c
10467 files.files[1].dir: /srcdir
10468
10469 The line number information for list0.c has to end up in a single
10470 subfile, so that `break /srcdir/list0.c:1' works as expected.
10471 start_subfile will ensure that this happens provided that we pass the
10472 concatenation of files.files[1].dir and files.files[1].name as the
10473 subfile's name. */
10474
10475 static void
10476 dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
10477 {
10478 char *fullname;
10479
10480 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10481 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10482 second argument to start_subfile. To be consistent, we do the
10483 same here. In order not to lose the line information directory,
10484 we concatenate it to the filename when it makes sense.
10485 Note that the Dwarf3 standard says (speaking of filenames in line
10486 information): ``The directory index is ignored for file names
10487 that represent full path names''. Thus ignoring dirname in the
10488 `else' branch below isn't an issue. */
10489
10490 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10491 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10492 else
10493 fullname = filename;
10494
10495 start_subfile (fullname, comp_dir);
10496
10497 if (fullname != filename)
10498 xfree (fullname);
10499 }
10500
10501 static void
10502 var_decode_location (struct attribute *attr, struct symbol *sym,
10503 struct dwarf2_cu *cu)
10504 {
10505 struct objfile *objfile = cu->objfile;
10506 struct comp_unit_head *cu_header = &cu->header;
10507
10508 /* NOTE drow/2003-01-30: There used to be a comment and some special
10509 code here to turn a symbol with DW_AT_external and a
10510 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10511 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10512 with some versions of binutils) where shared libraries could have
10513 relocations against symbols in their debug information - the
10514 minimal symbol would have the right address, but the debug info
10515 would not. It's no longer necessary, because we will explicitly
10516 apply relocations when we read in the debug information now. */
10517
10518 /* A DW_AT_location attribute with no contents indicates that a
10519 variable has been optimized away. */
10520 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10521 {
10522 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10523 return;
10524 }
10525
10526 /* Handle one degenerate form of location expression specially, to
10527 preserve GDB's previous behavior when section offsets are
10528 specified. If this is just a DW_OP_addr then mark this symbol
10529 as LOC_STATIC. */
10530
10531 if (attr_form_is_block (attr)
10532 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10533 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10534 {
10535 unsigned int dummy;
10536
10537 SYMBOL_VALUE_ADDRESS (sym) =
10538 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10539 SYMBOL_CLASS (sym) = LOC_STATIC;
10540 fixup_symbol_section (sym, objfile);
10541 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10542 SYMBOL_SECTION (sym));
10543 return;
10544 }
10545
10546 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10547 expression evaluator, and use LOC_COMPUTED only when necessary
10548 (i.e. when the value of a register or memory location is
10549 referenced, or a thread-local block, etc.). Then again, it might
10550 not be worthwhile. I'm assuming that it isn't unless performance
10551 or memory numbers show me otherwise. */
10552
10553 dwarf2_symbol_mark_computed (attr, sym, cu);
10554 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10555 }
10556
10557 /* Given a pointer to a DWARF information entry, figure out if we need
10558 to make a symbol table entry for it, and if so, create a new entry
10559 and return a pointer to it.
10560 If TYPE is NULL, determine symbol type from the die, otherwise
10561 used the passed type.
10562 If SPACE is not NULL, use it to hold the new symbol. If it is
10563 NULL, allocate a new symbol on the objfile's obstack. */
10564
10565 static struct symbol *
10566 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10567 struct symbol *space)
10568 {
10569 struct objfile *objfile = cu->objfile;
10570 struct symbol *sym = NULL;
10571 char *name;
10572 struct attribute *attr = NULL;
10573 struct attribute *attr2 = NULL;
10574 CORE_ADDR baseaddr;
10575 struct pending **list_to_add = NULL;
10576
10577 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10578
10579 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10580
10581 name = dwarf2_name (die, cu);
10582 if (name)
10583 {
10584 const char *linkagename;
10585 int suppress_add = 0;
10586
10587 if (space)
10588 sym = space;
10589 else
10590 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10591 OBJSTAT (objfile, n_syms++);
10592
10593 /* Cache this symbol's name and the name's demangled form (if any). */
10594 SYMBOL_SET_LANGUAGE (sym, cu->language);
10595 linkagename = dwarf2_physname (name, die, cu);
10596 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10597
10598 /* Fortran does not have mangling standard and the mangling does differ
10599 between gfortran, iFort etc. */
10600 if (cu->language == language_fortran
10601 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10602 symbol_set_demangled_name (&(sym->ginfo),
10603 (char *) dwarf2_full_name (name, die, cu),
10604 NULL);
10605
10606 /* Default assumptions.
10607 Use the passed type or decode it from the die. */
10608 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10609 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10610 if (type != NULL)
10611 SYMBOL_TYPE (sym) = type;
10612 else
10613 SYMBOL_TYPE (sym) = die_type (die, cu);
10614 attr = dwarf2_attr (die,
10615 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10616 cu);
10617 if (attr)
10618 {
10619 SYMBOL_LINE (sym) = DW_UNSND (attr);
10620 }
10621
10622 attr = dwarf2_attr (die,
10623 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10624 cu);
10625 if (attr)
10626 {
10627 int file_index = DW_UNSND (attr);
10628
10629 if (cu->line_header == NULL
10630 || file_index > cu->line_header->num_file_names)
10631 complaint (&symfile_complaints,
10632 _("file index out of range"));
10633 else if (file_index > 0)
10634 {
10635 struct file_entry *fe;
10636
10637 fe = &cu->line_header->file_names[file_index - 1];
10638 SYMBOL_SYMTAB (sym) = fe->symtab;
10639 }
10640 }
10641
10642 switch (die->tag)
10643 {
10644 case DW_TAG_label:
10645 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10646 if (attr)
10647 {
10648 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10649 }
10650 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10651 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10652 SYMBOL_CLASS (sym) = LOC_LABEL;
10653 add_symbol_to_list (sym, cu->list_in_scope);
10654 break;
10655 case DW_TAG_subprogram:
10656 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10657 finish_block. */
10658 SYMBOL_CLASS (sym) = LOC_BLOCK;
10659 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10660 if ((attr2 && (DW_UNSND (attr2) != 0))
10661 || cu->language == language_ada)
10662 {
10663 /* Subprograms marked external are stored as a global symbol.
10664 Ada subprograms, whether marked external or not, are always
10665 stored as a global symbol, because we want to be able to
10666 access them globally. For instance, we want to be able
10667 to break on a nested subprogram without having to
10668 specify the context. */
10669 list_to_add = &global_symbols;
10670 }
10671 else
10672 {
10673 list_to_add = cu->list_in_scope;
10674 }
10675 break;
10676 case DW_TAG_inlined_subroutine:
10677 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10678 finish_block. */
10679 SYMBOL_CLASS (sym) = LOC_BLOCK;
10680 SYMBOL_INLINED (sym) = 1;
10681 /* Do not add the symbol to any lists. It will be found via
10682 BLOCK_FUNCTION from the blockvector. */
10683 break;
10684 case DW_TAG_template_value_param:
10685 suppress_add = 1;
10686 /* Fall through. */
10687 case DW_TAG_constant:
10688 case DW_TAG_variable:
10689 case DW_TAG_member:
10690 /* Compilation with minimal debug info may result in variables
10691 with missing type entries. Change the misleading `void' type
10692 to something sensible. */
10693 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10694 SYMBOL_TYPE (sym)
10695 = objfile_type (objfile)->nodebug_data_symbol;
10696
10697 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10698 /* In the case of DW_TAG_member, we should only be called for
10699 static const members. */
10700 if (die->tag == DW_TAG_member)
10701 {
10702 /* dwarf2_add_field uses die_is_declaration,
10703 so we do the same. */
10704 gdb_assert (die_is_declaration (die, cu));
10705 gdb_assert (attr);
10706 }
10707 if (attr)
10708 {
10709 dwarf2_const_value (attr, sym, cu);
10710 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10711 if (!suppress_add)
10712 {
10713 if (attr2 && (DW_UNSND (attr2) != 0))
10714 list_to_add = &global_symbols;
10715 else
10716 list_to_add = cu->list_in_scope;
10717 }
10718 break;
10719 }
10720 attr = dwarf2_attr (die, DW_AT_location, cu);
10721 if (attr)
10722 {
10723 var_decode_location (attr, sym, cu);
10724 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10725 if (SYMBOL_CLASS (sym) == LOC_STATIC
10726 && SYMBOL_VALUE_ADDRESS (sym) == 0
10727 && !dwarf2_per_objfile->has_section_at_zero)
10728 {
10729 /* When a static variable is eliminated by the linker,
10730 the corresponding debug information is not stripped
10731 out, but the variable address is set to null;
10732 do not add such variables into symbol table. */
10733 }
10734 else if (attr2 && (DW_UNSND (attr2) != 0))
10735 {
10736 /* Workaround gfortran PR debug/40040 - it uses
10737 DW_AT_location for variables in -fPIC libraries which may
10738 get overriden by other libraries/executable and get
10739 a different address. Resolve it by the minimal symbol
10740 which may come from inferior's executable using copy
10741 relocation. Make this workaround only for gfortran as for
10742 other compilers GDB cannot guess the minimal symbol
10743 Fortran mangling kind. */
10744 if (cu->language == language_fortran && die->parent
10745 && die->parent->tag == DW_TAG_module
10746 && cu->producer
10747 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10748 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10749
10750 /* A variable with DW_AT_external is never static,
10751 but it may be block-scoped. */
10752 list_to_add = (cu->list_in_scope == &file_symbols
10753 ? &global_symbols : cu->list_in_scope);
10754 }
10755 else
10756 list_to_add = cu->list_in_scope;
10757 }
10758 else
10759 {
10760 /* We do not know the address of this symbol.
10761 If it is an external symbol and we have type information
10762 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10763 The address of the variable will then be determined from
10764 the minimal symbol table whenever the variable is
10765 referenced. */
10766 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10767 if (attr2 && (DW_UNSND (attr2) != 0)
10768 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10769 {
10770 /* A variable with DW_AT_external is never static, but it
10771 may be block-scoped. */
10772 list_to_add = (cu->list_in_scope == &file_symbols
10773 ? &global_symbols : cu->list_in_scope);
10774
10775 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10776 }
10777 else if (!die_is_declaration (die, cu))
10778 {
10779 /* Use the default LOC_OPTIMIZED_OUT class. */
10780 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10781 if (!suppress_add)
10782 list_to_add = cu->list_in_scope;
10783 }
10784 }
10785 break;
10786 case DW_TAG_formal_parameter:
10787 /* If we are inside a function, mark this as an argument. If
10788 not, we might be looking at an argument to an inlined function
10789 when we do not have enough information to show inlined frames;
10790 pretend it's a local variable in that case so that the user can
10791 still see it. */
10792 if (context_stack_depth > 0
10793 && context_stack[context_stack_depth - 1].name != NULL)
10794 SYMBOL_IS_ARGUMENT (sym) = 1;
10795 attr = dwarf2_attr (die, DW_AT_location, cu);
10796 if (attr)
10797 {
10798 var_decode_location (attr, sym, cu);
10799 }
10800 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10801 if (attr)
10802 {
10803 dwarf2_const_value (attr, sym, cu);
10804 }
10805 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10806 if (attr && DW_UNSND (attr))
10807 {
10808 struct type *ref_type;
10809
10810 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10811 SYMBOL_TYPE (sym) = ref_type;
10812 }
10813
10814 list_to_add = cu->list_in_scope;
10815 break;
10816 case DW_TAG_unspecified_parameters:
10817 /* From varargs functions; gdb doesn't seem to have any
10818 interest in this information, so just ignore it for now.
10819 (FIXME?) */
10820 break;
10821 case DW_TAG_template_type_param:
10822 suppress_add = 1;
10823 /* Fall through. */
10824 case DW_TAG_class_type:
10825 case DW_TAG_interface_type:
10826 case DW_TAG_structure_type:
10827 case DW_TAG_union_type:
10828 case DW_TAG_set_type:
10829 case DW_TAG_enumeration_type:
10830 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10831 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10832
10833 {
10834 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10835 really ever be static objects: otherwise, if you try
10836 to, say, break of a class's method and you're in a file
10837 which doesn't mention that class, it won't work unless
10838 the check for all static symbols in lookup_symbol_aux
10839 saves you. See the OtherFileClass tests in
10840 gdb.c++/namespace.exp. */
10841
10842 if (!suppress_add)
10843 {
10844 list_to_add = (cu->list_in_scope == &file_symbols
10845 && (cu->language == language_cplus
10846 || cu->language == language_java)
10847 ? &global_symbols : cu->list_in_scope);
10848
10849 /* The semantics of C++ state that "struct foo {
10850 ... }" also defines a typedef for "foo". A Java
10851 class declaration also defines a typedef for the
10852 class. */
10853 if (cu->language == language_cplus
10854 || cu->language == language_java
10855 || cu->language == language_ada)
10856 {
10857 /* The symbol's name is already allocated along
10858 with this objfile, so we don't need to
10859 duplicate it for the type. */
10860 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10861 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10862 }
10863 }
10864 }
10865 break;
10866 case DW_TAG_typedef:
10867 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10868 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10869 list_to_add = cu->list_in_scope;
10870 break;
10871 case DW_TAG_base_type:
10872 case DW_TAG_subrange_type:
10873 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10874 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10875 list_to_add = cu->list_in_scope;
10876 break;
10877 case DW_TAG_enumerator:
10878 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10879 if (attr)
10880 {
10881 dwarf2_const_value (attr, sym, cu);
10882 }
10883 {
10884 /* NOTE: carlton/2003-11-10: See comment above in the
10885 DW_TAG_class_type, etc. block. */
10886
10887 list_to_add = (cu->list_in_scope == &file_symbols
10888 && (cu->language == language_cplus
10889 || cu->language == language_java)
10890 ? &global_symbols : cu->list_in_scope);
10891 }
10892 break;
10893 case DW_TAG_namespace:
10894 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10895 list_to_add = &global_symbols;
10896 break;
10897 default:
10898 /* Not a tag we recognize. Hopefully we aren't processing
10899 trash data, but since we must specifically ignore things
10900 we don't recognize, there is nothing else we should do at
10901 this point. */
10902 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10903 dwarf_tag_name (die->tag));
10904 break;
10905 }
10906
10907 if (suppress_add)
10908 {
10909 sym->hash_next = objfile->template_symbols;
10910 objfile->template_symbols = sym;
10911 list_to_add = NULL;
10912 }
10913
10914 if (list_to_add != NULL)
10915 add_symbol_to_list (sym, list_to_add);
10916
10917 /* For the benefit of old versions of GCC, check for anonymous
10918 namespaces based on the demangled name. */
10919 if (!processing_has_namespace_info
10920 && cu->language == language_cplus)
10921 cp_scan_for_anonymous_namespaces (sym);
10922 }
10923 return (sym);
10924 }
10925
10926 /* A wrapper for new_symbol_full that always allocates a new symbol. */
10927
10928 static struct symbol *
10929 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10930 {
10931 return new_symbol_full (die, type, cu, NULL);
10932 }
10933
10934 /* Given an attr with a DW_FORM_dataN value in host byte order,
10935 zero-extend it as appropriate for the symbol's type. The DWARF
10936 standard (v4) is not entirely clear about the meaning of using
10937 DW_FORM_dataN for a constant with a signed type, where the type is
10938 wider than the data. The conclusion of a discussion on the DWARF
10939 list was that this is unspecified. We choose to always zero-extend
10940 because that is the interpretation long in use by GCC. */
10941
10942 static gdb_byte *
10943 dwarf2_const_value_data (struct attribute *attr, struct type *type,
10944 const char *name, struct obstack *obstack,
10945 struct dwarf2_cu *cu, long *value, int bits)
10946 {
10947 struct objfile *objfile = cu->objfile;
10948 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10949 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10950 LONGEST l = DW_UNSND (attr);
10951
10952 if (bits < sizeof (*value) * 8)
10953 {
10954 l &= ((LONGEST) 1 << bits) - 1;
10955 *value = l;
10956 }
10957 else if (bits == sizeof (*value) * 8)
10958 *value = l;
10959 else
10960 {
10961 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10962 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10963 return bytes;
10964 }
10965
10966 return NULL;
10967 }
10968
10969 /* Read a constant value from an attribute. Either set *VALUE, or if
10970 the value does not fit in *VALUE, set *BYTES - either already
10971 allocated on the objfile obstack, or newly allocated on OBSTACK,
10972 or, set *BATON, if we translated the constant to a location
10973 expression. */
10974
10975 static void
10976 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10977 const char *name, struct obstack *obstack,
10978 struct dwarf2_cu *cu,
10979 long *value, gdb_byte **bytes,
10980 struct dwarf2_locexpr_baton **baton)
10981 {
10982 struct objfile *objfile = cu->objfile;
10983 struct comp_unit_head *cu_header = &cu->header;
10984 struct dwarf_block *blk;
10985 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10986 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10987
10988 *value = 0;
10989 *bytes = NULL;
10990 *baton = NULL;
10991
10992 switch (attr->form)
10993 {
10994 case DW_FORM_addr:
10995 {
10996 gdb_byte *data;
10997
10998 if (TYPE_LENGTH (type) != cu_header->addr_size)
10999 dwarf2_const_value_length_mismatch_complaint (name,
11000 cu_header->addr_size,
11001 TYPE_LENGTH (type));
11002 /* Symbols of this form are reasonably rare, so we just
11003 piggyback on the existing location code rather than writing
11004 a new implementation of symbol_computed_ops. */
11005 *baton = obstack_alloc (&objfile->objfile_obstack,
11006 sizeof (struct dwarf2_locexpr_baton));
11007 (*baton)->per_cu = cu->per_cu;
11008 gdb_assert ((*baton)->per_cu);
11009
11010 (*baton)->size = 2 + cu_header->addr_size;
11011 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11012 (*baton)->data = data;
11013
11014 data[0] = DW_OP_addr;
11015 store_unsigned_integer (&data[1], cu_header->addr_size,
11016 byte_order, DW_ADDR (attr));
11017 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11018 }
11019 break;
11020 case DW_FORM_string:
11021 case DW_FORM_strp:
11022 /* DW_STRING is already allocated on the objfile obstack, point
11023 directly to it. */
11024 *bytes = (gdb_byte *) DW_STRING (attr);
11025 break;
11026 case DW_FORM_block1:
11027 case DW_FORM_block2:
11028 case DW_FORM_block4:
11029 case DW_FORM_block:
11030 case DW_FORM_exprloc:
11031 blk = DW_BLOCK (attr);
11032 if (TYPE_LENGTH (type) != blk->size)
11033 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11034 TYPE_LENGTH (type));
11035 *bytes = blk->data;
11036 break;
11037
11038 /* The DW_AT_const_value attributes are supposed to carry the
11039 symbol's value "represented as it would be on the target
11040 architecture." By the time we get here, it's already been
11041 converted to host endianness, so we just need to sign- or
11042 zero-extend it as appropriate. */
11043 case DW_FORM_data1:
11044 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
11045 break;
11046 case DW_FORM_data2:
11047 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
11048 break;
11049 case DW_FORM_data4:
11050 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
11051 break;
11052 case DW_FORM_data8:
11053 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
11054 break;
11055
11056 case DW_FORM_sdata:
11057 *value = DW_SND (attr);
11058 break;
11059
11060 case DW_FORM_udata:
11061 *value = DW_UNSND (attr);
11062 break;
11063
11064 default:
11065 complaint (&symfile_complaints,
11066 _("unsupported const value attribute form: '%s'"),
11067 dwarf_form_name (attr->form));
11068 *value = 0;
11069 break;
11070 }
11071 }
11072
11073
11074 /* Copy constant value from an attribute to a symbol. */
11075
11076 static void
11077 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11078 struct dwarf2_cu *cu)
11079 {
11080 struct objfile *objfile = cu->objfile;
11081 struct comp_unit_head *cu_header = &cu->header;
11082 long value;
11083 gdb_byte *bytes;
11084 struct dwarf2_locexpr_baton *baton;
11085
11086 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11087 SYMBOL_PRINT_NAME (sym),
11088 &objfile->objfile_obstack, cu,
11089 &value, &bytes, &baton);
11090
11091 if (baton != NULL)
11092 {
11093 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11094 SYMBOL_LOCATION_BATON (sym) = baton;
11095 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11096 }
11097 else if (bytes != NULL)
11098 {
11099 SYMBOL_VALUE_BYTES (sym) = bytes;
11100 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11101 }
11102 else
11103 {
11104 SYMBOL_VALUE (sym) = value;
11105 SYMBOL_CLASS (sym) = LOC_CONST;
11106 }
11107 }
11108
11109 /* Return the type of the die in question using its DW_AT_type attribute. */
11110
11111 static struct type *
11112 die_type (struct die_info *die, struct dwarf2_cu *cu)
11113 {
11114 struct attribute *type_attr;
11115
11116 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11117 if (!type_attr)
11118 {
11119 /* A missing DW_AT_type represents a void type. */
11120 return objfile_type (cu->objfile)->builtin_void;
11121 }
11122
11123 return lookup_die_type (die, type_attr, cu);
11124 }
11125
11126 /* True iff CU's producer generates GNAT Ada auxiliary information
11127 that allows to find parallel types through that information instead
11128 of having to do expensive parallel lookups by type name. */
11129
11130 static int
11131 need_gnat_info (struct dwarf2_cu *cu)
11132 {
11133 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11134 of GNAT produces this auxiliary information, without any indication
11135 that it is produced. Part of enhancing the FSF version of GNAT
11136 to produce that information will be to put in place an indicator
11137 that we can use in order to determine whether the descriptive type
11138 info is available or not. One suggestion that has been made is
11139 to use a new attribute, attached to the CU die. For now, assume
11140 that the descriptive type info is not available. */
11141 return 0;
11142 }
11143
11144 /* Return the auxiliary type of the die in question using its
11145 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11146 attribute is not present. */
11147
11148 static struct type *
11149 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11150 {
11151 struct attribute *type_attr;
11152
11153 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11154 if (!type_attr)
11155 return NULL;
11156
11157 return lookup_die_type (die, type_attr, cu);
11158 }
11159
11160 /* If DIE has a descriptive_type attribute, then set the TYPE's
11161 descriptive type accordingly. */
11162
11163 static void
11164 set_descriptive_type (struct type *type, struct die_info *die,
11165 struct dwarf2_cu *cu)
11166 {
11167 struct type *descriptive_type = die_descriptive_type (die, cu);
11168
11169 if (descriptive_type)
11170 {
11171 ALLOCATE_GNAT_AUX_TYPE (type);
11172 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11173 }
11174 }
11175
11176 /* Return the containing type of the die in question using its
11177 DW_AT_containing_type attribute. */
11178
11179 static struct type *
11180 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11181 {
11182 struct attribute *type_attr;
11183
11184 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11185 if (!type_attr)
11186 error (_("Dwarf Error: Problem turning containing type into gdb type "
11187 "[in module %s]"), cu->objfile->name);
11188
11189 return lookup_die_type (die, type_attr, cu);
11190 }
11191
11192 /* Look up the type of DIE in CU using its type attribute ATTR.
11193 If there is no type substitute an error marker. */
11194
11195 static struct type *
11196 lookup_die_type (struct die_info *die, struct attribute *attr,
11197 struct dwarf2_cu *cu)
11198 {
11199 struct type *this_type;
11200
11201 /* First see if we have it cached. */
11202
11203 if (is_ref_attr (attr))
11204 {
11205 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11206
11207 this_type = get_die_type_at_offset (offset, cu->per_cu);
11208 }
11209 else if (attr->form == DW_FORM_sig8)
11210 {
11211 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11212 struct dwarf2_cu *sig_cu;
11213 unsigned int offset;
11214
11215 /* sig_type will be NULL if the signatured type is missing from
11216 the debug info. */
11217 if (sig_type == NULL)
11218 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11219 "at 0x%x [in module %s]"),
11220 die->offset, cu->objfile->name);
11221
11222 gdb_assert (sig_type->per_cu.from_debug_types);
11223 offset = sig_type->offset + sig_type->type_offset;
11224 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11225 }
11226 else
11227 {
11228 dump_die_for_error (die);
11229 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11230 dwarf_attr_name (attr->name), cu->objfile->name);
11231 }
11232
11233 /* If not cached we need to read it in. */
11234
11235 if (this_type == NULL)
11236 {
11237 struct die_info *type_die;
11238 struct dwarf2_cu *type_cu = cu;
11239
11240 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11241 /* If the type is cached, we should have found it above. */
11242 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11243 this_type = read_type_die_1 (type_die, type_cu);
11244 }
11245
11246 /* If we still don't have a type use an error marker. */
11247
11248 if (this_type == NULL)
11249 {
11250 char *message, *saved;
11251
11252 /* read_type_die already issued a complaint. */
11253 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11254 cu->objfile->name,
11255 cu->header.offset,
11256 die->offset);
11257 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11258 message, strlen (message));
11259 xfree (message);
11260
11261 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11262 }
11263
11264 return this_type;
11265 }
11266
11267 /* Return the type in DIE, CU.
11268 Returns NULL for invalid types.
11269
11270 This first does a lookup in the appropriate type_hash table,
11271 and only reads the die in if necessary.
11272
11273 NOTE: This can be called when reading in partial or full symbols. */
11274
11275 static struct type *
11276 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11277 {
11278 struct type *this_type;
11279
11280 this_type = get_die_type (die, cu);
11281 if (this_type)
11282 return this_type;
11283
11284 return read_type_die_1 (die, cu);
11285 }
11286
11287 /* Read the type in DIE, CU.
11288 Returns NULL for invalid types. */
11289
11290 static struct type *
11291 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11292 {
11293 struct type *this_type = NULL;
11294
11295 switch (die->tag)
11296 {
11297 case DW_TAG_class_type:
11298 case DW_TAG_interface_type:
11299 case DW_TAG_structure_type:
11300 case DW_TAG_union_type:
11301 this_type = read_structure_type (die, cu);
11302 break;
11303 case DW_TAG_enumeration_type:
11304 this_type = read_enumeration_type (die, cu);
11305 break;
11306 case DW_TAG_subprogram:
11307 case DW_TAG_subroutine_type:
11308 case DW_TAG_inlined_subroutine:
11309 this_type = read_subroutine_type (die, cu);
11310 break;
11311 case DW_TAG_array_type:
11312 this_type = read_array_type (die, cu);
11313 break;
11314 case DW_TAG_set_type:
11315 this_type = read_set_type (die, cu);
11316 break;
11317 case DW_TAG_pointer_type:
11318 this_type = read_tag_pointer_type (die, cu);
11319 break;
11320 case DW_TAG_ptr_to_member_type:
11321 this_type = read_tag_ptr_to_member_type (die, cu);
11322 break;
11323 case DW_TAG_reference_type:
11324 this_type = read_tag_reference_type (die, cu);
11325 break;
11326 case DW_TAG_const_type:
11327 this_type = read_tag_const_type (die, cu);
11328 break;
11329 case DW_TAG_volatile_type:
11330 this_type = read_tag_volatile_type (die, cu);
11331 break;
11332 case DW_TAG_string_type:
11333 this_type = read_tag_string_type (die, cu);
11334 break;
11335 case DW_TAG_typedef:
11336 this_type = read_typedef (die, cu);
11337 break;
11338 case DW_TAG_subrange_type:
11339 this_type = read_subrange_type (die, cu);
11340 break;
11341 case DW_TAG_base_type:
11342 this_type = read_base_type (die, cu);
11343 break;
11344 case DW_TAG_unspecified_type:
11345 this_type = read_unspecified_type (die, cu);
11346 break;
11347 case DW_TAG_namespace:
11348 this_type = read_namespace_type (die, cu);
11349 break;
11350 case DW_TAG_module:
11351 this_type = read_module_type (die, cu);
11352 break;
11353 default:
11354 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
11355 dwarf_tag_name (die->tag));
11356 break;
11357 }
11358
11359 return this_type;
11360 }
11361
11362 /* See if we can figure out if the class lives in a namespace. We do
11363 this by looking for a member function; its demangled name will
11364 contain namespace info, if there is any.
11365 Return the computed name or NULL.
11366 Space for the result is allocated on the objfile's obstack.
11367 This is the full-die version of guess_partial_die_structure_name.
11368 In this case we know DIE has no useful parent. */
11369
11370 static char *
11371 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11372 {
11373 struct die_info *spec_die;
11374 struct dwarf2_cu *spec_cu;
11375 struct die_info *child;
11376
11377 spec_cu = cu;
11378 spec_die = die_specification (die, &spec_cu);
11379 if (spec_die != NULL)
11380 {
11381 die = spec_die;
11382 cu = spec_cu;
11383 }
11384
11385 for (child = die->child;
11386 child != NULL;
11387 child = child->sibling)
11388 {
11389 if (child->tag == DW_TAG_subprogram)
11390 {
11391 struct attribute *attr;
11392
11393 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11394 if (attr == NULL)
11395 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11396 if (attr != NULL)
11397 {
11398 char *actual_name
11399 = language_class_name_from_physname (cu->language_defn,
11400 DW_STRING (attr));
11401 char *name = NULL;
11402
11403 if (actual_name != NULL)
11404 {
11405 char *die_name = dwarf2_name (die, cu);
11406
11407 if (die_name != NULL
11408 && strcmp (die_name, actual_name) != 0)
11409 {
11410 /* Strip off the class name from the full name.
11411 We want the prefix. */
11412 int die_name_len = strlen (die_name);
11413 int actual_name_len = strlen (actual_name);
11414
11415 /* Test for '::' as a sanity check. */
11416 if (actual_name_len > die_name_len + 2
11417 && actual_name[actual_name_len - die_name_len - 1] == ':')
11418 name =
11419 obsavestring (actual_name,
11420 actual_name_len - die_name_len - 2,
11421 &cu->objfile->objfile_obstack);
11422 }
11423 }
11424 xfree (actual_name);
11425 return name;
11426 }
11427 }
11428 }
11429
11430 return NULL;
11431 }
11432
11433 /* Return the name of the namespace/class that DIE is defined within,
11434 or "" if we can't tell. The caller should not xfree the result.
11435
11436 For example, if we're within the method foo() in the following
11437 code:
11438
11439 namespace N {
11440 class C {
11441 void foo () {
11442 }
11443 };
11444 }
11445
11446 then determine_prefix on foo's die will return "N::C". */
11447
11448 static char *
11449 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11450 {
11451 struct die_info *parent, *spec_die;
11452 struct dwarf2_cu *spec_cu;
11453 struct type *parent_type;
11454
11455 if (cu->language != language_cplus && cu->language != language_java
11456 && cu->language != language_fortran)
11457 return "";
11458
11459 /* We have to be careful in the presence of DW_AT_specification.
11460 For example, with GCC 3.4, given the code
11461
11462 namespace N {
11463 void foo() {
11464 // Definition of N::foo.
11465 }
11466 }
11467
11468 then we'll have a tree of DIEs like this:
11469
11470 1: DW_TAG_compile_unit
11471 2: DW_TAG_namespace // N
11472 3: DW_TAG_subprogram // declaration of N::foo
11473 4: DW_TAG_subprogram // definition of N::foo
11474 DW_AT_specification // refers to die #3
11475
11476 Thus, when processing die #4, we have to pretend that we're in
11477 the context of its DW_AT_specification, namely the contex of die
11478 #3. */
11479 spec_cu = cu;
11480 spec_die = die_specification (die, &spec_cu);
11481 if (spec_die == NULL)
11482 parent = die->parent;
11483 else
11484 {
11485 parent = spec_die->parent;
11486 cu = spec_cu;
11487 }
11488
11489 if (parent == NULL)
11490 return "";
11491 else if (parent->building_fullname)
11492 {
11493 const char *name;
11494 const char *parent_name;
11495
11496 /* It has been seen on RealView 2.2 built binaries,
11497 DW_TAG_template_type_param types actually _defined_ as
11498 children of the parent class:
11499
11500 enum E {};
11501 template class <class Enum> Class{};
11502 Class<enum E> class_e;
11503
11504 1: DW_TAG_class_type (Class)
11505 2: DW_TAG_enumeration_type (E)
11506 3: DW_TAG_enumerator (enum1:0)
11507 3: DW_TAG_enumerator (enum2:1)
11508 ...
11509 2: DW_TAG_template_type_param
11510 DW_AT_type DW_FORM_ref_udata (E)
11511
11512 Besides being broken debug info, it can put GDB into an
11513 infinite loop. Consider:
11514
11515 When we're building the full name for Class<E>, we'll start
11516 at Class, and go look over its template type parameters,
11517 finding E. We'll then try to build the full name of E, and
11518 reach here. We're now trying to build the full name of E,
11519 and look over the parent DIE for containing scope. In the
11520 broken case, if we followed the parent DIE of E, we'd again
11521 find Class, and once again go look at its template type
11522 arguments, etc., etc. Simply don't consider such parent die
11523 as source-level parent of this die (it can't be, the language
11524 doesn't allow it), and break the loop here. */
11525 name = dwarf2_name (die, cu);
11526 parent_name = dwarf2_name (parent, cu);
11527 complaint (&symfile_complaints,
11528 _("template param type '%s' defined within parent '%s'"),
11529 name ? name : "<unknown>",
11530 parent_name ? parent_name : "<unknown>");
11531 return "";
11532 }
11533 else
11534 switch (parent->tag)
11535 {
11536 case DW_TAG_namespace:
11537 parent_type = read_type_die (parent, cu);
11538 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11539 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11540 Work around this problem here. */
11541 if (cu->language == language_cplus
11542 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11543 return "";
11544 /* We give a name to even anonymous namespaces. */
11545 return TYPE_TAG_NAME (parent_type);
11546 case DW_TAG_class_type:
11547 case DW_TAG_interface_type:
11548 case DW_TAG_structure_type:
11549 case DW_TAG_union_type:
11550 case DW_TAG_module:
11551 parent_type = read_type_die (parent, cu);
11552 if (TYPE_TAG_NAME (parent_type) != NULL)
11553 return TYPE_TAG_NAME (parent_type);
11554 else
11555 /* An anonymous structure is only allowed non-static data
11556 members; no typedefs, no member functions, et cetera.
11557 So it does not need a prefix. */
11558 return "";
11559 case DW_TAG_compile_unit:
11560 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11561 if (cu->language == language_cplus
11562 && dwarf2_per_objfile->types.asection != NULL
11563 && die->child != NULL
11564 && (die->tag == DW_TAG_class_type
11565 || die->tag == DW_TAG_structure_type
11566 || die->tag == DW_TAG_union_type))
11567 {
11568 char *name = guess_full_die_structure_name (die, cu);
11569 if (name != NULL)
11570 return name;
11571 }
11572 return "";
11573 default:
11574 return determine_prefix (parent, cu);
11575 }
11576 }
11577
11578 /* Return a newly-allocated string formed by concatenating PREFIX and
11579 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11580 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11581 perform an obconcat, otherwise allocate storage for the result. The CU argument
11582 is used to determine the language and hence, the appropriate separator. */
11583
11584 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
11585
11586 static char *
11587 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11588 int physname, struct dwarf2_cu *cu)
11589 {
11590 const char *lead = "";
11591 const char *sep;
11592
11593 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11594 sep = "";
11595 else if (cu->language == language_java)
11596 sep = ".";
11597 else if (cu->language == language_fortran && physname)
11598 {
11599 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11600 DW_AT_MIPS_linkage_name is preferred and used instead. */
11601
11602 lead = "__";
11603 sep = "_MOD_";
11604 }
11605 else
11606 sep = "::";
11607
11608 if (prefix == NULL)
11609 prefix = "";
11610 if (suffix == NULL)
11611 suffix = "";
11612
11613 if (obs == NULL)
11614 {
11615 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11616
11617 strcpy (retval, lead);
11618 strcat (retval, prefix);
11619 strcat (retval, sep);
11620 strcat (retval, suffix);
11621 return retval;
11622 }
11623 else
11624 {
11625 /* We have an obstack. */
11626 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11627 }
11628 }
11629
11630 /* Return sibling of die, NULL if no sibling. */
11631
11632 static struct die_info *
11633 sibling_die (struct die_info *die)
11634 {
11635 return die->sibling;
11636 }
11637
11638 /* Get name of a die, return NULL if not found. */
11639
11640 static char *
11641 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11642 struct obstack *obstack)
11643 {
11644 if (name && cu->language == language_cplus)
11645 {
11646 char *canon_name = cp_canonicalize_string (name);
11647
11648 if (canon_name != NULL)
11649 {
11650 if (strcmp (canon_name, name) != 0)
11651 name = obsavestring (canon_name, strlen (canon_name),
11652 obstack);
11653 xfree (canon_name);
11654 }
11655 }
11656
11657 return name;
11658 }
11659
11660 /* Get name of a die, return NULL if not found. */
11661
11662 static char *
11663 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11664 {
11665 struct attribute *attr;
11666
11667 attr = dwarf2_attr (die, DW_AT_name, cu);
11668 if (!attr || !DW_STRING (attr))
11669 return NULL;
11670
11671 switch (die->tag)
11672 {
11673 case DW_TAG_compile_unit:
11674 /* Compilation units have a DW_AT_name that is a filename, not
11675 a source language identifier. */
11676 case DW_TAG_enumeration_type:
11677 case DW_TAG_enumerator:
11678 /* These tags always have simple identifiers already; no need
11679 to canonicalize them. */
11680 return DW_STRING (attr);
11681
11682 case DW_TAG_subprogram:
11683 /* Java constructors will all be named "<init>", so return
11684 the class name when we see this special case. */
11685 if (cu->language == language_java
11686 && DW_STRING (attr) != NULL
11687 && strcmp (DW_STRING (attr), "<init>") == 0)
11688 {
11689 struct dwarf2_cu *spec_cu = cu;
11690 struct die_info *spec_die;
11691
11692 /* GCJ will output '<init>' for Java constructor names.
11693 For this special case, return the name of the parent class. */
11694
11695 /* GCJ may output suprogram DIEs with AT_specification set.
11696 If so, use the name of the specified DIE. */
11697 spec_die = die_specification (die, &spec_cu);
11698 if (spec_die != NULL)
11699 return dwarf2_name (spec_die, spec_cu);
11700
11701 do
11702 {
11703 die = die->parent;
11704 if (die->tag == DW_TAG_class_type)
11705 return dwarf2_name (die, cu);
11706 }
11707 while (die->tag != DW_TAG_compile_unit);
11708 }
11709 break;
11710
11711 case DW_TAG_class_type:
11712 case DW_TAG_interface_type:
11713 case DW_TAG_structure_type:
11714 case DW_TAG_union_type:
11715 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11716 structures or unions. These were of the form "._%d" in GCC 4.1,
11717 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11718 and GCC 4.4. We work around this problem by ignoring these. */
11719 if (strncmp (DW_STRING (attr), "._", 2) == 0
11720 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11721 return NULL;
11722 break;
11723
11724 default:
11725 break;
11726 }
11727
11728 if (!DW_STRING_IS_CANONICAL (attr))
11729 {
11730 DW_STRING (attr)
11731 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11732 &cu->objfile->objfile_obstack);
11733 DW_STRING_IS_CANONICAL (attr) = 1;
11734 }
11735 return DW_STRING (attr);
11736 }
11737
11738 /* Return the die that this die in an extension of, or NULL if there
11739 is none. *EXT_CU is the CU containing DIE on input, and the CU
11740 containing the return value on output. */
11741
11742 static struct die_info *
11743 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11744 {
11745 struct attribute *attr;
11746
11747 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11748 if (attr == NULL)
11749 return NULL;
11750
11751 return follow_die_ref (die, attr, ext_cu);
11752 }
11753
11754 /* Convert a DIE tag into its string name. */
11755
11756 static char *
11757 dwarf_tag_name (unsigned tag)
11758 {
11759 switch (tag)
11760 {
11761 case DW_TAG_padding:
11762 return "DW_TAG_padding";
11763 case DW_TAG_array_type:
11764 return "DW_TAG_array_type";
11765 case DW_TAG_class_type:
11766 return "DW_TAG_class_type";
11767 case DW_TAG_entry_point:
11768 return "DW_TAG_entry_point";
11769 case DW_TAG_enumeration_type:
11770 return "DW_TAG_enumeration_type";
11771 case DW_TAG_formal_parameter:
11772 return "DW_TAG_formal_parameter";
11773 case DW_TAG_imported_declaration:
11774 return "DW_TAG_imported_declaration";
11775 case DW_TAG_label:
11776 return "DW_TAG_label";
11777 case DW_TAG_lexical_block:
11778 return "DW_TAG_lexical_block";
11779 case DW_TAG_member:
11780 return "DW_TAG_member";
11781 case DW_TAG_pointer_type:
11782 return "DW_TAG_pointer_type";
11783 case DW_TAG_reference_type:
11784 return "DW_TAG_reference_type";
11785 case DW_TAG_compile_unit:
11786 return "DW_TAG_compile_unit";
11787 case DW_TAG_string_type:
11788 return "DW_TAG_string_type";
11789 case DW_TAG_structure_type:
11790 return "DW_TAG_structure_type";
11791 case DW_TAG_subroutine_type:
11792 return "DW_TAG_subroutine_type";
11793 case DW_TAG_typedef:
11794 return "DW_TAG_typedef";
11795 case DW_TAG_union_type:
11796 return "DW_TAG_union_type";
11797 case DW_TAG_unspecified_parameters:
11798 return "DW_TAG_unspecified_parameters";
11799 case DW_TAG_variant:
11800 return "DW_TAG_variant";
11801 case DW_TAG_common_block:
11802 return "DW_TAG_common_block";
11803 case DW_TAG_common_inclusion:
11804 return "DW_TAG_common_inclusion";
11805 case DW_TAG_inheritance:
11806 return "DW_TAG_inheritance";
11807 case DW_TAG_inlined_subroutine:
11808 return "DW_TAG_inlined_subroutine";
11809 case DW_TAG_module:
11810 return "DW_TAG_module";
11811 case DW_TAG_ptr_to_member_type:
11812 return "DW_TAG_ptr_to_member_type";
11813 case DW_TAG_set_type:
11814 return "DW_TAG_set_type";
11815 case DW_TAG_subrange_type:
11816 return "DW_TAG_subrange_type";
11817 case DW_TAG_with_stmt:
11818 return "DW_TAG_with_stmt";
11819 case DW_TAG_access_declaration:
11820 return "DW_TAG_access_declaration";
11821 case DW_TAG_base_type:
11822 return "DW_TAG_base_type";
11823 case DW_TAG_catch_block:
11824 return "DW_TAG_catch_block";
11825 case DW_TAG_const_type:
11826 return "DW_TAG_const_type";
11827 case DW_TAG_constant:
11828 return "DW_TAG_constant";
11829 case DW_TAG_enumerator:
11830 return "DW_TAG_enumerator";
11831 case DW_TAG_file_type:
11832 return "DW_TAG_file_type";
11833 case DW_TAG_friend:
11834 return "DW_TAG_friend";
11835 case DW_TAG_namelist:
11836 return "DW_TAG_namelist";
11837 case DW_TAG_namelist_item:
11838 return "DW_TAG_namelist_item";
11839 case DW_TAG_packed_type:
11840 return "DW_TAG_packed_type";
11841 case DW_TAG_subprogram:
11842 return "DW_TAG_subprogram";
11843 case DW_TAG_template_type_param:
11844 return "DW_TAG_template_type_param";
11845 case DW_TAG_template_value_param:
11846 return "DW_TAG_template_value_param";
11847 case DW_TAG_thrown_type:
11848 return "DW_TAG_thrown_type";
11849 case DW_TAG_try_block:
11850 return "DW_TAG_try_block";
11851 case DW_TAG_variant_part:
11852 return "DW_TAG_variant_part";
11853 case DW_TAG_variable:
11854 return "DW_TAG_variable";
11855 case DW_TAG_volatile_type:
11856 return "DW_TAG_volatile_type";
11857 case DW_TAG_dwarf_procedure:
11858 return "DW_TAG_dwarf_procedure";
11859 case DW_TAG_restrict_type:
11860 return "DW_TAG_restrict_type";
11861 case DW_TAG_interface_type:
11862 return "DW_TAG_interface_type";
11863 case DW_TAG_namespace:
11864 return "DW_TAG_namespace";
11865 case DW_TAG_imported_module:
11866 return "DW_TAG_imported_module";
11867 case DW_TAG_unspecified_type:
11868 return "DW_TAG_unspecified_type";
11869 case DW_TAG_partial_unit:
11870 return "DW_TAG_partial_unit";
11871 case DW_TAG_imported_unit:
11872 return "DW_TAG_imported_unit";
11873 case DW_TAG_condition:
11874 return "DW_TAG_condition";
11875 case DW_TAG_shared_type:
11876 return "DW_TAG_shared_type";
11877 case DW_TAG_type_unit:
11878 return "DW_TAG_type_unit";
11879 case DW_TAG_MIPS_loop:
11880 return "DW_TAG_MIPS_loop";
11881 case DW_TAG_HP_array_descriptor:
11882 return "DW_TAG_HP_array_descriptor";
11883 case DW_TAG_format_label:
11884 return "DW_TAG_format_label";
11885 case DW_TAG_function_template:
11886 return "DW_TAG_function_template";
11887 case DW_TAG_class_template:
11888 return "DW_TAG_class_template";
11889 case DW_TAG_GNU_BINCL:
11890 return "DW_TAG_GNU_BINCL";
11891 case DW_TAG_GNU_EINCL:
11892 return "DW_TAG_GNU_EINCL";
11893 case DW_TAG_upc_shared_type:
11894 return "DW_TAG_upc_shared_type";
11895 case DW_TAG_upc_strict_type:
11896 return "DW_TAG_upc_strict_type";
11897 case DW_TAG_upc_relaxed_type:
11898 return "DW_TAG_upc_relaxed_type";
11899 case DW_TAG_PGI_kanji_type:
11900 return "DW_TAG_PGI_kanji_type";
11901 case DW_TAG_PGI_interface_block:
11902 return "DW_TAG_PGI_interface_block";
11903 default:
11904 return "DW_TAG_<unknown>";
11905 }
11906 }
11907
11908 /* Convert a DWARF attribute code into its string name. */
11909
11910 static char *
11911 dwarf_attr_name (unsigned attr)
11912 {
11913 switch (attr)
11914 {
11915 case DW_AT_sibling:
11916 return "DW_AT_sibling";
11917 case DW_AT_location:
11918 return "DW_AT_location";
11919 case DW_AT_name:
11920 return "DW_AT_name";
11921 case DW_AT_ordering:
11922 return "DW_AT_ordering";
11923 case DW_AT_subscr_data:
11924 return "DW_AT_subscr_data";
11925 case DW_AT_byte_size:
11926 return "DW_AT_byte_size";
11927 case DW_AT_bit_offset:
11928 return "DW_AT_bit_offset";
11929 case DW_AT_bit_size:
11930 return "DW_AT_bit_size";
11931 case DW_AT_element_list:
11932 return "DW_AT_element_list";
11933 case DW_AT_stmt_list:
11934 return "DW_AT_stmt_list";
11935 case DW_AT_low_pc:
11936 return "DW_AT_low_pc";
11937 case DW_AT_high_pc:
11938 return "DW_AT_high_pc";
11939 case DW_AT_language:
11940 return "DW_AT_language";
11941 case DW_AT_member:
11942 return "DW_AT_member";
11943 case DW_AT_discr:
11944 return "DW_AT_discr";
11945 case DW_AT_discr_value:
11946 return "DW_AT_discr_value";
11947 case DW_AT_visibility:
11948 return "DW_AT_visibility";
11949 case DW_AT_import:
11950 return "DW_AT_import";
11951 case DW_AT_string_length:
11952 return "DW_AT_string_length";
11953 case DW_AT_common_reference:
11954 return "DW_AT_common_reference";
11955 case DW_AT_comp_dir:
11956 return "DW_AT_comp_dir";
11957 case DW_AT_const_value:
11958 return "DW_AT_const_value";
11959 case DW_AT_containing_type:
11960 return "DW_AT_containing_type";
11961 case DW_AT_default_value:
11962 return "DW_AT_default_value";
11963 case DW_AT_inline:
11964 return "DW_AT_inline";
11965 case DW_AT_is_optional:
11966 return "DW_AT_is_optional";
11967 case DW_AT_lower_bound:
11968 return "DW_AT_lower_bound";
11969 case DW_AT_producer:
11970 return "DW_AT_producer";
11971 case DW_AT_prototyped:
11972 return "DW_AT_prototyped";
11973 case DW_AT_return_addr:
11974 return "DW_AT_return_addr";
11975 case DW_AT_start_scope:
11976 return "DW_AT_start_scope";
11977 case DW_AT_bit_stride:
11978 return "DW_AT_bit_stride";
11979 case DW_AT_upper_bound:
11980 return "DW_AT_upper_bound";
11981 case DW_AT_abstract_origin:
11982 return "DW_AT_abstract_origin";
11983 case DW_AT_accessibility:
11984 return "DW_AT_accessibility";
11985 case DW_AT_address_class:
11986 return "DW_AT_address_class";
11987 case DW_AT_artificial:
11988 return "DW_AT_artificial";
11989 case DW_AT_base_types:
11990 return "DW_AT_base_types";
11991 case DW_AT_calling_convention:
11992 return "DW_AT_calling_convention";
11993 case DW_AT_count:
11994 return "DW_AT_count";
11995 case DW_AT_data_member_location:
11996 return "DW_AT_data_member_location";
11997 case DW_AT_decl_column:
11998 return "DW_AT_decl_column";
11999 case DW_AT_decl_file:
12000 return "DW_AT_decl_file";
12001 case DW_AT_decl_line:
12002 return "DW_AT_decl_line";
12003 case DW_AT_declaration:
12004 return "DW_AT_declaration";
12005 case DW_AT_discr_list:
12006 return "DW_AT_discr_list";
12007 case DW_AT_encoding:
12008 return "DW_AT_encoding";
12009 case DW_AT_external:
12010 return "DW_AT_external";
12011 case DW_AT_frame_base:
12012 return "DW_AT_frame_base";
12013 case DW_AT_friend:
12014 return "DW_AT_friend";
12015 case DW_AT_identifier_case:
12016 return "DW_AT_identifier_case";
12017 case DW_AT_macro_info:
12018 return "DW_AT_macro_info";
12019 case DW_AT_namelist_items:
12020 return "DW_AT_namelist_items";
12021 case DW_AT_priority:
12022 return "DW_AT_priority";
12023 case DW_AT_segment:
12024 return "DW_AT_segment";
12025 case DW_AT_specification:
12026 return "DW_AT_specification";
12027 case DW_AT_static_link:
12028 return "DW_AT_static_link";
12029 case DW_AT_type:
12030 return "DW_AT_type";
12031 case DW_AT_use_location:
12032 return "DW_AT_use_location";
12033 case DW_AT_variable_parameter:
12034 return "DW_AT_variable_parameter";
12035 case DW_AT_virtuality:
12036 return "DW_AT_virtuality";
12037 case DW_AT_vtable_elem_location:
12038 return "DW_AT_vtable_elem_location";
12039 /* DWARF 3 values. */
12040 case DW_AT_allocated:
12041 return "DW_AT_allocated";
12042 case DW_AT_associated:
12043 return "DW_AT_associated";
12044 case DW_AT_data_location:
12045 return "DW_AT_data_location";
12046 case DW_AT_byte_stride:
12047 return "DW_AT_byte_stride";
12048 case DW_AT_entry_pc:
12049 return "DW_AT_entry_pc";
12050 case DW_AT_use_UTF8:
12051 return "DW_AT_use_UTF8";
12052 case DW_AT_extension:
12053 return "DW_AT_extension";
12054 case DW_AT_ranges:
12055 return "DW_AT_ranges";
12056 case DW_AT_trampoline:
12057 return "DW_AT_trampoline";
12058 case DW_AT_call_column:
12059 return "DW_AT_call_column";
12060 case DW_AT_call_file:
12061 return "DW_AT_call_file";
12062 case DW_AT_call_line:
12063 return "DW_AT_call_line";
12064 case DW_AT_description:
12065 return "DW_AT_description";
12066 case DW_AT_binary_scale:
12067 return "DW_AT_binary_scale";
12068 case DW_AT_decimal_scale:
12069 return "DW_AT_decimal_scale";
12070 case DW_AT_small:
12071 return "DW_AT_small";
12072 case DW_AT_decimal_sign:
12073 return "DW_AT_decimal_sign";
12074 case DW_AT_digit_count:
12075 return "DW_AT_digit_count";
12076 case DW_AT_picture_string:
12077 return "DW_AT_picture_string";
12078 case DW_AT_mutable:
12079 return "DW_AT_mutable";
12080 case DW_AT_threads_scaled:
12081 return "DW_AT_threads_scaled";
12082 case DW_AT_explicit:
12083 return "DW_AT_explicit";
12084 case DW_AT_object_pointer:
12085 return "DW_AT_object_pointer";
12086 case DW_AT_endianity:
12087 return "DW_AT_endianity";
12088 case DW_AT_elemental:
12089 return "DW_AT_elemental";
12090 case DW_AT_pure:
12091 return "DW_AT_pure";
12092 case DW_AT_recursive:
12093 return "DW_AT_recursive";
12094 /* DWARF 4 values. */
12095 case DW_AT_signature:
12096 return "DW_AT_signature";
12097 case DW_AT_linkage_name:
12098 return "DW_AT_linkage_name";
12099 /* SGI/MIPS extensions. */
12100 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12101 case DW_AT_MIPS_fde:
12102 return "DW_AT_MIPS_fde";
12103 #endif
12104 case DW_AT_MIPS_loop_begin:
12105 return "DW_AT_MIPS_loop_begin";
12106 case DW_AT_MIPS_tail_loop_begin:
12107 return "DW_AT_MIPS_tail_loop_begin";
12108 case DW_AT_MIPS_epilog_begin:
12109 return "DW_AT_MIPS_epilog_begin";
12110 case DW_AT_MIPS_loop_unroll_factor:
12111 return "DW_AT_MIPS_loop_unroll_factor";
12112 case DW_AT_MIPS_software_pipeline_depth:
12113 return "DW_AT_MIPS_software_pipeline_depth";
12114 case DW_AT_MIPS_linkage_name:
12115 return "DW_AT_MIPS_linkage_name";
12116 case DW_AT_MIPS_stride:
12117 return "DW_AT_MIPS_stride";
12118 case DW_AT_MIPS_abstract_name:
12119 return "DW_AT_MIPS_abstract_name";
12120 case DW_AT_MIPS_clone_origin:
12121 return "DW_AT_MIPS_clone_origin";
12122 case DW_AT_MIPS_has_inlines:
12123 return "DW_AT_MIPS_has_inlines";
12124 /* HP extensions. */
12125 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12126 case DW_AT_HP_block_index:
12127 return "DW_AT_HP_block_index";
12128 #endif
12129 case DW_AT_HP_unmodifiable:
12130 return "DW_AT_HP_unmodifiable";
12131 case DW_AT_HP_actuals_stmt_list:
12132 return "DW_AT_HP_actuals_stmt_list";
12133 case DW_AT_HP_proc_per_section:
12134 return "DW_AT_HP_proc_per_section";
12135 case DW_AT_HP_raw_data_ptr:
12136 return "DW_AT_HP_raw_data_ptr";
12137 case DW_AT_HP_pass_by_reference:
12138 return "DW_AT_HP_pass_by_reference";
12139 case DW_AT_HP_opt_level:
12140 return "DW_AT_HP_opt_level";
12141 case DW_AT_HP_prof_version_id:
12142 return "DW_AT_HP_prof_version_id";
12143 case DW_AT_HP_opt_flags:
12144 return "DW_AT_HP_opt_flags";
12145 case DW_AT_HP_cold_region_low_pc:
12146 return "DW_AT_HP_cold_region_low_pc";
12147 case DW_AT_HP_cold_region_high_pc:
12148 return "DW_AT_HP_cold_region_high_pc";
12149 case DW_AT_HP_all_variables_modifiable:
12150 return "DW_AT_HP_all_variables_modifiable";
12151 case DW_AT_HP_linkage_name:
12152 return "DW_AT_HP_linkage_name";
12153 case DW_AT_HP_prof_flags:
12154 return "DW_AT_HP_prof_flags";
12155 /* GNU extensions. */
12156 case DW_AT_sf_names:
12157 return "DW_AT_sf_names";
12158 case DW_AT_src_info:
12159 return "DW_AT_src_info";
12160 case DW_AT_mac_info:
12161 return "DW_AT_mac_info";
12162 case DW_AT_src_coords:
12163 return "DW_AT_src_coords";
12164 case DW_AT_body_begin:
12165 return "DW_AT_body_begin";
12166 case DW_AT_body_end:
12167 return "DW_AT_body_end";
12168 case DW_AT_GNU_vector:
12169 return "DW_AT_GNU_vector";
12170 case DW_AT_GNU_odr_signature:
12171 return "DW_AT_GNU_odr_signature";
12172 /* VMS extensions. */
12173 case DW_AT_VMS_rtnbeg_pd_address:
12174 return "DW_AT_VMS_rtnbeg_pd_address";
12175 /* UPC extension. */
12176 case DW_AT_upc_threads_scaled:
12177 return "DW_AT_upc_threads_scaled";
12178 /* PGI (STMicroelectronics) extensions. */
12179 case DW_AT_PGI_lbase:
12180 return "DW_AT_PGI_lbase";
12181 case DW_AT_PGI_soffset:
12182 return "DW_AT_PGI_soffset";
12183 case DW_AT_PGI_lstride:
12184 return "DW_AT_PGI_lstride";
12185 default:
12186 return "DW_AT_<unknown>";
12187 }
12188 }
12189
12190 /* Convert a DWARF value form code into its string name. */
12191
12192 static char *
12193 dwarf_form_name (unsigned form)
12194 {
12195 switch (form)
12196 {
12197 case DW_FORM_addr:
12198 return "DW_FORM_addr";
12199 case DW_FORM_block2:
12200 return "DW_FORM_block2";
12201 case DW_FORM_block4:
12202 return "DW_FORM_block4";
12203 case DW_FORM_data2:
12204 return "DW_FORM_data2";
12205 case DW_FORM_data4:
12206 return "DW_FORM_data4";
12207 case DW_FORM_data8:
12208 return "DW_FORM_data8";
12209 case DW_FORM_string:
12210 return "DW_FORM_string";
12211 case DW_FORM_block:
12212 return "DW_FORM_block";
12213 case DW_FORM_block1:
12214 return "DW_FORM_block1";
12215 case DW_FORM_data1:
12216 return "DW_FORM_data1";
12217 case DW_FORM_flag:
12218 return "DW_FORM_flag";
12219 case DW_FORM_sdata:
12220 return "DW_FORM_sdata";
12221 case DW_FORM_strp:
12222 return "DW_FORM_strp";
12223 case DW_FORM_udata:
12224 return "DW_FORM_udata";
12225 case DW_FORM_ref_addr:
12226 return "DW_FORM_ref_addr";
12227 case DW_FORM_ref1:
12228 return "DW_FORM_ref1";
12229 case DW_FORM_ref2:
12230 return "DW_FORM_ref2";
12231 case DW_FORM_ref4:
12232 return "DW_FORM_ref4";
12233 case DW_FORM_ref8:
12234 return "DW_FORM_ref8";
12235 case DW_FORM_ref_udata:
12236 return "DW_FORM_ref_udata";
12237 case DW_FORM_indirect:
12238 return "DW_FORM_indirect";
12239 case DW_FORM_sec_offset:
12240 return "DW_FORM_sec_offset";
12241 case DW_FORM_exprloc:
12242 return "DW_FORM_exprloc";
12243 case DW_FORM_flag_present:
12244 return "DW_FORM_flag_present";
12245 case DW_FORM_sig8:
12246 return "DW_FORM_sig8";
12247 default:
12248 return "DW_FORM_<unknown>";
12249 }
12250 }
12251
12252 /* Convert a DWARF stack opcode into its string name. */
12253
12254 const char *
12255 dwarf_stack_op_name (unsigned op, int def)
12256 {
12257 switch (op)
12258 {
12259 case DW_OP_addr:
12260 return "DW_OP_addr";
12261 case DW_OP_deref:
12262 return "DW_OP_deref";
12263 case DW_OP_const1u:
12264 return "DW_OP_const1u";
12265 case DW_OP_const1s:
12266 return "DW_OP_const1s";
12267 case DW_OP_const2u:
12268 return "DW_OP_const2u";
12269 case DW_OP_const2s:
12270 return "DW_OP_const2s";
12271 case DW_OP_const4u:
12272 return "DW_OP_const4u";
12273 case DW_OP_const4s:
12274 return "DW_OP_const4s";
12275 case DW_OP_const8u:
12276 return "DW_OP_const8u";
12277 case DW_OP_const8s:
12278 return "DW_OP_const8s";
12279 case DW_OP_constu:
12280 return "DW_OP_constu";
12281 case DW_OP_consts:
12282 return "DW_OP_consts";
12283 case DW_OP_dup:
12284 return "DW_OP_dup";
12285 case DW_OP_drop:
12286 return "DW_OP_drop";
12287 case DW_OP_over:
12288 return "DW_OP_over";
12289 case DW_OP_pick:
12290 return "DW_OP_pick";
12291 case DW_OP_swap:
12292 return "DW_OP_swap";
12293 case DW_OP_rot:
12294 return "DW_OP_rot";
12295 case DW_OP_xderef:
12296 return "DW_OP_xderef";
12297 case DW_OP_abs:
12298 return "DW_OP_abs";
12299 case DW_OP_and:
12300 return "DW_OP_and";
12301 case DW_OP_div:
12302 return "DW_OP_div";
12303 case DW_OP_minus:
12304 return "DW_OP_minus";
12305 case DW_OP_mod:
12306 return "DW_OP_mod";
12307 case DW_OP_mul:
12308 return "DW_OP_mul";
12309 case DW_OP_neg:
12310 return "DW_OP_neg";
12311 case DW_OP_not:
12312 return "DW_OP_not";
12313 case DW_OP_or:
12314 return "DW_OP_or";
12315 case DW_OP_plus:
12316 return "DW_OP_plus";
12317 case DW_OP_plus_uconst:
12318 return "DW_OP_plus_uconst";
12319 case DW_OP_shl:
12320 return "DW_OP_shl";
12321 case DW_OP_shr:
12322 return "DW_OP_shr";
12323 case DW_OP_shra:
12324 return "DW_OP_shra";
12325 case DW_OP_xor:
12326 return "DW_OP_xor";
12327 case DW_OP_bra:
12328 return "DW_OP_bra";
12329 case DW_OP_eq:
12330 return "DW_OP_eq";
12331 case DW_OP_ge:
12332 return "DW_OP_ge";
12333 case DW_OP_gt:
12334 return "DW_OP_gt";
12335 case DW_OP_le:
12336 return "DW_OP_le";
12337 case DW_OP_lt:
12338 return "DW_OP_lt";
12339 case DW_OP_ne:
12340 return "DW_OP_ne";
12341 case DW_OP_skip:
12342 return "DW_OP_skip";
12343 case DW_OP_lit0:
12344 return "DW_OP_lit0";
12345 case DW_OP_lit1:
12346 return "DW_OP_lit1";
12347 case DW_OP_lit2:
12348 return "DW_OP_lit2";
12349 case DW_OP_lit3:
12350 return "DW_OP_lit3";
12351 case DW_OP_lit4:
12352 return "DW_OP_lit4";
12353 case DW_OP_lit5:
12354 return "DW_OP_lit5";
12355 case DW_OP_lit6:
12356 return "DW_OP_lit6";
12357 case DW_OP_lit7:
12358 return "DW_OP_lit7";
12359 case DW_OP_lit8:
12360 return "DW_OP_lit8";
12361 case DW_OP_lit9:
12362 return "DW_OP_lit9";
12363 case DW_OP_lit10:
12364 return "DW_OP_lit10";
12365 case DW_OP_lit11:
12366 return "DW_OP_lit11";
12367 case DW_OP_lit12:
12368 return "DW_OP_lit12";
12369 case DW_OP_lit13:
12370 return "DW_OP_lit13";
12371 case DW_OP_lit14:
12372 return "DW_OP_lit14";
12373 case DW_OP_lit15:
12374 return "DW_OP_lit15";
12375 case DW_OP_lit16:
12376 return "DW_OP_lit16";
12377 case DW_OP_lit17:
12378 return "DW_OP_lit17";
12379 case DW_OP_lit18:
12380 return "DW_OP_lit18";
12381 case DW_OP_lit19:
12382 return "DW_OP_lit19";
12383 case DW_OP_lit20:
12384 return "DW_OP_lit20";
12385 case DW_OP_lit21:
12386 return "DW_OP_lit21";
12387 case DW_OP_lit22:
12388 return "DW_OP_lit22";
12389 case DW_OP_lit23:
12390 return "DW_OP_lit23";
12391 case DW_OP_lit24:
12392 return "DW_OP_lit24";
12393 case DW_OP_lit25:
12394 return "DW_OP_lit25";
12395 case DW_OP_lit26:
12396 return "DW_OP_lit26";
12397 case DW_OP_lit27:
12398 return "DW_OP_lit27";
12399 case DW_OP_lit28:
12400 return "DW_OP_lit28";
12401 case DW_OP_lit29:
12402 return "DW_OP_lit29";
12403 case DW_OP_lit30:
12404 return "DW_OP_lit30";
12405 case DW_OP_lit31:
12406 return "DW_OP_lit31";
12407 case DW_OP_reg0:
12408 return "DW_OP_reg0";
12409 case DW_OP_reg1:
12410 return "DW_OP_reg1";
12411 case DW_OP_reg2:
12412 return "DW_OP_reg2";
12413 case DW_OP_reg3:
12414 return "DW_OP_reg3";
12415 case DW_OP_reg4:
12416 return "DW_OP_reg4";
12417 case DW_OP_reg5:
12418 return "DW_OP_reg5";
12419 case DW_OP_reg6:
12420 return "DW_OP_reg6";
12421 case DW_OP_reg7:
12422 return "DW_OP_reg7";
12423 case DW_OP_reg8:
12424 return "DW_OP_reg8";
12425 case DW_OP_reg9:
12426 return "DW_OP_reg9";
12427 case DW_OP_reg10:
12428 return "DW_OP_reg10";
12429 case DW_OP_reg11:
12430 return "DW_OP_reg11";
12431 case DW_OP_reg12:
12432 return "DW_OP_reg12";
12433 case DW_OP_reg13:
12434 return "DW_OP_reg13";
12435 case DW_OP_reg14:
12436 return "DW_OP_reg14";
12437 case DW_OP_reg15:
12438 return "DW_OP_reg15";
12439 case DW_OP_reg16:
12440 return "DW_OP_reg16";
12441 case DW_OP_reg17:
12442 return "DW_OP_reg17";
12443 case DW_OP_reg18:
12444 return "DW_OP_reg18";
12445 case DW_OP_reg19:
12446 return "DW_OP_reg19";
12447 case DW_OP_reg20:
12448 return "DW_OP_reg20";
12449 case DW_OP_reg21:
12450 return "DW_OP_reg21";
12451 case DW_OP_reg22:
12452 return "DW_OP_reg22";
12453 case DW_OP_reg23:
12454 return "DW_OP_reg23";
12455 case DW_OP_reg24:
12456 return "DW_OP_reg24";
12457 case DW_OP_reg25:
12458 return "DW_OP_reg25";
12459 case DW_OP_reg26:
12460 return "DW_OP_reg26";
12461 case DW_OP_reg27:
12462 return "DW_OP_reg27";
12463 case DW_OP_reg28:
12464 return "DW_OP_reg28";
12465 case DW_OP_reg29:
12466 return "DW_OP_reg29";
12467 case DW_OP_reg30:
12468 return "DW_OP_reg30";
12469 case DW_OP_reg31:
12470 return "DW_OP_reg31";
12471 case DW_OP_breg0:
12472 return "DW_OP_breg0";
12473 case DW_OP_breg1:
12474 return "DW_OP_breg1";
12475 case DW_OP_breg2:
12476 return "DW_OP_breg2";
12477 case DW_OP_breg3:
12478 return "DW_OP_breg3";
12479 case DW_OP_breg4:
12480 return "DW_OP_breg4";
12481 case DW_OP_breg5:
12482 return "DW_OP_breg5";
12483 case DW_OP_breg6:
12484 return "DW_OP_breg6";
12485 case DW_OP_breg7:
12486 return "DW_OP_breg7";
12487 case DW_OP_breg8:
12488 return "DW_OP_breg8";
12489 case DW_OP_breg9:
12490 return "DW_OP_breg9";
12491 case DW_OP_breg10:
12492 return "DW_OP_breg10";
12493 case DW_OP_breg11:
12494 return "DW_OP_breg11";
12495 case DW_OP_breg12:
12496 return "DW_OP_breg12";
12497 case DW_OP_breg13:
12498 return "DW_OP_breg13";
12499 case DW_OP_breg14:
12500 return "DW_OP_breg14";
12501 case DW_OP_breg15:
12502 return "DW_OP_breg15";
12503 case DW_OP_breg16:
12504 return "DW_OP_breg16";
12505 case DW_OP_breg17:
12506 return "DW_OP_breg17";
12507 case DW_OP_breg18:
12508 return "DW_OP_breg18";
12509 case DW_OP_breg19:
12510 return "DW_OP_breg19";
12511 case DW_OP_breg20:
12512 return "DW_OP_breg20";
12513 case DW_OP_breg21:
12514 return "DW_OP_breg21";
12515 case DW_OP_breg22:
12516 return "DW_OP_breg22";
12517 case DW_OP_breg23:
12518 return "DW_OP_breg23";
12519 case DW_OP_breg24:
12520 return "DW_OP_breg24";
12521 case DW_OP_breg25:
12522 return "DW_OP_breg25";
12523 case DW_OP_breg26:
12524 return "DW_OP_breg26";
12525 case DW_OP_breg27:
12526 return "DW_OP_breg27";
12527 case DW_OP_breg28:
12528 return "DW_OP_breg28";
12529 case DW_OP_breg29:
12530 return "DW_OP_breg29";
12531 case DW_OP_breg30:
12532 return "DW_OP_breg30";
12533 case DW_OP_breg31:
12534 return "DW_OP_breg31";
12535 case DW_OP_regx:
12536 return "DW_OP_regx";
12537 case DW_OP_fbreg:
12538 return "DW_OP_fbreg";
12539 case DW_OP_bregx:
12540 return "DW_OP_bregx";
12541 case DW_OP_piece:
12542 return "DW_OP_piece";
12543 case DW_OP_deref_size:
12544 return "DW_OP_deref_size";
12545 case DW_OP_xderef_size:
12546 return "DW_OP_xderef_size";
12547 case DW_OP_nop:
12548 return "DW_OP_nop";
12549 /* DWARF 3 extensions. */
12550 case DW_OP_push_object_address:
12551 return "DW_OP_push_object_address";
12552 case DW_OP_call2:
12553 return "DW_OP_call2";
12554 case DW_OP_call4:
12555 return "DW_OP_call4";
12556 case DW_OP_call_ref:
12557 return "DW_OP_call_ref";
12558 case DW_OP_form_tls_address:
12559 return "DW_OP_form_tls_address";
12560 case DW_OP_call_frame_cfa:
12561 return "DW_OP_call_frame_cfa";
12562 case DW_OP_bit_piece:
12563 return "DW_OP_bit_piece";
12564 /* DWARF 4 extensions. */
12565 case DW_OP_implicit_value:
12566 return "DW_OP_implicit_value";
12567 case DW_OP_stack_value:
12568 return "DW_OP_stack_value";
12569 /* GNU extensions. */
12570 case DW_OP_GNU_push_tls_address:
12571 return "DW_OP_GNU_push_tls_address";
12572 case DW_OP_GNU_uninit:
12573 return "DW_OP_GNU_uninit";
12574 case DW_OP_GNU_implicit_pointer:
12575 return "DW_OP_GNU_implicit_pointer";
12576 default:
12577 return def ? "OP_<unknown>" : NULL;
12578 }
12579 }
12580
12581 static char *
12582 dwarf_bool_name (unsigned mybool)
12583 {
12584 if (mybool)
12585 return "TRUE";
12586 else
12587 return "FALSE";
12588 }
12589
12590 /* Convert a DWARF type code into its string name. */
12591
12592 static char *
12593 dwarf_type_encoding_name (unsigned enc)
12594 {
12595 switch (enc)
12596 {
12597 case DW_ATE_void:
12598 return "DW_ATE_void";
12599 case DW_ATE_address:
12600 return "DW_ATE_address";
12601 case DW_ATE_boolean:
12602 return "DW_ATE_boolean";
12603 case DW_ATE_complex_float:
12604 return "DW_ATE_complex_float";
12605 case DW_ATE_float:
12606 return "DW_ATE_float";
12607 case DW_ATE_signed:
12608 return "DW_ATE_signed";
12609 case DW_ATE_signed_char:
12610 return "DW_ATE_signed_char";
12611 case DW_ATE_unsigned:
12612 return "DW_ATE_unsigned";
12613 case DW_ATE_unsigned_char:
12614 return "DW_ATE_unsigned_char";
12615 /* DWARF 3. */
12616 case DW_ATE_imaginary_float:
12617 return "DW_ATE_imaginary_float";
12618 case DW_ATE_packed_decimal:
12619 return "DW_ATE_packed_decimal";
12620 case DW_ATE_numeric_string:
12621 return "DW_ATE_numeric_string";
12622 case DW_ATE_edited:
12623 return "DW_ATE_edited";
12624 case DW_ATE_signed_fixed:
12625 return "DW_ATE_signed_fixed";
12626 case DW_ATE_unsigned_fixed:
12627 return "DW_ATE_unsigned_fixed";
12628 case DW_ATE_decimal_float:
12629 return "DW_ATE_decimal_float";
12630 /* DWARF 4. */
12631 case DW_ATE_UTF:
12632 return "DW_ATE_UTF";
12633 /* HP extensions. */
12634 case DW_ATE_HP_float80:
12635 return "DW_ATE_HP_float80";
12636 case DW_ATE_HP_complex_float80:
12637 return "DW_ATE_HP_complex_float80";
12638 case DW_ATE_HP_float128:
12639 return "DW_ATE_HP_float128";
12640 case DW_ATE_HP_complex_float128:
12641 return "DW_ATE_HP_complex_float128";
12642 case DW_ATE_HP_floathpintel:
12643 return "DW_ATE_HP_floathpintel";
12644 case DW_ATE_HP_imaginary_float80:
12645 return "DW_ATE_HP_imaginary_float80";
12646 case DW_ATE_HP_imaginary_float128:
12647 return "DW_ATE_HP_imaginary_float128";
12648 default:
12649 return "DW_ATE_<unknown>";
12650 }
12651 }
12652
12653 /* Convert a DWARF call frame info operation to its string name. */
12654
12655 #if 0
12656 static char *
12657 dwarf_cfi_name (unsigned cfi_opc)
12658 {
12659 switch (cfi_opc)
12660 {
12661 case DW_CFA_advance_loc:
12662 return "DW_CFA_advance_loc";
12663 case DW_CFA_offset:
12664 return "DW_CFA_offset";
12665 case DW_CFA_restore:
12666 return "DW_CFA_restore";
12667 case DW_CFA_nop:
12668 return "DW_CFA_nop";
12669 case DW_CFA_set_loc:
12670 return "DW_CFA_set_loc";
12671 case DW_CFA_advance_loc1:
12672 return "DW_CFA_advance_loc1";
12673 case DW_CFA_advance_loc2:
12674 return "DW_CFA_advance_loc2";
12675 case DW_CFA_advance_loc4:
12676 return "DW_CFA_advance_loc4";
12677 case DW_CFA_offset_extended:
12678 return "DW_CFA_offset_extended";
12679 case DW_CFA_restore_extended:
12680 return "DW_CFA_restore_extended";
12681 case DW_CFA_undefined:
12682 return "DW_CFA_undefined";
12683 case DW_CFA_same_value:
12684 return "DW_CFA_same_value";
12685 case DW_CFA_register:
12686 return "DW_CFA_register";
12687 case DW_CFA_remember_state:
12688 return "DW_CFA_remember_state";
12689 case DW_CFA_restore_state:
12690 return "DW_CFA_restore_state";
12691 case DW_CFA_def_cfa:
12692 return "DW_CFA_def_cfa";
12693 case DW_CFA_def_cfa_register:
12694 return "DW_CFA_def_cfa_register";
12695 case DW_CFA_def_cfa_offset:
12696 return "DW_CFA_def_cfa_offset";
12697 /* DWARF 3. */
12698 case DW_CFA_def_cfa_expression:
12699 return "DW_CFA_def_cfa_expression";
12700 case DW_CFA_expression:
12701 return "DW_CFA_expression";
12702 case DW_CFA_offset_extended_sf:
12703 return "DW_CFA_offset_extended_sf";
12704 case DW_CFA_def_cfa_sf:
12705 return "DW_CFA_def_cfa_sf";
12706 case DW_CFA_def_cfa_offset_sf:
12707 return "DW_CFA_def_cfa_offset_sf";
12708 case DW_CFA_val_offset:
12709 return "DW_CFA_val_offset";
12710 case DW_CFA_val_offset_sf:
12711 return "DW_CFA_val_offset_sf";
12712 case DW_CFA_val_expression:
12713 return "DW_CFA_val_expression";
12714 /* SGI/MIPS specific. */
12715 case DW_CFA_MIPS_advance_loc8:
12716 return "DW_CFA_MIPS_advance_loc8";
12717 /* GNU extensions. */
12718 case DW_CFA_GNU_window_save:
12719 return "DW_CFA_GNU_window_save";
12720 case DW_CFA_GNU_args_size:
12721 return "DW_CFA_GNU_args_size";
12722 case DW_CFA_GNU_negative_offset_extended:
12723 return "DW_CFA_GNU_negative_offset_extended";
12724 default:
12725 return "DW_CFA_<unknown>";
12726 }
12727 }
12728 #endif
12729
12730 static void
12731 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12732 {
12733 unsigned int i;
12734
12735 print_spaces (indent, f);
12736 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12737 dwarf_tag_name (die->tag), die->abbrev, die->offset);
12738
12739 if (die->parent != NULL)
12740 {
12741 print_spaces (indent, f);
12742 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12743 die->parent->offset);
12744 }
12745
12746 print_spaces (indent, f);
12747 fprintf_unfiltered (f, " has children: %s\n",
12748 dwarf_bool_name (die->child != NULL));
12749
12750 print_spaces (indent, f);
12751 fprintf_unfiltered (f, " attributes:\n");
12752
12753 for (i = 0; i < die->num_attrs; ++i)
12754 {
12755 print_spaces (indent, f);
12756 fprintf_unfiltered (f, " %s (%s) ",
12757 dwarf_attr_name (die->attrs[i].name),
12758 dwarf_form_name (die->attrs[i].form));
12759
12760 switch (die->attrs[i].form)
12761 {
12762 case DW_FORM_ref_addr:
12763 case DW_FORM_addr:
12764 fprintf_unfiltered (f, "address: ");
12765 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12766 break;
12767 case DW_FORM_block2:
12768 case DW_FORM_block4:
12769 case DW_FORM_block:
12770 case DW_FORM_block1:
12771 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12772 break;
12773 case DW_FORM_exprloc:
12774 fprintf_unfiltered (f, "expression: size %u",
12775 DW_BLOCK (&die->attrs[i])->size);
12776 break;
12777 case DW_FORM_ref1:
12778 case DW_FORM_ref2:
12779 case DW_FORM_ref4:
12780 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12781 (long) (DW_ADDR (&die->attrs[i])));
12782 break;
12783 case DW_FORM_data1:
12784 case DW_FORM_data2:
12785 case DW_FORM_data4:
12786 case DW_FORM_data8:
12787 case DW_FORM_udata:
12788 case DW_FORM_sdata:
12789 fprintf_unfiltered (f, "constant: %s",
12790 pulongest (DW_UNSND (&die->attrs[i])));
12791 break;
12792 case DW_FORM_sec_offset:
12793 fprintf_unfiltered (f, "section offset: %s",
12794 pulongest (DW_UNSND (&die->attrs[i])));
12795 break;
12796 case DW_FORM_sig8:
12797 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12798 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12799 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12800 else
12801 fprintf_unfiltered (f, "signatured type, offset: unknown");
12802 break;
12803 case DW_FORM_string:
12804 case DW_FORM_strp:
12805 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12806 DW_STRING (&die->attrs[i])
12807 ? DW_STRING (&die->attrs[i]) : "",
12808 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12809 break;
12810 case DW_FORM_flag:
12811 if (DW_UNSND (&die->attrs[i]))
12812 fprintf_unfiltered (f, "flag: TRUE");
12813 else
12814 fprintf_unfiltered (f, "flag: FALSE");
12815 break;
12816 case DW_FORM_flag_present:
12817 fprintf_unfiltered (f, "flag: TRUE");
12818 break;
12819 case DW_FORM_indirect:
12820 /* the reader will have reduced the indirect form to
12821 the "base form" so this form should not occur */
12822 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12823 break;
12824 default:
12825 fprintf_unfiltered (f, "unsupported attribute form: %d.",
12826 die->attrs[i].form);
12827 break;
12828 }
12829 fprintf_unfiltered (f, "\n");
12830 }
12831 }
12832
12833 static void
12834 dump_die_for_error (struct die_info *die)
12835 {
12836 dump_die_shallow (gdb_stderr, 0, die);
12837 }
12838
12839 static void
12840 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12841 {
12842 int indent = level * 4;
12843
12844 gdb_assert (die != NULL);
12845
12846 if (level >= max_level)
12847 return;
12848
12849 dump_die_shallow (f, indent, die);
12850
12851 if (die->child != NULL)
12852 {
12853 print_spaces (indent, f);
12854 fprintf_unfiltered (f, " Children:");
12855 if (level + 1 < max_level)
12856 {
12857 fprintf_unfiltered (f, "\n");
12858 dump_die_1 (f, level + 1, max_level, die->child);
12859 }
12860 else
12861 {
12862 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12863 }
12864 }
12865
12866 if (die->sibling != NULL && level > 0)
12867 {
12868 dump_die_1 (f, level, max_level, die->sibling);
12869 }
12870 }
12871
12872 /* This is called from the pdie macro in gdbinit.in.
12873 It's not static so gcc will keep a copy callable from gdb. */
12874
12875 void
12876 dump_die (struct die_info *die, int max_level)
12877 {
12878 dump_die_1 (gdb_stdlog, 0, max_level, die);
12879 }
12880
12881 static void
12882 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12883 {
12884 void **slot;
12885
12886 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12887
12888 *slot = die;
12889 }
12890
12891 static int
12892 is_ref_attr (struct attribute *attr)
12893 {
12894 switch (attr->form)
12895 {
12896 case DW_FORM_ref_addr:
12897 case DW_FORM_ref1:
12898 case DW_FORM_ref2:
12899 case DW_FORM_ref4:
12900 case DW_FORM_ref8:
12901 case DW_FORM_ref_udata:
12902 return 1;
12903 default:
12904 return 0;
12905 }
12906 }
12907
12908 static unsigned int
12909 dwarf2_get_ref_die_offset (struct attribute *attr)
12910 {
12911 if (is_ref_attr (attr))
12912 return DW_ADDR (attr);
12913
12914 complaint (&symfile_complaints,
12915 _("unsupported die ref attribute form: '%s'"),
12916 dwarf_form_name (attr->form));
12917 return 0;
12918 }
12919
12920 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12921 * the value held by the attribute is not constant. */
12922
12923 static LONGEST
12924 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12925 {
12926 if (attr->form == DW_FORM_sdata)
12927 return DW_SND (attr);
12928 else if (attr->form == DW_FORM_udata
12929 || attr->form == DW_FORM_data1
12930 || attr->form == DW_FORM_data2
12931 || attr->form == DW_FORM_data4
12932 || attr->form == DW_FORM_data8)
12933 return DW_UNSND (attr);
12934 else
12935 {
12936 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12937 dwarf_form_name (attr->form));
12938 return default_value;
12939 }
12940 }
12941
12942 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
12943 unit and add it to our queue.
12944 The result is non-zero if PER_CU was queued, otherwise the result is zero
12945 meaning either PER_CU is already queued or it is already loaded. */
12946
12947 static int
12948 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12949 struct dwarf2_per_cu_data *per_cu)
12950 {
12951 /* We may arrive here during partial symbol reading, if we need full
12952 DIEs to process an unusual case (e.g. template arguments). Do
12953 not queue PER_CU, just tell our caller to load its DIEs. */
12954 if (dwarf2_per_objfile->reading_partial_symbols)
12955 {
12956 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12957 return 1;
12958 return 0;
12959 }
12960
12961 /* Mark the dependence relation so that we don't flush PER_CU
12962 too early. */
12963 dwarf2_add_dependence (this_cu, per_cu);
12964
12965 /* If it's already on the queue, we have nothing to do. */
12966 if (per_cu->queued)
12967 return 0;
12968
12969 /* If the compilation unit is already loaded, just mark it as
12970 used. */
12971 if (per_cu->cu != NULL)
12972 {
12973 per_cu->cu->last_used = 0;
12974 return 0;
12975 }
12976
12977 /* Add it to the queue. */
12978 queue_comp_unit (per_cu, this_cu->objfile);
12979
12980 return 1;
12981 }
12982
12983 /* Follow reference or signature attribute ATTR of SRC_DIE.
12984 On entry *REF_CU is the CU of SRC_DIE.
12985 On exit *REF_CU is the CU of the result. */
12986
12987 static struct die_info *
12988 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12989 struct dwarf2_cu **ref_cu)
12990 {
12991 struct die_info *die;
12992
12993 if (is_ref_attr (attr))
12994 die = follow_die_ref (src_die, attr, ref_cu);
12995 else if (attr->form == DW_FORM_sig8)
12996 die = follow_die_sig (src_die, attr, ref_cu);
12997 else
12998 {
12999 dump_die_for_error (src_die);
13000 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13001 (*ref_cu)->objfile->name);
13002 }
13003
13004 return die;
13005 }
13006
13007 /* Follow reference OFFSET.
13008 On entry *REF_CU is the CU of the source die referencing OFFSET.
13009 On exit *REF_CU is the CU of the result.
13010 Returns NULL if OFFSET is invalid. */
13011
13012 static struct die_info *
13013 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13014 {
13015 struct die_info temp_die;
13016 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13017
13018 gdb_assert (cu->per_cu != NULL);
13019
13020 target_cu = cu;
13021
13022 if (cu->per_cu->from_debug_types)
13023 {
13024 /* .debug_types CUs cannot reference anything outside their CU.
13025 If they need to, they have to reference a signatured type via
13026 DW_FORM_sig8. */
13027 if (! offset_in_cu_p (&cu->header, offset))
13028 return NULL;
13029 }
13030 else if (! offset_in_cu_p (&cu->header, offset))
13031 {
13032 struct dwarf2_per_cu_data *per_cu;
13033
13034 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13035
13036 /* If necessary, add it to the queue and load its DIEs. */
13037 if (maybe_queue_comp_unit (cu, per_cu))
13038 load_full_comp_unit (per_cu, cu->objfile);
13039
13040 target_cu = per_cu->cu;
13041 }
13042 else if (cu->dies == NULL)
13043 {
13044 /* We're loading full DIEs during partial symbol reading. */
13045 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13046 load_full_comp_unit (cu->per_cu, cu->objfile);
13047 }
13048
13049 *ref_cu = target_cu;
13050 temp_die.offset = offset;
13051 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13052 }
13053
13054 /* Follow reference attribute ATTR of SRC_DIE.
13055 On entry *REF_CU is the CU of SRC_DIE.
13056 On exit *REF_CU is the CU of the result. */
13057
13058 static struct die_info *
13059 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13060 struct dwarf2_cu **ref_cu)
13061 {
13062 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13063 struct dwarf2_cu *cu = *ref_cu;
13064 struct die_info *die;
13065
13066 die = follow_die_offset (offset, ref_cu);
13067 if (!die)
13068 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13069 "at 0x%x [in module %s]"),
13070 offset, src_die->offset, cu->objfile->name);
13071
13072 return die;
13073 }
13074
13075 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13076 value is intended for DW_OP_call*. */
13077
13078 struct dwarf2_locexpr_baton
13079 dwarf2_fetch_die_location_block (unsigned int offset,
13080 struct dwarf2_per_cu_data *per_cu,
13081 CORE_ADDR (*get_frame_pc) (void *baton),
13082 void *baton)
13083 {
13084 struct dwarf2_cu *cu = per_cu->cu;
13085 struct die_info *die;
13086 struct attribute *attr;
13087 struct dwarf2_locexpr_baton retval;
13088
13089 dw2_setup (per_cu->objfile);
13090
13091 die = follow_die_offset (offset, &cu);
13092 if (!die)
13093 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13094 offset, per_cu->cu->objfile->name);
13095
13096 attr = dwarf2_attr (die, DW_AT_location, cu);
13097 if (!attr)
13098 {
13099 /* DWARF: "If there is no such attribute, then there is no effect.". */
13100
13101 retval.data = NULL;
13102 retval.size = 0;
13103 }
13104 else if (attr_form_is_section_offset (attr))
13105 {
13106 struct dwarf2_loclist_baton loclist_baton;
13107 CORE_ADDR pc = (*get_frame_pc) (baton);
13108 size_t size;
13109
13110 fill_in_loclist_baton (cu, &loclist_baton, attr);
13111
13112 retval.data = dwarf2_find_location_expression (&loclist_baton,
13113 &size, pc);
13114 retval.size = size;
13115 }
13116 else
13117 {
13118 if (!attr_form_is_block (attr))
13119 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13120 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13121 offset, per_cu->cu->objfile->name);
13122
13123 retval.data = DW_BLOCK (attr)->data;
13124 retval.size = DW_BLOCK (attr)->size;
13125 }
13126 retval.per_cu = cu->per_cu;
13127 return retval;
13128 }
13129
13130 /* Follow the signature attribute ATTR in SRC_DIE.
13131 On entry *REF_CU is the CU of SRC_DIE.
13132 On exit *REF_CU is the CU of the result. */
13133
13134 static struct die_info *
13135 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13136 struct dwarf2_cu **ref_cu)
13137 {
13138 struct objfile *objfile = (*ref_cu)->objfile;
13139 struct die_info temp_die;
13140 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13141 struct dwarf2_cu *sig_cu;
13142 struct die_info *die;
13143
13144 /* sig_type will be NULL if the signatured type is missing from
13145 the debug info. */
13146 if (sig_type == NULL)
13147 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13148 "at 0x%x [in module %s]"),
13149 src_die->offset, objfile->name);
13150
13151 /* If necessary, add it to the queue and load its DIEs. */
13152
13153 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13154 read_signatured_type (objfile, sig_type);
13155
13156 gdb_assert (sig_type->per_cu.cu != NULL);
13157
13158 sig_cu = sig_type->per_cu.cu;
13159 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13160 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13161 if (die)
13162 {
13163 *ref_cu = sig_cu;
13164 return die;
13165 }
13166
13167 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13168 "at 0x%x [in module %s]"),
13169 sig_type->type_offset, src_die->offset, objfile->name);
13170 }
13171
13172 /* Given an offset of a signatured type, return its signatured_type. */
13173
13174 static struct signatured_type *
13175 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13176 {
13177 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13178 unsigned int length, initial_length_size;
13179 unsigned int sig_offset;
13180 struct signatured_type find_entry, *type_sig;
13181
13182 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13183 sig_offset = (initial_length_size
13184 + 2 /*version*/
13185 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13186 + 1 /*address_size*/);
13187 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13188 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13189
13190 /* This is only used to lookup previously recorded types.
13191 If we didn't find it, it's our bug. */
13192 gdb_assert (type_sig != NULL);
13193 gdb_assert (offset == type_sig->offset);
13194
13195 return type_sig;
13196 }
13197
13198 /* Read in signatured type at OFFSET and build its CU and die(s). */
13199
13200 static void
13201 read_signatured_type_at_offset (struct objfile *objfile,
13202 unsigned int offset)
13203 {
13204 struct signatured_type *type_sig;
13205
13206 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13207
13208 /* We have the section offset, but we need the signature to do the
13209 hash table lookup. */
13210 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13211
13212 gdb_assert (type_sig->per_cu.cu == NULL);
13213
13214 read_signatured_type (objfile, type_sig);
13215
13216 gdb_assert (type_sig->per_cu.cu != NULL);
13217 }
13218
13219 /* Read in a signatured type and build its CU and DIEs. */
13220
13221 static void
13222 read_signatured_type (struct objfile *objfile,
13223 struct signatured_type *type_sig)
13224 {
13225 gdb_byte *types_ptr;
13226 struct die_reader_specs reader_specs;
13227 struct dwarf2_cu *cu;
13228 ULONGEST signature;
13229 struct cleanup *back_to, *free_cu_cleanup;
13230
13231 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13232 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13233
13234 gdb_assert (type_sig->per_cu.cu == NULL);
13235
13236 cu = xmalloc (sizeof (*cu));
13237 init_one_comp_unit (cu, objfile);
13238
13239 type_sig->per_cu.cu = cu;
13240 cu->per_cu = &type_sig->per_cu;
13241
13242 /* If an error occurs while loading, release our storage. */
13243 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13244
13245 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13246 types_ptr, objfile->obfd);
13247 gdb_assert (signature == type_sig->signature);
13248
13249 cu->die_hash
13250 = htab_create_alloc_ex (cu->header.length / 12,
13251 die_hash,
13252 die_eq,
13253 NULL,
13254 &cu->comp_unit_obstack,
13255 hashtab_obstack_allocate,
13256 dummy_obstack_deallocate);
13257
13258 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13259 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13260
13261 init_cu_die_reader (&reader_specs, cu);
13262
13263 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13264 NULL /*parent*/);
13265
13266 /* We try not to read any attributes in this function, because not
13267 all objfiles needed for references have been loaded yet, and symbol
13268 table processing isn't initialized. But we have to set the CU language,
13269 or we won't be able to build types correctly. */
13270 prepare_one_comp_unit (cu, cu->dies);
13271
13272 do_cleanups (back_to);
13273
13274 /* We've successfully allocated this compilation unit. Let our caller
13275 clean it up when finished with it. */
13276 discard_cleanups (free_cu_cleanup);
13277
13278 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13279 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13280 }
13281
13282 /* Decode simple location descriptions.
13283 Given a pointer to a dwarf block that defines a location, compute
13284 the location and return the value.
13285
13286 NOTE drow/2003-11-18: This function is called in two situations
13287 now: for the address of static or global variables (partial symbols
13288 only) and for offsets into structures which are expected to be
13289 (more or less) constant. The partial symbol case should go away,
13290 and only the constant case should remain. That will let this
13291 function complain more accurately. A few special modes are allowed
13292 without complaint for global variables (for instance, global
13293 register values and thread-local values).
13294
13295 A location description containing no operations indicates that the
13296 object is optimized out. The return value is 0 for that case.
13297 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13298 callers will only want a very basic result and this can become a
13299 complaint.
13300
13301 Note that stack[0] is unused except as a default error return. */
13302
13303 static CORE_ADDR
13304 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13305 {
13306 struct objfile *objfile = cu->objfile;
13307 int i;
13308 int size = blk->size;
13309 gdb_byte *data = blk->data;
13310 CORE_ADDR stack[64];
13311 int stacki;
13312 unsigned int bytes_read, unsnd;
13313 gdb_byte op;
13314
13315 i = 0;
13316 stacki = 0;
13317 stack[stacki] = 0;
13318 stack[++stacki] = 0;
13319
13320 while (i < size)
13321 {
13322 op = data[i++];
13323 switch (op)
13324 {
13325 case DW_OP_lit0:
13326 case DW_OP_lit1:
13327 case DW_OP_lit2:
13328 case DW_OP_lit3:
13329 case DW_OP_lit4:
13330 case DW_OP_lit5:
13331 case DW_OP_lit6:
13332 case DW_OP_lit7:
13333 case DW_OP_lit8:
13334 case DW_OP_lit9:
13335 case DW_OP_lit10:
13336 case DW_OP_lit11:
13337 case DW_OP_lit12:
13338 case DW_OP_lit13:
13339 case DW_OP_lit14:
13340 case DW_OP_lit15:
13341 case DW_OP_lit16:
13342 case DW_OP_lit17:
13343 case DW_OP_lit18:
13344 case DW_OP_lit19:
13345 case DW_OP_lit20:
13346 case DW_OP_lit21:
13347 case DW_OP_lit22:
13348 case DW_OP_lit23:
13349 case DW_OP_lit24:
13350 case DW_OP_lit25:
13351 case DW_OP_lit26:
13352 case DW_OP_lit27:
13353 case DW_OP_lit28:
13354 case DW_OP_lit29:
13355 case DW_OP_lit30:
13356 case DW_OP_lit31:
13357 stack[++stacki] = op - DW_OP_lit0;
13358 break;
13359
13360 case DW_OP_reg0:
13361 case DW_OP_reg1:
13362 case DW_OP_reg2:
13363 case DW_OP_reg3:
13364 case DW_OP_reg4:
13365 case DW_OP_reg5:
13366 case DW_OP_reg6:
13367 case DW_OP_reg7:
13368 case DW_OP_reg8:
13369 case DW_OP_reg9:
13370 case DW_OP_reg10:
13371 case DW_OP_reg11:
13372 case DW_OP_reg12:
13373 case DW_OP_reg13:
13374 case DW_OP_reg14:
13375 case DW_OP_reg15:
13376 case DW_OP_reg16:
13377 case DW_OP_reg17:
13378 case DW_OP_reg18:
13379 case DW_OP_reg19:
13380 case DW_OP_reg20:
13381 case DW_OP_reg21:
13382 case DW_OP_reg22:
13383 case DW_OP_reg23:
13384 case DW_OP_reg24:
13385 case DW_OP_reg25:
13386 case DW_OP_reg26:
13387 case DW_OP_reg27:
13388 case DW_OP_reg28:
13389 case DW_OP_reg29:
13390 case DW_OP_reg30:
13391 case DW_OP_reg31:
13392 stack[++stacki] = op - DW_OP_reg0;
13393 if (i < size)
13394 dwarf2_complex_location_expr_complaint ();
13395 break;
13396
13397 case DW_OP_regx:
13398 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13399 i += bytes_read;
13400 stack[++stacki] = unsnd;
13401 if (i < size)
13402 dwarf2_complex_location_expr_complaint ();
13403 break;
13404
13405 case DW_OP_addr:
13406 stack[++stacki] = read_address (objfile->obfd, &data[i],
13407 cu, &bytes_read);
13408 i += bytes_read;
13409 break;
13410
13411 case DW_OP_const1u:
13412 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13413 i += 1;
13414 break;
13415
13416 case DW_OP_const1s:
13417 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13418 i += 1;
13419 break;
13420
13421 case DW_OP_const2u:
13422 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13423 i += 2;
13424 break;
13425
13426 case DW_OP_const2s:
13427 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13428 i += 2;
13429 break;
13430
13431 case DW_OP_const4u:
13432 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13433 i += 4;
13434 break;
13435
13436 case DW_OP_const4s:
13437 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13438 i += 4;
13439 break;
13440
13441 case DW_OP_constu:
13442 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13443 &bytes_read);
13444 i += bytes_read;
13445 break;
13446
13447 case DW_OP_consts:
13448 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13449 i += bytes_read;
13450 break;
13451
13452 case DW_OP_dup:
13453 stack[stacki + 1] = stack[stacki];
13454 stacki++;
13455 break;
13456
13457 case DW_OP_plus:
13458 stack[stacki - 1] += stack[stacki];
13459 stacki--;
13460 break;
13461
13462 case DW_OP_plus_uconst:
13463 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13464 i += bytes_read;
13465 break;
13466
13467 case DW_OP_minus:
13468 stack[stacki - 1] -= stack[stacki];
13469 stacki--;
13470 break;
13471
13472 case DW_OP_deref:
13473 /* If we're not the last op, then we definitely can't encode
13474 this using GDB's address_class enum. This is valid for partial
13475 global symbols, although the variable's address will be bogus
13476 in the psymtab. */
13477 if (i < size)
13478 dwarf2_complex_location_expr_complaint ();
13479 break;
13480
13481 case DW_OP_GNU_push_tls_address:
13482 /* The top of the stack has the offset from the beginning
13483 of the thread control block at which the variable is located. */
13484 /* Nothing should follow this operator, so the top of stack would
13485 be returned. */
13486 /* This is valid for partial global symbols, but the variable's
13487 address will be bogus in the psymtab. */
13488 if (i < size)
13489 dwarf2_complex_location_expr_complaint ();
13490 break;
13491
13492 case DW_OP_GNU_uninit:
13493 break;
13494
13495 default:
13496 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13497 dwarf_stack_op_name (op, 1));
13498 return (stack[stacki]);
13499 }
13500
13501 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13502 outside of the allocated space. Also enforce minimum>0. */
13503 if (stacki >= ARRAY_SIZE (stack) - 1)
13504 {
13505 complaint (&symfile_complaints,
13506 _("location description stack overflow"));
13507 return 0;
13508 }
13509
13510 if (stacki <= 0)
13511 {
13512 complaint (&symfile_complaints,
13513 _("location description stack underflow"));
13514 return 0;
13515 }
13516 }
13517 return (stack[stacki]);
13518 }
13519
13520 /* memory allocation interface */
13521
13522 static struct dwarf_block *
13523 dwarf_alloc_block (struct dwarf2_cu *cu)
13524 {
13525 struct dwarf_block *blk;
13526
13527 blk = (struct dwarf_block *)
13528 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13529 return (blk);
13530 }
13531
13532 static struct abbrev_info *
13533 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13534 {
13535 struct abbrev_info *abbrev;
13536
13537 abbrev = (struct abbrev_info *)
13538 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13539 memset (abbrev, 0, sizeof (struct abbrev_info));
13540 return (abbrev);
13541 }
13542
13543 static struct die_info *
13544 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13545 {
13546 struct die_info *die;
13547 size_t size = sizeof (struct die_info);
13548
13549 if (num_attrs > 1)
13550 size += (num_attrs - 1) * sizeof (struct attribute);
13551
13552 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13553 memset (die, 0, sizeof (struct die_info));
13554 return (die);
13555 }
13556
13557 \f
13558 /* Macro support. */
13559
13560
13561 /* Return the full name of file number I in *LH's file name table.
13562 Use COMP_DIR as the name of the current directory of the
13563 compilation. The result is allocated using xmalloc; the caller is
13564 responsible for freeing it. */
13565 static char *
13566 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13567 {
13568 /* Is the file number a valid index into the line header's file name
13569 table? Remember that file numbers start with one, not zero. */
13570 if (1 <= file && file <= lh->num_file_names)
13571 {
13572 struct file_entry *fe = &lh->file_names[file - 1];
13573
13574 if (IS_ABSOLUTE_PATH (fe->name))
13575 return xstrdup (fe->name);
13576 else
13577 {
13578 const char *dir;
13579 int dir_len;
13580 char *full_name;
13581
13582 if (fe->dir_index)
13583 dir = lh->include_dirs[fe->dir_index - 1];
13584 else
13585 dir = comp_dir;
13586
13587 if (dir)
13588 {
13589 dir_len = strlen (dir);
13590 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13591 strcpy (full_name, dir);
13592 full_name[dir_len] = '/';
13593 strcpy (full_name + dir_len + 1, fe->name);
13594 return full_name;
13595 }
13596 else
13597 return xstrdup (fe->name);
13598 }
13599 }
13600 else
13601 {
13602 /* The compiler produced a bogus file number. We can at least
13603 record the macro definitions made in the file, even if we
13604 won't be able to find the file by name. */
13605 char fake_name[80];
13606
13607 sprintf (fake_name, "<bad macro file number %d>", file);
13608
13609 complaint (&symfile_complaints,
13610 _("bad file number in macro information (%d)"),
13611 file);
13612
13613 return xstrdup (fake_name);
13614 }
13615 }
13616
13617
13618 static struct macro_source_file *
13619 macro_start_file (int file, int line,
13620 struct macro_source_file *current_file,
13621 const char *comp_dir,
13622 struct line_header *lh, struct objfile *objfile)
13623 {
13624 /* The full name of this source file. */
13625 char *full_name = file_full_name (file, lh, comp_dir);
13626
13627 /* We don't create a macro table for this compilation unit
13628 at all until we actually get a filename. */
13629 if (! pending_macros)
13630 pending_macros = new_macro_table (&objfile->objfile_obstack,
13631 objfile->macro_cache);
13632
13633 if (! current_file)
13634 /* If we have no current file, then this must be the start_file
13635 directive for the compilation unit's main source file. */
13636 current_file = macro_set_main (pending_macros, full_name);
13637 else
13638 current_file = macro_include (current_file, line, full_name);
13639
13640 xfree (full_name);
13641
13642 return current_file;
13643 }
13644
13645
13646 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13647 followed by a null byte. */
13648 static char *
13649 copy_string (const char *buf, int len)
13650 {
13651 char *s = xmalloc (len + 1);
13652
13653 memcpy (s, buf, len);
13654 s[len] = '\0';
13655 return s;
13656 }
13657
13658
13659 static const char *
13660 consume_improper_spaces (const char *p, const char *body)
13661 {
13662 if (*p == ' ')
13663 {
13664 complaint (&symfile_complaints,
13665 _("macro definition contains spaces in formal argument list:\n`%s'"),
13666 body);
13667
13668 while (*p == ' ')
13669 p++;
13670 }
13671
13672 return p;
13673 }
13674
13675
13676 static void
13677 parse_macro_definition (struct macro_source_file *file, int line,
13678 const char *body)
13679 {
13680 const char *p;
13681
13682 /* The body string takes one of two forms. For object-like macro
13683 definitions, it should be:
13684
13685 <macro name> " " <definition>
13686
13687 For function-like macro definitions, it should be:
13688
13689 <macro name> "() " <definition>
13690 or
13691 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13692
13693 Spaces may appear only where explicitly indicated, and in the
13694 <definition>.
13695
13696 The Dwarf 2 spec says that an object-like macro's name is always
13697 followed by a space, but versions of GCC around March 2002 omit
13698 the space when the macro's definition is the empty string.
13699
13700 The Dwarf 2 spec says that there should be no spaces between the
13701 formal arguments in a function-like macro's formal argument list,
13702 but versions of GCC around March 2002 include spaces after the
13703 commas. */
13704
13705
13706 /* Find the extent of the macro name. The macro name is terminated
13707 by either a space or null character (for an object-like macro) or
13708 an opening paren (for a function-like macro). */
13709 for (p = body; *p; p++)
13710 if (*p == ' ' || *p == '(')
13711 break;
13712
13713 if (*p == ' ' || *p == '\0')
13714 {
13715 /* It's an object-like macro. */
13716 int name_len = p - body;
13717 char *name = copy_string (body, name_len);
13718 const char *replacement;
13719
13720 if (*p == ' ')
13721 replacement = body + name_len + 1;
13722 else
13723 {
13724 dwarf2_macro_malformed_definition_complaint (body);
13725 replacement = body + name_len;
13726 }
13727
13728 macro_define_object (file, line, name, replacement);
13729
13730 xfree (name);
13731 }
13732 else if (*p == '(')
13733 {
13734 /* It's a function-like macro. */
13735 char *name = copy_string (body, p - body);
13736 int argc = 0;
13737 int argv_size = 1;
13738 char **argv = xmalloc (argv_size * sizeof (*argv));
13739
13740 p++;
13741
13742 p = consume_improper_spaces (p, body);
13743
13744 /* Parse the formal argument list. */
13745 while (*p && *p != ')')
13746 {
13747 /* Find the extent of the current argument name. */
13748 const char *arg_start = p;
13749
13750 while (*p && *p != ',' && *p != ')' && *p != ' ')
13751 p++;
13752
13753 if (! *p || p == arg_start)
13754 dwarf2_macro_malformed_definition_complaint (body);
13755 else
13756 {
13757 /* Make sure argv has room for the new argument. */
13758 if (argc >= argv_size)
13759 {
13760 argv_size *= 2;
13761 argv = xrealloc (argv, argv_size * sizeof (*argv));
13762 }
13763
13764 argv[argc++] = copy_string (arg_start, p - arg_start);
13765 }
13766
13767 p = consume_improper_spaces (p, body);
13768
13769 /* Consume the comma, if present. */
13770 if (*p == ',')
13771 {
13772 p++;
13773
13774 p = consume_improper_spaces (p, body);
13775 }
13776 }
13777
13778 if (*p == ')')
13779 {
13780 p++;
13781
13782 if (*p == ' ')
13783 /* Perfectly formed definition, no complaints. */
13784 macro_define_function (file, line, name,
13785 argc, (const char **) argv,
13786 p + 1);
13787 else if (*p == '\0')
13788 {
13789 /* Complain, but do define it. */
13790 dwarf2_macro_malformed_definition_complaint (body);
13791 macro_define_function (file, line, name,
13792 argc, (const char **) argv,
13793 p);
13794 }
13795 else
13796 /* Just complain. */
13797 dwarf2_macro_malformed_definition_complaint (body);
13798 }
13799 else
13800 /* Just complain. */
13801 dwarf2_macro_malformed_definition_complaint (body);
13802
13803 xfree (name);
13804 {
13805 int i;
13806
13807 for (i = 0; i < argc; i++)
13808 xfree (argv[i]);
13809 }
13810 xfree (argv);
13811 }
13812 else
13813 dwarf2_macro_malformed_definition_complaint (body);
13814 }
13815
13816
13817 static void
13818 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13819 char *comp_dir, bfd *abfd,
13820 struct dwarf2_cu *cu)
13821 {
13822 gdb_byte *mac_ptr, *mac_end;
13823 struct macro_source_file *current_file = 0;
13824 enum dwarf_macinfo_record_type macinfo_type;
13825 int at_commandline;
13826
13827 dwarf2_read_section (dwarf2_per_objfile->objfile,
13828 &dwarf2_per_objfile->macinfo);
13829 if (dwarf2_per_objfile->macinfo.buffer == NULL)
13830 {
13831 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13832 return;
13833 }
13834
13835 /* First pass: Find the name of the base filename.
13836 This filename is needed in order to process all macros whose definition
13837 (or undefinition) comes from the command line. These macros are defined
13838 before the first DW_MACINFO_start_file entry, and yet still need to be
13839 associated to the base file.
13840
13841 To determine the base file name, we scan the macro definitions until we
13842 reach the first DW_MACINFO_start_file entry. We then initialize
13843 CURRENT_FILE accordingly so that any macro definition found before the
13844 first DW_MACINFO_start_file can still be associated to the base file. */
13845
13846 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13847 mac_end = dwarf2_per_objfile->macinfo.buffer
13848 + dwarf2_per_objfile->macinfo.size;
13849
13850 do
13851 {
13852 /* Do we at least have room for a macinfo type byte? */
13853 if (mac_ptr >= mac_end)
13854 {
13855 /* Complaint is printed during the second pass as GDB will probably
13856 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13857 break;
13858 }
13859
13860 macinfo_type = read_1_byte (abfd, mac_ptr);
13861 mac_ptr++;
13862
13863 switch (macinfo_type)
13864 {
13865 /* A zero macinfo type indicates the end of the macro
13866 information. */
13867 case 0:
13868 break;
13869
13870 case DW_MACINFO_define:
13871 case DW_MACINFO_undef:
13872 /* Only skip the data by MAC_PTR. */
13873 {
13874 unsigned int bytes_read;
13875
13876 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13877 mac_ptr += bytes_read;
13878 read_direct_string (abfd, mac_ptr, &bytes_read);
13879 mac_ptr += bytes_read;
13880 }
13881 break;
13882
13883 case DW_MACINFO_start_file:
13884 {
13885 unsigned int bytes_read;
13886 int line, file;
13887
13888 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13889 mac_ptr += bytes_read;
13890 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13891 mac_ptr += bytes_read;
13892
13893 current_file = macro_start_file (file, line, current_file, comp_dir,
13894 lh, cu->objfile);
13895 }
13896 break;
13897
13898 case DW_MACINFO_end_file:
13899 /* No data to skip by MAC_PTR. */
13900 break;
13901
13902 case DW_MACINFO_vendor_ext:
13903 /* Only skip the data by MAC_PTR. */
13904 {
13905 unsigned int bytes_read;
13906
13907 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13908 mac_ptr += bytes_read;
13909 read_direct_string (abfd, mac_ptr, &bytes_read);
13910 mac_ptr += bytes_read;
13911 }
13912 break;
13913
13914 default:
13915 break;
13916 }
13917 } while (macinfo_type != 0 && current_file == NULL);
13918
13919 /* Second pass: Process all entries.
13920
13921 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13922 command-line macro definitions/undefinitions. This flag is unset when we
13923 reach the first DW_MACINFO_start_file entry. */
13924
13925 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13926
13927 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13928 GDB is still reading the definitions from command line. First
13929 DW_MACINFO_start_file will need to be ignored as it was already executed
13930 to create CURRENT_FILE for the main source holding also the command line
13931 definitions. On first met DW_MACINFO_start_file this flag is reset to
13932 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13933
13934 at_commandline = 1;
13935
13936 do
13937 {
13938 /* Do we at least have room for a macinfo type byte? */
13939 if (mac_ptr >= mac_end)
13940 {
13941 dwarf2_macros_too_long_complaint ();
13942 break;
13943 }
13944
13945 macinfo_type = read_1_byte (abfd, mac_ptr);
13946 mac_ptr++;
13947
13948 switch (macinfo_type)
13949 {
13950 /* A zero macinfo type indicates the end of the macro
13951 information. */
13952 case 0:
13953 break;
13954
13955 case DW_MACINFO_define:
13956 case DW_MACINFO_undef:
13957 {
13958 unsigned int bytes_read;
13959 int line;
13960 char *body;
13961
13962 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13963 mac_ptr += bytes_read;
13964 body = read_direct_string (abfd, mac_ptr, &bytes_read);
13965 mac_ptr += bytes_read;
13966
13967 if (! current_file)
13968 {
13969 /* DWARF violation as no main source is present. */
13970 complaint (&symfile_complaints,
13971 _("debug info with no main source gives macro %s "
13972 "on line %d: %s"),
13973 macinfo_type == DW_MACINFO_define ?
13974 _("definition") :
13975 macinfo_type == DW_MACINFO_undef ?
13976 _("undefinition") :
13977 _("something-or-other"), line, body);
13978 break;
13979 }
13980 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13981 complaint (&symfile_complaints,
13982 _("debug info gives %s macro %s with %s line %d: %s"),
13983 at_commandline ? _("command-line") : _("in-file"),
13984 macinfo_type == DW_MACINFO_define ?
13985 _("definition") :
13986 macinfo_type == DW_MACINFO_undef ?
13987 _("undefinition") :
13988 _("something-or-other"),
13989 line == 0 ? _("zero") : _("non-zero"), line, body);
13990
13991 if (macinfo_type == DW_MACINFO_define)
13992 parse_macro_definition (current_file, line, body);
13993 else if (macinfo_type == DW_MACINFO_undef)
13994 macro_undef (current_file, line, body);
13995 }
13996 break;
13997
13998 case DW_MACINFO_start_file:
13999 {
14000 unsigned int bytes_read;
14001 int line, file;
14002
14003 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14004 mac_ptr += bytes_read;
14005 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14006 mac_ptr += bytes_read;
14007
14008 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
14009 complaint (&symfile_complaints,
14010 _("debug info gives source %d included "
14011 "from %s at %s line %d"),
14012 file, at_commandline ? _("command-line") : _("file"),
14013 line == 0 ? _("zero") : _("non-zero"), line);
14014
14015 if (at_commandline)
14016 {
14017 /* This DW_MACINFO_start_file was executed in the pass one. */
14018 at_commandline = 0;
14019 }
14020 else
14021 current_file = macro_start_file (file, line,
14022 current_file, comp_dir,
14023 lh, cu->objfile);
14024 }
14025 break;
14026
14027 case DW_MACINFO_end_file:
14028 if (! current_file)
14029 complaint (&symfile_complaints,
14030 _("macro debug info has an unmatched `close_file' directive"));
14031 else
14032 {
14033 current_file = current_file->included_by;
14034 if (! current_file)
14035 {
14036 enum dwarf_macinfo_record_type next_type;
14037
14038 /* GCC circa March 2002 doesn't produce the zero
14039 type byte marking the end of the compilation
14040 unit. Complain if it's not there, but exit no
14041 matter what. */
14042
14043 /* Do we at least have room for a macinfo type byte? */
14044 if (mac_ptr >= mac_end)
14045 {
14046 dwarf2_macros_too_long_complaint ();
14047 return;
14048 }
14049
14050 /* We don't increment mac_ptr here, so this is just
14051 a look-ahead. */
14052 next_type = read_1_byte (abfd, mac_ptr);
14053 if (next_type != 0)
14054 complaint (&symfile_complaints,
14055 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
14056
14057 return;
14058 }
14059 }
14060 break;
14061
14062 case DW_MACINFO_vendor_ext:
14063 {
14064 unsigned int bytes_read;
14065 int constant;
14066 char *string;
14067
14068 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14069 mac_ptr += bytes_read;
14070 string = read_direct_string (abfd, mac_ptr, &bytes_read);
14071 mac_ptr += bytes_read;
14072
14073 /* We don't recognize any vendor extensions. */
14074 }
14075 break;
14076 }
14077 } while (macinfo_type != 0);
14078 }
14079
14080 /* Check if the attribute's form is a DW_FORM_block*
14081 if so return true else false. */
14082 static int
14083 attr_form_is_block (struct attribute *attr)
14084 {
14085 return (attr == NULL ? 0 :
14086 attr->form == DW_FORM_block1
14087 || attr->form == DW_FORM_block2
14088 || attr->form == DW_FORM_block4
14089 || attr->form == DW_FORM_block
14090 || attr->form == DW_FORM_exprloc);
14091 }
14092
14093 /* Return non-zero if ATTR's value is a section offset --- classes
14094 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14095 You may use DW_UNSND (attr) to retrieve such offsets.
14096
14097 Section 7.5.4, "Attribute Encodings", explains that no attribute
14098 may have a value that belongs to more than one of these classes; it
14099 would be ambiguous if we did, because we use the same forms for all
14100 of them. */
14101 static int
14102 attr_form_is_section_offset (struct attribute *attr)
14103 {
14104 return (attr->form == DW_FORM_data4
14105 || attr->form == DW_FORM_data8
14106 || attr->form == DW_FORM_sec_offset);
14107 }
14108
14109
14110 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14111 zero otherwise. When this function returns true, you can apply
14112 dwarf2_get_attr_constant_value to it.
14113
14114 However, note that for some attributes you must check
14115 attr_form_is_section_offset before using this test. DW_FORM_data4
14116 and DW_FORM_data8 are members of both the constant class, and of
14117 the classes that contain offsets into other debug sections
14118 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14119 that, if an attribute's can be either a constant or one of the
14120 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14121 taken as section offsets, not constants. */
14122 static int
14123 attr_form_is_constant (struct attribute *attr)
14124 {
14125 switch (attr->form)
14126 {
14127 case DW_FORM_sdata:
14128 case DW_FORM_udata:
14129 case DW_FORM_data1:
14130 case DW_FORM_data2:
14131 case DW_FORM_data4:
14132 case DW_FORM_data8:
14133 return 1;
14134 default:
14135 return 0;
14136 }
14137 }
14138
14139 /* A helper function that fills in a dwarf2_loclist_baton. */
14140
14141 static void
14142 fill_in_loclist_baton (struct dwarf2_cu *cu,
14143 struct dwarf2_loclist_baton *baton,
14144 struct attribute *attr)
14145 {
14146 dwarf2_read_section (dwarf2_per_objfile->objfile,
14147 &dwarf2_per_objfile->loc);
14148
14149 baton->per_cu = cu->per_cu;
14150 gdb_assert (baton->per_cu);
14151 /* We don't know how long the location list is, but make sure we
14152 don't run off the edge of the section. */
14153 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14154 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14155 baton->base_address = cu->base_address;
14156 }
14157
14158 static void
14159 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14160 struct dwarf2_cu *cu)
14161 {
14162 if (attr_form_is_section_offset (attr)
14163 /* ".debug_loc" may not exist at all, or the offset may be outside
14164 the section. If so, fall through to the complaint in the
14165 other branch. */
14166 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
14167 {
14168 struct dwarf2_loclist_baton *baton;
14169
14170 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14171 sizeof (struct dwarf2_loclist_baton));
14172
14173 fill_in_loclist_baton (cu, baton, attr);
14174
14175 if (cu->base_known == 0)
14176 complaint (&symfile_complaints,
14177 _("Location list used without specifying the CU base address."));
14178
14179 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14180 SYMBOL_LOCATION_BATON (sym) = baton;
14181 }
14182 else
14183 {
14184 struct dwarf2_locexpr_baton *baton;
14185
14186 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14187 sizeof (struct dwarf2_locexpr_baton));
14188 baton->per_cu = cu->per_cu;
14189 gdb_assert (baton->per_cu);
14190
14191 if (attr_form_is_block (attr))
14192 {
14193 /* Note that we're just copying the block's data pointer
14194 here, not the actual data. We're still pointing into the
14195 info_buffer for SYM's objfile; right now we never release
14196 that buffer, but when we do clean up properly this may
14197 need to change. */
14198 baton->size = DW_BLOCK (attr)->size;
14199 baton->data = DW_BLOCK (attr)->data;
14200 }
14201 else
14202 {
14203 dwarf2_invalid_attrib_class_complaint ("location description",
14204 SYMBOL_NATURAL_NAME (sym));
14205 baton->size = 0;
14206 baton->data = NULL;
14207 }
14208
14209 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14210 SYMBOL_LOCATION_BATON (sym) = baton;
14211 }
14212 }
14213
14214 /* Return the OBJFILE associated with the compilation unit CU. If CU
14215 came from a separate debuginfo file, then the master objfile is
14216 returned. */
14217
14218 struct objfile *
14219 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14220 {
14221 struct objfile *objfile = per_cu->objfile;
14222
14223 /* Return the master objfile, so that we can report and look up the
14224 correct file containing this variable. */
14225 if (objfile->separate_debug_objfile_backlink)
14226 objfile = objfile->separate_debug_objfile_backlink;
14227
14228 return objfile;
14229 }
14230
14231 /* Return the address size given in the compilation unit header for CU. */
14232
14233 CORE_ADDR
14234 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14235 {
14236 if (per_cu->cu)
14237 return per_cu->cu->header.addr_size;
14238 else
14239 {
14240 /* If the CU is not currently read in, we re-read its header. */
14241 struct objfile *objfile = per_cu->objfile;
14242 struct dwarf2_per_objfile *per_objfile
14243 = objfile_data (objfile, dwarf2_objfile_data_key);
14244 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14245 struct comp_unit_head cu_header;
14246
14247 memset (&cu_header, 0, sizeof cu_header);
14248 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14249 return cu_header.addr_size;
14250 }
14251 }
14252
14253 /* Return the offset size given in the compilation unit header for CU. */
14254
14255 int
14256 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14257 {
14258 if (per_cu->cu)
14259 return per_cu->cu->header.offset_size;
14260 else
14261 {
14262 /* If the CU is not currently read in, we re-read its header. */
14263 struct objfile *objfile = per_cu->objfile;
14264 struct dwarf2_per_objfile *per_objfile
14265 = objfile_data (objfile, dwarf2_objfile_data_key);
14266 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14267 struct comp_unit_head cu_header;
14268
14269 memset (&cu_header, 0, sizeof cu_header);
14270 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14271 return cu_header.offset_size;
14272 }
14273 }
14274
14275 /* Return the text offset of the CU. The returned offset comes from
14276 this CU's objfile. If this objfile came from a separate debuginfo
14277 file, then the offset may be different from the corresponding
14278 offset in the parent objfile. */
14279
14280 CORE_ADDR
14281 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14282 {
14283 struct objfile *objfile = per_cu->objfile;
14284
14285 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14286 }
14287
14288 /* Locate the .debug_info compilation unit from CU's objfile which contains
14289 the DIE at OFFSET. Raises an error on failure. */
14290
14291 static struct dwarf2_per_cu_data *
14292 dwarf2_find_containing_comp_unit (unsigned int offset,
14293 struct objfile *objfile)
14294 {
14295 struct dwarf2_per_cu_data *this_cu;
14296 int low, high;
14297
14298 low = 0;
14299 high = dwarf2_per_objfile->n_comp_units - 1;
14300 while (high > low)
14301 {
14302 int mid = low + (high - low) / 2;
14303
14304 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14305 high = mid;
14306 else
14307 low = mid + 1;
14308 }
14309 gdb_assert (low == high);
14310 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14311 {
14312 if (low == 0)
14313 error (_("Dwarf Error: could not find partial DIE containing "
14314 "offset 0x%lx [in module %s]"),
14315 (long) offset, bfd_get_filename (objfile->obfd));
14316
14317 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14318 return dwarf2_per_objfile->all_comp_units[low-1];
14319 }
14320 else
14321 {
14322 this_cu = dwarf2_per_objfile->all_comp_units[low];
14323 if (low == dwarf2_per_objfile->n_comp_units - 1
14324 && offset >= this_cu->offset + this_cu->length)
14325 error (_("invalid dwarf2 offset %u"), offset);
14326 gdb_assert (offset < this_cu->offset + this_cu->length);
14327 return this_cu;
14328 }
14329 }
14330
14331 /* Locate the compilation unit from OBJFILE which is located at exactly
14332 OFFSET. Raises an error on failure. */
14333
14334 static struct dwarf2_per_cu_data *
14335 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14336 {
14337 struct dwarf2_per_cu_data *this_cu;
14338
14339 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14340 if (this_cu->offset != offset)
14341 error (_("no compilation unit with offset %u."), offset);
14342 return this_cu;
14343 }
14344
14345 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
14346
14347 static void
14348 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14349 {
14350 memset (cu, 0, sizeof (*cu));
14351 cu->objfile = objfile;
14352 obstack_init (&cu->comp_unit_obstack);
14353 }
14354
14355 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14356
14357 static void
14358 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14359 {
14360 struct attribute *attr;
14361
14362 /* Set the language we're debugging. */
14363 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14364 if (attr)
14365 set_cu_language (DW_UNSND (attr), cu);
14366 else
14367 set_cu_language (language_minimal, cu);
14368 }
14369
14370 /* Release one cached compilation unit, CU. We unlink it from the tree
14371 of compilation units, but we don't remove it from the read_in_chain;
14372 the caller is responsible for that.
14373 NOTE: DATA is a void * because this function is also used as a
14374 cleanup routine. */
14375
14376 static void
14377 free_one_comp_unit (void *data)
14378 {
14379 struct dwarf2_cu *cu = data;
14380
14381 if (cu->per_cu != NULL)
14382 cu->per_cu->cu = NULL;
14383 cu->per_cu = NULL;
14384
14385 obstack_free (&cu->comp_unit_obstack, NULL);
14386
14387 xfree (cu);
14388 }
14389
14390 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14391 when we're finished with it. We can't free the pointer itself, but be
14392 sure to unlink it from the cache. Also release any associated storage
14393 and perform cache maintenance.
14394
14395 Only used during partial symbol parsing. */
14396
14397 static void
14398 free_stack_comp_unit (void *data)
14399 {
14400 struct dwarf2_cu *cu = data;
14401
14402 obstack_free (&cu->comp_unit_obstack, NULL);
14403 cu->partial_dies = NULL;
14404
14405 if (cu->per_cu != NULL)
14406 {
14407 /* This compilation unit is on the stack in our caller, so we
14408 should not xfree it. Just unlink it. */
14409 cu->per_cu->cu = NULL;
14410 cu->per_cu = NULL;
14411
14412 /* If we had a per-cu pointer, then we may have other compilation
14413 units loaded, so age them now. */
14414 age_cached_comp_units ();
14415 }
14416 }
14417
14418 /* Free all cached compilation units. */
14419
14420 static void
14421 free_cached_comp_units (void *data)
14422 {
14423 struct dwarf2_per_cu_data *per_cu, **last_chain;
14424
14425 per_cu = dwarf2_per_objfile->read_in_chain;
14426 last_chain = &dwarf2_per_objfile->read_in_chain;
14427 while (per_cu != NULL)
14428 {
14429 struct dwarf2_per_cu_data *next_cu;
14430
14431 next_cu = per_cu->cu->read_in_chain;
14432
14433 free_one_comp_unit (per_cu->cu);
14434 *last_chain = next_cu;
14435
14436 per_cu = next_cu;
14437 }
14438 }
14439
14440 /* Increase the age counter on each cached compilation unit, and free
14441 any that are too old. */
14442
14443 static void
14444 age_cached_comp_units (void)
14445 {
14446 struct dwarf2_per_cu_data *per_cu, **last_chain;
14447
14448 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14449 per_cu = dwarf2_per_objfile->read_in_chain;
14450 while (per_cu != NULL)
14451 {
14452 per_cu->cu->last_used ++;
14453 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14454 dwarf2_mark (per_cu->cu);
14455 per_cu = per_cu->cu->read_in_chain;
14456 }
14457
14458 per_cu = dwarf2_per_objfile->read_in_chain;
14459 last_chain = &dwarf2_per_objfile->read_in_chain;
14460 while (per_cu != NULL)
14461 {
14462 struct dwarf2_per_cu_data *next_cu;
14463
14464 next_cu = per_cu->cu->read_in_chain;
14465
14466 if (!per_cu->cu->mark)
14467 {
14468 free_one_comp_unit (per_cu->cu);
14469 *last_chain = next_cu;
14470 }
14471 else
14472 last_chain = &per_cu->cu->read_in_chain;
14473
14474 per_cu = next_cu;
14475 }
14476 }
14477
14478 /* Remove a single compilation unit from the cache. */
14479
14480 static void
14481 free_one_cached_comp_unit (void *target_cu)
14482 {
14483 struct dwarf2_per_cu_data *per_cu, **last_chain;
14484
14485 per_cu = dwarf2_per_objfile->read_in_chain;
14486 last_chain = &dwarf2_per_objfile->read_in_chain;
14487 while (per_cu != NULL)
14488 {
14489 struct dwarf2_per_cu_data *next_cu;
14490
14491 next_cu = per_cu->cu->read_in_chain;
14492
14493 if (per_cu->cu == target_cu)
14494 {
14495 free_one_comp_unit (per_cu->cu);
14496 *last_chain = next_cu;
14497 break;
14498 }
14499 else
14500 last_chain = &per_cu->cu->read_in_chain;
14501
14502 per_cu = next_cu;
14503 }
14504 }
14505
14506 /* Release all extra memory associated with OBJFILE. */
14507
14508 void
14509 dwarf2_free_objfile (struct objfile *objfile)
14510 {
14511 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14512
14513 if (dwarf2_per_objfile == NULL)
14514 return;
14515
14516 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14517 free_cached_comp_units (NULL);
14518
14519 if (dwarf2_per_objfile->using_index)
14520 {
14521 int i;
14522
14523 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14524 {
14525 int j;
14526 struct dwarf2_per_cu_data *per_cu =
14527 dwarf2_per_objfile->all_comp_units[i];
14528
14529 if (!per_cu->v.quick->lines)
14530 continue;
14531
14532 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
14533 {
14534 if (per_cu->v.quick->file_names)
14535 xfree ((void *) per_cu->v.quick->file_names[j]);
14536 if (per_cu->v.quick->full_names)
14537 xfree ((void *) per_cu->v.quick->full_names[j]);
14538 }
14539
14540 free_line_header (per_cu->v.quick->lines);
14541 }
14542 }
14543
14544 /* Everything else should be on the objfile obstack. */
14545 }
14546
14547 /* A pair of DIE offset and GDB type pointer. We store these
14548 in a hash table separate from the DIEs, and preserve them
14549 when the DIEs are flushed out of cache. */
14550
14551 struct dwarf2_offset_and_type
14552 {
14553 unsigned int offset;
14554 struct type *type;
14555 };
14556
14557 /* Hash function for a dwarf2_offset_and_type. */
14558
14559 static hashval_t
14560 offset_and_type_hash (const void *item)
14561 {
14562 const struct dwarf2_offset_and_type *ofs = item;
14563
14564 return ofs->offset;
14565 }
14566
14567 /* Equality function for a dwarf2_offset_and_type. */
14568
14569 static int
14570 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14571 {
14572 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14573 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14574
14575 return ofs_lhs->offset == ofs_rhs->offset;
14576 }
14577
14578 /* Set the type associated with DIE to TYPE. Save it in CU's hash
14579 table if necessary. For convenience, return TYPE.
14580
14581 The DIEs reading must have careful ordering to:
14582 * Not cause infite loops trying to read in DIEs as a prerequisite for
14583 reading current DIE.
14584 * Not trying to dereference contents of still incompletely read in types
14585 while reading in other DIEs.
14586 * Enable referencing still incompletely read in types just by a pointer to
14587 the type without accessing its fields.
14588
14589 Therefore caller should follow these rules:
14590 * Try to fetch any prerequisite types we may need to build this DIE type
14591 before building the type and calling set_die_type.
14592 * After building type call set_die_type for current DIE as soon as
14593 possible before fetching more types to complete the current type.
14594 * Make the type as complete as possible before fetching more types. */
14595
14596 static struct type *
14597 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14598 {
14599 struct dwarf2_offset_and_type **slot, ofs;
14600 struct objfile *objfile = cu->objfile;
14601 htab_t *type_hash_ptr;
14602
14603 /* For Ada types, make sure that the gnat-specific data is always
14604 initialized (if not already set). There are a few types where
14605 we should not be doing so, because the type-specific area is
14606 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14607 where the type-specific area is used to store the floatformat).
14608 But this is not a problem, because the gnat-specific information
14609 is actually not needed for these types. */
14610 if (need_gnat_info (cu)
14611 && TYPE_CODE (type) != TYPE_CODE_FUNC
14612 && TYPE_CODE (type) != TYPE_CODE_FLT
14613 && !HAVE_GNAT_AUX_INFO (type))
14614 INIT_GNAT_SPECIFIC (type);
14615
14616 if (cu->per_cu->from_debug_types)
14617 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14618 else
14619 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14620
14621 if (*type_hash_ptr == NULL)
14622 {
14623 *type_hash_ptr
14624 = htab_create_alloc_ex (127,
14625 offset_and_type_hash,
14626 offset_and_type_eq,
14627 NULL,
14628 &objfile->objfile_obstack,
14629 hashtab_obstack_allocate,
14630 dummy_obstack_deallocate);
14631 }
14632
14633 ofs.offset = die->offset;
14634 ofs.type = type;
14635 slot = (struct dwarf2_offset_and_type **)
14636 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14637 if (*slot)
14638 complaint (&symfile_complaints,
14639 _("A problem internal to GDB: DIE 0x%x has type already set"),
14640 die->offset);
14641 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14642 **slot = ofs;
14643 return type;
14644 }
14645
14646 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14647 table, or return NULL if the die does not have a saved type. */
14648
14649 static struct type *
14650 get_die_type_at_offset (unsigned int offset,
14651 struct dwarf2_per_cu_data *per_cu)
14652 {
14653 struct dwarf2_offset_and_type *slot, ofs;
14654 htab_t type_hash;
14655
14656 if (per_cu->from_debug_types)
14657 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14658 else
14659 type_hash = dwarf2_per_objfile->debug_info_type_hash;
14660 if (type_hash == NULL)
14661 return NULL;
14662
14663 ofs.offset = offset;
14664 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14665 if (slot)
14666 return slot->type;
14667 else
14668 return NULL;
14669 }
14670
14671 /* Look up the type for DIE in the appropriate type_hash table,
14672 or return NULL if DIE does not have a saved type. */
14673
14674 static struct type *
14675 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14676 {
14677 return get_die_type_at_offset (die->offset, cu->per_cu);
14678 }
14679
14680 /* Add a dependence relationship from CU to REF_PER_CU. */
14681
14682 static void
14683 dwarf2_add_dependence (struct dwarf2_cu *cu,
14684 struct dwarf2_per_cu_data *ref_per_cu)
14685 {
14686 void **slot;
14687
14688 if (cu->dependencies == NULL)
14689 cu->dependencies
14690 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14691 NULL, &cu->comp_unit_obstack,
14692 hashtab_obstack_allocate,
14693 dummy_obstack_deallocate);
14694
14695 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14696 if (*slot == NULL)
14697 *slot = ref_per_cu;
14698 }
14699
14700 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14701 Set the mark field in every compilation unit in the
14702 cache that we must keep because we are keeping CU. */
14703
14704 static int
14705 dwarf2_mark_helper (void **slot, void *data)
14706 {
14707 struct dwarf2_per_cu_data *per_cu;
14708
14709 per_cu = (struct dwarf2_per_cu_data *) *slot;
14710 if (per_cu->cu->mark)
14711 return 1;
14712 per_cu->cu->mark = 1;
14713
14714 if (per_cu->cu->dependencies != NULL)
14715 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14716
14717 return 1;
14718 }
14719
14720 /* Set the mark field in CU and in every other compilation unit in the
14721 cache that we must keep because we are keeping CU. */
14722
14723 static void
14724 dwarf2_mark (struct dwarf2_cu *cu)
14725 {
14726 if (cu->mark)
14727 return;
14728 cu->mark = 1;
14729 if (cu->dependencies != NULL)
14730 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14731 }
14732
14733 static void
14734 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14735 {
14736 while (per_cu)
14737 {
14738 per_cu->cu->mark = 0;
14739 per_cu = per_cu->cu->read_in_chain;
14740 }
14741 }
14742
14743 /* Trivial hash function for partial_die_info: the hash value of a DIE
14744 is its offset in .debug_info for this objfile. */
14745
14746 static hashval_t
14747 partial_die_hash (const void *item)
14748 {
14749 const struct partial_die_info *part_die = item;
14750
14751 return part_die->offset;
14752 }
14753
14754 /* Trivial comparison function for partial_die_info structures: two DIEs
14755 are equal if they have the same offset. */
14756
14757 static int
14758 partial_die_eq (const void *item_lhs, const void *item_rhs)
14759 {
14760 const struct partial_die_info *part_die_lhs = item_lhs;
14761 const struct partial_die_info *part_die_rhs = item_rhs;
14762
14763 return part_die_lhs->offset == part_die_rhs->offset;
14764 }
14765
14766 static struct cmd_list_element *set_dwarf2_cmdlist;
14767 static struct cmd_list_element *show_dwarf2_cmdlist;
14768
14769 static void
14770 set_dwarf2_cmd (char *args, int from_tty)
14771 {
14772 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14773 }
14774
14775 static void
14776 show_dwarf2_cmd (char *args, int from_tty)
14777 {
14778 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14779 }
14780
14781 /* If section described by INFO was mmapped, munmap it now. */
14782
14783 static void
14784 munmap_section_buffer (struct dwarf2_section_info *info)
14785 {
14786 if (info->was_mmapped)
14787 {
14788 #ifdef HAVE_MMAP
14789 intptr_t begin = (intptr_t) info->buffer;
14790 intptr_t map_begin = begin & ~(pagesize - 1);
14791 size_t map_length = info->size + begin - map_begin;
14792
14793 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14794 #else
14795 /* Without HAVE_MMAP, we should never be here to begin with. */
14796 gdb_assert_not_reached ("no mmap support");
14797 #endif
14798 }
14799 }
14800
14801 /* munmap debug sections for OBJFILE, if necessary. */
14802
14803 static void
14804 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14805 {
14806 struct dwarf2_per_objfile *data = d;
14807
14808 /* This is sorted according to the order they're defined in to make it easier
14809 to keep in sync. */
14810 munmap_section_buffer (&data->info);
14811 munmap_section_buffer (&data->abbrev);
14812 munmap_section_buffer (&data->line);
14813 munmap_section_buffer (&data->loc);
14814 munmap_section_buffer (&data->macinfo);
14815 munmap_section_buffer (&data->str);
14816 munmap_section_buffer (&data->ranges);
14817 munmap_section_buffer (&data->types);
14818 munmap_section_buffer (&data->frame);
14819 munmap_section_buffer (&data->eh_frame);
14820 munmap_section_buffer (&data->gdb_index);
14821 }
14822
14823 \f
14824
14825 /* The contents of the hash table we create when building the string
14826 table. */
14827 struct strtab_entry
14828 {
14829 offset_type offset;
14830 const char *str;
14831 };
14832
14833 /* Hash function for a strtab_entry. */
14834
14835 static hashval_t
14836 hash_strtab_entry (const void *e)
14837 {
14838 const struct strtab_entry *entry = e;
14839 return mapped_index_string_hash (entry->str);
14840 }
14841
14842 /* Equality function for a strtab_entry. */
14843
14844 static int
14845 eq_strtab_entry (const void *a, const void *b)
14846 {
14847 const struct strtab_entry *ea = a;
14848 const struct strtab_entry *eb = b;
14849 return !strcmp (ea->str, eb->str);
14850 }
14851
14852 /* Create a strtab_entry hash table. */
14853
14854 static htab_t
14855 create_strtab (void)
14856 {
14857 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14858 xfree, xcalloc, xfree);
14859 }
14860
14861 /* Add a string to the constant pool. Return the string's offset in
14862 host order. */
14863
14864 static offset_type
14865 add_string (htab_t table, struct obstack *cpool, const char *str)
14866 {
14867 void **slot;
14868 struct strtab_entry entry;
14869 struct strtab_entry *result;
14870
14871 entry.str = str;
14872 slot = htab_find_slot (table, &entry, INSERT);
14873 if (*slot)
14874 result = *slot;
14875 else
14876 {
14877 result = XNEW (struct strtab_entry);
14878 result->offset = obstack_object_size (cpool);
14879 result->str = str;
14880 obstack_grow_str0 (cpool, str);
14881 *slot = result;
14882 }
14883 return result->offset;
14884 }
14885
14886 /* An entry in the symbol table. */
14887 struct symtab_index_entry
14888 {
14889 /* The name of the symbol. */
14890 const char *name;
14891 /* The offset of the name in the constant pool. */
14892 offset_type index_offset;
14893 /* A sorted vector of the indices of all the CUs that hold an object
14894 of this name. */
14895 VEC (offset_type) *cu_indices;
14896 };
14897
14898 /* The symbol table. This is a power-of-2-sized hash table. */
14899 struct mapped_symtab
14900 {
14901 offset_type n_elements;
14902 offset_type size;
14903 struct symtab_index_entry **data;
14904 };
14905
14906 /* Hash function for a symtab_index_entry. */
14907
14908 static hashval_t
14909 hash_symtab_entry (const void *e)
14910 {
14911 const struct symtab_index_entry *entry = e;
14912 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14913 sizeof (offset_type) * VEC_length (offset_type,
14914 entry->cu_indices),
14915 0);
14916 }
14917
14918 /* Equality function for a symtab_index_entry. */
14919
14920 static int
14921 eq_symtab_entry (const void *a, const void *b)
14922 {
14923 const struct symtab_index_entry *ea = a;
14924 const struct symtab_index_entry *eb = b;
14925 int len = VEC_length (offset_type, ea->cu_indices);
14926 if (len != VEC_length (offset_type, eb->cu_indices))
14927 return 0;
14928 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14929 VEC_address (offset_type, eb->cu_indices),
14930 sizeof (offset_type) * len);
14931 }
14932
14933 /* Destroy a symtab_index_entry. */
14934
14935 static void
14936 delete_symtab_entry (void *p)
14937 {
14938 struct symtab_index_entry *entry = p;
14939 VEC_free (offset_type, entry->cu_indices);
14940 xfree (entry);
14941 }
14942
14943 /* Create a hash table holding symtab_index_entry objects. */
14944
14945 static htab_t
14946 create_symbol_hash_table (void)
14947 {
14948 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14949 delete_symtab_entry, xcalloc, xfree);
14950 }
14951
14952 /* Create a new mapped symtab object. */
14953
14954 static struct mapped_symtab *
14955 create_mapped_symtab (void)
14956 {
14957 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14958 symtab->n_elements = 0;
14959 symtab->size = 1024;
14960 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14961 return symtab;
14962 }
14963
14964 /* Destroy a mapped_symtab. */
14965
14966 static void
14967 cleanup_mapped_symtab (void *p)
14968 {
14969 struct mapped_symtab *symtab = p;
14970 /* The contents of the array are freed when the other hash table is
14971 destroyed. */
14972 xfree (symtab->data);
14973 xfree (symtab);
14974 }
14975
14976 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14977 the slot. */
14978
14979 static struct symtab_index_entry **
14980 find_slot (struct mapped_symtab *symtab, const char *name)
14981 {
14982 offset_type index, step, hash = mapped_index_string_hash (name);
14983
14984 index = hash & (symtab->size - 1);
14985 step = ((hash * 17) & (symtab->size - 1)) | 1;
14986
14987 for (;;)
14988 {
14989 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14990 return &symtab->data[index];
14991 index = (index + step) & (symtab->size - 1);
14992 }
14993 }
14994
14995 /* Expand SYMTAB's hash table. */
14996
14997 static void
14998 hash_expand (struct mapped_symtab *symtab)
14999 {
15000 offset_type old_size = symtab->size;
15001 offset_type i;
15002 struct symtab_index_entry **old_entries = symtab->data;
15003
15004 symtab->size *= 2;
15005 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15006
15007 for (i = 0; i < old_size; ++i)
15008 {
15009 if (old_entries[i])
15010 {
15011 struct symtab_index_entry **slot = find_slot (symtab,
15012 old_entries[i]->name);
15013 *slot = old_entries[i];
15014 }
15015 }
15016
15017 xfree (old_entries);
15018 }
15019
15020 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15021 is the index of the CU in which the symbol appears. */
15022
15023 static void
15024 add_index_entry (struct mapped_symtab *symtab, const char *name,
15025 offset_type cu_index)
15026 {
15027 struct symtab_index_entry **slot;
15028
15029 ++symtab->n_elements;
15030 if (4 * symtab->n_elements / 3 >= symtab->size)
15031 hash_expand (symtab);
15032
15033 slot = find_slot (symtab, name);
15034 if (!*slot)
15035 {
15036 *slot = XNEW (struct symtab_index_entry);
15037 (*slot)->name = name;
15038 (*slot)->cu_indices = NULL;
15039 }
15040 /* Don't push an index twice. Due to how we add entries we only
15041 have to check the last one. */
15042 if (VEC_empty (offset_type, (*slot)->cu_indices)
15043 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15044 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15045 }
15046
15047 /* Add a vector of indices to the constant pool. */
15048
15049 static offset_type
15050 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15051 struct symtab_index_entry *entry)
15052 {
15053 void **slot;
15054
15055 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15056 if (!*slot)
15057 {
15058 offset_type len = VEC_length (offset_type, entry->cu_indices);
15059 offset_type val = MAYBE_SWAP (len);
15060 offset_type iter;
15061 int i;
15062
15063 *slot = entry;
15064 entry->index_offset = obstack_object_size (cpool);
15065
15066 obstack_grow (cpool, &val, sizeof (val));
15067 for (i = 0;
15068 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15069 ++i)
15070 {
15071 val = MAYBE_SWAP (iter);
15072 obstack_grow (cpool, &val, sizeof (val));
15073 }
15074 }
15075 else
15076 {
15077 struct symtab_index_entry *old_entry = *slot;
15078 entry->index_offset = old_entry->index_offset;
15079 entry = old_entry;
15080 }
15081 return entry->index_offset;
15082 }
15083
15084 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15085 constant pool entries going into the obstack CPOOL. */
15086
15087 static void
15088 write_hash_table (struct mapped_symtab *symtab,
15089 struct obstack *output, struct obstack *cpool)
15090 {
15091 offset_type i;
15092 htab_t symbol_hash_table;
15093 htab_t str_table;
15094
15095 symbol_hash_table = create_symbol_hash_table ();
15096 str_table = create_strtab ();
15097
15098 /* We add all the index vectors to the constant pool first, to
15099 ensure alignment is ok. */
15100 for (i = 0; i < symtab->size; ++i)
15101 {
15102 if (symtab->data[i])
15103 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15104 }
15105
15106 /* Now write out the hash table. */
15107 for (i = 0; i < symtab->size; ++i)
15108 {
15109 offset_type str_off, vec_off;
15110
15111 if (symtab->data[i])
15112 {
15113 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15114 vec_off = symtab->data[i]->index_offset;
15115 }
15116 else
15117 {
15118 /* While 0 is a valid constant pool index, it is not valid
15119 to have 0 for both offsets. */
15120 str_off = 0;
15121 vec_off = 0;
15122 }
15123
15124 str_off = MAYBE_SWAP (str_off);
15125 vec_off = MAYBE_SWAP (vec_off);
15126
15127 obstack_grow (output, &str_off, sizeof (str_off));
15128 obstack_grow (output, &vec_off, sizeof (vec_off));
15129 }
15130
15131 htab_delete (str_table);
15132 htab_delete (symbol_hash_table);
15133 }
15134
15135 /* Write an address entry to ADDR_OBSTACK. The addresses are taken
15136 from PST; CU_INDEX is the index of the CU in the vector of all
15137 CUs. */
15138
15139 static void
15140 add_address_entry (struct objfile *objfile,
15141 struct obstack *addr_obstack, struct partial_symtab *pst,
15142 unsigned int cu_index)
15143 {
15144 offset_type offset;
15145 char addr[8];
15146 CORE_ADDR baseaddr;
15147
15148 /* Don't bother recording empty ranges. */
15149 if (pst->textlow == pst->texthigh)
15150 return;
15151
15152 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15153
15154 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
15155 obstack_grow (addr_obstack, addr, 8);
15156 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
15157 obstack_grow (addr_obstack, addr, 8);
15158 offset = MAYBE_SWAP (cu_index);
15159 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
15160 }
15161
15162 /* Add a list of partial symbols to SYMTAB. */
15163
15164 static void
15165 write_psymbols (struct mapped_symtab *symtab,
15166 htab_t psyms_seen,
15167 struct partial_symbol **psymp,
15168 int count,
15169 offset_type cu_index,
15170 int is_static)
15171 {
15172 for (; count-- > 0; ++psymp)
15173 {
15174 void **slot, *lookup;
15175
15176 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15177 error (_("Ada is not currently supported by the index"));
15178
15179 /* We only want to add a given psymbol once. However, we also
15180 want to account for whether it is global or static. So, we
15181 may add it twice, using slightly different values. */
15182 if (is_static)
15183 {
15184 uintptr_t val = 1 | (uintptr_t) *psymp;
15185
15186 lookup = (void *) val;
15187 }
15188 else
15189 lookup = *psymp;
15190
15191 /* Only add a given psymbol once. */
15192 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15193 if (!*slot)
15194 {
15195 *slot = lookup;
15196 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15197 }
15198 }
15199 }
15200
15201 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
15202 exception if there is an error. */
15203
15204 static void
15205 write_obstack (FILE *file, struct obstack *obstack)
15206 {
15207 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15208 file)
15209 != obstack_object_size (obstack))
15210 error (_("couldn't data write to file"));
15211 }
15212
15213 /* Unlink a file if the argument is not NULL. */
15214
15215 static void
15216 unlink_if_set (void *p)
15217 {
15218 char **filename = p;
15219 if (*filename)
15220 unlink (*filename);
15221 }
15222
15223 /* A helper struct used when iterating over debug_types. */
15224 struct signatured_type_index_data
15225 {
15226 struct objfile *objfile;
15227 struct mapped_symtab *symtab;
15228 struct obstack *types_list;
15229 htab_t psyms_seen;
15230 int cu_index;
15231 };
15232
15233 /* A helper function that writes a single signatured_type to an
15234 obstack. */
15235
15236 static int
15237 write_one_signatured_type (void **slot, void *d)
15238 {
15239 struct signatured_type_index_data *info = d;
15240 struct signatured_type *entry = (struct signatured_type *) *slot;
15241 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15242 struct partial_symtab *psymtab = per_cu->v.psymtab;
15243 gdb_byte val[8];
15244
15245 write_psymbols (info->symtab,
15246 info->psyms_seen,
15247 info->objfile->global_psymbols.list + psymtab->globals_offset,
15248 psymtab->n_global_syms, info->cu_index,
15249 0);
15250 write_psymbols (info->symtab,
15251 info->psyms_seen,
15252 info->objfile->static_psymbols.list + psymtab->statics_offset,
15253 psymtab->n_static_syms, info->cu_index,
15254 1);
15255
15256 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15257 obstack_grow (info->types_list, val, 8);
15258 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15259 obstack_grow (info->types_list, val, 8);
15260 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15261 obstack_grow (info->types_list, val, 8);
15262
15263 ++info->cu_index;
15264
15265 return 1;
15266 }
15267
15268 /* A cleanup function for an htab_t. */
15269
15270 static void
15271 cleanup_htab (void *arg)
15272 {
15273 htab_delete (arg);
15274 }
15275
15276 /* Create an index file for OBJFILE in the directory DIR. */
15277
15278 static void
15279 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15280 {
15281 struct cleanup *cleanup;
15282 char *filename, *cleanup_filename;
15283 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15284 struct obstack cu_list, types_cu_list;
15285 int i;
15286 FILE *out_file;
15287 struct mapped_symtab *symtab;
15288 offset_type val, size_of_contents, total_len;
15289 struct stat st;
15290 char buf[8];
15291 htab_t psyms_seen;
15292
15293 if (!objfile->psymtabs)
15294 return;
15295 if (dwarf2_per_objfile->using_index)
15296 error (_("Cannot use an index to create the index"));
15297
15298 if (stat (objfile->name, &st) < 0)
15299 perror_with_name (_("Could not stat"));
15300
15301 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15302 INDEX_SUFFIX, (char *) NULL);
15303 cleanup = make_cleanup (xfree, filename);
15304
15305 out_file = fopen (filename, "wb");
15306 if (!out_file)
15307 error (_("Can't open `%s' for writing"), filename);
15308
15309 cleanup_filename = filename;
15310 make_cleanup (unlink_if_set, &cleanup_filename);
15311
15312 symtab = create_mapped_symtab ();
15313 make_cleanup (cleanup_mapped_symtab, symtab);
15314
15315 obstack_init (&addr_obstack);
15316 make_cleanup_obstack_free (&addr_obstack);
15317
15318 obstack_init (&cu_list);
15319 make_cleanup_obstack_free (&cu_list);
15320
15321 obstack_init (&types_cu_list);
15322 make_cleanup_obstack_free (&types_cu_list);
15323
15324 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15325 NULL, xcalloc, xfree);
15326 make_cleanup (cleanup_htab, psyms_seen);
15327
15328 /* The list is already sorted, so we don't need to do additional
15329 work here. Also, the debug_types entries do not appear in
15330 all_comp_units, but only in their own hash table. */
15331 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15332 {
15333 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15334 struct partial_symtab *psymtab = per_cu->v.psymtab;
15335 gdb_byte val[8];
15336
15337 write_psymbols (symtab,
15338 psyms_seen,
15339 objfile->global_psymbols.list + psymtab->globals_offset,
15340 psymtab->n_global_syms, i,
15341 0);
15342 write_psymbols (symtab,
15343 psyms_seen,
15344 objfile->static_psymbols.list + psymtab->statics_offset,
15345 psymtab->n_static_syms, i,
15346 1);
15347
15348 add_address_entry (objfile, &addr_obstack, psymtab, i);
15349
15350 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15351 obstack_grow (&cu_list, val, 8);
15352 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15353 obstack_grow (&cu_list, val, 8);
15354 }
15355
15356 /* Write out the .debug_type entries, if any. */
15357 if (dwarf2_per_objfile->signatured_types)
15358 {
15359 struct signatured_type_index_data sig_data;
15360
15361 sig_data.objfile = objfile;
15362 sig_data.symtab = symtab;
15363 sig_data.types_list = &types_cu_list;
15364 sig_data.psyms_seen = psyms_seen;
15365 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15366 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15367 write_one_signatured_type, &sig_data);
15368 }
15369
15370 obstack_init (&constant_pool);
15371 make_cleanup_obstack_free (&constant_pool);
15372 obstack_init (&symtab_obstack);
15373 make_cleanup_obstack_free (&symtab_obstack);
15374 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15375
15376 obstack_init (&contents);
15377 make_cleanup_obstack_free (&contents);
15378 size_of_contents = 6 * sizeof (offset_type);
15379 total_len = size_of_contents;
15380
15381 /* The version number. */
15382 val = MAYBE_SWAP (3);
15383 obstack_grow (&contents, &val, sizeof (val));
15384
15385 /* The offset of the CU list from the start of the file. */
15386 val = MAYBE_SWAP (total_len);
15387 obstack_grow (&contents, &val, sizeof (val));
15388 total_len += obstack_object_size (&cu_list);
15389
15390 /* The offset of the types CU list from the start of the file. */
15391 val = MAYBE_SWAP (total_len);
15392 obstack_grow (&contents, &val, sizeof (val));
15393 total_len += obstack_object_size (&types_cu_list);
15394
15395 /* The offset of the address table from the start of the file. */
15396 val = MAYBE_SWAP (total_len);
15397 obstack_grow (&contents, &val, sizeof (val));
15398 total_len += obstack_object_size (&addr_obstack);
15399
15400 /* The offset of the symbol table from the start of the file. */
15401 val = MAYBE_SWAP (total_len);
15402 obstack_grow (&contents, &val, sizeof (val));
15403 total_len += obstack_object_size (&symtab_obstack);
15404
15405 /* The offset of the constant pool from the start of the file. */
15406 val = MAYBE_SWAP (total_len);
15407 obstack_grow (&contents, &val, sizeof (val));
15408 total_len += obstack_object_size (&constant_pool);
15409
15410 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15411
15412 write_obstack (out_file, &contents);
15413 write_obstack (out_file, &cu_list);
15414 write_obstack (out_file, &types_cu_list);
15415 write_obstack (out_file, &addr_obstack);
15416 write_obstack (out_file, &symtab_obstack);
15417 write_obstack (out_file, &constant_pool);
15418
15419 fclose (out_file);
15420
15421 /* We want to keep the file, so we set cleanup_filename to NULL
15422 here. See unlink_if_set. */
15423 cleanup_filename = NULL;
15424
15425 do_cleanups (cleanup);
15426 }
15427
15428 /* The mapped index file format is designed to be directly mmap()able
15429 on any architecture. In most cases, a datum is represented using a
15430 little-endian 32-bit integer value, called an offset_type. Big
15431 endian machines must byte-swap the values before using them.
15432 Exceptions to this rule are noted. The data is laid out such that
15433 alignment is always respected.
15434
15435 A mapped index consists of several sections.
15436
15437 1. The file header. This is a sequence of values, of offset_type
15438 unless otherwise noted:
15439
15440 [0] The version number, currently 3. Versions 1 and 2 are
15441 obsolete.
15442 [1] The offset, from the start of the file, of the CU list.
15443 [2] The offset, from the start of the file, of the types CU list.
15444 Note that this section can be empty, in which case this offset will
15445 be equal to the next offset.
15446 [3] The offset, from the start of the file, of the address section.
15447 [4] The offset, from the start of the file, of the symbol table.
15448 [5] The offset, from the start of the file, of the constant pool.
15449
15450 2. The CU list. This is a sequence of pairs of 64-bit
15451 little-endian values, sorted by the CU offset. The first element
15452 in each pair is the offset of a CU in the .debug_info section. The
15453 second element in each pair is the length of that CU. References
15454 to a CU elsewhere in the map are done using a CU index, which is
15455 just the 0-based index into this table. Note that if there are
15456 type CUs, then conceptually CUs and type CUs form a single list for
15457 the purposes of CU indices.
15458
15459 3. The types CU list. This is a sequence of triplets of 64-bit
15460 little-endian values. In a triplet, the first value is the CU
15461 offset, the second value is the type offset in the CU, and the
15462 third value is the type signature. The types CU list is not
15463 sorted.
15464
15465 4. The address section. The address section consists of a sequence
15466 of address entries. Each address entry has three elements.
15467 [0] The low address. This is a 64-bit little-endian value.
15468 [1] The high address. This is a 64-bit little-endian value.
15469 Like DW_AT_high_pc, the value is one byte beyond the end.
15470 [2] The CU index. This is an offset_type value.
15471
15472 5. The symbol table. This is a hash table. The size of the hash
15473 table is always a power of 2. The initial hash and the step are
15474 currently defined by the `find_slot' function.
15475
15476 Each slot in the hash table consists of a pair of offset_type
15477 values. The first value is the offset of the symbol's name in the
15478 constant pool. The second value is the offset of the CU vector in
15479 the constant pool.
15480
15481 If both values are 0, then this slot in the hash table is empty.
15482 This is ok because while 0 is a valid constant pool index, it
15483 cannot be a valid index for both a string and a CU vector.
15484
15485 A string in the constant pool is stored as a \0-terminated string,
15486 as you'd expect.
15487
15488 A CU vector in the constant pool is a sequence of offset_type
15489 values. The first value is the number of CU indices in the vector.
15490 Each subsequent value is the index of a CU in the CU list. This
15491 element in the hash table is used to indicate which CUs define the
15492 symbol.
15493
15494 6. The constant pool. This is simply a bunch of bytes. It is
15495 organized so that alignment is correct: CU vectors are stored
15496 first, followed by strings. */
15497
15498 static void
15499 save_gdb_index_command (char *arg, int from_tty)
15500 {
15501 struct objfile *objfile;
15502
15503 if (!arg || !*arg)
15504 error (_("usage: save gdb-index DIRECTORY"));
15505
15506 ALL_OBJFILES (objfile)
15507 {
15508 struct stat st;
15509
15510 /* If the objfile does not correspond to an actual file, skip it. */
15511 if (stat (objfile->name, &st) < 0)
15512 continue;
15513
15514 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15515 if (dwarf2_per_objfile)
15516 {
15517 volatile struct gdb_exception except;
15518
15519 TRY_CATCH (except, RETURN_MASK_ERROR)
15520 {
15521 write_psymtabs_to_index (objfile, arg);
15522 }
15523 if (except.reason < 0)
15524 exception_fprintf (gdb_stderr, except,
15525 _("Error while writing index for `%s': "),
15526 objfile->name);
15527 }
15528 }
15529 }
15530
15531 \f
15532
15533 int dwarf2_always_disassemble;
15534
15535 static void
15536 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15537 struct cmd_list_element *c, const char *value)
15538 {
15539 fprintf_filtered (file, _("\
15540 Whether to always disassemble DWARF expressions is %s.\n"),
15541 value);
15542 }
15543
15544 void _initialize_dwarf2_read (void);
15545
15546 void
15547 _initialize_dwarf2_read (void)
15548 {
15549 struct cmd_list_element *c;
15550
15551 dwarf2_objfile_data_key
15552 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15553
15554 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15555 Set DWARF 2 specific variables.\n\
15556 Configure DWARF 2 variables such as the cache size"),
15557 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15558 0/*allow-unknown*/, &maintenance_set_cmdlist);
15559
15560 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15561 Show DWARF 2 specific variables\n\
15562 Show DWARF 2 variables such as the cache size"),
15563 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15564 0/*allow-unknown*/, &maintenance_show_cmdlist);
15565
15566 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15567 &dwarf2_max_cache_age, _("\
15568 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15569 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15570 A higher limit means that cached compilation units will be stored\n\
15571 in memory longer, and more total memory will be used. Zero disables\n\
15572 caching, which can slow down startup."),
15573 NULL,
15574 show_dwarf2_max_cache_age,
15575 &set_dwarf2_cmdlist,
15576 &show_dwarf2_cmdlist);
15577
15578 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15579 &dwarf2_always_disassemble, _("\
15580 Set whether `info address' always disassembles DWARF expressions."), _("\
15581 Show whether `info address' always disassembles DWARF expressions."), _("\
15582 When enabled, DWARF expressions are always printed in an assembly-like\n\
15583 syntax. When disabled, expressions will be printed in a more\n\
15584 conversational style, when possible."),
15585 NULL,
15586 show_dwarf2_always_disassemble,
15587 &set_dwarf2_cmdlist,
15588 &show_dwarf2_cmdlist);
15589
15590 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15591 Set debugging of the dwarf2 DIE reader."), _("\
15592 Show debugging of the dwarf2 DIE reader."), _("\
15593 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15594 The value is the maximum depth to print."),
15595 NULL,
15596 NULL,
15597 &setdebuglist, &showdebuglist);
15598
15599 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15600 _("\
15601 Save a .gdb-index file.\n\
15602 Usage: save gdb-index DIRECTORY"),
15603 &save_cmdlist);
15604 set_cmd_completer (c, filename_completer);
15605 }
This page took 0.442901 seconds and 4 git commands to generate.