4289a4934fd3e9c81ca81bc159b465b3f8daf46f
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 support.
13
14 This file is part of GDB.
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h" /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82 typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98 typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in. */
119 static int dwarf2_die_debug = 0;
120
121 static int pagesize;
122
123 /* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127 static int processing_has_namespace_info;
128
129 static const struct objfile_data *dwarf2_objfile_data_key;
130
131 struct dwarf2_section_info
132 {
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
137 /* True if we have tried to read this section. */
138 int readin;
139 };
140
141 /* All offsets in the index are of this type. It must be
142 architecture-independent. */
143 typedef uint32_t offset_type;
144
145 DEF_VEC_I (offset_type);
146
147 /* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149 struct mapped_index
150 {
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157 /* The hash table. */
158 const offset_type *index_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type index_table_slots;
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163 };
164
165 struct dwarf2_per_objfile
166 {
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
174 struct dwarf2_section_info types;
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
177 struct dwarf2_section_info gdb_index;
178
179 /* Back link. */
180 struct objfile *objfile;
181
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
198
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
206
207 /* True if we are using the mapped index. */
208 unsigned char using_index;
209
210 /* The mapped index. */
211 struct mapped_index *index_table;
212
213 /* Set during partial symbol reading, to prevent queueing of full
214 symbols. */
215 int reading_partial_symbols;
216
217 /* Table mapping type .debug_info DIE offsets to types.
218 This is NULL if not allocated yet.
219 It (currently) makes sense to allocate debug_types_type_hash lazily.
220 To keep things simple we allocate both lazily. */
221 htab_t debug_info_type_hash;
222
223 /* Table mapping type .debug_types DIE offsets to types.
224 This is NULL if not allocated yet. */
225 htab_t debug_types_type_hash;
226 };
227
228 static struct dwarf2_per_objfile *dwarf2_per_objfile;
229
230 /* names of the debugging sections */
231
232 /* Note that if the debugging section has been compressed, it might
233 have a name like .zdebug_info. */
234
235 #define INFO_SECTION "debug_info"
236 #define ABBREV_SECTION "debug_abbrev"
237 #define LINE_SECTION "debug_line"
238 #define LOC_SECTION "debug_loc"
239 #define MACINFO_SECTION "debug_macinfo"
240 #define STR_SECTION "debug_str"
241 #define RANGES_SECTION "debug_ranges"
242 #define TYPES_SECTION "debug_types"
243 #define FRAME_SECTION "debug_frame"
244 #define EH_FRAME_SECTION "eh_frame"
245 #define GDB_INDEX_SECTION "gdb_index"
246
247 /* local data types */
248
249 /* We hold several abbreviation tables in memory at the same time. */
250 #ifndef ABBREV_HASH_SIZE
251 #define ABBREV_HASH_SIZE 121
252 #endif
253
254 /* The data in a compilation unit header, after target2host
255 translation, looks like this. */
256 struct comp_unit_head
257 {
258 unsigned int length;
259 short version;
260 unsigned char addr_size;
261 unsigned char signed_addr_p;
262 unsigned int abbrev_offset;
263
264 /* Size of file offsets; either 4 or 8. */
265 unsigned int offset_size;
266
267 /* Size of the length field; either 4 or 12. */
268 unsigned int initial_length_size;
269
270 /* Offset to the first byte of this compilation unit header in the
271 .debug_info section, for resolving relative reference dies. */
272 unsigned int offset;
273
274 /* Offset to first die in this cu from the start of the cu.
275 This will be the first byte following the compilation unit header. */
276 unsigned int first_die_offset;
277 };
278
279 /* Type used for delaying computation of method physnames.
280 See comments for compute_delayed_physnames. */
281 struct delayed_method_info
282 {
283 /* The type to which the method is attached, i.e., its parent class. */
284 struct type *type;
285
286 /* The index of the method in the type's function fieldlists. */
287 int fnfield_index;
288
289 /* The index of the method in the fieldlist. */
290 int index;
291
292 /* The name of the DIE. */
293 const char *name;
294
295 /* The DIE associated with this method. */
296 struct die_info *die;
297 };
298
299 typedef struct delayed_method_info delayed_method_info;
300 DEF_VEC_O (delayed_method_info);
301
302 /* Internal state when decoding a particular compilation unit. */
303 struct dwarf2_cu
304 {
305 /* The objfile containing this compilation unit. */
306 struct objfile *objfile;
307
308 /* The header of the compilation unit. */
309 struct comp_unit_head header;
310
311 /* Base address of this compilation unit. */
312 CORE_ADDR base_address;
313
314 /* Non-zero if base_address has been set. */
315 int base_known;
316
317 struct function_range *first_fn, *last_fn, *cached_fn;
318
319 /* The language we are debugging. */
320 enum language language;
321 const struct language_defn *language_defn;
322
323 const char *producer;
324
325 /* The generic symbol table building routines have separate lists for
326 file scope symbols and all all other scopes (local scopes). So
327 we need to select the right one to pass to add_symbol_to_list().
328 We do it by keeping a pointer to the correct list in list_in_scope.
329
330 FIXME: The original dwarf code just treated the file scope as the
331 first local scope, and all other local scopes as nested local
332 scopes, and worked fine. Check to see if we really need to
333 distinguish these in buildsym.c. */
334 struct pending **list_in_scope;
335
336 /* DWARF abbreviation table associated with this compilation unit. */
337 struct abbrev_info **dwarf2_abbrevs;
338
339 /* Storage for the abbrev table. */
340 struct obstack abbrev_obstack;
341
342 /* Hash table holding all the loaded partial DIEs. */
343 htab_t partial_dies;
344
345 /* Storage for things with the same lifetime as this read-in compilation
346 unit, including partial DIEs. */
347 struct obstack comp_unit_obstack;
348
349 /* When multiple dwarf2_cu structures are living in memory, this field
350 chains them all together, so that they can be released efficiently.
351 We will probably also want a generation counter so that most-recently-used
352 compilation units are cached... */
353 struct dwarf2_per_cu_data *read_in_chain;
354
355 /* Backchain to our per_cu entry if the tree has been built. */
356 struct dwarf2_per_cu_data *per_cu;
357
358 /* How many compilation units ago was this CU last referenced? */
359 int last_used;
360
361 /* A hash table of die offsets for following references. */
362 htab_t die_hash;
363
364 /* Full DIEs if read in. */
365 struct die_info *dies;
366
367 /* A set of pointers to dwarf2_per_cu_data objects for compilation
368 units referenced by this one. Only set during full symbol processing;
369 partial symbol tables do not have dependencies. */
370 htab_t dependencies;
371
372 /* Header data from the line table, during full symbol processing. */
373 struct line_header *line_header;
374
375 /* A list of methods which need to have physnames computed
376 after all type information has been read. */
377 VEC (delayed_method_info) *method_list;
378
379 /* Mark used when releasing cached dies. */
380 unsigned int mark : 1;
381
382 /* This flag will be set if this compilation unit might include
383 inter-compilation-unit references. */
384 unsigned int has_form_ref_addr : 1;
385
386 /* This flag will be set if this compilation unit includes any
387 DW_TAG_namespace DIEs. If we know that there are explicit
388 DIEs for namespaces, we don't need to try to infer them
389 from mangled names. */
390 unsigned int has_namespace_info : 1;
391 };
392
393 /* When using the index (and thus not using psymtabs), each CU has an
394 object of this type. This is used to hold information needed by
395 the various "quick" methods. */
396 struct dwarf2_per_cu_quick_data
397 {
398 /* The line table. This can be NULL if there was no line table. */
399 struct line_header *lines;
400
401 /* The file names from the line table. */
402 const char **file_names;
403 /* The file names from the line table after being run through
404 gdb_realpath. */
405 const char **full_names;
406
407 /* The corresponding symbol table. This is NULL if symbols for this
408 CU have not yet been read. */
409 struct symtab *symtab;
410
411 /* A temporary mark bit used when iterating over all CUs in
412 expand_symtabs_matching. */
413 unsigned int mark : 1;
414
415 /* True if we've tried to read the line table. */
416 unsigned int read_lines : 1;
417 };
418
419 /* Persistent data held for a compilation unit, even when not
420 processing it. We put a pointer to this structure in the
421 read_symtab_private field of the psymtab. If we encounter
422 inter-compilation-unit references, we also maintain a sorted
423 list of all compilation units. */
424
425 struct dwarf2_per_cu_data
426 {
427 /* The start offset and length of this compilation unit. 2**29-1
428 bytes should suffice to store the length of any compilation unit
429 - if it doesn't, GDB will fall over anyway.
430 NOTE: Unlike comp_unit_head.length, this length includes
431 initial_length_size. */
432 unsigned int offset;
433 unsigned int length : 29;
434
435 /* Flag indicating this compilation unit will be read in before
436 any of the current compilation units are processed. */
437 unsigned int queued : 1;
438
439 /* This flag will be set if we need to load absolutely all DIEs
440 for this compilation unit, instead of just the ones we think
441 are interesting. It gets set if we look for a DIE in the
442 hash table and don't find it. */
443 unsigned int load_all_dies : 1;
444
445 /* Non-zero if this CU is from .debug_types.
446 Otherwise it's from .debug_info. */
447 unsigned int from_debug_types : 1;
448
449 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
450 of the CU cache it gets reset to NULL again. */
451 struct dwarf2_cu *cu;
452
453 /* The corresponding objfile. */
454 struct objfile *objfile;
455
456 /* When using partial symbol tables, the 'psymtab' field is active.
457 Otherwise the 'quick' field is active. */
458 union
459 {
460 /* The partial symbol table associated with this compilation unit,
461 or NULL for partial units (which do not have an associated
462 symtab). */
463 struct partial_symtab *psymtab;
464
465 /* Data needed by the "quick" functions. */
466 struct dwarf2_per_cu_quick_data *quick;
467 } v;
468 };
469
470 /* Entry in the signatured_types hash table. */
471
472 struct signatured_type
473 {
474 ULONGEST signature;
475
476 /* Offset in .debug_types of the TU (type_unit) for this type. */
477 unsigned int offset;
478
479 /* Offset in .debug_types of the type defined by this TU. */
480 unsigned int type_offset;
481
482 /* The CU(/TU) of this type. */
483 struct dwarf2_per_cu_data per_cu;
484 };
485
486 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
487 which are used for both .debug_info and .debug_types dies.
488 All parameters here are unchanging for the life of the call.
489 This struct exists to abstract away the constant parameters of
490 die reading. */
491
492 struct die_reader_specs
493 {
494 /* The bfd of this objfile. */
495 bfd* abfd;
496
497 /* The CU of the DIE we are parsing. */
498 struct dwarf2_cu *cu;
499
500 /* Pointer to start of section buffer.
501 This is either the start of .debug_info or .debug_types. */
502 const gdb_byte *buffer;
503 };
504
505 /* The line number information for a compilation unit (found in the
506 .debug_line section) begins with a "statement program header",
507 which contains the following information. */
508 struct line_header
509 {
510 unsigned int total_length;
511 unsigned short version;
512 unsigned int header_length;
513 unsigned char minimum_instruction_length;
514 unsigned char maximum_ops_per_instruction;
515 unsigned char default_is_stmt;
516 int line_base;
517 unsigned char line_range;
518 unsigned char opcode_base;
519
520 /* standard_opcode_lengths[i] is the number of operands for the
521 standard opcode whose value is i. This means that
522 standard_opcode_lengths[0] is unused, and the last meaningful
523 element is standard_opcode_lengths[opcode_base - 1]. */
524 unsigned char *standard_opcode_lengths;
525
526 /* The include_directories table. NOTE! These strings are not
527 allocated with xmalloc; instead, they are pointers into
528 debug_line_buffer. If you try to free them, `free' will get
529 indigestion. */
530 unsigned int num_include_dirs, include_dirs_size;
531 char **include_dirs;
532
533 /* The file_names table. NOTE! These strings are not allocated
534 with xmalloc; instead, they are pointers into debug_line_buffer.
535 Don't try to free them directly. */
536 unsigned int num_file_names, file_names_size;
537 struct file_entry
538 {
539 char *name;
540 unsigned int dir_index;
541 unsigned int mod_time;
542 unsigned int length;
543 int included_p; /* Non-zero if referenced by the Line Number Program. */
544 struct symtab *symtab; /* The associated symbol table, if any. */
545 } *file_names;
546
547 /* The start and end of the statement program following this
548 header. These point into dwarf2_per_objfile->line_buffer. */
549 gdb_byte *statement_program_start, *statement_program_end;
550 };
551
552 /* When we construct a partial symbol table entry we only
553 need this much information. */
554 struct partial_die_info
555 {
556 /* Offset of this DIE. */
557 unsigned int offset;
558
559 /* DWARF-2 tag for this DIE. */
560 ENUM_BITFIELD(dwarf_tag) tag : 16;
561
562 /* Assorted flags describing the data found in this DIE. */
563 unsigned int has_children : 1;
564 unsigned int is_external : 1;
565 unsigned int is_declaration : 1;
566 unsigned int has_type : 1;
567 unsigned int has_specification : 1;
568 unsigned int has_pc_info : 1;
569
570 /* Flag set if the SCOPE field of this structure has been
571 computed. */
572 unsigned int scope_set : 1;
573
574 /* Flag set if the DIE has a byte_size attribute. */
575 unsigned int has_byte_size : 1;
576
577 /* Flag set if any of the DIE's children are template arguments. */
578 unsigned int has_template_arguments : 1;
579
580 /* The name of this DIE. Normally the value of DW_AT_name, but
581 sometimes a default name for unnamed DIEs. */
582 char *name;
583
584 /* The scope to prepend to our children. This is generally
585 allocated on the comp_unit_obstack, so will disappear
586 when this compilation unit leaves the cache. */
587 char *scope;
588
589 /* The location description associated with this DIE, if any. */
590 struct dwarf_block *locdesc;
591
592 /* If HAS_PC_INFO, the PC range associated with this DIE. */
593 CORE_ADDR lowpc;
594 CORE_ADDR highpc;
595
596 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
597 DW_AT_sibling, if any. */
598 gdb_byte *sibling;
599
600 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
601 DW_AT_specification (or DW_AT_abstract_origin or
602 DW_AT_extension). */
603 unsigned int spec_offset;
604
605 /* Pointers to this DIE's parent, first child, and next sibling,
606 if any. */
607 struct partial_die_info *die_parent, *die_child, *die_sibling;
608 };
609
610 /* This data structure holds the information of an abbrev. */
611 struct abbrev_info
612 {
613 unsigned int number; /* number identifying abbrev */
614 enum dwarf_tag tag; /* dwarf tag */
615 unsigned short has_children; /* boolean */
616 unsigned short num_attrs; /* number of attributes */
617 struct attr_abbrev *attrs; /* an array of attribute descriptions */
618 struct abbrev_info *next; /* next in chain */
619 };
620
621 struct attr_abbrev
622 {
623 ENUM_BITFIELD(dwarf_attribute) name : 16;
624 ENUM_BITFIELD(dwarf_form) form : 16;
625 };
626
627 /* Attributes have a name and a value */
628 struct attribute
629 {
630 ENUM_BITFIELD(dwarf_attribute) name : 16;
631 ENUM_BITFIELD(dwarf_form) form : 15;
632
633 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
634 field should be in u.str (existing only for DW_STRING) but it is kept
635 here for better struct attribute alignment. */
636 unsigned int string_is_canonical : 1;
637
638 union
639 {
640 char *str;
641 struct dwarf_block *blk;
642 ULONGEST unsnd;
643 LONGEST snd;
644 CORE_ADDR addr;
645 struct signatured_type *signatured_type;
646 }
647 u;
648 };
649
650 /* This data structure holds a complete die structure. */
651 struct die_info
652 {
653 /* DWARF-2 tag for this DIE. */
654 ENUM_BITFIELD(dwarf_tag) tag : 16;
655
656 /* Number of attributes */
657 unsigned char num_attrs;
658
659 /* True if we're presently building the full type name for the
660 type derived from this DIE. */
661 unsigned char building_fullname : 1;
662
663 /* Abbrev number */
664 unsigned int abbrev;
665
666 /* Offset in .debug_info or .debug_types section. */
667 unsigned int offset;
668
669 /* The dies in a compilation unit form an n-ary tree. PARENT
670 points to this die's parent; CHILD points to the first child of
671 this node; and all the children of a given node are chained
672 together via their SIBLING fields. */
673 struct die_info *child; /* Its first child, if any. */
674 struct die_info *sibling; /* Its next sibling, if any. */
675 struct die_info *parent; /* Its parent, if any. */
676
677 /* An array of attributes, with NUM_ATTRS elements. There may be
678 zero, but it's not common and zero-sized arrays are not
679 sufficiently portable C. */
680 struct attribute attrs[1];
681 };
682
683 struct function_range
684 {
685 const char *name;
686 CORE_ADDR lowpc, highpc;
687 int seen_line;
688 struct function_range *next;
689 };
690
691 /* Get at parts of an attribute structure */
692
693 #define DW_STRING(attr) ((attr)->u.str)
694 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
695 #define DW_UNSND(attr) ((attr)->u.unsnd)
696 #define DW_BLOCK(attr) ((attr)->u.blk)
697 #define DW_SND(attr) ((attr)->u.snd)
698 #define DW_ADDR(attr) ((attr)->u.addr)
699 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
700
701 /* Blocks are a bunch of untyped bytes. */
702 struct dwarf_block
703 {
704 unsigned int size;
705 gdb_byte *data;
706 };
707
708 #ifndef ATTR_ALLOC_CHUNK
709 #define ATTR_ALLOC_CHUNK 4
710 #endif
711
712 /* Allocate fields for structs, unions and enums in this size. */
713 #ifndef DW_FIELD_ALLOC_CHUNK
714 #define DW_FIELD_ALLOC_CHUNK 4
715 #endif
716
717 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
718 but this would require a corresponding change in unpack_field_as_long
719 and friends. */
720 static int bits_per_byte = 8;
721
722 /* The routines that read and process dies for a C struct or C++ class
723 pass lists of data member fields and lists of member function fields
724 in an instance of a field_info structure, as defined below. */
725 struct field_info
726 {
727 /* List of data member and baseclasses fields. */
728 struct nextfield
729 {
730 struct nextfield *next;
731 int accessibility;
732 int virtuality;
733 struct field field;
734 }
735 *fields, *baseclasses;
736
737 /* Number of fields (including baseclasses). */
738 int nfields;
739
740 /* Number of baseclasses. */
741 int nbaseclasses;
742
743 /* Set if the accesibility of one of the fields is not public. */
744 int non_public_fields;
745
746 /* Member function fields array, entries are allocated in the order they
747 are encountered in the object file. */
748 struct nextfnfield
749 {
750 struct nextfnfield *next;
751 struct fn_field fnfield;
752 }
753 *fnfields;
754
755 /* Member function fieldlist array, contains name of possibly overloaded
756 member function, number of overloaded member functions and a pointer
757 to the head of the member function field chain. */
758 struct fnfieldlist
759 {
760 char *name;
761 int length;
762 struct nextfnfield *head;
763 }
764 *fnfieldlists;
765
766 /* Number of entries in the fnfieldlists array. */
767 int nfnfields;
768
769 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
770 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
771 struct typedef_field_list
772 {
773 struct typedef_field field;
774 struct typedef_field_list *next;
775 }
776 *typedef_field_list;
777 unsigned typedef_field_list_count;
778 };
779
780 /* One item on the queue of compilation units to read in full symbols
781 for. */
782 struct dwarf2_queue_item
783 {
784 struct dwarf2_per_cu_data *per_cu;
785 struct dwarf2_queue_item *next;
786 };
787
788 /* The current queue. */
789 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
790
791 /* Loaded secondary compilation units are kept in memory until they
792 have not been referenced for the processing of this many
793 compilation units. Set this to zero to disable caching. Cache
794 sizes of up to at least twenty will improve startup time for
795 typical inter-CU-reference binaries, at an obvious memory cost. */
796 static int dwarf2_max_cache_age = 5;
797 static void
798 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
799 struct cmd_list_element *c, const char *value)
800 {
801 fprintf_filtered (file, _("\
802 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
803 value);
804 }
805
806
807 /* Various complaints about symbol reading that don't abort the process */
808
809 static void
810 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
811 {
812 complaint (&symfile_complaints,
813 _("statement list doesn't fit in .debug_line section"));
814 }
815
816 static void
817 dwarf2_debug_line_missing_file_complaint (void)
818 {
819 complaint (&symfile_complaints,
820 _(".debug_line section has line data without a file"));
821 }
822
823 static void
824 dwarf2_debug_line_missing_end_sequence_complaint (void)
825 {
826 complaint (&symfile_complaints,
827 _(".debug_line section has line program sequence without an end"));
828 }
829
830 static void
831 dwarf2_complex_location_expr_complaint (void)
832 {
833 complaint (&symfile_complaints, _("location expression too complex"));
834 }
835
836 static void
837 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
838 int arg3)
839 {
840 complaint (&symfile_complaints,
841 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
842 arg2, arg3);
843 }
844
845 static void
846 dwarf2_macros_too_long_complaint (void)
847 {
848 complaint (&symfile_complaints,
849 _("macro info runs off end of `.debug_macinfo' section"));
850 }
851
852 static void
853 dwarf2_macro_malformed_definition_complaint (const char *arg1)
854 {
855 complaint (&symfile_complaints,
856 _("macro debug info contains a malformed macro definition:\n`%s'"),
857 arg1);
858 }
859
860 static void
861 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
862 {
863 complaint (&symfile_complaints,
864 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
865 }
866
867 /* local function prototypes */
868
869 static void dwarf2_locate_sections (bfd *, asection *, void *);
870
871 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
872 struct objfile *);
873
874 static void dwarf2_build_psymtabs_hard (struct objfile *);
875
876 static void scan_partial_symbols (struct partial_die_info *,
877 CORE_ADDR *, CORE_ADDR *,
878 int, struct dwarf2_cu *);
879
880 static void add_partial_symbol (struct partial_die_info *,
881 struct dwarf2_cu *);
882
883 static void add_partial_namespace (struct partial_die_info *pdi,
884 CORE_ADDR *lowpc, CORE_ADDR *highpc,
885 int need_pc, struct dwarf2_cu *cu);
886
887 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
888 CORE_ADDR *highpc, int need_pc,
889 struct dwarf2_cu *cu);
890
891 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
892 struct dwarf2_cu *cu);
893
894 static void add_partial_subprogram (struct partial_die_info *pdi,
895 CORE_ADDR *lowpc, CORE_ADDR *highpc,
896 int need_pc, struct dwarf2_cu *cu);
897
898 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
899 gdb_byte *buffer, gdb_byte *info_ptr,
900 bfd *abfd, struct dwarf2_cu *cu);
901
902 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
903
904 static void psymtab_to_symtab_1 (struct partial_symtab *);
905
906 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
907
908 static void dwarf2_free_abbrev_table (void *);
909
910 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
911 struct dwarf2_cu *);
912
913 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
914 struct dwarf2_cu *);
915
916 static struct partial_die_info *load_partial_dies (bfd *,
917 gdb_byte *, gdb_byte *,
918 int, struct dwarf2_cu *);
919
920 static gdb_byte *read_partial_die (struct partial_die_info *,
921 struct abbrev_info *abbrev,
922 unsigned int, bfd *,
923 gdb_byte *, gdb_byte *,
924 struct dwarf2_cu *);
925
926 static struct partial_die_info *find_partial_die (unsigned int,
927 struct dwarf2_cu *);
928
929 static void fixup_partial_die (struct partial_die_info *,
930 struct dwarf2_cu *);
931
932 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
933 bfd *, gdb_byte *, struct dwarf2_cu *);
934
935 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
936 bfd *, gdb_byte *, struct dwarf2_cu *);
937
938 static unsigned int read_1_byte (bfd *, gdb_byte *);
939
940 static int read_1_signed_byte (bfd *, gdb_byte *);
941
942 static unsigned int read_2_bytes (bfd *, gdb_byte *);
943
944 static unsigned int read_4_bytes (bfd *, gdb_byte *);
945
946 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
947
948 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
949 unsigned int *);
950
951 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
952
953 static LONGEST read_checked_initial_length_and_offset
954 (bfd *, gdb_byte *, const struct comp_unit_head *,
955 unsigned int *, unsigned int *);
956
957 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
958 unsigned int *);
959
960 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
961
962 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
963
964 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
965
966 static char *read_indirect_string (bfd *, gdb_byte *,
967 const struct comp_unit_head *,
968 unsigned int *);
969
970 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
971
972 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
973
974 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
975
976 static void set_cu_language (unsigned int, struct dwarf2_cu *);
977
978 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
979 struct dwarf2_cu *);
980
981 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
982 unsigned int,
983 struct dwarf2_cu *);
984
985 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
986 struct dwarf2_cu *cu);
987
988 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
989
990 static struct die_info *die_specification (struct die_info *die,
991 struct dwarf2_cu **);
992
993 static void free_line_header (struct line_header *lh);
994
995 static void add_file_name (struct line_header *, char *, unsigned int,
996 unsigned int, unsigned int);
997
998 static struct line_header *(dwarf_decode_line_header
999 (unsigned int offset,
1000 bfd *abfd, struct dwarf2_cu *cu));
1001
1002 static void dwarf_decode_lines (struct line_header *, char *, bfd *,
1003 struct dwarf2_cu *, struct partial_symtab *);
1004
1005 static void dwarf2_start_subfile (char *, char *, char *);
1006
1007 static struct symbol *new_symbol (struct die_info *, struct type *,
1008 struct dwarf2_cu *);
1009
1010 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1011 struct dwarf2_cu *, struct symbol *);
1012
1013 static void dwarf2_const_value (struct attribute *, struct symbol *,
1014 struct dwarf2_cu *);
1015
1016 static void dwarf2_const_value_attr (struct attribute *attr,
1017 struct type *type,
1018 const char *name,
1019 struct obstack *obstack,
1020 struct dwarf2_cu *cu, long *value,
1021 gdb_byte **bytes,
1022 struct dwarf2_locexpr_baton **baton);
1023
1024 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1025
1026 static int need_gnat_info (struct dwarf2_cu *);
1027
1028 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1029
1030 static void set_descriptive_type (struct type *, struct die_info *,
1031 struct dwarf2_cu *);
1032
1033 static struct type *die_containing_type (struct die_info *,
1034 struct dwarf2_cu *);
1035
1036 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1037 struct dwarf2_cu *);
1038
1039 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1040
1041 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1042
1043 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1044
1045 static char *typename_concat (struct obstack *obs, const char *prefix,
1046 const char *suffix, int physname,
1047 struct dwarf2_cu *cu);
1048
1049 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1050
1051 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1052
1053 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1054
1055 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1056
1057 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1058 struct dwarf2_cu *, struct partial_symtab *);
1059
1060 static int dwarf2_get_pc_bounds (struct die_info *,
1061 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1062 struct partial_symtab *);
1063
1064 static void get_scope_pc_bounds (struct die_info *,
1065 CORE_ADDR *, CORE_ADDR *,
1066 struct dwarf2_cu *);
1067
1068 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1069 CORE_ADDR, struct dwarf2_cu *);
1070
1071 static void dwarf2_add_field (struct field_info *, struct die_info *,
1072 struct dwarf2_cu *);
1073
1074 static void dwarf2_attach_fields_to_type (struct field_info *,
1075 struct type *, struct dwarf2_cu *);
1076
1077 static void dwarf2_add_member_fn (struct field_info *,
1078 struct die_info *, struct type *,
1079 struct dwarf2_cu *);
1080
1081 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1082 struct type *, struct dwarf2_cu *);
1083
1084 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1085
1086 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1087
1088 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1089
1090 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1091
1092 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1093
1094 static struct type *read_module_type (struct die_info *die,
1095 struct dwarf2_cu *cu);
1096
1097 static const char *namespace_name (struct die_info *die,
1098 int *is_anonymous, struct dwarf2_cu *);
1099
1100 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1101
1102 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1103
1104 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1105 struct dwarf2_cu *);
1106
1107 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1108
1109 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1110 gdb_byte *info_ptr,
1111 gdb_byte **new_info_ptr,
1112 struct die_info *parent);
1113
1114 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1115 gdb_byte *info_ptr,
1116 gdb_byte **new_info_ptr,
1117 struct die_info *parent);
1118
1119 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1120 gdb_byte *info_ptr,
1121 gdb_byte **new_info_ptr,
1122 struct die_info *parent);
1123
1124 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1125 struct die_info **, gdb_byte *,
1126 int *);
1127
1128 static void process_die (struct die_info *, struct dwarf2_cu *);
1129
1130 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1131 struct obstack *);
1132
1133 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1134
1135 static const char *dwarf2_full_name (char *name,
1136 struct die_info *die,
1137 struct dwarf2_cu *cu);
1138
1139 static struct die_info *dwarf2_extension (struct die_info *die,
1140 struct dwarf2_cu **);
1141
1142 static char *dwarf_tag_name (unsigned int);
1143
1144 static char *dwarf_attr_name (unsigned int);
1145
1146 static char *dwarf_form_name (unsigned int);
1147
1148 static char *dwarf_bool_name (unsigned int);
1149
1150 static char *dwarf_type_encoding_name (unsigned int);
1151
1152 #if 0
1153 static char *dwarf_cfi_name (unsigned int);
1154 #endif
1155
1156 static struct die_info *sibling_die (struct die_info *);
1157
1158 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1159
1160 static void dump_die_for_error (struct die_info *);
1161
1162 static void dump_die_1 (struct ui_file *, int level, int max_level,
1163 struct die_info *);
1164
1165 /*static*/ void dump_die (struct die_info *, int max_level);
1166
1167 static void store_in_ref_table (struct die_info *,
1168 struct dwarf2_cu *);
1169
1170 static int is_ref_attr (struct attribute *);
1171
1172 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1173
1174 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1175
1176 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1177 struct attribute *,
1178 struct dwarf2_cu **);
1179
1180 static struct die_info *follow_die_ref (struct die_info *,
1181 struct attribute *,
1182 struct dwarf2_cu **);
1183
1184 static struct die_info *follow_die_sig (struct die_info *,
1185 struct attribute *,
1186 struct dwarf2_cu **);
1187
1188 static void read_signatured_type_at_offset (struct objfile *objfile,
1189 unsigned int offset);
1190
1191 static void read_signatured_type (struct objfile *,
1192 struct signatured_type *type_sig);
1193
1194 /* memory allocation interface */
1195
1196 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1197
1198 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1199
1200 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1201
1202 static void initialize_cu_func_list (struct dwarf2_cu *);
1203
1204 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1205 struct dwarf2_cu *);
1206
1207 static void dwarf_decode_macros (struct line_header *, unsigned int,
1208 char *, bfd *, struct dwarf2_cu *);
1209
1210 static int attr_form_is_block (struct attribute *);
1211
1212 static int attr_form_is_section_offset (struct attribute *);
1213
1214 static int attr_form_is_constant (struct attribute *);
1215
1216 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1217 struct symbol *sym,
1218 struct dwarf2_cu *cu);
1219
1220 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1221 struct abbrev_info *abbrev,
1222 struct dwarf2_cu *cu);
1223
1224 static void free_stack_comp_unit (void *);
1225
1226 static hashval_t partial_die_hash (const void *item);
1227
1228 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1229
1230 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1231 (unsigned int offset, struct objfile *objfile);
1232
1233 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1234 (unsigned int offset, struct objfile *objfile);
1235
1236 static struct dwarf2_cu *alloc_one_comp_unit (struct objfile *objfile);
1237
1238 static void free_one_comp_unit (void *);
1239
1240 static void free_cached_comp_units (void *);
1241
1242 static void age_cached_comp_units (void);
1243
1244 static void free_one_cached_comp_unit (void *);
1245
1246 static struct type *set_die_type (struct die_info *, struct type *,
1247 struct dwarf2_cu *);
1248
1249 static void create_all_comp_units (struct objfile *);
1250
1251 static int create_debug_types_hash_table (struct objfile *objfile);
1252
1253 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1254 struct objfile *);
1255
1256 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1257
1258 static void dwarf2_add_dependence (struct dwarf2_cu *,
1259 struct dwarf2_per_cu_data *);
1260
1261 static void dwarf2_mark (struct dwarf2_cu *);
1262
1263 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1264
1265 static struct type *get_die_type_at_offset (unsigned int,
1266 struct dwarf2_per_cu_data *per_cu);
1267
1268 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1269
1270 static void dwarf2_release_queue (void *dummy);
1271
1272 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1273 struct objfile *objfile);
1274
1275 static void process_queue (struct objfile *objfile);
1276
1277 static void find_file_and_directory (struct die_info *die,
1278 struct dwarf2_cu *cu,
1279 char **name, char **comp_dir);
1280
1281 static char *file_full_name (int file, struct line_header *lh,
1282 const char *comp_dir);
1283
1284 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1285 gdb_byte *info_ptr,
1286 gdb_byte *buffer,
1287 unsigned int buffer_size,
1288 bfd *abfd);
1289
1290 static void init_cu_die_reader (struct die_reader_specs *reader,
1291 struct dwarf2_cu *cu);
1292
1293 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1294
1295 #if WORDS_BIGENDIAN
1296
1297 /* Convert VALUE between big- and little-endian. */
1298 static offset_type
1299 byte_swap (offset_type value)
1300 {
1301 offset_type result;
1302
1303 result = (value & 0xff) << 24;
1304 result |= (value & 0xff00) << 8;
1305 result |= (value & 0xff0000) >> 8;
1306 result |= (value & 0xff000000) >> 24;
1307 return result;
1308 }
1309
1310 #define MAYBE_SWAP(V) byte_swap (V)
1311
1312 #else
1313 #define MAYBE_SWAP(V) (V)
1314 #endif /* WORDS_BIGENDIAN */
1315
1316 /* The suffix for an index file. */
1317 #define INDEX_SUFFIX ".gdb-index"
1318
1319 static const char *dwarf2_physname (char *name, struct die_info *die,
1320 struct dwarf2_cu *cu);
1321
1322 /* Try to locate the sections we need for DWARF 2 debugging
1323 information and return true if we have enough to do something. */
1324
1325 int
1326 dwarf2_has_info (struct objfile *objfile)
1327 {
1328 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1329 if (!dwarf2_per_objfile)
1330 {
1331 /* Initialize per-objfile state. */
1332 struct dwarf2_per_objfile *data
1333 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1334
1335 memset (data, 0, sizeof (*data));
1336 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1337 dwarf2_per_objfile = data;
1338
1339 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1340 dwarf2_per_objfile->objfile = objfile;
1341 }
1342 return (dwarf2_per_objfile->info.asection != NULL
1343 && dwarf2_per_objfile->abbrev.asection != NULL);
1344 }
1345
1346 /* When loading sections, we can either look for ".<name>", or for
1347 * ".z<name>", which indicates a compressed section. */
1348
1349 static int
1350 section_is_p (const char *section_name, const char *name)
1351 {
1352 return (section_name[0] == '.'
1353 && (strcmp (section_name + 1, name) == 0
1354 || (section_name[1] == 'z'
1355 && strcmp (section_name + 2, name) == 0)));
1356 }
1357
1358 /* This function is mapped across the sections and remembers the
1359 offset and size of each of the debugging sections we are interested
1360 in. */
1361
1362 static void
1363 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1364 {
1365 if (section_is_p (sectp->name, INFO_SECTION))
1366 {
1367 dwarf2_per_objfile->info.asection = sectp;
1368 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1369 }
1370 else if (section_is_p (sectp->name, ABBREV_SECTION))
1371 {
1372 dwarf2_per_objfile->abbrev.asection = sectp;
1373 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1374 }
1375 else if (section_is_p (sectp->name, LINE_SECTION))
1376 {
1377 dwarf2_per_objfile->line.asection = sectp;
1378 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1379 }
1380 else if (section_is_p (sectp->name, LOC_SECTION))
1381 {
1382 dwarf2_per_objfile->loc.asection = sectp;
1383 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1384 }
1385 else if (section_is_p (sectp->name, MACINFO_SECTION))
1386 {
1387 dwarf2_per_objfile->macinfo.asection = sectp;
1388 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1389 }
1390 else if (section_is_p (sectp->name, STR_SECTION))
1391 {
1392 dwarf2_per_objfile->str.asection = sectp;
1393 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1394 }
1395 else if (section_is_p (sectp->name, FRAME_SECTION))
1396 {
1397 dwarf2_per_objfile->frame.asection = sectp;
1398 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1399 }
1400 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1401 {
1402 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1403
1404 if (aflag & SEC_HAS_CONTENTS)
1405 {
1406 dwarf2_per_objfile->eh_frame.asection = sectp;
1407 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1408 }
1409 }
1410 else if (section_is_p (sectp->name, RANGES_SECTION))
1411 {
1412 dwarf2_per_objfile->ranges.asection = sectp;
1413 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1414 }
1415 else if (section_is_p (sectp->name, TYPES_SECTION))
1416 {
1417 dwarf2_per_objfile->types.asection = sectp;
1418 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1419 }
1420 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1421 {
1422 dwarf2_per_objfile->gdb_index.asection = sectp;
1423 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1424 }
1425
1426 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1427 && bfd_section_vma (abfd, sectp) == 0)
1428 dwarf2_per_objfile->has_section_at_zero = 1;
1429 }
1430
1431 /* Decompress a section that was compressed using zlib. Store the
1432 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1433
1434 static void
1435 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1436 gdb_byte **outbuf, bfd_size_type *outsize)
1437 {
1438 bfd *abfd = objfile->obfd;
1439 #ifndef HAVE_ZLIB_H
1440 error (_("Support for zlib-compressed DWARF data (from '%s') "
1441 "is disabled in this copy of GDB"),
1442 bfd_get_filename (abfd));
1443 #else
1444 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1445 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1446 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1447 bfd_size_type uncompressed_size;
1448 gdb_byte *uncompressed_buffer;
1449 z_stream strm;
1450 int rc;
1451 int header_size = 12;
1452
1453 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1454 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1455 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1456 bfd_get_filename (abfd));
1457
1458 /* Read the zlib header. In this case, it should be "ZLIB" followed
1459 by the uncompressed section size, 8 bytes in big-endian order. */
1460 if (compressed_size < header_size
1461 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1462 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1463 bfd_get_filename (abfd));
1464 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1465 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1466 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1467 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1468 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1469 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1470 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1471 uncompressed_size += compressed_buffer[11];
1472
1473 /* It is possible the section consists of several compressed
1474 buffers concatenated together, so we uncompress in a loop. */
1475 strm.zalloc = NULL;
1476 strm.zfree = NULL;
1477 strm.opaque = NULL;
1478 strm.avail_in = compressed_size - header_size;
1479 strm.next_in = (Bytef*) compressed_buffer + header_size;
1480 strm.avail_out = uncompressed_size;
1481 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1482 uncompressed_size);
1483 rc = inflateInit (&strm);
1484 while (strm.avail_in > 0)
1485 {
1486 if (rc != Z_OK)
1487 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1488 bfd_get_filename (abfd), rc);
1489 strm.next_out = ((Bytef*) uncompressed_buffer
1490 + (uncompressed_size - strm.avail_out));
1491 rc = inflate (&strm, Z_FINISH);
1492 if (rc != Z_STREAM_END)
1493 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1494 bfd_get_filename (abfd), rc);
1495 rc = inflateReset (&strm);
1496 }
1497 rc = inflateEnd (&strm);
1498 if (rc != Z_OK
1499 || strm.avail_out != 0)
1500 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1501 bfd_get_filename (abfd), rc);
1502
1503 do_cleanups (cleanup);
1504 *outbuf = uncompressed_buffer;
1505 *outsize = uncompressed_size;
1506 #endif
1507 }
1508
1509 /* Read the contents of the section SECTP from object file specified by
1510 OBJFILE, store info about the section into INFO.
1511 If the section is compressed, uncompress it before returning. */
1512
1513 static void
1514 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1515 {
1516 bfd *abfd = objfile->obfd;
1517 asection *sectp = info->asection;
1518 gdb_byte *buf, *retbuf;
1519 unsigned char header[4];
1520
1521 if (info->readin)
1522 return;
1523 info->buffer = NULL;
1524 info->was_mmapped = 0;
1525 info->readin = 1;
1526
1527 if (info->asection == NULL || info->size == 0)
1528 return;
1529
1530 /* Check if the file has a 4-byte header indicating compression. */
1531 if (info->size > sizeof (header)
1532 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1533 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1534 {
1535 /* Upon decompression, update the buffer and its size. */
1536 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1537 {
1538 zlib_decompress_section (objfile, sectp, &info->buffer,
1539 &info->size);
1540 return;
1541 }
1542 }
1543
1544 #ifdef HAVE_MMAP
1545 if (pagesize == 0)
1546 pagesize = getpagesize ();
1547
1548 /* Only try to mmap sections which are large enough: we don't want to
1549 waste space due to fragmentation. Also, only try mmap for sections
1550 without relocations. */
1551
1552 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1553 {
1554 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1555 size_t map_length = info->size + sectp->filepos - pg_offset;
1556 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1557 MAP_PRIVATE, pg_offset);
1558
1559 if (retbuf != MAP_FAILED)
1560 {
1561 info->was_mmapped = 1;
1562 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1563 #if HAVE_POSIX_MADVISE
1564 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1565 #endif
1566 return;
1567 }
1568 }
1569 #endif
1570
1571 /* If we get here, we are a normal, not-compressed section. */
1572 info->buffer = buf
1573 = obstack_alloc (&objfile->objfile_obstack, info->size);
1574
1575 /* When debugging .o files, we may need to apply relocations; see
1576 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1577 We never compress sections in .o files, so we only need to
1578 try this when the section is not compressed. */
1579 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1580 if (retbuf != NULL)
1581 {
1582 info->buffer = retbuf;
1583 return;
1584 }
1585
1586 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1587 || bfd_bread (buf, info->size, abfd) != info->size)
1588 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1589 bfd_get_filename (abfd));
1590 }
1591
1592 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1593 SECTION_NAME. */
1594
1595 void
1596 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1597 asection **sectp, gdb_byte **bufp,
1598 bfd_size_type *sizep)
1599 {
1600 struct dwarf2_per_objfile *data
1601 = objfile_data (objfile, dwarf2_objfile_data_key);
1602 struct dwarf2_section_info *info;
1603
1604 /* We may see an objfile without any DWARF, in which case we just
1605 return nothing. */
1606 if (data == NULL)
1607 {
1608 *sectp = NULL;
1609 *bufp = NULL;
1610 *sizep = 0;
1611 return;
1612 }
1613 if (section_is_p (section_name, EH_FRAME_SECTION))
1614 info = &data->eh_frame;
1615 else if (section_is_p (section_name, FRAME_SECTION))
1616 info = &data->frame;
1617 else
1618 gdb_assert_not_reached ("unexpected section");
1619
1620 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1621 /* We haven't read this section in yet. Do it now. */
1622 dwarf2_read_section (objfile, info);
1623
1624 *sectp = info->asection;
1625 *bufp = info->buffer;
1626 *sizep = info->size;
1627 }
1628
1629 \f
1630
1631 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1632 this CU came. */
1633 static void
1634 dw2_do_instantiate_symtab (struct objfile *objfile,
1635 struct dwarf2_per_cu_data *per_cu)
1636 {
1637 struct cleanup *back_to;
1638
1639 back_to = make_cleanup (dwarf2_release_queue, NULL);
1640
1641 queue_comp_unit (per_cu, objfile);
1642
1643 if (per_cu->from_debug_types)
1644 read_signatured_type_at_offset (objfile, per_cu->offset);
1645 else
1646 load_full_comp_unit (per_cu, objfile);
1647
1648 process_queue (objfile);
1649
1650 /* Age the cache, releasing compilation units that have not
1651 been used recently. */
1652 age_cached_comp_units ();
1653
1654 do_cleanups (back_to);
1655 }
1656
1657 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1658 the objfile from which this CU came. Returns the resulting symbol
1659 table. */
1660 static struct symtab *
1661 dw2_instantiate_symtab (struct objfile *objfile,
1662 struct dwarf2_per_cu_data *per_cu)
1663 {
1664 if (!per_cu->v.quick->symtab)
1665 {
1666 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1667 increment_reading_symtab ();
1668 dw2_do_instantiate_symtab (objfile, per_cu);
1669 do_cleanups (back_to);
1670 }
1671 return per_cu->v.quick->symtab;
1672 }
1673
1674 /* Return the CU given its index. */
1675 static struct dwarf2_per_cu_data *
1676 dw2_get_cu (int index)
1677 {
1678 if (index >= dwarf2_per_objfile->n_comp_units)
1679 {
1680 index -= dwarf2_per_objfile->n_comp_units;
1681 return dwarf2_per_objfile->type_comp_units[index];
1682 }
1683 return dwarf2_per_objfile->all_comp_units[index];
1684 }
1685
1686 /* A helper function that knows how to read a 64-bit value in a way
1687 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1688 otherwise. */
1689 static int
1690 extract_cu_value (const char *bytes, ULONGEST *result)
1691 {
1692 if (sizeof (ULONGEST) < 8)
1693 {
1694 int i;
1695
1696 /* Ignore the upper 4 bytes if they are all zero. */
1697 for (i = 0; i < 4; ++i)
1698 if (bytes[i + 4] != 0)
1699 return 0;
1700
1701 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1702 }
1703 else
1704 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1705 return 1;
1706 }
1707
1708 /* Read the CU list from the mapped index, and use it to create all
1709 the CU objects for this objfile. Return 0 if something went wrong,
1710 1 if everything went ok. */
1711 static int
1712 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1713 offset_type cu_list_elements)
1714 {
1715 offset_type i;
1716
1717 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1718 dwarf2_per_objfile->all_comp_units
1719 = obstack_alloc (&objfile->objfile_obstack,
1720 dwarf2_per_objfile->n_comp_units
1721 * sizeof (struct dwarf2_per_cu_data *));
1722
1723 for (i = 0; i < cu_list_elements; i += 2)
1724 {
1725 struct dwarf2_per_cu_data *the_cu;
1726 ULONGEST offset, length;
1727
1728 if (!extract_cu_value (cu_list, &offset)
1729 || !extract_cu_value (cu_list + 8, &length))
1730 return 0;
1731 cu_list += 2 * 8;
1732
1733 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1734 struct dwarf2_per_cu_data);
1735 the_cu->offset = offset;
1736 the_cu->length = length;
1737 the_cu->objfile = objfile;
1738 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1739 struct dwarf2_per_cu_quick_data);
1740 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1741 }
1742
1743 return 1;
1744 }
1745
1746 /* Create the signatured type hash table from the index. */
1747
1748 static int
1749 create_signatured_type_table_from_index (struct objfile *objfile,
1750 const gdb_byte *bytes,
1751 offset_type elements)
1752 {
1753 offset_type i;
1754 htab_t sig_types_hash;
1755
1756 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1757 dwarf2_per_objfile->type_comp_units
1758 = obstack_alloc (&objfile->objfile_obstack,
1759 dwarf2_per_objfile->n_type_comp_units
1760 * sizeof (struct dwarf2_per_cu_data *));
1761
1762 sig_types_hash = allocate_signatured_type_table (objfile);
1763
1764 for (i = 0; i < elements; i += 3)
1765 {
1766 struct signatured_type *type_sig;
1767 ULONGEST offset, type_offset, signature;
1768 void **slot;
1769
1770 if (!extract_cu_value (bytes, &offset)
1771 || !extract_cu_value (bytes + 8, &type_offset))
1772 return 0;
1773 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1774 bytes += 3 * 8;
1775
1776 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1777 struct signatured_type);
1778 type_sig->signature = signature;
1779 type_sig->offset = offset;
1780 type_sig->type_offset = type_offset;
1781 type_sig->per_cu.from_debug_types = 1;
1782 type_sig->per_cu.offset = offset;
1783 type_sig->per_cu.objfile = objfile;
1784 type_sig->per_cu.v.quick
1785 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1786 struct dwarf2_per_cu_quick_data);
1787
1788 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1789 *slot = type_sig;
1790
1791 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1792 }
1793
1794 dwarf2_per_objfile->signatured_types = sig_types_hash;
1795
1796 return 1;
1797 }
1798
1799 /* Read the address map data from the mapped index, and use it to
1800 populate the objfile's psymtabs_addrmap. */
1801 static void
1802 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1803 {
1804 const gdb_byte *iter, *end;
1805 struct obstack temp_obstack;
1806 struct addrmap *mutable_map;
1807 struct cleanup *cleanup;
1808 CORE_ADDR baseaddr;
1809
1810 obstack_init (&temp_obstack);
1811 cleanup = make_cleanup_obstack_free (&temp_obstack);
1812 mutable_map = addrmap_create_mutable (&temp_obstack);
1813
1814 iter = index->address_table;
1815 end = iter + index->address_table_size;
1816
1817 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1818
1819 while (iter < end)
1820 {
1821 ULONGEST hi, lo, cu_index;
1822 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1823 iter += 8;
1824 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1825 iter += 8;
1826 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1827 iter += 4;
1828
1829 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1830 dw2_get_cu (cu_index));
1831 }
1832
1833 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1834 &objfile->objfile_obstack);
1835 do_cleanups (cleanup);
1836 }
1837
1838 /* The hash function for strings in the mapped index. This is the
1839 same as the hashtab.c hash function, but we keep a separate copy to
1840 maintain control over the implementation. This is necessary
1841 because the hash function is tied to the format of the mapped index
1842 file. */
1843 static hashval_t
1844 mapped_index_string_hash (const void *p)
1845 {
1846 const unsigned char *str = (const unsigned char *) p;
1847 hashval_t r = 0;
1848 unsigned char c;
1849
1850 while ((c = *str++) != 0)
1851 r = r * 67 + c - 113;
1852
1853 return r;
1854 }
1855
1856 /* Find a slot in the mapped index INDEX for the object named NAME.
1857 If NAME is found, set *VEC_OUT to point to the CU vector in the
1858 constant pool and return 1. If NAME cannot be found, return 0. */
1859 static int
1860 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1861 offset_type **vec_out)
1862 {
1863 offset_type hash = mapped_index_string_hash (name);
1864 offset_type slot, step;
1865
1866 slot = hash & (index->index_table_slots - 1);
1867 step = ((hash * 17) & (index->index_table_slots - 1)) | 1;
1868
1869 for (;;)
1870 {
1871 /* Convert a slot number to an offset into the table. */
1872 offset_type i = 2 * slot;
1873 const char *str;
1874 if (index->index_table[i] == 0 && index->index_table[i + 1] == 0)
1875 return 0;
1876
1877 str = index->constant_pool + MAYBE_SWAP (index->index_table[i]);
1878 if (!strcmp (name, str))
1879 {
1880 *vec_out = (offset_type *) (index->constant_pool
1881 + MAYBE_SWAP (index->index_table[i + 1]));
1882 return 1;
1883 }
1884
1885 slot = (slot + step) & (index->index_table_slots - 1);
1886 }
1887 }
1888
1889 /* Read the index file. If everything went ok, initialize the "quick"
1890 elements of all the CUs and return 1. Otherwise, return 0. */
1891 static int
1892 dwarf2_read_index (struct objfile *objfile)
1893 {
1894 char *addr;
1895 struct mapped_index *map;
1896 offset_type *metadata;
1897 const gdb_byte *cu_list;
1898 const gdb_byte *types_list = NULL;
1899 offset_type version, cu_list_elements;
1900 offset_type types_list_elements = 0;
1901 int i;
1902
1903 if (dwarf2_per_objfile->gdb_index.asection == NULL
1904 || dwarf2_per_objfile->gdb_index.size == 0)
1905 return 0;
1906
1907 /* Older elfutils strip versions could keep the section in the main
1908 executable while splitting it for the separate debug info file. */
1909 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1910 & SEC_HAS_CONTENTS) == 0)
1911 return 0;
1912
1913 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1914
1915 addr = dwarf2_per_objfile->gdb_index.buffer;
1916 /* Version check. */
1917 version = MAYBE_SWAP (*(offset_type *) addr);
1918 /* Versions earlier than 3 emitted every copy of a psymbol. This
1919 causes the index to behave very poorly for certain requests. So,
1920 it seems better to just ignore such indices. */
1921 if (version < 3)
1922 return 0;
1923
1924 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1925 map->total_size = dwarf2_per_objfile->gdb_index.size;
1926
1927 metadata = (offset_type *) (addr + sizeof (offset_type));
1928
1929 i = 0;
1930 cu_list = addr + MAYBE_SWAP (metadata[i]);
1931 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1932 / 8);
1933 ++i;
1934
1935 types_list = addr + MAYBE_SWAP (metadata[i]);
1936 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1937 - MAYBE_SWAP (metadata[i]))
1938 / 8);
1939 ++i;
1940
1941 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1942 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1943 - MAYBE_SWAP (metadata[i]));
1944 ++i;
1945
1946 map->index_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1947 map->index_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1948 - MAYBE_SWAP (metadata[i]))
1949 / (2 * sizeof (offset_type)));
1950 ++i;
1951
1952 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1953
1954 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1955 return 0;
1956
1957 if (types_list_elements
1958 && !create_signatured_type_table_from_index (objfile, types_list,
1959 types_list_elements))
1960 return 0;
1961
1962 create_addrmap_from_index (objfile, map);
1963
1964 dwarf2_per_objfile->index_table = map;
1965 dwarf2_per_objfile->using_index = 1;
1966
1967 return 1;
1968 }
1969
1970 /* A helper for the "quick" functions which sets the global
1971 dwarf2_per_objfile according to OBJFILE. */
1972 static void
1973 dw2_setup (struct objfile *objfile)
1974 {
1975 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1976 gdb_assert (dwarf2_per_objfile);
1977 }
1978
1979 /* A helper for the "quick" functions which attempts to read the line
1980 table for THIS_CU. */
1981 static void
1982 dw2_require_line_header (struct objfile *objfile,
1983 struct dwarf2_per_cu_data *this_cu)
1984 {
1985 bfd *abfd = objfile->obfd;
1986 struct line_header *lh = NULL;
1987 struct attribute *attr;
1988 struct cleanup *cleanups;
1989 struct die_info *comp_unit_die;
1990 struct dwarf2_section_info* sec;
1991 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
1992 int has_children, i;
1993 struct dwarf2_cu cu;
1994 unsigned int bytes_read, buffer_size;
1995 struct die_reader_specs reader_specs;
1996 char *name, *comp_dir;
1997
1998 if (this_cu->v.quick->read_lines)
1999 return;
2000 this_cu->v.quick->read_lines = 1;
2001
2002 memset (&cu, 0, sizeof (cu));
2003 cu.objfile = objfile;
2004 obstack_init (&cu.comp_unit_obstack);
2005
2006 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2007
2008 if (this_cu->from_debug_types)
2009 sec = &dwarf2_per_objfile->types;
2010 else
2011 sec = &dwarf2_per_objfile->info;
2012 dwarf2_read_section (objfile, sec);
2013 buffer_size = sec->size;
2014 buffer = sec->buffer;
2015 info_ptr = buffer + this_cu->offset;
2016 beg_of_comp_unit = info_ptr;
2017
2018 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2019 buffer, buffer_size,
2020 abfd);
2021
2022 /* Complete the cu_header. */
2023 cu.header.offset = beg_of_comp_unit - buffer;
2024 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2025
2026 this_cu->cu = &cu;
2027 cu.per_cu = this_cu;
2028
2029 dwarf2_read_abbrevs (abfd, &cu);
2030 make_cleanup (dwarf2_free_abbrev_table, &cu);
2031
2032 if (this_cu->from_debug_types)
2033 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2034 init_cu_die_reader (&reader_specs, &cu);
2035 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2036 &has_children);
2037
2038 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2039 if (attr)
2040 {
2041 unsigned int line_offset = DW_UNSND (attr);
2042 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2043 }
2044 if (lh == NULL)
2045 {
2046 do_cleanups (cleanups);
2047 return;
2048 }
2049
2050 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2051
2052 this_cu->v.quick->lines = lh;
2053
2054 this_cu->v.quick->file_names
2055 = obstack_alloc (&objfile->objfile_obstack,
2056 lh->num_file_names * sizeof (char *));
2057 for (i = 0; i < lh->num_file_names; ++i)
2058 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2059
2060 do_cleanups (cleanups);
2061 }
2062
2063 /* A helper for the "quick" functions which computes and caches the
2064 real path for a given file name from the line table.
2065 dw2_require_line_header must have been called before this is
2066 invoked. */
2067 static const char *
2068 dw2_require_full_path (struct objfile *objfile,
2069 struct dwarf2_per_cu_data *per_cu,
2070 int index)
2071 {
2072 if (!per_cu->v.quick->full_names)
2073 per_cu->v.quick->full_names
2074 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2075 per_cu->v.quick->lines->num_file_names,
2076 sizeof (char *));
2077
2078 if (!per_cu->v.quick->full_names[index])
2079 per_cu->v.quick->full_names[index]
2080 = gdb_realpath (per_cu->v.quick->file_names[index]);
2081
2082 return per_cu->v.quick->full_names[index];
2083 }
2084
2085 static struct symtab *
2086 dw2_find_last_source_symtab (struct objfile *objfile)
2087 {
2088 int index;
2089 dw2_setup (objfile);
2090 index = dwarf2_per_objfile->n_comp_units - 1;
2091 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2092 }
2093
2094 static void
2095 dw2_forget_cached_source_info (struct objfile *objfile)
2096 {
2097 int i;
2098
2099 dw2_setup (objfile);
2100 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2101 + dwarf2_per_objfile->n_type_comp_units); ++i)
2102 {
2103 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2104
2105 if (per_cu->v.quick->full_names)
2106 {
2107 int j;
2108
2109 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2110 xfree ((void *) per_cu->v.quick->full_names[j]);
2111 }
2112 }
2113 }
2114
2115 static int
2116 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2117 const char *full_path, const char *real_path,
2118 struct symtab **result)
2119 {
2120 int i;
2121 int check_basename = lbasename (name) == name;
2122 struct dwarf2_per_cu_data *base_cu = NULL;
2123
2124 dw2_setup (objfile);
2125 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2126 + dwarf2_per_objfile->n_type_comp_units); ++i)
2127 {
2128 int j;
2129 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2130
2131 if (per_cu->v.quick->symtab)
2132 continue;
2133
2134 dw2_require_line_header (objfile, per_cu);
2135 if (!per_cu->v.quick->lines)
2136 continue;
2137
2138 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2139 {
2140 const char *this_name = per_cu->v.quick->file_names[j];
2141
2142 if (FILENAME_CMP (name, this_name) == 0)
2143 {
2144 *result = dw2_instantiate_symtab (objfile, per_cu);
2145 return 1;
2146 }
2147
2148 if (check_basename && ! base_cu
2149 && FILENAME_CMP (lbasename (this_name), name) == 0)
2150 base_cu = per_cu;
2151
2152 if (full_path != NULL)
2153 {
2154 const char *this_full_name = dw2_require_full_path (objfile,
2155 per_cu, j);
2156
2157 if (this_full_name
2158 && FILENAME_CMP (full_path, this_full_name) == 0)
2159 {
2160 *result = dw2_instantiate_symtab (objfile, per_cu);
2161 return 1;
2162 }
2163 }
2164
2165 if (real_path != NULL)
2166 {
2167 const char *this_full_name = dw2_require_full_path (objfile,
2168 per_cu, j);
2169
2170 if (this_full_name != NULL)
2171 {
2172 char *rp = gdb_realpath (this_full_name);
2173 if (rp != NULL && FILENAME_CMP (real_path, rp) == 0)
2174 {
2175 xfree (rp);
2176 *result = dw2_instantiate_symtab (objfile, per_cu);
2177 return 1;
2178 }
2179 xfree (rp);
2180 }
2181 }
2182 }
2183 }
2184
2185 if (base_cu)
2186 {
2187 *result = dw2_instantiate_symtab (objfile, base_cu);
2188 return 1;
2189 }
2190
2191 return 0;
2192 }
2193
2194 static struct symtab *
2195 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2196 const char *name, domain_enum domain)
2197 {
2198 /* We do all the work in the pre_expand_symtabs_matching hook
2199 instead. */
2200 return NULL;
2201 }
2202
2203 /* A helper function that expands all symtabs that hold an object
2204 named NAME. */
2205 static void
2206 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2207 {
2208 dw2_setup (objfile);
2209
2210 if (dwarf2_per_objfile->index_table)
2211 {
2212 offset_type *vec;
2213
2214 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2215 name, &vec))
2216 {
2217 offset_type i, len = MAYBE_SWAP (*vec);
2218 for (i = 0; i < len; ++i)
2219 {
2220 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2221 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2222
2223 dw2_instantiate_symtab (objfile, per_cu);
2224 }
2225 }
2226 }
2227 }
2228
2229 static void
2230 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2231 int kind, const char *name,
2232 domain_enum domain)
2233 {
2234 dw2_do_expand_symtabs_matching (objfile, name);
2235 }
2236
2237 static void
2238 dw2_print_stats (struct objfile *objfile)
2239 {
2240 int i, count;
2241
2242 dw2_setup (objfile);
2243 count = 0;
2244 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2245 + dwarf2_per_objfile->n_type_comp_units); ++i)
2246 {
2247 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2248
2249 if (!per_cu->v.quick->symtab)
2250 ++count;
2251 }
2252 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2253 }
2254
2255 static void
2256 dw2_dump (struct objfile *objfile)
2257 {
2258 /* Nothing worth printing. */
2259 }
2260
2261 static void
2262 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2263 struct section_offsets *delta)
2264 {
2265 /* There's nothing to relocate here. */
2266 }
2267
2268 static void
2269 dw2_expand_symtabs_for_function (struct objfile *objfile,
2270 const char *func_name)
2271 {
2272 dw2_do_expand_symtabs_matching (objfile, func_name);
2273 }
2274
2275 static void
2276 dw2_expand_all_symtabs (struct objfile *objfile)
2277 {
2278 int i;
2279
2280 dw2_setup (objfile);
2281
2282 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2283 + dwarf2_per_objfile->n_type_comp_units); ++i)
2284 {
2285 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2286
2287 dw2_instantiate_symtab (objfile, per_cu);
2288 }
2289 }
2290
2291 static void
2292 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2293 const char *filename)
2294 {
2295 int i;
2296
2297 dw2_setup (objfile);
2298 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2299 + dwarf2_per_objfile->n_type_comp_units); ++i)
2300 {
2301 int j;
2302 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2303
2304 if (per_cu->v.quick->symtab)
2305 continue;
2306
2307 dw2_require_line_header (objfile, per_cu);
2308 if (!per_cu->v.quick->lines)
2309 continue;
2310
2311 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2312 {
2313 const char *this_name = per_cu->v.quick->file_names[j];
2314 if (strcmp (this_name, filename) == 0)
2315 {
2316 dw2_instantiate_symtab (objfile, per_cu);
2317 break;
2318 }
2319 }
2320 }
2321 }
2322
2323 static const char *
2324 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2325 {
2326 struct dwarf2_per_cu_data *per_cu;
2327 offset_type *vec;
2328
2329 dw2_setup (objfile);
2330
2331 if (!dwarf2_per_objfile->index_table)
2332 return NULL;
2333
2334 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2335 name, &vec))
2336 return NULL;
2337
2338 /* Note that this just looks at the very first one named NAME -- but
2339 actually we are looking for a function. find_main_filename
2340 should be rewritten so that it doesn't require a custom hook. It
2341 could just use the ordinary symbol tables. */
2342 /* vec[0] is the length, which must always be >0. */
2343 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2344
2345 dw2_require_line_header (objfile, per_cu);
2346 if (!per_cu->v.quick->lines)
2347 return NULL;
2348
2349 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
2350 }
2351
2352 static void
2353 dw2_map_ada_symtabs (struct objfile *objfile,
2354 int (*wild_match) (const char *, int, const char *),
2355 int (*is_name_suffix) (const char *),
2356 void (*callback) (struct objfile *,
2357 struct symtab *, void *),
2358 const char *name, int global,
2359 domain_enum namespace, int wild,
2360 void *data)
2361 {
2362 /* For now, we don't support Ada. Still the function can be called if the
2363 current language is Ada for a non-Ada objfile using GNU index. As Ada
2364 does not look for non-Ada symbols this function should just return. */
2365 }
2366
2367 static void
2368 dw2_expand_symtabs_matching (struct objfile *objfile,
2369 int (*file_matcher) (const char *, void *),
2370 int (*name_matcher) (const char *, void *),
2371 domain_enum kind,
2372 void *data)
2373 {
2374 int i;
2375 offset_type iter;
2376 struct mapped_index *index;
2377
2378 dw2_setup (objfile);
2379 if (!dwarf2_per_objfile->index_table)
2380 return;
2381 index = dwarf2_per_objfile->index_table;
2382
2383 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2384 + dwarf2_per_objfile->n_type_comp_units); ++i)
2385 {
2386 int j;
2387 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2388
2389 per_cu->v.quick->mark = 0;
2390 if (per_cu->v.quick->symtab)
2391 continue;
2392
2393 dw2_require_line_header (objfile, per_cu);
2394 if (!per_cu->v.quick->lines)
2395 continue;
2396
2397 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2398 {
2399 if (file_matcher (per_cu->v.quick->file_names[j], data))
2400 {
2401 per_cu->v.quick->mark = 1;
2402 break;
2403 }
2404 }
2405 }
2406
2407 for (iter = 0; iter < index->index_table_slots; ++iter)
2408 {
2409 offset_type idx = 2 * iter;
2410 const char *name;
2411 offset_type *vec, vec_len, vec_idx;
2412
2413 if (index->index_table[idx] == 0 && index->index_table[idx + 1] == 0)
2414 continue;
2415
2416 name = index->constant_pool + MAYBE_SWAP (index->index_table[idx]);
2417
2418 if (! (*name_matcher) (name, data))
2419 continue;
2420
2421 /* The name was matched, now expand corresponding CUs that were
2422 marked. */
2423 vec = (offset_type *) (index->constant_pool
2424 + MAYBE_SWAP (index->index_table[idx + 1]));
2425 vec_len = MAYBE_SWAP (vec[0]);
2426 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2427 {
2428 struct dwarf2_per_cu_data *per_cu;
2429
2430 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2431 if (per_cu->v.quick->mark)
2432 dw2_instantiate_symtab (objfile, per_cu);
2433 }
2434 }
2435 }
2436
2437 static struct symtab *
2438 dw2_find_pc_sect_symtab (struct objfile *objfile,
2439 struct minimal_symbol *msymbol,
2440 CORE_ADDR pc,
2441 struct obj_section *section,
2442 int warn_if_readin)
2443 {
2444 struct dwarf2_per_cu_data *data;
2445
2446 dw2_setup (objfile);
2447
2448 if (!objfile->psymtabs_addrmap)
2449 return NULL;
2450
2451 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2452 if (!data)
2453 return NULL;
2454
2455 if (warn_if_readin && data->v.quick->symtab)
2456 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2457 paddress (get_objfile_arch (objfile), pc));
2458
2459 return dw2_instantiate_symtab (objfile, data);
2460 }
2461
2462 static void
2463 dw2_map_symbol_names (struct objfile *objfile,
2464 void (*fun) (const char *, void *),
2465 void *data)
2466 {
2467 offset_type iter;
2468 struct mapped_index *index;
2469
2470 dw2_setup (objfile);
2471
2472 if (!dwarf2_per_objfile->index_table)
2473 return;
2474 index = dwarf2_per_objfile->index_table;
2475
2476 for (iter = 0; iter < index->index_table_slots; ++iter)
2477 {
2478 offset_type idx = 2 * iter;
2479 const char *name;
2480 offset_type *vec, vec_len, vec_idx;
2481
2482 if (index->index_table[idx] == 0 && index->index_table[idx + 1] == 0)
2483 continue;
2484
2485 name = (index->constant_pool + MAYBE_SWAP (index->index_table[idx]));
2486
2487 (*fun) (name, data);
2488 }
2489 }
2490
2491 static void
2492 dw2_map_symbol_filenames (struct objfile *objfile,
2493 void (*fun) (const char *, const char *, void *),
2494 void *data)
2495 {
2496 int i;
2497
2498 dw2_setup (objfile);
2499 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2500 + dwarf2_per_objfile->n_type_comp_units); ++i)
2501 {
2502 int j;
2503 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2504
2505 if (per_cu->v.quick->symtab)
2506 continue;
2507
2508 dw2_require_line_header (objfile, per_cu);
2509 if (!per_cu->v.quick->lines)
2510 continue;
2511
2512 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2513 {
2514 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2515 j);
2516 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
2517 }
2518 }
2519 }
2520
2521 static int
2522 dw2_has_symbols (struct objfile *objfile)
2523 {
2524 return 1;
2525 }
2526
2527 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2528 {
2529 dw2_has_symbols,
2530 dw2_find_last_source_symtab,
2531 dw2_forget_cached_source_info,
2532 dw2_lookup_symtab,
2533 dw2_lookup_symbol,
2534 dw2_pre_expand_symtabs_matching,
2535 dw2_print_stats,
2536 dw2_dump,
2537 dw2_relocate,
2538 dw2_expand_symtabs_for_function,
2539 dw2_expand_all_symtabs,
2540 dw2_expand_symtabs_with_filename,
2541 dw2_find_symbol_file,
2542 dw2_map_ada_symtabs,
2543 dw2_expand_symtabs_matching,
2544 dw2_find_pc_sect_symtab,
2545 dw2_map_symbol_names,
2546 dw2_map_symbol_filenames
2547 };
2548
2549 /* Initialize for reading DWARF for this objfile. Return 0 if this
2550 file will use psymtabs, or 1 if using the GNU index. */
2551
2552 int
2553 dwarf2_initialize_objfile (struct objfile *objfile)
2554 {
2555 /* If we're about to read full symbols, don't bother with the
2556 indices. In this case we also don't care if some other debug
2557 format is making psymtabs, because they are all about to be
2558 expanded anyway. */
2559 if ((objfile->flags & OBJF_READNOW))
2560 {
2561 int i;
2562
2563 dwarf2_per_objfile->using_index = 1;
2564 create_all_comp_units (objfile);
2565 create_debug_types_hash_table (objfile);
2566
2567 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2568 + dwarf2_per_objfile->n_type_comp_units); ++i)
2569 {
2570 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2571
2572 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2573 struct dwarf2_per_cu_quick_data);
2574 }
2575
2576 /* Return 1 so that gdb sees the "quick" functions. However,
2577 these functions will be no-ops because we will have expanded
2578 all symtabs. */
2579 return 1;
2580 }
2581
2582 if (dwarf2_read_index (objfile))
2583 return 1;
2584
2585 dwarf2_build_psymtabs (objfile);
2586 return 0;
2587 }
2588
2589 \f
2590
2591 /* Build a partial symbol table. */
2592
2593 void
2594 dwarf2_build_psymtabs (struct objfile *objfile)
2595 {
2596 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2597 {
2598 init_psymbol_list (objfile, 1024);
2599 }
2600
2601 dwarf2_build_psymtabs_hard (objfile);
2602 }
2603
2604 /* Return TRUE if OFFSET is within CU_HEADER. */
2605
2606 static inline int
2607 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2608 {
2609 unsigned int bottom = cu_header->offset;
2610 unsigned int top = (cu_header->offset
2611 + cu_header->length
2612 + cu_header->initial_length_size);
2613
2614 return (offset >= bottom && offset < top);
2615 }
2616
2617 /* Read in the comp unit header information from the debug_info at info_ptr.
2618 NOTE: This leaves members offset, first_die_offset to be filled in
2619 by the caller. */
2620
2621 static gdb_byte *
2622 read_comp_unit_head (struct comp_unit_head *cu_header,
2623 gdb_byte *info_ptr, bfd *abfd)
2624 {
2625 int signed_addr;
2626 unsigned int bytes_read;
2627
2628 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2629 cu_header->initial_length_size = bytes_read;
2630 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2631 info_ptr += bytes_read;
2632 cu_header->version = read_2_bytes (abfd, info_ptr);
2633 info_ptr += 2;
2634 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2635 &bytes_read);
2636 info_ptr += bytes_read;
2637 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2638 info_ptr += 1;
2639 signed_addr = bfd_get_sign_extend_vma (abfd);
2640 if (signed_addr < 0)
2641 internal_error (__FILE__, __LINE__,
2642 _("read_comp_unit_head: dwarf from non elf file"));
2643 cu_header->signed_addr_p = signed_addr;
2644
2645 return info_ptr;
2646 }
2647
2648 static gdb_byte *
2649 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2650 gdb_byte *buffer, unsigned int buffer_size,
2651 bfd *abfd)
2652 {
2653 gdb_byte *beg_of_comp_unit = info_ptr;
2654
2655 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2656
2657 if (header->version != 2 && header->version != 3 && header->version != 4)
2658 error (_("Dwarf Error: wrong version in compilation unit header "
2659 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2660 bfd_get_filename (abfd));
2661
2662 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2663 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2664 "(offset 0x%lx + 6) [in module %s]"),
2665 (long) header->abbrev_offset,
2666 (long) (beg_of_comp_unit - buffer),
2667 bfd_get_filename (abfd));
2668
2669 if (beg_of_comp_unit + header->length + header->initial_length_size
2670 > buffer + buffer_size)
2671 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2672 "(offset 0x%lx + 0) [in module %s]"),
2673 (long) header->length,
2674 (long) (beg_of_comp_unit - buffer),
2675 bfd_get_filename (abfd));
2676
2677 return info_ptr;
2678 }
2679
2680 /* Read in the types comp unit header information from .debug_types entry at
2681 types_ptr. The result is a pointer to one past the end of the header. */
2682
2683 static gdb_byte *
2684 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2685 ULONGEST *signature,
2686 gdb_byte *types_ptr, bfd *abfd)
2687 {
2688 gdb_byte *initial_types_ptr = types_ptr;
2689
2690 dwarf2_read_section (dwarf2_per_objfile->objfile,
2691 &dwarf2_per_objfile->types);
2692 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2693
2694 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2695
2696 *signature = read_8_bytes (abfd, types_ptr);
2697 types_ptr += 8;
2698 types_ptr += cu_header->offset_size;
2699 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2700
2701 return types_ptr;
2702 }
2703
2704 /* Allocate a new partial symtab for file named NAME and mark this new
2705 partial symtab as being an include of PST. */
2706
2707 static void
2708 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2709 struct objfile *objfile)
2710 {
2711 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2712
2713 subpst->section_offsets = pst->section_offsets;
2714 subpst->textlow = 0;
2715 subpst->texthigh = 0;
2716
2717 subpst->dependencies = (struct partial_symtab **)
2718 obstack_alloc (&objfile->objfile_obstack,
2719 sizeof (struct partial_symtab *));
2720 subpst->dependencies[0] = pst;
2721 subpst->number_of_dependencies = 1;
2722
2723 subpst->globals_offset = 0;
2724 subpst->n_global_syms = 0;
2725 subpst->statics_offset = 0;
2726 subpst->n_static_syms = 0;
2727 subpst->symtab = NULL;
2728 subpst->read_symtab = pst->read_symtab;
2729 subpst->readin = 0;
2730
2731 /* No private part is necessary for include psymtabs. This property
2732 can be used to differentiate between such include psymtabs and
2733 the regular ones. */
2734 subpst->read_symtab_private = NULL;
2735 }
2736
2737 /* Read the Line Number Program data and extract the list of files
2738 included by the source file represented by PST. Build an include
2739 partial symtab for each of these included files. */
2740
2741 static void
2742 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2743 struct die_info *die,
2744 struct partial_symtab *pst)
2745 {
2746 struct objfile *objfile = cu->objfile;
2747 bfd *abfd = objfile->obfd;
2748 struct line_header *lh = NULL;
2749 struct attribute *attr;
2750
2751 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2752 if (attr)
2753 {
2754 unsigned int line_offset = DW_UNSND (attr);
2755
2756 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2757 }
2758 if (lh == NULL)
2759 return; /* No linetable, so no includes. */
2760
2761 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2762 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2763
2764 free_line_header (lh);
2765 }
2766
2767 static hashval_t
2768 hash_type_signature (const void *item)
2769 {
2770 const struct signatured_type *type_sig = item;
2771
2772 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2773 return type_sig->signature;
2774 }
2775
2776 static int
2777 eq_type_signature (const void *item_lhs, const void *item_rhs)
2778 {
2779 const struct signatured_type *lhs = item_lhs;
2780 const struct signatured_type *rhs = item_rhs;
2781
2782 return lhs->signature == rhs->signature;
2783 }
2784
2785 /* Allocate a hash table for signatured types. */
2786
2787 static htab_t
2788 allocate_signatured_type_table (struct objfile *objfile)
2789 {
2790 return htab_create_alloc_ex (41,
2791 hash_type_signature,
2792 eq_type_signature,
2793 NULL,
2794 &objfile->objfile_obstack,
2795 hashtab_obstack_allocate,
2796 dummy_obstack_deallocate);
2797 }
2798
2799 /* A helper function to add a signatured type CU to a list. */
2800
2801 static int
2802 add_signatured_type_cu_to_list (void **slot, void *datum)
2803 {
2804 struct signatured_type *sigt = *slot;
2805 struct dwarf2_per_cu_data ***datap = datum;
2806
2807 **datap = &sigt->per_cu;
2808 ++*datap;
2809
2810 return 1;
2811 }
2812
2813 /* Create the hash table of all entries in the .debug_types section.
2814 The result is zero if there is an error (e.g. missing .debug_types section),
2815 otherwise non-zero. */
2816
2817 static int
2818 create_debug_types_hash_table (struct objfile *objfile)
2819 {
2820 gdb_byte *info_ptr;
2821 htab_t types_htab;
2822 struct dwarf2_per_cu_data **iter;
2823
2824 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2825 info_ptr = dwarf2_per_objfile->types.buffer;
2826
2827 if (info_ptr == NULL)
2828 {
2829 dwarf2_per_objfile->signatured_types = NULL;
2830 return 0;
2831 }
2832
2833 types_htab = allocate_signatured_type_table (objfile);
2834
2835 if (dwarf2_die_debug)
2836 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2837
2838 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2839 {
2840 unsigned int offset;
2841 unsigned int offset_size;
2842 unsigned int type_offset;
2843 unsigned int length, initial_length_size;
2844 unsigned short version;
2845 ULONGEST signature;
2846 struct signatured_type *type_sig;
2847 void **slot;
2848 gdb_byte *ptr = info_ptr;
2849
2850 offset = ptr - dwarf2_per_objfile->types.buffer;
2851
2852 /* We need to read the type's signature in order to build the hash
2853 table, but we don't need to read anything else just yet. */
2854
2855 /* Sanity check to ensure entire cu is present. */
2856 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2857 if (ptr + length + initial_length_size
2858 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2859 {
2860 complaint (&symfile_complaints,
2861 _("debug type entry runs off end of `.debug_types' section, ignored"));
2862 break;
2863 }
2864
2865 offset_size = initial_length_size == 4 ? 4 : 8;
2866 ptr += initial_length_size;
2867 version = bfd_get_16 (objfile->obfd, ptr);
2868 ptr += 2;
2869 ptr += offset_size; /* abbrev offset */
2870 ptr += 1; /* address size */
2871 signature = bfd_get_64 (objfile->obfd, ptr);
2872 ptr += 8;
2873 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2874
2875 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2876 memset (type_sig, 0, sizeof (*type_sig));
2877 type_sig->signature = signature;
2878 type_sig->offset = offset;
2879 type_sig->type_offset = type_offset;
2880 type_sig->per_cu.objfile = objfile;
2881 type_sig->per_cu.from_debug_types = 1;
2882
2883 slot = htab_find_slot (types_htab, type_sig, INSERT);
2884 gdb_assert (slot != NULL);
2885 *slot = type_sig;
2886
2887 if (dwarf2_die_debug)
2888 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2889 offset, phex (signature, sizeof (signature)));
2890
2891 info_ptr = info_ptr + initial_length_size + length;
2892 }
2893
2894 dwarf2_per_objfile->signatured_types = types_htab;
2895
2896 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2897 dwarf2_per_objfile->type_comp_units
2898 = obstack_alloc (&objfile->objfile_obstack,
2899 dwarf2_per_objfile->n_type_comp_units
2900 * sizeof (struct dwarf2_per_cu_data *));
2901 iter = &dwarf2_per_objfile->type_comp_units[0];
2902 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2903 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2904 == dwarf2_per_objfile->n_type_comp_units);
2905
2906 return 1;
2907 }
2908
2909 /* Lookup a signature based type.
2910 Returns NULL if SIG is not present in the table. */
2911
2912 static struct signatured_type *
2913 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2914 {
2915 struct signatured_type find_entry, *entry;
2916
2917 if (dwarf2_per_objfile->signatured_types == NULL)
2918 {
2919 complaint (&symfile_complaints,
2920 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2921 return 0;
2922 }
2923
2924 find_entry.signature = sig;
2925 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2926 return entry;
2927 }
2928
2929 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2930
2931 static void
2932 init_cu_die_reader (struct die_reader_specs *reader,
2933 struct dwarf2_cu *cu)
2934 {
2935 reader->abfd = cu->objfile->obfd;
2936 reader->cu = cu;
2937 if (cu->per_cu->from_debug_types)
2938 {
2939 gdb_assert (dwarf2_per_objfile->types.readin);
2940 reader->buffer = dwarf2_per_objfile->types.buffer;
2941 }
2942 else
2943 {
2944 gdb_assert (dwarf2_per_objfile->info.readin);
2945 reader->buffer = dwarf2_per_objfile->info.buffer;
2946 }
2947 }
2948
2949 /* Find the base address of the compilation unit for range lists and
2950 location lists. It will normally be specified by DW_AT_low_pc.
2951 In DWARF-3 draft 4, the base address could be overridden by
2952 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2953 compilation units with discontinuous ranges. */
2954
2955 static void
2956 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2957 {
2958 struct attribute *attr;
2959
2960 cu->base_known = 0;
2961 cu->base_address = 0;
2962
2963 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
2964 if (attr)
2965 {
2966 cu->base_address = DW_ADDR (attr);
2967 cu->base_known = 1;
2968 }
2969 else
2970 {
2971 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
2972 if (attr)
2973 {
2974 cu->base_address = DW_ADDR (attr);
2975 cu->base_known = 1;
2976 }
2977 }
2978 }
2979
2980 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
2981 to combine the common parts.
2982 Process a compilation unit for a psymtab.
2983 BUFFER is a pointer to the beginning of the dwarf section buffer,
2984 either .debug_info or debug_types.
2985 INFO_PTR is a pointer to the start of the CU.
2986 Returns a pointer to the next CU. */
2987
2988 static gdb_byte *
2989 process_psymtab_comp_unit (struct objfile *objfile,
2990 struct dwarf2_per_cu_data *this_cu,
2991 gdb_byte *buffer, gdb_byte *info_ptr,
2992 unsigned int buffer_size)
2993 {
2994 bfd *abfd = objfile->obfd;
2995 gdb_byte *beg_of_comp_unit = info_ptr;
2996 struct die_info *comp_unit_die;
2997 struct partial_symtab *pst;
2998 CORE_ADDR baseaddr;
2999 struct cleanup *back_to_inner;
3000 struct dwarf2_cu cu;
3001 int has_children, has_pc_info;
3002 struct attribute *attr;
3003 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3004 struct die_reader_specs reader_specs;
3005
3006 memset (&cu, 0, sizeof (cu));
3007 cu.objfile = objfile;
3008 obstack_init (&cu.comp_unit_obstack);
3009
3010 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3011
3012 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3013 buffer, buffer_size,
3014 abfd);
3015
3016 /* Complete the cu_header. */
3017 cu.header.offset = beg_of_comp_unit - buffer;
3018 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3019
3020 cu.list_in_scope = &file_symbols;
3021
3022 /* If this compilation unit was already read in, free the
3023 cached copy in order to read it in again. This is
3024 necessary because we skipped some symbols when we first
3025 read in the compilation unit (see load_partial_dies).
3026 This problem could be avoided, but the benefit is
3027 unclear. */
3028 if (this_cu->cu != NULL)
3029 free_one_cached_comp_unit (this_cu->cu);
3030
3031 /* Note that this is a pointer to our stack frame, being
3032 added to a global data structure. It will be cleaned up
3033 in free_stack_comp_unit when we finish with this
3034 compilation unit. */
3035 this_cu->cu = &cu;
3036 cu.per_cu = this_cu;
3037
3038 /* Read the abbrevs for this compilation unit into a table. */
3039 dwarf2_read_abbrevs (abfd, &cu);
3040 make_cleanup (dwarf2_free_abbrev_table, &cu);
3041
3042 /* Read the compilation unit die. */
3043 if (this_cu->from_debug_types)
3044 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3045 init_cu_die_reader (&reader_specs, &cu);
3046 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3047 &has_children);
3048
3049 if (this_cu->from_debug_types)
3050 {
3051 /* offset,length haven't been set yet for type units. */
3052 this_cu->offset = cu.header.offset;
3053 this_cu->length = cu.header.length + cu.header.initial_length_size;
3054 }
3055 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3056 {
3057 info_ptr = (beg_of_comp_unit + cu.header.length
3058 + cu.header.initial_length_size);
3059 do_cleanups (back_to_inner);
3060 return info_ptr;
3061 }
3062
3063 /* Set the language we're debugging. */
3064 attr = dwarf2_attr (comp_unit_die, DW_AT_language, &cu);
3065 if (attr)
3066 set_cu_language (DW_UNSND (attr), &cu);
3067 else
3068 set_cu_language (language_minimal, &cu);
3069
3070 /* Allocate a new partial symbol table structure. */
3071 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3072 pst = start_psymtab_common (objfile, objfile->section_offsets,
3073 (attr != NULL) ? DW_STRING (attr) : "",
3074 /* TEXTLOW and TEXTHIGH are set below. */
3075 0,
3076 objfile->global_psymbols.next,
3077 objfile->static_psymbols.next);
3078
3079 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3080 if (attr != NULL)
3081 pst->dirname = DW_STRING (attr);
3082
3083 pst->read_symtab_private = this_cu;
3084
3085 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3086
3087 /* Store the function that reads in the rest of the symbol table */
3088 pst->read_symtab = dwarf2_psymtab_to_symtab;
3089
3090 this_cu->v.psymtab = pst;
3091
3092 dwarf2_find_base_address (comp_unit_die, &cu);
3093
3094 /* Possibly set the default values of LOWPC and HIGHPC from
3095 `DW_AT_ranges'. */
3096 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3097 &best_highpc, &cu, pst);
3098 if (has_pc_info == 1 && best_lowpc < best_highpc)
3099 /* Store the contiguous range if it is not empty; it can be empty for
3100 CUs with no code. */
3101 addrmap_set_empty (objfile->psymtabs_addrmap,
3102 best_lowpc + baseaddr,
3103 best_highpc + baseaddr - 1, pst);
3104
3105 /* Check if comp unit has_children.
3106 If so, read the rest of the partial symbols from this comp unit.
3107 If not, there's no more debug_info for this comp unit. */
3108 if (has_children)
3109 {
3110 struct partial_die_info *first_die;
3111 CORE_ADDR lowpc, highpc;
3112
3113 lowpc = ((CORE_ADDR) -1);
3114 highpc = ((CORE_ADDR) 0);
3115
3116 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3117
3118 scan_partial_symbols (first_die, &lowpc, &highpc,
3119 ! has_pc_info, &cu);
3120
3121 /* If we didn't find a lowpc, set it to highpc to avoid
3122 complaints from `maint check'. */
3123 if (lowpc == ((CORE_ADDR) -1))
3124 lowpc = highpc;
3125
3126 /* If the compilation unit didn't have an explicit address range,
3127 then use the information extracted from its child dies. */
3128 if (! has_pc_info)
3129 {
3130 best_lowpc = lowpc;
3131 best_highpc = highpc;
3132 }
3133 }
3134 pst->textlow = best_lowpc + baseaddr;
3135 pst->texthigh = best_highpc + baseaddr;
3136
3137 pst->n_global_syms = objfile->global_psymbols.next -
3138 (objfile->global_psymbols.list + pst->globals_offset);
3139 pst->n_static_syms = objfile->static_psymbols.next -
3140 (objfile->static_psymbols.list + pst->statics_offset);
3141 sort_pst_symbols (pst);
3142
3143 info_ptr = (beg_of_comp_unit + cu.header.length
3144 + cu.header.initial_length_size);
3145
3146 if (this_cu->from_debug_types)
3147 {
3148 /* It's not clear we want to do anything with stmt lists here.
3149 Waiting to see what gcc ultimately does. */
3150 }
3151 else
3152 {
3153 /* Get the list of files included in the current compilation unit,
3154 and build a psymtab for each of them. */
3155 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3156 }
3157
3158 do_cleanups (back_to_inner);
3159
3160 return info_ptr;
3161 }
3162
3163 /* Traversal function for htab_traverse_noresize.
3164 Process one .debug_types comp-unit. */
3165
3166 static int
3167 process_type_comp_unit (void **slot, void *info)
3168 {
3169 struct signatured_type *entry = (struct signatured_type *) *slot;
3170 struct objfile *objfile = (struct objfile *) info;
3171 struct dwarf2_per_cu_data *this_cu;
3172
3173 this_cu = &entry->per_cu;
3174
3175 gdb_assert (dwarf2_per_objfile->types.readin);
3176 process_psymtab_comp_unit (objfile, this_cu,
3177 dwarf2_per_objfile->types.buffer,
3178 dwarf2_per_objfile->types.buffer + entry->offset,
3179 dwarf2_per_objfile->types.size);
3180
3181 return 1;
3182 }
3183
3184 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3185 Build partial symbol tables for the .debug_types comp-units. */
3186
3187 static void
3188 build_type_psymtabs (struct objfile *objfile)
3189 {
3190 if (! create_debug_types_hash_table (objfile))
3191 return;
3192
3193 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3194 process_type_comp_unit, objfile);
3195 }
3196
3197 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3198
3199 static void
3200 psymtabs_addrmap_cleanup (void *o)
3201 {
3202 struct objfile *objfile = o;
3203
3204 objfile->psymtabs_addrmap = NULL;
3205 }
3206
3207 /* Build the partial symbol table by doing a quick pass through the
3208 .debug_info and .debug_abbrev sections. */
3209
3210 static void
3211 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3212 {
3213 gdb_byte *info_ptr;
3214 struct cleanup *back_to, *addrmap_cleanup;
3215 struct obstack temp_obstack;
3216
3217 dwarf2_per_objfile->reading_partial_symbols = 1;
3218
3219 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3220 info_ptr = dwarf2_per_objfile->info.buffer;
3221
3222 /* Any cached compilation units will be linked by the per-objfile
3223 read_in_chain. Make sure to free them when we're done. */
3224 back_to = make_cleanup (free_cached_comp_units, NULL);
3225
3226 build_type_psymtabs (objfile);
3227
3228 create_all_comp_units (objfile);
3229
3230 /* Create a temporary address map on a temporary obstack. We later
3231 copy this to the final obstack. */
3232 obstack_init (&temp_obstack);
3233 make_cleanup_obstack_free (&temp_obstack);
3234 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3235 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3236
3237 /* Since the objects we're extracting from .debug_info vary in
3238 length, only the individual functions to extract them (like
3239 read_comp_unit_head and load_partial_die) can really know whether
3240 the buffer is large enough to hold another complete object.
3241
3242 At the moment, they don't actually check that. If .debug_info
3243 holds just one extra byte after the last compilation unit's dies,
3244 then read_comp_unit_head will happily read off the end of the
3245 buffer. read_partial_die is similarly casual. Those functions
3246 should be fixed.
3247
3248 For this loop condition, simply checking whether there's any data
3249 left at all should be sufficient. */
3250
3251 while (info_ptr < (dwarf2_per_objfile->info.buffer
3252 + dwarf2_per_objfile->info.size))
3253 {
3254 struct dwarf2_per_cu_data *this_cu;
3255
3256 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3257 objfile);
3258
3259 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3260 dwarf2_per_objfile->info.buffer,
3261 info_ptr,
3262 dwarf2_per_objfile->info.size);
3263 }
3264
3265 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3266 &objfile->objfile_obstack);
3267 discard_cleanups (addrmap_cleanup);
3268
3269 do_cleanups (back_to);
3270 }
3271
3272 /* Load the partial DIEs for a secondary CU into memory. */
3273
3274 static void
3275 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3276 struct objfile *objfile)
3277 {
3278 bfd *abfd = objfile->obfd;
3279 gdb_byte *info_ptr, *beg_of_comp_unit;
3280 struct die_info *comp_unit_die;
3281 struct dwarf2_cu *cu;
3282 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3283 struct attribute *attr;
3284 int has_children;
3285 struct die_reader_specs reader_specs;
3286 int read_cu = 0;
3287
3288 gdb_assert (! this_cu->from_debug_types);
3289
3290 gdb_assert (dwarf2_per_objfile->info.readin);
3291 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3292 beg_of_comp_unit = info_ptr;
3293
3294 if (this_cu->cu == NULL)
3295 {
3296 cu = alloc_one_comp_unit (objfile);
3297
3298 read_cu = 1;
3299
3300 /* If an error occurs while loading, release our storage. */
3301 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3302
3303 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3304 dwarf2_per_objfile->info.buffer,
3305 dwarf2_per_objfile->info.size,
3306 abfd);
3307
3308 /* Complete the cu_header. */
3309 cu->header.offset = this_cu->offset;
3310 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3311
3312 /* Link this compilation unit into the compilation unit tree. */
3313 this_cu->cu = cu;
3314 cu->per_cu = this_cu;
3315
3316 /* Link this CU into read_in_chain. */
3317 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3318 dwarf2_per_objfile->read_in_chain = this_cu;
3319 }
3320 else
3321 {
3322 cu = this_cu->cu;
3323 info_ptr += cu->header.first_die_offset;
3324 }
3325
3326 /* Read the abbrevs for this compilation unit into a table. */
3327 gdb_assert (cu->dwarf2_abbrevs == NULL);
3328 dwarf2_read_abbrevs (abfd, cu);
3329 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3330
3331 /* Read the compilation unit die. */
3332 init_cu_die_reader (&reader_specs, cu);
3333 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3334 &has_children);
3335
3336 /* Set the language we're debugging. */
3337 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
3338 if (attr)
3339 set_cu_language (DW_UNSND (attr), cu);
3340 else
3341 set_cu_language (language_minimal, cu);
3342
3343 /* Check if comp unit has_children.
3344 If so, read the rest of the partial symbols from this comp unit.
3345 If not, there's no more debug_info for this comp unit. */
3346 if (has_children)
3347 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3348
3349 do_cleanups (free_abbrevs_cleanup);
3350
3351 if (read_cu)
3352 {
3353 /* We've successfully allocated this compilation unit. Let our
3354 caller clean it up when finished with it. */
3355 discard_cleanups (free_cu_cleanup);
3356 }
3357 }
3358
3359 /* Create a list of all compilation units in OBJFILE. We do this only
3360 if an inter-comp-unit reference is found; presumably if there is one,
3361 there will be many, and one will occur early in the .debug_info section.
3362 So there's no point in building this list incrementally. */
3363
3364 static void
3365 create_all_comp_units (struct objfile *objfile)
3366 {
3367 int n_allocated;
3368 int n_comp_units;
3369 struct dwarf2_per_cu_data **all_comp_units;
3370 gdb_byte *info_ptr;
3371
3372 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3373 info_ptr = dwarf2_per_objfile->info.buffer;
3374
3375 n_comp_units = 0;
3376 n_allocated = 10;
3377 all_comp_units = xmalloc (n_allocated
3378 * sizeof (struct dwarf2_per_cu_data *));
3379
3380 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3381 {
3382 unsigned int length, initial_length_size;
3383 struct dwarf2_per_cu_data *this_cu;
3384 unsigned int offset;
3385
3386 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3387
3388 /* Read just enough information to find out where the next
3389 compilation unit is. */
3390 length = read_initial_length (objfile->obfd, info_ptr,
3391 &initial_length_size);
3392
3393 /* Save the compilation unit for later lookup. */
3394 this_cu = obstack_alloc (&objfile->objfile_obstack,
3395 sizeof (struct dwarf2_per_cu_data));
3396 memset (this_cu, 0, sizeof (*this_cu));
3397 this_cu->offset = offset;
3398 this_cu->length = length + initial_length_size;
3399 this_cu->objfile = objfile;
3400
3401 if (n_comp_units == n_allocated)
3402 {
3403 n_allocated *= 2;
3404 all_comp_units = xrealloc (all_comp_units,
3405 n_allocated
3406 * sizeof (struct dwarf2_per_cu_data *));
3407 }
3408 all_comp_units[n_comp_units++] = this_cu;
3409
3410 info_ptr = info_ptr + this_cu->length;
3411 }
3412
3413 dwarf2_per_objfile->all_comp_units
3414 = obstack_alloc (&objfile->objfile_obstack,
3415 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3416 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3417 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3418 xfree (all_comp_units);
3419 dwarf2_per_objfile->n_comp_units = n_comp_units;
3420 }
3421
3422 /* Process all loaded DIEs for compilation unit CU, starting at
3423 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3424 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3425 DW_AT_ranges). If NEED_PC is set, then this function will set
3426 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3427 and record the covered ranges in the addrmap. */
3428
3429 static void
3430 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3431 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3432 {
3433 struct partial_die_info *pdi;
3434
3435 /* Now, march along the PDI's, descending into ones which have
3436 interesting children but skipping the children of the other ones,
3437 until we reach the end of the compilation unit. */
3438
3439 pdi = first_die;
3440
3441 while (pdi != NULL)
3442 {
3443 fixup_partial_die (pdi, cu);
3444
3445 /* Anonymous namespaces or modules have no name but have interesting
3446 children, so we need to look at them. Ditto for anonymous
3447 enums. */
3448
3449 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3450 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3451 {
3452 switch (pdi->tag)
3453 {
3454 case DW_TAG_subprogram:
3455 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3456 break;
3457 case DW_TAG_constant:
3458 case DW_TAG_variable:
3459 case DW_TAG_typedef:
3460 case DW_TAG_union_type:
3461 if (!pdi->is_declaration)
3462 {
3463 add_partial_symbol (pdi, cu);
3464 }
3465 break;
3466 case DW_TAG_class_type:
3467 case DW_TAG_interface_type:
3468 case DW_TAG_structure_type:
3469 if (!pdi->is_declaration)
3470 {
3471 add_partial_symbol (pdi, cu);
3472 }
3473 break;
3474 case DW_TAG_enumeration_type:
3475 if (!pdi->is_declaration)
3476 add_partial_enumeration (pdi, cu);
3477 break;
3478 case DW_TAG_base_type:
3479 case DW_TAG_subrange_type:
3480 /* File scope base type definitions are added to the partial
3481 symbol table. */
3482 add_partial_symbol (pdi, cu);
3483 break;
3484 case DW_TAG_namespace:
3485 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3486 break;
3487 case DW_TAG_module:
3488 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3489 break;
3490 default:
3491 break;
3492 }
3493 }
3494
3495 /* If the die has a sibling, skip to the sibling. */
3496
3497 pdi = pdi->die_sibling;
3498 }
3499 }
3500
3501 /* Functions used to compute the fully scoped name of a partial DIE.
3502
3503 Normally, this is simple. For C++, the parent DIE's fully scoped
3504 name is concatenated with "::" and the partial DIE's name. For
3505 Java, the same thing occurs except that "." is used instead of "::".
3506 Enumerators are an exception; they use the scope of their parent
3507 enumeration type, i.e. the name of the enumeration type is not
3508 prepended to the enumerator.
3509
3510 There are two complexities. One is DW_AT_specification; in this
3511 case "parent" means the parent of the target of the specification,
3512 instead of the direct parent of the DIE. The other is compilers
3513 which do not emit DW_TAG_namespace; in this case we try to guess
3514 the fully qualified name of structure types from their members'
3515 linkage names. This must be done using the DIE's children rather
3516 than the children of any DW_AT_specification target. We only need
3517 to do this for structures at the top level, i.e. if the target of
3518 any DW_AT_specification (if any; otherwise the DIE itself) does not
3519 have a parent. */
3520
3521 /* Compute the scope prefix associated with PDI's parent, in
3522 compilation unit CU. The result will be allocated on CU's
3523 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3524 field. NULL is returned if no prefix is necessary. */
3525 static char *
3526 partial_die_parent_scope (struct partial_die_info *pdi,
3527 struct dwarf2_cu *cu)
3528 {
3529 char *grandparent_scope;
3530 struct partial_die_info *parent, *real_pdi;
3531
3532 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3533 then this means the parent of the specification DIE. */
3534
3535 real_pdi = pdi;
3536 while (real_pdi->has_specification)
3537 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3538
3539 parent = real_pdi->die_parent;
3540 if (parent == NULL)
3541 return NULL;
3542
3543 if (parent->scope_set)
3544 return parent->scope;
3545
3546 fixup_partial_die (parent, cu);
3547
3548 grandparent_scope = partial_die_parent_scope (parent, cu);
3549
3550 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3551 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3552 Work around this problem here. */
3553 if (cu->language == language_cplus
3554 && parent->tag == DW_TAG_namespace
3555 && strcmp (parent->name, "::") == 0
3556 && grandparent_scope == NULL)
3557 {
3558 parent->scope = NULL;
3559 parent->scope_set = 1;
3560 return NULL;
3561 }
3562
3563 if (parent->tag == DW_TAG_namespace
3564 || parent->tag == DW_TAG_module
3565 || parent->tag == DW_TAG_structure_type
3566 || parent->tag == DW_TAG_class_type
3567 || parent->tag == DW_TAG_interface_type
3568 || parent->tag == DW_TAG_union_type
3569 || parent->tag == DW_TAG_enumeration_type)
3570 {
3571 if (grandparent_scope == NULL)
3572 parent->scope = parent->name;
3573 else
3574 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3575 parent->name, 0, cu);
3576 }
3577 else if (parent->tag == DW_TAG_enumerator)
3578 /* Enumerators should not get the name of the enumeration as a prefix. */
3579 parent->scope = grandparent_scope;
3580 else
3581 {
3582 /* FIXME drow/2004-04-01: What should we be doing with
3583 function-local names? For partial symbols, we should probably be
3584 ignoring them. */
3585 complaint (&symfile_complaints,
3586 _("unhandled containing DIE tag %d for DIE at %d"),
3587 parent->tag, pdi->offset);
3588 parent->scope = grandparent_scope;
3589 }
3590
3591 parent->scope_set = 1;
3592 return parent->scope;
3593 }
3594
3595 /* Return the fully scoped name associated with PDI, from compilation unit
3596 CU. The result will be allocated with malloc. */
3597 static char *
3598 partial_die_full_name (struct partial_die_info *pdi,
3599 struct dwarf2_cu *cu)
3600 {
3601 char *parent_scope;
3602
3603 /* If this is a template instantiation, we can not work out the
3604 template arguments from partial DIEs. So, unfortunately, we have
3605 to go through the full DIEs. At least any work we do building
3606 types here will be reused if full symbols are loaded later. */
3607 if (pdi->has_template_arguments)
3608 {
3609 fixup_partial_die (pdi, cu);
3610
3611 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3612 {
3613 struct die_info *die;
3614 struct attribute attr;
3615 struct dwarf2_cu *ref_cu = cu;
3616
3617 attr.name = 0;
3618 attr.form = DW_FORM_ref_addr;
3619 attr.u.addr = pdi->offset;
3620 die = follow_die_ref (NULL, &attr, &ref_cu);
3621
3622 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3623 }
3624 }
3625
3626 parent_scope = partial_die_parent_scope (pdi, cu);
3627 if (parent_scope == NULL)
3628 return NULL;
3629 else
3630 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3631 }
3632
3633 static void
3634 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3635 {
3636 struct objfile *objfile = cu->objfile;
3637 CORE_ADDR addr = 0;
3638 char *actual_name = NULL;
3639 const struct partial_symbol *psym = NULL;
3640 CORE_ADDR baseaddr;
3641 int built_actual_name = 0;
3642
3643 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3644
3645 actual_name = partial_die_full_name (pdi, cu);
3646 if (actual_name)
3647 built_actual_name = 1;
3648
3649 if (actual_name == NULL)
3650 actual_name = pdi->name;
3651
3652 switch (pdi->tag)
3653 {
3654 case DW_TAG_subprogram:
3655 if (pdi->is_external || cu->language == language_ada)
3656 {
3657 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3658 of the global scope. But in Ada, we want to be able to access
3659 nested procedures globally. So all Ada subprograms are stored
3660 in the global scope. */
3661 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3662 mst_text, objfile); */
3663 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3664 built_actual_name,
3665 VAR_DOMAIN, LOC_BLOCK,
3666 &objfile->global_psymbols,
3667 0, pdi->lowpc + baseaddr,
3668 cu->language, objfile);
3669 }
3670 else
3671 {
3672 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3673 mst_file_text, objfile); */
3674 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3675 built_actual_name,
3676 VAR_DOMAIN, LOC_BLOCK,
3677 &objfile->static_psymbols,
3678 0, pdi->lowpc + baseaddr,
3679 cu->language, objfile);
3680 }
3681 break;
3682 case DW_TAG_constant:
3683 {
3684 struct psymbol_allocation_list *list;
3685
3686 if (pdi->is_external)
3687 list = &objfile->global_psymbols;
3688 else
3689 list = &objfile->static_psymbols;
3690 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3691 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3692 list, 0, 0, cu->language, objfile);
3693
3694 }
3695 break;
3696 case DW_TAG_variable:
3697 if (pdi->locdesc)
3698 addr = decode_locdesc (pdi->locdesc, cu);
3699
3700 if (pdi->locdesc
3701 && addr == 0
3702 && !dwarf2_per_objfile->has_section_at_zero)
3703 {
3704 /* A global or static variable may also have been stripped
3705 out by the linker if unused, in which case its address
3706 will be nullified; do not add such variables into partial
3707 symbol table then. */
3708 }
3709 else if (pdi->is_external)
3710 {
3711 /* Global Variable.
3712 Don't enter into the minimal symbol tables as there is
3713 a minimal symbol table entry from the ELF symbols already.
3714 Enter into partial symbol table if it has a location
3715 descriptor or a type.
3716 If the location descriptor is missing, new_symbol will create
3717 a LOC_UNRESOLVED symbol, the address of the variable will then
3718 be determined from the minimal symbol table whenever the variable
3719 is referenced.
3720 The address for the partial symbol table entry is not
3721 used by GDB, but it comes in handy for debugging partial symbol
3722 table building. */
3723
3724 if (pdi->locdesc || pdi->has_type)
3725 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3726 built_actual_name,
3727 VAR_DOMAIN, LOC_STATIC,
3728 &objfile->global_psymbols,
3729 0, addr + baseaddr,
3730 cu->language, objfile);
3731 }
3732 else
3733 {
3734 /* Static Variable. Skip symbols without location descriptors. */
3735 if (pdi->locdesc == NULL)
3736 {
3737 if (built_actual_name)
3738 xfree (actual_name);
3739 return;
3740 }
3741 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3742 mst_file_data, objfile); */
3743 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3744 built_actual_name,
3745 VAR_DOMAIN, LOC_STATIC,
3746 &objfile->static_psymbols,
3747 0, addr + baseaddr,
3748 cu->language, objfile);
3749 }
3750 break;
3751 case DW_TAG_typedef:
3752 case DW_TAG_base_type:
3753 case DW_TAG_subrange_type:
3754 add_psymbol_to_list (actual_name, strlen (actual_name),
3755 built_actual_name,
3756 VAR_DOMAIN, LOC_TYPEDEF,
3757 &objfile->static_psymbols,
3758 0, (CORE_ADDR) 0, cu->language, objfile);
3759 break;
3760 case DW_TAG_namespace:
3761 add_psymbol_to_list (actual_name, strlen (actual_name),
3762 built_actual_name,
3763 VAR_DOMAIN, LOC_TYPEDEF,
3764 &objfile->global_psymbols,
3765 0, (CORE_ADDR) 0, cu->language, objfile);
3766 break;
3767 case DW_TAG_class_type:
3768 case DW_TAG_interface_type:
3769 case DW_TAG_structure_type:
3770 case DW_TAG_union_type:
3771 case DW_TAG_enumeration_type:
3772 /* Skip external references. The DWARF standard says in the section
3773 about "Structure, Union, and Class Type Entries": "An incomplete
3774 structure, union or class type is represented by a structure,
3775 union or class entry that does not have a byte size attribute
3776 and that has a DW_AT_declaration attribute." */
3777 if (!pdi->has_byte_size && pdi->is_declaration)
3778 {
3779 if (built_actual_name)
3780 xfree (actual_name);
3781 return;
3782 }
3783
3784 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3785 static vs. global. */
3786 add_psymbol_to_list (actual_name, strlen (actual_name),
3787 built_actual_name,
3788 STRUCT_DOMAIN, LOC_TYPEDEF,
3789 (cu->language == language_cplus
3790 || cu->language == language_java)
3791 ? &objfile->global_psymbols
3792 : &objfile->static_psymbols,
3793 0, (CORE_ADDR) 0, cu->language, objfile);
3794
3795 break;
3796 case DW_TAG_enumerator:
3797 add_psymbol_to_list (actual_name, strlen (actual_name),
3798 built_actual_name,
3799 VAR_DOMAIN, LOC_CONST,
3800 (cu->language == language_cplus
3801 || cu->language == language_java)
3802 ? &objfile->global_psymbols
3803 : &objfile->static_psymbols,
3804 0, (CORE_ADDR) 0, cu->language, objfile);
3805 break;
3806 default:
3807 break;
3808 }
3809
3810 if (built_actual_name)
3811 xfree (actual_name);
3812 }
3813
3814 /* Read a partial die corresponding to a namespace; also, add a symbol
3815 corresponding to that namespace to the symbol table. NAMESPACE is
3816 the name of the enclosing namespace. */
3817
3818 static void
3819 add_partial_namespace (struct partial_die_info *pdi,
3820 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3821 int need_pc, struct dwarf2_cu *cu)
3822 {
3823 /* Add a symbol for the namespace. */
3824
3825 add_partial_symbol (pdi, cu);
3826
3827 /* Now scan partial symbols in that namespace. */
3828
3829 if (pdi->has_children)
3830 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3831 }
3832
3833 /* Read a partial die corresponding to a Fortran module. */
3834
3835 static void
3836 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3837 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3838 {
3839 /* Now scan partial symbols in that module. */
3840
3841 if (pdi->has_children)
3842 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3843 }
3844
3845 /* Read a partial die corresponding to a subprogram and create a partial
3846 symbol for that subprogram. When the CU language allows it, this
3847 routine also defines a partial symbol for each nested subprogram
3848 that this subprogram contains.
3849
3850 DIE my also be a lexical block, in which case we simply search
3851 recursively for suprograms defined inside that lexical block.
3852 Again, this is only performed when the CU language allows this
3853 type of definitions. */
3854
3855 static void
3856 add_partial_subprogram (struct partial_die_info *pdi,
3857 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3858 int need_pc, struct dwarf2_cu *cu)
3859 {
3860 if (pdi->tag == DW_TAG_subprogram)
3861 {
3862 if (pdi->has_pc_info)
3863 {
3864 if (pdi->lowpc < *lowpc)
3865 *lowpc = pdi->lowpc;
3866 if (pdi->highpc > *highpc)
3867 *highpc = pdi->highpc;
3868 if (need_pc)
3869 {
3870 CORE_ADDR baseaddr;
3871 struct objfile *objfile = cu->objfile;
3872
3873 baseaddr = ANOFFSET (objfile->section_offsets,
3874 SECT_OFF_TEXT (objfile));
3875 addrmap_set_empty (objfile->psymtabs_addrmap,
3876 pdi->lowpc + baseaddr,
3877 pdi->highpc - 1 + baseaddr,
3878 cu->per_cu->v.psymtab);
3879 }
3880 if (!pdi->is_declaration)
3881 /* Ignore subprogram DIEs that do not have a name, they are
3882 illegal. Do not emit a complaint at this point, we will
3883 do so when we convert this psymtab into a symtab. */
3884 if (pdi->name)
3885 add_partial_symbol (pdi, cu);
3886 }
3887 }
3888
3889 if (! pdi->has_children)
3890 return;
3891
3892 if (cu->language == language_ada)
3893 {
3894 pdi = pdi->die_child;
3895 while (pdi != NULL)
3896 {
3897 fixup_partial_die (pdi, cu);
3898 if (pdi->tag == DW_TAG_subprogram
3899 || pdi->tag == DW_TAG_lexical_block)
3900 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3901 pdi = pdi->die_sibling;
3902 }
3903 }
3904 }
3905
3906 /* See if we can figure out if the class lives in a namespace. We do
3907 this by looking for a member function; its demangled name will
3908 contain namespace info, if there is any. */
3909
3910 static void
3911 guess_structure_name (struct partial_die_info *struct_pdi,
3912 struct dwarf2_cu *cu)
3913 {
3914 if ((cu->language == language_cplus
3915 || cu->language == language_java)
3916 && cu->has_namespace_info == 0
3917 && struct_pdi->has_children)
3918 {
3919 /* NOTE: carlton/2003-10-07: Getting the info this way changes
3920 what template types look like, because the demangler
3921 frequently doesn't give the same name as the debug info. We
3922 could fix this by only using the demangled name to get the
3923 prefix (but see comment in read_structure_type). */
3924
3925 struct partial_die_info *real_pdi;
3926
3927 /* If this DIE (this DIE's specification, if any) has a parent, then
3928 we should not do this. We'll prepend the parent's fully qualified
3929 name when we create the partial symbol. */
3930
3931 real_pdi = struct_pdi;
3932 while (real_pdi->has_specification)
3933 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3934
3935 if (real_pdi->die_parent != NULL)
3936 return;
3937 }
3938 }
3939
3940 /* Read a partial die corresponding to an enumeration type. */
3941
3942 static void
3943 add_partial_enumeration (struct partial_die_info *enum_pdi,
3944 struct dwarf2_cu *cu)
3945 {
3946 struct partial_die_info *pdi;
3947
3948 if (enum_pdi->name != NULL)
3949 add_partial_symbol (enum_pdi, cu);
3950
3951 pdi = enum_pdi->die_child;
3952 while (pdi)
3953 {
3954 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3955 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3956 else
3957 add_partial_symbol (pdi, cu);
3958 pdi = pdi->die_sibling;
3959 }
3960 }
3961
3962 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3963 Return the corresponding abbrev, or NULL if the number is zero (indicating
3964 an empty DIE). In either case *BYTES_READ will be set to the length of
3965 the initial number. */
3966
3967 static struct abbrev_info *
3968 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3969 struct dwarf2_cu *cu)
3970 {
3971 bfd *abfd = cu->objfile->obfd;
3972 unsigned int abbrev_number;
3973 struct abbrev_info *abbrev;
3974
3975 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3976
3977 if (abbrev_number == 0)
3978 return NULL;
3979
3980 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3981 if (!abbrev)
3982 {
3983 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3984 bfd_get_filename (abfd));
3985 }
3986
3987 return abbrev;
3988 }
3989
3990 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3991 Returns a pointer to the end of a series of DIEs, terminated by an empty
3992 DIE. Any children of the skipped DIEs will also be skipped. */
3993
3994 static gdb_byte *
3995 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3996 {
3997 struct abbrev_info *abbrev;
3998 unsigned int bytes_read;
3999
4000 while (1)
4001 {
4002 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4003 if (abbrev == NULL)
4004 return info_ptr + bytes_read;
4005 else
4006 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4007 }
4008 }
4009
4010 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4011 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4012 abbrev corresponding to that skipped uleb128 should be passed in
4013 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4014 children. */
4015
4016 static gdb_byte *
4017 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4018 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4019 {
4020 unsigned int bytes_read;
4021 struct attribute attr;
4022 bfd *abfd = cu->objfile->obfd;
4023 unsigned int form, i;
4024
4025 for (i = 0; i < abbrev->num_attrs; i++)
4026 {
4027 /* The only abbrev we care about is DW_AT_sibling. */
4028 if (abbrev->attrs[i].name == DW_AT_sibling)
4029 {
4030 read_attribute (&attr, &abbrev->attrs[i],
4031 abfd, info_ptr, cu);
4032 if (attr.form == DW_FORM_ref_addr)
4033 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4034 else
4035 return buffer + dwarf2_get_ref_die_offset (&attr);
4036 }
4037
4038 /* If it isn't DW_AT_sibling, skip this attribute. */
4039 form = abbrev->attrs[i].form;
4040 skip_attribute:
4041 switch (form)
4042 {
4043 case DW_FORM_ref_addr:
4044 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4045 and later it is offset sized. */
4046 if (cu->header.version == 2)
4047 info_ptr += cu->header.addr_size;
4048 else
4049 info_ptr += cu->header.offset_size;
4050 break;
4051 case DW_FORM_addr:
4052 info_ptr += cu->header.addr_size;
4053 break;
4054 case DW_FORM_data1:
4055 case DW_FORM_ref1:
4056 case DW_FORM_flag:
4057 info_ptr += 1;
4058 break;
4059 case DW_FORM_flag_present:
4060 break;
4061 case DW_FORM_data2:
4062 case DW_FORM_ref2:
4063 info_ptr += 2;
4064 break;
4065 case DW_FORM_data4:
4066 case DW_FORM_ref4:
4067 info_ptr += 4;
4068 break;
4069 case DW_FORM_data8:
4070 case DW_FORM_ref8:
4071 case DW_FORM_sig8:
4072 info_ptr += 8;
4073 break;
4074 case DW_FORM_string:
4075 read_direct_string (abfd, info_ptr, &bytes_read);
4076 info_ptr += bytes_read;
4077 break;
4078 case DW_FORM_sec_offset:
4079 case DW_FORM_strp:
4080 info_ptr += cu->header.offset_size;
4081 break;
4082 case DW_FORM_exprloc:
4083 case DW_FORM_block:
4084 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4085 info_ptr += bytes_read;
4086 break;
4087 case DW_FORM_block1:
4088 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4089 break;
4090 case DW_FORM_block2:
4091 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4092 break;
4093 case DW_FORM_block4:
4094 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4095 break;
4096 case DW_FORM_sdata:
4097 case DW_FORM_udata:
4098 case DW_FORM_ref_udata:
4099 info_ptr = skip_leb128 (abfd, info_ptr);
4100 break;
4101 case DW_FORM_indirect:
4102 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4103 info_ptr += bytes_read;
4104 /* We need to continue parsing from here, so just go back to
4105 the top. */
4106 goto skip_attribute;
4107
4108 default:
4109 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4110 dwarf_form_name (form),
4111 bfd_get_filename (abfd));
4112 }
4113 }
4114
4115 if (abbrev->has_children)
4116 return skip_children (buffer, info_ptr, cu);
4117 else
4118 return info_ptr;
4119 }
4120
4121 /* Locate ORIG_PDI's sibling.
4122 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4123 in BUFFER. */
4124
4125 static gdb_byte *
4126 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4127 gdb_byte *buffer, gdb_byte *info_ptr,
4128 bfd *abfd, struct dwarf2_cu *cu)
4129 {
4130 /* Do we know the sibling already? */
4131
4132 if (orig_pdi->sibling)
4133 return orig_pdi->sibling;
4134
4135 /* Are there any children to deal with? */
4136
4137 if (!orig_pdi->has_children)
4138 return info_ptr;
4139
4140 /* Skip the children the long way. */
4141
4142 return skip_children (buffer, info_ptr, cu);
4143 }
4144
4145 /* Expand this partial symbol table into a full symbol table. */
4146
4147 static void
4148 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4149 {
4150 if (pst != NULL)
4151 {
4152 if (pst->readin)
4153 {
4154 warning (_("bug: psymtab for %s is already read in."), pst->filename);
4155 }
4156 else
4157 {
4158 if (info_verbose)
4159 {
4160 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4161 gdb_flush (gdb_stdout);
4162 }
4163
4164 /* Restore our global data. */
4165 dwarf2_per_objfile = objfile_data (pst->objfile,
4166 dwarf2_objfile_data_key);
4167
4168 /* If this psymtab is constructed from a debug-only objfile, the
4169 has_section_at_zero flag will not necessarily be correct. We
4170 can get the correct value for this flag by looking at the data
4171 associated with the (presumably stripped) associated objfile. */
4172 if (pst->objfile->separate_debug_objfile_backlink)
4173 {
4174 struct dwarf2_per_objfile *dpo_backlink
4175 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4176 dwarf2_objfile_data_key);
4177
4178 dwarf2_per_objfile->has_section_at_zero
4179 = dpo_backlink->has_section_at_zero;
4180 }
4181
4182 dwarf2_per_objfile->reading_partial_symbols = 0;
4183
4184 psymtab_to_symtab_1 (pst);
4185
4186 /* Finish up the debug error message. */
4187 if (info_verbose)
4188 printf_filtered (_("done.\n"));
4189 }
4190 }
4191 }
4192
4193 /* Add PER_CU to the queue. */
4194
4195 static void
4196 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4197 {
4198 struct dwarf2_queue_item *item;
4199
4200 per_cu->queued = 1;
4201 item = xmalloc (sizeof (*item));
4202 item->per_cu = per_cu;
4203 item->next = NULL;
4204
4205 if (dwarf2_queue == NULL)
4206 dwarf2_queue = item;
4207 else
4208 dwarf2_queue_tail->next = item;
4209
4210 dwarf2_queue_tail = item;
4211 }
4212
4213 /* Process the queue. */
4214
4215 static void
4216 process_queue (struct objfile *objfile)
4217 {
4218 struct dwarf2_queue_item *item, *next_item;
4219
4220 /* The queue starts out with one item, but following a DIE reference
4221 may load a new CU, adding it to the end of the queue. */
4222 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4223 {
4224 if (dwarf2_per_objfile->using_index
4225 ? !item->per_cu->v.quick->symtab
4226 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4227 process_full_comp_unit (item->per_cu);
4228
4229 item->per_cu->queued = 0;
4230 next_item = item->next;
4231 xfree (item);
4232 }
4233
4234 dwarf2_queue_tail = NULL;
4235 }
4236
4237 /* Free all allocated queue entries. This function only releases anything if
4238 an error was thrown; if the queue was processed then it would have been
4239 freed as we went along. */
4240
4241 static void
4242 dwarf2_release_queue (void *dummy)
4243 {
4244 struct dwarf2_queue_item *item, *last;
4245
4246 item = dwarf2_queue;
4247 while (item)
4248 {
4249 /* Anything still marked queued is likely to be in an
4250 inconsistent state, so discard it. */
4251 if (item->per_cu->queued)
4252 {
4253 if (item->per_cu->cu != NULL)
4254 free_one_cached_comp_unit (item->per_cu->cu);
4255 item->per_cu->queued = 0;
4256 }
4257
4258 last = item;
4259 item = item->next;
4260 xfree (last);
4261 }
4262
4263 dwarf2_queue = dwarf2_queue_tail = NULL;
4264 }
4265
4266 /* Read in full symbols for PST, and anything it depends on. */
4267
4268 static void
4269 psymtab_to_symtab_1 (struct partial_symtab *pst)
4270 {
4271 struct dwarf2_per_cu_data *per_cu;
4272 struct cleanup *back_to;
4273 int i;
4274
4275 for (i = 0; i < pst->number_of_dependencies; i++)
4276 if (!pst->dependencies[i]->readin)
4277 {
4278 /* Inform about additional files that need to be read in. */
4279 if (info_verbose)
4280 {
4281 /* FIXME: i18n: Need to make this a single string. */
4282 fputs_filtered (" ", gdb_stdout);
4283 wrap_here ("");
4284 fputs_filtered ("and ", gdb_stdout);
4285 wrap_here ("");
4286 printf_filtered ("%s...", pst->dependencies[i]->filename);
4287 wrap_here (""); /* Flush output */
4288 gdb_flush (gdb_stdout);
4289 }
4290 psymtab_to_symtab_1 (pst->dependencies[i]);
4291 }
4292
4293 per_cu = pst->read_symtab_private;
4294
4295 if (per_cu == NULL)
4296 {
4297 /* It's an include file, no symbols to read for it.
4298 Everything is in the parent symtab. */
4299 pst->readin = 1;
4300 return;
4301 }
4302
4303 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4304 }
4305
4306 /* Load the DIEs associated with PER_CU into memory. */
4307
4308 static void
4309 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4310 {
4311 bfd *abfd = objfile->obfd;
4312 struct dwarf2_cu *cu;
4313 unsigned int offset;
4314 gdb_byte *info_ptr, *beg_of_comp_unit;
4315 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4316 struct attribute *attr;
4317 int read_cu = 0;
4318
4319 gdb_assert (! per_cu->from_debug_types);
4320
4321 /* Set local variables from the partial symbol table info. */
4322 offset = per_cu->offset;
4323
4324 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4325 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4326 beg_of_comp_unit = info_ptr;
4327
4328 if (per_cu->cu == NULL)
4329 {
4330 cu = alloc_one_comp_unit (objfile);
4331
4332 read_cu = 1;
4333
4334 /* If an error occurs while loading, release our storage. */
4335 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4336
4337 /* Read in the comp_unit header. */
4338 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4339
4340 /* Complete the cu_header. */
4341 cu->header.offset = offset;
4342 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4343
4344 /* Read the abbrevs for this compilation unit. */
4345 dwarf2_read_abbrevs (abfd, cu);
4346 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4347
4348 /* Link this compilation unit into the compilation unit tree. */
4349 per_cu->cu = cu;
4350 cu->per_cu = per_cu;
4351
4352 /* Link this CU into read_in_chain. */
4353 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4354 dwarf2_per_objfile->read_in_chain = per_cu;
4355 }
4356 else
4357 {
4358 cu = per_cu->cu;
4359 info_ptr += cu->header.first_die_offset;
4360 }
4361
4362 cu->dies = read_comp_unit (info_ptr, cu);
4363
4364 /* We try not to read any attributes in this function, because not
4365 all objfiles needed for references have been loaded yet, and symbol
4366 table processing isn't initialized. But we have to set the CU language,
4367 or we won't be able to build types correctly. */
4368 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
4369 if (attr)
4370 set_cu_language (DW_UNSND (attr), cu);
4371 else
4372 set_cu_language (language_minimal, cu);
4373
4374 /* Similarly, if we do not read the producer, we can not apply
4375 producer-specific interpretation. */
4376 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4377 if (attr)
4378 cu->producer = DW_STRING (attr);
4379
4380 if (read_cu)
4381 {
4382 do_cleanups (free_abbrevs_cleanup);
4383
4384 /* We've successfully allocated this compilation unit. Let our
4385 caller clean it up when finished with it. */
4386 discard_cleanups (free_cu_cleanup);
4387 }
4388 }
4389
4390 /* Add a DIE to the delayed physname list. */
4391
4392 static void
4393 add_to_method_list (struct type *type, int fnfield_index, int index,
4394 const char *name, struct die_info *die,
4395 struct dwarf2_cu *cu)
4396 {
4397 struct delayed_method_info mi;
4398 mi.type = type;
4399 mi.fnfield_index = fnfield_index;
4400 mi.index = index;
4401 mi.name = name;
4402 mi.die = die;
4403 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4404 }
4405
4406 /* A cleanup for freeing the delayed method list. */
4407
4408 static void
4409 free_delayed_list (void *ptr)
4410 {
4411 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4412 if (cu->method_list != NULL)
4413 {
4414 VEC_free (delayed_method_info, cu->method_list);
4415 cu->method_list = NULL;
4416 }
4417 }
4418
4419 /* Compute the physnames of any methods on the CU's method list.
4420
4421 The computation of method physnames is delayed in order to avoid the
4422 (bad) condition that one of the method's formal parameters is of an as yet
4423 incomplete type. */
4424
4425 static void
4426 compute_delayed_physnames (struct dwarf2_cu *cu)
4427 {
4428 int i;
4429 struct delayed_method_info *mi;
4430 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4431 {
4432 char *physname;
4433 struct fn_fieldlist *fn_flp
4434 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4435 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4436 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4437 }
4438 }
4439
4440 /* Generate full symbol information for PST and CU, whose DIEs have
4441 already been loaded into memory. */
4442
4443 static void
4444 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4445 {
4446 struct dwarf2_cu *cu = per_cu->cu;
4447 struct objfile *objfile = per_cu->objfile;
4448 CORE_ADDR lowpc, highpc;
4449 struct symtab *symtab;
4450 struct cleanup *back_to, *delayed_list_cleanup;
4451 CORE_ADDR baseaddr;
4452
4453 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4454
4455 buildsym_init ();
4456 back_to = make_cleanup (really_free_pendings, NULL);
4457 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4458
4459 cu->list_in_scope = &file_symbols;
4460
4461 dwarf2_find_base_address (cu->dies, cu);
4462
4463 /* Do line number decoding in read_file_scope () */
4464 process_die (cu->dies, cu);
4465
4466 /* Now that we have processed all the DIEs in the CU, all the types
4467 should be complete, and it should now be safe to compute all of the
4468 physnames. */
4469 compute_delayed_physnames (cu);
4470 do_cleanups (delayed_list_cleanup);
4471
4472 /* Some compilers don't define a DW_AT_high_pc attribute for the
4473 compilation unit. If the DW_AT_high_pc is missing, synthesize
4474 it, by scanning the DIE's below the compilation unit. */
4475 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4476
4477 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4478
4479 /* Set symtab language to language from DW_AT_language.
4480 If the compilation is from a C file generated by language preprocessors,
4481 do not set the language if it was already deduced by start_subfile. */
4482 if (symtab != NULL
4483 && !(cu->language == language_c && symtab->language != language_c))
4484 {
4485 symtab->language = cu->language;
4486 }
4487
4488 if (dwarf2_per_objfile->using_index)
4489 per_cu->v.quick->symtab = symtab;
4490 else
4491 {
4492 struct partial_symtab *pst = per_cu->v.psymtab;
4493 pst->symtab = symtab;
4494 pst->readin = 1;
4495 }
4496
4497 do_cleanups (back_to);
4498 }
4499
4500 /* Process a die and its children. */
4501
4502 static void
4503 process_die (struct die_info *die, struct dwarf2_cu *cu)
4504 {
4505 switch (die->tag)
4506 {
4507 case DW_TAG_padding:
4508 break;
4509 case DW_TAG_compile_unit:
4510 read_file_scope (die, cu);
4511 break;
4512 case DW_TAG_type_unit:
4513 read_type_unit_scope (die, cu);
4514 break;
4515 case DW_TAG_subprogram:
4516 case DW_TAG_inlined_subroutine:
4517 read_func_scope (die, cu);
4518 break;
4519 case DW_TAG_lexical_block:
4520 case DW_TAG_try_block:
4521 case DW_TAG_catch_block:
4522 read_lexical_block_scope (die, cu);
4523 break;
4524 case DW_TAG_class_type:
4525 case DW_TAG_interface_type:
4526 case DW_TAG_structure_type:
4527 case DW_TAG_union_type:
4528 process_structure_scope (die, cu);
4529 break;
4530 case DW_TAG_enumeration_type:
4531 process_enumeration_scope (die, cu);
4532 break;
4533
4534 /* These dies have a type, but processing them does not create
4535 a symbol or recurse to process the children. Therefore we can
4536 read them on-demand through read_type_die. */
4537 case DW_TAG_subroutine_type:
4538 case DW_TAG_set_type:
4539 case DW_TAG_array_type:
4540 case DW_TAG_pointer_type:
4541 case DW_TAG_ptr_to_member_type:
4542 case DW_TAG_reference_type:
4543 case DW_TAG_string_type:
4544 break;
4545
4546 case DW_TAG_base_type:
4547 case DW_TAG_subrange_type:
4548 case DW_TAG_typedef:
4549 /* Add a typedef symbol for the type definition, if it has a
4550 DW_AT_name. */
4551 new_symbol (die, read_type_die (die, cu), cu);
4552 break;
4553 case DW_TAG_common_block:
4554 read_common_block (die, cu);
4555 break;
4556 case DW_TAG_common_inclusion:
4557 break;
4558 case DW_TAG_namespace:
4559 processing_has_namespace_info = 1;
4560 read_namespace (die, cu);
4561 break;
4562 case DW_TAG_module:
4563 processing_has_namespace_info = 1;
4564 read_module (die, cu);
4565 break;
4566 case DW_TAG_imported_declaration:
4567 case DW_TAG_imported_module:
4568 processing_has_namespace_info = 1;
4569 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4570 || cu->language != language_fortran))
4571 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4572 dwarf_tag_name (die->tag));
4573 read_import_statement (die, cu);
4574 break;
4575 default:
4576 new_symbol (die, NULL, cu);
4577 break;
4578 }
4579 }
4580
4581 /* A helper function for dwarf2_compute_name which determines whether DIE
4582 needs to have the name of the scope prepended to the name listed in the
4583 die. */
4584
4585 static int
4586 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4587 {
4588 struct attribute *attr;
4589
4590 switch (die->tag)
4591 {
4592 case DW_TAG_namespace:
4593 case DW_TAG_typedef:
4594 case DW_TAG_class_type:
4595 case DW_TAG_interface_type:
4596 case DW_TAG_structure_type:
4597 case DW_TAG_union_type:
4598 case DW_TAG_enumeration_type:
4599 case DW_TAG_enumerator:
4600 case DW_TAG_subprogram:
4601 case DW_TAG_member:
4602 return 1;
4603
4604 case DW_TAG_variable:
4605 case DW_TAG_constant:
4606 /* We only need to prefix "globally" visible variables. These include
4607 any variable marked with DW_AT_external or any variable that
4608 lives in a namespace. [Variables in anonymous namespaces
4609 require prefixing, but they are not DW_AT_external.] */
4610
4611 if (dwarf2_attr (die, DW_AT_specification, cu))
4612 {
4613 struct dwarf2_cu *spec_cu = cu;
4614
4615 return die_needs_namespace (die_specification (die, &spec_cu),
4616 spec_cu);
4617 }
4618
4619 attr = dwarf2_attr (die, DW_AT_external, cu);
4620 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4621 && die->parent->tag != DW_TAG_module)
4622 return 0;
4623 /* A variable in a lexical block of some kind does not need a
4624 namespace, even though in C++ such variables may be external
4625 and have a mangled name. */
4626 if (die->parent->tag == DW_TAG_lexical_block
4627 || die->parent->tag == DW_TAG_try_block
4628 || die->parent->tag == DW_TAG_catch_block
4629 || die->parent->tag == DW_TAG_subprogram)
4630 return 0;
4631 return 1;
4632
4633 default:
4634 return 0;
4635 }
4636 }
4637
4638 /* Retrieve the last character from a mem_file. */
4639
4640 static void
4641 do_ui_file_peek_last (void *object, const char *buffer, long length)
4642 {
4643 char *last_char_p = (char *) object;
4644
4645 if (length > 0)
4646 *last_char_p = buffer[length - 1];
4647 }
4648
4649 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4650 compute the physname for the object, which include a method's
4651 formal parameters (C++/Java) and return type (Java).
4652
4653 For Ada, return the DIE's linkage name rather than the fully qualified
4654 name. PHYSNAME is ignored..
4655
4656 The result is allocated on the objfile_obstack and canonicalized. */
4657
4658 static const char *
4659 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4660 int physname)
4661 {
4662 if (name == NULL)
4663 name = dwarf2_name (die, cu);
4664
4665 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4666 compute it by typename_concat inside GDB. */
4667 if (cu->language == language_ada
4668 || (cu->language == language_fortran && physname))
4669 {
4670 /* For Ada unit, we prefer the linkage name over the name, as
4671 the former contains the exported name, which the user expects
4672 to be able to reference. Ideally, we want the user to be able
4673 to reference this entity using either natural or linkage name,
4674 but we haven't started looking at this enhancement yet. */
4675 struct attribute *attr;
4676
4677 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4678 if (attr == NULL)
4679 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4680 if (attr && DW_STRING (attr))
4681 return DW_STRING (attr);
4682 }
4683
4684 /* These are the only languages we know how to qualify names in. */
4685 if (name != NULL
4686 && (cu->language == language_cplus || cu->language == language_java
4687 || cu->language == language_fortran))
4688 {
4689 if (die_needs_namespace (die, cu))
4690 {
4691 long length;
4692 char *prefix;
4693 struct ui_file *buf;
4694
4695 prefix = determine_prefix (die, cu);
4696 buf = mem_fileopen ();
4697 if (*prefix != '\0')
4698 {
4699 char *prefixed_name = typename_concat (NULL, prefix, name,
4700 physname, cu);
4701
4702 fputs_unfiltered (prefixed_name, buf);
4703 xfree (prefixed_name);
4704 }
4705 else
4706 fputs_unfiltered (name ? name : "", buf);
4707
4708 /* Template parameters may be specified in the DIE's DW_AT_name, or
4709 as children with DW_TAG_template_type_param or
4710 DW_TAG_value_type_param. If the latter, add them to the name
4711 here. If the name already has template parameters, then
4712 skip this step; some versions of GCC emit both, and
4713 it is more efficient to use the pre-computed name.
4714
4715 Something to keep in mind about this process: it is very
4716 unlikely, or in some cases downright impossible, to produce
4717 something that will match the mangled name of a function.
4718 If the definition of the function has the same debug info,
4719 we should be able to match up with it anyway. But fallbacks
4720 using the minimal symbol, for instance to find a method
4721 implemented in a stripped copy of libstdc++, will not work.
4722 If we do not have debug info for the definition, we will have to
4723 match them up some other way.
4724
4725 When we do name matching there is a related problem with function
4726 templates; two instantiated function templates are allowed to
4727 differ only by their return types, which we do not add here. */
4728
4729 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4730 {
4731 struct attribute *attr;
4732 struct die_info *child;
4733 int first = 1;
4734
4735 die->building_fullname = 1;
4736
4737 for (child = die->child; child != NULL; child = child->sibling)
4738 {
4739 struct type *type;
4740 long value;
4741 gdb_byte *bytes;
4742 struct dwarf2_locexpr_baton *baton;
4743 struct value *v;
4744
4745 if (child->tag != DW_TAG_template_type_param
4746 && child->tag != DW_TAG_template_value_param)
4747 continue;
4748
4749 if (first)
4750 {
4751 fputs_unfiltered ("<", buf);
4752 first = 0;
4753 }
4754 else
4755 fputs_unfiltered (", ", buf);
4756
4757 attr = dwarf2_attr (child, DW_AT_type, cu);
4758 if (attr == NULL)
4759 {
4760 complaint (&symfile_complaints,
4761 _("template parameter missing DW_AT_type"));
4762 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4763 continue;
4764 }
4765 type = die_type (child, cu);
4766
4767 if (child->tag == DW_TAG_template_type_param)
4768 {
4769 c_print_type (type, "", buf, -1, 0);
4770 continue;
4771 }
4772
4773 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4774 if (attr == NULL)
4775 {
4776 complaint (&symfile_complaints,
4777 _("template parameter missing DW_AT_const_value"));
4778 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4779 continue;
4780 }
4781
4782 dwarf2_const_value_attr (attr, type, name,
4783 &cu->comp_unit_obstack, cu,
4784 &value, &bytes, &baton);
4785
4786 if (TYPE_NOSIGN (type))
4787 /* GDB prints characters as NUMBER 'CHAR'. If that's
4788 changed, this can use value_print instead. */
4789 c_printchar (value, type, buf);
4790 else
4791 {
4792 struct value_print_options opts;
4793
4794 if (baton != NULL)
4795 v = dwarf2_evaluate_loc_desc (type, NULL,
4796 baton->data,
4797 baton->size,
4798 baton->per_cu);
4799 else if (bytes != NULL)
4800 {
4801 v = allocate_value (type);
4802 memcpy (value_contents_writeable (v), bytes,
4803 TYPE_LENGTH (type));
4804 }
4805 else
4806 v = value_from_longest (type, value);
4807
4808 /* Specify decimal so that we do not depend on the radix. */
4809 get_formatted_print_options (&opts, 'd');
4810 opts.raw = 1;
4811 value_print (v, buf, &opts);
4812 release_value (v);
4813 value_free (v);
4814 }
4815 }
4816
4817 die->building_fullname = 0;
4818
4819 if (!first)
4820 {
4821 /* Close the argument list, with a space if necessary
4822 (nested templates). */
4823 char last_char = '\0';
4824 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4825 if (last_char == '>')
4826 fputs_unfiltered (" >", buf);
4827 else
4828 fputs_unfiltered (">", buf);
4829 }
4830 }
4831
4832 /* For Java and C++ methods, append formal parameter type
4833 information, if PHYSNAME. */
4834
4835 if (physname && die->tag == DW_TAG_subprogram
4836 && (cu->language == language_cplus
4837 || cu->language == language_java))
4838 {
4839 struct type *type = read_type_die (die, cu);
4840
4841 c_type_print_args (type, buf, 0, cu->language);
4842
4843 if (cu->language == language_java)
4844 {
4845 /* For java, we must append the return type to method
4846 names. */
4847 if (die->tag == DW_TAG_subprogram)
4848 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4849 0, 0);
4850 }
4851 else if (cu->language == language_cplus)
4852 {
4853 /* Assume that an artificial first parameter is
4854 "this", but do not crash if it is not. RealView
4855 marks unnamed (and thus unused) parameters as
4856 artificial; there is no way to differentiate
4857 the two cases. */
4858 if (TYPE_NFIELDS (type) > 0
4859 && TYPE_FIELD_ARTIFICIAL (type, 0)
4860 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
4861 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4862 fputs_unfiltered (" const", buf);
4863 }
4864 }
4865
4866 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4867 &length);
4868 ui_file_delete (buf);
4869
4870 if (cu->language == language_cplus)
4871 {
4872 char *cname
4873 = dwarf2_canonicalize_name (name, cu,
4874 &cu->objfile->objfile_obstack);
4875
4876 if (cname != NULL)
4877 name = cname;
4878 }
4879 }
4880 }
4881
4882 return name;
4883 }
4884
4885 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4886 If scope qualifiers are appropriate they will be added. The result
4887 will be allocated on the objfile_obstack, or NULL if the DIE does
4888 not have a name. NAME may either be from a previous call to
4889 dwarf2_name or NULL.
4890
4891 The output string will be canonicalized (if C++/Java). */
4892
4893 static const char *
4894 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4895 {
4896 return dwarf2_compute_name (name, die, cu, 0);
4897 }
4898
4899 /* Construct a physname for the given DIE in CU. NAME may either be
4900 from a previous call to dwarf2_name or NULL. The result will be
4901 allocated on the objfile_objstack or NULL if the DIE does not have a
4902 name.
4903
4904 The output string will be canonicalized (if C++/Java). */
4905
4906 static const char *
4907 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4908 {
4909 return dwarf2_compute_name (name, die, cu, 1);
4910 }
4911
4912 /* Read the import statement specified by the given die and record it. */
4913
4914 static void
4915 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4916 {
4917 struct attribute *import_attr;
4918 struct die_info *imported_die;
4919 struct dwarf2_cu *imported_cu;
4920 const char *imported_name;
4921 const char *imported_name_prefix;
4922 const char *canonical_name;
4923 const char *import_alias;
4924 const char *imported_declaration = NULL;
4925 const char *import_prefix;
4926
4927 char *temp;
4928
4929 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4930 if (import_attr == NULL)
4931 {
4932 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4933 dwarf_tag_name (die->tag));
4934 return;
4935 }
4936
4937 imported_cu = cu;
4938 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4939 imported_name = dwarf2_name (imported_die, imported_cu);
4940 if (imported_name == NULL)
4941 {
4942 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4943
4944 The import in the following code:
4945 namespace A
4946 {
4947 typedef int B;
4948 }
4949
4950 int main ()
4951 {
4952 using A::B;
4953 B b;
4954 return b;
4955 }
4956
4957 ...
4958 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4959 <52> DW_AT_decl_file : 1
4960 <53> DW_AT_decl_line : 6
4961 <54> DW_AT_import : <0x75>
4962 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4963 <59> DW_AT_name : B
4964 <5b> DW_AT_decl_file : 1
4965 <5c> DW_AT_decl_line : 2
4966 <5d> DW_AT_type : <0x6e>
4967 ...
4968 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4969 <76> DW_AT_byte_size : 4
4970 <77> DW_AT_encoding : 5 (signed)
4971
4972 imports the wrong die ( 0x75 instead of 0x58 ).
4973 This case will be ignored until the gcc bug is fixed. */
4974 return;
4975 }
4976
4977 /* Figure out the local name after import. */
4978 import_alias = dwarf2_name (die, cu);
4979
4980 /* Figure out where the statement is being imported to. */
4981 import_prefix = determine_prefix (die, cu);
4982
4983 /* Figure out what the scope of the imported die is and prepend it
4984 to the name of the imported die. */
4985 imported_name_prefix = determine_prefix (imported_die, imported_cu);
4986
4987 if (imported_die->tag != DW_TAG_namespace
4988 && imported_die->tag != DW_TAG_module)
4989 {
4990 imported_declaration = imported_name;
4991 canonical_name = imported_name_prefix;
4992 }
4993 else if (strlen (imported_name_prefix) > 0)
4994 {
4995 temp = alloca (strlen (imported_name_prefix)
4996 + 2 + strlen (imported_name) + 1);
4997 strcpy (temp, imported_name_prefix);
4998 strcat (temp, "::");
4999 strcat (temp, imported_name);
5000 canonical_name = temp;
5001 }
5002 else
5003 canonical_name = imported_name;
5004
5005 cp_add_using_directive (import_prefix,
5006 canonical_name,
5007 import_alias,
5008 imported_declaration,
5009 &cu->objfile->objfile_obstack);
5010 }
5011
5012 static void
5013 initialize_cu_func_list (struct dwarf2_cu *cu)
5014 {
5015 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5016 }
5017
5018 static void
5019 free_cu_line_header (void *arg)
5020 {
5021 struct dwarf2_cu *cu = arg;
5022
5023 free_line_header (cu->line_header);
5024 cu->line_header = NULL;
5025 }
5026
5027 static void
5028 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5029 char **name, char **comp_dir)
5030 {
5031 struct attribute *attr;
5032
5033 *name = NULL;
5034 *comp_dir = NULL;
5035
5036 /* Find the filename. Do not use dwarf2_name here, since the filename
5037 is not a source language identifier. */
5038 attr = dwarf2_attr (die, DW_AT_name, cu);
5039 if (attr)
5040 {
5041 *name = DW_STRING (attr);
5042 }
5043
5044 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5045 if (attr)
5046 *comp_dir = DW_STRING (attr);
5047 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5048 {
5049 *comp_dir = ldirname (*name);
5050 if (*comp_dir != NULL)
5051 make_cleanup (xfree, *comp_dir);
5052 }
5053 if (*comp_dir != NULL)
5054 {
5055 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5056 directory, get rid of it. */
5057 char *cp = strchr (*comp_dir, ':');
5058
5059 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5060 *comp_dir = cp + 1;
5061 }
5062
5063 if (*name == NULL)
5064 *name = "<unknown>";
5065 }
5066
5067 static void
5068 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5069 {
5070 struct objfile *objfile = cu->objfile;
5071 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5072 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5073 CORE_ADDR highpc = ((CORE_ADDR) 0);
5074 struct attribute *attr;
5075 char *name = NULL;
5076 char *comp_dir = NULL;
5077 struct die_info *child_die;
5078 bfd *abfd = objfile->obfd;
5079 struct line_header *line_header = 0;
5080 CORE_ADDR baseaddr;
5081
5082 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5083
5084 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5085
5086 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5087 from finish_block. */
5088 if (lowpc == ((CORE_ADDR) -1))
5089 lowpc = highpc;
5090 lowpc += baseaddr;
5091 highpc += baseaddr;
5092
5093 find_file_and_directory (die, cu, &name, &comp_dir);
5094
5095 attr = dwarf2_attr (die, DW_AT_language, cu);
5096 if (attr)
5097 {
5098 set_cu_language (DW_UNSND (attr), cu);
5099 }
5100
5101 attr = dwarf2_attr (die, DW_AT_producer, cu);
5102 if (attr)
5103 cu->producer = DW_STRING (attr);
5104
5105 /* We assume that we're processing GCC output. */
5106 processing_gcc_compilation = 2;
5107
5108 processing_has_namespace_info = 0;
5109
5110 start_symtab (name, comp_dir, lowpc);
5111 record_debugformat ("DWARF 2");
5112 record_producer (cu->producer);
5113
5114 initialize_cu_func_list (cu);
5115
5116 /* Decode line number information if present. We do this before
5117 processing child DIEs, so that the line header table is available
5118 for DW_AT_decl_file. */
5119 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5120 if (attr)
5121 {
5122 unsigned int line_offset = DW_UNSND (attr);
5123 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5124 if (line_header)
5125 {
5126 cu->line_header = line_header;
5127 make_cleanup (free_cu_line_header, cu);
5128 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5129 }
5130 }
5131
5132 /* Process all dies in compilation unit. */
5133 if (die->child != NULL)
5134 {
5135 child_die = die->child;
5136 while (child_die && child_die->tag)
5137 {
5138 process_die (child_die, cu);
5139 child_die = sibling_die (child_die);
5140 }
5141 }
5142
5143 /* Decode macro information, if present. Dwarf 2 macro information
5144 refers to information in the line number info statement program
5145 header, so we can only read it if we've read the header
5146 successfully. */
5147 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5148 if (attr && line_header)
5149 {
5150 unsigned int macro_offset = DW_UNSND (attr);
5151
5152 dwarf_decode_macros (line_header, macro_offset,
5153 comp_dir, abfd, cu);
5154 }
5155 do_cleanups (back_to);
5156 }
5157
5158 /* For TUs we want to skip the first top level sibling if it's not the
5159 actual type being defined by this TU. In this case the first top
5160 level sibling is there to provide context only. */
5161
5162 static void
5163 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5164 {
5165 struct objfile *objfile = cu->objfile;
5166 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5167 CORE_ADDR lowpc;
5168 struct attribute *attr;
5169 char *name = NULL;
5170 char *comp_dir = NULL;
5171 struct die_info *child_die;
5172 bfd *abfd = objfile->obfd;
5173
5174 /* start_symtab needs a low pc, but we don't really have one.
5175 Do what read_file_scope would do in the absence of such info. */
5176 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5177
5178 /* Find the filename. Do not use dwarf2_name here, since the filename
5179 is not a source language identifier. */
5180 attr = dwarf2_attr (die, DW_AT_name, cu);
5181 if (attr)
5182 name = DW_STRING (attr);
5183
5184 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5185 if (attr)
5186 comp_dir = DW_STRING (attr);
5187 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5188 {
5189 comp_dir = ldirname (name);
5190 if (comp_dir != NULL)
5191 make_cleanup (xfree, comp_dir);
5192 }
5193
5194 if (name == NULL)
5195 name = "<unknown>";
5196
5197 attr = dwarf2_attr (die, DW_AT_language, cu);
5198 if (attr)
5199 set_cu_language (DW_UNSND (attr), cu);
5200
5201 /* This isn't technically needed today. It is done for symmetry
5202 with read_file_scope. */
5203 attr = dwarf2_attr (die, DW_AT_producer, cu);
5204 if (attr)
5205 cu->producer = DW_STRING (attr);
5206
5207 /* We assume that we're processing GCC output. */
5208 processing_gcc_compilation = 2;
5209
5210 processing_has_namespace_info = 0;
5211
5212 start_symtab (name, comp_dir, lowpc);
5213 record_debugformat ("DWARF 2");
5214 record_producer (cu->producer);
5215
5216 /* Process the dies in the type unit. */
5217 if (die->child == NULL)
5218 {
5219 dump_die_for_error (die);
5220 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5221 bfd_get_filename (abfd));
5222 }
5223
5224 child_die = die->child;
5225
5226 while (child_die && child_die->tag)
5227 {
5228 process_die (child_die, cu);
5229
5230 child_die = sibling_die (child_die);
5231 }
5232
5233 do_cleanups (back_to);
5234 }
5235
5236 static void
5237 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5238 struct dwarf2_cu *cu)
5239 {
5240 struct function_range *thisfn;
5241
5242 thisfn = (struct function_range *)
5243 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5244 thisfn->name = name;
5245 thisfn->lowpc = lowpc;
5246 thisfn->highpc = highpc;
5247 thisfn->seen_line = 0;
5248 thisfn->next = NULL;
5249
5250 if (cu->last_fn == NULL)
5251 cu->first_fn = thisfn;
5252 else
5253 cu->last_fn->next = thisfn;
5254
5255 cu->last_fn = thisfn;
5256 }
5257
5258 /* qsort helper for inherit_abstract_dies. */
5259
5260 static int
5261 unsigned_int_compar (const void *ap, const void *bp)
5262 {
5263 unsigned int a = *(unsigned int *) ap;
5264 unsigned int b = *(unsigned int *) bp;
5265
5266 return (a > b) - (b > a);
5267 }
5268
5269 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5270 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5271 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5272
5273 static void
5274 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5275 {
5276 struct die_info *child_die;
5277 unsigned die_children_count;
5278 /* CU offsets which were referenced by children of the current DIE. */
5279 unsigned *offsets;
5280 unsigned *offsets_end, *offsetp;
5281 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5282 struct die_info *origin_die;
5283 /* Iterator of the ORIGIN_DIE children. */
5284 struct die_info *origin_child_die;
5285 struct cleanup *cleanups;
5286 struct attribute *attr;
5287 struct dwarf2_cu *origin_cu;
5288 struct pending **origin_previous_list_in_scope;
5289
5290 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5291 if (!attr)
5292 return;
5293
5294 /* Note that following die references may follow to a die in a
5295 different cu. */
5296
5297 origin_cu = cu;
5298 origin_die = follow_die_ref (die, attr, &origin_cu);
5299
5300 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5301 symbols in. */
5302 origin_previous_list_in_scope = origin_cu->list_in_scope;
5303 origin_cu->list_in_scope = cu->list_in_scope;
5304
5305 if (die->tag != origin_die->tag
5306 && !(die->tag == DW_TAG_inlined_subroutine
5307 && origin_die->tag == DW_TAG_subprogram))
5308 complaint (&symfile_complaints,
5309 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5310 die->offset, origin_die->offset);
5311
5312 child_die = die->child;
5313 die_children_count = 0;
5314 while (child_die && child_die->tag)
5315 {
5316 child_die = sibling_die (child_die);
5317 die_children_count++;
5318 }
5319 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5320 cleanups = make_cleanup (xfree, offsets);
5321
5322 offsets_end = offsets;
5323 child_die = die->child;
5324 while (child_die && child_die->tag)
5325 {
5326 /* For each CHILD_DIE, find the corresponding child of
5327 ORIGIN_DIE. If there is more than one layer of
5328 DW_AT_abstract_origin, follow them all; there shouldn't be,
5329 but GCC versions at least through 4.4 generate this (GCC PR
5330 40573). */
5331 struct die_info *child_origin_die = child_die;
5332 struct dwarf2_cu *child_origin_cu = cu;
5333
5334 while (1)
5335 {
5336 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5337 child_origin_cu);
5338 if (attr == NULL)
5339 break;
5340 child_origin_die = follow_die_ref (child_origin_die, attr,
5341 &child_origin_cu);
5342 }
5343
5344 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5345 counterpart may exist. */
5346 if (child_origin_die != child_die)
5347 {
5348 if (child_die->tag != child_origin_die->tag
5349 && !(child_die->tag == DW_TAG_inlined_subroutine
5350 && child_origin_die->tag == DW_TAG_subprogram))
5351 complaint (&symfile_complaints,
5352 _("Child DIE 0x%x and its abstract origin 0x%x have "
5353 "different tags"), child_die->offset,
5354 child_origin_die->offset);
5355 if (child_origin_die->parent != origin_die)
5356 complaint (&symfile_complaints,
5357 _("Child DIE 0x%x and its abstract origin 0x%x have "
5358 "different parents"), child_die->offset,
5359 child_origin_die->offset);
5360 else
5361 *offsets_end++ = child_origin_die->offset;
5362 }
5363 child_die = sibling_die (child_die);
5364 }
5365 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5366 unsigned_int_compar);
5367 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5368 if (offsetp[-1] == *offsetp)
5369 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5370 "to DIE 0x%x as their abstract origin"),
5371 die->offset, *offsetp);
5372
5373 offsetp = offsets;
5374 origin_child_die = origin_die->child;
5375 while (origin_child_die && origin_child_die->tag)
5376 {
5377 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5378 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5379 offsetp++;
5380 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5381 {
5382 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5383 process_die (origin_child_die, origin_cu);
5384 }
5385 origin_child_die = sibling_die (origin_child_die);
5386 }
5387 origin_cu->list_in_scope = origin_previous_list_in_scope;
5388
5389 do_cleanups (cleanups);
5390 }
5391
5392 static void
5393 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5394 {
5395 struct objfile *objfile = cu->objfile;
5396 struct context_stack *new;
5397 CORE_ADDR lowpc;
5398 CORE_ADDR highpc;
5399 struct die_info *child_die;
5400 struct attribute *attr, *call_line, *call_file;
5401 char *name;
5402 CORE_ADDR baseaddr;
5403 struct block *block;
5404 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5405 VEC (symbolp) *template_args = NULL;
5406 struct template_symbol *templ_func = NULL;
5407
5408 if (inlined_func)
5409 {
5410 /* If we do not have call site information, we can't show the
5411 caller of this inlined function. That's too confusing, so
5412 only use the scope for local variables. */
5413 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5414 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5415 if (call_line == NULL || call_file == NULL)
5416 {
5417 read_lexical_block_scope (die, cu);
5418 return;
5419 }
5420 }
5421
5422 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5423
5424 name = dwarf2_name (die, cu);
5425
5426 /* Ignore functions with missing or empty names. These are actually
5427 illegal according to the DWARF standard. */
5428 if (name == NULL)
5429 {
5430 complaint (&symfile_complaints,
5431 _("missing name for subprogram DIE at %d"), die->offset);
5432 return;
5433 }
5434
5435 /* Ignore functions with missing or invalid low and high pc attributes. */
5436 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5437 {
5438 attr = dwarf2_attr (die, DW_AT_external, cu);
5439 if (!attr || !DW_UNSND (attr))
5440 complaint (&symfile_complaints,
5441 _("cannot get low and high bounds for subprogram DIE at %d"),
5442 die->offset);
5443 return;
5444 }
5445
5446 lowpc += baseaddr;
5447 highpc += baseaddr;
5448
5449 /* Record the function range for dwarf_decode_lines. */
5450 add_to_cu_func_list (name, lowpc, highpc, cu);
5451
5452 /* If we have any template arguments, then we must allocate a
5453 different sort of symbol. */
5454 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5455 {
5456 if (child_die->tag == DW_TAG_template_type_param
5457 || child_die->tag == DW_TAG_template_value_param)
5458 {
5459 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5460 struct template_symbol);
5461 templ_func->base.is_cplus_template_function = 1;
5462 break;
5463 }
5464 }
5465
5466 new = push_context (0, lowpc);
5467 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5468 (struct symbol *) templ_func);
5469
5470 /* If there is a location expression for DW_AT_frame_base, record
5471 it. */
5472 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5473 if (attr)
5474 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5475 expression is being recorded directly in the function's symbol
5476 and not in a separate frame-base object. I guess this hack is
5477 to avoid adding some sort of frame-base adjunct/annex to the
5478 function's symbol :-(. The problem with doing this is that it
5479 results in a function symbol with a location expression that
5480 has nothing to do with the location of the function, ouch! The
5481 relationship should be: a function's symbol has-a frame base; a
5482 frame-base has-a location expression. */
5483 dwarf2_symbol_mark_computed (attr, new->name, cu);
5484
5485 cu->list_in_scope = &local_symbols;
5486
5487 if (die->child != NULL)
5488 {
5489 child_die = die->child;
5490 while (child_die && child_die->tag)
5491 {
5492 if (child_die->tag == DW_TAG_template_type_param
5493 || child_die->tag == DW_TAG_template_value_param)
5494 {
5495 struct symbol *arg = new_symbol (child_die, NULL, cu);
5496
5497 if (arg != NULL)
5498 VEC_safe_push (symbolp, template_args, arg);
5499 }
5500 else
5501 process_die (child_die, cu);
5502 child_die = sibling_die (child_die);
5503 }
5504 }
5505
5506 inherit_abstract_dies (die, cu);
5507
5508 /* If we have a DW_AT_specification, we might need to import using
5509 directives from the context of the specification DIE. See the
5510 comment in determine_prefix. */
5511 if (cu->language == language_cplus
5512 && dwarf2_attr (die, DW_AT_specification, cu))
5513 {
5514 struct dwarf2_cu *spec_cu = cu;
5515 struct die_info *spec_die = die_specification (die, &spec_cu);
5516
5517 while (spec_die)
5518 {
5519 child_die = spec_die->child;
5520 while (child_die && child_die->tag)
5521 {
5522 if (child_die->tag == DW_TAG_imported_module)
5523 process_die (child_die, spec_cu);
5524 child_die = sibling_die (child_die);
5525 }
5526
5527 /* In some cases, GCC generates specification DIEs that
5528 themselves contain DW_AT_specification attributes. */
5529 spec_die = die_specification (spec_die, &spec_cu);
5530 }
5531 }
5532
5533 new = pop_context ();
5534 /* Make a block for the local symbols within. */
5535 block = finish_block (new->name, &local_symbols, new->old_blocks,
5536 lowpc, highpc, objfile);
5537
5538 /* For C++, set the block's scope. */
5539 if (cu->language == language_cplus || cu->language == language_fortran)
5540 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5541 determine_prefix (die, cu),
5542 processing_has_namespace_info);
5543
5544 /* If we have address ranges, record them. */
5545 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5546
5547 /* Attach template arguments to function. */
5548 if (! VEC_empty (symbolp, template_args))
5549 {
5550 gdb_assert (templ_func != NULL);
5551
5552 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5553 templ_func->template_arguments
5554 = obstack_alloc (&objfile->objfile_obstack,
5555 (templ_func->n_template_arguments
5556 * sizeof (struct symbol *)));
5557 memcpy (templ_func->template_arguments,
5558 VEC_address (symbolp, template_args),
5559 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5560 VEC_free (symbolp, template_args);
5561 }
5562
5563 /* In C++, we can have functions nested inside functions (e.g., when
5564 a function declares a class that has methods). This means that
5565 when we finish processing a function scope, we may need to go
5566 back to building a containing block's symbol lists. */
5567 local_symbols = new->locals;
5568 param_symbols = new->params;
5569 using_directives = new->using_directives;
5570
5571 /* If we've finished processing a top-level function, subsequent
5572 symbols go in the file symbol list. */
5573 if (outermost_context_p ())
5574 cu->list_in_scope = &file_symbols;
5575 }
5576
5577 /* Process all the DIES contained within a lexical block scope. Start
5578 a new scope, process the dies, and then close the scope. */
5579
5580 static void
5581 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5582 {
5583 struct objfile *objfile = cu->objfile;
5584 struct context_stack *new;
5585 CORE_ADDR lowpc, highpc;
5586 struct die_info *child_die;
5587 CORE_ADDR baseaddr;
5588
5589 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5590
5591 /* Ignore blocks with missing or invalid low and high pc attributes. */
5592 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5593 as multiple lexical blocks? Handling children in a sane way would
5594 be nasty. Might be easier to properly extend generic blocks to
5595 describe ranges. */
5596 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5597 return;
5598 lowpc += baseaddr;
5599 highpc += baseaddr;
5600
5601 push_context (0, lowpc);
5602 if (die->child != NULL)
5603 {
5604 child_die = die->child;
5605 while (child_die && child_die->tag)
5606 {
5607 process_die (child_die, cu);
5608 child_die = sibling_die (child_die);
5609 }
5610 }
5611 new = pop_context ();
5612
5613 if (local_symbols != NULL || using_directives != NULL)
5614 {
5615 struct block *block
5616 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5617 highpc, objfile);
5618
5619 /* Note that recording ranges after traversing children, as we
5620 do here, means that recording a parent's ranges entails
5621 walking across all its children's ranges as they appear in
5622 the address map, which is quadratic behavior.
5623
5624 It would be nicer to record the parent's ranges before
5625 traversing its children, simply overriding whatever you find
5626 there. But since we don't even decide whether to create a
5627 block until after we've traversed its children, that's hard
5628 to do. */
5629 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5630 }
5631 local_symbols = new->locals;
5632 using_directives = new->using_directives;
5633 }
5634
5635 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5636 Return 1 if the attributes are present and valid, otherwise, return 0.
5637 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
5638
5639 static int
5640 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5641 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5642 struct partial_symtab *ranges_pst)
5643 {
5644 struct objfile *objfile = cu->objfile;
5645 struct comp_unit_head *cu_header = &cu->header;
5646 bfd *obfd = objfile->obfd;
5647 unsigned int addr_size = cu_header->addr_size;
5648 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5649 /* Base address selection entry. */
5650 CORE_ADDR base;
5651 int found_base;
5652 unsigned int dummy;
5653 gdb_byte *buffer;
5654 CORE_ADDR marker;
5655 int low_set;
5656 CORE_ADDR low = 0;
5657 CORE_ADDR high = 0;
5658 CORE_ADDR baseaddr;
5659
5660 found_base = cu->base_known;
5661 base = cu->base_address;
5662
5663 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5664 if (offset >= dwarf2_per_objfile->ranges.size)
5665 {
5666 complaint (&symfile_complaints,
5667 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5668 offset);
5669 return 0;
5670 }
5671 buffer = dwarf2_per_objfile->ranges.buffer + offset;
5672
5673 /* Read in the largest possible address. */
5674 marker = read_address (obfd, buffer, cu, &dummy);
5675 if ((marker & mask) == mask)
5676 {
5677 /* If we found the largest possible address, then
5678 read the base address. */
5679 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5680 buffer += 2 * addr_size;
5681 offset += 2 * addr_size;
5682 found_base = 1;
5683 }
5684
5685 low_set = 0;
5686
5687 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5688
5689 while (1)
5690 {
5691 CORE_ADDR range_beginning, range_end;
5692
5693 range_beginning = read_address (obfd, buffer, cu, &dummy);
5694 buffer += addr_size;
5695 range_end = read_address (obfd, buffer, cu, &dummy);
5696 buffer += addr_size;
5697 offset += 2 * addr_size;
5698
5699 /* An end of list marker is a pair of zero addresses. */
5700 if (range_beginning == 0 && range_end == 0)
5701 /* Found the end of list entry. */
5702 break;
5703
5704 /* Each base address selection entry is a pair of 2 values.
5705 The first is the largest possible address, the second is
5706 the base address. Check for a base address here. */
5707 if ((range_beginning & mask) == mask)
5708 {
5709 /* If we found the largest possible address, then
5710 read the base address. */
5711 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5712 found_base = 1;
5713 continue;
5714 }
5715
5716 if (!found_base)
5717 {
5718 /* We have no valid base address for the ranges
5719 data. */
5720 complaint (&symfile_complaints,
5721 _("Invalid .debug_ranges data (no base address)"));
5722 return 0;
5723 }
5724
5725 range_beginning += base;
5726 range_end += base;
5727
5728 if (ranges_pst != NULL && range_beginning < range_end)
5729 addrmap_set_empty (objfile->psymtabs_addrmap,
5730 range_beginning + baseaddr, range_end - 1 + baseaddr,
5731 ranges_pst);
5732
5733 /* FIXME: This is recording everything as a low-high
5734 segment of consecutive addresses. We should have a
5735 data structure for discontiguous block ranges
5736 instead. */
5737 if (! low_set)
5738 {
5739 low = range_beginning;
5740 high = range_end;
5741 low_set = 1;
5742 }
5743 else
5744 {
5745 if (range_beginning < low)
5746 low = range_beginning;
5747 if (range_end > high)
5748 high = range_end;
5749 }
5750 }
5751
5752 if (! low_set)
5753 /* If the first entry is an end-of-list marker, the range
5754 describes an empty scope, i.e. no instructions. */
5755 return 0;
5756
5757 if (low_return)
5758 *low_return = low;
5759 if (high_return)
5760 *high_return = high;
5761 return 1;
5762 }
5763
5764 /* Get low and high pc attributes from a die. Return 1 if the attributes
5765 are present and valid, otherwise, return 0. Return -1 if the range is
5766 discontinuous, i.e. derived from DW_AT_ranges information. */
5767 static int
5768 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5769 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5770 struct partial_symtab *pst)
5771 {
5772 struct attribute *attr;
5773 CORE_ADDR low = 0;
5774 CORE_ADDR high = 0;
5775 int ret = 0;
5776
5777 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5778 if (attr)
5779 {
5780 high = DW_ADDR (attr);
5781 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5782 if (attr)
5783 low = DW_ADDR (attr);
5784 else
5785 /* Found high w/o low attribute. */
5786 return 0;
5787
5788 /* Found consecutive range of addresses. */
5789 ret = 1;
5790 }
5791 else
5792 {
5793 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5794 if (attr != NULL)
5795 {
5796 /* Value of the DW_AT_ranges attribute is the offset in the
5797 .debug_ranges section. */
5798 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5799 return 0;
5800 /* Found discontinuous range of addresses. */
5801 ret = -1;
5802 }
5803 }
5804
5805 if (high < low)
5806 return 0;
5807
5808 /* When using the GNU linker, .gnu.linkonce. sections are used to
5809 eliminate duplicate copies of functions and vtables and such.
5810 The linker will arbitrarily choose one and discard the others.
5811 The AT_*_pc values for such functions refer to local labels in
5812 these sections. If the section from that file was discarded, the
5813 labels are not in the output, so the relocs get a value of 0.
5814 If this is a discarded function, mark the pc bounds as invalid,
5815 so that GDB will ignore it. */
5816 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5817 return 0;
5818
5819 *lowpc = low;
5820 *highpc = high;
5821 return ret;
5822 }
5823
5824 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5825 its low and high PC addresses. Do nothing if these addresses could not
5826 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5827 and HIGHPC to the high address if greater than HIGHPC. */
5828
5829 static void
5830 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5831 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5832 struct dwarf2_cu *cu)
5833 {
5834 CORE_ADDR low, high;
5835 struct die_info *child = die->child;
5836
5837 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5838 {
5839 *lowpc = min (*lowpc, low);
5840 *highpc = max (*highpc, high);
5841 }
5842
5843 /* If the language does not allow nested subprograms (either inside
5844 subprograms or lexical blocks), we're done. */
5845 if (cu->language != language_ada)
5846 return;
5847
5848 /* Check all the children of the given DIE. If it contains nested
5849 subprograms, then check their pc bounds. Likewise, we need to
5850 check lexical blocks as well, as they may also contain subprogram
5851 definitions. */
5852 while (child && child->tag)
5853 {
5854 if (child->tag == DW_TAG_subprogram
5855 || child->tag == DW_TAG_lexical_block)
5856 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5857 child = sibling_die (child);
5858 }
5859 }
5860
5861 /* Get the low and high pc's represented by the scope DIE, and store
5862 them in *LOWPC and *HIGHPC. If the correct values can't be
5863 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5864
5865 static void
5866 get_scope_pc_bounds (struct die_info *die,
5867 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5868 struct dwarf2_cu *cu)
5869 {
5870 CORE_ADDR best_low = (CORE_ADDR) -1;
5871 CORE_ADDR best_high = (CORE_ADDR) 0;
5872 CORE_ADDR current_low, current_high;
5873
5874 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5875 {
5876 best_low = current_low;
5877 best_high = current_high;
5878 }
5879 else
5880 {
5881 struct die_info *child = die->child;
5882
5883 while (child && child->tag)
5884 {
5885 switch (child->tag) {
5886 case DW_TAG_subprogram:
5887 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5888 break;
5889 case DW_TAG_namespace:
5890 case DW_TAG_module:
5891 /* FIXME: carlton/2004-01-16: Should we do this for
5892 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5893 that current GCC's always emit the DIEs corresponding
5894 to definitions of methods of classes as children of a
5895 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5896 the DIEs giving the declarations, which could be
5897 anywhere). But I don't see any reason why the
5898 standards says that they have to be there. */
5899 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5900
5901 if (current_low != ((CORE_ADDR) -1))
5902 {
5903 best_low = min (best_low, current_low);
5904 best_high = max (best_high, current_high);
5905 }
5906 break;
5907 default:
5908 /* Ignore. */
5909 break;
5910 }
5911
5912 child = sibling_die (child);
5913 }
5914 }
5915
5916 *lowpc = best_low;
5917 *highpc = best_high;
5918 }
5919
5920 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5921 in DIE. */
5922 static void
5923 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5924 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5925 {
5926 struct attribute *attr;
5927
5928 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5929 if (attr)
5930 {
5931 CORE_ADDR high = DW_ADDR (attr);
5932
5933 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5934 if (attr)
5935 {
5936 CORE_ADDR low = DW_ADDR (attr);
5937
5938 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5939 }
5940 }
5941
5942 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5943 if (attr)
5944 {
5945 bfd *obfd = cu->objfile->obfd;
5946
5947 /* The value of the DW_AT_ranges attribute is the offset of the
5948 address range list in the .debug_ranges section. */
5949 unsigned long offset = DW_UNSND (attr);
5950 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5951
5952 /* For some target architectures, but not others, the
5953 read_address function sign-extends the addresses it returns.
5954 To recognize base address selection entries, we need a
5955 mask. */
5956 unsigned int addr_size = cu->header.addr_size;
5957 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5958
5959 /* The base address, to which the next pair is relative. Note
5960 that this 'base' is a DWARF concept: most entries in a range
5961 list are relative, to reduce the number of relocs against the
5962 debugging information. This is separate from this function's
5963 'baseaddr' argument, which GDB uses to relocate debugging
5964 information from a shared library based on the address at
5965 which the library was loaded. */
5966 CORE_ADDR base = cu->base_address;
5967 int base_known = cu->base_known;
5968
5969 gdb_assert (dwarf2_per_objfile->ranges.readin);
5970 if (offset >= dwarf2_per_objfile->ranges.size)
5971 {
5972 complaint (&symfile_complaints,
5973 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5974 offset);
5975 return;
5976 }
5977
5978 for (;;)
5979 {
5980 unsigned int bytes_read;
5981 CORE_ADDR start, end;
5982
5983 start = read_address (obfd, buffer, cu, &bytes_read);
5984 buffer += bytes_read;
5985 end = read_address (obfd, buffer, cu, &bytes_read);
5986 buffer += bytes_read;
5987
5988 /* Did we find the end of the range list? */
5989 if (start == 0 && end == 0)
5990 break;
5991
5992 /* Did we find a base address selection entry? */
5993 else if ((start & base_select_mask) == base_select_mask)
5994 {
5995 base = end;
5996 base_known = 1;
5997 }
5998
5999 /* We found an ordinary address range. */
6000 else
6001 {
6002 if (!base_known)
6003 {
6004 complaint (&symfile_complaints,
6005 _("Invalid .debug_ranges data (no base address)"));
6006 return;
6007 }
6008
6009 record_block_range (block,
6010 baseaddr + base + start,
6011 baseaddr + base + end - 1);
6012 }
6013 }
6014 }
6015 }
6016
6017 /* Add an aggregate field to the field list. */
6018
6019 static void
6020 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6021 struct dwarf2_cu *cu)
6022 {
6023 struct objfile *objfile = cu->objfile;
6024 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6025 struct nextfield *new_field;
6026 struct attribute *attr;
6027 struct field *fp;
6028 char *fieldname = "";
6029
6030 /* Allocate a new field list entry and link it in. */
6031 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6032 make_cleanup (xfree, new_field);
6033 memset (new_field, 0, sizeof (struct nextfield));
6034
6035 if (die->tag == DW_TAG_inheritance)
6036 {
6037 new_field->next = fip->baseclasses;
6038 fip->baseclasses = new_field;
6039 }
6040 else
6041 {
6042 new_field->next = fip->fields;
6043 fip->fields = new_field;
6044 }
6045 fip->nfields++;
6046
6047 /* Handle accessibility and virtuality of field.
6048 The default accessibility for members is public, the default
6049 accessibility for inheritance is private. */
6050 if (die->tag != DW_TAG_inheritance)
6051 new_field->accessibility = DW_ACCESS_public;
6052 else
6053 new_field->accessibility = DW_ACCESS_private;
6054 new_field->virtuality = DW_VIRTUALITY_none;
6055
6056 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6057 if (attr)
6058 new_field->accessibility = DW_UNSND (attr);
6059 if (new_field->accessibility != DW_ACCESS_public)
6060 fip->non_public_fields = 1;
6061 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6062 if (attr)
6063 new_field->virtuality = DW_UNSND (attr);
6064
6065 fp = &new_field->field;
6066
6067 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6068 {
6069 /* Data member other than a C++ static data member. */
6070
6071 /* Get type of field. */
6072 fp->type = die_type (die, cu);
6073
6074 SET_FIELD_BITPOS (*fp, 0);
6075
6076 /* Get bit size of field (zero if none). */
6077 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6078 if (attr)
6079 {
6080 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6081 }
6082 else
6083 {
6084 FIELD_BITSIZE (*fp) = 0;
6085 }
6086
6087 /* Get bit offset of field. */
6088 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6089 if (attr)
6090 {
6091 int byte_offset = 0;
6092
6093 if (attr_form_is_section_offset (attr))
6094 dwarf2_complex_location_expr_complaint ();
6095 else if (attr_form_is_constant (attr))
6096 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6097 else if (attr_form_is_block (attr))
6098 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6099 else
6100 dwarf2_complex_location_expr_complaint ();
6101
6102 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6103 }
6104 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6105 if (attr)
6106 {
6107 if (gdbarch_bits_big_endian (gdbarch))
6108 {
6109 /* For big endian bits, the DW_AT_bit_offset gives the
6110 additional bit offset from the MSB of the containing
6111 anonymous object to the MSB of the field. We don't
6112 have to do anything special since we don't need to
6113 know the size of the anonymous object. */
6114 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6115 }
6116 else
6117 {
6118 /* For little endian bits, compute the bit offset to the
6119 MSB of the anonymous object, subtract off the number of
6120 bits from the MSB of the field to the MSB of the
6121 object, and then subtract off the number of bits of
6122 the field itself. The result is the bit offset of
6123 the LSB of the field. */
6124 int anonymous_size;
6125 int bit_offset = DW_UNSND (attr);
6126
6127 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6128 if (attr)
6129 {
6130 /* The size of the anonymous object containing
6131 the bit field is explicit, so use the
6132 indicated size (in bytes). */
6133 anonymous_size = DW_UNSND (attr);
6134 }
6135 else
6136 {
6137 /* The size of the anonymous object containing
6138 the bit field must be inferred from the type
6139 attribute of the data member containing the
6140 bit field. */
6141 anonymous_size = TYPE_LENGTH (fp->type);
6142 }
6143 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6144 - bit_offset - FIELD_BITSIZE (*fp);
6145 }
6146 }
6147
6148 /* Get name of field. */
6149 fieldname = dwarf2_name (die, cu);
6150 if (fieldname == NULL)
6151 fieldname = "";
6152
6153 /* The name is already allocated along with this objfile, so we don't
6154 need to duplicate it for the type. */
6155 fp->name = fieldname;
6156
6157 /* Change accessibility for artificial fields (e.g. virtual table
6158 pointer or virtual base class pointer) to private. */
6159 if (dwarf2_attr (die, DW_AT_artificial, cu))
6160 {
6161 FIELD_ARTIFICIAL (*fp) = 1;
6162 new_field->accessibility = DW_ACCESS_private;
6163 fip->non_public_fields = 1;
6164 }
6165 }
6166 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6167 {
6168 /* C++ static member. */
6169
6170 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6171 is a declaration, but all versions of G++ as of this writing
6172 (so through at least 3.2.1) incorrectly generate
6173 DW_TAG_variable tags. */
6174
6175 char *physname;
6176
6177 /* Get name of field. */
6178 fieldname = dwarf2_name (die, cu);
6179 if (fieldname == NULL)
6180 return;
6181
6182 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6183 if (attr
6184 /* Only create a symbol if this is an external value.
6185 new_symbol checks this and puts the value in the global symbol
6186 table, which we want. If it is not external, new_symbol
6187 will try to put the value in cu->list_in_scope which is wrong. */
6188 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6189 {
6190 /* A static const member, not much different than an enum as far as
6191 we're concerned, except that we can support more types. */
6192 new_symbol (die, NULL, cu);
6193 }
6194
6195 /* Get physical name. */
6196 physname = (char *) dwarf2_physname (fieldname, die, cu);
6197
6198 /* The name is already allocated along with this objfile, so we don't
6199 need to duplicate it for the type. */
6200 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6201 FIELD_TYPE (*fp) = die_type (die, cu);
6202 FIELD_NAME (*fp) = fieldname;
6203 }
6204 else if (die->tag == DW_TAG_inheritance)
6205 {
6206 /* C++ base class field. */
6207 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6208 if (attr)
6209 {
6210 int byte_offset = 0;
6211
6212 if (attr_form_is_section_offset (attr))
6213 dwarf2_complex_location_expr_complaint ();
6214 else if (attr_form_is_constant (attr))
6215 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6216 else if (attr_form_is_block (attr))
6217 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6218 else
6219 dwarf2_complex_location_expr_complaint ();
6220
6221 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6222 }
6223 FIELD_BITSIZE (*fp) = 0;
6224 FIELD_TYPE (*fp) = die_type (die, cu);
6225 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6226 fip->nbaseclasses++;
6227 }
6228 }
6229
6230 /* Add a typedef defined in the scope of the FIP's class. */
6231
6232 static void
6233 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6234 struct dwarf2_cu *cu)
6235 {
6236 struct objfile *objfile = cu->objfile;
6237 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6238 struct typedef_field_list *new_field;
6239 struct attribute *attr;
6240 struct typedef_field *fp;
6241 char *fieldname = "";
6242
6243 /* Allocate a new field list entry and link it in. */
6244 new_field = xzalloc (sizeof (*new_field));
6245 make_cleanup (xfree, new_field);
6246
6247 gdb_assert (die->tag == DW_TAG_typedef);
6248
6249 fp = &new_field->field;
6250
6251 /* Get name of field. */
6252 fp->name = dwarf2_name (die, cu);
6253 if (fp->name == NULL)
6254 return;
6255
6256 fp->type = read_type_die (die, cu);
6257
6258 new_field->next = fip->typedef_field_list;
6259 fip->typedef_field_list = new_field;
6260 fip->typedef_field_list_count++;
6261 }
6262
6263 /* Create the vector of fields, and attach it to the type. */
6264
6265 static void
6266 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6267 struct dwarf2_cu *cu)
6268 {
6269 int nfields = fip->nfields;
6270
6271 /* Record the field count, allocate space for the array of fields,
6272 and create blank accessibility bitfields if necessary. */
6273 TYPE_NFIELDS (type) = nfields;
6274 TYPE_FIELDS (type) = (struct field *)
6275 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6276 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6277
6278 if (fip->non_public_fields && cu->language != language_ada)
6279 {
6280 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6281
6282 TYPE_FIELD_PRIVATE_BITS (type) =
6283 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6284 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6285
6286 TYPE_FIELD_PROTECTED_BITS (type) =
6287 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6288 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6289
6290 TYPE_FIELD_IGNORE_BITS (type) =
6291 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6292 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6293 }
6294
6295 /* If the type has baseclasses, allocate and clear a bit vector for
6296 TYPE_FIELD_VIRTUAL_BITS. */
6297 if (fip->nbaseclasses && cu->language != language_ada)
6298 {
6299 int num_bytes = B_BYTES (fip->nbaseclasses);
6300 unsigned char *pointer;
6301
6302 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6303 pointer = TYPE_ALLOC (type, num_bytes);
6304 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6305 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6306 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6307 }
6308
6309 /* Copy the saved-up fields into the field vector. Start from the head
6310 of the list, adding to the tail of the field array, so that they end
6311 up in the same order in the array in which they were added to the list. */
6312 while (nfields-- > 0)
6313 {
6314 struct nextfield *fieldp;
6315
6316 if (fip->fields)
6317 {
6318 fieldp = fip->fields;
6319 fip->fields = fieldp->next;
6320 }
6321 else
6322 {
6323 fieldp = fip->baseclasses;
6324 fip->baseclasses = fieldp->next;
6325 }
6326
6327 TYPE_FIELD (type, nfields) = fieldp->field;
6328 switch (fieldp->accessibility)
6329 {
6330 case DW_ACCESS_private:
6331 if (cu->language != language_ada)
6332 SET_TYPE_FIELD_PRIVATE (type, nfields);
6333 break;
6334
6335 case DW_ACCESS_protected:
6336 if (cu->language != language_ada)
6337 SET_TYPE_FIELD_PROTECTED (type, nfields);
6338 break;
6339
6340 case DW_ACCESS_public:
6341 break;
6342
6343 default:
6344 /* Unknown accessibility. Complain and treat it as public. */
6345 {
6346 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6347 fieldp->accessibility);
6348 }
6349 break;
6350 }
6351 if (nfields < fip->nbaseclasses)
6352 {
6353 switch (fieldp->virtuality)
6354 {
6355 case DW_VIRTUALITY_virtual:
6356 case DW_VIRTUALITY_pure_virtual:
6357 if (cu->language == language_ada)
6358 error ("unexpected virtuality in component of Ada type");
6359 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6360 break;
6361 }
6362 }
6363 }
6364 }
6365
6366 /* Add a member function to the proper fieldlist. */
6367
6368 static void
6369 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6370 struct type *type, struct dwarf2_cu *cu)
6371 {
6372 struct objfile *objfile = cu->objfile;
6373 struct attribute *attr;
6374 struct fnfieldlist *flp;
6375 int i;
6376 struct fn_field *fnp;
6377 char *fieldname;
6378 struct nextfnfield *new_fnfield;
6379 struct type *this_type;
6380
6381 if (cu->language == language_ada)
6382 error ("unexpected member function in Ada type");
6383
6384 /* Get name of member function. */
6385 fieldname = dwarf2_name (die, cu);
6386 if (fieldname == NULL)
6387 return;
6388
6389 /* Look up member function name in fieldlist. */
6390 for (i = 0; i < fip->nfnfields; i++)
6391 {
6392 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6393 break;
6394 }
6395
6396 /* Create new list element if necessary. */
6397 if (i < fip->nfnfields)
6398 flp = &fip->fnfieldlists[i];
6399 else
6400 {
6401 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6402 {
6403 fip->fnfieldlists = (struct fnfieldlist *)
6404 xrealloc (fip->fnfieldlists,
6405 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6406 * sizeof (struct fnfieldlist));
6407 if (fip->nfnfields == 0)
6408 make_cleanup (free_current_contents, &fip->fnfieldlists);
6409 }
6410 flp = &fip->fnfieldlists[fip->nfnfields];
6411 flp->name = fieldname;
6412 flp->length = 0;
6413 flp->head = NULL;
6414 i = fip->nfnfields++;
6415 }
6416
6417 /* Create a new member function field and chain it to the field list
6418 entry. */
6419 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6420 make_cleanup (xfree, new_fnfield);
6421 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6422 new_fnfield->next = flp->head;
6423 flp->head = new_fnfield;
6424 flp->length++;
6425
6426 /* Fill in the member function field info. */
6427 fnp = &new_fnfield->fnfield;
6428
6429 /* Delay processing of the physname until later. */
6430 if (cu->language == language_cplus || cu->language == language_java)
6431 {
6432 add_to_method_list (type, i, flp->length - 1, fieldname,
6433 die, cu);
6434 }
6435 else
6436 {
6437 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6438 fnp->physname = physname ? physname : "";
6439 }
6440
6441 fnp->type = alloc_type (objfile);
6442 this_type = read_type_die (die, cu);
6443 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6444 {
6445 int nparams = TYPE_NFIELDS (this_type);
6446
6447 /* TYPE is the domain of this method, and THIS_TYPE is the type
6448 of the method itself (TYPE_CODE_METHOD). */
6449 smash_to_method_type (fnp->type, type,
6450 TYPE_TARGET_TYPE (this_type),
6451 TYPE_FIELDS (this_type),
6452 TYPE_NFIELDS (this_type),
6453 TYPE_VARARGS (this_type));
6454
6455 /* Handle static member functions.
6456 Dwarf2 has no clean way to discern C++ static and non-static
6457 member functions. G++ helps GDB by marking the first
6458 parameter for non-static member functions (which is the
6459 this pointer) as artificial. We obtain this information
6460 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
6461 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6462 fnp->voffset = VOFFSET_STATIC;
6463 }
6464 else
6465 complaint (&symfile_complaints, _("member function type missing for '%s'"),
6466 dwarf2_full_name (fieldname, die, cu));
6467
6468 /* Get fcontext from DW_AT_containing_type if present. */
6469 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6470 fnp->fcontext = die_containing_type (die, cu);
6471
6472 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6473 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6474
6475 /* Get accessibility. */
6476 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6477 if (attr)
6478 {
6479 switch (DW_UNSND (attr))
6480 {
6481 case DW_ACCESS_private:
6482 fnp->is_private = 1;
6483 break;
6484 case DW_ACCESS_protected:
6485 fnp->is_protected = 1;
6486 break;
6487 }
6488 }
6489
6490 /* Check for artificial methods. */
6491 attr = dwarf2_attr (die, DW_AT_artificial, cu);
6492 if (attr && DW_UNSND (attr) != 0)
6493 fnp->is_artificial = 1;
6494
6495 /* Get index in virtual function table if it is a virtual member
6496 function. For older versions of GCC, this is an offset in the
6497 appropriate virtual table, as specified by DW_AT_containing_type.
6498 For everyone else, it is an expression to be evaluated relative
6499 to the object address. */
6500
6501 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6502 if (attr)
6503 {
6504 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6505 {
6506 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6507 {
6508 /* Old-style GCC. */
6509 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6510 }
6511 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6512 || (DW_BLOCK (attr)->size > 1
6513 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6514 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6515 {
6516 struct dwarf_block blk;
6517 int offset;
6518
6519 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6520 ? 1 : 2);
6521 blk.size = DW_BLOCK (attr)->size - offset;
6522 blk.data = DW_BLOCK (attr)->data + offset;
6523 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6524 if ((fnp->voffset % cu->header.addr_size) != 0)
6525 dwarf2_complex_location_expr_complaint ();
6526 else
6527 fnp->voffset /= cu->header.addr_size;
6528 fnp->voffset += 2;
6529 }
6530 else
6531 dwarf2_complex_location_expr_complaint ();
6532
6533 if (!fnp->fcontext)
6534 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6535 }
6536 else if (attr_form_is_section_offset (attr))
6537 {
6538 dwarf2_complex_location_expr_complaint ();
6539 }
6540 else
6541 {
6542 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6543 fieldname);
6544 }
6545 }
6546 else
6547 {
6548 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6549 if (attr && DW_UNSND (attr))
6550 {
6551 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6552 complaint (&symfile_complaints,
6553 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6554 fieldname, die->offset);
6555 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6556 TYPE_CPLUS_DYNAMIC (type) = 1;
6557 }
6558 }
6559 }
6560
6561 /* Create the vector of member function fields, and attach it to the type. */
6562
6563 static void
6564 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6565 struct dwarf2_cu *cu)
6566 {
6567 struct fnfieldlist *flp;
6568 int total_length = 0;
6569 int i;
6570
6571 if (cu->language == language_ada)
6572 error ("unexpected member functions in Ada type");
6573
6574 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6575 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6576 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6577
6578 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6579 {
6580 struct nextfnfield *nfp = flp->head;
6581 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6582 int k;
6583
6584 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6585 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6586 fn_flp->fn_fields = (struct fn_field *)
6587 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6588 for (k = flp->length; (k--, nfp); nfp = nfp->next)
6589 fn_flp->fn_fields[k] = nfp->fnfield;
6590
6591 total_length += flp->length;
6592 }
6593
6594 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6595 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6596 }
6597
6598 /* Returns non-zero if NAME is the name of a vtable member in CU's
6599 language, zero otherwise. */
6600 static int
6601 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6602 {
6603 static const char vptr[] = "_vptr";
6604 static const char vtable[] = "vtable";
6605
6606 /* Look for the C++ and Java forms of the vtable. */
6607 if ((cu->language == language_java
6608 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6609 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6610 && is_cplus_marker (name[sizeof (vptr) - 1])))
6611 return 1;
6612
6613 return 0;
6614 }
6615
6616 /* GCC outputs unnamed structures that are really pointers to member
6617 functions, with the ABI-specified layout. If TYPE describes
6618 such a structure, smash it into a member function type.
6619
6620 GCC shouldn't do this; it should just output pointer to member DIEs.
6621 This is GCC PR debug/28767. */
6622
6623 static void
6624 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6625 {
6626 struct type *pfn_type, *domain_type, *new_type;
6627
6628 /* Check for a structure with no name and two children. */
6629 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6630 return;
6631
6632 /* Check for __pfn and __delta members. */
6633 if (TYPE_FIELD_NAME (type, 0) == NULL
6634 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6635 || TYPE_FIELD_NAME (type, 1) == NULL
6636 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6637 return;
6638
6639 /* Find the type of the method. */
6640 pfn_type = TYPE_FIELD_TYPE (type, 0);
6641 if (pfn_type == NULL
6642 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6643 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6644 return;
6645
6646 /* Look for the "this" argument. */
6647 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6648 if (TYPE_NFIELDS (pfn_type) == 0
6649 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6650 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6651 return;
6652
6653 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6654 new_type = alloc_type (objfile);
6655 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6656 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6657 TYPE_VARARGS (pfn_type));
6658 smash_to_methodptr_type (type, new_type);
6659 }
6660
6661 /* Called when we find the DIE that starts a structure or union scope
6662 (definition) to create a type for the structure or union. Fill in
6663 the type's name and general properties; the members will not be
6664 processed until process_structure_type.
6665
6666 NOTE: we need to call these functions regardless of whether or not the
6667 DIE has a DW_AT_name attribute, since it might be an anonymous
6668 structure or union. This gets the type entered into our set of
6669 user defined types.
6670
6671 However, if the structure is incomplete (an opaque struct/union)
6672 then suppress creating a symbol table entry for it since gdb only
6673 wants to find the one with the complete definition. Note that if
6674 it is complete, we just call new_symbol, which does it's own
6675 checking about whether the struct/union is anonymous or not (and
6676 suppresses creating a symbol table entry itself). */
6677
6678 static struct type *
6679 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6680 {
6681 struct objfile *objfile = cu->objfile;
6682 struct type *type;
6683 struct attribute *attr;
6684 char *name;
6685
6686 /* If the definition of this type lives in .debug_types, read that type.
6687 Don't follow DW_AT_specification though, that will take us back up
6688 the chain and we want to go down. */
6689 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6690 if (attr)
6691 {
6692 struct dwarf2_cu *type_cu = cu;
6693 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6694
6695 /* We could just recurse on read_structure_type, but we need to call
6696 get_die_type to ensure only one type for this DIE is created.
6697 This is important, for example, because for c++ classes we need
6698 TYPE_NAME set which is only done by new_symbol. Blech. */
6699 type = read_type_die (type_die, type_cu);
6700
6701 /* TYPE_CU may not be the same as CU.
6702 Ensure TYPE is recorded in CU's type_hash table. */
6703 return set_die_type (die, type, cu);
6704 }
6705
6706 type = alloc_type (objfile);
6707 INIT_CPLUS_SPECIFIC (type);
6708
6709 name = dwarf2_name (die, cu);
6710 if (name != NULL)
6711 {
6712 if (cu->language == language_cplus
6713 || cu->language == language_java)
6714 {
6715 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6716
6717 /* dwarf2_full_name might have already finished building the DIE's
6718 type. If so, there is no need to continue. */
6719 if (get_die_type (die, cu) != NULL)
6720 return get_die_type (die, cu);
6721
6722 TYPE_TAG_NAME (type) = full_name;
6723 if (die->tag == DW_TAG_structure_type
6724 || die->tag == DW_TAG_class_type)
6725 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6726 }
6727 else
6728 {
6729 /* The name is already allocated along with this objfile, so
6730 we don't need to duplicate it for the type. */
6731 TYPE_TAG_NAME (type) = (char *) name;
6732 if (die->tag == DW_TAG_class_type)
6733 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6734 }
6735 }
6736
6737 if (die->tag == DW_TAG_structure_type)
6738 {
6739 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6740 }
6741 else if (die->tag == DW_TAG_union_type)
6742 {
6743 TYPE_CODE (type) = TYPE_CODE_UNION;
6744 }
6745 else
6746 {
6747 TYPE_CODE (type) = TYPE_CODE_CLASS;
6748 }
6749
6750 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6751 TYPE_DECLARED_CLASS (type) = 1;
6752
6753 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6754 if (attr)
6755 {
6756 TYPE_LENGTH (type) = DW_UNSND (attr);
6757 }
6758 else
6759 {
6760 TYPE_LENGTH (type) = 0;
6761 }
6762
6763 TYPE_STUB_SUPPORTED (type) = 1;
6764 if (die_is_declaration (die, cu))
6765 TYPE_STUB (type) = 1;
6766 else if (attr == NULL && die->child == NULL
6767 && producer_is_realview (cu->producer))
6768 /* RealView does not output the required DW_AT_declaration
6769 on incomplete types. */
6770 TYPE_STUB (type) = 1;
6771
6772 /* We need to add the type field to the die immediately so we don't
6773 infinitely recurse when dealing with pointers to the structure
6774 type within the structure itself. */
6775 set_die_type (die, type, cu);
6776
6777 /* set_die_type should be already done. */
6778 set_descriptive_type (type, die, cu);
6779
6780 return type;
6781 }
6782
6783 /* Finish creating a structure or union type, including filling in
6784 its members and creating a symbol for it. */
6785
6786 static void
6787 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6788 {
6789 struct objfile *objfile = cu->objfile;
6790 struct die_info *child_die = die->child;
6791 struct type *type;
6792
6793 type = get_die_type (die, cu);
6794 if (type == NULL)
6795 type = read_structure_type (die, cu);
6796
6797 if (die->child != NULL && ! die_is_declaration (die, cu))
6798 {
6799 struct field_info fi;
6800 struct die_info *child_die;
6801 VEC (symbolp) *template_args = NULL;
6802 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6803
6804 memset (&fi, 0, sizeof (struct field_info));
6805
6806 child_die = die->child;
6807
6808 while (child_die && child_die->tag)
6809 {
6810 if (child_die->tag == DW_TAG_member
6811 || child_die->tag == DW_TAG_variable)
6812 {
6813 /* NOTE: carlton/2002-11-05: A C++ static data member
6814 should be a DW_TAG_member that is a declaration, but
6815 all versions of G++ as of this writing (so through at
6816 least 3.2.1) incorrectly generate DW_TAG_variable
6817 tags for them instead. */
6818 dwarf2_add_field (&fi, child_die, cu);
6819 }
6820 else if (child_die->tag == DW_TAG_subprogram)
6821 {
6822 /* C++ member function. */
6823 dwarf2_add_member_fn (&fi, child_die, type, cu);
6824 }
6825 else if (child_die->tag == DW_TAG_inheritance)
6826 {
6827 /* C++ base class field. */
6828 dwarf2_add_field (&fi, child_die, cu);
6829 }
6830 else if (child_die->tag == DW_TAG_typedef)
6831 dwarf2_add_typedef (&fi, child_die, cu);
6832 else if (child_die->tag == DW_TAG_template_type_param
6833 || child_die->tag == DW_TAG_template_value_param)
6834 {
6835 struct symbol *arg = new_symbol (child_die, NULL, cu);
6836
6837 if (arg != NULL)
6838 VEC_safe_push (symbolp, template_args, arg);
6839 }
6840
6841 child_die = sibling_die (child_die);
6842 }
6843
6844 /* Attach template arguments to type. */
6845 if (! VEC_empty (symbolp, template_args))
6846 {
6847 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6848 TYPE_N_TEMPLATE_ARGUMENTS (type)
6849 = VEC_length (symbolp, template_args);
6850 TYPE_TEMPLATE_ARGUMENTS (type)
6851 = obstack_alloc (&objfile->objfile_obstack,
6852 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6853 * sizeof (struct symbol *)));
6854 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6855 VEC_address (symbolp, template_args),
6856 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6857 * sizeof (struct symbol *)));
6858 VEC_free (symbolp, template_args);
6859 }
6860
6861 /* Attach fields and member functions to the type. */
6862 if (fi.nfields)
6863 dwarf2_attach_fields_to_type (&fi, type, cu);
6864 if (fi.nfnfields)
6865 {
6866 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6867
6868 /* Get the type which refers to the base class (possibly this
6869 class itself) which contains the vtable pointer for the current
6870 class from the DW_AT_containing_type attribute. This use of
6871 DW_AT_containing_type is a GNU extension. */
6872
6873 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6874 {
6875 struct type *t = die_containing_type (die, cu);
6876
6877 TYPE_VPTR_BASETYPE (type) = t;
6878 if (type == t)
6879 {
6880 int i;
6881
6882 /* Our own class provides vtbl ptr. */
6883 for (i = TYPE_NFIELDS (t) - 1;
6884 i >= TYPE_N_BASECLASSES (t);
6885 --i)
6886 {
6887 char *fieldname = TYPE_FIELD_NAME (t, i);
6888
6889 if (is_vtable_name (fieldname, cu))
6890 {
6891 TYPE_VPTR_FIELDNO (type) = i;
6892 break;
6893 }
6894 }
6895
6896 /* Complain if virtual function table field not found. */
6897 if (i < TYPE_N_BASECLASSES (t))
6898 complaint (&symfile_complaints,
6899 _("virtual function table pointer not found when defining class '%s'"),
6900 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6901 "");
6902 }
6903 else
6904 {
6905 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6906 }
6907 }
6908 else if (cu->producer
6909 && strncmp (cu->producer,
6910 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6911 {
6912 /* The IBM XLC compiler does not provide direct indication
6913 of the containing type, but the vtable pointer is
6914 always named __vfp. */
6915
6916 int i;
6917
6918 for (i = TYPE_NFIELDS (type) - 1;
6919 i >= TYPE_N_BASECLASSES (type);
6920 --i)
6921 {
6922 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6923 {
6924 TYPE_VPTR_FIELDNO (type) = i;
6925 TYPE_VPTR_BASETYPE (type) = type;
6926 break;
6927 }
6928 }
6929 }
6930 }
6931
6932 /* Copy fi.typedef_field_list linked list elements content into the
6933 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6934 if (fi.typedef_field_list)
6935 {
6936 int i = fi.typedef_field_list_count;
6937
6938 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6939 TYPE_TYPEDEF_FIELD_ARRAY (type)
6940 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6941 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6942
6943 /* Reverse the list order to keep the debug info elements order. */
6944 while (--i >= 0)
6945 {
6946 struct typedef_field *dest, *src;
6947
6948 dest = &TYPE_TYPEDEF_FIELD (type, i);
6949 src = &fi.typedef_field_list->field;
6950 fi.typedef_field_list = fi.typedef_field_list->next;
6951 *dest = *src;
6952 }
6953 }
6954
6955 do_cleanups (back_to);
6956 }
6957
6958 quirk_gcc_member_function_pointer (type, cu->objfile);
6959
6960 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6961 snapshots) has been known to create a die giving a declaration
6962 for a class that has, as a child, a die giving a definition for a
6963 nested class. So we have to process our children even if the
6964 current die is a declaration. Normally, of course, a declaration
6965 won't have any children at all. */
6966
6967 while (child_die != NULL && child_die->tag)
6968 {
6969 if (child_die->tag == DW_TAG_member
6970 || child_die->tag == DW_TAG_variable
6971 || child_die->tag == DW_TAG_inheritance
6972 || child_die->tag == DW_TAG_template_value_param
6973 || child_die->tag == DW_TAG_template_type_param)
6974 {
6975 /* Do nothing. */
6976 }
6977 else
6978 process_die (child_die, cu);
6979
6980 child_die = sibling_die (child_die);
6981 }
6982
6983 /* Do not consider external references. According to the DWARF standard,
6984 these DIEs are identified by the fact that they have no byte_size
6985 attribute, and a declaration attribute. */
6986 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6987 || !die_is_declaration (die, cu))
6988 new_symbol (die, type, cu);
6989 }
6990
6991 /* Given a DW_AT_enumeration_type die, set its type. We do not
6992 complete the type's fields yet, or create any symbols. */
6993
6994 static struct type *
6995 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6996 {
6997 struct objfile *objfile = cu->objfile;
6998 struct type *type;
6999 struct attribute *attr;
7000 const char *name;
7001
7002 /* If the definition of this type lives in .debug_types, read that type.
7003 Don't follow DW_AT_specification though, that will take us back up
7004 the chain and we want to go down. */
7005 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7006 if (attr)
7007 {
7008 struct dwarf2_cu *type_cu = cu;
7009 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7010
7011 type = read_type_die (type_die, type_cu);
7012
7013 /* TYPE_CU may not be the same as CU.
7014 Ensure TYPE is recorded in CU's type_hash table. */
7015 return set_die_type (die, type, cu);
7016 }
7017
7018 type = alloc_type (objfile);
7019
7020 TYPE_CODE (type) = TYPE_CODE_ENUM;
7021 name = dwarf2_full_name (NULL, die, cu);
7022 if (name != NULL)
7023 TYPE_TAG_NAME (type) = (char *) name;
7024
7025 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7026 if (attr)
7027 {
7028 TYPE_LENGTH (type) = DW_UNSND (attr);
7029 }
7030 else
7031 {
7032 TYPE_LENGTH (type) = 0;
7033 }
7034
7035 /* The enumeration DIE can be incomplete. In Ada, any type can be
7036 declared as private in the package spec, and then defined only
7037 inside the package body. Such types are known as Taft Amendment
7038 Types. When another package uses such a type, an incomplete DIE
7039 may be generated by the compiler. */
7040 if (die_is_declaration (die, cu))
7041 TYPE_STUB (type) = 1;
7042
7043 return set_die_type (die, type, cu);
7044 }
7045
7046 /* Given a pointer to a die which begins an enumeration, process all
7047 the dies that define the members of the enumeration, and create the
7048 symbol for the enumeration type.
7049
7050 NOTE: We reverse the order of the element list. */
7051
7052 static void
7053 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7054 {
7055 struct type *this_type;
7056
7057 this_type = get_die_type (die, cu);
7058 if (this_type == NULL)
7059 this_type = read_enumeration_type (die, cu);
7060
7061 if (die->child != NULL)
7062 {
7063 struct die_info *child_die;
7064 struct symbol *sym;
7065 struct field *fields = NULL;
7066 int num_fields = 0;
7067 int unsigned_enum = 1;
7068 char *name;
7069
7070 child_die = die->child;
7071 while (child_die && child_die->tag)
7072 {
7073 if (child_die->tag != DW_TAG_enumerator)
7074 {
7075 process_die (child_die, cu);
7076 }
7077 else
7078 {
7079 name = dwarf2_name (child_die, cu);
7080 if (name)
7081 {
7082 sym = new_symbol (child_die, this_type, cu);
7083 if (SYMBOL_VALUE (sym) < 0)
7084 unsigned_enum = 0;
7085
7086 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7087 {
7088 fields = (struct field *)
7089 xrealloc (fields,
7090 (num_fields + DW_FIELD_ALLOC_CHUNK)
7091 * sizeof (struct field));
7092 }
7093
7094 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7095 FIELD_TYPE (fields[num_fields]) = NULL;
7096 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7097 FIELD_BITSIZE (fields[num_fields]) = 0;
7098
7099 num_fields++;
7100 }
7101 }
7102
7103 child_die = sibling_die (child_die);
7104 }
7105
7106 if (num_fields)
7107 {
7108 TYPE_NFIELDS (this_type) = num_fields;
7109 TYPE_FIELDS (this_type) = (struct field *)
7110 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7111 memcpy (TYPE_FIELDS (this_type), fields,
7112 sizeof (struct field) * num_fields);
7113 xfree (fields);
7114 }
7115 if (unsigned_enum)
7116 TYPE_UNSIGNED (this_type) = 1;
7117 }
7118
7119 new_symbol (die, this_type, cu);
7120 }
7121
7122 /* Extract all information from a DW_TAG_array_type DIE and put it in
7123 the DIE's type field. For now, this only handles one dimensional
7124 arrays. */
7125
7126 static struct type *
7127 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7128 {
7129 struct objfile *objfile = cu->objfile;
7130 struct die_info *child_die;
7131 struct type *type;
7132 struct type *element_type, *range_type, *index_type;
7133 struct type **range_types = NULL;
7134 struct attribute *attr;
7135 int ndim = 0;
7136 struct cleanup *back_to;
7137 char *name;
7138
7139 element_type = die_type (die, cu);
7140
7141 /* The die_type call above may have already set the type for this DIE. */
7142 type = get_die_type (die, cu);
7143 if (type)
7144 return type;
7145
7146 /* Irix 6.2 native cc creates array types without children for
7147 arrays with unspecified length. */
7148 if (die->child == NULL)
7149 {
7150 index_type = objfile_type (objfile)->builtin_int;
7151 range_type = create_range_type (NULL, index_type, 0, -1);
7152 type = create_array_type (NULL, element_type, range_type);
7153 return set_die_type (die, type, cu);
7154 }
7155
7156 back_to = make_cleanup (null_cleanup, NULL);
7157 child_die = die->child;
7158 while (child_die && child_die->tag)
7159 {
7160 if (child_die->tag == DW_TAG_subrange_type)
7161 {
7162 struct type *child_type = read_type_die (child_die, cu);
7163
7164 if (child_type != NULL)
7165 {
7166 /* The range type was succesfully read. Save it for
7167 the array type creation. */
7168 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7169 {
7170 range_types = (struct type **)
7171 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7172 * sizeof (struct type *));
7173 if (ndim == 0)
7174 make_cleanup (free_current_contents, &range_types);
7175 }
7176 range_types[ndim++] = child_type;
7177 }
7178 }
7179 child_die = sibling_die (child_die);
7180 }
7181
7182 /* Dwarf2 dimensions are output from left to right, create the
7183 necessary array types in backwards order. */
7184
7185 type = element_type;
7186
7187 if (read_array_order (die, cu) == DW_ORD_col_major)
7188 {
7189 int i = 0;
7190
7191 while (i < ndim)
7192 type = create_array_type (NULL, type, range_types[i++]);
7193 }
7194 else
7195 {
7196 while (ndim-- > 0)
7197 type = create_array_type (NULL, type, range_types[ndim]);
7198 }
7199
7200 /* Understand Dwarf2 support for vector types (like they occur on
7201 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7202 array type. This is not part of the Dwarf2/3 standard yet, but a
7203 custom vendor extension. The main difference between a regular
7204 array and the vector variant is that vectors are passed by value
7205 to functions. */
7206 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7207 if (attr)
7208 make_vector_type (type);
7209
7210 name = dwarf2_name (die, cu);
7211 if (name)
7212 TYPE_NAME (type) = name;
7213
7214 /* Install the type in the die. */
7215 set_die_type (die, type, cu);
7216
7217 /* set_die_type should be already done. */
7218 set_descriptive_type (type, die, cu);
7219
7220 do_cleanups (back_to);
7221
7222 return type;
7223 }
7224
7225 static enum dwarf_array_dim_ordering
7226 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7227 {
7228 struct attribute *attr;
7229
7230 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7231
7232 if (attr) return DW_SND (attr);
7233
7234 /*
7235 GNU F77 is a special case, as at 08/2004 array type info is the
7236 opposite order to the dwarf2 specification, but data is still
7237 laid out as per normal fortran.
7238
7239 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7240 version checking.
7241 */
7242
7243 if (cu->language == language_fortran
7244 && cu->producer && strstr (cu->producer, "GNU F77"))
7245 {
7246 return DW_ORD_row_major;
7247 }
7248
7249 switch (cu->language_defn->la_array_ordering)
7250 {
7251 case array_column_major:
7252 return DW_ORD_col_major;
7253 case array_row_major:
7254 default:
7255 return DW_ORD_row_major;
7256 };
7257 }
7258
7259 /* Extract all information from a DW_TAG_set_type DIE and put it in
7260 the DIE's type field. */
7261
7262 static struct type *
7263 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7264 {
7265 struct type *domain_type, *set_type;
7266 struct attribute *attr;
7267
7268 domain_type = die_type (die, cu);
7269
7270 /* The die_type call above may have already set the type for this DIE. */
7271 set_type = get_die_type (die, cu);
7272 if (set_type)
7273 return set_type;
7274
7275 set_type = create_set_type (NULL, domain_type);
7276
7277 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7278 if (attr)
7279 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7280
7281 return set_die_type (die, set_type, cu);
7282 }
7283
7284 /* First cut: install each common block member as a global variable. */
7285
7286 static void
7287 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7288 {
7289 struct die_info *child_die;
7290 struct attribute *attr;
7291 struct symbol *sym;
7292 CORE_ADDR base = (CORE_ADDR) 0;
7293
7294 attr = dwarf2_attr (die, DW_AT_location, cu);
7295 if (attr)
7296 {
7297 /* Support the .debug_loc offsets */
7298 if (attr_form_is_block (attr))
7299 {
7300 base = decode_locdesc (DW_BLOCK (attr), cu);
7301 }
7302 else if (attr_form_is_section_offset (attr))
7303 {
7304 dwarf2_complex_location_expr_complaint ();
7305 }
7306 else
7307 {
7308 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7309 "common block member");
7310 }
7311 }
7312 if (die->child != NULL)
7313 {
7314 child_die = die->child;
7315 while (child_die && child_die->tag)
7316 {
7317 sym = new_symbol (child_die, NULL, cu);
7318 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7319 if (sym != NULL && attr != NULL)
7320 {
7321 CORE_ADDR byte_offset = 0;
7322
7323 if (attr_form_is_section_offset (attr))
7324 dwarf2_complex_location_expr_complaint ();
7325 else if (attr_form_is_constant (attr))
7326 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7327 else if (attr_form_is_block (attr))
7328 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7329 else
7330 dwarf2_complex_location_expr_complaint ();
7331
7332 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7333 add_symbol_to_list (sym, &global_symbols);
7334 }
7335 child_die = sibling_die (child_die);
7336 }
7337 }
7338 }
7339
7340 /* Create a type for a C++ namespace. */
7341
7342 static struct type *
7343 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7344 {
7345 struct objfile *objfile = cu->objfile;
7346 const char *previous_prefix, *name;
7347 int is_anonymous;
7348 struct type *type;
7349
7350 /* For extensions, reuse the type of the original namespace. */
7351 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7352 {
7353 struct die_info *ext_die;
7354 struct dwarf2_cu *ext_cu = cu;
7355
7356 ext_die = dwarf2_extension (die, &ext_cu);
7357 type = read_type_die (ext_die, ext_cu);
7358
7359 /* EXT_CU may not be the same as CU.
7360 Ensure TYPE is recorded in CU's type_hash table. */
7361 return set_die_type (die, type, cu);
7362 }
7363
7364 name = namespace_name (die, &is_anonymous, cu);
7365
7366 /* Now build the name of the current namespace. */
7367
7368 previous_prefix = determine_prefix (die, cu);
7369 if (previous_prefix[0] != '\0')
7370 name = typename_concat (&objfile->objfile_obstack,
7371 previous_prefix, name, 0, cu);
7372
7373 /* Create the type. */
7374 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7375 objfile);
7376 TYPE_NAME (type) = (char *) name;
7377 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7378
7379 return set_die_type (die, type, cu);
7380 }
7381
7382 /* Read a C++ namespace. */
7383
7384 static void
7385 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7386 {
7387 struct objfile *objfile = cu->objfile;
7388 const char *name;
7389 int is_anonymous;
7390
7391 /* Add a symbol associated to this if we haven't seen the namespace
7392 before. Also, add a using directive if it's an anonymous
7393 namespace. */
7394
7395 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7396 {
7397 struct type *type;
7398
7399 type = read_type_die (die, cu);
7400 new_symbol (die, type, cu);
7401
7402 name = namespace_name (die, &is_anonymous, cu);
7403 if (is_anonymous)
7404 {
7405 const char *previous_prefix = determine_prefix (die, cu);
7406
7407 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7408 NULL, &objfile->objfile_obstack);
7409 }
7410 }
7411
7412 if (die->child != NULL)
7413 {
7414 struct die_info *child_die = die->child;
7415
7416 while (child_die && child_die->tag)
7417 {
7418 process_die (child_die, cu);
7419 child_die = sibling_die (child_die);
7420 }
7421 }
7422 }
7423
7424 /* Read a Fortran module as type. This DIE can be only a declaration used for
7425 imported module. Still we need that type as local Fortran "use ... only"
7426 declaration imports depend on the created type in determine_prefix. */
7427
7428 static struct type *
7429 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7430 {
7431 struct objfile *objfile = cu->objfile;
7432 char *module_name;
7433 struct type *type;
7434
7435 module_name = dwarf2_name (die, cu);
7436 if (!module_name)
7437 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7438 die->offset);
7439 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7440
7441 /* determine_prefix uses TYPE_TAG_NAME. */
7442 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7443
7444 return set_die_type (die, type, cu);
7445 }
7446
7447 /* Read a Fortran module. */
7448
7449 static void
7450 read_module (struct die_info *die, struct dwarf2_cu *cu)
7451 {
7452 struct die_info *child_die = die->child;
7453
7454 while (child_die && child_die->tag)
7455 {
7456 process_die (child_die, cu);
7457 child_die = sibling_die (child_die);
7458 }
7459 }
7460
7461 /* Return the name of the namespace represented by DIE. Set
7462 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7463 namespace. */
7464
7465 static const char *
7466 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7467 {
7468 struct die_info *current_die;
7469 const char *name = NULL;
7470
7471 /* Loop through the extensions until we find a name. */
7472
7473 for (current_die = die;
7474 current_die != NULL;
7475 current_die = dwarf2_extension (die, &cu))
7476 {
7477 name = dwarf2_name (current_die, cu);
7478 if (name != NULL)
7479 break;
7480 }
7481
7482 /* Is it an anonymous namespace? */
7483
7484 *is_anonymous = (name == NULL);
7485 if (*is_anonymous)
7486 name = "(anonymous namespace)";
7487
7488 return name;
7489 }
7490
7491 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7492 the user defined type vector. */
7493
7494 static struct type *
7495 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7496 {
7497 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7498 struct comp_unit_head *cu_header = &cu->header;
7499 struct type *type;
7500 struct attribute *attr_byte_size;
7501 struct attribute *attr_address_class;
7502 int byte_size, addr_class;
7503 struct type *target_type;
7504
7505 target_type = die_type (die, cu);
7506
7507 /* The die_type call above may have already set the type for this DIE. */
7508 type = get_die_type (die, cu);
7509 if (type)
7510 return type;
7511
7512 type = lookup_pointer_type (target_type);
7513
7514 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7515 if (attr_byte_size)
7516 byte_size = DW_UNSND (attr_byte_size);
7517 else
7518 byte_size = cu_header->addr_size;
7519
7520 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7521 if (attr_address_class)
7522 addr_class = DW_UNSND (attr_address_class);
7523 else
7524 addr_class = DW_ADDR_none;
7525
7526 /* If the pointer size or address class is different than the
7527 default, create a type variant marked as such and set the
7528 length accordingly. */
7529 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7530 {
7531 if (gdbarch_address_class_type_flags_p (gdbarch))
7532 {
7533 int type_flags;
7534
7535 type_flags = gdbarch_address_class_type_flags
7536 (gdbarch, byte_size, addr_class);
7537 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7538 == 0);
7539 type = make_type_with_address_space (type, type_flags);
7540 }
7541 else if (TYPE_LENGTH (type) != byte_size)
7542 {
7543 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7544 }
7545 else
7546 {
7547 /* Should we also complain about unhandled address classes? */
7548 }
7549 }
7550
7551 TYPE_LENGTH (type) = byte_size;
7552 return set_die_type (die, type, cu);
7553 }
7554
7555 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7556 the user defined type vector. */
7557
7558 static struct type *
7559 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7560 {
7561 struct type *type;
7562 struct type *to_type;
7563 struct type *domain;
7564
7565 to_type = die_type (die, cu);
7566 domain = die_containing_type (die, cu);
7567
7568 /* The calls above may have already set the type for this DIE. */
7569 type = get_die_type (die, cu);
7570 if (type)
7571 return type;
7572
7573 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7574 type = lookup_methodptr_type (to_type);
7575 else
7576 type = lookup_memberptr_type (to_type, domain);
7577
7578 return set_die_type (die, type, cu);
7579 }
7580
7581 /* Extract all information from a DW_TAG_reference_type DIE and add to
7582 the user defined type vector. */
7583
7584 static struct type *
7585 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7586 {
7587 struct comp_unit_head *cu_header = &cu->header;
7588 struct type *type, *target_type;
7589 struct attribute *attr;
7590
7591 target_type = die_type (die, cu);
7592
7593 /* The die_type call above may have already set the type for this DIE. */
7594 type = get_die_type (die, cu);
7595 if (type)
7596 return type;
7597
7598 type = lookup_reference_type (target_type);
7599 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7600 if (attr)
7601 {
7602 TYPE_LENGTH (type) = DW_UNSND (attr);
7603 }
7604 else
7605 {
7606 TYPE_LENGTH (type) = cu_header->addr_size;
7607 }
7608 return set_die_type (die, type, cu);
7609 }
7610
7611 static struct type *
7612 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7613 {
7614 struct type *base_type, *cv_type;
7615
7616 base_type = die_type (die, cu);
7617
7618 /* The die_type call above may have already set the type for this DIE. */
7619 cv_type = get_die_type (die, cu);
7620 if (cv_type)
7621 return cv_type;
7622
7623 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7624 return set_die_type (die, cv_type, cu);
7625 }
7626
7627 static struct type *
7628 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7629 {
7630 struct type *base_type, *cv_type;
7631
7632 base_type = die_type (die, cu);
7633
7634 /* The die_type call above may have already set the type for this DIE. */
7635 cv_type = get_die_type (die, cu);
7636 if (cv_type)
7637 return cv_type;
7638
7639 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7640 return set_die_type (die, cv_type, cu);
7641 }
7642
7643 /* Extract all information from a DW_TAG_string_type DIE and add to
7644 the user defined type vector. It isn't really a user defined type,
7645 but it behaves like one, with other DIE's using an AT_user_def_type
7646 attribute to reference it. */
7647
7648 static struct type *
7649 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7650 {
7651 struct objfile *objfile = cu->objfile;
7652 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7653 struct type *type, *range_type, *index_type, *char_type;
7654 struct attribute *attr;
7655 unsigned int length;
7656
7657 attr = dwarf2_attr (die, DW_AT_string_length, cu);
7658 if (attr)
7659 {
7660 length = DW_UNSND (attr);
7661 }
7662 else
7663 {
7664 /* check for the DW_AT_byte_size attribute */
7665 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7666 if (attr)
7667 {
7668 length = DW_UNSND (attr);
7669 }
7670 else
7671 {
7672 length = 1;
7673 }
7674 }
7675
7676 index_type = objfile_type (objfile)->builtin_int;
7677 range_type = create_range_type (NULL, index_type, 1, length);
7678 char_type = language_string_char_type (cu->language_defn, gdbarch);
7679 type = create_string_type (NULL, char_type, range_type);
7680
7681 return set_die_type (die, type, cu);
7682 }
7683
7684 /* Handle DIES due to C code like:
7685
7686 struct foo
7687 {
7688 int (*funcp)(int a, long l);
7689 int b;
7690 };
7691
7692 ('funcp' generates a DW_TAG_subroutine_type DIE)
7693 */
7694
7695 static struct type *
7696 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7697 {
7698 struct type *type; /* Type that this function returns */
7699 struct type *ftype; /* Function that returns above type */
7700 struct attribute *attr;
7701
7702 type = die_type (die, cu);
7703
7704 /* The die_type call above may have already set the type for this DIE. */
7705 ftype = get_die_type (die, cu);
7706 if (ftype)
7707 return ftype;
7708
7709 ftype = lookup_function_type (type);
7710
7711 /* All functions in C++, Pascal and Java have prototypes. */
7712 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7713 if ((attr && (DW_UNSND (attr) != 0))
7714 || cu->language == language_cplus
7715 || cu->language == language_java
7716 || cu->language == language_pascal)
7717 TYPE_PROTOTYPED (ftype) = 1;
7718 else if (producer_is_realview (cu->producer))
7719 /* RealView does not emit DW_AT_prototyped. We can not
7720 distinguish prototyped and unprototyped functions; default to
7721 prototyped, since that is more common in modern code (and
7722 RealView warns about unprototyped functions). */
7723 TYPE_PROTOTYPED (ftype) = 1;
7724
7725 /* Store the calling convention in the type if it's available in
7726 the subroutine die. Otherwise set the calling convention to
7727 the default value DW_CC_normal. */
7728 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7729 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7730
7731 /* We need to add the subroutine type to the die immediately so
7732 we don't infinitely recurse when dealing with parameters
7733 declared as the same subroutine type. */
7734 set_die_type (die, ftype, cu);
7735
7736 if (die->child != NULL)
7737 {
7738 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7739 struct die_info *child_die;
7740 int nparams, iparams;
7741
7742 /* Count the number of parameters.
7743 FIXME: GDB currently ignores vararg functions, but knows about
7744 vararg member functions. */
7745 nparams = 0;
7746 child_die = die->child;
7747 while (child_die && child_die->tag)
7748 {
7749 if (child_die->tag == DW_TAG_formal_parameter)
7750 nparams++;
7751 else if (child_die->tag == DW_TAG_unspecified_parameters)
7752 TYPE_VARARGS (ftype) = 1;
7753 child_die = sibling_die (child_die);
7754 }
7755
7756 /* Allocate storage for parameters and fill them in. */
7757 TYPE_NFIELDS (ftype) = nparams;
7758 TYPE_FIELDS (ftype) = (struct field *)
7759 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7760
7761 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7762 even if we error out during the parameters reading below. */
7763 for (iparams = 0; iparams < nparams; iparams++)
7764 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7765
7766 iparams = 0;
7767 child_die = die->child;
7768 while (child_die && child_die->tag)
7769 {
7770 if (child_die->tag == DW_TAG_formal_parameter)
7771 {
7772 struct type *arg_type;
7773
7774 /* DWARF version 2 has no clean way to discern C++
7775 static and non-static member functions. G++ helps
7776 GDB by marking the first parameter for non-static
7777 member functions (which is the this pointer) as
7778 artificial. We pass this information to
7779 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7780
7781 DWARF version 3 added DW_AT_object_pointer, which GCC
7782 4.5 does not yet generate. */
7783 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7784 if (attr)
7785 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7786 else
7787 {
7788 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7789
7790 /* GCC/43521: In java, the formal parameter
7791 "this" is sometimes not marked with DW_AT_artificial. */
7792 if (cu->language == language_java)
7793 {
7794 const char *name = dwarf2_name (child_die, cu);
7795
7796 if (name && !strcmp (name, "this"))
7797 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7798 }
7799 }
7800 arg_type = die_type (child_die, cu);
7801
7802 /* RealView does not mark THIS as const, which the testsuite
7803 expects. GCC marks THIS as const in method definitions,
7804 but not in the class specifications (GCC PR 43053). */
7805 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7806 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7807 {
7808 int is_this = 0;
7809 struct dwarf2_cu *arg_cu = cu;
7810 const char *name = dwarf2_name (child_die, cu);
7811
7812 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7813 if (attr)
7814 {
7815 /* If the compiler emits this, use it. */
7816 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7817 is_this = 1;
7818 }
7819 else if (name && strcmp (name, "this") == 0)
7820 /* Function definitions will have the argument names. */
7821 is_this = 1;
7822 else if (name == NULL && iparams == 0)
7823 /* Declarations may not have the names, so like
7824 elsewhere in GDB, assume an artificial first
7825 argument is "this". */
7826 is_this = 1;
7827
7828 if (is_this)
7829 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7830 arg_type, 0);
7831 }
7832
7833 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7834 iparams++;
7835 }
7836 child_die = sibling_die (child_die);
7837 }
7838 }
7839
7840 return ftype;
7841 }
7842
7843 static struct type *
7844 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7845 {
7846 struct objfile *objfile = cu->objfile;
7847 const char *name = NULL;
7848 struct type *this_type;
7849
7850 name = dwarf2_full_name (NULL, die, cu);
7851 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7852 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7853 TYPE_NAME (this_type) = (char *) name;
7854 set_die_type (die, this_type, cu);
7855 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7856 return this_type;
7857 }
7858
7859 /* Find a representation of a given base type and install
7860 it in the TYPE field of the die. */
7861
7862 static struct type *
7863 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7864 {
7865 struct objfile *objfile = cu->objfile;
7866 struct type *type;
7867 struct attribute *attr;
7868 int encoding = 0, size = 0;
7869 char *name;
7870 enum type_code code = TYPE_CODE_INT;
7871 int type_flags = 0;
7872 struct type *target_type = NULL;
7873
7874 attr = dwarf2_attr (die, DW_AT_encoding, cu);
7875 if (attr)
7876 {
7877 encoding = DW_UNSND (attr);
7878 }
7879 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7880 if (attr)
7881 {
7882 size = DW_UNSND (attr);
7883 }
7884 name = dwarf2_name (die, cu);
7885 if (!name)
7886 {
7887 complaint (&symfile_complaints,
7888 _("DW_AT_name missing from DW_TAG_base_type"));
7889 }
7890
7891 switch (encoding)
7892 {
7893 case DW_ATE_address:
7894 /* Turn DW_ATE_address into a void * pointer. */
7895 code = TYPE_CODE_PTR;
7896 type_flags |= TYPE_FLAG_UNSIGNED;
7897 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7898 break;
7899 case DW_ATE_boolean:
7900 code = TYPE_CODE_BOOL;
7901 type_flags |= TYPE_FLAG_UNSIGNED;
7902 break;
7903 case DW_ATE_complex_float:
7904 code = TYPE_CODE_COMPLEX;
7905 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7906 break;
7907 case DW_ATE_decimal_float:
7908 code = TYPE_CODE_DECFLOAT;
7909 break;
7910 case DW_ATE_float:
7911 code = TYPE_CODE_FLT;
7912 break;
7913 case DW_ATE_signed:
7914 break;
7915 case DW_ATE_unsigned:
7916 type_flags |= TYPE_FLAG_UNSIGNED;
7917 break;
7918 case DW_ATE_signed_char:
7919 if (cu->language == language_ada || cu->language == language_m2
7920 || cu->language == language_pascal)
7921 code = TYPE_CODE_CHAR;
7922 break;
7923 case DW_ATE_unsigned_char:
7924 if (cu->language == language_ada || cu->language == language_m2
7925 || cu->language == language_pascal)
7926 code = TYPE_CODE_CHAR;
7927 type_flags |= TYPE_FLAG_UNSIGNED;
7928 break;
7929 case DW_ATE_UTF:
7930 /* We just treat this as an integer and then recognize the
7931 type by name elsewhere. */
7932 break;
7933
7934 default:
7935 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7936 dwarf_type_encoding_name (encoding));
7937 break;
7938 }
7939
7940 type = init_type (code, size, type_flags, NULL, objfile);
7941 TYPE_NAME (type) = name;
7942 TYPE_TARGET_TYPE (type) = target_type;
7943
7944 if (name && strcmp (name, "char") == 0)
7945 TYPE_NOSIGN (type) = 1;
7946
7947 return set_die_type (die, type, cu);
7948 }
7949
7950 /* Read the given DW_AT_subrange DIE. */
7951
7952 static struct type *
7953 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7954 {
7955 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7956 struct type *base_type;
7957 struct type *range_type;
7958 struct attribute *attr;
7959 LONGEST low = 0;
7960 LONGEST high = -1;
7961 char *name;
7962 LONGEST negative_mask;
7963
7964 base_type = die_type (die, cu);
7965 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
7966 check_typedef (base_type);
7967
7968 /* The die_type call above may have already set the type for this DIE. */
7969 range_type = get_die_type (die, cu);
7970 if (range_type)
7971 return range_type;
7972
7973 if (cu->language == language_fortran)
7974 {
7975 /* FORTRAN implies a lower bound of 1, if not given. */
7976 low = 1;
7977 }
7978
7979 /* FIXME: For variable sized arrays either of these could be
7980 a variable rather than a constant value. We'll allow it,
7981 but we don't know how to handle it. */
7982 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
7983 if (attr)
7984 low = dwarf2_get_attr_constant_value (attr, 0);
7985
7986 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
7987 if (attr)
7988 {
7989 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
7990 {
7991 /* GCC encodes arrays with unspecified or dynamic length
7992 with a DW_FORM_block1 attribute or a reference attribute.
7993 FIXME: GDB does not yet know how to handle dynamic
7994 arrays properly, treat them as arrays with unspecified
7995 length for now.
7996
7997 FIXME: jimb/2003-09-22: GDB does not really know
7998 how to handle arrays of unspecified length
7999 either; we just represent them as zero-length
8000 arrays. Choose an appropriate upper bound given
8001 the lower bound we've computed above. */
8002 high = low - 1;
8003 }
8004 else
8005 high = dwarf2_get_attr_constant_value (attr, 1);
8006 }
8007 else
8008 {
8009 attr = dwarf2_attr (die, DW_AT_count, cu);
8010 if (attr)
8011 {
8012 int count = dwarf2_get_attr_constant_value (attr, 1);
8013 high = low + count - 1;
8014 }
8015 }
8016
8017 /* Dwarf-2 specifications explicitly allows to create subrange types
8018 without specifying a base type.
8019 In that case, the base type must be set to the type of
8020 the lower bound, upper bound or count, in that order, if any of these
8021 three attributes references an object that has a type.
8022 If no base type is found, the Dwarf-2 specifications say that
8023 a signed integer type of size equal to the size of an address should
8024 be used.
8025 For the following C code: `extern char gdb_int [];'
8026 GCC produces an empty range DIE.
8027 FIXME: muller/2010-05-28: Possible references to object for low bound,
8028 high bound or count are not yet handled by this code.
8029 */
8030 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8031 {
8032 struct objfile *objfile = cu->objfile;
8033 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8034 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8035 struct type *int_type = objfile_type (objfile)->builtin_int;
8036
8037 /* Test "int", "long int", and "long long int" objfile types,
8038 and select the first one having a size above or equal to the
8039 architecture address size. */
8040 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8041 base_type = int_type;
8042 else
8043 {
8044 int_type = objfile_type (objfile)->builtin_long;
8045 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8046 base_type = int_type;
8047 else
8048 {
8049 int_type = objfile_type (objfile)->builtin_long_long;
8050 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8051 base_type = int_type;
8052 }
8053 }
8054 }
8055
8056 negative_mask =
8057 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8058 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8059 low |= negative_mask;
8060 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8061 high |= negative_mask;
8062
8063 range_type = create_range_type (NULL, base_type, low, high);
8064
8065 /* Mark arrays with dynamic length at least as an array of unspecified
8066 length. GDB could check the boundary but before it gets implemented at
8067 least allow accessing the array elements. */
8068 if (attr && attr->form == DW_FORM_block1)
8069 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8070
8071 name = dwarf2_name (die, cu);
8072 if (name)
8073 TYPE_NAME (range_type) = name;
8074
8075 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8076 if (attr)
8077 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8078
8079 set_die_type (die, range_type, cu);
8080
8081 /* set_die_type should be already done. */
8082 set_descriptive_type (range_type, die, cu);
8083
8084 return range_type;
8085 }
8086
8087 static struct type *
8088 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8089 {
8090 struct type *type;
8091
8092 /* For now, we only support the C meaning of an unspecified type: void. */
8093
8094 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8095 TYPE_NAME (type) = dwarf2_name (die, cu);
8096
8097 return set_die_type (die, type, cu);
8098 }
8099
8100 /* Trivial hash function for die_info: the hash value of a DIE
8101 is its offset in .debug_info for this objfile. */
8102
8103 static hashval_t
8104 die_hash (const void *item)
8105 {
8106 const struct die_info *die = item;
8107
8108 return die->offset;
8109 }
8110
8111 /* Trivial comparison function for die_info structures: two DIEs
8112 are equal if they have the same offset. */
8113
8114 static int
8115 die_eq (const void *item_lhs, const void *item_rhs)
8116 {
8117 const struct die_info *die_lhs = item_lhs;
8118 const struct die_info *die_rhs = item_rhs;
8119
8120 return die_lhs->offset == die_rhs->offset;
8121 }
8122
8123 /* Read a whole compilation unit into a linked list of dies. */
8124
8125 static struct die_info *
8126 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8127 {
8128 struct die_reader_specs reader_specs;
8129 int read_abbrevs = 0;
8130 struct cleanup *back_to = NULL;
8131 struct die_info *die;
8132
8133 if (cu->dwarf2_abbrevs == NULL)
8134 {
8135 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8136 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8137 read_abbrevs = 1;
8138 }
8139
8140 gdb_assert (cu->die_hash == NULL);
8141 cu->die_hash
8142 = htab_create_alloc_ex (cu->header.length / 12,
8143 die_hash,
8144 die_eq,
8145 NULL,
8146 &cu->comp_unit_obstack,
8147 hashtab_obstack_allocate,
8148 dummy_obstack_deallocate);
8149
8150 init_cu_die_reader (&reader_specs, cu);
8151
8152 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8153
8154 if (read_abbrevs)
8155 do_cleanups (back_to);
8156
8157 return die;
8158 }
8159
8160 /* Main entry point for reading a DIE and all children.
8161 Read the DIE and dump it if requested. */
8162
8163 static struct die_info *
8164 read_die_and_children (const struct die_reader_specs *reader,
8165 gdb_byte *info_ptr,
8166 gdb_byte **new_info_ptr,
8167 struct die_info *parent)
8168 {
8169 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8170 new_info_ptr, parent);
8171
8172 if (dwarf2_die_debug)
8173 {
8174 fprintf_unfiltered (gdb_stdlog,
8175 "\nRead die from %s of %s:\n",
8176 reader->buffer == dwarf2_per_objfile->info.buffer
8177 ? ".debug_info"
8178 : reader->buffer == dwarf2_per_objfile->types.buffer
8179 ? ".debug_types"
8180 : "unknown section",
8181 reader->abfd->filename);
8182 dump_die (result, dwarf2_die_debug);
8183 }
8184
8185 return result;
8186 }
8187
8188 /* Read a single die and all its descendents. Set the die's sibling
8189 field to NULL; set other fields in the die correctly, and set all
8190 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8191 location of the info_ptr after reading all of those dies. PARENT
8192 is the parent of the die in question. */
8193
8194 static struct die_info *
8195 read_die_and_children_1 (const struct die_reader_specs *reader,
8196 gdb_byte *info_ptr,
8197 gdb_byte **new_info_ptr,
8198 struct die_info *parent)
8199 {
8200 struct die_info *die;
8201 gdb_byte *cur_ptr;
8202 int has_children;
8203
8204 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8205 if (die == NULL)
8206 {
8207 *new_info_ptr = cur_ptr;
8208 return NULL;
8209 }
8210 store_in_ref_table (die, reader->cu);
8211
8212 if (has_children)
8213 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8214 else
8215 {
8216 die->child = NULL;
8217 *new_info_ptr = cur_ptr;
8218 }
8219
8220 die->sibling = NULL;
8221 die->parent = parent;
8222 return die;
8223 }
8224
8225 /* Read a die, all of its descendents, and all of its siblings; set
8226 all of the fields of all of the dies correctly. Arguments are as
8227 in read_die_and_children. */
8228
8229 static struct die_info *
8230 read_die_and_siblings (const struct die_reader_specs *reader,
8231 gdb_byte *info_ptr,
8232 gdb_byte **new_info_ptr,
8233 struct die_info *parent)
8234 {
8235 struct die_info *first_die, *last_sibling;
8236 gdb_byte *cur_ptr;
8237
8238 cur_ptr = info_ptr;
8239 first_die = last_sibling = NULL;
8240
8241 while (1)
8242 {
8243 struct die_info *die
8244 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8245
8246 if (die == NULL)
8247 {
8248 *new_info_ptr = cur_ptr;
8249 return first_die;
8250 }
8251
8252 if (!first_die)
8253 first_die = die;
8254 else
8255 last_sibling->sibling = die;
8256
8257 last_sibling = die;
8258 }
8259 }
8260
8261 /* Read the die from the .debug_info section buffer. Set DIEP to
8262 point to a newly allocated die with its information, except for its
8263 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8264 whether the die has children or not. */
8265
8266 static gdb_byte *
8267 read_full_die (const struct die_reader_specs *reader,
8268 struct die_info **diep, gdb_byte *info_ptr,
8269 int *has_children)
8270 {
8271 unsigned int abbrev_number, bytes_read, i, offset;
8272 struct abbrev_info *abbrev;
8273 struct die_info *die;
8274 struct dwarf2_cu *cu = reader->cu;
8275 bfd *abfd = reader->abfd;
8276
8277 offset = info_ptr - reader->buffer;
8278 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8279 info_ptr += bytes_read;
8280 if (!abbrev_number)
8281 {
8282 *diep = NULL;
8283 *has_children = 0;
8284 return info_ptr;
8285 }
8286
8287 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8288 if (!abbrev)
8289 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8290 abbrev_number,
8291 bfd_get_filename (abfd));
8292
8293 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8294 die->offset = offset;
8295 die->tag = abbrev->tag;
8296 die->abbrev = abbrev_number;
8297
8298 die->num_attrs = abbrev->num_attrs;
8299
8300 for (i = 0; i < abbrev->num_attrs; ++i)
8301 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8302 abfd, info_ptr, cu);
8303
8304 *diep = die;
8305 *has_children = abbrev->has_children;
8306 return info_ptr;
8307 }
8308
8309 /* In DWARF version 2, the description of the debugging information is
8310 stored in a separate .debug_abbrev section. Before we read any
8311 dies from a section we read in all abbreviations and install them
8312 in a hash table. This function also sets flags in CU describing
8313 the data found in the abbrev table. */
8314
8315 static void
8316 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8317 {
8318 struct comp_unit_head *cu_header = &cu->header;
8319 gdb_byte *abbrev_ptr;
8320 struct abbrev_info *cur_abbrev;
8321 unsigned int abbrev_number, bytes_read, abbrev_name;
8322 unsigned int abbrev_form, hash_number;
8323 struct attr_abbrev *cur_attrs;
8324 unsigned int allocated_attrs;
8325
8326 /* Initialize dwarf2 abbrevs */
8327 obstack_init (&cu->abbrev_obstack);
8328 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8329 (ABBREV_HASH_SIZE
8330 * sizeof (struct abbrev_info *)));
8331 memset (cu->dwarf2_abbrevs, 0,
8332 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8333
8334 dwarf2_read_section (dwarf2_per_objfile->objfile,
8335 &dwarf2_per_objfile->abbrev);
8336 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8337 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8338 abbrev_ptr += bytes_read;
8339
8340 allocated_attrs = ATTR_ALLOC_CHUNK;
8341 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8342
8343 /* loop until we reach an abbrev number of 0 */
8344 while (abbrev_number)
8345 {
8346 cur_abbrev = dwarf_alloc_abbrev (cu);
8347
8348 /* read in abbrev header */
8349 cur_abbrev->number = abbrev_number;
8350 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8351 abbrev_ptr += bytes_read;
8352 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8353 abbrev_ptr += 1;
8354
8355 if (cur_abbrev->tag == DW_TAG_namespace)
8356 cu->has_namespace_info = 1;
8357
8358 /* now read in declarations */
8359 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8360 abbrev_ptr += bytes_read;
8361 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8362 abbrev_ptr += bytes_read;
8363 while (abbrev_name)
8364 {
8365 if (cur_abbrev->num_attrs == allocated_attrs)
8366 {
8367 allocated_attrs += ATTR_ALLOC_CHUNK;
8368 cur_attrs
8369 = xrealloc (cur_attrs, (allocated_attrs
8370 * sizeof (struct attr_abbrev)));
8371 }
8372
8373 /* Record whether this compilation unit might have
8374 inter-compilation-unit references. If we don't know what form
8375 this attribute will have, then it might potentially be a
8376 DW_FORM_ref_addr, so we conservatively expect inter-CU
8377 references. */
8378
8379 if (abbrev_form == DW_FORM_ref_addr
8380 || abbrev_form == DW_FORM_indirect)
8381 cu->has_form_ref_addr = 1;
8382
8383 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8384 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8385 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8386 abbrev_ptr += bytes_read;
8387 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8388 abbrev_ptr += bytes_read;
8389 }
8390
8391 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8392 (cur_abbrev->num_attrs
8393 * sizeof (struct attr_abbrev)));
8394 memcpy (cur_abbrev->attrs, cur_attrs,
8395 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8396
8397 hash_number = abbrev_number % ABBREV_HASH_SIZE;
8398 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8399 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8400
8401 /* Get next abbreviation.
8402 Under Irix6 the abbreviations for a compilation unit are not
8403 always properly terminated with an abbrev number of 0.
8404 Exit loop if we encounter an abbreviation which we have
8405 already read (which means we are about to read the abbreviations
8406 for the next compile unit) or if the end of the abbreviation
8407 table is reached. */
8408 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8409 >= dwarf2_per_objfile->abbrev.size)
8410 break;
8411 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8412 abbrev_ptr += bytes_read;
8413 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8414 break;
8415 }
8416
8417 xfree (cur_attrs);
8418 }
8419
8420 /* Release the memory used by the abbrev table for a compilation unit. */
8421
8422 static void
8423 dwarf2_free_abbrev_table (void *ptr_to_cu)
8424 {
8425 struct dwarf2_cu *cu = ptr_to_cu;
8426
8427 obstack_free (&cu->abbrev_obstack, NULL);
8428 cu->dwarf2_abbrevs = NULL;
8429 }
8430
8431 /* Lookup an abbrev_info structure in the abbrev hash table. */
8432
8433 static struct abbrev_info *
8434 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8435 {
8436 unsigned int hash_number;
8437 struct abbrev_info *abbrev;
8438
8439 hash_number = number % ABBREV_HASH_SIZE;
8440 abbrev = cu->dwarf2_abbrevs[hash_number];
8441
8442 while (abbrev)
8443 {
8444 if (abbrev->number == number)
8445 return abbrev;
8446 else
8447 abbrev = abbrev->next;
8448 }
8449 return NULL;
8450 }
8451
8452 /* Returns nonzero if TAG represents a type that we might generate a partial
8453 symbol for. */
8454
8455 static int
8456 is_type_tag_for_partial (int tag)
8457 {
8458 switch (tag)
8459 {
8460 #if 0
8461 /* Some types that would be reasonable to generate partial symbols for,
8462 that we don't at present. */
8463 case DW_TAG_array_type:
8464 case DW_TAG_file_type:
8465 case DW_TAG_ptr_to_member_type:
8466 case DW_TAG_set_type:
8467 case DW_TAG_string_type:
8468 case DW_TAG_subroutine_type:
8469 #endif
8470 case DW_TAG_base_type:
8471 case DW_TAG_class_type:
8472 case DW_TAG_interface_type:
8473 case DW_TAG_enumeration_type:
8474 case DW_TAG_structure_type:
8475 case DW_TAG_subrange_type:
8476 case DW_TAG_typedef:
8477 case DW_TAG_union_type:
8478 return 1;
8479 default:
8480 return 0;
8481 }
8482 }
8483
8484 /* Load all DIEs that are interesting for partial symbols into memory. */
8485
8486 static struct partial_die_info *
8487 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8488 int building_psymtab, struct dwarf2_cu *cu)
8489 {
8490 struct partial_die_info *part_die;
8491 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8492 struct abbrev_info *abbrev;
8493 unsigned int bytes_read;
8494 unsigned int load_all = 0;
8495
8496 int nesting_level = 1;
8497
8498 parent_die = NULL;
8499 last_die = NULL;
8500
8501 if (cu->per_cu && cu->per_cu->load_all_dies)
8502 load_all = 1;
8503
8504 cu->partial_dies
8505 = htab_create_alloc_ex (cu->header.length / 12,
8506 partial_die_hash,
8507 partial_die_eq,
8508 NULL,
8509 &cu->comp_unit_obstack,
8510 hashtab_obstack_allocate,
8511 dummy_obstack_deallocate);
8512
8513 part_die = obstack_alloc (&cu->comp_unit_obstack,
8514 sizeof (struct partial_die_info));
8515
8516 while (1)
8517 {
8518 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8519
8520 /* A NULL abbrev means the end of a series of children. */
8521 if (abbrev == NULL)
8522 {
8523 if (--nesting_level == 0)
8524 {
8525 /* PART_DIE was probably the last thing allocated on the
8526 comp_unit_obstack, so we could call obstack_free
8527 here. We don't do that because the waste is small,
8528 and will be cleaned up when we're done with this
8529 compilation unit. This way, we're also more robust
8530 against other users of the comp_unit_obstack. */
8531 return first_die;
8532 }
8533 info_ptr += bytes_read;
8534 last_die = parent_die;
8535 parent_die = parent_die->die_parent;
8536 continue;
8537 }
8538
8539 /* Check for template arguments. We never save these; if
8540 they're seen, we just mark the parent, and go on our way. */
8541 if (parent_die != NULL
8542 && cu->language == language_cplus
8543 && (abbrev->tag == DW_TAG_template_type_param
8544 || abbrev->tag == DW_TAG_template_value_param))
8545 {
8546 parent_die->has_template_arguments = 1;
8547
8548 if (!load_all)
8549 {
8550 /* We don't need a partial DIE for the template argument. */
8551 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8552 cu);
8553 continue;
8554 }
8555 }
8556
8557 /* We only recurse into subprograms looking for template arguments.
8558 Skip their other children. */
8559 if (!load_all
8560 && cu->language == language_cplus
8561 && parent_die != NULL
8562 && parent_die->tag == DW_TAG_subprogram)
8563 {
8564 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8565 continue;
8566 }
8567
8568 /* Check whether this DIE is interesting enough to save. Normally
8569 we would not be interested in members here, but there may be
8570 later variables referencing them via DW_AT_specification (for
8571 static members). */
8572 if (!load_all
8573 && !is_type_tag_for_partial (abbrev->tag)
8574 && abbrev->tag != DW_TAG_constant
8575 && abbrev->tag != DW_TAG_enumerator
8576 && abbrev->tag != DW_TAG_subprogram
8577 && abbrev->tag != DW_TAG_lexical_block
8578 && abbrev->tag != DW_TAG_variable
8579 && abbrev->tag != DW_TAG_namespace
8580 && abbrev->tag != DW_TAG_module
8581 && abbrev->tag != DW_TAG_member)
8582 {
8583 /* Otherwise we skip to the next sibling, if any. */
8584 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8585 continue;
8586 }
8587
8588 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8589 buffer, info_ptr, cu);
8590
8591 /* This two-pass algorithm for processing partial symbols has a
8592 high cost in cache pressure. Thus, handle some simple cases
8593 here which cover the majority of C partial symbols. DIEs
8594 which neither have specification tags in them, nor could have
8595 specification tags elsewhere pointing at them, can simply be
8596 processed and discarded.
8597
8598 This segment is also optional; scan_partial_symbols and
8599 add_partial_symbol will handle these DIEs if we chain
8600 them in normally. When compilers which do not emit large
8601 quantities of duplicate debug information are more common,
8602 this code can probably be removed. */
8603
8604 /* Any complete simple types at the top level (pretty much all
8605 of them, for a language without namespaces), can be processed
8606 directly. */
8607 if (parent_die == NULL
8608 && part_die->has_specification == 0
8609 && part_die->is_declaration == 0
8610 && (part_die->tag == DW_TAG_typedef
8611 || part_die->tag == DW_TAG_base_type
8612 || part_die->tag == DW_TAG_subrange_type))
8613 {
8614 if (building_psymtab && part_die->name != NULL)
8615 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8616 VAR_DOMAIN, LOC_TYPEDEF,
8617 &cu->objfile->static_psymbols,
8618 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8619 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8620 continue;
8621 }
8622
8623 /* If we're at the second level, and we're an enumerator, and
8624 our parent has no specification (meaning possibly lives in a
8625 namespace elsewhere), then we can add the partial symbol now
8626 instead of queueing it. */
8627 if (part_die->tag == DW_TAG_enumerator
8628 && parent_die != NULL
8629 && parent_die->die_parent == NULL
8630 && parent_die->tag == DW_TAG_enumeration_type
8631 && parent_die->has_specification == 0)
8632 {
8633 if (part_die->name == NULL)
8634 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8635 else if (building_psymtab)
8636 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8637 VAR_DOMAIN, LOC_CONST,
8638 (cu->language == language_cplus
8639 || cu->language == language_java)
8640 ? &cu->objfile->global_psymbols
8641 : &cu->objfile->static_psymbols,
8642 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8643
8644 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8645 continue;
8646 }
8647
8648 /* We'll save this DIE so link it in. */
8649 part_die->die_parent = parent_die;
8650 part_die->die_sibling = NULL;
8651 part_die->die_child = NULL;
8652
8653 if (last_die && last_die == parent_die)
8654 last_die->die_child = part_die;
8655 else if (last_die)
8656 last_die->die_sibling = part_die;
8657
8658 last_die = part_die;
8659
8660 if (first_die == NULL)
8661 first_die = part_die;
8662
8663 /* Maybe add the DIE to the hash table. Not all DIEs that we
8664 find interesting need to be in the hash table, because we
8665 also have the parent/sibling/child chains; only those that we
8666 might refer to by offset later during partial symbol reading.
8667
8668 For now this means things that might have be the target of a
8669 DW_AT_specification, DW_AT_abstract_origin, or
8670 DW_AT_extension. DW_AT_extension will refer only to
8671 namespaces; DW_AT_abstract_origin refers to functions (and
8672 many things under the function DIE, but we do not recurse
8673 into function DIEs during partial symbol reading) and
8674 possibly variables as well; DW_AT_specification refers to
8675 declarations. Declarations ought to have the DW_AT_declaration
8676 flag. It happens that GCC forgets to put it in sometimes, but
8677 only for functions, not for types.
8678
8679 Adding more things than necessary to the hash table is harmless
8680 except for the performance cost. Adding too few will result in
8681 wasted time in find_partial_die, when we reread the compilation
8682 unit with load_all_dies set. */
8683
8684 if (load_all
8685 || abbrev->tag == DW_TAG_constant
8686 || abbrev->tag == DW_TAG_subprogram
8687 || abbrev->tag == DW_TAG_variable
8688 || abbrev->tag == DW_TAG_namespace
8689 || part_die->is_declaration)
8690 {
8691 void **slot;
8692
8693 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8694 part_die->offset, INSERT);
8695 *slot = part_die;
8696 }
8697
8698 part_die = obstack_alloc (&cu->comp_unit_obstack,
8699 sizeof (struct partial_die_info));
8700
8701 /* For some DIEs we want to follow their children (if any). For C
8702 we have no reason to follow the children of structures; for other
8703 languages we have to, so that we can get at method physnames
8704 to infer fully qualified class names, for DW_AT_specification,
8705 and for C++ template arguments. For C++, we also look one level
8706 inside functions to find template arguments (if the name of the
8707 function does not already contain the template arguments).
8708
8709 For Ada, we need to scan the children of subprograms and lexical
8710 blocks as well because Ada allows the definition of nested
8711 entities that could be interesting for the debugger, such as
8712 nested subprograms for instance. */
8713 if (last_die->has_children
8714 && (load_all
8715 || last_die->tag == DW_TAG_namespace
8716 || last_die->tag == DW_TAG_module
8717 || last_die->tag == DW_TAG_enumeration_type
8718 || (cu->language == language_cplus
8719 && last_die->tag == DW_TAG_subprogram
8720 && (last_die->name == NULL
8721 || strchr (last_die->name, '<') == NULL))
8722 || (cu->language != language_c
8723 && (last_die->tag == DW_TAG_class_type
8724 || last_die->tag == DW_TAG_interface_type
8725 || last_die->tag == DW_TAG_structure_type
8726 || last_die->tag == DW_TAG_union_type))
8727 || (cu->language == language_ada
8728 && (last_die->tag == DW_TAG_subprogram
8729 || last_die->tag == DW_TAG_lexical_block))))
8730 {
8731 nesting_level++;
8732 parent_die = last_die;
8733 continue;
8734 }
8735
8736 /* Otherwise we skip to the next sibling, if any. */
8737 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8738
8739 /* Back to the top, do it again. */
8740 }
8741 }
8742
8743 /* Read a minimal amount of information into the minimal die structure. */
8744
8745 static gdb_byte *
8746 read_partial_die (struct partial_die_info *part_die,
8747 struct abbrev_info *abbrev,
8748 unsigned int abbrev_len, bfd *abfd,
8749 gdb_byte *buffer, gdb_byte *info_ptr,
8750 struct dwarf2_cu *cu)
8751 {
8752 unsigned int i;
8753 struct attribute attr;
8754 int has_low_pc_attr = 0;
8755 int has_high_pc_attr = 0;
8756
8757 memset (part_die, 0, sizeof (struct partial_die_info));
8758
8759 part_die->offset = info_ptr - buffer;
8760
8761 info_ptr += abbrev_len;
8762
8763 if (abbrev == NULL)
8764 return info_ptr;
8765
8766 part_die->tag = abbrev->tag;
8767 part_die->has_children = abbrev->has_children;
8768
8769 for (i = 0; i < abbrev->num_attrs; ++i)
8770 {
8771 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8772
8773 /* Store the data if it is of an attribute we want to keep in a
8774 partial symbol table. */
8775 switch (attr.name)
8776 {
8777 case DW_AT_name:
8778 switch (part_die->tag)
8779 {
8780 case DW_TAG_compile_unit:
8781 case DW_TAG_type_unit:
8782 /* Compilation units have a DW_AT_name that is a filename, not
8783 a source language identifier. */
8784 case DW_TAG_enumeration_type:
8785 case DW_TAG_enumerator:
8786 /* These tags always have simple identifiers already; no need
8787 to canonicalize them. */
8788 part_die->name = DW_STRING (&attr);
8789 break;
8790 default:
8791 part_die->name
8792 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8793 &cu->objfile->objfile_obstack);
8794 break;
8795 }
8796 break;
8797 case DW_AT_linkage_name:
8798 case DW_AT_MIPS_linkage_name:
8799 /* Note that both forms of linkage name might appear. We
8800 assume they will be the same, and we only store the last
8801 one we see. */
8802 if (cu->language == language_ada)
8803 part_die->name = DW_STRING (&attr);
8804 break;
8805 case DW_AT_low_pc:
8806 has_low_pc_attr = 1;
8807 part_die->lowpc = DW_ADDR (&attr);
8808 break;
8809 case DW_AT_high_pc:
8810 has_high_pc_attr = 1;
8811 part_die->highpc = DW_ADDR (&attr);
8812 break;
8813 case DW_AT_location:
8814 /* Support the .debug_loc offsets */
8815 if (attr_form_is_block (&attr))
8816 {
8817 part_die->locdesc = DW_BLOCK (&attr);
8818 }
8819 else if (attr_form_is_section_offset (&attr))
8820 {
8821 dwarf2_complex_location_expr_complaint ();
8822 }
8823 else
8824 {
8825 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8826 "partial symbol information");
8827 }
8828 break;
8829 case DW_AT_external:
8830 part_die->is_external = DW_UNSND (&attr);
8831 break;
8832 case DW_AT_declaration:
8833 part_die->is_declaration = DW_UNSND (&attr);
8834 break;
8835 case DW_AT_type:
8836 part_die->has_type = 1;
8837 break;
8838 case DW_AT_abstract_origin:
8839 case DW_AT_specification:
8840 case DW_AT_extension:
8841 part_die->has_specification = 1;
8842 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8843 break;
8844 case DW_AT_sibling:
8845 /* Ignore absolute siblings, they might point outside of
8846 the current compile unit. */
8847 if (attr.form == DW_FORM_ref_addr)
8848 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8849 else
8850 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8851 break;
8852 case DW_AT_byte_size:
8853 part_die->has_byte_size = 1;
8854 break;
8855 case DW_AT_calling_convention:
8856 /* DWARF doesn't provide a way to identify a program's source-level
8857 entry point. DW_AT_calling_convention attributes are only meant
8858 to describe functions' calling conventions.
8859
8860 However, because it's a necessary piece of information in
8861 Fortran, and because DW_CC_program is the only piece of debugging
8862 information whose definition refers to a 'main program' at all,
8863 several compilers have begun marking Fortran main programs with
8864 DW_CC_program --- even when those functions use the standard
8865 calling conventions.
8866
8867 So until DWARF specifies a way to provide this information and
8868 compilers pick up the new representation, we'll support this
8869 practice. */
8870 if (DW_UNSND (&attr) == DW_CC_program
8871 && cu->language == language_fortran)
8872 set_main_name (part_die->name);
8873 break;
8874 default:
8875 break;
8876 }
8877 }
8878
8879 /* When using the GNU linker, .gnu.linkonce. sections are used to
8880 eliminate duplicate copies of functions and vtables and such.
8881 The linker will arbitrarily choose one and discard the others.
8882 The AT_*_pc values for such functions refer to local labels in
8883 these sections. If the section from that file was discarded, the
8884 labels are not in the output, so the relocs get a value of 0.
8885 If this is a discarded function, mark the pc bounds as invalid,
8886 so that GDB will ignore it. */
8887 if (has_low_pc_attr && has_high_pc_attr
8888 && part_die->lowpc < part_die->highpc
8889 && (part_die->lowpc != 0
8890 || dwarf2_per_objfile->has_section_at_zero))
8891 part_die->has_pc_info = 1;
8892
8893 return info_ptr;
8894 }
8895
8896 /* Find a cached partial DIE at OFFSET in CU. */
8897
8898 static struct partial_die_info *
8899 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8900 {
8901 struct partial_die_info *lookup_die = NULL;
8902 struct partial_die_info part_die;
8903
8904 part_die.offset = offset;
8905 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8906
8907 return lookup_die;
8908 }
8909
8910 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8911 except in the case of .debug_types DIEs which do not reference
8912 outside their CU (they do however referencing other types via
8913 DW_FORM_sig8). */
8914
8915 static struct partial_die_info *
8916 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8917 {
8918 struct dwarf2_per_cu_data *per_cu = NULL;
8919 struct partial_die_info *pd = NULL;
8920
8921 if (cu->per_cu->from_debug_types)
8922 {
8923 pd = find_partial_die_in_comp_unit (offset, cu);
8924 if (pd != NULL)
8925 return pd;
8926 goto not_found;
8927 }
8928
8929 if (offset_in_cu_p (&cu->header, offset))
8930 {
8931 pd = find_partial_die_in_comp_unit (offset, cu);
8932 if (pd != NULL)
8933 return pd;
8934 }
8935
8936 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8937
8938 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8939 load_partial_comp_unit (per_cu, cu->objfile);
8940
8941 per_cu->cu->last_used = 0;
8942 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8943
8944 if (pd == NULL && per_cu->load_all_dies == 0)
8945 {
8946 struct cleanup *back_to;
8947 struct partial_die_info comp_unit_die;
8948 struct abbrev_info *abbrev;
8949 unsigned int bytes_read;
8950 char *info_ptr;
8951
8952 per_cu->load_all_dies = 1;
8953
8954 /* Re-read the DIEs. */
8955 back_to = make_cleanup (null_cleanup, 0);
8956 if (per_cu->cu->dwarf2_abbrevs == NULL)
8957 {
8958 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
8959 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
8960 }
8961 info_ptr = (dwarf2_per_objfile->info.buffer
8962 + per_cu->cu->header.offset
8963 + per_cu->cu->header.first_die_offset);
8964 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
8965 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
8966 per_cu->cu->objfile->obfd,
8967 dwarf2_per_objfile->info.buffer, info_ptr,
8968 per_cu->cu);
8969 if (comp_unit_die.has_children)
8970 load_partial_dies (per_cu->cu->objfile->obfd,
8971 dwarf2_per_objfile->info.buffer, info_ptr,
8972 0, per_cu->cu);
8973 do_cleanups (back_to);
8974
8975 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8976 }
8977
8978 not_found:
8979
8980 if (pd == NULL)
8981 internal_error (__FILE__, __LINE__,
8982 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
8983 offset, bfd_get_filename (cu->objfile->obfd));
8984 return pd;
8985 }
8986
8987 /* Adjust PART_DIE before generating a symbol for it. This function
8988 may set the is_external flag or change the DIE's name. */
8989
8990 static void
8991 fixup_partial_die (struct partial_die_info *part_die,
8992 struct dwarf2_cu *cu)
8993 {
8994 /* If we found a reference attribute and the DIE has no name, try
8995 to find a name in the referred to DIE. */
8996
8997 if (part_die->name == NULL && part_die->has_specification)
8998 {
8999 struct partial_die_info *spec_die;
9000
9001 spec_die = find_partial_die (part_die->spec_offset, cu);
9002
9003 fixup_partial_die (spec_die, cu);
9004
9005 if (spec_die->name)
9006 {
9007 part_die->name = spec_die->name;
9008
9009 /* Copy DW_AT_external attribute if it is set. */
9010 if (spec_die->is_external)
9011 part_die->is_external = spec_die->is_external;
9012 }
9013 }
9014
9015 /* Set default names for some unnamed DIEs. */
9016
9017 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9018 part_die->name = "(anonymous namespace)";
9019
9020 if (part_die->tag == DW_TAG_structure_type
9021 || part_die->tag == DW_TAG_class_type
9022 || part_die->tag == DW_TAG_union_type)
9023 guess_structure_name (part_die, cu);
9024 }
9025
9026 /* Read an attribute value described by an attribute form. */
9027
9028 static gdb_byte *
9029 read_attribute_value (struct attribute *attr, unsigned form,
9030 bfd *abfd, gdb_byte *info_ptr,
9031 struct dwarf2_cu *cu)
9032 {
9033 struct comp_unit_head *cu_header = &cu->header;
9034 unsigned int bytes_read;
9035 struct dwarf_block *blk;
9036
9037 attr->form = form;
9038 switch (form)
9039 {
9040 case DW_FORM_ref_addr:
9041 if (cu->header.version == 2)
9042 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9043 else
9044 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9045 info_ptr += bytes_read;
9046 break;
9047 case DW_FORM_addr:
9048 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9049 info_ptr += bytes_read;
9050 break;
9051 case DW_FORM_block2:
9052 blk = dwarf_alloc_block (cu);
9053 blk->size = read_2_bytes (abfd, info_ptr);
9054 info_ptr += 2;
9055 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9056 info_ptr += blk->size;
9057 DW_BLOCK (attr) = blk;
9058 break;
9059 case DW_FORM_block4:
9060 blk = dwarf_alloc_block (cu);
9061 blk->size = read_4_bytes (abfd, info_ptr);
9062 info_ptr += 4;
9063 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9064 info_ptr += blk->size;
9065 DW_BLOCK (attr) = blk;
9066 break;
9067 case DW_FORM_data2:
9068 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9069 info_ptr += 2;
9070 break;
9071 case DW_FORM_data4:
9072 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9073 info_ptr += 4;
9074 break;
9075 case DW_FORM_data8:
9076 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9077 info_ptr += 8;
9078 break;
9079 case DW_FORM_sec_offset:
9080 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9081 info_ptr += bytes_read;
9082 break;
9083 case DW_FORM_string:
9084 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9085 DW_STRING_IS_CANONICAL (attr) = 0;
9086 info_ptr += bytes_read;
9087 break;
9088 case DW_FORM_strp:
9089 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9090 &bytes_read);
9091 DW_STRING_IS_CANONICAL (attr) = 0;
9092 info_ptr += bytes_read;
9093 break;
9094 case DW_FORM_exprloc:
9095 case DW_FORM_block:
9096 blk = dwarf_alloc_block (cu);
9097 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9098 info_ptr += bytes_read;
9099 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9100 info_ptr += blk->size;
9101 DW_BLOCK (attr) = blk;
9102 break;
9103 case DW_FORM_block1:
9104 blk = dwarf_alloc_block (cu);
9105 blk->size = read_1_byte (abfd, info_ptr);
9106 info_ptr += 1;
9107 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9108 info_ptr += blk->size;
9109 DW_BLOCK (attr) = blk;
9110 break;
9111 case DW_FORM_data1:
9112 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9113 info_ptr += 1;
9114 break;
9115 case DW_FORM_flag:
9116 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9117 info_ptr += 1;
9118 break;
9119 case DW_FORM_flag_present:
9120 DW_UNSND (attr) = 1;
9121 break;
9122 case DW_FORM_sdata:
9123 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9124 info_ptr += bytes_read;
9125 break;
9126 case DW_FORM_udata:
9127 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9128 info_ptr += bytes_read;
9129 break;
9130 case DW_FORM_ref1:
9131 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9132 info_ptr += 1;
9133 break;
9134 case DW_FORM_ref2:
9135 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9136 info_ptr += 2;
9137 break;
9138 case DW_FORM_ref4:
9139 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9140 info_ptr += 4;
9141 break;
9142 case DW_FORM_ref8:
9143 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9144 info_ptr += 8;
9145 break;
9146 case DW_FORM_sig8:
9147 /* Convert the signature to something we can record in DW_UNSND
9148 for later lookup.
9149 NOTE: This is NULL if the type wasn't found. */
9150 DW_SIGNATURED_TYPE (attr) =
9151 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9152 info_ptr += 8;
9153 break;
9154 case DW_FORM_ref_udata:
9155 DW_ADDR (attr) = (cu->header.offset
9156 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9157 info_ptr += bytes_read;
9158 break;
9159 case DW_FORM_indirect:
9160 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9161 info_ptr += bytes_read;
9162 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9163 break;
9164 default:
9165 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9166 dwarf_form_name (form),
9167 bfd_get_filename (abfd));
9168 }
9169
9170 /* We have seen instances where the compiler tried to emit a byte
9171 size attribute of -1 which ended up being encoded as an unsigned
9172 0xffffffff. Although 0xffffffff is technically a valid size value,
9173 an object of this size seems pretty unlikely so we can relatively
9174 safely treat these cases as if the size attribute was invalid and
9175 treat them as zero by default. */
9176 if (attr->name == DW_AT_byte_size
9177 && form == DW_FORM_data4
9178 && DW_UNSND (attr) >= 0xffffffff)
9179 {
9180 complaint
9181 (&symfile_complaints,
9182 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9183 hex_string (DW_UNSND (attr)));
9184 DW_UNSND (attr) = 0;
9185 }
9186
9187 return info_ptr;
9188 }
9189
9190 /* Read an attribute described by an abbreviated attribute. */
9191
9192 static gdb_byte *
9193 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9194 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9195 {
9196 attr->name = abbrev->name;
9197 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9198 }
9199
9200 /* read dwarf information from a buffer */
9201
9202 static unsigned int
9203 read_1_byte (bfd *abfd, gdb_byte *buf)
9204 {
9205 return bfd_get_8 (abfd, buf);
9206 }
9207
9208 static int
9209 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9210 {
9211 return bfd_get_signed_8 (abfd, buf);
9212 }
9213
9214 static unsigned int
9215 read_2_bytes (bfd *abfd, gdb_byte *buf)
9216 {
9217 return bfd_get_16 (abfd, buf);
9218 }
9219
9220 static int
9221 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9222 {
9223 return bfd_get_signed_16 (abfd, buf);
9224 }
9225
9226 static unsigned int
9227 read_4_bytes (bfd *abfd, gdb_byte *buf)
9228 {
9229 return bfd_get_32 (abfd, buf);
9230 }
9231
9232 static int
9233 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9234 {
9235 return bfd_get_signed_32 (abfd, buf);
9236 }
9237
9238 static ULONGEST
9239 read_8_bytes (bfd *abfd, gdb_byte *buf)
9240 {
9241 return bfd_get_64 (abfd, buf);
9242 }
9243
9244 static CORE_ADDR
9245 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9246 unsigned int *bytes_read)
9247 {
9248 struct comp_unit_head *cu_header = &cu->header;
9249 CORE_ADDR retval = 0;
9250
9251 if (cu_header->signed_addr_p)
9252 {
9253 switch (cu_header->addr_size)
9254 {
9255 case 2:
9256 retval = bfd_get_signed_16 (abfd, buf);
9257 break;
9258 case 4:
9259 retval = bfd_get_signed_32 (abfd, buf);
9260 break;
9261 case 8:
9262 retval = bfd_get_signed_64 (abfd, buf);
9263 break;
9264 default:
9265 internal_error (__FILE__, __LINE__,
9266 _("read_address: bad switch, signed [in module %s]"),
9267 bfd_get_filename (abfd));
9268 }
9269 }
9270 else
9271 {
9272 switch (cu_header->addr_size)
9273 {
9274 case 2:
9275 retval = bfd_get_16 (abfd, buf);
9276 break;
9277 case 4:
9278 retval = bfd_get_32 (abfd, buf);
9279 break;
9280 case 8:
9281 retval = bfd_get_64 (abfd, buf);
9282 break;
9283 default:
9284 internal_error (__FILE__, __LINE__,
9285 _("read_address: bad switch, unsigned [in module %s]"),
9286 bfd_get_filename (abfd));
9287 }
9288 }
9289
9290 *bytes_read = cu_header->addr_size;
9291 return retval;
9292 }
9293
9294 /* Read the initial length from a section. The (draft) DWARF 3
9295 specification allows the initial length to take up either 4 bytes
9296 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9297 bytes describe the length and all offsets will be 8 bytes in length
9298 instead of 4.
9299
9300 An older, non-standard 64-bit format is also handled by this
9301 function. The older format in question stores the initial length
9302 as an 8-byte quantity without an escape value. Lengths greater
9303 than 2^32 aren't very common which means that the initial 4 bytes
9304 is almost always zero. Since a length value of zero doesn't make
9305 sense for the 32-bit format, this initial zero can be considered to
9306 be an escape value which indicates the presence of the older 64-bit
9307 format. As written, the code can't detect (old format) lengths
9308 greater than 4GB. If it becomes necessary to handle lengths
9309 somewhat larger than 4GB, we could allow other small values (such
9310 as the non-sensical values of 1, 2, and 3) to also be used as
9311 escape values indicating the presence of the old format.
9312
9313 The value returned via bytes_read should be used to increment the
9314 relevant pointer after calling read_initial_length().
9315
9316 [ Note: read_initial_length() and read_offset() are based on the
9317 document entitled "DWARF Debugging Information Format", revision
9318 3, draft 8, dated November 19, 2001. This document was obtained
9319 from:
9320
9321 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9322
9323 This document is only a draft and is subject to change. (So beware.)
9324
9325 Details regarding the older, non-standard 64-bit format were
9326 determined empirically by examining 64-bit ELF files produced by
9327 the SGI toolchain on an IRIX 6.5 machine.
9328
9329 - Kevin, July 16, 2002
9330 ] */
9331
9332 static LONGEST
9333 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9334 {
9335 LONGEST length = bfd_get_32 (abfd, buf);
9336
9337 if (length == 0xffffffff)
9338 {
9339 length = bfd_get_64 (abfd, buf + 4);
9340 *bytes_read = 12;
9341 }
9342 else if (length == 0)
9343 {
9344 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
9345 length = bfd_get_64 (abfd, buf);
9346 *bytes_read = 8;
9347 }
9348 else
9349 {
9350 *bytes_read = 4;
9351 }
9352
9353 return length;
9354 }
9355
9356 /* Cover function for read_initial_length.
9357 Returns the length of the object at BUF, and stores the size of the
9358 initial length in *BYTES_READ and stores the size that offsets will be in
9359 *OFFSET_SIZE.
9360 If the initial length size is not equivalent to that specified in
9361 CU_HEADER then issue a complaint.
9362 This is useful when reading non-comp-unit headers. */
9363
9364 static LONGEST
9365 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9366 const struct comp_unit_head *cu_header,
9367 unsigned int *bytes_read,
9368 unsigned int *offset_size)
9369 {
9370 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9371
9372 gdb_assert (cu_header->initial_length_size == 4
9373 || cu_header->initial_length_size == 8
9374 || cu_header->initial_length_size == 12);
9375
9376 if (cu_header->initial_length_size != *bytes_read)
9377 complaint (&symfile_complaints,
9378 _("intermixed 32-bit and 64-bit DWARF sections"));
9379
9380 *offset_size = (*bytes_read == 4) ? 4 : 8;
9381 return length;
9382 }
9383
9384 /* Read an offset from the data stream. The size of the offset is
9385 given by cu_header->offset_size. */
9386
9387 static LONGEST
9388 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9389 unsigned int *bytes_read)
9390 {
9391 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9392
9393 *bytes_read = cu_header->offset_size;
9394 return offset;
9395 }
9396
9397 /* Read an offset from the data stream. */
9398
9399 static LONGEST
9400 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9401 {
9402 LONGEST retval = 0;
9403
9404 switch (offset_size)
9405 {
9406 case 4:
9407 retval = bfd_get_32 (abfd, buf);
9408 break;
9409 case 8:
9410 retval = bfd_get_64 (abfd, buf);
9411 break;
9412 default:
9413 internal_error (__FILE__, __LINE__,
9414 _("read_offset_1: bad switch [in module %s]"),
9415 bfd_get_filename (abfd));
9416 }
9417
9418 return retval;
9419 }
9420
9421 static gdb_byte *
9422 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9423 {
9424 /* If the size of a host char is 8 bits, we can return a pointer
9425 to the buffer, otherwise we have to copy the data to a buffer
9426 allocated on the temporary obstack. */
9427 gdb_assert (HOST_CHAR_BIT == 8);
9428 return buf;
9429 }
9430
9431 static char *
9432 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9433 {
9434 /* If the size of a host char is 8 bits, we can return a pointer
9435 to the string, otherwise we have to copy the string to a buffer
9436 allocated on the temporary obstack. */
9437 gdb_assert (HOST_CHAR_BIT == 8);
9438 if (*buf == '\0')
9439 {
9440 *bytes_read_ptr = 1;
9441 return NULL;
9442 }
9443 *bytes_read_ptr = strlen ((char *) buf) + 1;
9444 return (char *) buf;
9445 }
9446
9447 static char *
9448 read_indirect_string (bfd *abfd, gdb_byte *buf,
9449 const struct comp_unit_head *cu_header,
9450 unsigned int *bytes_read_ptr)
9451 {
9452 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9453
9454 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9455 if (dwarf2_per_objfile->str.buffer == NULL)
9456 {
9457 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9458 bfd_get_filename (abfd));
9459 return NULL;
9460 }
9461 if (str_offset >= dwarf2_per_objfile->str.size)
9462 {
9463 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9464 bfd_get_filename (abfd));
9465 return NULL;
9466 }
9467 gdb_assert (HOST_CHAR_BIT == 8);
9468 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9469 return NULL;
9470 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9471 }
9472
9473 static unsigned long
9474 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9475 {
9476 unsigned long result;
9477 unsigned int num_read;
9478 int i, shift;
9479 unsigned char byte;
9480
9481 result = 0;
9482 shift = 0;
9483 num_read = 0;
9484 i = 0;
9485 while (1)
9486 {
9487 byte = bfd_get_8 (abfd, buf);
9488 buf++;
9489 num_read++;
9490 result |= ((unsigned long)(byte & 127) << shift);
9491 if ((byte & 128) == 0)
9492 {
9493 break;
9494 }
9495 shift += 7;
9496 }
9497 *bytes_read_ptr = num_read;
9498 return result;
9499 }
9500
9501 static long
9502 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9503 {
9504 long result;
9505 int i, shift, num_read;
9506 unsigned char byte;
9507
9508 result = 0;
9509 shift = 0;
9510 num_read = 0;
9511 i = 0;
9512 while (1)
9513 {
9514 byte = bfd_get_8 (abfd, buf);
9515 buf++;
9516 num_read++;
9517 result |= ((long)(byte & 127) << shift);
9518 shift += 7;
9519 if ((byte & 128) == 0)
9520 {
9521 break;
9522 }
9523 }
9524 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9525 result |= -(((long)1) << shift);
9526 *bytes_read_ptr = num_read;
9527 return result;
9528 }
9529
9530 /* Return a pointer to just past the end of an LEB128 number in BUF. */
9531
9532 static gdb_byte *
9533 skip_leb128 (bfd *abfd, gdb_byte *buf)
9534 {
9535 int byte;
9536
9537 while (1)
9538 {
9539 byte = bfd_get_8 (abfd, buf);
9540 buf++;
9541 if ((byte & 128) == 0)
9542 return buf;
9543 }
9544 }
9545
9546 static void
9547 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9548 {
9549 switch (lang)
9550 {
9551 case DW_LANG_C89:
9552 case DW_LANG_C99:
9553 case DW_LANG_C:
9554 cu->language = language_c;
9555 break;
9556 case DW_LANG_C_plus_plus:
9557 cu->language = language_cplus;
9558 break;
9559 case DW_LANG_D:
9560 cu->language = language_d;
9561 break;
9562 case DW_LANG_Fortran77:
9563 case DW_LANG_Fortran90:
9564 case DW_LANG_Fortran95:
9565 cu->language = language_fortran;
9566 break;
9567 case DW_LANG_Mips_Assembler:
9568 cu->language = language_asm;
9569 break;
9570 case DW_LANG_Java:
9571 cu->language = language_java;
9572 break;
9573 case DW_LANG_Ada83:
9574 case DW_LANG_Ada95:
9575 cu->language = language_ada;
9576 break;
9577 case DW_LANG_Modula2:
9578 cu->language = language_m2;
9579 break;
9580 case DW_LANG_Pascal83:
9581 cu->language = language_pascal;
9582 break;
9583 case DW_LANG_ObjC:
9584 cu->language = language_objc;
9585 break;
9586 case DW_LANG_Cobol74:
9587 case DW_LANG_Cobol85:
9588 default:
9589 cu->language = language_minimal;
9590 break;
9591 }
9592 cu->language_defn = language_def (cu->language);
9593 }
9594
9595 /* Return the named attribute or NULL if not there. */
9596
9597 static struct attribute *
9598 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9599 {
9600 unsigned int i;
9601 struct attribute *spec = NULL;
9602
9603 for (i = 0; i < die->num_attrs; ++i)
9604 {
9605 if (die->attrs[i].name == name)
9606 return &die->attrs[i];
9607 if (die->attrs[i].name == DW_AT_specification
9608 || die->attrs[i].name == DW_AT_abstract_origin)
9609 spec = &die->attrs[i];
9610 }
9611
9612 if (spec)
9613 {
9614 die = follow_die_ref (die, spec, &cu);
9615 return dwarf2_attr (die, name, cu);
9616 }
9617
9618 return NULL;
9619 }
9620
9621 /* Return the named attribute or NULL if not there,
9622 but do not follow DW_AT_specification, etc.
9623 This is for use in contexts where we're reading .debug_types dies.
9624 Following DW_AT_specification, DW_AT_abstract_origin will take us
9625 back up the chain, and we want to go down. */
9626
9627 static struct attribute *
9628 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9629 struct dwarf2_cu *cu)
9630 {
9631 unsigned int i;
9632
9633 for (i = 0; i < die->num_attrs; ++i)
9634 if (die->attrs[i].name == name)
9635 return &die->attrs[i];
9636
9637 return NULL;
9638 }
9639
9640 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9641 and holds a non-zero value. This function should only be used for
9642 DW_FORM_flag or DW_FORM_flag_present attributes. */
9643
9644 static int
9645 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9646 {
9647 struct attribute *attr = dwarf2_attr (die, name, cu);
9648
9649 return (attr && DW_UNSND (attr));
9650 }
9651
9652 static int
9653 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9654 {
9655 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9656 which value is non-zero. However, we have to be careful with
9657 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9658 (via dwarf2_flag_true_p) follows this attribute. So we may
9659 end up accidently finding a declaration attribute that belongs
9660 to a different DIE referenced by the specification attribute,
9661 even though the given DIE does not have a declaration attribute. */
9662 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9663 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9664 }
9665
9666 /* Return the die giving the specification for DIE, if there is
9667 one. *SPEC_CU is the CU containing DIE on input, and the CU
9668 containing the return value on output. If there is no
9669 specification, but there is an abstract origin, that is
9670 returned. */
9671
9672 static struct die_info *
9673 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9674 {
9675 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9676 *spec_cu);
9677
9678 if (spec_attr == NULL)
9679 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9680
9681 if (spec_attr == NULL)
9682 return NULL;
9683 else
9684 return follow_die_ref (die, spec_attr, spec_cu);
9685 }
9686
9687 /* Free the line_header structure *LH, and any arrays and strings it
9688 refers to. */
9689 static void
9690 free_line_header (struct line_header *lh)
9691 {
9692 if (lh->standard_opcode_lengths)
9693 xfree (lh->standard_opcode_lengths);
9694
9695 /* Remember that all the lh->file_names[i].name pointers are
9696 pointers into debug_line_buffer, and don't need to be freed. */
9697 if (lh->file_names)
9698 xfree (lh->file_names);
9699
9700 /* Similarly for the include directory names. */
9701 if (lh->include_dirs)
9702 xfree (lh->include_dirs);
9703
9704 xfree (lh);
9705 }
9706
9707
9708 /* Add an entry to LH's include directory table. */
9709 static void
9710 add_include_dir (struct line_header *lh, char *include_dir)
9711 {
9712 /* Grow the array if necessary. */
9713 if (lh->include_dirs_size == 0)
9714 {
9715 lh->include_dirs_size = 1; /* for testing */
9716 lh->include_dirs = xmalloc (lh->include_dirs_size
9717 * sizeof (*lh->include_dirs));
9718 }
9719 else if (lh->num_include_dirs >= lh->include_dirs_size)
9720 {
9721 lh->include_dirs_size *= 2;
9722 lh->include_dirs = xrealloc (lh->include_dirs,
9723 (lh->include_dirs_size
9724 * sizeof (*lh->include_dirs)));
9725 }
9726
9727 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9728 }
9729
9730
9731 /* Add an entry to LH's file name table. */
9732 static void
9733 add_file_name (struct line_header *lh,
9734 char *name,
9735 unsigned int dir_index,
9736 unsigned int mod_time,
9737 unsigned int length)
9738 {
9739 struct file_entry *fe;
9740
9741 /* Grow the array if necessary. */
9742 if (lh->file_names_size == 0)
9743 {
9744 lh->file_names_size = 1; /* for testing */
9745 lh->file_names = xmalloc (lh->file_names_size
9746 * sizeof (*lh->file_names));
9747 }
9748 else if (lh->num_file_names >= lh->file_names_size)
9749 {
9750 lh->file_names_size *= 2;
9751 lh->file_names = xrealloc (lh->file_names,
9752 (lh->file_names_size
9753 * sizeof (*lh->file_names)));
9754 }
9755
9756 fe = &lh->file_names[lh->num_file_names++];
9757 fe->name = name;
9758 fe->dir_index = dir_index;
9759 fe->mod_time = mod_time;
9760 fe->length = length;
9761 fe->included_p = 0;
9762 fe->symtab = NULL;
9763 }
9764
9765
9766 /* Read the statement program header starting at OFFSET in
9767 .debug_line, according to the endianness of ABFD. Return a pointer
9768 to a struct line_header, allocated using xmalloc.
9769
9770 NOTE: the strings in the include directory and file name tables of
9771 the returned object point into debug_line_buffer, and must not be
9772 freed. */
9773 static struct line_header *
9774 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9775 struct dwarf2_cu *cu)
9776 {
9777 struct cleanup *back_to;
9778 struct line_header *lh;
9779 gdb_byte *line_ptr;
9780 unsigned int bytes_read, offset_size;
9781 int i;
9782 char *cur_dir, *cur_file;
9783
9784 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9785 if (dwarf2_per_objfile->line.buffer == NULL)
9786 {
9787 complaint (&symfile_complaints, _("missing .debug_line section"));
9788 return 0;
9789 }
9790
9791 /* Make sure that at least there's room for the total_length field.
9792 That could be 12 bytes long, but we're just going to fudge that. */
9793 if (offset + 4 >= dwarf2_per_objfile->line.size)
9794 {
9795 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9796 return 0;
9797 }
9798
9799 lh = xmalloc (sizeof (*lh));
9800 memset (lh, 0, sizeof (*lh));
9801 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9802 (void *) lh);
9803
9804 line_ptr = dwarf2_per_objfile->line.buffer + offset;
9805
9806 /* Read in the header. */
9807 lh->total_length =
9808 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9809 &bytes_read, &offset_size);
9810 line_ptr += bytes_read;
9811 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9812 + dwarf2_per_objfile->line.size))
9813 {
9814 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9815 return 0;
9816 }
9817 lh->statement_program_end = line_ptr + lh->total_length;
9818 lh->version = read_2_bytes (abfd, line_ptr);
9819 line_ptr += 2;
9820 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9821 line_ptr += offset_size;
9822 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9823 line_ptr += 1;
9824 if (lh->version >= 4)
9825 {
9826 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9827 line_ptr += 1;
9828 }
9829 else
9830 lh->maximum_ops_per_instruction = 1;
9831
9832 if (lh->maximum_ops_per_instruction == 0)
9833 {
9834 lh->maximum_ops_per_instruction = 1;
9835 complaint (&symfile_complaints,
9836 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9837 }
9838
9839 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9840 line_ptr += 1;
9841 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9842 line_ptr += 1;
9843 lh->line_range = read_1_byte (abfd, line_ptr);
9844 line_ptr += 1;
9845 lh->opcode_base = read_1_byte (abfd, line_ptr);
9846 line_ptr += 1;
9847 lh->standard_opcode_lengths
9848 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9849
9850 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9851 for (i = 1; i < lh->opcode_base; ++i)
9852 {
9853 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9854 line_ptr += 1;
9855 }
9856
9857 /* Read directory table. */
9858 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9859 {
9860 line_ptr += bytes_read;
9861 add_include_dir (lh, cur_dir);
9862 }
9863 line_ptr += bytes_read;
9864
9865 /* Read file name table. */
9866 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9867 {
9868 unsigned int dir_index, mod_time, length;
9869
9870 line_ptr += bytes_read;
9871 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9872 line_ptr += bytes_read;
9873 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9874 line_ptr += bytes_read;
9875 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9876 line_ptr += bytes_read;
9877
9878 add_file_name (lh, cur_file, dir_index, mod_time, length);
9879 }
9880 line_ptr += bytes_read;
9881 lh->statement_program_start = line_ptr;
9882
9883 if (line_ptr > (dwarf2_per_objfile->line.buffer
9884 + dwarf2_per_objfile->line.size))
9885 complaint (&symfile_complaints,
9886 _("line number info header doesn't fit in `.debug_line' section"));
9887
9888 discard_cleanups (back_to);
9889 return lh;
9890 }
9891
9892 /* This function exists to work around a bug in certain compilers
9893 (particularly GCC 2.95), in which the first line number marker of a
9894 function does not show up until after the prologue, right before
9895 the second line number marker. This function shifts ADDRESS down
9896 to the beginning of the function if necessary, and is called on
9897 addresses passed to record_line. */
9898
9899 static CORE_ADDR
9900 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
9901 {
9902 struct function_range *fn;
9903
9904 /* Find the function_range containing address. */
9905 if (!cu->first_fn)
9906 return address;
9907
9908 if (!cu->cached_fn)
9909 cu->cached_fn = cu->first_fn;
9910
9911 fn = cu->cached_fn;
9912 while (fn)
9913 if (fn->lowpc <= address && fn->highpc > address)
9914 goto found;
9915 else
9916 fn = fn->next;
9917
9918 fn = cu->first_fn;
9919 while (fn && fn != cu->cached_fn)
9920 if (fn->lowpc <= address && fn->highpc > address)
9921 goto found;
9922 else
9923 fn = fn->next;
9924
9925 return address;
9926
9927 found:
9928 if (fn->seen_line)
9929 return address;
9930 if (address != fn->lowpc)
9931 complaint (&symfile_complaints,
9932 _("misplaced first line number at 0x%lx for '%s'"),
9933 (unsigned long) address, fn->name);
9934 fn->seen_line = 1;
9935 return fn->lowpc;
9936 }
9937
9938 /* Subroutine of dwarf_decode_lines to simplify it.
9939 Return the file name of the psymtab for included file FILE_INDEX
9940 in line header LH of PST.
9941 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
9942 If space for the result is malloc'd, it will be freed by a cleanup.
9943 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
9944
9945 static char *
9946 psymtab_include_file_name (const struct line_header *lh, int file_index,
9947 const struct partial_symtab *pst,
9948 const char *comp_dir)
9949 {
9950 const struct file_entry fe = lh->file_names [file_index];
9951 char *include_name = fe.name;
9952 char *include_name_to_compare = include_name;
9953 char *dir_name = NULL;
9954 char *pst_filename;
9955 int file_is_pst;
9956
9957 if (fe.dir_index)
9958 dir_name = lh->include_dirs[fe.dir_index - 1];
9959
9960 if (!IS_ABSOLUTE_PATH (include_name)
9961 && (dir_name != NULL || comp_dir != NULL))
9962 {
9963 /* Avoid creating a duplicate psymtab for PST.
9964 We do this by comparing INCLUDE_NAME and PST_FILENAME.
9965 Before we do the comparison, however, we need to account
9966 for DIR_NAME and COMP_DIR.
9967 First prepend dir_name (if non-NULL). If we still don't
9968 have an absolute path prepend comp_dir (if non-NULL).
9969 However, the directory we record in the include-file's
9970 psymtab does not contain COMP_DIR (to match the
9971 corresponding symtab(s)).
9972
9973 Example:
9974
9975 bash$ cd /tmp
9976 bash$ gcc -g ./hello.c
9977 include_name = "hello.c"
9978 dir_name = "."
9979 DW_AT_comp_dir = comp_dir = "/tmp"
9980 DW_AT_name = "./hello.c" */
9981
9982 if (dir_name != NULL)
9983 {
9984 include_name = concat (dir_name, SLASH_STRING,
9985 include_name, (char *)NULL);
9986 include_name_to_compare = include_name;
9987 make_cleanup (xfree, include_name);
9988 }
9989 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
9990 {
9991 include_name_to_compare = concat (comp_dir, SLASH_STRING,
9992 include_name, (char *)NULL);
9993 }
9994 }
9995
9996 pst_filename = pst->filename;
9997 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
9998 {
9999 pst_filename = concat (pst->dirname, SLASH_STRING,
10000 pst_filename, (char *)NULL);
10001 }
10002
10003 file_is_pst = strcmp (include_name_to_compare, pst_filename) == 0;
10004
10005 if (include_name_to_compare != include_name)
10006 xfree (include_name_to_compare);
10007 if (pst_filename != pst->filename)
10008 xfree (pst_filename);
10009
10010 if (file_is_pst)
10011 return NULL;
10012 return include_name;
10013 }
10014
10015 /* Decode the Line Number Program (LNP) for the given line_header
10016 structure and CU. The actual information extracted and the type
10017 of structures created from the LNP depends on the value of PST.
10018
10019 1. If PST is NULL, then this procedure uses the data from the program
10020 to create all necessary symbol tables, and their linetables.
10021
10022 2. If PST is not NULL, this procedure reads the program to determine
10023 the list of files included by the unit represented by PST, and
10024 builds all the associated partial symbol tables.
10025
10026 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10027 It is used for relative paths in the line table.
10028 NOTE: When processing partial symtabs (pst != NULL),
10029 comp_dir == pst->dirname.
10030
10031 NOTE: It is important that psymtabs have the same file name (via strcmp)
10032 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10033 symtab we don't use it in the name of the psymtabs we create.
10034 E.g. expand_line_sal requires this when finding psymtabs to expand.
10035 A good testcase for this is mb-inline.exp. */
10036
10037 static void
10038 dwarf_decode_lines (struct line_header *lh, char *comp_dir, bfd *abfd,
10039 struct dwarf2_cu *cu, struct partial_symtab *pst)
10040 {
10041 gdb_byte *line_ptr, *extended_end;
10042 gdb_byte *line_end;
10043 unsigned int bytes_read, extended_len;
10044 unsigned char op_code, extended_op, adj_opcode;
10045 CORE_ADDR baseaddr;
10046 struct objfile *objfile = cu->objfile;
10047 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10048 const int decode_for_pst_p = (pst != NULL);
10049 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10050
10051 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10052
10053 line_ptr = lh->statement_program_start;
10054 line_end = lh->statement_program_end;
10055
10056 /* Read the statement sequences until there's nothing left. */
10057 while (line_ptr < line_end)
10058 {
10059 /* state machine registers */
10060 CORE_ADDR address = 0;
10061 unsigned int file = 1;
10062 unsigned int line = 1;
10063 unsigned int column = 0;
10064 int is_stmt = lh->default_is_stmt;
10065 int basic_block = 0;
10066 int end_sequence = 0;
10067 CORE_ADDR addr;
10068 unsigned char op_index = 0;
10069
10070 if (!decode_for_pst_p && lh->num_file_names >= file)
10071 {
10072 /* Start a subfile for the current file of the state machine. */
10073 /* lh->include_dirs and lh->file_names are 0-based, but the
10074 directory and file name numbers in the statement program
10075 are 1-based. */
10076 struct file_entry *fe = &lh->file_names[file - 1];
10077 char *dir = NULL;
10078
10079 if (fe->dir_index)
10080 dir = lh->include_dirs[fe->dir_index - 1];
10081
10082 dwarf2_start_subfile (fe->name, dir, comp_dir);
10083 }
10084
10085 /* Decode the table. */
10086 while (!end_sequence)
10087 {
10088 op_code = read_1_byte (abfd, line_ptr);
10089 line_ptr += 1;
10090 if (line_ptr > line_end)
10091 {
10092 dwarf2_debug_line_missing_end_sequence_complaint ();
10093 break;
10094 }
10095
10096 if (op_code >= lh->opcode_base)
10097 {
10098 /* Special operand. */
10099 adj_opcode = op_code - lh->opcode_base;
10100 address += (((op_index + (adj_opcode / lh->line_range))
10101 / lh->maximum_ops_per_instruction)
10102 * lh->minimum_instruction_length);
10103 op_index = ((op_index + (adj_opcode / lh->line_range))
10104 % lh->maximum_ops_per_instruction);
10105 line += lh->line_base + (adj_opcode % lh->line_range);
10106 if (lh->num_file_names < file || file == 0)
10107 dwarf2_debug_line_missing_file_complaint ();
10108 /* For now we ignore lines not starting on an
10109 instruction boundary. */
10110 else if (op_index == 0)
10111 {
10112 lh->file_names[file - 1].included_p = 1;
10113 if (!decode_for_pst_p && is_stmt)
10114 {
10115 if (last_subfile != current_subfile)
10116 {
10117 addr = gdbarch_addr_bits_remove (gdbarch, address);
10118 if (last_subfile)
10119 record_line (last_subfile, 0, addr);
10120 last_subfile = current_subfile;
10121 }
10122 /* Append row to matrix using current values. */
10123 addr = check_cu_functions (address, cu);
10124 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10125 record_line (current_subfile, line, addr);
10126 }
10127 }
10128 basic_block = 0;
10129 }
10130 else switch (op_code)
10131 {
10132 case DW_LNS_extended_op:
10133 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10134 line_ptr += bytes_read;
10135 extended_end = line_ptr + extended_len;
10136 extended_op = read_1_byte (abfd, line_ptr);
10137 line_ptr += 1;
10138 switch (extended_op)
10139 {
10140 case DW_LNE_end_sequence:
10141 end_sequence = 1;
10142 break;
10143 case DW_LNE_set_address:
10144 address = read_address (abfd, line_ptr, cu, &bytes_read);
10145 op_index = 0;
10146 line_ptr += bytes_read;
10147 address += baseaddr;
10148 break;
10149 case DW_LNE_define_file:
10150 {
10151 char *cur_file;
10152 unsigned int dir_index, mod_time, length;
10153
10154 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
10155 line_ptr += bytes_read;
10156 dir_index =
10157 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10158 line_ptr += bytes_read;
10159 mod_time =
10160 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10161 line_ptr += bytes_read;
10162 length =
10163 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10164 line_ptr += bytes_read;
10165 add_file_name (lh, cur_file, dir_index, mod_time, length);
10166 }
10167 break;
10168 case DW_LNE_set_discriminator:
10169 /* The discriminator is not interesting to the debugger;
10170 just ignore it. */
10171 line_ptr = extended_end;
10172 break;
10173 default:
10174 complaint (&symfile_complaints,
10175 _("mangled .debug_line section"));
10176 return;
10177 }
10178 /* Make sure that we parsed the extended op correctly. If e.g.
10179 we expected a different address size than the producer used,
10180 we may have read the wrong number of bytes. */
10181 if (line_ptr != extended_end)
10182 {
10183 complaint (&symfile_complaints,
10184 _("mangled .debug_line section"));
10185 return;
10186 }
10187 break;
10188 case DW_LNS_copy:
10189 if (lh->num_file_names < file || file == 0)
10190 dwarf2_debug_line_missing_file_complaint ();
10191 else
10192 {
10193 lh->file_names[file - 1].included_p = 1;
10194 if (!decode_for_pst_p && is_stmt)
10195 {
10196 if (last_subfile != current_subfile)
10197 {
10198 addr = gdbarch_addr_bits_remove (gdbarch, address);
10199 if (last_subfile)
10200 record_line (last_subfile, 0, addr);
10201 last_subfile = current_subfile;
10202 }
10203 addr = check_cu_functions (address, cu);
10204 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10205 record_line (current_subfile, line, addr);
10206 }
10207 }
10208 basic_block = 0;
10209 break;
10210 case DW_LNS_advance_pc:
10211 {
10212 CORE_ADDR adjust
10213 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10214
10215 address += (((op_index + adjust)
10216 / lh->maximum_ops_per_instruction)
10217 * lh->minimum_instruction_length);
10218 op_index = ((op_index + adjust)
10219 % lh->maximum_ops_per_instruction);
10220 line_ptr += bytes_read;
10221 }
10222 break;
10223 case DW_LNS_advance_line:
10224 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10225 line_ptr += bytes_read;
10226 break;
10227 case DW_LNS_set_file:
10228 {
10229 /* The arrays lh->include_dirs and lh->file_names are
10230 0-based, but the directory and file name numbers in
10231 the statement program are 1-based. */
10232 struct file_entry *fe;
10233 char *dir = NULL;
10234
10235 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10236 line_ptr += bytes_read;
10237 if (lh->num_file_names < file || file == 0)
10238 dwarf2_debug_line_missing_file_complaint ();
10239 else
10240 {
10241 fe = &lh->file_names[file - 1];
10242 if (fe->dir_index)
10243 dir = lh->include_dirs[fe->dir_index - 1];
10244 if (!decode_for_pst_p)
10245 {
10246 last_subfile = current_subfile;
10247 dwarf2_start_subfile (fe->name, dir, comp_dir);
10248 }
10249 }
10250 }
10251 break;
10252 case DW_LNS_set_column:
10253 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10254 line_ptr += bytes_read;
10255 break;
10256 case DW_LNS_negate_stmt:
10257 is_stmt = (!is_stmt);
10258 break;
10259 case DW_LNS_set_basic_block:
10260 basic_block = 1;
10261 break;
10262 /* Add to the address register of the state machine the
10263 address increment value corresponding to special opcode
10264 255. I.e., this value is scaled by the minimum
10265 instruction length since special opcode 255 would have
10266 scaled the the increment. */
10267 case DW_LNS_const_add_pc:
10268 {
10269 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10270
10271 address += (((op_index + adjust)
10272 / lh->maximum_ops_per_instruction)
10273 * lh->minimum_instruction_length);
10274 op_index = ((op_index + adjust)
10275 % lh->maximum_ops_per_instruction);
10276 }
10277 break;
10278 case DW_LNS_fixed_advance_pc:
10279 address += read_2_bytes (abfd, line_ptr);
10280 op_index = 0;
10281 line_ptr += 2;
10282 break;
10283 default:
10284 {
10285 /* Unknown standard opcode, ignore it. */
10286 int i;
10287
10288 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10289 {
10290 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10291 line_ptr += bytes_read;
10292 }
10293 }
10294 }
10295 }
10296 if (lh->num_file_names < file || file == 0)
10297 dwarf2_debug_line_missing_file_complaint ();
10298 else
10299 {
10300 lh->file_names[file - 1].included_p = 1;
10301 if (!decode_for_pst_p)
10302 {
10303 addr = gdbarch_addr_bits_remove (gdbarch, address);
10304 record_line (current_subfile, 0, addr);
10305 }
10306 }
10307 }
10308
10309 if (decode_for_pst_p)
10310 {
10311 int file_index;
10312
10313 /* Now that we're done scanning the Line Header Program, we can
10314 create the psymtab of each included file. */
10315 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10316 if (lh->file_names[file_index].included_p == 1)
10317 {
10318 char *include_name =
10319 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10320 if (include_name != NULL)
10321 dwarf2_create_include_psymtab (include_name, pst, objfile);
10322 }
10323 }
10324 else
10325 {
10326 /* Make sure a symtab is created for every file, even files
10327 which contain only variables (i.e. no code with associated
10328 line numbers). */
10329
10330 int i;
10331 struct file_entry *fe;
10332
10333 for (i = 0; i < lh->num_file_names; i++)
10334 {
10335 char *dir = NULL;
10336
10337 fe = &lh->file_names[i];
10338 if (fe->dir_index)
10339 dir = lh->include_dirs[fe->dir_index - 1];
10340 dwarf2_start_subfile (fe->name, dir, comp_dir);
10341
10342 /* Skip the main file; we don't need it, and it must be
10343 allocated last, so that it will show up before the
10344 non-primary symtabs in the objfile's symtab list. */
10345 if (current_subfile == first_subfile)
10346 continue;
10347
10348 if (current_subfile->symtab == NULL)
10349 current_subfile->symtab = allocate_symtab (current_subfile->name,
10350 cu->objfile);
10351 fe->symtab = current_subfile->symtab;
10352 }
10353 }
10354 }
10355
10356 /* Start a subfile for DWARF. FILENAME is the name of the file and
10357 DIRNAME the name of the source directory which contains FILENAME
10358 or NULL if not known. COMP_DIR is the compilation directory for the
10359 linetable's compilation unit or NULL if not known.
10360 This routine tries to keep line numbers from identical absolute and
10361 relative file names in a common subfile.
10362
10363 Using the `list' example from the GDB testsuite, which resides in
10364 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10365 of /srcdir/list0.c yields the following debugging information for list0.c:
10366
10367 DW_AT_name: /srcdir/list0.c
10368 DW_AT_comp_dir: /compdir
10369 files.files[0].name: list0.h
10370 files.files[0].dir: /srcdir
10371 files.files[1].name: list0.c
10372 files.files[1].dir: /srcdir
10373
10374 The line number information for list0.c has to end up in a single
10375 subfile, so that `break /srcdir/list0.c:1' works as expected.
10376 start_subfile will ensure that this happens provided that we pass the
10377 concatenation of files.files[1].dir and files.files[1].name as the
10378 subfile's name. */
10379
10380 static void
10381 dwarf2_start_subfile (char *filename, char *dirname, char *comp_dir)
10382 {
10383 char *fullname;
10384
10385 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10386 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10387 second argument to start_subfile. To be consistent, we do the
10388 same here. In order not to lose the line information directory,
10389 we concatenate it to the filename when it makes sense.
10390 Note that the Dwarf3 standard says (speaking of filenames in line
10391 information): ``The directory index is ignored for file names
10392 that represent full path names''. Thus ignoring dirname in the
10393 `else' branch below isn't an issue. */
10394
10395 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10396 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10397 else
10398 fullname = filename;
10399
10400 start_subfile (fullname, comp_dir);
10401
10402 if (fullname != filename)
10403 xfree (fullname);
10404 }
10405
10406 static void
10407 var_decode_location (struct attribute *attr, struct symbol *sym,
10408 struct dwarf2_cu *cu)
10409 {
10410 struct objfile *objfile = cu->objfile;
10411 struct comp_unit_head *cu_header = &cu->header;
10412
10413 /* NOTE drow/2003-01-30: There used to be a comment and some special
10414 code here to turn a symbol with DW_AT_external and a
10415 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10416 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10417 with some versions of binutils) where shared libraries could have
10418 relocations against symbols in their debug information - the
10419 minimal symbol would have the right address, but the debug info
10420 would not. It's no longer necessary, because we will explicitly
10421 apply relocations when we read in the debug information now. */
10422
10423 /* A DW_AT_location attribute with no contents indicates that a
10424 variable has been optimized away. */
10425 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10426 {
10427 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10428 return;
10429 }
10430
10431 /* Handle one degenerate form of location expression specially, to
10432 preserve GDB's previous behavior when section offsets are
10433 specified. If this is just a DW_OP_addr then mark this symbol
10434 as LOC_STATIC. */
10435
10436 if (attr_form_is_block (attr)
10437 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10438 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10439 {
10440 unsigned int dummy;
10441
10442 SYMBOL_VALUE_ADDRESS (sym) =
10443 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10444 SYMBOL_CLASS (sym) = LOC_STATIC;
10445 fixup_symbol_section (sym, objfile);
10446 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10447 SYMBOL_SECTION (sym));
10448 return;
10449 }
10450
10451 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10452 expression evaluator, and use LOC_COMPUTED only when necessary
10453 (i.e. when the value of a register or memory location is
10454 referenced, or a thread-local block, etc.). Then again, it might
10455 not be worthwhile. I'm assuming that it isn't unless performance
10456 or memory numbers show me otherwise. */
10457
10458 dwarf2_symbol_mark_computed (attr, sym, cu);
10459 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10460 }
10461
10462 /* Given a pointer to a DWARF information entry, figure out if we need
10463 to make a symbol table entry for it, and if so, create a new entry
10464 and return a pointer to it.
10465 If TYPE is NULL, determine symbol type from the die, otherwise
10466 used the passed type.
10467 If SPACE is not NULL, use it to hold the new symbol. If it is
10468 NULL, allocate a new symbol on the objfile's obstack. */
10469
10470 static struct symbol *
10471 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10472 struct symbol *space)
10473 {
10474 struct objfile *objfile = cu->objfile;
10475 struct symbol *sym = NULL;
10476 char *name;
10477 struct attribute *attr = NULL;
10478 struct attribute *attr2 = NULL;
10479 CORE_ADDR baseaddr;
10480 struct pending **list_to_add = NULL;
10481
10482 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10483
10484 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10485
10486 name = dwarf2_name (die, cu);
10487 if (name)
10488 {
10489 const char *linkagename;
10490 int suppress_add = 0;
10491
10492 if (space)
10493 sym = space;
10494 else
10495 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10496 OBJSTAT (objfile, n_syms++);
10497
10498 /* Cache this symbol's name and the name's demangled form (if any). */
10499 SYMBOL_SET_LANGUAGE (sym, cu->language);
10500 linkagename = dwarf2_physname (name, die, cu);
10501 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10502
10503 /* Fortran does not have mangling standard and the mangling does differ
10504 between gfortran, iFort etc. */
10505 if (cu->language == language_fortran
10506 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10507 symbol_set_demangled_name (&(sym->ginfo),
10508 (char *) dwarf2_full_name (name, die, cu),
10509 NULL);
10510
10511 /* Default assumptions.
10512 Use the passed type or decode it from the die. */
10513 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10514 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10515 if (type != NULL)
10516 SYMBOL_TYPE (sym) = type;
10517 else
10518 SYMBOL_TYPE (sym) = die_type (die, cu);
10519 attr = dwarf2_attr (die,
10520 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10521 cu);
10522 if (attr)
10523 {
10524 SYMBOL_LINE (sym) = DW_UNSND (attr);
10525 }
10526
10527 attr = dwarf2_attr (die,
10528 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10529 cu);
10530 if (attr)
10531 {
10532 int file_index = DW_UNSND (attr);
10533
10534 if (cu->line_header == NULL
10535 || file_index > cu->line_header->num_file_names)
10536 complaint (&symfile_complaints,
10537 _("file index out of range"));
10538 else if (file_index > 0)
10539 {
10540 struct file_entry *fe;
10541
10542 fe = &cu->line_header->file_names[file_index - 1];
10543 SYMBOL_SYMTAB (sym) = fe->symtab;
10544 }
10545 }
10546
10547 switch (die->tag)
10548 {
10549 case DW_TAG_label:
10550 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10551 if (attr)
10552 {
10553 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10554 }
10555 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10556 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10557 SYMBOL_CLASS (sym) = LOC_LABEL;
10558 add_symbol_to_list (sym, cu->list_in_scope);
10559 break;
10560 case DW_TAG_subprogram:
10561 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10562 finish_block. */
10563 SYMBOL_CLASS (sym) = LOC_BLOCK;
10564 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10565 if ((attr2 && (DW_UNSND (attr2) != 0))
10566 || cu->language == language_ada)
10567 {
10568 /* Subprograms marked external are stored as a global symbol.
10569 Ada subprograms, whether marked external or not, are always
10570 stored as a global symbol, because we want to be able to
10571 access them globally. For instance, we want to be able
10572 to break on a nested subprogram without having to
10573 specify the context. */
10574 list_to_add = &global_symbols;
10575 }
10576 else
10577 {
10578 list_to_add = cu->list_in_scope;
10579 }
10580 break;
10581 case DW_TAG_inlined_subroutine:
10582 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10583 finish_block. */
10584 SYMBOL_CLASS (sym) = LOC_BLOCK;
10585 SYMBOL_INLINED (sym) = 1;
10586 /* Do not add the symbol to any lists. It will be found via
10587 BLOCK_FUNCTION from the blockvector. */
10588 break;
10589 case DW_TAG_template_value_param:
10590 suppress_add = 1;
10591 /* Fall through. */
10592 case DW_TAG_constant:
10593 case DW_TAG_variable:
10594 case DW_TAG_member:
10595 /* Compilation with minimal debug info may result in variables
10596 with missing type entries. Change the misleading `void' type
10597 to something sensible. */
10598 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10599 SYMBOL_TYPE (sym)
10600 = objfile_type (objfile)->nodebug_data_symbol;
10601
10602 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10603 /* In the case of DW_TAG_member, we should only be called for
10604 static const members. */
10605 if (die->tag == DW_TAG_member)
10606 {
10607 /* dwarf2_add_field uses die_is_declaration,
10608 so we do the same. */
10609 gdb_assert (die_is_declaration (die, cu));
10610 gdb_assert (attr);
10611 }
10612 if (attr)
10613 {
10614 dwarf2_const_value (attr, sym, cu);
10615 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10616 if (!suppress_add)
10617 {
10618 if (attr2 && (DW_UNSND (attr2) != 0))
10619 list_to_add = &global_symbols;
10620 else
10621 list_to_add = cu->list_in_scope;
10622 }
10623 break;
10624 }
10625 attr = dwarf2_attr (die, DW_AT_location, cu);
10626 if (attr)
10627 {
10628 var_decode_location (attr, sym, cu);
10629 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10630 if (SYMBOL_CLASS (sym) == LOC_STATIC
10631 && SYMBOL_VALUE_ADDRESS (sym) == 0
10632 && !dwarf2_per_objfile->has_section_at_zero)
10633 {
10634 /* When a static variable is eliminated by the linker,
10635 the corresponding debug information is not stripped
10636 out, but the variable address is set to null;
10637 do not add such variables into symbol table. */
10638 }
10639 else if (attr2 && (DW_UNSND (attr2) != 0))
10640 {
10641 /* Workaround gfortran PR debug/40040 - it uses
10642 DW_AT_location for variables in -fPIC libraries which may
10643 get overriden by other libraries/executable and get
10644 a different address. Resolve it by the minimal symbol
10645 which may come from inferior's executable using copy
10646 relocation. Make this workaround only for gfortran as for
10647 other compilers GDB cannot guess the minimal symbol
10648 Fortran mangling kind. */
10649 if (cu->language == language_fortran && die->parent
10650 && die->parent->tag == DW_TAG_module
10651 && cu->producer
10652 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10653 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10654
10655 /* A variable with DW_AT_external is never static,
10656 but it may be block-scoped. */
10657 list_to_add = (cu->list_in_scope == &file_symbols
10658 ? &global_symbols : cu->list_in_scope);
10659 }
10660 else
10661 list_to_add = cu->list_in_scope;
10662 }
10663 else
10664 {
10665 /* We do not know the address of this symbol.
10666 If it is an external symbol and we have type information
10667 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10668 The address of the variable will then be determined from
10669 the minimal symbol table whenever the variable is
10670 referenced. */
10671 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10672 if (attr2 && (DW_UNSND (attr2) != 0)
10673 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10674 {
10675 /* A variable with DW_AT_external is never static, but it
10676 may be block-scoped. */
10677 list_to_add = (cu->list_in_scope == &file_symbols
10678 ? &global_symbols : cu->list_in_scope);
10679
10680 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10681 }
10682 else if (!die_is_declaration (die, cu))
10683 {
10684 /* Use the default LOC_OPTIMIZED_OUT class. */
10685 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10686 if (!suppress_add)
10687 list_to_add = cu->list_in_scope;
10688 }
10689 }
10690 break;
10691 case DW_TAG_formal_parameter:
10692 /* If we are inside a function, mark this as an argument. If
10693 not, we might be looking at an argument to an inlined function
10694 when we do not have enough information to show inlined frames;
10695 pretend it's a local variable in that case so that the user can
10696 still see it. */
10697 if (context_stack_depth > 0
10698 && context_stack[context_stack_depth - 1].name != NULL)
10699 SYMBOL_IS_ARGUMENT (sym) = 1;
10700 attr = dwarf2_attr (die, DW_AT_location, cu);
10701 if (attr)
10702 {
10703 var_decode_location (attr, sym, cu);
10704 }
10705 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10706 if (attr)
10707 {
10708 dwarf2_const_value (attr, sym, cu);
10709 }
10710 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10711 if (attr && DW_UNSND (attr))
10712 {
10713 struct type *ref_type;
10714
10715 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10716 SYMBOL_TYPE (sym) = ref_type;
10717 }
10718
10719 list_to_add = cu->list_in_scope;
10720 break;
10721 case DW_TAG_unspecified_parameters:
10722 /* From varargs functions; gdb doesn't seem to have any
10723 interest in this information, so just ignore it for now.
10724 (FIXME?) */
10725 break;
10726 case DW_TAG_template_type_param:
10727 suppress_add = 1;
10728 /* Fall through. */
10729 case DW_TAG_class_type:
10730 case DW_TAG_interface_type:
10731 case DW_TAG_structure_type:
10732 case DW_TAG_union_type:
10733 case DW_TAG_set_type:
10734 case DW_TAG_enumeration_type:
10735 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10736 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10737
10738 {
10739 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10740 really ever be static objects: otherwise, if you try
10741 to, say, break of a class's method and you're in a file
10742 which doesn't mention that class, it won't work unless
10743 the check for all static symbols in lookup_symbol_aux
10744 saves you. See the OtherFileClass tests in
10745 gdb.c++/namespace.exp. */
10746
10747 if (!suppress_add)
10748 {
10749 list_to_add = (cu->list_in_scope == &file_symbols
10750 && (cu->language == language_cplus
10751 || cu->language == language_java)
10752 ? &global_symbols : cu->list_in_scope);
10753
10754 /* The semantics of C++ state that "struct foo {
10755 ... }" also defines a typedef for "foo". A Java
10756 class declaration also defines a typedef for the
10757 class. */
10758 if (cu->language == language_cplus
10759 || cu->language == language_java
10760 || cu->language == language_ada)
10761 {
10762 /* The symbol's name is already allocated along
10763 with this objfile, so we don't need to
10764 duplicate it for the type. */
10765 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10766 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10767 }
10768 }
10769 }
10770 break;
10771 case DW_TAG_typedef:
10772 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10773 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10774 list_to_add = cu->list_in_scope;
10775 break;
10776 case DW_TAG_base_type:
10777 case DW_TAG_subrange_type:
10778 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10779 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10780 list_to_add = cu->list_in_scope;
10781 break;
10782 case DW_TAG_enumerator:
10783 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10784 if (attr)
10785 {
10786 dwarf2_const_value (attr, sym, cu);
10787 }
10788 {
10789 /* NOTE: carlton/2003-11-10: See comment above in the
10790 DW_TAG_class_type, etc. block. */
10791
10792 list_to_add = (cu->list_in_scope == &file_symbols
10793 && (cu->language == language_cplus
10794 || cu->language == language_java)
10795 ? &global_symbols : cu->list_in_scope);
10796 }
10797 break;
10798 case DW_TAG_namespace:
10799 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10800 list_to_add = &global_symbols;
10801 break;
10802 default:
10803 /* Not a tag we recognize. Hopefully we aren't processing
10804 trash data, but since we must specifically ignore things
10805 we don't recognize, there is nothing else we should do at
10806 this point. */
10807 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10808 dwarf_tag_name (die->tag));
10809 break;
10810 }
10811
10812 if (suppress_add)
10813 {
10814 sym->hash_next = objfile->template_symbols;
10815 objfile->template_symbols = sym;
10816 list_to_add = NULL;
10817 }
10818
10819 if (list_to_add != NULL)
10820 add_symbol_to_list (sym, list_to_add);
10821
10822 /* For the benefit of old versions of GCC, check for anonymous
10823 namespaces based on the demangled name. */
10824 if (!processing_has_namespace_info
10825 && cu->language == language_cplus)
10826 cp_scan_for_anonymous_namespaces (sym);
10827 }
10828 return (sym);
10829 }
10830
10831 /* A wrapper for new_symbol_full that always allocates a new symbol. */
10832
10833 static struct symbol *
10834 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10835 {
10836 return new_symbol_full (die, type, cu, NULL);
10837 }
10838
10839 /* Given an attr with a DW_FORM_dataN value in host byte order,
10840 zero-extend it as appropriate for the symbol's type. The DWARF
10841 standard (v4) is not entirely clear about the meaning of using
10842 DW_FORM_dataN for a constant with a signed type, where the type is
10843 wider than the data. The conclusion of a discussion on the DWARF
10844 list was that this is unspecified. We choose to always zero-extend
10845 because that is the interpretation long in use by GCC. */
10846
10847 static gdb_byte *
10848 dwarf2_const_value_data (struct attribute *attr, struct type *type,
10849 const char *name, struct obstack *obstack,
10850 struct dwarf2_cu *cu, long *value, int bits)
10851 {
10852 struct objfile *objfile = cu->objfile;
10853 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10854 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10855 LONGEST l = DW_UNSND (attr);
10856
10857 if (bits < sizeof (*value) * 8)
10858 {
10859 l &= ((LONGEST) 1 << bits) - 1;
10860 *value = l;
10861 }
10862 else if (bits == sizeof (*value) * 8)
10863 *value = l;
10864 else
10865 {
10866 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10867 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10868 return bytes;
10869 }
10870
10871 return NULL;
10872 }
10873
10874 /* Read a constant value from an attribute. Either set *VALUE, or if
10875 the value does not fit in *VALUE, set *BYTES - either already
10876 allocated on the objfile obstack, or newly allocated on OBSTACK,
10877 or, set *BATON, if we translated the constant to a location
10878 expression. */
10879
10880 static void
10881 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10882 const char *name, struct obstack *obstack,
10883 struct dwarf2_cu *cu,
10884 long *value, gdb_byte **bytes,
10885 struct dwarf2_locexpr_baton **baton)
10886 {
10887 struct objfile *objfile = cu->objfile;
10888 struct comp_unit_head *cu_header = &cu->header;
10889 struct dwarf_block *blk;
10890 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
10891 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
10892
10893 *value = 0;
10894 *bytes = NULL;
10895 *baton = NULL;
10896
10897 switch (attr->form)
10898 {
10899 case DW_FORM_addr:
10900 {
10901 gdb_byte *data;
10902
10903 if (TYPE_LENGTH (type) != cu_header->addr_size)
10904 dwarf2_const_value_length_mismatch_complaint (name,
10905 cu_header->addr_size,
10906 TYPE_LENGTH (type));
10907 /* Symbols of this form are reasonably rare, so we just
10908 piggyback on the existing location code rather than writing
10909 a new implementation of symbol_computed_ops. */
10910 *baton = obstack_alloc (&objfile->objfile_obstack,
10911 sizeof (struct dwarf2_locexpr_baton));
10912 (*baton)->per_cu = cu->per_cu;
10913 gdb_assert ((*baton)->per_cu);
10914
10915 (*baton)->size = 2 + cu_header->addr_size;
10916 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
10917 (*baton)->data = data;
10918
10919 data[0] = DW_OP_addr;
10920 store_unsigned_integer (&data[1], cu_header->addr_size,
10921 byte_order, DW_ADDR (attr));
10922 data[cu_header->addr_size + 1] = DW_OP_stack_value;
10923 }
10924 break;
10925 case DW_FORM_string:
10926 case DW_FORM_strp:
10927 /* DW_STRING is already allocated on the objfile obstack, point
10928 directly to it. */
10929 *bytes = (gdb_byte *) DW_STRING (attr);
10930 break;
10931 case DW_FORM_block1:
10932 case DW_FORM_block2:
10933 case DW_FORM_block4:
10934 case DW_FORM_block:
10935 case DW_FORM_exprloc:
10936 blk = DW_BLOCK (attr);
10937 if (TYPE_LENGTH (type) != blk->size)
10938 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
10939 TYPE_LENGTH (type));
10940 *bytes = blk->data;
10941 break;
10942
10943 /* The DW_AT_const_value attributes are supposed to carry the
10944 symbol's value "represented as it would be on the target
10945 architecture." By the time we get here, it's already been
10946 converted to host endianness, so we just need to sign- or
10947 zero-extend it as appropriate. */
10948 case DW_FORM_data1:
10949 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
10950 break;
10951 case DW_FORM_data2:
10952 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
10953 break;
10954 case DW_FORM_data4:
10955 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
10956 break;
10957 case DW_FORM_data8:
10958 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
10959 break;
10960
10961 case DW_FORM_sdata:
10962 *value = DW_SND (attr);
10963 break;
10964
10965 case DW_FORM_udata:
10966 *value = DW_UNSND (attr);
10967 break;
10968
10969 default:
10970 complaint (&symfile_complaints,
10971 _("unsupported const value attribute form: '%s'"),
10972 dwarf_form_name (attr->form));
10973 *value = 0;
10974 break;
10975 }
10976 }
10977
10978
10979 /* Copy constant value from an attribute to a symbol. */
10980
10981 static void
10982 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
10983 struct dwarf2_cu *cu)
10984 {
10985 struct objfile *objfile = cu->objfile;
10986 struct comp_unit_head *cu_header = &cu->header;
10987 long value;
10988 gdb_byte *bytes;
10989 struct dwarf2_locexpr_baton *baton;
10990
10991 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
10992 SYMBOL_PRINT_NAME (sym),
10993 &objfile->objfile_obstack, cu,
10994 &value, &bytes, &baton);
10995
10996 if (baton != NULL)
10997 {
10998 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
10999 SYMBOL_LOCATION_BATON (sym) = baton;
11000 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11001 }
11002 else if (bytes != NULL)
11003 {
11004 SYMBOL_VALUE_BYTES (sym) = bytes;
11005 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11006 }
11007 else
11008 {
11009 SYMBOL_VALUE (sym) = value;
11010 SYMBOL_CLASS (sym) = LOC_CONST;
11011 }
11012 }
11013
11014 /* Return the type of the die in question using its DW_AT_type attribute. */
11015
11016 static struct type *
11017 die_type (struct die_info *die, struct dwarf2_cu *cu)
11018 {
11019 struct attribute *type_attr;
11020
11021 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11022 if (!type_attr)
11023 {
11024 /* A missing DW_AT_type represents a void type. */
11025 return objfile_type (cu->objfile)->builtin_void;
11026 }
11027
11028 return lookup_die_type (die, type_attr, cu);
11029 }
11030
11031 /* True iff CU's producer generates GNAT Ada auxiliary information
11032 that allows to find parallel types through that information instead
11033 of having to do expensive parallel lookups by type name. */
11034
11035 static int
11036 need_gnat_info (struct dwarf2_cu *cu)
11037 {
11038 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11039 of GNAT produces this auxiliary information, without any indication
11040 that it is produced. Part of enhancing the FSF version of GNAT
11041 to produce that information will be to put in place an indicator
11042 that we can use in order to determine whether the descriptive type
11043 info is available or not. One suggestion that has been made is
11044 to use a new attribute, attached to the CU die. For now, assume
11045 that the descriptive type info is not available. */
11046 return 0;
11047 }
11048
11049 /* Return the auxiliary type of the die in question using its
11050 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11051 attribute is not present. */
11052
11053 static struct type *
11054 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11055 {
11056 struct attribute *type_attr;
11057
11058 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11059 if (!type_attr)
11060 return NULL;
11061
11062 return lookup_die_type (die, type_attr, cu);
11063 }
11064
11065 /* If DIE has a descriptive_type attribute, then set the TYPE's
11066 descriptive type accordingly. */
11067
11068 static void
11069 set_descriptive_type (struct type *type, struct die_info *die,
11070 struct dwarf2_cu *cu)
11071 {
11072 struct type *descriptive_type = die_descriptive_type (die, cu);
11073
11074 if (descriptive_type)
11075 {
11076 ALLOCATE_GNAT_AUX_TYPE (type);
11077 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11078 }
11079 }
11080
11081 /* Return the containing type of the die in question using its
11082 DW_AT_containing_type attribute. */
11083
11084 static struct type *
11085 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11086 {
11087 struct attribute *type_attr;
11088
11089 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11090 if (!type_attr)
11091 error (_("Dwarf Error: Problem turning containing type into gdb type "
11092 "[in module %s]"), cu->objfile->name);
11093
11094 return lookup_die_type (die, type_attr, cu);
11095 }
11096
11097 /* Look up the type of DIE in CU using its type attribute ATTR.
11098 If there is no type substitute an error marker. */
11099
11100 static struct type *
11101 lookup_die_type (struct die_info *die, struct attribute *attr,
11102 struct dwarf2_cu *cu)
11103 {
11104 struct type *this_type;
11105
11106 /* First see if we have it cached. */
11107
11108 if (is_ref_attr (attr))
11109 {
11110 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11111
11112 this_type = get_die_type_at_offset (offset, cu->per_cu);
11113 }
11114 else if (attr->form == DW_FORM_sig8)
11115 {
11116 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11117 struct dwarf2_cu *sig_cu;
11118 unsigned int offset;
11119
11120 /* sig_type will be NULL if the signatured type is missing from
11121 the debug info. */
11122 if (sig_type == NULL)
11123 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11124 "at 0x%x [in module %s]"),
11125 die->offset, cu->objfile->name);
11126
11127 gdb_assert (sig_type->per_cu.from_debug_types);
11128 offset = sig_type->offset + sig_type->type_offset;
11129 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11130 }
11131 else
11132 {
11133 dump_die_for_error (die);
11134 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11135 dwarf_attr_name (attr->name), cu->objfile->name);
11136 }
11137
11138 /* If not cached we need to read it in. */
11139
11140 if (this_type == NULL)
11141 {
11142 struct die_info *type_die;
11143 struct dwarf2_cu *type_cu = cu;
11144
11145 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11146 /* If the type is cached, we should have found it above. */
11147 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11148 this_type = read_type_die_1 (type_die, type_cu);
11149 }
11150
11151 /* If we still don't have a type use an error marker. */
11152
11153 if (this_type == NULL)
11154 {
11155 char *message, *saved;
11156
11157 /* read_type_die already issued a complaint. */
11158 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11159 cu->objfile->name,
11160 cu->header.offset,
11161 die->offset);
11162 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11163 message, strlen (message));
11164 xfree (message);
11165
11166 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11167 }
11168
11169 return this_type;
11170 }
11171
11172 /* Return the type in DIE, CU.
11173 Returns NULL for invalid types.
11174
11175 This first does a lookup in the appropriate type_hash table,
11176 and only reads the die in if necessary.
11177
11178 NOTE: This can be called when reading in partial or full symbols. */
11179
11180 static struct type *
11181 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11182 {
11183 struct type *this_type;
11184
11185 this_type = get_die_type (die, cu);
11186 if (this_type)
11187 return this_type;
11188
11189 return read_type_die_1 (die, cu);
11190 }
11191
11192 /* Read the type in DIE, CU.
11193 Returns NULL for invalid types. */
11194
11195 static struct type *
11196 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11197 {
11198 struct type *this_type = NULL;
11199
11200 switch (die->tag)
11201 {
11202 case DW_TAG_class_type:
11203 case DW_TAG_interface_type:
11204 case DW_TAG_structure_type:
11205 case DW_TAG_union_type:
11206 this_type = read_structure_type (die, cu);
11207 break;
11208 case DW_TAG_enumeration_type:
11209 this_type = read_enumeration_type (die, cu);
11210 break;
11211 case DW_TAG_subprogram:
11212 case DW_TAG_subroutine_type:
11213 case DW_TAG_inlined_subroutine:
11214 this_type = read_subroutine_type (die, cu);
11215 break;
11216 case DW_TAG_array_type:
11217 this_type = read_array_type (die, cu);
11218 break;
11219 case DW_TAG_set_type:
11220 this_type = read_set_type (die, cu);
11221 break;
11222 case DW_TAG_pointer_type:
11223 this_type = read_tag_pointer_type (die, cu);
11224 break;
11225 case DW_TAG_ptr_to_member_type:
11226 this_type = read_tag_ptr_to_member_type (die, cu);
11227 break;
11228 case DW_TAG_reference_type:
11229 this_type = read_tag_reference_type (die, cu);
11230 break;
11231 case DW_TAG_const_type:
11232 this_type = read_tag_const_type (die, cu);
11233 break;
11234 case DW_TAG_volatile_type:
11235 this_type = read_tag_volatile_type (die, cu);
11236 break;
11237 case DW_TAG_string_type:
11238 this_type = read_tag_string_type (die, cu);
11239 break;
11240 case DW_TAG_typedef:
11241 this_type = read_typedef (die, cu);
11242 break;
11243 case DW_TAG_subrange_type:
11244 this_type = read_subrange_type (die, cu);
11245 break;
11246 case DW_TAG_base_type:
11247 this_type = read_base_type (die, cu);
11248 break;
11249 case DW_TAG_unspecified_type:
11250 this_type = read_unspecified_type (die, cu);
11251 break;
11252 case DW_TAG_namespace:
11253 this_type = read_namespace_type (die, cu);
11254 break;
11255 case DW_TAG_module:
11256 this_type = read_module_type (die, cu);
11257 break;
11258 default:
11259 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
11260 dwarf_tag_name (die->tag));
11261 break;
11262 }
11263
11264 return this_type;
11265 }
11266
11267 /* Return the name of the namespace/class that DIE is defined within,
11268 or "" if we can't tell. The caller should not xfree the result.
11269
11270 For example, if we're within the method foo() in the following
11271 code:
11272
11273 namespace N {
11274 class C {
11275 void foo () {
11276 }
11277 };
11278 }
11279
11280 then determine_prefix on foo's die will return "N::C". */
11281
11282 static char *
11283 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11284 {
11285 struct die_info *parent, *spec_die;
11286 struct dwarf2_cu *spec_cu;
11287 struct type *parent_type;
11288
11289 if (cu->language != language_cplus && cu->language != language_java
11290 && cu->language != language_fortran)
11291 return "";
11292
11293 /* We have to be careful in the presence of DW_AT_specification.
11294 For example, with GCC 3.4, given the code
11295
11296 namespace N {
11297 void foo() {
11298 // Definition of N::foo.
11299 }
11300 }
11301
11302 then we'll have a tree of DIEs like this:
11303
11304 1: DW_TAG_compile_unit
11305 2: DW_TAG_namespace // N
11306 3: DW_TAG_subprogram // declaration of N::foo
11307 4: DW_TAG_subprogram // definition of N::foo
11308 DW_AT_specification // refers to die #3
11309
11310 Thus, when processing die #4, we have to pretend that we're in
11311 the context of its DW_AT_specification, namely the contex of die
11312 #3. */
11313 spec_cu = cu;
11314 spec_die = die_specification (die, &spec_cu);
11315 if (spec_die == NULL)
11316 parent = die->parent;
11317 else
11318 {
11319 parent = spec_die->parent;
11320 cu = spec_cu;
11321 }
11322
11323 if (parent == NULL)
11324 return "";
11325 else if (parent->building_fullname)
11326 {
11327 const char *name;
11328 const char *parent_name;
11329
11330 /* It has been seen on RealView 2.2 built binaries,
11331 DW_TAG_template_type_param types actually _defined_ as
11332 children of the parent class:
11333
11334 enum E {};
11335 template class <class Enum> Class{};
11336 Class<enum E> class_e;
11337
11338 1: DW_TAG_class_type (Class)
11339 2: DW_TAG_enumeration_type (E)
11340 3: DW_TAG_enumerator (enum1:0)
11341 3: DW_TAG_enumerator (enum2:1)
11342 ...
11343 2: DW_TAG_template_type_param
11344 DW_AT_type DW_FORM_ref_udata (E)
11345
11346 Besides being broken debug info, it can put GDB into an
11347 infinite loop. Consider:
11348
11349 When we're building the full name for Class<E>, we'll start
11350 at Class, and go look over its template type parameters,
11351 finding E. We'll then try to build the full name of E, and
11352 reach here. We're now trying to build the full name of E,
11353 and look over the parent DIE for containing scope. In the
11354 broken case, if we followed the parent DIE of E, we'd again
11355 find Class, and once again go look at its template type
11356 arguments, etc., etc. Simply don't consider such parent die
11357 as source-level parent of this die (it can't be, the language
11358 doesn't allow it), and break the loop here. */
11359 name = dwarf2_name (die, cu);
11360 parent_name = dwarf2_name (parent, cu);
11361 complaint (&symfile_complaints,
11362 _("template param type '%s' defined within parent '%s'"),
11363 name ? name : "<unknown>",
11364 parent_name ? parent_name : "<unknown>");
11365 return "";
11366 }
11367 else
11368 switch (parent->tag)
11369 {
11370 case DW_TAG_namespace:
11371 parent_type = read_type_die (parent, cu);
11372 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11373 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11374 Work around this problem here. */
11375 if (cu->language == language_cplus
11376 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11377 return "";
11378 /* We give a name to even anonymous namespaces. */
11379 return TYPE_TAG_NAME (parent_type);
11380 case DW_TAG_class_type:
11381 case DW_TAG_interface_type:
11382 case DW_TAG_structure_type:
11383 case DW_TAG_union_type:
11384 case DW_TAG_module:
11385 parent_type = read_type_die (parent, cu);
11386 if (TYPE_TAG_NAME (parent_type) != NULL)
11387 return TYPE_TAG_NAME (parent_type);
11388 else
11389 /* An anonymous structure is only allowed non-static data
11390 members; no typedefs, no member functions, et cetera.
11391 So it does not need a prefix. */
11392 return "";
11393 default:
11394 return determine_prefix (parent, cu);
11395 }
11396 }
11397
11398 /* Return a newly-allocated string formed by concatenating PREFIX and
11399 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11400 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11401 perform an obconcat, otherwise allocate storage for the result. The CU argument
11402 is used to determine the language and hence, the appropriate separator. */
11403
11404 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
11405
11406 static char *
11407 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11408 int physname, struct dwarf2_cu *cu)
11409 {
11410 const char *lead = "";
11411 const char *sep;
11412
11413 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11414 sep = "";
11415 else if (cu->language == language_java)
11416 sep = ".";
11417 else if (cu->language == language_fortran && physname)
11418 {
11419 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11420 DW_AT_MIPS_linkage_name is preferred and used instead. */
11421
11422 lead = "__";
11423 sep = "_MOD_";
11424 }
11425 else
11426 sep = "::";
11427
11428 if (prefix == NULL)
11429 prefix = "";
11430 if (suffix == NULL)
11431 suffix = "";
11432
11433 if (obs == NULL)
11434 {
11435 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11436
11437 strcpy (retval, lead);
11438 strcat (retval, prefix);
11439 strcat (retval, sep);
11440 strcat (retval, suffix);
11441 return retval;
11442 }
11443 else
11444 {
11445 /* We have an obstack. */
11446 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11447 }
11448 }
11449
11450 /* Return sibling of die, NULL if no sibling. */
11451
11452 static struct die_info *
11453 sibling_die (struct die_info *die)
11454 {
11455 return die->sibling;
11456 }
11457
11458 /* Get name of a die, return NULL if not found. */
11459
11460 static char *
11461 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11462 struct obstack *obstack)
11463 {
11464 if (name && cu->language == language_cplus)
11465 {
11466 char *canon_name = cp_canonicalize_string (name);
11467
11468 if (canon_name != NULL)
11469 {
11470 if (strcmp (canon_name, name) != 0)
11471 name = obsavestring (canon_name, strlen (canon_name),
11472 obstack);
11473 xfree (canon_name);
11474 }
11475 }
11476
11477 return name;
11478 }
11479
11480 /* Get name of a die, return NULL if not found. */
11481
11482 static char *
11483 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11484 {
11485 struct attribute *attr;
11486
11487 attr = dwarf2_attr (die, DW_AT_name, cu);
11488 if (!attr || !DW_STRING (attr))
11489 return NULL;
11490
11491 switch (die->tag)
11492 {
11493 case DW_TAG_compile_unit:
11494 /* Compilation units have a DW_AT_name that is a filename, not
11495 a source language identifier. */
11496 case DW_TAG_enumeration_type:
11497 case DW_TAG_enumerator:
11498 /* These tags always have simple identifiers already; no need
11499 to canonicalize them. */
11500 return DW_STRING (attr);
11501
11502 case DW_TAG_subprogram:
11503 /* Java constructors will all be named "<init>", so return
11504 the class name when we see this special case. */
11505 if (cu->language == language_java
11506 && DW_STRING (attr) != NULL
11507 && strcmp (DW_STRING (attr), "<init>") == 0)
11508 {
11509 struct dwarf2_cu *spec_cu = cu;
11510 struct die_info *spec_die;
11511
11512 /* GCJ will output '<init>' for Java constructor names.
11513 For this special case, return the name of the parent class. */
11514
11515 /* GCJ may output suprogram DIEs with AT_specification set.
11516 If so, use the name of the specified DIE. */
11517 spec_die = die_specification (die, &spec_cu);
11518 if (spec_die != NULL)
11519 return dwarf2_name (spec_die, spec_cu);
11520
11521 do
11522 {
11523 die = die->parent;
11524 if (die->tag == DW_TAG_class_type)
11525 return dwarf2_name (die, cu);
11526 }
11527 while (die->tag != DW_TAG_compile_unit);
11528 }
11529 break;
11530
11531 case DW_TAG_class_type:
11532 case DW_TAG_interface_type:
11533 case DW_TAG_structure_type:
11534 case DW_TAG_union_type:
11535 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11536 structures or unions. These were of the form "._%d" in GCC 4.1,
11537 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11538 and GCC 4.4. We work around this problem by ignoring these. */
11539 if (strncmp (DW_STRING (attr), "._", 2) == 0
11540 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11541 return NULL;
11542 break;
11543
11544 default:
11545 break;
11546 }
11547
11548 if (!DW_STRING_IS_CANONICAL (attr))
11549 {
11550 DW_STRING (attr)
11551 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11552 &cu->objfile->objfile_obstack);
11553 DW_STRING_IS_CANONICAL (attr) = 1;
11554 }
11555 return DW_STRING (attr);
11556 }
11557
11558 /* Return the die that this die in an extension of, or NULL if there
11559 is none. *EXT_CU is the CU containing DIE on input, and the CU
11560 containing the return value on output. */
11561
11562 static struct die_info *
11563 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11564 {
11565 struct attribute *attr;
11566
11567 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11568 if (attr == NULL)
11569 return NULL;
11570
11571 return follow_die_ref (die, attr, ext_cu);
11572 }
11573
11574 /* Convert a DIE tag into its string name. */
11575
11576 static char *
11577 dwarf_tag_name (unsigned tag)
11578 {
11579 switch (tag)
11580 {
11581 case DW_TAG_padding:
11582 return "DW_TAG_padding";
11583 case DW_TAG_array_type:
11584 return "DW_TAG_array_type";
11585 case DW_TAG_class_type:
11586 return "DW_TAG_class_type";
11587 case DW_TAG_entry_point:
11588 return "DW_TAG_entry_point";
11589 case DW_TAG_enumeration_type:
11590 return "DW_TAG_enumeration_type";
11591 case DW_TAG_formal_parameter:
11592 return "DW_TAG_formal_parameter";
11593 case DW_TAG_imported_declaration:
11594 return "DW_TAG_imported_declaration";
11595 case DW_TAG_label:
11596 return "DW_TAG_label";
11597 case DW_TAG_lexical_block:
11598 return "DW_TAG_lexical_block";
11599 case DW_TAG_member:
11600 return "DW_TAG_member";
11601 case DW_TAG_pointer_type:
11602 return "DW_TAG_pointer_type";
11603 case DW_TAG_reference_type:
11604 return "DW_TAG_reference_type";
11605 case DW_TAG_compile_unit:
11606 return "DW_TAG_compile_unit";
11607 case DW_TAG_string_type:
11608 return "DW_TAG_string_type";
11609 case DW_TAG_structure_type:
11610 return "DW_TAG_structure_type";
11611 case DW_TAG_subroutine_type:
11612 return "DW_TAG_subroutine_type";
11613 case DW_TAG_typedef:
11614 return "DW_TAG_typedef";
11615 case DW_TAG_union_type:
11616 return "DW_TAG_union_type";
11617 case DW_TAG_unspecified_parameters:
11618 return "DW_TAG_unspecified_parameters";
11619 case DW_TAG_variant:
11620 return "DW_TAG_variant";
11621 case DW_TAG_common_block:
11622 return "DW_TAG_common_block";
11623 case DW_TAG_common_inclusion:
11624 return "DW_TAG_common_inclusion";
11625 case DW_TAG_inheritance:
11626 return "DW_TAG_inheritance";
11627 case DW_TAG_inlined_subroutine:
11628 return "DW_TAG_inlined_subroutine";
11629 case DW_TAG_module:
11630 return "DW_TAG_module";
11631 case DW_TAG_ptr_to_member_type:
11632 return "DW_TAG_ptr_to_member_type";
11633 case DW_TAG_set_type:
11634 return "DW_TAG_set_type";
11635 case DW_TAG_subrange_type:
11636 return "DW_TAG_subrange_type";
11637 case DW_TAG_with_stmt:
11638 return "DW_TAG_with_stmt";
11639 case DW_TAG_access_declaration:
11640 return "DW_TAG_access_declaration";
11641 case DW_TAG_base_type:
11642 return "DW_TAG_base_type";
11643 case DW_TAG_catch_block:
11644 return "DW_TAG_catch_block";
11645 case DW_TAG_const_type:
11646 return "DW_TAG_const_type";
11647 case DW_TAG_constant:
11648 return "DW_TAG_constant";
11649 case DW_TAG_enumerator:
11650 return "DW_TAG_enumerator";
11651 case DW_TAG_file_type:
11652 return "DW_TAG_file_type";
11653 case DW_TAG_friend:
11654 return "DW_TAG_friend";
11655 case DW_TAG_namelist:
11656 return "DW_TAG_namelist";
11657 case DW_TAG_namelist_item:
11658 return "DW_TAG_namelist_item";
11659 case DW_TAG_packed_type:
11660 return "DW_TAG_packed_type";
11661 case DW_TAG_subprogram:
11662 return "DW_TAG_subprogram";
11663 case DW_TAG_template_type_param:
11664 return "DW_TAG_template_type_param";
11665 case DW_TAG_template_value_param:
11666 return "DW_TAG_template_value_param";
11667 case DW_TAG_thrown_type:
11668 return "DW_TAG_thrown_type";
11669 case DW_TAG_try_block:
11670 return "DW_TAG_try_block";
11671 case DW_TAG_variant_part:
11672 return "DW_TAG_variant_part";
11673 case DW_TAG_variable:
11674 return "DW_TAG_variable";
11675 case DW_TAG_volatile_type:
11676 return "DW_TAG_volatile_type";
11677 case DW_TAG_dwarf_procedure:
11678 return "DW_TAG_dwarf_procedure";
11679 case DW_TAG_restrict_type:
11680 return "DW_TAG_restrict_type";
11681 case DW_TAG_interface_type:
11682 return "DW_TAG_interface_type";
11683 case DW_TAG_namespace:
11684 return "DW_TAG_namespace";
11685 case DW_TAG_imported_module:
11686 return "DW_TAG_imported_module";
11687 case DW_TAG_unspecified_type:
11688 return "DW_TAG_unspecified_type";
11689 case DW_TAG_partial_unit:
11690 return "DW_TAG_partial_unit";
11691 case DW_TAG_imported_unit:
11692 return "DW_TAG_imported_unit";
11693 case DW_TAG_condition:
11694 return "DW_TAG_condition";
11695 case DW_TAG_shared_type:
11696 return "DW_TAG_shared_type";
11697 case DW_TAG_type_unit:
11698 return "DW_TAG_type_unit";
11699 case DW_TAG_MIPS_loop:
11700 return "DW_TAG_MIPS_loop";
11701 case DW_TAG_HP_array_descriptor:
11702 return "DW_TAG_HP_array_descriptor";
11703 case DW_TAG_format_label:
11704 return "DW_TAG_format_label";
11705 case DW_TAG_function_template:
11706 return "DW_TAG_function_template";
11707 case DW_TAG_class_template:
11708 return "DW_TAG_class_template";
11709 case DW_TAG_GNU_BINCL:
11710 return "DW_TAG_GNU_BINCL";
11711 case DW_TAG_GNU_EINCL:
11712 return "DW_TAG_GNU_EINCL";
11713 case DW_TAG_upc_shared_type:
11714 return "DW_TAG_upc_shared_type";
11715 case DW_TAG_upc_strict_type:
11716 return "DW_TAG_upc_strict_type";
11717 case DW_TAG_upc_relaxed_type:
11718 return "DW_TAG_upc_relaxed_type";
11719 case DW_TAG_PGI_kanji_type:
11720 return "DW_TAG_PGI_kanji_type";
11721 case DW_TAG_PGI_interface_block:
11722 return "DW_TAG_PGI_interface_block";
11723 default:
11724 return "DW_TAG_<unknown>";
11725 }
11726 }
11727
11728 /* Convert a DWARF attribute code into its string name. */
11729
11730 static char *
11731 dwarf_attr_name (unsigned attr)
11732 {
11733 switch (attr)
11734 {
11735 case DW_AT_sibling:
11736 return "DW_AT_sibling";
11737 case DW_AT_location:
11738 return "DW_AT_location";
11739 case DW_AT_name:
11740 return "DW_AT_name";
11741 case DW_AT_ordering:
11742 return "DW_AT_ordering";
11743 case DW_AT_subscr_data:
11744 return "DW_AT_subscr_data";
11745 case DW_AT_byte_size:
11746 return "DW_AT_byte_size";
11747 case DW_AT_bit_offset:
11748 return "DW_AT_bit_offset";
11749 case DW_AT_bit_size:
11750 return "DW_AT_bit_size";
11751 case DW_AT_element_list:
11752 return "DW_AT_element_list";
11753 case DW_AT_stmt_list:
11754 return "DW_AT_stmt_list";
11755 case DW_AT_low_pc:
11756 return "DW_AT_low_pc";
11757 case DW_AT_high_pc:
11758 return "DW_AT_high_pc";
11759 case DW_AT_language:
11760 return "DW_AT_language";
11761 case DW_AT_member:
11762 return "DW_AT_member";
11763 case DW_AT_discr:
11764 return "DW_AT_discr";
11765 case DW_AT_discr_value:
11766 return "DW_AT_discr_value";
11767 case DW_AT_visibility:
11768 return "DW_AT_visibility";
11769 case DW_AT_import:
11770 return "DW_AT_import";
11771 case DW_AT_string_length:
11772 return "DW_AT_string_length";
11773 case DW_AT_common_reference:
11774 return "DW_AT_common_reference";
11775 case DW_AT_comp_dir:
11776 return "DW_AT_comp_dir";
11777 case DW_AT_const_value:
11778 return "DW_AT_const_value";
11779 case DW_AT_containing_type:
11780 return "DW_AT_containing_type";
11781 case DW_AT_default_value:
11782 return "DW_AT_default_value";
11783 case DW_AT_inline:
11784 return "DW_AT_inline";
11785 case DW_AT_is_optional:
11786 return "DW_AT_is_optional";
11787 case DW_AT_lower_bound:
11788 return "DW_AT_lower_bound";
11789 case DW_AT_producer:
11790 return "DW_AT_producer";
11791 case DW_AT_prototyped:
11792 return "DW_AT_prototyped";
11793 case DW_AT_return_addr:
11794 return "DW_AT_return_addr";
11795 case DW_AT_start_scope:
11796 return "DW_AT_start_scope";
11797 case DW_AT_bit_stride:
11798 return "DW_AT_bit_stride";
11799 case DW_AT_upper_bound:
11800 return "DW_AT_upper_bound";
11801 case DW_AT_abstract_origin:
11802 return "DW_AT_abstract_origin";
11803 case DW_AT_accessibility:
11804 return "DW_AT_accessibility";
11805 case DW_AT_address_class:
11806 return "DW_AT_address_class";
11807 case DW_AT_artificial:
11808 return "DW_AT_artificial";
11809 case DW_AT_base_types:
11810 return "DW_AT_base_types";
11811 case DW_AT_calling_convention:
11812 return "DW_AT_calling_convention";
11813 case DW_AT_count:
11814 return "DW_AT_count";
11815 case DW_AT_data_member_location:
11816 return "DW_AT_data_member_location";
11817 case DW_AT_decl_column:
11818 return "DW_AT_decl_column";
11819 case DW_AT_decl_file:
11820 return "DW_AT_decl_file";
11821 case DW_AT_decl_line:
11822 return "DW_AT_decl_line";
11823 case DW_AT_declaration:
11824 return "DW_AT_declaration";
11825 case DW_AT_discr_list:
11826 return "DW_AT_discr_list";
11827 case DW_AT_encoding:
11828 return "DW_AT_encoding";
11829 case DW_AT_external:
11830 return "DW_AT_external";
11831 case DW_AT_frame_base:
11832 return "DW_AT_frame_base";
11833 case DW_AT_friend:
11834 return "DW_AT_friend";
11835 case DW_AT_identifier_case:
11836 return "DW_AT_identifier_case";
11837 case DW_AT_macro_info:
11838 return "DW_AT_macro_info";
11839 case DW_AT_namelist_items:
11840 return "DW_AT_namelist_items";
11841 case DW_AT_priority:
11842 return "DW_AT_priority";
11843 case DW_AT_segment:
11844 return "DW_AT_segment";
11845 case DW_AT_specification:
11846 return "DW_AT_specification";
11847 case DW_AT_static_link:
11848 return "DW_AT_static_link";
11849 case DW_AT_type:
11850 return "DW_AT_type";
11851 case DW_AT_use_location:
11852 return "DW_AT_use_location";
11853 case DW_AT_variable_parameter:
11854 return "DW_AT_variable_parameter";
11855 case DW_AT_virtuality:
11856 return "DW_AT_virtuality";
11857 case DW_AT_vtable_elem_location:
11858 return "DW_AT_vtable_elem_location";
11859 /* DWARF 3 values. */
11860 case DW_AT_allocated:
11861 return "DW_AT_allocated";
11862 case DW_AT_associated:
11863 return "DW_AT_associated";
11864 case DW_AT_data_location:
11865 return "DW_AT_data_location";
11866 case DW_AT_byte_stride:
11867 return "DW_AT_byte_stride";
11868 case DW_AT_entry_pc:
11869 return "DW_AT_entry_pc";
11870 case DW_AT_use_UTF8:
11871 return "DW_AT_use_UTF8";
11872 case DW_AT_extension:
11873 return "DW_AT_extension";
11874 case DW_AT_ranges:
11875 return "DW_AT_ranges";
11876 case DW_AT_trampoline:
11877 return "DW_AT_trampoline";
11878 case DW_AT_call_column:
11879 return "DW_AT_call_column";
11880 case DW_AT_call_file:
11881 return "DW_AT_call_file";
11882 case DW_AT_call_line:
11883 return "DW_AT_call_line";
11884 case DW_AT_description:
11885 return "DW_AT_description";
11886 case DW_AT_binary_scale:
11887 return "DW_AT_binary_scale";
11888 case DW_AT_decimal_scale:
11889 return "DW_AT_decimal_scale";
11890 case DW_AT_small:
11891 return "DW_AT_small";
11892 case DW_AT_decimal_sign:
11893 return "DW_AT_decimal_sign";
11894 case DW_AT_digit_count:
11895 return "DW_AT_digit_count";
11896 case DW_AT_picture_string:
11897 return "DW_AT_picture_string";
11898 case DW_AT_mutable:
11899 return "DW_AT_mutable";
11900 case DW_AT_threads_scaled:
11901 return "DW_AT_threads_scaled";
11902 case DW_AT_explicit:
11903 return "DW_AT_explicit";
11904 case DW_AT_object_pointer:
11905 return "DW_AT_object_pointer";
11906 case DW_AT_endianity:
11907 return "DW_AT_endianity";
11908 case DW_AT_elemental:
11909 return "DW_AT_elemental";
11910 case DW_AT_pure:
11911 return "DW_AT_pure";
11912 case DW_AT_recursive:
11913 return "DW_AT_recursive";
11914 /* DWARF 4 values. */
11915 case DW_AT_signature:
11916 return "DW_AT_signature";
11917 case DW_AT_linkage_name:
11918 return "DW_AT_linkage_name";
11919 /* SGI/MIPS extensions. */
11920 #ifdef MIPS /* collides with DW_AT_HP_block_index */
11921 case DW_AT_MIPS_fde:
11922 return "DW_AT_MIPS_fde";
11923 #endif
11924 case DW_AT_MIPS_loop_begin:
11925 return "DW_AT_MIPS_loop_begin";
11926 case DW_AT_MIPS_tail_loop_begin:
11927 return "DW_AT_MIPS_tail_loop_begin";
11928 case DW_AT_MIPS_epilog_begin:
11929 return "DW_AT_MIPS_epilog_begin";
11930 case DW_AT_MIPS_loop_unroll_factor:
11931 return "DW_AT_MIPS_loop_unroll_factor";
11932 case DW_AT_MIPS_software_pipeline_depth:
11933 return "DW_AT_MIPS_software_pipeline_depth";
11934 case DW_AT_MIPS_linkage_name:
11935 return "DW_AT_MIPS_linkage_name";
11936 case DW_AT_MIPS_stride:
11937 return "DW_AT_MIPS_stride";
11938 case DW_AT_MIPS_abstract_name:
11939 return "DW_AT_MIPS_abstract_name";
11940 case DW_AT_MIPS_clone_origin:
11941 return "DW_AT_MIPS_clone_origin";
11942 case DW_AT_MIPS_has_inlines:
11943 return "DW_AT_MIPS_has_inlines";
11944 /* HP extensions. */
11945 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
11946 case DW_AT_HP_block_index:
11947 return "DW_AT_HP_block_index";
11948 #endif
11949 case DW_AT_HP_unmodifiable:
11950 return "DW_AT_HP_unmodifiable";
11951 case DW_AT_HP_actuals_stmt_list:
11952 return "DW_AT_HP_actuals_stmt_list";
11953 case DW_AT_HP_proc_per_section:
11954 return "DW_AT_HP_proc_per_section";
11955 case DW_AT_HP_raw_data_ptr:
11956 return "DW_AT_HP_raw_data_ptr";
11957 case DW_AT_HP_pass_by_reference:
11958 return "DW_AT_HP_pass_by_reference";
11959 case DW_AT_HP_opt_level:
11960 return "DW_AT_HP_opt_level";
11961 case DW_AT_HP_prof_version_id:
11962 return "DW_AT_HP_prof_version_id";
11963 case DW_AT_HP_opt_flags:
11964 return "DW_AT_HP_opt_flags";
11965 case DW_AT_HP_cold_region_low_pc:
11966 return "DW_AT_HP_cold_region_low_pc";
11967 case DW_AT_HP_cold_region_high_pc:
11968 return "DW_AT_HP_cold_region_high_pc";
11969 case DW_AT_HP_all_variables_modifiable:
11970 return "DW_AT_HP_all_variables_modifiable";
11971 case DW_AT_HP_linkage_name:
11972 return "DW_AT_HP_linkage_name";
11973 case DW_AT_HP_prof_flags:
11974 return "DW_AT_HP_prof_flags";
11975 /* GNU extensions. */
11976 case DW_AT_sf_names:
11977 return "DW_AT_sf_names";
11978 case DW_AT_src_info:
11979 return "DW_AT_src_info";
11980 case DW_AT_mac_info:
11981 return "DW_AT_mac_info";
11982 case DW_AT_src_coords:
11983 return "DW_AT_src_coords";
11984 case DW_AT_body_begin:
11985 return "DW_AT_body_begin";
11986 case DW_AT_body_end:
11987 return "DW_AT_body_end";
11988 case DW_AT_GNU_vector:
11989 return "DW_AT_GNU_vector";
11990 case DW_AT_GNU_odr_signature:
11991 return "DW_AT_GNU_odr_signature";
11992 /* VMS extensions. */
11993 case DW_AT_VMS_rtnbeg_pd_address:
11994 return "DW_AT_VMS_rtnbeg_pd_address";
11995 /* UPC extension. */
11996 case DW_AT_upc_threads_scaled:
11997 return "DW_AT_upc_threads_scaled";
11998 /* PGI (STMicroelectronics) extensions. */
11999 case DW_AT_PGI_lbase:
12000 return "DW_AT_PGI_lbase";
12001 case DW_AT_PGI_soffset:
12002 return "DW_AT_PGI_soffset";
12003 case DW_AT_PGI_lstride:
12004 return "DW_AT_PGI_lstride";
12005 default:
12006 return "DW_AT_<unknown>";
12007 }
12008 }
12009
12010 /* Convert a DWARF value form code into its string name. */
12011
12012 static char *
12013 dwarf_form_name (unsigned form)
12014 {
12015 switch (form)
12016 {
12017 case DW_FORM_addr:
12018 return "DW_FORM_addr";
12019 case DW_FORM_block2:
12020 return "DW_FORM_block2";
12021 case DW_FORM_block4:
12022 return "DW_FORM_block4";
12023 case DW_FORM_data2:
12024 return "DW_FORM_data2";
12025 case DW_FORM_data4:
12026 return "DW_FORM_data4";
12027 case DW_FORM_data8:
12028 return "DW_FORM_data8";
12029 case DW_FORM_string:
12030 return "DW_FORM_string";
12031 case DW_FORM_block:
12032 return "DW_FORM_block";
12033 case DW_FORM_block1:
12034 return "DW_FORM_block1";
12035 case DW_FORM_data1:
12036 return "DW_FORM_data1";
12037 case DW_FORM_flag:
12038 return "DW_FORM_flag";
12039 case DW_FORM_sdata:
12040 return "DW_FORM_sdata";
12041 case DW_FORM_strp:
12042 return "DW_FORM_strp";
12043 case DW_FORM_udata:
12044 return "DW_FORM_udata";
12045 case DW_FORM_ref_addr:
12046 return "DW_FORM_ref_addr";
12047 case DW_FORM_ref1:
12048 return "DW_FORM_ref1";
12049 case DW_FORM_ref2:
12050 return "DW_FORM_ref2";
12051 case DW_FORM_ref4:
12052 return "DW_FORM_ref4";
12053 case DW_FORM_ref8:
12054 return "DW_FORM_ref8";
12055 case DW_FORM_ref_udata:
12056 return "DW_FORM_ref_udata";
12057 case DW_FORM_indirect:
12058 return "DW_FORM_indirect";
12059 case DW_FORM_sec_offset:
12060 return "DW_FORM_sec_offset";
12061 case DW_FORM_exprloc:
12062 return "DW_FORM_exprloc";
12063 case DW_FORM_flag_present:
12064 return "DW_FORM_flag_present";
12065 case DW_FORM_sig8:
12066 return "DW_FORM_sig8";
12067 default:
12068 return "DW_FORM_<unknown>";
12069 }
12070 }
12071
12072 /* Convert a DWARF stack opcode into its string name. */
12073
12074 const char *
12075 dwarf_stack_op_name (unsigned op, int def)
12076 {
12077 switch (op)
12078 {
12079 case DW_OP_addr:
12080 return "DW_OP_addr";
12081 case DW_OP_deref:
12082 return "DW_OP_deref";
12083 case DW_OP_const1u:
12084 return "DW_OP_const1u";
12085 case DW_OP_const1s:
12086 return "DW_OP_const1s";
12087 case DW_OP_const2u:
12088 return "DW_OP_const2u";
12089 case DW_OP_const2s:
12090 return "DW_OP_const2s";
12091 case DW_OP_const4u:
12092 return "DW_OP_const4u";
12093 case DW_OP_const4s:
12094 return "DW_OP_const4s";
12095 case DW_OP_const8u:
12096 return "DW_OP_const8u";
12097 case DW_OP_const8s:
12098 return "DW_OP_const8s";
12099 case DW_OP_constu:
12100 return "DW_OP_constu";
12101 case DW_OP_consts:
12102 return "DW_OP_consts";
12103 case DW_OP_dup:
12104 return "DW_OP_dup";
12105 case DW_OP_drop:
12106 return "DW_OP_drop";
12107 case DW_OP_over:
12108 return "DW_OP_over";
12109 case DW_OP_pick:
12110 return "DW_OP_pick";
12111 case DW_OP_swap:
12112 return "DW_OP_swap";
12113 case DW_OP_rot:
12114 return "DW_OP_rot";
12115 case DW_OP_xderef:
12116 return "DW_OP_xderef";
12117 case DW_OP_abs:
12118 return "DW_OP_abs";
12119 case DW_OP_and:
12120 return "DW_OP_and";
12121 case DW_OP_div:
12122 return "DW_OP_div";
12123 case DW_OP_minus:
12124 return "DW_OP_minus";
12125 case DW_OP_mod:
12126 return "DW_OP_mod";
12127 case DW_OP_mul:
12128 return "DW_OP_mul";
12129 case DW_OP_neg:
12130 return "DW_OP_neg";
12131 case DW_OP_not:
12132 return "DW_OP_not";
12133 case DW_OP_or:
12134 return "DW_OP_or";
12135 case DW_OP_plus:
12136 return "DW_OP_plus";
12137 case DW_OP_plus_uconst:
12138 return "DW_OP_plus_uconst";
12139 case DW_OP_shl:
12140 return "DW_OP_shl";
12141 case DW_OP_shr:
12142 return "DW_OP_shr";
12143 case DW_OP_shra:
12144 return "DW_OP_shra";
12145 case DW_OP_xor:
12146 return "DW_OP_xor";
12147 case DW_OP_bra:
12148 return "DW_OP_bra";
12149 case DW_OP_eq:
12150 return "DW_OP_eq";
12151 case DW_OP_ge:
12152 return "DW_OP_ge";
12153 case DW_OP_gt:
12154 return "DW_OP_gt";
12155 case DW_OP_le:
12156 return "DW_OP_le";
12157 case DW_OP_lt:
12158 return "DW_OP_lt";
12159 case DW_OP_ne:
12160 return "DW_OP_ne";
12161 case DW_OP_skip:
12162 return "DW_OP_skip";
12163 case DW_OP_lit0:
12164 return "DW_OP_lit0";
12165 case DW_OP_lit1:
12166 return "DW_OP_lit1";
12167 case DW_OP_lit2:
12168 return "DW_OP_lit2";
12169 case DW_OP_lit3:
12170 return "DW_OP_lit3";
12171 case DW_OP_lit4:
12172 return "DW_OP_lit4";
12173 case DW_OP_lit5:
12174 return "DW_OP_lit5";
12175 case DW_OP_lit6:
12176 return "DW_OP_lit6";
12177 case DW_OP_lit7:
12178 return "DW_OP_lit7";
12179 case DW_OP_lit8:
12180 return "DW_OP_lit8";
12181 case DW_OP_lit9:
12182 return "DW_OP_lit9";
12183 case DW_OP_lit10:
12184 return "DW_OP_lit10";
12185 case DW_OP_lit11:
12186 return "DW_OP_lit11";
12187 case DW_OP_lit12:
12188 return "DW_OP_lit12";
12189 case DW_OP_lit13:
12190 return "DW_OP_lit13";
12191 case DW_OP_lit14:
12192 return "DW_OP_lit14";
12193 case DW_OP_lit15:
12194 return "DW_OP_lit15";
12195 case DW_OP_lit16:
12196 return "DW_OP_lit16";
12197 case DW_OP_lit17:
12198 return "DW_OP_lit17";
12199 case DW_OP_lit18:
12200 return "DW_OP_lit18";
12201 case DW_OP_lit19:
12202 return "DW_OP_lit19";
12203 case DW_OP_lit20:
12204 return "DW_OP_lit20";
12205 case DW_OP_lit21:
12206 return "DW_OP_lit21";
12207 case DW_OP_lit22:
12208 return "DW_OP_lit22";
12209 case DW_OP_lit23:
12210 return "DW_OP_lit23";
12211 case DW_OP_lit24:
12212 return "DW_OP_lit24";
12213 case DW_OP_lit25:
12214 return "DW_OP_lit25";
12215 case DW_OP_lit26:
12216 return "DW_OP_lit26";
12217 case DW_OP_lit27:
12218 return "DW_OP_lit27";
12219 case DW_OP_lit28:
12220 return "DW_OP_lit28";
12221 case DW_OP_lit29:
12222 return "DW_OP_lit29";
12223 case DW_OP_lit30:
12224 return "DW_OP_lit30";
12225 case DW_OP_lit31:
12226 return "DW_OP_lit31";
12227 case DW_OP_reg0:
12228 return "DW_OP_reg0";
12229 case DW_OP_reg1:
12230 return "DW_OP_reg1";
12231 case DW_OP_reg2:
12232 return "DW_OP_reg2";
12233 case DW_OP_reg3:
12234 return "DW_OP_reg3";
12235 case DW_OP_reg4:
12236 return "DW_OP_reg4";
12237 case DW_OP_reg5:
12238 return "DW_OP_reg5";
12239 case DW_OP_reg6:
12240 return "DW_OP_reg6";
12241 case DW_OP_reg7:
12242 return "DW_OP_reg7";
12243 case DW_OP_reg8:
12244 return "DW_OP_reg8";
12245 case DW_OP_reg9:
12246 return "DW_OP_reg9";
12247 case DW_OP_reg10:
12248 return "DW_OP_reg10";
12249 case DW_OP_reg11:
12250 return "DW_OP_reg11";
12251 case DW_OP_reg12:
12252 return "DW_OP_reg12";
12253 case DW_OP_reg13:
12254 return "DW_OP_reg13";
12255 case DW_OP_reg14:
12256 return "DW_OP_reg14";
12257 case DW_OP_reg15:
12258 return "DW_OP_reg15";
12259 case DW_OP_reg16:
12260 return "DW_OP_reg16";
12261 case DW_OP_reg17:
12262 return "DW_OP_reg17";
12263 case DW_OP_reg18:
12264 return "DW_OP_reg18";
12265 case DW_OP_reg19:
12266 return "DW_OP_reg19";
12267 case DW_OP_reg20:
12268 return "DW_OP_reg20";
12269 case DW_OP_reg21:
12270 return "DW_OP_reg21";
12271 case DW_OP_reg22:
12272 return "DW_OP_reg22";
12273 case DW_OP_reg23:
12274 return "DW_OP_reg23";
12275 case DW_OP_reg24:
12276 return "DW_OP_reg24";
12277 case DW_OP_reg25:
12278 return "DW_OP_reg25";
12279 case DW_OP_reg26:
12280 return "DW_OP_reg26";
12281 case DW_OP_reg27:
12282 return "DW_OP_reg27";
12283 case DW_OP_reg28:
12284 return "DW_OP_reg28";
12285 case DW_OP_reg29:
12286 return "DW_OP_reg29";
12287 case DW_OP_reg30:
12288 return "DW_OP_reg30";
12289 case DW_OP_reg31:
12290 return "DW_OP_reg31";
12291 case DW_OP_breg0:
12292 return "DW_OP_breg0";
12293 case DW_OP_breg1:
12294 return "DW_OP_breg1";
12295 case DW_OP_breg2:
12296 return "DW_OP_breg2";
12297 case DW_OP_breg3:
12298 return "DW_OP_breg3";
12299 case DW_OP_breg4:
12300 return "DW_OP_breg4";
12301 case DW_OP_breg5:
12302 return "DW_OP_breg5";
12303 case DW_OP_breg6:
12304 return "DW_OP_breg6";
12305 case DW_OP_breg7:
12306 return "DW_OP_breg7";
12307 case DW_OP_breg8:
12308 return "DW_OP_breg8";
12309 case DW_OP_breg9:
12310 return "DW_OP_breg9";
12311 case DW_OP_breg10:
12312 return "DW_OP_breg10";
12313 case DW_OP_breg11:
12314 return "DW_OP_breg11";
12315 case DW_OP_breg12:
12316 return "DW_OP_breg12";
12317 case DW_OP_breg13:
12318 return "DW_OP_breg13";
12319 case DW_OP_breg14:
12320 return "DW_OP_breg14";
12321 case DW_OP_breg15:
12322 return "DW_OP_breg15";
12323 case DW_OP_breg16:
12324 return "DW_OP_breg16";
12325 case DW_OP_breg17:
12326 return "DW_OP_breg17";
12327 case DW_OP_breg18:
12328 return "DW_OP_breg18";
12329 case DW_OP_breg19:
12330 return "DW_OP_breg19";
12331 case DW_OP_breg20:
12332 return "DW_OP_breg20";
12333 case DW_OP_breg21:
12334 return "DW_OP_breg21";
12335 case DW_OP_breg22:
12336 return "DW_OP_breg22";
12337 case DW_OP_breg23:
12338 return "DW_OP_breg23";
12339 case DW_OP_breg24:
12340 return "DW_OP_breg24";
12341 case DW_OP_breg25:
12342 return "DW_OP_breg25";
12343 case DW_OP_breg26:
12344 return "DW_OP_breg26";
12345 case DW_OP_breg27:
12346 return "DW_OP_breg27";
12347 case DW_OP_breg28:
12348 return "DW_OP_breg28";
12349 case DW_OP_breg29:
12350 return "DW_OP_breg29";
12351 case DW_OP_breg30:
12352 return "DW_OP_breg30";
12353 case DW_OP_breg31:
12354 return "DW_OP_breg31";
12355 case DW_OP_regx:
12356 return "DW_OP_regx";
12357 case DW_OP_fbreg:
12358 return "DW_OP_fbreg";
12359 case DW_OP_bregx:
12360 return "DW_OP_bregx";
12361 case DW_OP_piece:
12362 return "DW_OP_piece";
12363 case DW_OP_deref_size:
12364 return "DW_OP_deref_size";
12365 case DW_OP_xderef_size:
12366 return "DW_OP_xderef_size";
12367 case DW_OP_nop:
12368 return "DW_OP_nop";
12369 /* DWARF 3 extensions. */
12370 case DW_OP_push_object_address:
12371 return "DW_OP_push_object_address";
12372 case DW_OP_call2:
12373 return "DW_OP_call2";
12374 case DW_OP_call4:
12375 return "DW_OP_call4";
12376 case DW_OP_call_ref:
12377 return "DW_OP_call_ref";
12378 case DW_OP_form_tls_address:
12379 return "DW_OP_form_tls_address";
12380 case DW_OP_call_frame_cfa:
12381 return "DW_OP_call_frame_cfa";
12382 case DW_OP_bit_piece:
12383 return "DW_OP_bit_piece";
12384 /* DWARF 4 extensions. */
12385 case DW_OP_implicit_value:
12386 return "DW_OP_implicit_value";
12387 case DW_OP_stack_value:
12388 return "DW_OP_stack_value";
12389 /* GNU extensions. */
12390 case DW_OP_GNU_push_tls_address:
12391 return "DW_OP_GNU_push_tls_address";
12392 case DW_OP_GNU_uninit:
12393 return "DW_OP_GNU_uninit";
12394 default:
12395 return def ? "OP_<unknown>" : NULL;
12396 }
12397 }
12398
12399 static char *
12400 dwarf_bool_name (unsigned mybool)
12401 {
12402 if (mybool)
12403 return "TRUE";
12404 else
12405 return "FALSE";
12406 }
12407
12408 /* Convert a DWARF type code into its string name. */
12409
12410 static char *
12411 dwarf_type_encoding_name (unsigned enc)
12412 {
12413 switch (enc)
12414 {
12415 case DW_ATE_void:
12416 return "DW_ATE_void";
12417 case DW_ATE_address:
12418 return "DW_ATE_address";
12419 case DW_ATE_boolean:
12420 return "DW_ATE_boolean";
12421 case DW_ATE_complex_float:
12422 return "DW_ATE_complex_float";
12423 case DW_ATE_float:
12424 return "DW_ATE_float";
12425 case DW_ATE_signed:
12426 return "DW_ATE_signed";
12427 case DW_ATE_signed_char:
12428 return "DW_ATE_signed_char";
12429 case DW_ATE_unsigned:
12430 return "DW_ATE_unsigned";
12431 case DW_ATE_unsigned_char:
12432 return "DW_ATE_unsigned_char";
12433 /* DWARF 3. */
12434 case DW_ATE_imaginary_float:
12435 return "DW_ATE_imaginary_float";
12436 case DW_ATE_packed_decimal:
12437 return "DW_ATE_packed_decimal";
12438 case DW_ATE_numeric_string:
12439 return "DW_ATE_numeric_string";
12440 case DW_ATE_edited:
12441 return "DW_ATE_edited";
12442 case DW_ATE_signed_fixed:
12443 return "DW_ATE_signed_fixed";
12444 case DW_ATE_unsigned_fixed:
12445 return "DW_ATE_unsigned_fixed";
12446 case DW_ATE_decimal_float:
12447 return "DW_ATE_decimal_float";
12448 /* DWARF 4. */
12449 case DW_ATE_UTF:
12450 return "DW_ATE_UTF";
12451 /* HP extensions. */
12452 case DW_ATE_HP_float80:
12453 return "DW_ATE_HP_float80";
12454 case DW_ATE_HP_complex_float80:
12455 return "DW_ATE_HP_complex_float80";
12456 case DW_ATE_HP_float128:
12457 return "DW_ATE_HP_float128";
12458 case DW_ATE_HP_complex_float128:
12459 return "DW_ATE_HP_complex_float128";
12460 case DW_ATE_HP_floathpintel:
12461 return "DW_ATE_HP_floathpintel";
12462 case DW_ATE_HP_imaginary_float80:
12463 return "DW_ATE_HP_imaginary_float80";
12464 case DW_ATE_HP_imaginary_float128:
12465 return "DW_ATE_HP_imaginary_float128";
12466 default:
12467 return "DW_ATE_<unknown>";
12468 }
12469 }
12470
12471 /* Convert a DWARF call frame info operation to its string name. */
12472
12473 #if 0
12474 static char *
12475 dwarf_cfi_name (unsigned cfi_opc)
12476 {
12477 switch (cfi_opc)
12478 {
12479 case DW_CFA_advance_loc:
12480 return "DW_CFA_advance_loc";
12481 case DW_CFA_offset:
12482 return "DW_CFA_offset";
12483 case DW_CFA_restore:
12484 return "DW_CFA_restore";
12485 case DW_CFA_nop:
12486 return "DW_CFA_nop";
12487 case DW_CFA_set_loc:
12488 return "DW_CFA_set_loc";
12489 case DW_CFA_advance_loc1:
12490 return "DW_CFA_advance_loc1";
12491 case DW_CFA_advance_loc2:
12492 return "DW_CFA_advance_loc2";
12493 case DW_CFA_advance_loc4:
12494 return "DW_CFA_advance_loc4";
12495 case DW_CFA_offset_extended:
12496 return "DW_CFA_offset_extended";
12497 case DW_CFA_restore_extended:
12498 return "DW_CFA_restore_extended";
12499 case DW_CFA_undefined:
12500 return "DW_CFA_undefined";
12501 case DW_CFA_same_value:
12502 return "DW_CFA_same_value";
12503 case DW_CFA_register:
12504 return "DW_CFA_register";
12505 case DW_CFA_remember_state:
12506 return "DW_CFA_remember_state";
12507 case DW_CFA_restore_state:
12508 return "DW_CFA_restore_state";
12509 case DW_CFA_def_cfa:
12510 return "DW_CFA_def_cfa";
12511 case DW_CFA_def_cfa_register:
12512 return "DW_CFA_def_cfa_register";
12513 case DW_CFA_def_cfa_offset:
12514 return "DW_CFA_def_cfa_offset";
12515 /* DWARF 3. */
12516 case DW_CFA_def_cfa_expression:
12517 return "DW_CFA_def_cfa_expression";
12518 case DW_CFA_expression:
12519 return "DW_CFA_expression";
12520 case DW_CFA_offset_extended_sf:
12521 return "DW_CFA_offset_extended_sf";
12522 case DW_CFA_def_cfa_sf:
12523 return "DW_CFA_def_cfa_sf";
12524 case DW_CFA_def_cfa_offset_sf:
12525 return "DW_CFA_def_cfa_offset_sf";
12526 case DW_CFA_val_offset:
12527 return "DW_CFA_val_offset";
12528 case DW_CFA_val_offset_sf:
12529 return "DW_CFA_val_offset_sf";
12530 case DW_CFA_val_expression:
12531 return "DW_CFA_val_expression";
12532 /* SGI/MIPS specific. */
12533 case DW_CFA_MIPS_advance_loc8:
12534 return "DW_CFA_MIPS_advance_loc8";
12535 /* GNU extensions. */
12536 case DW_CFA_GNU_window_save:
12537 return "DW_CFA_GNU_window_save";
12538 case DW_CFA_GNU_args_size:
12539 return "DW_CFA_GNU_args_size";
12540 case DW_CFA_GNU_negative_offset_extended:
12541 return "DW_CFA_GNU_negative_offset_extended";
12542 default:
12543 return "DW_CFA_<unknown>";
12544 }
12545 }
12546 #endif
12547
12548 static void
12549 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12550 {
12551 unsigned int i;
12552
12553 print_spaces (indent, f);
12554 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12555 dwarf_tag_name (die->tag), die->abbrev, die->offset);
12556
12557 if (die->parent != NULL)
12558 {
12559 print_spaces (indent, f);
12560 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12561 die->parent->offset);
12562 }
12563
12564 print_spaces (indent, f);
12565 fprintf_unfiltered (f, " has children: %s\n",
12566 dwarf_bool_name (die->child != NULL));
12567
12568 print_spaces (indent, f);
12569 fprintf_unfiltered (f, " attributes:\n");
12570
12571 for (i = 0; i < die->num_attrs; ++i)
12572 {
12573 print_spaces (indent, f);
12574 fprintf_unfiltered (f, " %s (%s) ",
12575 dwarf_attr_name (die->attrs[i].name),
12576 dwarf_form_name (die->attrs[i].form));
12577
12578 switch (die->attrs[i].form)
12579 {
12580 case DW_FORM_ref_addr:
12581 case DW_FORM_addr:
12582 fprintf_unfiltered (f, "address: ");
12583 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12584 break;
12585 case DW_FORM_block2:
12586 case DW_FORM_block4:
12587 case DW_FORM_block:
12588 case DW_FORM_block1:
12589 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12590 break;
12591 case DW_FORM_exprloc:
12592 fprintf_unfiltered (f, "expression: size %u",
12593 DW_BLOCK (&die->attrs[i])->size);
12594 break;
12595 case DW_FORM_ref1:
12596 case DW_FORM_ref2:
12597 case DW_FORM_ref4:
12598 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12599 (long) (DW_ADDR (&die->attrs[i])));
12600 break;
12601 case DW_FORM_data1:
12602 case DW_FORM_data2:
12603 case DW_FORM_data4:
12604 case DW_FORM_data8:
12605 case DW_FORM_udata:
12606 case DW_FORM_sdata:
12607 fprintf_unfiltered (f, "constant: %s",
12608 pulongest (DW_UNSND (&die->attrs[i])));
12609 break;
12610 case DW_FORM_sec_offset:
12611 fprintf_unfiltered (f, "section offset: %s",
12612 pulongest (DW_UNSND (&die->attrs[i])));
12613 break;
12614 case DW_FORM_sig8:
12615 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12616 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12617 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12618 else
12619 fprintf_unfiltered (f, "signatured type, offset: unknown");
12620 break;
12621 case DW_FORM_string:
12622 case DW_FORM_strp:
12623 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12624 DW_STRING (&die->attrs[i])
12625 ? DW_STRING (&die->attrs[i]) : "",
12626 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12627 break;
12628 case DW_FORM_flag:
12629 if (DW_UNSND (&die->attrs[i]))
12630 fprintf_unfiltered (f, "flag: TRUE");
12631 else
12632 fprintf_unfiltered (f, "flag: FALSE");
12633 break;
12634 case DW_FORM_flag_present:
12635 fprintf_unfiltered (f, "flag: TRUE");
12636 break;
12637 case DW_FORM_indirect:
12638 /* the reader will have reduced the indirect form to
12639 the "base form" so this form should not occur */
12640 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12641 break;
12642 default:
12643 fprintf_unfiltered (f, "unsupported attribute form: %d.",
12644 die->attrs[i].form);
12645 break;
12646 }
12647 fprintf_unfiltered (f, "\n");
12648 }
12649 }
12650
12651 static void
12652 dump_die_for_error (struct die_info *die)
12653 {
12654 dump_die_shallow (gdb_stderr, 0, die);
12655 }
12656
12657 static void
12658 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12659 {
12660 int indent = level * 4;
12661
12662 gdb_assert (die != NULL);
12663
12664 if (level >= max_level)
12665 return;
12666
12667 dump_die_shallow (f, indent, die);
12668
12669 if (die->child != NULL)
12670 {
12671 print_spaces (indent, f);
12672 fprintf_unfiltered (f, " Children:");
12673 if (level + 1 < max_level)
12674 {
12675 fprintf_unfiltered (f, "\n");
12676 dump_die_1 (f, level + 1, max_level, die->child);
12677 }
12678 else
12679 {
12680 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12681 }
12682 }
12683
12684 if (die->sibling != NULL && level > 0)
12685 {
12686 dump_die_1 (f, level, max_level, die->sibling);
12687 }
12688 }
12689
12690 /* This is called from the pdie macro in gdbinit.in.
12691 It's not static so gcc will keep a copy callable from gdb. */
12692
12693 void
12694 dump_die (struct die_info *die, int max_level)
12695 {
12696 dump_die_1 (gdb_stdlog, 0, max_level, die);
12697 }
12698
12699 static void
12700 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12701 {
12702 void **slot;
12703
12704 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12705
12706 *slot = die;
12707 }
12708
12709 static int
12710 is_ref_attr (struct attribute *attr)
12711 {
12712 switch (attr->form)
12713 {
12714 case DW_FORM_ref_addr:
12715 case DW_FORM_ref1:
12716 case DW_FORM_ref2:
12717 case DW_FORM_ref4:
12718 case DW_FORM_ref8:
12719 case DW_FORM_ref_udata:
12720 return 1;
12721 default:
12722 return 0;
12723 }
12724 }
12725
12726 static unsigned int
12727 dwarf2_get_ref_die_offset (struct attribute *attr)
12728 {
12729 if (is_ref_attr (attr))
12730 return DW_ADDR (attr);
12731
12732 complaint (&symfile_complaints,
12733 _("unsupported die ref attribute form: '%s'"),
12734 dwarf_form_name (attr->form));
12735 return 0;
12736 }
12737
12738 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12739 * the value held by the attribute is not constant. */
12740
12741 static LONGEST
12742 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12743 {
12744 if (attr->form == DW_FORM_sdata)
12745 return DW_SND (attr);
12746 else if (attr->form == DW_FORM_udata
12747 || attr->form == DW_FORM_data1
12748 || attr->form == DW_FORM_data2
12749 || attr->form == DW_FORM_data4
12750 || attr->form == DW_FORM_data8)
12751 return DW_UNSND (attr);
12752 else
12753 {
12754 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12755 dwarf_form_name (attr->form));
12756 return default_value;
12757 }
12758 }
12759
12760 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
12761 unit and add it to our queue.
12762 The result is non-zero if PER_CU was queued, otherwise the result is zero
12763 meaning either PER_CU is already queued or it is already loaded. */
12764
12765 static int
12766 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12767 struct dwarf2_per_cu_data *per_cu)
12768 {
12769 /* We may arrive here during partial symbol reading, if we need full
12770 DIEs to process an unusual case (e.g. template arguments). Do
12771 not queue PER_CU, just tell our caller to load its DIEs. */
12772 if (dwarf2_per_objfile->reading_partial_symbols)
12773 {
12774 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12775 return 1;
12776 return 0;
12777 }
12778
12779 /* Mark the dependence relation so that we don't flush PER_CU
12780 too early. */
12781 dwarf2_add_dependence (this_cu, per_cu);
12782
12783 /* If it's already on the queue, we have nothing to do. */
12784 if (per_cu->queued)
12785 return 0;
12786
12787 /* If the compilation unit is already loaded, just mark it as
12788 used. */
12789 if (per_cu->cu != NULL)
12790 {
12791 per_cu->cu->last_used = 0;
12792 return 0;
12793 }
12794
12795 /* Add it to the queue. */
12796 queue_comp_unit (per_cu, this_cu->objfile);
12797
12798 return 1;
12799 }
12800
12801 /* Follow reference or signature attribute ATTR of SRC_DIE.
12802 On entry *REF_CU is the CU of SRC_DIE.
12803 On exit *REF_CU is the CU of the result. */
12804
12805 static struct die_info *
12806 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
12807 struct dwarf2_cu **ref_cu)
12808 {
12809 struct die_info *die;
12810
12811 if (is_ref_attr (attr))
12812 die = follow_die_ref (src_die, attr, ref_cu);
12813 else if (attr->form == DW_FORM_sig8)
12814 die = follow_die_sig (src_die, attr, ref_cu);
12815 else
12816 {
12817 dump_die_for_error (src_die);
12818 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
12819 (*ref_cu)->objfile->name);
12820 }
12821
12822 return die;
12823 }
12824
12825 /* Follow reference OFFSET.
12826 On entry *REF_CU is the CU of the source die referencing OFFSET.
12827 On exit *REF_CU is the CU of the result.
12828 Returns NULL if OFFSET is invalid. */
12829
12830 static struct die_info *
12831 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
12832 {
12833 struct die_info temp_die;
12834 struct dwarf2_cu *target_cu, *cu = *ref_cu;
12835
12836 gdb_assert (cu->per_cu != NULL);
12837
12838 target_cu = cu;
12839
12840 if (cu->per_cu->from_debug_types)
12841 {
12842 /* .debug_types CUs cannot reference anything outside their CU.
12843 If they need to, they have to reference a signatured type via
12844 DW_FORM_sig8. */
12845 if (! offset_in_cu_p (&cu->header, offset))
12846 return NULL;
12847 }
12848 else if (! offset_in_cu_p (&cu->header, offset))
12849 {
12850 struct dwarf2_per_cu_data *per_cu;
12851
12852 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
12853
12854 /* If necessary, add it to the queue and load its DIEs. */
12855 if (maybe_queue_comp_unit (cu, per_cu))
12856 load_full_comp_unit (per_cu, cu->objfile);
12857
12858 target_cu = per_cu->cu;
12859 }
12860 else if (cu->dies == NULL)
12861 {
12862 /* We're loading full DIEs during partial symbol reading. */
12863 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
12864 load_full_comp_unit (cu->per_cu, cu->objfile);
12865 }
12866
12867 *ref_cu = target_cu;
12868 temp_die.offset = offset;
12869 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
12870 }
12871
12872 /* Follow reference attribute ATTR of SRC_DIE.
12873 On entry *REF_CU is the CU of SRC_DIE.
12874 On exit *REF_CU is the CU of the result. */
12875
12876 static struct die_info *
12877 follow_die_ref (struct die_info *src_die, struct attribute *attr,
12878 struct dwarf2_cu **ref_cu)
12879 {
12880 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12881 struct dwarf2_cu *cu = *ref_cu;
12882 struct die_info *die;
12883
12884 die = follow_die_offset (offset, ref_cu);
12885 if (!die)
12886 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
12887 "at 0x%x [in module %s]"),
12888 offset, src_die->offset, cu->objfile->name);
12889
12890 return die;
12891 }
12892
12893 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
12894 value is intended for DW_OP_call*. */
12895
12896 struct dwarf2_locexpr_baton
12897 dwarf2_fetch_die_location_block (unsigned int offset,
12898 struct dwarf2_per_cu_data *per_cu)
12899 {
12900 struct dwarf2_cu *cu = per_cu->cu;
12901 struct die_info *die;
12902 struct attribute *attr;
12903 struct dwarf2_locexpr_baton retval;
12904
12905 die = follow_die_offset (offset, &cu);
12906 if (!die)
12907 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
12908 offset, per_cu->cu->objfile->name);
12909
12910 attr = dwarf2_attr (die, DW_AT_location, cu);
12911 if (!attr)
12912 {
12913 /* DWARF: "If there is no such attribute, then there is no effect.". */
12914
12915 retval.data = NULL;
12916 retval.size = 0;
12917 }
12918 else
12919 {
12920 if (!attr_form_is_block (attr))
12921 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
12922 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
12923 offset, per_cu->cu->objfile->name);
12924
12925 retval.data = DW_BLOCK (attr)->data;
12926 retval.size = DW_BLOCK (attr)->size;
12927 }
12928 retval.per_cu = cu->per_cu;
12929 return retval;
12930 }
12931
12932 /* Follow the signature attribute ATTR in SRC_DIE.
12933 On entry *REF_CU is the CU of SRC_DIE.
12934 On exit *REF_CU is the CU of the result. */
12935
12936 static struct die_info *
12937 follow_die_sig (struct die_info *src_die, struct attribute *attr,
12938 struct dwarf2_cu **ref_cu)
12939 {
12940 struct objfile *objfile = (*ref_cu)->objfile;
12941 struct die_info temp_die;
12942 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12943 struct dwarf2_cu *sig_cu;
12944 struct die_info *die;
12945
12946 /* sig_type will be NULL if the signatured type is missing from
12947 the debug info. */
12948 if (sig_type == NULL)
12949 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12950 "at 0x%x [in module %s]"),
12951 src_die->offset, objfile->name);
12952
12953 /* If necessary, add it to the queue and load its DIEs. */
12954
12955 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
12956 read_signatured_type (objfile, sig_type);
12957
12958 gdb_assert (sig_type->per_cu.cu != NULL);
12959
12960 sig_cu = sig_type->per_cu.cu;
12961 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
12962 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
12963 if (die)
12964 {
12965 *ref_cu = sig_cu;
12966 return die;
12967 }
12968
12969 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
12970 "at 0x%x [in module %s]"),
12971 sig_type->type_offset, src_die->offset, objfile->name);
12972 }
12973
12974 /* Given an offset of a signatured type, return its signatured_type. */
12975
12976 static struct signatured_type *
12977 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
12978 {
12979 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
12980 unsigned int length, initial_length_size;
12981 unsigned int sig_offset;
12982 struct signatured_type find_entry, *type_sig;
12983
12984 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
12985 sig_offset = (initial_length_size
12986 + 2 /*version*/
12987 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
12988 + 1 /*address_size*/);
12989 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
12990 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
12991
12992 /* This is only used to lookup previously recorded types.
12993 If we didn't find it, it's our bug. */
12994 gdb_assert (type_sig != NULL);
12995 gdb_assert (offset == type_sig->offset);
12996
12997 return type_sig;
12998 }
12999
13000 /* Read in signatured type at OFFSET and build its CU and die(s). */
13001
13002 static void
13003 read_signatured_type_at_offset (struct objfile *objfile,
13004 unsigned int offset)
13005 {
13006 struct signatured_type *type_sig;
13007
13008 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13009
13010 /* We have the section offset, but we need the signature to do the
13011 hash table lookup. */
13012 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13013
13014 gdb_assert (type_sig->per_cu.cu == NULL);
13015
13016 read_signatured_type (objfile, type_sig);
13017
13018 gdb_assert (type_sig->per_cu.cu != NULL);
13019 }
13020
13021 /* Read in a signatured type and build its CU and DIEs. */
13022
13023 static void
13024 read_signatured_type (struct objfile *objfile,
13025 struct signatured_type *type_sig)
13026 {
13027 gdb_byte *types_ptr;
13028 struct die_reader_specs reader_specs;
13029 struct dwarf2_cu *cu;
13030 ULONGEST signature;
13031 struct cleanup *back_to, *free_cu_cleanup;
13032 struct attribute *attr;
13033
13034 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13035 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13036
13037 gdb_assert (type_sig->per_cu.cu == NULL);
13038
13039 cu = xmalloc (sizeof (struct dwarf2_cu));
13040 memset (cu, 0, sizeof (struct dwarf2_cu));
13041 obstack_init (&cu->comp_unit_obstack);
13042 cu->objfile = objfile;
13043 type_sig->per_cu.cu = cu;
13044 cu->per_cu = &type_sig->per_cu;
13045
13046 /* If an error occurs while loading, release our storage. */
13047 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13048
13049 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13050 types_ptr, objfile->obfd);
13051 gdb_assert (signature == type_sig->signature);
13052
13053 cu->die_hash
13054 = htab_create_alloc_ex (cu->header.length / 12,
13055 die_hash,
13056 die_eq,
13057 NULL,
13058 &cu->comp_unit_obstack,
13059 hashtab_obstack_allocate,
13060 dummy_obstack_deallocate);
13061
13062 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13063 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13064
13065 init_cu_die_reader (&reader_specs, cu);
13066
13067 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13068 NULL /*parent*/);
13069
13070 /* We try not to read any attributes in this function, because not
13071 all objfiles needed for references have been loaded yet, and symbol
13072 table processing isn't initialized. But we have to set the CU language,
13073 or we won't be able to build types correctly. */
13074 attr = dwarf2_attr (cu->dies, DW_AT_language, cu);
13075 if (attr)
13076 set_cu_language (DW_UNSND (attr), cu);
13077 else
13078 set_cu_language (language_minimal, cu);
13079
13080 do_cleanups (back_to);
13081
13082 /* We've successfully allocated this compilation unit. Let our caller
13083 clean it up when finished with it. */
13084 discard_cleanups (free_cu_cleanup);
13085
13086 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13087 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13088 }
13089
13090 /* Decode simple location descriptions.
13091 Given a pointer to a dwarf block that defines a location, compute
13092 the location and return the value.
13093
13094 NOTE drow/2003-11-18: This function is called in two situations
13095 now: for the address of static or global variables (partial symbols
13096 only) and for offsets into structures which are expected to be
13097 (more or less) constant. The partial symbol case should go away,
13098 and only the constant case should remain. That will let this
13099 function complain more accurately. A few special modes are allowed
13100 without complaint for global variables (for instance, global
13101 register values and thread-local values).
13102
13103 A location description containing no operations indicates that the
13104 object is optimized out. The return value is 0 for that case.
13105 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13106 callers will only want a very basic result and this can become a
13107 complaint.
13108
13109 Note that stack[0] is unused except as a default error return.
13110 Note that stack overflow is not yet handled. */
13111
13112 static CORE_ADDR
13113 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13114 {
13115 struct objfile *objfile = cu->objfile;
13116 int i;
13117 int size = blk->size;
13118 gdb_byte *data = blk->data;
13119 CORE_ADDR stack[64];
13120 int stacki;
13121 unsigned int bytes_read, unsnd;
13122 gdb_byte op;
13123
13124 i = 0;
13125 stacki = 0;
13126 stack[stacki] = 0;
13127
13128 while (i < size)
13129 {
13130 op = data[i++];
13131 switch (op)
13132 {
13133 case DW_OP_lit0:
13134 case DW_OP_lit1:
13135 case DW_OP_lit2:
13136 case DW_OP_lit3:
13137 case DW_OP_lit4:
13138 case DW_OP_lit5:
13139 case DW_OP_lit6:
13140 case DW_OP_lit7:
13141 case DW_OP_lit8:
13142 case DW_OP_lit9:
13143 case DW_OP_lit10:
13144 case DW_OP_lit11:
13145 case DW_OP_lit12:
13146 case DW_OP_lit13:
13147 case DW_OP_lit14:
13148 case DW_OP_lit15:
13149 case DW_OP_lit16:
13150 case DW_OP_lit17:
13151 case DW_OP_lit18:
13152 case DW_OP_lit19:
13153 case DW_OP_lit20:
13154 case DW_OP_lit21:
13155 case DW_OP_lit22:
13156 case DW_OP_lit23:
13157 case DW_OP_lit24:
13158 case DW_OP_lit25:
13159 case DW_OP_lit26:
13160 case DW_OP_lit27:
13161 case DW_OP_lit28:
13162 case DW_OP_lit29:
13163 case DW_OP_lit30:
13164 case DW_OP_lit31:
13165 stack[++stacki] = op - DW_OP_lit0;
13166 break;
13167
13168 case DW_OP_reg0:
13169 case DW_OP_reg1:
13170 case DW_OP_reg2:
13171 case DW_OP_reg3:
13172 case DW_OP_reg4:
13173 case DW_OP_reg5:
13174 case DW_OP_reg6:
13175 case DW_OP_reg7:
13176 case DW_OP_reg8:
13177 case DW_OP_reg9:
13178 case DW_OP_reg10:
13179 case DW_OP_reg11:
13180 case DW_OP_reg12:
13181 case DW_OP_reg13:
13182 case DW_OP_reg14:
13183 case DW_OP_reg15:
13184 case DW_OP_reg16:
13185 case DW_OP_reg17:
13186 case DW_OP_reg18:
13187 case DW_OP_reg19:
13188 case DW_OP_reg20:
13189 case DW_OP_reg21:
13190 case DW_OP_reg22:
13191 case DW_OP_reg23:
13192 case DW_OP_reg24:
13193 case DW_OP_reg25:
13194 case DW_OP_reg26:
13195 case DW_OP_reg27:
13196 case DW_OP_reg28:
13197 case DW_OP_reg29:
13198 case DW_OP_reg30:
13199 case DW_OP_reg31:
13200 stack[++stacki] = op - DW_OP_reg0;
13201 if (i < size)
13202 dwarf2_complex_location_expr_complaint ();
13203 break;
13204
13205 case DW_OP_regx:
13206 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13207 i += bytes_read;
13208 stack[++stacki] = unsnd;
13209 if (i < size)
13210 dwarf2_complex_location_expr_complaint ();
13211 break;
13212
13213 case DW_OP_addr:
13214 stack[++stacki] = read_address (objfile->obfd, &data[i],
13215 cu, &bytes_read);
13216 i += bytes_read;
13217 break;
13218
13219 case DW_OP_const1u:
13220 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13221 i += 1;
13222 break;
13223
13224 case DW_OP_const1s:
13225 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13226 i += 1;
13227 break;
13228
13229 case DW_OP_const2u:
13230 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13231 i += 2;
13232 break;
13233
13234 case DW_OP_const2s:
13235 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13236 i += 2;
13237 break;
13238
13239 case DW_OP_const4u:
13240 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13241 i += 4;
13242 break;
13243
13244 case DW_OP_const4s:
13245 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13246 i += 4;
13247 break;
13248
13249 case DW_OP_constu:
13250 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13251 &bytes_read);
13252 i += bytes_read;
13253 break;
13254
13255 case DW_OP_consts:
13256 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13257 i += bytes_read;
13258 break;
13259
13260 case DW_OP_dup:
13261 stack[stacki + 1] = stack[stacki];
13262 stacki++;
13263 break;
13264
13265 case DW_OP_plus:
13266 stack[stacki - 1] += stack[stacki];
13267 stacki--;
13268 break;
13269
13270 case DW_OP_plus_uconst:
13271 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13272 i += bytes_read;
13273 break;
13274
13275 case DW_OP_minus:
13276 stack[stacki - 1] -= stack[stacki];
13277 stacki--;
13278 break;
13279
13280 case DW_OP_deref:
13281 /* If we're not the last op, then we definitely can't encode
13282 this using GDB's address_class enum. This is valid for partial
13283 global symbols, although the variable's address will be bogus
13284 in the psymtab. */
13285 if (i < size)
13286 dwarf2_complex_location_expr_complaint ();
13287 break;
13288
13289 case DW_OP_GNU_push_tls_address:
13290 /* The top of the stack has the offset from the beginning
13291 of the thread control block at which the variable is located. */
13292 /* Nothing should follow this operator, so the top of stack would
13293 be returned. */
13294 /* This is valid for partial global symbols, but the variable's
13295 address will be bogus in the psymtab. */
13296 if (i < size)
13297 dwarf2_complex_location_expr_complaint ();
13298 break;
13299
13300 case DW_OP_GNU_uninit:
13301 break;
13302
13303 default:
13304 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13305 dwarf_stack_op_name (op, 1));
13306 return (stack[stacki]);
13307 }
13308 }
13309 return (stack[stacki]);
13310 }
13311
13312 /* memory allocation interface */
13313
13314 static struct dwarf_block *
13315 dwarf_alloc_block (struct dwarf2_cu *cu)
13316 {
13317 struct dwarf_block *blk;
13318
13319 blk = (struct dwarf_block *)
13320 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13321 return (blk);
13322 }
13323
13324 static struct abbrev_info *
13325 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13326 {
13327 struct abbrev_info *abbrev;
13328
13329 abbrev = (struct abbrev_info *)
13330 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13331 memset (abbrev, 0, sizeof (struct abbrev_info));
13332 return (abbrev);
13333 }
13334
13335 static struct die_info *
13336 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13337 {
13338 struct die_info *die;
13339 size_t size = sizeof (struct die_info);
13340
13341 if (num_attrs > 1)
13342 size += (num_attrs - 1) * sizeof (struct attribute);
13343
13344 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13345 memset (die, 0, sizeof (struct die_info));
13346 return (die);
13347 }
13348
13349 \f
13350 /* Macro support. */
13351
13352
13353 /* Return the full name of file number I in *LH's file name table.
13354 Use COMP_DIR as the name of the current directory of the
13355 compilation. The result is allocated using xmalloc; the caller is
13356 responsible for freeing it. */
13357 static char *
13358 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13359 {
13360 /* Is the file number a valid index into the line header's file name
13361 table? Remember that file numbers start with one, not zero. */
13362 if (1 <= file && file <= lh->num_file_names)
13363 {
13364 struct file_entry *fe = &lh->file_names[file - 1];
13365
13366 if (IS_ABSOLUTE_PATH (fe->name))
13367 return xstrdup (fe->name);
13368 else
13369 {
13370 const char *dir;
13371 int dir_len;
13372 char *full_name;
13373
13374 if (fe->dir_index)
13375 dir = lh->include_dirs[fe->dir_index - 1];
13376 else
13377 dir = comp_dir;
13378
13379 if (dir)
13380 {
13381 dir_len = strlen (dir);
13382 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13383 strcpy (full_name, dir);
13384 full_name[dir_len] = '/';
13385 strcpy (full_name + dir_len + 1, fe->name);
13386 return full_name;
13387 }
13388 else
13389 return xstrdup (fe->name);
13390 }
13391 }
13392 else
13393 {
13394 /* The compiler produced a bogus file number. We can at least
13395 record the macro definitions made in the file, even if we
13396 won't be able to find the file by name. */
13397 char fake_name[80];
13398
13399 sprintf (fake_name, "<bad macro file number %d>", file);
13400
13401 complaint (&symfile_complaints,
13402 _("bad file number in macro information (%d)"),
13403 file);
13404
13405 return xstrdup (fake_name);
13406 }
13407 }
13408
13409
13410 static struct macro_source_file *
13411 macro_start_file (int file, int line,
13412 struct macro_source_file *current_file,
13413 const char *comp_dir,
13414 struct line_header *lh, struct objfile *objfile)
13415 {
13416 /* The full name of this source file. */
13417 char *full_name = file_full_name (file, lh, comp_dir);
13418
13419 /* We don't create a macro table for this compilation unit
13420 at all until we actually get a filename. */
13421 if (! pending_macros)
13422 pending_macros = new_macro_table (&objfile->objfile_obstack,
13423 objfile->macro_cache);
13424
13425 if (! current_file)
13426 /* If we have no current file, then this must be the start_file
13427 directive for the compilation unit's main source file. */
13428 current_file = macro_set_main (pending_macros, full_name);
13429 else
13430 current_file = macro_include (current_file, line, full_name);
13431
13432 xfree (full_name);
13433
13434 return current_file;
13435 }
13436
13437
13438 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13439 followed by a null byte. */
13440 static char *
13441 copy_string (const char *buf, int len)
13442 {
13443 char *s = xmalloc (len + 1);
13444
13445 memcpy (s, buf, len);
13446 s[len] = '\0';
13447 return s;
13448 }
13449
13450
13451 static const char *
13452 consume_improper_spaces (const char *p, const char *body)
13453 {
13454 if (*p == ' ')
13455 {
13456 complaint (&symfile_complaints,
13457 _("macro definition contains spaces in formal argument list:\n`%s'"),
13458 body);
13459
13460 while (*p == ' ')
13461 p++;
13462 }
13463
13464 return p;
13465 }
13466
13467
13468 static void
13469 parse_macro_definition (struct macro_source_file *file, int line,
13470 const char *body)
13471 {
13472 const char *p;
13473
13474 /* The body string takes one of two forms. For object-like macro
13475 definitions, it should be:
13476
13477 <macro name> " " <definition>
13478
13479 For function-like macro definitions, it should be:
13480
13481 <macro name> "() " <definition>
13482 or
13483 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13484
13485 Spaces may appear only where explicitly indicated, and in the
13486 <definition>.
13487
13488 The Dwarf 2 spec says that an object-like macro's name is always
13489 followed by a space, but versions of GCC around March 2002 omit
13490 the space when the macro's definition is the empty string.
13491
13492 The Dwarf 2 spec says that there should be no spaces between the
13493 formal arguments in a function-like macro's formal argument list,
13494 but versions of GCC around March 2002 include spaces after the
13495 commas. */
13496
13497
13498 /* Find the extent of the macro name. The macro name is terminated
13499 by either a space or null character (for an object-like macro) or
13500 an opening paren (for a function-like macro). */
13501 for (p = body; *p; p++)
13502 if (*p == ' ' || *p == '(')
13503 break;
13504
13505 if (*p == ' ' || *p == '\0')
13506 {
13507 /* It's an object-like macro. */
13508 int name_len = p - body;
13509 char *name = copy_string (body, name_len);
13510 const char *replacement;
13511
13512 if (*p == ' ')
13513 replacement = body + name_len + 1;
13514 else
13515 {
13516 dwarf2_macro_malformed_definition_complaint (body);
13517 replacement = body + name_len;
13518 }
13519
13520 macro_define_object (file, line, name, replacement);
13521
13522 xfree (name);
13523 }
13524 else if (*p == '(')
13525 {
13526 /* It's a function-like macro. */
13527 char *name = copy_string (body, p - body);
13528 int argc = 0;
13529 int argv_size = 1;
13530 char **argv = xmalloc (argv_size * sizeof (*argv));
13531
13532 p++;
13533
13534 p = consume_improper_spaces (p, body);
13535
13536 /* Parse the formal argument list. */
13537 while (*p && *p != ')')
13538 {
13539 /* Find the extent of the current argument name. */
13540 const char *arg_start = p;
13541
13542 while (*p && *p != ',' && *p != ')' && *p != ' ')
13543 p++;
13544
13545 if (! *p || p == arg_start)
13546 dwarf2_macro_malformed_definition_complaint (body);
13547 else
13548 {
13549 /* Make sure argv has room for the new argument. */
13550 if (argc >= argv_size)
13551 {
13552 argv_size *= 2;
13553 argv = xrealloc (argv, argv_size * sizeof (*argv));
13554 }
13555
13556 argv[argc++] = copy_string (arg_start, p - arg_start);
13557 }
13558
13559 p = consume_improper_spaces (p, body);
13560
13561 /* Consume the comma, if present. */
13562 if (*p == ',')
13563 {
13564 p++;
13565
13566 p = consume_improper_spaces (p, body);
13567 }
13568 }
13569
13570 if (*p == ')')
13571 {
13572 p++;
13573
13574 if (*p == ' ')
13575 /* Perfectly formed definition, no complaints. */
13576 macro_define_function (file, line, name,
13577 argc, (const char **) argv,
13578 p + 1);
13579 else if (*p == '\0')
13580 {
13581 /* Complain, but do define it. */
13582 dwarf2_macro_malformed_definition_complaint (body);
13583 macro_define_function (file, line, name,
13584 argc, (const char **) argv,
13585 p);
13586 }
13587 else
13588 /* Just complain. */
13589 dwarf2_macro_malformed_definition_complaint (body);
13590 }
13591 else
13592 /* Just complain. */
13593 dwarf2_macro_malformed_definition_complaint (body);
13594
13595 xfree (name);
13596 {
13597 int i;
13598
13599 for (i = 0; i < argc; i++)
13600 xfree (argv[i]);
13601 }
13602 xfree (argv);
13603 }
13604 else
13605 dwarf2_macro_malformed_definition_complaint (body);
13606 }
13607
13608
13609 static void
13610 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13611 char *comp_dir, bfd *abfd,
13612 struct dwarf2_cu *cu)
13613 {
13614 gdb_byte *mac_ptr, *mac_end;
13615 struct macro_source_file *current_file = 0;
13616 enum dwarf_macinfo_record_type macinfo_type;
13617 int at_commandline;
13618
13619 dwarf2_read_section (dwarf2_per_objfile->objfile,
13620 &dwarf2_per_objfile->macinfo);
13621 if (dwarf2_per_objfile->macinfo.buffer == NULL)
13622 {
13623 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13624 return;
13625 }
13626
13627 /* First pass: Find the name of the base filename.
13628 This filename is needed in order to process all macros whose definition
13629 (or undefinition) comes from the command line. These macros are defined
13630 before the first DW_MACINFO_start_file entry, and yet still need to be
13631 associated to the base file.
13632
13633 To determine the base file name, we scan the macro definitions until we
13634 reach the first DW_MACINFO_start_file entry. We then initialize
13635 CURRENT_FILE accordingly so that any macro definition found before the
13636 first DW_MACINFO_start_file can still be associated to the base file. */
13637
13638 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13639 mac_end = dwarf2_per_objfile->macinfo.buffer
13640 + dwarf2_per_objfile->macinfo.size;
13641
13642 do
13643 {
13644 /* Do we at least have room for a macinfo type byte? */
13645 if (mac_ptr >= mac_end)
13646 {
13647 /* Complaint is printed during the second pass as GDB will probably
13648 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13649 break;
13650 }
13651
13652 macinfo_type = read_1_byte (abfd, mac_ptr);
13653 mac_ptr++;
13654
13655 switch (macinfo_type)
13656 {
13657 /* A zero macinfo type indicates the end of the macro
13658 information. */
13659 case 0:
13660 break;
13661
13662 case DW_MACINFO_define:
13663 case DW_MACINFO_undef:
13664 /* Only skip the data by MAC_PTR. */
13665 {
13666 unsigned int bytes_read;
13667
13668 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13669 mac_ptr += bytes_read;
13670 read_direct_string (abfd, mac_ptr, &bytes_read);
13671 mac_ptr += bytes_read;
13672 }
13673 break;
13674
13675 case DW_MACINFO_start_file:
13676 {
13677 unsigned int bytes_read;
13678 int line, file;
13679
13680 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13681 mac_ptr += bytes_read;
13682 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13683 mac_ptr += bytes_read;
13684
13685 current_file = macro_start_file (file, line, current_file, comp_dir,
13686 lh, cu->objfile);
13687 }
13688 break;
13689
13690 case DW_MACINFO_end_file:
13691 /* No data to skip by MAC_PTR. */
13692 break;
13693
13694 case DW_MACINFO_vendor_ext:
13695 /* Only skip the data by MAC_PTR. */
13696 {
13697 unsigned int bytes_read;
13698
13699 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13700 mac_ptr += bytes_read;
13701 read_direct_string (abfd, mac_ptr, &bytes_read);
13702 mac_ptr += bytes_read;
13703 }
13704 break;
13705
13706 default:
13707 break;
13708 }
13709 } while (macinfo_type != 0 && current_file == NULL);
13710
13711 /* Second pass: Process all entries.
13712
13713 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13714 command-line macro definitions/undefinitions. This flag is unset when we
13715 reach the first DW_MACINFO_start_file entry. */
13716
13717 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13718
13719 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13720 GDB is still reading the definitions from command line. First
13721 DW_MACINFO_start_file will need to be ignored as it was already executed
13722 to create CURRENT_FILE for the main source holding also the command line
13723 definitions. On first met DW_MACINFO_start_file this flag is reset to
13724 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13725
13726 at_commandline = 1;
13727
13728 do
13729 {
13730 /* Do we at least have room for a macinfo type byte? */
13731 if (mac_ptr >= mac_end)
13732 {
13733 dwarf2_macros_too_long_complaint ();
13734 break;
13735 }
13736
13737 macinfo_type = read_1_byte (abfd, mac_ptr);
13738 mac_ptr++;
13739
13740 switch (macinfo_type)
13741 {
13742 /* A zero macinfo type indicates the end of the macro
13743 information. */
13744 case 0:
13745 break;
13746
13747 case DW_MACINFO_define:
13748 case DW_MACINFO_undef:
13749 {
13750 unsigned int bytes_read;
13751 int line;
13752 char *body;
13753
13754 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13755 mac_ptr += bytes_read;
13756 body = read_direct_string (abfd, mac_ptr, &bytes_read);
13757 mac_ptr += bytes_read;
13758
13759 if (! current_file)
13760 {
13761 /* DWARF violation as no main source is present. */
13762 complaint (&symfile_complaints,
13763 _("debug info with no main source gives macro %s "
13764 "on line %d: %s"),
13765 macinfo_type == DW_MACINFO_define ?
13766 _("definition") :
13767 macinfo_type == DW_MACINFO_undef ?
13768 _("undefinition") :
13769 _("something-or-other"), line, body);
13770 break;
13771 }
13772 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13773 complaint (&symfile_complaints,
13774 _("debug info gives %s macro %s with %s line %d: %s"),
13775 at_commandline ? _("command-line") : _("in-file"),
13776 macinfo_type == DW_MACINFO_define ?
13777 _("definition") :
13778 macinfo_type == DW_MACINFO_undef ?
13779 _("undefinition") :
13780 _("something-or-other"),
13781 line == 0 ? _("zero") : _("non-zero"), line, body);
13782
13783 if (macinfo_type == DW_MACINFO_define)
13784 parse_macro_definition (current_file, line, body);
13785 else if (macinfo_type == DW_MACINFO_undef)
13786 macro_undef (current_file, line, body);
13787 }
13788 break;
13789
13790 case DW_MACINFO_start_file:
13791 {
13792 unsigned int bytes_read;
13793 int line, file;
13794
13795 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13796 mac_ptr += bytes_read;
13797 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13798 mac_ptr += bytes_read;
13799
13800 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
13801 complaint (&symfile_complaints,
13802 _("debug info gives source %d included "
13803 "from %s at %s line %d"),
13804 file, at_commandline ? _("command-line") : _("file"),
13805 line == 0 ? _("zero") : _("non-zero"), line);
13806
13807 if (at_commandline)
13808 {
13809 /* This DW_MACINFO_start_file was executed in the pass one. */
13810 at_commandline = 0;
13811 }
13812 else
13813 current_file = macro_start_file (file, line,
13814 current_file, comp_dir,
13815 lh, cu->objfile);
13816 }
13817 break;
13818
13819 case DW_MACINFO_end_file:
13820 if (! current_file)
13821 complaint (&symfile_complaints,
13822 _("macro debug info has an unmatched `close_file' directive"));
13823 else
13824 {
13825 current_file = current_file->included_by;
13826 if (! current_file)
13827 {
13828 enum dwarf_macinfo_record_type next_type;
13829
13830 /* GCC circa March 2002 doesn't produce the zero
13831 type byte marking the end of the compilation
13832 unit. Complain if it's not there, but exit no
13833 matter what. */
13834
13835 /* Do we at least have room for a macinfo type byte? */
13836 if (mac_ptr >= mac_end)
13837 {
13838 dwarf2_macros_too_long_complaint ();
13839 return;
13840 }
13841
13842 /* We don't increment mac_ptr here, so this is just
13843 a look-ahead. */
13844 next_type = read_1_byte (abfd, mac_ptr);
13845 if (next_type != 0)
13846 complaint (&symfile_complaints,
13847 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
13848
13849 return;
13850 }
13851 }
13852 break;
13853
13854 case DW_MACINFO_vendor_ext:
13855 {
13856 unsigned int bytes_read;
13857 int constant;
13858 char *string;
13859
13860 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13861 mac_ptr += bytes_read;
13862 string = read_direct_string (abfd, mac_ptr, &bytes_read);
13863 mac_ptr += bytes_read;
13864
13865 /* We don't recognize any vendor extensions. */
13866 }
13867 break;
13868 }
13869 } while (macinfo_type != 0);
13870 }
13871
13872 /* Check if the attribute's form is a DW_FORM_block*
13873 if so return true else false. */
13874 static int
13875 attr_form_is_block (struct attribute *attr)
13876 {
13877 return (attr == NULL ? 0 :
13878 attr->form == DW_FORM_block1
13879 || attr->form == DW_FORM_block2
13880 || attr->form == DW_FORM_block4
13881 || attr->form == DW_FORM_block
13882 || attr->form == DW_FORM_exprloc);
13883 }
13884
13885 /* Return non-zero if ATTR's value is a section offset --- classes
13886 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
13887 You may use DW_UNSND (attr) to retrieve such offsets.
13888
13889 Section 7.5.4, "Attribute Encodings", explains that no attribute
13890 may have a value that belongs to more than one of these classes; it
13891 would be ambiguous if we did, because we use the same forms for all
13892 of them. */
13893 static int
13894 attr_form_is_section_offset (struct attribute *attr)
13895 {
13896 return (attr->form == DW_FORM_data4
13897 || attr->form == DW_FORM_data8
13898 || attr->form == DW_FORM_sec_offset);
13899 }
13900
13901
13902 /* Return non-zero if ATTR's value falls in the 'constant' class, or
13903 zero otherwise. When this function returns true, you can apply
13904 dwarf2_get_attr_constant_value to it.
13905
13906 However, note that for some attributes you must check
13907 attr_form_is_section_offset before using this test. DW_FORM_data4
13908 and DW_FORM_data8 are members of both the constant class, and of
13909 the classes that contain offsets into other debug sections
13910 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
13911 that, if an attribute's can be either a constant or one of the
13912 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
13913 taken as section offsets, not constants. */
13914 static int
13915 attr_form_is_constant (struct attribute *attr)
13916 {
13917 switch (attr->form)
13918 {
13919 case DW_FORM_sdata:
13920 case DW_FORM_udata:
13921 case DW_FORM_data1:
13922 case DW_FORM_data2:
13923 case DW_FORM_data4:
13924 case DW_FORM_data8:
13925 return 1;
13926 default:
13927 return 0;
13928 }
13929 }
13930
13931 static void
13932 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
13933 struct dwarf2_cu *cu)
13934 {
13935 if (attr_form_is_section_offset (attr)
13936 /* ".debug_loc" may not exist at all, or the offset may be outside
13937 the section. If so, fall through to the complaint in the
13938 other branch. */
13939 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
13940 {
13941 struct dwarf2_loclist_baton *baton;
13942
13943 baton = obstack_alloc (&cu->objfile->objfile_obstack,
13944 sizeof (struct dwarf2_loclist_baton));
13945 baton->per_cu = cu->per_cu;
13946 gdb_assert (baton->per_cu);
13947
13948 dwarf2_read_section (dwarf2_per_objfile->objfile,
13949 &dwarf2_per_objfile->loc);
13950
13951 /* We don't know how long the location list is, but make sure we
13952 don't run off the edge of the section. */
13953 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
13954 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
13955 baton->base_address = cu->base_address;
13956 if (cu->base_known == 0)
13957 complaint (&symfile_complaints,
13958 _("Location list used without specifying the CU base address."));
13959
13960 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
13961 SYMBOL_LOCATION_BATON (sym) = baton;
13962 }
13963 else
13964 {
13965 struct dwarf2_locexpr_baton *baton;
13966
13967 baton = obstack_alloc (&cu->objfile->objfile_obstack,
13968 sizeof (struct dwarf2_locexpr_baton));
13969 baton->per_cu = cu->per_cu;
13970 gdb_assert (baton->per_cu);
13971
13972 if (attr_form_is_block (attr))
13973 {
13974 /* Note that we're just copying the block's data pointer
13975 here, not the actual data. We're still pointing into the
13976 info_buffer for SYM's objfile; right now we never release
13977 that buffer, but when we do clean up properly this may
13978 need to change. */
13979 baton->size = DW_BLOCK (attr)->size;
13980 baton->data = DW_BLOCK (attr)->data;
13981 }
13982 else
13983 {
13984 dwarf2_invalid_attrib_class_complaint ("location description",
13985 SYMBOL_NATURAL_NAME (sym));
13986 baton->size = 0;
13987 baton->data = NULL;
13988 }
13989
13990 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
13991 SYMBOL_LOCATION_BATON (sym) = baton;
13992 }
13993 }
13994
13995 /* Return the OBJFILE associated with the compilation unit CU. If CU
13996 came from a separate debuginfo file, then the master objfile is
13997 returned. */
13998
13999 struct objfile *
14000 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14001 {
14002 struct objfile *objfile = per_cu->objfile;
14003
14004 /* Return the master objfile, so that we can report and look up the
14005 correct file containing this variable. */
14006 if (objfile->separate_debug_objfile_backlink)
14007 objfile = objfile->separate_debug_objfile_backlink;
14008
14009 return objfile;
14010 }
14011
14012 /* Return the address size given in the compilation unit header for CU. */
14013
14014 CORE_ADDR
14015 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14016 {
14017 if (per_cu->cu)
14018 return per_cu->cu->header.addr_size;
14019 else
14020 {
14021 /* If the CU is not currently read in, we re-read its header. */
14022 struct objfile *objfile = per_cu->objfile;
14023 struct dwarf2_per_objfile *per_objfile
14024 = objfile_data (objfile, dwarf2_objfile_data_key);
14025 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14026 struct comp_unit_head cu_header;
14027
14028 memset (&cu_header, 0, sizeof cu_header);
14029 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14030 return cu_header.addr_size;
14031 }
14032 }
14033
14034 /* Return the offset size given in the compilation unit header for CU. */
14035
14036 int
14037 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14038 {
14039 if (per_cu->cu)
14040 return per_cu->cu->header.offset_size;
14041 else
14042 {
14043 /* If the CU is not currently read in, we re-read its header. */
14044 struct objfile *objfile = per_cu->objfile;
14045 struct dwarf2_per_objfile *per_objfile
14046 = objfile_data (objfile, dwarf2_objfile_data_key);
14047 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14048 struct comp_unit_head cu_header;
14049
14050 memset (&cu_header, 0, sizeof cu_header);
14051 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14052 return cu_header.offset_size;
14053 }
14054 }
14055
14056 /* Return the text offset of the CU. The returned offset comes from
14057 this CU's objfile. If this objfile came from a separate debuginfo
14058 file, then the offset may be different from the corresponding
14059 offset in the parent objfile. */
14060
14061 CORE_ADDR
14062 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14063 {
14064 struct objfile *objfile = per_cu->objfile;
14065
14066 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14067 }
14068
14069 /* Locate the .debug_info compilation unit from CU's objfile which contains
14070 the DIE at OFFSET. Raises an error on failure. */
14071
14072 static struct dwarf2_per_cu_data *
14073 dwarf2_find_containing_comp_unit (unsigned int offset,
14074 struct objfile *objfile)
14075 {
14076 struct dwarf2_per_cu_data *this_cu;
14077 int low, high;
14078
14079 low = 0;
14080 high = dwarf2_per_objfile->n_comp_units - 1;
14081 while (high > low)
14082 {
14083 int mid = low + (high - low) / 2;
14084
14085 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14086 high = mid;
14087 else
14088 low = mid + 1;
14089 }
14090 gdb_assert (low == high);
14091 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14092 {
14093 if (low == 0)
14094 error (_("Dwarf Error: could not find partial DIE containing "
14095 "offset 0x%lx [in module %s]"),
14096 (long) offset, bfd_get_filename (objfile->obfd));
14097
14098 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14099 return dwarf2_per_objfile->all_comp_units[low-1];
14100 }
14101 else
14102 {
14103 this_cu = dwarf2_per_objfile->all_comp_units[low];
14104 if (low == dwarf2_per_objfile->n_comp_units - 1
14105 && offset >= this_cu->offset + this_cu->length)
14106 error (_("invalid dwarf2 offset %u"), offset);
14107 gdb_assert (offset < this_cu->offset + this_cu->length);
14108 return this_cu;
14109 }
14110 }
14111
14112 /* Locate the compilation unit from OBJFILE which is located at exactly
14113 OFFSET. Raises an error on failure. */
14114
14115 static struct dwarf2_per_cu_data *
14116 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14117 {
14118 struct dwarf2_per_cu_data *this_cu;
14119
14120 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14121 if (this_cu->offset != offset)
14122 error (_("no compilation unit with offset %u."), offset);
14123 return this_cu;
14124 }
14125
14126 /* Malloc space for a dwarf2_cu for OBJFILE and initialize it. */
14127
14128 static struct dwarf2_cu *
14129 alloc_one_comp_unit (struct objfile *objfile)
14130 {
14131 struct dwarf2_cu *cu = xcalloc (1, sizeof (struct dwarf2_cu));
14132 cu->objfile = objfile;
14133 obstack_init (&cu->comp_unit_obstack);
14134 return cu;
14135 }
14136
14137 /* Release one cached compilation unit, CU. We unlink it from the tree
14138 of compilation units, but we don't remove it from the read_in_chain;
14139 the caller is responsible for that.
14140 NOTE: DATA is a void * because this function is also used as a
14141 cleanup routine. */
14142
14143 static void
14144 free_one_comp_unit (void *data)
14145 {
14146 struct dwarf2_cu *cu = data;
14147
14148 if (cu->per_cu != NULL)
14149 cu->per_cu->cu = NULL;
14150 cu->per_cu = NULL;
14151
14152 obstack_free (&cu->comp_unit_obstack, NULL);
14153
14154 xfree (cu);
14155 }
14156
14157 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14158 when we're finished with it. We can't free the pointer itself, but be
14159 sure to unlink it from the cache. Also release any associated storage
14160 and perform cache maintenance.
14161
14162 Only used during partial symbol parsing. */
14163
14164 static void
14165 free_stack_comp_unit (void *data)
14166 {
14167 struct dwarf2_cu *cu = data;
14168
14169 obstack_free (&cu->comp_unit_obstack, NULL);
14170 cu->partial_dies = NULL;
14171
14172 if (cu->per_cu != NULL)
14173 {
14174 /* This compilation unit is on the stack in our caller, so we
14175 should not xfree it. Just unlink it. */
14176 cu->per_cu->cu = NULL;
14177 cu->per_cu = NULL;
14178
14179 /* If we had a per-cu pointer, then we may have other compilation
14180 units loaded, so age them now. */
14181 age_cached_comp_units ();
14182 }
14183 }
14184
14185 /* Free all cached compilation units. */
14186
14187 static void
14188 free_cached_comp_units (void *data)
14189 {
14190 struct dwarf2_per_cu_data *per_cu, **last_chain;
14191
14192 per_cu = dwarf2_per_objfile->read_in_chain;
14193 last_chain = &dwarf2_per_objfile->read_in_chain;
14194 while (per_cu != NULL)
14195 {
14196 struct dwarf2_per_cu_data *next_cu;
14197
14198 next_cu = per_cu->cu->read_in_chain;
14199
14200 free_one_comp_unit (per_cu->cu);
14201 *last_chain = next_cu;
14202
14203 per_cu = next_cu;
14204 }
14205 }
14206
14207 /* Increase the age counter on each cached compilation unit, and free
14208 any that are too old. */
14209
14210 static void
14211 age_cached_comp_units (void)
14212 {
14213 struct dwarf2_per_cu_data *per_cu, **last_chain;
14214
14215 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14216 per_cu = dwarf2_per_objfile->read_in_chain;
14217 while (per_cu != NULL)
14218 {
14219 per_cu->cu->last_used ++;
14220 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14221 dwarf2_mark (per_cu->cu);
14222 per_cu = per_cu->cu->read_in_chain;
14223 }
14224
14225 per_cu = dwarf2_per_objfile->read_in_chain;
14226 last_chain = &dwarf2_per_objfile->read_in_chain;
14227 while (per_cu != NULL)
14228 {
14229 struct dwarf2_per_cu_data *next_cu;
14230
14231 next_cu = per_cu->cu->read_in_chain;
14232
14233 if (!per_cu->cu->mark)
14234 {
14235 free_one_comp_unit (per_cu->cu);
14236 *last_chain = next_cu;
14237 }
14238 else
14239 last_chain = &per_cu->cu->read_in_chain;
14240
14241 per_cu = next_cu;
14242 }
14243 }
14244
14245 /* Remove a single compilation unit from the cache. */
14246
14247 static void
14248 free_one_cached_comp_unit (void *target_cu)
14249 {
14250 struct dwarf2_per_cu_data *per_cu, **last_chain;
14251
14252 per_cu = dwarf2_per_objfile->read_in_chain;
14253 last_chain = &dwarf2_per_objfile->read_in_chain;
14254 while (per_cu != NULL)
14255 {
14256 struct dwarf2_per_cu_data *next_cu;
14257
14258 next_cu = per_cu->cu->read_in_chain;
14259
14260 if (per_cu->cu == target_cu)
14261 {
14262 free_one_comp_unit (per_cu->cu);
14263 *last_chain = next_cu;
14264 break;
14265 }
14266 else
14267 last_chain = &per_cu->cu->read_in_chain;
14268
14269 per_cu = next_cu;
14270 }
14271 }
14272
14273 /* Release all extra memory associated with OBJFILE. */
14274
14275 void
14276 dwarf2_free_objfile (struct objfile *objfile)
14277 {
14278 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14279
14280 if (dwarf2_per_objfile == NULL)
14281 return;
14282
14283 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14284 free_cached_comp_units (NULL);
14285
14286 if (dwarf2_per_objfile->using_index)
14287 {
14288 int i;
14289
14290 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14291 {
14292 int j;
14293 struct dwarf2_per_cu_data *per_cu =
14294 dwarf2_per_objfile->all_comp_units[i];
14295
14296 if (!per_cu->v.quick->lines)
14297 continue;
14298
14299 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
14300 {
14301 if (per_cu->v.quick->file_names)
14302 xfree ((void *) per_cu->v.quick->file_names[j]);
14303 if (per_cu->v.quick->full_names)
14304 xfree ((void *) per_cu->v.quick->full_names[j]);
14305 }
14306
14307 free_line_header (per_cu->v.quick->lines);
14308 }
14309 }
14310
14311 /* Everything else should be on the objfile obstack. */
14312 }
14313
14314 /* A pair of DIE offset and GDB type pointer. We store these
14315 in a hash table separate from the DIEs, and preserve them
14316 when the DIEs are flushed out of cache. */
14317
14318 struct dwarf2_offset_and_type
14319 {
14320 unsigned int offset;
14321 struct type *type;
14322 };
14323
14324 /* Hash function for a dwarf2_offset_and_type. */
14325
14326 static hashval_t
14327 offset_and_type_hash (const void *item)
14328 {
14329 const struct dwarf2_offset_and_type *ofs = item;
14330
14331 return ofs->offset;
14332 }
14333
14334 /* Equality function for a dwarf2_offset_and_type. */
14335
14336 static int
14337 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14338 {
14339 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14340 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14341
14342 return ofs_lhs->offset == ofs_rhs->offset;
14343 }
14344
14345 /* Set the type associated with DIE to TYPE. Save it in CU's hash
14346 table if necessary. For convenience, return TYPE.
14347
14348 The DIEs reading must have careful ordering to:
14349 * Not cause infite loops trying to read in DIEs as a prerequisite for
14350 reading current DIE.
14351 * Not trying to dereference contents of still incompletely read in types
14352 while reading in other DIEs.
14353 * Enable referencing still incompletely read in types just by a pointer to
14354 the type without accessing its fields.
14355
14356 Therefore caller should follow these rules:
14357 * Try to fetch any prerequisite types we may need to build this DIE type
14358 before building the type and calling set_die_type.
14359 * After building type call set_die_type for current DIE as soon as
14360 possible before fetching more types to complete the current type.
14361 * Make the type as complete as possible before fetching more types. */
14362
14363 static struct type *
14364 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14365 {
14366 struct dwarf2_offset_and_type **slot, ofs;
14367 struct objfile *objfile = cu->objfile;
14368 htab_t *type_hash_ptr;
14369
14370 /* For Ada types, make sure that the gnat-specific data is always
14371 initialized (if not already set). There are a few types where
14372 we should not be doing so, because the type-specific area is
14373 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14374 where the type-specific area is used to store the floatformat).
14375 But this is not a problem, because the gnat-specific information
14376 is actually not needed for these types. */
14377 if (need_gnat_info (cu)
14378 && TYPE_CODE (type) != TYPE_CODE_FUNC
14379 && TYPE_CODE (type) != TYPE_CODE_FLT
14380 && !HAVE_GNAT_AUX_INFO (type))
14381 INIT_GNAT_SPECIFIC (type);
14382
14383 if (cu->per_cu->from_debug_types)
14384 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14385 else
14386 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14387
14388 if (*type_hash_ptr == NULL)
14389 {
14390 *type_hash_ptr
14391 = htab_create_alloc_ex (127,
14392 offset_and_type_hash,
14393 offset_and_type_eq,
14394 NULL,
14395 &objfile->objfile_obstack,
14396 hashtab_obstack_allocate,
14397 dummy_obstack_deallocate);
14398 }
14399
14400 ofs.offset = die->offset;
14401 ofs.type = type;
14402 slot = (struct dwarf2_offset_and_type **)
14403 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14404 if (*slot)
14405 complaint (&symfile_complaints,
14406 _("A problem internal to GDB: DIE 0x%x has type already set"),
14407 die->offset);
14408 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14409 **slot = ofs;
14410 return type;
14411 }
14412
14413 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14414 table, or return NULL if the die does not have a saved type. */
14415
14416 static struct type *
14417 get_die_type_at_offset (unsigned int offset,
14418 struct dwarf2_per_cu_data *per_cu)
14419 {
14420 struct dwarf2_offset_and_type *slot, ofs;
14421 htab_t type_hash;
14422
14423 if (per_cu->from_debug_types)
14424 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14425 else
14426 type_hash = dwarf2_per_objfile->debug_info_type_hash;
14427 if (type_hash == NULL)
14428 return NULL;
14429
14430 ofs.offset = offset;
14431 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14432 if (slot)
14433 return slot->type;
14434 else
14435 return NULL;
14436 }
14437
14438 /* Look up the type for DIE in the appropriate type_hash table,
14439 or return NULL if DIE does not have a saved type. */
14440
14441 static struct type *
14442 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14443 {
14444 return get_die_type_at_offset (die->offset, cu->per_cu);
14445 }
14446
14447 /* Add a dependence relationship from CU to REF_PER_CU. */
14448
14449 static void
14450 dwarf2_add_dependence (struct dwarf2_cu *cu,
14451 struct dwarf2_per_cu_data *ref_per_cu)
14452 {
14453 void **slot;
14454
14455 if (cu->dependencies == NULL)
14456 cu->dependencies
14457 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14458 NULL, &cu->comp_unit_obstack,
14459 hashtab_obstack_allocate,
14460 dummy_obstack_deallocate);
14461
14462 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14463 if (*slot == NULL)
14464 *slot = ref_per_cu;
14465 }
14466
14467 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14468 Set the mark field in every compilation unit in the
14469 cache that we must keep because we are keeping CU. */
14470
14471 static int
14472 dwarf2_mark_helper (void **slot, void *data)
14473 {
14474 struct dwarf2_per_cu_data *per_cu;
14475
14476 per_cu = (struct dwarf2_per_cu_data *) *slot;
14477 if (per_cu->cu->mark)
14478 return 1;
14479 per_cu->cu->mark = 1;
14480
14481 if (per_cu->cu->dependencies != NULL)
14482 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14483
14484 return 1;
14485 }
14486
14487 /* Set the mark field in CU and in every other compilation unit in the
14488 cache that we must keep because we are keeping CU. */
14489
14490 static void
14491 dwarf2_mark (struct dwarf2_cu *cu)
14492 {
14493 if (cu->mark)
14494 return;
14495 cu->mark = 1;
14496 if (cu->dependencies != NULL)
14497 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14498 }
14499
14500 static void
14501 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14502 {
14503 while (per_cu)
14504 {
14505 per_cu->cu->mark = 0;
14506 per_cu = per_cu->cu->read_in_chain;
14507 }
14508 }
14509
14510 /* Trivial hash function for partial_die_info: the hash value of a DIE
14511 is its offset in .debug_info for this objfile. */
14512
14513 static hashval_t
14514 partial_die_hash (const void *item)
14515 {
14516 const struct partial_die_info *part_die = item;
14517
14518 return part_die->offset;
14519 }
14520
14521 /* Trivial comparison function for partial_die_info structures: two DIEs
14522 are equal if they have the same offset. */
14523
14524 static int
14525 partial_die_eq (const void *item_lhs, const void *item_rhs)
14526 {
14527 const struct partial_die_info *part_die_lhs = item_lhs;
14528 const struct partial_die_info *part_die_rhs = item_rhs;
14529
14530 return part_die_lhs->offset == part_die_rhs->offset;
14531 }
14532
14533 static struct cmd_list_element *set_dwarf2_cmdlist;
14534 static struct cmd_list_element *show_dwarf2_cmdlist;
14535
14536 static void
14537 set_dwarf2_cmd (char *args, int from_tty)
14538 {
14539 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14540 }
14541
14542 static void
14543 show_dwarf2_cmd (char *args, int from_tty)
14544 {
14545 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14546 }
14547
14548 /* If section described by INFO was mmapped, munmap it now. */
14549
14550 static void
14551 munmap_section_buffer (struct dwarf2_section_info *info)
14552 {
14553 if (info->was_mmapped)
14554 {
14555 #ifdef HAVE_MMAP
14556 intptr_t begin = (intptr_t) info->buffer;
14557 intptr_t map_begin = begin & ~(pagesize - 1);
14558 size_t map_length = info->size + begin - map_begin;
14559
14560 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14561 #else
14562 /* Without HAVE_MMAP, we should never be here to begin with. */
14563 gdb_assert_not_reached ("no mmap support");
14564 #endif
14565 }
14566 }
14567
14568 /* munmap debug sections for OBJFILE, if necessary. */
14569
14570 static void
14571 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14572 {
14573 struct dwarf2_per_objfile *data = d;
14574
14575 /* This is sorted according to the order they're defined in to make it easier
14576 to keep in sync. */
14577 munmap_section_buffer (&data->info);
14578 munmap_section_buffer (&data->abbrev);
14579 munmap_section_buffer (&data->line);
14580 munmap_section_buffer (&data->loc);
14581 munmap_section_buffer (&data->macinfo);
14582 munmap_section_buffer (&data->str);
14583 munmap_section_buffer (&data->ranges);
14584 munmap_section_buffer (&data->types);
14585 munmap_section_buffer (&data->frame);
14586 munmap_section_buffer (&data->eh_frame);
14587 munmap_section_buffer (&data->gdb_index);
14588 }
14589
14590 \f
14591
14592 /* The contents of the hash table we create when building the string
14593 table. */
14594 struct strtab_entry
14595 {
14596 offset_type offset;
14597 const char *str;
14598 };
14599
14600 /* Hash function for a strtab_entry. */
14601 static hashval_t
14602 hash_strtab_entry (const void *e)
14603 {
14604 const struct strtab_entry *entry = e;
14605 return mapped_index_string_hash (entry->str);
14606 }
14607
14608 /* Equality function for a strtab_entry. */
14609 static int
14610 eq_strtab_entry (const void *a, const void *b)
14611 {
14612 const struct strtab_entry *ea = a;
14613 const struct strtab_entry *eb = b;
14614 return !strcmp (ea->str, eb->str);
14615 }
14616
14617 /* Create a strtab_entry hash table. */
14618 static htab_t
14619 create_strtab (void)
14620 {
14621 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14622 xfree, xcalloc, xfree);
14623 }
14624
14625 /* Add a string to the constant pool. Return the string's offset in
14626 host order. */
14627 static offset_type
14628 add_string (htab_t table, struct obstack *cpool, const char *str)
14629 {
14630 void **slot;
14631 struct strtab_entry entry;
14632 struct strtab_entry *result;
14633
14634 entry.str = str;
14635 slot = htab_find_slot (table, &entry, INSERT);
14636 if (*slot)
14637 result = *slot;
14638 else
14639 {
14640 result = XNEW (struct strtab_entry);
14641 result->offset = obstack_object_size (cpool);
14642 result->str = str;
14643 obstack_grow_str0 (cpool, str);
14644 *slot = result;
14645 }
14646 return result->offset;
14647 }
14648
14649 /* An entry in the symbol table. */
14650 struct symtab_index_entry
14651 {
14652 /* The name of the symbol. */
14653 const char *name;
14654 /* The offset of the name in the constant pool. */
14655 offset_type index_offset;
14656 /* A sorted vector of the indices of all the CUs that hold an object
14657 of this name. */
14658 VEC (offset_type) *cu_indices;
14659 };
14660
14661 /* The symbol table. This is a power-of-2-sized hash table. */
14662 struct mapped_symtab
14663 {
14664 offset_type n_elements;
14665 offset_type size;
14666 struct symtab_index_entry **data;
14667 };
14668
14669 /* Hash function for a symtab_index_entry. */
14670 static hashval_t
14671 hash_symtab_entry (const void *e)
14672 {
14673 const struct symtab_index_entry *entry = e;
14674 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14675 sizeof (offset_type) * VEC_length (offset_type,
14676 entry->cu_indices),
14677 0);
14678 }
14679
14680 /* Equality function for a symtab_index_entry. */
14681 static int
14682 eq_symtab_entry (const void *a, const void *b)
14683 {
14684 const struct symtab_index_entry *ea = a;
14685 const struct symtab_index_entry *eb = b;
14686 int len = VEC_length (offset_type, ea->cu_indices);
14687 if (len != VEC_length (offset_type, eb->cu_indices))
14688 return 0;
14689 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14690 VEC_address (offset_type, eb->cu_indices),
14691 sizeof (offset_type) * len);
14692 }
14693
14694 /* Destroy a symtab_index_entry. */
14695 static void
14696 delete_symtab_entry (void *p)
14697 {
14698 struct symtab_index_entry *entry = p;
14699 VEC_free (offset_type, entry->cu_indices);
14700 xfree (entry);
14701 }
14702
14703 /* Create a hash table holding symtab_index_entry objects. */
14704 static htab_t
14705 create_index_table (void)
14706 {
14707 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14708 delete_symtab_entry, xcalloc, xfree);
14709 }
14710
14711 /* Create a new mapped symtab object. */
14712 static struct mapped_symtab *
14713 create_mapped_symtab (void)
14714 {
14715 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14716 symtab->n_elements = 0;
14717 symtab->size = 1024;
14718 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14719 return symtab;
14720 }
14721
14722 /* Destroy a mapped_symtab. */
14723 static void
14724 cleanup_mapped_symtab (void *p)
14725 {
14726 struct mapped_symtab *symtab = p;
14727 /* The contents of the array are freed when the other hash table is
14728 destroyed. */
14729 xfree (symtab->data);
14730 xfree (symtab);
14731 }
14732
14733 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14734 the slot. */
14735 static struct symtab_index_entry **
14736 find_slot (struct mapped_symtab *symtab, const char *name)
14737 {
14738 offset_type index, step, hash = mapped_index_string_hash (name);
14739
14740 index = hash & (symtab->size - 1);
14741 step = ((hash * 17) & (symtab->size - 1)) | 1;
14742
14743 for (;;)
14744 {
14745 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
14746 return &symtab->data[index];
14747 index = (index + step) & (symtab->size - 1);
14748 }
14749 }
14750
14751 /* Expand SYMTAB's hash table. */
14752 static void
14753 hash_expand (struct mapped_symtab *symtab)
14754 {
14755 offset_type old_size = symtab->size;
14756 offset_type i;
14757 struct symtab_index_entry **old_entries = symtab->data;
14758
14759 symtab->size *= 2;
14760 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14761
14762 for (i = 0; i < old_size; ++i)
14763 {
14764 if (old_entries[i])
14765 {
14766 struct symtab_index_entry **slot = find_slot (symtab,
14767 old_entries[i]->name);
14768 *slot = old_entries[i];
14769 }
14770 }
14771
14772 xfree (old_entries);
14773 }
14774
14775 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
14776 is the index of the CU in which the symbol appears. */
14777 static void
14778 add_index_entry (struct mapped_symtab *symtab, const char *name,
14779 offset_type cu_index)
14780 {
14781 struct symtab_index_entry **slot;
14782
14783 ++symtab->n_elements;
14784 if (4 * symtab->n_elements / 3 >= symtab->size)
14785 hash_expand (symtab);
14786
14787 slot = find_slot (symtab, name);
14788 if (!*slot)
14789 {
14790 *slot = XNEW (struct symtab_index_entry);
14791 (*slot)->name = name;
14792 (*slot)->cu_indices = NULL;
14793 }
14794 /* Don't push an index twice. Due to how we add entries we only
14795 have to check the last one. */
14796 if (VEC_empty (offset_type, (*slot)->cu_indices)
14797 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
14798 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
14799 }
14800
14801 /* Add a vector of indices to the constant pool. */
14802 static offset_type
14803 add_indices_to_cpool (htab_t index_table, struct obstack *cpool,
14804 struct symtab_index_entry *entry)
14805 {
14806 void **slot;
14807
14808 slot = htab_find_slot (index_table, entry, INSERT);
14809 if (!*slot)
14810 {
14811 offset_type len = VEC_length (offset_type, entry->cu_indices);
14812 offset_type val = MAYBE_SWAP (len);
14813 offset_type iter;
14814 int i;
14815
14816 *slot = entry;
14817 entry->index_offset = obstack_object_size (cpool);
14818
14819 obstack_grow (cpool, &val, sizeof (val));
14820 for (i = 0;
14821 VEC_iterate (offset_type, entry->cu_indices, i, iter);
14822 ++i)
14823 {
14824 val = MAYBE_SWAP (iter);
14825 obstack_grow (cpool, &val, sizeof (val));
14826 }
14827 }
14828 else
14829 {
14830 struct symtab_index_entry *old_entry = *slot;
14831 entry->index_offset = old_entry->index_offset;
14832 entry = old_entry;
14833 }
14834 return entry->index_offset;
14835 }
14836
14837 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
14838 constant pool entries going into the obstack CPOOL. */
14839 static void
14840 write_hash_table (struct mapped_symtab *symtab,
14841 struct obstack *output, struct obstack *cpool)
14842 {
14843 offset_type i;
14844 htab_t index_table;
14845 htab_t str_table;
14846
14847 index_table = create_index_table ();
14848 str_table = create_strtab ();
14849 /* We add all the index vectors to the constant pool first, to
14850 ensure alignment is ok. */
14851 for (i = 0; i < symtab->size; ++i)
14852 {
14853 if (symtab->data[i])
14854 add_indices_to_cpool (index_table, cpool, symtab->data[i]);
14855 }
14856
14857 /* Now write out the hash table. */
14858 for (i = 0; i < symtab->size; ++i)
14859 {
14860 offset_type str_off, vec_off;
14861
14862 if (symtab->data[i])
14863 {
14864 str_off = add_string (str_table, cpool, symtab->data[i]->name);
14865 vec_off = symtab->data[i]->index_offset;
14866 }
14867 else
14868 {
14869 /* While 0 is a valid constant pool index, it is not valid
14870 to have 0 for both offsets. */
14871 str_off = 0;
14872 vec_off = 0;
14873 }
14874
14875 str_off = MAYBE_SWAP (str_off);
14876 vec_off = MAYBE_SWAP (vec_off);
14877
14878 obstack_grow (output, &str_off, sizeof (str_off));
14879 obstack_grow (output, &vec_off, sizeof (vec_off));
14880 }
14881
14882 htab_delete (str_table);
14883 htab_delete (index_table);
14884 }
14885
14886 /* Write an address entry to ADDR_OBSTACK. The addresses are taken
14887 from PST; CU_INDEX is the index of the CU in the vector of all
14888 CUs. */
14889 static void
14890 add_address_entry (struct objfile *objfile,
14891 struct obstack *addr_obstack, struct partial_symtab *pst,
14892 unsigned int cu_index)
14893 {
14894 offset_type offset;
14895 char addr[8];
14896 CORE_ADDR baseaddr;
14897
14898 /* Don't bother recording empty ranges. */
14899 if (pst->textlow == pst->texthigh)
14900 return;
14901
14902 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14903
14904 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->textlow - baseaddr);
14905 obstack_grow (addr_obstack, addr, 8);
14906 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, pst->texthigh - baseaddr);
14907 obstack_grow (addr_obstack, addr, 8);
14908 offset = MAYBE_SWAP (cu_index);
14909 obstack_grow (addr_obstack, &offset, sizeof (offset_type));
14910 }
14911
14912 /* Add a list of partial symbols to SYMTAB. */
14913 static void
14914 write_psymbols (struct mapped_symtab *symtab,
14915 htab_t psyms_seen,
14916 struct partial_symbol **psymp,
14917 int count,
14918 offset_type cu_index,
14919 int is_static)
14920 {
14921 for (; count-- > 0; ++psymp)
14922 {
14923 void **slot, *lookup;
14924
14925 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
14926 error (_("Ada is not currently supported by the index"));
14927
14928 /* We only want to add a given psymbol once. However, we also
14929 want to account for whether it is global or static. So, we
14930 may add it twice, using slightly different values. */
14931 if (is_static)
14932 {
14933 uintptr_t val = 1 | (uintptr_t) *psymp;
14934
14935 lookup = (void *) val;
14936 }
14937 else
14938 lookup = *psymp;
14939
14940 /* Only add a given psymbol once. */
14941 slot = htab_find_slot (psyms_seen, lookup, INSERT);
14942 if (!*slot)
14943 {
14944 *slot = lookup;
14945 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
14946 }
14947 }
14948 }
14949
14950 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
14951 exception if there is an error. */
14952 static void
14953 write_obstack (FILE *file, struct obstack *obstack)
14954 {
14955 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
14956 file)
14957 != obstack_object_size (obstack))
14958 error (_("couldn't data write to file"));
14959 }
14960
14961 /* Unlink a file if the argument is not NULL. */
14962 static void
14963 unlink_if_set (void *p)
14964 {
14965 char **filename = p;
14966 if (*filename)
14967 unlink (*filename);
14968 }
14969
14970 /* A helper struct used when iterating over debug_types. */
14971 struct signatured_type_index_data
14972 {
14973 struct objfile *objfile;
14974 struct mapped_symtab *symtab;
14975 struct obstack *types_list;
14976 htab_t psyms_seen;
14977 int cu_index;
14978 };
14979
14980 /* A helper function that writes a single signatured_type to an
14981 obstack. */
14982 static int
14983 write_one_signatured_type (void **slot, void *d)
14984 {
14985 struct signatured_type_index_data *info = d;
14986 struct signatured_type *entry = (struct signatured_type *) *slot;
14987 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
14988 struct partial_symtab *psymtab = per_cu->v.psymtab;
14989 gdb_byte val[8];
14990
14991 write_psymbols (info->symtab,
14992 info->psyms_seen,
14993 info->objfile->global_psymbols.list + psymtab->globals_offset,
14994 psymtab->n_global_syms, info->cu_index,
14995 0);
14996 write_psymbols (info->symtab,
14997 info->psyms_seen,
14998 info->objfile->static_psymbols.list + psymtab->statics_offset,
14999 psymtab->n_static_syms, info->cu_index,
15000 1);
15001
15002 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15003 obstack_grow (info->types_list, val, 8);
15004 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15005 obstack_grow (info->types_list, val, 8);
15006 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15007 obstack_grow (info->types_list, val, 8);
15008
15009 ++info->cu_index;
15010
15011 return 1;
15012 }
15013
15014 /* A cleanup function for an htab_t. */
15015
15016 static void
15017 cleanup_htab (void *arg)
15018 {
15019 htab_delete (arg);
15020 }
15021
15022 /* Create an index file for OBJFILE in the directory DIR. */
15023 static void
15024 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15025 {
15026 struct cleanup *cleanup;
15027 char *filename, *cleanup_filename;
15028 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15029 struct obstack cu_list, types_cu_list;
15030 int i;
15031 FILE *out_file;
15032 struct mapped_symtab *symtab;
15033 offset_type val, size_of_contents, total_len;
15034 struct stat st;
15035 char buf[8];
15036 htab_t psyms_seen;
15037
15038 if (!objfile->psymtabs)
15039 return;
15040 if (dwarf2_per_objfile->using_index)
15041 error (_("Cannot use an index to create the index"));
15042
15043 if (stat (objfile->name, &st) < 0)
15044 perror_with_name (_("Could not stat"));
15045
15046 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15047 INDEX_SUFFIX, (char *) NULL);
15048 cleanup = make_cleanup (xfree, filename);
15049
15050 out_file = fopen (filename, "wb");
15051 if (!out_file)
15052 error (_("Can't open `%s' for writing"), filename);
15053
15054 cleanup_filename = filename;
15055 make_cleanup (unlink_if_set, &cleanup_filename);
15056
15057 symtab = create_mapped_symtab ();
15058 make_cleanup (cleanup_mapped_symtab, symtab);
15059
15060 obstack_init (&addr_obstack);
15061 make_cleanup_obstack_free (&addr_obstack);
15062
15063 obstack_init (&cu_list);
15064 make_cleanup_obstack_free (&cu_list);
15065
15066 obstack_init (&types_cu_list);
15067 make_cleanup_obstack_free (&types_cu_list);
15068
15069 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15070 NULL, xcalloc, xfree);
15071 make_cleanup (cleanup_htab, psyms_seen);
15072
15073 /* The list is already sorted, so we don't need to do additional
15074 work here. Also, the debug_types entries do not appear in
15075 all_comp_units, but only in their own hash table. */
15076 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15077 {
15078 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15079 struct partial_symtab *psymtab = per_cu->v.psymtab;
15080 gdb_byte val[8];
15081
15082 write_psymbols (symtab,
15083 psyms_seen,
15084 objfile->global_psymbols.list + psymtab->globals_offset,
15085 psymtab->n_global_syms, i,
15086 0);
15087 write_psymbols (symtab,
15088 psyms_seen,
15089 objfile->static_psymbols.list + psymtab->statics_offset,
15090 psymtab->n_static_syms, i,
15091 1);
15092
15093 add_address_entry (objfile, &addr_obstack, psymtab, i);
15094
15095 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15096 obstack_grow (&cu_list, val, 8);
15097 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15098 obstack_grow (&cu_list, val, 8);
15099 }
15100
15101 /* Write out the .debug_type entries, if any. */
15102 if (dwarf2_per_objfile->signatured_types)
15103 {
15104 struct signatured_type_index_data sig_data;
15105
15106 sig_data.objfile = objfile;
15107 sig_data.symtab = symtab;
15108 sig_data.types_list = &types_cu_list;
15109 sig_data.psyms_seen = psyms_seen;
15110 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15111 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15112 write_one_signatured_type, &sig_data);
15113 }
15114
15115 obstack_init (&constant_pool);
15116 make_cleanup_obstack_free (&constant_pool);
15117 obstack_init (&symtab_obstack);
15118 make_cleanup_obstack_free (&symtab_obstack);
15119 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15120
15121 obstack_init (&contents);
15122 make_cleanup_obstack_free (&contents);
15123 size_of_contents = 6 * sizeof (offset_type);
15124 total_len = size_of_contents;
15125
15126 /* The version number. */
15127 val = MAYBE_SWAP (3);
15128 obstack_grow (&contents, &val, sizeof (val));
15129
15130 /* The offset of the CU list from the start of the file. */
15131 val = MAYBE_SWAP (total_len);
15132 obstack_grow (&contents, &val, sizeof (val));
15133 total_len += obstack_object_size (&cu_list);
15134
15135 /* The offset of the types CU list from the start of the file. */
15136 val = MAYBE_SWAP (total_len);
15137 obstack_grow (&contents, &val, sizeof (val));
15138 total_len += obstack_object_size (&types_cu_list);
15139
15140 /* The offset of the address table from the start of the file. */
15141 val = MAYBE_SWAP (total_len);
15142 obstack_grow (&contents, &val, sizeof (val));
15143 total_len += obstack_object_size (&addr_obstack);
15144
15145 /* The offset of the symbol table from the start of the file. */
15146 val = MAYBE_SWAP (total_len);
15147 obstack_grow (&contents, &val, sizeof (val));
15148 total_len += obstack_object_size (&symtab_obstack);
15149
15150 /* The offset of the constant pool from the start of the file. */
15151 val = MAYBE_SWAP (total_len);
15152 obstack_grow (&contents, &val, sizeof (val));
15153 total_len += obstack_object_size (&constant_pool);
15154
15155 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15156
15157 write_obstack (out_file, &contents);
15158 write_obstack (out_file, &cu_list);
15159 write_obstack (out_file, &types_cu_list);
15160 write_obstack (out_file, &addr_obstack);
15161 write_obstack (out_file, &symtab_obstack);
15162 write_obstack (out_file, &constant_pool);
15163
15164 fclose (out_file);
15165
15166 /* We want to keep the file, so we set cleanup_filename to NULL
15167 here. See unlink_if_set. */
15168 cleanup_filename = NULL;
15169
15170 do_cleanups (cleanup);
15171 }
15172
15173 /* The mapped index file format is designed to be directly mmap()able
15174 on any architecture. In most cases, a datum is represented using a
15175 little-endian 32-bit integer value, called an offset_type. Big
15176 endian machines must byte-swap the values before using them.
15177 Exceptions to this rule are noted. The data is laid out such that
15178 alignment is always respected.
15179
15180 A mapped index consists of several sections.
15181
15182 1. The file header. This is a sequence of values, of offset_type
15183 unless otherwise noted:
15184
15185 [0] The version number, currently 3. Versions 1 and 2 are
15186 obsolete.
15187 [1] The offset, from the start of the file, of the CU list.
15188 [2] The offset, from the start of the file, of the types CU list.
15189 Note that this section can be empty, in which case this offset will
15190 be equal to the next offset.
15191 [3] The offset, from the start of the file, of the address section.
15192 [4] The offset, from the start of the file, of the symbol table.
15193 [5] The offset, from the start of the file, of the constant pool.
15194
15195 2. The CU list. This is a sequence of pairs of 64-bit
15196 little-endian values, sorted by the CU offset. The first element
15197 in each pair is the offset of a CU in the .debug_info section. The
15198 second element in each pair is the length of that CU. References
15199 to a CU elsewhere in the map are done using a CU index, which is
15200 just the 0-based index into this table. Note that if there are
15201 type CUs, then conceptually CUs and type CUs form a single list for
15202 the purposes of CU indices.
15203
15204 3. The types CU list. This is a sequence of triplets of 64-bit
15205 little-endian values. In a triplet, the first value is the CU
15206 offset, the second value is the type offset in the CU, and the
15207 third value is the type signature. The types CU list is not
15208 sorted.
15209
15210 4. The address section. The address section consists of a sequence
15211 of address entries. Each address entry has three elements.
15212 [0] The low address. This is a 64-bit little-endian value.
15213 [1] The high address. This is a 64-bit little-endian value.
15214 [2] The CU index. This is an offset_type value.
15215
15216 5. The symbol table. This is a hash table. The size of the hash
15217 table is always a power of 2. The initial hash and the step are
15218 currently defined by the `find_slot' function.
15219
15220 Each slot in the hash table consists of a pair of offset_type
15221 values. The first value is the offset of the symbol's name in the
15222 constant pool. The second value is the offset of the CU vector in
15223 the constant pool.
15224
15225 If both values are 0, then this slot in the hash table is empty.
15226 This is ok because while 0 is a valid constant pool index, it
15227 cannot be a valid index for both a string and a CU vector.
15228
15229 A string in the constant pool is stored as a \0-terminated string,
15230 as you'd expect.
15231
15232 A CU vector in the constant pool is a sequence of offset_type
15233 values. The first value is the number of CU indices in the vector.
15234 Each subsequent value is the index of a CU in the CU list. This
15235 element in the hash table is used to indicate which CUs define the
15236 symbol.
15237
15238 6. The constant pool. This is simply a bunch of bytes. It is
15239 organized so that alignment is correct: CU vectors are stored
15240 first, followed by strings. */
15241 static void
15242 save_gdb_index_command (char *arg, int from_tty)
15243 {
15244 struct objfile *objfile;
15245
15246 if (!arg || !*arg)
15247 error (_("usage: save gdb-index DIRECTORY"));
15248
15249 ALL_OBJFILES (objfile)
15250 {
15251 struct stat st;
15252
15253 /* If the objfile does not correspond to an actual file, skip it. */
15254 if (stat (objfile->name, &st) < 0)
15255 continue;
15256
15257 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15258 if (dwarf2_per_objfile)
15259 {
15260 volatile struct gdb_exception except;
15261
15262 TRY_CATCH (except, RETURN_MASK_ERROR)
15263 {
15264 write_psymtabs_to_index (objfile, arg);
15265 }
15266 if (except.reason < 0)
15267 exception_fprintf (gdb_stderr, except,
15268 _("Error while writing index for `%s': "),
15269 objfile->name);
15270 }
15271 }
15272 }
15273
15274 \f
15275
15276 int dwarf2_always_disassemble;
15277
15278 static void
15279 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15280 struct cmd_list_element *c, const char *value)
15281 {
15282 fprintf_filtered (file, _("\
15283 Whether to always disassemble DWARF expressions is %s.\n"),
15284 value);
15285 }
15286
15287 void _initialize_dwarf2_read (void);
15288
15289 void
15290 _initialize_dwarf2_read (void)
15291 {
15292 struct cmd_list_element *c;
15293
15294 dwarf2_objfile_data_key
15295 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15296
15297 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15298 Set DWARF 2 specific variables.\n\
15299 Configure DWARF 2 variables such as the cache size"),
15300 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15301 0/*allow-unknown*/, &maintenance_set_cmdlist);
15302
15303 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15304 Show DWARF 2 specific variables\n\
15305 Show DWARF 2 variables such as the cache size"),
15306 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15307 0/*allow-unknown*/, &maintenance_show_cmdlist);
15308
15309 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15310 &dwarf2_max_cache_age, _("\
15311 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15312 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15313 A higher limit means that cached compilation units will be stored\n\
15314 in memory longer, and more total memory will be used. Zero disables\n\
15315 caching, which can slow down startup."),
15316 NULL,
15317 show_dwarf2_max_cache_age,
15318 &set_dwarf2_cmdlist,
15319 &show_dwarf2_cmdlist);
15320
15321 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15322 &dwarf2_always_disassemble, _("\
15323 Set whether `info address' always disassembles DWARF expressions."), _("\
15324 Show whether `info address' always disassembles DWARF expressions."), _("\
15325 When enabled, DWARF expressions are always printed in an assembly-like\n\
15326 syntax. When disabled, expressions will be printed in a more\n\
15327 conversational style, when possible."),
15328 NULL,
15329 show_dwarf2_always_disassemble,
15330 &set_dwarf2_cmdlist,
15331 &show_dwarf2_cmdlist);
15332
15333 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15334 Set debugging of the dwarf2 DIE reader."), _("\
15335 Show debugging of the dwarf2 DIE reader."), _("\
15336 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15337 The value is the maximum depth to print."),
15338 NULL,
15339 NULL,
15340 &setdebuglist, &showdebuglist);
15341
15342 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15343 _("Save a .gdb-index file"),
15344 &save_cmdlist);
15345 set_cmd_completer (c, filename_completer);
15346 }
This page took 0.374854 seconds and 4 git commands to generate.