gdb:
[binutils-gdb.git] / gdb / dwarf2read.c
1 /* DWARF 2 debugging format support for GDB.
2
3 Copyright (C) 1994-2012 Free Software Foundation, Inc.
4
5 Adapted by Gary Funck (gary@intrepid.com), Intrepid Technology,
6 Inc. with support from Florida State University (under contract
7 with the Ada Joint Program Office), and Silicon Graphics, Inc.
8 Initial contribution by Brent Benson, Harris Computer Systems, Inc.,
9 based on Fred Fish's (Cygnus Support) implementation of DWARF 1
10 support.
11
12 This file is part of GDB.
13
14 This program is free software; you can redistribute it and/or modify
15 it under the terms of the GNU General Public License as published by
16 the Free Software Foundation; either version 3 of the License, or
17 (at your option) any later version.
18
19 This program is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
23
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>. */
26
27 /* FIXME: Various die-reading functions need to be more careful with
28 reading off the end of the section.
29 E.g., load_partial_dies, read_partial_die. */
30
31 #include "defs.h"
32 #include "bfd.h"
33 #include "symtab.h"
34 #include "gdbtypes.h"
35 #include "objfiles.h"
36 #include "dwarf2.h"
37 #include "buildsym.h"
38 #include "demangle.h"
39 #include "gdb-demangle.h"
40 #include "expression.h"
41 #include "filenames.h" /* for DOSish file names */
42 #include "macrotab.h"
43 #include "language.h"
44 #include "complaints.h"
45 #include "bcache.h"
46 #include "dwarf2expr.h"
47 #include "dwarf2loc.h"
48 #include "cp-support.h"
49 #include "hashtab.h"
50 #include "command.h"
51 #include "gdbcmd.h"
52 #include "block.h"
53 #include "addrmap.h"
54 #include "typeprint.h"
55 #include "jv-lang.h"
56 #include "psympriv.h"
57 #include "exceptions.h"
58 #include "gdb_stat.h"
59 #include "completer.h"
60 #include "vec.h"
61 #include "c-lang.h"
62 #include "valprint.h"
63 #include <ctype.h>
64
65 #include <fcntl.h>
66 #include "gdb_string.h"
67 #include "gdb_assert.h"
68 #include <sys/types.h>
69 #ifdef HAVE_ZLIB_H
70 #include <zlib.h>
71 #endif
72 #ifdef HAVE_MMAP
73 #include <sys/mman.h>
74 #ifndef MAP_FAILED
75 #define MAP_FAILED ((void *) -1)
76 #endif
77 #endif
78
79 typedef struct symbol *symbolp;
80 DEF_VEC_P (symbolp);
81
82 /* When non-zero, dump DIEs after they are read in. */
83 static int dwarf2_die_debug = 0;
84
85 /* When non-zero, cross-check physname against demangler. */
86 static int check_physname = 0;
87
88 /* When non-zero, do not reject deprecated .gdb_index sections. */
89 int use_deprecated_index_sections = 0;
90
91 static int pagesize;
92
93 /* When set, the file that we're processing is known to have debugging
94 info for C++ namespaces. GCC 3.3.x did not produce this information,
95 but later versions do. */
96
97 static int processing_has_namespace_info;
98
99 static const struct objfile_data *dwarf2_objfile_data_key;
100
101 struct dwarf2_section_info
102 {
103 asection *asection;
104 gdb_byte *buffer;
105 bfd_size_type size;
106 /* Not NULL if the section was actually mmapped. */
107 void *map_addr;
108 /* Page aligned size of mmapped area. */
109 bfd_size_type map_len;
110 /* True if we have tried to read this section. */
111 int readin;
112 };
113
114 typedef struct dwarf2_section_info dwarf2_section_info_def;
115 DEF_VEC_O (dwarf2_section_info_def);
116
117 /* All offsets in the index are of this type. It must be
118 architecture-independent. */
119 typedef uint32_t offset_type;
120
121 DEF_VEC_I (offset_type);
122
123 /* A description of the mapped index. The file format is described in
124 a comment by the code that writes the index. */
125 struct mapped_index
126 {
127 /* Index data format version. */
128 int version;
129
130 /* The total length of the buffer. */
131 off_t total_size;
132
133 /* A pointer to the address table data. */
134 const gdb_byte *address_table;
135
136 /* Size of the address table data in bytes. */
137 offset_type address_table_size;
138
139 /* The symbol table, implemented as a hash table. */
140 const offset_type *symbol_table;
141
142 /* Size in slots, each slot is 2 offset_types. */
143 offset_type symbol_table_slots;
144
145 /* A pointer to the constant pool. */
146 const char *constant_pool;
147 };
148
149 /* Collection of data recorded per objfile.
150 This hangs off of dwarf2_objfile_data_key. */
151
152 struct dwarf2_per_objfile
153 {
154 struct dwarf2_section_info info;
155 struct dwarf2_section_info abbrev;
156 struct dwarf2_section_info line;
157 struct dwarf2_section_info loc;
158 struct dwarf2_section_info macinfo;
159 struct dwarf2_section_info macro;
160 struct dwarf2_section_info str;
161 struct dwarf2_section_info ranges;
162 struct dwarf2_section_info frame;
163 struct dwarf2_section_info eh_frame;
164 struct dwarf2_section_info gdb_index;
165
166 VEC (dwarf2_section_info_def) *types;
167
168 /* Back link. */
169 struct objfile *objfile;
170
171 /* Table of all the compilation units. This is used to locate
172 the target compilation unit of a particular reference. */
173 struct dwarf2_per_cu_data **all_comp_units;
174
175 /* The number of compilation units in ALL_COMP_UNITS. */
176 int n_comp_units;
177
178 /* The number of .debug_types-related CUs. */
179 int n_type_units;
180
181 /* The .debug_types-related CUs (TUs). */
182 struct dwarf2_per_cu_data **all_type_units;
183
184 /* A chain of compilation units that are currently read in, so that
185 they can be freed later. */
186 struct dwarf2_per_cu_data *read_in_chain;
187
188 /* A table mapping .debug_types signatures to its signatured_type entry.
189 This is NULL if the .debug_types section hasn't been read in yet. */
190 htab_t signatured_types;
191
192 /* A flag indicating wether this objfile has a section loaded at a
193 VMA of 0. */
194 int has_section_at_zero;
195
196 /* True if we are using the mapped index,
197 or we are faking it for OBJF_READNOW's sake. */
198 unsigned char using_index;
199
200 /* The mapped index, or NULL if .gdb_index is missing or not being used. */
201 struct mapped_index *index_table;
202
203 /* When using index_table, this keeps track of all quick_file_names entries.
204 TUs can share line table entries with CUs or other TUs, and there can be
205 a lot more TUs than unique line tables, so we maintain a separate table
206 of all line table entries to support the sharing. */
207 htab_t quick_file_names_table;
208
209 /* Set during partial symbol reading, to prevent queueing of full
210 symbols. */
211 int reading_partial_symbols;
212
213 /* Table mapping type .debug_info DIE offsets to types.
214 This is NULL if not allocated yet.
215 It (currently) makes sense to allocate debug_types_type_hash lazily.
216 To keep things simple we allocate both lazily. */
217 htab_t debug_info_type_hash;
218
219 /* Table mapping type .debug_types DIE offsets to types.
220 This is NULL if not allocated yet. */
221 htab_t debug_types_type_hash;
222 };
223
224 static struct dwarf2_per_objfile *dwarf2_per_objfile;
225
226 /* Default names of the debugging sections. */
227
228 /* Note that if the debugging section has been compressed, it might
229 have a name like .zdebug_info. */
230
231 static const struct dwarf2_debug_sections dwarf2_elf_names =
232 {
233 { ".debug_info", ".zdebug_info" },
234 { ".debug_abbrev", ".zdebug_abbrev" },
235 { ".debug_line", ".zdebug_line" },
236 { ".debug_loc", ".zdebug_loc" },
237 { ".debug_macinfo", ".zdebug_macinfo" },
238 { ".debug_macro", ".zdebug_macro" },
239 { ".debug_str", ".zdebug_str" },
240 { ".debug_ranges", ".zdebug_ranges" },
241 { ".debug_types", ".zdebug_types" },
242 { ".debug_frame", ".zdebug_frame" },
243 { ".eh_frame", NULL },
244 { ".gdb_index", ".zgdb_index" },
245 23
246 };
247
248 /* local data types */
249
250 /* We hold several abbreviation tables in memory at the same time. */
251 #ifndef ABBREV_HASH_SIZE
252 #define ABBREV_HASH_SIZE 121
253 #endif
254
255 /* The data in a compilation unit header, after target2host
256 translation, looks like this. */
257 struct comp_unit_head
258 {
259 unsigned int length;
260 short version;
261 unsigned char addr_size;
262 unsigned char signed_addr_p;
263 unsigned int abbrev_offset;
264
265 /* Size of file offsets; either 4 or 8. */
266 unsigned int offset_size;
267
268 /* Size of the length field; either 4 or 12. */
269 unsigned int initial_length_size;
270
271 /* Offset to the first byte of this compilation unit header in the
272 .debug_info section, for resolving relative reference dies. */
273 unsigned int offset;
274
275 /* Offset to first die in this cu from the start of the cu.
276 This will be the first byte following the compilation unit header. */
277 unsigned int first_die_offset;
278 };
279
280 /* Type used for delaying computation of method physnames.
281 See comments for compute_delayed_physnames. */
282 struct delayed_method_info
283 {
284 /* The type to which the method is attached, i.e., its parent class. */
285 struct type *type;
286
287 /* The index of the method in the type's function fieldlists. */
288 int fnfield_index;
289
290 /* The index of the method in the fieldlist. */
291 int index;
292
293 /* The name of the DIE. */
294 const char *name;
295
296 /* The DIE associated with this method. */
297 struct die_info *die;
298 };
299
300 typedef struct delayed_method_info delayed_method_info;
301 DEF_VEC_O (delayed_method_info);
302
303 /* Internal state when decoding a particular compilation unit. */
304 struct dwarf2_cu
305 {
306 /* The objfile containing this compilation unit. */
307 struct objfile *objfile;
308
309 /* The header of the compilation unit. */
310 struct comp_unit_head header;
311
312 /* Base address of this compilation unit. */
313 CORE_ADDR base_address;
314
315 /* Non-zero if base_address has been set. */
316 int base_known;
317
318 /* The language we are debugging. */
319 enum language language;
320 const struct language_defn *language_defn;
321
322 const char *producer;
323
324 /* The generic symbol table building routines have separate lists for
325 file scope symbols and all all other scopes (local scopes). So
326 we need to select the right one to pass to add_symbol_to_list().
327 We do it by keeping a pointer to the correct list in list_in_scope.
328
329 FIXME: The original dwarf code just treated the file scope as the
330 first local scope, and all other local scopes as nested local
331 scopes, and worked fine. Check to see if we really need to
332 distinguish these in buildsym.c. */
333 struct pending **list_in_scope;
334
335 /* DWARF abbreviation table associated with this compilation unit. */
336 struct abbrev_info **dwarf2_abbrevs;
337
338 /* Storage for the abbrev table. */
339 struct obstack abbrev_obstack;
340
341 /* Hash table holding all the loaded partial DIEs. */
342 htab_t partial_dies;
343
344 /* Storage for things with the same lifetime as this read-in compilation
345 unit, including partial DIEs. */
346 struct obstack comp_unit_obstack;
347
348 /* When multiple dwarf2_cu structures are living in memory, this field
349 chains them all together, so that they can be released efficiently.
350 We will probably also want a generation counter so that most-recently-used
351 compilation units are cached... */
352 struct dwarf2_per_cu_data *read_in_chain;
353
354 /* Backchain to our per_cu entry if the tree has been built. */
355 struct dwarf2_per_cu_data *per_cu;
356
357 /* How many compilation units ago was this CU last referenced? */
358 int last_used;
359
360 /* A hash table of die offsets for following references. */
361 htab_t die_hash;
362
363 /* Full DIEs if read in. */
364 struct die_info *dies;
365
366 /* A set of pointers to dwarf2_per_cu_data objects for compilation
367 units referenced by this one. Only set during full symbol processing;
368 partial symbol tables do not have dependencies. */
369 htab_t dependencies;
370
371 /* Header data from the line table, during full symbol processing. */
372 struct line_header *line_header;
373
374 /* A list of methods which need to have physnames computed
375 after all type information has been read. */
376 VEC (delayed_method_info) *method_list;
377
378 /* To be copied to symtab->call_site_htab. */
379 htab_t call_site_htab;
380
381 /* Mark used when releasing cached dies. */
382 unsigned int mark : 1;
383
384 /* This CU references .debug_loc. See the symtab->locations_valid field.
385 This test is imperfect as there may exist optimized debug code not using
386 any location list and still facing inlining issues if handled as
387 unoptimized code. For a future better test see GCC PR other/32998. */
388 unsigned int has_loclist : 1;
389
390 /* These cache the results of producer_is_gxx_lt_4_6.
391 CHECKED_PRODUCER is set if PRODUCER_IS_GXX_LT_4_6 is valid. This
392 information is cached because profiling CU expansion showed
393 excessive time spent in producer_is_gxx_lt_4_6. */
394 unsigned int checked_producer : 1;
395 unsigned int producer_is_gxx_lt_4_6 : 1;
396 };
397
398 /* Persistent data held for a compilation unit, even when not
399 processing it. We put a pointer to this structure in the
400 read_symtab_private field of the psymtab. */
401
402 struct dwarf2_per_cu_data
403 {
404 /* The start offset and length of this compilation unit. 2**29-1
405 bytes should suffice to store the length of any compilation unit
406 - if it doesn't, GDB will fall over anyway.
407 NOTE: Unlike comp_unit_head.length, this length includes
408 initial_length_size. */
409 unsigned int offset;
410 unsigned int length : 29;
411
412 /* Flag indicating this compilation unit will be read in before
413 any of the current compilation units are processed. */
414 unsigned int queued : 1;
415
416 /* This flag will be set if we need to load absolutely all DIEs
417 for this compilation unit, instead of just the ones we think
418 are interesting. It gets set if we look for a DIE in the
419 hash table and don't find it. */
420 unsigned int load_all_dies : 1;
421
422 /* Non-null if this CU is from .debug_types; in which case it points
423 to the section. Otherwise it's from .debug_info. */
424 struct dwarf2_section_info *debug_types_section;
425
426 /* Set to non-NULL iff this CU is currently loaded. When it gets freed out
427 of the CU cache it gets reset to NULL again. */
428 struct dwarf2_cu *cu;
429
430 /* The corresponding objfile.
431 Normally we can get the objfile from dwarf2_per_objfile.
432 However we can enter this file with just a "per_cu" handle. */
433 struct objfile *objfile;
434
435 /* When using partial symbol tables, the 'psymtab' field is active.
436 Otherwise the 'quick' field is active. */
437 union
438 {
439 /* The partial symbol table associated with this compilation unit,
440 or NULL for partial units (which do not have an associated
441 symtab). */
442 struct partial_symtab *psymtab;
443
444 /* Data needed by the "quick" functions. */
445 struct dwarf2_per_cu_quick_data *quick;
446 } v;
447 };
448
449 /* Entry in the signatured_types hash table. */
450
451 struct signatured_type
452 {
453 ULONGEST signature;
454
455 /* Offset in .debug_types of the type defined by this TU. */
456 unsigned int type_offset;
457
458 /* The CU(/TU) of this type. */
459 struct dwarf2_per_cu_data per_cu;
460 };
461
462 /* Struct used to pass misc. parameters to read_die_and_children, et
463 al. which are used for both .debug_info and .debug_types dies.
464 All parameters here are unchanging for the life of the call. This
465 struct exists to abstract away the constant parameters of die
466 reading. */
467
468 struct die_reader_specs
469 {
470 /* The bfd of this objfile. */
471 bfd* abfd;
472
473 /* The CU of the DIE we are parsing. */
474 struct dwarf2_cu *cu;
475
476 /* Pointer to start of section buffer.
477 This is either the start of .debug_info or .debug_types. */
478 const gdb_byte *buffer;
479 };
480
481 /* The line number information for a compilation unit (found in the
482 .debug_line section) begins with a "statement program header",
483 which contains the following information. */
484 struct line_header
485 {
486 unsigned int total_length;
487 unsigned short version;
488 unsigned int header_length;
489 unsigned char minimum_instruction_length;
490 unsigned char maximum_ops_per_instruction;
491 unsigned char default_is_stmt;
492 int line_base;
493 unsigned char line_range;
494 unsigned char opcode_base;
495
496 /* standard_opcode_lengths[i] is the number of operands for the
497 standard opcode whose value is i. This means that
498 standard_opcode_lengths[0] is unused, and the last meaningful
499 element is standard_opcode_lengths[opcode_base - 1]. */
500 unsigned char *standard_opcode_lengths;
501
502 /* The include_directories table. NOTE! These strings are not
503 allocated with xmalloc; instead, they are pointers into
504 debug_line_buffer. If you try to free them, `free' will get
505 indigestion. */
506 unsigned int num_include_dirs, include_dirs_size;
507 char **include_dirs;
508
509 /* The file_names table. NOTE! These strings are not allocated
510 with xmalloc; instead, they are pointers into debug_line_buffer.
511 Don't try to free them directly. */
512 unsigned int num_file_names, file_names_size;
513 struct file_entry
514 {
515 char *name;
516 unsigned int dir_index;
517 unsigned int mod_time;
518 unsigned int length;
519 int included_p; /* Non-zero if referenced by the Line Number Program. */
520 struct symtab *symtab; /* The associated symbol table, if any. */
521 } *file_names;
522
523 /* The start and end of the statement program following this
524 header. These point into dwarf2_per_objfile->line_buffer. */
525 gdb_byte *statement_program_start, *statement_program_end;
526 };
527
528 /* When we construct a partial symbol table entry we only
529 need this much information. */
530 struct partial_die_info
531 {
532 /* Offset of this DIE. */
533 unsigned int offset;
534
535 /* DWARF-2 tag for this DIE. */
536 ENUM_BITFIELD(dwarf_tag) tag : 16;
537
538 /* Assorted flags describing the data found in this DIE. */
539 unsigned int has_children : 1;
540 unsigned int is_external : 1;
541 unsigned int is_declaration : 1;
542 unsigned int has_type : 1;
543 unsigned int has_specification : 1;
544 unsigned int has_pc_info : 1;
545 unsigned int may_be_inlined : 1;
546
547 /* Flag set if the SCOPE field of this structure has been
548 computed. */
549 unsigned int scope_set : 1;
550
551 /* Flag set if the DIE has a byte_size attribute. */
552 unsigned int has_byte_size : 1;
553
554 /* Flag set if any of the DIE's children are template arguments. */
555 unsigned int has_template_arguments : 1;
556
557 /* Flag set if fixup_partial_die has been called on this die. */
558 unsigned int fixup_called : 1;
559
560 /* The name of this DIE. Normally the value of DW_AT_name, but
561 sometimes a default name for unnamed DIEs. */
562 char *name;
563
564 /* The linkage name, if present. */
565 const char *linkage_name;
566
567 /* The scope to prepend to our children. This is generally
568 allocated on the comp_unit_obstack, so will disappear
569 when this compilation unit leaves the cache. */
570 char *scope;
571
572 /* The location description associated with this DIE, if any. */
573 struct dwarf_block *locdesc;
574
575 /* If HAS_PC_INFO, the PC range associated with this DIE. */
576 CORE_ADDR lowpc;
577 CORE_ADDR highpc;
578
579 /* Pointer into the info_buffer (or types_buffer) pointing at the target of
580 DW_AT_sibling, if any. */
581 /* NOTE: This member isn't strictly necessary, read_partial_die could
582 return DW_AT_sibling values to its caller load_partial_dies. */
583 gdb_byte *sibling;
584
585 /* If HAS_SPECIFICATION, the offset of the DIE referred to by
586 DW_AT_specification (or DW_AT_abstract_origin or
587 DW_AT_extension). */
588 unsigned int spec_offset;
589
590 /* Pointers to this DIE's parent, first child, and next sibling,
591 if any. */
592 struct partial_die_info *die_parent, *die_child, *die_sibling;
593 };
594
595 /* This data structure holds the information of an abbrev. */
596 struct abbrev_info
597 {
598 unsigned int number; /* number identifying abbrev */
599 enum dwarf_tag tag; /* dwarf tag */
600 unsigned short has_children; /* boolean */
601 unsigned short num_attrs; /* number of attributes */
602 struct attr_abbrev *attrs; /* an array of attribute descriptions */
603 struct abbrev_info *next; /* next in chain */
604 };
605
606 struct attr_abbrev
607 {
608 ENUM_BITFIELD(dwarf_attribute) name : 16;
609 ENUM_BITFIELD(dwarf_form) form : 16;
610 };
611
612 /* Attributes have a name and a value. */
613 struct attribute
614 {
615 ENUM_BITFIELD(dwarf_attribute) name : 16;
616 ENUM_BITFIELD(dwarf_form) form : 15;
617
618 /* Has DW_STRING already been updated by dwarf2_canonicalize_name? This
619 field should be in u.str (existing only for DW_STRING) but it is kept
620 here for better struct attribute alignment. */
621 unsigned int string_is_canonical : 1;
622
623 union
624 {
625 char *str;
626 struct dwarf_block *blk;
627 ULONGEST unsnd;
628 LONGEST snd;
629 CORE_ADDR addr;
630 struct signatured_type *signatured_type;
631 }
632 u;
633 };
634
635 /* This data structure holds a complete die structure. */
636 struct die_info
637 {
638 /* DWARF-2 tag for this DIE. */
639 ENUM_BITFIELD(dwarf_tag) tag : 16;
640
641 /* Number of attributes */
642 unsigned char num_attrs;
643
644 /* True if we're presently building the full type name for the
645 type derived from this DIE. */
646 unsigned char building_fullname : 1;
647
648 /* Abbrev number */
649 unsigned int abbrev;
650
651 /* Offset in .debug_info or .debug_types section. */
652 unsigned int offset;
653
654 /* The dies in a compilation unit form an n-ary tree. PARENT
655 points to this die's parent; CHILD points to the first child of
656 this node; and all the children of a given node are chained
657 together via their SIBLING fields. */
658 struct die_info *child; /* Its first child, if any. */
659 struct die_info *sibling; /* Its next sibling, if any. */
660 struct die_info *parent; /* Its parent, if any. */
661
662 /* An array of attributes, with NUM_ATTRS elements. There may be
663 zero, but it's not common and zero-sized arrays are not
664 sufficiently portable C. */
665 struct attribute attrs[1];
666 };
667
668 /* Get at parts of an attribute structure. */
669
670 #define DW_STRING(attr) ((attr)->u.str)
671 #define DW_STRING_IS_CANONICAL(attr) ((attr)->string_is_canonical)
672 #define DW_UNSND(attr) ((attr)->u.unsnd)
673 #define DW_BLOCK(attr) ((attr)->u.blk)
674 #define DW_SND(attr) ((attr)->u.snd)
675 #define DW_ADDR(attr) ((attr)->u.addr)
676 #define DW_SIGNATURED_TYPE(attr) ((attr)->u.signatured_type)
677
678 /* Blocks are a bunch of untyped bytes. */
679 struct dwarf_block
680 {
681 unsigned int size;
682
683 /* Valid only if SIZE is not zero. */
684 gdb_byte *data;
685 };
686
687 #ifndef ATTR_ALLOC_CHUNK
688 #define ATTR_ALLOC_CHUNK 4
689 #endif
690
691 /* Allocate fields for structs, unions and enums in this size. */
692 #ifndef DW_FIELD_ALLOC_CHUNK
693 #define DW_FIELD_ALLOC_CHUNK 4
694 #endif
695
696 /* FIXME: We might want to set this from BFD via bfd_arch_bits_per_byte,
697 but this would require a corresponding change in unpack_field_as_long
698 and friends. */
699 static int bits_per_byte = 8;
700
701 /* The routines that read and process dies for a C struct or C++ class
702 pass lists of data member fields and lists of member function fields
703 in an instance of a field_info structure, as defined below. */
704 struct field_info
705 {
706 /* List of data member and baseclasses fields. */
707 struct nextfield
708 {
709 struct nextfield *next;
710 int accessibility;
711 int virtuality;
712 struct field field;
713 }
714 *fields, *baseclasses;
715
716 /* Number of fields (including baseclasses). */
717 int nfields;
718
719 /* Number of baseclasses. */
720 int nbaseclasses;
721
722 /* Set if the accesibility of one of the fields is not public. */
723 int non_public_fields;
724
725 /* Member function fields array, entries are allocated in the order they
726 are encountered in the object file. */
727 struct nextfnfield
728 {
729 struct nextfnfield *next;
730 struct fn_field fnfield;
731 }
732 *fnfields;
733
734 /* Member function fieldlist array, contains name of possibly overloaded
735 member function, number of overloaded member functions and a pointer
736 to the head of the member function field chain. */
737 struct fnfieldlist
738 {
739 char *name;
740 int length;
741 struct nextfnfield *head;
742 }
743 *fnfieldlists;
744
745 /* Number of entries in the fnfieldlists array. */
746 int nfnfields;
747
748 /* typedefs defined inside this class. TYPEDEF_FIELD_LIST contains head of
749 a NULL terminated list of TYPEDEF_FIELD_LIST_COUNT elements. */
750 struct typedef_field_list
751 {
752 struct typedef_field field;
753 struct typedef_field_list *next;
754 }
755 *typedef_field_list;
756 unsigned typedef_field_list_count;
757 };
758
759 /* One item on the queue of compilation units to read in full symbols
760 for. */
761 struct dwarf2_queue_item
762 {
763 struct dwarf2_per_cu_data *per_cu;
764 struct dwarf2_queue_item *next;
765 };
766
767 /* The current queue. */
768 static struct dwarf2_queue_item *dwarf2_queue, *dwarf2_queue_tail;
769
770 /* Loaded secondary compilation units are kept in memory until they
771 have not been referenced for the processing of this many
772 compilation units. Set this to zero to disable caching. Cache
773 sizes of up to at least twenty will improve startup time for
774 typical inter-CU-reference binaries, at an obvious memory cost. */
775 static int dwarf2_max_cache_age = 5;
776 static void
777 show_dwarf2_max_cache_age (struct ui_file *file, int from_tty,
778 struct cmd_list_element *c, const char *value)
779 {
780 fprintf_filtered (file, _("The upper bound on the age of cached "
781 "dwarf2 compilation units is %s.\n"),
782 value);
783 }
784
785
786 /* Various complaints about symbol reading that don't abort the process. */
787
788 static void
789 dwarf2_statement_list_fits_in_line_number_section_complaint (void)
790 {
791 complaint (&symfile_complaints,
792 _("statement list doesn't fit in .debug_line section"));
793 }
794
795 static void
796 dwarf2_debug_line_missing_file_complaint (void)
797 {
798 complaint (&symfile_complaints,
799 _(".debug_line section has line data without a file"));
800 }
801
802 static void
803 dwarf2_debug_line_missing_end_sequence_complaint (void)
804 {
805 complaint (&symfile_complaints,
806 _(".debug_line section has line "
807 "program sequence without an end"));
808 }
809
810 static void
811 dwarf2_complex_location_expr_complaint (void)
812 {
813 complaint (&symfile_complaints, _("location expression too complex"));
814 }
815
816 static void
817 dwarf2_const_value_length_mismatch_complaint (const char *arg1, int arg2,
818 int arg3)
819 {
820 complaint (&symfile_complaints,
821 _("const value length mismatch for '%s', got %d, expected %d"),
822 arg1, arg2, arg3);
823 }
824
825 static void
826 dwarf2_macros_too_long_complaint (struct dwarf2_section_info *section)
827 {
828 complaint (&symfile_complaints,
829 _("macro info runs off end of `%s' section"),
830 section->asection->name);
831 }
832
833 static void
834 dwarf2_macro_malformed_definition_complaint (const char *arg1)
835 {
836 complaint (&symfile_complaints,
837 _("macro debug info contains a "
838 "malformed macro definition:\n`%s'"),
839 arg1);
840 }
841
842 static void
843 dwarf2_invalid_attrib_class_complaint (const char *arg1, const char *arg2)
844 {
845 complaint (&symfile_complaints,
846 _("invalid attribute class or form for '%s' in '%s'"),
847 arg1, arg2);
848 }
849
850 /* local function prototypes */
851
852 static void dwarf2_locate_sections (bfd *, asection *, void *);
853
854 static void dwarf2_create_include_psymtab (char *, struct partial_symtab *,
855 struct objfile *);
856
857 static void dwarf2_find_base_address (struct die_info *die,
858 struct dwarf2_cu *cu);
859
860 static void dwarf2_build_psymtabs_hard (struct objfile *);
861
862 static void scan_partial_symbols (struct partial_die_info *,
863 CORE_ADDR *, CORE_ADDR *,
864 int, struct dwarf2_cu *);
865
866 static void add_partial_symbol (struct partial_die_info *,
867 struct dwarf2_cu *);
868
869 static void add_partial_namespace (struct partial_die_info *pdi,
870 CORE_ADDR *lowpc, CORE_ADDR *highpc,
871 int need_pc, struct dwarf2_cu *cu);
872
873 static void add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
874 CORE_ADDR *highpc, int need_pc,
875 struct dwarf2_cu *cu);
876
877 static void add_partial_enumeration (struct partial_die_info *enum_pdi,
878 struct dwarf2_cu *cu);
879
880 static void add_partial_subprogram (struct partial_die_info *pdi,
881 CORE_ADDR *lowpc, CORE_ADDR *highpc,
882 int need_pc, struct dwarf2_cu *cu);
883
884 static gdb_byte *locate_pdi_sibling (struct partial_die_info *orig_pdi,
885 gdb_byte *buffer, gdb_byte *info_ptr,
886 bfd *abfd, struct dwarf2_cu *cu);
887
888 static void dwarf2_psymtab_to_symtab (struct partial_symtab *);
889
890 static void psymtab_to_symtab_1 (struct partial_symtab *);
891
892 static void dwarf2_read_abbrevs (struct dwarf2_cu *cu);
893
894 static void dwarf2_free_abbrev_table (void *);
895
896 static unsigned int peek_abbrev_code (bfd *, gdb_byte *);
897
898 static struct abbrev_info *peek_die_abbrev (gdb_byte *, unsigned int *,
899 struct dwarf2_cu *);
900
901 static struct abbrev_info *dwarf2_lookup_abbrev (unsigned int,
902 struct dwarf2_cu *);
903
904 static struct partial_die_info *load_partial_dies (bfd *,
905 gdb_byte *, gdb_byte *,
906 int, struct dwarf2_cu *);
907
908 static gdb_byte *read_partial_die (struct partial_die_info *,
909 struct abbrev_info *abbrev,
910 unsigned int, bfd *,
911 gdb_byte *, gdb_byte *,
912 struct dwarf2_cu *);
913
914 static struct partial_die_info *find_partial_die (unsigned int,
915 struct dwarf2_cu *);
916
917 static void fixup_partial_die (struct partial_die_info *,
918 struct dwarf2_cu *);
919
920 static gdb_byte *read_attribute (struct attribute *, struct attr_abbrev *,
921 bfd *, gdb_byte *, struct dwarf2_cu *);
922
923 static gdb_byte *read_attribute_value (struct attribute *, unsigned,
924 bfd *, gdb_byte *, struct dwarf2_cu *);
925
926 static unsigned int read_1_byte (bfd *, gdb_byte *);
927
928 static int read_1_signed_byte (bfd *, gdb_byte *);
929
930 static unsigned int read_2_bytes (bfd *, gdb_byte *);
931
932 static unsigned int read_4_bytes (bfd *, gdb_byte *);
933
934 static ULONGEST read_8_bytes (bfd *, gdb_byte *);
935
936 static CORE_ADDR read_address (bfd *, gdb_byte *ptr, struct dwarf2_cu *,
937 unsigned int *);
938
939 static LONGEST read_initial_length (bfd *, gdb_byte *, unsigned int *);
940
941 static LONGEST read_checked_initial_length_and_offset
942 (bfd *, gdb_byte *, const struct comp_unit_head *,
943 unsigned int *, unsigned int *);
944
945 static LONGEST read_offset (bfd *, gdb_byte *, const struct comp_unit_head *,
946 unsigned int *);
947
948 static LONGEST read_offset_1 (bfd *, gdb_byte *, unsigned int);
949
950 static gdb_byte *read_n_bytes (bfd *, gdb_byte *, unsigned int);
951
952 static char *read_direct_string (bfd *, gdb_byte *, unsigned int *);
953
954 static char *read_indirect_string (bfd *, gdb_byte *,
955 const struct comp_unit_head *,
956 unsigned int *);
957
958 static unsigned long read_unsigned_leb128 (bfd *, gdb_byte *, unsigned int *);
959
960 static long read_signed_leb128 (bfd *, gdb_byte *, unsigned int *);
961
962 static gdb_byte *skip_leb128 (bfd *, gdb_byte *);
963
964 static void set_cu_language (unsigned int, struct dwarf2_cu *);
965
966 static struct attribute *dwarf2_attr (struct die_info *, unsigned int,
967 struct dwarf2_cu *);
968
969 static struct attribute *dwarf2_attr_no_follow (struct die_info *,
970 unsigned int,
971 struct dwarf2_cu *);
972
973 static int dwarf2_flag_true_p (struct die_info *die, unsigned name,
974 struct dwarf2_cu *cu);
975
976 static int die_is_declaration (struct die_info *, struct dwarf2_cu *cu);
977
978 static struct die_info *die_specification (struct die_info *die,
979 struct dwarf2_cu **);
980
981 static void free_line_header (struct line_header *lh);
982
983 static void add_file_name (struct line_header *, char *, unsigned int,
984 unsigned int, unsigned int);
985
986 static struct line_header *(dwarf_decode_line_header
987 (unsigned int offset,
988 bfd *abfd, struct dwarf2_cu *cu));
989
990 static void dwarf_decode_lines (struct line_header *, const char *,
991 struct dwarf2_cu *, struct partial_symtab *,
992 int);
993
994 static void dwarf2_start_subfile (char *, const char *, const char *);
995
996 static struct symbol *new_symbol (struct die_info *, struct type *,
997 struct dwarf2_cu *);
998
999 static struct symbol *new_symbol_full (struct die_info *, struct type *,
1000 struct dwarf2_cu *, struct symbol *);
1001
1002 static void dwarf2_const_value (struct attribute *, struct symbol *,
1003 struct dwarf2_cu *);
1004
1005 static void dwarf2_const_value_attr (struct attribute *attr,
1006 struct type *type,
1007 const char *name,
1008 struct obstack *obstack,
1009 struct dwarf2_cu *cu, long *value,
1010 gdb_byte **bytes,
1011 struct dwarf2_locexpr_baton **baton);
1012
1013 static struct type *die_type (struct die_info *, struct dwarf2_cu *);
1014
1015 static int need_gnat_info (struct dwarf2_cu *);
1016
1017 static struct type *die_descriptive_type (struct die_info *,
1018 struct dwarf2_cu *);
1019
1020 static void set_descriptive_type (struct type *, struct die_info *,
1021 struct dwarf2_cu *);
1022
1023 static struct type *die_containing_type (struct die_info *,
1024 struct dwarf2_cu *);
1025
1026 static struct type *lookup_die_type (struct die_info *, struct attribute *,
1027 struct dwarf2_cu *);
1028
1029 static struct type *read_type_die (struct die_info *, struct dwarf2_cu *);
1030
1031 static struct type *read_type_die_1 (struct die_info *, struct dwarf2_cu *);
1032
1033 static const char *determine_prefix (struct die_info *die, struct dwarf2_cu *);
1034
1035 static char *typename_concat (struct obstack *obs, const char *prefix,
1036 const char *suffix, int physname,
1037 struct dwarf2_cu *cu);
1038
1039 static void read_file_scope (struct die_info *, struct dwarf2_cu *);
1040
1041 static void read_type_unit_scope (struct die_info *, struct dwarf2_cu *);
1042
1043 static void read_func_scope (struct die_info *, struct dwarf2_cu *);
1044
1045 static void read_lexical_block_scope (struct die_info *, struct dwarf2_cu *);
1046
1047 static void read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu);
1048
1049 static int dwarf2_ranges_read (unsigned, CORE_ADDR *, CORE_ADDR *,
1050 struct dwarf2_cu *, struct partial_symtab *);
1051
1052 static int dwarf2_get_pc_bounds (struct die_info *,
1053 CORE_ADDR *, CORE_ADDR *, struct dwarf2_cu *,
1054 struct partial_symtab *);
1055
1056 static void get_scope_pc_bounds (struct die_info *,
1057 CORE_ADDR *, CORE_ADDR *,
1058 struct dwarf2_cu *);
1059
1060 static void dwarf2_record_block_ranges (struct die_info *, struct block *,
1061 CORE_ADDR, struct dwarf2_cu *);
1062
1063 static void dwarf2_add_field (struct field_info *, struct die_info *,
1064 struct dwarf2_cu *);
1065
1066 static void dwarf2_attach_fields_to_type (struct field_info *,
1067 struct type *, struct dwarf2_cu *);
1068
1069 static void dwarf2_add_member_fn (struct field_info *,
1070 struct die_info *, struct type *,
1071 struct dwarf2_cu *);
1072
1073 static void dwarf2_attach_fn_fields_to_type (struct field_info *,
1074 struct type *,
1075 struct dwarf2_cu *);
1076
1077 static void process_structure_scope (struct die_info *, struct dwarf2_cu *);
1078
1079 static void read_common_block (struct die_info *, struct dwarf2_cu *);
1080
1081 static void read_namespace (struct die_info *die, struct dwarf2_cu *);
1082
1083 static void read_module (struct die_info *die, struct dwarf2_cu *cu);
1084
1085 static void read_import_statement (struct die_info *die, struct dwarf2_cu *);
1086
1087 static struct type *read_module_type (struct die_info *die,
1088 struct dwarf2_cu *cu);
1089
1090 static const char *namespace_name (struct die_info *die,
1091 int *is_anonymous, struct dwarf2_cu *);
1092
1093 static void process_enumeration_scope (struct die_info *, struct dwarf2_cu *);
1094
1095 static CORE_ADDR decode_locdesc (struct dwarf_block *, struct dwarf2_cu *);
1096
1097 static enum dwarf_array_dim_ordering read_array_order (struct die_info *,
1098 struct dwarf2_cu *);
1099
1100 static struct die_info *read_comp_unit (gdb_byte *, struct dwarf2_cu *);
1101
1102 static struct die_info *read_die_and_children_1 (const struct die_reader_specs *reader,
1103 gdb_byte *info_ptr,
1104 gdb_byte **new_info_ptr,
1105 struct die_info *parent);
1106
1107 static struct die_info *read_die_and_children (const struct die_reader_specs *reader,
1108 gdb_byte *info_ptr,
1109 gdb_byte **new_info_ptr,
1110 struct die_info *parent);
1111
1112 static struct die_info *read_die_and_siblings (const struct die_reader_specs *reader,
1113 gdb_byte *info_ptr,
1114 gdb_byte **new_info_ptr,
1115 struct die_info *parent);
1116
1117 static gdb_byte *read_full_die (const struct die_reader_specs *reader,
1118 struct die_info **, gdb_byte *,
1119 int *);
1120
1121 static void process_die (struct die_info *, struct dwarf2_cu *);
1122
1123 static char *dwarf2_canonicalize_name (char *, struct dwarf2_cu *,
1124 struct obstack *);
1125
1126 static char *dwarf2_name (struct die_info *die, struct dwarf2_cu *);
1127
1128 static const char *dwarf2_full_name (char *name,
1129 struct die_info *die,
1130 struct dwarf2_cu *cu);
1131
1132 static struct die_info *dwarf2_extension (struct die_info *die,
1133 struct dwarf2_cu **);
1134
1135 static char *dwarf_tag_name (unsigned int);
1136
1137 static char *dwarf_attr_name (unsigned int);
1138
1139 static char *dwarf_form_name (unsigned int);
1140
1141 static char *dwarf_bool_name (unsigned int);
1142
1143 static char *dwarf_type_encoding_name (unsigned int);
1144
1145 #if 0
1146 static char *dwarf_cfi_name (unsigned int);
1147 #endif
1148
1149 static struct die_info *sibling_die (struct die_info *);
1150
1151 static void dump_die_shallow (struct ui_file *, int indent, struct die_info *);
1152
1153 static void dump_die_for_error (struct die_info *);
1154
1155 static void dump_die_1 (struct ui_file *, int level, int max_level,
1156 struct die_info *);
1157
1158 /*static*/ void dump_die (struct die_info *, int max_level);
1159
1160 static void store_in_ref_table (struct die_info *,
1161 struct dwarf2_cu *);
1162
1163 static int is_ref_attr (struct attribute *);
1164
1165 static unsigned int dwarf2_get_ref_die_offset (struct attribute *);
1166
1167 static LONGEST dwarf2_get_attr_constant_value (struct attribute *, int);
1168
1169 static struct die_info *follow_die_ref_or_sig (struct die_info *,
1170 struct attribute *,
1171 struct dwarf2_cu **);
1172
1173 static struct die_info *follow_die_ref (struct die_info *,
1174 struct attribute *,
1175 struct dwarf2_cu **);
1176
1177 static struct die_info *follow_die_sig (struct die_info *,
1178 struct attribute *,
1179 struct dwarf2_cu **);
1180
1181 static struct signatured_type *lookup_signatured_type_at_offset
1182 (struct objfile *objfile,
1183 struct dwarf2_section_info *section,
1184 unsigned int offset);
1185
1186 static void load_full_type_unit (struct dwarf2_per_cu_data *per_cu);
1187
1188 static void read_signatured_type (struct signatured_type *type_sig);
1189
1190 /* memory allocation interface */
1191
1192 static struct dwarf_block *dwarf_alloc_block (struct dwarf2_cu *);
1193
1194 static struct abbrev_info *dwarf_alloc_abbrev (struct dwarf2_cu *);
1195
1196 static struct die_info *dwarf_alloc_die (struct dwarf2_cu *, int);
1197
1198 static void dwarf_decode_macros (struct line_header *, unsigned int,
1199 char *, bfd *, struct dwarf2_cu *,
1200 struct dwarf2_section_info *,
1201 int);
1202
1203 static int attr_form_is_block (struct attribute *);
1204
1205 static int attr_form_is_section_offset (struct attribute *);
1206
1207 static int attr_form_is_constant (struct attribute *);
1208
1209 static void fill_in_loclist_baton (struct dwarf2_cu *cu,
1210 struct dwarf2_loclist_baton *baton,
1211 struct attribute *attr);
1212
1213 static void dwarf2_symbol_mark_computed (struct attribute *attr,
1214 struct symbol *sym,
1215 struct dwarf2_cu *cu);
1216
1217 static gdb_byte *skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
1218 struct abbrev_info *abbrev,
1219 struct dwarf2_cu *cu);
1220
1221 static void free_stack_comp_unit (void *);
1222
1223 static hashval_t partial_die_hash (const void *item);
1224
1225 static int partial_die_eq (const void *item_lhs, const void *item_rhs);
1226
1227 static struct dwarf2_per_cu_data *dwarf2_find_containing_comp_unit
1228 (unsigned int offset, struct objfile *objfile);
1229
1230 static void init_one_comp_unit (struct dwarf2_cu *cu,
1231 struct dwarf2_per_cu_data *per_cu);
1232
1233 static void prepare_one_comp_unit (struct dwarf2_cu *cu,
1234 struct die_info *comp_unit_die);
1235
1236 static void free_heap_comp_unit (void *);
1237
1238 static void free_cached_comp_units (void *);
1239
1240 static void age_cached_comp_units (void);
1241
1242 static void free_one_cached_comp_unit (void *);
1243
1244 static struct type *set_die_type (struct die_info *, struct type *,
1245 struct dwarf2_cu *);
1246
1247 static void create_all_comp_units (struct objfile *);
1248
1249 static int create_debug_types_hash_table (struct objfile *objfile);
1250
1251 static void load_full_comp_unit (struct dwarf2_per_cu_data *);
1252
1253 static void process_full_comp_unit (struct dwarf2_per_cu_data *);
1254
1255 static void dwarf2_add_dependence (struct dwarf2_cu *,
1256 struct dwarf2_per_cu_data *);
1257
1258 static void dwarf2_mark (struct dwarf2_cu *);
1259
1260 static void dwarf2_clear_marks (struct dwarf2_per_cu_data *);
1261
1262 static struct type *get_die_type_at_offset (unsigned int,
1263 struct dwarf2_per_cu_data *per_cu);
1264
1265 static struct type *get_die_type (struct die_info *die, struct dwarf2_cu *cu);
1266
1267 static void dwarf2_release_queue (void *dummy);
1268
1269 static void queue_comp_unit (struct dwarf2_per_cu_data *per_cu);
1270
1271 static void process_queue (void);
1272
1273 static void find_file_and_directory (struct die_info *die,
1274 struct dwarf2_cu *cu,
1275 char **name, char **comp_dir);
1276
1277 static char *file_full_name (int file, struct line_header *lh,
1278 const char *comp_dir);
1279
1280 static gdb_byte *read_and_check_comp_unit_head
1281 (struct comp_unit_head *header,
1282 struct dwarf2_section_info *section, gdb_byte *info_ptr,
1283 int is_debug_types_section);
1284
1285 static void init_cu_die_reader (struct die_reader_specs *reader,
1286 struct dwarf2_cu *cu);
1287
1288 static htab_t allocate_signatured_type_table (struct objfile *objfile);
1289
1290 #if WORDS_BIGENDIAN
1291
1292 /* Convert VALUE between big- and little-endian. */
1293 static offset_type
1294 byte_swap (offset_type value)
1295 {
1296 offset_type result;
1297
1298 result = (value & 0xff) << 24;
1299 result |= (value & 0xff00) << 8;
1300 result |= (value & 0xff0000) >> 8;
1301 result |= (value & 0xff000000) >> 24;
1302 return result;
1303 }
1304
1305 #define MAYBE_SWAP(V) byte_swap (V)
1306
1307 #else
1308 #define MAYBE_SWAP(V) (V)
1309 #endif /* WORDS_BIGENDIAN */
1310
1311 /* The suffix for an index file. */
1312 #define INDEX_SUFFIX ".gdb-index"
1313
1314 static const char *dwarf2_physname (char *name, struct die_info *die,
1315 struct dwarf2_cu *cu);
1316
1317 /* Try to locate the sections we need for DWARF 2 debugging
1318 information and return true if we have enough to do something.
1319 NAMES points to the dwarf2 section names, or is NULL if the standard
1320 ELF names are used. */
1321
1322 int
1323 dwarf2_has_info (struct objfile *objfile,
1324 const struct dwarf2_debug_sections *names)
1325 {
1326 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
1327 if (!dwarf2_per_objfile)
1328 {
1329 /* Initialize per-objfile state. */
1330 struct dwarf2_per_objfile *data
1331 = obstack_alloc (&objfile->objfile_obstack, sizeof (*data));
1332
1333 memset (data, 0, sizeof (*data));
1334 set_objfile_data (objfile, dwarf2_objfile_data_key, data);
1335 dwarf2_per_objfile = data;
1336
1337 bfd_map_over_sections (objfile->obfd, dwarf2_locate_sections,
1338 (void *) names);
1339 dwarf2_per_objfile->objfile = objfile;
1340 }
1341 return (dwarf2_per_objfile->info.asection != NULL
1342 && dwarf2_per_objfile->abbrev.asection != NULL);
1343 }
1344
1345 /* When loading sections, we look either for uncompressed section or for
1346 compressed section names. */
1347
1348 static int
1349 section_is_p (const char *section_name,
1350 const struct dwarf2_section_names *names)
1351 {
1352 if (names->normal != NULL
1353 && strcmp (section_name, names->normal) == 0)
1354 return 1;
1355 if (names->compressed != NULL
1356 && strcmp (section_name, names->compressed) == 0)
1357 return 1;
1358 return 0;
1359 }
1360
1361 /* This function is mapped across the sections and remembers the
1362 offset and size of each of the debugging sections we are interested
1363 in. */
1364
1365 static void
1366 dwarf2_locate_sections (bfd *abfd, asection *sectp, void *vnames)
1367 {
1368 const struct dwarf2_debug_sections *names;
1369
1370 if (vnames == NULL)
1371 names = &dwarf2_elf_names;
1372 else
1373 names = (const struct dwarf2_debug_sections *) vnames;
1374
1375 if (section_is_p (sectp->name, &names->info))
1376 {
1377 dwarf2_per_objfile->info.asection = sectp;
1378 dwarf2_per_objfile->info.size = bfd_get_section_size (sectp);
1379 }
1380 else if (section_is_p (sectp->name, &names->abbrev))
1381 {
1382 dwarf2_per_objfile->abbrev.asection = sectp;
1383 dwarf2_per_objfile->abbrev.size = bfd_get_section_size (sectp);
1384 }
1385 else if (section_is_p (sectp->name, &names->line))
1386 {
1387 dwarf2_per_objfile->line.asection = sectp;
1388 dwarf2_per_objfile->line.size = bfd_get_section_size (sectp);
1389 }
1390 else if (section_is_p (sectp->name, &names->loc))
1391 {
1392 dwarf2_per_objfile->loc.asection = sectp;
1393 dwarf2_per_objfile->loc.size = bfd_get_section_size (sectp);
1394 }
1395 else if (section_is_p (sectp->name, &names->macinfo))
1396 {
1397 dwarf2_per_objfile->macinfo.asection = sectp;
1398 dwarf2_per_objfile->macinfo.size = bfd_get_section_size (sectp);
1399 }
1400 else if (section_is_p (sectp->name, &names->macro))
1401 {
1402 dwarf2_per_objfile->macro.asection = sectp;
1403 dwarf2_per_objfile->macro.size = bfd_get_section_size (sectp);
1404 }
1405 else if (section_is_p (sectp->name, &names->str))
1406 {
1407 dwarf2_per_objfile->str.asection = sectp;
1408 dwarf2_per_objfile->str.size = bfd_get_section_size (sectp);
1409 }
1410 else if (section_is_p (sectp->name, &names->frame))
1411 {
1412 dwarf2_per_objfile->frame.asection = sectp;
1413 dwarf2_per_objfile->frame.size = bfd_get_section_size (sectp);
1414 }
1415 else if (section_is_p (sectp->name, &names->eh_frame))
1416 {
1417 flagword aflag = bfd_get_section_flags (ignore_abfd, sectp);
1418
1419 if (aflag & SEC_HAS_CONTENTS)
1420 {
1421 dwarf2_per_objfile->eh_frame.asection = sectp;
1422 dwarf2_per_objfile->eh_frame.size = bfd_get_section_size (sectp);
1423 }
1424 }
1425 else if (section_is_p (sectp->name, &names->ranges))
1426 {
1427 dwarf2_per_objfile->ranges.asection = sectp;
1428 dwarf2_per_objfile->ranges.size = bfd_get_section_size (sectp);
1429 }
1430 else if (section_is_p (sectp->name, &names->types))
1431 {
1432 struct dwarf2_section_info type_section;
1433
1434 memset (&type_section, 0, sizeof (type_section));
1435 type_section.asection = sectp;
1436 type_section.size = bfd_get_section_size (sectp);
1437
1438 VEC_safe_push (dwarf2_section_info_def, dwarf2_per_objfile->types,
1439 &type_section);
1440 }
1441 else if (section_is_p (sectp->name, &names->gdb_index))
1442 {
1443 dwarf2_per_objfile->gdb_index.asection = sectp;
1444 dwarf2_per_objfile->gdb_index.size = bfd_get_section_size (sectp);
1445 }
1446
1447 if ((bfd_get_section_flags (abfd, sectp) & SEC_LOAD)
1448 && bfd_section_vma (abfd, sectp) == 0)
1449 dwarf2_per_objfile->has_section_at_zero = 1;
1450 }
1451
1452 /* Decompress a section that was compressed using zlib. Store the
1453 decompressed buffer, and its size, in OUTBUF and OUTSIZE. */
1454
1455 static void
1456 zlib_decompress_section (struct objfile *objfile, asection *sectp,
1457 gdb_byte **outbuf, bfd_size_type *outsize)
1458 {
1459 bfd *abfd = objfile->obfd;
1460 #ifndef HAVE_ZLIB_H
1461 error (_("Support for zlib-compressed DWARF data (from '%s') "
1462 "is disabled in this copy of GDB"),
1463 bfd_get_filename (abfd));
1464 #else
1465 bfd_size_type compressed_size = bfd_get_section_size (sectp);
1466 gdb_byte *compressed_buffer = xmalloc (compressed_size);
1467 struct cleanup *cleanup = make_cleanup (xfree, compressed_buffer);
1468 bfd_size_type uncompressed_size;
1469 gdb_byte *uncompressed_buffer;
1470 z_stream strm;
1471 int rc;
1472 int header_size = 12;
1473
1474 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1475 || bfd_bread (compressed_buffer,
1476 compressed_size, abfd) != compressed_size)
1477 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1478 bfd_get_filename (abfd));
1479
1480 /* Read the zlib header. In this case, it should be "ZLIB" followed
1481 by the uncompressed section size, 8 bytes in big-endian order. */
1482 if (compressed_size < header_size
1483 || strncmp (compressed_buffer, "ZLIB", 4) != 0)
1484 error (_("Dwarf Error: Corrupt DWARF ZLIB header from '%s'"),
1485 bfd_get_filename (abfd));
1486 uncompressed_size = compressed_buffer[4]; uncompressed_size <<= 8;
1487 uncompressed_size += compressed_buffer[5]; uncompressed_size <<= 8;
1488 uncompressed_size += compressed_buffer[6]; uncompressed_size <<= 8;
1489 uncompressed_size += compressed_buffer[7]; uncompressed_size <<= 8;
1490 uncompressed_size += compressed_buffer[8]; uncompressed_size <<= 8;
1491 uncompressed_size += compressed_buffer[9]; uncompressed_size <<= 8;
1492 uncompressed_size += compressed_buffer[10]; uncompressed_size <<= 8;
1493 uncompressed_size += compressed_buffer[11];
1494
1495 /* It is possible the section consists of several compressed
1496 buffers concatenated together, so we uncompress in a loop. */
1497 strm.zalloc = NULL;
1498 strm.zfree = NULL;
1499 strm.opaque = NULL;
1500 strm.avail_in = compressed_size - header_size;
1501 strm.next_in = (Bytef*) compressed_buffer + header_size;
1502 strm.avail_out = uncompressed_size;
1503 uncompressed_buffer = obstack_alloc (&objfile->objfile_obstack,
1504 uncompressed_size);
1505 rc = inflateInit (&strm);
1506 while (strm.avail_in > 0)
1507 {
1508 if (rc != Z_OK)
1509 error (_("Dwarf Error: setting up DWARF uncompression in '%s': %d"),
1510 bfd_get_filename (abfd), rc);
1511 strm.next_out = ((Bytef*) uncompressed_buffer
1512 + (uncompressed_size - strm.avail_out));
1513 rc = inflate (&strm, Z_FINISH);
1514 if (rc != Z_STREAM_END)
1515 error (_("Dwarf Error: zlib error uncompressing from '%s': %d"),
1516 bfd_get_filename (abfd), rc);
1517 rc = inflateReset (&strm);
1518 }
1519 rc = inflateEnd (&strm);
1520 if (rc != Z_OK
1521 || strm.avail_out != 0)
1522 error (_("Dwarf Error: concluding DWARF uncompression in '%s': %d"),
1523 bfd_get_filename (abfd), rc);
1524
1525 do_cleanups (cleanup);
1526 *outbuf = uncompressed_buffer;
1527 *outsize = uncompressed_size;
1528 #endif
1529 }
1530
1531 /* A helper function that decides whether a section is empty. */
1532
1533 static int
1534 dwarf2_section_empty_p (struct dwarf2_section_info *info)
1535 {
1536 return info->asection == NULL || info->size == 0;
1537 }
1538
1539 /* Read the contents of the section INFO from object file specified by
1540 OBJFILE, store info about the section into INFO.
1541 If the section is compressed, uncompress it before returning. */
1542
1543 static void
1544 dwarf2_read_section (struct objfile *objfile, struct dwarf2_section_info *info)
1545 {
1546 bfd *abfd = objfile->obfd;
1547 asection *sectp = info->asection;
1548 gdb_byte *buf, *retbuf;
1549 unsigned char header[4];
1550
1551 if (info->readin)
1552 return;
1553 info->buffer = NULL;
1554 info->map_addr = NULL;
1555 info->readin = 1;
1556
1557 if (dwarf2_section_empty_p (info))
1558 return;
1559
1560 /* Check if the file has a 4-byte header indicating compression. */
1561 if (info->size > sizeof (header)
1562 && bfd_seek (abfd, sectp->filepos, SEEK_SET) == 0
1563 && bfd_bread (header, sizeof (header), abfd) == sizeof (header))
1564 {
1565 /* Upon decompression, update the buffer and its size. */
1566 if (strncmp (header, "ZLIB", sizeof (header)) == 0)
1567 {
1568 zlib_decompress_section (objfile, sectp, &info->buffer,
1569 &info->size);
1570 return;
1571 }
1572 }
1573
1574 #ifdef HAVE_MMAP
1575 if (pagesize == 0)
1576 pagesize = getpagesize ();
1577
1578 /* Only try to mmap sections which are large enough: we don't want to
1579 waste space due to fragmentation. Also, only try mmap for sections
1580 without relocations. */
1581
1582 if (info->size > 4 * pagesize && (sectp->flags & SEC_RELOC) == 0)
1583 {
1584 info->buffer = bfd_mmap (abfd, 0, info->size, PROT_READ,
1585 MAP_PRIVATE, sectp->filepos,
1586 &info->map_addr, &info->map_len);
1587
1588 if ((caddr_t)info->buffer != MAP_FAILED)
1589 {
1590 #if HAVE_POSIX_MADVISE
1591 posix_madvise (info->map_addr, info->map_len, POSIX_MADV_WILLNEED);
1592 #endif
1593 return;
1594 }
1595 }
1596 #endif
1597
1598 /* If we get here, we are a normal, not-compressed section. */
1599 info->buffer = buf
1600 = obstack_alloc (&objfile->objfile_obstack, info->size);
1601
1602 /* When debugging .o files, we may need to apply relocations; see
1603 http://sourceware.org/ml/gdb-patches/2002-04/msg00136.html .
1604 We never compress sections in .o files, so we only need to
1605 try this when the section is not compressed. */
1606 retbuf = symfile_relocate_debug_section (objfile, sectp, buf);
1607 if (retbuf != NULL)
1608 {
1609 info->buffer = retbuf;
1610 return;
1611 }
1612
1613 if (bfd_seek (abfd, sectp->filepos, SEEK_SET) != 0
1614 || bfd_bread (buf, info->size, abfd) != info->size)
1615 error (_("Dwarf Error: Can't read DWARF data from '%s'"),
1616 bfd_get_filename (abfd));
1617 }
1618
1619 /* A helper function that returns the size of a section in a safe way.
1620 If you are positive that the section has been read before using the
1621 size, then it is safe to refer to the dwarf2_section_info object's
1622 "size" field directly. In other cases, you must call this
1623 function, because for compressed sections the size field is not set
1624 correctly until the section has been read. */
1625
1626 static bfd_size_type
1627 dwarf2_section_size (struct objfile *objfile,
1628 struct dwarf2_section_info *info)
1629 {
1630 if (!info->readin)
1631 dwarf2_read_section (objfile, info);
1632 return info->size;
1633 }
1634
1635 /* Fill in SECTP, BUFP and SIZEP with section info, given OBJFILE and
1636 SECTION_NAME. */
1637
1638 void
1639 dwarf2_get_section_info (struct objfile *objfile,
1640 enum dwarf2_section_enum sect,
1641 asection **sectp, gdb_byte **bufp,
1642 bfd_size_type *sizep)
1643 {
1644 struct dwarf2_per_objfile *data
1645 = objfile_data (objfile, dwarf2_objfile_data_key);
1646 struct dwarf2_section_info *info;
1647
1648 /* We may see an objfile without any DWARF, in which case we just
1649 return nothing. */
1650 if (data == NULL)
1651 {
1652 *sectp = NULL;
1653 *bufp = NULL;
1654 *sizep = 0;
1655 return;
1656 }
1657 switch (sect)
1658 {
1659 case DWARF2_DEBUG_FRAME:
1660 info = &data->frame;
1661 break;
1662 case DWARF2_EH_FRAME:
1663 info = &data->eh_frame;
1664 break;
1665 default:
1666 gdb_assert_not_reached ("unexpected section");
1667 }
1668
1669 dwarf2_read_section (objfile, info);
1670
1671 *sectp = info->asection;
1672 *bufp = info->buffer;
1673 *sizep = info->size;
1674 }
1675
1676 \f
1677 /* DWARF quick_symbols_functions support. */
1678
1679 /* TUs can share .debug_line entries, and there can be a lot more TUs than
1680 unique line tables, so we maintain a separate table of all .debug_line
1681 derived entries to support the sharing.
1682 All the quick functions need is the list of file names. We discard the
1683 line_header when we're done and don't need to record it here. */
1684 struct quick_file_names
1685 {
1686 /* The offset in .debug_line of the line table. We hash on this. */
1687 unsigned int offset;
1688
1689 /* The number of entries in file_names, real_names. */
1690 unsigned int num_file_names;
1691
1692 /* The file names from the line table, after being run through
1693 file_full_name. */
1694 const char **file_names;
1695
1696 /* The file names from the line table after being run through
1697 gdb_realpath. These are computed lazily. */
1698 const char **real_names;
1699 };
1700
1701 /* When using the index (and thus not using psymtabs), each CU has an
1702 object of this type. This is used to hold information needed by
1703 the various "quick" methods. */
1704 struct dwarf2_per_cu_quick_data
1705 {
1706 /* The file table. This can be NULL if there was no file table
1707 or it's currently not read in.
1708 NOTE: This points into dwarf2_per_objfile->quick_file_names_table. */
1709 struct quick_file_names *file_names;
1710
1711 /* The corresponding symbol table. This is NULL if symbols for this
1712 CU have not yet been read. */
1713 struct symtab *symtab;
1714
1715 /* A temporary mark bit used when iterating over all CUs in
1716 expand_symtabs_matching. */
1717 unsigned int mark : 1;
1718
1719 /* True if we've tried to read the file table and found there isn't one.
1720 There will be no point in trying to read it again next time. */
1721 unsigned int no_file_data : 1;
1722 };
1723
1724 /* Hash function for a quick_file_names. */
1725
1726 static hashval_t
1727 hash_file_name_entry (const void *e)
1728 {
1729 const struct quick_file_names *file_data = e;
1730
1731 return file_data->offset;
1732 }
1733
1734 /* Equality function for a quick_file_names. */
1735
1736 static int
1737 eq_file_name_entry (const void *a, const void *b)
1738 {
1739 const struct quick_file_names *ea = a;
1740 const struct quick_file_names *eb = b;
1741
1742 return ea->offset == eb->offset;
1743 }
1744
1745 /* Delete function for a quick_file_names. */
1746
1747 static void
1748 delete_file_name_entry (void *e)
1749 {
1750 struct quick_file_names *file_data = e;
1751 int i;
1752
1753 for (i = 0; i < file_data->num_file_names; ++i)
1754 {
1755 xfree ((void*) file_data->file_names[i]);
1756 if (file_data->real_names)
1757 xfree ((void*) file_data->real_names[i]);
1758 }
1759
1760 /* The space for the struct itself lives on objfile_obstack,
1761 so we don't free it here. */
1762 }
1763
1764 /* Create a quick_file_names hash table. */
1765
1766 static htab_t
1767 create_quick_file_names_table (unsigned int nr_initial_entries)
1768 {
1769 return htab_create_alloc (nr_initial_entries,
1770 hash_file_name_entry, eq_file_name_entry,
1771 delete_file_name_entry, xcalloc, xfree);
1772 }
1773
1774 /* Read in PER_CU->CU. This function is unrelated to symtabs, symtab would
1775 have to be created afterwards. You should call age_cached_comp_units after
1776 processing PER_CU->CU. dw2_setup must have been already called. */
1777
1778 static void
1779 load_cu (struct dwarf2_per_cu_data *per_cu)
1780 {
1781 if (per_cu->debug_types_section)
1782 load_full_type_unit (per_cu);
1783 else
1784 load_full_comp_unit (per_cu);
1785
1786 gdb_assert (per_cu->cu != NULL);
1787
1788 dwarf2_find_base_address (per_cu->cu->dies, per_cu->cu);
1789 }
1790
1791 /* Read in the symbols for PER_CU. */
1792
1793 static void
1794 dw2_do_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1795 {
1796 struct cleanup *back_to;
1797
1798 back_to = make_cleanup (dwarf2_release_queue, NULL);
1799
1800 queue_comp_unit (per_cu);
1801
1802 load_cu (per_cu);
1803
1804 process_queue ();
1805
1806 /* Age the cache, releasing compilation units that have not
1807 been used recently. */
1808 age_cached_comp_units ();
1809
1810 do_cleanups (back_to);
1811 }
1812
1813 /* Ensure that the symbols for PER_CU have been read in. OBJFILE is
1814 the objfile from which this CU came. Returns the resulting symbol
1815 table. */
1816
1817 static struct symtab *
1818 dw2_instantiate_symtab (struct dwarf2_per_cu_data *per_cu)
1819 {
1820 if (!per_cu->v.quick->symtab)
1821 {
1822 struct cleanup *back_to = make_cleanup (free_cached_comp_units, NULL);
1823 increment_reading_symtab ();
1824 dw2_do_instantiate_symtab (per_cu);
1825 do_cleanups (back_to);
1826 }
1827 return per_cu->v.quick->symtab;
1828 }
1829
1830 /* Return the CU given its index. */
1831
1832 static struct dwarf2_per_cu_data *
1833 dw2_get_cu (int index)
1834 {
1835 if (index >= dwarf2_per_objfile->n_comp_units)
1836 {
1837 index -= dwarf2_per_objfile->n_comp_units;
1838 return dwarf2_per_objfile->all_type_units[index];
1839 }
1840 return dwarf2_per_objfile->all_comp_units[index];
1841 }
1842
1843 /* A helper function that knows how to read a 64-bit value in a way
1844 that doesn't make gdb die. Returns 1 if the conversion went ok, 0
1845 otherwise. */
1846
1847 static int
1848 extract_cu_value (const char *bytes, ULONGEST *result)
1849 {
1850 if (sizeof (ULONGEST) < 8)
1851 {
1852 int i;
1853
1854 /* Ignore the upper 4 bytes if they are all zero. */
1855 for (i = 0; i < 4; ++i)
1856 if (bytes[i + 4] != 0)
1857 return 0;
1858
1859 *result = extract_unsigned_integer (bytes, 4, BFD_ENDIAN_LITTLE);
1860 }
1861 else
1862 *result = extract_unsigned_integer (bytes, 8, BFD_ENDIAN_LITTLE);
1863 return 1;
1864 }
1865
1866 /* Read the CU list from the mapped index, and use it to create all
1867 the CU objects for this objfile. Return 0 if something went wrong,
1868 1 if everything went ok. */
1869
1870 static int
1871 create_cus_from_index (struct objfile *objfile, const gdb_byte *cu_list,
1872 offset_type cu_list_elements)
1873 {
1874 offset_type i;
1875
1876 dwarf2_per_objfile->n_comp_units = cu_list_elements / 2;
1877 dwarf2_per_objfile->all_comp_units
1878 = obstack_alloc (&objfile->objfile_obstack,
1879 dwarf2_per_objfile->n_comp_units
1880 * sizeof (struct dwarf2_per_cu_data *));
1881
1882 for (i = 0; i < cu_list_elements; i += 2)
1883 {
1884 struct dwarf2_per_cu_data *the_cu;
1885 ULONGEST offset, length;
1886
1887 if (!extract_cu_value (cu_list, &offset)
1888 || !extract_cu_value (cu_list + 8, &length))
1889 return 0;
1890 cu_list += 2 * 8;
1891
1892 the_cu = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1893 struct dwarf2_per_cu_data);
1894 the_cu->offset = offset;
1895 the_cu->length = length;
1896 the_cu->objfile = objfile;
1897 the_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1898 struct dwarf2_per_cu_quick_data);
1899 dwarf2_per_objfile->all_comp_units[i / 2] = the_cu;
1900 }
1901
1902 return 1;
1903 }
1904
1905 /* Create the signatured type hash table from the index. */
1906
1907 static int
1908 create_signatured_type_table_from_index (struct objfile *objfile,
1909 struct dwarf2_section_info *section,
1910 const gdb_byte *bytes,
1911 offset_type elements)
1912 {
1913 offset_type i;
1914 htab_t sig_types_hash;
1915
1916 dwarf2_per_objfile->n_type_units = elements / 3;
1917 dwarf2_per_objfile->all_type_units
1918 = obstack_alloc (&objfile->objfile_obstack,
1919 dwarf2_per_objfile->n_type_units
1920 * sizeof (struct dwarf2_per_cu_data *));
1921
1922 sig_types_hash = allocate_signatured_type_table (objfile);
1923
1924 for (i = 0; i < elements; i += 3)
1925 {
1926 struct signatured_type *type_sig;
1927 ULONGEST offset, type_offset, signature;
1928 void **slot;
1929
1930 if (!extract_cu_value (bytes, &offset)
1931 || !extract_cu_value (bytes + 8, &type_offset))
1932 return 0;
1933 signature = extract_unsigned_integer (bytes + 16, 8, BFD_ENDIAN_LITTLE);
1934 bytes += 3 * 8;
1935
1936 type_sig = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1937 struct signatured_type);
1938 type_sig->signature = signature;
1939 type_sig->type_offset = type_offset;
1940 type_sig->per_cu.debug_types_section = section;
1941 type_sig->per_cu.offset = offset;
1942 type_sig->per_cu.objfile = objfile;
1943 type_sig->per_cu.v.quick
1944 = OBSTACK_ZALLOC (&objfile->objfile_obstack,
1945 struct dwarf2_per_cu_quick_data);
1946
1947 slot = htab_find_slot (sig_types_hash, type_sig, INSERT);
1948 *slot = type_sig;
1949
1950 dwarf2_per_objfile->all_type_units[i / 3] = &type_sig->per_cu;
1951 }
1952
1953 dwarf2_per_objfile->signatured_types = sig_types_hash;
1954
1955 return 1;
1956 }
1957
1958 /* Read the address map data from the mapped index, and use it to
1959 populate the objfile's psymtabs_addrmap. */
1960
1961 static void
1962 create_addrmap_from_index (struct objfile *objfile, struct mapped_index *index)
1963 {
1964 const gdb_byte *iter, *end;
1965 struct obstack temp_obstack;
1966 struct addrmap *mutable_map;
1967 struct cleanup *cleanup;
1968 CORE_ADDR baseaddr;
1969
1970 obstack_init (&temp_obstack);
1971 cleanup = make_cleanup_obstack_free (&temp_obstack);
1972 mutable_map = addrmap_create_mutable (&temp_obstack);
1973
1974 iter = index->address_table;
1975 end = iter + index->address_table_size;
1976
1977 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
1978
1979 while (iter < end)
1980 {
1981 ULONGEST hi, lo, cu_index;
1982 lo = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1983 iter += 8;
1984 hi = extract_unsigned_integer (iter, 8, BFD_ENDIAN_LITTLE);
1985 iter += 8;
1986 cu_index = extract_unsigned_integer (iter, 4, BFD_ENDIAN_LITTLE);
1987 iter += 4;
1988
1989 addrmap_set_empty (mutable_map, lo + baseaddr, hi + baseaddr - 1,
1990 dw2_get_cu (cu_index));
1991 }
1992
1993 objfile->psymtabs_addrmap = addrmap_create_fixed (mutable_map,
1994 &objfile->objfile_obstack);
1995 do_cleanups (cleanup);
1996 }
1997
1998 /* The hash function for strings in the mapped index. This is the same as
1999 SYMBOL_HASH_NEXT, but we keep a separate copy to maintain control over the
2000 implementation. This is necessary because the hash function is tied to the
2001 format of the mapped index file. The hash values do not have to match with
2002 SYMBOL_HASH_NEXT.
2003
2004 Use INT_MAX for INDEX_VERSION if you generate the current index format. */
2005
2006 static hashval_t
2007 mapped_index_string_hash (int index_version, const void *p)
2008 {
2009 const unsigned char *str = (const unsigned char *) p;
2010 hashval_t r = 0;
2011 unsigned char c;
2012
2013 while ((c = *str++) != 0)
2014 {
2015 if (index_version >= 5)
2016 c = tolower (c);
2017 r = r * 67 + c - 113;
2018 }
2019
2020 return r;
2021 }
2022
2023 /* Find a slot in the mapped index INDEX for the object named NAME.
2024 If NAME is found, set *VEC_OUT to point to the CU vector in the
2025 constant pool and return 1. If NAME cannot be found, return 0. */
2026
2027 static int
2028 find_slot_in_mapped_hash (struct mapped_index *index, const char *name,
2029 offset_type **vec_out)
2030 {
2031 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
2032 offset_type hash;
2033 offset_type slot, step;
2034 int (*cmp) (const char *, const char *);
2035
2036 if (current_language->la_language == language_cplus
2037 || current_language->la_language == language_java
2038 || current_language->la_language == language_fortran)
2039 {
2040 /* NAME is already canonical. Drop any qualifiers as .gdb_index does
2041 not contain any. */
2042 const char *paren = strchr (name, '(');
2043
2044 if (paren)
2045 {
2046 char *dup;
2047
2048 dup = xmalloc (paren - name + 1);
2049 memcpy (dup, name, paren - name);
2050 dup[paren - name] = 0;
2051
2052 make_cleanup (xfree, dup);
2053 name = dup;
2054 }
2055 }
2056
2057 /* Index version 4 did not support case insensitive searches. But the
2058 indices for case insensitive languages are built in lowercase, therefore
2059 simulate our NAME being searched is also lowercased. */
2060 hash = mapped_index_string_hash ((index->version == 4
2061 && case_sensitivity == case_sensitive_off
2062 ? 5 : index->version),
2063 name);
2064
2065 slot = hash & (index->symbol_table_slots - 1);
2066 step = ((hash * 17) & (index->symbol_table_slots - 1)) | 1;
2067 cmp = (case_sensitivity == case_sensitive_on ? strcmp : strcasecmp);
2068
2069 for (;;)
2070 {
2071 /* Convert a slot number to an offset into the table. */
2072 offset_type i = 2 * slot;
2073 const char *str;
2074 if (index->symbol_table[i] == 0 && index->symbol_table[i + 1] == 0)
2075 {
2076 do_cleanups (back_to);
2077 return 0;
2078 }
2079
2080 str = index->constant_pool + MAYBE_SWAP (index->symbol_table[i]);
2081 if (!cmp (name, str))
2082 {
2083 *vec_out = (offset_type *) (index->constant_pool
2084 + MAYBE_SWAP (index->symbol_table[i + 1]));
2085 do_cleanups (back_to);
2086 return 1;
2087 }
2088
2089 slot = (slot + step) & (index->symbol_table_slots - 1);
2090 }
2091 }
2092
2093 /* Read the index file. If everything went ok, initialize the "quick"
2094 elements of all the CUs and return 1. Otherwise, return 0. */
2095
2096 static int
2097 dwarf2_read_index (struct objfile *objfile)
2098 {
2099 char *addr;
2100 struct mapped_index *map;
2101 offset_type *metadata;
2102 const gdb_byte *cu_list;
2103 const gdb_byte *types_list = NULL;
2104 offset_type version, cu_list_elements;
2105 offset_type types_list_elements = 0;
2106 int i;
2107
2108 if (dwarf2_section_empty_p (&dwarf2_per_objfile->gdb_index))
2109 return 0;
2110
2111 /* Older elfutils strip versions could keep the section in the main
2112 executable while splitting it for the separate debug info file. */
2113 if ((bfd_get_file_flags (dwarf2_per_objfile->gdb_index.asection)
2114 & SEC_HAS_CONTENTS) == 0)
2115 return 0;
2116
2117 dwarf2_read_section (objfile, &dwarf2_per_objfile->gdb_index);
2118
2119 addr = dwarf2_per_objfile->gdb_index.buffer;
2120 /* Version check. */
2121 version = MAYBE_SWAP (*(offset_type *) addr);
2122 /* Versions earlier than 3 emitted every copy of a psymbol. This
2123 causes the index to behave very poorly for certain requests. Version 3
2124 contained incomplete addrmap. So, it seems better to just ignore such
2125 indices. */
2126 if (version < 4)
2127 {
2128 static int warning_printed = 0;
2129 if (!warning_printed)
2130 {
2131 warning (_("Skipping obsolete .gdb_index section in %s."),
2132 objfile->name);
2133 warning_printed = 1;
2134 }
2135 return 0;
2136 }
2137 /* Index version 4 uses a different hash function than index version
2138 5 and later.
2139
2140 Versions earlier than 6 did not emit psymbols for inlined
2141 functions. Using these files will cause GDB not to be able to
2142 set breakpoints on inlined functions by name, so we ignore these
2143 indices unless the --use-deprecated-index-sections command line
2144 option was supplied. */
2145 if (version < 6 && !use_deprecated_index_sections)
2146 {
2147 static int warning_printed = 0;
2148 if (!warning_printed)
2149 {
2150 warning (_("Skipping deprecated .gdb_index section in %s, pass "
2151 "--use-deprecated-index-sections to use them anyway"),
2152 objfile->name);
2153 warning_printed = 1;
2154 }
2155 return 0;
2156 }
2157 /* Indexes with higher version than the one supported by GDB may be no
2158 longer backward compatible. */
2159 if (version > 6)
2160 return 0;
2161
2162 map = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct mapped_index);
2163 map->version = version;
2164 map->total_size = dwarf2_per_objfile->gdb_index.size;
2165
2166 metadata = (offset_type *) (addr + sizeof (offset_type));
2167
2168 i = 0;
2169 cu_list = addr + MAYBE_SWAP (metadata[i]);
2170 cu_list_elements = ((MAYBE_SWAP (metadata[i + 1]) - MAYBE_SWAP (metadata[i]))
2171 / 8);
2172 ++i;
2173
2174 types_list = addr + MAYBE_SWAP (metadata[i]);
2175 types_list_elements = ((MAYBE_SWAP (metadata[i + 1])
2176 - MAYBE_SWAP (metadata[i]))
2177 / 8);
2178 ++i;
2179
2180 map->address_table = addr + MAYBE_SWAP (metadata[i]);
2181 map->address_table_size = (MAYBE_SWAP (metadata[i + 1])
2182 - MAYBE_SWAP (metadata[i]));
2183 ++i;
2184
2185 map->symbol_table = (offset_type *) (addr + MAYBE_SWAP (metadata[i]));
2186 map->symbol_table_slots = ((MAYBE_SWAP (metadata[i + 1])
2187 - MAYBE_SWAP (metadata[i]))
2188 / (2 * sizeof (offset_type)));
2189 ++i;
2190
2191 map->constant_pool = addr + MAYBE_SWAP (metadata[i]);
2192
2193 if (!create_cus_from_index (objfile, cu_list, cu_list_elements))
2194 return 0;
2195
2196 if (types_list_elements)
2197 {
2198 struct dwarf2_section_info *section;
2199
2200 /* We can only handle a single .debug_types when we have an
2201 index. */
2202 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) != 1)
2203 return 0;
2204
2205 section = VEC_index (dwarf2_section_info_def,
2206 dwarf2_per_objfile->types, 0);
2207
2208 if (!create_signatured_type_table_from_index (objfile, section,
2209 types_list,
2210 types_list_elements))
2211 return 0;
2212 }
2213
2214 create_addrmap_from_index (objfile, map);
2215
2216 dwarf2_per_objfile->index_table = map;
2217 dwarf2_per_objfile->using_index = 1;
2218 dwarf2_per_objfile->quick_file_names_table =
2219 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2220
2221 return 1;
2222 }
2223
2224 /* A helper for the "quick" functions which sets the global
2225 dwarf2_per_objfile according to OBJFILE. */
2226
2227 static void
2228 dw2_setup (struct objfile *objfile)
2229 {
2230 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
2231 gdb_assert (dwarf2_per_objfile);
2232 }
2233
2234 /* A helper for the "quick" functions which attempts to read the line
2235 table for THIS_CU. */
2236
2237 static struct quick_file_names *
2238 dw2_get_file_names (struct objfile *objfile,
2239 struct dwarf2_per_cu_data *this_cu)
2240 {
2241 bfd *abfd = objfile->obfd;
2242 struct line_header *lh;
2243 struct attribute *attr;
2244 struct cleanup *cleanups;
2245 struct die_info *comp_unit_die;
2246 struct dwarf2_section_info* sec;
2247 gdb_byte *info_ptr;
2248 int has_children, i;
2249 struct dwarf2_cu cu;
2250 unsigned int bytes_read;
2251 struct die_reader_specs reader_specs;
2252 char *name, *comp_dir;
2253 void **slot;
2254 struct quick_file_names *qfn;
2255 unsigned int line_offset;
2256
2257 if (this_cu->v.quick->file_names != NULL)
2258 return this_cu->v.quick->file_names;
2259 /* If we know there is no line data, no point in looking again. */
2260 if (this_cu->v.quick->no_file_data)
2261 return NULL;
2262
2263 init_one_comp_unit (&cu, this_cu);
2264 cleanups = make_cleanup (free_stack_comp_unit, &cu);
2265
2266 if (this_cu->debug_types_section)
2267 sec = this_cu->debug_types_section;
2268 else
2269 sec = &dwarf2_per_objfile->info;
2270 dwarf2_read_section (objfile, sec);
2271 info_ptr = sec->buffer + this_cu->offset;
2272
2273 info_ptr = read_and_check_comp_unit_head (&cu.header, sec, info_ptr,
2274 this_cu->debug_types_section != NULL);
2275
2276 /* Skip dummy compilation units. */
2277 if (info_ptr >= (sec->buffer + sec->size)
2278 || peek_abbrev_code (abfd, info_ptr) == 0)
2279 {
2280 do_cleanups (cleanups);
2281 return NULL;
2282 }
2283
2284 dwarf2_read_abbrevs (&cu);
2285 make_cleanup (dwarf2_free_abbrev_table, &cu);
2286
2287 init_cu_die_reader (&reader_specs, &cu);
2288 read_full_die (&reader_specs, &comp_unit_die, info_ptr,
2289 &has_children);
2290
2291 lh = NULL;
2292 slot = NULL;
2293 line_offset = 0;
2294 attr = dwarf2_attr (comp_unit_die, DW_AT_stmt_list, &cu);
2295 if (attr)
2296 {
2297 struct quick_file_names find_entry;
2298
2299 line_offset = DW_UNSND (attr);
2300
2301 /* We may have already read in this line header (TU line header sharing).
2302 If we have we're done. */
2303 find_entry.offset = line_offset;
2304 slot = htab_find_slot (dwarf2_per_objfile->quick_file_names_table,
2305 &find_entry, INSERT);
2306 if (*slot != NULL)
2307 {
2308 do_cleanups (cleanups);
2309 this_cu->v.quick->file_names = *slot;
2310 return *slot;
2311 }
2312
2313 lh = dwarf_decode_line_header (line_offset, abfd, &cu);
2314 }
2315 if (lh == NULL)
2316 {
2317 do_cleanups (cleanups);
2318 this_cu->v.quick->no_file_data = 1;
2319 return NULL;
2320 }
2321
2322 qfn = obstack_alloc (&objfile->objfile_obstack, sizeof (*qfn));
2323 qfn->offset = line_offset;
2324 gdb_assert (slot != NULL);
2325 *slot = qfn;
2326
2327 find_file_and_directory (comp_unit_die, &cu, &name, &comp_dir);
2328
2329 qfn->num_file_names = lh->num_file_names;
2330 qfn->file_names = obstack_alloc (&objfile->objfile_obstack,
2331 lh->num_file_names * sizeof (char *));
2332 for (i = 0; i < lh->num_file_names; ++i)
2333 qfn->file_names[i] = file_full_name (i + 1, lh, comp_dir);
2334 qfn->real_names = NULL;
2335
2336 free_line_header (lh);
2337 do_cleanups (cleanups);
2338
2339 this_cu->v.quick->file_names = qfn;
2340 return qfn;
2341 }
2342
2343 /* A helper for the "quick" functions which computes and caches the
2344 real path for a given file name from the line table. */
2345
2346 static const char *
2347 dw2_get_real_path (struct objfile *objfile,
2348 struct quick_file_names *qfn, int index)
2349 {
2350 if (qfn->real_names == NULL)
2351 qfn->real_names = OBSTACK_CALLOC (&objfile->objfile_obstack,
2352 qfn->num_file_names, sizeof (char *));
2353
2354 if (qfn->real_names[index] == NULL)
2355 qfn->real_names[index] = gdb_realpath (qfn->file_names[index]);
2356
2357 return qfn->real_names[index];
2358 }
2359
2360 static struct symtab *
2361 dw2_find_last_source_symtab (struct objfile *objfile)
2362 {
2363 int index;
2364
2365 dw2_setup (objfile);
2366 index = dwarf2_per_objfile->n_comp_units - 1;
2367 return dw2_instantiate_symtab (dw2_get_cu (index));
2368 }
2369
2370 /* Traversal function for dw2_forget_cached_source_info. */
2371
2372 static int
2373 dw2_free_cached_file_names (void **slot, void *info)
2374 {
2375 struct quick_file_names *file_data = (struct quick_file_names *) *slot;
2376
2377 if (file_data->real_names)
2378 {
2379 int i;
2380
2381 for (i = 0; i < file_data->num_file_names; ++i)
2382 {
2383 xfree ((void*) file_data->real_names[i]);
2384 file_data->real_names[i] = NULL;
2385 }
2386 }
2387
2388 return 1;
2389 }
2390
2391 static void
2392 dw2_forget_cached_source_info (struct objfile *objfile)
2393 {
2394 dw2_setup (objfile);
2395
2396 htab_traverse_noresize (dwarf2_per_objfile->quick_file_names_table,
2397 dw2_free_cached_file_names, NULL);
2398 }
2399
2400 /* Helper function for dw2_map_symtabs_matching_filename that expands
2401 the symtabs and calls the iterator. */
2402
2403 static int
2404 dw2_map_expand_apply (struct objfile *objfile,
2405 struct dwarf2_per_cu_data *per_cu,
2406 const char *name,
2407 const char *full_path, const char *real_path,
2408 int (*callback) (struct symtab *, void *),
2409 void *data)
2410 {
2411 struct symtab *last_made = objfile->symtabs;
2412
2413 /* Don't visit already-expanded CUs. */
2414 if (per_cu->v.quick->symtab)
2415 return 0;
2416
2417 /* This may expand more than one symtab, and we want to iterate over
2418 all of them. */
2419 dw2_instantiate_symtab (per_cu);
2420
2421 return iterate_over_some_symtabs (name, full_path, real_path, callback, data,
2422 objfile->symtabs, last_made);
2423 }
2424
2425 /* Implementation of the map_symtabs_matching_filename method. */
2426
2427 static int
2428 dw2_map_symtabs_matching_filename (struct objfile *objfile, const char *name,
2429 const char *full_path, const char *real_path,
2430 int (*callback) (struct symtab *, void *),
2431 void *data)
2432 {
2433 int i;
2434 const char *name_basename = lbasename (name);
2435 int name_len = strlen (name);
2436 int is_abs = IS_ABSOLUTE_PATH (name);
2437
2438 dw2_setup (objfile);
2439
2440 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2441 + dwarf2_per_objfile->n_type_units); ++i)
2442 {
2443 int j;
2444 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2445 struct quick_file_names *file_data;
2446
2447 /* We only need to look at symtabs not already expanded. */
2448 if (per_cu->v.quick->symtab)
2449 continue;
2450
2451 file_data = dw2_get_file_names (objfile, per_cu);
2452 if (file_data == NULL)
2453 continue;
2454
2455 for (j = 0; j < file_data->num_file_names; ++j)
2456 {
2457 const char *this_name = file_data->file_names[j];
2458
2459 if (FILENAME_CMP (name, this_name) == 0
2460 || (!is_abs && compare_filenames_for_search (this_name,
2461 name, name_len)))
2462 {
2463 if (dw2_map_expand_apply (objfile, per_cu,
2464 name, full_path, real_path,
2465 callback, data))
2466 return 1;
2467 }
2468
2469 /* Before we invoke realpath, which can get expensive when many
2470 files are involved, do a quick comparison of the basenames. */
2471 if (! basenames_may_differ
2472 && FILENAME_CMP (lbasename (this_name), name_basename) != 0)
2473 continue;
2474
2475 if (full_path != NULL)
2476 {
2477 const char *this_real_name = dw2_get_real_path (objfile,
2478 file_data, j);
2479
2480 if (this_real_name != NULL
2481 && (FILENAME_CMP (full_path, this_real_name) == 0
2482 || (!is_abs
2483 && compare_filenames_for_search (this_real_name,
2484 name, name_len))))
2485 {
2486 if (dw2_map_expand_apply (objfile, per_cu,
2487 name, full_path, real_path,
2488 callback, data))
2489 return 1;
2490 }
2491 }
2492
2493 if (real_path != NULL)
2494 {
2495 const char *this_real_name = dw2_get_real_path (objfile,
2496 file_data, j);
2497
2498 if (this_real_name != NULL
2499 && (FILENAME_CMP (real_path, this_real_name) == 0
2500 || (!is_abs
2501 && compare_filenames_for_search (this_real_name,
2502 name, name_len))))
2503 {
2504 if (dw2_map_expand_apply (objfile, per_cu,
2505 name, full_path, real_path,
2506 callback, data))
2507 return 1;
2508 }
2509 }
2510 }
2511 }
2512
2513 return 0;
2514 }
2515
2516 static struct symtab *
2517 dw2_lookup_symbol (struct objfile *objfile, int block_index,
2518 const char *name, domain_enum domain)
2519 {
2520 /* We do all the work in the pre_expand_symtabs_matching hook
2521 instead. */
2522 return NULL;
2523 }
2524
2525 /* A helper function that expands all symtabs that hold an object
2526 named NAME. */
2527
2528 static void
2529 dw2_do_expand_symtabs_matching (struct objfile *objfile, const char *name)
2530 {
2531 dw2_setup (objfile);
2532
2533 /* index_table is NULL if OBJF_READNOW. */
2534 if (dwarf2_per_objfile->index_table)
2535 {
2536 offset_type *vec;
2537
2538 if (find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2539 name, &vec))
2540 {
2541 offset_type i, len = MAYBE_SWAP (*vec);
2542 for (i = 0; i < len; ++i)
2543 {
2544 offset_type cu_index = MAYBE_SWAP (vec[i + 1]);
2545 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (cu_index);
2546
2547 dw2_instantiate_symtab (per_cu);
2548 }
2549 }
2550 }
2551 }
2552
2553 static void
2554 dw2_pre_expand_symtabs_matching (struct objfile *objfile,
2555 enum block_enum block_kind, const char *name,
2556 domain_enum domain)
2557 {
2558 dw2_do_expand_symtabs_matching (objfile, name);
2559 }
2560
2561 static void
2562 dw2_print_stats (struct objfile *objfile)
2563 {
2564 int i, count;
2565
2566 dw2_setup (objfile);
2567 count = 0;
2568 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2569 + dwarf2_per_objfile->n_type_units); ++i)
2570 {
2571 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2572
2573 if (!per_cu->v.quick->symtab)
2574 ++count;
2575 }
2576 printf_filtered (_(" Number of unread CUs: %d\n"), count);
2577 }
2578
2579 static void
2580 dw2_dump (struct objfile *objfile)
2581 {
2582 /* Nothing worth printing. */
2583 }
2584
2585 static void
2586 dw2_relocate (struct objfile *objfile, struct section_offsets *new_offsets,
2587 struct section_offsets *delta)
2588 {
2589 /* There's nothing to relocate here. */
2590 }
2591
2592 static void
2593 dw2_expand_symtabs_for_function (struct objfile *objfile,
2594 const char *func_name)
2595 {
2596 dw2_do_expand_symtabs_matching (objfile, func_name);
2597 }
2598
2599 static void
2600 dw2_expand_all_symtabs (struct objfile *objfile)
2601 {
2602 int i;
2603
2604 dw2_setup (objfile);
2605
2606 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2607 + dwarf2_per_objfile->n_type_units); ++i)
2608 {
2609 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2610
2611 dw2_instantiate_symtab (per_cu);
2612 }
2613 }
2614
2615 static void
2616 dw2_expand_symtabs_with_filename (struct objfile *objfile,
2617 const char *filename)
2618 {
2619 int i;
2620
2621 dw2_setup (objfile);
2622
2623 /* We don't need to consider type units here.
2624 This is only called for examining code, e.g. expand_line_sal.
2625 There can be an order of magnitude (or more) more type units
2626 than comp units, and we avoid them if we can. */
2627
2628 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
2629 {
2630 int j;
2631 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2632 struct quick_file_names *file_data;
2633
2634 /* We only need to look at symtabs not already expanded. */
2635 if (per_cu->v.quick->symtab)
2636 continue;
2637
2638 file_data = dw2_get_file_names (objfile, per_cu);
2639 if (file_data == NULL)
2640 continue;
2641
2642 for (j = 0; j < file_data->num_file_names; ++j)
2643 {
2644 const char *this_name = file_data->file_names[j];
2645 if (FILENAME_CMP (this_name, filename) == 0)
2646 {
2647 dw2_instantiate_symtab (per_cu);
2648 break;
2649 }
2650 }
2651 }
2652 }
2653
2654 static const char *
2655 dw2_find_symbol_file (struct objfile *objfile, const char *name)
2656 {
2657 struct dwarf2_per_cu_data *per_cu;
2658 offset_type *vec;
2659 struct quick_file_names *file_data;
2660
2661 dw2_setup (objfile);
2662
2663 /* index_table is NULL if OBJF_READNOW. */
2664 if (!dwarf2_per_objfile->index_table)
2665 {
2666 struct symtab *s;
2667
2668 ALL_OBJFILE_SYMTABS (objfile, s)
2669 if (s->primary)
2670 {
2671 struct blockvector *bv = BLOCKVECTOR (s);
2672 const struct block *block = BLOCKVECTOR_BLOCK (bv, GLOBAL_BLOCK);
2673 struct symbol *sym = lookup_block_symbol (block, name, VAR_DOMAIN);
2674
2675 if (sym)
2676 return sym->symtab->filename;
2677 }
2678 return NULL;
2679 }
2680
2681 if (!find_slot_in_mapped_hash (dwarf2_per_objfile->index_table,
2682 name, &vec))
2683 return NULL;
2684
2685 /* Note that this just looks at the very first one named NAME -- but
2686 actually we are looking for a function. find_main_filename
2687 should be rewritten so that it doesn't require a custom hook. It
2688 could just use the ordinary symbol tables. */
2689 /* vec[0] is the length, which must always be >0. */
2690 per_cu = dw2_get_cu (MAYBE_SWAP (vec[1]));
2691
2692 file_data = dw2_get_file_names (objfile, per_cu);
2693 if (file_data == NULL)
2694 return NULL;
2695
2696 return file_data->file_names[file_data->num_file_names - 1];
2697 }
2698
2699 static void
2700 dw2_map_matching_symbols (const char * name, domain_enum namespace,
2701 struct objfile *objfile, int global,
2702 int (*callback) (struct block *,
2703 struct symbol *, void *),
2704 void *data, symbol_compare_ftype *match,
2705 symbol_compare_ftype *ordered_compare)
2706 {
2707 /* Currently unimplemented; used for Ada. The function can be called if the
2708 current language is Ada for a non-Ada objfile using GNU index. As Ada
2709 does not look for non-Ada symbols this function should just return. */
2710 }
2711
2712 static void
2713 dw2_expand_symtabs_matching
2714 (struct objfile *objfile,
2715 int (*file_matcher) (const char *, void *),
2716 int (*name_matcher) (const char *, void *),
2717 enum search_domain kind,
2718 void *data)
2719 {
2720 int i;
2721 offset_type iter;
2722 struct mapped_index *index;
2723
2724 dw2_setup (objfile);
2725
2726 /* index_table is NULL if OBJF_READNOW. */
2727 if (!dwarf2_per_objfile->index_table)
2728 return;
2729 index = dwarf2_per_objfile->index_table;
2730
2731 if (file_matcher != NULL)
2732 {
2733 struct cleanup *cleanup;
2734 htab_t visited_found, visited_not_found;
2735
2736 visited_found = htab_create_alloc (10,
2737 htab_hash_pointer, htab_eq_pointer,
2738 NULL, xcalloc, xfree);
2739 cleanup = make_cleanup_htab_delete (visited_found);
2740 visited_not_found = htab_create_alloc (10,
2741 htab_hash_pointer, htab_eq_pointer,
2742 NULL, xcalloc, xfree);
2743 make_cleanup_htab_delete (visited_not_found);
2744
2745 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2746 + dwarf2_per_objfile->n_type_units); ++i)
2747 {
2748 int j;
2749 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2750 struct quick_file_names *file_data;
2751 void **slot;
2752
2753 per_cu->v.quick->mark = 0;
2754
2755 /* We only need to look at symtabs not already expanded. */
2756 if (per_cu->v.quick->symtab)
2757 continue;
2758
2759 file_data = dw2_get_file_names (objfile, per_cu);
2760 if (file_data == NULL)
2761 continue;
2762
2763 if (htab_find (visited_not_found, file_data) != NULL)
2764 continue;
2765 else if (htab_find (visited_found, file_data) != NULL)
2766 {
2767 per_cu->v.quick->mark = 1;
2768 continue;
2769 }
2770
2771 for (j = 0; j < file_data->num_file_names; ++j)
2772 {
2773 if (file_matcher (file_data->file_names[j], data))
2774 {
2775 per_cu->v.quick->mark = 1;
2776 break;
2777 }
2778 }
2779
2780 slot = htab_find_slot (per_cu->v.quick->mark
2781 ? visited_found
2782 : visited_not_found,
2783 file_data, INSERT);
2784 *slot = file_data;
2785 }
2786
2787 do_cleanups (cleanup);
2788 }
2789
2790 for (iter = 0; iter < index->symbol_table_slots; ++iter)
2791 {
2792 offset_type idx = 2 * iter;
2793 const char *name;
2794 offset_type *vec, vec_len, vec_idx;
2795
2796 if (index->symbol_table[idx] == 0 && index->symbol_table[idx + 1] == 0)
2797 continue;
2798
2799 name = index->constant_pool + MAYBE_SWAP (index->symbol_table[idx]);
2800
2801 if (! (*name_matcher) (name, data))
2802 continue;
2803
2804 /* The name was matched, now expand corresponding CUs that were
2805 marked. */
2806 vec = (offset_type *) (index->constant_pool
2807 + MAYBE_SWAP (index->symbol_table[idx + 1]));
2808 vec_len = MAYBE_SWAP (vec[0]);
2809 for (vec_idx = 0; vec_idx < vec_len; ++vec_idx)
2810 {
2811 struct dwarf2_per_cu_data *per_cu;
2812
2813 per_cu = dw2_get_cu (MAYBE_SWAP (vec[vec_idx + 1]));
2814 if (file_matcher == NULL || per_cu->v.quick->mark)
2815 dw2_instantiate_symtab (per_cu);
2816 }
2817 }
2818 }
2819
2820 static struct symtab *
2821 dw2_find_pc_sect_symtab (struct objfile *objfile,
2822 struct minimal_symbol *msymbol,
2823 CORE_ADDR pc,
2824 struct obj_section *section,
2825 int warn_if_readin)
2826 {
2827 struct dwarf2_per_cu_data *data;
2828
2829 dw2_setup (objfile);
2830
2831 if (!objfile->psymtabs_addrmap)
2832 return NULL;
2833
2834 data = addrmap_find (objfile->psymtabs_addrmap, pc);
2835 if (!data)
2836 return NULL;
2837
2838 if (warn_if_readin && data->v.quick->symtab)
2839 warning (_("(Internal error: pc %s in read in CU, but not in symtab.)"),
2840 paddress (get_objfile_arch (objfile), pc));
2841
2842 return dw2_instantiate_symtab (data);
2843 }
2844
2845 static void
2846 dw2_map_symbol_filenames (struct objfile *objfile, symbol_filename_ftype *fun,
2847 void *data, int need_fullname)
2848 {
2849 int i;
2850 struct cleanup *cleanup;
2851 htab_t visited = htab_create_alloc (10, htab_hash_pointer, htab_eq_pointer,
2852 NULL, xcalloc, xfree);
2853
2854 cleanup = make_cleanup_htab_delete (visited);
2855 dw2_setup (objfile);
2856
2857 /* We can ignore file names coming from already-expanded CUs. */
2858 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2859 + dwarf2_per_objfile->n_type_units); ++i)
2860 {
2861 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2862
2863 if (per_cu->v.quick->symtab)
2864 {
2865 void **slot = htab_find_slot (visited, per_cu->v.quick->file_names,
2866 INSERT);
2867
2868 *slot = per_cu->v.quick->file_names;
2869 }
2870 }
2871
2872 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2873 + dwarf2_per_objfile->n_type_units); ++i)
2874 {
2875 int j;
2876 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2877 struct quick_file_names *file_data;
2878 void **slot;
2879
2880 /* We only need to look at symtabs not already expanded. */
2881 if (per_cu->v.quick->symtab)
2882 continue;
2883
2884 file_data = dw2_get_file_names (objfile, per_cu);
2885 if (file_data == NULL)
2886 continue;
2887
2888 slot = htab_find_slot (visited, file_data, INSERT);
2889 if (*slot)
2890 {
2891 /* Already visited. */
2892 continue;
2893 }
2894 *slot = file_data;
2895
2896 for (j = 0; j < file_data->num_file_names; ++j)
2897 {
2898 const char *this_real_name;
2899
2900 if (need_fullname)
2901 this_real_name = dw2_get_real_path (objfile, file_data, j);
2902 else
2903 this_real_name = NULL;
2904 (*fun) (file_data->file_names[j], this_real_name, data);
2905 }
2906 }
2907
2908 do_cleanups (cleanup);
2909 }
2910
2911 static int
2912 dw2_has_symbols (struct objfile *objfile)
2913 {
2914 return 1;
2915 }
2916
2917 const struct quick_symbol_functions dwarf2_gdb_index_functions =
2918 {
2919 dw2_has_symbols,
2920 dw2_find_last_source_symtab,
2921 dw2_forget_cached_source_info,
2922 dw2_map_symtabs_matching_filename,
2923 dw2_lookup_symbol,
2924 dw2_pre_expand_symtabs_matching,
2925 dw2_print_stats,
2926 dw2_dump,
2927 dw2_relocate,
2928 dw2_expand_symtabs_for_function,
2929 dw2_expand_all_symtabs,
2930 dw2_expand_symtabs_with_filename,
2931 dw2_find_symbol_file,
2932 dw2_map_matching_symbols,
2933 dw2_expand_symtabs_matching,
2934 dw2_find_pc_sect_symtab,
2935 dw2_map_symbol_filenames
2936 };
2937
2938 /* Initialize for reading DWARF for this objfile. Return 0 if this
2939 file will use psymtabs, or 1 if using the GNU index. */
2940
2941 int
2942 dwarf2_initialize_objfile (struct objfile *objfile)
2943 {
2944 /* If we're about to read full symbols, don't bother with the
2945 indices. In this case we also don't care if some other debug
2946 format is making psymtabs, because they are all about to be
2947 expanded anyway. */
2948 if ((objfile->flags & OBJF_READNOW))
2949 {
2950 int i;
2951
2952 dwarf2_per_objfile->using_index = 1;
2953 create_all_comp_units (objfile);
2954 create_debug_types_hash_table (objfile);
2955 dwarf2_per_objfile->quick_file_names_table =
2956 create_quick_file_names_table (dwarf2_per_objfile->n_comp_units);
2957
2958 for (i = 0; i < (dwarf2_per_objfile->n_comp_units
2959 + dwarf2_per_objfile->n_type_units); ++i)
2960 {
2961 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
2962
2963 per_cu->v.quick = OBSTACK_ZALLOC (&objfile->objfile_obstack,
2964 struct dwarf2_per_cu_quick_data);
2965 }
2966
2967 /* Return 1 so that gdb sees the "quick" functions. However,
2968 these functions will be no-ops because we will have expanded
2969 all symtabs. */
2970 return 1;
2971 }
2972
2973 if (dwarf2_read_index (objfile))
2974 return 1;
2975
2976 return 0;
2977 }
2978
2979 \f
2980
2981 /* Build a partial symbol table. */
2982
2983 void
2984 dwarf2_build_psymtabs (struct objfile *objfile)
2985 {
2986 if (objfile->global_psymbols.size == 0 && objfile->static_psymbols.size == 0)
2987 {
2988 init_psymbol_list (objfile, 1024);
2989 }
2990
2991 dwarf2_build_psymtabs_hard (objfile);
2992 }
2993
2994 /* Return TRUE if OFFSET is within CU_HEADER. */
2995
2996 static inline int
2997 offset_in_cu_p (const struct comp_unit_head *cu_header, unsigned int offset)
2998 {
2999 unsigned int bottom = cu_header->offset;
3000 unsigned int top = (cu_header->offset
3001 + cu_header->length
3002 + cu_header->initial_length_size);
3003
3004 return (offset >= bottom && offset < top);
3005 }
3006
3007 /* Read in the comp unit header information from the debug_info at info_ptr.
3008 NOTE: This leaves members offset, first_die_offset to be filled in
3009 by the caller. */
3010
3011 static gdb_byte *
3012 read_comp_unit_head (struct comp_unit_head *cu_header,
3013 gdb_byte *info_ptr, bfd *abfd)
3014 {
3015 int signed_addr;
3016 unsigned int bytes_read;
3017
3018 cu_header->length = read_initial_length (abfd, info_ptr, &bytes_read);
3019 cu_header->initial_length_size = bytes_read;
3020 cu_header->offset_size = (bytes_read == 4) ? 4 : 8;
3021 info_ptr += bytes_read;
3022 cu_header->version = read_2_bytes (abfd, info_ptr);
3023 info_ptr += 2;
3024 cu_header->abbrev_offset = read_offset (abfd, info_ptr, cu_header,
3025 &bytes_read);
3026 info_ptr += bytes_read;
3027 cu_header->addr_size = read_1_byte (abfd, info_ptr);
3028 info_ptr += 1;
3029 signed_addr = bfd_get_sign_extend_vma (abfd);
3030 if (signed_addr < 0)
3031 internal_error (__FILE__, __LINE__,
3032 _("read_comp_unit_head: dwarf from non elf file"));
3033 cu_header->signed_addr_p = signed_addr;
3034
3035 return info_ptr;
3036 }
3037
3038 /* Subroutine of read_and_check_comp_unit_head and
3039 read_and_check_type_unit_head to simplify them.
3040 Perform various error checking on the header. */
3041
3042 static void
3043 error_check_comp_unit_head (struct comp_unit_head *header,
3044 struct dwarf2_section_info *section)
3045 {
3046 bfd *abfd = section->asection->owner;
3047 const char *filename = bfd_get_filename (abfd);
3048
3049 if (header->version != 2 && header->version != 3 && header->version != 4)
3050 error (_("Dwarf Error: wrong version in compilation unit header "
3051 "(is %d, should be 2, 3, or 4) [in module %s]"), header->version,
3052 filename);
3053
3054 if (header->abbrev_offset
3055 >= dwarf2_section_size (dwarf2_per_objfile->objfile,
3056 &dwarf2_per_objfile->abbrev))
3057 error (_("Dwarf Error: bad offset (0x%lx) in compilation unit header "
3058 "(offset 0x%lx + 6) [in module %s]"),
3059 (long) header->abbrev_offset, (long) header->offset,
3060 filename);
3061
3062 /* Cast to unsigned long to use 64-bit arithmetic when possible to
3063 avoid potential 32-bit overflow. */
3064 if (((unsigned long) header->offset
3065 + header->length + header->initial_length_size)
3066 > section->size)
3067 error (_("Dwarf Error: bad length (0x%lx) in compilation unit header "
3068 "(offset 0x%lx + 0) [in module %s]"),
3069 (long) header->length, (long) header->offset,
3070 filename);
3071 }
3072
3073 /* Read in a CU/TU header and perform some basic error checking.
3074 The contents of the header are stored in HEADER.
3075 The result is a pointer to the start of the first DIE. */
3076
3077 static gdb_byte *
3078 read_and_check_comp_unit_head (struct comp_unit_head *header,
3079 struct dwarf2_section_info *section,
3080 gdb_byte *info_ptr,
3081 int is_debug_types_section)
3082 {
3083 gdb_byte *beg_of_comp_unit = info_ptr;
3084 bfd *abfd = section->asection->owner;
3085
3086 header->offset = beg_of_comp_unit - section->buffer;
3087
3088 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3089
3090 /* If we're reading a type unit, skip over the signature and
3091 type_offset fields. */
3092 if (is_debug_types_section)
3093 info_ptr += 8 /*signature*/ + header->offset_size;
3094
3095 header->first_die_offset = info_ptr - beg_of_comp_unit;
3096
3097 error_check_comp_unit_head (header, section);
3098
3099 return info_ptr;
3100 }
3101
3102 /* Read in the types comp unit header information from .debug_types entry at
3103 types_ptr. The result is a pointer to one past the end of the header. */
3104
3105 static gdb_byte *
3106 read_and_check_type_unit_head (struct comp_unit_head *header,
3107 struct dwarf2_section_info *section,
3108 gdb_byte *info_ptr,
3109 ULONGEST *signature, unsigned int *type_offset)
3110 {
3111 gdb_byte *beg_of_comp_unit = info_ptr;
3112 bfd *abfd = section->asection->owner;
3113
3114 header->offset = beg_of_comp_unit - section->buffer;
3115
3116 info_ptr = read_comp_unit_head (header, info_ptr, abfd);
3117
3118 /* If we're reading a type unit, skip over the signature and
3119 type_offset fields. */
3120 if (signature != NULL)
3121 *signature = read_8_bytes (abfd, info_ptr);
3122 info_ptr += 8;
3123 if (type_offset != NULL)
3124 *type_offset = read_offset_1 (abfd, info_ptr, header->offset_size);
3125 info_ptr += header->offset_size;
3126
3127 header->first_die_offset = info_ptr - beg_of_comp_unit;
3128
3129 error_check_comp_unit_head (header, section);
3130
3131 return info_ptr;
3132 }
3133
3134 /* Allocate a new partial symtab for file named NAME and mark this new
3135 partial symtab as being an include of PST. */
3136
3137 static void
3138 dwarf2_create_include_psymtab (char *name, struct partial_symtab *pst,
3139 struct objfile *objfile)
3140 {
3141 struct partial_symtab *subpst = allocate_psymtab (name, objfile);
3142
3143 subpst->section_offsets = pst->section_offsets;
3144 subpst->textlow = 0;
3145 subpst->texthigh = 0;
3146
3147 subpst->dependencies = (struct partial_symtab **)
3148 obstack_alloc (&objfile->objfile_obstack,
3149 sizeof (struct partial_symtab *));
3150 subpst->dependencies[0] = pst;
3151 subpst->number_of_dependencies = 1;
3152
3153 subpst->globals_offset = 0;
3154 subpst->n_global_syms = 0;
3155 subpst->statics_offset = 0;
3156 subpst->n_static_syms = 0;
3157 subpst->symtab = NULL;
3158 subpst->read_symtab = pst->read_symtab;
3159 subpst->readin = 0;
3160
3161 /* No private part is necessary for include psymtabs. This property
3162 can be used to differentiate between such include psymtabs and
3163 the regular ones. */
3164 subpst->read_symtab_private = NULL;
3165 }
3166
3167 /* Read the Line Number Program data and extract the list of files
3168 included by the source file represented by PST. Build an include
3169 partial symtab for each of these included files. */
3170
3171 static void
3172 dwarf2_build_include_psymtabs (struct dwarf2_cu *cu,
3173 struct die_info *die,
3174 struct partial_symtab *pst)
3175 {
3176 struct objfile *objfile = cu->objfile;
3177 bfd *abfd = objfile->obfd;
3178 struct line_header *lh = NULL;
3179 struct attribute *attr;
3180
3181 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
3182 if (attr)
3183 {
3184 unsigned int line_offset = DW_UNSND (attr);
3185
3186 lh = dwarf_decode_line_header (line_offset, abfd, cu);
3187 }
3188 if (lh == NULL)
3189 return; /* No linetable, so no includes. */
3190
3191 /* NOTE: pst->dirname is DW_AT_comp_dir (if present). */
3192 dwarf_decode_lines (lh, pst->dirname, cu, pst, 1);
3193
3194 free_line_header (lh);
3195 }
3196
3197 static hashval_t
3198 hash_type_signature (const void *item)
3199 {
3200 const struct signatured_type *type_sig = item;
3201
3202 /* This drops the top 32 bits of the signature, but is ok for a hash. */
3203 return type_sig->signature;
3204 }
3205
3206 static int
3207 eq_type_signature (const void *item_lhs, const void *item_rhs)
3208 {
3209 const struct signatured_type *lhs = item_lhs;
3210 const struct signatured_type *rhs = item_rhs;
3211
3212 return lhs->signature == rhs->signature;
3213 }
3214
3215 /* Allocate a hash table for signatured types. */
3216
3217 static htab_t
3218 allocate_signatured_type_table (struct objfile *objfile)
3219 {
3220 return htab_create_alloc_ex (41,
3221 hash_type_signature,
3222 eq_type_signature,
3223 NULL,
3224 &objfile->objfile_obstack,
3225 hashtab_obstack_allocate,
3226 dummy_obstack_deallocate);
3227 }
3228
3229 /* A helper function to add a signatured type CU to a table. */
3230
3231 static int
3232 add_signatured_type_cu_to_table (void **slot, void *datum)
3233 {
3234 struct signatured_type *sigt = *slot;
3235 struct dwarf2_per_cu_data ***datap = datum;
3236
3237 **datap = &sigt->per_cu;
3238 ++*datap;
3239
3240 return 1;
3241 }
3242
3243 /* Create the hash table of all entries in the .debug_types section(s).
3244 The result is zero if there is an error (e.g. missing .debug_types section),
3245 otherwise non-zero. */
3246
3247 static int
3248 create_debug_types_hash_table (struct objfile *objfile)
3249 {
3250 htab_t types_htab = NULL;
3251 struct dwarf2_per_cu_data **iter;
3252 int ix;
3253 struct dwarf2_section_info *section;
3254
3255 if (VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types))
3256 {
3257 dwarf2_per_objfile->signatured_types = NULL;
3258 return 0;
3259 }
3260
3261 for (ix = 0;
3262 VEC_iterate (dwarf2_section_info_def, dwarf2_per_objfile->types,
3263 ix, section);
3264 ++ix)
3265 {
3266 gdb_byte *info_ptr, *end_ptr;
3267
3268 dwarf2_read_section (objfile, section);
3269 info_ptr = section->buffer;
3270
3271 if (info_ptr == NULL)
3272 continue;
3273
3274 if (types_htab == NULL)
3275 types_htab = allocate_signatured_type_table (objfile);
3276
3277 if (dwarf2_die_debug)
3278 fprintf_unfiltered (gdb_stdlog, "Signatured types:\n");
3279
3280 end_ptr = info_ptr + section->size;
3281 while (info_ptr < end_ptr)
3282 {
3283 unsigned int offset;
3284 unsigned int type_offset;
3285 ULONGEST signature;
3286 struct signatured_type *type_sig;
3287 void **slot;
3288 gdb_byte *ptr = info_ptr;
3289 struct comp_unit_head header;
3290
3291 offset = ptr - section->buffer;
3292
3293 /* We need to read the type's signature in order to build the hash
3294 table, but we don't need anything else just yet. */
3295
3296 ptr = read_and_check_type_unit_head (&header, section, ptr,
3297 &signature, &type_offset);
3298
3299 /* Skip dummy type units. */
3300 if (ptr >= end_ptr || peek_abbrev_code (objfile->obfd, ptr) == 0)
3301 {
3302 info_ptr = info_ptr + header.initial_length_size + header.length;
3303 continue;
3304 }
3305
3306 type_sig = obstack_alloc (&objfile->objfile_obstack, sizeof (*type_sig));
3307 memset (type_sig, 0, sizeof (*type_sig));
3308 type_sig->signature = signature;
3309 type_sig->type_offset = type_offset;
3310 type_sig->per_cu.objfile = objfile;
3311 type_sig->per_cu.debug_types_section = section;
3312 type_sig->per_cu.offset = offset;
3313
3314 slot = htab_find_slot (types_htab, type_sig, INSERT);
3315 gdb_assert (slot != NULL);
3316 if (*slot != NULL)
3317 {
3318 const struct signatured_type *dup_sig = *slot;
3319
3320 complaint (&symfile_complaints,
3321 _("debug type entry at offset 0x%x is duplicate to the "
3322 "entry at offset 0x%x, signature 0x%s"),
3323 offset, dup_sig->per_cu.offset,
3324 phex (signature, sizeof (signature)));
3325 gdb_assert (signature == dup_sig->signature);
3326 }
3327 *slot = type_sig;
3328
3329 if (dwarf2_die_debug)
3330 fprintf_unfiltered (gdb_stdlog, " offset 0x%x, signature 0x%s\n",
3331 offset, phex (signature, sizeof (signature)));
3332
3333 info_ptr = info_ptr + header.initial_length_size + header.length;
3334 }
3335 }
3336
3337 dwarf2_per_objfile->signatured_types = types_htab;
3338
3339 dwarf2_per_objfile->n_type_units = htab_elements (types_htab);
3340 dwarf2_per_objfile->all_type_units
3341 = obstack_alloc (&objfile->objfile_obstack,
3342 dwarf2_per_objfile->n_type_units
3343 * sizeof (struct dwarf2_per_cu_data *));
3344 iter = &dwarf2_per_objfile->all_type_units[0];
3345 htab_traverse_noresize (types_htab, add_signatured_type_cu_to_table, &iter);
3346 gdb_assert (iter - &dwarf2_per_objfile->all_type_units[0]
3347 == dwarf2_per_objfile->n_type_units);
3348
3349 return 1;
3350 }
3351
3352 /* Lookup a signature based type.
3353 Returns NULL if SIG is not present in the table. */
3354
3355 static struct signatured_type *
3356 lookup_signatured_type (struct objfile *objfile, ULONGEST sig)
3357 {
3358 struct signatured_type find_entry, *entry;
3359
3360 if (dwarf2_per_objfile->signatured_types == NULL)
3361 {
3362 complaint (&symfile_complaints,
3363 _("missing `.debug_types' section for DW_FORM_ref_sig8 die"));
3364 return 0;
3365 }
3366
3367 find_entry.signature = sig;
3368 entry = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
3369 return entry;
3370 }
3371
3372 /* Initialize a die_reader_specs struct from a dwarf2_cu struct. */
3373
3374 static void
3375 init_cu_die_reader (struct die_reader_specs *reader,
3376 struct dwarf2_cu *cu)
3377 {
3378 reader->abfd = cu->objfile->obfd;
3379 reader->cu = cu;
3380 if (cu->per_cu->debug_types_section)
3381 {
3382 gdb_assert (cu->per_cu->debug_types_section->readin);
3383 reader->buffer = cu->per_cu->debug_types_section->buffer;
3384 }
3385 else
3386 {
3387 gdb_assert (dwarf2_per_objfile->info.readin);
3388 reader->buffer = dwarf2_per_objfile->info.buffer;
3389 }
3390 }
3391
3392 /* Find the base address of the compilation unit for range lists and
3393 location lists. It will normally be specified by DW_AT_low_pc.
3394 In DWARF-3 draft 4, the base address could be overridden by
3395 DW_AT_entry_pc. It's been removed, but GCC still uses this for
3396 compilation units with discontinuous ranges. */
3397
3398 static void
3399 dwarf2_find_base_address (struct die_info *die, struct dwarf2_cu *cu)
3400 {
3401 struct attribute *attr;
3402
3403 cu->base_known = 0;
3404 cu->base_address = 0;
3405
3406 attr = dwarf2_attr (die, DW_AT_entry_pc, cu);
3407 if (attr)
3408 {
3409 cu->base_address = DW_ADDR (attr);
3410 cu->base_known = 1;
3411 }
3412 else
3413 {
3414 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
3415 if (attr)
3416 {
3417 cu->base_address = DW_ADDR (attr);
3418 cu->base_known = 1;
3419 }
3420 }
3421 }
3422
3423 /* Subroutine of process_type_comp_unit and dwarf2_build_psymtabs_hard
3424 to combine the common parts.
3425 Process compilation unit THIS_CU for a psymtab.
3426 SECTION is the section the CU/TU comes from,
3427 either .debug_info or .debug_types. */
3428
3429 static void
3430 process_psymtab_comp_unit (struct dwarf2_per_cu_data *this_cu,
3431 struct dwarf2_section_info *section,
3432 int is_debug_types_section)
3433 {
3434 struct objfile *objfile = this_cu->objfile;
3435 bfd *abfd = objfile->obfd;
3436 gdb_byte *buffer = section->buffer;
3437 gdb_byte *info_ptr = buffer + this_cu->offset;
3438 unsigned int buffer_size = section->size;
3439 gdb_byte *beg_of_comp_unit = info_ptr;
3440 struct die_info *comp_unit_die;
3441 struct partial_symtab *pst;
3442 CORE_ADDR baseaddr;
3443 struct cleanup *back_to_inner;
3444 struct dwarf2_cu cu;
3445 int has_children, has_pc_info;
3446 struct attribute *attr;
3447 CORE_ADDR best_lowpc = 0, best_highpc = 0;
3448 struct die_reader_specs reader_specs;
3449 const char *filename;
3450
3451 /* If this compilation unit was already read in, free the
3452 cached copy in order to read it in again. This is
3453 necessary because we skipped some symbols when we first
3454 read in the compilation unit (see load_partial_dies).
3455 This problem could be avoided, but the benefit is
3456 unclear. */
3457 if (this_cu->cu != NULL)
3458 free_one_cached_comp_unit (this_cu->cu);
3459
3460 /* Note that this is a pointer to our stack frame, being
3461 added to a global data structure. It will be cleaned up
3462 in free_stack_comp_unit when we finish with this
3463 compilation unit. */
3464 init_one_comp_unit (&cu, this_cu);
3465 back_to_inner = make_cleanup (free_stack_comp_unit, &cu);
3466
3467 info_ptr = read_and_check_comp_unit_head (&cu.header, section, info_ptr,
3468 is_debug_types_section);
3469
3470 /* Skip dummy compilation units. */
3471 if (info_ptr >= buffer + buffer_size
3472 || peek_abbrev_code (abfd, info_ptr) == 0)
3473 {
3474 do_cleanups (back_to_inner);
3475 return;
3476 }
3477
3478 cu.list_in_scope = &file_symbols;
3479
3480 /* Read the abbrevs for this compilation unit into a table. */
3481 dwarf2_read_abbrevs (&cu);
3482 make_cleanup (dwarf2_free_abbrev_table, &cu);
3483
3484 /* Read the compilation unit die. */
3485 init_cu_die_reader (&reader_specs, &cu);
3486 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3487 &has_children);
3488
3489 if (is_debug_types_section)
3490 {
3491 /* LENGTH has not been set yet for type units. */
3492 gdb_assert (this_cu->offset == cu.header.offset);
3493 this_cu->length = cu.header.length + cu.header.initial_length_size;
3494 }
3495 else if (comp_unit_die->tag == DW_TAG_partial_unit)
3496 {
3497 do_cleanups (back_to_inner);
3498 return;
3499 }
3500
3501 prepare_one_comp_unit (&cu, comp_unit_die);
3502
3503 /* Allocate a new partial symbol table structure. */
3504 attr = dwarf2_attr (comp_unit_die, DW_AT_name, &cu);
3505 if (attr == NULL || !DW_STRING (attr))
3506 filename = "";
3507 else
3508 filename = DW_STRING (attr);
3509 pst = start_psymtab_common (objfile, objfile->section_offsets,
3510 filename,
3511 /* TEXTLOW and TEXTHIGH are set below. */
3512 0,
3513 objfile->global_psymbols.next,
3514 objfile->static_psymbols.next);
3515 pst->psymtabs_addrmap_supported = 1;
3516
3517 attr = dwarf2_attr (comp_unit_die, DW_AT_comp_dir, &cu);
3518 if (attr != NULL)
3519 pst->dirname = DW_STRING (attr);
3520
3521 pst->read_symtab_private = this_cu;
3522
3523 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
3524
3525 /* Store the function that reads in the rest of the symbol table. */
3526 pst->read_symtab = dwarf2_psymtab_to_symtab;
3527
3528 this_cu->v.psymtab = pst;
3529
3530 dwarf2_find_base_address (comp_unit_die, &cu);
3531
3532 /* Possibly set the default values of LOWPC and HIGHPC from
3533 `DW_AT_ranges'. */
3534 has_pc_info = dwarf2_get_pc_bounds (comp_unit_die, &best_lowpc,
3535 &best_highpc, &cu, pst);
3536 if (has_pc_info == 1 && best_lowpc < best_highpc)
3537 /* Store the contiguous range if it is not empty; it can be empty for
3538 CUs with no code. */
3539 addrmap_set_empty (objfile->psymtabs_addrmap,
3540 best_lowpc + baseaddr,
3541 best_highpc + baseaddr - 1, pst);
3542
3543 /* Check if comp unit has_children.
3544 If so, read the rest of the partial symbols from this comp unit.
3545 If not, there's no more debug_info for this comp unit. */
3546 if (has_children)
3547 {
3548 struct partial_die_info *first_die;
3549 CORE_ADDR lowpc, highpc;
3550
3551 lowpc = ((CORE_ADDR) -1);
3552 highpc = ((CORE_ADDR) 0);
3553
3554 first_die = load_partial_dies (abfd, buffer, info_ptr, 1, &cu);
3555
3556 scan_partial_symbols (first_die, &lowpc, &highpc,
3557 ! has_pc_info, &cu);
3558
3559 /* If we didn't find a lowpc, set it to highpc to avoid
3560 complaints from `maint check'. */
3561 if (lowpc == ((CORE_ADDR) -1))
3562 lowpc = highpc;
3563
3564 /* If the compilation unit didn't have an explicit address range,
3565 then use the information extracted from its child dies. */
3566 if (! has_pc_info)
3567 {
3568 best_lowpc = lowpc;
3569 best_highpc = highpc;
3570 }
3571 }
3572 pst->textlow = best_lowpc + baseaddr;
3573 pst->texthigh = best_highpc + baseaddr;
3574
3575 pst->n_global_syms = objfile->global_psymbols.next -
3576 (objfile->global_psymbols.list + pst->globals_offset);
3577 pst->n_static_syms = objfile->static_psymbols.next -
3578 (objfile->static_psymbols.list + pst->statics_offset);
3579 sort_pst_symbols (pst);
3580
3581 if (is_debug_types_section)
3582 {
3583 /* It's not clear we want to do anything with stmt lists here.
3584 Waiting to see what gcc ultimately does. */
3585 }
3586 else
3587 {
3588 /* Get the list of files included in the current compilation unit,
3589 and build a psymtab for each of them. */
3590 dwarf2_build_include_psymtabs (&cu, comp_unit_die, pst);
3591 }
3592
3593 do_cleanups (back_to_inner);
3594 }
3595
3596 /* Traversal function for htab_traverse_noresize.
3597 Process one .debug_types comp-unit. */
3598
3599 static int
3600 process_type_comp_unit (void **slot, void *info)
3601 {
3602 struct signatured_type *entry = (struct signatured_type *) *slot;
3603 struct dwarf2_per_cu_data *this_cu;
3604
3605 gdb_assert (info == NULL);
3606 this_cu = &entry->per_cu;
3607
3608 gdb_assert (this_cu->debug_types_section->readin);
3609 process_psymtab_comp_unit (this_cu, this_cu->debug_types_section, 1);
3610
3611 return 1;
3612 }
3613
3614 /* Subroutine of dwarf2_build_psymtabs_hard to simplify it.
3615 Build partial symbol tables for the .debug_types comp-units. */
3616
3617 static void
3618 build_type_psymtabs (struct objfile *objfile)
3619 {
3620 if (! create_debug_types_hash_table (objfile))
3621 return;
3622
3623 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
3624 process_type_comp_unit, NULL);
3625 }
3626
3627 /* A cleanup function that clears objfile's psymtabs_addrmap field. */
3628
3629 static void
3630 psymtabs_addrmap_cleanup (void *o)
3631 {
3632 struct objfile *objfile = o;
3633
3634 objfile->psymtabs_addrmap = NULL;
3635 }
3636
3637 /* Build the partial symbol table by doing a quick pass through the
3638 .debug_info and .debug_abbrev sections. */
3639
3640 static void
3641 dwarf2_build_psymtabs_hard (struct objfile *objfile)
3642 {
3643 struct cleanup *back_to, *addrmap_cleanup;
3644 struct obstack temp_obstack;
3645 int i;
3646
3647 dwarf2_per_objfile->reading_partial_symbols = 1;
3648
3649 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3650
3651 /* Any cached compilation units will be linked by the per-objfile
3652 read_in_chain. Make sure to free them when we're done. */
3653 back_to = make_cleanup (free_cached_comp_units, NULL);
3654
3655 build_type_psymtabs (objfile);
3656
3657 create_all_comp_units (objfile);
3658
3659 /* Create a temporary address map on a temporary obstack. We later
3660 copy this to the final obstack. */
3661 obstack_init (&temp_obstack);
3662 make_cleanup_obstack_free (&temp_obstack);
3663 objfile->psymtabs_addrmap = addrmap_create_mutable (&temp_obstack);
3664 addrmap_cleanup = make_cleanup (psymtabs_addrmap_cleanup, objfile);
3665
3666 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
3667 {
3668 struct dwarf2_per_cu_data *per_cu = dw2_get_cu (i);
3669
3670 process_psymtab_comp_unit (per_cu, &dwarf2_per_objfile->info, 0);
3671 }
3672
3673 objfile->psymtabs_addrmap = addrmap_create_fixed (objfile->psymtabs_addrmap,
3674 &objfile->objfile_obstack);
3675 discard_cleanups (addrmap_cleanup);
3676
3677 do_cleanups (back_to);
3678 }
3679
3680 /* Load the partial DIEs for a secondary CU into memory. */
3681
3682 static void
3683 load_partial_comp_unit (struct dwarf2_per_cu_data *this_cu)
3684 {
3685 struct objfile *objfile = this_cu->objfile;
3686 bfd *abfd = objfile->obfd;
3687 gdb_byte *info_ptr;
3688 struct die_info *comp_unit_die;
3689 struct dwarf2_cu *cu;
3690 struct cleanup *free_abbrevs_cleanup, *free_cu_cleanup = NULL;
3691 int has_children;
3692 struct die_reader_specs reader_specs;
3693 int read_cu = 0;
3694 struct dwarf2_section_info *section = &dwarf2_per_objfile->info;
3695
3696 gdb_assert (! this_cu->debug_types_section);
3697
3698 gdb_assert (section->readin);
3699 info_ptr = section->buffer + this_cu->offset;
3700
3701 if (this_cu->cu == NULL)
3702 {
3703 cu = xmalloc (sizeof (*cu));
3704 init_one_comp_unit (cu, this_cu);
3705
3706 read_cu = 1;
3707
3708 /* If an error occurs while loading, release our storage. */
3709 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
3710
3711 info_ptr = read_and_check_comp_unit_head (&cu->header, section, info_ptr,
3712 0);
3713
3714 /* Skip dummy compilation units. */
3715 if (info_ptr >= (section->buffer + section->size)
3716 || peek_abbrev_code (abfd, info_ptr) == 0)
3717 {
3718 do_cleanups (free_cu_cleanup);
3719 return;
3720 }
3721 }
3722 else
3723 {
3724 cu = this_cu->cu;
3725 info_ptr += cu->header.first_die_offset;
3726 }
3727
3728 /* Read the abbrevs for this compilation unit into a table. */
3729 gdb_assert (cu->dwarf2_abbrevs == NULL);
3730 dwarf2_read_abbrevs (cu);
3731 free_abbrevs_cleanup = make_cleanup (dwarf2_free_abbrev_table, cu);
3732
3733 /* Read the compilation unit die. */
3734 init_cu_die_reader (&reader_specs, cu);
3735 info_ptr = read_full_die (&reader_specs, &comp_unit_die, info_ptr,
3736 &has_children);
3737
3738 prepare_one_comp_unit (cu, comp_unit_die);
3739
3740 /* Check if comp unit has_children.
3741 If so, read the rest of the partial symbols from this comp unit.
3742 If not, there's no more debug_info for this comp unit. */
3743 if (has_children)
3744 load_partial_dies (abfd, section->buffer, info_ptr, 0, cu);
3745
3746 do_cleanups (free_abbrevs_cleanup);
3747
3748 if (read_cu)
3749 {
3750 /* We've successfully allocated this compilation unit. Let our
3751 caller clean it up when finished with it. */
3752 discard_cleanups (free_cu_cleanup);
3753
3754 /* Link this CU into read_in_chain. */
3755 this_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
3756 dwarf2_per_objfile->read_in_chain = this_cu;
3757 }
3758 }
3759
3760 /* Create a list of all compilation units in OBJFILE.
3761 This is only done for -readnow and building partial symtabs. */
3762
3763 static void
3764 create_all_comp_units (struct objfile *objfile)
3765 {
3766 int n_allocated;
3767 int n_comp_units;
3768 struct dwarf2_per_cu_data **all_comp_units;
3769 gdb_byte *info_ptr;
3770
3771 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
3772 info_ptr = dwarf2_per_objfile->info.buffer;
3773
3774 n_comp_units = 0;
3775 n_allocated = 10;
3776 all_comp_units = xmalloc (n_allocated
3777 * sizeof (struct dwarf2_per_cu_data *));
3778
3779 while (info_ptr < dwarf2_per_objfile->info.buffer
3780 + dwarf2_per_objfile->info.size)
3781 {
3782 unsigned int length, initial_length_size;
3783 struct dwarf2_per_cu_data *this_cu;
3784 unsigned int offset;
3785
3786 offset = info_ptr - dwarf2_per_objfile->info.buffer;
3787
3788 /* Read just enough information to find out where the next
3789 compilation unit is. */
3790 length = read_initial_length (objfile->obfd, info_ptr,
3791 &initial_length_size);
3792
3793 /* Save the compilation unit for later lookup. */
3794 this_cu = obstack_alloc (&objfile->objfile_obstack,
3795 sizeof (struct dwarf2_per_cu_data));
3796 memset (this_cu, 0, sizeof (*this_cu));
3797 this_cu->offset = offset;
3798 this_cu->length = length + initial_length_size;
3799 this_cu->objfile = objfile;
3800
3801 if (n_comp_units == n_allocated)
3802 {
3803 n_allocated *= 2;
3804 all_comp_units = xrealloc (all_comp_units,
3805 n_allocated
3806 * sizeof (struct dwarf2_per_cu_data *));
3807 }
3808 all_comp_units[n_comp_units++] = this_cu;
3809
3810 info_ptr = info_ptr + this_cu->length;
3811 }
3812
3813 dwarf2_per_objfile->all_comp_units
3814 = obstack_alloc (&objfile->objfile_obstack,
3815 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3816 memcpy (dwarf2_per_objfile->all_comp_units, all_comp_units,
3817 n_comp_units * sizeof (struct dwarf2_per_cu_data *));
3818 xfree (all_comp_units);
3819 dwarf2_per_objfile->n_comp_units = n_comp_units;
3820 }
3821
3822 /* Process all loaded DIEs for compilation unit CU, starting at
3823 FIRST_DIE. The caller should pass NEED_PC == 1 if the compilation
3824 unit DIE did not have PC info (DW_AT_low_pc and DW_AT_high_pc, or
3825 DW_AT_ranges). If NEED_PC is set, then this function will set
3826 *LOWPC and *HIGHPC to the lowest and highest PC values found in CU
3827 and record the covered ranges in the addrmap. */
3828
3829 static void
3830 scan_partial_symbols (struct partial_die_info *first_die, CORE_ADDR *lowpc,
3831 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
3832 {
3833 struct partial_die_info *pdi;
3834
3835 /* Now, march along the PDI's, descending into ones which have
3836 interesting children but skipping the children of the other ones,
3837 until we reach the end of the compilation unit. */
3838
3839 pdi = first_die;
3840
3841 while (pdi != NULL)
3842 {
3843 fixup_partial_die (pdi, cu);
3844
3845 /* Anonymous namespaces or modules have no name but have interesting
3846 children, so we need to look at them. Ditto for anonymous
3847 enums. */
3848
3849 if (pdi->name != NULL || pdi->tag == DW_TAG_namespace
3850 || pdi->tag == DW_TAG_module || pdi->tag == DW_TAG_enumeration_type)
3851 {
3852 switch (pdi->tag)
3853 {
3854 case DW_TAG_subprogram:
3855 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
3856 break;
3857 case DW_TAG_constant:
3858 case DW_TAG_variable:
3859 case DW_TAG_typedef:
3860 case DW_TAG_union_type:
3861 if (!pdi->is_declaration)
3862 {
3863 add_partial_symbol (pdi, cu);
3864 }
3865 break;
3866 case DW_TAG_class_type:
3867 case DW_TAG_interface_type:
3868 case DW_TAG_structure_type:
3869 if (!pdi->is_declaration)
3870 {
3871 add_partial_symbol (pdi, cu);
3872 }
3873 break;
3874 case DW_TAG_enumeration_type:
3875 if (!pdi->is_declaration)
3876 add_partial_enumeration (pdi, cu);
3877 break;
3878 case DW_TAG_base_type:
3879 case DW_TAG_subrange_type:
3880 /* File scope base type definitions are added to the partial
3881 symbol table. */
3882 add_partial_symbol (pdi, cu);
3883 break;
3884 case DW_TAG_namespace:
3885 add_partial_namespace (pdi, lowpc, highpc, need_pc, cu);
3886 break;
3887 case DW_TAG_module:
3888 add_partial_module (pdi, lowpc, highpc, need_pc, cu);
3889 break;
3890 default:
3891 break;
3892 }
3893 }
3894
3895 /* If the die has a sibling, skip to the sibling. */
3896
3897 pdi = pdi->die_sibling;
3898 }
3899 }
3900
3901 /* Functions used to compute the fully scoped name of a partial DIE.
3902
3903 Normally, this is simple. For C++, the parent DIE's fully scoped
3904 name is concatenated with "::" and the partial DIE's name. For
3905 Java, the same thing occurs except that "." is used instead of "::".
3906 Enumerators are an exception; they use the scope of their parent
3907 enumeration type, i.e. the name of the enumeration type is not
3908 prepended to the enumerator.
3909
3910 There are two complexities. One is DW_AT_specification; in this
3911 case "parent" means the parent of the target of the specification,
3912 instead of the direct parent of the DIE. The other is compilers
3913 which do not emit DW_TAG_namespace; in this case we try to guess
3914 the fully qualified name of structure types from their members'
3915 linkage names. This must be done using the DIE's children rather
3916 than the children of any DW_AT_specification target. We only need
3917 to do this for structures at the top level, i.e. if the target of
3918 any DW_AT_specification (if any; otherwise the DIE itself) does not
3919 have a parent. */
3920
3921 /* Compute the scope prefix associated with PDI's parent, in
3922 compilation unit CU. The result will be allocated on CU's
3923 comp_unit_obstack, or a copy of the already allocated PDI->NAME
3924 field. NULL is returned if no prefix is necessary. */
3925 static char *
3926 partial_die_parent_scope (struct partial_die_info *pdi,
3927 struct dwarf2_cu *cu)
3928 {
3929 char *grandparent_scope;
3930 struct partial_die_info *parent, *real_pdi;
3931
3932 /* We need to look at our parent DIE; if we have a DW_AT_specification,
3933 then this means the parent of the specification DIE. */
3934
3935 real_pdi = pdi;
3936 while (real_pdi->has_specification)
3937 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
3938
3939 parent = real_pdi->die_parent;
3940 if (parent == NULL)
3941 return NULL;
3942
3943 if (parent->scope_set)
3944 return parent->scope;
3945
3946 fixup_partial_die (parent, cu);
3947
3948 grandparent_scope = partial_die_parent_scope (parent, cu);
3949
3950 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
3951 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
3952 Work around this problem here. */
3953 if (cu->language == language_cplus
3954 && parent->tag == DW_TAG_namespace
3955 && strcmp (parent->name, "::") == 0
3956 && grandparent_scope == NULL)
3957 {
3958 parent->scope = NULL;
3959 parent->scope_set = 1;
3960 return NULL;
3961 }
3962
3963 if (pdi->tag == DW_TAG_enumerator)
3964 /* Enumerators should not get the name of the enumeration as a prefix. */
3965 parent->scope = grandparent_scope;
3966 else if (parent->tag == DW_TAG_namespace
3967 || parent->tag == DW_TAG_module
3968 || parent->tag == DW_TAG_structure_type
3969 || parent->tag == DW_TAG_class_type
3970 || parent->tag == DW_TAG_interface_type
3971 || parent->tag == DW_TAG_union_type
3972 || parent->tag == DW_TAG_enumeration_type)
3973 {
3974 if (grandparent_scope == NULL)
3975 parent->scope = parent->name;
3976 else
3977 parent->scope = typename_concat (&cu->comp_unit_obstack,
3978 grandparent_scope,
3979 parent->name, 0, cu);
3980 }
3981 else
3982 {
3983 /* FIXME drow/2004-04-01: What should we be doing with
3984 function-local names? For partial symbols, we should probably be
3985 ignoring them. */
3986 complaint (&symfile_complaints,
3987 _("unhandled containing DIE tag %d for DIE at %d"),
3988 parent->tag, pdi->offset);
3989 parent->scope = grandparent_scope;
3990 }
3991
3992 parent->scope_set = 1;
3993 return parent->scope;
3994 }
3995
3996 /* Return the fully scoped name associated with PDI, from compilation unit
3997 CU. The result will be allocated with malloc. */
3998 static char *
3999 partial_die_full_name (struct partial_die_info *pdi,
4000 struct dwarf2_cu *cu)
4001 {
4002 char *parent_scope;
4003
4004 /* If this is a template instantiation, we can not work out the
4005 template arguments from partial DIEs. So, unfortunately, we have
4006 to go through the full DIEs. At least any work we do building
4007 types here will be reused if full symbols are loaded later. */
4008 if (pdi->has_template_arguments)
4009 {
4010 fixup_partial_die (pdi, cu);
4011
4012 if (pdi->name != NULL && strchr (pdi->name, '<') == NULL)
4013 {
4014 struct die_info *die;
4015 struct attribute attr;
4016 struct dwarf2_cu *ref_cu = cu;
4017
4018 attr.name = 0;
4019 attr.form = DW_FORM_ref_addr;
4020 attr.u.addr = pdi->offset;
4021 die = follow_die_ref (NULL, &attr, &ref_cu);
4022
4023 return xstrdup (dwarf2_full_name (NULL, die, ref_cu));
4024 }
4025 }
4026
4027 parent_scope = partial_die_parent_scope (pdi, cu);
4028 if (parent_scope == NULL)
4029 return NULL;
4030 else
4031 return typename_concat (NULL, parent_scope, pdi->name, 0, cu);
4032 }
4033
4034 static void
4035 add_partial_symbol (struct partial_die_info *pdi, struct dwarf2_cu *cu)
4036 {
4037 struct objfile *objfile = cu->objfile;
4038 CORE_ADDR addr = 0;
4039 char *actual_name = NULL;
4040 CORE_ADDR baseaddr;
4041 int built_actual_name = 0;
4042
4043 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4044
4045 actual_name = partial_die_full_name (pdi, cu);
4046 if (actual_name)
4047 built_actual_name = 1;
4048
4049 if (actual_name == NULL)
4050 actual_name = pdi->name;
4051
4052 switch (pdi->tag)
4053 {
4054 case DW_TAG_subprogram:
4055 if (pdi->is_external || cu->language == language_ada)
4056 {
4057 /* brobecker/2007-12-26: Normally, only "external" DIEs are part
4058 of the global scope. But in Ada, we want to be able to access
4059 nested procedures globally. So all Ada subprograms are stored
4060 in the global scope. */
4061 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4062 mst_text, objfile); */
4063 add_psymbol_to_list (actual_name, strlen (actual_name),
4064 built_actual_name,
4065 VAR_DOMAIN, LOC_BLOCK,
4066 &objfile->global_psymbols,
4067 0, pdi->lowpc + baseaddr,
4068 cu->language, objfile);
4069 }
4070 else
4071 {
4072 /* prim_record_minimal_symbol (actual_name, pdi->lowpc + baseaddr,
4073 mst_file_text, objfile); */
4074 add_psymbol_to_list (actual_name, strlen (actual_name),
4075 built_actual_name,
4076 VAR_DOMAIN, LOC_BLOCK,
4077 &objfile->static_psymbols,
4078 0, pdi->lowpc + baseaddr,
4079 cu->language, objfile);
4080 }
4081 break;
4082 case DW_TAG_constant:
4083 {
4084 struct psymbol_allocation_list *list;
4085
4086 if (pdi->is_external)
4087 list = &objfile->global_psymbols;
4088 else
4089 list = &objfile->static_psymbols;
4090 add_psymbol_to_list (actual_name, strlen (actual_name),
4091 built_actual_name, VAR_DOMAIN, LOC_STATIC,
4092 list, 0, 0, cu->language, objfile);
4093 }
4094 break;
4095 case DW_TAG_variable:
4096 if (pdi->locdesc)
4097 addr = decode_locdesc (pdi->locdesc, cu);
4098
4099 if (pdi->locdesc
4100 && addr == 0
4101 && !dwarf2_per_objfile->has_section_at_zero)
4102 {
4103 /* A global or static variable may also have been stripped
4104 out by the linker if unused, in which case its address
4105 will be nullified; do not add such variables into partial
4106 symbol table then. */
4107 }
4108 else if (pdi->is_external)
4109 {
4110 /* Global Variable.
4111 Don't enter into the minimal symbol tables as there is
4112 a minimal symbol table entry from the ELF symbols already.
4113 Enter into partial symbol table if it has a location
4114 descriptor or a type.
4115 If the location descriptor is missing, new_symbol will create
4116 a LOC_UNRESOLVED symbol, the address of the variable will then
4117 be determined from the minimal symbol table whenever the variable
4118 is referenced.
4119 The address for the partial symbol table entry is not
4120 used by GDB, but it comes in handy for debugging partial symbol
4121 table building. */
4122
4123 if (pdi->locdesc || pdi->has_type)
4124 add_psymbol_to_list (actual_name, strlen (actual_name),
4125 built_actual_name,
4126 VAR_DOMAIN, LOC_STATIC,
4127 &objfile->global_psymbols,
4128 0, addr + baseaddr,
4129 cu->language, objfile);
4130 }
4131 else
4132 {
4133 /* Static Variable. Skip symbols without location descriptors. */
4134 if (pdi->locdesc == NULL)
4135 {
4136 if (built_actual_name)
4137 xfree (actual_name);
4138 return;
4139 }
4140 /* prim_record_minimal_symbol (actual_name, addr + baseaddr,
4141 mst_file_data, objfile); */
4142 add_psymbol_to_list (actual_name, strlen (actual_name),
4143 built_actual_name,
4144 VAR_DOMAIN, LOC_STATIC,
4145 &objfile->static_psymbols,
4146 0, addr + baseaddr,
4147 cu->language, objfile);
4148 }
4149 break;
4150 case DW_TAG_typedef:
4151 case DW_TAG_base_type:
4152 case DW_TAG_subrange_type:
4153 add_psymbol_to_list (actual_name, strlen (actual_name),
4154 built_actual_name,
4155 VAR_DOMAIN, LOC_TYPEDEF,
4156 &objfile->static_psymbols,
4157 0, (CORE_ADDR) 0, cu->language, objfile);
4158 break;
4159 case DW_TAG_namespace:
4160 add_psymbol_to_list (actual_name, strlen (actual_name),
4161 built_actual_name,
4162 VAR_DOMAIN, LOC_TYPEDEF,
4163 &objfile->global_psymbols,
4164 0, (CORE_ADDR) 0, cu->language, objfile);
4165 break;
4166 case DW_TAG_class_type:
4167 case DW_TAG_interface_type:
4168 case DW_TAG_structure_type:
4169 case DW_TAG_union_type:
4170 case DW_TAG_enumeration_type:
4171 /* Skip external references. The DWARF standard says in the section
4172 about "Structure, Union, and Class Type Entries": "An incomplete
4173 structure, union or class type is represented by a structure,
4174 union or class entry that does not have a byte size attribute
4175 and that has a DW_AT_declaration attribute." */
4176 if (!pdi->has_byte_size && pdi->is_declaration)
4177 {
4178 if (built_actual_name)
4179 xfree (actual_name);
4180 return;
4181 }
4182
4183 /* NOTE: carlton/2003-10-07: See comment in new_symbol about
4184 static vs. global. */
4185 add_psymbol_to_list (actual_name, strlen (actual_name),
4186 built_actual_name,
4187 STRUCT_DOMAIN, LOC_TYPEDEF,
4188 (cu->language == language_cplus
4189 || cu->language == language_java)
4190 ? &objfile->global_psymbols
4191 : &objfile->static_psymbols,
4192 0, (CORE_ADDR) 0, cu->language, objfile);
4193
4194 break;
4195 case DW_TAG_enumerator:
4196 add_psymbol_to_list (actual_name, strlen (actual_name),
4197 built_actual_name,
4198 VAR_DOMAIN, LOC_CONST,
4199 (cu->language == language_cplus
4200 || cu->language == language_java)
4201 ? &objfile->global_psymbols
4202 : &objfile->static_psymbols,
4203 0, (CORE_ADDR) 0, cu->language, objfile);
4204 break;
4205 default:
4206 break;
4207 }
4208
4209 if (built_actual_name)
4210 xfree (actual_name);
4211 }
4212
4213 /* Read a partial die corresponding to a namespace; also, add a symbol
4214 corresponding to that namespace to the symbol table. NAMESPACE is
4215 the name of the enclosing namespace. */
4216
4217 static void
4218 add_partial_namespace (struct partial_die_info *pdi,
4219 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4220 int need_pc, struct dwarf2_cu *cu)
4221 {
4222 /* Add a symbol for the namespace. */
4223
4224 add_partial_symbol (pdi, cu);
4225
4226 /* Now scan partial symbols in that namespace. */
4227
4228 if (pdi->has_children)
4229 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4230 }
4231
4232 /* Read a partial die corresponding to a Fortran module. */
4233
4234 static void
4235 add_partial_module (struct partial_die_info *pdi, CORE_ADDR *lowpc,
4236 CORE_ADDR *highpc, int need_pc, struct dwarf2_cu *cu)
4237 {
4238 /* Now scan partial symbols in that module. */
4239
4240 if (pdi->has_children)
4241 scan_partial_symbols (pdi->die_child, lowpc, highpc, need_pc, cu);
4242 }
4243
4244 /* Read a partial die corresponding to a subprogram and create a partial
4245 symbol for that subprogram. When the CU language allows it, this
4246 routine also defines a partial symbol for each nested subprogram
4247 that this subprogram contains.
4248
4249 DIE my also be a lexical block, in which case we simply search
4250 recursively for suprograms defined inside that lexical block.
4251 Again, this is only performed when the CU language allows this
4252 type of definitions. */
4253
4254 static void
4255 add_partial_subprogram (struct partial_die_info *pdi,
4256 CORE_ADDR *lowpc, CORE_ADDR *highpc,
4257 int need_pc, struct dwarf2_cu *cu)
4258 {
4259 if (pdi->tag == DW_TAG_subprogram)
4260 {
4261 if (pdi->has_pc_info)
4262 {
4263 if (pdi->lowpc < *lowpc)
4264 *lowpc = pdi->lowpc;
4265 if (pdi->highpc > *highpc)
4266 *highpc = pdi->highpc;
4267 if (need_pc)
4268 {
4269 CORE_ADDR baseaddr;
4270 struct objfile *objfile = cu->objfile;
4271
4272 baseaddr = ANOFFSET (objfile->section_offsets,
4273 SECT_OFF_TEXT (objfile));
4274 addrmap_set_empty (objfile->psymtabs_addrmap,
4275 pdi->lowpc + baseaddr,
4276 pdi->highpc - 1 + baseaddr,
4277 cu->per_cu->v.psymtab);
4278 }
4279 }
4280
4281 if (pdi->has_pc_info || (!pdi->is_external && pdi->may_be_inlined))
4282 {
4283 if (!pdi->is_declaration)
4284 /* Ignore subprogram DIEs that do not have a name, they are
4285 illegal. Do not emit a complaint at this point, we will
4286 do so when we convert this psymtab into a symtab. */
4287 if (pdi->name)
4288 add_partial_symbol (pdi, cu);
4289 }
4290 }
4291
4292 if (! pdi->has_children)
4293 return;
4294
4295 if (cu->language == language_ada)
4296 {
4297 pdi = pdi->die_child;
4298 while (pdi != NULL)
4299 {
4300 fixup_partial_die (pdi, cu);
4301 if (pdi->tag == DW_TAG_subprogram
4302 || pdi->tag == DW_TAG_lexical_block)
4303 add_partial_subprogram (pdi, lowpc, highpc, need_pc, cu);
4304 pdi = pdi->die_sibling;
4305 }
4306 }
4307 }
4308
4309 /* Read a partial die corresponding to an enumeration type. */
4310
4311 static void
4312 add_partial_enumeration (struct partial_die_info *enum_pdi,
4313 struct dwarf2_cu *cu)
4314 {
4315 struct partial_die_info *pdi;
4316
4317 if (enum_pdi->name != NULL)
4318 add_partial_symbol (enum_pdi, cu);
4319
4320 pdi = enum_pdi->die_child;
4321 while (pdi)
4322 {
4323 if (pdi->tag != DW_TAG_enumerator || pdi->name == NULL)
4324 complaint (&symfile_complaints, _("malformed enumerator DIE ignored"));
4325 else
4326 add_partial_symbol (pdi, cu);
4327 pdi = pdi->die_sibling;
4328 }
4329 }
4330
4331 /* Return the initial uleb128 in the die at INFO_PTR. */
4332
4333 static unsigned int
4334 peek_abbrev_code (bfd *abfd, gdb_byte *info_ptr)
4335 {
4336 unsigned int bytes_read;
4337
4338 return read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4339 }
4340
4341 /* Read the initial uleb128 in the die at INFO_PTR in compilation unit CU.
4342 Return the corresponding abbrev, or NULL if the number is zero (indicating
4343 an empty DIE). In either case *BYTES_READ will be set to the length of
4344 the initial number. */
4345
4346 static struct abbrev_info *
4347 peek_die_abbrev (gdb_byte *info_ptr, unsigned int *bytes_read,
4348 struct dwarf2_cu *cu)
4349 {
4350 bfd *abfd = cu->objfile->obfd;
4351 unsigned int abbrev_number;
4352 struct abbrev_info *abbrev;
4353
4354 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, bytes_read);
4355
4356 if (abbrev_number == 0)
4357 return NULL;
4358
4359 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
4360 if (!abbrev)
4361 {
4362 error (_("Dwarf Error: Could not find abbrev number %d [in module %s]"),
4363 abbrev_number, bfd_get_filename (abfd));
4364 }
4365
4366 return abbrev;
4367 }
4368
4369 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4370 Returns a pointer to the end of a series of DIEs, terminated by an empty
4371 DIE. Any children of the skipped DIEs will also be skipped. */
4372
4373 static gdb_byte *
4374 skip_children (gdb_byte *buffer, gdb_byte *info_ptr, struct dwarf2_cu *cu)
4375 {
4376 struct abbrev_info *abbrev;
4377 unsigned int bytes_read;
4378
4379 while (1)
4380 {
4381 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
4382 if (abbrev == NULL)
4383 return info_ptr + bytes_read;
4384 else
4385 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
4386 }
4387 }
4388
4389 /* Scan the debug information for CU starting at INFO_PTR in buffer BUFFER.
4390 INFO_PTR should point just after the initial uleb128 of a DIE, and the
4391 abbrev corresponding to that skipped uleb128 should be passed in
4392 ABBREV. Returns a pointer to this DIE's sibling, skipping any
4393 children. */
4394
4395 static gdb_byte *
4396 skip_one_die (gdb_byte *buffer, gdb_byte *info_ptr,
4397 struct abbrev_info *abbrev, struct dwarf2_cu *cu)
4398 {
4399 unsigned int bytes_read;
4400 struct attribute attr;
4401 bfd *abfd = cu->objfile->obfd;
4402 unsigned int form, i;
4403
4404 for (i = 0; i < abbrev->num_attrs; i++)
4405 {
4406 /* The only abbrev we care about is DW_AT_sibling. */
4407 if (abbrev->attrs[i].name == DW_AT_sibling)
4408 {
4409 read_attribute (&attr, &abbrev->attrs[i],
4410 abfd, info_ptr, cu);
4411 if (attr.form == DW_FORM_ref_addr)
4412 complaint (&symfile_complaints,
4413 _("ignoring absolute DW_AT_sibling"));
4414 else
4415 return buffer + dwarf2_get_ref_die_offset (&attr);
4416 }
4417
4418 /* If it isn't DW_AT_sibling, skip this attribute. */
4419 form = abbrev->attrs[i].form;
4420 skip_attribute:
4421 switch (form)
4422 {
4423 case DW_FORM_ref_addr:
4424 /* In DWARF 2, DW_FORM_ref_addr is address sized; in DWARF 3
4425 and later it is offset sized. */
4426 if (cu->header.version == 2)
4427 info_ptr += cu->header.addr_size;
4428 else
4429 info_ptr += cu->header.offset_size;
4430 break;
4431 case DW_FORM_addr:
4432 info_ptr += cu->header.addr_size;
4433 break;
4434 case DW_FORM_data1:
4435 case DW_FORM_ref1:
4436 case DW_FORM_flag:
4437 info_ptr += 1;
4438 break;
4439 case DW_FORM_flag_present:
4440 break;
4441 case DW_FORM_data2:
4442 case DW_FORM_ref2:
4443 info_ptr += 2;
4444 break;
4445 case DW_FORM_data4:
4446 case DW_FORM_ref4:
4447 info_ptr += 4;
4448 break;
4449 case DW_FORM_data8:
4450 case DW_FORM_ref8:
4451 case DW_FORM_ref_sig8:
4452 info_ptr += 8;
4453 break;
4454 case DW_FORM_string:
4455 read_direct_string (abfd, info_ptr, &bytes_read);
4456 info_ptr += bytes_read;
4457 break;
4458 case DW_FORM_sec_offset:
4459 case DW_FORM_strp:
4460 info_ptr += cu->header.offset_size;
4461 break;
4462 case DW_FORM_exprloc:
4463 case DW_FORM_block:
4464 info_ptr += read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4465 info_ptr += bytes_read;
4466 break;
4467 case DW_FORM_block1:
4468 info_ptr += 1 + read_1_byte (abfd, info_ptr);
4469 break;
4470 case DW_FORM_block2:
4471 info_ptr += 2 + read_2_bytes (abfd, info_ptr);
4472 break;
4473 case DW_FORM_block4:
4474 info_ptr += 4 + read_4_bytes (abfd, info_ptr);
4475 break;
4476 case DW_FORM_sdata:
4477 case DW_FORM_udata:
4478 case DW_FORM_ref_udata:
4479 info_ptr = skip_leb128 (abfd, info_ptr);
4480 break;
4481 case DW_FORM_indirect:
4482 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
4483 info_ptr += bytes_read;
4484 /* We need to continue parsing from here, so just go back to
4485 the top. */
4486 goto skip_attribute;
4487
4488 default:
4489 error (_("Dwarf Error: Cannot handle %s "
4490 "in DWARF reader [in module %s]"),
4491 dwarf_form_name (form),
4492 bfd_get_filename (abfd));
4493 }
4494 }
4495
4496 if (abbrev->has_children)
4497 return skip_children (buffer, info_ptr, cu);
4498 else
4499 return info_ptr;
4500 }
4501
4502 /* Locate ORIG_PDI's sibling.
4503 INFO_PTR should point to the start of the next DIE after ORIG_PDI
4504 in BUFFER. */
4505
4506 static gdb_byte *
4507 locate_pdi_sibling (struct partial_die_info *orig_pdi,
4508 gdb_byte *buffer, gdb_byte *info_ptr,
4509 bfd *abfd, struct dwarf2_cu *cu)
4510 {
4511 /* Do we know the sibling already? */
4512
4513 if (orig_pdi->sibling)
4514 return orig_pdi->sibling;
4515
4516 /* Are there any children to deal with? */
4517
4518 if (!orig_pdi->has_children)
4519 return info_ptr;
4520
4521 /* Skip the children the long way. */
4522
4523 return skip_children (buffer, info_ptr, cu);
4524 }
4525
4526 /* Expand this partial symbol table into a full symbol table. */
4527
4528 static void
4529 dwarf2_psymtab_to_symtab (struct partial_symtab *pst)
4530 {
4531 if (pst != NULL)
4532 {
4533 if (pst->readin)
4534 {
4535 warning (_("bug: psymtab for %s is already read in."),
4536 pst->filename);
4537 }
4538 else
4539 {
4540 if (info_verbose)
4541 {
4542 printf_filtered (_("Reading in symbols for %s..."),
4543 pst->filename);
4544 gdb_flush (gdb_stdout);
4545 }
4546
4547 /* Restore our global data. */
4548 dwarf2_per_objfile = objfile_data (pst->objfile,
4549 dwarf2_objfile_data_key);
4550
4551 /* If this psymtab is constructed from a debug-only objfile, the
4552 has_section_at_zero flag will not necessarily be correct. We
4553 can get the correct value for this flag by looking at the data
4554 associated with the (presumably stripped) associated objfile. */
4555 if (pst->objfile->separate_debug_objfile_backlink)
4556 {
4557 struct dwarf2_per_objfile *dpo_backlink
4558 = objfile_data (pst->objfile->separate_debug_objfile_backlink,
4559 dwarf2_objfile_data_key);
4560
4561 dwarf2_per_objfile->has_section_at_zero
4562 = dpo_backlink->has_section_at_zero;
4563 }
4564
4565 dwarf2_per_objfile->reading_partial_symbols = 0;
4566
4567 psymtab_to_symtab_1 (pst);
4568
4569 /* Finish up the debug error message. */
4570 if (info_verbose)
4571 printf_filtered (_("done.\n"));
4572 }
4573 }
4574 }
4575 \f
4576 /* Reading in full CUs. */
4577
4578 /* Add PER_CU to the queue. */
4579
4580 static void
4581 queue_comp_unit (struct dwarf2_per_cu_data *per_cu)
4582 {
4583 struct dwarf2_queue_item *item;
4584
4585 per_cu->queued = 1;
4586 item = xmalloc (sizeof (*item));
4587 item->per_cu = per_cu;
4588 item->next = NULL;
4589
4590 if (dwarf2_queue == NULL)
4591 dwarf2_queue = item;
4592 else
4593 dwarf2_queue_tail->next = item;
4594
4595 dwarf2_queue_tail = item;
4596 }
4597
4598 /* Process the queue. */
4599
4600 static void
4601 process_queue (void)
4602 {
4603 struct dwarf2_queue_item *item, *next_item;
4604
4605 /* The queue starts out with one item, but following a DIE reference
4606 may load a new CU, adding it to the end of the queue. */
4607 for (item = dwarf2_queue; item != NULL; dwarf2_queue = item = next_item)
4608 {
4609 if (dwarf2_per_objfile->using_index
4610 ? !item->per_cu->v.quick->symtab
4611 : (item->per_cu->v.psymtab && !item->per_cu->v.psymtab->readin))
4612 process_full_comp_unit (item->per_cu);
4613
4614 item->per_cu->queued = 0;
4615 next_item = item->next;
4616 xfree (item);
4617 }
4618
4619 dwarf2_queue_tail = NULL;
4620 }
4621
4622 /* Free all allocated queue entries. This function only releases anything if
4623 an error was thrown; if the queue was processed then it would have been
4624 freed as we went along. */
4625
4626 static void
4627 dwarf2_release_queue (void *dummy)
4628 {
4629 struct dwarf2_queue_item *item, *last;
4630
4631 item = dwarf2_queue;
4632 while (item)
4633 {
4634 /* Anything still marked queued is likely to be in an
4635 inconsistent state, so discard it. */
4636 if (item->per_cu->queued)
4637 {
4638 if (item->per_cu->cu != NULL)
4639 free_one_cached_comp_unit (item->per_cu->cu);
4640 item->per_cu->queued = 0;
4641 }
4642
4643 last = item;
4644 item = item->next;
4645 xfree (last);
4646 }
4647
4648 dwarf2_queue = dwarf2_queue_tail = NULL;
4649 }
4650
4651 /* Read in full symbols for PST, and anything it depends on. */
4652
4653 static void
4654 psymtab_to_symtab_1 (struct partial_symtab *pst)
4655 {
4656 struct dwarf2_per_cu_data *per_cu;
4657 struct cleanup *back_to;
4658 int i;
4659
4660 for (i = 0; i < pst->number_of_dependencies; i++)
4661 if (!pst->dependencies[i]->readin)
4662 {
4663 /* Inform about additional files that need to be read in. */
4664 if (info_verbose)
4665 {
4666 /* FIXME: i18n: Need to make this a single string. */
4667 fputs_filtered (" ", gdb_stdout);
4668 wrap_here ("");
4669 fputs_filtered ("and ", gdb_stdout);
4670 wrap_here ("");
4671 printf_filtered ("%s...", pst->dependencies[i]->filename);
4672 wrap_here (""); /* Flush output. */
4673 gdb_flush (gdb_stdout);
4674 }
4675 psymtab_to_symtab_1 (pst->dependencies[i]);
4676 }
4677
4678 per_cu = pst->read_symtab_private;
4679
4680 if (per_cu == NULL)
4681 {
4682 /* It's an include file, no symbols to read for it.
4683 Everything is in the parent symtab. */
4684 pst->readin = 1;
4685 return;
4686 }
4687
4688 dw2_do_instantiate_symtab (per_cu);
4689 }
4690
4691 /* Load the DIEs associated with PER_CU into memory. */
4692
4693 static void
4694 load_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4695 {
4696 struct objfile *objfile = per_cu->objfile;
4697 bfd *abfd = objfile->obfd;
4698 struct dwarf2_cu *cu;
4699 unsigned int offset;
4700 gdb_byte *info_ptr, *beg_of_comp_unit;
4701 struct cleanup *free_cu_cleanup = NULL;
4702 struct attribute *attr;
4703 int read_cu = 0;
4704
4705 gdb_assert (! per_cu->debug_types_section);
4706
4707 /* Set local variables from the partial symbol table info. */
4708 offset = per_cu->offset;
4709
4710 dwarf2_read_section (objfile, &dwarf2_per_objfile->info);
4711 info_ptr = dwarf2_per_objfile->info.buffer + offset;
4712 beg_of_comp_unit = info_ptr;
4713
4714 if (per_cu->cu == NULL)
4715 {
4716 cu = xmalloc (sizeof (*cu));
4717 init_one_comp_unit (cu, per_cu);
4718
4719 read_cu = 1;
4720
4721 /* If an error occurs while loading, release our storage. */
4722 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
4723
4724 /* Read in the comp_unit header. */
4725 info_ptr = read_comp_unit_head (&cu->header, info_ptr, abfd);
4726
4727 /* Skip dummy compilation units. */
4728 if (info_ptr >= (dwarf2_per_objfile->info.buffer
4729 + dwarf2_per_objfile->info.size)
4730 || peek_abbrev_code (abfd, info_ptr) == 0)
4731 {
4732 do_cleanups (free_cu_cleanup);
4733 return;
4734 }
4735
4736 /* Complete the cu_header. */
4737 cu->header.offset = offset;
4738 cu->header.first_die_offset = info_ptr - beg_of_comp_unit;
4739 }
4740 else
4741 {
4742 cu = per_cu->cu;
4743 info_ptr += cu->header.first_die_offset;
4744 }
4745
4746 cu->dies = read_comp_unit (info_ptr, cu);
4747
4748 /* We try not to read any attributes in this function, because not
4749 all CUs needed for references have been loaded yet, and symbol
4750 table processing isn't initialized. But we have to set the CU language,
4751 or we won't be able to build types correctly. */
4752 prepare_one_comp_unit (cu, cu->dies);
4753
4754 /* Similarly, if we do not read the producer, we can not apply
4755 producer-specific interpretation. */
4756 attr = dwarf2_attr (cu->dies, DW_AT_producer, cu);
4757 if (attr)
4758 cu->producer = DW_STRING (attr);
4759
4760 if (read_cu)
4761 {
4762 /* We've successfully allocated this compilation unit. Let our
4763 caller clean it up when finished with it. */
4764 discard_cleanups (free_cu_cleanup);
4765
4766 /* Link this CU into read_in_chain. */
4767 per_cu->cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
4768 dwarf2_per_objfile->read_in_chain = per_cu;
4769 }
4770 }
4771
4772 /* Add a DIE to the delayed physname list. */
4773
4774 static void
4775 add_to_method_list (struct type *type, int fnfield_index, int index,
4776 const char *name, struct die_info *die,
4777 struct dwarf2_cu *cu)
4778 {
4779 struct delayed_method_info mi;
4780 mi.type = type;
4781 mi.fnfield_index = fnfield_index;
4782 mi.index = index;
4783 mi.name = name;
4784 mi.die = die;
4785 VEC_safe_push (delayed_method_info, cu->method_list, &mi);
4786 }
4787
4788 /* A cleanup for freeing the delayed method list. */
4789
4790 static void
4791 free_delayed_list (void *ptr)
4792 {
4793 struct dwarf2_cu *cu = (struct dwarf2_cu *) ptr;
4794 if (cu->method_list != NULL)
4795 {
4796 VEC_free (delayed_method_info, cu->method_list);
4797 cu->method_list = NULL;
4798 }
4799 }
4800
4801 /* Compute the physnames of any methods on the CU's method list.
4802
4803 The computation of method physnames is delayed in order to avoid the
4804 (bad) condition that one of the method's formal parameters is of an as yet
4805 incomplete type. */
4806
4807 static void
4808 compute_delayed_physnames (struct dwarf2_cu *cu)
4809 {
4810 int i;
4811 struct delayed_method_info *mi;
4812 for (i = 0; VEC_iterate (delayed_method_info, cu->method_list, i, mi) ; ++i)
4813 {
4814 const char *physname;
4815 struct fn_fieldlist *fn_flp
4816 = &TYPE_FN_FIELDLIST (mi->type, mi->fnfield_index);
4817 physname = dwarf2_physname ((char *) mi->name, mi->die, cu);
4818 fn_flp->fn_fields[mi->index].physname = physname ? physname : "";
4819 }
4820 }
4821
4822 /* Generate full symbol information for PER_CU, whose DIEs have
4823 already been loaded into memory. */
4824
4825 static void
4826 process_full_comp_unit (struct dwarf2_per_cu_data *per_cu)
4827 {
4828 struct dwarf2_cu *cu = per_cu->cu;
4829 struct objfile *objfile = per_cu->objfile;
4830 CORE_ADDR lowpc, highpc;
4831 struct symtab *symtab;
4832 struct cleanup *back_to, *delayed_list_cleanup;
4833 CORE_ADDR baseaddr;
4834
4835 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
4836
4837 buildsym_init ();
4838 back_to = make_cleanup (really_free_pendings, NULL);
4839 delayed_list_cleanup = make_cleanup (free_delayed_list, cu);
4840
4841 cu->list_in_scope = &file_symbols;
4842
4843 /* Do line number decoding in read_file_scope () */
4844 process_die (cu->dies, cu);
4845
4846 /* Now that we have processed all the DIEs in the CU, all the types
4847 should be complete, and it should now be safe to compute all of the
4848 physnames. */
4849 compute_delayed_physnames (cu);
4850 do_cleanups (delayed_list_cleanup);
4851
4852 /* Some compilers don't define a DW_AT_high_pc attribute for the
4853 compilation unit. If the DW_AT_high_pc is missing, synthesize
4854 it, by scanning the DIE's below the compilation unit. */
4855 get_scope_pc_bounds (cu->dies, &lowpc, &highpc, cu);
4856
4857 symtab = end_symtab (highpc + baseaddr, objfile, SECT_OFF_TEXT (objfile));
4858
4859 if (symtab != NULL)
4860 {
4861 int gcc_4_minor = producer_is_gcc_ge_4 (cu->producer);
4862
4863 /* Set symtab language to language from DW_AT_language. If the
4864 compilation is from a C file generated by language preprocessors, do
4865 not set the language if it was already deduced by start_subfile. */
4866 if (!(cu->language == language_c && symtab->language != language_c))
4867 symtab->language = cu->language;
4868
4869 /* GCC-4.0 has started to support -fvar-tracking. GCC-3.x still can
4870 produce DW_AT_location with location lists but it can be possibly
4871 invalid without -fvar-tracking.
4872
4873 For -gdwarf-4 type units LOCATIONS_VALID indication is fortunately not
4874 needed, it would be wrong due to missing DW_AT_producer there.
4875
4876 Still one can confuse GDB by using non-standard GCC compilation
4877 options - this waits on GCC PR other/32998 (-frecord-gcc-switches).
4878 */
4879 if (cu->has_loclist && gcc_4_minor >= 0)
4880 symtab->locations_valid = 1;
4881
4882 if (gcc_4_minor >= 5)
4883 symtab->epilogue_unwind_valid = 1;
4884
4885 symtab->call_site_htab = cu->call_site_htab;
4886 }
4887
4888 if (dwarf2_per_objfile->using_index)
4889 per_cu->v.quick->symtab = symtab;
4890 else
4891 {
4892 struct partial_symtab *pst = per_cu->v.psymtab;
4893 pst->symtab = symtab;
4894 pst->readin = 1;
4895 }
4896
4897 do_cleanups (back_to);
4898 }
4899
4900 /* Process a die and its children. */
4901
4902 static void
4903 process_die (struct die_info *die, struct dwarf2_cu *cu)
4904 {
4905 switch (die->tag)
4906 {
4907 case DW_TAG_padding:
4908 break;
4909 case DW_TAG_compile_unit:
4910 read_file_scope (die, cu);
4911 break;
4912 case DW_TAG_type_unit:
4913 read_type_unit_scope (die, cu);
4914 break;
4915 case DW_TAG_subprogram:
4916 case DW_TAG_inlined_subroutine:
4917 read_func_scope (die, cu);
4918 break;
4919 case DW_TAG_lexical_block:
4920 case DW_TAG_try_block:
4921 case DW_TAG_catch_block:
4922 read_lexical_block_scope (die, cu);
4923 break;
4924 case DW_TAG_GNU_call_site:
4925 read_call_site_scope (die, cu);
4926 break;
4927 case DW_TAG_class_type:
4928 case DW_TAG_interface_type:
4929 case DW_TAG_structure_type:
4930 case DW_TAG_union_type:
4931 process_structure_scope (die, cu);
4932 break;
4933 case DW_TAG_enumeration_type:
4934 process_enumeration_scope (die, cu);
4935 break;
4936
4937 /* These dies have a type, but processing them does not create
4938 a symbol or recurse to process the children. Therefore we can
4939 read them on-demand through read_type_die. */
4940 case DW_TAG_subroutine_type:
4941 case DW_TAG_set_type:
4942 case DW_TAG_array_type:
4943 case DW_TAG_pointer_type:
4944 case DW_TAG_ptr_to_member_type:
4945 case DW_TAG_reference_type:
4946 case DW_TAG_string_type:
4947 break;
4948
4949 case DW_TAG_base_type:
4950 case DW_TAG_subrange_type:
4951 case DW_TAG_typedef:
4952 /* Add a typedef symbol for the type definition, if it has a
4953 DW_AT_name. */
4954 new_symbol (die, read_type_die (die, cu), cu);
4955 break;
4956 case DW_TAG_common_block:
4957 read_common_block (die, cu);
4958 break;
4959 case DW_TAG_common_inclusion:
4960 break;
4961 case DW_TAG_namespace:
4962 processing_has_namespace_info = 1;
4963 read_namespace (die, cu);
4964 break;
4965 case DW_TAG_module:
4966 processing_has_namespace_info = 1;
4967 read_module (die, cu);
4968 break;
4969 case DW_TAG_imported_declaration:
4970 case DW_TAG_imported_module:
4971 processing_has_namespace_info = 1;
4972 if (die->child != NULL && (die->tag == DW_TAG_imported_declaration
4973 || cu->language != language_fortran))
4974 complaint (&symfile_complaints, _("Tag '%s' has unexpected children"),
4975 dwarf_tag_name (die->tag));
4976 read_import_statement (die, cu);
4977 break;
4978 default:
4979 new_symbol (die, NULL, cu);
4980 break;
4981 }
4982 }
4983
4984 /* A helper function for dwarf2_compute_name which determines whether DIE
4985 needs to have the name of the scope prepended to the name listed in the
4986 die. */
4987
4988 static int
4989 die_needs_namespace (struct die_info *die, struct dwarf2_cu *cu)
4990 {
4991 struct attribute *attr;
4992
4993 switch (die->tag)
4994 {
4995 case DW_TAG_namespace:
4996 case DW_TAG_typedef:
4997 case DW_TAG_class_type:
4998 case DW_TAG_interface_type:
4999 case DW_TAG_structure_type:
5000 case DW_TAG_union_type:
5001 case DW_TAG_enumeration_type:
5002 case DW_TAG_enumerator:
5003 case DW_TAG_subprogram:
5004 case DW_TAG_member:
5005 return 1;
5006
5007 case DW_TAG_variable:
5008 case DW_TAG_constant:
5009 /* We only need to prefix "globally" visible variables. These include
5010 any variable marked with DW_AT_external or any variable that
5011 lives in a namespace. [Variables in anonymous namespaces
5012 require prefixing, but they are not DW_AT_external.] */
5013
5014 if (dwarf2_attr (die, DW_AT_specification, cu))
5015 {
5016 struct dwarf2_cu *spec_cu = cu;
5017
5018 return die_needs_namespace (die_specification (die, &spec_cu),
5019 spec_cu);
5020 }
5021
5022 attr = dwarf2_attr (die, DW_AT_external, cu);
5023 if (attr == NULL && die->parent->tag != DW_TAG_namespace
5024 && die->parent->tag != DW_TAG_module)
5025 return 0;
5026 /* A variable in a lexical block of some kind does not need a
5027 namespace, even though in C++ such variables may be external
5028 and have a mangled name. */
5029 if (die->parent->tag == DW_TAG_lexical_block
5030 || die->parent->tag == DW_TAG_try_block
5031 || die->parent->tag == DW_TAG_catch_block
5032 || die->parent->tag == DW_TAG_subprogram)
5033 return 0;
5034 return 1;
5035
5036 default:
5037 return 0;
5038 }
5039 }
5040
5041 /* Retrieve the last character from a mem_file. */
5042
5043 static void
5044 do_ui_file_peek_last (void *object, const char *buffer, long length)
5045 {
5046 char *last_char_p = (char *) object;
5047
5048 if (length > 0)
5049 *last_char_p = buffer[length - 1];
5050 }
5051
5052 /* Compute the fully qualified name of DIE in CU. If PHYSNAME is nonzero,
5053 compute the physname for the object, which include a method's
5054 formal parameters (C++/Java) and return type (Java).
5055
5056 For Ada, return the DIE's linkage name rather than the fully qualified
5057 name. PHYSNAME is ignored..
5058
5059 The result is allocated on the objfile_obstack and canonicalized. */
5060
5061 static const char *
5062 dwarf2_compute_name (char *name, struct die_info *die, struct dwarf2_cu *cu,
5063 int physname)
5064 {
5065 struct objfile *objfile = cu->objfile;
5066
5067 if (name == NULL)
5068 name = dwarf2_name (die, cu);
5069
5070 /* For Fortran GDB prefers DW_AT_*linkage_name if present but otherwise
5071 compute it by typename_concat inside GDB. */
5072 if (cu->language == language_ada
5073 || (cu->language == language_fortran && physname))
5074 {
5075 /* For Ada unit, we prefer the linkage name over the name, as
5076 the former contains the exported name, which the user expects
5077 to be able to reference. Ideally, we want the user to be able
5078 to reference this entity using either natural or linkage name,
5079 but we haven't started looking at this enhancement yet. */
5080 struct attribute *attr;
5081
5082 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5083 if (attr == NULL)
5084 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5085 if (attr && DW_STRING (attr))
5086 return DW_STRING (attr);
5087 }
5088
5089 /* These are the only languages we know how to qualify names in. */
5090 if (name != NULL
5091 && (cu->language == language_cplus || cu->language == language_java
5092 || cu->language == language_fortran))
5093 {
5094 if (die_needs_namespace (die, cu))
5095 {
5096 long length;
5097 const char *prefix;
5098 struct ui_file *buf;
5099
5100 prefix = determine_prefix (die, cu);
5101 buf = mem_fileopen ();
5102 if (*prefix != '\0')
5103 {
5104 char *prefixed_name = typename_concat (NULL, prefix, name,
5105 physname, cu);
5106
5107 fputs_unfiltered (prefixed_name, buf);
5108 xfree (prefixed_name);
5109 }
5110 else
5111 fputs_unfiltered (name, buf);
5112
5113 /* Template parameters may be specified in the DIE's DW_AT_name, or
5114 as children with DW_TAG_template_type_param or
5115 DW_TAG_value_type_param. If the latter, add them to the name
5116 here. If the name already has template parameters, then
5117 skip this step; some versions of GCC emit both, and
5118 it is more efficient to use the pre-computed name.
5119
5120 Something to keep in mind about this process: it is very
5121 unlikely, or in some cases downright impossible, to produce
5122 something that will match the mangled name of a function.
5123 If the definition of the function has the same debug info,
5124 we should be able to match up with it anyway. But fallbacks
5125 using the minimal symbol, for instance to find a method
5126 implemented in a stripped copy of libstdc++, will not work.
5127 If we do not have debug info for the definition, we will have to
5128 match them up some other way.
5129
5130 When we do name matching there is a related problem with function
5131 templates; two instantiated function templates are allowed to
5132 differ only by their return types, which we do not add here. */
5133
5134 if (cu->language == language_cplus && strchr (name, '<') == NULL)
5135 {
5136 struct attribute *attr;
5137 struct die_info *child;
5138 int first = 1;
5139
5140 die->building_fullname = 1;
5141
5142 for (child = die->child; child != NULL; child = child->sibling)
5143 {
5144 struct type *type;
5145 long value;
5146 gdb_byte *bytes;
5147 struct dwarf2_locexpr_baton *baton;
5148 struct value *v;
5149
5150 if (child->tag != DW_TAG_template_type_param
5151 && child->tag != DW_TAG_template_value_param)
5152 continue;
5153
5154 if (first)
5155 {
5156 fputs_unfiltered ("<", buf);
5157 first = 0;
5158 }
5159 else
5160 fputs_unfiltered (", ", buf);
5161
5162 attr = dwarf2_attr (child, DW_AT_type, cu);
5163 if (attr == NULL)
5164 {
5165 complaint (&symfile_complaints,
5166 _("template parameter missing DW_AT_type"));
5167 fputs_unfiltered ("UNKNOWN_TYPE", buf);
5168 continue;
5169 }
5170 type = die_type (child, cu);
5171
5172 if (child->tag == DW_TAG_template_type_param)
5173 {
5174 c_print_type (type, "", buf, -1, 0);
5175 continue;
5176 }
5177
5178 attr = dwarf2_attr (child, DW_AT_const_value, cu);
5179 if (attr == NULL)
5180 {
5181 complaint (&symfile_complaints,
5182 _("template parameter missing "
5183 "DW_AT_const_value"));
5184 fputs_unfiltered ("UNKNOWN_VALUE", buf);
5185 continue;
5186 }
5187
5188 dwarf2_const_value_attr (attr, type, name,
5189 &cu->comp_unit_obstack, cu,
5190 &value, &bytes, &baton);
5191
5192 if (TYPE_NOSIGN (type))
5193 /* GDB prints characters as NUMBER 'CHAR'. If that's
5194 changed, this can use value_print instead. */
5195 c_printchar (value, type, buf);
5196 else
5197 {
5198 struct value_print_options opts;
5199
5200 if (baton != NULL)
5201 v = dwarf2_evaluate_loc_desc (type, NULL,
5202 baton->data,
5203 baton->size,
5204 baton->per_cu);
5205 else if (bytes != NULL)
5206 {
5207 v = allocate_value (type);
5208 memcpy (value_contents_writeable (v), bytes,
5209 TYPE_LENGTH (type));
5210 }
5211 else
5212 v = value_from_longest (type, value);
5213
5214 /* Specify decimal so that we do not depend on
5215 the radix. */
5216 get_formatted_print_options (&opts, 'd');
5217 opts.raw = 1;
5218 value_print (v, buf, &opts);
5219 release_value (v);
5220 value_free (v);
5221 }
5222 }
5223
5224 die->building_fullname = 0;
5225
5226 if (!first)
5227 {
5228 /* Close the argument list, with a space if necessary
5229 (nested templates). */
5230 char last_char = '\0';
5231 ui_file_put (buf, do_ui_file_peek_last, &last_char);
5232 if (last_char == '>')
5233 fputs_unfiltered (" >", buf);
5234 else
5235 fputs_unfiltered (">", buf);
5236 }
5237 }
5238
5239 /* For Java and C++ methods, append formal parameter type
5240 information, if PHYSNAME. */
5241
5242 if (physname && die->tag == DW_TAG_subprogram
5243 && (cu->language == language_cplus
5244 || cu->language == language_java))
5245 {
5246 struct type *type = read_type_die (die, cu);
5247
5248 c_type_print_args (type, buf, 1, cu->language);
5249
5250 if (cu->language == language_java)
5251 {
5252 /* For java, we must append the return type to method
5253 names. */
5254 if (die->tag == DW_TAG_subprogram)
5255 java_print_type (TYPE_TARGET_TYPE (type), "", buf,
5256 0, 0);
5257 }
5258 else if (cu->language == language_cplus)
5259 {
5260 /* Assume that an artificial first parameter is
5261 "this", but do not crash if it is not. RealView
5262 marks unnamed (and thus unused) parameters as
5263 artificial; there is no way to differentiate
5264 the two cases. */
5265 if (TYPE_NFIELDS (type) > 0
5266 && TYPE_FIELD_ARTIFICIAL (type, 0)
5267 && TYPE_CODE (TYPE_FIELD_TYPE (type, 0)) == TYPE_CODE_PTR
5268 && TYPE_CONST (TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (type,
5269 0))))
5270 fputs_unfiltered (" const", buf);
5271 }
5272 }
5273
5274 name = ui_file_obsavestring (buf, &objfile->objfile_obstack,
5275 &length);
5276 ui_file_delete (buf);
5277
5278 if (cu->language == language_cplus)
5279 {
5280 char *cname
5281 = dwarf2_canonicalize_name (name, cu,
5282 &objfile->objfile_obstack);
5283
5284 if (cname != NULL)
5285 name = cname;
5286 }
5287 }
5288 }
5289
5290 return name;
5291 }
5292
5293 /* Return the fully qualified name of DIE, based on its DW_AT_name.
5294 If scope qualifiers are appropriate they will be added. The result
5295 will be allocated on the objfile_obstack, or NULL if the DIE does
5296 not have a name. NAME may either be from a previous call to
5297 dwarf2_name or NULL.
5298
5299 The output string will be canonicalized (if C++/Java). */
5300
5301 static const char *
5302 dwarf2_full_name (char *name, struct die_info *die, struct dwarf2_cu *cu)
5303 {
5304 return dwarf2_compute_name (name, die, cu, 0);
5305 }
5306
5307 /* Construct a physname for the given DIE in CU. NAME may either be
5308 from a previous call to dwarf2_name or NULL. The result will be
5309 allocated on the objfile_objstack or NULL if the DIE does not have a
5310 name.
5311
5312 The output string will be canonicalized (if C++/Java). */
5313
5314 static const char *
5315 dwarf2_physname (char *name, struct die_info *die, struct dwarf2_cu *cu)
5316 {
5317 struct objfile *objfile = cu->objfile;
5318 struct attribute *attr;
5319 const char *retval, *mangled = NULL, *canon = NULL;
5320 struct cleanup *back_to;
5321 int need_copy = 1;
5322
5323 /* In this case dwarf2_compute_name is just a shortcut not building anything
5324 on its own. */
5325 if (!die_needs_namespace (die, cu))
5326 return dwarf2_compute_name (name, die, cu, 1);
5327
5328 back_to = make_cleanup (null_cleanup, NULL);
5329
5330 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
5331 if (!attr)
5332 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
5333
5334 /* DW_AT_linkage_name is missing in some cases - depend on what GDB
5335 has computed. */
5336 if (attr && DW_STRING (attr))
5337 {
5338 char *demangled;
5339
5340 mangled = DW_STRING (attr);
5341
5342 /* Use DMGL_RET_DROP for C++ template functions to suppress their return
5343 type. It is easier for GDB users to search for such functions as
5344 `name(params)' than `long name(params)'. In such case the minimal
5345 symbol names do not match the full symbol names but for template
5346 functions there is never a need to look up their definition from their
5347 declaration so the only disadvantage remains the minimal symbol
5348 variant `long name(params)' does not have the proper inferior type.
5349 */
5350
5351 demangled = cplus_demangle (mangled, (DMGL_PARAMS | DMGL_ANSI
5352 | (cu->language == language_java
5353 ? DMGL_JAVA | DMGL_RET_POSTFIX
5354 : DMGL_RET_DROP)));
5355 if (demangled)
5356 {
5357 make_cleanup (xfree, demangled);
5358 canon = demangled;
5359 }
5360 else
5361 {
5362 canon = mangled;
5363 need_copy = 0;
5364 }
5365 }
5366
5367 if (canon == NULL || check_physname)
5368 {
5369 const char *physname = dwarf2_compute_name (name, die, cu, 1);
5370
5371 if (canon != NULL && strcmp (physname, canon) != 0)
5372 {
5373 /* It may not mean a bug in GDB. The compiler could also
5374 compute DW_AT_linkage_name incorrectly. But in such case
5375 GDB would need to be bug-to-bug compatible. */
5376
5377 complaint (&symfile_complaints,
5378 _("Computed physname <%s> does not match demangled <%s> "
5379 "(from linkage <%s>) - DIE at 0x%x [in module %s]"),
5380 physname, canon, mangled, die->offset, objfile->name);
5381
5382 /* Prefer DW_AT_linkage_name (in the CANON form) - when it
5383 is available here - over computed PHYSNAME. It is safer
5384 against both buggy GDB and buggy compilers. */
5385
5386 retval = canon;
5387 }
5388 else
5389 {
5390 retval = physname;
5391 need_copy = 0;
5392 }
5393 }
5394 else
5395 retval = canon;
5396
5397 if (need_copy)
5398 retval = obsavestring (retval, strlen (retval),
5399 &objfile->objfile_obstack);
5400
5401 do_cleanups (back_to);
5402 return retval;
5403 }
5404
5405 /* Read the import statement specified by the given die and record it. */
5406
5407 static void
5408 read_import_statement (struct die_info *die, struct dwarf2_cu *cu)
5409 {
5410 struct objfile *objfile = cu->objfile;
5411 struct attribute *import_attr;
5412 struct die_info *imported_die, *child_die;
5413 struct dwarf2_cu *imported_cu;
5414 const char *imported_name;
5415 const char *imported_name_prefix;
5416 const char *canonical_name;
5417 const char *import_alias;
5418 const char *imported_declaration = NULL;
5419 const char *import_prefix;
5420 VEC (const_char_ptr) *excludes = NULL;
5421 struct cleanup *cleanups;
5422
5423 char *temp;
5424
5425 import_attr = dwarf2_attr (die, DW_AT_import, cu);
5426 if (import_attr == NULL)
5427 {
5428 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5429 dwarf_tag_name (die->tag));
5430 return;
5431 }
5432
5433 imported_cu = cu;
5434 imported_die = follow_die_ref_or_sig (die, import_attr, &imported_cu);
5435 imported_name = dwarf2_name (imported_die, imported_cu);
5436 if (imported_name == NULL)
5437 {
5438 /* GCC bug: https://bugzilla.redhat.com/show_bug.cgi?id=506524
5439
5440 The import in the following code:
5441 namespace A
5442 {
5443 typedef int B;
5444 }
5445
5446 int main ()
5447 {
5448 using A::B;
5449 B b;
5450 return b;
5451 }
5452
5453 ...
5454 <2><51>: Abbrev Number: 3 (DW_TAG_imported_declaration)
5455 <52> DW_AT_decl_file : 1
5456 <53> DW_AT_decl_line : 6
5457 <54> DW_AT_import : <0x75>
5458 <2><58>: Abbrev Number: 4 (DW_TAG_typedef)
5459 <59> DW_AT_name : B
5460 <5b> DW_AT_decl_file : 1
5461 <5c> DW_AT_decl_line : 2
5462 <5d> DW_AT_type : <0x6e>
5463 ...
5464 <1><75>: Abbrev Number: 7 (DW_TAG_base_type)
5465 <76> DW_AT_byte_size : 4
5466 <77> DW_AT_encoding : 5 (signed)
5467
5468 imports the wrong die ( 0x75 instead of 0x58 ).
5469 This case will be ignored until the gcc bug is fixed. */
5470 return;
5471 }
5472
5473 /* Figure out the local name after import. */
5474 import_alias = dwarf2_name (die, cu);
5475
5476 /* Figure out where the statement is being imported to. */
5477 import_prefix = determine_prefix (die, cu);
5478
5479 /* Figure out what the scope of the imported die is and prepend it
5480 to the name of the imported die. */
5481 imported_name_prefix = determine_prefix (imported_die, imported_cu);
5482
5483 if (imported_die->tag != DW_TAG_namespace
5484 && imported_die->tag != DW_TAG_module)
5485 {
5486 imported_declaration = imported_name;
5487 canonical_name = imported_name_prefix;
5488 }
5489 else if (strlen (imported_name_prefix) > 0)
5490 {
5491 temp = alloca (strlen (imported_name_prefix)
5492 + 2 + strlen (imported_name) + 1);
5493 strcpy (temp, imported_name_prefix);
5494 strcat (temp, "::");
5495 strcat (temp, imported_name);
5496 canonical_name = temp;
5497 }
5498 else
5499 canonical_name = imported_name;
5500
5501 cleanups = make_cleanup (VEC_cleanup (const_char_ptr), &excludes);
5502
5503 if (die->tag == DW_TAG_imported_module && cu->language == language_fortran)
5504 for (child_die = die->child; child_die && child_die->tag;
5505 child_die = sibling_die (child_die))
5506 {
5507 /* DWARF-4: A Fortran use statement with a “rename list” may be
5508 represented by an imported module entry with an import attribute
5509 referring to the module and owned entries corresponding to those
5510 entities that are renamed as part of being imported. */
5511
5512 if (child_die->tag != DW_TAG_imported_declaration)
5513 {
5514 complaint (&symfile_complaints,
5515 _("child DW_TAG_imported_declaration expected "
5516 "- DIE at 0x%x [in module %s]"),
5517 child_die->offset, objfile->name);
5518 continue;
5519 }
5520
5521 import_attr = dwarf2_attr (child_die, DW_AT_import, cu);
5522 if (import_attr == NULL)
5523 {
5524 complaint (&symfile_complaints, _("Tag '%s' has no DW_AT_import"),
5525 dwarf_tag_name (child_die->tag));
5526 continue;
5527 }
5528
5529 imported_cu = cu;
5530 imported_die = follow_die_ref_or_sig (child_die, import_attr,
5531 &imported_cu);
5532 imported_name = dwarf2_name (imported_die, imported_cu);
5533 if (imported_name == NULL)
5534 {
5535 complaint (&symfile_complaints,
5536 _("child DW_TAG_imported_declaration has unknown "
5537 "imported name - DIE at 0x%x [in module %s]"),
5538 child_die->offset, objfile->name);
5539 continue;
5540 }
5541
5542 VEC_safe_push (const_char_ptr, excludes, imported_name);
5543
5544 process_die (child_die, cu);
5545 }
5546
5547 cp_add_using_directive (import_prefix,
5548 canonical_name,
5549 import_alias,
5550 imported_declaration,
5551 excludes,
5552 &objfile->objfile_obstack);
5553
5554 do_cleanups (cleanups);
5555 }
5556
5557 /* Cleanup function for read_file_scope. */
5558
5559 static void
5560 free_cu_line_header (void *arg)
5561 {
5562 struct dwarf2_cu *cu = arg;
5563
5564 free_line_header (cu->line_header);
5565 cu->line_header = NULL;
5566 }
5567
5568 static void
5569 find_file_and_directory (struct die_info *die, struct dwarf2_cu *cu,
5570 char **name, char **comp_dir)
5571 {
5572 struct attribute *attr;
5573
5574 *name = NULL;
5575 *comp_dir = NULL;
5576
5577 /* Find the filename. Do not use dwarf2_name here, since the filename
5578 is not a source language identifier. */
5579 attr = dwarf2_attr (die, DW_AT_name, cu);
5580 if (attr)
5581 {
5582 *name = DW_STRING (attr);
5583 }
5584
5585 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5586 if (attr)
5587 *comp_dir = DW_STRING (attr);
5588 else if (*name != NULL && IS_ABSOLUTE_PATH (*name))
5589 {
5590 *comp_dir = ldirname (*name);
5591 if (*comp_dir != NULL)
5592 make_cleanup (xfree, *comp_dir);
5593 }
5594 if (*comp_dir != NULL)
5595 {
5596 /* Irix 6.2 native cc prepends <machine>.: to the compilation
5597 directory, get rid of it. */
5598 char *cp = strchr (*comp_dir, ':');
5599
5600 if (cp && cp != *comp_dir && cp[-1] == '.' && cp[1] == '/')
5601 *comp_dir = cp + 1;
5602 }
5603
5604 if (*name == NULL)
5605 *name = "<unknown>";
5606 }
5607
5608 /* Handle DW_AT_stmt_list for a compilation unit or type unit.
5609 DIE is the DW_TAG_compile_unit or DW_TAG_type_unit die for CU.
5610 COMP_DIR is the compilation directory.
5611 WANT_LINE_INFO is non-zero if the pc/line-number mapping is needed. */
5612
5613 static void
5614 handle_DW_AT_stmt_list (struct die_info *die, struct dwarf2_cu *cu,
5615 const char *comp_dir, int want_line_info)
5616 {
5617 struct attribute *attr;
5618 struct objfile *objfile = cu->objfile;
5619 bfd *abfd = objfile->obfd;
5620
5621 attr = dwarf2_attr (die, DW_AT_stmt_list, cu);
5622 if (attr)
5623 {
5624 unsigned int line_offset = DW_UNSND (attr);
5625 struct line_header *line_header
5626 = dwarf_decode_line_header (line_offset, abfd, cu);
5627
5628 if (line_header)
5629 {
5630 cu->line_header = line_header;
5631 make_cleanup (free_cu_line_header, cu);
5632 dwarf_decode_lines (line_header, comp_dir, cu, NULL, want_line_info);
5633 }
5634 }
5635 }
5636
5637 /* Process DW_TAG_compile_unit. */
5638
5639 static void
5640 read_file_scope (struct die_info *die, struct dwarf2_cu *cu)
5641 {
5642 struct objfile *objfile = cu->objfile;
5643 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5644 CORE_ADDR lowpc = ((CORE_ADDR) -1);
5645 CORE_ADDR highpc = ((CORE_ADDR) 0);
5646 struct attribute *attr;
5647 char *name = NULL;
5648 char *comp_dir = NULL;
5649 struct die_info *child_die;
5650 bfd *abfd = objfile->obfd;
5651 CORE_ADDR baseaddr;
5652
5653 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5654
5655 get_scope_pc_bounds (die, &lowpc, &highpc, cu);
5656
5657 /* If we didn't find a lowpc, set it to highpc to avoid complaints
5658 from finish_block. */
5659 if (lowpc == ((CORE_ADDR) -1))
5660 lowpc = highpc;
5661 lowpc += baseaddr;
5662 highpc += baseaddr;
5663
5664 find_file_and_directory (die, cu, &name, &comp_dir);
5665
5666 attr = dwarf2_attr (die, DW_AT_language, cu);
5667 if (attr)
5668 {
5669 set_cu_language (DW_UNSND (attr), cu);
5670 }
5671
5672 attr = dwarf2_attr (die, DW_AT_producer, cu);
5673 if (attr)
5674 cu->producer = DW_STRING (attr);
5675
5676 /* The XLCL doesn't generate DW_LANG_OpenCL because this attribute is not
5677 standardised yet. As a workaround for the language detection we fall
5678 back to the DW_AT_producer string. */
5679 if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL") != NULL)
5680 cu->language = language_opencl;
5681
5682 /* We assume that we're processing GCC output. */
5683 processing_gcc_compilation = 2;
5684
5685 processing_has_namespace_info = 0;
5686
5687 start_symtab (name, comp_dir, lowpc);
5688 record_debugformat ("DWARF 2");
5689 record_producer (cu->producer);
5690
5691 /* Decode line number information if present. We do this before
5692 processing child DIEs, so that the line header table is available
5693 for DW_AT_decl_file. */
5694 handle_DW_AT_stmt_list (die, cu, comp_dir, 1);
5695
5696 /* Process all dies in compilation unit. */
5697 if (die->child != NULL)
5698 {
5699 child_die = die->child;
5700 while (child_die && child_die->tag)
5701 {
5702 process_die (child_die, cu);
5703 child_die = sibling_die (child_die);
5704 }
5705 }
5706
5707 /* Decode macro information, if present. Dwarf 2 macro information
5708 refers to information in the line number info statement program
5709 header, so we can only read it if we've read the header
5710 successfully. */
5711 attr = dwarf2_attr (die, DW_AT_GNU_macros, cu);
5712 if (attr && cu->line_header)
5713 {
5714 if (dwarf2_attr (die, DW_AT_macro_info, cu))
5715 complaint (&symfile_complaints,
5716 _("CU refers to both DW_AT_GNU_macros and DW_AT_macro_info"));
5717
5718 dwarf_decode_macros (cu->line_header, DW_UNSND (attr),
5719 comp_dir, abfd, cu,
5720 &dwarf2_per_objfile->macro, 1);
5721 }
5722 else
5723 {
5724 attr = dwarf2_attr (die, DW_AT_macro_info, cu);
5725 if (attr && cu->line_header)
5726 {
5727 unsigned int macro_offset = DW_UNSND (attr);
5728
5729 dwarf_decode_macros (cu->line_header, macro_offset,
5730 comp_dir, abfd, cu,
5731 &dwarf2_per_objfile->macinfo, 0);
5732 }
5733 }
5734
5735 do_cleanups (back_to);
5736 }
5737
5738 /* Process DW_TAG_type_unit.
5739 For TUs we want to skip the first top level sibling if it's not the
5740 actual type being defined by this TU. In this case the first top
5741 level sibling is there to provide context only. */
5742
5743 static void
5744 read_type_unit_scope (struct die_info *die, struct dwarf2_cu *cu)
5745 {
5746 struct objfile *objfile = cu->objfile;
5747 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
5748 CORE_ADDR lowpc;
5749 struct attribute *attr;
5750 char *name = NULL;
5751 char *comp_dir = NULL;
5752 struct die_info *child_die;
5753 bfd *abfd = objfile->obfd;
5754
5755 /* start_symtab needs a low pc, but we don't really have one.
5756 Do what read_file_scope would do in the absence of such info. */
5757 lowpc = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5758
5759 /* Find the filename. Do not use dwarf2_name here, since the filename
5760 is not a source language identifier. */
5761 attr = dwarf2_attr (die, DW_AT_name, cu);
5762 if (attr)
5763 name = DW_STRING (attr);
5764
5765 attr = dwarf2_attr (die, DW_AT_comp_dir, cu);
5766 if (attr)
5767 comp_dir = DW_STRING (attr);
5768 else if (name != NULL && IS_ABSOLUTE_PATH (name))
5769 {
5770 comp_dir = ldirname (name);
5771 if (comp_dir != NULL)
5772 make_cleanup (xfree, comp_dir);
5773 }
5774
5775 if (name == NULL)
5776 name = "<unknown>";
5777
5778 attr = dwarf2_attr (die, DW_AT_language, cu);
5779 if (attr)
5780 set_cu_language (DW_UNSND (attr), cu);
5781
5782 /* This isn't technically needed today. It is done for symmetry
5783 with read_file_scope. */
5784 attr = dwarf2_attr (die, DW_AT_producer, cu);
5785 if (attr)
5786 cu->producer = DW_STRING (attr);
5787
5788 /* We assume that we're processing GCC output. */
5789 processing_gcc_compilation = 2;
5790
5791 processing_has_namespace_info = 0;
5792
5793 start_symtab (name, comp_dir, lowpc);
5794 record_debugformat ("DWARF 2");
5795 record_producer (cu->producer);
5796
5797 /* Decode line number information if present. We do this before
5798 processing child DIEs, so that the line header table is available
5799 for DW_AT_decl_file.
5800 We don't need the pc/line-number mapping for type units. */
5801 handle_DW_AT_stmt_list (die, cu, comp_dir, 0);
5802
5803 /* Process the dies in the type unit. */
5804 if (die->child == NULL)
5805 {
5806 dump_die_for_error (die);
5807 error (_("Dwarf Error: Missing children for type unit [in module %s]"),
5808 bfd_get_filename (abfd));
5809 }
5810
5811 child_die = die->child;
5812
5813 while (child_die && child_die->tag)
5814 {
5815 process_die (child_die, cu);
5816
5817 child_die = sibling_die (child_die);
5818 }
5819
5820 do_cleanups (back_to);
5821 }
5822
5823 /* qsort helper for inherit_abstract_dies. */
5824
5825 static int
5826 unsigned_int_compar (const void *ap, const void *bp)
5827 {
5828 unsigned int a = *(unsigned int *) ap;
5829 unsigned int b = *(unsigned int *) bp;
5830
5831 return (a > b) - (b > a);
5832 }
5833
5834 /* DW_AT_abstract_origin inherits whole DIEs (not just their attributes).
5835 Inherit only the children of the DW_AT_abstract_origin DIE not being
5836 already referenced by DW_AT_abstract_origin from the children of the
5837 current DIE. */
5838
5839 static void
5840 inherit_abstract_dies (struct die_info *die, struct dwarf2_cu *cu)
5841 {
5842 struct die_info *child_die;
5843 unsigned die_children_count;
5844 /* CU offsets which were referenced by children of the current DIE. */
5845 unsigned *offsets;
5846 unsigned *offsets_end, *offsetp;
5847 /* Parent of DIE - referenced by DW_AT_abstract_origin. */
5848 struct die_info *origin_die;
5849 /* Iterator of the ORIGIN_DIE children. */
5850 struct die_info *origin_child_die;
5851 struct cleanup *cleanups;
5852 struct attribute *attr;
5853 struct dwarf2_cu *origin_cu;
5854 struct pending **origin_previous_list_in_scope;
5855
5856 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
5857 if (!attr)
5858 return;
5859
5860 /* Note that following die references may follow to a die in a
5861 different cu. */
5862
5863 origin_cu = cu;
5864 origin_die = follow_die_ref (die, attr, &origin_cu);
5865
5866 /* We're inheriting ORIGIN's children into the scope we'd put DIE's
5867 symbols in. */
5868 origin_previous_list_in_scope = origin_cu->list_in_scope;
5869 origin_cu->list_in_scope = cu->list_in_scope;
5870
5871 if (die->tag != origin_die->tag
5872 && !(die->tag == DW_TAG_inlined_subroutine
5873 && origin_die->tag == DW_TAG_subprogram))
5874 complaint (&symfile_complaints,
5875 _("DIE 0x%x and its abstract origin 0x%x have different tags"),
5876 die->offset, origin_die->offset);
5877
5878 child_die = die->child;
5879 die_children_count = 0;
5880 while (child_die && child_die->tag)
5881 {
5882 child_die = sibling_die (child_die);
5883 die_children_count++;
5884 }
5885 offsets = xmalloc (sizeof (*offsets) * die_children_count);
5886 cleanups = make_cleanup (xfree, offsets);
5887
5888 offsets_end = offsets;
5889 child_die = die->child;
5890 while (child_die && child_die->tag)
5891 {
5892 /* For each CHILD_DIE, find the corresponding child of
5893 ORIGIN_DIE. If there is more than one layer of
5894 DW_AT_abstract_origin, follow them all; there shouldn't be,
5895 but GCC versions at least through 4.4 generate this (GCC PR
5896 40573). */
5897 struct die_info *child_origin_die = child_die;
5898 struct dwarf2_cu *child_origin_cu = cu;
5899
5900 while (1)
5901 {
5902 attr = dwarf2_attr (child_origin_die, DW_AT_abstract_origin,
5903 child_origin_cu);
5904 if (attr == NULL)
5905 break;
5906 child_origin_die = follow_die_ref (child_origin_die, attr,
5907 &child_origin_cu);
5908 }
5909
5910 /* According to DWARF3 3.3.8.2 #3 new entries without their abstract
5911 counterpart may exist. */
5912 if (child_origin_die != child_die)
5913 {
5914 if (child_die->tag != child_origin_die->tag
5915 && !(child_die->tag == DW_TAG_inlined_subroutine
5916 && child_origin_die->tag == DW_TAG_subprogram))
5917 complaint (&symfile_complaints,
5918 _("Child DIE 0x%x and its abstract origin 0x%x have "
5919 "different tags"), child_die->offset,
5920 child_origin_die->offset);
5921 if (child_origin_die->parent != origin_die)
5922 complaint (&symfile_complaints,
5923 _("Child DIE 0x%x and its abstract origin 0x%x have "
5924 "different parents"), child_die->offset,
5925 child_origin_die->offset);
5926 else
5927 *offsets_end++ = child_origin_die->offset;
5928 }
5929 child_die = sibling_die (child_die);
5930 }
5931 qsort (offsets, offsets_end - offsets, sizeof (*offsets),
5932 unsigned_int_compar);
5933 for (offsetp = offsets + 1; offsetp < offsets_end; offsetp++)
5934 if (offsetp[-1] == *offsetp)
5935 complaint (&symfile_complaints,
5936 _("Multiple children of DIE 0x%x refer "
5937 "to DIE 0x%x as their abstract origin"),
5938 die->offset, *offsetp);
5939
5940 offsetp = offsets;
5941 origin_child_die = origin_die->child;
5942 while (origin_child_die && origin_child_die->tag)
5943 {
5944 /* Is ORIGIN_CHILD_DIE referenced by any of the DIE children? */
5945 while (offsetp < offsets_end && *offsetp < origin_child_die->offset)
5946 offsetp++;
5947 if (offsetp >= offsets_end || *offsetp > origin_child_die->offset)
5948 {
5949 /* Found that ORIGIN_CHILD_DIE is really not referenced. */
5950 process_die (origin_child_die, origin_cu);
5951 }
5952 origin_child_die = sibling_die (origin_child_die);
5953 }
5954 origin_cu->list_in_scope = origin_previous_list_in_scope;
5955
5956 do_cleanups (cleanups);
5957 }
5958
5959 static void
5960 read_func_scope (struct die_info *die, struct dwarf2_cu *cu)
5961 {
5962 struct objfile *objfile = cu->objfile;
5963 struct context_stack *new;
5964 CORE_ADDR lowpc;
5965 CORE_ADDR highpc;
5966 struct die_info *child_die;
5967 struct attribute *attr, *call_line, *call_file;
5968 char *name;
5969 CORE_ADDR baseaddr;
5970 struct block *block;
5971 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
5972 VEC (symbolp) *template_args = NULL;
5973 struct template_symbol *templ_func = NULL;
5974
5975 if (inlined_func)
5976 {
5977 /* If we do not have call site information, we can't show the
5978 caller of this inlined function. That's too confusing, so
5979 only use the scope for local variables. */
5980 call_line = dwarf2_attr (die, DW_AT_call_line, cu);
5981 call_file = dwarf2_attr (die, DW_AT_call_file, cu);
5982 if (call_line == NULL || call_file == NULL)
5983 {
5984 read_lexical_block_scope (die, cu);
5985 return;
5986 }
5987 }
5988
5989 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
5990
5991 name = dwarf2_name (die, cu);
5992
5993 /* Ignore functions with missing or empty names. These are actually
5994 illegal according to the DWARF standard. */
5995 if (name == NULL)
5996 {
5997 complaint (&symfile_complaints,
5998 _("missing name for subprogram DIE at %d"), die->offset);
5999 return;
6000 }
6001
6002 /* Ignore functions with missing or invalid low and high pc attributes. */
6003 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6004 {
6005 attr = dwarf2_attr (die, DW_AT_external, cu);
6006 if (!attr || !DW_UNSND (attr))
6007 complaint (&symfile_complaints,
6008 _("cannot get low and high bounds "
6009 "for subprogram DIE at %d"),
6010 die->offset);
6011 return;
6012 }
6013
6014 lowpc += baseaddr;
6015 highpc += baseaddr;
6016
6017 /* If we have any template arguments, then we must allocate a
6018 different sort of symbol. */
6019 for (child_die = die->child; child_die; child_die = sibling_die (child_die))
6020 {
6021 if (child_die->tag == DW_TAG_template_type_param
6022 || child_die->tag == DW_TAG_template_value_param)
6023 {
6024 templ_func = OBSTACK_ZALLOC (&objfile->objfile_obstack,
6025 struct template_symbol);
6026 templ_func->base.is_cplus_template_function = 1;
6027 break;
6028 }
6029 }
6030
6031 new = push_context (0, lowpc);
6032 new->name = new_symbol_full (die, read_type_die (die, cu), cu,
6033 (struct symbol *) templ_func);
6034
6035 /* If there is a location expression for DW_AT_frame_base, record
6036 it. */
6037 attr = dwarf2_attr (die, DW_AT_frame_base, cu);
6038 if (attr)
6039 /* FIXME: cagney/2004-01-26: The DW_AT_frame_base's location
6040 expression is being recorded directly in the function's symbol
6041 and not in a separate frame-base object. I guess this hack is
6042 to avoid adding some sort of frame-base adjunct/annex to the
6043 function's symbol :-(. The problem with doing this is that it
6044 results in a function symbol with a location expression that
6045 has nothing to do with the location of the function, ouch! The
6046 relationship should be: a function's symbol has-a frame base; a
6047 frame-base has-a location expression. */
6048 dwarf2_symbol_mark_computed (attr, new->name, cu);
6049
6050 cu->list_in_scope = &local_symbols;
6051
6052 if (die->child != NULL)
6053 {
6054 child_die = die->child;
6055 while (child_die && child_die->tag)
6056 {
6057 if (child_die->tag == DW_TAG_template_type_param
6058 || child_die->tag == DW_TAG_template_value_param)
6059 {
6060 struct symbol *arg = new_symbol (child_die, NULL, cu);
6061
6062 if (arg != NULL)
6063 VEC_safe_push (symbolp, template_args, arg);
6064 }
6065 else
6066 process_die (child_die, cu);
6067 child_die = sibling_die (child_die);
6068 }
6069 }
6070
6071 inherit_abstract_dies (die, cu);
6072
6073 /* If we have a DW_AT_specification, we might need to import using
6074 directives from the context of the specification DIE. See the
6075 comment in determine_prefix. */
6076 if (cu->language == language_cplus
6077 && dwarf2_attr (die, DW_AT_specification, cu))
6078 {
6079 struct dwarf2_cu *spec_cu = cu;
6080 struct die_info *spec_die = die_specification (die, &spec_cu);
6081
6082 while (spec_die)
6083 {
6084 child_die = spec_die->child;
6085 while (child_die && child_die->tag)
6086 {
6087 if (child_die->tag == DW_TAG_imported_module)
6088 process_die (child_die, spec_cu);
6089 child_die = sibling_die (child_die);
6090 }
6091
6092 /* In some cases, GCC generates specification DIEs that
6093 themselves contain DW_AT_specification attributes. */
6094 spec_die = die_specification (spec_die, &spec_cu);
6095 }
6096 }
6097
6098 new = pop_context ();
6099 /* Make a block for the local symbols within. */
6100 block = finish_block (new->name, &local_symbols, new->old_blocks,
6101 lowpc, highpc, objfile);
6102
6103 /* For C++, set the block's scope. */
6104 if (cu->language == language_cplus || cu->language == language_fortran)
6105 cp_set_block_scope (new->name, block, &objfile->objfile_obstack,
6106 determine_prefix (die, cu),
6107 processing_has_namespace_info);
6108
6109 /* If we have address ranges, record them. */
6110 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6111
6112 /* Attach template arguments to function. */
6113 if (! VEC_empty (symbolp, template_args))
6114 {
6115 gdb_assert (templ_func != NULL);
6116
6117 templ_func->n_template_arguments = VEC_length (symbolp, template_args);
6118 templ_func->template_arguments
6119 = obstack_alloc (&objfile->objfile_obstack,
6120 (templ_func->n_template_arguments
6121 * sizeof (struct symbol *)));
6122 memcpy (templ_func->template_arguments,
6123 VEC_address (symbolp, template_args),
6124 (templ_func->n_template_arguments * sizeof (struct symbol *)));
6125 VEC_free (symbolp, template_args);
6126 }
6127
6128 /* In C++, we can have functions nested inside functions (e.g., when
6129 a function declares a class that has methods). This means that
6130 when we finish processing a function scope, we may need to go
6131 back to building a containing block's symbol lists. */
6132 local_symbols = new->locals;
6133 param_symbols = new->params;
6134 using_directives = new->using_directives;
6135
6136 /* If we've finished processing a top-level function, subsequent
6137 symbols go in the file symbol list. */
6138 if (outermost_context_p ())
6139 cu->list_in_scope = &file_symbols;
6140 }
6141
6142 /* Process all the DIES contained within a lexical block scope. Start
6143 a new scope, process the dies, and then close the scope. */
6144
6145 static void
6146 read_lexical_block_scope (struct die_info *die, struct dwarf2_cu *cu)
6147 {
6148 struct objfile *objfile = cu->objfile;
6149 struct context_stack *new;
6150 CORE_ADDR lowpc, highpc;
6151 struct die_info *child_die;
6152 CORE_ADDR baseaddr;
6153
6154 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6155
6156 /* Ignore blocks with missing or invalid low and high pc attributes. */
6157 /* ??? Perhaps consider discontiguous blocks defined by DW_AT_ranges
6158 as multiple lexical blocks? Handling children in a sane way would
6159 be nasty. Might be easier to properly extend generic blocks to
6160 describe ranges. */
6161 if (!dwarf2_get_pc_bounds (die, &lowpc, &highpc, cu, NULL))
6162 return;
6163 lowpc += baseaddr;
6164 highpc += baseaddr;
6165
6166 push_context (0, lowpc);
6167 if (die->child != NULL)
6168 {
6169 child_die = die->child;
6170 while (child_die && child_die->tag)
6171 {
6172 process_die (child_die, cu);
6173 child_die = sibling_die (child_die);
6174 }
6175 }
6176 new = pop_context ();
6177
6178 if (local_symbols != NULL || using_directives != NULL)
6179 {
6180 struct block *block
6181 = finish_block (0, &local_symbols, new->old_blocks, new->start_addr,
6182 highpc, objfile);
6183
6184 /* Note that recording ranges after traversing children, as we
6185 do here, means that recording a parent's ranges entails
6186 walking across all its children's ranges as they appear in
6187 the address map, which is quadratic behavior.
6188
6189 It would be nicer to record the parent's ranges before
6190 traversing its children, simply overriding whatever you find
6191 there. But since we don't even decide whether to create a
6192 block until after we've traversed its children, that's hard
6193 to do. */
6194 dwarf2_record_block_ranges (die, block, baseaddr, cu);
6195 }
6196 local_symbols = new->locals;
6197 using_directives = new->using_directives;
6198 }
6199
6200 /* Read in DW_TAG_GNU_call_site and insert it to CU->call_site_htab. */
6201
6202 static void
6203 read_call_site_scope (struct die_info *die, struct dwarf2_cu *cu)
6204 {
6205 struct objfile *objfile = cu->objfile;
6206 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6207 CORE_ADDR pc, baseaddr;
6208 struct attribute *attr;
6209 struct call_site *call_site, call_site_local;
6210 void **slot;
6211 int nparams;
6212 struct die_info *child_die;
6213
6214 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6215
6216 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6217 if (!attr)
6218 {
6219 complaint (&symfile_complaints,
6220 _("missing DW_AT_low_pc for DW_TAG_GNU_call_site "
6221 "DIE 0x%x [in module %s]"),
6222 die->offset, objfile->name);
6223 return;
6224 }
6225 pc = DW_ADDR (attr) + baseaddr;
6226
6227 if (cu->call_site_htab == NULL)
6228 cu->call_site_htab = htab_create_alloc_ex (16, core_addr_hash, core_addr_eq,
6229 NULL, &objfile->objfile_obstack,
6230 hashtab_obstack_allocate, NULL);
6231 call_site_local.pc = pc;
6232 slot = htab_find_slot (cu->call_site_htab, &call_site_local, INSERT);
6233 if (*slot != NULL)
6234 {
6235 complaint (&symfile_complaints,
6236 _("Duplicate PC %s for DW_TAG_GNU_call_site "
6237 "DIE 0x%x [in module %s]"),
6238 paddress (gdbarch, pc), die->offset, objfile->name);
6239 return;
6240 }
6241
6242 /* Count parameters at the caller. */
6243
6244 nparams = 0;
6245 for (child_die = die->child; child_die && child_die->tag;
6246 child_die = sibling_die (child_die))
6247 {
6248 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6249 {
6250 complaint (&symfile_complaints,
6251 _("Tag %d is not DW_TAG_GNU_call_site_parameter in "
6252 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6253 child_die->tag, child_die->offset, objfile->name);
6254 continue;
6255 }
6256
6257 nparams++;
6258 }
6259
6260 call_site = obstack_alloc (&objfile->objfile_obstack,
6261 (sizeof (*call_site)
6262 + (sizeof (*call_site->parameter)
6263 * (nparams - 1))));
6264 *slot = call_site;
6265 memset (call_site, 0, sizeof (*call_site) - sizeof (*call_site->parameter));
6266 call_site->pc = pc;
6267
6268 if (dwarf2_flag_true_p (die, DW_AT_GNU_tail_call, cu))
6269 {
6270 struct die_info *func_die;
6271
6272 /* Skip also over DW_TAG_inlined_subroutine. */
6273 for (func_die = die->parent;
6274 func_die && func_die->tag != DW_TAG_subprogram
6275 && func_die->tag != DW_TAG_subroutine_type;
6276 func_die = func_die->parent);
6277
6278 /* DW_AT_GNU_all_call_sites is a superset
6279 of DW_AT_GNU_all_tail_call_sites. */
6280 if (func_die
6281 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_call_sites, cu)
6282 && !dwarf2_flag_true_p (func_die, DW_AT_GNU_all_tail_call_sites, cu))
6283 {
6284 /* TYPE_TAIL_CALL_LIST is not interesting in functions where it is
6285 not complete. But keep CALL_SITE for look ups via call_site_htab,
6286 both the initial caller containing the real return address PC and
6287 the final callee containing the current PC of a chain of tail
6288 calls do not need to have the tail call list complete. But any
6289 function candidate for a virtual tail call frame searched via
6290 TYPE_TAIL_CALL_LIST must have the tail call list complete to be
6291 determined unambiguously. */
6292 }
6293 else
6294 {
6295 struct type *func_type = NULL;
6296
6297 if (func_die)
6298 func_type = get_die_type (func_die, cu);
6299 if (func_type != NULL)
6300 {
6301 gdb_assert (TYPE_CODE (func_type) == TYPE_CODE_FUNC);
6302
6303 /* Enlist this call site to the function. */
6304 call_site->tail_call_next = TYPE_TAIL_CALL_LIST (func_type);
6305 TYPE_TAIL_CALL_LIST (func_type) = call_site;
6306 }
6307 else
6308 complaint (&symfile_complaints,
6309 _("Cannot find function owning DW_TAG_GNU_call_site "
6310 "DIE 0x%x [in module %s]"),
6311 die->offset, objfile->name);
6312 }
6313 }
6314
6315 attr = dwarf2_attr (die, DW_AT_GNU_call_site_target, cu);
6316 if (attr == NULL)
6317 attr = dwarf2_attr (die, DW_AT_abstract_origin, cu);
6318 SET_FIELD_DWARF_BLOCK (call_site->target, NULL);
6319 if (!attr || (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0))
6320 /* Keep NULL DWARF_BLOCK. */;
6321 else if (attr_form_is_block (attr))
6322 {
6323 struct dwarf2_locexpr_baton *dlbaton;
6324
6325 dlbaton = obstack_alloc (&objfile->objfile_obstack, sizeof (*dlbaton));
6326 dlbaton->data = DW_BLOCK (attr)->data;
6327 dlbaton->size = DW_BLOCK (attr)->size;
6328 dlbaton->per_cu = cu->per_cu;
6329
6330 SET_FIELD_DWARF_BLOCK (call_site->target, dlbaton);
6331 }
6332 else if (is_ref_attr (attr))
6333 {
6334 struct dwarf2_cu *target_cu = cu;
6335 struct die_info *target_die;
6336
6337 target_die = follow_die_ref_or_sig (die, attr, &target_cu);
6338 gdb_assert (target_cu->objfile == objfile);
6339 if (die_is_declaration (target_die, target_cu))
6340 {
6341 const char *target_physname;
6342
6343 target_physname = dwarf2_physname (NULL, target_die, target_cu);
6344 if (target_physname == NULL)
6345 complaint (&symfile_complaints,
6346 _("DW_AT_GNU_call_site_target target DIE has invalid "
6347 "physname, for referencing DIE 0x%x [in module %s]"),
6348 die->offset, objfile->name);
6349 else
6350 SET_FIELD_PHYSNAME (call_site->target, (char *) target_physname);
6351 }
6352 else
6353 {
6354 CORE_ADDR lowpc;
6355
6356 /* DW_AT_entry_pc should be preferred. */
6357 if (!dwarf2_get_pc_bounds (target_die, &lowpc, NULL, target_cu, NULL))
6358 complaint (&symfile_complaints,
6359 _("DW_AT_GNU_call_site_target target DIE has invalid "
6360 "low pc, for referencing DIE 0x%x [in module %s]"),
6361 die->offset, objfile->name);
6362 else
6363 SET_FIELD_PHYSADDR (call_site->target, lowpc + baseaddr);
6364 }
6365 }
6366 else
6367 complaint (&symfile_complaints,
6368 _("DW_TAG_GNU_call_site DW_AT_GNU_call_site_target is neither "
6369 "block nor reference, for DIE 0x%x [in module %s]"),
6370 die->offset, objfile->name);
6371
6372 call_site->per_cu = cu->per_cu;
6373
6374 for (child_die = die->child;
6375 child_die && child_die->tag;
6376 child_die = sibling_die (child_die))
6377 {
6378 struct dwarf2_locexpr_baton *dlbaton;
6379 struct call_site_parameter *parameter;
6380
6381 if (child_die->tag != DW_TAG_GNU_call_site_parameter)
6382 {
6383 /* Already printed the complaint above. */
6384 continue;
6385 }
6386
6387 gdb_assert (call_site->parameter_count < nparams);
6388 parameter = &call_site->parameter[call_site->parameter_count];
6389
6390 /* DW_AT_location specifies the register number. Value of the data
6391 assumed for the register is contained in DW_AT_GNU_call_site_value. */
6392
6393 attr = dwarf2_attr (child_die, DW_AT_location, cu);
6394 if (!attr || !attr_form_is_block (attr))
6395 {
6396 complaint (&symfile_complaints,
6397 _("No DW_FORM_block* DW_AT_location for "
6398 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6399 child_die->offset, objfile->name);
6400 continue;
6401 }
6402 parameter->dwarf_reg = dwarf_block_to_dwarf_reg (DW_BLOCK (attr)->data,
6403 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size]);
6404 if (parameter->dwarf_reg == -1
6405 && !dwarf_block_to_sp_offset (gdbarch, DW_BLOCK (attr)->data,
6406 &DW_BLOCK (attr)->data[DW_BLOCK (attr)->size],
6407 &parameter->fb_offset))
6408 {
6409 complaint (&symfile_complaints,
6410 _("Only single DW_OP_reg or DW_OP_fbreg is supported "
6411 "for DW_FORM_block* DW_AT_location for "
6412 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6413 child_die->offset, objfile->name);
6414 continue;
6415 }
6416
6417 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_value, cu);
6418 if (!attr_form_is_block (attr))
6419 {
6420 complaint (&symfile_complaints,
6421 _("No DW_FORM_block* DW_AT_GNU_call_site_value for "
6422 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6423 child_die->offset, objfile->name);
6424 continue;
6425 }
6426 parameter->value = DW_BLOCK (attr)->data;
6427 parameter->value_size = DW_BLOCK (attr)->size;
6428
6429 /* Parameters are not pre-cleared by memset above. */
6430 parameter->data_value = NULL;
6431 parameter->data_value_size = 0;
6432 call_site->parameter_count++;
6433
6434 attr = dwarf2_attr (child_die, DW_AT_GNU_call_site_data_value, cu);
6435 if (attr)
6436 {
6437 if (!attr_form_is_block (attr))
6438 complaint (&symfile_complaints,
6439 _("No DW_FORM_block* DW_AT_GNU_call_site_data_value for "
6440 "DW_TAG_GNU_call_site child DIE 0x%x [in module %s]"),
6441 child_die->offset, objfile->name);
6442 else
6443 {
6444 parameter->data_value = DW_BLOCK (attr)->data;
6445 parameter->data_value_size = DW_BLOCK (attr)->size;
6446 }
6447 }
6448 }
6449 }
6450
6451 /* Get low and high pc attributes from DW_AT_ranges attribute value OFFSET.
6452 Return 1 if the attributes are present and valid, otherwise, return 0.
6453 If RANGES_PST is not NULL we should setup `objfile->psymtabs_addrmap'. */
6454
6455 static int
6456 dwarf2_ranges_read (unsigned offset, CORE_ADDR *low_return,
6457 CORE_ADDR *high_return, struct dwarf2_cu *cu,
6458 struct partial_symtab *ranges_pst)
6459 {
6460 struct objfile *objfile = cu->objfile;
6461 struct comp_unit_head *cu_header = &cu->header;
6462 bfd *obfd = objfile->obfd;
6463 unsigned int addr_size = cu_header->addr_size;
6464 CORE_ADDR mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6465 /* Base address selection entry. */
6466 CORE_ADDR base;
6467 int found_base;
6468 unsigned int dummy;
6469 gdb_byte *buffer;
6470 CORE_ADDR marker;
6471 int low_set;
6472 CORE_ADDR low = 0;
6473 CORE_ADDR high = 0;
6474 CORE_ADDR baseaddr;
6475
6476 found_base = cu->base_known;
6477 base = cu->base_address;
6478
6479 dwarf2_read_section (objfile, &dwarf2_per_objfile->ranges);
6480 if (offset >= dwarf2_per_objfile->ranges.size)
6481 {
6482 complaint (&symfile_complaints,
6483 _("Offset %d out of bounds for DW_AT_ranges attribute"),
6484 offset);
6485 return 0;
6486 }
6487 buffer = dwarf2_per_objfile->ranges.buffer + offset;
6488
6489 /* Read in the largest possible address. */
6490 marker = read_address (obfd, buffer, cu, &dummy);
6491 if ((marker & mask) == mask)
6492 {
6493 /* If we found the largest possible address, then
6494 read the base address. */
6495 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6496 buffer += 2 * addr_size;
6497 offset += 2 * addr_size;
6498 found_base = 1;
6499 }
6500
6501 low_set = 0;
6502
6503 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
6504
6505 while (1)
6506 {
6507 CORE_ADDR range_beginning, range_end;
6508
6509 range_beginning = read_address (obfd, buffer, cu, &dummy);
6510 buffer += addr_size;
6511 range_end = read_address (obfd, buffer, cu, &dummy);
6512 buffer += addr_size;
6513 offset += 2 * addr_size;
6514
6515 /* An end of list marker is a pair of zero addresses. */
6516 if (range_beginning == 0 && range_end == 0)
6517 /* Found the end of list entry. */
6518 break;
6519
6520 /* Each base address selection entry is a pair of 2 values.
6521 The first is the largest possible address, the second is
6522 the base address. Check for a base address here. */
6523 if ((range_beginning & mask) == mask)
6524 {
6525 /* If we found the largest possible address, then
6526 read the base address. */
6527 base = read_address (obfd, buffer + addr_size, cu, &dummy);
6528 found_base = 1;
6529 continue;
6530 }
6531
6532 if (!found_base)
6533 {
6534 /* We have no valid base address for the ranges
6535 data. */
6536 complaint (&symfile_complaints,
6537 _("Invalid .debug_ranges data (no base address)"));
6538 return 0;
6539 }
6540
6541 if (range_beginning > range_end)
6542 {
6543 /* Inverted range entries are invalid. */
6544 complaint (&symfile_complaints,
6545 _("Invalid .debug_ranges data (inverted range)"));
6546 return 0;
6547 }
6548
6549 /* Empty range entries have no effect. */
6550 if (range_beginning == range_end)
6551 continue;
6552
6553 range_beginning += base;
6554 range_end += base;
6555
6556 if (ranges_pst != NULL)
6557 addrmap_set_empty (objfile->psymtabs_addrmap,
6558 range_beginning + baseaddr,
6559 range_end - 1 + baseaddr,
6560 ranges_pst);
6561
6562 /* FIXME: This is recording everything as a low-high
6563 segment of consecutive addresses. We should have a
6564 data structure for discontiguous block ranges
6565 instead. */
6566 if (! low_set)
6567 {
6568 low = range_beginning;
6569 high = range_end;
6570 low_set = 1;
6571 }
6572 else
6573 {
6574 if (range_beginning < low)
6575 low = range_beginning;
6576 if (range_end > high)
6577 high = range_end;
6578 }
6579 }
6580
6581 if (! low_set)
6582 /* If the first entry is an end-of-list marker, the range
6583 describes an empty scope, i.e. no instructions. */
6584 return 0;
6585
6586 if (low_return)
6587 *low_return = low;
6588 if (high_return)
6589 *high_return = high;
6590 return 1;
6591 }
6592
6593 /* Get low and high pc attributes from a die. Return 1 if the attributes
6594 are present and valid, otherwise, return 0. Return -1 if the range is
6595 discontinuous, i.e. derived from DW_AT_ranges information. */
6596 static int
6597 dwarf2_get_pc_bounds (struct die_info *die, CORE_ADDR *lowpc,
6598 CORE_ADDR *highpc, struct dwarf2_cu *cu,
6599 struct partial_symtab *pst)
6600 {
6601 struct attribute *attr;
6602 CORE_ADDR low = 0;
6603 CORE_ADDR high = 0;
6604 int ret = 0;
6605
6606 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6607 if (attr)
6608 {
6609 high = DW_ADDR (attr);
6610 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6611 if (attr)
6612 low = DW_ADDR (attr);
6613 else
6614 /* Found high w/o low attribute. */
6615 return 0;
6616
6617 /* Found consecutive range of addresses. */
6618 ret = 1;
6619 }
6620 else
6621 {
6622 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6623 if (attr != NULL)
6624 {
6625 /* Value of the DW_AT_ranges attribute is the offset in the
6626 .debug_ranges section. */
6627 if (!dwarf2_ranges_read (DW_UNSND (attr), &low, &high, cu, pst))
6628 return 0;
6629 /* Found discontinuous range of addresses. */
6630 ret = -1;
6631 }
6632 }
6633
6634 /* read_partial_die has also the strict LOW < HIGH requirement. */
6635 if (high <= low)
6636 return 0;
6637
6638 /* When using the GNU linker, .gnu.linkonce. sections are used to
6639 eliminate duplicate copies of functions and vtables and such.
6640 The linker will arbitrarily choose one and discard the others.
6641 The AT_*_pc values for such functions refer to local labels in
6642 these sections. If the section from that file was discarded, the
6643 labels are not in the output, so the relocs get a value of 0.
6644 If this is a discarded function, mark the pc bounds as invalid,
6645 so that GDB will ignore it. */
6646 if (low == 0 && !dwarf2_per_objfile->has_section_at_zero)
6647 return 0;
6648
6649 *lowpc = low;
6650 if (highpc)
6651 *highpc = high;
6652 return ret;
6653 }
6654
6655 /* Assuming that DIE represents a subprogram DIE or a lexical block, get
6656 its low and high PC addresses. Do nothing if these addresses could not
6657 be determined. Otherwise, set LOWPC to the low address if it is smaller,
6658 and HIGHPC to the high address if greater than HIGHPC. */
6659
6660 static void
6661 dwarf2_get_subprogram_pc_bounds (struct die_info *die,
6662 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6663 struct dwarf2_cu *cu)
6664 {
6665 CORE_ADDR low, high;
6666 struct die_info *child = die->child;
6667
6668 if (dwarf2_get_pc_bounds (die, &low, &high, cu, NULL))
6669 {
6670 *lowpc = min (*lowpc, low);
6671 *highpc = max (*highpc, high);
6672 }
6673
6674 /* If the language does not allow nested subprograms (either inside
6675 subprograms or lexical blocks), we're done. */
6676 if (cu->language != language_ada)
6677 return;
6678
6679 /* Check all the children of the given DIE. If it contains nested
6680 subprograms, then check their pc bounds. Likewise, we need to
6681 check lexical blocks as well, as they may also contain subprogram
6682 definitions. */
6683 while (child && child->tag)
6684 {
6685 if (child->tag == DW_TAG_subprogram
6686 || child->tag == DW_TAG_lexical_block)
6687 dwarf2_get_subprogram_pc_bounds (child, lowpc, highpc, cu);
6688 child = sibling_die (child);
6689 }
6690 }
6691
6692 /* Get the low and high pc's represented by the scope DIE, and store
6693 them in *LOWPC and *HIGHPC. If the correct values can't be
6694 determined, set *LOWPC to -1 and *HIGHPC to 0. */
6695
6696 static void
6697 get_scope_pc_bounds (struct die_info *die,
6698 CORE_ADDR *lowpc, CORE_ADDR *highpc,
6699 struct dwarf2_cu *cu)
6700 {
6701 CORE_ADDR best_low = (CORE_ADDR) -1;
6702 CORE_ADDR best_high = (CORE_ADDR) 0;
6703 CORE_ADDR current_low, current_high;
6704
6705 if (dwarf2_get_pc_bounds (die, &current_low, &current_high, cu, NULL))
6706 {
6707 best_low = current_low;
6708 best_high = current_high;
6709 }
6710 else
6711 {
6712 struct die_info *child = die->child;
6713
6714 while (child && child->tag)
6715 {
6716 switch (child->tag) {
6717 case DW_TAG_subprogram:
6718 dwarf2_get_subprogram_pc_bounds (child, &best_low, &best_high, cu);
6719 break;
6720 case DW_TAG_namespace:
6721 case DW_TAG_module:
6722 /* FIXME: carlton/2004-01-16: Should we do this for
6723 DW_TAG_class_type/DW_TAG_structure_type, too? I think
6724 that current GCC's always emit the DIEs corresponding
6725 to definitions of methods of classes as children of a
6726 DW_TAG_compile_unit or DW_TAG_namespace (as opposed to
6727 the DIEs giving the declarations, which could be
6728 anywhere). But I don't see any reason why the
6729 standards says that they have to be there. */
6730 get_scope_pc_bounds (child, &current_low, &current_high, cu);
6731
6732 if (current_low != ((CORE_ADDR) -1))
6733 {
6734 best_low = min (best_low, current_low);
6735 best_high = max (best_high, current_high);
6736 }
6737 break;
6738 default:
6739 /* Ignore. */
6740 break;
6741 }
6742
6743 child = sibling_die (child);
6744 }
6745 }
6746
6747 *lowpc = best_low;
6748 *highpc = best_high;
6749 }
6750
6751 /* Record the address ranges for BLOCK, offset by BASEADDR, as given
6752 in DIE. */
6753 static void
6754 dwarf2_record_block_ranges (struct die_info *die, struct block *block,
6755 CORE_ADDR baseaddr, struct dwarf2_cu *cu)
6756 {
6757 struct objfile *objfile = cu->objfile;
6758 struct attribute *attr;
6759
6760 attr = dwarf2_attr (die, DW_AT_high_pc, cu);
6761 if (attr)
6762 {
6763 CORE_ADDR high = DW_ADDR (attr);
6764
6765 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
6766 if (attr)
6767 {
6768 CORE_ADDR low = DW_ADDR (attr);
6769
6770 record_block_range (block, baseaddr + low, baseaddr + high - 1);
6771 }
6772 }
6773
6774 attr = dwarf2_attr (die, DW_AT_ranges, cu);
6775 if (attr)
6776 {
6777 bfd *obfd = objfile->obfd;
6778
6779 /* The value of the DW_AT_ranges attribute is the offset of the
6780 address range list in the .debug_ranges section. */
6781 unsigned long offset = DW_UNSND (attr);
6782 gdb_byte *buffer = dwarf2_per_objfile->ranges.buffer + offset;
6783
6784 /* For some target architectures, but not others, the
6785 read_address function sign-extends the addresses it returns.
6786 To recognize base address selection entries, we need a
6787 mask. */
6788 unsigned int addr_size = cu->header.addr_size;
6789 CORE_ADDR base_select_mask = ~(~(CORE_ADDR)1 << (addr_size * 8 - 1));
6790
6791 /* The base address, to which the next pair is relative. Note
6792 that this 'base' is a DWARF concept: most entries in a range
6793 list are relative, to reduce the number of relocs against the
6794 debugging information. This is separate from this function's
6795 'baseaddr' argument, which GDB uses to relocate debugging
6796 information from a shared library based on the address at
6797 which the library was loaded. */
6798 CORE_ADDR base = cu->base_address;
6799 int base_known = cu->base_known;
6800
6801 gdb_assert (dwarf2_per_objfile->ranges.readin);
6802 if (offset >= dwarf2_per_objfile->ranges.size)
6803 {
6804 complaint (&symfile_complaints,
6805 _("Offset %lu out of bounds for DW_AT_ranges attribute"),
6806 offset);
6807 return;
6808 }
6809
6810 for (;;)
6811 {
6812 unsigned int bytes_read;
6813 CORE_ADDR start, end;
6814
6815 start = read_address (obfd, buffer, cu, &bytes_read);
6816 buffer += bytes_read;
6817 end = read_address (obfd, buffer, cu, &bytes_read);
6818 buffer += bytes_read;
6819
6820 /* Did we find the end of the range list? */
6821 if (start == 0 && end == 0)
6822 break;
6823
6824 /* Did we find a base address selection entry? */
6825 else if ((start & base_select_mask) == base_select_mask)
6826 {
6827 base = end;
6828 base_known = 1;
6829 }
6830
6831 /* We found an ordinary address range. */
6832 else
6833 {
6834 if (!base_known)
6835 {
6836 complaint (&symfile_complaints,
6837 _("Invalid .debug_ranges data "
6838 "(no base address)"));
6839 return;
6840 }
6841
6842 if (start > end)
6843 {
6844 /* Inverted range entries are invalid. */
6845 complaint (&symfile_complaints,
6846 _("Invalid .debug_ranges data "
6847 "(inverted range)"));
6848 return;
6849 }
6850
6851 /* Empty range entries have no effect. */
6852 if (start == end)
6853 continue;
6854
6855 record_block_range (block,
6856 baseaddr + base + start,
6857 baseaddr + base + end - 1);
6858 }
6859 }
6860 }
6861 }
6862
6863 /* Check for GCC PR debug/45124 fix which is not present in any G++ version up
6864 to 4.5.any while it is present already in G++ 4.6.0 - the PR has been fixed
6865 during 4.6.0 experimental. */
6866
6867 static int
6868 producer_is_gxx_lt_4_6 (struct dwarf2_cu *cu)
6869 {
6870 const char *cs;
6871 int major, minor, release;
6872 int result = 0;
6873
6874 if (cu->producer == NULL)
6875 {
6876 /* For unknown compilers expect their behavior is DWARF version
6877 compliant.
6878
6879 GCC started to support .debug_types sections by -gdwarf-4 since
6880 gcc-4.5.x. As the .debug_types sections are missing DW_AT_producer
6881 for their space efficiency GDB cannot workaround gcc-4.5.x -gdwarf-4
6882 combination. gcc-4.5.x -gdwarf-4 binaries have DW_AT_accessibility
6883 interpreted incorrectly by GDB now - GCC PR debug/48229. */
6884
6885 return 0;
6886 }
6887
6888 if (cu->checked_producer)
6889 return cu->producer_is_gxx_lt_4_6;
6890
6891 /* Skip any identifier after "GNU " - such as "C++" or "Java". */
6892
6893 if (strncmp (cu->producer, "GNU ", strlen ("GNU ")) != 0)
6894 {
6895 /* For non-GCC compilers expect their behavior is DWARF version
6896 compliant. */
6897 }
6898 else
6899 {
6900 cs = &cu->producer[strlen ("GNU ")];
6901 while (*cs && !isdigit (*cs))
6902 cs++;
6903 if (sscanf (cs, "%d.%d.%d", &major, &minor, &release) != 3)
6904 {
6905 /* Not recognized as GCC. */
6906 }
6907 else
6908 result = major < 4 || (major == 4 && minor < 6);
6909 }
6910
6911 cu->checked_producer = 1;
6912 cu->producer_is_gxx_lt_4_6 = result;
6913
6914 return result;
6915 }
6916
6917 /* Return the default accessibility type if it is not overriden by
6918 DW_AT_accessibility. */
6919
6920 static enum dwarf_access_attribute
6921 dwarf2_default_access_attribute (struct die_info *die, struct dwarf2_cu *cu)
6922 {
6923 if (cu->header.version < 3 || producer_is_gxx_lt_4_6 (cu))
6924 {
6925 /* The default DWARF 2 accessibility for members is public, the default
6926 accessibility for inheritance is private. */
6927
6928 if (die->tag != DW_TAG_inheritance)
6929 return DW_ACCESS_public;
6930 else
6931 return DW_ACCESS_private;
6932 }
6933 else
6934 {
6935 /* DWARF 3+ defines the default accessibility a different way. The same
6936 rules apply now for DW_TAG_inheritance as for the members and it only
6937 depends on the container kind. */
6938
6939 if (die->parent->tag == DW_TAG_class_type)
6940 return DW_ACCESS_private;
6941 else
6942 return DW_ACCESS_public;
6943 }
6944 }
6945
6946 /* Look for DW_AT_data_member_location. Set *OFFSET to the byte
6947 offset. If the attribute was not found return 0, otherwise return
6948 1. If it was found but could not properly be handled, set *OFFSET
6949 to 0. */
6950
6951 static int
6952 handle_data_member_location (struct die_info *die, struct dwarf2_cu *cu,
6953 LONGEST *offset)
6954 {
6955 struct attribute *attr;
6956
6957 attr = dwarf2_attr (die, DW_AT_data_member_location, cu);
6958 if (attr != NULL)
6959 {
6960 *offset = 0;
6961
6962 /* Note that we do not check for a section offset first here.
6963 This is because DW_AT_data_member_location is new in DWARF 4,
6964 so if we see it, we can assume that a constant form is really
6965 a constant and not a section offset. */
6966 if (attr_form_is_constant (attr))
6967 *offset = dwarf2_get_attr_constant_value (attr, 0);
6968 else if (attr_form_is_section_offset (attr))
6969 dwarf2_complex_location_expr_complaint ();
6970 else if (attr_form_is_block (attr))
6971 *offset = decode_locdesc (DW_BLOCK (attr), cu);
6972 else
6973 dwarf2_complex_location_expr_complaint ();
6974
6975 return 1;
6976 }
6977
6978 return 0;
6979 }
6980
6981 /* Add an aggregate field to the field list. */
6982
6983 static void
6984 dwarf2_add_field (struct field_info *fip, struct die_info *die,
6985 struct dwarf2_cu *cu)
6986 {
6987 struct objfile *objfile = cu->objfile;
6988 struct gdbarch *gdbarch = get_objfile_arch (objfile);
6989 struct nextfield *new_field;
6990 struct attribute *attr;
6991 struct field *fp;
6992 char *fieldname = "";
6993
6994 /* Allocate a new field list entry and link it in. */
6995 new_field = (struct nextfield *) xmalloc (sizeof (struct nextfield));
6996 make_cleanup (xfree, new_field);
6997 memset (new_field, 0, sizeof (struct nextfield));
6998
6999 if (die->tag == DW_TAG_inheritance)
7000 {
7001 new_field->next = fip->baseclasses;
7002 fip->baseclasses = new_field;
7003 }
7004 else
7005 {
7006 new_field->next = fip->fields;
7007 fip->fields = new_field;
7008 }
7009 fip->nfields++;
7010
7011 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7012 if (attr)
7013 new_field->accessibility = DW_UNSND (attr);
7014 else
7015 new_field->accessibility = dwarf2_default_access_attribute (die, cu);
7016 if (new_field->accessibility != DW_ACCESS_public)
7017 fip->non_public_fields = 1;
7018
7019 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7020 if (attr)
7021 new_field->virtuality = DW_UNSND (attr);
7022 else
7023 new_field->virtuality = DW_VIRTUALITY_none;
7024
7025 fp = &new_field->field;
7026
7027 if (die->tag == DW_TAG_member && ! die_is_declaration (die, cu))
7028 {
7029 LONGEST offset;
7030
7031 /* Data member other than a C++ static data member. */
7032
7033 /* Get type of field. */
7034 fp->type = die_type (die, cu);
7035
7036 SET_FIELD_BITPOS (*fp, 0);
7037
7038 /* Get bit size of field (zero if none). */
7039 attr = dwarf2_attr (die, DW_AT_bit_size, cu);
7040 if (attr)
7041 {
7042 FIELD_BITSIZE (*fp) = DW_UNSND (attr);
7043 }
7044 else
7045 {
7046 FIELD_BITSIZE (*fp) = 0;
7047 }
7048
7049 /* Get bit offset of field. */
7050 if (handle_data_member_location (die, cu, &offset))
7051 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7052 attr = dwarf2_attr (die, DW_AT_bit_offset, cu);
7053 if (attr)
7054 {
7055 if (gdbarch_bits_big_endian (gdbarch))
7056 {
7057 /* For big endian bits, the DW_AT_bit_offset gives the
7058 additional bit offset from the MSB of the containing
7059 anonymous object to the MSB of the field. We don't
7060 have to do anything special since we don't need to
7061 know the size of the anonymous object. */
7062 FIELD_BITPOS (*fp) += DW_UNSND (attr);
7063 }
7064 else
7065 {
7066 /* For little endian bits, compute the bit offset to the
7067 MSB of the anonymous object, subtract off the number of
7068 bits from the MSB of the field to the MSB of the
7069 object, and then subtract off the number of bits of
7070 the field itself. The result is the bit offset of
7071 the LSB of the field. */
7072 int anonymous_size;
7073 int bit_offset = DW_UNSND (attr);
7074
7075 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7076 if (attr)
7077 {
7078 /* The size of the anonymous object containing
7079 the bit field is explicit, so use the
7080 indicated size (in bytes). */
7081 anonymous_size = DW_UNSND (attr);
7082 }
7083 else
7084 {
7085 /* The size of the anonymous object containing
7086 the bit field must be inferred from the type
7087 attribute of the data member containing the
7088 bit field. */
7089 anonymous_size = TYPE_LENGTH (fp->type);
7090 }
7091 FIELD_BITPOS (*fp) += anonymous_size * bits_per_byte
7092 - bit_offset - FIELD_BITSIZE (*fp);
7093 }
7094 }
7095
7096 /* Get name of field. */
7097 fieldname = dwarf2_name (die, cu);
7098 if (fieldname == NULL)
7099 fieldname = "";
7100
7101 /* The name is already allocated along with this objfile, so we don't
7102 need to duplicate it for the type. */
7103 fp->name = fieldname;
7104
7105 /* Change accessibility for artificial fields (e.g. virtual table
7106 pointer or virtual base class pointer) to private. */
7107 if (dwarf2_attr (die, DW_AT_artificial, cu))
7108 {
7109 FIELD_ARTIFICIAL (*fp) = 1;
7110 new_field->accessibility = DW_ACCESS_private;
7111 fip->non_public_fields = 1;
7112 }
7113 }
7114 else if (die->tag == DW_TAG_member || die->tag == DW_TAG_variable)
7115 {
7116 /* C++ static member. */
7117
7118 /* NOTE: carlton/2002-11-05: It should be a DW_TAG_member that
7119 is a declaration, but all versions of G++ as of this writing
7120 (so through at least 3.2.1) incorrectly generate
7121 DW_TAG_variable tags. */
7122
7123 const char *physname;
7124
7125 /* Get name of field. */
7126 fieldname = dwarf2_name (die, cu);
7127 if (fieldname == NULL)
7128 return;
7129
7130 attr = dwarf2_attr (die, DW_AT_const_value, cu);
7131 if (attr
7132 /* Only create a symbol if this is an external value.
7133 new_symbol checks this and puts the value in the global symbol
7134 table, which we want. If it is not external, new_symbol
7135 will try to put the value in cu->list_in_scope which is wrong. */
7136 && dwarf2_flag_true_p (die, DW_AT_external, cu))
7137 {
7138 /* A static const member, not much different than an enum as far as
7139 we're concerned, except that we can support more types. */
7140 new_symbol (die, NULL, cu);
7141 }
7142
7143 /* Get physical name. */
7144 physname = dwarf2_physname (fieldname, die, cu);
7145
7146 /* The name is already allocated along with this objfile, so we don't
7147 need to duplicate it for the type. */
7148 SET_FIELD_PHYSNAME (*fp, physname ? physname : "");
7149 FIELD_TYPE (*fp) = die_type (die, cu);
7150 FIELD_NAME (*fp) = fieldname;
7151 }
7152 else if (die->tag == DW_TAG_inheritance)
7153 {
7154 LONGEST offset;
7155
7156 /* C++ base class field. */
7157 if (handle_data_member_location (die, cu, &offset))
7158 SET_FIELD_BITPOS (*fp, offset * bits_per_byte);
7159 FIELD_BITSIZE (*fp) = 0;
7160 FIELD_TYPE (*fp) = die_type (die, cu);
7161 FIELD_NAME (*fp) = type_name_no_tag (fp->type);
7162 fip->nbaseclasses++;
7163 }
7164 }
7165
7166 /* Add a typedef defined in the scope of the FIP's class. */
7167
7168 static void
7169 dwarf2_add_typedef (struct field_info *fip, struct die_info *die,
7170 struct dwarf2_cu *cu)
7171 {
7172 struct objfile *objfile = cu->objfile;
7173 struct typedef_field_list *new_field;
7174 struct attribute *attr;
7175 struct typedef_field *fp;
7176 char *fieldname = "";
7177
7178 /* Allocate a new field list entry and link it in. */
7179 new_field = xzalloc (sizeof (*new_field));
7180 make_cleanup (xfree, new_field);
7181
7182 gdb_assert (die->tag == DW_TAG_typedef);
7183
7184 fp = &new_field->field;
7185
7186 /* Get name of field. */
7187 fp->name = dwarf2_name (die, cu);
7188 if (fp->name == NULL)
7189 return;
7190
7191 fp->type = read_type_die (die, cu);
7192
7193 new_field->next = fip->typedef_field_list;
7194 fip->typedef_field_list = new_field;
7195 fip->typedef_field_list_count++;
7196 }
7197
7198 /* Create the vector of fields, and attach it to the type. */
7199
7200 static void
7201 dwarf2_attach_fields_to_type (struct field_info *fip, struct type *type,
7202 struct dwarf2_cu *cu)
7203 {
7204 int nfields = fip->nfields;
7205
7206 /* Record the field count, allocate space for the array of fields,
7207 and create blank accessibility bitfields if necessary. */
7208 TYPE_NFIELDS (type) = nfields;
7209 TYPE_FIELDS (type) = (struct field *)
7210 TYPE_ALLOC (type, sizeof (struct field) * nfields);
7211 memset (TYPE_FIELDS (type), 0, sizeof (struct field) * nfields);
7212
7213 if (fip->non_public_fields && cu->language != language_ada)
7214 {
7215 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7216
7217 TYPE_FIELD_PRIVATE_BITS (type) =
7218 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7219 B_CLRALL (TYPE_FIELD_PRIVATE_BITS (type), nfields);
7220
7221 TYPE_FIELD_PROTECTED_BITS (type) =
7222 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7223 B_CLRALL (TYPE_FIELD_PROTECTED_BITS (type), nfields);
7224
7225 TYPE_FIELD_IGNORE_BITS (type) =
7226 (B_TYPE *) TYPE_ALLOC (type, B_BYTES (nfields));
7227 B_CLRALL (TYPE_FIELD_IGNORE_BITS (type), nfields);
7228 }
7229
7230 /* If the type has baseclasses, allocate and clear a bit vector for
7231 TYPE_FIELD_VIRTUAL_BITS. */
7232 if (fip->nbaseclasses && cu->language != language_ada)
7233 {
7234 int num_bytes = B_BYTES (fip->nbaseclasses);
7235 unsigned char *pointer;
7236
7237 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7238 pointer = TYPE_ALLOC (type, num_bytes);
7239 TYPE_FIELD_VIRTUAL_BITS (type) = pointer;
7240 B_CLRALL (TYPE_FIELD_VIRTUAL_BITS (type), fip->nbaseclasses);
7241 TYPE_N_BASECLASSES (type) = fip->nbaseclasses;
7242 }
7243
7244 /* Copy the saved-up fields into the field vector. Start from the head of
7245 the list, adding to the tail of the field array, so that they end up in
7246 the same order in the array in which they were added to the list. */
7247 while (nfields-- > 0)
7248 {
7249 struct nextfield *fieldp;
7250
7251 if (fip->fields)
7252 {
7253 fieldp = fip->fields;
7254 fip->fields = fieldp->next;
7255 }
7256 else
7257 {
7258 fieldp = fip->baseclasses;
7259 fip->baseclasses = fieldp->next;
7260 }
7261
7262 TYPE_FIELD (type, nfields) = fieldp->field;
7263 switch (fieldp->accessibility)
7264 {
7265 case DW_ACCESS_private:
7266 if (cu->language != language_ada)
7267 SET_TYPE_FIELD_PRIVATE (type, nfields);
7268 break;
7269
7270 case DW_ACCESS_protected:
7271 if (cu->language != language_ada)
7272 SET_TYPE_FIELD_PROTECTED (type, nfields);
7273 break;
7274
7275 case DW_ACCESS_public:
7276 break;
7277
7278 default:
7279 /* Unknown accessibility. Complain and treat it as public. */
7280 {
7281 complaint (&symfile_complaints, _("unsupported accessibility %d"),
7282 fieldp->accessibility);
7283 }
7284 break;
7285 }
7286 if (nfields < fip->nbaseclasses)
7287 {
7288 switch (fieldp->virtuality)
7289 {
7290 case DW_VIRTUALITY_virtual:
7291 case DW_VIRTUALITY_pure_virtual:
7292 if (cu->language == language_ada)
7293 error (_("unexpected virtuality in component of Ada type"));
7294 SET_TYPE_FIELD_VIRTUAL (type, nfields);
7295 break;
7296 }
7297 }
7298 }
7299 }
7300
7301 /* Add a member function to the proper fieldlist. */
7302
7303 static void
7304 dwarf2_add_member_fn (struct field_info *fip, struct die_info *die,
7305 struct type *type, struct dwarf2_cu *cu)
7306 {
7307 struct objfile *objfile = cu->objfile;
7308 struct attribute *attr;
7309 struct fnfieldlist *flp;
7310 int i;
7311 struct fn_field *fnp;
7312 char *fieldname;
7313 struct nextfnfield *new_fnfield;
7314 struct type *this_type;
7315 enum dwarf_access_attribute accessibility;
7316
7317 if (cu->language == language_ada)
7318 error (_("unexpected member function in Ada type"));
7319
7320 /* Get name of member function. */
7321 fieldname = dwarf2_name (die, cu);
7322 if (fieldname == NULL)
7323 return;
7324
7325 /* Look up member function name in fieldlist. */
7326 for (i = 0; i < fip->nfnfields; i++)
7327 {
7328 if (strcmp (fip->fnfieldlists[i].name, fieldname) == 0)
7329 break;
7330 }
7331
7332 /* Create new list element if necessary. */
7333 if (i < fip->nfnfields)
7334 flp = &fip->fnfieldlists[i];
7335 else
7336 {
7337 if ((fip->nfnfields % DW_FIELD_ALLOC_CHUNK) == 0)
7338 {
7339 fip->fnfieldlists = (struct fnfieldlist *)
7340 xrealloc (fip->fnfieldlists,
7341 (fip->nfnfields + DW_FIELD_ALLOC_CHUNK)
7342 * sizeof (struct fnfieldlist));
7343 if (fip->nfnfields == 0)
7344 make_cleanup (free_current_contents, &fip->fnfieldlists);
7345 }
7346 flp = &fip->fnfieldlists[fip->nfnfields];
7347 flp->name = fieldname;
7348 flp->length = 0;
7349 flp->head = NULL;
7350 i = fip->nfnfields++;
7351 }
7352
7353 /* Create a new member function field and chain it to the field list
7354 entry. */
7355 new_fnfield = (struct nextfnfield *) xmalloc (sizeof (struct nextfnfield));
7356 make_cleanup (xfree, new_fnfield);
7357 memset (new_fnfield, 0, sizeof (struct nextfnfield));
7358 new_fnfield->next = flp->head;
7359 flp->head = new_fnfield;
7360 flp->length++;
7361
7362 /* Fill in the member function field info. */
7363 fnp = &new_fnfield->fnfield;
7364
7365 /* Delay processing of the physname until later. */
7366 if (cu->language == language_cplus || cu->language == language_java)
7367 {
7368 add_to_method_list (type, i, flp->length - 1, fieldname,
7369 die, cu);
7370 }
7371 else
7372 {
7373 const char *physname = dwarf2_physname (fieldname, die, cu);
7374 fnp->physname = physname ? physname : "";
7375 }
7376
7377 fnp->type = alloc_type (objfile);
7378 this_type = read_type_die (die, cu);
7379 if (this_type && TYPE_CODE (this_type) == TYPE_CODE_FUNC)
7380 {
7381 int nparams = TYPE_NFIELDS (this_type);
7382
7383 /* TYPE is the domain of this method, and THIS_TYPE is the type
7384 of the method itself (TYPE_CODE_METHOD). */
7385 smash_to_method_type (fnp->type, type,
7386 TYPE_TARGET_TYPE (this_type),
7387 TYPE_FIELDS (this_type),
7388 TYPE_NFIELDS (this_type),
7389 TYPE_VARARGS (this_type));
7390
7391 /* Handle static member functions.
7392 Dwarf2 has no clean way to discern C++ static and non-static
7393 member functions. G++ helps GDB by marking the first
7394 parameter for non-static member functions (which is the this
7395 pointer) as artificial. We obtain this information from
7396 read_subroutine_type via TYPE_FIELD_ARTIFICIAL. */
7397 if (nparams == 0 || TYPE_FIELD_ARTIFICIAL (this_type, 0) == 0)
7398 fnp->voffset = VOFFSET_STATIC;
7399 }
7400 else
7401 complaint (&symfile_complaints, _("member function type missing for '%s'"),
7402 dwarf2_full_name (fieldname, die, cu));
7403
7404 /* Get fcontext from DW_AT_containing_type if present. */
7405 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7406 fnp->fcontext = die_containing_type (die, cu);
7407
7408 /* dwarf2 doesn't have stubbed physical names, so the setting of is_const and
7409 is_volatile is irrelevant, as it is needed by gdb_mangle_name only. */
7410
7411 /* Get accessibility. */
7412 attr = dwarf2_attr (die, DW_AT_accessibility, cu);
7413 if (attr)
7414 accessibility = DW_UNSND (attr);
7415 else
7416 accessibility = dwarf2_default_access_attribute (die, cu);
7417 switch (accessibility)
7418 {
7419 case DW_ACCESS_private:
7420 fnp->is_private = 1;
7421 break;
7422 case DW_ACCESS_protected:
7423 fnp->is_protected = 1;
7424 break;
7425 }
7426
7427 /* Check for artificial methods. */
7428 attr = dwarf2_attr (die, DW_AT_artificial, cu);
7429 if (attr && DW_UNSND (attr) != 0)
7430 fnp->is_artificial = 1;
7431
7432 /* Get index in virtual function table if it is a virtual member
7433 function. For older versions of GCC, this is an offset in the
7434 appropriate virtual table, as specified by DW_AT_containing_type.
7435 For everyone else, it is an expression to be evaluated relative
7436 to the object address. */
7437
7438 attr = dwarf2_attr (die, DW_AT_vtable_elem_location, cu);
7439 if (attr)
7440 {
7441 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size > 0)
7442 {
7443 if (DW_BLOCK (attr)->data[0] == DW_OP_constu)
7444 {
7445 /* Old-style GCC. */
7446 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu) + 2;
7447 }
7448 else if (DW_BLOCK (attr)->data[0] == DW_OP_deref
7449 || (DW_BLOCK (attr)->size > 1
7450 && DW_BLOCK (attr)->data[0] == DW_OP_deref_size
7451 && DW_BLOCK (attr)->data[1] == cu->header.addr_size))
7452 {
7453 struct dwarf_block blk;
7454 int offset;
7455
7456 offset = (DW_BLOCK (attr)->data[0] == DW_OP_deref
7457 ? 1 : 2);
7458 blk.size = DW_BLOCK (attr)->size - offset;
7459 blk.data = DW_BLOCK (attr)->data + offset;
7460 fnp->voffset = decode_locdesc (DW_BLOCK (attr), cu);
7461 if ((fnp->voffset % cu->header.addr_size) != 0)
7462 dwarf2_complex_location_expr_complaint ();
7463 else
7464 fnp->voffset /= cu->header.addr_size;
7465 fnp->voffset += 2;
7466 }
7467 else
7468 dwarf2_complex_location_expr_complaint ();
7469
7470 if (!fnp->fcontext)
7471 fnp->fcontext = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (this_type, 0));
7472 }
7473 else if (attr_form_is_section_offset (attr))
7474 {
7475 dwarf2_complex_location_expr_complaint ();
7476 }
7477 else
7478 {
7479 dwarf2_invalid_attrib_class_complaint ("DW_AT_vtable_elem_location",
7480 fieldname);
7481 }
7482 }
7483 else
7484 {
7485 attr = dwarf2_attr (die, DW_AT_virtuality, cu);
7486 if (attr && DW_UNSND (attr))
7487 {
7488 /* GCC does this, as of 2008-08-25; PR debug/37237. */
7489 complaint (&symfile_complaints,
7490 _("Member function \"%s\" (offset %d) is virtual "
7491 "but the vtable offset is not specified"),
7492 fieldname, die->offset);
7493 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7494 TYPE_CPLUS_DYNAMIC (type) = 1;
7495 }
7496 }
7497 }
7498
7499 /* Create the vector of member function fields, and attach it to the type. */
7500
7501 static void
7502 dwarf2_attach_fn_fields_to_type (struct field_info *fip, struct type *type,
7503 struct dwarf2_cu *cu)
7504 {
7505 struct fnfieldlist *flp;
7506 int i;
7507
7508 if (cu->language == language_ada)
7509 error (_("unexpected member functions in Ada type"));
7510
7511 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7512 TYPE_FN_FIELDLISTS (type) = (struct fn_fieldlist *)
7513 TYPE_ALLOC (type, sizeof (struct fn_fieldlist) * fip->nfnfields);
7514
7515 for (i = 0, flp = fip->fnfieldlists; i < fip->nfnfields; i++, flp++)
7516 {
7517 struct nextfnfield *nfp = flp->head;
7518 struct fn_fieldlist *fn_flp = &TYPE_FN_FIELDLIST (type, i);
7519 int k;
7520
7521 TYPE_FN_FIELDLIST_NAME (type, i) = flp->name;
7522 TYPE_FN_FIELDLIST_LENGTH (type, i) = flp->length;
7523 fn_flp->fn_fields = (struct fn_field *)
7524 TYPE_ALLOC (type, sizeof (struct fn_field) * flp->length);
7525 for (k = flp->length; (k--, nfp); nfp = nfp->next)
7526 fn_flp->fn_fields[k] = nfp->fnfield;
7527 }
7528
7529 TYPE_NFN_FIELDS (type) = fip->nfnfields;
7530 }
7531
7532 /* Returns non-zero if NAME is the name of a vtable member in CU's
7533 language, zero otherwise. */
7534 static int
7535 is_vtable_name (const char *name, struct dwarf2_cu *cu)
7536 {
7537 static const char vptr[] = "_vptr";
7538 static const char vtable[] = "vtable";
7539
7540 /* Look for the C++ and Java forms of the vtable. */
7541 if ((cu->language == language_java
7542 && strncmp (name, vtable, sizeof (vtable) - 1) == 0)
7543 || (strncmp (name, vptr, sizeof (vptr) - 1) == 0
7544 && is_cplus_marker (name[sizeof (vptr) - 1])))
7545 return 1;
7546
7547 return 0;
7548 }
7549
7550 /* GCC outputs unnamed structures that are really pointers to member
7551 functions, with the ABI-specified layout. If TYPE describes
7552 such a structure, smash it into a member function type.
7553
7554 GCC shouldn't do this; it should just output pointer to member DIEs.
7555 This is GCC PR debug/28767. */
7556
7557 static void
7558 quirk_gcc_member_function_pointer (struct type *type, struct objfile *objfile)
7559 {
7560 struct type *pfn_type, *domain_type, *new_type;
7561
7562 /* Check for a structure with no name and two children. */
7563 if (TYPE_CODE (type) != TYPE_CODE_STRUCT || TYPE_NFIELDS (type) != 2)
7564 return;
7565
7566 /* Check for __pfn and __delta members. */
7567 if (TYPE_FIELD_NAME (type, 0) == NULL
7568 || strcmp (TYPE_FIELD_NAME (type, 0), "__pfn") != 0
7569 || TYPE_FIELD_NAME (type, 1) == NULL
7570 || strcmp (TYPE_FIELD_NAME (type, 1), "__delta") != 0)
7571 return;
7572
7573 /* Find the type of the method. */
7574 pfn_type = TYPE_FIELD_TYPE (type, 0);
7575 if (pfn_type == NULL
7576 || TYPE_CODE (pfn_type) != TYPE_CODE_PTR
7577 || TYPE_CODE (TYPE_TARGET_TYPE (pfn_type)) != TYPE_CODE_FUNC)
7578 return;
7579
7580 /* Look for the "this" argument. */
7581 pfn_type = TYPE_TARGET_TYPE (pfn_type);
7582 if (TYPE_NFIELDS (pfn_type) == 0
7583 /* || TYPE_FIELD_TYPE (pfn_type, 0) == NULL */
7584 || TYPE_CODE (TYPE_FIELD_TYPE (pfn_type, 0)) != TYPE_CODE_PTR)
7585 return;
7586
7587 domain_type = TYPE_TARGET_TYPE (TYPE_FIELD_TYPE (pfn_type, 0));
7588 new_type = alloc_type (objfile);
7589 smash_to_method_type (new_type, domain_type, TYPE_TARGET_TYPE (pfn_type),
7590 TYPE_FIELDS (pfn_type), TYPE_NFIELDS (pfn_type),
7591 TYPE_VARARGS (pfn_type));
7592 smash_to_methodptr_type (type, new_type);
7593 }
7594
7595 /* Called when we find the DIE that starts a structure or union scope
7596 (definition) to create a type for the structure or union. Fill in
7597 the type's name and general properties; the members will not be
7598 processed until process_structure_type.
7599
7600 NOTE: we need to call these functions regardless of whether or not the
7601 DIE has a DW_AT_name attribute, since it might be an anonymous
7602 structure or union. This gets the type entered into our set of
7603 user defined types.
7604
7605 However, if the structure is incomplete (an opaque struct/union)
7606 then suppress creating a symbol table entry for it since gdb only
7607 wants to find the one with the complete definition. Note that if
7608 it is complete, we just call new_symbol, which does it's own
7609 checking about whether the struct/union is anonymous or not (and
7610 suppresses creating a symbol table entry itself). */
7611
7612 static struct type *
7613 read_structure_type (struct die_info *die, struct dwarf2_cu *cu)
7614 {
7615 struct objfile *objfile = cu->objfile;
7616 struct type *type;
7617 struct attribute *attr;
7618 char *name;
7619
7620 /* If the definition of this type lives in .debug_types, read that type.
7621 Don't follow DW_AT_specification though, that will take us back up
7622 the chain and we want to go down. */
7623 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7624 if (attr)
7625 {
7626 struct dwarf2_cu *type_cu = cu;
7627 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7628
7629 /* We could just recurse on read_structure_type, but we need to call
7630 get_die_type to ensure only one type for this DIE is created.
7631 This is important, for example, because for c++ classes we need
7632 TYPE_NAME set which is only done by new_symbol. Blech. */
7633 type = read_type_die (type_die, type_cu);
7634
7635 /* TYPE_CU may not be the same as CU.
7636 Ensure TYPE is recorded in CU's type_hash table. */
7637 return set_die_type (die, type, cu);
7638 }
7639
7640 type = alloc_type (objfile);
7641 INIT_CPLUS_SPECIFIC (type);
7642
7643 name = dwarf2_name (die, cu);
7644 if (name != NULL)
7645 {
7646 if (cu->language == language_cplus
7647 || cu->language == language_java)
7648 {
7649 char *full_name = (char *) dwarf2_full_name (name, die, cu);
7650
7651 /* dwarf2_full_name might have already finished building the DIE's
7652 type. If so, there is no need to continue. */
7653 if (get_die_type (die, cu) != NULL)
7654 return get_die_type (die, cu);
7655
7656 TYPE_TAG_NAME (type) = full_name;
7657 if (die->tag == DW_TAG_structure_type
7658 || die->tag == DW_TAG_class_type)
7659 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7660 }
7661 else
7662 {
7663 /* The name is already allocated along with this objfile, so
7664 we don't need to duplicate it for the type. */
7665 TYPE_TAG_NAME (type) = (char *) name;
7666 if (die->tag == DW_TAG_class_type)
7667 TYPE_NAME (type) = TYPE_TAG_NAME (type);
7668 }
7669 }
7670
7671 if (die->tag == DW_TAG_structure_type)
7672 {
7673 TYPE_CODE (type) = TYPE_CODE_STRUCT;
7674 }
7675 else if (die->tag == DW_TAG_union_type)
7676 {
7677 TYPE_CODE (type) = TYPE_CODE_UNION;
7678 }
7679 else
7680 {
7681 TYPE_CODE (type) = TYPE_CODE_CLASS;
7682 }
7683
7684 if (cu->language == language_cplus && die->tag == DW_TAG_class_type)
7685 TYPE_DECLARED_CLASS (type) = 1;
7686
7687 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7688 if (attr)
7689 {
7690 TYPE_LENGTH (type) = DW_UNSND (attr);
7691 }
7692 else
7693 {
7694 TYPE_LENGTH (type) = 0;
7695 }
7696
7697 TYPE_STUB_SUPPORTED (type) = 1;
7698 if (die_is_declaration (die, cu))
7699 TYPE_STUB (type) = 1;
7700 else if (attr == NULL && die->child == NULL
7701 && producer_is_realview (cu->producer))
7702 /* RealView does not output the required DW_AT_declaration
7703 on incomplete types. */
7704 TYPE_STUB (type) = 1;
7705
7706 /* We need to add the type field to the die immediately so we don't
7707 infinitely recurse when dealing with pointers to the structure
7708 type within the structure itself. */
7709 set_die_type (die, type, cu);
7710
7711 /* set_die_type should be already done. */
7712 set_descriptive_type (type, die, cu);
7713
7714 return type;
7715 }
7716
7717 /* Finish creating a structure or union type, including filling in
7718 its members and creating a symbol for it. */
7719
7720 static void
7721 process_structure_scope (struct die_info *die, struct dwarf2_cu *cu)
7722 {
7723 struct objfile *objfile = cu->objfile;
7724 struct die_info *child_die = die->child;
7725 struct type *type;
7726
7727 type = get_die_type (die, cu);
7728 if (type == NULL)
7729 type = read_structure_type (die, cu);
7730
7731 if (die->child != NULL && ! die_is_declaration (die, cu))
7732 {
7733 struct field_info fi;
7734 struct die_info *child_die;
7735 VEC (symbolp) *template_args = NULL;
7736 struct cleanup *back_to = make_cleanup (null_cleanup, 0);
7737
7738 memset (&fi, 0, sizeof (struct field_info));
7739
7740 child_die = die->child;
7741
7742 while (child_die && child_die->tag)
7743 {
7744 if (child_die->tag == DW_TAG_member
7745 || child_die->tag == DW_TAG_variable)
7746 {
7747 /* NOTE: carlton/2002-11-05: A C++ static data member
7748 should be a DW_TAG_member that is a declaration, but
7749 all versions of G++ as of this writing (so through at
7750 least 3.2.1) incorrectly generate DW_TAG_variable
7751 tags for them instead. */
7752 dwarf2_add_field (&fi, child_die, cu);
7753 }
7754 else if (child_die->tag == DW_TAG_subprogram)
7755 {
7756 /* C++ member function. */
7757 dwarf2_add_member_fn (&fi, child_die, type, cu);
7758 }
7759 else if (child_die->tag == DW_TAG_inheritance)
7760 {
7761 /* C++ base class field. */
7762 dwarf2_add_field (&fi, child_die, cu);
7763 }
7764 else if (child_die->tag == DW_TAG_typedef)
7765 dwarf2_add_typedef (&fi, child_die, cu);
7766 else if (child_die->tag == DW_TAG_template_type_param
7767 || child_die->tag == DW_TAG_template_value_param)
7768 {
7769 struct symbol *arg = new_symbol (child_die, NULL, cu);
7770
7771 if (arg != NULL)
7772 VEC_safe_push (symbolp, template_args, arg);
7773 }
7774
7775 child_die = sibling_die (child_die);
7776 }
7777
7778 /* Attach template arguments to type. */
7779 if (! VEC_empty (symbolp, template_args))
7780 {
7781 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7782 TYPE_N_TEMPLATE_ARGUMENTS (type)
7783 = VEC_length (symbolp, template_args);
7784 TYPE_TEMPLATE_ARGUMENTS (type)
7785 = obstack_alloc (&objfile->objfile_obstack,
7786 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7787 * sizeof (struct symbol *)));
7788 memcpy (TYPE_TEMPLATE_ARGUMENTS (type),
7789 VEC_address (symbolp, template_args),
7790 (TYPE_N_TEMPLATE_ARGUMENTS (type)
7791 * sizeof (struct symbol *)));
7792 VEC_free (symbolp, template_args);
7793 }
7794
7795 /* Attach fields and member functions to the type. */
7796 if (fi.nfields)
7797 dwarf2_attach_fields_to_type (&fi, type, cu);
7798 if (fi.nfnfields)
7799 {
7800 dwarf2_attach_fn_fields_to_type (&fi, type, cu);
7801
7802 /* Get the type which refers to the base class (possibly this
7803 class itself) which contains the vtable pointer for the current
7804 class from the DW_AT_containing_type attribute. This use of
7805 DW_AT_containing_type is a GNU extension. */
7806
7807 if (dwarf2_attr (die, DW_AT_containing_type, cu) != NULL)
7808 {
7809 struct type *t = die_containing_type (die, cu);
7810
7811 TYPE_VPTR_BASETYPE (type) = t;
7812 if (type == t)
7813 {
7814 int i;
7815
7816 /* Our own class provides vtbl ptr. */
7817 for (i = TYPE_NFIELDS (t) - 1;
7818 i >= TYPE_N_BASECLASSES (t);
7819 --i)
7820 {
7821 const char *fieldname = TYPE_FIELD_NAME (t, i);
7822
7823 if (is_vtable_name (fieldname, cu))
7824 {
7825 TYPE_VPTR_FIELDNO (type) = i;
7826 break;
7827 }
7828 }
7829
7830 /* Complain if virtual function table field not found. */
7831 if (i < TYPE_N_BASECLASSES (t))
7832 complaint (&symfile_complaints,
7833 _("virtual function table pointer "
7834 "not found when defining class '%s'"),
7835 TYPE_TAG_NAME (type) ? TYPE_TAG_NAME (type) :
7836 "");
7837 }
7838 else
7839 {
7840 TYPE_VPTR_FIELDNO (type) = TYPE_VPTR_FIELDNO (t);
7841 }
7842 }
7843 else if (cu->producer
7844 && strncmp (cu->producer,
7845 "IBM(R) XL C/C++ Advanced Edition", 32) == 0)
7846 {
7847 /* The IBM XLC compiler does not provide direct indication
7848 of the containing type, but the vtable pointer is
7849 always named __vfp. */
7850
7851 int i;
7852
7853 for (i = TYPE_NFIELDS (type) - 1;
7854 i >= TYPE_N_BASECLASSES (type);
7855 --i)
7856 {
7857 if (strcmp (TYPE_FIELD_NAME (type, i), "__vfp") == 0)
7858 {
7859 TYPE_VPTR_FIELDNO (type) = i;
7860 TYPE_VPTR_BASETYPE (type) = type;
7861 break;
7862 }
7863 }
7864 }
7865 }
7866
7867 /* Copy fi.typedef_field_list linked list elements content into the
7868 allocated array TYPE_TYPEDEF_FIELD_ARRAY (type). */
7869 if (fi.typedef_field_list)
7870 {
7871 int i = fi.typedef_field_list_count;
7872
7873 ALLOCATE_CPLUS_STRUCT_TYPE (type);
7874 TYPE_TYPEDEF_FIELD_ARRAY (type)
7875 = TYPE_ALLOC (type, sizeof (TYPE_TYPEDEF_FIELD (type, 0)) * i);
7876 TYPE_TYPEDEF_FIELD_COUNT (type) = i;
7877
7878 /* Reverse the list order to keep the debug info elements order. */
7879 while (--i >= 0)
7880 {
7881 struct typedef_field *dest, *src;
7882
7883 dest = &TYPE_TYPEDEF_FIELD (type, i);
7884 src = &fi.typedef_field_list->field;
7885 fi.typedef_field_list = fi.typedef_field_list->next;
7886 *dest = *src;
7887 }
7888 }
7889
7890 do_cleanups (back_to);
7891
7892 if (HAVE_CPLUS_STRUCT (type))
7893 TYPE_CPLUS_REALLY_JAVA (type) = cu->language == language_java;
7894 }
7895
7896 quirk_gcc_member_function_pointer (type, objfile);
7897
7898 /* NOTE: carlton/2004-03-16: GCC 3.4 (or at least one of its
7899 snapshots) has been known to create a die giving a declaration
7900 for a class that has, as a child, a die giving a definition for a
7901 nested class. So we have to process our children even if the
7902 current die is a declaration. Normally, of course, a declaration
7903 won't have any children at all. */
7904
7905 while (child_die != NULL && child_die->tag)
7906 {
7907 if (child_die->tag == DW_TAG_member
7908 || child_die->tag == DW_TAG_variable
7909 || child_die->tag == DW_TAG_inheritance
7910 || child_die->tag == DW_TAG_template_value_param
7911 || child_die->tag == DW_TAG_template_type_param)
7912 {
7913 /* Do nothing. */
7914 }
7915 else
7916 process_die (child_die, cu);
7917
7918 child_die = sibling_die (child_die);
7919 }
7920
7921 /* Do not consider external references. According to the DWARF standard,
7922 these DIEs are identified by the fact that they have no byte_size
7923 attribute, and a declaration attribute. */
7924 if (dwarf2_attr (die, DW_AT_byte_size, cu) != NULL
7925 || !die_is_declaration (die, cu))
7926 new_symbol (die, type, cu);
7927 }
7928
7929 /* Given a DW_AT_enumeration_type die, set its type. We do not
7930 complete the type's fields yet, or create any symbols. */
7931
7932 static struct type *
7933 read_enumeration_type (struct die_info *die, struct dwarf2_cu *cu)
7934 {
7935 struct objfile *objfile = cu->objfile;
7936 struct type *type;
7937 struct attribute *attr;
7938 const char *name;
7939
7940 /* If the definition of this type lives in .debug_types, read that type.
7941 Don't follow DW_AT_specification though, that will take us back up
7942 the chain and we want to go down. */
7943 attr = dwarf2_attr_no_follow (die, DW_AT_signature, cu);
7944 if (attr)
7945 {
7946 struct dwarf2_cu *type_cu = cu;
7947 struct die_info *type_die = follow_die_ref_or_sig (die, attr, &type_cu);
7948
7949 type = read_type_die (type_die, type_cu);
7950
7951 /* TYPE_CU may not be the same as CU.
7952 Ensure TYPE is recorded in CU's type_hash table. */
7953 return set_die_type (die, type, cu);
7954 }
7955
7956 type = alloc_type (objfile);
7957
7958 TYPE_CODE (type) = TYPE_CODE_ENUM;
7959 name = dwarf2_full_name (NULL, die, cu);
7960 if (name != NULL)
7961 TYPE_TAG_NAME (type) = (char *) name;
7962
7963 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
7964 if (attr)
7965 {
7966 TYPE_LENGTH (type) = DW_UNSND (attr);
7967 }
7968 else
7969 {
7970 TYPE_LENGTH (type) = 0;
7971 }
7972
7973 /* The enumeration DIE can be incomplete. In Ada, any type can be
7974 declared as private in the package spec, and then defined only
7975 inside the package body. Such types are known as Taft Amendment
7976 Types. When another package uses such a type, an incomplete DIE
7977 may be generated by the compiler. */
7978 if (die_is_declaration (die, cu))
7979 TYPE_STUB (type) = 1;
7980
7981 return set_die_type (die, type, cu);
7982 }
7983
7984 /* Given a pointer to a die which begins an enumeration, process all
7985 the dies that define the members of the enumeration, and create the
7986 symbol for the enumeration type.
7987
7988 NOTE: We reverse the order of the element list. */
7989
7990 static void
7991 process_enumeration_scope (struct die_info *die, struct dwarf2_cu *cu)
7992 {
7993 struct type *this_type;
7994
7995 this_type = get_die_type (die, cu);
7996 if (this_type == NULL)
7997 this_type = read_enumeration_type (die, cu);
7998
7999 if (die->child != NULL)
8000 {
8001 struct die_info *child_die;
8002 struct symbol *sym;
8003 struct field *fields = NULL;
8004 int num_fields = 0;
8005 int unsigned_enum = 1;
8006 char *name;
8007 int flag_enum = 1;
8008 ULONGEST mask = 0;
8009
8010 child_die = die->child;
8011 while (child_die && child_die->tag)
8012 {
8013 if (child_die->tag != DW_TAG_enumerator)
8014 {
8015 process_die (child_die, cu);
8016 }
8017 else
8018 {
8019 name = dwarf2_name (child_die, cu);
8020 if (name)
8021 {
8022 sym = new_symbol (child_die, this_type, cu);
8023 if (SYMBOL_VALUE (sym) < 0)
8024 {
8025 unsigned_enum = 0;
8026 flag_enum = 0;
8027 }
8028 else if ((mask & SYMBOL_VALUE (sym)) != 0)
8029 flag_enum = 0;
8030 else
8031 mask |= SYMBOL_VALUE (sym);
8032
8033 if ((num_fields % DW_FIELD_ALLOC_CHUNK) == 0)
8034 {
8035 fields = (struct field *)
8036 xrealloc (fields,
8037 (num_fields + DW_FIELD_ALLOC_CHUNK)
8038 * sizeof (struct field));
8039 }
8040
8041 FIELD_NAME (fields[num_fields]) = SYMBOL_LINKAGE_NAME (sym);
8042 FIELD_TYPE (fields[num_fields]) = NULL;
8043 SET_FIELD_BITPOS (fields[num_fields], SYMBOL_VALUE (sym));
8044 FIELD_BITSIZE (fields[num_fields]) = 0;
8045
8046 num_fields++;
8047 }
8048 }
8049
8050 child_die = sibling_die (child_die);
8051 }
8052
8053 if (num_fields)
8054 {
8055 TYPE_NFIELDS (this_type) = num_fields;
8056 TYPE_FIELDS (this_type) = (struct field *)
8057 TYPE_ALLOC (this_type, sizeof (struct field) * num_fields);
8058 memcpy (TYPE_FIELDS (this_type), fields,
8059 sizeof (struct field) * num_fields);
8060 xfree (fields);
8061 }
8062 if (unsigned_enum)
8063 TYPE_UNSIGNED (this_type) = 1;
8064 if (flag_enum)
8065 TYPE_FLAG_ENUM (this_type) = 1;
8066 }
8067
8068 /* If we are reading an enum from a .debug_types unit, and the enum
8069 is a declaration, and the enum is not the signatured type in the
8070 unit, then we do not want to add a symbol for it. Adding a
8071 symbol would in some cases obscure the true definition of the
8072 enum, giving users an incomplete type when the definition is
8073 actually available. Note that we do not want to do this for all
8074 enums which are just declarations, because C++0x allows forward
8075 enum declarations. */
8076 if (cu->per_cu->debug_types_section
8077 && die_is_declaration (die, cu))
8078 {
8079 struct signatured_type *type_sig;
8080
8081 type_sig
8082 = lookup_signatured_type_at_offset (dwarf2_per_objfile->objfile,
8083 cu->per_cu->debug_types_section,
8084 cu->per_cu->offset);
8085 if (type_sig->per_cu.offset + type_sig->type_offset
8086 != die->offset)
8087 return;
8088 }
8089
8090 new_symbol (die, this_type, cu);
8091 }
8092
8093 /* Extract all information from a DW_TAG_array_type DIE and put it in
8094 the DIE's type field. For now, this only handles one dimensional
8095 arrays. */
8096
8097 static struct type *
8098 read_array_type (struct die_info *die, struct dwarf2_cu *cu)
8099 {
8100 struct objfile *objfile = cu->objfile;
8101 struct die_info *child_die;
8102 struct type *type;
8103 struct type *element_type, *range_type, *index_type;
8104 struct type **range_types = NULL;
8105 struct attribute *attr;
8106 int ndim = 0;
8107 struct cleanup *back_to;
8108 char *name;
8109
8110 element_type = die_type (die, cu);
8111
8112 /* The die_type call above may have already set the type for this DIE. */
8113 type = get_die_type (die, cu);
8114 if (type)
8115 return type;
8116
8117 /* Irix 6.2 native cc creates array types without children for
8118 arrays with unspecified length. */
8119 if (die->child == NULL)
8120 {
8121 index_type = objfile_type (objfile)->builtin_int;
8122 range_type = create_range_type (NULL, index_type, 0, -1);
8123 type = create_array_type (NULL, element_type, range_type);
8124 return set_die_type (die, type, cu);
8125 }
8126
8127 back_to = make_cleanup (null_cleanup, NULL);
8128 child_die = die->child;
8129 while (child_die && child_die->tag)
8130 {
8131 if (child_die->tag == DW_TAG_subrange_type)
8132 {
8133 struct type *child_type = read_type_die (child_die, cu);
8134
8135 if (child_type != NULL)
8136 {
8137 /* The range type was succesfully read. Save it for the
8138 array type creation. */
8139 if ((ndim % DW_FIELD_ALLOC_CHUNK) == 0)
8140 {
8141 range_types = (struct type **)
8142 xrealloc (range_types, (ndim + DW_FIELD_ALLOC_CHUNK)
8143 * sizeof (struct type *));
8144 if (ndim == 0)
8145 make_cleanup (free_current_contents, &range_types);
8146 }
8147 range_types[ndim++] = child_type;
8148 }
8149 }
8150 child_die = sibling_die (child_die);
8151 }
8152
8153 /* Dwarf2 dimensions are output from left to right, create the
8154 necessary array types in backwards order. */
8155
8156 type = element_type;
8157
8158 if (read_array_order (die, cu) == DW_ORD_col_major)
8159 {
8160 int i = 0;
8161
8162 while (i < ndim)
8163 type = create_array_type (NULL, type, range_types[i++]);
8164 }
8165 else
8166 {
8167 while (ndim-- > 0)
8168 type = create_array_type (NULL, type, range_types[ndim]);
8169 }
8170
8171 /* Understand Dwarf2 support for vector types (like they occur on
8172 the PowerPC w/ AltiVec). Gcc just adds another attribute to the
8173 array type. This is not part of the Dwarf2/3 standard yet, but a
8174 custom vendor extension. The main difference between a regular
8175 array and the vector variant is that vectors are passed by value
8176 to functions. */
8177 attr = dwarf2_attr (die, DW_AT_GNU_vector, cu);
8178 if (attr)
8179 make_vector_type (type);
8180
8181 /* The DIE may have DW_AT_byte_size set. For example an OpenCL
8182 implementation may choose to implement triple vectors using this
8183 attribute. */
8184 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8185 if (attr)
8186 {
8187 if (DW_UNSND (attr) >= TYPE_LENGTH (type))
8188 TYPE_LENGTH (type) = DW_UNSND (attr);
8189 else
8190 complaint (&symfile_complaints,
8191 _("DW_AT_byte_size for array type smaller "
8192 "than the total size of elements"));
8193 }
8194
8195 name = dwarf2_name (die, cu);
8196 if (name)
8197 TYPE_NAME (type) = name;
8198
8199 /* Install the type in the die. */
8200 set_die_type (die, type, cu);
8201
8202 /* set_die_type should be already done. */
8203 set_descriptive_type (type, die, cu);
8204
8205 do_cleanups (back_to);
8206
8207 return type;
8208 }
8209
8210 static enum dwarf_array_dim_ordering
8211 read_array_order (struct die_info *die, struct dwarf2_cu *cu)
8212 {
8213 struct attribute *attr;
8214
8215 attr = dwarf2_attr (die, DW_AT_ordering, cu);
8216
8217 if (attr) return DW_SND (attr);
8218
8219 /* GNU F77 is a special case, as at 08/2004 array type info is the
8220 opposite order to the dwarf2 specification, but data is still
8221 laid out as per normal fortran.
8222
8223 FIXME: dsl/2004-8-20: If G77 is ever fixed, this will also need
8224 version checking. */
8225
8226 if (cu->language == language_fortran
8227 && cu->producer && strstr (cu->producer, "GNU F77"))
8228 {
8229 return DW_ORD_row_major;
8230 }
8231
8232 switch (cu->language_defn->la_array_ordering)
8233 {
8234 case array_column_major:
8235 return DW_ORD_col_major;
8236 case array_row_major:
8237 default:
8238 return DW_ORD_row_major;
8239 };
8240 }
8241
8242 /* Extract all information from a DW_TAG_set_type DIE and put it in
8243 the DIE's type field. */
8244
8245 static struct type *
8246 read_set_type (struct die_info *die, struct dwarf2_cu *cu)
8247 {
8248 struct type *domain_type, *set_type;
8249 struct attribute *attr;
8250
8251 domain_type = die_type (die, cu);
8252
8253 /* The die_type call above may have already set the type for this DIE. */
8254 set_type = get_die_type (die, cu);
8255 if (set_type)
8256 return set_type;
8257
8258 set_type = create_set_type (NULL, domain_type);
8259
8260 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8261 if (attr)
8262 TYPE_LENGTH (set_type) = DW_UNSND (attr);
8263
8264 return set_die_type (die, set_type, cu);
8265 }
8266
8267 /* First cut: install each common block member as a global variable. */
8268
8269 static void
8270 read_common_block (struct die_info *die, struct dwarf2_cu *cu)
8271 {
8272 struct die_info *child_die;
8273 struct attribute *attr;
8274 struct symbol *sym;
8275 CORE_ADDR base = (CORE_ADDR) 0;
8276
8277 attr = dwarf2_attr (die, DW_AT_location, cu);
8278 if (attr)
8279 {
8280 /* Support the .debug_loc offsets. */
8281 if (attr_form_is_block (attr))
8282 {
8283 base = decode_locdesc (DW_BLOCK (attr), cu);
8284 }
8285 else if (attr_form_is_section_offset (attr))
8286 {
8287 dwarf2_complex_location_expr_complaint ();
8288 }
8289 else
8290 {
8291 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
8292 "common block member");
8293 }
8294 }
8295 if (die->child != NULL)
8296 {
8297 child_die = die->child;
8298 while (child_die && child_die->tag)
8299 {
8300 LONGEST offset;
8301
8302 sym = new_symbol (child_die, NULL, cu);
8303 if (sym != NULL
8304 && handle_data_member_location (child_die, cu, &offset))
8305 {
8306 SYMBOL_VALUE_ADDRESS (sym) = base + offset;
8307 add_symbol_to_list (sym, &global_symbols);
8308 }
8309 child_die = sibling_die (child_die);
8310 }
8311 }
8312 }
8313
8314 /* Create a type for a C++ namespace. */
8315
8316 static struct type *
8317 read_namespace_type (struct die_info *die, struct dwarf2_cu *cu)
8318 {
8319 struct objfile *objfile = cu->objfile;
8320 const char *previous_prefix, *name;
8321 int is_anonymous;
8322 struct type *type;
8323
8324 /* For extensions, reuse the type of the original namespace. */
8325 if (dwarf2_attr (die, DW_AT_extension, cu) != NULL)
8326 {
8327 struct die_info *ext_die;
8328 struct dwarf2_cu *ext_cu = cu;
8329
8330 ext_die = dwarf2_extension (die, &ext_cu);
8331 type = read_type_die (ext_die, ext_cu);
8332
8333 /* EXT_CU may not be the same as CU.
8334 Ensure TYPE is recorded in CU's type_hash table. */
8335 return set_die_type (die, type, cu);
8336 }
8337
8338 name = namespace_name (die, &is_anonymous, cu);
8339
8340 /* Now build the name of the current namespace. */
8341
8342 previous_prefix = determine_prefix (die, cu);
8343 if (previous_prefix[0] != '\0')
8344 name = typename_concat (&objfile->objfile_obstack,
8345 previous_prefix, name, 0, cu);
8346
8347 /* Create the type. */
8348 type = init_type (TYPE_CODE_NAMESPACE, 0, 0, NULL,
8349 objfile);
8350 TYPE_NAME (type) = (char *) name;
8351 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8352
8353 return set_die_type (die, type, cu);
8354 }
8355
8356 /* Read a C++ namespace. */
8357
8358 static void
8359 read_namespace (struct die_info *die, struct dwarf2_cu *cu)
8360 {
8361 struct objfile *objfile = cu->objfile;
8362 int is_anonymous;
8363
8364 /* Add a symbol associated to this if we haven't seen the namespace
8365 before. Also, add a using directive if it's an anonymous
8366 namespace. */
8367
8368 if (dwarf2_attr (die, DW_AT_extension, cu) == NULL)
8369 {
8370 struct type *type;
8371
8372 type = read_type_die (die, cu);
8373 new_symbol (die, type, cu);
8374
8375 namespace_name (die, &is_anonymous, cu);
8376 if (is_anonymous)
8377 {
8378 const char *previous_prefix = determine_prefix (die, cu);
8379
8380 cp_add_using_directive (previous_prefix, TYPE_NAME (type), NULL,
8381 NULL, NULL, &objfile->objfile_obstack);
8382 }
8383 }
8384
8385 if (die->child != NULL)
8386 {
8387 struct die_info *child_die = die->child;
8388
8389 while (child_die && child_die->tag)
8390 {
8391 process_die (child_die, cu);
8392 child_die = sibling_die (child_die);
8393 }
8394 }
8395 }
8396
8397 /* Read a Fortran module as type. This DIE can be only a declaration used for
8398 imported module. Still we need that type as local Fortran "use ... only"
8399 declaration imports depend on the created type in determine_prefix. */
8400
8401 static struct type *
8402 read_module_type (struct die_info *die, struct dwarf2_cu *cu)
8403 {
8404 struct objfile *objfile = cu->objfile;
8405 char *module_name;
8406 struct type *type;
8407
8408 module_name = dwarf2_name (die, cu);
8409 if (!module_name)
8410 complaint (&symfile_complaints,
8411 _("DW_TAG_module has no name, offset 0x%x"),
8412 die->offset);
8413 type = init_type (TYPE_CODE_MODULE, 0, 0, module_name, objfile);
8414
8415 /* determine_prefix uses TYPE_TAG_NAME. */
8416 TYPE_TAG_NAME (type) = TYPE_NAME (type);
8417
8418 return set_die_type (die, type, cu);
8419 }
8420
8421 /* Read a Fortran module. */
8422
8423 static void
8424 read_module (struct die_info *die, struct dwarf2_cu *cu)
8425 {
8426 struct die_info *child_die = die->child;
8427
8428 while (child_die && child_die->tag)
8429 {
8430 process_die (child_die, cu);
8431 child_die = sibling_die (child_die);
8432 }
8433 }
8434
8435 /* Return the name of the namespace represented by DIE. Set
8436 *IS_ANONYMOUS to tell whether or not the namespace is an anonymous
8437 namespace. */
8438
8439 static const char *
8440 namespace_name (struct die_info *die, int *is_anonymous, struct dwarf2_cu *cu)
8441 {
8442 struct die_info *current_die;
8443 const char *name = NULL;
8444
8445 /* Loop through the extensions until we find a name. */
8446
8447 for (current_die = die;
8448 current_die != NULL;
8449 current_die = dwarf2_extension (die, &cu))
8450 {
8451 name = dwarf2_name (current_die, cu);
8452 if (name != NULL)
8453 break;
8454 }
8455
8456 /* Is it an anonymous namespace? */
8457
8458 *is_anonymous = (name == NULL);
8459 if (*is_anonymous)
8460 name = CP_ANONYMOUS_NAMESPACE_STR;
8461
8462 return name;
8463 }
8464
8465 /* Extract all information from a DW_TAG_pointer_type DIE and add to
8466 the user defined type vector. */
8467
8468 static struct type *
8469 read_tag_pointer_type (struct die_info *die, struct dwarf2_cu *cu)
8470 {
8471 struct gdbarch *gdbarch = get_objfile_arch (cu->objfile);
8472 struct comp_unit_head *cu_header = &cu->header;
8473 struct type *type;
8474 struct attribute *attr_byte_size;
8475 struct attribute *attr_address_class;
8476 int byte_size, addr_class;
8477 struct type *target_type;
8478
8479 target_type = die_type (die, cu);
8480
8481 /* The die_type call above may have already set the type for this DIE. */
8482 type = get_die_type (die, cu);
8483 if (type)
8484 return type;
8485
8486 type = lookup_pointer_type (target_type);
8487
8488 attr_byte_size = dwarf2_attr (die, DW_AT_byte_size, cu);
8489 if (attr_byte_size)
8490 byte_size = DW_UNSND (attr_byte_size);
8491 else
8492 byte_size = cu_header->addr_size;
8493
8494 attr_address_class = dwarf2_attr (die, DW_AT_address_class, cu);
8495 if (attr_address_class)
8496 addr_class = DW_UNSND (attr_address_class);
8497 else
8498 addr_class = DW_ADDR_none;
8499
8500 /* If the pointer size or address class is different than the
8501 default, create a type variant marked as such and set the
8502 length accordingly. */
8503 if (TYPE_LENGTH (type) != byte_size || addr_class != DW_ADDR_none)
8504 {
8505 if (gdbarch_address_class_type_flags_p (gdbarch))
8506 {
8507 int type_flags;
8508
8509 type_flags = gdbarch_address_class_type_flags
8510 (gdbarch, byte_size, addr_class);
8511 gdb_assert ((type_flags & ~TYPE_INSTANCE_FLAG_ADDRESS_CLASS_ALL)
8512 == 0);
8513 type = make_type_with_address_space (type, type_flags);
8514 }
8515 else if (TYPE_LENGTH (type) != byte_size)
8516 {
8517 complaint (&symfile_complaints,
8518 _("invalid pointer size %d"), byte_size);
8519 }
8520 else
8521 {
8522 /* Should we also complain about unhandled address classes? */
8523 }
8524 }
8525
8526 TYPE_LENGTH (type) = byte_size;
8527 return set_die_type (die, type, cu);
8528 }
8529
8530 /* Extract all information from a DW_TAG_ptr_to_member_type DIE and add to
8531 the user defined type vector. */
8532
8533 static struct type *
8534 read_tag_ptr_to_member_type (struct die_info *die, struct dwarf2_cu *cu)
8535 {
8536 struct type *type;
8537 struct type *to_type;
8538 struct type *domain;
8539
8540 to_type = die_type (die, cu);
8541 domain = die_containing_type (die, cu);
8542
8543 /* The calls above may have already set the type for this DIE. */
8544 type = get_die_type (die, cu);
8545 if (type)
8546 return type;
8547
8548 if (TYPE_CODE (check_typedef (to_type)) == TYPE_CODE_METHOD)
8549 type = lookup_methodptr_type (to_type);
8550 else
8551 type = lookup_memberptr_type (to_type, domain);
8552
8553 return set_die_type (die, type, cu);
8554 }
8555
8556 /* Extract all information from a DW_TAG_reference_type DIE and add to
8557 the user defined type vector. */
8558
8559 static struct type *
8560 read_tag_reference_type (struct die_info *die, struct dwarf2_cu *cu)
8561 {
8562 struct comp_unit_head *cu_header = &cu->header;
8563 struct type *type, *target_type;
8564 struct attribute *attr;
8565
8566 target_type = die_type (die, cu);
8567
8568 /* The die_type call above may have already set the type for this DIE. */
8569 type = get_die_type (die, cu);
8570 if (type)
8571 return type;
8572
8573 type = lookup_reference_type (target_type);
8574 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8575 if (attr)
8576 {
8577 TYPE_LENGTH (type) = DW_UNSND (attr);
8578 }
8579 else
8580 {
8581 TYPE_LENGTH (type) = cu_header->addr_size;
8582 }
8583 return set_die_type (die, type, cu);
8584 }
8585
8586 static struct type *
8587 read_tag_const_type (struct die_info *die, struct dwarf2_cu *cu)
8588 {
8589 struct type *base_type, *cv_type;
8590
8591 base_type = die_type (die, cu);
8592
8593 /* The die_type call above may have already set the type for this DIE. */
8594 cv_type = get_die_type (die, cu);
8595 if (cv_type)
8596 return cv_type;
8597
8598 /* In case the const qualifier is applied to an array type, the element type
8599 is so qualified, not the array type (section 6.7.3 of C99). */
8600 if (TYPE_CODE (base_type) == TYPE_CODE_ARRAY)
8601 {
8602 struct type *el_type, *inner_array;
8603
8604 base_type = copy_type (base_type);
8605 inner_array = base_type;
8606
8607 while (TYPE_CODE (TYPE_TARGET_TYPE (inner_array)) == TYPE_CODE_ARRAY)
8608 {
8609 TYPE_TARGET_TYPE (inner_array) =
8610 copy_type (TYPE_TARGET_TYPE (inner_array));
8611 inner_array = TYPE_TARGET_TYPE (inner_array);
8612 }
8613
8614 el_type = TYPE_TARGET_TYPE (inner_array);
8615 TYPE_TARGET_TYPE (inner_array) =
8616 make_cv_type (1, TYPE_VOLATILE (el_type), el_type, NULL);
8617
8618 return set_die_type (die, base_type, cu);
8619 }
8620
8621 cv_type = make_cv_type (1, TYPE_VOLATILE (base_type), base_type, 0);
8622 return set_die_type (die, cv_type, cu);
8623 }
8624
8625 static struct type *
8626 read_tag_volatile_type (struct die_info *die, struct dwarf2_cu *cu)
8627 {
8628 struct type *base_type, *cv_type;
8629
8630 base_type = die_type (die, cu);
8631
8632 /* The die_type call above may have already set the type for this DIE. */
8633 cv_type = get_die_type (die, cu);
8634 if (cv_type)
8635 return cv_type;
8636
8637 cv_type = make_cv_type (TYPE_CONST (base_type), 1, base_type, 0);
8638 return set_die_type (die, cv_type, cu);
8639 }
8640
8641 /* Extract all information from a DW_TAG_string_type DIE and add to
8642 the user defined type vector. It isn't really a user defined type,
8643 but it behaves like one, with other DIE's using an AT_user_def_type
8644 attribute to reference it. */
8645
8646 static struct type *
8647 read_tag_string_type (struct die_info *die, struct dwarf2_cu *cu)
8648 {
8649 struct objfile *objfile = cu->objfile;
8650 struct gdbarch *gdbarch = get_objfile_arch (objfile);
8651 struct type *type, *range_type, *index_type, *char_type;
8652 struct attribute *attr;
8653 unsigned int length;
8654
8655 attr = dwarf2_attr (die, DW_AT_string_length, cu);
8656 if (attr)
8657 {
8658 length = DW_UNSND (attr);
8659 }
8660 else
8661 {
8662 /* Check for the DW_AT_byte_size attribute. */
8663 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8664 if (attr)
8665 {
8666 length = DW_UNSND (attr);
8667 }
8668 else
8669 {
8670 length = 1;
8671 }
8672 }
8673
8674 index_type = objfile_type (objfile)->builtin_int;
8675 range_type = create_range_type (NULL, index_type, 1, length);
8676 char_type = language_string_char_type (cu->language_defn, gdbarch);
8677 type = create_string_type (NULL, char_type, range_type);
8678
8679 return set_die_type (die, type, cu);
8680 }
8681
8682 /* Handle DIES due to C code like:
8683
8684 struct foo
8685 {
8686 int (*funcp)(int a, long l);
8687 int b;
8688 };
8689
8690 ('funcp' generates a DW_TAG_subroutine_type DIE). */
8691
8692 static struct type *
8693 read_subroutine_type (struct die_info *die, struct dwarf2_cu *cu)
8694 {
8695 struct objfile *objfile = cu->objfile;
8696 struct type *type; /* Type that this function returns. */
8697 struct type *ftype; /* Function that returns above type. */
8698 struct attribute *attr;
8699
8700 type = die_type (die, cu);
8701
8702 /* The die_type call above may have already set the type for this DIE. */
8703 ftype = get_die_type (die, cu);
8704 if (ftype)
8705 return ftype;
8706
8707 ftype = lookup_function_type (type);
8708
8709 /* All functions in C++, Pascal and Java have prototypes. */
8710 attr = dwarf2_attr (die, DW_AT_prototyped, cu);
8711 if ((attr && (DW_UNSND (attr) != 0))
8712 || cu->language == language_cplus
8713 || cu->language == language_java
8714 || cu->language == language_pascal)
8715 TYPE_PROTOTYPED (ftype) = 1;
8716 else if (producer_is_realview (cu->producer))
8717 /* RealView does not emit DW_AT_prototyped. We can not
8718 distinguish prototyped and unprototyped functions; default to
8719 prototyped, since that is more common in modern code (and
8720 RealView warns about unprototyped functions). */
8721 TYPE_PROTOTYPED (ftype) = 1;
8722
8723 /* Store the calling convention in the type if it's available in
8724 the subroutine die. Otherwise set the calling convention to
8725 the default value DW_CC_normal. */
8726 attr = dwarf2_attr (die, DW_AT_calling_convention, cu);
8727 if (attr)
8728 TYPE_CALLING_CONVENTION (ftype) = DW_UNSND (attr);
8729 else if (cu->producer && strstr (cu->producer, "IBM XL C for OpenCL"))
8730 TYPE_CALLING_CONVENTION (ftype) = DW_CC_GDB_IBM_OpenCL;
8731 else
8732 TYPE_CALLING_CONVENTION (ftype) = DW_CC_normal;
8733
8734 /* We need to add the subroutine type to the die immediately so
8735 we don't infinitely recurse when dealing with parameters
8736 declared as the same subroutine type. */
8737 set_die_type (die, ftype, cu);
8738
8739 if (die->child != NULL)
8740 {
8741 struct type *void_type = objfile_type (objfile)->builtin_void;
8742 struct die_info *child_die;
8743 int nparams, iparams;
8744
8745 /* Count the number of parameters.
8746 FIXME: GDB currently ignores vararg functions, but knows about
8747 vararg member functions. */
8748 nparams = 0;
8749 child_die = die->child;
8750 while (child_die && child_die->tag)
8751 {
8752 if (child_die->tag == DW_TAG_formal_parameter)
8753 nparams++;
8754 else if (child_die->tag == DW_TAG_unspecified_parameters)
8755 TYPE_VARARGS (ftype) = 1;
8756 child_die = sibling_die (child_die);
8757 }
8758
8759 /* Allocate storage for parameters and fill them in. */
8760 TYPE_NFIELDS (ftype) = nparams;
8761 TYPE_FIELDS (ftype) = (struct field *)
8762 TYPE_ZALLOC (ftype, nparams * sizeof (struct field));
8763
8764 /* TYPE_FIELD_TYPE must never be NULL. Pre-fill the array to ensure it
8765 even if we error out during the parameters reading below. */
8766 for (iparams = 0; iparams < nparams; iparams++)
8767 TYPE_FIELD_TYPE (ftype, iparams) = void_type;
8768
8769 iparams = 0;
8770 child_die = die->child;
8771 while (child_die && child_die->tag)
8772 {
8773 if (child_die->tag == DW_TAG_formal_parameter)
8774 {
8775 struct type *arg_type;
8776
8777 /* DWARF version 2 has no clean way to discern C++
8778 static and non-static member functions. G++ helps
8779 GDB by marking the first parameter for non-static
8780 member functions (which is the this pointer) as
8781 artificial. We pass this information to
8782 dwarf2_add_member_fn via TYPE_FIELD_ARTIFICIAL.
8783
8784 DWARF version 3 added DW_AT_object_pointer, which GCC
8785 4.5 does not yet generate. */
8786 attr = dwarf2_attr (child_die, DW_AT_artificial, cu);
8787 if (attr)
8788 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = DW_UNSND (attr);
8789 else
8790 {
8791 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 0;
8792
8793 /* GCC/43521: In java, the formal parameter
8794 "this" is sometimes not marked with DW_AT_artificial. */
8795 if (cu->language == language_java)
8796 {
8797 const char *name = dwarf2_name (child_die, cu);
8798
8799 if (name && !strcmp (name, "this"))
8800 TYPE_FIELD_ARTIFICIAL (ftype, iparams) = 1;
8801 }
8802 }
8803 arg_type = die_type (child_die, cu);
8804
8805 /* RealView does not mark THIS as const, which the testsuite
8806 expects. GCC marks THIS as const in method definitions,
8807 but not in the class specifications (GCC PR 43053). */
8808 if (cu->language == language_cplus && !TYPE_CONST (arg_type)
8809 && TYPE_FIELD_ARTIFICIAL (ftype, iparams))
8810 {
8811 int is_this = 0;
8812 struct dwarf2_cu *arg_cu = cu;
8813 const char *name = dwarf2_name (child_die, cu);
8814
8815 attr = dwarf2_attr (die, DW_AT_object_pointer, cu);
8816 if (attr)
8817 {
8818 /* If the compiler emits this, use it. */
8819 if (follow_die_ref (die, attr, &arg_cu) == child_die)
8820 is_this = 1;
8821 }
8822 else if (name && strcmp (name, "this") == 0)
8823 /* Function definitions will have the argument names. */
8824 is_this = 1;
8825 else if (name == NULL && iparams == 0)
8826 /* Declarations may not have the names, so like
8827 elsewhere in GDB, assume an artificial first
8828 argument is "this". */
8829 is_this = 1;
8830
8831 if (is_this)
8832 arg_type = make_cv_type (1, TYPE_VOLATILE (arg_type),
8833 arg_type, 0);
8834 }
8835
8836 TYPE_FIELD_TYPE (ftype, iparams) = arg_type;
8837 iparams++;
8838 }
8839 child_die = sibling_die (child_die);
8840 }
8841 }
8842
8843 return ftype;
8844 }
8845
8846 static struct type *
8847 read_typedef (struct die_info *die, struct dwarf2_cu *cu)
8848 {
8849 struct objfile *objfile = cu->objfile;
8850 const char *name = NULL;
8851 struct type *this_type, *target_type;
8852
8853 name = dwarf2_full_name (NULL, die, cu);
8854 this_type = init_type (TYPE_CODE_TYPEDEF, 0,
8855 TYPE_FLAG_TARGET_STUB, NULL, objfile);
8856 TYPE_NAME (this_type) = (char *) name;
8857 set_die_type (die, this_type, cu);
8858 target_type = die_type (die, cu);
8859 if (target_type != this_type)
8860 TYPE_TARGET_TYPE (this_type) = target_type;
8861 else
8862 {
8863 /* Self-referential typedefs are, it seems, not allowed by the DWARF
8864 spec and cause infinite loops in GDB. */
8865 complaint (&symfile_complaints,
8866 _("Self-referential DW_TAG_typedef "
8867 "- DIE at 0x%x [in module %s]"),
8868 die->offset, objfile->name);
8869 TYPE_TARGET_TYPE (this_type) = NULL;
8870 }
8871 return this_type;
8872 }
8873
8874 /* Find a representation of a given base type and install
8875 it in the TYPE field of the die. */
8876
8877 static struct type *
8878 read_base_type (struct die_info *die, struct dwarf2_cu *cu)
8879 {
8880 struct objfile *objfile = cu->objfile;
8881 struct type *type;
8882 struct attribute *attr;
8883 int encoding = 0, size = 0;
8884 char *name;
8885 enum type_code code = TYPE_CODE_INT;
8886 int type_flags = 0;
8887 struct type *target_type = NULL;
8888
8889 attr = dwarf2_attr (die, DW_AT_encoding, cu);
8890 if (attr)
8891 {
8892 encoding = DW_UNSND (attr);
8893 }
8894 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
8895 if (attr)
8896 {
8897 size = DW_UNSND (attr);
8898 }
8899 name = dwarf2_name (die, cu);
8900 if (!name)
8901 {
8902 complaint (&symfile_complaints,
8903 _("DW_AT_name missing from DW_TAG_base_type"));
8904 }
8905
8906 switch (encoding)
8907 {
8908 case DW_ATE_address:
8909 /* Turn DW_ATE_address into a void * pointer. */
8910 code = TYPE_CODE_PTR;
8911 type_flags |= TYPE_FLAG_UNSIGNED;
8912 target_type = init_type (TYPE_CODE_VOID, 1, 0, NULL, objfile);
8913 break;
8914 case DW_ATE_boolean:
8915 code = TYPE_CODE_BOOL;
8916 type_flags |= TYPE_FLAG_UNSIGNED;
8917 break;
8918 case DW_ATE_complex_float:
8919 code = TYPE_CODE_COMPLEX;
8920 target_type = init_type (TYPE_CODE_FLT, size / 2, 0, NULL, objfile);
8921 break;
8922 case DW_ATE_decimal_float:
8923 code = TYPE_CODE_DECFLOAT;
8924 break;
8925 case DW_ATE_float:
8926 code = TYPE_CODE_FLT;
8927 break;
8928 case DW_ATE_signed:
8929 break;
8930 case DW_ATE_unsigned:
8931 type_flags |= TYPE_FLAG_UNSIGNED;
8932 if (cu->language == language_fortran
8933 && name
8934 && strncmp (name, "character(", sizeof ("character(") - 1) == 0)
8935 code = TYPE_CODE_CHAR;
8936 break;
8937 case DW_ATE_signed_char:
8938 if (cu->language == language_ada || cu->language == language_m2
8939 || cu->language == language_pascal
8940 || cu->language == language_fortran)
8941 code = TYPE_CODE_CHAR;
8942 break;
8943 case DW_ATE_unsigned_char:
8944 if (cu->language == language_ada || cu->language == language_m2
8945 || cu->language == language_pascal
8946 || cu->language == language_fortran)
8947 code = TYPE_CODE_CHAR;
8948 type_flags |= TYPE_FLAG_UNSIGNED;
8949 break;
8950 case DW_ATE_UTF:
8951 /* We just treat this as an integer and then recognize the
8952 type by name elsewhere. */
8953 break;
8954
8955 default:
8956 complaint (&symfile_complaints, _("unsupported DW_AT_encoding: '%s'"),
8957 dwarf_type_encoding_name (encoding));
8958 break;
8959 }
8960
8961 type = init_type (code, size, type_flags, NULL, objfile);
8962 TYPE_NAME (type) = name;
8963 TYPE_TARGET_TYPE (type) = target_type;
8964
8965 if (name && strcmp (name, "char") == 0)
8966 TYPE_NOSIGN (type) = 1;
8967
8968 return set_die_type (die, type, cu);
8969 }
8970
8971 /* Read the given DW_AT_subrange DIE. */
8972
8973 static struct type *
8974 read_subrange_type (struct die_info *die, struct dwarf2_cu *cu)
8975 {
8976 struct type *base_type;
8977 struct type *range_type;
8978 struct attribute *attr;
8979 LONGEST low = 0;
8980 LONGEST high = -1;
8981 char *name;
8982 LONGEST negative_mask;
8983
8984 base_type = die_type (die, cu);
8985 /* Preserve BASE_TYPE's original type, just set its LENGTH. */
8986 check_typedef (base_type);
8987
8988 /* The die_type call above may have already set the type for this DIE. */
8989 range_type = get_die_type (die, cu);
8990 if (range_type)
8991 return range_type;
8992
8993 if (cu->language == language_fortran)
8994 {
8995 /* FORTRAN implies a lower bound of 1, if not given. */
8996 low = 1;
8997 }
8998
8999 /* FIXME: For variable sized arrays either of these could be
9000 a variable rather than a constant value. We'll allow it,
9001 but we don't know how to handle it. */
9002 attr = dwarf2_attr (die, DW_AT_lower_bound, cu);
9003 if (attr)
9004 low = dwarf2_get_attr_constant_value (attr, 0);
9005
9006 attr = dwarf2_attr (die, DW_AT_upper_bound, cu);
9007 if (attr)
9008 {
9009 if (attr_form_is_block (attr) || is_ref_attr (attr))
9010 {
9011 /* GCC encodes arrays with unspecified or dynamic length
9012 with a DW_FORM_block1 attribute or a reference attribute.
9013 FIXME: GDB does not yet know how to handle dynamic
9014 arrays properly, treat them as arrays with unspecified
9015 length for now.
9016
9017 FIXME: jimb/2003-09-22: GDB does not really know
9018 how to handle arrays of unspecified length
9019 either; we just represent them as zero-length
9020 arrays. Choose an appropriate upper bound given
9021 the lower bound we've computed above. */
9022 high = low - 1;
9023 }
9024 else
9025 high = dwarf2_get_attr_constant_value (attr, 1);
9026 }
9027 else
9028 {
9029 attr = dwarf2_attr (die, DW_AT_count, cu);
9030 if (attr)
9031 {
9032 int count = dwarf2_get_attr_constant_value (attr, 1);
9033 high = low + count - 1;
9034 }
9035 else
9036 {
9037 /* Unspecified array length. */
9038 high = low - 1;
9039 }
9040 }
9041
9042 /* Dwarf-2 specifications explicitly allows to create subrange types
9043 without specifying a base type.
9044 In that case, the base type must be set to the type of
9045 the lower bound, upper bound or count, in that order, if any of these
9046 three attributes references an object that has a type.
9047 If no base type is found, the Dwarf-2 specifications say that
9048 a signed integer type of size equal to the size of an address should
9049 be used.
9050 For the following C code: `extern char gdb_int [];'
9051 GCC produces an empty range DIE.
9052 FIXME: muller/2010-05-28: Possible references to object for low bound,
9053 high bound or count are not yet handled by this code. */
9054 if (TYPE_CODE (base_type) == TYPE_CODE_VOID)
9055 {
9056 struct objfile *objfile = cu->objfile;
9057 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9058 int addr_size = gdbarch_addr_bit (gdbarch) /8;
9059 struct type *int_type = objfile_type (objfile)->builtin_int;
9060
9061 /* Test "int", "long int", and "long long int" objfile types,
9062 and select the first one having a size above or equal to the
9063 architecture address size. */
9064 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9065 base_type = int_type;
9066 else
9067 {
9068 int_type = objfile_type (objfile)->builtin_long;
9069 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9070 base_type = int_type;
9071 else
9072 {
9073 int_type = objfile_type (objfile)->builtin_long_long;
9074 if (int_type && TYPE_LENGTH (int_type) >= addr_size)
9075 base_type = int_type;
9076 }
9077 }
9078 }
9079
9080 negative_mask =
9081 (LONGEST) -1 << (TYPE_LENGTH (base_type) * TARGET_CHAR_BIT - 1);
9082 if (!TYPE_UNSIGNED (base_type) && (low & negative_mask))
9083 low |= negative_mask;
9084 if (!TYPE_UNSIGNED (base_type) && (high & negative_mask))
9085 high |= negative_mask;
9086
9087 range_type = create_range_type (NULL, base_type, low, high);
9088
9089 /* Mark arrays with dynamic length at least as an array of unspecified
9090 length. GDB could check the boundary but before it gets implemented at
9091 least allow accessing the array elements. */
9092 if (attr && attr_form_is_block (attr))
9093 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9094
9095 /* Ada expects an empty array on no boundary attributes. */
9096 if (attr == NULL && cu->language != language_ada)
9097 TYPE_HIGH_BOUND_UNDEFINED (range_type) = 1;
9098
9099 name = dwarf2_name (die, cu);
9100 if (name)
9101 TYPE_NAME (range_type) = name;
9102
9103 attr = dwarf2_attr (die, DW_AT_byte_size, cu);
9104 if (attr)
9105 TYPE_LENGTH (range_type) = DW_UNSND (attr);
9106
9107 set_die_type (die, range_type, cu);
9108
9109 /* set_die_type should be already done. */
9110 set_descriptive_type (range_type, die, cu);
9111
9112 return range_type;
9113 }
9114
9115 static struct type *
9116 read_unspecified_type (struct die_info *die, struct dwarf2_cu *cu)
9117 {
9118 struct type *type;
9119
9120 /* For now, we only support the C meaning of an unspecified type: void. */
9121
9122 type = init_type (TYPE_CODE_VOID, 0, 0, NULL, cu->objfile);
9123 TYPE_NAME (type) = dwarf2_name (die, cu);
9124
9125 return set_die_type (die, type, cu);
9126 }
9127
9128 /* Trivial hash function for die_info: the hash value of a DIE
9129 is its offset in .debug_info for this objfile. */
9130
9131 static hashval_t
9132 die_hash (const void *item)
9133 {
9134 const struct die_info *die = item;
9135
9136 return die->offset;
9137 }
9138
9139 /* Trivial comparison function for die_info structures: two DIEs
9140 are equal if they have the same offset. */
9141
9142 static int
9143 die_eq (const void *item_lhs, const void *item_rhs)
9144 {
9145 const struct die_info *die_lhs = item_lhs;
9146 const struct die_info *die_rhs = item_rhs;
9147
9148 return die_lhs->offset == die_rhs->offset;
9149 }
9150
9151 /* Read a whole compilation unit into a linked list of dies. */
9152
9153 static struct die_info *
9154 read_comp_unit (gdb_byte *info_ptr, struct dwarf2_cu *cu)
9155 {
9156 struct die_reader_specs reader_specs;
9157 int read_abbrevs = 0;
9158 struct cleanup *back_to = NULL;
9159 struct die_info *die;
9160
9161 if (cu->dwarf2_abbrevs == NULL)
9162 {
9163 dwarf2_read_abbrevs (cu);
9164 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
9165 read_abbrevs = 1;
9166 }
9167
9168 gdb_assert (cu->die_hash == NULL);
9169 cu->die_hash
9170 = htab_create_alloc_ex (cu->header.length / 12,
9171 die_hash,
9172 die_eq,
9173 NULL,
9174 &cu->comp_unit_obstack,
9175 hashtab_obstack_allocate,
9176 dummy_obstack_deallocate);
9177
9178 init_cu_die_reader (&reader_specs, cu);
9179
9180 die = read_die_and_children (&reader_specs, info_ptr, &info_ptr, NULL);
9181
9182 if (read_abbrevs)
9183 do_cleanups (back_to);
9184
9185 return die;
9186 }
9187
9188 /* Main entry point for reading a DIE and all children.
9189 Read the DIE and dump it if requested. */
9190
9191 static struct die_info *
9192 read_die_and_children (const struct die_reader_specs *reader,
9193 gdb_byte *info_ptr,
9194 gdb_byte **new_info_ptr,
9195 struct die_info *parent)
9196 {
9197 struct die_info *result = read_die_and_children_1 (reader, info_ptr,
9198 new_info_ptr, parent);
9199
9200 if (dwarf2_die_debug)
9201 {
9202 fprintf_unfiltered (gdb_stdlog,
9203 "\nRead die from %s of %s:\n",
9204 (reader->cu->per_cu->debug_types_section
9205 ? ".debug_types"
9206 : ".debug_info"),
9207 reader->abfd->filename);
9208 dump_die (result, dwarf2_die_debug);
9209 }
9210
9211 return result;
9212 }
9213
9214 /* Read a single die and all its descendents. Set the die's sibling
9215 field to NULL; set other fields in the die correctly, and set all
9216 of the descendents' fields correctly. Set *NEW_INFO_PTR to the
9217 location of the info_ptr after reading all of those dies. PARENT
9218 is the parent of the die in question. */
9219
9220 static struct die_info *
9221 read_die_and_children_1 (const struct die_reader_specs *reader,
9222 gdb_byte *info_ptr,
9223 gdb_byte **new_info_ptr,
9224 struct die_info *parent)
9225 {
9226 struct die_info *die;
9227 gdb_byte *cur_ptr;
9228 int has_children;
9229
9230 cur_ptr = read_full_die (reader, &die, info_ptr, &has_children);
9231 if (die == NULL)
9232 {
9233 *new_info_ptr = cur_ptr;
9234 return NULL;
9235 }
9236 store_in_ref_table (die, reader->cu);
9237
9238 if (has_children)
9239 die->child = read_die_and_siblings (reader, cur_ptr, new_info_ptr, die);
9240 else
9241 {
9242 die->child = NULL;
9243 *new_info_ptr = cur_ptr;
9244 }
9245
9246 die->sibling = NULL;
9247 die->parent = parent;
9248 return die;
9249 }
9250
9251 /* Read a die, all of its descendents, and all of its siblings; set
9252 all of the fields of all of the dies correctly. Arguments are as
9253 in read_die_and_children. */
9254
9255 static struct die_info *
9256 read_die_and_siblings (const struct die_reader_specs *reader,
9257 gdb_byte *info_ptr,
9258 gdb_byte **new_info_ptr,
9259 struct die_info *parent)
9260 {
9261 struct die_info *first_die, *last_sibling;
9262 gdb_byte *cur_ptr;
9263
9264 cur_ptr = info_ptr;
9265 first_die = last_sibling = NULL;
9266
9267 while (1)
9268 {
9269 struct die_info *die
9270 = read_die_and_children_1 (reader, cur_ptr, &cur_ptr, parent);
9271
9272 if (die == NULL)
9273 {
9274 *new_info_ptr = cur_ptr;
9275 return first_die;
9276 }
9277
9278 if (!first_die)
9279 first_die = die;
9280 else
9281 last_sibling->sibling = die;
9282
9283 last_sibling = die;
9284 }
9285 }
9286
9287 /* Read the die from the .debug_info section buffer. Set DIEP to
9288 point to a newly allocated die with its information, except for its
9289 child, sibling, and parent fields. Set HAS_CHILDREN to tell
9290 whether the die has children or not. */
9291
9292 static gdb_byte *
9293 read_full_die (const struct die_reader_specs *reader,
9294 struct die_info **diep, gdb_byte *info_ptr,
9295 int *has_children)
9296 {
9297 unsigned int abbrev_number, bytes_read, i, offset;
9298 struct abbrev_info *abbrev;
9299 struct die_info *die;
9300 struct dwarf2_cu *cu = reader->cu;
9301 bfd *abfd = reader->abfd;
9302
9303 offset = info_ptr - reader->buffer;
9304 abbrev_number = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
9305 info_ptr += bytes_read;
9306 if (!abbrev_number)
9307 {
9308 *diep = NULL;
9309 *has_children = 0;
9310 return info_ptr;
9311 }
9312
9313 abbrev = dwarf2_lookup_abbrev (abbrev_number, cu);
9314 if (!abbrev)
9315 error (_("Dwarf Error: could not find abbrev number %d [in module %s]"),
9316 abbrev_number,
9317 bfd_get_filename (abfd));
9318
9319 die = dwarf_alloc_die (cu, abbrev->num_attrs);
9320 die->offset = offset;
9321 die->tag = abbrev->tag;
9322 die->abbrev = abbrev_number;
9323
9324 die->num_attrs = abbrev->num_attrs;
9325
9326 for (i = 0; i < abbrev->num_attrs; ++i)
9327 info_ptr = read_attribute (&die->attrs[i], &abbrev->attrs[i],
9328 abfd, info_ptr, cu);
9329
9330 *diep = die;
9331 *has_children = abbrev->has_children;
9332 return info_ptr;
9333 }
9334
9335 /* In DWARF version 2, the description of the debugging information is
9336 stored in a separate .debug_abbrev section. Before we read any
9337 dies from a section we read in all abbreviations and install them
9338 in a hash table. This function also sets flags in CU describing
9339 the data found in the abbrev table. */
9340
9341 static void
9342 dwarf2_read_abbrevs (struct dwarf2_cu *cu)
9343 {
9344 bfd *abfd = cu->objfile->obfd;
9345 struct comp_unit_head *cu_header = &cu->header;
9346 gdb_byte *abbrev_ptr;
9347 struct abbrev_info *cur_abbrev;
9348 unsigned int abbrev_number, bytes_read, abbrev_name;
9349 unsigned int abbrev_form, hash_number;
9350 struct attr_abbrev *cur_attrs;
9351 unsigned int allocated_attrs;
9352
9353 /* Initialize dwarf2 abbrevs. */
9354 obstack_init (&cu->abbrev_obstack);
9355 cu->dwarf2_abbrevs = obstack_alloc (&cu->abbrev_obstack,
9356 (ABBREV_HASH_SIZE
9357 * sizeof (struct abbrev_info *)));
9358 memset (cu->dwarf2_abbrevs, 0,
9359 ABBREV_HASH_SIZE * sizeof (struct abbrev_info *));
9360
9361 dwarf2_read_section (dwarf2_per_objfile->objfile,
9362 &dwarf2_per_objfile->abbrev);
9363 abbrev_ptr = dwarf2_per_objfile->abbrev.buffer + cu_header->abbrev_offset;
9364 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9365 abbrev_ptr += bytes_read;
9366
9367 allocated_attrs = ATTR_ALLOC_CHUNK;
9368 cur_attrs = xmalloc (allocated_attrs * sizeof (struct attr_abbrev));
9369
9370 /* Loop until we reach an abbrev number of 0. */
9371 while (abbrev_number)
9372 {
9373 cur_abbrev = dwarf_alloc_abbrev (cu);
9374
9375 /* read in abbrev header */
9376 cur_abbrev->number = abbrev_number;
9377 cur_abbrev->tag = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9378 abbrev_ptr += bytes_read;
9379 cur_abbrev->has_children = read_1_byte (abfd, abbrev_ptr);
9380 abbrev_ptr += 1;
9381
9382 /* now read in declarations */
9383 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9384 abbrev_ptr += bytes_read;
9385 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9386 abbrev_ptr += bytes_read;
9387 while (abbrev_name)
9388 {
9389 if (cur_abbrev->num_attrs == allocated_attrs)
9390 {
9391 allocated_attrs += ATTR_ALLOC_CHUNK;
9392 cur_attrs
9393 = xrealloc (cur_attrs, (allocated_attrs
9394 * sizeof (struct attr_abbrev)));
9395 }
9396
9397 cur_attrs[cur_abbrev->num_attrs].name = abbrev_name;
9398 cur_attrs[cur_abbrev->num_attrs++].form = abbrev_form;
9399 abbrev_name = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9400 abbrev_ptr += bytes_read;
9401 abbrev_form = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9402 abbrev_ptr += bytes_read;
9403 }
9404
9405 cur_abbrev->attrs = obstack_alloc (&cu->abbrev_obstack,
9406 (cur_abbrev->num_attrs
9407 * sizeof (struct attr_abbrev)));
9408 memcpy (cur_abbrev->attrs, cur_attrs,
9409 cur_abbrev->num_attrs * sizeof (struct attr_abbrev));
9410
9411 hash_number = abbrev_number % ABBREV_HASH_SIZE;
9412 cur_abbrev->next = cu->dwarf2_abbrevs[hash_number];
9413 cu->dwarf2_abbrevs[hash_number] = cur_abbrev;
9414
9415 /* Get next abbreviation.
9416 Under Irix6 the abbreviations for a compilation unit are not
9417 always properly terminated with an abbrev number of 0.
9418 Exit loop if we encounter an abbreviation which we have
9419 already read (which means we are about to read the abbreviations
9420 for the next compile unit) or if the end of the abbreviation
9421 table is reached. */
9422 if ((unsigned int) (abbrev_ptr - dwarf2_per_objfile->abbrev.buffer)
9423 >= dwarf2_per_objfile->abbrev.size)
9424 break;
9425 abbrev_number = read_unsigned_leb128 (abfd, abbrev_ptr, &bytes_read);
9426 abbrev_ptr += bytes_read;
9427 if (dwarf2_lookup_abbrev (abbrev_number, cu) != NULL)
9428 break;
9429 }
9430
9431 xfree (cur_attrs);
9432 }
9433
9434 /* Release the memory used by the abbrev table for a compilation unit. */
9435
9436 static void
9437 dwarf2_free_abbrev_table (void *ptr_to_cu)
9438 {
9439 struct dwarf2_cu *cu = ptr_to_cu;
9440
9441 obstack_free (&cu->abbrev_obstack, NULL);
9442 cu->dwarf2_abbrevs = NULL;
9443 }
9444
9445 /* Lookup an abbrev_info structure in the abbrev hash table. */
9446
9447 static struct abbrev_info *
9448 dwarf2_lookup_abbrev (unsigned int number, struct dwarf2_cu *cu)
9449 {
9450 unsigned int hash_number;
9451 struct abbrev_info *abbrev;
9452
9453 hash_number = number % ABBREV_HASH_SIZE;
9454 abbrev = cu->dwarf2_abbrevs[hash_number];
9455
9456 while (abbrev)
9457 {
9458 if (abbrev->number == number)
9459 return abbrev;
9460 else
9461 abbrev = abbrev->next;
9462 }
9463 return NULL;
9464 }
9465
9466 /* Returns nonzero if TAG represents a type that we might generate a partial
9467 symbol for. */
9468
9469 static int
9470 is_type_tag_for_partial (int tag)
9471 {
9472 switch (tag)
9473 {
9474 #if 0
9475 /* Some types that would be reasonable to generate partial symbols for,
9476 that we don't at present. */
9477 case DW_TAG_array_type:
9478 case DW_TAG_file_type:
9479 case DW_TAG_ptr_to_member_type:
9480 case DW_TAG_set_type:
9481 case DW_TAG_string_type:
9482 case DW_TAG_subroutine_type:
9483 #endif
9484 case DW_TAG_base_type:
9485 case DW_TAG_class_type:
9486 case DW_TAG_interface_type:
9487 case DW_TAG_enumeration_type:
9488 case DW_TAG_structure_type:
9489 case DW_TAG_subrange_type:
9490 case DW_TAG_typedef:
9491 case DW_TAG_union_type:
9492 return 1;
9493 default:
9494 return 0;
9495 }
9496 }
9497
9498 /* Load all DIEs that are interesting for partial symbols into memory. */
9499
9500 static struct partial_die_info *
9501 load_partial_dies (bfd *abfd, gdb_byte *buffer, gdb_byte *info_ptr,
9502 int building_psymtab, struct dwarf2_cu *cu)
9503 {
9504 struct objfile *objfile = cu->objfile;
9505 struct partial_die_info *part_die;
9506 struct partial_die_info *parent_die, *last_die, *first_die = NULL;
9507 struct abbrev_info *abbrev;
9508 unsigned int bytes_read;
9509 unsigned int load_all = 0;
9510
9511 int nesting_level = 1;
9512
9513 parent_die = NULL;
9514 last_die = NULL;
9515
9516 if (cu->per_cu && cu->per_cu->load_all_dies)
9517 load_all = 1;
9518
9519 cu->partial_dies
9520 = htab_create_alloc_ex (cu->header.length / 12,
9521 partial_die_hash,
9522 partial_die_eq,
9523 NULL,
9524 &cu->comp_unit_obstack,
9525 hashtab_obstack_allocate,
9526 dummy_obstack_deallocate);
9527
9528 part_die = obstack_alloc (&cu->comp_unit_obstack,
9529 sizeof (struct partial_die_info));
9530
9531 while (1)
9532 {
9533 abbrev = peek_die_abbrev (info_ptr, &bytes_read, cu);
9534
9535 /* A NULL abbrev means the end of a series of children. */
9536 if (abbrev == NULL)
9537 {
9538 if (--nesting_level == 0)
9539 {
9540 /* PART_DIE was probably the last thing allocated on the
9541 comp_unit_obstack, so we could call obstack_free
9542 here. We don't do that because the waste is small,
9543 and will be cleaned up when we're done with this
9544 compilation unit. This way, we're also more robust
9545 against other users of the comp_unit_obstack. */
9546 return first_die;
9547 }
9548 info_ptr += bytes_read;
9549 last_die = parent_die;
9550 parent_die = parent_die->die_parent;
9551 continue;
9552 }
9553
9554 /* Check for template arguments. We never save these; if
9555 they're seen, we just mark the parent, and go on our way. */
9556 if (parent_die != NULL
9557 && cu->language == language_cplus
9558 && (abbrev->tag == DW_TAG_template_type_param
9559 || abbrev->tag == DW_TAG_template_value_param))
9560 {
9561 parent_die->has_template_arguments = 1;
9562
9563 if (!load_all)
9564 {
9565 /* We don't need a partial DIE for the template argument. */
9566 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev,
9567 cu);
9568 continue;
9569 }
9570 }
9571
9572 /* We only recurse into subprograms looking for template arguments.
9573 Skip their other children. */
9574 if (!load_all
9575 && cu->language == language_cplus
9576 && parent_die != NULL
9577 && parent_die->tag == DW_TAG_subprogram)
9578 {
9579 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9580 continue;
9581 }
9582
9583 /* Check whether this DIE is interesting enough to save. Normally
9584 we would not be interested in members here, but there may be
9585 later variables referencing them via DW_AT_specification (for
9586 static members). */
9587 if (!load_all
9588 && !is_type_tag_for_partial (abbrev->tag)
9589 && abbrev->tag != DW_TAG_constant
9590 && abbrev->tag != DW_TAG_enumerator
9591 && abbrev->tag != DW_TAG_subprogram
9592 && abbrev->tag != DW_TAG_lexical_block
9593 && abbrev->tag != DW_TAG_variable
9594 && abbrev->tag != DW_TAG_namespace
9595 && abbrev->tag != DW_TAG_module
9596 && abbrev->tag != DW_TAG_member)
9597 {
9598 /* Otherwise we skip to the next sibling, if any. */
9599 info_ptr = skip_one_die (buffer, info_ptr + bytes_read, abbrev, cu);
9600 continue;
9601 }
9602
9603 info_ptr = read_partial_die (part_die, abbrev, bytes_read, abfd,
9604 buffer, info_ptr, cu);
9605
9606 /* This two-pass algorithm for processing partial symbols has a
9607 high cost in cache pressure. Thus, handle some simple cases
9608 here which cover the majority of C partial symbols. DIEs
9609 which neither have specification tags in them, nor could have
9610 specification tags elsewhere pointing at them, can simply be
9611 processed and discarded.
9612
9613 This segment is also optional; scan_partial_symbols and
9614 add_partial_symbol will handle these DIEs if we chain
9615 them in normally. When compilers which do not emit large
9616 quantities of duplicate debug information are more common,
9617 this code can probably be removed. */
9618
9619 /* Any complete simple types at the top level (pretty much all
9620 of them, for a language without namespaces), can be processed
9621 directly. */
9622 if (parent_die == NULL
9623 && part_die->has_specification == 0
9624 && part_die->is_declaration == 0
9625 && ((part_die->tag == DW_TAG_typedef && !part_die->has_children)
9626 || part_die->tag == DW_TAG_base_type
9627 || part_die->tag == DW_TAG_subrange_type))
9628 {
9629 if (building_psymtab && part_die->name != NULL)
9630 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9631 VAR_DOMAIN, LOC_TYPEDEF,
9632 &objfile->static_psymbols,
9633 0, (CORE_ADDR) 0, cu->language, objfile);
9634 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9635 continue;
9636 }
9637
9638 /* The exception for DW_TAG_typedef with has_children above is
9639 a workaround of GCC PR debug/47510. In the case of this complaint
9640 type_name_no_tag_or_error will error on such types later.
9641
9642 GDB skipped children of DW_TAG_typedef by the shortcut above and then
9643 it could not find the child DIEs referenced later, this is checked
9644 above. In correct DWARF DW_TAG_typedef should have no children. */
9645
9646 if (part_die->tag == DW_TAG_typedef && part_die->has_children)
9647 complaint (&symfile_complaints,
9648 _("DW_TAG_typedef has childen - GCC PR debug/47510 bug "
9649 "- DIE at 0x%x [in module %s]"),
9650 part_die->offset, objfile->name);
9651
9652 /* If we're at the second level, and we're an enumerator, and
9653 our parent has no specification (meaning possibly lives in a
9654 namespace elsewhere), then we can add the partial symbol now
9655 instead of queueing it. */
9656 if (part_die->tag == DW_TAG_enumerator
9657 && parent_die != NULL
9658 && parent_die->die_parent == NULL
9659 && parent_die->tag == DW_TAG_enumeration_type
9660 && parent_die->has_specification == 0)
9661 {
9662 if (part_die->name == NULL)
9663 complaint (&symfile_complaints,
9664 _("malformed enumerator DIE ignored"));
9665 else if (building_psymtab)
9666 add_psymbol_to_list (part_die->name, strlen (part_die->name), 0,
9667 VAR_DOMAIN, LOC_CONST,
9668 (cu->language == language_cplus
9669 || cu->language == language_java)
9670 ? &objfile->global_psymbols
9671 : &objfile->static_psymbols,
9672 0, (CORE_ADDR) 0, cu->language, objfile);
9673
9674 info_ptr = locate_pdi_sibling (part_die, buffer, info_ptr, abfd, cu);
9675 continue;
9676 }
9677
9678 /* We'll save this DIE so link it in. */
9679 part_die->die_parent = parent_die;
9680 part_die->die_sibling = NULL;
9681 part_die->die_child = NULL;
9682
9683 if (last_die && last_die == parent_die)
9684 last_die->die_child = part_die;
9685 else if (last_die)
9686 last_die->die_sibling = part_die;
9687
9688 last_die = part_die;
9689
9690 if (first_die == NULL)
9691 first_die = part_die;
9692
9693 /* Maybe add the DIE to the hash table. Not all DIEs that we
9694 find interesting need to be in the hash table, because we
9695 also have the parent/sibling/child chains; only those that we
9696 might refer to by offset later during partial symbol reading.
9697
9698 For now this means things that might have be the target of a
9699 DW_AT_specification, DW_AT_abstract_origin, or
9700 DW_AT_extension. DW_AT_extension will refer only to
9701 namespaces; DW_AT_abstract_origin refers to functions (and
9702 many things under the function DIE, but we do not recurse
9703 into function DIEs during partial symbol reading) and
9704 possibly variables as well; DW_AT_specification refers to
9705 declarations. Declarations ought to have the DW_AT_declaration
9706 flag. It happens that GCC forgets to put it in sometimes, but
9707 only for functions, not for types.
9708
9709 Adding more things than necessary to the hash table is harmless
9710 except for the performance cost. Adding too few will result in
9711 wasted time in find_partial_die, when we reread the compilation
9712 unit with load_all_dies set. */
9713
9714 if (load_all
9715 || abbrev->tag == DW_TAG_constant
9716 || abbrev->tag == DW_TAG_subprogram
9717 || abbrev->tag == DW_TAG_variable
9718 || abbrev->tag == DW_TAG_namespace
9719 || part_die->is_declaration)
9720 {
9721 void **slot;
9722
9723 slot = htab_find_slot_with_hash (cu->partial_dies, part_die,
9724 part_die->offset, INSERT);
9725 *slot = part_die;
9726 }
9727
9728 part_die = obstack_alloc (&cu->comp_unit_obstack,
9729 sizeof (struct partial_die_info));
9730
9731 /* For some DIEs we want to follow their children (if any). For C
9732 we have no reason to follow the children of structures; for other
9733 languages we have to, so that we can get at method physnames
9734 to infer fully qualified class names, for DW_AT_specification,
9735 and for C++ template arguments. For C++, we also look one level
9736 inside functions to find template arguments (if the name of the
9737 function does not already contain the template arguments).
9738
9739 For Ada, we need to scan the children of subprograms and lexical
9740 blocks as well because Ada allows the definition of nested
9741 entities that could be interesting for the debugger, such as
9742 nested subprograms for instance. */
9743 if (last_die->has_children
9744 && (load_all
9745 || last_die->tag == DW_TAG_namespace
9746 || last_die->tag == DW_TAG_module
9747 || last_die->tag == DW_TAG_enumeration_type
9748 || (cu->language == language_cplus
9749 && last_die->tag == DW_TAG_subprogram
9750 && (last_die->name == NULL
9751 || strchr (last_die->name, '<') == NULL))
9752 || (cu->language != language_c
9753 && (last_die->tag == DW_TAG_class_type
9754 || last_die->tag == DW_TAG_interface_type
9755 || last_die->tag == DW_TAG_structure_type
9756 || last_die->tag == DW_TAG_union_type))
9757 || (cu->language == language_ada
9758 && (last_die->tag == DW_TAG_subprogram
9759 || last_die->tag == DW_TAG_lexical_block))))
9760 {
9761 nesting_level++;
9762 parent_die = last_die;
9763 continue;
9764 }
9765
9766 /* Otherwise we skip to the next sibling, if any. */
9767 info_ptr = locate_pdi_sibling (last_die, buffer, info_ptr, abfd, cu);
9768
9769 /* Back to the top, do it again. */
9770 }
9771 }
9772
9773 /* Read a minimal amount of information into the minimal die structure. */
9774
9775 static gdb_byte *
9776 read_partial_die (struct partial_die_info *part_die,
9777 struct abbrev_info *abbrev,
9778 unsigned int abbrev_len, bfd *abfd,
9779 gdb_byte *buffer, gdb_byte *info_ptr,
9780 struct dwarf2_cu *cu)
9781 {
9782 struct objfile *objfile = cu->objfile;
9783 unsigned int i;
9784 struct attribute attr;
9785 int has_low_pc_attr = 0;
9786 int has_high_pc_attr = 0;
9787
9788 memset (part_die, 0, sizeof (struct partial_die_info));
9789
9790 part_die->offset = info_ptr - buffer;
9791
9792 info_ptr += abbrev_len;
9793
9794 if (abbrev == NULL)
9795 return info_ptr;
9796
9797 part_die->tag = abbrev->tag;
9798 part_die->has_children = abbrev->has_children;
9799
9800 for (i = 0; i < abbrev->num_attrs; ++i)
9801 {
9802 info_ptr = read_attribute (&attr, &abbrev->attrs[i], abfd, info_ptr, cu);
9803
9804 /* Store the data if it is of an attribute we want to keep in a
9805 partial symbol table. */
9806 switch (attr.name)
9807 {
9808 case DW_AT_name:
9809 switch (part_die->tag)
9810 {
9811 case DW_TAG_compile_unit:
9812 case DW_TAG_type_unit:
9813 /* Compilation units have a DW_AT_name that is a filename, not
9814 a source language identifier. */
9815 case DW_TAG_enumeration_type:
9816 case DW_TAG_enumerator:
9817 /* These tags always have simple identifiers already; no need
9818 to canonicalize them. */
9819 part_die->name = DW_STRING (&attr);
9820 break;
9821 default:
9822 part_die->name
9823 = dwarf2_canonicalize_name (DW_STRING (&attr), cu,
9824 &objfile->objfile_obstack);
9825 break;
9826 }
9827 break;
9828 case DW_AT_linkage_name:
9829 case DW_AT_MIPS_linkage_name:
9830 /* Note that both forms of linkage name might appear. We
9831 assume they will be the same, and we only store the last
9832 one we see. */
9833 if (cu->language == language_ada)
9834 part_die->name = DW_STRING (&attr);
9835 part_die->linkage_name = DW_STRING (&attr);
9836 break;
9837 case DW_AT_low_pc:
9838 has_low_pc_attr = 1;
9839 part_die->lowpc = DW_ADDR (&attr);
9840 break;
9841 case DW_AT_high_pc:
9842 has_high_pc_attr = 1;
9843 part_die->highpc = DW_ADDR (&attr);
9844 break;
9845 case DW_AT_location:
9846 /* Support the .debug_loc offsets. */
9847 if (attr_form_is_block (&attr))
9848 {
9849 part_die->locdesc = DW_BLOCK (&attr);
9850 }
9851 else if (attr_form_is_section_offset (&attr))
9852 {
9853 dwarf2_complex_location_expr_complaint ();
9854 }
9855 else
9856 {
9857 dwarf2_invalid_attrib_class_complaint ("DW_AT_location",
9858 "partial symbol information");
9859 }
9860 break;
9861 case DW_AT_external:
9862 part_die->is_external = DW_UNSND (&attr);
9863 break;
9864 case DW_AT_declaration:
9865 part_die->is_declaration = DW_UNSND (&attr);
9866 break;
9867 case DW_AT_type:
9868 part_die->has_type = 1;
9869 break;
9870 case DW_AT_abstract_origin:
9871 case DW_AT_specification:
9872 case DW_AT_extension:
9873 part_die->has_specification = 1;
9874 part_die->spec_offset = dwarf2_get_ref_die_offset (&attr);
9875 break;
9876 case DW_AT_sibling:
9877 /* Ignore absolute siblings, they might point outside of
9878 the current compile unit. */
9879 if (attr.form == DW_FORM_ref_addr)
9880 complaint (&symfile_complaints,
9881 _("ignoring absolute DW_AT_sibling"));
9882 else
9883 part_die->sibling = buffer + dwarf2_get_ref_die_offset (&attr);
9884 break;
9885 case DW_AT_byte_size:
9886 part_die->has_byte_size = 1;
9887 break;
9888 case DW_AT_calling_convention:
9889 /* DWARF doesn't provide a way to identify a program's source-level
9890 entry point. DW_AT_calling_convention attributes are only meant
9891 to describe functions' calling conventions.
9892
9893 However, because it's a necessary piece of information in
9894 Fortran, and because DW_CC_program is the only piece of debugging
9895 information whose definition refers to a 'main program' at all,
9896 several compilers have begun marking Fortran main programs with
9897 DW_CC_program --- even when those functions use the standard
9898 calling conventions.
9899
9900 So until DWARF specifies a way to provide this information and
9901 compilers pick up the new representation, we'll support this
9902 practice. */
9903 if (DW_UNSND (&attr) == DW_CC_program
9904 && cu->language == language_fortran)
9905 {
9906 set_main_name (part_die->name);
9907
9908 /* As this DIE has a static linkage the name would be difficult
9909 to look up later. */
9910 language_of_main = language_fortran;
9911 }
9912 break;
9913 case DW_AT_inline:
9914 if (DW_UNSND (&attr) == DW_INL_inlined
9915 || DW_UNSND (&attr) == DW_INL_declared_inlined)
9916 part_die->may_be_inlined = 1;
9917 break;
9918 default:
9919 break;
9920 }
9921 }
9922
9923 if (has_low_pc_attr && has_high_pc_attr)
9924 {
9925 /* When using the GNU linker, .gnu.linkonce. sections are used to
9926 eliminate duplicate copies of functions and vtables and such.
9927 The linker will arbitrarily choose one and discard the others.
9928 The AT_*_pc values for such functions refer to local labels in
9929 these sections. If the section from that file was discarded, the
9930 labels are not in the output, so the relocs get a value of 0.
9931 If this is a discarded function, mark the pc bounds as invalid,
9932 so that GDB will ignore it. */
9933 if (part_die->lowpc == 0 && !dwarf2_per_objfile->has_section_at_zero)
9934 {
9935 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9936
9937 complaint (&symfile_complaints,
9938 _("DW_AT_low_pc %s is zero "
9939 "for DIE at 0x%x [in module %s]"),
9940 paddress (gdbarch, part_die->lowpc),
9941 part_die->offset, objfile->name);
9942 }
9943 /* dwarf2_get_pc_bounds has also the strict low < high requirement. */
9944 else if (part_die->lowpc >= part_die->highpc)
9945 {
9946 struct gdbarch *gdbarch = get_objfile_arch (objfile);
9947
9948 complaint (&symfile_complaints,
9949 _("DW_AT_low_pc %s is not < DW_AT_high_pc %s "
9950 "for DIE at 0x%x [in module %s]"),
9951 paddress (gdbarch, part_die->lowpc),
9952 paddress (gdbarch, part_die->highpc),
9953 part_die->offset, objfile->name);
9954 }
9955 else
9956 part_die->has_pc_info = 1;
9957 }
9958
9959 return info_ptr;
9960 }
9961
9962 /* Find a cached partial DIE at OFFSET in CU. */
9963
9964 static struct partial_die_info *
9965 find_partial_die_in_comp_unit (unsigned int offset, struct dwarf2_cu *cu)
9966 {
9967 struct partial_die_info *lookup_die = NULL;
9968 struct partial_die_info part_die;
9969
9970 part_die.offset = offset;
9971 lookup_die = htab_find_with_hash (cu->partial_dies, &part_die, offset);
9972
9973 return lookup_die;
9974 }
9975
9976 /* Find a partial DIE at OFFSET, which may or may not be in CU,
9977 except in the case of .debug_types DIEs which do not reference
9978 outside their CU (they do however referencing other types via
9979 DW_FORM_ref_sig8). */
9980
9981 static struct partial_die_info *
9982 find_partial_die (unsigned int offset, struct dwarf2_cu *cu)
9983 {
9984 struct objfile *objfile = cu->objfile;
9985 struct dwarf2_per_cu_data *per_cu = NULL;
9986 struct partial_die_info *pd = NULL;
9987
9988 if (cu->per_cu->debug_types_section)
9989 {
9990 pd = find_partial_die_in_comp_unit (offset, cu);
9991 if (pd != NULL)
9992 return pd;
9993 goto not_found;
9994 }
9995
9996 if (offset_in_cu_p (&cu->header, offset))
9997 {
9998 pd = find_partial_die_in_comp_unit (offset, cu);
9999 if (pd != NULL)
10000 return pd;
10001 }
10002
10003 per_cu = dwarf2_find_containing_comp_unit (offset, objfile);
10004
10005 if (per_cu->cu == NULL || per_cu->cu->partial_dies == NULL)
10006 load_partial_comp_unit (per_cu);
10007
10008 per_cu->cu->last_used = 0;
10009 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10010
10011 if (pd == NULL && per_cu->load_all_dies == 0)
10012 {
10013 struct cleanup *back_to;
10014 struct partial_die_info comp_unit_die;
10015 struct abbrev_info *abbrev;
10016 unsigned int bytes_read;
10017 char *info_ptr;
10018
10019 per_cu->load_all_dies = 1;
10020
10021 /* Re-read the DIEs. */
10022 back_to = make_cleanup (null_cleanup, 0);
10023 if (per_cu->cu->dwarf2_abbrevs == NULL)
10024 {
10025 dwarf2_read_abbrevs (per_cu->cu);
10026 make_cleanup (dwarf2_free_abbrev_table, per_cu->cu);
10027 }
10028 info_ptr = (dwarf2_per_objfile->info.buffer
10029 + per_cu->cu->header.offset
10030 + per_cu->cu->header.first_die_offset);
10031 abbrev = peek_die_abbrev (info_ptr, &bytes_read, per_cu->cu);
10032 info_ptr = read_partial_die (&comp_unit_die, abbrev, bytes_read,
10033 objfile->obfd,
10034 dwarf2_per_objfile->info.buffer, info_ptr,
10035 per_cu->cu);
10036 if (comp_unit_die.has_children)
10037 load_partial_dies (objfile->obfd,
10038 dwarf2_per_objfile->info.buffer, info_ptr,
10039 0, per_cu->cu);
10040 do_cleanups (back_to);
10041
10042 pd = find_partial_die_in_comp_unit (offset, per_cu->cu);
10043 }
10044
10045 not_found:
10046
10047 if (pd == NULL)
10048 internal_error (__FILE__, __LINE__,
10049 _("could not find partial DIE 0x%x "
10050 "in cache [from module %s]\n"),
10051 offset, bfd_get_filename (objfile->obfd));
10052 return pd;
10053 }
10054
10055 /* See if we can figure out if the class lives in a namespace. We do
10056 this by looking for a member function; its demangled name will
10057 contain namespace info, if there is any. */
10058
10059 static void
10060 guess_partial_die_structure_name (struct partial_die_info *struct_pdi,
10061 struct dwarf2_cu *cu)
10062 {
10063 /* NOTE: carlton/2003-10-07: Getting the info this way changes
10064 what template types look like, because the demangler
10065 frequently doesn't give the same name as the debug info. We
10066 could fix this by only using the demangled name to get the
10067 prefix (but see comment in read_structure_type). */
10068
10069 struct partial_die_info *real_pdi;
10070 struct partial_die_info *child_pdi;
10071
10072 /* If this DIE (this DIE's specification, if any) has a parent, then
10073 we should not do this. We'll prepend the parent's fully qualified
10074 name when we create the partial symbol. */
10075
10076 real_pdi = struct_pdi;
10077 while (real_pdi->has_specification)
10078 real_pdi = find_partial_die (real_pdi->spec_offset, cu);
10079
10080 if (real_pdi->die_parent != NULL)
10081 return;
10082
10083 for (child_pdi = struct_pdi->die_child;
10084 child_pdi != NULL;
10085 child_pdi = child_pdi->die_sibling)
10086 {
10087 if (child_pdi->tag == DW_TAG_subprogram
10088 && child_pdi->linkage_name != NULL)
10089 {
10090 char *actual_class_name
10091 = language_class_name_from_physname (cu->language_defn,
10092 child_pdi->linkage_name);
10093 if (actual_class_name != NULL)
10094 {
10095 struct_pdi->name
10096 = obsavestring (actual_class_name,
10097 strlen (actual_class_name),
10098 &cu->objfile->objfile_obstack);
10099 xfree (actual_class_name);
10100 }
10101 break;
10102 }
10103 }
10104 }
10105
10106 /* Adjust PART_DIE before generating a symbol for it. This function
10107 may set the is_external flag or change the DIE's name. */
10108
10109 static void
10110 fixup_partial_die (struct partial_die_info *part_die,
10111 struct dwarf2_cu *cu)
10112 {
10113 /* Once we've fixed up a die, there's no point in doing so again.
10114 This also avoids a memory leak if we were to call
10115 guess_partial_die_structure_name multiple times. */
10116 if (part_die->fixup_called)
10117 return;
10118
10119 /* If we found a reference attribute and the DIE has no name, try
10120 to find a name in the referred to DIE. */
10121
10122 if (part_die->name == NULL && part_die->has_specification)
10123 {
10124 struct partial_die_info *spec_die;
10125
10126 spec_die = find_partial_die (part_die->spec_offset, cu);
10127
10128 fixup_partial_die (spec_die, cu);
10129
10130 if (spec_die->name)
10131 {
10132 part_die->name = spec_die->name;
10133
10134 /* Copy DW_AT_external attribute if it is set. */
10135 if (spec_die->is_external)
10136 part_die->is_external = spec_die->is_external;
10137 }
10138 }
10139
10140 /* Set default names for some unnamed DIEs. */
10141
10142 if (part_die->name == NULL && part_die->tag == DW_TAG_namespace)
10143 part_die->name = CP_ANONYMOUS_NAMESPACE_STR;
10144
10145 /* If there is no parent die to provide a namespace, and there are
10146 children, see if we can determine the namespace from their linkage
10147 name. */
10148 if (cu->language == language_cplus
10149 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
10150 && part_die->die_parent == NULL
10151 && part_die->has_children
10152 && (part_die->tag == DW_TAG_class_type
10153 || part_die->tag == DW_TAG_structure_type
10154 || part_die->tag == DW_TAG_union_type))
10155 guess_partial_die_structure_name (part_die, cu);
10156
10157 /* GCC might emit a nameless struct or union that has a linkage
10158 name. See http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
10159 if (part_die->name == NULL
10160 && (part_die->tag == DW_TAG_class_type
10161 || part_die->tag == DW_TAG_interface_type
10162 || part_die->tag == DW_TAG_structure_type
10163 || part_die->tag == DW_TAG_union_type)
10164 && part_die->linkage_name != NULL)
10165 {
10166 char *demangled;
10167
10168 demangled = cplus_demangle (part_die->linkage_name, DMGL_TYPES);
10169 if (demangled)
10170 {
10171 const char *base;
10172
10173 /* Strip any leading namespaces/classes, keep only the base name.
10174 DW_AT_name for named DIEs does not contain the prefixes. */
10175 base = strrchr (demangled, ':');
10176 if (base && base > demangled && base[-1] == ':')
10177 base++;
10178 else
10179 base = demangled;
10180
10181 part_die->name = obsavestring (base, strlen (base),
10182 &cu->objfile->objfile_obstack);
10183 xfree (demangled);
10184 }
10185 }
10186
10187 part_die->fixup_called = 1;
10188 }
10189
10190 /* Read an attribute value described by an attribute form. */
10191
10192 static gdb_byte *
10193 read_attribute_value (struct attribute *attr, unsigned form,
10194 bfd *abfd, gdb_byte *info_ptr,
10195 struct dwarf2_cu *cu)
10196 {
10197 struct comp_unit_head *cu_header = &cu->header;
10198 unsigned int bytes_read;
10199 struct dwarf_block *blk;
10200
10201 attr->form = form;
10202 switch (form)
10203 {
10204 case DW_FORM_ref_addr:
10205 if (cu->header.version == 2)
10206 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10207 else
10208 DW_ADDR (attr) = read_offset (abfd, info_ptr,
10209 &cu->header, &bytes_read);
10210 info_ptr += bytes_read;
10211 break;
10212 case DW_FORM_addr:
10213 DW_ADDR (attr) = read_address (abfd, info_ptr, cu, &bytes_read);
10214 info_ptr += bytes_read;
10215 break;
10216 case DW_FORM_block2:
10217 blk = dwarf_alloc_block (cu);
10218 blk->size = read_2_bytes (abfd, info_ptr);
10219 info_ptr += 2;
10220 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10221 info_ptr += blk->size;
10222 DW_BLOCK (attr) = blk;
10223 break;
10224 case DW_FORM_block4:
10225 blk = dwarf_alloc_block (cu);
10226 blk->size = read_4_bytes (abfd, info_ptr);
10227 info_ptr += 4;
10228 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10229 info_ptr += blk->size;
10230 DW_BLOCK (attr) = blk;
10231 break;
10232 case DW_FORM_data2:
10233 DW_UNSND (attr) = read_2_bytes (abfd, info_ptr);
10234 info_ptr += 2;
10235 break;
10236 case DW_FORM_data4:
10237 DW_UNSND (attr) = read_4_bytes (abfd, info_ptr);
10238 info_ptr += 4;
10239 break;
10240 case DW_FORM_data8:
10241 DW_UNSND (attr) = read_8_bytes (abfd, info_ptr);
10242 info_ptr += 8;
10243 break;
10244 case DW_FORM_sec_offset:
10245 DW_UNSND (attr) = read_offset (abfd, info_ptr, &cu->header, &bytes_read);
10246 info_ptr += bytes_read;
10247 break;
10248 case DW_FORM_string:
10249 DW_STRING (attr) = read_direct_string (abfd, info_ptr, &bytes_read);
10250 DW_STRING_IS_CANONICAL (attr) = 0;
10251 info_ptr += bytes_read;
10252 break;
10253 case DW_FORM_strp:
10254 DW_STRING (attr) = read_indirect_string (abfd, info_ptr, cu_header,
10255 &bytes_read);
10256 DW_STRING_IS_CANONICAL (attr) = 0;
10257 info_ptr += bytes_read;
10258 break;
10259 case DW_FORM_exprloc:
10260 case DW_FORM_block:
10261 blk = dwarf_alloc_block (cu);
10262 blk->size = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10263 info_ptr += bytes_read;
10264 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10265 info_ptr += blk->size;
10266 DW_BLOCK (attr) = blk;
10267 break;
10268 case DW_FORM_block1:
10269 blk = dwarf_alloc_block (cu);
10270 blk->size = read_1_byte (abfd, info_ptr);
10271 info_ptr += 1;
10272 blk->data = read_n_bytes (abfd, info_ptr, blk->size);
10273 info_ptr += blk->size;
10274 DW_BLOCK (attr) = blk;
10275 break;
10276 case DW_FORM_data1:
10277 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10278 info_ptr += 1;
10279 break;
10280 case DW_FORM_flag:
10281 DW_UNSND (attr) = read_1_byte (abfd, info_ptr);
10282 info_ptr += 1;
10283 break;
10284 case DW_FORM_flag_present:
10285 DW_UNSND (attr) = 1;
10286 break;
10287 case DW_FORM_sdata:
10288 DW_SND (attr) = read_signed_leb128 (abfd, info_ptr, &bytes_read);
10289 info_ptr += bytes_read;
10290 break;
10291 case DW_FORM_udata:
10292 DW_UNSND (attr) = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10293 info_ptr += bytes_read;
10294 break;
10295 case DW_FORM_ref1:
10296 DW_ADDR (attr) = cu->header.offset + read_1_byte (abfd, info_ptr);
10297 info_ptr += 1;
10298 break;
10299 case DW_FORM_ref2:
10300 DW_ADDR (attr) = cu->header.offset + read_2_bytes (abfd, info_ptr);
10301 info_ptr += 2;
10302 break;
10303 case DW_FORM_ref4:
10304 DW_ADDR (attr) = cu->header.offset + read_4_bytes (abfd, info_ptr);
10305 info_ptr += 4;
10306 break;
10307 case DW_FORM_ref8:
10308 DW_ADDR (attr) = cu->header.offset + read_8_bytes (abfd, info_ptr);
10309 info_ptr += 8;
10310 break;
10311 case DW_FORM_ref_sig8:
10312 /* Convert the signature to something we can record in DW_UNSND
10313 for later lookup.
10314 NOTE: This is NULL if the type wasn't found. */
10315 DW_SIGNATURED_TYPE (attr) =
10316 lookup_signatured_type (cu->objfile, read_8_bytes (abfd, info_ptr));
10317 info_ptr += 8;
10318 break;
10319 case DW_FORM_ref_udata:
10320 DW_ADDR (attr) = (cu->header.offset
10321 + read_unsigned_leb128 (abfd, info_ptr, &bytes_read));
10322 info_ptr += bytes_read;
10323 break;
10324 case DW_FORM_indirect:
10325 form = read_unsigned_leb128 (abfd, info_ptr, &bytes_read);
10326 info_ptr += bytes_read;
10327 info_ptr = read_attribute_value (attr, form, abfd, info_ptr, cu);
10328 break;
10329 default:
10330 error (_("Dwarf Error: Cannot handle %s in DWARF reader [in module %s]"),
10331 dwarf_form_name (form),
10332 bfd_get_filename (abfd));
10333 }
10334
10335 /* We have seen instances where the compiler tried to emit a byte
10336 size attribute of -1 which ended up being encoded as an unsigned
10337 0xffffffff. Although 0xffffffff is technically a valid size value,
10338 an object of this size seems pretty unlikely so we can relatively
10339 safely treat these cases as if the size attribute was invalid and
10340 treat them as zero by default. */
10341 if (attr->name == DW_AT_byte_size
10342 && form == DW_FORM_data4
10343 && DW_UNSND (attr) >= 0xffffffff)
10344 {
10345 complaint
10346 (&symfile_complaints,
10347 _("Suspicious DW_AT_byte_size value treated as zero instead of %s"),
10348 hex_string (DW_UNSND (attr)));
10349 DW_UNSND (attr) = 0;
10350 }
10351
10352 return info_ptr;
10353 }
10354
10355 /* Read an attribute described by an abbreviated attribute. */
10356
10357 static gdb_byte *
10358 read_attribute (struct attribute *attr, struct attr_abbrev *abbrev,
10359 bfd *abfd, gdb_byte *info_ptr, struct dwarf2_cu *cu)
10360 {
10361 attr->name = abbrev->name;
10362 return read_attribute_value (attr, abbrev->form, abfd, info_ptr, cu);
10363 }
10364
10365 /* Read dwarf information from a buffer. */
10366
10367 static unsigned int
10368 read_1_byte (bfd *abfd, gdb_byte *buf)
10369 {
10370 return bfd_get_8 (abfd, buf);
10371 }
10372
10373 static int
10374 read_1_signed_byte (bfd *abfd, gdb_byte *buf)
10375 {
10376 return bfd_get_signed_8 (abfd, buf);
10377 }
10378
10379 static unsigned int
10380 read_2_bytes (bfd *abfd, gdb_byte *buf)
10381 {
10382 return bfd_get_16 (abfd, buf);
10383 }
10384
10385 static int
10386 read_2_signed_bytes (bfd *abfd, gdb_byte *buf)
10387 {
10388 return bfd_get_signed_16 (abfd, buf);
10389 }
10390
10391 static unsigned int
10392 read_4_bytes (bfd *abfd, gdb_byte *buf)
10393 {
10394 return bfd_get_32 (abfd, buf);
10395 }
10396
10397 static int
10398 read_4_signed_bytes (bfd *abfd, gdb_byte *buf)
10399 {
10400 return bfd_get_signed_32 (abfd, buf);
10401 }
10402
10403 static ULONGEST
10404 read_8_bytes (bfd *abfd, gdb_byte *buf)
10405 {
10406 return bfd_get_64 (abfd, buf);
10407 }
10408
10409 static CORE_ADDR
10410 read_address (bfd *abfd, gdb_byte *buf, struct dwarf2_cu *cu,
10411 unsigned int *bytes_read)
10412 {
10413 struct comp_unit_head *cu_header = &cu->header;
10414 CORE_ADDR retval = 0;
10415
10416 if (cu_header->signed_addr_p)
10417 {
10418 switch (cu_header->addr_size)
10419 {
10420 case 2:
10421 retval = bfd_get_signed_16 (abfd, buf);
10422 break;
10423 case 4:
10424 retval = bfd_get_signed_32 (abfd, buf);
10425 break;
10426 case 8:
10427 retval = bfd_get_signed_64 (abfd, buf);
10428 break;
10429 default:
10430 internal_error (__FILE__, __LINE__,
10431 _("read_address: bad switch, signed [in module %s]"),
10432 bfd_get_filename (abfd));
10433 }
10434 }
10435 else
10436 {
10437 switch (cu_header->addr_size)
10438 {
10439 case 2:
10440 retval = bfd_get_16 (abfd, buf);
10441 break;
10442 case 4:
10443 retval = bfd_get_32 (abfd, buf);
10444 break;
10445 case 8:
10446 retval = bfd_get_64 (abfd, buf);
10447 break;
10448 default:
10449 internal_error (__FILE__, __LINE__,
10450 _("read_address: bad switch, "
10451 "unsigned [in module %s]"),
10452 bfd_get_filename (abfd));
10453 }
10454 }
10455
10456 *bytes_read = cu_header->addr_size;
10457 return retval;
10458 }
10459
10460 /* Read the initial length from a section. The (draft) DWARF 3
10461 specification allows the initial length to take up either 4 bytes
10462 or 12 bytes. If the first 4 bytes are 0xffffffff, then the next 8
10463 bytes describe the length and all offsets will be 8 bytes in length
10464 instead of 4.
10465
10466 An older, non-standard 64-bit format is also handled by this
10467 function. The older format in question stores the initial length
10468 as an 8-byte quantity without an escape value. Lengths greater
10469 than 2^32 aren't very common which means that the initial 4 bytes
10470 is almost always zero. Since a length value of zero doesn't make
10471 sense for the 32-bit format, this initial zero can be considered to
10472 be an escape value which indicates the presence of the older 64-bit
10473 format. As written, the code can't detect (old format) lengths
10474 greater than 4GB. If it becomes necessary to handle lengths
10475 somewhat larger than 4GB, we could allow other small values (such
10476 as the non-sensical values of 1, 2, and 3) to also be used as
10477 escape values indicating the presence of the old format.
10478
10479 The value returned via bytes_read should be used to increment the
10480 relevant pointer after calling read_initial_length().
10481
10482 [ Note: read_initial_length() and read_offset() are based on the
10483 document entitled "DWARF Debugging Information Format", revision
10484 3, draft 8, dated November 19, 2001. This document was obtained
10485 from:
10486
10487 http://reality.sgiweb.org/davea/dwarf3-draft8-011125.pdf
10488
10489 This document is only a draft and is subject to change. (So beware.)
10490
10491 Details regarding the older, non-standard 64-bit format were
10492 determined empirically by examining 64-bit ELF files produced by
10493 the SGI toolchain on an IRIX 6.5 machine.
10494
10495 - Kevin, July 16, 2002
10496 ] */
10497
10498 static LONGEST
10499 read_initial_length (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read)
10500 {
10501 LONGEST length = bfd_get_32 (abfd, buf);
10502
10503 if (length == 0xffffffff)
10504 {
10505 length = bfd_get_64 (abfd, buf + 4);
10506 *bytes_read = 12;
10507 }
10508 else if (length == 0)
10509 {
10510 /* Handle the (non-standard) 64-bit DWARF2 format used by IRIX. */
10511 length = bfd_get_64 (abfd, buf);
10512 *bytes_read = 8;
10513 }
10514 else
10515 {
10516 *bytes_read = 4;
10517 }
10518
10519 return length;
10520 }
10521
10522 /* Cover function for read_initial_length.
10523 Returns the length of the object at BUF, and stores the size of the
10524 initial length in *BYTES_READ and stores the size that offsets will be in
10525 *OFFSET_SIZE.
10526 If the initial length size is not equivalent to that specified in
10527 CU_HEADER then issue a complaint.
10528 This is useful when reading non-comp-unit headers. */
10529
10530 static LONGEST
10531 read_checked_initial_length_and_offset (bfd *abfd, gdb_byte *buf,
10532 const struct comp_unit_head *cu_header,
10533 unsigned int *bytes_read,
10534 unsigned int *offset_size)
10535 {
10536 LONGEST length = read_initial_length (abfd, buf, bytes_read);
10537
10538 gdb_assert (cu_header->initial_length_size == 4
10539 || cu_header->initial_length_size == 8
10540 || cu_header->initial_length_size == 12);
10541
10542 if (cu_header->initial_length_size != *bytes_read)
10543 complaint (&symfile_complaints,
10544 _("intermixed 32-bit and 64-bit DWARF sections"));
10545
10546 *offset_size = (*bytes_read == 4) ? 4 : 8;
10547 return length;
10548 }
10549
10550 /* Read an offset from the data stream. The size of the offset is
10551 given by cu_header->offset_size. */
10552
10553 static LONGEST
10554 read_offset (bfd *abfd, gdb_byte *buf, const struct comp_unit_head *cu_header,
10555 unsigned int *bytes_read)
10556 {
10557 LONGEST offset = read_offset_1 (abfd, buf, cu_header->offset_size);
10558
10559 *bytes_read = cu_header->offset_size;
10560 return offset;
10561 }
10562
10563 /* Read an offset from the data stream. */
10564
10565 static LONGEST
10566 read_offset_1 (bfd *abfd, gdb_byte *buf, unsigned int offset_size)
10567 {
10568 LONGEST retval = 0;
10569
10570 switch (offset_size)
10571 {
10572 case 4:
10573 retval = bfd_get_32 (abfd, buf);
10574 break;
10575 case 8:
10576 retval = bfd_get_64 (abfd, buf);
10577 break;
10578 default:
10579 internal_error (__FILE__, __LINE__,
10580 _("read_offset_1: bad switch [in module %s]"),
10581 bfd_get_filename (abfd));
10582 }
10583
10584 return retval;
10585 }
10586
10587 static gdb_byte *
10588 read_n_bytes (bfd *abfd, gdb_byte *buf, unsigned int size)
10589 {
10590 /* If the size of a host char is 8 bits, we can return a pointer
10591 to the buffer, otherwise we have to copy the data to a buffer
10592 allocated on the temporary obstack. */
10593 gdb_assert (HOST_CHAR_BIT == 8);
10594 return buf;
10595 }
10596
10597 static char *
10598 read_direct_string (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10599 {
10600 /* If the size of a host char is 8 bits, we can return a pointer
10601 to the string, otherwise we have to copy the string to a buffer
10602 allocated on the temporary obstack. */
10603 gdb_assert (HOST_CHAR_BIT == 8);
10604 if (*buf == '\0')
10605 {
10606 *bytes_read_ptr = 1;
10607 return NULL;
10608 }
10609 *bytes_read_ptr = strlen ((char *) buf) + 1;
10610 return (char *) buf;
10611 }
10612
10613 static char *
10614 read_indirect_string_at_offset (bfd *abfd, LONGEST str_offset)
10615 {
10616 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->str);
10617 if (dwarf2_per_objfile->str.buffer == NULL)
10618 error (_("DW_FORM_strp used without .debug_str section [in module %s]"),
10619 bfd_get_filename (abfd));
10620 if (str_offset >= dwarf2_per_objfile->str.size)
10621 error (_("DW_FORM_strp pointing outside of "
10622 ".debug_str section [in module %s]"),
10623 bfd_get_filename (abfd));
10624 gdb_assert (HOST_CHAR_BIT == 8);
10625 if (dwarf2_per_objfile->str.buffer[str_offset] == '\0')
10626 return NULL;
10627 return (char *) (dwarf2_per_objfile->str.buffer + str_offset);
10628 }
10629
10630 static char *
10631 read_indirect_string (bfd *abfd, gdb_byte *buf,
10632 const struct comp_unit_head *cu_header,
10633 unsigned int *bytes_read_ptr)
10634 {
10635 LONGEST str_offset = read_offset (abfd, buf, cu_header, bytes_read_ptr);
10636
10637 return read_indirect_string_at_offset (abfd, str_offset);
10638 }
10639
10640 static unsigned long
10641 read_unsigned_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10642 {
10643 unsigned long result;
10644 unsigned int num_read;
10645 int i, shift;
10646 unsigned char byte;
10647
10648 result = 0;
10649 shift = 0;
10650 num_read = 0;
10651 i = 0;
10652 while (1)
10653 {
10654 byte = bfd_get_8 (abfd, buf);
10655 buf++;
10656 num_read++;
10657 result |= ((unsigned long)(byte & 127) << shift);
10658 if ((byte & 128) == 0)
10659 {
10660 break;
10661 }
10662 shift += 7;
10663 }
10664 *bytes_read_ptr = num_read;
10665 return result;
10666 }
10667
10668 static long
10669 read_signed_leb128 (bfd *abfd, gdb_byte *buf, unsigned int *bytes_read_ptr)
10670 {
10671 long result;
10672 int i, shift, num_read;
10673 unsigned char byte;
10674
10675 result = 0;
10676 shift = 0;
10677 num_read = 0;
10678 i = 0;
10679 while (1)
10680 {
10681 byte = bfd_get_8 (abfd, buf);
10682 buf++;
10683 num_read++;
10684 result |= ((long)(byte & 127) << shift);
10685 shift += 7;
10686 if ((byte & 128) == 0)
10687 {
10688 break;
10689 }
10690 }
10691 if ((shift < 8 * sizeof (result)) && (byte & 0x40))
10692 result |= -(((long)1) << shift);
10693 *bytes_read_ptr = num_read;
10694 return result;
10695 }
10696
10697 /* Return a pointer to just past the end of an LEB128 number in BUF. */
10698
10699 static gdb_byte *
10700 skip_leb128 (bfd *abfd, gdb_byte *buf)
10701 {
10702 int byte;
10703
10704 while (1)
10705 {
10706 byte = bfd_get_8 (abfd, buf);
10707 buf++;
10708 if ((byte & 128) == 0)
10709 return buf;
10710 }
10711 }
10712
10713 static void
10714 set_cu_language (unsigned int lang, struct dwarf2_cu *cu)
10715 {
10716 switch (lang)
10717 {
10718 case DW_LANG_C89:
10719 case DW_LANG_C99:
10720 case DW_LANG_C:
10721 cu->language = language_c;
10722 break;
10723 case DW_LANG_C_plus_plus:
10724 cu->language = language_cplus;
10725 break;
10726 case DW_LANG_D:
10727 cu->language = language_d;
10728 break;
10729 case DW_LANG_Fortran77:
10730 case DW_LANG_Fortran90:
10731 case DW_LANG_Fortran95:
10732 cu->language = language_fortran;
10733 break;
10734 case DW_LANG_Mips_Assembler:
10735 cu->language = language_asm;
10736 break;
10737 case DW_LANG_Java:
10738 cu->language = language_java;
10739 break;
10740 case DW_LANG_Ada83:
10741 case DW_LANG_Ada95:
10742 cu->language = language_ada;
10743 break;
10744 case DW_LANG_Modula2:
10745 cu->language = language_m2;
10746 break;
10747 case DW_LANG_Pascal83:
10748 cu->language = language_pascal;
10749 break;
10750 case DW_LANG_ObjC:
10751 cu->language = language_objc;
10752 break;
10753 case DW_LANG_Cobol74:
10754 case DW_LANG_Cobol85:
10755 default:
10756 cu->language = language_minimal;
10757 break;
10758 }
10759 cu->language_defn = language_def (cu->language);
10760 }
10761
10762 /* Return the named attribute or NULL if not there. */
10763
10764 static struct attribute *
10765 dwarf2_attr (struct die_info *die, unsigned int name, struct dwarf2_cu *cu)
10766 {
10767 for (;;)
10768 {
10769 unsigned int i;
10770 struct attribute *spec = NULL;
10771
10772 for (i = 0; i < die->num_attrs; ++i)
10773 {
10774 if (die->attrs[i].name == name)
10775 return &die->attrs[i];
10776 if (die->attrs[i].name == DW_AT_specification
10777 || die->attrs[i].name == DW_AT_abstract_origin)
10778 spec = &die->attrs[i];
10779 }
10780
10781 if (!spec)
10782 break;
10783
10784 die = follow_die_ref (die, spec, &cu);
10785 }
10786
10787 return NULL;
10788 }
10789
10790 /* Return the named attribute or NULL if not there,
10791 but do not follow DW_AT_specification, etc.
10792 This is for use in contexts where we're reading .debug_types dies.
10793 Following DW_AT_specification, DW_AT_abstract_origin will take us
10794 back up the chain, and we want to go down. */
10795
10796 static struct attribute *
10797 dwarf2_attr_no_follow (struct die_info *die, unsigned int name,
10798 struct dwarf2_cu *cu)
10799 {
10800 unsigned int i;
10801
10802 for (i = 0; i < die->num_attrs; ++i)
10803 if (die->attrs[i].name == name)
10804 return &die->attrs[i];
10805
10806 return NULL;
10807 }
10808
10809 /* Return non-zero iff the attribute NAME is defined for the given DIE,
10810 and holds a non-zero value. This function should only be used for
10811 DW_FORM_flag or DW_FORM_flag_present attributes. */
10812
10813 static int
10814 dwarf2_flag_true_p (struct die_info *die, unsigned name, struct dwarf2_cu *cu)
10815 {
10816 struct attribute *attr = dwarf2_attr (die, name, cu);
10817
10818 return (attr && DW_UNSND (attr));
10819 }
10820
10821 static int
10822 die_is_declaration (struct die_info *die, struct dwarf2_cu *cu)
10823 {
10824 /* A DIE is a declaration if it has a DW_AT_declaration attribute
10825 which value is non-zero. However, we have to be careful with
10826 DIEs having a DW_AT_specification attribute, because dwarf2_attr()
10827 (via dwarf2_flag_true_p) follows this attribute. So we may
10828 end up accidently finding a declaration attribute that belongs
10829 to a different DIE referenced by the specification attribute,
10830 even though the given DIE does not have a declaration attribute. */
10831 return (dwarf2_flag_true_p (die, DW_AT_declaration, cu)
10832 && dwarf2_attr (die, DW_AT_specification, cu) == NULL);
10833 }
10834
10835 /* Return the die giving the specification for DIE, if there is
10836 one. *SPEC_CU is the CU containing DIE on input, and the CU
10837 containing the return value on output. If there is no
10838 specification, but there is an abstract origin, that is
10839 returned. */
10840
10841 static struct die_info *
10842 die_specification (struct die_info *die, struct dwarf2_cu **spec_cu)
10843 {
10844 struct attribute *spec_attr = dwarf2_attr (die, DW_AT_specification,
10845 *spec_cu);
10846
10847 if (spec_attr == NULL)
10848 spec_attr = dwarf2_attr (die, DW_AT_abstract_origin, *spec_cu);
10849
10850 if (spec_attr == NULL)
10851 return NULL;
10852 else
10853 return follow_die_ref (die, spec_attr, spec_cu);
10854 }
10855
10856 /* Free the line_header structure *LH, and any arrays and strings it
10857 refers to.
10858 NOTE: This is also used as a "cleanup" function. */
10859
10860 static void
10861 free_line_header (struct line_header *lh)
10862 {
10863 if (lh->standard_opcode_lengths)
10864 xfree (lh->standard_opcode_lengths);
10865
10866 /* Remember that all the lh->file_names[i].name pointers are
10867 pointers into debug_line_buffer, and don't need to be freed. */
10868 if (lh->file_names)
10869 xfree (lh->file_names);
10870
10871 /* Similarly for the include directory names. */
10872 if (lh->include_dirs)
10873 xfree (lh->include_dirs);
10874
10875 xfree (lh);
10876 }
10877
10878 /* Add an entry to LH's include directory table. */
10879
10880 static void
10881 add_include_dir (struct line_header *lh, char *include_dir)
10882 {
10883 /* Grow the array if necessary. */
10884 if (lh->include_dirs_size == 0)
10885 {
10886 lh->include_dirs_size = 1; /* for testing */
10887 lh->include_dirs = xmalloc (lh->include_dirs_size
10888 * sizeof (*lh->include_dirs));
10889 }
10890 else if (lh->num_include_dirs >= lh->include_dirs_size)
10891 {
10892 lh->include_dirs_size *= 2;
10893 lh->include_dirs = xrealloc (lh->include_dirs,
10894 (lh->include_dirs_size
10895 * sizeof (*lh->include_dirs)));
10896 }
10897
10898 lh->include_dirs[lh->num_include_dirs++] = include_dir;
10899 }
10900
10901 /* Add an entry to LH's file name table. */
10902
10903 static void
10904 add_file_name (struct line_header *lh,
10905 char *name,
10906 unsigned int dir_index,
10907 unsigned int mod_time,
10908 unsigned int length)
10909 {
10910 struct file_entry *fe;
10911
10912 /* Grow the array if necessary. */
10913 if (lh->file_names_size == 0)
10914 {
10915 lh->file_names_size = 1; /* for testing */
10916 lh->file_names = xmalloc (lh->file_names_size
10917 * sizeof (*lh->file_names));
10918 }
10919 else if (lh->num_file_names >= lh->file_names_size)
10920 {
10921 lh->file_names_size *= 2;
10922 lh->file_names = xrealloc (lh->file_names,
10923 (lh->file_names_size
10924 * sizeof (*lh->file_names)));
10925 }
10926
10927 fe = &lh->file_names[lh->num_file_names++];
10928 fe->name = name;
10929 fe->dir_index = dir_index;
10930 fe->mod_time = mod_time;
10931 fe->length = length;
10932 fe->included_p = 0;
10933 fe->symtab = NULL;
10934 }
10935
10936 /* Read the statement program header starting at OFFSET in
10937 .debug_line, according to the endianness of ABFD. Return a pointer
10938 to a struct line_header, allocated using xmalloc.
10939
10940 NOTE: the strings in the include directory and file name tables of
10941 the returned object point into debug_line_buffer, and must not be
10942 freed. */
10943
10944 static struct line_header *
10945 dwarf_decode_line_header (unsigned int offset, bfd *abfd,
10946 struct dwarf2_cu *cu)
10947 {
10948 struct cleanup *back_to;
10949 struct line_header *lh;
10950 gdb_byte *line_ptr;
10951 unsigned int bytes_read, offset_size;
10952 int i;
10953 char *cur_dir, *cur_file;
10954
10955 dwarf2_read_section (dwarf2_per_objfile->objfile, &dwarf2_per_objfile->line);
10956 if (dwarf2_per_objfile->line.buffer == NULL)
10957 {
10958 complaint (&symfile_complaints, _("missing .debug_line section"));
10959 return 0;
10960 }
10961
10962 /* Make sure that at least there's room for the total_length field.
10963 That could be 12 bytes long, but we're just going to fudge that. */
10964 if (offset + 4 >= dwarf2_per_objfile->line.size)
10965 {
10966 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10967 return 0;
10968 }
10969
10970 lh = xmalloc (sizeof (*lh));
10971 memset (lh, 0, sizeof (*lh));
10972 back_to = make_cleanup ((make_cleanup_ftype *) free_line_header,
10973 (void *) lh);
10974
10975 line_ptr = dwarf2_per_objfile->line.buffer + offset;
10976
10977 /* Read in the header. */
10978 lh->total_length =
10979 read_checked_initial_length_and_offset (abfd, line_ptr, &cu->header,
10980 &bytes_read, &offset_size);
10981 line_ptr += bytes_read;
10982 if (line_ptr + lh->total_length > (dwarf2_per_objfile->line.buffer
10983 + dwarf2_per_objfile->line.size))
10984 {
10985 dwarf2_statement_list_fits_in_line_number_section_complaint ();
10986 return 0;
10987 }
10988 lh->statement_program_end = line_ptr + lh->total_length;
10989 lh->version = read_2_bytes (abfd, line_ptr);
10990 line_ptr += 2;
10991 lh->header_length = read_offset_1 (abfd, line_ptr, offset_size);
10992 line_ptr += offset_size;
10993 lh->minimum_instruction_length = read_1_byte (abfd, line_ptr);
10994 line_ptr += 1;
10995 if (lh->version >= 4)
10996 {
10997 lh->maximum_ops_per_instruction = read_1_byte (abfd, line_ptr);
10998 line_ptr += 1;
10999 }
11000 else
11001 lh->maximum_ops_per_instruction = 1;
11002
11003 if (lh->maximum_ops_per_instruction == 0)
11004 {
11005 lh->maximum_ops_per_instruction = 1;
11006 complaint (&symfile_complaints,
11007 _("invalid maximum_ops_per_instruction "
11008 "in `.debug_line' section"));
11009 }
11010
11011 lh->default_is_stmt = read_1_byte (abfd, line_ptr);
11012 line_ptr += 1;
11013 lh->line_base = read_1_signed_byte (abfd, line_ptr);
11014 line_ptr += 1;
11015 lh->line_range = read_1_byte (abfd, line_ptr);
11016 line_ptr += 1;
11017 lh->opcode_base = read_1_byte (abfd, line_ptr);
11018 line_ptr += 1;
11019 lh->standard_opcode_lengths
11020 = xmalloc (lh->opcode_base * sizeof (lh->standard_opcode_lengths[0]));
11021
11022 lh->standard_opcode_lengths[0] = 1; /* This should never be used anyway. */
11023 for (i = 1; i < lh->opcode_base; ++i)
11024 {
11025 lh->standard_opcode_lengths[i] = read_1_byte (abfd, line_ptr);
11026 line_ptr += 1;
11027 }
11028
11029 /* Read directory table. */
11030 while ((cur_dir = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11031 {
11032 line_ptr += bytes_read;
11033 add_include_dir (lh, cur_dir);
11034 }
11035 line_ptr += bytes_read;
11036
11037 /* Read file name table. */
11038 while ((cur_file = read_direct_string (abfd, line_ptr, &bytes_read)) != NULL)
11039 {
11040 unsigned int dir_index, mod_time, length;
11041
11042 line_ptr += bytes_read;
11043 dir_index = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11044 line_ptr += bytes_read;
11045 mod_time = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11046 line_ptr += bytes_read;
11047 length = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11048 line_ptr += bytes_read;
11049
11050 add_file_name (lh, cur_file, dir_index, mod_time, length);
11051 }
11052 line_ptr += bytes_read;
11053 lh->statement_program_start = line_ptr;
11054
11055 if (line_ptr > (dwarf2_per_objfile->line.buffer
11056 + dwarf2_per_objfile->line.size))
11057 complaint (&symfile_complaints,
11058 _("line number info header doesn't "
11059 "fit in `.debug_line' section"));
11060
11061 discard_cleanups (back_to);
11062 return lh;
11063 }
11064
11065 /* Subroutine of dwarf_decode_lines to simplify it.
11066 Return the file name of the psymtab for included file FILE_INDEX
11067 in line header LH of PST.
11068 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11069 If space for the result is malloc'd, it will be freed by a cleanup.
11070 Returns NULL if FILE_INDEX should be ignored, i.e., it is pst->filename. */
11071
11072 static char *
11073 psymtab_include_file_name (const struct line_header *lh, int file_index,
11074 const struct partial_symtab *pst,
11075 const char *comp_dir)
11076 {
11077 const struct file_entry fe = lh->file_names [file_index];
11078 char *include_name = fe.name;
11079 char *include_name_to_compare = include_name;
11080 char *dir_name = NULL;
11081 const char *pst_filename;
11082 char *copied_name = NULL;
11083 int file_is_pst;
11084
11085 if (fe.dir_index)
11086 dir_name = lh->include_dirs[fe.dir_index - 1];
11087
11088 if (!IS_ABSOLUTE_PATH (include_name)
11089 && (dir_name != NULL || comp_dir != NULL))
11090 {
11091 /* Avoid creating a duplicate psymtab for PST.
11092 We do this by comparing INCLUDE_NAME and PST_FILENAME.
11093 Before we do the comparison, however, we need to account
11094 for DIR_NAME and COMP_DIR.
11095 First prepend dir_name (if non-NULL). If we still don't
11096 have an absolute path prepend comp_dir (if non-NULL).
11097 However, the directory we record in the include-file's
11098 psymtab does not contain COMP_DIR (to match the
11099 corresponding symtab(s)).
11100
11101 Example:
11102
11103 bash$ cd /tmp
11104 bash$ gcc -g ./hello.c
11105 include_name = "hello.c"
11106 dir_name = "."
11107 DW_AT_comp_dir = comp_dir = "/tmp"
11108 DW_AT_name = "./hello.c" */
11109
11110 if (dir_name != NULL)
11111 {
11112 include_name = concat (dir_name, SLASH_STRING,
11113 include_name, (char *)NULL);
11114 include_name_to_compare = include_name;
11115 make_cleanup (xfree, include_name);
11116 }
11117 if (!IS_ABSOLUTE_PATH (include_name) && comp_dir != NULL)
11118 {
11119 include_name_to_compare = concat (comp_dir, SLASH_STRING,
11120 include_name, (char *)NULL);
11121 }
11122 }
11123
11124 pst_filename = pst->filename;
11125 if (!IS_ABSOLUTE_PATH (pst_filename) && pst->dirname != NULL)
11126 {
11127 copied_name = concat (pst->dirname, SLASH_STRING,
11128 pst_filename, (char *)NULL);
11129 pst_filename = copied_name;
11130 }
11131
11132 file_is_pst = FILENAME_CMP (include_name_to_compare, pst_filename) == 0;
11133
11134 if (include_name_to_compare != include_name)
11135 xfree (include_name_to_compare);
11136 if (copied_name != NULL)
11137 xfree (copied_name);
11138
11139 if (file_is_pst)
11140 return NULL;
11141 return include_name;
11142 }
11143
11144 /* Ignore this record_line request. */
11145
11146 static void
11147 noop_record_line (struct subfile *subfile, int line, CORE_ADDR pc)
11148 {
11149 return;
11150 }
11151
11152 /* Subroutine of dwarf_decode_lines to simplify it.
11153 Process the line number information in LH. */
11154
11155 static void
11156 dwarf_decode_lines_1 (struct line_header *lh, const char *comp_dir,
11157 struct dwarf2_cu *cu, struct partial_symtab *pst)
11158 {
11159 gdb_byte *line_ptr, *extended_end;
11160 gdb_byte *line_end;
11161 unsigned int bytes_read, extended_len;
11162 unsigned char op_code, extended_op, adj_opcode;
11163 CORE_ADDR baseaddr;
11164 struct objfile *objfile = cu->objfile;
11165 bfd *abfd = objfile->obfd;
11166 struct gdbarch *gdbarch = get_objfile_arch (objfile);
11167 const int decode_for_pst_p = (pst != NULL);
11168 struct subfile *last_subfile = NULL;
11169 void (*p_record_line) (struct subfile *subfile, int line, CORE_ADDR pc)
11170 = record_line;
11171
11172 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11173
11174 line_ptr = lh->statement_program_start;
11175 line_end = lh->statement_program_end;
11176
11177 /* Read the statement sequences until there's nothing left. */
11178 while (line_ptr < line_end)
11179 {
11180 /* state machine registers */
11181 CORE_ADDR address = 0;
11182 unsigned int file = 1;
11183 unsigned int line = 1;
11184 unsigned int column = 0;
11185 int is_stmt = lh->default_is_stmt;
11186 int basic_block = 0;
11187 int end_sequence = 0;
11188 CORE_ADDR addr;
11189 unsigned char op_index = 0;
11190
11191 if (!decode_for_pst_p && lh->num_file_names >= file)
11192 {
11193 /* Start a subfile for the current file of the state machine. */
11194 /* lh->include_dirs and lh->file_names are 0-based, but the
11195 directory and file name numbers in the statement program
11196 are 1-based. */
11197 struct file_entry *fe = &lh->file_names[file - 1];
11198 char *dir = NULL;
11199
11200 if (fe->dir_index)
11201 dir = lh->include_dirs[fe->dir_index - 1];
11202
11203 dwarf2_start_subfile (fe->name, dir, comp_dir);
11204 }
11205
11206 /* Decode the table. */
11207 while (!end_sequence)
11208 {
11209 op_code = read_1_byte (abfd, line_ptr);
11210 line_ptr += 1;
11211 if (line_ptr > line_end)
11212 {
11213 dwarf2_debug_line_missing_end_sequence_complaint ();
11214 break;
11215 }
11216
11217 if (op_code >= lh->opcode_base)
11218 {
11219 /* Special operand. */
11220 adj_opcode = op_code - lh->opcode_base;
11221 address += (((op_index + (adj_opcode / lh->line_range))
11222 / lh->maximum_ops_per_instruction)
11223 * lh->minimum_instruction_length);
11224 op_index = ((op_index + (adj_opcode / lh->line_range))
11225 % lh->maximum_ops_per_instruction);
11226 line += lh->line_base + (adj_opcode % lh->line_range);
11227 if (lh->num_file_names < file || file == 0)
11228 dwarf2_debug_line_missing_file_complaint ();
11229 /* For now we ignore lines not starting on an
11230 instruction boundary. */
11231 else if (op_index == 0)
11232 {
11233 lh->file_names[file - 1].included_p = 1;
11234 if (!decode_for_pst_p && is_stmt)
11235 {
11236 if (last_subfile != current_subfile)
11237 {
11238 addr = gdbarch_addr_bits_remove (gdbarch, address);
11239 if (last_subfile)
11240 (*p_record_line) (last_subfile, 0, addr);
11241 last_subfile = current_subfile;
11242 }
11243 /* Append row to matrix using current values. */
11244 addr = gdbarch_addr_bits_remove (gdbarch, address);
11245 (*p_record_line) (current_subfile, line, addr);
11246 }
11247 }
11248 basic_block = 0;
11249 }
11250 else switch (op_code)
11251 {
11252 case DW_LNS_extended_op:
11253 extended_len = read_unsigned_leb128 (abfd, line_ptr,
11254 &bytes_read);
11255 line_ptr += bytes_read;
11256 extended_end = line_ptr + extended_len;
11257 extended_op = read_1_byte (abfd, line_ptr);
11258 line_ptr += 1;
11259 switch (extended_op)
11260 {
11261 case DW_LNE_end_sequence:
11262 p_record_line = record_line;
11263 end_sequence = 1;
11264 break;
11265 case DW_LNE_set_address:
11266 address = read_address (abfd, line_ptr, cu, &bytes_read);
11267
11268 if (address == 0 && !dwarf2_per_objfile->has_section_at_zero)
11269 {
11270 /* This line table is for a function which has been
11271 GCd by the linker. Ignore it. PR gdb/12528 */
11272
11273 long line_offset
11274 = line_ptr - dwarf2_per_objfile->line.buffer;
11275
11276 complaint (&symfile_complaints,
11277 _(".debug_line address at offset 0x%lx is 0 "
11278 "[in module %s]"),
11279 line_offset, objfile->name);
11280 p_record_line = noop_record_line;
11281 }
11282
11283 op_index = 0;
11284 line_ptr += bytes_read;
11285 address += baseaddr;
11286 break;
11287 case DW_LNE_define_file:
11288 {
11289 char *cur_file;
11290 unsigned int dir_index, mod_time, length;
11291
11292 cur_file = read_direct_string (abfd, line_ptr,
11293 &bytes_read);
11294 line_ptr += bytes_read;
11295 dir_index =
11296 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11297 line_ptr += bytes_read;
11298 mod_time =
11299 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11300 line_ptr += bytes_read;
11301 length =
11302 read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11303 line_ptr += bytes_read;
11304 add_file_name (lh, cur_file, dir_index, mod_time, length);
11305 }
11306 break;
11307 case DW_LNE_set_discriminator:
11308 /* The discriminator is not interesting to the debugger;
11309 just ignore it. */
11310 line_ptr = extended_end;
11311 break;
11312 default:
11313 complaint (&symfile_complaints,
11314 _("mangled .debug_line section"));
11315 return;
11316 }
11317 /* Make sure that we parsed the extended op correctly. If e.g.
11318 we expected a different address size than the producer used,
11319 we may have read the wrong number of bytes. */
11320 if (line_ptr != extended_end)
11321 {
11322 complaint (&symfile_complaints,
11323 _("mangled .debug_line section"));
11324 return;
11325 }
11326 break;
11327 case DW_LNS_copy:
11328 if (lh->num_file_names < file || file == 0)
11329 dwarf2_debug_line_missing_file_complaint ();
11330 else
11331 {
11332 lh->file_names[file - 1].included_p = 1;
11333 if (!decode_for_pst_p && is_stmt)
11334 {
11335 if (last_subfile != current_subfile)
11336 {
11337 addr = gdbarch_addr_bits_remove (gdbarch, address);
11338 if (last_subfile)
11339 (*p_record_line) (last_subfile, 0, addr);
11340 last_subfile = current_subfile;
11341 }
11342 addr = gdbarch_addr_bits_remove (gdbarch, address);
11343 (*p_record_line) (current_subfile, line, addr);
11344 }
11345 }
11346 basic_block = 0;
11347 break;
11348 case DW_LNS_advance_pc:
11349 {
11350 CORE_ADDR adjust
11351 = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11352
11353 address += (((op_index + adjust)
11354 / lh->maximum_ops_per_instruction)
11355 * lh->minimum_instruction_length);
11356 op_index = ((op_index + adjust)
11357 % lh->maximum_ops_per_instruction);
11358 line_ptr += bytes_read;
11359 }
11360 break;
11361 case DW_LNS_advance_line:
11362 line += read_signed_leb128 (abfd, line_ptr, &bytes_read);
11363 line_ptr += bytes_read;
11364 break;
11365 case DW_LNS_set_file:
11366 {
11367 /* The arrays lh->include_dirs and lh->file_names are
11368 0-based, but the directory and file name numbers in
11369 the statement program are 1-based. */
11370 struct file_entry *fe;
11371 char *dir = NULL;
11372
11373 file = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11374 line_ptr += bytes_read;
11375 if (lh->num_file_names < file || file == 0)
11376 dwarf2_debug_line_missing_file_complaint ();
11377 else
11378 {
11379 fe = &lh->file_names[file - 1];
11380 if (fe->dir_index)
11381 dir = lh->include_dirs[fe->dir_index - 1];
11382 if (!decode_for_pst_p)
11383 {
11384 last_subfile = current_subfile;
11385 dwarf2_start_subfile (fe->name, dir, comp_dir);
11386 }
11387 }
11388 }
11389 break;
11390 case DW_LNS_set_column:
11391 column = read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11392 line_ptr += bytes_read;
11393 break;
11394 case DW_LNS_negate_stmt:
11395 is_stmt = (!is_stmt);
11396 break;
11397 case DW_LNS_set_basic_block:
11398 basic_block = 1;
11399 break;
11400 /* Add to the address register of the state machine the
11401 address increment value corresponding to special opcode
11402 255. I.e., this value is scaled by the minimum
11403 instruction length since special opcode 255 would have
11404 scaled the increment. */
11405 case DW_LNS_const_add_pc:
11406 {
11407 CORE_ADDR adjust = (255 - lh->opcode_base) / lh->line_range;
11408
11409 address += (((op_index + adjust)
11410 / lh->maximum_ops_per_instruction)
11411 * lh->minimum_instruction_length);
11412 op_index = ((op_index + adjust)
11413 % lh->maximum_ops_per_instruction);
11414 }
11415 break;
11416 case DW_LNS_fixed_advance_pc:
11417 address += read_2_bytes (abfd, line_ptr);
11418 op_index = 0;
11419 line_ptr += 2;
11420 break;
11421 default:
11422 {
11423 /* Unknown standard opcode, ignore it. */
11424 int i;
11425
11426 for (i = 0; i < lh->standard_opcode_lengths[op_code]; i++)
11427 {
11428 (void) read_unsigned_leb128 (abfd, line_ptr, &bytes_read);
11429 line_ptr += bytes_read;
11430 }
11431 }
11432 }
11433 }
11434 if (lh->num_file_names < file || file == 0)
11435 dwarf2_debug_line_missing_file_complaint ();
11436 else
11437 {
11438 lh->file_names[file - 1].included_p = 1;
11439 if (!decode_for_pst_p)
11440 {
11441 addr = gdbarch_addr_bits_remove (gdbarch, address);
11442 (*p_record_line) (current_subfile, 0, addr);
11443 }
11444 }
11445 }
11446 }
11447
11448 /* Decode the Line Number Program (LNP) for the given line_header
11449 structure and CU. The actual information extracted and the type
11450 of structures created from the LNP depends on the value of PST.
11451
11452 1. If PST is NULL, then this procedure uses the data from the program
11453 to create all necessary symbol tables, and their linetables.
11454
11455 2. If PST is not NULL, this procedure reads the program to determine
11456 the list of files included by the unit represented by PST, and
11457 builds all the associated partial symbol tables.
11458
11459 COMP_DIR is the compilation directory (DW_AT_comp_dir) or NULL if unknown.
11460 It is used for relative paths in the line table.
11461 NOTE: When processing partial symtabs (pst != NULL),
11462 comp_dir == pst->dirname.
11463
11464 NOTE: It is important that psymtabs have the same file name (via strcmp)
11465 as the corresponding symtab. Since COMP_DIR is not used in the name of the
11466 symtab we don't use it in the name of the psymtabs we create.
11467 E.g. expand_line_sal requires this when finding psymtabs to expand.
11468 A good testcase for this is mb-inline.exp. */
11469
11470 static void
11471 dwarf_decode_lines (struct line_header *lh, const char *comp_dir,
11472 struct dwarf2_cu *cu, struct partial_symtab *pst,
11473 int want_line_info)
11474 {
11475 struct objfile *objfile = cu->objfile;
11476 const int decode_for_pst_p = (pst != NULL);
11477 struct subfile *first_subfile = current_subfile;
11478
11479 if (want_line_info)
11480 dwarf_decode_lines_1 (lh, comp_dir, cu, pst);
11481
11482 if (decode_for_pst_p)
11483 {
11484 int file_index;
11485
11486 /* Now that we're done scanning the Line Header Program, we can
11487 create the psymtab of each included file. */
11488 for (file_index = 0; file_index < lh->num_file_names; file_index++)
11489 if (lh->file_names[file_index].included_p == 1)
11490 {
11491 char *include_name =
11492 psymtab_include_file_name (lh, file_index, pst, comp_dir);
11493 if (include_name != NULL)
11494 dwarf2_create_include_psymtab (include_name, pst, objfile);
11495 }
11496 }
11497 else
11498 {
11499 /* Make sure a symtab is created for every file, even files
11500 which contain only variables (i.e. no code with associated
11501 line numbers). */
11502 int i;
11503
11504 for (i = 0; i < lh->num_file_names; i++)
11505 {
11506 char *dir = NULL;
11507 struct file_entry *fe;
11508
11509 fe = &lh->file_names[i];
11510 if (fe->dir_index)
11511 dir = lh->include_dirs[fe->dir_index - 1];
11512 dwarf2_start_subfile (fe->name, dir, comp_dir);
11513
11514 /* Skip the main file; we don't need it, and it must be
11515 allocated last, so that it will show up before the
11516 non-primary symtabs in the objfile's symtab list. */
11517 if (current_subfile == first_subfile)
11518 continue;
11519
11520 if (current_subfile->symtab == NULL)
11521 current_subfile->symtab = allocate_symtab (current_subfile->name,
11522 objfile);
11523 fe->symtab = current_subfile->symtab;
11524 }
11525 }
11526 }
11527
11528 /* Start a subfile for DWARF. FILENAME is the name of the file and
11529 DIRNAME the name of the source directory which contains FILENAME
11530 or NULL if not known. COMP_DIR is the compilation directory for the
11531 linetable's compilation unit or NULL if not known.
11532 This routine tries to keep line numbers from identical absolute and
11533 relative file names in a common subfile.
11534
11535 Using the `list' example from the GDB testsuite, which resides in
11536 /srcdir and compiling it with Irix6.2 cc in /compdir using a filename
11537 of /srcdir/list0.c yields the following debugging information for list0.c:
11538
11539 DW_AT_name: /srcdir/list0.c
11540 DW_AT_comp_dir: /compdir
11541 files.files[0].name: list0.h
11542 files.files[0].dir: /srcdir
11543 files.files[1].name: list0.c
11544 files.files[1].dir: /srcdir
11545
11546 The line number information for list0.c has to end up in a single
11547 subfile, so that `break /srcdir/list0.c:1' works as expected.
11548 start_subfile will ensure that this happens provided that we pass the
11549 concatenation of files.files[1].dir and files.files[1].name as the
11550 subfile's name. */
11551
11552 static void
11553 dwarf2_start_subfile (char *filename, const char *dirname,
11554 const char *comp_dir)
11555 {
11556 char *fullname;
11557
11558 /* While reading the DIEs, we call start_symtab(DW_AT_name, DW_AT_comp_dir).
11559 `start_symtab' will always pass the contents of DW_AT_comp_dir as
11560 second argument to start_subfile. To be consistent, we do the
11561 same here. In order not to lose the line information directory,
11562 we concatenate it to the filename when it makes sense.
11563 Note that the Dwarf3 standard says (speaking of filenames in line
11564 information): ``The directory index is ignored for file names
11565 that represent full path names''. Thus ignoring dirname in the
11566 `else' branch below isn't an issue. */
11567
11568 if (!IS_ABSOLUTE_PATH (filename) && dirname != NULL)
11569 fullname = concat (dirname, SLASH_STRING, filename, (char *)NULL);
11570 else
11571 fullname = filename;
11572
11573 start_subfile (fullname, comp_dir);
11574
11575 if (fullname != filename)
11576 xfree (fullname);
11577 }
11578
11579 static void
11580 var_decode_location (struct attribute *attr, struct symbol *sym,
11581 struct dwarf2_cu *cu)
11582 {
11583 struct objfile *objfile = cu->objfile;
11584 struct comp_unit_head *cu_header = &cu->header;
11585
11586 /* NOTE drow/2003-01-30: There used to be a comment and some special
11587 code here to turn a symbol with DW_AT_external and a
11588 SYMBOL_VALUE_ADDRESS of 0 into a LOC_UNRESOLVED symbol. This was
11589 necessary for platforms (maybe Alpha, certainly PowerPC GNU/Linux
11590 with some versions of binutils) where shared libraries could have
11591 relocations against symbols in their debug information - the
11592 minimal symbol would have the right address, but the debug info
11593 would not. It's no longer necessary, because we will explicitly
11594 apply relocations when we read in the debug information now. */
11595
11596 /* A DW_AT_location attribute with no contents indicates that a
11597 variable has been optimized away. */
11598 if (attr_form_is_block (attr) && DW_BLOCK (attr)->size == 0)
11599 {
11600 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11601 return;
11602 }
11603
11604 /* Handle one degenerate form of location expression specially, to
11605 preserve GDB's previous behavior when section offsets are
11606 specified. If this is just a DW_OP_addr then mark this symbol
11607 as LOC_STATIC. */
11608
11609 if (attr_form_is_block (attr)
11610 && DW_BLOCK (attr)->size == 1 + cu_header->addr_size
11611 && DW_BLOCK (attr)->data[0] == DW_OP_addr)
11612 {
11613 unsigned int dummy;
11614
11615 SYMBOL_VALUE_ADDRESS (sym) =
11616 read_address (objfile->obfd, DW_BLOCK (attr)->data + 1, cu, &dummy);
11617 SYMBOL_CLASS (sym) = LOC_STATIC;
11618 fixup_symbol_section (sym, objfile);
11619 SYMBOL_VALUE_ADDRESS (sym) += ANOFFSET (objfile->section_offsets,
11620 SYMBOL_SECTION (sym));
11621 return;
11622 }
11623
11624 /* NOTE drow/2002-01-30: It might be worthwhile to have a static
11625 expression evaluator, and use LOC_COMPUTED only when necessary
11626 (i.e. when the value of a register or memory location is
11627 referenced, or a thread-local block, etc.). Then again, it might
11628 not be worthwhile. I'm assuming that it isn't unless performance
11629 or memory numbers show me otherwise. */
11630
11631 dwarf2_symbol_mark_computed (attr, sym, cu);
11632 SYMBOL_CLASS (sym) = LOC_COMPUTED;
11633
11634 if (SYMBOL_COMPUTED_OPS (sym) == &dwarf2_loclist_funcs)
11635 cu->has_loclist = 1;
11636 }
11637
11638 /* Given a pointer to a DWARF information entry, figure out if we need
11639 to make a symbol table entry for it, and if so, create a new entry
11640 and return a pointer to it.
11641 If TYPE is NULL, determine symbol type from the die, otherwise
11642 used the passed type.
11643 If SPACE is not NULL, use it to hold the new symbol. If it is
11644 NULL, allocate a new symbol on the objfile's obstack. */
11645
11646 static struct symbol *
11647 new_symbol_full (struct die_info *die, struct type *type, struct dwarf2_cu *cu,
11648 struct symbol *space)
11649 {
11650 struct objfile *objfile = cu->objfile;
11651 struct symbol *sym = NULL;
11652 char *name;
11653 struct attribute *attr = NULL;
11654 struct attribute *attr2 = NULL;
11655 CORE_ADDR baseaddr;
11656 struct pending **list_to_add = NULL;
11657
11658 int inlined_func = (die->tag == DW_TAG_inlined_subroutine);
11659
11660 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
11661
11662 name = dwarf2_name (die, cu);
11663 if (name)
11664 {
11665 const char *linkagename;
11666 int suppress_add = 0;
11667
11668 if (space)
11669 sym = space;
11670 else
11671 sym = OBSTACK_ZALLOC (&objfile->objfile_obstack, struct symbol);
11672 OBJSTAT (objfile, n_syms++);
11673
11674 /* Cache this symbol's name and the name's demangled form (if any). */
11675 SYMBOL_SET_LANGUAGE (sym, cu->language);
11676 linkagename = dwarf2_physname (name, die, cu);
11677 SYMBOL_SET_NAMES (sym, linkagename, strlen (linkagename), 0, objfile);
11678
11679 /* Fortran does not have mangling standard and the mangling does differ
11680 between gfortran, iFort etc. */
11681 if (cu->language == language_fortran
11682 && symbol_get_demangled_name (&(sym->ginfo)) == NULL)
11683 symbol_set_demangled_name (&(sym->ginfo),
11684 (char *) dwarf2_full_name (name, die, cu),
11685 NULL);
11686
11687 /* Default assumptions.
11688 Use the passed type or decode it from the die. */
11689 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11690 SYMBOL_CLASS (sym) = LOC_OPTIMIZED_OUT;
11691 if (type != NULL)
11692 SYMBOL_TYPE (sym) = type;
11693 else
11694 SYMBOL_TYPE (sym) = die_type (die, cu);
11695 attr = dwarf2_attr (die,
11696 inlined_func ? DW_AT_call_line : DW_AT_decl_line,
11697 cu);
11698 if (attr)
11699 {
11700 SYMBOL_LINE (sym) = DW_UNSND (attr);
11701 }
11702
11703 attr = dwarf2_attr (die,
11704 inlined_func ? DW_AT_call_file : DW_AT_decl_file,
11705 cu);
11706 if (attr)
11707 {
11708 int file_index = DW_UNSND (attr);
11709
11710 if (cu->line_header == NULL
11711 || file_index > cu->line_header->num_file_names)
11712 complaint (&symfile_complaints,
11713 _("file index out of range"));
11714 else if (file_index > 0)
11715 {
11716 struct file_entry *fe;
11717
11718 fe = &cu->line_header->file_names[file_index - 1];
11719 SYMBOL_SYMTAB (sym) = fe->symtab;
11720 }
11721 }
11722
11723 switch (die->tag)
11724 {
11725 case DW_TAG_label:
11726 attr = dwarf2_attr (die, DW_AT_low_pc, cu);
11727 if (attr)
11728 {
11729 SYMBOL_VALUE_ADDRESS (sym) = DW_ADDR (attr) + baseaddr;
11730 }
11731 SYMBOL_TYPE (sym) = objfile_type (objfile)->builtin_core_addr;
11732 SYMBOL_DOMAIN (sym) = LABEL_DOMAIN;
11733 SYMBOL_CLASS (sym) = LOC_LABEL;
11734 add_symbol_to_list (sym, cu->list_in_scope);
11735 break;
11736 case DW_TAG_subprogram:
11737 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11738 finish_block. */
11739 SYMBOL_CLASS (sym) = LOC_BLOCK;
11740 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11741 if ((attr2 && (DW_UNSND (attr2) != 0))
11742 || cu->language == language_ada)
11743 {
11744 /* Subprograms marked external are stored as a global symbol.
11745 Ada subprograms, whether marked external or not, are always
11746 stored as a global symbol, because we want to be able to
11747 access them globally. For instance, we want to be able
11748 to break on a nested subprogram without having to
11749 specify the context. */
11750 list_to_add = &global_symbols;
11751 }
11752 else
11753 {
11754 list_to_add = cu->list_in_scope;
11755 }
11756 break;
11757 case DW_TAG_inlined_subroutine:
11758 /* SYMBOL_BLOCK_VALUE (sym) will be filled in later by
11759 finish_block. */
11760 SYMBOL_CLASS (sym) = LOC_BLOCK;
11761 SYMBOL_INLINED (sym) = 1;
11762 list_to_add = cu->list_in_scope;
11763 break;
11764 case DW_TAG_template_value_param:
11765 suppress_add = 1;
11766 /* Fall through. */
11767 case DW_TAG_constant:
11768 case DW_TAG_variable:
11769 case DW_TAG_member:
11770 /* Compilation with minimal debug info may result in
11771 variables with missing type entries. Change the
11772 misleading `void' type to something sensible. */
11773 if (TYPE_CODE (SYMBOL_TYPE (sym)) == TYPE_CODE_VOID)
11774 SYMBOL_TYPE (sym)
11775 = objfile_type (objfile)->nodebug_data_symbol;
11776
11777 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11778 /* In the case of DW_TAG_member, we should only be called for
11779 static const members. */
11780 if (die->tag == DW_TAG_member)
11781 {
11782 /* dwarf2_add_field uses die_is_declaration,
11783 so we do the same. */
11784 gdb_assert (die_is_declaration (die, cu));
11785 gdb_assert (attr);
11786 }
11787 if (attr)
11788 {
11789 dwarf2_const_value (attr, sym, cu);
11790 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11791 if (!suppress_add)
11792 {
11793 if (attr2 && (DW_UNSND (attr2) != 0))
11794 list_to_add = &global_symbols;
11795 else
11796 list_to_add = cu->list_in_scope;
11797 }
11798 break;
11799 }
11800 attr = dwarf2_attr (die, DW_AT_location, cu);
11801 if (attr)
11802 {
11803 var_decode_location (attr, sym, cu);
11804 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11805 if (SYMBOL_CLASS (sym) == LOC_STATIC
11806 && SYMBOL_VALUE_ADDRESS (sym) == 0
11807 && !dwarf2_per_objfile->has_section_at_zero)
11808 {
11809 /* When a static variable is eliminated by the linker,
11810 the corresponding debug information is not stripped
11811 out, but the variable address is set to null;
11812 do not add such variables into symbol table. */
11813 }
11814 else if (attr2 && (DW_UNSND (attr2) != 0))
11815 {
11816 /* Workaround gfortran PR debug/40040 - it uses
11817 DW_AT_location for variables in -fPIC libraries which may
11818 get overriden by other libraries/executable and get
11819 a different address. Resolve it by the minimal symbol
11820 which may come from inferior's executable using copy
11821 relocation. Make this workaround only for gfortran as for
11822 other compilers GDB cannot guess the minimal symbol
11823 Fortran mangling kind. */
11824 if (cu->language == language_fortran && die->parent
11825 && die->parent->tag == DW_TAG_module
11826 && cu->producer
11827 && strncmp (cu->producer, "GNU Fortran ", 12) == 0)
11828 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11829
11830 /* A variable with DW_AT_external is never static,
11831 but it may be block-scoped. */
11832 list_to_add = (cu->list_in_scope == &file_symbols
11833 ? &global_symbols : cu->list_in_scope);
11834 }
11835 else
11836 list_to_add = cu->list_in_scope;
11837 }
11838 else
11839 {
11840 /* We do not know the address of this symbol.
11841 If it is an external symbol and we have type information
11842 for it, enter the symbol as a LOC_UNRESOLVED symbol.
11843 The address of the variable will then be determined from
11844 the minimal symbol table whenever the variable is
11845 referenced. */
11846 attr2 = dwarf2_attr (die, DW_AT_external, cu);
11847 if (attr2 && (DW_UNSND (attr2) != 0)
11848 && dwarf2_attr (die, DW_AT_type, cu) != NULL)
11849 {
11850 /* A variable with DW_AT_external is never static, but it
11851 may be block-scoped. */
11852 list_to_add = (cu->list_in_scope == &file_symbols
11853 ? &global_symbols : cu->list_in_scope);
11854
11855 SYMBOL_CLASS (sym) = LOC_UNRESOLVED;
11856 }
11857 else if (!die_is_declaration (die, cu))
11858 {
11859 /* Use the default LOC_OPTIMIZED_OUT class. */
11860 gdb_assert (SYMBOL_CLASS (sym) == LOC_OPTIMIZED_OUT);
11861 if (!suppress_add)
11862 list_to_add = cu->list_in_scope;
11863 }
11864 }
11865 break;
11866 case DW_TAG_formal_parameter:
11867 /* If we are inside a function, mark this as an argument. If
11868 not, we might be looking at an argument to an inlined function
11869 when we do not have enough information to show inlined frames;
11870 pretend it's a local variable in that case so that the user can
11871 still see it. */
11872 if (context_stack_depth > 0
11873 && context_stack[context_stack_depth - 1].name != NULL)
11874 SYMBOL_IS_ARGUMENT (sym) = 1;
11875 attr = dwarf2_attr (die, DW_AT_location, cu);
11876 if (attr)
11877 {
11878 var_decode_location (attr, sym, cu);
11879 }
11880 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11881 if (attr)
11882 {
11883 dwarf2_const_value (attr, sym, cu);
11884 }
11885
11886 list_to_add = cu->list_in_scope;
11887 break;
11888 case DW_TAG_unspecified_parameters:
11889 /* From varargs functions; gdb doesn't seem to have any
11890 interest in this information, so just ignore it for now.
11891 (FIXME?) */
11892 break;
11893 case DW_TAG_template_type_param:
11894 suppress_add = 1;
11895 /* Fall through. */
11896 case DW_TAG_class_type:
11897 case DW_TAG_interface_type:
11898 case DW_TAG_structure_type:
11899 case DW_TAG_union_type:
11900 case DW_TAG_set_type:
11901 case DW_TAG_enumeration_type:
11902 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11903 SYMBOL_DOMAIN (sym) = STRUCT_DOMAIN;
11904
11905 {
11906 /* NOTE: carlton/2003-11-10: C++ and Java class symbols shouldn't
11907 really ever be static objects: otherwise, if you try
11908 to, say, break of a class's method and you're in a file
11909 which doesn't mention that class, it won't work unless
11910 the check for all static symbols in lookup_symbol_aux
11911 saves you. See the OtherFileClass tests in
11912 gdb.c++/namespace.exp. */
11913
11914 if (!suppress_add)
11915 {
11916 list_to_add = (cu->list_in_scope == &file_symbols
11917 && (cu->language == language_cplus
11918 || cu->language == language_java)
11919 ? &global_symbols : cu->list_in_scope);
11920
11921 /* The semantics of C++ state that "struct foo {
11922 ... }" also defines a typedef for "foo". A Java
11923 class declaration also defines a typedef for the
11924 class. */
11925 if (cu->language == language_cplus
11926 || cu->language == language_java
11927 || cu->language == language_ada)
11928 {
11929 /* The symbol's name is already allocated along
11930 with this objfile, so we don't need to
11931 duplicate it for the type. */
11932 if (TYPE_NAME (SYMBOL_TYPE (sym)) == 0)
11933 TYPE_NAME (SYMBOL_TYPE (sym)) = SYMBOL_SEARCH_NAME (sym);
11934 }
11935 }
11936 }
11937 break;
11938 case DW_TAG_typedef:
11939 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11940 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11941 list_to_add = cu->list_in_scope;
11942 break;
11943 case DW_TAG_base_type:
11944 case DW_TAG_subrange_type:
11945 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11946 SYMBOL_DOMAIN (sym) = VAR_DOMAIN;
11947 list_to_add = cu->list_in_scope;
11948 break;
11949 case DW_TAG_enumerator:
11950 attr = dwarf2_attr (die, DW_AT_const_value, cu);
11951 if (attr)
11952 {
11953 dwarf2_const_value (attr, sym, cu);
11954 }
11955 {
11956 /* NOTE: carlton/2003-11-10: See comment above in the
11957 DW_TAG_class_type, etc. block. */
11958
11959 list_to_add = (cu->list_in_scope == &file_symbols
11960 && (cu->language == language_cplus
11961 || cu->language == language_java)
11962 ? &global_symbols : cu->list_in_scope);
11963 }
11964 break;
11965 case DW_TAG_namespace:
11966 SYMBOL_CLASS (sym) = LOC_TYPEDEF;
11967 list_to_add = &global_symbols;
11968 break;
11969 default:
11970 /* Not a tag we recognize. Hopefully we aren't processing
11971 trash data, but since we must specifically ignore things
11972 we don't recognize, there is nothing else we should do at
11973 this point. */
11974 complaint (&symfile_complaints, _("unsupported tag: '%s'"),
11975 dwarf_tag_name (die->tag));
11976 break;
11977 }
11978
11979 if (suppress_add)
11980 {
11981 sym->hash_next = objfile->template_symbols;
11982 objfile->template_symbols = sym;
11983 list_to_add = NULL;
11984 }
11985
11986 if (list_to_add != NULL)
11987 add_symbol_to_list (sym, list_to_add);
11988
11989 /* For the benefit of old versions of GCC, check for anonymous
11990 namespaces based on the demangled name. */
11991 if (!processing_has_namespace_info
11992 && cu->language == language_cplus)
11993 cp_scan_for_anonymous_namespaces (sym, objfile);
11994 }
11995 return (sym);
11996 }
11997
11998 /* A wrapper for new_symbol_full that always allocates a new symbol. */
11999
12000 static struct symbol *
12001 new_symbol (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
12002 {
12003 return new_symbol_full (die, type, cu, NULL);
12004 }
12005
12006 /* Given an attr with a DW_FORM_dataN value in host byte order,
12007 zero-extend it as appropriate for the symbol's type. The DWARF
12008 standard (v4) is not entirely clear about the meaning of using
12009 DW_FORM_dataN for a constant with a signed type, where the type is
12010 wider than the data. The conclusion of a discussion on the DWARF
12011 list was that this is unspecified. We choose to always zero-extend
12012 because that is the interpretation long in use by GCC. */
12013
12014 static gdb_byte *
12015 dwarf2_const_value_data (struct attribute *attr, struct type *type,
12016 const char *name, struct obstack *obstack,
12017 struct dwarf2_cu *cu, long *value, int bits)
12018 {
12019 struct objfile *objfile = cu->objfile;
12020 enum bfd_endian byte_order = bfd_big_endian (objfile->obfd) ?
12021 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE;
12022 LONGEST l = DW_UNSND (attr);
12023
12024 if (bits < sizeof (*value) * 8)
12025 {
12026 l &= ((LONGEST) 1 << bits) - 1;
12027 *value = l;
12028 }
12029 else if (bits == sizeof (*value) * 8)
12030 *value = l;
12031 else
12032 {
12033 gdb_byte *bytes = obstack_alloc (obstack, bits / 8);
12034 store_unsigned_integer (bytes, bits / 8, byte_order, l);
12035 return bytes;
12036 }
12037
12038 return NULL;
12039 }
12040
12041 /* Read a constant value from an attribute. Either set *VALUE, or if
12042 the value does not fit in *VALUE, set *BYTES - either already
12043 allocated on the objfile obstack, or newly allocated on OBSTACK,
12044 or, set *BATON, if we translated the constant to a location
12045 expression. */
12046
12047 static void
12048 dwarf2_const_value_attr (struct attribute *attr, struct type *type,
12049 const char *name, struct obstack *obstack,
12050 struct dwarf2_cu *cu,
12051 long *value, gdb_byte **bytes,
12052 struct dwarf2_locexpr_baton **baton)
12053 {
12054 struct objfile *objfile = cu->objfile;
12055 struct comp_unit_head *cu_header = &cu->header;
12056 struct dwarf_block *blk;
12057 enum bfd_endian byte_order = (bfd_big_endian (objfile->obfd) ?
12058 BFD_ENDIAN_BIG : BFD_ENDIAN_LITTLE);
12059
12060 *value = 0;
12061 *bytes = NULL;
12062 *baton = NULL;
12063
12064 switch (attr->form)
12065 {
12066 case DW_FORM_addr:
12067 {
12068 gdb_byte *data;
12069
12070 if (TYPE_LENGTH (type) != cu_header->addr_size)
12071 dwarf2_const_value_length_mismatch_complaint (name,
12072 cu_header->addr_size,
12073 TYPE_LENGTH (type));
12074 /* Symbols of this form are reasonably rare, so we just
12075 piggyback on the existing location code rather than writing
12076 a new implementation of symbol_computed_ops. */
12077 *baton = obstack_alloc (&objfile->objfile_obstack,
12078 sizeof (struct dwarf2_locexpr_baton));
12079 (*baton)->per_cu = cu->per_cu;
12080 gdb_assert ((*baton)->per_cu);
12081
12082 (*baton)->size = 2 + cu_header->addr_size;
12083 data = obstack_alloc (&objfile->objfile_obstack, (*baton)->size);
12084 (*baton)->data = data;
12085
12086 data[0] = DW_OP_addr;
12087 store_unsigned_integer (&data[1], cu_header->addr_size,
12088 byte_order, DW_ADDR (attr));
12089 data[cu_header->addr_size + 1] = DW_OP_stack_value;
12090 }
12091 break;
12092 case DW_FORM_string:
12093 case DW_FORM_strp:
12094 /* DW_STRING is already allocated on the objfile obstack, point
12095 directly to it. */
12096 *bytes = (gdb_byte *) DW_STRING (attr);
12097 break;
12098 case DW_FORM_block1:
12099 case DW_FORM_block2:
12100 case DW_FORM_block4:
12101 case DW_FORM_block:
12102 case DW_FORM_exprloc:
12103 blk = DW_BLOCK (attr);
12104 if (TYPE_LENGTH (type) != blk->size)
12105 dwarf2_const_value_length_mismatch_complaint (name, blk->size,
12106 TYPE_LENGTH (type));
12107 *bytes = blk->data;
12108 break;
12109
12110 /* The DW_AT_const_value attributes are supposed to carry the
12111 symbol's value "represented as it would be on the target
12112 architecture." By the time we get here, it's already been
12113 converted to host endianness, so we just need to sign- or
12114 zero-extend it as appropriate. */
12115 case DW_FORM_data1:
12116 *bytes = dwarf2_const_value_data (attr, type, name,
12117 obstack, cu, value, 8);
12118 break;
12119 case DW_FORM_data2:
12120 *bytes = dwarf2_const_value_data (attr, type, name,
12121 obstack, cu, value, 16);
12122 break;
12123 case DW_FORM_data4:
12124 *bytes = dwarf2_const_value_data (attr, type, name,
12125 obstack, cu, value, 32);
12126 break;
12127 case DW_FORM_data8:
12128 *bytes = dwarf2_const_value_data (attr, type, name,
12129 obstack, cu, value, 64);
12130 break;
12131
12132 case DW_FORM_sdata:
12133 *value = DW_SND (attr);
12134 break;
12135
12136 case DW_FORM_udata:
12137 *value = DW_UNSND (attr);
12138 break;
12139
12140 default:
12141 complaint (&symfile_complaints,
12142 _("unsupported const value attribute form: '%s'"),
12143 dwarf_form_name (attr->form));
12144 *value = 0;
12145 break;
12146 }
12147 }
12148
12149
12150 /* Copy constant value from an attribute to a symbol. */
12151
12152 static void
12153 dwarf2_const_value (struct attribute *attr, struct symbol *sym,
12154 struct dwarf2_cu *cu)
12155 {
12156 struct objfile *objfile = cu->objfile;
12157 struct comp_unit_head *cu_header = &cu->header;
12158 long value;
12159 gdb_byte *bytes;
12160 struct dwarf2_locexpr_baton *baton;
12161
12162 dwarf2_const_value_attr (attr, SYMBOL_TYPE (sym),
12163 SYMBOL_PRINT_NAME (sym),
12164 &objfile->objfile_obstack, cu,
12165 &value, &bytes, &baton);
12166
12167 if (baton != NULL)
12168 {
12169 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
12170 SYMBOL_LOCATION_BATON (sym) = baton;
12171 SYMBOL_CLASS (sym) = LOC_COMPUTED;
12172 }
12173 else if (bytes != NULL)
12174 {
12175 SYMBOL_VALUE_BYTES (sym) = bytes;
12176 SYMBOL_CLASS (sym) = LOC_CONST_BYTES;
12177 }
12178 else
12179 {
12180 SYMBOL_VALUE (sym) = value;
12181 SYMBOL_CLASS (sym) = LOC_CONST;
12182 }
12183 }
12184
12185 /* Return the type of the die in question using its DW_AT_type attribute. */
12186
12187 static struct type *
12188 die_type (struct die_info *die, struct dwarf2_cu *cu)
12189 {
12190 struct attribute *type_attr;
12191
12192 type_attr = dwarf2_attr (die, DW_AT_type, cu);
12193 if (!type_attr)
12194 {
12195 /* A missing DW_AT_type represents a void type. */
12196 return objfile_type (cu->objfile)->builtin_void;
12197 }
12198
12199 return lookup_die_type (die, type_attr, cu);
12200 }
12201
12202 /* True iff CU's producer generates GNAT Ada auxiliary information
12203 that allows to find parallel types through that information instead
12204 of having to do expensive parallel lookups by type name. */
12205
12206 static int
12207 need_gnat_info (struct dwarf2_cu *cu)
12208 {
12209 /* FIXME: brobecker/2010-10-12: As of now, only the AdaCore version
12210 of GNAT produces this auxiliary information, without any indication
12211 that it is produced. Part of enhancing the FSF version of GNAT
12212 to produce that information will be to put in place an indicator
12213 that we can use in order to determine whether the descriptive type
12214 info is available or not. One suggestion that has been made is
12215 to use a new attribute, attached to the CU die. For now, assume
12216 that the descriptive type info is not available. */
12217 return 0;
12218 }
12219
12220 /* Return the auxiliary type of the die in question using its
12221 DW_AT_GNAT_descriptive_type attribute. Returns NULL if the
12222 attribute is not present. */
12223
12224 static struct type *
12225 die_descriptive_type (struct die_info *die, struct dwarf2_cu *cu)
12226 {
12227 struct attribute *type_attr;
12228
12229 type_attr = dwarf2_attr (die, DW_AT_GNAT_descriptive_type, cu);
12230 if (!type_attr)
12231 return NULL;
12232
12233 return lookup_die_type (die, type_attr, cu);
12234 }
12235
12236 /* If DIE has a descriptive_type attribute, then set the TYPE's
12237 descriptive type accordingly. */
12238
12239 static void
12240 set_descriptive_type (struct type *type, struct die_info *die,
12241 struct dwarf2_cu *cu)
12242 {
12243 struct type *descriptive_type = die_descriptive_type (die, cu);
12244
12245 if (descriptive_type)
12246 {
12247 ALLOCATE_GNAT_AUX_TYPE (type);
12248 TYPE_DESCRIPTIVE_TYPE (type) = descriptive_type;
12249 }
12250 }
12251
12252 /* Return the containing type of the die in question using its
12253 DW_AT_containing_type attribute. */
12254
12255 static struct type *
12256 die_containing_type (struct die_info *die, struct dwarf2_cu *cu)
12257 {
12258 struct attribute *type_attr;
12259
12260 type_attr = dwarf2_attr (die, DW_AT_containing_type, cu);
12261 if (!type_attr)
12262 error (_("Dwarf Error: Problem turning containing type into gdb type "
12263 "[in module %s]"), cu->objfile->name);
12264
12265 return lookup_die_type (die, type_attr, cu);
12266 }
12267
12268 /* Look up the type of DIE in CU using its type attribute ATTR.
12269 If there is no type substitute an error marker. */
12270
12271 static struct type *
12272 lookup_die_type (struct die_info *die, struct attribute *attr,
12273 struct dwarf2_cu *cu)
12274 {
12275 struct objfile *objfile = cu->objfile;
12276 struct type *this_type;
12277
12278 /* First see if we have it cached. */
12279
12280 if (is_ref_attr (attr))
12281 {
12282 unsigned int offset = dwarf2_get_ref_die_offset (attr);
12283
12284 this_type = get_die_type_at_offset (offset, cu->per_cu);
12285 }
12286 else if (attr->form == DW_FORM_ref_sig8)
12287 {
12288 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
12289 struct dwarf2_cu *sig_cu;
12290 unsigned int offset;
12291
12292 /* sig_type will be NULL if the signatured type is missing from
12293 the debug info. */
12294 if (sig_type == NULL)
12295 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
12296 "at 0x%x [in module %s]"),
12297 die->offset, objfile->name);
12298
12299 gdb_assert (sig_type->per_cu.debug_types_section);
12300 offset = sig_type->per_cu.offset + sig_type->type_offset;
12301 this_type = get_die_type_at_offset (offset, &sig_type->per_cu);
12302 }
12303 else
12304 {
12305 dump_die_for_error (die);
12306 error (_("Dwarf Error: Bad type attribute %s [in module %s]"),
12307 dwarf_attr_name (attr->name), objfile->name);
12308 }
12309
12310 /* If not cached we need to read it in. */
12311
12312 if (this_type == NULL)
12313 {
12314 struct die_info *type_die;
12315 struct dwarf2_cu *type_cu = cu;
12316
12317 type_die = follow_die_ref_or_sig (die, attr, &type_cu);
12318 /* If the type is cached, we should have found it above. */
12319 gdb_assert (get_die_type (type_die, type_cu) == NULL);
12320 this_type = read_type_die_1 (type_die, type_cu);
12321 }
12322
12323 /* If we still don't have a type use an error marker. */
12324
12325 if (this_type == NULL)
12326 {
12327 char *message, *saved;
12328
12329 /* read_type_die already issued a complaint. */
12330 message = xstrprintf (_("<unknown type in %s, CU 0x%x, DIE 0x%x>"),
12331 objfile->name,
12332 cu->header.offset,
12333 die->offset);
12334 saved = obstack_copy0 (&objfile->objfile_obstack,
12335 message, strlen (message));
12336 xfree (message);
12337
12338 this_type = init_type (TYPE_CODE_ERROR, 0, 0, saved, objfile);
12339 }
12340
12341 return this_type;
12342 }
12343
12344 /* Return the type in DIE, CU.
12345 Returns NULL for invalid types.
12346
12347 This first does a lookup in the appropriate type_hash table,
12348 and only reads the die in if necessary.
12349
12350 NOTE: This can be called when reading in partial or full symbols. */
12351
12352 static struct type *
12353 read_type_die (struct die_info *die, struct dwarf2_cu *cu)
12354 {
12355 struct type *this_type;
12356
12357 this_type = get_die_type (die, cu);
12358 if (this_type)
12359 return this_type;
12360
12361 return read_type_die_1 (die, cu);
12362 }
12363
12364 /* Read the type in DIE, CU.
12365 Returns NULL for invalid types. */
12366
12367 static struct type *
12368 read_type_die_1 (struct die_info *die, struct dwarf2_cu *cu)
12369 {
12370 struct type *this_type = NULL;
12371
12372 switch (die->tag)
12373 {
12374 case DW_TAG_class_type:
12375 case DW_TAG_interface_type:
12376 case DW_TAG_structure_type:
12377 case DW_TAG_union_type:
12378 this_type = read_structure_type (die, cu);
12379 break;
12380 case DW_TAG_enumeration_type:
12381 this_type = read_enumeration_type (die, cu);
12382 break;
12383 case DW_TAG_subprogram:
12384 case DW_TAG_subroutine_type:
12385 case DW_TAG_inlined_subroutine:
12386 this_type = read_subroutine_type (die, cu);
12387 break;
12388 case DW_TAG_array_type:
12389 this_type = read_array_type (die, cu);
12390 break;
12391 case DW_TAG_set_type:
12392 this_type = read_set_type (die, cu);
12393 break;
12394 case DW_TAG_pointer_type:
12395 this_type = read_tag_pointer_type (die, cu);
12396 break;
12397 case DW_TAG_ptr_to_member_type:
12398 this_type = read_tag_ptr_to_member_type (die, cu);
12399 break;
12400 case DW_TAG_reference_type:
12401 this_type = read_tag_reference_type (die, cu);
12402 break;
12403 case DW_TAG_const_type:
12404 this_type = read_tag_const_type (die, cu);
12405 break;
12406 case DW_TAG_volatile_type:
12407 this_type = read_tag_volatile_type (die, cu);
12408 break;
12409 case DW_TAG_string_type:
12410 this_type = read_tag_string_type (die, cu);
12411 break;
12412 case DW_TAG_typedef:
12413 this_type = read_typedef (die, cu);
12414 break;
12415 case DW_TAG_subrange_type:
12416 this_type = read_subrange_type (die, cu);
12417 break;
12418 case DW_TAG_base_type:
12419 this_type = read_base_type (die, cu);
12420 break;
12421 case DW_TAG_unspecified_type:
12422 this_type = read_unspecified_type (die, cu);
12423 break;
12424 case DW_TAG_namespace:
12425 this_type = read_namespace_type (die, cu);
12426 break;
12427 case DW_TAG_module:
12428 this_type = read_module_type (die, cu);
12429 break;
12430 default:
12431 complaint (&symfile_complaints,
12432 _("unexpected tag in read_type_die: '%s'"),
12433 dwarf_tag_name (die->tag));
12434 break;
12435 }
12436
12437 return this_type;
12438 }
12439
12440 /* See if we can figure out if the class lives in a namespace. We do
12441 this by looking for a member function; its demangled name will
12442 contain namespace info, if there is any.
12443 Return the computed name or NULL.
12444 Space for the result is allocated on the objfile's obstack.
12445 This is the full-die version of guess_partial_die_structure_name.
12446 In this case we know DIE has no useful parent. */
12447
12448 static char *
12449 guess_full_die_structure_name (struct die_info *die, struct dwarf2_cu *cu)
12450 {
12451 struct die_info *spec_die;
12452 struct dwarf2_cu *spec_cu;
12453 struct die_info *child;
12454
12455 spec_cu = cu;
12456 spec_die = die_specification (die, &spec_cu);
12457 if (spec_die != NULL)
12458 {
12459 die = spec_die;
12460 cu = spec_cu;
12461 }
12462
12463 for (child = die->child;
12464 child != NULL;
12465 child = child->sibling)
12466 {
12467 if (child->tag == DW_TAG_subprogram)
12468 {
12469 struct attribute *attr;
12470
12471 attr = dwarf2_attr (child, DW_AT_linkage_name, cu);
12472 if (attr == NULL)
12473 attr = dwarf2_attr (child, DW_AT_MIPS_linkage_name, cu);
12474 if (attr != NULL)
12475 {
12476 char *actual_name
12477 = language_class_name_from_physname (cu->language_defn,
12478 DW_STRING (attr));
12479 char *name = NULL;
12480
12481 if (actual_name != NULL)
12482 {
12483 char *die_name = dwarf2_name (die, cu);
12484
12485 if (die_name != NULL
12486 && strcmp (die_name, actual_name) != 0)
12487 {
12488 /* Strip off the class name from the full name.
12489 We want the prefix. */
12490 int die_name_len = strlen (die_name);
12491 int actual_name_len = strlen (actual_name);
12492
12493 /* Test for '::' as a sanity check. */
12494 if (actual_name_len > die_name_len + 2
12495 && actual_name[actual_name_len
12496 - die_name_len - 1] == ':')
12497 name =
12498 obsavestring (actual_name,
12499 actual_name_len - die_name_len - 2,
12500 &cu->objfile->objfile_obstack);
12501 }
12502 }
12503 xfree (actual_name);
12504 return name;
12505 }
12506 }
12507 }
12508
12509 return NULL;
12510 }
12511
12512 /* GCC might emit a nameless typedef that has a linkage name. Determine the
12513 prefix part in such case. See
12514 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12515
12516 static char *
12517 anonymous_struct_prefix (struct die_info *die, struct dwarf2_cu *cu)
12518 {
12519 struct attribute *attr;
12520 char *base;
12521
12522 if (die->tag != DW_TAG_class_type && die->tag != DW_TAG_interface_type
12523 && die->tag != DW_TAG_structure_type && die->tag != DW_TAG_union_type)
12524 return NULL;
12525
12526 attr = dwarf2_attr (die, DW_AT_name, cu);
12527 if (attr != NULL && DW_STRING (attr) != NULL)
12528 return NULL;
12529
12530 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12531 if (attr == NULL)
12532 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12533 if (attr == NULL || DW_STRING (attr) == NULL)
12534 return NULL;
12535
12536 /* dwarf2_name had to be already called. */
12537 gdb_assert (DW_STRING_IS_CANONICAL (attr));
12538
12539 /* Strip the base name, keep any leading namespaces/classes. */
12540 base = strrchr (DW_STRING (attr), ':');
12541 if (base == NULL || base == DW_STRING (attr) || base[-1] != ':')
12542 return "";
12543
12544 return obsavestring (DW_STRING (attr), &base[-1] - DW_STRING (attr),
12545 &cu->objfile->objfile_obstack);
12546 }
12547
12548 /* Return the name of the namespace/class that DIE is defined within,
12549 or "" if we can't tell. The caller should not xfree the result.
12550
12551 For example, if we're within the method foo() in the following
12552 code:
12553
12554 namespace N {
12555 class C {
12556 void foo () {
12557 }
12558 };
12559 }
12560
12561 then determine_prefix on foo's die will return "N::C". */
12562
12563 static const char *
12564 determine_prefix (struct die_info *die, struct dwarf2_cu *cu)
12565 {
12566 struct die_info *parent, *spec_die;
12567 struct dwarf2_cu *spec_cu;
12568 struct type *parent_type;
12569 char *retval;
12570
12571 if (cu->language != language_cplus && cu->language != language_java
12572 && cu->language != language_fortran)
12573 return "";
12574
12575 retval = anonymous_struct_prefix (die, cu);
12576 if (retval)
12577 return retval;
12578
12579 /* We have to be careful in the presence of DW_AT_specification.
12580 For example, with GCC 3.4, given the code
12581
12582 namespace N {
12583 void foo() {
12584 // Definition of N::foo.
12585 }
12586 }
12587
12588 then we'll have a tree of DIEs like this:
12589
12590 1: DW_TAG_compile_unit
12591 2: DW_TAG_namespace // N
12592 3: DW_TAG_subprogram // declaration of N::foo
12593 4: DW_TAG_subprogram // definition of N::foo
12594 DW_AT_specification // refers to die #3
12595
12596 Thus, when processing die #4, we have to pretend that we're in
12597 the context of its DW_AT_specification, namely the contex of die
12598 #3. */
12599 spec_cu = cu;
12600 spec_die = die_specification (die, &spec_cu);
12601 if (spec_die == NULL)
12602 parent = die->parent;
12603 else
12604 {
12605 parent = spec_die->parent;
12606 cu = spec_cu;
12607 }
12608
12609 if (parent == NULL)
12610 return "";
12611 else if (parent->building_fullname)
12612 {
12613 const char *name;
12614 const char *parent_name;
12615
12616 /* It has been seen on RealView 2.2 built binaries,
12617 DW_TAG_template_type_param types actually _defined_ as
12618 children of the parent class:
12619
12620 enum E {};
12621 template class <class Enum> Class{};
12622 Class<enum E> class_e;
12623
12624 1: DW_TAG_class_type (Class)
12625 2: DW_TAG_enumeration_type (E)
12626 3: DW_TAG_enumerator (enum1:0)
12627 3: DW_TAG_enumerator (enum2:1)
12628 ...
12629 2: DW_TAG_template_type_param
12630 DW_AT_type DW_FORM_ref_udata (E)
12631
12632 Besides being broken debug info, it can put GDB into an
12633 infinite loop. Consider:
12634
12635 When we're building the full name for Class<E>, we'll start
12636 at Class, and go look over its template type parameters,
12637 finding E. We'll then try to build the full name of E, and
12638 reach here. We're now trying to build the full name of E,
12639 and look over the parent DIE for containing scope. In the
12640 broken case, if we followed the parent DIE of E, we'd again
12641 find Class, and once again go look at its template type
12642 arguments, etc., etc. Simply don't consider such parent die
12643 as source-level parent of this die (it can't be, the language
12644 doesn't allow it), and break the loop here. */
12645 name = dwarf2_name (die, cu);
12646 parent_name = dwarf2_name (parent, cu);
12647 complaint (&symfile_complaints,
12648 _("template param type '%s' defined within parent '%s'"),
12649 name ? name : "<unknown>",
12650 parent_name ? parent_name : "<unknown>");
12651 return "";
12652 }
12653 else
12654 switch (parent->tag)
12655 {
12656 case DW_TAG_namespace:
12657 parent_type = read_type_die (parent, cu);
12658 /* GCC 4.0 and 4.1 had a bug (PR c++/28460) where they generated bogus
12659 DW_TAG_namespace DIEs with a name of "::" for the global namespace.
12660 Work around this problem here. */
12661 if (cu->language == language_cplus
12662 && strcmp (TYPE_TAG_NAME (parent_type), "::") == 0)
12663 return "";
12664 /* We give a name to even anonymous namespaces. */
12665 return TYPE_TAG_NAME (parent_type);
12666 case DW_TAG_class_type:
12667 case DW_TAG_interface_type:
12668 case DW_TAG_structure_type:
12669 case DW_TAG_union_type:
12670 case DW_TAG_module:
12671 parent_type = read_type_die (parent, cu);
12672 if (TYPE_TAG_NAME (parent_type) != NULL)
12673 return TYPE_TAG_NAME (parent_type);
12674 else
12675 /* An anonymous structure is only allowed non-static data
12676 members; no typedefs, no member functions, et cetera.
12677 So it does not need a prefix. */
12678 return "";
12679 case DW_TAG_compile_unit:
12680 /* gcc-4.5 -gdwarf-4 can drop the enclosing namespace. Cope. */
12681 if (cu->language == language_cplus
12682 && !VEC_empty (dwarf2_section_info_def, dwarf2_per_objfile->types)
12683 && die->child != NULL
12684 && (die->tag == DW_TAG_class_type
12685 || die->tag == DW_TAG_structure_type
12686 || die->tag == DW_TAG_union_type))
12687 {
12688 char *name = guess_full_die_structure_name (die, cu);
12689 if (name != NULL)
12690 return name;
12691 }
12692 return "";
12693 default:
12694 return determine_prefix (parent, cu);
12695 }
12696 }
12697
12698 /* Return a newly-allocated string formed by concatenating PREFIX and SUFFIX
12699 with appropriate separator. If PREFIX or SUFFIX is NULL or empty, then
12700 simply copy the SUFFIX or PREFIX, respectively. If OBS is non-null, perform
12701 an obconcat, otherwise allocate storage for the result. The CU argument is
12702 used to determine the language and hence, the appropriate separator. */
12703
12704 #define MAX_SEP_LEN 7 /* strlen ("__") + strlen ("_MOD_") */
12705
12706 static char *
12707 typename_concat (struct obstack *obs, const char *prefix, const char *suffix,
12708 int physname, struct dwarf2_cu *cu)
12709 {
12710 const char *lead = "";
12711 const char *sep;
12712
12713 if (suffix == NULL || suffix[0] == '\0'
12714 || prefix == NULL || prefix[0] == '\0')
12715 sep = "";
12716 else if (cu->language == language_java)
12717 sep = ".";
12718 else if (cu->language == language_fortran && physname)
12719 {
12720 /* This is gfortran specific mangling. Normally DW_AT_linkage_name or
12721 DW_AT_MIPS_linkage_name is preferred and used instead. */
12722
12723 lead = "__";
12724 sep = "_MOD_";
12725 }
12726 else
12727 sep = "::";
12728
12729 if (prefix == NULL)
12730 prefix = "";
12731 if (suffix == NULL)
12732 suffix = "";
12733
12734 if (obs == NULL)
12735 {
12736 char *retval
12737 = xmalloc (strlen (prefix) + MAX_SEP_LEN + strlen (suffix) + 1);
12738
12739 strcpy (retval, lead);
12740 strcat (retval, prefix);
12741 strcat (retval, sep);
12742 strcat (retval, suffix);
12743 return retval;
12744 }
12745 else
12746 {
12747 /* We have an obstack. */
12748 return obconcat (obs, lead, prefix, sep, suffix, (char *) NULL);
12749 }
12750 }
12751
12752 /* Return sibling of die, NULL if no sibling. */
12753
12754 static struct die_info *
12755 sibling_die (struct die_info *die)
12756 {
12757 return die->sibling;
12758 }
12759
12760 /* Get name of a die, return NULL if not found. */
12761
12762 static char *
12763 dwarf2_canonicalize_name (char *name, struct dwarf2_cu *cu,
12764 struct obstack *obstack)
12765 {
12766 if (name && cu->language == language_cplus)
12767 {
12768 char *canon_name = cp_canonicalize_string (name);
12769
12770 if (canon_name != NULL)
12771 {
12772 if (strcmp (canon_name, name) != 0)
12773 name = obsavestring (canon_name, strlen (canon_name),
12774 obstack);
12775 xfree (canon_name);
12776 }
12777 }
12778
12779 return name;
12780 }
12781
12782 /* Get name of a die, return NULL if not found. */
12783
12784 static char *
12785 dwarf2_name (struct die_info *die, struct dwarf2_cu *cu)
12786 {
12787 struct attribute *attr;
12788
12789 attr = dwarf2_attr (die, DW_AT_name, cu);
12790 if ((!attr || !DW_STRING (attr))
12791 && die->tag != DW_TAG_class_type
12792 && die->tag != DW_TAG_interface_type
12793 && die->tag != DW_TAG_structure_type
12794 && die->tag != DW_TAG_union_type)
12795 return NULL;
12796
12797 switch (die->tag)
12798 {
12799 case DW_TAG_compile_unit:
12800 /* Compilation units have a DW_AT_name that is a filename, not
12801 a source language identifier. */
12802 case DW_TAG_enumeration_type:
12803 case DW_TAG_enumerator:
12804 /* These tags always have simple identifiers already; no need
12805 to canonicalize them. */
12806 return DW_STRING (attr);
12807
12808 case DW_TAG_subprogram:
12809 /* Java constructors will all be named "<init>", so return
12810 the class name when we see this special case. */
12811 if (cu->language == language_java
12812 && DW_STRING (attr) != NULL
12813 && strcmp (DW_STRING (attr), "<init>") == 0)
12814 {
12815 struct dwarf2_cu *spec_cu = cu;
12816 struct die_info *spec_die;
12817
12818 /* GCJ will output '<init>' for Java constructor names.
12819 For this special case, return the name of the parent class. */
12820
12821 /* GCJ may output suprogram DIEs with AT_specification set.
12822 If so, use the name of the specified DIE. */
12823 spec_die = die_specification (die, &spec_cu);
12824 if (spec_die != NULL)
12825 return dwarf2_name (spec_die, spec_cu);
12826
12827 do
12828 {
12829 die = die->parent;
12830 if (die->tag == DW_TAG_class_type)
12831 return dwarf2_name (die, cu);
12832 }
12833 while (die->tag != DW_TAG_compile_unit);
12834 }
12835 break;
12836
12837 case DW_TAG_class_type:
12838 case DW_TAG_interface_type:
12839 case DW_TAG_structure_type:
12840 case DW_TAG_union_type:
12841 /* Some GCC versions emit spurious DW_AT_name attributes for unnamed
12842 structures or unions. These were of the form "._%d" in GCC 4.1,
12843 or simply "<anonymous struct>" or "<anonymous union>" in GCC 4.3
12844 and GCC 4.4. We work around this problem by ignoring these. */
12845 if (attr && DW_STRING (attr)
12846 && (strncmp (DW_STRING (attr), "._", 2) == 0
12847 || strncmp (DW_STRING (attr), "<anonymous", 10) == 0))
12848 return NULL;
12849
12850 /* GCC might emit a nameless typedef that has a linkage name. See
12851 http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47510. */
12852 if (!attr || DW_STRING (attr) == NULL)
12853 {
12854 char *demangled = NULL;
12855
12856 attr = dwarf2_attr (die, DW_AT_linkage_name, cu);
12857 if (attr == NULL)
12858 attr = dwarf2_attr (die, DW_AT_MIPS_linkage_name, cu);
12859
12860 if (attr == NULL || DW_STRING (attr) == NULL)
12861 return NULL;
12862
12863 /* Avoid demangling DW_STRING (attr) the second time on a second
12864 call for the same DIE. */
12865 if (!DW_STRING_IS_CANONICAL (attr))
12866 demangled = cplus_demangle (DW_STRING (attr), DMGL_TYPES);
12867
12868 if (demangled)
12869 {
12870 char *base;
12871
12872 /* FIXME: we already did this for the partial symbol... */
12873 DW_STRING (attr) = obsavestring (demangled, strlen (demangled),
12874 &cu->objfile->objfile_obstack);
12875 DW_STRING_IS_CANONICAL (attr) = 1;
12876 xfree (demangled);
12877
12878 /* Strip any leading namespaces/classes, keep only the base name.
12879 DW_AT_name for named DIEs does not contain the prefixes. */
12880 base = strrchr (DW_STRING (attr), ':');
12881 if (base && base > DW_STRING (attr) && base[-1] == ':')
12882 return &base[1];
12883 else
12884 return DW_STRING (attr);
12885 }
12886 }
12887 break;
12888
12889 default:
12890 break;
12891 }
12892
12893 if (!DW_STRING_IS_CANONICAL (attr))
12894 {
12895 DW_STRING (attr)
12896 = dwarf2_canonicalize_name (DW_STRING (attr), cu,
12897 &cu->objfile->objfile_obstack);
12898 DW_STRING_IS_CANONICAL (attr) = 1;
12899 }
12900 return DW_STRING (attr);
12901 }
12902
12903 /* Return the die that this die in an extension of, or NULL if there
12904 is none. *EXT_CU is the CU containing DIE on input, and the CU
12905 containing the return value on output. */
12906
12907 static struct die_info *
12908 dwarf2_extension (struct die_info *die, struct dwarf2_cu **ext_cu)
12909 {
12910 struct attribute *attr;
12911
12912 attr = dwarf2_attr (die, DW_AT_extension, *ext_cu);
12913 if (attr == NULL)
12914 return NULL;
12915
12916 return follow_die_ref (die, attr, ext_cu);
12917 }
12918
12919 /* Convert a DIE tag into its string name. */
12920
12921 static char *
12922 dwarf_tag_name (unsigned tag)
12923 {
12924 switch (tag)
12925 {
12926 case DW_TAG_padding:
12927 return "DW_TAG_padding";
12928 case DW_TAG_array_type:
12929 return "DW_TAG_array_type";
12930 case DW_TAG_class_type:
12931 return "DW_TAG_class_type";
12932 case DW_TAG_entry_point:
12933 return "DW_TAG_entry_point";
12934 case DW_TAG_enumeration_type:
12935 return "DW_TAG_enumeration_type";
12936 case DW_TAG_formal_parameter:
12937 return "DW_TAG_formal_parameter";
12938 case DW_TAG_imported_declaration:
12939 return "DW_TAG_imported_declaration";
12940 case DW_TAG_label:
12941 return "DW_TAG_label";
12942 case DW_TAG_lexical_block:
12943 return "DW_TAG_lexical_block";
12944 case DW_TAG_member:
12945 return "DW_TAG_member";
12946 case DW_TAG_pointer_type:
12947 return "DW_TAG_pointer_type";
12948 case DW_TAG_reference_type:
12949 return "DW_TAG_reference_type";
12950 case DW_TAG_compile_unit:
12951 return "DW_TAG_compile_unit";
12952 case DW_TAG_string_type:
12953 return "DW_TAG_string_type";
12954 case DW_TAG_structure_type:
12955 return "DW_TAG_structure_type";
12956 case DW_TAG_subroutine_type:
12957 return "DW_TAG_subroutine_type";
12958 case DW_TAG_typedef:
12959 return "DW_TAG_typedef";
12960 case DW_TAG_union_type:
12961 return "DW_TAG_union_type";
12962 case DW_TAG_unspecified_parameters:
12963 return "DW_TAG_unspecified_parameters";
12964 case DW_TAG_variant:
12965 return "DW_TAG_variant";
12966 case DW_TAG_common_block:
12967 return "DW_TAG_common_block";
12968 case DW_TAG_common_inclusion:
12969 return "DW_TAG_common_inclusion";
12970 case DW_TAG_inheritance:
12971 return "DW_TAG_inheritance";
12972 case DW_TAG_inlined_subroutine:
12973 return "DW_TAG_inlined_subroutine";
12974 case DW_TAG_module:
12975 return "DW_TAG_module";
12976 case DW_TAG_ptr_to_member_type:
12977 return "DW_TAG_ptr_to_member_type";
12978 case DW_TAG_set_type:
12979 return "DW_TAG_set_type";
12980 case DW_TAG_subrange_type:
12981 return "DW_TAG_subrange_type";
12982 case DW_TAG_with_stmt:
12983 return "DW_TAG_with_stmt";
12984 case DW_TAG_access_declaration:
12985 return "DW_TAG_access_declaration";
12986 case DW_TAG_base_type:
12987 return "DW_TAG_base_type";
12988 case DW_TAG_catch_block:
12989 return "DW_TAG_catch_block";
12990 case DW_TAG_const_type:
12991 return "DW_TAG_const_type";
12992 case DW_TAG_constant:
12993 return "DW_TAG_constant";
12994 case DW_TAG_enumerator:
12995 return "DW_TAG_enumerator";
12996 case DW_TAG_file_type:
12997 return "DW_TAG_file_type";
12998 case DW_TAG_friend:
12999 return "DW_TAG_friend";
13000 case DW_TAG_namelist:
13001 return "DW_TAG_namelist";
13002 case DW_TAG_namelist_item:
13003 return "DW_TAG_namelist_item";
13004 case DW_TAG_packed_type:
13005 return "DW_TAG_packed_type";
13006 case DW_TAG_subprogram:
13007 return "DW_TAG_subprogram";
13008 case DW_TAG_template_type_param:
13009 return "DW_TAG_template_type_param";
13010 case DW_TAG_template_value_param:
13011 return "DW_TAG_template_value_param";
13012 case DW_TAG_thrown_type:
13013 return "DW_TAG_thrown_type";
13014 case DW_TAG_try_block:
13015 return "DW_TAG_try_block";
13016 case DW_TAG_variant_part:
13017 return "DW_TAG_variant_part";
13018 case DW_TAG_variable:
13019 return "DW_TAG_variable";
13020 case DW_TAG_volatile_type:
13021 return "DW_TAG_volatile_type";
13022 case DW_TAG_dwarf_procedure:
13023 return "DW_TAG_dwarf_procedure";
13024 case DW_TAG_restrict_type:
13025 return "DW_TAG_restrict_type";
13026 case DW_TAG_interface_type:
13027 return "DW_TAG_interface_type";
13028 case DW_TAG_namespace:
13029 return "DW_TAG_namespace";
13030 case DW_TAG_imported_module:
13031 return "DW_TAG_imported_module";
13032 case DW_TAG_unspecified_type:
13033 return "DW_TAG_unspecified_type";
13034 case DW_TAG_partial_unit:
13035 return "DW_TAG_partial_unit";
13036 case DW_TAG_imported_unit:
13037 return "DW_TAG_imported_unit";
13038 case DW_TAG_condition:
13039 return "DW_TAG_condition";
13040 case DW_TAG_shared_type:
13041 return "DW_TAG_shared_type";
13042 case DW_TAG_type_unit:
13043 return "DW_TAG_type_unit";
13044 case DW_TAG_MIPS_loop:
13045 return "DW_TAG_MIPS_loop";
13046 case DW_TAG_HP_array_descriptor:
13047 return "DW_TAG_HP_array_descriptor";
13048 case DW_TAG_format_label:
13049 return "DW_TAG_format_label";
13050 case DW_TAG_function_template:
13051 return "DW_TAG_function_template";
13052 case DW_TAG_class_template:
13053 return "DW_TAG_class_template";
13054 case DW_TAG_GNU_BINCL:
13055 return "DW_TAG_GNU_BINCL";
13056 case DW_TAG_GNU_EINCL:
13057 return "DW_TAG_GNU_EINCL";
13058 case DW_TAG_upc_shared_type:
13059 return "DW_TAG_upc_shared_type";
13060 case DW_TAG_upc_strict_type:
13061 return "DW_TAG_upc_strict_type";
13062 case DW_TAG_upc_relaxed_type:
13063 return "DW_TAG_upc_relaxed_type";
13064 case DW_TAG_PGI_kanji_type:
13065 return "DW_TAG_PGI_kanji_type";
13066 case DW_TAG_PGI_interface_block:
13067 return "DW_TAG_PGI_interface_block";
13068 case DW_TAG_GNU_call_site:
13069 return "DW_TAG_GNU_call_site";
13070 default:
13071 return "DW_TAG_<unknown>";
13072 }
13073 }
13074
13075 /* Convert a DWARF attribute code into its string name. */
13076
13077 static char *
13078 dwarf_attr_name (unsigned attr)
13079 {
13080 switch (attr)
13081 {
13082 case DW_AT_sibling:
13083 return "DW_AT_sibling";
13084 case DW_AT_location:
13085 return "DW_AT_location";
13086 case DW_AT_name:
13087 return "DW_AT_name";
13088 case DW_AT_ordering:
13089 return "DW_AT_ordering";
13090 case DW_AT_subscr_data:
13091 return "DW_AT_subscr_data";
13092 case DW_AT_byte_size:
13093 return "DW_AT_byte_size";
13094 case DW_AT_bit_offset:
13095 return "DW_AT_bit_offset";
13096 case DW_AT_bit_size:
13097 return "DW_AT_bit_size";
13098 case DW_AT_element_list:
13099 return "DW_AT_element_list";
13100 case DW_AT_stmt_list:
13101 return "DW_AT_stmt_list";
13102 case DW_AT_low_pc:
13103 return "DW_AT_low_pc";
13104 case DW_AT_high_pc:
13105 return "DW_AT_high_pc";
13106 case DW_AT_language:
13107 return "DW_AT_language";
13108 case DW_AT_member:
13109 return "DW_AT_member";
13110 case DW_AT_discr:
13111 return "DW_AT_discr";
13112 case DW_AT_discr_value:
13113 return "DW_AT_discr_value";
13114 case DW_AT_visibility:
13115 return "DW_AT_visibility";
13116 case DW_AT_import:
13117 return "DW_AT_import";
13118 case DW_AT_string_length:
13119 return "DW_AT_string_length";
13120 case DW_AT_common_reference:
13121 return "DW_AT_common_reference";
13122 case DW_AT_comp_dir:
13123 return "DW_AT_comp_dir";
13124 case DW_AT_const_value:
13125 return "DW_AT_const_value";
13126 case DW_AT_containing_type:
13127 return "DW_AT_containing_type";
13128 case DW_AT_default_value:
13129 return "DW_AT_default_value";
13130 case DW_AT_inline:
13131 return "DW_AT_inline";
13132 case DW_AT_is_optional:
13133 return "DW_AT_is_optional";
13134 case DW_AT_lower_bound:
13135 return "DW_AT_lower_bound";
13136 case DW_AT_producer:
13137 return "DW_AT_producer";
13138 case DW_AT_prototyped:
13139 return "DW_AT_prototyped";
13140 case DW_AT_return_addr:
13141 return "DW_AT_return_addr";
13142 case DW_AT_start_scope:
13143 return "DW_AT_start_scope";
13144 case DW_AT_bit_stride:
13145 return "DW_AT_bit_stride";
13146 case DW_AT_upper_bound:
13147 return "DW_AT_upper_bound";
13148 case DW_AT_abstract_origin:
13149 return "DW_AT_abstract_origin";
13150 case DW_AT_accessibility:
13151 return "DW_AT_accessibility";
13152 case DW_AT_address_class:
13153 return "DW_AT_address_class";
13154 case DW_AT_artificial:
13155 return "DW_AT_artificial";
13156 case DW_AT_base_types:
13157 return "DW_AT_base_types";
13158 case DW_AT_calling_convention:
13159 return "DW_AT_calling_convention";
13160 case DW_AT_count:
13161 return "DW_AT_count";
13162 case DW_AT_data_member_location:
13163 return "DW_AT_data_member_location";
13164 case DW_AT_decl_column:
13165 return "DW_AT_decl_column";
13166 case DW_AT_decl_file:
13167 return "DW_AT_decl_file";
13168 case DW_AT_decl_line:
13169 return "DW_AT_decl_line";
13170 case DW_AT_declaration:
13171 return "DW_AT_declaration";
13172 case DW_AT_discr_list:
13173 return "DW_AT_discr_list";
13174 case DW_AT_encoding:
13175 return "DW_AT_encoding";
13176 case DW_AT_external:
13177 return "DW_AT_external";
13178 case DW_AT_frame_base:
13179 return "DW_AT_frame_base";
13180 case DW_AT_friend:
13181 return "DW_AT_friend";
13182 case DW_AT_identifier_case:
13183 return "DW_AT_identifier_case";
13184 case DW_AT_macro_info:
13185 return "DW_AT_macro_info";
13186 case DW_AT_namelist_items:
13187 return "DW_AT_namelist_items";
13188 case DW_AT_priority:
13189 return "DW_AT_priority";
13190 case DW_AT_segment:
13191 return "DW_AT_segment";
13192 case DW_AT_specification:
13193 return "DW_AT_specification";
13194 case DW_AT_static_link:
13195 return "DW_AT_static_link";
13196 case DW_AT_type:
13197 return "DW_AT_type";
13198 case DW_AT_use_location:
13199 return "DW_AT_use_location";
13200 case DW_AT_variable_parameter:
13201 return "DW_AT_variable_parameter";
13202 case DW_AT_virtuality:
13203 return "DW_AT_virtuality";
13204 case DW_AT_vtable_elem_location:
13205 return "DW_AT_vtable_elem_location";
13206 /* DWARF 3 values. */
13207 case DW_AT_allocated:
13208 return "DW_AT_allocated";
13209 case DW_AT_associated:
13210 return "DW_AT_associated";
13211 case DW_AT_data_location:
13212 return "DW_AT_data_location";
13213 case DW_AT_byte_stride:
13214 return "DW_AT_byte_stride";
13215 case DW_AT_entry_pc:
13216 return "DW_AT_entry_pc";
13217 case DW_AT_use_UTF8:
13218 return "DW_AT_use_UTF8";
13219 case DW_AT_extension:
13220 return "DW_AT_extension";
13221 case DW_AT_ranges:
13222 return "DW_AT_ranges";
13223 case DW_AT_trampoline:
13224 return "DW_AT_trampoline";
13225 case DW_AT_call_column:
13226 return "DW_AT_call_column";
13227 case DW_AT_call_file:
13228 return "DW_AT_call_file";
13229 case DW_AT_call_line:
13230 return "DW_AT_call_line";
13231 case DW_AT_description:
13232 return "DW_AT_description";
13233 case DW_AT_binary_scale:
13234 return "DW_AT_binary_scale";
13235 case DW_AT_decimal_scale:
13236 return "DW_AT_decimal_scale";
13237 case DW_AT_small:
13238 return "DW_AT_small";
13239 case DW_AT_decimal_sign:
13240 return "DW_AT_decimal_sign";
13241 case DW_AT_digit_count:
13242 return "DW_AT_digit_count";
13243 case DW_AT_picture_string:
13244 return "DW_AT_picture_string";
13245 case DW_AT_mutable:
13246 return "DW_AT_mutable";
13247 case DW_AT_threads_scaled:
13248 return "DW_AT_threads_scaled";
13249 case DW_AT_explicit:
13250 return "DW_AT_explicit";
13251 case DW_AT_object_pointer:
13252 return "DW_AT_object_pointer";
13253 case DW_AT_endianity:
13254 return "DW_AT_endianity";
13255 case DW_AT_elemental:
13256 return "DW_AT_elemental";
13257 case DW_AT_pure:
13258 return "DW_AT_pure";
13259 case DW_AT_recursive:
13260 return "DW_AT_recursive";
13261 /* DWARF 4 values. */
13262 case DW_AT_signature:
13263 return "DW_AT_signature";
13264 case DW_AT_linkage_name:
13265 return "DW_AT_linkage_name";
13266 /* SGI/MIPS extensions. */
13267 #ifdef MIPS /* collides with DW_AT_HP_block_index */
13268 case DW_AT_MIPS_fde:
13269 return "DW_AT_MIPS_fde";
13270 #endif
13271 case DW_AT_MIPS_loop_begin:
13272 return "DW_AT_MIPS_loop_begin";
13273 case DW_AT_MIPS_tail_loop_begin:
13274 return "DW_AT_MIPS_tail_loop_begin";
13275 case DW_AT_MIPS_epilog_begin:
13276 return "DW_AT_MIPS_epilog_begin";
13277 case DW_AT_MIPS_loop_unroll_factor:
13278 return "DW_AT_MIPS_loop_unroll_factor";
13279 case DW_AT_MIPS_software_pipeline_depth:
13280 return "DW_AT_MIPS_software_pipeline_depth";
13281 case DW_AT_MIPS_linkage_name:
13282 return "DW_AT_MIPS_linkage_name";
13283 case DW_AT_MIPS_stride:
13284 return "DW_AT_MIPS_stride";
13285 case DW_AT_MIPS_abstract_name:
13286 return "DW_AT_MIPS_abstract_name";
13287 case DW_AT_MIPS_clone_origin:
13288 return "DW_AT_MIPS_clone_origin";
13289 case DW_AT_MIPS_has_inlines:
13290 return "DW_AT_MIPS_has_inlines";
13291 /* HP extensions. */
13292 #ifndef MIPS /* collides with DW_AT_MIPS_fde */
13293 case DW_AT_HP_block_index:
13294 return "DW_AT_HP_block_index";
13295 #endif
13296 case DW_AT_HP_unmodifiable:
13297 return "DW_AT_HP_unmodifiable";
13298 case DW_AT_HP_actuals_stmt_list:
13299 return "DW_AT_HP_actuals_stmt_list";
13300 case DW_AT_HP_proc_per_section:
13301 return "DW_AT_HP_proc_per_section";
13302 case DW_AT_HP_raw_data_ptr:
13303 return "DW_AT_HP_raw_data_ptr";
13304 case DW_AT_HP_pass_by_reference:
13305 return "DW_AT_HP_pass_by_reference";
13306 case DW_AT_HP_opt_level:
13307 return "DW_AT_HP_opt_level";
13308 case DW_AT_HP_prof_version_id:
13309 return "DW_AT_HP_prof_version_id";
13310 case DW_AT_HP_opt_flags:
13311 return "DW_AT_HP_opt_flags";
13312 case DW_AT_HP_cold_region_low_pc:
13313 return "DW_AT_HP_cold_region_low_pc";
13314 case DW_AT_HP_cold_region_high_pc:
13315 return "DW_AT_HP_cold_region_high_pc";
13316 case DW_AT_HP_all_variables_modifiable:
13317 return "DW_AT_HP_all_variables_modifiable";
13318 case DW_AT_HP_linkage_name:
13319 return "DW_AT_HP_linkage_name";
13320 case DW_AT_HP_prof_flags:
13321 return "DW_AT_HP_prof_flags";
13322 /* GNU extensions. */
13323 case DW_AT_sf_names:
13324 return "DW_AT_sf_names";
13325 case DW_AT_src_info:
13326 return "DW_AT_src_info";
13327 case DW_AT_mac_info:
13328 return "DW_AT_mac_info";
13329 case DW_AT_src_coords:
13330 return "DW_AT_src_coords";
13331 case DW_AT_body_begin:
13332 return "DW_AT_body_begin";
13333 case DW_AT_body_end:
13334 return "DW_AT_body_end";
13335 case DW_AT_GNU_vector:
13336 return "DW_AT_GNU_vector";
13337 case DW_AT_GNU_odr_signature:
13338 return "DW_AT_GNU_odr_signature";
13339 /* VMS extensions. */
13340 case DW_AT_VMS_rtnbeg_pd_address:
13341 return "DW_AT_VMS_rtnbeg_pd_address";
13342 /* UPC extension. */
13343 case DW_AT_upc_threads_scaled:
13344 return "DW_AT_upc_threads_scaled";
13345 /* PGI (STMicroelectronics) extensions. */
13346 case DW_AT_PGI_lbase:
13347 return "DW_AT_PGI_lbase";
13348 case DW_AT_PGI_soffset:
13349 return "DW_AT_PGI_soffset";
13350 case DW_AT_PGI_lstride:
13351 return "DW_AT_PGI_lstride";
13352 default:
13353 return "DW_AT_<unknown>";
13354 }
13355 }
13356
13357 /* Convert a DWARF value form code into its string name. */
13358
13359 static char *
13360 dwarf_form_name (unsigned form)
13361 {
13362 switch (form)
13363 {
13364 case DW_FORM_addr:
13365 return "DW_FORM_addr";
13366 case DW_FORM_block2:
13367 return "DW_FORM_block2";
13368 case DW_FORM_block4:
13369 return "DW_FORM_block4";
13370 case DW_FORM_data2:
13371 return "DW_FORM_data2";
13372 case DW_FORM_data4:
13373 return "DW_FORM_data4";
13374 case DW_FORM_data8:
13375 return "DW_FORM_data8";
13376 case DW_FORM_string:
13377 return "DW_FORM_string";
13378 case DW_FORM_block:
13379 return "DW_FORM_block";
13380 case DW_FORM_block1:
13381 return "DW_FORM_block1";
13382 case DW_FORM_data1:
13383 return "DW_FORM_data1";
13384 case DW_FORM_flag:
13385 return "DW_FORM_flag";
13386 case DW_FORM_sdata:
13387 return "DW_FORM_sdata";
13388 case DW_FORM_strp:
13389 return "DW_FORM_strp";
13390 case DW_FORM_udata:
13391 return "DW_FORM_udata";
13392 case DW_FORM_ref_addr:
13393 return "DW_FORM_ref_addr";
13394 case DW_FORM_ref1:
13395 return "DW_FORM_ref1";
13396 case DW_FORM_ref2:
13397 return "DW_FORM_ref2";
13398 case DW_FORM_ref4:
13399 return "DW_FORM_ref4";
13400 case DW_FORM_ref8:
13401 return "DW_FORM_ref8";
13402 case DW_FORM_ref_udata:
13403 return "DW_FORM_ref_udata";
13404 case DW_FORM_indirect:
13405 return "DW_FORM_indirect";
13406 case DW_FORM_sec_offset:
13407 return "DW_FORM_sec_offset";
13408 case DW_FORM_exprloc:
13409 return "DW_FORM_exprloc";
13410 case DW_FORM_flag_present:
13411 return "DW_FORM_flag_present";
13412 case DW_FORM_ref_sig8:
13413 return "DW_FORM_ref_sig8";
13414 default:
13415 return "DW_FORM_<unknown>";
13416 }
13417 }
13418
13419 /* Convert a DWARF stack opcode into its string name. */
13420
13421 const char *
13422 dwarf_stack_op_name (unsigned op)
13423 {
13424 switch (op)
13425 {
13426 case DW_OP_addr:
13427 return "DW_OP_addr";
13428 case DW_OP_deref:
13429 return "DW_OP_deref";
13430 case DW_OP_const1u:
13431 return "DW_OP_const1u";
13432 case DW_OP_const1s:
13433 return "DW_OP_const1s";
13434 case DW_OP_const2u:
13435 return "DW_OP_const2u";
13436 case DW_OP_const2s:
13437 return "DW_OP_const2s";
13438 case DW_OP_const4u:
13439 return "DW_OP_const4u";
13440 case DW_OP_const4s:
13441 return "DW_OP_const4s";
13442 case DW_OP_const8u:
13443 return "DW_OP_const8u";
13444 case DW_OP_const8s:
13445 return "DW_OP_const8s";
13446 case DW_OP_constu:
13447 return "DW_OP_constu";
13448 case DW_OP_consts:
13449 return "DW_OP_consts";
13450 case DW_OP_dup:
13451 return "DW_OP_dup";
13452 case DW_OP_drop:
13453 return "DW_OP_drop";
13454 case DW_OP_over:
13455 return "DW_OP_over";
13456 case DW_OP_pick:
13457 return "DW_OP_pick";
13458 case DW_OP_swap:
13459 return "DW_OP_swap";
13460 case DW_OP_rot:
13461 return "DW_OP_rot";
13462 case DW_OP_xderef:
13463 return "DW_OP_xderef";
13464 case DW_OP_abs:
13465 return "DW_OP_abs";
13466 case DW_OP_and:
13467 return "DW_OP_and";
13468 case DW_OP_div:
13469 return "DW_OP_div";
13470 case DW_OP_minus:
13471 return "DW_OP_minus";
13472 case DW_OP_mod:
13473 return "DW_OP_mod";
13474 case DW_OP_mul:
13475 return "DW_OP_mul";
13476 case DW_OP_neg:
13477 return "DW_OP_neg";
13478 case DW_OP_not:
13479 return "DW_OP_not";
13480 case DW_OP_or:
13481 return "DW_OP_or";
13482 case DW_OP_plus:
13483 return "DW_OP_plus";
13484 case DW_OP_plus_uconst:
13485 return "DW_OP_plus_uconst";
13486 case DW_OP_shl:
13487 return "DW_OP_shl";
13488 case DW_OP_shr:
13489 return "DW_OP_shr";
13490 case DW_OP_shra:
13491 return "DW_OP_shra";
13492 case DW_OP_xor:
13493 return "DW_OP_xor";
13494 case DW_OP_bra:
13495 return "DW_OP_bra";
13496 case DW_OP_eq:
13497 return "DW_OP_eq";
13498 case DW_OP_ge:
13499 return "DW_OP_ge";
13500 case DW_OP_gt:
13501 return "DW_OP_gt";
13502 case DW_OP_le:
13503 return "DW_OP_le";
13504 case DW_OP_lt:
13505 return "DW_OP_lt";
13506 case DW_OP_ne:
13507 return "DW_OP_ne";
13508 case DW_OP_skip:
13509 return "DW_OP_skip";
13510 case DW_OP_lit0:
13511 return "DW_OP_lit0";
13512 case DW_OP_lit1:
13513 return "DW_OP_lit1";
13514 case DW_OP_lit2:
13515 return "DW_OP_lit2";
13516 case DW_OP_lit3:
13517 return "DW_OP_lit3";
13518 case DW_OP_lit4:
13519 return "DW_OP_lit4";
13520 case DW_OP_lit5:
13521 return "DW_OP_lit5";
13522 case DW_OP_lit6:
13523 return "DW_OP_lit6";
13524 case DW_OP_lit7:
13525 return "DW_OP_lit7";
13526 case DW_OP_lit8:
13527 return "DW_OP_lit8";
13528 case DW_OP_lit9:
13529 return "DW_OP_lit9";
13530 case DW_OP_lit10:
13531 return "DW_OP_lit10";
13532 case DW_OP_lit11:
13533 return "DW_OP_lit11";
13534 case DW_OP_lit12:
13535 return "DW_OP_lit12";
13536 case DW_OP_lit13:
13537 return "DW_OP_lit13";
13538 case DW_OP_lit14:
13539 return "DW_OP_lit14";
13540 case DW_OP_lit15:
13541 return "DW_OP_lit15";
13542 case DW_OP_lit16:
13543 return "DW_OP_lit16";
13544 case DW_OP_lit17:
13545 return "DW_OP_lit17";
13546 case DW_OP_lit18:
13547 return "DW_OP_lit18";
13548 case DW_OP_lit19:
13549 return "DW_OP_lit19";
13550 case DW_OP_lit20:
13551 return "DW_OP_lit20";
13552 case DW_OP_lit21:
13553 return "DW_OP_lit21";
13554 case DW_OP_lit22:
13555 return "DW_OP_lit22";
13556 case DW_OP_lit23:
13557 return "DW_OP_lit23";
13558 case DW_OP_lit24:
13559 return "DW_OP_lit24";
13560 case DW_OP_lit25:
13561 return "DW_OP_lit25";
13562 case DW_OP_lit26:
13563 return "DW_OP_lit26";
13564 case DW_OP_lit27:
13565 return "DW_OP_lit27";
13566 case DW_OP_lit28:
13567 return "DW_OP_lit28";
13568 case DW_OP_lit29:
13569 return "DW_OP_lit29";
13570 case DW_OP_lit30:
13571 return "DW_OP_lit30";
13572 case DW_OP_lit31:
13573 return "DW_OP_lit31";
13574 case DW_OP_reg0:
13575 return "DW_OP_reg0";
13576 case DW_OP_reg1:
13577 return "DW_OP_reg1";
13578 case DW_OP_reg2:
13579 return "DW_OP_reg2";
13580 case DW_OP_reg3:
13581 return "DW_OP_reg3";
13582 case DW_OP_reg4:
13583 return "DW_OP_reg4";
13584 case DW_OP_reg5:
13585 return "DW_OP_reg5";
13586 case DW_OP_reg6:
13587 return "DW_OP_reg6";
13588 case DW_OP_reg7:
13589 return "DW_OP_reg7";
13590 case DW_OP_reg8:
13591 return "DW_OP_reg8";
13592 case DW_OP_reg9:
13593 return "DW_OP_reg9";
13594 case DW_OP_reg10:
13595 return "DW_OP_reg10";
13596 case DW_OP_reg11:
13597 return "DW_OP_reg11";
13598 case DW_OP_reg12:
13599 return "DW_OP_reg12";
13600 case DW_OP_reg13:
13601 return "DW_OP_reg13";
13602 case DW_OP_reg14:
13603 return "DW_OP_reg14";
13604 case DW_OP_reg15:
13605 return "DW_OP_reg15";
13606 case DW_OP_reg16:
13607 return "DW_OP_reg16";
13608 case DW_OP_reg17:
13609 return "DW_OP_reg17";
13610 case DW_OP_reg18:
13611 return "DW_OP_reg18";
13612 case DW_OP_reg19:
13613 return "DW_OP_reg19";
13614 case DW_OP_reg20:
13615 return "DW_OP_reg20";
13616 case DW_OP_reg21:
13617 return "DW_OP_reg21";
13618 case DW_OP_reg22:
13619 return "DW_OP_reg22";
13620 case DW_OP_reg23:
13621 return "DW_OP_reg23";
13622 case DW_OP_reg24:
13623 return "DW_OP_reg24";
13624 case DW_OP_reg25:
13625 return "DW_OP_reg25";
13626 case DW_OP_reg26:
13627 return "DW_OP_reg26";
13628 case DW_OP_reg27:
13629 return "DW_OP_reg27";
13630 case DW_OP_reg28:
13631 return "DW_OP_reg28";
13632 case DW_OP_reg29:
13633 return "DW_OP_reg29";
13634 case DW_OP_reg30:
13635 return "DW_OP_reg30";
13636 case DW_OP_reg31:
13637 return "DW_OP_reg31";
13638 case DW_OP_breg0:
13639 return "DW_OP_breg0";
13640 case DW_OP_breg1:
13641 return "DW_OP_breg1";
13642 case DW_OP_breg2:
13643 return "DW_OP_breg2";
13644 case DW_OP_breg3:
13645 return "DW_OP_breg3";
13646 case DW_OP_breg4:
13647 return "DW_OP_breg4";
13648 case DW_OP_breg5:
13649 return "DW_OP_breg5";
13650 case DW_OP_breg6:
13651 return "DW_OP_breg6";
13652 case DW_OP_breg7:
13653 return "DW_OP_breg7";
13654 case DW_OP_breg8:
13655 return "DW_OP_breg8";
13656 case DW_OP_breg9:
13657 return "DW_OP_breg9";
13658 case DW_OP_breg10:
13659 return "DW_OP_breg10";
13660 case DW_OP_breg11:
13661 return "DW_OP_breg11";
13662 case DW_OP_breg12:
13663 return "DW_OP_breg12";
13664 case DW_OP_breg13:
13665 return "DW_OP_breg13";
13666 case DW_OP_breg14:
13667 return "DW_OP_breg14";
13668 case DW_OP_breg15:
13669 return "DW_OP_breg15";
13670 case DW_OP_breg16:
13671 return "DW_OP_breg16";
13672 case DW_OP_breg17:
13673 return "DW_OP_breg17";
13674 case DW_OP_breg18:
13675 return "DW_OP_breg18";
13676 case DW_OP_breg19:
13677 return "DW_OP_breg19";
13678 case DW_OP_breg20:
13679 return "DW_OP_breg20";
13680 case DW_OP_breg21:
13681 return "DW_OP_breg21";
13682 case DW_OP_breg22:
13683 return "DW_OP_breg22";
13684 case DW_OP_breg23:
13685 return "DW_OP_breg23";
13686 case DW_OP_breg24:
13687 return "DW_OP_breg24";
13688 case DW_OP_breg25:
13689 return "DW_OP_breg25";
13690 case DW_OP_breg26:
13691 return "DW_OP_breg26";
13692 case DW_OP_breg27:
13693 return "DW_OP_breg27";
13694 case DW_OP_breg28:
13695 return "DW_OP_breg28";
13696 case DW_OP_breg29:
13697 return "DW_OP_breg29";
13698 case DW_OP_breg30:
13699 return "DW_OP_breg30";
13700 case DW_OP_breg31:
13701 return "DW_OP_breg31";
13702 case DW_OP_regx:
13703 return "DW_OP_regx";
13704 case DW_OP_fbreg:
13705 return "DW_OP_fbreg";
13706 case DW_OP_bregx:
13707 return "DW_OP_bregx";
13708 case DW_OP_piece:
13709 return "DW_OP_piece";
13710 case DW_OP_deref_size:
13711 return "DW_OP_deref_size";
13712 case DW_OP_xderef_size:
13713 return "DW_OP_xderef_size";
13714 case DW_OP_nop:
13715 return "DW_OP_nop";
13716 /* DWARF 3 extensions. */
13717 case DW_OP_push_object_address:
13718 return "DW_OP_push_object_address";
13719 case DW_OP_call2:
13720 return "DW_OP_call2";
13721 case DW_OP_call4:
13722 return "DW_OP_call4";
13723 case DW_OP_call_ref:
13724 return "DW_OP_call_ref";
13725 case DW_OP_form_tls_address:
13726 return "DW_OP_form_tls_address";
13727 case DW_OP_call_frame_cfa:
13728 return "DW_OP_call_frame_cfa";
13729 case DW_OP_bit_piece:
13730 return "DW_OP_bit_piece";
13731 /* DWARF 4 extensions. */
13732 case DW_OP_implicit_value:
13733 return "DW_OP_implicit_value";
13734 case DW_OP_stack_value:
13735 return "DW_OP_stack_value";
13736 /* GNU extensions. */
13737 case DW_OP_GNU_push_tls_address:
13738 return "DW_OP_GNU_push_tls_address";
13739 case DW_OP_GNU_uninit:
13740 return "DW_OP_GNU_uninit";
13741 case DW_OP_GNU_encoded_addr:
13742 return "DW_OP_GNU_encoded_addr";
13743 case DW_OP_GNU_implicit_pointer:
13744 return "DW_OP_GNU_implicit_pointer";
13745 case DW_OP_GNU_entry_value:
13746 return "DW_OP_GNU_entry_value";
13747 case DW_OP_GNU_const_type:
13748 return "DW_OP_GNU_const_type";
13749 case DW_OP_GNU_regval_type:
13750 return "DW_OP_GNU_regval_type";
13751 case DW_OP_GNU_deref_type:
13752 return "DW_OP_GNU_deref_type";
13753 case DW_OP_GNU_convert:
13754 return "DW_OP_GNU_convert";
13755 case DW_OP_GNU_reinterpret:
13756 return "DW_OP_GNU_reinterpret";
13757 case DW_OP_GNU_parameter_ref:
13758 return "DW_OP_GNU_parameter_ref";
13759 default:
13760 return NULL;
13761 }
13762 }
13763
13764 static char *
13765 dwarf_bool_name (unsigned mybool)
13766 {
13767 if (mybool)
13768 return "TRUE";
13769 else
13770 return "FALSE";
13771 }
13772
13773 /* Convert a DWARF type code into its string name. */
13774
13775 static char *
13776 dwarf_type_encoding_name (unsigned enc)
13777 {
13778 switch (enc)
13779 {
13780 case DW_ATE_void:
13781 return "DW_ATE_void";
13782 case DW_ATE_address:
13783 return "DW_ATE_address";
13784 case DW_ATE_boolean:
13785 return "DW_ATE_boolean";
13786 case DW_ATE_complex_float:
13787 return "DW_ATE_complex_float";
13788 case DW_ATE_float:
13789 return "DW_ATE_float";
13790 case DW_ATE_signed:
13791 return "DW_ATE_signed";
13792 case DW_ATE_signed_char:
13793 return "DW_ATE_signed_char";
13794 case DW_ATE_unsigned:
13795 return "DW_ATE_unsigned";
13796 case DW_ATE_unsigned_char:
13797 return "DW_ATE_unsigned_char";
13798 /* DWARF 3. */
13799 case DW_ATE_imaginary_float:
13800 return "DW_ATE_imaginary_float";
13801 case DW_ATE_packed_decimal:
13802 return "DW_ATE_packed_decimal";
13803 case DW_ATE_numeric_string:
13804 return "DW_ATE_numeric_string";
13805 case DW_ATE_edited:
13806 return "DW_ATE_edited";
13807 case DW_ATE_signed_fixed:
13808 return "DW_ATE_signed_fixed";
13809 case DW_ATE_unsigned_fixed:
13810 return "DW_ATE_unsigned_fixed";
13811 case DW_ATE_decimal_float:
13812 return "DW_ATE_decimal_float";
13813 /* DWARF 4. */
13814 case DW_ATE_UTF:
13815 return "DW_ATE_UTF";
13816 /* HP extensions. */
13817 case DW_ATE_HP_float80:
13818 return "DW_ATE_HP_float80";
13819 case DW_ATE_HP_complex_float80:
13820 return "DW_ATE_HP_complex_float80";
13821 case DW_ATE_HP_float128:
13822 return "DW_ATE_HP_float128";
13823 case DW_ATE_HP_complex_float128:
13824 return "DW_ATE_HP_complex_float128";
13825 case DW_ATE_HP_floathpintel:
13826 return "DW_ATE_HP_floathpintel";
13827 case DW_ATE_HP_imaginary_float80:
13828 return "DW_ATE_HP_imaginary_float80";
13829 case DW_ATE_HP_imaginary_float128:
13830 return "DW_ATE_HP_imaginary_float128";
13831 default:
13832 return "DW_ATE_<unknown>";
13833 }
13834 }
13835
13836 /* Convert a DWARF call frame info operation to its string name. */
13837
13838 #if 0
13839 static char *
13840 dwarf_cfi_name (unsigned cfi_opc)
13841 {
13842 switch (cfi_opc)
13843 {
13844 case DW_CFA_advance_loc:
13845 return "DW_CFA_advance_loc";
13846 case DW_CFA_offset:
13847 return "DW_CFA_offset";
13848 case DW_CFA_restore:
13849 return "DW_CFA_restore";
13850 case DW_CFA_nop:
13851 return "DW_CFA_nop";
13852 case DW_CFA_set_loc:
13853 return "DW_CFA_set_loc";
13854 case DW_CFA_advance_loc1:
13855 return "DW_CFA_advance_loc1";
13856 case DW_CFA_advance_loc2:
13857 return "DW_CFA_advance_loc2";
13858 case DW_CFA_advance_loc4:
13859 return "DW_CFA_advance_loc4";
13860 case DW_CFA_offset_extended:
13861 return "DW_CFA_offset_extended";
13862 case DW_CFA_restore_extended:
13863 return "DW_CFA_restore_extended";
13864 case DW_CFA_undefined:
13865 return "DW_CFA_undefined";
13866 case DW_CFA_same_value:
13867 return "DW_CFA_same_value";
13868 case DW_CFA_register:
13869 return "DW_CFA_register";
13870 case DW_CFA_remember_state:
13871 return "DW_CFA_remember_state";
13872 case DW_CFA_restore_state:
13873 return "DW_CFA_restore_state";
13874 case DW_CFA_def_cfa:
13875 return "DW_CFA_def_cfa";
13876 case DW_CFA_def_cfa_register:
13877 return "DW_CFA_def_cfa_register";
13878 case DW_CFA_def_cfa_offset:
13879 return "DW_CFA_def_cfa_offset";
13880 /* DWARF 3. */
13881 case DW_CFA_def_cfa_expression:
13882 return "DW_CFA_def_cfa_expression";
13883 case DW_CFA_expression:
13884 return "DW_CFA_expression";
13885 case DW_CFA_offset_extended_sf:
13886 return "DW_CFA_offset_extended_sf";
13887 case DW_CFA_def_cfa_sf:
13888 return "DW_CFA_def_cfa_sf";
13889 case DW_CFA_def_cfa_offset_sf:
13890 return "DW_CFA_def_cfa_offset_sf";
13891 case DW_CFA_val_offset:
13892 return "DW_CFA_val_offset";
13893 case DW_CFA_val_offset_sf:
13894 return "DW_CFA_val_offset_sf";
13895 case DW_CFA_val_expression:
13896 return "DW_CFA_val_expression";
13897 /* SGI/MIPS specific. */
13898 case DW_CFA_MIPS_advance_loc8:
13899 return "DW_CFA_MIPS_advance_loc8";
13900 /* GNU extensions. */
13901 case DW_CFA_GNU_window_save:
13902 return "DW_CFA_GNU_window_save";
13903 case DW_CFA_GNU_args_size:
13904 return "DW_CFA_GNU_args_size";
13905 case DW_CFA_GNU_negative_offset_extended:
13906 return "DW_CFA_GNU_negative_offset_extended";
13907 default:
13908 return "DW_CFA_<unknown>";
13909 }
13910 }
13911 #endif
13912
13913 static void
13914 dump_die_shallow (struct ui_file *f, int indent, struct die_info *die)
13915 {
13916 unsigned int i;
13917
13918 print_spaces (indent, f);
13919 fprintf_unfiltered (f, "Die: %s (abbrev %d, offset 0x%x)\n",
13920 dwarf_tag_name (die->tag), die->abbrev, die->offset);
13921
13922 if (die->parent != NULL)
13923 {
13924 print_spaces (indent, f);
13925 fprintf_unfiltered (f, " parent at offset: 0x%x\n",
13926 die->parent->offset);
13927 }
13928
13929 print_spaces (indent, f);
13930 fprintf_unfiltered (f, " has children: %s\n",
13931 dwarf_bool_name (die->child != NULL));
13932
13933 print_spaces (indent, f);
13934 fprintf_unfiltered (f, " attributes:\n");
13935
13936 for (i = 0; i < die->num_attrs; ++i)
13937 {
13938 print_spaces (indent, f);
13939 fprintf_unfiltered (f, " %s (%s) ",
13940 dwarf_attr_name (die->attrs[i].name),
13941 dwarf_form_name (die->attrs[i].form));
13942
13943 switch (die->attrs[i].form)
13944 {
13945 case DW_FORM_ref_addr:
13946 case DW_FORM_addr:
13947 fprintf_unfiltered (f, "address: ");
13948 fputs_filtered (hex_string (DW_ADDR (&die->attrs[i])), f);
13949 break;
13950 case DW_FORM_block2:
13951 case DW_FORM_block4:
13952 case DW_FORM_block:
13953 case DW_FORM_block1:
13954 fprintf_unfiltered (f, "block: size %d",
13955 DW_BLOCK (&die->attrs[i])->size);
13956 break;
13957 case DW_FORM_exprloc:
13958 fprintf_unfiltered (f, "expression: size %u",
13959 DW_BLOCK (&die->attrs[i])->size);
13960 break;
13961 case DW_FORM_ref1:
13962 case DW_FORM_ref2:
13963 case DW_FORM_ref4:
13964 fprintf_unfiltered (f, "constant ref: 0x%lx (adjusted)",
13965 (long) (DW_ADDR (&die->attrs[i])));
13966 break;
13967 case DW_FORM_data1:
13968 case DW_FORM_data2:
13969 case DW_FORM_data4:
13970 case DW_FORM_data8:
13971 case DW_FORM_udata:
13972 case DW_FORM_sdata:
13973 fprintf_unfiltered (f, "constant: %s",
13974 pulongest (DW_UNSND (&die->attrs[i])));
13975 break;
13976 case DW_FORM_sec_offset:
13977 fprintf_unfiltered (f, "section offset: %s",
13978 pulongest (DW_UNSND (&die->attrs[i])));
13979 break;
13980 case DW_FORM_ref_sig8:
13981 if (DW_SIGNATURED_TYPE (&die->attrs[i]) != NULL)
13982 fprintf_unfiltered (f, "signatured type, offset: 0x%x",
13983 DW_SIGNATURED_TYPE (&die->attrs[i])->per_cu.offset);
13984 else
13985 fprintf_unfiltered (f, "signatured type, offset: unknown");
13986 break;
13987 case DW_FORM_string:
13988 case DW_FORM_strp:
13989 fprintf_unfiltered (f, "string: \"%s\" (%s canonicalized)",
13990 DW_STRING (&die->attrs[i])
13991 ? DW_STRING (&die->attrs[i]) : "",
13992 DW_STRING_IS_CANONICAL (&die->attrs[i]) ? "is" : "not");
13993 break;
13994 case DW_FORM_flag:
13995 if (DW_UNSND (&die->attrs[i]))
13996 fprintf_unfiltered (f, "flag: TRUE");
13997 else
13998 fprintf_unfiltered (f, "flag: FALSE");
13999 break;
14000 case DW_FORM_flag_present:
14001 fprintf_unfiltered (f, "flag: TRUE");
14002 break;
14003 case DW_FORM_indirect:
14004 /* The reader will have reduced the indirect form to
14005 the "base form" so this form should not occur. */
14006 fprintf_unfiltered (f,
14007 "unexpected attribute form: DW_FORM_indirect");
14008 break;
14009 default:
14010 fprintf_unfiltered (f, "unsupported attribute form: %d.",
14011 die->attrs[i].form);
14012 break;
14013 }
14014 fprintf_unfiltered (f, "\n");
14015 }
14016 }
14017
14018 static void
14019 dump_die_for_error (struct die_info *die)
14020 {
14021 dump_die_shallow (gdb_stderr, 0, die);
14022 }
14023
14024 static void
14025 dump_die_1 (struct ui_file *f, int level, int max_level, struct die_info *die)
14026 {
14027 int indent = level * 4;
14028
14029 gdb_assert (die != NULL);
14030
14031 if (level >= max_level)
14032 return;
14033
14034 dump_die_shallow (f, indent, die);
14035
14036 if (die->child != NULL)
14037 {
14038 print_spaces (indent, f);
14039 fprintf_unfiltered (f, " Children:");
14040 if (level + 1 < max_level)
14041 {
14042 fprintf_unfiltered (f, "\n");
14043 dump_die_1 (f, level + 1, max_level, die->child);
14044 }
14045 else
14046 {
14047 fprintf_unfiltered (f,
14048 " [not printed, max nesting level reached]\n");
14049 }
14050 }
14051
14052 if (die->sibling != NULL && level > 0)
14053 {
14054 dump_die_1 (f, level, max_level, die->sibling);
14055 }
14056 }
14057
14058 /* This is called from the pdie macro in gdbinit.in.
14059 It's not static so gcc will keep a copy callable from gdb. */
14060
14061 void
14062 dump_die (struct die_info *die, int max_level)
14063 {
14064 dump_die_1 (gdb_stdlog, 0, max_level, die);
14065 }
14066
14067 static void
14068 store_in_ref_table (struct die_info *die, struct dwarf2_cu *cu)
14069 {
14070 void **slot;
14071
14072 slot = htab_find_slot_with_hash (cu->die_hash, die, die->offset, INSERT);
14073
14074 *slot = die;
14075 }
14076
14077 static int
14078 is_ref_attr (struct attribute *attr)
14079 {
14080 switch (attr->form)
14081 {
14082 case DW_FORM_ref_addr:
14083 case DW_FORM_ref1:
14084 case DW_FORM_ref2:
14085 case DW_FORM_ref4:
14086 case DW_FORM_ref8:
14087 case DW_FORM_ref_udata:
14088 return 1;
14089 default:
14090 return 0;
14091 }
14092 }
14093
14094 static unsigned int
14095 dwarf2_get_ref_die_offset (struct attribute *attr)
14096 {
14097 if (is_ref_attr (attr))
14098 return DW_ADDR (attr);
14099
14100 complaint (&symfile_complaints,
14101 _("unsupported die ref attribute form: '%s'"),
14102 dwarf_form_name (attr->form));
14103 return 0;
14104 }
14105
14106 /* Return the constant value held by ATTR. Return DEFAULT_VALUE if
14107 * the value held by the attribute is not constant. */
14108
14109 static LONGEST
14110 dwarf2_get_attr_constant_value (struct attribute *attr, int default_value)
14111 {
14112 if (attr->form == DW_FORM_sdata)
14113 return DW_SND (attr);
14114 else if (attr->form == DW_FORM_udata
14115 || attr->form == DW_FORM_data1
14116 || attr->form == DW_FORM_data2
14117 || attr->form == DW_FORM_data4
14118 || attr->form == DW_FORM_data8)
14119 return DW_UNSND (attr);
14120 else
14121 {
14122 complaint (&symfile_complaints,
14123 _("Attribute value is not a constant (%s)"),
14124 dwarf_form_name (attr->form));
14125 return default_value;
14126 }
14127 }
14128
14129 /* THIS_CU has a reference to PER_CU. If necessary, load the new compilation
14130 unit and add it to our queue.
14131 The result is non-zero if PER_CU was queued, otherwise the result is zero
14132 meaning either PER_CU is already queued or it is already loaded. */
14133
14134 static int
14135 maybe_queue_comp_unit (struct dwarf2_cu *this_cu,
14136 struct dwarf2_per_cu_data *per_cu)
14137 {
14138 /* We may arrive here during partial symbol reading, if we need full
14139 DIEs to process an unusual case (e.g. template arguments). Do
14140 not queue PER_CU, just tell our caller to load its DIEs. */
14141 if (dwarf2_per_objfile->reading_partial_symbols)
14142 {
14143 if (per_cu->cu == NULL || per_cu->cu->dies == NULL)
14144 return 1;
14145 return 0;
14146 }
14147
14148 /* Mark the dependence relation so that we don't flush PER_CU
14149 too early. */
14150 dwarf2_add_dependence (this_cu, per_cu);
14151
14152 /* If it's already on the queue, we have nothing to do. */
14153 if (per_cu->queued)
14154 return 0;
14155
14156 /* If the compilation unit is already loaded, just mark it as
14157 used. */
14158 if (per_cu->cu != NULL)
14159 {
14160 per_cu->cu->last_used = 0;
14161 return 0;
14162 }
14163
14164 /* Add it to the queue. */
14165 queue_comp_unit (per_cu);
14166
14167 return 1;
14168 }
14169
14170 /* Follow reference or signature attribute ATTR of SRC_DIE.
14171 On entry *REF_CU is the CU of SRC_DIE.
14172 On exit *REF_CU is the CU of the result. */
14173
14174 static struct die_info *
14175 follow_die_ref_or_sig (struct die_info *src_die, struct attribute *attr,
14176 struct dwarf2_cu **ref_cu)
14177 {
14178 struct die_info *die;
14179
14180 if (is_ref_attr (attr))
14181 die = follow_die_ref (src_die, attr, ref_cu);
14182 else if (attr->form == DW_FORM_ref_sig8)
14183 die = follow_die_sig (src_die, attr, ref_cu);
14184 else
14185 {
14186 dump_die_for_error (src_die);
14187 error (_("Dwarf Error: Expected reference attribute [in module %s]"),
14188 (*ref_cu)->objfile->name);
14189 }
14190
14191 return die;
14192 }
14193
14194 /* Follow reference OFFSET.
14195 On entry *REF_CU is the CU of the source die referencing OFFSET.
14196 On exit *REF_CU is the CU of the result.
14197 Returns NULL if OFFSET is invalid. */
14198
14199 static struct die_info *
14200 follow_die_offset (unsigned int offset, struct dwarf2_cu **ref_cu)
14201 {
14202 struct die_info temp_die;
14203 struct dwarf2_cu *target_cu, *cu = *ref_cu;
14204
14205 gdb_assert (cu->per_cu != NULL);
14206
14207 target_cu = cu;
14208
14209 if (cu->per_cu->debug_types_section)
14210 {
14211 /* .debug_types CUs cannot reference anything outside their CU.
14212 If they need to, they have to reference a signatured type via
14213 DW_FORM_ref_sig8. */
14214 if (! offset_in_cu_p (&cu->header, offset))
14215 return NULL;
14216 }
14217 else if (! offset_in_cu_p (&cu->header, offset))
14218 {
14219 struct dwarf2_per_cu_data *per_cu;
14220
14221 per_cu = dwarf2_find_containing_comp_unit (offset, cu->objfile);
14222
14223 /* If necessary, add it to the queue and load its DIEs. */
14224 if (maybe_queue_comp_unit (cu, per_cu))
14225 load_full_comp_unit (per_cu);
14226
14227 target_cu = per_cu->cu;
14228 }
14229 else if (cu->dies == NULL)
14230 {
14231 /* We're loading full DIEs during partial symbol reading. */
14232 gdb_assert (dwarf2_per_objfile->reading_partial_symbols);
14233 load_full_comp_unit (cu->per_cu);
14234 }
14235
14236 *ref_cu = target_cu;
14237 temp_die.offset = offset;
14238 return htab_find_with_hash (target_cu->die_hash, &temp_die, offset);
14239 }
14240
14241 /* Follow reference attribute ATTR of SRC_DIE.
14242 On entry *REF_CU is the CU of SRC_DIE.
14243 On exit *REF_CU is the CU of the result. */
14244
14245 static struct die_info *
14246 follow_die_ref (struct die_info *src_die, struct attribute *attr,
14247 struct dwarf2_cu **ref_cu)
14248 {
14249 unsigned int offset = dwarf2_get_ref_die_offset (attr);
14250 struct dwarf2_cu *cu = *ref_cu;
14251 struct die_info *die;
14252
14253 die = follow_die_offset (offset, ref_cu);
14254 if (!die)
14255 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced from DIE "
14256 "at 0x%x [in module %s]"),
14257 offset, src_die->offset, cu->objfile->name);
14258
14259 return die;
14260 }
14261
14262 /* Return DWARF block referenced by DW_AT_location of DIE at OFFSET at PER_CU.
14263 Returned value is intended for DW_OP_call*. Returned
14264 dwarf2_locexpr_baton->data has lifetime of PER_CU->OBJFILE. */
14265
14266 struct dwarf2_locexpr_baton
14267 dwarf2_fetch_die_location_block (unsigned int offset_in_cu,
14268 struct dwarf2_per_cu_data *per_cu,
14269 CORE_ADDR (*get_frame_pc) (void *baton),
14270 void *baton)
14271 {
14272 unsigned int offset = per_cu->offset + offset_in_cu;
14273 struct dwarf2_cu *cu;
14274 struct die_info *die;
14275 struct attribute *attr;
14276 struct dwarf2_locexpr_baton retval;
14277
14278 dw2_setup (per_cu->objfile);
14279
14280 if (per_cu->cu == NULL)
14281 load_cu (per_cu);
14282 cu = per_cu->cu;
14283
14284 die = follow_die_offset (offset, &cu);
14285 if (!die)
14286 error (_("Dwarf Error: Cannot find DIE at 0x%x referenced in module %s"),
14287 offset, per_cu->objfile->name);
14288
14289 attr = dwarf2_attr (die, DW_AT_location, cu);
14290 if (!attr)
14291 {
14292 /* DWARF: "If there is no such attribute, then there is no effect.".
14293 DATA is ignored if SIZE is 0. */
14294
14295 retval.data = NULL;
14296 retval.size = 0;
14297 }
14298 else if (attr_form_is_section_offset (attr))
14299 {
14300 struct dwarf2_loclist_baton loclist_baton;
14301 CORE_ADDR pc = (*get_frame_pc) (baton);
14302 size_t size;
14303
14304 fill_in_loclist_baton (cu, &loclist_baton, attr);
14305
14306 retval.data = dwarf2_find_location_expression (&loclist_baton,
14307 &size, pc);
14308 retval.size = size;
14309 }
14310 else
14311 {
14312 if (!attr_form_is_block (attr))
14313 error (_("Dwarf Error: DIE at 0x%x referenced in module %s "
14314 "is neither DW_FORM_block* nor DW_FORM_exprloc"),
14315 offset, per_cu->objfile->name);
14316
14317 retval.data = DW_BLOCK (attr)->data;
14318 retval.size = DW_BLOCK (attr)->size;
14319 }
14320 retval.per_cu = cu->per_cu;
14321
14322 age_cached_comp_units ();
14323
14324 return retval;
14325 }
14326
14327 /* Return the type of the DIE at DIE_OFFSET in the CU named by
14328 PER_CU. */
14329
14330 struct type *
14331 dwarf2_get_die_type (unsigned int die_offset,
14332 struct dwarf2_per_cu_data *per_cu)
14333 {
14334 dw2_setup (per_cu->objfile);
14335 return get_die_type_at_offset (per_cu->offset + die_offset, per_cu);
14336 }
14337
14338 /* Follow the signature attribute ATTR in SRC_DIE.
14339 On entry *REF_CU is the CU of SRC_DIE.
14340 On exit *REF_CU is the CU of the result. */
14341
14342 static struct die_info *
14343 follow_die_sig (struct die_info *src_die, struct attribute *attr,
14344 struct dwarf2_cu **ref_cu)
14345 {
14346 struct objfile *objfile = (*ref_cu)->objfile;
14347 struct die_info temp_die;
14348 struct signatured_type *sig_type = DW_SIGNATURED_TYPE (attr);
14349 struct dwarf2_cu *sig_cu;
14350 struct die_info *die;
14351
14352 /* sig_type will be NULL if the signatured type is missing from
14353 the debug info. */
14354 if (sig_type == NULL)
14355 error (_("Dwarf Error: Cannot find signatured DIE referenced from DIE "
14356 "at 0x%x [in module %s]"),
14357 src_die->offset, objfile->name);
14358
14359 /* If necessary, add it to the queue and load its DIEs. */
14360
14361 if (maybe_queue_comp_unit (*ref_cu, &sig_type->per_cu))
14362 read_signatured_type (sig_type);
14363
14364 gdb_assert (sig_type->per_cu.cu != NULL);
14365
14366 sig_cu = sig_type->per_cu.cu;
14367 temp_die.offset = sig_cu->header.offset + sig_type->type_offset;
14368 die = htab_find_with_hash (sig_cu->die_hash, &temp_die, temp_die.offset);
14369 if (die)
14370 {
14371 *ref_cu = sig_cu;
14372 return die;
14373 }
14374
14375 error (_("Dwarf Error: Cannot find signatured DIE at 0x%x referenced "
14376 "from DIE at 0x%x [in module %s]"),
14377 sig_type->type_offset, src_die->offset, objfile->name);
14378 }
14379
14380 /* Given an offset of a signatured type, return its signatured_type. */
14381
14382 static struct signatured_type *
14383 lookup_signatured_type_at_offset (struct objfile *objfile,
14384 struct dwarf2_section_info *section,
14385 unsigned int offset)
14386 {
14387 gdb_byte *info_ptr = section->buffer + offset;
14388 unsigned int length, initial_length_size;
14389 unsigned int sig_offset;
14390 struct signatured_type find_entry, *type_sig;
14391
14392 length = read_initial_length (objfile->obfd, info_ptr, &initial_length_size);
14393 sig_offset = (initial_length_size
14394 + 2 /*version*/
14395 + (initial_length_size == 4 ? 4 : 8) /*debug_abbrev_offset*/
14396 + 1 /*address_size*/);
14397 find_entry.signature = bfd_get_64 (objfile->obfd, info_ptr + sig_offset);
14398 type_sig = htab_find (dwarf2_per_objfile->signatured_types, &find_entry);
14399
14400 /* This is only used to lookup previously recorded types.
14401 If we didn't find it, it's our bug. */
14402 gdb_assert (type_sig != NULL);
14403 gdb_assert (offset == type_sig->per_cu.offset);
14404
14405 return type_sig;
14406 }
14407
14408 /* Load the DIEs associated with type unit PER_CU into memory. */
14409
14410 static void
14411 load_full_type_unit (struct dwarf2_per_cu_data *per_cu)
14412 {
14413 struct objfile *objfile = per_cu->objfile;
14414 struct dwarf2_section_info *sect = per_cu->debug_types_section;
14415 unsigned int offset = per_cu->offset;
14416 struct signatured_type *type_sig;
14417
14418 dwarf2_read_section (objfile, sect);
14419
14420 /* We have the section offset, but we need the signature to do the
14421 hash table lookup. */
14422 /* FIXME: This is sorta unnecessary, read_signatured_type only uses
14423 the signature to assert we found the right one.
14424 Ok, but it's a lot of work. We should simplify things so any needed
14425 assert doesn't require all this clumsiness. */
14426 type_sig = lookup_signatured_type_at_offset (objfile, sect, offset);
14427
14428 gdb_assert (type_sig->per_cu.cu == NULL);
14429
14430 read_signatured_type (type_sig);
14431
14432 gdb_assert (type_sig->per_cu.cu != NULL);
14433 }
14434
14435 /* Read in a signatured type and build its CU and DIEs. */
14436
14437 static void
14438 read_signatured_type (struct signatured_type *type_sig)
14439 {
14440 struct objfile *objfile = type_sig->per_cu.objfile;
14441 gdb_byte *types_ptr;
14442 struct die_reader_specs reader_specs;
14443 struct dwarf2_cu *cu;
14444 ULONGEST signature;
14445 struct cleanup *back_to, *free_cu_cleanup;
14446 struct dwarf2_section_info *section = type_sig->per_cu.debug_types_section;
14447
14448 dwarf2_read_section (objfile, section);
14449 types_ptr = section->buffer + type_sig->per_cu.offset;
14450
14451 gdb_assert (type_sig->per_cu.cu == NULL);
14452
14453 cu = xmalloc (sizeof (*cu));
14454 init_one_comp_unit (cu, &type_sig->per_cu);
14455
14456 /* If an error occurs while loading, release our storage. */
14457 free_cu_cleanup = make_cleanup (free_heap_comp_unit, cu);
14458
14459 types_ptr = read_and_check_type_unit_head (&cu->header, section, types_ptr,
14460 &signature, NULL);
14461 gdb_assert (signature == type_sig->signature);
14462
14463 cu->die_hash
14464 = htab_create_alloc_ex (cu->header.length / 12,
14465 die_hash,
14466 die_eq,
14467 NULL,
14468 &cu->comp_unit_obstack,
14469 hashtab_obstack_allocate,
14470 dummy_obstack_deallocate);
14471
14472 dwarf2_read_abbrevs (cu);
14473 back_to = make_cleanup (dwarf2_free_abbrev_table, cu);
14474
14475 init_cu_die_reader (&reader_specs, cu);
14476
14477 cu->dies = read_die_and_children (&reader_specs, types_ptr, &types_ptr,
14478 NULL /*parent*/);
14479
14480 /* We try not to read any attributes in this function, because not
14481 all CUs needed for references have been loaded yet, and symbol
14482 table processing isn't initialized. But we have to set the CU language,
14483 or we won't be able to build types correctly. */
14484 prepare_one_comp_unit (cu, cu->dies);
14485
14486 do_cleanups (back_to);
14487
14488 /* We've successfully allocated this compilation unit. Let our caller
14489 clean it up when finished with it. */
14490 discard_cleanups (free_cu_cleanup);
14491
14492 /* Link this TU into read_in_chain. */
14493 type_sig->per_cu.cu->read_in_chain = dwarf2_per_objfile->read_in_chain;
14494 dwarf2_per_objfile->read_in_chain = &type_sig->per_cu;
14495 }
14496
14497 /* Decode simple location descriptions.
14498 Given a pointer to a dwarf block that defines a location, compute
14499 the location and return the value.
14500
14501 NOTE drow/2003-11-18: This function is called in two situations
14502 now: for the address of static or global variables (partial symbols
14503 only) and for offsets into structures which are expected to be
14504 (more or less) constant. The partial symbol case should go away,
14505 and only the constant case should remain. That will let this
14506 function complain more accurately. A few special modes are allowed
14507 without complaint for global variables (for instance, global
14508 register values and thread-local values).
14509
14510 A location description containing no operations indicates that the
14511 object is optimized out. The return value is 0 for that case.
14512 FIXME drow/2003-11-16: No callers check for this case any more; soon all
14513 callers will only want a very basic result and this can become a
14514 complaint.
14515
14516 Note that stack[0] is unused except as a default error return. */
14517
14518 static CORE_ADDR
14519 decode_locdesc (struct dwarf_block *blk, struct dwarf2_cu *cu)
14520 {
14521 struct objfile *objfile = cu->objfile;
14522 int i;
14523 int size = blk->size;
14524 gdb_byte *data = blk->data;
14525 CORE_ADDR stack[64];
14526 int stacki;
14527 unsigned int bytes_read, unsnd;
14528 gdb_byte op;
14529
14530 i = 0;
14531 stacki = 0;
14532 stack[stacki] = 0;
14533 stack[++stacki] = 0;
14534
14535 while (i < size)
14536 {
14537 op = data[i++];
14538 switch (op)
14539 {
14540 case DW_OP_lit0:
14541 case DW_OP_lit1:
14542 case DW_OP_lit2:
14543 case DW_OP_lit3:
14544 case DW_OP_lit4:
14545 case DW_OP_lit5:
14546 case DW_OP_lit6:
14547 case DW_OP_lit7:
14548 case DW_OP_lit8:
14549 case DW_OP_lit9:
14550 case DW_OP_lit10:
14551 case DW_OP_lit11:
14552 case DW_OP_lit12:
14553 case DW_OP_lit13:
14554 case DW_OP_lit14:
14555 case DW_OP_lit15:
14556 case DW_OP_lit16:
14557 case DW_OP_lit17:
14558 case DW_OP_lit18:
14559 case DW_OP_lit19:
14560 case DW_OP_lit20:
14561 case DW_OP_lit21:
14562 case DW_OP_lit22:
14563 case DW_OP_lit23:
14564 case DW_OP_lit24:
14565 case DW_OP_lit25:
14566 case DW_OP_lit26:
14567 case DW_OP_lit27:
14568 case DW_OP_lit28:
14569 case DW_OP_lit29:
14570 case DW_OP_lit30:
14571 case DW_OP_lit31:
14572 stack[++stacki] = op - DW_OP_lit0;
14573 break;
14574
14575 case DW_OP_reg0:
14576 case DW_OP_reg1:
14577 case DW_OP_reg2:
14578 case DW_OP_reg3:
14579 case DW_OP_reg4:
14580 case DW_OP_reg5:
14581 case DW_OP_reg6:
14582 case DW_OP_reg7:
14583 case DW_OP_reg8:
14584 case DW_OP_reg9:
14585 case DW_OP_reg10:
14586 case DW_OP_reg11:
14587 case DW_OP_reg12:
14588 case DW_OP_reg13:
14589 case DW_OP_reg14:
14590 case DW_OP_reg15:
14591 case DW_OP_reg16:
14592 case DW_OP_reg17:
14593 case DW_OP_reg18:
14594 case DW_OP_reg19:
14595 case DW_OP_reg20:
14596 case DW_OP_reg21:
14597 case DW_OP_reg22:
14598 case DW_OP_reg23:
14599 case DW_OP_reg24:
14600 case DW_OP_reg25:
14601 case DW_OP_reg26:
14602 case DW_OP_reg27:
14603 case DW_OP_reg28:
14604 case DW_OP_reg29:
14605 case DW_OP_reg30:
14606 case DW_OP_reg31:
14607 stack[++stacki] = op - DW_OP_reg0;
14608 if (i < size)
14609 dwarf2_complex_location_expr_complaint ();
14610 break;
14611
14612 case DW_OP_regx:
14613 unsnd = read_unsigned_leb128 (NULL, (data + i), &bytes_read);
14614 i += bytes_read;
14615 stack[++stacki] = unsnd;
14616 if (i < size)
14617 dwarf2_complex_location_expr_complaint ();
14618 break;
14619
14620 case DW_OP_addr:
14621 stack[++stacki] = read_address (objfile->obfd, &data[i],
14622 cu, &bytes_read);
14623 i += bytes_read;
14624 break;
14625
14626 case DW_OP_const1u:
14627 stack[++stacki] = read_1_byte (objfile->obfd, &data[i]);
14628 i += 1;
14629 break;
14630
14631 case DW_OP_const1s:
14632 stack[++stacki] = read_1_signed_byte (objfile->obfd, &data[i]);
14633 i += 1;
14634 break;
14635
14636 case DW_OP_const2u:
14637 stack[++stacki] = read_2_bytes (objfile->obfd, &data[i]);
14638 i += 2;
14639 break;
14640
14641 case DW_OP_const2s:
14642 stack[++stacki] = read_2_signed_bytes (objfile->obfd, &data[i]);
14643 i += 2;
14644 break;
14645
14646 case DW_OP_const4u:
14647 stack[++stacki] = read_4_bytes (objfile->obfd, &data[i]);
14648 i += 4;
14649 break;
14650
14651 case DW_OP_const4s:
14652 stack[++stacki] = read_4_signed_bytes (objfile->obfd, &data[i]);
14653 i += 4;
14654 break;
14655
14656 case DW_OP_const8u:
14657 stack[++stacki] = read_8_bytes (objfile->obfd, &data[i]);
14658 i += 8;
14659 break;
14660
14661 case DW_OP_constu:
14662 stack[++stacki] = read_unsigned_leb128 (NULL, (data + i),
14663 &bytes_read);
14664 i += bytes_read;
14665 break;
14666
14667 case DW_OP_consts:
14668 stack[++stacki] = read_signed_leb128 (NULL, (data + i), &bytes_read);
14669 i += bytes_read;
14670 break;
14671
14672 case DW_OP_dup:
14673 stack[stacki + 1] = stack[stacki];
14674 stacki++;
14675 break;
14676
14677 case DW_OP_plus:
14678 stack[stacki - 1] += stack[stacki];
14679 stacki--;
14680 break;
14681
14682 case DW_OP_plus_uconst:
14683 stack[stacki] += read_unsigned_leb128 (NULL, (data + i),
14684 &bytes_read);
14685 i += bytes_read;
14686 break;
14687
14688 case DW_OP_minus:
14689 stack[stacki - 1] -= stack[stacki];
14690 stacki--;
14691 break;
14692
14693 case DW_OP_deref:
14694 /* If we're not the last op, then we definitely can't encode
14695 this using GDB's address_class enum. This is valid for partial
14696 global symbols, although the variable's address will be bogus
14697 in the psymtab. */
14698 if (i < size)
14699 dwarf2_complex_location_expr_complaint ();
14700 break;
14701
14702 case DW_OP_GNU_push_tls_address:
14703 /* The top of the stack has the offset from the beginning
14704 of the thread control block at which the variable is located. */
14705 /* Nothing should follow this operator, so the top of stack would
14706 be returned. */
14707 /* This is valid for partial global symbols, but the variable's
14708 address will be bogus in the psymtab. Make it always at least
14709 non-zero to not look as a variable garbage collected by linker
14710 which have DW_OP_addr 0. */
14711 if (i < size)
14712 dwarf2_complex_location_expr_complaint ();
14713 stack[stacki]++;
14714 break;
14715
14716 case DW_OP_GNU_uninit:
14717 break;
14718
14719 default:
14720 {
14721 const char *name = dwarf_stack_op_name (op);
14722
14723 if (name)
14724 complaint (&symfile_complaints, _("unsupported stack op: '%s'"),
14725 name);
14726 else
14727 complaint (&symfile_complaints, _("unsupported stack op: '%02x'"),
14728 op);
14729 }
14730
14731 return (stack[stacki]);
14732 }
14733
14734 /* Enforce maximum stack depth of SIZE-1 to avoid writing
14735 outside of the allocated space. Also enforce minimum>0. */
14736 if (stacki >= ARRAY_SIZE (stack) - 1)
14737 {
14738 complaint (&symfile_complaints,
14739 _("location description stack overflow"));
14740 return 0;
14741 }
14742
14743 if (stacki <= 0)
14744 {
14745 complaint (&symfile_complaints,
14746 _("location description stack underflow"));
14747 return 0;
14748 }
14749 }
14750 return (stack[stacki]);
14751 }
14752
14753 /* memory allocation interface */
14754
14755 static struct dwarf_block *
14756 dwarf_alloc_block (struct dwarf2_cu *cu)
14757 {
14758 struct dwarf_block *blk;
14759
14760 blk = (struct dwarf_block *)
14761 obstack_alloc (&cu->comp_unit_obstack, sizeof (struct dwarf_block));
14762 return (blk);
14763 }
14764
14765 static struct abbrev_info *
14766 dwarf_alloc_abbrev (struct dwarf2_cu *cu)
14767 {
14768 struct abbrev_info *abbrev;
14769
14770 abbrev = (struct abbrev_info *)
14771 obstack_alloc (&cu->abbrev_obstack, sizeof (struct abbrev_info));
14772 memset (abbrev, 0, sizeof (struct abbrev_info));
14773 return (abbrev);
14774 }
14775
14776 static struct die_info *
14777 dwarf_alloc_die (struct dwarf2_cu *cu, int num_attrs)
14778 {
14779 struct die_info *die;
14780 size_t size = sizeof (struct die_info);
14781
14782 if (num_attrs > 1)
14783 size += (num_attrs - 1) * sizeof (struct attribute);
14784
14785 die = (struct die_info *) obstack_alloc (&cu->comp_unit_obstack, size);
14786 memset (die, 0, sizeof (struct die_info));
14787 return (die);
14788 }
14789
14790 \f
14791 /* Macro support. */
14792
14793 /* Return the full name of file number I in *LH's file name table.
14794 Use COMP_DIR as the name of the current directory of the
14795 compilation. The result is allocated using xmalloc; the caller is
14796 responsible for freeing it. */
14797 static char *
14798 file_full_name (int file, struct line_header *lh, const char *comp_dir)
14799 {
14800 /* Is the file number a valid index into the line header's file name
14801 table? Remember that file numbers start with one, not zero. */
14802 if (1 <= file && file <= lh->num_file_names)
14803 {
14804 struct file_entry *fe = &lh->file_names[file - 1];
14805
14806 if (IS_ABSOLUTE_PATH (fe->name))
14807 return xstrdup (fe->name);
14808 else
14809 {
14810 const char *dir;
14811 int dir_len;
14812 char *full_name;
14813
14814 if (fe->dir_index)
14815 dir = lh->include_dirs[fe->dir_index - 1];
14816 else
14817 dir = comp_dir;
14818
14819 if (dir)
14820 {
14821 dir_len = strlen (dir);
14822 full_name = xmalloc (dir_len + 1 + strlen (fe->name) + 1);
14823 strcpy (full_name, dir);
14824 full_name[dir_len] = '/';
14825 strcpy (full_name + dir_len + 1, fe->name);
14826 return full_name;
14827 }
14828 else
14829 return xstrdup (fe->name);
14830 }
14831 }
14832 else
14833 {
14834 /* The compiler produced a bogus file number. We can at least
14835 record the macro definitions made in the file, even if we
14836 won't be able to find the file by name. */
14837 char fake_name[80];
14838
14839 sprintf (fake_name, "<bad macro file number %d>", file);
14840
14841 complaint (&symfile_complaints,
14842 _("bad file number in macro information (%d)"),
14843 file);
14844
14845 return xstrdup (fake_name);
14846 }
14847 }
14848
14849
14850 static struct macro_source_file *
14851 macro_start_file (int file, int line,
14852 struct macro_source_file *current_file,
14853 const char *comp_dir,
14854 struct line_header *lh, struct objfile *objfile)
14855 {
14856 /* The full name of this source file. */
14857 char *full_name = file_full_name (file, lh, comp_dir);
14858
14859 /* We don't create a macro table for this compilation unit
14860 at all until we actually get a filename. */
14861 if (! pending_macros)
14862 pending_macros = new_macro_table (&objfile->objfile_obstack,
14863 objfile->macro_cache);
14864
14865 if (! current_file)
14866 /* If we have no current file, then this must be the start_file
14867 directive for the compilation unit's main source file. */
14868 current_file = macro_set_main (pending_macros, full_name);
14869 else
14870 current_file = macro_include (current_file, line, full_name);
14871
14872 xfree (full_name);
14873
14874 return current_file;
14875 }
14876
14877
14878 /* Copy the LEN characters at BUF to a xmalloc'ed block of memory,
14879 followed by a null byte. */
14880 static char *
14881 copy_string (const char *buf, int len)
14882 {
14883 char *s = xmalloc (len + 1);
14884
14885 memcpy (s, buf, len);
14886 s[len] = '\0';
14887 return s;
14888 }
14889
14890
14891 static const char *
14892 consume_improper_spaces (const char *p, const char *body)
14893 {
14894 if (*p == ' ')
14895 {
14896 complaint (&symfile_complaints,
14897 _("macro definition contains spaces "
14898 "in formal argument list:\n`%s'"),
14899 body);
14900
14901 while (*p == ' ')
14902 p++;
14903 }
14904
14905 return p;
14906 }
14907
14908
14909 static void
14910 parse_macro_definition (struct macro_source_file *file, int line,
14911 const char *body)
14912 {
14913 const char *p;
14914
14915 /* The body string takes one of two forms. For object-like macro
14916 definitions, it should be:
14917
14918 <macro name> " " <definition>
14919
14920 For function-like macro definitions, it should be:
14921
14922 <macro name> "() " <definition>
14923 or
14924 <macro name> "(" <arg name> ( "," <arg name> ) * ") " <definition>
14925
14926 Spaces may appear only where explicitly indicated, and in the
14927 <definition>.
14928
14929 The Dwarf 2 spec says that an object-like macro's name is always
14930 followed by a space, but versions of GCC around March 2002 omit
14931 the space when the macro's definition is the empty string.
14932
14933 The Dwarf 2 spec says that there should be no spaces between the
14934 formal arguments in a function-like macro's formal argument list,
14935 but versions of GCC around March 2002 include spaces after the
14936 commas. */
14937
14938
14939 /* Find the extent of the macro name. The macro name is terminated
14940 by either a space or null character (for an object-like macro) or
14941 an opening paren (for a function-like macro). */
14942 for (p = body; *p; p++)
14943 if (*p == ' ' || *p == '(')
14944 break;
14945
14946 if (*p == ' ' || *p == '\0')
14947 {
14948 /* It's an object-like macro. */
14949 int name_len = p - body;
14950 char *name = copy_string (body, name_len);
14951 const char *replacement;
14952
14953 if (*p == ' ')
14954 replacement = body + name_len + 1;
14955 else
14956 {
14957 dwarf2_macro_malformed_definition_complaint (body);
14958 replacement = body + name_len;
14959 }
14960
14961 macro_define_object (file, line, name, replacement);
14962
14963 xfree (name);
14964 }
14965 else if (*p == '(')
14966 {
14967 /* It's a function-like macro. */
14968 char *name = copy_string (body, p - body);
14969 int argc = 0;
14970 int argv_size = 1;
14971 char **argv = xmalloc (argv_size * sizeof (*argv));
14972
14973 p++;
14974
14975 p = consume_improper_spaces (p, body);
14976
14977 /* Parse the formal argument list. */
14978 while (*p && *p != ')')
14979 {
14980 /* Find the extent of the current argument name. */
14981 const char *arg_start = p;
14982
14983 while (*p && *p != ',' && *p != ')' && *p != ' ')
14984 p++;
14985
14986 if (! *p || p == arg_start)
14987 dwarf2_macro_malformed_definition_complaint (body);
14988 else
14989 {
14990 /* Make sure argv has room for the new argument. */
14991 if (argc >= argv_size)
14992 {
14993 argv_size *= 2;
14994 argv = xrealloc (argv, argv_size * sizeof (*argv));
14995 }
14996
14997 argv[argc++] = copy_string (arg_start, p - arg_start);
14998 }
14999
15000 p = consume_improper_spaces (p, body);
15001
15002 /* Consume the comma, if present. */
15003 if (*p == ',')
15004 {
15005 p++;
15006
15007 p = consume_improper_spaces (p, body);
15008 }
15009 }
15010
15011 if (*p == ')')
15012 {
15013 p++;
15014
15015 if (*p == ' ')
15016 /* Perfectly formed definition, no complaints. */
15017 macro_define_function (file, line, name,
15018 argc, (const char **) argv,
15019 p + 1);
15020 else if (*p == '\0')
15021 {
15022 /* Complain, but do define it. */
15023 dwarf2_macro_malformed_definition_complaint (body);
15024 macro_define_function (file, line, name,
15025 argc, (const char **) argv,
15026 p);
15027 }
15028 else
15029 /* Just complain. */
15030 dwarf2_macro_malformed_definition_complaint (body);
15031 }
15032 else
15033 /* Just complain. */
15034 dwarf2_macro_malformed_definition_complaint (body);
15035
15036 xfree (name);
15037 {
15038 int i;
15039
15040 for (i = 0; i < argc; i++)
15041 xfree (argv[i]);
15042 }
15043 xfree (argv);
15044 }
15045 else
15046 dwarf2_macro_malformed_definition_complaint (body);
15047 }
15048
15049 /* Skip some bytes from BYTES according to the form given in FORM.
15050 Returns the new pointer. */
15051
15052 static gdb_byte *
15053 skip_form_bytes (bfd *abfd, gdb_byte *bytes,
15054 enum dwarf_form form,
15055 unsigned int offset_size,
15056 struct dwarf2_section_info *section)
15057 {
15058 unsigned int bytes_read;
15059
15060 switch (form)
15061 {
15062 case DW_FORM_data1:
15063 case DW_FORM_flag:
15064 ++bytes;
15065 break;
15066
15067 case DW_FORM_data2:
15068 bytes += 2;
15069 break;
15070
15071 case DW_FORM_data4:
15072 bytes += 4;
15073 break;
15074
15075 case DW_FORM_data8:
15076 bytes += 8;
15077 break;
15078
15079 case DW_FORM_string:
15080 read_direct_string (abfd, bytes, &bytes_read);
15081 bytes += bytes_read;
15082 break;
15083
15084 case DW_FORM_sec_offset:
15085 case DW_FORM_strp:
15086 bytes += offset_size;
15087 break;
15088
15089 case DW_FORM_block:
15090 bytes += read_unsigned_leb128 (abfd, bytes, &bytes_read);
15091 bytes += bytes_read;
15092 break;
15093
15094 case DW_FORM_block1:
15095 bytes += 1 + read_1_byte (abfd, bytes);
15096 break;
15097 case DW_FORM_block2:
15098 bytes += 2 + read_2_bytes (abfd, bytes);
15099 break;
15100 case DW_FORM_block4:
15101 bytes += 4 + read_4_bytes (abfd, bytes);
15102 break;
15103
15104 case DW_FORM_sdata:
15105 case DW_FORM_udata:
15106 bytes = skip_leb128 (abfd, bytes);
15107 break;
15108
15109 default:
15110 {
15111 complain:
15112 complaint (&symfile_complaints,
15113 _("invalid form 0x%x in `%s'"),
15114 form,
15115 section->asection->name);
15116 return NULL;
15117 }
15118 }
15119
15120 return bytes;
15121 }
15122
15123 /* A helper for dwarf_decode_macros that handles skipping an unknown
15124 opcode. Returns an updated pointer to the macro data buffer; or,
15125 on error, issues a complaint and returns NULL. */
15126
15127 static gdb_byte *
15128 skip_unknown_opcode (unsigned int opcode,
15129 gdb_byte **opcode_definitions,
15130 gdb_byte *mac_ptr,
15131 bfd *abfd,
15132 unsigned int offset_size,
15133 struct dwarf2_section_info *section)
15134 {
15135 unsigned int bytes_read, i;
15136 unsigned long arg;
15137 gdb_byte *defn;
15138
15139 if (opcode_definitions[opcode] == NULL)
15140 {
15141 complaint (&symfile_complaints,
15142 _("unrecognized DW_MACFINO opcode 0x%x"),
15143 opcode);
15144 return NULL;
15145 }
15146
15147 defn = opcode_definitions[opcode];
15148 arg = read_unsigned_leb128 (abfd, defn, &bytes_read);
15149 defn += bytes_read;
15150
15151 for (i = 0; i < arg; ++i)
15152 {
15153 mac_ptr = skip_form_bytes (abfd, mac_ptr, defn[i], offset_size, section);
15154 if (mac_ptr == NULL)
15155 {
15156 /* skip_form_bytes already issued the complaint. */
15157 return NULL;
15158 }
15159 }
15160
15161 return mac_ptr;
15162 }
15163
15164 /* A helper function which parses the header of a macro section.
15165 If the macro section is the extended (for now called "GNU") type,
15166 then this updates *OFFSET_SIZE. Returns a pointer to just after
15167 the header, or issues a complaint and returns NULL on error. */
15168
15169 static gdb_byte *
15170 dwarf_parse_macro_header (gdb_byte **opcode_definitions,
15171 bfd *abfd,
15172 gdb_byte *mac_ptr,
15173 unsigned int *offset_size,
15174 int section_is_gnu)
15175 {
15176 memset (opcode_definitions, 0, 256 * sizeof (gdb_byte *));
15177
15178 if (section_is_gnu)
15179 {
15180 unsigned int version, flags;
15181
15182 version = read_2_bytes (abfd, mac_ptr);
15183 if (version != 4)
15184 {
15185 complaint (&symfile_complaints,
15186 _("unrecognized version `%d' in .debug_macro section"),
15187 version);
15188 return NULL;
15189 }
15190 mac_ptr += 2;
15191
15192 flags = read_1_byte (abfd, mac_ptr);
15193 ++mac_ptr;
15194 *offset_size = (flags & 1) ? 8 : 4;
15195
15196 if ((flags & 2) != 0)
15197 /* We don't need the line table offset. */
15198 mac_ptr += *offset_size;
15199
15200 /* Vendor opcode descriptions. */
15201 if ((flags & 4) != 0)
15202 {
15203 unsigned int i, count;
15204
15205 count = read_1_byte (abfd, mac_ptr);
15206 ++mac_ptr;
15207 for (i = 0; i < count; ++i)
15208 {
15209 unsigned int opcode, bytes_read;
15210 unsigned long arg;
15211
15212 opcode = read_1_byte (abfd, mac_ptr);
15213 ++mac_ptr;
15214 opcode_definitions[opcode] = mac_ptr;
15215 arg = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15216 mac_ptr += bytes_read;
15217 mac_ptr += arg;
15218 }
15219 }
15220 }
15221
15222 return mac_ptr;
15223 }
15224
15225 /* A helper for dwarf_decode_macros that handles the GNU extensions,
15226 including DW_MACRO_GNU_transparent_include. */
15227
15228 static void
15229 dwarf_decode_macro_bytes (bfd *abfd, gdb_byte *mac_ptr, gdb_byte *mac_end,
15230 struct macro_source_file *current_file,
15231 struct line_header *lh, char *comp_dir,
15232 struct dwarf2_section_info *section,
15233 int section_is_gnu,
15234 unsigned int offset_size,
15235 struct objfile *objfile,
15236 htab_t include_hash)
15237 {
15238 enum dwarf_macro_record_type macinfo_type;
15239 int at_commandline;
15240 gdb_byte *opcode_definitions[256];
15241
15242 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15243 &offset_size, section_is_gnu);
15244 if (mac_ptr == NULL)
15245 {
15246 /* We already issued a complaint. */
15247 return;
15248 }
15249
15250 /* Determines if GDB is still before first DW_MACINFO_start_file. If true
15251 GDB is still reading the definitions from command line. First
15252 DW_MACINFO_start_file will need to be ignored as it was already executed
15253 to create CURRENT_FILE for the main source holding also the command line
15254 definitions. On first met DW_MACINFO_start_file this flag is reset to
15255 normally execute all the remaining DW_MACINFO_start_file macinfos. */
15256
15257 at_commandline = 1;
15258
15259 do
15260 {
15261 /* Do we at least have room for a macinfo type byte? */
15262 if (mac_ptr >= mac_end)
15263 {
15264 dwarf2_macros_too_long_complaint (section);
15265 break;
15266 }
15267
15268 macinfo_type = read_1_byte (abfd, mac_ptr);
15269 mac_ptr++;
15270
15271 /* Note that we rely on the fact that the corresponding GNU and
15272 DWARF constants are the same. */
15273 switch (macinfo_type)
15274 {
15275 /* A zero macinfo type indicates the end of the macro
15276 information. */
15277 case 0:
15278 break;
15279
15280 case DW_MACRO_GNU_define:
15281 case DW_MACRO_GNU_undef:
15282 case DW_MACRO_GNU_define_indirect:
15283 case DW_MACRO_GNU_undef_indirect:
15284 {
15285 unsigned int bytes_read;
15286 int line;
15287 char *body;
15288 int is_define;
15289
15290 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15291 mac_ptr += bytes_read;
15292
15293 if (macinfo_type == DW_MACRO_GNU_define
15294 || macinfo_type == DW_MACRO_GNU_undef)
15295 {
15296 body = read_direct_string (abfd, mac_ptr, &bytes_read);
15297 mac_ptr += bytes_read;
15298 }
15299 else
15300 {
15301 LONGEST str_offset;
15302
15303 str_offset = read_offset_1 (abfd, mac_ptr, offset_size);
15304 mac_ptr += offset_size;
15305
15306 body = read_indirect_string_at_offset (abfd, str_offset);
15307 }
15308
15309 is_define = (macinfo_type == DW_MACRO_GNU_define
15310 || macinfo_type == DW_MACRO_GNU_define_indirect);
15311 if (! current_file)
15312 {
15313 /* DWARF violation as no main source is present. */
15314 complaint (&symfile_complaints,
15315 _("debug info with no main source gives macro %s "
15316 "on line %d: %s"),
15317 is_define ? _("definition") : _("undefinition"),
15318 line, body);
15319 break;
15320 }
15321 if ((line == 0 && !at_commandline)
15322 || (line != 0 && at_commandline))
15323 complaint (&symfile_complaints,
15324 _("debug info gives %s macro %s with %s line %d: %s"),
15325 at_commandline ? _("command-line") : _("in-file"),
15326 is_define ? _("definition") : _("undefinition"),
15327 line == 0 ? _("zero") : _("non-zero"), line, body);
15328
15329 if (is_define)
15330 parse_macro_definition (current_file, line, body);
15331 else
15332 {
15333 gdb_assert (macinfo_type == DW_MACRO_GNU_undef
15334 || macinfo_type == DW_MACRO_GNU_undef_indirect);
15335 macro_undef (current_file, line, body);
15336 }
15337 }
15338 break;
15339
15340 case DW_MACRO_GNU_start_file:
15341 {
15342 unsigned int bytes_read;
15343 int line, file;
15344
15345 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15346 mac_ptr += bytes_read;
15347 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15348 mac_ptr += bytes_read;
15349
15350 if ((line == 0 && !at_commandline)
15351 || (line != 0 && at_commandline))
15352 complaint (&symfile_complaints,
15353 _("debug info gives source %d included "
15354 "from %s at %s line %d"),
15355 file, at_commandline ? _("command-line") : _("file"),
15356 line == 0 ? _("zero") : _("non-zero"), line);
15357
15358 if (at_commandline)
15359 {
15360 /* This DW_MACRO_GNU_start_file was executed in the
15361 pass one. */
15362 at_commandline = 0;
15363 }
15364 else
15365 current_file = macro_start_file (file, line,
15366 current_file, comp_dir,
15367 lh, objfile);
15368 }
15369 break;
15370
15371 case DW_MACRO_GNU_end_file:
15372 if (! current_file)
15373 complaint (&symfile_complaints,
15374 _("macro debug info has an unmatched "
15375 "`close_file' directive"));
15376 else
15377 {
15378 current_file = current_file->included_by;
15379 if (! current_file)
15380 {
15381 enum dwarf_macro_record_type next_type;
15382
15383 /* GCC circa March 2002 doesn't produce the zero
15384 type byte marking the end of the compilation
15385 unit. Complain if it's not there, but exit no
15386 matter what. */
15387
15388 /* Do we at least have room for a macinfo type byte? */
15389 if (mac_ptr >= mac_end)
15390 {
15391 dwarf2_macros_too_long_complaint (section);
15392 return;
15393 }
15394
15395 /* We don't increment mac_ptr here, so this is just
15396 a look-ahead. */
15397 next_type = read_1_byte (abfd, mac_ptr);
15398 if (next_type != 0)
15399 complaint (&symfile_complaints,
15400 _("no terminating 0-type entry for "
15401 "macros in `.debug_macinfo' section"));
15402
15403 return;
15404 }
15405 }
15406 break;
15407
15408 case DW_MACRO_GNU_transparent_include:
15409 {
15410 LONGEST offset;
15411 void **slot;
15412
15413 offset = read_offset_1 (abfd, mac_ptr, offset_size);
15414 mac_ptr += offset_size;
15415
15416 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15417 if (*slot != NULL)
15418 {
15419 /* This has actually happened; see
15420 http://sourceware.org/bugzilla/show_bug.cgi?id=13568. */
15421 complaint (&symfile_complaints,
15422 _("recursive DW_MACRO_GNU_transparent_include in "
15423 ".debug_macro section"));
15424 }
15425 else
15426 {
15427 *slot = mac_ptr;
15428
15429 dwarf_decode_macro_bytes (abfd,
15430 section->buffer + offset,
15431 mac_end, current_file,
15432 lh, comp_dir,
15433 section, section_is_gnu,
15434 offset_size, objfile, include_hash);
15435
15436 htab_remove_elt (include_hash, mac_ptr);
15437 }
15438 }
15439 break;
15440
15441 case DW_MACINFO_vendor_ext:
15442 if (!section_is_gnu)
15443 {
15444 unsigned int bytes_read;
15445 int constant;
15446
15447 constant = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15448 mac_ptr += bytes_read;
15449 read_direct_string (abfd, mac_ptr, &bytes_read);
15450 mac_ptr += bytes_read;
15451
15452 /* We don't recognize any vendor extensions. */
15453 break;
15454 }
15455 /* FALLTHROUGH */
15456
15457 default:
15458 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15459 mac_ptr, abfd, offset_size,
15460 section);
15461 if (mac_ptr == NULL)
15462 return;
15463 break;
15464 }
15465 } while (macinfo_type != 0);
15466 }
15467
15468 static void
15469 dwarf_decode_macros (struct line_header *lh, unsigned int offset,
15470 char *comp_dir, bfd *abfd,
15471 struct dwarf2_cu *cu,
15472 struct dwarf2_section_info *section,
15473 int section_is_gnu)
15474 {
15475 struct objfile *objfile = dwarf2_per_objfile->objfile;
15476 gdb_byte *mac_ptr, *mac_end;
15477 struct macro_source_file *current_file = 0;
15478 enum dwarf_macro_record_type macinfo_type;
15479 unsigned int offset_size = cu->header.offset_size;
15480 gdb_byte *opcode_definitions[256];
15481 struct cleanup *cleanup;
15482 htab_t include_hash;
15483 void **slot;
15484
15485 dwarf2_read_section (objfile, section);
15486 if (section->buffer == NULL)
15487 {
15488 complaint (&symfile_complaints, _("missing %s section"),
15489 section->asection->name);
15490 return;
15491 }
15492
15493 /* First pass: Find the name of the base filename.
15494 This filename is needed in order to process all macros whose definition
15495 (or undefinition) comes from the command line. These macros are defined
15496 before the first DW_MACINFO_start_file entry, and yet still need to be
15497 associated to the base file.
15498
15499 To determine the base file name, we scan the macro definitions until we
15500 reach the first DW_MACINFO_start_file entry. We then initialize
15501 CURRENT_FILE accordingly so that any macro definition found before the
15502 first DW_MACINFO_start_file can still be associated to the base file. */
15503
15504 mac_ptr = section->buffer + offset;
15505 mac_end = section->buffer + section->size;
15506
15507 mac_ptr = dwarf_parse_macro_header (opcode_definitions, abfd, mac_ptr,
15508 &offset_size, section_is_gnu);
15509 if (mac_ptr == NULL)
15510 {
15511 /* We already issued a complaint. */
15512 return;
15513 }
15514
15515 do
15516 {
15517 /* Do we at least have room for a macinfo type byte? */
15518 if (mac_ptr >= mac_end)
15519 {
15520 /* Complaint is printed during the second pass as GDB will probably
15521 stop the first pass earlier upon finding
15522 DW_MACINFO_start_file. */
15523 break;
15524 }
15525
15526 macinfo_type = read_1_byte (abfd, mac_ptr);
15527 mac_ptr++;
15528
15529 /* Note that we rely on the fact that the corresponding GNU and
15530 DWARF constants are the same. */
15531 switch (macinfo_type)
15532 {
15533 /* A zero macinfo type indicates the end of the macro
15534 information. */
15535 case 0:
15536 break;
15537
15538 case DW_MACRO_GNU_define:
15539 case DW_MACRO_GNU_undef:
15540 /* Only skip the data by MAC_PTR. */
15541 {
15542 unsigned int bytes_read;
15543
15544 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15545 mac_ptr += bytes_read;
15546 read_direct_string (abfd, mac_ptr, &bytes_read);
15547 mac_ptr += bytes_read;
15548 }
15549 break;
15550
15551 case DW_MACRO_GNU_start_file:
15552 {
15553 unsigned int bytes_read;
15554 int line, file;
15555
15556 line = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15557 mac_ptr += bytes_read;
15558 file = read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15559 mac_ptr += bytes_read;
15560
15561 current_file = macro_start_file (file, line, current_file,
15562 comp_dir, lh, objfile);
15563 }
15564 break;
15565
15566 case DW_MACRO_GNU_end_file:
15567 /* No data to skip by MAC_PTR. */
15568 break;
15569
15570 case DW_MACRO_GNU_define_indirect:
15571 case DW_MACRO_GNU_undef_indirect:
15572 {
15573 unsigned int bytes_read;
15574
15575 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15576 mac_ptr += bytes_read;
15577 mac_ptr += offset_size;
15578 }
15579 break;
15580
15581 case DW_MACRO_GNU_transparent_include:
15582 /* Note that, according to the spec, a transparent include
15583 chain cannot call DW_MACRO_GNU_start_file. So, we can just
15584 skip this opcode. */
15585 mac_ptr += offset_size;
15586 break;
15587
15588 case DW_MACINFO_vendor_ext:
15589 /* Only skip the data by MAC_PTR. */
15590 if (!section_is_gnu)
15591 {
15592 unsigned int bytes_read;
15593
15594 read_unsigned_leb128 (abfd, mac_ptr, &bytes_read);
15595 mac_ptr += bytes_read;
15596 read_direct_string (abfd, mac_ptr, &bytes_read);
15597 mac_ptr += bytes_read;
15598 }
15599 /* FALLTHROUGH */
15600
15601 default:
15602 mac_ptr = skip_unknown_opcode (macinfo_type, opcode_definitions,
15603 mac_ptr, abfd, offset_size,
15604 section);
15605 if (mac_ptr == NULL)
15606 return;
15607 break;
15608 }
15609 } while (macinfo_type != 0 && current_file == NULL);
15610
15611 /* Second pass: Process all entries.
15612
15613 Use the AT_COMMAND_LINE flag to determine whether we are still processing
15614 command-line macro definitions/undefinitions. This flag is unset when we
15615 reach the first DW_MACINFO_start_file entry. */
15616
15617 include_hash = htab_create_alloc (1, htab_hash_pointer, htab_eq_pointer,
15618 NULL, xcalloc, xfree);
15619 cleanup = make_cleanup_htab_delete (include_hash);
15620 mac_ptr = section->buffer + offset;
15621 slot = htab_find_slot (include_hash, mac_ptr, INSERT);
15622 *slot = mac_ptr;
15623 dwarf_decode_macro_bytes (abfd, mac_ptr, mac_end,
15624 current_file, lh, comp_dir, section, section_is_gnu,
15625 offset_size, objfile, include_hash);
15626 do_cleanups (cleanup);
15627 }
15628
15629 /* Check if the attribute's form is a DW_FORM_block*
15630 if so return true else false. */
15631 static int
15632 attr_form_is_block (struct attribute *attr)
15633 {
15634 return (attr == NULL ? 0 :
15635 attr->form == DW_FORM_block1
15636 || attr->form == DW_FORM_block2
15637 || attr->form == DW_FORM_block4
15638 || attr->form == DW_FORM_block
15639 || attr->form == DW_FORM_exprloc);
15640 }
15641
15642 /* Return non-zero if ATTR's value is a section offset --- classes
15643 lineptr, loclistptr, macptr or rangelistptr --- or zero, otherwise.
15644 You may use DW_UNSND (attr) to retrieve such offsets.
15645
15646 Section 7.5.4, "Attribute Encodings", explains that no attribute
15647 may have a value that belongs to more than one of these classes; it
15648 would be ambiguous if we did, because we use the same forms for all
15649 of them. */
15650 static int
15651 attr_form_is_section_offset (struct attribute *attr)
15652 {
15653 return (attr->form == DW_FORM_data4
15654 || attr->form == DW_FORM_data8
15655 || attr->form == DW_FORM_sec_offset);
15656 }
15657
15658
15659 /* Return non-zero if ATTR's value falls in the 'constant' class, or
15660 zero otherwise. When this function returns true, you can apply
15661 dwarf2_get_attr_constant_value to it.
15662
15663 However, note that for some attributes you must check
15664 attr_form_is_section_offset before using this test. DW_FORM_data4
15665 and DW_FORM_data8 are members of both the constant class, and of
15666 the classes that contain offsets into other debug sections
15667 (lineptr, loclistptr, macptr or rangelistptr). The DWARF spec says
15668 that, if an attribute's can be either a constant or one of the
15669 section offset classes, DW_FORM_data4 and DW_FORM_data8 should be
15670 taken as section offsets, not constants. */
15671 static int
15672 attr_form_is_constant (struct attribute *attr)
15673 {
15674 switch (attr->form)
15675 {
15676 case DW_FORM_sdata:
15677 case DW_FORM_udata:
15678 case DW_FORM_data1:
15679 case DW_FORM_data2:
15680 case DW_FORM_data4:
15681 case DW_FORM_data8:
15682 return 1;
15683 default:
15684 return 0;
15685 }
15686 }
15687
15688 /* A helper function that fills in a dwarf2_loclist_baton. */
15689
15690 static void
15691 fill_in_loclist_baton (struct dwarf2_cu *cu,
15692 struct dwarf2_loclist_baton *baton,
15693 struct attribute *attr)
15694 {
15695 dwarf2_read_section (dwarf2_per_objfile->objfile,
15696 &dwarf2_per_objfile->loc);
15697
15698 baton->per_cu = cu->per_cu;
15699 gdb_assert (baton->per_cu);
15700 /* We don't know how long the location list is, but make sure we
15701 don't run off the edge of the section. */
15702 baton->size = dwarf2_per_objfile->loc.size - DW_UNSND (attr);
15703 baton->data = dwarf2_per_objfile->loc.buffer + DW_UNSND (attr);
15704 baton->base_address = cu->base_address;
15705 }
15706
15707 static void
15708 dwarf2_symbol_mark_computed (struct attribute *attr, struct symbol *sym,
15709 struct dwarf2_cu *cu)
15710 {
15711 struct objfile *objfile = dwarf2_per_objfile->objfile;
15712
15713 if (attr_form_is_section_offset (attr)
15714 /* ".debug_loc" may not exist at all, or the offset may be outside
15715 the section. If so, fall through to the complaint in the
15716 other branch. */
15717 && DW_UNSND (attr) < dwarf2_section_size (objfile,
15718 &dwarf2_per_objfile->loc))
15719 {
15720 struct dwarf2_loclist_baton *baton;
15721
15722 baton = obstack_alloc (&objfile->objfile_obstack,
15723 sizeof (struct dwarf2_loclist_baton));
15724
15725 fill_in_loclist_baton (cu, baton, attr);
15726
15727 if (cu->base_known == 0)
15728 complaint (&symfile_complaints,
15729 _("Location list used without "
15730 "specifying the CU base address."));
15731
15732 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_loclist_funcs;
15733 SYMBOL_LOCATION_BATON (sym) = baton;
15734 }
15735 else
15736 {
15737 struct dwarf2_locexpr_baton *baton;
15738
15739 baton = obstack_alloc (&objfile->objfile_obstack,
15740 sizeof (struct dwarf2_locexpr_baton));
15741 baton->per_cu = cu->per_cu;
15742 gdb_assert (baton->per_cu);
15743
15744 if (attr_form_is_block (attr))
15745 {
15746 /* Note that we're just copying the block's data pointer
15747 here, not the actual data. We're still pointing into the
15748 info_buffer for SYM's objfile; right now we never release
15749 that buffer, but when we do clean up properly this may
15750 need to change. */
15751 baton->size = DW_BLOCK (attr)->size;
15752 baton->data = DW_BLOCK (attr)->data;
15753 }
15754 else
15755 {
15756 dwarf2_invalid_attrib_class_complaint ("location description",
15757 SYMBOL_NATURAL_NAME (sym));
15758 baton->size = 0;
15759 }
15760
15761 SYMBOL_COMPUTED_OPS (sym) = &dwarf2_locexpr_funcs;
15762 SYMBOL_LOCATION_BATON (sym) = baton;
15763 }
15764 }
15765
15766 /* Return the OBJFILE associated with the compilation unit CU. If CU
15767 came from a separate debuginfo file, then the master objfile is
15768 returned. */
15769
15770 struct objfile *
15771 dwarf2_per_cu_objfile (struct dwarf2_per_cu_data *per_cu)
15772 {
15773 struct objfile *objfile = per_cu->objfile;
15774
15775 /* Return the master objfile, so that we can report and look up the
15776 correct file containing this variable. */
15777 if (objfile->separate_debug_objfile_backlink)
15778 objfile = objfile->separate_debug_objfile_backlink;
15779
15780 return objfile;
15781 }
15782
15783 /* Return comp_unit_head for PER_CU, either already available in PER_CU->CU
15784 (CU_HEADERP is unused in such case) or prepare a temporary copy at
15785 CU_HEADERP first. */
15786
15787 static const struct comp_unit_head *
15788 per_cu_header_read_in (struct comp_unit_head *cu_headerp,
15789 struct dwarf2_per_cu_data *per_cu)
15790 {
15791 struct objfile *objfile;
15792 struct dwarf2_per_objfile *per_objfile;
15793 gdb_byte *info_ptr;
15794
15795 if (per_cu->cu)
15796 return &per_cu->cu->header;
15797
15798 objfile = per_cu->objfile;
15799 per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
15800 info_ptr = per_objfile->info.buffer + per_cu->offset;
15801
15802 memset (cu_headerp, 0, sizeof (*cu_headerp));
15803 read_comp_unit_head (cu_headerp, info_ptr, objfile->obfd);
15804
15805 return cu_headerp;
15806 }
15807
15808 /* Return the address size given in the compilation unit header for CU. */
15809
15810 int
15811 dwarf2_per_cu_addr_size (struct dwarf2_per_cu_data *per_cu)
15812 {
15813 struct comp_unit_head cu_header_local;
15814 const struct comp_unit_head *cu_headerp;
15815
15816 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15817
15818 return cu_headerp->addr_size;
15819 }
15820
15821 /* Return the offset size given in the compilation unit header for CU. */
15822
15823 int
15824 dwarf2_per_cu_offset_size (struct dwarf2_per_cu_data *per_cu)
15825 {
15826 struct comp_unit_head cu_header_local;
15827 const struct comp_unit_head *cu_headerp;
15828
15829 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15830
15831 return cu_headerp->offset_size;
15832 }
15833
15834 /* See its dwarf2loc.h declaration. */
15835
15836 int
15837 dwarf2_per_cu_ref_addr_size (struct dwarf2_per_cu_data *per_cu)
15838 {
15839 struct comp_unit_head cu_header_local;
15840 const struct comp_unit_head *cu_headerp;
15841
15842 cu_headerp = per_cu_header_read_in (&cu_header_local, per_cu);
15843
15844 if (cu_headerp->version == 2)
15845 return cu_headerp->addr_size;
15846 else
15847 return cu_headerp->offset_size;
15848 }
15849
15850 /* Return the text offset of the CU. The returned offset comes from
15851 this CU's objfile. If this objfile came from a separate debuginfo
15852 file, then the offset may be different from the corresponding
15853 offset in the parent objfile. */
15854
15855 CORE_ADDR
15856 dwarf2_per_cu_text_offset (struct dwarf2_per_cu_data *per_cu)
15857 {
15858 struct objfile *objfile = per_cu->objfile;
15859
15860 return ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
15861 }
15862
15863 /* Locate the .debug_info compilation unit from CU's objfile which contains
15864 the DIE at OFFSET. Raises an error on failure. */
15865
15866 static struct dwarf2_per_cu_data *
15867 dwarf2_find_containing_comp_unit (unsigned int offset,
15868 struct objfile *objfile)
15869 {
15870 struct dwarf2_per_cu_data *this_cu;
15871 int low, high;
15872
15873 low = 0;
15874 high = dwarf2_per_objfile->n_comp_units - 1;
15875 while (high > low)
15876 {
15877 int mid = low + (high - low) / 2;
15878
15879 if (dwarf2_per_objfile->all_comp_units[mid]->offset >= offset)
15880 high = mid;
15881 else
15882 low = mid + 1;
15883 }
15884 gdb_assert (low == high);
15885 if (dwarf2_per_objfile->all_comp_units[low]->offset > offset)
15886 {
15887 if (low == 0)
15888 error (_("Dwarf Error: could not find partial DIE containing "
15889 "offset 0x%lx [in module %s]"),
15890 (long) offset, bfd_get_filename (objfile->obfd));
15891
15892 gdb_assert (dwarf2_per_objfile->all_comp_units[low-1]->offset <= offset);
15893 return dwarf2_per_objfile->all_comp_units[low-1];
15894 }
15895 else
15896 {
15897 this_cu = dwarf2_per_objfile->all_comp_units[low];
15898 if (low == dwarf2_per_objfile->n_comp_units - 1
15899 && offset >= this_cu->offset + this_cu->length)
15900 error (_("invalid dwarf2 offset %u"), offset);
15901 gdb_assert (offset < this_cu->offset + this_cu->length);
15902 return this_cu;
15903 }
15904 }
15905
15906 /* Initialize dwarf2_cu CU, owned by PER_CU. */
15907
15908 static void
15909 init_one_comp_unit (struct dwarf2_cu *cu, struct dwarf2_per_cu_data *per_cu)
15910 {
15911 memset (cu, 0, sizeof (*cu));
15912 per_cu->cu = cu;
15913 cu->per_cu = per_cu;
15914 cu->objfile = per_cu->objfile;
15915 obstack_init (&cu->comp_unit_obstack);
15916 }
15917
15918 /* Initialize basic fields of dwarf_cu CU according to DIE COMP_UNIT_DIE. */
15919
15920 static void
15921 prepare_one_comp_unit (struct dwarf2_cu *cu, struct die_info *comp_unit_die)
15922 {
15923 struct attribute *attr;
15924
15925 /* Set the language we're debugging. */
15926 attr = dwarf2_attr (comp_unit_die, DW_AT_language, cu);
15927 if (attr)
15928 set_cu_language (DW_UNSND (attr), cu);
15929 else
15930 {
15931 cu->language = language_minimal;
15932 cu->language_defn = language_def (cu->language);
15933 }
15934 }
15935
15936 /* Release one cached compilation unit, CU. We unlink it from the tree
15937 of compilation units, but we don't remove it from the read_in_chain;
15938 the caller is responsible for that.
15939 NOTE: DATA is a void * because this function is also used as a
15940 cleanup routine. */
15941
15942 static void
15943 free_heap_comp_unit (void *data)
15944 {
15945 struct dwarf2_cu *cu = data;
15946
15947 gdb_assert (cu->per_cu != NULL);
15948 cu->per_cu->cu = NULL;
15949 cu->per_cu = NULL;
15950
15951 obstack_free (&cu->comp_unit_obstack, NULL);
15952
15953 xfree (cu);
15954 }
15955
15956 /* This cleanup function is passed the address of a dwarf2_cu on the stack
15957 when we're finished with it. We can't free the pointer itself, but be
15958 sure to unlink it from the cache. Also release any associated storage
15959 and perform cache maintenance.
15960
15961 Only used during partial symbol parsing. */
15962
15963 static void
15964 free_stack_comp_unit (void *data)
15965 {
15966 struct dwarf2_cu *cu = data;
15967
15968 gdb_assert (cu->per_cu != NULL);
15969 cu->per_cu->cu = NULL;
15970 cu->per_cu = NULL;
15971
15972 obstack_free (&cu->comp_unit_obstack, NULL);
15973 cu->partial_dies = NULL;
15974
15975 /* The previous code only did this if per_cu != NULL.
15976 But that would always succeed, so now we just unconditionally do
15977 the aging. This seems like the wrong place to do such aging,
15978 but cleaning that up is left for later. */
15979 age_cached_comp_units ();
15980 }
15981
15982 /* Free all cached compilation units. */
15983
15984 static void
15985 free_cached_comp_units (void *data)
15986 {
15987 struct dwarf2_per_cu_data *per_cu, **last_chain;
15988
15989 per_cu = dwarf2_per_objfile->read_in_chain;
15990 last_chain = &dwarf2_per_objfile->read_in_chain;
15991 while (per_cu != NULL)
15992 {
15993 struct dwarf2_per_cu_data *next_cu;
15994
15995 next_cu = per_cu->cu->read_in_chain;
15996
15997 free_heap_comp_unit (per_cu->cu);
15998 *last_chain = next_cu;
15999
16000 per_cu = next_cu;
16001 }
16002 }
16003
16004 /* Increase the age counter on each cached compilation unit, and free
16005 any that are too old. */
16006
16007 static void
16008 age_cached_comp_units (void)
16009 {
16010 struct dwarf2_per_cu_data *per_cu, **last_chain;
16011
16012 dwarf2_clear_marks (dwarf2_per_objfile->read_in_chain);
16013 per_cu = dwarf2_per_objfile->read_in_chain;
16014 while (per_cu != NULL)
16015 {
16016 per_cu->cu->last_used ++;
16017 if (per_cu->cu->last_used <= dwarf2_max_cache_age)
16018 dwarf2_mark (per_cu->cu);
16019 per_cu = per_cu->cu->read_in_chain;
16020 }
16021
16022 per_cu = dwarf2_per_objfile->read_in_chain;
16023 last_chain = &dwarf2_per_objfile->read_in_chain;
16024 while (per_cu != NULL)
16025 {
16026 struct dwarf2_per_cu_data *next_cu;
16027
16028 next_cu = per_cu->cu->read_in_chain;
16029
16030 if (!per_cu->cu->mark)
16031 {
16032 free_heap_comp_unit (per_cu->cu);
16033 *last_chain = next_cu;
16034 }
16035 else
16036 last_chain = &per_cu->cu->read_in_chain;
16037
16038 per_cu = next_cu;
16039 }
16040 }
16041
16042 /* Remove a single compilation unit from the cache. */
16043
16044 static void
16045 free_one_cached_comp_unit (void *target_cu)
16046 {
16047 struct dwarf2_per_cu_data *per_cu, **last_chain;
16048
16049 per_cu = dwarf2_per_objfile->read_in_chain;
16050 last_chain = &dwarf2_per_objfile->read_in_chain;
16051 while (per_cu != NULL)
16052 {
16053 struct dwarf2_per_cu_data *next_cu;
16054
16055 next_cu = per_cu->cu->read_in_chain;
16056
16057 if (per_cu->cu == target_cu)
16058 {
16059 free_heap_comp_unit (per_cu->cu);
16060 *last_chain = next_cu;
16061 break;
16062 }
16063 else
16064 last_chain = &per_cu->cu->read_in_chain;
16065
16066 per_cu = next_cu;
16067 }
16068 }
16069
16070 /* Release all extra memory associated with OBJFILE. */
16071
16072 void
16073 dwarf2_free_objfile (struct objfile *objfile)
16074 {
16075 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
16076
16077 if (dwarf2_per_objfile == NULL)
16078 return;
16079
16080 /* Cached DIE trees use xmalloc and the comp_unit_obstack. */
16081 free_cached_comp_units (NULL);
16082
16083 if (dwarf2_per_objfile->quick_file_names_table)
16084 htab_delete (dwarf2_per_objfile->quick_file_names_table);
16085
16086 /* Everything else should be on the objfile obstack. */
16087 }
16088
16089 /* A pair of DIE offset and GDB type pointer. We store these
16090 in a hash table separate from the DIEs, and preserve them
16091 when the DIEs are flushed out of cache. */
16092
16093 struct dwarf2_offset_and_type
16094 {
16095 unsigned int offset;
16096 struct type *type;
16097 };
16098
16099 /* Hash function for a dwarf2_offset_and_type. */
16100
16101 static hashval_t
16102 offset_and_type_hash (const void *item)
16103 {
16104 const struct dwarf2_offset_and_type *ofs = item;
16105
16106 return ofs->offset;
16107 }
16108
16109 /* Equality function for a dwarf2_offset_and_type. */
16110
16111 static int
16112 offset_and_type_eq (const void *item_lhs, const void *item_rhs)
16113 {
16114 const struct dwarf2_offset_and_type *ofs_lhs = item_lhs;
16115 const struct dwarf2_offset_and_type *ofs_rhs = item_rhs;
16116
16117 return ofs_lhs->offset == ofs_rhs->offset;
16118 }
16119
16120 /* Set the type associated with DIE to TYPE. Save it in CU's hash
16121 table if necessary. For convenience, return TYPE.
16122
16123 The DIEs reading must have careful ordering to:
16124 * Not cause infite loops trying to read in DIEs as a prerequisite for
16125 reading current DIE.
16126 * Not trying to dereference contents of still incompletely read in types
16127 while reading in other DIEs.
16128 * Enable referencing still incompletely read in types just by a pointer to
16129 the type without accessing its fields.
16130
16131 Therefore caller should follow these rules:
16132 * Try to fetch any prerequisite types we may need to build this DIE type
16133 before building the type and calling set_die_type.
16134 * After building type call set_die_type for current DIE as soon as
16135 possible before fetching more types to complete the current type.
16136 * Make the type as complete as possible before fetching more types. */
16137
16138 static struct type *
16139 set_die_type (struct die_info *die, struct type *type, struct dwarf2_cu *cu)
16140 {
16141 struct dwarf2_offset_and_type **slot, ofs;
16142 struct objfile *objfile = cu->objfile;
16143 htab_t *type_hash_ptr;
16144
16145 /* For Ada types, make sure that the gnat-specific data is always
16146 initialized (if not already set). There are a few types where
16147 we should not be doing so, because the type-specific area is
16148 already used to hold some other piece of info (eg: TYPE_CODE_FLT
16149 where the type-specific area is used to store the floatformat).
16150 But this is not a problem, because the gnat-specific information
16151 is actually not needed for these types. */
16152 if (need_gnat_info (cu)
16153 && TYPE_CODE (type) != TYPE_CODE_FUNC
16154 && TYPE_CODE (type) != TYPE_CODE_FLT
16155 && !HAVE_GNAT_AUX_INFO (type))
16156 INIT_GNAT_SPECIFIC (type);
16157
16158 if (cu->per_cu->debug_types_section)
16159 type_hash_ptr = &dwarf2_per_objfile->debug_types_type_hash;
16160 else
16161 type_hash_ptr = &dwarf2_per_objfile->debug_info_type_hash;
16162
16163 if (*type_hash_ptr == NULL)
16164 {
16165 *type_hash_ptr
16166 = htab_create_alloc_ex (127,
16167 offset_and_type_hash,
16168 offset_and_type_eq,
16169 NULL,
16170 &objfile->objfile_obstack,
16171 hashtab_obstack_allocate,
16172 dummy_obstack_deallocate);
16173 }
16174
16175 ofs.offset = die->offset;
16176 ofs.type = type;
16177 slot = (struct dwarf2_offset_and_type **)
16178 htab_find_slot_with_hash (*type_hash_ptr, &ofs, ofs.offset, INSERT);
16179 if (*slot)
16180 complaint (&symfile_complaints,
16181 _("A problem internal to GDB: DIE 0x%x has type already set"),
16182 die->offset);
16183 *slot = obstack_alloc (&objfile->objfile_obstack, sizeof (**slot));
16184 **slot = ofs;
16185 return type;
16186 }
16187
16188 /* Look up the type for the die at DIE_OFFSET in the appropriate type_hash
16189 table, or return NULL if the die does not have a saved type. */
16190
16191 static struct type *
16192 get_die_type_at_offset (unsigned int offset,
16193 struct dwarf2_per_cu_data *per_cu)
16194 {
16195 struct dwarf2_offset_and_type *slot, ofs;
16196 htab_t type_hash;
16197
16198 if (per_cu->debug_types_section)
16199 type_hash = dwarf2_per_objfile->debug_types_type_hash;
16200 else
16201 type_hash = dwarf2_per_objfile->debug_info_type_hash;
16202 if (type_hash == NULL)
16203 return NULL;
16204
16205 ofs.offset = offset;
16206 slot = htab_find_with_hash (type_hash, &ofs, ofs.offset);
16207 if (slot)
16208 return slot->type;
16209 else
16210 return NULL;
16211 }
16212
16213 /* Look up the type for DIE in the appropriate type_hash table,
16214 or return NULL if DIE does not have a saved type. */
16215
16216 static struct type *
16217 get_die_type (struct die_info *die, struct dwarf2_cu *cu)
16218 {
16219 return get_die_type_at_offset (die->offset, cu->per_cu);
16220 }
16221
16222 /* Add a dependence relationship from CU to REF_PER_CU. */
16223
16224 static void
16225 dwarf2_add_dependence (struct dwarf2_cu *cu,
16226 struct dwarf2_per_cu_data *ref_per_cu)
16227 {
16228 void **slot;
16229
16230 if (cu->dependencies == NULL)
16231 cu->dependencies
16232 = htab_create_alloc_ex (5, htab_hash_pointer, htab_eq_pointer,
16233 NULL, &cu->comp_unit_obstack,
16234 hashtab_obstack_allocate,
16235 dummy_obstack_deallocate);
16236
16237 slot = htab_find_slot (cu->dependencies, ref_per_cu, INSERT);
16238 if (*slot == NULL)
16239 *slot = ref_per_cu;
16240 }
16241
16242 /* Subroutine of dwarf2_mark to pass to htab_traverse.
16243 Set the mark field in every compilation unit in the
16244 cache that we must keep because we are keeping CU. */
16245
16246 static int
16247 dwarf2_mark_helper (void **slot, void *data)
16248 {
16249 struct dwarf2_per_cu_data *per_cu;
16250
16251 per_cu = (struct dwarf2_per_cu_data *) *slot;
16252
16253 /* cu->dependencies references may not yet have been ever read if QUIT aborts
16254 reading of the chain. As such dependencies remain valid it is not much
16255 useful to track and undo them during QUIT cleanups. */
16256 if (per_cu->cu == NULL)
16257 return 1;
16258
16259 if (per_cu->cu->mark)
16260 return 1;
16261 per_cu->cu->mark = 1;
16262
16263 if (per_cu->cu->dependencies != NULL)
16264 htab_traverse (per_cu->cu->dependencies, dwarf2_mark_helper, NULL);
16265
16266 return 1;
16267 }
16268
16269 /* Set the mark field in CU and in every other compilation unit in the
16270 cache that we must keep because we are keeping CU. */
16271
16272 static void
16273 dwarf2_mark (struct dwarf2_cu *cu)
16274 {
16275 if (cu->mark)
16276 return;
16277 cu->mark = 1;
16278 if (cu->dependencies != NULL)
16279 htab_traverse (cu->dependencies, dwarf2_mark_helper, NULL);
16280 }
16281
16282 static void
16283 dwarf2_clear_marks (struct dwarf2_per_cu_data *per_cu)
16284 {
16285 while (per_cu)
16286 {
16287 per_cu->cu->mark = 0;
16288 per_cu = per_cu->cu->read_in_chain;
16289 }
16290 }
16291
16292 /* Trivial hash function for partial_die_info: the hash value of a DIE
16293 is its offset in .debug_info for this objfile. */
16294
16295 static hashval_t
16296 partial_die_hash (const void *item)
16297 {
16298 const struct partial_die_info *part_die = item;
16299
16300 return part_die->offset;
16301 }
16302
16303 /* Trivial comparison function for partial_die_info structures: two DIEs
16304 are equal if they have the same offset. */
16305
16306 static int
16307 partial_die_eq (const void *item_lhs, const void *item_rhs)
16308 {
16309 const struct partial_die_info *part_die_lhs = item_lhs;
16310 const struct partial_die_info *part_die_rhs = item_rhs;
16311
16312 return part_die_lhs->offset == part_die_rhs->offset;
16313 }
16314
16315 static struct cmd_list_element *set_dwarf2_cmdlist;
16316 static struct cmd_list_element *show_dwarf2_cmdlist;
16317
16318 static void
16319 set_dwarf2_cmd (char *args, int from_tty)
16320 {
16321 help_list (set_dwarf2_cmdlist, "maintenance set dwarf2 ", -1, gdb_stdout);
16322 }
16323
16324 static void
16325 show_dwarf2_cmd (char *args, int from_tty)
16326 {
16327 cmd_show_list (show_dwarf2_cmdlist, from_tty, "");
16328 }
16329
16330 /* If section described by INFO was mmapped, munmap it now. */
16331
16332 static void
16333 munmap_section_buffer (struct dwarf2_section_info *info)
16334 {
16335 if (info->map_addr != NULL)
16336 {
16337 #ifdef HAVE_MMAP
16338 int res;
16339
16340 res = munmap (info->map_addr, info->map_len);
16341 gdb_assert (res == 0);
16342 #else
16343 /* Without HAVE_MMAP, we should never be here to begin with. */
16344 gdb_assert_not_reached ("no mmap support");
16345 #endif
16346 }
16347 }
16348
16349 /* munmap debug sections for OBJFILE, if necessary. */
16350
16351 static void
16352 dwarf2_per_objfile_free (struct objfile *objfile, void *d)
16353 {
16354 struct dwarf2_per_objfile *data = d;
16355 int ix;
16356 struct dwarf2_section_info *section;
16357
16358 /* This is sorted according to the order they're defined in to make it easier
16359 to keep in sync. */
16360 munmap_section_buffer (&data->info);
16361 munmap_section_buffer (&data->abbrev);
16362 munmap_section_buffer (&data->line);
16363 munmap_section_buffer (&data->loc);
16364 munmap_section_buffer (&data->macinfo);
16365 munmap_section_buffer (&data->macro);
16366 munmap_section_buffer (&data->str);
16367 munmap_section_buffer (&data->ranges);
16368 munmap_section_buffer (&data->frame);
16369 munmap_section_buffer (&data->eh_frame);
16370 munmap_section_buffer (&data->gdb_index);
16371
16372 for (ix = 0;
16373 VEC_iterate (dwarf2_section_info_def, data->types, ix, section);
16374 ++ix)
16375 munmap_section_buffer (section);
16376
16377 VEC_free (dwarf2_section_info_def, data->types);
16378 }
16379
16380 \f
16381 /* The "save gdb-index" command. */
16382
16383 /* The contents of the hash table we create when building the string
16384 table. */
16385 struct strtab_entry
16386 {
16387 offset_type offset;
16388 const char *str;
16389 };
16390
16391 /* Hash function for a strtab_entry.
16392
16393 Function is used only during write_hash_table so no index format backward
16394 compatibility is needed. */
16395
16396 static hashval_t
16397 hash_strtab_entry (const void *e)
16398 {
16399 const struct strtab_entry *entry = e;
16400 return mapped_index_string_hash (INT_MAX, entry->str);
16401 }
16402
16403 /* Equality function for a strtab_entry. */
16404
16405 static int
16406 eq_strtab_entry (const void *a, const void *b)
16407 {
16408 const struct strtab_entry *ea = a;
16409 const struct strtab_entry *eb = b;
16410 return !strcmp (ea->str, eb->str);
16411 }
16412
16413 /* Create a strtab_entry hash table. */
16414
16415 static htab_t
16416 create_strtab (void)
16417 {
16418 return htab_create_alloc (100, hash_strtab_entry, eq_strtab_entry,
16419 xfree, xcalloc, xfree);
16420 }
16421
16422 /* Add a string to the constant pool. Return the string's offset in
16423 host order. */
16424
16425 static offset_type
16426 add_string (htab_t table, struct obstack *cpool, const char *str)
16427 {
16428 void **slot;
16429 struct strtab_entry entry;
16430 struct strtab_entry *result;
16431
16432 entry.str = str;
16433 slot = htab_find_slot (table, &entry, INSERT);
16434 if (*slot)
16435 result = *slot;
16436 else
16437 {
16438 result = XNEW (struct strtab_entry);
16439 result->offset = obstack_object_size (cpool);
16440 result->str = str;
16441 obstack_grow_str0 (cpool, str);
16442 *slot = result;
16443 }
16444 return result->offset;
16445 }
16446
16447 /* An entry in the symbol table. */
16448 struct symtab_index_entry
16449 {
16450 /* The name of the symbol. */
16451 const char *name;
16452 /* The offset of the name in the constant pool. */
16453 offset_type index_offset;
16454 /* A sorted vector of the indices of all the CUs that hold an object
16455 of this name. */
16456 VEC (offset_type) *cu_indices;
16457 };
16458
16459 /* The symbol table. This is a power-of-2-sized hash table. */
16460 struct mapped_symtab
16461 {
16462 offset_type n_elements;
16463 offset_type size;
16464 struct symtab_index_entry **data;
16465 };
16466
16467 /* Hash function for a symtab_index_entry. */
16468
16469 static hashval_t
16470 hash_symtab_entry (const void *e)
16471 {
16472 const struct symtab_index_entry *entry = e;
16473 return iterative_hash (VEC_address (offset_type, entry->cu_indices),
16474 sizeof (offset_type) * VEC_length (offset_type,
16475 entry->cu_indices),
16476 0);
16477 }
16478
16479 /* Equality function for a symtab_index_entry. */
16480
16481 static int
16482 eq_symtab_entry (const void *a, const void *b)
16483 {
16484 const struct symtab_index_entry *ea = a;
16485 const struct symtab_index_entry *eb = b;
16486 int len = VEC_length (offset_type, ea->cu_indices);
16487 if (len != VEC_length (offset_type, eb->cu_indices))
16488 return 0;
16489 return !memcmp (VEC_address (offset_type, ea->cu_indices),
16490 VEC_address (offset_type, eb->cu_indices),
16491 sizeof (offset_type) * len);
16492 }
16493
16494 /* Destroy a symtab_index_entry. */
16495
16496 static void
16497 delete_symtab_entry (void *p)
16498 {
16499 struct symtab_index_entry *entry = p;
16500 VEC_free (offset_type, entry->cu_indices);
16501 xfree (entry);
16502 }
16503
16504 /* Create a hash table holding symtab_index_entry objects. */
16505
16506 static htab_t
16507 create_symbol_hash_table (void)
16508 {
16509 return htab_create_alloc (100, hash_symtab_entry, eq_symtab_entry,
16510 delete_symtab_entry, xcalloc, xfree);
16511 }
16512
16513 /* Create a new mapped symtab object. */
16514
16515 static struct mapped_symtab *
16516 create_mapped_symtab (void)
16517 {
16518 struct mapped_symtab *symtab = XNEW (struct mapped_symtab);
16519 symtab->n_elements = 0;
16520 symtab->size = 1024;
16521 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16522 return symtab;
16523 }
16524
16525 /* Destroy a mapped_symtab. */
16526
16527 static void
16528 cleanup_mapped_symtab (void *p)
16529 {
16530 struct mapped_symtab *symtab = p;
16531 /* The contents of the array are freed when the other hash table is
16532 destroyed. */
16533 xfree (symtab->data);
16534 xfree (symtab);
16535 }
16536
16537 /* Find a slot in SYMTAB for the symbol NAME. Returns a pointer to
16538 the slot.
16539
16540 Function is used only during write_hash_table so no index format backward
16541 compatibility is needed. */
16542
16543 static struct symtab_index_entry **
16544 find_slot (struct mapped_symtab *symtab, const char *name)
16545 {
16546 offset_type index, step, hash = mapped_index_string_hash (INT_MAX, name);
16547
16548 index = hash & (symtab->size - 1);
16549 step = ((hash * 17) & (symtab->size - 1)) | 1;
16550
16551 for (;;)
16552 {
16553 if (!symtab->data[index] || !strcmp (name, symtab->data[index]->name))
16554 return &symtab->data[index];
16555 index = (index + step) & (symtab->size - 1);
16556 }
16557 }
16558
16559 /* Expand SYMTAB's hash table. */
16560
16561 static void
16562 hash_expand (struct mapped_symtab *symtab)
16563 {
16564 offset_type old_size = symtab->size;
16565 offset_type i;
16566 struct symtab_index_entry **old_entries = symtab->data;
16567
16568 symtab->size *= 2;
16569 symtab->data = XCNEWVEC (struct symtab_index_entry *, symtab->size);
16570
16571 for (i = 0; i < old_size; ++i)
16572 {
16573 if (old_entries[i])
16574 {
16575 struct symtab_index_entry **slot = find_slot (symtab,
16576 old_entries[i]->name);
16577 *slot = old_entries[i];
16578 }
16579 }
16580
16581 xfree (old_entries);
16582 }
16583
16584 /* Add an entry to SYMTAB. NAME is the name of the symbol. CU_INDEX
16585 is the index of the CU in which the symbol appears. */
16586
16587 static void
16588 add_index_entry (struct mapped_symtab *symtab, const char *name,
16589 offset_type cu_index)
16590 {
16591 struct symtab_index_entry **slot;
16592
16593 ++symtab->n_elements;
16594 if (4 * symtab->n_elements / 3 >= symtab->size)
16595 hash_expand (symtab);
16596
16597 slot = find_slot (symtab, name);
16598 if (!*slot)
16599 {
16600 *slot = XNEW (struct symtab_index_entry);
16601 (*slot)->name = name;
16602 (*slot)->cu_indices = NULL;
16603 }
16604 /* Don't push an index twice. Due to how we add entries we only
16605 have to check the last one. */
16606 if (VEC_empty (offset_type, (*slot)->cu_indices)
16607 || VEC_last (offset_type, (*slot)->cu_indices) != cu_index)
16608 VEC_safe_push (offset_type, (*slot)->cu_indices, cu_index);
16609 }
16610
16611 /* Add a vector of indices to the constant pool. */
16612
16613 static offset_type
16614 add_indices_to_cpool (htab_t symbol_hash_table, struct obstack *cpool,
16615 struct symtab_index_entry *entry)
16616 {
16617 void **slot;
16618
16619 slot = htab_find_slot (symbol_hash_table, entry, INSERT);
16620 if (!*slot)
16621 {
16622 offset_type len = VEC_length (offset_type, entry->cu_indices);
16623 offset_type val = MAYBE_SWAP (len);
16624 offset_type iter;
16625 int i;
16626
16627 *slot = entry;
16628 entry->index_offset = obstack_object_size (cpool);
16629
16630 obstack_grow (cpool, &val, sizeof (val));
16631 for (i = 0;
16632 VEC_iterate (offset_type, entry->cu_indices, i, iter);
16633 ++i)
16634 {
16635 val = MAYBE_SWAP (iter);
16636 obstack_grow (cpool, &val, sizeof (val));
16637 }
16638 }
16639 else
16640 {
16641 struct symtab_index_entry *old_entry = *slot;
16642 entry->index_offset = old_entry->index_offset;
16643 entry = old_entry;
16644 }
16645 return entry->index_offset;
16646 }
16647
16648 /* Write the mapped hash table SYMTAB to the obstack OUTPUT, with
16649 constant pool entries going into the obstack CPOOL. */
16650
16651 static void
16652 write_hash_table (struct mapped_symtab *symtab,
16653 struct obstack *output, struct obstack *cpool)
16654 {
16655 offset_type i;
16656 htab_t symbol_hash_table;
16657 htab_t str_table;
16658
16659 symbol_hash_table = create_symbol_hash_table ();
16660 str_table = create_strtab ();
16661
16662 /* We add all the index vectors to the constant pool first, to
16663 ensure alignment is ok. */
16664 for (i = 0; i < symtab->size; ++i)
16665 {
16666 if (symtab->data[i])
16667 add_indices_to_cpool (symbol_hash_table, cpool, symtab->data[i]);
16668 }
16669
16670 /* Now write out the hash table. */
16671 for (i = 0; i < symtab->size; ++i)
16672 {
16673 offset_type str_off, vec_off;
16674
16675 if (symtab->data[i])
16676 {
16677 str_off = add_string (str_table, cpool, symtab->data[i]->name);
16678 vec_off = symtab->data[i]->index_offset;
16679 }
16680 else
16681 {
16682 /* While 0 is a valid constant pool index, it is not valid
16683 to have 0 for both offsets. */
16684 str_off = 0;
16685 vec_off = 0;
16686 }
16687
16688 str_off = MAYBE_SWAP (str_off);
16689 vec_off = MAYBE_SWAP (vec_off);
16690
16691 obstack_grow (output, &str_off, sizeof (str_off));
16692 obstack_grow (output, &vec_off, sizeof (vec_off));
16693 }
16694
16695 htab_delete (str_table);
16696 htab_delete (symbol_hash_table);
16697 }
16698
16699 /* Struct to map psymtab to CU index in the index file. */
16700 struct psymtab_cu_index_map
16701 {
16702 struct partial_symtab *psymtab;
16703 unsigned int cu_index;
16704 };
16705
16706 static hashval_t
16707 hash_psymtab_cu_index (const void *item)
16708 {
16709 const struct psymtab_cu_index_map *map = item;
16710
16711 return htab_hash_pointer (map->psymtab);
16712 }
16713
16714 static int
16715 eq_psymtab_cu_index (const void *item_lhs, const void *item_rhs)
16716 {
16717 const struct psymtab_cu_index_map *lhs = item_lhs;
16718 const struct psymtab_cu_index_map *rhs = item_rhs;
16719
16720 return lhs->psymtab == rhs->psymtab;
16721 }
16722
16723 /* Helper struct for building the address table. */
16724 struct addrmap_index_data
16725 {
16726 struct objfile *objfile;
16727 struct obstack *addr_obstack;
16728 htab_t cu_index_htab;
16729
16730 /* Non-zero if the previous_* fields are valid.
16731 We can't write an entry until we see the next entry (since it is only then
16732 that we know the end of the entry). */
16733 int previous_valid;
16734 /* Index of the CU in the table of all CUs in the index file. */
16735 unsigned int previous_cu_index;
16736 /* Start address of the CU. */
16737 CORE_ADDR previous_cu_start;
16738 };
16739
16740 /* Write an address entry to OBSTACK. */
16741
16742 static void
16743 add_address_entry (struct objfile *objfile, struct obstack *obstack,
16744 CORE_ADDR start, CORE_ADDR end, unsigned int cu_index)
16745 {
16746 offset_type cu_index_to_write;
16747 char addr[8];
16748 CORE_ADDR baseaddr;
16749
16750 baseaddr = ANOFFSET (objfile->section_offsets, SECT_OFF_TEXT (objfile));
16751
16752 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, start - baseaddr);
16753 obstack_grow (obstack, addr, 8);
16754 store_unsigned_integer (addr, 8, BFD_ENDIAN_LITTLE, end - baseaddr);
16755 obstack_grow (obstack, addr, 8);
16756 cu_index_to_write = MAYBE_SWAP (cu_index);
16757 obstack_grow (obstack, &cu_index_to_write, sizeof (offset_type));
16758 }
16759
16760 /* Worker function for traversing an addrmap to build the address table. */
16761
16762 static int
16763 add_address_entry_worker (void *datap, CORE_ADDR start_addr, void *obj)
16764 {
16765 struct addrmap_index_data *data = datap;
16766 struct partial_symtab *pst = obj;
16767 offset_type cu_index;
16768 void **slot;
16769
16770 if (data->previous_valid)
16771 add_address_entry (data->objfile, data->addr_obstack,
16772 data->previous_cu_start, start_addr,
16773 data->previous_cu_index);
16774
16775 data->previous_cu_start = start_addr;
16776 if (pst != NULL)
16777 {
16778 struct psymtab_cu_index_map find_map, *map;
16779 find_map.psymtab = pst;
16780 map = htab_find (data->cu_index_htab, &find_map);
16781 gdb_assert (map != NULL);
16782 data->previous_cu_index = map->cu_index;
16783 data->previous_valid = 1;
16784 }
16785 else
16786 data->previous_valid = 0;
16787
16788 return 0;
16789 }
16790
16791 /* Write OBJFILE's address map to OBSTACK.
16792 CU_INDEX_HTAB is used to map addrmap entries to their CU indices
16793 in the index file. */
16794
16795 static void
16796 write_address_map (struct objfile *objfile, struct obstack *obstack,
16797 htab_t cu_index_htab)
16798 {
16799 struct addrmap_index_data addrmap_index_data;
16800
16801 /* When writing the address table, we have to cope with the fact that
16802 the addrmap iterator only provides the start of a region; we have to
16803 wait until the next invocation to get the start of the next region. */
16804
16805 addrmap_index_data.objfile = objfile;
16806 addrmap_index_data.addr_obstack = obstack;
16807 addrmap_index_data.cu_index_htab = cu_index_htab;
16808 addrmap_index_data.previous_valid = 0;
16809
16810 addrmap_foreach (objfile->psymtabs_addrmap, add_address_entry_worker,
16811 &addrmap_index_data);
16812
16813 /* It's highly unlikely the last entry (end address = 0xff...ff)
16814 is valid, but we should still handle it.
16815 The end address is recorded as the start of the next region, but that
16816 doesn't work here. To cope we pass 0xff...ff, this is a rare situation
16817 anyway. */
16818 if (addrmap_index_data.previous_valid)
16819 add_address_entry (objfile, obstack,
16820 addrmap_index_data.previous_cu_start, (CORE_ADDR) -1,
16821 addrmap_index_data.previous_cu_index);
16822 }
16823
16824 /* Add a list of partial symbols to SYMTAB. */
16825
16826 static void
16827 write_psymbols (struct mapped_symtab *symtab,
16828 htab_t psyms_seen,
16829 struct partial_symbol **psymp,
16830 int count,
16831 offset_type cu_index,
16832 int is_static)
16833 {
16834 for (; count-- > 0; ++psymp)
16835 {
16836 void **slot, *lookup;
16837
16838 if (SYMBOL_LANGUAGE (*psymp) == language_ada)
16839 error (_("Ada is not currently supported by the index"));
16840
16841 /* We only want to add a given psymbol once. However, we also
16842 want to account for whether it is global or static. So, we
16843 may add it twice, using slightly different values. */
16844 if (is_static)
16845 {
16846 uintptr_t val = 1 | (uintptr_t) *psymp;
16847
16848 lookup = (void *) val;
16849 }
16850 else
16851 lookup = *psymp;
16852
16853 /* Only add a given psymbol once. */
16854 slot = htab_find_slot (psyms_seen, lookup, INSERT);
16855 if (!*slot)
16856 {
16857 *slot = lookup;
16858 add_index_entry (symtab, SYMBOL_SEARCH_NAME (*psymp), cu_index);
16859 }
16860 }
16861 }
16862
16863 /* Write the contents of an ("unfinished") obstack to FILE. Throw an
16864 exception if there is an error. */
16865
16866 static void
16867 write_obstack (FILE *file, struct obstack *obstack)
16868 {
16869 if (fwrite (obstack_base (obstack), 1, obstack_object_size (obstack),
16870 file)
16871 != obstack_object_size (obstack))
16872 error (_("couldn't data write to file"));
16873 }
16874
16875 /* Unlink a file if the argument is not NULL. */
16876
16877 static void
16878 unlink_if_set (void *p)
16879 {
16880 char **filename = p;
16881 if (*filename)
16882 unlink (*filename);
16883 }
16884
16885 /* A helper struct used when iterating over debug_types. */
16886 struct signatured_type_index_data
16887 {
16888 struct objfile *objfile;
16889 struct mapped_symtab *symtab;
16890 struct obstack *types_list;
16891 htab_t psyms_seen;
16892 int cu_index;
16893 };
16894
16895 /* A helper function that writes a single signatured_type to an
16896 obstack. */
16897
16898 static int
16899 write_one_signatured_type (void **slot, void *d)
16900 {
16901 struct signatured_type_index_data *info = d;
16902 struct signatured_type *entry = (struct signatured_type *) *slot;
16903 struct dwarf2_per_cu_data *per_cu = &entry->per_cu;
16904 struct partial_symtab *psymtab = per_cu->v.psymtab;
16905 gdb_byte val[8];
16906
16907 write_psymbols (info->symtab,
16908 info->psyms_seen,
16909 info->objfile->global_psymbols.list
16910 + psymtab->globals_offset,
16911 psymtab->n_global_syms, info->cu_index,
16912 0);
16913 write_psymbols (info->symtab,
16914 info->psyms_seen,
16915 info->objfile->static_psymbols.list
16916 + psymtab->statics_offset,
16917 psymtab->n_static_syms, info->cu_index,
16918 1);
16919
16920 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->per_cu.offset);
16921 obstack_grow (info->types_list, val, 8);
16922 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->type_offset);
16923 obstack_grow (info->types_list, val, 8);
16924 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, entry->signature);
16925 obstack_grow (info->types_list, val, 8);
16926
16927 ++info->cu_index;
16928
16929 return 1;
16930 }
16931
16932 /* Create an index file for OBJFILE in the directory DIR. */
16933
16934 static void
16935 write_psymtabs_to_index (struct objfile *objfile, const char *dir)
16936 {
16937 struct cleanup *cleanup;
16938 char *filename, *cleanup_filename;
16939 struct obstack contents, addr_obstack, constant_pool, symtab_obstack;
16940 struct obstack cu_list, types_cu_list;
16941 int i;
16942 FILE *out_file;
16943 struct mapped_symtab *symtab;
16944 offset_type val, size_of_contents, total_len;
16945 struct stat st;
16946 char buf[8];
16947 htab_t psyms_seen;
16948 htab_t cu_index_htab;
16949 struct psymtab_cu_index_map *psymtab_cu_index_map;
16950
16951 if (!objfile->psymtabs || !objfile->psymtabs_addrmap)
16952 return;
16953
16954 if (dwarf2_per_objfile->using_index)
16955 error (_("Cannot use an index to create the index"));
16956
16957 if (VEC_length (dwarf2_section_info_def, dwarf2_per_objfile->types) > 1)
16958 error (_("Cannot make an index when the file has multiple .debug_types sections"));
16959
16960 if (stat (objfile->name, &st) < 0)
16961 perror_with_name (objfile->name);
16962
16963 filename = concat (dir, SLASH_STRING, lbasename (objfile->name),
16964 INDEX_SUFFIX, (char *) NULL);
16965 cleanup = make_cleanup (xfree, filename);
16966
16967 out_file = fopen (filename, "wb");
16968 if (!out_file)
16969 error (_("Can't open `%s' for writing"), filename);
16970
16971 cleanup_filename = filename;
16972 make_cleanup (unlink_if_set, &cleanup_filename);
16973
16974 symtab = create_mapped_symtab ();
16975 make_cleanup (cleanup_mapped_symtab, symtab);
16976
16977 obstack_init (&addr_obstack);
16978 make_cleanup_obstack_free (&addr_obstack);
16979
16980 obstack_init (&cu_list);
16981 make_cleanup_obstack_free (&cu_list);
16982
16983 obstack_init (&types_cu_list);
16984 make_cleanup_obstack_free (&types_cu_list);
16985
16986 psyms_seen = htab_create_alloc (100, htab_hash_pointer, htab_eq_pointer,
16987 NULL, xcalloc, xfree);
16988 make_cleanup_htab_delete (psyms_seen);
16989
16990 /* While we're scanning CU's create a table that maps a psymtab pointer
16991 (which is what addrmap records) to its index (which is what is recorded
16992 in the index file). This will later be needed to write the address
16993 table. */
16994 cu_index_htab = htab_create_alloc (100,
16995 hash_psymtab_cu_index,
16996 eq_psymtab_cu_index,
16997 NULL, xcalloc, xfree);
16998 make_cleanup_htab_delete (cu_index_htab);
16999 psymtab_cu_index_map = (struct psymtab_cu_index_map *)
17000 xmalloc (sizeof (struct psymtab_cu_index_map)
17001 * dwarf2_per_objfile->n_comp_units);
17002 make_cleanup (xfree, psymtab_cu_index_map);
17003
17004 /* The CU list is already sorted, so we don't need to do additional
17005 work here. Also, the debug_types entries do not appear in
17006 all_comp_units, but only in their own hash table. */
17007 for (i = 0; i < dwarf2_per_objfile->n_comp_units; ++i)
17008 {
17009 struct dwarf2_per_cu_data *per_cu
17010 = dwarf2_per_objfile->all_comp_units[i];
17011 struct partial_symtab *psymtab = per_cu->v.psymtab;
17012 gdb_byte val[8];
17013 struct psymtab_cu_index_map *map;
17014 void **slot;
17015
17016 write_psymbols (symtab,
17017 psyms_seen,
17018 objfile->global_psymbols.list + psymtab->globals_offset,
17019 psymtab->n_global_syms, i,
17020 0);
17021 write_psymbols (symtab,
17022 psyms_seen,
17023 objfile->static_psymbols.list + psymtab->statics_offset,
17024 psymtab->n_static_syms, i,
17025 1);
17026
17027 map = &psymtab_cu_index_map[i];
17028 map->psymtab = psymtab;
17029 map->cu_index = i;
17030 slot = htab_find_slot (cu_index_htab, map, INSERT);
17031 gdb_assert (slot != NULL);
17032 gdb_assert (*slot == NULL);
17033 *slot = map;
17034
17035 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->offset);
17036 obstack_grow (&cu_list, val, 8);
17037 store_unsigned_integer (val, 8, BFD_ENDIAN_LITTLE, per_cu->length);
17038 obstack_grow (&cu_list, val, 8);
17039 }
17040
17041 /* Dump the address map. */
17042 write_address_map (objfile, &addr_obstack, cu_index_htab);
17043
17044 /* Write out the .debug_type entries, if any. */
17045 if (dwarf2_per_objfile->signatured_types)
17046 {
17047 struct signatured_type_index_data sig_data;
17048
17049 sig_data.objfile = objfile;
17050 sig_data.symtab = symtab;
17051 sig_data.types_list = &types_cu_list;
17052 sig_data.psyms_seen = psyms_seen;
17053 sig_data.cu_index = dwarf2_per_objfile->n_comp_units;
17054 htab_traverse_noresize (dwarf2_per_objfile->signatured_types,
17055 write_one_signatured_type, &sig_data);
17056 }
17057
17058 obstack_init (&constant_pool);
17059 make_cleanup_obstack_free (&constant_pool);
17060 obstack_init (&symtab_obstack);
17061 make_cleanup_obstack_free (&symtab_obstack);
17062 write_hash_table (symtab, &symtab_obstack, &constant_pool);
17063
17064 obstack_init (&contents);
17065 make_cleanup_obstack_free (&contents);
17066 size_of_contents = 6 * sizeof (offset_type);
17067 total_len = size_of_contents;
17068
17069 /* The version number. */
17070 val = MAYBE_SWAP (6);
17071 obstack_grow (&contents, &val, sizeof (val));
17072
17073 /* The offset of the CU list from the start of the file. */
17074 val = MAYBE_SWAP (total_len);
17075 obstack_grow (&contents, &val, sizeof (val));
17076 total_len += obstack_object_size (&cu_list);
17077
17078 /* The offset of the types CU list from the start of the file. */
17079 val = MAYBE_SWAP (total_len);
17080 obstack_grow (&contents, &val, sizeof (val));
17081 total_len += obstack_object_size (&types_cu_list);
17082
17083 /* The offset of the address table from the start of the file. */
17084 val = MAYBE_SWAP (total_len);
17085 obstack_grow (&contents, &val, sizeof (val));
17086 total_len += obstack_object_size (&addr_obstack);
17087
17088 /* The offset of the symbol table from the start of the file. */
17089 val = MAYBE_SWAP (total_len);
17090 obstack_grow (&contents, &val, sizeof (val));
17091 total_len += obstack_object_size (&symtab_obstack);
17092
17093 /* The offset of the constant pool from the start of the file. */
17094 val = MAYBE_SWAP (total_len);
17095 obstack_grow (&contents, &val, sizeof (val));
17096 total_len += obstack_object_size (&constant_pool);
17097
17098 gdb_assert (obstack_object_size (&contents) == size_of_contents);
17099
17100 write_obstack (out_file, &contents);
17101 write_obstack (out_file, &cu_list);
17102 write_obstack (out_file, &types_cu_list);
17103 write_obstack (out_file, &addr_obstack);
17104 write_obstack (out_file, &symtab_obstack);
17105 write_obstack (out_file, &constant_pool);
17106
17107 fclose (out_file);
17108
17109 /* We want to keep the file, so we set cleanup_filename to NULL
17110 here. See unlink_if_set. */
17111 cleanup_filename = NULL;
17112
17113 do_cleanups (cleanup);
17114 }
17115
17116 /* Implementation of the `save gdb-index' command.
17117
17118 Note that the file format used by this command is documented in the
17119 GDB manual. Any changes here must be documented there. */
17120
17121 static void
17122 save_gdb_index_command (char *arg, int from_tty)
17123 {
17124 struct objfile *objfile;
17125
17126 if (!arg || !*arg)
17127 error (_("usage: save gdb-index DIRECTORY"));
17128
17129 ALL_OBJFILES (objfile)
17130 {
17131 struct stat st;
17132
17133 /* If the objfile does not correspond to an actual file, skip it. */
17134 if (stat (objfile->name, &st) < 0)
17135 continue;
17136
17137 dwarf2_per_objfile = objfile_data (objfile, dwarf2_objfile_data_key);
17138 if (dwarf2_per_objfile)
17139 {
17140 volatile struct gdb_exception except;
17141
17142 TRY_CATCH (except, RETURN_MASK_ERROR)
17143 {
17144 write_psymtabs_to_index (objfile, arg);
17145 }
17146 if (except.reason < 0)
17147 exception_fprintf (gdb_stderr, except,
17148 _("Error while writing index for `%s': "),
17149 objfile->name);
17150 }
17151 }
17152 }
17153
17154 \f
17155
17156 int dwarf2_always_disassemble;
17157
17158 static void
17159 show_dwarf2_always_disassemble (struct ui_file *file, int from_tty,
17160 struct cmd_list_element *c, const char *value)
17161 {
17162 fprintf_filtered (file,
17163 _("Whether to always disassemble "
17164 "DWARF expressions is %s.\n"),
17165 value);
17166 }
17167
17168 static void
17169 show_check_physname (struct ui_file *file, int from_tty,
17170 struct cmd_list_element *c, const char *value)
17171 {
17172 fprintf_filtered (file,
17173 _("Whether to check \"physname\" is %s.\n"),
17174 value);
17175 }
17176
17177 void _initialize_dwarf2_read (void);
17178
17179 void
17180 _initialize_dwarf2_read (void)
17181 {
17182 struct cmd_list_element *c;
17183
17184 dwarf2_objfile_data_key
17185 = register_objfile_data_with_cleanup (NULL, dwarf2_per_objfile_free);
17186
17187 add_prefix_cmd ("dwarf2", class_maintenance, set_dwarf2_cmd, _("\
17188 Set DWARF 2 specific variables.\n\
17189 Configure DWARF 2 variables such as the cache size"),
17190 &set_dwarf2_cmdlist, "maintenance set dwarf2 ",
17191 0/*allow-unknown*/, &maintenance_set_cmdlist);
17192
17193 add_prefix_cmd ("dwarf2", class_maintenance, show_dwarf2_cmd, _("\
17194 Show DWARF 2 specific variables\n\
17195 Show DWARF 2 variables such as the cache size"),
17196 &show_dwarf2_cmdlist, "maintenance show dwarf2 ",
17197 0/*allow-unknown*/, &maintenance_show_cmdlist);
17198
17199 add_setshow_zinteger_cmd ("max-cache-age", class_obscure,
17200 &dwarf2_max_cache_age, _("\
17201 Set the upper bound on the age of cached dwarf2 compilation units."), _("\
17202 Show the upper bound on the age of cached dwarf2 compilation units."), _("\
17203 A higher limit means that cached compilation units will be stored\n\
17204 in memory longer, and more total memory will be used. Zero disables\n\
17205 caching, which can slow down startup."),
17206 NULL,
17207 show_dwarf2_max_cache_age,
17208 &set_dwarf2_cmdlist,
17209 &show_dwarf2_cmdlist);
17210
17211 add_setshow_boolean_cmd ("always-disassemble", class_obscure,
17212 &dwarf2_always_disassemble, _("\
17213 Set whether `info address' always disassembles DWARF expressions."), _("\
17214 Show whether `info address' always disassembles DWARF expressions."), _("\
17215 When enabled, DWARF expressions are always printed in an assembly-like\n\
17216 syntax. When disabled, expressions will be printed in a more\n\
17217 conversational style, when possible."),
17218 NULL,
17219 show_dwarf2_always_disassemble,
17220 &set_dwarf2_cmdlist,
17221 &show_dwarf2_cmdlist);
17222
17223 add_setshow_zinteger_cmd ("dwarf2-die", no_class, &dwarf2_die_debug, _("\
17224 Set debugging of the dwarf2 DIE reader."), _("\
17225 Show debugging of the dwarf2 DIE reader."), _("\
17226 When enabled (non-zero), DIEs are dumped after they are read in.\n\
17227 The value is the maximum depth to print."),
17228 NULL,
17229 NULL,
17230 &setdebuglist, &showdebuglist);
17231
17232 add_setshow_boolean_cmd ("check-physname", no_class, &check_physname, _("\
17233 Set cross-checking of \"physname\" code against demangler."), _("\
17234 Show cross-checking of \"physname\" code against demangler."), _("\
17235 When enabled, GDB's internal \"physname\" code is checked against\n\
17236 the demangler."),
17237 NULL, show_check_physname,
17238 &setdebuglist, &showdebuglist);
17239
17240 c = add_cmd ("gdb-index", class_files, save_gdb_index_command,
17241 _("\
17242 Save a gdb-index file.\n\
17243 Usage: save gdb-index DIRECTORY"),
17244 &save_cmdlist);
17245 set_cmd_completer (c, filename_completer);
17246 }