* symfile-mem.c (symbol_file_add_from_memory): Call do_cleanups.
[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, 2011
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 #include <ctype.h>
61
62 #include <fcntl.h>
63 #include "gdb_string.h"
64 #include "gdb_assert.h"
65 #include <sys/types.h>
66 #ifdef HAVE_ZLIB_H
67 #include <zlib.h>
68 #endif
69 #ifdef HAVE_MMAP
70 #include <sys/mman.h>
71 #ifndef MAP_FAILED
72 #define MAP_FAILED ((void *) -1)
73 #endif
74 #endif
75
76 typedef struct symbol *symbolp;
77 DEF_VEC_P (symbolp);
78
79 #if 0
80 /* .debug_info header for a compilation unit
81 Because of alignment constraints, this structure has padding and cannot
82 be mapped directly onto the beginning of the .debug_info section. */
83 typedef struct comp_unit_header
84 {
85 unsigned int length; /* length of the .debug_info
86 contribution */
87 unsigned short version; /* version number -- 2 for DWARF
88 version 2 */
89 unsigned int abbrev_offset; /* offset into .debug_abbrev section */
90 unsigned char addr_size; /* byte size of an address -- 4 */
91 }
92 _COMP_UNIT_HEADER;
93 #define _ACTUAL_COMP_UNIT_HEADER_SIZE 11
94 #endif
95
96 /* .debug_line statement program prologue
97 Because of alignment constraints, this structure has padding and cannot
98 be mapped directly onto the beginning of the .debug_info section. */
99 typedef struct statement_prologue
100 {
101 unsigned int total_length; /* byte length of the statement
102 information */
103 unsigned short version; /* version number -- 2 for DWARF
104 version 2 */
105 unsigned int prologue_length; /* # bytes between prologue &
106 stmt program */
107 unsigned char minimum_instruction_length; /* byte size of
108 smallest instr */
109 unsigned char default_is_stmt; /* initial value of is_stmt
110 register */
111 char line_base;
112 unsigned char line_range;
113 unsigned char opcode_base; /* number assigned to first special
114 opcode */
115 unsigned char *standard_opcode_lengths;
116 }
117 _STATEMENT_PROLOGUE;
118
119 /* When non-zero, dump DIEs after they are read in. */
120 static int dwarf2_die_debug = 0;
121
122 static int pagesize;
123
124 /* When set, the file that we're processing is known to have debugging
125 info for C++ namespaces. GCC 3.3.x did not produce this information,
126 but later versions do. */
127
128 static int processing_has_namespace_info;
129
130 static const struct objfile_data *dwarf2_objfile_data_key;
131
132 struct dwarf2_section_info
133 {
134 asection *asection;
135 gdb_byte *buffer;
136 bfd_size_type size;
137 /* Not NULL if the section was actually mmapped. */
138 void *map_addr;
139 /* Page aligned size of mmapped area. */
140 bfd_size_type map_len;
141 /* True if we have tried to read this section. */
142 int readin;
143 };
144
145 /* All offsets in the index are of this type. It must be
146 architecture-independent. */
147 typedef uint32_t offset_type;
148
149 DEF_VEC_I (offset_type);
150
151 /* A description of the mapped index. The file format is described in
152 a comment by the code that writes the index. */
153 struct mapped_index
154 {
155 /* Index data format version. */
156 int version;
157
158 /* The total length of the buffer. */
159 off_t total_size;
160
161 /* A pointer to the address table data. */
162 const gdb_byte *address_table;
163
164 /* Size of the address table data in bytes. */
165 offset_type address_table_size;
166
167 /* The symbol table, implemented as a hash table. */
168 const offset_type *symbol_table;
169
170 /* Size in slots, each slot is 2 offset_types. */
171 offset_type symbol_table_slots;
172
173 /* A pointer to the constant pool. */
174 const char *constant_pool;
175 };
176
177 struct dwarf2_per_objfile
178 {
179 struct dwarf2_section_info info;
180 struct dwarf2_section_info abbrev;
181 struct dwarf2_section_info line;
182 struct dwarf2_section_info loc;
183 struct dwarf2_section_info macinfo;
184 struct dwarf2_section_info str;
185 struct dwarf2_section_info ranges;
186 struct dwarf2_section_info types;
187 struct dwarf2_section_info frame;
188 struct dwarf2_section_info eh_frame;
189 struct dwarf2_section_info gdb_index;
190
191 /* Back link. */
192 struct objfile *objfile;
193
194 /* A list of all the compilation units. This is used to locate
195 the target compilation unit of a particular reference. */
196 struct dwarf2_per_cu_data **all_comp_units;
197
198 /* The number of compilation units in ALL_COMP_UNITS. */
199 int n_comp_units;
200
201 /* The number of .debug_types-related CUs. */
202 int n_type_comp_units;
203
204 /* The .debug_types-related CUs. */
205 struct dwarf2_per_cu_data **type_comp_units;
206
207 /* A chain of compilation units that are currently read in, so that
208 they can be freed later. */
209 struct dwarf2_per_cu_data *read_in_chain;
210
211 /* A table mapping .debug_types signatures to its signatured_type entry.
212 This is NULL if the .debug_types section hasn't been read in yet. */
213 htab_t signatured_types;
214
215 /* A flag indicating wether this objfile has a section loaded at a
216 VMA of 0. */
217 int has_section_at_zero;
218
219 /* True if we are using the mapped index,
220 or we are faking it for OBJF_READNOW's sake. */
221 unsigned char using_index;
222
223 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
224 struct mapped_index *index_table;
225
226 /* When using index_table, this keeps track of all quick_file_names entries.
227 TUs can share line table entries with CUs or other TUs, and there can be
228 a lot more TUs than unique line tables, so we maintain a separate table
229 of all line table entries to support the sharing. */
230 htab_t quick_file_names_table;
231
232 /* Set during partial symbol reading, to prevent queueing of full
233 symbols. */
234 int reading_partial_symbols;
235
236 /* Table mapping type .debug_info DIE offsets to types.
237 This is NULL if not allocated yet.
238 It (currently) makes sense to allocate debug_types_type_hash lazily.
239 To keep things simple we allocate both lazily. */
240 htab_t debug_info_type_hash;
241
242 /* Table mapping type .debug_types DIE offsets to types.
243 This is NULL if not allocated yet. */
244 htab_t debug_types_type_hash;
245 };
246
247 static struct dwarf2_per_objfile *dwarf2_per_objfile;
248
249 /* Default names of the debugging sections. */
250
251 /* Note that if the debugging section has been compressed, it might
252 have a name like .zdebug_info. */
253
254 static const struct dwarf2_debug_sections dwarf2_elf_names = {
255 { ".debug_info", ".zdebug_info" },
256 { ".debug_abbrev", ".zdebug_abbrev" },
257 { ".debug_line", ".zdebug_line" },
258 { ".debug_loc", ".zdebug_loc" },
259 { ".debug_macinfo", ".zdebug_macinfo" },
260 { ".debug_str", ".zdebug_str" },
261 { ".debug_ranges", ".zdebug_ranges" },
262 { ".debug_types", ".zdebug_types" },
263 { ".debug_frame", ".zdebug_frame" },
264 { ".eh_frame", NULL },
265 { ".gdb_index", ".zgdb_index" }
266 };
267
268 /* local data types */
269
270 /* We hold several abbreviation tables in memory at the same time. */
271 #ifndef ABBREV_HASH_SIZE
272 #define ABBREV_HASH_SIZE 121
273 #endif
274
275 /* The data in a compilation unit header, after target2host
276 translation, looks like this. */
277 struct comp_unit_head
278 {
279 unsigned int length;
280 short version;
281 unsigned char addr_size;
282 unsigned char signed_addr_p;
283 unsigned int abbrev_offset;
284
285 /* Size of file offsets; either 4 or 8. */
286 unsigned int offset_size;
287
288 /* Size of the length field; either 4 or 12. */
289 unsigned int initial_length_size;
290
291 /* Offset to the first byte of this compilation unit header in the
292 .debug_info section, for resolving relative reference dies. */
293 unsigned int offset;
294
295 /* Offset to first die in this cu from the start of the cu.
296 This will be the first byte following the compilation unit header. */
297 unsigned int first_die_offset;
298 };
299
300 /* Type used for delaying computation of method physnames.
301 See comments for compute_delayed_physnames. */
302 struct delayed_method_info
303 {
304 /* The type to which the method is attached, i.e., its parent class. */
305 struct type *type;
306
307 /* The index of the method in the type's function fieldlists. */
308 int fnfield_index;
309
310 /* The index of the method in the fieldlist. */
311 int index;
312
313 /* The name of the DIE. */
314 const char *name;
315
316 /* The DIE associated with this method. */
317 struct die_info *die;
318 };
319
320 typedef struct delayed_method_info delayed_method_info;
321 DEF_VEC_O (delayed_method_info);
322
323 /* Internal state when decoding a particular compilation unit. */
324 struct dwarf2_cu
325 {
326 /* The objfile containing this compilation unit. */
327 struct objfile *objfile;
328
329 /* The header of the compilation unit. */
330 struct comp_unit_head header;
331
332 /* Base address of this compilation unit. */
333 CORE_ADDR base_address;
334
335 /* Non-zero if base_address has been set. */
336 int base_known;
337
338 struct function_range *first_fn, *last_fn, *cached_fn;
339
340 /* The language we are debugging. */
341 enum language language;
342 const struct language_defn *language_defn;
343
344 const char *producer;
345
346 /* The generic symbol table building routines have separate lists for
347 file scope symbols and all all other scopes (local scopes). So
348 we need to select the right one to pass to add_symbol_to_list().
349 We do it by keeping a pointer to the correct list in list_in_scope.
350
351 FIXME: The original dwarf code just treated the file scope as the
352 first local scope, and all other local scopes as nested local
353 scopes, and worked fine. Check to see if we really need to
354 distinguish these in buildsym.c. */
355 struct pending **list_in_scope;
356
357 /* DWARF abbreviation table associated with this compilation unit. */
358 struct abbrev_info **dwarf2_abbrevs;
359
360 /* Storage for the abbrev table. */
361 struct obstack abbrev_obstack;
362
363 /* Hash table holding all the loaded partial DIEs. */
364 htab_t partial_dies;
365
366 /* Storage for things with the same lifetime as this read-in compilation
367 unit, including partial DIEs. */
368 struct obstack comp_unit_obstack;
369
370 /* When multiple dwarf2_cu structures are living in memory, this field
371 chains them all together, so that they can be released efficiently.
372 We will probably also want a generation counter so that most-recently-used
373 compilation units are cached... */
374 struct dwarf2_per_cu_data *read_in_chain;
375
376 /* Backchain to our per_cu entry if the tree has been built. */
377 struct dwarf2_per_cu_data *per_cu;
378
379 /* How many compilation units ago was this CU last referenced? */
380 int last_used;
381
382 /* A hash table of die offsets for following references. */
383 htab_t die_hash;
384
385 /* Full DIEs if read in. */
386 struct die_info *dies;
387
388 /* A set of pointers to dwarf2_per_cu_data objects for compilation
389 units referenced by this one. Only set during full symbol processing;
390 partial symbol tables do not have dependencies. */
391 htab_t dependencies;
392
393 /* Header data from the line table, during full symbol processing. */
394 struct line_header *line_header;
395
396 /* A list of methods which need to have physnames computed
397 after all type information has been read. */
398 VEC (delayed_method_info) *method_list;
399
400 /* Mark used when releasing cached dies. */
401 unsigned int mark : 1;
402
403 /* This flag will be set if this compilation unit might include
404 inter-compilation-unit references. */
405 unsigned int has_form_ref_addr : 1;
406
407 /* This flag will be set if this compilation unit includes any
408 DW_TAG_namespace DIEs. If we know that there are explicit
409 DIEs for namespaces, we don't need to try to infer them
410 from mangled names. */
411 unsigned int has_namespace_info : 1;
412
413 /* This CU references .debug_loc. See the symtab->locations_valid field.
414 This test is imperfect as there may exist optimized debug code not using
415 any location list and still facing inlining issues if handled as
416 unoptimized code. For a future better test see GCC PR other/32998. */
417
418 unsigned int has_loclist : 1;
419 };
420
421 /* Persistent data held for a compilation unit, even when not
422 processing it. We put a pointer to this structure in the
423 read_symtab_private field of the psymtab. If we encounter
424 inter-compilation-unit references, we also maintain a sorted
425 list of all compilation units. */
426
427 struct dwarf2_per_cu_data
428 {
429 /* The start offset and length of this compilation unit. 2**29-1
430 bytes should suffice to store the length of any compilation unit
431 - if it doesn't, GDB will fall over anyway.
432 NOTE: Unlike comp_unit_head.length, this length includes
433 initial_length_size. */
434 unsigned int offset;
435 unsigned int length : 29;
436
437 /* Flag indicating this compilation unit will be read in before
438 any of the current compilation units are processed. */
439 unsigned int queued : 1;
440
441 /* This flag will be set if we need to load absolutely all DIEs
442 for this compilation unit, instead of just the ones we think
443 are interesting. It gets set if we look for a DIE in the
444 hash table and don't find it. */
445 unsigned int load_all_dies : 1;
446
447 /* Non-zero if this CU is from .debug_types.
448 Otherwise it's from .debug_info. */
449 unsigned int from_debug_types : 1;
450
451 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
452 of the CU cache it gets reset to NULL again. */
453 struct dwarf2_cu *cu;
454
455 /* The corresponding objfile. */
456 struct objfile *objfile;
457
458 /* When using partial symbol tables, the 'psymtab' field is active.
459 Otherwise the 'quick' field is active. */
460 union
461 {
462 /* The partial symbol table associated with this compilation unit,
463 or NULL for partial units (which do not have an associated
464 symtab). */
465 struct partial_symtab *psymtab;
466
467 /* Data needed by the "quick" functions. */
468 struct dwarf2_per_cu_quick_data *quick;
469 } v;
470 };
471
472 /* Entry in the signatured_types hash table. */
473
474 struct signatured_type
475 {
476 ULONGEST signature;
477
478 /* Offset in .debug_types of the type defined by this TU. */
479 unsigned int type_offset;
480
481 /* The CU(/TU) of this type. */
482 struct dwarf2_per_cu_data per_cu;
483 };
484
485 /* Struct used to pass misc. parameters to read_die_and_children, et
486 al. which are used for both .debug_info and .debug_types dies.
487 All parameters here are unchanging for the life of the call. This
488 struct exists to abstract away the constant parameters of die
489 reading. */
490
491 struct die_reader_specs
492 {
493 /* The bfd of this objfile. */
494 bfd* abfd;
495
496 /* The CU of the DIE we are parsing. */
497 struct dwarf2_cu *cu;
498
499 /* Pointer to start of section buffer.
500 This is either the start of .debug_info or .debug_types. */
501 const gdb_byte *buffer;
502 };
503
504 /* The line number information for a compilation unit (found in the
505 .debug_line section) begins with a "statement program header",
506 which contains the following information. */
507 struct line_header
508 {
509 unsigned int total_length;
510 unsigned short version;
511 unsigned int header_length;
512 unsigned char minimum_instruction_length;
513 unsigned char maximum_ops_per_instruction;
514 unsigned char default_is_stmt;
515 int line_base;
516 unsigned char line_range;
517 unsigned char opcode_base;
518
519 /* standard_opcode_lengths[i] is the number of operands for the
520 standard opcode whose value is i. This means that
521 standard_opcode_lengths[0] is unused, and the last meaningful
522 element is standard_opcode_lengths[opcode_base - 1]. */
523 unsigned char *standard_opcode_lengths;
524
525 /* The include_directories table. NOTE! These strings are not
526 allocated with xmalloc; instead, they are pointers into
527 debug_line_buffer. If you try to free them, `free' will get
528 indigestion. */
529 unsigned int num_include_dirs, include_dirs_size;
530 char **include_dirs;
531
532 /* The file_names table. NOTE! These strings are not allocated
533 with xmalloc; instead, they are pointers into debug_line_buffer.
534 Don't try to free them directly. */
535 unsigned int num_file_names, file_names_size;
536 struct file_entry
537 {
538 char *name;
539 unsigned int dir_index;
540 unsigned int mod_time;
541 unsigned int length;
542 int included_p; /* Non-zero if referenced by the Line Number Program. */
543 struct symtab *symtab; /* The associated symbol table, if any. */
544 } *file_names;
545
546 /* The start and end of the statement program following this
547 header. These point into dwarf2_per_objfile->line_buffer. */
548 gdb_byte *statement_program_start, *statement_program_end;
549 };
550
551 /* When we construct a partial symbol table entry we only
552 need this much information. */
553 struct partial_die_info
554 {
555 /* Offset of this DIE. */
556 unsigned int offset;
557
558 /* DWARF-2 tag for this DIE. */
559 ENUM_BITFIELD(dwarf_tag) tag : 16;
560
561 /* Assorted flags describing the data found in this DIE. */
562 unsigned int has_children : 1;
563 unsigned int is_external : 1;
564 unsigned int is_declaration : 1;
565 unsigned int has_type : 1;
566 unsigned int has_specification : 1;
567 unsigned int has_pc_info : 1;
568
569 /* Flag set if the SCOPE field of this structure has been
570 computed. */
571 unsigned int scope_set : 1;
572
573 /* Flag set if the DIE has a byte_size attribute. */
574 unsigned int has_byte_size : 1;
575
576 /* Flag set if any of the DIE's children are template arguments. */
577 unsigned int has_template_arguments : 1;
578
579 /* Flag set if fixup_partial_die has been called on this die. */
580 unsigned int fixup_called : 1;
581
582 /* The name of this DIE. Normally the value of DW_AT_name, but
583 sometimes a default name for unnamed DIEs. */
584 char *name;
585
586 /* The linkage name, if present. */
587 const char *linkage_name;
588
589 /* The scope to prepend to our children. This is generally
590 allocated on the comp_unit_obstack, so will disappear
591 when this compilation unit leaves the cache. */
592 char *scope;
593
594 /* The location description associated with this DIE, if any. */
595 struct dwarf_block *locdesc;
596
597 /* If HAS_PC_INFO, the PC range associated with this DIE. */
598 CORE_ADDR lowpc;
599 CORE_ADDR highpc;
600
601 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
602 DW_AT_sibling, if any. */
603 /* NOTE: This member isn't strictly necessary, read_partial_die could
604 return DW_AT_sibling values to its caller load_partial_dies. */
605 gdb_byte *sibling;
606
607 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
608 DW_AT_specification (or DW_AT_abstract_origin or
609 DW_AT_extension). */
610 unsigned int spec_offset;
611
612 /* Pointers to this DIE's parent, first child, and next sibling,
613 if any. */
614 struct partial_die_info *die_parent, *die_child, *die_sibling;
615 };
616
617 /* This data structure holds the information of an abbrev. */
618 struct abbrev_info
619 {
620 unsigned int number; /* number identifying abbrev */
621 enum dwarf_tag tag; /* dwarf tag */
622 unsigned short has_children; /* boolean */
623 unsigned short num_attrs; /* number of attributes */
624 struct attr_abbrev *attrs; /* an array of attribute descriptions */
625 struct abbrev_info *next; /* next in chain */
626 };
627
628 struct attr_abbrev
629 {
630 ENUM_BITFIELD(dwarf_attribute) name : 16;
631 ENUM_BITFIELD(dwarf_form) form : 16;
632 };
633
634 /* Attributes have a name and a value. */
635 struct attribute
636 {
637 ENUM_BITFIELD(dwarf_attribute) name : 16;
638 ENUM_BITFIELD(dwarf_form) form : 15;
639
640 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
641 field should be in u.str (existing only for DW_STRING) but it is kept
642 here for better struct attribute alignment. */
643 unsigned int string_is_canonical : 1;
644
645 union
646 {
647 char *str;
648 struct dwarf_block *blk;
649 ULONGEST unsnd;
650 LONGEST snd;
651 CORE_ADDR addr;
652 struct signatured_type *signatured_type;
653 }
654 u;
655 };
656
657 /* This data structure holds a complete die structure. */
658 struct die_info
659 {
660 /* DWARF-2 tag for this DIE. */
661 ENUM_BITFIELD(dwarf_tag) tag : 16;
662
663 /* Number of attributes */
664 unsigned char num_attrs;
665
666 /* True if we're presently building the full type name for the
667 type derived from this DIE. */
668 unsigned char building_fullname : 1;
669
670 /* Abbrev number */
671 unsigned int abbrev;
672
673 /* Offset in .debug_info or .debug_types section. */
674 unsigned int offset;
675
676 /* The dies in a compilation unit form an n-ary tree. PARENT
677 points to this die's parent; CHILD points to the first child of
678 this node; and all the children of a given node are chained
679 together via their SIBLING fields. */
680 struct die_info *child; /* Its first child, if any. */
681 struct die_info *sibling; /* Its next sibling, if any. */
682 struct die_info *parent; /* Its parent, if any. */
683
684 /* An array of attributes, with NUM_ATTRS elements. There may be
685 zero, but it's not common and zero-sized arrays are not
686 sufficiently portable C. */
687 struct attribute attrs[1];
688 };
689
690 struct function_range
691 {
692 const char *name;
693 CORE_ADDR lowpc, highpc;
694 int seen_line;
695 struct function_range *next;
696 };
697
698 /* Get at parts of an attribute structure. */
699
700 #define DW_STRING(attr) ((attr)->u.str)
701 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
702 #define DW_UNSND(attr) ((attr)->u.unsnd)
703 #define DW_BLOCK(attr) ((attr)->u.blk)
704 #define DW_SND(attr) ((attr)->u.snd)
705 #define DW_ADDR(attr) ((attr)->u.addr)
706 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
707
708 /* Blocks are a bunch of untyped bytes. */
709 struct dwarf_block
710 {
711 unsigned int size;
712 gdb_byte *data;
713 };
714
715 #ifndef ATTR_ALLOC_CHUNK
716 #define ATTR_ALLOC_CHUNK 4
717 #endif
718
719 /* Allocate fields for structs, unions and enums in this size. */
720 #ifndef DW_FIELD_ALLOC_CHUNK
721 #define DW_FIELD_ALLOC_CHUNK 4
722 #endif
723
724 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
725 but this would require a corresponding change in unpack_field_as_long
726 and friends. */
727 static int bits_per_byte = 8;
728
729 /* The routines that read and process dies for a C struct or C++ class
730 pass lists of data member fields and lists of member function fields
731 in an instance of a field_info structure, as defined below. */
732 struct field_info
733 {
734 /* List of data member and baseclasses fields. */
735 struct nextfield
736 {
737 struct nextfield *next;
738 int accessibility;
739 int virtuality;
740 struct field field;
741 }
742 *fields, *baseclasses;
743
744 /* Number of fields (including baseclasses). */
745 int nfields;
746
747 /* Number of baseclasses. */
748 int nbaseclasses;
749
750 /* Set if the accesibility of one of the fields is not public. */
751 int non_public_fields;
752
753 /* Member function fields array, entries are allocated in the order they
754 are encountered in the object file. */
755 struct nextfnfield
756 {
757 struct nextfnfield *next;
758 struct fn_field fnfield;
759 }
760 *fnfields;
761
762 /* Member function fieldlist array, contains name of possibly overloaded
763 member function, number of overloaded member functions and a pointer
764 to the head of the member function field chain. */
765 struct fnfieldlist
766 {
767 char *name;
768 int length;
769 struct nextfnfield *head;
770 }
771 *fnfieldlists;
772
773 /* Number of entries in the fnfieldlists array. */
774 int nfnfields;
775
776 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
777 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
778 struct typedef_field_list
779 {
780 struct typedef_field field;
781 struct typedef_field_list *next;
782 }
783 *typedef_field_list;
784 unsigned typedef_field_list_count;
785 };
786
787 /* One item on the queue of compilation units to read in full symbols
788 for. */
789 struct dwarf2_queue_item
790 {
791 struct dwarf2_per_cu_data *per_cu;
792 struct dwarf2_queue_item *next;
793 };
794
795 /* The current queue. */
796 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
797
798 /* Loaded secondary compilation units are kept in memory until they
799 have not been referenced for the processing of this many
800 compilation units. Set this to zero to disable caching. Cache
801 sizes of up to at least twenty will improve startup time for
802 typical inter-CU-reference binaries, at an obvious memory cost. */
803 static int dwarf2_max_cache_age = 5;
804 static void
805 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
806 struct cmd_list_element *c, const char *value)
807 {
808 fprintf_filtered (file, _("The upper bound on the age of cached "
809 "dwarf2 compilation units is %s.\n"),
810 value);
811 }
812
813
814 /* Various complaints about symbol reading that don't abort the process. */
815
816 static void
817 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
818 {
819 complaint (&symfile_complaints,
820 _("statement list doesn't fit in .debug_line section"));
821 }
822
823 static void
824 dwarf2_debug_line_missing_file_complaint (void)
825 {
826 complaint (&symfile_complaints,
827 _(".debug_line section has line data without a file"));
828 }
829
830 static void
831 dwarf2_debug_line_missing_end_sequence_complaint (void)
832 {
833 complaint (&symfile_complaints,
834 _(".debug_line section has line "
835 "program sequence without an end"));
836 }
837
838 static void
839 dwarf2_complex_location_expr_complaint (void)
840 {
841 complaint (&symfile_complaints, _("location expression too complex"));
842 }
843
844 static void
845 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
846 int arg3)
847 {
848 complaint (&symfile_complaints,
849 _("const value length mismatch for '%s', got %d, expected %d"),
850 arg1, arg2, arg3);
851 }
852
853 static void
854 dwarf2_macros_too_long_complaint (void)
855 {
856 complaint (&symfile_complaints,
857 _("macro info runs off end of `.debug_macinfo' section"));
858 }
859
860 static void
861 dwarf2_macro_malformed_definition_complaint (const char *arg1)
862 {
863 complaint (&symfile_complaints,
864 _("macro debug info contains a "
865 "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'"),
874 arg1, arg2);
875 }
876
877 /* local function prototypes */
878
879 static void dwarf2_locate_sections (bfd *, asection *, void *);
880
881 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
882 struct objfile *);
883
884 static void dwarf2_build_psymtabs_hard (struct objfile *);
885
886 static void scan_partial_symbols (struct partial_die_info *,
887 CORE_ADDR *, CORE_ADDR *,
888 int, struct dwarf2_cu *);
889
890 static void add_partial_symbol (struct partial_die_info *,
891 struct dwarf2_cu *);
892
893 static void add_partial_namespace (struct partial_die_info *pdi,
894 CORE_ADDR *lowpc, CORE_ADDR *highpc,
895 int need_pc, struct dwarf2_cu *cu);
896
897 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
898 CORE_ADDR *highpc, int need_pc,
899 struct dwarf2_cu *cu);
900
901 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
902 struct dwarf2_cu *cu);
903
904 static void add_partial_subprogram (struct partial_die_info *pdi,
905 CORE_ADDR *lowpc, CORE_ADDR *highpc,
906 int need_pc, struct dwarf2_cu *cu);
907
908 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
909 gdb_byte *buffer, gdb_byte *info_ptr,
910 bfd *abfd, struct dwarf2_cu *cu);
911
912 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
913
914 static void psymtab_to_symtab_1 (struct partial_symtab *);
915
916 static void dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu);
917
918 static void dwarf2_free_abbrev_table (void *);
919
920 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
921 struct dwarf2_cu *);
922
923 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
924 struct dwarf2_cu *);
925
926 static struct partial_die_info *load_partial_dies (bfd *,
927 gdb_byte *, gdb_byte *,
928 int, struct dwarf2_cu *);
929
930 static gdb_byte *read_partial_die (struct partial_die_info *,
931 struct abbrev_info *abbrev,
932 unsigned int, bfd *,
933 gdb_byte *, gdb_byte *,
934 struct dwarf2_cu *);
935
936 static struct partial_die_info *find_partial_die (unsigned int,
937 struct dwarf2_cu *);
938
939 static void fixup_partial_die (struct partial_die_info *,
940 struct dwarf2_cu *);
941
942 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
943 bfd *, gdb_byte *, struct dwarf2_cu *);
944
945 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
946 bfd *, gdb_byte *, struct dwarf2_cu *);
947
948 static unsigned int read_1_byte (bfd *, gdb_byte *);
949
950 static int read_1_signed_byte (bfd *, gdb_byte *);
951
952 static unsigned int read_2_bytes (bfd *, gdb_byte *);
953
954 static unsigned int read_4_bytes (bfd *, gdb_byte *);
955
956 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
957
958 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
959 unsigned int *);
960
961 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
962
963 static LONGEST read_checked_initial_length_and_offset
964 (bfd *, gdb_byte *, const struct comp_unit_head *,
965 unsigned int *, unsigned int *);
966
967 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
968 unsigned int *);
969
970 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
971
972 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
973
974 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
975
976 static char *read_indirect_string (bfd *, gdb_byte *,
977 const struct comp_unit_head *,
978 unsigned int *);
979
980 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
981
982 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
983
984 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
985
986 static void set_cu_language (unsigned int, struct dwarf2_cu *);
987
988 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
989 struct dwarf2_cu *);
990
991 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
992 unsigned int,
993 struct dwarf2_cu *);
994
995 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
996 struct dwarf2_cu *cu);
997
998 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
999
1000 static struct die_info *die_specification (struct die_info *die,
1001 struct dwarf2_cu **);
1002
1003 static void free_line_header (struct line_header *lh);
1004
1005 static void add_file_name (struct line_header *, char *, unsigned int,
1006 unsigned int, unsigned int);
1007
1008 static struct line_header *(dwarf_decode_line_header
1009 (unsigned int offset,
1010 bfd *abfd, struct dwarf2_cu *cu));
1011
1012 static void dwarf_decode_lines (struct line_header *, const char *, bfd *,
1013 struct dwarf2_cu *, struct partial_symtab *);
1014
1015 static void dwarf2_start_subfile (char *, const char *, const char *);
1016
1017 static struct symbol *new_symbol (struct die_info *, struct type *,
1018 struct dwarf2_cu *);
1019
1020 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1021 struct dwarf2_cu *, struct symbol *);
1022
1023 static void dwarf2_const_value (struct attribute *, struct symbol *,
1024 struct dwarf2_cu *);
1025
1026 static void dwarf2_const_value_attr (struct attribute *attr,
1027 struct type *type,
1028 const char *name,
1029 struct obstack *obstack,
1030 struct dwarf2_cu *cu, long *value,
1031 gdb_byte **bytes,
1032 struct dwarf2_locexpr_baton **baton);
1033
1034 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1035
1036 static int need_gnat_info (struct dwarf2_cu *);
1037
1038 static struct type *die_descriptive_type (struct die_info *,
1039 struct dwarf2_cu *);
1040
1041 static void set_descriptive_type (struct type *, struct die_info *,
1042 struct dwarf2_cu *);
1043
1044 static struct type *die_containing_type (struct die_info *,
1045 struct dwarf2_cu *);
1046
1047 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1048 struct dwarf2_cu *);
1049
1050 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1051
1052 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1053
1054 static char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1055
1056 static char *typename_concat (struct obstack *obs, const char *prefix,
1057 const char *suffix, int physname,
1058 struct dwarf2_cu *cu);
1059
1060 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1061
1062 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1063
1064 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1065
1066 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1067
1068 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1069 struct dwarf2_cu *, struct partial_symtab *);
1070
1071 static int dwarf2_get_pc_bounds (struct die_info *,
1072 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1073 struct partial_symtab *);
1074
1075 static void get_scope_pc_bounds (struct die_info *,
1076 CORE_ADDR *, CORE_ADDR *,
1077 struct dwarf2_cu *);
1078
1079 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1080 CORE_ADDR, struct dwarf2_cu *);
1081
1082 static void dwarf2_add_field (struct field_info *, struct die_info *,
1083 struct dwarf2_cu *);
1084
1085 static void dwarf2_attach_fields_to_type (struct field_info *,
1086 struct type *, struct dwarf2_cu *);
1087
1088 static void dwarf2_add_member_fn (struct field_info *,
1089 struct die_info *, struct type *,
1090 struct dwarf2_cu *);
1091
1092 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1093 struct type *,
1094 struct dwarf2_cu *);
1095
1096 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1097
1098 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1099
1100 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1101
1102 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1103
1104 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1105
1106 static struct type *read_module_type (struct die_info *die,
1107 struct dwarf2_cu *cu);
1108
1109 static const char *namespace_name (struct die_info *die,
1110 int *is_anonymous, struct dwarf2_cu *);
1111
1112 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1113
1114 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1115
1116 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1117 struct dwarf2_cu *);
1118
1119 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1120
1121 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1122 gdb_byte *info_ptr,
1123 gdb_byte **new_info_ptr,
1124 struct die_info *parent);
1125
1126 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1127 gdb_byte *info_ptr,
1128 gdb_byte **new_info_ptr,
1129 struct die_info *parent);
1130
1131 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1132 gdb_byte *info_ptr,
1133 gdb_byte **new_info_ptr,
1134 struct die_info *parent);
1135
1136 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1137 struct die_info **, gdb_byte *,
1138 int *);
1139
1140 static void process_die (struct die_info *, struct dwarf2_cu *);
1141
1142 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1143 struct obstack *);
1144
1145 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1146
1147 static const char *dwarf2_full_name (char *name,
1148 struct die_info *die,
1149 struct dwarf2_cu *cu);
1150
1151 static struct die_info *dwarf2_extension (struct die_info *die,
1152 struct dwarf2_cu **);
1153
1154 static char *dwarf_tag_name (unsigned int);
1155
1156 static char *dwarf_attr_name (unsigned int);
1157
1158 static char *dwarf_form_name (unsigned int);
1159
1160 static char *dwarf_bool_name (unsigned int);
1161
1162 static char *dwarf_type_encoding_name (unsigned int);
1163
1164 #if 0
1165 static char *dwarf_cfi_name (unsigned int);
1166 #endif
1167
1168 static struct die_info *sibling_die (struct die_info *);
1169
1170 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1171
1172 static void dump_die_for_error (struct die_info *);
1173
1174 static void dump_die_1 (struct ui_file *, int level, int max_level,
1175 struct die_info *);
1176
1177 /*static*/ void dump_die (struct die_info *, int max_level);
1178
1179 static void store_in_ref_table (struct die_info *,
1180 struct dwarf2_cu *);
1181
1182 static int is_ref_attr (struct attribute *);
1183
1184 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1185
1186 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1187
1188 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1189 struct attribute *,
1190 struct dwarf2_cu **);
1191
1192 static struct die_info *follow_die_ref (struct die_info *,
1193 struct attribute *,
1194 struct dwarf2_cu **);
1195
1196 static struct die_info *follow_die_sig (struct die_info *,
1197 struct attribute *,
1198 struct dwarf2_cu **);
1199
1200 static void read_signatured_type_at_offset (struct objfile *objfile,
1201 unsigned int offset);
1202
1203 static void read_signatured_type (struct objfile *,
1204 struct signatured_type *type_sig);
1205
1206 /* memory allocation interface */
1207
1208 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1209
1210 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1211
1212 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1213
1214 static void initialize_cu_func_list (struct dwarf2_cu *);
1215
1216 static void add_to_cu_func_list (const char *, CORE_ADDR, CORE_ADDR,
1217 struct dwarf2_cu *);
1218
1219 static void dwarf_decode_macros (struct line_header *, unsigned int,
1220 char *, bfd *, struct dwarf2_cu *);
1221
1222 static int attr_form_is_block (struct attribute *);
1223
1224 static int attr_form_is_section_offset (struct attribute *);
1225
1226 static int attr_form_is_constant (struct attribute *);
1227
1228 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1229 struct dwarf2_loclist_baton *baton,
1230 struct attribute *attr);
1231
1232 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1233 struct symbol *sym,
1234 struct dwarf2_cu *cu);
1235
1236 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1237 struct abbrev_info *abbrev,
1238 struct dwarf2_cu *cu);
1239
1240 static void free_stack_comp_unit (void *);
1241
1242 static hashval_t partial_die_hash (const void *item);
1243
1244 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1245
1246 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1247 (unsigned int offset, struct objfile *objfile);
1248
1249 static struct dwarf2_per_cu_data *dwarf2_find_comp_unit
1250 (unsigned int offset, struct objfile *objfile);
1251
1252 static void init_one_comp_unit (struct dwarf2_cu *cu,
1253 struct objfile *objfile);
1254
1255 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1256 struct die_info *comp_unit_die);
1257
1258 static void free_one_comp_unit (void *);
1259
1260 static void free_cached_comp_units (void *);
1261
1262 static void age_cached_comp_units (void);
1263
1264 static void free_one_cached_comp_unit (void *);
1265
1266 static struct type *set_die_type (struct die_info *, struct type *,
1267 struct dwarf2_cu *);
1268
1269 static void create_all_comp_units (struct objfile *);
1270
1271 static int create_debug_types_hash_table (struct objfile *objfile);
1272
1273 static void load_full_comp_unit (struct dwarf2_per_cu_data *,
1274 struct objfile *);
1275
1276 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1277
1278 static void dwarf2_add_dependence (struct dwarf2_cu *,
1279 struct dwarf2_per_cu_data *);
1280
1281 static void dwarf2_mark (struct dwarf2_cu *);
1282
1283 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1284
1285 static struct type *get_die_type_at_offset (unsigned int,
1286 struct dwarf2_per_cu_data *per_cu);
1287
1288 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1289
1290 static void dwarf2_release_queue (void *dummy);
1291
1292 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu,
1293 struct objfile *objfile);
1294
1295 static void process_queue (struct objfile *objfile);
1296
1297 static void find_file_and_directory (struct die_info *die,
1298 struct dwarf2_cu *cu,
1299 char **name, char **comp_dir);
1300
1301 static char *file_full_name (int file, struct line_header *lh,
1302 const char *comp_dir);
1303
1304 static gdb_byte *partial_read_comp_unit_head (struct comp_unit_head *header,
1305 gdb_byte *info_ptr,
1306 gdb_byte *buffer,
1307 unsigned int buffer_size,
1308 bfd *abfd);
1309
1310 static void init_cu_die_reader (struct die_reader_specs *reader,
1311 struct dwarf2_cu *cu);
1312
1313 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1314
1315 #if WORDS_BIGENDIAN
1316
1317 /* Convert VALUE between big- and little-endian. */
1318 static offset_type
1319 byte_swap (offset_type value)
1320 {
1321 offset_type result;
1322
1323 result = (value & 0xff) << 24;
1324 result |= (value & 0xff00) << 8;
1325 result |= (value & 0xff0000) >> 8;
1326 result |= (value & 0xff000000) >> 24;
1327 return result;
1328 }
1329
1330 #define MAYBE_SWAP(V) byte_swap (V)
1331
1332 #else
1333 #define MAYBE_SWAP(V) (V)
1334 #endif /* WORDS_BIGENDIAN */
1335
1336 /* The suffix for an index file. */
1337 #define INDEX_SUFFIX ".gdb-index"
1338
1339 static const char *dwarf2_physname (char *name, struct die_info *die,
1340 struct dwarf2_cu *cu);
1341
1342 /* Try to locate the sections we need for DWARF 2 debugging
1343 information and return true if we have enough to do something.
1344 NAMES points to the dwarf2 section names, or is NULL if the standard
1345 ELF names are used. */
1346
1347 int
1348 dwarf2_has_info (struct objfile *objfile,
1349 const struct dwarf2_debug_sections *names)
1350 {
1351 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1352 if (!dwarf2_per_objfile)
1353 {
1354 /* Initialize per-objfile state. */
1355 struct dwarf2_per_objfile *data
1356 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1357
1358 memset (data, 0, sizeof (*data));
1359 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1360 dwarf2_per_objfile = data;
1361
1362 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1363 (void *) names);
1364 dwarf2_per_objfile->objfile = objfile;
1365 }
1366 return (dwarf2_per_objfile->info.asection != NULL
1367 && dwarf2_per_objfile->abbrev.asection != NULL);
1368 }
1369
1370 /* When loading sections, we look either for uncompressed section or for
1371 compressed section names. */
1372
1373 static int
1374 section_is_p (const char *section_name,
1375 const struct dwarf2_section_names *names)
1376 {
1377 if (names->normal != NULL
1378 && strcmp (section_name, names->normal) == 0)
1379 return 1;
1380 if (names->compressed != NULL
1381 && strcmp (section_name, names->compressed) == 0)
1382 return 1;
1383 return 0;
1384 }
1385
1386 /* This function is mapped across the sections and remembers the
1387 offset and size of each of the debugging sections we are interested
1388 in. */
1389
1390 static void
1391 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1392 {
1393 const struct dwarf2_debug_sections *names;
1394
1395 if (vnames == NULL)
1396 names = &dwarf2_elf_names;
1397 else
1398 names = (const struct dwarf2_debug_sections *) vnames;
1399
1400 if (section_is_p (sectp->name, &names->info))
1401 {
1402 dwarf2_per_objfile->info.asection = sectp;
1403 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1404 }
1405 else if (section_is_p (sectp->name, &names->abbrev))
1406 {
1407 dwarf2_per_objfile->abbrev.asection = sectp;
1408 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1409 }
1410 else if (section_is_p (sectp->name, &names->line))
1411 {
1412 dwarf2_per_objfile->line.asection = sectp;
1413 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1414 }
1415 else if (section_is_p (sectp->name, &names->loc))
1416 {
1417 dwarf2_per_objfile->loc.asection = sectp;
1418 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1419 }
1420 else if (section_is_p (sectp->name, &names->macinfo))
1421 {
1422 dwarf2_per_objfile->macinfo.asection = sectp;
1423 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1424 }
1425 else if (section_is_p (sectp->name, &names->str))
1426 {
1427 dwarf2_per_objfile->str.asection = sectp;
1428 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1429 }
1430 else if (section_is_p (sectp->name, &names->frame))
1431 {
1432 dwarf2_per_objfile->frame.asection = sectp;
1433 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1434 }
1435 else if (section_is_p (sectp->name, &names->eh_frame))
1436 {
1437 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1438
1439 if (aflag & SEC_HAS_CONTENTS)
1440 {
1441 dwarf2_per_objfile->eh_frame.asection = sectp;
1442 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1443 }
1444 }
1445 else if (section_is_p (sectp->name, &names->ranges))
1446 {
1447 dwarf2_per_objfile->ranges.asection = sectp;
1448 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1449 }
1450 else if (section_is_p (sectp->name, &names->types))
1451 {
1452 dwarf2_per_objfile->types.asection = sectp;
1453 dwarf2_per_objfile->types.size = bfd_get_section_size (sectp);
1454 }
1455 else if (section_is_p (sectp->name, &names->gdb_index))
1456 {
1457 dwarf2_per_objfile->gdb_index.asection = sectp;
1458 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1459 }
1460
1461 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1462 && bfd_section_vma (abfd, sectp) == 0)
1463 dwarf2_per_objfile->has_section_at_zero = 1;
1464 }
1465
1466 /* Decompress a section that was compressed using zlib. Store the
1467 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1468
1469 static void
1470 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1471 gdb_byte **outbuf, bfd_size_type *outsize)
1472 {
1473 bfd *abfd = objfile->obfd;
1474 #ifndef HAVE_ZLIB_H
1475 error (_("Support for zlib-compressed DWARF data (from '%s') "
1476 "is disabled in this copy of GDB"),
1477 bfd_get_filename (abfd));
1478 #else
1479 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1480 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1481 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1482 bfd_size_type uncompressed_size;
1483 gdb_byte *uncompressed_buffer;
1484 z_stream strm;
1485 int rc;
1486 int header_size = 12;
1487
1488 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1489 || bfd_bread (compressed_buffer,
1490 compressed_size, abfd) != compressed_size)
1491 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1492 bfd_get_filename (abfd));
1493
1494 /* Read the zlib header. In this case, it should be "ZLIB" followed
1495 by the uncompressed section size, 8 bytes in big-endian order. */
1496 if (compressed_size < header_size
1497 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1498 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1499 bfd_get_filename (abfd));
1500 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1501 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1502 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1503 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1504 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1505 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1506 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1507 uncompressed_size += compressed_buffer[11];
1508
1509 /* It is possible the section consists of several compressed
1510 buffers concatenated together, so we uncompress in a loop. */
1511 strm.zalloc = NULL;
1512 strm.zfree = NULL;
1513 strm.opaque = NULL;
1514 strm.avail_in = compressed_size - header_size;
1515 strm.next_in = (Bytef*) compressed_buffer + header_size;
1516 strm.avail_out = uncompressed_size;
1517 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1518 uncompressed_size);
1519 rc = inflateInit (&strm);
1520 while (strm.avail_in > 0)
1521 {
1522 if (rc != Z_OK)
1523 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1524 bfd_get_filename (abfd), rc);
1525 strm.next_out = ((Bytef*) uncompressed_buffer
1526 + (uncompressed_size - strm.avail_out));
1527 rc = inflate (&strm, Z_FINISH);
1528 if (rc != Z_STREAM_END)
1529 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1530 bfd_get_filename (abfd), rc);
1531 rc = inflateReset (&strm);
1532 }
1533 rc = inflateEnd (&strm);
1534 if (rc != Z_OK
1535 || strm.avail_out != 0)
1536 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1537 bfd_get_filename (abfd), rc);
1538
1539 do_cleanups (cleanup);
1540 *outbuf = uncompressed_buffer;
1541 *outsize = uncompressed_size;
1542 #endif
1543 }
1544
1545 /* A helper function that decides whether a section is empty. */
1546
1547 static int
1548 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1549 {
1550 return info->asection == NULL || info->size == 0;
1551 }
1552
1553 /* Read the contents of the section SECTP from object file specified by
1554 OBJFILE, store info about the section into INFO.
1555 If the section is compressed, uncompress it before returning. */
1556
1557 static void
1558 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1559 {
1560 bfd *abfd = objfile->obfd;
1561 asection *sectp = info->asection;
1562 gdb_byte *buf, *retbuf;
1563 unsigned char header[4];
1564
1565 if (info->readin)
1566 return;
1567 info->buffer = NULL;
1568 info->map_addr = NULL;
1569 info->readin = 1;
1570
1571 if (dwarf2_section_empty_p (info))
1572 return;
1573
1574 /* Check if the file has a 4-byte header indicating compression. */
1575 if (info->size > sizeof (header)
1576 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1577 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1578 {
1579 /* Upon decompression, update the buffer and its size. */
1580 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1581 {
1582 zlib_decompress_section (objfile, sectp, &info->buffer,
1583 &info->size);
1584 return;
1585 }
1586 }
1587
1588 #ifdef HAVE_MMAP
1589 if (pagesize == 0)
1590 pagesize = getpagesize ();
1591
1592 /* Only try to mmap sections which are large enough: we don't want to
1593 waste space due to fragmentation. Also, only try mmap for sections
1594 without relocations. */
1595
1596 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1597 {
1598 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1599 MAP_PRIVATE, sectp->filepos,
1600 &info->map_addr, &info->map_len);
1601
1602 if ((caddr_t)info->buffer != MAP_FAILED)
1603 {
1604 #if HAVE_POSIX_MADVISE
1605 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1606 #endif
1607 return;
1608 }
1609 }
1610 #endif
1611
1612 /* If we get here, we are a normal, not-compressed section. */
1613 info->buffer = buf
1614 = obstack_alloc (&objfile->objfile_obstack, info->size);
1615
1616 /* When debugging .o files, we may need to apply relocations; see
1617 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1618 We never compress sections in .o files, so we only need to
1619 try this when the section is not compressed. */
1620 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1621 if (retbuf != NULL)
1622 {
1623 info->buffer = retbuf;
1624 return;
1625 }
1626
1627 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1628 || bfd_bread (buf, info->size, abfd) != info->size)
1629 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1630 bfd_get_filename (abfd));
1631 }
1632
1633 /* A helper function that returns the size of a section in a safe way.
1634 If you are positive that the section has been read before using the
1635 size, then it is safe to refer to the dwarf2_section_info object's
1636 "size" field directly. In other cases, you must call this
1637 function, because for compressed sections the size field is not set
1638 correctly until the section has been read. */
1639
1640 static bfd_size_type
1641 dwarf2_section_size (struct objfile *objfile,
1642 struct dwarf2_section_info *info)
1643 {
1644 if (!info->readin)
1645 dwarf2_read_section (objfile, info);
1646 return info->size;
1647 }
1648
1649 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1650 SECTION_NAME. */
1651
1652 void
1653 dwarf2_get_section_info (struct objfile *objfile,
1654 enum dwarf2_section_enum sect,
1655 asection **sectp, gdb_byte **bufp,
1656 bfd_size_type *sizep)
1657 {
1658 struct dwarf2_per_objfile *data
1659 = objfile_data (objfile, dwarf2_objfile_data_key);
1660 struct dwarf2_section_info *info;
1661
1662 /* We may see an objfile without any DWARF, in which case we just
1663 return nothing. */
1664 if (data == NULL)
1665 {
1666 *sectp = NULL;
1667 *bufp = NULL;
1668 *sizep = 0;
1669 return;
1670 }
1671 switch (sect)
1672 {
1673 case DWARF2_DEBUG_FRAME:
1674 info = &data->frame;
1675 break;
1676 case DWARF2_EH_FRAME:
1677 info = &data->eh_frame;
1678 break;
1679 default:
1680 gdb_assert_not_reached ("unexpected section");
1681 }
1682
1683 dwarf2_read_section (objfile, info);
1684
1685 *sectp = info->asection;
1686 *bufp = info->buffer;
1687 *sizep = info->size;
1688 }
1689
1690 \f
1691 /* DWARF quick_symbols_functions support. */
1692
1693 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1694 unique line tables, so we maintain a separate table of all .debug_line
1695 derived entries to support the sharing.
1696 All the quick functions need is the list of file names. We discard the
1697 line_header when we're done and don't need to record it here. */
1698 struct quick_file_names
1699 {
1700 /* The offset in .debug_line of the line table. We hash on this. */
1701 unsigned int offset;
1702
1703 /* The number of entries in file_names, real_names. */
1704 unsigned int num_file_names;
1705
1706 /* The file names from the line table, after being run through
1707 file_full_name. */
1708 const char **file_names;
1709
1710 /* The file names from the line table after being run through
1711 gdb_realpath. These are computed lazily. */
1712 const char **real_names;
1713 };
1714
1715 /* When using the index (and thus not using psymtabs), each CU has an
1716 object of this type. This is used to hold information needed by
1717 the various "quick" methods. */
1718 struct dwarf2_per_cu_quick_data
1719 {
1720 /* The file table. This can be NULL if there was no file table
1721 or it's currently not read in.
1722 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1723 struct quick_file_names *file_names;
1724
1725 /* The corresponding symbol table. This is NULL if symbols for this
1726 CU have not yet been read. */
1727 struct symtab *symtab;
1728
1729 /* A temporary mark bit used when iterating over all CUs in
1730 expand_symtabs_matching. */
1731 unsigned int mark : 1;
1732
1733 /* True if we've tried to read the file table and found there isn't one.
1734 There will be no point in trying to read it again next time. */
1735 unsigned int no_file_data : 1;
1736 };
1737
1738 /* Hash function for a quick_file_names. */
1739
1740 static hashval_t
1741 hash_file_name_entry (const void *e)
1742 {
1743 const struct quick_file_names *file_data = e;
1744
1745 return file_data->offset;
1746 }
1747
1748 /* Equality function for a quick_file_names. */
1749
1750 static int
1751 eq_file_name_entry (const void *a, const void *b)
1752 {
1753 const struct quick_file_names *ea = a;
1754 const struct quick_file_names *eb = b;
1755
1756 return ea->offset == eb->offset;
1757 }
1758
1759 /* Delete function for a quick_file_names. */
1760
1761 static void
1762 delete_file_name_entry (void *e)
1763 {
1764 struct quick_file_names *file_data = e;
1765 int i;
1766
1767 for (i = 0; i < file_data->num_file_names; ++i)
1768 {
1769 xfree ((void*) file_data->file_names[i]);
1770 if (file_data->real_names)
1771 xfree ((void*) file_data->real_names[i]);
1772 }
1773
1774 /* The space for the struct itself lives on objfile_obstack,
1775 so we don't free it here. */
1776 }
1777
1778 /* Create a quick_file_names hash table. */
1779
1780 static htab_t
1781 create_quick_file_names_table (unsigned int nr_initial_entries)
1782 {
1783 return htab_create_alloc (nr_initial_entries,
1784 hash_file_name_entry, eq_file_name_entry,
1785 delete_file_name_entry, xcalloc, xfree);
1786 }
1787
1788 /* Read in the symbols for PER_CU. OBJFILE is the objfile from which
1789 this CU came. */
1790
1791 static void
1792 dw2_do_instantiate_symtab (struct objfile *objfile,
1793 struct dwarf2_per_cu_data *per_cu)
1794 {
1795 struct cleanup *back_to;
1796
1797 back_to = make_cleanup (dwarf2_release_queue, NULL);
1798
1799 queue_comp_unit (per_cu, objfile);
1800
1801 if (per_cu->from_debug_types)
1802 read_signatured_type_at_offset (objfile, per_cu->offset);
1803 else
1804 load_full_comp_unit (per_cu, objfile);
1805
1806 process_queue (objfile);
1807
1808 /* Age the cache, releasing compilation units that have not
1809 been used recently. */
1810 age_cached_comp_units ();
1811
1812 do_cleanups (back_to);
1813 }
1814
1815 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1816 the objfile from which this CU came. Returns the resulting symbol
1817 table. */
1818
1819 static struct symtab *
1820 dw2_instantiate_symtab (struct objfile *objfile,
1821 struct dwarf2_per_cu_data *per_cu)
1822 {
1823 if (!per_cu->v.quick->symtab)
1824 {
1825 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1826 increment_reading_symtab ();
1827 dw2_do_instantiate_symtab (objfile, per_cu);
1828 do_cleanups (back_to);
1829 }
1830 return per_cu->v.quick->symtab;
1831 }
1832
1833 /* Return the CU given its index. */
1834
1835 static struct dwarf2_per_cu_data *
1836 dw2_get_cu (int index)
1837 {
1838 if (index >= dwarf2_per_objfile->n_comp_units)
1839 {
1840 index -= dwarf2_per_objfile->n_comp_units;
1841 return dwarf2_per_objfile->type_comp_units[index];
1842 }
1843 return dwarf2_per_objfile->all_comp_units[index];
1844 }
1845
1846 /* A helper function that knows how to read a 64-bit value in a way
1847 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1848 otherwise. */
1849
1850 static int
1851 extract_cu_value (const char *bytes, ULONGEST *result)
1852 {
1853 if (sizeof (ULONGEST) < 8)
1854 {
1855 int i;
1856
1857 /* Ignore the upper 4 bytes if they are all zero. */
1858 for (i = 0; i < 4; ++i)
1859 if (bytes[i + 4] != 0)
1860 return 0;
1861
1862 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1863 }
1864 else
1865 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1866 return 1;
1867 }
1868
1869 /* Read the CU list from the mapped index, and use it to create all
1870 the CU objects for this objfile. Return 0 if something went wrong,
1871 1 if everything went ok. */
1872
1873 static int
1874 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1875 offset_type cu_list_elements)
1876 {
1877 offset_type i;
1878
1879 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1880 dwarf2_per_objfile->all_comp_units
1881 = obstack_alloc (&objfile->objfile_obstack,
1882 dwarf2_per_objfile->n_comp_units
1883 * sizeof (struct dwarf2_per_cu_data *));
1884
1885 for (i = 0; i < cu_list_elements; i += 2)
1886 {
1887 struct dwarf2_per_cu_data *the_cu;
1888 ULONGEST offset, length;
1889
1890 if (!extract_cu_value (cu_list, &offset)
1891 || !extract_cu_value (cu_list + 8, &length))
1892 return 0;
1893 cu_list += 2 * 8;
1894
1895 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1896 struct dwarf2_per_cu_data);
1897 the_cu->offset = offset;
1898 the_cu->length = length;
1899 the_cu->objfile = objfile;
1900 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1901 struct dwarf2_per_cu_quick_data);
1902 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1903 }
1904
1905 return 1;
1906 }
1907
1908 /* Create the signatured type hash table from the index. */
1909
1910 static int
1911 create_signatured_type_table_from_index (struct objfile *objfile,
1912 const gdb_byte *bytes,
1913 offset_type elements)
1914 {
1915 offset_type i;
1916 htab_t sig_types_hash;
1917
1918 dwarf2_per_objfile->n_type_comp_units = elements / 3;
1919 dwarf2_per_objfile->type_comp_units
1920 = obstack_alloc (&objfile->objfile_obstack,
1921 dwarf2_per_objfile->n_type_comp_units
1922 * sizeof (struct dwarf2_per_cu_data *));
1923
1924 sig_types_hash = allocate_signatured_type_table (objfile);
1925
1926 for (i = 0; i < elements; i += 3)
1927 {
1928 struct signatured_type *type_sig;
1929 ULONGEST offset, type_offset, signature;
1930 void **slot;
1931
1932 if (!extract_cu_value (bytes, &offset)
1933 || !extract_cu_value (bytes + 8, &type_offset))
1934 return 0;
1935 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1936 bytes += 3 * 8;
1937
1938 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1939 struct signatured_type);
1940 type_sig->signature = signature;
1941 type_sig->type_offset = type_offset;
1942 type_sig->per_cu.from_debug_types = 1;
1943 type_sig->per_cu.offset = offset;
1944 type_sig->per_cu.objfile = objfile;
1945 type_sig->per_cu.v.quick
1946 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1947 struct dwarf2_per_cu_quick_data);
1948
1949 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1950 *slot = type_sig;
1951
1952 dwarf2_per_objfile->type_comp_units[i / 3] = &type_sig->per_cu;
1953 }
1954
1955 dwarf2_per_objfile->signatured_types = sig_types_hash;
1956
1957 return 1;
1958 }
1959
1960 /* Read the address map data from the mapped index, and use it to
1961 populate the objfile's psymtabs_addrmap. */
1962
1963 static void
1964 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1965 {
1966 const gdb_byte *iter, *end;
1967 struct obstack temp_obstack;
1968 struct addrmap *mutable_map;
1969 struct cleanup *cleanup;
1970 CORE_ADDR baseaddr;
1971
1972 obstack_init (&temp_obstack);
1973 cleanup = make_cleanup_obstack_free (&temp_obstack);
1974 mutable_map = addrmap_create_mutable (&temp_obstack);
1975
1976 iter = index->address_table;
1977 end = iter + index->address_table_size;
1978
1979 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1980
1981 while (iter < end)
1982 {
1983 ULONGEST hi, lo, cu_index;
1984 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985 iter += 8;
1986 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1987 iter += 8;
1988 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1989 iter += 4;
1990
1991 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1992 dw2_get_cu (cu_index));
1993 }
1994
1995 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1996 &objfile->objfile_obstack);
1997 do_cleanups (cleanup);
1998 }
1999
2000 /* The hash function for strings in the mapped index. This is the same as
2001 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2002 implementation. This is necessary because the hash function is tied to the
2003 format of the mapped index file. The hash values do not have to match with
2004 SYMBOL_HASH_NEXT.
2005
2006 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2007
2008 static hashval_t
2009 mapped_index_string_hash (int index_version, const void *p)
2010 {
2011 const unsigned char *str = (const unsigned char *) p;
2012 hashval_t r = 0;
2013 unsigned char c;
2014
2015 while ((c = *str++) != 0)
2016 {
2017 if (index_version >= 5)
2018 c = tolower (c);
2019 r = r * 67 + c - 113;
2020 }
2021
2022 return r;
2023 }
2024
2025 /* Find a slot in the mapped index INDEX for the object named NAME.
2026 If NAME is found, set *VEC_OUT to point to the CU vector in the
2027 constant pool and return 1. If NAME cannot be found, return 0. */
2028
2029 static int
2030 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2031 offset_type **vec_out)
2032 {
2033 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2034 offset_type hash;
2035 offset_type slot, step;
2036 int (*cmp) (const char *, const char *);
2037
2038 if (current_language->la_language == language_cplus
2039 || current_language->la_language == language_java
2040 || current_language->la_language == language_fortran)
2041 {
2042 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2043 not contain any. */
2044 const char *paren = strchr (name, '(');
2045
2046 if (paren)
2047 {
2048 char *dup;
2049
2050 dup = xmalloc (paren - name + 1);
2051 memcpy (dup, name, paren - name);
2052 dup[paren - name] = 0;
2053
2054 make_cleanup (xfree, dup);
2055 name = dup;
2056 }
2057 }
2058
2059 /* Index version 4 did not support case insensitive searches. But the
2060 indexes for case insensitive languages are built in lowercase, therefore
2061 simulate our NAME being searched is also lowercased. */
2062 hash = mapped_index_string_hash ((index->version == 4
2063 && case_sensitivity == case_sensitive_off
2064 ? 5 : index->version),
2065 name);
2066
2067 slot = hash & (index->symbol_table_slots - 1);
2068 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2069 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2070
2071 for (;;)
2072 {
2073 /* Convert a slot number to an offset into the table. */
2074 offset_type i = 2 * slot;
2075 const char *str;
2076 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2077 {
2078 do_cleanups (back_to);
2079 return 0;
2080 }
2081
2082 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2083 if (!cmp (name, str))
2084 {
2085 *vec_out = (offset_type *) (index->constant_pool
2086 + MAYBE_SWAP (index->symbol_table[i + 1]));
2087 do_cleanups (back_to);
2088 return 1;
2089 }
2090
2091 slot = (slot + step) & (index->symbol_table_slots - 1);
2092 }
2093 }
2094
2095 /* Read the index file. If everything went ok, initialize the "quick"
2096 elements of all the CUs and return 1. Otherwise, return 0. */
2097
2098 static int
2099 dwarf2_read_index (struct objfile *objfile)
2100 {
2101 char *addr;
2102 struct mapped_index *map;
2103 offset_type *metadata;
2104 const gdb_byte *cu_list;
2105 const gdb_byte *types_list = NULL;
2106 offset_type version, cu_list_elements;
2107 offset_type types_list_elements = 0;
2108 int i;
2109
2110 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2111 return 0;
2112
2113 /* Older elfutils strip versions could keep the section in the main
2114 executable while splitting it for the separate debug info file. */
2115 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2116 & SEC_HAS_CONTENTS) == 0)
2117 return 0;
2118
2119 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2120
2121 addr = dwarf2_per_objfile->gdb_index.buffer;
2122 /* Version check. */
2123 version = MAYBE_SWAP (*(offset_type *) addr);
2124 /* Versions earlier than 3 emitted every copy of a psymbol. This
2125 causes the index to behave very poorly for certain requests. Version 3
2126 contained incomplete addrmap. So, it seems better to just ignore such
2127 indices. Index version 4 uses a different hash function than index
2128 version 5 and later. */
2129 if (version < 4)
2130 return 0;
2131 /* Indexes with higher version than the one supported by GDB may be no
2132 longer backward compatible. */
2133 if (version > 5)
2134 return 0;
2135
2136 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2137 map->version = version;
2138 map->total_size = dwarf2_per_objfile->gdb_index.size;
2139
2140 metadata = (offset_type *) (addr + sizeof (offset_type));
2141
2142 i = 0;
2143 cu_list = addr + MAYBE_SWAP (metadata[i]);
2144 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2145 / 8);
2146 ++i;
2147
2148 types_list = addr + MAYBE_SWAP (metadata[i]);
2149 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2150 - MAYBE_SWAP (metadata[i]))
2151 / 8);
2152 ++i;
2153
2154 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2155 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2156 - MAYBE_SWAP (metadata[i]));
2157 ++i;
2158
2159 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2160 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2161 - MAYBE_SWAP (metadata[i]))
2162 / (2 * sizeof (offset_type)));
2163 ++i;
2164
2165 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2166
2167 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2168 return 0;
2169
2170 if (types_list_elements
2171 && !create_signatured_type_table_from_index (objfile, types_list,
2172 types_list_elements))
2173 return 0;
2174
2175 create_addrmap_from_index (objfile, map);
2176
2177 dwarf2_per_objfile->index_table = map;
2178 dwarf2_per_objfile->using_index = 1;
2179 dwarf2_per_objfile->quick_file_names_table =
2180 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2181
2182 return 1;
2183 }
2184
2185 /* A helper for the "quick" functions which sets the global
2186 dwarf2_per_objfile according to OBJFILE. */
2187
2188 static void
2189 dw2_setup (struct objfile *objfile)
2190 {
2191 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2192 gdb_assert (dwarf2_per_objfile);
2193 }
2194
2195 /* A helper for the "quick" functions which attempts to read the line
2196 table for THIS_CU. */
2197
2198 static struct quick_file_names *
2199 dw2_get_file_names (struct objfile *objfile,
2200 struct dwarf2_per_cu_data *this_cu)
2201 {
2202 bfd *abfd = objfile->obfd;
2203 struct line_header *lh;
2204 struct attribute *attr;
2205 struct cleanup *cleanups;
2206 struct die_info *comp_unit_die;
2207 struct dwarf2_section_info* sec;
2208 gdb_byte *beg_of_comp_unit, *info_ptr, *buffer;
2209 int has_children, i;
2210 struct dwarf2_cu cu;
2211 unsigned int bytes_read, buffer_size;
2212 struct die_reader_specs reader_specs;
2213 char *name, *comp_dir;
2214 void **slot;
2215 struct quick_file_names *qfn;
2216 unsigned int line_offset;
2217
2218 if (this_cu->v.quick->file_names != NULL)
2219 return this_cu->v.quick->file_names;
2220 /* If we know there is no line data, no point in looking again. */
2221 if (this_cu->v.quick->no_file_data)
2222 return NULL;
2223
2224 init_one_comp_unit (&cu, objfile);
2225 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2226
2227 if (this_cu->from_debug_types)
2228 sec = &dwarf2_per_objfile->types;
2229 else
2230 sec = &dwarf2_per_objfile->info;
2231 dwarf2_read_section (objfile, sec);
2232 buffer_size = sec->size;
2233 buffer = sec->buffer;
2234 info_ptr = buffer + this_cu->offset;
2235 beg_of_comp_unit = info_ptr;
2236
2237 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
2238 buffer, buffer_size,
2239 abfd);
2240
2241 /* Complete the cu_header. */
2242 cu.header.offset = beg_of_comp_unit - buffer;
2243 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
2244
2245 this_cu->cu = &cu;
2246 cu.per_cu = this_cu;
2247
2248 dwarf2_read_abbrevs (abfd, &cu);
2249 make_cleanup (dwarf2_free_abbrev_table, &cu);
2250
2251 if (this_cu->from_debug_types)
2252 info_ptr += 8 /*signature*/ + cu.header.offset_size;
2253 init_cu_die_reader (&reader_specs, &cu);
2254 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2255 &has_children);
2256
2257 lh = NULL;
2258 slot = NULL;
2259 line_offset = 0;
2260 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2261 if (attr)
2262 {
2263 struct quick_file_names find_entry;
2264
2265 line_offset = DW_UNSND (attr);
2266
2267 /* We may have already read in this line header (TU line header sharing).
2268 If we have we're done. */
2269 find_entry.offset = line_offset;
2270 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2271 &find_entry, INSERT);
2272 if (*slot != NULL)
2273 {
2274 do_cleanups (cleanups);
2275 this_cu->v.quick->file_names = *slot;
2276 return *slot;
2277 }
2278
2279 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2280 }
2281 if (lh == NULL)
2282 {
2283 do_cleanups (cleanups);
2284 this_cu->v.quick->no_file_data = 1;
2285 return NULL;
2286 }
2287
2288 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2289 qfn->offset = line_offset;
2290 gdb_assert (slot != NULL);
2291 *slot = qfn;
2292
2293 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2294
2295 qfn->num_file_names = lh->num_file_names;
2296 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2297 lh->num_file_names * sizeof (char *));
2298 for (i = 0; i < lh->num_file_names; ++i)
2299 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2300 qfn->real_names = NULL;
2301
2302 free_line_header (lh);
2303 do_cleanups (cleanups);
2304
2305 this_cu->v.quick->file_names = qfn;
2306 return qfn;
2307 }
2308
2309 /* A helper for the "quick" functions which computes and caches the
2310 real path for a given file name from the line table. */
2311
2312 static const char *
2313 dw2_get_real_path (struct objfile *objfile,
2314 struct quick_file_names *qfn, int index)
2315 {
2316 if (qfn->real_names == NULL)
2317 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2318 qfn->num_file_names, sizeof (char *));
2319
2320 if (qfn->real_names[index] == NULL)
2321 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2322
2323 return qfn->real_names[index];
2324 }
2325
2326 static struct symtab *
2327 dw2_find_last_source_symtab (struct objfile *objfile)
2328 {
2329 int index;
2330
2331 dw2_setup (objfile);
2332 index = dwarf2_per_objfile->n_comp_units - 1;
2333 return dw2_instantiate_symtab (objfile, dw2_get_cu (index));
2334 }
2335
2336 /* Traversal function for dw2_forget_cached_source_info. */
2337
2338 static int
2339 dw2_free_cached_file_names (void **slot, void *info)
2340 {
2341 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2342
2343 if (file_data->real_names)
2344 {
2345 int i;
2346
2347 for (i = 0; i < file_data->num_file_names; ++i)
2348 {
2349 xfree ((void*) file_data->real_names[i]);
2350 file_data->real_names[i] = NULL;
2351 }
2352 }
2353
2354 return 1;
2355 }
2356
2357 static void
2358 dw2_forget_cached_source_info (struct objfile *objfile)
2359 {
2360 dw2_setup (objfile);
2361
2362 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2363 dw2_free_cached_file_names, NULL);
2364 }
2365
2366 static int
2367 dw2_lookup_symtab (struct objfile *objfile, const char *name,
2368 const char *full_path, const char *real_path,
2369 struct symtab **result)
2370 {
2371 int i;
2372 int check_basename = lbasename (name) == name;
2373 struct dwarf2_per_cu_data *base_cu = NULL;
2374
2375 dw2_setup (objfile);
2376
2377 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2378 + dwarf2_per_objfile->n_type_comp_units); ++i)
2379 {
2380 int j;
2381 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2382 struct quick_file_names *file_data;
2383
2384 if (per_cu->v.quick->symtab)
2385 continue;
2386
2387 file_data = dw2_get_file_names (objfile, per_cu);
2388 if (file_data == NULL)
2389 continue;
2390
2391 for (j = 0; j < file_data->num_file_names; ++j)
2392 {
2393 const char *this_name = file_data->file_names[j];
2394
2395 if (FILENAME_CMP (name, this_name) == 0)
2396 {
2397 *result = dw2_instantiate_symtab (objfile, per_cu);
2398 return 1;
2399 }
2400
2401 if (check_basename && ! base_cu
2402 && FILENAME_CMP (lbasename (this_name), name) == 0)
2403 base_cu = per_cu;
2404
2405 if (full_path != NULL)
2406 {
2407 const char *this_real_name = dw2_get_real_path (objfile,
2408 file_data, j);
2409
2410 if (this_real_name != NULL
2411 && FILENAME_CMP (full_path, this_real_name) == 0)
2412 {
2413 *result = dw2_instantiate_symtab (objfile, per_cu);
2414 return 1;
2415 }
2416 }
2417
2418 if (real_path != NULL)
2419 {
2420 const char *this_real_name = dw2_get_real_path (objfile,
2421 file_data, j);
2422
2423 if (this_real_name != NULL
2424 && FILENAME_CMP (real_path, this_real_name) == 0)
2425 {
2426 *result = dw2_instantiate_symtab (objfile, per_cu);
2427 return 1;
2428 }
2429 }
2430 }
2431 }
2432
2433 if (base_cu)
2434 {
2435 *result = dw2_instantiate_symtab (objfile, base_cu);
2436 return 1;
2437 }
2438
2439 return 0;
2440 }
2441
2442 static struct symtab *
2443 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2444 const char *name, domain_enum domain)
2445 {
2446 /* We do all the work in the pre_expand_symtabs_matching hook
2447 instead. */
2448 return NULL;
2449 }
2450
2451 /* A helper function that expands all symtabs that hold an object
2452 named NAME. */
2453
2454 static void
2455 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2456 {
2457 dw2_setup (objfile);
2458
2459 /* index_table is NULL if OBJF_READNOW. */
2460 if (dwarf2_per_objfile->index_table)
2461 {
2462 offset_type *vec;
2463
2464 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2465 name, &vec))
2466 {
2467 offset_type i, len = MAYBE_SWAP (*vec);
2468 for (i = 0; i < len; ++i)
2469 {
2470 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2471 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2472
2473 dw2_instantiate_symtab (objfile, per_cu);
2474 }
2475 }
2476 }
2477 }
2478
2479 static void
2480 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2481 enum block_enum block_kind, const char *name,
2482 domain_enum domain)
2483 {
2484 dw2_do_expand_symtabs_matching (objfile, name);
2485 }
2486
2487 static void
2488 dw2_print_stats (struct objfile *objfile)
2489 {
2490 int i, count;
2491
2492 dw2_setup (objfile);
2493 count = 0;
2494 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2495 + dwarf2_per_objfile->n_type_comp_units); ++i)
2496 {
2497 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2498
2499 if (!per_cu->v.quick->symtab)
2500 ++count;
2501 }
2502 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2503 }
2504
2505 static void
2506 dw2_dump (struct objfile *objfile)
2507 {
2508 /* Nothing worth printing. */
2509 }
2510
2511 static void
2512 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2513 struct section_offsets *delta)
2514 {
2515 /* There's nothing to relocate here. */
2516 }
2517
2518 static void
2519 dw2_expand_symtabs_for_function (struct objfile *objfile,
2520 const char *func_name)
2521 {
2522 dw2_do_expand_symtabs_matching (objfile, func_name);
2523 }
2524
2525 static void
2526 dw2_expand_all_symtabs (struct objfile *objfile)
2527 {
2528 int i;
2529
2530 dw2_setup (objfile);
2531
2532 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2533 + dwarf2_per_objfile->n_type_comp_units); ++i)
2534 {
2535 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2536
2537 dw2_instantiate_symtab (objfile, per_cu);
2538 }
2539 }
2540
2541 static void
2542 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2543 const char *filename)
2544 {
2545 int i;
2546
2547 dw2_setup (objfile);
2548
2549 /* We don't need to consider type units here.
2550 This is only called for examining code, e.g. expand_line_sal.
2551 There can be an order of magnitude (or more) more type units
2552 than comp units, and we avoid them if we can. */
2553
2554 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2555 {
2556 int j;
2557 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2558 struct quick_file_names *file_data;
2559
2560 if (per_cu->v.quick->symtab)
2561 continue;
2562
2563 file_data = dw2_get_file_names (objfile, per_cu);
2564 if (file_data == NULL)
2565 continue;
2566
2567 for (j = 0; j < file_data->num_file_names; ++j)
2568 {
2569 const char *this_name = file_data->file_names[j];
2570 if (FILENAME_CMP (this_name, filename) == 0)
2571 {
2572 dw2_instantiate_symtab (objfile, per_cu);
2573 break;
2574 }
2575 }
2576 }
2577 }
2578
2579 static const char *
2580 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2581 {
2582 struct dwarf2_per_cu_data *per_cu;
2583 offset_type *vec;
2584 struct quick_file_names *file_data;
2585
2586 dw2_setup (objfile);
2587
2588 /* index_table is NULL if OBJF_READNOW. */
2589 if (!dwarf2_per_objfile->index_table)
2590 return NULL;
2591
2592 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2593 name, &vec))
2594 return NULL;
2595
2596 /* Note that this just looks at the very first one named NAME -- but
2597 actually we are looking for a function. find_main_filename
2598 should be rewritten so that it doesn't require a custom hook. It
2599 could just use the ordinary symbol tables. */
2600 /* vec[0] is the length, which must always be >0. */
2601 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2602
2603 file_data = dw2_get_file_names (objfile, per_cu);
2604 if (file_data == NULL)
2605 return NULL;
2606
2607 return file_data->file_names[file_data->num_file_names - 1];
2608 }
2609
2610 static void
2611 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2612 struct objfile *objfile, int global,
2613 int (*callback) (struct block *,
2614 struct symbol *, void *),
2615 void *data, symbol_compare_ftype *match,
2616 symbol_compare_ftype *ordered_compare)
2617 {
2618 /* Currently unimplemented; used for Ada. The function can be called if the
2619 current language is Ada for a non-Ada objfile using GNU index. As Ada
2620 does not look for non-Ada symbols this function should just return. */
2621 }
2622
2623 static void
2624 dw2_expand_symtabs_matching (struct objfile *objfile,
2625 int (*file_matcher) (const char *, void *),
2626 int (*name_matcher) (const char *, void *),
2627 enum search_domain kind,
2628 void *data)
2629 {
2630 int i;
2631 offset_type iter;
2632 struct mapped_index *index;
2633
2634 dw2_setup (objfile);
2635
2636 /* index_table is NULL if OBJF_READNOW. */
2637 if (!dwarf2_per_objfile->index_table)
2638 return;
2639 index = dwarf2_per_objfile->index_table;
2640
2641 if (file_matcher != NULL)
2642 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2643 + dwarf2_per_objfile->n_type_comp_units); ++i)
2644 {
2645 int j;
2646 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2647 struct quick_file_names *file_data;
2648
2649 per_cu->v.quick->mark = 0;
2650 if (per_cu->v.quick->symtab)
2651 continue;
2652
2653 file_data = dw2_get_file_names (objfile, per_cu);
2654 if (file_data == NULL)
2655 continue;
2656
2657 for (j = 0; j < file_data->num_file_names; ++j)
2658 {
2659 if (file_matcher (file_data->file_names[j], data))
2660 {
2661 per_cu->v.quick->mark = 1;
2662 break;
2663 }
2664 }
2665 }
2666
2667 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2668 {
2669 offset_type idx = 2 * iter;
2670 const char *name;
2671 offset_type *vec, vec_len, vec_idx;
2672
2673 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2674 continue;
2675
2676 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2677
2678 if (! (*name_matcher) (name, data))
2679 continue;
2680
2681 /* The name was matched, now expand corresponding CUs that were
2682 marked. */
2683 vec = (offset_type *) (index->constant_pool
2684 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2685 vec_len = MAYBE_SWAP (vec[0]);
2686 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2687 {
2688 struct dwarf2_per_cu_data *per_cu;
2689
2690 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2691 if (file_matcher == NULL || per_cu->v.quick->mark)
2692 dw2_instantiate_symtab (objfile, per_cu);
2693 }
2694 }
2695 }
2696
2697 static struct symtab *
2698 dw2_find_pc_sect_symtab (struct objfile *objfile,
2699 struct minimal_symbol *msymbol,
2700 CORE_ADDR pc,
2701 struct obj_section *section,
2702 int warn_if_readin)
2703 {
2704 struct dwarf2_per_cu_data *data;
2705
2706 dw2_setup (objfile);
2707
2708 if (!objfile->psymtabs_addrmap)
2709 return NULL;
2710
2711 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2712 if (!data)
2713 return NULL;
2714
2715 if (warn_if_readin && data->v.quick->symtab)
2716 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2717 paddress (get_objfile_arch (objfile), pc));
2718
2719 return dw2_instantiate_symtab (objfile, data);
2720 }
2721
2722 static void
2723 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2724 void *data)
2725 {
2726 int i;
2727
2728 dw2_setup (objfile);
2729
2730 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2731 + dwarf2_per_objfile->n_type_comp_units); ++i)
2732 {
2733 int j;
2734 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2735 struct quick_file_names *file_data;
2736
2737 if (per_cu->v.quick->symtab)
2738 continue;
2739
2740 file_data = dw2_get_file_names (objfile, per_cu);
2741 if (file_data == NULL)
2742 continue;
2743
2744 for (j = 0; j < file_data->num_file_names; ++j)
2745 {
2746 const char *this_real_name = dw2_get_real_path (objfile, file_data,
2747 j);
2748 (*fun) (file_data->file_names[j], this_real_name, data);
2749 }
2750 }
2751 }
2752
2753 static int
2754 dw2_has_symbols (struct objfile *objfile)
2755 {
2756 return 1;
2757 }
2758
2759 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2760 {
2761 dw2_has_symbols,
2762 dw2_find_last_source_symtab,
2763 dw2_forget_cached_source_info,
2764 dw2_lookup_symtab,
2765 dw2_lookup_symbol,
2766 dw2_pre_expand_symtabs_matching,
2767 dw2_print_stats,
2768 dw2_dump,
2769 dw2_relocate,
2770 dw2_expand_symtabs_for_function,
2771 dw2_expand_all_symtabs,
2772 dw2_expand_symtabs_with_filename,
2773 dw2_find_symbol_file,
2774 dw2_map_matching_symbols,
2775 dw2_expand_symtabs_matching,
2776 dw2_find_pc_sect_symtab,
2777 dw2_map_symbol_filenames
2778 };
2779
2780 /* Initialize for reading DWARF for this objfile. Return 0 if this
2781 file will use psymtabs, or 1 if using the GNU index. */
2782
2783 int
2784 dwarf2_initialize_objfile (struct objfile *objfile)
2785 {
2786 /* If we're about to read full symbols, don't bother with the
2787 indices. In this case we also don't care if some other debug
2788 format is making psymtabs, because they are all about to be
2789 expanded anyway. */
2790 if ((objfile->flags & OBJF_READNOW))
2791 {
2792 int i;
2793
2794 dwarf2_per_objfile->using_index = 1;
2795 create_all_comp_units (objfile);
2796 create_debug_types_hash_table (objfile);
2797 dwarf2_per_objfile->quick_file_names_table =
2798 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2799
2800 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2801 + dwarf2_per_objfile->n_type_comp_units); ++i)
2802 {
2803 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2804
2805 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2806 struct dwarf2_per_cu_quick_data);
2807 }
2808
2809 /* Return 1 so that gdb sees the "quick" functions. However,
2810 these functions will be no-ops because we will have expanded
2811 all symtabs. */
2812 return 1;
2813 }
2814
2815 if (dwarf2_read_index (objfile))
2816 return 1;
2817
2818 return 0;
2819 }
2820
2821 \f
2822
2823 /* Build a partial symbol table. */
2824
2825 void
2826 dwarf2_build_psymtabs (struct objfile *objfile)
2827 {
2828 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2829 {
2830 init_psymbol_list (objfile, 1024);
2831 }
2832
2833 dwarf2_build_psymtabs_hard (objfile);
2834 }
2835
2836 /* Return TRUE if OFFSET is within CU_HEADER. */
2837
2838 static inline int
2839 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2840 {
2841 unsigned int bottom = cu_header->offset;
2842 unsigned int top = (cu_header->offset
2843 + cu_header->length
2844 + cu_header->initial_length_size);
2845
2846 return (offset >= bottom && offset < top);
2847 }
2848
2849 /* Read in the comp unit header information from the debug_info at info_ptr.
2850 NOTE: This leaves members offset, first_die_offset to be filled in
2851 by the caller. */
2852
2853 static gdb_byte *
2854 read_comp_unit_head (struct comp_unit_head *cu_header,
2855 gdb_byte *info_ptr, bfd *abfd)
2856 {
2857 int signed_addr;
2858 unsigned int bytes_read;
2859
2860 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
2861 cu_header->initial_length_size = bytes_read;
2862 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
2863 info_ptr += bytes_read;
2864 cu_header->version = read_2_bytes (abfd, info_ptr);
2865 info_ptr += 2;
2866 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
2867 &bytes_read);
2868 info_ptr += bytes_read;
2869 cu_header->addr_size = read_1_byte (abfd, info_ptr);
2870 info_ptr += 1;
2871 signed_addr = bfd_get_sign_extend_vma (abfd);
2872 if (signed_addr < 0)
2873 internal_error (__FILE__, __LINE__,
2874 _("read_comp_unit_head: dwarf from non elf file"));
2875 cu_header->signed_addr_p = signed_addr;
2876
2877 return info_ptr;
2878 }
2879
2880 static gdb_byte *
2881 partial_read_comp_unit_head (struct comp_unit_head *header, gdb_byte *info_ptr,
2882 gdb_byte *buffer, unsigned int buffer_size,
2883 bfd *abfd)
2884 {
2885 gdb_byte *beg_of_comp_unit = info_ptr;
2886
2887 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
2888
2889 if (header->version != 2 && header->version != 3 && header->version != 4)
2890 error (_("Dwarf Error: wrong version in compilation unit header "
2891 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
2892 bfd_get_filename (abfd));
2893
2894 if (header->abbrev_offset
2895 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
2896 &dwarf2_per_objfile->abbrev))
2897 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
2898 "(offset 0x%lx + 6) [in module %s]"),
2899 (long) header->abbrev_offset,
2900 (long) (beg_of_comp_unit - buffer),
2901 bfd_get_filename (abfd));
2902
2903 if (beg_of_comp_unit + header->length + header->initial_length_size
2904 > buffer + buffer_size)
2905 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
2906 "(offset 0x%lx + 0) [in module %s]"),
2907 (long) header->length,
2908 (long) (beg_of_comp_unit - buffer),
2909 bfd_get_filename (abfd));
2910
2911 return info_ptr;
2912 }
2913
2914 /* Read in the types comp unit header information from .debug_types entry at
2915 types_ptr. The result is a pointer to one past the end of the header. */
2916
2917 static gdb_byte *
2918 read_type_comp_unit_head (struct comp_unit_head *cu_header,
2919 ULONGEST *signature,
2920 gdb_byte *types_ptr, bfd *abfd)
2921 {
2922 gdb_byte *initial_types_ptr = types_ptr;
2923
2924 dwarf2_read_section (dwarf2_per_objfile->objfile,
2925 &dwarf2_per_objfile->types);
2926 cu_header->offset = types_ptr - dwarf2_per_objfile->types.buffer;
2927
2928 types_ptr = read_comp_unit_head (cu_header, types_ptr, abfd);
2929
2930 *signature = read_8_bytes (abfd, types_ptr);
2931 types_ptr += 8;
2932 types_ptr += cu_header->offset_size;
2933 cu_header->first_die_offset = types_ptr - initial_types_ptr;
2934
2935 return types_ptr;
2936 }
2937
2938 /* Allocate a new partial symtab for file named NAME and mark this new
2939 partial symtab as being an include of PST. */
2940
2941 static void
2942 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
2943 struct objfile *objfile)
2944 {
2945 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
2946
2947 subpst->section_offsets = pst->section_offsets;
2948 subpst->textlow = 0;
2949 subpst->texthigh = 0;
2950
2951 subpst->dependencies = (struct partial_symtab **)
2952 obstack_alloc (&objfile->objfile_obstack,
2953 sizeof (struct partial_symtab *));
2954 subpst->dependencies[0] = pst;
2955 subpst->number_of_dependencies = 1;
2956
2957 subpst->globals_offset = 0;
2958 subpst->n_global_syms = 0;
2959 subpst->statics_offset = 0;
2960 subpst->n_static_syms = 0;
2961 subpst->symtab = NULL;
2962 subpst->read_symtab = pst->read_symtab;
2963 subpst->readin = 0;
2964
2965 /* No private part is necessary for include psymtabs. This property
2966 can be used to differentiate between such include psymtabs and
2967 the regular ones. */
2968 subpst->read_symtab_private = NULL;
2969 }
2970
2971 /* Read the Line Number Program data and extract the list of files
2972 included by the source file represented by PST. Build an include
2973 partial symtab for each of these included files. */
2974
2975 static void
2976 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
2977 struct die_info *die,
2978 struct partial_symtab *pst)
2979 {
2980 struct objfile *objfile = cu->objfile;
2981 bfd *abfd = objfile->obfd;
2982 struct line_header *lh = NULL;
2983 struct attribute *attr;
2984
2985 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
2986 if (attr)
2987 {
2988 unsigned int line_offset = DW_UNSND (attr);
2989
2990 lh = dwarf_decode_line_header (line_offset, abfd, cu);
2991 }
2992 if (lh == NULL)
2993 return; /* No linetable, so no includes. */
2994
2995 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
2996 dwarf_decode_lines (lh, pst->dirname, abfd, cu, pst);
2997
2998 free_line_header (lh);
2999 }
3000
3001 static hashval_t
3002 hash_type_signature (const void *item)
3003 {
3004 const struct signatured_type *type_sig = item;
3005
3006 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3007 return type_sig->signature;
3008 }
3009
3010 static int
3011 eq_type_signature (const void *item_lhs, const void *item_rhs)
3012 {
3013 const struct signatured_type *lhs = item_lhs;
3014 const struct signatured_type *rhs = item_rhs;
3015
3016 return lhs->signature == rhs->signature;
3017 }
3018
3019 /* Allocate a hash table for signatured types. */
3020
3021 static htab_t
3022 allocate_signatured_type_table (struct objfile *objfile)
3023 {
3024 return htab_create_alloc_ex (41,
3025 hash_type_signature,
3026 eq_type_signature,
3027 NULL,
3028 &objfile->objfile_obstack,
3029 hashtab_obstack_allocate,
3030 dummy_obstack_deallocate);
3031 }
3032
3033 /* A helper function to add a signatured type CU to a list. */
3034
3035 static int
3036 add_signatured_type_cu_to_list (void **slot, void *datum)
3037 {
3038 struct signatured_type *sigt = *slot;
3039 struct dwarf2_per_cu_data ***datap = datum;
3040
3041 **datap = &sigt->per_cu;
3042 ++*datap;
3043
3044 return 1;
3045 }
3046
3047 /* Create the hash table of all entries in the .debug_types section.
3048 The result is zero if there is an error (e.g. missing .debug_types section),
3049 otherwise non-zero. */
3050
3051 static int
3052 create_debug_types_hash_table (struct objfile *objfile)
3053 {
3054 gdb_byte *info_ptr;
3055 htab_t types_htab;
3056 struct dwarf2_per_cu_data **iter;
3057
3058 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
3059 info_ptr = dwarf2_per_objfile->types.buffer;
3060
3061 if (info_ptr == NULL)
3062 {
3063 dwarf2_per_objfile->signatured_types = NULL;
3064 return 0;
3065 }
3066
3067 types_htab = allocate_signatured_type_table (objfile);
3068
3069 if (dwarf2_die_debug)
3070 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3071
3072 while (info_ptr < dwarf2_per_objfile->types.buffer
3073 + dwarf2_per_objfile->types.size)
3074 {
3075 unsigned int offset;
3076 unsigned int offset_size;
3077 unsigned int type_offset;
3078 unsigned int length, initial_length_size;
3079 unsigned short version;
3080 ULONGEST signature;
3081 struct signatured_type *type_sig;
3082 void **slot;
3083 gdb_byte *ptr = info_ptr;
3084
3085 offset = ptr - dwarf2_per_objfile->types.buffer;
3086
3087 /* We need to read the type's signature in order to build the hash
3088 table, but we don't need to read anything else just yet. */
3089
3090 /* Sanity check to ensure entire cu is present. */
3091 length = read_initial_length (objfile->obfd, ptr, &initial_length_size);
3092 if (ptr + length + initial_length_size
3093 > dwarf2_per_objfile->types.buffer + dwarf2_per_objfile->types.size)
3094 {
3095 complaint (&symfile_complaints,
3096 _("debug type entry runs off end "
3097 "of `.debug_types' section, ignored"));
3098 break;
3099 }
3100
3101 offset_size = initial_length_size == 4 ? 4 : 8;
3102 ptr += initial_length_size;
3103 version = bfd_get_16 (objfile->obfd, ptr);
3104 ptr += 2;
3105 ptr += offset_size; /* abbrev offset */
3106 ptr += 1; /* address size */
3107 signature = bfd_get_64 (objfile->obfd, ptr);
3108 ptr += 8;
3109 type_offset = read_offset_1 (objfile->obfd, ptr, offset_size);
3110
3111 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3112 memset (type_sig, 0, sizeof (*type_sig));
3113 type_sig->signature = signature;
3114 type_sig->type_offset = type_offset;
3115 type_sig->per_cu.objfile = objfile;
3116 type_sig->per_cu.from_debug_types = 1;
3117 type_sig->per_cu.offset = offset;
3118
3119 slot = htab_find_slot (types_htab, type_sig, INSERT);
3120 gdb_assert (slot != NULL);
3121 if (*slot != NULL)
3122 {
3123 const struct signatured_type *dup_sig = *slot;
3124
3125 complaint (&symfile_complaints,
3126 _("debug type entry at offset 0x%x is duplicate to the "
3127 "entry at offset 0x%x, signature 0x%s"),
3128 offset, dup_sig->per_cu.offset,
3129 phex (signature, sizeof (signature)));
3130 gdb_assert (signature == dup_sig->signature);
3131 }
3132 *slot = type_sig;
3133
3134 if (dwarf2_die_debug)
3135 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3136 offset, phex (signature, sizeof (signature)));
3137
3138 info_ptr = info_ptr + initial_length_size + length;
3139 }
3140
3141 dwarf2_per_objfile->signatured_types = types_htab;
3142
3143 dwarf2_per_objfile->n_type_comp_units = htab_elements (types_htab);
3144 dwarf2_per_objfile->type_comp_units
3145 = obstack_alloc (&objfile->objfile_obstack,
3146 dwarf2_per_objfile->n_type_comp_units
3147 * sizeof (struct dwarf2_per_cu_data *));
3148 iter = &dwarf2_per_objfile->type_comp_units[0];
3149 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_list, &iter);
3150 gdb_assert (iter - &dwarf2_per_objfile->type_comp_units[0]
3151 == dwarf2_per_objfile->n_type_comp_units);
3152
3153 return 1;
3154 }
3155
3156 /* Lookup a signature based type.
3157 Returns NULL if SIG is not present in the table. */
3158
3159 static struct signatured_type *
3160 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3161 {
3162 struct signatured_type find_entry, *entry;
3163
3164 if (dwarf2_per_objfile->signatured_types == NULL)
3165 {
3166 complaint (&symfile_complaints,
3167 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3168 return 0;
3169 }
3170
3171 find_entry.signature = sig;
3172 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3173 return entry;
3174 }
3175
3176 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3177
3178 static void
3179 init_cu_die_reader (struct die_reader_specs *reader,
3180 struct dwarf2_cu *cu)
3181 {
3182 reader->abfd = cu->objfile->obfd;
3183 reader->cu = cu;
3184 if (cu->per_cu->from_debug_types)
3185 {
3186 gdb_assert (dwarf2_per_objfile->types.readin);
3187 reader->buffer = dwarf2_per_objfile->types.buffer;
3188 }
3189 else
3190 {
3191 gdb_assert (dwarf2_per_objfile->info.readin);
3192 reader->buffer = dwarf2_per_objfile->info.buffer;
3193 }
3194 }
3195
3196 /* Find the base address of the compilation unit for range lists and
3197 location lists. It will normally be specified by DW_AT_low_pc.
3198 In DWARF-3 draft 4, the base address could be overridden by
3199 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3200 compilation units with discontinuous ranges. */
3201
3202 static void
3203 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3204 {
3205 struct attribute *attr;
3206
3207 cu->base_known = 0;
3208 cu->base_address = 0;
3209
3210 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3211 if (attr)
3212 {
3213 cu->base_address = DW_ADDR (attr);
3214 cu->base_known = 1;
3215 }
3216 else
3217 {
3218 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3219 if (attr)
3220 {
3221 cu->base_address = DW_ADDR (attr);
3222 cu->base_known = 1;
3223 }
3224 }
3225 }
3226
3227 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3228 to combine the common parts.
3229 Process a compilation unit for a psymtab.
3230 BUFFER is a pointer to the beginning of the dwarf section buffer,
3231 either .debug_info or debug_types.
3232 INFO_PTR is a pointer to the start of the CU.
3233 Returns a pointer to the next CU. */
3234
3235 static gdb_byte *
3236 process_psymtab_comp_unit (struct objfile *objfile,
3237 struct dwarf2_per_cu_data *this_cu,
3238 gdb_byte *buffer, gdb_byte *info_ptr,
3239 unsigned int buffer_size)
3240 {
3241 bfd *abfd = objfile->obfd;
3242 gdb_byte *beg_of_comp_unit = info_ptr;
3243 struct die_info *comp_unit_die;
3244 struct partial_symtab *pst;
3245 CORE_ADDR baseaddr;
3246 struct cleanup *back_to_inner;
3247 struct dwarf2_cu cu;
3248 int has_children, has_pc_info;
3249 struct attribute *attr;
3250 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3251 struct die_reader_specs reader_specs;
3252 const char *filename;
3253
3254 init_one_comp_unit (&cu, objfile);
3255 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3256
3257 info_ptr = partial_read_comp_unit_head (&cu.header, info_ptr,
3258 buffer, buffer_size,
3259 abfd);
3260
3261 /* Complete the cu_header. */
3262 cu.header.offset = beg_of_comp_unit - buffer;
3263 cu.header.first_die_offset = info_ptr - beg_of_comp_unit;
3264
3265 cu.list_in_scope = &file_symbols;
3266
3267 /* If this compilation unit was already read in, free the
3268 cached copy in order to read it in again. This is
3269 necessary because we skipped some symbols when we first
3270 read in the compilation unit (see load_partial_dies).
3271 This problem could be avoided, but the benefit is
3272 unclear. */
3273 if (this_cu->cu != NULL)
3274 free_one_cached_comp_unit (this_cu->cu);
3275
3276 /* Note that this is a pointer to our stack frame, being
3277 added to a global data structure. It will be cleaned up
3278 in free_stack_comp_unit when we finish with this
3279 compilation unit. */
3280 this_cu->cu = &cu;
3281 cu.per_cu = this_cu;
3282
3283 /* Read the abbrevs for this compilation unit into a table. */
3284 dwarf2_read_abbrevs (abfd, &cu);
3285 make_cleanup (dwarf2_free_abbrev_table, &cu);
3286
3287 /* Read the compilation unit die. */
3288 if (this_cu->from_debug_types)
3289 info_ptr += 8 /*signature*/ + cu.header.offset_size;
3290 init_cu_die_reader (&reader_specs, &cu);
3291 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3292 &has_children);
3293
3294 if (this_cu->from_debug_types)
3295 {
3296 /* LENGTH has not been set yet for type units. */
3297 gdb_assert (this_cu->offset == cu.header.offset);
3298 this_cu->length = cu.header.length + cu.header.initial_length_size;
3299 }
3300 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3301 {
3302 info_ptr = (beg_of_comp_unit + cu.header.length
3303 + cu.header.initial_length_size);
3304 do_cleanups (back_to_inner);
3305 return info_ptr;
3306 }
3307
3308 prepare_one_comp_unit (&cu, comp_unit_die);
3309
3310 /* Allocate a new partial symbol table structure. */
3311 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3312 if (attr == NULL || !DW_STRING (attr))
3313 filename = "";
3314 else
3315 filename = DW_STRING (attr);
3316 pst = start_psymtab_common (objfile, objfile->section_offsets,
3317 filename,
3318 /* TEXTLOW and TEXTHIGH are set below. */
3319 0,
3320 objfile->global_psymbols.next,
3321 objfile->static_psymbols.next);
3322
3323 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3324 if (attr != NULL)
3325 pst->dirname = DW_STRING (attr);
3326
3327 pst->read_symtab_private = this_cu;
3328
3329 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3330
3331 /* Store the function that reads in the rest of the symbol table. */
3332 pst->read_symtab = dwarf2_psymtab_to_symtab;
3333
3334 this_cu->v.psymtab = pst;
3335
3336 dwarf2_find_base_address (comp_unit_die, &cu);
3337
3338 /* Possibly set the default values of LOWPC and HIGHPC from
3339 `DW_AT_ranges'. */
3340 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3341 &best_highpc, &cu, pst);
3342 if (has_pc_info == 1 && best_lowpc < best_highpc)
3343 /* Store the contiguous range if it is not empty; it can be empty for
3344 CUs with no code. */
3345 addrmap_set_empty (objfile->psymtabs_addrmap,
3346 best_lowpc + baseaddr,
3347 best_highpc + baseaddr - 1, pst);
3348
3349 /* Check if comp unit has_children.
3350 If so, read the rest of the partial symbols from this comp unit.
3351 If not, there's no more debug_info for this comp unit. */
3352 if (has_children)
3353 {
3354 struct partial_die_info *first_die;
3355 CORE_ADDR lowpc, highpc;
3356
3357 lowpc = ((CORE_ADDR) -1);
3358 highpc = ((CORE_ADDR) 0);
3359
3360 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3361
3362 scan_partial_symbols (first_die, &lowpc, &highpc,
3363 ! has_pc_info, &cu);
3364
3365 /* If we didn't find a lowpc, set it to highpc to avoid
3366 complaints from `maint check'. */
3367 if (lowpc == ((CORE_ADDR) -1))
3368 lowpc = highpc;
3369
3370 /* If the compilation unit didn't have an explicit address range,
3371 then use the information extracted from its child dies. */
3372 if (! has_pc_info)
3373 {
3374 best_lowpc = lowpc;
3375 best_highpc = highpc;
3376 }
3377 }
3378 pst->textlow = best_lowpc + baseaddr;
3379 pst->texthigh = best_highpc + baseaddr;
3380
3381 pst->n_global_syms = objfile->global_psymbols.next -
3382 (objfile->global_psymbols.list + pst->globals_offset);
3383 pst->n_static_syms = objfile->static_psymbols.next -
3384 (objfile->static_psymbols.list + pst->statics_offset);
3385 sort_pst_symbols (pst);
3386
3387 info_ptr = (beg_of_comp_unit + cu.header.length
3388 + cu.header.initial_length_size);
3389
3390 if (this_cu->from_debug_types)
3391 {
3392 /* It's not clear we want to do anything with stmt lists here.
3393 Waiting to see what gcc ultimately does. */
3394 }
3395 else
3396 {
3397 /* Get the list of files included in the current compilation unit,
3398 and build a psymtab for each of them. */
3399 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3400 }
3401
3402 do_cleanups (back_to_inner);
3403
3404 return info_ptr;
3405 }
3406
3407 /* Traversal function for htab_traverse_noresize.
3408 Process one .debug_types comp-unit. */
3409
3410 static int
3411 process_type_comp_unit (void **slot, void *info)
3412 {
3413 struct signatured_type *entry = (struct signatured_type *) *slot;
3414 struct objfile *objfile = (struct objfile *) info;
3415 struct dwarf2_per_cu_data *this_cu;
3416
3417 this_cu = &entry->per_cu;
3418
3419 gdb_assert (dwarf2_per_objfile->types.readin);
3420 process_psymtab_comp_unit (objfile, this_cu,
3421 dwarf2_per_objfile->types.buffer,
3422 dwarf2_per_objfile->types.buffer + this_cu->offset,
3423 dwarf2_per_objfile->types.size);
3424
3425 return 1;
3426 }
3427
3428 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3429 Build partial symbol tables for the .debug_types comp-units. */
3430
3431 static void
3432 build_type_psymtabs (struct objfile *objfile)
3433 {
3434 if (! create_debug_types_hash_table (objfile))
3435 return;
3436
3437 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3438 process_type_comp_unit, objfile);
3439 }
3440
3441 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3442
3443 static void
3444 psymtabs_addrmap_cleanup (void *o)
3445 {
3446 struct objfile *objfile = o;
3447
3448 objfile->psymtabs_addrmap = NULL;
3449 }
3450
3451 /* Build the partial symbol table by doing a quick pass through the
3452 .debug_info and .debug_abbrev sections. */
3453
3454 static void
3455 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3456 {
3457 gdb_byte *info_ptr;
3458 struct cleanup *back_to, *addrmap_cleanup;
3459 struct obstack temp_obstack;
3460
3461 dwarf2_per_objfile->reading_partial_symbols = 1;
3462
3463 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3464 info_ptr = dwarf2_per_objfile->info.buffer;
3465
3466 /* Any cached compilation units will be linked by the per-objfile
3467 read_in_chain. Make sure to free them when we're done. */
3468 back_to = make_cleanup (free_cached_comp_units, NULL);
3469
3470 build_type_psymtabs (objfile);
3471
3472 create_all_comp_units (objfile);
3473
3474 /* Create a temporary address map on a temporary obstack. We later
3475 copy this to the final obstack. */
3476 obstack_init (&temp_obstack);
3477 make_cleanup_obstack_free (&temp_obstack);
3478 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3479 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3480
3481 /* Since the objects we're extracting from .debug_info vary in
3482 length, only the individual functions to extract them (like
3483 read_comp_unit_head and load_partial_die) can really know whether
3484 the buffer is large enough to hold another complete object.
3485
3486 At the moment, they don't actually check that. If .debug_info
3487 holds just one extra byte after the last compilation unit's dies,
3488 then read_comp_unit_head will happily read off the end of the
3489 buffer. read_partial_die is similarly casual. Those functions
3490 should be fixed.
3491
3492 For this loop condition, simply checking whether there's any data
3493 left at all should be sufficient. */
3494
3495 while (info_ptr < (dwarf2_per_objfile->info.buffer
3496 + dwarf2_per_objfile->info.size))
3497 {
3498 struct dwarf2_per_cu_data *this_cu;
3499
3500 this_cu = dwarf2_find_comp_unit (info_ptr
3501 - dwarf2_per_objfile->info.buffer,
3502 objfile);
3503
3504 info_ptr = process_psymtab_comp_unit (objfile, this_cu,
3505 dwarf2_per_objfile->info.buffer,
3506 info_ptr,
3507 dwarf2_per_objfile->info.size);
3508 }
3509
3510 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3511 &objfile->objfile_obstack);
3512 discard_cleanups (addrmap_cleanup);
3513
3514 do_cleanups (back_to);
3515 }
3516
3517 /* Load the partial DIEs for a secondary CU into memory. */
3518
3519 static void
3520 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu,
3521 struct objfile *objfile)
3522 {
3523 bfd *abfd = objfile->obfd;
3524 gdb_byte *info_ptr, *beg_of_comp_unit;
3525 struct die_info *comp_unit_die;
3526 struct dwarf2_cu *cu;
3527 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3528 int has_children;
3529 struct die_reader_specs reader_specs;
3530 int read_cu = 0;
3531
3532 gdb_assert (! this_cu->from_debug_types);
3533
3534 gdb_assert (dwarf2_per_objfile->info.readin);
3535 info_ptr = dwarf2_per_objfile->info.buffer + this_cu->offset;
3536 beg_of_comp_unit = info_ptr;
3537
3538 if (this_cu->cu == NULL)
3539 {
3540 cu = xmalloc (sizeof (*cu));
3541 init_one_comp_unit (cu, objfile);
3542
3543 read_cu = 1;
3544
3545 /* If an error occurs while loading, release our storage. */
3546 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
3547
3548 info_ptr = partial_read_comp_unit_head (&cu->header, info_ptr,
3549 dwarf2_per_objfile->info.buffer,
3550 dwarf2_per_objfile->info.size,
3551 abfd);
3552
3553 /* Complete the cu_header. */
3554 cu->header.offset = this_cu->offset;
3555 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
3556
3557 /* Link this compilation unit into the compilation unit tree. */
3558 this_cu->cu = cu;
3559 cu->per_cu = this_cu;
3560
3561 /* Link this CU into read_in_chain. */
3562 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3563 dwarf2_per_objfile->read_in_chain = this_cu;
3564 }
3565 else
3566 {
3567 cu = this_cu->cu;
3568 info_ptr += cu->header.first_die_offset;
3569 }
3570
3571 /* Read the abbrevs for this compilation unit into a table. */
3572 gdb_assert (cu->dwarf2_abbrevs == NULL);
3573 dwarf2_read_abbrevs (abfd, cu);
3574 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3575
3576 /* Read the compilation unit die. */
3577 init_cu_die_reader (&reader_specs, cu);
3578 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3579 &has_children);
3580
3581 prepare_one_comp_unit (cu, comp_unit_die);
3582
3583 /* Check if comp unit has_children.
3584 If so, read the rest of the partial symbols from this comp unit.
3585 If not, there's no more debug_info for this comp unit. */
3586 if (has_children)
3587 load_partial_dies (abfd, dwarf2_per_objfile->info.buffer, info_ptr, 0, cu);
3588
3589 do_cleanups (free_abbrevs_cleanup);
3590
3591 if (read_cu)
3592 {
3593 /* We've successfully allocated this compilation unit. Let our
3594 caller clean it up when finished with it. */
3595 discard_cleanups (free_cu_cleanup);
3596 }
3597 }
3598
3599 /* Create a list of all compilation units in OBJFILE. We do this only
3600 if an inter-comp-unit reference is found; presumably if there is one,
3601 there will be many, and one will occur early in the .debug_info section.
3602 So there's no point in building this list incrementally. */
3603
3604 static void
3605 create_all_comp_units (struct objfile *objfile)
3606 {
3607 int n_allocated;
3608 int n_comp_units;
3609 struct dwarf2_per_cu_data **all_comp_units;
3610 gdb_byte *info_ptr;
3611
3612 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3613 info_ptr = dwarf2_per_objfile->info.buffer;
3614
3615 n_comp_units = 0;
3616 n_allocated = 10;
3617 all_comp_units = xmalloc (n_allocated
3618 * sizeof (struct dwarf2_per_cu_data *));
3619
3620 while (info_ptr < dwarf2_per_objfile->info.buffer
3621 + dwarf2_per_objfile->info.size)
3622 {
3623 unsigned int length, initial_length_size;
3624 struct dwarf2_per_cu_data *this_cu;
3625 unsigned int offset;
3626
3627 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3628
3629 /* Read just enough information to find out where the next
3630 compilation unit is. */
3631 length = read_initial_length (objfile->obfd, info_ptr,
3632 &initial_length_size);
3633
3634 /* Save the compilation unit for later lookup. */
3635 this_cu = obstack_alloc (&objfile->objfile_obstack,
3636 sizeof (struct dwarf2_per_cu_data));
3637 memset (this_cu, 0, sizeof (*this_cu));
3638 this_cu->offset = offset;
3639 this_cu->length = length + initial_length_size;
3640 this_cu->objfile = objfile;
3641
3642 if (n_comp_units == n_allocated)
3643 {
3644 n_allocated *= 2;
3645 all_comp_units = xrealloc (all_comp_units,
3646 n_allocated
3647 * sizeof (struct dwarf2_per_cu_data *));
3648 }
3649 all_comp_units[n_comp_units++] = this_cu;
3650
3651 info_ptr = info_ptr + this_cu->length;
3652 }
3653
3654 dwarf2_per_objfile->all_comp_units
3655 = obstack_alloc (&objfile->objfile_obstack,
3656 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3657 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3658 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3659 xfree (all_comp_units);
3660 dwarf2_per_objfile->n_comp_units = n_comp_units;
3661 }
3662
3663 /* Process all loaded DIEs for compilation unit CU, starting at
3664 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3665 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3666 DW_AT_ranges). If NEED_PC is set, then this function will set
3667 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3668 and record the covered ranges in the addrmap. */
3669
3670 static void
3671 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3672 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3673 {
3674 struct partial_die_info *pdi;
3675
3676 /* Now, march along the PDI's, descending into ones which have
3677 interesting children but skipping the children of the other ones,
3678 until we reach the end of the compilation unit. */
3679
3680 pdi = first_die;
3681
3682 while (pdi != NULL)
3683 {
3684 fixup_partial_die (pdi, cu);
3685
3686 /* Anonymous namespaces or modules have no name but have interesting
3687 children, so we need to look at them. Ditto for anonymous
3688 enums. */
3689
3690 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3691 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3692 {
3693 switch (pdi->tag)
3694 {
3695 case DW_TAG_subprogram:
3696 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3697 break;
3698 case DW_TAG_constant:
3699 case DW_TAG_variable:
3700 case DW_TAG_typedef:
3701 case DW_TAG_union_type:
3702 if (!pdi->is_declaration)
3703 {
3704 add_partial_symbol (pdi, cu);
3705 }
3706 break;
3707 case DW_TAG_class_type:
3708 case DW_TAG_interface_type:
3709 case DW_TAG_structure_type:
3710 if (!pdi->is_declaration)
3711 {
3712 add_partial_symbol (pdi, cu);
3713 }
3714 break;
3715 case DW_TAG_enumeration_type:
3716 if (!pdi->is_declaration)
3717 add_partial_enumeration (pdi, cu);
3718 break;
3719 case DW_TAG_base_type:
3720 case DW_TAG_subrange_type:
3721 /* File scope base type definitions are added to the partial
3722 symbol table. */
3723 add_partial_symbol (pdi, cu);
3724 break;
3725 case DW_TAG_namespace:
3726 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3727 break;
3728 case DW_TAG_module:
3729 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3730 break;
3731 default:
3732 break;
3733 }
3734 }
3735
3736 /* If the die has a sibling, skip to the sibling. */
3737
3738 pdi = pdi->die_sibling;
3739 }
3740 }
3741
3742 /* Functions used to compute the fully scoped name of a partial DIE.
3743
3744 Normally, this is simple. For C++, the parent DIE's fully scoped
3745 name is concatenated with "::" and the partial DIE's name. For
3746 Java, the same thing occurs except that "." is used instead of "::".
3747 Enumerators are an exception; they use the scope of their parent
3748 enumeration type, i.e. the name of the enumeration type is not
3749 prepended to the enumerator.
3750
3751 There are two complexities. One is DW_AT_specification; in this
3752 case "parent" means the parent of the target of the specification,
3753 instead of the direct parent of the DIE. The other is compilers
3754 which do not emit DW_TAG_namespace; in this case we try to guess
3755 the fully qualified name of structure types from their members'
3756 linkage names. This must be done using the DIE's children rather
3757 than the children of any DW_AT_specification target. We only need
3758 to do this for structures at the top level, i.e. if the target of
3759 any DW_AT_specification (if any; otherwise the DIE itself) does not
3760 have a parent. */
3761
3762 /* Compute the scope prefix associated with PDI's parent, in
3763 compilation unit CU. The result will be allocated on CU's
3764 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3765 field. NULL is returned if no prefix is necessary. */
3766 static char *
3767 partial_die_parent_scope (struct partial_die_info *pdi,
3768 struct dwarf2_cu *cu)
3769 {
3770 char *grandparent_scope;
3771 struct partial_die_info *parent, *real_pdi;
3772
3773 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3774 then this means the parent of the specification DIE. */
3775
3776 real_pdi = pdi;
3777 while (real_pdi->has_specification)
3778 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3779
3780 parent = real_pdi->die_parent;
3781 if (parent == NULL)
3782 return NULL;
3783
3784 if (parent->scope_set)
3785 return parent->scope;
3786
3787 fixup_partial_die (parent, cu);
3788
3789 grandparent_scope = partial_die_parent_scope (parent, cu);
3790
3791 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3792 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3793 Work around this problem here. */
3794 if (cu->language == language_cplus
3795 && parent->tag == DW_TAG_namespace
3796 && strcmp (parent->name, "::") == 0
3797 && grandparent_scope == NULL)
3798 {
3799 parent->scope = NULL;
3800 parent->scope_set = 1;
3801 return NULL;
3802 }
3803
3804 if (parent->tag == DW_TAG_namespace
3805 || parent->tag == DW_TAG_module
3806 || parent->tag == DW_TAG_structure_type
3807 || parent->tag == DW_TAG_class_type
3808 || parent->tag == DW_TAG_interface_type
3809 || parent->tag == DW_TAG_union_type
3810 || parent->tag == DW_TAG_enumeration_type)
3811 {
3812 if (grandparent_scope == NULL)
3813 parent->scope = parent->name;
3814 else
3815 parent->scope = typename_concat (&cu->comp_unit_obstack,
3816 grandparent_scope,
3817 parent->name, 0, cu);
3818 }
3819 else if (parent->tag == DW_TAG_enumerator)
3820 /* Enumerators should not get the name of the enumeration as a prefix. */
3821 parent->scope = grandparent_scope;
3822 else
3823 {
3824 /* FIXME drow/2004-04-01: What should we be doing with
3825 function-local names? For partial symbols, we should probably be
3826 ignoring them. */
3827 complaint (&symfile_complaints,
3828 _("unhandled containing DIE tag %d for DIE at %d"),
3829 parent->tag, pdi->offset);
3830 parent->scope = grandparent_scope;
3831 }
3832
3833 parent->scope_set = 1;
3834 return parent->scope;
3835 }
3836
3837 /* Return the fully scoped name associated with PDI, from compilation unit
3838 CU. The result will be allocated with malloc. */
3839 static char *
3840 partial_die_full_name (struct partial_die_info *pdi,
3841 struct dwarf2_cu *cu)
3842 {
3843 char *parent_scope;
3844
3845 /* If this is a template instantiation, we can not work out the
3846 template arguments from partial DIEs. So, unfortunately, we have
3847 to go through the full DIEs. At least any work we do building
3848 types here will be reused if full symbols are loaded later. */
3849 if (pdi->has_template_arguments)
3850 {
3851 fixup_partial_die (pdi, cu);
3852
3853 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
3854 {
3855 struct die_info *die;
3856 struct attribute attr;
3857 struct dwarf2_cu *ref_cu = cu;
3858
3859 attr.name = 0;
3860 attr.form = DW_FORM_ref_addr;
3861 attr.u.addr = pdi->offset;
3862 die = follow_die_ref (NULL, &attr, &ref_cu);
3863
3864 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
3865 }
3866 }
3867
3868 parent_scope = partial_die_parent_scope (pdi, cu);
3869 if (parent_scope == NULL)
3870 return NULL;
3871 else
3872 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
3873 }
3874
3875 static void
3876 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
3877 {
3878 struct objfile *objfile = cu->objfile;
3879 CORE_ADDR addr = 0;
3880 char *actual_name = NULL;
3881 const struct partial_symbol *psym = NULL;
3882 CORE_ADDR baseaddr;
3883 int built_actual_name = 0;
3884
3885 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3886
3887 actual_name = partial_die_full_name (pdi, cu);
3888 if (actual_name)
3889 built_actual_name = 1;
3890
3891 if (actual_name == NULL)
3892 actual_name = pdi->name;
3893
3894 switch (pdi->tag)
3895 {
3896 case DW_TAG_subprogram:
3897 if (pdi->is_external || cu->language == language_ada)
3898 {
3899 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
3900 of the global scope. But in Ada, we want to be able to access
3901 nested procedures globally. So all Ada subprograms are stored
3902 in the global scope. */
3903 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3904 mst_text, objfile); */
3905 add_psymbol_to_list (actual_name, strlen (actual_name),
3906 built_actual_name,
3907 VAR_DOMAIN, LOC_BLOCK,
3908 &objfile->global_psymbols,
3909 0, pdi->lowpc + baseaddr,
3910 cu->language, objfile);
3911 }
3912 else
3913 {
3914 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
3915 mst_file_text, objfile); */
3916 add_psymbol_to_list (actual_name, strlen (actual_name),
3917 built_actual_name,
3918 VAR_DOMAIN, LOC_BLOCK,
3919 &objfile->static_psymbols,
3920 0, pdi->lowpc + baseaddr,
3921 cu->language, objfile);
3922 }
3923 break;
3924 case DW_TAG_constant:
3925 {
3926 struct psymbol_allocation_list *list;
3927
3928 if (pdi->is_external)
3929 list = &objfile->global_psymbols;
3930 else
3931 list = &objfile->static_psymbols;
3932 add_psymbol_to_list (actual_name, strlen (actual_name),
3933 built_actual_name, VAR_DOMAIN, LOC_STATIC,
3934 list, 0, 0, cu->language, objfile);
3935 }
3936 break;
3937 case DW_TAG_variable:
3938 if (pdi->locdesc)
3939 addr = decode_locdesc (pdi->locdesc, cu);
3940
3941 if (pdi->locdesc
3942 && addr == 0
3943 && !dwarf2_per_objfile->has_section_at_zero)
3944 {
3945 /* A global or static variable may also have been stripped
3946 out by the linker if unused, in which case its address
3947 will be nullified; do not add such variables into partial
3948 symbol table then. */
3949 }
3950 else if (pdi->is_external)
3951 {
3952 /* Global Variable.
3953 Don't enter into the minimal symbol tables as there is
3954 a minimal symbol table entry from the ELF symbols already.
3955 Enter into partial symbol table if it has a location
3956 descriptor or a type.
3957 If the location descriptor is missing, new_symbol will create
3958 a LOC_UNRESOLVED symbol, the address of the variable will then
3959 be determined from the minimal symbol table whenever the variable
3960 is referenced.
3961 The address for the partial symbol table entry is not
3962 used by GDB, but it comes in handy for debugging partial symbol
3963 table building. */
3964
3965 if (pdi->locdesc || pdi->has_type)
3966 add_psymbol_to_list (actual_name, strlen (actual_name),
3967 built_actual_name,
3968 VAR_DOMAIN, LOC_STATIC,
3969 &objfile->global_psymbols,
3970 0, addr + baseaddr,
3971 cu->language, objfile);
3972 }
3973 else
3974 {
3975 /* Static Variable. Skip symbols without location descriptors. */
3976 if (pdi->locdesc == NULL)
3977 {
3978 if (built_actual_name)
3979 xfree (actual_name);
3980 return;
3981 }
3982 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
3983 mst_file_data, objfile); */
3984 add_psymbol_to_list (actual_name, strlen (actual_name),
3985 built_actual_name,
3986 VAR_DOMAIN, LOC_STATIC,
3987 &objfile->static_psymbols,
3988 0, addr + baseaddr,
3989 cu->language, objfile);
3990 }
3991 break;
3992 case DW_TAG_typedef:
3993 case DW_TAG_base_type:
3994 case DW_TAG_subrange_type:
3995 add_psymbol_to_list (actual_name, strlen (actual_name),
3996 built_actual_name,
3997 VAR_DOMAIN, LOC_TYPEDEF,
3998 &objfile->static_psymbols,
3999 0, (CORE_ADDR) 0, cu->language, objfile);
4000 break;
4001 case DW_TAG_namespace:
4002 add_psymbol_to_list (actual_name, strlen (actual_name),
4003 built_actual_name,
4004 VAR_DOMAIN, LOC_TYPEDEF,
4005 &objfile->global_psymbols,
4006 0, (CORE_ADDR) 0, cu->language, objfile);
4007 break;
4008 case DW_TAG_class_type:
4009 case DW_TAG_interface_type:
4010 case DW_TAG_structure_type:
4011 case DW_TAG_union_type:
4012 case DW_TAG_enumeration_type:
4013 /* Skip external references. The DWARF standard says in the section
4014 about "Structure, Union, and Class Type Entries": "An incomplete
4015 structure, union or class type is represented by a structure,
4016 union or class entry that does not have a byte size attribute
4017 and that has a DW_AT_declaration attribute." */
4018 if (!pdi->has_byte_size && pdi->is_declaration)
4019 {
4020 if (built_actual_name)
4021 xfree (actual_name);
4022 return;
4023 }
4024
4025 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4026 static vs. global. */
4027 add_psymbol_to_list (actual_name, strlen (actual_name),
4028 built_actual_name,
4029 STRUCT_DOMAIN, LOC_TYPEDEF,
4030 (cu->language == language_cplus
4031 || cu->language == language_java)
4032 ? &objfile->global_psymbols
4033 : &objfile->static_psymbols,
4034 0, (CORE_ADDR) 0, cu->language, objfile);
4035
4036 break;
4037 case DW_TAG_enumerator:
4038 add_psymbol_to_list (actual_name, strlen (actual_name),
4039 built_actual_name,
4040 VAR_DOMAIN, LOC_CONST,
4041 (cu->language == language_cplus
4042 || cu->language == language_java)
4043 ? &objfile->global_psymbols
4044 : &objfile->static_psymbols,
4045 0, (CORE_ADDR) 0, cu->language, objfile);
4046 break;
4047 default:
4048 break;
4049 }
4050
4051 if (built_actual_name)
4052 xfree (actual_name);
4053 }
4054
4055 /* Read a partial die corresponding to a namespace; also, add a symbol
4056 corresponding to that namespace to the symbol table. NAMESPACE is
4057 the name of the enclosing namespace. */
4058
4059 static void
4060 add_partial_namespace (struct partial_die_info *pdi,
4061 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4062 int need_pc, struct dwarf2_cu *cu)
4063 {
4064 /* Add a symbol for the namespace. */
4065
4066 add_partial_symbol (pdi, cu);
4067
4068 /* Now scan partial symbols in that namespace. */
4069
4070 if (pdi->has_children)
4071 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4072 }
4073
4074 /* Read a partial die corresponding to a Fortran module. */
4075
4076 static void
4077 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4078 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4079 {
4080 /* Now scan partial symbols in that module. */
4081
4082 if (pdi->has_children)
4083 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4084 }
4085
4086 /* Read a partial die corresponding to a subprogram and create a partial
4087 symbol for that subprogram. When the CU language allows it, this
4088 routine also defines a partial symbol for each nested subprogram
4089 that this subprogram contains.
4090
4091 DIE my also be a lexical block, in which case we simply search
4092 recursively for suprograms defined inside that lexical block.
4093 Again, this is only performed when the CU language allows this
4094 type of definitions. */
4095
4096 static void
4097 add_partial_subprogram (struct partial_die_info *pdi,
4098 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4099 int need_pc, struct dwarf2_cu *cu)
4100 {
4101 if (pdi->tag == DW_TAG_subprogram)
4102 {
4103 if (pdi->has_pc_info)
4104 {
4105 if (pdi->lowpc < *lowpc)
4106 *lowpc = pdi->lowpc;
4107 if (pdi->highpc > *highpc)
4108 *highpc = pdi->highpc;
4109 if (need_pc)
4110 {
4111 CORE_ADDR baseaddr;
4112 struct objfile *objfile = cu->objfile;
4113
4114 baseaddr = ANOFFSET (objfile->section_offsets,
4115 SECT_OFF_TEXT (objfile));
4116 addrmap_set_empty (objfile->psymtabs_addrmap,
4117 pdi->lowpc + baseaddr,
4118 pdi->highpc - 1 + baseaddr,
4119 cu->per_cu->v.psymtab);
4120 }
4121 if (!pdi->is_declaration)
4122 /* Ignore subprogram DIEs that do not have a name, they are
4123 illegal. Do not emit a complaint at this point, we will
4124 do so when we convert this psymtab into a symtab. */
4125 if (pdi->name)
4126 add_partial_symbol (pdi, cu);
4127 }
4128 }
4129
4130 if (! pdi->has_children)
4131 return;
4132
4133 if (cu->language == language_ada)
4134 {
4135 pdi = pdi->die_child;
4136 while (pdi != NULL)
4137 {
4138 fixup_partial_die (pdi, cu);
4139 if (pdi->tag == DW_TAG_subprogram
4140 || pdi->tag == DW_TAG_lexical_block)
4141 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4142 pdi = pdi->die_sibling;
4143 }
4144 }
4145 }
4146
4147 /* Read a partial die corresponding to an enumeration type. */
4148
4149 static void
4150 add_partial_enumeration (struct partial_die_info *enum_pdi,
4151 struct dwarf2_cu *cu)
4152 {
4153 struct partial_die_info *pdi;
4154
4155 if (enum_pdi->name != NULL)
4156 add_partial_symbol (enum_pdi, cu);
4157
4158 pdi = enum_pdi->die_child;
4159 while (pdi)
4160 {
4161 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4162 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4163 else
4164 add_partial_symbol (pdi, cu);
4165 pdi = pdi->die_sibling;
4166 }
4167 }
4168
4169 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4170 Return the corresponding abbrev, or NULL if the number is zero (indicating
4171 an empty DIE). In either case *BYTES_READ will be set to the length of
4172 the initial number. */
4173
4174 static struct abbrev_info *
4175 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4176 struct dwarf2_cu *cu)
4177 {
4178 bfd *abfd = cu->objfile->obfd;
4179 unsigned int abbrev_number;
4180 struct abbrev_info *abbrev;
4181
4182 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4183
4184 if (abbrev_number == 0)
4185 return NULL;
4186
4187 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4188 if (!abbrev)
4189 {
4190 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4191 abbrev_number, bfd_get_filename (abfd));
4192 }
4193
4194 return abbrev;
4195 }
4196
4197 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4198 Returns a pointer to the end of a series of DIEs, terminated by an empty
4199 DIE. Any children of the skipped DIEs will also be skipped. */
4200
4201 static gdb_byte *
4202 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4203 {
4204 struct abbrev_info *abbrev;
4205 unsigned int bytes_read;
4206
4207 while (1)
4208 {
4209 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4210 if (abbrev == NULL)
4211 return info_ptr + bytes_read;
4212 else
4213 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4214 }
4215 }
4216
4217 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4218 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4219 abbrev corresponding to that skipped uleb128 should be passed in
4220 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4221 children. */
4222
4223 static gdb_byte *
4224 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4225 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4226 {
4227 unsigned int bytes_read;
4228 struct attribute attr;
4229 bfd *abfd = cu->objfile->obfd;
4230 unsigned int form, i;
4231
4232 for (i = 0; i < abbrev->num_attrs; i++)
4233 {
4234 /* The only abbrev we care about is DW_AT_sibling. */
4235 if (abbrev->attrs[i].name == DW_AT_sibling)
4236 {
4237 read_attribute (&attr, &abbrev->attrs[i],
4238 abfd, info_ptr, cu);
4239 if (attr.form == DW_FORM_ref_addr)
4240 complaint (&symfile_complaints,
4241 _("ignoring absolute DW_AT_sibling"));
4242 else
4243 return buffer + dwarf2_get_ref_die_offset (&attr);
4244 }
4245
4246 /* If it isn't DW_AT_sibling, skip this attribute. */
4247 form = abbrev->attrs[i].form;
4248 skip_attribute:
4249 switch (form)
4250 {
4251 case DW_FORM_ref_addr:
4252 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4253 and later it is offset sized. */
4254 if (cu->header.version == 2)
4255 info_ptr += cu->header.addr_size;
4256 else
4257 info_ptr += cu->header.offset_size;
4258 break;
4259 case DW_FORM_addr:
4260 info_ptr += cu->header.addr_size;
4261 break;
4262 case DW_FORM_data1:
4263 case DW_FORM_ref1:
4264 case DW_FORM_flag:
4265 info_ptr += 1;
4266 break;
4267 case DW_FORM_flag_present:
4268 break;
4269 case DW_FORM_data2:
4270 case DW_FORM_ref2:
4271 info_ptr += 2;
4272 break;
4273 case DW_FORM_data4:
4274 case DW_FORM_ref4:
4275 info_ptr += 4;
4276 break;
4277 case DW_FORM_data8:
4278 case DW_FORM_ref8:
4279 case DW_FORM_ref_sig8:
4280 info_ptr += 8;
4281 break;
4282 case DW_FORM_string:
4283 read_direct_string (abfd, info_ptr, &bytes_read);
4284 info_ptr += bytes_read;
4285 break;
4286 case DW_FORM_sec_offset:
4287 case DW_FORM_strp:
4288 info_ptr += cu->header.offset_size;
4289 break;
4290 case DW_FORM_exprloc:
4291 case DW_FORM_block:
4292 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4293 info_ptr += bytes_read;
4294 break;
4295 case DW_FORM_block1:
4296 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4297 break;
4298 case DW_FORM_block2:
4299 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4300 break;
4301 case DW_FORM_block4:
4302 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4303 break;
4304 case DW_FORM_sdata:
4305 case DW_FORM_udata:
4306 case DW_FORM_ref_udata:
4307 info_ptr = skip_leb128 (abfd, info_ptr);
4308 break;
4309 case DW_FORM_indirect:
4310 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4311 info_ptr += bytes_read;
4312 /* We need to continue parsing from here, so just go back to
4313 the top. */
4314 goto skip_attribute;
4315
4316 default:
4317 error (_("Dwarf Error: Cannot handle %s "
4318 "in DWARF reader [in module %s]"),
4319 dwarf_form_name (form),
4320 bfd_get_filename (abfd));
4321 }
4322 }
4323
4324 if (abbrev->has_children)
4325 return skip_children (buffer, info_ptr, cu);
4326 else
4327 return info_ptr;
4328 }
4329
4330 /* Locate ORIG_PDI's sibling.
4331 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4332 in BUFFER. */
4333
4334 static gdb_byte *
4335 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4336 gdb_byte *buffer, gdb_byte *info_ptr,
4337 bfd *abfd, struct dwarf2_cu *cu)
4338 {
4339 /* Do we know the sibling already? */
4340
4341 if (orig_pdi->sibling)
4342 return orig_pdi->sibling;
4343
4344 /* Are there any children to deal with? */
4345
4346 if (!orig_pdi->has_children)
4347 return info_ptr;
4348
4349 /* Skip the children the long way. */
4350
4351 return skip_children (buffer, info_ptr, cu);
4352 }
4353
4354 /* Expand this partial symbol table into a full symbol table. */
4355
4356 static void
4357 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4358 {
4359 if (pst != NULL)
4360 {
4361 if (pst->readin)
4362 {
4363 warning (_("bug: psymtab for %s is already read in."),
4364 pst->filename);
4365 }
4366 else
4367 {
4368 if (info_verbose)
4369 {
4370 printf_filtered (_("Reading in symbols for %s..."),
4371 pst->filename);
4372 gdb_flush (gdb_stdout);
4373 }
4374
4375 /* Restore our global data. */
4376 dwarf2_per_objfile = objfile_data (pst->objfile,
4377 dwarf2_objfile_data_key);
4378
4379 /* If this psymtab is constructed from a debug-only objfile, the
4380 has_section_at_zero flag will not necessarily be correct. We
4381 can get the correct value for this flag by looking at the data
4382 associated with the (presumably stripped) associated objfile. */
4383 if (pst->objfile->separate_debug_objfile_backlink)
4384 {
4385 struct dwarf2_per_objfile *dpo_backlink
4386 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4387 dwarf2_objfile_data_key);
4388
4389 dwarf2_per_objfile->has_section_at_zero
4390 = dpo_backlink->has_section_at_zero;
4391 }
4392
4393 dwarf2_per_objfile->reading_partial_symbols = 0;
4394
4395 psymtab_to_symtab_1 (pst);
4396
4397 /* Finish up the debug error message. */
4398 if (info_verbose)
4399 printf_filtered (_("done.\n"));
4400 }
4401 }
4402 }
4403
4404 /* Add PER_CU to the queue. */
4405
4406 static void
4407 queue_comp_unit (struct dwarf2_per_cu_data *per_cu, struct objfile *objfile)
4408 {
4409 struct dwarf2_queue_item *item;
4410
4411 per_cu->queued = 1;
4412 item = xmalloc (sizeof (*item));
4413 item->per_cu = per_cu;
4414 item->next = NULL;
4415
4416 if (dwarf2_queue == NULL)
4417 dwarf2_queue = item;
4418 else
4419 dwarf2_queue_tail->next = item;
4420
4421 dwarf2_queue_tail = item;
4422 }
4423
4424 /* Process the queue. */
4425
4426 static void
4427 process_queue (struct objfile *objfile)
4428 {
4429 struct dwarf2_queue_item *item, *next_item;
4430
4431 /* The queue starts out with one item, but following a DIE reference
4432 may load a new CU, adding it to the end of the queue. */
4433 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4434 {
4435 if (dwarf2_per_objfile->using_index
4436 ? !item->per_cu->v.quick->symtab
4437 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4438 process_full_comp_unit (item->per_cu);
4439
4440 item->per_cu->queued = 0;
4441 next_item = item->next;
4442 xfree (item);
4443 }
4444
4445 dwarf2_queue_tail = NULL;
4446 }
4447
4448 /* Free all allocated queue entries. This function only releases anything if
4449 an error was thrown; if the queue was processed then it would have been
4450 freed as we went along. */
4451
4452 static void
4453 dwarf2_release_queue (void *dummy)
4454 {
4455 struct dwarf2_queue_item *item, *last;
4456
4457 item = dwarf2_queue;
4458 while (item)
4459 {
4460 /* Anything still marked queued is likely to be in an
4461 inconsistent state, so discard it. */
4462 if (item->per_cu->queued)
4463 {
4464 if (item->per_cu->cu != NULL)
4465 free_one_cached_comp_unit (item->per_cu->cu);
4466 item->per_cu->queued = 0;
4467 }
4468
4469 last = item;
4470 item = item->next;
4471 xfree (last);
4472 }
4473
4474 dwarf2_queue = dwarf2_queue_tail = NULL;
4475 }
4476
4477 /* Read in full symbols for PST, and anything it depends on. */
4478
4479 static void
4480 psymtab_to_symtab_1 (struct partial_symtab *pst)
4481 {
4482 struct dwarf2_per_cu_data *per_cu;
4483 struct cleanup *back_to;
4484 int i;
4485
4486 for (i = 0; i < pst->number_of_dependencies; i++)
4487 if (!pst->dependencies[i]->readin)
4488 {
4489 /* Inform about additional files that need to be read in. */
4490 if (info_verbose)
4491 {
4492 /* FIXME: i18n: Need to make this a single string. */
4493 fputs_filtered (" ", gdb_stdout);
4494 wrap_here ("");
4495 fputs_filtered ("and ", gdb_stdout);
4496 wrap_here ("");
4497 printf_filtered ("%s...", pst->dependencies[i]->filename);
4498 wrap_here (""); /* Flush output. */
4499 gdb_flush (gdb_stdout);
4500 }
4501 psymtab_to_symtab_1 (pst->dependencies[i]);
4502 }
4503
4504 per_cu = pst->read_symtab_private;
4505
4506 if (per_cu == NULL)
4507 {
4508 /* It's an include file, no symbols to read for it.
4509 Everything is in the parent symtab. */
4510 pst->readin = 1;
4511 return;
4512 }
4513
4514 dw2_do_instantiate_symtab (pst->objfile, per_cu);
4515 }
4516
4517 /* Load the DIEs associated with PER_CU into memory. */
4518
4519 static void
4520 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu,
4521 struct objfile *objfile)
4522 {
4523 bfd *abfd = objfile->obfd;
4524 struct dwarf2_cu *cu;
4525 unsigned int offset;
4526 gdb_byte *info_ptr, *beg_of_comp_unit;
4527 struct cleanup *free_abbrevs_cleanup = NULL, *free_cu_cleanup = NULL;
4528 struct attribute *attr;
4529 int read_cu = 0;
4530
4531 gdb_assert (! per_cu->from_debug_types);
4532
4533 /* Set local variables from the partial symbol table info. */
4534 offset = per_cu->offset;
4535
4536 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4537 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4538 beg_of_comp_unit = info_ptr;
4539
4540 if (per_cu->cu == NULL)
4541 {
4542 cu = xmalloc (sizeof (*cu));
4543 init_one_comp_unit (cu, objfile);
4544
4545 read_cu = 1;
4546
4547 /* If an error occurs while loading, release our storage. */
4548 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
4549
4550 /* Read in the comp_unit header. */
4551 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4552
4553 /* Complete the cu_header. */
4554 cu->header.offset = offset;
4555 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4556
4557 /* Read the abbrevs for this compilation unit. */
4558 dwarf2_read_abbrevs (abfd, cu);
4559 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
4560
4561 /* Link this compilation unit into the compilation unit tree. */
4562 per_cu->cu = cu;
4563 cu->per_cu = per_cu;
4564
4565 /* Link this CU into read_in_chain. */
4566 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4567 dwarf2_per_objfile->read_in_chain = per_cu;
4568 }
4569 else
4570 {
4571 cu = per_cu->cu;
4572 info_ptr += cu->header.first_die_offset;
4573 }
4574
4575 cu->dies = read_comp_unit (info_ptr, cu);
4576
4577 /* We try not to read any attributes in this function, because not
4578 all objfiles needed for references have been loaded yet, and symbol
4579 table processing isn't initialized. But we have to set the CU language,
4580 or we won't be able to build types correctly. */
4581 prepare_one_comp_unit (cu, cu->dies);
4582
4583 /* Similarly, if we do not read the producer, we can not apply
4584 producer-specific interpretation. */
4585 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4586 if (attr)
4587 cu->producer = DW_STRING (attr);
4588
4589 if (read_cu)
4590 {
4591 do_cleanups (free_abbrevs_cleanup);
4592
4593 /* We've successfully allocated this compilation unit. Let our
4594 caller clean it up when finished with it. */
4595 discard_cleanups (free_cu_cleanup);
4596 }
4597 }
4598
4599 /* Add a DIE to the delayed physname list. */
4600
4601 static void
4602 add_to_method_list (struct type *type, int fnfield_index, int index,
4603 const char *name, struct die_info *die,
4604 struct dwarf2_cu *cu)
4605 {
4606 struct delayed_method_info mi;
4607 mi.type = type;
4608 mi.fnfield_index = fnfield_index;
4609 mi.index = index;
4610 mi.name = name;
4611 mi.die = die;
4612 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4613 }
4614
4615 /* A cleanup for freeing the delayed method list. */
4616
4617 static void
4618 free_delayed_list (void *ptr)
4619 {
4620 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4621 if (cu->method_list != NULL)
4622 {
4623 VEC_free (delayed_method_info, cu->method_list);
4624 cu->method_list = NULL;
4625 }
4626 }
4627
4628 /* Compute the physnames of any methods on the CU's method list.
4629
4630 The computation of method physnames is delayed in order to avoid the
4631 (bad) condition that one of the method's formal parameters is of an as yet
4632 incomplete type. */
4633
4634 static void
4635 compute_delayed_physnames (struct dwarf2_cu *cu)
4636 {
4637 int i;
4638 struct delayed_method_info *mi;
4639 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4640 {
4641 const char *physname;
4642 struct fn_fieldlist *fn_flp
4643 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4644 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4645 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4646 }
4647 }
4648
4649 /* Check for GCC >= 4.x. Return minor version (x) of 4.x in such case. If it
4650 is not GCC or it is GCC older than 4.x return -1. If it is GCC 5.x or
4651 higher return INT_MAX. */
4652
4653 static int
4654 producer_is_gcc_ge_4 (struct dwarf2_cu *cu)
4655 {
4656 const char *cs;
4657 int major, minor;
4658
4659 if (cu->producer == NULL)
4660 {
4661 /* For unknown compilers expect their behavior is not compliant. For GCC
4662 this case can also happen for -gdwarf-4 type units supported since
4663 gcc-4.5. */
4664
4665 return -1;
4666 }
4667
4668 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
4669
4670 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
4671 {
4672 /* For non-GCC compilers expect their behavior is not compliant. */
4673
4674 return -1;
4675 }
4676 cs = &cu->producer[strlen ("GNU ")];
4677 while (*cs && !isdigit (*cs))
4678 cs++;
4679 if (sscanf (cs, "%d.%d", &major, &minor) != 2)
4680 {
4681 /* Not recognized as GCC. */
4682
4683 return -1;
4684 }
4685
4686 if (major < 4)
4687 return -1;
4688 if (major > 4)
4689 return INT_MAX;
4690 return minor;
4691 }
4692
4693 /* Generate full symbol information for PST and CU, whose DIEs have
4694 already been loaded into memory. */
4695
4696 static void
4697 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4698 {
4699 struct dwarf2_cu *cu = per_cu->cu;
4700 struct objfile *objfile = per_cu->objfile;
4701 CORE_ADDR lowpc, highpc;
4702 struct symtab *symtab;
4703 struct cleanup *back_to, *delayed_list_cleanup;
4704 CORE_ADDR baseaddr;
4705
4706 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4707
4708 buildsym_init ();
4709 back_to = make_cleanup (really_free_pendings, NULL);
4710 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4711
4712 cu->list_in_scope = &file_symbols;
4713
4714 dwarf2_find_base_address (cu->dies, cu);
4715
4716 /* Do line number decoding in read_file_scope () */
4717 process_die (cu->dies, cu);
4718
4719 /* Now that we have processed all the DIEs in the CU, all the types
4720 should be complete, and it should now be safe to compute all of the
4721 physnames. */
4722 compute_delayed_physnames (cu);
4723 do_cleanups (delayed_list_cleanup);
4724
4725 /* Some compilers don't define a DW_AT_high_pc attribute for the
4726 compilation unit. If the DW_AT_high_pc is missing, synthesize
4727 it, by scanning the DIE's below the compilation unit. */
4728 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4729
4730 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4731
4732 if (symtab != NULL)
4733 {
4734 int gcc_4_minor = producer_is_gcc_ge_4 (cu);
4735
4736 /* Set symtab language to language from DW_AT_language. If the
4737 compilation is from a C file generated by language preprocessors, do
4738 not set the language if it was already deduced by start_subfile. */
4739 if (!(cu->language == language_c && symtab->language != language_c))
4740 symtab->language = cu->language;
4741
4742 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4743 produce DW_AT_location with location lists but it can be possibly
4744 invalid without -fvar-tracking.
4745
4746 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4747 needed, it would be wrong due to missing DW_AT_producer there.
4748
4749 Still one can confuse GDB by using non-standard GCC compilation
4750 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4751 */
4752 if (cu->has_loclist && gcc_4_minor >= 0)
4753 symtab->locations_valid = 1;
4754
4755 if (gcc_4_minor >= 5)
4756 symtab->epilogue_unwind_valid = 1;
4757 }
4758
4759 if (dwarf2_per_objfile->using_index)
4760 per_cu->v.quick->symtab = symtab;
4761 else
4762 {
4763 struct partial_symtab *pst = per_cu->v.psymtab;
4764 pst->symtab = symtab;
4765 pst->readin = 1;
4766 }
4767
4768 do_cleanups (back_to);
4769 }
4770
4771 /* Process a die and its children. */
4772
4773 static void
4774 process_die (struct die_info *die, struct dwarf2_cu *cu)
4775 {
4776 switch (die->tag)
4777 {
4778 case DW_TAG_padding:
4779 break;
4780 case DW_TAG_compile_unit:
4781 read_file_scope (die, cu);
4782 break;
4783 case DW_TAG_type_unit:
4784 read_type_unit_scope (die, cu);
4785 break;
4786 case DW_TAG_subprogram:
4787 case DW_TAG_inlined_subroutine:
4788 read_func_scope (die, cu);
4789 break;
4790 case DW_TAG_lexical_block:
4791 case DW_TAG_try_block:
4792 case DW_TAG_catch_block:
4793 read_lexical_block_scope (die, cu);
4794 break;
4795 case DW_TAG_class_type:
4796 case DW_TAG_interface_type:
4797 case DW_TAG_structure_type:
4798 case DW_TAG_union_type:
4799 process_structure_scope (die, cu);
4800 break;
4801 case DW_TAG_enumeration_type:
4802 process_enumeration_scope (die, cu);
4803 break;
4804
4805 /* These dies have a type, but processing them does not create
4806 a symbol or recurse to process the children. Therefore we can
4807 read them on-demand through read_type_die. */
4808 case DW_TAG_subroutine_type:
4809 case DW_TAG_set_type:
4810 case DW_TAG_array_type:
4811 case DW_TAG_pointer_type:
4812 case DW_TAG_ptr_to_member_type:
4813 case DW_TAG_reference_type:
4814 case DW_TAG_string_type:
4815 break;
4816
4817 case DW_TAG_base_type:
4818 case DW_TAG_subrange_type:
4819 case DW_TAG_typedef:
4820 /* Add a typedef symbol for the type definition, if it has a
4821 DW_AT_name. */
4822 new_symbol (die, read_type_die (die, cu), cu);
4823 break;
4824 case DW_TAG_common_block:
4825 read_common_block (die, cu);
4826 break;
4827 case DW_TAG_common_inclusion:
4828 break;
4829 case DW_TAG_namespace:
4830 processing_has_namespace_info = 1;
4831 read_namespace (die, cu);
4832 break;
4833 case DW_TAG_module:
4834 processing_has_namespace_info = 1;
4835 read_module (die, cu);
4836 break;
4837 case DW_TAG_imported_declaration:
4838 case DW_TAG_imported_module:
4839 processing_has_namespace_info = 1;
4840 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4841 || cu->language != language_fortran))
4842 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4843 dwarf_tag_name (die->tag));
4844 read_import_statement (die, cu);
4845 break;
4846 default:
4847 new_symbol (die, NULL, cu);
4848 break;
4849 }
4850 }
4851
4852 /* A helper function for dwarf2_compute_name which determines whether DIE
4853 needs to have the name of the scope prepended to the name listed in the
4854 die. */
4855
4856 static int
4857 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4858 {
4859 struct attribute *attr;
4860
4861 switch (die->tag)
4862 {
4863 case DW_TAG_namespace:
4864 case DW_TAG_typedef:
4865 case DW_TAG_class_type:
4866 case DW_TAG_interface_type:
4867 case DW_TAG_structure_type:
4868 case DW_TAG_union_type:
4869 case DW_TAG_enumeration_type:
4870 case DW_TAG_enumerator:
4871 case DW_TAG_subprogram:
4872 case DW_TAG_member:
4873 return 1;
4874
4875 case DW_TAG_variable:
4876 case DW_TAG_constant:
4877 /* We only need to prefix "globally" visible variables. These include
4878 any variable marked with DW_AT_external or any variable that
4879 lives in a namespace. [Variables in anonymous namespaces
4880 require prefixing, but they are not DW_AT_external.] */
4881
4882 if (dwarf2_attr (die, DW_AT_specification, cu))
4883 {
4884 struct dwarf2_cu *spec_cu = cu;
4885
4886 return die_needs_namespace (die_specification (die, &spec_cu),
4887 spec_cu);
4888 }
4889
4890 attr = dwarf2_attr (die, DW_AT_external, cu);
4891 if (attr == NULL && die->parent->tag != DW_TAG_namespace
4892 && die->parent->tag != DW_TAG_module)
4893 return 0;
4894 /* A variable in a lexical block of some kind does not need a
4895 namespace, even though in C++ such variables may be external
4896 and have a mangled name. */
4897 if (die->parent->tag == DW_TAG_lexical_block
4898 || die->parent->tag == DW_TAG_try_block
4899 || die->parent->tag == DW_TAG_catch_block
4900 || die->parent->tag == DW_TAG_subprogram)
4901 return 0;
4902 return 1;
4903
4904 default:
4905 return 0;
4906 }
4907 }
4908
4909 /* Retrieve the last character from a mem_file. */
4910
4911 static void
4912 do_ui_file_peek_last (void *object, const char *buffer, long length)
4913 {
4914 char *last_char_p = (char *) object;
4915
4916 if (length > 0)
4917 *last_char_p = buffer[length - 1];
4918 }
4919
4920 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
4921 compute the physname for the object, which include a method's
4922 formal parameters (C++/Java) and return type (Java).
4923
4924 For Ada, return the DIE's linkage name rather than the fully qualified
4925 name. PHYSNAME is ignored..
4926
4927 The result is allocated on the objfile_obstack and canonicalized. */
4928
4929 static const char *
4930 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
4931 int physname)
4932 {
4933 if (name == NULL)
4934 name = dwarf2_name (die, cu);
4935
4936 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
4937 compute it by typename_concat inside GDB. */
4938 if (cu->language == language_ada
4939 || (cu->language == language_fortran && physname))
4940 {
4941 /* For Ada unit, we prefer the linkage name over the name, as
4942 the former contains the exported name, which the user expects
4943 to be able to reference. Ideally, we want the user to be able
4944 to reference this entity using either natural or linkage name,
4945 but we haven't started looking at this enhancement yet. */
4946 struct attribute *attr;
4947
4948 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
4949 if (attr == NULL)
4950 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
4951 if (attr && DW_STRING (attr))
4952 return DW_STRING (attr);
4953 }
4954
4955 /* These are the only languages we know how to qualify names in. */
4956 if (name != NULL
4957 && (cu->language == language_cplus || cu->language == language_java
4958 || cu->language == language_fortran))
4959 {
4960 if (die_needs_namespace (die, cu))
4961 {
4962 long length;
4963 char *prefix;
4964 struct ui_file *buf;
4965
4966 prefix = determine_prefix (die, cu);
4967 buf = mem_fileopen ();
4968 if (*prefix != '\0')
4969 {
4970 char *prefixed_name = typename_concat (NULL, prefix, name,
4971 physname, cu);
4972
4973 fputs_unfiltered (prefixed_name, buf);
4974 xfree (prefixed_name);
4975 }
4976 else
4977 fputs_unfiltered (name, buf);
4978
4979 /* Template parameters may be specified in the DIE's DW_AT_name, or
4980 as children with DW_TAG_template_type_param or
4981 DW_TAG_value_type_param. If the latter, add them to the name
4982 here. If the name already has template parameters, then
4983 skip this step; some versions of GCC emit both, and
4984 it is more efficient to use the pre-computed name.
4985
4986 Something to keep in mind about this process: it is very
4987 unlikely, or in some cases downright impossible, to produce
4988 something that will match the mangled name of a function.
4989 If the definition of the function has the same debug info,
4990 we should be able to match up with it anyway. But fallbacks
4991 using the minimal symbol, for instance to find a method
4992 implemented in a stripped copy of libstdc++, will not work.
4993 If we do not have debug info for the definition, we will have to
4994 match them up some other way.
4995
4996 When we do name matching there is a related problem with function
4997 templates; two instantiated function templates are allowed to
4998 differ only by their return types, which we do not add here. */
4999
5000 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5001 {
5002 struct attribute *attr;
5003 struct die_info *child;
5004 int first = 1;
5005
5006 die->building_fullname = 1;
5007
5008 for (child = die->child; child != NULL; child = child->sibling)
5009 {
5010 struct type *type;
5011 long value;
5012 gdb_byte *bytes;
5013 struct dwarf2_locexpr_baton *baton;
5014 struct value *v;
5015
5016 if (child->tag != DW_TAG_template_type_param
5017 && child->tag != DW_TAG_template_value_param)
5018 continue;
5019
5020 if (first)
5021 {
5022 fputs_unfiltered ("<", buf);
5023 first = 0;
5024 }
5025 else
5026 fputs_unfiltered (", ", buf);
5027
5028 attr = dwarf2_attr (child, DW_AT_type, cu);
5029 if (attr == NULL)
5030 {
5031 complaint (&symfile_complaints,
5032 _("template parameter missing DW_AT_type"));
5033 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5034 continue;
5035 }
5036 type = die_type (child, cu);
5037
5038 if (child->tag == DW_TAG_template_type_param)
5039 {
5040 c_print_type (type, "", buf, -1, 0);
5041 continue;
5042 }
5043
5044 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5045 if (attr == NULL)
5046 {
5047 complaint (&symfile_complaints,
5048 _("template parameter missing "
5049 "DW_AT_const_value"));
5050 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5051 continue;
5052 }
5053
5054 dwarf2_const_value_attr (attr, type, name,
5055 &cu->comp_unit_obstack, cu,
5056 &value, &bytes, &baton);
5057
5058 if (TYPE_NOSIGN (type))
5059 /* GDB prints characters as NUMBER 'CHAR'. If that's
5060 changed, this can use value_print instead. */
5061 c_printchar (value, type, buf);
5062 else
5063 {
5064 struct value_print_options opts;
5065
5066 if (baton != NULL)
5067 v = dwarf2_evaluate_loc_desc (type, NULL,
5068 baton->data,
5069 baton->size,
5070 baton->per_cu);
5071 else if (bytes != NULL)
5072 {
5073 v = allocate_value (type);
5074 memcpy (value_contents_writeable (v), bytes,
5075 TYPE_LENGTH (type));
5076 }
5077 else
5078 v = value_from_longest (type, value);
5079
5080 /* Specify decimal so that we do not depend on
5081 the radix. */
5082 get_formatted_print_options (&opts, 'd');
5083 opts.raw = 1;
5084 value_print (v, buf, &opts);
5085 release_value (v);
5086 value_free (v);
5087 }
5088 }
5089
5090 die->building_fullname = 0;
5091
5092 if (!first)
5093 {
5094 /* Close the argument list, with a space if necessary
5095 (nested templates). */
5096 char last_char = '\0';
5097 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5098 if (last_char == '>')
5099 fputs_unfiltered (" >", buf);
5100 else
5101 fputs_unfiltered (">", buf);
5102 }
5103 }
5104
5105 /* For Java and C++ methods, append formal parameter type
5106 information, if PHYSNAME. */
5107
5108 if (physname && die->tag == DW_TAG_subprogram
5109 && (cu->language == language_cplus
5110 || cu->language == language_java))
5111 {
5112 struct type *type = read_type_die (die, cu);
5113
5114 c_type_print_args (type, buf, 1, cu->language);
5115
5116 if (cu->language == language_java)
5117 {
5118 /* For java, we must append the return type to method
5119 names. */
5120 if (die->tag == DW_TAG_subprogram)
5121 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5122 0, 0);
5123 }
5124 else if (cu->language == language_cplus)
5125 {
5126 /* Assume that an artificial first parameter is
5127 "this", but do not crash if it is not. RealView
5128 marks unnamed (and thus unused) parameters as
5129 artificial; there is no way to differentiate
5130 the two cases. */
5131 if (TYPE_NFIELDS (type) > 0
5132 && TYPE_FIELD_ARTIFICIAL (type, 0)
5133 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5134 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5135 0))))
5136 fputs_unfiltered (" const", buf);
5137 }
5138 }
5139
5140 name = ui_file_obsavestring (buf, &cu->objfile->objfile_obstack,
5141 &length);
5142 ui_file_delete (buf);
5143
5144 if (cu->language == language_cplus)
5145 {
5146 char *cname
5147 = dwarf2_canonicalize_name (name, cu,
5148 &cu->objfile->objfile_obstack);
5149
5150 if (cname != NULL)
5151 name = cname;
5152 }
5153 }
5154 }
5155
5156 return name;
5157 }
5158
5159 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5160 If scope qualifiers are appropriate they will be added. The result
5161 will be allocated on the objfile_obstack, or NULL if the DIE does
5162 not have a name. NAME may either be from a previous call to
5163 dwarf2_name or NULL.
5164
5165 The output string will be canonicalized (if C++/Java). */
5166
5167 static const char *
5168 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5169 {
5170 return dwarf2_compute_name (name, die, cu, 0);
5171 }
5172
5173 /* Construct a physname for the given DIE in CU. NAME may either be
5174 from a previous call to dwarf2_name or NULL. The result will be
5175 allocated on the objfile_objstack or NULL if the DIE does not have a
5176 name.
5177
5178 The output string will be canonicalized (if C++/Java). */
5179
5180 static const char *
5181 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5182 {
5183 return dwarf2_compute_name (name, die, cu, 1);
5184 }
5185
5186 /* Read the import statement specified by the given die and record it. */
5187
5188 static void
5189 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5190 {
5191 struct attribute *import_attr;
5192 struct die_info *imported_die, *child_die;
5193 struct dwarf2_cu *imported_cu;
5194 const char *imported_name;
5195 const char *imported_name_prefix;
5196 const char *canonical_name;
5197 const char *import_alias;
5198 const char *imported_declaration = NULL;
5199 const char *import_prefix;
5200 VEC (const_char_ptr) *excludes = NULL;
5201 struct cleanup *cleanups;
5202
5203 char *temp;
5204
5205 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5206 if (import_attr == NULL)
5207 {
5208 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5209 dwarf_tag_name (die->tag));
5210 return;
5211 }
5212
5213 imported_cu = cu;
5214 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5215 imported_name = dwarf2_name (imported_die, imported_cu);
5216 if (imported_name == NULL)
5217 {
5218 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5219
5220 The import in the following code:
5221 namespace A
5222 {
5223 typedef int B;
5224 }
5225
5226 int main ()
5227 {
5228 using A::B;
5229 B b;
5230 return b;
5231 }
5232
5233 ...
5234 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5235 <52> DW_AT_decl_file : 1
5236 <53> DW_AT_decl_line : 6
5237 <54> DW_AT_import : <0x75>
5238 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5239 <59> DW_AT_name : B
5240 <5b> DW_AT_decl_file : 1
5241 <5c> DW_AT_decl_line : 2
5242 <5d> DW_AT_type : <0x6e>
5243 ...
5244 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5245 <76> DW_AT_byte_size : 4
5246 <77> DW_AT_encoding : 5 (signed)
5247
5248 imports the wrong die ( 0x75 instead of 0x58 ).
5249 This case will be ignored until the gcc bug is fixed. */
5250 return;
5251 }
5252
5253 /* Figure out the local name after import. */
5254 import_alias = dwarf2_name (die, cu);
5255
5256 /* Figure out where the statement is being imported to. */
5257 import_prefix = determine_prefix (die, cu);
5258
5259 /* Figure out what the scope of the imported die is and prepend it
5260 to the name of the imported die. */
5261 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5262
5263 if (imported_die->tag != DW_TAG_namespace
5264 && imported_die->tag != DW_TAG_module)
5265 {
5266 imported_declaration = imported_name;
5267 canonical_name = imported_name_prefix;
5268 }
5269 else if (strlen (imported_name_prefix) > 0)
5270 {
5271 temp = alloca (strlen (imported_name_prefix)
5272 + 2 + strlen (imported_name) + 1);
5273 strcpy (temp, imported_name_prefix);
5274 strcat (temp, "::");
5275 strcat (temp, imported_name);
5276 canonical_name = temp;
5277 }
5278 else
5279 canonical_name = imported_name;
5280
5281 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5282
5283 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5284 for (child_die = die->child; child_die && child_die->tag;
5285 child_die = sibling_die (child_die))
5286 {
5287 /* DWARF-4: A Fortran use statement with a “rename list” may be
5288 represented by an imported module entry with an import attribute
5289 referring to the module and owned entries corresponding to those
5290 entities that are renamed as part of being imported. */
5291
5292 if (child_die->tag != DW_TAG_imported_declaration)
5293 {
5294 complaint (&symfile_complaints,
5295 _("child DW_TAG_imported_declaration expected "
5296 "- DIE at 0x%x [in module %s]"),
5297 child_die->offset, cu->objfile->name);
5298 continue;
5299 }
5300
5301 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5302 if (import_attr == NULL)
5303 {
5304 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5305 dwarf_tag_name (child_die->tag));
5306 continue;
5307 }
5308
5309 imported_cu = cu;
5310 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5311 &imported_cu);
5312 imported_name = dwarf2_name (imported_die, imported_cu);
5313 if (imported_name == NULL)
5314 {
5315 complaint (&symfile_complaints,
5316 _("child DW_TAG_imported_declaration has unknown "
5317 "imported name - DIE at 0x%x [in module %s]"),
5318 child_die->offset, cu->objfile->name);
5319 continue;
5320 }
5321
5322 VEC_safe_push (const_char_ptr, excludes, imported_name);
5323
5324 process_die (child_die, cu);
5325 }
5326
5327 cp_add_using_directive (import_prefix,
5328 canonical_name,
5329 import_alias,
5330 imported_declaration,
5331 excludes,
5332 &cu->objfile->objfile_obstack);
5333
5334 do_cleanups (cleanups);
5335 }
5336
5337 static void
5338 initialize_cu_func_list (struct dwarf2_cu *cu)
5339 {
5340 cu->first_fn = cu->last_fn = cu->cached_fn = NULL;
5341 }
5342
5343 /* Cleanup function for read_file_scope. */
5344
5345 static void
5346 free_cu_line_header (void *arg)
5347 {
5348 struct dwarf2_cu *cu = arg;
5349
5350 free_line_header (cu->line_header);
5351 cu->line_header = NULL;
5352 }
5353
5354 static void
5355 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5356 char **name, char **comp_dir)
5357 {
5358 struct attribute *attr;
5359
5360 *name = NULL;
5361 *comp_dir = NULL;
5362
5363 /* Find the filename. Do not use dwarf2_name here, since the filename
5364 is not a source language identifier. */
5365 attr = dwarf2_attr (die, DW_AT_name, cu);
5366 if (attr)
5367 {
5368 *name = DW_STRING (attr);
5369 }
5370
5371 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5372 if (attr)
5373 *comp_dir = DW_STRING (attr);
5374 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5375 {
5376 *comp_dir = ldirname (*name);
5377 if (*comp_dir != NULL)
5378 make_cleanup (xfree, *comp_dir);
5379 }
5380 if (*comp_dir != NULL)
5381 {
5382 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5383 directory, get rid of it. */
5384 char *cp = strchr (*comp_dir, ':');
5385
5386 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5387 *comp_dir = cp + 1;
5388 }
5389
5390 if (*name == NULL)
5391 *name = "<unknown>";
5392 }
5393
5394 /* Process DW_TAG_compile_unit. */
5395
5396 static void
5397 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5398 {
5399 struct objfile *objfile = cu->objfile;
5400 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5401 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5402 CORE_ADDR highpc = ((CORE_ADDR) 0);
5403 struct attribute *attr;
5404 char *name = NULL;
5405 char *comp_dir = NULL;
5406 struct die_info *child_die;
5407 bfd *abfd = objfile->obfd;
5408 struct line_header *line_header = 0;
5409 CORE_ADDR baseaddr;
5410
5411 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5412
5413 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5414
5415 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5416 from finish_block. */
5417 if (lowpc == ((CORE_ADDR) -1))
5418 lowpc = highpc;
5419 lowpc += baseaddr;
5420 highpc += baseaddr;
5421
5422 find_file_and_directory (die, cu, &name, &comp_dir);
5423
5424 attr = dwarf2_attr (die, DW_AT_language, cu);
5425 if (attr)
5426 {
5427 set_cu_language (DW_UNSND (attr), cu);
5428 }
5429
5430 attr = dwarf2_attr (die, DW_AT_producer, cu);
5431 if (attr)
5432 cu->producer = DW_STRING (attr);
5433
5434 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5435 standardised yet. As a workaround for the language detection we fall
5436 back to the DW_AT_producer string. */
5437 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5438 cu->language = language_opencl;
5439
5440 /* We assume that we're processing GCC output. */
5441 processing_gcc_compilation = 2;
5442
5443 processing_has_namespace_info = 0;
5444
5445 start_symtab (name, comp_dir, lowpc);
5446 record_debugformat ("DWARF 2");
5447 record_producer (cu->producer);
5448
5449 initialize_cu_func_list (cu);
5450
5451 /* Decode line number information if present. We do this before
5452 processing child DIEs, so that the line header table is available
5453 for DW_AT_decl_file. */
5454 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5455 if (attr)
5456 {
5457 unsigned int line_offset = DW_UNSND (attr);
5458 line_header = dwarf_decode_line_header (line_offset, abfd, cu);
5459 if (line_header)
5460 {
5461 cu->line_header = line_header;
5462 make_cleanup (free_cu_line_header, cu);
5463 dwarf_decode_lines (line_header, comp_dir, abfd, cu, NULL);
5464 }
5465 }
5466
5467 /* Process all dies in compilation unit. */
5468 if (die->child != NULL)
5469 {
5470 child_die = die->child;
5471 while (child_die && child_die->tag)
5472 {
5473 process_die (child_die, cu);
5474 child_die = sibling_die (child_die);
5475 }
5476 }
5477
5478 /* Decode macro information, if present. Dwarf 2 macro information
5479 refers to information in the line number info statement program
5480 header, so we can only read it if we've read the header
5481 successfully. */
5482 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5483 if (attr && line_header)
5484 {
5485 unsigned int macro_offset = DW_UNSND (attr);
5486
5487 dwarf_decode_macros (line_header, macro_offset,
5488 comp_dir, abfd, cu);
5489 }
5490 do_cleanups (back_to);
5491 }
5492
5493 /* Process DW_TAG_type_unit.
5494 For TUs we want to skip the first top level sibling if it's not the
5495 actual type being defined by this TU. In this case the first top
5496 level sibling is there to provide context only. */
5497
5498 static void
5499 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5500 {
5501 struct objfile *objfile = cu->objfile;
5502 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5503 CORE_ADDR lowpc;
5504 struct attribute *attr;
5505 char *name = NULL;
5506 char *comp_dir = NULL;
5507 struct die_info *child_die;
5508 bfd *abfd = objfile->obfd;
5509
5510 /* start_symtab needs a low pc, but we don't really have one.
5511 Do what read_file_scope would do in the absence of such info. */
5512 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5513
5514 /* Find the filename. Do not use dwarf2_name here, since the filename
5515 is not a source language identifier. */
5516 attr = dwarf2_attr (die, DW_AT_name, cu);
5517 if (attr)
5518 name = DW_STRING (attr);
5519
5520 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5521 if (attr)
5522 comp_dir = DW_STRING (attr);
5523 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5524 {
5525 comp_dir = ldirname (name);
5526 if (comp_dir != NULL)
5527 make_cleanup (xfree, comp_dir);
5528 }
5529
5530 if (name == NULL)
5531 name = "<unknown>";
5532
5533 attr = dwarf2_attr (die, DW_AT_language, cu);
5534 if (attr)
5535 set_cu_language (DW_UNSND (attr), cu);
5536
5537 /* This isn't technically needed today. It is done for symmetry
5538 with read_file_scope. */
5539 attr = dwarf2_attr (die, DW_AT_producer, cu);
5540 if (attr)
5541 cu->producer = DW_STRING (attr);
5542
5543 /* We assume that we're processing GCC output. */
5544 processing_gcc_compilation = 2;
5545
5546 processing_has_namespace_info = 0;
5547
5548 start_symtab (name, comp_dir, lowpc);
5549 record_debugformat ("DWARF 2");
5550 record_producer (cu->producer);
5551
5552 /* Process the dies in the type unit. */
5553 if (die->child == NULL)
5554 {
5555 dump_die_for_error (die);
5556 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5557 bfd_get_filename (abfd));
5558 }
5559
5560 child_die = die->child;
5561
5562 while (child_die && child_die->tag)
5563 {
5564 process_die (child_die, cu);
5565
5566 child_die = sibling_die (child_die);
5567 }
5568
5569 do_cleanups (back_to);
5570 }
5571
5572 static void
5573 add_to_cu_func_list (const char *name, CORE_ADDR lowpc, CORE_ADDR highpc,
5574 struct dwarf2_cu *cu)
5575 {
5576 struct function_range *thisfn;
5577
5578 thisfn = (struct function_range *)
5579 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct function_range));
5580 thisfn->name = name;
5581 thisfn->lowpc = lowpc;
5582 thisfn->highpc = highpc;
5583 thisfn->seen_line = 0;
5584 thisfn->next = NULL;
5585
5586 if (cu->last_fn == NULL)
5587 cu->first_fn = thisfn;
5588 else
5589 cu->last_fn->next = thisfn;
5590
5591 cu->last_fn = thisfn;
5592 }
5593
5594 /* qsort helper for inherit_abstract_dies. */
5595
5596 static int
5597 unsigned_int_compar (const void *ap, const void *bp)
5598 {
5599 unsigned int a = *(unsigned int *) ap;
5600 unsigned int b = *(unsigned int *) bp;
5601
5602 return (a > b) - (b > a);
5603 }
5604
5605 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5606 Inherit only the children of the DW_AT_abstract_origin DIE not being
5607 already referenced by DW_AT_abstract_origin from the children of the
5608 current DIE. */
5609
5610 static void
5611 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5612 {
5613 struct die_info *child_die;
5614 unsigned die_children_count;
5615 /* CU offsets which were referenced by children of the current DIE. */
5616 unsigned *offsets;
5617 unsigned *offsets_end, *offsetp;
5618 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5619 struct die_info *origin_die;
5620 /* Iterator of the ORIGIN_DIE children. */
5621 struct die_info *origin_child_die;
5622 struct cleanup *cleanups;
5623 struct attribute *attr;
5624 struct dwarf2_cu *origin_cu;
5625 struct pending **origin_previous_list_in_scope;
5626
5627 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5628 if (!attr)
5629 return;
5630
5631 /* Note that following die references may follow to a die in a
5632 different cu. */
5633
5634 origin_cu = cu;
5635 origin_die = follow_die_ref (die, attr, &origin_cu);
5636
5637 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5638 symbols in. */
5639 origin_previous_list_in_scope = origin_cu->list_in_scope;
5640 origin_cu->list_in_scope = cu->list_in_scope;
5641
5642 if (die->tag != origin_die->tag
5643 && !(die->tag == DW_TAG_inlined_subroutine
5644 && origin_die->tag == DW_TAG_subprogram))
5645 complaint (&symfile_complaints,
5646 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5647 die->offset, origin_die->offset);
5648
5649 child_die = die->child;
5650 die_children_count = 0;
5651 while (child_die && child_die->tag)
5652 {
5653 child_die = sibling_die (child_die);
5654 die_children_count++;
5655 }
5656 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5657 cleanups = make_cleanup (xfree, offsets);
5658
5659 offsets_end = offsets;
5660 child_die = die->child;
5661 while (child_die && child_die->tag)
5662 {
5663 /* For each CHILD_DIE, find the corresponding child of
5664 ORIGIN_DIE. If there is more than one layer of
5665 DW_AT_abstract_origin, follow them all; there shouldn't be,
5666 but GCC versions at least through 4.4 generate this (GCC PR
5667 40573). */
5668 struct die_info *child_origin_die = child_die;
5669 struct dwarf2_cu *child_origin_cu = cu;
5670
5671 while (1)
5672 {
5673 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5674 child_origin_cu);
5675 if (attr == NULL)
5676 break;
5677 child_origin_die = follow_die_ref (child_origin_die, attr,
5678 &child_origin_cu);
5679 }
5680
5681 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5682 counterpart may exist. */
5683 if (child_origin_die != child_die)
5684 {
5685 if (child_die->tag != child_origin_die->tag
5686 && !(child_die->tag == DW_TAG_inlined_subroutine
5687 && child_origin_die->tag == DW_TAG_subprogram))
5688 complaint (&symfile_complaints,
5689 _("Child DIE 0x%x and its abstract origin 0x%x have "
5690 "different tags"), child_die->offset,
5691 child_origin_die->offset);
5692 if (child_origin_die->parent != origin_die)
5693 complaint (&symfile_complaints,
5694 _("Child DIE 0x%x and its abstract origin 0x%x have "
5695 "different parents"), child_die->offset,
5696 child_origin_die->offset);
5697 else
5698 *offsets_end++ = child_origin_die->offset;
5699 }
5700 child_die = sibling_die (child_die);
5701 }
5702 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5703 unsigned_int_compar);
5704 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5705 if (offsetp[-1] == *offsetp)
5706 complaint (&symfile_complaints,
5707 _("Multiple children of DIE 0x%x refer "
5708 "to DIE 0x%x as their abstract origin"),
5709 die->offset, *offsetp);
5710
5711 offsetp = offsets;
5712 origin_child_die = origin_die->child;
5713 while (origin_child_die && origin_child_die->tag)
5714 {
5715 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5716 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5717 offsetp++;
5718 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5719 {
5720 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5721 process_die (origin_child_die, origin_cu);
5722 }
5723 origin_child_die = sibling_die (origin_child_die);
5724 }
5725 origin_cu->list_in_scope = origin_previous_list_in_scope;
5726
5727 do_cleanups (cleanups);
5728 }
5729
5730 static void
5731 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5732 {
5733 struct objfile *objfile = cu->objfile;
5734 struct context_stack *new;
5735 CORE_ADDR lowpc;
5736 CORE_ADDR highpc;
5737 struct die_info *child_die;
5738 struct attribute *attr, *call_line, *call_file;
5739 char *name;
5740 CORE_ADDR baseaddr;
5741 struct block *block;
5742 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5743 VEC (symbolp) *template_args = NULL;
5744 struct template_symbol *templ_func = NULL;
5745
5746 if (inlined_func)
5747 {
5748 /* If we do not have call site information, we can't show the
5749 caller of this inlined function. That's too confusing, so
5750 only use the scope for local variables. */
5751 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5752 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5753 if (call_line == NULL || call_file == NULL)
5754 {
5755 read_lexical_block_scope (die, cu);
5756 return;
5757 }
5758 }
5759
5760 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5761
5762 name = dwarf2_name (die, cu);
5763
5764 /* Ignore functions with missing or empty names. These are actually
5765 illegal according to the DWARF standard. */
5766 if (name == NULL)
5767 {
5768 complaint (&symfile_complaints,
5769 _("missing name for subprogram DIE at %d"), die->offset);
5770 return;
5771 }
5772
5773 /* Ignore functions with missing or invalid low and high pc attributes. */
5774 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5775 {
5776 attr = dwarf2_attr (die, DW_AT_external, cu);
5777 if (!attr || !DW_UNSND (attr))
5778 complaint (&symfile_complaints,
5779 _("cannot get low and high bounds "
5780 "for subprogram DIE at %d"),
5781 die->offset);
5782 return;
5783 }
5784
5785 lowpc += baseaddr;
5786 highpc += baseaddr;
5787
5788 /* Record the function range for dwarf_decode_lines. */
5789 add_to_cu_func_list (name, lowpc, highpc, cu);
5790
5791 /* If we have any template arguments, then we must allocate a
5792 different sort of symbol. */
5793 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
5794 {
5795 if (child_die->tag == DW_TAG_template_type_param
5796 || child_die->tag == DW_TAG_template_value_param)
5797 {
5798 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
5799 struct template_symbol);
5800 templ_func->base.is_cplus_template_function = 1;
5801 break;
5802 }
5803 }
5804
5805 new = push_context (0, lowpc);
5806 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
5807 (struct symbol *) templ_func);
5808
5809 /* If there is a location expression for DW_AT_frame_base, record
5810 it. */
5811 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
5812 if (attr)
5813 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
5814 expression is being recorded directly in the function's symbol
5815 and not in a separate frame-base object. I guess this hack is
5816 to avoid adding some sort of frame-base adjunct/annex to the
5817 function's symbol :-(. The problem with doing this is that it
5818 results in a function symbol with a location expression that
5819 has nothing to do with the location of the function, ouch! The
5820 relationship should be: a function's symbol has-a frame base; a
5821 frame-base has-a location expression. */
5822 dwarf2_symbol_mark_computed (attr, new->name, cu);
5823
5824 cu->list_in_scope = &local_symbols;
5825
5826 if (die->child != NULL)
5827 {
5828 child_die = die->child;
5829 while (child_die && child_die->tag)
5830 {
5831 if (child_die->tag == DW_TAG_template_type_param
5832 || child_die->tag == DW_TAG_template_value_param)
5833 {
5834 struct symbol *arg = new_symbol (child_die, NULL, cu);
5835
5836 if (arg != NULL)
5837 VEC_safe_push (symbolp, template_args, arg);
5838 }
5839 else
5840 process_die (child_die, cu);
5841 child_die = sibling_die (child_die);
5842 }
5843 }
5844
5845 inherit_abstract_dies (die, cu);
5846
5847 /* If we have a DW_AT_specification, we might need to import using
5848 directives from the context of the specification DIE. See the
5849 comment in determine_prefix. */
5850 if (cu->language == language_cplus
5851 && dwarf2_attr (die, DW_AT_specification, cu))
5852 {
5853 struct dwarf2_cu *spec_cu = cu;
5854 struct die_info *spec_die = die_specification (die, &spec_cu);
5855
5856 while (spec_die)
5857 {
5858 child_die = spec_die->child;
5859 while (child_die && child_die->tag)
5860 {
5861 if (child_die->tag == DW_TAG_imported_module)
5862 process_die (child_die, spec_cu);
5863 child_die = sibling_die (child_die);
5864 }
5865
5866 /* In some cases, GCC generates specification DIEs that
5867 themselves contain DW_AT_specification attributes. */
5868 spec_die = die_specification (spec_die, &spec_cu);
5869 }
5870 }
5871
5872 new = pop_context ();
5873 /* Make a block for the local symbols within. */
5874 block = finish_block (new->name, &local_symbols, new->old_blocks,
5875 lowpc, highpc, objfile);
5876
5877 /* For C++, set the block's scope. */
5878 if (cu->language == language_cplus || cu->language == language_fortran)
5879 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
5880 determine_prefix (die, cu),
5881 processing_has_namespace_info);
5882
5883 /* If we have address ranges, record them. */
5884 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5885
5886 /* Attach template arguments to function. */
5887 if (! VEC_empty (symbolp, template_args))
5888 {
5889 gdb_assert (templ_func != NULL);
5890
5891 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
5892 templ_func->template_arguments
5893 = obstack_alloc (&objfile->objfile_obstack,
5894 (templ_func->n_template_arguments
5895 * sizeof (struct symbol *)));
5896 memcpy (templ_func->template_arguments,
5897 VEC_address (symbolp, template_args),
5898 (templ_func->n_template_arguments * sizeof (struct symbol *)));
5899 VEC_free (symbolp, template_args);
5900 }
5901
5902 /* In C++, we can have functions nested inside functions (e.g., when
5903 a function declares a class that has methods). This means that
5904 when we finish processing a function scope, we may need to go
5905 back to building a containing block's symbol lists. */
5906 local_symbols = new->locals;
5907 param_symbols = new->params;
5908 using_directives = new->using_directives;
5909
5910 /* If we've finished processing a top-level function, subsequent
5911 symbols go in the file symbol list. */
5912 if (outermost_context_p ())
5913 cu->list_in_scope = &file_symbols;
5914 }
5915
5916 /* Process all the DIES contained within a lexical block scope. Start
5917 a new scope, process the dies, and then close the scope. */
5918
5919 static void
5920 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
5921 {
5922 struct objfile *objfile = cu->objfile;
5923 struct context_stack *new;
5924 CORE_ADDR lowpc, highpc;
5925 struct die_info *child_die;
5926 CORE_ADDR baseaddr;
5927
5928 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5929
5930 /* Ignore blocks with missing or invalid low and high pc attributes. */
5931 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
5932 as multiple lexical blocks? Handling children in a sane way would
5933 be nasty. Might be easier to properly extend generic blocks to
5934 describe ranges. */
5935 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
5936 return;
5937 lowpc += baseaddr;
5938 highpc += baseaddr;
5939
5940 push_context (0, lowpc);
5941 if (die->child != NULL)
5942 {
5943 child_die = die->child;
5944 while (child_die && child_die->tag)
5945 {
5946 process_die (child_die, cu);
5947 child_die = sibling_die (child_die);
5948 }
5949 }
5950 new = pop_context ();
5951
5952 if (local_symbols != NULL || using_directives != NULL)
5953 {
5954 struct block *block
5955 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
5956 highpc, objfile);
5957
5958 /* Note that recording ranges after traversing children, as we
5959 do here, means that recording a parent's ranges entails
5960 walking across all its children's ranges as they appear in
5961 the address map, which is quadratic behavior.
5962
5963 It would be nicer to record the parent's ranges before
5964 traversing its children, simply overriding whatever you find
5965 there. But since we don't even decide whether to create a
5966 block until after we've traversed its children, that's hard
5967 to do. */
5968 dwarf2_record_block_ranges (die, block, baseaddr, cu);
5969 }
5970 local_symbols = new->locals;
5971 using_directives = new->using_directives;
5972 }
5973
5974 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
5975 Return 1 if the attributes are present and valid, otherwise, return 0.
5976 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
5977
5978 static int
5979 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
5980 CORE_ADDR *high_return, struct dwarf2_cu *cu,
5981 struct partial_symtab *ranges_pst)
5982 {
5983 struct objfile *objfile = cu->objfile;
5984 struct comp_unit_head *cu_header = &cu->header;
5985 bfd *obfd = objfile->obfd;
5986 unsigned int addr_size = cu_header->addr_size;
5987 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
5988 /* Base address selection entry. */
5989 CORE_ADDR base;
5990 int found_base;
5991 unsigned int dummy;
5992 gdb_byte *buffer;
5993 CORE_ADDR marker;
5994 int low_set;
5995 CORE_ADDR low = 0;
5996 CORE_ADDR high = 0;
5997 CORE_ADDR baseaddr;
5998
5999 found_base = cu->base_known;
6000 base = cu->base_address;
6001
6002 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6003 if (offset >= dwarf2_per_objfile->ranges.size)
6004 {
6005 complaint (&symfile_complaints,
6006 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6007 offset);
6008 return 0;
6009 }
6010 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6011
6012 /* Read in the largest possible address. */
6013 marker = read_address (obfd, buffer, cu, &dummy);
6014 if ((marker & mask) == mask)
6015 {
6016 /* If we found the largest possible address, then
6017 read the base address. */
6018 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6019 buffer += 2 * addr_size;
6020 offset += 2 * addr_size;
6021 found_base = 1;
6022 }
6023
6024 low_set = 0;
6025
6026 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6027
6028 while (1)
6029 {
6030 CORE_ADDR range_beginning, range_end;
6031
6032 range_beginning = read_address (obfd, buffer, cu, &dummy);
6033 buffer += addr_size;
6034 range_end = read_address (obfd, buffer, cu, &dummy);
6035 buffer += addr_size;
6036 offset += 2 * addr_size;
6037
6038 /* An end of list marker is a pair of zero addresses. */
6039 if (range_beginning == 0 && range_end == 0)
6040 /* Found the end of list entry. */
6041 break;
6042
6043 /* Each base address selection entry is a pair of 2 values.
6044 The first is the largest possible address, the second is
6045 the base address. Check for a base address here. */
6046 if ((range_beginning & mask) == mask)
6047 {
6048 /* If we found the largest possible address, then
6049 read the base address. */
6050 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6051 found_base = 1;
6052 continue;
6053 }
6054
6055 if (!found_base)
6056 {
6057 /* We have no valid base address for the ranges
6058 data. */
6059 complaint (&symfile_complaints,
6060 _("Invalid .debug_ranges data (no base address)"));
6061 return 0;
6062 }
6063
6064 if (range_beginning > range_end)
6065 {
6066 /* Inverted range entries are invalid. */
6067 complaint (&symfile_complaints,
6068 _("Invalid .debug_ranges data (inverted range)"));
6069 return 0;
6070 }
6071
6072 /* Empty range entries have no effect. */
6073 if (range_beginning == range_end)
6074 continue;
6075
6076 range_beginning += base;
6077 range_end += base;
6078
6079 if (ranges_pst != NULL)
6080 addrmap_set_empty (objfile->psymtabs_addrmap,
6081 range_beginning + baseaddr,
6082 range_end - 1 + baseaddr,
6083 ranges_pst);
6084
6085 /* FIXME: This is recording everything as a low-high
6086 segment of consecutive addresses. We should have a
6087 data structure for discontiguous block ranges
6088 instead. */
6089 if (! low_set)
6090 {
6091 low = range_beginning;
6092 high = range_end;
6093 low_set = 1;
6094 }
6095 else
6096 {
6097 if (range_beginning < low)
6098 low = range_beginning;
6099 if (range_end > high)
6100 high = range_end;
6101 }
6102 }
6103
6104 if (! low_set)
6105 /* If the first entry is an end-of-list marker, the range
6106 describes an empty scope, i.e. no instructions. */
6107 return 0;
6108
6109 if (low_return)
6110 *low_return = low;
6111 if (high_return)
6112 *high_return = high;
6113 return 1;
6114 }
6115
6116 /* Get low and high pc attributes from a die. Return 1 if the attributes
6117 are present and valid, otherwise, return 0. Return -1 if the range is
6118 discontinuous, i.e. derived from DW_AT_ranges information. */
6119 static int
6120 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6121 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6122 struct partial_symtab *pst)
6123 {
6124 struct attribute *attr;
6125 CORE_ADDR low = 0;
6126 CORE_ADDR high = 0;
6127 int ret = 0;
6128
6129 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6130 if (attr)
6131 {
6132 high = DW_ADDR (attr);
6133 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6134 if (attr)
6135 low = DW_ADDR (attr);
6136 else
6137 /* Found high w/o low attribute. */
6138 return 0;
6139
6140 /* Found consecutive range of addresses. */
6141 ret = 1;
6142 }
6143 else
6144 {
6145 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6146 if (attr != NULL)
6147 {
6148 /* Value of the DW_AT_ranges attribute is the offset in the
6149 .debug_ranges section. */
6150 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6151 return 0;
6152 /* Found discontinuous range of addresses. */
6153 ret = -1;
6154 }
6155 }
6156
6157 /* read_partial_die has also the strict LOW < HIGH requirement. */
6158 if (high <= low)
6159 return 0;
6160
6161 /* When using the GNU linker, .gnu.linkonce. sections are used to
6162 eliminate duplicate copies of functions and vtables and such.
6163 The linker will arbitrarily choose one and discard the others.
6164 The AT_*_pc values for such functions refer to local labels in
6165 these sections. If the section from that file was discarded, the
6166 labels are not in the output, so the relocs get a value of 0.
6167 If this is a discarded function, mark the pc bounds as invalid,
6168 so that GDB will ignore it. */
6169 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6170 return 0;
6171
6172 *lowpc = low;
6173 *highpc = high;
6174 return ret;
6175 }
6176
6177 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6178 its low and high PC addresses. Do nothing if these addresses could not
6179 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6180 and HIGHPC to the high address if greater than HIGHPC. */
6181
6182 static void
6183 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6184 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6185 struct dwarf2_cu *cu)
6186 {
6187 CORE_ADDR low, high;
6188 struct die_info *child = die->child;
6189
6190 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6191 {
6192 *lowpc = min (*lowpc, low);
6193 *highpc = max (*highpc, high);
6194 }
6195
6196 /* If the language does not allow nested subprograms (either inside
6197 subprograms or lexical blocks), we're done. */
6198 if (cu->language != language_ada)
6199 return;
6200
6201 /* Check all the children of the given DIE. If it contains nested
6202 subprograms, then check their pc bounds. Likewise, we need to
6203 check lexical blocks as well, as they may also contain subprogram
6204 definitions. */
6205 while (child && child->tag)
6206 {
6207 if (child->tag == DW_TAG_subprogram
6208 || child->tag == DW_TAG_lexical_block)
6209 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6210 child = sibling_die (child);
6211 }
6212 }
6213
6214 /* Get the low and high pc's represented by the scope DIE, and store
6215 them in *LOWPC and *HIGHPC. If the correct values can't be
6216 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6217
6218 static void
6219 get_scope_pc_bounds (struct die_info *die,
6220 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6221 struct dwarf2_cu *cu)
6222 {
6223 CORE_ADDR best_low = (CORE_ADDR) -1;
6224 CORE_ADDR best_high = (CORE_ADDR) 0;
6225 CORE_ADDR current_low, current_high;
6226
6227 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6228 {
6229 best_low = current_low;
6230 best_high = current_high;
6231 }
6232 else
6233 {
6234 struct die_info *child = die->child;
6235
6236 while (child && child->tag)
6237 {
6238 switch (child->tag) {
6239 case DW_TAG_subprogram:
6240 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6241 break;
6242 case DW_TAG_namespace:
6243 case DW_TAG_module:
6244 /* FIXME: carlton/2004-01-16: Should we do this for
6245 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6246 that current GCC's always emit the DIEs corresponding
6247 to definitions of methods of classes as children of a
6248 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6249 the DIEs giving the declarations, which could be
6250 anywhere). But I don't see any reason why the
6251 standards says that they have to be there. */
6252 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6253
6254 if (current_low != ((CORE_ADDR) -1))
6255 {
6256 best_low = min (best_low, current_low);
6257 best_high = max (best_high, current_high);
6258 }
6259 break;
6260 default:
6261 /* Ignore. */
6262 break;
6263 }
6264
6265 child = sibling_die (child);
6266 }
6267 }
6268
6269 *lowpc = best_low;
6270 *highpc = best_high;
6271 }
6272
6273 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6274 in DIE. */
6275 static void
6276 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6277 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6278 {
6279 struct attribute *attr;
6280
6281 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6282 if (attr)
6283 {
6284 CORE_ADDR high = DW_ADDR (attr);
6285
6286 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6287 if (attr)
6288 {
6289 CORE_ADDR low = DW_ADDR (attr);
6290
6291 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6292 }
6293 }
6294
6295 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6296 if (attr)
6297 {
6298 bfd *obfd = cu->objfile->obfd;
6299
6300 /* The value of the DW_AT_ranges attribute is the offset of the
6301 address range list in the .debug_ranges section. */
6302 unsigned long offset = DW_UNSND (attr);
6303 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6304
6305 /* For some target architectures, but not others, the
6306 read_address function sign-extends the addresses it returns.
6307 To recognize base address selection entries, we need a
6308 mask. */
6309 unsigned int addr_size = cu->header.addr_size;
6310 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6311
6312 /* The base address, to which the next pair is relative. Note
6313 that this 'base' is a DWARF concept: most entries in a range
6314 list are relative, to reduce the number of relocs against the
6315 debugging information. This is separate from this function's
6316 'baseaddr' argument, which GDB uses to relocate debugging
6317 information from a shared library based on the address at
6318 which the library was loaded. */
6319 CORE_ADDR base = cu->base_address;
6320 int base_known = cu->base_known;
6321
6322 gdb_assert (dwarf2_per_objfile->ranges.readin);
6323 if (offset >= dwarf2_per_objfile->ranges.size)
6324 {
6325 complaint (&symfile_complaints,
6326 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6327 offset);
6328 return;
6329 }
6330
6331 for (;;)
6332 {
6333 unsigned int bytes_read;
6334 CORE_ADDR start, end;
6335
6336 start = read_address (obfd, buffer, cu, &bytes_read);
6337 buffer += bytes_read;
6338 end = read_address (obfd, buffer, cu, &bytes_read);
6339 buffer += bytes_read;
6340
6341 /* Did we find the end of the range list? */
6342 if (start == 0 && end == 0)
6343 break;
6344
6345 /* Did we find a base address selection entry? */
6346 else if ((start & base_select_mask) == base_select_mask)
6347 {
6348 base = end;
6349 base_known = 1;
6350 }
6351
6352 /* We found an ordinary address range. */
6353 else
6354 {
6355 if (!base_known)
6356 {
6357 complaint (&symfile_complaints,
6358 _("Invalid .debug_ranges data "
6359 "(no base address)"));
6360 return;
6361 }
6362
6363 if (start > end)
6364 {
6365 /* Inverted range entries are invalid. */
6366 complaint (&symfile_complaints,
6367 _("Invalid .debug_ranges data "
6368 "(inverted range)"));
6369 return;
6370 }
6371
6372 /* Empty range entries have no effect. */
6373 if (start == end)
6374 continue;
6375
6376 record_block_range (block,
6377 baseaddr + base + start,
6378 baseaddr + base + end - 1);
6379 }
6380 }
6381 }
6382 }
6383
6384 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6385 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6386 during 4.6.0 experimental. */
6387
6388 static int
6389 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6390 {
6391 const char *cs;
6392 int major, minor, release;
6393
6394 if (cu->producer == NULL)
6395 {
6396 /* For unknown compilers expect their behavior is DWARF version
6397 compliant.
6398
6399 GCC started to support .debug_types sections by -gdwarf-4 since
6400 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6401 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6402 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6403 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6404
6405 return 0;
6406 }
6407
6408 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6409
6410 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6411 {
6412 /* For non-GCC compilers expect their behavior is DWARF version
6413 compliant. */
6414
6415 return 0;
6416 }
6417 cs = &cu->producer[strlen ("GNU ")];
6418 while (*cs && !isdigit (*cs))
6419 cs++;
6420 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6421 {
6422 /* Not recognized as GCC. */
6423
6424 return 0;
6425 }
6426
6427 return major < 4 || (major == 4 && minor < 6);
6428 }
6429
6430 /* Return the default accessibility type if it is not overriden by
6431 DW_AT_accessibility. */
6432
6433 static enum dwarf_access_attribute
6434 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6435 {
6436 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6437 {
6438 /* The default DWARF 2 accessibility for members is public, the default
6439 accessibility for inheritance is private. */
6440
6441 if (die->tag != DW_TAG_inheritance)
6442 return DW_ACCESS_public;
6443 else
6444 return DW_ACCESS_private;
6445 }
6446 else
6447 {
6448 /* DWARF 3+ defines the default accessibility a different way. The same
6449 rules apply now for DW_TAG_inheritance as for the members and it only
6450 depends on the container kind. */
6451
6452 if (die->parent->tag == DW_TAG_class_type)
6453 return DW_ACCESS_private;
6454 else
6455 return DW_ACCESS_public;
6456 }
6457 }
6458
6459 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6460 offset. If the attribute was not found return 0, otherwise return
6461 1. If it was found but could not properly be handled, set *OFFSET
6462 to 0. */
6463
6464 static int
6465 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6466 LONGEST *offset)
6467 {
6468 struct attribute *attr;
6469
6470 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6471 if (attr != NULL)
6472 {
6473 *offset = 0;
6474
6475 /* Note that we do not check for a section offset first here.
6476 This is because DW_AT_data_member_location is new in DWARF 4,
6477 so if we see it, we can assume that a constant form is really
6478 a constant and not a section offset. */
6479 if (attr_form_is_constant (attr))
6480 *offset = dwarf2_get_attr_constant_value (attr, 0);
6481 else if (attr_form_is_section_offset (attr))
6482 dwarf2_complex_location_expr_complaint ();
6483 else if (attr_form_is_block (attr))
6484 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6485 else
6486 dwarf2_complex_location_expr_complaint ();
6487
6488 return 1;
6489 }
6490
6491 return 0;
6492 }
6493
6494 /* Add an aggregate field to the field list. */
6495
6496 static void
6497 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6498 struct dwarf2_cu *cu)
6499 {
6500 struct objfile *objfile = cu->objfile;
6501 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6502 struct nextfield *new_field;
6503 struct attribute *attr;
6504 struct field *fp;
6505 char *fieldname = "";
6506
6507 /* Allocate a new field list entry and link it in. */
6508 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6509 make_cleanup (xfree, new_field);
6510 memset (new_field, 0, sizeof (struct nextfield));
6511
6512 if (die->tag == DW_TAG_inheritance)
6513 {
6514 new_field->next = fip->baseclasses;
6515 fip->baseclasses = new_field;
6516 }
6517 else
6518 {
6519 new_field->next = fip->fields;
6520 fip->fields = new_field;
6521 }
6522 fip->nfields++;
6523
6524 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6525 if (attr)
6526 new_field->accessibility = DW_UNSND (attr);
6527 else
6528 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
6529 if (new_field->accessibility != DW_ACCESS_public)
6530 fip->non_public_fields = 1;
6531
6532 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6533 if (attr)
6534 new_field->virtuality = DW_UNSND (attr);
6535 else
6536 new_field->virtuality = DW_VIRTUALITY_none;
6537
6538 fp = &new_field->field;
6539
6540 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
6541 {
6542 LONGEST offset;
6543
6544 /* Data member other than a C++ static data member. */
6545
6546 /* Get type of field. */
6547 fp->type = die_type (die, cu);
6548
6549 SET_FIELD_BITPOS (*fp, 0);
6550
6551 /* Get bit size of field (zero if none). */
6552 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
6553 if (attr)
6554 {
6555 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
6556 }
6557 else
6558 {
6559 FIELD_BITSIZE (*fp) = 0;
6560 }
6561
6562 /* Get bit offset of field. */
6563 if (handle_data_member_location (die, cu, &offset))
6564 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6565 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
6566 if (attr)
6567 {
6568 if (gdbarch_bits_big_endian (gdbarch))
6569 {
6570 /* For big endian bits, the DW_AT_bit_offset gives the
6571 additional bit offset from the MSB of the containing
6572 anonymous object to the MSB of the field. We don't
6573 have to do anything special since we don't need to
6574 know the size of the anonymous object. */
6575 FIELD_BITPOS (*fp) += DW_UNSND (attr);
6576 }
6577 else
6578 {
6579 /* For little endian bits, compute the bit offset to the
6580 MSB of the anonymous object, subtract off the number of
6581 bits from the MSB of the field to the MSB of the
6582 object, and then subtract off the number of bits of
6583 the field itself. The result is the bit offset of
6584 the LSB of the field. */
6585 int anonymous_size;
6586 int bit_offset = DW_UNSND (attr);
6587
6588 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
6589 if (attr)
6590 {
6591 /* The size of the anonymous object containing
6592 the bit field is explicit, so use the
6593 indicated size (in bytes). */
6594 anonymous_size = DW_UNSND (attr);
6595 }
6596 else
6597 {
6598 /* The size of the anonymous object containing
6599 the bit field must be inferred from the type
6600 attribute of the data member containing the
6601 bit field. */
6602 anonymous_size = TYPE_LENGTH (fp->type);
6603 }
6604 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
6605 - bit_offset - FIELD_BITSIZE (*fp);
6606 }
6607 }
6608
6609 /* Get name of field. */
6610 fieldname = dwarf2_name (die, cu);
6611 if (fieldname == NULL)
6612 fieldname = "";
6613
6614 /* The name is already allocated along with this objfile, so we don't
6615 need to duplicate it for the type. */
6616 fp->name = fieldname;
6617
6618 /* Change accessibility for artificial fields (e.g. virtual table
6619 pointer or virtual base class pointer) to private. */
6620 if (dwarf2_attr (die, DW_AT_artificial, cu))
6621 {
6622 FIELD_ARTIFICIAL (*fp) = 1;
6623 new_field->accessibility = DW_ACCESS_private;
6624 fip->non_public_fields = 1;
6625 }
6626 }
6627 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
6628 {
6629 /* C++ static member. */
6630
6631 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
6632 is a declaration, but all versions of G++ as of this writing
6633 (so through at least 3.2.1) incorrectly generate
6634 DW_TAG_variable tags. */
6635
6636 const char *physname;
6637
6638 /* Get name of field. */
6639 fieldname = dwarf2_name (die, cu);
6640 if (fieldname == NULL)
6641 return;
6642
6643 attr = dwarf2_attr (die, DW_AT_const_value, cu);
6644 if (attr
6645 /* Only create a symbol if this is an external value.
6646 new_symbol checks this and puts the value in the global symbol
6647 table, which we want. If it is not external, new_symbol
6648 will try to put the value in cu->list_in_scope which is wrong. */
6649 && dwarf2_flag_true_p (die, DW_AT_external, cu))
6650 {
6651 /* A static const member, not much different than an enum as far as
6652 we're concerned, except that we can support more types. */
6653 new_symbol (die, NULL, cu);
6654 }
6655
6656 /* Get physical name. */
6657 physname = dwarf2_physname (fieldname, die, cu);
6658
6659 /* The name is already allocated along with this objfile, so we don't
6660 need to duplicate it for the type. */
6661 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
6662 FIELD_TYPE (*fp) = die_type (die, cu);
6663 FIELD_NAME (*fp) = fieldname;
6664 }
6665 else if (die->tag == DW_TAG_inheritance)
6666 {
6667 LONGEST offset;
6668
6669 /* C++ base class field. */
6670 if (handle_data_member_location (die, cu, &offset))
6671 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
6672 FIELD_BITSIZE (*fp) = 0;
6673 FIELD_TYPE (*fp) = die_type (die, cu);
6674 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
6675 fip->nbaseclasses++;
6676 }
6677 }
6678
6679 /* Add a typedef defined in the scope of the FIP's class. */
6680
6681 static void
6682 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
6683 struct dwarf2_cu *cu)
6684 {
6685 struct objfile *objfile = cu->objfile;
6686 struct typedef_field_list *new_field;
6687 struct attribute *attr;
6688 struct typedef_field *fp;
6689 char *fieldname = "";
6690
6691 /* Allocate a new field list entry and link it in. */
6692 new_field = xzalloc (sizeof (*new_field));
6693 make_cleanup (xfree, new_field);
6694
6695 gdb_assert (die->tag == DW_TAG_typedef);
6696
6697 fp = &new_field->field;
6698
6699 /* Get name of field. */
6700 fp->name = dwarf2_name (die, cu);
6701 if (fp->name == NULL)
6702 return;
6703
6704 fp->type = read_type_die (die, cu);
6705
6706 new_field->next = fip->typedef_field_list;
6707 fip->typedef_field_list = new_field;
6708 fip->typedef_field_list_count++;
6709 }
6710
6711 /* Create the vector of fields, and attach it to the type. */
6712
6713 static void
6714 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
6715 struct dwarf2_cu *cu)
6716 {
6717 int nfields = fip->nfields;
6718
6719 /* Record the field count, allocate space for the array of fields,
6720 and create blank accessibility bitfields if necessary. */
6721 TYPE_NFIELDS (type) = nfields;
6722 TYPE_FIELDS (type) = (struct field *)
6723 TYPE_ALLOC (type, sizeof (struct field) * nfields);
6724 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
6725
6726 if (fip->non_public_fields && cu->language != language_ada)
6727 {
6728 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6729
6730 TYPE_FIELD_PRIVATE_BITS (type) =
6731 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6732 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
6733
6734 TYPE_FIELD_PROTECTED_BITS (type) =
6735 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6736 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
6737
6738 TYPE_FIELD_IGNORE_BITS (type) =
6739 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
6740 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
6741 }
6742
6743 /* If the type has baseclasses, allocate and clear a bit vector for
6744 TYPE_FIELD_VIRTUAL_BITS. */
6745 if (fip->nbaseclasses && cu->language != language_ada)
6746 {
6747 int num_bytes = B_BYTES (fip->nbaseclasses);
6748 unsigned char *pointer;
6749
6750 ALLOCATE_CPLUS_STRUCT_TYPE (type);
6751 pointer = TYPE_ALLOC (type, num_bytes);
6752 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
6753 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
6754 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
6755 }
6756
6757 /* Copy the saved-up fields into the field vector. Start from the head of
6758 the list, adding to the tail of the field array, so that they end up in
6759 the same order in the array in which they were added to the list. */
6760 while (nfields-- > 0)
6761 {
6762 struct nextfield *fieldp;
6763
6764 if (fip->fields)
6765 {
6766 fieldp = fip->fields;
6767 fip->fields = fieldp->next;
6768 }
6769 else
6770 {
6771 fieldp = fip->baseclasses;
6772 fip->baseclasses = fieldp->next;
6773 }
6774
6775 TYPE_FIELD (type, nfields) = fieldp->field;
6776 switch (fieldp->accessibility)
6777 {
6778 case DW_ACCESS_private:
6779 if (cu->language != language_ada)
6780 SET_TYPE_FIELD_PRIVATE (type, nfields);
6781 break;
6782
6783 case DW_ACCESS_protected:
6784 if (cu->language != language_ada)
6785 SET_TYPE_FIELD_PROTECTED (type, nfields);
6786 break;
6787
6788 case DW_ACCESS_public:
6789 break;
6790
6791 default:
6792 /* Unknown accessibility. Complain and treat it as public. */
6793 {
6794 complaint (&symfile_complaints, _("unsupported accessibility %d"),
6795 fieldp->accessibility);
6796 }
6797 break;
6798 }
6799 if (nfields < fip->nbaseclasses)
6800 {
6801 switch (fieldp->virtuality)
6802 {
6803 case DW_VIRTUALITY_virtual:
6804 case DW_VIRTUALITY_pure_virtual:
6805 if (cu->language == language_ada)
6806 error (_("unexpected virtuality in component of Ada type"));
6807 SET_TYPE_FIELD_VIRTUAL (type, nfields);
6808 break;
6809 }
6810 }
6811 }
6812 }
6813
6814 /* Add a member function to the proper fieldlist. */
6815
6816 static void
6817 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
6818 struct type *type, struct dwarf2_cu *cu)
6819 {
6820 struct objfile *objfile = cu->objfile;
6821 struct attribute *attr;
6822 struct fnfieldlist *flp;
6823 int i;
6824 struct fn_field *fnp;
6825 char *fieldname;
6826 struct nextfnfield *new_fnfield;
6827 struct type *this_type;
6828 enum dwarf_access_attribute accessibility;
6829
6830 if (cu->language == language_ada)
6831 error (_("unexpected member function in Ada type"));
6832
6833 /* Get name of member function. */
6834 fieldname = dwarf2_name (die, cu);
6835 if (fieldname == NULL)
6836 return;
6837
6838 /* Look up member function name in fieldlist. */
6839 for (i = 0; i < fip->nfnfields; i++)
6840 {
6841 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
6842 break;
6843 }
6844
6845 /* Create new list element if necessary. */
6846 if (i < fip->nfnfields)
6847 flp = &fip->fnfieldlists[i];
6848 else
6849 {
6850 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
6851 {
6852 fip->fnfieldlists = (struct fnfieldlist *)
6853 xrealloc (fip->fnfieldlists,
6854 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
6855 * sizeof (struct fnfieldlist));
6856 if (fip->nfnfields == 0)
6857 make_cleanup (free_current_contents, &fip->fnfieldlists);
6858 }
6859 flp = &fip->fnfieldlists[fip->nfnfields];
6860 flp->name = fieldname;
6861 flp->length = 0;
6862 flp->head = NULL;
6863 i = fip->nfnfields++;
6864 }
6865
6866 /* Create a new member function field and chain it to the field list
6867 entry. */
6868 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
6869 make_cleanup (xfree, new_fnfield);
6870 memset (new_fnfield, 0, sizeof (struct nextfnfield));
6871 new_fnfield->next = flp->head;
6872 flp->head = new_fnfield;
6873 flp->length++;
6874
6875 /* Fill in the member function field info. */
6876 fnp = &new_fnfield->fnfield;
6877
6878 /* Delay processing of the physname until later. */
6879 if (cu->language == language_cplus || cu->language == language_java)
6880 {
6881 add_to_method_list (type, i, flp->length - 1, fieldname,
6882 die, cu);
6883 }
6884 else
6885 {
6886 const char *physname = dwarf2_physname (fieldname, die, cu);
6887 fnp->physname = physname ? physname : "";
6888 }
6889
6890 fnp->type = alloc_type (objfile);
6891 this_type = read_type_die (die, cu);
6892 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
6893 {
6894 int nparams = TYPE_NFIELDS (this_type);
6895
6896 /* TYPE is the domain of this method, and THIS_TYPE is the type
6897 of the method itself (TYPE_CODE_METHOD). */
6898 smash_to_method_type (fnp->type, type,
6899 TYPE_TARGET_TYPE (this_type),
6900 TYPE_FIELDS (this_type),
6901 TYPE_NFIELDS (this_type),
6902 TYPE_VARARGS (this_type));
6903
6904 /* Handle static member functions.
6905 Dwarf2 has no clean way to discern C++ static and non-static
6906 member functions. G++ helps GDB by marking the first
6907 parameter for non-static member functions (which is the this
6908 pointer) as artificial. We obtain this information from
6909 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
6910 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
6911 fnp->voffset = VOFFSET_STATIC;
6912 }
6913 else
6914 complaint (&symfile_complaints, _("member function type missing for '%s'"),
6915 dwarf2_full_name (fieldname, die, cu));
6916
6917 /* Get fcontext from DW_AT_containing_type if present. */
6918 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
6919 fnp->fcontext = die_containing_type (die, cu);
6920
6921 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
6922 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
6923
6924 /* Get accessibility. */
6925 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
6926 if (attr)
6927 accessibility = DW_UNSND (attr);
6928 else
6929 accessibility = dwarf2_default_access_attribute (die, cu);
6930 switch (accessibility)
6931 {
6932 case DW_ACCESS_private:
6933 fnp->is_private = 1;
6934 break;
6935 case DW_ACCESS_protected:
6936 fnp->is_protected = 1;
6937 break;
6938 }
6939
6940 /* Check for artificial methods. */
6941 attr = dwarf2_attr (die, DW_AT_artificial, cu);
6942 if (attr && DW_UNSND (attr) != 0)
6943 fnp->is_artificial = 1;
6944
6945 /* Get index in virtual function table if it is a virtual member
6946 function. For older versions of GCC, this is an offset in the
6947 appropriate virtual table, as specified by DW_AT_containing_type.
6948 For everyone else, it is an expression to be evaluated relative
6949 to the object address. */
6950
6951 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
6952 if (attr)
6953 {
6954 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
6955 {
6956 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
6957 {
6958 /* Old-style GCC. */
6959 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
6960 }
6961 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
6962 || (DW_BLOCK (attr)->size > 1
6963 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
6964 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
6965 {
6966 struct dwarf_block blk;
6967 int offset;
6968
6969 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
6970 ? 1 : 2);
6971 blk.size = DW_BLOCK (attr)->size - offset;
6972 blk.data = DW_BLOCK (attr)->data + offset;
6973 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
6974 if ((fnp->voffset % cu->header.addr_size) != 0)
6975 dwarf2_complex_location_expr_complaint ();
6976 else
6977 fnp->voffset /= cu->header.addr_size;
6978 fnp->voffset += 2;
6979 }
6980 else
6981 dwarf2_complex_location_expr_complaint ();
6982
6983 if (!fnp->fcontext)
6984 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
6985 }
6986 else if (attr_form_is_section_offset (attr))
6987 {
6988 dwarf2_complex_location_expr_complaint ();
6989 }
6990 else
6991 {
6992 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
6993 fieldname);
6994 }
6995 }
6996 else
6997 {
6998 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
6999 if (attr && DW_UNSND (attr))
7000 {
7001 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7002 complaint (&symfile_complaints,
7003 _("Member function \"%s\" (offset %d) is virtual "
7004 "but the vtable offset is not specified"),
7005 fieldname, die->offset);
7006 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7007 TYPE_CPLUS_DYNAMIC (type) = 1;
7008 }
7009 }
7010 }
7011
7012 /* Create the vector of member function fields, and attach it to the type. */
7013
7014 static void
7015 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7016 struct dwarf2_cu *cu)
7017 {
7018 struct fnfieldlist *flp;
7019 int total_length = 0;
7020 int i;
7021
7022 if (cu->language == language_ada)
7023 error (_("unexpected member functions in Ada type"));
7024
7025 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7026 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7027 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7028
7029 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7030 {
7031 struct nextfnfield *nfp = flp->head;
7032 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7033 int k;
7034
7035 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7036 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7037 fn_flp->fn_fields = (struct fn_field *)
7038 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7039 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7040 fn_flp->fn_fields[k] = nfp->fnfield;
7041
7042 total_length += flp->length;
7043 }
7044
7045 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7046 TYPE_NFN_FIELDS_TOTAL (type) = total_length;
7047 }
7048
7049 /* Returns non-zero if NAME is the name of a vtable member in CU's
7050 language, zero otherwise. */
7051 static int
7052 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7053 {
7054 static const char vptr[] = "_vptr";
7055 static const char vtable[] = "vtable";
7056
7057 /* Look for the C++ and Java forms of the vtable. */
7058 if ((cu->language == language_java
7059 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7060 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7061 && is_cplus_marker (name[sizeof (vptr) - 1])))
7062 return 1;
7063
7064 return 0;
7065 }
7066
7067 /* GCC outputs unnamed structures that are really pointers to member
7068 functions, with the ABI-specified layout. If TYPE describes
7069 such a structure, smash it into a member function type.
7070
7071 GCC shouldn't do this; it should just output pointer to member DIEs.
7072 This is GCC PR debug/28767. */
7073
7074 static void
7075 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7076 {
7077 struct type *pfn_type, *domain_type, *new_type;
7078
7079 /* Check for a structure with no name and two children. */
7080 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7081 return;
7082
7083 /* Check for __pfn and __delta members. */
7084 if (TYPE_FIELD_NAME (type, 0) == NULL
7085 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7086 || TYPE_FIELD_NAME (type, 1) == NULL
7087 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7088 return;
7089
7090 /* Find the type of the method. */
7091 pfn_type = TYPE_FIELD_TYPE (type, 0);
7092 if (pfn_type == NULL
7093 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7094 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7095 return;
7096
7097 /* Look for the "this" argument. */
7098 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7099 if (TYPE_NFIELDS (pfn_type) == 0
7100 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7101 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7102 return;
7103
7104 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7105 new_type = alloc_type (objfile);
7106 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7107 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7108 TYPE_VARARGS (pfn_type));
7109 smash_to_methodptr_type (type, new_type);
7110 }
7111
7112 /* Called when we find the DIE that starts a structure or union scope
7113 (definition) to create a type for the structure or union. Fill in
7114 the type's name and general properties; the members will not be
7115 processed until process_structure_type.
7116
7117 NOTE: we need to call these functions regardless of whether or not the
7118 DIE has a DW_AT_name attribute, since it might be an anonymous
7119 structure or union. This gets the type entered into our set of
7120 user defined types.
7121
7122 However, if the structure is incomplete (an opaque struct/union)
7123 then suppress creating a symbol table entry for it since gdb only
7124 wants to find the one with the complete definition. Note that if
7125 it is complete, we just call new_symbol, which does it's own
7126 checking about whether the struct/union is anonymous or not (and
7127 suppresses creating a symbol table entry itself). */
7128
7129 static struct type *
7130 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7131 {
7132 struct objfile *objfile = cu->objfile;
7133 struct type *type;
7134 struct attribute *attr;
7135 char *name;
7136
7137 /* If the definition of this type lives in .debug_types, read that type.
7138 Don't follow DW_AT_specification though, that will take us back up
7139 the chain and we want to go down. */
7140 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7141 if (attr)
7142 {
7143 struct dwarf2_cu *type_cu = cu;
7144 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7145
7146 /* We could just recurse on read_structure_type, but we need to call
7147 get_die_type to ensure only one type for this DIE is created.
7148 This is important, for example, because for c++ classes we need
7149 TYPE_NAME set which is only done by new_symbol. Blech. */
7150 type = read_type_die (type_die, type_cu);
7151
7152 /* TYPE_CU may not be the same as CU.
7153 Ensure TYPE is recorded in CU's type_hash table. */
7154 return set_die_type (die, type, cu);
7155 }
7156
7157 type = alloc_type (objfile);
7158 INIT_CPLUS_SPECIFIC (type);
7159
7160 name = dwarf2_name (die, cu);
7161 if (name != NULL)
7162 {
7163 if (cu->language == language_cplus
7164 || cu->language == language_java)
7165 {
7166 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7167
7168 /* dwarf2_full_name might have already finished building the DIE's
7169 type. If so, there is no need to continue. */
7170 if (get_die_type (die, cu) != NULL)
7171 return get_die_type (die, cu);
7172
7173 TYPE_TAG_NAME (type) = full_name;
7174 if (die->tag == DW_TAG_structure_type
7175 || die->tag == DW_TAG_class_type)
7176 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7177 }
7178 else
7179 {
7180 /* The name is already allocated along with this objfile, so
7181 we don't need to duplicate it for the type. */
7182 TYPE_TAG_NAME (type) = (char *) name;
7183 if (die->tag == DW_TAG_class_type)
7184 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7185 }
7186 }
7187
7188 if (die->tag == DW_TAG_structure_type)
7189 {
7190 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7191 }
7192 else if (die->tag == DW_TAG_union_type)
7193 {
7194 TYPE_CODE (type) = TYPE_CODE_UNION;
7195 }
7196 else
7197 {
7198 TYPE_CODE (type) = TYPE_CODE_CLASS;
7199 }
7200
7201 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7202 TYPE_DECLARED_CLASS (type) = 1;
7203
7204 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7205 if (attr)
7206 {
7207 TYPE_LENGTH (type) = DW_UNSND (attr);
7208 }
7209 else
7210 {
7211 TYPE_LENGTH (type) = 0;
7212 }
7213
7214 TYPE_STUB_SUPPORTED (type) = 1;
7215 if (die_is_declaration (die, cu))
7216 TYPE_STUB (type) = 1;
7217 else if (attr == NULL && die->child == NULL
7218 && producer_is_realview (cu->producer))
7219 /* RealView does not output the required DW_AT_declaration
7220 on incomplete types. */
7221 TYPE_STUB (type) = 1;
7222
7223 /* We need to add the type field to the die immediately so we don't
7224 infinitely recurse when dealing with pointers to the structure
7225 type within the structure itself. */
7226 set_die_type (die, type, cu);
7227
7228 /* set_die_type should be already done. */
7229 set_descriptive_type (type, die, cu);
7230
7231 return type;
7232 }
7233
7234 /* Finish creating a structure or union type, including filling in
7235 its members and creating a symbol for it. */
7236
7237 static void
7238 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7239 {
7240 struct objfile *objfile = cu->objfile;
7241 struct die_info *child_die = die->child;
7242 struct type *type;
7243
7244 type = get_die_type (die, cu);
7245 if (type == NULL)
7246 type = read_structure_type (die, cu);
7247
7248 if (die->child != NULL && ! die_is_declaration (die, cu))
7249 {
7250 struct field_info fi;
7251 struct die_info *child_die;
7252 VEC (symbolp) *template_args = NULL;
7253 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7254
7255 memset (&fi, 0, sizeof (struct field_info));
7256
7257 child_die = die->child;
7258
7259 while (child_die && child_die->tag)
7260 {
7261 if (child_die->tag == DW_TAG_member
7262 || child_die->tag == DW_TAG_variable)
7263 {
7264 /* NOTE: carlton/2002-11-05: A C++ static data member
7265 should be a DW_TAG_member that is a declaration, but
7266 all versions of G++ as of this writing (so through at
7267 least 3.2.1) incorrectly generate DW_TAG_variable
7268 tags for them instead. */
7269 dwarf2_add_field (&fi, child_die, cu);
7270 }
7271 else if (child_die->tag == DW_TAG_subprogram)
7272 {
7273 /* C++ member function. */
7274 dwarf2_add_member_fn (&fi, child_die, type, cu);
7275 }
7276 else if (child_die->tag == DW_TAG_inheritance)
7277 {
7278 /* C++ base class field. */
7279 dwarf2_add_field (&fi, child_die, cu);
7280 }
7281 else if (child_die->tag == DW_TAG_typedef)
7282 dwarf2_add_typedef (&fi, child_die, cu);
7283 else if (child_die->tag == DW_TAG_template_type_param
7284 || child_die->tag == DW_TAG_template_value_param)
7285 {
7286 struct symbol *arg = new_symbol (child_die, NULL, cu);
7287
7288 if (arg != NULL)
7289 VEC_safe_push (symbolp, template_args, arg);
7290 }
7291
7292 child_die = sibling_die (child_die);
7293 }
7294
7295 /* Attach template arguments to type. */
7296 if (! VEC_empty (symbolp, template_args))
7297 {
7298 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7299 TYPE_N_TEMPLATE_ARGUMENTS (type)
7300 = VEC_length (symbolp, template_args);
7301 TYPE_TEMPLATE_ARGUMENTS (type)
7302 = obstack_alloc (&objfile->objfile_obstack,
7303 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7304 * sizeof (struct symbol *)));
7305 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7306 VEC_address (symbolp, template_args),
7307 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7308 * sizeof (struct symbol *)));
7309 VEC_free (symbolp, template_args);
7310 }
7311
7312 /* Attach fields and member functions to the type. */
7313 if (fi.nfields)
7314 dwarf2_attach_fields_to_type (&fi, type, cu);
7315 if (fi.nfnfields)
7316 {
7317 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7318
7319 /* Get the type which refers to the base class (possibly this
7320 class itself) which contains the vtable pointer for the current
7321 class from the DW_AT_containing_type attribute. This use of
7322 DW_AT_containing_type is a GNU extension. */
7323
7324 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7325 {
7326 struct type *t = die_containing_type (die, cu);
7327
7328 TYPE_VPTR_BASETYPE (type) = t;
7329 if (type == t)
7330 {
7331 int i;
7332
7333 /* Our own class provides vtbl ptr. */
7334 for (i = TYPE_NFIELDS (t) - 1;
7335 i >= TYPE_N_BASECLASSES (t);
7336 --i)
7337 {
7338 char *fieldname = TYPE_FIELD_NAME (t, i);
7339
7340 if (is_vtable_name (fieldname, cu))
7341 {
7342 TYPE_VPTR_FIELDNO (type) = i;
7343 break;
7344 }
7345 }
7346
7347 /* Complain if virtual function table field not found. */
7348 if (i < TYPE_N_BASECLASSES (t))
7349 complaint (&symfile_complaints,
7350 _("virtual function table pointer "
7351 "not found when defining class '%s'"),
7352 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7353 "");
7354 }
7355 else
7356 {
7357 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7358 }
7359 }
7360 else if (cu->producer
7361 && strncmp (cu->producer,
7362 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7363 {
7364 /* The IBM XLC compiler does not provide direct indication
7365 of the containing type, but the vtable pointer is
7366 always named __vfp. */
7367
7368 int i;
7369
7370 for (i = TYPE_NFIELDS (type) - 1;
7371 i >= TYPE_N_BASECLASSES (type);
7372 --i)
7373 {
7374 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7375 {
7376 TYPE_VPTR_FIELDNO (type) = i;
7377 TYPE_VPTR_BASETYPE (type) = type;
7378 break;
7379 }
7380 }
7381 }
7382 }
7383
7384 /* Copy fi.typedef_field_list linked list elements content into the
7385 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7386 if (fi.typedef_field_list)
7387 {
7388 int i = fi.typedef_field_list_count;
7389
7390 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7391 TYPE_TYPEDEF_FIELD_ARRAY (type)
7392 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7393 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7394
7395 /* Reverse the list order to keep the debug info elements order. */
7396 while (--i >= 0)
7397 {
7398 struct typedef_field *dest, *src;
7399
7400 dest = &TYPE_TYPEDEF_FIELD (type, i);
7401 src = &fi.typedef_field_list->field;
7402 fi.typedef_field_list = fi.typedef_field_list->next;
7403 *dest = *src;
7404 }
7405 }
7406
7407 do_cleanups (back_to);
7408 }
7409
7410 quirk_gcc_member_function_pointer (type, cu->objfile);
7411
7412 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7413 snapshots) has been known to create a die giving a declaration
7414 for a class that has, as a child, a die giving a definition for a
7415 nested class. So we have to process our children even if the
7416 current die is a declaration. Normally, of course, a declaration
7417 won't have any children at all. */
7418
7419 while (child_die != NULL && child_die->tag)
7420 {
7421 if (child_die->tag == DW_TAG_member
7422 || child_die->tag == DW_TAG_variable
7423 || child_die->tag == DW_TAG_inheritance
7424 || child_die->tag == DW_TAG_template_value_param
7425 || child_die->tag == DW_TAG_template_type_param)
7426 {
7427 /* Do nothing. */
7428 }
7429 else
7430 process_die (child_die, cu);
7431
7432 child_die = sibling_die (child_die);
7433 }
7434
7435 /* Do not consider external references. According to the DWARF standard,
7436 these DIEs are identified by the fact that they have no byte_size
7437 attribute, and a declaration attribute. */
7438 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7439 || !die_is_declaration (die, cu))
7440 new_symbol (die, type, cu);
7441 }
7442
7443 /* Given a DW_AT_enumeration_type die, set its type. We do not
7444 complete the type's fields yet, or create any symbols. */
7445
7446 static struct type *
7447 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7448 {
7449 struct objfile *objfile = cu->objfile;
7450 struct type *type;
7451 struct attribute *attr;
7452 const char *name;
7453
7454 /* If the definition of this type lives in .debug_types, read that type.
7455 Don't follow DW_AT_specification though, that will take us back up
7456 the chain and we want to go down. */
7457 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7458 if (attr)
7459 {
7460 struct dwarf2_cu *type_cu = cu;
7461 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7462
7463 type = read_type_die (type_die, type_cu);
7464
7465 /* TYPE_CU may not be the same as CU.
7466 Ensure TYPE is recorded in CU's type_hash table. */
7467 return set_die_type (die, type, cu);
7468 }
7469
7470 type = alloc_type (objfile);
7471
7472 TYPE_CODE (type) = TYPE_CODE_ENUM;
7473 name = dwarf2_full_name (NULL, die, cu);
7474 if (name != NULL)
7475 TYPE_TAG_NAME (type) = (char *) name;
7476
7477 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7478 if (attr)
7479 {
7480 TYPE_LENGTH (type) = DW_UNSND (attr);
7481 }
7482 else
7483 {
7484 TYPE_LENGTH (type) = 0;
7485 }
7486
7487 /* The enumeration DIE can be incomplete. In Ada, any type can be
7488 declared as private in the package spec, and then defined only
7489 inside the package body. Such types are known as Taft Amendment
7490 Types. When another package uses such a type, an incomplete DIE
7491 may be generated by the compiler. */
7492 if (die_is_declaration (die, cu))
7493 TYPE_STUB (type) = 1;
7494
7495 return set_die_type (die, type, cu);
7496 }
7497
7498 /* Given a pointer to a die which begins an enumeration, process all
7499 the dies that define the members of the enumeration, and create the
7500 symbol for the enumeration type.
7501
7502 NOTE: We reverse the order of the element list. */
7503
7504 static void
7505 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7506 {
7507 struct type *this_type;
7508
7509 this_type = get_die_type (die, cu);
7510 if (this_type == NULL)
7511 this_type = read_enumeration_type (die, cu);
7512
7513 if (die->child != NULL)
7514 {
7515 struct die_info *child_die;
7516 struct symbol *sym;
7517 struct field *fields = NULL;
7518 int num_fields = 0;
7519 int unsigned_enum = 1;
7520 char *name;
7521
7522 child_die = die->child;
7523 while (child_die && child_die->tag)
7524 {
7525 if (child_die->tag != DW_TAG_enumerator)
7526 {
7527 process_die (child_die, cu);
7528 }
7529 else
7530 {
7531 name = dwarf2_name (child_die, cu);
7532 if (name)
7533 {
7534 sym = new_symbol (child_die, this_type, cu);
7535 if (SYMBOL_VALUE (sym) < 0)
7536 unsigned_enum = 0;
7537
7538 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
7539 {
7540 fields = (struct field *)
7541 xrealloc (fields,
7542 (num_fields + DW_FIELD_ALLOC_CHUNK)
7543 * sizeof (struct field));
7544 }
7545
7546 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
7547 FIELD_TYPE (fields[num_fields]) = NULL;
7548 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
7549 FIELD_BITSIZE (fields[num_fields]) = 0;
7550
7551 num_fields++;
7552 }
7553 }
7554
7555 child_die = sibling_die (child_die);
7556 }
7557
7558 if (num_fields)
7559 {
7560 TYPE_NFIELDS (this_type) = num_fields;
7561 TYPE_FIELDS (this_type) = (struct field *)
7562 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
7563 memcpy (TYPE_FIELDS (this_type), fields,
7564 sizeof (struct field) * num_fields);
7565 xfree (fields);
7566 }
7567 if (unsigned_enum)
7568 TYPE_UNSIGNED (this_type) = 1;
7569 }
7570
7571 new_symbol (die, this_type, cu);
7572 }
7573
7574 /* Extract all information from a DW_TAG_array_type DIE and put it in
7575 the DIE's type field. For now, this only handles one dimensional
7576 arrays. */
7577
7578 static struct type *
7579 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
7580 {
7581 struct objfile *objfile = cu->objfile;
7582 struct die_info *child_die;
7583 struct type *type;
7584 struct type *element_type, *range_type, *index_type;
7585 struct type **range_types = NULL;
7586 struct attribute *attr;
7587 int ndim = 0;
7588 struct cleanup *back_to;
7589 char *name;
7590
7591 element_type = die_type (die, cu);
7592
7593 /* The die_type call above may have already set the type for this DIE. */
7594 type = get_die_type (die, cu);
7595 if (type)
7596 return type;
7597
7598 /* Irix 6.2 native cc creates array types without children for
7599 arrays with unspecified length. */
7600 if (die->child == NULL)
7601 {
7602 index_type = objfile_type (objfile)->builtin_int;
7603 range_type = create_range_type (NULL, index_type, 0, -1);
7604 type = create_array_type (NULL, element_type, range_type);
7605 return set_die_type (die, type, cu);
7606 }
7607
7608 back_to = make_cleanup (null_cleanup, NULL);
7609 child_die = die->child;
7610 while (child_die && child_die->tag)
7611 {
7612 if (child_die->tag == DW_TAG_subrange_type)
7613 {
7614 struct type *child_type = read_type_die (child_die, cu);
7615
7616 if (child_type != NULL)
7617 {
7618 /* The range type was succesfully read. Save it for the
7619 array type creation. */
7620 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
7621 {
7622 range_types = (struct type **)
7623 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
7624 * sizeof (struct type *));
7625 if (ndim == 0)
7626 make_cleanup (free_current_contents, &range_types);
7627 }
7628 range_types[ndim++] = child_type;
7629 }
7630 }
7631 child_die = sibling_die (child_die);
7632 }
7633
7634 /* Dwarf2 dimensions are output from left to right, create the
7635 necessary array types in backwards order. */
7636
7637 type = element_type;
7638
7639 if (read_array_order (die, cu) == DW_ORD_col_major)
7640 {
7641 int i = 0;
7642
7643 while (i < ndim)
7644 type = create_array_type (NULL, type, range_types[i++]);
7645 }
7646 else
7647 {
7648 while (ndim-- > 0)
7649 type = create_array_type (NULL, type, range_types[ndim]);
7650 }
7651
7652 /* Understand Dwarf2 support for vector types (like they occur on
7653 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
7654 array type. This is not part of the Dwarf2/3 standard yet, but a
7655 custom vendor extension. The main difference between a regular
7656 array and the vector variant is that vectors are passed by value
7657 to functions. */
7658 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
7659 if (attr)
7660 make_vector_type (type);
7661
7662 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
7663 implementation may choose to implement triple vectors using this
7664 attribute. */
7665 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7666 if (attr)
7667 {
7668 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
7669 TYPE_LENGTH (type) = DW_UNSND (attr);
7670 else
7671 complaint (&symfile_complaints,
7672 _("DW_AT_byte_size for array type smaller "
7673 "than the total size of elements"));
7674 }
7675
7676 name = dwarf2_name (die, cu);
7677 if (name)
7678 TYPE_NAME (type) = name;
7679
7680 /* Install the type in the die. */
7681 set_die_type (die, type, cu);
7682
7683 /* set_die_type should be already done. */
7684 set_descriptive_type (type, die, cu);
7685
7686 do_cleanups (back_to);
7687
7688 return type;
7689 }
7690
7691 static enum dwarf_array_dim_ordering
7692 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
7693 {
7694 struct attribute *attr;
7695
7696 attr = dwarf2_attr (die, DW_AT_ordering, cu);
7697
7698 if (attr) return DW_SND (attr);
7699
7700 /* GNU F77 is a special case, as at 08/2004 array type info is the
7701 opposite order to the dwarf2 specification, but data is still
7702 laid out as per normal fortran.
7703
7704 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
7705 version checking. */
7706
7707 if (cu->language == language_fortran
7708 && cu->producer && strstr (cu->producer, "GNU F77"))
7709 {
7710 return DW_ORD_row_major;
7711 }
7712
7713 switch (cu->language_defn->la_array_ordering)
7714 {
7715 case array_column_major:
7716 return DW_ORD_col_major;
7717 case array_row_major:
7718 default:
7719 return DW_ORD_row_major;
7720 };
7721 }
7722
7723 /* Extract all information from a DW_TAG_set_type DIE and put it in
7724 the DIE's type field. */
7725
7726 static struct type *
7727 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
7728 {
7729 struct type *domain_type, *set_type;
7730 struct attribute *attr;
7731
7732 domain_type = die_type (die, cu);
7733
7734 /* The die_type call above may have already set the type for this DIE. */
7735 set_type = get_die_type (die, cu);
7736 if (set_type)
7737 return set_type;
7738
7739 set_type = create_set_type (NULL, domain_type);
7740
7741 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7742 if (attr)
7743 TYPE_LENGTH (set_type) = DW_UNSND (attr);
7744
7745 return set_die_type (die, set_type, cu);
7746 }
7747
7748 /* First cut: install each common block member as a global variable. */
7749
7750 static void
7751 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
7752 {
7753 struct die_info *child_die;
7754 struct attribute *attr;
7755 struct symbol *sym;
7756 CORE_ADDR base = (CORE_ADDR) 0;
7757
7758 attr = dwarf2_attr (die, DW_AT_location, cu);
7759 if (attr)
7760 {
7761 /* Support the .debug_loc offsets. */
7762 if (attr_form_is_block (attr))
7763 {
7764 base = decode_locdesc (DW_BLOCK (attr), cu);
7765 }
7766 else if (attr_form_is_section_offset (attr))
7767 {
7768 dwarf2_complex_location_expr_complaint ();
7769 }
7770 else
7771 {
7772 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
7773 "common block member");
7774 }
7775 }
7776 if (die->child != NULL)
7777 {
7778 child_die = die->child;
7779 while (child_die && child_die->tag)
7780 {
7781 LONGEST offset;
7782
7783 sym = new_symbol (child_die, NULL, cu);
7784 if (sym != NULL
7785 && handle_data_member_location (child_die, cu, &offset))
7786 {
7787 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
7788 add_symbol_to_list (sym, &global_symbols);
7789 }
7790 child_die = sibling_die (child_die);
7791 }
7792 }
7793 }
7794
7795 /* Create a type for a C++ namespace. */
7796
7797 static struct type *
7798 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
7799 {
7800 struct objfile *objfile = cu->objfile;
7801 const char *previous_prefix, *name;
7802 int is_anonymous;
7803 struct type *type;
7804
7805 /* For extensions, reuse the type of the original namespace. */
7806 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
7807 {
7808 struct die_info *ext_die;
7809 struct dwarf2_cu *ext_cu = cu;
7810
7811 ext_die = dwarf2_extension (die, &ext_cu);
7812 type = read_type_die (ext_die, ext_cu);
7813
7814 /* EXT_CU may not be the same as CU.
7815 Ensure TYPE is recorded in CU's type_hash table. */
7816 return set_die_type (die, type, cu);
7817 }
7818
7819 name = namespace_name (die, &is_anonymous, cu);
7820
7821 /* Now build the name of the current namespace. */
7822
7823 previous_prefix = determine_prefix (die, cu);
7824 if (previous_prefix[0] != '\0')
7825 name = typename_concat (&objfile->objfile_obstack,
7826 previous_prefix, name, 0, cu);
7827
7828 /* Create the type. */
7829 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
7830 objfile);
7831 TYPE_NAME (type) = (char *) name;
7832 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7833
7834 return set_die_type (die, type, cu);
7835 }
7836
7837 /* Read a C++ namespace. */
7838
7839 static void
7840 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
7841 {
7842 struct objfile *objfile = cu->objfile;
7843 int is_anonymous;
7844
7845 /* Add a symbol associated to this if we haven't seen the namespace
7846 before. Also, add a using directive if it's an anonymous
7847 namespace. */
7848
7849 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
7850 {
7851 struct type *type;
7852
7853 type = read_type_die (die, cu);
7854 new_symbol (die, type, cu);
7855
7856 namespace_name (die, &is_anonymous, cu);
7857 if (is_anonymous)
7858 {
7859 const char *previous_prefix = determine_prefix (die, cu);
7860
7861 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
7862 NULL, NULL, &objfile->objfile_obstack);
7863 }
7864 }
7865
7866 if (die->child != NULL)
7867 {
7868 struct die_info *child_die = die->child;
7869
7870 while (child_die && child_die->tag)
7871 {
7872 process_die (child_die, cu);
7873 child_die = sibling_die (child_die);
7874 }
7875 }
7876 }
7877
7878 /* Read a Fortran module as type. This DIE can be only a declaration used for
7879 imported module. Still we need that type as local Fortran "use ... only"
7880 declaration imports depend on the created type in determine_prefix. */
7881
7882 static struct type *
7883 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
7884 {
7885 struct objfile *objfile = cu->objfile;
7886 char *module_name;
7887 struct type *type;
7888
7889 module_name = dwarf2_name (die, cu);
7890 if (!module_name)
7891 complaint (&symfile_complaints,
7892 _("DW_TAG_module has no name, offset 0x%x"),
7893 die->offset);
7894 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
7895
7896 /* determine_prefix uses TYPE_TAG_NAME. */
7897 TYPE_TAG_NAME (type) = TYPE_NAME (type);
7898
7899 return set_die_type (die, type, cu);
7900 }
7901
7902 /* Read a Fortran module. */
7903
7904 static void
7905 read_module (struct die_info *die, struct dwarf2_cu *cu)
7906 {
7907 struct die_info *child_die = die->child;
7908
7909 while (child_die && child_die->tag)
7910 {
7911 process_die (child_die, cu);
7912 child_die = sibling_die (child_die);
7913 }
7914 }
7915
7916 /* Return the name of the namespace represented by DIE. Set
7917 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
7918 namespace. */
7919
7920 static const char *
7921 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
7922 {
7923 struct die_info *current_die;
7924 const char *name = NULL;
7925
7926 /* Loop through the extensions until we find a name. */
7927
7928 for (current_die = die;
7929 current_die != NULL;
7930 current_die = dwarf2_extension (die, &cu))
7931 {
7932 name = dwarf2_name (current_die, cu);
7933 if (name != NULL)
7934 break;
7935 }
7936
7937 /* Is it an anonymous namespace? */
7938
7939 *is_anonymous = (name == NULL);
7940 if (*is_anonymous)
7941 name = CP_ANONYMOUS_NAMESPACE_STR;
7942
7943 return name;
7944 }
7945
7946 /* Extract all information from a DW_TAG_pointer_type DIE and add to
7947 the user defined type vector. */
7948
7949 static struct type *
7950 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
7951 {
7952 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
7953 struct comp_unit_head *cu_header = &cu->header;
7954 struct type *type;
7955 struct attribute *attr_byte_size;
7956 struct attribute *attr_address_class;
7957 int byte_size, addr_class;
7958 struct type *target_type;
7959
7960 target_type = die_type (die, cu);
7961
7962 /* The die_type call above may have already set the type for this DIE. */
7963 type = get_die_type (die, cu);
7964 if (type)
7965 return type;
7966
7967 type = lookup_pointer_type (target_type);
7968
7969 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
7970 if (attr_byte_size)
7971 byte_size = DW_UNSND (attr_byte_size);
7972 else
7973 byte_size = cu_header->addr_size;
7974
7975 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
7976 if (attr_address_class)
7977 addr_class = DW_UNSND (attr_address_class);
7978 else
7979 addr_class = DW_ADDR_none;
7980
7981 /* If the pointer size or address class is different than the
7982 default, create a type variant marked as such and set the
7983 length accordingly. */
7984 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
7985 {
7986 if (gdbarch_address_class_type_flags_p (gdbarch))
7987 {
7988 int type_flags;
7989
7990 type_flags = gdbarch_address_class_type_flags
7991 (gdbarch, byte_size, addr_class);
7992 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
7993 == 0);
7994 type = make_type_with_address_space (type, type_flags);
7995 }
7996 else if (TYPE_LENGTH (type) != byte_size)
7997 {
7998 complaint (&symfile_complaints,
7999 _("invalid pointer size %d"), byte_size);
8000 }
8001 else
8002 {
8003 /* Should we also complain about unhandled address classes? */
8004 }
8005 }
8006
8007 TYPE_LENGTH (type) = byte_size;
8008 return set_die_type (die, type, cu);
8009 }
8010
8011 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8012 the user defined type vector. */
8013
8014 static struct type *
8015 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8016 {
8017 struct type *type;
8018 struct type *to_type;
8019 struct type *domain;
8020
8021 to_type = die_type (die, cu);
8022 domain = die_containing_type (die, cu);
8023
8024 /* The calls above may have already set the type for this DIE. */
8025 type = get_die_type (die, cu);
8026 if (type)
8027 return type;
8028
8029 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8030 type = lookup_methodptr_type (to_type);
8031 else
8032 type = lookup_memberptr_type (to_type, domain);
8033
8034 return set_die_type (die, type, cu);
8035 }
8036
8037 /* Extract all information from a DW_TAG_reference_type DIE and add to
8038 the user defined type vector. */
8039
8040 static struct type *
8041 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8042 {
8043 struct comp_unit_head *cu_header = &cu->header;
8044 struct type *type, *target_type;
8045 struct attribute *attr;
8046
8047 target_type = die_type (die, cu);
8048
8049 /* The die_type call above may have already set the type for this DIE. */
8050 type = get_die_type (die, cu);
8051 if (type)
8052 return type;
8053
8054 type = lookup_reference_type (target_type);
8055 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8056 if (attr)
8057 {
8058 TYPE_LENGTH (type) = DW_UNSND (attr);
8059 }
8060 else
8061 {
8062 TYPE_LENGTH (type) = cu_header->addr_size;
8063 }
8064 return set_die_type (die, type, cu);
8065 }
8066
8067 static struct type *
8068 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8069 {
8070 struct type *base_type, *cv_type;
8071
8072 base_type = die_type (die, cu);
8073
8074 /* The die_type call above may have already set the type for this DIE. */
8075 cv_type = get_die_type (die, cu);
8076 if (cv_type)
8077 return cv_type;
8078
8079 /* In case the const qualifier is applied to an array type, the element type
8080 is so qualified, not the array type (section 6.7.3 of C99). */
8081 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8082 {
8083 struct type *el_type, *inner_array;
8084
8085 base_type = copy_type (base_type);
8086 inner_array = base_type;
8087
8088 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8089 {
8090 TYPE_TARGET_TYPE (inner_array) =
8091 copy_type (TYPE_TARGET_TYPE (inner_array));
8092 inner_array = TYPE_TARGET_TYPE (inner_array);
8093 }
8094
8095 el_type = TYPE_TARGET_TYPE (inner_array);
8096 TYPE_TARGET_TYPE (inner_array) =
8097 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8098
8099 return set_die_type (die, base_type, cu);
8100 }
8101
8102 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8103 return set_die_type (die, cv_type, cu);
8104 }
8105
8106 static struct type *
8107 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8108 {
8109 struct type *base_type, *cv_type;
8110
8111 base_type = die_type (die, cu);
8112
8113 /* The die_type call above may have already set the type for this DIE. */
8114 cv_type = get_die_type (die, cu);
8115 if (cv_type)
8116 return cv_type;
8117
8118 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8119 return set_die_type (die, cv_type, cu);
8120 }
8121
8122 /* Extract all information from a DW_TAG_string_type DIE and add to
8123 the user defined type vector. It isn't really a user defined type,
8124 but it behaves like one, with other DIE's using an AT_user_def_type
8125 attribute to reference it. */
8126
8127 static struct type *
8128 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8129 {
8130 struct objfile *objfile = cu->objfile;
8131 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8132 struct type *type, *range_type, *index_type, *char_type;
8133 struct attribute *attr;
8134 unsigned int length;
8135
8136 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8137 if (attr)
8138 {
8139 length = DW_UNSND (attr);
8140 }
8141 else
8142 {
8143 /* Check for the DW_AT_byte_size attribute. */
8144 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8145 if (attr)
8146 {
8147 length = DW_UNSND (attr);
8148 }
8149 else
8150 {
8151 length = 1;
8152 }
8153 }
8154
8155 index_type = objfile_type (objfile)->builtin_int;
8156 range_type = create_range_type (NULL, index_type, 1, length);
8157 char_type = language_string_char_type (cu->language_defn, gdbarch);
8158 type = create_string_type (NULL, char_type, range_type);
8159
8160 return set_die_type (die, type, cu);
8161 }
8162
8163 /* Handle DIES due to C code like:
8164
8165 struct foo
8166 {
8167 int (*funcp)(int a, long l);
8168 int b;
8169 };
8170
8171 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8172
8173 static struct type *
8174 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8175 {
8176 struct type *type; /* Type that this function returns. */
8177 struct type *ftype; /* Function that returns above type. */
8178 struct attribute *attr;
8179
8180 type = die_type (die, cu);
8181
8182 /* The die_type call above may have already set the type for this DIE. */
8183 ftype = get_die_type (die, cu);
8184 if (ftype)
8185 return ftype;
8186
8187 ftype = lookup_function_type (type);
8188
8189 /* All functions in C++, Pascal and Java have prototypes. */
8190 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8191 if ((attr && (DW_UNSND (attr) != 0))
8192 || cu->language == language_cplus
8193 || cu->language == language_java
8194 || cu->language == language_pascal)
8195 TYPE_PROTOTYPED (ftype) = 1;
8196 else if (producer_is_realview (cu->producer))
8197 /* RealView does not emit DW_AT_prototyped. We can not
8198 distinguish prototyped and unprototyped functions; default to
8199 prototyped, since that is more common in modern code (and
8200 RealView warns about unprototyped functions). */
8201 TYPE_PROTOTYPED (ftype) = 1;
8202
8203 /* Store the calling convention in the type if it's available in
8204 the subroutine die. Otherwise set the calling convention to
8205 the default value DW_CC_normal. */
8206 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8207 if (attr)
8208 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8209 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8210 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8211 else
8212 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8213
8214 /* We need to add the subroutine type to the die immediately so
8215 we don't infinitely recurse when dealing with parameters
8216 declared as the same subroutine type. */
8217 set_die_type (die, ftype, cu);
8218
8219 if (die->child != NULL)
8220 {
8221 struct type *void_type = objfile_type (cu->objfile)->builtin_void;
8222 struct die_info *child_die;
8223 int nparams, iparams;
8224
8225 /* Count the number of parameters.
8226 FIXME: GDB currently ignores vararg functions, but knows about
8227 vararg member functions. */
8228 nparams = 0;
8229 child_die = die->child;
8230 while (child_die && child_die->tag)
8231 {
8232 if (child_die->tag == DW_TAG_formal_parameter)
8233 nparams++;
8234 else if (child_die->tag == DW_TAG_unspecified_parameters)
8235 TYPE_VARARGS (ftype) = 1;
8236 child_die = sibling_die (child_die);
8237 }
8238
8239 /* Allocate storage for parameters and fill them in. */
8240 TYPE_NFIELDS (ftype) = nparams;
8241 TYPE_FIELDS (ftype) = (struct field *)
8242 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8243
8244 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8245 even if we error out during the parameters reading below. */
8246 for (iparams = 0; iparams < nparams; iparams++)
8247 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8248
8249 iparams = 0;
8250 child_die = die->child;
8251 while (child_die && child_die->tag)
8252 {
8253 if (child_die->tag == DW_TAG_formal_parameter)
8254 {
8255 struct type *arg_type;
8256
8257 /* DWARF version 2 has no clean way to discern C++
8258 static and non-static member functions. G++ helps
8259 GDB by marking the first parameter for non-static
8260 member functions (which is the this pointer) as
8261 artificial. We pass this information to
8262 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8263
8264 DWARF version 3 added DW_AT_object_pointer, which GCC
8265 4.5 does not yet generate. */
8266 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8267 if (attr)
8268 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8269 else
8270 {
8271 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8272
8273 /* GCC/43521: In java, the formal parameter
8274 "this" is sometimes not marked with DW_AT_artificial. */
8275 if (cu->language == language_java)
8276 {
8277 const char *name = dwarf2_name (child_die, cu);
8278
8279 if (name && !strcmp (name, "this"))
8280 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8281 }
8282 }
8283 arg_type = die_type (child_die, cu);
8284
8285 /* RealView does not mark THIS as const, which the testsuite
8286 expects. GCC marks THIS as const in method definitions,
8287 but not in the class specifications (GCC PR 43053). */
8288 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8289 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8290 {
8291 int is_this = 0;
8292 struct dwarf2_cu *arg_cu = cu;
8293 const char *name = dwarf2_name (child_die, cu);
8294
8295 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8296 if (attr)
8297 {
8298 /* If the compiler emits this, use it. */
8299 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8300 is_this = 1;
8301 }
8302 else if (name && strcmp (name, "this") == 0)
8303 /* Function definitions will have the argument names. */
8304 is_this = 1;
8305 else if (name == NULL && iparams == 0)
8306 /* Declarations may not have the names, so like
8307 elsewhere in GDB, assume an artificial first
8308 argument is "this". */
8309 is_this = 1;
8310
8311 if (is_this)
8312 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8313 arg_type, 0);
8314 }
8315
8316 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8317 iparams++;
8318 }
8319 child_die = sibling_die (child_die);
8320 }
8321 }
8322
8323 return ftype;
8324 }
8325
8326 static struct type *
8327 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8328 {
8329 struct objfile *objfile = cu->objfile;
8330 const char *name = NULL;
8331 struct type *this_type;
8332
8333 name = dwarf2_full_name (NULL, die, cu);
8334 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8335 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8336 TYPE_NAME (this_type) = (char *) name;
8337 set_die_type (die, this_type, cu);
8338 TYPE_TARGET_TYPE (this_type) = die_type (die, cu);
8339 return this_type;
8340 }
8341
8342 /* Find a representation of a given base type and install
8343 it in the TYPE field of the die. */
8344
8345 static struct type *
8346 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8347 {
8348 struct objfile *objfile = cu->objfile;
8349 struct type *type;
8350 struct attribute *attr;
8351 int encoding = 0, size = 0;
8352 char *name;
8353 enum type_code code = TYPE_CODE_INT;
8354 int type_flags = 0;
8355 struct type *target_type = NULL;
8356
8357 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8358 if (attr)
8359 {
8360 encoding = DW_UNSND (attr);
8361 }
8362 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8363 if (attr)
8364 {
8365 size = DW_UNSND (attr);
8366 }
8367 name = dwarf2_name (die, cu);
8368 if (!name)
8369 {
8370 complaint (&symfile_complaints,
8371 _("DW_AT_name missing from DW_TAG_base_type"));
8372 }
8373
8374 switch (encoding)
8375 {
8376 case DW_ATE_address:
8377 /* Turn DW_ATE_address into a void * pointer. */
8378 code = TYPE_CODE_PTR;
8379 type_flags |= TYPE_FLAG_UNSIGNED;
8380 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8381 break;
8382 case DW_ATE_boolean:
8383 code = TYPE_CODE_BOOL;
8384 type_flags |= TYPE_FLAG_UNSIGNED;
8385 break;
8386 case DW_ATE_complex_float:
8387 code = TYPE_CODE_COMPLEX;
8388 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8389 break;
8390 case DW_ATE_decimal_float:
8391 code = TYPE_CODE_DECFLOAT;
8392 break;
8393 case DW_ATE_float:
8394 code = TYPE_CODE_FLT;
8395 break;
8396 case DW_ATE_signed:
8397 break;
8398 case DW_ATE_unsigned:
8399 type_flags |= TYPE_FLAG_UNSIGNED;
8400 if (cu->language == language_fortran
8401 && name
8402 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8403 code = TYPE_CODE_CHAR;
8404 break;
8405 case DW_ATE_signed_char:
8406 if (cu->language == language_ada || cu->language == language_m2
8407 || cu->language == language_pascal
8408 || cu->language == language_fortran)
8409 code = TYPE_CODE_CHAR;
8410 break;
8411 case DW_ATE_unsigned_char:
8412 if (cu->language == language_ada || cu->language == language_m2
8413 || cu->language == language_pascal
8414 || cu->language == language_fortran)
8415 code = TYPE_CODE_CHAR;
8416 type_flags |= TYPE_FLAG_UNSIGNED;
8417 break;
8418 case DW_ATE_UTF:
8419 /* We just treat this as an integer and then recognize the
8420 type by name elsewhere. */
8421 break;
8422
8423 default:
8424 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8425 dwarf_type_encoding_name (encoding));
8426 break;
8427 }
8428
8429 type = init_type (code, size, type_flags, NULL, objfile);
8430 TYPE_NAME (type) = name;
8431 TYPE_TARGET_TYPE (type) = target_type;
8432
8433 if (name && strcmp (name, "char") == 0)
8434 TYPE_NOSIGN (type) = 1;
8435
8436 return set_die_type (die, type, cu);
8437 }
8438
8439 /* Read the given DW_AT_subrange DIE. */
8440
8441 static struct type *
8442 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8443 {
8444 struct type *base_type;
8445 struct type *range_type;
8446 struct attribute *attr;
8447 LONGEST low = 0;
8448 LONGEST high = -1;
8449 char *name;
8450 LONGEST negative_mask;
8451
8452 base_type = die_type (die, cu);
8453 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8454 check_typedef (base_type);
8455
8456 /* The die_type call above may have already set the type for this DIE. */
8457 range_type = get_die_type (die, cu);
8458 if (range_type)
8459 return range_type;
8460
8461 if (cu->language == language_fortran)
8462 {
8463 /* FORTRAN implies a lower bound of 1, if not given. */
8464 low = 1;
8465 }
8466
8467 /* FIXME: For variable sized arrays either of these could be
8468 a variable rather than a constant value. We'll allow it,
8469 but we don't know how to handle it. */
8470 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
8471 if (attr)
8472 low = dwarf2_get_attr_constant_value (attr, 0);
8473
8474 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
8475 if (attr)
8476 {
8477 if (attr->form == DW_FORM_block1 || is_ref_attr (attr))
8478 {
8479 /* GCC encodes arrays with unspecified or dynamic length
8480 with a DW_FORM_block1 attribute or a reference attribute.
8481 FIXME: GDB does not yet know how to handle dynamic
8482 arrays properly, treat them as arrays with unspecified
8483 length for now.
8484
8485 FIXME: jimb/2003-09-22: GDB does not really know
8486 how to handle arrays of unspecified length
8487 either; we just represent them as zero-length
8488 arrays. Choose an appropriate upper bound given
8489 the lower bound we've computed above. */
8490 high = low - 1;
8491 }
8492 else
8493 high = dwarf2_get_attr_constant_value (attr, 1);
8494 }
8495 else
8496 {
8497 attr = dwarf2_attr (die, DW_AT_count, cu);
8498 if (attr)
8499 {
8500 int count = dwarf2_get_attr_constant_value (attr, 1);
8501 high = low + count - 1;
8502 }
8503 else
8504 {
8505 /* Unspecified array length. */
8506 high = low - 1;
8507 }
8508 }
8509
8510 /* Dwarf-2 specifications explicitly allows to create subrange types
8511 without specifying a base type.
8512 In that case, the base type must be set to the type of
8513 the lower bound, upper bound or count, in that order, if any of these
8514 three attributes references an object that has a type.
8515 If no base type is found, the Dwarf-2 specifications say that
8516 a signed integer type of size equal to the size of an address should
8517 be used.
8518 For the following C code: `extern char gdb_int [];'
8519 GCC produces an empty range DIE.
8520 FIXME: muller/2010-05-28: Possible references to object for low bound,
8521 high bound or count are not yet handled by this code. */
8522 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
8523 {
8524 struct objfile *objfile = cu->objfile;
8525 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8526 int addr_size = gdbarch_addr_bit (gdbarch) /8;
8527 struct type *int_type = objfile_type (objfile)->builtin_int;
8528
8529 /* Test "int", "long int", and "long long int" objfile types,
8530 and select the first one having a size above or equal to the
8531 architecture address size. */
8532 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8533 base_type = int_type;
8534 else
8535 {
8536 int_type = objfile_type (objfile)->builtin_long;
8537 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8538 base_type = int_type;
8539 else
8540 {
8541 int_type = objfile_type (objfile)->builtin_long_long;
8542 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
8543 base_type = int_type;
8544 }
8545 }
8546 }
8547
8548 negative_mask =
8549 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
8550 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
8551 low |= negative_mask;
8552 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
8553 high |= negative_mask;
8554
8555 range_type = create_range_type (NULL, base_type, low, high);
8556
8557 /* Mark arrays with dynamic length at least as an array of unspecified
8558 length. GDB could check the boundary but before it gets implemented at
8559 least allow accessing the array elements. */
8560 if (attr && attr->form == DW_FORM_block1)
8561 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8562
8563 /* Ada expects an empty array on no boundary attributes. */
8564 if (attr == NULL && cu->language != language_ada)
8565 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
8566
8567 name = dwarf2_name (die, cu);
8568 if (name)
8569 TYPE_NAME (range_type) = name;
8570
8571 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8572 if (attr)
8573 TYPE_LENGTH (range_type) = DW_UNSND (attr);
8574
8575 set_die_type (die, range_type, cu);
8576
8577 /* set_die_type should be already done. */
8578 set_descriptive_type (range_type, die, cu);
8579
8580 return range_type;
8581 }
8582
8583 static struct type *
8584 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
8585 {
8586 struct type *type;
8587
8588 /* For now, we only support the C meaning of an unspecified type: void. */
8589
8590 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
8591 TYPE_NAME (type) = dwarf2_name (die, cu);
8592
8593 return set_die_type (die, type, cu);
8594 }
8595
8596 /* Trivial hash function for die_info: the hash value of a DIE
8597 is its offset in .debug_info for this objfile. */
8598
8599 static hashval_t
8600 die_hash (const void *item)
8601 {
8602 const struct die_info *die = item;
8603
8604 return die->offset;
8605 }
8606
8607 /* Trivial comparison function for die_info structures: two DIEs
8608 are equal if they have the same offset. */
8609
8610 static int
8611 die_eq (const void *item_lhs, const void *item_rhs)
8612 {
8613 const struct die_info *die_lhs = item_lhs;
8614 const struct die_info *die_rhs = item_rhs;
8615
8616 return die_lhs->offset == die_rhs->offset;
8617 }
8618
8619 /* Read a whole compilation unit into a linked list of dies. */
8620
8621 static struct die_info *
8622 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
8623 {
8624 struct die_reader_specs reader_specs;
8625 int read_abbrevs = 0;
8626 struct cleanup *back_to = NULL;
8627 struct die_info *die;
8628
8629 if (cu->dwarf2_abbrevs == NULL)
8630 {
8631 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
8632 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
8633 read_abbrevs = 1;
8634 }
8635
8636 gdb_assert (cu->die_hash == NULL);
8637 cu->die_hash
8638 = htab_create_alloc_ex (cu->header.length / 12,
8639 die_hash,
8640 die_eq,
8641 NULL,
8642 &cu->comp_unit_obstack,
8643 hashtab_obstack_allocate,
8644 dummy_obstack_deallocate);
8645
8646 init_cu_die_reader (&reader_specs, cu);
8647
8648 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
8649
8650 if (read_abbrevs)
8651 do_cleanups (back_to);
8652
8653 return die;
8654 }
8655
8656 /* Main entry point for reading a DIE and all children.
8657 Read the DIE and dump it if requested. */
8658
8659 static struct die_info *
8660 read_die_and_children (const struct die_reader_specs *reader,
8661 gdb_byte *info_ptr,
8662 gdb_byte **new_info_ptr,
8663 struct die_info *parent)
8664 {
8665 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
8666 new_info_ptr, parent);
8667
8668 if (dwarf2_die_debug)
8669 {
8670 fprintf_unfiltered (gdb_stdlog,
8671 "\nRead die from %s of %s:\n",
8672 reader->buffer == dwarf2_per_objfile->info.buffer
8673 ? ".debug_info"
8674 : reader->buffer == dwarf2_per_objfile->types.buffer
8675 ? ".debug_types"
8676 : "unknown section",
8677 reader->abfd->filename);
8678 dump_die (result, dwarf2_die_debug);
8679 }
8680
8681 return result;
8682 }
8683
8684 /* Read a single die and all its descendents. Set the die's sibling
8685 field to NULL; set other fields in the die correctly, and set all
8686 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
8687 location of the info_ptr after reading all of those dies. PARENT
8688 is the parent of the die in question. */
8689
8690 static struct die_info *
8691 read_die_and_children_1 (const struct die_reader_specs *reader,
8692 gdb_byte *info_ptr,
8693 gdb_byte **new_info_ptr,
8694 struct die_info *parent)
8695 {
8696 struct die_info *die;
8697 gdb_byte *cur_ptr;
8698 int has_children;
8699
8700 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
8701 if (die == NULL)
8702 {
8703 *new_info_ptr = cur_ptr;
8704 return NULL;
8705 }
8706 store_in_ref_table (die, reader->cu);
8707
8708 if (has_children)
8709 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
8710 else
8711 {
8712 die->child = NULL;
8713 *new_info_ptr = cur_ptr;
8714 }
8715
8716 die->sibling = NULL;
8717 die->parent = parent;
8718 return die;
8719 }
8720
8721 /* Read a die, all of its descendents, and all of its siblings; set
8722 all of the fields of all of the dies correctly. Arguments are as
8723 in read_die_and_children. */
8724
8725 static struct die_info *
8726 read_die_and_siblings (const struct die_reader_specs *reader,
8727 gdb_byte *info_ptr,
8728 gdb_byte **new_info_ptr,
8729 struct die_info *parent)
8730 {
8731 struct die_info *first_die, *last_sibling;
8732 gdb_byte *cur_ptr;
8733
8734 cur_ptr = info_ptr;
8735 first_die = last_sibling = NULL;
8736
8737 while (1)
8738 {
8739 struct die_info *die
8740 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
8741
8742 if (die == NULL)
8743 {
8744 *new_info_ptr = cur_ptr;
8745 return first_die;
8746 }
8747
8748 if (!first_die)
8749 first_die = die;
8750 else
8751 last_sibling->sibling = die;
8752
8753 last_sibling = die;
8754 }
8755 }
8756
8757 /* Read the die from the .debug_info section buffer. Set DIEP to
8758 point to a newly allocated die with its information, except for its
8759 child, sibling, and parent fields. Set HAS_CHILDREN to tell
8760 whether the die has children or not. */
8761
8762 static gdb_byte *
8763 read_full_die (const struct die_reader_specs *reader,
8764 struct die_info **diep, gdb_byte *info_ptr,
8765 int *has_children)
8766 {
8767 unsigned int abbrev_number, bytes_read, i, offset;
8768 struct abbrev_info *abbrev;
8769 struct die_info *die;
8770 struct dwarf2_cu *cu = reader->cu;
8771 bfd *abfd = reader->abfd;
8772
8773 offset = info_ptr - reader->buffer;
8774 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
8775 info_ptr += bytes_read;
8776 if (!abbrev_number)
8777 {
8778 *diep = NULL;
8779 *has_children = 0;
8780 return info_ptr;
8781 }
8782
8783 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
8784 if (!abbrev)
8785 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
8786 abbrev_number,
8787 bfd_get_filename (abfd));
8788
8789 die = dwarf_alloc_die (cu, abbrev->num_attrs);
8790 die->offset = offset;
8791 die->tag = abbrev->tag;
8792 die->abbrev = abbrev_number;
8793
8794 die->num_attrs = abbrev->num_attrs;
8795
8796 for (i = 0; i < abbrev->num_attrs; ++i)
8797 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
8798 abfd, info_ptr, cu);
8799
8800 *diep = die;
8801 *has_children = abbrev->has_children;
8802 return info_ptr;
8803 }
8804
8805 /* In DWARF version 2, the description of the debugging information is
8806 stored in a separate .debug_abbrev section. Before we read any
8807 dies from a section we read in all abbreviations and install them
8808 in a hash table. This function also sets flags in CU describing
8809 the data found in the abbrev table. */
8810
8811 static void
8812 dwarf2_read_abbrevs (bfd *abfd, struct dwarf2_cu *cu)
8813 {
8814 struct comp_unit_head *cu_header = &cu->header;
8815 gdb_byte *abbrev_ptr;
8816 struct abbrev_info *cur_abbrev;
8817 unsigned int abbrev_number, bytes_read, abbrev_name;
8818 unsigned int abbrev_form, hash_number;
8819 struct attr_abbrev *cur_attrs;
8820 unsigned int allocated_attrs;
8821
8822 /* Initialize dwarf2 abbrevs. */
8823 obstack_init (&cu->abbrev_obstack);
8824 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
8825 (ABBREV_HASH_SIZE
8826 * sizeof (struct abbrev_info *)));
8827 memset (cu->dwarf2_abbrevs, 0,
8828 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
8829
8830 dwarf2_read_section (dwarf2_per_objfile->objfile,
8831 &dwarf2_per_objfile->abbrev);
8832 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
8833 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8834 abbrev_ptr += bytes_read;
8835
8836 allocated_attrs = ATTR_ALLOC_CHUNK;
8837 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
8838
8839 /* Loop until we reach an abbrev number of 0. */
8840 while (abbrev_number)
8841 {
8842 cur_abbrev = dwarf_alloc_abbrev (cu);
8843
8844 /* read in abbrev header */
8845 cur_abbrev->number = abbrev_number;
8846 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8847 abbrev_ptr += bytes_read;
8848 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
8849 abbrev_ptr += 1;
8850
8851 if (cur_abbrev->tag == DW_TAG_namespace)
8852 cu->has_namespace_info = 1;
8853
8854 /* now read in declarations */
8855 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8856 abbrev_ptr += bytes_read;
8857 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8858 abbrev_ptr += bytes_read;
8859 while (abbrev_name)
8860 {
8861 if (cur_abbrev->num_attrs == allocated_attrs)
8862 {
8863 allocated_attrs += ATTR_ALLOC_CHUNK;
8864 cur_attrs
8865 = xrealloc (cur_attrs, (allocated_attrs
8866 * sizeof (struct attr_abbrev)));
8867 }
8868
8869 /* Record whether this compilation unit might have
8870 inter-compilation-unit references. If we don't know what form
8871 this attribute will have, then it might potentially be a
8872 DW_FORM_ref_addr, so we conservatively expect inter-CU
8873 references. */
8874
8875 if (abbrev_form == DW_FORM_ref_addr
8876 || abbrev_form == DW_FORM_indirect)
8877 cu->has_form_ref_addr = 1;
8878
8879 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
8880 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
8881 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8882 abbrev_ptr += bytes_read;
8883 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8884 abbrev_ptr += bytes_read;
8885 }
8886
8887 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
8888 (cur_abbrev->num_attrs
8889 * sizeof (struct attr_abbrev)));
8890 memcpy (cur_abbrev->attrs, cur_attrs,
8891 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
8892
8893 hash_number = abbrev_number % ABBREV_HASH_SIZE;
8894 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
8895 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
8896
8897 /* Get next abbreviation.
8898 Under Irix6 the abbreviations for a compilation unit are not
8899 always properly terminated with an abbrev number of 0.
8900 Exit loop if we encounter an abbreviation which we have
8901 already read (which means we are about to read the abbreviations
8902 for the next compile unit) or if the end of the abbreviation
8903 table is reached. */
8904 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
8905 >= dwarf2_per_objfile->abbrev.size)
8906 break;
8907 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
8908 abbrev_ptr += bytes_read;
8909 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
8910 break;
8911 }
8912
8913 xfree (cur_attrs);
8914 }
8915
8916 /* Release the memory used by the abbrev table for a compilation unit. */
8917
8918 static void
8919 dwarf2_free_abbrev_table (void *ptr_to_cu)
8920 {
8921 struct dwarf2_cu *cu = ptr_to_cu;
8922
8923 obstack_free (&cu->abbrev_obstack, NULL);
8924 cu->dwarf2_abbrevs = NULL;
8925 }
8926
8927 /* Lookup an abbrev_info structure in the abbrev hash table. */
8928
8929 static struct abbrev_info *
8930 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
8931 {
8932 unsigned int hash_number;
8933 struct abbrev_info *abbrev;
8934
8935 hash_number = number % ABBREV_HASH_SIZE;
8936 abbrev = cu->dwarf2_abbrevs[hash_number];
8937
8938 while (abbrev)
8939 {
8940 if (abbrev->number == number)
8941 return abbrev;
8942 else
8943 abbrev = abbrev->next;
8944 }
8945 return NULL;
8946 }
8947
8948 /* Returns nonzero if TAG represents a type that we might generate a partial
8949 symbol for. */
8950
8951 static int
8952 is_type_tag_for_partial (int tag)
8953 {
8954 switch (tag)
8955 {
8956 #if 0
8957 /* Some types that would be reasonable to generate partial symbols for,
8958 that we don't at present. */
8959 case DW_TAG_array_type:
8960 case DW_TAG_file_type:
8961 case DW_TAG_ptr_to_member_type:
8962 case DW_TAG_set_type:
8963 case DW_TAG_string_type:
8964 case DW_TAG_subroutine_type:
8965 #endif
8966 case DW_TAG_base_type:
8967 case DW_TAG_class_type:
8968 case DW_TAG_interface_type:
8969 case DW_TAG_enumeration_type:
8970 case DW_TAG_structure_type:
8971 case DW_TAG_subrange_type:
8972 case DW_TAG_typedef:
8973 case DW_TAG_union_type:
8974 return 1;
8975 default:
8976 return 0;
8977 }
8978 }
8979
8980 /* Load all DIEs that are interesting for partial symbols into memory. */
8981
8982 static struct partial_die_info *
8983 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
8984 int building_psymtab, struct dwarf2_cu *cu)
8985 {
8986 struct partial_die_info *part_die;
8987 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
8988 struct abbrev_info *abbrev;
8989 unsigned int bytes_read;
8990 unsigned int load_all = 0;
8991
8992 int nesting_level = 1;
8993
8994 parent_die = NULL;
8995 last_die = NULL;
8996
8997 if (cu->per_cu && cu->per_cu->load_all_dies)
8998 load_all = 1;
8999
9000 cu->partial_dies
9001 = htab_create_alloc_ex (cu->header.length / 12,
9002 partial_die_hash,
9003 partial_die_eq,
9004 NULL,
9005 &cu->comp_unit_obstack,
9006 hashtab_obstack_allocate,
9007 dummy_obstack_deallocate);
9008
9009 part_die = obstack_alloc (&cu->comp_unit_obstack,
9010 sizeof (struct partial_die_info));
9011
9012 while (1)
9013 {
9014 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9015
9016 /* A NULL abbrev means the end of a series of children. */
9017 if (abbrev == NULL)
9018 {
9019 if (--nesting_level == 0)
9020 {
9021 /* PART_DIE was probably the last thing allocated on the
9022 comp_unit_obstack, so we could call obstack_free
9023 here. We don't do that because the waste is small,
9024 and will be cleaned up when we're done with this
9025 compilation unit. This way, we're also more robust
9026 against other users of the comp_unit_obstack. */
9027 return first_die;
9028 }
9029 info_ptr += bytes_read;
9030 last_die = parent_die;
9031 parent_die = parent_die->die_parent;
9032 continue;
9033 }
9034
9035 /* Check for template arguments. We never save these; if
9036 they're seen, we just mark the parent, and go on our way. */
9037 if (parent_die != NULL
9038 && cu->language == language_cplus
9039 && (abbrev->tag == DW_TAG_template_type_param
9040 || abbrev->tag == DW_TAG_template_value_param))
9041 {
9042 parent_die->has_template_arguments = 1;
9043
9044 if (!load_all)
9045 {
9046 /* We don't need a partial DIE for the template argument. */
9047 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9048 cu);
9049 continue;
9050 }
9051 }
9052
9053 /* We only recurse into subprograms looking for template arguments.
9054 Skip their other children. */
9055 if (!load_all
9056 && cu->language == language_cplus
9057 && parent_die != NULL
9058 && parent_die->tag == DW_TAG_subprogram)
9059 {
9060 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9061 continue;
9062 }
9063
9064 /* Check whether this DIE is interesting enough to save. Normally
9065 we would not be interested in members here, but there may be
9066 later variables referencing them via DW_AT_specification (for
9067 static members). */
9068 if (!load_all
9069 && !is_type_tag_for_partial (abbrev->tag)
9070 && abbrev->tag != DW_TAG_constant
9071 && abbrev->tag != DW_TAG_enumerator
9072 && abbrev->tag != DW_TAG_subprogram
9073 && abbrev->tag != DW_TAG_lexical_block
9074 && abbrev->tag != DW_TAG_variable
9075 && abbrev->tag != DW_TAG_namespace
9076 && abbrev->tag != DW_TAG_module
9077 && abbrev->tag != DW_TAG_member)
9078 {
9079 /* Otherwise we skip to the next sibling, if any. */
9080 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9081 continue;
9082 }
9083
9084 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9085 buffer, info_ptr, cu);
9086
9087 /* This two-pass algorithm for processing partial symbols has a
9088 high cost in cache pressure. Thus, handle some simple cases
9089 here which cover the majority of C partial symbols. DIEs
9090 which neither have specification tags in them, nor could have
9091 specification tags elsewhere pointing at them, can simply be
9092 processed and discarded.
9093
9094 This segment is also optional; scan_partial_symbols and
9095 add_partial_symbol will handle these DIEs if we chain
9096 them in normally. When compilers which do not emit large
9097 quantities of duplicate debug information are more common,
9098 this code can probably be removed. */
9099
9100 /* Any complete simple types at the top level (pretty much all
9101 of them, for a language without namespaces), can be processed
9102 directly. */
9103 if (parent_die == NULL
9104 && part_die->has_specification == 0
9105 && part_die->is_declaration == 0
9106 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9107 || part_die->tag == DW_TAG_base_type
9108 || part_die->tag == DW_TAG_subrange_type))
9109 {
9110 if (building_psymtab && part_die->name != NULL)
9111 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9112 VAR_DOMAIN, LOC_TYPEDEF,
9113 &cu->objfile->static_psymbols,
9114 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9115 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9116 continue;
9117 }
9118
9119 /* The exception for DW_TAG_typedef with has_children above is
9120 a workaround of GCC PR debug/47510. In the case of this complaint
9121 type_name_no_tag_or_error will error on such types later.
9122
9123 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9124 it could not find the child DIEs referenced later, this is checked
9125 above. In correct DWARF DW_TAG_typedef should have no children. */
9126
9127 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9128 complaint (&symfile_complaints,
9129 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9130 "- DIE at 0x%x [in module %s]"),
9131 part_die->offset, cu->objfile->name);
9132
9133 /* If we're at the second level, and we're an enumerator, and
9134 our parent has no specification (meaning possibly lives in a
9135 namespace elsewhere), then we can add the partial symbol now
9136 instead of queueing it. */
9137 if (part_die->tag == DW_TAG_enumerator
9138 && parent_die != NULL
9139 && parent_die->die_parent == NULL
9140 && parent_die->tag == DW_TAG_enumeration_type
9141 && parent_die->has_specification == 0)
9142 {
9143 if (part_die->name == NULL)
9144 complaint (&symfile_complaints,
9145 _("malformed enumerator DIE ignored"));
9146 else if (building_psymtab)
9147 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9148 VAR_DOMAIN, LOC_CONST,
9149 (cu->language == language_cplus
9150 || cu->language == language_java)
9151 ? &cu->objfile->global_psymbols
9152 : &cu->objfile->static_psymbols,
9153 0, (CORE_ADDR) 0, cu->language, cu->objfile);
9154
9155 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9156 continue;
9157 }
9158
9159 /* We'll save this DIE so link it in. */
9160 part_die->die_parent = parent_die;
9161 part_die->die_sibling = NULL;
9162 part_die->die_child = NULL;
9163
9164 if (last_die && last_die == parent_die)
9165 last_die->die_child = part_die;
9166 else if (last_die)
9167 last_die->die_sibling = part_die;
9168
9169 last_die = part_die;
9170
9171 if (first_die == NULL)
9172 first_die = part_die;
9173
9174 /* Maybe add the DIE to the hash table. Not all DIEs that we
9175 find interesting need to be in the hash table, because we
9176 also have the parent/sibling/child chains; only those that we
9177 might refer to by offset later during partial symbol reading.
9178
9179 For now this means things that might have be the target of a
9180 DW_AT_specification, DW_AT_abstract_origin, or
9181 DW_AT_extension. DW_AT_extension will refer only to
9182 namespaces; DW_AT_abstract_origin refers to functions (and
9183 many things under the function DIE, but we do not recurse
9184 into function DIEs during partial symbol reading) and
9185 possibly variables as well; DW_AT_specification refers to
9186 declarations. Declarations ought to have the DW_AT_declaration
9187 flag. It happens that GCC forgets to put it in sometimes, but
9188 only for functions, not for types.
9189
9190 Adding more things than necessary to the hash table is harmless
9191 except for the performance cost. Adding too few will result in
9192 wasted time in find_partial_die, when we reread the compilation
9193 unit with load_all_dies set. */
9194
9195 if (load_all
9196 || abbrev->tag == DW_TAG_constant
9197 || abbrev->tag == DW_TAG_subprogram
9198 || abbrev->tag == DW_TAG_variable
9199 || abbrev->tag == DW_TAG_namespace
9200 || part_die->is_declaration)
9201 {
9202 void **slot;
9203
9204 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9205 part_die->offset, INSERT);
9206 *slot = part_die;
9207 }
9208
9209 part_die = obstack_alloc (&cu->comp_unit_obstack,
9210 sizeof (struct partial_die_info));
9211
9212 /* For some DIEs we want to follow their children (if any). For C
9213 we have no reason to follow the children of structures; for other
9214 languages we have to, so that we can get at method physnames
9215 to infer fully qualified class names, for DW_AT_specification,
9216 and for C++ template arguments. For C++, we also look one level
9217 inside functions to find template arguments (if the name of the
9218 function does not already contain the template arguments).
9219
9220 For Ada, we need to scan the children of subprograms and lexical
9221 blocks as well because Ada allows the definition of nested
9222 entities that could be interesting for the debugger, such as
9223 nested subprograms for instance. */
9224 if (last_die->has_children
9225 && (load_all
9226 || last_die->tag == DW_TAG_namespace
9227 || last_die->tag == DW_TAG_module
9228 || last_die->tag == DW_TAG_enumeration_type
9229 || (cu->language == language_cplus
9230 && last_die->tag == DW_TAG_subprogram
9231 && (last_die->name == NULL
9232 || strchr (last_die->name, '<') == NULL))
9233 || (cu->language != language_c
9234 && (last_die->tag == DW_TAG_class_type
9235 || last_die->tag == DW_TAG_interface_type
9236 || last_die->tag == DW_TAG_structure_type
9237 || last_die->tag == DW_TAG_union_type))
9238 || (cu->language == language_ada
9239 && (last_die->tag == DW_TAG_subprogram
9240 || last_die->tag == DW_TAG_lexical_block))))
9241 {
9242 nesting_level++;
9243 parent_die = last_die;
9244 continue;
9245 }
9246
9247 /* Otherwise we skip to the next sibling, if any. */
9248 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9249
9250 /* Back to the top, do it again. */
9251 }
9252 }
9253
9254 /* Read a minimal amount of information into the minimal die structure. */
9255
9256 static gdb_byte *
9257 read_partial_die (struct partial_die_info *part_die,
9258 struct abbrev_info *abbrev,
9259 unsigned int abbrev_len, bfd *abfd,
9260 gdb_byte *buffer, gdb_byte *info_ptr,
9261 struct dwarf2_cu *cu)
9262 {
9263 unsigned int i;
9264 struct attribute attr;
9265 int has_low_pc_attr = 0;
9266 int has_high_pc_attr = 0;
9267
9268 memset (part_die, 0, sizeof (struct partial_die_info));
9269
9270 part_die->offset = info_ptr - buffer;
9271
9272 info_ptr += abbrev_len;
9273
9274 if (abbrev == NULL)
9275 return info_ptr;
9276
9277 part_die->tag = abbrev->tag;
9278 part_die->has_children = abbrev->has_children;
9279
9280 for (i = 0; i < abbrev->num_attrs; ++i)
9281 {
9282 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9283
9284 /* Store the data if it is of an attribute we want to keep in a
9285 partial symbol table. */
9286 switch (attr.name)
9287 {
9288 case DW_AT_name:
9289 switch (part_die->tag)
9290 {
9291 case DW_TAG_compile_unit:
9292 case DW_TAG_type_unit:
9293 /* Compilation units have a DW_AT_name that is a filename, not
9294 a source language identifier. */
9295 case DW_TAG_enumeration_type:
9296 case DW_TAG_enumerator:
9297 /* These tags always have simple identifiers already; no need
9298 to canonicalize them. */
9299 part_die->name = DW_STRING (&attr);
9300 break;
9301 default:
9302 part_die->name
9303 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9304 &cu->objfile->objfile_obstack);
9305 break;
9306 }
9307 break;
9308 case DW_AT_linkage_name:
9309 case DW_AT_MIPS_linkage_name:
9310 /* Note that both forms of linkage name might appear. We
9311 assume they will be the same, and we only store the last
9312 one we see. */
9313 if (cu->language == language_ada)
9314 part_die->name = DW_STRING (&attr);
9315 part_die->linkage_name = DW_STRING (&attr);
9316 break;
9317 case DW_AT_low_pc:
9318 has_low_pc_attr = 1;
9319 part_die->lowpc = DW_ADDR (&attr);
9320 break;
9321 case DW_AT_high_pc:
9322 has_high_pc_attr = 1;
9323 part_die->highpc = DW_ADDR (&attr);
9324 break;
9325 case DW_AT_location:
9326 /* Support the .debug_loc offsets. */
9327 if (attr_form_is_block (&attr))
9328 {
9329 part_die->locdesc = DW_BLOCK (&attr);
9330 }
9331 else if (attr_form_is_section_offset (&attr))
9332 {
9333 dwarf2_complex_location_expr_complaint ();
9334 }
9335 else
9336 {
9337 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9338 "partial symbol information");
9339 }
9340 break;
9341 case DW_AT_external:
9342 part_die->is_external = DW_UNSND (&attr);
9343 break;
9344 case DW_AT_declaration:
9345 part_die->is_declaration = DW_UNSND (&attr);
9346 break;
9347 case DW_AT_type:
9348 part_die->has_type = 1;
9349 break;
9350 case DW_AT_abstract_origin:
9351 case DW_AT_specification:
9352 case DW_AT_extension:
9353 part_die->has_specification = 1;
9354 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9355 break;
9356 case DW_AT_sibling:
9357 /* Ignore absolute siblings, they might point outside of
9358 the current compile unit. */
9359 if (attr.form == DW_FORM_ref_addr)
9360 complaint (&symfile_complaints,
9361 _("ignoring absolute DW_AT_sibling"));
9362 else
9363 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9364 break;
9365 case DW_AT_byte_size:
9366 part_die->has_byte_size = 1;
9367 break;
9368 case DW_AT_calling_convention:
9369 /* DWARF doesn't provide a way to identify a program's source-level
9370 entry point. DW_AT_calling_convention attributes are only meant
9371 to describe functions' calling conventions.
9372
9373 However, because it's a necessary piece of information in
9374 Fortran, and because DW_CC_program is the only piece of debugging
9375 information whose definition refers to a 'main program' at all,
9376 several compilers have begun marking Fortran main programs with
9377 DW_CC_program --- even when those functions use the standard
9378 calling conventions.
9379
9380 So until DWARF specifies a way to provide this information and
9381 compilers pick up the new representation, we'll support this
9382 practice. */
9383 if (DW_UNSND (&attr) == DW_CC_program
9384 && cu->language == language_fortran)
9385 {
9386 set_main_name (part_die->name);
9387
9388 /* As this DIE has a static linkage the name would be difficult
9389 to look up later. */
9390 language_of_main = language_fortran;
9391 }
9392 break;
9393 default:
9394 break;
9395 }
9396 }
9397
9398 if (has_low_pc_attr && has_high_pc_attr)
9399 {
9400 /* When using the GNU linker, .gnu.linkonce. sections are used to
9401 eliminate duplicate copies of functions and vtables and such.
9402 The linker will arbitrarily choose one and discard the others.
9403 The AT_*_pc values for such functions refer to local labels in
9404 these sections. If the section from that file was discarded, the
9405 labels are not in the output, so the relocs get a value of 0.
9406 If this is a discarded function, mark the pc bounds as invalid,
9407 so that GDB will ignore it. */
9408 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9409 {
9410 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9411
9412 complaint (&symfile_complaints,
9413 _("DW_AT_low_pc %s is zero "
9414 "for DIE at 0x%x [in module %s]"),
9415 paddress (gdbarch, part_die->lowpc),
9416 part_die->offset, cu->objfile->name);
9417 }
9418 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9419 else if (part_die->lowpc >= part_die->highpc)
9420 {
9421 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
9422
9423 complaint (&symfile_complaints,
9424 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9425 "for DIE at 0x%x [in module %s]"),
9426 paddress (gdbarch, part_die->lowpc),
9427 paddress (gdbarch, part_die->highpc),
9428 part_die->offset, cu->objfile->name);
9429 }
9430 else
9431 part_die->has_pc_info = 1;
9432 }
9433
9434 return info_ptr;
9435 }
9436
9437 /* Find a cached partial DIE at OFFSET in CU. */
9438
9439 static struct partial_die_info *
9440 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9441 {
9442 struct partial_die_info *lookup_die = NULL;
9443 struct partial_die_info part_die;
9444
9445 part_die.offset = offset;
9446 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9447
9448 return lookup_die;
9449 }
9450
9451 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9452 except in the case of .debug_types DIEs which do not reference
9453 outside their CU (they do however referencing other types via
9454 DW_FORM_ref_sig8). */
9455
9456 static struct partial_die_info *
9457 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9458 {
9459 struct dwarf2_per_cu_data *per_cu = NULL;
9460 struct partial_die_info *pd = NULL;
9461
9462 if (cu->per_cu->from_debug_types)
9463 {
9464 pd = find_partial_die_in_comp_unit (offset, cu);
9465 if (pd != NULL)
9466 return pd;
9467 goto not_found;
9468 }
9469
9470 if (offset_in_cu_p (&cu->header, offset))
9471 {
9472 pd = find_partial_die_in_comp_unit (offset, cu);
9473 if (pd != NULL)
9474 return pd;
9475 }
9476
9477 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
9478
9479 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
9480 load_partial_comp_unit (per_cu, cu->objfile);
9481
9482 per_cu->cu->last_used = 0;
9483 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9484
9485 if (pd == NULL && per_cu->load_all_dies == 0)
9486 {
9487 struct cleanup *back_to;
9488 struct partial_die_info comp_unit_die;
9489 struct abbrev_info *abbrev;
9490 unsigned int bytes_read;
9491 char *info_ptr;
9492
9493 per_cu->load_all_dies = 1;
9494
9495 /* Re-read the DIEs. */
9496 back_to = make_cleanup (null_cleanup, 0);
9497 if (per_cu->cu->dwarf2_abbrevs == NULL)
9498 {
9499 dwarf2_read_abbrevs (per_cu->cu->objfile->obfd, per_cu->cu);
9500 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
9501 }
9502 info_ptr = (dwarf2_per_objfile->info.buffer
9503 + per_cu->cu->header.offset
9504 + per_cu->cu->header.first_die_offset);
9505 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
9506 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
9507 per_cu->cu->objfile->obfd,
9508 dwarf2_per_objfile->info.buffer, info_ptr,
9509 per_cu->cu);
9510 if (comp_unit_die.has_children)
9511 load_partial_dies (per_cu->cu->objfile->obfd,
9512 dwarf2_per_objfile->info.buffer, info_ptr,
9513 0, per_cu->cu);
9514 do_cleanups (back_to);
9515
9516 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
9517 }
9518
9519 not_found:
9520
9521 if (pd == NULL)
9522 internal_error (__FILE__, __LINE__,
9523 _("could not find partial DIE 0x%x "
9524 "in cache [from module %s]\n"),
9525 offset, bfd_get_filename (cu->objfile->obfd));
9526 return pd;
9527 }
9528
9529 /* See if we can figure out if the class lives in a namespace. We do
9530 this by looking for a member function; its demangled name will
9531 contain namespace info, if there is any. */
9532
9533 static void
9534 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
9535 struct dwarf2_cu *cu)
9536 {
9537 /* NOTE: carlton/2003-10-07: Getting the info this way changes
9538 what template types look like, because the demangler
9539 frequently doesn't give the same name as the debug info. We
9540 could fix this by only using the demangled name to get the
9541 prefix (but see comment in read_structure_type). */
9542
9543 struct partial_die_info *real_pdi;
9544 struct partial_die_info *child_pdi;
9545
9546 /* If this DIE (this DIE's specification, if any) has a parent, then
9547 we should not do this. We'll prepend the parent's fully qualified
9548 name when we create the partial symbol. */
9549
9550 real_pdi = struct_pdi;
9551 while (real_pdi->has_specification)
9552 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
9553
9554 if (real_pdi->die_parent != NULL)
9555 return;
9556
9557 for (child_pdi = struct_pdi->die_child;
9558 child_pdi != NULL;
9559 child_pdi = child_pdi->die_sibling)
9560 {
9561 if (child_pdi->tag == DW_TAG_subprogram
9562 && child_pdi->linkage_name != NULL)
9563 {
9564 char *actual_class_name
9565 = language_class_name_from_physname (cu->language_defn,
9566 child_pdi->linkage_name);
9567 if (actual_class_name != NULL)
9568 {
9569 struct_pdi->name
9570 = obsavestring (actual_class_name,
9571 strlen (actual_class_name),
9572 &cu->objfile->objfile_obstack);
9573 xfree (actual_class_name);
9574 }
9575 break;
9576 }
9577 }
9578 }
9579
9580 /* Adjust PART_DIE before generating a symbol for it. This function
9581 may set the is_external flag or change the DIE's name. */
9582
9583 static void
9584 fixup_partial_die (struct partial_die_info *part_die,
9585 struct dwarf2_cu *cu)
9586 {
9587 /* Once we've fixed up a die, there's no point in doing so again.
9588 This also avoids a memory leak if we were to call
9589 guess_partial_die_structure_name multiple times. */
9590 if (part_die->fixup_called)
9591 return;
9592
9593 /* If we found a reference attribute and the DIE has no name, try
9594 to find a name in the referred to DIE. */
9595
9596 if (part_die->name == NULL && part_die->has_specification)
9597 {
9598 struct partial_die_info *spec_die;
9599
9600 spec_die = find_partial_die (part_die->spec_offset, cu);
9601
9602 fixup_partial_die (spec_die, cu);
9603
9604 if (spec_die->name)
9605 {
9606 part_die->name = spec_die->name;
9607
9608 /* Copy DW_AT_external attribute if it is set. */
9609 if (spec_die->is_external)
9610 part_die->is_external = spec_die->is_external;
9611 }
9612 }
9613
9614 /* Set default names for some unnamed DIEs. */
9615
9616 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
9617 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
9618
9619 /* If there is no parent die to provide a namespace, and there are
9620 children, see if we can determine the namespace from their linkage
9621 name.
9622 NOTE: We need to do this even if cu->has_namespace_info != 0.
9623 gcc-4.5 -gdwarf-4 can drop the enclosing namespace. */
9624 if (cu->language == language_cplus
9625 && dwarf2_per_objfile->types.asection != NULL
9626 && part_die->die_parent == NULL
9627 && part_die->has_children
9628 && (part_die->tag == DW_TAG_class_type
9629 || part_die->tag == DW_TAG_structure_type
9630 || part_die->tag == DW_TAG_union_type))
9631 guess_partial_die_structure_name (part_die, cu);
9632
9633 /* GCC might emit a nameless struct or union that has a linkage
9634 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
9635 if (part_die->name == NULL
9636 && (part_die->tag == DW_TAG_structure_type
9637 || part_die->tag == DW_TAG_union_type
9638 || part_die->tag == DW_TAG_class_type)
9639 && part_die->linkage_name != NULL)
9640 {
9641 char *demangled;
9642
9643 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
9644 if (demangled)
9645 {
9646 part_die->name = obsavestring (demangled, strlen (demangled),
9647 &cu->objfile->objfile_obstack);
9648 xfree (demangled);
9649 }
9650 }
9651
9652 part_die->fixup_called = 1;
9653 }
9654
9655 /* Read an attribute value described by an attribute form. */
9656
9657 static gdb_byte *
9658 read_attribute_value (struct attribute *attr, unsigned form,
9659 bfd *abfd, gdb_byte *info_ptr,
9660 struct dwarf2_cu *cu)
9661 {
9662 struct comp_unit_head *cu_header = &cu->header;
9663 unsigned int bytes_read;
9664 struct dwarf_block *blk;
9665
9666 attr->form = form;
9667 switch (form)
9668 {
9669 case DW_FORM_ref_addr:
9670 if (cu->header.version == 2)
9671 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9672 else
9673 DW_ADDR (attr) = read_offset (abfd, info_ptr,
9674 &cu->header, &bytes_read);
9675 info_ptr += bytes_read;
9676 break;
9677 case DW_FORM_addr:
9678 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
9679 info_ptr += bytes_read;
9680 break;
9681 case DW_FORM_block2:
9682 blk = dwarf_alloc_block (cu);
9683 blk->size = read_2_bytes (abfd, info_ptr);
9684 info_ptr += 2;
9685 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9686 info_ptr += blk->size;
9687 DW_BLOCK (attr) = blk;
9688 break;
9689 case DW_FORM_block4:
9690 blk = dwarf_alloc_block (cu);
9691 blk->size = read_4_bytes (abfd, info_ptr);
9692 info_ptr += 4;
9693 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9694 info_ptr += blk->size;
9695 DW_BLOCK (attr) = blk;
9696 break;
9697 case DW_FORM_data2:
9698 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
9699 info_ptr += 2;
9700 break;
9701 case DW_FORM_data4:
9702 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
9703 info_ptr += 4;
9704 break;
9705 case DW_FORM_data8:
9706 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
9707 info_ptr += 8;
9708 break;
9709 case DW_FORM_sec_offset:
9710 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
9711 info_ptr += bytes_read;
9712 break;
9713 case DW_FORM_string:
9714 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
9715 DW_STRING_IS_CANONICAL (attr) = 0;
9716 info_ptr += bytes_read;
9717 break;
9718 case DW_FORM_strp:
9719 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
9720 &bytes_read);
9721 DW_STRING_IS_CANONICAL (attr) = 0;
9722 info_ptr += bytes_read;
9723 break;
9724 case DW_FORM_exprloc:
9725 case DW_FORM_block:
9726 blk = dwarf_alloc_block (cu);
9727 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9728 info_ptr += bytes_read;
9729 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9730 info_ptr += blk->size;
9731 DW_BLOCK (attr) = blk;
9732 break;
9733 case DW_FORM_block1:
9734 blk = dwarf_alloc_block (cu);
9735 blk->size = read_1_byte (abfd, info_ptr);
9736 info_ptr += 1;
9737 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
9738 info_ptr += blk->size;
9739 DW_BLOCK (attr) = blk;
9740 break;
9741 case DW_FORM_data1:
9742 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9743 info_ptr += 1;
9744 break;
9745 case DW_FORM_flag:
9746 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
9747 info_ptr += 1;
9748 break;
9749 case DW_FORM_flag_present:
9750 DW_UNSND (attr) = 1;
9751 break;
9752 case DW_FORM_sdata:
9753 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
9754 info_ptr += bytes_read;
9755 break;
9756 case DW_FORM_udata:
9757 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9758 info_ptr += bytes_read;
9759 break;
9760 case DW_FORM_ref1:
9761 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
9762 info_ptr += 1;
9763 break;
9764 case DW_FORM_ref2:
9765 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
9766 info_ptr += 2;
9767 break;
9768 case DW_FORM_ref4:
9769 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
9770 info_ptr += 4;
9771 break;
9772 case DW_FORM_ref8:
9773 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
9774 info_ptr += 8;
9775 break;
9776 case DW_FORM_ref_sig8:
9777 /* Convert the signature to something we can record in DW_UNSND
9778 for later lookup.
9779 NOTE: This is NULL if the type wasn't found. */
9780 DW_SIGNATURED_TYPE (attr) =
9781 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
9782 info_ptr += 8;
9783 break;
9784 case DW_FORM_ref_udata:
9785 DW_ADDR (attr) = (cu->header.offset
9786 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
9787 info_ptr += bytes_read;
9788 break;
9789 case DW_FORM_indirect:
9790 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9791 info_ptr += bytes_read;
9792 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
9793 break;
9794 default:
9795 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
9796 dwarf_form_name (form),
9797 bfd_get_filename (abfd));
9798 }
9799
9800 /* We have seen instances where the compiler tried to emit a byte
9801 size attribute of -1 which ended up being encoded as an unsigned
9802 0xffffffff. Although 0xffffffff is technically a valid size value,
9803 an object of this size seems pretty unlikely so we can relatively
9804 safely treat these cases as if the size attribute was invalid and
9805 treat them as zero by default. */
9806 if (attr->name == DW_AT_byte_size
9807 && form == DW_FORM_data4
9808 && DW_UNSND (attr) >= 0xffffffff)
9809 {
9810 complaint
9811 (&symfile_complaints,
9812 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
9813 hex_string (DW_UNSND (attr)));
9814 DW_UNSND (attr) = 0;
9815 }
9816
9817 return info_ptr;
9818 }
9819
9820 /* Read an attribute described by an abbreviated attribute. */
9821
9822 static gdb_byte *
9823 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
9824 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
9825 {
9826 attr->name = abbrev->name;
9827 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
9828 }
9829
9830 /* Read dwarf information from a buffer. */
9831
9832 static unsigned int
9833 read_1_byte (bfd *abfd, gdb_byte *buf)
9834 {
9835 return bfd_get_8 (abfd, buf);
9836 }
9837
9838 static int
9839 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
9840 {
9841 return bfd_get_signed_8 (abfd, buf);
9842 }
9843
9844 static unsigned int
9845 read_2_bytes (bfd *abfd, gdb_byte *buf)
9846 {
9847 return bfd_get_16 (abfd, buf);
9848 }
9849
9850 static int
9851 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
9852 {
9853 return bfd_get_signed_16 (abfd, buf);
9854 }
9855
9856 static unsigned int
9857 read_4_bytes (bfd *abfd, gdb_byte *buf)
9858 {
9859 return bfd_get_32 (abfd, buf);
9860 }
9861
9862 static int
9863 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
9864 {
9865 return bfd_get_signed_32 (abfd, buf);
9866 }
9867
9868 static ULONGEST
9869 read_8_bytes (bfd *abfd, gdb_byte *buf)
9870 {
9871 return bfd_get_64 (abfd, buf);
9872 }
9873
9874 static CORE_ADDR
9875 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
9876 unsigned int *bytes_read)
9877 {
9878 struct comp_unit_head *cu_header = &cu->header;
9879 CORE_ADDR retval = 0;
9880
9881 if (cu_header->signed_addr_p)
9882 {
9883 switch (cu_header->addr_size)
9884 {
9885 case 2:
9886 retval = bfd_get_signed_16 (abfd, buf);
9887 break;
9888 case 4:
9889 retval = bfd_get_signed_32 (abfd, buf);
9890 break;
9891 case 8:
9892 retval = bfd_get_signed_64 (abfd, buf);
9893 break;
9894 default:
9895 internal_error (__FILE__, __LINE__,
9896 _("read_address: bad switch, signed [in module %s]"),
9897 bfd_get_filename (abfd));
9898 }
9899 }
9900 else
9901 {
9902 switch (cu_header->addr_size)
9903 {
9904 case 2:
9905 retval = bfd_get_16 (abfd, buf);
9906 break;
9907 case 4:
9908 retval = bfd_get_32 (abfd, buf);
9909 break;
9910 case 8:
9911 retval = bfd_get_64 (abfd, buf);
9912 break;
9913 default:
9914 internal_error (__FILE__, __LINE__,
9915 _("read_address: bad switch, "
9916 "unsigned [in module %s]"),
9917 bfd_get_filename (abfd));
9918 }
9919 }
9920
9921 *bytes_read = cu_header->addr_size;
9922 return retval;
9923 }
9924
9925 /* Read the initial length from a section. The (draft) DWARF 3
9926 specification allows the initial length to take up either 4 bytes
9927 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
9928 bytes describe the length and all offsets will be 8 bytes in length
9929 instead of 4.
9930
9931 An older, non-standard 64-bit format is also handled by this
9932 function. The older format in question stores the initial length
9933 as an 8-byte quantity without an escape value. Lengths greater
9934 than 2^32 aren't very common which means that the initial 4 bytes
9935 is almost always zero. Since a length value of zero doesn't make
9936 sense for the 32-bit format, this initial zero can be considered to
9937 be an escape value which indicates the presence of the older 64-bit
9938 format. As written, the code can't detect (old format) lengths
9939 greater than 4GB. If it becomes necessary to handle lengths
9940 somewhat larger than 4GB, we could allow other small values (such
9941 as the non-sensical values of 1, 2, and 3) to also be used as
9942 escape values indicating the presence of the old format.
9943
9944 The value returned via bytes_read should be used to increment the
9945 relevant pointer after calling read_initial_length().
9946
9947 [ Note: read_initial_length() and read_offset() are based on the
9948 document entitled "DWARF Debugging Information Format", revision
9949 3, draft 8, dated November 19, 2001. This document was obtained
9950 from:
9951
9952 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
9953
9954 This document is only a draft and is subject to change. (So beware.)
9955
9956 Details regarding the older, non-standard 64-bit format were
9957 determined empirically by examining 64-bit ELF files produced by
9958 the SGI toolchain on an IRIX 6.5 machine.
9959
9960 - Kevin, July 16, 2002
9961 ] */
9962
9963 static LONGEST
9964 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
9965 {
9966 LONGEST length = bfd_get_32 (abfd, buf);
9967
9968 if (length == 0xffffffff)
9969 {
9970 length = bfd_get_64 (abfd, buf + 4);
9971 *bytes_read = 12;
9972 }
9973 else if (length == 0)
9974 {
9975 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
9976 length = bfd_get_64 (abfd, buf);
9977 *bytes_read = 8;
9978 }
9979 else
9980 {
9981 *bytes_read = 4;
9982 }
9983
9984 return length;
9985 }
9986
9987 /* Cover function for read_initial_length.
9988 Returns the length of the object at BUF, and stores the size of the
9989 initial length in *BYTES_READ and stores the size that offsets will be in
9990 *OFFSET_SIZE.
9991 If the initial length size is not equivalent to that specified in
9992 CU_HEADER then issue a complaint.
9993 This is useful when reading non-comp-unit headers. */
9994
9995 static LONGEST
9996 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
9997 const struct comp_unit_head *cu_header,
9998 unsigned int *bytes_read,
9999 unsigned int *offset_size)
10000 {
10001 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10002
10003 gdb_assert (cu_header->initial_length_size == 4
10004 || cu_header->initial_length_size == 8
10005 || cu_header->initial_length_size == 12);
10006
10007 if (cu_header->initial_length_size != *bytes_read)
10008 complaint (&symfile_complaints,
10009 _("intermixed 32-bit and 64-bit DWARF sections"));
10010
10011 *offset_size = (*bytes_read == 4) ? 4 : 8;
10012 return length;
10013 }
10014
10015 /* Read an offset from the data stream. The size of the offset is
10016 given by cu_header->offset_size. */
10017
10018 static LONGEST
10019 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10020 unsigned int *bytes_read)
10021 {
10022 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10023
10024 *bytes_read = cu_header->offset_size;
10025 return offset;
10026 }
10027
10028 /* Read an offset from the data stream. */
10029
10030 static LONGEST
10031 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10032 {
10033 LONGEST retval = 0;
10034
10035 switch (offset_size)
10036 {
10037 case 4:
10038 retval = bfd_get_32 (abfd, buf);
10039 break;
10040 case 8:
10041 retval = bfd_get_64 (abfd, buf);
10042 break;
10043 default:
10044 internal_error (__FILE__, __LINE__,
10045 _("read_offset_1: bad switch [in module %s]"),
10046 bfd_get_filename (abfd));
10047 }
10048
10049 return retval;
10050 }
10051
10052 static gdb_byte *
10053 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10054 {
10055 /* If the size of a host char is 8 bits, we can return a pointer
10056 to the buffer, otherwise we have to copy the data to a buffer
10057 allocated on the temporary obstack. */
10058 gdb_assert (HOST_CHAR_BIT == 8);
10059 return buf;
10060 }
10061
10062 static char *
10063 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10064 {
10065 /* If the size of a host char is 8 bits, we can return a pointer
10066 to the string, otherwise we have to copy the string to a buffer
10067 allocated on the temporary obstack. */
10068 gdb_assert (HOST_CHAR_BIT == 8);
10069 if (*buf == '\0')
10070 {
10071 *bytes_read_ptr = 1;
10072 return NULL;
10073 }
10074 *bytes_read_ptr = strlen ((char *) buf) + 1;
10075 return (char *) buf;
10076 }
10077
10078 static char *
10079 read_indirect_string (bfd *abfd, gdb_byte *buf,
10080 const struct comp_unit_head *cu_header,
10081 unsigned int *bytes_read_ptr)
10082 {
10083 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10084
10085 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10086 if (dwarf2_per_objfile->str.buffer == NULL)
10087 {
10088 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10089 bfd_get_filename (abfd));
10090 return NULL;
10091 }
10092 if (str_offset >= dwarf2_per_objfile->str.size)
10093 {
10094 error (_("DW_FORM_strp pointing outside of "
10095 ".debug_str section [in module %s]"),
10096 bfd_get_filename (abfd));
10097 return NULL;
10098 }
10099 gdb_assert (HOST_CHAR_BIT == 8);
10100 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10101 return NULL;
10102 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10103 }
10104
10105 static unsigned long
10106 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10107 {
10108 unsigned long result;
10109 unsigned int num_read;
10110 int i, shift;
10111 unsigned char byte;
10112
10113 result = 0;
10114 shift = 0;
10115 num_read = 0;
10116 i = 0;
10117 while (1)
10118 {
10119 byte = bfd_get_8 (abfd, buf);
10120 buf++;
10121 num_read++;
10122 result |= ((unsigned long)(byte & 127) << shift);
10123 if ((byte & 128) == 0)
10124 {
10125 break;
10126 }
10127 shift += 7;
10128 }
10129 *bytes_read_ptr = num_read;
10130 return result;
10131 }
10132
10133 static long
10134 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10135 {
10136 long result;
10137 int i, shift, num_read;
10138 unsigned char byte;
10139
10140 result = 0;
10141 shift = 0;
10142 num_read = 0;
10143 i = 0;
10144 while (1)
10145 {
10146 byte = bfd_get_8 (abfd, buf);
10147 buf++;
10148 num_read++;
10149 result |= ((long)(byte & 127) << shift);
10150 shift += 7;
10151 if ((byte & 128) == 0)
10152 {
10153 break;
10154 }
10155 }
10156 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10157 result |= -(((long)1) << shift);
10158 *bytes_read_ptr = num_read;
10159 return result;
10160 }
10161
10162 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10163
10164 static gdb_byte *
10165 skip_leb128 (bfd *abfd, gdb_byte *buf)
10166 {
10167 int byte;
10168
10169 while (1)
10170 {
10171 byte = bfd_get_8 (abfd, buf);
10172 buf++;
10173 if ((byte & 128) == 0)
10174 return buf;
10175 }
10176 }
10177
10178 static void
10179 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10180 {
10181 switch (lang)
10182 {
10183 case DW_LANG_C89:
10184 case DW_LANG_C99:
10185 case DW_LANG_C:
10186 cu->language = language_c;
10187 break;
10188 case DW_LANG_C_plus_plus:
10189 cu->language = language_cplus;
10190 break;
10191 case DW_LANG_D:
10192 cu->language = language_d;
10193 break;
10194 case DW_LANG_Fortran77:
10195 case DW_LANG_Fortran90:
10196 case DW_LANG_Fortran95:
10197 cu->language = language_fortran;
10198 break;
10199 case DW_LANG_Mips_Assembler:
10200 cu->language = language_asm;
10201 break;
10202 case DW_LANG_Java:
10203 cu->language = language_java;
10204 break;
10205 case DW_LANG_Ada83:
10206 case DW_LANG_Ada95:
10207 cu->language = language_ada;
10208 break;
10209 case DW_LANG_Modula2:
10210 cu->language = language_m2;
10211 break;
10212 case DW_LANG_Pascal83:
10213 cu->language = language_pascal;
10214 break;
10215 case DW_LANG_ObjC:
10216 cu->language = language_objc;
10217 break;
10218 case DW_LANG_Cobol74:
10219 case DW_LANG_Cobol85:
10220 default:
10221 cu->language = language_minimal;
10222 break;
10223 }
10224 cu->language_defn = language_def (cu->language);
10225 }
10226
10227 /* Return the named attribute or NULL if not there. */
10228
10229 static struct attribute *
10230 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10231 {
10232 unsigned int i;
10233 struct attribute *spec = NULL;
10234
10235 for (i = 0; i < die->num_attrs; ++i)
10236 {
10237 if (die->attrs[i].name == name)
10238 return &die->attrs[i];
10239 if (die->attrs[i].name == DW_AT_specification
10240 || die->attrs[i].name == DW_AT_abstract_origin)
10241 spec = &die->attrs[i];
10242 }
10243
10244 if (spec)
10245 {
10246 die = follow_die_ref (die, spec, &cu);
10247 return dwarf2_attr (die, name, cu);
10248 }
10249
10250 return NULL;
10251 }
10252
10253 /* Return the named attribute or NULL if not there,
10254 but do not follow DW_AT_specification, etc.
10255 This is for use in contexts where we're reading .debug_types dies.
10256 Following DW_AT_specification, DW_AT_abstract_origin will take us
10257 back up the chain, and we want to go down. */
10258
10259 static struct attribute *
10260 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10261 struct dwarf2_cu *cu)
10262 {
10263 unsigned int i;
10264
10265 for (i = 0; i < die->num_attrs; ++i)
10266 if (die->attrs[i].name == name)
10267 return &die->attrs[i];
10268
10269 return NULL;
10270 }
10271
10272 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10273 and holds a non-zero value. This function should only be used for
10274 DW_FORM_flag or DW_FORM_flag_present attributes. */
10275
10276 static int
10277 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10278 {
10279 struct attribute *attr = dwarf2_attr (die, name, cu);
10280
10281 return (attr && DW_UNSND (attr));
10282 }
10283
10284 static int
10285 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10286 {
10287 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10288 which value is non-zero. However, we have to be careful with
10289 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10290 (via dwarf2_flag_true_p) follows this attribute. So we may
10291 end up accidently finding a declaration attribute that belongs
10292 to a different DIE referenced by the specification attribute,
10293 even though the given DIE does not have a declaration attribute. */
10294 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10295 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10296 }
10297
10298 /* Return the die giving the specification for DIE, if there is
10299 one. *SPEC_CU is the CU containing DIE on input, and the CU
10300 containing the return value on output. If there is no
10301 specification, but there is an abstract origin, that is
10302 returned. */
10303
10304 static struct die_info *
10305 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10306 {
10307 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10308 *spec_cu);
10309
10310 if (spec_attr == NULL)
10311 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10312
10313 if (spec_attr == NULL)
10314 return NULL;
10315 else
10316 return follow_die_ref (die, spec_attr, spec_cu);
10317 }
10318
10319 /* Free the line_header structure *LH, and any arrays and strings it
10320 refers to.
10321 NOTE: This is also used as a "cleanup" function. */
10322
10323 static void
10324 free_line_header (struct line_header *lh)
10325 {
10326 if (lh->standard_opcode_lengths)
10327 xfree (lh->standard_opcode_lengths);
10328
10329 /* Remember that all the lh->file_names[i].name pointers are
10330 pointers into debug_line_buffer, and don't need to be freed. */
10331 if (lh->file_names)
10332 xfree (lh->file_names);
10333
10334 /* Similarly for the include directory names. */
10335 if (lh->include_dirs)
10336 xfree (lh->include_dirs);
10337
10338 xfree (lh);
10339 }
10340
10341 /* Add an entry to LH's include directory table. */
10342
10343 static void
10344 add_include_dir (struct line_header *lh, char *include_dir)
10345 {
10346 /* Grow the array if necessary. */
10347 if (lh->include_dirs_size == 0)
10348 {
10349 lh->include_dirs_size = 1; /* for testing */
10350 lh->include_dirs = xmalloc (lh->include_dirs_size
10351 * sizeof (*lh->include_dirs));
10352 }
10353 else if (lh->num_include_dirs >= lh->include_dirs_size)
10354 {
10355 lh->include_dirs_size *= 2;
10356 lh->include_dirs = xrealloc (lh->include_dirs,
10357 (lh->include_dirs_size
10358 * sizeof (*lh->include_dirs)));
10359 }
10360
10361 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10362 }
10363
10364 /* Add an entry to LH's file name table. */
10365
10366 static void
10367 add_file_name (struct line_header *lh,
10368 char *name,
10369 unsigned int dir_index,
10370 unsigned int mod_time,
10371 unsigned int length)
10372 {
10373 struct file_entry *fe;
10374
10375 /* Grow the array if necessary. */
10376 if (lh->file_names_size == 0)
10377 {
10378 lh->file_names_size = 1; /* for testing */
10379 lh->file_names = xmalloc (lh->file_names_size
10380 * sizeof (*lh->file_names));
10381 }
10382 else if (lh->num_file_names >= lh->file_names_size)
10383 {
10384 lh->file_names_size *= 2;
10385 lh->file_names = xrealloc (lh->file_names,
10386 (lh->file_names_size
10387 * sizeof (*lh->file_names)));
10388 }
10389
10390 fe = &lh->file_names[lh->num_file_names++];
10391 fe->name = name;
10392 fe->dir_index = dir_index;
10393 fe->mod_time = mod_time;
10394 fe->length = length;
10395 fe->included_p = 0;
10396 fe->symtab = NULL;
10397 }
10398
10399 /* Read the statement program header starting at OFFSET in
10400 .debug_line, according to the endianness of ABFD. Return a pointer
10401 to a struct line_header, allocated using xmalloc.
10402
10403 NOTE: the strings in the include directory and file name tables of
10404 the returned object point into debug_line_buffer, and must not be
10405 freed. */
10406
10407 static struct line_header *
10408 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10409 struct dwarf2_cu *cu)
10410 {
10411 struct cleanup *back_to;
10412 struct line_header *lh;
10413 gdb_byte *line_ptr;
10414 unsigned int bytes_read, offset_size;
10415 int i;
10416 char *cur_dir, *cur_file;
10417
10418 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10419 if (dwarf2_per_objfile->line.buffer == NULL)
10420 {
10421 complaint (&symfile_complaints, _("missing .debug_line section"));
10422 return 0;
10423 }
10424
10425 /* Make sure that at least there's room for the total_length field.
10426 That could be 12 bytes long, but we're just going to fudge that. */
10427 if (offset + 4 >= dwarf2_per_objfile->line.size)
10428 {
10429 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10430 return 0;
10431 }
10432
10433 lh = xmalloc (sizeof (*lh));
10434 memset (lh, 0, sizeof (*lh));
10435 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10436 (void *) lh);
10437
10438 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10439
10440 /* Read in the header. */
10441 lh->total_length =
10442 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10443 &bytes_read, &offset_size);
10444 line_ptr += bytes_read;
10445 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10446 + dwarf2_per_objfile->line.size))
10447 {
10448 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10449 return 0;
10450 }
10451 lh->statement_program_end = line_ptr + lh->total_length;
10452 lh->version = read_2_bytes (abfd, line_ptr);
10453 line_ptr += 2;
10454 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10455 line_ptr += offset_size;
10456 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10457 line_ptr += 1;
10458 if (lh->version >= 4)
10459 {
10460 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10461 line_ptr += 1;
10462 }
10463 else
10464 lh->maximum_ops_per_instruction = 1;
10465
10466 if (lh->maximum_ops_per_instruction == 0)
10467 {
10468 lh->maximum_ops_per_instruction = 1;
10469 complaint (&symfile_complaints,
10470 _("invalid maximum_ops_per_instruction "
10471 "in `.debug_line' section"));
10472 }
10473
10474 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
10475 line_ptr += 1;
10476 lh->line_base = read_1_signed_byte (abfd, line_ptr);
10477 line_ptr += 1;
10478 lh->line_range = read_1_byte (abfd, line_ptr);
10479 line_ptr += 1;
10480 lh->opcode_base = read_1_byte (abfd, line_ptr);
10481 line_ptr += 1;
10482 lh->standard_opcode_lengths
10483 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
10484
10485 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
10486 for (i = 1; i < lh->opcode_base; ++i)
10487 {
10488 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
10489 line_ptr += 1;
10490 }
10491
10492 /* Read directory table. */
10493 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10494 {
10495 line_ptr += bytes_read;
10496 add_include_dir (lh, cur_dir);
10497 }
10498 line_ptr += bytes_read;
10499
10500 /* Read file name table. */
10501 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
10502 {
10503 unsigned int dir_index, mod_time, length;
10504
10505 line_ptr += bytes_read;
10506 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10507 line_ptr += bytes_read;
10508 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10509 line_ptr += bytes_read;
10510 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10511 line_ptr += bytes_read;
10512
10513 add_file_name (lh, cur_file, dir_index, mod_time, length);
10514 }
10515 line_ptr += bytes_read;
10516 lh->statement_program_start = line_ptr;
10517
10518 if (line_ptr > (dwarf2_per_objfile->line.buffer
10519 + dwarf2_per_objfile->line.size))
10520 complaint (&symfile_complaints,
10521 _("line number info header doesn't "
10522 "fit in `.debug_line' section"));
10523
10524 discard_cleanups (back_to);
10525 return lh;
10526 }
10527
10528 /* This function exists to work around a bug in certain compilers
10529 (particularly GCC 2.95), in which the first line number marker of a
10530 function does not show up until after the prologue, right before
10531 the second line number marker. This function shifts ADDRESS down
10532 to the beginning of the function if necessary, and is called on
10533 addresses passed to record_line. */
10534
10535 static CORE_ADDR
10536 check_cu_functions (CORE_ADDR address, struct dwarf2_cu *cu)
10537 {
10538 struct function_range *fn;
10539
10540 /* Find the function_range containing address. */
10541 if (!cu->first_fn)
10542 return address;
10543
10544 if (!cu->cached_fn)
10545 cu->cached_fn = cu->first_fn;
10546
10547 fn = cu->cached_fn;
10548 while (fn)
10549 if (fn->lowpc <= address && fn->highpc > address)
10550 goto found;
10551 else
10552 fn = fn->next;
10553
10554 fn = cu->first_fn;
10555 while (fn && fn != cu->cached_fn)
10556 if (fn->lowpc <= address && fn->highpc > address)
10557 goto found;
10558 else
10559 fn = fn->next;
10560
10561 return address;
10562
10563 found:
10564 if (fn->seen_line)
10565 return address;
10566 if (address != fn->lowpc)
10567 complaint (&symfile_complaints,
10568 _("misplaced first line number at 0x%lx for '%s'"),
10569 (unsigned long) address, fn->name);
10570 fn->seen_line = 1;
10571 return fn->lowpc;
10572 }
10573
10574 /* Subroutine of dwarf_decode_lines to simplify it.
10575 Return the file name of the psymtab for included file FILE_INDEX
10576 in line header LH of PST.
10577 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10578 If space for the result is malloc'd, it will be freed by a cleanup.
10579 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
10580
10581 static char *
10582 psymtab_include_file_name (const struct line_header *lh, int file_index,
10583 const struct partial_symtab *pst,
10584 const char *comp_dir)
10585 {
10586 const struct file_entry fe = lh->file_names [file_index];
10587 char *include_name = fe.name;
10588 char *include_name_to_compare = include_name;
10589 char *dir_name = NULL;
10590 const char *pst_filename;
10591 char *copied_name = NULL;
10592 int file_is_pst;
10593
10594 if (fe.dir_index)
10595 dir_name = lh->include_dirs[fe.dir_index - 1];
10596
10597 if (!IS_ABSOLUTE_PATH (include_name)
10598 && (dir_name != NULL || comp_dir != NULL))
10599 {
10600 /* Avoid creating a duplicate psymtab for PST.
10601 We do this by comparing INCLUDE_NAME and PST_FILENAME.
10602 Before we do the comparison, however, we need to account
10603 for DIR_NAME and COMP_DIR.
10604 First prepend dir_name (if non-NULL). If we still don't
10605 have an absolute path prepend comp_dir (if non-NULL).
10606 However, the directory we record in the include-file's
10607 psymtab does not contain COMP_DIR (to match the
10608 corresponding symtab(s)).
10609
10610 Example:
10611
10612 bash$ cd /tmp
10613 bash$ gcc -g ./hello.c
10614 include_name = "hello.c"
10615 dir_name = "."
10616 DW_AT_comp_dir = comp_dir = "/tmp"
10617 DW_AT_name = "./hello.c" */
10618
10619 if (dir_name != NULL)
10620 {
10621 include_name = concat (dir_name, SLASH_STRING,
10622 include_name, (char *)NULL);
10623 include_name_to_compare = include_name;
10624 make_cleanup (xfree, include_name);
10625 }
10626 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
10627 {
10628 include_name_to_compare = concat (comp_dir, SLASH_STRING,
10629 include_name, (char *)NULL);
10630 }
10631 }
10632
10633 pst_filename = pst->filename;
10634 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
10635 {
10636 copied_name = concat (pst->dirname, SLASH_STRING,
10637 pst_filename, (char *)NULL);
10638 pst_filename = copied_name;
10639 }
10640
10641 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
10642
10643 if (include_name_to_compare != include_name)
10644 xfree (include_name_to_compare);
10645 if (copied_name != NULL)
10646 xfree (copied_name);
10647
10648 if (file_is_pst)
10649 return NULL;
10650 return include_name;
10651 }
10652
10653 /* Ignore this record_line request. */
10654
10655 static void
10656 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
10657 {
10658 return;
10659 }
10660
10661 /* Decode the Line Number Program (LNP) for the given line_header
10662 structure and CU. The actual information extracted and the type
10663 of structures created from the LNP depends on the value of PST.
10664
10665 1. If PST is NULL, then this procedure uses the data from the program
10666 to create all necessary symbol tables, and their linetables.
10667
10668 2. If PST is not NULL, this procedure reads the program to determine
10669 the list of files included by the unit represented by PST, and
10670 builds all the associated partial symbol tables.
10671
10672 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
10673 It is used for relative paths in the line table.
10674 NOTE: When processing partial symtabs (pst != NULL),
10675 comp_dir == pst->dirname.
10676
10677 NOTE: It is important that psymtabs have the same file name (via strcmp)
10678 as the corresponding symtab. Since COMP_DIR is not used in the name of the
10679 symtab we don't use it in the name of the psymtabs we create.
10680 E.g. expand_line_sal requires this when finding psymtabs to expand.
10681 A good testcase for this is mb-inline.exp. */
10682
10683 static void
10684 dwarf_decode_lines (struct line_header *lh, const char *comp_dir, bfd *abfd,
10685 struct dwarf2_cu *cu, struct partial_symtab *pst)
10686 {
10687 gdb_byte *line_ptr, *extended_end;
10688 gdb_byte *line_end;
10689 unsigned int bytes_read, extended_len;
10690 unsigned char op_code, extended_op, adj_opcode;
10691 CORE_ADDR baseaddr;
10692 struct objfile *objfile = cu->objfile;
10693 struct gdbarch *gdbarch = get_objfile_arch (objfile);
10694 const int decode_for_pst_p = (pst != NULL);
10695 struct subfile *last_subfile = NULL, *first_subfile = current_subfile;
10696 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
10697 = record_line;
10698
10699 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
10700
10701 line_ptr = lh->statement_program_start;
10702 line_end = lh->statement_program_end;
10703
10704 /* Read the statement sequences until there's nothing left. */
10705 while (line_ptr < line_end)
10706 {
10707 /* state machine registers */
10708 CORE_ADDR address = 0;
10709 unsigned int file = 1;
10710 unsigned int line = 1;
10711 unsigned int column = 0;
10712 int is_stmt = lh->default_is_stmt;
10713 int basic_block = 0;
10714 int end_sequence = 0;
10715 CORE_ADDR addr;
10716 unsigned char op_index = 0;
10717
10718 if (!decode_for_pst_p && lh->num_file_names >= file)
10719 {
10720 /* Start a subfile for the current file of the state machine. */
10721 /* lh->include_dirs and lh->file_names are 0-based, but the
10722 directory and file name numbers in the statement program
10723 are 1-based. */
10724 struct file_entry *fe = &lh->file_names[file - 1];
10725 char *dir = NULL;
10726
10727 if (fe->dir_index)
10728 dir = lh->include_dirs[fe->dir_index - 1];
10729
10730 dwarf2_start_subfile (fe->name, dir, comp_dir);
10731 }
10732
10733 /* Decode the table. */
10734 while (!end_sequence)
10735 {
10736 op_code = read_1_byte (abfd, line_ptr);
10737 line_ptr += 1;
10738 if (line_ptr > line_end)
10739 {
10740 dwarf2_debug_line_missing_end_sequence_complaint ();
10741 break;
10742 }
10743
10744 if (op_code >= lh->opcode_base)
10745 {
10746 /* Special operand. */
10747 adj_opcode = op_code - lh->opcode_base;
10748 address += (((op_index + (adj_opcode / lh->line_range))
10749 / lh->maximum_ops_per_instruction)
10750 * lh->minimum_instruction_length);
10751 op_index = ((op_index + (adj_opcode / lh->line_range))
10752 % lh->maximum_ops_per_instruction);
10753 line += lh->line_base + (adj_opcode % lh->line_range);
10754 if (lh->num_file_names < file || file == 0)
10755 dwarf2_debug_line_missing_file_complaint ();
10756 /* For now we ignore lines not starting on an
10757 instruction boundary. */
10758 else if (op_index == 0)
10759 {
10760 lh->file_names[file - 1].included_p = 1;
10761 if (!decode_for_pst_p && is_stmt)
10762 {
10763 if (last_subfile != current_subfile)
10764 {
10765 addr = gdbarch_addr_bits_remove (gdbarch, address);
10766 if (last_subfile)
10767 (*p_record_line) (last_subfile, 0, addr);
10768 last_subfile = current_subfile;
10769 }
10770 /* Append row to matrix using current values. */
10771 addr = check_cu_functions (address, cu);
10772 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10773 (*p_record_line) (current_subfile, line, addr);
10774 }
10775 }
10776 basic_block = 0;
10777 }
10778 else switch (op_code)
10779 {
10780 case DW_LNS_extended_op:
10781 extended_len = read_unsigned_leb128 (abfd, line_ptr,
10782 &bytes_read);
10783 line_ptr += bytes_read;
10784 extended_end = line_ptr + extended_len;
10785 extended_op = read_1_byte (abfd, line_ptr);
10786 line_ptr += 1;
10787 switch (extended_op)
10788 {
10789 case DW_LNE_end_sequence:
10790 p_record_line = record_line;
10791 end_sequence = 1;
10792 break;
10793 case DW_LNE_set_address:
10794 address = read_address (abfd, line_ptr, cu, &bytes_read);
10795
10796 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
10797 {
10798 /* This line table is for a function which has been
10799 GCd by the linker. Ignore it. PR gdb/12528 */
10800
10801 long line_offset
10802 = line_ptr - dwarf2_per_objfile->line.buffer;
10803
10804 complaint (&symfile_complaints,
10805 _(".debug_line address at offset 0x%lx is 0 "
10806 "[in module %s]"),
10807 line_offset, cu->objfile->name);
10808 p_record_line = noop_record_line;
10809 }
10810
10811 op_index = 0;
10812 line_ptr += bytes_read;
10813 address += baseaddr;
10814 break;
10815 case DW_LNE_define_file:
10816 {
10817 char *cur_file;
10818 unsigned int dir_index, mod_time, length;
10819
10820 cur_file = read_direct_string (abfd, line_ptr,
10821 &bytes_read);
10822 line_ptr += bytes_read;
10823 dir_index =
10824 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10825 line_ptr += bytes_read;
10826 mod_time =
10827 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10828 line_ptr += bytes_read;
10829 length =
10830 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10831 line_ptr += bytes_read;
10832 add_file_name (lh, cur_file, dir_index, mod_time, length);
10833 }
10834 break;
10835 case DW_LNE_set_discriminator:
10836 /* The discriminator is not interesting to the debugger;
10837 just ignore it. */
10838 line_ptr = extended_end;
10839 break;
10840 default:
10841 complaint (&symfile_complaints,
10842 _("mangled .debug_line section"));
10843 return;
10844 }
10845 /* Make sure that we parsed the extended op correctly. If e.g.
10846 we expected a different address size than the producer used,
10847 we may have read the wrong number of bytes. */
10848 if (line_ptr != extended_end)
10849 {
10850 complaint (&symfile_complaints,
10851 _("mangled .debug_line section"));
10852 return;
10853 }
10854 break;
10855 case DW_LNS_copy:
10856 if (lh->num_file_names < file || file == 0)
10857 dwarf2_debug_line_missing_file_complaint ();
10858 else
10859 {
10860 lh->file_names[file - 1].included_p = 1;
10861 if (!decode_for_pst_p && is_stmt)
10862 {
10863 if (last_subfile != current_subfile)
10864 {
10865 addr = gdbarch_addr_bits_remove (gdbarch, address);
10866 if (last_subfile)
10867 (*p_record_line) (last_subfile, 0, addr);
10868 last_subfile = current_subfile;
10869 }
10870 addr = check_cu_functions (address, cu);
10871 addr = gdbarch_addr_bits_remove (gdbarch, addr);
10872 (*p_record_line) (current_subfile, line, addr);
10873 }
10874 }
10875 basic_block = 0;
10876 break;
10877 case DW_LNS_advance_pc:
10878 {
10879 CORE_ADDR adjust
10880 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10881
10882 address += (((op_index + adjust)
10883 / lh->maximum_ops_per_instruction)
10884 * lh->minimum_instruction_length);
10885 op_index = ((op_index + adjust)
10886 % lh->maximum_ops_per_instruction);
10887 line_ptr += bytes_read;
10888 }
10889 break;
10890 case DW_LNS_advance_line:
10891 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
10892 line_ptr += bytes_read;
10893 break;
10894 case DW_LNS_set_file:
10895 {
10896 /* The arrays lh->include_dirs and lh->file_names are
10897 0-based, but the directory and file name numbers in
10898 the statement program are 1-based. */
10899 struct file_entry *fe;
10900 char *dir = NULL;
10901
10902 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10903 line_ptr += bytes_read;
10904 if (lh->num_file_names < file || file == 0)
10905 dwarf2_debug_line_missing_file_complaint ();
10906 else
10907 {
10908 fe = &lh->file_names[file - 1];
10909 if (fe->dir_index)
10910 dir = lh->include_dirs[fe->dir_index - 1];
10911 if (!decode_for_pst_p)
10912 {
10913 last_subfile = current_subfile;
10914 dwarf2_start_subfile (fe->name, dir, comp_dir);
10915 }
10916 }
10917 }
10918 break;
10919 case DW_LNS_set_column:
10920 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10921 line_ptr += bytes_read;
10922 break;
10923 case DW_LNS_negate_stmt:
10924 is_stmt = (!is_stmt);
10925 break;
10926 case DW_LNS_set_basic_block:
10927 basic_block = 1;
10928 break;
10929 /* Add to the address register of the state machine the
10930 address increment value corresponding to special opcode
10931 255. I.e., this value is scaled by the minimum
10932 instruction length since special opcode 255 would have
10933 scaled the increment. */
10934 case DW_LNS_const_add_pc:
10935 {
10936 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
10937
10938 address += (((op_index + adjust)
10939 / lh->maximum_ops_per_instruction)
10940 * lh->minimum_instruction_length);
10941 op_index = ((op_index + adjust)
10942 % lh->maximum_ops_per_instruction);
10943 }
10944 break;
10945 case DW_LNS_fixed_advance_pc:
10946 address += read_2_bytes (abfd, line_ptr);
10947 op_index = 0;
10948 line_ptr += 2;
10949 break;
10950 default:
10951 {
10952 /* Unknown standard opcode, ignore it. */
10953 int i;
10954
10955 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
10956 {
10957 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
10958 line_ptr += bytes_read;
10959 }
10960 }
10961 }
10962 }
10963 if (lh->num_file_names < file || file == 0)
10964 dwarf2_debug_line_missing_file_complaint ();
10965 else
10966 {
10967 lh->file_names[file - 1].included_p = 1;
10968 if (!decode_for_pst_p)
10969 {
10970 addr = gdbarch_addr_bits_remove (gdbarch, address);
10971 (*p_record_line) (current_subfile, 0, addr);
10972 }
10973 }
10974 }
10975
10976 if (decode_for_pst_p)
10977 {
10978 int file_index;
10979
10980 /* Now that we're done scanning the Line Header Program, we can
10981 create the psymtab of each included file. */
10982 for (file_index = 0; file_index < lh->num_file_names; file_index++)
10983 if (lh->file_names[file_index].included_p == 1)
10984 {
10985 char *include_name =
10986 psymtab_include_file_name (lh, file_index, pst, comp_dir);
10987 if (include_name != NULL)
10988 dwarf2_create_include_psymtab (include_name, pst, objfile);
10989 }
10990 }
10991 else
10992 {
10993 /* Make sure a symtab is created for every file, even files
10994 which contain only variables (i.e. no code with associated
10995 line numbers). */
10996
10997 int i;
10998 struct file_entry *fe;
10999
11000 for (i = 0; i < lh->num_file_names; i++)
11001 {
11002 char *dir = NULL;
11003
11004 fe = &lh->file_names[i];
11005 if (fe->dir_index)
11006 dir = lh->include_dirs[fe->dir_index - 1];
11007 dwarf2_start_subfile (fe->name, dir, comp_dir);
11008
11009 /* Skip the main file; we don't need it, and it must be
11010 allocated last, so that it will show up before the
11011 non-primary symtabs in the objfile's symtab list. */
11012 if (current_subfile == first_subfile)
11013 continue;
11014
11015 if (current_subfile->symtab == NULL)
11016 current_subfile->symtab = allocate_symtab (current_subfile->name,
11017 cu->objfile);
11018 fe->symtab = current_subfile->symtab;
11019 }
11020 }
11021 }
11022
11023 /* Start a subfile for DWARF. FILENAME is the name of the file and
11024 DIRNAME the name of the source directory which contains FILENAME
11025 or NULL if not known. COMP_DIR is the compilation directory for the
11026 linetable's compilation unit or NULL if not known.
11027 This routine tries to keep line numbers from identical absolute and
11028 relative file names in a common subfile.
11029
11030 Using the `list' example from the GDB testsuite, which resides in
11031 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11032 of /srcdir/list0.c yields the following debugging information for list0.c:
11033
11034 DW_AT_name: /srcdir/list0.c
11035 DW_AT_comp_dir: /compdir
11036 files.files[0].name: list0.h
11037 files.files[0].dir: /srcdir
11038 files.files[1].name: list0.c
11039 files.files[1].dir: /srcdir
11040
11041 The line number information for list0.c has to end up in a single
11042 subfile, so that `break /srcdir/list0.c:1' works as expected.
11043 start_subfile will ensure that this happens provided that we pass the
11044 concatenation of files.files[1].dir and files.files[1].name as the
11045 subfile's name. */
11046
11047 static void
11048 dwarf2_start_subfile (char *filename, const char *dirname,
11049 const char *comp_dir)
11050 {
11051 char *fullname;
11052
11053 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11054 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11055 second argument to start_subfile. To be consistent, we do the
11056 same here. In order not to lose the line information directory,
11057 we concatenate it to the filename when it makes sense.
11058 Note that the Dwarf3 standard says (speaking of filenames in line
11059 information): ``The directory index is ignored for file names
11060 that represent full path names''. Thus ignoring dirname in the
11061 `else' branch below isn't an issue. */
11062
11063 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11064 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11065 else
11066 fullname = filename;
11067
11068 start_subfile (fullname, comp_dir);
11069
11070 if (fullname != filename)
11071 xfree (fullname);
11072 }
11073
11074 static void
11075 var_decode_location (struct attribute *attr, struct symbol *sym,
11076 struct dwarf2_cu *cu)
11077 {
11078 struct objfile *objfile = cu->objfile;
11079 struct comp_unit_head *cu_header = &cu->header;
11080
11081 /* NOTE drow/2003-01-30: There used to be a comment and some special
11082 code here to turn a symbol with DW_AT_external and a
11083 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11084 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11085 with some versions of binutils) where shared libraries could have
11086 relocations against symbols in their debug information - the
11087 minimal symbol would have the right address, but the debug info
11088 would not. It's no longer necessary, because we will explicitly
11089 apply relocations when we read in the debug information now. */
11090
11091 /* A DW_AT_location attribute with no contents indicates that a
11092 variable has been optimized away. */
11093 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11094 {
11095 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11096 return;
11097 }
11098
11099 /* Handle one degenerate form of location expression specially, to
11100 preserve GDB's previous behavior when section offsets are
11101 specified. If this is just a DW_OP_addr then mark this symbol
11102 as LOC_STATIC. */
11103
11104 if (attr_form_is_block (attr)
11105 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11106 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11107 {
11108 unsigned int dummy;
11109
11110 SYMBOL_VALUE_ADDRESS (sym) =
11111 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11112 SYMBOL_CLASS (sym) = LOC_STATIC;
11113 fixup_symbol_section (sym, objfile);
11114 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11115 SYMBOL_SECTION (sym));
11116 return;
11117 }
11118
11119 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11120 expression evaluator, and use LOC_COMPUTED only when necessary
11121 (i.e. when the value of a register or memory location is
11122 referenced, or a thread-local block, etc.). Then again, it might
11123 not be worthwhile. I'm assuming that it isn't unless performance
11124 or memory numbers show me otherwise. */
11125
11126 dwarf2_symbol_mark_computed (attr, sym, cu);
11127 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11128
11129 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11130 cu->has_loclist = 1;
11131 }
11132
11133 /* Given a pointer to a DWARF information entry, figure out if we need
11134 to make a symbol table entry for it, and if so, create a new entry
11135 and return a pointer to it.
11136 If TYPE is NULL, determine symbol type from the die, otherwise
11137 used the passed type.
11138 If SPACE is not NULL, use it to hold the new symbol. If it is
11139 NULL, allocate a new symbol on the objfile's obstack. */
11140
11141 static struct symbol *
11142 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11143 struct symbol *space)
11144 {
11145 struct objfile *objfile = cu->objfile;
11146 struct symbol *sym = NULL;
11147 char *name;
11148 struct attribute *attr = NULL;
11149 struct attribute *attr2 = NULL;
11150 CORE_ADDR baseaddr;
11151 struct pending **list_to_add = NULL;
11152
11153 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11154
11155 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11156
11157 name = dwarf2_name (die, cu);
11158 if (name)
11159 {
11160 const char *linkagename;
11161 int suppress_add = 0;
11162
11163 if (space)
11164 sym = space;
11165 else
11166 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11167 OBJSTAT (objfile, n_syms++);
11168
11169 /* Cache this symbol's name and the name's demangled form (if any). */
11170 SYMBOL_SET_LANGUAGE (sym, cu->language);
11171 linkagename = dwarf2_physname (name, die, cu);
11172 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11173
11174 /* Fortran does not have mangling standard and the mangling does differ
11175 between gfortran, iFort etc. */
11176 if (cu->language == language_fortran
11177 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11178 symbol_set_demangled_name (&(sym->ginfo),
11179 (char *) dwarf2_full_name (name, die, cu),
11180 NULL);
11181
11182 /* Default assumptions.
11183 Use the passed type or decode it from the die. */
11184 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11185 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11186 if (type != NULL)
11187 SYMBOL_TYPE (sym) = type;
11188 else
11189 SYMBOL_TYPE (sym) = die_type (die, cu);
11190 attr = dwarf2_attr (die,
11191 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11192 cu);
11193 if (attr)
11194 {
11195 SYMBOL_LINE (sym) = DW_UNSND (attr);
11196 }
11197
11198 attr = dwarf2_attr (die,
11199 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11200 cu);
11201 if (attr)
11202 {
11203 int file_index = DW_UNSND (attr);
11204
11205 if (cu->line_header == NULL
11206 || file_index > cu->line_header->num_file_names)
11207 complaint (&symfile_complaints,
11208 _("file index out of range"));
11209 else if (file_index > 0)
11210 {
11211 struct file_entry *fe;
11212
11213 fe = &cu->line_header->file_names[file_index - 1];
11214 SYMBOL_SYMTAB (sym) = fe->symtab;
11215 }
11216 }
11217
11218 switch (die->tag)
11219 {
11220 case DW_TAG_label:
11221 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11222 if (attr)
11223 {
11224 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11225 }
11226 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11227 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11228 SYMBOL_CLASS (sym) = LOC_LABEL;
11229 add_symbol_to_list (sym, cu->list_in_scope);
11230 break;
11231 case DW_TAG_subprogram:
11232 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11233 finish_block. */
11234 SYMBOL_CLASS (sym) = LOC_BLOCK;
11235 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11236 if ((attr2 && (DW_UNSND (attr2) != 0))
11237 || cu->language == language_ada)
11238 {
11239 /* Subprograms marked external are stored as a global symbol.
11240 Ada subprograms, whether marked external or not, are always
11241 stored as a global symbol, because we want to be able to
11242 access them globally. For instance, we want to be able
11243 to break on a nested subprogram without having to
11244 specify the context. */
11245 list_to_add = &global_symbols;
11246 }
11247 else
11248 {
11249 list_to_add = cu->list_in_scope;
11250 }
11251 break;
11252 case DW_TAG_inlined_subroutine:
11253 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11254 finish_block. */
11255 SYMBOL_CLASS (sym) = LOC_BLOCK;
11256 SYMBOL_INLINED (sym) = 1;
11257 /* Do not add the symbol to any lists. It will be found via
11258 BLOCK_FUNCTION from the blockvector. */
11259 break;
11260 case DW_TAG_template_value_param:
11261 suppress_add = 1;
11262 /* Fall through. */
11263 case DW_TAG_constant:
11264 case DW_TAG_variable:
11265 case DW_TAG_member:
11266 /* Compilation with minimal debug info may result in
11267 variables with missing type entries. Change the
11268 misleading `void' type to something sensible. */
11269 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11270 SYMBOL_TYPE (sym)
11271 = objfile_type (objfile)->nodebug_data_symbol;
11272
11273 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11274 /* In the case of DW_TAG_member, we should only be called for
11275 static const members. */
11276 if (die->tag == DW_TAG_member)
11277 {
11278 /* dwarf2_add_field uses die_is_declaration,
11279 so we do the same. */
11280 gdb_assert (die_is_declaration (die, cu));
11281 gdb_assert (attr);
11282 }
11283 if (attr)
11284 {
11285 dwarf2_const_value (attr, sym, cu);
11286 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11287 if (!suppress_add)
11288 {
11289 if (attr2 && (DW_UNSND (attr2) != 0))
11290 list_to_add = &global_symbols;
11291 else
11292 list_to_add = cu->list_in_scope;
11293 }
11294 break;
11295 }
11296 attr = dwarf2_attr (die, DW_AT_location, cu);
11297 if (attr)
11298 {
11299 var_decode_location (attr, sym, cu);
11300 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11301 if (SYMBOL_CLASS (sym) == LOC_STATIC
11302 && SYMBOL_VALUE_ADDRESS (sym) == 0
11303 && !dwarf2_per_objfile->has_section_at_zero)
11304 {
11305 /* When a static variable is eliminated by the linker,
11306 the corresponding debug information is not stripped
11307 out, but the variable address is set to null;
11308 do not add such variables into symbol table. */
11309 }
11310 else if (attr2 && (DW_UNSND (attr2) != 0))
11311 {
11312 /* Workaround gfortran PR debug/40040 - it uses
11313 DW_AT_location for variables in -fPIC libraries which may
11314 get overriden by other libraries/executable and get
11315 a different address. Resolve it by the minimal symbol
11316 which may come from inferior's executable using copy
11317 relocation. Make this workaround only for gfortran as for
11318 other compilers GDB cannot guess the minimal symbol
11319 Fortran mangling kind. */
11320 if (cu->language == language_fortran && die->parent
11321 && die->parent->tag == DW_TAG_module
11322 && cu->producer
11323 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11324 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11325
11326 /* A variable with DW_AT_external is never static,
11327 but it may be block-scoped. */
11328 list_to_add = (cu->list_in_scope == &file_symbols
11329 ? &global_symbols : cu->list_in_scope);
11330 }
11331 else
11332 list_to_add = cu->list_in_scope;
11333 }
11334 else
11335 {
11336 /* We do not know the address of this symbol.
11337 If it is an external symbol and we have type information
11338 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11339 The address of the variable will then be determined from
11340 the minimal symbol table whenever the variable is
11341 referenced. */
11342 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11343 if (attr2 && (DW_UNSND (attr2) != 0)
11344 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11345 {
11346 /* A variable with DW_AT_external is never static, but it
11347 may be block-scoped. */
11348 list_to_add = (cu->list_in_scope == &file_symbols
11349 ? &global_symbols : cu->list_in_scope);
11350
11351 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11352 }
11353 else if (!die_is_declaration (die, cu))
11354 {
11355 /* Use the default LOC_OPTIMIZED_OUT class. */
11356 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11357 if (!suppress_add)
11358 list_to_add = cu->list_in_scope;
11359 }
11360 }
11361 break;
11362 case DW_TAG_formal_parameter:
11363 /* If we are inside a function, mark this as an argument. If
11364 not, we might be looking at an argument to an inlined function
11365 when we do not have enough information to show inlined frames;
11366 pretend it's a local variable in that case so that the user can
11367 still see it. */
11368 if (context_stack_depth > 0
11369 && context_stack[context_stack_depth - 1].name != NULL)
11370 SYMBOL_IS_ARGUMENT (sym) = 1;
11371 attr = dwarf2_attr (die, DW_AT_location, cu);
11372 if (attr)
11373 {
11374 var_decode_location (attr, sym, cu);
11375 }
11376 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11377 if (attr)
11378 {
11379 dwarf2_const_value (attr, sym, cu);
11380 }
11381 attr = dwarf2_attr (die, DW_AT_variable_parameter, cu);
11382 if (attr && DW_UNSND (attr))
11383 {
11384 struct type *ref_type;
11385
11386 ref_type = lookup_reference_type (SYMBOL_TYPE (sym));
11387 SYMBOL_TYPE (sym) = ref_type;
11388 }
11389
11390 list_to_add = cu->list_in_scope;
11391 break;
11392 case DW_TAG_unspecified_parameters:
11393 /* From varargs functions; gdb doesn't seem to have any
11394 interest in this information, so just ignore it for now.
11395 (FIXME?) */
11396 break;
11397 case DW_TAG_template_type_param:
11398 suppress_add = 1;
11399 /* Fall through. */
11400 case DW_TAG_class_type:
11401 case DW_TAG_interface_type:
11402 case DW_TAG_structure_type:
11403 case DW_TAG_union_type:
11404 case DW_TAG_set_type:
11405 case DW_TAG_enumeration_type:
11406 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11407 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11408
11409 {
11410 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11411 really ever be static objects: otherwise, if you try
11412 to, say, break of a class's method and you're in a file
11413 which doesn't mention that class, it won't work unless
11414 the check for all static symbols in lookup_symbol_aux
11415 saves you. See the OtherFileClass tests in
11416 gdb.c++/namespace.exp. */
11417
11418 if (!suppress_add)
11419 {
11420 list_to_add = (cu->list_in_scope == &file_symbols
11421 && (cu->language == language_cplus
11422 || cu->language == language_java)
11423 ? &global_symbols : cu->list_in_scope);
11424
11425 /* The semantics of C++ state that "struct foo {
11426 ... }" also defines a typedef for "foo". A Java
11427 class declaration also defines a typedef for the
11428 class. */
11429 if (cu->language == language_cplus
11430 || cu->language == language_java
11431 || cu->language == language_ada)
11432 {
11433 /* The symbol's name is already allocated along
11434 with this objfile, so we don't need to
11435 duplicate it for the type. */
11436 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11437 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11438 }
11439 }
11440 }
11441 break;
11442 case DW_TAG_typedef:
11443 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11444 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11445 list_to_add = cu->list_in_scope;
11446 break;
11447 case DW_TAG_base_type:
11448 case DW_TAG_subrange_type:
11449 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11450 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11451 list_to_add = cu->list_in_scope;
11452 break;
11453 case DW_TAG_enumerator:
11454 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11455 if (attr)
11456 {
11457 dwarf2_const_value (attr, sym, cu);
11458 }
11459 {
11460 /* NOTE: carlton/2003-11-10: See comment above in the
11461 DW_TAG_class_type, etc. block. */
11462
11463 list_to_add = (cu->list_in_scope == &file_symbols
11464 && (cu->language == language_cplus
11465 || cu->language == language_java)
11466 ? &global_symbols : cu->list_in_scope);
11467 }
11468 break;
11469 case DW_TAG_namespace:
11470 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11471 list_to_add = &global_symbols;
11472 break;
11473 default:
11474 /* Not a tag we recognize. Hopefully we aren't processing
11475 trash data, but since we must specifically ignore things
11476 we don't recognize, there is nothing else we should do at
11477 this point. */
11478 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11479 dwarf_tag_name (die->tag));
11480 break;
11481 }
11482
11483 if (suppress_add)
11484 {
11485 sym->hash_next = objfile->template_symbols;
11486 objfile->template_symbols = sym;
11487 list_to_add = NULL;
11488 }
11489
11490 if (list_to_add != NULL)
11491 add_symbol_to_list (sym, list_to_add);
11492
11493 /* For the benefit of old versions of GCC, check for anonymous
11494 namespaces based on the demangled name. */
11495 if (!processing_has_namespace_info
11496 && cu->language == language_cplus)
11497 cp_scan_for_anonymous_namespaces (sym);
11498 }
11499 return (sym);
11500 }
11501
11502 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11503
11504 static struct symbol *
11505 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
11506 {
11507 return new_symbol_full (die, type, cu, NULL);
11508 }
11509
11510 /* Given an attr with a DW_FORM_dataN value in host byte order,
11511 zero-extend it as appropriate for the symbol's type. The DWARF
11512 standard (v4) is not entirely clear about the meaning of using
11513 DW_FORM_dataN for a constant with a signed type, where the type is
11514 wider than the data. The conclusion of a discussion on the DWARF
11515 list was that this is unspecified. We choose to always zero-extend
11516 because that is the interpretation long in use by GCC. */
11517
11518 static gdb_byte *
11519 dwarf2_const_value_data (struct attribute *attr, struct type *type,
11520 const char *name, struct obstack *obstack,
11521 struct dwarf2_cu *cu, long *value, int bits)
11522 {
11523 struct objfile *objfile = cu->objfile;
11524 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
11525 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
11526 LONGEST l = DW_UNSND (attr);
11527
11528 if (bits < sizeof (*value) * 8)
11529 {
11530 l &= ((LONGEST) 1 << bits) - 1;
11531 *value = l;
11532 }
11533 else if (bits == sizeof (*value) * 8)
11534 *value = l;
11535 else
11536 {
11537 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
11538 store_unsigned_integer (bytes, bits / 8, byte_order, l);
11539 return bytes;
11540 }
11541
11542 return NULL;
11543 }
11544
11545 /* Read a constant value from an attribute. Either set *VALUE, or if
11546 the value does not fit in *VALUE, set *BYTES - either already
11547 allocated on the objfile obstack, or newly allocated on OBSTACK,
11548 or, set *BATON, if we translated the constant to a location
11549 expression. */
11550
11551 static void
11552 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
11553 const char *name, struct obstack *obstack,
11554 struct dwarf2_cu *cu,
11555 long *value, gdb_byte **bytes,
11556 struct dwarf2_locexpr_baton **baton)
11557 {
11558 struct objfile *objfile = cu->objfile;
11559 struct comp_unit_head *cu_header = &cu->header;
11560 struct dwarf_block *blk;
11561 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
11562 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
11563
11564 *value = 0;
11565 *bytes = NULL;
11566 *baton = NULL;
11567
11568 switch (attr->form)
11569 {
11570 case DW_FORM_addr:
11571 {
11572 gdb_byte *data;
11573
11574 if (TYPE_LENGTH (type) != cu_header->addr_size)
11575 dwarf2_const_value_length_mismatch_complaint (name,
11576 cu_header->addr_size,
11577 TYPE_LENGTH (type));
11578 /* Symbols of this form are reasonably rare, so we just
11579 piggyback on the existing location code rather than writing
11580 a new implementation of symbol_computed_ops. */
11581 *baton = obstack_alloc (&objfile->objfile_obstack,
11582 sizeof (struct dwarf2_locexpr_baton));
11583 (*baton)->per_cu = cu->per_cu;
11584 gdb_assert ((*baton)->per_cu);
11585
11586 (*baton)->size = 2 + cu_header->addr_size;
11587 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
11588 (*baton)->data = data;
11589
11590 data[0] = DW_OP_addr;
11591 store_unsigned_integer (&data[1], cu_header->addr_size,
11592 byte_order, DW_ADDR (attr));
11593 data[cu_header->addr_size + 1] = DW_OP_stack_value;
11594 }
11595 break;
11596 case DW_FORM_string:
11597 case DW_FORM_strp:
11598 /* DW_STRING is already allocated on the objfile obstack, point
11599 directly to it. */
11600 *bytes = (gdb_byte *) DW_STRING (attr);
11601 break;
11602 case DW_FORM_block1:
11603 case DW_FORM_block2:
11604 case DW_FORM_block4:
11605 case DW_FORM_block:
11606 case DW_FORM_exprloc:
11607 blk = DW_BLOCK (attr);
11608 if (TYPE_LENGTH (type) != blk->size)
11609 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
11610 TYPE_LENGTH (type));
11611 *bytes = blk->data;
11612 break;
11613
11614 /* The DW_AT_const_value attributes are supposed to carry the
11615 symbol's value "represented as it would be on the target
11616 architecture." By the time we get here, it's already been
11617 converted to host endianness, so we just need to sign- or
11618 zero-extend it as appropriate. */
11619 case DW_FORM_data1:
11620 *bytes = dwarf2_const_value_data (attr, type, name,
11621 obstack, cu, value, 8);
11622 break;
11623 case DW_FORM_data2:
11624 *bytes = dwarf2_const_value_data (attr, type, name,
11625 obstack, cu, value, 16);
11626 break;
11627 case DW_FORM_data4:
11628 *bytes = dwarf2_const_value_data (attr, type, name,
11629 obstack, cu, value, 32);
11630 break;
11631 case DW_FORM_data8:
11632 *bytes = dwarf2_const_value_data (attr, type, name,
11633 obstack, cu, value, 64);
11634 break;
11635
11636 case DW_FORM_sdata:
11637 *value = DW_SND (attr);
11638 break;
11639
11640 case DW_FORM_udata:
11641 *value = DW_UNSND (attr);
11642 break;
11643
11644 default:
11645 complaint (&symfile_complaints,
11646 _("unsupported const value attribute form: '%s'"),
11647 dwarf_form_name (attr->form));
11648 *value = 0;
11649 break;
11650 }
11651 }
11652
11653
11654 /* Copy constant value from an attribute to a symbol. */
11655
11656 static void
11657 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
11658 struct dwarf2_cu *cu)
11659 {
11660 struct objfile *objfile = cu->objfile;
11661 struct comp_unit_head *cu_header = &cu->header;
11662 long value;
11663 gdb_byte *bytes;
11664 struct dwarf2_locexpr_baton *baton;
11665
11666 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
11667 SYMBOL_PRINT_NAME (sym),
11668 &objfile->objfile_obstack, cu,
11669 &value, &bytes, &baton);
11670
11671 if (baton != NULL)
11672 {
11673 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
11674 SYMBOL_LOCATION_BATON (sym) = baton;
11675 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11676 }
11677 else if (bytes != NULL)
11678 {
11679 SYMBOL_VALUE_BYTES (sym) = bytes;
11680 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
11681 }
11682 else
11683 {
11684 SYMBOL_VALUE (sym) = value;
11685 SYMBOL_CLASS (sym) = LOC_CONST;
11686 }
11687 }
11688
11689 /* Return the type of the die in question using its DW_AT_type attribute. */
11690
11691 static struct type *
11692 die_type (struct die_info *die, struct dwarf2_cu *cu)
11693 {
11694 struct attribute *type_attr;
11695
11696 type_attr = dwarf2_attr (die, DW_AT_type, cu);
11697 if (!type_attr)
11698 {
11699 /* A missing DW_AT_type represents a void type. */
11700 return objfile_type (cu->objfile)->builtin_void;
11701 }
11702
11703 return lookup_die_type (die, type_attr, cu);
11704 }
11705
11706 /* True iff CU's producer generates GNAT Ada auxiliary information
11707 that allows to find parallel types through that information instead
11708 of having to do expensive parallel lookups by type name. */
11709
11710 static int
11711 need_gnat_info (struct dwarf2_cu *cu)
11712 {
11713 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
11714 of GNAT produces this auxiliary information, without any indication
11715 that it is produced. Part of enhancing the FSF version of GNAT
11716 to produce that information will be to put in place an indicator
11717 that we can use in order to determine whether the descriptive type
11718 info is available or not. One suggestion that has been made is
11719 to use a new attribute, attached to the CU die. For now, assume
11720 that the descriptive type info is not available. */
11721 return 0;
11722 }
11723
11724 /* Return the auxiliary type of the die in question using its
11725 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
11726 attribute is not present. */
11727
11728 static struct type *
11729 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
11730 {
11731 struct attribute *type_attr;
11732
11733 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
11734 if (!type_attr)
11735 return NULL;
11736
11737 return lookup_die_type (die, type_attr, cu);
11738 }
11739
11740 /* If DIE has a descriptive_type attribute, then set the TYPE's
11741 descriptive type accordingly. */
11742
11743 static void
11744 set_descriptive_type (struct type *type, struct die_info *die,
11745 struct dwarf2_cu *cu)
11746 {
11747 struct type *descriptive_type = die_descriptive_type (die, cu);
11748
11749 if (descriptive_type)
11750 {
11751 ALLOCATE_GNAT_AUX_TYPE (type);
11752 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
11753 }
11754 }
11755
11756 /* Return the containing type of the die in question using its
11757 DW_AT_containing_type attribute. */
11758
11759 static struct type *
11760 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
11761 {
11762 struct attribute *type_attr;
11763
11764 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
11765 if (!type_attr)
11766 error (_("Dwarf Error: Problem turning containing type into gdb type "
11767 "[in module %s]"), cu->objfile->name);
11768
11769 return lookup_die_type (die, type_attr, cu);
11770 }
11771
11772 /* Look up the type of DIE in CU using its type attribute ATTR.
11773 If there is no type substitute an error marker. */
11774
11775 static struct type *
11776 lookup_die_type (struct die_info *die, struct attribute *attr,
11777 struct dwarf2_cu *cu)
11778 {
11779 struct type *this_type;
11780
11781 /* First see if we have it cached. */
11782
11783 if (is_ref_attr (attr))
11784 {
11785 unsigned int offset = dwarf2_get_ref_die_offset (attr);
11786
11787 this_type = get_die_type_at_offset (offset, cu->per_cu);
11788 }
11789 else if (attr->form == DW_FORM_ref_sig8)
11790 {
11791 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
11792 struct dwarf2_cu *sig_cu;
11793 unsigned int offset;
11794
11795 /* sig_type will be NULL if the signatured type is missing from
11796 the debug info. */
11797 if (sig_type == NULL)
11798 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
11799 "at 0x%x [in module %s]"),
11800 die->offset, cu->objfile->name);
11801
11802 gdb_assert (sig_type->per_cu.from_debug_types);
11803 offset = sig_type->per_cu.offset + sig_type->type_offset;
11804 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
11805 }
11806 else
11807 {
11808 dump_die_for_error (die);
11809 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
11810 dwarf_attr_name (attr->name), cu->objfile->name);
11811 }
11812
11813 /* If not cached we need to read it in. */
11814
11815 if (this_type == NULL)
11816 {
11817 struct die_info *type_die;
11818 struct dwarf2_cu *type_cu = cu;
11819
11820 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
11821 /* If the type is cached, we should have found it above. */
11822 gdb_assert (get_die_type (type_die, type_cu) == NULL);
11823 this_type = read_type_die_1 (type_die, type_cu);
11824 }
11825
11826 /* If we still don't have a type use an error marker. */
11827
11828 if (this_type == NULL)
11829 {
11830 char *message, *saved;
11831
11832 /* read_type_die already issued a complaint. */
11833 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
11834 cu->objfile->name,
11835 cu->header.offset,
11836 die->offset);
11837 saved = obstack_copy0 (&cu->objfile->objfile_obstack,
11838 message, strlen (message));
11839 xfree (message);
11840
11841 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, cu->objfile);
11842 }
11843
11844 return this_type;
11845 }
11846
11847 /* Return the type in DIE, CU.
11848 Returns NULL for invalid types.
11849
11850 This first does a lookup in the appropriate type_hash table,
11851 and only reads the die in if necessary.
11852
11853 NOTE: This can be called when reading in partial or full symbols. */
11854
11855 static struct type *
11856 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
11857 {
11858 struct type *this_type;
11859
11860 this_type = get_die_type (die, cu);
11861 if (this_type)
11862 return this_type;
11863
11864 return read_type_die_1 (die, cu);
11865 }
11866
11867 /* Read the type in DIE, CU.
11868 Returns NULL for invalid types. */
11869
11870 static struct type *
11871 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
11872 {
11873 struct type *this_type = NULL;
11874
11875 switch (die->tag)
11876 {
11877 case DW_TAG_class_type:
11878 case DW_TAG_interface_type:
11879 case DW_TAG_structure_type:
11880 case DW_TAG_union_type:
11881 this_type = read_structure_type (die, cu);
11882 break;
11883 case DW_TAG_enumeration_type:
11884 this_type = read_enumeration_type (die, cu);
11885 break;
11886 case DW_TAG_subprogram:
11887 case DW_TAG_subroutine_type:
11888 case DW_TAG_inlined_subroutine:
11889 this_type = read_subroutine_type (die, cu);
11890 break;
11891 case DW_TAG_array_type:
11892 this_type = read_array_type (die, cu);
11893 break;
11894 case DW_TAG_set_type:
11895 this_type = read_set_type (die, cu);
11896 break;
11897 case DW_TAG_pointer_type:
11898 this_type = read_tag_pointer_type (die, cu);
11899 break;
11900 case DW_TAG_ptr_to_member_type:
11901 this_type = read_tag_ptr_to_member_type (die, cu);
11902 break;
11903 case DW_TAG_reference_type:
11904 this_type = read_tag_reference_type (die, cu);
11905 break;
11906 case DW_TAG_const_type:
11907 this_type = read_tag_const_type (die, cu);
11908 break;
11909 case DW_TAG_volatile_type:
11910 this_type = read_tag_volatile_type (die, cu);
11911 break;
11912 case DW_TAG_string_type:
11913 this_type = read_tag_string_type (die, cu);
11914 break;
11915 case DW_TAG_typedef:
11916 this_type = read_typedef (die, cu);
11917 break;
11918 case DW_TAG_subrange_type:
11919 this_type = read_subrange_type (die, cu);
11920 break;
11921 case DW_TAG_base_type:
11922 this_type = read_base_type (die, cu);
11923 break;
11924 case DW_TAG_unspecified_type:
11925 this_type = read_unspecified_type (die, cu);
11926 break;
11927 case DW_TAG_namespace:
11928 this_type = read_namespace_type (die, cu);
11929 break;
11930 case DW_TAG_module:
11931 this_type = read_module_type (die, cu);
11932 break;
11933 default:
11934 complaint (&symfile_complaints,
11935 _("unexpected tag in read_type_die: '%s'"),
11936 dwarf_tag_name (die->tag));
11937 break;
11938 }
11939
11940 return this_type;
11941 }
11942
11943 /* See if we can figure out if the class lives in a namespace. We do
11944 this by looking for a member function; its demangled name will
11945 contain namespace info, if there is any.
11946 Return the computed name or NULL.
11947 Space for the result is allocated on the objfile's obstack.
11948 This is the full-die version of guess_partial_die_structure_name.
11949 In this case we know DIE has no useful parent. */
11950
11951 static char *
11952 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
11953 {
11954 struct die_info *spec_die;
11955 struct dwarf2_cu *spec_cu;
11956 struct die_info *child;
11957
11958 spec_cu = cu;
11959 spec_die = die_specification (die, &spec_cu);
11960 if (spec_die != NULL)
11961 {
11962 die = spec_die;
11963 cu = spec_cu;
11964 }
11965
11966 for (child = die->child;
11967 child != NULL;
11968 child = child->sibling)
11969 {
11970 if (child->tag == DW_TAG_subprogram)
11971 {
11972 struct attribute *attr;
11973
11974 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
11975 if (attr == NULL)
11976 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
11977 if (attr != NULL)
11978 {
11979 char *actual_name
11980 = language_class_name_from_physname (cu->language_defn,
11981 DW_STRING (attr));
11982 char *name = NULL;
11983
11984 if (actual_name != NULL)
11985 {
11986 char *die_name = dwarf2_name (die, cu);
11987
11988 if (die_name != NULL
11989 && strcmp (die_name, actual_name) != 0)
11990 {
11991 /* Strip off the class name from the full name.
11992 We want the prefix. */
11993 int die_name_len = strlen (die_name);
11994 int actual_name_len = strlen (actual_name);
11995
11996 /* Test for '::' as a sanity check. */
11997 if (actual_name_len > die_name_len + 2
11998 && actual_name[actual_name_len
11999 - die_name_len - 1] == ':')
12000 name =
12001 obsavestring (actual_name,
12002 actual_name_len - die_name_len - 2,
12003 &cu->objfile->objfile_obstack);
12004 }
12005 }
12006 xfree (actual_name);
12007 return name;
12008 }
12009 }
12010 }
12011
12012 return NULL;
12013 }
12014
12015 /* Return the name of the namespace/class that DIE is defined within,
12016 or "" if we can't tell. The caller should not xfree the result.
12017
12018 For example, if we're within the method foo() in the following
12019 code:
12020
12021 namespace N {
12022 class C {
12023 void foo () {
12024 }
12025 };
12026 }
12027
12028 then determine_prefix on foo's die will return "N::C". */
12029
12030 static char *
12031 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12032 {
12033 struct die_info *parent, *spec_die;
12034 struct dwarf2_cu *spec_cu;
12035 struct type *parent_type;
12036
12037 if (cu->language != language_cplus && cu->language != language_java
12038 && cu->language != language_fortran)
12039 return "";
12040
12041 /* We have to be careful in the presence of DW_AT_specification.
12042 For example, with GCC 3.4, given the code
12043
12044 namespace N {
12045 void foo() {
12046 // Definition of N::foo.
12047 }
12048 }
12049
12050 then we'll have a tree of DIEs like this:
12051
12052 1: DW_TAG_compile_unit
12053 2: DW_TAG_namespace // N
12054 3: DW_TAG_subprogram // declaration of N::foo
12055 4: DW_TAG_subprogram // definition of N::foo
12056 DW_AT_specification // refers to die #3
12057
12058 Thus, when processing die #4, we have to pretend that we're in
12059 the context of its DW_AT_specification, namely the contex of die
12060 #3. */
12061 spec_cu = cu;
12062 spec_die = die_specification (die, &spec_cu);
12063 if (spec_die == NULL)
12064 parent = die->parent;
12065 else
12066 {
12067 parent = spec_die->parent;
12068 cu = spec_cu;
12069 }
12070
12071 if (parent == NULL)
12072 return "";
12073 else if (parent->building_fullname)
12074 {
12075 const char *name;
12076 const char *parent_name;
12077
12078 /* It has been seen on RealView 2.2 built binaries,
12079 DW_TAG_template_type_param types actually _defined_ as
12080 children of the parent class:
12081
12082 enum E {};
12083 template class <class Enum> Class{};
12084 Class<enum E> class_e;
12085
12086 1: DW_TAG_class_type (Class)
12087 2: DW_TAG_enumeration_type (E)
12088 3: DW_TAG_enumerator (enum1:0)
12089 3: DW_TAG_enumerator (enum2:1)
12090 ...
12091 2: DW_TAG_template_type_param
12092 DW_AT_type DW_FORM_ref_udata (E)
12093
12094 Besides being broken debug info, it can put GDB into an
12095 infinite loop. Consider:
12096
12097 When we're building the full name for Class<E>, we'll start
12098 at Class, and go look over its template type parameters,
12099 finding E. We'll then try to build the full name of E, and
12100 reach here. We're now trying to build the full name of E,
12101 and look over the parent DIE for containing scope. In the
12102 broken case, if we followed the parent DIE of E, we'd again
12103 find Class, and once again go look at its template type
12104 arguments, etc., etc. Simply don't consider such parent die
12105 as source-level parent of this die (it can't be, the language
12106 doesn't allow it), and break the loop here. */
12107 name = dwarf2_name (die, cu);
12108 parent_name = dwarf2_name (parent, cu);
12109 complaint (&symfile_complaints,
12110 _("template param type '%s' defined within parent '%s'"),
12111 name ? name : "<unknown>",
12112 parent_name ? parent_name : "<unknown>");
12113 return "";
12114 }
12115 else
12116 switch (parent->tag)
12117 {
12118 case DW_TAG_namespace:
12119 parent_type = read_type_die (parent, cu);
12120 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12121 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12122 Work around this problem here. */
12123 if (cu->language == language_cplus
12124 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12125 return "";
12126 /* We give a name to even anonymous namespaces. */
12127 return TYPE_TAG_NAME (parent_type);
12128 case DW_TAG_class_type:
12129 case DW_TAG_interface_type:
12130 case DW_TAG_structure_type:
12131 case DW_TAG_union_type:
12132 case DW_TAG_module:
12133 parent_type = read_type_die (parent, cu);
12134 if (TYPE_TAG_NAME (parent_type) != NULL)
12135 return TYPE_TAG_NAME (parent_type);
12136 else
12137 /* An anonymous structure is only allowed non-static data
12138 members; no typedefs, no member functions, et cetera.
12139 So it does not need a prefix. */
12140 return "";
12141 case DW_TAG_compile_unit:
12142 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12143 if (cu->language == language_cplus
12144 && dwarf2_per_objfile->types.asection != NULL
12145 && die->child != NULL
12146 && (die->tag == DW_TAG_class_type
12147 || die->tag == DW_TAG_structure_type
12148 || die->tag == DW_TAG_union_type))
12149 {
12150 char *name = guess_full_die_structure_name (die, cu);
12151 if (name != NULL)
12152 return name;
12153 }
12154 return "";
12155 default:
12156 return determine_prefix (parent, cu);
12157 }
12158 }
12159
12160 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12161 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12162 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12163 an obconcat, otherwise allocate storage for the result. The CU argument is
12164 used to determine the language and hence, the appropriate separator. */
12165
12166 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12167
12168 static char *
12169 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12170 int physname, struct dwarf2_cu *cu)
12171 {
12172 const char *lead = "";
12173 const char *sep;
12174
12175 if (suffix == NULL || suffix[0] == '\0'
12176 || prefix == NULL || prefix[0] == '\0')
12177 sep = "";
12178 else if (cu->language == language_java)
12179 sep = ".";
12180 else if (cu->language == language_fortran && physname)
12181 {
12182 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12183 DW_AT_MIPS_linkage_name is preferred and used instead. */
12184
12185 lead = "__";
12186 sep = "_MOD_";
12187 }
12188 else
12189 sep = "::";
12190
12191 if (prefix == NULL)
12192 prefix = "";
12193 if (suffix == NULL)
12194 suffix = "";
12195
12196 if (obs == NULL)
12197 {
12198 char *retval
12199 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12200
12201 strcpy (retval, lead);
12202 strcat (retval, prefix);
12203 strcat (retval, sep);
12204 strcat (retval, suffix);
12205 return retval;
12206 }
12207 else
12208 {
12209 /* We have an obstack. */
12210 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12211 }
12212 }
12213
12214 /* Return sibling of die, NULL if no sibling. */
12215
12216 static struct die_info *
12217 sibling_die (struct die_info *die)
12218 {
12219 return die->sibling;
12220 }
12221
12222 /* Get name of a die, return NULL if not found. */
12223
12224 static char *
12225 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12226 struct obstack *obstack)
12227 {
12228 if (name && cu->language == language_cplus)
12229 {
12230 char *canon_name = cp_canonicalize_string (name);
12231
12232 if (canon_name != NULL)
12233 {
12234 if (strcmp (canon_name, name) != 0)
12235 name = obsavestring (canon_name, strlen (canon_name),
12236 obstack);
12237 xfree (canon_name);
12238 }
12239 }
12240
12241 return name;
12242 }
12243
12244 /* Get name of a die, return NULL if not found. */
12245
12246 static char *
12247 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12248 {
12249 struct attribute *attr;
12250
12251 attr = dwarf2_attr (die, DW_AT_name, cu);
12252 if ((!attr || !DW_STRING (attr))
12253 && die->tag != DW_TAG_class_type
12254 && die->tag != DW_TAG_interface_type
12255 && die->tag != DW_TAG_structure_type
12256 && die->tag != DW_TAG_union_type)
12257 return NULL;
12258
12259 switch (die->tag)
12260 {
12261 case DW_TAG_compile_unit:
12262 /* Compilation units have a DW_AT_name that is a filename, not
12263 a source language identifier. */
12264 case DW_TAG_enumeration_type:
12265 case DW_TAG_enumerator:
12266 /* These tags always have simple identifiers already; no need
12267 to canonicalize them. */
12268 return DW_STRING (attr);
12269
12270 case DW_TAG_subprogram:
12271 /* Java constructors will all be named "<init>", so return
12272 the class name when we see this special case. */
12273 if (cu->language == language_java
12274 && DW_STRING (attr) != NULL
12275 && strcmp (DW_STRING (attr), "<init>") == 0)
12276 {
12277 struct dwarf2_cu *spec_cu = cu;
12278 struct die_info *spec_die;
12279
12280 /* GCJ will output '<init>' for Java constructor names.
12281 For this special case, return the name of the parent class. */
12282
12283 /* GCJ may output suprogram DIEs with AT_specification set.
12284 If so, use the name of the specified DIE. */
12285 spec_die = die_specification (die, &spec_cu);
12286 if (spec_die != NULL)
12287 return dwarf2_name (spec_die, spec_cu);
12288
12289 do
12290 {
12291 die = die->parent;
12292 if (die->tag == DW_TAG_class_type)
12293 return dwarf2_name (die, cu);
12294 }
12295 while (die->tag != DW_TAG_compile_unit);
12296 }
12297 break;
12298
12299 case DW_TAG_class_type:
12300 case DW_TAG_interface_type:
12301 case DW_TAG_structure_type:
12302 case DW_TAG_union_type:
12303 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12304 structures or unions. These were of the form "._%d" in GCC 4.1,
12305 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12306 and GCC 4.4. We work around this problem by ignoring these. */
12307 if (attr && DW_STRING (attr)
12308 && (strncmp (DW_STRING (attr), "._", 2) == 0
12309 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12310 return NULL;
12311
12312 /* GCC might emit a nameless typedef that has a linkage name. See
12313 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12314 if (!attr || DW_STRING (attr) == NULL)
12315 {
12316 char *demangled = NULL;
12317
12318 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12319 if (attr == NULL)
12320 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12321
12322 if (attr == NULL || DW_STRING (attr) == NULL)
12323 return NULL;
12324
12325 /* Avoid demangling DW_STRING (attr) the second time on a second
12326 call for the same DIE. */
12327 if (!DW_STRING_IS_CANONICAL (attr))
12328 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12329
12330 if (demangled)
12331 {
12332 /* FIXME: we already did this for the partial symbol... */
12333 DW_STRING (attr)
12334 = obsavestring (demangled, strlen (demangled),
12335 &cu->objfile->objfile_obstack);
12336 DW_STRING_IS_CANONICAL (attr) = 1;
12337 xfree (demangled);
12338 }
12339 }
12340 break;
12341
12342 default:
12343 break;
12344 }
12345
12346 if (!DW_STRING_IS_CANONICAL (attr))
12347 {
12348 DW_STRING (attr)
12349 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12350 &cu->objfile->objfile_obstack);
12351 DW_STRING_IS_CANONICAL (attr) = 1;
12352 }
12353 return DW_STRING (attr);
12354 }
12355
12356 /* Return the die that this die in an extension of, or NULL if there
12357 is none. *EXT_CU is the CU containing DIE on input, and the CU
12358 containing the return value on output. */
12359
12360 static struct die_info *
12361 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12362 {
12363 struct attribute *attr;
12364
12365 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12366 if (attr == NULL)
12367 return NULL;
12368
12369 return follow_die_ref (die, attr, ext_cu);
12370 }
12371
12372 /* Convert a DIE tag into its string name. */
12373
12374 static char *
12375 dwarf_tag_name (unsigned tag)
12376 {
12377 switch (tag)
12378 {
12379 case DW_TAG_padding:
12380 return "DW_TAG_padding";
12381 case DW_TAG_array_type:
12382 return "DW_TAG_array_type";
12383 case DW_TAG_class_type:
12384 return "DW_TAG_class_type";
12385 case DW_TAG_entry_point:
12386 return "DW_TAG_entry_point";
12387 case DW_TAG_enumeration_type:
12388 return "DW_TAG_enumeration_type";
12389 case DW_TAG_formal_parameter:
12390 return "DW_TAG_formal_parameter";
12391 case DW_TAG_imported_declaration:
12392 return "DW_TAG_imported_declaration";
12393 case DW_TAG_label:
12394 return "DW_TAG_label";
12395 case DW_TAG_lexical_block:
12396 return "DW_TAG_lexical_block";
12397 case DW_TAG_member:
12398 return "DW_TAG_member";
12399 case DW_TAG_pointer_type:
12400 return "DW_TAG_pointer_type";
12401 case DW_TAG_reference_type:
12402 return "DW_TAG_reference_type";
12403 case DW_TAG_compile_unit:
12404 return "DW_TAG_compile_unit";
12405 case DW_TAG_string_type:
12406 return "DW_TAG_string_type";
12407 case DW_TAG_structure_type:
12408 return "DW_TAG_structure_type";
12409 case DW_TAG_subroutine_type:
12410 return "DW_TAG_subroutine_type";
12411 case DW_TAG_typedef:
12412 return "DW_TAG_typedef";
12413 case DW_TAG_union_type:
12414 return "DW_TAG_union_type";
12415 case DW_TAG_unspecified_parameters:
12416 return "DW_TAG_unspecified_parameters";
12417 case DW_TAG_variant:
12418 return "DW_TAG_variant";
12419 case DW_TAG_common_block:
12420 return "DW_TAG_common_block";
12421 case DW_TAG_common_inclusion:
12422 return "DW_TAG_common_inclusion";
12423 case DW_TAG_inheritance:
12424 return "DW_TAG_inheritance";
12425 case DW_TAG_inlined_subroutine:
12426 return "DW_TAG_inlined_subroutine";
12427 case DW_TAG_module:
12428 return "DW_TAG_module";
12429 case DW_TAG_ptr_to_member_type:
12430 return "DW_TAG_ptr_to_member_type";
12431 case DW_TAG_set_type:
12432 return "DW_TAG_set_type";
12433 case DW_TAG_subrange_type:
12434 return "DW_TAG_subrange_type";
12435 case DW_TAG_with_stmt:
12436 return "DW_TAG_with_stmt";
12437 case DW_TAG_access_declaration:
12438 return "DW_TAG_access_declaration";
12439 case DW_TAG_base_type:
12440 return "DW_TAG_base_type";
12441 case DW_TAG_catch_block:
12442 return "DW_TAG_catch_block";
12443 case DW_TAG_const_type:
12444 return "DW_TAG_const_type";
12445 case DW_TAG_constant:
12446 return "DW_TAG_constant";
12447 case DW_TAG_enumerator:
12448 return "DW_TAG_enumerator";
12449 case DW_TAG_file_type:
12450 return "DW_TAG_file_type";
12451 case DW_TAG_friend:
12452 return "DW_TAG_friend";
12453 case DW_TAG_namelist:
12454 return "DW_TAG_namelist";
12455 case DW_TAG_namelist_item:
12456 return "DW_TAG_namelist_item";
12457 case DW_TAG_packed_type:
12458 return "DW_TAG_packed_type";
12459 case DW_TAG_subprogram:
12460 return "DW_TAG_subprogram";
12461 case DW_TAG_template_type_param:
12462 return "DW_TAG_template_type_param";
12463 case DW_TAG_template_value_param:
12464 return "DW_TAG_template_value_param";
12465 case DW_TAG_thrown_type:
12466 return "DW_TAG_thrown_type";
12467 case DW_TAG_try_block:
12468 return "DW_TAG_try_block";
12469 case DW_TAG_variant_part:
12470 return "DW_TAG_variant_part";
12471 case DW_TAG_variable:
12472 return "DW_TAG_variable";
12473 case DW_TAG_volatile_type:
12474 return "DW_TAG_volatile_type";
12475 case DW_TAG_dwarf_procedure:
12476 return "DW_TAG_dwarf_procedure";
12477 case DW_TAG_restrict_type:
12478 return "DW_TAG_restrict_type";
12479 case DW_TAG_interface_type:
12480 return "DW_TAG_interface_type";
12481 case DW_TAG_namespace:
12482 return "DW_TAG_namespace";
12483 case DW_TAG_imported_module:
12484 return "DW_TAG_imported_module";
12485 case DW_TAG_unspecified_type:
12486 return "DW_TAG_unspecified_type";
12487 case DW_TAG_partial_unit:
12488 return "DW_TAG_partial_unit";
12489 case DW_TAG_imported_unit:
12490 return "DW_TAG_imported_unit";
12491 case DW_TAG_condition:
12492 return "DW_TAG_condition";
12493 case DW_TAG_shared_type:
12494 return "DW_TAG_shared_type";
12495 case DW_TAG_type_unit:
12496 return "DW_TAG_type_unit";
12497 case DW_TAG_MIPS_loop:
12498 return "DW_TAG_MIPS_loop";
12499 case DW_TAG_HP_array_descriptor:
12500 return "DW_TAG_HP_array_descriptor";
12501 case DW_TAG_format_label:
12502 return "DW_TAG_format_label";
12503 case DW_TAG_function_template:
12504 return "DW_TAG_function_template";
12505 case DW_TAG_class_template:
12506 return "DW_TAG_class_template";
12507 case DW_TAG_GNU_BINCL:
12508 return "DW_TAG_GNU_BINCL";
12509 case DW_TAG_GNU_EINCL:
12510 return "DW_TAG_GNU_EINCL";
12511 case DW_TAG_upc_shared_type:
12512 return "DW_TAG_upc_shared_type";
12513 case DW_TAG_upc_strict_type:
12514 return "DW_TAG_upc_strict_type";
12515 case DW_TAG_upc_relaxed_type:
12516 return "DW_TAG_upc_relaxed_type";
12517 case DW_TAG_PGI_kanji_type:
12518 return "DW_TAG_PGI_kanji_type";
12519 case DW_TAG_PGI_interface_block:
12520 return "DW_TAG_PGI_interface_block";
12521 default:
12522 return "DW_TAG_<unknown>";
12523 }
12524 }
12525
12526 /* Convert a DWARF attribute code into its string name. */
12527
12528 static char *
12529 dwarf_attr_name (unsigned attr)
12530 {
12531 switch (attr)
12532 {
12533 case DW_AT_sibling:
12534 return "DW_AT_sibling";
12535 case DW_AT_location:
12536 return "DW_AT_location";
12537 case DW_AT_name:
12538 return "DW_AT_name";
12539 case DW_AT_ordering:
12540 return "DW_AT_ordering";
12541 case DW_AT_subscr_data:
12542 return "DW_AT_subscr_data";
12543 case DW_AT_byte_size:
12544 return "DW_AT_byte_size";
12545 case DW_AT_bit_offset:
12546 return "DW_AT_bit_offset";
12547 case DW_AT_bit_size:
12548 return "DW_AT_bit_size";
12549 case DW_AT_element_list:
12550 return "DW_AT_element_list";
12551 case DW_AT_stmt_list:
12552 return "DW_AT_stmt_list";
12553 case DW_AT_low_pc:
12554 return "DW_AT_low_pc";
12555 case DW_AT_high_pc:
12556 return "DW_AT_high_pc";
12557 case DW_AT_language:
12558 return "DW_AT_language";
12559 case DW_AT_member:
12560 return "DW_AT_member";
12561 case DW_AT_discr:
12562 return "DW_AT_discr";
12563 case DW_AT_discr_value:
12564 return "DW_AT_discr_value";
12565 case DW_AT_visibility:
12566 return "DW_AT_visibility";
12567 case DW_AT_import:
12568 return "DW_AT_import";
12569 case DW_AT_string_length:
12570 return "DW_AT_string_length";
12571 case DW_AT_common_reference:
12572 return "DW_AT_common_reference";
12573 case DW_AT_comp_dir:
12574 return "DW_AT_comp_dir";
12575 case DW_AT_const_value:
12576 return "DW_AT_const_value";
12577 case DW_AT_containing_type:
12578 return "DW_AT_containing_type";
12579 case DW_AT_default_value:
12580 return "DW_AT_default_value";
12581 case DW_AT_inline:
12582 return "DW_AT_inline";
12583 case DW_AT_is_optional:
12584 return "DW_AT_is_optional";
12585 case DW_AT_lower_bound:
12586 return "DW_AT_lower_bound";
12587 case DW_AT_producer:
12588 return "DW_AT_producer";
12589 case DW_AT_prototyped:
12590 return "DW_AT_prototyped";
12591 case DW_AT_return_addr:
12592 return "DW_AT_return_addr";
12593 case DW_AT_start_scope:
12594 return "DW_AT_start_scope";
12595 case DW_AT_bit_stride:
12596 return "DW_AT_bit_stride";
12597 case DW_AT_upper_bound:
12598 return "DW_AT_upper_bound";
12599 case DW_AT_abstract_origin:
12600 return "DW_AT_abstract_origin";
12601 case DW_AT_accessibility:
12602 return "DW_AT_accessibility";
12603 case DW_AT_address_class:
12604 return "DW_AT_address_class";
12605 case DW_AT_artificial:
12606 return "DW_AT_artificial";
12607 case DW_AT_base_types:
12608 return "DW_AT_base_types";
12609 case DW_AT_calling_convention:
12610 return "DW_AT_calling_convention";
12611 case DW_AT_count:
12612 return "DW_AT_count";
12613 case DW_AT_data_member_location:
12614 return "DW_AT_data_member_location";
12615 case DW_AT_decl_column:
12616 return "DW_AT_decl_column";
12617 case DW_AT_decl_file:
12618 return "DW_AT_decl_file";
12619 case DW_AT_decl_line:
12620 return "DW_AT_decl_line";
12621 case DW_AT_declaration:
12622 return "DW_AT_declaration";
12623 case DW_AT_discr_list:
12624 return "DW_AT_discr_list";
12625 case DW_AT_encoding:
12626 return "DW_AT_encoding";
12627 case DW_AT_external:
12628 return "DW_AT_external";
12629 case DW_AT_frame_base:
12630 return "DW_AT_frame_base";
12631 case DW_AT_friend:
12632 return "DW_AT_friend";
12633 case DW_AT_identifier_case:
12634 return "DW_AT_identifier_case";
12635 case DW_AT_macro_info:
12636 return "DW_AT_macro_info";
12637 case DW_AT_namelist_items:
12638 return "DW_AT_namelist_items";
12639 case DW_AT_priority:
12640 return "DW_AT_priority";
12641 case DW_AT_segment:
12642 return "DW_AT_segment";
12643 case DW_AT_specification:
12644 return "DW_AT_specification";
12645 case DW_AT_static_link:
12646 return "DW_AT_static_link";
12647 case DW_AT_type:
12648 return "DW_AT_type";
12649 case DW_AT_use_location:
12650 return "DW_AT_use_location";
12651 case DW_AT_variable_parameter:
12652 return "DW_AT_variable_parameter";
12653 case DW_AT_virtuality:
12654 return "DW_AT_virtuality";
12655 case DW_AT_vtable_elem_location:
12656 return "DW_AT_vtable_elem_location";
12657 /* DWARF 3 values. */
12658 case DW_AT_allocated:
12659 return "DW_AT_allocated";
12660 case DW_AT_associated:
12661 return "DW_AT_associated";
12662 case DW_AT_data_location:
12663 return "DW_AT_data_location";
12664 case DW_AT_byte_stride:
12665 return "DW_AT_byte_stride";
12666 case DW_AT_entry_pc:
12667 return "DW_AT_entry_pc";
12668 case DW_AT_use_UTF8:
12669 return "DW_AT_use_UTF8";
12670 case DW_AT_extension:
12671 return "DW_AT_extension";
12672 case DW_AT_ranges:
12673 return "DW_AT_ranges";
12674 case DW_AT_trampoline:
12675 return "DW_AT_trampoline";
12676 case DW_AT_call_column:
12677 return "DW_AT_call_column";
12678 case DW_AT_call_file:
12679 return "DW_AT_call_file";
12680 case DW_AT_call_line:
12681 return "DW_AT_call_line";
12682 case DW_AT_description:
12683 return "DW_AT_description";
12684 case DW_AT_binary_scale:
12685 return "DW_AT_binary_scale";
12686 case DW_AT_decimal_scale:
12687 return "DW_AT_decimal_scale";
12688 case DW_AT_small:
12689 return "DW_AT_small";
12690 case DW_AT_decimal_sign:
12691 return "DW_AT_decimal_sign";
12692 case DW_AT_digit_count:
12693 return "DW_AT_digit_count";
12694 case DW_AT_picture_string:
12695 return "DW_AT_picture_string";
12696 case DW_AT_mutable:
12697 return "DW_AT_mutable";
12698 case DW_AT_threads_scaled:
12699 return "DW_AT_threads_scaled";
12700 case DW_AT_explicit:
12701 return "DW_AT_explicit";
12702 case DW_AT_object_pointer:
12703 return "DW_AT_object_pointer";
12704 case DW_AT_endianity:
12705 return "DW_AT_endianity";
12706 case DW_AT_elemental:
12707 return "DW_AT_elemental";
12708 case DW_AT_pure:
12709 return "DW_AT_pure";
12710 case DW_AT_recursive:
12711 return "DW_AT_recursive";
12712 /* DWARF 4 values. */
12713 case DW_AT_signature:
12714 return "DW_AT_signature";
12715 case DW_AT_linkage_name:
12716 return "DW_AT_linkage_name";
12717 /* SGI/MIPS extensions. */
12718 #ifdef MIPS /* collides with DW_AT_HP_block_index */
12719 case DW_AT_MIPS_fde:
12720 return "DW_AT_MIPS_fde";
12721 #endif
12722 case DW_AT_MIPS_loop_begin:
12723 return "DW_AT_MIPS_loop_begin";
12724 case DW_AT_MIPS_tail_loop_begin:
12725 return "DW_AT_MIPS_tail_loop_begin";
12726 case DW_AT_MIPS_epilog_begin:
12727 return "DW_AT_MIPS_epilog_begin";
12728 case DW_AT_MIPS_loop_unroll_factor:
12729 return "DW_AT_MIPS_loop_unroll_factor";
12730 case DW_AT_MIPS_software_pipeline_depth:
12731 return "DW_AT_MIPS_software_pipeline_depth";
12732 case DW_AT_MIPS_linkage_name:
12733 return "DW_AT_MIPS_linkage_name";
12734 case DW_AT_MIPS_stride:
12735 return "DW_AT_MIPS_stride";
12736 case DW_AT_MIPS_abstract_name:
12737 return "DW_AT_MIPS_abstract_name";
12738 case DW_AT_MIPS_clone_origin:
12739 return "DW_AT_MIPS_clone_origin";
12740 case DW_AT_MIPS_has_inlines:
12741 return "DW_AT_MIPS_has_inlines";
12742 /* HP extensions. */
12743 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
12744 case DW_AT_HP_block_index:
12745 return "DW_AT_HP_block_index";
12746 #endif
12747 case DW_AT_HP_unmodifiable:
12748 return "DW_AT_HP_unmodifiable";
12749 case DW_AT_HP_actuals_stmt_list:
12750 return "DW_AT_HP_actuals_stmt_list";
12751 case DW_AT_HP_proc_per_section:
12752 return "DW_AT_HP_proc_per_section";
12753 case DW_AT_HP_raw_data_ptr:
12754 return "DW_AT_HP_raw_data_ptr";
12755 case DW_AT_HP_pass_by_reference:
12756 return "DW_AT_HP_pass_by_reference";
12757 case DW_AT_HP_opt_level:
12758 return "DW_AT_HP_opt_level";
12759 case DW_AT_HP_prof_version_id:
12760 return "DW_AT_HP_prof_version_id";
12761 case DW_AT_HP_opt_flags:
12762 return "DW_AT_HP_opt_flags";
12763 case DW_AT_HP_cold_region_low_pc:
12764 return "DW_AT_HP_cold_region_low_pc";
12765 case DW_AT_HP_cold_region_high_pc:
12766 return "DW_AT_HP_cold_region_high_pc";
12767 case DW_AT_HP_all_variables_modifiable:
12768 return "DW_AT_HP_all_variables_modifiable";
12769 case DW_AT_HP_linkage_name:
12770 return "DW_AT_HP_linkage_name";
12771 case DW_AT_HP_prof_flags:
12772 return "DW_AT_HP_prof_flags";
12773 /* GNU extensions. */
12774 case DW_AT_sf_names:
12775 return "DW_AT_sf_names";
12776 case DW_AT_src_info:
12777 return "DW_AT_src_info";
12778 case DW_AT_mac_info:
12779 return "DW_AT_mac_info";
12780 case DW_AT_src_coords:
12781 return "DW_AT_src_coords";
12782 case DW_AT_body_begin:
12783 return "DW_AT_body_begin";
12784 case DW_AT_body_end:
12785 return "DW_AT_body_end";
12786 case DW_AT_GNU_vector:
12787 return "DW_AT_GNU_vector";
12788 case DW_AT_GNU_odr_signature:
12789 return "DW_AT_GNU_odr_signature";
12790 /* VMS extensions. */
12791 case DW_AT_VMS_rtnbeg_pd_address:
12792 return "DW_AT_VMS_rtnbeg_pd_address";
12793 /* UPC extension. */
12794 case DW_AT_upc_threads_scaled:
12795 return "DW_AT_upc_threads_scaled";
12796 /* PGI (STMicroelectronics) extensions. */
12797 case DW_AT_PGI_lbase:
12798 return "DW_AT_PGI_lbase";
12799 case DW_AT_PGI_soffset:
12800 return "DW_AT_PGI_soffset";
12801 case DW_AT_PGI_lstride:
12802 return "DW_AT_PGI_lstride";
12803 default:
12804 return "DW_AT_<unknown>";
12805 }
12806 }
12807
12808 /* Convert a DWARF value form code into its string name. */
12809
12810 static char *
12811 dwarf_form_name (unsigned form)
12812 {
12813 switch (form)
12814 {
12815 case DW_FORM_addr:
12816 return "DW_FORM_addr";
12817 case DW_FORM_block2:
12818 return "DW_FORM_block2";
12819 case DW_FORM_block4:
12820 return "DW_FORM_block4";
12821 case DW_FORM_data2:
12822 return "DW_FORM_data2";
12823 case DW_FORM_data4:
12824 return "DW_FORM_data4";
12825 case DW_FORM_data8:
12826 return "DW_FORM_data8";
12827 case DW_FORM_string:
12828 return "DW_FORM_string";
12829 case DW_FORM_block:
12830 return "DW_FORM_block";
12831 case DW_FORM_block1:
12832 return "DW_FORM_block1";
12833 case DW_FORM_data1:
12834 return "DW_FORM_data1";
12835 case DW_FORM_flag:
12836 return "DW_FORM_flag";
12837 case DW_FORM_sdata:
12838 return "DW_FORM_sdata";
12839 case DW_FORM_strp:
12840 return "DW_FORM_strp";
12841 case DW_FORM_udata:
12842 return "DW_FORM_udata";
12843 case DW_FORM_ref_addr:
12844 return "DW_FORM_ref_addr";
12845 case DW_FORM_ref1:
12846 return "DW_FORM_ref1";
12847 case DW_FORM_ref2:
12848 return "DW_FORM_ref2";
12849 case DW_FORM_ref4:
12850 return "DW_FORM_ref4";
12851 case DW_FORM_ref8:
12852 return "DW_FORM_ref8";
12853 case DW_FORM_ref_udata:
12854 return "DW_FORM_ref_udata";
12855 case DW_FORM_indirect:
12856 return "DW_FORM_indirect";
12857 case DW_FORM_sec_offset:
12858 return "DW_FORM_sec_offset";
12859 case DW_FORM_exprloc:
12860 return "DW_FORM_exprloc";
12861 case DW_FORM_flag_present:
12862 return "DW_FORM_flag_present";
12863 case DW_FORM_ref_sig8:
12864 return "DW_FORM_ref_sig8";
12865 default:
12866 return "DW_FORM_<unknown>";
12867 }
12868 }
12869
12870 /* Convert a DWARF stack opcode into its string name. */
12871
12872 const char *
12873 dwarf_stack_op_name (unsigned op)
12874 {
12875 switch (op)
12876 {
12877 case DW_OP_addr:
12878 return "DW_OP_addr";
12879 case DW_OP_deref:
12880 return "DW_OP_deref";
12881 case DW_OP_const1u:
12882 return "DW_OP_const1u";
12883 case DW_OP_const1s:
12884 return "DW_OP_const1s";
12885 case DW_OP_const2u:
12886 return "DW_OP_const2u";
12887 case DW_OP_const2s:
12888 return "DW_OP_const2s";
12889 case DW_OP_const4u:
12890 return "DW_OP_const4u";
12891 case DW_OP_const4s:
12892 return "DW_OP_const4s";
12893 case DW_OP_const8u:
12894 return "DW_OP_const8u";
12895 case DW_OP_const8s:
12896 return "DW_OP_const8s";
12897 case DW_OP_constu:
12898 return "DW_OP_constu";
12899 case DW_OP_consts:
12900 return "DW_OP_consts";
12901 case DW_OP_dup:
12902 return "DW_OP_dup";
12903 case DW_OP_drop:
12904 return "DW_OP_drop";
12905 case DW_OP_over:
12906 return "DW_OP_over";
12907 case DW_OP_pick:
12908 return "DW_OP_pick";
12909 case DW_OP_swap:
12910 return "DW_OP_swap";
12911 case DW_OP_rot:
12912 return "DW_OP_rot";
12913 case DW_OP_xderef:
12914 return "DW_OP_xderef";
12915 case DW_OP_abs:
12916 return "DW_OP_abs";
12917 case DW_OP_and:
12918 return "DW_OP_and";
12919 case DW_OP_div:
12920 return "DW_OP_div";
12921 case DW_OP_minus:
12922 return "DW_OP_minus";
12923 case DW_OP_mod:
12924 return "DW_OP_mod";
12925 case DW_OP_mul:
12926 return "DW_OP_mul";
12927 case DW_OP_neg:
12928 return "DW_OP_neg";
12929 case DW_OP_not:
12930 return "DW_OP_not";
12931 case DW_OP_or:
12932 return "DW_OP_or";
12933 case DW_OP_plus:
12934 return "DW_OP_plus";
12935 case DW_OP_plus_uconst:
12936 return "DW_OP_plus_uconst";
12937 case DW_OP_shl:
12938 return "DW_OP_shl";
12939 case DW_OP_shr:
12940 return "DW_OP_shr";
12941 case DW_OP_shra:
12942 return "DW_OP_shra";
12943 case DW_OP_xor:
12944 return "DW_OP_xor";
12945 case DW_OP_bra:
12946 return "DW_OP_bra";
12947 case DW_OP_eq:
12948 return "DW_OP_eq";
12949 case DW_OP_ge:
12950 return "DW_OP_ge";
12951 case DW_OP_gt:
12952 return "DW_OP_gt";
12953 case DW_OP_le:
12954 return "DW_OP_le";
12955 case DW_OP_lt:
12956 return "DW_OP_lt";
12957 case DW_OP_ne:
12958 return "DW_OP_ne";
12959 case DW_OP_skip:
12960 return "DW_OP_skip";
12961 case DW_OP_lit0:
12962 return "DW_OP_lit0";
12963 case DW_OP_lit1:
12964 return "DW_OP_lit1";
12965 case DW_OP_lit2:
12966 return "DW_OP_lit2";
12967 case DW_OP_lit3:
12968 return "DW_OP_lit3";
12969 case DW_OP_lit4:
12970 return "DW_OP_lit4";
12971 case DW_OP_lit5:
12972 return "DW_OP_lit5";
12973 case DW_OP_lit6:
12974 return "DW_OP_lit6";
12975 case DW_OP_lit7:
12976 return "DW_OP_lit7";
12977 case DW_OP_lit8:
12978 return "DW_OP_lit8";
12979 case DW_OP_lit9:
12980 return "DW_OP_lit9";
12981 case DW_OP_lit10:
12982 return "DW_OP_lit10";
12983 case DW_OP_lit11:
12984 return "DW_OP_lit11";
12985 case DW_OP_lit12:
12986 return "DW_OP_lit12";
12987 case DW_OP_lit13:
12988 return "DW_OP_lit13";
12989 case DW_OP_lit14:
12990 return "DW_OP_lit14";
12991 case DW_OP_lit15:
12992 return "DW_OP_lit15";
12993 case DW_OP_lit16:
12994 return "DW_OP_lit16";
12995 case DW_OP_lit17:
12996 return "DW_OP_lit17";
12997 case DW_OP_lit18:
12998 return "DW_OP_lit18";
12999 case DW_OP_lit19:
13000 return "DW_OP_lit19";
13001 case DW_OP_lit20:
13002 return "DW_OP_lit20";
13003 case DW_OP_lit21:
13004 return "DW_OP_lit21";
13005 case DW_OP_lit22:
13006 return "DW_OP_lit22";
13007 case DW_OP_lit23:
13008 return "DW_OP_lit23";
13009 case DW_OP_lit24:
13010 return "DW_OP_lit24";
13011 case DW_OP_lit25:
13012 return "DW_OP_lit25";
13013 case DW_OP_lit26:
13014 return "DW_OP_lit26";
13015 case DW_OP_lit27:
13016 return "DW_OP_lit27";
13017 case DW_OP_lit28:
13018 return "DW_OP_lit28";
13019 case DW_OP_lit29:
13020 return "DW_OP_lit29";
13021 case DW_OP_lit30:
13022 return "DW_OP_lit30";
13023 case DW_OP_lit31:
13024 return "DW_OP_lit31";
13025 case DW_OP_reg0:
13026 return "DW_OP_reg0";
13027 case DW_OP_reg1:
13028 return "DW_OP_reg1";
13029 case DW_OP_reg2:
13030 return "DW_OP_reg2";
13031 case DW_OP_reg3:
13032 return "DW_OP_reg3";
13033 case DW_OP_reg4:
13034 return "DW_OP_reg4";
13035 case DW_OP_reg5:
13036 return "DW_OP_reg5";
13037 case DW_OP_reg6:
13038 return "DW_OP_reg6";
13039 case DW_OP_reg7:
13040 return "DW_OP_reg7";
13041 case DW_OP_reg8:
13042 return "DW_OP_reg8";
13043 case DW_OP_reg9:
13044 return "DW_OP_reg9";
13045 case DW_OP_reg10:
13046 return "DW_OP_reg10";
13047 case DW_OP_reg11:
13048 return "DW_OP_reg11";
13049 case DW_OP_reg12:
13050 return "DW_OP_reg12";
13051 case DW_OP_reg13:
13052 return "DW_OP_reg13";
13053 case DW_OP_reg14:
13054 return "DW_OP_reg14";
13055 case DW_OP_reg15:
13056 return "DW_OP_reg15";
13057 case DW_OP_reg16:
13058 return "DW_OP_reg16";
13059 case DW_OP_reg17:
13060 return "DW_OP_reg17";
13061 case DW_OP_reg18:
13062 return "DW_OP_reg18";
13063 case DW_OP_reg19:
13064 return "DW_OP_reg19";
13065 case DW_OP_reg20:
13066 return "DW_OP_reg20";
13067 case DW_OP_reg21:
13068 return "DW_OP_reg21";
13069 case DW_OP_reg22:
13070 return "DW_OP_reg22";
13071 case DW_OP_reg23:
13072 return "DW_OP_reg23";
13073 case DW_OP_reg24:
13074 return "DW_OP_reg24";
13075 case DW_OP_reg25:
13076 return "DW_OP_reg25";
13077 case DW_OP_reg26:
13078 return "DW_OP_reg26";
13079 case DW_OP_reg27:
13080 return "DW_OP_reg27";
13081 case DW_OP_reg28:
13082 return "DW_OP_reg28";
13083 case DW_OP_reg29:
13084 return "DW_OP_reg29";
13085 case DW_OP_reg30:
13086 return "DW_OP_reg30";
13087 case DW_OP_reg31:
13088 return "DW_OP_reg31";
13089 case DW_OP_breg0:
13090 return "DW_OP_breg0";
13091 case DW_OP_breg1:
13092 return "DW_OP_breg1";
13093 case DW_OP_breg2:
13094 return "DW_OP_breg2";
13095 case DW_OP_breg3:
13096 return "DW_OP_breg3";
13097 case DW_OP_breg4:
13098 return "DW_OP_breg4";
13099 case DW_OP_breg5:
13100 return "DW_OP_breg5";
13101 case DW_OP_breg6:
13102 return "DW_OP_breg6";
13103 case DW_OP_breg7:
13104 return "DW_OP_breg7";
13105 case DW_OP_breg8:
13106 return "DW_OP_breg8";
13107 case DW_OP_breg9:
13108 return "DW_OP_breg9";
13109 case DW_OP_breg10:
13110 return "DW_OP_breg10";
13111 case DW_OP_breg11:
13112 return "DW_OP_breg11";
13113 case DW_OP_breg12:
13114 return "DW_OP_breg12";
13115 case DW_OP_breg13:
13116 return "DW_OP_breg13";
13117 case DW_OP_breg14:
13118 return "DW_OP_breg14";
13119 case DW_OP_breg15:
13120 return "DW_OP_breg15";
13121 case DW_OP_breg16:
13122 return "DW_OP_breg16";
13123 case DW_OP_breg17:
13124 return "DW_OP_breg17";
13125 case DW_OP_breg18:
13126 return "DW_OP_breg18";
13127 case DW_OP_breg19:
13128 return "DW_OP_breg19";
13129 case DW_OP_breg20:
13130 return "DW_OP_breg20";
13131 case DW_OP_breg21:
13132 return "DW_OP_breg21";
13133 case DW_OP_breg22:
13134 return "DW_OP_breg22";
13135 case DW_OP_breg23:
13136 return "DW_OP_breg23";
13137 case DW_OP_breg24:
13138 return "DW_OP_breg24";
13139 case DW_OP_breg25:
13140 return "DW_OP_breg25";
13141 case DW_OP_breg26:
13142 return "DW_OP_breg26";
13143 case DW_OP_breg27:
13144 return "DW_OP_breg27";
13145 case DW_OP_breg28:
13146 return "DW_OP_breg28";
13147 case DW_OP_breg29:
13148 return "DW_OP_breg29";
13149 case DW_OP_breg30:
13150 return "DW_OP_breg30";
13151 case DW_OP_breg31:
13152 return "DW_OP_breg31";
13153 case DW_OP_regx:
13154 return "DW_OP_regx";
13155 case DW_OP_fbreg:
13156 return "DW_OP_fbreg";
13157 case DW_OP_bregx:
13158 return "DW_OP_bregx";
13159 case DW_OP_piece:
13160 return "DW_OP_piece";
13161 case DW_OP_deref_size:
13162 return "DW_OP_deref_size";
13163 case DW_OP_xderef_size:
13164 return "DW_OP_xderef_size";
13165 case DW_OP_nop:
13166 return "DW_OP_nop";
13167 /* DWARF 3 extensions. */
13168 case DW_OP_push_object_address:
13169 return "DW_OP_push_object_address";
13170 case DW_OP_call2:
13171 return "DW_OP_call2";
13172 case DW_OP_call4:
13173 return "DW_OP_call4";
13174 case DW_OP_call_ref:
13175 return "DW_OP_call_ref";
13176 case DW_OP_form_tls_address:
13177 return "DW_OP_form_tls_address";
13178 case DW_OP_call_frame_cfa:
13179 return "DW_OP_call_frame_cfa";
13180 case DW_OP_bit_piece:
13181 return "DW_OP_bit_piece";
13182 /* DWARF 4 extensions. */
13183 case DW_OP_implicit_value:
13184 return "DW_OP_implicit_value";
13185 case DW_OP_stack_value:
13186 return "DW_OP_stack_value";
13187 /* GNU extensions. */
13188 case DW_OP_GNU_push_tls_address:
13189 return "DW_OP_GNU_push_tls_address";
13190 case DW_OP_GNU_uninit:
13191 return "DW_OP_GNU_uninit";
13192 case DW_OP_GNU_implicit_pointer:
13193 return "DW_OP_GNU_implicit_pointer";
13194 case DW_OP_GNU_entry_value:
13195 return "DW_OP_GNU_entry_value";
13196 case DW_OP_GNU_const_type:
13197 return "DW_OP_GNU_const_type";
13198 case DW_OP_GNU_regval_type:
13199 return "DW_OP_GNU_regval_type";
13200 case DW_OP_GNU_deref_type:
13201 return "DW_OP_GNU_deref_type";
13202 case DW_OP_GNU_convert:
13203 return "DW_OP_GNU_convert";
13204 case DW_OP_GNU_reinterpret:
13205 return "DW_OP_GNU_reinterpret";
13206 default:
13207 return NULL;
13208 }
13209 }
13210
13211 static char *
13212 dwarf_bool_name (unsigned mybool)
13213 {
13214 if (mybool)
13215 return "TRUE";
13216 else
13217 return "FALSE";
13218 }
13219
13220 /* Convert a DWARF type code into its string name. */
13221
13222 static char *
13223 dwarf_type_encoding_name (unsigned enc)
13224 {
13225 switch (enc)
13226 {
13227 case DW_ATE_void:
13228 return "DW_ATE_void";
13229 case DW_ATE_address:
13230 return "DW_ATE_address";
13231 case DW_ATE_boolean:
13232 return "DW_ATE_boolean";
13233 case DW_ATE_complex_float:
13234 return "DW_ATE_complex_float";
13235 case DW_ATE_float:
13236 return "DW_ATE_float";
13237 case DW_ATE_signed:
13238 return "DW_ATE_signed";
13239 case DW_ATE_signed_char:
13240 return "DW_ATE_signed_char";
13241 case DW_ATE_unsigned:
13242 return "DW_ATE_unsigned";
13243 case DW_ATE_unsigned_char:
13244 return "DW_ATE_unsigned_char";
13245 /* DWARF 3. */
13246 case DW_ATE_imaginary_float:
13247 return "DW_ATE_imaginary_float";
13248 case DW_ATE_packed_decimal:
13249 return "DW_ATE_packed_decimal";
13250 case DW_ATE_numeric_string:
13251 return "DW_ATE_numeric_string";
13252 case DW_ATE_edited:
13253 return "DW_ATE_edited";
13254 case DW_ATE_signed_fixed:
13255 return "DW_ATE_signed_fixed";
13256 case DW_ATE_unsigned_fixed:
13257 return "DW_ATE_unsigned_fixed";
13258 case DW_ATE_decimal_float:
13259 return "DW_ATE_decimal_float";
13260 /* DWARF 4. */
13261 case DW_ATE_UTF:
13262 return "DW_ATE_UTF";
13263 /* HP extensions. */
13264 case DW_ATE_HP_float80:
13265 return "DW_ATE_HP_float80";
13266 case DW_ATE_HP_complex_float80:
13267 return "DW_ATE_HP_complex_float80";
13268 case DW_ATE_HP_float128:
13269 return "DW_ATE_HP_float128";
13270 case DW_ATE_HP_complex_float128:
13271 return "DW_ATE_HP_complex_float128";
13272 case DW_ATE_HP_floathpintel:
13273 return "DW_ATE_HP_floathpintel";
13274 case DW_ATE_HP_imaginary_float80:
13275 return "DW_ATE_HP_imaginary_float80";
13276 case DW_ATE_HP_imaginary_float128:
13277 return "DW_ATE_HP_imaginary_float128";
13278 default:
13279 return "DW_ATE_<unknown>";
13280 }
13281 }
13282
13283 /* Convert a DWARF call frame info operation to its string name. */
13284
13285 #if 0
13286 static char *
13287 dwarf_cfi_name (unsigned cfi_opc)
13288 {
13289 switch (cfi_opc)
13290 {
13291 case DW_CFA_advance_loc:
13292 return "DW_CFA_advance_loc";
13293 case DW_CFA_offset:
13294 return "DW_CFA_offset";
13295 case DW_CFA_restore:
13296 return "DW_CFA_restore";
13297 case DW_CFA_nop:
13298 return "DW_CFA_nop";
13299 case DW_CFA_set_loc:
13300 return "DW_CFA_set_loc";
13301 case DW_CFA_advance_loc1:
13302 return "DW_CFA_advance_loc1";
13303 case DW_CFA_advance_loc2:
13304 return "DW_CFA_advance_loc2";
13305 case DW_CFA_advance_loc4:
13306 return "DW_CFA_advance_loc4";
13307 case DW_CFA_offset_extended:
13308 return "DW_CFA_offset_extended";
13309 case DW_CFA_restore_extended:
13310 return "DW_CFA_restore_extended";
13311 case DW_CFA_undefined:
13312 return "DW_CFA_undefined";
13313 case DW_CFA_same_value:
13314 return "DW_CFA_same_value";
13315 case DW_CFA_register:
13316 return "DW_CFA_register";
13317 case DW_CFA_remember_state:
13318 return "DW_CFA_remember_state";
13319 case DW_CFA_restore_state:
13320 return "DW_CFA_restore_state";
13321 case DW_CFA_def_cfa:
13322 return "DW_CFA_def_cfa";
13323 case DW_CFA_def_cfa_register:
13324 return "DW_CFA_def_cfa_register";
13325 case DW_CFA_def_cfa_offset:
13326 return "DW_CFA_def_cfa_offset";
13327 /* DWARF 3. */
13328 case DW_CFA_def_cfa_expression:
13329 return "DW_CFA_def_cfa_expression";
13330 case DW_CFA_expression:
13331 return "DW_CFA_expression";
13332 case DW_CFA_offset_extended_sf:
13333 return "DW_CFA_offset_extended_sf";
13334 case DW_CFA_def_cfa_sf:
13335 return "DW_CFA_def_cfa_sf";
13336 case DW_CFA_def_cfa_offset_sf:
13337 return "DW_CFA_def_cfa_offset_sf";
13338 case DW_CFA_val_offset:
13339 return "DW_CFA_val_offset";
13340 case DW_CFA_val_offset_sf:
13341 return "DW_CFA_val_offset_sf";
13342 case DW_CFA_val_expression:
13343 return "DW_CFA_val_expression";
13344 /* SGI/MIPS specific. */
13345 case DW_CFA_MIPS_advance_loc8:
13346 return "DW_CFA_MIPS_advance_loc8";
13347 /* GNU extensions. */
13348 case DW_CFA_GNU_window_save:
13349 return "DW_CFA_GNU_window_save";
13350 case DW_CFA_GNU_args_size:
13351 return "DW_CFA_GNU_args_size";
13352 case DW_CFA_GNU_negative_offset_extended:
13353 return "DW_CFA_GNU_negative_offset_extended";
13354 default:
13355 return "DW_CFA_<unknown>";
13356 }
13357 }
13358 #endif
13359
13360 static void
13361 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13362 {
13363 unsigned int i;
13364
13365 print_spaces (indent, f);
13366 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13367 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13368
13369 if (die->parent != NULL)
13370 {
13371 print_spaces (indent, f);
13372 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13373 die->parent->offset);
13374 }
13375
13376 print_spaces (indent, f);
13377 fprintf_unfiltered (f, " has children: %s\n",
13378 dwarf_bool_name (die->child != NULL));
13379
13380 print_spaces (indent, f);
13381 fprintf_unfiltered (f, " attributes:\n");
13382
13383 for (i = 0; i < die->num_attrs; ++i)
13384 {
13385 print_spaces (indent, f);
13386 fprintf_unfiltered (f, " %s (%s) ",
13387 dwarf_attr_name (die->attrs[i].name),
13388 dwarf_form_name (die->attrs[i].form));
13389
13390 switch (die->attrs[i].form)
13391 {
13392 case DW_FORM_ref_addr:
13393 case DW_FORM_addr:
13394 fprintf_unfiltered (f, "address: ");
13395 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13396 break;
13397 case DW_FORM_block2:
13398 case DW_FORM_block4:
13399 case DW_FORM_block:
13400 case DW_FORM_block1:
13401 fprintf_unfiltered (f, "block: size %d",
13402 DW_BLOCK (&die->attrs[i])->size);
13403 break;
13404 case DW_FORM_exprloc:
13405 fprintf_unfiltered (f, "expression: size %u",
13406 DW_BLOCK (&die->attrs[i])->size);
13407 break;
13408 case DW_FORM_ref1:
13409 case DW_FORM_ref2:
13410 case DW_FORM_ref4:
13411 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13412 (long) (DW_ADDR (&die->attrs[i])));
13413 break;
13414 case DW_FORM_data1:
13415 case DW_FORM_data2:
13416 case DW_FORM_data4:
13417 case DW_FORM_data8:
13418 case DW_FORM_udata:
13419 case DW_FORM_sdata:
13420 fprintf_unfiltered (f, "constant: %s",
13421 pulongest (DW_UNSND (&die->attrs[i])));
13422 break;
13423 case DW_FORM_sec_offset:
13424 fprintf_unfiltered (f, "section offset: %s",
13425 pulongest (DW_UNSND (&die->attrs[i])));
13426 break;
13427 case DW_FORM_ref_sig8:
13428 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13429 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13430 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13431 else
13432 fprintf_unfiltered (f, "signatured type, offset: unknown");
13433 break;
13434 case DW_FORM_string:
13435 case DW_FORM_strp:
13436 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13437 DW_STRING (&die->attrs[i])
13438 ? DW_STRING (&die->attrs[i]) : "",
13439 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13440 break;
13441 case DW_FORM_flag:
13442 if (DW_UNSND (&die->attrs[i]))
13443 fprintf_unfiltered (f, "flag: TRUE");
13444 else
13445 fprintf_unfiltered (f, "flag: FALSE");
13446 break;
13447 case DW_FORM_flag_present:
13448 fprintf_unfiltered (f, "flag: TRUE");
13449 break;
13450 case DW_FORM_indirect:
13451 /* The reader will have reduced the indirect form to
13452 the "base form" so this form should not occur. */
13453 fprintf_unfiltered (f,
13454 "unexpected attribute form: DW_FORM_indirect");
13455 break;
13456 default:
13457 fprintf_unfiltered (f, "unsupported attribute form: %d.",
13458 die->attrs[i].form);
13459 break;
13460 }
13461 fprintf_unfiltered (f, "\n");
13462 }
13463 }
13464
13465 static void
13466 dump_die_for_error (struct die_info *die)
13467 {
13468 dump_die_shallow (gdb_stderr, 0, die);
13469 }
13470
13471 static void
13472 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
13473 {
13474 int indent = level * 4;
13475
13476 gdb_assert (die != NULL);
13477
13478 if (level >= max_level)
13479 return;
13480
13481 dump_die_shallow (f, indent, die);
13482
13483 if (die->child != NULL)
13484 {
13485 print_spaces (indent, f);
13486 fprintf_unfiltered (f, " Children:");
13487 if (level + 1 < max_level)
13488 {
13489 fprintf_unfiltered (f, "\n");
13490 dump_die_1 (f, level + 1, max_level, die->child);
13491 }
13492 else
13493 {
13494 fprintf_unfiltered (f,
13495 " [not printed, max nesting level reached]\n");
13496 }
13497 }
13498
13499 if (die->sibling != NULL && level > 0)
13500 {
13501 dump_die_1 (f, level, max_level, die->sibling);
13502 }
13503 }
13504
13505 /* This is called from the pdie macro in gdbinit.in.
13506 It's not static so gcc will keep a copy callable from gdb. */
13507
13508 void
13509 dump_die (struct die_info *die, int max_level)
13510 {
13511 dump_die_1 (gdb_stdlog, 0, max_level, die);
13512 }
13513
13514 static void
13515 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
13516 {
13517 void **slot;
13518
13519 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
13520
13521 *slot = die;
13522 }
13523
13524 static int
13525 is_ref_attr (struct attribute *attr)
13526 {
13527 switch (attr->form)
13528 {
13529 case DW_FORM_ref_addr:
13530 case DW_FORM_ref1:
13531 case DW_FORM_ref2:
13532 case DW_FORM_ref4:
13533 case DW_FORM_ref8:
13534 case DW_FORM_ref_udata:
13535 return 1;
13536 default:
13537 return 0;
13538 }
13539 }
13540
13541 static unsigned int
13542 dwarf2_get_ref_die_offset (struct attribute *attr)
13543 {
13544 if (is_ref_attr (attr))
13545 return DW_ADDR (attr);
13546
13547 complaint (&symfile_complaints,
13548 _("unsupported die ref attribute form: '%s'"),
13549 dwarf_form_name (attr->form));
13550 return 0;
13551 }
13552
13553 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
13554 * the value held by the attribute is not constant. */
13555
13556 static LONGEST
13557 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
13558 {
13559 if (attr->form == DW_FORM_sdata)
13560 return DW_SND (attr);
13561 else if (attr->form == DW_FORM_udata
13562 || attr->form == DW_FORM_data1
13563 || attr->form == DW_FORM_data2
13564 || attr->form == DW_FORM_data4
13565 || attr->form == DW_FORM_data8)
13566 return DW_UNSND (attr);
13567 else
13568 {
13569 complaint (&symfile_complaints,
13570 _("Attribute value is not a constant (%s)"),
13571 dwarf_form_name (attr->form));
13572 return default_value;
13573 }
13574 }
13575
13576 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
13577 unit and add it to our queue.
13578 The result is non-zero if PER_CU was queued, otherwise the result is zero
13579 meaning either PER_CU is already queued or it is already loaded. */
13580
13581 static int
13582 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
13583 struct dwarf2_per_cu_data *per_cu)
13584 {
13585 /* We may arrive here during partial symbol reading, if we need full
13586 DIEs to process an unusual case (e.g. template arguments). Do
13587 not queue PER_CU, just tell our caller to load its DIEs. */
13588 if (dwarf2_per_objfile->reading_partial_symbols)
13589 {
13590 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
13591 return 1;
13592 return 0;
13593 }
13594
13595 /* Mark the dependence relation so that we don't flush PER_CU
13596 too early. */
13597 dwarf2_add_dependence (this_cu, per_cu);
13598
13599 /* If it's already on the queue, we have nothing to do. */
13600 if (per_cu->queued)
13601 return 0;
13602
13603 /* If the compilation unit is already loaded, just mark it as
13604 used. */
13605 if (per_cu->cu != NULL)
13606 {
13607 per_cu->cu->last_used = 0;
13608 return 0;
13609 }
13610
13611 /* Add it to the queue. */
13612 queue_comp_unit (per_cu, this_cu->objfile);
13613
13614 return 1;
13615 }
13616
13617 /* Follow reference or signature attribute ATTR of SRC_DIE.
13618 On entry *REF_CU is the CU of SRC_DIE.
13619 On exit *REF_CU is the CU of the result. */
13620
13621 static struct die_info *
13622 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
13623 struct dwarf2_cu **ref_cu)
13624 {
13625 struct die_info *die;
13626
13627 if (is_ref_attr (attr))
13628 die = follow_die_ref (src_die, attr, ref_cu);
13629 else if (attr->form == DW_FORM_ref_sig8)
13630 die = follow_die_sig (src_die, attr, ref_cu);
13631 else
13632 {
13633 dump_die_for_error (src_die);
13634 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
13635 (*ref_cu)->objfile->name);
13636 }
13637
13638 return die;
13639 }
13640
13641 /* Follow reference OFFSET.
13642 On entry *REF_CU is the CU of the source die referencing OFFSET.
13643 On exit *REF_CU is the CU of the result.
13644 Returns NULL if OFFSET is invalid. */
13645
13646 static struct die_info *
13647 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
13648 {
13649 struct die_info temp_die;
13650 struct dwarf2_cu *target_cu, *cu = *ref_cu;
13651
13652 gdb_assert (cu->per_cu != NULL);
13653
13654 target_cu = cu;
13655
13656 if (cu->per_cu->from_debug_types)
13657 {
13658 /* .debug_types CUs cannot reference anything outside their CU.
13659 If they need to, they have to reference a signatured type via
13660 DW_FORM_ref_sig8. */
13661 if (! offset_in_cu_p (&cu->header, offset))
13662 return NULL;
13663 }
13664 else if (! offset_in_cu_p (&cu->header, offset))
13665 {
13666 struct dwarf2_per_cu_data *per_cu;
13667
13668 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
13669
13670 /* If necessary, add it to the queue and load its DIEs. */
13671 if (maybe_queue_comp_unit (cu, per_cu))
13672 load_full_comp_unit (per_cu, cu->objfile);
13673
13674 target_cu = per_cu->cu;
13675 }
13676 else if (cu->dies == NULL)
13677 {
13678 /* We're loading full DIEs during partial symbol reading. */
13679 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
13680 load_full_comp_unit (cu->per_cu, cu->objfile);
13681 }
13682
13683 *ref_cu = target_cu;
13684 temp_die.offset = offset;
13685 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
13686 }
13687
13688 /* Follow reference attribute ATTR of SRC_DIE.
13689 On entry *REF_CU is the CU of SRC_DIE.
13690 On exit *REF_CU is the CU of the result. */
13691
13692 static struct die_info *
13693 follow_die_ref (struct die_info *src_die, struct attribute *attr,
13694 struct dwarf2_cu **ref_cu)
13695 {
13696 unsigned int offset = dwarf2_get_ref_die_offset (attr);
13697 struct dwarf2_cu *cu = *ref_cu;
13698 struct die_info *die;
13699
13700 die = follow_die_offset (offset, ref_cu);
13701 if (!die)
13702 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
13703 "at 0x%x [in module %s]"),
13704 offset, src_die->offset, cu->objfile->name);
13705
13706 return die;
13707 }
13708
13709 /* Return DWARF block and its CU referenced by OFFSET at PER_CU. Returned
13710 value is intended for DW_OP_call*. */
13711
13712 struct dwarf2_locexpr_baton
13713 dwarf2_fetch_die_location_block (unsigned int offset,
13714 struct dwarf2_per_cu_data *per_cu,
13715 CORE_ADDR (*get_frame_pc) (void *baton),
13716 void *baton)
13717 {
13718 struct dwarf2_cu *cu = per_cu->cu;
13719 struct die_info *die;
13720 struct attribute *attr;
13721 struct dwarf2_locexpr_baton retval;
13722
13723 dw2_setup (per_cu->objfile);
13724
13725 die = follow_die_offset (offset, &cu);
13726 if (!die)
13727 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
13728 offset, per_cu->cu->objfile->name);
13729
13730 attr = dwarf2_attr (die, DW_AT_location, cu);
13731 if (!attr)
13732 {
13733 /* DWARF: "If there is no such attribute, then there is no effect.". */
13734
13735 retval.data = NULL;
13736 retval.size = 0;
13737 }
13738 else if (attr_form_is_section_offset (attr))
13739 {
13740 struct dwarf2_loclist_baton loclist_baton;
13741 CORE_ADDR pc = (*get_frame_pc) (baton);
13742 size_t size;
13743
13744 fill_in_loclist_baton (cu, &loclist_baton, attr);
13745
13746 retval.data = dwarf2_find_location_expression (&loclist_baton,
13747 &size, pc);
13748 retval.size = size;
13749 }
13750 else
13751 {
13752 if (!attr_form_is_block (attr))
13753 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
13754 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
13755 offset, per_cu->cu->objfile->name);
13756
13757 retval.data = DW_BLOCK (attr)->data;
13758 retval.size = DW_BLOCK (attr)->size;
13759 }
13760 retval.per_cu = cu->per_cu;
13761 return retval;
13762 }
13763
13764 /* Return the type of the DIE at DIE_OFFSET in the CU named by
13765 PER_CU. */
13766
13767 struct type *
13768 dwarf2_get_die_type (unsigned int die_offset,
13769 struct dwarf2_per_cu_data *per_cu)
13770 {
13771 dw2_setup (per_cu->objfile);
13772 return get_die_type_at_offset (die_offset, per_cu);
13773 }
13774
13775 /* Follow the signature attribute ATTR in SRC_DIE.
13776 On entry *REF_CU is the CU of SRC_DIE.
13777 On exit *REF_CU is the CU of the result. */
13778
13779 static struct die_info *
13780 follow_die_sig (struct die_info *src_die, struct attribute *attr,
13781 struct dwarf2_cu **ref_cu)
13782 {
13783 struct objfile *objfile = (*ref_cu)->objfile;
13784 struct die_info temp_die;
13785 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
13786 struct dwarf2_cu *sig_cu;
13787 struct die_info *die;
13788
13789 /* sig_type will be NULL if the signatured type is missing from
13790 the debug info. */
13791 if (sig_type == NULL)
13792 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
13793 "at 0x%x [in module %s]"),
13794 src_die->offset, objfile->name);
13795
13796 /* If necessary, add it to the queue and load its DIEs. */
13797
13798 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
13799 read_signatured_type (objfile, sig_type);
13800
13801 gdb_assert (sig_type->per_cu.cu != NULL);
13802
13803 sig_cu = sig_type->per_cu.cu;
13804 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
13805 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
13806 if (die)
13807 {
13808 *ref_cu = sig_cu;
13809 return die;
13810 }
13811
13812 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
13813 "from DIE at 0x%x [in module %s]"),
13814 sig_type->type_offset, src_die->offset, objfile->name);
13815 }
13816
13817 /* Given an offset of a signatured type, return its signatured_type. */
13818
13819 static struct signatured_type *
13820 lookup_signatured_type_at_offset (struct objfile *objfile, unsigned int offset)
13821 {
13822 gdb_byte *info_ptr = dwarf2_per_objfile->types.buffer + offset;
13823 unsigned int length, initial_length_size;
13824 unsigned int sig_offset;
13825 struct signatured_type find_entry, *type_sig;
13826
13827 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
13828 sig_offset = (initial_length_size
13829 + 2 /*version*/
13830 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
13831 + 1 /*address_size*/);
13832 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
13833 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
13834
13835 /* This is only used to lookup previously recorded types.
13836 If we didn't find it, it's our bug. */
13837 gdb_assert (type_sig != NULL);
13838 gdb_assert (offset == type_sig->per_cu.offset);
13839
13840 return type_sig;
13841 }
13842
13843 /* Read in signatured type at OFFSET and build its CU and die(s). */
13844
13845 static void
13846 read_signatured_type_at_offset (struct objfile *objfile,
13847 unsigned int offset)
13848 {
13849 struct signatured_type *type_sig;
13850
13851 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13852
13853 /* We have the section offset, but we need the signature to do the
13854 hash table lookup. */
13855 type_sig = lookup_signatured_type_at_offset (objfile, offset);
13856
13857 gdb_assert (type_sig->per_cu.cu == NULL);
13858
13859 read_signatured_type (objfile, type_sig);
13860
13861 gdb_assert (type_sig->per_cu.cu != NULL);
13862 }
13863
13864 /* Read in a signatured type and build its CU and DIEs. */
13865
13866 static void
13867 read_signatured_type (struct objfile *objfile,
13868 struct signatured_type *type_sig)
13869 {
13870 gdb_byte *types_ptr;
13871 struct die_reader_specs reader_specs;
13872 struct dwarf2_cu *cu;
13873 ULONGEST signature;
13874 struct cleanup *back_to, *free_cu_cleanup;
13875
13876 dwarf2_read_section (objfile, &dwarf2_per_objfile->types);
13877 types_ptr = dwarf2_per_objfile->types.buffer + type_sig->per_cu.offset;
13878
13879 gdb_assert (type_sig->per_cu.cu == NULL);
13880
13881 cu = xmalloc (sizeof (*cu));
13882 init_one_comp_unit (cu, objfile);
13883
13884 type_sig->per_cu.cu = cu;
13885 cu->per_cu = &type_sig->per_cu;
13886
13887 /* If an error occurs while loading, release our storage. */
13888 free_cu_cleanup = make_cleanup (free_one_comp_unit, cu);
13889
13890 types_ptr = read_type_comp_unit_head (&cu->header, &signature,
13891 types_ptr, objfile->obfd);
13892 gdb_assert (signature == type_sig->signature);
13893
13894 cu->die_hash
13895 = htab_create_alloc_ex (cu->header.length / 12,
13896 die_hash,
13897 die_eq,
13898 NULL,
13899 &cu->comp_unit_obstack,
13900 hashtab_obstack_allocate,
13901 dummy_obstack_deallocate);
13902
13903 dwarf2_read_abbrevs (cu->objfile->obfd, cu);
13904 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
13905
13906 init_cu_die_reader (&reader_specs, cu);
13907
13908 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
13909 NULL /*parent*/);
13910
13911 /* We try not to read any attributes in this function, because not
13912 all objfiles needed for references have been loaded yet, and symbol
13913 table processing isn't initialized. But we have to set the CU language,
13914 or we won't be able to build types correctly. */
13915 prepare_one_comp_unit (cu, cu->dies);
13916
13917 do_cleanups (back_to);
13918
13919 /* We've successfully allocated this compilation unit. Let our caller
13920 clean it up when finished with it. */
13921 discard_cleanups (free_cu_cleanup);
13922
13923 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
13924 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
13925 }
13926
13927 /* Decode simple location descriptions.
13928 Given a pointer to a dwarf block that defines a location, compute
13929 the location and return the value.
13930
13931 NOTE drow/2003-11-18: This function is called in two situations
13932 now: for the address of static or global variables (partial symbols
13933 only) and for offsets into structures which are expected to be
13934 (more or less) constant. The partial symbol case should go away,
13935 and only the constant case should remain. That will let this
13936 function complain more accurately. A few special modes are allowed
13937 without complaint for global variables (for instance, global
13938 register values and thread-local values).
13939
13940 A location description containing no operations indicates that the
13941 object is optimized out. The return value is 0 for that case.
13942 FIXME drow/2003-11-16: No callers check for this case any more; soon all
13943 callers will only want a very basic result and this can become a
13944 complaint.
13945
13946 Note that stack[0] is unused except as a default error return. */
13947
13948 static CORE_ADDR
13949 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
13950 {
13951 struct objfile *objfile = cu->objfile;
13952 int i;
13953 int size = blk->size;
13954 gdb_byte *data = blk->data;
13955 CORE_ADDR stack[64];
13956 int stacki;
13957 unsigned int bytes_read, unsnd;
13958 gdb_byte op;
13959
13960 i = 0;
13961 stacki = 0;
13962 stack[stacki] = 0;
13963 stack[++stacki] = 0;
13964
13965 while (i < size)
13966 {
13967 op = data[i++];
13968 switch (op)
13969 {
13970 case DW_OP_lit0:
13971 case DW_OP_lit1:
13972 case DW_OP_lit2:
13973 case DW_OP_lit3:
13974 case DW_OP_lit4:
13975 case DW_OP_lit5:
13976 case DW_OP_lit6:
13977 case DW_OP_lit7:
13978 case DW_OP_lit8:
13979 case DW_OP_lit9:
13980 case DW_OP_lit10:
13981 case DW_OP_lit11:
13982 case DW_OP_lit12:
13983 case DW_OP_lit13:
13984 case DW_OP_lit14:
13985 case DW_OP_lit15:
13986 case DW_OP_lit16:
13987 case DW_OP_lit17:
13988 case DW_OP_lit18:
13989 case DW_OP_lit19:
13990 case DW_OP_lit20:
13991 case DW_OP_lit21:
13992 case DW_OP_lit22:
13993 case DW_OP_lit23:
13994 case DW_OP_lit24:
13995 case DW_OP_lit25:
13996 case DW_OP_lit26:
13997 case DW_OP_lit27:
13998 case DW_OP_lit28:
13999 case DW_OP_lit29:
14000 case DW_OP_lit30:
14001 case DW_OP_lit31:
14002 stack[++stacki] = op - DW_OP_lit0;
14003 break;
14004
14005 case DW_OP_reg0:
14006 case DW_OP_reg1:
14007 case DW_OP_reg2:
14008 case DW_OP_reg3:
14009 case DW_OP_reg4:
14010 case DW_OP_reg5:
14011 case DW_OP_reg6:
14012 case DW_OP_reg7:
14013 case DW_OP_reg8:
14014 case DW_OP_reg9:
14015 case DW_OP_reg10:
14016 case DW_OP_reg11:
14017 case DW_OP_reg12:
14018 case DW_OP_reg13:
14019 case DW_OP_reg14:
14020 case DW_OP_reg15:
14021 case DW_OP_reg16:
14022 case DW_OP_reg17:
14023 case DW_OP_reg18:
14024 case DW_OP_reg19:
14025 case DW_OP_reg20:
14026 case DW_OP_reg21:
14027 case DW_OP_reg22:
14028 case DW_OP_reg23:
14029 case DW_OP_reg24:
14030 case DW_OP_reg25:
14031 case DW_OP_reg26:
14032 case DW_OP_reg27:
14033 case DW_OP_reg28:
14034 case DW_OP_reg29:
14035 case DW_OP_reg30:
14036 case DW_OP_reg31:
14037 stack[++stacki] = op - DW_OP_reg0;
14038 if (i < size)
14039 dwarf2_complex_location_expr_complaint ();
14040 break;
14041
14042 case DW_OP_regx:
14043 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14044 i += bytes_read;
14045 stack[++stacki] = unsnd;
14046 if (i < size)
14047 dwarf2_complex_location_expr_complaint ();
14048 break;
14049
14050 case DW_OP_addr:
14051 stack[++stacki] = read_address (objfile->obfd, &data[i],
14052 cu, &bytes_read);
14053 i += bytes_read;
14054 break;
14055
14056 case DW_OP_const1u:
14057 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14058 i += 1;
14059 break;
14060
14061 case DW_OP_const1s:
14062 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14063 i += 1;
14064 break;
14065
14066 case DW_OP_const2u:
14067 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14068 i += 2;
14069 break;
14070
14071 case DW_OP_const2s:
14072 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14073 i += 2;
14074 break;
14075
14076 case DW_OP_const4u:
14077 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14078 i += 4;
14079 break;
14080
14081 case DW_OP_const4s:
14082 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14083 i += 4;
14084 break;
14085
14086 case DW_OP_constu:
14087 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14088 &bytes_read);
14089 i += bytes_read;
14090 break;
14091
14092 case DW_OP_consts:
14093 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14094 i += bytes_read;
14095 break;
14096
14097 case DW_OP_dup:
14098 stack[stacki + 1] = stack[stacki];
14099 stacki++;
14100 break;
14101
14102 case DW_OP_plus:
14103 stack[stacki - 1] += stack[stacki];
14104 stacki--;
14105 break;
14106
14107 case DW_OP_plus_uconst:
14108 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14109 &bytes_read);
14110 i += bytes_read;
14111 break;
14112
14113 case DW_OP_minus:
14114 stack[stacki - 1] -= stack[stacki];
14115 stacki--;
14116 break;
14117
14118 case DW_OP_deref:
14119 /* If we're not the last op, then we definitely can't encode
14120 this using GDB's address_class enum. This is valid for partial
14121 global symbols, although the variable's address will be bogus
14122 in the psymtab. */
14123 if (i < size)
14124 dwarf2_complex_location_expr_complaint ();
14125 break;
14126
14127 case DW_OP_GNU_push_tls_address:
14128 /* The top of the stack has the offset from the beginning
14129 of the thread control block at which the variable is located. */
14130 /* Nothing should follow this operator, so the top of stack would
14131 be returned. */
14132 /* This is valid for partial global symbols, but the variable's
14133 address will be bogus in the psymtab. */
14134 if (i < size)
14135 dwarf2_complex_location_expr_complaint ();
14136 break;
14137
14138 case DW_OP_GNU_uninit:
14139 break;
14140
14141 default:
14142 {
14143 const char *name = dwarf_stack_op_name (op);
14144
14145 if (name)
14146 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14147 name);
14148 else
14149 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14150 op);
14151 }
14152
14153 return (stack[stacki]);
14154 }
14155
14156 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14157 outside of the allocated space. Also enforce minimum>0. */
14158 if (stacki >= ARRAY_SIZE (stack) - 1)
14159 {
14160 complaint (&symfile_complaints,
14161 _("location description stack overflow"));
14162 return 0;
14163 }
14164
14165 if (stacki <= 0)
14166 {
14167 complaint (&symfile_complaints,
14168 _("location description stack underflow"));
14169 return 0;
14170 }
14171 }
14172 return (stack[stacki]);
14173 }
14174
14175 /* memory allocation interface */
14176
14177 static struct dwarf_block *
14178 dwarf_alloc_block (struct dwarf2_cu *cu)
14179 {
14180 struct dwarf_block *blk;
14181
14182 blk = (struct dwarf_block *)
14183 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14184 return (blk);
14185 }
14186
14187 static struct abbrev_info *
14188 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14189 {
14190 struct abbrev_info *abbrev;
14191
14192 abbrev = (struct abbrev_info *)
14193 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14194 memset (abbrev, 0, sizeof (struct abbrev_info));
14195 return (abbrev);
14196 }
14197
14198 static struct die_info *
14199 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14200 {
14201 struct die_info *die;
14202 size_t size = sizeof (struct die_info);
14203
14204 if (num_attrs > 1)
14205 size += (num_attrs - 1) * sizeof (struct attribute);
14206
14207 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14208 memset (die, 0, sizeof (struct die_info));
14209 return (die);
14210 }
14211
14212 \f
14213 /* Macro support. */
14214
14215 /* Return the full name of file number I in *LH's file name table.
14216 Use COMP_DIR as the name of the current directory of the
14217 compilation. The result is allocated using xmalloc; the caller is
14218 responsible for freeing it. */
14219 static char *
14220 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14221 {
14222 /* Is the file number a valid index into the line header's file name
14223 table? Remember that file numbers start with one, not zero. */
14224 if (1 <= file && file <= lh->num_file_names)
14225 {
14226 struct file_entry *fe = &lh->file_names[file - 1];
14227
14228 if (IS_ABSOLUTE_PATH (fe->name))
14229 return xstrdup (fe->name);
14230 else
14231 {
14232 const char *dir;
14233 int dir_len;
14234 char *full_name;
14235
14236 if (fe->dir_index)
14237 dir = lh->include_dirs[fe->dir_index - 1];
14238 else
14239 dir = comp_dir;
14240
14241 if (dir)
14242 {
14243 dir_len = strlen (dir);
14244 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14245 strcpy (full_name, dir);
14246 full_name[dir_len] = '/';
14247 strcpy (full_name + dir_len + 1, fe->name);
14248 return full_name;
14249 }
14250 else
14251 return xstrdup (fe->name);
14252 }
14253 }
14254 else
14255 {
14256 /* The compiler produced a bogus file number. We can at least
14257 record the macro definitions made in the file, even if we
14258 won't be able to find the file by name. */
14259 char fake_name[80];
14260
14261 sprintf (fake_name, "<bad macro file number %d>", file);
14262
14263 complaint (&symfile_complaints,
14264 _("bad file number in macro information (%d)"),
14265 file);
14266
14267 return xstrdup (fake_name);
14268 }
14269 }
14270
14271
14272 static struct macro_source_file *
14273 macro_start_file (int file, int line,
14274 struct macro_source_file *current_file,
14275 const char *comp_dir,
14276 struct line_header *lh, struct objfile *objfile)
14277 {
14278 /* The full name of this source file. */
14279 char *full_name = file_full_name (file, lh, comp_dir);
14280
14281 /* We don't create a macro table for this compilation unit
14282 at all until we actually get a filename. */
14283 if (! pending_macros)
14284 pending_macros = new_macro_table (&objfile->objfile_obstack,
14285 objfile->macro_cache);
14286
14287 if (! current_file)
14288 /* If we have no current file, then this must be the start_file
14289 directive for the compilation unit's main source file. */
14290 current_file = macro_set_main (pending_macros, full_name);
14291 else
14292 current_file = macro_include (current_file, line, full_name);
14293
14294 xfree (full_name);
14295
14296 return current_file;
14297 }
14298
14299
14300 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14301 followed by a null byte. */
14302 static char *
14303 copy_string (const char *buf, int len)
14304 {
14305 char *s = xmalloc (len + 1);
14306
14307 memcpy (s, buf, len);
14308 s[len] = '\0';
14309 return s;
14310 }
14311
14312
14313 static const char *
14314 consume_improper_spaces (const char *p, const char *body)
14315 {
14316 if (*p == ' ')
14317 {
14318 complaint (&symfile_complaints,
14319 _("macro definition contains spaces "
14320 "in formal argument list:\n`%s'"),
14321 body);
14322
14323 while (*p == ' ')
14324 p++;
14325 }
14326
14327 return p;
14328 }
14329
14330
14331 static void
14332 parse_macro_definition (struct macro_source_file *file, int line,
14333 const char *body)
14334 {
14335 const char *p;
14336
14337 /* The body string takes one of two forms. For object-like macro
14338 definitions, it should be:
14339
14340 <macro name> " " <definition>
14341
14342 For function-like macro definitions, it should be:
14343
14344 <macro name> "() " <definition>
14345 or
14346 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14347
14348 Spaces may appear only where explicitly indicated, and in the
14349 <definition>.
14350
14351 The Dwarf 2 spec says that an object-like macro's name is always
14352 followed by a space, but versions of GCC around March 2002 omit
14353 the space when the macro's definition is the empty string.
14354
14355 The Dwarf 2 spec says that there should be no spaces between the
14356 formal arguments in a function-like macro's formal argument list,
14357 but versions of GCC around March 2002 include spaces after the
14358 commas. */
14359
14360
14361 /* Find the extent of the macro name. The macro name is terminated
14362 by either a space or null character (for an object-like macro) or
14363 an opening paren (for a function-like macro). */
14364 for (p = body; *p; p++)
14365 if (*p == ' ' || *p == '(')
14366 break;
14367
14368 if (*p == ' ' || *p == '\0')
14369 {
14370 /* It's an object-like macro. */
14371 int name_len = p - body;
14372 char *name = copy_string (body, name_len);
14373 const char *replacement;
14374
14375 if (*p == ' ')
14376 replacement = body + name_len + 1;
14377 else
14378 {
14379 dwarf2_macro_malformed_definition_complaint (body);
14380 replacement = body + name_len;
14381 }
14382
14383 macro_define_object (file, line, name, replacement);
14384
14385 xfree (name);
14386 }
14387 else if (*p == '(')
14388 {
14389 /* It's a function-like macro. */
14390 char *name = copy_string (body, p - body);
14391 int argc = 0;
14392 int argv_size = 1;
14393 char **argv = xmalloc (argv_size * sizeof (*argv));
14394
14395 p++;
14396
14397 p = consume_improper_spaces (p, body);
14398
14399 /* Parse the formal argument list. */
14400 while (*p && *p != ')')
14401 {
14402 /* Find the extent of the current argument name. */
14403 const char *arg_start = p;
14404
14405 while (*p && *p != ',' && *p != ')' && *p != ' ')
14406 p++;
14407
14408 if (! *p || p == arg_start)
14409 dwarf2_macro_malformed_definition_complaint (body);
14410 else
14411 {
14412 /* Make sure argv has room for the new argument. */
14413 if (argc >= argv_size)
14414 {
14415 argv_size *= 2;
14416 argv = xrealloc (argv, argv_size * sizeof (*argv));
14417 }
14418
14419 argv[argc++] = copy_string (arg_start, p - arg_start);
14420 }
14421
14422 p = consume_improper_spaces (p, body);
14423
14424 /* Consume the comma, if present. */
14425 if (*p == ',')
14426 {
14427 p++;
14428
14429 p = consume_improper_spaces (p, body);
14430 }
14431 }
14432
14433 if (*p == ')')
14434 {
14435 p++;
14436
14437 if (*p == ' ')
14438 /* Perfectly formed definition, no complaints. */
14439 macro_define_function (file, line, name,
14440 argc, (const char **) argv,
14441 p + 1);
14442 else if (*p == '\0')
14443 {
14444 /* Complain, but do define it. */
14445 dwarf2_macro_malformed_definition_complaint (body);
14446 macro_define_function (file, line, name,
14447 argc, (const char **) argv,
14448 p);
14449 }
14450 else
14451 /* Just complain. */
14452 dwarf2_macro_malformed_definition_complaint (body);
14453 }
14454 else
14455 /* Just complain. */
14456 dwarf2_macro_malformed_definition_complaint (body);
14457
14458 xfree (name);
14459 {
14460 int i;
14461
14462 for (i = 0; i < argc; i++)
14463 xfree (argv[i]);
14464 }
14465 xfree (argv);
14466 }
14467 else
14468 dwarf2_macro_malformed_definition_complaint (body);
14469 }
14470
14471
14472 static void
14473 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
14474 char *comp_dir, bfd *abfd,
14475 struct dwarf2_cu *cu)
14476 {
14477 gdb_byte *mac_ptr, *mac_end;
14478 struct macro_source_file *current_file = 0;
14479 enum dwarf_macinfo_record_type macinfo_type;
14480 int at_commandline;
14481
14482 dwarf2_read_section (dwarf2_per_objfile->objfile,
14483 &dwarf2_per_objfile->macinfo);
14484 if (dwarf2_per_objfile->macinfo.buffer == NULL)
14485 {
14486 complaint (&symfile_complaints, _("missing .debug_macinfo section"));
14487 return;
14488 }
14489
14490 /* First pass: Find the name of the base filename.
14491 This filename is needed in order to process all macros whose definition
14492 (or undefinition) comes from the command line. These macros are defined
14493 before the first DW_MACINFO_start_file entry, and yet still need to be
14494 associated to the base file.
14495
14496 To determine the base file name, we scan the macro definitions until we
14497 reach the first DW_MACINFO_start_file entry. We then initialize
14498 CURRENT_FILE accordingly so that any macro definition found before the
14499 first DW_MACINFO_start_file can still be associated to the base file. */
14500
14501 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14502 mac_end = dwarf2_per_objfile->macinfo.buffer
14503 + dwarf2_per_objfile->macinfo.size;
14504
14505 do
14506 {
14507 /* Do we at least have room for a macinfo type byte? */
14508 if (mac_ptr >= mac_end)
14509 {
14510 /* Complaint is printed during the second pass as GDB will probably
14511 stop the first pass earlier upon finding
14512 DW_MACINFO_start_file. */
14513 break;
14514 }
14515
14516 macinfo_type = read_1_byte (abfd, mac_ptr);
14517 mac_ptr++;
14518
14519 switch (macinfo_type)
14520 {
14521 /* A zero macinfo type indicates the end of the macro
14522 information. */
14523 case 0:
14524 break;
14525
14526 case DW_MACINFO_define:
14527 case DW_MACINFO_undef:
14528 /* Only skip the data by MAC_PTR. */
14529 {
14530 unsigned int bytes_read;
14531
14532 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14533 mac_ptr += bytes_read;
14534 read_direct_string (abfd, mac_ptr, &bytes_read);
14535 mac_ptr += bytes_read;
14536 }
14537 break;
14538
14539 case DW_MACINFO_start_file:
14540 {
14541 unsigned int bytes_read;
14542 int line, file;
14543
14544 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14545 mac_ptr += bytes_read;
14546 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14547 mac_ptr += bytes_read;
14548
14549 current_file = macro_start_file (file, line, current_file,
14550 comp_dir, lh, cu->objfile);
14551 }
14552 break;
14553
14554 case DW_MACINFO_end_file:
14555 /* No data to skip by MAC_PTR. */
14556 break;
14557
14558 case DW_MACINFO_vendor_ext:
14559 /* Only skip the data by MAC_PTR. */
14560 {
14561 unsigned int bytes_read;
14562
14563 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14564 mac_ptr += bytes_read;
14565 read_direct_string (abfd, mac_ptr, &bytes_read);
14566 mac_ptr += bytes_read;
14567 }
14568 break;
14569
14570 default:
14571 break;
14572 }
14573 } while (macinfo_type != 0 && current_file == NULL);
14574
14575 /* Second pass: Process all entries.
14576
14577 Use the AT_COMMAND_LINE flag to determine whether we are still processing
14578 command-line macro definitions/undefinitions. This flag is unset when we
14579 reach the first DW_MACINFO_start_file entry. */
14580
14581 mac_ptr = dwarf2_per_objfile->macinfo.buffer + offset;
14582
14583 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
14584 GDB is still reading the definitions from command line. First
14585 DW_MACINFO_start_file will need to be ignored as it was already executed
14586 to create CURRENT_FILE for the main source holding also the command line
14587 definitions. On first met DW_MACINFO_start_file this flag is reset to
14588 normally execute all the remaining DW_MACINFO_start_file macinfos. */
14589
14590 at_commandline = 1;
14591
14592 do
14593 {
14594 /* Do we at least have room for a macinfo type byte? */
14595 if (mac_ptr >= mac_end)
14596 {
14597 dwarf2_macros_too_long_complaint ();
14598 break;
14599 }
14600
14601 macinfo_type = read_1_byte (abfd, mac_ptr);
14602 mac_ptr++;
14603
14604 switch (macinfo_type)
14605 {
14606 /* A zero macinfo type indicates the end of the macro
14607 information. */
14608 case 0:
14609 break;
14610
14611 case DW_MACINFO_define:
14612 case DW_MACINFO_undef:
14613 {
14614 unsigned int bytes_read;
14615 int line;
14616 char *body;
14617
14618 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14619 mac_ptr += bytes_read;
14620 body = read_direct_string (abfd, mac_ptr, &bytes_read);
14621 mac_ptr += bytes_read;
14622
14623 if (! current_file)
14624 {
14625 /* DWARF violation as no main source is present. */
14626 complaint (&symfile_complaints,
14627 _("debug info with no main source gives macro %s "
14628 "on line %d: %s"),
14629 macinfo_type == DW_MACINFO_define ?
14630 _("definition") :
14631 macinfo_type == DW_MACINFO_undef ?
14632 _("undefinition") :
14633 _("something-or-other"), line, body);
14634 break;
14635 }
14636 if ((line == 0 && !at_commandline)
14637 || (line != 0 && at_commandline))
14638 complaint (&symfile_complaints,
14639 _("debug info gives %s macro %s with %s line %d: %s"),
14640 at_commandline ? _("command-line") : _("in-file"),
14641 macinfo_type == DW_MACINFO_define ?
14642 _("definition") :
14643 macinfo_type == DW_MACINFO_undef ?
14644 _("undefinition") :
14645 _("something-or-other"),
14646 line == 0 ? _("zero") : _("non-zero"), line, body);
14647
14648 if (macinfo_type == DW_MACINFO_define)
14649 parse_macro_definition (current_file, line, body);
14650 else if (macinfo_type == DW_MACINFO_undef)
14651 macro_undef (current_file, line, body);
14652 }
14653 break;
14654
14655 case DW_MACINFO_start_file:
14656 {
14657 unsigned int bytes_read;
14658 int line, file;
14659
14660 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14661 mac_ptr += bytes_read;
14662 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14663 mac_ptr += bytes_read;
14664
14665 if ((line == 0 && !at_commandline)
14666 || (line != 0 && at_commandline))
14667 complaint (&symfile_complaints,
14668 _("debug info gives source %d included "
14669 "from %s at %s line %d"),
14670 file, at_commandline ? _("command-line") : _("file"),
14671 line == 0 ? _("zero") : _("non-zero"), line);
14672
14673 if (at_commandline)
14674 {
14675 /* This DW_MACINFO_start_file was executed in the pass one. */
14676 at_commandline = 0;
14677 }
14678 else
14679 current_file = macro_start_file (file, line,
14680 current_file, comp_dir,
14681 lh, cu->objfile);
14682 }
14683 break;
14684
14685 case DW_MACINFO_end_file:
14686 if (! current_file)
14687 complaint (&symfile_complaints,
14688 _("macro debug info has an unmatched "
14689 "`close_file' directive"));
14690 else
14691 {
14692 current_file = current_file->included_by;
14693 if (! current_file)
14694 {
14695 enum dwarf_macinfo_record_type next_type;
14696
14697 /* GCC circa March 2002 doesn't produce the zero
14698 type byte marking the end of the compilation
14699 unit. Complain if it's not there, but exit no
14700 matter what. */
14701
14702 /* Do we at least have room for a macinfo type byte? */
14703 if (mac_ptr >= mac_end)
14704 {
14705 dwarf2_macros_too_long_complaint ();
14706 return;
14707 }
14708
14709 /* We don't increment mac_ptr here, so this is just
14710 a look-ahead. */
14711 next_type = read_1_byte (abfd, mac_ptr);
14712 if (next_type != 0)
14713 complaint (&symfile_complaints,
14714 _("no terminating 0-type entry for "
14715 "macros in `.debug_macinfo' section"));
14716
14717 return;
14718 }
14719 }
14720 break;
14721
14722 case DW_MACINFO_vendor_ext:
14723 {
14724 unsigned int bytes_read;
14725 int constant;
14726
14727 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
14728 mac_ptr += bytes_read;
14729 read_direct_string (abfd, mac_ptr, &bytes_read);
14730 mac_ptr += bytes_read;
14731
14732 /* We don't recognize any vendor extensions. */
14733 }
14734 break;
14735 }
14736 } while (macinfo_type != 0);
14737 }
14738
14739 /* Check if the attribute's form is a DW_FORM_block*
14740 if so return true else false. */
14741 static int
14742 attr_form_is_block (struct attribute *attr)
14743 {
14744 return (attr == NULL ? 0 :
14745 attr->form == DW_FORM_block1
14746 || attr->form == DW_FORM_block2
14747 || attr->form == DW_FORM_block4
14748 || attr->form == DW_FORM_block
14749 || attr->form == DW_FORM_exprloc);
14750 }
14751
14752 /* Return non-zero if ATTR's value is a section offset --- classes
14753 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
14754 You may use DW_UNSND (attr) to retrieve such offsets.
14755
14756 Section 7.5.4, "Attribute Encodings", explains that no attribute
14757 may have a value that belongs to more than one of these classes; it
14758 would be ambiguous if we did, because we use the same forms for all
14759 of them. */
14760 static int
14761 attr_form_is_section_offset (struct attribute *attr)
14762 {
14763 return (attr->form == DW_FORM_data4
14764 || attr->form == DW_FORM_data8
14765 || attr->form == DW_FORM_sec_offset);
14766 }
14767
14768
14769 /* Return non-zero if ATTR's value falls in the 'constant' class, or
14770 zero otherwise. When this function returns true, you can apply
14771 dwarf2_get_attr_constant_value to it.
14772
14773 However, note that for some attributes you must check
14774 attr_form_is_section_offset before using this test. DW_FORM_data4
14775 and DW_FORM_data8 are members of both the constant class, and of
14776 the classes that contain offsets into other debug sections
14777 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
14778 that, if an attribute's can be either a constant or one of the
14779 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
14780 taken as section offsets, not constants. */
14781 static int
14782 attr_form_is_constant (struct attribute *attr)
14783 {
14784 switch (attr->form)
14785 {
14786 case DW_FORM_sdata:
14787 case DW_FORM_udata:
14788 case DW_FORM_data1:
14789 case DW_FORM_data2:
14790 case DW_FORM_data4:
14791 case DW_FORM_data8:
14792 return 1;
14793 default:
14794 return 0;
14795 }
14796 }
14797
14798 /* A helper function that fills in a dwarf2_loclist_baton. */
14799
14800 static void
14801 fill_in_loclist_baton (struct dwarf2_cu *cu,
14802 struct dwarf2_loclist_baton *baton,
14803 struct attribute *attr)
14804 {
14805 dwarf2_read_section (dwarf2_per_objfile->objfile,
14806 &dwarf2_per_objfile->loc);
14807
14808 baton->per_cu = cu->per_cu;
14809 gdb_assert (baton->per_cu);
14810 /* We don't know how long the location list is, but make sure we
14811 don't run off the edge of the section. */
14812 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
14813 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
14814 baton->base_address = cu->base_address;
14815 }
14816
14817 static void
14818 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
14819 struct dwarf2_cu *cu)
14820 {
14821 if (attr_form_is_section_offset (attr)
14822 /* ".debug_loc" may not exist at all, or the offset may be outside
14823 the section. If so, fall through to the complaint in the
14824 other branch. */
14825 && DW_UNSND (attr) < dwarf2_section_size (dwarf2_per_objfile->objfile,
14826 &dwarf2_per_objfile->loc))
14827 {
14828 struct dwarf2_loclist_baton *baton;
14829
14830 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14831 sizeof (struct dwarf2_loclist_baton));
14832
14833 fill_in_loclist_baton (cu, baton, attr);
14834
14835 if (cu->base_known == 0)
14836 complaint (&symfile_complaints,
14837 _("Location list used without "
14838 "specifying the CU base address."));
14839
14840 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
14841 SYMBOL_LOCATION_BATON (sym) = baton;
14842 }
14843 else
14844 {
14845 struct dwarf2_locexpr_baton *baton;
14846
14847 baton = obstack_alloc (&cu->objfile->objfile_obstack,
14848 sizeof (struct dwarf2_locexpr_baton));
14849 baton->per_cu = cu->per_cu;
14850 gdb_assert (baton->per_cu);
14851
14852 if (attr_form_is_block (attr))
14853 {
14854 /* Note that we're just copying the block's data pointer
14855 here, not the actual data. We're still pointing into the
14856 info_buffer for SYM's objfile; right now we never release
14857 that buffer, but when we do clean up properly this may
14858 need to change. */
14859 baton->size = DW_BLOCK (attr)->size;
14860 baton->data = DW_BLOCK (attr)->data;
14861 }
14862 else
14863 {
14864 dwarf2_invalid_attrib_class_complaint ("location description",
14865 SYMBOL_NATURAL_NAME (sym));
14866 baton->size = 0;
14867 baton->data = NULL;
14868 }
14869
14870 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
14871 SYMBOL_LOCATION_BATON (sym) = baton;
14872 }
14873 }
14874
14875 /* Return the OBJFILE associated with the compilation unit CU. If CU
14876 came from a separate debuginfo file, then the master objfile is
14877 returned. */
14878
14879 struct objfile *
14880 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
14881 {
14882 struct objfile *objfile = per_cu->objfile;
14883
14884 /* Return the master objfile, so that we can report and look up the
14885 correct file containing this variable. */
14886 if (objfile->separate_debug_objfile_backlink)
14887 objfile = objfile->separate_debug_objfile_backlink;
14888
14889 return objfile;
14890 }
14891
14892 /* Return the address size given in the compilation unit header for CU. */
14893
14894 CORE_ADDR
14895 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
14896 {
14897 if (per_cu->cu)
14898 return per_cu->cu->header.addr_size;
14899 else
14900 {
14901 /* If the CU is not currently read in, we re-read its header. */
14902 struct objfile *objfile = per_cu->objfile;
14903 struct dwarf2_per_objfile *per_objfile
14904 = objfile_data (objfile, dwarf2_objfile_data_key);
14905 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14906 struct comp_unit_head cu_header;
14907
14908 memset (&cu_header, 0, sizeof cu_header);
14909 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14910 return cu_header.addr_size;
14911 }
14912 }
14913
14914 /* Return the offset size given in the compilation unit header for CU. */
14915
14916 int
14917 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
14918 {
14919 if (per_cu->cu)
14920 return per_cu->cu->header.offset_size;
14921 else
14922 {
14923 /* If the CU is not currently read in, we re-read its header. */
14924 struct objfile *objfile = per_cu->objfile;
14925 struct dwarf2_per_objfile *per_objfile
14926 = objfile_data (objfile, dwarf2_objfile_data_key);
14927 gdb_byte *info_ptr = per_objfile->info.buffer + per_cu->offset;
14928 struct comp_unit_head cu_header;
14929
14930 memset (&cu_header, 0, sizeof cu_header);
14931 read_comp_unit_head (&cu_header, info_ptr, objfile->obfd);
14932 return cu_header.offset_size;
14933 }
14934 }
14935
14936 /* Return the text offset of the CU. The returned offset comes from
14937 this CU's objfile. If this objfile came from a separate debuginfo
14938 file, then the offset may be different from the corresponding
14939 offset in the parent objfile. */
14940
14941 CORE_ADDR
14942 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
14943 {
14944 struct objfile *objfile = per_cu->objfile;
14945
14946 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
14947 }
14948
14949 /* Locate the .debug_info compilation unit from CU's objfile which contains
14950 the DIE at OFFSET. Raises an error on failure. */
14951
14952 static struct dwarf2_per_cu_data *
14953 dwarf2_find_containing_comp_unit (unsigned int offset,
14954 struct objfile *objfile)
14955 {
14956 struct dwarf2_per_cu_data *this_cu;
14957 int low, high;
14958
14959 low = 0;
14960 high = dwarf2_per_objfile->n_comp_units - 1;
14961 while (high > low)
14962 {
14963 int mid = low + (high - low) / 2;
14964
14965 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
14966 high = mid;
14967 else
14968 low = mid + 1;
14969 }
14970 gdb_assert (low == high);
14971 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
14972 {
14973 if (low == 0)
14974 error (_("Dwarf Error: could not find partial DIE containing "
14975 "offset 0x%lx [in module %s]"),
14976 (long) offset, bfd_get_filename (objfile->obfd));
14977
14978 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
14979 return dwarf2_per_objfile->all_comp_units[low-1];
14980 }
14981 else
14982 {
14983 this_cu = dwarf2_per_objfile->all_comp_units[low];
14984 if (low == dwarf2_per_objfile->n_comp_units - 1
14985 && offset >= this_cu->offset + this_cu->length)
14986 error (_("invalid dwarf2 offset %u"), offset);
14987 gdb_assert (offset < this_cu->offset + this_cu->length);
14988 return this_cu;
14989 }
14990 }
14991
14992 /* Locate the compilation unit from OBJFILE which is located at exactly
14993 OFFSET. Raises an error on failure. */
14994
14995 static struct dwarf2_per_cu_data *
14996 dwarf2_find_comp_unit (unsigned int offset, struct objfile *objfile)
14997 {
14998 struct dwarf2_per_cu_data *this_cu;
14999
15000 this_cu = dwarf2_find_containing_comp_unit (offset, objfile);
15001 if (this_cu->offset != offset)
15002 error (_("no compilation unit with offset %u."), offset);
15003 return this_cu;
15004 }
15005
15006 /* Initialize dwarf2_cu CU for OBJFILE in a pre-allocated space. */
15007
15008 static void
15009 init_one_comp_unit (struct dwarf2_cu *cu, struct objfile *objfile)
15010 {
15011 memset (cu, 0, sizeof (*cu));
15012 cu->objfile = objfile;
15013 obstack_init (&cu->comp_unit_obstack);
15014 }
15015
15016 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15017
15018 static void
15019 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15020 {
15021 struct attribute *attr;
15022
15023 /* Set the language we're debugging. */
15024 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15025 if (attr)
15026 set_cu_language (DW_UNSND (attr), cu);
15027 else
15028 {
15029 cu->language = language_minimal;
15030 cu->language_defn = language_def (cu->language);
15031 }
15032 }
15033
15034 /* Release one cached compilation unit, CU. We unlink it from the tree
15035 of compilation units, but we don't remove it from the read_in_chain;
15036 the caller is responsible for that.
15037 NOTE: DATA is a void * because this function is also used as a
15038 cleanup routine. */
15039
15040 static void
15041 free_one_comp_unit (void *data)
15042 {
15043 struct dwarf2_cu *cu = data;
15044
15045 if (cu->per_cu != NULL)
15046 cu->per_cu->cu = NULL;
15047 cu->per_cu = NULL;
15048
15049 obstack_free (&cu->comp_unit_obstack, NULL);
15050
15051 xfree (cu);
15052 }
15053
15054 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15055 when we're finished with it. We can't free the pointer itself, but be
15056 sure to unlink it from the cache. Also release any associated storage
15057 and perform cache maintenance.
15058
15059 Only used during partial symbol parsing. */
15060
15061 static void
15062 free_stack_comp_unit (void *data)
15063 {
15064 struct dwarf2_cu *cu = data;
15065
15066 obstack_free (&cu->comp_unit_obstack, NULL);
15067 cu->partial_dies = NULL;
15068
15069 if (cu->per_cu != NULL)
15070 {
15071 /* This compilation unit is on the stack in our caller, so we
15072 should not xfree it. Just unlink it. */
15073 cu->per_cu->cu = NULL;
15074 cu->per_cu = NULL;
15075
15076 /* If we had a per-cu pointer, then we may have other compilation
15077 units loaded, so age them now. */
15078 age_cached_comp_units ();
15079 }
15080 }
15081
15082 /* Free all cached compilation units. */
15083
15084 static void
15085 free_cached_comp_units (void *data)
15086 {
15087 struct dwarf2_per_cu_data *per_cu, **last_chain;
15088
15089 per_cu = dwarf2_per_objfile->read_in_chain;
15090 last_chain = &dwarf2_per_objfile->read_in_chain;
15091 while (per_cu != NULL)
15092 {
15093 struct dwarf2_per_cu_data *next_cu;
15094
15095 next_cu = per_cu->cu->read_in_chain;
15096
15097 free_one_comp_unit (per_cu->cu);
15098 *last_chain = next_cu;
15099
15100 per_cu = next_cu;
15101 }
15102 }
15103
15104 /* Increase the age counter on each cached compilation unit, and free
15105 any that are too old. */
15106
15107 static void
15108 age_cached_comp_units (void)
15109 {
15110 struct dwarf2_per_cu_data *per_cu, **last_chain;
15111
15112 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
15113 per_cu = dwarf2_per_objfile->read_in_chain;
15114 while (per_cu != NULL)
15115 {
15116 per_cu->cu->last_used ++;
15117 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
15118 dwarf2_mark (per_cu->cu);
15119 per_cu = per_cu->cu->read_in_chain;
15120 }
15121
15122 per_cu = dwarf2_per_objfile->read_in_chain;
15123 last_chain = &dwarf2_per_objfile->read_in_chain;
15124 while (per_cu != NULL)
15125 {
15126 struct dwarf2_per_cu_data *next_cu;
15127
15128 next_cu = per_cu->cu->read_in_chain;
15129
15130 if (!per_cu->cu->mark)
15131 {
15132 free_one_comp_unit (per_cu->cu);
15133 *last_chain = next_cu;
15134 }
15135 else
15136 last_chain = &per_cu->cu->read_in_chain;
15137
15138 per_cu = next_cu;
15139 }
15140 }
15141
15142 /* Remove a single compilation unit from the cache. */
15143
15144 static void
15145 free_one_cached_comp_unit (void *target_cu)
15146 {
15147 struct dwarf2_per_cu_data *per_cu, **last_chain;
15148
15149 per_cu = dwarf2_per_objfile->read_in_chain;
15150 last_chain = &dwarf2_per_objfile->read_in_chain;
15151 while (per_cu != NULL)
15152 {
15153 struct dwarf2_per_cu_data *next_cu;
15154
15155 next_cu = per_cu->cu->read_in_chain;
15156
15157 if (per_cu->cu == target_cu)
15158 {
15159 free_one_comp_unit (per_cu->cu);
15160 *last_chain = next_cu;
15161 break;
15162 }
15163 else
15164 last_chain = &per_cu->cu->read_in_chain;
15165
15166 per_cu = next_cu;
15167 }
15168 }
15169
15170 /* Release all extra memory associated with OBJFILE. */
15171
15172 void
15173 dwarf2_free_objfile (struct objfile *objfile)
15174 {
15175 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15176
15177 if (dwarf2_per_objfile == NULL)
15178 return;
15179
15180 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
15181 free_cached_comp_units (NULL);
15182
15183 if (dwarf2_per_objfile->quick_file_names_table)
15184 htab_delete (dwarf2_per_objfile->quick_file_names_table);
15185
15186 /* Everything else should be on the objfile obstack. */
15187 }
15188
15189 /* A pair of DIE offset and GDB type pointer. We store these
15190 in a hash table separate from the DIEs, and preserve them
15191 when the DIEs are flushed out of cache. */
15192
15193 struct dwarf2_offset_and_type
15194 {
15195 unsigned int offset;
15196 struct type *type;
15197 };
15198
15199 /* Hash function for a dwarf2_offset_and_type. */
15200
15201 static hashval_t
15202 offset_and_type_hash (const void *item)
15203 {
15204 const struct dwarf2_offset_and_type *ofs = item;
15205
15206 return ofs->offset;
15207 }
15208
15209 /* Equality function for a dwarf2_offset_and_type. */
15210
15211 static int
15212 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
15213 {
15214 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
15215 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
15216
15217 return ofs_lhs->offset == ofs_rhs->offset;
15218 }
15219
15220 /* Set the type associated with DIE to TYPE. Save it in CU's hash
15221 table if necessary. For convenience, return TYPE.
15222
15223 The DIEs reading must have careful ordering to:
15224 * Not cause infite loops trying to read in DIEs as a prerequisite for
15225 reading current DIE.
15226 * Not trying to dereference contents of still incompletely read in types
15227 while reading in other DIEs.
15228 * Enable referencing still incompletely read in types just by a pointer to
15229 the type without accessing its fields.
15230
15231 Therefore caller should follow these rules:
15232 * Try to fetch any prerequisite types we may need to build this DIE type
15233 before building the type and calling set_die_type.
15234 * After building type call set_die_type for current DIE as soon as
15235 possible before fetching more types to complete the current type.
15236 * Make the type as complete as possible before fetching more types. */
15237
15238 static struct type *
15239 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
15240 {
15241 struct dwarf2_offset_and_type **slot, ofs;
15242 struct objfile *objfile = cu->objfile;
15243 htab_t *type_hash_ptr;
15244
15245 /* For Ada types, make sure that the gnat-specific data is always
15246 initialized (if not already set). There are a few types where
15247 we should not be doing so, because the type-specific area is
15248 already used to hold some other piece of info (eg: TYPE_CODE_FLT
15249 where the type-specific area is used to store the floatformat).
15250 But this is not a problem, because the gnat-specific information
15251 is actually not needed for these types. */
15252 if (need_gnat_info (cu)
15253 && TYPE_CODE (type) != TYPE_CODE_FUNC
15254 && TYPE_CODE (type) != TYPE_CODE_FLT
15255 && !HAVE_GNAT_AUX_INFO (type))
15256 INIT_GNAT_SPECIFIC (type);
15257
15258 if (cu->per_cu->from_debug_types)
15259 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
15260 else
15261 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
15262
15263 if (*type_hash_ptr == NULL)
15264 {
15265 *type_hash_ptr
15266 = htab_create_alloc_ex (127,
15267 offset_and_type_hash,
15268 offset_and_type_eq,
15269 NULL,
15270 &objfile->objfile_obstack,
15271 hashtab_obstack_allocate,
15272 dummy_obstack_deallocate);
15273 }
15274
15275 ofs.offset = die->offset;
15276 ofs.type = type;
15277 slot = (struct dwarf2_offset_and_type **)
15278 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
15279 if (*slot)
15280 complaint (&symfile_complaints,
15281 _("A problem internal to GDB: DIE 0x%x has type already set"),
15282 die->offset);
15283 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
15284 **slot = ofs;
15285 return type;
15286 }
15287
15288 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
15289 table, or return NULL if the die does not have a saved type. */
15290
15291 static struct type *
15292 get_die_type_at_offset (unsigned int offset,
15293 struct dwarf2_per_cu_data *per_cu)
15294 {
15295 struct dwarf2_offset_and_type *slot, ofs;
15296 htab_t type_hash;
15297
15298 if (per_cu->from_debug_types)
15299 type_hash = dwarf2_per_objfile->debug_types_type_hash;
15300 else
15301 type_hash = dwarf2_per_objfile->debug_info_type_hash;
15302 if (type_hash == NULL)
15303 return NULL;
15304
15305 ofs.offset = offset;
15306 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
15307 if (slot)
15308 return slot->type;
15309 else
15310 return NULL;
15311 }
15312
15313 /* Look up the type for DIE in the appropriate type_hash table,
15314 or return NULL if DIE does not have a saved type. */
15315
15316 static struct type *
15317 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
15318 {
15319 return get_die_type_at_offset (die->offset, cu->per_cu);
15320 }
15321
15322 /* Add a dependence relationship from CU to REF_PER_CU. */
15323
15324 static void
15325 dwarf2_add_dependence (struct dwarf2_cu *cu,
15326 struct dwarf2_per_cu_data *ref_per_cu)
15327 {
15328 void **slot;
15329
15330 if (cu->dependencies == NULL)
15331 cu->dependencies
15332 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
15333 NULL, &cu->comp_unit_obstack,
15334 hashtab_obstack_allocate,
15335 dummy_obstack_deallocate);
15336
15337 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
15338 if (*slot == NULL)
15339 *slot = ref_per_cu;
15340 }
15341
15342 /* Subroutine of dwarf2_mark to pass to htab_traverse.
15343 Set the mark field in every compilation unit in the
15344 cache that we must keep because we are keeping CU. */
15345
15346 static int
15347 dwarf2_mark_helper (void **slot, void *data)
15348 {
15349 struct dwarf2_per_cu_data *per_cu;
15350
15351 per_cu = (struct dwarf2_per_cu_data *) *slot;
15352 if (per_cu->cu->mark)
15353 return 1;
15354 per_cu->cu->mark = 1;
15355
15356 if (per_cu->cu->dependencies != NULL)
15357 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
15358
15359 return 1;
15360 }
15361
15362 /* Set the mark field in CU and in every other compilation unit in the
15363 cache that we must keep because we are keeping CU. */
15364
15365 static void
15366 dwarf2_mark (struct dwarf2_cu *cu)
15367 {
15368 if (cu->mark)
15369 return;
15370 cu->mark = 1;
15371 if (cu->dependencies != NULL)
15372 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
15373 }
15374
15375 static void
15376 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
15377 {
15378 while (per_cu)
15379 {
15380 per_cu->cu->mark = 0;
15381 per_cu = per_cu->cu->read_in_chain;
15382 }
15383 }
15384
15385 /* Trivial hash function for partial_die_info: the hash value of a DIE
15386 is its offset in .debug_info for this objfile. */
15387
15388 static hashval_t
15389 partial_die_hash (const void *item)
15390 {
15391 const struct partial_die_info *part_die = item;
15392
15393 return part_die->offset;
15394 }
15395
15396 /* Trivial comparison function for partial_die_info structures: two DIEs
15397 are equal if they have the same offset. */
15398
15399 static int
15400 partial_die_eq (const void *item_lhs, const void *item_rhs)
15401 {
15402 const struct partial_die_info *part_die_lhs = item_lhs;
15403 const struct partial_die_info *part_die_rhs = item_rhs;
15404
15405 return part_die_lhs->offset == part_die_rhs->offset;
15406 }
15407
15408 static struct cmd_list_element *set_dwarf2_cmdlist;
15409 static struct cmd_list_element *show_dwarf2_cmdlist;
15410
15411 static void
15412 set_dwarf2_cmd (char *args, int from_tty)
15413 {
15414 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
15415 }
15416
15417 static void
15418 show_dwarf2_cmd (char *args, int from_tty)
15419 {
15420 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
15421 }
15422
15423 /* If section described by INFO was mmapped, munmap it now. */
15424
15425 static void
15426 munmap_section_buffer (struct dwarf2_section_info *info)
15427 {
15428 if (info->map_addr != NULL)
15429 {
15430 #ifdef HAVE_MMAP
15431 int res;
15432
15433 res = munmap (info->map_addr, info->map_len);
15434 gdb_assert (res == 0);
15435 #else
15436 /* Without HAVE_MMAP, we should never be here to begin with. */
15437 gdb_assert_not_reached ("no mmap support");
15438 #endif
15439 }
15440 }
15441
15442 /* munmap debug sections for OBJFILE, if necessary. */
15443
15444 static void
15445 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
15446 {
15447 struct dwarf2_per_objfile *data = d;
15448
15449 /* This is sorted according to the order they're defined in to make it easier
15450 to keep in sync. */
15451 munmap_section_buffer (&data->info);
15452 munmap_section_buffer (&data->abbrev);
15453 munmap_section_buffer (&data->line);
15454 munmap_section_buffer (&data->loc);
15455 munmap_section_buffer (&data->macinfo);
15456 munmap_section_buffer (&data->str);
15457 munmap_section_buffer (&data->ranges);
15458 munmap_section_buffer (&data->types);
15459 munmap_section_buffer (&data->frame);
15460 munmap_section_buffer (&data->eh_frame);
15461 munmap_section_buffer (&data->gdb_index);
15462 }
15463
15464 \f
15465 /* The "save gdb-index" command. */
15466
15467 /* The contents of the hash table we create when building the string
15468 table. */
15469 struct strtab_entry
15470 {
15471 offset_type offset;
15472 const char *str;
15473 };
15474
15475 /* Hash function for a strtab_entry.
15476
15477 Function is used only during write_hash_table so no index format backward
15478 compatibility is needed. */
15479
15480 static hashval_t
15481 hash_strtab_entry (const void *e)
15482 {
15483 const struct strtab_entry *entry = e;
15484 return mapped_index_string_hash (INT_MAX, entry->str);
15485 }
15486
15487 /* Equality function for a strtab_entry. */
15488
15489 static int
15490 eq_strtab_entry (const void *a, const void *b)
15491 {
15492 const struct strtab_entry *ea = a;
15493 const struct strtab_entry *eb = b;
15494 return !strcmp (ea->str, eb->str);
15495 }
15496
15497 /* Create a strtab_entry hash table. */
15498
15499 static htab_t
15500 create_strtab (void)
15501 {
15502 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
15503 xfree, xcalloc, xfree);
15504 }
15505
15506 /* Add a string to the constant pool. Return the string's offset in
15507 host order. */
15508
15509 static offset_type
15510 add_string (htab_t table, struct obstack *cpool, const char *str)
15511 {
15512 void **slot;
15513 struct strtab_entry entry;
15514 struct strtab_entry *result;
15515
15516 entry.str = str;
15517 slot = htab_find_slot (table, &entry, INSERT);
15518 if (*slot)
15519 result = *slot;
15520 else
15521 {
15522 result = XNEW (struct strtab_entry);
15523 result->offset = obstack_object_size (cpool);
15524 result->str = str;
15525 obstack_grow_str0 (cpool, str);
15526 *slot = result;
15527 }
15528 return result->offset;
15529 }
15530
15531 /* An entry in the symbol table. */
15532 struct symtab_index_entry
15533 {
15534 /* The name of the symbol. */
15535 const char *name;
15536 /* The offset of the name in the constant pool. */
15537 offset_type index_offset;
15538 /* A sorted vector of the indices of all the CUs that hold an object
15539 of this name. */
15540 VEC (offset_type) *cu_indices;
15541 };
15542
15543 /* The symbol table. This is a power-of-2-sized hash table. */
15544 struct mapped_symtab
15545 {
15546 offset_type n_elements;
15547 offset_type size;
15548 struct symtab_index_entry **data;
15549 };
15550
15551 /* Hash function for a symtab_index_entry. */
15552
15553 static hashval_t
15554 hash_symtab_entry (const void *e)
15555 {
15556 const struct symtab_index_entry *entry = e;
15557 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
15558 sizeof (offset_type) * VEC_length (offset_type,
15559 entry->cu_indices),
15560 0);
15561 }
15562
15563 /* Equality function for a symtab_index_entry. */
15564
15565 static int
15566 eq_symtab_entry (const void *a, const void *b)
15567 {
15568 const struct symtab_index_entry *ea = a;
15569 const struct symtab_index_entry *eb = b;
15570 int len = VEC_length (offset_type, ea->cu_indices);
15571 if (len != VEC_length (offset_type, eb->cu_indices))
15572 return 0;
15573 return !memcmp (VEC_address (offset_type, ea->cu_indices),
15574 VEC_address (offset_type, eb->cu_indices),
15575 sizeof (offset_type) * len);
15576 }
15577
15578 /* Destroy a symtab_index_entry. */
15579
15580 static void
15581 delete_symtab_entry (void *p)
15582 {
15583 struct symtab_index_entry *entry = p;
15584 VEC_free (offset_type, entry->cu_indices);
15585 xfree (entry);
15586 }
15587
15588 /* Create a hash table holding symtab_index_entry objects. */
15589
15590 static htab_t
15591 create_symbol_hash_table (void)
15592 {
15593 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
15594 delete_symtab_entry, xcalloc, xfree);
15595 }
15596
15597 /* Create a new mapped symtab object. */
15598
15599 static struct mapped_symtab *
15600 create_mapped_symtab (void)
15601 {
15602 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
15603 symtab->n_elements = 0;
15604 symtab->size = 1024;
15605 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15606 return symtab;
15607 }
15608
15609 /* Destroy a mapped_symtab. */
15610
15611 static void
15612 cleanup_mapped_symtab (void *p)
15613 {
15614 struct mapped_symtab *symtab = p;
15615 /* The contents of the array are freed when the other hash table is
15616 destroyed. */
15617 xfree (symtab->data);
15618 xfree (symtab);
15619 }
15620
15621 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
15622 the slot.
15623
15624 Function is used only during write_hash_table so no index format backward
15625 compatibility is needed. */
15626
15627 static struct symtab_index_entry **
15628 find_slot (struct mapped_symtab *symtab, const char *name)
15629 {
15630 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
15631
15632 index = hash & (symtab->size - 1);
15633 step = ((hash * 17) & (symtab->size - 1)) | 1;
15634
15635 for (;;)
15636 {
15637 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
15638 return &symtab->data[index];
15639 index = (index + step) & (symtab->size - 1);
15640 }
15641 }
15642
15643 /* Expand SYMTAB's hash table. */
15644
15645 static void
15646 hash_expand (struct mapped_symtab *symtab)
15647 {
15648 offset_type old_size = symtab->size;
15649 offset_type i;
15650 struct symtab_index_entry **old_entries = symtab->data;
15651
15652 symtab->size *= 2;
15653 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
15654
15655 for (i = 0; i < old_size; ++i)
15656 {
15657 if (old_entries[i])
15658 {
15659 struct symtab_index_entry **slot = find_slot (symtab,
15660 old_entries[i]->name);
15661 *slot = old_entries[i];
15662 }
15663 }
15664
15665 xfree (old_entries);
15666 }
15667
15668 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
15669 is the index of the CU in which the symbol appears. */
15670
15671 static void
15672 add_index_entry (struct mapped_symtab *symtab, const char *name,
15673 offset_type cu_index)
15674 {
15675 struct symtab_index_entry **slot;
15676
15677 ++symtab->n_elements;
15678 if (4 * symtab->n_elements / 3 >= symtab->size)
15679 hash_expand (symtab);
15680
15681 slot = find_slot (symtab, name);
15682 if (!*slot)
15683 {
15684 *slot = XNEW (struct symtab_index_entry);
15685 (*slot)->name = name;
15686 (*slot)->cu_indices = NULL;
15687 }
15688 /* Don't push an index twice. Due to how we add entries we only
15689 have to check the last one. */
15690 if (VEC_empty (offset_type, (*slot)->cu_indices)
15691 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
15692 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
15693 }
15694
15695 /* Add a vector of indices to the constant pool. */
15696
15697 static offset_type
15698 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
15699 struct symtab_index_entry *entry)
15700 {
15701 void **slot;
15702
15703 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
15704 if (!*slot)
15705 {
15706 offset_type len = VEC_length (offset_type, entry->cu_indices);
15707 offset_type val = MAYBE_SWAP (len);
15708 offset_type iter;
15709 int i;
15710
15711 *slot = entry;
15712 entry->index_offset = obstack_object_size (cpool);
15713
15714 obstack_grow (cpool, &val, sizeof (val));
15715 for (i = 0;
15716 VEC_iterate (offset_type, entry->cu_indices, i, iter);
15717 ++i)
15718 {
15719 val = MAYBE_SWAP (iter);
15720 obstack_grow (cpool, &val, sizeof (val));
15721 }
15722 }
15723 else
15724 {
15725 struct symtab_index_entry *old_entry = *slot;
15726 entry->index_offset = old_entry->index_offset;
15727 entry = old_entry;
15728 }
15729 return entry->index_offset;
15730 }
15731
15732 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
15733 constant pool entries going into the obstack CPOOL. */
15734
15735 static void
15736 write_hash_table (struct mapped_symtab *symtab,
15737 struct obstack *output, struct obstack *cpool)
15738 {
15739 offset_type i;
15740 htab_t symbol_hash_table;
15741 htab_t str_table;
15742
15743 symbol_hash_table = create_symbol_hash_table ();
15744 str_table = create_strtab ();
15745
15746 /* We add all the index vectors to the constant pool first, to
15747 ensure alignment is ok. */
15748 for (i = 0; i < symtab->size; ++i)
15749 {
15750 if (symtab->data[i])
15751 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
15752 }
15753
15754 /* Now write out the hash table. */
15755 for (i = 0; i < symtab->size; ++i)
15756 {
15757 offset_type str_off, vec_off;
15758
15759 if (symtab->data[i])
15760 {
15761 str_off = add_string (str_table, cpool, symtab->data[i]->name);
15762 vec_off = symtab->data[i]->index_offset;
15763 }
15764 else
15765 {
15766 /* While 0 is a valid constant pool index, it is not valid
15767 to have 0 for both offsets. */
15768 str_off = 0;
15769 vec_off = 0;
15770 }
15771
15772 str_off = MAYBE_SWAP (str_off);
15773 vec_off = MAYBE_SWAP (vec_off);
15774
15775 obstack_grow (output, &str_off, sizeof (str_off));
15776 obstack_grow (output, &vec_off, sizeof (vec_off));
15777 }
15778
15779 htab_delete (str_table);
15780 htab_delete (symbol_hash_table);
15781 }
15782
15783 /* Struct to map psymtab to CU index in the index file. */
15784 struct psymtab_cu_index_map
15785 {
15786 struct partial_symtab *psymtab;
15787 unsigned int cu_index;
15788 };
15789
15790 static hashval_t
15791 hash_psymtab_cu_index (const void *item)
15792 {
15793 const struct psymtab_cu_index_map *map = item;
15794
15795 return htab_hash_pointer (map->psymtab);
15796 }
15797
15798 static int
15799 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
15800 {
15801 const struct psymtab_cu_index_map *lhs = item_lhs;
15802 const struct psymtab_cu_index_map *rhs = item_rhs;
15803
15804 return lhs->psymtab == rhs->psymtab;
15805 }
15806
15807 /* Helper struct for building the address table. */
15808 struct addrmap_index_data
15809 {
15810 struct objfile *objfile;
15811 struct obstack *addr_obstack;
15812 htab_t cu_index_htab;
15813
15814 /* Non-zero if the previous_* fields are valid.
15815 We can't write an entry until we see the next entry (since it is only then
15816 that we know the end of the entry). */
15817 int previous_valid;
15818 /* Index of the CU in the table of all CUs in the index file. */
15819 unsigned int previous_cu_index;
15820 /* Start address of the CU. */
15821 CORE_ADDR previous_cu_start;
15822 };
15823
15824 /* Write an address entry to OBSTACK. */
15825
15826 static void
15827 add_address_entry (struct objfile *objfile, struct obstack *obstack,
15828 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
15829 {
15830 offset_type cu_index_to_write;
15831 char addr[8];
15832 CORE_ADDR baseaddr;
15833
15834 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15835
15836 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
15837 obstack_grow (obstack, addr, 8);
15838 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
15839 obstack_grow (obstack, addr, 8);
15840 cu_index_to_write = MAYBE_SWAP (cu_index);
15841 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
15842 }
15843
15844 /* Worker function for traversing an addrmap to build the address table. */
15845
15846 static int
15847 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
15848 {
15849 struct addrmap_index_data *data = datap;
15850 struct partial_symtab *pst = obj;
15851 offset_type cu_index;
15852 void **slot;
15853
15854 if (data->previous_valid)
15855 add_address_entry (data->objfile, data->addr_obstack,
15856 data->previous_cu_start, start_addr,
15857 data->previous_cu_index);
15858
15859 data->previous_cu_start = start_addr;
15860 if (pst != NULL)
15861 {
15862 struct psymtab_cu_index_map find_map, *map;
15863 find_map.psymtab = pst;
15864 map = htab_find (data->cu_index_htab, &find_map);
15865 gdb_assert (map != NULL);
15866 data->previous_cu_index = map->cu_index;
15867 data->previous_valid = 1;
15868 }
15869 else
15870 data->previous_valid = 0;
15871
15872 return 0;
15873 }
15874
15875 /* Write OBJFILE's address map to OBSTACK.
15876 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
15877 in the index file. */
15878
15879 static void
15880 write_address_map (struct objfile *objfile, struct obstack *obstack,
15881 htab_t cu_index_htab)
15882 {
15883 struct addrmap_index_data addrmap_index_data;
15884
15885 /* When writing the address table, we have to cope with the fact that
15886 the addrmap iterator only provides the start of a region; we have to
15887 wait until the next invocation to get the start of the next region. */
15888
15889 addrmap_index_data.objfile = objfile;
15890 addrmap_index_data.addr_obstack = obstack;
15891 addrmap_index_data.cu_index_htab = cu_index_htab;
15892 addrmap_index_data.previous_valid = 0;
15893
15894 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
15895 &addrmap_index_data);
15896
15897 /* It's highly unlikely the last entry (end address = 0xff...ff)
15898 is valid, but we should still handle it.
15899 The end address is recorded as the start of the next region, but that
15900 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
15901 anyway. */
15902 if (addrmap_index_data.previous_valid)
15903 add_address_entry (objfile, obstack,
15904 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
15905 addrmap_index_data.previous_cu_index);
15906 }
15907
15908 /* Add a list of partial symbols to SYMTAB. */
15909
15910 static void
15911 write_psymbols (struct mapped_symtab *symtab,
15912 htab_t psyms_seen,
15913 struct partial_symbol **psymp,
15914 int count,
15915 offset_type cu_index,
15916 int is_static)
15917 {
15918 for (; count-- > 0; ++psymp)
15919 {
15920 void **slot, *lookup;
15921
15922 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
15923 error (_("Ada is not currently supported by the index"));
15924
15925 /* We only want to add a given psymbol once. However, we also
15926 want to account for whether it is global or static. So, we
15927 may add it twice, using slightly different values. */
15928 if (is_static)
15929 {
15930 uintptr_t val = 1 | (uintptr_t) *psymp;
15931
15932 lookup = (void *) val;
15933 }
15934 else
15935 lookup = *psymp;
15936
15937 /* Only add a given psymbol once. */
15938 slot = htab_find_slot (psyms_seen, lookup, INSERT);
15939 if (!*slot)
15940 {
15941 *slot = lookup;
15942 add_index_entry (symtab, SYMBOL_NATURAL_NAME (*psymp), cu_index);
15943 }
15944 }
15945 }
15946
15947 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
15948 exception if there is an error. */
15949
15950 static void
15951 write_obstack (FILE *file, struct obstack *obstack)
15952 {
15953 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
15954 file)
15955 != obstack_object_size (obstack))
15956 error (_("couldn't data write to file"));
15957 }
15958
15959 /* Unlink a file if the argument is not NULL. */
15960
15961 static void
15962 unlink_if_set (void *p)
15963 {
15964 char **filename = p;
15965 if (*filename)
15966 unlink (*filename);
15967 }
15968
15969 /* A helper struct used when iterating over debug_types. */
15970 struct signatured_type_index_data
15971 {
15972 struct objfile *objfile;
15973 struct mapped_symtab *symtab;
15974 struct obstack *types_list;
15975 htab_t psyms_seen;
15976 int cu_index;
15977 };
15978
15979 /* A helper function that writes a single signatured_type to an
15980 obstack. */
15981
15982 static int
15983 write_one_signatured_type (void **slot, void *d)
15984 {
15985 struct signatured_type_index_data *info = d;
15986 struct signatured_type *entry = (struct signatured_type *) *slot;
15987 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
15988 struct partial_symtab *psymtab = per_cu->v.psymtab;
15989 gdb_byte val[8];
15990
15991 write_psymbols (info->symtab,
15992 info->psyms_seen,
15993 info->objfile->global_psymbols.list
15994 + psymtab->globals_offset,
15995 psymtab->n_global_syms, info->cu_index,
15996 0);
15997 write_psymbols (info->symtab,
15998 info->psyms_seen,
15999 info->objfile->static_psymbols.list
16000 + psymtab->statics_offset,
16001 psymtab->n_static_syms, info->cu_index,
16002 1);
16003
16004 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16005 obstack_grow (info->types_list, val, 8);
16006 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16007 obstack_grow (info->types_list, val, 8);
16008 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16009 obstack_grow (info->types_list, val, 8);
16010
16011 ++info->cu_index;
16012
16013 return 1;
16014 }
16015
16016 /* A cleanup function for an htab_t. */
16017
16018 static void
16019 cleanup_htab (void *arg)
16020 {
16021 htab_delete (arg);
16022 }
16023
16024 /* Create an index file for OBJFILE in the directory DIR. */
16025
16026 static void
16027 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16028 {
16029 struct cleanup *cleanup;
16030 char *filename, *cleanup_filename;
16031 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16032 struct obstack cu_list, types_cu_list;
16033 int i;
16034 FILE *out_file;
16035 struct mapped_symtab *symtab;
16036 offset_type val, size_of_contents, total_len;
16037 struct stat st;
16038 char buf[8];
16039 htab_t psyms_seen;
16040 htab_t cu_index_htab;
16041 struct psymtab_cu_index_map *psymtab_cu_index_map;
16042
16043 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16044 return;
16045
16046 if (dwarf2_per_objfile->using_index)
16047 error (_("Cannot use an index to create the index"));
16048
16049 if (stat (objfile->name, &st) < 0)
16050 perror_with_name (objfile->name);
16051
16052 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16053 INDEX_SUFFIX, (char *) NULL);
16054 cleanup = make_cleanup (xfree, filename);
16055
16056 out_file = fopen (filename, "wb");
16057 if (!out_file)
16058 error (_("Can't open `%s' for writing"), filename);
16059
16060 cleanup_filename = filename;
16061 make_cleanup (unlink_if_set, &cleanup_filename);
16062
16063 symtab = create_mapped_symtab ();
16064 make_cleanup (cleanup_mapped_symtab, symtab);
16065
16066 obstack_init (&addr_obstack);
16067 make_cleanup_obstack_free (&addr_obstack);
16068
16069 obstack_init (&cu_list);
16070 make_cleanup_obstack_free (&cu_list);
16071
16072 obstack_init (&types_cu_list);
16073 make_cleanup_obstack_free (&types_cu_list);
16074
16075 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16076 NULL, xcalloc, xfree);
16077 make_cleanup (cleanup_htab, psyms_seen);
16078
16079 /* While we're scanning CU's create a table that maps a psymtab pointer
16080 (which is what addrmap records) to its index (which is what is recorded
16081 in the index file). This will later be needed to write the address
16082 table. */
16083 cu_index_htab = htab_create_alloc (100,
16084 hash_psymtab_cu_index,
16085 eq_psymtab_cu_index,
16086 NULL, xcalloc, xfree);
16087 make_cleanup (cleanup_htab, cu_index_htab);
16088 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
16089 xmalloc (sizeof (struct psymtab_cu_index_map)
16090 * dwarf2_per_objfile->n_comp_units);
16091 make_cleanup (xfree, psymtab_cu_index_map);
16092
16093 /* The CU list is already sorted, so we don't need to do additional
16094 work here. Also, the debug_types entries do not appear in
16095 all_comp_units, but only in their own hash table. */
16096 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
16097 {
16098 struct dwarf2_per_cu_data *per_cu
16099 = dwarf2_per_objfile->all_comp_units[i];
16100 struct partial_symtab *psymtab = per_cu->v.psymtab;
16101 gdb_byte val[8];
16102 struct psymtab_cu_index_map *map;
16103 void **slot;
16104
16105 write_psymbols (symtab,
16106 psyms_seen,
16107 objfile->global_psymbols.list + psymtab->globals_offset,
16108 psymtab->n_global_syms, i,
16109 0);
16110 write_psymbols (symtab,
16111 psyms_seen,
16112 objfile->static_psymbols.list + psymtab->statics_offset,
16113 psymtab->n_static_syms, i,
16114 1);
16115
16116 map = &psymtab_cu_index_map[i];
16117 map->psymtab = psymtab;
16118 map->cu_index = i;
16119 slot = htab_find_slot (cu_index_htab, map, INSERT);
16120 gdb_assert (slot != NULL);
16121 gdb_assert (*slot == NULL);
16122 *slot = map;
16123
16124 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
16125 obstack_grow (&cu_list, val, 8);
16126 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
16127 obstack_grow (&cu_list, val, 8);
16128 }
16129
16130 /* Dump the address map. */
16131 write_address_map (objfile, &addr_obstack, cu_index_htab);
16132
16133 /* Write out the .debug_type entries, if any. */
16134 if (dwarf2_per_objfile->signatured_types)
16135 {
16136 struct signatured_type_index_data sig_data;
16137
16138 sig_data.objfile = objfile;
16139 sig_data.symtab = symtab;
16140 sig_data.types_list = &types_cu_list;
16141 sig_data.psyms_seen = psyms_seen;
16142 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
16143 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
16144 write_one_signatured_type, &sig_data);
16145 }
16146
16147 obstack_init (&constant_pool);
16148 make_cleanup_obstack_free (&constant_pool);
16149 obstack_init (&symtab_obstack);
16150 make_cleanup_obstack_free (&symtab_obstack);
16151 write_hash_table (symtab, &symtab_obstack, &constant_pool);
16152
16153 obstack_init (&contents);
16154 make_cleanup_obstack_free (&contents);
16155 size_of_contents = 6 * sizeof (offset_type);
16156 total_len = size_of_contents;
16157
16158 /* The version number. */
16159 val = MAYBE_SWAP (5);
16160 obstack_grow (&contents, &val, sizeof (val));
16161
16162 /* The offset of the CU list from the start of the file. */
16163 val = MAYBE_SWAP (total_len);
16164 obstack_grow (&contents, &val, sizeof (val));
16165 total_len += obstack_object_size (&cu_list);
16166
16167 /* The offset of the types CU list from the start of the file. */
16168 val = MAYBE_SWAP (total_len);
16169 obstack_grow (&contents, &val, sizeof (val));
16170 total_len += obstack_object_size (&types_cu_list);
16171
16172 /* The offset of the address table from the start of the file. */
16173 val = MAYBE_SWAP (total_len);
16174 obstack_grow (&contents, &val, sizeof (val));
16175 total_len += obstack_object_size (&addr_obstack);
16176
16177 /* The offset of the symbol table from the start of the file. */
16178 val = MAYBE_SWAP (total_len);
16179 obstack_grow (&contents, &val, sizeof (val));
16180 total_len += obstack_object_size (&symtab_obstack);
16181
16182 /* The offset of the constant pool from the start of the file. */
16183 val = MAYBE_SWAP (total_len);
16184 obstack_grow (&contents, &val, sizeof (val));
16185 total_len += obstack_object_size (&constant_pool);
16186
16187 gdb_assert (obstack_object_size (&contents) == size_of_contents);
16188
16189 write_obstack (out_file, &contents);
16190 write_obstack (out_file, &cu_list);
16191 write_obstack (out_file, &types_cu_list);
16192 write_obstack (out_file, &addr_obstack);
16193 write_obstack (out_file, &symtab_obstack);
16194 write_obstack (out_file, &constant_pool);
16195
16196 fclose (out_file);
16197
16198 /* We want to keep the file, so we set cleanup_filename to NULL
16199 here. See unlink_if_set. */
16200 cleanup_filename = NULL;
16201
16202 do_cleanups (cleanup);
16203 }
16204
16205 /* Implementation of the `save gdb-index' command.
16206
16207 Note that the file format used by this command is documented in the
16208 GDB manual. Any changes here must be documented there. */
16209
16210 static void
16211 save_gdb_index_command (char *arg, int from_tty)
16212 {
16213 struct objfile *objfile;
16214
16215 if (!arg || !*arg)
16216 error (_("usage: save gdb-index DIRECTORY"));
16217
16218 ALL_OBJFILES (objfile)
16219 {
16220 struct stat st;
16221
16222 /* If the objfile does not correspond to an actual file, skip it. */
16223 if (stat (objfile->name, &st) < 0)
16224 continue;
16225
16226 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16227 if (dwarf2_per_objfile)
16228 {
16229 volatile struct gdb_exception except;
16230
16231 TRY_CATCH (except, RETURN_MASK_ERROR)
16232 {
16233 write_psymtabs_to_index (objfile, arg);
16234 }
16235 if (except.reason < 0)
16236 exception_fprintf (gdb_stderr, except,
16237 _("Error while writing index for `%s': "),
16238 objfile->name);
16239 }
16240 }
16241 }
16242
16243 \f
16244
16245 int dwarf2_always_disassemble;
16246
16247 static void
16248 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
16249 struct cmd_list_element *c, const char *value)
16250 {
16251 fprintf_filtered (file,
16252 _("Whether to always disassemble "
16253 "DWARF expressions is %s.\n"),
16254 value);
16255 }
16256
16257 void _initialize_dwarf2_read (void);
16258
16259 void
16260 _initialize_dwarf2_read (void)
16261 {
16262 struct cmd_list_element *c;
16263
16264 dwarf2_objfile_data_key
16265 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
16266
16267 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
16268 Set DWARF 2 specific variables.\n\
16269 Configure DWARF 2 variables such as the cache size"),
16270 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
16271 0/*allow-unknown*/, &maintenance_set_cmdlist);
16272
16273 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
16274 Show DWARF 2 specific variables\n\
16275 Show DWARF 2 variables such as the cache size"),
16276 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
16277 0/*allow-unknown*/, &maintenance_show_cmdlist);
16278
16279 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
16280 &dwarf2_max_cache_age, _("\
16281 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
16282 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
16283 A higher limit means that cached compilation units will be stored\n\
16284 in memory longer, and more total memory will be used. Zero disables\n\
16285 caching, which can slow down startup."),
16286 NULL,
16287 show_dwarf2_max_cache_age,
16288 &set_dwarf2_cmdlist,
16289 &show_dwarf2_cmdlist);
16290
16291 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
16292 &dwarf2_always_disassemble, _("\
16293 Set whether `info address' always disassembles DWARF expressions."), _("\
16294 Show whether `info address' always disassembles DWARF expressions."), _("\
16295 When enabled, DWARF expressions are always printed in an assembly-like\n\
16296 syntax. When disabled, expressions will be printed in a more\n\
16297 conversational style, when possible."),
16298 NULL,
16299 show_dwarf2_always_disassemble,
16300 &set_dwarf2_cmdlist,
16301 &show_dwarf2_cmdlist);
16302
16303 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
16304 Set debugging of the dwarf2 DIE reader."), _("\
16305 Show debugging of the dwarf2 DIE reader."), _("\
16306 When enabled (non-zero), DIEs are dumped after they are read in.\n\
16307 The value is the maximum depth to print."),
16308 NULL,
16309 NULL,
16310 &setdebuglist, &showdebuglist);
16311
16312 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
16313 _("\
16314 Save a gdb-index file.\n\
16315 Usage: save gdb-index DIRECTORY"),
16316 &save_cmdlist);
16317 set_cmd_completer (c, filename_completer);
16318 }
This page took 0.396921 seconds and 4 git commands to generate.