* dwarf2read.c (dw2_do_instantiate_symtab): Insert blank line after
[binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 support.
13
14 This file is part of GDB.
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h" /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82 typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98 typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in. */
119 static int dwarf2_die_debug = 0;
120
121 static int pagesize;
122
123 /* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127 static int processing_has_namespace_info;
128
129 static const struct objfile_data *dwarf2_objfile_data_key;
130
131 struct dwarf2_section_info
132 {
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
137 /* True if we have tried to read this section. */
138 int readin;
139 };
140
141 /* All offsets in the index are of this type. It must be
142 architecture-independent. */
143 typedef uint32_t offset_type;
144
145 DEF_VEC_I (offset_type);
146
147 /* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149 struct mapped_index
150 {
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157 /* The hash table. */
158 const offset_type *index_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type index_table_slots;
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163 };
164
165 struct dwarf2_per_objfile
166 {
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
174 struct dwarf2_section_info types;
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
177 struct dwarf2_section_info gdb_index;
178
179 /* Back link. */
180 struct objfile *objfile;
181
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
198
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
216
217 /* Table mapping type .debug_info DIE offsets to types.
218 This is NULL if not allocated yet.
219 It (currently) makes sense to allocate debug_types_type_hash lazily.
220 To keep things simple we allocate both lazily. */
221 htab_t debug_info_type_hash;
222
223 /* Table mapping type .debug_types DIE offsets to types.
224 This is NULL if not allocated yet. */
225 htab_t debug_types_type_hash;
226 };
227
228 static struct dwarf2_per_objfile *dwarf2_per_objfile;
229
230 /* names of the debugging sections */
231
232 /* Note that if the debugging section has been compressed, it might
233 have a name like .zdebug_info. */
234
235 #define INFO_SECTION "debug_info"
236 #define ABBREV_SECTION "debug_abbrev"
237 #define LINE_SECTION "debug_line"
238 #define LOC_SECTION "debug_loc"
239 #define MACINFO_SECTION "debug_macinfo"
240 #define STR_SECTION "debug_str"
241 #define RANGES_SECTION "debug_ranges"
242 #define TYPES_SECTION "debug_types"
243 #define FRAME_SECTION "debug_frame"
244 #define EH_FRAME_SECTION "eh_frame"
245 #define GDB_INDEX_SECTION "gdb_index"
246
247 /* local data types */
248
249 /* We hold several abbreviation tables in memory at the same time. */
250 #ifndef ABBREV_HASH_SIZE
251 #define ABBREV_HASH_SIZE 121
252 #endif
253
254 /* The data in a compilation unit header, after target2host
255 translation, looks like this. */
256 struct comp_unit_head
257 {
258 unsigned int length;
259 short version;
260 unsigned char addr_size;
261 unsigned char signed_addr_p;
262 unsigned int abbrev_offset;
263
264 /* Size of file offsets; either 4 or 8. */
265 unsigned int offset_size;
266
267 /* Size of the length field; either 4 or 12. */
268 unsigned int initial_length_size;
269
270 /* Offset to the first byte of this compilation unit header in the
271 .debug_info section, for resolving relative reference dies. */
272 unsigned int offset;
273
274 /* Offset to first die in this cu from the start of the cu.
275 This will be the first byte following the compilation unit header. */
276 unsigned int first_die_offset;
277 };
278
279 /* Type used for delaying computation of method physnames.
280 See comments for compute_delayed_physnames. */
281 struct delayed_method_info
282 {
283 /* The type to which the method is attached, i.e., its parent class. */
284 struct type *type;
285
286 /* The index of the method in the type's function fieldlists. */
287 int fnfield_index;
288
289 /* The index of the method in the fieldlist. */
290 int index;
291
292 /* The name of the DIE. */
293 const char *name;
294
295 /* The DIE associated with this method. */
296 struct die_info *die;
297 };
298
299 typedef struct delayed_method_info delayed_method_info;
300 DEF_VEC_O (delayed_method_info);
301
302 /* Internal state when decoding a particular compilation unit. */
303 struct dwarf2_cu
304 {
305 /* The objfile containing this compilation unit. */
306 struct objfile *objfile;
307
308 /* The header of the compilation unit. */
309 struct comp_unit_head header;
310
311 /* Base address of this compilation unit. */
312 CORE_ADDR base_address;
313
314 /* Non-zero if base_address has been set. */
315 int base_known;
316
317 struct function_range *first_fn, *last_fn, *cached_fn;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
323 const char *producer;
324
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
341
342 /* Hash table holding all the loaded partial DIEs. */
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
361 /* A hash table of die offsets for following references. */
362 htab_t die_hash;
363
364 /* Full DIEs if read in. */
365 struct die_info *dies;
366
367 /* A set of pointers to dwarf2_per_cu_data objects for compilation
368 units referenced by this one. Only set during full symbol processing;
369 partial symbol tables do not have dependencies. */
370 htab_t dependencies;
371
372 /* Header data from the line table, during full symbol processing. */
373 struct line_header *line_header;
374
375 /* A list of methods which need to have physnames computed
376 after all type information has been read. */
377 VEC (delayed_method_info) *method_list;
378
379 /* Mark used when releasing cached dies. */
380 unsigned int mark : 1;
381
382 /* This flag will be set if this compilation unit might include
383 inter-compilation-unit references. */
384 unsigned int has_form_ref_addr : 1;
385
386 /* This flag will be set if this compilation unit includes any
387 DW_TAG_namespace DIEs. If we know that there are explicit
388 DIEs for namespaces, we don't need to try to infer them
389 from mangled names. */
390 unsigned int has_namespace_info : 1;
391 };
392
393 /* When using the index (and thus not using psymtabs), each CU has an
394 object of this type. This is used to hold information needed by
395 the various "quick" methods. */
396 struct dwarf2_per_cu_quick_data
397 {
398 /* The line table. This can be NULL if there was no line table. */
399 struct line_header *lines;
400
401 /* The file names from the line table. */
402 const char **file_names;
403 /* The file names from the line table after being run through
404 gdb_realpath. */
405 const char **full_names;
406
407 /* The corresponding symbol table. This is NULL if symbols for this
408 CU have not yet been read. */
409 struct symtab *symtab;
410
411 /* A temporary mark bit used when iterating over all CUs in
412 expand_symtabs_matching. */
413 unsigned int mark : 1;
414
415 /* True if we've tried to read the line table. */
416 unsigned int read_lines : 1;
417 };
418
419 /* Persistent data held for a compilation unit, even when not
420 processing it. We put a pointer to this structure in the
421 read_symtab_private field of the psymtab. If we encounter
422 inter-compilation-unit references, we also maintain a sorted
423 list of all compilation units. */
424
425 struct dwarf2_per_cu_data
426 {
427 /* The start offset and length of this compilation unit. 2**29-1
428 bytes should suffice to store the length of any compilation unit
429 - if it doesn't, GDB will fall over anyway.
430 NOTE: Unlike comp_unit_head.length, this length includes
431 initial_length_size. */
432 unsigned int offset;
433 unsigned int length : 29;
434
435 /* Flag indicating this compilation unit will be read in before
436 any of the current compilation units are processed. */
437 unsigned int queued : 1;
438
439 /* This flag will be set if we need to load absolutely all DIEs
440 for this compilation unit, instead of just the ones we think
441 are interesting. It gets set if we look for a DIE in the
442 hash table and don't find it. */
443 unsigned int load_all_dies : 1;
444
445 /* Non-zero if this CU is from .debug_types.
446 Otherwise it's from .debug_info. */
447 unsigned int from_debug_types : 1;
448
449 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
450 of the CU cache it gets reset to NULL again. */
451 struct dwarf2_cu *cu;
452
453 /* The corresponding objfile. */
454 struct objfile *objfile;
455
456 /* When using partial symbol tables, the 'psymtab' field is active.
457 Otherwise the 'quick' field is active. */
458 union
459 {
460 /* The partial symbol table associated with this compilation unit,
461 or NULL for partial units (which do not have an associated
462 symtab). */
463 struct partial_symtab *psymtab;
464
465 /* Data needed by the "quick" functions. */
466 struct dwarf2_per_cu_quick_data *quick;
467 } v;
468 };
469
470 /* Entry in the signatured_types hash table. */
471
472 struct signatured_type
473 {
474 ULONGEST signature;
475
476 /* Offset in .debug_types of the TU (type_unit) for this type. */
477 unsigned int offset;
478
479 /* Offset in .debug_types of the type defined by this TU. */
480 unsigned int type_offset;
481
482 /* The CU(/TU) of this type. */
483 struct dwarf2_per_cu_data per_cu;
484 };
485
486 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
487 which are used for both .debug_info and .debug_types dies.
488 All parameters here are unchanging for the life of the call.
489 This struct exists to abstract away the constant parameters of
490 die reading. */
491
492 struct die_reader_specs
493 {
494 /* The bfd of this objfile. */
495 bfd* abfd;
496
497 /* The CU of the DIE we are parsing. */
498 struct dwarf2_cu *cu;
499
500 /* Pointer to start of section buffer.
501 This is either the start of .debug_info or .debug_types. */
502 const gdb_byte *buffer;
503 };
504
505 /* The line number information for a compilation unit (found in the
506 .debug_line section) begins with a "statement program header",
507 which contains the following information. */
508 struct line_header
509 {
510 unsigned int total_length;
511 unsigned short version;
512 unsigned int header_length;
513 unsigned char minimum_instruction_length;
514 unsigned char maximum_ops_per_instruction;
515 unsigned char default_is_stmt;
516 int line_base;
517 unsigned char line_range;
518 unsigned char opcode_base;
519
520 /* standard_opcode_lengths[i] is the number of operands for the
521 standard opcode whose value is i. This means that
522 standard_opcode_lengths[0] is unused, and the last meaningful
523 element is standard_opcode_lengths[opcode_base - 1]. */
524 unsigned char *standard_opcode_lengths;
525
526 /* The include_directories table. NOTE! These strings are not
527 allocated with xmalloc; instead, they are pointers into
528 debug_line_buffer. If you try to free them, `free' will get
529 indigestion. */
530 unsigned int num_include_dirs, include_dirs_size;
531 char **include_dirs;
532
533 /* The file_names table. NOTE! These strings are not allocated
534 with xmalloc; instead, they are pointers into debug_line_buffer.
535 Don't try to free them directly. */
536 unsigned int num_file_names, file_names_size;
537 struct file_entry
538 {
539 char *name;
540 unsigned int dir_index;
541 unsigned int mod_time;
542 unsigned int length;
543 int included_p; /* Non-zero if referenced by the Line Number Program. */
544 struct symtab *symtab; /* The associated symbol table, if any. */
545 } *file_names;
546
547 /* The start and end of the statement program following this
548 header. These point into dwarf2_per_objfile->line_buffer. */
549 gdb_byte *statement_program_start, *statement_program_end;
550 };
551
552 /* When we construct a partial symbol table entry we only
553 need this much information. */
554 struct partial_die_info
555 {
556 /* Offset of this DIE. */
557 unsigned int offset;
558
559 /* DWARF-2 tag for this DIE. */
560 ENUM_BITFIELD(dwarf_tag) tag : 16;
561
562 /* Assorted flags describing the data found in this DIE. */
563 unsigned int has_children : 1;
564 unsigned int is_external : 1;
565 unsigned int is_declaration : 1;
566 unsigned int has_type : 1;
567 unsigned int has_specification : 1;
568 unsigned int has_pc_info : 1;
569
570 /* Flag set if the SCOPE field of this structure has been
571 computed. */
572 unsigned int scope_set : 1;
573
574 /* Flag set if the DIE has a byte_size attribute. */
575 unsigned int has_byte_size : 1;
576
577 /* Flag set if any of the DIE's children are template arguments. */
578 unsigned int has_template_arguments : 1;
579
580 /* 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 dwarf2_symbol_mark_computed (struct attribute *attr,
1225 struct symbol *sym,
1226 struct dwarf2_cu *cu);
1227
1228 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1229 struct abbrev_info *abbrev,
1230 struct dwarf2_cu *cu);
1231
1232 static void free_stack_comp_unit (void *);
1233
1234 static hashval_t partial_die_hash (const void *item);
1235
1236 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1237
1238 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1239 (unsigned int offset, struct objfile *objfile);
1240
1241 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1242 (unsigned int offset, struct objfile *objfile);
1243
1244 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1245
1246 static void free_one_comp_unit (void *);
1247
1248 static void free_cached_comp_units (void *);
1249
1250 static void age_cached_comp_units (void);
1251
1252 static void free_one_cached_comp_unit (void *);
1253
1254 static struct type *set_die_type (struct die_info *, struct type *,
1255 struct dwarf2_cu *);
1256
1257 static void create_all_comp_units (struct objfile *);
1258
1259 static int create_debug_types_hash_table (struct objfile *objfile);
1260
1261 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1262 struct objfile *);
1263
1264 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1265
1266 static void dwarf2_add_dependence (struct dwarf2_cu *,
1267 struct dwarf2_per_cu_data *);
1268
1269 static void dwarf2_mark (struct dwarf2_cu *);
1270
1271 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1272
1273 static struct type *get_die_type_at_offset (unsigned int,
1274 struct dwarf2_per_cu_data *per_cu);
1275
1276 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1277
1278 static void dwarf2_release_queue (void *dummy);
1279
1280 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1281 struct objfile *objfile);
1282
1283 static void process_queue (struct objfile *objfile);
1284
1285 static void find_file_and_directory (struct die_info *die,
1286 struct dwarf2_cu *cu,
1287 char **name, char **comp_dir);
1288
1289 static char *file_full_name (int file, struct line_header *lh,
1290 const char *comp_dir);
1291
1292 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1293 gdb_byte *info_ptr,
1294 gdb_byte *buffer,
1295 unsigned int buffer_size,
1296 bfd *abfd);
1297
1298 static void init_cu_die_reader (struct die_reader_specs *reader,
1299 struct dwarf2_cu *cu);
1300
1301 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1302
1303 #if WORDS_BIGENDIAN
1304
1305 /* Convert VALUE between big- and little-endian. */
1306 static offset_type
1307 byte_swap (offset_type value)
1308 {
1309 offset_type result;
1310
1311 result = (value & 0xff) << 24;
1312 result |= (value & 0xff00) << 8;
1313 result |= (value & 0xff0000) >> 8;
1314 result |= (value & 0xff000000) >> 24;
1315 return result;
1316 }
1317
1318 #define MAYBE_SWAP(V) byte_swap (V)
1319
1320 #else
1321 #define MAYBE_SWAP(V) (V)
1322 #endif /* WORDS_BIGENDIAN */
1323
1324 /* The suffix for an index file. */
1325 #define INDEX_SUFFIX ".gdb-index"
1326
1327 static const char *dwarf2_physname (char *name, struct die_info *die,
1328 struct dwarf2_cu *cu);
1329
1330 /* Try to locate the sections we need for DWARF 2 debugging
1331 information and return true if we have enough to do something. */
1332
1333 int
1334 dwarf2_has_info (struct objfile *objfile)
1335 {
1336 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1337 if (!dwarf2_per_objfile)
1338 {
1339 /* Initialize per-objfile state. */
1340 struct dwarf2_per_objfile *data
1341 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1342
1343 memset (data, 0, sizeof (*data));
1344 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1345 dwarf2_per_objfile = data;
1346
1347 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1348 dwarf2_per_objfile->objfile = objfile;
1349 }
1350 return (dwarf2_per_objfile->info.asection != NULL
1351 && dwarf2_per_objfile->abbrev.asection != NULL);
1352 }
1353
1354 /* When loading sections, we can either look for ".<name>", or for
1355 * ".z<name>", which indicates a compressed section. */
1356
1357 static int
1358 section_is_p (const char *section_name, const char *name)
1359 {
1360 return (section_name[0] == '.'
1361 && (strcmp (section_name + 1, name) == 0
1362 || (section_name[1] == 'z'
1363 && strcmp (section_name + 2, name) == 0)));
1364 }
1365
1366 /* This function is mapped across the sections and remembers the
1367 offset and size of each of the debugging sections we are interested
1368 in. */
1369
1370 static void
1371 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1372 {
1373 if (section_is_p (sectp->name, INFO_SECTION))
1374 {
1375 dwarf2_per_objfile->info.asection = sectp;
1376 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1377 }
1378 else if (section_is_p (sectp->name, ABBREV_SECTION))
1379 {
1380 dwarf2_per_objfile->abbrev.asection = sectp;
1381 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1382 }
1383 else if (section_is_p (sectp->name, LINE_SECTION))
1384 {
1385 dwarf2_per_objfile->line.asection = sectp;
1386 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1387 }
1388 else if (section_is_p (sectp->name, LOC_SECTION))
1389 {
1390 dwarf2_per_objfile->loc.asection = sectp;
1391 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1392 }
1393 else if (section_is_p (sectp->name, MACINFO_SECTION))
1394 {
1395 dwarf2_per_objfile->macinfo.asection = sectp;
1396 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1397 }
1398 else if (section_is_p (sectp->name, STR_SECTION))
1399 {
1400 dwarf2_per_objfile->str.asection = sectp;
1401 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1402 }
1403 else if (section_is_p (sectp->name, FRAME_SECTION))
1404 {
1405 dwarf2_per_objfile->frame.asection = sectp;
1406 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1407 }
1408 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1409 {
1410 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1411
1412 if (aflag & SEC_HAS_CONTENTS)
1413 {
1414 dwarf2_per_objfile->eh_frame.asection = sectp;
1415 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1416 }
1417 }
1418 else if (section_is_p (sectp->name, RANGES_SECTION))
1419 {
1420 dwarf2_per_objfile->ranges.asection = sectp;
1421 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1422 }
1423 else if (section_is_p (sectp->name, TYPES_SECTION))
1424 {
1425 dwarf2_per_objfile->types.asection = sectp;
1426 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1427 }
1428 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1429 {
1430 dwarf2_per_objfile->gdb_index.asection = sectp;
1431 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1432 }
1433
1434 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1435 && bfd_section_vma (abfd, sectp) == 0)
1436 dwarf2_per_objfile->has_section_at_zero = 1;
1437 }
1438
1439 /* Decompress a section that was compressed using zlib. Store the
1440 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1441
1442 static void
1443 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1444 gdb_byte **outbuf, bfd_size_type *outsize)
1445 {
1446 bfd *abfd = objfile->obfd;
1447 #ifndef HAVE_ZLIB_H
1448 error (_("Support for zlib-compressed DWARF data (from '%s') "
1449 "is disabled in this copy of GDB"),
1450 bfd_get_filename (abfd));
1451 #else
1452 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1453 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1454 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1455 bfd_size_type uncompressed_size;
1456 gdb_byte *uncompressed_buffer;
1457 z_stream strm;
1458 int rc;
1459 int header_size = 12;
1460
1461 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1462 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1463 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1464 bfd_get_filename (abfd));
1465
1466 /* Read the zlib header. In this case, it should be "ZLIB" followed
1467 by the uncompressed section size, 8 bytes in big-endian order. */
1468 if (compressed_size < header_size
1469 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1470 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1471 bfd_get_filename (abfd));
1472 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1473 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1474 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1475 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1476 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1477 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1478 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1479 uncompressed_size += compressed_buffer[11];
1480
1481 /* It is possible the section consists of several compressed
1482 buffers concatenated together, so we uncompress in a loop. */
1483 strm.zalloc = NULL;
1484 strm.zfree = NULL;
1485 strm.opaque = NULL;
1486 strm.avail_in = compressed_size - header_size;
1487 strm.next_in = (Bytef*) compressed_buffer + header_size;
1488 strm.avail_out = uncompressed_size;
1489 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1490 uncompressed_size);
1491 rc = inflateInit (&strm);
1492 while (strm.avail_in > 0)
1493 {
1494 if (rc != Z_OK)
1495 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1496 bfd_get_filename (abfd), rc);
1497 strm.next_out = ((Bytef*) uncompressed_buffer
1498 + (uncompressed_size - strm.avail_out));
1499 rc = inflate (&strm, Z_FINISH);
1500 if (rc != Z_STREAM_END)
1501 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1502 bfd_get_filename (abfd), rc);
1503 rc = inflateReset (&strm);
1504 }
1505 rc = inflateEnd (&strm);
1506 if (rc != Z_OK
1507 || strm.avail_out != 0)
1508 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1509 bfd_get_filename (abfd), rc);
1510
1511 do_cleanups (cleanup);
1512 *outbuf = uncompressed_buffer;
1513 *outsize = uncompressed_size;
1514 #endif
1515 }
1516
1517 /* Read the contents of the section SECTP from object file specified by
1518 OBJFILE, store info about the section into INFO.
1519 If the section is compressed, uncompress it before returning. */
1520
1521 static void
1522 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1523 {
1524 bfd *abfd = objfile->obfd;
1525 asection *sectp = info->asection;
1526 gdb_byte *buf, *retbuf;
1527 unsigned char header[4];
1528
1529 if (info->readin)
1530 return;
1531 info->buffer = NULL;
1532 info->was_mmapped = 0;
1533 info->readin = 1;
1534
1535 if (info->asection == NULL || info->size == 0)
1536 return;
1537
1538 /* Check if the file has a 4-byte header indicating compression. */
1539 if (info->size > sizeof (header)
1540 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1541 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1542 {
1543 /* Upon decompression, update the buffer and its size. */
1544 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1545 {
1546 zlib_decompress_section (objfile, sectp, &info->buffer,
1547 &info->size);
1548 return;
1549 }
1550 }
1551
1552 #ifdef HAVE_MMAP
1553 if (pagesize == 0)
1554 pagesize = getpagesize ();
1555
1556 /* Only try to mmap sections which are large enough: we don't want to
1557 waste space due to fragmentation. Also, only try mmap for sections
1558 without relocations. */
1559
1560 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1561 {
1562 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1563 size_t map_length = info->size + sectp->filepos - pg_offset;
1564 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1565 MAP_PRIVATE, pg_offset);
1566
1567 if (retbuf != MAP_FAILED)
1568 {
1569 info->was_mmapped = 1;
1570 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1571 #if HAVE_POSIX_MADVISE
1572 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1573 #endif
1574 return;
1575 }
1576 }
1577 #endif
1578
1579 /* If we get here, we are a normal, not-compressed section. */
1580 info->buffer = buf
1581 = obstack_alloc (&objfile->objfile_obstack, info->size);
1582
1583 /* When debugging .o files, we may need to apply relocations; see
1584 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1585 We never compress sections in .o files, so we only need to
1586 try this when the section is not compressed. */
1587 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1588 if (retbuf != NULL)
1589 {
1590 info->buffer = retbuf;
1591 return;
1592 }
1593
1594 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1595 || bfd_bread (buf, info->size, abfd) != info->size)
1596 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1597 bfd_get_filename (abfd));
1598 }
1599
1600 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1601 SECTION_NAME. */
1602
1603 void
1604 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1605 asection **sectp, gdb_byte **bufp,
1606 bfd_size_type *sizep)
1607 {
1608 struct dwarf2_per_objfile *data
1609 = objfile_data (objfile, dwarf2_objfile_data_key);
1610 struct dwarf2_section_info *info;
1611
1612 /* We may see an objfile without any DWARF, in which case we just
1613 return nothing. */
1614 if (data == NULL)
1615 {
1616 *sectp = NULL;
1617 *bufp = NULL;
1618 *sizep = 0;
1619 return;
1620 }
1621 if (section_is_p (section_name, EH_FRAME_SECTION))
1622 info = &data->eh_frame;
1623 else if (section_is_p (section_name, FRAME_SECTION))
1624 info = &data->frame;
1625 else
1626 gdb_assert_not_reached ("unexpected section");
1627
1628 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1629 /* We haven't read this section in yet. Do it now. */
1630 dwarf2_read_section (objfile, info);
1631
1632 *sectp = info->asection;
1633 *bufp = info->buffer;
1634 *sizep = info->size;
1635 }
1636
1637 \f
1638
1639 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1640 this CU came. */
1641
1642 static void
1643 dw2_do_instantiate_symtab (struct objfile *objfile,
1644 struct dwarf2_per_cu_data *per_cu)
1645 {
1646 struct cleanup *back_to;
1647
1648 back_to = make_cleanup (dwarf2_release_queue, NULL);
1649
1650 queue_comp_unit (per_cu, objfile);
1651
1652 if (per_cu->from_debug_types)
1653 read_signatured_type_at_offset (objfile, per_cu->offset);
1654 else
1655 load_full_comp_unit (per_cu, objfile);
1656
1657 process_queue (objfile);
1658
1659 /* Age the cache, releasing compilation units that have not
1660 been used recently. */
1661 age_cached_comp_units ();
1662
1663 do_cleanups (back_to);
1664 }
1665
1666 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1667 the objfile from which this CU came. Returns the resulting symbol
1668 table. */
1669
1670 static struct symtab *
1671 dw2_instantiate_symtab (struct objfile *objfile,
1672 struct dwarf2_per_cu_data *per_cu)
1673 {
1674 if (!per_cu->v.quick->symtab)
1675 {
1676 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1677 increment_reading_symtab ();
1678 dw2_do_instantiate_symtab (objfile, per_cu);
1679 do_cleanups (back_to);
1680 }
1681 return per_cu->v.quick->symtab;
1682 }
1683
1684 /* Return the CU given its index. */
1685
1686 static struct dwarf2_per_cu_data *
1687 dw2_get_cu (int index)
1688 {
1689 if (index >= dwarf2_per_objfile->n_comp_units)
1690 {
1691 index -= dwarf2_per_objfile->n_comp_units;
1692 return dwarf2_per_objfile->type_comp_units[index];
1693 }
1694 return dwarf2_per_objfile->all_comp_units[index];
1695 }
1696
1697 /* A helper function that knows how to read a 64-bit value in a way
1698 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1699 otherwise. */
1700
1701 static int
1702 extract_cu_value (const char *bytes, ULONGEST *result)
1703 {
1704 if (sizeof (ULONGEST) < 8)
1705 {
1706 int i;
1707
1708 /* Ignore the upper 4 bytes if they are all zero. */
1709 for (i = 0; i < 4; ++i)
1710 if (bytes[i + 4] != 0)
1711 return 0;
1712
1713 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1714 }
1715 else
1716 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1717 return 1;
1718 }
1719
1720 /* Read the CU list from the mapped index, and use it to create all
1721 the CU objects for this objfile. Return 0 if something went wrong,
1722 1 if everything went ok. */
1723
1724 static int
1725 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1726 offset_type cu_list_elements)
1727 {
1728 offset_type i;
1729
1730 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1731 dwarf2_per_objfile->all_comp_units
1732 = obstack_alloc (&objfile->objfile_obstack,
1733 dwarf2_per_objfile->n_comp_units
1734 * sizeof (struct dwarf2_per_cu_data *));
1735
1736 for (i = 0; i < cu_list_elements; i += 2)
1737 {
1738 struct dwarf2_per_cu_data *the_cu;
1739 ULONGEST offset, length;
1740
1741 if (!extract_cu_value (cu_list, &offset)
1742 || !extract_cu_value (cu_list + 8, &length))
1743 return 0;
1744 cu_list += 2 * 8;
1745
1746 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1747 struct dwarf2_per_cu_data);
1748 the_cu->offset = offset;
1749 the_cu->length = length;
1750 the_cu->objfile = objfile;
1751 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1752 struct dwarf2_per_cu_quick_data);
1753 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1754 }
1755
1756 return 1;
1757 }
1758
1759 /* Create the signatured type hash table from the index. */
1760
1761 static int
1762 create_signatured_type_table_from_index (struct objfile *objfile,
1763 const gdb_byte *bytes,
1764 offset_type elements)
1765 {
1766 offset_type i;
1767 htab_t sig_types_hash;
1768
1769 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1770 dwarf2_per_objfile->type_comp_units
1771 = obstack_alloc (&objfile->objfile_obstack,
1772 dwarf2_per_objfile->n_type_comp_units
1773 * sizeof (struct dwarf2_per_cu_data *));
1774
1775 sig_types_hash = allocate_signatured_type_table (objfile);
1776
1777 for (i = 0; i < elements; i += 3)
1778 {
1779 struct signatured_type *type_sig;
1780 ULONGEST offset, type_offset, signature;
1781 void **slot;
1782
1783 if (!extract_cu_value (bytes, &offset)
1784 || !extract_cu_value (bytes + 8, &type_offset))
1785 return 0;
1786 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1787 bytes += 3 * 8;
1788
1789 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1790 struct signatured_type);
1791 type_sig->signature = signature;
1792 type_sig->offset = offset;
1793 type_sig->type_offset = type_offset;
1794 type_sig->per_cu.from_debug_types = 1;
1795 type_sig->per_cu.offset = offset;
1796 type_sig->per_cu.objfile = objfile;
1797 type_sig->per_cu.v.quick
1798 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1799 struct dwarf2_per_cu_quick_data);
1800
1801 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1802 *slot = type_sig;
1803
1804 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1805 }
1806
1807 dwarf2_per_objfile->signatured_types = sig_types_hash;
1808
1809 return 1;
1810 }
1811
1812 /* Read the address map data from the mapped index, and use it to
1813 populate the objfile's psymtabs_addrmap. */
1814
1815 static void
1816 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1817 {
1818 const gdb_byte *iter, *end;
1819 struct obstack temp_obstack;
1820 struct addrmap *mutable_map;
1821 struct cleanup *cleanup;
1822 CORE_ADDR baseaddr;
1823
1824 obstack_init (&temp_obstack);
1825 cleanup = make_cleanup_obstack_free (&temp_obstack);
1826 mutable_map = addrmap_create_mutable (&temp_obstack);
1827
1828 iter = index->address_table;
1829 end = iter + index->address_table_size;
1830
1831 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1832
1833 while (iter < end)
1834 {
1835 ULONGEST hi, lo, cu_index;
1836 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1837 iter += 8;
1838 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1839 iter += 8;
1840 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1841 iter += 4;
1842
1843 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1844 dw2_get_cu (cu_index));
1845 }
1846
1847 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1848 &objfile->objfile_obstack);
1849 do_cleanups (cleanup);
1850 }
1851
1852 /* The hash function for strings in the mapped index. This is the
1853 same as the hashtab.c hash function, but we keep a separate copy to
1854 maintain control over the implementation. This is necessary
1855 because the hash function is tied to the format of the mapped index
1856 file. */
1857
1858 static hashval_t
1859 mapped_index_string_hash (const void *p)
1860 {
1861 const unsigned char *str = (const unsigned char *) p;
1862 hashval_t r = 0;
1863 unsigned char c;
1864
1865 while ((c = *str++) != 0)
1866 r = r * 67 + c - 113;
1867
1868 return r;
1869 }
1870
1871 /* Find a slot in the mapped index INDEX for the object named NAME.
1872 If NAME is found, set *VEC_OUT to point to the CU vector in the
1873 constant pool and return 1. If NAME cannot be found, return 0. */
1874
1875 static int
1876 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1877 offset_type **vec_out)
1878 {
1879 offset_type hash = mapped_index_string_hash (name);
1880 offset_type slot, step;
1881
1882 slot = hash & (index->index_table_slots - 1);
1883 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1884
1885 for (;;)
1886 {
1887 /* Convert a slot number to an offset into the table. */
1888 offset_type i = 2 * slot;
1889 const char *str;
1890 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1891 return 0;
1892
1893 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1894 if (!strcmp (name, str))
1895 {
1896 *vec_out = (offset_type *) (index->constant_pool
1897 + MAYBE_SWAP (index->index_table[i + 1]));
1898 return 1;
1899 }
1900
1901 slot = (slot + step) & (index->index_table_slots - 1);
1902 }
1903 }
1904
1905 /* Read the index file. If everything went ok, initialize the "quick"
1906 elements of all the CUs and return 1. Otherwise, return 0. */
1907
1908 static int
1909 dwarf2_read_index (struct objfile *objfile)
1910 {
1911 char *addr;
1912 struct mapped_index *map;
1913 offset_type *metadata;
1914 const gdb_byte *cu_list;
1915 const gdb_byte *types_list = NULL;
1916 offset_type version, cu_list_elements;
1917 offset_type types_list_elements = 0;
1918 int i;
1919
1920 if (dwarf2_per_objfile->gdb_index.asection == NULL
1921 || dwarf2_per_objfile->gdb_index.size == 0)
1922 return 0;
1923
1924 /* Older elfutils strip versions could keep the section in the main
1925 executable while splitting it for the separate debug info file. */
1926 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1927 & SEC_HAS_CONTENTS) == 0)
1928 return 0;
1929
1930 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1931
1932 addr = dwarf2_per_objfile->gdb_index.buffer;
1933 /* Version check. */
1934 version = MAYBE_SWAP (*(offset_type *) addr);
1935 /* Versions earlier than 3 emitted every copy of a psymbol. This
1936 causes the index to behave very poorly for certain requests. So,
1937 it seems better to just ignore such indices. */
1938 if (version < 3)
1939 return 0;
1940
1941 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1942 map->total_size = dwarf2_per_objfile->gdb_index.size;
1943
1944 metadata = (offset_type *) (addr + sizeof (offset_type));
1945
1946 i = 0;
1947 cu_list = addr + MAYBE_SWAP (metadata[i]);
1948 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1949 / 8);
1950 ++i;
1951
1952 types_list = addr + MAYBE_SWAP (metadata[i]);
1953 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1954 - MAYBE_SWAP (metadata[i]))
1955 / 8);
1956 ++i;
1957
1958 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1959 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1960 - MAYBE_SWAP (metadata[i]));
1961 ++i;
1962
1963 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1964 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1965 - MAYBE_SWAP (metadata[i]))
1966 / (2 * sizeof (offset_type)));
1967 ++i;
1968
1969 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1970
1971 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1972 return 0;
1973
1974 if (types_list_elements
1975 && !create_signatured_type_table_from_index (objfile, types_list,
1976 types_list_elements))
1977 return 0;
1978
1979 create_addrmap_from_index (objfile, map);
1980
1981 dwarf2_per_objfile->index_table = map;
1982 dwarf2_per_objfile->using_index = 1;
1983
1984 return 1;
1985 }
1986
1987 /* A helper for the "quick" functions which sets the global
1988 dwarf2_per_objfile according to OBJFILE. */
1989
1990 static void
1991 dw2_setup (struct objfile *objfile)
1992 {
1993 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1994 gdb_assert (dwarf2_per_objfile);
1995 }
1996
1997 /* A helper for the "quick" functions which attempts to read the line
1998 table for THIS_CU. */
1999
2000 static void
2001 dw2_require_line_header (struct objfile *objfile,
2002 struct dwarf2_per_cu_data *this_cu)
2003 {
2004 bfd *abfd = objfile->obfd;
2005 struct line_header *lh = NULL;
2006 struct attribute *attr;
2007 struct cleanup *cleanups;
2008 struct die_info *comp_unit_die;
2009 struct dwarf2_section_info* sec;
2010 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2011 int has_children, i;
2012 struct dwarf2_cu cu;
2013 unsigned int bytes_read, buffer_size;
2014 struct die_reader_specs reader_specs;
2015 char *name, *comp_dir;
2016
2017 if (this_cu->v.quick->read_lines)
2018 return;
2019 this_cu->v.quick->read_lines = 1;
2020
2021 memset (&cu, 0, sizeof (cu));
2022 cu.objfile = objfile;
2023 obstack_init (&cu.comp_unit_obstack);
2024
2025 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2026
2027 if (this_cu->from_debug_types)
2028 sec = &dwarf2_per_objfile->types;
2029 else
2030 sec = &dwarf2_per_objfile->info;
2031 dwarf2_read_section (objfile, sec);
2032 buffer_size = sec->size;
2033 buffer = sec->buffer;
2034 info_ptr = buffer + this_cu->offset;
2035 beg_of_comp_unit = info_ptr;
2036
2037 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2038 buffer, buffer_size,
2039 abfd);
2040
2041 /* Complete the cu_header. */
2042 cu.header.offset = beg_of_comp_unit - buffer;
2043 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2044
2045 this_cu->cu = &cu;
2046 cu.per_cu = this_cu;
2047
2048 dwarf2_read_abbrevs (abfd, &cu);
2049 make_cleanup (dwarf2_free_abbrev_table, &cu);
2050
2051 if (this_cu->from_debug_types)
2052 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2053 init_cu_die_reader (&reader_specs, &cu);
2054 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2055 &has_children);
2056
2057 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2058 if (attr)
2059 {
2060 unsigned int line_offset = DW_UNSND (attr);
2061 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2062 }
2063 if (lh == NULL)
2064 {
2065 do_cleanups (cleanups);
2066 return;
2067 }
2068
2069 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2070
2071 this_cu->v.quick->lines = lh;
2072
2073 this_cu->v.quick->file_names
2074 = obstack_alloc (&objfile->objfile_obstack,
2075 lh->num_file_names * sizeof (char *));
2076 for (i = 0; i < lh->num_file_names; ++i)
2077 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2078
2079 do_cleanups (cleanups);
2080 }
2081
2082 /* A helper for the "quick" functions which computes and caches the
2083 real path for a given file name from the line table.
2084 dw2_require_line_header must have been called before this is
2085 invoked. */
2086
2087 static const char *
2088 dw2_require_full_path (struct objfile *objfile,
2089 struct dwarf2_per_cu_data *per_cu,
2090 int index)
2091 {
2092 if (!per_cu->v.quick->full_names)
2093 per_cu->v.quick->full_names
2094 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2095 per_cu->v.quick->lines->num_file_names,
2096 sizeof (char *));
2097
2098 if (!per_cu->v.quick->full_names[index])
2099 per_cu->v.quick->full_names[index]
2100 = gdb_realpath (per_cu->v.quick->file_names[index]);
2101
2102 return per_cu->v.quick->full_names[index];
2103 }
2104
2105 static struct symtab *
2106 dw2_find_last_source_symtab (struct objfile *objfile)
2107 {
2108 int index;
2109 dw2_setup (objfile);
2110 index = dwarf2_per_objfile->n_comp_units - 1;
2111 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2112 }
2113
2114 static void
2115 dw2_forget_cached_source_info (struct objfile *objfile)
2116 {
2117 int i;
2118
2119 dw2_setup (objfile);
2120 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2121 + dwarf2_per_objfile->n_type_comp_units); ++i)
2122 {
2123 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2124
2125 if (per_cu->v.quick->full_names)
2126 {
2127 int j;
2128
2129 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2130 xfree ((void *) per_cu->v.quick->full_names[j]);
2131 }
2132 }
2133 }
2134
2135 static int
2136 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2137 const char *full_path, const char *real_path,
2138 struct symtab **result)
2139 {
2140 int i;
2141 int check_basename = lbasename (name) == name;
2142 struct dwarf2_per_cu_data *base_cu = NULL;
2143
2144 dw2_setup (objfile);
2145 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2146 + dwarf2_per_objfile->n_type_comp_units); ++i)
2147 {
2148 int j;
2149 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2150
2151 if (per_cu->v.quick->symtab)
2152 continue;
2153
2154 dw2_require_line_header (objfile, per_cu);
2155 if (!per_cu->v.quick->lines)
2156 continue;
2157
2158 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2159 {
2160 const char *this_name = per_cu->v.quick->file_names[j];
2161
2162 if (FILENAME_CMP (name, this_name) == 0)
2163 {
2164 *result = dw2_instantiate_symtab (objfile, per_cu);
2165 return 1;
2166 }
2167
2168 if (check_basename && ! base_cu
2169 && FILENAME_CMP (lbasename (this_name), name) == 0)
2170 base_cu = per_cu;
2171
2172 if (full_path != NULL)
2173 {
2174 const char *this_full_name = dw2_require_full_path (objfile,
2175 per_cu, j);
2176
2177 if (this_full_name
2178 && FILENAME_CMP (full_path, this_full_name) == 0)
2179 {
2180 *result = dw2_instantiate_symtab (objfile, per_cu);
2181 return 1;
2182 }
2183 }
2184
2185 if (real_path != NULL)
2186 {
2187 const char *this_full_name = dw2_require_full_path (objfile,
2188 per_cu, j);
2189
2190 if (this_full_name != NULL)
2191 {
2192 char *rp = gdb_realpath (this_full_name);
2193 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2194 {
2195 xfree (rp);
2196 *result = dw2_instantiate_symtab (objfile, per_cu);
2197 return 1;
2198 }
2199 xfree (rp);
2200 }
2201 }
2202 }
2203 }
2204
2205 if (base_cu)
2206 {
2207 *result = dw2_instantiate_symtab (objfile, base_cu);
2208 return 1;
2209 }
2210
2211 return 0;
2212 }
2213
2214 static struct symtab *
2215 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2216 const char *name, domain_enum domain)
2217 {
2218 /* We do all the work in the pre_expand_symtabs_matching hook
2219 instead. */
2220 return NULL;
2221 }
2222
2223 /* A helper function that expands all symtabs that hold an object
2224 named NAME. */
2225
2226 static void
2227 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2228 {
2229 dw2_setup (objfile);
2230
2231 if (dwarf2_per_objfile->index_table)
2232 {
2233 offset_type *vec;
2234
2235 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2236 name, &vec))
2237 {
2238 offset_type i, len = MAYBE_SWAP (*vec);
2239 for (i = 0; i < len; ++i)
2240 {
2241 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2242 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2243
2244 dw2_instantiate_symtab (objfile, per_cu);
2245 }
2246 }
2247 }
2248 }
2249
2250 static void
2251 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2252 int kind, const char *name,
2253 domain_enum domain)
2254 {
2255 dw2_do_expand_symtabs_matching (objfile, name);
2256 }
2257
2258 static void
2259 dw2_print_stats (struct objfile *objfile)
2260 {
2261 int i, count;
2262
2263 dw2_setup (objfile);
2264 count = 0;
2265 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2266 + dwarf2_per_objfile->n_type_comp_units); ++i)
2267 {
2268 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2269
2270 if (!per_cu->v.quick->symtab)
2271 ++count;
2272 }
2273 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2274 }
2275
2276 static void
2277 dw2_dump (struct objfile *objfile)
2278 {
2279 /* Nothing worth printing. */
2280 }
2281
2282 static void
2283 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2284 struct section_offsets *delta)
2285 {
2286 /* There's nothing to relocate here. */
2287 }
2288
2289 static void
2290 dw2_expand_symtabs_for_function (struct objfile *objfile,
2291 const char *func_name)
2292 {
2293 dw2_do_expand_symtabs_matching (objfile, func_name);
2294 }
2295
2296 static void
2297 dw2_expand_all_symtabs (struct objfile *objfile)
2298 {
2299 int i;
2300
2301 dw2_setup (objfile);
2302
2303 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2304 + dwarf2_per_objfile->n_type_comp_units); ++i)
2305 {
2306 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2307
2308 dw2_instantiate_symtab (objfile, per_cu);
2309 }
2310 }
2311
2312 static void
2313 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2314 const char *filename)
2315 {
2316 int i;
2317
2318 dw2_setup (objfile);
2319 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2320 + dwarf2_per_objfile->n_type_comp_units); ++i)
2321 {
2322 int j;
2323 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2324
2325 if (per_cu->v.quick->symtab)
2326 continue;
2327
2328 dw2_require_line_header (objfile, per_cu);
2329 if (!per_cu->v.quick->lines)
2330 continue;
2331
2332 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2333 {
2334 const char *this_name = per_cu->v.quick->file_names[j];
2335 if (strcmp (this_name, filename) == 0)
2336 {
2337 dw2_instantiate_symtab (objfile, per_cu);
2338 break;
2339 }
2340 }
2341 }
2342 }
2343
2344 static const char *
2345 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2346 {
2347 struct dwarf2_per_cu_data *per_cu;
2348 offset_type *vec;
2349
2350 dw2_setup (objfile);
2351
2352 if (!dwarf2_per_objfile->index_table)
2353 return NULL;
2354
2355 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2356 name, &vec))
2357 return NULL;
2358
2359 /* Note that this just looks at the very first one named NAME -- but
2360 actually we are looking for a function. find_main_filename
2361 should be rewritten so that it doesn't require a custom hook. It
2362 could just use the ordinary symbol tables. */
2363 /* vec[0] is the length, which must always be >0. */
2364 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2365
2366 dw2_require_line_header (objfile, per_cu);
2367 if (!per_cu->v.quick->lines)
2368 return NULL;
2369
2370 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
2371 }
2372
2373 static void
2374 dw2_map_ada_symtabs (struct objfile *objfile,
2375 int (*wild_match) (const char *, const char *),
2376 int (*is_name_suffix) (const char *),
2377 void (*callback) (struct objfile *,
2378 struct symtab *, void *),
2379 const char *name, int global,
2380 domain_enum namespace, int wild,
2381 void *data)
2382 {
2383 /* For now, we don't support Ada. Still the function can be called if the
2384 current language is Ada for a non-Ada objfile using GNU index. As Ada
2385 does not look for non-Ada symbols this function should just return. */
2386 }
2387
2388 static void
2389 dw2_expand_symtabs_matching (struct objfile *objfile,
2390 int (*file_matcher) (const char *, void *),
2391 int (*name_matcher) (const char *, void *),
2392 domain_enum kind,
2393 void *data)
2394 {
2395 int i;
2396 offset_type iter;
2397 struct mapped_index *index;
2398
2399 dw2_setup (objfile);
2400 if (!dwarf2_per_objfile->index_table)
2401 return;
2402 index = dwarf2_per_objfile->index_table;
2403
2404 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2405 + dwarf2_per_objfile->n_type_comp_units); ++i)
2406 {
2407 int j;
2408 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2409
2410 per_cu->v.quick->mark = 0;
2411 if (per_cu->v.quick->symtab)
2412 continue;
2413
2414 dw2_require_line_header (objfile, per_cu);
2415 if (!per_cu->v.quick->lines)
2416 continue;
2417
2418 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2419 {
2420 if (file_matcher (per_cu->v.quick->file_names[j], data))
2421 {
2422 per_cu->v.quick->mark = 1;
2423 break;
2424 }
2425 }
2426 }
2427
2428 for (iter = 0; iter < index->index_table_slots; ++iter)
2429 {
2430 offset_type idx = 2 * iter;
2431 const char *name;
2432 offset_type *vec, vec_len, vec_idx;
2433
2434 if (index->index_table[idx] == 0 && index->index_table[idx + 1] == 0)
2435 continue;
2436
2437 name = index->constant_pool + MAYBE_SWAP (index->index_table[idx]);
2438
2439 if (! (*name_matcher) (name, data))
2440 continue;
2441
2442 /* The name was matched, now expand corresponding CUs that were
2443 marked. */
2444 vec = (offset_type *) (index->constant_pool
2445 + MAYBE_SWAP (index->index_table[idx + 1]));
2446 vec_len = MAYBE_SWAP (vec[0]);
2447 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2448 {
2449 struct dwarf2_per_cu_data *per_cu;
2450
2451 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2452 if (per_cu->v.quick->mark)
2453 dw2_instantiate_symtab (objfile, per_cu);
2454 }
2455 }
2456 }
2457
2458 static struct symtab *
2459 dw2_find_pc_sect_symtab (struct objfile *objfile,
2460 struct minimal_symbol *msymbol,
2461 CORE_ADDR pc,
2462 struct obj_section *section,
2463 int warn_if_readin)
2464 {
2465 struct dwarf2_per_cu_data *data;
2466
2467 dw2_setup (objfile);
2468
2469 if (!objfile->psymtabs_addrmap)
2470 return NULL;
2471
2472 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2473 if (!data)
2474 return NULL;
2475
2476 if (warn_if_readin && data->v.quick->symtab)
2477 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2478 paddress (get_objfile_arch (objfile), pc));
2479
2480 return dw2_instantiate_symtab (objfile, data);
2481 }
2482
2483 static void
2484 dw2_map_symbol_names (struct objfile *objfile,
2485 void (*fun) (const char *, void *),
2486 void *data)
2487 {
2488 offset_type iter;
2489 struct mapped_index *index;
2490
2491 dw2_setup (objfile);
2492
2493 if (!dwarf2_per_objfile->index_table)
2494 return;
2495 index = dwarf2_per_objfile->index_table;
2496
2497 for (iter = 0; iter < index->index_table_slots; ++iter)
2498 {
2499 offset_type idx = 2 * iter;
2500 const char *name;
2501 offset_type *vec, vec_len, vec_idx;
2502
2503 if (index->index_table[idx] == 0 && index->index_table[idx + 1] == 0)
2504 continue;
2505
2506 name = (index->constant_pool + MAYBE_SWAP (index->index_table[idx]));
2507
2508 (*fun) (name, data);
2509 }
2510 }
2511
2512 static void
2513 dw2_map_symbol_filenames (struct objfile *objfile,
2514 void (*fun) (const char *, const char *, void *),
2515 void *data)
2516 {
2517 int i;
2518
2519 dw2_setup (objfile);
2520 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2521 + dwarf2_per_objfile->n_type_comp_units); ++i)
2522 {
2523 int j;
2524 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2525
2526 if (per_cu->v.quick->symtab)
2527 continue;
2528
2529 dw2_require_line_header (objfile, per_cu);
2530 if (!per_cu->v.quick->lines)
2531 continue;
2532
2533 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2534 {
2535 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2536 j);
2537 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
2538 }
2539 }
2540 }
2541
2542 static int
2543 dw2_has_symbols (struct objfile *objfile)
2544 {
2545 return 1;
2546 }
2547
2548 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2549 {
2550 dw2_has_symbols,
2551 dw2_find_last_source_symtab,
2552 dw2_forget_cached_source_info,
2553 dw2_lookup_symtab,
2554 dw2_lookup_symbol,
2555 dw2_pre_expand_symtabs_matching,
2556 dw2_print_stats,
2557 dw2_dump,
2558 dw2_relocate,
2559 dw2_expand_symtabs_for_function,
2560 dw2_expand_all_symtabs,
2561 dw2_expand_symtabs_with_filename,
2562 dw2_find_symbol_file,
2563 dw2_map_ada_symtabs,
2564 dw2_expand_symtabs_matching,
2565 dw2_find_pc_sect_symtab,
2566 dw2_map_symbol_names,
2567 dw2_map_symbol_filenames
2568 };
2569
2570 /* Initialize for reading DWARF for this objfile. Return 0 if this
2571 file will use psymtabs, or 1 if using the GNU index. */
2572
2573 int
2574 dwarf2_initialize_objfile (struct objfile *objfile)
2575 {
2576 /* If we're about to read full symbols, don't bother with the
2577 indices. In this case we also don't care if some other debug
2578 format is making psymtabs, because they are all about to be
2579 expanded anyway. */
2580 if ((objfile->flags & OBJF_READNOW))
2581 {
2582 int i;
2583
2584 dwarf2_per_objfile->using_index = 1;
2585 create_all_comp_units (objfile);
2586 create_debug_types_hash_table (objfile);
2587
2588 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2589 + dwarf2_per_objfile->n_type_comp_units); ++i)
2590 {
2591 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2592
2593 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2594 struct dwarf2_per_cu_quick_data);
2595 }
2596
2597 /* Return 1 so that gdb sees the "quick" functions. However,
2598 these functions will be no-ops because we will have expanded
2599 all symtabs. */
2600 return 1;
2601 }
2602
2603 if (dwarf2_read_index (objfile))
2604 return 1;
2605
2606 dwarf2_build_psymtabs (objfile);
2607 return 0;
2608 }
2609
2610 \f
2611
2612 /* Build a partial symbol table. */
2613
2614 void
2615 dwarf2_build_psymtabs (struct objfile *objfile)
2616 {
2617 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2618 {
2619 init_psymbol_list (objfile, 1024);
2620 }
2621
2622 dwarf2_build_psymtabs_hard (objfile);
2623 }
2624
2625 /* Return TRUE if OFFSET is within CU_HEADER. */
2626
2627 static inline int
2628 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2629 {
2630 unsigned int bottom = cu_header->offset;
2631 unsigned int top = (cu_header->offset
2632 + cu_header->length
2633 + cu_header->initial_length_size);
2634
2635 return (offset >= bottom && offset < top);
2636 }
2637
2638 /* Read in the comp unit header information from the debug_info at info_ptr.
2639 NOTE: This leaves members offset, first_die_offset to be filled in
2640 by the caller. */
2641
2642 static gdb_byte *
2643 read_comp_unit_head (struct comp_unit_head *cu_header,
2644 gdb_byte *info_ptr, bfd *abfd)
2645 {
2646 int signed_addr;
2647 unsigned int bytes_read;
2648
2649 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2650 cu_header->initial_length_size = bytes_read;
2651 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2652 info_ptr += bytes_read;
2653 cu_header->version = read_2_bytes (abfd, info_ptr);
2654 info_ptr += 2;
2655 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2656 &bytes_read);
2657 info_ptr += bytes_read;
2658 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2659 info_ptr += 1;
2660 signed_addr = bfd_get_sign_extend_vma (abfd);
2661 if (signed_addr < 0)
2662 internal_error (__FILE__, __LINE__,
2663 _("read_comp_unit_head: dwarf from non elf file"));
2664 cu_header->signed_addr_p = signed_addr;
2665
2666 return info_ptr;
2667 }
2668
2669 static gdb_byte *
2670 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2671 gdb_byte *buffer, unsigned int buffer_size,
2672 bfd *abfd)
2673 {
2674 gdb_byte *beg_of_comp_unit = info_ptr;
2675
2676 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2677
2678 if (header->version != 2 && header->version != 3 && header->version != 4)
2679 error (_("Dwarf Error: wrong version in compilation unit header "
2680 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2681 bfd_get_filename (abfd));
2682
2683 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2684 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2685 "(offset 0x%lx + 6) [in module %s]"),
2686 (long) header->abbrev_offset,
2687 (long) (beg_of_comp_unit - buffer),
2688 bfd_get_filename (abfd));
2689
2690 if (beg_of_comp_unit + header->length + header->initial_length_size
2691 > buffer + buffer_size)
2692 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2693 "(offset 0x%lx + 0) [in module %s]"),
2694 (long) header->length,
2695 (long) (beg_of_comp_unit - buffer),
2696 bfd_get_filename (abfd));
2697
2698 return info_ptr;
2699 }
2700
2701 /* Read in the types comp unit header information from .debug_types entry at
2702 types_ptr. The result is a pointer to one past the end of the header. */
2703
2704 static gdb_byte *
2705 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2706 ULONGEST *signature,
2707 gdb_byte *types_ptr, bfd *abfd)
2708 {
2709 gdb_byte *initial_types_ptr = types_ptr;
2710
2711 dwarf2_read_section (dwarf2_per_objfile->objfile,
2712 &dwarf2_per_objfile->types);
2713 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2714
2715 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2716
2717 *signature = read_8_bytes (abfd, types_ptr);
2718 types_ptr += 8;
2719 types_ptr += cu_header->offset_size;
2720 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2721
2722 return types_ptr;
2723 }
2724
2725 /* Allocate a new partial symtab for file named NAME and mark this new
2726 partial symtab as being an include of PST. */
2727
2728 static void
2729 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2730 struct objfile *objfile)
2731 {
2732 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2733
2734 subpst->section_offsets = pst->section_offsets;
2735 subpst->textlow = 0;
2736 subpst->texthigh = 0;
2737
2738 subpst->dependencies = (struct partial_symtab **)
2739 obstack_alloc (&objfile->objfile_obstack,
2740 sizeof (struct partial_symtab *));
2741 subpst->dependencies[0] = pst;
2742 subpst->number_of_dependencies = 1;
2743
2744 subpst->globals_offset = 0;
2745 subpst->n_global_syms = 0;
2746 subpst->statics_offset = 0;
2747 subpst->n_static_syms = 0;
2748 subpst->symtab = NULL;
2749 subpst->read_symtab = pst->read_symtab;
2750 subpst->readin = 0;
2751
2752 /* No private part is necessary for include psymtabs. This property
2753 can be used to differentiate between such include psymtabs and
2754 the regular ones. */
2755 subpst->read_symtab_private = NULL;
2756 }
2757
2758 /* Read the Line Number Program data and extract the list of files
2759 included by the source file represented by PST. Build an include
2760 partial symtab for each of these included files. */
2761
2762 static void
2763 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2764 struct die_info *die,
2765 struct partial_symtab *pst)
2766 {
2767 struct objfile *objfile = cu->objfile;
2768 bfd *abfd = objfile->obfd;
2769 struct line_header *lh = NULL;
2770 struct attribute *attr;
2771
2772 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2773 if (attr)
2774 {
2775 unsigned int line_offset = DW_UNSND (attr);
2776
2777 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2778 }
2779 if (lh == NULL)
2780 return; /* No linetable, so no includes. */
2781
2782 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2783 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2784
2785 free_line_header (lh);
2786 }
2787
2788 static hashval_t
2789 hash_type_signature (const void *item)
2790 {
2791 const struct signatured_type *type_sig = item;
2792
2793 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2794 return type_sig->signature;
2795 }
2796
2797 static int
2798 eq_type_signature (const void *item_lhs, const void *item_rhs)
2799 {
2800 const struct signatured_type *lhs = item_lhs;
2801 const struct signatured_type *rhs = item_rhs;
2802
2803 return lhs->signature == rhs->signature;
2804 }
2805
2806 /* Allocate a hash table for signatured types. */
2807
2808 static htab_t
2809 allocate_signatured_type_table (struct objfile *objfile)
2810 {
2811 return htab_create_alloc_ex (41,
2812 hash_type_signature,
2813 eq_type_signature,
2814 NULL,
2815 &objfile->objfile_obstack,
2816 hashtab_obstack_allocate,
2817 dummy_obstack_deallocate);
2818 }
2819
2820 /* A helper function to add a signatured type CU to a list. */
2821
2822 static int
2823 add_signatured_type_cu_to_list (void **slot, void *datum)
2824 {
2825 struct signatured_type *sigt = *slot;
2826 struct dwarf2_per_cu_data ***datap = datum;
2827
2828 **datap = &sigt->per_cu;
2829 ++*datap;
2830
2831 return 1;
2832 }
2833
2834 /* Create the hash table of all entries in the .debug_types section.
2835 The result is zero if there is an error (e.g. missing .debug_types section),
2836 otherwise non-zero. */
2837
2838 static int
2839 create_debug_types_hash_table (struct objfile *objfile)
2840 {
2841 gdb_byte *info_ptr;
2842 htab_t types_htab;
2843 struct dwarf2_per_cu_data **iter;
2844
2845 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2846 info_ptr = dwarf2_per_objfile->types.buffer;
2847
2848 if (info_ptr == NULL)
2849 {
2850 dwarf2_per_objfile->signatured_types = NULL;
2851 return 0;
2852 }
2853
2854 types_htab = allocate_signatured_type_table (objfile);
2855
2856 if (dwarf2_die_debug)
2857 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2858
2859 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2860 {
2861 unsigned int offset;
2862 unsigned int offset_size;
2863 unsigned int type_offset;
2864 unsigned int length, initial_length_size;
2865 unsigned short version;
2866 ULONGEST signature;
2867 struct signatured_type *type_sig;
2868 void **slot;
2869 gdb_byte *ptr = info_ptr;
2870
2871 offset = ptr - dwarf2_per_objfile->types.buffer;
2872
2873 /* We need to read the type's signature in order to build the hash
2874 table, but we don't need to read anything else just yet. */
2875
2876 /* Sanity check to ensure entire cu is present. */
2877 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2878 if (ptr + length + initial_length_size
2879 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2880 {
2881 complaint (&symfile_complaints,
2882 _("debug type entry runs off end of `.debug_types' section, ignored"));
2883 break;
2884 }
2885
2886 offset_size = initial_length_size == 4 ? 4 : 8;
2887 ptr += initial_length_size;
2888 version = bfd_get_16 (objfile->obfd, ptr);
2889 ptr += 2;
2890 ptr += offset_size; /* abbrev offset */
2891 ptr += 1; /* address size */
2892 signature = bfd_get_64 (objfile->obfd, ptr);
2893 ptr += 8;
2894 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2895
2896 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2897 memset (type_sig, 0, sizeof (*type_sig));
2898 type_sig->signature = signature;
2899 type_sig->offset = offset;
2900 type_sig->type_offset = type_offset;
2901 type_sig->per_cu.objfile = objfile;
2902 type_sig->per_cu.from_debug_types = 1;
2903
2904 slot = htab_find_slot (types_htab, type_sig, INSERT);
2905 gdb_assert (slot != NULL);
2906 *slot = type_sig;
2907
2908 if (dwarf2_die_debug)
2909 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2910 offset, phex (signature, sizeof (signature)));
2911
2912 info_ptr = info_ptr + initial_length_size + length;
2913 }
2914
2915 dwarf2_per_objfile->signatured_types = types_htab;
2916
2917 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2918 dwarf2_per_objfile->type_comp_units
2919 = obstack_alloc (&objfile->objfile_obstack,
2920 dwarf2_per_objfile->n_type_comp_units
2921 * sizeof (struct dwarf2_per_cu_data *));
2922 iter = &dwarf2_per_objfile->type_comp_units[0];
2923 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2924 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2925 == dwarf2_per_objfile->n_type_comp_units);
2926
2927 return 1;
2928 }
2929
2930 /* Lookup a signature based type.
2931 Returns NULL if SIG is not present in the table. */
2932
2933 static struct signatured_type *
2934 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2935 {
2936 struct signatured_type find_entry, *entry;
2937
2938 if (dwarf2_per_objfile->signatured_types == NULL)
2939 {
2940 complaint (&symfile_complaints,
2941 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2942 return 0;
2943 }
2944
2945 find_entry.signature = sig;
2946 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2947 return entry;
2948 }
2949
2950 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2951
2952 static void
2953 init_cu_die_reader (struct die_reader_specs *reader,
2954 struct dwarf2_cu *cu)
2955 {
2956 reader->abfd = cu->objfile->obfd;
2957 reader->cu = cu;
2958 if (cu->per_cu->from_debug_types)
2959 {
2960 gdb_assert (dwarf2_per_objfile->types.readin);
2961 reader->buffer = dwarf2_per_objfile->types.buffer;
2962 }
2963 else
2964 {
2965 gdb_assert (dwarf2_per_objfile->info.readin);
2966 reader->buffer = dwarf2_per_objfile->info.buffer;
2967 }
2968 }
2969
2970 /* Find the base address of the compilation unit for range lists and
2971 location lists. It will normally be specified by DW_AT_low_pc.
2972 In DWARF-3 draft 4, the base address could be overridden by
2973 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2974 compilation units with discontinuous ranges. */
2975
2976 static void
2977 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2978 {
2979 struct attribute *attr;
2980
2981 cu->base_known = 0;
2982 cu->base_address = 0;
2983
2984 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2985 if (attr)
2986 {
2987 cu->base_address = DW_ADDR (attr);
2988 cu->base_known = 1;
2989 }
2990 else
2991 {
2992 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2993 if (attr)
2994 {
2995 cu->base_address = DW_ADDR (attr);
2996 cu->base_known = 1;
2997 }
2998 }
2999 }
3000
3001 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3002 to combine the common parts.
3003 Process a compilation unit for a psymtab.
3004 BUFFER is a pointer to the beginning of the dwarf section buffer,
3005 either .debug_info or debug_types.
3006 INFO_PTR is a pointer to the start of the CU.
3007 Returns a pointer to the next CU. */
3008
3009 static gdb_byte *
3010 process_psymtab_comp_unit (struct objfile *objfile,
3011 struct dwarf2_per_cu_data *this_cu,
3012 gdb_byte *buffer, gdb_byte *info_ptr,
3013 unsigned int buffer_size)
3014 {
3015 bfd *abfd = objfile->obfd;
3016 gdb_byte *beg_of_comp_unit = info_ptr;
3017 struct die_info *comp_unit_die;
3018 struct partial_symtab *pst;
3019 CORE_ADDR baseaddr;
3020 struct cleanup *back_to_inner;
3021 struct dwarf2_cu cu;
3022 int has_children, has_pc_info;
3023 struct attribute *attr;
3024 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3025 struct die_reader_specs reader_specs;
3026
3027 memset (&cu, 0, sizeof (cu));
3028 cu.objfile = objfile;
3029 obstack_init (&cu.comp_unit_obstack);
3030
3031 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3032
3033 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3034 buffer, buffer_size,
3035 abfd);
3036
3037 /* Complete the cu_header. */
3038 cu.header.offset = beg_of_comp_unit - buffer;
3039 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3040
3041 cu.list_in_scope = &file_symbols;
3042
3043 /* If this compilation unit was already read in, free the
3044 cached copy in order to read it in again. This is
3045 necessary because we skipped some symbols when we first
3046 read in the compilation unit (see load_partial_dies).
3047 This problem could be avoided, but the benefit is
3048 unclear. */
3049 if (this_cu->cu != NULL)
3050 free_one_cached_comp_unit (this_cu->cu);
3051
3052 /* Note that this is a pointer to our stack frame, being
3053 added to a global data structure. It will be cleaned up
3054 in free_stack_comp_unit when we finish with this
3055 compilation unit. */
3056 this_cu->cu = &cu;
3057 cu.per_cu = this_cu;
3058
3059 /* Read the abbrevs for this compilation unit into a table. */
3060 dwarf2_read_abbrevs (abfd, &cu);
3061 make_cleanup (dwarf2_free_abbrev_table, &cu);
3062
3063 /* Read the compilation unit die. */
3064 if (this_cu->from_debug_types)
3065 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3066 init_cu_die_reader (&reader_specs, &cu);
3067 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3068 &has_children);
3069
3070 if (this_cu->from_debug_types)
3071 {
3072 /* offset,length haven't been set yet for type units. */
3073 this_cu->offset = cu.header.offset;
3074 this_cu->length = cu.header.length + cu.header.initial_length_size;
3075 }
3076 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3077 {
3078 info_ptr = (beg_of_comp_unit + cu.header.length
3079 + cu.header.initial_length_size);
3080 do_cleanups (back_to_inner);
3081 return info_ptr;
3082 }
3083
3084 /* Set the language we're debugging. */
3085 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3086 if (attr)
3087 set_cu_language (DW_UNSND (attr), &cu);
3088 else
3089 set_cu_language (language_minimal, &cu);
3090
3091 /* Allocate a new partial symbol table structure. */
3092 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3093 pst = start_psymtab_common (objfile, objfile->section_offsets,
3094 (attr != NULL) ? DW_STRING (attr) : "",
3095 /* TEXTLOW and TEXTHIGH are set below. */
3096 0,
3097 objfile->global_psymbols.next,
3098 objfile->static_psymbols.next);
3099
3100 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3101 if (attr != NULL)
3102 pst->dirname = DW_STRING (attr);
3103
3104 pst->read_symtab_private = this_cu;
3105
3106 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3107
3108 /* Store the function that reads in the rest of the symbol table */
3109 pst->read_symtab = dwarf2_psymtab_to_symtab;
3110
3111 this_cu->v.psymtab = pst;
3112
3113 dwarf2_find_base_address (comp_unit_die, &cu);
3114
3115 /* Possibly set the default values of LOWPC and HIGHPC from
3116 `DW_AT_ranges'. */
3117 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3118 &best_highpc, &cu, pst);
3119 if (has_pc_info == 1 && best_lowpc < best_highpc)
3120 /* Store the contiguous range if it is not empty; it can be empty for
3121 CUs with no code. */
3122 addrmap_set_empty (objfile->psymtabs_addrmap,
3123 best_lowpc + baseaddr,
3124 best_highpc + baseaddr - 1, pst);
3125
3126 /* Check if comp unit has_children.
3127 If so, read the rest of the partial symbols from this comp unit.
3128 If not, there's no more debug_info for this comp unit. */
3129 if (has_children)
3130 {
3131 struct partial_die_info *first_die;
3132 CORE_ADDR lowpc, highpc;
3133
3134 lowpc = ((CORE_ADDR) -1);
3135 highpc = ((CORE_ADDR) 0);
3136
3137 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3138
3139 scan_partial_symbols (first_die, &lowpc, &highpc,
3140 ! has_pc_info, &cu);
3141
3142 /* If we didn't find a lowpc, set it to highpc to avoid
3143 complaints from `maint check'. */
3144 if (lowpc == ((CORE_ADDR) -1))
3145 lowpc = highpc;
3146
3147 /* If the compilation unit didn't have an explicit address range,
3148 then use the information extracted from its child dies. */
3149 if (! has_pc_info)
3150 {
3151 best_lowpc = lowpc;
3152 best_highpc = highpc;
3153 }
3154 }
3155 pst->textlow = best_lowpc + baseaddr;
3156 pst->texthigh = best_highpc + baseaddr;
3157
3158 pst->n_global_syms = objfile->global_psymbols.next -
3159 (objfile->global_psymbols.list + pst->globals_offset);
3160 pst->n_static_syms = objfile->static_psymbols.next -
3161 (objfile->static_psymbols.list + pst->statics_offset);
3162 sort_pst_symbols (pst);
3163
3164 info_ptr = (beg_of_comp_unit + cu.header.length
3165 + cu.header.initial_length_size);
3166
3167 if (this_cu->from_debug_types)
3168 {
3169 /* It's not clear we want to do anything with stmt lists here.
3170 Waiting to see what gcc ultimately does. */
3171 }
3172 else
3173 {
3174 /* Get the list of files included in the current compilation unit,
3175 and build a psymtab for each of them. */
3176 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3177 }
3178
3179 do_cleanups (back_to_inner);
3180
3181 return info_ptr;
3182 }
3183
3184 /* Traversal function for htab_traverse_noresize.
3185 Process one .debug_types comp-unit. */
3186
3187 static int
3188 process_type_comp_unit (void **slot, void *info)
3189 {
3190 struct signatured_type *entry = (struct signatured_type *) *slot;
3191 struct objfile *objfile = (struct objfile *) info;
3192 struct dwarf2_per_cu_data *this_cu;
3193
3194 this_cu = &entry->per_cu;
3195
3196 gdb_assert (dwarf2_per_objfile->types.readin);
3197 process_psymtab_comp_unit (objfile, this_cu,
3198 dwarf2_per_objfile->types.buffer,
3199 dwarf2_per_objfile->types.buffer + entry->offset,
3200 dwarf2_per_objfile->types.size);
3201
3202 return 1;
3203 }
3204
3205 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3206 Build partial symbol tables for the .debug_types comp-units. */
3207
3208 static void
3209 build_type_psymtabs (struct objfile *objfile)
3210 {
3211 if (! create_debug_types_hash_table (objfile))
3212 return;
3213
3214 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3215 process_type_comp_unit, objfile);
3216 }
3217
3218 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3219
3220 static void
3221 psymtabs_addrmap_cleanup (void *o)
3222 {
3223 struct objfile *objfile = o;
3224
3225 objfile->psymtabs_addrmap = NULL;
3226 }
3227
3228 /* Build the partial symbol table by doing a quick pass through the
3229 .debug_info and .debug_abbrev sections. */
3230
3231 static void
3232 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3233 {
3234 gdb_byte *info_ptr;
3235 struct cleanup *back_to, *addrmap_cleanup;
3236 struct obstack temp_obstack;
3237
3238 dwarf2_per_objfile->reading_partial_symbols = 1;
3239
3240 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3241 info_ptr = dwarf2_per_objfile->info.buffer;
3242
3243 /* Any cached compilation units will be linked by the per-objfile
3244 read_in_chain. Make sure to free them when we're done. */
3245 back_to = make_cleanup (free_cached_comp_units, NULL);
3246
3247 build_type_psymtabs (objfile);
3248
3249 create_all_comp_units (objfile);
3250
3251 /* Create a temporary address map on a temporary obstack. We later
3252 copy this to the final obstack. */
3253 obstack_init (&temp_obstack);
3254 make_cleanup_obstack_free (&temp_obstack);
3255 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3256 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3257
3258 /* Since the objects we're extracting from .debug_info vary in
3259 length, only the individual functions to extract them (like
3260 read_comp_unit_head and load_partial_die) can really know whether
3261 the buffer is large enough to hold another complete object.
3262
3263 At the moment, they don't actually check that. If .debug_info
3264 holds just one extra byte after the last compilation unit's dies,
3265 then read_comp_unit_head will happily read off the end of the
3266 buffer. read_partial_die is similarly casual. Those functions
3267 should be fixed.
3268
3269 For this loop condition, simply checking whether there's any data
3270 left at all should be sufficient. */
3271
3272 while (info_ptr < (dwarf2_per_objfile->info.buffer
3273 + dwarf2_per_objfile->info.size))
3274 {
3275 struct dwarf2_per_cu_data *this_cu;
3276
3277 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3278 objfile);
3279
3280 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3281 dwarf2_per_objfile->info.buffer,
3282 info_ptr,
3283 dwarf2_per_objfile->info.size);
3284 }
3285
3286 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3287 &objfile->objfile_obstack);
3288 discard_cleanups (addrmap_cleanup);
3289
3290 do_cleanups (back_to);
3291 }
3292
3293 /* Load the partial DIEs for a secondary CU into memory. */
3294
3295 static void
3296 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3297 struct objfile *objfile)
3298 {
3299 bfd *abfd = objfile->obfd;
3300 gdb_byte *info_ptr, *beg_of_comp_unit;
3301 struct die_info *comp_unit_die;
3302 struct dwarf2_cu *cu;
3303 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3304 struct attribute *attr;
3305 int has_children;
3306 struct die_reader_specs reader_specs;
3307 int read_cu = 0;
3308
3309 gdb_assert (! this_cu->from_debug_types);
3310
3311 gdb_assert (dwarf2_per_objfile->info.readin);
3312 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3313 beg_of_comp_unit = info_ptr;
3314
3315 if (this_cu->cu == NULL)
3316 {
3317 cu = alloc_one_comp_unit (objfile);
3318
3319 read_cu = 1;
3320
3321 /* If an error occurs while loading, release our storage. */
3322 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3323
3324 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3325 dwarf2_per_objfile->info.buffer,
3326 dwarf2_per_objfile->info.size,
3327 abfd);
3328
3329 /* Complete the cu_header. */
3330 cu->header.offset = this_cu->offset;
3331 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3332
3333 /* Link this compilation unit into the compilation unit tree. */
3334 this_cu->cu = cu;
3335 cu->per_cu = this_cu;
3336
3337 /* Link this CU into read_in_chain. */
3338 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3339 dwarf2_per_objfile->read_in_chain = this_cu;
3340 }
3341 else
3342 {
3343 cu = this_cu->cu;
3344 info_ptr += cu->header.first_die_offset;
3345 }
3346
3347 /* Read the abbrevs for this compilation unit into a table. */
3348 gdb_assert (cu->dwarf2_abbrevs == NULL);
3349 dwarf2_read_abbrevs (abfd, cu);
3350 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3351
3352 /* Read the compilation unit die. */
3353 init_cu_die_reader (&reader_specs, cu);
3354 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3355 &has_children);
3356
3357 /* Set the language we're debugging. */
3358 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3359 if (attr)
3360 set_cu_language (DW_UNSND (attr), cu);
3361 else
3362 set_cu_language (language_minimal, cu);
3363
3364 /* Check if comp unit has_children.
3365 If so, read the rest of the partial symbols from this comp unit.
3366 If not, there's no more debug_info for this comp unit. */
3367 if (has_children)
3368 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3369
3370 do_cleanups (free_abbrevs_cleanup);
3371
3372 if (read_cu)
3373 {
3374 /* We've successfully allocated this compilation unit. Let our
3375 caller clean it up when finished with it. */
3376 discard_cleanups (free_cu_cleanup);
3377 }
3378 }
3379
3380 /* Create a list of all compilation units in OBJFILE. We do this only
3381 if an inter-comp-unit reference is found; presumably if there is one,
3382 there will be many, and one will occur early in the .debug_info section.
3383 So there's no point in building this list incrementally. */
3384
3385 static void
3386 create_all_comp_units (struct objfile *objfile)
3387 {
3388 int n_allocated;
3389 int n_comp_units;
3390 struct dwarf2_per_cu_data **all_comp_units;
3391 gdb_byte *info_ptr;
3392
3393 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3394 info_ptr = dwarf2_per_objfile->info.buffer;
3395
3396 n_comp_units = 0;
3397 n_allocated = 10;
3398 all_comp_units = xmalloc (n_allocated
3399 * sizeof (struct dwarf2_per_cu_data *));
3400
3401 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3402 {
3403 unsigned int length, initial_length_size;
3404 struct dwarf2_per_cu_data *this_cu;
3405 unsigned int offset;
3406
3407 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3408
3409 /* Read just enough information to find out where the next
3410 compilation unit is. */
3411 length = read_initial_length (objfile->obfd, info_ptr,
3412 &initial_length_size);
3413
3414 /* Save the compilation unit for later lookup. */
3415 this_cu = obstack_alloc (&objfile->objfile_obstack,
3416 sizeof (struct dwarf2_per_cu_data));
3417 memset (this_cu, 0, sizeof (*this_cu));
3418 this_cu->offset = offset;
3419 this_cu->length = length + initial_length_size;
3420 this_cu->objfile = objfile;
3421
3422 if (n_comp_units == n_allocated)
3423 {
3424 n_allocated *= 2;
3425 all_comp_units = xrealloc (all_comp_units,
3426 n_allocated
3427 * sizeof (struct dwarf2_per_cu_data *));
3428 }
3429 all_comp_units[n_comp_units++] = this_cu;
3430
3431 info_ptr = info_ptr + this_cu->length;
3432 }
3433
3434 dwarf2_per_objfile->all_comp_units
3435 = obstack_alloc (&objfile->objfile_obstack,
3436 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3437 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3438 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3439 xfree (all_comp_units);
3440 dwarf2_per_objfile->n_comp_units = n_comp_units;
3441 }
3442
3443 /* Process all loaded DIEs for compilation unit CU, starting at
3444 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3445 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3446 DW_AT_ranges). If NEED_PC is set, then this function will set
3447 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3448 and record the covered ranges in the addrmap. */
3449
3450 static void
3451 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3452 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3453 {
3454 struct partial_die_info *pdi;
3455
3456 /* Now, march along the PDI's, descending into ones which have
3457 interesting children but skipping the children of the other ones,
3458 until we reach the end of the compilation unit. */
3459
3460 pdi = first_die;
3461
3462 while (pdi != NULL)
3463 {
3464 fixup_partial_die (pdi, cu);
3465
3466 /* Anonymous namespaces or modules have no name but have interesting
3467 children, so we need to look at them. Ditto for anonymous
3468 enums. */
3469
3470 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3471 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3472 {
3473 switch (pdi->tag)
3474 {
3475 case DW_TAG_subprogram:
3476 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3477 break;
3478 case DW_TAG_constant:
3479 case DW_TAG_variable:
3480 case DW_TAG_typedef:
3481 case DW_TAG_union_type:
3482 if (!pdi->is_declaration)
3483 {
3484 add_partial_symbol (pdi, cu);
3485 }
3486 break;
3487 case DW_TAG_class_type:
3488 case DW_TAG_interface_type:
3489 case DW_TAG_structure_type:
3490 if (!pdi->is_declaration)
3491 {
3492 add_partial_symbol (pdi, cu);
3493 }
3494 break;
3495 case DW_TAG_enumeration_type:
3496 if (!pdi->is_declaration)
3497 add_partial_enumeration (pdi, cu);
3498 break;
3499 case DW_TAG_base_type:
3500 case DW_TAG_subrange_type:
3501 /* File scope base type definitions are added to the partial
3502 symbol table. */
3503 add_partial_symbol (pdi, cu);
3504 break;
3505 case DW_TAG_namespace:
3506 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3507 break;
3508 case DW_TAG_module:
3509 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3510 break;
3511 default:
3512 break;
3513 }
3514 }
3515
3516 /* If the die has a sibling, skip to the sibling. */
3517
3518 pdi = pdi->die_sibling;
3519 }
3520 }
3521
3522 /* Functions used to compute the fully scoped name of a partial DIE.
3523
3524 Normally, this is simple. For C++, the parent DIE's fully scoped
3525 name is concatenated with "::" and the partial DIE's name. For
3526 Java, the same thing occurs except that "." is used instead of "::".
3527 Enumerators are an exception; they use the scope of their parent
3528 enumeration type, i.e. the name of the enumeration type is not
3529 prepended to the enumerator.
3530
3531 There are two complexities. One is DW_AT_specification; in this
3532 case "parent" means the parent of the target of the specification,
3533 instead of the direct parent of the DIE. The other is compilers
3534 which do not emit DW_TAG_namespace; in this case we try to guess
3535 the fully qualified name of structure types from their members'
3536 linkage names. This must be done using the DIE's children rather
3537 than the children of any DW_AT_specification target. We only need
3538 to do this for structures at the top level, i.e. if the target of
3539 any DW_AT_specification (if any; otherwise the DIE itself) does not
3540 have a parent. */
3541
3542 /* Compute the scope prefix associated with PDI's parent, in
3543 compilation unit CU. The result will be allocated on CU's
3544 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3545 field. NULL is returned if no prefix is necessary. */
3546 static char *
3547 partial_die_parent_scope (struct partial_die_info *pdi,
3548 struct dwarf2_cu *cu)
3549 {
3550 char *grandparent_scope;
3551 struct partial_die_info *parent, *real_pdi;
3552
3553 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3554 then this means the parent of the specification DIE. */
3555
3556 real_pdi = pdi;
3557 while (real_pdi->has_specification)
3558 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3559
3560 parent = real_pdi->die_parent;
3561 if (parent == NULL)
3562 return NULL;
3563
3564 if (parent->scope_set)
3565 return parent->scope;
3566
3567 fixup_partial_die (parent, cu);
3568
3569 grandparent_scope = partial_die_parent_scope (parent, cu);
3570
3571 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3572 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3573 Work around this problem here. */
3574 if (cu->language == language_cplus
3575 && parent->tag == DW_TAG_namespace
3576 && strcmp (parent->name, "::") == 0
3577 && grandparent_scope == NULL)
3578 {
3579 parent->scope = NULL;
3580 parent->scope_set = 1;
3581 return NULL;
3582 }
3583
3584 if (parent->tag == DW_TAG_namespace
3585 || parent->tag == DW_TAG_module
3586 || parent->tag == DW_TAG_structure_type
3587 || parent->tag == DW_TAG_class_type
3588 || parent->tag == DW_TAG_interface_type
3589 || parent->tag == DW_TAG_union_type
3590 || parent->tag == DW_TAG_enumeration_type)
3591 {
3592 if (grandparent_scope == NULL)
3593 parent->scope = parent->name;
3594 else
3595 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3596 parent->name, 0, cu);
3597 }
3598 else if (parent->tag == DW_TAG_enumerator)
3599 /* Enumerators should not get the name of the enumeration as a prefix. */
3600 parent->scope = grandparent_scope;
3601 else
3602 {
3603 /* FIXME drow/2004-04-01: What should we be doing with
3604 function-local names? For partial symbols, we should probably be
3605 ignoring them. */
3606 complaint (&symfile_complaints,
3607 _("unhandled containing DIE tag %d for DIE at %d"),
3608 parent->tag, pdi->offset);
3609 parent->scope = grandparent_scope;
3610 }
3611
3612 parent->scope_set = 1;
3613 return parent->scope;
3614 }
3615
3616 /* Return the fully scoped name associated with PDI, from compilation unit
3617 CU. The result will be allocated with malloc. */
3618 static char *
3619 partial_die_full_name (struct partial_die_info *pdi,
3620 struct dwarf2_cu *cu)
3621 {
3622 char *parent_scope;
3623
3624 /* If this is a template instantiation, we can not work out the
3625 template arguments from partial DIEs. So, unfortunately, we have
3626 to go through the full DIEs. At least any work we do building
3627 types here will be reused if full symbols are loaded later. */
3628 if (pdi->has_template_arguments)
3629 {
3630 fixup_partial_die (pdi, cu);
3631
3632 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3633 {
3634 struct die_info *die;
3635 struct attribute attr;
3636 struct dwarf2_cu *ref_cu = cu;
3637
3638 attr.name = 0;
3639 attr.form = DW_FORM_ref_addr;
3640 attr.u.addr = pdi->offset;
3641 die = follow_die_ref (NULL, &attr, &ref_cu);
3642
3643 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3644 }
3645 }
3646
3647 parent_scope = partial_die_parent_scope (pdi, cu);
3648 if (parent_scope == NULL)
3649 return NULL;
3650 else
3651 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3652 }
3653
3654 static void
3655 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3656 {
3657 struct objfile *objfile = cu->objfile;
3658 CORE_ADDR addr = 0;
3659 char *actual_name = NULL;
3660 const struct partial_symbol *psym = NULL;
3661 CORE_ADDR baseaddr;
3662 int built_actual_name = 0;
3663
3664 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3665
3666 actual_name = partial_die_full_name (pdi, cu);
3667 if (actual_name)
3668 built_actual_name = 1;
3669
3670 if (actual_name == NULL)
3671 actual_name = pdi->name;
3672
3673 switch (pdi->tag)
3674 {
3675 case DW_TAG_subprogram:
3676 if (pdi->is_external || cu->language == language_ada)
3677 {
3678 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3679 of the global scope. But in Ada, we want to be able to access
3680 nested procedures globally. So all Ada subprograms are stored
3681 in the global scope. */
3682 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3683 mst_text, objfile); */
3684 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3685 built_actual_name,
3686 VAR_DOMAIN, LOC_BLOCK,
3687 &objfile->global_psymbols,
3688 0, pdi->lowpc + baseaddr,
3689 cu->language, objfile);
3690 }
3691 else
3692 {
3693 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3694 mst_file_text, objfile); */
3695 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3696 built_actual_name,
3697 VAR_DOMAIN, LOC_BLOCK,
3698 &objfile->static_psymbols,
3699 0, pdi->lowpc + baseaddr,
3700 cu->language, objfile);
3701 }
3702 break;
3703 case DW_TAG_constant:
3704 {
3705 struct psymbol_allocation_list *list;
3706
3707 if (pdi->is_external)
3708 list = &objfile->global_psymbols;
3709 else
3710 list = &objfile->static_psymbols;
3711 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3712 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3713 list, 0, 0, cu->language, objfile);
3714
3715 }
3716 break;
3717 case DW_TAG_variable:
3718 if (pdi->locdesc)
3719 addr = decode_locdesc (pdi->locdesc, cu);
3720
3721 if (pdi->locdesc
3722 && addr == 0
3723 && !dwarf2_per_objfile->has_section_at_zero)
3724 {
3725 /* A global or static variable may also have been stripped
3726 out by the linker if unused, in which case its address
3727 will be nullified; do not add such variables into partial
3728 symbol table then. */
3729 }
3730 else if (pdi->is_external)
3731 {
3732 /* Global Variable.
3733 Don't enter into the minimal symbol tables as there is
3734 a minimal symbol table entry from the ELF symbols already.
3735 Enter into partial symbol table if it has a location
3736 descriptor or a type.
3737 If the location descriptor is missing, new_symbol will create
3738 a LOC_UNRESOLVED symbol, the address of the variable will then
3739 be determined from the minimal symbol table whenever the variable
3740 is referenced.
3741 The address for the partial symbol table entry is not
3742 used by GDB, but it comes in handy for debugging partial symbol
3743 table building. */
3744
3745 if (pdi->locdesc || pdi->has_type)
3746 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3747 built_actual_name,
3748 VAR_DOMAIN, LOC_STATIC,
3749 &objfile->global_psymbols,
3750 0, addr + baseaddr,
3751 cu->language, objfile);
3752 }
3753 else
3754 {
3755 /* Static Variable. Skip symbols without location descriptors. */
3756 if (pdi->locdesc == NULL)
3757 {
3758 if (built_actual_name)
3759 xfree (actual_name);
3760 return;
3761 }
3762 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3763 mst_file_data, objfile); */
3764 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3765 built_actual_name,
3766 VAR_DOMAIN, LOC_STATIC,
3767 &objfile->static_psymbols,
3768 0, addr + baseaddr,
3769 cu->language, objfile);
3770 }
3771 break;
3772 case DW_TAG_typedef:
3773 case DW_TAG_base_type:
3774 case DW_TAG_subrange_type:
3775 add_psymbol_to_list (actual_name, strlen (actual_name),
3776 built_actual_name,
3777 VAR_DOMAIN, LOC_TYPEDEF,
3778 &objfile->static_psymbols,
3779 0, (CORE_ADDR) 0, cu->language, objfile);
3780 break;
3781 case DW_TAG_namespace:
3782 add_psymbol_to_list (actual_name, strlen (actual_name),
3783 built_actual_name,
3784 VAR_DOMAIN, LOC_TYPEDEF,
3785 &objfile->global_psymbols,
3786 0, (CORE_ADDR) 0, cu->language, objfile);
3787 break;
3788 case DW_TAG_class_type:
3789 case DW_TAG_interface_type:
3790 case DW_TAG_structure_type:
3791 case DW_TAG_union_type:
3792 case DW_TAG_enumeration_type:
3793 /* Skip external references. The DWARF standard says in the section
3794 about "Structure, Union, and Class Type Entries": "An incomplete
3795 structure, union or class type is represented by a structure,
3796 union or class entry that does not have a byte size attribute
3797 and that has a DW_AT_declaration attribute." */
3798 if (!pdi->has_byte_size && pdi->is_declaration)
3799 {
3800 if (built_actual_name)
3801 xfree (actual_name);
3802 return;
3803 }
3804
3805 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3806 static vs. global. */
3807 add_psymbol_to_list (actual_name, strlen (actual_name),
3808 built_actual_name,
3809 STRUCT_DOMAIN, LOC_TYPEDEF,
3810 (cu->language == language_cplus
3811 || cu->language == language_java)
3812 ? &objfile->global_psymbols
3813 : &objfile->static_psymbols,
3814 0, (CORE_ADDR) 0, cu->language, objfile);
3815
3816 break;
3817 case DW_TAG_enumerator:
3818 add_psymbol_to_list (actual_name, strlen (actual_name),
3819 built_actual_name,
3820 VAR_DOMAIN, LOC_CONST,
3821 (cu->language == language_cplus
3822 || cu->language == language_java)
3823 ? &objfile->global_psymbols
3824 : &objfile->static_psymbols,
3825 0, (CORE_ADDR) 0, cu->language, objfile);
3826 break;
3827 default:
3828 break;
3829 }
3830
3831 if (built_actual_name)
3832 xfree (actual_name);
3833 }
3834
3835 /* Read a partial die corresponding to a namespace; also, add a symbol
3836 corresponding to that namespace to the symbol table. NAMESPACE is
3837 the name of the enclosing namespace. */
3838
3839 static void
3840 add_partial_namespace (struct partial_die_info *pdi,
3841 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3842 int need_pc, struct dwarf2_cu *cu)
3843 {
3844 /* Add a symbol for the namespace. */
3845
3846 add_partial_symbol (pdi, cu);
3847
3848 /* Now scan partial symbols in that namespace. */
3849
3850 if (pdi->has_children)
3851 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3852 }
3853
3854 /* Read a partial die corresponding to a Fortran module. */
3855
3856 static void
3857 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3858 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3859 {
3860 /* Now scan partial symbols in that module. */
3861
3862 if (pdi->has_children)
3863 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3864 }
3865
3866 /* Read a partial die corresponding to a subprogram and create a partial
3867 symbol for that subprogram. When the CU language allows it, this
3868 routine also defines a partial symbol for each nested subprogram
3869 that this subprogram contains.
3870
3871 DIE my also be a lexical block, in which case we simply search
3872 recursively for suprograms defined inside that lexical block.
3873 Again, this is only performed when the CU language allows this
3874 type of definitions. */
3875
3876 static void
3877 add_partial_subprogram (struct partial_die_info *pdi,
3878 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3879 int need_pc, struct dwarf2_cu *cu)
3880 {
3881 if (pdi->tag == DW_TAG_subprogram)
3882 {
3883 if (pdi->has_pc_info)
3884 {
3885 if (pdi->lowpc < *lowpc)
3886 *lowpc = pdi->lowpc;
3887 if (pdi->highpc > *highpc)
3888 *highpc = pdi->highpc;
3889 if (need_pc)
3890 {
3891 CORE_ADDR baseaddr;
3892 struct objfile *objfile = cu->objfile;
3893
3894 baseaddr = ANOFFSET (objfile->section_offsets,
3895 SECT_OFF_TEXT (objfile));
3896 addrmap_set_empty (objfile->psymtabs_addrmap,
3897 pdi->lowpc + baseaddr,
3898 pdi->highpc - 1 + baseaddr,
3899 cu->per_cu->v.psymtab);
3900 }
3901 if (!pdi->is_declaration)
3902 /* Ignore subprogram DIEs that do not have a name, they are
3903 illegal. Do not emit a complaint at this point, we will
3904 do so when we convert this psymtab into a symtab. */
3905 if (pdi->name)
3906 add_partial_symbol (pdi, cu);
3907 }
3908 }
3909
3910 if (! pdi->has_children)
3911 return;
3912
3913 if (cu->language == language_ada)
3914 {
3915 pdi = pdi->die_child;
3916 while (pdi != NULL)
3917 {
3918 fixup_partial_die (pdi, cu);
3919 if (pdi->tag == DW_TAG_subprogram
3920 || pdi->tag == DW_TAG_lexical_block)
3921 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3922 pdi = pdi->die_sibling;
3923 }
3924 }
3925 }
3926
3927 /* Read a partial die corresponding to an enumeration type. */
3928
3929 static void
3930 add_partial_enumeration (struct partial_die_info *enum_pdi,
3931 struct dwarf2_cu *cu)
3932 {
3933 struct partial_die_info *pdi;
3934
3935 if (enum_pdi->name != NULL)
3936 add_partial_symbol (enum_pdi, cu);
3937
3938 pdi = enum_pdi->die_child;
3939 while (pdi)
3940 {
3941 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3942 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3943 else
3944 add_partial_symbol (pdi, cu);
3945 pdi = pdi->die_sibling;
3946 }
3947 }
3948
3949 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3950 Return the corresponding abbrev, or NULL if the number is zero (indicating
3951 an empty DIE). In either case *BYTES_READ will be set to the length of
3952 the initial number. */
3953
3954 static struct abbrev_info *
3955 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3956 struct dwarf2_cu *cu)
3957 {
3958 bfd *abfd = cu->objfile->obfd;
3959 unsigned int abbrev_number;
3960 struct abbrev_info *abbrev;
3961
3962 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3963
3964 if (abbrev_number == 0)
3965 return NULL;
3966
3967 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3968 if (!abbrev)
3969 {
3970 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3971 bfd_get_filename (abfd));
3972 }
3973
3974 return abbrev;
3975 }
3976
3977 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3978 Returns a pointer to the end of a series of DIEs, terminated by an empty
3979 DIE. Any children of the skipped DIEs will also be skipped. */
3980
3981 static gdb_byte *
3982 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3983 {
3984 struct abbrev_info *abbrev;
3985 unsigned int bytes_read;
3986
3987 while (1)
3988 {
3989 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3990 if (abbrev == NULL)
3991 return info_ptr + bytes_read;
3992 else
3993 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
3994 }
3995 }
3996
3997 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3998 INFO_PTR should point just after the initial uleb128 of a DIE, and the
3999 abbrev corresponding to that skipped uleb128 should be passed in
4000 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4001 children. */
4002
4003 static gdb_byte *
4004 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4005 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4006 {
4007 unsigned int bytes_read;
4008 struct attribute attr;
4009 bfd *abfd = cu->objfile->obfd;
4010 unsigned int form, i;
4011
4012 for (i = 0; i < abbrev->num_attrs; i++)
4013 {
4014 /* The only abbrev we care about is DW_AT_sibling. */
4015 if (abbrev->attrs[i].name == DW_AT_sibling)
4016 {
4017 read_attribute (&attr, &abbrev->attrs[i],
4018 abfd, info_ptr, cu);
4019 if (attr.form == DW_FORM_ref_addr)
4020 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4021 else
4022 return buffer + dwarf2_get_ref_die_offset (&attr);
4023 }
4024
4025 /* If it isn't DW_AT_sibling, skip this attribute. */
4026 form = abbrev->attrs[i].form;
4027 skip_attribute:
4028 switch (form)
4029 {
4030 case DW_FORM_ref_addr:
4031 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4032 and later it is offset sized. */
4033 if (cu->header.version == 2)
4034 info_ptr += cu->header.addr_size;
4035 else
4036 info_ptr += cu->header.offset_size;
4037 break;
4038 case DW_FORM_addr:
4039 info_ptr += cu->header.addr_size;
4040 break;
4041 case DW_FORM_data1:
4042 case DW_FORM_ref1:
4043 case DW_FORM_flag:
4044 info_ptr += 1;
4045 break;
4046 case DW_FORM_flag_present:
4047 break;
4048 case DW_FORM_data2:
4049 case DW_FORM_ref2:
4050 info_ptr += 2;
4051 break;
4052 case DW_FORM_data4:
4053 case DW_FORM_ref4:
4054 info_ptr += 4;
4055 break;
4056 case DW_FORM_data8:
4057 case DW_FORM_ref8:
4058 case DW_FORM_sig8:
4059 info_ptr += 8;
4060 break;
4061 case DW_FORM_string:
4062 read_direct_string (abfd, info_ptr, &bytes_read);
4063 info_ptr += bytes_read;
4064 break;
4065 case DW_FORM_sec_offset:
4066 case DW_FORM_strp:
4067 info_ptr += cu->header.offset_size;
4068 break;
4069 case DW_FORM_exprloc:
4070 case DW_FORM_block:
4071 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4072 info_ptr += bytes_read;
4073 break;
4074 case DW_FORM_block1:
4075 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4076 break;
4077 case DW_FORM_block2:
4078 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4079 break;
4080 case DW_FORM_block4:
4081 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4082 break;
4083 case DW_FORM_sdata:
4084 case DW_FORM_udata:
4085 case DW_FORM_ref_udata:
4086 info_ptr = skip_leb128 (abfd, info_ptr);
4087 break;
4088 case DW_FORM_indirect:
4089 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4090 info_ptr += bytes_read;
4091 /* We need to continue parsing from here, so just go back to
4092 the top. */
4093 goto skip_attribute;
4094
4095 default:
4096 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4097 dwarf_form_name (form),
4098 bfd_get_filename (abfd));
4099 }
4100 }
4101
4102 if (abbrev->has_children)
4103 return skip_children (buffer, info_ptr, cu);
4104 else
4105 return info_ptr;
4106 }
4107
4108 /* Locate ORIG_PDI's sibling.
4109 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4110 in BUFFER. */
4111
4112 static gdb_byte *
4113 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4114 gdb_byte *buffer, gdb_byte *info_ptr,
4115 bfd *abfd, struct dwarf2_cu *cu)
4116 {
4117 /* Do we know the sibling already? */
4118
4119 if (orig_pdi->sibling)
4120 return orig_pdi->sibling;
4121
4122 /* Are there any children to deal with? */
4123
4124 if (!orig_pdi->has_children)
4125 return info_ptr;
4126
4127 /* Skip the children the long way. */
4128
4129 return skip_children (buffer, info_ptr, cu);
4130 }
4131
4132 /* Expand this partial symbol table into a full symbol table. */
4133
4134 static void
4135 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4136 {
4137 if (pst != NULL)
4138 {
4139 if (pst->readin)
4140 {
4141 warning (_("bug: psymtab for %s is already read in."), pst->filename);
4142 }
4143 else
4144 {
4145 if (info_verbose)
4146 {
4147 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4148 gdb_flush (gdb_stdout);
4149 }
4150
4151 /* Restore our global data. */
4152 dwarf2_per_objfile = objfile_data (pst->objfile,
4153 dwarf2_objfile_data_key);
4154
4155 /* If this psymtab is constructed from a debug-only objfile, the
4156 has_section_at_zero flag will not necessarily be correct. We
4157 can get the correct value for this flag by looking at the data
4158 associated with the (presumably stripped) associated objfile. */
4159 if (pst->objfile->separate_debug_objfile_backlink)
4160 {
4161 struct dwarf2_per_objfile *dpo_backlink
4162 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4163 dwarf2_objfile_data_key);
4164
4165 dwarf2_per_objfile->has_section_at_zero
4166 = dpo_backlink->has_section_at_zero;
4167 }
4168
4169 dwarf2_per_objfile->reading_partial_symbols = 0;
4170
4171 psymtab_to_symtab_1 (pst);
4172
4173 /* Finish up the debug error message. */
4174 if (info_verbose)
4175 printf_filtered (_("done.\n"));
4176 }
4177 }
4178 }
4179
4180 /* Add PER_CU to the queue. */
4181
4182 static void
4183 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4184 {
4185 struct dwarf2_queue_item *item;
4186
4187 per_cu->queued = 1;
4188 item = xmalloc (sizeof (*item));
4189 item->per_cu = per_cu;
4190 item->next = NULL;
4191
4192 if (dwarf2_queue == NULL)
4193 dwarf2_queue = item;
4194 else
4195 dwarf2_queue_tail->next = item;
4196
4197 dwarf2_queue_tail = item;
4198 }
4199
4200 /* Process the queue. */
4201
4202 static void
4203 process_queue (struct objfile *objfile)
4204 {
4205 struct dwarf2_queue_item *item, *next_item;
4206
4207 /* The queue starts out with one item, but following a DIE reference
4208 may load a new CU, adding it to the end of the queue. */
4209 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4210 {
4211 if (dwarf2_per_objfile->using_index
4212 ? !item->per_cu->v.quick->symtab
4213 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4214 process_full_comp_unit (item->per_cu);
4215
4216 item->per_cu->queued = 0;
4217 next_item = item->next;
4218 xfree (item);
4219 }
4220
4221 dwarf2_queue_tail = NULL;
4222 }
4223
4224 /* Free all allocated queue entries. This function only releases anything if
4225 an error was thrown; if the queue was processed then it would have been
4226 freed as we went along. */
4227
4228 static void
4229 dwarf2_release_queue (void *dummy)
4230 {
4231 struct dwarf2_queue_item *item, *last;
4232
4233 item = dwarf2_queue;
4234 while (item)
4235 {
4236 /* Anything still marked queued is likely to be in an
4237 inconsistent state, so discard it. */
4238 if (item->per_cu->queued)
4239 {
4240 if (item->per_cu->cu != NULL)
4241 free_one_cached_comp_unit (item->per_cu->cu);
4242 item->per_cu->queued = 0;
4243 }
4244
4245 last = item;
4246 item = item->next;
4247 xfree (last);
4248 }
4249
4250 dwarf2_queue = dwarf2_queue_tail = NULL;
4251 }
4252
4253 /* Read in full symbols for PST, and anything it depends on. */
4254
4255 static void
4256 psymtab_to_symtab_1 (struct partial_symtab *pst)
4257 {
4258 struct dwarf2_per_cu_data *per_cu;
4259 struct cleanup *back_to;
4260 int i;
4261
4262 for (i = 0; i < pst->number_of_dependencies; i++)
4263 if (!pst->dependencies[i]->readin)
4264 {
4265 /* Inform about additional files that need to be read in. */
4266 if (info_verbose)
4267 {
4268 /* FIXME: i18n: Need to make this a single string. */
4269 fputs_filtered (" ", gdb_stdout);
4270 wrap_here ("");
4271 fputs_filtered ("and ", gdb_stdout);
4272 wrap_here ("");
4273 printf_filtered ("%s...", pst->dependencies[i]->filename);
4274 wrap_here (""); /* Flush output */
4275 gdb_flush (gdb_stdout);
4276 }
4277 psymtab_to_symtab_1 (pst->dependencies[i]);
4278 }
4279
4280 per_cu = pst->read_symtab_private;
4281
4282 if (per_cu == NULL)
4283 {
4284 /* It's an include file, no symbols to read for it.
4285 Everything is in the parent symtab. */
4286 pst->readin = 1;
4287 return;
4288 }
4289
4290 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4291 }
4292
4293 /* Load the DIEs associated with PER_CU into memory. */
4294
4295 static void
4296 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4297 {
4298 bfd *abfd = objfile->obfd;
4299 struct dwarf2_cu *cu;
4300 unsigned int offset;
4301 gdb_byte *info_ptr, *beg_of_comp_unit;
4302 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4303 struct attribute *attr;
4304 int read_cu = 0;
4305
4306 gdb_assert (! per_cu->from_debug_types);
4307
4308 /* Set local variables from the partial symbol table info. */
4309 offset = per_cu->offset;
4310
4311 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4312 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4313 beg_of_comp_unit = info_ptr;
4314
4315 if (per_cu->cu == NULL)
4316 {
4317 cu = alloc_one_comp_unit (objfile);
4318
4319 read_cu = 1;
4320
4321 /* If an error occurs while loading, release our storage. */
4322 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4323
4324 /* Read in the comp_unit header. */
4325 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4326
4327 /* Complete the cu_header. */
4328 cu->header.offset = offset;
4329 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4330
4331 /* Read the abbrevs for this compilation unit. */
4332 dwarf2_read_abbrevs (abfd, cu);
4333 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4334
4335 /* Link this compilation unit into the compilation unit tree. */
4336 per_cu->cu = cu;
4337 cu->per_cu = per_cu;
4338
4339 /* Link this CU into read_in_chain. */
4340 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4341 dwarf2_per_objfile->read_in_chain = per_cu;
4342 }
4343 else
4344 {
4345 cu = per_cu->cu;
4346 info_ptr += cu->header.first_die_offset;
4347 }
4348
4349 cu->dies = read_comp_unit (info_ptr, cu);
4350
4351 /* We try not to read any attributes in this function, because not
4352 all objfiles needed for references have been loaded yet, and symbol
4353 table processing isn't initialized. But we have to set the CU language,
4354 or we won't be able to build types correctly. */
4355 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4356 if (attr)
4357 set_cu_language (DW_UNSND (attr), cu);
4358 else
4359 set_cu_language (language_minimal, cu);
4360
4361 /* Similarly, if we do not read the producer, we can not apply
4362 producer-specific interpretation. */
4363 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4364 if (attr)
4365 cu->producer = DW_STRING (attr);
4366
4367 if (read_cu)
4368 {
4369 do_cleanups (free_abbrevs_cleanup);
4370
4371 /* We've successfully allocated this compilation unit. Let our
4372 caller clean it up when finished with it. */
4373 discard_cleanups (free_cu_cleanup);
4374 }
4375 }
4376
4377 /* Add a DIE to the delayed physname list. */
4378
4379 static void
4380 add_to_method_list (struct type *type, int fnfield_index, int index,
4381 const char *name, struct die_info *die,
4382 struct dwarf2_cu *cu)
4383 {
4384 struct delayed_method_info mi;
4385 mi.type = type;
4386 mi.fnfield_index = fnfield_index;
4387 mi.index = index;
4388 mi.name = name;
4389 mi.die = die;
4390 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4391 }
4392
4393 /* A cleanup for freeing the delayed method list. */
4394
4395 static void
4396 free_delayed_list (void *ptr)
4397 {
4398 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4399 if (cu->method_list != NULL)
4400 {
4401 VEC_free (delayed_method_info, cu->method_list);
4402 cu->method_list = NULL;
4403 }
4404 }
4405
4406 /* Compute the physnames of any methods on the CU's method list.
4407
4408 The computation of method physnames is delayed in order to avoid the
4409 (bad) condition that one of the method's formal parameters is of an as yet
4410 incomplete type. */
4411
4412 static void
4413 compute_delayed_physnames (struct dwarf2_cu *cu)
4414 {
4415 int i;
4416 struct delayed_method_info *mi;
4417 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4418 {
4419 char *physname;
4420 struct fn_fieldlist *fn_flp
4421 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4422 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4423 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4424 }
4425 }
4426
4427 /* Generate full symbol information for PST and CU, whose DIEs have
4428 already been loaded into memory. */
4429
4430 static void
4431 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4432 {
4433 struct dwarf2_cu *cu = per_cu->cu;
4434 struct objfile *objfile = per_cu->objfile;
4435 CORE_ADDR lowpc, highpc;
4436 struct symtab *symtab;
4437 struct cleanup *back_to, *delayed_list_cleanup;
4438 CORE_ADDR baseaddr;
4439
4440 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4441
4442 buildsym_init ();
4443 back_to = make_cleanup (really_free_pendings, NULL);
4444 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4445
4446 cu->list_in_scope = &file_symbols;
4447
4448 dwarf2_find_base_address (cu->dies, cu);
4449
4450 /* Do line number decoding in read_file_scope () */
4451 process_die (cu->dies, cu);
4452
4453 /* Now that we have processed all the DIEs in the CU, all the types
4454 should be complete, and it should now be safe to compute all of the
4455 physnames. */
4456 compute_delayed_physnames (cu);
4457 do_cleanups (delayed_list_cleanup);
4458
4459 /* Some compilers don't define a DW_AT_high_pc attribute for the
4460 compilation unit. If the DW_AT_high_pc is missing, synthesize
4461 it, by scanning the DIE's below the compilation unit. */
4462 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4463
4464 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4465
4466 /* Set symtab language to language from DW_AT_language.
4467 If the compilation is from a C file generated by language preprocessors,
4468 do not set the language if it was already deduced by start_subfile. */
4469 if (symtab != NULL
4470 && !(cu->language == language_c && symtab->language != language_c))
4471 {
4472 symtab->language = cu->language;
4473 }
4474
4475 if (dwarf2_per_objfile->using_index)
4476 per_cu->v.quick->symtab = symtab;
4477 else
4478 {
4479 struct partial_symtab *pst = per_cu->v.psymtab;
4480 pst->symtab = symtab;
4481 pst->readin = 1;
4482 }
4483
4484 do_cleanups (back_to);
4485 }
4486
4487 /* Process a die and its children. */
4488
4489 static void
4490 process_die (struct die_info *die, struct dwarf2_cu *cu)
4491 {
4492 switch (die->tag)
4493 {
4494 case DW_TAG_padding:
4495 break;
4496 case DW_TAG_compile_unit:
4497 read_file_scope (die, cu);
4498 break;
4499 case DW_TAG_type_unit:
4500 read_type_unit_scope (die, cu);
4501 break;
4502 case DW_TAG_subprogram:
4503 case DW_TAG_inlined_subroutine:
4504 read_func_scope (die, cu);
4505 break;
4506 case DW_TAG_lexical_block:
4507 case DW_TAG_try_block:
4508 case DW_TAG_catch_block:
4509 read_lexical_block_scope (die, cu);
4510 break;
4511 case DW_TAG_class_type:
4512 case DW_TAG_interface_type:
4513 case DW_TAG_structure_type:
4514 case DW_TAG_union_type:
4515 process_structure_scope (die, cu);
4516 break;
4517 case DW_TAG_enumeration_type:
4518 process_enumeration_scope (die, cu);
4519 break;
4520
4521 /* These dies have a type, but processing them does not create
4522 a symbol or recurse to process the children. Therefore we can
4523 read them on-demand through read_type_die. */
4524 case DW_TAG_subroutine_type:
4525 case DW_TAG_set_type:
4526 case DW_TAG_array_type:
4527 case DW_TAG_pointer_type:
4528 case DW_TAG_ptr_to_member_type:
4529 case DW_TAG_reference_type:
4530 case DW_TAG_string_type:
4531 break;
4532
4533 case DW_TAG_base_type:
4534 case DW_TAG_subrange_type:
4535 case DW_TAG_typedef:
4536 /* Add a typedef symbol for the type definition, if it has a
4537 DW_AT_name. */
4538 new_symbol (die, read_type_die (die, cu), cu);
4539 break;
4540 case DW_TAG_common_block:
4541 read_common_block (die, cu);
4542 break;
4543 case DW_TAG_common_inclusion:
4544 break;
4545 case DW_TAG_namespace:
4546 processing_has_namespace_info = 1;
4547 read_namespace (die, cu);
4548 break;
4549 case DW_TAG_module:
4550 processing_has_namespace_info = 1;
4551 read_module (die, cu);
4552 break;
4553 case DW_TAG_imported_declaration:
4554 case DW_TAG_imported_module:
4555 processing_has_namespace_info = 1;
4556 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4557 || cu->language != language_fortran))
4558 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4559 dwarf_tag_name (die->tag));
4560 read_import_statement (die, cu);
4561 break;
4562 default:
4563 new_symbol (die, NULL, cu);
4564 break;
4565 }
4566 }
4567
4568 /* A helper function for dwarf2_compute_name which determines whether DIE
4569 needs to have the name of the scope prepended to the name listed in the
4570 die. */
4571
4572 static int
4573 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4574 {
4575 struct attribute *attr;
4576
4577 switch (die->tag)
4578 {
4579 case DW_TAG_namespace:
4580 case DW_TAG_typedef:
4581 case DW_TAG_class_type:
4582 case DW_TAG_interface_type:
4583 case DW_TAG_structure_type:
4584 case DW_TAG_union_type:
4585 case DW_TAG_enumeration_type:
4586 case DW_TAG_enumerator:
4587 case DW_TAG_subprogram:
4588 case DW_TAG_member:
4589 return 1;
4590
4591 case DW_TAG_variable:
4592 case DW_TAG_constant:
4593 /* We only need to prefix "globally" visible variables. These include
4594 any variable marked with DW_AT_external or any variable that
4595 lives in a namespace. [Variables in anonymous namespaces
4596 require prefixing, but they are not DW_AT_external.] */
4597
4598 if (dwarf2_attr (die, DW_AT_specification, cu))
4599 {
4600 struct dwarf2_cu *spec_cu = cu;
4601
4602 return die_needs_namespace (die_specification (die, &spec_cu),
4603 spec_cu);
4604 }
4605
4606 attr = dwarf2_attr (die, DW_AT_external, cu);
4607 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4608 && die->parent->tag != DW_TAG_module)
4609 return 0;
4610 /* A variable in a lexical block of some kind does not need a
4611 namespace, even though in C++ such variables may be external
4612 and have a mangled name. */
4613 if (die->parent->tag == DW_TAG_lexical_block
4614 || die->parent->tag == DW_TAG_try_block
4615 || die->parent->tag == DW_TAG_catch_block
4616 || die->parent->tag == DW_TAG_subprogram)
4617 return 0;
4618 return 1;
4619
4620 default:
4621 return 0;
4622 }
4623 }
4624
4625 /* Retrieve the last character from a mem_file. */
4626
4627 static void
4628 do_ui_file_peek_last (void *object, const char *buffer, long length)
4629 {
4630 char *last_char_p = (char *) object;
4631
4632 if (length > 0)
4633 *last_char_p = buffer[length - 1];
4634 }
4635
4636 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4637 compute the physname for the object, which include a method's
4638 formal parameters (C++/Java) and return type (Java).
4639
4640 For Ada, return the DIE's linkage name rather than the fully qualified
4641 name. PHYSNAME is ignored..
4642
4643 The result is allocated on the objfile_obstack and canonicalized. */
4644
4645 static const char *
4646 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4647 int physname)
4648 {
4649 if (name == NULL)
4650 name = dwarf2_name (die, cu);
4651
4652 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4653 compute it by typename_concat inside GDB. */
4654 if (cu->language == language_ada
4655 || (cu->language == language_fortran && physname))
4656 {
4657 /* For Ada unit, we prefer the linkage name over the name, as
4658 the former contains the exported name, which the user expects
4659 to be able to reference. Ideally, we want the user to be able
4660 to reference this entity using either natural or linkage name,
4661 but we haven't started looking at this enhancement yet. */
4662 struct attribute *attr;
4663
4664 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4665 if (attr == NULL)
4666 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4667 if (attr && DW_STRING (attr))
4668 return DW_STRING (attr);
4669 }
4670
4671 /* These are the only languages we know how to qualify names in. */
4672 if (name != NULL
4673 && (cu->language == language_cplus || cu->language == language_java
4674 || cu->language == language_fortran))
4675 {
4676 if (die_needs_namespace (die, cu))
4677 {
4678 long length;
4679 char *prefix;
4680 struct ui_file *buf;
4681
4682 prefix = determine_prefix (die, cu);
4683 buf = mem_fileopen ();
4684 if (*prefix != '\0')
4685 {
4686 char *prefixed_name = typename_concat (NULL, prefix, name,
4687 physname, cu);
4688
4689 fputs_unfiltered (prefixed_name, buf);
4690 xfree (prefixed_name);
4691 }
4692 else
4693 fputs_unfiltered (name ? name : "", buf);
4694
4695 /* Template parameters may be specified in the DIE's DW_AT_name, or
4696 as children with DW_TAG_template_type_param or
4697 DW_TAG_value_type_param. If the latter, add them to the name
4698 here. If the name already has template parameters, then
4699 skip this step; some versions of GCC emit both, and
4700 it is more efficient to use the pre-computed name.
4701
4702 Something to keep in mind about this process: it is very
4703 unlikely, or in some cases downright impossible, to produce
4704 something that will match the mangled name of a function.
4705 If the definition of the function has the same debug info,
4706 we should be able to match up with it anyway. But fallbacks
4707 using the minimal symbol, for instance to find a method
4708 implemented in a stripped copy of libstdc++, will not work.
4709 If we do not have debug info for the definition, we will have to
4710 match them up some other way.
4711
4712 When we do name matching there is a related problem with function
4713 templates; two instantiated function templates are allowed to
4714 differ only by their return types, which we do not add here. */
4715
4716 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4717 {
4718 struct attribute *attr;
4719 struct die_info *child;
4720 int first = 1;
4721
4722 die->building_fullname = 1;
4723
4724 for (child = die->child; child != NULL; child = child->sibling)
4725 {
4726 struct type *type;
4727 long value;
4728 gdb_byte *bytes;
4729 struct dwarf2_locexpr_baton *baton;
4730 struct value *v;
4731
4732 if (child->tag != DW_TAG_template_type_param
4733 && child->tag != DW_TAG_template_value_param)
4734 continue;
4735
4736 if (first)
4737 {
4738 fputs_unfiltered ("<", buf);
4739 first = 0;
4740 }
4741 else
4742 fputs_unfiltered (", ", buf);
4743
4744 attr = dwarf2_attr (child, DW_AT_type, cu);
4745 if (attr == NULL)
4746 {
4747 complaint (&symfile_complaints,
4748 _("template parameter missing DW_AT_type"));
4749 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4750 continue;
4751 }
4752 type = die_type (child, cu);
4753
4754 if (child->tag == DW_TAG_template_type_param)
4755 {
4756 c_print_type (type, "", buf, -1, 0);
4757 continue;
4758 }
4759
4760 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4761 if (attr == NULL)
4762 {
4763 complaint (&symfile_complaints,
4764 _("template parameter missing DW_AT_const_value"));
4765 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4766 continue;
4767 }
4768
4769 dwarf2_const_value_attr (attr, type, name,
4770 &cu->comp_unit_obstack, cu,
4771 &value, &bytes, &baton);
4772
4773 if (TYPE_NOSIGN (type))
4774 /* GDB prints characters as NUMBER 'CHAR'. If that's
4775 changed, this can use value_print instead. */
4776 c_printchar (value, type, buf);
4777 else
4778 {
4779 struct value_print_options opts;
4780
4781 if (baton != NULL)
4782 v = dwarf2_evaluate_loc_desc (type, NULL,
4783 baton->data,
4784 baton->size,
4785 baton->per_cu);
4786 else if (bytes != NULL)
4787 {
4788 v = allocate_value (type);
4789 memcpy (value_contents_writeable (v), bytes,
4790 TYPE_LENGTH (type));
4791 }
4792 else
4793 v = value_from_longest (type, value);
4794
4795 /* Specify decimal so that we do not depend on the radix. */
4796 get_formatted_print_options (&opts, 'd');
4797 opts.raw = 1;
4798 value_print (v, buf, &opts);
4799 release_value (v);
4800 value_free (v);
4801 }
4802 }
4803
4804 die->building_fullname = 0;
4805
4806 if (!first)
4807 {
4808 /* Close the argument list, with a space if necessary
4809 (nested templates). */
4810 char last_char = '\0';
4811 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4812 if (last_char == '>')
4813 fputs_unfiltered (" >", buf);
4814 else
4815 fputs_unfiltered (">", buf);
4816 }
4817 }
4818
4819 /* For Java and C++ methods, append formal parameter type
4820 information, if PHYSNAME. */
4821
4822 if (physname && die->tag == DW_TAG_subprogram
4823 && (cu->language == language_cplus
4824 || cu->language == language_java))
4825 {
4826 struct type *type = read_type_die (die, cu);
4827
4828 c_type_print_args (type, buf, 0, cu->language);
4829
4830 if (cu->language == language_java)
4831 {
4832 /* For java, we must append the return type to method
4833 names. */
4834 if (die->tag == DW_TAG_subprogram)
4835 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4836 0, 0);
4837 }
4838 else if (cu->language == language_cplus)
4839 {
4840 /* Assume that an artificial first parameter is
4841 "this", but do not crash if it is not. RealView
4842 marks unnamed (and thus unused) parameters as
4843 artificial; there is no way to differentiate
4844 the two cases. */
4845 if (TYPE_NFIELDS (type) > 0
4846 && TYPE_FIELD_ARTIFICIAL (type, 0)
4847 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
4848 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4849 fputs_unfiltered (" const", buf);
4850 }
4851 }
4852
4853 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4854 &length);
4855 ui_file_delete (buf);
4856
4857 if (cu->language == language_cplus)
4858 {
4859 char *cname
4860 = dwarf2_canonicalize_name (name, cu,
4861 &cu->objfile->objfile_obstack);
4862
4863 if (cname != NULL)
4864 name = cname;
4865 }
4866 }
4867 }
4868
4869 return name;
4870 }
4871
4872 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4873 If scope qualifiers are appropriate they will be added. The result
4874 will be allocated on the objfile_obstack, or NULL if the DIE does
4875 not have a name. NAME may either be from a previous call to
4876 dwarf2_name or NULL.
4877
4878 The output string will be canonicalized (if C++/Java). */
4879
4880 static const char *
4881 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4882 {
4883 return dwarf2_compute_name (name, die, cu, 0);
4884 }
4885
4886 /* Construct a physname for the given DIE in CU. NAME may either be
4887 from a previous call to dwarf2_name or NULL. The result will be
4888 allocated on the objfile_objstack or NULL if the DIE does not have a
4889 name.
4890
4891 The output string will be canonicalized (if C++/Java). */
4892
4893 static const char *
4894 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4895 {
4896 return dwarf2_compute_name (name, die, cu, 1);
4897 }
4898
4899 /* Read the import statement specified by the given die and record it. */
4900
4901 static void
4902 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4903 {
4904 struct attribute *import_attr;
4905 struct die_info *imported_die;
4906 struct dwarf2_cu *imported_cu;
4907 const char *imported_name;
4908 const char *imported_name_prefix;
4909 const char *canonical_name;
4910 const char *import_alias;
4911 const char *imported_declaration = NULL;
4912 const char *import_prefix;
4913
4914 char *temp;
4915
4916 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4917 if (import_attr == NULL)
4918 {
4919 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4920 dwarf_tag_name (die->tag));
4921 return;
4922 }
4923
4924 imported_cu = cu;
4925 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4926 imported_name = dwarf2_name (imported_die, imported_cu);
4927 if (imported_name == NULL)
4928 {
4929 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4930
4931 The import in the following code:
4932 namespace A
4933 {
4934 typedef int B;
4935 }
4936
4937 int main ()
4938 {
4939 using A::B;
4940 B b;
4941 return b;
4942 }
4943
4944 ...
4945 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4946 <52> DW_AT_decl_file : 1
4947 <53> DW_AT_decl_line : 6
4948 <54> DW_AT_import : <0x75>
4949 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4950 <59> DW_AT_name : B
4951 <5b> DW_AT_decl_file : 1
4952 <5c> DW_AT_decl_line : 2
4953 <5d> DW_AT_type : <0x6e>
4954 ...
4955 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4956 <76> DW_AT_byte_size : 4
4957 <77> DW_AT_encoding : 5 (signed)
4958
4959 imports the wrong die ( 0x75 instead of 0x58 ).
4960 This case will be ignored until the gcc bug is fixed. */
4961 return;
4962 }
4963
4964 /* Figure out the local name after import. */
4965 import_alias = dwarf2_name (die, cu);
4966
4967 /* Figure out where the statement is being imported to. */
4968 import_prefix = determine_prefix (die, cu);
4969
4970 /* Figure out what the scope of the imported die is and prepend it
4971 to the name of the imported die. */
4972 imported_name_prefix = determine_prefix (imported_die, imported_cu);
4973
4974 if (imported_die->tag != DW_TAG_namespace
4975 && imported_die->tag != DW_TAG_module)
4976 {
4977 imported_declaration = imported_name;
4978 canonical_name = imported_name_prefix;
4979 }
4980 else if (strlen (imported_name_prefix) > 0)
4981 {
4982 temp = alloca (strlen (imported_name_prefix)
4983 + 2 + strlen (imported_name) + 1);
4984 strcpy (temp, imported_name_prefix);
4985 strcat (temp, "::");
4986 strcat (temp, imported_name);
4987 canonical_name = temp;
4988 }
4989 else
4990 canonical_name = imported_name;
4991
4992 cp_add_using_directive (import_prefix,
4993 canonical_name,
4994 import_alias,
4995 imported_declaration,
4996 &cu->objfile->objfile_obstack);
4997 }
4998
4999 static void
5000 initialize_cu_func_list (struct dwarf2_cu *cu)
5001 {
5002 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5003 }
5004
5005 static void
5006 free_cu_line_header (void *arg)
5007 {
5008 struct dwarf2_cu *cu = arg;
5009
5010 free_line_header (cu->line_header);
5011 cu->line_header = NULL;
5012 }
5013
5014 static void
5015 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5016 char **name, char **comp_dir)
5017 {
5018 struct attribute *attr;
5019
5020 *name = NULL;
5021 *comp_dir = NULL;
5022
5023 /* Find the filename. Do not use dwarf2_name here, since the filename
5024 is not a source language identifier. */
5025 attr = dwarf2_attr (die, DW_AT_name, cu);
5026 if (attr)
5027 {
5028 *name = DW_STRING (attr);
5029 }
5030
5031 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5032 if (attr)
5033 *comp_dir = DW_STRING (attr);
5034 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5035 {
5036 *comp_dir = ldirname (*name);
5037 if (*comp_dir != NULL)
5038 make_cleanup (xfree, *comp_dir);
5039 }
5040 if (*comp_dir != NULL)
5041 {
5042 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5043 directory, get rid of it. */
5044 char *cp = strchr (*comp_dir, ':');
5045
5046 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5047 *comp_dir = cp + 1;
5048 }
5049
5050 if (*name == NULL)
5051 *name = "<unknown>";
5052 }
5053
5054 static void
5055 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5056 {
5057 struct objfile *objfile = cu->objfile;
5058 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5059 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5060 CORE_ADDR highpc = ((CORE_ADDR) 0);
5061 struct attribute *attr;
5062 char *name = NULL;
5063 char *comp_dir = NULL;
5064 struct die_info *child_die;
5065 bfd *abfd = objfile->obfd;
5066 struct line_header *line_header = 0;
5067 CORE_ADDR baseaddr;
5068
5069 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5070
5071 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5072
5073 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5074 from finish_block. */
5075 if (lowpc == ((CORE_ADDR) -1))
5076 lowpc = highpc;
5077 lowpc += baseaddr;
5078 highpc += baseaddr;
5079
5080 find_file_and_directory (die, cu, &name, &comp_dir);
5081
5082 attr = dwarf2_attr (die, DW_AT_language, cu);
5083 if (attr)
5084 {
5085 set_cu_language (DW_UNSND (attr), cu);
5086 }
5087
5088 attr = dwarf2_attr (die, DW_AT_producer, cu);
5089 if (attr)
5090 cu->producer = DW_STRING (attr);
5091
5092 /* We assume that we're processing GCC output. */
5093 processing_gcc_compilation = 2;
5094
5095 processing_has_namespace_info = 0;
5096
5097 start_symtab (name, comp_dir, lowpc);
5098 record_debugformat ("DWARF 2");
5099 record_producer (cu->producer);
5100
5101 initialize_cu_func_list (cu);
5102
5103 /* Decode line number information if present. We do this before
5104 processing child DIEs, so that the line header table is available
5105 for DW_AT_decl_file. */
5106 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5107 if (attr)
5108 {
5109 unsigned int line_offset = DW_UNSND (attr);
5110 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5111 if (line_header)
5112 {
5113 cu->line_header = line_header;
5114 make_cleanup (free_cu_line_header, cu);
5115 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5116 }
5117 }
5118
5119 /* Process all dies in compilation unit. */
5120 if (die->child != NULL)
5121 {
5122 child_die = die->child;
5123 while (child_die && child_die->tag)
5124 {
5125 process_die (child_die, cu);
5126 child_die = sibling_die (child_die);
5127 }
5128 }
5129
5130 /* Decode macro information, if present. Dwarf 2 macro information
5131 refers to information in the line number info statement program
5132 header, so we can only read it if we've read the header
5133 successfully. */
5134 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5135 if (attr && line_header)
5136 {
5137 unsigned int macro_offset = DW_UNSND (attr);
5138
5139 dwarf_decode_macros (line_header, macro_offset,
5140 comp_dir, abfd, cu);
5141 }
5142 do_cleanups (back_to);
5143 }
5144
5145 /* For TUs we want to skip the first top level sibling if it's not the
5146 actual type being defined by this TU. In this case the first top
5147 level sibling is there to provide context only. */
5148
5149 static void
5150 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5151 {
5152 struct objfile *objfile = cu->objfile;
5153 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5154 CORE_ADDR lowpc;
5155 struct attribute *attr;
5156 char *name = NULL;
5157 char *comp_dir = NULL;
5158 struct die_info *child_die;
5159 bfd *abfd = objfile->obfd;
5160
5161 /* start_symtab needs a low pc, but we don't really have one.
5162 Do what read_file_scope would do in the absence of such info. */
5163 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5164
5165 /* Find the filename. Do not use dwarf2_name here, since the filename
5166 is not a source language identifier. */
5167 attr = dwarf2_attr (die, DW_AT_name, cu);
5168 if (attr)
5169 name = DW_STRING (attr);
5170
5171 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5172 if (attr)
5173 comp_dir = DW_STRING (attr);
5174 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5175 {
5176 comp_dir = ldirname (name);
5177 if (comp_dir != NULL)
5178 make_cleanup (xfree, comp_dir);
5179 }
5180
5181 if (name == NULL)
5182 name = "<unknown>";
5183
5184 attr = dwarf2_attr (die, DW_AT_language, cu);
5185 if (attr)
5186 set_cu_language (DW_UNSND (attr), cu);
5187
5188 /* This isn't technically needed today. It is done for symmetry
5189 with read_file_scope. */
5190 attr = dwarf2_attr (die, DW_AT_producer, cu);
5191 if (attr)
5192 cu->producer = DW_STRING (attr);
5193
5194 /* We assume that we're processing GCC output. */
5195 processing_gcc_compilation = 2;
5196
5197 processing_has_namespace_info = 0;
5198
5199 start_symtab (name, comp_dir, lowpc);
5200 record_debugformat ("DWARF 2");
5201 record_producer (cu->producer);
5202
5203 /* Process the dies in the type unit. */
5204 if (die->child == NULL)
5205 {
5206 dump_die_for_error (die);
5207 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5208 bfd_get_filename (abfd));
5209 }
5210
5211 child_die = die->child;
5212
5213 while (child_die && child_die->tag)
5214 {
5215 process_die (child_die, cu);
5216
5217 child_die = sibling_die (child_die);
5218 }
5219
5220 do_cleanups (back_to);
5221 }
5222
5223 static void
5224 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5225 struct dwarf2_cu *cu)
5226 {
5227 struct function_range *thisfn;
5228
5229 thisfn = (struct function_range *)
5230 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5231 thisfn->name = name;
5232 thisfn->lowpc = lowpc;
5233 thisfn->highpc = highpc;
5234 thisfn->seen_line = 0;
5235 thisfn->next = NULL;
5236
5237 if (cu->last_fn == NULL)
5238 cu->first_fn = thisfn;
5239 else
5240 cu->last_fn->next = thisfn;
5241
5242 cu->last_fn = thisfn;
5243 }
5244
5245 /* qsort helper for inherit_abstract_dies. */
5246
5247 static int
5248 unsigned_int_compar (const void *ap, const void *bp)
5249 {
5250 unsigned int a = *(unsigned int *) ap;
5251 unsigned int b = *(unsigned int *) bp;
5252
5253 return (a > b) - (b > a);
5254 }
5255
5256 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5257 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5258 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5259
5260 static void
5261 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5262 {
5263 struct die_info *child_die;
5264 unsigned die_children_count;
5265 /* CU offsets which were referenced by children of the current DIE. */
5266 unsigned *offsets;
5267 unsigned *offsets_end, *offsetp;
5268 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5269 struct die_info *origin_die;
5270 /* Iterator of the ORIGIN_DIE children. */
5271 struct die_info *origin_child_die;
5272 struct cleanup *cleanups;
5273 struct attribute *attr;
5274 struct dwarf2_cu *origin_cu;
5275 struct pending **origin_previous_list_in_scope;
5276
5277 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5278 if (!attr)
5279 return;
5280
5281 /* Note that following die references may follow to a die in a
5282 different cu. */
5283
5284 origin_cu = cu;
5285 origin_die = follow_die_ref (die, attr, &origin_cu);
5286
5287 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5288 symbols in. */
5289 origin_previous_list_in_scope = origin_cu->list_in_scope;
5290 origin_cu->list_in_scope = cu->list_in_scope;
5291
5292 if (die->tag != origin_die->tag
5293 && !(die->tag == DW_TAG_inlined_subroutine
5294 && origin_die->tag == DW_TAG_subprogram))
5295 complaint (&symfile_complaints,
5296 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5297 die->offset, origin_die->offset);
5298
5299 child_die = die->child;
5300 die_children_count = 0;
5301 while (child_die && child_die->tag)
5302 {
5303 child_die = sibling_die (child_die);
5304 die_children_count++;
5305 }
5306 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5307 cleanups = make_cleanup (xfree, offsets);
5308
5309 offsets_end = offsets;
5310 child_die = die->child;
5311 while (child_die && child_die->tag)
5312 {
5313 /* For each CHILD_DIE, find the corresponding child of
5314 ORIGIN_DIE. If there is more than one layer of
5315 DW_AT_abstract_origin, follow them all; there shouldn't be,
5316 but GCC versions at least through 4.4 generate this (GCC PR
5317 40573). */
5318 struct die_info *child_origin_die = child_die;
5319 struct dwarf2_cu *child_origin_cu = cu;
5320
5321 while (1)
5322 {
5323 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5324 child_origin_cu);
5325 if (attr == NULL)
5326 break;
5327 child_origin_die = follow_die_ref (child_origin_die, attr,
5328 &child_origin_cu);
5329 }
5330
5331 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5332 counterpart may exist. */
5333 if (child_origin_die != child_die)
5334 {
5335 if (child_die->tag != child_origin_die->tag
5336 && !(child_die->tag == DW_TAG_inlined_subroutine
5337 && child_origin_die->tag == DW_TAG_subprogram))
5338 complaint (&symfile_complaints,
5339 _("Child DIE 0x%x and its abstract origin 0x%x have "
5340 "different tags"), child_die->offset,
5341 child_origin_die->offset);
5342 if (child_origin_die->parent != origin_die)
5343 complaint (&symfile_complaints,
5344 _("Child DIE 0x%x and its abstract origin 0x%x have "
5345 "different parents"), child_die->offset,
5346 child_origin_die->offset);
5347 else
5348 *offsets_end++ = child_origin_die->offset;
5349 }
5350 child_die = sibling_die (child_die);
5351 }
5352 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5353 unsigned_int_compar);
5354 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5355 if (offsetp[-1] == *offsetp)
5356 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5357 "to DIE 0x%x as their abstract origin"),
5358 die->offset, *offsetp);
5359
5360 offsetp = offsets;
5361 origin_child_die = origin_die->child;
5362 while (origin_child_die && origin_child_die->tag)
5363 {
5364 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5365 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5366 offsetp++;
5367 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5368 {
5369 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5370 process_die (origin_child_die, origin_cu);
5371 }
5372 origin_child_die = sibling_die (origin_child_die);
5373 }
5374 origin_cu->list_in_scope = origin_previous_list_in_scope;
5375
5376 do_cleanups (cleanups);
5377 }
5378
5379 static void
5380 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5381 {
5382 struct objfile *objfile = cu->objfile;
5383 struct context_stack *new;
5384 CORE_ADDR lowpc;
5385 CORE_ADDR highpc;
5386 struct die_info *child_die;
5387 struct attribute *attr, *call_line, *call_file;
5388 char *name;
5389 CORE_ADDR baseaddr;
5390 struct block *block;
5391 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5392 VEC (symbolp) *template_args = NULL;
5393 struct template_symbol *templ_func = NULL;
5394
5395 if (inlined_func)
5396 {
5397 /* If we do not have call site information, we can't show the
5398 caller of this inlined function. That's too confusing, so
5399 only use the scope for local variables. */
5400 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5401 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5402 if (call_line == NULL || call_file == NULL)
5403 {
5404 read_lexical_block_scope (die, cu);
5405 return;
5406 }
5407 }
5408
5409 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5410
5411 name = dwarf2_name (die, cu);
5412
5413 /* Ignore functions with missing or empty names. These are actually
5414 illegal according to the DWARF standard. */
5415 if (name == NULL)
5416 {
5417 complaint (&symfile_complaints,
5418 _("missing name for subprogram DIE at %d"), die->offset);
5419 return;
5420 }
5421
5422 /* Ignore functions with missing or invalid low and high pc attributes. */
5423 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5424 {
5425 attr = dwarf2_attr (die, DW_AT_external, cu);
5426 if (!attr || !DW_UNSND (attr))
5427 complaint (&symfile_complaints,
5428 _("cannot get low and high bounds for subprogram DIE at %d"),
5429 die->offset);
5430 return;
5431 }
5432
5433 lowpc += baseaddr;
5434 highpc += baseaddr;
5435
5436 /* Record the function range for dwarf_decode_lines. */
5437 add_to_cu_func_list (name, lowpc, highpc, cu);
5438
5439 /* If we have any template arguments, then we must allocate a
5440 different sort of symbol. */
5441 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5442 {
5443 if (child_die->tag == DW_TAG_template_type_param
5444 || child_die->tag == DW_TAG_template_value_param)
5445 {
5446 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5447 struct template_symbol);
5448 templ_func->base.is_cplus_template_function = 1;
5449 break;
5450 }
5451 }
5452
5453 new = push_context (0, lowpc);
5454 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5455 (struct symbol *) templ_func);
5456
5457 /* If there is a location expression for DW_AT_frame_base, record
5458 it. */
5459 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5460 if (attr)
5461 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5462 expression is being recorded directly in the function's symbol
5463 and not in a separate frame-base object. I guess this hack is
5464 to avoid adding some sort of frame-base adjunct/annex to the
5465 function's symbol :-(. The problem with doing this is that it
5466 results in a function symbol with a location expression that
5467 has nothing to do with the location of the function, ouch! The
5468 relationship should be: a function's symbol has-a frame base; a
5469 frame-base has-a location expression. */
5470 dwarf2_symbol_mark_computed (attr, new->name, cu);
5471
5472 cu->list_in_scope = &local_symbols;
5473
5474 if (die->child != NULL)
5475 {
5476 child_die = die->child;
5477 while (child_die && child_die->tag)
5478 {
5479 if (child_die->tag == DW_TAG_template_type_param
5480 || child_die->tag == DW_TAG_template_value_param)
5481 {
5482 struct symbol *arg = new_symbol (child_die, NULL, cu);
5483
5484 if (arg != NULL)
5485 VEC_safe_push (symbolp, template_args, arg);
5486 }
5487 else
5488 process_die (child_die, cu);
5489 child_die = sibling_die (child_die);
5490 }
5491 }
5492
5493 inherit_abstract_dies (die, cu);
5494
5495 /* If we have a DW_AT_specification, we might need to import using
5496 directives from the context of the specification DIE. See the
5497 comment in determine_prefix. */
5498 if (cu->language == language_cplus
5499 && dwarf2_attr (die, DW_AT_specification, cu))
5500 {
5501 struct dwarf2_cu *spec_cu = cu;
5502 struct die_info *spec_die = die_specification (die, &spec_cu);
5503
5504 while (spec_die)
5505 {
5506 child_die = spec_die->child;
5507 while (child_die && child_die->tag)
5508 {
5509 if (child_die->tag == DW_TAG_imported_module)
5510 process_die (child_die, spec_cu);
5511 child_die = sibling_die (child_die);
5512 }
5513
5514 /* In some cases, GCC generates specification DIEs that
5515 themselves contain DW_AT_specification attributes. */
5516 spec_die = die_specification (spec_die, &spec_cu);
5517 }
5518 }
5519
5520 new = pop_context ();
5521 /* Make a block for the local symbols within. */
5522 block = finish_block (new->name, &local_symbols, new->old_blocks,
5523 lowpc, highpc, objfile);
5524
5525 /* For C++, set the block's scope. */
5526 if (cu->language == language_cplus || cu->language == language_fortran)
5527 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5528 determine_prefix (die, cu),
5529 processing_has_namespace_info);
5530
5531 /* If we have address ranges, record them. */
5532 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5533
5534 /* Attach template arguments to function. */
5535 if (! VEC_empty (symbolp, template_args))
5536 {
5537 gdb_assert (templ_func != NULL);
5538
5539 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5540 templ_func->template_arguments
5541 = obstack_alloc (&objfile->objfile_obstack,
5542 (templ_func->n_template_arguments
5543 * sizeof (struct symbol *)));
5544 memcpy (templ_func->template_arguments,
5545 VEC_address (symbolp, template_args),
5546 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5547 VEC_free (symbolp, template_args);
5548 }
5549
5550 /* In C++, we can have functions nested inside functions (e.g., when
5551 a function declares a class that has methods). This means that
5552 when we finish processing a function scope, we may need to go
5553 back to building a containing block's symbol lists. */
5554 local_symbols = new->locals;
5555 param_symbols = new->params;
5556 using_directives = new->using_directives;
5557
5558 /* If we've finished processing a top-level function, subsequent
5559 symbols go in the file symbol list. */
5560 if (outermost_context_p ())
5561 cu->list_in_scope = &file_symbols;
5562 }
5563
5564 /* Process all the DIES contained within a lexical block scope. Start
5565 a new scope, process the dies, and then close the scope. */
5566
5567 static void
5568 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5569 {
5570 struct objfile *objfile = cu->objfile;
5571 struct context_stack *new;
5572 CORE_ADDR lowpc, highpc;
5573 struct die_info *child_die;
5574 CORE_ADDR baseaddr;
5575
5576 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5577
5578 /* Ignore blocks with missing or invalid low and high pc attributes. */
5579 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5580 as multiple lexical blocks? Handling children in a sane way would
5581 be nasty. Might be easier to properly extend generic blocks to
5582 describe ranges. */
5583 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5584 return;
5585 lowpc += baseaddr;
5586 highpc += baseaddr;
5587
5588 push_context (0, lowpc);
5589 if (die->child != NULL)
5590 {
5591 child_die = die->child;
5592 while (child_die && child_die->tag)
5593 {
5594 process_die (child_die, cu);
5595 child_die = sibling_die (child_die);
5596 }
5597 }
5598 new = pop_context ();
5599
5600 if (local_symbols != NULL || using_directives != NULL)
5601 {
5602 struct block *block
5603 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5604 highpc, objfile);
5605
5606 /* Note that recording ranges after traversing children, as we
5607 do here, means that recording a parent's ranges entails
5608 walking across all its children's ranges as they appear in
5609 the address map, which is quadratic behavior.
5610
5611 It would be nicer to record the parent's ranges before
5612 traversing its children, simply overriding whatever you find
5613 there. But since we don't even decide whether to create a
5614 block until after we've traversed its children, that's hard
5615 to do. */
5616 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5617 }
5618 local_symbols = new->locals;
5619 using_directives = new->using_directives;
5620 }
5621
5622 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5623 Return 1 if the attributes are present and valid, otherwise, return 0.
5624 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
5625
5626 static int
5627 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5628 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5629 struct partial_symtab *ranges_pst)
5630 {
5631 struct objfile *objfile = cu->objfile;
5632 struct comp_unit_head *cu_header = &cu->header;
5633 bfd *obfd = objfile->obfd;
5634 unsigned int addr_size = cu_header->addr_size;
5635 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5636 /* Base address selection entry. */
5637 CORE_ADDR base;
5638 int found_base;
5639 unsigned int dummy;
5640 gdb_byte *buffer;
5641 CORE_ADDR marker;
5642 int low_set;
5643 CORE_ADDR low = 0;
5644 CORE_ADDR high = 0;
5645 CORE_ADDR baseaddr;
5646
5647 found_base = cu->base_known;
5648 base = cu->base_address;
5649
5650 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5651 if (offset >= dwarf2_per_objfile->ranges.size)
5652 {
5653 complaint (&symfile_complaints,
5654 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5655 offset);
5656 return 0;
5657 }
5658 buffer = dwarf2_per_objfile->ranges.buffer + offset;
5659
5660 /* Read in the largest possible address. */
5661 marker = read_address (obfd, buffer, cu, &dummy);
5662 if ((marker & mask) == mask)
5663 {
5664 /* If we found the largest possible address, then
5665 read the base address. */
5666 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5667 buffer += 2 * addr_size;
5668 offset += 2 * addr_size;
5669 found_base = 1;
5670 }
5671
5672 low_set = 0;
5673
5674 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5675
5676 while (1)
5677 {
5678 CORE_ADDR range_beginning, range_end;
5679
5680 range_beginning = read_address (obfd, buffer, cu, &dummy);
5681 buffer += addr_size;
5682 range_end = read_address (obfd, buffer, cu, &dummy);
5683 buffer += addr_size;
5684 offset += 2 * addr_size;
5685
5686 /* An end of list marker is a pair of zero addresses. */
5687 if (range_beginning == 0 && range_end == 0)
5688 /* Found the end of list entry. */
5689 break;
5690
5691 /* Each base address selection entry is a pair of 2 values.
5692 The first is the largest possible address, the second is
5693 the base address. Check for a base address here. */
5694 if ((range_beginning & mask) == mask)
5695 {
5696 /* If we found the largest possible address, then
5697 read the base address. */
5698 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5699 found_base = 1;
5700 continue;
5701 }
5702
5703 if (!found_base)
5704 {
5705 /* We have no valid base address for the ranges
5706 data. */
5707 complaint (&symfile_complaints,
5708 _("Invalid .debug_ranges data (no base address)"));
5709 return 0;
5710 }
5711
5712 range_beginning += base;
5713 range_end += base;
5714
5715 if (ranges_pst != NULL && range_beginning < range_end)
5716 addrmap_set_empty (objfile->psymtabs_addrmap,
5717 range_beginning + baseaddr, range_end - 1 + baseaddr,
5718 ranges_pst);
5719
5720 /* FIXME: This is recording everything as a low-high
5721 segment of consecutive addresses. We should have a
5722 data structure for discontiguous block ranges
5723 instead. */
5724 if (! low_set)
5725 {
5726 low = range_beginning;
5727 high = range_end;
5728 low_set = 1;
5729 }
5730 else
5731 {
5732 if (range_beginning < low)
5733 low = range_beginning;
5734 if (range_end > high)
5735 high = range_end;
5736 }
5737 }
5738
5739 if (! low_set)
5740 /* If the first entry is an end-of-list marker, the range
5741 describes an empty scope, i.e. no instructions. */
5742 return 0;
5743
5744 if (low_return)
5745 *low_return = low;
5746 if (high_return)
5747 *high_return = high;
5748 return 1;
5749 }
5750
5751 /* Get low and high pc attributes from a die. Return 1 if the attributes
5752 are present and valid, otherwise, return 0. Return -1 if the range is
5753 discontinuous, i.e. derived from DW_AT_ranges information. */
5754 static int
5755 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5756 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5757 struct partial_symtab *pst)
5758 {
5759 struct attribute *attr;
5760 CORE_ADDR low = 0;
5761 CORE_ADDR high = 0;
5762 int ret = 0;
5763
5764 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5765 if (attr)
5766 {
5767 high = DW_ADDR (attr);
5768 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5769 if (attr)
5770 low = DW_ADDR (attr);
5771 else
5772 /* Found high w/o low attribute. */
5773 return 0;
5774
5775 /* Found consecutive range of addresses. */
5776 ret = 1;
5777 }
5778 else
5779 {
5780 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5781 if (attr != NULL)
5782 {
5783 /* Value of the DW_AT_ranges attribute is the offset in the
5784 .debug_ranges section. */
5785 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5786 return 0;
5787 /* Found discontinuous range of addresses. */
5788 ret = -1;
5789 }
5790 }
5791
5792 if (high < low)
5793 return 0;
5794
5795 /* When using the GNU linker, .gnu.linkonce. sections are used to
5796 eliminate duplicate copies of functions and vtables and such.
5797 The linker will arbitrarily choose one and discard the others.
5798 The AT_*_pc values for such functions refer to local labels in
5799 these sections. If the section from that file was discarded, the
5800 labels are not in the output, so the relocs get a value of 0.
5801 If this is a discarded function, mark the pc bounds as invalid,
5802 so that GDB will ignore it. */
5803 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5804 return 0;
5805
5806 *lowpc = low;
5807 *highpc = high;
5808 return ret;
5809 }
5810
5811 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5812 its low and high PC addresses. Do nothing if these addresses could not
5813 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5814 and HIGHPC to the high address if greater than HIGHPC. */
5815
5816 static void
5817 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5818 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5819 struct dwarf2_cu *cu)
5820 {
5821 CORE_ADDR low, high;
5822 struct die_info *child = die->child;
5823
5824 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5825 {
5826 *lowpc = min (*lowpc, low);
5827 *highpc = max (*highpc, high);
5828 }
5829
5830 /* If the language does not allow nested subprograms (either inside
5831 subprograms or lexical blocks), we're done. */
5832 if (cu->language != language_ada)
5833 return;
5834
5835 /* Check all the children of the given DIE. If it contains nested
5836 subprograms, then check their pc bounds. Likewise, we need to
5837 check lexical blocks as well, as they may also contain subprogram
5838 definitions. */
5839 while (child && child->tag)
5840 {
5841 if (child->tag == DW_TAG_subprogram
5842 || child->tag == DW_TAG_lexical_block)
5843 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5844 child = sibling_die (child);
5845 }
5846 }
5847
5848 /* Get the low and high pc's represented by the scope DIE, and store
5849 them in *LOWPC and *HIGHPC. If the correct values can't be
5850 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5851
5852 static void
5853 get_scope_pc_bounds (struct die_info *die,
5854 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5855 struct dwarf2_cu *cu)
5856 {
5857 CORE_ADDR best_low = (CORE_ADDR) -1;
5858 CORE_ADDR best_high = (CORE_ADDR) 0;
5859 CORE_ADDR current_low, current_high;
5860
5861 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5862 {
5863 best_low = current_low;
5864 best_high = current_high;
5865 }
5866 else
5867 {
5868 struct die_info *child = die->child;
5869
5870 while (child && child->tag)
5871 {
5872 switch (child->tag) {
5873 case DW_TAG_subprogram:
5874 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5875 break;
5876 case DW_TAG_namespace:
5877 case DW_TAG_module:
5878 /* FIXME: carlton/2004-01-16: Should we do this for
5879 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5880 that current GCC's always emit the DIEs corresponding
5881 to definitions of methods of classes as children of a
5882 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5883 the DIEs giving the declarations, which could be
5884 anywhere). But I don't see any reason why the
5885 standards says that they have to be there. */
5886 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5887
5888 if (current_low != ((CORE_ADDR) -1))
5889 {
5890 best_low = min (best_low, current_low);
5891 best_high = max (best_high, current_high);
5892 }
5893 break;
5894 default:
5895 /* Ignore. */
5896 break;
5897 }
5898
5899 child = sibling_die (child);
5900 }
5901 }
5902
5903 *lowpc = best_low;
5904 *highpc = best_high;
5905 }
5906
5907 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5908 in DIE. */
5909 static void
5910 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5911 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5912 {
5913 struct attribute *attr;
5914
5915 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5916 if (attr)
5917 {
5918 CORE_ADDR high = DW_ADDR (attr);
5919
5920 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5921 if (attr)
5922 {
5923 CORE_ADDR low = DW_ADDR (attr);
5924
5925 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5926 }
5927 }
5928
5929 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5930 if (attr)
5931 {
5932 bfd *obfd = cu->objfile->obfd;
5933
5934 /* The value of the DW_AT_ranges attribute is the offset of the
5935 address range list in the .debug_ranges section. */
5936 unsigned long offset = DW_UNSND (attr);
5937 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5938
5939 /* For some target architectures, but not others, the
5940 read_address function sign-extends the addresses it returns.
5941 To recognize base address selection entries, we need a
5942 mask. */
5943 unsigned int addr_size = cu->header.addr_size;
5944 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5945
5946 /* The base address, to which the next pair is relative. Note
5947 that this 'base' is a DWARF concept: most entries in a range
5948 list are relative, to reduce the number of relocs against the
5949 debugging information. This is separate from this function's
5950 'baseaddr' argument, which GDB uses to relocate debugging
5951 information from a shared library based on the address at
5952 which the library was loaded. */
5953 CORE_ADDR base = cu->base_address;
5954 int base_known = cu->base_known;
5955
5956 gdb_assert (dwarf2_per_objfile->ranges.readin);
5957 if (offset >= dwarf2_per_objfile->ranges.size)
5958 {
5959 complaint (&symfile_complaints,
5960 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5961 offset);
5962 return;
5963 }
5964
5965 for (;;)
5966 {
5967 unsigned int bytes_read;
5968 CORE_ADDR start, end;
5969
5970 start = read_address (obfd, buffer, cu, &bytes_read);
5971 buffer += bytes_read;
5972 end = read_address (obfd, buffer, cu, &bytes_read);
5973 buffer += bytes_read;
5974
5975 /* Did we find the end of the range list? */
5976 if (start == 0 && end == 0)
5977 break;
5978
5979 /* Did we find a base address selection entry? */
5980 else if ((start & base_select_mask) == base_select_mask)
5981 {
5982 base = end;
5983 base_known = 1;
5984 }
5985
5986 /* We found an ordinary address range. */
5987 else
5988 {
5989 if (!base_known)
5990 {
5991 complaint (&symfile_complaints,
5992 _("Invalid .debug_ranges data (no base address)"));
5993 return;
5994 }
5995
5996 record_block_range (block,
5997 baseaddr + base + start,
5998 baseaddr + base + end - 1);
5999 }
6000 }
6001 }
6002 }
6003
6004 /* Add an aggregate field to the field list. */
6005
6006 static void
6007 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6008 struct dwarf2_cu *cu)
6009 {
6010 struct objfile *objfile = cu->objfile;
6011 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6012 struct nextfield *new_field;
6013 struct attribute *attr;
6014 struct field *fp;
6015 char *fieldname = "";
6016
6017 /* Allocate a new field list entry and link it in. */
6018 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6019 make_cleanup (xfree, new_field);
6020 memset (new_field, 0, sizeof (struct nextfield));
6021
6022 if (die->tag == DW_TAG_inheritance)
6023 {
6024 new_field->next = fip->baseclasses;
6025 fip->baseclasses = new_field;
6026 }
6027 else
6028 {
6029 new_field->next = fip->fields;
6030 fip->fields = new_field;
6031 }
6032 fip->nfields++;
6033
6034 /* Handle accessibility and virtuality of field.
6035 The default accessibility for members is public, the default
6036 accessibility for inheritance is private. */
6037 if (die->tag != DW_TAG_inheritance)
6038 new_field->accessibility = DW_ACCESS_public;
6039 else
6040 new_field->accessibility = DW_ACCESS_private;
6041 new_field->virtuality = DW_VIRTUALITY_none;
6042
6043 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6044 if (attr)
6045 new_field->accessibility = DW_UNSND (attr);
6046 if (new_field->accessibility != DW_ACCESS_public)
6047 fip->non_public_fields = 1;
6048 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6049 if (attr)
6050 new_field->virtuality = DW_UNSND (attr);
6051
6052 fp = &new_field->field;
6053
6054 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6055 {
6056 /* Data member other than a C++ static data member. */
6057
6058 /* Get type of field. */
6059 fp->type = die_type (die, cu);
6060
6061 SET_FIELD_BITPOS (*fp, 0);
6062
6063 /* Get bit size of field (zero if none). */
6064 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6065 if (attr)
6066 {
6067 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6068 }
6069 else
6070 {
6071 FIELD_BITSIZE (*fp) = 0;
6072 }
6073
6074 /* Get bit offset of field. */
6075 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6076 if (attr)
6077 {
6078 int byte_offset = 0;
6079
6080 if (attr_form_is_section_offset (attr))
6081 dwarf2_complex_location_expr_complaint ();
6082 else if (attr_form_is_constant (attr))
6083 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6084 else if (attr_form_is_block (attr))
6085 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6086 else
6087 dwarf2_complex_location_expr_complaint ();
6088
6089 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6090 }
6091 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6092 if (attr)
6093 {
6094 if (gdbarch_bits_big_endian (gdbarch))
6095 {
6096 /* For big endian bits, the DW_AT_bit_offset gives the
6097 additional bit offset from the MSB of the containing
6098 anonymous object to the MSB of the field. We don't
6099 have to do anything special since we don't need to
6100 know the size of the anonymous object. */
6101 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6102 }
6103 else
6104 {
6105 /* For little endian bits, compute the bit offset to the
6106 MSB of the anonymous object, subtract off the number of
6107 bits from the MSB of the field to the MSB of the
6108 object, and then subtract off the number of bits of
6109 the field itself. The result is the bit offset of
6110 the LSB of the field. */
6111 int anonymous_size;
6112 int bit_offset = DW_UNSND (attr);
6113
6114 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6115 if (attr)
6116 {
6117 /* The size of the anonymous object containing
6118 the bit field is explicit, so use the
6119 indicated size (in bytes). */
6120 anonymous_size = DW_UNSND (attr);
6121 }
6122 else
6123 {
6124 /* The size of the anonymous object containing
6125 the bit field must be inferred from the type
6126 attribute of the data member containing the
6127 bit field. */
6128 anonymous_size = TYPE_LENGTH (fp->type);
6129 }
6130 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6131 - bit_offset - FIELD_BITSIZE (*fp);
6132 }
6133 }
6134
6135 /* Get name of field. */
6136 fieldname = dwarf2_name (die, cu);
6137 if (fieldname == NULL)
6138 fieldname = "";
6139
6140 /* The name is already allocated along with this objfile, so we don't
6141 need to duplicate it for the type. */
6142 fp->name = fieldname;
6143
6144 /* Change accessibility for artificial fields (e.g. virtual table
6145 pointer or virtual base class pointer) to private. */
6146 if (dwarf2_attr (die, DW_AT_artificial, cu))
6147 {
6148 FIELD_ARTIFICIAL (*fp) = 1;
6149 new_field->accessibility = DW_ACCESS_private;
6150 fip->non_public_fields = 1;
6151 }
6152 }
6153 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6154 {
6155 /* C++ static member. */
6156
6157 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6158 is a declaration, but all versions of G++ as of this writing
6159 (so through at least 3.2.1) incorrectly generate
6160 DW_TAG_variable tags. */
6161
6162 char *physname;
6163
6164 /* Get name of field. */
6165 fieldname = dwarf2_name (die, cu);
6166 if (fieldname == NULL)
6167 return;
6168
6169 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6170 if (attr
6171 /* Only create a symbol if this is an external value.
6172 new_symbol checks this and puts the value in the global symbol
6173 table, which we want. If it is not external, new_symbol
6174 will try to put the value in cu->list_in_scope which is wrong. */
6175 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6176 {
6177 /* A static const member, not much different than an enum as far as
6178 we're concerned, except that we can support more types. */
6179 new_symbol (die, NULL, cu);
6180 }
6181
6182 /* Get physical name. */
6183 physname = (char *) dwarf2_physname (fieldname, die, cu);
6184
6185 /* The name is already allocated along with this objfile, so we don't
6186 need to duplicate it for the type. */
6187 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6188 FIELD_TYPE (*fp) = die_type (die, cu);
6189 FIELD_NAME (*fp) = fieldname;
6190 }
6191 else if (die->tag == DW_TAG_inheritance)
6192 {
6193 /* C++ base class field. */
6194 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6195 if (attr)
6196 {
6197 int byte_offset = 0;
6198
6199 if (attr_form_is_section_offset (attr))
6200 dwarf2_complex_location_expr_complaint ();
6201 else if (attr_form_is_constant (attr))
6202 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6203 else if (attr_form_is_block (attr))
6204 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6205 else
6206 dwarf2_complex_location_expr_complaint ();
6207
6208 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6209 }
6210 FIELD_BITSIZE (*fp) = 0;
6211 FIELD_TYPE (*fp) = die_type (die, cu);
6212 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6213 fip->nbaseclasses++;
6214 }
6215 }
6216
6217 /* Add a typedef defined in the scope of the FIP's class. */
6218
6219 static void
6220 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6221 struct dwarf2_cu *cu)
6222 {
6223 struct objfile *objfile = cu->objfile;
6224 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6225 struct typedef_field_list *new_field;
6226 struct attribute *attr;
6227 struct typedef_field *fp;
6228 char *fieldname = "";
6229
6230 /* Allocate a new field list entry and link it in. */
6231 new_field = xzalloc (sizeof (*new_field));
6232 make_cleanup (xfree, new_field);
6233
6234 gdb_assert (die->tag == DW_TAG_typedef);
6235
6236 fp = &new_field->field;
6237
6238 /* Get name of field. */
6239 fp->name = dwarf2_name (die, cu);
6240 if (fp->name == NULL)
6241 return;
6242
6243 fp->type = read_type_die (die, cu);
6244
6245 new_field->next = fip->typedef_field_list;
6246 fip->typedef_field_list = new_field;
6247 fip->typedef_field_list_count++;
6248 }
6249
6250 /* Create the vector of fields, and attach it to the type. */
6251
6252 static void
6253 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6254 struct dwarf2_cu *cu)
6255 {
6256 int nfields = fip->nfields;
6257
6258 /* Record the field count, allocate space for the array of fields,
6259 and create blank accessibility bitfields if necessary. */
6260 TYPE_NFIELDS (type) = nfields;
6261 TYPE_FIELDS (type) = (struct field *)
6262 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6263 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6264
6265 if (fip->non_public_fields && cu->language != language_ada)
6266 {
6267 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6268
6269 TYPE_FIELD_PRIVATE_BITS (type) =
6270 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6271 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6272
6273 TYPE_FIELD_PROTECTED_BITS (type) =
6274 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6275 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6276
6277 TYPE_FIELD_IGNORE_BITS (type) =
6278 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6279 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6280 }
6281
6282 /* If the type has baseclasses, allocate and clear a bit vector for
6283 TYPE_FIELD_VIRTUAL_BITS. */
6284 if (fip->nbaseclasses && cu->language != language_ada)
6285 {
6286 int num_bytes = B_BYTES (fip->nbaseclasses);
6287 unsigned char *pointer;
6288
6289 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6290 pointer = TYPE_ALLOC (type, num_bytes);
6291 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6292 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6293 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6294 }
6295
6296 /* Copy the saved-up fields into the field vector. Start from the head
6297 of the list, adding to the tail of the field array, so that they end
6298 up in the same order in the array in which they were added to the list. */
6299 while (nfields-- > 0)
6300 {
6301 struct nextfield *fieldp;
6302
6303 if (fip->fields)
6304 {
6305 fieldp = fip->fields;
6306 fip->fields = fieldp->next;
6307 }
6308 else
6309 {
6310 fieldp = fip->baseclasses;
6311 fip->baseclasses = fieldp->next;
6312 }
6313
6314 TYPE_FIELD (type, nfields) = fieldp->field;
6315 switch (fieldp->accessibility)
6316 {
6317 case DW_ACCESS_private:
6318 if (cu->language != language_ada)
6319 SET_TYPE_FIELD_PRIVATE (type, nfields);
6320 break;
6321
6322 case DW_ACCESS_protected:
6323 if (cu->language != language_ada)
6324 SET_TYPE_FIELD_PROTECTED (type, nfields);
6325 break;
6326
6327 case DW_ACCESS_public:
6328 break;
6329
6330 default:
6331 /* Unknown accessibility. Complain and treat it as public. */
6332 {
6333 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6334 fieldp->accessibility);
6335 }
6336 break;
6337 }
6338 if (nfields < fip->nbaseclasses)
6339 {
6340 switch (fieldp->virtuality)
6341 {
6342 case DW_VIRTUALITY_virtual:
6343 case DW_VIRTUALITY_pure_virtual:
6344 if (cu->language == language_ada)
6345 error ("unexpected virtuality in component of Ada type");
6346 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6347 break;
6348 }
6349 }
6350 }
6351 }
6352
6353 /* Add a member function to the proper fieldlist. */
6354
6355 static void
6356 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6357 struct type *type, struct dwarf2_cu *cu)
6358 {
6359 struct objfile *objfile = cu->objfile;
6360 struct attribute *attr;
6361 struct fnfieldlist *flp;
6362 int i;
6363 struct fn_field *fnp;
6364 char *fieldname;
6365 struct nextfnfield *new_fnfield;
6366 struct type *this_type;
6367
6368 if (cu->language == language_ada)
6369 error ("unexpected member function in Ada type");
6370
6371 /* Get name of member function. */
6372 fieldname = dwarf2_name (die, cu);
6373 if (fieldname == NULL)
6374 return;
6375
6376 /* Look up member function name in fieldlist. */
6377 for (i = 0; i < fip->nfnfields; i++)
6378 {
6379 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6380 break;
6381 }
6382
6383 /* Create new list element if necessary. */
6384 if (i < fip->nfnfields)
6385 flp = &fip->fnfieldlists[i];
6386 else
6387 {
6388 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6389 {
6390 fip->fnfieldlists = (struct fnfieldlist *)
6391 xrealloc (fip->fnfieldlists,
6392 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6393 * sizeof (struct fnfieldlist));
6394 if (fip->nfnfields == 0)
6395 make_cleanup (free_current_contents, &fip->fnfieldlists);
6396 }
6397 flp = &fip->fnfieldlists[fip->nfnfields];
6398 flp->name = fieldname;
6399 flp->length = 0;
6400 flp->head = NULL;
6401 i = fip->nfnfields++;
6402 }
6403
6404 /* Create a new member function field and chain it to the field list
6405 entry. */
6406 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6407 make_cleanup (xfree, new_fnfield);
6408 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6409 new_fnfield->next = flp->head;
6410 flp->head = new_fnfield;
6411 flp->length++;
6412
6413 /* Fill in the member function field info. */
6414 fnp = &new_fnfield->fnfield;
6415
6416 /* Delay processing of the physname until later. */
6417 if (cu->language == language_cplus || cu->language == language_java)
6418 {
6419 add_to_method_list (type, i, flp->length - 1, fieldname,
6420 die, cu);
6421 }
6422 else
6423 {
6424 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6425 fnp->physname = physname ? physname : "";
6426 }
6427
6428 fnp->type = alloc_type (objfile);
6429 this_type = read_type_die (die, cu);
6430 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6431 {
6432 int nparams = TYPE_NFIELDS (this_type);
6433
6434 /* TYPE is the domain of this method, and THIS_TYPE is the type
6435 of the method itself (TYPE_CODE_METHOD). */
6436 smash_to_method_type (fnp->type, type,
6437 TYPE_TARGET_TYPE (this_type),
6438 TYPE_FIELDS (this_type),
6439 TYPE_NFIELDS (this_type),
6440 TYPE_VARARGS (this_type));
6441
6442 /* Handle static member functions.
6443 Dwarf2 has no clean way to discern C++ static and non-static
6444 member functions. G++ helps GDB by marking the first
6445 parameter for non-static member functions (which is the
6446 this pointer) as artificial. We obtain this information
6447 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
6448 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6449 fnp->voffset = VOFFSET_STATIC;
6450 }
6451 else
6452 complaint (&symfile_complaints, _("member function type missing for '%s'"),
6453 dwarf2_full_name (fieldname, die, cu));
6454
6455 /* Get fcontext from DW_AT_containing_type if present. */
6456 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6457 fnp->fcontext = die_containing_type (die, cu);
6458
6459 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6460 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6461
6462 /* Get accessibility. */
6463 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6464 if (attr)
6465 {
6466 switch (DW_UNSND (attr))
6467 {
6468 case DW_ACCESS_private:
6469 fnp->is_private = 1;
6470 break;
6471 case DW_ACCESS_protected:
6472 fnp->is_protected = 1;
6473 break;
6474 }
6475 }
6476
6477 /* Check for artificial methods. */
6478 attr = dwarf2_attr (die, DW_AT_artificial, cu);
6479 if (attr && DW_UNSND (attr) != 0)
6480 fnp->is_artificial = 1;
6481
6482 /* Get index in virtual function table if it is a virtual member
6483 function. For older versions of GCC, this is an offset in the
6484 appropriate virtual table, as specified by DW_AT_containing_type.
6485 For everyone else, it is an expression to be evaluated relative
6486 to the object address. */
6487
6488 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6489 if (attr)
6490 {
6491 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6492 {
6493 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6494 {
6495 /* Old-style GCC. */
6496 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6497 }
6498 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6499 || (DW_BLOCK (attr)->size > 1
6500 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6501 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6502 {
6503 struct dwarf_block blk;
6504 int offset;
6505
6506 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6507 ? 1 : 2);
6508 blk.size = DW_BLOCK (attr)->size - offset;
6509 blk.data = DW_BLOCK (attr)->data + offset;
6510 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6511 if ((fnp->voffset % cu->header.addr_size) != 0)
6512 dwarf2_complex_location_expr_complaint ();
6513 else
6514 fnp->voffset /= cu->header.addr_size;
6515 fnp->voffset += 2;
6516 }
6517 else
6518 dwarf2_complex_location_expr_complaint ();
6519
6520 if (!fnp->fcontext)
6521 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6522 }
6523 else if (attr_form_is_section_offset (attr))
6524 {
6525 dwarf2_complex_location_expr_complaint ();
6526 }
6527 else
6528 {
6529 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6530 fieldname);
6531 }
6532 }
6533 else
6534 {
6535 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6536 if (attr && DW_UNSND (attr))
6537 {
6538 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6539 complaint (&symfile_complaints,
6540 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6541 fieldname, die->offset);
6542 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6543 TYPE_CPLUS_DYNAMIC (type) = 1;
6544 }
6545 }
6546 }
6547
6548 /* Create the vector of member function fields, and attach it to the type. */
6549
6550 static void
6551 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6552 struct dwarf2_cu *cu)
6553 {
6554 struct fnfieldlist *flp;
6555 int total_length = 0;
6556 int i;
6557
6558 if (cu->language == language_ada)
6559 error ("unexpected member functions in Ada type");
6560
6561 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6562 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6563 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6564
6565 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6566 {
6567 struct nextfnfield *nfp = flp->head;
6568 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6569 int k;
6570
6571 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6572 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6573 fn_flp->fn_fields = (struct fn_field *)
6574 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6575 for (k = flp->length; (k--, nfp); nfp = nfp->next)
6576 fn_flp->fn_fields[k] = nfp->fnfield;
6577
6578 total_length += flp->length;
6579 }
6580
6581 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6582 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6583 }
6584
6585 /* Returns non-zero if NAME is the name of a vtable member in CU's
6586 language, zero otherwise. */
6587 static int
6588 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6589 {
6590 static const char vptr[] = "_vptr";
6591 static const char vtable[] = "vtable";
6592
6593 /* Look for the C++ and Java forms of the vtable. */
6594 if ((cu->language == language_java
6595 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6596 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6597 && is_cplus_marker (name[sizeof (vptr) - 1])))
6598 return 1;
6599
6600 return 0;
6601 }
6602
6603 /* GCC outputs unnamed structures that are really pointers to member
6604 functions, with the ABI-specified layout. If TYPE describes
6605 such a structure, smash it into a member function type.
6606
6607 GCC shouldn't do this; it should just output pointer to member DIEs.
6608 This is GCC PR debug/28767. */
6609
6610 static void
6611 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6612 {
6613 struct type *pfn_type, *domain_type, *new_type;
6614
6615 /* Check for a structure with no name and two children. */
6616 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6617 return;
6618
6619 /* Check for __pfn and __delta members. */
6620 if (TYPE_FIELD_NAME (type, 0) == NULL
6621 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6622 || TYPE_FIELD_NAME (type, 1) == NULL
6623 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6624 return;
6625
6626 /* Find the type of the method. */
6627 pfn_type = TYPE_FIELD_TYPE (type, 0);
6628 if (pfn_type == NULL
6629 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6630 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6631 return;
6632
6633 /* Look for the "this" argument. */
6634 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6635 if (TYPE_NFIELDS (pfn_type) == 0
6636 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6637 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6638 return;
6639
6640 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6641 new_type = alloc_type (objfile);
6642 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6643 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6644 TYPE_VARARGS (pfn_type));
6645 smash_to_methodptr_type (type, new_type);
6646 }
6647
6648 /* Called when we find the DIE that starts a structure or union scope
6649 (definition) to create a type for the structure or union. Fill in
6650 the type's name and general properties; the members will not be
6651 processed until process_structure_type.
6652
6653 NOTE: we need to call these functions regardless of whether or not the
6654 DIE has a DW_AT_name attribute, since it might be an anonymous
6655 structure or union. This gets the type entered into our set of
6656 user defined types.
6657
6658 However, if the structure is incomplete (an opaque struct/union)
6659 then suppress creating a symbol table entry for it since gdb only
6660 wants to find the one with the complete definition. Note that if
6661 it is complete, we just call new_symbol, which does it's own
6662 checking about whether the struct/union is anonymous or not (and
6663 suppresses creating a symbol table entry itself). */
6664
6665 static struct type *
6666 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6667 {
6668 struct objfile *objfile = cu->objfile;
6669 struct type *type;
6670 struct attribute *attr;
6671 char *name;
6672
6673 /* If the definition of this type lives in .debug_types, read that type.
6674 Don't follow DW_AT_specification though, that will take us back up
6675 the chain and we want to go down. */
6676 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6677 if (attr)
6678 {
6679 struct dwarf2_cu *type_cu = cu;
6680 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6681
6682 /* We could just recurse on read_structure_type, but we need to call
6683 get_die_type to ensure only one type for this DIE is created.
6684 This is important, for example, because for c++ classes we need
6685 TYPE_NAME set which is only done by new_symbol. Blech. */
6686 type = read_type_die (type_die, type_cu);
6687
6688 /* TYPE_CU may not be the same as CU.
6689 Ensure TYPE is recorded in CU's type_hash table. */
6690 return set_die_type (die, type, cu);
6691 }
6692
6693 type = alloc_type (objfile);
6694 INIT_CPLUS_SPECIFIC (type);
6695
6696 name = dwarf2_name (die, cu);
6697 if (name != NULL)
6698 {
6699 if (cu->language == language_cplus
6700 || cu->language == language_java)
6701 {
6702 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6703
6704 /* dwarf2_full_name might have already finished building the DIE's
6705 type. If so, there is no need to continue. */
6706 if (get_die_type (die, cu) != NULL)
6707 return get_die_type (die, cu);
6708
6709 TYPE_TAG_NAME (type) = full_name;
6710 if (die->tag == DW_TAG_structure_type
6711 || die->tag == DW_TAG_class_type)
6712 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6713 }
6714 else
6715 {
6716 /* The name is already allocated along with this objfile, so
6717 we don't need to duplicate it for the type. */
6718 TYPE_TAG_NAME (type) = (char *) name;
6719 if (die->tag == DW_TAG_class_type)
6720 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6721 }
6722 }
6723
6724 if (die->tag == DW_TAG_structure_type)
6725 {
6726 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6727 }
6728 else if (die->tag == DW_TAG_union_type)
6729 {
6730 TYPE_CODE (type) = TYPE_CODE_UNION;
6731 }
6732 else
6733 {
6734 TYPE_CODE (type) = TYPE_CODE_CLASS;
6735 }
6736
6737 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6738 TYPE_DECLARED_CLASS (type) = 1;
6739
6740 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6741 if (attr)
6742 {
6743 TYPE_LENGTH (type) = DW_UNSND (attr);
6744 }
6745 else
6746 {
6747 TYPE_LENGTH (type) = 0;
6748 }
6749
6750 TYPE_STUB_SUPPORTED (type) = 1;
6751 if (die_is_declaration (die, cu))
6752 TYPE_STUB (type) = 1;
6753 else if (attr == NULL && die->child == NULL
6754 && producer_is_realview (cu->producer))
6755 /* RealView does not output the required DW_AT_declaration
6756 on incomplete types. */
6757 TYPE_STUB (type) = 1;
6758
6759 /* We need to add the type field to the die immediately so we don't
6760 infinitely recurse when dealing with pointers to the structure
6761 type within the structure itself. */
6762 set_die_type (die, type, cu);
6763
6764 /* set_die_type should be already done. */
6765 set_descriptive_type (type, die, cu);
6766
6767 return type;
6768 }
6769
6770 /* Finish creating a structure or union type, including filling in
6771 its members and creating a symbol for it. */
6772
6773 static void
6774 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6775 {
6776 struct objfile *objfile = cu->objfile;
6777 struct die_info *child_die = die->child;
6778 struct type *type;
6779
6780 type = get_die_type (die, cu);
6781 if (type == NULL)
6782 type = read_structure_type (die, cu);
6783
6784 if (die->child != NULL && ! die_is_declaration (die, cu))
6785 {
6786 struct field_info fi;
6787 struct die_info *child_die;
6788 VEC (symbolp) *template_args = NULL;
6789 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6790
6791 memset (&fi, 0, sizeof (struct field_info));
6792
6793 child_die = die->child;
6794
6795 while (child_die && child_die->tag)
6796 {
6797 if (child_die->tag == DW_TAG_member
6798 || child_die->tag == DW_TAG_variable)
6799 {
6800 /* NOTE: carlton/2002-11-05: A C++ static data member
6801 should be a DW_TAG_member that is a declaration, but
6802 all versions of G++ as of this writing (so through at
6803 least 3.2.1) incorrectly generate DW_TAG_variable
6804 tags for them instead. */
6805 dwarf2_add_field (&fi, child_die, cu);
6806 }
6807 else if (child_die->tag == DW_TAG_subprogram)
6808 {
6809 /* C++ member function. */
6810 dwarf2_add_member_fn (&fi, child_die, type, cu);
6811 }
6812 else if (child_die->tag == DW_TAG_inheritance)
6813 {
6814 /* C++ base class field. */
6815 dwarf2_add_field (&fi, child_die, cu);
6816 }
6817 else if (child_die->tag == DW_TAG_typedef)
6818 dwarf2_add_typedef (&fi, child_die, cu);
6819 else if (child_die->tag == DW_TAG_template_type_param
6820 || child_die->tag == DW_TAG_template_value_param)
6821 {
6822 struct symbol *arg = new_symbol (child_die, NULL, cu);
6823
6824 if (arg != NULL)
6825 VEC_safe_push (symbolp, template_args, arg);
6826 }
6827
6828 child_die = sibling_die (child_die);
6829 }
6830
6831 /* Attach template arguments to type. */
6832 if (! VEC_empty (symbolp, template_args))
6833 {
6834 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6835 TYPE_N_TEMPLATE_ARGUMENTS (type)
6836 = VEC_length (symbolp, template_args);
6837 TYPE_TEMPLATE_ARGUMENTS (type)
6838 = obstack_alloc (&objfile->objfile_obstack,
6839 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6840 * sizeof (struct symbol *)));
6841 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6842 VEC_address (symbolp, template_args),
6843 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6844 * sizeof (struct symbol *)));
6845 VEC_free (symbolp, template_args);
6846 }
6847
6848 /* Attach fields and member functions to the type. */
6849 if (fi.nfields)
6850 dwarf2_attach_fields_to_type (&fi, type, cu);
6851 if (fi.nfnfields)
6852 {
6853 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6854
6855 /* Get the type which refers to the base class (possibly this
6856 class itself) which contains the vtable pointer for the current
6857 class from the DW_AT_containing_type attribute. This use of
6858 DW_AT_containing_type is a GNU extension. */
6859
6860 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6861 {
6862 struct type *t = die_containing_type (die, cu);
6863
6864 TYPE_VPTR_BASETYPE (type) = t;
6865 if (type == t)
6866 {
6867 int i;
6868
6869 /* Our own class provides vtbl ptr. */
6870 for (i = TYPE_NFIELDS (t) - 1;
6871 i >= TYPE_N_BASECLASSES (t);
6872 --i)
6873 {
6874 char *fieldname = TYPE_FIELD_NAME (t, i);
6875
6876 if (is_vtable_name (fieldname, cu))
6877 {
6878 TYPE_VPTR_FIELDNO (type) = i;
6879 break;
6880 }
6881 }
6882
6883 /* Complain if virtual function table field not found. */
6884 if (i < TYPE_N_BASECLASSES (t))
6885 complaint (&symfile_complaints,
6886 _("virtual function table pointer not found when defining class '%s'"),
6887 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6888 "");
6889 }
6890 else
6891 {
6892 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6893 }
6894 }
6895 else if (cu->producer
6896 && strncmp (cu->producer,
6897 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6898 {
6899 /* The IBM XLC compiler does not provide direct indication
6900 of the containing type, but the vtable pointer is
6901 always named __vfp. */
6902
6903 int i;
6904
6905 for (i = TYPE_NFIELDS (type) - 1;
6906 i >= TYPE_N_BASECLASSES (type);
6907 --i)
6908 {
6909 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6910 {
6911 TYPE_VPTR_FIELDNO (type) = i;
6912 TYPE_VPTR_BASETYPE (type) = type;
6913 break;
6914 }
6915 }
6916 }
6917 }
6918
6919 /* Copy fi.typedef_field_list linked list elements content into the
6920 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6921 if (fi.typedef_field_list)
6922 {
6923 int i = fi.typedef_field_list_count;
6924
6925 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6926 TYPE_TYPEDEF_FIELD_ARRAY (type)
6927 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6928 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6929
6930 /* Reverse the list order to keep the debug info elements order. */
6931 while (--i >= 0)
6932 {
6933 struct typedef_field *dest, *src;
6934
6935 dest = &TYPE_TYPEDEF_FIELD (type, i);
6936 src = &fi.typedef_field_list->field;
6937 fi.typedef_field_list = fi.typedef_field_list->next;
6938 *dest = *src;
6939 }
6940 }
6941
6942 do_cleanups (back_to);
6943 }
6944
6945 quirk_gcc_member_function_pointer (type, cu->objfile);
6946
6947 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6948 snapshots) has been known to create a die giving a declaration
6949 for a class that has, as a child, a die giving a definition for a
6950 nested class. So we have to process our children even if the
6951 current die is a declaration. Normally, of course, a declaration
6952 won't have any children at all. */
6953
6954 while (child_die != NULL && child_die->tag)
6955 {
6956 if (child_die->tag == DW_TAG_member
6957 || child_die->tag == DW_TAG_variable
6958 || child_die->tag == DW_TAG_inheritance
6959 || child_die->tag == DW_TAG_template_value_param
6960 || child_die->tag == DW_TAG_template_type_param)
6961 {
6962 /* Do nothing. */
6963 }
6964 else
6965 process_die (child_die, cu);
6966
6967 child_die = sibling_die (child_die);
6968 }
6969
6970 /* Do not consider external references. According to the DWARF standard,
6971 these DIEs are identified by the fact that they have no byte_size
6972 attribute, and a declaration attribute. */
6973 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6974 || !die_is_declaration (die, cu))
6975 new_symbol (die, type, cu);
6976 }
6977
6978 /* Given a DW_AT_enumeration_type die, set its type. We do not
6979 complete the type's fields yet, or create any symbols. */
6980
6981 static struct type *
6982 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6983 {
6984 struct objfile *objfile = cu->objfile;
6985 struct type *type;
6986 struct attribute *attr;
6987 const char *name;
6988
6989 /* If the definition of this type lives in .debug_types, read that type.
6990 Don't follow DW_AT_specification though, that will take us back up
6991 the chain and we want to go down. */
6992 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6993 if (attr)
6994 {
6995 struct dwarf2_cu *type_cu = cu;
6996 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6997
6998 type = read_type_die (type_die, type_cu);
6999
7000 /* TYPE_CU may not be the same as CU.
7001 Ensure TYPE is recorded in CU's type_hash table. */
7002 return set_die_type (die, type, cu);
7003 }
7004
7005 type = alloc_type (objfile);
7006
7007 TYPE_CODE (type) = TYPE_CODE_ENUM;
7008 name = dwarf2_full_name (NULL, die, cu);
7009 if (name != NULL)
7010 TYPE_TAG_NAME (type) = (char *) name;
7011
7012 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7013 if (attr)
7014 {
7015 TYPE_LENGTH (type) = DW_UNSND (attr);
7016 }
7017 else
7018 {
7019 TYPE_LENGTH (type) = 0;
7020 }
7021
7022 /* The enumeration DIE can be incomplete. In Ada, any type can be
7023 declared as private in the package spec, and then defined only
7024 inside the package body. Such types are known as Taft Amendment
7025 Types. When another package uses such a type, an incomplete DIE
7026 may be generated by the compiler. */
7027 if (die_is_declaration (die, cu))
7028 TYPE_STUB (type) = 1;
7029
7030 return set_die_type (die, type, cu);
7031 }
7032
7033 /* Given a pointer to a die which begins an enumeration, process all
7034 the dies that define the members of the enumeration, and create the
7035 symbol for the enumeration type.
7036
7037 NOTE: We reverse the order of the element list. */
7038
7039 static void
7040 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7041 {
7042 struct type *this_type;
7043
7044 this_type = get_die_type (die, cu);
7045 if (this_type == NULL)
7046 this_type = read_enumeration_type (die, cu);
7047
7048 if (die->child != NULL)
7049 {
7050 struct die_info *child_die;
7051 struct symbol *sym;
7052 struct field *fields = NULL;
7053 int num_fields = 0;
7054 int unsigned_enum = 1;
7055 char *name;
7056
7057 child_die = die->child;
7058 while (child_die && child_die->tag)
7059 {
7060 if (child_die->tag != DW_TAG_enumerator)
7061 {
7062 process_die (child_die, cu);
7063 }
7064 else
7065 {
7066 name = dwarf2_name (child_die, cu);
7067 if (name)
7068 {
7069 sym = new_symbol (child_die, this_type, cu);
7070 if (SYMBOL_VALUE (sym) < 0)
7071 unsigned_enum = 0;
7072
7073 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7074 {
7075 fields = (struct field *)
7076 xrealloc (fields,
7077 (num_fields + DW_FIELD_ALLOC_CHUNK)
7078 * sizeof (struct field));
7079 }
7080
7081 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7082 FIELD_TYPE (fields[num_fields]) = NULL;
7083 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7084 FIELD_BITSIZE (fields[num_fields]) = 0;
7085
7086 num_fields++;
7087 }
7088 }
7089
7090 child_die = sibling_die (child_die);
7091 }
7092
7093 if (num_fields)
7094 {
7095 TYPE_NFIELDS (this_type) = num_fields;
7096 TYPE_FIELDS (this_type) = (struct field *)
7097 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7098 memcpy (TYPE_FIELDS (this_type), fields,
7099 sizeof (struct field) * num_fields);
7100 xfree (fields);
7101 }
7102 if (unsigned_enum)
7103 TYPE_UNSIGNED (this_type) = 1;
7104 }
7105
7106 new_symbol (die, this_type, cu);
7107 }
7108
7109 /* Extract all information from a DW_TAG_array_type DIE and put it in
7110 the DIE's type field. For now, this only handles one dimensional
7111 arrays. */
7112
7113 static struct type *
7114 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7115 {
7116 struct objfile *objfile = cu->objfile;
7117 struct die_info *child_die;
7118 struct type *type;
7119 struct type *element_type, *range_type, *index_type;
7120 struct type **range_types = NULL;
7121 struct attribute *attr;
7122 int ndim = 0;
7123 struct cleanup *back_to;
7124 char *name;
7125
7126 element_type = die_type (die, cu);
7127
7128 /* The die_type call above may have already set the type for this DIE. */
7129 type = get_die_type (die, cu);
7130 if (type)
7131 return type;
7132
7133 /* Irix 6.2 native cc creates array types without children for
7134 arrays with unspecified length. */
7135 if (die->child == NULL)
7136 {
7137 index_type = objfile_type (objfile)->builtin_int;
7138 range_type = create_range_type (NULL, index_type, 0, -1);
7139 type = create_array_type (NULL, element_type, range_type);
7140 return set_die_type (die, type, cu);
7141 }
7142
7143 back_to = make_cleanup (null_cleanup, NULL);
7144 child_die = die->child;
7145 while (child_die && child_die->tag)
7146 {
7147 if (child_die->tag == DW_TAG_subrange_type)
7148 {
7149 struct type *child_type = read_type_die (child_die, cu);
7150
7151 if (child_type != NULL)
7152 {
7153 /* The range type was succesfully read. Save it for
7154 the array type creation. */
7155 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7156 {
7157 range_types = (struct type **)
7158 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7159 * sizeof (struct type *));
7160 if (ndim == 0)
7161 make_cleanup (free_current_contents, &range_types);
7162 }
7163 range_types[ndim++] = child_type;
7164 }
7165 }
7166 child_die = sibling_die (child_die);
7167 }
7168
7169 /* Dwarf2 dimensions are output from left to right, create the
7170 necessary array types in backwards order. */
7171
7172 type = element_type;
7173
7174 if (read_array_order (die, cu) == DW_ORD_col_major)
7175 {
7176 int i = 0;
7177
7178 while (i < ndim)
7179 type = create_array_type (NULL, type, range_types[i++]);
7180 }
7181 else
7182 {
7183 while (ndim-- > 0)
7184 type = create_array_type (NULL, type, range_types[ndim]);
7185 }
7186
7187 /* Understand Dwarf2 support for vector types (like they occur on
7188 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7189 array type. This is not part of the Dwarf2/3 standard yet, but a
7190 custom vendor extension. The main difference between a regular
7191 array and the vector variant is that vectors are passed by value
7192 to functions. */
7193 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7194 if (attr)
7195 make_vector_type (type);
7196
7197 name = dwarf2_name (die, cu);
7198 if (name)
7199 TYPE_NAME (type) = name;
7200
7201 /* Install the type in the die. */
7202 set_die_type (die, type, cu);
7203
7204 /* set_die_type should be already done. */
7205 set_descriptive_type (type, die, cu);
7206
7207 do_cleanups (back_to);
7208
7209 return type;
7210 }
7211
7212 static enum dwarf_array_dim_ordering
7213 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7214 {
7215 struct attribute *attr;
7216
7217 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7218
7219 if (attr) return DW_SND (attr);
7220
7221 /*
7222 GNU F77 is a special case, as at 08/2004 array type info is the
7223 opposite order to the dwarf2 specification, but data is still
7224 laid out as per normal fortran.
7225
7226 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7227 version checking.
7228 */
7229
7230 if (cu->language == language_fortran
7231 && cu->producer && strstr (cu->producer, "GNU F77"))
7232 {
7233 return DW_ORD_row_major;
7234 }
7235
7236 switch (cu->language_defn->la_array_ordering)
7237 {
7238 case array_column_major:
7239 return DW_ORD_col_major;
7240 case array_row_major:
7241 default:
7242 return DW_ORD_row_major;
7243 };
7244 }
7245
7246 /* Extract all information from a DW_TAG_set_type DIE and put it in
7247 the DIE's type field. */
7248
7249 static struct type *
7250 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7251 {
7252 struct type *domain_type, *set_type;
7253 struct attribute *attr;
7254
7255 domain_type = die_type (die, cu);
7256
7257 /* The die_type call above may have already set the type for this DIE. */
7258 set_type = get_die_type (die, cu);
7259 if (set_type)
7260 return set_type;
7261
7262 set_type = create_set_type (NULL, domain_type);
7263
7264 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7265 if (attr)
7266 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7267
7268 return set_die_type (die, set_type, cu);
7269 }
7270
7271 /* First cut: install each common block member as a global variable. */
7272
7273 static void
7274 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7275 {
7276 struct die_info *child_die;
7277 struct attribute *attr;
7278 struct symbol *sym;
7279 CORE_ADDR base = (CORE_ADDR) 0;
7280
7281 attr = dwarf2_attr (die, DW_AT_location, cu);
7282 if (attr)
7283 {
7284 /* Support the .debug_loc offsets */
7285 if (attr_form_is_block (attr))
7286 {
7287 base = decode_locdesc (DW_BLOCK (attr), cu);
7288 }
7289 else if (attr_form_is_section_offset (attr))
7290 {
7291 dwarf2_complex_location_expr_complaint ();
7292 }
7293 else
7294 {
7295 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7296 "common block member");
7297 }
7298 }
7299 if (die->child != NULL)
7300 {
7301 child_die = die->child;
7302 while (child_die && child_die->tag)
7303 {
7304 sym = new_symbol (child_die, NULL, cu);
7305 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7306 if (sym != NULL && attr != NULL)
7307 {
7308 CORE_ADDR byte_offset = 0;
7309
7310 if (attr_form_is_section_offset (attr))
7311 dwarf2_complex_location_expr_complaint ();
7312 else if (attr_form_is_constant (attr))
7313 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7314 else if (attr_form_is_block (attr))
7315 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7316 else
7317 dwarf2_complex_location_expr_complaint ();
7318
7319 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7320 add_symbol_to_list (sym, &global_symbols);
7321 }
7322 child_die = sibling_die (child_die);
7323 }
7324 }
7325 }
7326
7327 /* Create a type for a C++ namespace. */
7328
7329 static struct type *
7330 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7331 {
7332 struct objfile *objfile = cu->objfile;
7333 const char *previous_prefix, *name;
7334 int is_anonymous;
7335 struct type *type;
7336
7337 /* For extensions, reuse the type of the original namespace. */
7338 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7339 {
7340 struct die_info *ext_die;
7341 struct dwarf2_cu *ext_cu = cu;
7342
7343 ext_die = dwarf2_extension (die, &ext_cu);
7344 type = read_type_die (ext_die, ext_cu);
7345
7346 /* EXT_CU may not be the same as CU.
7347 Ensure TYPE is recorded in CU's type_hash table. */
7348 return set_die_type (die, type, cu);
7349 }
7350
7351 name = namespace_name (die, &is_anonymous, cu);
7352
7353 /* Now build the name of the current namespace. */
7354
7355 previous_prefix = determine_prefix (die, cu);
7356 if (previous_prefix[0] != '\0')
7357 name = typename_concat (&objfile->objfile_obstack,
7358 previous_prefix, name, 0, cu);
7359
7360 /* Create the type. */
7361 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7362 objfile);
7363 TYPE_NAME (type) = (char *) name;
7364 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7365
7366 return set_die_type (die, type, cu);
7367 }
7368
7369 /* Read a C++ namespace. */
7370
7371 static void
7372 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7373 {
7374 struct objfile *objfile = cu->objfile;
7375 const char *name;
7376 int is_anonymous;
7377
7378 /* Add a symbol associated to this if we haven't seen the namespace
7379 before. Also, add a using directive if it's an anonymous
7380 namespace. */
7381
7382 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7383 {
7384 struct type *type;
7385
7386 type = read_type_die (die, cu);
7387 new_symbol (die, type, cu);
7388
7389 name = namespace_name (die, &is_anonymous, cu);
7390 if (is_anonymous)
7391 {
7392 const char *previous_prefix = determine_prefix (die, cu);
7393
7394 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7395 NULL, &objfile->objfile_obstack);
7396 }
7397 }
7398
7399 if (die->child != NULL)
7400 {
7401 struct die_info *child_die = die->child;
7402
7403 while (child_die && child_die->tag)
7404 {
7405 process_die (child_die, cu);
7406 child_die = sibling_die (child_die);
7407 }
7408 }
7409 }
7410
7411 /* Read a Fortran module as type. This DIE can be only a declaration used for
7412 imported module. Still we need that type as local Fortran "use ... only"
7413 declaration imports depend on the created type in determine_prefix. */
7414
7415 static struct type *
7416 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7417 {
7418 struct objfile *objfile = cu->objfile;
7419 char *module_name;
7420 struct type *type;
7421
7422 module_name = dwarf2_name (die, cu);
7423 if (!module_name)
7424 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7425 die->offset);
7426 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7427
7428 /* determine_prefix uses TYPE_TAG_NAME. */
7429 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7430
7431 return set_die_type (die, type, cu);
7432 }
7433
7434 /* Read a Fortran module. */
7435
7436 static void
7437 read_module (struct die_info *die, struct dwarf2_cu *cu)
7438 {
7439 struct die_info *child_die = die->child;
7440
7441 while (child_die && child_die->tag)
7442 {
7443 process_die (child_die, cu);
7444 child_die = sibling_die (child_die);
7445 }
7446 }
7447
7448 /* Return the name of the namespace represented by DIE. Set
7449 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7450 namespace. */
7451
7452 static const char *
7453 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7454 {
7455 struct die_info *current_die;
7456 const char *name = NULL;
7457
7458 /* Loop through the extensions until we find a name. */
7459
7460 for (current_die = die;
7461 current_die != NULL;
7462 current_die = dwarf2_extension (die, &cu))
7463 {
7464 name = dwarf2_name (current_die, cu);
7465 if (name != NULL)
7466 break;
7467 }
7468
7469 /* Is it an anonymous namespace? */
7470
7471 *is_anonymous = (name == NULL);
7472 if (*is_anonymous)
7473 name = "(anonymous namespace)";
7474
7475 return name;
7476 }
7477
7478 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7479 the user defined type vector. */
7480
7481 static struct type *
7482 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7483 {
7484 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7485 struct comp_unit_head *cu_header = &cu->header;
7486 struct type *type;
7487 struct attribute *attr_byte_size;
7488 struct attribute *attr_address_class;
7489 int byte_size, addr_class;
7490 struct type *target_type;
7491
7492 target_type = die_type (die, cu);
7493
7494 /* The die_type call above may have already set the type for this DIE. */
7495 type = get_die_type (die, cu);
7496 if (type)
7497 return type;
7498
7499 type = lookup_pointer_type (target_type);
7500
7501 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7502 if (attr_byte_size)
7503 byte_size = DW_UNSND (attr_byte_size);
7504 else
7505 byte_size = cu_header->addr_size;
7506
7507 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7508 if (attr_address_class)
7509 addr_class = DW_UNSND (attr_address_class);
7510 else
7511 addr_class = DW_ADDR_none;
7512
7513 /* If the pointer size or address class is different than the
7514 default, create a type variant marked as such and set the
7515 length accordingly. */
7516 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7517 {
7518 if (gdbarch_address_class_type_flags_p (gdbarch))
7519 {
7520 int type_flags;
7521
7522 type_flags = gdbarch_address_class_type_flags
7523 (gdbarch, byte_size, addr_class);
7524 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7525 == 0);
7526 type = make_type_with_address_space (type, type_flags);
7527 }
7528 else if (TYPE_LENGTH (type) != byte_size)
7529 {
7530 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7531 }
7532 else
7533 {
7534 /* Should we also complain about unhandled address classes? */
7535 }
7536 }
7537
7538 TYPE_LENGTH (type) = byte_size;
7539 return set_die_type (die, type, cu);
7540 }
7541
7542 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7543 the user defined type vector. */
7544
7545 static struct type *
7546 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7547 {
7548 struct type *type;
7549 struct type *to_type;
7550 struct type *domain;
7551
7552 to_type = die_type (die, cu);
7553 domain = die_containing_type (die, cu);
7554
7555 /* The calls above may have already set the type for this DIE. */
7556 type = get_die_type (die, cu);
7557 if (type)
7558 return type;
7559
7560 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7561 type = lookup_methodptr_type (to_type);
7562 else
7563 type = lookup_memberptr_type (to_type, domain);
7564
7565 return set_die_type (die, type, cu);
7566 }
7567
7568 /* Extract all information from a DW_TAG_reference_type DIE and add to
7569 the user defined type vector. */
7570
7571 static struct type *
7572 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7573 {
7574 struct comp_unit_head *cu_header = &cu->header;
7575 struct type *type, *target_type;
7576 struct attribute *attr;
7577
7578 target_type = die_type (die, cu);
7579
7580 /* The die_type call above may have already set the type for this DIE. */
7581 type = get_die_type (die, cu);
7582 if (type)
7583 return type;
7584
7585 type = lookup_reference_type (target_type);
7586 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7587 if (attr)
7588 {
7589 TYPE_LENGTH (type) = DW_UNSND (attr);
7590 }
7591 else
7592 {
7593 TYPE_LENGTH (type) = cu_header->addr_size;
7594 }
7595 return set_die_type (die, type, cu);
7596 }
7597
7598 static struct type *
7599 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7600 {
7601 struct type *base_type, *cv_type;
7602
7603 base_type = die_type (die, cu);
7604
7605 /* The die_type call above may have already set the type for this DIE. */
7606 cv_type = get_die_type (die, cu);
7607 if (cv_type)
7608 return cv_type;
7609
7610 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7611 return set_die_type (die, cv_type, cu);
7612 }
7613
7614 static struct type *
7615 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7616 {
7617 struct type *base_type, *cv_type;
7618
7619 base_type = die_type (die, cu);
7620
7621 /* The die_type call above may have already set the type for this DIE. */
7622 cv_type = get_die_type (die, cu);
7623 if (cv_type)
7624 return cv_type;
7625
7626 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7627 return set_die_type (die, cv_type, cu);
7628 }
7629
7630 /* Extract all information from a DW_TAG_string_type DIE and add to
7631 the user defined type vector. It isn't really a user defined type,
7632 but it behaves like one, with other DIE's using an AT_user_def_type
7633 attribute to reference it. */
7634
7635 static struct type *
7636 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7637 {
7638 struct objfile *objfile = cu->objfile;
7639 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7640 struct type *type, *range_type, *index_type, *char_type;
7641 struct attribute *attr;
7642 unsigned int length;
7643
7644 attr = dwarf2_attr (die, DW_AT_string_length, cu);
7645 if (attr)
7646 {
7647 length = DW_UNSND (attr);
7648 }
7649 else
7650 {
7651 /* check for the DW_AT_byte_size attribute */
7652 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7653 if (attr)
7654 {
7655 length = DW_UNSND (attr);
7656 }
7657 else
7658 {
7659 length = 1;
7660 }
7661 }
7662
7663 index_type = objfile_type (objfile)->builtin_int;
7664 range_type = create_range_type (NULL, index_type, 1, length);
7665 char_type = language_string_char_type (cu->language_defn, gdbarch);
7666 type = create_string_type (NULL, char_type, range_type);
7667
7668 return set_die_type (die, type, cu);
7669 }
7670
7671 /* Handle DIES due to C code like:
7672
7673 struct foo
7674 {
7675 int (*funcp)(int a, long l);
7676 int b;
7677 };
7678
7679 ('funcp' generates a DW_TAG_subroutine_type DIE)
7680 */
7681
7682 static struct type *
7683 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7684 {
7685 struct type *type; /* Type that this function returns */
7686 struct type *ftype; /* Function that returns above type */
7687 struct attribute *attr;
7688
7689 type = die_type (die, cu);
7690
7691 /* The die_type call above may have already set the type for this DIE. */
7692 ftype = get_die_type (die, cu);
7693 if (ftype)
7694 return ftype;
7695
7696 ftype = lookup_function_type (type);
7697
7698 /* All functions in C++, Pascal and Java have prototypes. */
7699 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7700 if ((attr && (DW_UNSND (attr) != 0))
7701 || cu->language == language_cplus
7702 || cu->language == language_java
7703 || cu->language == language_pascal)
7704 TYPE_PROTOTYPED (ftype) = 1;
7705 else if (producer_is_realview (cu->producer))
7706 /* RealView does not emit DW_AT_prototyped. We can not
7707 distinguish prototyped and unprototyped functions; default to
7708 prototyped, since that is more common in modern code (and
7709 RealView warns about unprototyped functions). */
7710 TYPE_PROTOTYPED (ftype) = 1;
7711
7712 /* Store the calling convention in the type if it's available in
7713 the subroutine die. Otherwise set the calling convention to
7714 the default value DW_CC_normal. */
7715 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7716 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7717
7718 /* We need to add the subroutine type to the die immediately so
7719 we don't infinitely recurse when dealing with parameters
7720 declared as the same subroutine type. */
7721 set_die_type (die, ftype, cu);
7722
7723 if (die->child != NULL)
7724 {
7725 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7726 struct die_info *child_die;
7727 int nparams, iparams;
7728
7729 /* Count the number of parameters.
7730 FIXME: GDB currently ignores vararg functions, but knows about
7731 vararg member functions. */
7732 nparams = 0;
7733 child_die = die->child;
7734 while (child_die && child_die->tag)
7735 {
7736 if (child_die->tag == DW_TAG_formal_parameter)
7737 nparams++;
7738 else if (child_die->tag == DW_TAG_unspecified_parameters)
7739 TYPE_VARARGS (ftype) = 1;
7740 child_die = sibling_die (child_die);
7741 }
7742
7743 /* Allocate storage for parameters and fill them in. */
7744 TYPE_NFIELDS (ftype) = nparams;
7745 TYPE_FIELDS (ftype) = (struct field *)
7746 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7747
7748 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7749 even if we error out during the parameters reading below. */
7750 for (iparams = 0; iparams < nparams; iparams++)
7751 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7752
7753 iparams = 0;
7754 child_die = die->child;
7755 while (child_die && child_die->tag)
7756 {
7757 if (child_die->tag == DW_TAG_formal_parameter)
7758 {
7759 struct type *arg_type;
7760
7761 /* DWARF version 2 has no clean way to discern C++
7762 static and non-static member functions. G++ helps
7763 GDB by marking the first parameter for non-static
7764 member functions (which is the this pointer) as
7765 artificial. We pass this information to
7766 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7767
7768 DWARF version 3 added DW_AT_object_pointer, which GCC
7769 4.5 does not yet generate. */
7770 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7771 if (attr)
7772 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7773 else
7774 {
7775 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7776
7777 /* GCC/43521: In java, the formal parameter
7778 "this" is sometimes not marked with DW_AT_artificial. */
7779 if (cu->language == language_java)
7780 {
7781 const char *name = dwarf2_name (child_die, cu);
7782
7783 if (name && !strcmp (name, "this"))
7784 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7785 }
7786 }
7787 arg_type = die_type (child_die, cu);
7788
7789 /* RealView does not mark THIS as const, which the testsuite
7790 expects. GCC marks THIS as const in method definitions,
7791 but not in the class specifications (GCC PR 43053). */
7792 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7793 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7794 {
7795 int is_this = 0;
7796 struct dwarf2_cu *arg_cu = cu;
7797 const char *name = dwarf2_name (child_die, cu);
7798
7799 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7800 if (attr)
7801 {
7802 /* If the compiler emits this, use it. */
7803 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7804 is_this = 1;
7805 }
7806 else if (name && strcmp (name, "this") == 0)
7807 /* Function definitions will have the argument names. */
7808 is_this = 1;
7809 else if (name == NULL && iparams == 0)
7810 /* Declarations may not have the names, so like
7811 elsewhere in GDB, assume an artificial first
7812 argument is "this". */
7813 is_this = 1;
7814
7815 if (is_this)
7816 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7817 arg_type, 0);
7818 }
7819
7820 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7821 iparams++;
7822 }
7823 child_die = sibling_die (child_die);
7824 }
7825 }
7826
7827 return ftype;
7828 }
7829
7830 static struct type *
7831 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7832 {
7833 struct objfile *objfile = cu->objfile;
7834 const char *name = NULL;
7835 struct type *this_type;
7836
7837 name = dwarf2_full_name (NULL, die, cu);
7838 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7839 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7840 TYPE_NAME (this_type) = (char *) name;
7841 set_die_type (die, this_type, cu);
7842 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7843 return this_type;
7844 }
7845
7846 /* Find a representation of a given base type and install
7847 it in the TYPE field of the die. */
7848
7849 static struct type *
7850 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7851 {
7852 struct objfile *objfile = cu->objfile;
7853 struct type *type;
7854 struct attribute *attr;
7855 int encoding = 0, size = 0;
7856 char *name;
7857 enum type_code code = TYPE_CODE_INT;
7858 int type_flags = 0;
7859 struct type *target_type = NULL;
7860
7861 attr = dwarf2_attr (die, DW_AT_encoding, cu);
7862 if (attr)
7863 {
7864 encoding = DW_UNSND (attr);
7865 }
7866 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7867 if (attr)
7868 {
7869 size = DW_UNSND (attr);
7870 }
7871 name = dwarf2_name (die, cu);
7872 if (!name)
7873 {
7874 complaint (&symfile_complaints,
7875 _("DW_AT_name missing from DW_TAG_base_type"));
7876 }
7877
7878 switch (encoding)
7879 {
7880 case DW_ATE_address:
7881 /* Turn DW_ATE_address into a void * pointer. */
7882 code = TYPE_CODE_PTR;
7883 type_flags |= TYPE_FLAG_UNSIGNED;
7884 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7885 break;
7886 case DW_ATE_boolean:
7887 code = TYPE_CODE_BOOL;
7888 type_flags |= TYPE_FLAG_UNSIGNED;
7889 break;
7890 case DW_ATE_complex_float:
7891 code = TYPE_CODE_COMPLEX;
7892 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7893 break;
7894 case DW_ATE_decimal_float:
7895 code = TYPE_CODE_DECFLOAT;
7896 break;
7897 case DW_ATE_float:
7898 code = TYPE_CODE_FLT;
7899 break;
7900 case DW_ATE_signed:
7901 break;
7902 case DW_ATE_unsigned:
7903 type_flags |= TYPE_FLAG_UNSIGNED;
7904 break;
7905 case DW_ATE_signed_char:
7906 if (cu->language == language_ada || cu->language == language_m2
7907 || cu->language == language_pascal)
7908 code = TYPE_CODE_CHAR;
7909 break;
7910 case DW_ATE_unsigned_char:
7911 if (cu->language == language_ada || cu->language == language_m2
7912 || cu->language == language_pascal)
7913 code = TYPE_CODE_CHAR;
7914 type_flags |= TYPE_FLAG_UNSIGNED;
7915 break;
7916 case DW_ATE_UTF:
7917 /* We just treat this as an integer and then recognize the
7918 type by name elsewhere. */
7919 break;
7920
7921 default:
7922 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7923 dwarf_type_encoding_name (encoding));
7924 break;
7925 }
7926
7927 type = init_type (code, size, type_flags, NULL, objfile);
7928 TYPE_NAME (type) = name;
7929 TYPE_TARGET_TYPE (type) = target_type;
7930
7931 if (name && strcmp (name, "char") == 0)
7932 TYPE_NOSIGN (type) = 1;
7933
7934 return set_die_type (die, type, cu);
7935 }
7936
7937 /* Read the given DW_AT_subrange DIE. */
7938
7939 static struct type *
7940 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7941 {
7942 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7943 struct type *base_type;
7944 struct type *range_type;
7945 struct attribute *attr;
7946 LONGEST low = 0;
7947 LONGEST high = -1;
7948 char *name;
7949 LONGEST negative_mask;
7950
7951 base_type = die_type (die, cu);
7952 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7953 check_typedef (base_type);
7954
7955 /* The die_type call above may have already set the type for this DIE. */
7956 range_type = get_die_type (die, cu);
7957 if (range_type)
7958 return range_type;
7959
7960 if (cu->language == language_fortran)
7961 {
7962 /* FORTRAN implies a lower bound of 1, if not given. */
7963 low = 1;
7964 }
7965
7966 /* FIXME: For variable sized arrays either of these could be
7967 a variable rather than a constant value. We'll allow it,
7968 but we don't know how to handle it. */
7969 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7970 if (attr)
7971 low = dwarf2_get_attr_constant_value (attr, 0);
7972
7973 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7974 if (attr)
7975 {
7976 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7977 {
7978 /* GCC encodes arrays with unspecified or dynamic length
7979 with a DW_FORM_block1 attribute or a reference attribute.
7980 FIXME: GDB does not yet know how to handle dynamic
7981 arrays properly, treat them as arrays with unspecified
7982 length for now.
7983
7984 FIXME: jimb/2003-09-22: GDB does not really know
7985 how to handle arrays of unspecified length
7986 either; we just represent them as zero-length
7987 arrays. Choose an appropriate upper bound given
7988 the lower bound we've computed above. */
7989 high = low - 1;
7990 }
7991 else
7992 high = dwarf2_get_attr_constant_value (attr, 1);
7993 }
7994 else
7995 {
7996 attr = dwarf2_attr (die, DW_AT_count, cu);
7997 if (attr)
7998 {
7999 int count = dwarf2_get_attr_constant_value (attr, 1);
8000 high = low + count - 1;
8001 }
8002 }
8003
8004 /* Dwarf-2 specifications explicitly allows to create subrange types
8005 without specifying a base type.
8006 In that case, the base type must be set to the type of
8007 the lower bound, upper bound or count, in that order, if any of these
8008 three attributes references an object that has a type.
8009 If no base type is found, the Dwarf-2 specifications say that
8010 a signed integer type of size equal to the size of an address should
8011 be used.
8012 For the following C code: `extern char gdb_int [];'
8013 GCC produces an empty range DIE.
8014 FIXME: muller/2010-05-28: Possible references to object for low bound,
8015 high bound or count are not yet handled by this code.
8016 */
8017 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8018 {
8019 struct objfile *objfile = cu->objfile;
8020 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8021 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8022 struct type *int_type = objfile_type (objfile)->builtin_int;
8023
8024 /* Test "int", "long int", and "long long int" objfile types,
8025 and select the first one having a size above or equal to the
8026 architecture address size. */
8027 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8028 base_type = int_type;
8029 else
8030 {
8031 int_type = objfile_type (objfile)->builtin_long;
8032 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8033 base_type = int_type;
8034 else
8035 {
8036 int_type = objfile_type (objfile)->builtin_long_long;
8037 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8038 base_type = int_type;
8039 }
8040 }
8041 }
8042
8043 negative_mask =
8044 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8045 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8046 low |= negative_mask;
8047 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8048 high |= negative_mask;
8049
8050 range_type = create_range_type (NULL, base_type, low, high);
8051
8052 /* Mark arrays with dynamic length at least as an array of unspecified
8053 length. GDB could check the boundary but before it gets implemented at
8054 least allow accessing the array elements. */
8055 if (attr && attr->form == DW_FORM_block1)
8056 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8057
8058 name = dwarf2_name (die, cu);
8059 if (name)
8060 TYPE_NAME (range_type) = name;
8061
8062 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8063 if (attr)
8064 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8065
8066 set_die_type (die, range_type, cu);
8067
8068 /* set_die_type should be already done. */
8069 set_descriptive_type (range_type, die, cu);
8070
8071 return range_type;
8072 }
8073
8074 static struct type *
8075 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8076 {
8077 struct type *type;
8078
8079 /* For now, we only support the C meaning of an unspecified type: void. */
8080
8081 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8082 TYPE_NAME (type) = dwarf2_name (die, cu);
8083
8084 return set_die_type (die, type, cu);
8085 }
8086
8087 /* Trivial hash function for die_info: the hash value of a DIE
8088 is its offset in .debug_info for this objfile. */
8089
8090 static hashval_t
8091 die_hash (const void *item)
8092 {
8093 const struct die_info *die = item;
8094
8095 return die->offset;
8096 }
8097
8098 /* Trivial comparison function for die_info structures: two DIEs
8099 are equal if they have the same offset. */
8100
8101 static int
8102 die_eq (const void *item_lhs, const void *item_rhs)
8103 {
8104 const struct die_info *die_lhs = item_lhs;
8105 const struct die_info *die_rhs = item_rhs;
8106
8107 return die_lhs->offset == die_rhs->offset;
8108 }
8109
8110 /* Read a whole compilation unit into a linked list of dies. */
8111
8112 static struct die_info *
8113 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8114 {
8115 struct die_reader_specs reader_specs;
8116 int read_abbrevs = 0;
8117 struct cleanup *back_to = NULL;
8118 struct die_info *die;
8119
8120 if (cu->dwarf2_abbrevs == NULL)
8121 {
8122 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8123 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8124 read_abbrevs = 1;
8125 }
8126
8127 gdb_assert (cu->die_hash == NULL);
8128 cu->die_hash
8129 = htab_create_alloc_ex (cu->header.length / 12,
8130 die_hash,
8131 die_eq,
8132 NULL,
8133 &cu->comp_unit_obstack,
8134 hashtab_obstack_allocate,
8135 dummy_obstack_deallocate);
8136
8137 init_cu_die_reader (&reader_specs, cu);
8138
8139 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8140
8141 if (read_abbrevs)
8142 do_cleanups (back_to);
8143
8144 return die;
8145 }
8146
8147 /* Main entry point for reading a DIE and all children.
8148 Read the DIE and dump it if requested. */
8149
8150 static struct die_info *
8151 read_die_and_children (const struct die_reader_specs *reader,
8152 gdb_byte *info_ptr,
8153 gdb_byte **new_info_ptr,
8154 struct die_info *parent)
8155 {
8156 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8157 new_info_ptr, parent);
8158
8159 if (dwarf2_die_debug)
8160 {
8161 fprintf_unfiltered (gdb_stdlog,
8162 "\nRead die from %s of %s:\n",
8163 reader->buffer == dwarf2_per_objfile->info.buffer
8164 ? ".debug_info"
8165 : reader->buffer == dwarf2_per_objfile->types.buffer
8166 ? ".debug_types"
8167 : "unknown section",
8168 reader->abfd->filename);
8169 dump_die (result, dwarf2_die_debug);
8170 }
8171
8172 return result;
8173 }
8174
8175 /* Read a single die and all its descendents. Set the die's sibling
8176 field to NULL; set other fields in the die correctly, and set all
8177 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8178 location of the info_ptr after reading all of those dies. PARENT
8179 is the parent of the die in question. */
8180
8181 static struct die_info *
8182 read_die_and_children_1 (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 *die;
8188 gdb_byte *cur_ptr;
8189 int has_children;
8190
8191 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8192 if (die == NULL)
8193 {
8194 *new_info_ptr = cur_ptr;
8195 return NULL;
8196 }
8197 store_in_ref_table (die, reader->cu);
8198
8199 if (has_children)
8200 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8201 else
8202 {
8203 die->child = NULL;
8204 *new_info_ptr = cur_ptr;
8205 }
8206
8207 die->sibling = NULL;
8208 die->parent = parent;
8209 return die;
8210 }
8211
8212 /* Read a die, all of its descendents, and all of its siblings; set
8213 all of the fields of all of the dies correctly. Arguments are as
8214 in read_die_and_children. */
8215
8216 static struct die_info *
8217 read_die_and_siblings (const struct die_reader_specs *reader,
8218 gdb_byte *info_ptr,
8219 gdb_byte **new_info_ptr,
8220 struct die_info *parent)
8221 {
8222 struct die_info *first_die, *last_sibling;
8223 gdb_byte *cur_ptr;
8224
8225 cur_ptr = info_ptr;
8226 first_die = last_sibling = NULL;
8227
8228 while (1)
8229 {
8230 struct die_info *die
8231 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8232
8233 if (die == NULL)
8234 {
8235 *new_info_ptr = cur_ptr;
8236 return first_die;
8237 }
8238
8239 if (!first_die)
8240 first_die = die;
8241 else
8242 last_sibling->sibling = die;
8243
8244 last_sibling = die;
8245 }
8246 }
8247
8248 /* Read the die from the .debug_info section buffer. Set DIEP to
8249 point to a newly allocated die with its information, except for its
8250 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8251 whether the die has children or not. */
8252
8253 static gdb_byte *
8254 read_full_die (const struct die_reader_specs *reader,
8255 struct die_info **diep, gdb_byte *info_ptr,
8256 int *has_children)
8257 {
8258 unsigned int abbrev_number, bytes_read, i, offset;
8259 struct abbrev_info *abbrev;
8260 struct die_info *die;
8261 struct dwarf2_cu *cu = reader->cu;
8262 bfd *abfd = reader->abfd;
8263
8264 offset = info_ptr - reader->buffer;
8265 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8266 info_ptr += bytes_read;
8267 if (!abbrev_number)
8268 {
8269 *diep = NULL;
8270 *has_children = 0;
8271 return info_ptr;
8272 }
8273
8274 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8275 if (!abbrev)
8276 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8277 abbrev_number,
8278 bfd_get_filename (abfd));
8279
8280 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8281 die->offset = offset;
8282 die->tag = abbrev->tag;
8283 die->abbrev = abbrev_number;
8284
8285 die->num_attrs = abbrev->num_attrs;
8286
8287 for (i = 0; i < abbrev->num_attrs; ++i)
8288 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8289 abfd, info_ptr, cu);
8290
8291 *diep = die;
8292 *has_children = abbrev->has_children;
8293 return info_ptr;
8294 }
8295
8296 /* In DWARF version 2, the description of the debugging information is
8297 stored in a separate .debug_abbrev section. Before we read any
8298 dies from a section we read in all abbreviations and install them
8299 in a hash table. This function also sets flags in CU describing
8300 the data found in the abbrev table. */
8301
8302 static void
8303 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8304 {
8305 struct comp_unit_head *cu_header = &cu->header;
8306 gdb_byte *abbrev_ptr;
8307 struct abbrev_info *cur_abbrev;
8308 unsigned int abbrev_number, bytes_read, abbrev_name;
8309 unsigned int abbrev_form, hash_number;
8310 struct attr_abbrev *cur_attrs;
8311 unsigned int allocated_attrs;
8312
8313 /* Initialize dwarf2 abbrevs */
8314 obstack_init (&cu->abbrev_obstack);
8315 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8316 (ABBREV_HASH_SIZE
8317 * sizeof (struct abbrev_info *)));
8318 memset (cu->dwarf2_abbrevs, 0,
8319 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8320
8321 dwarf2_read_section (dwarf2_per_objfile->objfile,
8322 &dwarf2_per_objfile->abbrev);
8323 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8324 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8325 abbrev_ptr += bytes_read;
8326
8327 allocated_attrs = ATTR_ALLOC_CHUNK;
8328 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8329
8330 /* loop until we reach an abbrev number of 0 */
8331 while (abbrev_number)
8332 {
8333 cur_abbrev = dwarf_alloc_abbrev (cu);
8334
8335 /* read in abbrev header */
8336 cur_abbrev->number = abbrev_number;
8337 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8338 abbrev_ptr += bytes_read;
8339 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8340 abbrev_ptr += 1;
8341
8342 if (cur_abbrev->tag == DW_TAG_namespace)
8343 cu->has_namespace_info = 1;
8344
8345 /* now read in declarations */
8346 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8347 abbrev_ptr += bytes_read;
8348 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8349 abbrev_ptr += bytes_read;
8350 while (abbrev_name)
8351 {
8352 if (cur_abbrev->num_attrs == allocated_attrs)
8353 {
8354 allocated_attrs += ATTR_ALLOC_CHUNK;
8355 cur_attrs
8356 = xrealloc (cur_attrs, (allocated_attrs
8357 * sizeof (struct attr_abbrev)));
8358 }
8359
8360 /* Record whether this compilation unit might have
8361 inter-compilation-unit references. If we don't know what form
8362 this attribute will have, then it might potentially be a
8363 DW_FORM_ref_addr, so we conservatively expect inter-CU
8364 references. */
8365
8366 if (abbrev_form == DW_FORM_ref_addr
8367 || abbrev_form == DW_FORM_indirect)
8368 cu->has_form_ref_addr = 1;
8369
8370 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8371 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8372 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8373 abbrev_ptr += bytes_read;
8374 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8375 abbrev_ptr += bytes_read;
8376 }
8377
8378 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8379 (cur_abbrev->num_attrs
8380 * sizeof (struct attr_abbrev)));
8381 memcpy (cur_abbrev->attrs, cur_attrs,
8382 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8383
8384 hash_number = abbrev_number % ABBREV_HASH_SIZE;
8385 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8386 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8387
8388 /* Get next abbreviation.
8389 Under Irix6 the abbreviations for a compilation unit are not
8390 always properly terminated with an abbrev number of 0.
8391 Exit loop if we encounter an abbreviation which we have
8392 already read (which means we are about to read the abbreviations
8393 for the next compile unit) or if the end of the abbreviation
8394 table is reached. */
8395 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8396 >= dwarf2_per_objfile->abbrev.size)
8397 break;
8398 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8399 abbrev_ptr += bytes_read;
8400 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8401 break;
8402 }
8403
8404 xfree (cur_attrs);
8405 }
8406
8407 /* Release the memory used by the abbrev table for a compilation unit. */
8408
8409 static void
8410 dwarf2_free_abbrev_table (void *ptr_to_cu)
8411 {
8412 struct dwarf2_cu *cu = ptr_to_cu;
8413
8414 obstack_free (&cu->abbrev_obstack, NULL);
8415 cu->dwarf2_abbrevs = NULL;
8416 }
8417
8418 /* Lookup an abbrev_info structure in the abbrev hash table. */
8419
8420 static struct abbrev_info *
8421 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8422 {
8423 unsigned int hash_number;
8424 struct abbrev_info *abbrev;
8425
8426 hash_number = number % ABBREV_HASH_SIZE;
8427 abbrev = cu->dwarf2_abbrevs[hash_number];
8428
8429 while (abbrev)
8430 {
8431 if (abbrev->number == number)
8432 return abbrev;
8433 else
8434 abbrev = abbrev->next;
8435 }
8436 return NULL;
8437 }
8438
8439 /* Returns nonzero if TAG represents a type that we might generate a partial
8440 symbol for. */
8441
8442 static int
8443 is_type_tag_for_partial (int tag)
8444 {
8445 switch (tag)
8446 {
8447 #if 0
8448 /* Some types that would be reasonable to generate partial symbols for,
8449 that we don't at present. */
8450 case DW_TAG_array_type:
8451 case DW_TAG_file_type:
8452 case DW_TAG_ptr_to_member_type:
8453 case DW_TAG_set_type:
8454 case DW_TAG_string_type:
8455 case DW_TAG_subroutine_type:
8456 #endif
8457 case DW_TAG_base_type:
8458 case DW_TAG_class_type:
8459 case DW_TAG_interface_type:
8460 case DW_TAG_enumeration_type:
8461 case DW_TAG_structure_type:
8462 case DW_TAG_subrange_type:
8463 case DW_TAG_typedef:
8464 case DW_TAG_union_type:
8465 return 1;
8466 default:
8467 return 0;
8468 }
8469 }
8470
8471 /* Load all DIEs that are interesting for partial symbols into memory. */
8472
8473 static struct partial_die_info *
8474 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8475 int building_psymtab, struct dwarf2_cu *cu)
8476 {
8477 struct partial_die_info *part_die;
8478 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8479 struct abbrev_info *abbrev;
8480 unsigned int bytes_read;
8481 unsigned int load_all = 0;
8482
8483 int nesting_level = 1;
8484
8485 parent_die = NULL;
8486 last_die = NULL;
8487
8488 if (cu->per_cu && cu->per_cu->load_all_dies)
8489 load_all = 1;
8490
8491 cu->partial_dies
8492 = htab_create_alloc_ex (cu->header.length / 12,
8493 partial_die_hash,
8494 partial_die_eq,
8495 NULL,
8496 &cu->comp_unit_obstack,
8497 hashtab_obstack_allocate,
8498 dummy_obstack_deallocate);
8499
8500 part_die = obstack_alloc (&cu->comp_unit_obstack,
8501 sizeof (struct partial_die_info));
8502
8503 while (1)
8504 {
8505 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8506
8507 /* A NULL abbrev means the end of a series of children. */
8508 if (abbrev == NULL)
8509 {
8510 if (--nesting_level == 0)
8511 {
8512 /* PART_DIE was probably the last thing allocated on the
8513 comp_unit_obstack, so we could call obstack_free
8514 here. We don't do that because the waste is small,
8515 and will be cleaned up when we're done with this
8516 compilation unit. This way, we're also more robust
8517 against other users of the comp_unit_obstack. */
8518 return first_die;
8519 }
8520 info_ptr += bytes_read;
8521 last_die = parent_die;
8522 parent_die = parent_die->die_parent;
8523 continue;
8524 }
8525
8526 /* Check for template arguments. We never save these; if
8527 they're seen, we just mark the parent, and go on our way. */
8528 if (parent_die != NULL
8529 && cu->language == language_cplus
8530 && (abbrev->tag == DW_TAG_template_type_param
8531 || abbrev->tag == DW_TAG_template_value_param))
8532 {
8533 parent_die->has_template_arguments = 1;
8534
8535 if (!load_all)
8536 {
8537 /* We don't need a partial DIE for the template argument. */
8538 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8539 cu);
8540 continue;
8541 }
8542 }
8543
8544 /* We only recurse into subprograms looking for template arguments.
8545 Skip their other children. */
8546 if (!load_all
8547 && cu->language == language_cplus
8548 && parent_die != NULL
8549 && parent_die->tag == DW_TAG_subprogram)
8550 {
8551 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8552 continue;
8553 }
8554
8555 /* Check whether this DIE is interesting enough to save. Normally
8556 we would not be interested in members here, but there may be
8557 later variables referencing them via DW_AT_specification (for
8558 static members). */
8559 if (!load_all
8560 && !is_type_tag_for_partial (abbrev->tag)
8561 && abbrev->tag != DW_TAG_constant
8562 && abbrev->tag != DW_TAG_enumerator
8563 && abbrev->tag != DW_TAG_subprogram
8564 && abbrev->tag != DW_TAG_lexical_block
8565 && abbrev->tag != DW_TAG_variable
8566 && abbrev->tag != DW_TAG_namespace
8567 && abbrev->tag != DW_TAG_module
8568 && abbrev->tag != DW_TAG_member)
8569 {
8570 /* Otherwise we skip to the next sibling, if any. */
8571 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8572 continue;
8573 }
8574
8575 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8576 buffer, info_ptr, cu);
8577
8578 /* This two-pass algorithm for processing partial symbols has a
8579 high cost in cache pressure. Thus, handle some simple cases
8580 here which cover the majority of C partial symbols. DIEs
8581 which neither have specification tags in them, nor could have
8582 specification tags elsewhere pointing at them, can simply be
8583 processed and discarded.
8584
8585 This segment is also optional; scan_partial_symbols and
8586 add_partial_symbol will handle these DIEs if we chain
8587 them in normally. When compilers which do not emit large
8588 quantities of duplicate debug information are more common,
8589 this code can probably be removed. */
8590
8591 /* Any complete simple types at the top level (pretty much all
8592 of them, for a language without namespaces), can be processed
8593 directly. */
8594 if (parent_die == NULL
8595 && part_die->has_specification == 0
8596 && part_die->is_declaration == 0
8597 && (part_die->tag == DW_TAG_typedef
8598 || part_die->tag == DW_TAG_base_type
8599 || part_die->tag == DW_TAG_subrange_type))
8600 {
8601 if (building_psymtab && part_die->name != NULL)
8602 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8603 VAR_DOMAIN, LOC_TYPEDEF,
8604 &cu->objfile->static_psymbols,
8605 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8606 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8607 continue;
8608 }
8609
8610 /* If we're at the second level, and we're an enumerator, and
8611 our parent has no specification (meaning possibly lives in a
8612 namespace elsewhere), then we can add the partial symbol now
8613 instead of queueing it. */
8614 if (part_die->tag == DW_TAG_enumerator
8615 && parent_die != NULL
8616 && parent_die->die_parent == NULL
8617 && parent_die->tag == DW_TAG_enumeration_type
8618 && parent_die->has_specification == 0)
8619 {
8620 if (part_die->name == NULL)
8621 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8622 else if (building_psymtab)
8623 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8624 VAR_DOMAIN, LOC_CONST,
8625 (cu->language == language_cplus
8626 || cu->language == language_java)
8627 ? &cu->objfile->global_psymbols
8628 : &cu->objfile->static_psymbols,
8629 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8630
8631 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8632 continue;
8633 }
8634
8635 /* We'll save this DIE so link it in. */
8636 part_die->die_parent = parent_die;
8637 part_die->die_sibling = NULL;
8638 part_die->die_child = NULL;
8639
8640 if (last_die && last_die == parent_die)
8641 last_die->die_child = part_die;
8642 else if (last_die)
8643 last_die->die_sibling = part_die;
8644
8645 last_die = part_die;
8646
8647 if (first_die == NULL)
8648 first_die = part_die;
8649
8650 /* Maybe add the DIE to the hash table. Not all DIEs that we
8651 find interesting need to be in the hash table, because we
8652 also have the parent/sibling/child chains; only those that we
8653 might refer to by offset later during partial symbol reading.
8654
8655 For now this means things that might have be the target of a
8656 DW_AT_specification, DW_AT_abstract_origin, or
8657 DW_AT_extension. DW_AT_extension will refer only to
8658 namespaces; DW_AT_abstract_origin refers to functions (and
8659 many things under the function DIE, but we do not recurse
8660 into function DIEs during partial symbol reading) and
8661 possibly variables as well; DW_AT_specification refers to
8662 declarations. Declarations ought to have the DW_AT_declaration
8663 flag. It happens that GCC forgets to put it in sometimes, but
8664 only for functions, not for types.
8665
8666 Adding more things than necessary to the hash table is harmless
8667 except for the performance cost. Adding too few will result in
8668 wasted time in find_partial_die, when we reread the compilation
8669 unit with load_all_dies set. */
8670
8671 if (load_all
8672 || abbrev->tag == DW_TAG_constant
8673 || abbrev->tag == DW_TAG_subprogram
8674 || abbrev->tag == DW_TAG_variable
8675 || abbrev->tag == DW_TAG_namespace
8676 || part_die->is_declaration)
8677 {
8678 void **slot;
8679
8680 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8681 part_die->offset, INSERT);
8682 *slot = part_die;
8683 }
8684
8685 part_die = obstack_alloc (&cu->comp_unit_obstack,
8686 sizeof (struct partial_die_info));
8687
8688 /* For some DIEs we want to follow their children (if any). For C
8689 we have no reason to follow the children of structures; for other
8690 languages we have to, so that we can get at method physnames
8691 to infer fully qualified class names, for DW_AT_specification,
8692 and for C++ template arguments. For C++, we also look one level
8693 inside functions to find template arguments (if the name of the
8694 function does not already contain the template arguments).
8695
8696 For Ada, we need to scan the children of subprograms and lexical
8697 blocks as well because Ada allows the definition of nested
8698 entities that could be interesting for the debugger, such as
8699 nested subprograms for instance. */
8700 if (last_die->has_children
8701 && (load_all
8702 || last_die->tag == DW_TAG_namespace
8703 || last_die->tag == DW_TAG_module
8704 || last_die->tag == DW_TAG_enumeration_type
8705 || (cu->language == language_cplus
8706 && last_die->tag == DW_TAG_subprogram
8707 && (last_die->name == NULL
8708 || strchr (last_die->name, '<') == NULL))
8709 || (cu->language != language_c
8710 && (last_die->tag == DW_TAG_class_type
8711 || last_die->tag == DW_TAG_interface_type
8712 || last_die->tag == DW_TAG_structure_type
8713 || last_die->tag == DW_TAG_union_type))
8714 || (cu->language == language_ada
8715 && (last_die->tag == DW_TAG_subprogram
8716 || last_die->tag == DW_TAG_lexical_block))))
8717 {
8718 nesting_level++;
8719 parent_die = last_die;
8720 continue;
8721 }
8722
8723 /* Otherwise we skip to the next sibling, if any. */
8724 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8725
8726 /* Back to the top, do it again. */
8727 }
8728 }
8729
8730 /* Read a minimal amount of information into the minimal die structure. */
8731
8732 static gdb_byte *
8733 read_partial_die (struct partial_die_info *part_die,
8734 struct abbrev_info *abbrev,
8735 unsigned int abbrev_len, bfd *abfd,
8736 gdb_byte *buffer, gdb_byte *info_ptr,
8737 struct dwarf2_cu *cu)
8738 {
8739 unsigned int i;
8740 struct attribute attr;
8741 int has_low_pc_attr = 0;
8742 int has_high_pc_attr = 0;
8743
8744 memset (part_die, 0, sizeof (struct partial_die_info));
8745
8746 part_die->offset = info_ptr - buffer;
8747
8748 info_ptr += abbrev_len;
8749
8750 if (abbrev == NULL)
8751 return info_ptr;
8752
8753 part_die->tag = abbrev->tag;
8754 part_die->has_children = abbrev->has_children;
8755
8756 for (i = 0; i < abbrev->num_attrs; ++i)
8757 {
8758 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8759
8760 /* Store the data if it is of an attribute we want to keep in a
8761 partial symbol table. */
8762 switch (attr.name)
8763 {
8764 case DW_AT_name:
8765 switch (part_die->tag)
8766 {
8767 case DW_TAG_compile_unit:
8768 case DW_TAG_type_unit:
8769 /* Compilation units have a DW_AT_name that is a filename, not
8770 a source language identifier. */
8771 case DW_TAG_enumeration_type:
8772 case DW_TAG_enumerator:
8773 /* These tags always have simple identifiers already; no need
8774 to canonicalize them. */
8775 part_die->name = DW_STRING (&attr);
8776 break;
8777 default:
8778 part_die->name
8779 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8780 &cu->objfile->objfile_obstack);
8781 break;
8782 }
8783 break;
8784 case DW_AT_linkage_name:
8785 case DW_AT_MIPS_linkage_name:
8786 /* Note that both forms of linkage name might appear. We
8787 assume they will be the same, and we only store the last
8788 one we see. */
8789 if (cu->language == language_ada)
8790 part_die->name = DW_STRING (&attr);
8791 part_die->linkage_name = DW_STRING (&attr);
8792 break;
8793 case DW_AT_low_pc:
8794 has_low_pc_attr = 1;
8795 part_die->lowpc = DW_ADDR (&attr);
8796 break;
8797 case DW_AT_high_pc:
8798 has_high_pc_attr = 1;
8799 part_die->highpc = DW_ADDR (&attr);
8800 break;
8801 case DW_AT_location:
8802 /* Support the .debug_loc offsets */
8803 if (attr_form_is_block (&attr))
8804 {
8805 part_die->locdesc = DW_BLOCK (&attr);
8806 }
8807 else if (attr_form_is_section_offset (&attr))
8808 {
8809 dwarf2_complex_location_expr_complaint ();
8810 }
8811 else
8812 {
8813 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8814 "partial symbol information");
8815 }
8816 break;
8817 case DW_AT_external:
8818 part_die->is_external = DW_UNSND (&attr);
8819 break;
8820 case DW_AT_declaration:
8821 part_die->is_declaration = DW_UNSND (&attr);
8822 break;
8823 case DW_AT_type:
8824 part_die->has_type = 1;
8825 break;
8826 case DW_AT_abstract_origin:
8827 case DW_AT_specification:
8828 case DW_AT_extension:
8829 part_die->has_specification = 1;
8830 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8831 break;
8832 case DW_AT_sibling:
8833 /* Ignore absolute siblings, they might point outside of
8834 the current compile unit. */
8835 if (attr.form == DW_FORM_ref_addr)
8836 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8837 else
8838 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8839 break;
8840 case DW_AT_byte_size:
8841 part_die->has_byte_size = 1;
8842 break;
8843 case DW_AT_calling_convention:
8844 /* DWARF doesn't provide a way to identify a program's source-level
8845 entry point. DW_AT_calling_convention attributes are only meant
8846 to describe functions' calling conventions.
8847
8848 However, because it's a necessary piece of information in
8849 Fortran, and because DW_CC_program is the only piece of debugging
8850 information whose definition refers to a 'main program' at all,
8851 several compilers have begun marking Fortran main programs with
8852 DW_CC_program --- even when those functions use the standard
8853 calling conventions.
8854
8855 So until DWARF specifies a way to provide this information and
8856 compilers pick up the new representation, we'll support this
8857 practice. */
8858 if (DW_UNSND (&attr) == DW_CC_program
8859 && cu->language == language_fortran)
8860 set_main_name (part_die->name);
8861 break;
8862 default:
8863 break;
8864 }
8865 }
8866
8867 /* When using the GNU linker, .gnu.linkonce. sections are used to
8868 eliminate duplicate copies of functions and vtables and such.
8869 The linker will arbitrarily choose one and discard the others.
8870 The AT_*_pc values for such functions refer to local labels in
8871 these sections. If the section from that file was discarded, the
8872 labels are not in the output, so the relocs get a value of 0.
8873 If this is a discarded function, mark the pc bounds as invalid,
8874 so that GDB will ignore it. */
8875 if (has_low_pc_attr && has_high_pc_attr
8876 && part_die->lowpc < part_die->highpc
8877 && (part_die->lowpc != 0
8878 || dwarf2_per_objfile->has_section_at_zero))
8879 part_die->has_pc_info = 1;
8880
8881 return info_ptr;
8882 }
8883
8884 /* Find a cached partial DIE at OFFSET in CU. */
8885
8886 static struct partial_die_info *
8887 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8888 {
8889 struct partial_die_info *lookup_die = NULL;
8890 struct partial_die_info part_die;
8891
8892 part_die.offset = offset;
8893 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8894
8895 return lookup_die;
8896 }
8897
8898 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8899 except in the case of .debug_types DIEs which do not reference
8900 outside their CU (they do however referencing other types via
8901 DW_FORM_sig8). */
8902
8903 static struct partial_die_info *
8904 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8905 {
8906 struct dwarf2_per_cu_data *per_cu = NULL;
8907 struct partial_die_info *pd = NULL;
8908
8909 if (cu->per_cu->from_debug_types)
8910 {
8911 pd = find_partial_die_in_comp_unit (offset, cu);
8912 if (pd != NULL)
8913 return pd;
8914 goto not_found;
8915 }
8916
8917 if (offset_in_cu_p (&cu->header, offset))
8918 {
8919 pd = find_partial_die_in_comp_unit (offset, cu);
8920 if (pd != NULL)
8921 return pd;
8922 }
8923
8924 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8925
8926 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8927 load_partial_comp_unit (per_cu, cu->objfile);
8928
8929 per_cu->cu->last_used = 0;
8930 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8931
8932 if (pd == NULL && per_cu->load_all_dies == 0)
8933 {
8934 struct cleanup *back_to;
8935 struct partial_die_info comp_unit_die;
8936 struct abbrev_info *abbrev;
8937 unsigned int bytes_read;
8938 char *info_ptr;
8939
8940 per_cu->load_all_dies = 1;
8941
8942 /* Re-read the DIEs. */
8943 back_to = make_cleanup (null_cleanup, 0);
8944 if (per_cu->cu->dwarf2_abbrevs == NULL)
8945 {
8946 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8947 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8948 }
8949 info_ptr = (dwarf2_per_objfile->info.buffer
8950 + per_cu->cu->header.offset
8951 + per_cu->cu->header.first_die_offset);
8952 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8953 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8954 per_cu->cu->objfile->obfd,
8955 dwarf2_per_objfile->info.buffer, info_ptr,
8956 per_cu->cu);
8957 if (comp_unit_die.has_children)
8958 load_partial_dies (per_cu->cu->objfile->obfd,
8959 dwarf2_per_objfile->info.buffer, info_ptr,
8960 0, per_cu->cu);
8961 do_cleanups (back_to);
8962
8963 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8964 }
8965
8966 not_found:
8967
8968 if (pd == NULL)
8969 internal_error (__FILE__, __LINE__,
8970 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8971 offset, bfd_get_filename (cu->objfile->obfd));
8972 return pd;
8973 }
8974
8975 /* See if we can figure out if the class lives in a namespace. We do
8976 this by looking for a member function; its demangled name will
8977 contain namespace info, if there is any. */
8978
8979 static void
8980 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
8981 struct dwarf2_cu *cu)
8982 {
8983 /* NOTE: carlton/2003-10-07: Getting the info this way changes
8984 what template types look like, because the demangler
8985 frequently doesn't give the same name as the debug info. We
8986 could fix this by only using the demangled name to get the
8987 prefix (but see comment in read_structure_type). */
8988
8989 struct partial_die_info *real_pdi;
8990 struct partial_die_info *child_pdi;
8991
8992 /* If this DIE (this DIE's specification, if any) has a parent, then
8993 we should not do this. We'll prepend the parent's fully qualified
8994 name when we create the partial symbol. */
8995
8996 real_pdi = struct_pdi;
8997 while (real_pdi->has_specification)
8998 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
8999
9000 if (real_pdi->die_parent != NULL)
9001 return;
9002
9003 for (child_pdi = struct_pdi->die_child;
9004 child_pdi != NULL;
9005 child_pdi = child_pdi->die_sibling)
9006 {
9007 if (child_pdi->tag == DW_TAG_subprogram
9008 && child_pdi->linkage_name != NULL)
9009 {
9010 char *actual_class_name
9011 = language_class_name_from_physname (cu->language_defn,
9012 child_pdi->linkage_name);
9013 if (actual_class_name != NULL)
9014 {
9015 struct_pdi->name
9016 = obsavestring (actual_class_name,
9017 strlen (actual_class_name),
9018 &cu->objfile->objfile_obstack);
9019 xfree (actual_class_name);
9020 }
9021 break;
9022 }
9023 }
9024 }
9025
9026 /* Adjust PART_DIE before generating a symbol for it. This function
9027 may set the is_external flag or change the DIE's name. */
9028
9029 static void
9030 fixup_partial_die (struct partial_die_info *part_die,
9031 struct dwarf2_cu *cu)
9032 {
9033 /* Once we've fixed up a die, there's no point in doing so again.
9034 This also avoids a memory leak if we were to call
9035 guess_partial_die_structure_name multiple times. */
9036 if (part_die->fixup_called)
9037 return;
9038
9039 /* If we found a reference attribute and the DIE has no name, try
9040 to find a name in the referred to DIE. */
9041
9042 if (part_die->name == NULL && part_die->has_specification)
9043 {
9044 struct partial_die_info *spec_die;
9045
9046 spec_die = find_partial_die (part_die->spec_offset, cu);
9047
9048 fixup_partial_die (spec_die, cu);
9049
9050 if (spec_die->name)
9051 {
9052 part_die->name = spec_die->name;
9053
9054 /* Copy DW_AT_external attribute if it is set. */
9055 if (spec_die->is_external)
9056 part_die->is_external = spec_die->is_external;
9057 }
9058 }
9059
9060 /* Set default names for some unnamed DIEs. */
9061
9062 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9063 part_die->name = "(anonymous namespace)";
9064
9065 /* If there is no parent die to provide a namespace, and there are
9066 children, see if we can determine the namespace from their linkage
9067 name.
9068 NOTE: We need to do this even if cu->has_namespace_info != 0.
9069 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9070 if (cu->language == language_cplus
9071 && dwarf2_per_objfile->types.asection != NULL
9072 && part_die->die_parent == NULL
9073 && part_die->has_children
9074 && (part_die->tag == DW_TAG_class_type
9075 || part_die->tag == DW_TAG_structure_type
9076 || part_die->tag == DW_TAG_union_type))
9077 guess_partial_die_structure_name (part_die, cu);
9078
9079 part_die->fixup_called = 1;
9080 }
9081
9082 /* Read an attribute value described by an attribute form. */
9083
9084 static gdb_byte *
9085 read_attribute_value (struct attribute *attr, unsigned form,
9086 bfd *abfd, gdb_byte *info_ptr,
9087 struct dwarf2_cu *cu)
9088 {
9089 struct comp_unit_head *cu_header = &cu->header;
9090 unsigned int bytes_read;
9091 struct dwarf_block *blk;
9092
9093 attr->form = form;
9094 switch (form)
9095 {
9096 case DW_FORM_ref_addr:
9097 if (cu->header.version == 2)
9098 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9099 else
9100 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9101 info_ptr += bytes_read;
9102 break;
9103 case DW_FORM_addr:
9104 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9105 info_ptr += bytes_read;
9106 break;
9107 case DW_FORM_block2:
9108 blk = dwarf_alloc_block (cu);
9109 blk->size = read_2_bytes (abfd, info_ptr);
9110 info_ptr += 2;
9111 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9112 info_ptr += blk->size;
9113 DW_BLOCK (attr) = blk;
9114 break;
9115 case DW_FORM_block4:
9116 blk = dwarf_alloc_block (cu);
9117 blk->size = read_4_bytes (abfd, info_ptr);
9118 info_ptr += 4;
9119 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9120 info_ptr += blk->size;
9121 DW_BLOCK (attr) = blk;
9122 break;
9123 case DW_FORM_data2:
9124 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9125 info_ptr += 2;
9126 break;
9127 case DW_FORM_data4:
9128 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9129 info_ptr += 4;
9130 break;
9131 case DW_FORM_data8:
9132 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9133 info_ptr += 8;
9134 break;
9135 case DW_FORM_sec_offset:
9136 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9137 info_ptr += bytes_read;
9138 break;
9139 case DW_FORM_string:
9140 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9141 DW_STRING_IS_CANONICAL (attr) = 0;
9142 info_ptr += bytes_read;
9143 break;
9144 case DW_FORM_strp:
9145 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9146 &bytes_read);
9147 DW_STRING_IS_CANONICAL (attr) = 0;
9148 info_ptr += bytes_read;
9149 break;
9150 case DW_FORM_exprloc:
9151 case DW_FORM_block:
9152 blk = dwarf_alloc_block (cu);
9153 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9154 info_ptr += bytes_read;
9155 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9156 info_ptr += blk->size;
9157 DW_BLOCK (attr) = blk;
9158 break;
9159 case DW_FORM_block1:
9160 blk = dwarf_alloc_block (cu);
9161 blk->size = read_1_byte (abfd, info_ptr);
9162 info_ptr += 1;
9163 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9164 info_ptr += blk->size;
9165 DW_BLOCK (attr) = blk;
9166 break;
9167 case DW_FORM_data1:
9168 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9169 info_ptr += 1;
9170 break;
9171 case DW_FORM_flag:
9172 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9173 info_ptr += 1;
9174 break;
9175 case DW_FORM_flag_present:
9176 DW_UNSND (attr) = 1;
9177 break;
9178 case DW_FORM_sdata:
9179 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9180 info_ptr += bytes_read;
9181 break;
9182 case DW_FORM_udata:
9183 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9184 info_ptr += bytes_read;
9185 break;
9186 case DW_FORM_ref1:
9187 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9188 info_ptr += 1;
9189 break;
9190 case DW_FORM_ref2:
9191 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9192 info_ptr += 2;
9193 break;
9194 case DW_FORM_ref4:
9195 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9196 info_ptr += 4;
9197 break;
9198 case DW_FORM_ref8:
9199 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9200 info_ptr += 8;
9201 break;
9202 case DW_FORM_sig8:
9203 /* Convert the signature to something we can record in DW_UNSND
9204 for later lookup.
9205 NOTE: This is NULL if the type wasn't found. */
9206 DW_SIGNATURED_TYPE (attr) =
9207 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9208 info_ptr += 8;
9209 break;
9210 case DW_FORM_ref_udata:
9211 DW_ADDR (attr) = (cu->header.offset
9212 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9213 info_ptr += bytes_read;
9214 break;
9215 case DW_FORM_indirect:
9216 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9217 info_ptr += bytes_read;
9218 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9219 break;
9220 default:
9221 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9222 dwarf_form_name (form),
9223 bfd_get_filename (abfd));
9224 }
9225
9226 /* We have seen instances where the compiler tried to emit a byte
9227 size attribute of -1 which ended up being encoded as an unsigned
9228 0xffffffff. Although 0xffffffff is technically a valid size value,
9229 an object of this size seems pretty unlikely so we can relatively
9230 safely treat these cases as if the size attribute was invalid and
9231 treat them as zero by default. */
9232 if (attr->name == DW_AT_byte_size
9233 && form == DW_FORM_data4
9234 && DW_UNSND (attr) >= 0xffffffff)
9235 {
9236 complaint
9237 (&symfile_complaints,
9238 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9239 hex_string (DW_UNSND (attr)));
9240 DW_UNSND (attr) = 0;
9241 }
9242
9243 return info_ptr;
9244 }
9245
9246 /* Read an attribute described by an abbreviated attribute. */
9247
9248 static gdb_byte *
9249 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9250 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9251 {
9252 attr->name = abbrev->name;
9253 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9254 }
9255
9256 /* read dwarf information from a buffer */
9257
9258 static unsigned int
9259 read_1_byte (bfd *abfd, gdb_byte *buf)
9260 {
9261 return bfd_get_8 (abfd, buf);
9262 }
9263
9264 static int
9265 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9266 {
9267 return bfd_get_signed_8 (abfd, buf);
9268 }
9269
9270 static unsigned int
9271 read_2_bytes (bfd *abfd, gdb_byte *buf)
9272 {
9273 return bfd_get_16 (abfd, buf);
9274 }
9275
9276 static int
9277 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9278 {
9279 return bfd_get_signed_16 (abfd, buf);
9280 }
9281
9282 static unsigned int
9283 read_4_bytes (bfd *abfd, gdb_byte *buf)
9284 {
9285 return bfd_get_32 (abfd, buf);
9286 }
9287
9288 static int
9289 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9290 {
9291 return bfd_get_signed_32 (abfd, buf);
9292 }
9293
9294 static ULONGEST
9295 read_8_bytes (bfd *abfd, gdb_byte *buf)
9296 {
9297 return bfd_get_64 (abfd, buf);
9298 }
9299
9300 static CORE_ADDR
9301 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9302 unsigned int *bytes_read)
9303 {
9304 struct comp_unit_head *cu_header = &cu->header;
9305 CORE_ADDR retval = 0;
9306
9307 if (cu_header->signed_addr_p)
9308 {
9309 switch (cu_header->addr_size)
9310 {
9311 case 2:
9312 retval = bfd_get_signed_16 (abfd, buf);
9313 break;
9314 case 4:
9315 retval = bfd_get_signed_32 (abfd, buf);
9316 break;
9317 case 8:
9318 retval = bfd_get_signed_64 (abfd, buf);
9319 break;
9320 default:
9321 internal_error (__FILE__, __LINE__,
9322 _("read_address: bad switch, signed [in module %s]"),
9323 bfd_get_filename (abfd));
9324 }
9325 }
9326 else
9327 {
9328 switch (cu_header->addr_size)
9329 {
9330 case 2:
9331 retval = bfd_get_16 (abfd, buf);
9332 break;
9333 case 4:
9334 retval = bfd_get_32 (abfd, buf);
9335 break;
9336 case 8:
9337 retval = bfd_get_64 (abfd, buf);
9338 break;
9339 default:
9340 internal_error (__FILE__, __LINE__,
9341 _("read_address: bad switch, unsigned [in module %s]"),
9342 bfd_get_filename (abfd));
9343 }
9344 }
9345
9346 *bytes_read = cu_header->addr_size;
9347 return retval;
9348 }
9349
9350 /* Read the initial length from a section. The (draft) DWARF 3
9351 specification allows the initial length to take up either 4 bytes
9352 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9353 bytes describe the length and all offsets will be 8 bytes in length
9354 instead of 4.
9355
9356 An older, non-standard 64-bit format is also handled by this
9357 function. The older format in question stores the initial length
9358 as an 8-byte quantity without an escape value. Lengths greater
9359 than 2^32 aren't very common which means that the initial 4 bytes
9360 is almost always zero. Since a length value of zero doesn't make
9361 sense for the 32-bit format, this initial zero can be considered to
9362 be an escape value which indicates the presence of the older 64-bit
9363 format. As written, the code can't detect (old format) lengths
9364 greater than 4GB. If it becomes necessary to handle lengths
9365 somewhat larger than 4GB, we could allow other small values (such
9366 as the non-sensical values of 1, 2, and 3) to also be used as
9367 escape values indicating the presence of the old format.
9368
9369 The value returned via bytes_read should be used to increment the
9370 relevant pointer after calling read_initial_length().
9371
9372 [ Note: read_initial_length() and read_offset() are based on the
9373 document entitled "DWARF Debugging Information Format", revision
9374 3, draft 8, dated November 19, 2001. This document was obtained
9375 from:
9376
9377 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9378
9379 This document is only a draft and is subject to change. (So beware.)
9380
9381 Details regarding the older, non-standard 64-bit format were
9382 determined empirically by examining 64-bit ELF files produced by
9383 the SGI toolchain on an IRIX 6.5 machine.
9384
9385 - Kevin, July 16, 2002
9386 ] */
9387
9388 static LONGEST
9389 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9390 {
9391 LONGEST length = bfd_get_32 (abfd, buf);
9392
9393 if (length == 0xffffffff)
9394 {
9395 length = bfd_get_64 (abfd, buf + 4);
9396 *bytes_read = 12;
9397 }
9398 else if (length == 0)
9399 {
9400 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
9401 length = bfd_get_64 (abfd, buf);
9402 *bytes_read = 8;
9403 }
9404 else
9405 {
9406 *bytes_read = 4;
9407 }
9408
9409 return length;
9410 }
9411
9412 /* Cover function for read_initial_length.
9413 Returns the length of the object at BUF, and stores the size of the
9414 initial length in *BYTES_READ and stores the size that offsets will be in
9415 *OFFSET_SIZE.
9416 If the initial length size is not equivalent to that specified in
9417 CU_HEADER then issue a complaint.
9418 This is useful when reading non-comp-unit headers. */
9419
9420 static LONGEST
9421 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9422 const struct comp_unit_head *cu_header,
9423 unsigned int *bytes_read,
9424 unsigned int *offset_size)
9425 {
9426 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9427
9428 gdb_assert (cu_header->initial_length_size == 4
9429 || cu_header->initial_length_size == 8
9430 || cu_header->initial_length_size == 12);
9431
9432 if (cu_header->initial_length_size != *bytes_read)
9433 complaint (&symfile_complaints,
9434 _("intermixed 32-bit and 64-bit DWARF sections"));
9435
9436 *offset_size = (*bytes_read == 4) ? 4 : 8;
9437 return length;
9438 }
9439
9440 /* Read an offset from the data stream. The size of the offset is
9441 given by cu_header->offset_size. */
9442
9443 static LONGEST
9444 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9445 unsigned int *bytes_read)
9446 {
9447 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9448
9449 *bytes_read = cu_header->offset_size;
9450 return offset;
9451 }
9452
9453 /* Read an offset from the data stream. */
9454
9455 static LONGEST
9456 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9457 {
9458 LONGEST retval = 0;
9459
9460 switch (offset_size)
9461 {
9462 case 4:
9463 retval = bfd_get_32 (abfd, buf);
9464 break;
9465 case 8:
9466 retval = bfd_get_64 (abfd, buf);
9467 break;
9468 default:
9469 internal_error (__FILE__, __LINE__,
9470 _("read_offset_1: bad switch [in module %s]"),
9471 bfd_get_filename (abfd));
9472 }
9473
9474 return retval;
9475 }
9476
9477 static gdb_byte *
9478 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9479 {
9480 /* If the size of a host char is 8 bits, we can return a pointer
9481 to the buffer, otherwise we have to copy the data to a buffer
9482 allocated on the temporary obstack. */
9483 gdb_assert (HOST_CHAR_BIT == 8);
9484 return buf;
9485 }
9486
9487 static char *
9488 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9489 {
9490 /* If the size of a host char is 8 bits, we can return a pointer
9491 to the string, otherwise we have to copy the string to a buffer
9492 allocated on the temporary obstack. */
9493 gdb_assert (HOST_CHAR_BIT == 8);
9494 if (*buf == '\0')
9495 {
9496 *bytes_read_ptr = 1;
9497 return NULL;
9498 }
9499 *bytes_read_ptr = strlen ((char *) buf) + 1;
9500 return (char *) buf;
9501 }
9502
9503 static char *
9504 read_indirect_string (bfd *abfd, gdb_byte *buf,
9505 const struct comp_unit_head *cu_header,
9506 unsigned int *bytes_read_ptr)
9507 {
9508 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9509
9510 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9511 if (dwarf2_per_objfile->str.buffer == NULL)
9512 {
9513 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9514 bfd_get_filename (abfd));
9515 return NULL;
9516 }
9517 if (str_offset >= dwarf2_per_objfile->str.size)
9518 {
9519 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9520 bfd_get_filename (abfd));
9521 return NULL;
9522 }
9523 gdb_assert (HOST_CHAR_BIT == 8);
9524 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9525 return NULL;
9526 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9527 }
9528
9529 static unsigned long
9530 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9531 {
9532 unsigned long result;
9533 unsigned int num_read;
9534 int i, shift;
9535 unsigned char byte;
9536
9537 result = 0;
9538 shift = 0;
9539 num_read = 0;
9540 i = 0;
9541 while (1)
9542 {
9543 byte = bfd_get_8 (abfd, buf);
9544 buf++;
9545 num_read++;
9546 result |= ((unsigned long)(byte & 127) << shift);
9547 if ((byte & 128) == 0)
9548 {
9549 break;
9550 }
9551 shift += 7;
9552 }
9553 *bytes_read_ptr = num_read;
9554 return result;
9555 }
9556
9557 static long
9558 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9559 {
9560 long result;
9561 int i, shift, num_read;
9562 unsigned char byte;
9563
9564 result = 0;
9565 shift = 0;
9566 num_read = 0;
9567 i = 0;
9568 while (1)
9569 {
9570 byte = bfd_get_8 (abfd, buf);
9571 buf++;
9572 num_read++;
9573 result |= ((long)(byte & 127) << shift);
9574 shift += 7;
9575 if ((byte & 128) == 0)
9576 {
9577 break;
9578 }
9579 }
9580 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9581 result |= -(((long)1) << shift);
9582 *bytes_read_ptr = num_read;
9583 return result;
9584 }
9585
9586 /* Return a pointer to just past the end of an LEB128 number in BUF. */
9587
9588 static gdb_byte *
9589 skip_leb128 (bfd *abfd, gdb_byte *buf)
9590 {
9591 int byte;
9592
9593 while (1)
9594 {
9595 byte = bfd_get_8 (abfd, buf);
9596 buf++;
9597 if ((byte & 128) == 0)
9598 return buf;
9599 }
9600 }
9601
9602 static void
9603 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9604 {
9605 switch (lang)
9606 {
9607 case DW_LANG_C89:
9608 case DW_LANG_C99:
9609 case DW_LANG_C:
9610 cu->language = language_c;
9611 break;
9612 case DW_LANG_C_plus_plus:
9613 cu->language = language_cplus;
9614 break;
9615 case DW_LANG_D:
9616 cu->language = language_d;
9617 break;
9618 case DW_LANG_Fortran77:
9619 case DW_LANG_Fortran90:
9620 case DW_LANG_Fortran95:
9621 cu->language = language_fortran;
9622 break;
9623 case DW_LANG_Mips_Assembler:
9624 cu->language = language_asm;
9625 break;
9626 case DW_LANG_Java:
9627 cu->language = language_java;
9628 break;
9629 case DW_LANG_Ada83:
9630 case DW_LANG_Ada95:
9631 cu->language = language_ada;
9632 break;
9633 case DW_LANG_Modula2:
9634 cu->language = language_m2;
9635 break;
9636 case DW_LANG_Pascal83:
9637 cu->language = language_pascal;
9638 break;
9639 case DW_LANG_ObjC:
9640 cu->language = language_objc;
9641 break;
9642 case DW_LANG_Cobol74:
9643 case DW_LANG_Cobol85:
9644 default:
9645 cu->language = language_minimal;
9646 break;
9647 }
9648 cu->language_defn = language_def (cu->language);
9649 }
9650
9651 /* Return the named attribute or NULL if not there. */
9652
9653 static struct attribute *
9654 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9655 {
9656 unsigned int i;
9657 struct attribute *spec = NULL;
9658
9659 for (i = 0; i < die->num_attrs; ++i)
9660 {
9661 if (die->attrs[i].name == name)
9662 return &die->attrs[i];
9663 if (die->attrs[i].name == DW_AT_specification
9664 || die->attrs[i].name == DW_AT_abstract_origin)
9665 spec = &die->attrs[i];
9666 }
9667
9668 if (spec)
9669 {
9670 die = follow_die_ref (die, spec, &cu);
9671 return dwarf2_attr (die, name, cu);
9672 }
9673
9674 return NULL;
9675 }
9676
9677 /* Return the named attribute or NULL if not there,
9678 but do not follow DW_AT_specification, etc.
9679 This is for use in contexts where we're reading .debug_types dies.
9680 Following DW_AT_specification, DW_AT_abstract_origin will take us
9681 back up the chain, and we want to go down. */
9682
9683 static struct attribute *
9684 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9685 struct dwarf2_cu *cu)
9686 {
9687 unsigned int i;
9688
9689 for (i = 0; i < die->num_attrs; ++i)
9690 if (die->attrs[i].name == name)
9691 return &die->attrs[i];
9692
9693 return NULL;
9694 }
9695
9696 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9697 and holds a non-zero value. This function should only be used for
9698 DW_FORM_flag or DW_FORM_flag_present attributes. */
9699
9700 static int
9701 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9702 {
9703 struct attribute *attr = dwarf2_attr (die, name, cu);
9704
9705 return (attr && DW_UNSND (attr));
9706 }
9707
9708 static int
9709 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9710 {
9711 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9712 which value is non-zero. However, we have to be careful with
9713 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9714 (via dwarf2_flag_true_p) follows this attribute. So we may
9715 end up accidently finding a declaration attribute that belongs
9716 to a different DIE referenced by the specification attribute,
9717 even though the given DIE does not have a declaration attribute. */
9718 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9719 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9720 }
9721
9722 /* Return the die giving the specification for DIE, if there is
9723 one. *SPEC_CU is the CU containing DIE on input, and the CU
9724 containing the return value on output. If there is no
9725 specification, but there is an abstract origin, that is
9726 returned. */
9727
9728 static struct die_info *
9729 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9730 {
9731 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9732 *spec_cu);
9733
9734 if (spec_attr == NULL)
9735 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9736
9737 if (spec_attr == NULL)
9738 return NULL;
9739 else
9740 return follow_die_ref (die, spec_attr, spec_cu);
9741 }
9742
9743 /* Free the line_header structure *LH, and any arrays and strings it
9744 refers to. */
9745 static void
9746 free_line_header (struct line_header *lh)
9747 {
9748 if (lh->standard_opcode_lengths)
9749 xfree (lh->standard_opcode_lengths);
9750
9751 /* Remember that all the lh->file_names[i].name pointers are
9752 pointers into debug_line_buffer, and don't need to be freed. */
9753 if (lh->file_names)
9754 xfree (lh->file_names);
9755
9756 /* Similarly for the include directory names. */
9757 if (lh->include_dirs)
9758 xfree (lh->include_dirs);
9759
9760 xfree (lh);
9761 }
9762
9763
9764 /* Add an entry to LH's include directory table. */
9765 static void
9766 add_include_dir (struct line_header *lh, char *include_dir)
9767 {
9768 /* Grow the array if necessary. */
9769 if (lh->include_dirs_size == 0)
9770 {
9771 lh->include_dirs_size = 1; /* for testing */
9772 lh->include_dirs = xmalloc (lh->include_dirs_size
9773 * sizeof (*lh->include_dirs));
9774 }
9775 else if (lh->num_include_dirs >= lh->include_dirs_size)
9776 {
9777 lh->include_dirs_size *= 2;
9778 lh->include_dirs = xrealloc (lh->include_dirs,
9779 (lh->include_dirs_size
9780 * sizeof (*lh->include_dirs)));
9781 }
9782
9783 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9784 }
9785
9786
9787 /* Add an entry to LH's file name table. */
9788 static void
9789 add_file_name (struct line_header *lh,
9790 char *name,
9791 unsigned int dir_index,
9792 unsigned int mod_time,
9793 unsigned int length)
9794 {
9795 struct file_entry *fe;
9796
9797 /* Grow the array if necessary. */
9798 if (lh->file_names_size == 0)
9799 {
9800 lh->file_names_size = 1; /* for testing */
9801 lh->file_names = xmalloc (lh->file_names_size
9802 * sizeof (*lh->file_names));
9803 }
9804 else if (lh->num_file_names >= lh->file_names_size)
9805 {
9806 lh->file_names_size *= 2;
9807 lh->file_names = xrealloc (lh->file_names,
9808 (lh->file_names_size
9809 * sizeof (*lh->file_names)));
9810 }
9811
9812 fe = &lh->file_names[lh->num_file_names++];
9813 fe->name = name;
9814 fe->dir_index = dir_index;
9815 fe->mod_time = mod_time;
9816 fe->length = length;
9817 fe->included_p = 0;
9818 fe->symtab = NULL;
9819 }
9820
9821
9822 /* Read the statement program header starting at OFFSET in
9823 .debug_line, according to the endianness of ABFD. Return a pointer
9824 to a struct line_header, allocated using xmalloc.
9825
9826 NOTE: the strings in the include directory and file name tables of
9827 the returned object point into debug_line_buffer, and must not be
9828 freed. */
9829 static struct line_header *
9830 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9831 struct dwarf2_cu *cu)
9832 {
9833 struct cleanup *back_to;
9834 struct line_header *lh;
9835 gdb_byte *line_ptr;
9836 unsigned int bytes_read, offset_size;
9837 int i;
9838 char *cur_dir, *cur_file;
9839
9840 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9841 if (dwarf2_per_objfile->line.buffer == NULL)
9842 {
9843 complaint (&symfile_complaints, _("missing .debug_line section"));
9844 return 0;
9845 }
9846
9847 /* Make sure that at least there's room for the total_length field.
9848 That could be 12 bytes long, but we're just going to fudge that. */
9849 if (offset + 4 >= dwarf2_per_objfile->line.size)
9850 {
9851 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9852 return 0;
9853 }
9854
9855 lh = xmalloc (sizeof (*lh));
9856 memset (lh, 0, sizeof (*lh));
9857 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9858 (void *) lh);
9859
9860 line_ptr = dwarf2_per_objfile->line.buffer + offset;
9861
9862 /* Read in the header. */
9863 lh->total_length =
9864 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9865 &bytes_read, &offset_size);
9866 line_ptr += bytes_read;
9867 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9868 + dwarf2_per_objfile->line.size))
9869 {
9870 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9871 return 0;
9872 }
9873 lh->statement_program_end = line_ptr + lh->total_length;
9874 lh->version = read_2_bytes (abfd, line_ptr);
9875 line_ptr += 2;
9876 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9877 line_ptr += offset_size;
9878 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9879 line_ptr += 1;
9880 if (lh->version >= 4)
9881 {
9882 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9883 line_ptr += 1;
9884 }
9885 else
9886 lh->maximum_ops_per_instruction = 1;
9887
9888 if (lh->maximum_ops_per_instruction == 0)
9889 {
9890 lh->maximum_ops_per_instruction = 1;
9891 complaint (&symfile_complaints,
9892 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9893 }
9894
9895 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9896 line_ptr += 1;
9897 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9898 line_ptr += 1;
9899 lh->line_range = read_1_byte (abfd, line_ptr);
9900 line_ptr += 1;
9901 lh->opcode_base = read_1_byte (abfd, line_ptr);
9902 line_ptr += 1;
9903 lh->standard_opcode_lengths
9904 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9905
9906 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9907 for (i = 1; i < lh->opcode_base; ++i)
9908 {
9909 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9910 line_ptr += 1;
9911 }
9912
9913 /* Read directory table. */
9914 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9915 {
9916 line_ptr += bytes_read;
9917 add_include_dir (lh, cur_dir);
9918 }
9919 line_ptr += bytes_read;
9920
9921 /* Read file name table. */
9922 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9923 {
9924 unsigned int dir_index, mod_time, length;
9925
9926 line_ptr += bytes_read;
9927 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9928 line_ptr += bytes_read;
9929 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9930 line_ptr += bytes_read;
9931 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9932 line_ptr += bytes_read;
9933
9934 add_file_name (lh, cur_file, dir_index, mod_time, length);
9935 }
9936 line_ptr += bytes_read;
9937 lh->statement_program_start = line_ptr;
9938
9939 if (line_ptr > (dwarf2_per_objfile->line.buffer
9940 + dwarf2_per_objfile->line.size))
9941 complaint (&symfile_complaints,
9942 _("line number info header doesn't fit in `.debug_line' section"));
9943
9944 discard_cleanups (back_to);
9945 return lh;
9946 }
9947
9948 /* This function exists to work around a bug in certain compilers
9949 (particularly GCC 2.95), in which the first line number marker of a
9950 function does not show up until after the prologue, right before
9951 the second line number marker. This function shifts ADDRESS down
9952 to the beginning of the function if necessary, and is called on
9953 addresses passed to record_line. */
9954
9955 static CORE_ADDR
9956 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9957 {
9958 struct function_range *fn;
9959
9960 /* Find the function_range containing address. */
9961 if (!cu->first_fn)
9962 return address;
9963
9964 if (!cu->cached_fn)
9965 cu->cached_fn = cu->first_fn;
9966
9967 fn = cu->cached_fn;
9968 while (fn)
9969 if (fn->lowpc <= address && fn->highpc > address)
9970 goto found;
9971 else
9972 fn = fn->next;
9973
9974 fn = cu->first_fn;
9975 while (fn && fn != cu->cached_fn)
9976 if (fn->lowpc <= address && fn->highpc > address)
9977 goto found;
9978 else
9979 fn = fn->next;
9980
9981 return address;
9982
9983 found:
9984 if (fn->seen_line)
9985 return address;
9986 if (address != fn->lowpc)
9987 complaint (&symfile_complaints,
9988 _("misplaced first line number at 0x%lx for '%s'"),
9989 (unsigned long) address, fn->name);
9990 fn->seen_line = 1;
9991 return fn->lowpc;
9992 }
9993
9994 /* Subroutine of dwarf_decode_lines to simplify it.
9995 Return the file name of the psymtab for included file FILE_INDEX
9996 in line header LH of PST.
9997 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
9998 If space for the result is malloc'd, it will be freed by a cleanup.
9999 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10000
10001 static char *
10002 psymtab_include_file_name (const struct line_header *lh, int file_index,
10003 const struct partial_symtab *pst,
10004 const char *comp_dir)
10005 {
10006 const struct file_entry fe = lh->file_names [file_index];
10007 char *include_name = fe.name;
10008 char *include_name_to_compare = include_name;
10009 char *dir_name = NULL;
10010 const char *pst_filename;
10011 char *copied_name = NULL;
10012 int file_is_pst;
10013
10014 if (fe.dir_index)
10015 dir_name = lh->include_dirs[fe.dir_index - 1];
10016
10017 if (!IS_ABSOLUTE_PATH (include_name)
10018 && (dir_name != NULL || comp_dir != NULL))
10019 {
10020 /* Avoid creating a duplicate psymtab for PST.
10021 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10022 Before we do the comparison, however, we need to account
10023 for DIR_NAME and COMP_DIR.
10024 First prepend dir_name (if non-NULL). If we still don't
10025 have an absolute path prepend comp_dir (if non-NULL).
10026 However, the directory we record in the include-file's
10027 psymtab does not contain COMP_DIR (to match the
10028 corresponding symtab(s)).
10029
10030 Example:
10031
10032 bash$ cd /tmp
10033 bash$ gcc -g ./hello.c
10034 include_name = "hello.c"
10035 dir_name = "."
10036 DW_AT_comp_dir = comp_dir = "/tmp"
10037 DW_AT_name = "./hello.c" */
10038
10039 if (dir_name != NULL)
10040 {
10041 include_name = concat (dir_name, SLASH_STRING,
10042 include_name, (char *)NULL);
10043 include_name_to_compare = include_name;
10044 make_cleanup (xfree, include_name);
10045 }
10046 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10047 {
10048 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10049 include_name, (char *)NULL);
10050 }
10051 }
10052
10053 pst_filename = pst->filename;
10054 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10055 {
10056 copied_name = concat (pst->dirname, SLASH_STRING,
10057 pst_filename, (char *)NULL);
10058 pst_filename = copied_name;
10059 }
10060
10061 file_is_pst = strcmp (include_name_to_compare, pst_filename) == 0;
10062
10063 if (include_name_to_compare != include_name)
10064 xfree (include_name_to_compare);
10065 if (copied_name != NULL)
10066 xfree (copied_name);
10067
10068 if (file_is_pst)
10069 return NULL;
10070 return include_name;
10071 }
10072
10073 /* Decode the Line Number Program (LNP) for the given line_header
10074 structure and CU. The actual information extracted and the type
10075 of structures created from the LNP depends on the value of PST.
10076
10077 1. If PST is NULL, then this procedure uses the data from the program
10078 to create all necessary symbol tables, and their linetables.
10079
10080 2. If PST is not NULL, this procedure reads the program to determine
10081 the list of files included by the unit represented by PST, and
10082 builds all the associated partial symbol tables.
10083
10084 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10085 It is used for relative paths in the line table.
10086 NOTE: When processing partial symtabs (pst != NULL),
10087 comp_dir == pst->dirname.
10088
10089 NOTE: It is important that psymtabs have the same file name (via strcmp)
10090 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10091 symtab we don't use it in the name of the psymtabs we create.
10092 E.g. expand_line_sal requires this when finding psymtabs to expand.
10093 A good testcase for this is mb-inline.exp. */
10094
10095 static void
10096 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10097 struct dwarf2_cu *cu, struct partial_symtab *pst)
10098 {
10099 gdb_byte *line_ptr, *extended_end;
10100 gdb_byte *line_end;
10101 unsigned int bytes_read, extended_len;
10102 unsigned char op_code, extended_op, adj_opcode;
10103 CORE_ADDR baseaddr;
10104 struct objfile *objfile = cu->objfile;
10105 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10106 const int decode_for_pst_p = (pst != NULL);
10107 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10108
10109 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10110
10111 line_ptr = lh->statement_program_start;
10112 line_end = lh->statement_program_end;
10113
10114 /* Read the statement sequences until there's nothing left. */
10115 while (line_ptr < line_end)
10116 {
10117 /* state machine registers */
10118 CORE_ADDR address = 0;
10119 unsigned int file = 1;
10120 unsigned int line = 1;
10121 unsigned int column = 0;
10122 int is_stmt = lh->default_is_stmt;
10123 int basic_block = 0;
10124 int end_sequence = 0;
10125 CORE_ADDR addr;
10126 unsigned char op_index = 0;
10127
10128 if (!decode_for_pst_p && lh->num_file_names >= file)
10129 {
10130 /* Start a subfile for the current file of the state machine. */
10131 /* lh->include_dirs and lh->file_names are 0-based, but the
10132 directory and file name numbers in the statement program
10133 are 1-based. */
10134 struct file_entry *fe = &lh->file_names[file - 1];
10135 char *dir = NULL;
10136
10137 if (fe->dir_index)
10138 dir = lh->include_dirs[fe->dir_index - 1];
10139
10140 dwarf2_start_subfile (fe->name, dir, comp_dir);
10141 }
10142
10143 /* Decode the table. */
10144 while (!end_sequence)
10145 {
10146 op_code = read_1_byte (abfd, line_ptr);
10147 line_ptr += 1;
10148 if (line_ptr > line_end)
10149 {
10150 dwarf2_debug_line_missing_end_sequence_complaint ();
10151 break;
10152 }
10153
10154 if (op_code >= lh->opcode_base)
10155 {
10156 /* Special operand. */
10157 adj_opcode = op_code - lh->opcode_base;
10158 address += (((op_index + (adj_opcode / lh->line_range))
10159 / lh->maximum_ops_per_instruction)
10160 * lh->minimum_instruction_length);
10161 op_index = ((op_index + (adj_opcode / lh->line_range))
10162 % lh->maximum_ops_per_instruction);
10163 line += lh->line_base + (adj_opcode % lh->line_range);
10164 if (lh->num_file_names < file || file == 0)
10165 dwarf2_debug_line_missing_file_complaint ();
10166 /* For now we ignore lines not starting on an
10167 instruction boundary. */
10168 else if (op_index == 0)
10169 {
10170 lh->file_names[file - 1].included_p = 1;
10171 if (!decode_for_pst_p && is_stmt)
10172 {
10173 if (last_subfile != current_subfile)
10174 {
10175 addr = gdbarch_addr_bits_remove (gdbarch, address);
10176 if (last_subfile)
10177 record_line (last_subfile, 0, addr);
10178 last_subfile = current_subfile;
10179 }
10180 /* Append row to matrix using current values. */
10181 addr = check_cu_functions (address, cu);
10182 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10183 record_line (current_subfile, line, addr);
10184 }
10185 }
10186 basic_block = 0;
10187 }
10188 else switch (op_code)
10189 {
10190 case DW_LNS_extended_op:
10191 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10192 line_ptr += bytes_read;
10193 extended_end = line_ptr + extended_len;
10194 extended_op = read_1_byte (abfd, line_ptr);
10195 line_ptr += 1;
10196 switch (extended_op)
10197 {
10198 case DW_LNE_end_sequence:
10199 end_sequence = 1;
10200 break;
10201 case DW_LNE_set_address:
10202 address = read_address (abfd, line_ptr, cu, &bytes_read);
10203 op_index = 0;
10204 line_ptr += bytes_read;
10205 address += baseaddr;
10206 break;
10207 case DW_LNE_define_file:
10208 {
10209 char *cur_file;
10210 unsigned int dir_index, mod_time, length;
10211
10212 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
10213 line_ptr += bytes_read;
10214 dir_index =
10215 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10216 line_ptr += bytes_read;
10217 mod_time =
10218 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10219 line_ptr += bytes_read;
10220 length =
10221 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10222 line_ptr += bytes_read;
10223 add_file_name (lh, cur_file, dir_index, mod_time, length);
10224 }
10225 break;
10226 case DW_LNE_set_discriminator:
10227 /* The discriminator is not interesting to the debugger;
10228 just ignore it. */
10229 line_ptr = extended_end;
10230 break;
10231 default:
10232 complaint (&symfile_complaints,
10233 _("mangled .debug_line section"));
10234 return;
10235 }
10236 /* Make sure that we parsed the extended op correctly. If e.g.
10237 we expected a different address size than the producer used,
10238 we may have read the wrong number of bytes. */
10239 if (line_ptr != extended_end)
10240 {
10241 complaint (&symfile_complaints,
10242 _("mangled .debug_line section"));
10243 return;
10244 }
10245 break;
10246 case DW_LNS_copy:
10247 if (lh->num_file_names < file || file == 0)
10248 dwarf2_debug_line_missing_file_complaint ();
10249 else
10250 {
10251 lh->file_names[file - 1].included_p = 1;
10252 if (!decode_for_pst_p && is_stmt)
10253 {
10254 if (last_subfile != current_subfile)
10255 {
10256 addr = gdbarch_addr_bits_remove (gdbarch, address);
10257 if (last_subfile)
10258 record_line (last_subfile, 0, addr);
10259 last_subfile = current_subfile;
10260 }
10261 addr = check_cu_functions (address, cu);
10262 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10263 record_line (current_subfile, line, addr);
10264 }
10265 }
10266 basic_block = 0;
10267 break;
10268 case DW_LNS_advance_pc:
10269 {
10270 CORE_ADDR adjust
10271 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10272
10273 address += (((op_index + adjust)
10274 / lh->maximum_ops_per_instruction)
10275 * lh->minimum_instruction_length);
10276 op_index = ((op_index + adjust)
10277 % lh->maximum_ops_per_instruction);
10278 line_ptr += bytes_read;
10279 }
10280 break;
10281 case DW_LNS_advance_line:
10282 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10283 line_ptr += bytes_read;
10284 break;
10285 case DW_LNS_set_file:
10286 {
10287 /* The arrays lh->include_dirs and lh->file_names are
10288 0-based, but the directory and file name numbers in
10289 the statement program are 1-based. */
10290 struct file_entry *fe;
10291 char *dir = NULL;
10292
10293 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10294 line_ptr += bytes_read;
10295 if (lh->num_file_names < file || file == 0)
10296 dwarf2_debug_line_missing_file_complaint ();
10297 else
10298 {
10299 fe = &lh->file_names[file - 1];
10300 if (fe->dir_index)
10301 dir = lh->include_dirs[fe->dir_index - 1];
10302 if (!decode_for_pst_p)
10303 {
10304 last_subfile = current_subfile;
10305 dwarf2_start_subfile (fe->name, dir, comp_dir);
10306 }
10307 }
10308 }
10309 break;
10310 case DW_LNS_set_column:
10311 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10312 line_ptr += bytes_read;
10313 break;
10314 case DW_LNS_negate_stmt:
10315 is_stmt = (!is_stmt);
10316 break;
10317 case DW_LNS_set_basic_block:
10318 basic_block = 1;
10319 break;
10320 /* Add to the address register of the state machine the
10321 address increment value corresponding to special opcode
10322 255. I.e., this value is scaled by the minimum
10323 instruction length since special opcode 255 would have
10324 scaled the the increment. */
10325 case DW_LNS_const_add_pc:
10326 {
10327 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10328
10329 address += (((op_index + adjust)
10330 / lh->maximum_ops_per_instruction)
10331 * lh->minimum_instruction_length);
10332 op_index = ((op_index + adjust)
10333 % lh->maximum_ops_per_instruction);
10334 }
10335 break;
10336 case DW_LNS_fixed_advance_pc:
10337 address += read_2_bytes (abfd, line_ptr);
10338 op_index = 0;
10339 line_ptr += 2;
10340 break;
10341 default:
10342 {
10343 /* Unknown standard opcode, ignore it. */
10344 int i;
10345
10346 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10347 {
10348 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10349 line_ptr += bytes_read;
10350 }
10351 }
10352 }
10353 }
10354 if (lh->num_file_names < file || file == 0)
10355 dwarf2_debug_line_missing_file_complaint ();
10356 else
10357 {
10358 lh->file_names[file - 1].included_p = 1;
10359 if (!decode_for_pst_p)
10360 {
10361 addr = gdbarch_addr_bits_remove (gdbarch, address);
10362 record_line (current_subfile, 0, addr);
10363 }
10364 }
10365 }
10366
10367 if (decode_for_pst_p)
10368 {
10369 int file_index;
10370
10371 /* Now that we're done scanning the Line Header Program, we can
10372 create the psymtab of each included file. */
10373 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10374 if (lh->file_names[file_index].included_p == 1)
10375 {
10376 char *include_name =
10377 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10378 if (include_name != NULL)
10379 dwarf2_create_include_psymtab (include_name, pst, objfile);
10380 }
10381 }
10382 else
10383 {
10384 /* Make sure a symtab is created for every file, even files
10385 which contain only variables (i.e. no code with associated
10386 line numbers). */
10387
10388 int i;
10389 struct file_entry *fe;
10390
10391 for (i = 0; i < lh->num_file_names; i++)
10392 {
10393 char *dir = NULL;
10394
10395 fe = &lh->file_names[i];
10396 if (fe->dir_index)
10397 dir = lh->include_dirs[fe->dir_index - 1];
10398 dwarf2_start_subfile (fe->name, dir, comp_dir);
10399
10400 /* Skip the main file; we don't need it, and it must be
10401 allocated last, so that it will show up before the
10402 non-primary symtabs in the objfile's symtab list. */
10403 if (current_subfile == first_subfile)
10404 continue;
10405
10406 if (current_subfile->symtab == NULL)
10407 current_subfile->symtab = allocate_symtab (current_subfile->name,
10408 cu->objfile);
10409 fe->symtab = current_subfile->symtab;
10410 }
10411 }
10412 }
10413
10414 /* Start a subfile for DWARF. FILENAME is the name of the file and
10415 DIRNAME the name of the source directory which contains FILENAME
10416 or NULL if not known. COMP_DIR is the compilation directory for the
10417 linetable's compilation unit or NULL if not known.
10418 This routine tries to keep line numbers from identical absolute and
10419 relative file names in a common subfile.
10420
10421 Using the `list' example from the GDB testsuite, which resides in
10422 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10423 of /srcdir/list0.c yields the following debugging information for list0.c:
10424
10425 DW_AT_name: /srcdir/list0.c
10426 DW_AT_comp_dir: /compdir
10427 files.files[0].name: list0.h
10428 files.files[0].dir: /srcdir
10429 files.files[1].name: list0.c
10430 files.files[1].dir: /srcdir
10431
10432 The line number information for list0.c has to end up in a single
10433 subfile, so that `break /srcdir/list0.c:1' works as expected.
10434 start_subfile will ensure that this happens provided that we pass the
10435 concatenation of files.files[1].dir and files.files[1].name as the
10436 subfile's name. */
10437
10438 static void
10439 dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
10440 {
10441 char *fullname;
10442
10443 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10444 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10445 second argument to start_subfile. To be consistent, we do the
10446 same here. In order not to lose the line information directory,
10447 we concatenate it to the filename when it makes sense.
10448 Note that the Dwarf3 standard says (speaking of filenames in line
10449 information): ``The directory index is ignored for file names
10450 that represent full path names''. Thus ignoring dirname in the
10451 `else' branch below isn't an issue. */
10452
10453 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10454 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10455 else
10456 fullname = filename;
10457
10458 start_subfile (fullname, comp_dir);
10459
10460 if (fullname != filename)
10461 xfree (fullname);
10462 }
10463
10464 static void
10465 var_decode_location (struct attribute *attr, struct symbol *sym,
10466 struct dwarf2_cu *cu)
10467 {
10468 struct objfile *objfile = cu->objfile;
10469 struct comp_unit_head *cu_header = &cu->header;
10470
10471 /* NOTE drow/2003-01-30: There used to be a comment and some special
10472 code here to turn a symbol with DW_AT_external and a
10473 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10474 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10475 with some versions of binutils) where shared libraries could have
10476 relocations against symbols in their debug information - the
10477 minimal symbol would have the right address, but the debug info
10478 would not. It's no longer necessary, because we will explicitly
10479 apply relocations when we read in the debug information now. */
10480
10481 /* A DW_AT_location attribute with no contents indicates that a
10482 variable has been optimized away. */
10483 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10484 {
10485 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10486 return;
10487 }
10488
10489 /* Handle one degenerate form of location expression specially, to
10490 preserve GDB's previous behavior when section offsets are
10491 specified. If this is just a DW_OP_addr then mark this symbol
10492 as LOC_STATIC. */
10493
10494 if (attr_form_is_block (attr)
10495 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10496 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10497 {
10498 unsigned int dummy;
10499
10500 SYMBOL_VALUE_ADDRESS (sym) =
10501 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10502 SYMBOL_CLASS (sym) = LOC_STATIC;
10503 fixup_symbol_section (sym, objfile);
10504 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10505 SYMBOL_SECTION (sym));
10506 return;
10507 }
10508
10509 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10510 expression evaluator, and use LOC_COMPUTED only when necessary
10511 (i.e. when the value of a register or memory location is
10512 referenced, or a thread-local block, etc.). Then again, it might
10513 not be worthwhile. I'm assuming that it isn't unless performance
10514 or memory numbers show me otherwise. */
10515
10516 dwarf2_symbol_mark_computed (attr, sym, cu);
10517 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10518 }
10519
10520 /* Given a pointer to a DWARF information entry, figure out if we need
10521 to make a symbol table entry for it, and if so, create a new entry
10522 and return a pointer to it.
10523 If TYPE is NULL, determine symbol type from the die, otherwise
10524 used the passed type.
10525 If SPACE is not NULL, use it to hold the new symbol. If it is
10526 NULL, allocate a new symbol on the objfile's obstack. */
10527
10528 static struct symbol *
10529 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10530 struct symbol *space)
10531 {
10532 struct objfile *objfile = cu->objfile;
10533 struct symbol *sym = NULL;
10534 char *name;
10535 struct attribute *attr = NULL;
10536 struct attribute *attr2 = NULL;
10537 CORE_ADDR baseaddr;
10538 struct pending **list_to_add = NULL;
10539
10540 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10541
10542 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10543
10544 name = dwarf2_name (die, cu);
10545 if (name)
10546 {
10547 const char *linkagename;
10548 int suppress_add = 0;
10549
10550 if (space)
10551 sym = space;
10552 else
10553 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10554 OBJSTAT (objfile, n_syms++);
10555
10556 /* Cache this symbol's name and the name's demangled form (if any). */
10557 SYMBOL_SET_LANGUAGE (sym, cu->language);
10558 linkagename = dwarf2_physname (name, die, cu);
10559 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10560
10561 /* Fortran does not have mangling standard and the mangling does differ
10562 between gfortran, iFort etc. */
10563 if (cu->language == language_fortran
10564 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10565 symbol_set_demangled_name (&(sym->ginfo),
10566 (char *) dwarf2_full_name (name, die, cu),
10567 NULL);
10568
10569 /* Default assumptions.
10570 Use the passed type or decode it from the die. */
10571 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10572 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10573 if (type != NULL)
10574 SYMBOL_TYPE (sym) = type;
10575 else
10576 SYMBOL_TYPE (sym) = die_type (die, cu);
10577 attr = dwarf2_attr (die,
10578 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10579 cu);
10580 if (attr)
10581 {
10582 SYMBOL_LINE (sym) = DW_UNSND (attr);
10583 }
10584
10585 attr = dwarf2_attr (die,
10586 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10587 cu);
10588 if (attr)
10589 {
10590 int file_index = DW_UNSND (attr);
10591
10592 if (cu->line_header == NULL
10593 || file_index > cu->line_header->num_file_names)
10594 complaint (&symfile_complaints,
10595 _("file index out of range"));
10596 else if (file_index > 0)
10597 {
10598 struct file_entry *fe;
10599
10600 fe = &cu->line_header->file_names[file_index - 1];
10601 SYMBOL_SYMTAB (sym) = fe->symtab;
10602 }
10603 }
10604
10605 switch (die->tag)
10606 {
10607 case DW_TAG_label:
10608 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10609 if (attr)
10610 {
10611 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10612 }
10613 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10614 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10615 SYMBOL_CLASS (sym) = LOC_LABEL;
10616 add_symbol_to_list (sym, cu->list_in_scope);
10617 break;
10618 case DW_TAG_subprogram:
10619 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10620 finish_block. */
10621 SYMBOL_CLASS (sym) = LOC_BLOCK;
10622 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10623 if ((attr2 && (DW_UNSND (attr2) != 0))
10624 || cu->language == language_ada)
10625 {
10626 /* Subprograms marked external are stored as a global symbol.
10627 Ada subprograms, whether marked external or not, are always
10628 stored as a global symbol, because we want to be able to
10629 access them globally. For instance, we want to be able
10630 to break on a nested subprogram without having to
10631 specify the context. */
10632 list_to_add = &global_symbols;
10633 }
10634 else
10635 {
10636 list_to_add = cu->list_in_scope;
10637 }
10638 break;
10639 case DW_TAG_inlined_subroutine:
10640 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10641 finish_block. */
10642 SYMBOL_CLASS (sym) = LOC_BLOCK;
10643 SYMBOL_INLINED (sym) = 1;
10644 /* Do not add the symbol to any lists. It will be found via
10645 BLOCK_FUNCTION from the blockvector. */
10646 break;
10647 case DW_TAG_template_value_param:
10648 suppress_add = 1;
10649 /* Fall through. */
10650 case DW_TAG_constant:
10651 case DW_TAG_variable:
10652 case DW_TAG_member:
10653 /* Compilation with minimal debug info may result in variables
10654 with missing type entries. Change the misleading `void' type
10655 to something sensible. */
10656 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10657 SYMBOL_TYPE (sym)
10658 = objfile_type (objfile)->nodebug_data_symbol;
10659
10660 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10661 /* In the case of DW_TAG_member, we should only be called for
10662 static const members. */
10663 if (die->tag == DW_TAG_member)
10664 {
10665 /* dwarf2_add_field uses die_is_declaration,
10666 so we do the same. */
10667 gdb_assert (die_is_declaration (die, cu));
10668 gdb_assert (attr);
10669 }
10670 if (attr)
10671 {
10672 dwarf2_const_value (attr, sym, cu);
10673 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10674 if (!suppress_add)
10675 {
10676 if (attr2 && (DW_UNSND (attr2) != 0))
10677 list_to_add = &global_symbols;
10678 else
10679 list_to_add = cu->list_in_scope;
10680 }
10681 break;
10682 }
10683 attr = dwarf2_attr (die, DW_AT_location, cu);
10684 if (attr)
10685 {
10686 var_decode_location (attr, sym, cu);
10687 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10688 if (SYMBOL_CLASS (sym) == LOC_STATIC
10689 && SYMBOL_VALUE_ADDRESS (sym) == 0
10690 && !dwarf2_per_objfile->has_section_at_zero)
10691 {
10692 /* When a static variable is eliminated by the linker,
10693 the corresponding debug information is not stripped
10694 out, but the variable address is set to null;
10695 do not add such variables into symbol table. */
10696 }
10697 else if (attr2 && (DW_UNSND (attr2) != 0))
10698 {
10699 /* Workaround gfortran PR debug/40040 - it uses
10700 DW_AT_location for variables in -fPIC libraries which may
10701 get overriden by other libraries/executable and get
10702 a different address. Resolve it by the minimal symbol
10703 which may come from inferior's executable using copy
10704 relocation. Make this workaround only for gfortran as for
10705 other compilers GDB cannot guess the minimal symbol
10706 Fortran mangling kind. */
10707 if (cu->language == language_fortran && die->parent
10708 && die->parent->tag == DW_TAG_module
10709 && cu->producer
10710 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10711 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10712
10713 /* A variable with DW_AT_external is never static,
10714 but it may be block-scoped. */
10715 list_to_add = (cu->list_in_scope == &file_symbols
10716 ? &global_symbols : cu->list_in_scope);
10717 }
10718 else
10719 list_to_add = cu->list_in_scope;
10720 }
10721 else
10722 {
10723 /* We do not know the address of this symbol.
10724 If it is an external symbol and we have type information
10725 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10726 The address of the variable will then be determined from
10727 the minimal symbol table whenever the variable is
10728 referenced. */
10729 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10730 if (attr2 && (DW_UNSND (attr2) != 0)
10731 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10732 {
10733 /* A variable with DW_AT_external is never static, but it
10734 may be block-scoped. */
10735 list_to_add = (cu->list_in_scope == &file_symbols
10736 ? &global_symbols : cu->list_in_scope);
10737
10738 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10739 }
10740 else if (!die_is_declaration (die, cu))
10741 {
10742 /* Use the default LOC_OPTIMIZED_OUT class. */
10743 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10744 if (!suppress_add)
10745 list_to_add = cu->list_in_scope;
10746 }
10747 }
10748 break;
10749 case DW_TAG_formal_parameter:
10750 /* If we are inside a function, mark this as an argument. If
10751 not, we might be looking at an argument to an inlined function
10752 when we do not have enough information to show inlined frames;
10753 pretend it's a local variable in that case so that the user can
10754 still see it. */
10755 if (context_stack_depth > 0
10756 && context_stack[context_stack_depth - 1].name != NULL)
10757 SYMBOL_IS_ARGUMENT (sym) = 1;
10758 attr = dwarf2_attr (die, DW_AT_location, cu);
10759 if (attr)
10760 {
10761 var_decode_location (attr, sym, cu);
10762 }
10763 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10764 if (attr)
10765 {
10766 dwarf2_const_value (attr, sym, cu);
10767 }
10768 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10769 if (attr && DW_UNSND (attr))
10770 {
10771 struct type *ref_type;
10772
10773 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10774 SYMBOL_TYPE (sym) = ref_type;
10775 }
10776
10777 list_to_add = cu->list_in_scope;
10778 break;
10779 case DW_TAG_unspecified_parameters:
10780 /* From varargs functions; gdb doesn't seem to have any
10781 interest in this information, so just ignore it for now.
10782 (FIXME?) */
10783 break;
10784 case DW_TAG_template_type_param:
10785 suppress_add = 1;
10786 /* Fall through. */
10787 case DW_TAG_class_type:
10788 case DW_TAG_interface_type:
10789 case DW_TAG_structure_type:
10790 case DW_TAG_union_type:
10791 case DW_TAG_set_type:
10792 case DW_TAG_enumeration_type:
10793 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10794 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10795
10796 {
10797 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10798 really ever be static objects: otherwise, if you try
10799 to, say, break of a class's method and you're in a file
10800 which doesn't mention that class, it won't work unless
10801 the check for all static symbols in lookup_symbol_aux
10802 saves you. See the OtherFileClass tests in
10803 gdb.c++/namespace.exp. */
10804
10805 if (!suppress_add)
10806 {
10807 list_to_add = (cu->list_in_scope == &file_symbols
10808 && (cu->language == language_cplus
10809 || cu->language == language_java)
10810 ? &global_symbols : cu->list_in_scope);
10811
10812 /* The semantics of C++ state that "struct foo {
10813 ... }" also defines a typedef for "foo". A Java
10814 class declaration also defines a typedef for the
10815 class. */
10816 if (cu->language == language_cplus
10817 || cu->language == language_java
10818 || cu->language == language_ada)
10819 {
10820 /* The symbol's name is already allocated along
10821 with this objfile, so we don't need to
10822 duplicate it for the type. */
10823 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10824 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10825 }
10826 }
10827 }
10828 break;
10829 case DW_TAG_typedef:
10830 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10831 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10832 list_to_add = cu->list_in_scope;
10833 break;
10834 case DW_TAG_base_type:
10835 case DW_TAG_subrange_type:
10836 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10837 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10838 list_to_add = cu->list_in_scope;
10839 break;
10840 case DW_TAG_enumerator:
10841 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10842 if (attr)
10843 {
10844 dwarf2_const_value (attr, sym, cu);
10845 }
10846 {
10847 /* NOTE: carlton/2003-11-10: See comment above in the
10848 DW_TAG_class_type, etc. block. */
10849
10850 list_to_add = (cu->list_in_scope == &file_symbols
10851 && (cu->language == language_cplus
10852 || cu->language == language_java)
10853 ? &global_symbols : cu->list_in_scope);
10854 }
10855 break;
10856 case DW_TAG_namespace:
10857 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10858 list_to_add = &global_symbols;
10859 break;
10860 default:
10861 /* Not a tag we recognize. Hopefully we aren't processing
10862 trash data, but since we must specifically ignore things
10863 we don't recognize, there is nothing else we should do at
10864 this point. */
10865 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10866 dwarf_tag_name (die->tag));
10867 break;
10868 }
10869
10870 if (suppress_add)
10871 {
10872 sym->hash_next = objfile->template_symbols;
10873 objfile->template_symbols = sym;
10874 list_to_add = NULL;
10875 }
10876
10877 if (list_to_add != NULL)
10878 add_symbol_to_list (sym, list_to_add);
10879
10880 /* For the benefit of old versions of GCC, check for anonymous
10881 namespaces based on the demangled name. */
10882 if (!processing_has_namespace_info
10883 && cu->language == language_cplus)
10884 cp_scan_for_anonymous_namespaces (sym);
10885 }
10886 return (sym);
10887 }
10888
10889 /* A wrapper for new_symbol_full that always allocates a new symbol. */
10890
10891 static struct symbol *
10892 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10893 {
10894 return new_symbol_full (die, type, cu, NULL);
10895 }
10896
10897 /* Given an attr with a DW_FORM_dataN value in host byte order,
10898 zero-extend it as appropriate for the symbol's type. The DWARF
10899 standard (v4) is not entirely clear about the meaning of using
10900 DW_FORM_dataN for a constant with a signed type, where the type is
10901 wider than the data. The conclusion of a discussion on the DWARF
10902 list was that this is unspecified. We choose to always zero-extend
10903 because that is the interpretation long in use by GCC. */
10904
10905 static gdb_byte *
10906 dwarf2_const_value_data (struct attribute *attr, struct type *type,
10907 const char *name, struct obstack *obstack,
10908 struct dwarf2_cu *cu, long *value, int bits)
10909 {
10910 struct objfile *objfile = cu->objfile;
10911 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10912 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10913 LONGEST l = DW_UNSND (attr);
10914
10915 if (bits < sizeof (*value) * 8)
10916 {
10917 l &= ((LONGEST) 1 << bits) - 1;
10918 *value = l;
10919 }
10920 else if (bits == sizeof (*value) * 8)
10921 *value = l;
10922 else
10923 {
10924 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10925 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10926 return bytes;
10927 }
10928
10929 return NULL;
10930 }
10931
10932 /* Read a constant value from an attribute. Either set *VALUE, or if
10933 the value does not fit in *VALUE, set *BYTES - either already
10934 allocated on the objfile obstack, or newly allocated on OBSTACK,
10935 or, set *BATON, if we translated the constant to a location
10936 expression. */
10937
10938 static void
10939 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10940 const char *name, struct obstack *obstack,
10941 struct dwarf2_cu *cu,
10942 long *value, gdb_byte **bytes,
10943 struct dwarf2_locexpr_baton **baton)
10944 {
10945 struct objfile *objfile = cu->objfile;
10946 struct comp_unit_head *cu_header = &cu->header;
10947 struct dwarf_block *blk;
10948 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10949 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10950
10951 *value = 0;
10952 *bytes = NULL;
10953 *baton = NULL;
10954
10955 switch (attr->form)
10956 {
10957 case DW_FORM_addr:
10958 {
10959 gdb_byte *data;
10960
10961 if (TYPE_LENGTH (type) != cu_header->addr_size)
10962 dwarf2_const_value_length_mismatch_complaint (name,
10963 cu_header->addr_size,
10964 TYPE_LENGTH (type));
10965 /* Symbols of this form are reasonably rare, so we just
10966 piggyback on the existing location code rather than writing
10967 a new implementation of symbol_computed_ops. */
10968 *baton = obstack_alloc (&objfile->objfile_obstack,
10969 sizeof (struct dwarf2_locexpr_baton));
10970 (*baton)->per_cu = cu->per_cu;
10971 gdb_assert ((*baton)->per_cu);
10972
10973 (*baton)->size = 2 + cu_header->addr_size;
10974 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10975 (*baton)->data = data;
10976
10977 data[0] = DW_OP_addr;
10978 store_unsigned_integer (&data[1], cu_header->addr_size,
10979 byte_order, DW_ADDR (attr));
10980 data[cu_header->addr_size + 1] = DW_OP_stack_value;
10981 }
10982 break;
10983 case DW_FORM_string:
10984 case DW_FORM_strp:
10985 /* DW_STRING is already allocated on the objfile obstack, point
10986 directly to it. */
10987 *bytes = (gdb_byte *) DW_STRING (attr);
10988 break;
10989 case DW_FORM_block1:
10990 case DW_FORM_block2:
10991 case DW_FORM_block4:
10992 case DW_FORM_block:
10993 case DW_FORM_exprloc:
10994 blk = DW_BLOCK (attr);
10995 if (TYPE_LENGTH (type) != blk->size)
10996 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10997 TYPE_LENGTH (type));
10998 *bytes = blk->data;
10999 break;
11000
11001 /* The DW_AT_const_value attributes are supposed to carry the
11002 symbol's value "represented as it would be on the target
11003 architecture." By the time we get here, it's already been
11004 converted to host endianness, so we just need to sign- or
11005 zero-extend it as appropriate. */
11006 case DW_FORM_data1:
11007 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
11008 break;
11009 case DW_FORM_data2:
11010 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
11011 break;
11012 case DW_FORM_data4:
11013 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
11014 break;
11015 case DW_FORM_data8:
11016 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
11017 break;
11018
11019 case DW_FORM_sdata:
11020 *value = DW_SND (attr);
11021 break;
11022
11023 case DW_FORM_udata:
11024 *value = DW_UNSND (attr);
11025 break;
11026
11027 default:
11028 complaint (&symfile_complaints,
11029 _("unsupported const value attribute form: '%s'"),
11030 dwarf_form_name (attr->form));
11031 *value = 0;
11032 break;
11033 }
11034 }
11035
11036
11037 /* Copy constant value from an attribute to a symbol. */
11038
11039 static void
11040 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11041 struct dwarf2_cu *cu)
11042 {
11043 struct objfile *objfile = cu->objfile;
11044 struct comp_unit_head *cu_header = &cu->header;
11045 long value;
11046 gdb_byte *bytes;
11047 struct dwarf2_locexpr_baton *baton;
11048
11049 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11050 SYMBOL_PRINT_NAME (sym),
11051 &objfile->objfile_obstack, cu,
11052 &value, &bytes, &baton);
11053
11054 if (baton != NULL)
11055 {
11056 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11057 SYMBOL_LOCATION_BATON (sym) = baton;
11058 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11059 }
11060 else if (bytes != NULL)
11061 {
11062 SYMBOL_VALUE_BYTES (sym) = bytes;
11063 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11064 }
11065 else
11066 {
11067 SYMBOL_VALUE (sym) = value;
11068 SYMBOL_CLASS (sym) = LOC_CONST;
11069 }
11070 }
11071
11072 /* Return the type of the die in question using its DW_AT_type attribute. */
11073
11074 static struct type *
11075 die_type (struct die_info *die, struct dwarf2_cu *cu)
11076 {
11077 struct attribute *type_attr;
11078
11079 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11080 if (!type_attr)
11081 {
11082 /* A missing DW_AT_type represents a void type. */
11083 return objfile_type (cu->objfile)->builtin_void;
11084 }
11085
11086 return lookup_die_type (die, type_attr, cu);
11087 }
11088
11089 /* True iff CU's producer generates GNAT Ada auxiliary information
11090 that allows to find parallel types through that information instead
11091 of having to do expensive parallel lookups by type name. */
11092
11093 static int
11094 need_gnat_info (struct dwarf2_cu *cu)
11095 {
11096 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11097 of GNAT produces this auxiliary information, without any indication
11098 that it is produced. Part of enhancing the FSF version of GNAT
11099 to produce that information will be to put in place an indicator
11100 that we can use in order to determine whether the descriptive type
11101 info is available or not. One suggestion that has been made is
11102 to use a new attribute, attached to the CU die. For now, assume
11103 that the descriptive type info is not available. */
11104 return 0;
11105 }
11106
11107 /* Return the auxiliary type of the die in question using its
11108 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11109 attribute is not present. */
11110
11111 static struct type *
11112 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11113 {
11114 struct attribute *type_attr;
11115
11116 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11117 if (!type_attr)
11118 return NULL;
11119
11120 return lookup_die_type (die, type_attr, cu);
11121 }
11122
11123 /* If DIE has a descriptive_type attribute, then set the TYPE's
11124 descriptive type accordingly. */
11125
11126 static void
11127 set_descriptive_type (struct type *type, struct die_info *die,
11128 struct dwarf2_cu *cu)
11129 {
11130 struct type *descriptive_type = die_descriptive_type (die, cu);
11131
11132 if (descriptive_type)
11133 {
11134 ALLOCATE_GNAT_AUX_TYPE (type);
11135 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11136 }
11137 }
11138
11139 /* Return the containing type of the die in question using its
11140 DW_AT_containing_type attribute. */
11141
11142 static struct type *
11143 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11144 {
11145 struct attribute *type_attr;
11146
11147 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11148 if (!type_attr)
11149 error (_("Dwarf Error: Problem turning containing type into gdb type "
11150 "[in module %s]"), cu->objfile->name);
11151
11152 return lookup_die_type (die, type_attr, cu);
11153 }
11154
11155 /* Look up the type of DIE in CU using its type attribute ATTR.
11156 If there is no type substitute an error marker. */
11157
11158 static struct type *
11159 lookup_die_type (struct die_info *die, struct attribute *attr,
11160 struct dwarf2_cu *cu)
11161 {
11162 struct type *this_type;
11163
11164 /* First see if we have it cached. */
11165
11166 if (is_ref_attr (attr))
11167 {
11168 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11169
11170 this_type = get_die_type_at_offset (offset, cu->per_cu);
11171 }
11172 else if (attr->form == DW_FORM_sig8)
11173 {
11174 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11175 struct dwarf2_cu *sig_cu;
11176 unsigned int offset;
11177
11178 /* sig_type will be NULL if the signatured type is missing from
11179 the debug info. */
11180 if (sig_type == NULL)
11181 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11182 "at 0x%x [in module %s]"),
11183 die->offset, cu->objfile->name);
11184
11185 gdb_assert (sig_type->per_cu.from_debug_types);
11186 offset = sig_type->offset + sig_type->type_offset;
11187 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11188 }
11189 else
11190 {
11191 dump_die_for_error (die);
11192 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11193 dwarf_attr_name (attr->name), cu->objfile->name);
11194 }
11195
11196 /* If not cached we need to read it in. */
11197
11198 if (this_type == NULL)
11199 {
11200 struct die_info *type_die;
11201 struct dwarf2_cu *type_cu = cu;
11202
11203 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11204 /* If the type is cached, we should have found it above. */
11205 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11206 this_type = read_type_die_1 (type_die, type_cu);
11207 }
11208
11209 /* If we still don't have a type use an error marker. */
11210
11211 if (this_type == NULL)
11212 {
11213 char *message, *saved;
11214
11215 /* read_type_die already issued a complaint. */
11216 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11217 cu->objfile->name,
11218 cu->header.offset,
11219 die->offset);
11220 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11221 message, strlen (message));
11222 xfree (message);
11223
11224 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11225 }
11226
11227 return this_type;
11228 }
11229
11230 /* Return the type in DIE, CU.
11231 Returns NULL for invalid types.
11232
11233 This first does a lookup in the appropriate type_hash table,
11234 and only reads the die in if necessary.
11235
11236 NOTE: This can be called when reading in partial or full symbols. */
11237
11238 static struct type *
11239 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11240 {
11241 struct type *this_type;
11242
11243 this_type = get_die_type (die, cu);
11244 if (this_type)
11245 return this_type;
11246
11247 return read_type_die_1 (die, cu);
11248 }
11249
11250 /* Read the type in DIE, CU.
11251 Returns NULL for invalid types. */
11252
11253 static struct type *
11254 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11255 {
11256 struct type *this_type = NULL;
11257
11258 switch (die->tag)
11259 {
11260 case DW_TAG_class_type:
11261 case DW_TAG_interface_type:
11262 case DW_TAG_structure_type:
11263 case DW_TAG_union_type:
11264 this_type = read_structure_type (die, cu);
11265 break;
11266 case DW_TAG_enumeration_type:
11267 this_type = read_enumeration_type (die, cu);
11268 break;
11269 case DW_TAG_subprogram:
11270 case DW_TAG_subroutine_type:
11271 case DW_TAG_inlined_subroutine:
11272 this_type = read_subroutine_type (die, cu);
11273 break;
11274 case DW_TAG_array_type:
11275 this_type = read_array_type (die, cu);
11276 break;
11277 case DW_TAG_set_type:
11278 this_type = read_set_type (die, cu);
11279 break;
11280 case DW_TAG_pointer_type:
11281 this_type = read_tag_pointer_type (die, cu);
11282 break;
11283 case DW_TAG_ptr_to_member_type:
11284 this_type = read_tag_ptr_to_member_type (die, cu);
11285 break;
11286 case DW_TAG_reference_type:
11287 this_type = read_tag_reference_type (die, cu);
11288 break;
11289 case DW_TAG_const_type:
11290 this_type = read_tag_const_type (die, cu);
11291 break;
11292 case DW_TAG_volatile_type:
11293 this_type = read_tag_volatile_type (die, cu);
11294 break;
11295 case DW_TAG_string_type:
11296 this_type = read_tag_string_type (die, cu);
11297 break;
11298 case DW_TAG_typedef:
11299 this_type = read_typedef (die, cu);
11300 break;
11301 case DW_TAG_subrange_type:
11302 this_type = read_subrange_type (die, cu);
11303 break;
11304 case DW_TAG_base_type:
11305 this_type = read_base_type (die, cu);
11306 break;
11307 case DW_TAG_unspecified_type:
11308 this_type = read_unspecified_type (die, cu);
11309 break;
11310 case DW_TAG_namespace:
11311 this_type = read_namespace_type (die, cu);
11312 break;
11313 case DW_TAG_module:
11314 this_type = read_module_type (die, cu);
11315 break;
11316 default:
11317 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
11318 dwarf_tag_name (die->tag));
11319 break;
11320 }
11321
11322 return this_type;
11323 }
11324
11325 /* See if we can figure out if the class lives in a namespace. We do
11326 this by looking for a member function; its demangled name will
11327 contain namespace info, if there is any.
11328 Return the computed name or NULL.
11329 Space for the result is allocated on the objfile's obstack.
11330 This is the full-die version of guess_partial_die_structure_name.
11331 In this case we know DIE has no useful parent. */
11332
11333 static char *
11334 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11335 {
11336 struct die_info *spec_die;
11337 struct dwarf2_cu *spec_cu;
11338 struct die_info *child;
11339
11340 spec_cu = cu;
11341 spec_die = die_specification (die, &spec_cu);
11342 if (spec_die != NULL)
11343 {
11344 die = spec_die;
11345 cu = spec_cu;
11346 }
11347
11348 for (child = die->child;
11349 child != NULL;
11350 child = child->sibling)
11351 {
11352 if (child->tag == DW_TAG_subprogram)
11353 {
11354 struct attribute *attr;
11355
11356 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11357 if (attr == NULL)
11358 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11359 if (attr != NULL)
11360 {
11361 char *actual_name
11362 = language_class_name_from_physname (cu->language_defn,
11363 DW_STRING (attr));
11364 char *name = NULL;
11365
11366 if (actual_name != NULL)
11367 {
11368 char *die_name = dwarf2_name (die, cu);
11369
11370 if (die_name != NULL
11371 && strcmp (die_name, actual_name) != 0)
11372 {
11373 /* Strip off the class name from the full name.
11374 We want the prefix. */
11375 int die_name_len = strlen (die_name);
11376 int actual_name_len = strlen (actual_name);
11377
11378 /* Test for '::' as a sanity check. */
11379 if (actual_name_len > die_name_len + 2
11380 && actual_name[actual_name_len - die_name_len - 1] == ':')
11381 name =
11382 obsavestring (actual_name,
11383 actual_name_len - die_name_len - 2,
11384 &cu->objfile->objfile_obstack);
11385 }
11386 }
11387 xfree (actual_name);
11388 return name;
11389 }
11390 }
11391 }
11392
11393 return NULL;
11394 }
11395
11396 /* Return the name of the namespace/class that DIE is defined within,
11397 or "" if we can't tell. The caller should not xfree the result.
11398
11399 For example, if we're within the method foo() in the following
11400 code:
11401
11402 namespace N {
11403 class C {
11404 void foo () {
11405 }
11406 };
11407 }
11408
11409 then determine_prefix on foo's die will return "N::C". */
11410
11411 static char *
11412 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11413 {
11414 struct die_info *parent, *spec_die;
11415 struct dwarf2_cu *spec_cu;
11416 struct type *parent_type;
11417
11418 if (cu->language != language_cplus && cu->language != language_java
11419 && cu->language != language_fortran)
11420 return "";
11421
11422 /* We have to be careful in the presence of DW_AT_specification.
11423 For example, with GCC 3.4, given the code
11424
11425 namespace N {
11426 void foo() {
11427 // Definition of N::foo.
11428 }
11429 }
11430
11431 then we'll have a tree of DIEs like this:
11432
11433 1: DW_TAG_compile_unit
11434 2: DW_TAG_namespace // N
11435 3: DW_TAG_subprogram // declaration of N::foo
11436 4: DW_TAG_subprogram // definition of N::foo
11437 DW_AT_specification // refers to die #3
11438
11439 Thus, when processing die #4, we have to pretend that we're in
11440 the context of its DW_AT_specification, namely the contex of die
11441 #3. */
11442 spec_cu = cu;
11443 spec_die = die_specification (die, &spec_cu);
11444 if (spec_die == NULL)
11445 parent = die->parent;
11446 else
11447 {
11448 parent = spec_die->parent;
11449 cu = spec_cu;
11450 }
11451
11452 if (parent == NULL)
11453 return "";
11454 else if (parent->building_fullname)
11455 {
11456 const char *name;
11457 const char *parent_name;
11458
11459 /* It has been seen on RealView 2.2 built binaries,
11460 DW_TAG_template_type_param types actually _defined_ as
11461 children of the parent class:
11462
11463 enum E {};
11464 template class <class Enum> Class{};
11465 Class<enum E> class_e;
11466
11467 1: DW_TAG_class_type (Class)
11468 2: DW_TAG_enumeration_type (E)
11469 3: DW_TAG_enumerator (enum1:0)
11470 3: DW_TAG_enumerator (enum2:1)
11471 ...
11472 2: DW_TAG_template_type_param
11473 DW_AT_type DW_FORM_ref_udata (E)
11474
11475 Besides being broken debug info, it can put GDB into an
11476 infinite loop. Consider:
11477
11478 When we're building the full name for Class<E>, we'll start
11479 at Class, and go look over its template type parameters,
11480 finding E. We'll then try to build the full name of E, and
11481 reach here. We're now trying to build the full name of E,
11482 and look over the parent DIE for containing scope. In the
11483 broken case, if we followed the parent DIE of E, we'd again
11484 find Class, and once again go look at its template type
11485 arguments, etc., etc. Simply don't consider such parent die
11486 as source-level parent of this die (it can't be, the language
11487 doesn't allow it), and break the loop here. */
11488 name = dwarf2_name (die, cu);
11489 parent_name = dwarf2_name (parent, cu);
11490 complaint (&symfile_complaints,
11491 _("template param type '%s' defined within parent '%s'"),
11492 name ? name : "<unknown>",
11493 parent_name ? parent_name : "<unknown>");
11494 return "";
11495 }
11496 else
11497 switch (parent->tag)
11498 {
11499 case DW_TAG_namespace:
11500 parent_type = read_type_die (parent, cu);
11501 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11502 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11503 Work around this problem here. */
11504 if (cu->language == language_cplus
11505 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11506 return "";
11507 /* We give a name to even anonymous namespaces. */
11508 return TYPE_TAG_NAME (parent_type);
11509 case DW_TAG_class_type:
11510 case DW_TAG_interface_type:
11511 case DW_TAG_structure_type:
11512 case DW_TAG_union_type:
11513 case DW_TAG_module:
11514 parent_type = read_type_die (parent, cu);
11515 if (TYPE_TAG_NAME (parent_type) != NULL)
11516 return TYPE_TAG_NAME (parent_type);
11517 else
11518 /* An anonymous structure is only allowed non-static data
11519 members; no typedefs, no member functions, et cetera.
11520 So it does not need a prefix. */
11521 return "";
11522 case DW_TAG_compile_unit:
11523 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11524 if (cu->language == language_cplus
11525 && dwarf2_per_objfile->types.asection != NULL
11526 && die->child != NULL
11527 && (die->tag == DW_TAG_class_type
11528 || die->tag == DW_TAG_structure_type
11529 || die->tag == DW_TAG_union_type))
11530 {
11531 char *name = guess_full_die_structure_name (die, cu);
11532 if (name != NULL)
11533 return name;
11534 }
11535 return "";
11536 default:
11537 return determine_prefix (parent, cu);
11538 }
11539 }
11540
11541 /* Return a newly-allocated string formed by concatenating PREFIX and
11542 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11543 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11544 perform an obconcat, otherwise allocate storage for the result. The CU argument
11545 is used to determine the language and hence, the appropriate separator. */
11546
11547 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
11548
11549 static char *
11550 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11551 int physname, struct dwarf2_cu *cu)
11552 {
11553 const char *lead = "";
11554 const char *sep;
11555
11556 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11557 sep = "";
11558 else if (cu->language == language_java)
11559 sep = ".";
11560 else if (cu->language == language_fortran && physname)
11561 {
11562 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11563 DW_AT_MIPS_linkage_name is preferred and used instead. */
11564
11565 lead = "__";
11566 sep = "_MOD_";
11567 }
11568 else
11569 sep = "::";
11570
11571 if (prefix == NULL)
11572 prefix = "";
11573 if (suffix == NULL)
11574 suffix = "";
11575
11576 if (obs == NULL)
11577 {
11578 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11579
11580 strcpy (retval, lead);
11581 strcat (retval, prefix);
11582 strcat (retval, sep);
11583 strcat (retval, suffix);
11584 return retval;
11585 }
11586 else
11587 {
11588 /* We have an obstack. */
11589 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11590 }
11591 }
11592
11593 /* Return sibling of die, NULL if no sibling. */
11594
11595 static struct die_info *
11596 sibling_die (struct die_info *die)
11597 {
11598 return die->sibling;
11599 }
11600
11601 /* Get name of a die, return NULL if not found. */
11602
11603 static char *
11604 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11605 struct obstack *obstack)
11606 {
11607 if (name && cu->language == language_cplus)
11608 {
11609 char *canon_name = cp_canonicalize_string (name);
11610
11611 if (canon_name != NULL)
11612 {
11613 if (strcmp (canon_name, name) != 0)
11614 name = obsavestring (canon_name, strlen (canon_name),
11615 obstack);
11616 xfree (canon_name);
11617 }
11618 }
11619
11620 return name;
11621 }
11622
11623 /* Get name of a die, return NULL if not found. */
11624
11625 static char *
11626 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11627 {
11628 struct attribute *attr;
11629
11630 attr = dwarf2_attr (die, DW_AT_name, cu);
11631 if (!attr || !DW_STRING (attr))
11632 return NULL;
11633
11634 switch (die->tag)
11635 {
11636 case DW_TAG_compile_unit:
11637 /* Compilation units have a DW_AT_name that is a filename, not
11638 a source language identifier. */
11639 case DW_TAG_enumeration_type:
11640 case DW_TAG_enumerator:
11641 /* These tags always have simple identifiers already; no need
11642 to canonicalize them. */
11643 return DW_STRING (attr);
11644
11645 case DW_TAG_subprogram:
11646 /* Java constructors will all be named "<init>", so return
11647 the class name when we see this special case. */
11648 if (cu->language == language_java
11649 && DW_STRING (attr) != NULL
11650 && strcmp (DW_STRING (attr), "<init>") == 0)
11651 {
11652 struct dwarf2_cu *spec_cu = cu;
11653 struct die_info *spec_die;
11654
11655 /* GCJ will output '<init>' for Java constructor names.
11656 For this special case, return the name of the parent class. */
11657
11658 /* GCJ may output suprogram DIEs with AT_specification set.
11659 If so, use the name of the specified DIE. */
11660 spec_die = die_specification (die, &spec_cu);
11661 if (spec_die != NULL)
11662 return dwarf2_name (spec_die, spec_cu);
11663
11664 do
11665 {
11666 die = die->parent;
11667 if (die->tag == DW_TAG_class_type)
11668 return dwarf2_name (die, cu);
11669 }
11670 while (die->tag != DW_TAG_compile_unit);
11671 }
11672 break;
11673
11674 case DW_TAG_class_type:
11675 case DW_TAG_interface_type:
11676 case DW_TAG_structure_type:
11677 case DW_TAG_union_type:
11678 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11679 structures or unions. These were of the form "._%d" in GCC 4.1,
11680 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11681 and GCC 4.4. We work around this problem by ignoring these. */
11682 if (strncmp (DW_STRING (attr), "._", 2) == 0
11683 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11684 return NULL;
11685 break;
11686
11687 default:
11688 break;
11689 }
11690
11691 if (!DW_STRING_IS_CANONICAL (attr))
11692 {
11693 DW_STRING (attr)
11694 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11695 &cu->objfile->objfile_obstack);
11696 DW_STRING_IS_CANONICAL (attr) = 1;
11697 }
11698 return DW_STRING (attr);
11699 }
11700
11701 /* Return the die that this die in an extension of, or NULL if there
11702 is none. *EXT_CU is the CU containing DIE on input, and the CU
11703 containing the return value on output. */
11704
11705 static struct die_info *
11706 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11707 {
11708 struct attribute *attr;
11709
11710 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11711 if (attr == NULL)
11712 return NULL;
11713
11714 return follow_die_ref (die, attr, ext_cu);
11715 }
11716
11717 /* Convert a DIE tag into its string name. */
11718
11719 static char *
11720 dwarf_tag_name (unsigned tag)
11721 {
11722 switch (tag)
11723 {
11724 case DW_TAG_padding:
11725 return "DW_TAG_padding";
11726 case DW_TAG_array_type:
11727 return "DW_TAG_array_type";
11728 case DW_TAG_class_type:
11729 return "DW_TAG_class_type";
11730 case DW_TAG_entry_point:
11731 return "DW_TAG_entry_point";
11732 case DW_TAG_enumeration_type:
11733 return "DW_TAG_enumeration_type";
11734 case DW_TAG_formal_parameter:
11735 return "DW_TAG_formal_parameter";
11736 case DW_TAG_imported_declaration:
11737 return "DW_TAG_imported_declaration";
11738 case DW_TAG_label:
11739 return "DW_TAG_label";
11740 case DW_TAG_lexical_block:
11741 return "DW_TAG_lexical_block";
11742 case DW_TAG_member:
11743 return "DW_TAG_member";
11744 case DW_TAG_pointer_type:
11745 return "DW_TAG_pointer_type";
11746 case DW_TAG_reference_type:
11747 return "DW_TAG_reference_type";
11748 case DW_TAG_compile_unit:
11749 return "DW_TAG_compile_unit";
11750 case DW_TAG_string_type:
11751 return "DW_TAG_string_type";
11752 case DW_TAG_structure_type:
11753 return "DW_TAG_structure_type";
11754 case DW_TAG_subroutine_type:
11755 return "DW_TAG_subroutine_type";
11756 case DW_TAG_typedef:
11757 return "DW_TAG_typedef";
11758 case DW_TAG_union_type:
11759 return "DW_TAG_union_type";
11760 case DW_TAG_unspecified_parameters:
11761 return "DW_TAG_unspecified_parameters";
11762 case DW_TAG_variant:
11763 return "DW_TAG_variant";
11764 case DW_TAG_common_block:
11765 return "DW_TAG_common_block";
11766 case DW_TAG_common_inclusion:
11767 return "DW_TAG_common_inclusion";
11768 case DW_TAG_inheritance:
11769 return "DW_TAG_inheritance";
11770 case DW_TAG_inlined_subroutine:
11771 return "DW_TAG_inlined_subroutine";
11772 case DW_TAG_module:
11773 return "DW_TAG_module";
11774 case DW_TAG_ptr_to_member_type:
11775 return "DW_TAG_ptr_to_member_type";
11776 case DW_TAG_set_type:
11777 return "DW_TAG_set_type";
11778 case DW_TAG_subrange_type:
11779 return "DW_TAG_subrange_type";
11780 case DW_TAG_with_stmt:
11781 return "DW_TAG_with_stmt";
11782 case DW_TAG_access_declaration:
11783 return "DW_TAG_access_declaration";
11784 case DW_TAG_base_type:
11785 return "DW_TAG_base_type";
11786 case DW_TAG_catch_block:
11787 return "DW_TAG_catch_block";
11788 case DW_TAG_const_type:
11789 return "DW_TAG_const_type";
11790 case DW_TAG_constant:
11791 return "DW_TAG_constant";
11792 case DW_TAG_enumerator:
11793 return "DW_TAG_enumerator";
11794 case DW_TAG_file_type:
11795 return "DW_TAG_file_type";
11796 case DW_TAG_friend:
11797 return "DW_TAG_friend";
11798 case DW_TAG_namelist:
11799 return "DW_TAG_namelist";
11800 case DW_TAG_namelist_item:
11801 return "DW_TAG_namelist_item";
11802 case DW_TAG_packed_type:
11803 return "DW_TAG_packed_type";
11804 case DW_TAG_subprogram:
11805 return "DW_TAG_subprogram";
11806 case DW_TAG_template_type_param:
11807 return "DW_TAG_template_type_param";
11808 case DW_TAG_template_value_param:
11809 return "DW_TAG_template_value_param";
11810 case DW_TAG_thrown_type:
11811 return "DW_TAG_thrown_type";
11812 case DW_TAG_try_block:
11813 return "DW_TAG_try_block";
11814 case DW_TAG_variant_part:
11815 return "DW_TAG_variant_part";
11816 case DW_TAG_variable:
11817 return "DW_TAG_variable";
11818 case DW_TAG_volatile_type:
11819 return "DW_TAG_volatile_type";
11820 case DW_TAG_dwarf_procedure:
11821 return "DW_TAG_dwarf_procedure";
11822 case DW_TAG_restrict_type:
11823 return "DW_TAG_restrict_type";
11824 case DW_TAG_interface_type:
11825 return "DW_TAG_interface_type";
11826 case DW_TAG_namespace:
11827 return "DW_TAG_namespace";
11828 case DW_TAG_imported_module:
11829 return "DW_TAG_imported_module";
11830 case DW_TAG_unspecified_type:
11831 return "DW_TAG_unspecified_type";
11832 case DW_TAG_partial_unit:
11833 return "DW_TAG_partial_unit";
11834 case DW_TAG_imported_unit:
11835 return "DW_TAG_imported_unit";
11836 case DW_TAG_condition:
11837 return "DW_TAG_condition";
11838 case DW_TAG_shared_type:
11839 return "DW_TAG_shared_type";
11840 case DW_TAG_type_unit:
11841 return "DW_TAG_type_unit";
11842 case DW_TAG_MIPS_loop:
11843 return "DW_TAG_MIPS_loop";
11844 case DW_TAG_HP_array_descriptor:
11845 return "DW_TAG_HP_array_descriptor";
11846 case DW_TAG_format_label:
11847 return "DW_TAG_format_label";
11848 case DW_TAG_function_template:
11849 return "DW_TAG_function_template";
11850 case DW_TAG_class_template:
11851 return "DW_TAG_class_template";
11852 case DW_TAG_GNU_BINCL:
11853 return "DW_TAG_GNU_BINCL";
11854 case DW_TAG_GNU_EINCL:
11855 return "DW_TAG_GNU_EINCL";
11856 case DW_TAG_upc_shared_type:
11857 return "DW_TAG_upc_shared_type";
11858 case DW_TAG_upc_strict_type:
11859 return "DW_TAG_upc_strict_type";
11860 case DW_TAG_upc_relaxed_type:
11861 return "DW_TAG_upc_relaxed_type";
11862 case DW_TAG_PGI_kanji_type:
11863 return "DW_TAG_PGI_kanji_type";
11864 case DW_TAG_PGI_interface_block:
11865 return "DW_TAG_PGI_interface_block";
11866 default:
11867 return "DW_TAG_<unknown>";
11868 }
11869 }
11870
11871 /* Convert a DWARF attribute code into its string name. */
11872
11873 static char *
11874 dwarf_attr_name (unsigned attr)
11875 {
11876 switch (attr)
11877 {
11878 case DW_AT_sibling:
11879 return "DW_AT_sibling";
11880 case DW_AT_location:
11881 return "DW_AT_location";
11882 case DW_AT_name:
11883 return "DW_AT_name";
11884 case DW_AT_ordering:
11885 return "DW_AT_ordering";
11886 case DW_AT_subscr_data:
11887 return "DW_AT_subscr_data";
11888 case DW_AT_byte_size:
11889 return "DW_AT_byte_size";
11890 case DW_AT_bit_offset:
11891 return "DW_AT_bit_offset";
11892 case DW_AT_bit_size:
11893 return "DW_AT_bit_size";
11894 case DW_AT_element_list:
11895 return "DW_AT_element_list";
11896 case DW_AT_stmt_list:
11897 return "DW_AT_stmt_list";
11898 case DW_AT_low_pc:
11899 return "DW_AT_low_pc";
11900 case DW_AT_high_pc:
11901 return "DW_AT_high_pc";
11902 case DW_AT_language:
11903 return "DW_AT_language";
11904 case DW_AT_member:
11905 return "DW_AT_member";
11906 case DW_AT_discr:
11907 return "DW_AT_discr";
11908 case DW_AT_discr_value:
11909 return "DW_AT_discr_value";
11910 case DW_AT_visibility:
11911 return "DW_AT_visibility";
11912 case DW_AT_import:
11913 return "DW_AT_import";
11914 case DW_AT_string_length:
11915 return "DW_AT_string_length";
11916 case DW_AT_common_reference:
11917 return "DW_AT_common_reference";
11918 case DW_AT_comp_dir:
11919 return "DW_AT_comp_dir";
11920 case DW_AT_const_value:
11921 return "DW_AT_const_value";
11922 case DW_AT_containing_type:
11923 return "DW_AT_containing_type";
11924 case DW_AT_default_value:
11925 return "DW_AT_default_value";
11926 case DW_AT_inline:
11927 return "DW_AT_inline";
11928 case DW_AT_is_optional:
11929 return "DW_AT_is_optional";
11930 case DW_AT_lower_bound:
11931 return "DW_AT_lower_bound";
11932 case DW_AT_producer:
11933 return "DW_AT_producer";
11934 case DW_AT_prototyped:
11935 return "DW_AT_prototyped";
11936 case DW_AT_return_addr:
11937 return "DW_AT_return_addr";
11938 case DW_AT_start_scope:
11939 return "DW_AT_start_scope";
11940 case DW_AT_bit_stride:
11941 return "DW_AT_bit_stride";
11942 case DW_AT_upper_bound:
11943 return "DW_AT_upper_bound";
11944 case DW_AT_abstract_origin:
11945 return "DW_AT_abstract_origin";
11946 case DW_AT_accessibility:
11947 return "DW_AT_accessibility";
11948 case DW_AT_address_class:
11949 return "DW_AT_address_class";
11950 case DW_AT_artificial:
11951 return "DW_AT_artificial";
11952 case DW_AT_base_types:
11953 return "DW_AT_base_types";
11954 case DW_AT_calling_convention:
11955 return "DW_AT_calling_convention";
11956 case DW_AT_count:
11957 return "DW_AT_count";
11958 case DW_AT_data_member_location:
11959 return "DW_AT_data_member_location";
11960 case DW_AT_decl_column:
11961 return "DW_AT_decl_column";
11962 case DW_AT_decl_file:
11963 return "DW_AT_decl_file";
11964 case DW_AT_decl_line:
11965 return "DW_AT_decl_line";
11966 case DW_AT_declaration:
11967 return "DW_AT_declaration";
11968 case DW_AT_discr_list:
11969 return "DW_AT_discr_list";
11970 case DW_AT_encoding:
11971 return "DW_AT_encoding";
11972 case DW_AT_external:
11973 return "DW_AT_external";
11974 case DW_AT_frame_base:
11975 return "DW_AT_frame_base";
11976 case DW_AT_friend:
11977 return "DW_AT_friend";
11978 case DW_AT_identifier_case:
11979 return "DW_AT_identifier_case";
11980 case DW_AT_macro_info:
11981 return "DW_AT_macro_info";
11982 case DW_AT_namelist_items:
11983 return "DW_AT_namelist_items";
11984 case DW_AT_priority:
11985 return "DW_AT_priority";
11986 case DW_AT_segment:
11987 return "DW_AT_segment";
11988 case DW_AT_specification:
11989 return "DW_AT_specification";
11990 case DW_AT_static_link:
11991 return "DW_AT_static_link";
11992 case DW_AT_type:
11993 return "DW_AT_type";
11994 case DW_AT_use_location:
11995 return "DW_AT_use_location";
11996 case DW_AT_variable_parameter:
11997 return "DW_AT_variable_parameter";
11998 case DW_AT_virtuality:
11999 return "DW_AT_virtuality";
12000 case DW_AT_vtable_elem_location:
12001 return "DW_AT_vtable_elem_location";
12002 /* DWARF 3 values. */
12003 case DW_AT_allocated:
12004 return "DW_AT_allocated";
12005 case DW_AT_associated:
12006 return "DW_AT_associated";
12007 case DW_AT_data_location:
12008 return "DW_AT_data_location";
12009 case DW_AT_byte_stride:
12010 return "DW_AT_byte_stride";
12011 case DW_AT_entry_pc:
12012 return "DW_AT_entry_pc";
12013 case DW_AT_use_UTF8:
12014 return "DW_AT_use_UTF8";
12015 case DW_AT_extension:
12016 return "DW_AT_extension";
12017 case DW_AT_ranges:
12018 return "DW_AT_ranges";
12019 case DW_AT_trampoline:
12020 return "DW_AT_trampoline";
12021 case DW_AT_call_column:
12022 return "DW_AT_call_column";
12023 case DW_AT_call_file:
12024 return "DW_AT_call_file";
12025 case DW_AT_call_line:
12026 return "DW_AT_call_line";
12027 case DW_AT_description:
12028 return "DW_AT_description";
12029 case DW_AT_binary_scale:
12030 return "DW_AT_binary_scale";
12031 case DW_AT_decimal_scale:
12032 return "DW_AT_decimal_scale";
12033 case DW_AT_small:
12034 return "DW_AT_small";
12035 case DW_AT_decimal_sign:
12036 return "DW_AT_decimal_sign";
12037 case DW_AT_digit_count:
12038 return "DW_AT_digit_count";
12039 case DW_AT_picture_string:
12040 return "DW_AT_picture_string";
12041 case DW_AT_mutable:
12042 return "DW_AT_mutable";
12043 case DW_AT_threads_scaled:
12044 return "DW_AT_threads_scaled";
12045 case DW_AT_explicit:
12046 return "DW_AT_explicit";
12047 case DW_AT_object_pointer:
12048 return "DW_AT_object_pointer";
12049 case DW_AT_endianity:
12050 return "DW_AT_endianity";
12051 case DW_AT_elemental:
12052 return "DW_AT_elemental";
12053 case DW_AT_pure:
12054 return "DW_AT_pure";
12055 case DW_AT_recursive:
12056 return "DW_AT_recursive";
12057 /* DWARF 4 values. */
12058 case DW_AT_signature:
12059 return "DW_AT_signature";
12060 case DW_AT_linkage_name:
12061 return "DW_AT_linkage_name";
12062 /* SGI/MIPS extensions. */
12063 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12064 case DW_AT_MIPS_fde:
12065 return "DW_AT_MIPS_fde";
12066 #endif
12067 case DW_AT_MIPS_loop_begin:
12068 return "DW_AT_MIPS_loop_begin";
12069 case DW_AT_MIPS_tail_loop_begin:
12070 return "DW_AT_MIPS_tail_loop_begin";
12071 case DW_AT_MIPS_epilog_begin:
12072 return "DW_AT_MIPS_epilog_begin";
12073 case DW_AT_MIPS_loop_unroll_factor:
12074 return "DW_AT_MIPS_loop_unroll_factor";
12075 case DW_AT_MIPS_software_pipeline_depth:
12076 return "DW_AT_MIPS_software_pipeline_depth";
12077 case DW_AT_MIPS_linkage_name:
12078 return "DW_AT_MIPS_linkage_name";
12079 case DW_AT_MIPS_stride:
12080 return "DW_AT_MIPS_stride";
12081 case DW_AT_MIPS_abstract_name:
12082 return "DW_AT_MIPS_abstract_name";
12083 case DW_AT_MIPS_clone_origin:
12084 return "DW_AT_MIPS_clone_origin";
12085 case DW_AT_MIPS_has_inlines:
12086 return "DW_AT_MIPS_has_inlines";
12087 /* HP extensions. */
12088 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12089 case DW_AT_HP_block_index:
12090 return "DW_AT_HP_block_index";
12091 #endif
12092 case DW_AT_HP_unmodifiable:
12093 return "DW_AT_HP_unmodifiable";
12094 case DW_AT_HP_actuals_stmt_list:
12095 return "DW_AT_HP_actuals_stmt_list";
12096 case DW_AT_HP_proc_per_section:
12097 return "DW_AT_HP_proc_per_section";
12098 case DW_AT_HP_raw_data_ptr:
12099 return "DW_AT_HP_raw_data_ptr";
12100 case DW_AT_HP_pass_by_reference:
12101 return "DW_AT_HP_pass_by_reference";
12102 case DW_AT_HP_opt_level:
12103 return "DW_AT_HP_opt_level";
12104 case DW_AT_HP_prof_version_id:
12105 return "DW_AT_HP_prof_version_id";
12106 case DW_AT_HP_opt_flags:
12107 return "DW_AT_HP_opt_flags";
12108 case DW_AT_HP_cold_region_low_pc:
12109 return "DW_AT_HP_cold_region_low_pc";
12110 case DW_AT_HP_cold_region_high_pc:
12111 return "DW_AT_HP_cold_region_high_pc";
12112 case DW_AT_HP_all_variables_modifiable:
12113 return "DW_AT_HP_all_variables_modifiable";
12114 case DW_AT_HP_linkage_name:
12115 return "DW_AT_HP_linkage_name";
12116 case DW_AT_HP_prof_flags:
12117 return "DW_AT_HP_prof_flags";
12118 /* GNU extensions. */
12119 case DW_AT_sf_names:
12120 return "DW_AT_sf_names";
12121 case DW_AT_src_info:
12122 return "DW_AT_src_info";
12123 case DW_AT_mac_info:
12124 return "DW_AT_mac_info";
12125 case DW_AT_src_coords:
12126 return "DW_AT_src_coords";
12127 case DW_AT_body_begin:
12128 return "DW_AT_body_begin";
12129 case DW_AT_body_end:
12130 return "DW_AT_body_end";
12131 case DW_AT_GNU_vector:
12132 return "DW_AT_GNU_vector";
12133 case DW_AT_GNU_odr_signature:
12134 return "DW_AT_GNU_odr_signature";
12135 /* VMS extensions. */
12136 case DW_AT_VMS_rtnbeg_pd_address:
12137 return "DW_AT_VMS_rtnbeg_pd_address";
12138 /* UPC extension. */
12139 case DW_AT_upc_threads_scaled:
12140 return "DW_AT_upc_threads_scaled";
12141 /* PGI (STMicroelectronics) extensions. */
12142 case DW_AT_PGI_lbase:
12143 return "DW_AT_PGI_lbase";
12144 case DW_AT_PGI_soffset:
12145 return "DW_AT_PGI_soffset";
12146 case DW_AT_PGI_lstride:
12147 return "DW_AT_PGI_lstride";
12148 default:
12149 return "DW_AT_<unknown>";
12150 }
12151 }
12152
12153 /* Convert a DWARF value form code into its string name. */
12154
12155 static char *
12156 dwarf_form_name (unsigned form)
12157 {
12158 switch (form)
12159 {
12160 case DW_FORM_addr:
12161 return "DW_FORM_addr";
12162 case DW_FORM_block2:
12163 return "DW_FORM_block2";
12164 case DW_FORM_block4:
12165 return "DW_FORM_block4";
12166 case DW_FORM_data2:
12167 return "DW_FORM_data2";
12168 case DW_FORM_data4:
12169 return "DW_FORM_data4";
12170 case DW_FORM_data8:
12171 return "DW_FORM_data8";
12172 case DW_FORM_string:
12173 return "DW_FORM_string";
12174 case DW_FORM_block:
12175 return "DW_FORM_block";
12176 case DW_FORM_block1:
12177 return "DW_FORM_block1";
12178 case DW_FORM_data1:
12179 return "DW_FORM_data1";
12180 case DW_FORM_flag:
12181 return "DW_FORM_flag";
12182 case DW_FORM_sdata:
12183 return "DW_FORM_sdata";
12184 case DW_FORM_strp:
12185 return "DW_FORM_strp";
12186 case DW_FORM_udata:
12187 return "DW_FORM_udata";
12188 case DW_FORM_ref_addr:
12189 return "DW_FORM_ref_addr";
12190 case DW_FORM_ref1:
12191 return "DW_FORM_ref1";
12192 case DW_FORM_ref2:
12193 return "DW_FORM_ref2";
12194 case DW_FORM_ref4:
12195 return "DW_FORM_ref4";
12196 case DW_FORM_ref8:
12197 return "DW_FORM_ref8";
12198 case DW_FORM_ref_udata:
12199 return "DW_FORM_ref_udata";
12200 case DW_FORM_indirect:
12201 return "DW_FORM_indirect";
12202 case DW_FORM_sec_offset:
12203 return "DW_FORM_sec_offset";
12204 case DW_FORM_exprloc:
12205 return "DW_FORM_exprloc";
12206 case DW_FORM_flag_present:
12207 return "DW_FORM_flag_present";
12208 case DW_FORM_sig8:
12209 return "DW_FORM_sig8";
12210 default:
12211 return "DW_FORM_<unknown>";
12212 }
12213 }
12214
12215 /* Convert a DWARF stack opcode into its string name. */
12216
12217 const char *
12218 dwarf_stack_op_name (unsigned op, int def)
12219 {
12220 switch (op)
12221 {
12222 case DW_OP_addr:
12223 return "DW_OP_addr";
12224 case DW_OP_deref:
12225 return "DW_OP_deref";
12226 case DW_OP_const1u:
12227 return "DW_OP_const1u";
12228 case DW_OP_const1s:
12229 return "DW_OP_const1s";
12230 case DW_OP_const2u:
12231 return "DW_OP_const2u";
12232 case DW_OP_const2s:
12233 return "DW_OP_const2s";
12234 case DW_OP_const4u:
12235 return "DW_OP_const4u";
12236 case DW_OP_const4s:
12237 return "DW_OP_const4s";
12238 case DW_OP_const8u:
12239 return "DW_OP_const8u";
12240 case DW_OP_const8s:
12241 return "DW_OP_const8s";
12242 case DW_OP_constu:
12243 return "DW_OP_constu";
12244 case DW_OP_consts:
12245 return "DW_OP_consts";
12246 case DW_OP_dup:
12247 return "DW_OP_dup";
12248 case DW_OP_drop:
12249 return "DW_OP_drop";
12250 case DW_OP_over:
12251 return "DW_OP_over";
12252 case DW_OP_pick:
12253 return "DW_OP_pick";
12254 case DW_OP_swap:
12255 return "DW_OP_swap";
12256 case DW_OP_rot:
12257 return "DW_OP_rot";
12258 case DW_OP_xderef:
12259 return "DW_OP_xderef";
12260 case DW_OP_abs:
12261 return "DW_OP_abs";
12262 case DW_OP_and:
12263 return "DW_OP_and";
12264 case DW_OP_div:
12265 return "DW_OP_div";
12266 case DW_OP_minus:
12267 return "DW_OP_minus";
12268 case DW_OP_mod:
12269 return "DW_OP_mod";
12270 case DW_OP_mul:
12271 return "DW_OP_mul";
12272 case DW_OP_neg:
12273 return "DW_OP_neg";
12274 case DW_OP_not:
12275 return "DW_OP_not";
12276 case DW_OP_or:
12277 return "DW_OP_or";
12278 case DW_OP_plus:
12279 return "DW_OP_plus";
12280 case DW_OP_plus_uconst:
12281 return "DW_OP_plus_uconst";
12282 case DW_OP_shl:
12283 return "DW_OP_shl";
12284 case DW_OP_shr:
12285 return "DW_OP_shr";
12286 case DW_OP_shra:
12287 return "DW_OP_shra";
12288 case DW_OP_xor:
12289 return "DW_OP_xor";
12290 case DW_OP_bra:
12291 return "DW_OP_bra";
12292 case DW_OP_eq:
12293 return "DW_OP_eq";
12294 case DW_OP_ge:
12295 return "DW_OP_ge";
12296 case DW_OP_gt:
12297 return "DW_OP_gt";
12298 case DW_OP_le:
12299 return "DW_OP_le";
12300 case DW_OP_lt:
12301 return "DW_OP_lt";
12302 case DW_OP_ne:
12303 return "DW_OP_ne";
12304 case DW_OP_skip:
12305 return "DW_OP_skip";
12306 case DW_OP_lit0:
12307 return "DW_OP_lit0";
12308 case DW_OP_lit1:
12309 return "DW_OP_lit1";
12310 case DW_OP_lit2:
12311 return "DW_OP_lit2";
12312 case DW_OP_lit3:
12313 return "DW_OP_lit3";
12314 case DW_OP_lit4:
12315 return "DW_OP_lit4";
12316 case DW_OP_lit5:
12317 return "DW_OP_lit5";
12318 case DW_OP_lit6:
12319 return "DW_OP_lit6";
12320 case DW_OP_lit7:
12321 return "DW_OP_lit7";
12322 case DW_OP_lit8:
12323 return "DW_OP_lit8";
12324 case DW_OP_lit9:
12325 return "DW_OP_lit9";
12326 case DW_OP_lit10:
12327 return "DW_OP_lit10";
12328 case DW_OP_lit11:
12329 return "DW_OP_lit11";
12330 case DW_OP_lit12:
12331 return "DW_OP_lit12";
12332 case DW_OP_lit13:
12333 return "DW_OP_lit13";
12334 case DW_OP_lit14:
12335 return "DW_OP_lit14";
12336 case DW_OP_lit15:
12337 return "DW_OP_lit15";
12338 case DW_OP_lit16:
12339 return "DW_OP_lit16";
12340 case DW_OP_lit17:
12341 return "DW_OP_lit17";
12342 case DW_OP_lit18:
12343 return "DW_OP_lit18";
12344 case DW_OP_lit19:
12345 return "DW_OP_lit19";
12346 case DW_OP_lit20:
12347 return "DW_OP_lit20";
12348 case DW_OP_lit21:
12349 return "DW_OP_lit21";
12350 case DW_OP_lit22:
12351 return "DW_OP_lit22";
12352 case DW_OP_lit23:
12353 return "DW_OP_lit23";
12354 case DW_OP_lit24:
12355 return "DW_OP_lit24";
12356 case DW_OP_lit25:
12357 return "DW_OP_lit25";
12358 case DW_OP_lit26:
12359 return "DW_OP_lit26";
12360 case DW_OP_lit27:
12361 return "DW_OP_lit27";
12362 case DW_OP_lit28:
12363 return "DW_OP_lit28";
12364 case DW_OP_lit29:
12365 return "DW_OP_lit29";
12366 case DW_OP_lit30:
12367 return "DW_OP_lit30";
12368 case DW_OP_lit31:
12369 return "DW_OP_lit31";
12370 case DW_OP_reg0:
12371 return "DW_OP_reg0";
12372 case DW_OP_reg1:
12373 return "DW_OP_reg1";
12374 case DW_OP_reg2:
12375 return "DW_OP_reg2";
12376 case DW_OP_reg3:
12377 return "DW_OP_reg3";
12378 case DW_OP_reg4:
12379 return "DW_OP_reg4";
12380 case DW_OP_reg5:
12381 return "DW_OP_reg5";
12382 case DW_OP_reg6:
12383 return "DW_OP_reg6";
12384 case DW_OP_reg7:
12385 return "DW_OP_reg7";
12386 case DW_OP_reg8:
12387 return "DW_OP_reg8";
12388 case DW_OP_reg9:
12389 return "DW_OP_reg9";
12390 case DW_OP_reg10:
12391 return "DW_OP_reg10";
12392 case DW_OP_reg11:
12393 return "DW_OP_reg11";
12394 case DW_OP_reg12:
12395 return "DW_OP_reg12";
12396 case DW_OP_reg13:
12397 return "DW_OP_reg13";
12398 case DW_OP_reg14:
12399 return "DW_OP_reg14";
12400 case DW_OP_reg15:
12401 return "DW_OP_reg15";
12402 case DW_OP_reg16:
12403 return "DW_OP_reg16";
12404 case DW_OP_reg17:
12405 return "DW_OP_reg17";
12406 case DW_OP_reg18:
12407 return "DW_OP_reg18";
12408 case DW_OP_reg19:
12409 return "DW_OP_reg19";
12410 case DW_OP_reg20:
12411 return "DW_OP_reg20";
12412 case DW_OP_reg21:
12413 return "DW_OP_reg21";
12414 case DW_OP_reg22:
12415 return "DW_OP_reg22";
12416 case DW_OP_reg23:
12417 return "DW_OP_reg23";
12418 case DW_OP_reg24:
12419 return "DW_OP_reg24";
12420 case DW_OP_reg25:
12421 return "DW_OP_reg25";
12422 case DW_OP_reg26:
12423 return "DW_OP_reg26";
12424 case DW_OP_reg27:
12425 return "DW_OP_reg27";
12426 case DW_OP_reg28:
12427 return "DW_OP_reg28";
12428 case DW_OP_reg29:
12429 return "DW_OP_reg29";
12430 case DW_OP_reg30:
12431 return "DW_OP_reg30";
12432 case DW_OP_reg31:
12433 return "DW_OP_reg31";
12434 case DW_OP_breg0:
12435 return "DW_OP_breg0";
12436 case DW_OP_breg1:
12437 return "DW_OP_breg1";
12438 case DW_OP_breg2:
12439 return "DW_OP_breg2";
12440 case DW_OP_breg3:
12441 return "DW_OP_breg3";
12442 case DW_OP_breg4:
12443 return "DW_OP_breg4";
12444 case DW_OP_breg5:
12445 return "DW_OP_breg5";
12446 case DW_OP_breg6:
12447 return "DW_OP_breg6";
12448 case DW_OP_breg7:
12449 return "DW_OP_breg7";
12450 case DW_OP_breg8:
12451 return "DW_OP_breg8";
12452 case DW_OP_breg9:
12453 return "DW_OP_breg9";
12454 case DW_OP_breg10:
12455 return "DW_OP_breg10";
12456 case DW_OP_breg11:
12457 return "DW_OP_breg11";
12458 case DW_OP_breg12:
12459 return "DW_OP_breg12";
12460 case DW_OP_breg13:
12461 return "DW_OP_breg13";
12462 case DW_OP_breg14:
12463 return "DW_OP_breg14";
12464 case DW_OP_breg15:
12465 return "DW_OP_breg15";
12466 case DW_OP_breg16:
12467 return "DW_OP_breg16";
12468 case DW_OP_breg17:
12469 return "DW_OP_breg17";
12470 case DW_OP_breg18:
12471 return "DW_OP_breg18";
12472 case DW_OP_breg19:
12473 return "DW_OP_breg19";
12474 case DW_OP_breg20:
12475 return "DW_OP_breg20";
12476 case DW_OP_breg21:
12477 return "DW_OP_breg21";
12478 case DW_OP_breg22:
12479 return "DW_OP_breg22";
12480 case DW_OP_breg23:
12481 return "DW_OP_breg23";
12482 case DW_OP_breg24:
12483 return "DW_OP_breg24";
12484 case DW_OP_breg25:
12485 return "DW_OP_breg25";
12486 case DW_OP_breg26:
12487 return "DW_OP_breg26";
12488 case DW_OP_breg27:
12489 return "DW_OP_breg27";
12490 case DW_OP_breg28:
12491 return "DW_OP_breg28";
12492 case DW_OP_breg29:
12493 return "DW_OP_breg29";
12494 case DW_OP_breg30:
12495 return "DW_OP_breg30";
12496 case DW_OP_breg31:
12497 return "DW_OP_breg31";
12498 case DW_OP_regx:
12499 return "DW_OP_regx";
12500 case DW_OP_fbreg:
12501 return "DW_OP_fbreg";
12502 case DW_OP_bregx:
12503 return "DW_OP_bregx";
12504 case DW_OP_piece:
12505 return "DW_OP_piece";
12506 case DW_OP_deref_size:
12507 return "DW_OP_deref_size";
12508 case DW_OP_xderef_size:
12509 return "DW_OP_xderef_size";
12510 case DW_OP_nop:
12511 return "DW_OP_nop";
12512 /* DWARF 3 extensions. */
12513 case DW_OP_push_object_address:
12514 return "DW_OP_push_object_address";
12515 case DW_OP_call2:
12516 return "DW_OP_call2";
12517 case DW_OP_call4:
12518 return "DW_OP_call4";
12519 case DW_OP_call_ref:
12520 return "DW_OP_call_ref";
12521 case DW_OP_form_tls_address:
12522 return "DW_OP_form_tls_address";
12523 case DW_OP_call_frame_cfa:
12524 return "DW_OP_call_frame_cfa";
12525 case DW_OP_bit_piece:
12526 return "DW_OP_bit_piece";
12527 /* DWARF 4 extensions. */
12528 case DW_OP_implicit_value:
12529 return "DW_OP_implicit_value";
12530 case DW_OP_stack_value:
12531 return "DW_OP_stack_value";
12532 /* GNU extensions. */
12533 case DW_OP_GNU_push_tls_address:
12534 return "DW_OP_GNU_push_tls_address";
12535 case DW_OP_GNU_uninit:
12536 return "DW_OP_GNU_uninit";
12537 default:
12538 return def ? "OP_<unknown>" : NULL;
12539 }
12540 }
12541
12542 static char *
12543 dwarf_bool_name (unsigned mybool)
12544 {
12545 if (mybool)
12546 return "TRUE";
12547 else
12548 return "FALSE";
12549 }
12550
12551 /* Convert a DWARF type code into its string name. */
12552
12553 static char *
12554 dwarf_type_encoding_name (unsigned enc)
12555 {
12556 switch (enc)
12557 {
12558 case DW_ATE_void:
12559 return "DW_ATE_void";
12560 case DW_ATE_address:
12561 return "DW_ATE_address";
12562 case DW_ATE_boolean:
12563 return "DW_ATE_boolean";
12564 case DW_ATE_complex_float:
12565 return "DW_ATE_complex_float";
12566 case DW_ATE_float:
12567 return "DW_ATE_float";
12568 case DW_ATE_signed:
12569 return "DW_ATE_signed";
12570 case DW_ATE_signed_char:
12571 return "DW_ATE_signed_char";
12572 case DW_ATE_unsigned:
12573 return "DW_ATE_unsigned";
12574 case DW_ATE_unsigned_char:
12575 return "DW_ATE_unsigned_char";
12576 /* DWARF 3. */
12577 case DW_ATE_imaginary_float:
12578 return "DW_ATE_imaginary_float";
12579 case DW_ATE_packed_decimal:
12580 return "DW_ATE_packed_decimal";
12581 case DW_ATE_numeric_string:
12582 return "DW_ATE_numeric_string";
12583 case DW_ATE_edited:
12584 return "DW_ATE_edited";
12585 case DW_ATE_signed_fixed:
12586 return "DW_ATE_signed_fixed";
12587 case DW_ATE_unsigned_fixed:
12588 return "DW_ATE_unsigned_fixed";
12589 case DW_ATE_decimal_float:
12590 return "DW_ATE_decimal_float";
12591 /* DWARF 4. */
12592 case DW_ATE_UTF:
12593 return "DW_ATE_UTF";
12594 /* HP extensions. */
12595 case DW_ATE_HP_float80:
12596 return "DW_ATE_HP_float80";
12597 case DW_ATE_HP_complex_float80:
12598 return "DW_ATE_HP_complex_float80";
12599 case DW_ATE_HP_float128:
12600 return "DW_ATE_HP_float128";
12601 case DW_ATE_HP_complex_float128:
12602 return "DW_ATE_HP_complex_float128";
12603 case DW_ATE_HP_floathpintel:
12604 return "DW_ATE_HP_floathpintel";
12605 case DW_ATE_HP_imaginary_float80:
12606 return "DW_ATE_HP_imaginary_float80";
12607 case DW_ATE_HP_imaginary_float128:
12608 return "DW_ATE_HP_imaginary_float128";
12609 default:
12610 return "DW_ATE_<unknown>";
12611 }
12612 }
12613
12614 /* Convert a DWARF call frame info operation to its string name. */
12615
12616 #if 0
12617 static char *
12618 dwarf_cfi_name (unsigned cfi_opc)
12619 {
12620 switch (cfi_opc)
12621 {
12622 case DW_CFA_advance_loc:
12623 return "DW_CFA_advance_loc";
12624 case DW_CFA_offset:
12625 return "DW_CFA_offset";
12626 case DW_CFA_restore:
12627 return "DW_CFA_restore";
12628 case DW_CFA_nop:
12629 return "DW_CFA_nop";
12630 case DW_CFA_set_loc:
12631 return "DW_CFA_set_loc";
12632 case DW_CFA_advance_loc1:
12633 return "DW_CFA_advance_loc1";
12634 case DW_CFA_advance_loc2:
12635 return "DW_CFA_advance_loc2";
12636 case DW_CFA_advance_loc4:
12637 return "DW_CFA_advance_loc4";
12638 case DW_CFA_offset_extended:
12639 return "DW_CFA_offset_extended";
12640 case DW_CFA_restore_extended:
12641 return "DW_CFA_restore_extended";
12642 case DW_CFA_undefined:
12643 return "DW_CFA_undefined";
12644 case DW_CFA_same_value:
12645 return "DW_CFA_same_value";
12646 case DW_CFA_register:
12647 return "DW_CFA_register";
12648 case DW_CFA_remember_state:
12649 return "DW_CFA_remember_state";
12650 case DW_CFA_restore_state:
12651 return "DW_CFA_restore_state";
12652 case DW_CFA_def_cfa:
12653 return "DW_CFA_def_cfa";
12654 case DW_CFA_def_cfa_register:
12655 return "DW_CFA_def_cfa_register";
12656 case DW_CFA_def_cfa_offset:
12657 return "DW_CFA_def_cfa_offset";
12658 /* DWARF 3. */
12659 case DW_CFA_def_cfa_expression:
12660 return "DW_CFA_def_cfa_expression";
12661 case DW_CFA_expression:
12662 return "DW_CFA_expression";
12663 case DW_CFA_offset_extended_sf:
12664 return "DW_CFA_offset_extended_sf";
12665 case DW_CFA_def_cfa_sf:
12666 return "DW_CFA_def_cfa_sf";
12667 case DW_CFA_def_cfa_offset_sf:
12668 return "DW_CFA_def_cfa_offset_sf";
12669 case DW_CFA_val_offset:
12670 return "DW_CFA_val_offset";
12671 case DW_CFA_val_offset_sf:
12672 return "DW_CFA_val_offset_sf";
12673 case DW_CFA_val_expression:
12674 return "DW_CFA_val_expression";
12675 /* SGI/MIPS specific. */
12676 case DW_CFA_MIPS_advance_loc8:
12677 return "DW_CFA_MIPS_advance_loc8";
12678 /* GNU extensions. */
12679 case DW_CFA_GNU_window_save:
12680 return "DW_CFA_GNU_window_save";
12681 case DW_CFA_GNU_args_size:
12682 return "DW_CFA_GNU_args_size";
12683 case DW_CFA_GNU_negative_offset_extended:
12684 return "DW_CFA_GNU_negative_offset_extended";
12685 default:
12686 return "DW_CFA_<unknown>";
12687 }
12688 }
12689 #endif
12690
12691 static void
12692 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12693 {
12694 unsigned int i;
12695
12696 print_spaces (indent, f);
12697 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12698 dwarf_tag_name (die->tag), die->abbrev, die->offset);
12699
12700 if (die->parent != NULL)
12701 {
12702 print_spaces (indent, f);
12703 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12704 die->parent->offset);
12705 }
12706
12707 print_spaces (indent, f);
12708 fprintf_unfiltered (f, " has children: %s\n",
12709 dwarf_bool_name (die->child != NULL));
12710
12711 print_spaces (indent, f);
12712 fprintf_unfiltered (f, " attributes:\n");
12713
12714 for (i = 0; i < die->num_attrs; ++i)
12715 {
12716 print_spaces (indent, f);
12717 fprintf_unfiltered (f, " %s (%s) ",
12718 dwarf_attr_name (die->attrs[i].name),
12719 dwarf_form_name (die->attrs[i].form));
12720
12721 switch (die->attrs[i].form)
12722 {
12723 case DW_FORM_ref_addr:
12724 case DW_FORM_addr:
12725 fprintf_unfiltered (f, "address: ");
12726 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12727 break;
12728 case DW_FORM_block2:
12729 case DW_FORM_block4:
12730 case DW_FORM_block:
12731 case DW_FORM_block1:
12732 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12733 break;
12734 case DW_FORM_exprloc:
12735 fprintf_unfiltered (f, "expression: size %u",
12736 DW_BLOCK (&die->attrs[i])->size);
12737 break;
12738 case DW_FORM_ref1:
12739 case DW_FORM_ref2:
12740 case DW_FORM_ref4:
12741 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12742 (long) (DW_ADDR (&die->attrs[i])));
12743 break;
12744 case DW_FORM_data1:
12745 case DW_FORM_data2:
12746 case DW_FORM_data4:
12747 case DW_FORM_data8:
12748 case DW_FORM_udata:
12749 case DW_FORM_sdata:
12750 fprintf_unfiltered (f, "constant: %s",
12751 pulongest (DW_UNSND (&die->attrs[i])));
12752 break;
12753 case DW_FORM_sec_offset:
12754 fprintf_unfiltered (f, "section offset: %s",
12755 pulongest (DW_UNSND (&die->attrs[i])));
12756 break;
12757 case DW_FORM_sig8:
12758 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12759 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12760 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12761 else
12762 fprintf_unfiltered (f, "signatured type, offset: unknown");
12763 break;
12764 case DW_FORM_string:
12765 case DW_FORM_strp:
12766 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12767 DW_STRING (&die->attrs[i])
12768 ? DW_STRING (&die->attrs[i]) : "",
12769 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12770 break;
12771 case DW_FORM_flag:
12772 if (DW_UNSND (&die->attrs[i]))
12773 fprintf_unfiltered (f, "flag: TRUE");
12774 else
12775 fprintf_unfiltered (f, "flag: FALSE");
12776 break;
12777 case DW_FORM_flag_present:
12778 fprintf_unfiltered (f, "flag: TRUE");
12779 break;
12780 case DW_FORM_indirect:
12781 /* the reader will have reduced the indirect form to
12782 the "base form" so this form should not occur */
12783 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12784 break;
12785 default:
12786 fprintf_unfiltered (f, "unsupported attribute form: %d.",
12787 die->attrs[i].form);
12788 break;
12789 }
12790 fprintf_unfiltered (f, "\n");
12791 }
12792 }
12793
12794 static void
12795 dump_die_for_error (struct die_info *die)
12796 {
12797 dump_die_shallow (gdb_stderr, 0, die);
12798 }
12799
12800 static void
12801 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12802 {
12803 int indent = level * 4;
12804
12805 gdb_assert (die != NULL);
12806
12807 if (level >= max_level)
12808 return;
12809
12810 dump_die_shallow (f, indent, die);
12811
12812 if (die->child != NULL)
12813 {
12814 print_spaces (indent, f);
12815 fprintf_unfiltered (f, " Children:");
12816 if (level + 1 < max_level)
12817 {
12818 fprintf_unfiltered (f, "\n");
12819 dump_die_1 (f, level + 1, max_level, die->child);
12820 }
12821 else
12822 {
12823 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12824 }
12825 }
12826
12827 if (die->sibling != NULL && level > 0)
12828 {
12829 dump_die_1 (f, level, max_level, die->sibling);
12830 }
12831 }
12832
12833 /* This is called from the pdie macro in gdbinit.in.
12834 It's not static so gcc will keep a copy callable from gdb. */
12835
12836 void
12837 dump_die (struct die_info *die, int max_level)
12838 {
12839 dump_die_1 (gdb_stdlog, 0, max_level, die);
12840 }
12841
12842 static void
12843 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12844 {
12845 void **slot;
12846
12847 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12848
12849 *slot = die;
12850 }
12851
12852 static int
12853 is_ref_attr (struct attribute *attr)
12854 {
12855 switch (attr->form)
12856 {
12857 case DW_FORM_ref_addr:
12858 case DW_FORM_ref1:
12859 case DW_FORM_ref2:
12860 case DW_FORM_ref4:
12861 case DW_FORM_ref8:
12862 case DW_FORM_ref_udata:
12863 return 1;
12864 default:
12865 return 0;
12866 }
12867 }
12868
12869 static unsigned int
12870 dwarf2_get_ref_die_offset (struct attribute *attr)
12871 {
12872 if (is_ref_attr (attr))
12873 return DW_ADDR (attr);
12874
12875 complaint (&symfile_complaints,
12876 _("unsupported die ref attribute form: '%s'"),
12877 dwarf_form_name (attr->form));
12878 return 0;
12879 }
12880
12881 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12882 * the value held by the attribute is not constant. */
12883
12884 static LONGEST
12885 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12886 {
12887 if (attr->form == DW_FORM_sdata)
12888 return DW_SND (attr);
12889 else if (attr->form == DW_FORM_udata
12890 || attr->form == DW_FORM_data1
12891 || attr->form == DW_FORM_data2
12892 || attr->form == DW_FORM_data4
12893 || attr->form == DW_FORM_data8)
12894 return DW_UNSND (attr);
12895 else
12896 {
12897 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12898 dwarf_form_name (attr->form));
12899 return default_value;
12900 }
12901 }
12902
12903 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
12904 unit and add it to our queue.
12905 The result is non-zero if PER_CU was queued, otherwise the result is zero
12906 meaning either PER_CU is already queued or it is already loaded. */
12907
12908 static int
12909 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12910 struct dwarf2_per_cu_data *per_cu)
12911 {
12912 /* We may arrive here during partial symbol reading, if we need full
12913 DIEs to process an unusual case (e.g. template arguments). Do
12914 not queue PER_CU, just tell our caller to load its DIEs. */
12915 if (dwarf2_per_objfile->reading_partial_symbols)
12916 {
12917 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12918 return 1;
12919 return 0;
12920 }
12921
12922 /* Mark the dependence relation so that we don't flush PER_CU
12923 too early. */
12924 dwarf2_add_dependence (this_cu, per_cu);
12925
12926 /* If it's already on the queue, we have nothing to do. */
12927 if (per_cu->queued)
12928 return 0;
12929
12930 /* If the compilation unit is already loaded, just mark it as
12931 used. */
12932 if (per_cu->cu != NULL)
12933 {
12934 per_cu->cu->last_used = 0;
12935 return 0;
12936 }
12937
12938 /* Add it to the queue. */
12939 queue_comp_unit (per_cu, this_cu->objfile);
12940
12941 return 1;
12942 }
12943
12944 /* Follow reference or signature attribute ATTR of SRC_DIE.
12945 On entry *REF_CU is the CU of SRC_DIE.
12946 On exit *REF_CU is the CU of the result. */
12947
12948 static struct die_info *
12949 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12950 struct dwarf2_cu **ref_cu)
12951 {
12952 struct die_info *die;
12953
12954 if (is_ref_attr (attr))
12955 die = follow_die_ref (src_die, attr, ref_cu);
12956 else if (attr->form == DW_FORM_sig8)
12957 die = follow_die_sig (src_die, attr, ref_cu);
12958 else
12959 {
12960 dump_die_for_error (src_die);
12961 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12962 (*ref_cu)->objfile->name);
12963 }
12964
12965 return die;
12966 }
12967
12968 /* Follow reference OFFSET.
12969 On entry *REF_CU is the CU of the source die referencing OFFSET.
12970 On exit *REF_CU is the CU of the result.
12971 Returns NULL if OFFSET is invalid. */
12972
12973 static struct die_info *
12974 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
12975 {
12976 struct die_info temp_die;
12977 struct dwarf2_cu *target_cu, *cu = *ref_cu;
12978
12979 gdb_assert (cu->per_cu != NULL);
12980
12981 target_cu = cu;
12982
12983 if (cu->per_cu->from_debug_types)
12984 {
12985 /* .debug_types CUs cannot reference anything outside their CU.
12986 If they need to, they have to reference a signatured type via
12987 DW_FORM_sig8. */
12988 if (! offset_in_cu_p (&cu->header, offset))
12989 return NULL;
12990 }
12991 else if (! offset_in_cu_p (&cu->header, offset))
12992 {
12993 struct dwarf2_per_cu_data *per_cu;
12994
12995 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
12996
12997 /* If necessary, add it to the queue and load its DIEs. */
12998 if (maybe_queue_comp_unit (cu, per_cu))
12999 load_full_comp_unit (per_cu, cu->objfile);
13000
13001 target_cu = per_cu->cu;
13002 }
13003 else if (cu->dies == NULL)
13004 {
13005 /* We're loading full DIEs during partial symbol reading. */
13006 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13007 load_full_comp_unit (cu->per_cu, cu->objfile);
13008 }
13009
13010 *ref_cu = target_cu;
13011 temp_die.offset = offset;
13012 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13013 }
13014
13015 /* Follow reference attribute ATTR of SRC_DIE.
13016 On entry *REF_CU is the CU of SRC_DIE.
13017 On exit *REF_CU is the CU of the result. */
13018
13019 static struct die_info *
13020 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13021 struct dwarf2_cu **ref_cu)
13022 {
13023 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13024 struct dwarf2_cu *cu = *ref_cu;
13025 struct die_info *die;
13026
13027 die = follow_die_offset (offset, ref_cu);
13028 if (!die)
13029 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13030 "at 0x%x [in module %s]"),
13031 offset, src_die->offset, cu->objfile->name);
13032
13033 return die;
13034 }
13035
13036 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13037 value is intended for DW_OP_call*. */
13038
13039 struct dwarf2_locexpr_baton
13040 dwarf2_fetch_die_location_block (unsigned int offset,
13041 struct dwarf2_per_cu_data *per_cu)
13042 {
13043 struct dwarf2_cu *cu = per_cu->cu;
13044 struct die_info *die;
13045 struct attribute *attr;
13046 struct dwarf2_locexpr_baton retval;
13047
13048 die = follow_die_offset (offset, &cu);
13049 if (!die)
13050 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13051 offset, per_cu->cu->objfile->name);
13052
13053 attr = dwarf2_attr (die, DW_AT_location, cu);
13054 if (!attr)
13055 {
13056 /* DWARF: "If there is no such attribute, then there is no effect.". */
13057
13058 retval.data = NULL;
13059 retval.size = 0;
13060 }
13061 else
13062 {
13063 if (!attr_form_is_block (attr))
13064 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13065 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13066 offset, per_cu->cu->objfile->name);
13067
13068 retval.data = DW_BLOCK (attr)->data;
13069 retval.size = DW_BLOCK (attr)->size;
13070 }
13071 retval.per_cu = cu->per_cu;
13072 return retval;
13073 }
13074
13075 /* Follow the signature attribute ATTR in SRC_DIE.
13076 On entry *REF_CU is the CU of SRC_DIE.
13077 On exit *REF_CU is the CU of the result. */
13078
13079 static struct die_info *
13080 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13081 struct dwarf2_cu **ref_cu)
13082 {
13083 struct objfile *objfile = (*ref_cu)->objfile;
13084 struct die_info temp_die;
13085 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13086 struct dwarf2_cu *sig_cu;
13087 struct die_info *die;
13088
13089 /* sig_type will be NULL if the signatured type is missing from
13090 the debug info. */
13091 if (sig_type == NULL)
13092 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13093 "at 0x%x [in module %s]"),
13094 src_die->offset, objfile->name);
13095
13096 /* If necessary, add it to the queue and load its DIEs. */
13097
13098 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13099 read_signatured_type (objfile, sig_type);
13100
13101 gdb_assert (sig_type->per_cu.cu != NULL);
13102
13103 sig_cu = sig_type->per_cu.cu;
13104 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13105 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13106 if (die)
13107 {
13108 *ref_cu = sig_cu;
13109 return die;
13110 }
13111
13112 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13113 "at 0x%x [in module %s]"),
13114 sig_type->type_offset, src_die->offset, objfile->name);
13115 }
13116
13117 /* Given an offset of a signatured type, return its signatured_type. */
13118
13119 static struct signatured_type *
13120 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13121 {
13122 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13123 unsigned int length, initial_length_size;
13124 unsigned int sig_offset;
13125 struct signatured_type find_entry, *type_sig;
13126
13127 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13128 sig_offset = (initial_length_size
13129 + 2 /*version*/
13130 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13131 + 1 /*address_size*/);
13132 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13133 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13134
13135 /* This is only used to lookup previously recorded types.
13136 If we didn't find it, it's our bug. */
13137 gdb_assert (type_sig != NULL);
13138 gdb_assert (offset == type_sig->offset);
13139
13140 return type_sig;
13141 }
13142
13143 /* Read in signatured type at OFFSET and build its CU and die(s). */
13144
13145 static void
13146 read_signatured_type_at_offset (struct objfile *objfile,
13147 unsigned int offset)
13148 {
13149 struct signatured_type *type_sig;
13150
13151 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13152
13153 /* We have the section offset, but we need the signature to do the
13154 hash table lookup. */
13155 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13156
13157 gdb_assert (type_sig->per_cu.cu == NULL);
13158
13159 read_signatured_type (objfile, type_sig);
13160
13161 gdb_assert (type_sig->per_cu.cu != NULL);
13162 }
13163
13164 /* Read in a signatured type and build its CU and DIEs. */
13165
13166 static void
13167 read_signatured_type (struct objfile *objfile,
13168 struct signatured_type *type_sig)
13169 {
13170 gdb_byte *types_ptr;
13171 struct die_reader_specs reader_specs;
13172 struct dwarf2_cu *cu;
13173 ULONGEST signature;
13174 struct cleanup *back_to, *free_cu_cleanup;
13175 struct attribute *attr;
13176
13177 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13178 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13179
13180 gdb_assert (type_sig->per_cu.cu == NULL);
13181
13182 cu = xmalloc (sizeof (struct dwarf2_cu));
13183 memset (cu, 0, sizeof (struct dwarf2_cu));
13184 obstack_init (&cu->comp_unit_obstack);
13185 cu->objfile = objfile;
13186 type_sig->per_cu.cu = cu;
13187 cu->per_cu = &type_sig->per_cu;
13188
13189 /* If an error occurs while loading, release our storage. */
13190 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13191
13192 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13193 types_ptr, objfile->obfd);
13194 gdb_assert (signature == type_sig->signature);
13195
13196 cu->die_hash
13197 = htab_create_alloc_ex (cu->header.length / 12,
13198 die_hash,
13199 die_eq,
13200 NULL,
13201 &cu->comp_unit_obstack,
13202 hashtab_obstack_allocate,
13203 dummy_obstack_deallocate);
13204
13205 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13206 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13207
13208 init_cu_die_reader (&reader_specs, cu);
13209
13210 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13211 NULL /*parent*/);
13212
13213 /* We try not to read any attributes in this function, because not
13214 all objfiles needed for references have been loaded yet, and symbol
13215 table processing isn't initialized. But we have to set the CU language,
13216 or we won't be able to build types correctly. */
13217 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13218 if (attr)
13219 set_cu_language (DW_UNSND (attr), cu);
13220 else
13221 set_cu_language (language_minimal, cu);
13222
13223 do_cleanups (back_to);
13224
13225 /* We've successfully allocated this compilation unit. Let our caller
13226 clean it up when finished with it. */
13227 discard_cleanups (free_cu_cleanup);
13228
13229 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13230 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13231 }
13232
13233 /* Decode simple location descriptions.
13234 Given a pointer to a dwarf block that defines a location, compute
13235 the location and return the value.
13236
13237 NOTE drow/2003-11-18: This function is called in two situations
13238 now: for the address of static or global variables (partial symbols
13239 only) and for offsets into structures which are expected to be
13240 (more or less) constant. The partial symbol case should go away,
13241 and only the constant case should remain. That will let this
13242 function complain more accurately. A few special modes are allowed
13243 without complaint for global variables (for instance, global
13244 register values and thread-local values).
13245
13246 A location description containing no operations indicates that the
13247 object is optimized out. The return value is 0 for that case.
13248 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13249 callers will only want a very basic result and this can become a
13250 complaint.
13251
13252 Note that stack[0] is unused except as a default error return.
13253 Note that stack overflow is not yet handled. */
13254
13255 static CORE_ADDR
13256 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13257 {
13258 struct objfile *objfile = cu->objfile;
13259 int i;
13260 int size = blk->size;
13261 gdb_byte *data = blk->data;
13262 CORE_ADDR stack[64];
13263 int stacki;
13264 unsigned int bytes_read, unsnd;
13265 gdb_byte op;
13266
13267 i = 0;
13268 stacki = 0;
13269 stack[stacki] = 0;
13270
13271 while (i < size)
13272 {
13273 op = data[i++];
13274 switch (op)
13275 {
13276 case DW_OP_lit0:
13277 case DW_OP_lit1:
13278 case DW_OP_lit2:
13279 case DW_OP_lit3:
13280 case DW_OP_lit4:
13281 case DW_OP_lit5:
13282 case DW_OP_lit6:
13283 case DW_OP_lit7:
13284 case DW_OP_lit8:
13285 case DW_OP_lit9:
13286 case DW_OP_lit10:
13287 case DW_OP_lit11:
13288 case DW_OP_lit12:
13289 case DW_OP_lit13:
13290 case DW_OP_lit14:
13291 case DW_OP_lit15:
13292 case DW_OP_lit16:
13293 case DW_OP_lit17:
13294 case DW_OP_lit18:
13295 case DW_OP_lit19:
13296 case DW_OP_lit20:
13297 case DW_OP_lit21:
13298 case DW_OP_lit22:
13299 case DW_OP_lit23:
13300 case DW_OP_lit24:
13301 case DW_OP_lit25:
13302 case DW_OP_lit26:
13303 case DW_OP_lit27:
13304 case DW_OP_lit28:
13305 case DW_OP_lit29:
13306 case DW_OP_lit30:
13307 case DW_OP_lit31:
13308 stack[++stacki] = op - DW_OP_lit0;
13309 break;
13310
13311 case DW_OP_reg0:
13312 case DW_OP_reg1:
13313 case DW_OP_reg2:
13314 case DW_OP_reg3:
13315 case DW_OP_reg4:
13316 case DW_OP_reg5:
13317 case DW_OP_reg6:
13318 case DW_OP_reg7:
13319 case DW_OP_reg8:
13320 case DW_OP_reg9:
13321 case DW_OP_reg10:
13322 case DW_OP_reg11:
13323 case DW_OP_reg12:
13324 case DW_OP_reg13:
13325 case DW_OP_reg14:
13326 case DW_OP_reg15:
13327 case DW_OP_reg16:
13328 case DW_OP_reg17:
13329 case DW_OP_reg18:
13330 case DW_OP_reg19:
13331 case DW_OP_reg20:
13332 case DW_OP_reg21:
13333 case DW_OP_reg22:
13334 case DW_OP_reg23:
13335 case DW_OP_reg24:
13336 case DW_OP_reg25:
13337 case DW_OP_reg26:
13338 case DW_OP_reg27:
13339 case DW_OP_reg28:
13340 case DW_OP_reg29:
13341 case DW_OP_reg30:
13342 case DW_OP_reg31:
13343 stack[++stacki] = op - DW_OP_reg0;
13344 if (i < size)
13345 dwarf2_complex_location_expr_complaint ();
13346 break;
13347
13348 case DW_OP_regx:
13349 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13350 i += bytes_read;
13351 stack[++stacki] = unsnd;
13352 if (i < size)
13353 dwarf2_complex_location_expr_complaint ();
13354 break;
13355
13356 case DW_OP_addr:
13357 stack[++stacki] = read_address (objfile->obfd, &data[i],
13358 cu, &bytes_read);
13359 i += bytes_read;
13360 break;
13361
13362 case DW_OP_const1u:
13363 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13364 i += 1;
13365 break;
13366
13367 case DW_OP_const1s:
13368 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13369 i += 1;
13370 break;
13371
13372 case DW_OP_const2u:
13373 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13374 i += 2;
13375 break;
13376
13377 case DW_OP_const2s:
13378 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13379 i += 2;
13380 break;
13381
13382 case DW_OP_const4u:
13383 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13384 i += 4;
13385 break;
13386
13387 case DW_OP_const4s:
13388 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13389 i += 4;
13390 break;
13391
13392 case DW_OP_constu:
13393 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13394 &bytes_read);
13395 i += bytes_read;
13396 break;
13397
13398 case DW_OP_consts:
13399 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13400 i += bytes_read;
13401 break;
13402
13403 case DW_OP_dup:
13404 stack[stacki + 1] = stack[stacki];
13405 stacki++;
13406 break;
13407
13408 case DW_OP_plus:
13409 stack[stacki - 1] += stack[stacki];
13410 stacki--;
13411 break;
13412
13413 case DW_OP_plus_uconst:
13414 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13415 i += bytes_read;
13416 break;
13417
13418 case DW_OP_minus:
13419 stack[stacki - 1] -= stack[stacki];
13420 stacki--;
13421 break;
13422
13423 case DW_OP_deref:
13424 /* If we're not the last op, then we definitely can't encode
13425 this using GDB's address_class enum. This is valid for partial
13426 global symbols, although the variable's address will be bogus
13427 in the psymtab. */
13428 if (i < size)
13429 dwarf2_complex_location_expr_complaint ();
13430 break;
13431
13432 case DW_OP_GNU_push_tls_address:
13433 /* The top of the stack has the offset from the beginning
13434 of the thread control block at which the variable is located. */
13435 /* Nothing should follow this operator, so the top of stack would
13436 be returned. */
13437 /* This is valid for partial global symbols, but the variable's
13438 address will be bogus in the psymtab. */
13439 if (i < size)
13440 dwarf2_complex_location_expr_complaint ();
13441 break;
13442
13443 case DW_OP_GNU_uninit:
13444 break;
13445
13446 default:
13447 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13448 dwarf_stack_op_name (op, 1));
13449 return (stack[stacki]);
13450 }
13451 }
13452 return (stack[stacki]);
13453 }
13454
13455 /* memory allocation interface */
13456
13457 static struct dwarf_block *
13458 dwarf_alloc_block (struct dwarf2_cu *cu)
13459 {
13460 struct dwarf_block *blk;
13461
13462 blk = (struct dwarf_block *)
13463 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13464 return (blk);
13465 }
13466
13467 static struct abbrev_info *
13468 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13469 {
13470 struct abbrev_info *abbrev;
13471
13472 abbrev = (struct abbrev_info *)
13473 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13474 memset (abbrev, 0, sizeof (struct abbrev_info));
13475 return (abbrev);
13476 }
13477
13478 static struct die_info *
13479 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13480 {
13481 struct die_info *die;
13482 size_t size = sizeof (struct die_info);
13483
13484 if (num_attrs > 1)
13485 size += (num_attrs - 1) * sizeof (struct attribute);
13486
13487 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13488 memset (die, 0, sizeof (struct die_info));
13489 return (die);
13490 }
13491
13492 \f
13493 /* Macro support. */
13494
13495
13496 /* Return the full name of file number I in *LH's file name table.
13497 Use COMP_DIR as the name of the current directory of the
13498 compilation. The result is allocated using xmalloc; the caller is
13499 responsible for freeing it. */
13500 static char *
13501 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13502 {
13503 /* Is the file number a valid index into the line header's file name
13504 table? Remember that file numbers start with one, not zero. */
13505 if (1 <= file && file <= lh->num_file_names)
13506 {
13507 struct file_entry *fe = &lh->file_names[file - 1];
13508
13509 if (IS_ABSOLUTE_PATH (fe->name))
13510 return xstrdup (fe->name);
13511 else
13512 {
13513 const char *dir;
13514 int dir_len;
13515 char *full_name;
13516
13517 if (fe->dir_index)
13518 dir = lh->include_dirs[fe->dir_index - 1];
13519 else
13520 dir = comp_dir;
13521
13522 if (dir)
13523 {
13524 dir_len = strlen (dir);
13525 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13526 strcpy (full_name, dir);
13527 full_name[dir_len] = '/';
13528 strcpy (full_name + dir_len + 1, fe->name);
13529 return full_name;
13530 }
13531 else
13532 return xstrdup (fe->name);
13533 }
13534 }
13535 else
13536 {
13537 /* The compiler produced a bogus file number. We can at least
13538 record the macro definitions made in the file, even if we
13539 won't be able to find the file by name. */
13540 char fake_name[80];
13541
13542 sprintf (fake_name, "<bad macro file number %d>", file);
13543
13544 complaint (&symfile_complaints,
13545 _("bad file number in macro information (%d)"),
13546 file);
13547
13548 return xstrdup (fake_name);
13549 }
13550 }
13551
13552
13553 static struct macro_source_file *
13554 macro_start_file (int file, int line,
13555 struct macro_source_file *current_file,
13556 const char *comp_dir,
13557 struct line_header *lh, struct objfile *objfile)
13558 {
13559 /* The full name of this source file. */
13560 char *full_name = file_full_name (file, lh, comp_dir);
13561
13562 /* We don't create a macro table for this compilation unit
13563 at all until we actually get a filename. */
13564 if (! pending_macros)
13565 pending_macros = new_macro_table (&objfile->objfile_obstack,
13566 objfile->macro_cache);
13567
13568 if (! current_file)
13569 /* If we have no current file, then this must be the start_file
13570 directive for the compilation unit's main source file. */
13571 current_file = macro_set_main (pending_macros, full_name);
13572 else
13573 current_file = macro_include (current_file, line, full_name);
13574
13575 xfree (full_name);
13576
13577 return current_file;
13578 }
13579
13580
13581 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13582 followed by a null byte. */
13583 static char *
13584 copy_string (const char *buf, int len)
13585 {
13586 char *s = xmalloc (len + 1);
13587
13588 memcpy (s, buf, len);
13589 s[len] = '\0';
13590 return s;
13591 }
13592
13593
13594 static const char *
13595 consume_improper_spaces (const char *p, const char *body)
13596 {
13597 if (*p == ' ')
13598 {
13599 complaint (&symfile_complaints,
13600 _("macro definition contains spaces in formal argument list:\n`%s'"),
13601 body);
13602
13603 while (*p == ' ')
13604 p++;
13605 }
13606
13607 return p;
13608 }
13609
13610
13611 static void
13612 parse_macro_definition (struct macro_source_file *file, int line,
13613 const char *body)
13614 {
13615 const char *p;
13616
13617 /* The body string takes one of two forms. For object-like macro
13618 definitions, it should be:
13619
13620 <macro name> " " <definition>
13621
13622 For function-like macro definitions, it should be:
13623
13624 <macro name> "() " <definition>
13625 or
13626 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13627
13628 Spaces may appear only where explicitly indicated, and in the
13629 <definition>.
13630
13631 The Dwarf 2 spec says that an object-like macro's name is always
13632 followed by a space, but versions of GCC around March 2002 omit
13633 the space when the macro's definition is the empty string.
13634
13635 The Dwarf 2 spec says that there should be no spaces between the
13636 formal arguments in a function-like macro's formal argument list,
13637 but versions of GCC around March 2002 include spaces after the
13638 commas. */
13639
13640
13641 /* Find the extent of the macro name. The macro name is terminated
13642 by either a space or null character (for an object-like macro) or
13643 an opening paren (for a function-like macro). */
13644 for (p = body; *p; p++)
13645 if (*p == ' ' || *p == '(')
13646 break;
13647
13648 if (*p == ' ' || *p == '\0')
13649 {
13650 /* It's an object-like macro. */
13651 int name_len = p - body;
13652 char *name = copy_string (body, name_len);
13653 const char *replacement;
13654
13655 if (*p == ' ')
13656 replacement = body + name_len + 1;
13657 else
13658 {
13659 dwarf2_macro_malformed_definition_complaint (body);
13660 replacement = body + name_len;
13661 }
13662
13663 macro_define_object (file, line, name, replacement);
13664
13665 xfree (name);
13666 }
13667 else if (*p == '(')
13668 {
13669 /* It's a function-like macro. */
13670 char *name = copy_string (body, p - body);
13671 int argc = 0;
13672 int argv_size = 1;
13673 char **argv = xmalloc (argv_size * sizeof (*argv));
13674
13675 p++;
13676
13677 p = consume_improper_spaces (p, body);
13678
13679 /* Parse the formal argument list. */
13680 while (*p && *p != ')')
13681 {
13682 /* Find the extent of the current argument name. */
13683 const char *arg_start = p;
13684
13685 while (*p && *p != ',' && *p != ')' && *p != ' ')
13686 p++;
13687
13688 if (! *p || p == arg_start)
13689 dwarf2_macro_malformed_definition_complaint (body);
13690 else
13691 {
13692 /* Make sure argv has room for the new argument. */
13693 if (argc >= argv_size)
13694 {
13695 argv_size *= 2;
13696 argv = xrealloc (argv, argv_size * sizeof (*argv));
13697 }
13698
13699 argv[argc++] = copy_string (arg_start, p - arg_start);
13700 }
13701
13702 p = consume_improper_spaces (p, body);
13703
13704 /* Consume the comma, if present. */
13705 if (*p == ',')
13706 {
13707 p++;
13708
13709 p = consume_improper_spaces (p, body);
13710 }
13711 }
13712
13713 if (*p == ')')
13714 {
13715 p++;
13716
13717 if (*p == ' ')
13718 /* Perfectly formed definition, no complaints. */
13719 macro_define_function (file, line, name,
13720 argc, (const char **) argv,
13721 p + 1);
13722 else if (*p == '\0')
13723 {
13724 /* Complain, but do define it. */
13725 dwarf2_macro_malformed_definition_complaint (body);
13726 macro_define_function (file, line, name,
13727 argc, (const char **) argv,
13728 p);
13729 }
13730 else
13731 /* Just complain. */
13732 dwarf2_macro_malformed_definition_complaint (body);
13733 }
13734 else
13735 /* Just complain. */
13736 dwarf2_macro_malformed_definition_complaint (body);
13737
13738 xfree (name);
13739 {
13740 int i;
13741
13742 for (i = 0; i < argc; i++)
13743 xfree (argv[i]);
13744 }
13745 xfree (argv);
13746 }
13747 else
13748 dwarf2_macro_malformed_definition_complaint (body);
13749 }
13750
13751
13752 static void
13753 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13754 char *comp_dir, bfd *abfd,
13755 struct dwarf2_cu *cu)
13756 {
13757 gdb_byte *mac_ptr, *mac_end;
13758 struct macro_source_file *current_file = 0;
13759 enum dwarf_macinfo_record_type macinfo_type;
13760 int at_commandline;
13761
13762 dwarf2_read_section (dwarf2_per_objfile->objfile,
13763 &dwarf2_per_objfile->macinfo);
13764 if (dwarf2_per_objfile->macinfo.buffer == NULL)
13765 {
13766 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13767 return;
13768 }
13769
13770 /* First pass: Find the name of the base filename.
13771 This filename is needed in order to process all macros whose definition
13772 (or undefinition) comes from the command line. These macros are defined
13773 before the first DW_MACINFO_start_file entry, and yet still need to be
13774 associated to the base file.
13775
13776 To determine the base file name, we scan the macro definitions until we
13777 reach the first DW_MACINFO_start_file entry. We then initialize
13778 CURRENT_FILE accordingly so that any macro definition found before the
13779 first DW_MACINFO_start_file can still be associated to the base file. */
13780
13781 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13782 mac_end = dwarf2_per_objfile->macinfo.buffer
13783 + dwarf2_per_objfile->macinfo.size;
13784
13785 do
13786 {
13787 /* Do we at least have room for a macinfo type byte? */
13788 if (mac_ptr >= mac_end)
13789 {
13790 /* Complaint is printed during the second pass as GDB will probably
13791 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13792 break;
13793 }
13794
13795 macinfo_type = read_1_byte (abfd, mac_ptr);
13796 mac_ptr++;
13797
13798 switch (macinfo_type)
13799 {
13800 /* A zero macinfo type indicates the end of the macro
13801 information. */
13802 case 0:
13803 break;
13804
13805 case DW_MACINFO_define:
13806 case DW_MACINFO_undef:
13807 /* Only skip the data by MAC_PTR. */
13808 {
13809 unsigned int bytes_read;
13810
13811 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13812 mac_ptr += bytes_read;
13813 read_direct_string (abfd, mac_ptr, &bytes_read);
13814 mac_ptr += bytes_read;
13815 }
13816 break;
13817
13818 case DW_MACINFO_start_file:
13819 {
13820 unsigned int bytes_read;
13821 int line, file;
13822
13823 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13824 mac_ptr += bytes_read;
13825 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13826 mac_ptr += bytes_read;
13827
13828 current_file = macro_start_file (file, line, current_file, comp_dir,
13829 lh, cu->objfile);
13830 }
13831 break;
13832
13833 case DW_MACINFO_end_file:
13834 /* No data to skip by MAC_PTR. */
13835 break;
13836
13837 case DW_MACINFO_vendor_ext:
13838 /* Only skip the data by MAC_PTR. */
13839 {
13840 unsigned int bytes_read;
13841
13842 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13843 mac_ptr += bytes_read;
13844 read_direct_string (abfd, mac_ptr, &bytes_read);
13845 mac_ptr += bytes_read;
13846 }
13847 break;
13848
13849 default:
13850 break;
13851 }
13852 } while (macinfo_type != 0 && current_file == NULL);
13853
13854 /* Second pass: Process all entries.
13855
13856 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13857 command-line macro definitions/undefinitions. This flag is unset when we
13858 reach the first DW_MACINFO_start_file entry. */
13859
13860 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13861
13862 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13863 GDB is still reading the definitions from command line. First
13864 DW_MACINFO_start_file will need to be ignored as it was already executed
13865 to create CURRENT_FILE for the main source holding also the command line
13866 definitions. On first met DW_MACINFO_start_file this flag is reset to
13867 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13868
13869 at_commandline = 1;
13870
13871 do
13872 {
13873 /* Do we at least have room for a macinfo type byte? */
13874 if (mac_ptr >= mac_end)
13875 {
13876 dwarf2_macros_too_long_complaint ();
13877 break;
13878 }
13879
13880 macinfo_type = read_1_byte (abfd, mac_ptr);
13881 mac_ptr++;
13882
13883 switch (macinfo_type)
13884 {
13885 /* A zero macinfo type indicates the end of the macro
13886 information. */
13887 case 0:
13888 break;
13889
13890 case DW_MACINFO_define:
13891 case DW_MACINFO_undef:
13892 {
13893 unsigned int bytes_read;
13894 int line;
13895 char *body;
13896
13897 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13898 mac_ptr += bytes_read;
13899 body = read_direct_string (abfd, mac_ptr, &bytes_read);
13900 mac_ptr += bytes_read;
13901
13902 if (! current_file)
13903 {
13904 /* DWARF violation as no main source is present. */
13905 complaint (&symfile_complaints,
13906 _("debug info with no main source gives macro %s "
13907 "on line %d: %s"),
13908 macinfo_type == DW_MACINFO_define ?
13909 _("definition") :
13910 macinfo_type == DW_MACINFO_undef ?
13911 _("undefinition") :
13912 _("something-or-other"), line, body);
13913 break;
13914 }
13915 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13916 complaint (&symfile_complaints,
13917 _("debug info gives %s macro %s with %s line %d: %s"),
13918 at_commandline ? _("command-line") : _("in-file"),
13919 macinfo_type == DW_MACINFO_define ?
13920 _("definition") :
13921 macinfo_type == DW_MACINFO_undef ?
13922 _("undefinition") :
13923 _("something-or-other"),
13924 line == 0 ? _("zero") : _("non-zero"), line, body);
13925
13926 if (macinfo_type == DW_MACINFO_define)
13927 parse_macro_definition (current_file, line, body);
13928 else if (macinfo_type == DW_MACINFO_undef)
13929 macro_undef (current_file, line, body);
13930 }
13931 break;
13932
13933 case DW_MACINFO_start_file:
13934 {
13935 unsigned int bytes_read;
13936 int line, file;
13937
13938 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13939 mac_ptr += bytes_read;
13940 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13941 mac_ptr += bytes_read;
13942
13943 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13944 complaint (&symfile_complaints,
13945 _("debug info gives source %d included "
13946 "from %s at %s line %d"),
13947 file, at_commandline ? _("command-line") : _("file"),
13948 line == 0 ? _("zero") : _("non-zero"), line);
13949
13950 if (at_commandline)
13951 {
13952 /* This DW_MACINFO_start_file was executed in the pass one. */
13953 at_commandline = 0;
13954 }
13955 else
13956 current_file = macro_start_file (file, line,
13957 current_file, comp_dir,
13958 lh, cu->objfile);
13959 }
13960 break;
13961
13962 case DW_MACINFO_end_file:
13963 if (! current_file)
13964 complaint (&symfile_complaints,
13965 _("macro debug info has an unmatched `close_file' directive"));
13966 else
13967 {
13968 current_file = current_file->included_by;
13969 if (! current_file)
13970 {
13971 enum dwarf_macinfo_record_type next_type;
13972
13973 /* GCC circa March 2002 doesn't produce the zero
13974 type byte marking the end of the compilation
13975 unit. Complain if it's not there, but exit no
13976 matter what. */
13977
13978 /* Do we at least have room for a macinfo type byte? */
13979 if (mac_ptr >= mac_end)
13980 {
13981 dwarf2_macros_too_long_complaint ();
13982 return;
13983 }
13984
13985 /* We don't increment mac_ptr here, so this is just
13986 a look-ahead. */
13987 next_type = read_1_byte (abfd, mac_ptr);
13988 if (next_type != 0)
13989 complaint (&symfile_complaints,
13990 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
13991
13992 return;
13993 }
13994 }
13995 break;
13996
13997 case DW_MACINFO_vendor_ext:
13998 {
13999 unsigned int bytes_read;
14000 int constant;
14001 char *string;
14002
14003 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14004 mac_ptr += bytes_read;
14005 string = read_direct_string (abfd, mac_ptr, &bytes_read);
14006 mac_ptr += bytes_read;
14007
14008 /* We don't recognize any vendor extensions. */
14009 }
14010 break;
14011 }
14012 } while (macinfo_type != 0);
14013 }
14014
14015 /* Check if the attribute's form is a DW_FORM_block*
14016 if so return true else false. */
14017 static int
14018 attr_form_is_block (struct attribute *attr)
14019 {
14020 return (attr == NULL ? 0 :
14021 attr->form == DW_FORM_block1
14022 || attr->form == DW_FORM_block2
14023 || attr->form == DW_FORM_block4
14024 || attr->form == DW_FORM_block
14025 || attr->form == DW_FORM_exprloc);
14026 }
14027
14028 /* Return non-zero if ATTR's value is a section offset --- classes
14029 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14030 You may use DW_UNSND (attr) to retrieve such offsets.
14031
14032 Section 7.5.4, "Attribute Encodings", explains that no attribute
14033 may have a value that belongs to more than one of these classes; it
14034 would be ambiguous if we did, because we use the same forms for all
14035 of them. */
14036 static int
14037 attr_form_is_section_offset (struct attribute *attr)
14038 {
14039 return (attr->form == DW_FORM_data4
14040 || attr->form == DW_FORM_data8
14041 || attr->form == DW_FORM_sec_offset);
14042 }
14043
14044
14045 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14046 zero otherwise. When this function returns true, you can apply
14047 dwarf2_get_attr_constant_value to it.
14048
14049 However, note that for some attributes you must check
14050 attr_form_is_section_offset before using this test. DW_FORM_data4
14051 and DW_FORM_data8 are members of both the constant class, and of
14052 the classes that contain offsets into other debug sections
14053 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14054 that, if an attribute's can be either a constant or one of the
14055 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14056 taken as section offsets, not constants. */
14057 static int
14058 attr_form_is_constant (struct attribute *attr)
14059 {
14060 switch (attr->form)
14061 {
14062 case DW_FORM_sdata:
14063 case DW_FORM_udata:
14064 case DW_FORM_data1:
14065 case DW_FORM_data2:
14066 case DW_FORM_data4:
14067 case DW_FORM_data8:
14068 return 1;
14069 default:
14070 return 0;
14071 }
14072 }
14073
14074 static void
14075 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14076 struct dwarf2_cu *cu)
14077 {
14078 if (attr_form_is_section_offset (attr)
14079 /* ".debug_loc" may not exist at all, or the offset may be outside
14080 the section. If so, fall through to the complaint in the
14081 other branch. */
14082 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
14083 {
14084 struct dwarf2_loclist_baton *baton;
14085
14086 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14087 sizeof (struct dwarf2_loclist_baton));
14088 baton->per_cu = cu->per_cu;
14089 gdb_assert (baton->per_cu);
14090
14091 dwarf2_read_section (dwarf2_per_objfile->objfile,
14092 &dwarf2_per_objfile->loc);
14093
14094 /* We don't know how long the location list is, but make sure we
14095 don't run off the edge of the section. */
14096 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14097 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14098 baton->base_address = cu->base_address;
14099 if (cu->base_known == 0)
14100 complaint (&symfile_complaints,
14101 _("Location list used without specifying the CU base address."));
14102
14103 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14104 SYMBOL_LOCATION_BATON (sym) = baton;
14105 }
14106 else
14107 {
14108 struct dwarf2_locexpr_baton *baton;
14109
14110 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14111 sizeof (struct dwarf2_locexpr_baton));
14112 baton->per_cu = cu->per_cu;
14113 gdb_assert (baton->per_cu);
14114
14115 if (attr_form_is_block (attr))
14116 {
14117 /* Note that we're just copying the block's data pointer
14118 here, not the actual data. We're still pointing into the
14119 info_buffer for SYM's objfile; right now we never release
14120 that buffer, but when we do clean up properly this may
14121 need to change. */
14122 baton->size = DW_BLOCK (attr)->size;
14123 baton->data = DW_BLOCK (attr)->data;
14124 }
14125 else
14126 {
14127 dwarf2_invalid_attrib_class_complaint ("location description",
14128 SYMBOL_NATURAL_NAME (sym));
14129 baton->size = 0;
14130 baton->data = NULL;
14131 }
14132
14133 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14134 SYMBOL_LOCATION_BATON (sym) = baton;
14135 }
14136 }
14137
14138 /* Return the OBJFILE associated with the compilation unit CU. If CU
14139 came from a separate debuginfo file, then the master objfile is
14140 returned. */
14141
14142 struct objfile *
14143 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14144 {
14145 struct objfile *objfile = per_cu->objfile;
14146
14147 /* Return the master objfile, so that we can report and look up the
14148 correct file containing this variable. */
14149 if (objfile->separate_debug_objfile_backlink)
14150 objfile = objfile->separate_debug_objfile_backlink;
14151
14152 return objfile;
14153 }
14154
14155 /* Return the address size given in the compilation unit header for CU. */
14156
14157 CORE_ADDR
14158 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14159 {
14160 if (per_cu->cu)
14161 return per_cu->cu->header.addr_size;
14162 else
14163 {
14164 /* If the CU is not currently read in, we re-read its header. */
14165 struct objfile *objfile = per_cu->objfile;
14166 struct dwarf2_per_objfile *per_objfile
14167 = objfile_data (objfile, dwarf2_objfile_data_key);
14168 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14169 struct comp_unit_head cu_header;
14170
14171 memset (&cu_header, 0, sizeof cu_header);
14172 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14173 return cu_header.addr_size;
14174 }
14175 }
14176
14177 /* Return the offset size given in the compilation unit header for CU. */
14178
14179 int
14180 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14181 {
14182 if (per_cu->cu)
14183 return per_cu->cu->header.offset_size;
14184 else
14185 {
14186 /* If the CU is not currently read in, we re-read its header. */
14187 struct objfile *objfile = per_cu->objfile;
14188 struct dwarf2_per_objfile *per_objfile
14189 = objfile_data (objfile, dwarf2_objfile_data_key);
14190 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14191 struct comp_unit_head cu_header;
14192
14193 memset (&cu_header, 0, sizeof cu_header);
14194 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14195 return cu_header.offset_size;
14196 }
14197 }
14198
14199 /* Return the text offset of the CU. The returned offset comes from
14200 this CU's objfile. If this objfile came from a separate debuginfo
14201 file, then the offset may be different from the corresponding
14202 offset in the parent objfile. */
14203
14204 CORE_ADDR
14205 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14206 {
14207 struct objfile *objfile = per_cu->objfile;
14208
14209 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14210 }
14211
14212 /* Locate the .debug_info compilation unit from CU's objfile which contains
14213 the DIE at OFFSET. Raises an error on failure. */
14214
14215 static struct dwarf2_per_cu_data *
14216 dwarf2_find_containing_comp_unit (unsigned int offset,
14217 struct objfile *objfile)
14218 {
14219 struct dwarf2_per_cu_data *this_cu;
14220 int low, high;
14221
14222 low = 0;
14223 high = dwarf2_per_objfile->n_comp_units - 1;
14224 while (high > low)
14225 {
14226 int mid = low + (high - low) / 2;
14227
14228 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14229 high = mid;
14230 else
14231 low = mid + 1;
14232 }
14233 gdb_assert (low == high);
14234 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14235 {
14236 if (low == 0)
14237 error (_("Dwarf Error: could not find partial DIE containing "
14238 "offset 0x%lx [in module %s]"),
14239 (long) offset, bfd_get_filename (objfile->obfd));
14240
14241 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14242 return dwarf2_per_objfile->all_comp_units[low-1];
14243 }
14244 else
14245 {
14246 this_cu = dwarf2_per_objfile->all_comp_units[low];
14247 if (low == dwarf2_per_objfile->n_comp_units - 1
14248 && offset >= this_cu->offset + this_cu->length)
14249 error (_("invalid dwarf2 offset %u"), offset);
14250 gdb_assert (offset < this_cu->offset + this_cu->length);
14251 return this_cu;
14252 }
14253 }
14254
14255 /* Locate the compilation unit from OBJFILE which is located at exactly
14256 OFFSET. Raises an error on failure. */
14257
14258 static struct dwarf2_per_cu_data *
14259 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14260 {
14261 struct dwarf2_per_cu_data *this_cu;
14262
14263 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14264 if (this_cu->offset != offset)
14265 error (_("no compilation unit with offset %u."), offset);
14266 return this_cu;
14267 }
14268
14269 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
14270
14271 static struct dwarf2_cu *
14272 alloc_one_comp_unit (struct objfile *objfile)
14273 {
14274 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14275 cu->objfile = objfile;
14276 obstack_init (&cu->comp_unit_obstack);
14277 return cu;
14278 }
14279
14280 /* Release one cached compilation unit, CU. We unlink it from the tree
14281 of compilation units, but we don't remove it from the read_in_chain;
14282 the caller is responsible for that.
14283 NOTE: DATA is a void * because this function is also used as a
14284 cleanup routine. */
14285
14286 static void
14287 free_one_comp_unit (void *data)
14288 {
14289 struct dwarf2_cu *cu = data;
14290
14291 if (cu->per_cu != NULL)
14292 cu->per_cu->cu = NULL;
14293 cu->per_cu = NULL;
14294
14295 obstack_free (&cu->comp_unit_obstack, NULL);
14296
14297 xfree (cu);
14298 }
14299
14300 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14301 when we're finished with it. We can't free the pointer itself, but be
14302 sure to unlink it from the cache. Also release any associated storage
14303 and perform cache maintenance.
14304
14305 Only used during partial symbol parsing. */
14306
14307 static void
14308 free_stack_comp_unit (void *data)
14309 {
14310 struct dwarf2_cu *cu = data;
14311
14312 obstack_free (&cu->comp_unit_obstack, NULL);
14313 cu->partial_dies = NULL;
14314
14315 if (cu->per_cu != NULL)
14316 {
14317 /* This compilation unit is on the stack in our caller, so we
14318 should not xfree it. Just unlink it. */
14319 cu->per_cu->cu = NULL;
14320 cu->per_cu = NULL;
14321
14322 /* If we had a per-cu pointer, then we may have other compilation
14323 units loaded, so age them now. */
14324 age_cached_comp_units ();
14325 }
14326 }
14327
14328 /* Free all cached compilation units. */
14329
14330 static void
14331 free_cached_comp_units (void *data)
14332 {
14333 struct dwarf2_per_cu_data *per_cu, **last_chain;
14334
14335 per_cu = dwarf2_per_objfile->read_in_chain;
14336 last_chain = &dwarf2_per_objfile->read_in_chain;
14337 while (per_cu != NULL)
14338 {
14339 struct dwarf2_per_cu_data *next_cu;
14340
14341 next_cu = per_cu->cu->read_in_chain;
14342
14343 free_one_comp_unit (per_cu->cu);
14344 *last_chain = next_cu;
14345
14346 per_cu = next_cu;
14347 }
14348 }
14349
14350 /* Increase the age counter on each cached compilation unit, and free
14351 any that are too old. */
14352
14353 static void
14354 age_cached_comp_units (void)
14355 {
14356 struct dwarf2_per_cu_data *per_cu, **last_chain;
14357
14358 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14359 per_cu = dwarf2_per_objfile->read_in_chain;
14360 while (per_cu != NULL)
14361 {
14362 per_cu->cu->last_used ++;
14363 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14364 dwarf2_mark (per_cu->cu);
14365 per_cu = per_cu->cu->read_in_chain;
14366 }
14367
14368 per_cu = dwarf2_per_objfile->read_in_chain;
14369 last_chain = &dwarf2_per_objfile->read_in_chain;
14370 while (per_cu != NULL)
14371 {
14372 struct dwarf2_per_cu_data *next_cu;
14373
14374 next_cu = per_cu->cu->read_in_chain;
14375
14376 if (!per_cu->cu->mark)
14377 {
14378 free_one_comp_unit (per_cu->cu);
14379 *last_chain = next_cu;
14380 }
14381 else
14382 last_chain = &per_cu->cu->read_in_chain;
14383
14384 per_cu = next_cu;
14385 }
14386 }
14387
14388 /* Remove a single compilation unit from the cache. */
14389
14390 static void
14391 free_one_cached_comp_unit (void *target_cu)
14392 {
14393 struct dwarf2_per_cu_data *per_cu, **last_chain;
14394
14395 per_cu = dwarf2_per_objfile->read_in_chain;
14396 last_chain = &dwarf2_per_objfile->read_in_chain;
14397 while (per_cu != NULL)
14398 {
14399 struct dwarf2_per_cu_data *next_cu;
14400
14401 next_cu = per_cu->cu->read_in_chain;
14402
14403 if (per_cu->cu == target_cu)
14404 {
14405 free_one_comp_unit (per_cu->cu);
14406 *last_chain = next_cu;
14407 break;
14408 }
14409 else
14410 last_chain = &per_cu->cu->read_in_chain;
14411
14412 per_cu = next_cu;
14413 }
14414 }
14415
14416 /* Release all extra memory associated with OBJFILE. */
14417
14418 void
14419 dwarf2_free_objfile (struct objfile *objfile)
14420 {
14421 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14422
14423 if (dwarf2_per_objfile == NULL)
14424 return;
14425
14426 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14427 free_cached_comp_units (NULL);
14428
14429 if (dwarf2_per_objfile->using_index)
14430 {
14431 int i;
14432
14433 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14434 {
14435 int j;
14436 struct dwarf2_per_cu_data *per_cu =
14437 dwarf2_per_objfile->all_comp_units[i];
14438
14439 if (!per_cu->v.quick->lines)
14440 continue;
14441
14442 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
14443 {
14444 if (per_cu->v.quick->file_names)
14445 xfree ((void *) per_cu->v.quick->file_names[j]);
14446 if (per_cu->v.quick->full_names)
14447 xfree ((void *) per_cu->v.quick->full_names[j]);
14448 }
14449
14450 free_line_header (per_cu->v.quick->lines);
14451 }
14452 }
14453
14454 /* Everything else should be on the objfile obstack. */
14455 }
14456
14457 /* A pair of DIE offset and GDB type pointer. We store these
14458 in a hash table separate from the DIEs, and preserve them
14459 when the DIEs are flushed out of cache. */
14460
14461 struct dwarf2_offset_and_type
14462 {
14463 unsigned int offset;
14464 struct type *type;
14465 };
14466
14467 /* Hash function for a dwarf2_offset_and_type. */
14468
14469 static hashval_t
14470 offset_and_type_hash (const void *item)
14471 {
14472 const struct dwarf2_offset_and_type *ofs = item;
14473
14474 return ofs->offset;
14475 }
14476
14477 /* Equality function for a dwarf2_offset_and_type. */
14478
14479 static int
14480 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14481 {
14482 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14483 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14484
14485 return ofs_lhs->offset == ofs_rhs->offset;
14486 }
14487
14488 /* Set the type associated with DIE to TYPE. Save it in CU's hash
14489 table if necessary. For convenience, return TYPE.
14490
14491 The DIEs reading must have careful ordering to:
14492 * Not cause infite loops trying to read in DIEs as a prerequisite for
14493 reading current DIE.
14494 * Not trying to dereference contents of still incompletely read in types
14495 while reading in other DIEs.
14496 * Enable referencing still incompletely read in types just by a pointer to
14497 the type without accessing its fields.
14498
14499 Therefore caller should follow these rules:
14500 * Try to fetch any prerequisite types we may need to build this DIE type
14501 before building the type and calling set_die_type.
14502 * After building type call set_die_type for current DIE as soon as
14503 possible before fetching more types to complete the current type.
14504 * Make the type as complete as possible before fetching more types. */
14505
14506 static struct type *
14507 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14508 {
14509 struct dwarf2_offset_and_type **slot, ofs;
14510 struct objfile *objfile = cu->objfile;
14511 htab_t *type_hash_ptr;
14512
14513 /* For Ada types, make sure that the gnat-specific data is always
14514 initialized (if not already set). There are a few types where
14515 we should not be doing so, because the type-specific area is
14516 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14517 where the type-specific area is used to store the floatformat).
14518 But this is not a problem, because the gnat-specific information
14519 is actually not needed for these types. */
14520 if (need_gnat_info (cu)
14521 && TYPE_CODE (type) != TYPE_CODE_FUNC
14522 && TYPE_CODE (type) != TYPE_CODE_FLT
14523 && !HAVE_GNAT_AUX_INFO (type))
14524 INIT_GNAT_SPECIFIC (type);
14525
14526 if (cu->per_cu->from_debug_types)
14527 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14528 else
14529 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14530
14531 if (*type_hash_ptr == NULL)
14532 {
14533 *type_hash_ptr
14534 = htab_create_alloc_ex (127,
14535 offset_and_type_hash,
14536 offset_and_type_eq,
14537 NULL,
14538 &objfile->objfile_obstack,
14539 hashtab_obstack_allocate,
14540 dummy_obstack_deallocate);
14541 }
14542
14543 ofs.offset = die->offset;
14544 ofs.type = type;
14545 slot = (struct dwarf2_offset_and_type **)
14546 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14547 if (*slot)
14548 complaint (&symfile_complaints,
14549 _("A problem internal to GDB: DIE 0x%x has type already set"),
14550 die->offset);
14551 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14552 **slot = ofs;
14553 return type;
14554 }
14555
14556 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14557 table, or return NULL if the die does not have a saved type. */
14558
14559 static struct type *
14560 get_die_type_at_offset (unsigned int offset,
14561 struct dwarf2_per_cu_data *per_cu)
14562 {
14563 struct dwarf2_offset_and_type *slot, ofs;
14564 htab_t type_hash;
14565
14566 if (per_cu->from_debug_types)
14567 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14568 else
14569 type_hash = dwarf2_per_objfile->debug_info_type_hash;
14570 if (type_hash == NULL)
14571 return NULL;
14572
14573 ofs.offset = offset;
14574 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14575 if (slot)
14576 return slot->type;
14577 else
14578 return NULL;
14579 }
14580
14581 /* Look up the type for DIE in the appropriate type_hash table,
14582 or return NULL if DIE does not have a saved type. */
14583
14584 static struct type *
14585 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14586 {
14587 return get_die_type_at_offset (die->offset, cu->per_cu);
14588 }
14589
14590 /* Add a dependence relationship from CU to REF_PER_CU. */
14591
14592 static void
14593 dwarf2_add_dependence (struct dwarf2_cu *cu,
14594 struct dwarf2_per_cu_data *ref_per_cu)
14595 {
14596 void **slot;
14597
14598 if (cu->dependencies == NULL)
14599 cu->dependencies
14600 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14601 NULL, &cu->comp_unit_obstack,
14602 hashtab_obstack_allocate,
14603 dummy_obstack_deallocate);
14604
14605 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14606 if (*slot == NULL)
14607 *slot = ref_per_cu;
14608 }
14609
14610 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14611 Set the mark field in every compilation unit in the
14612 cache that we must keep because we are keeping CU. */
14613
14614 static int
14615 dwarf2_mark_helper (void **slot, void *data)
14616 {
14617 struct dwarf2_per_cu_data *per_cu;
14618
14619 per_cu = (struct dwarf2_per_cu_data *) *slot;
14620 if (per_cu->cu->mark)
14621 return 1;
14622 per_cu->cu->mark = 1;
14623
14624 if (per_cu->cu->dependencies != NULL)
14625 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14626
14627 return 1;
14628 }
14629
14630 /* Set the mark field in CU and in every other compilation unit in the
14631 cache that we must keep because we are keeping CU. */
14632
14633 static void
14634 dwarf2_mark (struct dwarf2_cu *cu)
14635 {
14636 if (cu->mark)
14637 return;
14638 cu->mark = 1;
14639 if (cu->dependencies != NULL)
14640 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14641 }
14642
14643 static void
14644 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14645 {
14646 while (per_cu)
14647 {
14648 per_cu->cu->mark = 0;
14649 per_cu = per_cu->cu->read_in_chain;
14650 }
14651 }
14652
14653 /* Trivial hash function for partial_die_info: the hash value of a DIE
14654 is its offset in .debug_info for this objfile. */
14655
14656 static hashval_t
14657 partial_die_hash (const void *item)
14658 {
14659 const struct partial_die_info *part_die = item;
14660
14661 return part_die->offset;
14662 }
14663
14664 /* Trivial comparison function for partial_die_info structures: two DIEs
14665 are equal if they have the same offset. */
14666
14667 static int
14668 partial_die_eq (const void *item_lhs, const void *item_rhs)
14669 {
14670 const struct partial_die_info *part_die_lhs = item_lhs;
14671 const struct partial_die_info *part_die_rhs = item_rhs;
14672
14673 return part_die_lhs->offset == part_die_rhs->offset;
14674 }
14675
14676 static struct cmd_list_element *set_dwarf2_cmdlist;
14677 static struct cmd_list_element *show_dwarf2_cmdlist;
14678
14679 static void
14680 set_dwarf2_cmd (char *args, int from_tty)
14681 {
14682 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14683 }
14684
14685 static void
14686 show_dwarf2_cmd (char *args, int from_tty)
14687 {
14688 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14689 }
14690
14691 /* If section described by INFO was mmapped, munmap it now. */
14692
14693 static void
14694 munmap_section_buffer (struct dwarf2_section_info *info)
14695 {
14696 if (info->was_mmapped)
14697 {
14698 #ifdef HAVE_MMAP
14699 intptr_t begin = (intptr_t) info->buffer;
14700 intptr_t map_begin = begin & ~(pagesize - 1);
14701 size_t map_length = info->size + begin - map_begin;
14702
14703 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14704 #else
14705 /* Without HAVE_MMAP, we should never be here to begin with. */
14706 gdb_assert_not_reached ("no mmap support");
14707 #endif
14708 }
14709 }
14710
14711 /* munmap debug sections for OBJFILE, if necessary. */
14712
14713 static void
14714 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14715 {
14716 struct dwarf2_per_objfile *data = d;
14717
14718 /* This is sorted according to the order they're defined in to make it easier
14719 to keep in sync. */
14720 munmap_section_buffer (&data->info);
14721 munmap_section_buffer (&data->abbrev);
14722 munmap_section_buffer (&data->line);
14723 munmap_section_buffer (&data->loc);
14724 munmap_section_buffer (&data->macinfo);
14725 munmap_section_buffer (&data->str);
14726 munmap_section_buffer (&data->ranges);
14727 munmap_section_buffer (&data->types);
14728 munmap_section_buffer (&data->frame);
14729 munmap_section_buffer (&data->eh_frame);
14730 munmap_section_buffer (&data->gdb_index);
14731 }
14732
14733 \f
14734
14735 /* The contents of the hash table we create when building the string
14736 table. */
14737 struct strtab_entry
14738 {
14739 offset_type offset;
14740 const char *str;
14741 };
14742
14743 /* Hash function for a strtab_entry. */
14744 static hashval_t
14745 hash_strtab_entry (const void *e)
14746 {
14747 const struct strtab_entry *entry = e;
14748 return mapped_index_string_hash (entry->str);
14749 }
14750
14751 /* Equality function for a strtab_entry. */
14752 static int
14753 eq_strtab_entry (const void *a, const void *b)
14754 {
14755 const struct strtab_entry *ea = a;
14756 const struct strtab_entry *eb = b;
14757 return !strcmp (ea->str, eb->str);
14758 }
14759
14760 /* Create a strtab_entry hash table. */
14761 static htab_t
14762 create_strtab (void)
14763 {
14764 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14765 xfree, xcalloc, xfree);
14766 }
14767
14768 /* Add a string to the constant pool. Return the string's offset in
14769 host order. */
14770 static offset_type
14771 add_string (htab_t table, struct obstack *cpool, const char *str)
14772 {
14773 void **slot;
14774 struct strtab_entry entry;
14775 struct strtab_entry *result;
14776
14777 entry.str = str;
14778 slot = htab_find_slot (table, &entry, INSERT);
14779 if (*slot)
14780 result = *slot;
14781 else
14782 {
14783 result = XNEW (struct strtab_entry);
14784 result->offset = obstack_object_size (cpool);
14785 result->str = str;
14786 obstack_grow_str0 (cpool, str);
14787 *slot = result;
14788 }
14789 return result->offset;
14790 }
14791
14792 /* An entry in the symbol table. */
14793 struct symtab_index_entry
14794 {
14795 /* The name of the symbol. */
14796 const char *name;
14797 /* The offset of the name in the constant pool. */
14798 offset_type index_offset;
14799 /* A sorted vector of the indices of all the CUs that hold an object
14800 of this name. */
14801 VEC (offset_type) *cu_indices;
14802 };
14803
14804 /* The symbol table. This is a power-of-2-sized hash table. */
14805 struct mapped_symtab
14806 {
14807 offset_type n_elements;
14808 offset_type size;
14809 struct symtab_index_entry **data;
14810 };
14811
14812 /* Hash function for a symtab_index_entry. */
14813 static hashval_t
14814 hash_symtab_entry (const void *e)
14815 {
14816 const struct symtab_index_entry *entry = e;
14817 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14818 sizeof (offset_type) * VEC_length (offset_type,
14819 entry->cu_indices),
14820 0);
14821 }
14822
14823 /* Equality function for a symtab_index_entry. */
14824 static int
14825 eq_symtab_entry (const void *a, const void *b)
14826 {
14827 const struct symtab_index_entry *ea = a;
14828 const struct symtab_index_entry *eb = b;
14829 int len = VEC_length (offset_type, ea->cu_indices);
14830 if (len != VEC_length (offset_type, eb->cu_indices))
14831 return 0;
14832 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14833 VEC_address (offset_type, eb->cu_indices),
14834 sizeof (offset_type) * len);
14835 }
14836
14837 /* Destroy a symtab_index_entry. */
14838 static void
14839 delete_symtab_entry (void *p)
14840 {
14841 struct symtab_index_entry *entry = p;
14842 VEC_free (offset_type, entry->cu_indices);
14843 xfree (entry);
14844 }
14845
14846 /* Create a hash table holding symtab_index_entry objects. */
14847 static htab_t
14848 create_index_table (void)
14849 {
14850 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14851 delete_symtab_entry, xcalloc, xfree);
14852 }
14853
14854 /* Create a new mapped symtab object. */
14855 static struct mapped_symtab *
14856 create_mapped_symtab (void)
14857 {
14858 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14859 symtab->n_elements = 0;
14860 symtab->size = 1024;
14861 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14862 return symtab;
14863 }
14864
14865 /* Destroy a mapped_symtab. */
14866 static void
14867 cleanup_mapped_symtab (void *p)
14868 {
14869 struct mapped_symtab *symtab = p;
14870 /* The contents of the array are freed when the other hash table is
14871 destroyed. */
14872 xfree (symtab->data);
14873 xfree (symtab);
14874 }
14875
14876 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14877 the slot. */
14878 static struct symtab_index_entry **
14879 find_slot (struct mapped_symtab *symtab, const char *name)
14880 {
14881 offset_type index, step, hash = mapped_index_string_hash (name);
14882
14883 index = hash & (symtab->size - 1);
14884 step = ((hash * 17) & (symtab->size - 1)) | 1;
14885
14886 for (;;)
14887 {
14888 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14889 return &symtab->data[index];
14890 index = (index + step) & (symtab->size - 1);
14891 }
14892 }
14893
14894 /* Expand SYMTAB's hash table. */
14895 static void
14896 hash_expand (struct mapped_symtab *symtab)
14897 {
14898 offset_type old_size = symtab->size;
14899 offset_type i;
14900 struct symtab_index_entry **old_entries = symtab->data;
14901
14902 symtab->size *= 2;
14903 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14904
14905 for (i = 0; i < old_size; ++i)
14906 {
14907 if (old_entries[i])
14908 {
14909 struct symtab_index_entry **slot = find_slot (symtab,
14910 old_entries[i]->name);
14911 *slot = old_entries[i];
14912 }
14913 }
14914
14915 xfree (old_entries);
14916 }
14917
14918 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14919 is the index of the CU in which the symbol appears. */
14920 static void
14921 add_index_entry (struct mapped_symtab *symtab, const char *name,
14922 offset_type cu_index)
14923 {
14924 struct symtab_index_entry **slot;
14925
14926 ++symtab->n_elements;
14927 if (4 * symtab->n_elements / 3 >= symtab->size)
14928 hash_expand (symtab);
14929
14930 slot = find_slot (symtab, name);
14931 if (!*slot)
14932 {
14933 *slot = XNEW (struct symtab_index_entry);
14934 (*slot)->name = name;
14935 (*slot)->cu_indices = NULL;
14936 }
14937 /* Don't push an index twice. Due to how we add entries we only
14938 have to check the last one. */
14939 if (VEC_empty (offset_type, (*slot)->cu_indices)
14940 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14941 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14942 }
14943
14944 /* Add a vector of indices to the constant pool. */
14945 static offset_type
14946 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14947 struct symtab_index_entry *entry)
14948 {
14949 void **slot;
14950
14951 slot = htab_find_slot (index_table, entry, INSERT);
14952 if (!*slot)
14953 {
14954 offset_type len = VEC_length (offset_type, entry->cu_indices);
14955 offset_type val = MAYBE_SWAP (len);
14956 offset_type iter;
14957 int i;
14958
14959 *slot = entry;
14960 entry->index_offset = obstack_object_size (cpool);
14961
14962 obstack_grow (cpool, &val, sizeof (val));
14963 for (i = 0;
14964 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14965 ++i)
14966 {
14967 val = MAYBE_SWAP (iter);
14968 obstack_grow (cpool, &val, sizeof (val));
14969 }
14970 }
14971 else
14972 {
14973 struct symtab_index_entry *old_entry = *slot;
14974 entry->index_offset = old_entry->index_offset;
14975 entry = old_entry;
14976 }
14977 return entry->index_offset;
14978 }
14979
14980 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14981 constant pool entries going into the obstack CPOOL. */
14982 static void
14983 write_hash_table (struct mapped_symtab *symtab,
14984 struct obstack *output, struct obstack *cpool)
14985 {
14986 offset_type i;
14987 htab_t index_table;
14988 htab_t str_table;
14989
14990 index_table = create_index_table ();
14991 str_table = create_strtab ();
14992 /* We add all the index vectors to the constant pool first, to
14993 ensure alignment is ok. */
14994 for (i = 0; i < symtab->size; ++i)
14995 {
14996 if (symtab->data[i])
14997 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14998 }
14999
15000 /* Now write out the hash table. */
15001 for (i = 0; i < symtab->size; ++i)
15002 {
15003 offset_type str_off, vec_off;
15004
15005 if (symtab->data[i])
15006 {
15007 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15008 vec_off = symtab->data[i]->index_offset;
15009 }
15010 else
15011 {
15012 /* While 0 is a valid constant pool index, it is not valid
15013 to have 0 for both offsets. */
15014 str_off = 0;
15015 vec_off = 0;
15016 }
15017
15018 str_off = MAYBE_SWAP (str_off);
15019 vec_off = MAYBE_SWAP (vec_off);
15020
15021 obstack_grow (output, &str_off, sizeof (str_off));
15022 obstack_grow (output, &vec_off, sizeof (vec_off));
15023 }
15024
15025 htab_delete (str_table);
15026 htab_delete (index_table);
15027 }
15028
15029 /* Write an address entry to ADDR_OBSTACK. The addresses are taken
15030 from PST; CU_INDEX is the index of the CU in the vector of all
15031 CUs. */
15032 static void
15033 add_address_entry (struct objfile *objfile,
15034 struct obstack *addr_obstack, struct partial_symtab *pst,
15035 unsigned int cu_index)
15036 {
15037 offset_type offset;
15038 char addr[8];
15039 CORE_ADDR baseaddr;
15040
15041 /* Don't bother recording empty ranges. */
15042 if (pst->textlow == pst->texthigh)
15043 return;
15044
15045 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15046
15047 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
15048 obstack_grow (addr_obstack, addr, 8);
15049 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
15050 obstack_grow (addr_obstack, addr, 8);
15051 offset = MAYBE_SWAP (cu_index);
15052 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
15053 }
15054
15055 /* Add a list of partial symbols to SYMTAB. */
15056 static void
15057 write_psymbols (struct mapped_symtab *symtab,
15058 htab_t psyms_seen,
15059 struct partial_symbol **psymp,
15060 int count,
15061 offset_type cu_index,
15062 int is_static)
15063 {
15064 for (; count-- > 0; ++psymp)
15065 {
15066 void **slot, *lookup;
15067
15068 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15069 error (_("Ada is not currently supported by the index"));
15070
15071 /* We only want to add a given psymbol once. However, we also
15072 want to account for whether it is global or static. So, we
15073 may add it twice, using slightly different values. */
15074 if (is_static)
15075 {
15076 uintptr_t val = 1 | (uintptr_t) *psymp;
15077
15078 lookup = (void *) val;
15079 }
15080 else
15081 lookup = *psymp;
15082
15083 /* Only add a given psymbol once. */
15084 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15085 if (!*slot)
15086 {
15087 *slot = lookup;
15088 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15089 }
15090 }
15091 }
15092
15093 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
15094 exception if there is an error. */
15095 static void
15096 write_obstack (FILE *file, struct obstack *obstack)
15097 {
15098 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15099 file)
15100 != obstack_object_size (obstack))
15101 error (_("couldn't data write to file"));
15102 }
15103
15104 /* Unlink a file if the argument is not NULL. */
15105 static void
15106 unlink_if_set (void *p)
15107 {
15108 char **filename = p;
15109 if (*filename)
15110 unlink (*filename);
15111 }
15112
15113 /* A helper struct used when iterating over debug_types. */
15114 struct signatured_type_index_data
15115 {
15116 struct objfile *objfile;
15117 struct mapped_symtab *symtab;
15118 struct obstack *types_list;
15119 htab_t psyms_seen;
15120 int cu_index;
15121 };
15122
15123 /* A helper function that writes a single signatured_type to an
15124 obstack. */
15125 static int
15126 write_one_signatured_type (void **slot, void *d)
15127 {
15128 struct signatured_type_index_data *info = d;
15129 struct signatured_type *entry = (struct signatured_type *) *slot;
15130 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15131 struct partial_symtab *psymtab = per_cu->v.psymtab;
15132 gdb_byte val[8];
15133
15134 write_psymbols (info->symtab,
15135 info->psyms_seen,
15136 info->objfile->global_psymbols.list + psymtab->globals_offset,
15137 psymtab->n_global_syms, info->cu_index,
15138 0);
15139 write_psymbols (info->symtab,
15140 info->psyms_seen,
15141 info->objfile->static_psymbols.list + psymtab->statics_offset,
15142 psymtab->n_static_syms, info->cu_index,
15143 1);
15144
15145 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15146 obstack_grow (info->types_list, val, 8);
15147 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15148 obstack_grow (info->types_list, val, 8);
15149 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15150 obstack_grow (info->types_list, val, 8);
15151
15152 ++info->cu_index;
15153
15154 return 1;
15155 }
15156
15157 /* A cleanup function for an htab_t. */
15158
15159 static void
15160 cleanup_htab (void *arg)
15161 {
15162 htab_delete (arg);
15163 }
15164
15165 /* Create an index file for OBJFILE in the directory DIR. */
15166 static void
15167 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15168 {
15169 struct cleanup *cleanup;
15170 char *filename, *cleanup_filename;
15171 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15172 struct obstack cu_list, types_cu_list;
15173 int i;
15174 FILE *out_file;
15175 struct mapped_symtab *symtab;
15176 offset_type val, size_of_contents, total_len;
15177 struct stat st;
15178 char buf[8];
15179 htab_t psyms_seen;
15180
15181 if (!objfile->psymtabs)
15182 return;
15183 if (dwarf2_per_objfile->using_index)
15184 error (_("Cannot use an index to create the index"));
15185
15186 if (stat (objfile->name, &st) < 0)
15187 perror_with_name (_("Could not stat"));
15188
15189 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15190 INDEX_SUFFIX, (char *) NULL);
15191 cleanup = make_cleanup (xfree, filename);
15192
15193 out_file = fopen (filename, "wb");
15194 if (!out_file)
15195 error (_("Can't open `%s' for writing"), filename);
15196
15197 cleanup_filename = filename;
15198 make_cleanup (unlink_if_set, &cleanup_filename);
15199
15200 symtab = create_mapped_symtab ();
15201 make_cleanup (cleanup_mapped_symtab, symtab);
15202
15203 obstack_init (&addr_obstack);
15204 make_cleanup_obstack_free (&addr_obstack);
15205
15206 obstack_init (&cu_list);
15207 make_cleanup_obstack_free (&cu_list);
15208
15209 obstack_init (&types_cu_list);
15210 make_cleanup_obstack_free (&types_cu_list);
15211
15212 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15213 NULL, xcalloc, xfree);
15214 make_cleanup (cleanup_htab, psyms_seen);
15215
15216 /* The list is already sorted, so we don't need to do additional
15217 work here. Also, the debug_types entries do not appear in
15218 all_comp_units, but only in their own hash table. */
15219 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15220 {
15221 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15222 struct partial_symtab *psymtab = per_cu->v.psymtab;
15223 gdb_byte val[8];
15224
15225 write_psymbols (symtab,
15226 psyms_seen,
15227 objfile->global_psymbols.list + psymtab->globals_offset,
15228 psymtab->n_global_syms, i,
15229 0);
15230 write_psymbols (symtab,
15231 psyms_seen,
15232 objfile->static_psymbols.list + psymtab->statics_offset,
15233 psymtab->n_static_syms, i,
15234 1);
15235
15236 add_address_entry (objfile, &addr_obstack, psymtab, i);
15237
15238 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15239 obstack_grow (&cu_list, val, 8);
15240 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15241 obstack_grow (&cu_list, val, 8);
15242 }
15243
15244 /* Write out the .debug_type entries, if any. */
15245 if (dwarf2_per_objfile->signatured_types)
15246 {
15247 struct signatured_type_index_data sig_data;
15248
15249 sig_data.objfile = objfile;
15250 sig_data.symtab = symtab;
15251 sig_data.types_list = &types_cu_list;
15252 sig_data.psyms_seen = psyms_seen;
15253 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15254 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15255 write_one_signatured_type, &sig_data);
15256 }
15257
15258 obstack_init (&constant_pool);
15259 make_cleanup_obstack_free (&constant_pool);
15260 obstack_init (&symtab_obstack);
15261 make_cleanup_obstack_free (&symtab_obstack);
15262 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15263
15264 obstack_init (&contents);
15265 make_cleanup_obstack_free (&contents);
15266 size_of_contents = 6 * sizeof (offset_type);
15267 total_len = size_of_contents;
15268
15269 /* The version number. */
15270 val = MAYBE_SWAP (3);
15271 obstack_grow (&contents, &val, sizeof (val));
15272
15273 /* The offset of the CU list from the start of the file. */
15274 val = MAYBE_SWAP (total_len);
15275 obstack_grow (&contents, &val, sizeof (val));
15276 total_len += obstack_object_size (&cu_list);
15277
15278 /* The offset of the types CU list from the start of the file. */
15279 val = MAYBE_SWAP (total_len);
15280 obstack_grow (&contents, &val, sizeof (val));
15281 total_len += obstack_object_size (&types_cu_list);
15282
15283 /* The offset of the address table from the start of the file. */
15284 val = MAYBE_SWAP (total_len);
15285 obstack_grow (&contents, &val, sizeof (val));
15286 total_len += obstack_object_size (&addr_obstack);
15287
15288 /* The offset of the symbol table from the start of the file. */
15289 val = MAYBE_SWAP (total_len);
15290 obstack_grow (&contents, &val, sizeof (val));
15291 total_len += obstack_object_size (&symtab_obstack);
15292
15293 /* The offset of the constant pool from the start of the file. */
15294 val = MAYBE_SWAP (total_len);
15295 obstack_grow (&contents, &val, sizeof (val));
15296 total_len += obstack_object_size (&constant_pool);
15297
15298 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15299
15300 write_obstack (out_file, &contents);
15301 write_obstack (out_file, &cu_list);
15302 write_obstack (out_file, &types_cu_list);
15303 write_obstack (out_file, &addr_obstack);
15304 write_obstack (out_file, &symtab_obstack);
15305 write_obstack (out_file, &constant_pool);
15306
15307 fclose (out_file);
15308
15309 /* We want to keep the file, so we set cleanup_filename to NULL
15310 here. See unlink_if_set. */
15311 cleanup_filename = NULL;
15312
15313 do_cleanups (cleanup);
15314 }
15315
15316 /* The mapped index file format is designed to be directly mmap()able
15317 on any architecture. In most cases, a datum is represented using a
15318 little-endian 32-bit integer value, called an offset_type. Big
15319 endian machines must byte-swap the values before using them.
15320 Exceptions to this rule are noted. The data is laid out such that
15321 alignment is always respected.
15322
15323 A mapped index consists of several sections.
15324
15325 1. The file header. This is a sequence of values, of offset_type
15326 unless otherwise noted:
15327
15328 [0] The version number, currently 3. Versions 1 and 2 are
15329 obsolete.
15330 [1] The offset, from the start of the file, of the CU list.
15331 [2] The offset, from the start of the file, of the types CU list.
15332 Note that this section can be empty, in which case this offset will
15333 be equal to the next offset.
15334 [3] The offset, from the start of the file, of the address section.
15335 [4] The offset, from the start of the file, of the symbol table.
15336 [5] The offset, from the start of the file, of the constant pool.
15337
15338 2. The CU list. This is a sequence of pairs of 64-bit
15339 little-endian values, sorted by the CU offset. The first element
15340 in each pair is the offset of a CU in the .debug_info section. The
15341 second element in each pair is the length of that CU. References
15342 to a CU elsewhere in the map are done using a CU index, which is
15343 just the 0-based index into this table. Note that if there are
15344 type CUs, then conceptually CUs and type CUs form a single list for
15345 the purposes of CU indices.
15346
15347 3. The types CU list. This is a sequence of triplets of 64-bit
15348 little-endian values. In a triplet, the first value is the CU
15349 offset, the second value is the type offset in the CU, and the
15350 third value is the type signature. The types CU list is not
15351 sorted.
15352
15353 4. The address section. The address section consists of a sequence
15354 of address entries. Each address entry has three elements.
15355 [0] The low address. This is a 64-bit little-endian value.
15356 [1] The high address. This is a 64-bit little-endian value.
15357 [2] The CU index. This is an offset_type value.
15358
15359 5. The symbol table. This is a hash table. The size of the hash
15360 table is always a power of 2. The initial hash and the step are
15361 currently defined by the `find_slot' function.
15362
15363 Each slot in the hash table consists of a pair of offset_type
15364 values. The first value is the offset of the symbol's name in the
15365 constant pool. The second value is the offset of the CU vector in
15366 the constant pool.
15367
15368 If both values are 0, then this slot in the hash table is empty.
15369 This is ok because while 0 is a valid constant pool index, it
15370 cannot be a valid index for both a string and a CU vector.
15371
15372 A string in the constant pool is stored as a \0-terminated string,
15373 as you'd expect.
15374
15375 A CU vector in the constant pool is a sequence of offset_type
15376 values. The first value is the number of CU indices in the vector.
15377 Each subsequent value is the index of a CU in the CU list. This
15378 element in the hash table is used to indicate which CUs define the
15379 symbol.
15380
15381 6. The constant pool. This is simply a bunch of bytes. It is
15382 organized so that alignment is correct: CU vectors are stored
15383 first, followed by strings. */
15384
15385 static void
15386 save_gdb_index_command (char *arg, int from_tty)
15387 {
15388 struct objfile *objfile;
15389
15390 if (!arg || !*arg)
15391 error (_("usage: save gdb-index DIRECTORY"));
15392
15393 ALL_OBJFILES (objfile)
15394 {
15395 struct stat st;
15396
15397 /* If the objfile does not correspond to an actual file, skip it. */
15398 if (stat (objfile->name, &st) < 0)
15399 continue;
15400
15401 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15402 if (dwarf2_per_objfile)
15403 {
15404 volatile struct gdb_exception except;
15405
15406 TRY_CATCH (except, RETURN_MASK_ERROR)
15407 {
15408 write_psymtabs_to_index (objfile, arg);
15409 }
15410 if (except.reason < 0)
15411 exception_fprintf (gdb_stderr, except,
15412 _("Error while writing index for `%s': "),
15413 objfile->name);
15414 }
15415 }
15416 }
15417
15418 \f
15419
15420 int dwarf2_always_disassemble;
15421
15422 static void
15423 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15424 struct cmd_list_element *c, const char *value)
15425 {
15426 fprintf_filtered (file, _("\
15427 Whether to always disassemble DWARF expressions is %s.\n"),
15428 value);
15429 }
15430
15431 void _initialize_dwarf2_read (void);
15432
15433 void
15434 _initialize_dwarf2_read (void)
15435 {
15436 struct cmd_list_element *c;
15437
15438 dwarf2_objfile_data_key
15439 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15440
15441 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15442 Set DWARF 2 specific variables.\n\
15443 Configure DWARF 2 variables such as the cache size"),
15444 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15445 0/*allow-unknown*/, &maintenance_set_cmdlist);
15446
15447 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15448 Show DWARF 2 specific variables\n\
15449 Show DWARF 2 variables such as the cache size"),
15450 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15451 0/*allow-unknown*/, &maintenance_show_cmdlist);
15452
15453 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15454 &dwarf2_max_cache_age, _("\
15455 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15456 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15457 A higher limit means that cached compilation units will be stored\n\
15458 in memory longer, and more total memory will be used. Zero disables\n\
15459 caching, which can slow down startup."),
15460 NULL,
15461 show_dwarf2_max_cache_age,
15462 &set_dwarf2_cmdlist,
15463 &show_dwarf2_cmdlist);
15464
15465 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15466 &dwarf2_always_disassemble, _("\
15467 Set whether `info address' always disassembles DWARF expressions."), _("\
15468 Show whether `info address' always disassembles DWARF expressions."), _("\
15469 When enabled, DWARF expressions are always printed in an assembly-like\n\
15470 syntax. When disabled, expressions will be printed in a more\n\
15471 conversational style, when possible."),
15472 NULL,
15473 show_dwarf2_always_disassemble,
15474 &set_dwarf2_cmdlist,
15475 &show_dwarf2_cmdlist);
15476
15477 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15478 Set debugging of the dwarf2 DIE reader."), _("\
15479 Show debugging of the dwarf2 DIE reader."), _("\
15480 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15481 The value is the maximum depth to print."),
15482 NULL,
15483 NULL,
15484 &setdebuglist, &showdebuglist);
15485
15486 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15487 _("\
15488 Save a .gdb-index file.\n\
15489 Usage: save gdb-index DIRECTORY"),
15490 &save_cmdlist);
15491 set_cmd_completer (c, filename_completer);
15492 }