* dwarf2read.c: Whitespace cleanup, plus comment additions.
[deliverable/binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003,
4 2004, 2005, 2006, 2007, 2008, 2009, 2010
5 Free Software Foundation, Inc.
6
7 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
8 Inc. with support from Florida State University (under contract
9 with the Ada Joint Program Office), and Silicon Graphics, Inc.
10 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
11 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
12 support.
13
14 This file is part of GDB.
15
16 This program is free software; you can redistribute it and/or modify
17 it under the terms of the GNU General Public License as published by
18 the Free Software Foundation; either version 3 of the License, or
19 (at your option) any later version.
20
21 This program is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
25
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>. */
28
29 #include "defs.h"
30 #include "bfd.h"
31 #include "symtab.h"
32 #include "gdbtypes.h"
33 #include "objfiles.h"
34 #include "dwarf2.h"
35 #include "buildsym.h"
36 #include "demangle.h"
37 #include "expression.h"
38 #include "filenames.h" /* for DOSish file names */
39 #include "macrotab.h"
40 #include "language.h"
41 #include "complaints.h"
42 #include "bcache.h"
43 #include "dwarf2expr.h"
44 #include "dwarf2loc.h"
45 #include "cp-support.h"
46 #include "hashtab.h"
47 #include "command.h"
48 #include "gdbcmd.h"
49 #include "block.h"
50 #include "addrmap.h"
51 #include "typeprint.h"
52 #include "jv-lang.h"
53 #include "psympriv.h"
54 #include "exceptions.h"
55 #include "gdb_stat.h"
56 #include "completer.h"
57 #include "vec.h"
58 #include "c-lang.h"
59 #include "valprint.h"
60
61 #include <fcntl.h>
62 #include "gdb_string.h"
63 #include "gdb_assert.h"
64 #include <sys/types.h>
65 #ifdef HAVE_ZLIB_H
66 #include <zlib.h>
67 #endif
68 #ifdef HAVE_MMAP
69 #include <sys/mman.h>
70 #ifndef MAP_FAILED
71 #define MAP_FAILED ((void *) -1)
72 #endif
73 #endif
74
75 typedef struct symbol *symbolp;
76 DEF_VEC_P (symbolp);
77
78 #if 0
79 /* .debug_info header for a compilation unit
80 Because of alignment constraints, this structure has padding and cannot
81 be mapped directly onto the beginning of the .debug_info section. */
82 typedef struct comp_unit_header
83 {
84 unsigned int length; /* length of the .debug_info
85 contribution */
86 unsigned short version; /* version number -- 2 for DWARF
87 version 2 */
88 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
89 unsigned char addr_size; /* byte size of an address -- 4 */
90 }
91 _COMP_UNIT_HEADER;
92 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
93 #endif
94
95 /* .debug_line statement program prologue
96 Because of alignment constraints, this structure has padding and cannot
97 be mapped directly onto the beginning of the .debug_info section. */
98 typedef struct statement_prologue
99 {
100 unsigned int total_length; /* byte length of the statement
101 information */
102 unsigned short version; /* version number -- 2 for DWARF
103 version 2 */
104 unsigned int prologue_length; /* # bytes between prologue &
105 stmt program */
106 unsigned char minimum_instruction_length; /* byte size of
107 smallest instr */
108 unsigned char default_is_stmt; /* initial value of is_stmt
109 register */
110 char line_base;
111 unsigned char line_range;
112 unsigned char opcode_base; /* number assigned to first special
113 opcode */
114 unsigned char *standard_opcode_lengths;
115 }
116 _STATEMENT_PROLOGUE;
117
118 /* When non-zero, dump DIEs after they are read in. */
119 static int dwarf2_die_debug = 0;
120
121 static int pagesize;
122
123 /* When set, the file that we're processing is known to have debugging
124 info for C++ namespaces. GCC 3.3.x did not produce this information,
125 but later versions do. */
126
127 static int processing_has_namespace_info;
128
129 static const struct objfile_data *dwarf2_objfile_data_key;
130
131 struct dwarf2_section_info
132 {
133 asection *asection;
134 gdb_byte *buffer;
135 bfd_size_type size;
136 int was_mmapped;
137 /* True if we have tried to read this section. */
138 int readin;
139 };
140
141 /* All offsets in the index are of this type. It must be
142 architecture-independent. */
143 typedef uint32_t offset_type;
144
145 DEF_VEC_I (offset_type);
146
147 /* A description of the mapped index. The file format is described in
148 a comment by the code that writes the index. */
149 struct mapped_index
150 {
151 /* The total length of the buffer. */
152 off_t total_size;
153 /* A pointer to the address table data. */
154 const gdb_byte *address_table;
155 /* Size of the address table data in bytes. */
156 offset_type address_table_size;
157 /* The symbol table, implemented as a hash table. */
158 const offset_type *symbol_table;
159 /* Size in slots, each slot is 2 offset_types. */
160 offset_type symbol_table_slots;
161 /* A pointer to the constant pool. */
162 const char *constant_pool;
163 };
164
165 struct dwarf2_per_objfile
166 {
167 struct dwarf2_section_info info;
168 struct dwarf2_section_info abbrev;
169 struct dwarf2_section_info line;
170 struct dwarf2_section_info loc;
171 struct dwarf2_section_info macinfo;
172 struct dwarf2_section_info str;
173 struct dwarf2_section_info ranges;
174 struct dwarf2_section_info types;
175 struct dwarf2_section_info frame;
176 struct dwarf2_section_info eh_frame;
177 struct dwarf2_section_info gdb_index;
178
179 /* Back link. */
180 struct objfile *objfile;
181
182 /* A list of all the compilation units. This is used to locate
183 the target compilation unit of a particular reference. */
184 struct dwarf2_per_cu_data **all_comp_units;
185
186 /* The number of compilation units in ALL_COMP_UNITS. */
187 int n_comp_units;
188
189 /* The number of .debug_types-related CUs. */
190 int n_type_comp_units;
191
192 /* The .debug_types-related CUs. */
193 struct dwarf2_per_cu_data **type_comp_units;
194
195 /* A chain of compilation units that are currently read in, so that
196 they can be freed later. */
197 struct dwarf2_per_cu_data *read_in_chain;
198
199 /* A table mapping .debug_types signatures to its signatured_type entry.
200 This is NULL if the .debug_types section hasn't been read in yet. */
201 htab_t signatured_types;
202
203 /* A flag indicating wether this objfile has a section loaded at a
204 VMA of 0. */
205 int has_section_at_zero;
206
207 /* True if we are using the mapped index,
208 or we are faking it for OBJF_READNOW's sake. */
209 unsigned char using_index;
210
211 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
212 struct mapped_index *index_table;
213
214 /* Set during partial symbol reading, to prevent queueing of full
215 symbols. */
216 int reading_partial_symbols;
217
218 /* Table mapping type .debug_info DIE offsets to types.
219 This is NULL if not allocated yet.
220 It (currently) makes sense to allocate debug_types_type_hash lazily.
221 To keep things simple we allocate both lazily. */
222 htab_t debug_info_type_hash;
223
224 /* Table mapping type .debug_types DIE offsets to types.
225 This is NULL if not allocated yet. */
226 htab_t debug_types_type_hash;
227 };
228
229 static struct dwarf2_per_objfile *dwarf2_per_objfile;
230
231 /* names of the debugging sections */
232
233 /* Note that if the debugging section has been compressed, it might
234 have a name like .zdebug_info. */
235
236 #define INFO_SECTION "debug_info"
237 #define ABBREV_SECTION "debug_abbrev"
238 #define LINE_SECTION "debug_line"
239 #define LOC_SECTION "debug_loc"
240 #define MACINFO_SECTION "debug_macinfo"
241 #define STR_SECTION "debug_str"
242 #define RANGES_SECTION "debug_ranges"
243 #define TYPES_SECTION "debug_types"
244 #define FRAME_SECTION "debug_frame"
245 #define EH_FRAME_SECTION "eh_frame"
246 #define GDB_INDEX_SECTION "gdb_index"
247
248 /* local data types */
249
250 /* We hold several abbreviation tables in memory at the same time. */
251 #ifndef ABBREV_HASH_SIZE
252 #define ABBREV_HASH_SIZE 121
253 #endif
254
255 /* The data in a compilation unit header, after target2host
256 translation, looks like this. */
257 struct comp_unit_head
258 {
259 unsigned int length;
260 short version;
261 unsigned char addr_size;
262 unsigned char signed_addr_p;
263 unsigned int abbrev_offset;
264
265 /* Size of file offsets; either 4 or 8. */
266 unsigned int offset_size;
267
268 /* Size of the length field; either 4 or 12. */
269 unsigned int initial_length_size;
270
271 /* Offset to the first byte of this compilation unit header in the
272 .debug_info section, for resolving relative reference dies. */
273 unsigned int offset;
274
275 /* Offset to first die in this cu from the start of the cu.
276 This will be the first byte following the compilation unit header. */
277 unsigned int first_die_offset;
278 };
279
280 /* Type used for delaying computation of method physnames.
281 See comments for compute_delayed_physnames. */
282 struct delayed_method_info
283 {
284 /* The type to which the method is attached, i.e., its parent class. */
285 struct type *type;
286
287 /* The index of the method in the type's function fieldlists. */
288 int fnfield_index;
289
290 /* The index of the method in the fieldlist. */
291 int index;
292
293 /* The name of the DIE. */
294 const char *name;
295
296 /* The DIE associated with this method. */
297 struct die_info *die;
298 };
299
300 typedef struct delayed_method_info delayed_method_info;
301 DEF_VEC_O (delayed_method_info);
302
303 /* Internal state when decoding a particular compilation unit. */
304 struct dwarf2_cu
305 {
306 /* The objfile containing this compilation unit. */
307 struct objfile *objfile;
308
309 /* The header of the compilation unit. */
310 struct comp_unit_head header;
311
312 /* Base address of this compilation unit. */
313 CORE_ADDR base_address;
314
315 /* Non-zero if base_address has been set. */
316 int base_known;
317
318 struct function_range *first_fn, *last_fn, *cached_fn;
319
320 /* The language we are debugging. */
321 enum language language;
322 const struct language_defn *language_defn;
323
324 const char *producer;
325
326 /* The generic symbol table building routines have separate lists for
327 file scope symbols and all all other scopes (local scopes). So
328 we need to select the right one to pass to add_symbol_to_list().
329 We do it by keeping a pointer to the correct list in list_in_scope.
330
331 FIXME: The original dwarf code just treated the file scope as the
332 first local scope, and all other local scopes as nested local
333 scopes, and worked fine. Check to see if we really need to
334 distinguish these in buildsym.c. */
335 struct pending **list_in_scope;
336
337 /* DWARF abbreviation table associated with this compilation unit. */
338 struct abbrev_info **dwarf2_abbrevs;
339
340 /* Storage for the abbrev table. */
341 struct obstack abbrev_obstack;
342
343 /* Hash table holding all the loaded partial DIEs. */
344 htab_t partial_dies;
345
346 /* Storage for things with the same lifetime as this read-in compilation
347 unit, including partial DIEs. */
348 struct obstack comp_unit_obstack;
349
350 /* When multiple dwarf2_cu structures are living in memory, this field
351 chains them all together, so that they can be released efficiently.
352 We will probably also want a generation counter so that most-recently-used
353 compilation units are cached... */
354 struct dwarf2_per_cu_data *read_in_chain;
355
356 /* Backchain to our per_cu entry if the tree has been built. */
357 struct dwarf2_per_cu_data *per_cu;
358
359 /* How many compilation units ago was this CU last referenced? */
360 int last_used;
361
362 /* A hash table of die offsets for following references. */
363 htab_t die_hash;
364
365 /* Full DIEs if read in. */
366 struct die_info *dies;
367
368 /* A set of pointers to dwarf2_per_cu_data objects for compilation
369 units referenced by this one. Only set during full symbol processing;
370 partial symbol tables do not have dependencies. */
371 htab_t dependencies;
372
373 /* Header data from the line table, during full symbol processing. */
374 struct line_header *line_header;
375
376 /* A list of methods which need to have physnames computed
377 after all type information has been read. */
378 VEC (delayed_method_info) *method_list;
379
380 /* Mark used when releasing cached dies. */
381 unsigned int mark : 1;
382
383 /* This flag will be set if this compilation unit might include
384 inter-compilation-unit references. */
385 unsigned int has_form_ref_addr : 1;
386
387 /* This flag will be set if this compilation unit includes any
388 DW_TAG_namespace DIEs. If we know that there are explicit
389 DIEs for namespaces, we don't need to try to infer them
390 from mangled names. */
391 unsigned int has_namespace_info : 1;
392 };
393
394 /* When using the index (and thus not using psymtabs), each CU has an
395 object of this type. This is used to hold information needed by
396 the various "quick" methods. */
397 struct dwarf2_per_cu_quick_data
398 {
399 /* The line table. This can be NULL if there was no line table. */
400 struct line_header *lines;
401
402 /* The file names from the line table. */
403 const char **file_names;
404 /* The file names from the line table after being run through
405 gdb_realpath. */
406 const char **full_names;
407
408 /* The corresponding symbol table. This is NULL if symbols for this
409 CU have not yet been read. */
410 struct symtab *symtab;
411
412 /* A temporary mark bit used when iterating over all CUs in
413 expand_symtabs_matching. */
414 unsigned int mark : 1;
415
416 /* True if we've tried to read the line table. */
417 unsigned int read_lines : 1;
418 };
419
420 /* Persistent data held for a compilation unit, even when not
421 processing it. We put a pointer to this structure in the
422 read_symtab_private field of the psymtab. If we encounter
423 inter-compilation-unit references, we also maintain a sorted
424 list of all compilation units. */
425
426 struct dwarf2_per_cu_data
427 {
428 /* The start offset and length of this compilation unit. 2**29-1
429 bytes should suffice to store the length of any compilation unit
430 - if it doesn't, GDB will fall over anyway.
431 NOTE: Unlike comp_unit_head.length, this length includes
432 initial_length_size. */
433 unsigned int offset;
434 unsigned int length : 29;
435
436 /* Flag indicating this compilation unit will be read in before
437 any of the current compilation units are processed. */
438 unsigned int queued : 1;
439
440 /* This flag will be set if we need to load absolutely all DIEs
441 for this compilation unit, instead of just the ones we think
442 are interesting. It gets set if we look for a DIE in the
443 hash table and don't find it. */
444 unsigned int load_all_dies : 1;
445
446 /* Non-zero if this CU is from .debug_types.
447 Otherwise it's from .debug_info. */
448 unsigned int from_debug_types : 1;
449
450 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
451 of the CU cache it gets reset to NULL again. */
452 struct dwarf2_cu *cu;
453
454 /* The corresponding objfile. */
455 struct objfile *objfile;
456
457 /* When using partial symbol tables, the 'psymtab' field is active.
458 Otherwise the 'quick' field is active. */
459 union
460 {
461 /* The partial symbol table associated with this compilation unit,
462 or NULL for partial units (which do not have an associated
463 symtab). */
464 struct partial_symtab *psymtab;
465
466 /* Data needed by the "quick" functions. */
467 struct dwarf2_per_cu_quick_data *quick;
468 } v;
469 };
470
471 /* Entry in the signatured_types hash table. */
472
473 struct signatured_type
474 {
475 ULONGEST signature;
476
477 /* Offset in .debug_types of the TU (type_unit) for this type. */
478 unsigned int offset;
479
480 /* Offset in .debug_types of the type defined by this TU. */
481 unsigned int type_offset;
482
483 /* The CU(/TU) of this type. */
484 struct dwarf2_per_cu_data per_cu;
485 };
486
487 /* Struct used to pass misc. parameters to read_die_and_children, et. al.
488 which are used for both .debug_info and .debug_types dies.
489 All parameters here are unchanging for the life of the call.
490 This struct exists to abstract away the constant parameters of
491 die reading. */
492
493 struct die_reader_specs
494 {
495 /* The bfd of this objfile. */
496 bfd* abfd;
497
498 /* The CU of the DIE we are parsing. */
499 struct dwarf2_cu *cu;
500
501 /* Pointer to start of section buffer.
502 This is either the start of .debug_info or .debug_types. */
503 const gdb_byte *buffer;
504 };
505
506 /* The line number information for a compilation unit (found in the
507 .debug_line section) begins with a "statement program header",
508 which contains the following information. */
509 struct line_header
510 {
511 unsigned int total_length;
512 unsigned short version;
513 unsigned int header_length;
514 unsigned char minimum_instruction_length;
515 unsigned char maximum_ops_per_instruction;
516 unsigned char default_is_stmt;
517 int line_base;
518 unsigned char line_range;
519 unsigned char opcode_base;
520
521 /* standard_opcode_lengths[i] is the number of operands for the
522 standard opcode whose value is i. This means that
523 standard_opcode_lengths[0] is unused, and the last meaningful
524 element is standard_opcode_lengths[opcode_base - 1]. */
525 unsigned char *standard_opcode_lengths;
526
527 /* The include_directories table. NOTE! These strings are not
528 allocated with xmalloc; instead, they are pointers into
529 debug_line_buffer. If you try to free them, `free' will get
530 indigestion. */
531 unsigned int num_include_dirs, include_dirs_size;
532 char **include_dirs;
533
534 /* The file_names table. NOTE! These strings are not allocated
535 with xmalloc; instead, they are pointers into debug_line_buffer.
536 Don't try to free them directly. */
537 unsigned int num_file_names, file_names_size;
538 struct file_entry
539 {
540 char *name;
541 unsigned int dir_index;
542 unsigned int mod_time;
543 unsigned int length;
544 int included_p; /* Non-zero if referenced by the Line Number Program. */
545 struct symtab *symtab; /* The associated symbol table, if any. */
546 } *file_names;
547
548 /* The start and end of the statement program following this
549 header. These point into dwarf2_per_objfile->line_buffer. */
550 gdb_byte *statement_program_start, *statement_program_end;
551 };
552
553 /* When we construct a partial symbol table entry we only
554 need this much information. */
555 struct partial_die_info
556 {
557 /* Offset of this DIE. */
558 unsigned int offset;
559
560 /* DWARF-2 tag for this DIE. */
561 ENUM_BITFIELD(dwarf_tag) tag : 16;
562
563 /* Assorted flags describing the data found in this DIE. */
564 unsigned int has_children : 1;
565 unsigned int is_external : 1;
566 unsigned int is_declaration : 1;
567 unsigned int has_type : 1;
568 unsigned int has_specification : 1;
569 unsigned int has_pc_info : 1;
570
571 /* Flag set if the SCOPE field of this structure has been
572 computed. */
573 unsigned int scope_set : 1;
574
575 /* Flag set if the DIE has a byte_size attribute. */
576 unsigned int has_byte_size : 1;
577
578 /* Flag set if any of the DIE's children are template arguments. */
579 unsigned int has_template_arguments : 1;
580
581 /* Flag set if fixup_partial_die has been called on this die. */
582 unsigned int fixup_called : 1;
583
584 /* The name of this DIE. Normally the value of DW_AT_name, but
585 sometimes a default name for unnamed DIEs. */
586 char *name;
587
588 /* The linkage name, if present. */
589 const char *linkage_name;
590
591 /* The scope to prepend to our children. This is generally
592 allocated on the comp_unit_obstack, so will disappear
593 when this compilation unit leaves the cache. */
594 char *scope;
595
596 /* The location description associated with this DIE, if any. */
597 struct dwarf_block *locdesc;
598
599 /* If HAS_PC_INFO, the PC range associated with this DIE. */
600 CORE_ADDR lowpc;
601 CORE_ADDR highpc;
602
603 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
604 DW_AT_sibling, if any. */
605 /* NOTE: This member isn't strictly necessary, read_partial_die could
606 return DW_AT_sibling values to its caller load_partial_dies. */
607 gdb_byte *sibling;
608
609 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
610 DW_AT_specification (or DW_AT_abstract_origin or
611 DW_AT_extension). */
612 unsigned int spec_offset;
613
614 /* Pointers to this DIE's parent, first child, and next sibling,
615 if any. */
616 struct partial_die_info *die_parent, *die_child, *die_sibling;
617 };
618
619 /* This data structure holds the information of an abbrev. */
620 struct abbrev_info
621 {
622 unsigned int number; /* number identifying abbrev */
623 enum dwarf_tag tag; /* dwarf tag */
624 unsigned short has_children; /* boolean */
625 unsigned short num_attrs; /* number of attributes */
626 struct attr_abbrev *attrs; /* an array of attribute descriptions */
627 struct abbrev_info *next; /* next in chain */
628 };
629
630 struct attr_abbrev
631 {
632 ENUM_BITFIELD(dwarf_attribute) name : 16;
633 ENUM_BITFIELD(dwarf_form) form : 16;
634 };
635
636 /* Attributes have a name and a value */
637 struct attribute
638 {
639 ENUM_BITFIELD(dwarf_attribute) name : 16;
640 ENUM_BITFIELD(dwarf_form) form : 15;
641
642 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
643 field should be in u.str (existing only for DW_STRING) but it is kept
644 here for better struct attribute alignment. */
645 unsigned int string_is_canonical : 1;
646
647 union
648 {
649 char *str;
650 struct dwarf_block *blk;
651 ULONGEST unsnd;
652 LONGEST snd;
653 CORE_ADDR addr;
654 struct signatured_type *signatured_type;
655 }
656 u;
657 };
658
659 /* This data structure holds a complete die structure. */
660 struct die_info
661 {
662 /* DWARF-2 tag for this DIE. */
663 ENUM_BITFIELD(dwarf_tag) tag : 16;
664
665 /* Number of attributes */
666 unsigned char num_attrs;
667
668 /* True if we're presently building the full type name for the
669 type derived from this DIE. */
670 unsigned char building_fullname : 1;
671
672 /* Abbrev number */
673 unsigned int abbrev;
674
675 /* Offset in .debug_info or .debug_types section. */
676 unsigned int offset;
677
678 /* The dies in a compilation unit form an n-ary tree. PARENT
679 points to this die's parent; CHILD points to the first child of
680 this node; and all the children of a given node are chained
681 together via their SIBLING fields. */
682 struct die_info *child; /* Its first child, if any. */
683 struct die_info *sibling; /* Its next sibling, if any. */
684 struct die_info *parent; /* Its parent, if any. */
685
686 /* An array of attributes, with NUM_ATTRS elements. There may be
687 zero, but it's not common and zero-sized arrays are not
688 sufficiently portable C. */
689 struct attribute attrs[1];
690 };
691
692 struct function_range
693 {
694 const char *name;
695 CORE_ADDR lowpc, highpc;
696 int seen_line;
697 struct function_range *next;
698 };
699
700 /* Get at parts of an attribute structure */
701
702 #define DW_STRING(attr) ((attr)->u.str)
703 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
704 #define DW_UNSND(attr) ((attr)->u.unsnd)
705 #define DW_BLOCK(attr) ((attr)->u.blk)
706 #define DW_SND(attr) ((attr)->u.snd)
707 #define DW_ADDR(attr) ((attr)->u.addr)
708 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
709
710 /* Blocks are a bunch of untyped bytes. */
711 struct dwarf_block
712 {
713 unsigned int size;
714 gdb_byte *data;
715 };
716
717 #ifndef ATTR_ALLOC_CHUNK
718 #define ATTR_ALLOC_CHUNK 4
719 #endif
720
721 /* Allocate fields for structs, unions and enums in this size. */
722 #ifndef DW_FIELD_ALLOC_CHUNK
723 #define DW_FIELD_ALLOC_CHUNK 4
724 #endif
725
726 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
727 but this would require a corresponding change in unpack_field_as_long
728 and friends. */
729 static int bits_per_byte = 8;
730
731 /* The routines that read and process dies for a C struct or C++ class
732 pass lists of data member fields and lists of member function fields
733 in an instance of a field_info structure, as defined below. */
734 struct field_info
735 {
736 /* List of data member and baseclasses fields. */
737 struct nextfield
738 {
739 struct nextfield *next;
740 int accessibility;
741 int virtuality;
742 struct field field;
743 }
744 *fields, *baseclasses;
745
746 /* Number of fields (including baseclasses). */
747 int nfields;
748
749 /* Number of baseclasses. */
750 int nbaseclasses;
751
752 /* Set if the accesibility of one of the fields is not public. */
753 int non_public_fields;
754
755 /* Member function fields array, entries are allocated in the order they
756 are encountered in the object file. */
757 struct nextfnfield
758 {
759 struct nextfnfield *next;
760 struct fn_field fnfield;
761 }
762 *fnfields;
763
764 /* Member function fieldlist array, contains name of possibly overloaded
765 member function, number of overloaded member functions and a pointer
766 to the head of the member function field chain. */
767 struct fnfieldlist
768 {
769 char *name;
770 int length;
771 struct nextfnfield *head;
772 }
773 *fnfieldlists;
774
775 /* Number of entries in the fnfieldlists array. */
776 int nfnfields;
777
778 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
779 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
780 struct typedef_field_list
781 {
782 struct typedef_field field;
783 struct typedef_field_list *next;
784 }
785 *typedef_field_list;
786 unsigned typedef_field_list_count;
787 };
788
789 /* One item on the queue of compilation units to read in full symbols
790 for. */
791 struct dwarf2_queue_item
792 {
793 struct dwarf2_per_cu_data *per_cu;
794 struct dwarf2_queue_item *next;
795 };
796
797 /* The current queue. */
798 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
799
800 /* Loaded secondary compilation units are kept in memory until they
801 have not been referenced for the processing of this many
802 compilation units. Set this to zero to disable caching. Cache
803 sizes of up to at least twenty will improve startup time for
804 typical inter-CU-reference binaries, at an obvious memory cost. */
805 static int dwarf2_max_cache_age = 5;
806 static void
807 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
808 struct cmd_list_element *c, const char *value)
809 {
810 fprintf_filtered (file, _("\
811 The upper bound on the age of cached dwarf2 compilation units is %s.\n"),
812 value);
813 }
814
815
816 /* Various complaints about symbol reading that don't abort the process */
817
818 static void
819 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
820 {
821 complaint (&symfile_complaints,
822 _("statement list doesn't fit in .debug_line section"));
823 }
824
825 static void
826 dwarf2_debug_line_missing_file_complaint (void)
827 {
828 complaint (&symfile_complaints,
829 _(".debug_line section has line data without a file"));
830 }
831
832 static void
833 dwarf2_debug_line_missing_end_sequence_complaint (void)
834 {
835 complaint (&symfile_complaints,
836 _(".debug_line section has line program sequence without an end"));
837 }
838
839 static void
840 dwarf2_complex_location_expr_complaint (void)
841 {
842 complaint (&symfile_complaints, _("location expression too complex"));
843 }
844
845 static void
846 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
847 int arg3)
848 {
849 complaint (&symfile_complaints,
850 _("const value length mismatch for '%s', got %d, expected %d"), arg1,
851 arg2, arg3);
852 }
853
854 static void
855 dwarf2_macros_too_long_complaint (void)
856 {
857 complaint (&symfile_complaints,
858 _("macro info runs off end of `.debug_macinfo' section"));
859 }
860
861 static void
862 dwarf2_macro_malformed_definition_complaint (const char *arg1)
863 {
864 complaint (&symfile_complaints,
865 _("macro debug info contains a malformed macro definition:\n`%s'"),
866 arg1);
867 }
868
869 static void
870 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
871 {
872 complaint (&symfile_complaints,
873 _("invalid attribute class or form for '%s' in '%s'"), arg1, arg2);
874 }
875
876 /* local function prototypes */
877
878 static void dwarf2_locate_sections (bfd *, asection *, void *);
879
880 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
881 struct objfile *);
882
883 static void dwarf2_build_psymtabs_hard (struct objfile *);
884
885 static void scan_partial_symbols (struct partial_die_info *,
886 CORE_ADDR *, CORE_ADDR *,
887 int, struct dwarf2_cu *);
888
889 static void add_partial_symbol (struct partial_die_info *,
890 struct dwarf2_cu *);
891
892 static void add_partial_namespace (struct partial_die_info *pdi,
893 CORE_ADDR *lowpc, CORE_ADDR *highpc,
894 int need_pc, struct dwarf2_cu *cu);
895
896 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
897 CORE_ADDR *highpc, int need_pc,
898 struct dwarf2_cu *cu);
899
900 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
901 struct dwarf2_cu *cu);
902
903 static void add_partial_subprogram (struct partial_die_info *pdi,
904 CORE_ADDR *lowpc, CORE_ADDR *highpc,
905 int need_pc, struct dwarf2_cu *cu);
906
907 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
908 gdb_byte *buffer, gdb_byte *info_ptr,
909 bfd *abfd, struct dwarf2_cu *cu);
910
911 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
912
913 static void psymtab_to_symtab_1 (struct partial_symtab *);
914
915 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
916
917 static void dwarf2_free_abbrev_table (void *);
918
919 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
920 struct dwarf2_cu *);
921
922 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
923 struct dwarf2_cu *);
924
925 static struct partial_die_info *load_partial_dies (bfd *,
926 gdb_byte *, gdb_byte *,
927 int, struct dwarf2_cu *);
928
929 static gdb_byte *read_partial_die (struct partial_die_info *,
930 struct abbrev_info *abbrev,
931 unsigned int, bfd *,
932 gdb_byte *, gdb_byte *,
933 struct dwarf2_cu *);
934
935 static struct partial_die_info *find_partial_die (unsigned int,
936 struct dwarf2_cu *);
937
938 static void fixup_partial_die (struct partial_die_info *,
939 struct dwarf2_cu *);
940
941 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
942 bfd *, gdb_byte *, struct dwarf2_cu *);
943
944 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
945 bfd *, gdb_byte *, struct dwarf2_cu *);
946
947 static unsigned int read_1_byte (bfd *, gdb_byte *);
948
949 static int read_1_signed_byte (bfd *, gdb_byte *);
950
951 static unsigned int read_2_bytes (bfd *, gdb_byte *);
952
953 static unsigned int read_4_bytes (bfd *, gdb_byte *);
954
955 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
956
957 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
958 unsigned int *);
959
960 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
961
962 static LONGEST read_checked_initial_length_and_offset
963 (bfd *, gdb_byte *, const struct comp_unit_head *,
964 unsigned int *, unsigned int *);
965
966 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
967 unsigned int *);
968
969 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
970
971 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
972
973 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
974
975 static char *read_indirect_string (bfd *, gdb_byte *,
976 const struct comp_unit_head *,
977 unsigned int *);
978
979 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
980
981 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
982
983 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
984
985 static void set_cu_language (unsigned int, struct dwarf2_cu *);
986
987 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
988 struct dwarf2_cu *);
989
990 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
991 unsigned int,
992 struct dwarf2_cu *);
993
994 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
995 struct dwarf2_cu *cu);
996
997 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
998
999 static struct die_info *die_specification (struct die_info *die,
1000 struct dwarf2_cu **);
1001
1002 static void free_line_header (struct line_header *lh);
1003
1004 static void add_file_name (struct line_header *, char *, unsigned int,
1005 unsigned int, unsigned int);
1006
1007 static struct line_header *(dwarf_decode_line_header
1008 (unsigned int offset,
1009 bfd *abfd, struct dwarf2_cu *cu));
1010
1011 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1012 struct dwarf2_cu *, struct partial_symtab *);
1013
1014 static void dwarf2_start_subfile (char *, const char *, const char *);
1015
1016 static struct symbol *new_symbol (struct die_info *, struct type *,
1017 struct dwarf2_cu *);
1018
1019 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1020 struct dwarf2_cu *, struct symbol *);
1021
1022 static void dwarf2_const_value (struct attribute *, struct symbol *,
1023 struct dwarf2_cu *);
1024
1025 static void dwarf2_const_value_attr (struct attribute *attr,
1026 struct type *type,
1027 const char *name,
1028 struct obstack *obstack,
1029 struct dwarf2_cu *cu, long *value,
1030 gdb_byte **bytes,
1031 struct dwarf2_locexpr_baton **baton);
1032
1033 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1034
1035 static int need_gnat_info (struct dwarf2_cu *);
1036
1037 static struct type *die_descriptive_type (struct die_info *, struct dwarf2_cu *);
1038
1039 static void set_descriptive_type (struct type *, struct die_info *,
1040 struct dwarf2_cu *);
1041
1042 static struct type *die_containing_type (struct die_info *,
1043 struct dwarf2_cu *);
1044
1045 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1046 struct dwarf2_cu *);
1047
1048 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1049
1050 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1051
1052 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1053
1054 static char *typename_concat (struct obstack *obs, const char *prefix,
1055 const char *suffix, int physname,
1056 struct dwarf2_cu *cu);
1057
1058 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1059
1060 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1061
1062 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1063
1064 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1065
1066 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1067 struct dwarf2_cu *, struct partial_symtab *);
1068
1069 static int dwarf2_get_pc_bounds (struct die_info *,
1070 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1071 struct partial_symtab *);
1072
1073 static void get_scope_pc_bounds (struct die_info *,
1074 CORE_ADDR *, CORE_ADDR *,
1075 struct dwarf2_cu *);
1076
1077 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1078 CORE_ADDR, struct dwarf2_cu *);
1079
1080 static void dwarf2_add_field (struct field_info *, struct die_info *,
1081 struct dwarf2_cu *);
1082
1083 static void dwarf2_attach_fields_to_type (struct field_info *,
1084 struct type *, struct dwarf2_cu *);
1085
1086 static void dwarf2_add_member_fn (struct field_info *,
1087 struct die_info *, struct type *,
1088 struct dwarf2_cu *);
1089
1090 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1091 struct type *, struct dwarf2_cu *);
1092
1093 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1094
1095 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1096
1097 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1098
1099 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1100
1101 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1102
1103 static struct type *read_module_type (struct die_info *die,
1104 struct dwarf2_cu *cu);
1105
1106 static const char *namespace_name (struct die_info *die,
1107 int *is_anonymous, struct dwarf2_cu *);
1108
1109 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1110
1111 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1112
1113 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1114 struct dwarf2_cu *);
1115
1116 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1117
1118 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1119 gdb_byte *info_ptr,
1120 gdb_byte **new_info_ptr,
1121 struct die_info *parent);
1122
1123 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1124 gdb_byte *info_ptr,
1125 gdb_byte **new_info_ptr,
1126 struct die_info *parent);
1127
1128 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1129 gdb_byte *info_ptr,
1130 gdb_byte **new_info_ptr,
1131 struct die_info *parent);
1132
1133 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1134 struct die_info **, gdb_byte *,
1135 int *);
1136
1137 static void process_die (struct die_info *, struct dwarf2_cu *);
1138
1139 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1140 struct obstack *);
1141
1142 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1143
1144 static const char *dwarf2_full_name (char *name,
1145 struct die_info *die,
1146 struct dwarf2_cu *cu);
1147
1148 static struct die_info *dwarf2_extension (struct die_info *die,
1149 struct dwarf2_cu **);
1150
1151 static char *dwarf_tag_name (unsigned int);
1152
1153 static char *dwarf_attr_name (unsigned int);
1154
1155 static char *dwarf_form_name (unsigned int);
1156
1157 static char *dwarf_bool_name (unsigned int);
1158
1159 static char *dwarf_type_encoding_name (unsigned int);
1160
1161 #if 0
1162 static char *dwarf_cfi_name (unsigned int);
1163 #endif
1164
1165 static struct die_info *sibling_die (struct die_info *);
1166
1167 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1168
1169 static void dump_die_for_error (struct die_info *);
1170
1171 static void dump_die_1 (struct ui_file *, int level, int max_level,
1172 struct die_info *);
1173
1174 /*static*/ void dump_die (struct die_info *, int max_level);
1175
1176 static void store_in_ref_table (struct die_info *,
1177 struct dwarf2_cu *);
1178
1179 static int is_ref_attr (struct attribute *);
1180
1181 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1182
1183 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1184
1185 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1186 struct attribute *,
1187 struct dwarf2_cu **);
1188
1189 static struct die_info *follow_die_ref (struct die_info *,
1190 struct attribute *,
1191 struct dwarf2_cu **);
1192
1193 static struct die_info *follow_die_sig (struct die_info *,
1194 struct attribute *,
1195 struct dwarf2_cu **);
1196
1197 static void read_signatured_type_at_offset (struct objfile *objfile,
1198 unsigned int offset);
1199
1200 static void read_signatured_type (struct objfile *,
1201 struct signatured_type *type_sig);
1202
1203 /* memory allocation interface */
1204
1205 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1206
1207 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1208
1209 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1210
1211 static void initialize_cu_func_list (struct dwarf2_cu *);
1212
1213 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1214 struct dwarf2_cu *);
1215
1216 static void dwarf_decode_macros (struct line_header *, unsigned int,
1217 char *, bfd *, struct dwarf2_cu *);
1218
1219 static int attr_form_is_block (struct attribute *);
1220
1221 static int attr_form_is_section_offset (struct attribute *);
1222
1223 static int attr_form_is_constant (struct attribute *);
1224
1225 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1226 struct dwarf2_loclist_baton *baton,
1227 struct attribute *attr);
1228
1229 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1230 struct symbol *sym,
1231 struct dwarf2_cu *cu);
1232
1233 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1234 struct abbrev_info *abbrev,
1235 struct dwarf2_cu *cu);
1236
1237 static void free_stack_comp_unit (void *);
1238
1239 static hashval_t partial_die_hash (const void *item);
1240
1241 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1242
1243 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1244 (unsigned int offset, struct objfile *objfile);
1245
1246 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1247 (unsigned int offset, struct objfile *objfile);
1248
1249 static void init_one_comp_unit (struct dwarf2_cu *cu,
1250 struct objfile *objfile);
1251
1252 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1253 struct die_info *comp_unit_die);
1254
1255 static void free_one_comp_unit (void *);
1256
1257 static void free_cached_comp_units (void *);
1258
1259 static void age_cached_comp_units (void);
1260
1261 static void free_one_cached_comp_unit (void *);
1262
1263 static struct type *set_die_type (struct die_info *, struct type *,
1264 struct dwarf2_cu *);
1265
1266 static void create_all_comp_units (struct objfile *);
1267
1268 static int create_debug_types_hash_table (struct objfile *objfile);
1269
1270 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1271 struct objfile *);
1272
1273 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1274
1275 static void dwarf2_add_dependence (struct dwarf2_cu *,
1276 struct dwarf2_per_cu_data *);
1277
1278 static void dwarf2_mark (struct dwarf2_cu *);
1279
1280 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1281
1282 static struct type *get_die_type_at_offset (unsigned int,
1283 struct dwarf2_per_cu_data *per_cu);
1284
1285 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1286
1287 static void dwarf2_release_queue (void *dummy);
1288
1289 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1290 struct objfile *objfile);
1291
1292 static void process_queue (struct objfile *objfile);
1293
1294 static void find_file_and_directory (struct die_info *die,
1295 struct dwarf2_cu *cu,
1296 char **name, char **comp_dir);
1297
1298 static char *file_full_name (int file, struct line_header *lh,
1299 const char *comp_dir);
1300
1301 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1302 gdb_byte *info_ptr,
1303 gdb_byte *buffer,
1304 unsigned int buffer_size,
1305 bfd *abfd);
1306
1307 static void init_cu_die_reader (struct die_reader_specs *reader,
1308 struct dwarf2_cu *cu);
1309
1310 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1311
1312 #if WORDS_BIGENDIAN
1313
1314 /* Convert VALUE between big- and little-endian. */
1315 static offset_type
1316 byte_swap (offset_type value)
1317 {
1318 offset_type result;
1319
1320 result = (value & 0xff) << 24;
1321 result |= (value & 0xff00) << 8;
1322 result |= (value & 0xff0000) >> 8;
1323 result |= (value & 0xff000000) >> 24;
1324 return result;
1325 }
1326
1327 #define MAYBE_SWAP(V) byte_swap (V)
1328
1329 #else
1330 #define MAYBE_SWAP(V) (V)
1331 #endif /* WORDS_BIGENDIAN */
1332
1333 /* The suffix for an index file. */
1334 #define INDEX_SUFFIX ".gdb-index"
1335
1336 static const char *dwarf2_physname (char *name, struct die_info *die,
1337 struct dwarf2_cu *cu);
1338
1339 /* Try to locate the sections we need for DWARF 2 debugging
1340 information and return true if we have enough to do something. */
1341
1342 int
1343 dwarf2_has_info (struct objfile *objfile)
1344 {
1345 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1346 if (!dwarf2_per_objfile)
1347 {
1348 /* Initialize per-objfile state. */
1349 struct dwarf2_per_objfile *data
1350 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1351
1352 memset (data, 0, sizeof (*data));
1353 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1354 dwarf2_per_objfile = data;
1355
1356 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections, NULL);
1357 dwarf2_per_objfile->objfile = objfile;
1358 }
1359 return (dwarf2_per_objfile->info.asection != NULL
1360 && dwarf2_per_objfile->abbrev.asection != NULL);
1361 }
1362
1363 /* When loading sections, we can either look for ".<name>", or for
1364 * ".z<name>", which indicates a compressed section. */
1365
1366 static int
1367 section_is_p (const char *section_name, const char *name)
1368 {
1369 return (section_name[0] == '.'
1370 && (strcmp (section_name + 1, name) == 0
1371 || (section_name[1] == 'z'
1372 && strcmp (section_name + 2, name) == 0)));
1373 }
1374
1375 /* This function is mapped across the sections and remembers the
1376 offset and size of each of the debugging sections we are interested
1377 in. */
1378
1379 static void
1380 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *ignore_ptr)
1381 {
1382 if (section_is_p (sectp->name, INFO_SECTION))
1383 {
1384 dwarf2_per_objfile->info.asection = sectp;
1385 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1386 }
1387 else if (section_is_p (sectp->name, ABBREV_SECTION))
1388 {
1389 dwarf2_per_objfile->abbrev.asection = sectp;
1390 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1391 }
1392 else if (section_is_p (sectp->name, LINE_SECTION))
1393 {
1394 dwarf2_per_objfile->line.asection = sectp;
1395 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1396 }
1397 else if (section_is_p (sectp->name, LOC_SECTION))
1398 {
1399 dwarf2_per_objfile->loc.asection = sectp;
1400 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1401 }
1402 else if (section_is_p (sectp->name, MACINFO_SECTION))
1403 {
1404 dwarf2_per_objfile->macinfo.asection = sectp;
1405 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1406 }
1407 else if (section_is_p (sectp->name, STR_SECTION))
1408 {
1409 dwarf2_per_objfile->str.asection = sectp;
1410 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1411 }
1412 else if (section_is_p (sectp->name, FRAME_SECTION))
1413 {
1414 dwarf2_per_objfile->frame.asection = sectp;
1415 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1416 }
1417 else if (section_is_p (sectp->name, EH_FRAME_SECTION))
1418 {
1419 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1420
1421 if (aflag & SEC_HAS_CONTENTS)
1422 {
1423 dwarf2_per_objfile->eh_frame.asection = sectp;
1424 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1425 }
1426 }
1427 else if (section_is_p (sectp->name, RANGES_SECTION))
1428 {
1429 dwarf2_per_objfile->ranges.asection = sectp;
1430 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1431 }
1432 else if (section_is_p (sectp->name, TYPES_SECTION))
1433 {
1434 dwarf2_per_objfile->types.asection = sectp;
1435 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1436 }
1437 else if (section_is_p (sectp->name, GDB_INDEX_SECTION))
1438 {
1439 dwarf2_per_objfile->gdb_index.asection = sectp;
1440 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1441 }
1442
1443 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1444 && bfd_section_vma (abfd, sectp) == 0)
1445 dwarf2_per_objfile->has_section_at_zero = 1;
1446 }
1447
1448 /* Decompress a section that was compressed using zlib. Store the
1449 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1450
1451 static void
1452 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1453 gdb_byte **outbuf, bfd_size_type *outsize)
1454 {
1455 bfd *abfd = objfile->obfd;
1456 #ifndef HAVE_ZLIB_H
1457 error (_("Support for zlib-compressed DWARF data (from '%s') "
1458 "is disabled in this copy of GDB"),
1459 bfd_get_filename (abfd));
1460 #else
1461 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1462 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1463 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1464 bfd_size_type uncompressed_size;
1465 gdb_byte *uncompressed_buffer;
1466 z_stream strm;
1467 int rc;
1468 int header_size = 12;
1469
1470 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1471 || bfd_bread (compressed_buffer, compressed_size, abfd) != compressed_size)
1472 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1473 bfd_get_filename (abfd));
1474
1475 /* Read the zlib header. In this case, it should be "ZLIB" followed
1476 by the uncompressed section size, 8 bytes in big-endian order. */
1477 if (compressed_size < header_size
1478 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1479 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1480 bfd_get_filename (abfd));
1481 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1482 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1483 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1484 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1485 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1486 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[11];
1489
1490 /* It is possible the section consists of several compressed
1491 buffers concatenated together, so we uncompress in a loop. */
1492 strm.zalloc = NULL;
1493 strm.zfree = NULL;
1494 strm.opaque = NULL;
1495 strm.avail_in = compressed_size - header_size;
1496 strm.next_in = (Bytef*) compressed_buffer + header_size;
1497 strm.avail_out = uncompressed_size;
1498 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1499 uncompressed_size);
1500 rc = inflateInit (&strm);
1501 while (strm.avail_in > 0)
1502 {
1503 if (rc != Z_OK)
1504 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1505 bfd_get_filename (abfd), rc);
1506 strm.next_out = ((Bytef*) uncompressed_buffer
1507 + (uncompressed_size - strm.avail_out));
1508 rc = inflate (&strm, Z_FINISH);
1509 if (rc != Z_STREAM_END)
1510 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1511 bfd_get_filename (abfd), rc);
1512 rc = inflateReset (&strm);
1513 }
1514 rc = inflateEnd (&strm);
1515 if (rc != Z_OK
1516 || strm.avail_out != 0)
1517 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1518 bfd_get_filename (abfd), rc);
1519
1520 do_cleanups (cleanup);
1521 *outbuf = uncompressed_buffer;
1522 *outsize = uncompressed_size;
1523 #endif
1524 }
1525
1526 /* Read the contents of the section SECTP from object file specified by
1527 OBJFILE, store info about the section into INFO.
1528 If the section is compressed, uncompress it before returning. */
1529
1530 static void
1531 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1532 {
1533 bfd *abfd = objfile->obfd;
1534 asection *sectp = info->asection;
1535 gdb_byte *buf, *retbuf;
1536 unsigned char header[4];
1537
1538 if (info->readin)
1539 return;
1540 info->buffer = NULL;
1541 info->was_mmapped = 0;
1542 info->readin = 1;
1543
1544 if (info->asection == NULL || info->size == 0)
1545 return;
1546
1547 /* Check if the file has a 4-byte header indicating compression. */
1548 if (info->size > sizeof (header)
1549 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1550 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1551 {
1552 /* Upon decompression, update the buffer and its size. */
1553 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1554 {
1555 zlib_decompress_section (objfile, sectp, &info->buffer,
1556 &info->size);
1557 return;
1558 }
1559 }
1560
1561 #ifdef HAVE_MMAP
1562 if (pagesize == 0)
1563 pagesize = getpagesize ();
1564
1565 /* Only try to mmap sections which are large enough: we don't want to
1566 waste space due to fragmentation. Also, only try mmap for sections
1567 without relocations. */
1568
1569 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1570 {
1571 off_t pg_offset = sectp->filepos & ~(pagesize - 1);
1572 size_t map_length = info->size + sectp->filepos - pg_offset;
1573 caddr_t retbuf = bfd_mmap (abfd, 0, map_length, PROT_READ,
1574 MAP_PRIVATE, pg_offset);
1575
1576 if (retbuf != MAP_FAILED)
1577 {
1578 info->was_mmapped = 1;
1579 info->buffer = retbuf + (sectp->filepos & (pagesize - 1)) ;
1580 #if HAVE_POSIX_MADVISE
1581 posix_madvise (retbuf, map_length, POSIX_MADV_WILLNEED);
1582 #endif
1583 return;
1584 }
1585 }
1586 #endif
1587
1588 /* If we get here, we are a normal, not-compressed section. */
1589 info->buffer = buf
1590 = obstack_alloc (&objfile->objfile_obstack, info->size);
1591
1592 /* When debugging .o files, we may need to apply relocations; see
1593 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1594 We never compress sections in .o files, so we only need to
1595 try this when the section is not compressed. */
1596 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1597 if (retbuf != NULL)
1598 {
1599 info->buffer = retbuf;
1600 return;
1601 }
1602
1603 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1604 || bfd_bread (buf, info->size, abfd) != info->size)
1605 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1606 bfd_get_filename (abfd));
1607 }
1608
1609 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1610 SECTION_NAME. */
1611
1612 void
1613 dwarf2_get_section_info (struct objfile *objfile, const char *section_name,
1614 asection **sectp, gdb_byte **bufp,
1615 bfd_size_type *sizep)
1616 {
1617 struct dwarf2_per_objfile *data
1618 = objfile_data (objfile, dwarf2_objfile_data_key);
1619 struct dwarf2_section_info *info;
1620
1621 /* We may see an objfile without any DWARF, in which case we just
1622 return nothing. */
1623 if (data == NULL)
1624 {
1625 *sectp = NULL;
1626 *bufp = NULL;
1627 *sizep = 0;
1628 return;
1629 }
1630 if (section_is_p (section_name, EH_FRAME_SECTION))
1631 info = &data->eh_frame;
1632 else if (section_is_p (section_name, FRAME_SECTION))
1633 info = &data->frame;
1634 else
1635 gdb_assert_not_reached ("unexpected section");
1636
1637 if (info->asection != NULL && info->size != 0 && info->buffer == NULL)
1638 /* We haven't read this section in yet. Do it now. */
1639 dwarf2_read_section (objfile, info);
1640
1641 *sectp = info->asection;
1642 *bufp = info->buffer;
1643 *sizep = info->size;
1644 }
1645
1646 \f
1647
1648 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1649 this CU came. */
1650
1651 static void
1652 dw2_do_instantiate_symtab (struct objfile *objfile,
1653 struct dwarf2_per_cu_data *per_cu)
1654 {
1655 struct cleanup *back_to;
1656
1657 back_to = make_cleanup (dwarf2_release_queue, NULL);
1658
1659 queue_comp_unit (per_cu, objfile);
1660
1661 if (per_cu->from_debug_types)
1662 read_signatured_type_at_offset (objfile, per_cu->offset);
1663 else
1664 load_full_comp_unit (per_cu, objfile);
1665
1666 process_queue (objfile);
1667
1668 /* Age the cache, releasing compilation units that have not
1669 been used recently. */
1670 age_cached_comp_units ();
1671
1672 do_cleanups (back_to);
1673 }
1674
1675 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1676 the objfile from which this CU came. Returns the resulting symbol
1677 table. */
1678
1679 static struct symtab *
1680 dw2_instantiate_symtab (struct objfile *objfile,
1681 struct dwarf2_per_cu_data *per_cu)
1682 {
1683 if (!per_cu->v.quick->symtab)
1684 {
1685 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1686 increment_reading_symtab ();
1687 dw2_do_instantiate_symtab (objfile, per_cu);
1688 do_cleanups (back_to);
1689 }
1690 return per_cu->v.quick->symtab;
1691 }
1692
1693 /* Return the CU given its index. */
1694
1695 static struct dwarf2_per_cu_data *
1696 dw2_get_cu (int index)
1697 {
1698 if (index >= dwarf2_per_objfile->n_comp_units)
1699 {
1700 index -= dwarf2_per_objfile->n_comp_units;
1701 return dwarf2_per_objfile->type_comp_units[index];
1702 }
1703 return dwarf2_per_objfile->all_comp_units[index];
1704 }
1705
1706 /* A helper function that knows how to read a 64-bit value in a way
1707 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1708 otherwise. */
1709
1710 static int
1711 extract_cu_value (const char *bytes, ULONGEST *result)
1712 {
1713 if (sizeof (ULONGEST) < 8)
1714 {
1715 int i;
1716
1717 /* Ignore the upper 4 bytes if they are all zero. */
1718 for (i = 0; i < 4; ++i)
1719 if (bytes[i + 4] != 0)
1720 return 0;
1721
1722 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1723 }
1724 else
1725 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1726 return 1;
1727 }
1728
1729 /* Read the CU list from the mapped index, and use it to create all
1730 the CU objects for this objfile. Return 0 if something went wrong,
1731 1 if everything went ok. */
1732
1733 static int
1734 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1735 offset_type cu_list_elements)
1736 {
1737 offset_type i;
1738
1739 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1740 dwarf2_per_objfile->all_comp_units
1741 = obstack_alloc (&objfile->objfile_obstack,
1742 dwarf2_per_objfile->n_comp_units
1743 * sizeof (struct dwarf2_per_cu_data *));
1744
1745 for (i = 0; i < cu_list_elements; i += 2)
1746 {
1747 struct dwarf2_per_cu_data *the_cu;
1748 ULONGEST offset, length;
1749
1750 if (!extract_cu_value (cu_list, &offset)
1751 || !extract_cu_value (cu_list + 8, &length))
1752 return 0;
1753 cu_list += 2 * 8;
1754
1755 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1756 struct dwarf2_per_cu_data);
1757 the_cu->offset = offset;
1758 the_cu->length = length;
1759 the_cu->objfile = objfile;
1760 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1761 struct dwarf2_per_cu_quick_data);
1762 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1763 }
1764
1765 return 1;
1766 }
1767
1768 /* Create the signatured type hash table from the index. */
1769
1770 static int
1771 create_signatured_type_table_from_index (struct objfile *objfile,
1772 const gdb_byte *bytes,
1773 offset_type elements)
1774 {
1775 offset_type i;
1776 htab_t sig_types_hash;
1777
1778 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1779 dwarf2_per_objfile->type_comp_units
1780 = obstack_alloc (&objfile->objfile_obstack,
1781 dwarf2_per_objfile->n_type_comp_units
1782 * sizeof (struct dwarf2_per_cu_data *));
1783
1784 sig_types_hash = allocate_signatured_type_table (objfile);
1785
1786 for (i = 0; i < elements; i += 3)
1787 {
1788 struct signatured_type *type_sig;
1789 ULONGEST offset, type_offset, signature;
1790 void **slot;
1791
1792 if (!extract_cu_value (bytes, &offset)
1793 || !extract_cu_value (bytes + 8, &type_offset))
1794 return 0;
1795 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1796 bytes += 3 * 8;
1797
1798 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1799 struct signatured_type);
1800 type_sig->signature = signature;
1801 type_sig->offset = offset;
1802 type_sig->type_offset = type_offset;
1803 type_sig->per_cu.from_debug_types = 1;
1804 type_sig->per_cu.offset = offset;
1805 type_sig->per_cu.objfile = objfile;
1806 type_sig->per_cu.v.quick
1807 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1808 struct dwarf2_per_cu_quick_data);
1809
1810 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1811 *slot = type_sig;
1812
1813 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1814 }
1815
1816 dwarf2_per_objfile->signatured_types = sig_types_hash;
1817
1818 return 1;
1819 }
1820
1821 /* Read the address map data from the mapped index, and use it to
1822 populate the objfile's psymtabs_addrmap. */
1823
1824 static void
1825 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1826 {
1827 const gdb_byte *iter, *end;
1828 struct obstack temp_obstack;
1829 struct addrmap *mutable_map;
1830 struct cleanup *cleanup;
1831 CORE_ADDR baseaddr;
1832
1833 obstack_init (&temp_obstack);
1834 cleanup = make_cleanup_obstack_free (&temp_obstack);
1835 mutable_map = addrmap_create_mutable (&temp_obstack);
1836
1837 iter = index->address_table;
1838 end = iter + index->address_table_size;
1839
1840 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1841
1842 while (iter < end)
1843 {
1844 ULONGEST hi, lo, cu_index;
1845 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1846 iter += 8;
1847 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1848 iter += 8;
1849 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1850 iter += 4;
1851
1852 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1853 dw2_get_cu (cu_index));
1854 }
1855
1856 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1857 &objfile->objfile_obstack);
1858 do_cleanups (cleanup);
1859 }
1860
1861 /* The hash function for strings in the mapped index. This is the
1862 same as the hashtab.c hash function, but we keep a separate copy to
1863 maintain control over the implementation. This is necessary
1864 because the hash function is tied to the format of the mapped index
1865 file. */
1866
1867 static hashval_t
1868 mapped_index_string_hash (const void *p)
1869 {
1870 const unsigned char *str = (const unsigned char *) p;
1871 hashval_t r = 0;
1872 unsigned char c;
1873
1874 while ((c = *str++) != 0)
1875 r = r * 67 + c - 113;
1876
1877 return r;
1878 }
1879
1880 /* Find a slot in the mapped index INDEX for the object named NAME.
1881 If NAME is found, set *VEC_OUT to point to the CU vector in the
1882 constant pool and return 1. If NAME cannot be found, return 0. */
1883
1884 static int
1885 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
1886 offset_type **vec_out)
1887 {
1888 offset_type hash = mapped_index_string_hash (name);
1889 offset_type slot, step;
1890
1891 slot = hash & (index->symbol_table_slots - 1);
1892 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
1893
1894 for (;;)
1895 {
1896 /* Convert a slot number to an offset into the table. */
1897 offset_type i = 2 * slot;
1898 const char *str;
1899 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
1900 return 0;
1901
1902 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
1903 if (!strcmp (name, str))
1904 {
1905 *vec_out = (offset_type *) (index->constant_pool
1906 + MAYBE_SWAP (index->symbol_table[i + 1]));
1907 return 1;
1908 }
1909
1910 slot = (slot + step) & (index->symbol_table_slots - 1);
1911 }
1912 }
1913
1914 /* Read the index file. If everything went ok, initialize the "quick"
1915 elements of all the CUs and return 1. Otherwise, return 0. */
1916
1917 static int
1918 dwarf2_read_index (struct objfile *objfile)
1919 {
1920 char *addr;
1921 struct mapped_index *map;
1922 offset_type *metadata;
1923 const gdb_byte *cu_list;
1924 const gdb_byte *types_list = NULL;
1925 offset_type version, cu_list_elements;
1926 offset_type types_list_elements = 0;
1927 int i;
1928
1929 if (dwarf2_per_objfile->gdb_index.asection == NULL
1930 || dwarf2_per_objfile->gdb_index.size == 0)
1931 return 0;
1932
1933 /* Older elfutils strip versions could keep the section in the main
1934 executable while splitting it for the separate debug info file. */
1935 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
1936 & SEC_HAS_CONTENTS) == 0)
1937 return 0;
1938
1939 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
1940
1941 addr = dwarf2_per_objfile->gdb_index.buffer;
1942 /* Version check. */
1943 version = MAYBE_SWAP (*(offset_type *) addr);
1944 /* Versions earlier than 3 emitted every copy of a psymbol. This
1945 causes the index to behave very poorly for certain requests. So,
1946 it seems better to just ignore such indices. */
1947 if (version < 3)
1948 return 0;
1949 /* Indexes with higher version than the one supported by GDB may be no
1950 longer backward compatible. */
1951 if (version > 3)
1952 return 0;
1953
1954 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
1955 map->total_size = dwarf2_per_objfile->gdb_index.size;
1956
1957 metadata = (offset_type *) (addr + sizeof (offset_type));
1958
1959 i = 0;
1960 cu_list = addr + MAYBE_SWAP (metadata[i]);
1961 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
1962 / 8);
1963 ++i;
1964
1965 types_list = addr + MAYBE_SWAP (metadata[i]);
1966 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
1967 - MAYBE_SWAP (metadata[i]))
1968 / 8);
1969 ++i;
1970
1971 map->address_table = addr + MAYBE_SWAP (metadata[i]);
1972 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
1973 - MAYBE_SWAP (metadata[i]));
1974 ++i;
1975
1976 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
1977 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
1978 - MAYBE_SWAP (metadata[i]))
1979 / (2 * sizeof (offset_type)));
1980 ++i;
1981
1982 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
1983
1984 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
1985 return 0;
1986
1987 if (types_list_elements
1988 && !create_signatured_type_table_from_index (objfile, types_list,
1989 types_list_elements))
1990 return 0;
1991
1992 create_addrmap_from_index (objfile, map);
1993
1994 dwarf2_per_objfile->index_table = map;
1995 dwarf2_per_objfile->using_index = 1;
1996
1997 return 1;
1998 }
1999
2000 /* A helper for the "quick" functions which sets the global
2001 dwarf2_per_objfile according to OBJFILE. */
2002
2003 static void
2004 dw2_setup (struct objfile *objfile)
2005 {
2006 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2007 gdb_assert (dwarf2_per_objfile);
2008 }
2009
2010 /* A helper for the "quick" functions which attempts to read the line
2011 table for THIS_CU. */
2012
2013 static void
2014 dw2_require_line_header (struct objfile *objfile,
2015 struct dwarf2_per_cu_data *this_cu)
2016 {
2017 bfd *abfd = objfile->obfd;
2018 struct line_header *lh = NULL;
2019 struct attribute *attr;
2020 struct cleanup *cleanups;
2021 struct die_info *comp_unit_die;
2022 struct dwarf2_section_info* sec;
2023 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2024 int has_children, i;
2025 struct dwarf2_cu cu;
2026 unsigned int bytes_read, buffer_size;
2027 struct die_reader_specs reader_specs;
2028 char *name, *comp_dir;
2029
2030 if (this_cu->v.quick->read_lines)
2031 return;
2032 this_cu->v.quick->read_lines = 1;
2033
2034 init_one_comp_unit (&cu, objfile);
2035 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2036
2037 if (this_cu->from_debug_types)
2038 sec = &dwarf2_per_objfile->types;
2039 else
2040 sec = &dwarf2_per_objfile->info;
2041 dwarf2_read_section (objfile, sec);
2042 buffer_size = sec->size;
2043 buffer = sec->buffer;
2044 info_ptr = buffer + this_cu->offset;
2045 beg_of_comp_unit = info_ptr;
2046
2047 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2048 buffer, buffer_size,
2049 abfd);
2050
2051 /* Complete the cu_header. */
2052 cu.header.offset = beg_of_comp_unit - buffer;
2053 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2054
2055 this_cu->cu = &cu;
2056 cu.per_cu = this_cu;
2057
2058 dwarf2_read_abbrevs (abfd, &cu);
2059 make_cleanup (dwarf2_free_abbrev_table, &cu);
2060
2061 if (this_cu->from_debug_types)
2062 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2063 init_cu_die_reader (&reader_specs, &cu);
2064 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2065 &has_children);
2066
2067 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2068 if (attr)
2069 {
2070 unsigned int line_offset = DW_UNSND (attr);
2071 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2072 }
2073 if (lh == NULL)
2074 {
2075 do_cleanups (cleanups);
2076 return;
2077 }
2078
2079 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2080
2081 this_cu->v.quick->lines = lh;
2082
2083 this_cu->v.quick->file_names
2084 = obstack_alloc (&objfile->objfile_obstack,
2085 lh->num_file_names * sizeof (char *));
2086 for (i = 0; i < lh->num_file_names; ++i)
2087 this_cu->v.quick->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2088
2089 do_cleanups (cleanups);
2090 }
2091
2092 /* A helper for the "quick" functions which computes and caches the
2093 real path for a given file name from the line table.
2094 dw2_require_line_header must have been called before this is
2095 invoked. */
2096
2097 static const char *
2098 dw2_require_full_path (struct objfile *objfile,
2099 struct dwarf2_per_cu_data *per_cu,
2100 int index)
2101 {
2102 if (!per_cu->v.quick->full_names)
2103 per_cu->v.quick->full_names
2104 = OBSTACK_CALLOC (&objfile->objfile_obstack,
2105 per_cu->v.quick->lines->num_file_names,
2106 sizeof (char *));
2107
2108 if (!per_cu->v.quick->full_names[index])
2109 per_cu->v.quick->full_names[index]
2110 = gdb_realpath (per_cu->v.quick->file_names[index]);
2111
2112 return per_cu->v.quick->full_names[index];
2113 }
2114
2115 static struct symtab *
2116 dw2_find_last_source_symtab (struct objfile *objfile)
2117 {
2118 int index;
2119
2120 dw2_setup (objfile);
2121 index = dwarf2_per_objfile->n_comp_units - 1;
2122 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2123 }
2124
2125 static void
2126 dw2_forget_cached_source_info (struct objfile *objfile)
2127 {
2128 int i;
2129
2130 dw2_setup (objfile);
2131 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2132 + dwarf2_per_objfile->n_type_comp_units); ++i)
2133 {
2134 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2135
2136 if (per_cu->v.quick->full_names)
2137 {
2138 int j;
2139
2140 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2141 {
2142 xfree ((void *) per_cu->v.quick->full_names[j]);
2143 per_cu->v.quick->full_names[j] = NULL;
2144 }
2145 }
2146 }
2147 }
2148
2149 static int
2150 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2151 const char *full_path, const char *real_path,
2152 struct symtab **result)
2153 {
2154 int i;
2155 int check_basename = lbasename (name) == name;
2156 struct dwarf2_per_cu_data *base_cu = NULL;
2157
2158 dw2_setup (objfile);
2159
2160 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2161 + dwarf2_per_objfile->n_type_comp_units); ++i)
2162 {
2163 int j;
2164 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2165
2166 if (per_cu->v.quick->symtab)
2167 continue;
2168
2169 dw2_require_line_header (objfile, per_cu);
2170 if (!per_cu->v.quick->lines)
2171 continue;
2172
2173 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2174 {
2175 const char *this_name = per_cu->v.quick->file_names[j];
2176
2177 if (FILENAME_CMP (name, this_name) == 0)
2178 {
2179 *result = dw2_instantiate_symtab (objfile, per_cu);
2180 return 1;
2181 }
2182
2183 if (check_basename && ! base_cu
2184 && FILENAME_CMP (lbasename (this_name), name) == 0)
2185 base_cu = per_cu;
2186
2187 if (full_path != NULL)
2188 {
2189 const char *this_full_name = dw2_require_full_path (objfile,
2190 per_cu, j);
2191
2192 if (this_full_name
2193 && FILENAME_CMP (full_path, this_full_name) == 0)
2194 {
2195 *result = dw2_instantiate_symtab (objfile, per_cu);
2196 return 1;
2197 }
2198 }
2199
2200 if (real_path != NULL)
2201 {
2202 const char *this_full_name = dw2_require_full_path (objfile,
2203 per_cu, j);
2204
2205 if (this_full_name != NULL
2206 && FILENAME_CMP (real_path, this_full_name) == 0)
2207 {
2208 *result = dw2_instantiate_symtab (objfile, per_cu);
2209 return 1;
2210 }
2211 }
2212 }
2213 }
2214
2215 if (base_cu)
2216 {
2217 *result = dw2_instantiate_symtab (objfile, base_cu);
2218 return 1;
2219 }
2220
2221 return 0;
2222 }
2223
2224 static struct symtab *
2225 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2226 const char *name, domain_enum domain)
2227 {
2228 /* We do all the work in the pre_expand_symtabs_matching hook
2229 instead. */
2230 return NULL;
2231 }
2232
2233 /* A helper function that expands all symtabs that hold an object
2234 named NAME. */
2235
2236 static void
2237 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2238 {
2239 dw2_setup (objfile);
2240
2241 /* index_table is NULL if OBJF_READNOW. */
2242 if (dwarf2_per_objfile->index_table)
2243 {
2244 offset_type *vec;
2245
2246 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2247 name, &vec))
2248 {
2249 offset_type i, len = MAYBE_SWAP (*vec);
2250 for (i = 0; i < len; ++i)
2251 {
2252 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2253 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2254
2255 dw2_instantiate_symtab (objfile, per_cu);
2256 }
2257 }
2258 }
2259 }
2260
2261 static void
2262 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2263 int kind, const char *name,
2264 domain_enum domain)
2265 {
2266 dw2_do_expand_symtabs_matching (objfile, name);
2267 }
2268
2269 static void
2270 dw2_print_stats (struct objfile *objfile)
2271 {
2272 int i, count;
2273
2274 dw2_setup (objfile);
2275 count = 0;
2276 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2277 + dwarf2_per_objfile->n_type_comp_units); ++i)
2278 {
2279 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2280
2281 if (!per_cu->v.quick->symtab)
2282 ++count;
2283 }
2284 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2285 }
2286
2287 static void
2288 dw2_dump (struct objfile *objfile)
2289 {
2290 /* Nothing worth printing. */
2291 }
2292
2293 static void
2294 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2295 struct section_offsets *delta)
2296 {
2297 /* There's nothing to relocate here. */
2298 }
2299
2300 static void
2301 dw2_expand_symtabs_for_function (struct objfile *objfile,
2302 const char *func_name)
2303 {
2304 dw2_do_expand_symtabs_matching (objfile, func_name);
2305 }
2306
2307 static void
2308 dw2_expand_all_symtabs (struct objfile *objfile)
2309 {
2310 int i;
2311
2312 dw2_setup (objfile);
2313
2314 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2315 + dwarf2_per_objfile->n_type_comp_units); ++i)
2316 {
2317 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2318
2319 dw2_instantiate_symtab (objfile, per_cu);
2320 }
2321 }
2322
2323 static void
2324 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2325 const char *filename)
2326 {
2327 int i;
2328
2329 dw2_setup (objfile);
2330
2331 /* We don't need to consider type units here.
2332 This is only called for examining code, e.g. expand_line_sal.
2333 There can be an order of magnitude (or more) more type units
2334 than comp units, and we avoid them if we can. */
2335
2336 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2337 {
2338 int j;
2339 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2340
2341 if (per_cu->v.quick->symtab)
2342 continue;
2343
2344 dw2_require_line_header (objfile, per_cu);
2345 if (!per_cu->v.quick->lines)
2346 continue;
2347
2348 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2349 {
2350 const char *this_name = per_cu->v.quick->file_names[j];
2351 if (FILENAME_CMP (this_name, filename) == 0)
2352 {
2353 dw2_instantiate_symtab (objfile, per_cu);
2354 break;
2355 }
2356 }
2357 }
2358 }
2359
2360 static const char *
2361 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2362 {
2363 struct dwarf2_per_cu_data *per_cu;
2364 offset_type *vec;
2365
2366 dw2_setup (objfile);
2367
2368 /* index_table is NULL if OBJF_READNOW. */
2369 if (!dwarf2_per_objfile->index_table)
2370 return NULL;
2371
2372 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2373 name, &vec))
2374 return NULL;
2375
2376 /* Note that this just looks at the very first one named NAME -- but
2377 actually we are looking for a function. find_main_filename
2378 should be rewritten so that it doesn't require a custom hook. It
2379 could just use the ordinary symbol tables. */
2380 /* vec[0] is the length, which must always be >0. */
2381 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2382
2383 dw2_require_line_header (objfile, per_cu);
2384 if (!per_cu->v.quick->lines)
2385 return NULL;
2386
2387 return per_cu->v.quick->file_names[per_cu->v.quick->lines->num_file_names - 1];
2388 }
2389
2390 static void
2391 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2392 struct objfile *objfile, int global,
2393 int (*callback) (struct block *,
2394 struct symbol *, void *),
2395 void *data, symbol_compare_ftype *match,
2396 symbol_compare_ftype *ordered_compare)
2397 {
2398 /* Currently unimplemented; used for Ada. The function can be called if the
2399 current language is Ada for a non-Ada objfile using GNU index. As Ada
2400 does not look for non-Ada symbols this function should just return. */
2401 }
2402
2403 static void
2404 dw2_expand_symtabs_matching (struct objfile *objfile,
2405 int (*file_matcher) (const char *, void *),
2406 int (*name_matcher) (const char *, void *),
2407 domain_enum kind,
2408 void *data)
2409 {
2410 int i;
2411 offset_type iter;
2412 struct mapped_index *index;
2413
2414 dw2_setup (objfile);
2415
2416 /* index_table is NULL if OBJF_READNOW. */
2417 if (!dwarf2_per_objfile->index_table)
2418 return;
2419 index = dwarf2_per_objfile->index_table;
2420
2421 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2422 + dwarf2_per_objfile->n_type_comp_units); ++i)
2423 {
2424 int j;
2425 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2426
2427 per_cu->v.quick->mark = 0;
2428 if (per_cu->v.quick->symtab)
2429 continue;
2430
2431 dw2_require_line_header (objfile, per_cu);
2432 if (!per_cu->v.quick->lines)
2433 continue;
2434
2435 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2436 {
2437 if (file_matcher (per_cu->v.quick->file_names[j], data))
2438 {
2439 per_cu->v.quick->mark = 1;
2440 break;
2441 }
2442 }
2443 }
2444
2445 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2446 {
2447 offset_type idx = 2 * iter;
2448 const char *name;
2449 offset_type *vec, vec_len, vec_idx;
2450
2451 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2452 continue;
2453
2454 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2455
2456 if (! (*name_matcher) (name, data))
2457 continue;
2458
2459 /* The name was matched, now expand corresponding CUs that were
2460 marked. */
2461 vec = (offset_type *) (index->constant_pool
2462 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2463 vec_len = MAYBE_SWAP (vec[0]);
2464 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2465 {
2466 struct dwarf2_per_cu_data *per_cu;
2467
2468 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2469 if (per_cu->v.quick->mark)
2470 dw2_instantiate_symtab (objfile, per_cu);
2471 }
2472 }
2473 }
2474
2475 static struct symtab *
2476 dw2_find_pc_sect_symtab (struct objfile *objfile,
2477 struct minimal_symbol *msymbol,
2478 CORE_ADDR pc,
2479 struct obj_section *section,
2480 int warn_if_readin)
2481 {
2482 struct dwarf2_per_cu_data *data;
2483
2484 dw2_setup (objfile);
2485
2486 if (!objfile->psymtabs_addrmap)
2487 return NULL;
2488
2489 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2490 if (!data)
2491 return NULL;
2492
2493 if (warn_if_readin && data->v.quick->symtab)
2494 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2495 paddress (get_objfile_arch (objfile), pc));
2496
2497 return dw2_instantiate_symtab (objfile, data);
2498 }
2499
2500 static void
2501 dw2_map_symbol_names (struct objfile *objfile,
2502 void (*fun) (const char *, void *),
2503 void *data)
2504 {
2505 offset_type iter;
2506 struct mapped_index *index;
2507
2508 dw2_setup (objfile);
2509
2510 /* index_table is NULL if OBJF_READNOW. */
2511 if (!dwarf2_per_objfile->index_table)
2512 return;
2513 index = dwarf2_per_objfile->index_table;
2514
2515 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2516 {
2517 offset_type idx = 2 * iter;
2518 const char *name;
2519 offset_type *vec, vec_len, vec_idx;
2520
2521 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2522 continue;
2523
2524 name = (index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]));
2525
2526 (*fun) (name, data);
2527 }
2528 }
2529
2530 static void
2531 dw2_map_symbol_filenames (struct objfile *objfile,
2532 void (*fun) (const char *, const char *, void *),
2533 void *data)
2534 {
2535 int i;
2536
2537 dw2_setup (objfile);
2538
2539 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2540 + dwarf2_per_objfile->n_type_comp_units); ++i)
2541 {
2542 int j;
2543 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2544
2545 if (per_cu->v.quick->symtab)
2546 continue;
2547
2548 dw2_require_line_header (objfile, per_cu);
2549 if (!per_cu->v.quick->lines)
2550 continue;
2551
2552 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
2553 {
2554 const char *this_full_name = dw2_require_full_path (objfile, per_cu,
2555 j);
2556 (*fun) (per_cu->v.quick->file_names[j], this_full_name, data);
2557 }
2558 }
2559 }
2560
2561 static int
2562 dw2_has_symbols (struct objfile *objfile)
2563 {
2564 return 1;
2565 }
2566
2567 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2568 {
2569 dw2_has_symbols,
2570 dw2_find_last_source_symtab,
2571 dw2_forget_cached_source_info,
2572 dw2_lookup_symtab,
2573 dw2_lookup_symbol,
2574 dw2_pre_expand_symtabs_matching,
2575 dw2_print_stats,
2576 dw2_dump,
2577 dw2_relocate,
2578 dw2_expand_symtabs_for_function,
2579 dw2_expand_all_symtabs,
2580 dw2_expand_symtabs_with_filename,
2581 dw2_find_symbol_file,
2582 dw2_map_matching_symbols,
2583 dw2_expand_symtabs_matching,
2584 dw2_find_pc_sect_symtab,
2585 dw2_map_symbol_names,
2586 dw2_map_symbol_filenames
2587 };
2588
2589 /* Initialize for reading DWARF for this objfile. Return 0 if this
2590 file will use psymtabs, or 1 if using the GNU index. */
2591
2592 int
2593 dwarf2_initialize_objfile (struct objfile *objfile)
2594 {
2595 /* If we're about to read full symbols, don't bother with the
2596 indices. In this case we also don't care if some other debug
2597 format is making psymtabs, because they are all about to be
2598 expanded anyway. */
2599 if ((objfile->flags & OBJF_READNOW))
2600 {
2601 int i;
2602
2603 dwarf2_per_objfile->using_index = 1;
2604 create_all_comp_units (objfile);
2605 create_debug_types_hash_table (objfile);
2606
2607 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2608 + dwarf2_per_objfile->n_type_comp_units); ++i)
2609 {
2610 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2611
2612 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2613 struct dwarf2_per_cu_quick_data);
2614 }
2615
2616 /* Return 1 so that gdb sees the "quick" functions. However,
2617 these functions will be no-ops because we will have expanded
2618 all symtabs. */
2619 return 1;
2620 }
2621
2622 if (dwarf2_read_index (objfile))
2623 return 1;
2624
2625 dwarf2_build_psymtabs (objfile);
2626 return 0;
2627 }
2628
2629 \f
2630
2631 /* Build a partial symbol table. */
2632
2633 void
2634 dwarf2_build_psymtabs (struct objfile *objfile)
2635 {
2636 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2637 {
2638 init_psymbol_list (objfile, 1024);
2639 }
2640
2641 dwarf2_build_psymtabs_hard (objfile);
2642 }
2643
2644 /* Return TRUE if OFFSET is within CU_HEADER. */
2645
2646 static inline int
2647 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2648 {
2649 unsigned int bottom = cu_header->offset;
2650 unsigned int top = (cu_header->offset
2651 + cu_header->length
2652 + cu_header->initial_length_size);
2653
2654 return (offset >= bottom && offset < top);
2655 }
2656
2657 /* Read in the comp unit header information from the debug_info at info_ptr.
2658 NOTE: This leaves members offset, first_die_offset to be filled in
2659 by the caller. */
2660
2661 static gdb_byte *
2662 read_comp_unit_head (struct comp_unit_head *cu_header,
2663 gdb_byte *info_ptr, bfd *abfd)
2664 {
2665 int signed_addr;
2666 unsigned int bytes_read;
2667
2668 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2669 cu_header->initial_length_size = bytes_read;
2670 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2671 info_ptr += bytes_read;
2672 cu_header->version = read_2_bytes (abfd, info_ptr);
2673 info_ptr += 2;
2674 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2675 &bytes_read);
2676 info_ptr += bytes_read;
2677 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2678 info_ptr += 1;
2679 signed_addr = bfd_get_sign_extend_vma (abfd);
2680 if (signed_addr < 0)
2681 internal_error (__FILE__, __LINE__,
2682 _("read_comp_unit_head: dwarf from non elf file"));
2683 cu_header->signed_addr_p = signed_addr;
2684
2685 return info_ptr;
2686 }
2687
2688 static gdb_byte *
2689 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2690 gdb_byte *buffer, unsigned int buffer_size,
2691 bfd *abfd)
2692 {
2693 gdb_byte *beg_of_comp_unit = info_ptr;
2694
2695 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2696
2697 if (header->version != 2 && header->version != 3 && header->version != 4)
2698 error (_("Dwarf Error: wrong version in compilation unit header "
2699 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2700 bfd_get_filename (abfd));
2701
2702 if (header->abbrev_offset >= dwarf2_per_objfile->abbrev.size)
2703 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2704 "(offset 0x%lx + 6) [in module %s]"),
2705 (long) header->abbrev_offset,
2706 (long) (beg_of_comp_unit - buffer),
2707 bfd_get_filename (abfd));
2708
2709 if (beg_of_comp_unit + header->length + header->initial_length_size
2710 > buffer + buffer_size)
2711 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2712 "(offset 0x%lx + 0) [in module %s]"),
2713 (long) header->length,
2714 (long) (beg_of_comp_unit - buffer),
2715 bfd_get_filename (abfd));
2716
2717 return info_ptr;
2718 }
2719
2720 /* Read in the types comp unit header information from .debug_types entry at
2721 types_ptr. The result is a pointer to one past the end of the header. */
2722
2723 static gdb_byte *
2724 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2725 ULONGEST *signature,
2726 gdb_byte *types_ptr, bfd *abfd)
2727 {
2728 gdb_byte *initial_types_ptr = types_ptr;
2729
2730 dwarf2_read_section (dwarf2_per_objfile->objfile,
2731 &dwarf2_per_objfile->types);
2732 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2733
2734 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2735
2736 *signature = read_8_bytes (abfd, types_ptr);
2737 types_ptr += 8;
2738 types_ptr += cu_header->offset_size;
2739 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2740
2741 return types_ptr;
2742 }
2743
2744 /* Allocate a new partial symtab for file named NAME and mark this new
2745 partial symtab as being an include of PST. */
2746
2747 static void
2748 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2749 struct objfile *objfile)
2750 {
2751 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2752
2753 subpst->section_offsets = pst->section_offsets;
2754 subpst->textlow = 0;
2755 subpst->texthigh = 0;
2756
2757 subpst->dependencies = (struct partial_symtab **)
2758 obstack_alloc (&objfile->objfile_obstack,
2759 sizeof (struct partial_symtab *));
2760 subpst->dependencies[0] = pst;
2761 subpst->number_of_dependencies = 1;
2762
2763 subpst->globals_offset = 0;
2764 subpst->n_global_syms = 0;
2765 subpst->statics_offset = 0;
2766 subpst->n_static_syms = 0;
2767 subpst->symtab = NULL;
2768 subpst->read_symtab = pst->read_symtab;
2769 subpst->readin = 0;
2770
2771 /* No private part is necessary for include psymtabs. This property
2772 can be used to differentiate between such include psymtabs and
2773 the regular ones. */
2774 subpst->read_symtab_private = NULL;
2775 }
2776
2777 /* Read the Line Number Program data and extract the list of files
2778 included by the source file represented by PST. Build an include
2779 partial symtab for each of these included files. */
2780
2781 static void
2782 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2783 struct die_info *die,
2784 struct partial_symtab *pst)
2785 {
2786 struct objfile *objfile = cu->objfile;
2787 bfd *abfd = objfile->obfd;
2788 struct line_header *lh = NULL;
2789 struct attribute *attr;
2790
2791 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2792 if (attr)
2793 {
2794 unsigned int line_offset = DW_UNSND (attr);
2795
2796 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2797 }
2798 if (lh == NULL)
2799 return; /* No linetable, so no includes. */
2800
2801 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2802 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2803
2804 free_line_header (lh);
2805 }
2806
2807 static hashval_t
2808 hash_type_signature (const void *item)
2809 {
2810 const struct signatured_type *type_sig = item;
2811
2812 /* This drops the top 32 bits of the signature, but is ok for a hash. */
2813 return type_sig->signature;
2814 }
2815
2816 static int
2817 eq_type_signature (const void *item_lhs, const void *item_rhs)
2818 {
2819 const struct signatured_type *lhs = item_lhs;
2820 const struct signatured_type *rhs = item_rhs;
2821
2822 return lhs->signature == rhs->signature;
2823 }
2824
2825 /* Allocate a hash table for signatured types. */
2826
2827 static htab_t
2828 allocate_signatured_type_table (struct objfile *objfile)
2829 {
2830 return htab_create_alloc_ex (41,
2831 hash_type_signature,
2832 eq_type_signature,
2833 NULL,
2834 &objfile->objfile_obstack,
2835 hashtab_obstack_allocate,
2836 dummy_obstack_deallocate);
2837 }
2838
2839 /* A helper function to add a signatured type CU to a list. */
2840
2841 static int
2842 add_signatured_type_cu_to_list (void **slot, void *datum)
2843 {
2844 struct signatured_type *sigt = *slot;
2845 struct dwarf2_per_cu_data ***datap = datum;
2846
2847 **datap = &sigt->per_cu;
2848 ++*datap;
2849
2850 return 1;
2851 }
2852
2853 /* Create the hash table of all entries in the .debug_types section.
2854 The result is zero if there is an error (e.g. missing .debug_types section),
2855 otherwise non-zero. */
2856
2857 static int
2858 create_debug_types_hash_table (struct objfile *objfile)
2859 {
2860 gdb_byte *info_ptr;
2861 htab_t types_htab;
2862 struct dwarf2_per_cu_data **iter;
2863
2864 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
2865 info_ptr = dwarf2_per_objfile->types.buffer;
2866
2867 if (info_ptr == NULL)
2868 {
2869 dwarf2_per_objfile->signatured_types = NULL;
2870 return 0;
2871 }
2872
2873 types_htab = allocate_signatured_type_table (objfile);
2874
2875 if (dwarf2_die_debug)
2876 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
2877
2878 while (info_ptr < dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2879 {
2880 unsigned int offset;
2881 unsigned int offset_size;
2882 unsigned int type_offset;
2883 unsigned int length, initial_length_size;
2884 unsigned short version;
2885 ULONGEST signature;
2886 struct signatured_type *type_sig;
2887 void **slot;
2888 gdb_byte *ptr = info_ptr;
2889
2890 offset = ptr - dwarf2_per_objfile->types.buffer;
2891
2892 /* We need to read the type's signature in order to build the hash
2893 table, but we don't need to read anything else just yet. */
2894
2895 /* Sanity check to ensure entire cu is present. */
2896 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
2897 if (ptr + length + initial_length_size
2898 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
2899 {
2900 complaint (&symfile_complaints,
2901 _("debug type entry runs off end of `.debug_types' section, ignored"));
2902 break;
2903 }
2904
2905 offset_size = initial_length_size == 4 ? 4 : 8;
2906 ptr += initial_length_size;
2907 version = bfd_get_16 (objfile->obfd, ptr);
2908 ptr += 2;
2909 ptr += offset_size; /* abbrev offset */
2910 ptr += 1; /* address size */
2911 signature = bfd_get_64 (objfile->obfd, ptr);
2912 ptr += 8;
2913 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
2914
2915 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
2916 memset (type_sig, 0, sizeof (*type_sig));
2917 type_sig->signature = signature;
2918 type_sig->offset = offset;
2919 type_sig->type_offset = type_offset;
2920 type_sig->per_cu.objfile = objfile;
2921 type_sig->per_cu.from_debug_types = 1;
2922
2923 slot = htab_find_slot (types_htab, type_sig, INSERT);
2924 gdb_assert (slot != NULL);
2925 *slot = type_sig;
2926
2927 if (dwarf2_die_debug)
2928 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
2929 offset, phex (signature, sizeof (signature)));
2930
2931 info_ptr = info_ptr + initial_length_size + length;
2932 }
2933
2934 dwarf2_per_objfile->signatured_types = types_htab;
2935
2936 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
2937 dwarf2_per_objfile->type_comp_units
2938 = obstack_alloc (&objfile->objfile_obstack,
2939 dwarf2_per_objfile->n_type_comp_units
2940 * sizeof (struct dwarf2_per_cu_data *));
2941 iter = &dwarf2_per_objfile->type_comp_units[0];
2942 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
2943 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
2944 == dwarf2_per_objfile->n_type_comp_units);
2945
2946 return 1;
2947 }
2948
2949 /* Lookup a signature based type.
2950 Returns NULL if SIG is not present in the table. */
2951
2952 static struct signatured_type *
2953 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
2954 {
2955 struct signatured_type find_entry, *entry;
2956
2957 if (dwarf2_per_objfile->signatured_types == NULL)
2958 {
2959 complaint (&symfile_complaints,
2960 _("missing `.debug_types' section for DW_FORM_sig8 die"));
2961 return 0;
2962 }
2963
2964 find_entry.signature = sig;
2965 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
2966 return entry;
2967 }
2968
2969 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
2970
2971 static void
2972 init_cu_die_reader (struct die_reader_specs *reader,
2973 struct dwarf2_cu *cu)
2974 {
2975 reader->abfd = cu->objfile->obfd;
2976 reader->cu = cu;
2977 if (cu->per_cu->from_debug_types)
2978 {
2979 gdb_assert (dwarf2_per_objfile->types.readin);
2980 reader->buffer = dwarf2_per_objfile->types.buffer;
2981 }
2982 else
2983 {
2984 gdb_assert (dwarf2_per_objfile->info.readin);
2985 reader->buffer = dwarf2_per_objfile->info.buffer;
2986 }
2987 }
2988
2989 /* Find the base address of the compilation unit for range lists and
2990 location lists. It will normally be specified by DW_AT_low_pc.
2991 In DWARF-3 draft 4, the base address could be overridden by
2992 DW_AT_entry_pc. It's been removed, but GCC still uses this for
2993 compilation units with discontinuous ranges. */
2994
2995 static void
2996 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
2997 {
2998 struct attribute *attr;
2999
3000 cu->base_known = 0;
3001 cu->base_address = 0;
3002
3003 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3004 if (attr)
3005 {
3006 cu->base_address = DW_ADDR (attr);
3007 cu->base_known = 1;
3008 }
3009 else
3010 {
3011 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3012 if (attr)
3013 {
3014 cu->base_address = DW_ADDR (attr);
3015 cu->base_known = 1;
3016 }
3017 }
3018 }
3019
3020 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3021 to combine the common parts.
3022 Process a compilation unit for a psymtab.
3023 BUFFER is a pointer to the beginning of the dwarf section buffer,
3024 either .debug_info or debug_types.
3025 INFO_PTR is a pointer to the start of the CU.
3026 Returns a pointer to the next CU. */
3027
3028 static gdb_byte *
3029 process_psymtab_comp_unit (struct objfile *objfile,
3030 struct dwarf2_per_cu_data *this_cu,
3031 gdb_byte *buffer, gdb_byte *info_ptr,
3032 unsigned int buffer_size)
3033 {
3034 bfd *abfd = objfile->obfd;
3035 gdb_byte *beg_of_comp_unit = info_ptr;
3036 struct die_info *comp_unit_die;
3037 struct partial_symtab *pst;
3038 CORE_ADDR baseaddr;
3039 struct cleanup *back_to_inner;
3040 struct dwarf2_cu cu;
3041 int has_children, has_pc_info;
3042 struct attribute *attr;
3043 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3044 struct die_reader_specs reader_specs;
3045
3046 init_one_comp_unit (&cu, objfile);
3047 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3048
3049 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3050 buffer, buffer_size,
3051 abfd);
3052
3053 /* Complete the cu_header. */
3054 cu.header.offset = beg_of_comp_unit - buffer;
3055 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3056
3057 cu.list_in_scope = &file_symbols;
3058
3059 /* If this compilation unit was already read in, free the
3060 cached copy in order to read it in again. This is
3061 necessary because we skipped some symbols when we first
3062 read in the compilation unit (see load_partial_dies).
3063 This problem could be avoided, but the benefit is
3064 unclear. */
3065 if (this_cu->cu != NULL)
3066 free_one_cached_comp_unit (this_cu->cu);
3067
3068 /* Note that this is a pointer to our stack frame, being
3069 added to a global data structure. It will be cleaned up
3070 in free_stack_comp_unit when we finish with this
3071 compilation unit. */
3072 this_cu->cu = &cu;
3073 cu.per_cu = this_cu;
3074
3075 /* Read the abbrevs for this compilation unit into a table. */
3076 dwarf2_read_abbrevs (abfd, &cu);
3077 make_cleanup (dwarf2_free_abbrev_table, &cu);
3078
3079 /* Read the compilation unit die. */
3080 if (this_cu->from_debug_types)
3081 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3082 init_cu_die_reader (&reader_specs, &cu);
3083 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3084 &has_children);
3085
3086 if (this_cu->from_debug_types)
3087 {
3088 /* offset,length haven't been set yet for type units. */
3089 this_cu->offset = cu.header.offset;
3090 this_cu->length = cu.header.length + cu.header.initial_length_size;
3091 }
3092 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3093 {
3094 info_ptr = (beg_of_comp_unit + cu.header.length
3095 + cu.header.initial_length_size);
3096 do_cleanups (back_to_inner);
3097 return info_ptr;
3098 }
3099
3100 prepare_one_comp_unit (&cu, comp_unit_die);
3101
3102 /* Allocate a new partial symbol table structure. */
3103 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3104 pst = start_psymtab_common (objfile, objfile->section_offsets,
3105 (attr != NULL) ? DW_STRING (attr) : "",
3106 /* TEXTLOW and TEXTHIGH are set below. */
3107 0,
3108 objfile->global_psymbols.next,
3109 objfile->static_psymbols.next);
3110
3111 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3112 if (attr != NULL)
3113 pst->dirname = DW_STRING (attr);
3114
3115 pst->read_symtab_private = this_cu;
3116
3117 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3118
3119 /* Store the function that reads in the rest of the symbol table */
3120 pst->read_symtab = dwarf2_psymtab_to_symtab;
3121
3122 this_cu->v.psymtab = pst;
3123
3124 dwarf2_find_base_address (comp_unit_die, &cu);
3125
3126 /* Possibly set the default values of LOWPC and HIGHPC from
3127 `DW_AT_ranges'. */
3128 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3129 &best_highpc, &cu, pst);
3130 if (has_pc_info == 1 && best_lowpc < best_highpc)
3131 /* Store the contiguous range if it is not empty; it can be empty for
3132 CUs with no code. */
3133 addrmap_set_empty (objfile->psymtabs_addrmap,
3134 best_lowpc + baseaddr,
3135 best_highpc + baseaddr - 1, pst);
3136
3137 /* Check if comp unit has_children.
3138 If so, read the rest of the partial symbols from this comp unit.
3139 If not, there's no more debug_info for this comp unit. */
3140 if (has_children)
3141 {
3142 struct partial_die_info *first_die;
3143 CORE_ADDR lowpc, highpc;
3144
3145 lowpc = ((CORE_ADDR) -1);
3146 highpc = ((CORE_ADDR) 0);
3147
3148 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3149
3150 scan_partial_symbols (first_die, &lowpc, &highpc,
3151 ! has_pc_info, &cu);
3152
3153 /* If we didn't find a lowpc, set it to highpc to avoid
3154 complaints from `maint check'. */
3155 if (lowpc == ((CORE_ADDR) -1))
3156 lowpc = highpc;
3157
3158 /* If the compilation unit didn't have an explicit address range,
3159 then use the information extracted from its child dies. */
3160 if (! has_pc_info)
3161 {
3162 best_lowpc = lowpc;
3163 best_highpc = highpc;
3164 }
3165 }
3166 pst->textlow = best_lowpc + baseaddr;
3167 pst->texthigh = best_highpc + baseaddr;
3168
3169 pst->n_global_syms = objfile->global_psymbols.next -
3170 (objfile->global_psymbols.list + pst->globals_offset);
3171 pst->n_static_syms = objfile->static_psymbols.next -
3172 (objfile->static_psymbols.list + pst->statics_offset);
3173 sort_pst_symbols (pst);
3174
3175 info_ptr = (beg_of_comp_unit + cu.header.length
3176 + cu.header.initial_length_size);
3177
3178 if (this_cu->from_debug_types)
3179 {
3180 /* It's not clear we want to do anything with stmt lists here.
3181 Waiting to see what gcc ultimately does. */
3182 }
3183 else
3184 {
3185 /* Get the list of files included in the current compilation unit,
3186 and build a psymtab for each of them. */
3187 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3188 }
3189
3190 do_cleanups (back_to_inner);
3191
3192 return info_ptr;
3193 }
3194
3195 /* Traversal function for htab_traverse_noresize.
3196 Process one .debug_types comp-unit. */
3197
3198 static int
3199 process_type_comp_unit (void **slot, void *info)
3200 {
3201 struct signatured_type *entry = (struct signatured_type *) *slot;
3202 struct objfile *objfile = (struct objfile *) info;
3203 struct dwarf2_per_cu_data *this_cu;
3204
3205 this_cu = &entry->per_cu;
3206
3207 gdb_assert (dwarf2_per_objfile->types.readin);
3208 process_psymtab_comp_unit (objfile, this_cu,
3209 dwarf2_per_objfile->types.buffer,
3210 dwarf2_per_objfile->types.buffer + entry->offset,
3211 dwarf2_per_objfile->types.size);
3212
3213 return 1;
3214 }
3215
3216 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3217 Build partial symbol tables for the .debug_types comp-units. */
3218
3219 static void
3220 build_type_psymtabs (struct objfile *objfile)
3221 {
3222 if (! create_debug_types_hash_table (objfile))
3223 return;
3224
3225 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3226 process_type_comp_unit, objfile);
3227 }
3228
3229 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3230
3231 static void
3232 psymtabs_addrmap_cleanup (void *o)
3233 {
3234 struct objfile *objfile = o;
3235
3236 objfile->psymtabs_addrmap = NULL;
3237 }
3238
3239 /* Build the partial symbol table by doing a quick pass through the
3240 .debug_info and .debug_abbrev sections. */
3241
3242 static void
3243 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3244 {
3245 gdb_byte *info_ptr;
3246 struct cleanup *back_to, *addrmap_cleanup;
3247 struct obstack temp_obstack;
3248
3249 dwarf2_per_objfile->reading_partial_symbols = 1;
3250
3251 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3252 info_ptr = dwarf2_per_objfile->info.buffer;
3253
3254 /* Any cached compilation units will be linked by the per-objfile
3255 read_in_chain. Make sure to free them when we're done. */
3256 back_to = make_cleanup (free_cached_comp_units, NULL);
3257
3258 build_type_psymtabs (objfile);
3259
3260 create_all_comp_units (objfile);
3261
3262 /* Create a temporary address map on a temporary obstack. We later
3263 copy this to the final obstack. */
3264 obstack_init (&temp_obstack);
3265 make_cleanup_obstack_free (&temp_obstack);
3266 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3267 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3268
3269 /* Since the objects we're extracting from .debug_info vary in
3270 length, only the individual functions to extract them (like
3271 read_comp_unit_head and load_partial_die) can really know whether
3272 the buffer is large enough to hold another complete object.
3273
3274 At the moment, they don't actually check that. If .debug_info
3275 holds just one extra byte after the last compilation unit's dies,
3276 then read_comp_unit_head will happily read off the end of the
3277 buffer. read_partial_die is similarly casual. Those functions
3278 should be fixed.
3279
3280 For this loop condition, simply checking whether there's any data
3281 left at all should be sufficient. */
3282
3283 while (info_ptr < (dwarf2_per_objfile->info.buffer
3284 + dwarf2_per_objfile->info.size))
3285 {
3286 struct dwarf2_per_cu_data *this_cu;
3287
3288 this_cu = dwarf2_find_comp_unit (info_ptr - dwarf2_per_objfile->info.buffer,
3289 objfile);
3290
3291 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3292 dwarf2_per_objfile->info.buffer,
3293 info_ptr,
3294 dwarf2_per_objfile->info.size);
3295 }
3296
3297 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3298 &objfile->objfile_obstack);
3299 discard_cleanups (addrmap_cleanup);
3300
3301 do_cleanups (back_to);
3302 }
3303
3304 /* Load the partial DIEs for a secondary CU into memory. */
3305
3306 static void
3307 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3308 struct objfile *objfile)
3309 {
3310 bfd *abfd = objfile->obfd;
3311 gdb_byte *info_ptr, *beg_of_comp_unit;
3312 struct die_info *comp_unit_die;
3313 struct dwarf2_cu *cu;
3314 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3315 int has_children;
3316 struct die_reader_specs reader_specs;
3317 int read_cu = 0;
3318
3319 gdb_assert (! this_cu->from_debug_types);
3320
3321 gdb_assert (dwarf2_per_objfile->info.readin);
3322 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3323 beg_of_comp_unit = info_ptr;
3324
3325 if (this_cu->cu == NULL)
3326 {
3327 cu = xmalloc (sizeof (*cu));
3328 init_one_comp_unit (cu, objfile);
3329
3330 read_cu = 1;
3331
3332 /* If an error occurs while loading, release our storage. */
3333 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3334
3335 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3336 dwarf2_per_objfile->info.buffer,
3337 dwarf2_per_objfile->info.size,
3338 abfd);
3339
3340 /* Complete the cu_header. */
3341 cu->header.offset = this_cu->offset;
3342 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3343
3344 /* Link this compilation unit into the compilation unit tree. */
3345 this_cu->cu = cu;
3346 cu->per_cu = this_cu;
3347
3348 /* Link this CU into read_in_chain. */
3349 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3350 dwarf2_per_objfile->read_in_chain = this_cu;
3351 }
3352 else
3353 {
3354 cu = this_cu->cu;
3355 info_ptr += cu->header.first_die_offset;
3356 }
3357
3358 /* Read the abbrevs for this compilation unit into a table. */
3359 gdb_assert (cu->dwarf2_abbrevs == NULL);
3360 dwarf2_read_abbrevs (abfd, cu);
3361 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3362
3363 /* Read the compilation unit die. */
3364 init_cu_die_reader (&reader_specs, cu);
3365 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3366 &has_children);
3367
3368 prepare_one_comp_unit (cu, comp_unit_die);
3369
3370 /* Check if comp unit has_children.
3371 If so, read the rest of the partial symbols from this comp unit.
3372 If not, there's no more debug_info for this comp unit. */
3373 if (has_children)
3374 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3375
3376 do_cleanups (free_abbrevs_cleanup);
3377
3378 if (read_cu)
3379 {
3380 /* We've successfully allocated this compilation unit. Let our
3381 caller clean it up when finished with it. */
3382 discard_cleanups (free_cu_cleanup);
3383 }
3384 }
3385
3386 /* Create a list of all compilation units in OBJFILE. We do this only
3387 if an inter-comp-unit reference is found; presumably if there is one,
3388 there will be many, and one will occur early in the .debug_info section.
3389 So there's no point in building this list incrementally. */
3390
3391 static void
3392 create_all_comp_units (struct objfile *objfile)
3393 {
3394 int n_allocated;
3395 int n_comp_units;
3396 struct dwarf2_per_cu_data **all_comp_units;
3397 gdb_byte *info_ptr;
3398
3399 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3400 info_ptr = dwarf2_per_objfile->info.buffer;
3401
3402 n_comp_units = 0;
3403 n_allocated = 10;
3404 all_comp_units = xmalloc (n_allocated
3405 * sizeof (struct dwarf2_per_cu_data *));
3406
3407 while (info_ptr < dwarf2_per_objfile->info.buffer + dwarf2_per_objfile->info.size)
3408 {
3409 unsigned int length, initial_length_size;
3410 struct dwarf2_per_cu_data *this_cu;
3411 unsigned int offset;
3412
3413 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3414
3415 /* Read just enough information to find out where the next
3416 compilation unit is. */
3417 length = read_initial_length (objfile->obfd, info_ptr,
3418 &initial_length_size);
3419
3420 /* Save the compilation unit for later lookup. */
3421 this_cu = obstack_alloc (&objfile->objfile_obstack,
3422 sizeof (struct dwarf2_per_cu_data));
3423 memset (this_cu, 0, sizeof (*this_cu));
3424 this_cu->offset = offset;
3425 this_cu->length = length + initial_length_size;
3426 this_cu->objfile = objfile;
3427
3428 if (n_comp_units == n_allocated)
3429 {
3430 n_allocated *= 2;
3431 all_comp_units = xrealloc (all_comp_units,
3432 n_allocated
3433 * sizeof (struct dwarf2_per_cu_data *));
3434 }
3435 all_comp_units[n_comp_units++] = this_cu;
3436
3437 info_ptr = info_ptr + this_cu->length;
3438 }
3439
3440 dwarf2_per_objfile->all_comp_units
3441 = obstack_alloc (&objfile->objfile_obstack,
3442 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3443 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3444 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3445 xfree (all_comp_units);
3446 dwarf2_per_objfile->n_comp_units = n_comp_units;
3447 }
3448
3449 /* Process all loaded DIEs for compilation unit CU, starting at
3450 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3451 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3452 DW_AT_ranges). If NEED_PC is set, then this function will set
3453 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3454 and record the covered ranges in the addrmap. */
3455
3456 static void
3457 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3458 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3459 {
3460 struct partial_die_info *pdi;
3461
3462 /* Now, march along the PDI's, descending into ones which have
3463 interesting children but skipping the children of the other ones,
3464 until we reach the end of the compilation unit. */
3465
3466 pdi = first_die;
3467
3468 while (pdi != NULL)
3469 {
3470 fixup_partial_die (pdi, cu);
3471
3472 /* Anonymous namespaces or modules have no name but have interesting
3473 children, so we need to look at them. Ditto for anonymous
3474 enums. */
3475
3476 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3477 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3478 {
3479 switch (pdi->tag)
3480 {
3481 case DW_TAG_subprogram:
3482 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3483 break;
3484 case DW_TAG_constant:
3485 case DW_TAG_variable:
3486 case DW_TAG_typedef:
3487 case DW_TAG_union_type:
3488 if (!pdi->is_declaration)
3489 {
3490 add_partial_symbol (pdi, cu);
3491 }
3492 break;
3493 case DW_TAG_class_type:
3494 case DW_TAG_interface_type:
3495 case DW_TAG_structure_type:
3496 if (!pdi->is_declaration)
3497 {
3498 add_partial_symbol (pdi, cu);
3499 }
3500 break;
3501 case DW_TAG_enumeration_type:
3502 if (!pdi->is_declaration)
3503 add_partial_enumeration (pdi, cu);
3504 break;
3505 case DW_TAG_base_type:
3506 case DW_TAG_subrange_type:
3507 /* File scope base type definitions are added to the partial
3508 symbol table. */
3509 add_partial_symbol (pdi, cu);
3510 break;
3511 case DW_TAG_namespace:
3512 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3513 break;
3514 case DW_TAG_module:
3515 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3516 break;
3517 default:
3518 break;
3519 }
3520 }
3521
3522 /* If the die has a sibling, skip to the sibling. */
3523
3524 pdi = pdi->die_sibling;
3525 }
3526 }
3527
3528 /* Functions used to compute the fully scoped name of a partial DIE.
3529
3530 Normally, this is simple. For C++, the parent DIE's fully scoped
3531 name is concatenated with "::" and the partial DIE's name. For
3532 Java, the same thing occurs except that "." is used instead of "::".
3533 Enumerators are an exception; they use the scope of their parent
3534 enumeration type, i.e. the name of the enumeration type is not
3535 prepended to the enumerator.
3536
3537 There are two complexities. One is DW_AT_specification; in this
3538 case "parent" means the parent of the target of the specification,
3539 instead of the direct parent of the DIE. The other is compilers
3540 which do not emit DW_TAG_namespace; in this case we try to guess
3541 the fully qualified name of structure types from their members'
3542 linkage names. This must be done using the DIE's children rather
3543 than the children of any DW_AT_specification target. We only need
3544 to do this for structures at the top level, i.e. if the target of
3545 any DW_AT_specification (if any; otherwise the DIE itself) does not
3546 have a parent. */
3547
3548 /* Compute the scope prefix associated with PDI's parent, in
3549 compilation unit CU. The result will be allocated on CU's
3550 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3551 field. NULL is returned if no prefix is necessary. */
3552 static char *
3553 partial_die_parent_scope (struct partial_die_info *pdi,
3554 struct dwarf2_cu *cu)
3555 {
3556 char *grandparent_scope;
3557 struct partial_die_info *parent, *real_pdi;
3558
3559 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3560 then this means the parent of the specification DIE. */
3561
3562 real_pdi = pdi;
3563 while (real_pdi->has_specification)
3564 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3565
3566 parent = real_pdi->die_parent;
3567 if (parent == NULL)
3568 return NULL;
3569
3570 if (parent->scope_set)
3571 return parent->scope;
3572
3573 fixup_partial_die (parent, cu);
3574
3575 grandparent_scope = partial_die_parent_scope (parent, cu);
3576
3577 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3578 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3579 Work around this problem here. */
3580 if (cu->language == language_cplus
3581 && parent->tag == DW_TAG_namespace
3582 && strcmp (parent->name, "::") == 0
3583 && grandparent_scope == NULL)
3584 {
3585 parent->scope = NULL;
3586 parent->scope_set = 1;
3587 return NULL;
3588 }
3589
3590 if (parent->tag == DW_TAG_namespace
3591 || parent->tag == DW_TAG_module
3592 || parent->tag == DW_TAG_structure_type
3593 || parent->tag == DW_TAG_class_type
3594 || parent->tag == DW_TAG_interface_type
3595 || parent->tag == DW_TAG_union_type
3596 || parent->tag == DW_TAG_enumeration_type)
3597 {
3598 if (grandparent_scope == NULL)
3599 parent->scope = parent->name;
3600 else
3601 parent->scope = typename_concat (&cu->comp_unit_obstack, grandparent_scope,
3602 parent->name, 0, cu);
3603 }
3604 else if (parent->tag == DW_TAG_enumerator)
3605 /* Enumerators should not get the name of the enumeration as a prefix. */
3606 parent->scope = grandparent_scope;
3607 else
3608 {
3609 /* FIXME drow/2004-04-01: What should we be doing with
3610 function-local names? For partial symbols, we should probably be
3611 ignoring them. */
3612 complaint (&symfile_complaints,
3613 _("unhandled containing DIE tag %d for DIE at %d"),
3614 parent->tag, pdi->offset);
3615 parent->scope = grandparent_scope;
3616 }
3617
3618 parent->scope_set = 1;
3619 return parent->scope;
3620 }
3621
3622 /* Return the fully scoped name associated with PDI, from compilation unit
3623 CU. The result will be allocated with malloc. */
3624 static char *
3625 partial_die_full_name (struct partial_die_info *pdi,
3626 struct dwarf2_cu *cu)
3627 {
3628 char *parent_scope;
3629
3630 /* If this is a template instantiation, we can not work out the
3631 template arguments from partial DIEs. So, unfortunately, we have
3632 to go through the full DIEs. At least any work we do building
3633 types here will be reused if full symbols are loaded later. */
3634 if (pdi->has_template_arguments)
3635 {
3636 fixup_partial_die (pdi, cu);
3637
3638 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3639 {
3640 struct die_info *die;
3641 struct attribute attr;
3642 struct dwarf2_cu *ref_cu = cu;
3643
3644 attr.name = 0;
3645 attr.form = DW_FORM_ref_addr;
3646 attr.u.addr = pdi->offset;
3647 die = follow_die_ref (NULL, &attr, &ref_cu);
3648
3649 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3650 }
3651 }
3652
3653 parent_scope = partial_die_parent_scope (pdi, cu);
3654 if (parent_scope == NULL)
3655 return NULL;
3656 else
3657 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3658 }
3659
3660 static void
3661 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3662 {
3663 struct objfile *objfile = cu->objfile;
3664 CORE_ADDR addr = 0;
3665 char *actual_name = NULL;
3666 const struct partial_symbol *psym = NULL;
3667 CORE_ADDR baseaddr;
3668 int built_actual_name = 0;
3669
3670 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3671
3672 actual_name = partial_die_full_name (pdi, cu);
3673 if (actual_name)
3674 built_actual_name = 1;
3675
3676 if (actual_name == NULL)
3677 actual_name = pdi->name;
3678
3679 switch (pdi->tag)
3680 {
3681 case DW_TAG_subprogram:
3682 if (pdi->is_external || cu->language == language_ada)
3683 {
3684 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3685 of the global scope. But in Ada, we want to be able to access
3686 nested procedures globally. So all Ada subprograms are stored
3687 in the global scope. */
3688 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3689 mst_text, objfile); */
3690 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3691 built_actual_name,
3692 VAR_DOMAIN, LOC_BLOCK,
3693 &objfile->global_psymbols,
3694 0, pdi->lowpc + baseaddr,
3695 cu->language, objfile);
3696 }
3697 else
3698 {
3699 /*prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3700 mst_file_text, objfile); */
3701 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3702 built_actual_name,
3703 VAR_DOMAIN, LOC_BLOCK,
3704 &objfile->static_psymbols,
3705 0, pdi->lowpc + baseaddr,
3706 cu->language, objfile);
3707 }
3708 break;
3709 case DW_TAG_constant:
3710 {
3711 struct psymbol_allocation_list *list;
3712
3713 if (pdi->is_external)
3714 list = &objfile->global_psymbols;
3715 else
3716 list = &objfile->static_psymbols;
3717 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3718 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3719 list, 0, 0, cu->language, objfile);
3720
3721 }
3722 break;
3723 case DW_TAG_variable:
3724 if (pdi->locdesc)
3725 addr = decode_locdesc (pdi->locdesc, cu);
3726
3727 if (pdi->locdesc
3728 && addr == 0
3729 && !dwarf2_per_objfile->has_section_at_zero)
3730 {
3731 /* A global or static variable may also have been stripped
3732 out by the linker if unused, in which case its address
3733 will be nullified; do not add such variables into partial
3734 symbol table then. */
3735 }
3736 else if (pdi->is_external)
3737 {
3738 /* Global Variable.
3739 Don't enter into the minimal symbol tables as there is
3740 a minimal symbol table entry from the ELF symbols already.
3741 Enter into partial symbol table if it has a location
3742 descriptor or a type.
3743 If the location descriptor is missing, new_symbol will create
3744 a LOC_UNRESOLVED symbol, the address of the variable will then
3745 be determined from the minimal symbol table whenever the variable
3746 is referenced.
3747 The address for the partial symbol table entry is not
3748 used by GDB, but it comes in handy for debugging partial symbol
3749 table building. */
3750
3751 if (pdi->locdesc || pdi->has_type)
3752 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3753 built_actual_name,
3754 VAR_DOMAIN, LOC_STATIC,
3755 &objfile->global_psymbols,
3756 0, addr + baseaddr,
3757 cu->language, objfile);
3758 }
3759 else
3760 {
3761 /* Static Variable. Skip symbols without location descriptors. */
3762 if (pdi->locdesc == NULL)
3763 {
3764 if (built_actual_name)
3765 xfree (actual_name);
3766 return;
3767 }
3768 /*prim_record_minimal_symbol (actual_name, addr + baseaddr,
3769 mst_file_data, objfile); */
3770 psym = add_psymbol_to_list (actual_name, strlen (actual_name),
3771 built_actual_name,
3772 VAR_DOMAIN, LOC_STATIC,
3773 &objfile->static_psymbols,
3774 0, addr + baseaddr,
3775 cu->language, objfile);
3776 }
3777 break;
3778 case DW_TAG_typedef:
3779 case DW_TAG_base_type:
3780 case DW_TAG_subrange_type:
3781 add_psymbol_to_list (actual_name, strlen (actual_name),
3782 built_actual_name,
3783 VAR_DOMAIN, LOC_TYPEDEF,
3784 &objfile->static_psymbols,
3785 0, (CORE_ADDR) 0, cu->language, objfile);
3786 break;
3787 case DW_TAG_namespace:
3788 add_psymbol_to_list (actual_name, strlen (actual_name),
3789 built_actual_name,
3790 VAR_DOMAIN, LOC_TYPEDEF,
3791 &objfile->global_psymbols,
3792 0, (CORE_ADDR) 0, cu->language, objfile);
3793 break;
3794 case DW_TAG_class_type:
3795 case DW_TAG_interface_type:
3796 case DW_TAG_structure_type:
3797 case DW_TAG_union_type:
3798 case DW_TAG_enumeration_type:
3799 /* Skip external references. The DWARF standard says in the section
3800 about "Structure, Union, and Class Type Entries": "An incomplete
3801 structure, union or class type is represented by a structure,
3802 union or class entry that does not have a byte size attribute
3803 and that has a DW_AT_declaration attribute." */
3804 if (!pdi->has_byte_size && pdi->is_declaration)
3805 {
3806 if (built_actual_name)
3807 xfree (actual_name);
3808 return;
3809 }
3810
3811 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
3812 static vs. global. */
3813 add_psymbol_to_list (actual_name, strlen (actual_name),
3814 built_actual_name,
3815 STRUCT_DOMAIN, LOC_TYPEDEF,
3816 (cu->language == language_cplus
3817 || cu->language == language_java)
3818 ? &objfile->global_psymbols
3819 : &objfile->static_psymbols,
3820 0, (CORE_ADDR) 0, cu->language, objfile);
3821
3822 break;
3823 case DW_TAG_enumerator:
3824 add_psymbol_to_list (actual_name, strlen (actual_name),
3825 built_actual_name,
3826 VAR_DOMAIN, LOC_CONST,
3827 (cu->language == language_cplus
3828 || cu->language == language_java)
3829 ? &objfile->global_psymbols
3830 : &objfile->static_psymbols,
3831 0, (CORE_ADDR) 0, cu->language, objfile);
3832 break;
3833 default:
3834 break;
3835 }
3836
3837 if (built_actual_name)
3838 xfree (actual_name);
3839 }
3840
3841 /* Read a partial die corresponding to a namespace; also, add a symbol
3842 corresponding to that namespace to the symbol table. NAMESPACE is
3843 the name of the enclosing namespace. */
3844
3845 static void
3846 add_partial_namespace (struct partial_die_info *pdi,
3847 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3848 int need_pc, struct dwarf2_cu *cu)
3849 {
3850 /* Add a symbol for the namespace. */
3851
3852 add_partial_symbol (pdi, cu);
3853
3854 /* Now scan partial symbols in that namespace. */
3855
3856 if (pdi->has_children)
3857 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3858 }
3859
3860 /* Read a partial die corresponding to a Fortran module. */
3861
3862 static void
3863 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
3864 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3865 {
3866 /* Now scan partial symbols in that module. */
3867
3868 if (pdi->has_children)
3869 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
3870 }
3871
3872 /* Read a partial die corresponding to a subprogram and create a partial
3873 symbol for that subprogram. When the CU language allows it, this
3874 routine also defines a partial symbol for each nested subprogram
3875 that this subprogram contains.
3876
3877 DIE my also be a lexical block, in which case we simply search
3878 recursively for suprograms defined inside that lexical block.
3879 Again, this is only performed when the CU language allows this
3880 type of definitions. */
3881
3882 static void
3883 add_partial_subprogram (struct partial_die_info *pdi,
3884 CORE_ADDR *lowpc, CORE_ADDR *highpc,
3885 int need_pc, struct dwarf2_cu *cu)
3886 {
3887 if (pdi->tag == DW_TAG_subprogram)
3888 {
3889 if (pdi->has_pc_info)
3890 {
3891 if (pdi->lowpc < *lowpc)
3892 *lowpc = pdi->lowpc;
3893 if (pdi->highpc > *highpc)
3894 *highpc = pdi->highpc;
3895 if (need_pc)
3896 {
3897 CORE_ADDR baseaddr;
3898 struct objfile *objfile = cu->objfile;
3899
3900 baseaddr = ANOFFSET (objfile->section_offsets,
3901 SECT_OFF_TEXT (objfile));
3902 addrmap_set_empty (objfile->psymtabs_addrmap,
3903 pdi->lowpc + baseaddr,
3904 pdi->highpc - 1 + baseaddr,
3905 cu->per_cu->v.psymtab);
3906 }
3907 if (!pdi->is_declaration)
3908 /* Ignore subprogram DIEs that do not have a name, they are
3909 illegal. Do not emit a complaint at this point, we will
3910 do so when we convert this psymtab into a symtab. */
3911 if (pdi->name)
3912 add_partial_symbol (pdi, cu);
3913 }
3914 }
3915
3916 if (! pdi->has_children)
3917 return;
3918
3919 if (cu->language == language_ada)
3920 {
3921 pdi = pdi->die_child;
3922 while (pdi != NULL)
3923 {
3924 fixup_partial_die (pdi, cu);
3925 if (pdi->tag == DW_TAG_subprogram
3926 || pdi->tag == DW_TAG_lexical_block)
3927 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3928 pdi = pdi->die_sibling;
3929 }
3930 }
3931 }
3932
3933 /* Read a partial die corresponding to an enumeration type. */
3934
3935 static void
3936 add_partial_enumeration (struct partial_die_info *enum_pdi,
3937 struct dwarf2_cu *cu)
3938 {
3939 struct partial_die_info *pdi;
3940
3941 if (enum_pdi->name != NULL)
3942 add_partial_symbol (enum_pdi, cu);
3943
3944 pdi = enum_pdi->die_child;
3945 while (pdi)
3946 {
3947 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
3948 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
3949 else
3950 add_partial_symbol (pdi, cu);
3951 pdi = pdi->die_sibling;
3952 }
3953 }
3954
3955 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
3956 Return the corresponding abbrev, or NULL if the number is zero (indicating
3957 an empty DIE). In either case *BYTES_READ will be set to the length of
3958 the initial number. */
3959
3960 static struct abbrev_info *
3961 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
3962 struct dwarf2_cu *cu)
3963 {
3964 bfd *abfd = cu->objfile->obfd;
3965 unsigned int abbrev_number;
3966 struct abbrev_info *abbrev;
3967
3968 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
3969
3970 if (abbrev_number == 0)
3971 return NULL;
3972
3973 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
3974 if (!abbrev)
3975 {
3976 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"), abbrev_number,
3977 bfd_get_filename (abfd));
3978 }
3979
3980 return abbrev;
3981 }
3982
3983 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
3984 Returns a pointer to the end of a series of DIEs, terminated by an empty
3985 DIE. Any children of the skipped DIEs will also be skipped. */
3986
3987 static gdb_byte *
3988 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
3989 {
3990 struct abbrev_info *abbrev;
3991 unsigned int bytes_read;
3992
3993 while (1)
3994 {
3995 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
3996 if (abbrev == NULL)
3997 return info_ptr + bytes_read;
3998 else
3999 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4000 }
4001 }
4002
4003 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4004 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4005 abbrev corresponding to that skipped uleb128 should be passed in
4006 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4007 children. */
4008
4009 static gdb_byte *
4010 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4011 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4012 {
4013 unsigned int bytes_read;
4014 struct attribute attr;
4015 bfd *abfd = cu->objfile->obfd;
4016 unsigned int form, i;
4017
4018 for (i = 0; i < abbrev->num_attrs; i++)
4019 {
4020 /* The only abbrev we care about is DW_AT_sibling. */
4021 if (abbrev->attrs[i].name == DW_AT_sibling)
4022 {
4023 read_attribute (&attr, &abbrev->attrs[i],
4024 abfd, info_ptr, cu);
4025 if (attr.form == DW_FORM_ref_addr)
4026 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
4027 else
4028 return buffer + dwarf2_get_ref_die_offset (&attr);
4029 }
4030
4031 /* If it isn't DW_AT_sibling, skip this attribute. */
4032 form = abbrev->attrs[i].form;
4033 skip_attribute:
4034 switch (form)
4035 {
4036 case DW_FORM_ref_addr:
4037 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4038 and later it is offset sized. */
4039 if (cu->header.version == 2)
4040 info_ptr += cu->header.addr_size;
4041 else
4042 info_ptr += cu->header.offset_size;
4043 break;
4044 case DW_FORM_addr:
4045 info_ptr += cu->header.addr_size;
4046 break;
4047 case DW_FORM_data1:
4048 case DW_FORM_ref1:
4049 case DW_FORM_flag:
4050 info_ptr += 1;
4051 break;
4052 case DW_FORM_flag_present:
4053 break;
4054 case DW_FORM_data2:
4055 case DW_FORM_ref2:
4056 info_ptr += 2;
4057 break;
4058 case DW_FORM_data4:
4059 case DW_FORM_ref4:
4060 info_ptr += 4;
4061 break;
4062 case DW_FORM_data8:
4063 case DW_FORM_ref8:
4064 case DW_FORM_sig8:
4065 info_ptr += 8;
4066 break;
4067 case DW_FORM_string:
4068 read_direct_string (abfd, info_ptr, &bytes_read);
4069 info_ptr += bytes_read;
4070 break;
4071 case DW_FORM_sec_offset:
4072 case DW_FORM_strp:
4073 info_ptr += cu->header.offset_size;
4074 break;
4075 case DW_FORM_exprloc:
4076 case DW_FORM_block:
4077 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4078 info_ptr += bytes_read;
4079 break;
4080 case DW_FORM_block1:
4081 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4082 break;
4083 case DW_FORM_block2:
4084 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4085 break;
4086 case DW_FORM_block4:
4087 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4088 break;
4089 case DW_FORM_sdata:
4090 case DW_FORM_udata:
4091 case DW_FORM_ref_udata:
4092 info_ptr = skip_leb128 (abfd, info_ptr);
4093 break;
4094 case DW_FORM_indirect:
4095 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4096 info_ptr += bytes_read;
4097 /* We need to continue parsing from here, so just go back to
4098 the top. */
4099 goto skip_attribute;
4100
4101 default:
4102 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
4103 dwarf_form_name (form),
4104 bfd_get_filename (abfd));
4105 }
4106 }
4107
4108 if (abbrev->has_children)
4109 return skip_children (buffer, info_ptr, cu);
4110 else
4111 return info_ptr;
4112 }
4113
4114 /* Locate ORIG_PDI's sibling.
4115 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4116 in BUFFER. */
4117
4118 static gdb_byte *
4119 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4120 gdb_byte *buffer, gdb_byte *info_ptr,
4121 bfd *abfd, struct dwarf2_cu *cu)
4122 {
4123 /* Do we know the sibling already? */
4124
4125 if (orig_pdi->sibling)
4126 return orig_pdi->sibling;
4127
4128 /* Are there any children to deal with? */
4129
4130 if (!orig_pdi->has_children)
4131 return info_ptr;
4132
4133 /* Skip the children the long way. */
4134
4135 return skip_children (buffer, info_ptr, cu);
4136 }
4137
4138 /* Expand this partial symbol table into a full symbol table. */
4139
4140 static void
4141 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4142 {
4143 if (pst != NULL)
4144 {
4145 if (pst->readin)
4146 {
4147 warning (_("bug: psymtab for %s is already read in."), pst->filename);
4148 }
4149 else
4150 {
4151 if (info_verbose)
4152 {
4153 printf_filtered (_("Reading in symbols for %s..."), pst->filename);
4154 gdb_flush (gdb_stdout);
4155 }
4156
4157 /* Restore our global data. */
4158 dwarf2_per_objfile = objfile_data (pst->objfile,
4159 dwarf2_objfile_data_key);
4160
4161 /* If this psymtab is constructed from a debug-only objfile, the
4162 has_section_at_zero flag will not necessarily be correct. We
4163 can get the correct value for this flag by looking at the data
4164 associated with the (presumably stripped) associated objfile. */
4165 if (pst->objfile->separate_debug_objfile_backlink)
4166 {
4167 struct dwarf2_per_objfile *dpo_backlink
4168 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4169 dwarf2_objfile_data_key);
4170
4171 dwarf2_per_objfile->has_section_at_zero
4172 = dpo_backlink->has_section_at_zero;
4173 }
4174
4175 dwarf2_per_objfile->reading_partial_symbols = 0;
4176
4177 psymtab_to_symtab_1 (pst);
4178
4179 /* Finish up the debug error message. */
4180 if (info_verbose)
4181 printf_filtered (_("done.\n"));
4182 }
4183 }
4184 }
4185
4186 /* Add PER_CU to the queue. */
4187
4188 static void
4189 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4190 {
4191 struct dwarf2_queue_item *item;
4192
4193 per_cu->queued = 1;
4194 item = xmalloc (sizeof (*item));
4195 item->per_cu = per_cu;
4196 item->next = NULL;
4197
4198 if (dwarf2_queue == NULL)
4199 dwarf2_queue = item;
4200 else
4201 dwarf2_queue_tail->next = item;
4202
4203 dwarf2_queue_tail = item;
4204 }
4205
4206 /* Process the queue. */
4207
4208 static void
4209 process_queue (struct objfile *objfile)
4210 {
4211 struct dwarf2_queue_item *item, *next_item;
4212
4213 /* The queue starts out with one item, but following a DIE reference
4214 may load a new CU, adding it to the end of the queue. */
4215 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4216 {
4217 if (dwarf2_per_objfile->using_index
4218 ? !item->per_cu->v.quick->symtab
4219 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4220 process_full_comp_unit (item->per_cu);
4221
4222 item->per_cu->queued = 0;
4223 next_item = item->next;
4224 xfree (item);
4225 }
4226
4227 dwarf2_queue_tail = NULL;
4228 }
4229
4230 /* Free all allocated queue entries. This function only releases anything if
4231 an error was thrown; if the queue was processed then it would have been
4232 freed as we went along. */
4233
4234 static void
4235 dwarf2_release_queue (void *dummy)
4236 {
4237 struct dwarf2_queue_item *item, *last;
4238
4239 item = dwarf2_queue;
4240 while (item)
4241 {
4242 /* Anything still marked queued is likely to be in an
4243 inconsistent state, so discard it. */
4244 if (item->per_cu->queued)
4245 {
4246 if (item->per_cu->cu != NULL)
4247 free_one_cached_comp_unit (item->per_cu->cu);
4248 item->per_cu->queued = 0;
4249 }
4250
4251 last = item;
4252 item = item->next;
4253 xfree (last);
4254 }
4255
4256 dwarf2_queue = dwarf2_queue_tail = NULL;
4257 }
4258
4259 /* Read in full symbols for PST, and anything it depends on. */
4260
4261 static void
4262 psymtab_to_symtab_1 (struct partial_symtab *pst)
4263 {
4264 struct dwarf2_per_cu_data *per_cu;
4265 struct cleanup *back_to;
4266 int i;
4267
4268 for (i = 0; i < pst->number_of_dependencies; i++)
4269 if (!pst->dependencies[i]->readin)
4270 {
4271 /* Inform about additional files that need to be read in. */
4272 if (info_verbose)
4273 {
4274 /* FIXME: i18n: Need to make this a single string. */
4275 fputs_filtered (" ", gdb_stdout);
4276 wrap_here ("");
4277 fputs_filtered ("and ", gdb_stdout);
4278 wrap_here ("");
4279 printf_filtered ("%s...", pst->dependencies[i]->filename);
4280 wrap_here (""); /* Flush output */
4281 gdb_flush (gdb_stdout);
4282 }
4283 psymtab_to_symtab_1 (pst->dependencies[i]);
4284 }
4285
4286 per_cu = pst->read_symtab_private;
4287
4288 if (per_cu == NULL)
4289 {
4290 /* It's an include file, no symbols to read for it.
4291 Everything is in the parent symtab. */
4292 pst->readin = 1;
4293 return;
4294 }
4295
4296 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4297 }
4298
4299 /* Load the DIEs associated with PER_CU into memory. */
4300
4301 static void
4302 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4303 {
4304 bfd *abfd = objfile->obfd;
4305 struct dwarf2_cu *cu;
4306 unsigned int offset;
4307 gdb_byte *info_ptr, *beg_of_comp_unit;
4308 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4309 struct attribute *attr;
4310 int read_cu = 0;
4311
4312 gdb_assert (! per_cu->from_debug_types);
4313
4314 /* Set local variables from the partial symbol table info. */
4315 offset = per_cu->offset;
4316
4317 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4318 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4319 beg_of_comp_unit = info_ptr;
4320
4321 if (per_cu->cu == NULL)
4322 {
4323 cu = xmalloc (sizeof (*cu));
4324 init_one_comp_unit (cu, objfile);
4325
4326 read_cu = 1;
4327
4328 /* If an error occurs while loading, release our storage. */
4329 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4330
4331 /* Read in the comp_unit header. */
4332 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4333
4334 /* Complete the cu_header. */
4335 cu->header.offset = offset;
4336 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4337
4338 /* Read the abbrevs for this compilation unit. */
4339 dwarf2_read_abbrevs (abfd, cu);
4340 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4341
4342 /* Link this compilation unit into the compilation unit tree. */
4343 per_cu->cu = cu;
4344 cu->per_cu = per_cu;
4345
4346 /* Link this CU into read_in_chain. */
4347 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4348 dwarf2_per_objfile->read_in_chain = per_cu;
4349 }
4350 else
4351 {
4352 cu = per_cu->cu;
4353 info_ptr += cu->header.first_die_offset;
4354 }
4355
4356 cu->dies = read_comp_unit (info_ptr, cu);
4357
4358 /* We try not to read any attributes in this function, because not
4359 all objfiles needed for references have been loaded yet, and symbol
4360 table processing isn't initialized. But we have to set the CU language,
4361 or we won't be able to build types correctly. */
4362 prepare_one_comp_unit (cu, cu->dies);
4363
4364 /* Similarly, if we do not read the producer, we can not apply
4365 producer-specific interpretation. */
4366 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4367 if (attr)
4368 cu->producer = DW_STRING (attr);
4369
4370 if (read_cu)
4371 {
4372 do_cleanups (free_abbrevs_cleanup);
4373
4374 /* We've successfully allocated this compilation unit. Let our
4375 caller clean it up when finished with it. */
4376 discard_cleanups (free_cu_cleanup);
4377 }
4378 }
4379
4380 /* Add a DIE to the delayed physname list. */
4381
4382 static void
4383 add_to_method_list (struct type *type, int fnfield_index, int index,
4384 const char *name, struct die_info *die,
4385 struct dwarf2_cu *cu)
4386 {
4387 struct delayed_method_info mi;
4388 mi.type = type;
4389 mi.fnfield_index = fnfield_index;
4390 mi.index = index;
4391 mi.name = name;
4392 mi.die = die;
4393 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4394 }
4395
4396 /* A cleanup for freeing the delayed method list. */
4397
4398 static void
4399 free_delayed_list (void *ptr)
4400 {
4401 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4402 if (cu->method_list != NULL)
4403 {
4404 VEC_free (delayed_method_info, cu->method_list);
4405 cu->method_list = NULL;
4406 }
4407 }
4408
4409 /* Compute the physnames of any methods on the CU's method list.
4410
4411 The computation of method physnames is delayed in order to avoid the
4412 (bad) condition that one of the method's formal parameters is of an as yet
4413 incomplete type. */
4414
4415 static void
4416 compute_delayed_physnames (struct dwarf2_cu *cu)
4417 {
4418 int i;
4419 struct delayed_method_info *mi;
4420 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4421 {
4422 char *physname;
4423 struct fn_fieldlist *fn_flp
4424 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4425 physname = (char *) dwarf2_physname ((char *) mi->name, mi->die, cu);
4426 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4427 }
4428 }
4429
4430 /* Generate full symbol information for PST and CU, whose DIEs have
4431 already been loaded into memory. */
4432
4433 static void
4434 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4435 {
4436 struct dwarf2_cu *cu = per_cu->cu;
4437 struct objfile *objfile = per_cu->objfile;
4438 CORE_ADDR lowpc, highpc;
4439 struct symtab *symtab;
4440 struct cleanup *back_to, *delayed_list_cleanup;
4441 CORE_ADDR baseaddr;
4442
4443 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4444
4445 buildsym_init ();
4446 back_to = make_cleanup (really_free_pendings, NULL);
4447 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4448
4449 cu->list_in_scope = &file_symbols;
4450
4451 dwarf2_find_base_address (cu->dies, cu);
4452
4453 /* Do line number decoding in read_file_scope () */
4454 process_die (cu->dies, cu);
4455
4456 /* Now that we have processed all the DIEs in the CU, all the types
4457 should be complete, and it should now be safe to compute all of the
4458 physnames. */
4459 compute_delayed_physnames (cu);
4460 do_cleanups (delayed_list_cleanup);
4461
4462 /* Some compilers don't define a DW_AT_high_pc attribute for the
4463 compilation unit. If the DW_AT_high_pc is missing, synthesize
4464 it, by scanning the DIE's below the compilation unit. */
4465 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4466
4467 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4468
4469 /* Set symtab language to language from DW_AT_language.
4470 If the compilation is from a C file generated by language preprocessors,
4471 do not set the language if it was already deduced by start_subfile. */
4472 if (symtab != NULL
4473 && !(cu->language == language_c && symtab->language != language_c))
4474 {
4475 symtab->language = cu->language;
4476 }
4477
4478 if (dwarf2_per_objfile->using_index)
4479 per_cu->v.quick->symtab = symtab;
4480 else
4481 {
4482 struct partial_symtab *pst = per_cu->v.psymtab;
4483 pst->symtab = symtab;
4484 pst->readin = 1;
4485 }
4486
4487 do_cleanups (back_to);
4488 }
4489
4490 /* Process a die and its children. */
4491
4492 static void
4493 process_die (struct die_info *die, struct dwarf2_cu *cu)
4494 {
4495 switch (die->tag)
4496 {
4497 case DW_TAG_padding:
4498 break;
4499 case DW_TAG_compile_unit:
4500 read_file_scope (die, cu);
4501 break;
4502 case DW_TAG_type_unit:
4503 read_type_unit_scope (die, cu);
4504 break;
4505 case DW_TAG_subprogram:
4506 case DW_TAG_inlined_subroutine:
4507 read_func_scope (die, cu);
4508 break;
4509 case DW_TAG_lexical_block:
4510 case DW_TAG_try_block:
4511 case DW_TAG_catch_block:
4512 read_lexical_block_scope (die, cu);
4513 break;
4514 case DW_TAG_class_type:
4515 case DW_TAG_interface_type:
4516 case DW_TAG_structure_type:
4517 case DW_TAG_union_type:
4518 process_structure_scope (die, cu);
4519 break;
4520 case DW_TAG_enumeration_type:
4521 process_enumeration_scope (die, cu);
4522 break;
4523
4524 /* These dies have a type, but processing them does not create
4525 a symbol or recurse to process the children. Therefore we can
4526 read them on-demand through read_type_die. */
4527 case DW_TAG_subroutine_type:
4528 case DW_TAG_set_type:
4529 case DW_TAG_array_type:
4530 case DW_TAG_pointer_type:
4531 case DW_TAG_ptr_to_member_type:
4532 case DW_TAG_reference_type:
4533 case DW_TAG_string_type:
4534 break;
4535
4536 case DW_TAG_base_type:
4537 case DW_TAG_subrange_type:
4538 case DW_TAG_typedef:
4539 /* Add a typedef symbol for the type definition, if it has a
4540 DW_AT_name. */
4541 new_symbol (die, read_type_die (die, cu), cu);
4542 break;
4543 case DW_TAG_common_block:
4544 read_common_block (die, cu);
4545 break;
4546 case DW_TAG_common_inclusion:
4547 break;
4548 case DW_TAG_namespace:
4549 processing_has_namespace_info = 1;
4550 read_namespace (die, cu);
4551 break;
4552 case DW_TAG_module:
4553 processing_has_namespace_info = 1;
4554 read_module (die, cu);
4555 break;
4556 case DW_TAG_imported_declaration:
4557 case DW_TAG_imported_module:
4558 processing_has_namespace_info = 1;
4559 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4560 || cu->language != language_fortran))
4561 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4562 dwarf_tag_name (die->tag));
4563 read_import_statement (die, cu);
4564 break;
4565 default:
4566 new_symbol (die, NULL, cu);
4567 break;
4568 }
4569 }
4570
4571 /* A helper function for dwarf2_compute_name which determines whether DIE
4572 needs to have the name of the scope prepended to the name listed in the
4573 die. */
4574
4575 static int
4576 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4577 {
4578 struct attribute *attr;
4579
4580 switch (die->tag)
4581 {
4582 case DW_TAG_namespace:
4583 case DW_TAG_typedef:
4584 case DW_TAG_class_type:
4585 case DW_TAG_interface_type:
4586 case DW_TAG_structure_type:
4587 case DW_TAG_union_type:
4588 case DW_TAG_enumeration_type:
4589 case DW_TAG_enumerator:
4590 case DW_TAG_subprogram:
4591 case DW_TAG_member:
4592 return 1;
4593
4594 case DW_TAG_variable:
4595 case DW_TAG_constant:
4596 /* We only need to prefix "globally" visible variables. These include
4597 any variable marked with DW_AT_external or any variable that
4598 lives in a namespace. [Variables in anonymous namespaces
4599 require prefixing, but they are not DW_AT_external.] */
4600
4601 if (dwarf2_attr (die, DW_AT_specification, cu))
4602 {
4603 struct dwarf2_cu *spec_cu = cu;
4604
4605 return die_needs_namespace (die_specification (die, &spec_cu),
4606 spec_cu);
4607 }
4608
4609 attr = dwarf2_attr (die, DW_AT_external, cu);
4610 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4611 && die->parent->tag != DW_TAG_module)
4612 return 0;
4613 /* A variable in a lexical block of some kind does not need a
4614 namespace, even though in C++ such variables may be external
4615 and have a mangled name. */
4616 if (die->parent->tag == DW_TAG_lexical_block
4617 || die->parent->tag == DW_TAG_try_block
4618 || die->parent->tag == DW_TAG_catch_block
4619 || die->parent->tag == DW_TAG_subprogram)
4620 return 0;
4621 return 1;
4622
4623 default:
4624 return 0;
4625 }
4626 }
4627
4628 /* Retrieve the last character from a mem_file. */
4629
4630 static void
4631 do_ui_file_peek_last (void *object, const char *buffer, long length)
4632 {
4633 char *last_char_p = (char *) object;
4634
4635 if (length > 0)
4636 *last_char_p = buffer[length - 1];
4637 }
4638
4639 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4640 compute the physname for the object, which include a method's
4641 formal parameters (C++/Java) and return type (Java).
4642
4643 For Ada, return the DIE's linkage name rather than the fully qualified
4644 name. PHYSNAME is ignored..
4645
4646 The result is allocated on the objfile_obstack and canonicalized. */
4647
4648 static const char *
4649 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4650 int physname)
4651 {
4652 if (name == NULL)
4653 name = dwarf2_name (die, cu);
4654
4655 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4656 compute it by typename_concat inside GDB. */
4657 if (cu->language == language_ada
4658 || (cu->language == language_fortran && physname))
4659 {
4660 /* For Ada unit, we prefer the linkage name over the name, as
4661 the former contains the exported name, which the user expects
4662 to be able to reference. Ideally, we want the user to be able
4663 to reference this entity using either natural or linkage name,
4664 but we haven't started looking at this enhancement yet. */
4665 struct attribute *attr;
4666
4667 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4668 if (attr == NULL)
4669 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4670 if (attr && DW_STRING (attr))
4671 return DW_STRING (attr);
4672 }
4673
4674 /* These are the only languages we know how to qualify names in. */
4675 if (name != NULL
4676 && (cu->language == language_cplus || cu->language == language_java
4677 || cu->language == language_fortran))
4678 {
4679 if (die_needs_namespace (die, cu))
4680 {
4681 long length;
4682 char *prefix;
4683 struct ui_file *buf;
4684
4685 prefix = determine_prefix (die, cu);
4686 buf = mem_fileopen ();
4687 if (*prefix != '\0')
4688 {
4689 char *prefixed_name = typename_concat (NULL, prefix, name,
4690 physname, cu);
4691
4692 fputs_unfiltered (prefixed_name, buf);
4693 xfree (prefixed_name);
4694 }
4695 else
4696 fputs_unfiltered (name ? name : "", buf);
4697
4698 /* Template parameters may be specified in the DIE's DW_AT_name, or
4699 as children with DW_TAG_template_type_param or
4700 DW_TAG_value_type_param. If the latter, add them to the name
4701 here. If the name already has template parameters, then
4702 skip this step; some versions of GCC emit both, and
4703 it is more efficient to use the pre-computed name.
4704
4705 Something to keep in mind about this process: it is very
4706 unlikely, or in some cases downright impossible, to produce
4707 something that will match the mangled name of a function.
4708 If the definition of the function has the same debug info,
4709 we should be able to match up with it anyway. But fallbacks
4710 using the minimal symbol, for instance to find a method
4711 implemented in a stripped copy of libstdc++, will not work.
4712 If we do not have debug info for the definition, we will have to
4713 match them up some other way.
4714
4715 When we do name matching there is a related problem with function
4716 templates; two instantiated function templates are allowed to
4717 differ only by their return types, which we do not add here. */
4718
4719 if (cu->language == language_cplus && strchr (name, '<') == NULL)
4720 {
4721 struct attribute *attr;
4722 struct die_info *child;
4723 int first = 1;
4724
4725 die->building_fullname = 1;
4726
4727 for (child = die->child; child != NULL; child = child->sibling)
4728 {
4729 struct type *type;
4730 long value;
4731 gdb_byte *bytes;
4732 struct dwarf2_locexpr_baton *baton;
4733 struct value *v;
4734
4735 if (child->tag != DW_TAG_template_type_param
4736 && child->tag != DW_TAG_template_value_param)
4737 continue;
4738
4739 if (first)
4740 {
4741 fputs_unfiltered ("<", buf);
4742 first = 0;
4743 }
4744 else
4745 fputs_unfiltered (", ", buf);
4746
4747 attr = dwarf2_attr (child, DW_AT_type, cu);
4748 if (attr == NULL)
4749 {
4750 complaint (&symfile_complaints,
4751 _("template parameter missing DW_AT_type"));
4752 fputs_unfiltered ("UNKNOWN_TYPE", buf);
4753 continue;
4754 }
4755 type = die_type (child, cu);
4756
4757 if (child->tag == DW_TAG_template_type_param)
4758 {
4759 c_print_type (type, "", buf, -1, 0);
4760 continue;
4761 }
4762
4763 attr = dwarf2_attr (child, DW_AT_const_value, cu);
4764 if (attr == NULL)
4765 {
4766 complaint (&symfile_complaints,
4767 _("template parameter missing DW_AT_const_value"));
4768 fputs_unfiltered ("UNKNOWN_VALUE", buf);
4769 continue;
4770 }
4771
4772 dwarf2_const_value_attr (attr, type, name,
4773 &cu->comp_unit_obstack, cu,
4774 &value, &bytes, &baton);
4775
4776 if (TYPE_NOSIGN (type))
4777 /* GDB prints characters as NUMBER 'CHAR'. If that's
4778 changed, this can use value_print instead. */
4779 c_printchar (value, type, buf);
4780 else
4781 {
4782 struct value_print_options opts;
4783
4784 if (baton != NULL)
4785 v = dwarf2_evaluate_loc_desc (type, NULL,
4786 baton->data,
4787 baton->size,
4788 baton->per_cu);
4789 else if (bytes != NULL)
4790 {
4791 v = allocate_value (type);
4792 memcpy (value_contents_writeable (v), bytes,
4793 TYPE_LENGTH (type));
4794 }
4795 else
4796 v = value_from_longest (type, value);
4797
4798 /* Specify decimal so that we do not depend on the radix. */
4799 get_formatted_print_options (&opts, 'd');
4800 opts.raw = 1;
4801 value_print (v, buf, &opts);
4802 release_value (v);
4803 value_free (v);
4804 }
4805 }
4806
4807 die->building_fullname = 0;
4808
4809 if (!first)
4810 {
4811 /* Close the argument list, with a space if necessary
4812 (nested templates). */
4813 char last_char = '\0';
4814 ui_file_put (buf, do_ui_file_peek_last, &last_char);
4815 if (last_char == '>')
4816 fputs_unfiltered (" >", buf);
4817 else
4818 fputs_unfiltered (">", buf);
4819 }
4820 }
4821
4822 /* For Java and C++ methods, append formal parameter type
4823 information, if PHYSNAME. */
4824
4825 if (physname && die->tag == DW_TAG_subprogram
4826 && (cu->language == language_cplus
4827 || cu->language == language_java))
4828 {
4829 struct type *type = read_type_die (die, cu);
4830
4831 c_type_print_args (type, buf, 0, cu->language);
4832
4833 if (cu->language == language_java)
4834 {
4835 /* For java, we must append the return type to method
4836 names. */
4837 if (die->tag == DW_TAG_subprogram)
4838 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
4839 0, 0);
4840 }
4841 else if (cu->language == language_cplus)
4842 {
4843 /* Assume that an artificial first parameter is
4844 "this", but do not crash if it is not. RealView
4845 marks unnamed (and thus unused) parameters as
4846 artificial; there is no way to differentiate
4847 the two cases. */
4848 if (TYPE_NFIELDS (type) > 0
4849 && TYPE_FIELD_ARTIFICIAL (type, 0)
4850 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
4851 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type, 0))))
4852 fputs_unfiltered (" const", buf);
4853 }
4854 }
4855
4856 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
4857 &length);
4858 ui_file_delete (buf);
4859
4860 if (cu->language == language_cplus)
4861 {
4862 char *cname
4863 = dwarf2_canonicalize_name (name, cu,
4864 &cu->objfile->objfile_obstack);
4865
4866 if (cname != NULL)
4867 name = cname;
4868 }
4869 }
4870 }
4871
4872 return name;
4873 }
4874
4875 /* Return the fully qualified name of DIE, based on its DW_AT_name.
4876 If scope qualifiers are appropriate they will be added. The result
4877 will be allocated on the objfile_obstack, or NULL if the DIE does
4878 not have a name. NAME may either be from a previous call to
4879 dwarf2_name or NULL.
4880
4881 The output string will be canonicalized (if C++/Java). */
4882
4883 static const char *
4884 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
4885 {
4886 return dwarf2_compute_name (name, die, cu, 0);
4887 }
4888
4889 /* Construct a physname for the given DIE in CU. NAME may either be
4890 from a previous call to dwarf2_name or NULL. The result will be
4891 allocated on the objfile_objstack or NULL if the DIE does not have a
4892 name.
4893
4894 The output string will be canonicalized (if C++/Java). */
4895
4896 static const char *
4897 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
4898 {
4899 return dwarf2_compute_name (name, die, cu, 1);
4900 }
4901
4902 /* Read the import statement specified by the given die and record it. */
4903
4904 static void
4905 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
4906 {
4907 struct attribute *import_attr;
4908 struct die_info *imported_die;
4909 struct dwarf2_cu *imported_cu;
4910 const char *imported_name;
4911 const char *imported_name_prefix;
4912 const char *canonical_name;
4913 const char *import_alias;
4914 const char *imported_declaration = NULL;
4915 const char *import_prefix;
4916
4917 char *temp;
4918
4919 import_attr = dwarf2_attr (die, DW_AT_import, cu);
4920 if (import_attr == NULL)
4921 {
4922 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
4923 dwarf_tag_name (die->tag));
4924 return;
4925 }
4926
4927 imported_cu = cu;
4928 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
4929 imported_name = dwarf2_name (imported_die, imported_cu);
4930 if (imported_name == NULL)
4931 {
4932 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
4933
4934 The import in the following code:
4935 namespace A
4936 {
4937 typedef int B;
4938 }
4939
4940 int main ()
4941 {
4942 using A::B;
4943 B b;
4944 return b;
4945 }
4946
4947 ...
4948 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
4949 <52> DW_AT_decl_file : 1
4950 <53> DW_AT_decl_line : 6
4951 <54> DW_AT_import : <0x75>
4952 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
4953 <59> DW_AT_name : B
4954 <5b> DW_AT_decl_file : 1
4955 <5c> DW_AT_decl_line : 2
4956 <5d> DW_AT_type : <0x6e>
4957 ...
4958 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
4959 <76> DW_AT_byte_size : 4
4960 <77> DW_AT_encoding : 5 (signed)
4961
4962 imports the wrong die ( 0x75 instead of 0x58 ).
4963 This case will be ignored until the gcc bug is fixed. */
4964 return;
4965 }
4966
4967 /* Figure out the local name after import. */
4968 import_alias = dwarf2_name (die, cu);
4969
4970 /* Figure out where the statement is being imported to. */
4971 import_prefix = determine_prefix (die, cu);
4972
4973 /* Figure out what the scope of the imported die is and prepend it
4974 to the name of the imported die. */
4975 imported_name_prefix = determine_prefix (imported_die, imported_cu);
4976
4977 if (imported_die->tag != DW_TAG_namespace
4978 && imported_die->tag != DW_TAG_module)
4979 {
4980 imported_declaration = imported_name;
4981 canonical_name = imported_name_prefix;
4982 }
4983 else if (strlen (imported_name_prefix) > 0)
4984 {
4985 temp = alloca (strlen (imported_name_prefix)
4986 + 2 + strlen (imported_name) + 1);
4987 strcpy (temp, imported_name_prefix);
4988 strcat (temp, "::");
4989 strcat (temp, imported_name);
4990 canonical_name = temp;
4991 }
4992 else
4993 canonical_name = imported_name;
4994
4995 cp_add_using_directive (import_prefix,
4996 canonical_name,
4997 import_alias,
4998 imported_declaration,
4999 &cu->objfile->objfile_obstack);
5000 }
5001
5002 static void
5003 initialize_cu_func_list (struct dwarf2_cu *cu)
5004 {
5005 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5006 }
5007
5008 /* Cleanup function for read_file_scope. */
5009
5010 static void
5011 free_cu_line_header (void *arg)
5012 {
5013 struct dwarf2_cu *cu = arg;
5014
5015 free_line_header (cu->line_header);
5016 cu->line_header = NULL;
5017 }
5018
5019 static void
5020 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5021 char **name, char **comp_dir)
5022 {
5023 struct attribute *attr;
5024
5025 *name = NULL;
5026 *comp_dir = NULL;
5027
5028 /* Find the filename. Do not use dwarf2_name here, since the filename
5029 is not a source language identifier. */
5030 attr = dwarf2_attr (die, DW_AT_name, cu);
5031 if (attr)
5032 {
5033 *name = DW_STRING (attr);
5034 }
5035
5036 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5037 if (attr)
5038 *comp_dir = DW_STRING (attr);
5039 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5040 {
5041 *comp_dir = ldirname (*name);
5042 if (*comp_dir != NULL)
5043 make_cleanup (xfree, *comp_dir);
5044 }
5045 if (*comp_dir != NULL)
5046 {
5047 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5048 directory, get rid of it. */
5049 char *cp = strchr (*comp_dir, ':');
5050
5051 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5052 *comp_dir = cp + 1;
5053 }
5054
5055 if (*name == NULL)
5056 *name = "<unknown>";
5057 }
5058
5059 /* Process DW_TAG_compile_unit. */
5060
5061 static void
5062 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5063 {
5064 struct objfile *objfile = cu->objfile;
5065 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5066 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5067 CORE_ADDR highpc = ((CORE_ADDR) 0);
5068 struct attribute *attr;
5069 char *name = NULL;
5070 char *comp_dir = NULL;
5071 struct die_info *child_die;
5072 bfd *abfd = objfile->obfd;
5073 struct line_header *line_header = 0;
5074 CORE_ADDR baseaddr;
5075
5076 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5077
5078 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5079
5080 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5081 from finish_block. */
5082 if (lowpc == ((CORE_ADDR) -1))
5083 lowpc = highpc;
5084 lowpc += baseaddr;
5085 highpc += baseaddr;
5086
5087 find_file_and_directory (die, cu, &name, &comp_dir);
5088
5089 attr = dwarf2_attr (die, DW_AT_language, cu);
5090 if (attr)
5091 {
5092 set_cu_language (DW_UNSND (attr), cu);
5093 }
5094
5095 attr = dwarf2_attr (die, DW_AT_producer, cu);
5096 if (attr)
5097 cu->producer = DW_STRING (attr);
5098
5099 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5100 standardised yet. As a workaround for the language detection we fall
5101 back to the DW_AT_producer string. */
5102 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5103 cu->language = language_opencl;
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 /* Process DW_TAG_type_unit.
5159 For TUs we want to skip the first top level sibling if it's not the
5160 actual type being defined by this TU. In this case the first top
5161 level sibling is there to provide context only. */
5162
5163 static void
5164 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5165 {
5166 struct objfile *objfile = cu->objfile;
5167 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5168 CORE_ADDR lowpc;
5169 struct attribute *attr;
5170 char *name = NULL;
5171 char *comp_dir = NULL;
5172 struct die_info *child_die;
5173 bfd *abfd = objfile->obfd;
5174
5175 /* start_symtab needs a low pc, but we don't really have one.
5176 Do what read_file_scope would do in the absence of such info. */
5177 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5178
5179 /* Find the filename. Do not use dwarf2_name here, since the filename
5180 is not a source language identifier. */
5181 attr = dwarf2_attr (die, DW_AT_name, cu);
5182 if (attr)
5183 name = DW_STRING (attr);
5184
5185 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5186 if (attr)
5187 comp_dir = DW_STRING (attr);
5188 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5189 {
5190 comp_dir = ldirname (name);
5191 if (comp_dir != NULL)
5192 make_cleanup (xfree, comp_dir);
5193 }
5194
5195 if (name == NULL)
5196 name = "<unknown>";
5197
5198 attr = dwarf2_attr (die, DW_AT_language, cu);
5199 if (attr)
5200 set_cu_language (DW_UNSND (attr), cu);
5201
5202 /* This isn't technically needed today. It is done for symmetry
5203 with read_file_scope. */
5204 attr = dwarf2_attr (die, DW_AT_producer, cu);
5205 if (attr)
5206 cu->producer = DW_STRING (attr);
5207
5208 /* We assume that we're processing GCC output. */
5209 processing_gcc_compilation = 2;
5210
5211 processing_has_namespace_info = 0;
5212
5213 start_symtab (name, comp_dir, lowpc);
5214 record_debugformat ("DWARF 2");
5215 record_producer (cu->producer);
5216
5217 /* Process the dies in the type unit. */
5218 if (die->child == NULL)
5219 {
5220 dump_die_for_error (die);
5221 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5222 bfd_get_filename (abfd));
5223 }
5224
5225 child_die = die->child;
5226
5227 while (child_die && child_die->tag)
5228 {
5229 process_die (child_die, cu);
5230
5231 child_die = sibling_die (child_die);
5232 }
5233
5234 do_cleanups (back_to);
5235 }
5236
5237 static void
5238 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5239 struct dwarf2_cu *cu)
5240 {
5241 struct function_range *thisfn;
5242
5243 thisfn = (struct function_range *)
5244 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5245 thisfn->name = name;
5246 thisfn->lowpc = lowpc;
5247 thisfn->highpc = highpc;
5248 thisfn->seen_line = 0;
5249 thisfn->next = NULL;
5250
5251 if (cu->last_fn == NULL)
5252 cu->first_fn = thisfn;
5253 else
5254 cu->last_fn->next = thisfn;
5255
5256 cu->last_fn = thisfn;
5257 }
5258
5259 /* qsort helper for inherit_abstract_dies. */
5260
5261 static int
5262 unsigned_int_compar (const void *ap, const void *bp)
5263 {
5264 unsigned int a = *(unsigned int *) ap;
5265 unsigned int b = *(unsigned int *) bp;
5266
5267 return (a > b) - (b > a);
5268 }
5269
5270 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5271 Inherit only the children of the DW_AT_abstract_origin DIE not being already
5272 referenced by DW_AT_abstract_origin from the children of the current DIE. */
5273
5274 static void
5275 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5276 {
5277 struct die_info *child_die;
5278 unsigned die_children_count;
5279 /* CU offsets which were referenced by children of the current DIE. */
5280 unsigned *offsets;
5281 unsigned *offsets_end, *offsetp;
5282 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5283 struct die_info *origin_die;
5284 /* Iterator of the ORIGIN_DIE children. */
5285 struct die_info *origin_child_die;
5286 struct cleanup *cleanups;
5287 struct attribute *attr;
5288 struct dwarf2_cu *origin_cu;
5289 struct pending **origin_previous_list_in_scope;
5290
5291 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5292 if (!attr)
5293 return;
5294
5295 /* Note that following die references may follow to a die in a
5296 different cu. */
5297
5298 origin_cu = cu;
5299 origin_die = follow_die_ref (die, attr, &origin_cu);
5300
5301 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5302 symbols in. */
5303 origin_previous_list_in_scope = origin_cu->list_in_scope;
5304 origin_cu->list_in_scope = cu->list_in_scope;
5305
5306 if (die->tag != origin_die->tag
5307 && !(die->tag == DW_TAG_inlined_subroutine
5308 && origin_die->tag == DW_TAG_subprogram))
5309 complaint (&symfile_complaints,
5310 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5311 die->offset, origin_die->offset);
5312
5313 child_die = die->child;
5314 die_children_count = 0;
5315 while (child_die && child_die->tag)
5316 {
5317 child_die = sibling_die (child_die);
5318 die_children_count++;
5319 }
5320 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5321 cleanups = make_cleanup (xfree, offsets);
5322
5323 offsets_end = offsets;
5324 child_die = die->child;
5325 while (child_die && child_die->tag)
5326 {
5327 /* For each CHILD_DIE, find the corresponding child of
5328 ORIGIN_DIE. If there is more than one layer of
5329 DW_AT_abstract_origin, follow them all; there shouldn't be,
5330 but GCC versions at least through 4.4 generate this (GCC PR
5331 40573). */
5332 struct die_info *child_origin_die = child_die;
5333 struct dwarf2_cu *child_origin_cu = cu;
5334
5335 while (1)
5336 {
5337 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5338 child_origin_cu);
5339 if (attr == NULL)
5340 break;
5341 child_origin_die = follow_die_ref (child_origin_die, attr,
5342 &child_origin_cu);
5343 }
5344
5345 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5346 counterpart may exist. */
5347 if (child_origin_die != child_die)
5348 {
5349 if (child_die->tag != child_origin_die->tag
5350 && !(child_die->tag == DW_TAG_inlined_subroutine
5351 && child_origin_die->tag == DW_TAG_subprogram))
5352 complaint (&symfile_complaints,
5353 _("Child DIE 0x%x and its abstract origin 0x%x have "
5354 "different tags"), child_die->offset,
5355 child_origin_die->offset);
5356 if (child_origin_die->parent != origin_die)
5357 complaint (&symfile_complaints,
5358 _("Child DIE 0x%x and its abstract origin 0x%x have "
5359 "different parents"), child_die->offset,
5360 child_origin_die->offset);
5361 else
5362 *offsets_end++ = child_origin_die->offset;
5363 }
5364 child_die = sibling_die (child_die);
5365 }
5366 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5367 unsigned_int_compar);
5368 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5369 if (offsetp[-1] == *offsetp)
5370 complaint (&symfile_complaints, _("Multiple children of DIE 0x%x refer "
5371 "to DIE 0x%x as their abstract origin"),
5372 die->offset, *offsetp);
5373
5374 offsetp = offsets;
5375 origin_child_die = origin_die->child;
5376 while (origin_child_die && origin_child_die->tag)
5377 {
5378 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5379 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5380 offsetp++;
5381 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5382 {
5383 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5384 process_die (origin_child_die, origin_cu);
5385 }
5386 origin_child_die = sibling_die (origin_child_die);
5387 }
5388 origin_cu->list_in_scope = origin_previous_list_in_scope;
5389
5390 do_cleanups (cleanups);
5391 }
5392
5393 static void
5394 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5395 {
5396 struct objfile *objfile = cu->objfile;
5397 struct context_stack *new;
5398 CORE_ADDR lowpc;
5399 CORE_ADDR highpc;
5400 struct die_info *child_die;
5401 struct attribute *attr, *call_line, *call_file;
5402 char *name;
5403 CORE_ADDR baseaddr;
5404 struct block *block;
5405 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5406 VEC (symbolp) *template_args = NULL;
5407 struct template_symbol *templ_func = NULL;
5408
5409 if (inlined_func)
5410 {
5411 /* If we do not have call site information, we can't show the
5412 caller of this inlined function. That's too confusing, so
5413 only use the scope for local variables. */
5414 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5415 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5416 if (call_line == NULL || call_file == NULL)
5417 {
5418 read_lexical_block_scope (die, cu);
5419 return;
5420 }
5421 }
5422
5423 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5424
5425 name = dwarf2_name (die, cu);
5426
5427 /* Ignore functions with missing or empty names. These are actually
5428 illegal according to the DWARF standard. */
5429 if (name == NULL)
5430 {
5431 complaint (&symfile_complaints,
5432 _("missing name for subprogram DIE at %d"), die->offset);
5433 return;
5434 }
5435
5436 /* Ignore functions with missing or invalid low and high pc attributes. */
5437 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5438 {
5439 attr = dwarf2_attr (die, DW_AT_external, cu);
5440 if (!attr || !DW_UNSND (attr))
5441 complaint (&symfile_complaints,
5442 _("cannot get low and high bounds for subprogram DIE at %d"),
5443 die->offset);
5444 return;
5445 }
5446
5447 lowpc += baseaddr;
5448 highpc += baseaddr;
5449
5450 /* Record the function range for dwarf_decode_lines. */
5451 add_to_cu_func_list (name, lowpc, highpc, cu);
5452
5453 /* If we have any template arguments, then we must allocate a
5454 different sort of symbol. */
5455 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5456 {
5457 if (child_die->tag == DW_TAG_template_type_param
5458 || child_die->tag == DW_TAG_template_value_param)
5459 {
5460 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5461 struct template_symbol);
5462 templ_func->base.is_cplus_template_function = 1;
5463 break;
5464 }
5465 }
5466
5467 new = push_context (0, lowpc);
5468 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5469 (struct symbol *) templ_func);
5470
5471 /* If there is a location expression for DW_AT_frame_base, record
5472 it. */
5473 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5474 if (attr)
5475 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5476 expression is being recorded directly in the function's symbol
5477 and not in a separate frame-base object. I guess this hack is
5478 to avoid adding some sort of frame-base adjunct/annex to the
5479 function's symbol :-(. The problem with doing this is that it
5480 results in a function symbol with a location expression that
5481 has nothing to do with the location of the function, ouch! The
5482 relationship should be: a function's symbol has-a frame base; a
5483 frame-base has-a location expression. */
5484 dwarf2_symbol_mark_computed (attr, new->name, cu);
5485
5486 cu->list_in_scope = &local_symbols;
5487
5488 if (die->child != NULL)
5489 {
5490 child_die = die->child;
5491 while (child_die && child_die->tag)
5492 {
5493 if (child_die->tag == DW_TAG_template_type_param
5494 || child_die->tag == DW_TAG_template_value_param)
5495 {
5496 struct symbol *arg = new_symbol (child_die, NULL, cu);
5497
5498 if (arg != NULL)
5499 VEC_safe_push (symbolp, template_args, arg);
5500 }
5501 else
5502 process_die (child_die, cu);
5503 child_die = sibling_die (child_die);
5504 }
5505 }
5506
5507 inherit_abstract_dies (die, cu);
5508
5509 /* If we have a DW_AT_specification, we might need to import using
5510 directives from the context of the specification DIE. See the
5511 comment in determine_prefix. */
5512 if (cu->language == language_cplus
5513 && dwarf2_attr (die, DW_AT_specification, cu))
5514 {
5515 struct dwarf2_cu *spec_cu = cu;
5516 struct die_info *spec_die = die_specification (die, &spec_cu);
5517
5518 while (spec_die)
5519 {
5520 child_die = spec_die->child;
5521 while (child_die && child_die->tag)
5522 {
5523 if (child_die->tag == DW_TAG_imported_module)
5524 process_die (child_die, spec_cu);
5525 child_die = sibling_die (child_die);
5526 }
5527
5528 /* In some cases, GCC generates specification DIEs that
5529 themselves contain DW_AT_specification attributes. */
5530 spec_die = die_specification (spec_die, &spec_cu);
5531 }
5532 }
5533
5534 new = pop_context ();
5535 /* Make a block for the local symbols within. */
5536 block = finish_block (new->name, &local_symbols, new->old_blocks,
5537 lowpc, highpc, objfile);
5538
5539 /* For C++, set the block's scope. */
5540 if (cu->language == language_cplus || cu->language == language_fortran)
5541 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5542 determine_prefix (die, cu),
5543 processing_has_namespace_info);
5544
5545 /* If we have address ranges, record them. */
5546 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5547
5548 /* Attach template arguments to function. */
5549 if (! VEC_empty (symbolp, template_args))
5550 {
5551 gdb_assert (templ_func != NULL);
5552
5553 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5554 templ_func->template_arguments
5555 = obstack_alloc (&objfile->objfile_obstack,
5556 (templ_func->n_template_arguments
5557 * sizeof (struct symbol *)));
5558 memcpy (templ_func->template_arguments,
5559 VEC_address (symbolp, template_args),
5560 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5561 VEC_free (symbolp, template_args);
5562 }
5563
5564 /* In C++, we can have functions nested inside functions (e.g., when
5565 a function declares a class that has methods). This means that
5566 when we finish processing a function scope, we may need to go
5567 back to building a containing block's symbol lists. */
5568 local_symbols = new->locals;
5569 param_symbols = new->params;
5570 using_directives = new->using_directives;
5571
5572 /* If we've finished processing a top-level function, subsequent
5573 symbols go in the file symbol list. */
5574 if (outermost_context_p ())
5575 cu->list_in_scope = &file_symbols;
5576 }
5577
5578 /* Process all the DIES contained within a lexical block scope. Start
5579 a new scope, process the dies, and then close the scope. */
5580
5581 static void
5582 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5583 {
5584 struct objfile *objfile = cu->objfile;
5585 struct context_stack *new;
5586 CORE_ADDR lowpc, highpc;
5587 struct die_info *child_die;
5588 CORE_ADDR baseaddr;
5589
5590 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5591
5592 /* Ignore blocks with missing or invalid low and high pc attributes. */
5593 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5594 as multiple lexical blocks? Handling children in a sane way would
5595 be nasty. Might be easier to properly extend generic blocks to
5596 describe ranges. */
5597 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5598 return;
5599 lowpc += baseaddr;
5600 highpc += baseaddr;
5601
5602 push_context (0, lowpc);
5603 if (die->child != NULL)
5604 {
5605 child_die = die->child;
5606 while (child_die && child_die->tag)
5607 {
5608 process_die (child_die, cu);
5609 child_die = sibling_die (child_die);
5610 }
5611 }
5612 new = pop_context ();
5613
5614 if (local_symbols != NULL || using_directives != NULL)
5615 {
5616 struct block *block
5617 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5618 highpc, objfile);
5619
5620 /* Note that recording ranges after traversing children, as we
5621 do here, means that recording a parent's ranges entails
5622 walking across all its children's ranges as they appear in
5623 the address map, which is quadratic behavior.
5624
5625 It would be nicer to record the parent's ranges before
5626 traversing its children, simply overriding whatever you find
5627 there. But since we don't even decide whether to create a
5628 block until after we've traversed its children, that's hard
5629 to do. */
5630 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5631 }
5632 local_symbols = new->locals;
5633 using_directives = new->using_directives;
5634 }
5635
5636 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5637 Return 1 if the attributes are present and valid, otherwise, return 0.
5638 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
5639
5640 static int
5641 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5642 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5643 struct partial_symtab *ranges_pst)
5644 {
5645 struct objfile *objfile = cu->objfile;
5646 struct comp_unit_head *cu_header = &cu->header;
5647 bfd *obfd = objfile->obfd;
5648 unsigned int addr_size = cu_header->addr_size;
5649 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5650 /* Base address selection entry. */
5651 CORE_ADDR base;
5652 int found_base;
5653 unsigned int dummy;
5654 gdb_byte *buffer;
5655 CORE_ADDR marker;
5656 int low_set;
5657 CORE_ADDR low = 0;
5658 CORE_ADDR high = 0;
5659 CORE_ADDR baseaddr;
5660
5661 found_base = cu->base_known;
5662 base = cu->base_address;
5663
5664 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
5665 if (offset >= dwarf2_per_objfile->ranges.size)
5666 {
5667 complaint (&symfile_complaints,
5668 _("Offset %d out of bounds for DW_AT_ranges attribute"),
5669 offset);
5670 return 0;
5671 }
5672 buffer = dwarf2_per_objfile->ranges.buffer + offset;
5673
5674 /* Read in the largest possible address. */
5675 marker = read_address (obfd, buffer, cu, &dummy);
5676 if ((marker & mask) == mask)
5677 {
5678 /* If we found the largest possible address, then
5679 read the base address. */
5680 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5681 buffer += 2 * addr_size;
5682 offset += 2 * addr_size;
5683 found_base = 1;
5684 }
5685
5686 low_set = 0;
5687
5688 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5689
5690 while (1)
5691 {
5692 CORE_ADDR range_beginning, range_end;
5693
5694 range_beginning = read_address (obfd, buffer, cu, &dummy);
5695 buffer += addr_size;
5696 range_end = read_address (obfd, buffer, cu, &dummy);
5697 buffer += addr_size;
5698 offset += 2 * addr_size;
5699
5700 /* An end of list marker is a pair of zero addresses. */
5701 if (range_beginning == 0 && range_end == 0)
5702 /* Found the end of list entry. */
5703 break;
5704
5705 /* Each base address selection entry is a pair of 2 values.
5706 The first is the largest possible address, the second is
5707 the base address. Check for a base address here. */
5708 if ((range_beginning & mask) == mask)
5709 {
5710 /* If we found the largest possible address, then
5711 read the base address. */
5712 base = read_address (obfd, buffer + addr_size, cu, &dummy);
5713 found_base = 1;
5714 continue;
5715 }
5716
5717 if (!found_base)
5718 {
5719 /* We have no valid base address for the ranges
5720 data. */
5721 complaint (&symfile_complaints,
5722 _("Invalid .debug_ranges data (no base address)"));
5723 return 0;
5724 }
5725
5726 range_beginning += base;
5727 range_end += base;
5728
5729 if (ranges_pst != NULL && range_beginning < range_end)
5730 addrmap_set_empty (objfile->psymtabs_addrmap,
5731 range_beginning + baseaddr, range_end - 1 + baseaddr,
5732 ranges_pst);
5733
5734 /* FIXME: This is recording everything as a low-high
5735 segment of consecutive addresses. We should have a
5736 data structure for discontiguous block ranges
5737 instead. */
5738 if (! low_set)
5739 {
5740 low = range_beginning;
5741 high = range_end;
5742 low_set = 1;
5743 }
5744 else
5745 {
5746 if (range_beginning < low)
5747 low = range_beginning;
5748 if (range_end > high)
5749 high = range_end;
5750 }
5751 }
5752
5753 if (! low_set)
5754 /* If the first entry is an end-of-list marker, the range
5755 describes an empty scope, i.e. no instructions. */
5756 return 0;
5757
5758 if (low_return)
5759 *low_return = low;
5760 if (high_return)
5761 *high_return = high;
5762 return 1;
5763 }
5764
5765 /* Get low and high pc attributes from a die. Return 1 if the attributes
5766 are present and valid, otherwise, return 0. Return -1 if the range is
5767 discontinuous, i.e. derived from DW_AT_ranges information. */
5768 static int
5769 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
5770 CORE_ADDR *highpc, struct dwarf2_cu *cu,
5771 struct partial_symtab *pst)
5772 {
5773 struct attribute *attr;
5774 CORE_ADDR low = 0;
5775 CORE_ADDR high = 0;
5776 int ret = 0;
5777
5778 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5779 if (attr)
5780 {
5781 high = DW_ADDR (attr);
5782 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5783 if (attr)
5784 low = DW_ADDR (attr);
5785 else
5786 /* Found high w/o low attribute. */
5787 return 0;
5788
5789 /* Found consecutive range of addresses. */
5790 ret = 1;
5791 }
5792 else
5793 {
5794 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5795 if (attr != NULL)
5796 {
5797 /* Value of the DW_AT_ranges attribute is the offset in the
5798 .debug_ranges section. */
5799 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
5800 return 0;
5801 /* Found discontinuous range of addresses. */
5802 ret = -1;
5803 }
5804 }
5805
5806 if (high < low)
5807 return 0;
5808
5809 /* When using the GNU linker, .gnu.linkonce. sections are used to
5810 eliminate duplicate copies of functions and vtables and such.
5811 The linker will arbitrarily choose one and discard the others.
5812 The AT_*_pc values for such functions refer to local labels in
5813 these sections. If the section from that file was discarded, the
5814 labels are not in the output, so the relocs get a value of 0.
5815 If this is a discarded function, mark the pc bounds as invalid,
5816 so that GDB will ignore it. */
5817 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
5818 return 0;
5819
5820 *lowpc = low;
5821 *highpc = high;
5822 return ret;
5823 }
5824
5825 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
5826 its low and high PC addresses. Do nothing if these addresses could not
5827 be determined. Otherwise, set LOWPC to the low address if it is smaller,
5828 and HIGHPC to the high address if greater than HIGHPC. */
5829
5830 static void
5831 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
5832 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5833 struct dwarf2_cu *cu)
5834 {
5835 CORE_ADDR low, high;
5836 struct die_info *child = die->child;
5837
5838 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
5839 {
5840 *lowpc = min (*lowpc, low);
5841 *highpc = max (*highpc, high);
5842 }
5843
5844 /* If the language does not allow nested subprograms (either inside
5845 subprograms or lexical blocks), we're done. */
5846 if (cu->language != language_ada)
5847 return;
5848
5849 /* Check all the children of the given DIE. If it contains nested
5850 subprograms, then check their pc bounds. Likewise, we need to
5851 check lexical blocks as well, as they may also contain subprogram
5852 definitions. */
5853 while (child && child->tag)
5854 {
5855 if (child->tag == DW_TAG_subprogram
5856 || child->tag == DW_TAG_lexical_block)
5857 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
5858 child = sibling_die (child);
5859 }
5860 }
5861
5862 /* Get the low and high pc's represented by the scope DIE, and store
5863 them in *LOWPC and *HIGHPC. If the correct values can't be
5864 determined, set *LOWPC to -1 and *HIGHPC to 0. */
5865
5866 static void
5867 get_scope_pc_bounds (struct die_info *die,
5868 CORE_ADDR *lowpc, CORE_ADDR *highpc,
5869 struct dwarf2_cu *cu)
5870 {
5871 CORE_ADDR best_low = (CORE_ADDR) -1;
5872 CORE_ADDR best_high = (CORE_ADDR) 0;
5873 CORE_ADDR current_low, current_high;
5874
5875 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
5876 {
5877 best_low = current_low;
5878 best_high = current_high;
5879 }
5880 else
5881 {
5882 struct die_info *child = die->child;
5883
5884 while (child && child->tag)
5885 {
5886 switch (child->tag) {
5887 case DW_TAG_subprogram:
5888 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
5889 break;
5890 case DW_TAG_namespace:
5891 case DW_TAG_module:
5892 /* FIXME: carlton/2004-01-16: Should we do this for
5893 DW_TAG_class_type/DW_TAG_structure_type, too? I think
5894 that current GCC's always emit the DIEs corresponding
5895 to definitions of methods of classes as children of a
5896 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
5897 the DIEs giving the declarations, which could be
5898 anywhere). But I don't see any reason why the
5899 standards says that they have to be there. */
5900 get_scope_pc_bounds (child, &current_low, &current_high, cu);
5901
5902 if (current_low != ((CORE_ADDR) -1))
5903 {
5904 best_low = min (best_low, current_low);
5905 best_high = max (best_high, current_high);
5906 }
5907 break;
5908 default:
5909 /* Ignore. */
5910 break;
5911 }
5912
5913 child = sibling_die (child);
5914 }
5915 }
5916
5917 *lowpc = best_low;
5918 *highpc = best_high;
5919 }
5920
5921 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
5922 in DIE. */
5923 static void
5924 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
5925 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
5926 {
5927 struct attribute *attr;
5928
5929 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
5930 if (attr)
5931 {
5932 CORE_ADDR high = DW_ADDR (attr);
5933
5934 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
5935 if (attr)
5936 {
5937 CORE_ADDR low = DW_ADDR (attr);
5938
5939 record_block_range (block, baseaddr + low, baseaddr + high - 1);
5940 }
5941 }
5942
5943 attr = dwarf2_attr (die, DW_AT_ranges, cu);
5944 if (attr)
5945 {
5946 bfd *obfd = cu->objfile->obfd;
5947
5948 /* The value of the DW_AT_ranges attribute is the offset of the
5949 address range list in the .debug_ranges section. */
5950 unsigned long offset = DW_UNSND (attr);
5951 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
5952
5953 /* For some target architectures, but not others, the
5954 read_address function sign-extends the addresses it returns.
5955 To recognize base address selection entries, we need a
5956 mask. */
5957 unsigned int addr_size = cu->header.addr_size;
5958 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5959
5960 /* The base address, to which the next pair is relative. Note
5961 that this 'base' is a DWARF concept: most entries in a range
5962 list are relative, to reduce the number of relocs against the
5963 debugging information. This is separate from this function's
5964 'baseaddr' argument, which GDB uses to relocate debugging
5965 information from a shared library based on the address at
5966 which the library was loaded. */
5967 CORE_ADDR base = cu->base_address;
5968 int base_known = cu->base_known;
5969
5970 gdb_assert (dwarf2_per_objfile->ranges.readin);
5971 if (offset >= dwarf2_per_objfile->ranges.size)
5972 {
5973 complaint (&symfile_complaints,
5974 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
5975 offset);
5976 return;
5977 }
5978
5979 for (;;)
5980 {
5981 unsigned int bytes_read;
5982 CORE_ADDR start, end;
5983
5984 start = read_address (obfd, buffer, cu, &bytes_read);
5985 buffer += bytes_read;
5986 end = read_address (obfd, buffer, cu, &bytes_read);
5987 buffer += bytes_read;
5988
5989 /* Did we find the end of the range list? */
5990 if (start == 0 && end == 0)
5991 break;
5992
5993 /* Did we find a base address selection entry? */
5994 else if ((start & base_select_mask) == base_select_mask)
5995 {
5996 base = end;
5997 base_known = 1;
5998 }
5999
6000 /* We found an ordinary address range. */
6001 else
6002 {
6003 if (!base_known)
6004 {
6005 complaint (&symfile_complaints,
6006 _("Invalid .debug_ranges data (no base address)"));
6007 return;
6008 }
6009
6010 record_block_range (block,
6011 baseaddr + base + start,
6012 baseaddr + base + end - 1);
6013 }
6014 }
6015 }
6016 }
6017
6018 /* Add an aggregate field to the field list. */
6019
6020 static void
6021 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6022 struct dwarf2_cu *cu)
6023 {
6024 struct objfile *objfile = cu->objfile;
6025 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6026 struct nextfield *new_field;
6027 struct attribute *attr;
6028 struct field *fp;
6029 char *fieldname = "";
6030
6031 /* Allocate a new field list entry and link it in. */
6032 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6033 make_cleanup (xfree, new_field);
6034 memset (new_field, 0, sizeof (struct nextfield));
6035
6036 if (die->tag == DW_TAG_inheritance)
6037 {
6038 new_field->next = fip->baseclasses;
6039 fip->baseclasses = new_field;
6040 }
6041 else
6042 {
6043 new_field->next = fip->fields;
6044 fip->fields = new_field;
6045 }
6046 fip->nfields++;
6047
6048 /* Handle accessibility and virtuality of field.
6049 The default accessibility for members is public, the default
6050 accessibility for inheritance is private. */
6051 if (die->tag != DW_TAG_inheritance)
6052 new_field->accessibility = DW_ACCESS_public;
6053 else
6054 new_field->accessibility = DW_ACCESS_private;
6055 new_field->virtuality = DW_VIRTUALITY_none;
6056
6057 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6058 if (attr)
6059 new_field->accessibility = DW_UNSND (attr);
6060 if (new_field->accessibility != DW_ACCESS_public)
6061 fip->non_public_fields = 1;
6062 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6063 if (attr)
6064 new_field->virtuality = DW_UNSND (attr);
6065
6066 fp = &new_field->field;
6067
6068 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6069 {
6070 /* Data member other than a C++ static data member. */
6071
6072 /* Get type of field. */
6073 fp->type = die_type (die, cu);
6074
6075 SET_FIELD_BITPOS (*fp, 0);
6076
6077 /* Get bit size of field (zero if none). */
6078 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6079 if (attr)
6080 {
6081 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6082 }
6083 else
6084 {
6085 FIELD_BITSIZE (*fp) = 0;
6086 }
6087
6088 /* Get bit offset of field. */
6089 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6090 if (attr)
6091 {
6092 int byte_offset = 0;
6093
6094 if (attr_form_is_section_offset (attr))
6095 dwarf2_complex_location_expr_complaint ();
6096 else if (attr_form_is_constant (attr))
6097 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6098 else if (attr_form_is_block (attr))
6099 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6100 else
6101 dwarf2_complex_location_expr_complaint ();
6102
6103 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6104 }
6105 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6106 if (attr)
6107 {
6108 if (gdbarch_bits_big_endian (gdbarch))
6109 {
6110 /* For big endian bits, the DW_AT_bit_offset gives the
6111 additional bit offset from the MSB of the containing
6112 anonymous object to the MSB of the field. We don't
6113 have to do anything special since we don't need to
6114 know the size of the anonymous object. */
6115 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6116 }
6117 else
6118 {
6119 /* For little endian bits, compute the bit offset to the
6120 MSB of the anonymous object, subtract off the number of
6121 bits from the MSB of the field to the MSB of the
6122 object, and then subtract off the number of bits of
6123 the field itself. The result is the bit offset of
6124 the LSB of the field. */
6125 int anonymous_size;
6126 int bit_offset = DW_UNSND (attr);
6127
6128 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6129 if (attr)
6130 {
6131 /* The size of the anonymous object containing
6132 the bit field is explicit, so use the
6133 indicated size (in bytes). */
6134 anonymous_size = DW_UNSND (attr);
6135 }
6136 else
6137 {
6138 /* The size of the anonymous object containing
6139 the bit field must be inferred from the type
6140 attribute of the data member containing the
6141 bit field. */
6142 anonymous_size = TYPE_LENGTH (fp->type);
6143 }
6144 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6145 - bit_offset - FIELD_BITSIZE (*fp);
6146 }
6147 }
6148
6149 /* Get name of field. */
6150 fieldname = dwarf2_name (die, cu);
6151 if (fieldname == NULL)
6152 fieldname = "";
6153
6154 /* The name is already allocated along with this objfile, so we don't
6155 need to duplicate it for the type. */
6156 fp->name = fieldname;
6157
6158 /* Change accessibility for artificial fields (e.g. virtual table
6159 pointer or virtual base class pointer) to private. */
6160 if (dwarf2_attr (die, DW_AT_artificial, cu))
6161 {
6162 FIELD_ARTIFICIAL (*fp) = 1;
6163 new_field->accessibility = DW_ACCESS_private;
6164 fip->non_public_fields = 1;
6165 }
6166 }
6167 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6168 {
6169 /* C++ static member. */
6170
6171 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6172 is a declaration, but all versions of G++ as of this writing
6173 (so through at least 3.2.1) incorrectly generate
6174 DW_TAG_variable tags. */
6175
6176 char *physname;
6177
6178 /* Get name of field. */
6179 fieldname = dwarf2_name (die, cu);
6180 if (fieldname == NULL)
6181 return;
6182
6183 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6184 if (attr
6185 /* Only create a symbol if this is an external value.
6186 new_symbol checks this and puts the value in the global symbol
6187 table, which we want. If it is not external, new_symbol
6188 will try to put the value in cu->list_in_scope which is wrong. */
6189 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6190 {
6191 /* A static const member, not much different than an enum as far as
6192 we're concerned, except that we can support more types. */
6193 new_symbol (die, NULL, cu);
6194 }
6195
6196 /* Get physical name. */
6197 physname = (char *) dwarf2_physname (fieldname, die, cu);
6198
6199 /* The name is already allocated along with this objfile, so we don't
6200 need to duplicate it for the type. */
6201 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6202 FIELD_TYPE (*fp) = die_type (die, cu);
6203 FIELD_NAME (*fp) = fieldname;
6204 }
6205 else if (die->tag == DW_TAG_inheritance)
6206 {
6207 /* C++ base class field. */
6208 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6209 if (attr)
6210 {
6211 int byte_offset = 0;
6212
6213 if (attr_form_is_section_offset (attr))
6214 dwarf2_complex_location_expr_complaint ();
6215 else if (attr_form_is_constant (attr))
6216 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
6217 else if (attr_form_is_block (attr))
6218 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
6219 else
6220 dwarf2_complex_location_expr_complaint ();
6221
6222 SET_FIELD_BITPOS (*fp, byte_offset * bits_per_byte);
6223 }
6224 FIELD_BITSIZE (*fp) = 0;
6225 FIELD_TYPE (*fp) = die_type (die, cu);
6226 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6227 fip->nbaseclasses++;
6228 }
6229 }
6230
6231 /* Add a typedef defined in the scope of the FIP's class. */
6232
6233 static void
6234 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6235 struct dwarf2_cu *cu)
6236 {
6237 struct objfile *objfile = cu->objfile;
6238 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6239 struct typedef_field_list *new_field;
6240 struct attribute *attr;
6241 struct typedef_field *fp;
6242 char *fieldname = "";
6243
6244 /* Allocate a new field list entry and link it in. */
6245 new_field = xzalloc (sizeof (*new_field));
6246 make_cleanup (xfree, new_field);
6247
6248 gdb_assert (die->tag == DW_TAG_typedef);
6249
6250 fp = &new_field->field;
6251
6252 /* Get name of field. */
6253 fp->name = dwarf2_name (die, cu);
6254 if (fp->name == NULL)
6255 return;
6256
6257 fp->type = read_type_die (die, cu);
6258
6259 new_field->next = fip->typedef_field_list;
6260 fip->typedef_field_list = new_field;
6261 fip->typedef_field_list_count++;
6262 }
6263
6264 /* Create the vector of fields, and attach it to the type. */
6265
6266 static void
6267 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6268 struct dwarf2_cu *cu)
6269 {
6270 int nfields = fip->nfields;
6271
6272 /* Record the field count, allocate space for the array of fields,
6273 and create blank accessibility bitfields if necessary. */
6274 TYPE_NFIELDS (type) = nfields;
6275 TYPE_FIELDS (type) = (struct field *)
6276 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6277 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6278
6279 if (fip->non_public_fields && cu->language != language_ada)
6280 {
6281 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6282
6283 TYPE_FIELD_PRIVATE_BITS (type) =
6284 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6285 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6286
6287 TYPE_FIELD_PROTECTED_BITS (type) =
6288 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6289 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6290
6291 TYPE_FIELD_IGNORE_BITS (type) =
6292 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6293 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6294 }
6295
6296 /* If the type has baseclasses, allocate and clear a bit vector for
6297 TYPE_FIELD_VIRTUAL_BITS. */
6298 if (fip->nbaseclasses && cu->language != language_ada)
6299 {
6300 int num_bytes = B_BYTES (fip->nbaseclasses);
6301 unsigned char *pointer;
6302
6303 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6304 pointer = TYPE_ALLOC (type, num_bytes);
6305 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6306 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6307 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6308 }
6309
6310 /* Copy the saved-up fields into the field vector. Start from the head
6311 of the list, adding to the tail of the field array, so that they end
6312 up in the same order in the array in which they were added to the list. */
6313 while (nfields-- > 0)
6314 {
6315 struct nextfield *fieldp;
6316
6317 if (fip->fields)
6318 {
6319 fieldp = fip->fields;
6320 fip->fields = fieldp->next;
6321 }
6322 else
6323 {
6324 fieldp = fip->baseclasses;
6325 fip->baseclasses = fieldp->next;
6326 }
6327
6328 TYPE_FIELD (type, nfields) = fieldp->field;
6329 switch (fieldp->accessibility)
6330 {
6331 case DW_ACCESS_private:
6332 if (cu->language != language_ada)
6333 SET_TYPE_FIELD_PRIVATE (type, nfields);
6334 break;
6335
6336 case DW_ACCESS_protected:
6337 if (cu->language != language_ada)
6338 SET_TYPE_FIELD_PROTECTED (type, nfields);
6339 break;
6340
6341 case DW_ACCESS_public:
6342 break;
6343
6344 default:
6345 /* Unknown accessibility. Complain and treat it as public. */
6346 {
6347 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6348 fieldp->accessibility);
6349 }
6350 break;
6351 }
6352 if (nfields < fip->nbaseclasses)
6353 {
6354 switch (fieldp->virtuality)
6355 {
6356 case DW_VIRTUALITY_virtual:
6357 case DW_VIRTUALITY_pure_virtual:
6358 if (cu->language == language_ada)
6359 error ("unexpected virtuality in component of Ada type");
6360 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6361 break;
6362 }
6363 }
6364 }
6365 }
6366
6367 /* Add a member function to the proper fieldlist. */
6368
6369 static void
6370 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6371 struct type *type, struct dwarf2_cu *cu)
6372 {
6373 struct objfile *objfile = cu->objfile;
6374 struct attribute *attr;
6375 struct fnfieldlist *flp;
6376 int i;
6377 struct fn_field *fnp;
6378 char *fieldname;
6379 struct nextfnfield *new_fnfield;
6380 struct type *this_type;
6381
6382 if (cu->language == language_ada)
6383 error ("unexpected member function in Ada type");
6384
6385 /* Get name of member function. */
6386 fieldname = dwarf2_name (die, cu);
6387 if (fieldname == NULL)
6388 return;
6389
6390 /* Look up member function name in fieldlist. */
6391 for (i = 0; i < fip->nfnfields; i++)
6392 {
6393 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6394 break;
6395 }
6396
6397 /* Create new list element if necessary. */
6398 if (i < fip->nfnfields)
6399 flp = &fip->fnfieldlists[i];
6400 else
6401 {
6402 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6403 {
6404 fip->fnfieldlists = (struct fnfieldlist *)
6405 xrealloc (fip->fnfieldlists,
6406 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6407 * sizeof (struct fnfieldlist));
6408 if (fip->nfnfields == 0)
6409 make_cleanup (free_current_contents, &fip->fnfieldlists);
6410 }
6411 flp = &fip->fnfieldlists[fip->nfnfields];
6412 flp->name = fieldname;
6413 flp->length = 0;
6414 flp->head = NULL;
6415 i = fip->nfnfields++;
6416 }
6417
6418 /* Create a new member function field and chain it to the field list
6419 entry. */
6420 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6421 make_cleanup (xfree, new_fnfield);
6422 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6423 new_fnfield->next = flp->head;
6424 flp->head = new_fnfield;
6425 flp->length++;
6426
6427 /* Fill in the member function field info. */
6428 fnp = &new_fnfield->fnfield;
6429
6430 /* Delay processing of the physname until later. */
6431 if (cu->language == language_cplus || cu->language == language_java)
6432 {
6433 add_to_method_list (type, i, flp->length - 1, fieldname,
6434 die, cu);
6435 }
6436 else
6437 {
6438 char *physname = (char *) dwarf2_physname (fieldname, die, cu);
6439 fnp->physname = physname ? physname : "";
6440 }
6441
6442 fnp->type = alloc_type (objfile);
6443 this_type = read_type_die (die, cu);
6444 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6445 {
6446 int nparams = TYPE_NFIELDS (this_type);
6447
6448 /* TYPE is the domain of this method, and THIS_TYPE is the type
6449 of the method itself (TYPE_CODE_METHOD). */
6450 smash_to_method_type (fnp->type, type,
6451 TYPE_TARGET_TYPE (this_type),
6452 TYPE_FIELDS (this_type),
6453 TYPE_NFIELDS (this_type),
6454 TYPE_VARARGS (this_type));
6455
6456 /* Handle static member functions.
6457 Dwarf2 has no clean way to discern C++ static and non-static
6458 member functions. G++ helps GDB by marking the first
6459 parameter for non-static member functions (which is the
6460 this pointer) as artificial. We obtain this information
6461 from read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
6462 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6463 fnp->voffset = VOFFSET_STATIC;
6464 }
6465 else
6466 complaint (&symfile_complaints, _("member function type missing for '%s'"),
6467 dwarf2_full_name (fieldname, die, cu));
6468
6469 /* Get fcontext from DW_AT_containing_type if present. */
6470 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6471 fnp->fcontext = die_containing_type (die, cu);
6472
6473 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const
6474 and is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6475
6476 /* Get accessibility. */
6477 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6478 if (attr)
6479 {
6480 switch (DW_UNSND (attr))
6481 {
6482 case DW_ACCESS_private:
6483 fnp->is_private = 1;
6484 break;
6485 case DW_ACCESS_protected:
6486 fnp->is_protected = 1;
6487 break;
6488 }
6489 }
6490
6491 /* Check for artificial methods. */
6492 attr = dwarf2_attr (die, DW_AT_artificial, cu);
6493 if (attr && DW_UNSND (attr) != 0)
6494 fnp->is_artificial = 1;
6495
6496 /* Get index in virtual function table if it is a virtual member
6497 function. For older versions of GCC, this is an offset in the
6498 appropriate virtual table, as specified by DW_AT_containing_type.
6499 For everyone else, it is an expression to be evaluated relative
6500 to the object address. */
6501
6502 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6503 if (attr)
6504 {
6505 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6506 {
6507 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6508 {
6509 /* Old-style GCC. */
6510 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6511 }
6512 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6513 || (DW_BLOCK (attr)->size > 1
6514 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6515 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6516 {
6517 struct dwarf_block blk;
6518 int offset;
6519
6520 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6521 ? 1 : 2);
6522 blk.size = DW_BLOCK (attr)->size - offset;
6523 blk.data = DW_BLOCK (attr)->data + offset;
6524 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6525 if ((fnp->voffset % cu->header.addr_size) != 0)
6526 dwarf2_complex_location_expr_complaint ();
6527 else
6528 fnp->voffset /= cu->header.addr_size;
6529 fnp->voffset += 2;
6530 }
6531 else
6532 dwarf2_complex_location_expr_complaint ();
6533
6534 if (!fnp->fcontext)
6535 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6536 }
6537 else if (attr_form_is_section_offset (attr))
6538 {
6539 dwarf2_complex_location_expr_complaint ();
6540 }
6541 else
6542 {
6543 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6544 fieldname);
6545 }
6546 }
6547 else
6548 {
6549 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6550 if (attr && DW_UNSND (attr))
6551 {
6552 /* GCC does this, as of 2008-08-25; PR debug/37237. */
6553 complaint (&symfile_complaints,
6554 _("Member function \"%s\" (offset %d) is virtual but the vtable offset is not specified"),
6555 fieldname, die->offset);
6556 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6557 TYPE_CPLUS_DYNAMIC (type) = 1;
6558 }
6559 }
6560 }
6561
6562 /* Create the vector of member function fields, and attach it to the type. */
6563
6564 static void
6565 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
6566 struct dwarf2_cu *cu)
6567 {
6568 struct fnfieldlist *flp;
6569 int total_length = 0;
6570 int i;
6571
6572 if (cu->language == language_ada)
6573 error ("unexpected member functions in Ada type");
6574
6575 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6576 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
6577 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
6578
6579 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
6580 {
6581 struct nextfnfield *nfp = flp->head;
6582 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
6583 int k;
6584
6585 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
6586 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
6587 fn_flp->fn_fields = (struct fn_field *)
6588 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
6589 for (k = flp->length; (k--, nfp); nfp = nfp->next)
6590 fn_flp->fn_fields[k] = nfp->fnfield;
6591
6592 total_length += flp->length;
6593 }
6594
6595 TYPE_NFN_FIELDS (type) = fip->nfnfields;
6596 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
6597 }
6598
6599 /* Returns non-zero if NAME is the name of a vtable member in CU's
6600 language, zero otherwise. */
6601 static int
6602 is_vtable_name (const char *name, struct dwarf2_cu *cu)
6603 {
6604 static const char vptr[] = "_vptr";
6605 static const char vtable[] = "vtable";
6606
6607 /* Look for the C++ and Java forms of the vtable. */
6608 if ((cu->language == language_java
6609 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
6610 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
6611 && is_cplus_marker (name[sizeof (vptr) - 1])))
6612 return 1;
6613
6614 return 0;
6615 }
6616
6617 /* GCC outputs unnamed structures that are really pointers to member
6618 functions, with the ABI-specified layout. If TYPE describes
6619 such a structure, smash it into a member function type.
6620
6621 GCC shouldn't do this; it should just output pointer to member DIEs.
6622 This is GCC PR debug/28767. */
6623
6624 static void
6625 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
6626 {
6627 struct type *pfn_type, *domain_type, *new_type;
6628
6629 /* Check for a structure with no name and two children. */
6630 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
6631 return;
6632
6633 /* Check for __pfn and __delta members. */
6634 if (TYPE_FIELD_NAME (type, 0) == NULL
6635 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
6636 || TYPE_FIELD_NAME (type, 1) == NULL
6637 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
6638 return;
6639
6640 /* Find the type of the method. */
6641 pfn_type = TYPE_FIELD_TYPE (type, 0);
6642 if (pfn_type == NULL
6643 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
6644 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
6645 return;
6646
6647 /* Look for the "this" argument. */
6648 pfn_type = TYPE_TARGET_TYPE (pfn_type);
6649 if (TYPE_NFIELDS (pfn_type) == 0
6650 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
6651 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
6652 return;
6653
6654 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
6655 new_type = alloc_type (objfile);
6656 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
6657 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
6658 TYPE_VARARGS (pfn_type));
6659 smash_to_methodptr_type (type, new_type);
6660 }
6661
6662 /* Called when we find the DIE that starts a structure or union scope
6663 (definition) to create a type for the structure or union. Fill in
6664 the type's name and general properties; the members will not be
6665 processed until process_structure_type.
6666
6667 NOTE: we need to call these functions regardless of whether or not the
6668 DIE has a DW_AT_name attribute, since it might be an anonymous
6669 structure or union. This gets the type entered into our set of
6670 user defined types.
6671
6672 However, if the structure is incomplete (an opaque struct/union)
6673 then suppress creating a symbol table entry for it since gdb only
6674 wants to find the one with the complete definition. Note that if
6675 it is complete, we just call new_symbol, which does it's own
6676 checking about whether the struct/union is anonymous or not (and
6677 suppresses creating a symbol table entry itself). */
6678
6679 static struct type *
6680 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
6681 {
6682 struct objfile *objfile = cu->objfile;
6683 struct type *type;
6684 struct attribute *attr;
6685 char *name;
6686
6687 /* If the definition of this type lives in .debug_types, read that type.
6688 Don't follow DW_AT_specification though, that will take us back up
6689 the chain and we want to go down. */
6690 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
6691 if (attr)
6692 {
6693 struct dwarf2_cu *type_cu = cu;
6694 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
6695
6696 /* We could just recurse on read_structure_type, but we need to call
6697 get_die_type to ensure only one type for this DIE is created.
6698 This is important, for example, because for c++ classes we need
6699 TYPE_NAME set which is only done by new_symbol. Blech. */
6700 type = read_type_die (type_die, type_cu);
6701
6702 /* TYPE_CU may not be the same as CU.
6703 Ensure TYPE is recorded in CU's type_hash table. */
6704 return set_die_type (die, type, cu);
6705 }
6706
6707 type = alloc_type (objfile);
6708 INIT_CPLUS_SPECIFIC (type);
6709
6710 name = dwarf2_name (die, cu);
6711 if (name != NULL)
6712 {
6713 if (cu->language == language_cplus
6714 || cu->language == language_java)
6715 {
6716 char *full_name = (char *) dwarf2_full_name (name, die, cu);
6717
6718 /* dwarf2_full_name might have already finished building the DIE's
6719 type. If so, there is no need to continue. */
6720 if (get_die_type (die, cu) != NULL)
6721 return get_die_type (die, cu);
6722
6723 TYPE_TAG_NAME (type) = full_name;
6724 if (die->tag == DW_TAG_structure_type
6725 || die->tag == DW_TAG_class_type)
6726 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6727 }
6728 else
6729 {
6730 /* The name is already allocated along with this objfile, so
6731 we don't need to duplicate it for the type. */
6732 TYPE_TAG_NAME (type) = (char *) name;
6733 if (die->tag == DW_TAG_class_type)
6734 TYPE_NAME (type) = TYPE_TAG_NAME (type);
6735 }
6736 }
6737
6738 if (die->tag == DW_TAG_structure_type)
6739 {
6740 TYPE_CODE (type) = TYPE_CODE_STRUCT;
6741 }
6742 else if (die->tag == DW_TAG_union_type)
6743 {
6744 TYPE_CODE (type) = TYPE_CODE_UNION;
6745 }
6746 else
6747 {
6748 TYPE_CODE (type) = TYPE_CODE_CLASS;
6749 }
6750
6751 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
6752 TYPE_DECLARED_CLASS (type) = 1;
6753
6754 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6755 if (attr)
6756 {
6757 TYPE_LENGTH (type) = DW_UNSND (attr);
6758 }
6759 else
6760 {
6761 TYPE_LENGTH (type) = 0;
6762 }
6763
6764 TYPE_STUB_SUPPORTED (type) = 1;
6765 if (die_is_declaration (die, cu))
6766 TYPE_STUB (type) = 1;
6767 else if (attr == NULL && die->child == NULL
6768 && producer_is_realview (cu->producer))
6769 /* RealView does not output the required DW_AT_declaration
6770 on incomplete types. */
6771 TYPE_STUB (type) = 1;
6772
6773 /* We need to add the type field to the die immediately so we don't
6774 infinitely recurse when dealing with pointers to the structure
6775 type within the structure itself. */
6776 set_die_type (die, type, cu);
6777
6778 /* set_die_type should be already done. */
6779 set_descriptive_type (type, die, cu);
6780
6781 return type;
6782 }
6783
6784 /* Finish creating a structure or union type, including filling in
6785 its members and creating a symbol for it. */
6786
6787 static void
6788 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
6789 {
6790 struct objfile *objfile = cu->objfile;
6791 struct die_info *child_die = die->child;
6792 struct type *type;
6793
6794 type = get_die_type (die, cu);
6795 if (type == NULL)
6796 type = read_structure_type (die, cu);
6797
6798 if (die->child != NULL && ! die_is_declaration (die, cu))
6799 {
6800 struct field_info fi;
6801 struct die_info *child_die;
6802 VEC (symbolp) *template_args = NULL;
6803 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
6804
6805 memset (&fi, 0, sizeof (struct field_info));
6806
6807 child_die = die->child;
6808
6809 while (child_die && child_die->tag)
6810 {
6811 if (child_die->tag == DW_TAG_member
6812 || child_die->tag == DW_TAG_variable)
6813 {
6814 /* NOTE: carlton/2002-11-05: A C++ static data member
6815 should be a DW_TAG_member that is a declaration, but
6816 all versions of G++ as of this writing (so through at
6817 least 3.2.1) incorrectly generate DW_TAG_variable
6818 tags for them instead. */
6819 dwarf2_add_field (&fi, child_die, cu);
6820 }
6821 else if (child_die->tag == DW_TAG_subprogram)
6822 {
6823 /* C++ member function. */
6824 dwarf2_add_member_fn (&fi, child_die, type, cu);
6825 }
6826 else if (child_die->tag == DW_TAG_inheritance)
6827 {
6828 /* C++ base class field. */
6829 dwarf2_add_field (&fi, child_die, cu);
6830 }
6831 else if (child_die->tag == DW_TAG_typedef)
6832 dwarf2_add_typedef (&fi, child_die, cu);
6833 else if (child_die->tag == DW_TAG_template_type_param
6834 || child_die->tag == DW_TAG_template_value_param)
6835 {
6836 struct symbol *arg = new_symbol (child_die, NULL, cu);
6837
6838 if (arg != NULL)
6839 VEC_safe_push (symbolp, template_args, arg);
6840 }
6841
6842 child_die = sibling_die (child_die);
6843 }
6844
6845 /* Attach template arguments to type. */
6846 if (! VEC_empty (symbolp, template_args))
6847 {
6848 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6849 TYPE_N_TEMPLATE_ARGUMENTS (type)
6850 = VEC_length (symbolp, template_args);
6851 TYPE_TEMPLATE_ARGUMENTS (type)
6852 = obstack_alloc (&objfile->objfile_obstack,
6853 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6854 * sizeof (struct symbol *)));
6855 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
6856 VEC_address (symbolp, template_args),
6857 (TYPE_N_TEMPLATE_ARGUMENTS (type)
6858 * sizeof (struct symbol *)));
6859 VEC_free (symbolp, template_args);
6860 }
6861
6862 /* Attach fields and member functions to the type. */
6863 if (fi.nfields)
6864 dwarf2_attach_fields_to_type (&fi, type, cu);
6865 if (fi.nfnfields)
6866 {
6867 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
6868
6869 /* Get the type which refers to the base class (possibly this
6870 class itself) which contains the vtable pointer for the current
6871 class from the DW_AT_containing_type attribute. This use of
6872 DW_AT_containing_type is a GNU extension. */
6873
6874 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6875 {
6876 struct type *t = die_containing_type (die, cu);
6877
6878 TYPE_VPTR_BASETYPE (type) = t;
6879 if (type == t)
6880 {
6881 int i;
6882
6883 /* Our own class provides vtbl ptr. */
6884 for (i = TYPE_NFIELDS (t) - 1;
6885 i >= TYPE_N_BASECLASSES (t);
6886 --i)
6887 {
6888 char *fieldname = TYPE_FIELD_NAME (t, i);
6889
6890 if (is_vtable_name (fieldname, cu))
6891 {
6892 TYPE_VPTR_FIELDNO (type) = i;
6893 break;
6894 }
6895 }
6896
6897 /* Complain if virtual function table field not found. */
6898 if (i < TYPE_N_BASECLASSES (t))
6899 complaint (&symfile_complaints,
6900 _("virtual function table pointer not found when defining class '%s'"),
6901 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
6902 "");
6903 }
6904 else
6905 {
6906 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
6907 }
6908 }
6909 else if (cu->producer
6910 && strncmp (cu->producer,
6911 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
6912 {
6913 /* The IBM XLC compiler does not provide direct indication
6914 of the containing type, but the vtable pointer is
6915 always named __vfp. */
6916
6917 int i;
6918
6919 for (i = TYPE_NFIELDS (type) - 1;
6920 i >= TYPE_N_BASECLASSES (type);
6921 --i)
6922 {
6923 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
6924 {
6925 TYPE_VPTR_FIELDNO (type) = i;
6926 TYPE_VPTR_BASETYPE (type) = type;
6927 break;
6928 }
6929 }
6930 }
6931 }
6932
6933 /* Copy fi.typedef_field_list linked list elements content into the
6934 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
6935 if (fi.typedef_field_list)
6936 {
6937 int i = fi.typedef_field_list_count;
6938
6939 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6940 TYPE_TYPEDEF_FIELD_ARRAY (type)
6941 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
6942 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
6943
6944 /* Reverse the list order to keep the debug info elements order. */
6945 while (--i >= 0)
6946 {
6947 struct typedef_field *dest, *src;
6948
6949 dest = &TYPE_TYPEDEF_FIELD (type, i);
6950 src = &fi.typedef_field_list->field;
6951 fi.typedef_field_list = fi.typedef_field_list->next;
6952 *dest = *src;
6953 }
6954 }
6955
6956 do_cleanups (back_to);
6957 }
6958
6959 quirk_gcc_member_function_pointer (type, cu->objfile);
6960
6961 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
6962 snapshots) has been known to create a die giving a declaration
6963 for a class that has, as a child, a die giving a definition for a
6964 nested class. So we have to process our children even if the
6965 current die is a declaration. Normally, of course, a declaration
6966 won't have any children at all. */
6967
6968 while (child_die != NULL && child_die->tag)
6969 {
6970 if (child_die->tag == DW_TAG_member
6971 || child_die->tag == DW_TAG_variable
6972 || child_die->tag == DW_TAG_inheritance
6973 || child_die->tag == DW_TAG_template_value_param
6974 || child_die->tag == DW_TAG_template_type_param)
6975 {
6976 /* Do nothing. */
6977 }
6978 else
6979 process_die (child_die, cu);
6980
6981 child_die = sibling_die (child_die);
6982 }
6983
6984 /* Do not consider external references. According to the DWARF standard,
6985 these DIEs are identified by the fact that they have no byte_size
6986 attribute, and a declaration attribute. */
6987 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
6988 || !die_is_declaration (die, cu))
6989 new_symbol (die, type, cu);
6990 }
6991
6992 /* Given a DW_AT_enumeration_type die, set its type. We do not
6993 complete the type's fields yet, or create any symbols. */
6994
6995 static struct type *
6996 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
6997 {
6998 struct objfile *objfile = cu->objfile;
6999 struct type *type;
7000 struct attribute *attr;
7001 const char *name;
7002
7003 /* If the definition of this type lives in .debug_types, read that type.
7004 Don't follow DW_AT_specification though, that will take us back up
7005 the chain and we want to go down. */
7006 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7007 if (attr)
7008 {
7009 struct dwarf2_cu *type_cu = cu;
7010 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7011
7012 type = read_type_die (type_die, type_cu);
7013
7014 /* TYPE_CU may not be the same as CU.
7015 Ensure TYPE is recorded in CU's type_hash table. */
7016 return set_die_type (die, type, cu);
7017 }
7018
7019 type = alloc_type (objfile);
7020
7021 TYPE_CODE (type) = TYPE_CODE_ENUM;
7022 name = dwarf2_full_name (NULL, die, cu);
7023 if (name != NULL)
7024 TYPE_TAG_NAME (type) = (char *) name;
7025
7026 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7027 if (attr)
7028 {
7029 TYPE_LENGTH (type) = DW_UNSND (attr);
7030 }
7031 else
7032 {
7033 TYPE_LENGTH (type) = 0;
7034 }
7035
7036 /* The enumeration DIE can be incomplete. In Ada, any type can be
7037 declared as private in the package spec, and then defined only
7038 inside the package body. Such types are known as Taft Amendment
7039 Types. When another package uses such a type, an incomplete DIE
7040 may be generated by the compiler. */
7041 if (die_is_declaration (die, cu))
7042 TYPE_STUB (type) = 1;
7043
7044 return set_die_type (die, type, cu);
7045 }
7046
7047 /* Given a pointer to a die which begins an enumeration, process all
7048 the dies that define the members of the enumeration, and create the
7049 symbol for the enumeration type.
7050
7051 NOTE: We reverse the order of the element list. */
7052
7053 static void
7054 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7055 {
7056 struct type *this_type;
7057
7058 this_type = get_die_type (die, cu);
7059 if (this_type == NULL)
7060 this_type = read_enumeration_type (die, cu);
7061
7062 if (die->child != NULL)
7063 {
7064 struct die_info *child_die;
7065 struct symbol *sym;
7066 struct field *fields = NULL;
7067 int num_fields = 0;
7068 int unsigned_enum = 1;
7069 char *name;
7070
7071 child_die = die->child;
7072 while (child_die && child_die->tag)
7073 {
7074 if (child_die->tag != DW_TAG_enumerator)
7075 {
7076 process_die (child_die, cu);
7077 }
7078 else
7079 {
7080 name = dwarf2_name (child_die, cu);
7081 if (name)
7082 {
7083 sym = new_symbol (child_die, this_type, cu);
7084 if (SYMBOL_VALUE (sym) < 0)
7085 unsigned_enum = 0;
7086
7087 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7088 {
7089 fields = (struct field *)
7090 xrealloc (fields,
7091 (num_fields + DW_FIELD_ALLOC_CHUNK)
7092 * sizeof (struct field));
7093 }
7094
7095 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7096 FIELD_TYPE (fields[num_fields]) = NULL;
7097 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7098 FIELD_BITSIZE (fields[num_fields]) = 0;
7099
7100 num_fields++;
7101 }
7102 }
7103
7104 child_die = sibling_die (child_die);
7105 }
7106
7107 if (num_fields)
7108 {
7109 TYPE_NFIELDS (this_type) = num_fields;
7110 TYPE_FIELDS (this_type) = (struct field *)
7111 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7112 memcpy (TYPE_FIELDS (this_type), fields,
7113 sizeof (struct field) * num_fields);
7114 xfree (fields);
7115 }
7116 if (unsigned_enum)
7117 TYPE_UNSIGNED (this_type) = 1;
7118 }
7119
7120 new_symbol (die, this_type, cu);
7121 }
7122
7123 /* Extract all information from a DW_TAG_array_type DIE and put it in
7124 the DIE's type field. For now, this only handles one dimensional
7125 arrays. */
7126
7127 static struct type *
7128 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7129 {
7130 struct objfile *objfile = cu->objfile;
7131 struct die_info *child_die;
7132 struct type *type;
7133 struct type *element_type, *range_type, *index_type;
7134 struct type **range_types = NULL;
7135 struct attribute *attr;
7136 int ndim = 0;
7137 struct cleanup *back_to;
7138 char *name;
7139
7140 element_type = die_type (die, cu);
7141
7142 /* The die_type call above may have already set the type for this DIE. */
7143 type = get_die_type (die, cu);
7144 if (type)
7145 return type;
7146
7147 /* Irix 6.2 native cc creates array types without children for
7148 arrays with unspecified length. */
7149 if (die->child == NULL)
7150 {
7151 index_type = objfile_type (objfile)->builtin_int;
7152 range_type = create_range_type (NULL, index_type, 0, -1);
7153 type = create_array_type (NULL, element_type, range_type);
7154 return set_die_type (die, type, cu);
7155 }
7156
7157 back_to = make_cleanup (null_cleanup, NULL);
7158 child_die = die->child;
7159 while (child_die && child_die->tag)
7160 {
7161 if (child_die->tag == DW_TAG_subrange_type)
7162 {
7163 struct type *child_type = read_type_die (child_die, cu);
7164
7165 if (child_type != NULL)
7166 {
7167 /* The range type was succesfully read. Save it for
7168 the array type creation. */
7169 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7170 {
7171 range_types = (struct type **)
7172 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7173 * sizeof (struct type *));
7174 if (ndim == 0)
7175 make_cleanup (free_current_contents, &range_types);
7176 }
7177 range_types[ndim++] = child_type;
7178 }
7179 }
7180 child_die = sibling_die (child_die);
7181 }
7182
7183 /* Dwarf2 dimensions are output from left to right, create the
7184 necessary array types in backwards order. */
7185
7186 type = element_type;
7187
7188 if (read_array_order (die, cu) == DW_ORD_col_major)
7189 {
7190 int i = 0;
7191
7192 while (i < ndim)
7193 type = create_array_type (NULL, type, range_types[i++]);
7194 }
7195 else
7196 {
7197 while (ndim-- > 0)
7198 type = create_array_type (NULL, type, range_types[ndim]);
7199 }
7200
7201 /* Understand Dwarf2 support for vector types (like they occur on
7202 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7203 array type. This is not part of the Dwarf2/3 standard yet, but a
7204 custom vendor extension. The main difference between a regular
7205 array and the vector variant is that vectors are passed by value
7206 to functions. */
7207 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7208 if (attr)
7209 make_vector_type (type);
7210
7211 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7212 implementation may choose to implement triple vectors using this
7213 attribute. */
7214 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7215 if (attr)
7216 {
7217 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7218 TYPE_LENGTH (type) = DW_UNSND (attr);
7219 else
7220 complaint (&symfile_complaints, _("\
7221 DW_AT_byte_size for array type smaller than the total size of elements"));
7222 }
7223
7224 name = dwarf2_name (die, cu);
7225 if (name)
7226 TYPE_NAME (type) = name;
7227
7228 /* Install the type in the die. */
7229 set_die_type (die, type, cu);
7230
7231 /* set_die_type should be already done. */
7232 set_descriptive_type (type, die, cu);
7233
7234 do_cleanups (back_to);
7235
7236 return type;
7237 }
7238
7239 static enum dwarf_array_dim_ordering
7240 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7241 {
7242 struct attribute *attr;
7243
7244 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7245
7246 if (attr) return DW_SND (attr);
7247
7248 /*
7249 GNU F77 is a special case, as at 08/2004 array type info is the
7250 opposite order to the dwarf2 specification, but data is still
7251 laid out as per normal fortran.
7252
7253 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7254 version checking.
7255 */
7256
7257 if (cu->language == language_fortran
7258 && cu->producer && strstr (cu->producer, "GNU F77"))
7259 {
7260 return DW_ORD_row_major;
7261 }
7262
7263 switch (cu->language_defn->la_array_ordering)
7264 {
7265 case array_column_major:
7266 return DW_ORD_col_major;
7267 case array_row_major:
7268 default:
7269 return DW_ORD_row_major;
7270 };
7271 }
7272
7273 /* Extract all information from a DW_TAG_set_type DIE and put it in
7274 the DIE's type field. */
7275
7276 static struct type *
7277 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7278 {
7279 struct type *domain_type, *set_type;
7280 struct attribute *attr;
7281
7282 domain_type = die_type (die, cu);
7283
7284 /* The die_type call above may have already set the type for this DIE. */
7285 set_type = get_die_type (die, cu);
7286 if (set_type)
7287 return set_type;
7288
7289 set_type = create_set_type (NULL, domain_type);
7290
7291 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7292 if (attr)
7293 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7294
7295 return set_die_type (die, set_type, cu);
7296 }
7297
7298 /* First cut: install each common block member as a global variable. */
7299
7300 static void
7301 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7302 {
7303 struct die_info *child_die;
7304 struct attribute *attr;
7305 struct symbol *sym;
7306 CORE_ADDR base = (CORE_ADDR) 0;
7307
7308 attr = dwarf2_attr (die, DW_AT_location, cu);
7309 if (attr)
7310 {
7311 /* Support the .debug_loc offsets */
7312 if (attr_form_is_block (attr))
7313 {
7314 base = decode_locdesc (DW_BLOCK (attr), cu);
7315 }
7316 else if (attr_form_is_section_offset (attr))
7317 {
7318 dwarf2_complex_location_expr_complaint ();
7319 }
7320 else
7321 {
7322 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7323 "common block member");
7324 }
7325 }
7326 if (die->child != NULL)
7327 {
7328 child_die = die->child;
7329 while (child_die && child_die->tag)
7330 {
7331 sym = new_symbol (child_die, NULL, cu);
7332 attr = dwarf2_attr (child_die, DW_AT_data_member_location, cu);
7333 if (sym != NULL && attr != NULL)
7334 {
7335 CORE_ADDR byte_offset = 0;
7336
7337 if (attr_form_is_section_offset (attr))
7338 dwarf2_complex_location_expr_complaint ();
7339 else if (attr_form_is_constant (attr))
7340 byte_offset = dwarf2_get_attr_constant_value (attr, 0);
7341 else if (attr_form_is_block (attr))
7342 byte_offset = decode_locdesc (DW_BLOCK (attr), cu);
7343 else
7344 dwarf2_complex_location_expr_complaint ();
7345
7346 SYMBOL_VALUE_ADDRESS (sym) = base + byte_offset;
7347 add_symbol_to_list (sym, &global_symbols);
7348 }
7349 child_die = sibling_die (child_die);
7350 }
7351 }
7352 }
7353
7354 /* Create a type for a C++ namespace. */
7355
7356 static struct type *
7357 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7358 {
7359 struct objfile *objfile = cu->objfile;
7360 const char *previous_prefix, *name;
7361 int is_anonymous;
7362 struct type *type;
7363
7364 /* For extensions, reuse the type of the original namespace. */
7365 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7366 {
7367 struct die_info *ext_die;
7368 struct dwarf2_cu *ext_cu = cu;
7369
7370 ext_die = dwarf2_extension (die, &ext_cu);
7371 type = read_type_die (ext_die, ext_cu);
7372
7373 /* EXT_CU may not be the same as CU.
7374 Ensure TYPE is recorded in CU's type_hash table. */
7375 return set_die_type (die, type, cu);
7376 }
7377
7378 name = namespace_name (die, &is_anonymous, cu);
7379
7380 /* Now build the name of the current namespace. */
7381
7382 previous_prefix = determine_prefix (die, cu);
7383 if (previous_prefix[0] != '\0')
7384 name = typename_concat (&objfile->objfile_obstack,
7385 previous_prefix, name, 0, cu);
7386
7387 /* Create the type. */
7388 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7389 objfile);
7390 TYPE_NAME (type) = (char *) name;
7391 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7392
7393 return set_die_type (die, type, cu);
7394 }
7395
7396 /* Read a C++ namespace. */
7397
7398 static void
7399 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7400 {
7401 struct objfile *objfile = cu->objfile;
7402 const char *name;
7403 int is_anonymous;
7404
7405 /* Add a symbol associated to this if we haven't seen the namespace
7406 before. Also, add a using directive if it's an anonymous
7407 namespace. */
7408
7409 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7410 {
7411 struct type *type;
7412
7413 type = read_type_die (die, cu);
7414 new_symbol (die, type, cu);
7415
7416 name = namespace_name (die, &is_anonymous, cu);
7417 if (is_anonymous)
7418 {
7419 const char *previous_prefix = determine_prefix (die, cu);
7420
7421 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7422 NULL, &objfile->objfile_obstack);
7423 }
7424 }
7425
7426 if (die->child != NULL)
7427 {
7428 struct die_info *child_die = die->child;
7429
7430 while (child_die && child_die->tag)
7431 {
7432 process_die (child_die, cu);
7433 child_die = sibling_die (child_die);
7434 }
7435 }
7436 }
7437
7438 /* Read a Fortran module as type. This DIE can be only a declaration used for
7439 imported module. Still we need that type as local Fortran "use ... only"
7440 declaration imports depend on the created type in determine_prefix. */
7441
7442 static struct type *
7443 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7444 {
7445 struct objfile *objfile = cu->objfile;
7446 char *module_name;
7447 struct type *type;
7448
7449 module_name = dwarf2_name (die, cu);
7450 if (!module_name)
7451 complaint (&symfile_complaints, _("DW_TAG_module has no name, offset 0x%x"),
7452 die->offset);
7453 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7454
7455 /* determine_prefix uses TYPE_TAG_NAME. */
7456 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7457
7458 return set_die_type (die, type, cu);
7459 }
7460
7461 /* Read a Fortran module. */
7462
7463 static void
7464 read_module (struct die_info *die, struct dwarf2_cu *cu)
7465 {
7466 struct die_info *child_die = die->child;
7467
7468 while (child_die && child_die->tag)
7469 {
7470 process_die (child_die, cu);
7471 child_die = sibling_die (child_die);
7472 }
7473 }
7474
7475 /* Return the name of the namespace represented by DIE. Set
7476 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7477 namespace. */
7478
7479 static const char *
7480 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7481 {
7482 struct die_info *current_die;
7483 const char *name = NULL;
7484
7485 /* Loop through the extensions until we find a name. */
7486
7487 for (current_die = die;
7488 current_die != NULL;
7489 current_die = dwarf2_extension (die, &cu))
7490 {
7491 name = dwarf2_name (current_die, cu);
7492 if (name != NULL)
7493 break;
7494 }
7495
7496 /* Is it an anonymous namespace? */
7497
7498 *is_anonymous = (name == NULL);
7499 if (*is_anonymous)
7500 name = "(anonymous namespace)";
7501
7502 return name;
7503 }
7504
7505 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7506 the user defined type vector. */
7507
7508 static struct type *
7509 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7510 {
7511 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7512 struct comp_unit_head *cu_header = &cu->header;
7513 struct type *type;
7514 struct attribute *attr_byte_size;
7515 struct attribute *attr_address_class;
7516 int byte_size, addr_class;
7517 struct type *target_type;
7518
7519 target_type = die_type (die, cu);
7520
7521 /* The die_type call above may have already set the type for this DIE. */
7522 type = get_die_type (die, cu);
7523 if (type)
7524 return type;
7525
7526 type = lookup_pointer_type (target_type);
7527
7528 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7529 if (attr_byte_size)
7530 byte_size = DW_UNSND (attr_byte_size);
7531 else
7532 byte_size = cu_header->addr_size;
7533
7534 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7535 if (attr_address_class)
7536 addr_class = DW_UNSND (attr_address_class);
7537 else
7538 addr_class = DW_ADDR_none;
7539
7540 /* If the pointer size or address class is different than the
7541 default, create a type variant marked as such and set the
7542 length accordingly. */
7543 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7544 {
7545 if (gdbarch_address_class_type_flags_p (gdbarch))
7546 {
7547 int type_flags;
7548
7549 type_flags = gdbarch_address_class_type_flags
7550 (gdbarch, byte_size, addr_class);
7551 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7552 == 0);
7553 type = make_type_with_address_space (type, type_flags);
7554 }
7555 else if (TYPE_LENGTH (type) != byte_size)
7556 {
7557 complaint (&symfile_complaints, _("invalid pointer size %d"), byte_size);
7558 }
7559 else
7560 {
7561 /* Should we also complain about unhandled address classes? */
7562 }
7563 }
7564
7565 TYPE_LENGTH (type) = byte_size;
7566 return set_die_type (die, type, cu);
7567 }
7568
7569 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
7570 the user defined type vector. */
7571
7572 static struct type *
7573 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
7574 {
7575 struct type *type;
7576 struct type *to_type;
7577 struct type *domain;
7578
7579 to_type = die_type (die, cu);
7580 domain = die_containing_type (die, cu);
7581
7582 /* The calls above may have already set the type for this DIE. */
7583 type = get_die_type (die, cu);
7584 if (type)
7585 return type;
7586
7587 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
7588 type = lookup_methodptr_type (to_type);
7589 else
7590 type = lookup_memberptr_type (to_type, domain);
7591
7592 return set_die_type (die, type, cu);
7593 }
7594
7595 /* Extract all information from a DW_TAG_reference_type DIE and add to
7596 the user defined type vector. */
7597
7598 static struct type *
7599 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
7600 {
7601 struct comp_unit_head *cu_header = &cu->header;
7602 struct type *type, *target_type;
7603 struct attribute *attr;
7604
7605 target_type = die_type (die, cu);
7606
7607 /* The die_type call above may have already set the type for this DIE. */
7608 type = get_die_type (die, cu);
7609 if (type)
7610 return type;
7611
7612 type = lookup_reference_type (target_type);
7613 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7614 if (attr)
7615 {
7616 TYPE_LENGTH (type) = DW_UNSND (attr);
7617 }
7618 else
7619 {
7620 TYPE_LENGTH (type) = cu_header->addr_size;
7621 }
7622 return set_die_type (die, type, cu);
7623 }
7624
7625 static struct type *
7626 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
7627 {
7628 struct type *base_type, *cv_type;
7629
7630 base_type = die_type (die, cu);
7631
7632 /* The die_type call above may have already set the type for this DIE. */
7633 cv_type = get_die_type (die, cu);
7634 if (cv_type)
7635 return cv_type;
7636
7637 /* In case the const qualifier is applied to an array type, the element type
7638 is so qualified, not the array type (section 6.7.3 of C99). */
7639 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
7640 {
7641 struct type *el_type, *inner_array;
7642
7643 base_type = copy_type (base_type);
7644 inner_array = base_type;
7645
7646 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
7647 {
7648 TYPE_TARGET_TYPE (inner_array) =
7649 copy_type (TYPE_TARGET_TYPE (inner_array));
7650 inner_array = TYPE_TARGET_TYPE (inner_array);
7651 }
7652
7653 el_type = TYPE_TARGET_TYPE (inner_array);
7654 TYPE_TARGET_TYPE (inner_array) =
7655 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
7656
7657 return set_die_type (die, base_type, cu);
7658 }
7659
7660 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
7661 return set_die_type (die, cv_type, cu);
7662 }
7663
7664 static struct type *
7665 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
7666 {
7667 struct type *base_type, *cv_type;
7668
7669 base_type = die_type (die, cu);
7670
7671 /* The die_type call above may have already set the type for this DIE. */
7672 cv_type = get_die_type (die, cu);
7673 if (cv_type)
7674 return cv_type;
7675
7676 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
7677 return set_die_type (die, cv_type, cu);
7678 }
7679
7680 /* Extract all information from a DW_TAG_string_type DIE and add to
7681 the user defined type vector. It isn't really a user defined type,
7682 but it behaves like one, with other DIE's using an AT_user_def_type
7683 attribute to reference it. */
7684
7685 static struct type *
7686 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
7687 {
7688 struct objfile *objfile = cu->objfile;
7689 struct gdbarch *gdbarch = get_objfile_arch (objfile);
7690 struct type *type, *range_type, *index_type, *char_type;
7691 struct attribute *attr;
7692 unsigned int length;
7693
7694 attr = dwarf2_attr (die, DW_AT_string_length, cu);
7695 if (attr)
7696 {
7697 length = DW_UNSND (attr);
7698 }
7699 else
7700 {
7701 /* check for the DW_AT_byte_size attribute */
7702 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7703 if (attr)
7704 {
7705 length = DW_UNSND (attr);
7706 }
7707 else
7708 {
7709 length = 1;
7710 }
7711 }
7712
7713 index_type = objfile_type (objfile)->builtin_int;
7714 range_type = create_range_type (NULL, index_type, 1, length);
7715 char_type = language_string_char_type (cu->language_defn, gdbarch);
7716 type = create_string_type (NULL, char_type, range_type);
7717
7718 return set_die_type (die, type, cu);
7719 }
7720
7721 /* Handle DIES due to C code like:
7722
7723 struct foo
7724 {
7725 int (*funcp)(int a, long l);
7726 int b;
7727 };
7728
7729 ('funcp' generates a DW_TAG_subroutine_type DIE)
7730 */
7731
7732 static struct type *
7733 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
7734 {
7735 struct type *type; /* Type that this function returns */
7736 struct type *ftype; /* Function that returns above type */
7737 struct attribute *attr;
7738
7739 type = die_type (die, cu);
7740
7741 /* The die_type call above may have already set the type for this DIE. */
7742 ftype = get_die_type (die, cu);
7743 if (ftype)
7744 return ftype;
7745
7746 ftype = lookup_function_type (type);
7747
7748 /* All functions in C++, Pascal and Java have prototypes. */
7749 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
7750 if ((attr && (DW_UNSND (attr) != 0))
7751 || cu->language == language_cplus
7752 || cu->language == language_java
7753 || cu->language == language_pascal)
7754 TYPE_PROTOTYPED (ftype) = 1;
7755 else if (producer_is_realview (cu->producer))
7756 /* RealView does not emit DW_AT_prototyped. We can not
7757 distinguish prototyped and unprototyped functions; default to
7758 prototyped, since that is more common in modern code (and
7759 RealView warns about unprototyped functions). */
7760 TYPE_PROTOTYPED (ftype) = 1;
7761
7762 /* Store the calling convention in the type if it's available in
7763 the subroutine die. Otherwise set the calling convention to
7764 the default value DW_CC_normal. */
7765 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
7766 TYPE_CALLING_CONVENTION (ftype) = attr ? DW_UNSND (attr) : DW_CC_normal;
7767
7768 /* We need to add the subroutine type to the die immediately so
7769 we don't infinitely recurse when dealing with parameters
7770 declared as the same subroutine type. */
7771 set_die_type (die, ftype, cu);
7772
7773 if (die->child != NULL)
7774 {
7775 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
7776 struct die_info *child_die;
7777 int nparams, iparams;
7778
7779 /* Count the number of parameters.
7780 FIXME: GDB currently ignores vararg functions, but knows about
7781 vararg member functions. */
7782 nparams = 0;
7783 child_die = die->child;
7784 while (child_die && child_die->tag)
7785 {
7786 if (child_die->tag == DW_TAG_formal_parameter)
7787 nparams++;
7788 else if (child_die->tag == DW_TAG_unspecified_parameters)
7789 TYPE_VARARGS (ftype) = 1;
7790 child_die = sibling_die (child_die);
7791 }
7792
7793 /* Allocate storage for parameters and fill them in. */
7794 TYPE_NFIELDS (ftype) = nparams;
7795 TYPE_FIELDS (ftype) = (struct field *)
7796 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
7797
7798 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
7799 even if we error out during the parameters reading below. */
7800 for (iparams = 0; iparams < nparams; iparams++)
7801 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
7802
7803 iparams = 0;
7804 child_die = die->child;
7805 while (child_die && child_die->tag)
7806 {
7807 if (child_die->tag == DW_TAG_formal_parameter)
7808 {
7809 struct type *arg_type;
7810
7811 /* DWARF version 2 has no clean way to discern C++
7812 static and non-static member functions. G++ helps
7813 GDB by marking the first parameter for non-static
7814 member functions (which is the this pointer) as
7815 artificial. We pass this information to
7816 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
7817
7818 DWARF version 3 added DW_AT_object_pointer, which GCC
7819 4.5 does not yet generate. */
7820 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
7821 if (attr)
7822 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
7823 else
7824 {
7825 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
7826
7827 /* GCC/43521: In java, the formal parameter
7828 "this" is sometimes not marked with DW_AT_artificial. */
7829 if (cu->language == language_java)
7830 {
7831 const char *name = dwarf2_name (child_die, cu);
7832
7833 if (name && !strcmp (name, "this"))
7834 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
7835 }
7836 }
7837 arg_type = die_type (child_die, cu);
7838
7839 /* RealView does not mark THIS as const, which the testsuite
7840 expects. GCC marks THIS as const in method definitions,
7841 but not in the class specifications (GCC PR 43053). */
7842 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
7843 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
7844 {
7845 int is_this = 0;
7846 struct dwarf2_cu *arg_cu = cu;
7847 const char *name = dwarf2_name (child_die, cu);
7848
7849 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
7850 if (attr)
7851 {
7852 /* If the compiler emits this, use it. */
7853 if (follow_die_ref (die, attr, &arg_cu) == child_die)
7854 is_this = 1;
7855 }
7856 else if (name && strcmp (name, "this") == 0)
7857 /* Function definitions will have the argument names. */
7858 is_this = 1;
7859 else if (name == NULL && iparams == 0)
7860 /* Declarations may not have the names, so like
7861 elsewhere in GDB, assume an artificial first
7862 argument is "this". */
7863 is_this = 1;
7864
7865 if (is_this)
7866 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
7867 arg_type, 0);
7868 }
7869
7870 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
7871 iparams++;
7872 }
7873 child_die = sibling_die (child_die);
7874 }
7875 }
7876
7877 return ftype;
7878 }
7879
7880 static struct type *
7881 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
7882 {
7883 struct objfile *objfile = cu->objfile;
7884 const char *name = NULL;
7885 struct type *this_type;
7886
7887 name = dwarf2_full_name (NULL, die, cu);
7888 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
7889 TYPE_FLAG_TARGET_STUB, NULL, objfile);
7890 TYPE_NAME (this_type) = (char *) name;
7891 set_die_type (die, this_type, cu);
7892 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
7893 return this_type;
7894 }
7895
7896 /* Find a representation of a given base type and install
7897 it in the TYPE field of the die. */
7898
7899 static struct type *
7900 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
7901 {
7902 struct objfile *objfile = cu->objfile;
7903 struct type *type;
7904 struct attribute *attr;
7905 int encoding = 0, size = 0;
7906 char *name;
7907 enum type_code code = TYPE_CODE_INT;
7908 int type_flags = 0;
7909 struct type *target_type = NULL;
7910
7911 attr = dwarf2_attr (die, DW_AT_encoding, cu);
7912 if (attr)
7913 {
7914 encoding = DW_UNSND (attr);
7915 }
7916 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7917 if (attr)
7918 {
7919 size = DW_UNSND (attr);
7920 }
7921 name = dwarf2_name (die, cu);
7922 if (!name)
7923 {
7924 complaint (&symfile_complaints,
7925 _("DW_AT_name missing from DW_TAG_base_type"));
7926 }
7927
7928 switch (encoding)
7929 {
7930 case DW_ATE_address:
7931 /* Turn DW_ATE_address into a void * pointer. */
7932 code = TYPE_CODE_PTR;
7933 type_flags |= TYPE_FLAG_UNSIGNED;
7934 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
7935 break;
7936 case DW_ATE_boolean:
7937 code = TYPE_CODE_BOOL;
7938 type_flags |= TYPE_FLAG_UNSIGNED;
7939 break;
7940 case DW_ATE_complex_float:
7941 code = TYPE_CODE_COMPLEX;
7942 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
7943 break;
7944 case DW_ATE_decimal_float:
7945 code = TYPE_CODE_DECFLOAT;
7946 break;
7947 case DW_ATE_float:
7948 code = TYPE_CODE_FLT;
7949 break;
7950 case DW_ATE_signed:
7951 break;
7952 case DW_ATE_unsigned:
7953 type_flags |= TYPE_FLAG_UNSIGNED;
7954 break;
7955 case DW_ATE_signed_char:
7956 if (cu->language == language_ada || cu->language == language_m2
7957 || cu->language == language_pascal)
7958 code = TYPE_CODE_CHAR;
7959 break;
7960 case DW_ATE_unsigned_char:
7961 if (cu->language == language_ada || cu->language == language_m2
7962 || cu->language == language_pascal)
7963 code = TYPE_CODE_CHAR;
7964 type_flags |= TYPE_FLAG_UNSIGNED;
7965 break;
7966 case DW_ATE_UTF:
7967 /* We just treat this as an integer and then recognize the
7968 type by name elsewhere. */
7969 break;
7970
7971 default:
7972 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
7973 dwarf_type_encoding_name (encoding));
7974 break;
7975 }
7976
7977 type = init_type (code, size, type_flags, NULL, objfile);
7978 TYPE_NAME (type) = name;
7979 TYPE_TARGET_TYPE (type) = target_type;
7980
7981 if (name && strcmp (name, "char") == 0)
7982 TYPE_NOSIGN (type) = 1;
7983
7984 return set_die_type (die, type, cu);
7985 }
7986
7987 /* Read the given DW_AT_subrange DIE. */
7988
7989 static struct type *
7990 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
7991 {
7992 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7993 struct type *base_type;
7994 struct type *range_type;
7995 struct attribute *attr;
7996 LONGEST low = 0;
7997 LONGEST high = -1;
7998 char *name;
7999 LONGEST negative_mask;
8000
8001 base_type = die_type (die, cu);
8002 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8003 check_typedef (base_type);
8004
8005 /* The die_type call above may have already set the type for this DIE. */
8006 range_type = get_die_type (die, cu);
8007 if (range_type)
8008 return range_type;
8009
8010 if (cu->language == language_fortran)
8011 {
8012 /* FORTRAN implies a lower bound of 1, if not given. */
8013 low = 1;
8014 }
8015
8016 /* FIXME: For variable sized arrays either of these could be
8017 a variable rather than a constant value. We'll allow it,
8018 but we don't know how to handle it. */
8019 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8020 if (attr)
8021 low = dwarf2_get_attr_constant_value (attr, 0);
8022
8023 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8024 if (attr)
8025 {
8026 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8027 {
8028 /* GCC encodes arrays with unspecified or dynamic length
8029 with a DW_FORM_block1 attribute or a reference attribute.
8030 FIXME: GDB does not yet know how to handle dynamic
8031 arrays properly, treat them as arrays with unspecified
8032 length for now.
8033
8034 FIXME: jimb/2003-09-22: GDB does not really know
8035 how to handle arrays of unspecified length
8036 either; we just represent them as zero-length
8037 arrays. Choose an appropriate upper bound given
8038 the lower bound we've computed above. */
8039 high = low - 1;
8040 }
8041 else
8042 high = dwarf2_get_attr_constant_value (attr, 1);
8043 }
8044 else
8045 {
8046 attr = dwarf2_attr (die, DW_AT_count, cu);
8047 if (attr)
8048 {
8049 int count = dwarf2_get_attr_constant_value (attr, 1);
8050 high = low + count - 1;
8051 }
8052 }
8053
8054 /* Dwarf-2 specifications explicitly allows to create subrange types
8055 without specifying a base type.
8056 In that case, the base type must be set to the type of
8057 the lower bound, upper bound or count, in that order, if any of these
8058 three attributes references an object that has a type.
8059 If no base type is found, the Dwarf-2 specifications say that
8060 a signed integer type of size equal to the size of an address should
8061 be used.
8062 For the following C code: `extern char gdb_int [];'
8063 GCC produces an empty range DIE.
8064 FIXME: muller/2010-05-28: Possible references to object for low bound,
8065 high bound or count are not yet handled by this code.
8066 */
8067 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8068 {
8069 struct objfile *objfile = cu->objfile;
8070 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8071 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8072 struct type *int_type = objfile_type (objfile)->builtin_int;
8073
8074 /* Test "int", "long int", and "long long int" objfile types,
8075 and select the first one having a size above or equal to the
8076 architecture address size. */
8077 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8078 base_type = int_type;
8079 else
8080 {
8081 int_type = objfile_type (objfile)->builtin_long;
8082 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8083 base_type = int_type;
8084 else
8085 {
8086 int_type = objfile_type (objfile)->builtin_long_long;
8087 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8088 base_type = int_type;
8089 }
8090 }
8091 }
8092
8093 negative_mask =
8094 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8095 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8096 low |= negative_mask;
8097 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8098 high |= negative_mask;
8099
8100 range_type = create_range_type (NULL, base_type, low, high);
8101
8102 /* Mark arrays with dynamic length at least as an array of unspecified
8103 length. GDB could check the boundary but before it gets implemented at
8104 least allow accessing the array elements. */
8105 if (attr && attr->form == DW_FORM_block1)
8106 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8107
8108 name = dwarf2_name (die, cu);
8109 if (name)
8110 TYPE_NAME (range_type) = name;
8111
8112 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8113 if (attr)
8114 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8115
8116 set_die_type (die, range_type, cu);
8117
8118 /* set_die_type should be already done. */
8119 set_descriptive_type (range_type, die, cu);
8120
8121 return range_type;
8122 }
8123
8124 static struct type *
8125 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8126 {
8127 struct type *type;
8128
8129 /* For now, we only support the C meaning of an unspecified type: void. */
8130
8131 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8132 TYPE_NAME (type) = dwarf2_name (die, cu);
8133
8134 return set_die_type (die, type, cu);
8135 }
8136
8137 /* Trivial hash function for die_info: the hash value of a DIE
8138 is its offset in .debug_info for this objfile. */
8139
8140 static hashval_t
8141 die_hash (const void *item)
8142 {
8143 const struct die_info *die = item;
8144
8145 return die->offset;
8146 }
8147
8148 /* Trivial comparison function for die_info structures: two DIEs
8149 are equal if they have the same offset. */
8150
8151 static int
8152 die_eq (const void *item_lhs, const void *item_rhs)
8153 {
8154 const struct die_info *die_lhs = item_lhs;
8155 const struct die_info *die_rhs = item_rhs;
8156
8157 return die_lhs->offset == die_rhs->offset;
8158 }
8159
8160 /* Read a whole compilation unit into a linked list of dies. */
8161
8162 static struct die_info *
8163 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8164 {
8165 struct die_reader_specs reader_specs;
8166 int read_abbrevs = 0;
8167 struct cleanup *back_to = NULL;
8168 struct die_info *die;
8169
8170 if (cu->dwarf2_abbrevs == NULL)
8171 {
8172 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8173 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8174 read_abbrevs = 1;
8175 }
8176
8177 gdb_assert (cu->die_hash == NULL);
8178 cu->die_hash
8179 = htab_create_alloc_ex (cu->header.length / 12,
8180 die_hash,
8181 die_eq,
8182 NULL,
8183 &cu->comp_unit_obstack,
8184 hashtab_obstack_allocate,
8185 dummy_obstack_deallocate);
8186
8187 init_cu_die_reader (&reader_specs, cu);
8188
8189 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8190
8191 if (read_abbrevs)
8192 do_cleanups (back_to);
8193
8194 return die;
8195 }
8196
8197 /* Main entry point for reading a DIE and all children.
8198 Read the DIE and dump it if requested. */
8199
8200 static struct die_info *
8201 read_die_and_children (const struct die_reader_specs *reader,
8202 gdb_byte *info_ptr,
8203 gdb_byte **new_info_ptr,
8204 struct die_info *parent)
8205 {
8206 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8207 new_info_ptr, parent);
8208
8209 if (dwarf2_die_debug)
8210 {
8211 fprintf_unfiltered (gdb_stdlog,
8212 "\nRead die from %s of %s:\n",
8213 reader->buffer == dwarf2_per_objfile->info.buffer
8214 ? ".debug_info"
8215 : reader->buffer == dwarf2_per_objfile->types.buffer
8216 ? ".debug_types"
8217 : "unknown section",
8218 reader->abfd->filename);
8219 dump_die (result, dwarf2_die_debug);
8220 }
8221
8222 return result;
8223 }
8224
8225 /* Read a single die and all its descendents. Set the die's sibling
8226 field to NULL; set other fields in the die correctly, and set all
8227 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8228 location of the info_ptr after reading all of those dies. PARENT
8229 is the parent of the die in question. */
8230
8231 static struct die_info *
8232 read_die_and_children_1 (const struct die_reader_specs *reader,
8233 gdb_byte *info_ptr,
8234 gdb_byte **new_info_ptr,
8235 struct die_info *parent)
8236 {
8237 struct die_info *die;
8238 gdb_byte *cur_ptr;
8239 int has_children;
8240
8241 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8242 if (die == NULL)
8243 {
8244 *new_info_ptr = cur_ptr;
8245 return NULL;
8246 }
8247 store_in_ref_table (die, reader->cu);
8248
8249 if (has_children)
8250 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8251 else
8252 {
8253 die->child = NULL;
8254 *new_info_ptr = cur_ptr;
8255 }
8256
8257 die->sibling = NULL;
8258 die->parent = parent;
8259 return die;
8260 }
8261
8262 /* Read a die, all of its descendents, and all of its siblings; set
8263 all of the fields of all of the dies correctly. Arguments are as
8264 in read_die_and_children. */
8265
8266 static struct die_info *
8267 read_die_and_siblings (const struct die_reader_specs *reader,
8268 gdb_byte *info_ptr,
8269 gdb_byte **new_info_ptr,
8270 struct die_info *parent)
8271 {
8272 struct die_info *first_die, *last_sibling;
8273 gdb_byte *cur_ptr;
8274
8275 cur_ptr = info_ptr;
8276 first_die = last_sibling = NULL;
8277
8278 while (1)
8279 {
8280 struct die_info *die
8281 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8282
8283 if (die == NULL)
8284 {
8285 *new_info_ptr = cur_ptr;
8286 return first_die;
8287 }
8288
8289 if (!first_die)
8290 first_die = die;
8291 else
8292 last_sibling->sibling = die;
8293
8294 last_sibling = die;
8295 }
8296 }
8297
8298 /* Read the die from the .debug_info section buffer. Set DIEP to
8299 point to a newly allocated die with its information, except for its
8300 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8301 whether the die has children or not. */
8302
8303 static gdb_byte *
8304 read_full_die (const struct die_reader_specs *reader,
8305 struct die_info **diep, gdb_byte *info_ptr,
8306 int *has_children)
8307 {
8308 unsigned int abbrev_number, bytes_read, i, offset;
8309 struct abbrev_info *abbrev;
8310 struct die_info *die;
8311 struct dwarf2_cu *cu = reader->cu;
8312 bfd *abfd = reader->abfd;
8313
8314 offset = info_ptr - reader->buffer;
8315 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8316 info_ptr += bytes_read;
8317 if (!abbrev_number)
8318 {
8319 *diep = NULL;
8320 *has_children = 0;
8321 return info_ptr;
8322 }
8323
8324 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8325 if (!abbrev)
8326 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8327 abbrev_number,
8328 bfd_get_filename (abfd));
8329
8330 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8331 die->offset = offset;
8332 die->tag = abbrev->tag;
8333 die->abbrev = abbrev_number;
8334
8335 die->num_attrs = abbrev->num_attrs;
8336
8337 for (i = 0; i < abbrev->num_attrs; ++i)
8338 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8339 abfd, info_ptr, cu);
8340
8341 *diep = die;
8342 *has_children = abbrev->has_children;
8343 return info_ptr;
8344 }
8345
8346 /* In DWARF version 2, the description of the debugging information is
8347 stored in a separate .debug_abbrev section. Before we read any
8348 dies from a section we read in all abbreviations and install them
8349 in a hash table. This function also sets flags in CU describing
8350 the data found in the abbrev table. */
8351
8352 static void
8353 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8354 {
8355 struct comp_unit_head *cu_header = &cu->header;
8356 gdb_byte *abbrev_ptr;
8357 struct abbrev_info *cur_abbrev;
8358 unsigned int abbrev_number, bytes_read, abbrev_name;
8359 unsigned int abbrev_form, hash_number;
8360 struct attr_abbrev *cur_attrs;
8361 unsigned int allocated_attrs;
8362
8363 /* Initialize dwarf2 abbrevs */
8364 obstack_init (&cu->abbrev_obstack);
8365 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8366 (ABBREV_HASH_SIZE
8367 * sizeof (struct abbrev_info *)));
8368 memset (cu->dwarf2_abbrevs, 0,
8369 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8370
8371 dwarf2_read_section (dwarf2_per_objfile->objfile,
8372 &dwarf2_per_objfile->abbrev);
8373 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8374 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8375 abbrev_ptr += bytes_read;
8376
8377 allocated_attrs = ATTR_ALLOC_CHUNK;
8378 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8379
8380 /* loop until we reach an abbrev number of 0 */
8381 while (abbrev_number)
8382 {
8383 cur_abbrev = dwarf_alloc_abbrev (cu);
8384
8385 /* read in abbrev header */
8386 cur_abbrev->number = abbrev_number;
8387 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8388 abbrev_ptr += bytes_read;
8389 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8390 abbrev_ptr += 1;
8391
8392 if (cur_abbrev->tag == DW_TAG_namespace)
8393 cu->has_namespace_info = 1;
8394
8395 /* now read in declarations */
8396 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8397 abbrev_ptr += bytes_read;
8398 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8399 abbrev_ptr += bytes_read;
8400 while (abbrev_name)
8401 {
8402 if (cur_abbrev->num_attrs == allocated_attrs)
8403 {
8404 allocated_attrs += ATTR_ALLOC_CHUNK;
8405 cur_attrs
8406 = xrealloc (cur_attrs, (allocated_attrs
8407 * sizeof (struct attr_abbrev)));
8408 }
8409
8410 /* Record whether this compilation unit might have
8411 inter-compilation-unit references. If we don't know what form
8412 this attribute will have, then it might potentially be a
8413 DW_FORM_ref_addr, so we conservatively expect inter-CU
8414 references. */
8415
8416 if (abbrev_form == DW_FORM_ref_addr
8417 || abbrev_form == DW_FORM_indirect)
8418 cu->has_form_ref_addr = 1;
8419
8420 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8421 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8422 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8423 abbrev_ptr += bytes_read;
8424 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8425 abbrev_ptr += bytes_read;
8426 }
8427
8428 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8429 (cur_abbrev->num_attrs
8430 * sizeof (struct attr_abbrev)));
8431 memcpy (cur_abbrev->attrs, cur_attrs,
8432 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8433
8434 hash_number = abbrev_number % ABBREV_HASH_SIZE;
8435 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8436 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8437
8438 /* Get next abbreviation.
8439 Under Irix6 the abbreviations for a compilation unit are not
8440 always properly terminated with an abbrev number of 0.
8441 Exit loop if we encounter an abbreviation which we have
8442 already read (which means we are about to read the abbreviations
8443 for the next compile unit) or if the end of the abbreviation
8444 table is reached. */
8445 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8446 >= dwarf2_per_objfile->abbrev.size)
8447 break;
8448 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8449 abbrev_ptr += bytes_read;
8450 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8451 break;
8452 }
8453
8454 xfree (cur_attrs);
8455 }
8456
8457 /* Release the memory used by the abbrev table for a compilation unit. */
8458
8459 static void
8460 dwarf2_free_abbrev_table (void *ptr_to_cu)
8461 {
8462 struct dwarf2_cu *cu = ptr_to_cu;
8463
8464 obstack_free (&cu->abbrev_obstack, NULL);
8465 cu->dwarf2_abbrevs = NULL;
8466 }
8467
8468 /* Lookup an abbrev_info structure in the abbrev hash table. */
8469
8470 static struct abbrev_info *
8471 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8472 {
8473 unsigned int hash_number;
8474 struct abbrev_info *abbrev;
8475
8476 hash_number = number % ABBREV_HASH_SIZE;
8477 abbrev = cu->dwarf2_abbrevs[hash_number];
8478
8479 while (abbrev)
8480 {
8481 if (abbrev->number == number)
8482 return abbrev;
8483 else
8484 abbrev = abbrev->next;
8485 }
8486 return NULL;
8487 }
8488
8489 /* Returns nonzero if TAG represents a type that we might generate a partial
8490 symbol for. */
8491
8492 static int
8493 is_type_tag_for_partial (int tag)
8494 {
8495 switch (tag)
8496 {
8497 #if 0
8498 /* Some types that would be reasonable to generate partial symbols for,
8499 that we don't at present. */
8500 case DW_TAG_array_type:
8501 case DW_TAG_file_type:
8502 case DW_TAG_ptr_to_member_type:
8503 case DW_TAG_set_type:
8504 case DW_TAG_string_type:
8505 case DW_TAG_subroutine_type:
8506 #endif
8507 case DW_TAG_base_type:
8508 case DW_TAG_class_type:
8509 case DW_TAG_interface_type:
8510 case DW_TAG_enumeration_type:
8511 case DW_TAG_structure_type:
8512 case DW_TAG_subrange_type:
8513 case DW_TAG_typedef:
8514 case DW_TAG_union_type:
8515 return 1;
8516 default:
8517 return 0;
8518 }
8519 }
8520
8521 /* Load all DIEs that are interesting for partial symbols into memory. */
8522
8523 static struct partial_die_info *
8524 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8525 int building_psymtab, struct dwarf2_cu *cu)
8526 {
8527 struct partial_die_info *part_die;
8528 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8529 struct abbrev_info *abbrev;
8530 unsigned int bytes_read;
8531 unsigned int load_all = 0;
8532
8533 int nesting_level = 1;
8534
8535 parent_die = NULL;
8536 last_die = NULL;
8537
8538 if (cu->per_cu && cu->per_cu->load_all_dies)
8539 load_all = 1;
8540
8541 cu->partial_dies
8542 = htab_create_alloc_ex (cu->header.length / 12,
8543 partial_die_hash,
8544 partial_die_eq,
8545 NULL,
8546 &cu->comp_unit_obstack,
8547 hashtab_obstack_allocate,
8548 dummy_obstack_deallocate);
8549
8550 part_die = obstack_alloc (&cu->comp_unit_obstack,
8551 sizeof (struct partial_die_info));
8552
8553 while (1)
8554 {
8555 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
8556
8557 /* A NULL abbrev means the end of a series of children. */
8558 if (abbrev == NULL)
8559 {
8560 if (--nesting_level == 0)
8561 {
8562 /* PART_DIE was probably the last thing allocated on the
8563 comp_unit_obstack, so we could call obstack_free
8564 here. We don't do that because the waste is small,
8565 and will be cleaned up when we're done with this
8566 compilation unit. This way, we're also more robust
8567 against other users of the comp_unit_obstack. */
8568 return first_die;
8569 }
8570 info_ptr += bytes_read;
8571 last_die = parent_die;
8572 parent_die = parent_die->die_parent;
8573 continue;
8574 }
8575
8576 /* Check for template arguments. We never save these; if
8577 they're seen, we just mark the parent, and go on our way. */
8578 if (parent_die != NULL
8579 && cu->language == language_cplus
8580 && (abbrev->tag == DW_TAG_template_type_param
8581 || abbrev->tag == DW_TAG_template_value_param))
8582 {
8583 parent_die->has_template_arguments = 1;
8584
8585 if (!load_all)
8586 {
8587 /* We don't need a partial DIE for the template argument. */
8588 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
8589 cu);
8590 continue;
8591 }
8592 }
8593
8594 /* We only recurse into subprograms looking for template arguments.
8595 Skip their other children. */
8596 if (!load_all
8597 && cu->language == language_cplus
8598 && parent_die != NULL
8599 && parent_die->tag == DW_TAG_subprogram)
8600 {
8601 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8602 continue;
8603 }
8604
8605 /* Check whether this DIE is interesting enough to save. Normally
8606 we would not be interested in members here, but there may be
8607 later variables referencing them via DW_AT_specification (for
8608 static members). */
8609 if (!load_all
8610 && !is_type_tag_for_partial (abbrev->tag)
8611 && abbrev->tag != DW_TAG_constant
8612 && abbrev->tag != DW_TAG_enumerator
8613 && abbrev->tag != DW_TAG_subprogram
8614 && abbrev->tag != DW_TAG_lexical_block
8615 && abbrev->tag != DW_TAG_variable
8616 && abbrev->tag != DW_TAG_namespace
8617 && abbrev->tag != DW_TAG_module
8618 && abbrev->tag != DW_TAG_member)
8619 {
8620 /* Otherwise we skip to the next sibling, if any. */
8621 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
8622 continue;
8623 }
8624
8625 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
8626 buffer, info_ptr, cu);
8627
8628 /* This two-pass algorithm for processing partial symbols has a
8629 high cost in cache pressure. Thus, handle some simple cases
8630 here which cover the majority of C partial symbols. DIEs
8631 which neither have specification tags in them, nor could have
8632 specification tags elsewhere pointing at them, can simply be
8633 processed and discarded.
8634
8635 This segment is also optional; scan_partial_symbols and
8636 add_partial_symbol will handle these DIEs if we chain
8637 them in normally. When compilers which do not emit large
8638 quantities of duplicate debug information are more common,
8639 this code can probably be removed. */
8640
8641 /* Any complete simple types at the top level (pretty much all
8642 of them, for a language without namespaces), can be processed
8643 directly. */
8644 if (parent_die == NULL
8645 && part_die->has_specification == 0
8646 && part_die->is_declaration == 0
8647 && (part_die->tag == DW_TAG_typedef
8648 || part_die->tag == DW_TAG_base_type
8649 || part_die->tag == DW_TAG_subrange_type))
8650 {
8651 if (building_psymtab && part_die->name != NULL)
8652 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8653 VAR_DOMAIN, LOC_TYPEDEF,
8654 &cu->objfile->static_psymbols,
8655 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8656 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8657 continue;
8658 }
8659
8660 /* If we're at the second level, and we're an enumerator, and
8661 our parent has no specification (meaning possibly lives in a
8662 namespace elsewhere), then we can add the partial symbol now
8663 instead of queueing it. */
8664 if (part_die->tag == DW_TAG_enumerator
8665 && parent_die != NULL
8666 && parent_die->die_parent == NULL
8667 && parent_die->tag == DW_TAG_enumeration_type
8668 && parent_die->has_specification == 0)
8669 {
8670 if (part_die->name == NULL)
8671 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
8672 else if (building_psymtab)
8673 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
8674 VAR_DOMAIN, LOC_CONST,
8675 (cu->language == language_cplus
8676 || cu->language == language_java)
8677 ? &cu->objfile->global_psymbols
8678 : &cu->objfile->static_psymbols,
8679 0, (CORE_ADDR) 0, cu->language, cu->objfile);
8680
8681 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
8682 continue;
8683 }
8684
8685 /* We'll save this DIE so link it in. */
8686 part_die->die_parent = parent_die;
8687 part_die->die_sibling = NULL;
8688 part_die->die_child = NULL;
8689
8690 if (last_die && last_die == parent_die)
8691 last_die->die_child = part_die;
8692 else if (last_die)
8693 last_die->die_sibling = part_die;
8694
8695 last_die = part_die;
8696
8697 if (first_die == NULL)
8698 first_die = part_die;
8699
8700 /* Maybe add the DIE to the hash table. Not all DIEs that we
8701 find interesting need to be in the hash table, because we
8702 also have the parent/sibling/child chains; only those that we
8703 might refer to by offset later during partial symbol reading.
8704
8705 For now this means things that might have be the target of a
8706 DW_AT_specification, DW_AT_abstract_origin, or
8707 DW_AT_extension. DW_AT_extension will refer only to
8708 namespaces; DW_AT_abstract_origin refers to functions (and
8709 many things under the function DIE, but we do not recurse
8710 into function DIEs during partial symbol reading) and
8711 possibly variables as well; DW_AT_specification refers to
8712 declarations. Declarations ought to have the DW_AT_declaration
8713 flag. It happens that GCC forgets to put it in sometimes, but
8714 only for functions, not for types.
8715
8716 Adding more things than necessary to the hash table is harmless
8717 except for the performance cost. Adding too few will result in
8718 wasted time in find_partial_die, when we reread the compilation
8719 unit with load_all_dies set. */
8720
8721 if (load_all
8722 || abbrev->tag == DW_TAG_constant
8723 || abbrev->tag == DW_TAG_subprogram
8724 || abbrev->tag == DW_TAG_variable
8725 || abbrev->tag == DW_TAG_namespace
8726 || part_die->is_declaration)
8727 {
8728 void **slot;
8729
8730 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
8731 part_die->offset, INSERT);
8732 *slot = part_die;
8733 }
8734
8735 part_die = obstack_alloc (&cu->comp_unit_obstack,
8736 sizeof (struct partial_die_info));
8737
8738 /* For some DIEs we want to follow their children (if any). For C
8739 we have no reason to follow the children of structures; for other
8740 languages we have to, so that we can get at method physnames
8741 to infer fully qualified class names, for DW_AT_specification,
8742 and for C++ template arguments. For C++, we also look one level
8743 inside functions to find template arguments (if the name of the
8744 function does not already contain the template arguments).
8745
8746 For Ada, we need to scan the children of subprograms and lexical
8747 blocks as well because Ada allows the definition of nested
8748 entities that could be interesting for the debugger, such as
8749 nested subprograms for instance. */
8750 if (last_die->has_children
8751 && (load_all
8752 || last_die->tag == DW_TAG_namespace
8753 || last_die->tag == DW_TAG_module
8754 || last_die->tag == DW_TAG_enumeration_type
8755 || (cu->language == language_cplus
8756 && last_die->tag == DW_TAG_subprogram
8757 && (last_die->name == NULL
8758 || strchr (last_die->name, '<') == NULL))
8759 || (cu->language != language_c
8760 && (last_die->tag == DW_TAG_class_type
8761 || last_die->tag == DW_TAG_interface_type
8762 || last_die->tag == DW_TAG_structure_type
8763 || last_die->tag == DW_TAG_union_type))
8764 || (cu->language == language_ada
8765 && (last_die->tag == DW_TAG_subprogram
8766 || last_die->tag == DW_TAG_lexical_block))))
8767 {
8768 nesting_level++;
8769 parent_die = last_die;
8770 continue;
8771 }
8772
8773 /* Otherwise we skip to the next sibling, if any. */
8774 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
8775
8776 /* Back to the top, do it again. */
8777 }
8778 }
8779
8780 /* Read a minimal amount of information into the minimal die structure. */
8781
8782 static gdb_byte *
8783 read_partial_die (struct partial_die_info *part_die,
8784 struct abbrev_info *abbrev,
8785 unsigned int abbrev_len, bfd *abfd,
8786 gdb_byte *buffer, gdb_byte *info_ptr,
8787 struct dwarf2_cu *cu)
8788 {
8789 unsigned int i;
8790 struct attribute attr;
8791 int has_low_pc_attr = 0;
8792 int has_high_pc_attr = 0;
8793
8794 memset (part_die, 0, sizeof (struct partial_die_info));
8795
8796 part_die->offset = info_ptr - buffer;
8797
8798 info_ptr += abbrev_len;
8799
8800 if (abbrev == NULL)
8801 return info_ptr;
8802
8803 part_die->tag = abbrev->tag;
8804 part_die->has_children = abbrev->has_children;
8805
8806 for (i = 0; i < abbrev->num_attrs; ++i)
8807 {
8808 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
8809
8810 /* Store the data if it is of an attribute we want to keep in a
8811 partial symbol table. */
8812 switch (attr.name)
8813 {
8814 case DW_AT_name:
8815 switch (part_die->tag)
8816 {
8817 case DW_TAG_compile_unit:
8818 case DW_TAG_type_unit:
8819 /* Compilation units have a DW_AT_name that is a filename, not
8820 a source language identifier. */
8821 case DW_TAG_enumeration_type:
8822 case DW_TAG_enumerator:
8823 /* These tags always have simple identifiers already; no need
8824 to canonicalize them. */
8825 part_die->name = DW_STRING (&attr);
8826 break;
8827 default:
8828 part_die->name
8829 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
8830 &cu->objfile->objfile_obstack);
8831 break;
8832 }
8833 break;
8834 case DW_AT_linkage_name:
8835 case DW_AT_MIPS_linkage_name:
8836 /* Note that both forms of linkage name might appear. We
8837 assume they will be the same, and we only store the last
8838 one we see. */
8839 if (cu->language == language_ada)
8840 part_die->name = DW_STRING (&attr);
8841 part_die->linkage_name = DW_STRING (&attr);
8842 break;
8843 case DW_AT_low_pc:
8844 has_low_pc_attr = 1;
8845 part_die->lowpc = DW_ADDR (&attr);
8846 break;
8847 case DW_AT_high_pc:
8848 has_high_pc_attr = 1;
8849 part_die->highpc = DW_ADDR (&attr);
8850 break;
8851 case DW_AT_location:
8852 /* Support the .debug_loc offsets */
8853 if (attr_form_is_block (&attr))
8854 {
8855 part_die->locdesc = DW_BLOCK (&attr);
8856 }
8857 else if (attr_form_is_section_offset (&attr))
8858 {
8859 dwarf2_complex_location_expr_complaint ();
8860 }
8861 else
8862 {
8863 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8864 "partial symbol information");
8865 }
8866 break;
8867 case DW_AT_external:
8868 part_die->is_external = DW_UNSND (&attr);
8869 break;
8870 case DW_AT_declaration:
8871 part_die->is_declaration = DW_UNSND (&attr);
8872 break;
8873 case DW_AT_type:
8874 part_die->has_type = 1;
8875 break;
8876 case DW_AT_abstract_origin:
8877 case DW_AT_specification:
8878 case DW_AT_extension:
8879 part_die->has_specification = 1;
8880 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
8881 break;
8882 case DW_AT_sibling:
8883 /* Ignore absolute siblings, they might point outside of
8884 the current compile unit. */
8885 if (attr.form == DW_FORM_ref_addr)
8886 complaint (&symfile_complaints, _("ignoring absolute DW_AT_sibling"));
8887 else
8888 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
8889 break;
8890 case DW_AT_byte_size:
8891 part_die->has_byte_size = 1;
8892 break;
8893 case DW_AT_calling_convention:
8894 /* DWARF doesn't provide a way to identify a program's source-level
8895 entry point. DW_AT_calling_convention attributes are only meant
8896 to describe functions' calling conventions.
8897
8898 However, because it's a necessary piece of information in
8899 Fortran, and because DW_CC_program is the only piece of debugging
8900 information whose definition refers to a 'main program' at all,
8901 several compilers have begun marking Fortran main programs with
8902 DW_CC_program --- even when those functions use the standard
8903 calling conventions.
8904
8905 So until DWARF specifies a way to provide this information and
8906 compilers pick up the new representation, we'll support this
8907 practice. */
8908 if (DW_UNSND (&attr) == DW_CC_program
8909 && cu->language == language_fortran)
8910 {
8911 set_main_name (part_die->name);
8912
8913 /* As this DIE has a static linkage the name would be difficult
8914 to look up later. */
8915 language_of_main = language_fortran;
8916 }
8917 break;
8918 default:
8919 break;
8920 }
8921 }
8922
8923 /* When using the GNU linker, .gnu.linkonce. sections are used to
8924 eliminate duplicate copies of functions and vtables and such.
8925 The linker will arbitrarily choose one and discard the others.
8926 The AT_*_pc values for such functions refer to local labels in
8927 these sections. If the section from that file was discarded, the
8928 labels are not in the output, so the relocs get a value of 0.
8929 If this is a discarded function, mark the pc bounds as invalid,
8930 so that GDB will ignore it. */
8931 if (has_low_pc_attr && has_high_pc_attr
8932 && part_die->lowpc < part_die->highpc
8933 && (part_die->lowpc != 0
8934 || dwarf2_per_objfile->has_section_at_zero))
8935 part_die->has_pc_info = 1;
8936
8937 return info_ptr;
8938 }
8939
8940 /* Find a cached partial DIE at OFFSET in CU. */
8941
8942 static struct partial_die_info *
8943 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
8944 {
8945 struct partial_die_info *lookup_die = NULL;
8946 struct partial_die_info part_die;
8947
8948 part_die.offset = offset;
8949 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
8950
8951 return lookup_die;
8952 }
8953
8954 /* Find a partial DIE at OFFSET, which may or may not be in CU,
8955 except in the case of .debug_types DIEs which do not reference
8956 outside their CU (they do however referencing other types via
8957 DW_FORM_sig8). */
8958
8959 static struct partial_die_info *
8960 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
8961 {
8962 struct dwarf2_per_cu_data *per_cu = NULL;
8963 struct partial_die_info *pd = NULL;
8964
8965 if (cu->per_cu->from_debug_types)
8966 {
8967 pd = find_partial_die_in_comp_unit (offset, cu);
8968 if (pd != NULL)
8969 return pd;
8970 goto not_found;
8971 }
8972
8973 if (offset_in_cu_p (&cu->header, offset))
8974 {
8975 pd = find_partial_die_in_comp_unit (offset, cu);
8976 if (pd != NULL)
8977 return pd;
8978 }
8979
8980 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
8981
8982 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
8983 load_partial_comp_unit (per_cu, cu->objfile);
8984
8985 per_cu->cu->last_used = 0;
8986 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
8987
8988 if (pd == NULL && per_cu->load_all_dies == 0)
8989 {
8990 struct cleanup *back_to;
8991 struct partial_die_info comp_unit_die;
8992 struct abbrev_info *abbrev;
8993 unsigned int bytes_read;
8994 char *info_ptr;
8995
8996 per_cu->load_all_dies = 1;
8997
8998 /* Re-read the DIEs. */
8999 back_to = make_cleanup (null_cleanup, 0);
9000 if (per_cu->cu->dwarf2_abbrevs == NULL)
9001 {
9002 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9003 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9004 }
9005 info_ptr = (dwarf2_per_objfile->info.buffer
9006 + per_cu->cu->header.offset
9007 + per_cu->cu->header.first_die_offset);
9008 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9009 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9010 per_cu->cu->objfile->obfd,
9011 dwarf2_per_objfile->info.buffer, info_ptr,
9012 per_cu->cu);
9013 if (comp_unit_die.has_children)
9014 load_partial_dies (per_cu->cu->objfile->obfd,
9015 dwarf2_per_objfile->info.buffer, info_ptr,
9016 0, per_cu->cu);
9017 do_cleanups (back_to);
9018
9019 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9020 }
9021
9022 not_found:
9023
9024 if (pd == NULL)
9025 internal_error (__FILE__, __LINE__,
9026 _("could not find partial DIE 0x%x in cache [from module %s]\n"),
9027 offset, bfd_get_filename (cu->objfile->obfd));
9028 return pd;
9029 }
9030
9031 /* See if we can figure out if the class lives in a namespace. We do
9032 this by looking for a member function; its demangled name will
9033 contain namespace info, if there is any. */
9034
9035 static void
9036 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9037 struct dwarf2_cu *cu)
9038 {
9039 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9040 what template types look like, because the demangler
9041 frequently doesn't give the same name as the debug info. We
9042 could fix this by only using the demangled name to get the
9043 prefix (but see comment in read_structure_type). */
9044
9045 struct partial_die_info *real_pdi;
9046 struct partial_die_info *child_pdi;
9047
9048 /* If this DIE (this DIE's specification, if any) has a parent, then
9049 we should not do this. We'll prepend the parent's fully qualified
9050 name when we create the partial symbol. */
9051
9052 real_pdi = struct_pdi;
9053 while (real_pdi->has_specification)
9054 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9055
9056 if (real_pdi->die_parent != NULL)
9057 return;
9058
9059 for (child_pdi = struct_pdi->die_child;
9060 child_pdi != NULL;
9061 child_pdi = child_pdi->die_sibling)
9062 {
9063 if (child_pdi->tag == DW_TAG_subprogram
9064 && child_pdi->linkage_name != NULL)
9065 {
9066 char *actual_class_name
9067 = language_class_name_from_physname (cu->language_defn,
9068 child_pdi->linkage_name);
9069 if (actual_class_name != NULL)
9070 {
9071 struct_pdi->name
9072 = obsavestring (actual_class_name,
9073 strlen (actual_class_name),
9074 &cu->objfile->objfile_obstack);
9075 xfree (actual_class_name);
9076 }
9077 break;
9078 }
9079 }
9080 }
9081
9082 /* Adjust PART_DIE before generating a symbol for it. This function
9083 may set the is_external flag or change the DIE's name. */
9084
9085 static void
9086 fixup_partial_die (struct partial_die_info *part_die,
9087 struct dwarf2_cu *cu)
9088 {
9089 /* Once we've fixed up a die, there's no point in doing so again.
9090 This also avoids a memory leak if we were to call
9091 guess_partial_die_structure_name multiple times. */
9092 if (part_die->fixup_called)
9093 return;
9094
9095 /* If we found a reference attribute and the DIE has no name, try
9096 to find a name in the referred to DIE. */
9097
9098 if (part_die->name == NULL && part_die->has_specification)
9099 {
9100 struct partial_die_info *spec_die;
9101
9102 spec_die = find_partial_die (part_die->spec_offset, cu);
9103
9104 fixup_partial_die (spec_die, cu);
9105
9106 if (spec_die->name)
9107 {
9108 part_die->name = spec_die->name;
9109
9110 /* Copy DW_AT_external attribute if it is set. */
9111 if (spec_die->is_external)
9112 part_die->is_external = spec_die->is_external;
9113 }
9114 }
9115
9116 /* Set default names for some unnamed DIEs. */
9117
9118 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9119 part_die->name = "(anonymous namespace)";
9120
9121 /* If there is no parent die to provide a namespace, and there are
9122 children, see if we can determine the namespace from their linkage
9123 name.
9124 NOTE: We need to do this even if cu->has_namespace_info != 0.
9125 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9126 if (cu->language == language_cplus
9127 && dwarf2_per_objfile->types.asection != NULL
9128 && part_die->die_parent == NULL
9129 && part_die->has_children
9130 && (part_die->tag == DW_TAG_class_type
9131 || part_die->tag == DW_TAG_structure_type
9132 || part_die->tag == DW_TAG_union_type))
9133 guess_partial_die_structure_name (part_die, cu);
9134
9135 part_die->fixup_called = 1;
9136 }
9137
9138 /* Read an attribute value described by an attribute form. */
9139
9140 static gdb_byte *
9141 read_attribute_value (struct attribute *attr, unsigned form,
9142 bfd *abfd, gdb_byte *info_ptr,
9143 struct dwarf2_cu *cu)
9144 {
9145 struct comp_unit_head *cu_header = &cu->header;
9146 unsigned int bytes_read;
9147 struct dwarf_block *blk;
9148
9149 attr->form = form;
9150 switch (form)
9151 {
9152 case DW_FORM_ref_addr:
9153 if (cu->header.version == 2)
9154 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9155 else
9156 DW_ADDR (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9157 info_ptr += bytes_read;
9158 break;
9159 case DW_FORM_addr:
9160 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9161 info_ptr += bytes_read;
9162 break;
9163 case DW_FORM_block2:
9164 blk = dwarf_alloc_block (cu);
9165 blk->size = read_2_bytes (abfd, info_ptr);
9166 info_ptr += 2;
9167 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9168 info_ptr += blk->size;
9169 DW_BLOCK (attr) = blk;
9170 break;
9171 case DW_FORM_block4:
9172 blk = dwarf_alloc_block (cu);
9173 blk->size = read_4_bytes (abfd, info_ptr);
9174 info_ptr += 4;
9175 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9176 info_ptr += blk->size;
9177 DW_BLOCK (attr) = blk;
9178 break;
9179 case DW_FORM_data2:
9180 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9181 info_ptr += 2;
9182 break;
9183 case DW_FORM_data4:
9184 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9185 info_ptr += 4;
9186 break;
9187 case DW_FORM_data8:
9188 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9189 info_ptr += 8;
9190 break;
9191 case DW_FORM_sec_offset:
9192 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9193 info_ptr += bytes_read;
9194 break;
9195 case DW_FORM_string:
9196 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9197 DW_STRING_IS_CANONICAL (attr) = 0;
9198 info_ptr += bytes_read;
9199 break;
9200 case DW_FORM_strp:
9201 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9202 &bytes_read);
9203 DW_STRING_IS_CANONICAL (attr) = 0;
9204 info_ptr += bytes_read;
9205 break;
9206 case DW_FORM_exprloc:
9207 case DW_FORM_block:
9208 blk = dwarf_alloc_block (cu);
9209 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9210 info_ptr += bytes_read;
9211 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9212 info_ptr += blk->size;
9213 DW_BLOCK (attr) = blk;
9214 break;
9215 case DW_FORM_block1:
9216 blk = dwarf_alloc_block (cu);
9217 blk->size = read_1_byte (abfd, info_ptr);
9218 info_ptr += 1;
9219 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9220 info_ptr += blk->size;
9221 DW_BLOCK (attr) = blk;
9222 break;
9223 case DW_FORM_data1:
9224 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9225 info_ptr += 1;
9226 break;
9227 case DW_FORM_flag:
9228 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9229 info_ptr += 1;
9230 break;
9231 case DW_FORM_flag_present:
9232 DW_UNSND (attr) = 1;
9233 break;
9234 case DW_FORM_sdata:
9235 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9236 info_ptr += bytes_read;
9237 break;
9238 case DW_FORM_udata:
9239 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9240 info_ptr += bytes_read;
9241 break;
9242 case DW_FORM_ref1:
9243 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9244 info_ptr += 1;
9245 break;
9246 case DW_FORM_ref2:
9247 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9248 info_ptr += 2;
9249 break;
9250 case DW_FORM_ref4:
9251 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9252 info_ptr += 4;
9253 break;
9254 case DW_FORM_ref8:
9255 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9256 info_ptr += 8;
9257 break;
9258 case DW_FORM_sig8:
9259 /* Convert the signature to something we can record in DW_UNSND
9260 for later lookup.
9261 NOTE: This is NULL if the type wasn't found. */
9262 DW_SIGNATURED_TYPE (attr) =
9263 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9264 info_ptr += 8;
9265 break;
9266 case DW_FORM_ref_udata:
9267 DW_ADDR (attr) = (cu->header.offset
9268 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9269 info_ptr += bytes_read;
9270 break;
9271 case DW_FORM_indirect:
9272 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9273 info_ptr += bytes_read;
9274 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9275 break;
9276 default:
9277 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9278 dwarf_form_name (form),
9279 bfd_get_filename (abfd));
9280 }
9281
9282 /* We have seen instances where the compiler tried to emit a byte
9283 size attribute of -1 which ended up being encoded as an unsigned
9284 0xffffffff. Although 0xffffffff is technically a valid size value,
9285 an object of this size seems pretty unlikely so we can relatively
9286 safely treat these cases as if the size attribute was invalid and
9287 treat them as zero by default. */
9288 if (attr->name == DW_AT_byte_size
9289 && form == DW_FORM_data4
9290 && DW_UNSND (attr) >= 0xffffffff)
9291 {
9292 complaint
9293 (&symfile_complaints,
9294 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9295 hex_string (DW_UNSND (attr)));
9296 DW_UNSND (attr) = 0;
9297 }
9298
9299 return info_ptr;
9300 }
9301
9302 /* Read an attribute described by an abbreviated attribute. */
9303
9304 static gdb_byte *
9305 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9306 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9307 {
9308 attr->name = abbrev->name;
9309 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9310 }
9311
9312 /* read dwarf information from a buffer */
9313
9314 static unsigned int
9315 read_1_byte (bfd *abfd, gdb_byte *buf)
9316 {
9317 return bfd_get_8 (abfd, buf);
9318 }
9319
9320 static int
9321 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9322 {
9323 return bfd_get_signed_8 (abfd, buf);
9324 }
9325
9326 static unsigned int
9327 read_2_bytes (bfd *abfd, gdb_byte *buf)
9328 {
9329 return bfd_get_16 (abfd, buf);
9330 }
9331
9332 static int
9333 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9334 {
9335 return bfd_get_signed_16 (abfd, buf);
9336 }
9337
9338 static unsigned int
9339 read_4_bytes (bfd *abfd, gdb_byte *buf)
9340 {
9341 return bfd_get_32 (abfd, buf);
9342 }
9343
9344 static int
9345 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9346 {
9347 return bfd_get_signed_32 (abfd, buf);
9348 }
9349
9350 static ULONGEST
9351 read_8_bytes (bfd *abfd, gdb_byte *buf)
9352 {
9353 return bfd_get_64 (abfd, buf);
9354 }
9355
9356 static CORE_ADDR
9357 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9358 unsigned int *bytes_read)
9359 {
9360 struct comp_unit_head *cu_header = &cu->header;
9361 CORE_ADDR retval = 0;
9362
9363 if (cu_header->signed_addr_p)
9364 {
9365 switch (cu_header->addr_size)
9366 {
9367 case 2:
9368 retval = bfd_get_signed_16 (abfd, buf);
9369 break;
9370 case 4:
9371 retval = bfd_get_signed_32 (abfd, buf);
9372 break;
9373 case 8:
9374 retval = bfd_get_signed_64 (abfd, buf);
9375 break;
9376 default:
9377 internal_error (__FILE__, __LINE__,
9378 _("read_address: bad switch, signed [in module %s]"),
9379 bfd_get_filename (abfd));
9380 }
9381 }
9382 else
9383 {
9384 switch (cu_header->addr_size)
9385 {
9386 case 2:
9387 retval = bfd_get_16 (abfd, buf);
9388 break;
9389 case 4:
9390 retval = bfd_get_32 (abfd, buf);
9391 break;
9392 case 8:
9393 retval = bfd_get_64 (abfd, buf);
9394 break;
9395 default:
9396 internal_error (__FILE__, __LINE__,
9397 _("read_address: bad switch, unsigned [in module %s]"),
9398 bfd_get_filename (abfd));
9399 }
9400 }
9401
9402 *bytes_read = cu_header->addr_size;
9403 return retval;
9404 }
9405
9406 /* Read the initial length from a section. The (draft) DWARF 3
9407 specification allows the initial length to take up either 4 bytes
9408 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9409 bytes describe the length and all offsets will be 8 bytes in length
9410 instead of 4.
9411
9412 An older, non-standard 64-bit format is also handled by this
9413 function. The older format in question stores the initial length
9414 as an 8-byte quantity without an escape value. Lengths greater
9415 than 2^32 aren't very common which means that the initial 4 bytes
9416 is almost always zero. Since a length value of zero doesn't make
9417 sense for the 32-bit format, this initial zero can be considered to
9418 be an escape value which indicates the presence of the older 64-bit
9419 format. As written, the code can't detect (old format) lengths
9420 greater than 4GB. If it becomes necessary to handle lengths
9421 somewhat larger than 4GB, we could allow other small values (such
9422 as the non-sensical values of 1, 2, and 3) to also be used as
9423 escape values indicating the presence of the old format.
9424
9425 The value returned via bytes_read should be used to increment the
9426 relevant pointer after calling read_initial_length().
9427
9428 [ Note: read_initial_length() and read_offset() are based on the
9429 document entitled "DWARF Debugging Information Format", revision
9430 3, draft 8, dated November 19, 2001. This document was obtained
9431 from:
9432
9433 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9434
9435 This document is only a draft and is subject to change. (So beware.)
9436
9437 Details regarding the older, non-standard 64-bit format were
9438 determined empirically by examining 64-bit ELF files produced by
9439 the SGI toolchain on an IRIX 6.5 machine.
9440
9441 - Kevin, July 16, 2002
9442 ] */
9443
9444 static LONGEST
9445 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9446 {
9447 LONGEST length = bfd_get_32 (abfd, buf);
9448
9449 if (length == 0xffffffff)
9450 {
9451 length = bfd_get_64 (abfd, buf + 4);
9452 *bytes_read = 12;
9453 }
9454 else if (length == 0)
9455 {
9456 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
9457 length = bfd_get_64 (abfd, buf);
9458 *bytes_read = 8;
9459 }
9460 else
9461 {
9462 *bytes_read = 4;
9463 }
9464
9465 return length;
9466 }
9467
9468 /* Cover function for read_initial_length.
9469 Returns the length of the object at BUF, and stores the size of the
9470 initial length in *BYTES_READ and stores the size that offsets will be in
9471 *OFFSET_SIZE.
9472 If the initial length size is not equivalent to that specified in
9473 CU_HEADER then issue a complaint.
9474 This is useful when reading non-comp-unit headers. */
9475
9476 static LONGEST
9477 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9478 const struct comp_unit_head *cu_header,
9479 unsigned int *bytes_read,
9480 unsigned int *offset_size)
9481 {
9482 LONGEST length = read_initial_length (abfd, buf, bytes_read);
9483
9484 gdb_assert (cu_header->initial_length_size == 4
9485 || cu_header->initial_length_size == 8
9486 || cu_header->initial_length_size == 12);
9487
9488 if (cu_header->initial_length_size != *bytes_read)
9489 complaint (&symfile_complaints,
9490 _("intermixed 32-bit and 64-bit DWARF sections"));
9491
9492 *offset_size = (*bytes_read == 4) ? 4 : 8;
9493 return length;
9494 }
9495
9496 /* Read an offset from the data stream. The size of the offset is
9497 given by cu_header->offset_size. */
9498
9499 static LONGEST
9500 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
9501 unsigned int *bytes_read)
9502 {
9503 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
9504
9505 *bytes_read = cu_header->offset_size;
9506 return offset;
9507 }
9508
9509 /* Read an offset from the data stream. */
9510
9511 static LONGEST
9512 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
9513 {
9514 LONGEST retval = 0;
9515
9516 switch (offset_size)
9517 {
9518 case 4:
9519 retval = bfd_get_32 (abfd, buf);
9520 break;
9521 case 8:
9522 retval = bfd_get_64 (abfd, buf);
9523 break;
9524 default:
9525 internal_error (__FILE__, __LINE__,
9526 _("read_offset_1: bad switch [in module %s]"),
9527 bfd_get_filename (abfd));
9528 }
9529
9530 return retval;
9531 }
9532
9533 static gdb_byte *
9534 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
9535 {
9536 /* If the size of a host char is 8 bits, we can return a pointer
9537 to the buffer, otherwise we have to copy the data to a buffer
9538 allocated on the temporary obstack. */
9539 gdb_assert (HOST_CHAR_BIT == 8);
9540 return buf;
9541 }
9542
9543 static char *
9544 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9545 {
9546 /* If the size of a host char is 8 bits, we can return a pointer
9547 to the string, otherwise we have to copy the string to a buffer
9548 allocated on the temporary obstack. */
9549 gdb_assert (HOST_CHAR_BIT == 8);
9550 if (*buf == '\0')
9551 {
9552 *bytes_read_ptr = 1;
9553 return NULL;
9554 }
9555 *bytes_read_ptr = strlen ((char *) buf) + 1;
9556 return (char *) buf;
9557 }
9558
9559 static char *
9560 read_indirect_string (bfd *abfd, gdb_byte *buf,
9561 const struct comp_unit_head *cu_header,
9562 unsigned int *bytes_read_ptr)
9563 {
9564 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
9565
9566 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
9567 if (dwarf2_per_objfile->str.buffer == NULL)
9568 {
9569 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
9570 bfd_get_filename (abfd));
9571 return NULL;
9572 }
9573 if (str_offset >= dwarf2_per_objfile->str.size)
9574 {
9575 error (_("DW_FORM_strp pointing outside of .debug_str section [in module %s]"),
9576 bfd_get_filename (abfd));
9577 return NULL;
9578 }
9579 gdb_assert (HOST_CHAR_BIT == 8);
9580 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
9581 return NULL;
9582 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
9583 }
9584
9585 static unsigned long
9586 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9587 {
9588 unsigned long result;
9589 unsigned int num_read;
9590 int i, shift;
9591 unsigned char byte;
9592
9593 result = 0;
9594 shift = 0;
9595 num_read = 0;
9596 i = 0;
9597 while (1)
9598 {
9599 byte = bfd_get_8 (abfd, buf);
9600 buf++;
9601 num_read++;
9602 result |= ((unsigned long)(byte & 127) << shift);
9603 if ((byte & 128) == 0)
9604 {
9605 break;
9606 }
9607 shift += 7;
9608 }
9609 *bytes_read_ptr = num_read;
9610 return result;
9611 }
9612
9613 static long
9614 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
9615 {
9616 long result;
9617 int i, shift, num_read;
9618 unsigned char byte;
9619
9620 result = 0;
9621 shift = 0;
9622 num_read = 0;
9623 i = 0;
9624 while (1)
9625 {
9626 byte = bfd_get_8 (abfd, buf);
9627 buf++;
9628 num_read++;
9629 result |= ((long)(byte & 127) << shift);
9630 shift += 7;
9631 if ((byte & 128) == 0)
9632 {
9633 break;
9634 }
9635 }
9636 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
9637 result |= -(((long)1) << shift);
9638 *bytes_read_ptr = num_read;
9639 return result;
9640 }
9641
9642 /* Return a pointer to just past the end of an LEB128 number in BUF. */
9643
9644 static gdb_byte *
9645 skip_leb128 (bfd *abfd, gdb_byte *buf)
9646 {
9647 int byte;
9648
9649 while (1)
9650 {
9651 byte = bfd_get_8 (abfd, buf);
9652 buf++;
9653 if ((byte & 128) == 0)
9654 return buf;
9655 }
9656 }
9657
9658 static void
9659 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
9660 {
9661 switch (lang)
9662 {
9663 case DW_LANG_C89:
9664 case DW_LANG_C99:
9665 case DW_LANG_C:
9666 cu->language = language_c;
9667 break;
9668 case DW_LANG_C_plus_plus:
9669 cu->language = language_cplus;
9670 break;
9671 case DW_LANG_D:
9672 cu->language = language_d;
9673 break;
9674 case DW_LANG_Fortran77:
9675 case DW_LANG_Fortran90:
9676 case DW_LANG_Fortran95:
9677 cu->language = language_fortran;
9678 break;
9679 case DW_LANG_Mips_Assembler:
9680 cu->language = language_asm;
9681 break;
9682 case DW_LANG_Java:
9683 cu->language = language_java;
9684 break;
9685 case DW_LANG_Ada83:
9686 case DW_LANG_Ada95:
9687 cu->language = language_ada;
9688 break;
9689 case DW_LANG_Modula2:
9690 cu->language = language_m2;
9691 break;
9692 case DW_LANG_Pascal83:
9693 cu->language = language_pascal;
9694 break;
9695 case DW_LANG_ObjC:
9696 cu->language = language_objc;
9697 break;
9698 case DW_LANG_Cobol74:
9699 case DW_LANG_Cobol85:
9700 default:
9701 cu->language = language_minimal;
9702 break;
9703 }
9704 cu->language_defn = language_def (cu->language);
9705 }
9706
9707 /* Return the named attribute or NULL if not there. */
9708
9709 static struct attribute *
9710 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
9711 {
9712 unsigned int i;
9713 struct attribute *spec = NULL;
9714
9715 for (i = 0; i < die->num_attrs; ++i)
9716 {
9717 if (die->attrs[i].name == name)
9718 return &die->attrs[i];
9719 if (die->attrs[i].name == DW_AT_specification
9720 || die->attrs[i].name == DW_AT_abstract_origin)
9721 spec = &die->attrs[i];
9722 }
9723
9724 if (spec)
9725 {
9726 die = follow_die_ref (die, spec, &cu);
9727 return dwarf2_attr (die, name, cu);
9728 }
9729
9730 return NULL;
9731 }
9732
9733 /* Return the named attribute or NULL if not there,
9734 but do not follow DW_AT_specification, etc.
9735 This is for use in contexts where we're reading .debug_types dies.
9736 Following DW_AT_specification, DW_AT_abstract_origin will take us
9737 back up the chain, and we want to go down. */
9738
9739 static struct attribute *
9740 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
9741 struct dwarf2_cu *cu)
9742 {
9743 unsigned int i;
9744
9745 for (i = 0; i < die->num_attrs; ++i)
9746 if (die->attrs[i].name == name)
9747 return &die->attrs[i];
9748
9749 return NULL;
9750 }
9751
9752 /* Return non-zero iff the attribute NAME is defined for the given DIE,
9753 and holds a non-zero value. This function should only be used for
9754 DW_FORM_flag or DW_FORM_flag_present attributes. */
9755
9756 static int
9757 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
9758 {
9759 struct attribute *attr = dwarf2_attr (die, name, cu);
9760
9761 return (attr && DW_UNSND (attr));
9762 }
9763
9764 static int
9765 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
9766 {
9767 /* A DIE is a declaration if it has a DW_AT_declaration attribute
9768 which value is non-zero. However, we have to be careful with
9769 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
9770 (via dwarf2_flag_true_p) follows this attribute. So we may
9771 end up accidently finding a declaration attribute that belongs
9772 to a different DIE referenced by the specification attribute,
9773 even though the given DIE does not have a declaration attribute. */
9774 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
9775 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
9776 }
9777
9778 /* Return the die giving the specification for DIE, if there is
9779 one. *SPEC_CU is the CU containing DIE on input, and the CU
9780 containing the return value on output. If there is no
9781 specification, but there is an abstract origin, that is
9782 returned. */
9783
9784 static struct die_info *
9785 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
9786 {
9787 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
9788 *spec_cu);
9789
9790 if (spec_attr == NULL)
9791 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
9792
9793 if (spec_attr == NULL)
9794 return NULL;
9795 else
9796 return follow_die_ref (die, spec_attr, spec_cu);
9797 }
9798
9799 /* Free the line_header structure *LH, and any arrays and strings it
9800 refers to.
9801 NOTE: This is also used as a "cleanup" function. */
9802
9803 static void
9804 free_line_header (struct line_header *lh)
9805 {
9806 if (lh->standard_opcode_lengths)
9807 xfree (lh->standard_opcode_lengths);
9808
9809 /* Remember that all the lh->file_names[i].name pointers are
9810 pointers into debug_line_buffer, and don't need to be freed. */
9811 if (lh->file_names)
9812 xfree (lh->file_names);
9813
9814 /* Similarly for the include directory names. */
9815 if (lh->include_dirs)
9816 xfree (lh->include_dirs);
9817
9818 xfree (lh);
9819 }
9820
9821 /* Add an entry to LH's include directory table. */
9822
9823 static void
9824 add_include_dir (struct line_header *lh, char *include_dir)
9825 {
9826 /* Grow the array if necessary. */
9827 if (lh->include_dirs_size == 0)
9828 {
9829 lh->include_dirs_size = 1; /* for testing */
9830 lh->include_dirs = xmalloc (lh->include_dirs_size
9831 * sizeof (*lh->include_dirs));
9832 }
9833 else if (lh->num_include_dirs >= lh->include_dirs_size)
9834 {
9835 lh->include_dirs_size *= 2;
9836 lh->include_dirs = xrealloc (lh->include_dirs,
9837 (lh->include_dirs_size
9838 * sizeof (*lh->include_dirs)));
9839 }
9840
9841 lh->include_dirs[lh->num_include_dirs++] = include_dir;
9842 }
9843
9844 /* Add an entry to LH's file name table. */
9845
9846 static void
9847 add_file_name (struct line_header *lh,
9848 char *name,
9849 unsigned int dir_index,
9850 unsigned int mod_time,
9851 unsigned int length)
9852 {
9853 struct file_entry *fe;
9854
9855 /* Grow the array if necessary. */
9856 if (lh->file_names_size == 0)
9857 {
9858 lh->file_names_size = 1; /* for testing */
9859 lh->file_names = xmalloc (lh->file_names_size
9860 * sizeof (*lh->file_names));
9861 }
9862 else if (lh->num_file_names >= lh->file_names_size)
9863 {
9864 lh->file_names_size *= 2;
9865 lh->file_names = xrealloc (lh->file_names,
9866 (lh->file_names_size
9867 * sizeof (*lh->file_names)));
9868 }
9869
9870 fe = &lh->file_names[lh->num_file_names++];
9871 fe->name = name;
9872 fe->dir_index = dir_index;
9873 fe->mod_time = mod_time;
9874 fe->length = length;
9875 fe->included_p = 0;
9876 fe->symtab = NULL;
9877 }
9878
9879 /* Read the statement program header starting at OFFSET in
9880 .debug_line, according to the endianness of ABFD. Return a pointer
9881 to a struct line_header, allocated using xmalloc.
9882
9883 NOTE: the strings in the include directory and file name tables of
9884 the returned object point into debug_line_buffer, and must not be
9885 freed. */
9886
9887 static struct line_header *
9888 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
9889 struct dwarf2_cu *cu)
9890 {
9891 struct cleanup *back_to;
9892 struct line_header *lh;
9893 gdb_byte *line_ptr;
9894 unsigned int bytes_read, offset_size;
9895 int i;
9896 char *cur_dir, *cur_file;
9897
9898 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
9899 if (dwarf2_per_objfile->line.buffer == NULL)
9900 {
9901 complaint (&symfile_complaints, _("missing .debug_line section"));
9902 return 0;
9903 }
9904
9905 /* Make sure that at least there's room for the total_length field.
9906 That could be 12 bytes long, but we're just going to fudge that. */
9907 if (offset + 4 >= dwarf2_per_objfile->line.size)
9908 {
9909 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9910 return 0;
9911 }
9912
9913 lh = xmalloc (sizeof (*lh));
9914 memset (lh, 0, sizeof (*lh));
9915 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
9916 (void *) lh);
9917
9918 line_ptr = dwarf2_per_objfile->line.buffer + offset;
9919
9920 /* Read in the header. */
9921 lh->total_length =
9922 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
9923 &bytes_read, &offset_size);
9924 line_ptr += bytes_read;
9925 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
9926 + dwarf2_per_objfile->line.size))
9927 {
9928 dwarf2_statement_list_fits_in_line_number_section_complaint ();
9929 return 0;
9930 }
9931 lh->statement_program_end = line_ptr + lh->total_length;
9932 lh->version = read_2_bytes (abfd, line_ptr);
9933 line_ptr += 2;
9934 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
9935 line_ptr += offset_size;
9936 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
9937 line_ptr += 1;
9938 if (lh->version >= 4)
9939 {
9940 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
9941 line_ptr += 1;
9942 }
9943 else
9944 lh->maximum_ops_per_instruction = 1;
9945
9946 if (lh->maximum_ops_per_instruction == 0)
9947 {
9948 lh->maximum_ops_per_instruction = 1;
9949 complaint (&symfile_complaints,
9950 _("invalid maximum_ops_per_instruction in `.debug_line' section"));
9951 }
9952
9953 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
9954 line_ptr += 1;
9955 lh->line_base = read_1_signed_byte (abfd, line_ptr);
9956 line_ptr += 1;
9957 lh->line_range = read_1_byte (abfd, line_ptr);
9958 line_ptr += 1;
9959 lh->opcode_base = read_1_byte (abfd, line_ptr);
9960 line_ptr += 1;
9961 lh->standard_opcode_lengths
9962 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
9963
9964 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
9965 for (i = 1; i < lh->opcode_base; ++i)
9966 {
9967 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
9968 line_ptr += 1;
9969 }
9970
9971 /* Read directory table. */
9972 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9973 {
9974 line_ptr += bytes_read;
9975 add_include_dir (lh, cur_dir);
9976 }
9977 line_ptr += bytes_read;
9978
9979 /* Read file name table. */
9980 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
9981 {
9982 unsigned int dir_index, mod_time, length;
9983
9984 line_ptr += bytes_read;
9985 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9986 line_ptr += bytes_read;
9987 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9988 line_ptr += bytes_read;
9989 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
9990 line_ptr += bytes_read;
9991
9992 add_file_name (lh, cur_file, dir_index, mod_time, length);
9993 }
9994 line_ptr += bytes_read;
9995 lh->statement_program_start = line_ptr;
9996
9997 if (line_ptr > (dwarf2_per_objfile->line.buffer
9998 + dwarf2_per_objfile->line.size))
9999 complaint (&symfile_complaints,
10000 _("line number info header doesn't fit in `.debug_line' section"));
10001
10002 discard_cleanups (back_to);
10003 return lh;
10004 }
10005
10006 /* This function exists to work around a bug in certain compilers
10007 (particularly GCC 2.95), in which the first line number marker of a
10008 function does not show up until after the prologue, right before
10009 the second line number marker. This function shifts ADDRESS down
10010 to the beginning of the function if necessary, and is called on
10011 addresses passed to record_line. */
10012
10013 static CORE_ADDR
10014 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10015 {
10016 struct function_range *fn;
10017
10018 /* Find the function_range containing address. */
10019 if (!cu->first_fn)
10020 return address;
10021
10022 if (!cu->cached_fn)
10023 cu->cached_fn = cu->first_fn;
10024
10025 fn = cu->cached_fn;
10026 while (fn)
10027 if (fn->lowpc <= address && fn->highpc > address)
10028 goto found;
10029 else
10030 fn = fn->next;
10031
10032 fn = cu->first_fn;
10033 while (fn && fn != cu->cached_fn)
10034 if (fn->lowpc <= address && fn->highpc > address)
10035 goto found;
10036 else
10037 fn = fn->next;
10038
10039 return address;
10040
10041 found:
10042 if (fn->seen_line)
10043 return address;
10044 if (address != fn->lowpc)
10045 complaint (&symfile_complaints,
10046 _("misplaced first line number at 0x%lx for '%s'"),
10047 (unsigned long) address, fn->name);
10048 fn->seen_line = 1;
10049 return fn->lowpc;
10050 }
10051
10052 /* Subroutine of dwarf_decode_lines to simplify it.
10053 Return the file name of the psymtab for included file FILE_INDEX
10054 in line header LH of PST.
10055 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10056 If space for the result is malloc'd, it will be freed by a cleanup.
10057 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10058
10059 static char *
10060 psymtab_include_file_name (const struct line_header *lh, int file_index,
10061 const struct partial_symtab *pst,
10062 const char *comp_dir)
10063 {
10064 const struct file_entry fe = lh->file_names [file_index];
10065 char *include_name = fe.name;
10066 char *include_name_to_compare = include_name;
10067 char *dir_name = NULL;
10068 const char *pst_filename;
10069 char *copied_name = NULL;
10070 int file_is_pst;
10071
10072 if (fe.dir_index)
10073 dir_name = lh->include_dirs[fe.dir_index - 1];
10074
10075 if (!IS_ABSOLUTE_PATH (include_name)
10076 && (dir_name != NULL || comp_dir != NULL))
10077 {
10078 /* Avoid creating a duplicate psymtab for PST.
10079 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10080 Before we do the comparison, however, we need to account
10081 for DIR_NAME and COMP_DIR.
10082 First prepend dir_name (if non-NULL). If we still don't
10083 have an absolute path prepend comp_dir (if non-NULL).
10084 However, the directory we record in the include-file's
10085 psymtab does not contain COMP_DIR (to match the
10086 corresponding symtab(s)).
10087
10088 Example:
10089
10090 bash$ cd /tmp
10091 bash$ gcc -g ./hello.c
10092 include_name = "hello.c"
10093 dir_name = "."
10094 DW_AT_comp_dir = comp_dir = "/tmp"
10095 DW_AT_name = "./hello.c" */
10096
10097 if (dir_name != NULL)
10098 {
10099 include_name = concat (dir_name, SLASH_STRING,
10100 include_name, (char *)NULL);
10101 include_name_to_compare = include_name;
10102 make_cleanup (xfree, include_name);
10103 }
10104 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10105 {
10106 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10107 include_name, (char *)NULL);
10108 }
10109 }
10110
10111 pst_filename = pst->filename;
10112 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10113 {
10114 copied_name = concat (pst->dirname, SLASH_STRING,
10115 pst_filename, (char *)NULL);
10116 pst_filename = copied_name;
10117 }
10118
10119 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10120
10121 if (include_name_to_compare != include_name)
10122 xfree (include_name_to_compare);
10123 if (copied_name != NULL)
10124 xfree (copied_name);
10125
10126 if (file_is_pst)
10127 return NULL;
10128 return include_name;
10129 }
10130
10131 /* Decode the Line Number Program (LNP) for the given line_header
10132 structure and CU. The actual information extracted and the type
10133 of structures created from the LNP depends on the value of PST.
10134
10135 1. If PST is NULL, then this procedure uses the data from the program
10136 to create all necessary symbol tables, and their linetables.
10137
10138 2. If PST is not NULL, this procedure reads the program to determine
10139 the list of files included by the unit represented by PST, and
10140 builds all the associated partial symbol tables.
10141
10142 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10143 It is used for relative paths in the line table.
10144 NOTE: When processing partial symtabs (pst != NULL),
10145 comp_dir == pst->dirname.
10146
10147 NOTE: It is important that psymtabs have the same file name (via strcmp)
10148 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10149 symtab we don't use it in the name of the psymtabs we create.
10150 E.g. expand_line_sal requires this when finding psymtabs to expand.
10151 A good testcase for this is mb-inline.exp. */
10152
10153 static void
10154 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10155 struct dwarf2_cu *cu, struct partial_symtab *pst)
10156 {
10157 gdb_byte *line_ptr, *extended_end;
10158 gdb_byte *line_end;
10159 unsigned int bytes_read, extended_len;
10160 unsigned char op_code, extended_op, adj_opcode;
10161 CORE_ADDR baseaddr;
10162 struct objfile *objfile = cu->objfile;
10163 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10164 const int decode_for_pst_p = (pst != NULL);
10165 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10166
10167 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10168
10169 line_ptr = lh->statement_program_start;
10170 line_end = lh->statement_program_end;
10171
10172 /* Read the statement sequences until there's nothing left. */
10173 while (line_ptr < line_end)
10174 {
10175 /* state machine registers */
10176 CORE_ADDR address = 0;
10177 unsigned int file = 1;
10178 unsigned int line = 1;
10179 unsigned int column = 0;
10180 int is_stmt = lh->default_is_stmt;
10181 int basic_block = 0;
10182 int end_sequence = 0;
10183 CORE_ADDR addr;
10184 unsigned char op_index = 0;
10185
10186 if (!decode_for_pst_p && lh->num_file_names >= file)
10187 {
10188 /* Start a subfile for the current file of the state machine. */
10189 /* lh->include_dirs and lh->file_names are 0-based, but the
10190 directory and file name numbers in the statement program
10191 are 1-based. */
10192 struct file_entry *fe = &lh->file_names[file - 1];
10193 char *dir = NULL;
10194
10195 if (fe->dir_index)
10196 dir = lh->include_dirs[fe->dir_index - 1];
10197
10198 dwarf2_start_subfile (fe->name, dir, comp_dir);
10199 }
10200
10201 /* Decode the table. */
10202 while (!end_sequence)
10203 {
10204 op_code = read_1_byte (abfd, line_ptr);
10205 line_ptr += 1;
10206 if (line_ptr > line_end)
10207 {
10208 dwarf2_debug_line_missing_end_sequence_complaint ();
10209 break;
10210 }
10211
10212 if (op_code >= lh->opcode_base)
10213 {
10214 /* Special operand. */
10215 adj_opcode = op_code - lh->opcode_base;
10216 address += (((op_index + (adj_opcode / lh->line_range))
10217 / lh->maximum_ops_per_instruction)
10218 * lh->minimum_instruction_length);
10219 op_index = ((op_index + (adj_opcode / lh->line_range))
10220 % lh->maximum_ops_per_instruction);
10221 line += lh->line_base + (adj_opcode % lh->line_range);
10222 if (lh->num_file_names < file || file == 0)
10223 dwarf2_debug_line_missing_file_complaint ();
10224 /* For now we ignore lines not starting on an
10225 instruction boundary. */
10226 else if (op_index == 0)
10227 {
10228 lh->file_names[file - 1].included_p = 1;
10229 if (!decode_for_pst_p && is_stmt)
10230 {
10231 if (last_subfile != current_subfile)
10232 {
10233 addr = gdbarch_addr_bits_remove (gdbarch, address);
10234 if (last_subfile)
10235 record_line (last_subfile, 0, addr);
10236 last_subfile = current_subfile;
10237 }
10238 /* Append row to matrix using current values. */
10239 addr = check_cu_functions (address, cu);
10240 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10241 record_line (current_subfile, line, addr);
10242 }
10243 }
10244 basic_block = 0;
10245 }
10246 else switch (op_code)
10247 {
10248 case DW_LNS_extended_op:
10249 extended_len = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10250 line_ptr += bytes_read;
10251 extended_end = line_ptr + extended_len;
10252 extended_op = read_1_byte (abfd, line_ptr);
10253 line_ptr += 1;
10254 switch (extended_op)
10255 {
10256 case DW_LNE_end_sequence:
10257 end_sequence = 1;
10258 break;
10259 case DW_LNE_set_address:
10260 address = read_address (abfd, line_ptr, cu, &bytes_read);
10261 op_index = 0;
10262 line_ptr += bytes_read;
10263 address += baseaddr;
10264 break;
10265 case DW_LNE_define_file:
10266 {
10267 char *cur_file;
10268 unsigned int dir_index, mod_time, length;
10269
10270 cur_file = read_direct_string (abfd, line_ptr, &bytes_read);
10271 line_ptr += bytes_read;
10272 dir_index =
10273 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10274 line_ptr += bytes_read;
10275 mod_time =
10276 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10277 line_ptr += bytes_read;
10278 length =
10279 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10280 line_ptr += bytes_read;
10281 add_file_name (lh, cur_file, dir_index, mod_time, length);
10282 }
10283 break;
10284 case DW_LNE_set_discriminator:
10285 /* The discriminator is not interesting to the debugger;
10286 just ignore it. */
10287 line_ptr = extended_end;
10288 break;
10289 default:
10290 complaint (&symfile_complaints,
10291 _("mangled .debug_line section"));
10292 return;
10293 }
10294 /* Make sure that we parsed the extended op correctly. If e.g.
10295 we expected a different address size than the producer used,
10296 we may have read the wrong number of bytes. */
10297 if (line_ptr != extended_end)
10298 {
10299 complaint (&symfile_complaints,
10300 _("mangled .debug_line section"));
10301 return;
10302 }
10303 break;
10304 case DW_LNS_copy:
10305 if (lh->num_file_names < file || file == 0)
10306 dwarf2_debug_line_missing_file_complaint ();
10307 else
10308 {
10309 lh->file_names[file - 1].included_p = 1;
10310 if (!decode_for_pst_p && is_stmt)
10311 {
10312 if (last_subfile != current_subfile)
10313 {
10314 addr = gdbarch_addr_bits_remove (gdbarch, address);
10315 if (last_subfile)
10316 record_line (last_subfile, 0, addr);
10317 last_subfile = current_subfile;
10318 }
10319 addr = check_cu_functions (address, cu);
10320 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10321 record_line (current_subfile, line, addr);
10322 }
10323 }
10324 basic_block = 0;
10325 break;
10326 case DW_LNS_advance_pc:
10327 {
10328 CORE_ADDR adjust
10329 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10330
10331 address += (((op_index + adjust)
10332 / lh->maximum_ops_per_instruction)
10333 * lh->minimum_instruction_length);
10334 op_index = ((op_index + adjust)
10335 % lh->maximum_ops_per_instruction);
10336 line_ptr += bytes_read;
10337 }
10338 break;
10339 case DW_LNS_advance_line:
10340 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10341 line_ptr += bytes_read;
10342 break;
10343 case DW_LNS_set_file:
10344 {
10345 /* The arrays lh->include_dirs and lh->file_names are
10346 0-based, but the directory and file name numbers in
10347 the statement program are 1-based. */
10348 struct file_entry *fe;
10349 char *dir = NULL;
10350
10351 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10352 line_ptr += bytes_read;
10353 if (lh->num_file_names < file || file == 0)
10354 dwarf2_debug_line_missing_file_complaint ();
10355 else
10356 {
10357 fe = &lh->file_names[file - 1];
10358 if (fe->dir_index)
10359 dir = lh->include_dirs[fe->dir_index - 1];
10360 if (!decode_for_pst_p)
10361 {
10362 last_subfile = current_subfile;
10363 dwarf2_start_subfile (fe->name, dir, comp_dir);
10364 }
10365 }
10366 }
10367 break;
10368 case DW_LNS_set_column:
10369 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10370 line_ptr += bytes_read;
10371 break;
10372 case DW_LNS_negate_stmt:
10373 is_stmt = (!is_stmt);
10374 break;
10375 case DW_LNS_set_basic_block:
10376 basic_block = 1;
10377 break;
10378 /* Add to the address register of the state machine the
10379 address increment value corresponding to special opcode
10380 255. I.e., this value is scaled by the minimum
10381 instruction length since special opcode 255 would have
10382 scaled the the increment. */
10383 case DW_LNS_const_add_pc:
10384 {
10385 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10386
10387 address += (((op_index + adjust)
10388 / lh->maximum_ops_per_instruction)
10389 * lh->minimum_instruction_length);
10390 op_index = ((op_index + adjust)
10391 % lh->maximum_ops_per_instruction);
10392 }
10393 break;
10394 case DW_LNS_fixed_advance_pc:
10395 address += read_2_bytes (abfd, line_ptr);
10396 op_index = 0;
10397 line_ptr += 2;
10398 break;
10399 default:
10400 {
10401 /* Unknown standard opcode, ignore it. */
10402 int i;
10403
10404 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10405 {
10406 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10407 line_ptr += bytes_read;
10408 }
10409 }
10410 }
10411 }
10412 if (lh->num_file_names < file || file == 0)
10413 dwarf2_debug_line_missing_file_complaint ();
10414 else
10415 {
10416 lh->file_names[file - 1].included_p = 1;
10417 if (!decode_for_pst_p)
10418 {
10419 addr = gdbarch_addr_bits_remove (gdbarch, address);
10420 record_line (current_subfile, 0, addr);
10421 }
10422 }
10423 }
10424
10425 if (decode_for_pst_p)
10426 {
10427 int file_index;
10428
10429 /* Now that we're done scanning the Line Header Program, we can
10430 create the psymtab of each included file. */
10431 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10432 if (lh->file_names[file_index].included_p == 1)
10433 {
10434 char *include_name =
10435 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10436 if (include_name != NULL)
10437 dwarf2_create_include_psymtab (include_name, pst, objfile);
10438 }
10439 }
10440 else
10441 {
10442 /* Make sure a symtab is created for every file, even files
10443 which contain only variables (i.e. no code with associated
10444 line numbers). */
10445
10446 int i;
10447 struct file_entry *fe;
10448
10449 for (i = 0; i < lh->num_file_names; i++)
10450 {
10451 char *dir = NULL;
10452
10453 fe = &lh->file_names[i];
10454 if (fe->dir_index)
10455 dir = lh->include_dirs[fe->dir_index - 1];
10456 dwarf2_start_subfile (fe->name, dir, comp_dir);
10457
10458 /* Skip the main file; we don't need it, and it must be
10459 allocated last, so that it will show up before the
10460 non-primary symtabs in the objfile's symtab list. */
10461 if (current_subfile == first_subfile)
10462 continue;
10463
10464 if (current_subfile->symtab == NULL)
10465 current_subfile->symtab = allocate_symtab (current_subfile->name,
10466 cu->objfile);
10467 fe->symtab = current_subfile->symtab;
10468 }
10469 }
10470 }
10471
10472 /* Start a subfile for DWARF. FILENAME is the name of the file and
10473 DIRNAME the name of the source directory which contains FILENAME
10474 or NULL if not known. COMP_DIR is the compilation directory for the
10475 linetable's compilation unit or NULL if not known.
10476 This routine tries to keep line numbers from identical absolute and
10477 relative file names in a common subfile.
10478
10479 Using the `list' example from the GDB testsuite, which resides in
10480 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
10481 of /srcdir/list0.c yields the following debugging information for list0.c:
10482
10483 DW_AT_name: /srcdir/list0.c
10484 DW_AT_comp_dir: /compdir
10485 files.files[0].name: list0.h
10486 files.files[0].dir: /srcdir
10487 files.files[1].name: list0.c
10488 files.files[1].dir: /srcdir
10489
10490 The line number information for list0.c has to end up in a single
10491 subfile, so that `break /srcdir/list0.c:1' works as expected.
10492 start_subfile will ensure that this happens provided that we pass the
10493 concatenation of files.files[1].dir and files.files[1].name as the
10494 subfile's name. */
10495
10496 static void
10497 dwarf2_start_subfile (char *filename, const char *dirname, const char *comp_dir)
10498 {
10499 char *fullname;
10500
10501 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
10502 `start_symtab' will always pass the contents of DW_AT_comp_dir as
10503 second argument to start_subfile. To be consistent, we do the
10504 same here. In order not to lose the line information directory,
10505 we concatenate it to the filename when it makes sense.
10506 Note that the Dwarf3 standard says (speaking of filenames in line
10507 information): ``The directory index is ignored for file names
10508 that represent full path names''. Thus ignoring dirname in the
10509 `else' branch below isn't an issue. */
10510
10511 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
10512 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
10513 else
10514 fullname = filename;
10515
10516 start_subfile (fullname, comp_dir);
10517
10518 if (fullname != filename)
10519 xfree (fullname);
10520 }
10521
10522 static void
10523 var_decode_location (struct attribute *attr, struct symbol *sym,
10524 struct dwarf2_cu *cu)
10525 {
10526 struct objfile *objfile = cu->objfile;
10527 struct comp_unit_head *cu_header = &cu->header;
10528
10529 /* NOTE drow/2003-01-30: There used to be a comment and some special
10530 code here to turn a symbol with DW_AT_external and a
10531 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
10532 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
10533 with some versions of binutils) where shared libraries could have
10534 relocations against symbols in their debug information - the
10535 minimal symbol would have the right address, but the debug info
10536 would not. It's no longer necessary, because we will explicitly
10537 apply relocations when we read in the debug information now. */
10538
10539 /* A DW_AT_location attribute with no contents indicates that a
10540 variable has been optimized away. */
10541 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
10542 {
10543 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10544 return;
10545 }
10546
10547 /* Handle one degenerate form of location expression specially, to
10548 preserve GDB's previous behavior when section offsets are
10549 specified. If this is just a DW_OP_addr then mark this symbol
10550 as LOC_STATIC. */
10551
10552 if (attr_form_is_block (attr)
10553 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
10554 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
10555 {
10556 unsigned int dummy;
10557
10558 SYMBOL_VALUE_ADDRESS (sym) =
10559 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
10560 SYMBOL_CLASS (sym) = LOC_STATIC;
10561 fixup_symbol_section (sym, objfile);
10562 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
10563 SYMBOL_SECTION (sym));
10564 return;
10565 }
10566
10567 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
10568 expression evaluator, and use LOC_COMPUTED only when necessary
10569 (i.e. when the value of a register or memory location is
10570 referenced, or a thread-local block, etc.). Then again, it might
10571 not be worthwhile. I'm assuming that it isn't unless performance
10572 or memory numbers show me otherwise. */
10573
10574 dwarf2_symbol_mark_computed (attr, sym, cu);
10575 SYMBOL_CLASS (sym) = LOC_COMPUTED;
10576 }
10577
10578 /* Given a pointer to a DWARF information entry, figure out if we need
10579 to make a symbol table entry for it, and if so, create a new entry
10580 and return a pointer to it.
10581 If TYPE is NULL, determine symbol type from the die, otherwise
10582 used the passed type.
10583 If SPACE is not NULL, use it to hold the new symbol. If it is
10584 NULL, allocate a new symbol on the objfile's obstack. */
10585
10586 static struct symbol *
10587 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
10588 struct symbol *space)
10589 {
10590 struct objfile *objfile = cu->objfile;
10591 struct symbol *sym = NULL;
10592 char *name;
10593 struct attribute *attr = NULL;
10594 struct attribute *attr2 = NULL;
10595 CORE_ADDR baseaddr;
10596 struct pending **list_to_add = NULL;
10597
10598 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
10599
10600 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10601
10602 name = dwarf2_name (die, cu);
10603 if (name)
10604 {
10605 const char *linkagename;
10606 int suppress_add = 0;
10607
10608 if (space)
10609 sym = space;
10610 else
10611 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
10612 OBJSTAT (objfile, n_syms++);
10613
10614 /* Cache this symbol's name and the name's demangled form (if any). */
10615 SYMBOL_SET_LANGUAGE (sym, cu->language);
10616 linkagename = dwarf2_physname (name, die, cu);
10617 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
10618
10619 /* Fortran does not have mangling standard and the mangling does differ
10620 between gfortran, iFort etc. */
10621 if (cu->language == language_fortran
10622 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
10623 symbol_set_demangled_name (&(sym->ginfo),
10624 (char *) dwarf2_full_name (name, die, cu),
10625 NULL);
10626
10627 /* Default assumptions.
10628 Use the passed type or decode it from the die. */
10629 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10630 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
10631 if (type != NULL)
10632 SYMBOL_TYPE (sym) = type;
10633 else
10634 SYMBOL_TYPE (sym) = die_type (die, cu);
10635 attr = dwarf2_attr (die,
10636 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
10637 cu);
10638 if (attr)
10639 {
10640 SYMBOL_LINE (sym) = DW_UNSND (attr);
10641 }
10642
10643 attr = dwarf2_attr (die,
10644 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
10645 cu);
10646 if (attr)
10647 {
10648 int file_index = DW_UNSND (attr);
10649
10650 if (cu->line_header == NULL
10651 || file_index > cu->line_header->num_file_names)
10652 complaint (&symfile_complaints,
10653 _("file index out of range"));
10654 else if (file_index > 0)
10655 {
10656 struct file_entry *fe;
10657
10658 fe = &cu->line_header->file_names[file_index - 1];
10659 SYMBOL_SYMTAB (sym) = fe->symtab;
10660 }
10661 }
10662
10663 switch (die->tag)
10664 {
10665 case DW_TAG_label:
10666 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
10667 if (attr)
10668 {
10669 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
10670 }
10671 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
10672 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
10673 SYMBOL_CLASS (sym) = LOC_LABEL;
10674 add_symbol_to_list (sym, cu->list_in_scope);
10675 break;
10676 case DW_TAG_subprogram:
10677 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10678 finish_block. */
10679 SYMBOL_CLASS (sym) = LOC_BLOCK;
10680 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10681 if ((attr2 && (DW_UNSND (attr2) != 0))
10682 || cu->language == language_ada)
10683 {
10684 /* Subprograms marked external are stored as a global symbol.
10685 Ada subprograms, whether marked external or not, are always
10686 stored as a global symbol, because we want to be able to
10687 access them globally. For instance, we want to be able
10688 to break on a nested subprogram without having to
10689 specify the context. */
10690 list_to_add = &global_symbols;
10691 }
10692 else
10693 {
10694 list_to_add = cu->list_in_scope;
10695 }
10696 break;
10697 case DW_TAG_inlined_subroutine:
10698 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
10699 finish_block. */
10700 SYMBOL_CLASS (sym) = LOC_BLOCK;
10701 SYMBOL_INLINED (sym) = 1;
10702 /* Do not add the symbol to any lists. It will be found via
10703 BLOCK_FUNCTION from the blockvector. */
10704 break;
10705 case DW_TAG_template_value_param:
10706 suppress_add = 1;
10707 /* Fall through. */
10708 case DW_TAG_constant:
10709 case DW_TAG_variable:
10710 case DW_TAG_member:
10711 /* Compilation with minimal debug info may result in variables
10712 with missing type entries. Change the misleading `void' type
10713 to something sensible. */
10714 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
10715 SYMBOL_TYPE (sym)
10716 = objfile_type (objfile)->nodebug_data_symbol;
10717
10718 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10719 /* In the case of DW_TAG_member, we should only be called for
10720 static const members. */
10721 if (die->tag == DW_TAG_member)
10722 {
10723 /* dwarf2_add_field uses die_is_declaration,
10724 so we do the same. */
10725 gdb_assert (die_is_declaration (die, cu));
10726 gdb_assert (attr);
10727 }
10728 if (attr)
10729 {
10730 dwarf2_const_value (attr, sym, cu);
10731 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10732 if (!suppress_add)
10733 {
10734 if (attr2 && (DW_UNSND (attr2) != 0))
10735 list_to_add = &global_symbols;
10736 else
10737 list_to_add = cu->list_in_scope;
10738 }
10739 break;
10740 }
10741 attr = dwarf2_attr (die, DW_AT_location, cu);
10742 if (attr)
10743 {
10744 var_decode_location (attr, sym, cu);
10745 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10746 if (SYMBOL_CLASS (sym) == LOC_STATIC
10747 && SYMBOL_VALUE_ADDRESS (sym) == 0
10748 && !dwarf2_per_objfile->has_section_at_zero)
10749 {
10750 /* When a static variable is eliminated by the linker,
10751 the corresponding debug information is not stripped
10752 out, but the variable address is set to null;
10753 do not add such variables into symbol table. */
10754 }
10755 else if (attr2 && (DW_UNSND (attr2) != 0))
10756 {
10757 /* Workaround gfortran PR debug/40040 - it uses
10758 DW_AT_location for variables in -fPIC libraries which may
10759 get overriden by other libraries/executable and get
10760 a different address. Resolve it by the minimal symbol
10761 which may come from inferior's executable using copy
10762 relocation. Make this workaround only for gfortran as for
10763 other compilers GDB cannot guess the minimal symbol
10764 Fortran mangling kind. */
10765 if (cu->language == language_fortran && die->parent
10766 && die->parent->tag == DW_TAG_module
10767 && cu->producer
10768 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
10769 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10770
10771 /* A variable with DW_AT_external is never static,
10772 but it may be block-scoped. */
10773 list_to_add = (cu->list_in_scope == &file_symbols
10774 ? &global_symbols : cu->list_in_scope);
10775 }
10776 else
10777 list_to_add = cu->list_in_scope;
10778 }
10779 else
10780 {
10781 /* We do not know the address of this symbol.
10782 If it is an external symbol and we have type information
10783 for it, enter the symbol as a LOC_UNRESOLVED symbol.
10784 The address of the variable will then be determined from
10785 the minimal symbol table whenever the variable is
10786 referenced. */
10787 attr2 = dwarf2_attr (die, DW_AT_external, cu);
10788 if (attr2 && (DW_UNSND (attr2) != 0)
10789 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
10790 {
10791 /* A variable with DW_AT_external is never static, but it
10792 may be block-scoped. */
10793 list_to_add = (cu->list_in_scope == &file_symbols
10794 ? &global_symbols : cu->list_in_scope);
10795
10796 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
10797 }
10798 else if (!die_is_declaration (die, cu))
10799 {
10800 /* Use the default LOC_OPTIMIZED_OUT class. */
10801 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
10802 if (!suppress_add)
10803 list_to_add = cu->list_in_scope;
10804 }
10805 }
10806 break;
10807 case DW_TAG_formal_parameter:
10808 /* If we are inside a function, mark this as an argument. If
10809 not, we might be looking at an argument to an inlined function
10810 when we do not have enough information to show inlined frames;
10811 pretend it's a local variable in that case so that the user can
10812 still see it. */
10813 if (context_stack_depth > 0
10814 && context_stack[context_stack_depth - 1].name != NULL)
10815 SYMBOL_IS_ARGUMENT (sym) = 1;
10816 attr = dwarf2_attr (die, DW_AT_location, cu);
10817 if (attr)
10818 {
10819 var_decode_location (attr, sym, cu);
10820 }
10821 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10822 if (attr)
10823 {
10824 dwarf2_const_value (attr, sym, cu);
10825 }
10826 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
10827 if (attr && DW_UNSND (attr))
10828 {
10829 struct type *ref_type;
10830
10831 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
10832 SYMBOL_TYPE (sym) = ref_type;
10833 }
10834
10835 list_to_add = cu->list_in_scope;
10836 break;
10837 case DW_TAG_unspecified_parameters:
10838 /* From varargs functions; gdb doesn't seem to have any
10839 interest in this information, so just ignore it for now.
10840 (FIXME?) */
10841 break;
10842 case DW_TAG_template_type_param:
10843 suppress_add = 1;
10844 /* Fall through. */
10845 case DW_TAG_class_type:
10846 case DW_TAG_interface_type:
10847 case DW_TAG_structure_type:
10848 case DW_TAG_union_type:
10849 case DW_TAG_set_type:
10850 case DW_TAG_enumeration_type:
10851 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10852 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
10853
10854 {
10855 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
10856 really ever be static objects: otherwise, if you try
10857 to, say, break of a class's method and you're in a file
10858 which doesn't mention that class, it won't work unless
10859 the check for all static symbols in lookup_symbol_aux
10860 saves you. See the OtherFileClass tests in
10861 gdb.c++/namespace.exp. */
10862
10863 if (!suppress_add)
10864 {
10865 list_to_add = (cu->list_in_scope == &file_symbols
10866 && (cu->language == language_cplus
10867 || cu->language == language_java)
10868 ? &global_symbols : cu->list_in_scope);
10869
10870 /* The semantics of C++ state that "struct foo {
10871 ... }" also defines a typedef for "foo". A Java
10872 class declaration also defines a typedef for the
10873 class. */
10874 if (cu->language == language_cplus
10875 || cu->language == language_java
10876 || cu->language == language_ada)
10877 {
10878 /* The symbol's name is already allocated along
10879 with this objfile, so we don't need to
10880 duplicate it for the type. */
10881 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
10882 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
10883 }
10884 }
10885 }
10886 break;
10887 case DW_TAG_typedef:
10888 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10889 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10890 list_to_add = cu->list_in_scope;
10891 break;
10892 case DW_TAG_base_type:
10893 case DW_TAG_subrange_type:
10894 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10895 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
10896 list_to_add = cu->list_in_scope;
10897 break;
10898 case DW_TAG_enumerator:
10899 attr = dwarf2_attr (die, DW_AT_const_value, cu);
10900 if (attr)
10901 {
10902 dwarf2_const_value (attr, sym, cu);
10903 }
10904 {
10905 /* NOTE: carlton/2003-11-10: See comment above in the
10906 DW_TAG_class_type, etc. block. */
10907
10908 list_to_add = (cu->list_in_scope == &file_symbols
10909 && (cu->language == language_cplus
10910 || cu->language == language_java)
10911 ? &global_symbols : cu->list_in_scope);
10912 }
10913 break;
10914 case DW_TAG_namespace:
10915 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
10916 list_to_add = &global_symbols;
10917 break;
10918 default:
10919 /* Not a tag we recognize. Hopefully we aren't processing
10920 trash data, but since we must specifically ignore things
10921 we don't recognize, there is nothing else we should do at
10922 this point. */
10923 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
10924 dwarf_tag_name (die->tag));
10925 break;
10926 }
10927
10928 if (suppress_add)
10929 {
10930 sym->hash_next = objfile->template_symbols;
10931 objfile->template_symbols = sym;
10932 list_to_add = NULL;
10933 }
10934
10935 if (list_to_add != NULL)
10936 add_symbol_to_list (sym, list_to_add);
10937
10938 /* For the benefit of old versions of GCC, check for anonymous
10939 namespaces based on the demangled name. */
10940 if (!processing_has_namespace_info
10941 && cu->language == language_cplus)
10942 cp_scan_for_anonymous_namespaces (sym);
10943 }
10944 return (sym);
10945 }
10946
10947 /* A wrapper for new_symbol_full that always allocates a new symbol. */
10948
10949 static struct symbol *
10950 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
10951 {
10952 return new_symbol_full (die, type, cu, NULL);
10953 }
10954
10955 /* Given an attr with a DW_FORM_dataN value in host byte order,
10956 zero-extend it as appropriate for the symbol's type. The DWARF
10957 standard (v4) is not entirely clear about the meaning of using
10958 DW_FORM_dataN for a constant with a signed type, where the type is
10959 wider than the data. The conclusion of a discussion on the DWARF
10960 list was that this is unspecified. We choose to always zero-extend
10961 because that is the interpretation long in use by GCC. */
10962
10963 static gdb_byte *
10964 dwarf2_const_value_data (struct attribute *attr, struct type *type,
10965 const char *name, struct obstack *obstack,
10966 struct dwarf2_cu *cu, long *value, int bits)
10967 {
10968 struct objfile *objfile = cu->objfile;
10969 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
10970 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
10971 LONGEST l = DW_UNSND (attr);
10972
10973 if (bits < sizeof (*value) * 8)
10974 {
10975 l &= ((LONGEST) 1 << bits) - 1;
10976 *value = l;
10977 }
10978 else if (bits == sizeof (*value) * 8)
10979 *value = l;
10980 else
10981 {
10982 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
10983 store_unsigned_integer (bytes, bits / 8, byte_order, l);
10984 return bytes;
10985 }
10986
10987 return NULL;
10988 }
10989
10990 /* Read a constant value from an attribute. Either set *VALUE, or if
10991 the value does not fit in *VALUE, set *BYTES - either already
10992 allocated on the objfile obstack, or newly allocated on OBSTACK,
10993 or, set *BATON, if we translated the constant to a location
10994 expression. */
10995
10996 static void
10997 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
10998 const char *name, struct obstack *obstack,
10999 struct dwarf2_cu *cu,
11000 long *value, gdb_byte **bytes,
11001 struct dwarf2_locexpr_baton **baton)
11002 {
11003 struct objfile *objfile = cu->objfile;
11004 struct comp_unit_head *cu_header = &cu->header;
11005 struct dwarf_block *blk;
11006 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11007 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11008
11009 *value = 0;
11010 *bytes = NULL;
11011 *baton = NULL;
11012
11013 switch (attr->form)
11014 {
11015 case DW_FORM_addr:
11016 {
11017 gdb_byte *data;
11018
11019 if (TYPE_LENGTH (type) != cu_header->addr_size)
11020 dwarf2_const_value_length_mismatch_complaint (name,
11021 cu_header->addr_size,
11022 TYPE_LENGTH (type));
11023 /* Symbols of this form are reasonably rare, so we just
11024 piggyback on the existing location code rather than writing
11025 a new implementation of symbol_computed_ops. */
11026 *baton = obstack_alloc (&objfile->objfile_obstack,
11027 sizeof (struct dwarf2_locexpr_baton));
11028 (*baton)->per_cu = cu->per_cu;
11029 gdb_assert ((*baton)->per_cu);
11030
11031 (*baton)->size = 2 + cu_header->addr_size;
11032 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11033 (*baton)->data = data;
11034
11035 data[0] = DW_OP_addr;
11036 store_unsigned_integer (&data[1], cu_header->addr_size,
11037 byte_order, DW_ADDR (attr));
11038 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11039 }
11040 break;
11041 case DW_FORM_string:
11042 case DW_FORM_strp:
11043 /* DW_STRING is already allocated on the objfile obstack, point
11044 directly to it. */
11045 *bytes = (gdb_byte *) DW_STRING (attr);
11046 break;
11047 case DW_FORM_block1:
11048 case DW_FORM_block2:
11049 case DW_FORM_block4:
11050 case DW_FORM_block:
11051 case DW_FORM_exprloc:
11052 blk = DW_BLOCK (attr);
11053 if (TYPE_LENGTH (type) != blk->size)
11054 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11055 TYPE_LENGTH (type));
11056 *bytes = blk->data;
11057 break;
11058
11059 /* The DW_AT_const_value attributes are supposed to carry the
11060 symbol's value "represented as it would be on the target
11061 architecture." By the time we get here, it's already been
11062 converted to host endianness, so we just need to sign- or
11063 zero-extend it as appropriate. */
11064 case DW_FORM_data1:
11065 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 8);
11066 break;
11067 case DW_FORM_data2:
11068 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 16);
11069 break;
11070 case DW_FORM_data4:
11071 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 32);
11072 break;
11073 case DW_FORM_data8:
11074 *bytes = dwarf2_const_value_data (attr, type, name, obstack, cu, value, 64);
11075 break;
11076
11077 case DW_FORM_sdata:
11078 *value = DW_SND (attr);
11079 break;
11080
11081 case DW_FORM_udata:
11082 *value = DW_UNSND (attr);
11083 break;
11084
11085 default:
11086 complaint (&symfile_complaints,
11087 _("unsupported const value attribute form: '%s'"),
11088 dwarf_form_name (attr->form));
11089 *value = 0;
11090 break;
11091 }
11092 }
11093
11094
11095 /* Copy constant value from an attribute to a symbol. */
11096
11097 static void
11098 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11099 struct dwarf2_cu *cu)
11100 {
11101 struct objfile *objfile = cu->objfile;
11102 struct comp_unit_head *cu_header = &cu->header;
11103 long value;
11104 gdb_byte *bytes;
11105 struct dwarf2_locexpr_baton *baton;
11106
11107 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11108 SYMBOL_PRINT_NAME (sym),
11109 &objfile->objfile_obstack, cu,
11110 &value, &bytes, &baton);
11111
11112 if (baton != NULL)
11113 {
11114 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11115 SYMBOL_LOCATION_BATON (sym) = baton;
11116 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11117 }
11118 else if (bytes != NULL)
11119 {
11120 SYMBOL_VALUE_BYTES (sym) = bytes;
11121 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11122 }
11123 else
11124 {
11125 SYMBOL_VALUE (sym) = value;
11126 SYMBOL_CLASS (sym) = LOC_CONST;
11127 }
11128 }
11129
11130 /* Return the type of the die in question using its DW_AT_type attribute. */
11131
11132 static struct type *
11133 die_type (struct die_info *die, struct dwarf2_cu *cu)
11134 {
11135 struct attribute *type_attr;
11136
11137 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11138 if (!type_attr)
11139 {
11140 /* A missing DW_AT_type represents a void type. */
11141 return objfile_type (cu->objfile)->builtin_void;
11142 }
11143
11144 return lookup_die_type (die, type_attr, cu);
11145 }
11146
11147 /* True iff CU's producer generates GNAT Ada auxiliary information
11148 that allows to find parallel types through that information instead
11149 of having to do expensive parallel lookups by type name. */
11150
11151 static int
11152 need_gnat_info (struct dwarf2_cu *cu)
11153 {
11154 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11155 of GNAT produces this auxiliary information, without any indication
11156 that it is produced. Part of enhancing the FSF version of GNAT
11157 to produce that information will be to put in place an indicator
11158 that we can use in order to determine whether the descriptive type
11159 info is available or not. One suggestion that has been made is
11160 to use a new attribute, attached to the CU die. For now, assume
11161 that the descriptive type info is not available. */
11162 return 0;
11163 }
11164
11165 /* Return the auxiliary type of the die in question using its
11166 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11167 attribute is not present. */
11168
11169 static struct type *
11170 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11171 {
11172 struct attribute *type_attr;
11173
11174 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11175 if (!type_attr)
11176 return NULL;
11177
11178 return lookup_die_type (die, type_attr, cu);
11179 }
11180
11181 /* If DIE has a descriptive_type attribute, then set the TYPE's
11182 descriptive type accordingly. */
11183
11184 static void
11185 set_descriptive_type (struct type *type, struct die_info *die,
11186 struct dwarf2_cu *cu)
11187 {
11188 struct type *descriptive_type = die_descriptive_type (die, cu);
11189
11190 if (descriptive_type)
11191 {
11192 ALLOCATE_GNAT_AUX_TYPE (type);
11193 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11194 }
11195 }
11196
11197 /* Return the containing type of the die in question using its
11198 DW_AT_containing_type attribute. */
11199
11200 static struct type *
11201 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11202 {
11203 struct attribute *type_attr;
11204
11205 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11206 if (!type_attr)
11207 error (_("Dwarf Error: Problem turning containing type into gdb type "
11208 "[in module %s]"), cu->objfile->name);
11209
11210 return lookup_die_type (die, type_attr, cu);
11211 }
11212
11213 /* Look up the type of DIE in CU using its type attribute ATTR.
11214 If there is no type substitute an error marker. */
11215
11216 static struct type *
11217 lookup_die_type (struct die_info *die, struct attribute *attr,
11218 struct dwarf2_cu *cu)
11219 {
11220 struct type *this_type;
11221
11222 /* First see if we have it cached. */
11223
11224 if (is_ref_attr (attr))
11225 {
11226 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11227
11228 this_type = get_die_type_at_offset (offset, cu->per_cu);
11229 }
11230 else if (attr->form == DW_FORM_sig8)
11231 {
11232 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11233 struct dwarf2_cu *sig_cu;
11234 unsigned int offset;
11235
11236 /* sig_type will be NULL if the signatured type is missing from
11237 the debug info. */
11238 if (sig_type == NULL)
11239 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11240 "at 0x%x [in module %s]"),
11241 die->offset, cu->objfile->name);
11242
11243 gdb_assert (sig_type->per_cu.from_debug_types);
11244 offset = sig_type->offset + sig_type->type_offset;
11245 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11246 }
11247 else
11248 {
11249 dump_die_for_error (die);
11250 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11251 dwarf_attr_name (attr->name), cu->objfile->name);
11252 }
11253
11254 /* If not cached we need to read it in. */
11255
11256 if (this_type == NULL)
11257 {
11258 struct die_info *type_die;
11259 struct dwarf2_cu *type_cu = cu;
11260
11261 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11262 /* If the type is cached, we should have found it above. */
11263 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11264 this_type = read_type_die_1 (type_die, type_cu);
11265 }
11266
11267 /* If we still don't have a type use an error marker. */
11268
11269 if (this_type == NULL)
11270 {
11271 char *message, *saved;
11272
11273 /* read_type_die already issued a complaint. */
11274 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11275 cu->objfile->name,
11276 cu->header.offset,
11277 die->offset);
11278 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11279 message, strlen (message));
11280 xfree (message);
11281
11282 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11283 }
11284
11285 return this_type;
11286 }
11287
11288 /* Return the type in DIE, CU.
11289 Returns NULL for invalid types.
11290
11291 This first does a lookup in the appropriate type_hash table,
11292 and only reads the die in if necessary.
11293
11294 NOTE: This can be called when reading in partial or full symbols. */
11295
11296 static struct type *
11297 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11298 {
11299 struct type *this_type;
11300
11301 this_type = get_die_type (die, cu);
11302 if (this_type)
11303 return this_type;
11304
11305 return read_type_die_1 (die, cu);
11306 }
11307
11308 /* Read the type in DIE, CU.
11309 Returns NULL for invalid types. */
11310
11311 static struct type *
11312 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11313 {
11314 struct type *this_type = NULL;
11315
11316 switch (die->tag)
11317 {
11318 case DW_TAG_class_type:
11319 case DW_TAG_interface_type:
11320 case DW_TAG_structure_type:
11321 case DW_TAG_union_type:
11322 this_type = read_structure_type (die, cu);
11323 break;
11324 case DW_TAG_enumeration_type:
11325 this_type = read_enumeration_type (die, cu);
11326 break;
11327 case DW_TAG_subprogram:
11328 case DW_TAG_subroutine_type:
11329 case DW_TAG_inlined_subroutine:
11330 this_type = read_subroutine_type (die, cu);
11331 break;
11332 case DW_TAG_array_type:
11333 this_type = read_array_type (die, cu);
11334 break;
11335 case DW_TAG_set_type:
11336 this_type = read_set_type (die, cu);
11337 break;
11338 case DW_TAG_pointer_type:
11339 this_type = read_tag_pointer_type (die, cu);
11340 break;
11341 case DW_TAG_ptr_to_member_type:
11342 this_type = read_tag_ptr_to_member_type (die, cu);
11343 break;
11344 case DW_TAG_reference_type:
11345 this_type = read_tag_reference_type (die, cu);
11346 break;
11347 case DW_TAG_const_type:
11348 this_type = read_tag_const_type (die, cu);
11349 break;
11350 case DW_TAG_volatile_type:
11351 this_type = read_tag_volatile_type (die, cu);
11352 break;
11353 case DW_TAG_string_type:
11354 this_type = read_tag_string_type (die, cu);
11355 break;
11356 case DW_TAG_typedef:
11357 this_type = read_typedef (die, cu);
11358 break;
11359 case DW_TAG_subrange_type:
11360 this_type = read_subrange_type (die, cu);
11361 break;
11362 case DW_TAG_base_type:
11363 this_type = read_base_type (die, cu);
11364 break;
11365 case DW_TAG_unspecified_type:
11366 this_type = read_unspecified_type (die, cu);
11367 break;
11368 case DW_TAG_namespace:
11369 this_type = read_namespace_type (die, cu);
11370 break;
11371 case DW_TAG_module:
11372 this_type = read_module_type (die, cu);
11373 break;
11374 default:
11375 complaint (&symfile_complaints, _("unexpected tag in read_type_die: '%s'"),
11376 dwarf_tag_name (die->tag));
11377 break;
11378 }
11379
11380 return this_type;
11381 }
11382
11383 /* See if we can figure out if the class lives in a namespace. We do
11384 this by looking for a member function; its demangled name will
11385 contain namespace info, if there is any.
11386 Return the computed name or NULL.
11387 Space for the result is allocated on the objfile's obstack.
11388 This is the full-die version of guess_partial_die_structure_name.
11389 In this case we know DIE has no useful parent. */
11390
11391 static char *
11392 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11393 {
11394 struct die_info *spec_die;
11395 struct dwarf2_cu *spec_cu;
11396 struct die_info *child;
11397
11398 spec_cu = cu;
11399 spec_die = die_specification (die, &spec_cu);
11400 if (spec_die != NULL)
11401 {
11402 die = spec_die;
11403 cu = spec_cu;
11404 }
11405
11406 for (child = die->child;
11407 child != NULL;
11408 child = child->sibling)
11409 {
11410 if (child->tag == DW_TAG_subprogram)
11411 {
11412 struct attribute *attr;
11413
11414 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11415 if (attr == NULL)
11416 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11417 if (attr != NULL)
11418 {
11419 char *actual_name
11420 = language_class_name_from_physname (cu->language_defn,
11421 DW_STRING (attr));
11422 char *name = NULL;
11423
11424 if (actual_name != NULL)
11425 {
11426 char *die_name = dwarf2_name (die, cu);
11427
11428 if (die_name != NULL
11429 && strcmp (die_name, actual_name) != 0)
11430 {
11431 /* Strip off the class name from the full name.
11432 We want the prefix. */
11433 int die_name_len = strlen (die_name);
11434 int actual_name_len = strlen (actual_name);
11435
11436 /* Test for '::' as a sanity check. */
11437 if (actual_name_len > die_name_len + 2
11438 && actual_name[actual_name_len - die_name_len - 1] == ':')
11439 name =
11440 obsavestring (actual_name,
11441 actual_name_len - die_name_len - 2,
11442 &cu->objfile->objfile_obstack);
11443 }
11444 }
11445 xfree (actual_name);
11446 return name;
11447 }
11448 }
11449 }
11450
11451 return NULL;
11452 }
11453
11454 /* Return the name of the namespace/class that DIE is defined within,
11455 or "" if we can't tell. The caller should not xfree the result.
11456
11457 For example, if we're within the method foo() in the following
11458 code:
11459
11460 namespace N {
11461 class C {
11462 void foo () {
11463 }
11464 };
11465 }
11466
11467 then determine_prefix on foo's die will return "N::C". */
11468
11469 static char *
11470 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
11471 {
11472 struct die_info *parent, *spec_die;
11473 struct dwarf2_cu *spec_cu;
11474 struct type *parent_type;
11475
11476 if (cu->language != language_cplus && cu->language != language_java
11477 && cu->language != language_fortran)
11478 return "";
11479
11480 /* We have to be careful in the presence of DW_AT_specification.
11481 For example, with GCC 3.4, given the code
11482
11483 namespace N {
11484 void foo() {
11485 // Definition of N::foo.
11486 }
11487 }
11488
11489 then we'll have a tree of DIEs like this:
11490
11491 1: DW_TAG_compile_unit
11492 2: DW_TAG_namespace // N
11493 3: DW_TAG_subprogram // declaration of N::foo
11494 4: DW_TAG_subprogram // definition of N::foo
11495 DW_AT_specification // refers to die #3
11496
11497 Thus, when processing die #4, we have to pretend that we're in
11498 the context of its DW_AT_specification, namely the contex of die
11499 #3. */
11500 spec_cu = cu;
11501 spec_die = die_specification (die, &spec_cu);
11502 if (spec_die == NULL)
11503 parent = die->parent;
11504 else
11505 {
11506 parent = spec_die->parent;
11507 cu = spec_cu;
11508 }
11509
11510 if (parent == NULL)
11511 return "";
11512 else if (parent->building_fullname)
11513 {
11514 const char *name;
11515 const char *parent_name;
11516
11517 /* It has been seen on RealView 2.2 built binaries,
11518 DW_TAG_template_type_param types actually _defined_ as
11519 children of the parent class:
11520
11521 enum E {};
11522 template class <class Enum> Class{};
11523 Class<enum E> class_e;
11524
11525 1: DW_TAG_class_type (Class)
11526 2: DW_TAG_enumeration_type (E)
11527 3: DW_TAG_enumerator (enum1:0)
11528 3: DW_TAG_enumerator (enum2:1)
11529 ...
11530 2: DW_TAG_template_type_param
11531 DW_AT_type DW_FORM_ref_udata (E)
11532
11533 Besides being broken debug info, it can put GDB into an
11534 infinite loop. Consider:
11535
11536 When we're building the full name for Class<E>, we'll start
11537 at Class, and go look over its template type parameters,
11538 finding E. We'll then try to build the full name of E, and
11539 reach here. We're now trying to build the full name of E,
11540 and look over the parent DIE for containing scope. In the
11541 broken case, if we followed the parent DIE of E, we'd again
11542 find Class, and once again go look at its template type
11543 arguments, etc., etc. Simply don't consider such parent die
11544 as source-level parent of this die (it can't be, the language
11545 doesn't allow it), and break the loop here. */
11546 name = dwarf2_name (die, cu);
11547 parent_name = dwarf2_name (parent, cu);
11548 complaint (&symfile_complaints,
11549 _("template param type '%s' defined within parent '%s'"),
11550 name ? name : "<unknown>",
11551 parent_name ? parent_name : "<unknown>");
11552 return "";
11553 }
11554 else
11555 switch (parent->tag)
11556 {
11557 case DW_TAG_namespace:
11558 parent_type = read_type_die (parent, cu);
11559 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
11560 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
11561 Work around this problem here. */
11562 if (cu->language == language_cplus
11563 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
11564 return "";
11565 /* We give a name to even anonymous namespaces. */
11566 return TYPE_TAG_NAME (parent_type);
11567 case DW_TAG_class_type:
11568 case DW_TAG_interface_type:
11569 case DW_TAG_structure_type:
11570 case DW_TAG_union_type:
11571 case DW_TAG_module:
11572 parent_type = read_type_die (parent, cu);
11573 if (TYPE_TAG_NAME (parent_type) != NULL)
11574 return TYPE_TAG_NAME (parent_type);
11575 else
11576 /* An anonymous structure is only allowed non-static data
11577 members; no typedefs, no member functions, et cetera.
11578 So it does not need a prefix. */
11579 return "";
11580 case DW_TAG_compile_unit:
11581 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
11582 if (cu->language == language_cplus
11583 && dwarf2_per_objfile->types.asection != NULL
11584 && die->child != NULL
11585 && (die->tag == DW_TAG_class_type
11586 || die->tag == DW_TAG_structure_type
11587 || die->tag == DW_TAG_union_type))
11588 {
11589 char *name = guess_full_die_structure_name (die, cu);
11590 if (name != NULL)
11591 return name;
11592 }
11593 return "";
11594 default:
11595 return determine_prefix (parent, cu);
11596 }
11597 }
11598
11599 /* Return a newly-allocated string formed by concatenating PREFIX and
11600 SUFFIX with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
11601 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null,
11602 perform an obconcat, otherwise allocate storage for the result. The CU argument
11603 is used to determine the language and hence, the appropriate separator. */
11604
11605 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
11606
11607 static char *
11608 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
11609 int physname, struct dwarf2_cu *cu)
11610 {
11611 const char *lead = "";
11612 const char *sep;
11613
11614 if (suffix == NULL || suffix[0] == '\0' || prefix == NULL || prefix[0] == '\0')
11615 sep = "";
11616 else if (cu->language == language_java)
11617 sep = ".";
11618 else if (cu->language == language_fortran && physname)
11619 {
11620 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
11621 DW_AT_MIPS_linkage_name is preferred and used instead. */
11622
11623 lead = "__";
11624 sep = "_MOD_";
11625 }
11626 else
11627 sep = "::";
11628
11629 if (prefix == NULL)
11630 prefix = "";
11631 if (suffix == NULL)
11632 suffix = "";
11633
11634 if (obs == NULL)
11635 {
11636 char *retval = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
11637
11638 strcpy (retval, lead);
11639 strcat (retval, prefix);
11640 strcat (retval, sep);
11641 strcat (retval, suffix);
11642 return retval;
11643 }
11644 else
11645 {
11646 /* We have an obstack. */
11647 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
11648 }
11649 }
11650
11651 /* Return sibling of die, NULL if no sibling. */
11652
11653 static struct die_info *
11654 sibling_die (struct die_info *die)
11655 {
11656 return die->sibling;
11657 }
11658
11659 /* Get name of a die, return NULL if not found. */
11660
11661 static char *
11662 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
11663 struct obstack *obstack)
11664 {
11665 if (name && cu->language == language_cplus)
11666 {
11667 char *canon_name = cp_canonicalize_string (name);
11668
11669 if (canon_name != NULL)
11670 {
11671 if (strcmp (canon_name, name) != 0)
11672 name = obsavestring (canon_name, strlen (canon_name),
11673 obstack);
11674 xfree (canon_name);
11675 }
11676 }
11677
11678 return name;
11679 }
11680
11681 /* Get name of a die, return NULL if not found. */
11682
11683 static char *
11684 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
11685 {
11686 struct attribute *attr;
11687
11688 attr = dwarf2_attr (die, DW_AT_name, cu);
11689 if (!attr || !DW_STRING (attr))
11690 return NULL;
11691
11692 switch (die->tag)
11693 {
11694 case DW_TAG_compile_unit:
11695 /* Compilation units have a DW_AT_name that is a filename, not
11696 a source language identifier. */
11697 case DW_TAG_enumeration_type:
11698 case DW_TAG_enumerator:
11699 /* These tags always have simple identifiers already; no need
11700 to canonicalize them. */
11701 return DW_STRING (attr);
11702
11703 case DW_TAG_subprogram:
11704 /* Java constructors will all be named "<init>", so return
11705 the class name when we see this special case. */
11706 if (cu->language == language_java
11707 && DW_STRING (attr) != NULL
11708 && strcmp (DW_STRING (attr), "<init>") == 0)
11709 {
11710 struct dwarf2_cu *spec_cu = cu;
11711 struct die_info *spec_die;
11712
11713 /* GCJ will output '<init>' for Java constructor names.
11714 For this special case, return the name of the parent class. */
11715
11716 /* GCJ may output suprogram DIEs with AT_specification set.
11717 If so, use the name of the specified DIE. */
11718 spec_die = die_specification (die, &spec_cu);
11719 if (spec_die != NULL)
11720 return dwarf2_name (spec_die, spec_cu);
11721
11722 do
11723 {
11724 die = die->parent;
11725 if (die->tag == DW_TAG_class_type)
11726 return dwarf2_name (die, cu);
11727 }
11728 while (die->tag != DW_TAG_compile_unit);
11729 }
11730 break;
11731
11732 case DW_TAG_class_type:
11733 case DW_TAG_interface_type:
11734 case DW_TAG_structure_type:
11735 case DW_TAG_union_type:
11736 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
11737 structures or unions. These were of the form "._%d" in GCC 4.1,
11738 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
11739 and GCC 4.4. We work around this problem by ignoring these. */
11740 if (strncmp (DW_STRING (attr), "._", 2) == 0
11741 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0)
11742 return NULL;
11743 break;
11744
11745 default:
11746 break;
11747 }
11748
11749 if (!DW_STRING_IS_CANONICAL (attr))
11750 {
11751 DW_STRING (attr)
11752 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
11753 &cu->objfile->objfile_obstack);
11754 DW_STRING_IS_CANONICAL (attr) = 1;
11755 }
11756 return DW_STRING (attr);
11757 }
11758
11759 /* Return the die that this die in an extension of, or NULL if there
11760 is none. *EXT_CU is the CU containing DIE on input, and the CU
11761 containing the return value on output. */
11762
11763 static struct die_info *
11764 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
11765 {
11766 struct attribute *attr;
11767
11768 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
11769 if (attr == NULL)
11770 return NULL;
11771
11772 return follow_die_ref (die, attr, ext_cu);
11773 }
11774
11775 /* Convert a DIE tag into its string name. */
11776
11777 static char *
11778 dwarf_tag_name (unsigned tag)
11779 {
11780 switch (tag)
11781 {
11782 case DW_TAG_padding:
11783 return "DW_TAG_padding";
11784 case DW_TAG_array_type:
11785 return "DW_TAG_array_type";
11786 case DW_TAG_class_type:
11787 return "DW_TAG_class_type";
11788 case DW_TAG_entry_point:
11789 return "DW_TAG_entry_point";
11790 case DW_TAG_enumeration_type:
11791 return "DW_TAG_enumeration_type";
11792 case DW_TAG_formal_parameter:
11793 return "DW_TAG_formal_parameter";
11794 case DW_TAG_imported_declaration:
11795 return "DW_TAG_imported_declaration";
11796 case DW_TAG_label:
11797 return "DW_TAG_label";
11798 case DW_TAG_lexical_block:
11799 return "DW_TAG_lexical_block";
11800 case DW_TAG_member:
11801 return "DW_TAG_member";
11802 case DW_TAG_pointer_type:
11803 return "DW_TAG_pointer_type";
11804 case DW_TAG_reference_type:
11805 return "DW_TAG_reference_type";
11806 case DW_TAG_compile_unit:
11807 return "DW_TAG_compile_unit";
11808 case DW_TAG_string_type:
11809 return "DW_TAG_string_type";
11810 case DW_TAG_structure_type:
11811 return "DW_TAG_structure_type";
11812 case DW_TAG_subroutine_type:
11813 return "DW_TAG_subroutine_type";
11814 case DW_TAG_typedef:
11815 return "DW_TAG_typedef";
11816 case DW_TAG_union_type:
11817 return "DW_TAG_union_type";
11818 case DW_TAG_unspecified_parameters:
11819 return "DW_TAG_unspecified_parameters";
11820 case DW_TAG_variant:
11821 return "DW_TAG_variant";
11822 case DW_TAG_common_block:
11823 return "DW_TAG_common_block";
11824 case DW_TAG_common_inclusion:
11825 return "DW_TAG_common_inclusion";
11826 case DW_TAG_inheritance:
11827 return "DW_TAG_inheritance";
11828 case DW_TAG_inlined_subroutine:
11829 return "DW_TAG_inlined_subroutine";
11830 case DW_TAG_module:
11831 return "DW_TAG_module";
11832 case DW_TAG_ptr_to_member_type:
11833 return "DW_TAG_ptr_to_member_type";
11834 case DW_TAG_set_type:
11835 return "DW_TAG_set_type";
11836 case DW_TAG_subrange_type:
11837 return "DW_TAG_subrange_type";
11838 case DW_TAG_with_stmt:
11839 return "DW_TAG_with_stmt";
11840 case DW_TAG_access_declaration:
11841 return "DW_TAG_access_declaration";
11842 case DW_TAG_base_type:
11843 return "DW_TAG_base_type";
11844 case DW_TAG_catch_block:
11845 return "DW_TAG_catch_block";
11846 case DW_TAG_const_type:
11847 return "DW_TAG_const_type";
11848 case DW_TAG_constant:
11849 return "DW_TAG_constant";
11850 case DW_TAG_enumerator:
11851 return "DW_TAG_enumerator";
11852 case DW_TAG_file_type:
11853 return "DW_TAG_file_type";
11854 case DW_TAG_friend:
11855 return "DW_TAG_friend";
11856 case DW_TAG_namelist:
11857 return "DW_TAG_namelist";
11858 case DW_TAG_namelist_item:
11859 return "DW_TAG_namelist_item";
11860 case DW_TAG_packed_type:
11861 return "DW_TAG_packed_type";
11862 case DW_TAG_subprogram:
11863 return "DW_TAG_subprogram";
11864 case DW_TAG_template_type_param:
11865 return "DW_TAG_template_type_param";
11866 case DW_TAG_template_value_param:
11867 return "DW_TAG_template_value_param";
11868 case DW_TAG_thrown_type:
11869 return "DW_TAG_thrown_type";
11870 case DW_TAG_try_block:
11871 return "DW_TAG_try_block";
11872 case DW_TAG_variant_part:
11873 return "DW_TAG_variant_part";
11874 case DW_TAG_variable:
11875 return "DW_TAG_variable";
11876 case DW_TAG_volatile_type:
11877 return "DW_TAG_volatile_type";
11878 case DW_TAG_dwarf_procedure:
11879 return "DW_TAG_dwarf_procedure";
11880 case DW_TAG_restrict_type:
11881 return "DW_TAG_restrict_type";
11882 case DW_TAG_interface_type:
11883 return "DW_TAG_interface_type";
11884 case DW_TAG_namespace:
11885 return "DW_TAG_namespace";
11886 case DW_TAG_imported_module:
11887 return "DW_TAG_imported_module";
11888 case DW_TAG_unspecified_type:
11889 return "DW_TAG_unspecified_type";
11890 case DW_TAG_partial_unit:
11891 return "DW_TAG_partial_unit";
11892 case DW_TAG_imported_unit:
11893 return "DW_TAG_imported_unit";
11894 case DW_TAG_condition:
11895 return "DW_TAG_condition";
11896 case DW_TAG_shared_type:
11897 return "DW_TAG_shared_type";
11898 case DW_TAG_type_unit:
11899 return "DW_TAG_type_unit";
11900 case DW_TAG_MIPS_loop:
11901 return "DW_TAG_MIPS_loop";
11902 case DW_TAG_HP_array_descriptor:
11903 return "DW_TAG_HP_array_descriptor";
11904 case DW_TAG_format_label:
11905 return "DW_TAG_format_label";
11906 case DW_TAG_function_template:
11907 return "DW_TAG_function_template";
11908 case DW_TAG_class_template:
11909 return "DW_TAG_class_template";
11910 case DW_TAG_GNU_BINCL:
11911 return "DW_TAG_GNU_BINCL";
11912 case DW_TAG_GNU_EINCL:
11913 return "DW_TAG_GNU_EINCL";
11914 case DW_TAG_upc_shared_type:
11915 return "DW_TAG_upc_shared_type";
11916 case DW_TAG_upc_strict_type:
11917 return "DW_TAG_upc_strict_type";
11918 case DW_TAG_upc_relaxed_type:
11919 return "DW_TAG_upc_relaxed_type";
11920 case DW_TAG_PGI_kanji_type:
11921 return "DW_TAG_PGI_kanji_type";
11922 case DW_TAG_PGI_interface_block:
11923 return "DW_TAG_PGI_interface_block";
11924 default:
11925 return "DW_TAG_<unknown>";
11926 }
11927 }
11928
11929 /* Convert a DWARF attribute code into its string name. */
11930
11931 static char *
11932 dwarf_attr_name (unsigned attr)
11933 {
11934 switch (attr)
11935 {
11936 case DW_AT_sibling:
11937 return "DW_AT_sibling";
11938 case DW_AT_location:
11939 return "DW_AT_location";
11940 case DW_AT_name:
11941 return "DW_AT_name";
11942 case DW_AT_ordering:
11943 return "DW_AT_ordering";
11944 case DW_AT_subscr_data:
11945 return "DW_AT_subscr_data";
11946 case DW_AT_byte_size:
11947 return "DW_AT_byte_size";
11948 case DW_AT_bit_offset:
11949 return "DW_AT_bit_offset";
11950 case DW_AT_bit_size:
11951 return "DW_AT_bit_size";
11952 case DW_AT_element_list:
11953 return "DW_AT_element_list";
11954 case DW_AT_stmt_list:
11955 return "DW_AT_stmt_list";
11956 case DW_AT_low_pc:
11957 return "DW_AT_low_pc";
11958 case DW_AT_high_pc:
11959 return "DW_AT_high_pc";
11960 case DW_AT_language:
11961 return "DW_AT_language";
11962 case DW_AT_member:
11963 return "DW_AT_member";
11964 case DW_AT_discr:
11965 return "DW_AT_discr";
11966 case DW_AT_discr_value:
11967 return "DW_AT_discr_value";
11968 case DW_AT_visibility:
11969 return "DW_AT_visibility";
11970 case DW_AT_import:
11971 return "DW_AT_import";
11972 case DW_AT_string_length:
11973 return "DW_AT_string_length";
11974 case DW_AT_common_reference:
11975 return "DW_AT_common_reference";
11976 case DW_AT_comp_dir:
11977 return "DW_AT_comp_dir";
11978 case DW_AT_const_value:
11979 return "DW_AT_const_value";
11980 case DW_AT_containing_type:
11981 return "DW_AT_containing_type";
11982 case DW_AT_default_value:
11983 return "DW_AT_default_value";
11984 case DW_AT_inline:
11985 return "DW_AT_inline";
11986 case DW_AT_is_optional:
11987 return "DW_AT_is_optional";
11988 case DW_AT_lower_bound:
11989 return "DW_AT_lower_bound";
11990 case DW_AT_producer:
11991 return "DW_AT_producer";
11992 case DW_AT_prototyped:
11993 return "DW_AT_prototyped";
11994 case DW_AT_return_addr:
11995 return "DW_AT_return_addr";
11996 case DW_AT_start_scope:
11997 return "DW_AT_start_scope";
11998 case DW_AT_bit_stride:
11999 return "DW_AT_bit_stride";
12000 case DW_AT_upper_bound:
12001 return "DW_AT_upper_bound";
12002 case DW_AT_abstract_origin:
12003 return "DW_AT_abstract_origin";
12004 case DW_AT_accessibility:
12005 return "DW_AT_accessibility";
12006 case DW_AT_address_class:
12007 return "DW_AT_address_class";
12008 case DW_AT_artificial:
12009 return "DW_AT_artificial";
12010 case DW_AT_base_types:
12011 return "DW_AT_base_types";
12012 case DW_AT_calling_convention:
12013 return "DW_AT_calling_convention";
12014 case DW_AT_count:
12015 return "DW_AT_count";
12016 case DW_AT_data_member_location:
12017 return "DW_AT_data_member_location";
12018 case DW_AT_decl_column:
12019 return "DW_AT_decl_column";
12020 case DW_AT_decl_file:
12021 return "DW_AT_decl_file";
12022 case DW_AT_decl_line:
12023 return "DW_AT_decl_line";
12024 case DW_AT_declaration:
12025 return "DW_AT_declaration";
12026 case DW_AT_discr_list:
12027 return "DW_AT_discr_list";
12028 case DW_AT_encoding:
12029 return "DW_AT_encoding";
12030 case DW_AT_external:
12031 return "DW_AT_external";
12032 case DW_AT_frame_base:
12033 return "DW_AT_frame_base";
12034 case DW_AT_friend:
12035 return "DW_AT_friend";
12036 case DW_AT_identifier_case:
12037 return "DW_AT_identifier_case";
12038 case DW_AT_macro_info:
12039 return "DW_AT_macro_info";
12040 case DW_AT_namelist_items:
12041 return "DW_AT_namelist_items";
12042 case DW_AT_priority:
12043 return "DW_AT_priority";
12044 case DW_AT_segment:
12045 return "DW_AT_segment";
12046 case DW_AT_specification:
12047 return "DW_AT_specification";
12048 case DW_AT_static_link:
12049 return "DW_AT_static_link";
12050 case DW_AT_type:
12051 return "DW_AT_type";
12052 case DW_AT_use_location:
12053 return "DW_AT_use_location";
12054 case DW_AT_variable_parameter:
12055 return "DW_AT_variable_parameter";
12056 case DW_AT_virtuality:
12057 return "DW_AT_virtuality";
12058 case DW_AT_vtable_elem_location:
12059 return "DW_AT_vtable_elem_location";
12060 /* DWARF 3 values. */
12061 case DW_AT_allocated:
12062 return "DW_AT_allocated";
12063 case DW_AT_associated:
12064 return "DW_AT_associated";
12065 case DW_AT_data_location:
12066 return "DW_AT_data_location";
12067 case DW_AT_byte_stride:
12068 return "DW_AT_byte_stride";
12069 case DW_AT_entry_pc:
12070 return "DW_AT_entry_pc";
12071 case DW_AT_use_UTF8:
12072 return "DW_AT_use_UTF8";
12073 case DW_AT_extension:
12074 return "DW_AT_extension";
12075 case DW_AT_ranges:
12076 return "DW_AT_ranges";
12077 case DW_AT_trampoline:
12078 return "DW_AT_trampoline";
12079 case DW_AT_call_column:
12080 return "DW_AT_call_column";
12081 case DW_AT_call_file:
12082 return "DW_AT_call_file";
12083 case DW_AT_call_line:
12084 return "DW_AT_call_line";
12085 case DW_AT_description:
12086 return "DW_AT_description";
12087 case DW_AT_binary_scale:
12088 return "DW_AT_binary_scale";
12089 case DW_AT_decimal_scale:
12090 return "DW_AT_decimal_scale";
12091 case DW_AT_small:
12092 return "DW_AT_small";
12093 case DW_AT_decimal_sign:
12094 return "DW_AT_decimal_sign";
12095 case DW_AT_digit_count:
12096 return "DW_AT_digit_count";
12097 case DW_AT_picture_string:
12098 return "DW_AT_picture_string";
12099 case DW_AT_mutable:
12100 return "DW_AT_mutable";
12101 case DW_AT_threads_scaled:
12102 return "DW_AT_threads_scaled";
12103 case DW_AT_explicit:
12104 return "DW_AT_explicit";
12105 case DW_AT_object_pointer:
12106 return "DW_AT_object_pointer";
12107 case DW_AT_endianity:
12108 return "DW_AT_endianity";
12109 case DW_AT_elemental:
12110 return "DW_AT_elemental";
12111 case DW_AT_pure:
12112 return "DW_AT_pure";
12113 case DW_AT_recursive:
12114 return "DW_AT_recursive";
12115 /* DWARF 4 values. */
12116 case DW_AT_signature:
12117 return "DW_AT_signature";
12118 case DW_AT_linkage_name:
12119 return "DW_AT_linkage_name";
12120 /* SGI/MIPS extensions. */
12121 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12122 case DW_AT_MIPS_fde:
12123 return "DW_AT_MIPS_fde";
12124 #endif
12125 case DW_AT_MIPS_loop_begin:
12126 return "DW_AT_MIPS_loop_begin";
12127 case DW_AT_MIPS_tail_loop_begin:
12128 return "DW_AT_MIPS_tail_loop_begin";
12129 case DW_AT_MIPS_epilog_begin:
12130 return "DW_AT_MIPS_epilog_begin";
12131 case DW_AT_MIPS_loop_unroll_factor:
12132 return "DW_AT_MIPS_loop_unroll_factor";
12133 case DW_AT_MIPS_software_pipeline_depth:
12134 return "DW_AT_MIPS_software_pipeline_depth";
12135 case DW_AT_MIPS_linkage_name:
12136 return "DW_AT_MIPS_linkage_name";
12137 case DW_AT_MIPS_stride:
12138 return "DW_AT_MIPS_stride";
12139 case DW_AT_MIPS_abstract_name:
12140 return "DW_AT_MIPS_abstract_name";
12141 case DW_AT_MIPS_clone_origin:
12142 return "DW_AT_MIPS_clone_origin";
12143 case DW_AT_MIPS_has_inlines:
12144 return "DW_AT_MIPS_has_inlines";
12145 /* HP extensions. */
12146 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12147 case DW_AT_HP_block_index:
12148 return "DW_AT_HP_block_index";
12149 #endif
12150 case DW_AT_HP_unmodifiable:
12151 return "DW_AT_HP_unmodifiable";
12152 case DW_AT_HP_actuals_stmt_list:
12153 return "DW_AT_HP_actuals_stmt_list";
12154 case DW_AT_HP_proc_per_section:
12155 return "DW_AT_HP_proc_per_section";
12156 case DW_AT_HP_raw_data_ptr:
12157 return "DW_AT_HP_raw_data_ptr";
12158 case DW_AT_HP_pass_by_reference:
12159 return "DW_AT_HP_pass_by_reference";
12160 case DW_AT_HP_opt_level:
12161 return "DW_AT_HP_opt_level";
12162 case DW_AT_HP_prof_version_id:
12163 return "DW_AT_HP_prof_version_id";
12164 case DW_AT_HP_opt_flags:
12165 return "DW_AT_HP_opt_flags";
12166 case DW_AT_HP_cold_region_low_pc:
12167 return "DW_AT_HP_cold_region_low_pc";
12168 case DW_AT_HP_cold_region_high_pc:
12169 return "DW_AT_HP_cold_region_high_pc";
12170 case DW_AT_HP_all_variables_modifiable:
12171 return "DW_AT_HP_all_variables_modifiable";
12172 case DW_AT_HP_linkage_name:
12173 return "DW_AT_HP_linkage_name";
12174 case DW_AT_HP_prof_flags:
12175 return "DW_AT_HP_prof_flags";
12176 /* GNU extensions. */
12177 case DW_AT_sf_names:
12178 return "DW_AT_sf_names";
12179 case DW_AT_src_info:
12180 return "DW_AT_src_info";
12181 case DW_AT_mac_info:
12182 return "DW_AT_mac_info";
12183 case DW_AT_src_coords:
12184 return "DW_AT_src_coords";
12185 case DW_AT_body_begin:
12186 return "DW_AT_body_begin";
12187 case DW_AT_body_end:
12188 return "DW_AT_body_end";
12189 case DW_AT_GNU_vector:
12190 return "DW_AT_GNU_vector";
12191 case DW_AT_GNU_odr_signature:
12192 return "DW_AT_GNU_odr_signature";
12193 /* VMS extensions. */
12194 case DW_AT_VMS_rtnbeg_pd_address:
12195 return "DW_AT_VMS_rtnbeg_pd_address";
12196 /* UPC extension. */
12197 case DW_AT_upc_threads_scaled:
12198 return "DW_AT_upc_threads_scaled";
12199 /* PGI (STMicroelectronics) extensions. */
12200 case DW_AT_PGI_lbase:
12201 return "DW_AT_PGI_lbase";
12202 case DW_AT_PGI_soffset:
12203 return "DW_AT_PGI_soffset";
12204 case DW_AT_PGI_lstride:
12205 return "DW_AT_PGI_lstride";
12206 default:
12207 return "DW_AT_<unknown>";
12208 }
12209 }
12210
12211 /* Convert a DWARF value form code into its string name. */
12212
12213 static char *
12214 dwarf_form_name (unsigned form)
12215 {
12216 switch (form)
12217 {
12218 case DW_FORM_addr:
12219 return "DW_FORM_addr";
12220 case DW_FORM_block2:
12221 return "DW_FORM_block2";
12222 case DW_FORM_block4:
12223 return "DW_FORM_block4";
12224 case DW_FORM_data2:
12225 return "DW_FORM_data2";
12226 case DW_FORM_data4:
12227 return "DW_FORM_data4";
12228 case DW_FORM_data8:
12229 return "DW_FORM_data8";
12230 case DW_FORM_string:
12231 return "DW_FORM_string";
12232 case DW_FORM_block:
12233 return "DW_FORM_block";
12234 case DW_FORM_block1:
12235 return "DW_FORM_block1";
12236 case DW_FORM_data1:
12237 return "DW_FORM_data1";
12238 case DW_FORM_flag:
12239 return "DW_FORM_flag";
12240 case DW_FORM_sdata:
12241 return "DW_FORM_sdata";
12242 case DW_FORM_strp:
12243 return "DW_FORM_strp";
12244 case DW_FORM_udata:
12245 return "DW_FORM_udata";
12246 case DW_FORM_ref_addr:
12247 return "DW_FORM_ref_addr";
12248 case DW_FORM_ref1:
12249 return "DW_FORM_ref1";
12250 case DW_FORM_ref2:
12251 return "DW_FORM_ref2";
12252 case DW_FORM_ref4:
12253 return "DW_FORM_ref4";
12254 case DW_FORM_ref8:
12255 return "DW_FORM_ref8";
12256 case DW_FORM_ref_udata:
12257 return "DW_FORM_ref_udata";
12258 case DW_FORM_indirect:
12259 return "DW_FORM_indirect";
12260 case DW_FORM_sec_offset:
12261 return "DW_FORM_sec_offset";
12262 case DW_FORM_exprloc:
12263 return "DW_FORM_exprloc";
12264 case DW_FORM_flag_present:
12265 return "DW_FORM_flag_present";
12266 case DW_FORM_sig8:
12267 return "DW_FORM_sig8";
12268 default:
12269 return "DW_FORM_<unknown>";
12270 }
12271 }
12272
12273 /* Convert a DWARF stack opcode into its string name. */
12274
12275 const char *
12276 dwarf_stack_op_name (unsigned op, int def)
12277 {
12278 switch (op)
12279 {
12280 case DW_OP_addr:
12281 return "DW_OP_addr";
12282 case DW_OP_deref:
12283 return "DW_OP_deref";
12284 case DW_OP_const1u:
12285 return "DW_OP_const1u";
12286 case DW_OP_const1s:
12287 return "DW_OP_const1s";
12288 case DW_OP_const2u:
12289 return "DW_OP_const2u";
12290 case DW_OP_const2s:
12291 return "DW_OP_const2s";
12292 case DW_OP_const4u:
12293 return "DW_OP_const4u";
12294 case DW_OP_const4s:
12295 return "DW_OP_const4s";
12296 case DW_OP_const8u:
12297 return "DW_OP_const8u";
12298 case DW_OP_const8s:
12299 return "DW_OP_const8s";
12300 case DW_OP_constu:
12301 return "DW_OP_constu";
12302 case DW_OP_consts:
12303 return "DW_OP_consts";
12304 case DW_OP_dup:
12305 return "DW_OP_dup";
12306 case DW_OP_drop:
12307 return "DW_OP_drop";
12308 case DW_OP_over:
12309 return "DW_OP_over";
12310 case DW_OP_pick:
12311 return "DW_OP_pick";
12312 case DW_OP_swap:
12313 return "DW_OP_swap";
12314 case DW_OP_rot:
12315 return "DW_OP_rot";
12316 case DW_OP_xderef:
12317 return "DW_OP_xderef";
12318 case DW_OP_abs:
12319 return "DW_OP_abs";
12320 case DW_OP_and:
12321 return "DW_OP_and";
12322 case DW_OP_div:
12323 return "DW_OP_div";
12324 case DW_OP_minus:
12325 return "DW_OP_minus";
12326 case DW_OP_mod:
12327 return "DW_OP_mod";
12328 case DW_OP_mul:
12329 return "DW_OP_mul";
12330 case DW_OP_neg:
12331 return "DW_OP_neg";
12332 case DW_OP_not:
12333 return "DW_OP_not";
12334 case DW_OP_or:
12335 return "DW_OP_or";
12336 case DW_OP_plus:
12337 return "DW_OP_plus";
12338 case DW_OP_plus_uconst:
12339 return "DW_OP_plus_uconst";
12340 case DW_OP_shl:
12341 return "DW_OP_shl";
12342 case DW_OP_shr:
12343 return "DW_OP_shr";
12344 case DW_OP_shra:
12345 return "DW_OP_shra";
12346 case DW_OP_xor:
12347 return "DW_OP_xor";
12348 case DW_OP_bra:
12349 return "DW_OP_bra";
12350 case DW_OP_eq:
12351 return "DW_OP_eq";
12352 case DW_OP_ge:
12353 return "DW_OP_ge";
12354 case DW_OP_gt:
12355 return "DW_OP_gt";
12356 case DW_OP_le:
12357 return "DW_OP_le";
12358 case DW_OP_lt:
12359 return "DW_OP_lt";
12360 case DW_OP_ne:
12361 return "DW_OP_ne";
12362 case DW_OP_skip:
12363 return "DW_OP_skip";
12364 case DW_OP_lit0:
12365 return "DW_OP_lit0";
12366 case DW_OP_lit1:
12367 return "DW_OP_lit1";
12368 case DW_OP_lit2:
12369 return "DW_OP_lit2";
12370 case DW_OP_lit3:
12371 return "DW_OP_lit3";
12372 case DW_OP_lit4:
12373 return "DW_OP_lit4";
12374 case DW_OP_lit5:
12375 return "DW_OP_lit5";
12376 case DW_OP_lit6:
12377 return "DW_OP_lit6";
12378 case DW_OP_lit7:
12379 return "DW_OP_lit7";
12380 case DW_OP_lit8:
12381 return "DW_OP_lit8";
12382 case DW_OP_lit9:
12383 return "DW_OP_lit9";
12384 case DW_OP_lit10:
12385 return "DW_OP_lit10";
12386 case DW_OP_lit11:
12387 return "DW_OP_lit11";
12388 case DW_OP_lit12:
12389 return "DW_OP_lit12";
12390 case DW_OP_lit13:
12391 return "DW_OP_lit13";
12392 case DW_OP_lit14:
12393 return "DW_OP_lit14";
12394 case DW_OP_lit15:
12395 return "DW_OP_lit15";
12396 case DW_OP_lit16:
12397 return "DW_OP_lit16";
12398 case DW_OP_lit17:
12399 return "DW_OP_lit17";
12400 case DW_OP_lit18:
12401 return "DW_OP_lit18";
12402 case DW_OP_lit19:
12403 return "DW_OP_lit19";
12404 case DW_OP_lit20:
12405 return "DW_OP_lit20";
12406 case DW_OP_lit21:
12407 return "DW_OP_lit21";
12408 case DW_OP_lit22:
12409 return "DW_OP_lit22";
12410 case DW_OP_lit23:
12411 return "DW_OP_lit23";
12412 case DW_OP_lit24:
12413 return "DW_OP_lit24";
12414 case DW_OP_lit25:
12415 return "DW_OP_lit25";
12416 case DW_OP_lit26:
12417 return "DW_OP_lit26";
12418 case DW_OP_lit27:
12419 return "DW_OP_lit27";
12420 case DW_OP_lit28:
12421 return "DW_OP_lit28";
12422 case DW_OP_lit29:
12423 return "DW_OP_lit29";
12424 case DW_OP_lit30:
12425 return "DW_OP_lit30";
12426 case DW_OP_lit31:
12427 return "DW_OP_lit31";
12428 case DW_OP_reg0:
12429 return "DW_OP_reg0";
12430 case DW_OP_reg1:
12431 return "DW_OP_reg1";
12432 case DW_OP_reg2:
12433 return "DW_OP_reg2";
12434 case DW_OP_reg3:
12435 return "DW_OP_reg3";
12436 case DW_OP_reg4:
12437 return "DW_OP_reg4";
12438 case DW_OP_reg5:
12439 return "DW_OP_reg5";
12440 case DW_OP_reg6:
12441 return "DW_OP_reg6";
12442 case DW_OP_reg7:
12443 return "DW_OP_reg7";
12444 case DW_OP_reg8:
12445 return "DW_OP_reg8";
12446 case DW_OP_reg9:
12447 return "DW_OP_reg9";
12448 case DW_OP_reg10:
12449 return "DW_OP_reg10";
12450 case DW_OP_reg11:
12451 return "DW_OP_reg11";
12452 case DW_OP_reg12:
12453 return "DW_OP_reg12";
12454 case DW_OP_reg13:
12455 return "DW_OP_reg13";
12456 case DW_OP_reg14:
12457 return "DW_OP_reg14";
12458 case DW_OP_reg15:
12459 return "DW_OP_reg15";
12460 case DW_OP_reg16:
12461 return "DW_OP_reg16";
12462 case DW_OP_reg17:
12463 return "DW_OP_reg17";
12464 case DW_OP_reg18:
12465 return "DW_OP_reg18";
12466 case DW_OP_reg19:
12467 return "DW_OP_reg19";
12468 case DW_OP_reg20:
12469 return "DW_OP_reg20";
12470 case DW_OP_reg21:
12471 return "DW_OP_reg21";
12472 case DW_OP_reg22:
12473 return "DW_OP_reg22";
12474 case DW_OP_reg23:
12475 return "DW_OP_reg23";
12476 case DW_OP_reg24:
12477 return "DW_OP_reg24";
12478 case DW_OP_reg25:
12479 return "DW_OP_reg25";
12480 case DW_OP_reg26:
12481 return "DW_OP_reg26";
12482 case DW_OP_reg27:
12483 return "DW_OP_reg27";
12484 case DW_OP_reg28:
12485 return "DW_OP_reg28";
12486 case DW_OP_reg29:
12487 return "DW_OP_reg29";
12488 case DW_OP_reg30:
12489 return "DW_OP_reg30";
12490 case DW_OP_reg31:
12491 return "DW_OP_reg31";
12492 case DW_OP_breg0:
12493 return "DW_OP_breg0";
12494 case DW_OP_breg1:
12495 return "DW_OP_breg1";
12496 case DW_OP_breg2:
12497 return "DW_OP_breg2";
12498 case DW_OP_breg3:
12499 return "DW_OP_breg3";
12500 case DW_OP_breg4:
12501 return "DW_OP_breg4";
12502 case DW_OP_breg5:
12503 return "DW_OP_breg5";
12504 case DW_OP_breg6:
12505 return "DW_OP_breg6";
12506 case DW_OP_breg7:
12507 return "DW_OP_breg7";
12508 case DW_OP_breg8:
12509 return "DW_OP_breg8";
12510 case DW_OP_breg9:
12511 return "DW_OP_breg9";
12512 case DW_OP_breg10:
12513 return "DW_OP_breg10";
12514 case DW_OP_breg11:
12515 return "DW_OP_breg11";
12516 case DW_OP_breg12:
12517 return "DW_OP_breg12";
12518 case DW_OP_breg13:
12519 return "DW_OP_breg13";
12520 case DW_OP_breg14:
12521 return "DW_OP_breg14";
12522 case DW_OP_breg15:
12523 return "DW_OP_breg15";
12524 case DW_OP_breg16:
12525 return "DW_OP_breg16";
12526 case DW_OP_breg17:
12527 return "DW_OP_breg17";
12528 case DW_OP_breg18:
12529 return "DW_OP_breg18";
12530 case DW_OP_breg19:
12531 return "DW_OP_breg19";
12532 case DW_OP_breg20:
12533 return "DW_OP_breg20";
12534 case DW_OP_breg21:
12535 return "DW_OP_breg21";
12536 case DW_OP_breg22:
12537 return "DW_OP_breg22";
12538 case DW_OP_breg23:
12539 return "DW_OP_breg23";
12540 case DW_OP_breg24:
12541 return "DW_OP_breg24";
12542 case DW_OP_breg25:
12543 return "DW_OP_breg25";
12544 case DW_OP_breg26:
12545 return "DW_OP_breg26";
12546 case DW_OP_breg27:
12547 return "DW_OP_breg27";
12548 case DW_OP_breg28:
12549 return "DW_OP_breg28";
12550 case DW_OP_breg29:
12551 return "DW_OP_breg29";
12552 case DW_OP_breg30:
12553 return "DW_OP_breg30";
12554 case DW_OP_breg31:
12555 return "DW_OP_breg31";
12556 case DW_OP_regx:
12557 return "DW_OP_regx";
12558 case DW_OP_fbreg:
12559 return "DW_OP_fbreg";
12560 case DW_OP_bregx:
12561 return "DW_OP_bregx";
12562 case DW_OP_piece:
12563 return "DW_OP_piece";
12564 case DW_OP_deref_size:
12565 return "DW_OP_deref_size";
12566 case DW_OP_xderef_size:
12567 return "DW_OP_xderef_size";
12568 case DW_OP_nop:
12569 return "DW_OP_nop";
12570 /* DWARF 3 extensions. */
12571 case DW_OP_push_object_address:
12572 return "DW_OP_push_object_address";
12573 case DW_OP_call2:
12574 return "DW_OP_call2";
12575 case DW_OP_call4:
12576 return "DW_OP_call4";
12577 case DW_OP_call_ref:
12578 return "DW_OP_call_ref";
12579 case DW_OP_form_tls_address:
12580 return "DW_OP_form_tls_address";
12581 case DW_OP_call_frame_cfa:
12582 return "DW_OP_call_frame_cfa";
12583 case DW_OP_bit_piece:
12584 return "DW_OP_bit_piece";
12585 /* DWARF 4 extensions. */
12586 case DW_OP_implicit_value:
12587 return "DW_OP_implicit_value";
12588 case DW_OP_stack_value:
12589 return "DW_OP_stack_value";
12590 /* GNU extensions. */
12591 case DW_OP_GNU_push_tls_address:
12592 return "DW_OP_GNU_push_tls_address";
12593 case DW_OP_GNU_uninit:
12594 return "DW_OP_GNU_uninit";
12595 case DW_OP_GNU_implicit_pointer:
12596 return "DW_OP_GNU_implicit_pointer";
12597 default:
12598 return def ? "OP_<unknown>" : NULL;
12599 }
12600 }
12601
12602 static char *
12603 dwarf_bool_name (unsigned mybool)
12604 {
12605 if (mybool)
12606 return "TRUE";
12607 else
12608 return "FALSE";
12609 }
12610
12611 /* Convert a DWARF type code into its string name. */
12612
12613 static char *
12614 dwarf_type_encoding_name (unsigned enc)
12615 {
12616 switch (enc)
12617 {
12618 case DW_ATE_void:
12619 return "DW_ATE_void";
12620 case DW_ATE_address:
12621 return "DW_ATE_address";
12622 case DW_ATE_boolean:
12623 return "DW_ATE_boolean";
12624 case DW_ATE_complex_float:
12625 return "DW_ATE_complex_float";
12626 case DW_ATE_float:
12627 return "DW_ATE_float";
12628 case DW_ATE_signed:
12629 return "DW_ATE_signed";
12630 case DW_ATE_signed_char:
12631 return "DW_ATE_signed_char";
12632 case DW_ATE_unsigned:
12633 return "DW_ATE_unsigned";
12634 case DW_ATE_unsigned_char:
12635 return "DW_ATE_unsigned_char";
12636 /* DWARF 3. */
12637 case DW_ATE_imaginary_float:
12638 return "DW_ATE_imaginary_float";
12639 case DW_ATE_packed_decimal:
12640 return "DW_ATE_packed_decimal";
12641 case DW_ATE_numeric_string:
12642 return "DW_ATE_numeric_string";
12643 case DW_ATE_edited:
12644 return "DW_ATE_edited";
12645 case DW_ATE_signed_fixed:
12646 return "DW_ATE_signed_fixed";
12647 case DW_ATE_unsigned_fixed:
12648 return "DW_ATE_unsigned_fixed";
12649 case DW_ATE_decimal_float:
12650 return "DW_ATE_decimal_float";
12651 /* DWARF 4. */
12652 case DW_ATE_UTF:
12653 return "DW_ATE_UTF";
12654 /* HP extensions. */
12655 case DW_ATE_HP_float80:
12656 return "DW_ATE_HP_float80";
12657 case DW_ATE_HP_complex_float80:
12658 return "DW_ATE_HP_complex_float80";
12659 case DW_ATE_HP_float128:
12660 return "DW_ATE_HP_float128";
12661 case DW_ATE_HP_complex_float128:
12662 return "DW_ATE_HP_complex_float128";
12663 case DW_ATE_HP_floathpintel:
12664 return "DW_ATE_HP_floathpintel";
12665 case DW_ATE_HP_imaginary_float80:
12666 return "DW_ATE_HP_imaginary_float80";
12667 case DW_ATE_HP_imaginary_float128:
12668 return "DW_ATE_HP_imaginary_float128";
12669 default:
12670 return "DW_ATE_<unknown>";
12671 }
12672 }
12673
12674 /* Convert a DWARF call frame info operation to its string name. */
12675
12676 #if 0
12677 static char *
12678 dwarf_cfi_name (unsigned cfi_opc)
12679 {
12680 switch (cfi_opc)
12681 {
12682 case DW_CFA_advance_loc:
12683 return "DW_CFA_advance_loc";
12684 case DW_CFA_offset:
12685 return "DW_CFA_offset";
12686 case DW_CFA_restore:
12687 return "DW_CFA_restore";
12688 case DW_CFA_nop:
12689 return "DW_CFA_nop";
12690 case DW_CFA_set_loc:
12691 return "DW_CFA_set_loc";
12692 case DW_CFA_advance_loc1:
12693 return "DW_CFA_advance_loc1";
12694 case DW_CFA_advance_loc2:
12695 return "DW_CFA_advance_loc2";
12696 case DW_CFA_advance_loc4:
12697 return "DW_CFA_advance_loc4";
12698 case DW_CFA_offset_extended:
12699 return "DW_CFA_offset_extended";
12700 case DW_CFA_restore_extended:
12701 return "DW_CFA_restore_extended";
12702 case DW_CFA_undefined:
12703 return "DW_CFA_undefined";
12704 case DW_CFA_same_value:
12705 return "DW_CFA_same_value";
12706 case DW_CFA_register:
12707 return "DW_CFA_register";
12708 case DW_CFA_remember_state:
12709 return "DW_CFA_remember_state";
12710 case DW_CFA_restore_state:
12711 return "DW_CFA_restore_state";
12712 case DW_CFA_def_cfa:
12713 return "DW_CFA_def_cfa";
12714 case DW_CFA_def_cfa_register:
12715 return "DW_CFA_def_cfa_register";
12716 case DW_CFA_def_cfa_offset:
12717 return "DW_CFA_def_cfa_offset";
12718 /* DWARF 3. */
12719 case DW_CFA_def_cfa_expression:
12720 return "DW_CFA_def_cfa_expression";
12721 case DW_CFA_expression:
12722 return "DW_CFA_expression";
12723 case DW_CFA_offset_extended_sf:
12724 return "DW_CFA_offset_extended_sf";
12725 case DW_CFA_def_cfa_sf:
12726 return "DW_CFA_def_cfa_sf";
12727 case DW_CFA_def_cfa_offset_sf:
12728 return "DW_CFA_def_cfa_offset_sf";
12729 case DW_CFA_val_offset:
12730 return "DW_CFA_val_offset";
12731 case DW_CFA_val_offset_sf:
12732 return "DW_CFA_val_offset_sf";
12733 case DW_CFA_val_expression:
12734 return "DW_CFA_val_expression";
12735 /* SGI/MIPS specific. */
12736 case DW_CFA_MIPS_advance_loc8:
12737 return "DW_CFA_MIPS_advance_loc8";
12738 /* GNU extensions. */
12739 case DW_CFA_GNU_window_save:
12740 return "DW_CFA_GNU_window_save";
12741 case DW_CFA_GNU_args_size:
12742 return "DW_CFA_GNU_args_size";
12743 case DW_CFA_GNU_negative_offset_extended:
12744 return "DW_CFA_GNU_negative_offset_extended";
12745 default:
12746 return "DW_CFA_<unknown>";
12747 }
12748 }
12749 #endif
12750
12751 static void
12752 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
12753 {
12754 unsigned int i;
12755
12756 print_spaces (indent, f);
12757 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
12758 dwarf_tag_name (die->tag), die->abbrev, die->offset);
12759
12760 if (die->parent != NULL)
12761 {
12762 print_spaces (indent, f);
12763 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
12764 die->parent->offset);
12765 }
12766
12767 print_spaces (indent, f);
12768 fprintf_unfiltered (f, " has children: %s\n",
12769 dwarf_bool_name (die->child != NULL));
12770
12771 print_spaces (indent, f);
12772 fprintf_unfiltered (f, " attributes:\n");
12773
12774 for (i = 0; i < die->num_attrs; ++i)
12775 {
12776 print_spaces (indent, f);
12777 fprintf_unfiltered (f, " %s (%s) ",
12778 dwarf_attr_name (die->attrs[i].name),
12779 dwarf_form_name (die->attrs[i].form));
12780
12781 switch (die->attrs[i].form)
12782 {
12783 case DW_FORM_ref_addr:
12784 case DW_FORM_addr:
12785 fprintf_unfiltered (f, "address: ");
12786 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
12787 break;
12788 case DW_FORM_block2:
12789 case DW_FORM_block4:
12790 case DW_FORM_block:
12791 case DW_FORM_block1:
12792 fprintf_unfiltered (f, "block: size %d", DW_BLOCK (&die->attrs[i])->size);
12793 break;
12794 case DW_FORM_exprloc:
12795 fprintf_unfiltered (f, "expression: size %u",
12796 DW_BLOCK (&die->attrs[i])->size);
12797 break;
12798 case DW_FORM_ref1:
12799 case DW_FORM_ref2:
12800 case DW_FORM_ref4:
12801 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
12802 (long) (DW_ADDR (&die->attrs[i])));
12803 break;
12804 case DW_FORM_data1:
12805 case DW_FORM_data2:
12806 case DW_FORM_data4:
12807 case DW_FORM_data8:
12808 case DW_FORM_udata:
12809 case DW_FORM_sdata:
12810 fprintf_unfiltered (f, "constant: %s",
12811 pulongest (DW_UNSND (&die->attrs[i])));
12812 break;
12813 case DW_FORM_sec_offset:
12814 fprintf_unfiltered (f, "section offset: %s",
12815 pulongest (DW_UNSND (&die->attrs[i])));
12816 break;
12817 case DW_FORM_sig8:
12818 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
12819 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
12820 DW_SIGNATURED_TYPE (&die->attrs[i])->offset);
12821 else
12822 fprintf_unfiltered (f, "signatured type, offset: unknown");
12823 break;
12824 case DW_FORM_string:
12825 case DW_FORM_strp:
12826 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
12827 DW_STRING (&die->attrs[i])
12828 ? DW_STRING (&die->attrs[i]) : "",
12829 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
12830 break;
12831 case DW_FORM_flag:
12832 if (DW_UNSND (&die->attrs[i]))
12833 fprintf_unfiltered (f, "flag: TRUE");
12834 else
12835 fprintf_unfiltered (f, "flag: FALSE");
12836 break;
12837 case DW_FORM_flag_present:
12838 fprintf_unfiltered (f, "flag: TRUE");
12839 break;
12840 case DW_FORM_indirect:
12841 /* the reader will have reduced the indirect form to
12842 the "base form" so this form should not occur */
12843 fprintf_unfiltered (f, "unexpected attribute form: DW_FORM_indirect");
12844 break;
12845 default:
12846 fprintf_unfiltered (f, "unsupported attribute form: %d.",
12847 die->attrs[i].form);
12848 break;
12849 }
12850 fprintf_unfiltered (f, "\n");
12851 }
12852 }
12853
12854 static void
12855 dump_die_for_error (struct die_info *die)
12856 {
12857 dump_die_shallow (gdb_stderr, 0, die);
12858 }
12859
12860 static void
12861 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
12862 {
12863 int indent = level * 4;
12864
12865 gdb_assert (die != NULL);
12866
12867 if (level >= max_level)
12868 return;
12869
12870 dump_die_shallow (f, indent, die);
12871
12872 if (die->child != NULL)
12873 {
12874 print_spaces (indent, f);
12875 fprintf_unfiltered (f, " Children:");
12876 if (level + 1 < max_level)
12877 {
12878 fprintf_unfiltered (f, "\n");
12879 dump_die_1 (f, level + 1, max_level, die->child);
12880 }
12881 else
12882 {
12883 fprintf_unfiltered (f, " [not printed, max nesting level reached]\n");
12884 }
12885 }
12886
12887 if (die->sibling != NULL && level > 0)
12888 {
12889 dump_die_1 (f, level, max_level, die->sibling);
12890 }
12891 }
12892
12893 /* This is called from the pdie macro in gdbinit.in.
12894 It's not static so gcc will keep a copy callable from gdb. */
12895
12896 void
12897 dump_die (struct die_info *die, int max_level)
12898 {
12899 dump_die_1 (gdb_stdlog, 0, max_level, die);
12900 }
12901
12902 static void
12903 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
12904 {
12905 void **slot;
12906
12907 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
12908
12909 *slot = die;
12910 }
12911
12912 static int
12913 is_ref_attr (struct attribute *attr)
12914 {
12915 switch (attr->form)
12916 {
12917 case DW_FORM_ref_addr:
12918 case DW_FORM_ref1:
12919 case DW_FORM_ref2:
12920 case DW_FORM_ref4:
12921 case DW_FORM_ref8:
12922 case DW_FORM_ref_udata:
12923 return 1;
12924 default:
12925 return 0;
12926 }
12927 }
12928
12929 static unsigned int
12930 dwarf2_get_ref_die_offset (struct attribute *attr)
12931 {
12932 if (is_ref_attr (attr))
12933 return DW_ADDR (attr);
12934
12935 complaint (&symfile_complaints,
12936 _("unsupported die ref attribute form: '%s'"),
12937 dwarf_form_name (attr->form));
12938 return 0;
12939 }
12940
12941 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
12942 * the value held by the attribute is not constant. */
12943
12944 static LONGEST
12945 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
12946 {
12947 if (attr->form == DW_FORM_sdata)
12948 return DW_SND (attr);
12949 else if (attr->form == DW_FORM_udata
12950 || attr->form == DW_FORM_data1
12951 || attr->form == DW_FORM_data2
12952 || attr->form == DW_FORM_data4
12953 || attr->form == DW_FORM_data8)
12954 return DW_UNSND (attr);
12955 else
12956 {
12957 complaint (&symfile_complaints, _("Attribute value is not a constant (%s)"),
12958 dwarf_form_name (attr->form));
12959 return default_value;
12960 }
12961 }
12962
12963 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
12964 unit and add it to our queue.
12965 The result is non-zero if PER_CU was queued, otherwise the result is zero
12966 meaning either PER_CU is already queued or it is already loaded. */
12967
12968 static int
12969 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
12970 struct dwarf2_per_cu_data *per_cu)
12971 {
12972 /* We may arrive here during partial symbol reading, if we need full
12973 DIEs to process an unusual case (e.g. template arguments). Do
12974 not queue PER_CU, just tell our caller to load its DIEs. */
12975 if (dwarf2_per_objfile->reading_partial_symbols)
12976 {
12977 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
12978 return 1;
12979 return 0;
12980 }
12981
12982 /* Mark the dependence relation so that we don't flush PER_CU
12983 too early. */
12984 dwarf2_add_dependence (this_cu, per_cu);
12985
12986 /* If it's already on the queue, we have nothing to do. */
12987 if (per_cu->queued)
12988 return 0;
12989
12990 /* If the compilation unit is already loaded, just mark it as
12991 used. */
12992 if (per_cu->cu != NULL)
12993 {
12994 per_cu->cu->last_used = 0;
12995 return 0;
12996 }
12997
12998 /* Add it to the queue. */
12999 queue_comp_unit (per_cu, this_cu->objfile);
13000
13001 return 1;
13002 }
13003
13004 /* Follow reference or signature attribute ATTR of SRC_DIE.
13005 On entry *REF_CU is the CU of SRC_DIE.
13006 On exit *REF_CU is the CU of the result. */
13007
13008 static struct die_info *
13009 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13010 struct dwarf2_cu **ref_cu)
13011 {
13012 struct die_info *die;
13013
13014 if (is_ref_attr (attr))
13015 die = follow_die_ref (src_die, attr, ref_cu);
13016 else if (attr->form == DW_FORM_sig8)
13017 die = follow_die_sig (src_die, attr, ref_cu);
13018 else
13019 {
13020 dump_die_for_error (src_die);
13021 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13022 (*ref_cu)->objfile->name);
13023 }
13024
13025 return die;
13026 }
13027
13028 /* Follow reference OFFSET.
13029 On entry *REF_CU is the CU of the source die referencing OFFSET.
13030 On exit *REF_CU is the CU of the result.
13031 Returns NULL if OFFSET is invalid. */
13032
13033 static struct die_info *
13034 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13035 {
13036 struct die_info temp_die;
13037 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13038
13039 gdb_assert (cu->per_cu != NULL);
13040
13041 target_cu = cu;
13042
13043 if (cu->per_cu->from_debug_types)
13044 {
13045 /* .debug_types CUs cannot reference anything outside their CU.
13046 If they need to, they have to reference a signatured type via
13047 DW_FORM_sig8. */
13048 if (! offset_in_cu_p (&cu->header, offset))
13049 return NULL;
13050 }
13051 else if (! offset_in_cu_p (&cu->header, offset))
13052 {
13053 struct dwarf2_per_cu_data *per_cu;
13054
13055 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13056
13057 /* If necessary, add it to the queue and load its DIEs. */
13058 if (maybe_queue_comp_unit (cu, per_cu))
13059 load_full_comp_unit (per_cu, cu->objfile);
13060
13061 target_cu = per_cu->cu;
13062 }
13063 else if (cu->dies == NULL)
13064 {
13065 /* We're loading full DIEs during partial symbol reading. */
13066 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13067 load_full_comp_unit (cu->per_cu, cu->objfile);
13068 }
13069
13070 *ref_cu = target_cu;
13071 temp_die.offset = offset;
13072 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13073 }
13074
13075 /* Follow reference attribute ATTR of SRC_DIE.
13076 On entry *REF_CU is the CU of SRC_DIE.
13077 On exit *REF_CU is the CU of the result. */
13078
13079 static struct die_info *
13080 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13081 struct dwarf2_cu **ref_cu)
13082 {
13083 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13084 struct dwarf2_cu *cu = *ref_cu;
13085 struct die_info *die;
13086
13087 die = follow_die_offset (offset, ref_cu);
13088 if (!die)
13089 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13090 "at 0x%x [in module %s]"),
13091 offset, src_die->offset, cu->objfile->name);
13092
13093 return die;
13094 }
13095
13096 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13097 value is intended for DW_OP_call*. */
13098
13099 struct dwarf2_locexpr_baton
13100 dwarf2_fetch_die_location_block (unsigned int offset,
13101 struct dwarf2_per_cu_data *per_cu,
13102 CORE_ADDR (*get_frame_pc) (void *baton),
13103 void *baton)
13104 {
13105 struct dwarf2_cu *cu = per_cu->cu;
13106 struct die_info *die;
13107 struct attribute *attr;
13108 struct dwarf2_locexpr_baton retval;
13109
13110 dw2_setup (per_cu->objfile);
13111
13112 die = follow_die_offset (offset, &cu);
13113 if (!die)
13114 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13115 offset, per_cu->cu->objfile->name);
13116
13117 attr = dwarf2_attr (die, DW_AT_location, cu);
13118 if (!attr)
13119 {
13120 /* DWARF: "If there is no such attribute, then there is no effect.". */
13121
13122 retval.data = NULL;
13123 retval.size = 0;
13124 }
13125 else if (attr_form_is_section_offset (attr))
13126 {
13127 struct dwarf2_loclist_baton loclist_baton;
13128 CORE_ADDR pc = (*get_frame_pc) (baton);
13129 size_t size;
13130
13131 fill_in_loclist_baton (cu, &loclist_baton, attr);
13132
13133 retval.data = dwarf2_find_location_expression (&loclist_baton,
13134 &size, pc);
13135 retval.size = size;
13136 }
13137 else
13138 {
13139 if (!attr_form_is_block (attr))
13140 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13141 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13142 offset, per_cu->cu->objfile->name);
13143
13144 retval.data = DW_BLOCK (attr)->data;
13145 retval.size = DW_BLOCK (attr)->size;
13146 }
13147 retval.per_cu = cu->per_cu;
13148 return retval;
13149 }
13150
13151 /* Follow the signature attribute ATTR in SRC_DIE.
13152 On entry *REF_CU is the CU of SRC_DIE.
13153 On exit *REF_CU is the CU of the result. */
13154
13155 static struct die_info *
13156 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13157 struct dwarf2_cu **ref_cu)
13158 {
13159 struct objfile *objfile = (*ref_cu)->objfile;
13160 struct die_info temp_die;
13161 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13162 struct dwarf2_cu *sig_cu;
13163 struct die_info *die;
13164
13165 /* sig_type will be NULL if the signatured type is missing from
13166 the debug info. */
13167 if (sig_type == NULL)
13168 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13169 "at 0x%x [in module %s]"),
13170 src_die->offset, objfile->name);
13171
13172 /* If necessary, add it to the queue and load its DIEs. */
13173
13174 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13175 read_signatured_type (objfile, sig_type);
13176
13177 gdb_assert (sig_type->per_cu.cu != NULL);
13178
13179 sig_cu = sig_type->per_cu.cu;
13180 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13181 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13182 if (die)
13183 {
13184 *ref_cu = sig_cu;
13185 return die;
13186 }
13187
13188 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced from DIE "
13189 "at 0x%x [in module %s]"),
13190 sig_type->type_offset, src_die->offset, objfile->name);
13191 }
13192
13193 /* Given an offset of a signatured type, return its signatured_type. */
13194
13195 static struct signatured_type *
13196 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13197 {
13198 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13199 unsigned int length, initial_length_size;
13200 unsigned int sig_offset;
13201 struct signatured_type find_entry, *type_sig;
13202
13203 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13204 sig_offset = (initial_length_size
13205 + 2 /*version*/
13206 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13207 + 1 /*address_size*/);
13208 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13209 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13210
13211 /* This is only used to lookup previously recorded types.
13212 If we didn't find it, it's our bug. */
13213 gdb_assert (type_sig != NULL);
13214 gdb_assert (offset == type_sig->offset);
13215
13216 return type_sig;
13217 }
13218
13219 /* Read in signatured type at OFFSET and build its CU and die(s). */
13220
13221 static void
13222 read_signatured_type_at_offset (struct objfile *objfile,
13223 unsigned int offset)
13224 {
13225 struct signatured_type *type_sig;
13226
13227 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13228
13229 /* We have the section offset, but we need the signature to do the
13230 hash table lookup. */
13231 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13232
13233 gdb_assert (type_sig->per_cu.cu == NULL);
13234
13235 read_signatured_type (objfile, type_sig);
13236
13237 gdb_assert (type_sig->per_cu.cu != NULL);
13238 }
13239
13240 /* Read in a signatured type and build its CU and DIEs. */
13241
13242 static void
13243 read_signatured_type (struct objfile *objfile,
13244 struct signatured_type *type_sig)
13245 {
13246 gdb_byte *types_ptr;
13247 struct die_reader_specs reader_specs;
13248 struct dwarf2_cu *cu;
13249 ULONGEST signature;
13250 struct cleanup *back_to, *free_cu_cleanup;
13251
13252 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13253 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->offset;
13254
13255 gdb_assert (type_sig->per_cu.cu == NULL);
13256
13257 cu = xmalloc (sizeof (*cu));
13258 init_one_comp_unit (cu, objfile);
13259
13260 type_sig->per_cu.cu = cu;
13261 cu->per_cu = &type_sig->per_cu;
13262
13263 /* If an error occurs while loading, release our storage. */
13264 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13265
13266 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13267 types_ptr, objfile->obfd);
13268 gdb_assert (signature == type_sig->signature);
13269
13270 cu->die_hash
13271 = htab_create_alloc_ex (cu->header.length / 12,
13272 die_hash,
13273 die_eq,
13274 NULL,
13275 &cu->comp_unit_obstack,
13276 hashtab_obstack_allocate,
13277 dummy_obstack_deallocate);
13278
13279 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13280 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13281
13282 init_cu_die_reader (&reader_specs, cu);
13283
13284 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13285 NULL /*parent*/);
13286
13287 /* We try not to read any attributes in this function, because not
13288 all objfiles needed for references have been loaded yet, and symbol
13289 table processing isn't initialized. But we have to set the CU language,
13290 or we won't be able to build types correctly. */
13291 prepare_one_comp_unit (cu, cu->dies);
13292
13293 do_cleanups (back_to);
13294
13295 /* We've successfully allocated this compilation unit. Let our caller
13296 clean it up when finished with it. */
13297 discard_cleanups (free_cu_cleanup);
13298
13299 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13300 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13301 }
13302
13303 /* Decode simple location descriptions.
13304 Given a pointer to a dwarf block that defines a location, compute
13305 the location and return the value.
13306
13307 NOTE drow/2003-11-18: This function is called in two situations
13308 now: for the address of static or global variables (partial symbols
13309 only) and for offsets into structures which are expected to be
13310 (more or less) constant. The partial symbol case should go away,
13311 and only the constant case should remain. That will let this
13312 function complain more accurately. A few special modes are allowed
13313 without complaint for global variables (for instance, global
13314 register values and thread-local values).
13315
13316 A location description containing no operations indicates that the
13317 object is optimized out. The return value is 0 for that case.
13318 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13319 callers will only want a very basic result and this can become a
13320 complaint.
13321
13322 Note that stack[0] is unused except as a default error return. */
13323
13324 static CORE_ADDR
13325 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13326 {
13327 struct objfile *objfile = cu->objfile;
13328 int i;
13329 int size = blk->size;
13330 gdb_byte *data = blk->data;
13331 CORE_ADDR stack[64];
13332 int stacki;
13333 unsigned int bytes_read, unsnd;
13334 gdb_byte op;
13335
13336 i = 0;
13337 stacki = 0;
13338 stack[stacki] = 0;
13339 stack[++stacki] = 0;
13340
13341 while (i < size)
13342 {
13343 op = data[i++];
13344 switch (op)
13345 {
13346 case DW_OP_lit0:
13347 case DW_OP_lit1:
13348 case DW_OP_lit2:
13349 case DW_OP_lit3:
13350 case DW_OP_lit4:
13351 case DW_OP_lit5:
13352 case DW_OP_lit6:
13353 case DW_OP_lit7:
13354 case DW_OP_lit8:
13355 case DW_OP_lit9:
13356 case DW_OP_lit10:
13357 case DW_OP_lit11:
13358 case DW_OP_lit12:
13359 case DW_OP_lit13:
13360 case DW_OP_lit14:
13361 case DW_OP_lit15:
13362 case DW_OP_lit16:
13363 case DW_OP_lit17:
13364 case DW_OP_lit18:
13365 case DW_OP_lit19:
13366 case DW_OP_lit20:
13367 case DW_OP_lit21:
13368 case DW_OP_lit22:
13369 case DW_OP_lit23:
13370 case DW_OP_lit24:
13371 case DW_OP_lit25:
13372 case DW_OP_lit26:
13373 case DW_OP_lit27:
13374 case DW_OP_lit28:
13375 case DW_OP_lit29:
13376 case DW_OP_lit30:
13377 case DW_OP_lit31:
13378 stack[++stacki] = op - DW_OP_lit0;
13379 break;
13380
13381 case DW_OP_reg0:
13382 case DW_OP_reg1:
13383 case DW_OP_reg2:
13384 case DW_OP_reg3:
13385 case DW_OP_reg4:
13386 case DW_OP_reg5:
13387 case DW_OP_reg6:
13388 case DW_OP_reg7:
13389 case DW_OP_reg8:
13390 case DW_OP_reg9:
13391 case DW_OP_reg10:
13392 case DW_OP_reg11:
13393 case DW_OP_reg12:
13394 case DW_OP_reg13:
13395 case DW_OP_reg14:
13396 case DW_OP_reg15:
13397 case DW_OP_reg16:
13398 case DW_OP_reg17:
13399 case DW_OP_reg18:
13400 case DW_OP_reg19:
13401 case DW_OP_reg20:
13402 case DW_OP_reg21:
13403 case DW_OP_reg22:
13404 case DW_OP_reg23:
13405 case DW_OP_reg24:
13406 case DW_OP_reg25:
13407 case DW_OP_reg26:
13408 case DW_OP_reg27:
13409 case DW_OP_reg28:
13410 case DW_OP_reg29:
13411 case DW_OP_reg30:
13412 case DW_OP_reg31:
13413 stack[++stacki] = op - DW_OP_reg0;
13414 if (i < size)
13415 dwarf2_complex_location_expr_complaint ();
13416 break;
13417
13418 case DW_OP_regx:
13419 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13420 i += bytes_read;
13421 stack[++stacki] = unsnd;
13422 if (i < size)
13423 dwarf2_complex_location_expr_complaint ();
13424 break;
13425
13426 case DW_OP_addr:
13427 stack[++stacki] = read_address (objfile->obfd, &data[i],
13428 cu, &bytes_read);
13429 i += bytes_read;
13430 break;
13431
13432 case DW_OP_const1u:
13433 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
13434 i += 1;
13435 break;
13436
13437 case DW_OP_const1s:
13438 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
13439 i += 1;
13440 break;
13441
13442 case DW_OP_const2u:
13443 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
13444 i += 2;
13445 break;
13446
13447 case DW_OP_const2s:
13448 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
13449 i += 2;
13450 break;
13451
13452 case DW_OP_const4u:
13453 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
13454 i += 4;
13455 break;
13456
13457 case DW_OP_const4s:
13458 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
13459 i += 4;
13460 break;
13461
13462 case DW_OP_constu:
13463 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
13464 &bytes_read);
13465 i += bytes_read;
13466 break;
13467
13468 case DW_OP_consts:
13469 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
13470 i += bytes_read;
13471 break;
13472
13473 case DW_OP_dup:
13474 stack[stacki + 1] = stack[stacki];
13475 stacki++;
13476 break;
13477
13478 case DW_OP_plus:
13479 stack[stacki - 1] += stack[stacki];
13480 stacki--;
13481 break;
13482
13483 case DW_OP_plus_uconst:
13484 stack[stacki] += read_unsigned_leb128 (NULL, (data + i), &bytes_read);
13485 i += bytes_read;
13486 break;
13487
13488 case DW_OP_minus:
13489 stack[stacki - 1] -= stack[stacki];
13490 stacki--;
13491 break;
13492
13493 case DW_OP_deref:
13494 /* If we're not the last op, then we definitely can't encode
13495 this using GDB's address_class enum. This is valid for partial
13496 global symbols, although the variable's address will be bogus
13497 in the psymtab. */
13498 if (i < size)
13499 dwarf2_complex_location_expr_complaint ();
13500 break;
13501
13502 case DW_OP_GNU_push_tls_address:
13503 /* The top of the stack has the offset from the beginning
13504 of the thread control block at which the variable is located. */
13505 /* Nothing should follow this operator, so the top of stack would
13506 be returned. */
13507 /* This is valid for partial global symbols, but the variable's
13508 address will be bogus in the psymtab. */
13509 if (i < size)
13510 dwarf2_complex_location_expr_complaint ();
13511 break;
13512
13513 case DW_OP_GNU_uninit:
13514 break;
13515
13516 default:
13517 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
13518 dwarf_stack_op_name (op, 1));
13519 return (stack[stacki]);
13520 }
13521
13522 /* Enforce maximum stack depth of SIZE-1 to avoid writing
13523 outside of the allocated space. Also enforce minimum>0. */
13524 if (stacki >= ARRAY_SIZE (stack) - 1)
13525 {
13526 complaint (&symfile_complaints,
13527 _("location description stack overflow"));
13528 return 0;
13529 }
13530
13531 if (stacki <= 0)
13532 {
13533 complaint (&symfile_complaints,
13534 _("location description stack underflow"));
13535 return 0;
13536 }
13537 }
13538 return (stack[stacki]);
13539 }
13540
13541 /* memory allocation interface */
13542
13543 static struct dwarf_block *
13544 dwarf_alloc_block (struct dwarf2_cu *cu)
13545 {
13546 struct dwarf_block *blk;
13547
13548 blk = (struct dwarf_block *)
13549 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
13550 return (blk);
13551 }
13552
13553 static struct abbrev_info *
13554 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
13555 {
13556 struct abbrev_info *abbrev;
13557
13558 abbrev = (struct abbrev_info *)
13559 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
13560 memset (abbrev, 0, sizeof (struct abbrev_info));
13561 return (abbrev);
13562 }
13563
13564 static struct die_info *
13565 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
13566 {
13567 struct die_info *die;
13568 size_t size = sizeof (struct die_info);
13569
13570 if (num_attrs > 1)
13571 size += (num_attrs - 1) * sizeof (struct attribute);
13572
13573 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
13574 memset (die, 0, sizeof (struct die_info));
13575 return (die);
13576 }
13577
13578 \f
13579 /* Macro support. */
13580
13581 /* Return the full name of file number I in *LH's file name table.
13582 Use COMP_DIR as the name of the current directory of the
13583 compilation. The result is allocated using xmalloc; the caller is
13584 responsible for freeing it. */
13585 static char *
13586 file_full_name (int file, struct line_header *lh, const char *comp_dir)
13587 {
13588 /* Is the file number a valid index into the line header's file name
13589 table? Remember that file numbers start with one, not zero. */
13590 if (1 <= file && file <= lh->num_file_names)
13591 {
13592 struct file_entry *fe = &lh->file_names[file - 1];
13593
13594 if (IS_ABSOLUTE_PATH (fe->name))
13595 return xstrdup (fe->name);
13596 else
13597 {
13598 const char *dir;
13599 int dir_len;
13600 char *full_name;
13601
13602 if (fe->dir_index)
13603 dir = lh->include_dirs[fe->dir_index - 1];
13604 else
13605 dir = comp_dir;
13606
13607 if (dir)
13608 {
13609 dir_len = strlen (dir);
13610 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
13611 strcpy (full_name, dir);
13612 full_name[dir_len] = '/';
13613 strcpy (full_name + dir_len + 1, fe->name);
13614 return full_name;
13615 }
13616 else
13617 return xstrdup (fe->name);
13618 }
13619 }
13620 else
13621 {
13622 /* The compiler produced a bogus file number. We can at least
13623 record the macro definitions made in the file, even if we
13624 won't be able to find the file by name. */
13625 char fake_name[80];
13626
13627 sprintf (fake_name, "<bad macro file number %d>", file);
13628
13629 complaint (&symfile_complaints,
13630 _("bad file number in macro information (%d)"),
13631 file);
13632
13633 return xstrdup (fake_name);
13634 }
13635 }
13636
13637
13638 static struct macro_source_file *
13639 macro_start_file (int file, int line,
13640 struct macro_source_file *current_file,
13641 const char *comp_dir,
13642 struct line_header *lh, struct objfile *objfile)
13643 {
13644 /* The full name of this source file. */
13645 char *full_name = file_full_name (file, lh, comp_dir);
13646
13647 /* We don't create a macro table for this compilation unit
13648 at all until we actually get a filename. */
13649 if (! pending_macros)
13650 pending_macros = new_macro_table (&objfile->objfile_obstack,
13651 objfile->macro_cache);
13652
13653 if (! current_file)
13654 /* If we have no current file, then this must be the start_file
13655 directive for the compilation unit's main source file. */
13656 current_file = macro_set_main (pending_macros, full_name);
13657 else
13658 current_file = macro_include (current_file, line, full_name);
13659
13660 xfree (full_name);
13661
13662 return current_file;
13663 }
13664
13665
13666 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
13667 followed by a null byte. */
13668 static char *
13669 copy_string (const char *buf, int len)
13670 {
13671 char *s = xmalloc (len + 1);
13672
13673 memcpy (s, buf, len);
13674 s[len] = '\0';
13675 return s;
13676 }
13677
13678
13679 static const char *
13680 consume_improper_spaces (const char *p, const char *body)
13681 {
13682 if (*p == ' ')
13683 {
13684 complaint (&symfile_complaints,
13685 _("macro definition contains spaces in formal argument list:\n`%s'"),
13686 body);
13687
13688 while (*p == ' ')
13689 p++;
13690 }
13691
13692 return p;
13693 }
13694
13695
13696 static void
13697 parse_macro_definition (struct macro_source_file *file, int line,
13698 const char *body)
13699 {
13700 const char *p;
13701
13702 /* The body string takes one of two forms. For object-like macro
13703 definitions, it should be:
13704
13705 <macro name> " " <definition>
13706
13707 For function-like macro definitions, it should be:
13708
13709 <macro name> "() " <definition>
13710 or
13711 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
13712
13713 Spaces may appear only where explicitly indicated, and in the
13714 <definition>.
13715
13716 The Dwarf 2 spec says that an object-like macro's name is always
13717 followed by a space, but versions of GCC around March 2002 omit
13718 the space when the macro's definition is the empty string.
13719
13720 The Dwarf 2 spec says that there should be no spaces between the
13721 formal arguments in a function-like macro's formal argument list,
13722 but versions of GCC around March 2002 include spaces after the
13723 commas. */
13724
13725
13726 /* Find the extent of the macro name. The macro name is terminated
13727 by either a space or null character (for an object-like macro) or
13728 an opening paren (for a function-like macro). */
13729 for (p = body; *p; p++)
13730 if (*p == ' ' || *p == '(')
13731 break;
13732
13733 if (*p == ' ' || *p == '\0')
13734 {
13735 /* It's an object-like macro. */
13736 int name_len = p - body;
13737 char *name = copy_string (body, name_len);
13738 const char *replacement;
13739
13740 if (*p == ' ')
13741 replacement = body + name_len + 1;
13742 else
13743 {
13744 dwarf2_macro_malformed_definition_complaint (body);
13745 replacement = body + name_len;
13746 }
13747
13748 macro_define_object (file, line, name, replacement);
13749
13750 xfree (name);
13751 }
13752 else if (*p == '(')
13753 {
13754 /* It's a function-like macro. */
13755 char *name = copy_string (body, p - body);
13756 int argc = 0;
13757 int argv_size = 1;
13758 char **argv = xmalloc (argv_size * sizeof (*argv));
13759
13760 p++;
13761
13762 p = consume_improper_spaces (p, body);
13763
13764 /* Parse the formal argument list. */
13765 while (*p && *p != ')')
13766 {
13767 /* Find the extent of the current argument name. */
13768 const char *arg_start = p;
13769
13770 while (*p && *p != ',' && *p != ')' && *p != ' ')
13771 p++;
13772
13773 if (! *p || p == arg_start)
13774 dwarf2_macro_malformed_definition_complaint (body);
13775 else
13776 {
13777 /* Make sure argv has room for the new argument. */
13778 if (argc >= argv_size)
13779 {
13780 argv_size *= 2;
13781 argv = xrealloc (argv, argv_size * sizeof (*argv));
13782 }
13783
13784 argv[argc++] = copy_string (arg_start, p - arg_start);
13785 }
13786
13787 p = consume_improper_spaces (p, body);
13788
13789 /* Consume the comma, if present. */
13790 if (*p == ',')
13791 {
13792 p++;
13793
13794 p = consume_improper_spaces (p, body);
13795 }
13796 }
13797
13798 if (*p == ')')
13799 {
13800 p++;
13801
13802 if (*p == ' ')
13803 /* Perfectly formed definition, no complaints. */
13804 macro_define_function (file, line, name,
13805 argc, (const char **) argv,
13806 p + 1);
13807 else if (*p == '\0')
13808 {
13809 /* Complain, but do define it. */
13810 dwarf2_macro_malformed_definition_complaint (body);
13811 macro_define_function (file, line, name,
13812 argc, (const char **) argv,
13813 p);
13814 }
13815 else
13816 /* Just complain. */
13817 dwarf2_macro_malformed_definition_complaint (body);
13818 }
13819 else
13820 /* Just complain. */
13821 dwarf2_macro_malformed_definition_complaint (body);
13822
13823 xfree (name);
13824 {
13825 int i;
13826
13827 for (i = 0; i < argc; i++)
13828 xfree (argv[i]);
13829 }
13830 xfree (argv);
13831 }
13832 else
13833 dwarf2_macro_malformed_definition_complaint (body);
13834 }
13835
13836
13837 static void
13838 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
13839 char *comp_dir, bfd *abfd,
13840 struct dwarf2_cu *cu)
13841 {
13842 gdb_byte *mac_ptr, *mac_end;
13843 struct macro_source_file *current_file = 0;
13844 enum dwarf_macinfo_record_type macinfo_type;
13845 int at_commandline;
13846
13847 dwarf2_read_section (dwarf2_per_objfile->objfile,
13848 &dwarf2_per_objfile->macinfo);
13849 if (dwarf2_per_objfile->macinfo.buffer == NULL)
13850 {
13851 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
13852 return;
13853 }
13854
13855 /* First pass: Find the name of the base filename.
13856 This filename is needed in order to process all macros whose definition
13857 (or undefinition) comes from the command line. These macros are defined
13858 before the first DW_MACINFO_start_file entry, and yet still need to be
13859 associated to the base file.
13860
13861 To determine the base file name, we scan the macro definitions until we
13862 reach the first DW_MACINFO_start_file entry. We then initialize
13863 CURRENT_FILE accordingly so that any macro definition found before the
13864 first DW_MACINFO_start_file can still be associated to the base file. */
13865
13866 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13867 mac_end = dwarf2_per_objfile->macinfo.buffer
13868 + dwarf2_per_objfile->macinfo.size;
13869
13870 do
13871 {
13872 /* Do we at least have room for a macinfo type byte? */
13873 if (mac_ptr >= mac_end)
13874 {
13875 /* Complaint is printed during the second pass as GDB will probably
13876 stop the first pass earlier upon finding DW_MACINFO_start_file. */
13877 break;
13878 }
13879
13880 macinfo_type = read_1_byte (abfd, mac_ptr);
13881 mac_ptr++;
13882
13883 switch (macinfo_type)
13884 {
13885 /* A zero macinfo type indicates the end of the macro
13886 information. */
13887 case 0:
13888 break;
13889
13890 case DW_MACINFO_define:
13891 case DW_MACINFO_undef:
13892 /* Only skip the data by MAC_PTR. */
13893 {
13894 unsigned int bytes_read;
13895
13896 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13897 mac_ptr += bytes_read;
13898 read_direct_string (abfd, mac_ptr, &bytes_read);
13899 mac_ptr += bytes_read;
13900 }
13901 break;
13902
13903 case DW_MACINFO_start_file:
13904 {
13905 unsigned int bytes_read;
13906 int line, file;
13907
13908 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13909 mac_ptr += bytes_read;
13910 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13911 mac_ptr += bytes_read;
13912
13913 current_file = macro_start_file (file, line, current_file, comp_dir,
13914 lh, cu->objfile);
13915 }
13916 break;
13917
13918 case DW_MACINFO_end_file:
13919 /* No data to skip by MAC_PTR. */
13920 break;
13921
13922 case DW_MACINFO_vendor_ext:
13923 /* Only skip the data by MAC_PTR. */
13924 {
13925 unsigned int bytes_read;
13926
13927 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13928 mac_ptr += bytes_read;
13929 read_direct_string (abfd, mac_ptr, &bytes_read);
13930 mac_ptr += bytes_read;
13931 }
13932 break;
13933
13934 default:
13935 break;
13936 }
13937 } while (macinfo_type != 0 && current_file == NULL);
13938
13939 /* Second pass: Process all entries.
13940
13941 Use the AT_COMMAND_LINE flag to determine whether we are still processing
13942 command-line macro definitions/undefinitions. This flag is unset when we
13943 reach the first DW_MACINFO_start_file entry. */
13944
13945 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
13946
13947 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
13948 GDB is still reading the definitions from command line. First
13949 DW_MACINFO_start_file will need to be ignored as it was already executed
13950 to create CURRENT_FILE for the main source holding also the command line
13951 definitions. On first met DW_MACINFO_start_file this flag is reset to
13952 normally execute all the remaining DW_MACINFO_start_file macinfos. */
13953
13954 at_commandline = 1;
13955
13956 do
13957 {
13958 /* Do we at least have room for a macinfo type byte? */
13959 if (mac_ptr >= mac_end)
13960 {
13961 dwarf2_macros_too_long_complaint ();
13962 break;
13963 }
13964
13965 macinfo_type = read_1_byte (abfd, mac_ptr);
13966 mac_ptr++;
13967
13968 switch (macinfo_type)
13969 {
13970 /* A zero macinfo type indicates the end of the macro
13971 information. */
13972 case 0:
13973 break;
13974
13975 case DW_MACINFO_define:
13976 case DW_MACINFO_undef:
13977 {
13978 unsigned int bytes_read;
13979 int line;
13980 char *body;
13981
13982 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
13983 mac_ptr += bytes_read;
13984 body = read_direct_string (abfd, mac_ptr, &bytes_read);
13985 mac_ptr += bytes_read;
13986
13987 if (! current_file)
13988 {
13989 /* DWARF violation as no main source is present. */
13990 complaint (&symfile_complaints,
13991 _("debug info with no main source gives macro %s "
13992 "on line %d: %s"),
13993 macinfo_type == DW_MACINFO_define ?
13994 _("definition") :
13995 macinfo_type == DW_MACINFO_undef ?
13996 _("undefinition") :
13997 _("something-or-other"), line, body);
13998 break;
13999 }
14000 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
14001 complaint (&symfile_complaints,
14002 _("debug info gives %s macro %s with %s line %d: %s"),
14003 at_commandline ? _("command-line") : _("in-file"),
14004 macinfo_type == DW_MACINFO_define ?
14005 _("definition") :
14006 macinfo_type == DW_MACINFO_undef ?
14007 _("undefinition") :
14008 _("something-or-other"),
14009 line == 0 ? _("zero") : _("non-zero"), line, body);
14010
14011 if (macinfo_type == DW_MACINFO_define)
14012 parse_macro_definition (current_file, line, body);
14013 else if (macinfo_type == DW_MACINFO_undef)
14014 macro_undef (current_file, line, body);
14015 }
14016 break;
14017
14018 case DW_MACINFO_start_file:
14019 {
14020 unsigned int bytes_read;
14021 int line, file;
14022
14023 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14024 mac_ptr += bytes_read;
14025 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14026 mac_ptr += bytes_read;
14027
14028 if ((line == 0 && !at_commandline) || (line != 0 && at_commandline))
14029 complaint (&symfile_complaints,
14030 _("debug info gives source %d included "
14031 "from %s at %s line %d"),
14032 file, at_commandline ? _("command-line") : _("file"),
14033 line == 0 ? _("zero") : _("non-zero"), line);
14034
14035 if (at_commandline)
14036 {
14037 /* This DW_MACINFO_start_file was executed in the pass one. */
14038 at_commandline = 0;
14039 }
14040 else
14041 current_file = macro_start_file (file, line,
14042 current_file, comp_dir,
14043 lh, cu->objfile);
14044 }
14045 break;
14046
14047 case DW_MACINFO_end_file:
14048 if (! current_file)
14049 complaint (&symfile_complaints,
14050 _("macro debug info has an unmatched `close_file' directive"));
14051 else
14052 {
14053 current_file = current_file->included_by;
14054 if (! current_file)
14055 {
14056 enum dwarf_macinfo_record_type next_type;
14057
14058 /* GCC circa March 2002 doesn't produce the zero
14059 type byte marking the end of the compilation
14060 unit. Complain if it's not there, but exit no
14061 matter what. */
14062
14063 /* Do we at least have room for a macinfo type byte? */
14064 if (mac_ptr >= mac_end)
14065 {
14066 dwarf2_macros_too_long_complaint ();
14067 return;
14068 }
14069
14070 /* We don't increment mac_ptr here, so this is just
14071 a look-ahead. */
14072 next_type = read_1_byte (abfd, mac_ptr);
14073 if (next_type != 0)
14074 complaint (&symfile_complaints,
14075 _("no terminating 0-type entry for macros in `.debug_macinfo' section"));
14076
14077 return;
14078 }
14079 }
14080 break;
14081
14082 case DW_MACINFO_vendor_ext:
14083 {
14084 unsigned int bytes_read;
14085 int constant;
14086 char *string;
14087
14088 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14089 mac_ptr += bytes_read;
14090 string = read_direct_string (abfd, mac_ptr, &bytes_read);
14091 mac_ptr += bytes_read;
14092
14093 /* We don't recognize any vendor extensions. */
14094 }
14095 break;
14096 }
14097 } while (macinfo_type != 0);
14098 }
14099
14100 /* Check if the attribute's form is a DW_FORM_block*
14101 if so return true else false. */
14102 static int
14103 attr_form_is_block (struct attribute *attr)
14104 {
14105 return (attr == NULL ? 0 :
14106 attr->form == DW_FORM_block1
14107 || attr->form == DW_FORM_block2
14108 || attr->form == DW_FORM_block4
14109 || attr->form == DW_FORM_block
14110 || attr->form == DW_FORM_exprloc);
14111 }
14112
14113 /* Return non-zero if ATTR's value is a section offset --- classes
14114 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14115 You may use DW_UNSND (attr) to retrieve such offsets.
14116
14117 Section 7.5.4, "Attribute Encodings", explains that no attribute
14118 may have a value that belongs to more than one of these classes; it
14119 would be ambiguous if we did, because we use the same forms for all
14120 of them. */
14121 static int
14122 attr_form_is_section_offset (struct attribute *attr)
14123 {
14124 return (attr->form == DW_FORM_data4
14125 || attr->form == DW_FORM_data8
14126 || attr->form == DW_FORM_sec_offset);
14127 }
14128
14129
14130 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14131 zero otherwise. When this function returns true, you can apply
14132 dwarf2_get_attr_constant_value to it.
14133
14134 However, note that for some attributes you must check
14135 attr_form_is_section_offset before using this test. DW_FORM_data4
14136 and DW_FORM_data8 are members of both the constant class, and of
14137 the classes that contain offsets into other debug sections
14138 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14139 that, if an attribute's can be either a constant or one of the
14140 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14141 taken as section offsets, not constants. */
14142 static int
14143 attr_form_is_constant (struct attribute *attr)
14144 {
14145 switch (attr->form)
14146 {
14147 case DW_FORM_sdata:
14148 case DW_FORM_udata:
14149 case DW_FORM_data1:
14150 case DW_FORM_data2:
14151 case DW_FORM_data4:
14152 case DW_FORM_data8:
14153 return 1;
14154 default:
14155 return 0;
14156 }
14157 }
14158
14159 /* A helper function that fills in a dwarf2_loclist_baton. */
14160
14161 static void
14162 fill_in_loclist_baton (struct dwarf2_cu *cu,
14163 struct dwarf2_loclist_baton *baton,
14164 struct attribute *attr)
14165 {
14166 dwarf2_read_section (dwarf2_per_objfile->objfile,
14167 &dwarf2_per_objfile->loc);
14168
14169 baton->per_cu = cu->per_cu;
14170 gdb_assert (baton->per_cu);
14171 /* We don't know how long the location list is, but make sure we
14172 don't run off the edge of the section. */
14173 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14174 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14175 baton->base_address = cu->base_address;
14176 }
14177
14178 static void
14179 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14180 struct dwarf2_cu *cu)
14181 {
14182 if (attr_form_is_section_offset (attr)
14183 /* ".debug_loc" may not exist at all, or the offset may be outside
14184 the section. If so, fall through to the complaint in the
14185 other branch. */
14186 && DW_UNSND (attr) < dwarf2_per_objfile->loc.size)
14187 {
14188 struct dwarf2_loclist_baton *baton;
14189
14190 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14191 sizeof (struct dwarf2_loclist_baton));
14192
14193 fill_in_loclist_baton (cu, baton, attr);
14194
14195 if (cu->base_known == 0)
14196 complaint (&symfile_complaints,
14197 _("Location list used without specifying the CU base address."));
14198
14199 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14200 SYMBOL_LOCATION_BATON (sym) = baton;
14201 }
14202 else
14203 {
14204 struct dwarf2_locexpr_baton *baton;
14205
14206 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14207 sizeof (struct dwarf2_locexpr_baton));
14208 baton->per_cu = cu->per_cu;
14209 gdb_assert (baton->per_cu);
14210
14211 if (attr_form_is_block (attr))
14212 {
14213 /* Note that we're just copying the block's data pointer
14214 here, not the actual data. We're still pointing into the
14215 info_buffer for SYM's objfile; right now we never release
14216 that buffer, but when we do clean up properly this may
14217 need to change. */
14218 baton->size = DW_BLOCK (attr)->size;
14219 baton->data = DW_BLOCK (attr)->data;
14220 }
14221 else
14222 {
14223 dwarf2_invalid_attrib_class_complaint ("location description",
14224 SYMBOL_NATURAL_NAME (sym));
14225 baton->size = 0;
14226 baton->data = NULL;
14227 }
14228
14229 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14230 SYMBOL_LOCATION_BATON (sym) = baton;
14231 }
14232 }
14233
14234 /* Return the OBJFILE associated with the compilation unit CU. If CU
14235 came from a separate debuginfo file, then the master objfile is
14236 returned. */
14237
14238 struct objfile *
14239 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14240 {
14241 struct objfile *objfile = per_cu->objfile;
14242
14243 /* Return the master objfile, so that we can report and look up the
14244 correct file containing this variable. */
14245 if (objfile->separate_debug_objfile_backlink)
14246 objfile = objfile->separate_debug_objfile_backlink;
14247
14248 return objfile;
14249 }
14250
14251 /* Return the address size given in the compilation unit header for CU. */
14252
14253 CORE_ADDR
14254 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14255 {
14256 if (per_cu->cu)
14257 return per_cu->cu->header.addr_size;
14258 else
14259 {
14260 /* If the CU is not currently read in, we re-read its header. */
14261 struct objfile *objfile = per_cu->objfile;
14262 struct dwarf2_per_objfile *per_objfile
14263 = objfile_data (objfile, dwarf2_objfile_data_key);
14264 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14265 struct comp_unit_head cu_header;
14266
14267 memset (&cu_header, 0, sizeof cu_header);
14268 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14269 return cu_header.addr_size;
14270 }
14271 }
14272
14273 /* Return the offset size given in the compilation unit header for CU. */
14274
14275 int
14276 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14277 {
14278 if (per_cu->cu)
14279 return per_cu->cu->header.offset_size;
14280 else
14281 {
14282 /* If the CU is not currently read in, we re-read its header. */
14283 struct objfile *objfile = per_cu->objfile;
14284 struct dwarf2_per_objfile *per_objfile
14285 = objfile_data (objfile, dwarf2_objfile_data_key);
14286 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14287 struct comp_unit_head cu_header;
14288
14289 memset (&cu_header, 0, sizeof cu_header);
14290 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14291 return cu_header.offset_size;
14292 }
14293 }
14294
14295 /* Return the text offset of the CU. The returned offset comes from
14296 this CU's objfile. If this objfile came from a separate debuginfo
14297 file, then the offset may be different from the corresponding
14298 offset in the parent objfile. */
14299
14300 CORE_ADDR
14301 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14302 {
14303 struct objfile *objfile = per_cu->objfile;
14304
14305 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14306 }
14307
14308 /* Locate the .debug_info compilation unit from CU's objfile which contains
14309 the DIE at OFFSET. Raises an error on failure. */
14310
14311 static struct dwarf2_per_cu_data *
14312 dwarf2_find_containing_comp_unit (unsigned int offset,
14313 struct objfile *objfile)
14314 {
14315 struct dwarf2_per_cu_data *this_cu;
14316 int low, high;
14317
14318 low = 0;
14319 high = dwarf2_per_objfile->n_comp_units - 1;
14320 while (high > low)
14321 {
14322 int mid = low + (high - low) / 2;
14323
14324 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14325 high = mid;
14326 else
14327 low = mid + 1;
14328 }
14329 gdb_assert (low == high);
14330 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14331 {
14332 if (low == 0)
14333 error (_("Dwarf Error: could not find partial DIE containing "
14334 "offset 0x%lx [in module %s]"),
14335 (long) offset, bfd_get_filename (objfile->obfd));
14336
14337 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14338 return dwarf2_per_objfile->all_comp_units[low-1];
14339 }
14340 else
14341 {
14342 this_cu = dwarf2_per_objfile->all_comp_units[low];
14343 if (low == dwarf2_per_objfile->n_comp_units - 1
14344 && offset >= this_cu->offset + this_cu->length)
14345 error (_("invalid dwarf2 offset %u"), offset);
14346 gdb_assert (offset < this_cu->offset + this_cu->length);
14347 return this_cu;
14348 }
14349 }
14350
14351 /* Locate the compilation unit from OBJFILE which is located at exactly
14352 OFFSET. Raises an error on failure. */
14353
14354 static struct dwarf2_per_cu_data *
14355 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14356 {
14357 struct dwarf2_per_cu_data *this_cu;
14358
14359 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
14360 if (this_cu->offset != offset)
14361 error (_("no compilation unit with offset %u."), offset);
14362 return this_cu;
14363 }
14364
14365 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
14366
14367 static void
14368 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
14369 {
14370 memset (cu, 0, sizeof (*cu));
14371 cu->objfile = objfile;
14372 obstack_init (&cu->comp_unit_obstack);
14373 }
14374
14375 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
14376
14377 static void
14378 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
14379 {
14380 struct attribute *attr;
14381
14382 /* Set the language we're debugging. */
14383 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
14384 if (attr)
14385 set_cu_language (DW_UNSND (attr), cu);
14386 else
14387 set_cu_language (language_minimal, cu);
14388 }
14389
14390 /* Release one cached compilation unit, CU. We unlink it from the tree
14391 of compilation units, but we don't remove it from the read_in_chain;
14392 the caller is responsible for that.
14393 NOTE: DATA is a void * because this function is also used as a
14394 cleanup routine. */
14395
14396 static void
14397 free_one_comp_unit (void *data)
14398 {
14399 struct dwarf2_cu *cu = data;
14400
14401 if (cu->per_cu != NULL)
14402 cu->per_cu->cu = NULL;
14403 cu->per_cu = NULL;
14404
14405 obstack_free (&cu->comp_unit_obstack, NULL);
14406
14407 xfree (cu);
14408 }
14409
14410 /* This cleanup function is passed the address of a dwarf2_cu on the stack
14411 when we're finished with it. We can't free the pointer itself, but be
14412 sure to unlink it from the cache. Also release any associated storage
14413 and perform cache maintenance.
14414
14415 Only used during partial symbol parsing. */
14416
14417 static void
14418 free_stack_comp_unit (void *data)
14419 {
14420 struct dwarf2_cu *cu = data;
14421
14422 obstack_free (&cu->comp_unit_obstack, NULL);
14423 cu->partial_dies = NULL;
14424
14425 if (cu->per_cu != NULL)
14426 {
14427 /* This compilation unit is on the stack in our caller, so we
14428 should not xfree it. Just unlink it. */
14429 cu->per_cu->cu = NULL;
14430 cu->per_cu = NULL;
14431
14432 /* If we had a per-cu pointer, then we may have other compilation
14433 units loaded, so age them now. */
14434 age_cached_comp_units ();
14435 }
14436 }
14437
14438 /* Free all cached compilation units. */
14439
14440 static void
14441 free_cached_comp_units (void *data)
14442 {
14443 struct dwarf2_per_cu_data *per_cu, **last_chain;
14444
14445 per_cu = dwarf2_per_objfile->read_in_chain;
14446 last_chain = &dwarf2_per_objfile->read_in_chain;
14447 while (per_cu != NULL)
14448 {
14449 struct dwarf2_per_cu_data *next_cu;
14450
14451 next_cu = per_cu->cu->read_in_chain;
14452
14453 free_one_comp_unit (per_cu->cu);
14454 *last_chain = next_cu;
14455
14456 per_cu = next_cu;
14457 }
14458 }
14459
14460 /* Increase the age counter on each cached compilation unit, and free
14461 any that are too old. */
14462
14463 static void
14464 age_cached_comp_units (void)
14465 {
14466 struct dwarf2_per_cu_data *per_cu, **last_chain;
14467
14468 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
14469 per_cu = dwarf2_per_objfile->read_in_chain;
14470 while (per_cu != NULL)
14471 {
14472 per_cu->cu->last_used ++;
14473 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
14474 dwarf2_mark (per_cu->cu);
14475 per_cu = per_cu->cu->read_in_chain;
14476 }
14477
14478 per_cu = dwarf2_per_objfile->read_in_chain;
14479 last_chain = &dwarf2_per_objfile->read_in_chain;
14480 while (per_cu != NULL)
14481 {
14482 struct dwarf2_per_cu_data *next_cu;
14483
14484 next_cu = per_cu->cu->read_in_chain;
14485
14486 if (!per_cu->cu->mark)
14487 {
14488 free_one_comp_unit (per_cu->cu);
14489 *last_chain = next_cu;
14490 }
14491 else
14492 last_chain = &per_cu->cu->read_in_chain;
14493
14494 per_cu = next_cu;
14495 }
14496 }
14497
14498 /* Remove a single compilation unit from the cache. */
14499
14500 static void
14501 free_one_cached_comp_unit (void *target_cu)
14502 {
14503 struct dwarf2_per_cu_data *per_cu, **last_chain;
14504
14505 per_cu = dwarf2_per_objfile->read_in_chain;
14506 last_chain = &dwarf2_per_objfile->read_in_chain;
14507 while (per_cu != NULL)
14508 {
14509 struct dwarf2_per_cu_data *next_cu;
14510
14511 next_cu = per_cu->cu->read_in_chain;
14512
14513 if (per_cu->cu == target_cu)
14514 {
14515 free_one_comp_unit (per_cu->cu);
14516 *last_chain = next_cu;
14517 break;
14518 }
14519 else
14520 last_chain = &per_cu->cu->read_in_chain;
14521
14522 per_cu = next_cu;
14523 }
14524 }
14525
14526 /* Release all extra memory associated with OBJFILE. */
14527
14528 void
14529 dwarf2_free_objfile (struct objfile *objfile)
14530 {
14531 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
14532
14533 if (dwarf2_per_objfile == NULL)
14534 return;
14535
14536 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
14537 free_cached_comp_units (NULL);
14538
14539 if (dwarf2_per_objfile->using_index)
14540 {
14541 int i;
14542
14543 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
14544 {
14545 int j;
14546 struct dwarf2_per_cu_data *per_cu =
14547 dwarf2_per_objfile->all_comp_units[i];
14548
14549 if (!per_cu->v.quick->lines)
14550 continue;
14551
14552 for (j = 0; j < per_cu->v.quick->lines->num_file_names; ++j)
14553 {
14554 if (per_cu->v.quick->file_names)
14555 xfree ((void *) per_cu->v.quick->file_names[j]);
14556 if (per_cu->v.quick->full_names)
14557 xfree ((void *) per_cu->v.quick->full_names[j]);
14558 }
14559
14560 free_line_header (per_cu->v.quick->lines);
14561 }
14562 }
14563
14564 /* Everything else should be on the objfile obstack. */
14565 }
14566
14567 /* A pair of DIE offset and GDB type pointer. We store these
14568 in a hash table separate from the DIEs, and preserve them
14569 when the DIEs are flushed out of cache. */
14570
14571 struct dwarf2_offset_and_type
14572 {
14573 unsigned int offset;
14574 struct type *type;
14575 };
14576
14577 /* Hash function for a dwarf2_offset_and_type. */
14578
14579 static hashval_t
14580 offset_and_type_hash (const void *item)
14581 {
14582 const struct dwarf2_offset_and_type *ofs = item;
14583
14584 return ofs->offset;
14585 }
14586
14587 /* Equality function for a dwarf2_offset_and_type. */
14588
14589 static int
14590 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
14591 {
14592 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
14593 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
14594
14595 return ofs_lhs->offset == ofs_rhs->offset;
14596 }
14597
14598 /* Set the type associated with DIE to TYPE. Save it in CU's hash
14599 table if necessary. For convenience, return TYPE.
14600
14601 The DIEs reading must have careful ordering to:
14602 * Not cause infite loops trying to read in DIEs as a prerequisite for
14603 reading current DIE.
14604 * Not trying to dereference contents of still incompletely read in types
14605 while reading in other DIEs.
14606 * Enable referencing still incompletely read in types just by a pointer to
14607 the type without accessing its fields.
14608
14609 Therefore caller should follow these rules:
14610 * Try to fetch any prerequisite types we may need to build this DIE type
14611 before building the type and calling set_die_type.
14612 * After building type call set_die_type for current DIE as soon as
14613 possible before fetching more types to complete the current type.
14614 * Make the type as complete as possible before fetching more types. */
14615
14616 static struct type *
14617 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
14618 {
14619 struct dwarf2_offset_and_type **slot, ofs;
14620 struct objfile *objfile = cu->objfile;
14621 htab_t *type_hash_ptr;
14622
14623 /* For Ada types, make sure that the gnat-specific data is always
14624 initialized (if not already set). There are a few types where
14625 we should not be doing so, because the type-specific area is
14626 already used to hold some other piece of info (eg: TYPE_CODE_FLT
14627 where the type-specific area is used to store the floatformat).
14628 But this is not a problem, because the gnat-specific information
14629 is actually not needed for these types. */
14630 if (need_gnat_info (cu)
14631 && TYPE_CODE (type) != TYPE_CODE_FUNC
14632 && TYPE_CODE (type) != TYPE_CODE_FLT
14633 && !HAVE_GNAT_AUX_INFO (type))
14634 INIT_GNAT_SPECIFIC (type);
14635
14636 if (cu->per_cu->from_debug_types)
14637 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
14638 else
14639 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
14640
14641 if (*type_hash_ptr == NULL)
14642 {
14643 *type_hash_ptr
14644 = htab_create_alloc_ex (127,
14645 offset_and_type_hash,
14646 offset_and_type_eq,
14647 NULL,
14648 &objfile->objfile_obstack,
14649 hashtab_obstack_allocate,
14650 dummy_obstack_deallocate);
14651 }
14652
14653 ofs.offset = die->offset;
14654 ofs.type = type;
14655 slot = (struct dwarf2_offset_and_type **)
14656 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
14657 if (*slot)
14658 complaint (&symfile_complaints,
14659 _("A problem internal to GDB: DIE 0x%x has type already set"),
14660 die->offset);
14661 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
14662 **slot = ofs;
14663 return type;
14664 }
14665
14666 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
14667 table, or return NULL if the die does not have a saved type. */
14668
14669 static struct type *
14670 get_die_type_at_offset (unsigned int offset,
14671 struct dwarf2_per_cu_data *per_cu)
14672 {
14673 struct dwarf2_offset_and_type *slot, ofs;
14674 htab_t type_hash;
14675
14676 if (per_cu->from_debug_types)
14677 type_hash = dwarf2_per_objfile->debug_types_type_hash;
14678 else
14679 type_hash = dwarf2_per_objfile->debug_info_type_hash;
14680 if (type_hash == NULL)
14681 return NULL;
14682
14683 ofs.offset = offset;
14684 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
14685 if (slot)
14686 return slot->type;
14687 else
14688 return NULL;
14689 }
14690
14691 /* Look up the type for DIE in the appropriate type_hash table,
14692 or return NULL if DIE does not have a saved type. */
14693
14694 static struct type *
14695 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
14696 {
14697 return get_die_type_at_offset (die->offset, cu->per_cu);
14698 }
14699
14700 /* Add a dependence relationship from CU to REF_PER_CU. */
14701
14702 static void
14703 dwarf2_add_dependence (struct dwarf2_cu *cu,
14704 struct dwarf2_per_cu_data *ref_per_cu)
14705 {
14706 void **slot;
14707
14708 if (cu->dependencies == NULL)
14709 cu->dependencies
14710 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
14711 NULL, &cu->comp_unit_obstack,
14712 hashtab_obstack_allocate,
14713 dummy_obstack_deallocate);
14714
14715 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
14716 if (*slot == NULL)
14717 *slot = ref_per_cu;
14718 }
14719
14720 /* Subroutine of dwarf2_mark to pass to htab_traverse.
14721 Set the mark field in every compilation unit in the
14722 cache that we must keep because we are keeping CU. */
14723
14724 static int
14725 dwarf2_mark_helper (void **slot, void *data)
14726 {
14727 struct dwarf2_per_cu_data *per_cu;
14728
14729 per_cu = (struct dwarf2_per_cu_data *) *slot;
14730 if (per_cu->cu->mark)
14731 return 1;
14732 per_cu->cu->mark = 1;
14733
14734 if (per_cu->cu->dependencies != NULL)
14735 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
14736
14737 return 1;
14738 }
14739
14740 /* Set the mark field in CU and in every other compilation unit in the
14741 cache that we must keep because we are keeping CU. */
14742
14743 static void
14744 dwarf2_mark (struct dwarf2_cu *cu)
14745 {
14746 if (cu->mark)
14747 return;
14748 cu->mark = 1;
14749 if (cu->dependencies != NULL)
14750 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
14751 }
14752
14753 static void
14754 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
14755 {
14756 while (per_cu)
14757 {
14758 per_cu->cu->mark = 0;
14759 per_cu = per_cu->cu->read_in_chain;
14760 }
14761 }
14762
14763 /* Trivial hash function for partial_die_info: the hash value of a DIE
14764 is its offset in .debug_info for this objfile. */
14765
14766 static hashval_t
14767 partial_die_hash (const void *item)
14768 {
14769 const struct partial_die_info *part_die = item;
14770
14771 return part_die->offset;
14772 }
14773
14774 /* Trivial comparison function for partial_die_info structures: two DIEs
14775 are equal if they have the same offset. */
14776
14777 static int
14778 partial_die_eq (const void *item_lhs, const void *item_rhs)
14779 {
14780 const struct partial_die_info *part_die_lhs = item_lhs;
14781 const struct partial_die_info *part_die_rhs = item_rhs;
14782
14783 return part_die_lhs->offset == part_die_rhs->offset;
14784 }
14785
14786 static struct cmd_list_element *set_dwarf2_cmdlist;
14787 static struct cmd_list_element *show_dwarf2_cmdlist;
14788
14789 static void
14790 set_dwarf2_cmd (char *args, int from_tty)
14791 {
14792 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
14793 }
14794
14795 static void
14796 show_dwarf2_cmd (char *args, int from_tty)
14797 {
14798 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
14799 }
14800
14801 /* If section described by INFO was mmapped, munmap it now. */
14802
14803 static void
14804 munmap_section_buffer (struct dwarf2_section_info *info)
14805 {
14806 if (info->was_mmapped)
14807 {
14808 #ifdef HAVE_MMAP
14809 intptr_t begin = (intptr_t) info->buffer;
14810 intptr_t map_begin = begin & ~(pagesize - 1);
14811 size_t map_length = info->size + begin - map_begin;
14812
14813 gdb_assert (munmap ((void *) map_begin, map_length) == 0);
14814 #else
14815 /* Without HAVE_MMAP, we should never be here to begin with. */
14816 gdb_assert_not_reached ("no mmap support");
14817 #endif
14818 }
14819 }
14820
14821 /* munmap debug sections for OBJFILE, if necessary. */
14822
14823 static void
14824 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
14825 {
14826 struct dwarf2_per_objfile *data = d;
14827
14828 /* This is sorted according to the order they're defined in to make it easier
14829 to keep in sync. */
14830 munmap_section_buffer (&data->info);
14831 munmap_section_buffer (&data->abbrev);
14832 munmap_section_buffer (&data->line);
14833 munmap_section_buffer (&data->loc);
14834 munmap_section_buffer (&data->macinfo);
14835 munmap_section_buffer (&data->str);
14836 munmap_section_buffer (&data->ranges);
14837 munmap_section_buffer (&data->types);
14838 munmap_section_buffer (&data->frame);
14839 munmap_section_buffer (&data->eh_frame);
14840 munmap_section_buffer (&data->gdb_index);
14841 }
14842
14843 \f
14844 /* The "save gdb-index" command. */
14845
14846 /* The contents of the hash table we create when building the string
14847 table. */
14848 struct strtab_entry
14849 {
14850 offset_type offset;
14851 const char *str;
14852 };
14853
14854 /* Hash function for a strtab_entry. */
14855
14856 static hashval_t
14857 hash_strtab_entry (const void *e)
14858 {
14859 const struct strtab_entry *entry = e;
14860 return mapped_index_string_hash (entry->str);
14861 }
14862
14863 /* Equality function for a strtab_entry. */
14864
14865 static int
14866 eq_strtab_entry (const void *a, const void *b)
14867 {
14868 const struct strtab_entry *ea = a;
14869 const struct strtab_entry *eb = b;
14870 return !strcmp (ea->str, eb->str);
14871 }
14872
14873 /* Create a strtab_entry hash table. */
14874
14875 static htab_t
14876 create_strtab (void)
14877 {
14878 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
14879 xfree, xcalloc, xfree);
14880 }
14881
14882 /* Add a string to the constant pool. Return the string's offset in
14883 host order. */
14884
14885 static offset_type
14886 add_string (htab_t table, struct obstack *cpool, const char *str)
14887 {
14888 void **slot;
14889 struct strtab_entry entry;
14890 struct strtab_entry *result;
14891
14892 entry.str = str;
14893 slot = htab_find_slot (table, &entry, INSERT);
14894 if (*slot)
14895 result = *slot;
14896 else
14897 {
14898 result = XNEW (struct strtab_entry);
14899 result->offset = obstack_object_size (cpool);
14900 result->str = str;
14901 obstack_grow_str0 (cpool, str);
14902 *slot = result;
14903 }
14904 return result->offset;
14905 }
14906
14907 /* An entry in the symbol table. */
14908 struct symtab_index_entry
14909 {
14910 /* The name of the symbol. */
14911 const char *name;
14912 /* The offset of the name in the constant pool. */
14913 offset_type index_offset;
14914 /* A sorted vector of the indices of all the CUs that hold an object
14915 of this name. */
14916 VEC (offset_type) *cu_indices;
14917 };
14918
14919 /* The symbol table. This is a power-of-2-sized hash table. */
14920 struct mapped_symtab
14921 {
14922 offset_type n_elements;
14923 offset_type size;
14924 struct symtab_index_entry **data;
14925 };
14926
14927 /* Hash function for a symtab_index_entry. */
14928
14929 static hashval_t
14930 hash_symtab_entry (const void *e)
14931 {
14932 const struct symtab_index_entry *entry = e;
14933 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
14934 sizeof (offset_type) * VEC_length (offset_type,
14935 entry->cu_indices),
14936 0);
14937 }
14938
14939 /* Equality function for a symtab_index_entry. */
14940
14941 static int
14942 eq_symtab_entry (const void *a, const void *b)
14943 {
14944 const struct symtab_index_entry *ea = a;
14945 const struct symtab_index_entry *eb = b;
14946 int len = VEC_length (offset_type, ea->cu_indices);
14947 if (len != VEC_length (offset_type, eb->cu_indices))
14948 return 0;
14949 return !memcmp (VEC_address (offset_type, ea->cu_indices),
14950 VEC_address (offset_type, eb->cu_indices),
14951 sizeof (offset_type) * len);
14952 }
14953
14954 /* Destroy a symtab_index_entry. */
14955
14956 static void
14957 delete_symtab_entry (void *p)
14958 {
14959 struct symtab_index_entry *entry = p;
14960 VEC_free (offset_type, entry->cu_indices);
14961 xfree (entry);
14962 }
14963
14964 /* Create a hash table holding symtab_index_entry objects. */
14965
14966 static htab_t
14967 create_symbol_hash_table (void)
14968 {
14969 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
14970 delete_symtab_entry, xcalloc, xfree);
14971 }
14972
14973 /* Create a new mapped symtab object. */
14974
14975 static struct mapped_symtab *
14976 create_mapped_symtab (void)
14977 {
14978 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
14979 symtab->n_elements = 0;
14980 symtab->size = 1024;
14981 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
14982 return symtab;
14983 }
14984
14985 /* Destroy a mapped_symtab. */
14986
14987 static void
14988 cleanup_mapped_symtab (void *p)
14989 {
14990 struct mapped_symtab *symtab = p;
14991 /* The contents of the array are freed when the other hash table is
14992 destroyed. */
14993 xfree (symtab->data);
14994 xfree (symtab);
14995 }
14996
14997 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
14998 the slot. */
14999
15000 static struct symtab_index_entry **
15001 find_slot (struct mapped_symtab *symtab, const char *name)
15002 {
15003 offset_type index, step, hash = mapped_index_string_hash (name);
15004
15005 index = hash & (symtab->size - 1);
15006 step = ((hash * 17) & (symtab->size - 1)) | 1;
15007
15008 for (;;)
15009 {
15010 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15011 return &symtab->data[index];
15012 index = (index + step) & (symtab->size - 1);
15013 }
15014 }
15015
15016 /* Expand SYMTAB's hash table. */
15017
15018 static void
15019 hash_expand (struct mapped_symtab *symtab)
15020 {
15021 offset_type old_size = symtab->size;
15022 offset_type i;
15023 struct symtab_index_entry **old_entries = symtab->data;
15024
15025 symtab->size *= 2;
15026 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15027
15028 for (i = 0; i < old_size; ++i)
15029 {
15030 if (old_entries[i])
15031 {
15032 struct symtab_index_entry **slot = find_slot (symtab,
15033 old_entries[i]->name);
15034 *slot = old_entries[i];
15035 }
15036 }
15037
15038 xfree (old_entries);
15039 }
15040
15041 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15042 is the index of the CU in which the symbol appears. */
15043
15044 static void
15045 add_index_entry (struct mapped_symtab *symtab, const char *name,
15046 offset_type cu_index)
15047 {
15048 struct symtab_index_entry **slot;
15049
15050 ++symtab->n_elements;
15051 if (4 * symtab->n_elements / 3 >= symtab->size)
15052 hash_expand (symtab);
15053
15054 slot = find_slot (symtab, name);
15055 if (!*slot)
15056 {
15057 *slot = XNEW (struct symtab_index_entry);
15058 (*slot)->name = name;
15059 (*slot)->cu_indices = NULL;
15060 }
15061 /* Don't push an index twice. Due to how we add entries we only
15062 have to check the last one. */
15063 if (VEC_empty (offset_type, (*slot)->cu_indices)
15064 || VEC_length (offset_type, (*slot)->cu_indices) != cu_index)
15065 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15066 }
15067
15068 /* Add a vector of indices to the constant pool. */
15069
15070 static offset_type
15071 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15072 struct symtab_index_entry *entry)
15073 {
15074 void **slot;
15075
15076 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15077 if (!*slot)
15078 {
15079 offset_type len = VEC_length (offset_type, entry->cu_indices);
15080 offset_type val = MAYBE_SWAP (len);
15081 offset_type iter;
15082 int i;
15083
15084 *slot = entry;
15085 entry->index_offset = obstack_object_size (cpool);
15086
15087 obstack_grow (cpool, &val, sizeof (val));
15088 for (i = 0;
15089 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15090 ++i)
15091 {
15092 val = MAYBE_SWAP (iter);
15093 obstack_grow (cpool, &val, sizeof (val));
15094 }
15095 }
15096 else
15097 {
15098 struct symtab_index_entry *old_entry = *slot;
15099 entry->index_offset = old_entry->index_offset;
15100 entry = old_entry;
15101 }
15102 return entry->index_offset;
15103 }
15104
15105 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15106 constant pool entries going into the obstack CPOOL. */
15107
15108 static void
15109 write_hash_table (struct mapped_symtab *symtab,
15110 struct obstack *output, struct obstack *cpool)
15111 {
15112 offset_type i;
15113 htab_t symbol_hash_table;
15114 htab_t str_table;
15115
15116 symbol_hash_table = create_symbol_hash_table ();
15117 str_table = create_strtab ();
15118
15119 /* We add all the index vectors to the constant pool first, to
15120 ensure alignment is ok. */
15121 for (i = 0; i < symtab->size; ++i)
15122 {
15123 if (symtab->data[i])
15124 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15125 }
15126
15127 /* Now write out the hash table. */
15128 for (i = 0; i < symtab->size; ++i)
15129 {
15130 offset_type str_off, vec_off;
15131
15132 if (symtab->data[i])
15133 {
15134 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15135 vec_off = symtab->data[i]->index_offset;
15136 }
15137 else
15138 {
15139 /* While 0 is a valid constant pool index, it is not valid
15140 to have 0 for both offsets. */
15141 str_off = 0;
15142 vec_off = 0;
15143 }
15144
15145 str_off = MAYBE_SWAP (str_off);
15146 vec_off = MAYBE_SWAP (vec_off);
15147
15148 obstack_grow (output, &str_off, sizeof (str_off));
15149 obstack_grow (output, &vec_off, sizeof (vec_off));
15150 }
15151
15152 htab_delete (str_table);
15153 htab_delete (symbol_hash_table);
15154 }
15155
15156 /* Struct to map psymtab to CU index in the index file. */
15157 struct psymtab_cu_index_map
15158 {
15159 struct partial_symtab *psymtab;
15160 unsigned int cu_index;
15161 };
15162
15163 static hashval_t
15164 hash_psymtab_cu_index (const void *item)
15165 {
15166 const struct psymtab_cu_index_map *map = item;
15167
15168 return htab_hash_pointer (map->psymtab);
15169 }
15170
15171 static int
15172 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15173 {
15174 const struct psymtab_cu_index_map *lhs = item_lhs;
15175 const struct psymtab_cu_index_map *rhs = item_rhs;
15176
15177 return lhs->psymtab == rhs->psymtab;
15178 }
15179
15180 /* Helper struct for building the address table. */
15181 struct addrmap_index_data
15182 {
15183 struct objfile *objfile;
15184 struct obstack *addr_obstack;
15185 htab_t cu_index_htab;
15186
15187 /* Non-zero if the previous_* fields are valid.
15188 We can't write an entry until we see the next entry (since it is only then
15189 that we know the end of the entry). */
15190 int previous_valid;
15191 /* Index of the CU in the table of all CUs in the index file. */
15192 unsigned int previous_cu_index;
15193 /* Start address of the CU. */
15194 CORE_ADDR previous_cu_start;
15195 };
15196
15197 /* Write an address entry to OBSTACK. */
15198
15199 static void
15200 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15201 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15202 {
15203 offset_type cu_index_to_write;
15204 char addr[8];
15205 CORE_ADDR baseaddr;
15206
15207 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15208
15209 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15210 obstack_grow (obstack, addr, 8);
15211 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15212 obstack_grow (obstack, addr, 8);
15213 cu_index_to_write = MAYBE_SWAP (cu_index);
15214 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15215 }
15216
15217 /* Worker function for traversing an addrmap to build the address table. */
15218
15219 static int
15220 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15221 {
15222 struct addrmap_index_data *data = datap;
15223 struct partial_symtab *pst = obj;
15224 offset_type cu_index;
15225 void **slot;
15226
15227 if (data->previous_valid)
15228 add_address_entry (data->objfile, data->addr_obstack,
15229 data->previous_cu_start, start_addr,
15230 data->previous_cu_index);
15231
15232 data->previous_cu_start = start_addr;
15233 if (pst != NULL)
15234 {
15235 struct psymtab_cu_index_map find_map, *map;
15236 find_map.psymtab = pst;
15237 map = htab_find (data->cu_index_htab, &find_map);
15238 gdb_assert (map != NULL);
15239 data->previous_cu_index = map->cu_index;
15240 data->previous_valid = 1;
15241 }
15242 else
15243 data->previous_valid = 0;
15244
15245 return 0;
15246 }
15247
15248 /* Write OBJFILE's address map to OBSTACK.
15249 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15250 in the index file. */
15251
15252 static void
15253 write_address_map (struct objfile *objfile, struct obstack *obstack,
15254 htab_t cu_index_htab)
15255 {
15256 struct addrmap_index_data addrmap_index_data;
15257
15258 /* When writing the address table, we have to cope with the fact that
15259 the addrmap iterator only provides the start of a region; we have to
15260 wait until the next invocation to get the start of the next region. */
15261
15262 addrmap_index_data.objfile = objfile;
15263 addrmap_index_data.addr_obstack = obstack;
15264 addrmap_index_data.cu_index_htab = cu_index_htab;
15265 addrmap_index_data.previous_valid = 0;
15266
15267 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15268 &addrmap_index_data);
15269
15270 /* It's highly unlikely the last entry (end address = 0xff...ff)
15271 is valid, but we should still handle it.
15272 The end address is recorded as the start of the next region, but that
15273 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15274 anyway. */
15275 if (addrmap_index_data.previous_valid)
15276 add_address_entry (objfile, obstack,
15277 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15278 addrmap_index_data.previous_cu_index);
15279 }
15280
15281 /* Add a list of partial symbols to SYMTAB. */
15282
15283 static void
15284 write_psymbols (struct mapped_symtab *symtab,
15285 htab_t psyms_seen,
15286 struct partial_symbol **psymp,
15287 int count,
15288 offset_type cu_index,
15289 int is_static)
15290 {
15291 for (; count-- > 0; ++psymp)
15292 {
15293 void **slot, *lookup;
15294
15295 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15296 error (_("Ada is not currently supported by the index"));
15297
15298 /* We only want to add a given psymbol once. However, we also
15299 want to account for whether it is global or static. So, we
15300 may add it twice, using slightly different values. */
15301 if (is_static)
15302 {
15303 uintptr_t val = 1 | (uintptr_t) *psymp;
15304
15305 lookup = (void *) val;
15306 }
15307 else
15308 lookup = *psymp;
15309
15310 /* Only add a given psymbol once. */
15311 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15312 if (!*slot)
15313 {
15314 *slot = lookup;
15315 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15316 }
15317 }
15318 }
15319
15320 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
15321 exception if there is an error. */
15322
15323 static void
15324 write_obstack (FILE *file, struct obstack *obstack)
15325 {
15326 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15327 file)
15328 != obstack_object_size (obstack))
15329 error (_("couldn't data write to file"));
15330 }
15331
15332 /* Unlink a file if the argument is not NULL. */
15333
15334 static void
15335 unlink_if_set (void *p)
15336 {
15337 char **filename = p;
15338 if (*filename)
15339 unlink (*filename);
15340 }
15341
15342 /* A helper struct used when iterating over debug_types. */
15343 struct signatured_type_index_data
15344 {
15345 struct objfile *objfile;
15346 struct mapped_symtab *symtab;
15347 struct obstack *types_list;
15348 htab_t psyms_seen;
15349 int cu_index;
15350 };
15351
15352 /* A helper function that writes a single signatured_type to an
15353 obstack. */
15354
15355 static int
15356 write_one_signatured_type (void **slot, void *d)
15357 {
15358 struct signatured_type_index_data *info = d;
15359 struct signatured_type *entry = (struct signatured_type *) *slot;
15360 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15361 struct partial_symtab *psymtab = per_cu->v.psymtab;
15362 gdb_byte val[8];
15363
15364 write_psymbols (info->symtab,
15365 info->psyms_seen,
15366 info->objfile->global_psymbols.list + psymtab->globals_offset,
15367 psymtab->n_global_syms, info->cu_index,
15368 0);
15369 write_psymbols (info->symtab,
15370 info->psyms_seen,
15371 info->objfile->static_psymbols.list + psymtab->statics_offset,
15372 psymtab->n_static_syms, info->cu_index,
15373 1);
15374
15375 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->offset);
15376 obstack_grow (info->types_list, val, 8);
15377 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
15378 obstack_grow (info->types_list, val, 8);
15379 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
15380 obstack_grow (info->types_list, val, 8);
15381
15382 ++info->cu_index;
15383
15384 return 1;
15385 }
15386
15387 /* A cleanup function for an htab_t. */
15388
15389 static void
15390 cleanup_htab (void *arg)
15391 {
15392 htab_delete (arg);
15393 }
15394
15395 /* Create an index file for OBJFILE in the directory DIR. */
15396
15397 static void
15398 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
15399 {
15400 struct cleanup *cleanup;
15401 char *filename, *cleanup_filename;
15402 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
15403 struct obstack cu_list, types_cu_list;
15404 int i;
15405 FILE *out_file;
15406 struct mapped_symtab *symtab;
15407 offset_type val, size_of_contents, total_len;
15408 struct stat st;
15409 char buf[8];
15410 htab_t psyms_seen;
15411 htab_t cu_index_htab;
15412 struct psymtab_cu_index_map *psymtab_cu_index_map;
15413
15414 if (!objfile->psymtabs)
15415 return;
15416 if (dwarf2_per_objfile->using_index)
15417 error (_("Cannot use an index to create the index"));
15418
15419 if (stat (objfile->name, &st) < 0)
15420 perror_with_name (objfile->name);
15421
15422 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
15423 INDEX_SUFFIX, (char *) NULL);
15424 cleanup = make_cleanup (xfree, filename);
15425
15426 out_file = fopen (filename, "wb");
15427 if (!out_file)
15428 error (_("Can't open `%s' for writing"), filename);
15429
15430 cleanup_filename = filename;
15431 make_cleanup (unlink_if_set, &cleanup_filename);
15432
15433 symtab = create_mapped_symtab ();
15434 make_cleanup (cleanup_mapped_symtab, symtab);
15435
15436 obstack_init (&addr_obstack);
15437 make_cleanup_obstack_free (&addr_obstack);
15438
15439 obstack_init (&cu_list);
15440 make_cleanup_obstack_free (&cu_list);
15441
15442 obstack_init (&types_cu_list);
15443 make_cleanup_obstack_free (&types_cu_list);
15444
15445 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
15446 NULL, xcalloc, xfree);
15447 make_cleanup (cleanup_htab, psyms_seen);
15448
15449 /* While we're scanning CU's create a table that maps a psymtab pointer
15450 (which is what addrmap records) to its index (which is what is recorded
15451 in the index file). This will later be needed to write the address
15452 table. */
15453 cu_index_htab = htab_create_alloc (100,
15454 hash_psymtab_cu_index,
15455 eq_psymtab_cu_index,
15456 NULL, xcalloc, xfree);
15457 make_cleanup (cleanup_htab, cu_index_htab);
15458 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
15459 xmalloc (sizeof (struct psymtab_cu_index_map)
15460 * dwarf2_per_objfile->n_comp_units);
15461 make_cleanup (xfree, psymtab_cu_index_map);
15462
15463 /* The CU list is already sorted, so we don't need to do additional
15464 work here. Also, the debug_types entries do not appear in
15465 all_comp_units, but only in their own hash table. */
15466 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
15467 {
15468 struct dwarf2_per_cu_data *per_cu = dwarf2_per_objfile->all_comp_units[i];
15469 struct partial_symtab *psymtab = per_cu->v.psymtab;
15470 gdb_byte val[8];
15471 struct psymtab_cu_index_map *map;
15472 void **slot;
15473
15474 write_psymbols (symtab,
15475 psyms_seen,
15476 objfile->global_psymbols.list + psymtab->globals_offset,
15477 psymtab->n_global_syms, i,
15478 0);
15479 write_psymbols (symtab,
15480 psyms_seen,
15481 objfile->static_psymbols.list + psymtab->statics_offset,
15482 psymtab->n_static_syms, i,
15483 1);
15484
15485 map = &psymtab_cu_index_map[i];
15486 map->psymtab = psymtab;
15487 map->cu_index = i;
15488 slot = htab_find_slot (cu_index_htab, map, INSERT);
15489 gdb_assert (slot != NULL);
15490 gdb_assert (*slot == NULL);
15491 *slot = map;
15492
15493 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
15494 obstack_grow (&cu_list, val, 8);
15495 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
15496 obstack_grow (&cu_list, val, 8);
15497 }
15498
15499 /* Dump the address map. */
15500 write_address_map (objfile, &addr_obstack, cu_index_htab);
15501
15502 /* Write out the .debug_type entries, if any. */
15503 if (dwarf2_per_objfile->signatured_types)
15504 {
15505 struct signatured_type_index_data sig_data;
15506
15507 sig_data.objfile = objfile;
15508 sig_data.symtab = symtab;
15509 sig_data.types_list = &types_cu_list;
15510 sig_data.psyms_seen = psyms_seen;
15511 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
15512 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
15513 write_one_signatured_type, &sig_data);
15514 }
15515
15516 obstack_init (&constant_pool);
15517 make_cleanup_obstack_free (&constant_pool);
15518 obstack_init (&symtab_obstack);
15519 make_cleanup_obstack_free (&symtab_obstack);
15520 write_hash_table (symtab, &symtab_obstack, &constant_pool);
15521
15522 obstack_init (&contents);
15523 make_cleanup_obstack_free (&contents);
15524 size_of_contents = 6 * sizeof (offset_type);
15525 total_len = size_of_contents;
15526
15527 /* The version number. */
15528 val = MAYBE_SWAP (3);
15529 obstack_grow (&contents, &val, sizeof (val));
15530
15531 /* The offset of the CU list from the start of the file. */
15532 val = MAYBE_SWAP (total_len);
15533 obstack_grow (&contents, &val, sizeof (val));
15534 total_len += obstack_object_size (&cu_list);
15535
15536 /* The offset of the types CU list from the start of the file. */
15537 val = MAYBE_SWAP (total_len);
15538 obstack_grow (&contents, &val, sizeof (val));
15539 total_len += obstack_object_size (&types_cu_list);
15540
15541 /* The offset of the address table from the start of the file. */
15542 val = MAYBE_SWAP (total_len);
15543 obstack_grow (&contents, &val, sizeof (val));
15544 total_len += obstack_object_size (&addr_obstack);
15545
15546 /* The offset of the symbol table from the start of the file. */
15547 val = MAYBE_SWAP (total_len);
15548 obstack_grow (&contents, &val, sizeof (val));
15549 total_len += obstack_object_size (&symtab_obstack);
15550
15551 /* The offset of the constant pool from the start of the file. */
15552 val = MAYBE_SWAP (total_len);
15553 obstack_grow (&contents, &val, sizeof (val));
15554 total_len += obstack_object_size (&constant_pool);
15555
15556 gdb_assert (obstack_object_size (&contents) == size_of_contents);
15557
15558 write_obstack (out_file, &contents);
15559 write_obstack (out_file, &cu_list);
15560 write_obstack (out_file, &types_cu_list);
15561 write_obstack (out_file, &addr_obstack);
15562 write_obstack (out_file, &symtab_obstack);
15563 write_obstack (out_file, &constant_pool);
15564
15565 fclose (out_file);
15566
15567 /* We want to keep the file, so we set cleanup_filename to NULL
15568 here. See unlink_if_set. */
15569 cleanup_filename = NULL;
15570
15571 do_cleanups (cleanup);
15572 }
15573
15574 /* The mapped index file format is designed to be directly mmap()able
15575 on any architecture. In most cases, a datum is represented using a
15576 little-endian 32-bit integer value, called an offset_type. Big
15577 endian machines must byte-swap the values before using them.
15578 Exceptions to this rule are noted. The data is laid out such that
15579 alignment is always respected.
15580
15581 A mapped index consists of several sections.
15582
15583 1. The file header. This is a sequence of values, of offset_type
15584 unless otherwise noted:
15585
15586 [0] The version number, currently 3. Versions 1 and 2 are
15587 obsolete.
15588 [1] The offset, from the start of the file, of the CU list.
15589 [2] The offset, from the start of the file, of the types CU list.
15590 Note that this section can be empty, in which case this offset will
15591 be equal to the next offset.
15592 [3] The offset, from the start of the file, of the address section.
15593 [4] The offset, from the start of the file, of the symbol table.
15594 [5] The offset, from the start of the file, of the constant pool.
15595
15596 2. The CU list. This is a sequence of pairs of 64-bit
15597 little-endian values, sorted by the CU offset. The first element
15598 in each pair is the offset of a CU in the .debug_info section. The
15599 second element in each pair is the length of that CU. References
15600 to a CU elsewhere in the map are done using a CU index, which is
15601 just the 0-based index into this table. Note that if there are
15602 type CUs, then conceptually CUs and type CUs form a single list for
15603 the purposes of CU indices.
15604
15605 3. The types CU list. This is a sequence of triplets of 64-bit
15606 little-endian values. In a triplet, the first value is the CU
15607 offset, the second value is the type offset in the CU, and the
15608 third value is the type signature. The types CU list is not
15609 sorted.
15610
15611 4. The address section. The address section consists of a sequence
15612 of address entries. Each address entry has three elements.
15613 [0] The low address. This is a 64-bit little-endian value.
15614 [1] The high address. This is a 64-bit little-endian value.
15615 Like DW_AT_high_pc, the value is one byte beyond the end.
15616 [2] The CU index. This is an offset_type value.
15617
15618 5. The symbol table. This is a hash table. The size of the hash
15619 table is always a power of 2. The initial hash and the step are
15620 currently defined by the `find_slot' function.
15621
15622 Each slot in the hash table consists of a pair of offset_type
15623 values. The first value is the offset of the symbol's name in the
15624 constant pool. The second value is the offset of the CU vector in
15625 the constant pool.
15626
15627 If both values are 0, then this slot in the hash table is empty.
15628 This is ok because while 0 is a valid constant pool index, it
15629 cannot be a valid index for both a string and a CU vector.
15630
15631 A string in the constant pool is stored as a \0-terminated string,
15632 as you'd expect.
15633
15634 A CU vector in the constant pool is a sequence of offset_type
15635 values. The first value is the number of CU indices in the vector.
15636 Each subsequent value is the index of a CU in the CU list. This
15637 element in the hash table is used to indicate which CUs define the
15638 symbol.
15639
15640 6. The constant pool. This is simply a bunch of bytes. It is
15641 organized so that alignment is correct: CU vectors are stored
15642 first, followed by strings. */
15643
15644 static void
15645 save_gdb_index_command (char *arg, int from_tty)
15646 {
15647 struct objfile *objfile;
15648
15649 if (!arg || !*arg)
15650 error (_("usage: save gdb-index DIRECTORY"));
15651
15652 ALL_OBJFILES (objfile)
15653 {
15654 struct stat st;
15655
15656 /* If the objfile does not correspond to an actual file, skip it. */
15657 if (stat (objfile->name, &st) < 0)
15658 continue;
15659
15660 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15661 if (dwarf2_per_objfile)
15662 {
15663 volatile struct gdb_exception except;
15664
15665 TRY_CATCH (except, RETURN_MASK_ERROR)
15666 {
15667 write_psymtabs_to_index (objfile, arg);
15668 }
15669 if (except.reason < 0)
15670 exception_fprintf (gdb_stderr, except,
15671 _("Error while writing index for `%s': "),
15672 objfile->name);
15673 }
15674 }
15675 }
15676
15677 \f
15678
15679 int dwarf2_always_disassemble;
15680
15681 static void
15682 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
15683 struct cmd_list_element *c, const char *value)
15684 {
15685 fprintf_filtered (file, _("\
15686 Whether to always disassemble DWARF expressions is %s.\n"),
15687 value);
15688 }
15689
15690 void _initialize_dwarf2_read (void);
15691
15692 void
15693 _initialize_dwarf2_read (void)
15694 {
15695 struct cmd_list_element *c;
15696
15697 dwarf2_objfile_data_key
15698 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
15699
15700 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
15701 Set DWARF 2 specific variables.\n\
15702 Configure DWARF 2 variables such as the cache size"),
15703 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
15704 0/*allow-unknown*/, &maintenance_set_cmdlist);
15705
15706 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
15707 Show DWARF 2 specific variables\n\
15708 Show DWARF 2 variables such as the cache size"),
15709 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
15710 0/*allow-unknown*/, &maintenance_show_cmdlist);
15711
15712 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
15713 &dwarf2_max_cache_age, _("\
15714 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
15715 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
15716 A higher limit means that cached compilation units will be stored\n\
15717 in memory longer, and more total memory will be used. Zero disables\n\
15718 caching, which can slow down startup."),
15719 NULL,
15720 show_dwarf2_max_cache_age,
15721 &set_dwarf2_cmdlist,
15722 &show_dwarf2_cmdlist);
15723
15724 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
15725 &dwarf2_always_disassemble, _("\
15726 Set whether `info address' always disassembles DWARF expressions."), _("\
15727 Show whether `info address' always disassembles DWARF expressions."), _("\
15728 When enabled, DWARF expressions are always printed in an assembly-like\n\
15729 syntax. When disabled, expressions will be printed in a more\n\
15730 conversational style, when possible."),
15731 NULL,
15732 show_dwarf2_always_disassemble,
15733 &set_dwarf2_cmdlist,
15734 &show_dwarf2_cmdlist);
15735
15736 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
15737 Set debugging of the dwarf2 DIE reader."), _("\
15738 Show debugging of the dwarf2 DIE reader."), _("\
15739 When enabled (non-zero), DIEs are dumped after they are read in.\n\
15740 The value is the maximum depth to print."),
15741 NULL,
15742 NULL,
15743 &setdebuglist, &showdebuglist);
15744
15745 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
15746 _("\
15747 Save a .gdb-index file.\n\
15748 Usage: save gdb-index DIRECTORY"),
15749 &save_cmdlist);
15750 set_cmd_completer (c, filename_completer);
15751 }
This page took 0.391602 seconds and 4 git commands to generate.